tcmodules.pas 946 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909
  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, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. JSONNewLine = {$IFDEF Windows}'\r\n'{$ELSE}'\n'{$ENDIF};
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. const
  34. SrcMarker: array[TSrcMarkerKind] of char = (
  35. '#', // mkLabel
  36. '@', // mkResolverReference
  37. '=' // mkDirectReference
  38. );
  39. type
  40. PSrcMarker = ^TSrcMarker;
  41. TSrcMarker = record
  42. Kind: TSrcMarkerKind;
  43. Filename: string;
  44. Row: integer;
  45. StartCol, EndCol: integer; // token start, end column
  46. Identifier: string;
  47. Next: PSrcMarker;
  48. end;
  49. TSystemUnitPart = (
  50. supTObject,
  51. supTVarRec,
  52. supTypeInfo,
  53. supTInterfacedObject,
  54. supWriteln
  55. );
  56. TSystemUnitParts = set of TSystemUnitPart;
  57. { TTestHintMessage }
  58. TTestHintMessage = class
  59. public
  60. Id: int64;
  61. MsgType: TMessageType;
  62. MsgNumber: integer;
  63. Msg: string;
  64. SourcePos: TPasSourcePos;
  65. end;
  66. TTestResolverReferenceData = record
  67. Filename: string;
  68. Row: integer;
  69. StartCol: integer;
  70. EndCol: integer;
  71. Found: TFPList; // list of TPasElement at this token
  72. end;
  73. PTestResolverReferenceData = ^TTestResolverReferenceData;
  74. { TTestPasParser }
  75. TTestPasParser = Class(TPasParser)
  76. end;
  77. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  78. { TTestEnginePasResolver }
  79. TTestEnginePasResolver = class(TPas2JsResolver)
  80. private
  81. FFilename: string;
  82. FModule: TPasModule;
  83. FOnFindUnit: TOnFindUnit;
  84. FParser: TTestPasParser;
  85. FStreamResolver: TStreamResolver;
  86. FScanner: TPas2jsPasScanner;
  87. FSource: string;
  88. procedure SetModule(const AValue: TPasModule);
  89. public
  90. destructor Destroy; override;
  91. function CreateElement(AClass: TPTreeElement; const AName: String;
  92. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  93. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  94. overload; override;
  95. function FindUnit(const AName, InFilename: String; NameExpr,
  96. InFileExpr: TPasExpr): TPasModule; override;
  97. procedure UsedInterfacesFinished(Section: TPasSection); override;
  98. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  99. property Filename: string read FFilename write FFilename;
  100. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  101. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  102. property Parser: TTestPasParser read FParser write FParser;
  103. property Source: string read FSource write FSource;
  104. property Module: TPasModule read FModule write SetModule;
  105. end;
  106. { TCustomTestModule }
  107. TCustomTestModule = Class(TTestCase)
  108. private
  109. FWithTypeInfo: boolean;
  110. FSource: TStringList;
  111. FSkipTests: boolean;
  112. FScanner: TPas2jsPasScanner;
  113. FResolvers: TObjectList;// list of TTestEnginePasResolver
  114. FPasProgram: TPasProgram;
  115. FPasLibrary: TPasLibrary;
  116. FParser: TTestPasParser;
  117. FModule: TPasModule;
  118. FJSSource: TStringList;
  119. FJSRegModuleCall: TJSCallExpression;
  120. FJSModuleSrc: TJSSourceElements;
  121. FJSModuleCallArgs: TJSArguments;
  122. FJSModule: TJSSourceElements;
  123. FJSInterfaceUses: TJSArrayLiteral;
  124. FJSInitBody: TJSFunctionBody;
  125. FJSImplentationUses: TJSArrayLiteral;
  126. FJSImplementationUses: TJSArrayLiteral;
  127. FHub: TPas2JSResolverHub;
  128. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  129. FHintMsgs: TObjectList; // list of TTestHintMessage
  130. FFirstPasStatement: TPasImplBlock;
  131. FFileResolver: TStreamResolver;
  132. FFilename: string;
  133. FExpectedErrorNumber: integer;
  134. FExpectedErrorMsg: string;
  135. FExpectedErrorClass: ExceptClass;
  136. FEngine: TTestEnginePasResolver;
  137. FConverter: TPasToJSConverter;
  138. {$IFDEF EnablePasTreeGlobalRefCount}
  139. FElementRefCountAtSetup: int64;
  140. {$ENDIF}
  141. procedure FreeSrcMarkers;
  142. function GetResolverCount: integer;
  143. function GetResolvers(Index: integer): TTestEnginePasResolver;
  144. function GetMsgCount: integer;
  145. function GetMsgs(Index: integer): TTestHintMessage;
  146. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  147. procedure OnParserLog(Sender: TObject; const Msg: String);
  148. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  149. procedure OnScannerLog(Sender: TObject; const Msg: String);
  150. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  151. procedure OnFindReference(El: TPasElement; FindData: pointer);
  152. procedure SetWithTypeInfo(const AValue: boolean);
  153. protected
  154. procedure SetUp; override;
  155. function CreateConverter: TPasToJSConverter; virtual;
  156. function LoadUnit(const aUnitName: String): TPasModule;
  157. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  158. procedure TearDown; override;
  159. Procedure Add(Line: string); virtual;
  160. Procedure Add(const Lines: array of string);
  161. Procedure StartParsing; virtual;
  162. procedure ParseModuleQueue; virtual;
  163. procedure ParseModule; virtual;
  164. procedure ParseProgram; virtual;
  165. procedure ParseLibrary; virtual;
  166. procedure ParseUnit; virtual;
  167. protected
  168. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  169. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  172. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  173. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  174. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  175. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  178. procedure ConvertModule; virtual;
  179. procedure ConvertProgram; virtual;
  180. procedure ConvertLibrary; virtual;
  181. procedure ConvertUnit; virtual;
  182. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  183. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  184. function GetDottedIdentifier(El: TJSElement): string;
  185. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  186. ImplStatements: string = ''); virtual;
  187. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  188. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  189. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  190. procedure CheckReferenceDirectives; virtual;
  191. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  192. Msg: string; Marker: PSrcMarker = nil); virtual;
  193. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  194. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  197. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  198. function IsErrorExpected(E: Exception): boolean;
  199. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  200. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  201. procedure HandleScannerError(E: EScannerError);
  202. procedure HandleParserError(E: EParserError);
  203. procedure HandlePasResolveError(E: EPasResolve);
  204. procedure HandlePas2JSError(E: EPas2JS);
  205. procedure HandleException(E: Exception);
  206. procedure FailException(E: Exception);
  207. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  208. function IndexOfResolver(const Filename: string): integer;
  209. function GetResolver(const Filename: string): TTestEnginePasResolver;
  210. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  211. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  212. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  213. function FindSrcLabel(const Identifier: string): PSrcMarker;
  214. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  215. function GetDefaultNamespace: string;
  216. property PasProgram: TPasProgram Read FPasProgram;
  217. property PasLibrary: TPasLibrary Read FPasLibrary;
  218. property ResolverEngine: TTestEnginePasResolver read FEngine;
  219. property Filename: string read FFilename;
  220. Property Module: TPasModule Read FModule;
  221. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  222. property Converter: TPasToJSConverter read FConverter;
  223. property JSSource: TStringList read FJSSource;
  224. property JSModule: TJSSourceElements read FJSModule;
  225. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  226. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  227. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  228. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  229. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  230. property JSInitBody: TJSFunctionBody read FJSInitBody;
  231. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  232. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  233. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  234. property SkipTests: boolean read FSkipTests write FSkipTests;
  235. public
  236. constructor Create; override;
  237. destructor Destroy; override;
  238. property Hub: TPas2JSResolverHub read FHub;
  239. property Source: TStringList read FSource;
  240. property FileResolver: TStreamResolver read FFileResolver;
  241. property Scanner: TPas2jsPasScanner read FScanner;
  242. property Parser: TTestPasParser read FParser;
  243. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  244. property ResolverCount: integer read GetResolverCount;
  245. property MsgCount: integer read GetMsgCount;
  246. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  247. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  248. end;
  249. { TTestModule }
  250. TTestModule = class(TCustomTestModule)
  251. Published
  252. Procedure TestReservedWords;
  253. // program, units, includes
  254. Procedure TestEmptyProgram;
  255. Procedure TestEmptyProgramUseStrict;
  256. Procedure TestEmptyUnit;
  257. Procedure TestEmptyUnitUseStrict;
  258. Procedure TestDottedUnitNames;
  259. Procedure TestDottedUnitNameImpl;
  260. Procedure TestDottedUnitExpr;
  261. Procedure Test_ModeFPCFail;
  262. Procedure Test_ModeSwitchCBlocksFail;
  263. Procedure TestUnit_UseSystem;
  264. Procedure TestUnit_Intf1Impl2Intf1;
  265. Procedure TestIncludeVersion;
  266. // vars/const
  267. Procedure TestVarInt;
  268. Procedure TestVarBaseTypes;
  269. Procedure TestBaseTypeSingleFail;
  270. Procedure TestBaseTypeExtendedFail;
  271. Procedure TestConstBaseTypes;
  272. Procedure TestUnitImplVars;
  273. Procedure TestUnitImplConsts;
  274. Procedure TestUnitImplRecord;
  275. Procedure TestRenameJSNameConflict;
  276. Procedure TestLocalConst;
  277. Procedure TestVarExternal;
  278. Procedure TestVarExternalOtherUnit;
  279. Procedure TestVarAbsoluteFail;
  280. Procedure TestConstExternal;
  281. // numbers
  282. Procedure TestDouble;
  283. Procedure TestDoubleSmall;
  284. Procedure TestInteger;
  285. Procedure TestIntegerRange;
  286. Procedure TestIntegerTypecasts;
  287. Procedure TestInteger_BitwiseShrNativeInt;
  288. Procedure TestInteger_BitwiseShlNativeInt;
  289. Procedure TestInteger_SystemFunc;
  290. Procedure TestInteger_AssignOutsideConst;
  291. Procedure TestCurrency;
  292. Procedure TestForBoolDo;
  293. Procedure TestForIntDo;
  294. Procedure TestForIntInDo;
  295. // strings
  296. Procedure TestCharConst;
  297. Procedure TestChar_Compare;
  298. Procedure TestChar_BuiltInProcs;
  299. Procedure TestStringConst;
  300. Procedure TestStringConst_InvalidUTF16;
  301. Procedure TestStringConstSurrogate;
  302. Procedure TestStringConst_Multiline;
  303. Procedure TestString_Length;
  304. Procedure TestString_Compare;
  305. Procedure TestString_SetLength;
  306. Procedure TestString_CharAt;
  307. Procedure TestStringHMinusFail;
  308. Procedure TestStr;
  309. Procedure TestBaseType_AnsiStringFail;
  310. Procedure TestBaseType_WideStringFail;
  311. Procedure TestBaseType_ShortStringFail;
  312. Procedure TestBaseType_RawByteStringFail;
  313. Procedure TestTypeShortstring_Fail;
  314. Procedure TestCharSet_Custom;
  315. Procedure TestWideChar;
  316. Procedure TestForCharDo;
  317. Procedure TestForCharInDo;
  318. // alias types
  319. Procedure TestAliasTypeRef;
  320. Procedure TestTypeCast_BaseTypes;
  321. Procedure TestTypeCast_AliasBaseTypes;
  322. // functions
  323. Procedure TestEmptyProc;
  324. Procedure TestProcOneParam;
  325. Procedure TestFunctionWithoutParams;
  326. Procedure TestProcedureWithoutParams;
  327. Procedure TestPrgProcVar;
  328. Procedure TestProcTwoArgs;
  329. Procedure TestProc_DefaultValue;
  330. Procedure TestUnitProcVar;
  331. Procedure TestImplProc;
  332. Procedure TestFunctionResult;
  333. Procedure TestNestedProc;
  334. Procedure TestNestedProc_ResultString;
  335. Procedure TestForwardProc;
  336. Procedure TestNestedForwardProc;
  337. Procedure TestAssignFunctionResult;
  338. Procedure TestFunctionResultInCondition;
  339. Procedure TestFunctionResultInForLoop;
  340. Procedure TestFunctionResultInTypeCast;
  341. Procedure TestExit;
  342. Procedure TestExit_ResultInFinally;
  343. Procedure TestBreak;
  344. Procedure TestBreakAsVar;
  345. Procedure TestContinue;
  346. Procedure TestProc_External;
  347. Procedure TestProc_ExternalOtherUnit;
  348. Procedure TestProc_Asm;
  349. Procedure TestProc_AsmSubBlock;
  350. Procedure TestProc_Assembler;
  351. Procedure TestProc_VarParam;
  352. Procedure TestProc_VarParamString;
  353. Procedure TestProc_VarParamV;
  354. Procedure TestProc_Overload;
  355. Procedure TestProc_OverloadForward;
  356. Procedure TestProc_OverloadIntfImpl;
  357. Procedure TestProc_OverloadNested;
  358. Procedure TestProc_OverloadNestedForward;
  359. Procedure TestProc_OverloadUnitCycle;
  360. Procedure TestProc_Varargs;
  361. Procedure TestProc_ConstOrder;
  362. Procedure TestProc_DuplicateConst;
  363. Procedure TestProc_LocalVarAbsolute;
  364. Procedure TestProc_ResultAbsolute;
  365. Procedure TestProc_LocalVarInit;
  366. Procedure TestProc_ReservedWords;
  367. Procedure TestProc_ConstRefWord;
  368. // anonymous functions
  369. Procedure TestAnonymousProc_Assign_ObjFPC;
  370. Procedure TestAnonymousProc_Assign_Delphi;
  371. Procedure TestAnonymousProc_Arg;
  372. Procedure TestAnonymousProc_Typecast;
  373. Procedure TestAnonymousProc_With;
  374. Procedure TestAnonymousProc_ExceptOn;
  375. Procedure TestAnonymousProc_Nested;
  376. Procedure TestAnonymousProc_NestedAssignResult;
  377. Procedure TestAnonymousProc_Class;
  378. Procedure TestAnonymousProc_ForLoop;
  379. Procedure TestAnonymousProc_AsmDelphi;
  380. // enums, sets
  381. Procedure TestEnum_Name;
  382. Procedure TestEnum_Number;
  383. Procedure TestEnum_ConstFail;
  384. Procedure TestEnum_Functions;
  385. Procedure TestEnumRg_Functions;
  386. Procedure TestEnum_AsParams;
  387. Procedure TestEnumRange_Array;
  388. Procedure TestEnum_ForIn;
  389. Procedure TestEnum_ScopedNumber;
  390. Procedure TestEnum_InFunction;
  391. Procedure TestEnum_Name_Anonymous_Unit;
  392. Procedure TestSet_Enum;
  393. Procedure TestSet_Operators;
  394. Procedure TestSet_Operator_In;
  395. Procedure TestSet_Functions;
  396. Procedure TestSet_PassAsArgClone;
  397. Procedure TestSet_AsParams;
  398. Procedure TestSet_Property;
  399. Procedure TestSet_EnumConst;
  400. Procedure TestSet_IntConst;
  401. Procedure TestSet_IntRange;
  402. Procedure TestSet_AnonymousEnumType;
  403. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  404. Procedure TestSet_ConstEnum;
  405. Procedure TestSet_ConstChar;
  406. Procedure TestSet_ConstInt;
  407. Procedure TestSet_InFunction;
  408. Procedure TestSet_ForIn;
  409. // statements
  410. Procedure TestNestBegin;
  411. Procedure TestIncDec;
  412. Procedure TestLoHiFpcMode;
  413. Procedure TestLoHiDelphiMode;
  414. Procedure TestAssignments;
  415. Procedure TestArithmeticOperators1;
  416. Procedure TestMultiAdd;
  417. Procedure TestLogicalOperators;
  418. Procedure TestBitwiseOperators;
  419. Procedure TestBitwiseOperatorsLongword;
  420. Procedure TestFunctionInt;
  421. Procedure TestFunctionString;
  422. Procedure TestIfThen;
  423. Procedure TestForLoop;
  424. Procedure TestForLoopInsideFunction;
  425. Procedure TestForLoop_ReadVarAfter;
  426. Procedure TestForLoop_Nested;
  427. Procedure TestRepeatUntil;
  428. Procedure TestAsmBlock;
  429. Procedure TestAsmPas_Impl; // ToDo
  430. Procedure TestTryFinally;
  431. Procedure TestTryExcept;
  432. Procedure TestTryExcept_ReservedWords;
  433. Procedure TestIfThenRaiseElse;
  434. Procedure TestCaseOf;
  435. Procedure TestCaseOf_UseSwitch;
  436. Procedure TestCaseOfNoElse;
  437. Procedure TestCaseOfNoElse_UseSwitch;
  438. Procedure TestCaseOfRange;
  439. Procedure TestCaseOfString;
  440. Procedure TestCaseOfChar;
  441. Procedure TestCaseOfExternalClassConst;
  442. Procedure TestDebugger;
  443. // arrays
  444. Procedure TestArray_Dynamic;
  445. Procedure TestArray_Dynamic_Nil;
  446. Procedure TestArray_DynMultiDimensional;
  447. Procedure TestArray_DynamicAssign;
  448. Procedure TestArray_StaticInt;
  449. Procedure TestArray_StaticBool;
  450. Procedure TestArray_StaticChar;
  451. Procedure TestArray_StaticMultiDim;
  452. Procedure TestArray_StaticInFunction;
  453. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  454. Procedure TestArrayOfRecord;
  455. Procedure TestArray_StaticRecord;
  456. Procedure TestArrayOfSet;
  457. Procedure TestArray_DynAsParam;
  458. Procedure TestArray_StaticAsParam;
  459. Procedure TestArrayElement_AsParams;
  460. Procedure TestArrayElementFromFuncResult_AsParams;
  461. Procedure TestArrayEnumTypeRange;
  462. Procedure TestArray_SetLengthOutArg;
  463. Procedure TestArray_SetLengthProperty;
  464. Procedure TestArray_SetLengthMultiDim;
  465. Procedure TestArray_SetLengthDynOfStatic;
  466. Procedure TestArray_OpenArrayOfString;
  467. Procedure TestArray_ArrayOfCharAssignString;
  468. Procedure TestArray_ConstRef;
  469. Procedure TestArray_Concat;
  470. Procedure TestArray_Concat_Append;
  471. Procedure TestArray_Concat_Append_Var;
  472. Procedure TestArray_Copy;
  473. Procedure TestArray_InsertDelete;
  474. Procedure TestArray_Add_Append;
  475. Procedure TestArray_DynArrayConstObjFPC;
  476. Procedure TestArray_DynArrayConstDelphi;
  477. Procedure TestArray_ArrayLitAsParam;
  478. Procedure TestArray_ArrayLitMultiDimAsParam;
  479. Procedure TestArray_ArrayLitStaticAsParam;
  480. Procedure TestArray_ForInArrOfString;
  481. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  482. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  483. Procedure TestArrayOfConst_TVarRec;
  484. Procedure TestArrayOfConst_PassBaseTypes;
  485. Procedure TestArrayOfConst_PassObj;
  486. // record
  487. Procedure TestRecord_Empty;
  488. Procedure TestRecord_Var;
  489. Procedure TestRecord_VarExternal;
  490. Procedure TestRecord_WithDo;
  491. Procedure TestRecord_Assign;
  492. Procedure TestRecord_AsParams;
  493. Procedure TestRecord_ConstRef;
  494. Procedure TestRecordElement_AsParams;
  495. Procedure TestRecordElementFromFuncResult_AsParams;
  496. Procedure TestRecordElementFromWith_AsParams;
  497. Procedure TestRecord_Equal;
  498. Procedure TestRecord_JSValue;
  499. Procedure TestRecord_VariantFail;
  500. Procedure TestRecord_FieldArray;
  501. Procedure TestRecord_Const;
  502. Procedure TestRecord_TypecastFail;
  503. Procedure TestRecord_InFunction;
  504. Procedure TestRecord_ArrayConstMultiline;
  505. // anonymous record
  506. Procedure TestRecordAnonym_Field;
  507. Procedure TestRecordAnonym_Assign;
  508. Procedure TestRecordAnonym_Nested;
  509. Procedure TestRecordAnonym_Const;
  510. Procedure TestRecordAnonym_InFunction;
  511. // advanced record
  512. Procedure TestAdvRecord_Function;
  513. Procedure TestAdvRecord_Property;
  514. Procedure TestAdvRecord_PropertyDefault;
  515. Procedure TestAdvRecord_Property_ClassMethod;
  516. Procedure TestAdvRecord_Const;
  517. Procedure TestAdvRecord_ExternalField;
  518. Procedure TestAdvRecord_SubRecord;
  519. Procedure TestAdvRecord_SubClass;
  520. Procedure TestAdvRecord_SubInterfaceFail;
  521. Procedure TestAdvRecord_Constructor;
  522. Procedure TestAdvRecord_ClassConstructor_Program;
  523. Procedure TestAdvRecord_ClassConstructor_Unit;
  524. // classes
  525. Procedure TestClass_TObjectDefaultConstructor;
  526. Procedure TestClass_TObjectConstructorWithParams;
  527. Procedure TestClass_TObjectConstructorWithDefaultParam;
  528. Procedure TestClass_Var;
  529. Procedure TestClass_Method;
  530. Procedure TestClass_Implementation;
  531. Procedure TestClass_Inheritance;
  532. Procedure TestClass_TypeAlias;
  533. Procedure TestClass_AbstractMethod;
  534. Procedure TestClass_CallInherited_ProcNoParams;
  535. Procedure TestClass_CallInherited_WithParams;
  536. Procedure TestClasS_CallInheritedConstructor;
  537. Procedure TestClass_ClassVar_Assign;
  538. Procedure TestClass_CallClassMethod;
  539. Procedure TestClass_CallClassMethodStatic;
  540. Procedure TestClass_Property;
  541. Procedure TestClass_Property_ClassMethod;
  542. Procedure TestClass_Property_ClassMethodStatic;
  543. Procedure TestClass_Property_Indexed;
  544. Procedure TestClass_Property_IndexSpec;
  545. Procedure TestClass_PropertyOfTypeArray;
  546. Procedure TestClass_PropertyDefault;
  547. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  548. //Procedure TestClass_PropertyDefault;
  549. Procedure TestClass_PropertyOverride;
  550. Procedure TestClass_PropertyIncVisibility;
  551. Procedure TestClass_Assigned;
  552. Procedure TestClass_WithClassDoCreate;
  553. Procedure TestClass_WithClassInstDoProperty;
  554. Procedure TestClass_WithClassInstDoPropertyWithParams;
  555. Procedure TestClass_WithClassInstDoFunc;
  556. Procedure TestClass_TypeCast;
  557. Procedure TestClass_TypeCastUntypedParam;
  558. Procedure TestClass_Overloads;
  559. Procedure TestClass_OverloadsAncestor;
  560. Procedure TestClass_OverloadConstructor;
  561. Procedure TestClass_OverloadDelphiOverride;
  562. Procedure TestClass_ReintroduceVarDelphi;
  563. Procedure TestClass_ReintroducedVar;
  564. Procedure TestClass_RaiseDescendant;
  565. Procedure TestClass_ExternalMethod;
  566. Procedure TestClass_ExternalVirtualNameMismatchFail;
  567. Procedure TestClass_ExternalOverrideFail;
  568. Procedure TestClass_ExternalVar;
  569. Procedure TestClass_Const;
  570. Procedure TestClass_ConstEnum;
  571. Procedure TestClass_LocalConstDuplicate_Prg;
  572. Procedure TestClass_LocalConstDuplicate_Unit;
  573. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  574. Procedure TestClass_LocalVarSelfFail;
  575. Procedure TestClass_ArgSelfFail;
  576. Procedure TestClass_NestedProcSelf;
  577. Procedure TestClass_NestedProcSelf2;
  578. Procedure TestClass_NestedProcClassSelf;
  579. Procedure TestClass_NestedProcCallInherited;
  580. Procedure TestClass_TObjectFree;
  581. Procedure TestClass_TObjectFree_VarArg;
  582. Procedure TestClass_TObjectFreeNewInstance;
  583. Procedure TestClass_TObjectFreeLowerCase;
  584. Procedure TestClass_TObjectFreeFunctionFail;
  585. Procedure TestClass_TObjectFreePropertyFail;
  586. Procedure TestClass_ForIn;
  587. Procedure TestClass_DispatchMessage;
  588. Procedure TestClass_Message_DuplicateIntFail;
  589. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  590. // class of
  591. Procedure TestClassOf_Create;
  592. Procedure TestClassOf_Call;
  593. Procedure TestClassOf_Assign;
  594. Procedure TestClassOf_Is;
  595. Procedure TestClassOf_Compare;
  596. Procedure TestClassOf_ClassVar;
  597. Procedure TestClassOf_ClassMethod;
  598. Procedure TestClassOf_ClassProperty;
  599. Procedure TestClassOf_ClassMethodSelf;
  600. Procedure TestClassOf_TypeCast;
  601. Procedure TestClassOf_ImplicitFunctionCall;
  602. Procedure TestClassOf_Const;
  603. // nested class
  604. Procedure TestNestedClass_Alias;
  605. Procedure TestNestedClass_Record;
  606. Procedure TestNestedClass_Class;
  607. Procedure TestNestedClass_CallInherited;
  608. // external class
  609. Procedure TestExternalClass_Var;
  610. Procedure TestExternalClass_Const;
  611. Procedure TestExternalClass_Dollar;
  612. Procedure TestExternalClass_DuplicateVarFail;
  613. Procedure TestExternalClass_Method;
  614. Procedure TestExternalClass_ClassMethod;
  615. Procedure TestExternalClass_ClassMethodStatic;
  616. Procedure TestExternalClass_FunctionResultInTypeCast;
  617. Procedure TestExternalClass_NonExternalOverride;
  618. Procedure TestExternalClass_OverloadHint;
  619. Procedure TestExternalClass_SameNamePublishedProperty;
  620. Procedure TestExternalClass_Property;
  621. Procedure TestExternalClass_PropertyDate;
  622. Procedure TestExternalClass_ClassProperty;
  623. Procedure TestExternalClass_ClassOf;
  624. Procedure TestExternalClass_ClassOtherUnit;
  625. Procedure TestExternalClass_Is;
  626. Procedure TestExternalClass_As;
  627. Procedure TestExternalClass_DestructorFail;
  628. Procedure TestExternalClass_New;
  629. Procedure TestExternalClass_ClassOf_New;
  630. Procedure TestExternalClass_FuncClassOf_New;
  631. Procedure TestExternalClass_New_PasClassFail;
  632. Procedure TestExternalClass_New_PasClassBracketsFail;
  633. Procedure TestExternalClass_NewExtName;
  634. Procedure TestExternalClass_Constructor;
  635. Procedure TestExternalClass_ConstructorBrackets;
  636. Procedure TestExternalClass_LocalConstSameName;
  637. Procedure TestExternalClass_ReintroduceOverload;
  638. Procedure TestExternalClass_Inherited;
  639. Procedure TestExternalClass_PascalAncestorFail;
  640. Procedure TestExternalClass_NewInstance;
  641. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  642. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  643. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  644. Procedure TestExternalClass_JSFunctionPasDescendant;
  645. Procedure TestExternalClass_PascalProperty;
  646. Procedure TestExternalClass_TypeCastToRootClass;
  647. Procedure TestExternalClass_TypeCastToJSObject;
  648. Procedure TestExternalClass_TypeCastStringToExternalString;
  649. Procedure TestExternalClass_TypeCastToJSFunction;
  650. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  651. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  652. Procedure TestExternalClass_BracketAccessor;
  653. Procedure TestExternalClass_BracketAccessor_Call;
  654. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  655. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  656. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  657. Procedure TestExternalClass_BracketAccessor_MultiType;
  658. Procedure TestExternalClass_BracketAccessor_Index;
  659. Procedure TestExternalClass_ForInJSObject;
  660. Procedure TestExternalClass_ForInJSArray;
  661. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  662. Procedure TestExternalClass_NestedConstructor;
  663. // class interfaces
  664. Procedure TestClassInterface_Corba;
  665. Procedure TestClassInterface_Corba_ProcExternalFail;
  666. Procedure TestClassInterface_Corba_Overloads;
  667. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  668. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  669. Procedure TestClassInterface_Corba_AncestorImpl;
  670. Procedure TestClassInterface_Corba_ImplReintroduce;
  671. Procedure TestClassInterface_Corba_MethodResolution;
  672. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  673. Procedure TestClassInterface_Corba_MethodOverride;
  674. Procedure TestClassInterface_Corba_Delegation;
  675. Procedure TestClassInterface_Corba_DelegationStatic;
  676. Procedure TestClassInterface_Corba_Operators;
  677. Procedure TestClassInterface_Corba_Args;
  678. Procedure TestClassInterface_Corba_ForIn;
  679. Procedure TestClassInterface_Corba_ArrayOfIntf;
  680. Procedure TestClassInterface_COM_AssignVar;
  681. Procedure TestClassInterface_COM_AssignArg;
  682. Procedure TestClassInterface_COM_FunctionResult;
  683. Procedure TestClassInterface_COM_InheritedFuncResult;
  684. Procedure TestClassInterface_COM_IsAsTypeCasts;
  685. Procedure TestClassInterface_COM_PassAsArg;
  686. Procedure TestClassInterface_COM_PassToUntypedParam;
  687. Procedure TestClassInterface_COM_FunctionInExpr;
  688. Procedure TestClassInterface_COM_Property;
  689. Procedure TestClassInterface_COM_IntfProperty;
  690. Procedure TestClassInterface_COM_Delegation;
  691. Procedure TestClassInterface_COM_With;
  692. Procedure TestClassInterface_COM_ForIn;
  693. Procedure TestClassInterface_COM_ArrayOfIntf;
  694. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  695. Procedure TestClassInterface_COM_RecordIntfFail;
  696. Procedure TestClassInterface_COM_UnitInitialization;
  697. Procedure TestClassInterface_Corba_GUID;
  698. Procedure TestClassInterface_Corba_GUIDProperty;
  699. // helpers
  700. Procedure TestClassHelper_ClassVar;
  701. Procedure TestClassHelper_Method_AccessInstanceFields;
  702. Procedure TestClassHelper_Method_Call;
  703. Procedure TestClassHelper_Method_Nested_Call;
  704. Procedure TestClassHelper_ClassMethod_Call;
  705. Procedure TestClassHelper_ClassOf;
  706. Procedure TestClassHelper_MethodRefObjFPC;
  707. Procedure TestClassHelper_Constructor;
  708. Procedure TestClassHelper_InheritedObjFPC;
  709. Procedure TestClassHelper_Property;
  710. Procedure TestClassHelper_Property_Array;
  711. Procedure TestClassHelper_Property_Array_Default;
  712. Procedure TestClassHelper_Property_Array_DefaultDefault;
  713. Procedure TestClassHelper_ClassProperty;
  714. Procedure TestClassHelper_ClassPropertyStatic;
  715. Procedure TestClassHelper_ClassProperty_Array;
  716. Procedure TestClassHelper_ForIn;
  717. Procedure TestClassHelper_PassProperty;
  718. Procedure TestExtClassHelper_ClassVar;
  719. Procedure TestExtClassHelper_Method_Call;
  720. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  721. Procedure TestRecordHelper_ClassVar;
  722. Procedure TestRecordHelper_Method_Call;
  723. Procedure TestRecordHelper_Constructor;
  724. Procedure TestTypeHelper_ClassVar;
  725. Procedure TestTypeHelper_PassResultElement;
  726. Procedure TestTypeHelper_PassArgs;
  727. Procedure TestTypeHelper_PassVarConst;
  728. Procedure TestTypeHelper_PassFuncResult;
  729. Procedure TestTypeHelper_PassPropertyField;
  730. Procedure TestTypeHelper_PassPropertyGetter;
  731. Procedure TestTypeHelper_PassClassPropertyField;
  732. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  733. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  734. Procedure TestTypeHelper_Property;
  735. Procedure TestTypeHelper_Property_Array;
  736. Procedure TestTypeHelper_ClassProperty;
  737. Procedure TestTypeHelper_ClassProperty_Array;
  738. Procedure TestTypeHelper_ClassMethod;
  739. Procedure TestTypeHelper_ExtClassMethodFail;
  740. Procedure TestTypeHelper_Constructor;
  741. Procedure TestTypeHelper_Word;
  742. Procedure TestTypeHelper_Boolean;
  743. Procedure TestTypeHelper_WordBool;
  744. Procedure TestTypeHelper_Double;
  745. Procedure TestTypeHelper_NativeInt;
  746. Procedure TestTypeHelper_StringChar;
  747. Procedure TestTypeHelper_JSValue;
  748. Procedure TestTypeHelper_Array;
  749. Procedure TestTypeHelper_EnumType;
  750. Procedure TestTypeHelper_SetType;
  751. Procedure TestTypeHelper_InterfaceType;
  752. Procedure TestTypeHelper_NestedSelf;
  753. // proc types
  754. Procedure TestProcType;
  755. Procedure TestProcType_Arg;
  756. Procedure TestProcType_FunctionFPC;
  757. Procedure TestProcType_FunctionDelphi;
  758. Procedure TestProcType_ProcedureDelphi;
  759. Procedure TestProcType_AsParam;
  760. Procedure TestProcType_MethodFPC;
  761. Procedure TestProcType_MethodDelphi;
  762. Procedure TestProcType_PropertyFPC;
  763. Procedure TestProcType_PropertyDelphi;
  764. Procedure TestProcType_WithClassInstDoPropertyFPC;
  765. Procedure TestProcType_Nested;
  766. Procedure TestProcType_NestedOfObject;
  767. Procedure TestProcType_ReferenceToProc;
  768. Procedure TestProcType_ReferenceToMethod;
  769. Procedure TestProcType_Typecast;
  770. Procedure TestProcType_PassProcToUntyped;
  771. Procedure TestProcType_PassProcToArray;
  772. Procedure TestProcType_SafeCallObjFPC;
  773. Procedure TestProcType_SafeCallDelphi;
  774. Procedure TestProcType_SafeCall_Arg;
  775. // pointer
  776. Procedure TestPointer;
  777. Procedure TestPointer_Proc;
  778. Procedure TestPointer_AssignRecordFail;
  779. Procedure TestPointer_AssignStaticArrayFail;
  780. Procedure TestPointer_TypeCastJSValueToPointer;
  781. Procedure TestPointer_NonRecordFail;
  782. Procedure TestPointer_AnonymousArgTypeFail;
  783. Procedure TestPointer_AnonymousVarTypeFail;
  784. Procedure TestPointer_AnonymousResultTypeFail;
  785. Procedure TestPointer_AddrOperatorFail;
  786. Procedure TestPointer_ArrayParamsFail;
  787. Procedure TestPointer_PointerAddFail;
  788. Procedure TestPointer_IncPointerFail;
  789. Procedure TestPointer_Record;
  790. Procedure TestPointer_RecordArg;
  791. // jsvalue
  792. Procedure TestJSValue_AssignToJSValue;
  793. Procedure TestJSValue_TypeCastToBaseType;
  794. Procedure TestJSValue_TypecastToJSValue;
  795. Procedure TestJSValue_Equal;
  796. Procedure TestJSValue_If;
  797. Procedure TestJSValue_Not;
  798. Procedure TestJSValue_Enum;
  799. Procedure TestJSValue_ClassInstance;
  800. Procedure TestJSValue_ClassOf;
  801. Procedure TestJSValue_ArrayOfJSValue;
  802. Procedure TestJSValue_ArrayLit;
  803. Procedure TestJSValue_Params;
  804. Procedure TestJSValue_UntypedParam;
  805. Procedure TestJSValue_FuncResultType;
  806. Procedure TestJSValue_ProcType_Assign;
  807. Procedure TestJSValue_ProcType_Equal;
  808. Procedure TestJSValue_ProcType_Param;
  809. Procedure TestJSValue_AssignToPointerFail;
  810. Procedure TestJSValue_OverloadDouble;
  811. Procedure TestJSValue_OverloadNativeInt;
  812. Procedure TestJSValue_OverloadWord;
  813. Procedure TestJSValue_OverloadString;
  814. Procedure TestJSValue_OverloadChar;
  815. Procedure TestJSValue_OverloadPointer;
  816. Procedure TestJSValue_ForIn;
  817. // RTTI
  818. Procedure TestRTTI_IntRange;
  819. Procedure TestRTTI_Double;
  820. Procedure TestRTTI_ProcType;
  821. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  822. Procedure TestRTTI_ProcTypeAnonymous;
  823. Procedure TestRTTI_EnumAndSetType;
  824. Procedure TestRTTI_EnumRange;
  825. Procedure TestRTTI_AnonymousEnumType;
  826. Procedure TestRTTI_StaticArray;
  827. Procedure TestRTTI_DynArray;
  828. Procedure TestRTTI_ArrayNestedAnonymous;
  829. Procedure TestRTTI_PublishedMethodOverloadFail;
  830. Procedure TestRTTI_PublishedMethodHideNoHint;
  831. Procedure TestRTTI_PublishedMethodExternalFail;
  832. Procedure TestRTTI_PublishedClassPropertyFail;
  833. Procedure TestRTTI_PublishedClassFieldFail;
  834. Procedure TestRTTI_PublishedFieldExternalFail;
  835. Procedure TestRTTI_Class_Field;
  836. Procedure TestRTTI_Class_Method;
  837. Procedure TestRTTI_Class_MethodArgFlags;
  838. Procedure TestRTTI_Class_Property;
  839. Procedure TestRTTI_Class_PropertyParams;
  840. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  841. Procedure TestRTTI_Class_OmitRTTI;
  842. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  843. Procedure TestRTTI_IndexModifier;
  844. Procedure TestRTTI_StoredModifier;
  845. Procedure TestRTTI_DefaultValue;
  846. Procedure TestRTTI_DefaultValueSet;
  847. Procedure TestRTTI_DefaultValueRangeType;
  848. Procedure TestRTTI_DefaultValueInherit;
  849. Procedure TestRTTI_OverrideMethod;
  850. Procedure TestRTTI_ReintroduceMethod;
  851. Procedure TestRTTI_OverloadProperty;
  852. // ToDo: array argument
  853. Procedure TestRTTI_ClassForward;
  854. Procedure TestRTTI_ClassOf;
  855. Procedure TestRTTI_Record;
  856. Procedure TestRTTI_RecordAnonymousArray;
  857. Procedure TestRTTI_Record_ClassVarType;
  858. Procedure TestRTTI_LocalTypes;
  859. Procedure TestRTTI_TypeInfo_BaseTypes;
  860. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  861. Procedure TestRTTI_TypeInfo_LocalFail;
  862. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  863. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  864. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  865. Procedure TestRTTI_TypeInfo_FunctionClassType;
  866. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  867. Procedure TestRTTI_Interface_Corba;
  868. Procedure TestRTTI_Interface_COM;
  869. Procedure TestRTTI_ClassHelper;
  870. Procedure TestRTTI_ExternalClass;
  871. Procedure TestRTTI_Unit;
  872. // Resourcestring
  873. Procedure TestResourcestringProgram;
  874. Procedure TestResourcestringUnit;
  875. Procedure TestResourcestringImplementation;
  876. // Attributes
  877. Procedure TestAttributes_Members;
  878. Procedure TestAttributes_Types;
  879. Procedure TestAttributes_HelperConstructor_Fail;
  880. Procedure TestAttributes_InterfacesList;
  881. // Assertions, checks
  882. procedure TestAssert;
  883. procedure TestAssert_SysUtils;
  884. procedure TestObjectChecks;
  885. procedure TestOverflowChecks_Int;
  886. procedure TestRangeChecks_AssignInt;
  887. procedure TestRangeChecks_AssignIntRange;
  888. procedure TestRangeChecks_AssignEnum;
  889. procedure TestRangeChecks_AssignEnumRange;
  890. procedure TestRangeChecks_AssignChar;
  891. procedure TestRangeChecks_AssignCharRange;
  892. procedure TestRangeChecks_ArrayIndex;
  893. procedure TestRangeChecks_ArrayOfRecIndex;
  894. procedure TestRangeChecks_StringIndex;
  895. procedure TestRangeChecks_TypecastInt;
  896. procedure TestRangeChecks_TypeHelperInt;
  897. procedure TestRangeChecks_AssignCurrency;
  898. // Async/AWait
  899. Procedure TestAsync_Proc;
  900. Procedure TestAsync_CallResultIsPromise;
  901. Procedure TestAsync_ConstructorFail;
  902. Procedure TestAsync_PropertyGetterFail;
  903. Procedure TestAwait_NonPromiseWithTypeFail;
  904. Procedure TestAwait_AsyncCallTypeMismatch;
  905. Procedure TestAWait_OutsideAsyncFail;
  906. Procedure TestAWait_IntegerFail;
  907. Procedure TestAWait_ExternalClassPromise;
  908. Procedure TestAWait_JSValue;
  909. Procedure TestAWait_Result;
  910. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  911. Procedure TestAsync_AnonymousProc;
  912. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  913. Procedure TestAsync_ProcType;
  914. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  915. Procedure TestAsync_Inherited;
  916. Procedure TestAsync_ClassInterface;
  917. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  918. Procedure TestAWait_ClassAs;
  919. // Library
  920. Procedure TestLibrary_Empty;
  921. Procedure TestLibrary_ExportFunc;
  922. Procedure TestLibrary_ExportFuncOverloadedFail;
  923. Procedure TestLibrary_Export_Index_Fail;
  924. Procedure TestLibrary_ExportVar;
  925. Procedure TestLibrary_ExportUnitFunc;
  926. end;
  927. function LinesToStr(Args: array of const): string;
  928. function ExtractFileUnitName(aFilename: string): string;
  929. function JSToStr(El: TJSElement): string;
  930. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  931. implementation
  932. function LinesToStr(Args: array of const): string;
  933. var
  934. s: String;
  935. i: Integer;
  936. begin
  937. s:='';
  938. for i:=Low(Args) to High(Args) do
  939. case Args[i].VType of
  940. vtChar: s += Args[i].VChar+LineEnding;
  941. vtString: s += Args[i].VString^+LineEnding;
  942. vtPChar: s += Args[i].VPChar+LineEnding;
  943. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  944. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  945. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  946. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  947. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  948. end;
  949. Result:=s;
  950. end;
  951. function ExtractFileUnitName(aFilename: string): string;
  952. var
  953. p: Integer;
  954. begin
  955. Result:=ExtractFileName(aFilename);
  956. if Result='' then exit;
  957. for p:=length(Result) downto 1 do
  958. case Result[p] of
  959. '/','\': exit;
  960. '.':
  961. begin
  962. Delete(Result,p,length(Result));
  963. exit;
  964. end;
  965. end;
  966. end;
  967. function JSToStr(El: TJSElement): string;
  968. var
  969. aWriter: TBufferWriter;
  970. aJSWriter: TJSWriter;
  971. begin
  972. aJSWriter:=nil;
  973. aWriter:=TBufferWriter.Create(1000);
  974. try
  975. aJSWriter:=TJSWriter.Create(aWriter);
  976. aJSWriter.IndentSize:=2;
  977. aJSWriter.WriteJS(El);
  978. Result:=aWriter.AsString;
  979. finally
  980. aJSWriter.Free;
  981. aWriter.Free;
  982. end;
  983. end;
  984. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  985. // search diff, ignore changes in spaces
  986. const
  987. SpaceChars = [#9,#10,#13,' '];
  988. var
  989. ExpectedP, ActualP: PChar;
  990. function FindLineEnd(p: PChar): PChar;
  991. begin
  992. Result:=p;
  993. while not (Result^ in [#0,#10,#13]) do inc(Result);
  994. end;
  995. function FindLineStart(p, MinP: PChar): PChar;
  996. begin
  997. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  998. Result:=p;
  999. end;
  1000. procedure SkipLineEnd(var p: PChar);
  1001. begin
  1002. if p^ in [#10,#13] then
  1003. begin
  1004. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1005. inc(p,2)
  1006. else
  1007. inc(p);
  1008. end;
  1009. end;
  1010. function HasSpecialChar(s: string): boolean;
  1011. var
  1012. i: Integer;
  1013. begin
  1014. for i:=1 to length(s) do
  1015. if s[i] in [#0..#31,#127..#255] then
  1016. exit(true);
  1017. Result:=false;
  1018. end;
  1019. function HashSpecialChars(s: string): string;
  1020. var
  1021. i: Integer;
  1022. begin
  1023. Result:='';
  1024. for i:=1 to length(s) do
  1025. if s[i] in [#0..#31,#127..#255] then
  1026. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1027. else
  1028. Result:=Result+s[i];
  1029. end;
  1030. procedure DiffFound;
  1031. var
  1032. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1033. ExpLine, ActLine: String;
  1034. i, LineNo, DiffLineNo: Integer;
  1035. begin
  1036. writeln('Diff found "',Msg,'". Lines:');
  1037. // write correct lines
  1038. p:=PChar(Expected);
  1039. LineNo:=0;
  1040. DiffLineNo:=0;
  1041. repeat
  1042. StartPos:=p;
  1043. while not (p^ in [#0,#10,#13]) do inc(p);
  1044. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1045. SkipLineEnd(p);
  1046. inc(LineNo);
  1047. if (p<=ExpectedP) and (p^<>#0) then
  1048. begin
  1049. writeln('= ',ExpLine);
  1050. end else begin
  1051. // diff line
  1052. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1053. // write actual line
  1054. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1055. ActLineEndP:=FindLineEnd(ActualP);
  1056. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1057. writeln('- ',ActLine);
  1058. if HasSpecialChar(ActLine) then
  1059. writeln('- ',HashSpecialChars(ActLine));
  1060. // write expected line
  1061. writeln('+ ',ExpLine);
  1062. if HasSpecialChar(ExpLine) then
  1063. writeln('- ',HashSpecialChars(ExpLine));
  1064. // write empty line with pointer ^
  1065. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1066. writeln('^');
  1067. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1068. CheckSrcDiff:=false;
  1069. // write up to three following actual lines to get some context
  1070. for i:=1 to 3 do begin
  1071. ActLineStartP:=ActLineEndP;
  1072. SkipLineEnd(ActLineStartP);
  1073. if ActLineStartP^=#0 then break;
  1074. ActLineEndP:=FindLineEnd(ActLineStartP);
  1075. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1076. writeln('~ ',ActLine);
  1077. end;
  1078. exit;
  1079. end;
  1080. until p^=#0;
  1081. writeln('DiffFound Actual:-----------------------');
  1082. writeln(Actual);
  1083. writeln('DiffFound Expected:---------------------');
  1084. writeln(Expected);
  1085. writeln('DiffFound ------------------------------');
  1086. Msg:='diff found, but lines are the same, internal error';
  1087. CheckSrcDiff:=false;
  1088. end;
  1089. var
  1090. IsSpaceNeeded: Boolean;
  1091. LastChar, Quote: Char;
  1092. begin
  1093. Result:=true;
  1094. Msg:='';
  1095. if Expected='' then Expected:=' ';
  1096. if Actual='' then Actual:=' ';
  1097. ExpectedP:=PChar(Expected);
  1098. ActualP:=PChar(Actual);
  1099. repeat
  1100. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1101. case ExpectedP^ of
  1102. #0:
  1103. begin
  1104. // check that rest of Actual has only spaces
  1105. while ActualP^ in SpaceChars do inc(ActualP);
  1106. if ActualP^<>#0 then
  1107. begin
  1108. DiffFound;
  1109. exit;
  1110. end;
  1111. exit(true);
  1112. end;
  1113. ' ',#9,#10,#13:
  1114. begin
  1115. // skip space in Expected
  1116. IsSpaceNeeded:=false;
  1117. if ExpectedP>PChar(Expected) then
  1118. LastChar:=ExpectedP[-1]
  1119. else
  1120. LastChar:=#0;
  1121. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1122. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1123. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1124. IsSpaceNeeded:=true;
  1125. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1126. begin
  1127. DiffFound;
  1128. exit;
  1129. end;
  1130. while ActualP^ in SpaceChars do inc(ActualP);
  1131. end;
  1132. '''','"':
  1133. begin
  1134. while ActualP^ in SpaceChars do inc(ActualP);
  1135. if ExpectedP^<>ActualP^ then
  1136. begin
  1137. DiffFound;
  1138. exit;
  1139. end;
  1140. Quote:=ExpectedP^;
  1141. repeat
  1142. inc(ExpectedP);
  1143. inc(ActualP);
  1144. if ExpectedP^<>ActualP^ then
  1145. begin
  1146. DiffFound;
  1147. exit;
  1148. end;
  1149. if (ExpectedP^ in [#0,#10,#13]) then
  1150. break
  1151. else if (ExpectedP^=Quote) then
  1152. begin
  1153. inc(ExpectedP);
  1154. inc(ActualP);
  1155. break;
  1156. end;
  1157. until false;
  1158. end;
  1159. else
  1160. while ActualP^ in SpaceChars do inc(ActualP);
  1161. if ExpectedP^<>ActualP^ then
  1162. begin
  1163. DiffFound;
  1164. exit;
  1165. end;
  1166. inc(ExpectedP);
  1167. inc(ActualP);
  1168. end;
  1169. until false;
  1170. end;
  1171. { TTestEnginePasResolver }
  1172. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1173. begin
  1174. if FModule=AValue then Exit;
  1175. FModule:=AValue;
  1176. end;
  1177. destructor TTestEnginePasResolver.Destroy;
  1178. begin
  1179. FreeAndNil(FStreamResolver);
  1180. FreeAndNil(FParser);
  1181. FreeAndNil(FScanner);
  1182. FreeAndNil(FStreamResolver);
  1183. Module:=nil;
  1184. inherited Destroy;
  1185. end;
  1186. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1187. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1188. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1189. begin
  1190. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1191. TypeParams);
  1192. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1193. Module:=TPasModule(Result);
  1194. end;
  1195. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1196. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1197. begin
  1198. Result:=nil;
  1199. if InFilename<>'' then
  1200. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1201. if Assigned(OnFindUnit) then
  1202. Result:=OnFindUnit(AName);
  1203. if NameExpr=nil then ;
  1204. end;
  1205. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1206. begin
  1207. // do not parse recursively
  1208. // parse via the queue
  1209. if Section=nil then ;
  1210. end;
  1211. { TCustomTestModule }
  1212. procedure TCustomTestModule.FreeSrcMarkers;
  1213. var
  1214. aMarker, Last: PSrcMarker;
  1215. begin
  1216. aMarker:=FirstSrcMarker;
  1217. while aMarker<>nil do
  1218. begin
  1219. Last:=aMarker;
  1220. aMarker:=aMarker^.Next;
  1221. Dispose(Last);
  1222. end;
  1223. FirstSrcMarker:=nil;
  1224. LastSrcMarker:=nil;
  1225. end;
  1226. function TCustomTestModule.GetResolverCount: integer;
  1227. begin
  1228. Result:=FResolvers.Count;
  1229. end;
  1230. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1231. begin
  1232. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1233. end;
  1234. function TCustomTestModule.GetMsgCount: integer;
  1235. begin
  1236. Result:=FHintMsgs.Count;
  1237. end;
  1238. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1239. begin
  1240. Result:=TTestHintMessage(FHintMsgs[Index]);
  1241. end;
  1242. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1243. ): TPasModule;
  1244. var
  1245. DefNamespace: String;
  1246. begin
  1247. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1248. if (Pos('.',aUnitName)<1) then
  1249. begin
  1250. DefNamespace:=GetDefaultNamespace;
  1251. if DefNamespace<>'' then
  1252. begin
  1253. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1254. if Result<>nil then exit;
  1255. end;
  1256. end;
  1257. Result:=LoadUnit(aUnitName);
  1258. if Result<>nil then exit;
  1259. {$IFDEF VerbosePas2JS}
  1260. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1261. {$ENDIF}
  1262. Fail('can''t find unit "'+aUnitName+'"');
  1263. end;
  1264. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1265. var
  1266. aParser: TPasParser;
  1267. Item: TTestHintMessage;
  1268. begin
  1269. aParser:=Sender as TPasParser;
  1270. Item:=TTestHintMessage.Create;
  1271. Item.Id:=aParser.LastMsgNumber;
  1272. Item.MsgType:=aParser.LastMsgType;
  1273. Item.MsgNumber:=aParser.LastMsgNumber;
  1274. Item.Msg:=Msg;
  1275. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1276. {$IFDEF VerbosePas2JS}
  1277. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1278. {$ENDIF}
  1279. FHintMsgs.Add(Item);
  1280. end;
  1281. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1282. );
  1283. var
  1284. aResolver: TTestEnginePasResolver;
  1285. Item: TTestHintMessage;
  1286. begin
  1287. aResolver:=Sender as TTestEnginePasResolver;
  1288. Item:=TTestHintMessage.Create;
  1289. Item.Id:=aResolver.LastMsgId;
  1290. Item.MsgType:=aResolver.LastMsgType;
  1291. Item.MsgNumber:=aResolver.LastMsgNumber;
  1292. Item.Msg:=Msg;
  1293. Item.SourcePos:=aResolver.LastSourcePos;
  1294. {$IFDEF VerbosePas2JS}
  1295. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1296. {$ENDIF}
  1297. FHintMsgs.Add(Item);
  1298. end;
  1299. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1300. var
  1301. Item: TTestHintMessage;
  1302. aScanner: TPas2jsPasScanner;
  1303. begin
  1304. aScanner:=Sender as TPas2jsPasScanner;
  1305. Item:=TTestHintMessage.Create;
  1306. Item.Id:=aScanner.LastMsgNumber;
  1307. Item.MsgType:=aScanner.LastMsgType;
  1308. Item.MsgNumber:=aScanner.LastMsgNumber;
  1309. Item.Msg:=Msg;
  1310. Item.SourcePos:=aScanner.CurSourcePos;
  1311. {$IFDEF VerbosePas2JS}
  1312. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1313. {$ENDIF}
  1314. FHintMsgs.Add(Item);
  1315. end;
  1316. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1317. var
  1318. SubEl: TPasElement;
  1319. i: Integer;
  1320. procedure E(Msg: string);
  1321. var
  1322. s: String;
  1323. begin
  1324. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1325. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1326. writeln('ERROR: ',s);
  1327. Fail(s);
  1328. end;
  1329. begin
  1330. if arg=nil then ;
  1331. if El=nil then exit;
  1332. if El.Parent=El then
  1333. E('El.Parent=El='+GetObjName(El));
  1334. if El is TBinaryExpr then
  1335. begin
  1336. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1337. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1338. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1339. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1340. end
  1341. else if El is TParamsExpr then
  1342. begin
  1343. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1344. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1345. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1346. if TParamsExpr(El).Params[i].Parent<>El then
  1347. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1348. end
  1349. else if El is TProcedureExpr then
  1350. begin
  1351. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1352. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1353. end
  1354. else if El is TPasDeclarations then
  1355. begin
  1356. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1357. begin
  1358. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1359. if SubEl.Parent<>El then
  1360. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1361. end;
  1362. end
  1363. else if El is TPasImplBlock then
  1364. begin
  1365. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1366. begin
  1367. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1368. if SubEl.Parent<>El then
  1369. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1370. end;
  1371. end
  1372. else if El is TPasImplWithDo then
  1373. begin
  1374. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1375. begin
  1376. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1377. if SubEl.Parent<>El then
  1378. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1379. end;
  1380. end
  1381. else if El is TPasProcedure then
  1382. begin
  1383. if TPasProcedure(El).ProcType.Parent<>El then
  1384. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1385. end
  1386. else if El is TPasProcedureType then
  1387. begin
  1388. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1389. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1390. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1391. end;
  1392. end;
  1393. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1394. var
  1395. Data: PTestResolverReferenceData absolute FindData;
  1396. Line, Col: integer;
  1397. begin
  1398. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1399. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1400. if (Data^.Filename=El.SourceFilename)
  1401. and (Data^.Row=Line)
  1402. and (Data^.StartCol<=Col)
  1403. and (Data^.EndCol>=Col)
  1404. then
  1405. Data^.Found.Add(El);
  1406. end;
  1407. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1408. begin
  1409. if FWithTypeInfo=AValue then Exit;
  1410. FWithTypeInfo:=AValue;
  1411. if AValue then
  1412. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1413. else
  1414. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1415. end;
  1416. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1417. var
  1418. i: Integer;
  1419. CurEngine: TTestEnginePasResolver;
  1420. CurUnitName: String;
  1421. begin
  1422. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1423. Result:=nil;
  1424. if (Module.ClassType=TPasModule)
  1425. and (CompareText(Module.Name,aUnitName)=0) then
  1426. exit(Module);
  1427. for i:=0 to ResolverCount-1 do
  1428. begin
  1429. CurEngine:=Resolvers[i];
  1430. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1431. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1432. if CompareText(aUnitName,CurUnitName)=0 then
  1433. begin
  1434. Result:=CurEngine.Module;
  1435. if Result<>nil then exit;
  1436. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1437. FileResolver.FindSourceFile(aUnitName);
  1438. CurEngine.StreamResolver:=TStreamResolver.Create;
  1439. CurEngine.StreamResolver.OwnsStreams:=True;
  1440. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1441. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1442. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1443. InitScanner(CurEngine.Scanner);
  1444. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1445. CurEngine.Parser.Options:=po_tcmodules;
  1446. if CompareText(CurUnitName,'System')=0 then
  1447. CurEngine.Parser.ImplicitUses.Clear;
  1448. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1449. try
  1450. CurEngine.Parser.NextToken;
  1451. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1452. except
  1453. on E: Exception do
  1454. HandleException(E);
  1455. end;
  1456. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1457. Result:=CurEngine.Module;
  1458. exit;
  1459. end;
  1460. end;
  1461. end;
  1462. procedure TCustomTestModule.SetUp;
  1463. begin
  1464. {$IFDEF EnablePasTreeGlobalRefCount}
  1465. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1466. {$ENDIF}
  1467. if FResolvers<>nil then
  1468. begin
  1469. writeln('TCustomTestModule.SetUp FModules<>nil');
  1470. Halt;
  1471. end;
  1472. inherited SetUp;
  1473. FSkipTests:=false;
  1474. FWithTypeInfo:=false;
  1475. FSource:=TStringList.Create;
  1476. FHub:=TPas2JSResolverHub.Create(Self);
  1477. FResolvers:=TObjectList.Create(true);
  1478. FFilename:='test1.pp';
  1479. FFileResolver:=TStreamResolver.Create;
  1480. FFileResolver.OwnsStreams:=True;
  1481. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1482. InitScanner(FScanner);
  1483. FEngine:=AddModule(Filename);
  1484. FEngine.Scanner:=FScanner;
  1485. FScanner.Resolver:=FEngine;
  1486. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1487. FParser.OnLog:=@OnParserLog;
  1488. FEngine.Parser:=FParser;
  1489. Parser.Options:=po_tcmodules;
  1490. FModule:=Nil;
  1491. FConverter:=CreateConverter;
  1492. FExpectedErrorClass:=nil;
  1493. end;
  1494. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1495. var
  1496. Options: TPasToJsConverterOptions;
  1497. begin
  1498. Result:=TPasToJSConverter.Create;
  1499. Options:=co_tcmodules;
  1500. if WithTypeInfo then
  1501. Exclude(Options,coNoTypeInfo)
  1502. else
  1503. Include(Options,coNoTypeInfo);
  1504. Result.Options:=Options;
  1505. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1506. end;
  1507. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1508. begin
  1509. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1510. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1511. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1512. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1513. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1514. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1515. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1516. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1517. aScanner.OnLog:=@OnScannerLog;
  1518. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1519. end;
  1520. procedure TCustomTestModule.TearDown;
  1521. {$IFDEF CheckPasTreeRefCount}
  1522. var
  1523. El: TPasElement;
  1524. {$ENDIF}
  1525. var
  1526. i: Integer;
  1527. CurModule: TPasModule;
  1528. begin
  1529. FreeSrcMarkers;
  1530. FHintMsgs.Clear;
  1531. FHintMsgsGood.Clear;
  1532. FSkipTests:=false;
  1533. FWithTypeInfo:=false;
  1534. FJSRegModuleCall:=nil;
  1535. FJSModuleCallArgs:=nil;
  1536. FJSImplentationUses:=nil;
  1537. FJSInterfaceUses:=nil;
  1538. FJSModuleSrc:=nil;
  1539. FJSInitBody:=nil;
  1540. FreeAndNil(FJSSource);
  1541. FreeAndNil(FJSModule);
  1542. FreeAndNil(FConverter);
  1543. ResolverEngine.Clear;
  1544. FreeAndNil(FSource);
  1545. FreeAndNil(FFileResolver);
  1546. if FResolvers<>nil then
  1547. begin
  1548. for i:=0 to FResolvers.Count-1 do
  1549. begin
  1550. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1551. if CurModule=nil then continue;
  1552. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1553. end;
  1554. for i:=0 to FResolvers.Count-1 do
  1555. begin
  1556. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1557. if CurModule=nil then continue;
  1558. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1559. end;
  1560. FreeAndNil(FResolvers);
  1561. FModule:=nil;
  1562. FEngine:=nil;
  1563. end;
  1564. FreeAndNil(FHub);
  1565. inherited TearDown;
  1566. {$IFDEF EnablePasTreeGlobalRefCount}
  1567. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1568. begin
  1569. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1570. {$IFDEF CheckPasTreeRefCount}
  1571. El:=TPasElement.FirstRefEl;
  1572. while El<>nil do
  1573. begin
  1574. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1575. for i:=0 to El.RefIds.Count-1 do
  1576. writeln(' ',El.RefIds[i]);
  1577. El:=El.NextRefEl;
  1578. end;
  1579. {$ENDIF}
  1580. Halt;
  1581. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1582. end;
  1583. {$ENDIF}
  1584. end;
  1585. procedure TCustomTestModule.Add(Line: string);
  1586. begin
  1587. Source.Add(Line);
  1588. end;
  1589. procedure TCustomTestModule.Add(const Lines: array of string);
  1590. var
  1591. i: Integer;
  1592. begin
  1593. for i:=low(Lines) to high(Lines) do
  1594. Add(Lines[i]);
  1595. end;
  1596. procedure TCustomTestModule.StartParsing;
  1597. var
  1598. Src: String;
  1599. begin
  1600. Src:=Source.Text;
  1601. FEngine.Source:=Src;
  1602. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1603. Scanner.OpenFile(FileName);
  1604. Writeln('// Test : ',Self.TestName);
  1605. Writeln(Src);
  1606. end;
  1607. procedure TCustomTestModule.ParseModuleQueue;
  1608. var
  1609. i: Integer;
  1610. CurResolver: TTestEnginePasResolver;
  1611. Found: Boolean;
  1612. Section: TPasSection;
  1613. begin
  1614. // parse til exception or all Resolvers finished
  1615. while not SkipTests do
  1616. begin
  1617. Found:=false;
  1618. for i:=0 to ResolverCount-1 do
  1619. begin
  1620. CurResolver:=Resolvers[i];
  1621. if CurResolver.CurrentParser=nil then continue;
  1622. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1623. continue;
  1624. CurResolver.Parser.ParseContinue;
  1625. Found:=true;
  1626. break;
  1627. end;
  1628. if not Found then break;
  1629. end;
  1630. for i:=0 to ResolverCount-1 do
  1631. begin
  1632. CurResolver:=Resolvers[i];
  1633. if CurResolver.Parser=nil then
  1634. begin
  1635. if CurResolver.CurrentParser<>nil then
  1636. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1637. continue;
  1638. end;
  1639. if CurResolver.Parser.CurModule<>nil then
  1640. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1641. end;
  1642. end;
  1643. procedure TCustomTestModule.ParseModule;
  1644. begin
  1645. if SkipTests then exit;
  1646. FFirstPasStatement:=nil;
  1647. try
  1648. StartParsing;
  1649. Parser.ParseMain(FModule);
  1650. ParseModuleQueue;
  1651. except
  1652. on E: Exception do
  1653. HandleException(E);
  1654. end;
  1655. if SkipTests then exit;
  1656. AssertNotNull('Module resulted in Module',Module);
  1657. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1658. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1659. end;
  1660. procedure TCustomTestModule.ParseProgram;
  1661. begin
  1662. if SkipTests then exit;
  1663. ParseModule;
  1664. if SkipTests then exit;
  1665. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1666. FPasProgram:=TPasProgram(Module);
  1667. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1668. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1669. if (PasProgram.InitializationSection.Elements.Count>0) then
  1670. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1671. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1672. end;
  1673. procedure TCustomTestModule.ParseLibrary;
  1674. var
  1675. Init: TInitializationSection;
  1676. begin
  1677. if SkipTests then exit;
  1678. ParseModule;
  1679. if SkipTests then exit;
  1680. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1681. FPasLibrary:=TPasLibrary(Module);
  1682. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1683. Init:=PasLibrary.InitializationSection;
  1684. if (Init<>nil) and (Init.Elements.Count>0) then
  1685. if TObject(Init.Elements[0]) is TPasImplBlock then
  1686. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1687. end;
  1688. procedure TCustomTestModule.ParseUnit;
  1689. begin
  1690. if SkipTests then exit;
  1691. ParseModule;
  1692. if SkipTests then exit;
  1693. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1694. AssertNotNull('Has interface section',Module.InterfaceSection);
  1695. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1696. if (Module.InitializationSection<>nil)
  1697. and (Module.InitializationSection.Elements.Count>0)
  1698. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1699. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1700. end;
  1701. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1702. ): TTestEnginePasResolver;
  1703. var
  1704. i: Integer;
  1705. begin
  1706. for i:=0 to ResolverCount-1 do
  1707. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1708. exit(Resolvers[i]);
  1709. Result:=nil;
  1710. end;
  1711. function TCustomTestModule.AddModule(aFilename: string
  1712. ): TTestEnginePasResolver;
  1713. begin
  1714. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1715. if FindModuleWithFilename(aFilename)<>nil then
  1716. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1717. Result:=TTestEnginePasResolver.Create;
  1718. Result.Filename:=aFilename;
  1719. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1720. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1721. Result.OnLog:=@OnPasResolverLog;
  1722. Result.Hub:=Hub;
  1723. FResolvers.Add(Result);
  1724. end;
  1725. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1726. ): TTestEnginePasResolver;
  1727. begin
  1728. Result:=AddModule(aFilename);
  1729. Result.Source:=Src;
  1730. end;
  1731. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1732. ImplementationSrc: string): TTestEnginePasResolver;
  1733. var
  1734. Src: String;
  1735. begin
  1736. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1737. Src+=LineEnding;
  1738. Src+='interface'+LineEnding;
  1739. Src+=LineEnding;
  1740. Src+=InterfaceSrc;
  1741. Src+='implementation'+LineEnding;
  1742. Src+=LineEnding;
  1743. Src+=ImplementationSrc;
  1744. Src+='end.'+LineEnding;
  1745. Result:=AddModuleWithSrc(aFilename,Src);
  1746. end;
  1747. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1748. var
  1749. Intf, Impl: TStringList;
  1750. begin
  1751. Intf:=TStringList.Create;
  1752. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1753. // unit interface
  1754. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1755. Intf.Add('{$modeswitch externalclass}');
  1756. Intf.Add('type');
  1757. Intf.Add(' integer=longint;');
  1758. Intf.Add(' sizeint=nativeint;');
  1759. //'const',
  1760. //' LineEnding = #10;',
  1761. //' DirectorySeparator = ''/'';',
  1762. //' DriveSeparator = '''';',
  1763. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1764. //' AllowDriveSeparators : set of char = [];',
  1765. if supTObject in Parts then
  1766. Intf.AddStrings([
  1767. 'type',
  1768. ' TClass = class of TObject;',
  1769. ' TObject = class',
  1770. ' constructor Create;',
  1771. ' destructor Destroy; virtual;',
  1772. ' class function ClassType: TClass; assembler;',
  1773. ' class function ClassName: String; assembler;',
  1774. ' class function ClassNameIs(const Name: string): boolean;',
  1775. ' class function ClassParent: TClass; assembler;',
  1776. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1777. ' class function UnitName: String; assembler;',
  1778. ' procedure AfterConstruction; virtual;',
  1779. ' procedure BeforeDestruction;virtual;',
  1780. ' function Equals(Obj: TObject): boolean; virtual;',
  1781. ' function ToString: String; virtual;',
  1782. ' end;']);
  1783. if supTInterfacedObject in Parts then
  1784. Intf.AddStrings([
  1785. ' {$Interfaces COM}',
  1786. ' IUnknown = interface',
  1787. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1788. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1789. ' function _AddRef: Integer;',
  1790. ' function _Release: Integer;',
  1791. ' end;',
  1792. ' IInterface = IUnknown;',
  1793. ' TInterfacedObject = class(TObject,IUnknown)',
  1794. ' protected',
  1795. ' fRefCount: Integer;',
  1796. ' { implement methods of IUnknown }',
  1797. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1798. ' function _AddRef: Integer; virtual;',
  1799. ' function _Release: Integer; virtual;',
  1800. ' end;',
  1801. ' TInterfacedClass = class of TInterfacedObject;',
  1802. '',
  1803. '']);
  1804. if supTVarRec in Parts then
  1805. Intf.AddStrings([
  1806. 'const',
  1807. ' vtInteger = 0;',
  1808. ' vtBoolean = 1;',
  1809. ' vtJSValue = 19;',
  1810. 'type',
  1811. ' PVarRec = ^TVarRec;',
  1812. ' TVarRec = record',
  1813. ' VType : byte;',
  1814. ' VJSValue: JSValue;',
  1815. ' vInteger: longint external name ''VJSValue'';',
  1816. ' vBoolean: boolean external name ''VJSValue'';',
  1817. ' end;',
  1818. ' TVarRecArray = array of TVarRec;',
  1819. 'function VarRecs: TVarRecArray; varargs;',
  1820. '']);
  1821. if supTypeInfo in Parts then
  1822. begin
  1823. Intf.AddStrings([
  1824. 'type',
  1825. ' TTypeKind = (',
  1826. ' tkUnknown, // 0',
  1827. ' tkInteger, // 1',
  1828. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1829. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1830. ' tkEnumeration, // 4',
  1831. ' tkSet, // 5',
  1832. ' tkDouble, // 6',
  1833. ' tkBool, // 7',
  1834. ' tkProcVar, // 8 function or procedure',
  1835. ' tkMethod, // 9 proc var of object',
  1836. ' tkArray, // 10 static array',
  1837. ' tkDynArray, // 11',
  1838. ' tkRecord, // 12',
  1839. ' tkClass, // 13',
  1840. ' tkClassRef, // 14',
  1841. ' tkPointer, // 15',
  1842. ' tkJSValue, // 16',
  1843. ' tkRefToProcVar, // 17 variable of procedure type',
  1844. ' tkInterface, // 18',
  1845. ' //tkObject,',
  1846. ' //tkSString,tkLString,tkAString,tkWString,',
  1847. ' //tkVariant,',
  1848. ' //tkWChar,',
  1849. ' //tkInt64,',
  1850. ' //tkQWord,',
  1851. ' //tkInterfaceRaw,',
  1852. ' //tkUString,tkUChar,',
  1853. ' tkHelper, // 19',
  1854. ' //tkFile,',
  1855. ' tkExtClass // 20',
  1856. ' );',
  1857. ' TTypeKinds = set of TTypeKind;',
  1858. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1859. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1860. ' end;',
  1861. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1862. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1863. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1864. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1865. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1866. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1867. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1868. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1869. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1870. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1871. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1872. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1873. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1874. '']);
  1875. end;
  1876. if supWriteln in Parts then
  1877. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1878. Intf.Add('var');
  1879. Intf.Add(' ExitCode: Longint = 0;');
  1880. // unit implementation
  1881. Impl:=TStringList.Create;
  1882. if supTObject in Parts then
  1883. Impl.AddStrings([
  1884. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1885. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1886. 'asm',
  1887. 'end;',
  1888. 'constructor TObject.Create; begin end;',
  1889. 'destructor TObject.Destroy; begin end;',
  1890. 'class function TObject.ClassType: TClass; assembler;',
  1891. 'asm',
  1892. 'end;',
  1893. 'class function TObject.ClassName: String; assembler;',
  1894. 'asm',
  1895. 'end;',
  1896. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1897. 'begin',
  1898. ' Result:=SameText(Name,ClassName);',
  1899. 'end;',
  1900. 'class function TObject.ClassParent: TClass; assembler;',
  1901. 'asm',
  1902. 'end;',
  1903. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1904. 'asm',
  1905. 'end;',
  1906. 'class function TObject.UnitName: String; assembler;',
  1907. 'asm',
  1908. 'end;',
  1909. 'procedure TObject.AfterConstruction; begin end;',
  1910. 'procedure TObject.BeforeDestruction; begin end;',
  1911. 'function TObject.Equals(Obj: TObject): boolean;',
  1912. 'begin',
  1913. ' Result:=Obj=Self;',
  1914. 'end;',
  1915. 'function TObject.ToString: String;',
  1916. 'begin',
  1917. ' Result:=ClassName;',
  1918. 'end;'
  1919. ]);
  1920. if supTInterfacedObject in Parts then
  1921. Impl.AddStrings([
  1922. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1923. //'begin',
  1924. //'end;',
  1925. 'function TInterfacedObject._AddRef: Integer;',
  1926. 'begin',
  1927. 'end;',
  1928. 'function TInterfacedObject._Release: Integer;',
  1929. 'begin',
  1930. 'end;',
  1931. '']);
  1932. if supTVarRec in Parts then
  1933. Impl.AddStrings([
  1934. 'function VarRecs: TVarRecArray; varargs;',
  1935. 'var',
  1936. ' v: PVarRec;',
  1937. 'begin',
  1938. ' v^.VType:=1;',
  1939. ' v^.VJSValue:=2;',
  1940. 'end;',
  1941. '']);
  1942. try
  1943. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1944. finally
  1945. Intf.Free;
  1946. Impl.Free;
  1947. end;
  1948. end;
  1949. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1950. SystemUnitParts: TSystemUnitParts);
  1951. begin
  1952. if NeedSystemUnit then
  1953. AddSystemUnit(SystemUnitParts)
  1954. else
  1955. Parser.ImplicitUses.Clear;
  1956. Add('program '+ExtractFileUnitName(Filename)+';');
  1957. Add('');
  1958. end;
  1959. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1960. SystemUnitParts: TSystemUnitParts);
  1961. begin
  1962. if NeedSystemUnit then
  1963. AddSystemUnit(SystemUnitParts)
  1964. else
  1965. Parser.ImplicitUses.Clear;
  1966. Add('library '+ExtractFileUnitName(Filename)+';');
  1967. Add('');
  1968. end;
  1969. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1970. SystemUnitParts: TSystemUnitParts);
  1971. begin
  1972. if NeedSystemUnit then
  1973. AddSystemUnit(SystemUnitParts)
  1974. else
  1975. Parser.ImplicitUses.Clear;
  1976. Add('unit Test1;');
  1977. Add('');
  1978. end;
  1979. procedure TCustomTestModule.ConvertModule;
  1980. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1981. out UsesLit: TJSArrayLiteral);
  1982. var
  1983. i: Integer;
  1984. Item: TJSElement;
  1985. Lit: TJSLiteral;
  1986. begin
  1987. UsesLit:=nil;
  1988. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1989. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1990. exit; // null is ok
  1991. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1992. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1993. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1994. begin
  1995. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1996. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1997. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1998. Lit:=TJSLiteral(Item);
  1999. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  2000. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  2001. end;
  2002. end;
  2003. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2004. out Src: TJSSourceElements);
  2005. var
  2006. FunDecl: TJSFunctionDeclarationStatement;
  2007. FunDef: TJSFuncDef;
  2008. FunBody: TJSFunctionBody;
  2009. begin
  2010. Src:=nil;
  2011. AssertNotNull(ParamName,Arg.Expr);
  2012. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2013. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2014. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2015. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2016. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2017. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2018. AssertNotNull(ParamName+' body',FunDef.Body);
  2019. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2020. FunBody:=FunDef.Body as TJSFunctionBody;
  2021. AssertNotNull(ParamName+' body.A',FunBody.A);
  2022. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2023. Src:=FunBody.A as TJSSourceElements;
  2024. end;
  2025. var
  2026. ModuleNameExpr: TJSLiteral;
  2027. InitFunction: TJSFunctionDeclarationStatement;
  2028. InitAssign: TJSSimpleAssignStatement;
  2029. InitName: String;
  2030. LastNode, FirstNode: TJSElement;
  2031. Arg: TJSArrayLiteralElement;
  2032. IsProg, IsLib: Boolean;
  2033. begin
  2034. if SkipTests then exit;
  2035. IsProg:=false;
  2036. IsLib:=false;
  2037. if Module is TPasProgram then
  2038. IsProg:=true
  2039. else if Module is TPasLibrary then
  2040. IsLib:=true;
  2041. try
  2042. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2043. except
  2044. on E: Exception do
  2045. HandleException(E);
  2046. end;
  2047. if SkipTests then exit;
  2048. if ExpectedErrorClass<>nil then
  2049. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2050. FJSSource:=TStringList.Create;
  2051. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2052. {$IFDEF VerbosePas2JS}
  2053. writeln('TTestModule.ConvertModule JS:');
  2054. write(FJSSource.Text);
  2055. {$ENDIF}
  2056. // rtl.module(...
  2057. if JSModule.Statements.Count<1 then
  2058. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2059. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2060. AssertNotNull('register module call',FirstNode);
  2061. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2062. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2063. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2064. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2065. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2066. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2067. // parameter 'unitname'
  2068. if JSModuleCallArgs.Elements.Count<1 then
  2069. Fail('rtl.module first param unit missing');
  2070. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2071. AssertNotNull('module name param',Arg.Expr);
  2072. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2073. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2074. if IsProg then
  2075. begin
  2076. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2077. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2078. end
  2079. else if IsLib then
  2080. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2081. else
  2082. begin
  2083. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2084. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2085. end;
  2086. // main uses section
  2087. if JSModuleCallArgs.Elements.Count<2 then
  2088. Fail('rtl.module second param main uses missing');
  2089. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2090. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2091. // program/library/interface function()
  2092. if JSModuleCallArgs.Elements.Count<3 then
  2093. Fail('rtl.module third param intf-function missing');
  2094. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2095. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2096. // search for $mod.$init or $mod.$main - the last statement
  2097. if IsProg or IsLib then
  2098. begin
  2099. InitName:='$main';
  2100. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2101. end
  2102. else
  2103. InitName:='$init';
  2104. InitAssign:=nil;
  2105. InitFunction:=nil;
  2106. FJSInitBody:=nil;
  2107. if JSModuleSrc.Statements.Count>0 then
  2108. begin
  2109. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2110. if LastNode is TJSSimpleAssignStatement then
  2111. begin
  2112. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2113. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2114. begin
  2115. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2116. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2117. end
  2118. else if IsProg or IsLib then
  2119. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2120. end;
  2121. end;
  2122. // optional: implementation uses section
  2123. if JSModuleCallArgs.Elements.Count<4 then
  2124. exit;
  2125. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2126. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2127. end;
  2128. procedure TCustomTestModule.ConvertProgram;
  2129. begin
  2130. Add('end.');
  2131. ParseProgram;
  2132. ConvertModule;
  2133. end;
  2134. procedure TCustomTestModule.ConvertLibrary;
  2135. begin
  2136. Add('end.');
  2137. ParseLibrary;
  2138. ConvertModule;
  2139. end;
  2140. procedure TCustomTestModule.ConvertUnit;
  2141. begin
  2142. Add('end.');
  2143. ParseUnit;
  2144. ConvertModule;
  2145. end;
  2146. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2147. begin
  2148. Result:=tcmodules.JSToStr(El);
  2149. end;
  2150. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2151. DottedName: string);
  2152. begin
  2153. if DottedName='' then
  2154. begin
  2155. AssertNull(Msg,El);
  2156. end
  2157. else
  2158. begin
  2159. AssertNotNull(Msg,El);
  2160. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2161. end;
  2162. end;
  2163. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2164. begin
  2165. if El=nil then
  2166. Result:=''
  2167. else if El is TJSPrimaryExpressionIdent then
  2168. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2169. else if El is TJSDotMemberExpression then
  2170. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2171. else
  2172. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2173. end;
  2174. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2175. InitStatements: string; ImplStatements: string);
  2176. var
  2177. ActualSrc, ExpectedSrc, InitName: String;
  2178. IsProg, IsLib: Boolean;
  2179. begin
  2180. ActualSrc:=JSToStr(JSModuleSrc);
  2181. if coUseStrict in Converter.Options then
  2182. ExpectedSrc:='"use strict";'+LineEnding
  2183. else
  2184. ExpectedSrc:='';
  2185. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2186. ExpectedSrc:=ExpectedSrc+Statements;
  2187. // unit implementation
  2188. if (Trim(ImplStatements)<>'') then
  2189. ExpectedSrc:=ExpectedSrc+LineEnding
  2190. +'$mod.$implcode = function () {'+LineEnding
  2191. +ImplStatements
  2192. +'};'+LineEnding;
  2193. // program main or unit initialization
  2194. IsProg:=false;
  2195. IsLib:=false;
  2196. if Module is TPasProgram then
  2197. IsProg:=true
  2198. else if Module is TPasLibrary then
  2199. IsLib:=true;
  2200. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2201. begin
  2202. if IsProg or IsLib then
  2203. InitName:='$main'
  2204. else
  2205. InitName:='$init';
  2206. ExpectedSrc:=ExpectedSrc+LineEnding
  2207. +'$mod.'+InitName+' = function () {'+LineEnding
  2208. +InitStatements
  2209. +'};'+LineEnding;
  2210. end;
  2211. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2212. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2213. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2214. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2215. end;
  2216. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2217. var
  2218. ActualSrc: String;
  2219. begin
  2220. ActualSrc:=JSToStr(JSModule);
  2221. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2222. end;
  2223. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2224. // search diff, ignore changes in spaces
  2225. var
  2226. s: string;
  2227. begin
  2228. if CheckSrcDiff(Expected,Actual,s) then exit;
  2229. Fail(Msg+': '+s);
  2230. end;
  2231. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2232. var
  2233. aResolver: TTestEnginePasResolver;
  2234. aConverter: TPasToJSConverter;
  2235. aJSModule: TJSSourceElements;
  2236. ActualSrc: String;
  2237. begin
  2238. aResolver:=GetResolver(Filename);
  2239. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2240. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2241. {$IFDEF VerbosePas2JS}
  2242. writeln('CheckUnit '+Filename+' converting ...');
  2243. {$ENDIF}
  2244. aConverter:=CreateConverter;
  2245. aJSModule:=nil;
  2246. try
  2247. try
  2248. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2249. except
  2250. on E: Exception do
  2251. HandleException(E);
  2252. end;
  2253. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2254. {$IFDEF VerbosePas2JS}
  2255. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2256. write(aResolver.Source);
  2257. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2258. write(ActualSrc);
  2259. {$ENDIF}
  2260. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2261. finally
  2262. aJSModule.Free;
  2263. aConverter.Free;
  2264. end;
  2265. end;
  2266. procedure TCustomTestModule.CheckReferenceDirectives;
  2267. var
  2268. CurFilename: string;
  2269. LineNumber: Integer;
  2270. SrcLine: String;
  2271. CommentStartP, CommentEndP: PChar;
  2272. procedure RaiseError(Msg: string; p: PChar);
  2273. begin
  2274. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2275. end;
  2276. procedure AddMarker(Marker: PSrcMarker);
  2277. begin
  2278. if LastSrcMarker<>nil then
  2279. LastSrcMarker^.Next:=Marker
  2280. else
  2281. FirstSrcMarker:=Marker;
  2282. LastSrcMarker:=Marker;
  2283. end;
  2284. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2285. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2286. begin
  2287. New(Result);
  2288. Result^.Kind:=Kind;
  2289. Result^.Filename:=aFilename;
  2290. Result^.Row:=aLine;
  2291. Result^.StartCol:=aStartCol;
  2292. Result^.EndCol:=aEndCol;
  2293. Result^.Identifier:=Identifier;
  2294. Result^.Next:=nil;
  2295. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2296. AddMarker(Result);
  2297. end;
  2298. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2299. const Identifier: string): PSrcMarker;
  2300. var
  2301. TokenStart, p: PChar;
  2302. begin
  2303. p:=CommentEndP;
  2304. ReadNextPascalToken(p,TokenStart,false,false);
  2305. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2306. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2307. end;
  2308. function ReadIdentifier(var p: PChar): string;
  2309. var
  2310. StartP: PChar;
  2311. begin
  2312. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2313. RaiseError('identifier expected',p);
  2314. StartP:=p;
  2315. inc(p);
  2316. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2317. Result:='';
  2318. SetLength(Result,p-StartP);
  2319. Move(StartP^,Result[1],length(Result));
  2320. end;
  2321. procedure AddLabel;
  2322. var
  2323. Identifier: String;
  2324. p: PChar;
  2325. begin
  2326. p:=CommentStartP+2;
  2327. Identifier:=ReadIdentifier(p);
  2328. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2329. if FindSrcLabel(Identifier)<>nil then
  2330. RaiseError('duplicate label "'+Identifier+'"',p);
  2331. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2332. end;
  2333. procedure AddResolverReference;
  2334. var
  2335. Identifier: String;
  2336. p: PChar;
  2337. begin
  2338. p:=CommentStartP+2;
  2339. Identifier:=ReadIdentifier(p);
  2340. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2341. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2342. end;
  2343. procedure AddDirectReference;
  2344. var
  2345. Identifier: String;
  2346. p: PChar;
  2347. begin
  2348. p:=CommentStartP+2;
  2349. Identifier:=ReadIdentifier(p);
  2350. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2351. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2352. end;
  2353. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2354. var
  2355. p: PChar;
  2356. IsDirective: Boolean;
  2357. begin
  2358. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2359. CurFilename:=aFilename;
  2360. // parse code, find all labels
  2361. LineNumber:=0;
  2362. while LineNumber<SrcLines.Count do
  2363. begin
  2364. inc(LineNumber);
  2365. SrcLine:=SrcLines[LineNumber-1];
  2366. if SrcLine='' then continue;
  2367. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2368. p:=PChar(SrcLine);
  2369. repeat
  2370. case p^ of
  2371. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2372. '{':
  2373. begin
  2374. CommentStartP:=p;
  2375. inc(p);
  2376. IsDirective:=p^ in ['#','@','='];
  2377. // skip to end of comment
  2378. repeat
  2379. case p^ of
  2380. #0:
  2381. if (p-PChar(SrcLine)=length(SrcLine)) then
  2382. begin
  2383. // multi line comment
  2384. if IsDirective then
  2385. RaiseError('directive missing closing bracket',CommentStartP);
  2386. repeat
  2387. inc(LineNumber);
  2388. if LineNumber>SrcLines.Count then exit;
  2389. SrcLine:=SrcLines[LineNumber-1];
  2390. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2391. until SrcLine<>'';
  2392. p:=PChar(SrcLine);
  2393. continue;
  2394. end;
  2395. '}':
  2396. begin
  2397. inc(p);
  2398. break;
  2399. end;
  2400. end;
  2401. inc(p);
  2402. until false;
  2403. CommentEndP:=p;
  2404. case CommentStartP[1] of
  2405. '#': AddLabel;
  2406. '@': AddResolverReference;
  2407. '=': AddDirectReference;
  2408. end;
  2409. p:=CommentEndP;
  2410. continue;
  2411. end;
  2412. '/':
  2413. if p[1]='/' then
  2414. break; // rest of line is comment -> skip
  2415. end;
  2416. inc(p);
  2417. until false;
  2418. end;
  2419. end;
  2420. procedure CheckResolverReference(aMarker: PSrcMarker);
  2421. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2422. var
  2423. aLabel: PSrcMarker;
  2424. ReferenceElements, LabelElements: TFPList;
  2425. i, j, aLine, aCol: Integer;
  2426. El, Ref, LabelEl: TPasElement;
  2427. begin
  2428. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2429. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2430. if aLabel=nil then
  2431. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2432. LabelElements:=nil;
  2433. ReferenceElements:=nil;
  2434. try
  2435. LabelElements:=FindElementsAt(aLabel);
  2436. ReferenceElements:=FindElementsAt(aMarker);
  2437. for i:=0 to ReferenceElements.Count-1 do
  2438. begin
  2439. El:=TPasElement(ReferenceElements[i]);
  2440. Ref:=nil;
  2441. if El.CustomData is TResolvedReference then
  2442. Ref:=TResolvedReference(El.CustomData).Declaration
  2443. else if El.CustomData is TPasPropertyScope then
  2444. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2445. else if El.CustomData is TPasSpecializeTypeData then
  2446. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2447. if Ref<>nil then
  2448. for j:=0 to LabelElements.Count-1 do
  2449. begin
  2450. LabelEl:=TPasElement(LabelElements[j]);
  2451. if Ref=LabelEl then
  2452. exit; // success
  2453. end;
  2454. end;
  2455. // failure write candidates
  2456. for i:=0 to ReferenceElements.Count-1 do
  2457. begin
  2458. El:=TPasElement(ReferenceElements[i]);
  2459. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2460. write(' El=',GetObjName(El));
  2461. if EL is TPrimitiveExpr then
  2462. begin
  2463. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2464. end;
  2465. Ref:=nil;
  2466. if El.CustomData is TResolvedReference then
  2467. Ref:=TResolvedReference(El.CustomData).Declaration
  2468. else if El.CustomData is TPasPropertyScope then
  2469. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2470. else if El.CustomData is TPasSpecializeTypeData then
  2471. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2472. if Ref<>nil then
  2473. begin
  2474. write(' Decl=',GetObjName(Ref));
  2475. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2476. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2477. end
  2478. else
  2479. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2480. writeln;
  2481. end;
  2482. for i:=0 to LabelElements.Count-1 do
  2483. begin
  2484. El:=TPasElement(LabelElements[i]);
  2485. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2486. write(' El=',GetObjName(El));
  2487. writeln;
  2488. end;
  2489. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2490. finally
  2491. LabelElements.Free;
  2492. ReferenceElements.Free;
  2493. end;
  2494. end;
  2495. procedure CheckDirectReference(aMarker: PSrcMarker);
  2496. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2497. var
  2498. aLabel: PSrcMarker;
  2499. ReferenceElements, LabelElements: TFPList;
  2500. i, LabelLine, LabelCol, j: Integer;
  2501. El, LabelEl: TPasElement;
  2502. DeclEl, TypeEl: TPasType;
  2503. begin
  2504. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2505. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2506. if aLabel=nil then
  2507. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2508. LabelElements:=nil;
  2509. ReferenceElements:=nil;
  2510. try
  2511. //writeln('CheckDirectReference finding elements at label ...');
  2512. LabelElements:=FindElementsAt(aLabel);
  2513. //writeln('CheckDirectReference finding elements at reference ...');
  2514. ReferenceElements:=FindElementsAt(aMarker);
  2515. for i:=0 to ReferenceElements.Count-1 do
  2516. begin
  2517. El:=TPasElement(ReferenceElements[i]);
  2518. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2519. if El.ClassType=TPasVariable then
  2520. begin
  2521. if TPasVariable(El).VarType=nil then
  2522. begin
  2523. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2524. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2525. end;
  2526. TypeEl:=TPasVariable(El).VarType;
  2527. for j:=0 to LabelElements.Count-1 do
  2528. begin
  2529. LabelEl:=TPasElement(LabelElements[j]);
  2530. if TypeEl=LabelEl then
  2531. exit; // success
  2532. end;
  2533. end
  2534. else if El is TPasAliasType then
  2535. begin
  2536. DeclEl:=TPasAliasType(El).DestType;
  2537. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2538. if (aLabel^.Filename=DeclEl.SourceFilename)
  2539. and (integer(aLabel^.Row)=LabelLine)
  2540. and (aLabel^.StartCol<=LabelCol)
  2541. and (aLabel^.EndCol>=LabelCol) then
  2542. exit; // success
  2543. end
  2544. else if El.ClassType=TPasArgument then
  2545. begin
  2546. TypeEl:=TPasArgument(El).ArgType;
  2547. for j:=0 to LabelElements.Count-1 do
  2548. begin
  2549. LabelEl:=TPasElement(LabelElements[j]);
  2550. if TypeEl=LabelEl then
  2551. exit; // success
  2552. end;
  2553. end;
  2554. end;
  2555. // failed -> show candidates
  2556. writeln('CheckDirectReference failed: Labels:');
  2557. for j:=0 to LabelElements.Count-1 do
  2558. begin
  2559. LabelEl:=TPasElement(LabelElements[j]);
  2560. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2561. end;
  2562. writeln('CheckDirectReference failed: References:');
  2563. for i:=0 to ReferenceElements.Count-1 do
  2564. begin
  2565. El:=TPasElement(ReferenceElements[i]);
  2566. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2567. //if EL is TPasVariable then
  2568. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2569. end;
  2570. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2571. finally
  2572. LabelElements.Free;
  2573. ReferenceElements.Free;
  2574. end;
  2575. end;
  2576. var
  2577. aMarker: PSrcMarker;
  2578. i: Integer;
  2579. SrcLines: TStringList;
  2580. begin
  2581. Module.ForEachCall(@OnCheckElementParent,nil);
  2582. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2583. // find all markers
  2584. for i:=0 to FileResolver.Streams.Count-1 do
  2585. begin
  2586. GetSrc(i,SrcLines,CurFilename);
  2587. ParseCode(SrcLines,CurFilename);
  2588. SrcLines.Free;
  2589. end;
  2590. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2591. // check references
  2592. aMarker:=FirstSrcMarker;
  2593. while aMarker<>nil do
  2594. begin
  2595. case aMarker^.Kind of
  2596. mkResolverReference: CheckResolverReference(aMarker);
  2597. mkDirectReference: CheckDirectReference(aMarker);
  2598. end;
  2599. aMarker:=aMarker^.Next;
  2600. end;
  2601. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2602. end;
  2603. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2604. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2605. var
  2606. i: Integer;
  2607. Item: TTestHintMessage;
  2608. Expected,Actual: string;
  2609. begin
  2610. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2611. for i:=0 to MsgCount-1 do
  2612. begin
  2613. Item:=Msgs[i];
  2614. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2615. if (Marker<>nil) then
  2616. begin
  2617. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2618. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2619. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2620. end;
  2621. // found
  2622. FHintMsgsGood.Add(Item);
  2623. str(Item.MsgType,Actual);
  2624. str(MsgType,Expected);
  2625. AssertEquals('MsgType',Expected,Actual);
  2626. exit;
  2627. end;
  2628. // needed message missing -> show emitted messages
  2629. WriteSources('',0,0);
  2630. for i:=0 to MsgCount-1 do
  2631. begin
  2632. Item:=Msgs[i];
  2633. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2634. ' ('+IntToStr(Item.MsgNumber),')');
  2635. if Marker<>nil then
  2636. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2637. writeln(' {',Item.Msg,'}');
  2638. end;
  2639. str(MsgType,Expected);
  2640. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2641. if Marker<>nil then
  2642. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2643. Actual:=Actual+' '+Msg;
  2644. Fail(Actual);
  2645. end;
  2646. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2647. );
  2648. var
  2649. i: Integer;
  2650. s, Txt: String;
  2651. Msg: TTestHintMessage;
  2652. begin
  2653. for i:=0 to MsgCount-1 do
  2654. begin
  2655. Msg:=Msgs[i];
  2656. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2657. s:='';
  2658. str(Msg.MsgType,s);
  2659. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2660. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2661. if WithSourcePos then
  2662. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2663. Txt:=Txt+' {'+Msg.Msg+'}';
  2664. Fail(Txt);
  2665. end;
  2666. end;
  2667. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2668. MsgNumber: integer);
  2669. begin
  2670. ExpectedErrorClass:=EScannerError;
  2671. ExpectedErrorMsg:=Msg;
  2672. ExpectedErrorNumber:=MsgNumber;
  2673. end;
  2674. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2675. MsgNumber: integer);
  2676. begin
  2677. ExpectedErrorClass:=EParserError;
  2678. ExpectedErrorMsg:=Msg;
  2679. ExpectedErrorNumber:=MsgNumber;
  2680. end;
  2681. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2682. MsgNumber: integer);
  2683. begin
  2684. ExpectedErrorClass:=EPasResolve;
  2685. ExpectedErrorMsg:=Msg;
  2686. ExpectedErrorNumber:=MsgNumber;
  2687. end;
  2688. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2689. MsgNumber: integer);
  2690. begin
  2691. ExpectedErrorClass:=EPas2JS;
  2692. ExpectedErrorMsg:=Msg;
  2693. ExpectedErrorNumber:=MsgNumber;
  2694. end;
  2695. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2696. var
  2697. MsgNumber: Integer;
  2698. Msg: String;
  2699. begin
  2700. Result:=false;
  2701. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2702. Msg:=E.Message;
  2703. if E is EPas2JS then
  2704. MsgNumber:=EPas2JS(E).MsgNumber
  2705. else if E is EPasResolve then
  2706. MsgNumber:=EPasResolve(E).MsgNumber
  2707. else if E is EParserError then
  2708. MsgNumber:=Parser.LastMsgNumber
  2709. else if E is EScannerError then
  2710. begin
  2711. MsgNumber:=Scanner.LastMsgNumber;
  2712. Msg:=Scanner.LastMsg;
  2713. end
  2714. else
  2715. MsgNumber:=0;
  2716. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2717. if Result then
  2718. SkipTests:=true;
  2719. end;
  2720. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2721. const aFilename: string; aRow, aCol: integer);
  2722. var
  2723. s: String;
  2724. begin
  2725. WriteSources(aFilename,aRow,aCol);
  2726. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2727. writeln('ERROR: ',s);
  2728. Fail(s);
  2729. end;
  2730. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2731. aMarker: PSrcMarker);
  2732. begin
  2733. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2734. end;
  2735. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2736. begin
  2737. if IsErrorExpected(E) then exit;
  2738. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2739. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2740. +' '+Scanner.CurFilename
  2741. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2742. FailException(E);
  2743. end;
  2744. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2745. begin
  2746. if IsErrorExpected(E) then exit;
  2747. WriteSources(E.Filename,E.Row,E.Column);
  2748. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2749. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2750. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2751. );
  2752. FailException(E);
  2753. end;
  2754. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2755. var
  2756. P: TPasSourcePos;
  2757. begin
  2758. if IsErrorExpected(E) then exit;
  2759. P:=E.SourcePos;
  2760. WriteSources(P.FileName,P.Row,P.Column);
  2761. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2762. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2763. FailException(E);
  2764. end;
  2765. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2766. var
  2767. Row, Col: integer;
  2768. begin
  2769. if IsErrorExpected(E) then exit;
  2770. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2771. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2772. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2773. +' '+E.PasElement.SourceFilename
  2774. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2775. FailException(E);
  2776. end;
  2777. procedure TCustomTestModule.HandleException(E: Exception);
  2778. begin
  2779. if E is EScannerError then
  2780. HandleScannerError(EScannerError(E))
  2781. else if E is EParserError then
  2782. HandleParserError(EParserError(E))
  2783. else if E is EPasResolve then
  2784. HandlePasResolveError(EPasResolve(E))
  2785. else if E is EPas2JS then
  2786. HandlePas2JSError(EPas2JS(E))
  2787. else
  2788. begin
  2789. if IsErrorExpected(E) then exit;
  2790. if not (E is EAssertionFailedError) then
  2791. begin
  2792. WriteSources('',0,0);
  2793. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2794. end;
  2795. FailException(E);
  2796. end;
  2797. end;
  2798. procedure TCustomTestModule.FailException(E: Exception);
  2799. var
  2800. MsgNumber: Integer;
  2801. begin
  2802. if ExpectedErrorClass<>nil then
  2803. begin
  2804. if FExpectedErrorClass=E.ClassType then
  2805. begin
  2806. if E is EPas2JS then
  2807. MsgNumber:=EPas2JS(E).MsgNumber
  2808. else if E is EPasResolve then
  2809. MsgNumber:=EPasResolve(E).MsgNumber
  2810. else if E is EParserError then
  2811. MsgNumber:=Parser.LastMsgNumber
  2812. else if E is EScannerError then
  2813. MsgNumber:=Scanner.LastMsgNumber
  2814. else
  2815. MsgNumber:=0;
  2816. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2817. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2818. ExpectedErrorNumber,MsgNumber);
  2819. end else begin
  2820. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2821. end;
  2822. end;
  2823. Fail(E.Message);
  2824. end;
  2825. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2826. aCol: integer);
  2827. var
  2828. IsSrc: Boolean;
  2829. i, j: Integer;
  2830. SrcLines: TStringList;
  2831. Line: string;
  2832. aModule: TTestEnginePasResolver;
  2833. begin
  2834. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2835. for i:=0 to ResolverCount-1 do
  2836. begin
  2837. aModule:=Resolvers[i];
  2838. SrcLines:=TStringList.Create;
  2839. try
  2840. SrcLines.Text:=aModule.Source;
  2841. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2842. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2843. for j:=1 to SrcLines.Count do
  2844. begin
  2845. Line:=SrcLines[j-1];
  2846. if IsSrc and (j=aRow) then
  2847. begin
  2848. write('*');
  2849. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2850. end;
  2851. writeln(Format('%:4d: ',[j]),Line);
  2852. end;
  2853. finally
  2854. SrcLines.Free;
  2855. end;
  2856. end;
  2857. end;
  2858. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2859. var
  2860. i: Integer;
  2861. begin
  2862. for i:=0 to ResolverCount-1 do
  2863. if Filename=Resolvers[i].Filename then exit(i);
  2864. Result:=-1;
  2865. end;
  2866. function TCustomTestModule.GetResolver(const Filename: string
  2867. ): TTestEnginePasResolver;
  2868. var
  2869. i: Integer;
  2870. begin
  2871. i:=IndexOfResolver(Filename);
  2872. if i<0 then exit(nil);
  2873. Result:=Resolvers[i];
  2874. end;
  2875. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2876. out aFilename: string);
  2877. var
  2878. aStream: TStream;
  2879. begin
  2880. SrcLines:=TStringList.Create;
  2881. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2882. aStream.Position:=0;
  2883. SrcLines.LoadFromStream(aStream);
  2884. aFilename:=FileResolver.Streams[Index];
  2885. end;
  2886. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2887. aEndCol: integer): TFPList;
  2888. var
  2889. ok: Boolean;
  2890. FoundRefs: TTestResolverReferenceData;
  2891. i: Integer;
  2892. CurResolver: TTestEnginePasResolver;
  2893. begin
  2894. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2895. FoundRefs:=Default(TTestResolverReferenceData);
  2896. FoundRefs.Filename:=aFilename;
  2897. FoundRefs.Row:=aLine;
  2898. FoundRefs.StartCol:=aStartCol;
  2899. FoundRefs.EndCol:=aEndCol;
  2900. FoundRefs.Found:=TFPList.Create;
  2901. ok:=false;
  2902. try
  2903. // find all markers
  2904. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2905. for i:=0 to ResolverCount-1 do
  2906. begin
  2907. CurResolver:=Resolvers[i];
  2908. if CurResolver.Module=Module then continue;
  2909. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2910. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2911. end;
  2912. ok:=true;
  2913. finally
  2914. if not ok then
  2915. FreeAndNil(FoundRefs.Found);
  2916. end;
  2917. Result:=FoundRefs.Found;
  2918. FoundRefs.Found:=nil;
  2919. end;
  2920. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2921. ErrorOnNoElements: boolean): TFPList;
  2922. begin
  2923. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2924. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2925. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2926. end;
  2927. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2928. begin
  2929. Result:=FirstSrcMarker;
  2930. while Result<>nil do
  2931. begin
  2932. if (Result^.Kind=mkLabel)
  2933. and (CompareText(Result^.Identifier,Identifier)=0) then
  2934. exit;
  2935. Result:=Result^.Next;
  2936. end;
  2937. end;
  2938. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2939. ErrorOnNoElements: boolean): TFPList;
  2940. var
  2941. SrcLabel: PSrcMarker;
  2942. begin
  2943. SrcLabel:=FindSrcLabel(Identifier);
  2944. if SrcLabel=nil then
  2945. Fail('missing label "'+Identifier+'"');
  2946. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2947. end;
  2948. function TCustomTestModule.GetDefaultNamespace: string;
  2949. var
  2950. C: TClass;
  2951. begin
  2952. Result:='';
  2953. if FModule=nil then exit;
  2954. C:=FModule.ClassType;
  2955. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2956. Result:=ResolverEngine.DefaultNameSpace;
  2957. end;
  2958. constructor TCustomTestModule.Create;
  2959. begin
  2960. inherited Create;
  2961. FHintMsgs:=TObjectList.Create(true);
  2962. FHintMsgsGood:=TFPList.Create;
  2963. end;
  2964. destructor TCustomTestModule.Destroy;
  2965. begin
  2966. FreeAndNil(FHintMsgs);
  2967. FreeAndNil(FHintMsgsGood);
  2968. inherited Destroy;
  2969. end;
  2970. { TTestModule }
  2971. procedure TTestModule.TestReservedWords;
  2972. var
  2973. i: integer;
  2974. begin
  2975. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2976. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2977. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2978. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2979. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2980. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2981. end;
  2982. procedure TTestModule.TestEmptyProgram;
  2983. begin
  2984. StartProgram(false);
  2985. Add('begin');
  2986. ConvertProgram;
  2987. CheckSource('TestEmptyProgram','','');
  2988. end;
  2989. procedure TTestModule.TestEmptyProgramUseStrict;
  2990. begin
  2991. Converter.Options:=Converter.Options+[coUseStrict];
  2992. StartProgram(false);
  2993. Add('begin');
  2994. ConvertProgram;
  2995. CheckSource('TestEmptyProgramUseStrict','','');
  2996. end;
  2997. procedure TTestModule.TestEmptyUnit;
  2998. begin
  2999. StartUnit(false);
  3000. Add('interface');
  3001. Add('implementation');
  3002. ConvertUnit;
  3003. CheckSource('TestEmptyUnit',
  3004. LinesToStr([
  3005. ]),
  3006. '');
  3007. end;
  3008. procedure TTestModule.TestEmptyUnitUseStrict;
  3009. begin
  3010. Converter.Options:=Converter.Options+[coUseStrict];
  3011. StartUnit(false);
  3012. Add('interface');
  3013. Add('implementation');
  3014. ConvertUnit;
  3015. CheckSource('TestEmptyUnitUseStrict',
  3016. LinesToStr([
  3017. ''
  3018. ]),
  3019. '');
  3020. end;
  3021. procedure TTestModule.TestDottedUnitNames;
  3022. begin
  3023. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3024. LinesToStr([
  3025. 'var iV: longint;'
  3026. ]),
  3027. '');
  3028. FFilename:='ns1.test1.pp';
  3029. StartProgram(true);
  3030. Add('uses unIt2;');
  3031. Add('var');
  3032. Add(' i: longint;');
  3033. Add('begin');
  3034. Add(' i:=iv;');
  3035. Add(' i:=uNit2.iv;');
  3036. Add(' i:=Ns1.TEst1.i;');
  3037. ConvertProgram;
  3038. CheckSource('TestDottedUnitNames',
  3039. LinesToStr([
  3040. 'this.i = 0;',
  3041. '']),
  3042. LinesToStr([ // this.$init
  3043. '$mod.i = pas["NS1.Unit2"].iV;',
  3044. '$mod.i = pas["NS1.Unit2"].iV;',
  3045. '$mod.i = $mod.i;',
  3046. '']) );
  3047. end;
  3048. procedure TTestModule.TestDottedUnitNameImpl;
  3049. begin
  3050. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3051. LinesToStr([
  3052. 'type',
  3053. ' TObject = class end;',
  3054. ' TTestA = class',
  3055. ' end;'
  3056. ]),
  3057. LinesToStr(['uses TEST.UnitB;'])
  3058. );
  3059. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3060. LinesToStr([
  3061. 'uses TEST.UnitA;',
  3062. 'type TTestB = class(TTestA);'
  3063. ]),
  3064. ''
  3065. );
  3066. StartProgram(true);
  3067. Add('uses TEST.UnitA;');
  3068. Add('begin');
  3069. ConvertProgram;
  3070. CheckSource('TestDottedUnitNameImpl',
  3071. LinesToStr([
  3072. '']),
  3073. LinesToStr([ // this.$init
  3074. '']) );
  3075. CheckUnit('TEST.UnitA.pas',
  3076. LinesToStr([
  3077. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3078. ' var $mod = this;',
  3079. ' rtl.createClass(this, "TObject", null, function () {',
  3080. ' this.$init = function () {',
  3081. ' };',
  3082. ' this.$final = function () {',
  3083. ' };',
  3084. ' });',
  3085. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3086. ' });',
  3087. '}, ["TEST.UnitB"]);'
  3088. ]));
  3089. CheckUnit('TEST.UnitB.pas',
  3090. LinesToStr([
  3091. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3092. ' var $mod = this;',
  3093. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3094. ' });',
  3095. '});'
  3096. ]));
  3097. end;
  3098. procedure TTestModule.TestDottedUnitExpr;
  3099. begin
  3100. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3101. LinesToStr([
  3102. 'procedure DoIt;'
  3103. ]),
  3104. 'procedure DoIt; begin end;');
  3105. FFilename:='Ns1.SubNs1.Test1.pp';
  3106. StartProgram(true);
  3107. Add('uses Ns2.sUbnS2.unIt2;');
  3108. Add('var');
  3109. Add(' i: longint;');
  3110. Add('begin');
  3111. Add(' ns2.subns2.unit2.doit;');
  3112. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3113. ConvertProgram;
  3114. CheckSource('TestDottedUnitExpr',
  3115. LinesToStr([
  3116. 'this.i = 0;',
  3117. '']),
  3118. LinesToStr([ // this.$init
  3119. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3120. '$mod.i = $mod.i;',
  3121. '']) );
  3122. end;
  3123. procedure TTestModule.Test_ModeFPCFail;
  3124. begin
  3125. StartProgram(false);
  3126. Add('{$mode FPC}');
  3127. Add('begin');
  3128. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3129. ConvertProgram;
  3130. end;
  3131. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3132. begin
  3133. StartProgram(false);
  3134. Add('{$modeswitch cblocks-}');
  3135. Add('begin');
  3136. ConvertProgram;
  3137. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3138. CheckResolverUnexpectedHints();
  3139. end;
  3140. procedure TTestModule.TestUnit_UseSystem;
  3141. begin
  3142. StartUnit(true);
  3143. Add([
  3144. 'interface',
  3145. 'var i: integer;',
  3146. 'implementation']);
  3147. ConvertUnit;
  3148. CheckSource('TestUnit_UseSystem',
  3149. LinesToStr([
  3150. 'this.i = 0;',
  3151. '']),
  3152. LinesToStr([
  3153. '']) );
  3154. end;
  3155. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3156. begin
  3157. AddModuleWithIntfImplSrc('unit1.pp',
  3158. LinesToStr([
  3159. 'type number = longint;']),
  3160. LinesToStr([
  3161. 'uses test1;',
  3162. 'procedure DoIt;',
  3163. 'begin',
  3164. ' i:=3;',
  3165. 'end;']));
  3166. StartUnit(true);
  3167. Add([
  3168. 'interface',
  3169. 'uses unit1;',
  3170. 'var i: number;',
  3171. 'implementation']);
  3172. ConvertUnit;
  3173. CheckSource('TestUnit_Intf1Impl2Intf1',
  3174. LinesToStr([
  3175. 'this.i = 0;',
  3176. '']),
  3177. LinesToStr([
  3178. '']) );
  3179. end;
  3180. procedure TTestModule.TestIncludeVersion;
  3181. begin
  3182. StartProgram(false);
  3183. Add([
  3184. 'var',
  3185. ' s: string;',
  3186. ' i: word;',
  3187. 'begin',
  3188. ' s:={$I %line%};',
  3189. ' i:={$I %linenum%};',
  3190. ' s:={$I %currentroutine%};',
  3191. ' s:={$I %pas2jsversion%};',
  3192. ' s:={$I %pas2jstarget%};',
  3193. ' s:={$I %pas2jstargetos%};',
  3194. ' s:={$I %pas2jstargetcpu%};',
  3195. ' s:={$I %file%};',
  3196. '']);
  3197. ConvertProgram;
  3198. CheckSource('TestIncludeVersion',
  3199. LinesToStr([
  3200. 'this.s="";',
  3201. 'this.i = 0;']),
  3202. LinesToStr([
  3203. '$mod.s = "7";',
  3204. '$mod.i = 8;',
  3205. '$mod.s = "<anonymous>";',
  3206. '$mod.s = "Comp.Ver.tcmodules";',
  3207. '$mod.s = "Browser";',
  3208. '$mod.s = "Browser";',
  3209. '$mod.s = "ECMAScript5";',
  3210. '$mod.s = "test1.pp";',
  3211. '']));
  3212. end;
  3213. procedure TTestModule.TestVarInt;
  3214. begin
  3215. StartProgram(false);
  3216. Add('var MyI: longint;');
  3217. Add('begin');
  3218. ConvertProgram;
  3219. CheckSource('TestVarInt','this.MyI=0;','');
  3220. end;
  3221. procedure TTestModule.TestVarBaseTypes;
  3222. begin
  3223. StartProgram(false);
  3224. Add('var');
  3225. Add(' i: longint;');
  3226. Add(' s: string;');
  3227. Add(' c: char;');
  3228. Add(' b: boolean;');
  3229. Add(' d: double;');
  3230. Add(' i2: longint = 3;');
  3231. Add(' s2: string = ''foo'';');
  3232. Add(' c2: char = ''4'';');
  3233. Add(' b2: boolean = true;');
  3234. Add(' d2: double = 5.6;');
  3235. Add(' i3: longint = $707;');
  3236. Add(' i4: nativeint = 9007199254740991;');
  3237. Add(' i5: nativeint = -9007199254740991-1;');
  3238. Add(' i6: nativeint = $fffffffffffff;');
  3239. Add(' i7: nativeint = -$fffffffffffff-1;');
  3240. Add(' i8: byte = 00;');
  3241. Add(' u8: nativeuint = $fffffffffffff;');
  3242. Add(' u9: nativeuint = $0000000000000;');
  3243. Add(' u10: nativeuint = $00ff00;');
  3244. Add('begin');
  3245. ConvertProgram;
  3246. CheckSource('TestVarBaseTypes',
  3247. LinesToStr([
  3248. 'this.i = 0;',
  3249. 'this.s = "";',
  3250. 'this.c = "\x00";',
  3251. 'this.b = false;',
  3252. 'this.d = 0.0;',
  3253. 'this.i2 = 3;',
  3254. 'this.s2 = "foo";',
  3255. 'this.c2 = "4";',
  3256. 'this.b2 = true;',
  3257. 'this.d2 = 5.6;',
  3258. 'this.i3 = 0x707;',
  3259. 'this.i4 = 9007199254740991;',
  3260. 'this.i5 = -9007199254740991-1;',
  3261. 'this.i6 = 0xfffffffffffff;',
  3262. 'this.i7 =-0xfffffffffffff-1;',
  3263. 'this.i8 = 0;',
  3264. 'this.u8 = 0xfffffffffffff;',
  3265. 'this.u9 = 0x0;',
  3266. 'this.u10 = 0xff00;'
  3267. ]),
  3268. '');
  3269. end;
  3270. procedure TTestModule.TestBaseTypeSingleFail;
  3271. begin
  3272. StartProgram(false);
  3273. Add('var s: single;');
  3274. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3275. ConvertProgram;
  3276. end;
  3277. procedure TTestModule.TestBaseTypeExtendedFail;
  3278. begin
  3279. StartProgram(false);
  3280. Add('var e: extended;');
  3281. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3282. ConvertProgram;
  3283. end;
  3284. procedure TTestModule.TestConstBaseTypes;
  3285. begin
  3286. StartProgram(false);
  3287. Add('const');
  3288. Add(' i: longint = 3;');
  3289. Add(' s: string = ''foo'';');
  3290. Add(' c: char = ''4'';');
  3291. Add(' b: boolean = true;');
  3292. Add(' d: double = 5.6;');
  3293. Add(' e = low(word);');
  3294. Add(' f = high(word);');
  3295. Add('begin');
  3296. ConvertProgram;
  3297. CheckSource('TestVarBaseTypes',
  3298. LinesToStr([
  3299. 'this.i=3;',
  3300. 'this.s="foo";',
  3301. 'this.c="4";',
  3302. 'this.b=true;',
  3303. 'this.d=5.6;',
  3304. 'this.e = 0;',
  3305. 'this.f = 65535;'
  3306. ]),
  3307. '');
  3308. end;
  3309. procedure TTestModule.TestAliasTypeRef;
  3310. begin
  3311. StartProgram(false);
  3312. Add('type');
  3313. Add(' a=longint;');
  3314. Add(' b=a;');
  3315. Add('var');
  3316. Add(' c: A;');
  3317. Add(' d: B;');
  3318. Add('begin');
  3319. ConvertProgram;
  3320. CheckSource('TestAliasTypeRef',
  3321. LinesToStr([ // statements
  3322. 'this.c = 0;',
  3323. 'this.d = 0;'
  3324. ]),
  3325. LinesToStr([ // this.$main
  3326. ''
  3327. ]));
  3328. end;
  3329. procedure TTestModule.TestTypeCast_BaseTypes;
  3330. begin
  3331. StartProgram(false);
  3332. Add([
  3333. 'var',
  3334. ' i: longint;',
  3335. ' b: boolean;',
  3336. ' d: double;',
  3337. ' s: string;',
  3338. ' c: char;',
  3339. 'begin',
  3340. ' i:=longint(i);',
  3341. ' i:=longint(b);',
  3342. ' b:=boolean(b);',
  3343. ' b:=boolean(i);',
  3344. ' d:=double(d);',
  3345. ' d:=double(i);',
  3346. ' s:=string(s);',
  3347. ' s:=string(c);',
  3348. ' c:=char(c);',
  3349. ' c:=char(i);',
  3350. ' c:=char(65);',
  3351. ' c:=char(#10);',
  3352. ' c:=char(#$E000);',
  3353. '']);
  3354. ConvertProgram;
  3355. CheckSource('TestAliasTypeRef',
  3356. LinesToStr([ // statements
  3357. 'this.i = 0;',
  3358. 'this.b = false;',
  3359. 'this.d = 0.0;',
  3360. 'this.s = "";',
  3361. 'this.c = "\x00";',
  3362. '']),
  3363. LinesToStr([ // this.$main
  3364. '$mod.i = $mod.i;',
  3365. '$mod.i = ($mod.b ? 1 : 0);',
  3366. '$mod.b = $mod.b;',
  3367. '$mod.b = $mod.i != 0;',
  3368. '$mod.d = $mod.d;',
  3369. '$mod.d = $mod.i;',
  3370. '$mod.s = $mod.s;',
  3371. '$mod.s = $mod.c;',
  3372. '$mod.c = $mod.c;',
  3373. '$mod.c = String.fromCharCode($mod.i);',
  3374. '$mod.c = "A";',
  3375. '$mod.c = "\n";',
  3376. '$mod.c = "";',
  3377. '']));
  3378. end;
  3379. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3380. begin
  3381. StartProgram(false);
  3382. Add('type');
  3383. Add(' integer = longint;');
  3384. Add(' TYesNo = boolean;');
  3385. Add(' TFloat = double;');
  3386. Add(' TCaption = string;');
  3387. Add(' TChar = char;');
  3388. Add('var');
  3389. Add(' i: integer;');
  3390. Add(' b: TYesNo;');
  3391. Add(' d: TFloat;');
  3392. Add(' s: TCaption;');
  3393. Add(' c: TChar;');
  3394. Add('begin');
  3395. Add(' i:=integer(i);');
  3396. Add(' i:=integer(b);');
  3397. Add(' b:=TYesNo(b);');
  3398. Add(' b:=TYesNo(i);');
  3399. Add(' d:=TFloat(d);');
  3400. Add(' d:=TFloat(i);');
  3401. Add(' s:=TCaption(s);');
  3402. Add(' s:=TCaption(c);');
  3403. Add(' c:=TChar(c);');
  3404. ConvertProgram;
  3405. CheckSource('TestAliasTypeRef',
  3406. LinesToStr([ // statements
  3407. 'this.i = 0;',
  3408. 'this.b = false;',
  3409. 'this.d = 0.0;',
  3410. 'this.s = "";',
  3411. 'this.c = "\x00";',
  3412. '']),
  3413. LinesToStr([ // this.$main
  3414. '$mod.i = $mod.i;',
  3415. '$mod.i = ($mod.b ? 1 : 0);',
  3416. '$mod.b = $mod.b;',
  3417. '$mod.b = $mod.i != 0;',
  3418. '$mod.d = $mod.d;',
  3419. '$mod.d = $mod.i;',
  3420. '$mod.s = $mod.s;',
  3421. '$mod.s = $mod.c;',
  3422. '$mod.c = $mod.c;',
  3423. '']));
  3424. end;
  3425. procedure TTestModule.TestEmptyProc;
  3426. begin
  3427. StartProgram(false);
  3428. Add('procedure Test;');
  3429. Add('begin');
  3430. Add('end;');
  3431. Add('begin');
  3432. ConvertProgram;
  3433. CheckSource('TestEmptyProc',
  3434. LinesToStr([ // statements
  3435. 'this.Test = function () {',
  3436. '};'
  3437. ]),
  3438. LinesToStr([ // this.$main
  3439. ''
  3440. ]));
  3441. end;
  3442. procedure TTestModule.TestProcOneParam;
  3443. begin
  3444. StartProgram(false);
  3445. Add('procedure ProcA(i: longint);');
  3446. Add('begin');
  3447. Add('end;');
  3448. Add('begin');
  3449. Add(' PROCA(3);');
  3450. ConvertProgram;
  3451. CheckSource('TestProcOneParam',
  3452. LinesToStr([ // statements
  3453. 'this.ProcA = function (i) {',
  3454. '};'
  3455. ]),
  3456. LinesToStr([ // this.$main
  3457. '$mod.ProcA(3);'
  3458. ]));
  3459. end;
  3460. procedure TTestModule.TestFunctionWithoutParams;
  3461. begin
  3462. StartProgram(false);
  3463. Add('function FuncA: longint;');
  3464. Add('begin');
  3465. Add('end;');
  3466. Add('var i: longint;');
  3467. Add('begin');
  3468. Add(' I:=FUNCA();');
  3469. Add(' I:=FUNCA;');
  3470. Add(' FUNCA();');
  3471. Add(' FUNCA;');
  3472. ConvertProgram;
  3473. CheckSource('TestProcWithoutParams',
  3474. LinesToStr([ // statements
  3475. 'this.FuncA = function () {',
  3476. ' var Result = 0;',
  3477. ' return Result;',
  3478. '};',
  3479. 'this.i=0;'
  3480. ]),
  3481. LinesToStr([ // this.$main
  3482. '$mod.i=$mod.FuncA();',
  3483. '$mod.i=$mod.FuncA();',
  3484. '$mod.FuncA();',
  3485. '$mod.FuncA();'
  3486. ]));
  3487. end;
  3488. procedure TTestModule.TestProcedureWithoutParams;
  3489. begin
  3490. StartProgram(false);
  3491. Add('procedure ProcA;');
  3492. Add('begin');
  3493. Add('end;');
  3494. Add('begin');
  3495. Add(' PROCA();');
  3496. Add(' PROCA;');
  3497. ConvertProgram;
  3498. CheckSource('TestProcWithoutParams',
  3499. LinesToStr([ // statements
  3500. 'this.ProcA = function () {',
  3501. '};'
  3502. ]),
  3503. LinesToStr([ // this.$main
  3504. '$mod.ProcA();',
  3505. '$mod.ProcA();'
  3506. ]));
  3507. end;
  3508. procedure TTestModule.TestIncDec;
  3509. begin
  3510. StartProgram(false);
  3511. Add([
  3512. 'procedure DoIt(var i: longint);',
  3513. 'begin',
  3514. ' inc(i);',
  3515. ' inc(i,2);',
  3516. 'end;',
  3517. 'var',
  3518. ' Bar: longint;',
  3519. 'begin',
  3520. ' inc(bar);',
  3521. ' inc(bar,2);',
  3522. ' dec(bar);',
  3523. ' dec(bar,3);',
  3524. '']);
  3525. ConvertProgram;
  3526. CheckSource('TestIncDec',
  3527. LinesToStr([ // statements
  3528. 'this.DoIt = function (i) {',
  3529. ' i.set(i.get()+1);',
  3530. ' i.set(i.get()+2);',
  3531. '};',
  3532. 'this.Bar = 0;'
  3533. ]),
  3534. LinesToStr([ // this.$main
  3535. '$mod.Bar+=1;',
  3536. '$mod.Bar+=2;',
  3537. '$mod.Bar-=1;',
  3538. '$mod.Bar-=3;'
  3539. ]));
  3540. end;
  3541. procedure TTestModule.TestLoHiFpcMode;
  3542. begin
  3543. StartProgram(false);
  3544. Add([
  3545. '{$mode objfpc}',
  3546. 'const',
  3547. ' LoByte1 = Lo(Word($1234));',
  3548. ' HiByte1 = Hi(Word($1234));',
  3549. ' LoByte2 = Lo(SmallInt($1234));',
  3550. ' HiByte2 = Hi(SmallInt($1234));',
  3551. ' LoWord1 = Lo($1234CDEF);',
  3552. ' HiWord1 = Hi($1234CDEF);',
  3553. ' LoWord2 = Lo(-$1234CDEF);',
  3554. ' HiWord2 = Hi(-$1234CDEF);',
  3555. ' lo4:byte=lo(byte($34));',
  3556. ' hi4:byte=hi(byte($34));',
  3557. ' lo5:byte=lo(shortint(-$34));',
  3558. ' hi5:byte=hi(shortint(-$34));',
  3559. ' lo6:longword=lo($123456789ABCD);',
  3560. ' hi6:longword=hi($123456789ABCD);',
  3561. ' lo7:longword=lo(-$123456789ABCD);',
  3562. ' hi7:longword=hi(-$123456789ABCD);',
  3563. 'var',
  3564. ' b: Byte;',
  3565. ' ss: shortint;',
  3566. ' w: Word;',
  3567. ' si: SmallInt;',
  3568. ' lw: LongWord;',
  3569. ' li: LongInt;',
  3570. ' b2: Byte;',
  3571. ' ni: nativeint;',
  3572. 'begin',
  3573. ' w := $1234;',
  3574. ' ss := -$12;',
  3575. ' b := lo(ss);',
  3576. ' b := HI(ss);',
  3577. ' b := lo(w);',
  3578. ' b := HI(w);',
  3579. ' b2 := lo(b);',
  3580. ' b2 := hi(b);',
  3581. ' lw := $1234CDEF;',
  3582. ' w := lo(lw);',
  3583. ' w := hi(lw);',
  3584. ' ni := $123456789ABCD;',
  3585. ' lw := lo(ni);',
  3586. ' lw := hi(ni);',
  3587. '']);
  3588. ConvertProgram;
  3589. CheckSource('TestLoHiFpcMode',
  3590. LinesToStr([ // statements
  3591. 'this.LoByte1 = 0x1234 & 0xFF;',
  3592. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3593. 'this.LoByte2 = 0x1234 & 0xFF;',
  3594. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3595. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3596. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3597. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3598. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3599. 'this.lo4 = 0x34 & 0xF;',
  3600. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3601. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3602. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3603. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3604. 'this.hi6 = 74565 >>> 0;',
  3605. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3606. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3607. 'this.b = 0;',
  3608. 'this.ss = 0;',
  3609. 'this.w = 0;',
  3610. 'this.si = 0;',
  3611. 'this.lw = 0;',
  3612. 'this.li = 0;',
  3613. 'this.b2 = 0;',
  3614. 'this.ni = 0;',
  3615. '']),
  3616. LinesToStr([ // this.$main
  3617. '$mod.w = 0x1234;',
  3618. '$mod.ss = -0x12;',
  3619. '$mod.b = $mod.ss & 0xFF;',
  3620. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3621. '$mod.b = $mod.w & 0xFF;',
  3622. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3623. '$mod.b2 = $mod.b & 0xF;',
  3624. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3625. '$mod.lw = 0x1234CDEF;',
  3626. '$mod.w = $mod.lw & 0xFFFF;',
  3627. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3628. '$mod.ni = 0x123456789ABCD;',
  3629. '$mod.lw = $mod.ni >>> 0;',
  3630. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3631. '']));
  3632. end;
  3633. procedure TTestModule.TestLoHiDelphiMode;
  3634. begin
  3635. StartProgram(false);
  3636. Add([
  3637. '{$mode delphi}',
  3638. 'const',
  3639. ' LoByte1 = Lo(Word($1234));',
  3640. ' HiByte1 = Hi(Word($1234));',
  3641. ' LoByte2 = Lo(SmallInt($1234));',
  3642. ' HiByte2 = Hi(SmallInt($1234));',
  3643. ' LoByte3 = Lo($1234CDEF);',
  3644. ' HiByte3 = Hi($1234CDEF);',
  3645. ' LoByte4 = Lo(-$1234CDEF);',
  3646. ' HiByte4 = Hi(-$1234CDEF);',
  3647. 'var',
  3648. ' b: Byte;',
  3649. ' w: Word;',
  3650. ' si: SmallInt;',
  3651. ' lw: LongWord;',
  3652. ' li: LongInt;',
  3653. 'begin',
  3654. ' w := $1234;',
  3655. ' b := lo(w);',
  3656. ' b := HI(w);',
  3657. ' lw := $1234CDEF;',
  3658. ' b := lo(lw);',
  3659. ' b := hi(lw);',
  3660. '']);
  3661. ConvertProgram;
  3662. CheckSource('TestLoHiDelphiMode',
  3663. LinesToStr([ // statements
  3664. 'this.LoByte1 = 0x1234 & 0xFF;',
  3665. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3666. 'this.LoByte2 = 0x1234 & 0xFF;',
  3667. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3668. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3669. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3670. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3671. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3672. 'this.b = 0;',
  3673. 'this.w = 0;',
  3674. 'this.si = 0;',
  3675. 'this.lw = 0;',
  3676. 'this.li = 0;'
  3677. ]),
  3678. LinesToStr([ // this.$main
  3679. '$mod.w = 0x1234;',
  3680. '$mod.b = $mod.w & 0xFF;',
  3681. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3682. '$mod.lw = 0x1234CDEF;',
  3683. '$mod.b = $mod.lw & 0xFF;',
  3684. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3685. ]));
  3686. end;
  3687. procedure TTestModule.TestAssignments;
  3688. begin
  3689. StartProgram(false);
  3690. Parser.Options:=Parser.Options+[po_cassignments];
  3691. Add('var');
  3692. Add(' Bar:longint;');
  3693. Add('begin');
  3694. Add(' bar:=3;');
  3695. Add(' bar+=4;');
  3696. Add(' bar-=5;');
  3697. Add(' bar*=6;');
  3698. ConvertProgram;
  3699. CheckSource('TestAssignments',
  3700. LinesToStr([ // statements
  3701. 'this.Bar = 0;'
  3702. ]),
  3703. LinesToStr([ // this.$main
  3704. '$mod.Bar=3;',
  3705. '$mod.Bar+=4;',
  3706. '$mod.Bar-=5;',
  3707. '$mod.Bar*=6;'
  3708. ]));
  3709. end;
  3710. procedure TTestModule.TestArithmeticOperators1;
  3711. begin
  3712. StartProgram(false);
  3713. Add('var');
  3714. Add(' vA,vB,vC:longint;');
  3715. Add('begin');
  3716. Add(' va:=1;');
  3717. Add(' vb:=va+va;');
  3718. Add(' vb:=va div vb;');
  3719. Add(' vb:=va mod vb;');
  3720. Add(' vb:=va+va*vb+va div vb;');
  3721. Add(' vc:=-va;');
  3722. Add(' va:=va-vb;');
  3723. Add(' vb:=va;');
  3724. Add(' if va<vb then vc:=va else vc:=vb;');
  3725. ConvertProgram;
  3726. CheckSource('TestArithmeticOperators1',
  3727. LinesToStr([ // statements
  3728. 'this.vA = 0;',
  3729. 'this.vB = 0;',
  3730. 'this.vC = 0;'
  3731. ]),
  3732. LinesToStr([ // this.$main
  3733. '$mod.vA = 1;',
  3734. '$mod.vB = $mod.vA + $mod.vA;',
  3735. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3736. '$mod.vB = $mod.vA % $mod.vB;',
  3737. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3738. '$mod.vC = -$mod.vA;',
  3739. '$mod.vA = $mod.vA - $mod.vB;',
  3740. '$mod.vB = $mod.vA;',
  3741. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3742. ]));
  3743. end;
  3744. procedure TTestModule.TestMultiAdd;
  3745. begin
  3746. StartProgram(false);
  3747. Add([
  3748. 'function Fly: string; external name ''fly'';',
  3749. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3750. 'var',
  3751. ' Date: double;',
  3752. 'begin',
  3753. ' Result:=(Year>0) and (Year<10000) and',
  3754. ' (Month >= 1) and (Month<=12) and',
  3755. ' (Day>0) and (Day<=31);',
  3756. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3757. 'end;',
  3758. 'var s: string;',
  3759. 'begin',
  3760. ' s:=''a''+''b''+''c''+''d'';',
  3761. ' s:=s+Fly+''e'';',
  3762. ' s:=Fly+Fly+Fly;',
  3763. '']);
  3764. ConvertProgram;
  3765. CheckSource('TestMultiAdd',
  3766. LinesToStr([ // statements
  3767. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3768. ' var Result = false;',
  3769. ' var date = 0.0;',
  3770. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3771. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3772. ' return Result;',
  3773. '};',
  3774. 'this.s = "";',
  3775. '']),
  3776. LinesToStr([ // this.$main
  3777. '$mod.s = "a" + "b" + "c" + "d";',
  3778. '$mod.s = $mod.s + fly() + "e";',
  3779. '$mod.s = fly() + fly() + fly();',
  3780. '']));
  3781. end;
  3782. procedure TTestModule.TestLogicalOperators;
  3783. begin
  3784. StartProgram(false);
  3785. Add('var');
  3786. Add(' vA,vB,vC:boolean;');
  3787. Add('begin');
  3788. Add(' va:=vb and vc;');
  3789. Add(' va:=vb or vc;');
  3790. Add(' va:=vb xor vc;');
  3791. Add(' va:=true and vc;');
  3792. Add(' va:=(vb and vc) or (va and vb);');
  3793. Add(' va:=not vb;');
  3794. ConvertProgram;
  3795. CheckSource('TestLogicalOperators',
  3796. LinesToStr([ // statements
  3797. 'this.vA = false;',
  3798. 'this.vB = false;',
  3799. 'this.vC = false;'
  3800. ]),
  3801. LinesToStr([ // this.$main
  3802. '$mod.vA = $mod.vB && $mod.vC;',
  3803. '$mod.vA = $mod.vB || $mod.vC;',
  3804. '$mod.vA = $mod.vB ^ $mod.vC;',
  3805. '$mod.vA = true && $mod.vC;',
  3806. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3807. '$mod.vA = !$mod.vB;'
  3808. ]));
  3809. end;
  3810. procedure TTestModule.TestBitwiseOperators;
  3811. begin
  3812. StartProgram(false);
  3813. Add([
  3814. 'var',
  3815. ' vA,vB,vC:longint;',
  3816. ' X,Y,Z: nativeint;',
  3817. 'begin',
  3818. ' va:=vb and vc;',
  3819. ' va:=vb or vc;',
  3820. ' va:=vb xor vc;',
  3821. ' va:=vb shl vc;',
  3822. ' va:=vb shr vc;',
  3823. ' va:=3 and vc;',
  3824. ' va:=(vb and vc) or (va and vb);',
  3825. ' va:=not vb;',
  3826. ' X:=Y and Z;',
  3827. ' X:=Y and va;',
  3828. ' X:=Y or Z;',
  3829. ' X:=Y or va;',
  3830. ' X:=Y xor Z;',
  3831. ' X:=Y xor va;',
  3832. '']);
  3833. ConvertProgram;
  3834. CheckSource('TestBitwiseOperators',
  3835. LinesToStr([ // statements
  3836. 'this.vA = 0;',
  3837. 'this.vB = 0;',
  3838. 'this.vC = 0;',
  3839. 'this.X = 0;',
  3840. 'this.Y = 0;',
  3841. 'this.Z = 0;',
  3842. '']),
  3843. LinesToStr([ // this.$main
  3844. '$mod.vA = $mod.vB & $mod.vC;',
  3845. '$mod.vA = $mod.vB | $mod.vC;',
  3846. '$mod.vA = $mod.vB ^ $mod.vC;',
  3847. '$mod.vA = $mod.vB << $mod.vC;',
  3848. '$mod.vA = $mod.vB >>> $mod.vC;',
  3849. '$mod.vA = 3 & $mod.vC;',
  3850. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3851. '$mod.vA = ~$mod.vB;',
  3852. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3853. '$mod.X = $mod.Y & $mod.vA;',
  3854. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3855. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3856. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3857. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3858. '']));
  3859. end;
  3860. procedure TTestModule.TestBitwiseOperatorsLongword;
  3861. begin
  3862. StartProgram(false);
  3863. Add([
  3864. 'var',
  3865. ' a,b,c:longword;',
  3866. ' i: longint;',
  3867. 'begin',
  3868. ' a:=$12345678;',
  3869. ' b:=$EDCBA987;',
  3870. ' c:=not a;',
  3871. ' c:=a and b;',
  3872. ' c:=a and $ffff0000;',
  3873. ' c:=a or b;',
  3874. ' c:=a or $ff00ff00;',
  3875. ' c:=a xor b;',
  3876. ' c:=a xor $f0f0f0f0;',
  3877. ' c:=a shl 1;',
  3878. ' c:=a shl 16;',
  3879. ' c:=a shl 24;',
  3880. ' c:=a shl b;',
  3881. ' c:=a shr 1;',
  3882. ' c:=a shr 16;',
  3883. ' c:=a shr 24;',
  3884. ' c:=a shr b;',
  3885. ' c:=(b and c) or (a and b);',
  3886. ' c:=i and a;',
  3887. ' c:=i or a;',
  3888. ' c:=i xor a;',
  3889. '']);
  3890. ConvertProgram;
  3891. CheckSource('TestBitwiseOperatorsLongword',
  3892. LinesToStr([ // statements
  3893. 'this.a = 0;',
  3894. 'this.b = 0;',
  3895. 'this.c = 0;',
  3896. 'this.i = 0;',
  3897. '']),
  3898. LinesToStr([ // this.$main
  3899. '$mod.a = 0x12345678;',
  3900. '$mod.b = 0xEDCBA987;',
  3901. '$mod.c = rtl.lw(~$mod.a);',
  3902. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3903. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3904. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3905. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3906. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3907. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3908. '$mod.c = rtl.lw($mod.a << 1);',
  3909. '$mod.c = rtl.lw($mod.a << 16);',
  3910. '$mod.c = rtl.lw($mod.a << 24);',
  3911. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3912. '$mod.c = rtl.lw($mod.a >>> 1);',
  3913. '$mod.c = rtl.lw($mod.a >>> 16);',
  3914. '$mod.c = rtl.lw($mod.a >>> 24);',
  3915. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3916. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3917. '$mod.c = $mod.i & $mod.a;',
  3918. '$mod.c = $mod.i | $mod.a;',
  3919. '$mod.c = $mod.i ^ $mod.a;',
  3920. '']));
  3921. end;
  3922. procedure TTestModule.TestPrgProcVar;
  3923. begin
  3924. StartProgram(false);
  3925. Add('procedure Proc1;');
  3926. Add('type');
  3927. Add(' t1=longint;');
  3928. Add('var');
  3929. Add(' vA:t1;');
  3930. Add('begin');
  3931. Add('end;');
  3932. Add('begin');
  3933. ConvertProgram;
  3934. CheckSource('TestPrgProcVar',
  3935. LinesToStr([ // statements
  3936. 'this.Proc1 = function () {',
  3937. ' var vA=0;',
  3938. '};'
  3939. ]),
  3940. LinesToStr([ // this.$main
  3941. ''
  3942. ]));
  3943. end;
  3944. procedure TTestModule.TestUnitProcVar;
  3945. begin
  3946. StartUnit(false);
  3947. Add('interface');
  3948. Add('');
  3949. Add('type tA=string; // unit scope');
  3950. Add('procedure Proc1;');
  3951. Add('');
  3952. Add('implementation');
  3953. Add('');
  3954. Add('procedure Proc1;');
  3955. Add('type tA=longint; // local proc scope');
  3956. Add('var v1:tA; // using local tA');
  3957. Add('begin');
  3958. Add('end;');
  3959. Add('var v2:tA; // using interface tA');
  3960. ConvertUnit;
  3961. CheckSource('TestUnitProcVar',
  3962. LinesToStr([ // statements
  3963. 'var $impl = $mod.$impl;',
  3964. 'this.Proc1 = function () {',
  3965. ' var v1 = 0;',
  3966. '};',
  3967. '']),
  3968. // this.$init
  3969. '',
  3970. // implementation
  3971. LinesToStr([
  3972. '$impl.v2 = "";',
  3973. '']));
  3974. end;
  3975. procedure TTestModule.TestImplProc;
  3976. begin
  3977. StartUnit(false);
  3978. Add('interface');
  3979. Add('');
  3980. Add('procedure Proc1;');
  3981. Add('');
  3982. Add('implementation');
  3983. Add('');
  3984. Add('procedure Proc1; begin end;');
  3985. Add('procedure Proc2; begin end;');
  3986. Add('initialization');
  3987. Add(' Proc1;');
  3988. Add(' Proc2;');
  3989. ConvertUnit;
  3990. CheckSource('TestImplProc',
  3991. LinesToStr([ // statements
  3992. 'var $impl = $mod.$impl;',
  3993. 'this.Proc1 = function () {',
  3994. '};',
  3995. '']),
  3996. LinesToStr([ // this.$init
  3997. '$mod.Proc1();',
  3998. '$impl.Proc2();',
  3999. '']),
  4000. LinesToStr([ // implementation
  4001. '$impl.Proc2 = function () {',
  4002. '};',
  4003. ''])
  4004. );
  4005. end;
  4006. procedure TTestModule.TestFunctionResult;
  4007. begin
  4008. StartProgram(false);
  4009. Add('function Func1: longint;');
  4010. Add('begin');
  4011. Add(' Result:=3;');
  4012. Add(' Func1:=4;');
  4013. Add('end;');
  4014. Add('begin');
  4015. ConvertProgram;
  4016. CheckSource('TestFunctionResult',
  4017. LinesToStr([ // statements
  4018. 'this.Func1 = function () {',
  4019. ' var Result = 0;',
  4020. ' Result = 3;',
  4021. ' Result = 4;',
  4022. ' return Result;',
  4023. '};'
  4024. ]),
  4025. '');
  4026. end;
  4027. procedure TTestModule.TestNestedProc;
  4028. begin
  4029. StartProgram(false);
  4030. Add([
  4031. 'var vInUnit: longint;',
  4032. 'function DoIt(pA,pD: longint): longint;',
  4033. 'var',
  4034. ' vB: longint;',
  4035. ' vC: longint;',
  4036. ' function Nesty(pA: longint): longint; ',
  4037. ' var vB: longint;',
  4038. ' begin',
  4039. ' Result:=pa+vb+vc+pd+vInUnit;',
  4040. ' nesty:=3;',
  4041. ' doit:=4;',
  4042. ' exit;',
  4043. ' end;',
  4044. 'begin',
  4045. ' Result:=pa+vb+vc;',
  4046. ' doit:=6;',
  4047. ' exit;',
  4048. 'end;',
  4049. 'begin']);
  4050. ConvertProgram;
  4051. CheckSource('TestNestedProc',
  4052. LinesToStr([ // statements
  4053. 'this.vInUnit = 0;',
  4054. 'this.DoIt = function (pA, pD) {',
  4055. ' var Result = 0;',
  4056. ' var vB = 0;',
  4057. ' var vC = 0;',
  4058. ' function Nesty(pA) {',
  4059. ' var Result$1 = 0;',
  4060. ' var vB = 0;',
  4061. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4062. ' Result$1 = 3;',
  4063. ' Result = 4;',
  4064. ' return Result$1;',
  4065. ' return Result$1;',
  4066. ' };',
  4067. ' Result = pA + vB + vC;',
  4068. ' Result = 6;',
  4069. ' return Result;',
  4070. ' return Result;',
  4071. '};'
  4072. ]),
  4073. '');
  4074. end;
  4075. procedure TTestModule.TestNestedProc_ResultString;
  4076. begin
  4077. StartProgram(false);
  4078. Add([
  4079. 'function DoIt: string;',
  4080. ' function Nesty: string; ',
  4081. ' begin',
  4082. ' nesty:=#65#66;',
  4083. ' nesty[1]:=#67;',
  4084. ' doit:=#68;',
  4085. ' doit[2]:=#69;',
  4086. ' end;',
  4087. 'begin',
  4088. ' doit:=#70;',
  4089. ' doit[3]:=#71;',
  4090. 'end;',
  4091. 'begin']);
  4092. ConvertProgram;
  4093. CheckSource('TestNestedProc_ResultString',
  4094. LinesToStr([ // statements
  4095. 'this.DoIt = function () {',
  4096. ' var Result = "";',
  4097. ' function Nesty() {',
  4098. ' var Result$1 = "";',
  4099. ' Result$1 = "AB";',
  4100. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4101. ' Result = "D";',
  4102. ' Result = rtl.setCharAt(Result, 1, "E");',
  4103. ' return Result$1;',
  4104. ' };',
  4105. ' Result = "F";',
  4106. ' Result = rtl.setCharAt(Result, 2, "G");',
  4107. ' return Result;',
  4108. '};'
  4109. ]),
  4110. '');
  4111. end;
  4112. procedure TTestModule.TestForwardProc;
  4113. begin
  4114. StartProgram(false);
  4115. Add('procedure FuncA(Bar: longint); forward;');
  4116. Add('procedure FuncB(Bar: longint);');
  4117. Add('begin');
  4118. Add(' funca(bar);');
  4119. Add('end;');
  4120. Add('procedure funca(bar: longint);');
  4121. Add('begin');
  4122. Add(' if bar=3 then ;');
  4123. Add('end;');
  4124. Add('begin');
  4125. Add(' funca(4);');
  4126. Add(' funcb(5);');
  4127. ConvertProgram;
  4128. CheckSource('TestForwardProc',
  4129. LinesToStr([ // statements'
  4130. 'this.FuncB = function (Bar) {',
  4131. ' $mod.FuncA(Bar);',
  4132. '};',
  4133. 'this.FuncA = function (Bar) {',
  4134. ' if (Bar === 3);',
  4135. '};'
  4136. ]),
  4137. LinesToStr([
  4138. '$mod.FuncA(4);',
  4139. '$mod.FuncB(5);'
  4140. ])
  4141. );
  4142. end;
  4143. procedure TTestModule.TestNestedForwardProc;
  4144. begin
  4145. StartProgram(false);
  4146. Add('procedure FuncA;');
  4147. Add(' procedure FuncB(i: longint); forward;');
  4148. Add(' procedure FuncC(i: longint);');
  4149. Add(' begin');
  4150. Add(' funcb(i);');
  4151. Add(' end;');
  4152. Add(' procedure FuncB(i: longint);');
  4153. Add(' begin');
  4154. Add(' if i=3 then ;');
  4155. Add(' end;');
  4156. Add('begin');
  4157. Add(' funcc(4)');
  4158. Add('end;');
  4159. Add('begin');
  4160. Add(' funca;');
  4161. ConvertProgram;
  4162. CheckSource('TestNestedForwardProc',
  4163. LinesToStr([ // statements'
  4164. 'this.FuncA = function () {',
  4165. ' function FuncC(i) {',
  4166. ' FuncB(i);',
  4167. ' };',
  4168. ' function FuncB(i) {',
  4169. ' if (i === 3);',
  4170. ' };',
  4171. ' FuncC(4);',
  4172. '};'
  4173. ]),
  4174. LinesToStr([
  4175. '$mod.FuncA();'
  4176. ])
  4177. );
  4178. end;
  4179. procedure TTestModule.TestAssignFunctionResult;
  4180. begin
  4181. StartProgram(false);
  4182. Add('function Func1: longint;');
  4183. Add('begin');
  4184. Add('end;');
  4185. Add('var i: longint;');
  4186. Add('begin');
  4187. Add(' i:=func1();');
  4188. Add(' i:=func1()+func1();');
  4189. ConvertProgram;
  4190. CheckSource('TestAssignFunctionResult',
  4191. LinesToStr([ // statements
  4192. 'this.Func1 = function () {',
  4193. ' var Result = 0;',
  4194. ' return Result;',
  4195. '};',
  4196. 'this.i = 0;'
  4197. ]),
  4198. LinesToStr([
  4199. '$mod.i = $mod.Func1();',
  4200. '$mod.i = $mod.Func1() + $mod.Func1();'
  4201. ]));
  4202. end;
  4203. procedure TTestModule.TestFunctionResultInCondition;
  4204. begin
  4205. StartProgram(false);
  4206. Add('function Func1: longint;');
  4207. Add('begin');
  4208. Add('end;');
  4209. Add('function Func2: boolean;');
  4210. Add('begin');
  4211. Add('end;');
  4212. Add('var i: longint;');
  4213. Add('begin');
  4214. Add(' if func2 then ;');
  4215. Add(' if i=func1() then ;');
  4216. Add(' if i=func1 then ;');
  4217. ConvertProgram;
  4218. CheckSource('TestFunctionResultInCondition',
  4219. LinesToStr([ // statements
  4220. 'this.Func1 = function () {',
  4221. ' var Result = 0;',
  4222. ' return Result;',
  4223. '};',
  4224. 'this.Func2 = function () {',
  4225. ' var Result = false;',
  4226. ' return Result;',
  4227. '};',
  4228. 'this.i = 0;'
  4229. ]),
  4230. LinesToStr([
  4231. 'if ($mod.Func2());',
  4232. 'if ($mod.i === $mod.Func1());',
  4233. 'if ($mod.i === $mod.Func1());'
  4234. ]));
  4235. end;
  4236. procedure TTestModule.TestFunctionResultInForLoop;
  4237. begin
  4238. StartProgram(false);
  4239. Add([
  4240. 'function Func1(a: array of longint): longint;',
  4241. 'begin',
  4242. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4243. ' for Result in a do if a[Result]=0 then exit;',
  4244. 'end;',
  4245. 'begin',
  4246. ' Func1([1,2,3])']);
  4247. ConvertProgram;
  4248. CheckSource('TestFunctionResultInForLoop',
  4249. LinesToStr([ // statements
  4250. 'this.Func1 = function (a) {',
  4251. ' var Result = 0;',
  4252. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4253. ' Result = $l;',
  4254. ' if (a[Result] === 0) return Result;',
  4255. ' };',
  4256. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4257. ' Result = $in[$l1];',
  4258. ' if (a[Result] === 0) return Result;',
  4259. ' };',
  4260. ' return Result;',
  4261. '};',
  4262. '']),
  4263. LinesToStr([
  4264. '$mod.Func1([1, 2, 3]);'
  4265. ]));
  4266. end;
  4267. procedure TTestModule.TestFunctionResultInTypeCast;
  4268. begin
  4269. StartProgram(false);
  4270. Add([
  4271. 'function GetInt: longint;',
  4272. 'begin',
  4273. 'end;',
  4274. 'begin',
  4275. ' if Byte(GetInt)=0 then ;',
  4276. '']);
  4277. ConvertProgram;
  4278. CheckSource('TestFunctionResultInTypeCast',
  4279. LinesToStr([ // statements
  4280. 'this.GetInt = function () {',
  4281. ' var Result = 0;',
  4282. ' return Result;',
  4283. '};',
  4284. '']),
  4285. LinesToStr([
  4286. 'if (($mod.GetInt() & 255) === 0) ;'
  4287. ]));
  4288. end;
  4289. procedure TTestModule.TestExit;
  4290. begin
  4291. StartProgram(false);
  4292. Add('procedure ProcA;');
  4293. Add('begin');
  4294. Add(' exit;');
  4295. Add('end;');
  4296. Add('function FuncB: longint;');
  4297. Add('begin');
  4298. Add(' exit;');
  4299. Add(' exit(3);');
  4300. Add('end;');
  4301. Add('function FuncC: string;');
  4302. Add('begin');
  4303. Add(' exit;');
  4304. Add(' exit(''a'');');
  4305. Add(' exit(''abc'');');
  4306. Add('end;');
  4307. Add('begin');
  4308. Add(' exit;');
  4309. Add(' exit(1);');
  4310. ConvertProgram;
  4311. CheckSource('TestExit',
  4312. LinesToStr([ // statements
  4313. 'this.ProcA = function () {',
  4314. ' return;',
  4315. '};',
  4316. 'this.FuncB = function () {',
  4317. ' var Result = 0;',
  4318. ' return Result;',
  4319. ' return 3;',
  4320. ' return Result;',
  4321. '};',
  4322. 'this.FuncC = function () {',
  4323. ' var Result = "";',
  4324. ' return Result;',
  4325. ' return "a";',
  4326. ' return "abc";',
  4327. ' return Result;',
  4328. '};'
  4329. ]),
  4330. LinesToStr([
  4331. 'return;',
  4332. 'return 1;',
  4333. '']));
  4334. end;
  4335. procedure TTestModule.TestExit_ResultInFinally;
  4336. begin
  4337. StartProgram(false);
  4338. Add([
  4339. 'function Run: word;',
  4340. 'begin',
  4341. ' try',
  4342. ' exit(3);', // no Result in finally -> use return 3
  4343. ' finally',
  4344. ' end;',
  4345. 'end;',
  4346. 'function Fly: word;',
  4347. 'begin',
  4348. ' try',
  4349. ' exit(3);',
  4350. ' finally',
  4351. ' if Result>0 then ;',
  4352. ' end;',
  4353. 'end;',
  4354. 'function Jump: word;',
  4355. 'begin',
  4356. ' try',
  4357. ' try',
  4358. ' exit(4);',
  4359. ' finally',
  4360. ' end;',
  4361. ' finally',
  4362. ' if Result>0 then ;',
  4363. ' end;',
  4364. 'end;',
  4365. 'begin',
  4366. '']);
  4367. ConvertProgram;
  4368. CheckSource('TestExit_ResultInFinally',
  4369. LinesToStr([ // statements
  4370. 'this.Run = function () {',
  4371. ' var Result = 0;',
  4372. ' try {',
  4373. ' return 3;',
  4374. ' } finally {',
  4375. ' };',
  4376. ' return Result;',
  4377. '};',
  4378. 'this.Fly = function () {',
  4379. ' var Result = 0;',
  4380. ' try {',
  4381. ' Result = 3;',
  4382. ' return Result;',
  4383. ' } finally {',
  4384. ' if (Result > 0) ;',
  4385. ' };',
  4386. ' return Result;',
  4387. '};',
  4388. 'this.Jump = function () {',
  4389. ' var Result = 0;',
  4390. ' try {',
  4391. ' try {',
  4392. ' Result = 4;',
  4393. ' return Result;',
  4394. ' } finally {',
  4395. ' };',
  4396. ' } finally {',
  4397. ' if (Result > 0) ;',
  4398. ' };',
  4399. ' return Result;',
  4400. '};',
  4401. '']),
  4402. LinesToStr([
  4403. '']));
  4404. end;
  4405. procedure TTestModule.TestBreak;
  4406. begin
  4407. StartProgram(false);
  4408. Add([
  4409. 'var',
  4410. ' i: longint;',
  4411. 'begin',
  4412. ' repeat',
  4413. ' break;',
  4414. ' until true;',
  4415. ' while true do',
  4416. ' break;',
  4417. ' for i:=1 to 2 do',
  4418. ' break;']);
  4419. ConvertProgram;
  4420. CheckSource('TestBreak',
  4421. LinesToStr([ // statements
  4422. 'this.i = 0;'
  4423. ]),
  4424. LinesToStr([
  4425. 'do {',
  4426. ' break;',
  4427. '} while (!true);',
  4428. 'while (true) break;',
  4429. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4430. '']));
  4431. end;
  4432. procedure TTestModule.TestBreakAsVar;
  4433. begin
  4434. StartProgram(false);
  4435. Add([
  4436. 'procedure DoIt(break: boolean);',
  4437. 'begin',
  4438. ' if break then ;',
  4439. 'end;',
  4440. 'var',
  4441. ' break: boolean;',
  4442. 'begin',
  4443. ' if break then ;']);
  4444. ConvertProgram;
  4445. CheckSource('TestBreakAsVar',
  4446. LinesToStr([ // statements
  4447. 'this.DoIt = function (Break) {',
  4448. ' if (Break) ;',
  4449. '};',
  4450. 'this.Break = false;',
  4451. '']),
  4452. LinesToStr([
  4453. 'if($mod.Break) ;',
  4454. '']));
  4455. end;
  4456. procedure TTestModule.TestContinue;
  4457. begin
  4458. StartProgram(false);
  4459. Add('var i: longint;');
  4460. Add('begin');
  4461. Add(' repeat');
  4462. Add(' continue;');
  4463. Add(' until true;');
  4464. Add(' while true do');
  4465. Add(' continue;');
  4466. Add(' for i:=1 to 2 do');
  4467. Add(' continue;');
  4468. ConvertProgram;
  4469. CheckSource('TestContinue',
  4470. LinesToStr([ // statements
  4471. 'this.i = 0;'
  4472. ]),
  4473. LinesToStr([
  4474. 'do {',
  4475. ' continue;',
  4476. '} while (!true);',
  4477. 'while (true) continue;',
  4478. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4479. '']));
  4480. end;
  4481. procedure TTestModule.TestProc_External;
  4482. begin
  4483. StartProgram(false);
  4484. Add('procedure Foo; external name ''console.log'';');
  4485. Add('function Bar: longint; external name ''get.item'';');
  4486. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4487. Add('var');
  4488. Add(' i: longint;');
  4489. Add('begin');
  4490. Add(' Foo;');
  4491. Add(' i:=Bar;');
  4492. Add(' i:=Bla(''abc'');');
  4493. ConvertProgram;
  4494. CheckSource('TestProc_External',
  4495. LinesToStr([ // statements
  4496. 'this.i = 0;'
  4497. ]),
  4498. LinesToStr([
  4499. 'console.log();',
  4500. '$mod.i = get.item();',
  4501. '$mod.i = apply.something("abc");'
  4502. ]));
  4503. end;
  4504. procedure TTestModule.TestProc_ExternalOtherUnit;
  4505. begin
  4506. AddModuleWithIntfImplSrc('unit2.pas',
  4507. LinesToStr([
  4508. 'procedure Now; external name ''Date.now'';',
  4509. 'procedure DoIt;'
  4510. ]),
  4511. 'procedure doit; begin end;');
  4512. StartUnit(true);
  4513. Add('interface');
  4514. Add('uses unit2;');
  4515. Add('implementation');
  4516. Add('begin');
  4517. Add(' now;');
  4518. Add(' now();');
  4519. Add(' uNit2.now;');
  4520. Add(' uNit2.now();');
  4521. Add(' doit;');
  4522. Add(' uNit2.doit;');
  4523. ConvertUnit;
  4524. CheckSource('TestProc_ExternalOtherUnit',
  4525. LinesToStr([
  4526. '']),
  4527. LinesToStr([
  4528. 'Date.now();',
  4529. 'Date.now();',
  4530. 'Date.now();',
  4531. 'Date.now();',
  4532. 'pas.unit2.DoIt();',
  4533. 'pas.unit2.DoIt();',
  4534. '']));
  4535. end;
  4536. procedure TTestModule.TestProc_Asm;
  4537. begin
  4538. StartProgram(false);
  4539. Add([
  4540. '{$mode delphi}',
  4541. 'function DoIt: longint;',
  4542. 'begin;',
  4543. ' asm',
  4544. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4545. ' end;',
  4546. ' asm console.log(); end;',
  4547. ' asm',
  4548. ' s = "'' ";',
  4549. ' s = ''" '';',
  4550. ' s = s + "world" + "''";',
  4551. ' // end',
  4552. ' s = ''end'';',
  4553. ' s = "end";',
  4554. ' s = "foo\"bar";',
  4555. ' s = ''a\''b'';',
  4556. ' s = `${expr}\`-"-''-`;',
  4557. ' s = `multi',
  4558. 'line`;',
  4559. ' end;',
  4560. 'end;',
  4561. 'procedure Fly;',
  4562. 'asm',
  4563. ' return;',
  4564. 'end;',
  4565. 'begin']);
  4566. ConvertProgram;
  4567. CheckSource('TestProc_Asm',
  4568. LinesToStr([ // statements
  4569. 'this.DoIt = function () {',
  4570. ' var Result = 0;',
  4571. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4572. ' console.log();',
  4573. ' s = "'' ";',
  4574. ' s = ''" '';',
  4575. ' s = s + "world" + "''";',
  4576. ' // end',
  4577. ' s = ''end'';',
  4578. ' s = "end";',
  4579. ' s = "foo\"bar";',
  4580. ' s = ''a\''b'';',
  4581. ' s = `${expr}\`-"-''-`;',
  4582. ' s = `multi',
  4583. 'line`;',
  4584. ' return Result;',
  4585. '};',
  4586. 'this.Fly = function () {',
  4587. ' return;',
  4588. '};',
  4589. '']),
  4590. LinesToStr([
  4591. ''
  4592. ]));
  4593. end;
  4594. procedure TTestModule.TestProc_AsmSubBlock;
  4595. begin
  4596. StartProgram(true,[supTObject]);
  4597. Add([
  4598. '{$mode delphi}',
  4599. 'type',
  4600. ' TBird = class end;',
  4601. 'procedure Run(w: word);',
  4602. 'begin;',
  4603. ' if true then asm console.log(); end;',
  4604. ' if w>3 then asm',
  4605. ' var a = w+1;',
  4606. ' w = a+3;',
  4607. ' end;',
  4608. ' while (w>7) do asm',
  4609. ' w+=3; w*=2;',
  4610. ' end;',
  4611. ' try',
  4612. ' except',
  4613. ' on E: TBird do',
  4614. ' asm console.log(E); end;',
  4615. ' on E: TObject do',
  4616. ' asm var i=3; i--; end;',
  4617. ' else asm Fly; High; end;',
  4618. ' end;',
  4619. 'end;',
  4620. 'begin']);
  4621. ConvertProgram;
  4622. CheckSource('TestProc_AsmSubBlock',
  4623. LinesToStr([ // statements
  4624. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4625. '});',
  4626. 'this.Run = function (w) {',
  4627. ' if (true) console.log();',
  4628. ' if (w > 3) {',
  4629. ' var a = w+1;',
  4630. ' w = a+3;',
  4631. ' };',
  4632. ' while (w > 7) {',
  4633. ' w+=3; w*=2;',
  4634. ' };',
  4635. ' try {} catch ($e) {',
  4636. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4637. ' var E = $e;',
  4638. ' console.log(E);',
  4639. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4640. ' var E = $e;',
  4641. ' var i=3; i--;',
  4642. ' } else {',
  4643. ' Fly; High;',
  4644. ' }',
  4645. ' };',
  4646. '};',
  4647. '']),
  4648. LinesToStr([
  4649. ''
  4650. ]));
  4651. end;
  4652. procedure TTestModule.TestProc_Assembler;
  4653. begin
  4654. StartProgram(false);
  4655. Add('function DoIt: longint; assembler;');
  4656. Add('asm');
  4657. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4658. Add('end;');
  4659. Add('begin');
  4660. ConvertProgram;
  4661. CheckSource('TestProc_Assembler',
  4662. LinesToStr([ // statements
  4663. 'this.DoIt = function () {',
  4664. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4665. '};'
  4666. ]),
  4667. LinesToStr([
  4668. ''
  4669. ]));
  4670. end;
  4671. procedure TTestModule.TestProc_VarParam;
  4672. begin
  4673. StartProgram(false);
  4674. Add('type integer = longint;');
  4675. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4676. Add('var vJ: integer;');
  4677. Add('begin');
  4678. Add(' vg:=vg+1;');
  4679. Add(' vj:=vh+2;');
  4680. Add(' vi:=vi+3;');
  4681. Add(' doit(vg,vg,vg);');
  4682. Add(' doit(vh,vh,vj);');
  4683. Add(' doit(vi,vi,vi);');
  4684. Add(' doit(vj,vj,vj);');
  4685. Add('end;');
  4686. Add('var i: integer;');
  4687. Add('begin');
  4688. Add(' doit(i,i,i);');
  4689. ConvertProgram;
  4690. CheckSource('TestProc_VarParam',
  4691. LinesToStr([ // statements
  4692. 'this.DoIt = function (vG,vH,vI) {',
  4693. ' var vJ = 0;',
  4694. ' vG = vG + 1;',
  4695. ' vJ = vH + 2;',
  4696. ' vI.set(vI.get()+3);',
  4697. ' $mod.DoIt(vG, vG, {',
  4698. ' get: function () {',
  4699. ' return vG;',
  4700. ' },',
  4701. ' set: function (v) {',
  4702. ' vG = v;',
  4703. ' }',
  4704. ' });',
  4705. ' $mod.DoIt(vH, vH, {',
  4706. ' get: function () {',
  4707. ' return vJ;',
  4708. ' },',
  4709. ' set: function (v) {',
  4710. ' vJ = v;',
  4711. ' }',
  4712. ' });',
  4713. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4714. ' $mod.DoIt(vJ, vJ, {',
  4715. ' get: function () {',
  4716. ' return vJ;',
  4717. ' },',
  4718. ' set: function (v) {',
  4719. ' vJ = v;',
  4720. ' }',
  4721. ' });',
  4722. '};',
  4723. 'this.i = 0;'
  4724. ]),
  4725. LinesToStr([
  4726. '$mod.DoIt($mod.i,$mod.i,{',
  4727. ' p: $mod,',
  4728. ' get: function () {',
  4729. ' return this.p.i;',
  4730. ' },',
  4731. ' set: function (v) {',
  4732. ' this.p.i = v;',
  4733. ' }',
  4734. '});'
  4735. ]));
  4736. end;
  4737. procedure TTestModule.TestProc_VarParamString;
  4738. begin
  4739. StartProgram(false);
  4740. Add(['type TCaption = string;',
  4741. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4742. 'var c: char;',
  4743. 'begin',
  4744. ' va[1]:=c;',
  4745. ' vb[2]:=c;',
  4746. ' vc[3]:=c;',
  4747. 'end;',
  4748. 'begin']);
  4749. ConvertProgram;
  4750. CheckSource('TestProc_VarParamString',
  4751. LinesToStr([ // statements
  4752. 'this.DoIt = function (vA,vB,vC) {',
  4753. ' var c = "\x00";',
  4754. ' vA = rtl.setCharAt(vA, 0, c);',
  4755. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4756. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4757. '};',
  4758. '']),
  4759. LinesToStr([
  4760. ]));
  4761. end;
  4762. procedure TTestModule.TestProc_VarParamV;
  4763. begin
  4764. StartProgram(false);
  4765. Add([
  4766. 'procedure Inc2(var i: longint);',
  4767. 'begin',
  4768. ' i:=i+2;',
  4769. 'end;',
  4770. 'procedure DoIt(v: longint);',
  4771. 'var p: array of longint;',
  4772. 'begin',
  4773. ' Inc2(v);',
  4774. ' Inc2(p[v]);',
  4775. 'end;',
  4776. 'begin']);
  4777. ConvertProgram;
  4778. CheckSource('TestProc_VarParamV',
  4779. LinesToStr([ // statements
  4780. 'this.Inc2 = function (i) {',
  4781. ' i.set(i.get()+2);',
  4782. '};',
  4783. 'this.DoIt = function (v) {',
  4784. ' var p = [];',
  4785. ' $mod.Inc2({get: function () {',
  4786. ' return v;',
  4787. ' }, set: function (w) {',
  4788. ' v = w;',
  4789. ' }});',
  4790. ' $mod.Inc2({',
  4791. ' a: v,',
  4792. ' p: p,',
  4793. ' get: function () {',
  4794. ' return this.p[this.a];',
  4795. ' },',
  4796. ' set: function (v) {',
  4797. ' this.p[this.a] = v;',
  4798. ' }',
  4799. ' });',
  4800. '};',
  4801. '']),
  4802. LinesToStr([
  4803. '']));
  4804. end;
  4805. procedure TTestModule.TestProc_Overload;
  4806. begin
  4807. StartProgram(false);
  4808. Add('procedure DoIt(vI: longint); begin end;');
  4809. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4810. Add('procedure DoIt(vD: double); begin end;');
  4811. Add('begin');
  4812. Add(' DoIt(1);');
  4813. Add(' DoIt(2,3);');
  4814. Add(' DoIt(4.5);');
  4815. ConvertProgram;
  4816. CheckSource('TestProcedureOverload',
  4817. LinesToStr([ // statements
  4818. 'this.DoIt = function (vI) {',
  4819. '};',
  4820. 'this.DoIt$1 = function (vI, vJ) {',
  4821. '};',
  4822. 'this.DoIt$2 = function (vD) {',
  4823. '};',
  4824. '']),
  4825. LinesToStr([
  4826. '$mod.DoIt(1);',
  4827. '$mod.DoIt$1(2, 3);',
  4828. '$mod.DoIt$2(4.5);',
  4829. '']));
  4830. end;
  4831. procedure TTestModule.TestProc_OverloadForward;
  4832. begin
  4833. StartProgram(false);
  4834. Add('procedure DoIt(vI: longint); forward;');
  4835. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4836. Add('procedure doit(vi: longint); begin end;');
  4837. Add('begin');
  4838. Add(' doit(1);');
  4839. Add(' doit(2,3);');
  4840. ConvertProgram;
  4841. CheckSource('TestProcedureOverloadForward',
  4842. LinesToStr([ // statements
  4843. 'this.DoIt$1 = function (vI, vJ) {',
  4844. '};',
  4845. 'this.DoIt = function (vI) {',
  4846. '};',
  4847. '']),
  4848. LinesToStr([
  4849. '$mod.DoIt(1);',
  4850. '$mod.DoIt$1(2, 3);',
  4851. '']));
  4852. end;
  4853. procedure TTestModule.TestProc_OverloadIntfImpl;
  4854. begin
  4855. StartUnit(false);
  4856. Add('interface');
  4857. Add('procedure DoIt(vI: longint);');
  4858. Add('procedure DoIt(vI, vJ: longint);');
  4859. Add('implementation');
  4860. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4861. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4862. Add('procedure DoIt(vi: longint); begin end;');
  4863. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4864. Add('procedure DoIt(vi, vj: longint); begin end;');
  4865. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4866. Add('begin');
  4867. Add(' doit(1);');
  4868. Add(' doit(2,3);');
  4869. Add(' doit(4,5,6);');
  4870. Add(' doit(7,8,9,10);');
  4871. Add(' doit(11,12,13,14,15);');
  4872. ConvertUnit;
  4873. CheckSource('TestProcedureOverloadUnit',
  4874. LinesToStr([ // statements
  4875. 'var $impl = $mod.$impl;',
  4876. 'this.DoIt = function (vI) {',
  4877. '};',
  4878. 'this.DoIt$1 = function (vI, vJ) {',
  4879. '};',
  4880. '']),
  4881. LinesToStr([ // this.$init
  4882. '$mod.DoIt(1);',
  4883. '$mod.DoIt$1(2, 3);',
  4884. '$impl.DoIt$3(4,5,6);',
  4885. '$impl.DoIt$4(7,8,9,10);',
  4886. '$impl.DoIt$2(11,12,13,14,15);',
  4887. '']),
  4888. LinesToStr([ // implementation
  4889. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4890. '};',
  4891. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4892. '};',
  4893. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4894. '};',
  4895. '']));
  4896. end;
  4897. procedure TTestModule.TestProc_OverloadNested;
  4898. begin
  4899. StartProgram(false);
  4900. Add([
  4901. 'procedure doit(vA: longint);',
  4902. ' procedure DoIt(vA, vB: longint); overload;',
  4903. ' begin',
  4904. ' doit(1);',
  4905. ' doit(1,2);',
  4906. ' end;',
  4907. ' procedure doit(vA, vB, vC: longint);',
  4908. ' begin',
  4909. ' doit(1);',
  4910. ' doit(1,2);',
  4911. ' doit(1,2,3);',
  4912. ' end;',
  4913. 'begin',
  4914. ' doit(1);',
  4915. ' doit(1,2);',
  4916. ' doit(1,2,3);',
  4917. 'end;',
  4918. 'begin // main',
  4919. ' doit(1);']);
  4920. ConvertProgram;
  4921. CheckSource('TestProcedureOverloadNested',
  4922. LinesToStr([ // statements
  4923. 'this.doit = function (vA) {',
  4924. ' function DoIt$1(vA, vB) {',
  4925. ' $mod.doit(1);',
  4926. ' DoIt$1(1, 2);',
  4927. ' };',
  4928. ' function doit$2(vA, vB, vC) {',
  4929. ' $mod.doit(1);',
  4930. ' DoIt$1(1, 2);',
  4931. ' doit$2(1, 2, 3);',
  4932. ' };',
  4933. ' $mod.doit(1);',
  4934. ' DoIt$1(1, 2);',
  4935. ' doit$2(1, 2, 3);',
  4936. '};',
  4937. '']),
  4938. LinesToStr([
  4939. '$mod.doit(1);',
  4940. '']));
  4941. end;
  4942. procedure TTestModule.TestProc_OverloadNestedForward;
  4943. begin
  4944. StartProgram(false);
  4945. Add([
  4946. 'procedure DoIt(vA: longint); overload; forward;',
  4947. 'procedure DoIt(vB, vC: longint); overload;',
  4948. 'begin // 2 param overload',
  4949. ' doit(1);',
  4950. ' doit(1,2);',
  4951. 'end;',
  4952. 'procedure doit(vA: longint);',
  4953. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4954. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4955. ' begin // 4 param overload',
  4956. ' doit(1);',
  4957. ' doit(1,2);',
  4958. ' doit(1,2,3);',
  4959. ' doit(1,2,3,4);',
  4960. ' end;',
  4961. ' procedure doit(vA, vB, vC: longint);',
  4962. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4963. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4964. ' begin // 6 param overload',
  4965. ' doit(1);',
  4966. ' doit(1,2);',
  4967. ' doit(1,2,3);',
  4968. ' doit(1,2,3,4);',
  4969. ' doit(1,2,3,4,5);',
  4970. ' doit(1,2,3,4,5,6);',
  4971. ' end;',
  4972. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4973. ' begin // 5 param overload',
  4974. ' doit(1);',
  4975. ' doit(1,2);',
  4976. ' doit(1,2,3);',
  4977. ' doit(1,2,3,4);',
  4978. ' doit(1,2,3,4,5);',
  4979. ' doit(1,2,3,4,5,6);',
  4980. ' end;',
  4981. ' begin // 3 param overload',
  4982. ' doit(1);',
  4983. ' doit(1,2);',
  4984. ' doit(1,2,3);',
  4985. ' doit(1,2,3,4);',
  4986. ' doit(1,2,3,4,5);',
  4987. ' doit(1,2,3,4,5,6);',
  4988. ' end;',
  4989. 'begin // 1 param overload',
  4990. ' doit(1);',
  4991. ' doit(1,2);',
  4992. ' doit(1,2,3);',
  4993. ' doit(1,2,3,4);',
  4994. 'end;',
  4995. 'begin // main',
  4996. ' doit(1);',
  4997. ' doit(1,2);']);
  4998. ConvertProgram;
  4999. CheckSource('TestProc_OverloadNestedForward',
  5000. LinesToStr([ // statements
  5001. 'this.DoIt$1 = function (vB, vC) {',
  5002. ' $mod.DoIt(1);',
  5003. ' $mod.DoIt$1(1, 2);',
  5004. '};',
  5005. 'this.DoIt = function (vA) {',
  5006. ' function DoIt$3(vA, vB, vC, vD) {',
  5007. ' $mod.DoIt(1);',
  5008. ' $mod.DoIt$1(1, 2);',
  5009. ' DoIt$2(1, 2, 3);',
  5010. ' DoIt$3(1, 2, 3, 4);',
  5011. ' };',
  5012. ' function DoIt$2(vA, vB, vC) {',
  5013. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5014. ' $mod.DoIt(1);',
  5015. ' $mod.DoIt$1(1, 2);',
  5016. ' DoIt$2(1, 2, 3);',
  5017. ' DoIt$3(1, 2, 3, 4);',
  5018. ' DoIt$4(1, 2, 3, 4, 5);',
  5019. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5020. ' };',
  5021. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5022. ' $mod.DoIt(1);',
  5023. ' $mod.DoIt$1(1, 2);',
  5024. ' DoIt$2(1, 2, 3);',
  5025. ' DoIt$3(1, 2, 3, 4);',
  5026. ' DoIt$4(1, 2, 3, 4, 5);',
  5027. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5028. ' };',
  5029. ' $mod.DoIt(1);',
  5030. ' $mod.DoIt$1(1, 2);',
  5031. ' DoIt$2(1, 2, 3);',
  5032. ' DoIt$3(1, 2, 3, 4);',
  5033. ' DoIt$4(1, 2, 3, 4, 5);',
  5034. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5035. ' };',
  5036. ' $mod.DoIt(1);',
  5037. ' $mod.DoIt$1(1, 2);',
  5038. ' DoIt$2(1, 2, 3);',
  5039. ' DoIt$3(1, 2, 3, 4);',
  5040. '};',
  5041. '']),
  5042. LinesToStr([
  5043. '$mod.DoIt(1);',
  5044. '$mod.DoIt$1(1, 2);',
  5045. '']));
  5046. end;
  5047. procedure TTestModule.TestProc_OverloadUnitCycle;
  5048. begin
  5049. AddModuleWithIntfImplSrc('Unit2.pas',
  5050. LinesToStr([
  5051. 'type',
  5052. ' TObject = class',
  5053. ' procedure DoIt(b: boolean); virtual; abstract;',
  5054. ' procedure DoIt(i: longint); virtual; abstract;',
  5055. ' end;',
  5056. '']),
  5057. 'uses test1;');
  5058. StartUnit(true);
  5059. Add([
  5060. 'interface',
  5061. 'uses unit2;',
  5062. 'type',
  5063. ' TEagle = class(TObject)',
  5064. ' procedure DoIt(b: boolean); override;',
  5065. ' procedure DoIt(i: longint); override;',
  5066. ' end;',
  5067. 'implementation',
  5068. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5069. 'procedure TEagle.DoIt(i: longint); begin end;',
  5070. '']);
  5071. ConvertUnit;
  5072. CheckSource('TestProc_OverloadUnitCycle',
  5073. LinesToStr([ // statements
  5074. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5075. ' this.DoIt = function (b) {',
  5076. ' };',
  5077. ' this.DoIt$1 = function (i) {',
  5078. ' };',
  5079. '});',
  5080. '']),
  5081. '',
  5082. LinesToStr([
  5083. '']));
  5084. end;
  5085. procedure TTestModule.TestProc_Varargs;
  5086. begin
  5087. StartProgram(false);
  5088. Add([
  5089. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5090. 'procedure ProcB; varargs; external name ''ProcB'';',
  5091. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5092. 'function GetIt: longint; begin end;',
  5093. 'begin',
  5094. ' ProcA(1);',
  5095. ' ProcA(1,2);',
  5096. ' ProcA(1,2.0);',
  5097. ' ProcA(1,2,3);',
  5098. ' ProcA(1,''2'');',
  5099. ' ProcA(2,'''');',
  5100. ' ProcA(3,false);',
  5101. ' ProcB;',
  5102. ' ProcB();',
  5103. ' ProcB(4);',
  5104. ' ProcB(''foo'');',
  5105. ' ProcC;',
  5106. ' ProcC();',
  5107. ' ProcC(4);',
  5108. ' ProcC(5,''foo'');',
  5109. ' ProcB(GetIt);',
  5110. ' ProcB(GetIt());',
  5111. ' ProcB(GetIt,GetIt());']);
  5112. ConvertProgram;
  5113. CheckSource('TestProc_Varargs',
  5114. LinesToStr([ // statements
  5115. 'this.GetIt = function () {',
  5116. ' var Result = 0;',
  5117. ' return Result;',
  5118. '};',
  5119. '']),
  5120. LinesToStr([
  5121. 'ProcA(1);',
  5122. 'ProcA(1, 2);',
  5123. 'ProcA(1, 2.0);',
  5124. 'ProcA(1, 2, 3);',
  5125. 'ProcA(1, "2");',
  5126. 'ProcA(2, "");',
  5127. 'ProcA(3, false);',
  5128. 'ProcB();',
  5129. 'ProcB();',
  5130. 'ProcB(4);',
  5131. 'ProcB("foo");',
  5132. 'ProcC(17);',
  5133. 'ProcC(17);',
  5134. 'ProcC(4);',
  5135. 'ProcC(5, "foo");',
  5136. 'ProcB($mod.GetIt());',
  5137. 'ProcB($mod.GetIt());',
  5138. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5139. '']));
  5140. end;
  5141. procedure TTestModule.TestProc_ConstOrder;
  5142. begin
  5143. StartProgram(false);
  5144. Add([
  5145. 'const A = 3;',
  5146. 'const B = A+1;',
  5147. 'procedure DoIt;',
  5148. 'const C = A+1;',
  5149. 'const D = B+1;',
  5150. 'const E = D+C+B+A;',
  5151. 'begin',
  5152. 'end;',
  5153. 'begin'
  5154. ]);
  5155. ConvertProgram;
  5156. CheckSource('TestProc_ConstOrder',
  5157. LinesToStr([ // statements
  5158. 'this.A = 3;',
  5159. 'this.B = 3 + 1;',
  5160. 'var C = 3 + 1;',
  5161. 'var D = 4 + 1;',
  5162. 'var E = 5 + 4 + 4 + 3;',
  5163. 'this.DoIt = function () {',
  5164. '};',
  5165. '']),
  5166. LinesToStr([
  5167. ''
  5168. ]));
  5169. end;
  5170. procedure TTestModule.TestProc_DuplicateConst;
  5171. begin
  5172. StartProgram(false);
  5173. Add([
  5174. 'const A = 1;',
  5175. 'procedure DoIt;',
  5176. 'const A = 2;',
  5177. ' procedure SubIt;',
  5178. ' const A = 21;',
  5179. ' begin',
  5180. ' end;',
  5181. 'begin',
  5182. 'end;',
  5183. 'procedure DoSome;',
  5184. 'const A = 3;',
  5185. 'begin',
  5186. 'end;',
  5187. 'begin'
  5188. ]);
  5189. ConvertProgram;
  5190. CheckSource('TestProc_DuplicateConst',
  5191. LinesToStr([ // statements
  5192. 'this.A = 1;',
  5193. 'var A$1 = 2;',
  5194. 'var A$2 = 21;',
  5195. 'this.DoIt = function () {',
  5196. ' function SubIt() {',
  5197. ' };',
  5198. '};',
  5199. 'var A$3 = 3;',
  5200. 'this.DoSome = function () {',
  5201. '};',
  5202. '']),
  5203. LinesToStr([
  5204. ''
  5205. ]));
  5206. end;
  5207. procedure TTestModule.TestProc_LocalVarAbsolute;
  5208. begin
  5209. StartProgram(false);
  5210. Add([
  5211. 'type',
  5212. ' TObject = class',
  5213. ' Index: longint;',
  5214. ' procedure DoAbs(Item: pointer);',
  5215. ' end;',
  5216. 'procedure TObject.DoAbs(Item: pointer);',
  5217. 'var',
  5218. ' o: TObject absolute Item;',
  5219. 'begin',
  5220. ' if o.Index<o.Index then o.Index:=o.Index;',
  5221. 'end;',
  5222. 'procedure DoIt(i: longint; p: pointer);',
  5223. 'var',
  5224. ' d: double absolute i;',
  5225. ' s: string absolute d;',
  5226. ' oi: TObject absolute i;',
  5227. ' op: TObject absolute p;',
  5228. 'begin',
  5229. ' if d=d then d:=d;',
  5230. ' if s=s then s:=s;',
  5231. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5232. ' if op.Index=op.Index then op.Index:=op.Index;',
  5233. 'end;',
  5234. 'begin']);
  5235. ConvertProgram;
  5236. CheckSource('TestProc_LocalVarAbsolute',
  5237. LinesToStr([ // statements
  5238. 'rtl.createClass(this, "TObject", null, function () {',
  5239. ' this.$init = function () {',
  5240. ' this.Index = 0;',
  5241. ' };',
  5242. ' this.$final = function () {',
  5243. ' };',
  5244. ' this.DoAbs = function (Item) {',
  5245. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5246. ' };',
  5247. '});',
  5248. 'this.DoIt = function (i, p) {',
  5249. ' if (i === i) i = i;',
  5250. ' if (i === i) i = i;',
  5251. ' if (i.Index < i.Index) i.Index = i.Index;',
  5252. ' if (p.Index === p.Index) p.Index = p.Index;',
  5253. '};'
  5254. ]),
  5255. LinesToStr([
  5256. ]));
  5257. end;
  5258. procedure TTestModule.TestProc_ResultAbsolute;
  5259. begin
  5260. StartProgram(false);
  5261. Add([
  5262. 'type',
  5263. ' TObject = class',
  5264. ' Index: longint;',
  5265. ' function DoAbs: pointer;',
  5266. ' end;',
  5267. 'function TObject.DoAbs: pointer;',
  5268. 'var',
  5269. ' o: TObject absolute Result;',
  5270. 'begin',
  5271. ' if o.Index<o.Index then o.Index:=o.Index;',
  5272. 'end;',
  5273. 'function DoIt: jsvalue;',
  5274. 'var',
  5275. ' d: double absolute Result;',
  5276. ' s: string absolute Result;',
  5277. ' o: TObject absolute Result;',
  5278. 'begin',
  5279. ' if d=d then d:=d;',
  5280. ' if s=s then s:=s;',
  5281. ' if o.Index<o.Index then o.Index:=o.Index;',
  5282. 'end;',
  5283. 'begin']);
  5284. ConvertProgram;
  5285. CheckSource('TestProc_ResultAbsolute',
  5286. LinesToStr([ // statements
  5287. 'rtl.createClass(this, "TObject", null, function () {',
  5288. ' this.$init = function () {',
  5289. ' this.Index = 0;',
  5290. ' };',
  5291. ' this.$final = function () {',
  5292. ' };',
  5293. ' this.DoAbs = function () {',
  5294. ' var Result = null;',
  5295. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5296. ' return Result;',
  5297. ' };',
  5298. '});',
  5299. 'this.DoIt = function () {',
  5300. ' var Result = undefined;',
  5301. ' if (Result === Result) Result = Result;',
  5302. ' if (Result === Result) Result = Result;',
  5303. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5304. ' return Result;',
  5305. '};',
  5306. '']),
  5307. LinesToStr([
  5308. ]));
  5309. end;
  5310. procedure TTestModule.TestProc_LocalVarInit;
  5311. begin
  5312. StartProgram(false);
  5313. Add([
  5314. 'type TBytes = array of byte;',
  5315. 'procedure DoIt;',
  5316. 'const c = 4;',
  5317. 'var',
  5318. ' b: byte = 1;',
  5319. ' w: word = 2+c;',
  5320. ' p: pointer = nil;',
  5321. ' Buffer: TBytes = nil;',
  5322. 'begin',
  5323. 'end;',
  5324. 'begin']);
  5325. ConvertProgram;
  5326. CheckSource('TestProc_LocalVarInit',
  5327. LinesToStr([ // statements
  5328. 'var c = 4;',
  5329. 'this.DoIt = function () {',
  5330. ' var b = 1;',
  5331. ' var w = 2 + 4;',
  5332. ' var p = null;',
  5333. ' var Buffer = [];',
  5334. '};',
  5335. '']),
  5336. LinesToStr([
  5337. ]));
  5338. end;
  5339. procedure TTestModule.TestProc_ReservedWords;
  5340. begin
  5341. StartProgram(false);
  5342. Add([
  5343. 'procedure Date(ArrayBuffer: longint);',
  5344. 'const',
  5345. ' NaN: longint = 3;',
  5346. 'var',
  5347. ' &Boolean: longint;',
  5348. ' procedure Error(ArrayBuffer: longint);',
  5349. ' begin',
  5350. ' end;',
  5351. 'begin',
  5352. ' Nan:=&bOolean;',
  5353. 'end;',
  5354. 'begin',
  5355. ' Date(1);']);
  5356. ConvertProgram;
  5357. CheckSource('TestProc_ReservedWords',
  5358. LinesToStr([ // statements
  5359. 'var naN = 3;',
  5360. 'this.Date = function (arrayBuffer) {',
  5361. ' var boolean = 0;',
  5362. ' function error(arrayBuffer) {',
  5363. ' };',
  5364. ' naN = boolean;',
  5365. '};',
  5366. '']),
  5367. LinesToStr([
  5368. ' $mod.Date(1);'
  5369. ]));
  5370. end;
  5371. procedure TTestModule.TestProc_ConstRefWord;
  5372. begin
  5373. StartProgram(false);
  5374. Add([
  5375. 'procedure Run(constref w: word);',
  5376. 'var l: word;',
  5377. 'begin',
  5378. ' l:=w;',
  5379. ' Run(w);',
  5380. ' Run(l);',
  5381. 'end;',
  5382. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5383. 'begin',
  5384. ' Run(a);',
  5385. ' Run(b);',
  5386. ' Run(c);',
  5387. ' Run(d);',
  5388. ' Run(e);',
  5389. 'end;',
  5390. 'begin',
  5391. ' Run(1);']);
  5392. ConvertProgram;
  5393. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5394. CheckSource('TestProc_ConstRefWord',
  5395. LinesToStr([ // statements
  5396. 'this.Run = function (w) {',
  5397. ' var l = 0;',
  5398. ' l = w;',
  5399. ' $mod.Run(w);',
  5400. ' $mod.Run(l);',
  5401. '};',
  5402. 'this.Fly = function (a, b, c, d, e) {',
  5403. ' $mod.Run(a);',
  5404. ' $mod.Run(b.get());',
  5405. ' $mod.Run(c.get());',
  5406. ' $mod.Run(d);',
  5407. ' $mod.Run(e);',
  5408. '};',
  5409. '']),
  5410. LinesToStr([
  5411. '$mod.Run(1);'
  5412. ]));
  5413. end;
  5414. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5415. begin
  5416. StartProgram(false);
  5417. Add([
  5418. '{$mode objfpc}',
  5419. 'type',
  5420. ' TFunc = reference to function(x: word): word;',
  5421. 'var Func: TFunc;',
  5422. 'procedure DoIt(a: word);',
  5423. 'begin',
  5424. ' Func:=function(b:word): word',
  5425. ' begin',
  5426. ' Result:=a+b;',
  5427. ' exit(b);',
  5428. ' exit(Result);',
  5429. ' end;',// test semicolon
  5430. ' a:=3;',
  5431. 'end;',
  5432. 'begin',
  5433. ' Func:=function(c:word):word begin',
  5434. ' Result:=3+c;',
  5435. ' exit(c);',
  5436. ' exit(Result);',
  5437. ' end;']);
  5438. ConvertProgram;
  5439. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5440. LinesToStr([ // statements
  5441. 'this.Func = null;',
  5442. 'this.DoIt = function (a) {',
  5443. ' $mod.Func = function (b) {',
  5444. ' var Result = 0;',
  5445. ' Result = a + b;',
  5446. ' return b;',
  5447. ' return Result;',
  5448. ' return Result;',
  5449. ' };',
  5450. ' a = 3;',
  5451. '};',
  5452. '']),
  5453. LinesToStr([
  5454. '$mod.Func = function (c) {',
  5455. ' var Result = 0;',
  5456. ' Result = 3 + c;',
  5457. ' return c;',
  5458. ' return Result;',
  5459. ' return Result;',
  5460. '};',
  5461. '']));
  5462. end;
  5463. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5464. begin
  5465. StartProgram(false);
  5466. Add([
  5467. '{$mode delphi}',
  5468. 'type',
  5469. ' TProc = reference to procedure(x: word);',
  5470. 'procedure DoIt(a: word);',
  5471. 'var Proc: TProc;',
  5472. 'begin',
  5473. ' Proc:=procedure(b:word) begin end;',
  5474. 'end;',
  5475. 'var Proc: TProc;',
  5476. 'begin',
  5477. ' Proc:=procedure(c:word) begin end;',
  5478. '']);
  5479. ConvertProgram;
  5480. CheckSource('TestAnonymousProc_Assign_Delphi',
  5481. LinesToStr([ // statements
  5482. 'this.DoIt = function (a) {',
  5483. ' var Proc = null;',
  5484. ' Proc = function (b) {',
  5485. ' };',
  5486. '};',
  5487. 'this.Proc = null;',
  5488. '']),
  5489. LinesToStr([
  5490. '$mod.Proc = function (c) {',
  5491. '};',
  5492. '']));
  5493. end;
  5494. procedure TTestModule.TestAnonymousProc_Arg;
  5495. begin
  5496. StartProgram(false);
  5497. Add([
  5498. 'type',
  5499. ' TProc = reference to procedure;',
  5500. ' TFunc = reference to function(x: word): word;',
  5501. 'procedure DoMore(f,g: TProc);',
  5502. 'begin',
  5503. 'end;',
  5504. 'procedure DoOdd(v: jsvalue);',
  5505. 'begin',
  5506. 'end;',
  5507. 'procedure DoIt(f: TFunc);',
  5508. 'begin',
  5509. ' DoIt(function(b:word): word',
  5510. ' begin',
  5511. ' Result:=1+b;',
  5512. ' end);',
  5513. ' DoMore(procedure begin end, procedure begin end);',
  5514. ' DoOdd(procedure begin end);',
  5515. 'end;',
  5516. 'begin',
  5517. ' DoMore(procedure begin end,',
  5518. ' procedure assembler asm',
  5519. ' console.log("c");',
  5520. ' end);',
  5521. '']);
  5522. ConvertProgram;
  5523. CheckSource('TestAnonymousProc_Arg',
  5524. LinesToStr([ // statements
  5525. 'this.DoMore = function (f, g) {',
  5526. '};',
  5527. 'this.DoOdd = function (v) {',
  5528. '};',
  5529. 'this.DoIt = function (f) {',
  5530. ' $mod.DoIt(function (b) {',
  5531. ' var Result = 0;',
  5532. ' Result = 1 + b;',
  5533. ' return Result;',
  5534. ' });',
  5535. ' $mod.DoMore(function () {',
  5536. ' }, function () {',
  5537. ' });',
  5538. ' $mod.DoOdd(function () {',
  5539. ' });',
  5540. '};',
  5541. '']),
  5542. LinesToStr([
  5543. '$mod.DoMore(function () {',
  5544. '}, function () {',
  5545. ' console.log("c");',
  5546. '});',
  5547. '']));
  5548. end;
  5549. procedure TTestModule.TestAnonymousProc_Typecast;
  5550. begin
  5551. StartProgram(false);
  5552. Add([
  5553. 'type',
  5554. ' TProc = reference to procedure(w: word);',
  5555. ' TArr = array of word;',
  5556. ' TFuncArr = reference to function: TArr;',
  5557. 'procedure DoIt(p: TProc);',
  5558. 'var',
  5559. ' w: word;',
  5560. ' a: TArr;',
  5561. 'begin',
  5562. ' p:=TProc(procedure(b: smallint) begin end);',
  5563. ' a:=TFuncArr(function: TArr begin end)();',
  5564. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5565. 'end;',
  5566. 'begin']);
  5567. ConvertProgram;
  5568. CheckSource('TestAnonymousProc_Typecast',
  5569. LinesToStr([ // statements
  5570. 'this.DoIt = function (p) {',
  5571. ' var w = 0;',
  5572. ' var a = [];',
  5573. ' p = function (b) {',
  5574. ' };',
  5575. ' a = function () {',
  5576. ' var Result = [];',
  5577. ' return Result;',
  5578. ' }();',
  5579. ' w = function () {',
  5580. ' var Result = [];',
  5581. ' return Result;',
  5582. ' }()[3];',
  5583. '};',
  5584. '']),
  5585. LinesToStr([
  5586. '']));
  5587. end;
  5588. procedure TTestModule.TestAnonymousProc_With;
  5589. begin
  5590. StartProgram(false);
  5591. Add([
  5592. 'type',
  5593. ' TProc = reference to procedure(w: word);',
  5594. ' TObject = class',
  5595. ' b: boolean;',
  5596. ' end;',
  5597. 'var',
  5598. ' p: TProc;',
  5599. ' bird: TObject;',
  5600. 'begin',
  5601. ' with bird do',
  5602. ' p:=procedure(w: word)',
  5603. ' begin',
  5604. ' b:=w>2;',
  5605. ' end;',
  5606. '']);
  5607. ConvertProgram;
  5608. CheckSource('TestAnonymousProc_With',
  5609. LinesToStr([ // statements
  5610. 'rtl.createClass(this, "TObject", null, function () {',
  5611. ' this.$init = function () {',
  5612. ' this.b = false;',
  5613. ' };',
  5614. ' this.$final = function () {',
  5615. ' };',
  5616. '});',
  5617. 'this.p = null;',
  5618. 'this.bird = null;',
  5619. '']),
  5620. LinesToStr([
  5621. 'var $with = $mod.bird;',
  5622. '$mod.p = function (w) {',
  5623. ' $with.b = w > 2;',
  5624. '};',
  5625. '']));
  5626. end;
  5627. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5628. begin
  5629. StartProgram(false);
  5630. Add([
  5631. 'type',
  5632. ' TProc = reference to procedure;',
  5633. ' TObject = class',
  5634. ' b: boolean;',
  5635. ' end;',
  5636. 'procedure DoIt;',
  5637. 'var',
  5638. ' p: TProc;',
  5639. 'begin',
  5640. ' try',
  5641. ' except',
  5642. ' on E: TObject do',
  5643. ' p:=procedure',
  5644. ' begin',
  5645. ' E.b:=true;',
  5646. ' end;',
  5647. ' end;',
  5648. 'end;',
  5649. 'begin']);
  5650. ConvertProgram;
  5651. CheckSource('TestAnonymousProc_ExceptOn',
  5652. LinesToStr([ // statements
  5653. 'rtl.createClass(this, "TObject", null, function () {',
  5654. ' this.$init = function () {',
  5655. ' this.b = false;',
  5656. ' };',
  5657. ' this.$final = function () {',
  5658. ' };',
  5659. '});',
  5660. 'this.DoIt = function () {',
  5661. ' var p = null;',
  5662. ' try {} catch ($e) {',
  5663. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5664. ' var E = $e;',
  5665. ' p = function () {',
  5666. ' E.b = true;',
  5667. ' };',
  5668. ' } else throw $e',
  5669. ' };',
  5670. '};',
  5671. '']),
  5672. LinesToStr([
  5673. '']));
  5674. end;
  5675. procedure TTestModule.TestAnonymousProc_Nested;
  5676. begin
  5677. StartProgram(false);
  5678. Add([
  5679. 'type',
  5680. ' TProc = reference to procedure;',
  5681. ' TObject = class',
  5682. ' i: byte;',
  5683. ' procedure DoIt;',
  5684. ' end;',
  5685. 'procedure TObject.DoIt;',
  5686. 'var',
  5687. ' p: TProc;',
  5688. ' procedure Sub;',
  5689. ' begin',
  5690. ' p:=procedure',
  5691. ' begin',
  5692. ' i:=3;',
  5693. ' Self.i:=4;',
  5694. ' p:=procedure',
  5695. ' procedure SubSub;',
  5696. ' begin',
  5697. ' i:=13;',
  5698. ' Self.i:=14;',
  5699. ' end;',
  5700. ' begin',
  5701. ' i:=13;',
  5702. ' Self.i:=14;',
  5703. ' end;',
  5704. ' end;',
  5705. ' end;',
  5706. 'begin',
  5707. 'end;',
  5708. 'begin']);
  5709. ConvertProgram;
  5710. CheckSource('TestAnonymousProc_Nested',
  5711. LinesToStr([ // statements
  5712. 'rtl.createClass(this, "TObject", null, function () {',
  5713. ' this.$init = function () {',
  5714. ' this.i = 0;',
  5715. ' };',
  5716. ' this.$final = function () {',
  5717. ' };',
  5718. ' this.DoIt = function () {',
  5719. ' var $Self = this;',
  5720. ' var p = null;',
  5721. ' function Sub() {',
  5722. ' p = function () {',
  5723. ' $Self.i = 3;',
  5724. ' $Self.i = 4;',
  5725. ' p = function () {',
  5726. ' function SubSub() {',
  5727. ' $Self.i = 13;',
  5728. ' $Self.i = 14;',
  5729. ' };',
  5730. ' $Self.i = 13;',
  5731. ' $Self.i = 14;',
  5732. ' };',
  5733. ' };',
  5734. ' };',
  5735. ' };',
  5736. '});',
  5737. '']),
  5738. LinesToStr([
  5739. '']));
  5740. end;
  5741. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5742. begin
  5743. StartProgram(false);
  5744. Add([
  5745. 'type',
  5746. ' TProc = reference to procedure;',
  5747. 'function DoIt: TProc;',
  5748. ' function Sub: TProc;',
  5749. ' begin',
  5750. ' Result:=procedure',
  5751. ' begin',
  5752. ' Sub:=procedure',
  5753. ' procedure SubSub;',
  5754. ' begin',
  5755. ' Result:=nil;',
  5756. ' Sub:=nil;',
  5757. ' DoIt:=nil;',
  5758. ' end;',
  5759. ' begin',
  5760. ' Result:=nil;',
  5761. ' Sub:=nil;',
  5762. ' DoIt:=nil;',
  5763. ' end;',
  5764. ' end;',
  5765. ' end;',
  5766. 'begin',
  5767. 'end;',
  5768. 'begin']);
  5769. ConvertProgram;
  5770. CheckSource('TestAnonymousProc_NestedAssignResult',
  5771. LinesToStr([ // statements
  5772. 'this.DoIt = function () {',
  5773. ' var Result = null;',
  5774. ' function Sub() {',
  5775. ' var Result$1 = null;',
  5776. ' Result$1 = function () {',
  5777. ' Result$1 = function () {',
  5778. ' function SubSub() {',
  5779. ' Result$1 = null;',
  5780. ' Result$1 = null;',
  5781. ' Result = null;',
  5782. ' };',
  5783. ' Result$1 = null;',
  5784. ' Result$1 = null;',
  5785. ' Result = null;',
  5786. ' };',
  5787. ' };',
  5788. ' return Result$1;',
  5789. ' };',
  5790. ' return Result;',
  5791. '};',
  5792. '']),
  5793. LinesToStr([
  5794. '']));
  5795. end;
  5796. procedure TTestModule.TestAnonymousProc_Class;
  5797. begin
  5798. StartProgram(false);
  5799. Add([
  5800. 'type',
  5801. ' TProc = reference to procedure;',
  5802. ' TEvent = procedure of object;',
  5803. ' TObject = class',
  5804. ' Size: word;',
  5805. ' function GetIt: TProc;',
  5806. ' procedure DoIt; virtual; abstract;',
  5807. ' end;',
  5808. 'function TObject.GetIt: TProc;',
  5809. 'begin',
  5810. ' Result:=procedure',
  5811. ' var p: TEvent;',
  5812. ' begin',
  5813. ' Size:=Size;',
  5814. ' Size:=Self.Size;',
  5815. ' p:=@DoIt;',
  5816. ' p:[email protected];',
  5817. ' end;',
  5818. 'end;',
  5819. 'begin']);
  5820. ConvertProgram;
  5821. CheckSource('TestAnonymousProc_Class',
  5822. LinesToStr([ // statements
  5823. 'rtl.createClass(this, "TObject", null, function () {',
  5824. ' this.$init = function () {',
  5825. ' this.Size = 0;',
  5826. ' };',
  5827. ' this.$final = function () {',
  5828. ' };',
  5829. ' this.GetIt = function () {',
  5830. ' var $Self = this;',
  5831. ' var Result = null;',
  5832. ' Result = function () {',
  5833. ' var p = null;',
  5834. ' $Self.Size = $Self.Size;',
  5835. ' $Self.Size = $Self.Size;',
  5836. ' p = rtl.createCallback($Self, "DoIt");',
  5837. ' p = rtl.createCallback($Self, "DoIt");',
  5838. ' };',
  5839. ' return Result;',
  5840. ' };',
  5841. '});',
  5842. '']),
  5843. LinesToStr([
  5844. '']));
  5845. end;
  5846. procedure TTestModule.TestAnonymousProc_ForLoop;
  5847. begin
  5848. StartProgram(false);
  5849. Add([
  5850. 'type TProc = reference to procedure;',
  5851. 'procedure Foo(p: TProc);',
  5852. 'begin',
  5853. 'end;',
  5854. 'procedure DoIt;',
  5855. 'var i: word;',
  5856. ' a: word;',
  5857. 'begin',
  5858. ' for i:=1 to 10 do begin',
  5859. ' Foo(procedure begin a:=3; end);',
  5860. ' end;',
  5861. 'end;',
  5862. 'begin',
  5863. ' DoIt;']);
  5864. ConvertProgram;
  5865. CheckSource('TestAnonymousProc_ForLoop',
  5866. LinesToStr([ // statements
  5867. 'this.Foo = function (p) {',
  5868. '};',
  5869. 'this.DoIt = function () {',
  5870. ' var i = 0;',
  5871. ' var a = 0;',
  5872. ' for (i = 1; i <= 10; i++) {',
  5873. ' $mod.Foo(function () {',
  5874. ' a = 3;',
  5875. ' });',
  5876. ' };',
  5877. '};',
  5878. '']),
  5879. LinesToStr([
  5880. '$mod.DoIt();'
  5881. ]));
  5882. end;
  5883. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5884. begin
  5885. StartProgram(false);
  5886. Add([
  5887. '{$mode delphi}',
  5888. 'type',
  5889. ' TProc = reference to procedure;',
  5890. ' TFunc = reference to function(x: word): word;',
  5891. 'procedure Run;',
  5892. 'asm',
  5893. 'end;',
  5894. 'procedure Walk(p: TProc; f: TFunc);',
  5895. 'begin',
  5896. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5897. 'end;',
  5898. 'begin',
  5899. ' Walk(procedure',
  5900. ' asm',
  5901. ' console.log("a");',
  5902. ' end,',
  5903. ' function(x: word): word asm',
  5904. ' console.log("c");',
  5905. ' end);',
  5906. '']);
  5907. ConvertProgram;
  5908. CheckSource('TestAnonymousProc_AsmDelphi',
  5909. LinesToStr([ // statements
  5910. 'this.Run = function () {',
  5911. '};',
  5912. 'this.Walk = function (p, f) {',
  5913. ' $mod.Walk(function () {',
  5914. ' }, function (b) {',
  5915. ' return 1+b;',
  5916. ' });',
  5917. '};',
  5918. '']),
  5919. LinesToStr([
  5920. '$mod.Walk(function () {',
  5921. ' console.log("a");',
  5922. '}, function (x) {',
  5923. ' console.log("c");',
  5924. '});',
  5925. '']));
  5926. end;
  5927. procedure TTestModule.TestEnum_Name;
  5928. begin
  5929. StartProgram(false);
  5930. Add('type TMyEnum = (Red, Green, Blue);');
  5931. Add('var e: TMyEnum;');
  5932. Add('var f: TMyEnum = Blue;');
  5933. Add('begin');
  5934. Add(' e:=green;');
  5935. Add(' e:=default(TMyEnum);');
  5936. ConvertProgram;
  5937. CheckSource('TestEnum_Name',
  5938. LinesToStr([ // statements
  5939. 'this.TMyEnum = {',
  5940. ' "0":"Red",',
  5941. ' Red:0,',
  5942. ' "1":"Green",',
  5943. ' Green:1,',
  5944. ' "2":"Blue",',
  5945. ' Blue:2',
  5946. ' };',
  5947. 'this.e = 0;',
  5948. 'this.f = this.TMyEnum.Blue;'
  5949. ]),
  5950. LinesToStr([
  5951. '$mod.e=$mod.TMyEnum.Green;',
  5952. '$mod.e=$mod.TMyEnum.Red;'
  5953. ]));
  5954. end;
  5955. procedure TTestModule.TestEnum_Number;
  5956. begin
  5957. Converter.Options:=Converter.Options+[coEnumNumbers];
  5958. StartProgram(false);
  5959. Add('type TMyEnum = (Red, Green);');
  5960. Add('var');
  5961. Add(' e: TMyEnum;');
  5962. Add(' f: TMyEnum = Green;');
  5963. Add(' i: longint;');
  5964. Add('begin');
  5965. Add(' e:=green;');
  5966. Add(' i:=longint(e);');
  5967. ConvertProgram;
  5968. CheckSource('TestEnumNumber',
  5969. LinesToStr([ // statements
  5970. 'this.TMyEnum = {',
  5971. ' "0":"Red",',
  5972. ' Red:0,',
  5973. ' "1":"Green",',
  5974. ' Green:1',
  5975. ' };',
  5976. 'this.e = 0;',
  5977. 'this.f = 1;',
  5978. 'this.i = 0;'
  5979. ]),
  5980. LinesToStr([
  5981. '$mod.e=1;',
  5982. '$mod.i=$mod.e;'
  5983. ]));
  5984. end;
  5985. procedure TTestModule.TestEnum_ConstFail;
  5986. begin
  5987. StartProgram(false);
  5988. Add([
  5989. 'type TMyEnum = (Red = 100, Green = 101);',
  5990. 'var',
  5991. ' e: TMyEnum;',
  5992. ' f: TMyEnum = Green;',
  5993. 'begin',
  5994. ' e:=green;']);
  5995. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5996. ConvertProgram;
  5997. end;
  5998. procedure TTestModule.TestEnum_Functions;
  5999. begin
  6000. StartProgram(false);
  6001. Add([
  6002. 'type TMyEnum = (Red, Green);',
  6003. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6004. 'var',
  6005. ' v: longint;',
  6006. ' s: string;',
  6007. 'begin',
  6008. ' val(s,e,v);',
  6009. ' val(s,e,i);',
  6010. 'end;',
  6011. 'var',
  6012. ' e: TMyEnum;',
  6013. ' i: longint;',
  6014. ' s: string;',
  6015. ' b: boolean;',
  6016. 'begin',
  6017. ' i:=ord(red);',
  6018. ' i:=ord(green);',
  6019. ' i:=ord(e);',
  6020. ' i:=ord(b);',
  6021. ' e:=low(tmyenum);',
  6022. ' e:=low(e);',
  6023. ' b:=low(boolean);',
  6024. ' e:=high(tmyenum);',
  6025. ' e:=high(e);',
  6026. ' b:=high(boolean);',
  6027. ' e:=pred(green);',
  6028. ' e:=pred(e);',
  6029. ' b:=pred(b);',
  6030. ' e:=succ(red);',
  6031. ' e:=succ(e);',
  6032. ' b:=succ(b);',
  6033. ' e:=tmyenum(1);',
  6034. ' e:=tmyenum(i);',
  6035. ' s:=str(e);',
  6036. ' str(e,s);',
  6037. ' str(red,s);',
  6038. ' s:=str(e:3);',
  6039. ' writestr(s,e:3,red);',
  6040. ' val(s,e,i);',
  6041. ' i:=longint(e);']);
  6042. ConvertProgram;
  6043. CheckSource('TestEnum_Functions',
  6044. LinesToStr([ // statements
  6045. 'this.TMyEnum = {',
  6046. ' "0":"Red",',
  6047. ' Red:0,',
  6048. ' "1":"Green",',
  6049. ' Green:1',
  6050. ' };',
  6051. 'this.DoIt = function (e, i) {',
  6052. ' var v = 0;',
  6053. ' var s = "";',
  6054. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6055. ' v = w;',
  6056. ' }));',
  6057. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6058. '};',
  6059. 'this.e = 0;',
  6060. 'this.i = 0;',
  6061. 'this.s = "";',
  6062. 'this.b = false;',
  6063. '']),
  6064. LinesToStr([
  6065. '$mod.i=$mod.TMyEnum.Red;',
  6066. '$mod.i=$mod.TMyEnum.Green;',
  6067. '$mod.i=$mod.e;',
  6068. '$mod.i=$mod.b+0;',
  6069. '$mod.e=$mod.TMyEnum.Red;',
  6070. '$mod.e=$mod.TMyEnum.Red;',
  6071. '$mod.b=false;',
  6072. '$mod.e=$mod.TMyEnum.Green;',
  6073. '$mod.e=$mod.TMyEnum.Green;',
  6074. '$mod.b=true;',
  6075. '$mod.e=$mod.TMyEnum.Green-1;',
  6076. '$mod.e=$mod.e-1;',
  6077. '$mod.b=false;',
  6078. '$mod.e=$mod.TMyEnum.Red+1;',
  6079. '$mod.e=$mod.e+1;',
  6080. '$mod.b=true;',
  6081. '$mod.e=1;',
  6082. '$mod.e=$mod.i;',
  6083. '$mod.s = $mod.TMyEnum[$mod.e];',
  6084. '$mod.s = $mod.TMyEnum[$mod.e];',
  6085. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6086. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6087. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6088. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6089. ' $mod.i = v;',
  6090. '});',
  6091. '$mod.i=$mod.e;',
  6092. '']));
  6093. end;
  6094. procedure TTestModule.TestEnumRg_Functions;
  6095. begin
  6096. StartProgram(false);
  6097. Add([
  6098. 'type',
  6099. ' TEnum = (Red, Green, Blue);',
  6100. ' TEnumRg = Green..Blue;',
  6101. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6102. 'var',
  6103. ' v: longint;',
  6104. ' s: string;',
  6105. 'begin',
  6106. ' val(s,e,v);',
  6107. ' val(s,e,i);',
  6108. 'end;',
  6109. 'var',
  6110. ' e: TEnumRg;',
  6111. ' i: longint;',
  6112. ' s: string;',
  6113. 'begin',
  6114. ' i:=ord(green);',
  6115. ' i:=ord(e);',
  6116. ' e:=low(tenumrg);',
  6117. ' e:=low(e);',
  6118. ' e:=high(tenumrg);',
  6119. ' e:=high(e);',
  6120. ' e:=pred(blue);',
  6121. ' e:=pred(e);',
  6122. ' e:=succ(green);',
  6123. ' e:=succ(e);',
  6124. ' e:=tenumrg(1);',
  6125. ' e:=tenumrg(i);',
  6126. ' s:=str(e);',
  6127. ' str(e,s);',
  6128. ' str(red,s);',
  6129. ' s:=str(e:3);',
  6130. ' writestr(s,e:3,blue);',
  6131. ' val(s,e,i);',
  6132. ' i:=longint(e);']);
  6133. ConvertProgram;
  6134. CheckSource('TestEnumRg_Functions',
  6135. LinesToStr([ // statements
  6136. 'this.TEnum = {',
  6137. ' "0":"Red",',
  6138. ' Red:0,',
  6139. ' "1":"Green",',
  6140. ' Green:1,',
  6141. ' "2":"Blue",',
  6142. ' Blue:2',
  6143. ' };',
  6144. 'this.DoIt = function (e, i) {',
  6145. ' var v = 0;',
  6146. ' var s = "";',
  6147. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6148. ' v = w;',
  6149. ' }));',
  6150. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6151. '};',
  6152. 'this.e = this.TEnum.Green;',
  6153. 'this.i = 0;',
  6154. 'this.s = "";',
  6155. '']),
  6156. LinesToStr([
  6157. '$mod.i=$mod.TEnum.Green;',
  6158. '$mod.i=$mod.e;',
  6159. '$mod.e=$mod.TEnum.Green;',
  6160. '$mod.e=$mod.TEnum.Green;',
  6161. '$mod.e=$mod.TEnum.Blue;',
  6162. '$mod.e=$mod.TEnum.Blue;',
  6163. '$mod.e=$mod.TEnum.Blue-1;',
  6164. '$mod.e=$mod.e-1;',
  6165. '$mod.e=$mod.TEnum.Green+1;',
  6166. '$mod.e=$mod.e+1;',
  6167. '$mod.e=1;',
  6168. '$mod.e=$mod.i;',
  6169. '$mod.s = $mod.TEnum[$mod.e];',
  6170. '$mod.s = $mod.TEnum[$mod.e];',
  6171. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6172. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6173. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6174. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6175. ' $mod.i = v;',
  6176. '});',
  6177. '$mod.i=$mod.e;',
  6178. '']));
  6179. end;
  6180. procedure TTestModule.TestEnum_AsParams;
  6181. begin
  6182. StartProgram(false);
  6183. Add('type TEnum = (Red,Blue);');
  6184. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6185. Add('var vJ: TEnum;');
  6186. Add('begin');
  6187. Add(' vg:=vg;');
  6188. Add(' vj:=vh;');
  6189. Add(' vi:=vi;');
  6190. Add(' doit(vg,vg,vg);');
  6191. Add(' doit(vh,vh,vj);');
  6192. Add(' doit(vi,vi,vi);');
  6193. Add(' doit(vj,vj,vj);');
  6194. Add('end;');
  6195. Add('var i: TEnum;');
  6196. Add('begin');
  6197. Add(' doit(i,i,i);');
  6198. ConvertProgram;
  6199. CheckSource('TestEnum_AsParams',
  6200. LinesToStr([ // statements
  6201. 'this.TEnum = {',
  6202. ' "0": "Red",',
  6203. ' Red: 0,',
  6204. ' "1": "Blue",',
  6205. ' Blue: 1',
  6206. '};',
  6207. 'this.DoIt = function (vG,vH,vI) {',
  6208. ' var vJ = 0;',
  6209. ' vG = vG;',
  6210. ' vJ = vH;',
  6211. ' vI.set(vI.get());',
  6212. ' $mod.DoIt(vG, vG, {',
  6213. ' get: function () {',
  6214. ' return vG;',
  6215. ' },',
  6216. ' set: function (v) {',
  6217. ' vG = v;',
  6218. ' }',
  6219. ' });',
  6220. ' $mod.DoIt(vH, vH, {',
  6221. ' get: function () {',
  6222. ' return vJ;',
  6223. ' },',
  6224. ' set: function (v) {',
  6225. ' vJ = v;',
  6226. ' }',
  6227. ' });',
  6228. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6229. ' $mod.DoIt(vJ, vJ, {',
  6230. ' get: function () {',
  6231. ' return vJ;',
  6232. ' },',
  6233. ' set: function (v) {',
  6234. ' vJ = v;',
  6235. ' }',
  6236. ' });',
  6237. '};',
  6238. 'this.i = 0;'
  6239. ]),
  6240. LinesToStr([
  6241. '$mod.DoIt($mod.i,$mod.i,{',
  6242. ' p: $mod,',
  6243. ' get: function () {',
  6244. ' return this.p.i;',
  6245. ' },',
  6246. ' set: function (v) {',
  6247. ' this.p.i = v;',
  6248. ' }',
  6249. '});'
  6250. ]));
  6251. end;
  6252. procedure TTestModule.TestEnumRange_Array;
  6253. begin
  6254. StartProgram(false);
  6255. Add([
  6256. 'type',
  6257. ' TEnum = (Red, Green, Blue);',
  6258. ' TEnumRg = green..blue;',
  6259. ' TArr = array[TEnumRg] of byte;',
  6260. ' TArr2 = array[green..blue] of byte;',
  6261. 'var',
  6262. ' a: TArr;',
  6263. ' b: TArr = (3,4);',
  6264. ' c: TArr2 = (5,6);',
  6265. 'begin',
  6266. ' a[green] := b[blue];',
  6267. ' c[green] := c[blue];',
  6268. '']);
  6269. ConvertProgram;
  6270. CheckSource('TestEnumRange_Array',
  6271. LinesToStr([ // statements
  6272. 'this.TEnum = {',
  6273. ' "0": "Red",',
  6274. ' Red: 0,',
  6275. ' "1": "Green",',
  6276. ' Green: 1,',
  6277. ' "2": "Blue",',
  6278. ' Blue: 2',
  6279. '};',
  6280. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6281. 'this.b = [3, 4];',
  6282. 'this.c = [5, 6];',
  6283. '']),
  6284. LinesToStr([
  6285. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6286. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6287. '']));
  6288. end;
  6289. procedure TTestModule.TestEnum_ForIn;
  6290. begin
  6291. StartProgram(false);
  6292. Add([
  6293. 'type',
  6294. ' TEnum = (Red, Green, Blue);',
  6295. ' TEnumRg = green..blue;',
  6296. ' TArr = array[TEnum] of byte;',
  6297. ' TArrRg = array[TEnumRg] of byte;',
  6298. 'var',
  6299. ' e: TEnum;',
  6300. ' a1: TArr = (3,4,5);',
  6301. ' a2: TArrRg = (11,12);',
  6302. ' b: byte;',
  6303. 'begin',
  6304. ' for e in TEnum do ;',
  6305. ' for e in TEnumRg do ;',
  6306. ' for e in TArr do ;',
  6307. ' for e in TArrRg do ;',
  6308. ' for b in a1 do ;',
  6309. ' for b in a2 do ;',
  6310. '']);
  6311. ConvertProgram;
  6312. CheckSource('TestEnum_ForIn',
  6313. LinesToStr([ // statements
  6314. 'this.TEnum = {',
  6315. ' "0": "Red",',
  6316. ' Red: 0,',
  6317. ' "1": "Green",',
  6318. ' Green: 1,',
  6319. ' "2": "Blue",',
  6320. ' Blue: 2',
  6321. '};',
  6322. 'this.e = 0;',
  6323. 'this.a1 = [3, 4, 5];',
  6324. 'this.a2 = [11, 12];',
  6325. 'this.b = 0;',
  6326. '']),
  6327. LinesToStr([
  6328. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6329. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6330. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6331. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6332. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6333. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6334. '']));
  6335. end;
  6336. procedure TTestModule.TestEnum_ScopedNumber;
  6337. begin
  6338. Converter.Options:=Converter.Options+[coEnumNumbers];
  6339. StartProgram(false);
  6340. Add([
  6341. 'type',
  6342. ' TEnum = (Red, Green);',
  6343. 'var',
  6344. ' e: TEnum;',
  6345. 'begin',
  6346. ' e:=TEnum.Green;',
  6347. '']);
  6348. ConvertProgram;
  6349. CheckSource('TestEnum_ScopedNumber',
  6350. LinesToStr([ // statements
  6351. 'this.TEnum = {',
  6352. ' "0": "Red",',
  6353. ' Red: 0,',
  6354. ' "1": "Green",',
  6355. ' Green: 1',
  6356. '};',
  6357. 'this.e = 0;',
  6358. '']),
  6359. LinesToStr([
  6360. '$mod.e = 1;']));
  6361. end;
  6362. procedure TTestModule.TestEnum_InFunction;
  6363. begin
  6364. StartProgram(false);
  6365. Add([
  6366. 'const TEnum = 3;',
  6367. 'procedure DoIt;',
  6368. 'type',
  6369. ' TEnum = (Red, Green, Blue);',
  6370. ' procedure Sub;',
  6371. ' type',
  6372. ' TEnumSub = (Left, Right);',
  6373. ' var',
  6374. ' es: TEnumSub;',
  6375. ' begin',
  6376. ' es:=Left;',
  6377. ' end;',
  6378. 'var',
  6379. ' e, e2: TEnum;',
  6380. 'begin',
  6381. ' if e in [red,blue] then e2:=e;',
  6382. 'end;',
  6383. 'begin']);
  6384. ConvertProgram;
  6385. CheckSource('TestEnum_InFunction',
  6386. LinesToStr([ // statements
  6387. 'this.TEnum = 3;',
  6388. 'var TEnum$1 = {',
  6389. ' "0":"Red",',
  6390. ' Red:0,',
  6391. ' "1":"Green",',
  6392. ' Green:1,',
  6393. ' "2":"Blue",',
  6394. ' Blue:2',
  6395. ' };',
  6396. 'var TEnumSub = {',
  6397. ' "0": "Left",',
  6398. ' Left: 0,',
  6399. ' "1": "Right",',
  6400. ' Right: 1',
  6401. '};',
  6402. 'this.DoIt = function () {',
  6403. ' function Sub() {',
  6404. ' var es = 0;',
  6405. ' es = TEnumSub.Left;',
  6406. ' };',
  6407. ' var e = 0;',
  6408. ' var e2 = 0;',
  6409. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6410. '};',
  6411. '']),
  6412. LinesToStr([
  6413. '']));
  6414. end;
  6415. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6416. begin
  6417. StartUnit(true);
  6418. Add([
  6419. 'interface',
  6420. 'var color: (red, green);',
  6421. 'implementation',
  6422. 'initialization',
  6423. ' color:=green;',
  6424. '']);
  6425. ConvertUnit;
  6426. CheckSource('TestEnum_Name_Anonymous_Unit',
  6427. LinesToStr([
  6428. 'this.color$a = {',
  6429. ' "0": "red",',
  6430. ' red: 0,',
  6431. ' "1": "green",',
  6432. ' green: 1',
  6433. '};',
  6434. 'this.color = 0;',
  6435. '']),
  6436. LinesToStr([ // this.$init
  6437. '$mod.color = $mod.color$a.green;',
  6438. '']),
  6439. LinesToStr([ // implementation
  6440. '']) );
  6441. end;
  6442. procedure TTestModule.TestSet_Enum;
  6443. begin
  6444. StartProgram(false);
  6445. Add([
  6446. 'type',
  6447. ' TColor = (Red, Green, Blue);',
  6448. ' TColors = set of TColor;',
  6449. 'var',
  6450. ' c: TColor;',
  6451. ' s: TColors;',
  6452. ' t: TColors = [];',
  6453. ' u: TColors = [Red];',
  6454. 'begin',
  6455. ' s:=[];',
  6456. ' s:=[Green];',
  6457. ' s:=[Green,Blue];',
  6458. ' s:=[Red..Blue];',
  6459. ' s:=[Red,Green..Blue];',
  6460. ' s:=[Red,c];',
  6461. ' s:=t;',
  6462. ' s:=default(TColors);',
  6463. '']);
  6464. ConvertProgram;
  6465. CheckSource('TestSet',
  6466. LinesToStr([ // statements
  6467. 'this.TColor = {',
  6468. ' "0":"Red",',
  6469. ' Red:0,',
  6470. ' "1":"Green",',
  6471. ' Green:1,',
  6472. ' "2":"Blue",',
  6473. ' Blue:2',
  6474. ' };',
  6475. 'this.c = 0;',
  6476. 'this.s = {};',
  6477. 'this.t = {};',
  6478. 'this.u = rtl.createSet(this.TColor.Red);'
  6479. ]),
  6480. LinesToStr([
  6481. '$mod.s={};',
  6482. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6483. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6484. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6485. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6486. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6487. '$mod.s=rtl.refSet($mod.t);',
  6488. '$mod.s={};',
  6489. '']));
  6490. end;
  6491. procedure TTestModule.TestSet_Operators;
  6492. begin
  6493. StartProgram(false);
  6494. Add('type');
  6495. Add(' TColor = (Red, Green, Blue);');
  6496. Add(' TColors = set of tcolor;');
  6497. Add('var');
  6498. Add(' vC: TColor;');
  6499. Add(' vS: TColors;');
  6500. Add(' vT: TColors;');
  6501. Add(' vU: TColors;');
  6502. Add(' B: boolean;');
  6503. Add('begin');
  6504. Add(' include(vs,green);');
  6505. Add(' exclude(vs,vc);');
  6506. Add(' vs:=vt+vu;');
  6507. Add(' vs:=vt+[red];');
  6508. Add(' vs:=[red]+vt;');
  6509. Add(' vs:=[red]+[green];');
  6510. Add(' vs:=vt-vu;');
  6511. Add(' vs:=vt-[red];');
  6512. Add(' vs:=[red]-vt;');
  6513. Add(' vs:=[red]-[green];');
  6514. Add(' vs:=vt*vu;');
  6515. Add(' vs:=vt*[red];');
  6516. Add(' vs:=[red]*vt;');
  6517. Add(' vs:=[red]*[green];');
  6518. Add(' vs:=vt><vu;');
  6519. Add(' vs:=vt><[red];');
  6520. Add(' vs:=[red]><vt;');
  6521. Add(' vs:=[red]><[green];');
  6522. Add(' b:=vt=vu;');
  6523. Add(' b:=vt=[red];');
  6524. Add(' b:=[red]=vt;');
  6525. Add(' b:=[red]=[green];');
  6526. Add(' b:=vt<>vu;');
  6527. Add(' b:=vt<>[red];');
  6528. Add(' b:=[red]<>vt;');
  6529. Add(' b:=[red]<>[green];');
  6530. Add(' b:=vt<=vu;');
  6531. Add(' b:=vt<=[red];');
  6532. Add(' b:=[red]<=vt;');
  6533. Add(' b:=[red]<=[green];');
  6534. Add(' b:=vt>=vu;');
  6535. Add(' b:=vt>=[red];');
  6536. Add(' b:=[red]>=vt;');
  6537. Add(' b:=[red]>=[green];');
  6538. ConvertProgram;
  6539. CheckSource('TestSet_Operators',
  6540. LinesToStr([ // statements
  6541. 'this.TColor = {',
  6542. ' "0":"Red",',
  6543. ' Red:0,',
  6544. ' "1":"Green",',
  6545. ' Green:1,',
  6546. ' "2":"Blue",',
  6547. ' Blue:2',
  6548. ' };',
  6549. 'this.vC = 0;',
  6550. 'this.vS = {};',
  6551. 'this.vT = {};',
  6552. 'this.vU = {};',
  6553. 'this.B = false;'
  6554. ]),
  6555. LinesToStr([
  6556. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6557. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6558. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6559. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6560. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6561. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6562. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6563. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6564. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6565. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6566. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6567. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6568. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6569. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6570. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6571. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6572. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6573. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6574. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6575. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6576. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6577. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6578. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6579. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6580. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6581. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6582. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6583. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6584. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6585. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6586. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6587. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6588. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6589. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6590. '']));
  6591. end;
  6592. procedure TTestModule.TestSet_Operator_In;
  6593. begin
  6594. StartProgram(false);
  6595. Add([
  6596. 'type',
  6597. ' TColor = (Red, Green, Blue);',
  6598. ' TColors = set of tcolor;',
  6599. ' TColorRg = green..blue;',
  6600. 'var',
  6601. ' vC: tcolor;',
  6602. ' vT: tcolors;',
  6603. ' B: boolean;',
  6604. ' rg: TColorRg;',
  6605. 'begin',
  6606. ' b:=red in vt;',
  6607. ' b:=vc in vt;',
  6608. ' b:=green in [red..blue];',
  6609. ' b:=vc in [red..blue];',
  6610. ' ',
  6611. ' if red in vt then ;',
  6612. ' while vC in vt do ;',
  6613. ' repeat',
  6614. ' until vC in vt;',
  6615. ' if rg in [green..blue] then ;',
  6616. '']);
  6617. ConvertProgram;
  6618. CheckSource('TestSet_Operator_In',
  6619. LinesToStr([ // statements
  6620. 'this.TColor = {',
  6621. ' "0":"Red",',
  6622. ' Red:0,',
  6623. ' "1":"Green",',
  6624. ' Green:1,',
  6625. ' "2":"Blue",',
  6626. ' Blue:2',
  6627. ' };',
  6628. 'this.vC = 0;',
  6629. 'this.vT = {};',
  6630. 'this.B = false;',
  6631. 'this.rg = this.TColor.Green;',
  6632. '']),
  6633. LinesToStr([
  6634. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6635. '$mod.B = $mod.vC in $mod.vT;',
  6636. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6637. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6638. 'if ($mod.TColor.Red in $mod.vT) ;',
  6639. 'while ($mod.vC in $mod.vT) {',
  6640. '};',
  6641. 'do {',
  6642. '} while (!($mod.vC in $mod.vT));',
  6643. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6644. '']));
  6645. end;
  6646. procedure TTestModule.TestSet_Functions;
  6647. begin
  6648. StartProgram(false);
  6649. Add('type');
  6650. Add(' TMyEnum = (Red, Green);');
  6651. Add(' TMyEnums = set of TMyEnum;');
  6652. Add('var');
  6653. Add(' e: TMyEnum;');
  6654. Add(' s: TMyEnums;');
  6655. Add('begin');
  6656. Add(' e:=Low(TMyEnums);');
  6657. Add(' e:=Low(s);');
  6658. Add(' e:=High(TMyEnums);');
  6659. Add(' e:=High(s);');
  6660. ConvertProgram;
  6661. CheckSource('TestSetFunctions',
  6662. LinesToStr([ // statements
  6663. 'this.TMyEnum = {',
  6664. ' "0":"Red",',
  6665. ' Red:0,',
  6666. ' "1":"Green",',
  6667. ' Green:1',
  6668. ' };',
  6669. 'this.e = 0;',
  6670. 'this.s = {};'
  6671. ]),
  6672. LinesToStr([
  6673. '$mod.e=$mod.TMyEnum.Red;',
  6674. '$mod.e=$mod.TMyEnum.Red;',
  6675. '$mod.e=$mod.TMyEnum.Green;',
  6676. '$mod.e=$mod.TMyEnum.Green;',
  6677. '']));
  6678. end;
  6679. procedure TTestModule.TestSet_PassAsArgClone;
  6680. begin
  6681. StartProgram(false);
  6682. Add('type');
  6683. Add(' TMyEnum = (Red, Green);');
  6684. Add(' TMyEnums = set of TMyEnum;');
  6685. Add('procedure DoDefault(s: tmyenums); begin end;');
  6686. Add('procedure DoConst(const s: tmyenums); begin end;');
  6687. Add('var');
  6688. Add(' aSet: tmyenums;');
  6689. Add('begin');
  6690. Add(' dodefault(aset);');
  6691. Add(' doconst(aset);');
  6692. ConvertProgram;
  6693. CheckSource('TestSetFunctions',
  6694. LinesToStr([ // statements
  6695. 'this.TMyEnum = {',
  6696. ' "0":"Red",',
  6697. ' Red:0,',
  6698. ' "1":"Green",',
  6699. ' Green:1',
  6700. ' };',
  6701. 'this.DoDefault = function (s) {',
  6702. '};',
  6703. 'this.DoConst = function (s) {',
  6704. '};',
  6705. 'this.aSet = {};'
  6706. ]),
  6707. LinesToStr([
  6708. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6709. '$mod.DoConst($mod.aSet);',
  6710. '']));
  6711. end;
  6712. procedure TTestModule.TestSet_AsParams;
  6713. begin
  6714. StartProgram(false);
  6715. Add([
  6716. 'type TEnum = (Red,Blue);',
  6717. 'type TEnums = set of TEnum;',
  6718. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6719. 'var vJ: TEnums;',
  6720. 'begin',
  6721. ' Include(vg,red);',
  6722. ' Include(result,blue);',
  6723. ' vg:=vg;',
  6724. ' vj:=vh;',
  6725. ' vi:=vi;',
  6726. ' doit(vg,vg,vg);',
  6727. ' doit(vh,vh,vj);',
  6728. ' doit(vi,vi,vi);',
  6729. ' doit(vj,vj,vj);',
  6730. 'end;',
  6731. 'var i: TEnums;',
  6732. 'begin',
  6733. ' doit(i,i,i);']);
  6734. ConvertProgram;
  6735. CheckSource('TestSet_AsParams',
  6736. LinesToStr([ // statements
  6737. 'this.TEnum = {',
  6738. ' "0": "Red",',
  6739. ' Red: 0,',
  6740. ' "1": "Blue",',
  6741. ' Blue: 1',
  6742. '};',
  6743. 'this.DoIt = function (vG,vH,vI) {',
  6744. ' var Result = {};',
  6745. ' var vJ = {};',
  6746. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6747. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6748. ' vG = rtl.refSet(vG);',
  6749. ' vJ = rtl.refSet(vH);',
  6750. ' vI.set(rtl.refSet(vI.get()));',
  6751. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6752. ' get: function () {',
  6753. ' return vG;',
  6754. ' },',
  6755. ' set: function (v) {',
  6756. ' vG = v;',
  6757. ' }',
  6758. ' });',
  6759. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6760. ' get: function () {',
  6761. ' return vJ;',
  6762. ' },',
  6763. ' set: function (v) {',
  6764. ' vJ = v;',
  6765. ' }',
  6766. ' });',
  6767. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6768. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6769. ' get: function () {',
  6770. ' return vJ;',
  6771. ' },',
  6772. ' set: function (v) {',
  6773. ' vJ = v;',
  6774. ' }',
  6775. ' });',
  6776. ' return Result;',
  6777. '};',
  6778. 'this.i = {};'
  6779. ]),
  6780. LinesToStr([
  6781. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6782. ' p: $mod,',
  6783. ' get: function () {',
  6784. ' return this.p.i;',
  6785. ' },',
  6786. ' set: function (v) {',
  6787. ' this.p.i = v;',
  6788. ' }',
  6789. '});'
  6790. ]));
  6791. end;
  6792. procedure TTestModule.TestSet_Property;
  6793. begin
  6794. StartProgram(false);
  6795. Add('type');
  6796. Add(' TEnum = (Red,Blue);');
  6797. Add(' TEnums = set of TEnum;');
  6798. Add(' TObject = class');
  6799. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6800. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6801. Add(' property Colors: TEnums read GetColors write SetColors;');
  6802. Add(' end;');
  6803. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6804. Add('begin end;');
  6805. Add('var Obj: TObject;');
  6806. Add('begin');
  6807. Add(' Include(Obj.Colors,Red);');
  6808. Add(' Exclude(Obj.Colors,Red);');
  6809. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6810. ConvertProgram;
  6811. CheckSource('TestSet_Property',
  6812. LinesToStr([ // statements
  6813. 'this.TEnum = {',
  6814. ' "0": "Red",',
  6815. ' Red: 0,',
  6816. ' "1": "Blue",',
  6817. ' Blue: 1',
  6818. '};',
  6819. 'rtl.createClass(this, "TObject", null, function () {',
  6820. ' this.$init = function () {',
  6821. ' };',
  6822. ' this.$final = function () {',
  6823. ' };',
  6824. '});',
  6825. 'this.DoIt = function (i, j, k, l) {',
  6826. '};',
  6827. 'this.Obj = null;',
  6828. '']),
  6829. LinesToStr([
  6830. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6831. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6832. '']));
  6833. end;
  6834. procedure TTestModule.TestSet_EnumConst;
  6835. begin
  6836. StartProgram(false);
  6837. Add([
  6838. 'type',
  6839. ' TEnum = (Red,Blue);',
  6840. ' TEnums = set of TEnum;',
  6841. 'const',
  6842. ' Orange = red;',
  6843. 'var',
  6844. ' Enum: tenum;',
  6845. ' Enums: tenums;',
  6846. 'begin',
  6847. ' Include(enums,orange);',
  6848. ' Exclude(enums,orange);',
  6849. ' if orange in enums then;',
  6850. ' if orange in [orange,red] then;']);
  6851. ConvertProgram;
  6852. CheckSource('TestSet_EnumConst',
  6853. LinesToStr([ // statements
  6854. 'this.TEnum = {',
  6855. ' "0": "Red",',
  6856. ' Red: 0,',
  6857. ' "1": "Blue",',
  6858. ' Blue: 1',
  6859. '};',
  6860. 'this.Orange = this.TEnum.Red;',
  6861. 'this.Enum = 0;',
  6862. 'this.Enums = {};',
  6863. '']),
  6864. LinesToStr([
  6865. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6866. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6867. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6868. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6869. '']));
  6870. end;
  6871. procedure TTestModule.TestSet_IntConst;
  6872. begin
  6873. StartProgram(false);
  6874. Add([
  6875. 'type',
  6876. ' TEnums = set of Byte;',
  6877. 'const',
  6878. ' Orange = 0;',
  6879. 'var',
  6880. ' Enum: byte;',
  6881. ' Enums: tenums;',
  6882. 'begin',
  6883. ' Enum:=ord(1);',
  6884. ' Enums:=[];',
  6885. ' Enums:=[0];',
  6886. ' Enums:=[1..2];',
  6887. //' Include(enums,orange);',
  6888. //' Exclude(enums,orange);',
  6889. ' if orange in enums then;',
  6890. ' if orange in [orange,1] then;']);
  6891. ConvertProgram;
  6892. CheckSource('TestSet_IntConst',
  6893. LinesToStr([ // statements
  6894. 'this.Orange = 0;',
  6895. 'this.Enum = 0;',
  6896. 'this.Enums = {};',
  6897. '']),
  6898. LinesToStr([
  6899. '$mod.Enum = 1;',
  6900. '$mod.Enums = {};',
  6901. '$mod.Enums = rtl.createSet(0);',
  6902. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6903. 'if (0 in $mod.Enums) ;',
  6904. 'if (0 in rtl.createSet(0, 1)) ;',
  6905. '']));
  6906. end;
  6907. procedure TTestModule.TestSet_IntRange;
  6908. begin
  6909. StartProgram(false);
  6910. Add([
  6911. 'type',
  6912. ' TRange = 1..3;',
  6913. ' TEnums = set of TRange;',
  6914. 'const',
  6915. ' Orange = 2;',
  6916. 'var',
  6917. ' Enum: byte;',
  6918. ' Enums: TEnums;',
  6919. 'begin',
  6920. ' Enums:=[];',
  6921. ' Enums:=[1];',
  6922. ' Enums:=[2..3];',
  6923. ' Include(enums,orange);',
  6924. ' Exclude(enums,orange);',
  6925. ' if orange in enums then;',
  6926. ' if orange in [orange,1] then;']);
  6927. ConvertProgram;
  6928. CheckSource('TestSet_IntRange',
  6929. LinesToStr([ // statements
  6930. 'this.Orange = 2;',
  6931. 'this.Enum = 0;',
  6932. 'this.Enums = {};',
  6933. '']),
  6934. LinesToStr([
  6935. '$mod.Enums = {};',
  6936. '$mod.Enums = rtl.createSet(1);',
  6937. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6938. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6939. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6940. 'if (2 in $mod.Enums) ;',
  6941. 'if (2 in rtl.createSet(2, 1)) ;',
  6942. '']));
  6943. end;
  6944. procedure TTestModule.TestSet_AnonymousEnumType;
  6945. begin
  6946. StartProgram(false);
  6947. Add('type');
  6948. Add(' TFlags = set of (red, green);');
  6949. Add('const');
  6950. Add(' favorite = red;');
  6951. Add('var');
  6952. Add(' f: TFlags;');
  6953. Add(' i: longint;');
  6954. Add('begin');
  6955. Add(' Include(f,red);');
  6956. Add(' Include(f,favorite);');
  6957. Add(' i:=ord(red);');
  6958. Add(' i:=ord(favorite);');
  6959. Add(' i:=ord(low(TFlags));');
  6960. Add(' i:=ord(low(f));');
  6961. Add(' i:=ord(low(favorite));');
  6962. Add(' i:=ord(high(TFlags));');
  6963. Add(' i:=ord(high(f));');
  6964. Add(' i:=ord(high(favorite));');
  6965. Add(' f:=[green,favorite];');
  6966. ConvertProgram;
  6967. CheckSource('TestSet_AnonymousEnumType',
  6968. LinesToStr([ // statements
  6969. 'this.TFlags$a = {',
  6970. ' "0": "red",',
  6971. ' red: 0,',
  6972. ' "1": "green",',
  6973. ' green: 1',
  6974. '};',
  6975. 'this.favorite = this.TFlags$a.red;',
  6976. 'this.f = {};',
  6977. 'this.i = 0;',
  6978. '']),
  6979. LinesToStr([
  6980. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6981. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6982. '$mod.i = $mod.TFlags$a.red;',
  6983. '$mod.i = $mod.TFlags$a.red;',
  6984. '$mod.i = $mod.TFlags$a.red;',
  6985. '$mod.i = $mod.TFlags$a.red;',
  6986. '$mod.i = $mod.TFlags$a.red;',
  6987. '$mod.i = $mod.TFlags$a.green;',
  6988. '$mod.i = $mod.TFlags$a.green;',
  6989. '$mod.i = $mod.TFlags$a.green;',
  6990. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6991. '']));
  6992. end;
  6993. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6994. begin
  6995. exit;
  6996. StartProgram(false);
  6997. Add([
  6998. 'type',
  6999. ' TAtoZ = ''A''..''Z'';',
  7000. ' TSetOfAZ = set of TAtoZ;',
  7001. 'var',
  7002. ' c: char;',
  7003. ' a: TAtoZ;',
  7004. ' s: TSetOfAZ = [''P'',''A''];',
  7005. ' i: longint;',
  7006. 'begin',
  7007. ' Include(s,''S'');',
  7008. ' Include(s,c);',
  7009. ' Include(s,a);',
  7010. ' c:=low(TAtoZ);',
  7011. ' i:=ord(low(TAtoZ));',
  7012. ' a:=high(TAtoZ);',
  7013. ' a:=high(TSetOfAtoZ);',
  7014. ' s:=[a,c,''M''];',
  7015. '']);
  7016. ConvertProgram;
  7017. CheckSource('TestSet_AnonymousEnumTypeChar',
  7018. LinesToStr([ // statements
  7019. '']),
  7020. LinesToStr([
  7021. '']));
  7022. end;
  7023. procedure TTestModule.TestSet_ConstEnum;
  7024. begin
  7025. StartProgram(false);
  7026. Add([
  7027. 'type',
  7028. ' TEnum = (red,blue,green);',
  7029. ' TEnums = set of TEnum;',
  7030. 'const',
  7031. ' teAny = [low(TEnum)..high(TEnum)];',
  7032. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7033. 'var',
  7034. ' e: TEnum;',
  7035. ' s: TEnums;',
  7036. 'begin',
  7037. ' if blue in teAny then;',
  7038. ' if blue in teAny+[e] then;',
  7039. ' if blue in teAny+teRedBlue then;',
  7040. ' if e in [red,blue] then;',
  7041. ' s:=teAny;',
  7042. ' s:=teAny+[e];',
  7043. ' s:=[e]+teAny;',
  7044. ' s:=teAny+teRedBlue;',
  7045. ' s:=teAny+teRedBlue+[e];',
  7046. '']);
  7047. ConvertProgram;
  7048. CheckSource('TestSet_ConstEnum',
  7049. LinesToStr([ // statements
  7050. 'this.TEnum = {',
  7051. ' "0": "red",',
  7052. ' red: 0,',
  7053. ' "1": "blue",',
  7054. ' blue: 1,',
  7055. ' "2": "green",',
  7056. ' green: 2',
  7057. '};',
  7058. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7059. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7060. 'this.e = 0;',
  7061. 'this.s = {};',
  7062. '']),
  7063. LinesToStr([
  7064. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7065. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7066. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7067. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7068. '$mod.s = rtl.refSet($mod.teAny);',
  7069. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7070. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7071. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7072. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7073. '']));
  7074. end;
  7075. procedure TTestModule.TestSet_ConstChar;
  7076. begin
  7077. StartProgram(false);
  7078. Add([
  7079. 'const',
  7080. ' LowChars = [''a''..''z''];',
  7081. ' Chars = LowChars+[''A''..''Z''];',
  7082. ' sc = [''А'', ''Я''];',
  7083. 'var',
  7084. ' c: char;',
  7085. ' s: string;',
  7086. 'begin',
  7087. ' if c in lowchars then ;',
  7088. ' if ''a'' in lowchars then ;',
  7089. ' if s[1] in lowchars then ;',
  7090. ' if c in chars then ;',
  7091. ' if c in [''a''..''z'',''_''] then ;',
  7092. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7093. ' if ''Я'' in sc then ;',
  7094. ' if 3=ord('' '') then ;',
  7095. '']);
  7096. ConvertProgram;
  7097. CheckSource('TestSet_ConstChar',
  7098. LinesToStr([ // statements
  7099. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7100. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7101. 'this.sc = rtl.createSet(1040, 1071);',
  7102. 'this.c = "\x00";',
  7103. 'this.s = "";',
  7104. '']),
  7105. LinesToStr([
  7106. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7107. 'if (97 in $mod.LowChars) ;',
  7108. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7109. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7110. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7111. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7112. 'if (1071 in $mod.sc) ;',
  7113. 'if (3 === 32) ;',
  7114. '']));
  7115. end;
  7116. procedure TTestModule.TestSet_ConstInt;
  7117. begin
  7118. StartProgram(false);
  7119. Add([
  7120. 'const',
  7121. ' Months = [1..12];',
  7122. ' Mirror = [-12..-1]+Months;',
  7123. 'var',
  7124. ' i: smallint;',
  7125. 'begin',
  7126. ' if 3 in Months then;',
  7127. ' if i in Months+[i] then;',
  7128. ' if i in Months+Mirror then;',
  7129. ' if i in [4..6,8] then;',
  7130. '']);
  7131. ConvertProgram;
  7132. CheckSource('TestSet_ConstInt',
  7133. LinesToStr([ // statements
  7134. 'this.Months = rtl.createSet(null, 1, 12);',
  7135. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7136. 'this.i = 0;',
  7137. '']),
  7138. LinesToStr([
  7139. 'if (3 in $mod.Months) ;',
  7140. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7141. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7142. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7143. '']));
  7144. end;
  7145. procedure TTestModule.TestSet_InFunction;
  7146. begin
  7147. StartProgram(false);
  7148. Add([
  7149. 'const',
  7150. ' TEnum = 3;',
  7151. ' TSetOfEnum = 4;',
  7152. ' TSetOfAno = 5;',
  7153. 'procedure DoIt;',
  7154. 'type',
  7155. ' TEnum = (red, blue);',
  7156. ' TSetOfEnum = set of TEnum;',
  7157. ' TSetOfAno = set of (up,down);',
  7158. 'var',
  7159. ' e: TEnum;',
  7160. ' se: TSetOfEnum;',
  7161. ' sa: TSetOfAno;',
  7162. 'begin',
  7163. ' se:=[e];',
  7164. ' sa:=[up];',
  7165. 'end;',
  7166. 'begin',
  7167. '']);
  7168. ConvertProgram;
  7169. CheckSource('TestSet_InFunction',
  7170. LinesToStr([ // statements
  7171. 'this.TEnum = 3;',
  7172. 'this.TSetOfEnum = 4;',
  7173. 'this.TSetOfAno = 5;',
  7174. 'var TEnum$1 = {',
  7175. ' "0": "red",',
  7176. ' red: 0,',
  7177. ' "1": "blue",',
  7178. ' blue: 1',
  7179. '};',
  7180. 'var TSetOfAno$a = {',
  7181. ' "0": "up",',
  7182. ' up: 0,',
  7183. ' "1": "down",',
  7184. ' down: 1',
  7185. '};',
  7186. 'this.DoIt = function () {',
  7187. ' var e = 0;',
  7188. ' var se = {};',
  7189. ' var sa = {};',
  7190. ' se = rtl.createSet(e);',
  7191. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7192. '};',
  7193. '']),
  7194. LinesToStr([
  7195. '']));
  7196. end;
  7197. procedure TTestModule.TestSet_ForIn;
  7198. begin
  7199. StartProgram(false);
  7200. Add([
  7201. 'type',
  7202. ' TEnum = (Red, Green, Blue);',
  7203. ' TEnumRg = green..blue;',
  7204. ' TSetOfEnum = set of TEnum;',
  7205. ' TSetOfEnumRg = set of TEnumRg;',
  7206. 'var',
  7207. ' e, e2: TEnum;',
  7208. ' er: TEnum;',
  7209. ' s: TSetOfEnum;',
  7210. 'begin',
  7211. ' for e in TSetOfEnum do ;',
  7212. ' for e in TSetOfEnumRg do ;',
  7213. ' for e in [] do e2:=e;',
  7214. ' for e in [red..green] do e2:=e;',
  7215. ' for e in [green,blue] do e2:=e;',
  7216. ' for e in [red,blue] do e2:=e;',
  7217. ' for e in s do e2:=e;',
  7218. ' for er in TSetOfEnumRg do ;',
  7219. '']);
  7220. ConvertProgram;
  7221. CheckSource('TestSet_ForIn',
  7222. LinesToStr([ // statements
  7223. 'this.TEnum = {',
  7224. ' "0":"Red",',
  7225. ' Red:0,',
  7226. ' "1":"Green",',
  7227. ' Green:1,',
  7228. ' "2":"Blue",',
  7229. ' Blue:2',
  7230. ' };',
  7231. 'this.e = 0;',
  7232. 'this.e2 = 0;',
  7233. 'this.er = 0;',
  7234. 'this.s = {};',
  7235. '']),
  7236. LinesToStr([
  7237. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7238. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7239. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7240. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7241. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7242. 'for (var $l in $mod.s){',
  7243. ' $mod.e = +$l;',
  7244. ' $mod.e2 = $mod.e;',
  7245. '};',
  7246. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7247. '']));
  7248. end;
  7249. procedure TTestModule.TestNestBegin;
  7250. begin
  7251. StartProgram(false);
  7252. Add('begin');
  7253. Add(' begin');
  7254. Add(' begin');
  7255. Add(' end;');
  7256. Add(' begin');
  7257. Add(' if true then ;');
  7258. Add(' end;');
  7259. Add(' end;');
  7260. ConvertProgram;
  7261. CheckSource('TestNestBegin',
  7262. '',
  7263. 'if (true) ;');
  7264. end;
  7265. procedure TTestModule.TestUnitImplVars;
  7266. begin
  7267. StartUnit(false);
  7268. Add('interface');
  7269. Add('implementation');
  7270. Add('var');
  7271. Add(' V1:longint;');
  7272. Add(' V2:longint = 3;');
  7273. Add(' V3:string = ''abc'';');
  7274. ConvertUnit;
  7275. CheckSource('TestUnitImplVars',
  7276. LinesToStr([ // statements
  7277. 'var $impl = $mod.$impl;',
  7278. '']),
  7279. '', // this.$init
  7280. LinesToStr([ // implementation
  7281. '$impl.V1 = 0;',
  7282. '$impl.V2 = 3;',
  7283. '$impl.V3 = "abc";',
  7284. '']) );
  7285. end;
  7286. procedure TTestModule.TestUnitImplConsts;
  7287. begin
  7288. StartUnit(false);
  7289. Add('interface');
  7290. Add('implementation');
  7291. Add('const');
  7292. Add(' v1 = 3;');
  7293. Add(' v2:longint = 4;');
  7294. Add(' v3:string = ''abc'';');
  7295. ConvertUnit;
  7296. CheckSource('TestUnitImplConsts',
  7297. LinesToStr([ // statements
  7298. 'var $impl = $mod.$impl;',
  7299. '']),
  7300. '', // this.$init
  7301. LinesToStr([ // implementation
  7302. '$impl.v1 = 3;',
  7303. '$impl.v2 = 4;',
  7304. '$impl.v3 = "abc";',
  7305. '']) );
  7306. end;
  7307. procedure TTestModule.TestUnitImplRecord;
  7308. begin
  7309. StartUnit(false);
  7310. Add('interface');
  7311. Add('implementation');
  7312. Add('type');
  7313. Add(' TMyRecord = record');
  7314. Add(' i: longint;');
  7315. Add(' end;');
  7316. Add('var aRec: TMyRecord;');
  7317. Add('initialization');
  7318. Add(' arec.i:=3;');
  7319. ConvertUnit;
  7320. CheckSource('TestUnitImplRecord',
  7321. LinesToStr([ // statements
  7322. 'var $impl = $mod.$impl;',
  7323. '']),
  7324. // this.$init
  7325. '$impl.aRec.i = 3;',
  7326. LinesToStr([ // implementation
  7327. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7328. ' this.i = 0;',
  7329. ' this.$eq = function (b) {',
  7330. ' return this.i === b.i;',
  7331. ' };',
  7332. ' this.$assign = function (s) {',
  7333. ' this.i = s.i;',
  7334. ' return this;',
  7335. ' };',
  7336. '});',
  7337. '$impl.aRec = $impl.TMyRecord.$new();',
  7338. '']) );
  7339. end;
  7340. procedure TTestModule.TestRenameJSNameConflict;
  7341. begin
  7342. StartProgram(false);
  7343. Add('var apply: longint;');
  7344. Add('var bind: longint;');
  7345. Add('var call: longint;');
  7346. Add('begin');
  7347. ConvertProgram;
  7348. CheckSource('TestRenameJSNameConflict',
  7349. LinesToStr([ // statements
  7350. 'this.Apply = 0;',
  7351. 'this.Bind = 0;',
  7352. 'this.Call = 0;'
  7353. ]),
  7354. LinesToStr([ // this.$main
  7355. ''
  7356. ]));
  7357. end;
  7358. procedure TTestModule.TestLocalConst;
  7359. begin
  7360. StartProgram(false);
  7361. Add('procedure DoIt;');
  7362. Add('const');
  7363. Add(' cA: longint = 1;');
  7364. Add(' cB = 2;');
  7365. Add(' procedure Sub;');
  7366. Add(' const');
  7367. Add(' csA = 3;');
  7368. Add(' cB: double = 4;');
  7369. Add(' begin');
  7370. Add(' cb:=cb+csa;');
  7371. Add(' ca:=ca+csa+5;');
  7372. Add(' end;');
  7373. Add('begin');
  7374. Add(' ca:=ca+cb+6;');
  7375. Add('end;');
  7376. Add('begin');
  7377. ConvertProgram;
  7378. CheckSource('TestLocalConst',
  7379. LinesToStr([
  7380. 'var cA = 1;',
  7381. 'var cB = 2;',
  7382. 'var csA = 3;',
  7383. 'var cB$1 = 4;',
  7384. 'this.DoIt = function () {',
  7385. ' function Sub() {',
  7386. ' cB$1 = cB$1 + 3;',
  7387. ' cA = cA + 3 + 5;',
  7388. ' };',
  7389. ' cA = cA + 2 + 6;',
  7390. '};'
  7391. ]),
  7392. LinesToStr([
  7393. ]));
  7394. end;
  7395. procedure TTestModule.TestVarExternal;
  7396. begin
  7397. StartProgram(false);
  7398. Add('var');
  7399. Add(' NaN: double; external name ''Global.NaN'';');
  7400. Add(' d: double;');
  7401. Add('begin');
  7402. Add(' d:=NaN;');
  7403. ConvertProgram;
  7404. CheckSource('TestVarExternal',
  7405. LinesToStr([
  7406. 'this.d = 0.0;'
  7407. ]),
  7408. LinesToStr([
  7409. '$mod.d = Global.NaN;'
  7410. ]));
  7411. end;
  7412. procedure TTestModule.TestVarExternalOtherUnit;
  7413. begin
  7414. AddModuleWithIntfImplSrc('unit2.pas',
  7415. LinesToStr([
  7416. 'var NaN: double; external name ''Global.NaN'';',
  7417. 'var iV: longint;'
  7418. ]),
  7419. '');
  7420. StartUnit(true);
  7421. Add('interface');
  7422. Add('uses unit2;');
  7423. Add('implementation');
  7424. Add('var');
  7425. Add(' d: double;');
  7426. Add(' i: longint; external name ''$i'';');
  7427. Add('begin');
  7428. Add(' d:=nan;');
  7429. Add(' d:=uNit2.nan;');
  7430. Add(' d:=test1.d;');
  7431. Add(' i:=iv;');
  7432. Add(' i:=uNit2.iv;');
  7433. Add(' i:=test1.i;');
  7434. ConvertUnit;
  7435. CheckSource('TestVarExternalOtherUnit',
  7436. LinesToStr([
  7437. 'var $impl = $mod.$impl;',
  7438. '']),
  7439. LinesToStr([ // this.$init
  7440. '$impl.d = Global.NaN;',
  7441. '$impl.d = Global.NaN;',
  7442. '$impl.d = $impl.d;',
  7443. '$i = pas.unit2.iV;',
  7444. '$i = pas.unit2.iV;',
  7445. '$i = $i;',
  7446. '']),
  7447. LinesToStr([ // implementation
  7448. '$impl.d = 0.0;',
  7449. '']) );
  7450. end;
  7451. procedure TTestModule.TestVarAbsoluteFail;
  7452. begin
  7453. StartProgram(false);
  7454. Add([
  7455. 'var',
  7456. ' a: longint;',
  7457. ' b: longword absolute a;',
  7458. 'begin']);
  7459. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7460. ConvertProgram;
  7461. end;
  7462. procedure TTestModule.TestConstExternal;
  7463. begin
  7464. StartProgram(false);
  7465. Add([
  7466. 'const',
  7467. ' PI: double; external name ''Global.PI'';',
  7468. ' Tau = 2*pi;',
  7469. 'var d: double;',
  7470. 'begin',
  7471. ' d:=pi;',
  7472. ' d:=tau+pi;']);
  7473. ConvertProgram;
  7474. CheckSource('TestConstExternal',
  7475. LinesToStr([
  7476. 'this.Tau = 2*Global.PI;',
  7477. 'this.d = 0.0;'
  7478. ]),
  7479. LinesToStr([
  7480. '$mod.d = Global.PI;',
  7481. '$mod.d = $mod.Tau + Global.PI;'
  7482. ]));
  7483. end;
  7484. procedure TTestModule.TestDouble;
  7485. begin
  7486. StartProgram(false);
  7487. Add([
  7488. 'type',
  7489. ' TDateTime = double;',
  7490. 'const',
  7491. ' a = TDateTime(2.7);',
  7492. ' b = a + TDateTime(1.7);',
  7493. ' c = 0.9 + 0.1;',
  7494. ' f0_1 = 0.1;',
  7495. ' f0_3 = 0.3;',
  7496. ' fn0_1 = -0.1;',
  7497. ' fn0_3 = -0.3;',
  7498. ' fn0_003 = -0.003;',
  7499. ' fn0_123456789 = -0.123456789;',
  7500. ' fn300_0 = -300.0;',
  7501. ' fn123456_0 = -123456.0;',
  7502. ' fn1234567_8 = -1234567.8;',
  7503. ' fn12345678_9 = -12345678.9;',
  7504. ' f1_0En12 = 1E-12;',
  7505. ' fn1_0En12 = -1E-12;',
  7506. ' maxdouble = 1.7e+308;',
  7507. ' mindouble = -1.7e+308;',
  7508. ' MinSafeIntDouble = -$1fffffffffffff;',
  7509. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7510. ' MaxSafeIntDouble = $1fffffffffffff;',
  7511. ' DZeroResolution = 1E-12;',
  7512. ' Minus1 = -1E-12;',
  7513. ' EPS = 1E-9;',
  7514. ' DELTA = 0.001;',
  7515. ' Big = 129.789E+100;',
  7516. ' Test0_15 = 0.15;',
  7517. ' Test999 = 2.9999999999999;',
  7518. ' Test111999 = 211199999999999000.0;',
  7519. ' TestMinus111999 = -211199999999999000.0;',
  7520. ' Inf = 1.0 / 0.0;',
  7521. ' NegInf = -1.0 / 0.0;',
  7522. 'procedure Run(d: double); external name ''Run'';',
  7523. 'var',
  7524. ' d: double = b;',
  7525. 'begin',
  7526. ' d:=1.0;',
  7527. ' d:=1.0/3.0;',
  7528. ' d:=1.0/(3-2-1);',
  7529. ' d:=1/3;',
  7530. ' d:=5.0E-324;',
  7531. ' d:=1.7E308;',
  7532. ' d:=001.00E00;',
  7533. ' d:=002.00E001;',
  7534. ' d:=003.000E000;',
  7535. ' d:=-004.00E-00;',
  7536. ' d:=-005.00E-001;',
  7537. ' d:=10**3;',
  7538. ' d:=100*9**0.5;',
  7539. ' d:=10 mod 3;',
  7540. ' d:=10 div 3;',
  7541. ' d:=c;',
  7542. ' d:=f0_1;',
  7543. ' d:=f0_3;',
  7544. ' d:=fn0_1;',
  7545. ' d:=fn0_3;',
  7546. ' d:=fn0_003;',
  7547. ' d:=fn0_123456789;',
  7548. ' d:=fn300_0;',
  7549. ' d:=fn123456_0;',
  7550. ' d:=fn1234567_8;',
  7551. ' d:=fn12345678_9;',
  7552. ' d:=f1_0En12;',
  7553. ' d:=fn1_0En12;',
  7554. ' d:=maxdouble;',
  7555. ' d:=mindouble;',
  7556. ' d:=MinSafeIntDouble;',
  7557. ' d:=double(MinSafeIntDouble);',
  7558. ' d:=MinSafeIntDouble2;',
  7559. ' d:=double(MinSafeIntDouble2);',
  7560. ' d:=MaxSafeIntDouble;',
  7561. ' d:=default(double);',
  7562. ' Run(Inf);',
  7563. ' Run(NegInf);',
  7564. '']);
  7565. ConvertProgram;
  7566. CheckSource('TestDouble',
  7567. LinesToStr([
  7568. 'this.a = 2.7;',
  7569. 'this.b = 2.7 + 1.7;',
  7570. 'this.c = 0.9 + 0.1;',
  7571. 'this.f0_1 = 0.1;',
  7572. 'this.f0_3 = 0.3;',
  7573. 'this.fn0_1 = -0.1;',
  7574. 'this.fn0_3 = -0.3;',
  7575. 'this.fn0_003 = -0.003;',
  7576. 'this.fn0_123456789 = -0.123456789;',
  7577. 'this.fn300_0 = -300.0;',
  7578. 'this.fn123456_0 = -123456.0;',
  7579. 'this.fn1234567_8 = -1234567.8;',
  7580. 'this.fn12345678_9 = -12345678.9;',
  7581. 'this.f1_0En12 = 1E-12;',
  7582. 'this.fn1_0En12 = -1E-12;',
  7583. 'this.maxdouble = 1.7e+308;',
  7584. 'this.mindouble = -1.7e+308;',
  7585. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7586. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7587. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7588. 'this.DZeroResolution = 1E-12;',
  7589. 'this.Minus1 = -1E-12;',
  7590. 'this.EPS = 1E-9;',
  7591. 'this.DELTA = 0.001;',
  7592. 'this.Big = 129.789E+100;',
  7593. 'this.Test0_15 = 0.15;',
  7594. 'this.Test999 = 2.9999999999999;',
  7595. 'this.Test111999 = 211199999999999000.0;',
  7596. 'this.TestMinus111999 = -211199999999999000.0;',
  7597. 'this.Inf = 1.0 / 0.0;',
  7598. 'this.NegInf = -1.0 / 0.0;',
  7599. 'this.d = 4.4;',
  7600. '']),
  7601. LinesToStr([
  7602. '$mod.d = 1.0;',
  7603. '$mod.d = 1.0 / 3.0;',
  7604. '$mod.d = 1.0 / (3 - 2 - 1);',
  7605. '$mod.d = 1 / 3;',
  7606. '$mod.d = 5.0E-324;',
  7607. '$mod.d = 1.7E308;',
  7608. '$mod.d = 1.00E0;',
  7609. '$mod.d = 2.00E1;',
  7610. '$mod.d = 3.000E0;',
  7611. '$mod.d = -4.00E-0;',
  7612. '$mod.d = -5.00E-1;',
  7613. '$mod.d = 10 ** 3;',
  7614. '$mod.d = 100 * (9 ** 0.5);',
  7615. '$mod.d = 10 % 3;',
  7616. '$mod.d = rtl.trunc(10 / 3);',
  7617. '$mod.d = 1;',
  7618. '$mod.d = 0.1;',
  7619. '$mod.d = 0.3;',
  7620. '$mod.d = -0.1;',
  7621. '$mod.d = -0.3;',
  7622. '$mod.d = -3E-3;',
  7623. '$mod.d = -0.123456789;',
  7624. '$mod.d = -300;',
  7625. '$mod.d = -123456;',
  7626. '$mod.d = -1234567.8;',
  7627. '$mod.d = -1.23456789E7;',
  7628. '$mod.d = 1E-12;',
  7629. '$mod.d = -1E-12;',
  7630. '$mod.d = 1.7E308;',
  7631. '$mod.d = -1.7E308;',
  7632. '$mod.d = -9007199254740991;',
  7633. '$mod.d = -9007199254740991;',
  7634. '$mod.d = -9.007199254740992E15;',
  7635. '$mod.d = -9.007199254740992E15;',
  7636. '$mod.d = 9007199254740991;',
  7637. '$mod.d = 0.0;',
  7638. 'Run(1 / 0);',
  7639. 'Run(-1 / 0);',
  7640. '']));
  7641. end;
  7642. procedure TTestModule.TestDoubleSmall;
  7643. begin
  7644. StartProgram(false);
  7645. Add([
  7646. 'const',
  7647. ' a = 1e-1;',
  7648. ' b = 1e-2;',
  7649. ' c = 1e-3;',
  7650. ' d = 1e-4;',
  7651. ' e = 1e-5;',
  7652. ' f = 1e-6;',
  7653. ' g = 1e-7;',
  7654. ' h = -1e-1;',
  7655. ' i = -1e-2;',
  7656. 'procedure Fly(d: double);',
  7657. 'begin',
  7658. 'end;',
  7659. 'begin',
  7660. ' Fly(a);',
  7661. ' Fly(b);',
  7662. ' Fly(c);',
  7663. ' Fly(d);',
  7664. ' Fly(e);',
  7665. ' Fly(f);',
  7666. ' Fly(g);',
  7667. ' Fly(h);',
  7668. ' Fly(i);',
  7669. '']);
  7670. ConvertProgram;
  7671. CheckSource('TestDoubleSmall',
  7672. LinesToStr([
  7673. 'this.a = 1e-1;',
  7674. 'this.b = 1e-2;',
  7675. 'this.c = 1e-3;',
  7676. 'this.d = 1e-4;',
  7677. 'this.e = 1e-5;',
  7678. 'this.f = 1e-6;',
  7679. 'this.g = 1e-7;',
  7680. 'this.h = -1e-1;',
  7681. 'this.i = -1e-2;',
  7682. 'this.Fly = function (d) {',
  7683. '};',
  7684. '']),
  7685. LinesToStr([
  7686. '$mod.Fly(0.1);',
  7687. '$mod.Fly(0.01);',
  7688. '$mod.Fly(1E-3);',
  7689. '$mod.Fly(1E-4);',
  7690. '$mod.Fly(1E-5);',
  7691. '$mod.Fly(1E-6);',
  7692. '$mod.Fly(1E-7);',
  7693. '$mod.Fly(-0.1);',
  7694. '$mod.Fly(-0.01);',
  7695. '']));
  7696. end;
  7697. procedure TTestModule.TestInteger;
  7698. begin
  7699. StartProgram(false);
  7700. Add([
  7701. 'const',
  7702. ' MinInt = low(NativeInt);',
  7703. ' MaxInt = high(NativeInt);',
  7704. 'type',
  7705. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7706. 'const',
  7707. ' a = low(TMyInt)+High(TMyInt);',
  7708. 'var',
  7709. ' i: TMyInt;',
  7710. 'begin',
  7711. ' i:=-MinInt;',
  7712. ' i:=default(TMyInt);',
  7713. ' i:=low(i)+high(i);',
  7714. '']);
  7715. ConvertProgram;
  7716. CheckSource('TestIntegerRange',
  7717. LinesToStr([
  7718. 'this.MinInt = -9007199254740991;',
  7719. 'this.MaxInt = 9007199254740991;',
  7720. 'this.a = -9007199254740991 + 9007199254740991;',
  7721. 'this.i = 0;',
  7722. '']),
  7723. LinesToStr([
  7724. '$mod.i = - -9007199254740991;',
  7725. '$mod.i = -9007199254740991;',
  7726. '$mod.i = -9007199254740991 + 9007199254740991;',
  7727. '']));
  7728. end;
  7729. procedure TTestModule.TestIntegerRange;
  7730. begin
  7731. StartProgram(false);
  7732. Add([
  7733. 'const',
  7734. ' MinInt = -1;',
  7735. ' MaxInt = +1;',
  7736. 'type',
  7737. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7738. ' TInt2 = 1..3;',
  7739. 'const',
  7740. ' a = low(TMyInt)+High(TMyInt);',
  7741. ' b = low(TInt2)+High(TInt2);',
  7742. ' s1 = [1];',
  7743. ' s2 = [1,2];',
  7744. ' s3 = [1..3];',
  7745. ' s4 = [low(shortint)..high(shortint)];',
  7746. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7747. ' s6 = 1 in s2;',
  7748. 'var',
  7749. ' i: TMyInt;',
  7750. ' i2: TInt2;',
  7751. 'begin',
  7752. ' i:=i2;',
  7753. ' i:=default(TMyInt);',
  7754. ' if i=i2 then ;',
  7755. ' i:=ord(i2);',
  7756. '']);
  7757. ConvertProgram;
  7758. CheckSource('TestIntegerRange',
  7759. LinesToStr([
  7760. 'this.MinInt = -1;',
  7761. 'this.MaxInt = +1;',
  7762. 'this.a = -1 + 1;',
  7763. 'this.b = 1 + 3;',
  7764. 'this.s1 = rtl.createSet(1);',
  7765. 'this.s2 = rtl.createSet(1, 2);',
  7766. 'this.s3 = rtl.createSet(null, 1, 3);',
  7767. 'this.s4 = rtl.createSet(null, -128, 127);',
  7768. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7769. 'this.s6 = 1 in this.s2;',
  7770. 'this.i = 0;',
  7771. 'this.i2 = 0;',
  7772. '']),
  7773. LinesToStr([
  7774. '$mod.i = $mod.i2;',
  7775. '$mod.i = -1;',
  7776. 'if ($mod.i === $mod.i2) ;',
  7777. '$mod.i = $mod.i2;',
  7778. '']));
  7779. end;
  7780. procedure TTestModule.TestIntegerTypecasts;
  7781. begin
  7782. StartProgram(false);
  7783. Add([
  7784. 'var',
  7785. ' i: nativeint;',
  7786. ' b: byte;',
  7787. ' sh: shortint;',
  7788. ' w: word;',
  7789. ' sm: smallint;',
  7790. ' lw: longword;',
  7791. ' li: longint;',
  7792. 'begin',
  7793. ' b:=byte(i);',
  7794. ' sh:=shortint(i);',
  7795. ' w:=word(i);',
  7796. ' sm:=smallint(i);',
  7797. ' lw:=longword(i);',
  7798. ' li:=longint(i);',
  7799. '']);
  7800. ConvertProgram;
  7801. CheckSource('TestIntegerTypecasts',
  7802. LinesToStr([
  7803. 'this.i = 0;',
  7804. 'this.b = 0;',
  7805. 'this.sh = 0;',
  7806. 'this.w = 0;',
  7807. 'this.sm = 0;',
  7808. 'this.lw = 0;',
  7809. 'this.li = 0;',
  7810. '']),
  7811. LinesToStr([
  7812. '$mod.b = $mod.i & 255;',
  7813. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7814. '$mod.w = $mod.i & 65535;',
  7815. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7816. '$mod.lw = $mod.i >>> 0;',
  7817. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7818. '']));
  7819. end;
  7820. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7821. begin
  7822. StartProgram(false);
  7823. Add([
  7824. 'var',
  7825. ' i,j: nativeint;',
  7826. 'begin',
  7827. ' i:=i shr 0;',
  7828. ' i:=i shr 1;',
  7829. ' i:=i shr 3;',
  7830. ' i:=i shr 54;',
  7831. ' i:=j shr i;',
  7832. '']);
  7833. ConvertProgram;
  7834. CheckResolverUnexpectedHints;
  7835. CheckSource('TestInteger_BitwiseShrNativeInt',
  7836. LinesToStr([
  7837. 'this.i = 0;',
  7838. 'this.j = 0;',
  7839. '']),
  7840. LinesToStr([
  7841. '$mod.i = $mod.i;',
  7842. '$mod.i = Math.floor($mod.i / 2);',
  7843. '$mod.i = Math.floor($mod.i / 8);',
  7844. '$mod.i = 0;',
  7845. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7846. '']));
  7847. end;
  7848. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7849. begin
  7850. StartProgram(false);
  7851. Add([
  7852. 'var',
  7853. ' i: nativeint;',
  7854. 'begin',
  7855. ' i:=i shl 0;',
  7856. ' i:=i shl 54;',
  7857. ' i:=123456789012 shl 1;',
  7858. ' i:=i shl 1;',
  7859. '']);
  7860. ConvertProgram;
  7861. CheckResolverUnexpectedHints;
  7862. CheckSource('TestInteger_BitwiseShrNativeInt',
  7863. LinesToStr([
  7864. 'this.i = 0;',
  7865. '']),
  7866. LinesToStr([
  7867. '$mod.i = $mod.i;',
  7868. '$mod.i = 0;',
  7869. '$mod.i = 246913578024;',
  7870. '$mod.i = rtl.shl($mod.i, 1);',
  7871. '']));
  7872. end;
  7873. procedure TTestModule.TestInteger_SystemFunc;
  7874. begin
  7875. StartProgram(true);
  7876. Add([
  7877. 'var',
  7878. ' i: byte;',
  7879. ' s: string;',
  7880. 'begin',
  7881. ' system.inc(i);',
  7882. ' system.str(i,s);',
  7883. ' s:=system.str(i);',
  7884. ' i:=system.low(i);',
  7885. ' i:=system.high(i);',
  7886. ' i:=system.pred(i);',
  7887. ' i:=system.succ(i);',
  7888. ' i:=system.ord(i);',
  7889. '']);
  7890. ConvertProgram;
  7891. CheckResolverUnexpectedHints;
  7892. CheckSource('TestInteger_SystemFunc',
  7893. LinesToStr([
  7894. 'this.i = 0;',
  7895. 'this.s = "";',
  7896. '']),
  7897. LinesToStr([
  7898. '$mod.i += 1;',
  7899. '$mod.s = "" + $mod.i;',
  7900. '$mod.s = "" + $mod.i;',
  7901. '$mod.i = 0;',
  7902. '$mod.i = 255;',
  7903. '$mod.i = $mod.i - 1;',
  7904. '$mod.i = $mod.i + 1;',
  7905. '$mod.i = $mod.i;',
  7906. '']));
  7907. end;
  7908. procedure TTestModule.TestInteger_AssignOutsideConst;
  7909. begin
  7910. StartProgram(false);
  7911. Add([
  7912. 'const',
  7913. ' MinInt = low(longint);',
  7914. ' MaxInt = high(longint);',
  7915. 'type',
  7916. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7917. 'var',
  7918. ' i: TMyInt;',
  7919. ' aByte: byte;',
  7920. ' aShortInt: shortint;',
  7921. ' aWord: word;',
  7922. ' aSmallInt: smallint;',
  7923. ' aLongWord: longword;',
  7924. ' aLongInt: longint;',
  7925. ' aNativeInt: nativeint;',
  7926. ' aNativeUInt: nativeuint;',
  7927. 'begin',
  7928. ' aByte:=$FF;',
  7929. ' aByte:=$100;',
  7930. ' aByte:=-1;',
  7931. ' aByte:=-127;',
  7932. ' aByte:=-128;',
  7933. ' aByte:=-254;',
  7934. ' aByte:=-255;',
  7935. ' aByte:=-256;',
  7936. ' aShortInt:=127;',
  7937. ' aShortInt:=128;',
  7938. ' aShortInt:=-128;',
  7939. ' aShortInt:=-129;',
  7940. ' aWord:=$ffff;',
  7941. ' aWord:=$10000;',
  7942. ' aWord:=-1;',
  7943. ' aWord:=-$ffff;',
  7944. ' aWord:=-$10000;',
  7945. ' aWord:=-$10001;',
  7946. ' aSmallInt:=$7fff;',
  7947. ' aSmallInt:=$8000;',
  7948. ' aSmallInt:=-$8000;',
  7949. ' aSmallInt:=-$8001;',
  7950. ' aLongWord:=$ffffffff;',
  7951. ' aLongWord:=$100000000;',
  7952. ' aLongWord:=-1;',
  7953. ' aLongWord:=-$ffffffff;',
  7954. ' aNativeInt:=$1fffffffffffff;',
  7955. ' aNativeInt:=-$1fffffffffffff;',
  7956. ' aNativeUInt:=$1fffffffffffff;',
  7957. ' aNativeUInt:=-$1fffffffffffff;',
  7958. '']);
  7959. ConvertProgram;
  7960. CheckSource('TestInteger_AssignOutsideConst',
  7961. LinesToStr([
  7962. 'this.MinInt = -2147483648;',
  7963. 'this.MaxInt = 2147483647;',
  7964. 'this.i = 0;',
  7965. 'this.aByte = 0;',
  7966. 'this.aShortInt = 0;',
  7967. 'this.aWord = 0;',
  7968. 'this.aSmallInt = 0;',
  7969. 'this.aLongWord = 0;',
  7970. 'this.aLongInt = 0;',
  7971. 'this.aNativeInt = 0;',
  7972. 'this.aNativeUInt = 0;',
  7973. '']),
  7974. LinesToStr([
  7975. '$mod.aByte = 0xFF;',
  7976. '$mod.aByte = 0;',
  7977. '$mod.aByte = 255;',
  7978. '$mod.aByte = 129;',
  7979. '$mod.aByte = 128;',
  7980. '$mod.aByte = 2;',
  7981. '$mod.aByte = 1;',
  7982. '$mod.aByte = 0;',
  7983. '$mod.aShortInt = 127;',
  7984. '$mod.aShortInt = -128;',
  7985. '$mod.aShortInt = -128;',
  7986. '$mod.aShortInt = 127;',
  7987. '$mod.aWord = 0xffff;',
  7988. '$mod.aWord = 0;',
  7989. '$mod.aWord = 65535;',
  7990. '$mod.aWord = 1;',
  7991. '$mod.aWord = 0;',
  7992. '$mod.aWord = 65535;',
  7993. '$mod.aSmallInt = 0x7fff;',
  7994. '$mod.aSmallInt = -32768;',
  7995. '$mod.aSmallInt = -0x8000;',
  7996. '$mod.aSmallInt = 32767;',
  7997. '$mod.aLongWord = 0xffffffff;',
  7998. '$mod.aLongWord = 0;',
  7999. '$mod.aLongWord = 4294967295;',
  8000. '$mod.aLongWord = 1;',
  8001. '$mod.aNativeInt = 0x1fffffffffffff;',
  8002. '$mod.aNativeInt = -0x1fffffffffffff;',
  8003. '$mod.aNativeUInt = 0x1fffffffffffff;',
  8004. '$mod.aNativeUInt = 1;',
  8005. '']));
  8006. end;
  8007. procedure TTestModule.TestCurrency;
  8008. begin
  8009. StartProgram(false);
  8010. Add([
  8011. 'type',
  8012. ' TCoin = currency;',
  8013. 'const',
  8014. ' a = TCoin(2.7);',
  8015. ' b = a + TCoin(1.7);',
  8016. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8017. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8018. 'var',
  8019. ' c: TCoin = b;',
  8020. ' i: nativeint;',
  8021. ' d: double;',
  8022. ' j: jsvalue;',
  8023. 'function DoIt(c: currency): currency; begin end;',
  8024. 'function GetIt(d: double): double; begin end;',
  8025. 'procedure Write(v: jsvalue); begin end;',
  8026. 'begin',
  8027. ' c:=1.0;',
  8028. ' c:=0.1;',
  8029. ' c:=1.0/3.0;',
  8030. ' c:=1/3;',
  8031. ' c:=a;',
  8032. ' d:=c;',
  8033. ' c:=d;',
  8034. ' c:=currency(c);',
  8035. ' c:=currency(d);',
  8036. ' d:=double(c);',
  8037. ' c:=i;',
  8038. ' c:=currency(i);',
  8039. //' i:=c;', not allowed
  8040. ' i:=nativeint(c);',
  8041. ' c:=c+a;',
  8042. ' c:=-c-a;',
  8043. ' c:=d+c;',
  8044. ' c:=c+d;',
  8045. ' c:=d-c;',
  8046. ' c:=c-d;',
  8047. ' c:=c*a;',
  8048. ' c:=a*c;',
  8049. ' c:=d*c;',
  8050. ' c:=c*d;',
  8051. ' c:=c/a;',
  8052. ' c:=a/c;',
  8053. ' c:=d/c;',
  8054. ' c:=c/d;',
  8055. ' c:=c**a;',
  8056. ' c:=a**c;',
  8057. ' c:=d**c;',
  8058. ' c:=c**d;',
  8059. ' if c=c then ;',
  8060. ' if c=a then ;',
  8061. ' if a=c then ;',
  8062. ' if d=c then ;',
  8063. ' if c=d then ;',
  8064. ' c:=DoIt(c);',
  8065. ' c:=DoIt(i);',
  8066. ' c:=DoIt(d);',
  8067. ' c:=GetIt(c);',
  8068. ' j:=c;',
  8069. ' Write(c);',
  8070. ' c:=default(currency);',
  8071. ' j:=str(c);',
  8072. ' j:=str(c:0:3);',
  8073. '']);
  8074. ConvertProgram;
  8075. CheckSource('TestCurrency',
  8076. LinesToStr([
  8077. 'this.a = 27000;',
  8078. 'this.b = this.a + 17000;',
  8079. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8080. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8081. 'this.c = this.b;',
  8082. 'this.i = 0;',
  8083. 'this.d = 0.0;',
  8084. 'this.j = undefined;',
  8085. 'this.DoIt = function (c) {',
  8086. ' var Result = 0;',
  8087. ' return Result;',
  8088. '};',
  8089. 'this.GetIt = function (d) {',
  8090. ' var Result = 0.0;',
  8091. ' return Result;',
  8092. '};',
  8093. 'this.Write = function (v) {',
  8094. '};',
  8095. '']),
  8096. LinesToStr([
  8097. '$mod.c = 10000;',
  8098. '$mod.c = 1000;',
  8099. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8100. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8101. '$mod.c = $mod.a;',
  8102. '$mod.d = $mod.c / 10000;',
  8103. '$mod.c = rtl.trunc($mod.d * 10000);',
  8104. '$mod.c = $mod.c;',
  8105. '$mod.c = $mod.d * 10000;',
  8106. '$mod.d = $mod.c / 10000;',
  8107. '$mod.c = $mod.i * 10000;',
  8108. '$mod.c = $mod.i * 10000;',
  8109. '$mod.i = rtl.trunc($mod.c / 10000);',
  8110. '$mod.c = $mod.c + $mod.a;',
  8111. '$mod.c = -$mod.c - $mod.a;',
  8112. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8113. '$mod.c = $mod.c + ($mod.d * 10000);',
  8114. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8115. '$mod.c = $mod.c - ($mod.d * 10000);',
  8116. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8117. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8118. '$mod.c = $mod.d * $mod.c;',
  8119. '$mod.c = $mod.c * $mod.d;',
  8120. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8121. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8122. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8123. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8124. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8125. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8126. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8127. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8128. 'if ($mod.c === $mod.c) ;',
  8129. 'if ($mod.c === $mod.a) ;',
  8130. 'if ($mod.a === $mod.c) ;',
  8131. 'if (($mod.d * 10000) === $mod.c) ;',
  8132. 'if ($mod.c === ($mod.d * 10000)) ;',
  8133. '$mod.c = $mod.DoIt($mod.c);',
  8134. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8135. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8136. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8137. '$mod.j = $mod.c / 10000;',
  8138. '$mod.Write($mod.c / 10000);',
  8139. '$mod.c = 0;',
  8140. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8141. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8142. '']));
  8143. end;
  8144. procedure TTestModule.TestForBoolDo;
  8145. begin
  8146. StartProgram(false);
  8147. Add([
  8148. 'var b: boolean;',
  8149. 'begin',
  8150. ' for b:=false to true do ;',
  8151. ' for b:=b downto false do ;',
  8152. ' for b in boolean do ;',
  8153. '']);
  8154. ConvertProgram;
  8155. CheckSource('TestForBoolDo',
  8156. LinesToStr([ // statements
  8157. 'this.b = false;']),
  8158. LinesToStr([ // this.$main
  8159. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8160. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8161. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8162. '']));
  8163. end;
  8164. procedure TTestModule.TestForIntDo;
  8165. begin
  8166. StartProgram(false);
  8167. Add([
  8168. 'var i: longint;',
  8169. 'begin',
  8170. ' for i:=3 to 5 do ;',
  8171. ' for i:=i downto 2 do ;',
  8172. ' for i in byte do ;',
  8173. '']);
  8174. ConvertProgram;
  8175. CheckSource('TestForIntDo',
  8176. LinesToStr([ // statements
  8177. 'this.i = 0;']),
  8178. LinesToStr([ // this.$main
  8179. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8180. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8181. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8182. '']));
  8183. end;
  8184. procedure TTestModule.TestForIntInDo;
  8185. begin
  8186. StartProgram(false);
  8187. Add([
  8188. 'type',
  8189. ' TSetOfInt = set of byte;',
  8190. ' TIntRg = 3..7;',
  8191. ' TSetOfIntRg = set of TIntRg;',
  8192. 'var',
  8193. ' i,i2: longint;',
  8194. ' a1: array of byte;',
  8195. ' a2: array[1..3] of byte;',
  8196. ' soi: TSetOfInt;',
  8197. ' soir: TSetOfIntRg;',
  8198. ' ir: TIntRg;',
  8199. 'begin',
  8200. ' for i in byte do ;',
  8201. ' for i in a1 do ;',
  8202. ' for i in a2 do ;',
  8203. ' for i in [11..13] do ;',
  8204. ' for i in TSetOfInt do ;',
  8205. ' for i in TIntRg do ;',
  8206. ' for i in soi do i2:=i;',
  8207. ' for i in TSetOfIntRg do ;',
  8208. ' for i in soir do ;',
  8209. ' for ir in TIntRg do ;',
  8210. ' for ir in TSetOfIntRg do ;',
  8211. ' for ir in soir do ;',
  8212. '']);
  8213. ConvertProgram;
  8214. CheckSource('TestForIntInDo',
  8215. LinesToStr([ // statements
  8216. 'this.i = 0;',
  8217. 'this.i2 = 0;',
  8218. 'this.a1 = [];',
  8219. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8220. 'this.soi = {};',
  8221. 'this.soir = {};',
  8222. 'this.ir = 0;',
  8223. '']),
  8224. LinesToStr([ // this.$main
  8225. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8226. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8227. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8228. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8229. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8230. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8231. 'for (var $l6 in $mod.soi) {',
  8232. ' $mod.i = +$l6;',
  8233. ' $mod.i2 = $mod.i;',
  8234. '};',
  8235. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8236. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8237. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8238. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8239. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8240. '']));
  8241. end;
  8242. procedure TTestModule.TestCharConst;
  8243. begin
  8244. StartProgram(false);
  8245. Add([
  8246. 'const',
  8247. ' a = #$00F3;',
  8248. ' c: char = ''1'';',
  8249. ' wc: widechar = ''ä'';',
  8250. 'begin',
  8251. ' c:=#0;',
  8252. ' c:=#1;',
  8253. ' c:=#9;',
  8254. ' c:=#10;',
  8255. ' c:=#13;',
  8256. ' c:=#31;',
  8257. ' c:=#32;',
  8258. ' c:=#$A;',
  8259. ' c:=#$0A;',
  8260. ' c:=#$b;',
  8261. ' c:=#$0b;',
  8262. ' c:=^A;',
  8263. ' c:=''"'';',
  8264. ' c:=default(char);',
  8265. ' c:=#$00E4;', // ä
  8266. ' c:=''ä'';',
  8267. ' c:=#$E4;', // ä
  8268. ' c:=#$D800;', // invalid UTF-16
  8269. ' c:=#$DFFF;', // invalid UTF-16
  8270. ' c:=#$FFFF;', // last UCS-2
  8271. ' c:=high(c);', // last UCS-2
  8272. ' c:=#269;',
  8273. '']);
  8274. ConvertProgram;
  8275. CheckSource('TestCharConst',
  8276. LinesToStr([
  8277. 'this.a="ó";',
  8278. 'this.c="1";',
  8279. 'this.wc="ä";'
  8280. ]),
  8281. LinesToStr([
  8282. '$mod.c="\x00";',
  8283. '$mod.c="\x01";',
  8284. '$mod.c="\t";',
  8285. '$mod.c="\n";',
  8286. '$mod.c="\r";',
  8287. '$mod.c="\x1F";',
  8288. '$mod.c=" ";',
  8289. '$mod.c="\n";',
  8290. '$mod.c="\n";',
  8291. '$mod.c="\x0B";',
  8292. '$mod.c="\x0B";',
  8293. '$mod.c="\x01";',
  8294. '$mod.c=''"'';',
  8295. '$mod.c="\x00";',
  8296. '$mod.c = "ä";',
  8297. '$mod.c = "ä";',
  8298. '$mod.c = "ä";',
  8299. '$mod.c="\uD800";',
  8300. '$mod.c="\uDFFF";',
  8301. '$mod.c="\uFFFF";',
  8302. '$mod.c="\uFFFF";',
  8303. '$mod.c = "č";',
  8304. '']));
  8305. end;
  8306. procedure TTestModule.TestChar_Compare;
  8307. begin
  8308. StartProgram(false);
  8309. Add('var');
  8310. Add(' c: char;');
  8311. Add(' b: boolean;');
  8312. Add('begin');
  8313. Add(' b:=c=''1'';');
  8314. Add(' b:=''2''=c;');
  8315. Add(' b:=''3''=''4'';');
  8316. Add(' b:=c<>''5'';');
  8317. Add(' b:=''6''<>c;');
  8318. Add(' b:=c>''7'';');
  8319. Add(' b:=''8''>c;');
  8320. Add(' b:=c>=''9'';');
  8321. Add(' b:=''A''>=c;');
  8322. Add(' b:=c<''B'';');
  8323. Add(' b:=''C''<c;');
  8324. Add(' b:=c<=''D'';');
  8325. Add(' b:=''E''<=c;');
  8326. ConvertProgram;
  8327. CheckSource('TestChar_Compare',
  8328. LinesToStr([
  8329. 'this.c = "\x00";',
  8330. 'this.b = false;'
  8331. ]),
  8332. LinesToStr([
  8333. '$mod.b = $mod.c === "1";',
  8334. '$mod.b = "2" === $mod.c;',
  8335. '$mod.b = "3" === "4";',
  8336. '$mod.b = $mod.c !== "5";',
  8337. '$mod.b = "6" !== $mod.c;',
  8338. '$mod.b = $mod.c > "7";',
  8339. '$mod.b = "8" > $mod.c;',
  8340. '$mod.b = $mod.c >= "9";',
  8341. '$mod.b = "A" >= $mod.c;',
  8342. '$mod.b = $mod.c < "B";',
  8343. '$mod.b = "C" < $mod.c;',
  8344. '$mod.b = $mod.c <= "D";',
  8345. '$mod.b = "E" <= $mod.c;',
  8346. '']));
  8347. end;
  8348. procedure TTestModule.TestChar_BuiltInProcs;
  8349. begin
  8350. StartProgram(false);
  8351. Add([
  8352. 'var',
  8353. ' c: char;',
  8354. ' i: longint;',
  8355. ' s: string;',
  8356. 'begin',
  8357. ' i:=ord(c);',
  8358. ' i:=ord(s[i]);',
  8359. ' c:=chr(i);',
  8360. ' c:=pred(c);',
  8361. ' c:=succ(c);',
  8362. ' c:=low(c);',
  8363. ' c:=high(c);',
  8364. ' i:=byte(c);',
  8365. ' i:=word(c);',
  8366. ' i:=longint(c);',
  8367. '']);
  8368. ConvertProgram;
  8369. CheckSource('TestChar_BuiltInProcs',
  8370. LinesToStr([
  8371. 'this.c = "\x00";',
  8372. 'this.i = 0;',
  8373. 'this.s = "";'
  8374. ]),
  8375. LinesToStr([
  8376. '$mod.i = $mod.c.charCodeAt();',
  8377. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8378. '$mod.c = String.fromCharCode($mod.i);',
  8379. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8380. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8381. '$mod.c = "\x00";',
  8382. '$mod.c = "\uFFFF";',
  8383. '$mod.i = $mod.c.charCodeAt() & 255;',
  8384. '$mod.i = $mod.c.charCodeAt();',
  8385. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8386. '']));
  8387. end;
  8388. procedure TTestModule.TestStringConst;
  8389. begin
  8390. StartProgram(false);
  8391. Add([
  8392. '{$H+}',
  8393. 'const',
  8394. ' a = #$00F3#$017C;', // first <256, then >=256
  8395. ' b = string(''a'');',
  8396. ' c = string(''ä'');',
  8397. ' d = UnicodeString(''b'');',
  8398. ' e = UnicodeString(''ö'');',
  8399. ' f = low(a)+high(b);',
  8400. ' g: word = low(a);',
  8401. 'var',
  8402. ' s: string = ''abc'';',
  8403. ' i: longint;',
  8404. 'begin',
  8405. ' s:='''';',
  8406. ' s:=#13#10;',
  8407. ' s:=#9''foo'';',
  8408. ' s:=#$A9;',
  8409. ' s:=''foo''#13''bar'';',
  8410. ' s:=''"'';',
  8411. ' s:=''"''''"'';',
  8412. ' s:=#$20AC;', // euro
  8413. ' s:=#$10437;', // outside BMP
  8414. ' s:=''abc''#$20AC;', // ascii,#
  8415. ' s:=''ä''#$20AC;', // non ascii,#
  8416. ' s:=#$20AC''abc'';', // #, ascii
  8417. ' s:=#$20AC''ä'';', // #, non ascii
  8418. ' s:=default(string);',
  8419. ' s:=concat(s);',
  8420. ' s:=concat(s,''a'',s);',
  8421. ' s:=#250#269;',
  8422. ' i:=low(s)+high(a);',
  8423. ' s:=''a/b'';',
  8424. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8425. '']);
  8426. ConvertProgram;
  8427. CheckSource('TestStringConst',
  8428. LinesToStr([
  8429. 'this.a = "óż";',
  8430. 'this.b = "a";',
  8431. 'this.c = "ä";',
  8432. 'this.d = "b";',
  8433. 'this.e = "ö";',
  8434. 'this.f = 1 + this.b.length;',
  8435. 'this.g = 1;',
  8436. 'this.s="abc";',
  8437. 'this.i = 0;',
  8438. '']),
  8439. LinesToStr([
  8440. '$mod.s="";',
  8441. '$mod.s="\r\n";',
  8442. '$mod.s="\tfoo";',
  8443. '$mod.s="©";',
  8444. '$mod.s="foo\rbar";',
  8445. '$mod.s=''"'';',
  8446. '$mod.s=''"\''"'';',
  8447. '$mod.s="€";',
  8448. '$mod.s="'#$F0#$90#$90#$B7'";',
  8449. '$mod.s = "abc€";',
  8450. '$mod.s = "ä€";',
  8451. '$mod.s = "€abc";',
  8452. '$mod.s = "ۊ";',
  8453. '$mod.s="";',
  8454. '$mod.s = $mod.s;',
  8455. '$mod.s = $mod.s.concat("a", $mod.s);',
  8456. '$mod.s = "úč";',
  8457. '$mod.i = 1 + $mod.a.length;',
  8458. '$mod.s = "a/b";',
  8459. '']));
  8460. end;
  8461. procedure TTestModule.TestStringConst_InvalidUTF16;
  8462. begin
  8463. StartProgram(false);
  8464. Add([
  8465. 'const',
  8466. ' a: char = #$D87E;',
  8467. ' b: string = #$D87E;',
  8468. ' c: string = #$D87E#43;',
  8469. 'begin',
  8470. ' c:=''abc''#$D87E;',
  8471. ' c:=#0#1#2;',
  8472. ' c:=#127;',
  8473. ' c:=#128;',
  8474. ' c:=#255;',
  8475. ' c:=#256;',
  8476. '']);
  8477. ConvertProgram;
  8478. CheckSource('TestStringConst',
  8479. LinesToStr([
  8480. 'this.a = "\uD87E";',
  8481. 'this.b = "\uD87E";',
  8482. 'this.c = "\uD87E+";',
  8483. '']),
  8484. LinesToStr([
  8485. '$mod.c = "abc\uD87E";',
  8486. '$mod.c = "\x00\x01\x02";',
  8487. '$mod.c = "'#127'";',
  8488. '$mod.c = "'#$c2#$80'";',
  8489. '$mod.c = "'#$c3#$BF'";',
  8490. '$mod.c = "'#$c4#$80'";',
  8491. '']));
  8492. end;
  8493. procedure TTestModule.TestStringConstSurrogate;
  8494. begin
  8495. StartProgram(false);
  8496. Add([
  8497. 'var',
  8498. ' s: string;',
  8499. 'begin',
  8500. ' s:=''😊'';', // 1F60A
  8501. ' s:=''Hello ''#55357#56841', // #$D83D#$DE09
  8502. '']);
  8503. ConvertProgram;
  8504. CheckSource('TestStringConstSurrogate',
  8505. LinesToStr([
  8506. 'this.s="";'
  8507. ]),
  8508. LinesToStr([
  8509. '$mod.s="😊";',
  8510. '$mod.s="Hello 😉";'
  8511. ]));
  8512. end;
  8513. procedure TTestModule.TestStringConst_Multiline;
  8514. begin
  8515. StartProgram(false);
  8516. Add([
  8517. '{$modeswitch multilinestrings}',
  8518. 'const',
  8519. ' a = ``;',
  8520. ' b = `',
  8521. 'line`;',
  8522. ' c = `Single`;',
  8523. ' d = ````;',
  8524. ' e = `abc``xyz`;',
  8525. ' f = `first''line',
  8526. ' second''line`#10;',
  8527. 'begin',
  8528. '']);
  8529. ConvertProgram;
  8530. CheckSource('TestStringConst_Multiline',
  8531. LinesToStr([
  8532. 'this.a = "";',
  8533. 'this.b = "'+JSONNewLine+'line";',
  8534. 'this.c = "Single";',
  8535. 'this.d = "`";',
  8536. 'this.e = "abc`xyz";',
  8537. 'this.f = "first''line'+JSONNewLine+' second''line\n";',
  8538. '']),
  8539. LinesToStr([
  8540. ]));
  8541. end;
  8542. procedure TTestModule.TestString_Length;
  8543. begin
  8544. StartProgram(false);
  8545. Add('const c = ''foo'';');
  8546. Add('var');
  8547. Add(' s: string;');
  8548. Add(' i: longint;');
  8549. Add('begin');
  8550. Add(' i:=length(s);');
  8551. Add(' i:=length(s+s);');
  8552. Add(' i:=length(''abc'');');
  8553. Add(' i:=length(c);');
  8554. ConvertProgram;
  8555. CheckSource('TestString_Length',
  8556. LinesToStr([
  8557. 'this.c = "foo";',
  8558. 'this.s = "";',
  8559. 'this.i = 0;',
  8560. '']),
  8561. LinesToStr([
  8562. '$mod.i = $mod.s.length;',
  8563. '$mod.i = ($mod.s+$mod.s).length;',
  8564. '$mod.i = "abc".length;',
  8565. '$mod.i = $mod.c.length;',
  8566. '']));
  8567. end;
  8568. procedure TTestModule.TestString_Compare;
  8569. begin
  8570. StartProgram(false);
  8571. Add('var');
  8572. Add(' s, t: string;');
  8573. Add(' b: boolean;');
  8574. Add('begin');
  8575. Add(' b:=s=t;');
  8576. Add(' b:=s<>t;');
  8577. Add(' b:=s>t;');
  8578. Add(' b:=s>=t;');
  8579. Add(' b:=s<t;');
  8580. Add(' b:=s<=t;');
  8581. ConvertProgram;
  8582. CheckSource('TestString_Compare',
  8583. LinesToStr([ // statements
  8584. 'this.s = "";',
  8585. 'this.t = "";',
  8586. 'this.b =false;'
  8587. ]),
  8588. LinesToStr([ // this.$main
  8589. '$mod.b = $mod.s === $mod.t;',
  8590. '$mod.b = $mod.s !== $mod.t;',
  8591. '$mod.b = $mod.s > $mod.t;',
  8592. '$mod.b = $mod.s >= $mod.t;',
  8593. '$mod.b = $mod.s < $mod.t;',
  8594. '$mod.b = $mod.s <= $mod.t;',
  8595. '']));
  8596. end;
  8597. procedure TTestModule.TestString_SetLength;
  8598. begin
  8599. StartProgram(false);
  8600. Add([
  8601. 'procedure Fly(var s: string);',
  8602. 'begin',
  8603. ' SetLength(s,1);',
  8604. 'end;',
  8605. 'procedure Run(var s: unicodestring);',
  8606. 'begin',
  8607. ' SetLength(s,2);',
  8608. 'end;',
  8609. 'var s: string;',
  8610. ' u: unicodestring;',
  8611. 'begin',
  8612. ' SetLength(s,3);',
  8613. ' SetLength(u,4);',
  8614. '']);
  8615. ConvertProgram;
  8616. CheckSource('TestString_SetLength',
  8617. LinesToStr([ // statements
  8618. 'this.Fly = function (s) {',
  8619. ' s.set(rtl.strSetLength(s.get(), 1));',
  8620. '};',
  8621. 'this.Run = function (s) {',
  8622. ' s.set(rtl.strSetLength(s.get(), 2));',
  8623. '};',
  8624. 'this.s = "";',
  8625. 'this.u = "";',
  8626. '']),
  8627. LinesToStr([ // this.$main
  8628. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8629. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8630. ]));
  8631. end;
  8632. procedure TTestModule.TestString_CharAt;
  8633. begin
  8634. StartProgram(false);
  8635. Add([
  8636. 'var',
  8637. ' s: string;',
  8638. ' c: char;',
  8639. ' b: boolean;',
  8640. 'begin',
  8641. ' b:= s[1] = c;',
  8642. ' b:= c = s[1];',
  8643. ' b:= c <> s[1];',
  8644. ' b:= c > s[1];',
  8645. ' b:= c >= s[1];',
  8646. ' b:= c < s[2];',
  8647. ' b:= c <= s[1];',
  8648. ' s[1] := c;',
  8649. ' s[2+3] := c;']);
  8650. ConvertProgram;
  8651. CheckSource('TestString_CharAt',
  8652. LinesToStr([ // statements
  8653. 'this.s = "";',
  8654. 'this.c = "\x00";',
  8655. 'this.b = false;'
  8656. ]),
  8657. LinesToStr([ // this.$main
  8658. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8659. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8660. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8661. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8662. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8663. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8664. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8665. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8666. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8667. '']));
  8668. end;
  8669. procedure TTestModule.TestStringHMinusFail;
  8670. begin
  8671. StartProgram(false);
  8672. Add([
  8673. '{$H-}',
  8674. 'var s: string;',
  8675. 'begin']);
  8676. ConvertProgram;
  8677. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8678. end;
  8679. procedure TTestModule.TestStr;
  8680. begin
  8681. StartProgram(false);
  8682. Add('var');
  8683. Add(' b: boolean;');
  8684. Add(' i: longint;');
  8685. Add(' d: double;');
  8686. Add(' s: string;');
  8687. Add('begin');
  8688. Add(' str(b,s);');
  8689. Add(' str(i,s);');
  8690. Add(' str(d,s);');
  8691. Add(' str(i:3,s);');
  8692. Add(' str(d:3:2,s);');
  8693. Add(' Str(12.456:12:1,s);');
  8694. Add(' Str(12.456:12,s);');
  8695. Add(' s:=str(b);');
  8696. Add(' s:=str(i);');
  8697. Add(' s:=str(d);');
  8698. Add(' s:=str(i,i);');
  8699. Add(' s:=str(i:3);');
  8700. Add(' s:=str(d:3:2);');
  8701. Add(' s:=str(i:4,i);');
  8702. Add(' s:=str(i,i:5);');
  8703. Add(' s:=str(i:4,i:5);');
  8704. Add(' s:=str(s,s);');
  8705. Add(' s:=str(s,''foo'');');
  8706. ConvertProgram;
  8707. CheckSource('TestStr',
  8708. LinesToStr([ // statements
  8709. 'this.b = false;',
  8710. 'this.i = 0;',
  8711. 'this.d = 0.0;',
  8712. 'this.s = "";',
  8713. '']),
  8714. LinesToStr([ // this.$main
  8715. '$mod.s = ""+$mod.b;',
  8716. '$mod.s = ""+$mod.i;',
  8717. '$mod.s = rtl.floatToStr($mod.d);',
  8718. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8719. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8720. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8721. '$mod.s = rtl.floatToStr(12.456,12);',
  8722. '$mod.s = ""+$mod.b;',
  8723. '$mod.s = ""+$mod.i;',
  8724. '$mod.s = rtl.floatToStr($mod.d);',
  8725. '$mod.s = ""+$mod.i+$mod.i;',
  8726. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8727. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8728. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8729. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8730. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8731. '$mod.s = $mod.s + $mod.s;',
  8732. '$mod.s = $mod.s + "foo";',
  8733. '']));
  8734. end;
  8735. procedure TTestModule.TestBaseType_AnsiStringFail;
  8736. begin
  8737. StartProgram(false);
  8738. Add('var s: AnsiString');
  8739. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8740. ConvertProgram;
  8741. end;
  8742. procedure TTestModule.TestBaseType_WideStringFail;
  8743. begin
  8744. StartProgram(false);
  8745. Add('var s: WideString');
  8746. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8747. ConvertProgram;
  8748. end;
  8749. procedure TTestModule.TestBaseType_ShortStringFail;
  8750. begin
  8751. StartProgram(false);
  8752. Add('var s: ShortString');
  8753. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8754. ConvertProgram;
  8755. end;
  8756. procedure TTestModule.TestBaseType_RawByteStringFail;
  8757. begin
  8758. StartProgram(false);
  8759. Add('var s: RawByteString');
  8760. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8761. ConvertProgram;
  8762. end;
  8763. procedure TTestModule.TestTypeShortstring_Fail;
  8764. begin
  8765. StartProgram(false);
  8766. Add('type t = string[12];');
  8767. Add('var s: t;');
  8768. Add('begin');
  8769. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8770. ConvertProgram;
  8771. end;
  8772. procedure TTestModule.TestCharSet_Custom;
  8773. begin
  8774. StartProgram(false);
  8775. Add([
  8776. 'type',
  8777. ' TCharRg = ''a''..''z'';',
  8778. ' TSetOfCharRg = set of TCharRg;',
  8779. ' TCharRg2 = ''m''..''p'';',
  8780. 'const',
  8781. ' crg: TCharRg = ''b'';',
  8782. 'var',
  8783. ' c: char;',
  8784. ' crg2: TCharRg2;',
  8785. ' s: TSetOfCharRg;',
  8786. 'begin',
  8787. ' c:=crg;',
  8788. ' crg:=c;',
  8789. ' crg2:=crg;',
  8790. ' if c=crg then ;',
  8791. ' if crg=c then ;',
  8792. ' if crg=crg2 then ;',
  8793. ' if c in s then ;',
  8794. ' if crg2 in s then ;',
  8795. ' c:=default(TCharRg);',
  8796. '']);
  8797. ConvertProgram;
  8798. CheckSource('TestCharSet_Custom',
  8799. LinesToStr([ // statements
  8800. 'this.crg = "b";',
  8801. 'this.c = "\x00";',
  8802. 'this.crg2 = "m";',
  8803. 'this.s = {};',
  8804. '']),
  8805. LinesToStr([ // this.$main
  8806. '$mod.c = $mod.crg;',
  8807. '$mod.crg = $mod.c;',
  8808. '$mod.crg2 = $mod.crg;',
  8809. 'if ($mod.c === $mod.crg) ;',
  8810. 'if ($mod.crg === $mod.c) ;',
  8811. 'if ($mod.crg === $mod.crg2) ;',
  8812. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8813. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8814. '$mod.c = "a";',
  8815. '']));
  8816. end;
  8817. procedure TTestModule.TestWideChar;
  8818. begin
  8819. StartProgram(false);
  8820. Add([
  8821. 'procedure Fly(var c: char);',
  8822. 'begin',
  8823. 'end;',
  8824. 'procedure Run(var c: widechar);',
  8825. 'begin',
  8826. 'end;',
  8827. 'var',
  8828. ' c: char;',
  8829. ' wc: widechar;',
  8830. ' w: word;',
  8831. 'begin',
  8832. ' Fly(wc);',
  8833. ' Run(c);',
  8834. ' wc:=WideChar(w);',
  8835. ' w:=ord(wc);',
  8836. '']);
  8837. ConvertProgram;
  8838. CheckSource('TestWideChar_VarArg',
  8839. LinesToStr([ // statements
  8840. 'this.Fly = function (c) {',
  8841. '};',
  8842. 'this.Run = function (c) {',
  8843. '};',
  8844. 'this.c = "\x00";',
  8845. 'this.wc = "\x00";',
  8846. 'this.w = 0;',
  8847. '']),
  8848. LinesToStr([ // this.$main
  8849. '$mod.Fly({',
  8850. ' p: $mod,',
  8851. ' get: function () {',
  8852. ' return this.p.wc;',
  8853. ' },',
  8854. ' set: function (v) {',
  8855. ' this.p.wc = v;',
  8856. ' }',
  8857. '});',
  8858. '$mod.Run({',
  8859. ' p: $mod,',
  8860. ' get: function () {',
  8861. ' return this.p.c;',
  8862. ' },',
  8863. ' set: function (v) {',
  8864. ' this.p.c = v;',
  8865. ' }',
  8866. '});',
  8867. '$mod.wc = String.fromCharCode($mod.w);',
  8868. '$mod.w = $mod.wc.charCodeAt();',
  8869. '',
  8870. '']));
  8871. end;
  8872. procedure TTestModule.TestForCharDo;
  8873. begin
  8874. StartProgram(false);
  8875. Add([
  8876. 'var c: char;',
  8877. 'begin',
  8878. ' for c:=''a'' to ''c'' do ;',
  8879. ' for c:=c downto ''a'' do ;',
  8880. ' for c:=''Б'' to ''Я'' do ;',
  8881. '']);
  8882. ConvertProgram;
  8883. CheckSource('TestForCharDo',
  8884. LinesToStr([ // statements
  8885. 'this.c = "\x00";']),
  8886. LinesToStr([ // this.$main
  8887. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8888. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8889. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8890. '']));
  8891. end;
  8892. procedure TTestModule.TestForCharInDo;
  8893. begin
  8894. StartProgram(false);
  8895. Add([
  8896. 'type',
  8897. ' TSetOfChar = set of char;',
  8898. ' TCharRg = ''a''..''z'';',
  8899. ' TSetOfCharRg = set of TCharRg;',
  8900. 'const Foo = ''foo'';',
  8901. 'var',
  8902. ' c,c2: char;',
  8903. ' s: string;',
  8904. ' a1: array of char;',
  8905. ' a2: array[1..3] of char;',
  8906. ' soc: TSetOfChar;',
  8907. ' socr: TSetOfCharRg;',
  8908. ' cr: TCharRg;',
  8909. 'begin',
  8910. ' for c in foo do ;',
  8911. ' for c in s do ;',
  8912. ' for c in char do ;',
  8913. ' for c in a1 do ;',
  8914. ' for c in a2 do ;',
  8915. ' for c in [''1''..''3''] do ;',
  8916. ' for c in TSetOfChar do ;',
  8917. ' for c in TCharRg do ;',
  8918. ' for c in soc do c2:=c;',
  8919. ' for c in TSetOfCharRg do ;',
  8920. ' for c in socr do ;',
  8921. ' for cr in TCharRg do ;',
  8922. ' for cr in TSetOfCharRg do ;',
  8923. ' for cr in socr do ;',
  8924. '']);
  8925. ConvertProgram;
  8926. CheckSource('TestForCharInDo',
  8927. LinesToStr([ // statements
  8928. 'this.Foo = "foo";',
  8929. 'this.c = "\x00";',
  8930. 'this.c2 = "\x00";',
  8931. 'this.s = "";',
  8932. 'this.a1 = [];',
  8933. 'this.a2 = rtl.arraySetLength(null, "\x00", 3);',
  8934. 'this.soc = {};',
  8935. 'this.socr = {};',
  8936. 'this.cr = "a";',
  8937. '']),
  8938. LinesToStr([ // this.$main
  8939. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8940. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8941. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8942. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8943. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8944. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8945. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8946. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8947. 'for (var $l8 in $mod.soc) {',
  8948. ' $mod.c = String.fromCharCode($l8);',
  8949. ' $mod.c2 = $mod.c;',
  8950. '};',
  8951. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8952. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8953. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8954. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8955. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8956. '']));
  8957. end;
  8958. procedure TTestModule.TestProcTwoArgs;
  8959. begin
  8960. StartProgram(false);
  8961. Add('procedure Test(a,b: longint);');
  8962. Add('begin');
  8963. Add('end;');
  8964. Add('begin');
  8965. ConvertProgram;
  8966. CheckSource('TestProcTwoArgs',
  8967. LinesToStr([ // statements
  8968. 'this.Test = function (a,b) {',
  8969. '};'
  8970. ]),
  8971. LinesToStr([ // this.$main
  8972. ''
  8973. ]));
  8974. end;
  8975. procedure TTestModule.TestProc_DefaultValue;
  8976. begin
  8977. StartProgram(false);
  8978. Add('procedure p1(i: longint = 1);');
  8979. Add('begin');
  8980. Add('end;');
  8981. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8982. Add('begin');
  8983. Add('end;');
  8984. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8985. Add('begin');
  8986. Add('end;');
  8987. Add('begin');
  8988. Add(' p1;');
  8989. Add(' p1();');
  8990. Add(' p1(11);');
  8991. Add(' p2;');
  8992. Add(' p2();');
  8993. Add(' p2(12);');
  8994. Add(' p2(13,''b'');');
  8995. Add(' p3();');
  8996. ConvertProgram;
  8997. CheckSource('TestProc_DefaultValue',
  8998. LinesToStr([ // statements
  8999. 'this.p1 = function (i) {',
  9000. '};',
  9001. 'this.p2 = function (i,c) {',
  9002. '};',
  9003. 'this.p3 = function (d,b,s) {',
  9004. '};'
  9005. ]),
  9006. LinesToStr([ // this.$main
  9007. ' $mod.p1(1);',
  9008. ' $mod.p1(1);',
  9009. ' $mod.p1(11);',
  9010. ' $mod.p2(1,"a");',
  9011. ' $mod.p2(1,"a");',
  9012. ' $mod.p2(12,"a");',
  9013. ' $mod.p2(13,"b");',
  9014. ' $mod.p3(1.0,false,"abc");'
  9015. ]));
  9016. end;
  9017. procedure TTestModule.TestFunctionInt;
  9018. begin
  9019. StartProgram(false);
  9020. Add('function MyTest(Bar: longint): longint;');
  9021. Add('begin');
  9022. Add(' Result:=2*bar');
  9023. Add('end;');
  9024. Add('begin');
  9025. ConvertProgram;
  9026. CheckSource('TestFunctionInt',
  9027. LinesToStr([ // statements
  9028. 'this.MyTest = function (Bar) {',
  9029. ' var Result = 0;',
  9030. ' Result = 2*Bar;',
  9031. ' return Result;',
  9032. '};'
  9033. ]),
  9034. LinesToStr([ // this.$main
  9035. ''
  9036. ]));
  9037. end;
  9038. procedure TTestModule.TestFunctionString;
  9039. begin
  9040. StartProgram(false);
  9041. Add('function Test(Bar: string): string;');
  9042. Add('begin');
  9043. Add(' Result:=bar+BAR');
  9044. Add('end;');
  9045. Add('begin');
  9046. ConvertProgram;
  9047. CheckSource('TestFunctionString',
  9048. LinesToStr([ // statements
  9049. 'this.Test = function (Bar) {',
  9050. ' var Result = "";',
  9051. ' Result = Bar+Bar;',
  9052. ' return Result;',
  9053. '};'
  9054. ]),
  9055. LinesToStr([ // this.$main
  9056. ''
  9057. ]));
  9058. end;
  9059. procedure TTestModule.TestIfThen;
  9060. begin
  9061. StartProgram(false);
  9062. Add([
  9063. 'var b: boolean;',
  9064. 'begin',
  9065. ' if b then ;',
  9066. ' if b then else ;']);
  9067. ConvertProgram;
  9068. CheckSource('TestIfThen',
  9069. LinesToStr([ // statements
  9070. 'this.b = false;',
  9071. '']),
  9072. LinesToStr([ // this.$main
  9073. 'if ($mod.b) ;',
  9074. 'if ($mod.b) ;',
  9075. '']));
  9076. end;
  9077. procedure TTestModule.TestForLoop;
  9078. begin
  9079. StartProgram(false);
  9080. Add('var');
  9081. Add(' vI, vJ, vN: longint;');
  9082. Add('begin');
  9083. Add(' VJ:=0;');
  9084. Add(' VN:=3;');
  9085. Add(' for VI:=1 to VN do');
  9086. Add(' begin');
  9087. Add(' VJ:=VJ+VI;');
  9088. Add(' end;');
  9089. ConvertProgram;
  9090. CheckSource('TestForLoop',
  9091. LinesToStr([ // statements
  9092. 'this.vI = 0;',
  9093. 'this.vJ = 0;',
  9094. 'this.vN = 0;'
  9095. ]),
  9096. LinesToStr([ // this.$main
  9097. ' $mod.vJ = 0;',
  9098. ' $mod.vN = 3;',
  9099. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9100. ' $mod.vI = $l;',
  9101. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9102. ' };',
  9103. '']));
  9104. end;
  9105. procedure TTestModule.TestForLoopInsideFunction;
  9106. begin
  9107. StartProgram(false);
  9108. Add('function SumNumbers(Count: longint): longint;');
  9109. Add('var');
  9110. Add(' vI, vJ: longint;');
  9111. Add('begin');
  9112. Add(' vj:=0;');
  9113. Add(' for vi:=1 to count do');
  9114. Add(' begin');
  9115. Add(' vj:=vj+vi;');
  9116. Add(' end;');
  9117. Add('end;');
  9118. Add('begin');
  9119. Add(' sumnumbers(3);');
  9120. ConvertProgram;
  9121. CheckSource('TestForLoopInsideFunction',
  9122. LinesToStr([ // statements
  9123. 'this.SumNumbers = function (Count) {',
  9124. ' var Result = 0;',
  9125. ' var vI = 0;',
  9126. ' var vJ = 0;',
  9127. ' vJ = 0;',
  9128. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9129. ' vI = $l;',
  9130. ' vJ = vJ + vI;',
  9131. ' };',
  9132. ' return Result;',
  9133. '};'
  9134. ]),
  9135. LinesToStr([ // $mod.$main
  9136. ' $mod.SumNumbers(3);'
  9137. ]));
  9138. end;
  9139. procedure TTestModule.TestForLoop_ReadVarAfter;
  9140. begin
  9141. StartProgram(false);
  9142. Add('var');
  9143. Add(' vI: longint;');
  9144. Add('begin');
  9145. Add(' for vi:=1 to 2 do ;');
  9146. Add(' if vi=3 then ;');
  9147. ConvertProgram;
  9148. CheckSource('TestForLoop',
  9149. LinesToStr([ // statements
  9150. 'this.vI = 0;'
  9151. ]),
  9152. LinesToStr([ // this.$main
  9153. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9154. ' if ($mod.vI===3) ;'
  9155. ]));
  9156. end;
  9157. procedure TTestModule.TestForLoop_Nested;
  9158. begin
  9159. StartProgram(false);
  9160. Add('function SumNumbers(Count: longint): longint;');
  9161. Add('var');
  9162. Add(' vI, vJ, vK: longint;');
  9163. Add('begin');
  9164. Add(' VK:=0;');
  9165. Add(' for VI:=1 to count do');
  9166. Add(' begin');
  9167. Add(' for vj:=1 to vi do');
  9168. Add(' begin');
  9169. Add(' vk:=VK+VI;');
  9170. Add(' end;');
  9171. Add(' end;');
  9172. Add('end;');
  9173. Add('begin');
  9174. Add(' sumnumbers(3);');
  9175. ConvertProgram;
  9176. CheckSource('TestForLoopInFunction',
  9177. LinesToStr([ // statements
  9178. 'this.SumNumbers = function (Count) {',
  9179. ' var Result = 0;',
  9180. ' var vI = 0;',
  9181. ' var vJ = 0;',
  9182. ' var vK = 0;',
  9183. ' vK = 0;',
  9184. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9185. ' vI = $l;',
  9186. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9187. ' vJ = $l1;',
  9188. ' vK = vK + vI;',
  9189. ' };',
  9190. ' };',
  9191. ' return Result;',
  9192. '};'
  9193. ]),
  9194. LinesToStr([ // $mod.$main
  9195. ' $mod.SumNumbers(3);'
  9196. ]));
  9197. end;
  9198. procedure TTestModule.TestRepeatUntil;
  9199. begin
  9200. StartProgram(false);
  9201. Add('var');
  9202. Add(' vI, vJ, vN: longint;');
  9203. Add('begin');
  9204. Add(' vn:=3;');
  9205. Add(' vj:=0;');
  9206. Add(' VI:=0;');
  9207. Add(' repeat');
  9208. Add(' VI:=vi+1;');
  9209. Add(' vj:=VJ+vI;');
  9210. Add(' until vi>=vn');
  9211. ConvertProgram;
  9212. CheckSource('TestRepeatUntil',
  9213. LinesToStr([ // statements
  9214. 'this.vI = 0;',
  9215. 'this.vJ = 0;',
  9216. 'this.vN = 0;'
  9217. ]),
  9218. LinesToStr([ // $mod.$main
  9219. ' $mod.vN = 3;',
  9220. ' $mod.vJ = 0;',
  9221. ' $mod.vI = 0;',
  9222. ' do{',
  9223. ' $mod.vI = $mod.vI + 1;',
  9224. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9225. ' }while(!($mod.vI>=$mod.vN));'
  9226. ]));
  9227. end;
  9228. procedure TTestModule.TestAsmBlock;
  9229. begin
  9230. StartProgram(false);
  9231. Add([
  9232. 'var',
  9233. ' vI: longint;',
  9234. 'begin',
  9235. ' vi:=1;',
  9236. ' asm',
  9237. ' if (vI===1) {',
  9238. ' vI=2;',
  9239. //' console.log(''end;'');', ToDo
  9240. ' }',
  9241. ' if (vI===2){ vI=3; }',
  9242. ' end;',
  9243. ' VI:=4;']);
  9244. ConvertProgram;
  9245. CheckSource('TestAsmBlock',
  9246. LinesToStr([ // statements
  9247. 'this.vI = 0;'
  9248. ]),
  9249. LinesToStr([ // $mod.$main
  9250. '$mod.vI = 1;',
  9251. 'if (vI===1) {',
  9252. ' vI=2;',
  9253. '}',
  9254. 'if (vI===2){ vI=3; }',
  9255. ';',
  9256. '$mod.vI = 4;'
  9257. ]));
  9258. end;
  9259. procedure TTestModule.TestAsmPas_Impl;
  9260. begin
  9261. StartUnit(false);
  9262. Add('interface');
  9263. Add('const cIntf: longint = 1;');
  9264. Add('var vIntf: longint;');
  9265. Add('implementation');
  9266. Add('const cImpl: longint = 2;');
  9267. Add('var vImpl: longint;');
  9268. Add('procedure DoIt;');
  9269. Add('const cLoc: longint = 3;');
  9270. Add('var vLoc: longint;');
  9271. Add('begin;');
  9272. Add(' asm');
  9273. //Add(' pas(vIntf)=pas(cIntf);');
  9274. //Add(' pas(vImpl)=pas(cImpl);');
  9275. //Add(' pas(vLoc)=pas(cLoc);');
  9276. Add(' end;');
  9277. Add('end;');
  9278. ConvertUnit;
  9279. CheckSource('TestAsmPas_Impl',
  9280. LinesToStr([
  9281. 'var $impl = $mod.$impl;',
  9282. 'this.cIntf = 1;',
  9283. 'this.vIntf = 0;',
  9284. '']),
  9285. '', // this.$init
  9286. LinesToStr([ // implementation
  9287. '$impl.cImpl = 2;',
  9288. '$impl.vImpl = 0;',
  9289. 'var cLoc = 3;',
  9290. '$impl.DoIt = function () {',
  9291. ' var vLoc = 0;',
  9292. '};',
  9293. '']) );
  9294. end;
  9295. procedure TTestModule.TestTryFinally;
  9296. begin
  9297. StartProgram(false);
  9298. Add('var i: longint;');
  9299. Add('begin');
  9300. Add(' try');
  9301. Add(' i:=0; i:=2 div i;');
  9302. Add(' finally');
  9303. Add(' i:=3');
  9304. Add(' end;');
  9305. ConvertProgram;
  9306. CheckSource('TestTryFinally',
  9307. LinesToStr([ // statements
  9308. 'this.i = 0;'
  9309. ]),
  9310. LinesToStr([ // $mod.$main
  9311. 'try {',
  9312. ' $mod.i = 0;',
  9313. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9314. '} finally {',
  9315. ' $mod.i = 3;',
  9316. '};'
  9317. ]));
  9318. end;
  9319. procedure TTestModule.TestTryExcept;
  9320. begin
  9321. StartProgram(false);
  9322. Add([
  9323. 'type',
  9324. ' TObject = class end;',
  9325. ' Exception = class Msg: string; end;',
  9326. ' EInvalidCast = class(Exception) end;',
  9327. 'var vI: longint;',
  9328. 'begin',
  9329. ' try',
  9330. ' vi:=1;',
  9331. ' except',
  9332. ' vi:=2',
  9333. ' end;',
  9334. ' try',
  9335. ' vi:=3;',
  9336. ' except',
  9337. ' raise;',
  9338. ' end;',
  9339. ' try',
  9340. ' VI:=4;',
  9341. ' except',
  9342. ' on einvalidcast do',
  9343. ' raise;',
  9344. ' on E: exception do',
  9345. ' if e.msg='''' then',
  9346. ' raise e;',
  9347. ' else',
  9348. ' vi:=5',
  9349. ' end;',
  9350. ' try',
  9351. ' VI:=6;',
  9352. ' except',
  9353. ' on einvalidcast do ;',
  9354. ' end;',
  9355. '']);
  9356. ConvertProgram;
  9357. CheckSource('TestTryExcept',
  9358. LinesToStr([ // statements
  9359. 'rtl.createClass(this, "TObject", null, function () {',
  9360. ' this.$init = function () {',
  9361. ' };',
  9362. ' this.$final = function () {',
  9363. ' };',
  9364. '});',
  9365. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9366. ' this.$init = function () {',
  9367. ' $mod.TObject.$init.call(this);',
  9368. ' this.Msg = "";',
  9369. ' };',
  9370. '});',
  9371. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9372. '});',
  9373. 'this.vI = 0;'
  9374. ]),
  9375. LinesToStr([ // $mod.$main
  9376. 'try {',
  9377. ' $mod.vI = 1;',
  9378. '} catch ($e) {',
  9379. ' $mod.vI = 2;',
  9380. '};',
  9381. 'try {',
  9382. ' $mod.vI = 3;',
  9383. '} catch ($e) {',
  9384. ' throw $e;',
  9385. '};',
  9386. 'try {',
  9387. ' $mod.vI = 4;',
  9388. '} catch ($e) {',
  9389. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9390. ' throw $e',
  9391. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9392. ' var E = $e;',
  9393. ' if (E.Msg === "") throw E;',
  9394. ' } else {',
  9395. ' $mod.vI = 5;',
  9396. ' }',
  9397. '};',
  9398. 'try {',
  9399. ' $mod.vI = 6;',
  9400. '} catch ($e) {',
  9401. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9402. ' } else throw $e',
  9403. '};',
  9404. '']));
  9405. end;
  9406. procedure TTestModule.TestTryExcept_ReservedWords;
  9407. begin
  9408. StartProgram(false);
  9409. Add([
  9410. 'type',
  9411. ' TObject = class end;',
  9412. ' Exception = class',
  9413. ' Symbol: string;',
  9414. ' end;',
  9415. 'var &try: longint;',
  9416. 'begin',
  9417. ' try',
  9418. ' &try:=4;',
  9419. ' except',
  9420. ' on Error: exception do',
  9421. ' if errOR.symBol='''' then',
  9422. ' raise ERRor;',
  9423. ' end;',
  9424. '']);
  9425. ConvertProgram;
  9426. CheckSource('TestTryExcept_ReservedWords',
  9427. LinesToStr([ // statements
  9428. 'rtl.createClass(this, "TObject", null, function () {',
  9429. ' this.$init = function () {',
  9430. ' };',
  9431. ' this.$final = function () {',
  9432. ' };',
  9433. '});',
  9434. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9435. ' this.$init = function () {',
  9436. ' $mod.TObject.$init.call(this);',
  9437. ' this.Symbol = "";',
  9438. ' };',
  9439. '});',
  9440. 'this.Try = 0;',
  9441. '']),
  9442. LinesToStr([ // $mod.$main
  9443. 'try {',
  9444. ' $mod.Try = 4;',
  9445. '} catch ($e) {',
  9446. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9447. ' var error = $e;',
  9448. ' if (error.Symbol === "") throw error;',
  9449. ' } else throw $e',
  9450. '};',
  9451. '']));
  9452. end;
  9453. procedure TTestModule.TestIfThenRaiseElse;
  9454. begin
  9455. StartProgram(false);
  9456. Add([
  9457. 'type',
  9458. ' TObject = class',
  9459. ' constructor Create;',
  9460. ' end;',
  9461. 'constructor TObject.Create;',
  9462. 'begin',
  9463. 'end;',
  9464. 'var b: boolean;',
  9465. 'begin',
  9466. ' if b then',
  9467. ' raise TObject.Create',
  9468. ' else',
  9469. ' b:=false;',
  9470. '']);
  9471. ConvertProgram;
  9472. CheckSource('TestIfThenRaiseElse',
  9473. LinesToStr([ // statements
  9474. 'rtl.createClass(this, "TObject", null, function () {',
  9475. ' this.$init = function () {',
  9476. ' };',
  9477. ' this.$final = function () {',
  9478. ' };',
  9479. ' this.Create = function () {',
  9480. ' return this;',
  9481. ' };',
  9482. '});',
  9483. 'this.b = false;',
  9484. '']),
  9485. LinesToStr([ // $mod.$main
  9486. 'if ($mod.b) {',
  9487. ' throw $mod.TObject.$create("Create")}',
  9488. ' else $mod.b = false;',
  9489. '']));
  9490. end;
  9491. procedure TTestModule.TestCaseOf;
  9492. begin
  9493. StartProgram(false);
  9494. Add([
  9495. 'const e: longint; external name ''$e'';',
  9496. 'var vI: longint;',
  9497. 'begin',
  9498. ' case vi of',
  9499. ' 1: ;',
  9500. ' 2: vi:=3;',
  9501. ' e: ;',
  9502. ' else',
  9503. ' VI:=4',
  9504. ' end;']);
  9505. ConvertProgram;
  9506. CheckSource('TestCaseOf',
  9507. LinesToStr([ // statements
  9508. 'this.vI = 0;'
  9509. ]),
  9510. LinesToStr([ // $mod.$main
  9511. 'var $tmp = $mod.vI;',
  9512. 'if ($tmp === 1) {}',
  9513. 'else if ($tmp === 2) {',
  9514. ' $mod.vI = 3}',
  9515. ' else if ($tmp === $e) {}',
  9516. 'else {',
  9517. ' $mod.vI = 4;',
  9518. '};'
  9519. ]));
  9520. end;
  9521. procedure TTestModule.TestCaseOf_UseSwitch;
  9522. begin
  9523. StartProgram(false);
  9524. Converter.UseSwitchStatement:=true;
  9525. Add('var Vi: longint;');
  9526. Add('begin');
  9527. Add(' case vi of');
  9528. Add(' 1: ;');
  9529. Add(' 2: VI:=3;');
  9530. Add(' else');
  9531. Add(' vi:=4');
  9532. Add(' end;');
  9533. ConvertProgram;
  9534. CheckSource('TestCaseOf_UseSwitch',
  9535. LinesToStr([ // statements
  9536. 'this.Vi = 0;'
  9537. ]),
  9538. LinesToStr([ // $mod.$main
  9539. 'switch ($mod.Vi) {',
  9540. 'case 1:',
  9541. ' break;',
  9542. 'case 2:',
  9543. ' $mod.Vi = 3;',
  9544. ' break;',
  9545. 'default:',
  9546. ' $mod.Vi = 4;',
  9547. '};'
  9548. ]));
  9549. end;
  9550. procedure TTestModule.TestCaseOfNoElse;
  9551. begin
  9552. StartProgram(false);
  9553. Add('var Vi: longint;');
  9554. Add('begin');
  9555. Add(' case vi of');
  9556. Add(' 1: begin vi:=2; VI:=3; end;');
  9557. Add(' end;');
  9558. ConvertProgram;
  9559. CheckSource('TestCaseOfNoElse',
  9560. LinesToStr([ // statements
  9561. 'this.Vi = 0;'
  9562. ]),
  9563. LinesToStr([ // $mod.$main
  9564. 'var $tmp = $mod.Vi;',
  9565. 'if ($tmp === 1) {',
  9566. ' $mod.Vi = 2;',
  9567. ' $mod.Vi = 3;',
  9568. '};'
  9569. ]));
  9570. end;
  9571. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9572. begin
  9573. StartProgram(false);
  9574. Converter.UseSwitchStatement:=true;
  9575. Add('var vI: longint;');
  9576. Add('begin');
  9577. Add(' case vi of');
  9578. Add(' 1: begin VI:=2; vi:=3; end;');
  9579. Add(' end;');
  9580. ConvertProgram;
  9581. CheckSource('TestCaseOfNoElse_UseSwitch',
  9582. LinesToStr([ // statements
  9583. 'this.vI = 0;'
  9584. ]),
  9585. LinesToStr([ // $mod.$main
  9586. 'switch ($mod.vI) {',
  9587. 'case 1:',
  9588. ' $mod.vI = 2;',
  9589. ' $mod.vI = 3;',
  9590. ' break;',
  9591. '};'
  9592. ]));
  9593. end;
  9594. procedure TTestModule.TestCaseOfRange;
  9595. begin
  9596. StartProgram(false);
  9597. Add('var vI: longint;');
  9598. Add('begin');
  9599. Add(' case vi of');
  9600. Add(' 1..3: vi:=14;');
  9601. Add(' 4,5: vi:=16;');
  9602. Add(' 6..7,9..10: ;');
  9603. Add(' else ;');
  9604. Add(' end;');
  9605. ConvertProgram;
  9606. CheckSource('TestCaseOfRange',
  9607. LinesToStr([ // statements
  9608. 'this.vI = 0;'
  9609. ]),
  9610. LinesToStr([ // $mod.$main
  9611. 'var $tmp = $mod.vI;',
  9612. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9613. ' $mod.vI = 14',
  9614. '} else if (($tmp === 4) || ($tmp === 5)){',
  9615. ' $mod.vI = 16',
  9616. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9617. ]));
  9618. end;
  9619. procedure TTestModule.TestCaseOfString;
  9620. begin
  9621. StartProgram(false);
  9622. Add([
  9623. 'var s,h: string;',
  9624. 'begin',
  9625. ' case s of',
  9626. ' ''foo'': s:=h;',
  9627. ' ''a''..''z'': h:=s;',
  9628. ' ''ў'', ''ё'': ;',
  9629. ' ''Б''..''Я'': ;',
  9630. ' end;',
  9631. '']);
  9632. ConvertProgram;
  9633. CheckSource('TestCaseOfString',
  9634. LinesToStr([ // statements
  9635. 'this.s = "";',
  9636. 'this.h = "";',
  9637. '']),
  9638. LinesToStr([ // $mod.$main
  9639. 'var $tmp = $mod.s;',
  9640. 'if ($tmp === "foo") {',
  9641. ' $mod.s = $mod.h}',
  9642. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9643. ' $mod.h = $mod.s}',
  9644. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9645. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9646. '']));
  9647. end;
  9648. procedure TTestModule.TestCaseOfChar;
  9649. begin
  9650. StartProgram(false);
  9651. Add([
  9652. 'var s,h: char;',
  9653. 'begin',
  9654. ' case s of',
  9655. ' ''a''..''z'': h:=s;',
  9656. ' ''ä'': ;',
  9657. ' ''ў'', ''ё'': ;',
  9658. ' ''Б''..''Я'': ;',
  9659. ' end;',
  9660. '']);
  9661. ConvertProgram;
  9662. CheckSource('TestCaseOfString',
  9663. LinesToStr([ // statements
  9664. 'this.s = "\x00";',
  9665. 'this.h = "\x00";',
  9666. '']),
  9667. LinesToStr([ // $mod.$main
  9668. 'var $tmp = $mod.s;',
  9669. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9670. ' $mod.h = $mod.s}',
  9671. ' else if ($tmp === "ä") {}',
  9672. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9673. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9674. '']));
  9675. end;
  9676. procedure TTestModule.TestCaseOfExternalClassConst;
  9677. begin
  9678. StartProgram(false);
  9679. Add([
  9680. '{$modeswitch externalclass}',
  9681. 'type',
  9682. ' TBird = class external name ''Bird''',
  9683. ' const e: longint;',
  9684. ' end;',
  9685. 'var vI: longint;',
  9686. 'begin',
  9687. ' case vi of',
  9688. ' 1: vi:=3;',
  9689. ' TBird.e: ;',
  9690. ' end;']);
  9691. ConvertProgram;
  9692. CheckSource('TestCaseOfExternalClassConst',
  9693. LinesToStr([ // statements
  9694. 'this.vI = 0;'
  9695. ]),
  9696. LinesToStr([ // $mod.$main
  9697. 'var $tmp = $mod.vI;',
  9698. 'if ($tmp === 1) {',
  9699. ' $mod.vI = 3}',
  9700. ' else if ($tmp === Bird.e) ;'
  9701. ]));
  9702. end;
  9703. procedure TTestModule.TestDebugger;
  9704. begin
  9705. StartProgram(false);
  9706. Add([
  9707. 'procedure DoIt;',
  9708. 'begin',
  9709. ' deBugger;',
  9710. ' DeBugger();',
  9711. 'end;',
  9712. 'begin',
  9713. ' Debugger;']);
  9714. ConvertProgram;
  9715. CheckSource('TestDebugger',
  9716. LinesToStr([ // statements
  9717. 'this.DoIt = function () {',
  9718. ' debugger;',
  9719. ' debugger;',
  9720. '};',
  9721. '']),
  9722. LinesToStr([ // $mod.$main
  9723. 'debugger;',
  9724. '']));
  9725. end;
  9726. procedure TTestModule.TestArray_Dynamic;
  9727. begin
  9728. StartProgram(false);
  9729. Add([
  9730. 'type',
  9731. ' TArrayInt = array of longint;',
  9732. 'var',
  9733. ' Arr: TArrayInt;',
  9734. ' i: longint;',
  9735. ' b: boolean;',
  9736. 'begin',
  9737. ' SetLength(arr,3);',
  9738. ' arr[0]:=4;',
  9739. ' arr[1]:=length(arr)+arr[0];',
  9740. ' arr[i]:=5;',
  9741. ' arr[arr[i]]:=arr[6];',
  9742. ' i:=low(arr);',
  9743. ' i:=high(arr);',
  9744. ' b:=Assigned(arr);',
  9745. ' Arr:=default(TArrayInt);']);
  9746. ConvertProgram;
  9747. CheckSource('TestArray_Dynamic',
  9748. LinesToStr([ // statements
  9749. 'this.Arr = [];',
  9750. 'this.i = 0;',
  9751. 'this.b = false;'
  9752. ]),
  9753. LinesToStr([ // $mod.$main
  9754. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9755. '$mod.Arr[0] = 4;',
  9756. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9757. '$mod.Arr[$mod.i] = 5;',
  9758. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9759. '$mod.i = 0;',
  9760. '$mod.i = rtl.length($mod.Arr) - 1;',
  9761. '$mod.b = rtl.length($mod.Arr) > 0;',
  9762. '$mod.Arr = [];',
  9763. '']));
  9764. end;
  9765. procedure TTestModule.TestArray_Dynamic_Nil;
  9766. begin
  9767. StartProgram(false);
  9768. Add('type');
  9769. Add(' TArrayInt = array of longint;');
  9770. Add('var');
  9771. Add(' Arr: TArrayInt;');
  9772. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9773. Add('begin');
  9774. Add(' arr:=nil;');
  9775. Add(' if arr=nil then;');
  9776. Add(' if nil=arr then;');
  9777. Add(' if arr<>nil then;');
  9778. Add(' if nil<>arr then;');
  9779. Add(' DoIt(nil,nil);');
  9780. ConvertProgram;
  9781. CheckSource('TestArray_Dynamic',
  9782. LinesToStr([ // statements
  9783. 'this.Arr = [];',
  9784. 'this.DoIt = function(i,j){',
  9785. '};'
  9786. ]),
  9787. LinesToStr([ // $mod.$main
  9788. '$mod.Arr = [];',
  9789. 'if (rtl.length($mod.Arr) === 0) ;',
  9790. 'if (rtl.length($mod.Arr) === 0) ;',
  9791. 'if (rtl.length($mod.Arr) > 0) ;',
  9792. 'if (rtl.length($mod.Arr) > 0) ;',
  9793. '$mod.DoIt([],[]);',
  9794. '']));
  9795. end;
  9796. procedure TTestModule.TestArray_DynMultiDimensional;
  9797. begin
  9798. StartProgram(false);
  9799. Add([
  9800. 'type',
  9801. ' TArrayInt = array of longint;',
  9802. ' TArrayArrayInt = array of TArrayInt;',
  9803. 'var',
  9804. ' Arr: TArrayInt;',
  9805. ' Arr2: TArrayArrayInt;',
  9806. ' i: longint;',
  9807. 'begin',
  9808. ' arr2:=nil;',
  9809. ' if arr2=nil then;',
  9810. ' if nil=arr2 then;',
  9811. ' i:=low(arr2);',
  9812. ' i:=low(arr2[1]);',
  9813. ' i:=high(arr2);',
  9814. ' i:=high(arr2[2]);',
  9815. ' arr2[3]:=arr;',
  9816. ' arr2[4][5]:=i;',
  9817. ' i:=arr2[6][7];',
  9818. ' arr2[8,9]:=i;',
  9819. ' i:=arr2[10,11];',
  9820. ' SetLength(arr2,14);',
  9821. ' SetLength(arr2[15],16);']);
  9822. ConvertProgram;
  9823. CheckSource('TestArray_Dynamic',
  9824. LinesToStr([ // statements
  9825. 'this.Arr = [];',
  9826. 'this.Arr2 = [];',
  9827. 'this.i = 0;'
  9828. ]),
  9829. LinesToStr([ // $mod.$main
  9830. '$mod.Arr2 = [];',
  9831. 'if (rtl.length($mod.Arr2) === 0) ;',
  9832. 'if (rtl.length($mod.Arr2) === 0) ;',
  9833. '$mod.i = 0;',
  9834. '$mod.i = 0;',
  9835. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9836. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9837. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9838. '$mod.Arr2[4][5] = $mod.i;',
  9839. '$mod.i = $mod.Arr2[6][7];',
  9840. '$mod.Arr2[8][9] = $mod.i;',
  9841. '$mod.i = $mod.Arr2[10][11];',
  9842. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9843. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9844. '']));
  9845. end;
  9846. procedure TTestModule.TestArray_DynamicAssign;
  9847. begin
  9848. StartProgram(false);
  9849. Add([
  9850. 'type',
  9851. ' TArrayInt = array of longint;',
  9852. ' TArrayArrayInt = array of TArrayInt;',
  9853. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9854. 'begin',
  9855. 'end;',
  9856. 'procedure Fly(var a: TArrayInt);',
  9857. 'begin',
  9858. 'end;',
  9859. 'var',
  9860. ' Arr: TArrayInt;',
  9861. ' Arr2: TArrayArrayInt;',
  9862. 'begin',
  9863. ' arr:=nil;',
  9864. ' arr2:=nil;',
  9865. ' arr2[1]:=nil;',
  9866. ' arr2[2]:=arr;',
  9867. ' Run(arr,arr,arr);',
  9868. ' Fly(arr);',
  9869. ' Run(arr2[4],arr2[5],arr2[6]);',
  9870. ' Fly(arr2[7]);',
  9871. '']);
  9872. ConvertProgram;
  9873. CheckSource('TestArray_DynamicAssign',
  9874. LinesToStr([ // statements
  9875. 'this.Run = function (a, b, c) {',
  9876. '};',
  9877. 'this.Fly = function (a) {',
  9878. '};',
  9879. 'this.Arr = [];',
  9880. 'this.Arr2 = [];',
  9881. '']),
  9882. LinesToStr([ // $mod.$main
  9883. '$mod.Arr = [];',
  9884. '$mod.Arr2 = [];',
  9885. '$mod.Arr2[1] = [];',
  9886. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9887. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9888. '$mod.Fly({',
  9889. ' p: $mod,',
  9890. ' get: function () {',
  9891. ' return this.p.Arr;',
  9892. ' },',
  9893. ' set: function (v) {',
  9894. ' this.p.Arr = v;',
  9895. ' }',
  9896. '});',
  9897. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9898. '$mod.Fly({',
  9899. ' a: 7,',
  9900. ' p: $mod.Arr2,',
  9901. ' get: function () {',
  9902. ' return this.p[this.a];',
  9903. ' },',
  9904. ' set: function (v) {',
  9905. ' this.p[this.a] = v;',
  9906. ' }',
  9907. '});',
  9908. '']));
  9909. end;
  9910. procedure TTestModule.TestArray_StaticInt;
  9911. begin
  9912. StartProgram(false);
  9913. Add('type');
  9914. Add(' TArrayInt = array[2..4] of longint;');
  9915. Add('var');
  9916. Add(' Arr: TArrayInt;');
  9917. Add(' Arr2: TArrayInt = (5,6,7);');
  9918. Add(' i: longint;');
  9919. Add(' b: boolean;');
  9920. Add('begin');
  9921. Add(' arr[2]:=4;');
  9922. Add(' arr[3]:=arr[2]+arr[3];');
  9923. Add(' arr[i]:=5;');
  9924. Add(' arr[arr[i]]:=arr[high(arr)];');
  9925. Add(' i:=low(arr);');
  9926. Add(' i:=high(arr);');
  9927. Add(' b:=arr[2]=arr[3];');
  9928. Add(' arr:=default(TArrayInt);');
  9929. ConvertProgram;
  9930. CheckSource('TestArray_StaticInt',
  9931. LinesToStr([ // statements
  9932. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9933. 'this.Arr2 = [5, 6, 7];',
  9934. 'this.i = 0;',
  9935. 'this.b = false;'
  9936. ]),
  9937. LinesToStr([ // $mod.$main
  9938. '$mod.Arr[0] = 4;',
  9939. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9940. '$mod.Arr[$mod.i-2] = 5;',
  9941. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9942. '$mod.i = 2;',
  9943. '$mod.i = 4;',
  9944. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9945. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9946. '']));
  9947. end;
  9948. procedure TTestModule.TestArray_StaticBool;
  9949. begin
  9950. StartProgram(false);
  9951. Add('type');
  9952. Add(' TBools = array[boolean] of boolean;');
  9953. Add(' TBool2 = array[true..true] of boolean;');
  9954. Add('var');
  9955. Add(' Arr: TBools;');
  9956. Add(' Arr2: TBool2;');
  9957. Add(' Arr3: TBools = (true,false);');
  9958. Add(' b: boolean;');
  9959. Add('begin');
  9960. Add(' b:=low(arr);');
  9961. Add(' b:=high(arr);');
  9962. Add(' arr[true]:=false;');
  9963. Add(' arr[false]:=arr[b] or arr[true];');
  9964. Add(' arr[b]:=true;');
  9965. Add(' arr[arr[b]]:=arr[high(arr)];');
  9966. Add(' b:=arr[false]=arr[true];');
  9967. Add(' b:=low(arr2);');
  9968. Add(' b:=high(arr2);');
  9969. Add(' arr2[true]:=true;');
  9970. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9971. Add(' arr2[b]:=false;');
  9972. ConvertProgram;
  9973. CheckSource('TestArray_StaticBool',
  9974. LinesToStr([ // statements
  9975. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9976. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9977. 'this.Arr3 = [true, false];',
  9978. 'this.b = false;'
  9979. ]),
  9980. LinesToStr([ // $mod.$main
  9981. '$mod.b = false;',
  9982. '$mod.b = true;',
  9983. '$mod.Arr[1] = false;',
  9984. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9985. '$mod.Arr[+$mod.b] = true;',
  9986. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9987. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9988. '$mod.b = true;',
  9989. '$mod.b = true;',
  9990. '$mod.Arr2[0] = true;',
  9991. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9992. '$mod.Arr2[1-$mod.b] = false;',
  9993. '']));
  9994. end;
  9995. procedure TTestModule.TestArray_StaticChar;
  9996. begin
  9997. StartProgram(false);
  9998. Add([
  9999. 'type',
  10000. ' TChars = array[char] of char;',
  10001. ' TChars2 = array[''a''..''z''] of char;',
  10002. 'var',
  10003. ' Arr: TChars;',
  10004. ' Arr2: TChars2;',
  10005. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  10006. ' Arr4: array[11..13] of char = ''pas'';',
  10007. ' Arr5: array[21..22] of char = ''äö'';',
  10008. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10009. ' c: char;',
  10010. ' b: boolean;',
  10011. 'begin',
  10012. ' c:=low(arr);',
  10013. ' c:=high(arr);',
  10014. ' arr[''B'']:=''a'';',
  10015. ' arr[''D'']:=arr[c];',
  10016. ' arr[c]:=arr[''d''];',
  10017. ' arr[arr[c]]:=arr[high(arr)];',
  10018. ' b:=arr[low(arr)]=arr[''e''];',
  10019. ' c:=low(arr2);',
  10020. ' c:=high(arr2);',
  10021. ' arr2[''b'']:=''f'';',
  10022. ' arr2[''a'']:=arr2[c];',
  10023. ' arr2[c]:=arr2[''g''];']);
  10024. ConvertProgram;
  10025. CheckSource('TestArray_StaticChar',
  10026. LinesToStr([ // statements
  10027. 'this.Arr = rtl.arraySetLength(null, "\x00", 65536);',
  10028. 'this.Arr2 = rtl.arraySetLength(null, "\x00", 26);',
  10029. 'this.Arr3 = ["p", "a", "s"];',
  10030. 'this.Arr4 = ["p", "a", "s"];',
  10031. 'this.Arr5 = ["ä", "ö"];',
  10032. 'this.Arr6 = ["ä", "ö"];',
  10033. 'this.c = "\x00";',
  10034. 'this.b = false;',
  10035. '']),
  10036. LinesToStr([ // $mod.$main
  10037. '$mod.c = "\x00";',
  10038. '$mod.c = "\uFFFF";',
  10039. '$mod.Arr[66] = "a";',
  10040. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10041. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10042. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10043. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10044. '$mod.c = "a";',
  10045. '$mod.c = "z";',
  10046. '$mod.Arr2[1] = "f";',
  10047. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10048. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10049. '']));
  10050. end;
  10051. procedure TTestModule.TestArray_StaticMultiDim;
  10052. begin
  10053. StartProgram(false);
  10054. Add([
  10055. 'type',
  10056. ' TArrayInt = array[1..3] of longint;',
  10057. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10058. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10059. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10060. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10061. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10062. 'var',
  10063. ' Arr: TArrayInt;',
  10064. ' Arr2: TArrayArrayInt;',
  10065. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10066. ' Arr4: TArrayArrayInt;',
  10067. ' ArrDim2: TArrayDim2Int;',
  10068. ' ArrDim3: TArrayDim3Int;',
  10069. ' ArrDim4: TArrayDim4Int;',
  10070. ' i: longint;',
  10071. 'begin',
  10072. ' i:=low(arr);',
  10073. ' i:=low(arr2);',
  10074. ' i:=low(arr2[5]);',
  10075. ' i:=high(arr);',
  10076. ' i:=high(arr2);',
  10077. ' i:=high(arr2[6]);',
  10078. ' arr2[5]:=arr;',
  10079. ' arr2[6][2]:=i;',
  10080. ' i:=arr2[6][3];',
  10081. ' arr2[6,3]:=i;',
  10082. ' i:=arr2[5,2];',
  10083. ' arr2:=arr2;',// clone multi dim static array
  10084. ' arr3:=arr3;',// clone anonymous multi dim static array
  10085. ' arr4:=arr4;',
  10086. ' Arr:=Arr;',
  10087. ' ArrDim2:=ArrDim2;',
  10088. ' ArrDim3:=ArrDim3;',
  10089. ' ArrDim4:=ArrDim4;',
  10090. '']);
  10091. ConvertProgram;
  10092. CheckSource('TestArray_StaticMultiDim',
  10093. LinesToStr([ // statements
  10094. 'this.TArrayArrayInt$clone = function (a) {',
  10095. ' var b = [];',
  10096. ' b.length = 2;',
  10097. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10098. ' return b;',
  10099. '};',
  10100. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10101. ' var b = [];',
  10102. ' b.length = 2;',
  10103. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10104. ' return b;',
  10105. '};',
  10106. 'this.TArrayDim2Int$clone = function (a) {',
  10107. ' var b = [];',
  10108. ' b.length = 2;',
  10109. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10110. ' return b;',
  10111. '};',
  10112. 'this.TArrayDim3Int$clone = function (a) {',
  10113. ' var b = [];',
  10114. ' b.length = 2;',
  10115. ' for (var c = 0; c < 2; c++) {',
  10116. ' var d = b[c] = [];',
  10117. ' d.length = 3;',
  10118. ' var e = a[c];',
  10119. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10120. ' };',
  10121. ' return b;',
  10122. '};',
  10123. 'this.TArrayDim4Int$clone = function (a) {',
  10124. ' var b = [];',
  10125. ' b.length = 2;',
  10126. ' for (var c = 0; c < 2; c++) {',
  10127. ' var d = b[c] = [];',
  10128. ' d.length = 3;',
  10129. ' var e = a[c];',
  10130. ' for (var f = 0; f < 3; f++) {',
  10131. ' var g = d[f] = [];',
  10132. ' g.length = 4;',
  10133. ' var h = e[f];',
  10134. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10135. ' };',
  10136. ' };',
  10137. ' return b;',
  10138. '};',
  10139. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10140. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10141. 'this.Arr3$a$clone = function (a) {',
  10142. ' var b = [];',
  10143. ' b.length = 2;',
  10144. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10145. ' return b;',
  10146. '};',
  10147. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10148. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10149. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10150. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10151. 'this.ArrDim4 = rtl.arraySetLength(',
  10152. ' null,',
  10153. ' 0,',
  10154. ' 2,',
  10155. ' 3,',
  10156. ' 4,',
  10157. ' 5',
  10158. ');',
  10159. 'this.i = 0;'
  10160. ]),
  10161. LinesToStr([ // $mod.$main
  10162. '$mod.i = 1;',
  10163. '$mod.i = 5;',
  10164. '$mod.i = 1;',
  10165. '$mod.i = 3;',
  10166. '$mod.i = 6;',
  10167. '$mod.i = 3;',
  10168. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10169. '$mod.Arr2[1][1] = $mod.i;',
  10170. '$mod.i = $mod.Arr2[1][2];',
  10171. '$mod.Arr2[1][2] = $mod.i;',
  10172. '$mod.i = $mod.Arr2[0][1];',
  10173. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10174. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10175. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10176. '$mod.Arr = $mod.Arr.slice(0);',
  10177. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10178. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10179. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10180. '']));
  10181. end;
  10182. procedure TTestModule.TestArray_StaticInFunction;
  10183. begin
  10184. StartProgram(false);
  10185. Add([
  10186. 'const TArrayInt = 3;',
  10187. 'const TArrayArrayInt = 4;',
  10188. 'procedure DoIt;',
  10189. 'type',
  10190. ' TArrayInt = array[1..3] of longint;',
  10191. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10192. 'var',
  10193. ' Arr: TArrayInt;',
  10194. ' Arr2: TArrayArrayInt;',
  10195. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10196. ' i: longint;',
  10197. 'begin',
  10198. ' arr2[5]:=arr;',
  10199. ' arr2:=arr2;',// clone multi dim static array
  10200. ' arr3:=arr3;',// clone multi dim anonymous static array
  10201. 'end;',
  10202. 'begin',
  10203. '']);
  10204. ConvertProgram;
  10205. CheckSource('TestArray_StaticInFunction',
  10206. LinesToStr([ // statements
  10207. 'this.TArrayInt = 3;',
  10208. 'this.TArrayArrayInt = 4;',
  10209. 'var TArrayArrayInt$1$clone = function (a) {',
  10210. ' var b = [];',
  10211. ' b.length = 2;',
  10212. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10213. ' return b;',
  10214. '};',
  10215. 'var Arr3$a$clone = function (a) {',
  10216. ' var b = [];',
  10217. ' b.length = 2;',
  10218. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10219. ' return b;',
  10220. '};',
  10221. 'this.DoIt = function () {',
  10222. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10223. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10224. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10225. ' var i = 0;',
  10226. ' Arr2[0] = Arr.slice(0);',
  10227. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10228. ' Arr3 = Arr3$a$clone(Arr3);',
  10229. '};',
  10230. '']),
  10231. LinesToStr([ // $mod.$main
  10232. '']));
  10233. end;
  10234. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10235. begin
  10236. StartProgram(false);
  10237. Add([
  10238. 'type',
  10239. ' TArrayInt = array[1..3,1..2] of longint;',
  10240. 'var',
  10241. ' a,b: TArrayInt;',
  10242. 'begin',
  10243. ' if a=b then ;',
  10244. '']);
  10245. SetExpectedPasResolverError('compare static array is not supported',
  10246. nXIsNotSupported);
  10247. ConvertProgram;
  10248. end;
  10249. procedure TTestModule.TestArrayOfRecord;
  10250. begin
  10251. StartProgram(false);
  10252. Add([
  10253. 'type',
  10254. ' TRec = record',
  10255. ' Int: longint;',
  10256. ' end;',
  10257. ' TArrayRec = array of TRec;',
  10258. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10259. 'begin',
  10260. 'end;',
  10261. 'var',
  10262. ' Arr: TArrayRec;',
  10263. ' r: TRec;',
  10264. ' i: longint;',
  10265. 'begin',
  10266. ' SetLength(arr,3);',
  10267. ' arr[0].int:=4;',
  10268. ' arr[1].int:=length(arr)+arr[2].int;',
  10269. ' arr[arr[i].int].int:=arr[5].int;',
  10270. ' arr[7]:=r;',
  10271. ' r:=arr[8];',
  10272. ' i:=low(arr);',
  10273. ' i:=high(arr);',
  10274. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10275. ConvertProgram;
  10276. CheckSource('TestArrayOfRecord',
  10277. LinesToStr([ // statements
  10278. 'rtl.recNewT(this, "TRec", function () {',
  10279. ' this.Int = 0;',
  10280. ' this.$eq = function (b) {',
  10281. ' return this.Int === b.Int;',
  10282. ' };',
  10283. ' this.$assign = function (s) {',
  10284. ' this.Int = s.Int;',
  10285. ' return this;',
  10286. ' };',
  10287. '});',
  10288. 'this.DoIt = function (vd, vc, vv) {',
  10289. '};',
  10290. 'this.Arr = [];',
  10291. 'this.r = this.TRec.$new();',
  10292. 'this.i = 0;'
  10293. ]),
  10294. LinesToStr([ // $mod.$main
  10295. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10296. '$mod.Arr[0].Int = 4;',
  10297. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10298. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10299. '$mod.Arr[7].$assign($mod.r);',
  10300. '$mod.r.$assign($mod.Arr[8]);',
  10301. '$mod.i = 0;',
  10302. '$mod.i = rtl.length($mod.Arr)-1;',
  10303. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10304. '']));
  10305. end;
  10306. procedure TTestModule.TestArray_StaticRecord;
  10307. begin
  10308. StartProgram(false);
  10309. Add([
  10310. 'type',
  10311. ' TRec = record',
  10312. ' Int: longint;',
  10313. ' end;',
  10314. ' TArrayRec = array[1..2] of TRec;',
  10315. 'var',
  10316. ' Arr: TArrayRec;',
  10317. 'begin',
  10318. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10319. '']);
  10320. ConvertProgram;
  10321. CheckSource('TestArray_StaticRecord',
  10322. LinesToStr([ // statements
  10323. 'rtl.recNewT(this, "TRec", function () {',
  10324. ' this.Int = 0;',
  10325. ' this.$eq = function (b) {',
  10326. ' return this.Int === b.Int;',
  10327. ' };',
  10328. ' this.$assign = function (s) {',
  10329. ' this.Int = s.Int;',
  10330. ' return this;',
  10331. ' };',
  10332. '});',
  10333. 'this.TArrayRec$clone = function (a) {',
  10334. ' var b = [];',
  10335. ' b.length = 2;',
  10336. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10337. ' return b;',
  10338. '};',
  10339. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10340. '']),
  10341. LinesToStr([ // $mod.$main
  10342. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10343. end;
  10344. procedure TTestModule.TestArrayOfSet;
  10345. begin
  10346. StartProgram(false);
  10347. Add([
  10348. 'type',
  10349. ' TFlag = (big,small);',
  10350. ' TSetOfFlag = set of tflag;',
  10351. ' TArrayFlag = array of TSetOfFlag;',
  10352. 'procedure DoIt(const a: Tarrayflag);',
  10353. 'begin',
  10354. 'end;',
  10355. 'var',
  10356. ' f: TFlag;',
  10357. ' s: TSetOfFlag;',
  10358. ' Arr: TArrayFlag;',
  10359. ' i: longint;',
  10360. 'begin',
  10361. ' SetLength(arr,3);',
  10362. ' arr[0]:=s;',
  10363. ' arr[1]:=[big];',
  10364. ' arr[2]:=[big]+s;',
  10365. ' arr[3]:=s+[big];',
  10366. ' arr[4]:=arr[5];',
  10367. ' s:=arr[6];',
  10368. ' i:=low(arr);',
  10369. ' i:=high(arr);',
  10370. ' DoIt(arr);',
  10371. ' DoIt([s]);',
  10372. ' DoIt([[],s]);',
  10373. ' DoIt([s,[]]);',
  10374. '']);
  10375. ConvertProgram;
  10376. CheckSource('TestArrayOfSet',
  10377. LinesToStr([ // statements
  10378. 'this.TFlag = {',
  10379. ' "0": "big",',
  10380. ' big: 0,',
  10381. ' "1": "small",',
  10382. ' small: 1',
  10383. '};',
  10384. 'this.DoIt = function (a) {',
  10385. '};',
  10386. 'this.f = 0;',
  10387. 'this.s = {};',
  10388. 'this.Arr = [];',
  10389. 'this.i = 0;',
  10390. '']),
  10391. LinesToStr([ // $mod.$main
  10392. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10393. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10394. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10395. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10396. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10397. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10398. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10399. '$mod.i = 0;',
  10400. '$mod.i = rtl.length($mod.Arr) - 1;',
  10401. '$mod.DoIt($mod.Arr);',
  10402. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10403. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10404. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10405. '']));
  10406. end;
  10407. procedure TTestModule.TestArray_DynAsParam;
  10408. begin
  10409. StartProgram(false);
  10410. Add([
  10411. 'type integer = longint;',
  10412. 'type TArrInt = array of integer;',
  10413. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10414. 'var vJ: TArrInt;',
  10415. 'begin',
  10416. ' vg:=vg;',
  10417. ' vj:=vh;',
  10418. ' vi:=vi;',
  10419. ' doit(vg,vg,vg);',
  10420. ' doit(vh,vh,vj);',
  10421. ' doit(vi,vi,vi);',
  10422. ' doit(vj,vj,vj);',
  10423. 'end;',
  10424. 'var i: TArrInt;',
  10425. 'begin',
  10426. ' doit(i,i,i);']);
  10427. ConvertProgram;
  10428. CheckSource('TestArray_DynAsParams',
  10429. LinesToStr([ // statements
  10430. 'this.DoIt = function (vG,vH,vI) {',
  10431. ' var vJ = [];',
  10432. ' vG = rtl.arrayRef(vG);',
  10433. ' vJ = rtl.arrayRef(vH);',
  10434. ' vI.set(rtl.arrayRef(vI.get()));',
  10435. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10436. ' get: function () {',
  10437. ' return vG;',
  10438. ' },',
  10439. ' set: function (v) {',
  10440. ' vG = v;',
  10441. ' }',
  10442. ' });',
  10443. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10444. ' get: function () {',
  10445. ' return vJ;',
  10446. ' },',
  10447. ' set: function (v) {',
  10448. ' vJ = v;',
  10449. ' }',
  10450. ' });',
  10451. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10452. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10453. ' get: function () {',
  10454. ' return vJ;',
  10455. ' },',
  10456. ' set: function (v) {',
  10457. ' vJ = v;',
  10458. ' }',
  10459. ' });',
  10460. '};',
  10461. 'this.i = [];'
  10462. ]),
  10463. LinesToStr([
  10464. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10465. ' p: $mod,',
  10466. ' get: function () {',
  10467. ' return this.p.i;',
  10468. ' },',
  10469. ' set: function (v) {',
  10470. ' this.p.i = v;',
  10471. ' }',
  10472. '});'
  10473. ]));
  10474. end;
  10475. procedure TTestModule.TestArray_StaticAsParam;
  10476. begin
  10477. StartProgram(false);
  10478. Add([
  10479. 'type integer = longint;',
  10480. 'type TArrInt = array[1..2] of integer;',
  10481. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10482. 'var vJ: TArrInt;',
  10483. 'begin',
  10484. ' vg:=vg;',
  10485. ' vj:=vh;',
  10486. ' vi:=vi;',
  10487. ' doit(vg,vg,vg);',
  10488. ' doit(vh,vh,vj);',
  10489. ' doit(vi,vi,vi);',
  10490. ' doit(vj,vj,vj);',
  10491. 'end;',
  10492. 'var i: TArrInt;',
  10493. 'begin',
  10494. ' doit(i,i,i);']);
  10495. ConvertProgram;
  10496. CheckSource('TestArray_StaticAsParams',
  10497. LinesToStr([ // statements
  10498. 'this.DoIt = function (vG,vH,vI) {',
  10499. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10500. ' vG = vG.slice(0);',
  10501. ' vJ = vH.slice(0);',
  10502. ' vI.set(vI.get().slice(0));',
  10503. ' $mod.DoIt(vG.slice(0), vG, {',
  10504. ' get: function () {',
  10505. ' return vG;',
  10506. ' },',
  10507. ' set: function (v) {',
  10508. ' vG = v;',
  10509. ' }',
  10510. ' });',
  10511. ' $mod.DoIt(vH.slice(0), vH, {',
  10512. ' get: function () {',
  10513. ' return vJ;',
  10514. ' },',
  10515. ' set: function (v) {',
  10516. ' vJ = v;',
  10517. ' }',
  10518. ' });',
  10519. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10520. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10521. ' get: function () {',
  10522. ' return vJ;',
  10523. ' },',
  10524. ' set: function (v) {',
  10525. ' vJ = v;',
  10526. ' }',
  10527. ' });',
  10528. '};',
  10529. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10530. ]),
  10531. LinesToStr([
  10532. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10533. ' p: $mod,',
  10534. ' get: function () {',
  10535. ' return this.p.i;',
  10536. ' },',
  10537. ' set: function (v) {',
  10538. ' this.p.i = v;',
  10539. ' }',
  10540. '});'
  10541. ]));
  10542. end;
  10543. procedure TTestModule.TestArrayElement_AsParams;
  10544. begin
  10545. StartProgram(false);
  10546. Add('type integer = longint;');
  10547. Add('type TArrayInt = array of integer;');
  10548. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10549. Add('var vJ: tarrayint;');
  10550. Add('begin');
  10551. Add(' vi:=vi;');
  10552. Add(' doit(vi,vi,vi);');
  10553. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10554. Add('end;');
  10555. Add('var a: TArrayInt;');
  10556. Add('begin');
  10557. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10558. ConvertProgram;
  10559. CheckSource('TestArrayElement_AsParams',
  10560. LinesToStr([ // statements
  10561. 'this.DoIt = function (vG,vH,vI) {',
  10562. ' var vJ = [];',
  10563. ' vI.set(vI.get());',
  10564. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10565. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10566. ' a:1+3,',
  10567. ' p:vJ,',
  10568. ' get: function () {',
  10569. ' return this.p[this.a];',
  10570. ' },',
  10571. ' set: function (v) {',
  10572. ' this.p[this.a] = v;',
  10573. ' }',
  10574. ' });',
  10575. '};',
  10576. 'this.a = [];'
  10577. ]),
  10578. LinesToStr([
  10579. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10580. ' a: 1+6,',
  10581. ' p: $mod.a,',
  10582. ' get: function () {',
  10583. ' return this.p[this.a];',
  10584. ' },',
  10585. ' set: function (v) {',
  10586. ' this.p[this.a] = v;',
  10587. ' }',
  10588. '});'
  10589. ]));
  10590. end;
  10591. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10592. begin
  10593. StartProgram(false);
  10594. Add('type Integer = longint;');
  10595. Add('type TArrayInt = array of integer;');
  10596. Add('function GetArr(vB: integer = 0): tarrayint;');
  10597. Add('begin');
  10598. Add('end;');
  10599. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10600. Add('begin');
  10601. Add('end;');
  10602. Add('begin');
  10603. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10604. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10605. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10606. ConvertProgram;
  10607. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10608. LinesToStr([ // statements
  10609. 'this.GetArr = function (vB) {',
  10610. ' var Result = [];',
  10611. ' return Result;',
  10612. '};',
  10613. 'this.DoIt = function (vG,vH,vI) {',
  10614. '};'
  10615. ]),
  10616. LinesToStr([
  10617. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10618. ' a: 1+3,',
  10619. ' p: $mod.GetArr(0),',
  10620. ' get: function () {',
  10621. ' return this.p[this.a];',
  10622. ' },',
  10623. ' set: function (v) {',
  10624. ' this.p[this.a] = v;',
  10625. ' }',
  10626. '});',
  10627. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10628. ' a: 2+3,',
  10629. ' p: $mod.GetArr(0),',
  10630. ' get: function () {',
  10631. ' return this.p[this.a];',
  10632. ' },',
  10633. ' set: function (v) {',
  10634. ' this.p[this.a] = v;',
  10635. ' }',
  10636. '});',
  10637. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10638. ' a: 3+3,',
  10639. ' p: $mod.GetArr(9),',
  10640. ' get: function () {',
  10641. ' return this.p[this.a];',
  10642. ' },',
  10643. ' set: function (v) {',
  10644. ' this.p[this.a] = v;',
  10645. ' }',
  10646. '});',
  10647. '']));
  10648. end;
  10649. procedure TTestModule.TestArrayEnumTypeRange;
  10650. begin
  10651. StartProgram(false);
  10652. Add([
  10653. 'type',
  10654. ' TEnum = (red,blue);',
  10655. ' TEnumArray = array[TEnum] of longint;',
  10656. 'var',
  10657. ' e: TEnum;',
  10658. ' i: longint;',
  10659. ' a: TEnumArray;',
  10660. ' numbers: TEnumArray = (1,2);',
  10661. ' names: array[TEnum] of string = (''red'',''blue'');',
  10662. 'begin',
  10663. ' e:=low(a);',
  10664. ' e:=high(a);',
  10665. ' i:=a[red];',
  10666. ' a[e]:=a[e];']);
  10667. ConvertProgram;
  10668. CheckSource('TestArrayEnumTypeRange',
  10669. LinesToStr([ // statements
  10670. ' this.TEnum = {',
  10671. ' "0": "red",',
  10672. ' red: 0,',
  10673. ' "1": "blue",',
  10674. ' blue: 1',
  10675. '};',
  10676. 'this.e = 0;',
  10677. 'this.i = 0;',
  10678. 'this.a = rtl.arraySetLength(null,0,2);',
  10679. 'this.numbers = [1, 2];',
  10680. 'this.names = ["red", "blue"];',
  10681. '']),
  10682. LinesToStr([ // $mod.$main
  10683. '$mod.e = $mod.TEnum.red;',
  10684. '$mod.e = $mod.TEnum.blue;',
  10685. '$mod.i = $mod.a[$mod.TEnum.red];',
  10686. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10687. '']));
  10688. end;
  10689. procedure TTestModule.TestArray_SetLengthOutArg;
  10690. begin
  10691. StartProgram(false);
  10692. Add([
  10693. 'type TArrInt = array of longint;',
  10694. 'procedure DoIt(out a: TArrInt);',
  10695. 'begin',
  10696. ' SetLength(a,2);',
  10697. 'end;',
  10698. 'begin',
  10699. '']);
  10700. ConvertProgram;
  10701. CheckSource('TestArray_SetLengthOutArg',
  10702. LinesToStr([ // statements
  10703. 'this.DoIt = function (a) {',
  10704. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10705. '};',
  10706. '']),
  10707. LinesToStr([
  10708. '']));
  10709. end;
  10710. procedure TTestModule.TestArray_SetLengthProperty;
  10711. begin
  10712. StartProgram(false);
  10713. Add('type');
  10714. Add(' TArrInt = array of longint;');
  10715. Add(' TObject = class');
  10716. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10717. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10718. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10719. Add(' end;');
  10720. Add('var Obj: TObject;');
  10721. Add('begin');
  10722. Add(' SetLength(Obj.Colors,2);');
  10723. ConvertProgram;
  10724. CheckSource('TestArray_SetLengthProperty',
  10725. LinesToStr([ // statements
  10726. 'rtl.createClass(this, "TObject", null, function () {',
  10727. ' this.$init = function () {',
  10728. ' };',
  10729. ' this.$final = function () {',
  10730. ' };',
  10731. '});',
  10732. 'this.Obj = null;',
  10733. '']),
  10734. LinesToStr([
  10735. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10736. '']));
  10737. end;
  10738. procedure TTestModule.TestArray_SetLengthMultiDim;
  10739. begin
  10740. StartProgram(false);
  10741. Add([
  10742. 'type',
  10743. ' TArrArrInt = array of array of longint;',
  10744. ' TArrStaInt = array of array[1..2] of longint;',
  10745. 'var',
  10746. ' a: TArrArrInt;',
  10747. ' b: TArrStaInt;',
  10748. 'begin',
  10749. ' SetLength(a,2);',
  10750. ' SetLength(a,3,4);',
  10751. ' SetLength(b,5);',
  10752. '']);
  10753. ConvertProgram;
  10754. CheckSource('TestArray_SetLengthMultiDim',
  10755. LinesToStr([ // statements
  10756. 'this.a = [];',
  10757. 'this.b = [];',
  10758. '']),
  10759. LinesToStr([
  10760. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10761. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10762. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10763. '']));
  10764. end;
  10765. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10766. begin
  10767. StartProgram(false);
  10768. Add([
  10769. 'type',
  10770. ' TStaArr1 = array[1..3] of boolean;',
  10771. //' TStaArr2 = array[5..6] of TStaArr1;',
  10772. ' TDynArr1StaArr1 = array of TStaArr1;',
  10773. //' TDynArr1StaArr2 = array of TStaArr2;',
  10774. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10775. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10776. 'var',
  10777. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10778. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10779. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10780. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10781. 'begin',
  10782. ' SetLength(DynArr1StaArr1,11);',
  10783. ' SetLength(DynArr2StaArr1,12);',
  10784. ' SetLength(DynArr2StaArr1[13],14);',
  10785. ' SetLength(DynArr2StaArr1,15,16);',
  10786. //' SetLength(DynArr1StaArr2,21);',
  10787. //' SetLength(DynArr2StaArr2,22);',
  10788. //' SetLength(DynArr2StaArr2[23],24);',
  10789. //' SetLength(DynArr2StaArr2,25,26);',
  10790. '']);
  10791. ConvertProgram;
  10792. CheckSource('TestArray_DynOfStatic',
  10793. LinesToStr([ // statements
  10794. 'this.DynArr1StaArr1 = [];',
  10795. 'this.DynArr2StaArr1 = [];',
  10796. '']),
  10797. LinesToStr([ // $mod.$main
  10798. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10799. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10800. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10801. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10802. ' $mod.DynArr2StaArr1,',
  10803. ' false,',
  10804. ' 15,',
  10805. ' 16,',
  10806. ' "s",',
  10807. ' 3',
  10808. ');',
  10809. '']));
  10810. end;
  10811. procedure TTestModule.TestArray_OpenArrayOfString;
  10812. begin
  10813. StartProgram(false);
  10814. Add('procedure DoIt(const a: array of String);');
  10815. Add('var');
  10816. Add(' i: longint;');
  10817. Add(' s: string;');
  10818. Add('begin');
  10819. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10820. Add('end;');
  10821. Add('var s: string;');
  10822. Add('begin');
  10823. Add(' DoIt([]);');
  10824. Add(' DoIt([s,''foo'','''',s+s]);');
  10825. ConvertProgram;
  10826. CheckSource('TestArray_OpenArrayOfString',
  10827. LinesToStr([ // statements
  10828. 'this.DoIt = function (a) {',
  10829. ' var i = 0;',
  10830. ' var s = "";',
  10831. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10832. ' i = $l;',
  10833. ' s = a[rtl.length(a) - i - 1];',
  10834. ' };',
  10835. '};',
  10836. 'this.s = "";',
  10837. '']),
  10838. LinesToStr([
  10839. '$mod.DoIt([]);',
  10840. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10841. '']));
  10842. end;
  10843. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10844. begin
  10845. StartProgram(false);
  10846. Add([
  10847. 'type TArr = array of char;',
  10848. 'var',
  10849. ' c: char;',
  10850. ' s: string;',
  10851. ' a: TArr;',
  10852. 'procedure Run(const a: array of char);',
  10853. 'begin',
  10854. ' Run(c);',
  10855. ' Run(s);',
  10856. 'end;',
  10857. 'begin',
  10858. ' a:=c;',
  10859. ' a:=s;',
  10860. ' a:=#13;',
  10861. ' a:=''Foo'';',
  10862. ' Run(c);',
  10863. ' Run(s);',
  10864. '']);
  10865. ConvertProgram;
  10866. CheckSource('TestArray_ArrayOfCharAssignString',
  10867. LinesToStr([ // statements
  10868. 'this.c = "\x00";',
  10869. 'this.s = "";',
  10870. 'this.a = [];',
  10871. 'this.Run = function (a) {',
  10872. ' $mod.Run($mod.c.split(""));',
  10873. ' $mod.Run($mod.s.split(""));',
  10874. '};',
  10875. '']),
  10876. LinesToStr([
  10877. '$mod.a = $mod.c.split("");',
  10878. '$mod.a = $mod.s.split("");',
  10879. '$mod.a = "\r".split("");',
  10880. '$mod.a = "Foo".split("");',
  10881. '$mod.Run($mod.c.split(""));',
  10882. '$mod.Run($mod.s.split(""));',
  10883. '']));
  10884. end;
  10885. procedure TTestModule.TestArray_ConstRef;
  10886. begin
  10887. StartProgram(false);
  10888. Add([
  10889. 'type TArr = array of word;',
  10890. 'procedure Run(constref a: TArr);',
  10891. 'begin',
  10892. 'end;',
  10893. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10894. 'var l: TArr;',
  10895. 'begin',
  10896. ' Run(l);',
  10897. ' Run(a);',
  10898. ' Run(b);',
  10899. ' Run(c);',
  10900. ' Run(d);',
  10901. ' Run(e);',
  10902. 'end;',
  10903. 'begin',
  10904. '']);
  10905. ConvertProgram;
  10906. CheckResolverUnexpectedHints();
  10907. CheckSource('TestArray_ConstRef',
  10908. LinesToStr([ // statements
  10909. 'this.Run = function (a) {',
  10910. '};',
  10911. 'this.Fly = function (a, b, c, d, e) {',
  10912. ' var l = [];',
  10913. ' $mod.Run(l);',
  10914. ' $mod.Run(a);',
  10915. ' $mod.Run(b.get());',
  10916. ' $mod.Run(c.get());',
  10917. ' $mod.Run(d);',
  10918. ' $mod.Run(e);',
  10919. '};',
  10920. '']),
  10921. LinesToStr([
  10922. '']));
  10923. end;
  10924. procedure TTestModule.TestArray_Concat;
  10925. begin
  10926. StartProgram(false);
  10927. Add([
  10928. 'type',
  10929. ' integer = longint;',
  10930. ' TFlag = (big,small);',
  10931. ' TFlags = set of TFlag;',
  10932. ' TRec = record',
  10933. ' i: integer;',
  10934. ' end;',
  10935. ' TArrInt = array of integer;',
  10936. ' TArrRec = array of TRec;',
  10937. ' TArrFlag = array of TFlag;',
  10938. ' TArrSet = array of TFlags;',
  10939. ' TArrJSValue = array of jsvalue;',
  10940. 'var',
  10941. ' ArrInt1, ArrInt2: tarrint;',
  10942. ' ArrRec1, ArrRec2: tarrrec;',
  10943. ' ArrFlag1, ArrFlag2: tarrflag;',
  10944. ' ArrSet1, ArrSet2: tarrset;',
  10945. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10946. 'begin',
  10947. ' arrint1:=concat(arrint2);',
  10948. ' arrint1:=concat(arrint2,arrint2);',
  10949. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10950. ' arrrec1:=concat(arrrec2);',
  10951. ' arrrec1:=concat(arrrec2,arrrec2);',
  10952. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10953. ' arrset1:=concat(arrset2);',
  10954. ' arrset1:=concat(arrset2,arrset2);',
  10955. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10956. ' arrjsvalue1:=concat(arrjsvalue2);',
  10957. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10958. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10959. ' arrint1:=concat([1],arrint2);',
  10960. ' arrflag1:=concat([big]);',
  10961. ' arrflag1:=concat([big],arrflag2);',
  10962. ' arrflag1:=concat(arrflag2,[small]);',
  10963. '']);
  10964. ConvertProgram;
  10965. CheckSource('TestArray_Concat',
  10966. LinesToStr([ // statements
  10967. 'this.TFlag = {',
  10968. ' "0": "big",',
  10969. ' big: 0,',
  10970. ' "1": "small",',
  10971. ' small: 1',
  10972. '};',
  10973. 'rtl.recNewT(this, "TRec", function () {',
  10974. ' this.i = 0;',
  10975. ' this.$eq = function (b) {',
  10976. ' return this.i === b.i;',
  10977. ' };',
  10978. ' this.$assign = function (s) {',
  10979. ' this.i = s.i;',
  10980. ' return this;',
  10981. ' };',
  10982. '});',
  10983. 'this.ArrInt1 = [];',
  10984. 'this.ArrInt2 = [];',
  10985. 'this.ArrRec1 = [];',
  10986. 'this.ArrRec2 = [];',
  10987. 'this.ArrFlag1 = [];',
  10988. 'this.ArrFlag2 = [];',
  10989. 'this.ArrSet1 = [];',
  10990. 'this.ArrSet2 = [];',
  10991. 'this.ArrJSValue1 = [];',
  10992. 'this.ArrJSValue2 = [];',
  10993. '']),
  10994. LinesToStr([ // $mod.$main
  10995. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  10996. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  10997. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  10998. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  10999. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  11000. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  11001. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  11002. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  11003. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  11004. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  11005. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  11006. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  11007. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11008. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11009. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11010. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11011. '']));
  11012. end;
  11013. procedure TTestModule.TestArray_Concat_Append;
  11014. begin
  11015. StartProgram(false);
  11016. Add([
  11017. 'type',
  11018. ' integer = longint;',
  11019. ' TFlag = (big,small);',
  11020. ' TFlags = set of TFlag;',
  11021. ' TRec = record',
  11022. ' i: integer;',
  11023. ' end;',
  11024. ' TArrInt = array of integer;',
  11025. ' TArrRec = array of TRec;',
  11026. ' TArrFlag = array of TFlag;',
  11027. ' TArrSet = array of TFlags;',
  11028. ' TArrJSValue = array of jsvalue;',
  11029. 'var',
  11030. ' ArrInt: tarrint;',
  11031. ' ArrRec: tarrrec;',
  11032. ' ArrFlag: tarrflag;',
  11033. ' ArrSet: tarrset;',
  11034. ' ArrJSValue: tarrjsvalue;',
  11035. ' r: TRec;',
  11036. ' f: TFlags;',
  11037. 'begin',
  11038. ' // append',
  11039. ' arrint:=concat(arrint);',
  11040. ' arrint:=concat(arrint,[2]);',
  11041. ' arrint:=concat(arrint,[3,4]);',
  11042. ' arrrec:=concat(arrrec);',
  11043. ' arrrec:=concat(arrrec,[r]);',
  11044. ' arrrec:=concat(arrrec,[r,r]);',
  11045. ' arrset:=concat(arrset);',
  11046. ' arrset:=concat(arrset,[f]);',
  11047. ' arrset:=concat(arrset,[f,f]);',
  11048. ' arrjsvalue:=concat(arrjsvalue);',
  11049. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11050. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11051. ' arrflag:=concat(arrflag);',
  11052. ' arrflag:=concat(arrflag,[small]);',
  11053. ' arrflag:=concat(arrflag,[small,big]);',
  11054. '']);
  11055. ConvertProgram;
  11056. CheckSource('TestArray_Concat_Append',
  11057. LinesToStr([ // statements
  11058. 'this.TFlag = {',
  11059. ' "0": "big",',
  11060. ' big: 0,',
  11061. ' "1": "small",',
  11062. ' small: 1',
  11063. '};',
  11064. 'rtl.recNewT(this, "TRec", function () {',
  11065. ' this.i = 0;',
  11066. ' this.$eq = function (b) {',
  11067. ' return this.i === b.i;',
  11068. ' };',
  11069. ' this.$assign = function (s) {',
  11070. ' this.i = s.i;',
  11071. ' return this;',
  11072. ' };',
  11073. '});',
  11074. 'this.ArrInt = [];',
  11075. 'this.ArrRec = [];',
  11076. 'this.ArrFlag = [];',
  11077. 'this.ArrSet = [];',
  11078. 'this.ArrJSValue = [];',
  11079. 'this.r = this.TRec.$new();',
  11080. 'this.f = {};',
  11081. '']),
  11082. LinesToStr([ // $mod.$main
  11083. '$mod.ArrInt = $mod.ArrInt;',
  11084. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11085. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11086. '$mod.ArrRec = $mod.ArrRec;',
  11087. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11088. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11089. '$mod.ArrSet = $mod.ArrSet;',
  11090. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11091. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11092. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11093. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11094. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11095. '$mod.ArrFlag = $mod.ArrFlag;',
  11096. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11097. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11098. '']));
  11099. end;
  11100. procedure TTestModule.TestArray_Concat_Append_Var;
  11101. begin
  11102. StartProgram(false);
  11103. Add([
  11104. 'type',
  11105. ' TArrInt = array of word;',
  11106. '',
  11107. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11108. 'begin',
  11109. ' a:=concat(a,[2]);',
  11110. ' b:=concat(b,[2]);',
  11111. 'end;',
  11112. 'var',
  11113. ' ArrInt: tarrint;',
  11114. 'begin',
  11115. ' Fly(ArrInt,ArrInt);',
  11116. '']);
  11117. ConvertProgram;
  11118. CheckSource('TestArray_Concat_Append_Var',
  11119. LinesToStr([ // statements
  11120. 'this.Fly = function (a, b) {',
  11121. ' a = rtl.arrayPushN(a, 2);',
  11122. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11123. '};',
  11124. 'this.ArrInt = [];',
  11125. '']),
  11126. LinesToStr([ // $mod.$main
  11127. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11128. ' p: $mod,',
  11129. ' get: function () {',
  11130. ' return this.p.ArrInt;',
  11131. ' },',
  11132. ' set: function (v) {',
  11133. ' this.p.ArrInt = v;',
  11134. ' }',
  11135. '});',
  11136. '']));
  11137. end;
  11138. procedure TTestModule.TestArray_Copy;
  11139. begin
  11140. StartProgram(false);
  11141. Add([
  11142. 'type',
  11143. ' integer = longint;',
  11144. ' TFlag = (big,small);',
  11145. ' TFlags = set of TFlag;',
  11146. ' TRec = record',
  11147. ' i: integer;',
  11148. ' end;',
  11149. ' TArrInt = array of integer;',
  11150. ' TArrRec = array of TRec;',
  11151. ' TArrSet = array of TFlags;',
  11152. ' TArrJSValue = array of jsvalue;',
  11153. 'var',
  11154. ' ArrInt: tarrint;',
  11155. ' ArrRec: tarrrec;',
  11156. ' ArrSet: tarrset;',
  11157. ' ArrJSValue: tarrjsvalue;',
  11158. 'begin',
  11159. ' arrint:=copy(arrint);',
  11160. ' arrint:=copy(arrint,2);',
  11161. ' arrint:=copy(arrint,3,4);',
  11162. ' arrint:=copy([1,1],1,2);',
  11163. ' arrrec:=copy(arrrec);',
  11164. ' arrrec:=copy(arrrec,5);',
  11165. ' arrrec:=copy(arrrec,6,7);',
  11166. ' arrset:=copy(arrset);',
  11167. ' arrset:=copy(arrset,8);',
  11168. ' arrset:=copy(arrset,9,10);',
  11169. ' arrjsvalue:=copy(arrjsvalue);',
  11170. ' arrjsvalue:=copy(arrjsvalue,11);',
  11171. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11172. ' ']);
  11173. ConvertProgram;
  11174. CheckSource('TestArray_Copy',
  11175. LinesToStr([ // statements
  11176. 'this.TFlag = {',
  11177. ' "0": "big",',
  11178. ' big: 0,',
  11179. ' "1": "small",',
  11180. ' small: 1',
  11181. '};',
  11182. 'rtl.recNewT(this, "TRec", function () {',
  11183. ' this.i = 0;',
  11184. ' this.$eq = function (b) {',
  11185. ' return this.i === b.i;',
  11186. ' };',
  11187. ' this.$assign = function (s) {',
  11188. ' this.i = s.i;',
  11189. ' return this;',
  11190. ' };',
  11191. '});',
  11192. 'this.ArrInt = [];',
  11193. 'this.ArrRec = [];',
  11194. 'this.ArrSet = [];',
  11195. 'this.ArrJSValue = [];',
  11196. '']),
  11197. LinesToStr([ // $mod.$main
  11198. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11199. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11200. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11201. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11202. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11203. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11204. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11205. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11206. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11207. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11208. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11209. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11210. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11211. '']));
  11212. end;
  11213. procedure TTestModule.TestArray_InsertDelete;
  11214. begin
  11215. StartProgram(false);
  11216. Add([
  11217. 'type',
  11218. ' integer = longint;',
  11219. ' TFlag = (big,small);',
  11220. ' TFlags = set of TFlag;',
  11221. ' TRec = record',
  11222. ' i: integer;',
  11223. ' end;',
  11224. ' TArrInt = array of integer;',
  11225. ' TArrRec = array of TRec;',
  11226. ' TArrSet = array of TFlags;',
  11227. ' TArrJSValue = array of jsvalue;',
  11228. ' TArrArrInt = array of TArrInt;',
  11229. 'var',
  11230. ' ArrInt: tarrint;',
  11231. ' ArrRec: tarrrec;',
  11232. ' ArrSet: tarrset;',
  11233. ' ArrJSValue: tarrjsvalue;',
  11234. ' ArrArrInt: TArrArrInt;',
  11235. 'begin',
  11236. ' Insert(1,arrint,2);',
  11237. ' Insert(arrint[3],arrint,4);',
  11238. ' Insert(arrrec[5],arrrec,6);',
  11239. ' Insert(arrset[7],arrset,7);',
  11240. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11241. ' Insert(10,arrjsvalue,11);',
  11242. ' Insert([23],arrarrint,22);',
  11243. ' Delete(arrint,12,13);',
  11244. ' Delete(arrrec,14,15);',
  11245. ' Delete(arrset,17,18);',
  11246. ' Delete(arrjsvalue,19,10);']);
  11247. ConvertProgram;
  11248. CheckSource('TestArray_InsertDelete',
  11249. LinesToStr([ // statements
  11250. 'this.TFlag = {',
  11251. ' "0": "big",',
  11252. ' big: 0,',
  11253. ' "1": "small",',
  11254. ' small: 1',
  11255. '};',
  11256. 'rtl.recNewT(this, "TRec", function () {',
  11257. ' this.i = 0;',
  11258. ' this.$eq = function (b) {',
  11259. ' return this.i === b.i;',
  11260. ' };',
  11261. ' this.$assign = function (s) {',
  11262. ' this.i = s.i;',
  11263. ' return this;',
  11264. ' };',
  11265. '});',
  11266. 'this.ArrInt = [];',
  11267. 'this.ArrRec = [];',
  11268. 'this.ArrSet = [];',
  11269. 'this.ArrJSValue = [];',
  11270. 'this.ArrArrInt = [];',
  11271. '']),
  11272. LinesToStr([ // $mod.$main
  11273. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11274. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11275. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11276. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11277. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11278. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11279. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11280. '$mod.ArrInt.splice(12, 13);',
  11281. '$mod.ArrRec.splice(14, 15);',
  11282. '$mod.ArrSet.splice(17, 18);',
  11283. '$mod.ArrJSValue.splice(19, 10);',
  11284. '']));
  11285. end;
  11286. procedure TTestModule.TestArray_Add_Append;
  11287. begin
  11288. StartProgram(false);
  11289. Add([
  11290. '{$modeswitch arrayoperators}',
  11291. 'type',
  11292. ' integer = longint;',
  11293. ' TFlag = (big,small);',
  11294. ' TFlags = set of TFlag;',
  11295. ' TRec = record',
  11296. ' i: integer;',
  11297. ' end;',
  11298. ' TArrInt = array of integer;',
  11299. ' TArrRec = array of TRec;',
  11300. ' TArrFlag = array of TFlag;',
  11301. ' TArrSet = array of TFlags;',
  11302. ' TArrJSValue = array of jsvalue;',
  11303. 'var',
  11304. ' ArrInt: tarrint;',
  11305. ' ArrRec: tarrrec;',
  11306. ' ArrFlag: tarrflag;',
  11307. ' ArrSet: tarrset;',
  11308. ' ArrJSValue: tarrjsvalue;',
  11309. ' r: TRec;',
  11310. ' f: TFlags;',
  11311. 'begin',
  11312. ' // append',
  11313. ' arrint:=arrint+[2];',
  11314. ' arrint:=arrint+[3,4];',
  11315. ' arrrec:=arrrec+[r];',
  11316. ' arrrec:=arrrec+[r,r];',
  11317. ' arrset:=arrset+[f];',
  11318. ' arrset:=arrset+[f,f];',
  11319. ' arrjsvalue:=arrjsvalue+[11];',
  11320. ' arrjsvalue:=arrjsvalue+[12,13];',
  11321. ' arrflag:=arrflag+[small];',
  11322. ' arrflag:=arrflag+[small,big];',
  11323. '']);
  11324. ConvertProgram;
  11325. CheckSource('TestArray_Add_Append',
  11326. LinesToStr([ // statements
  11327. 'this.TFlag = {',
  11328. ' "0": "big",',
  11329. ' big: 0,',
  11330. ' "1": "small",',
  11331. ' small: 1',
  11332. '};',
  11333. 'rtl.recNewT(this, "TRec", function () {',
  11334. ' this.i = 0;',
  11335. ' this.$eq = function (b) {',
  11336. ' return this.i === b.i;',
  11337. ' };',
  11338. ' this.$assign = function (s) {',
  11339. ' this.i = s.i;',
  11340. ' return this;',
  11341. ' };',
  11342. '});',
  11343. 'this.ArrInt = [];',
  11344. 'this.ArrRec = [];',
  11345. 'this.ArrFlag = [];',
  11346. 'this.ArrSet = [];',
  11347. 'this.ArrJSValue = [];',
  11348. 'this.r = this.TRec.$new();',
  11349. 'this.f = {};',
  11350. '']),
  11351. LinesToStr([ // $mod.$main
  11352. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11353. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11354. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11355. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11356. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11357. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11358. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11359. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11360. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11361. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11362. '']));
  11363. end;
  11364. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11365. begin
  11366. Parser.Options:=Parser.Options+[po_cassignments];
  11367. StartProgram(false);
  11368. Add([
  11369. '{$modeswitch arrayoperators}',
  11370. 'type',
  11371. ' integer = longint;',
  11372. ' TArrInt = array of integer;',
  11373. ' TArrStr = array of string;',
  11374. 'const',
  11375. ' Ints: TArrInt = (1,2,3);',
  11376. ' Aliases: TarrStr = (''foo'',''b'');',
  11377. ' OneInt: TArrInt = (7);',
  11378. ' OneStr: array of integer = (7);',
  11379. ' Chars: array of char = ''aoc'';',
  11380. ' Names: array of string = (''a'',''foo'');',
  11381. ' NameCount = low(Names)+high(Names)+length(Names);',
  11382. 'var i: integer;',
  11383. 'begin',
  11384. ' Ints:=[];',
  11385. ' Ints:=[1,1];',
  11386. ' Ints:=[1]+[2];',
  11387. ' Ints:=[2];',
  11388. ' Ints:=[]+ints;',
  11389. ' Ints:=Ints+[];',
  11390. ' Ints:=Ints+OneInt;',
  11391. ' Ints:=Ints+[1,1];',
  11392. ' Ints:=[i,i]+Ints;',
  11393. ' Ints:=[1]+[i]+[3];',
  11394. '']);
  11395. ConvertProgram;
  11396. CheckSource('TestArray_DynArrayConstObjFPC',
  11397. LinesToStr([ // statements
  11398. 'this.Ints = [1, 2, 3];',
  11399. 'this.Aliases = ["foo", "b"];',
  11400. 'this.OneInt = [7];',
  11401. 'this.OneStr = [7];',
  11402. 'this.Chars = ["a", "o", "c"];',
  11403. 'this.Names = ["a", "foo"];',
  11404. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11405. 'this.i = 0;',
  11406. '']),
  11407. LinesToStr([ // $mod.$main
  11408. '$mod.Ints = [];',
  11409. '$mod.Ints = [1, 1];',
  11410. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11411. '$mod.Ints = [2];',
  11412. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11413. '$mod.Ints = $mod.Ints;',
  11414. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11415. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11416. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11417. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11418. '']));
  11419. end;
  11420. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11421. begin
  11422. StartProgram(false);
  11423. // Note: const c = [1,1]; defines a set!
  11424. Add([
  11425. '{$mode delphi}',
  11426. 'type',
  11427. ' integer = longint;',
  11428. ' TArrInt = array of integer;',
  11429. ' TArrStr = array of string;',
  11430. 'const',
  11431. ' Ints: TArrInt = [1,1,2];',
  11432. ' Aliases: TarrStr = [''foo'',''b''];',
  11433. ' OneInt: TArrInt = [7];',
  11434. ' OneStr: array of integer = [7]+[8];',
  11435. ' Chars: array of char = ''aoc'';',
  11436. ' Names: array of string = [''a'',''a''];',
  11437. ' NameCount = low(Names)+high(Names)+length(Names);',
  11438. 'begin',
  11439. '']);
  11440. ConvertProgram;
  11441. CheckSource('TestArray_DynArrayConstDelphi',
  11442. LinesToStr([ // statements
  11443. 'this.Ints = [1, 1, 2];',
  11444. 'this.Aliases = ["foo", "b"];',
  11445. 'this.OneInt = [7];',
  11446. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11447. 'this.Chars = ["a", "o", "c"];',
  11448. 'this.Names = ["a", "a"];',
  11449. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11450. '']),
  11451. LinesToStr([ // $mod.$main
  11452. '']));
  11453. end;
  11454. procedure TTestModule.TestArray_ArrayLitAsParam;
  11455. begin
  11456. StartProgram(false);
  11457. Add([
  11458. '{$modeswitch arrayoperators}',
  11459. 'type',
  11460. ' integer = longint;',
  11461. ' TArrInt = array of integer;',
  11462. ' TArrSet = array of (red,green,blue);',
  11463. 'procedure DoOpenInt(const a: array of integer); forward;',
  11464. 'procedure DoInt(const a: TArrInt);',
  11465. 'begin',
  11466. ' DoInt(a+[1]);',
  11467. ' DoInt([1]+a);',
  11468. ' DoOpenInt(a);',
  11469. ' DoOpenInt(a+[1]);',
  11470. ' DoOpenInt([1]+a);',
  11471. 'end;',
  11472. 'procedure DoOpenInt(const a: array of integer);',
  11473. 'begin',
  11474. ' DoOpenInt(a+[1]);',
  11475. ' DoOpenInt([1]+a);',
  11476. ' DoInt(a);',
  11477. ' DoInt(a+[1]);',
  11478. ' DoInt([1]+a);',
  11479. 'end;',
  11480. 'procedure DoSet(const a: TArrSet);',
  11481. 'begin',
  11482. ' DoSet(a+[red]);',
  11483. ' DoSet([blue]+a);',
  11484. 'end;',
  11485. 'var',
  11486. ' i: TArrInt;',
  11487. ' s: TArrSet;',
  11488. 'begin',
  11489. ' DoInt([1]);',
  11490. ' DoInt([1]+[2]);',
  11491. ' DoInt(i+[1]);',
  11492. ' DoInt([1]+i);',
  11493. ' DoOpenInt([1]);',
  11494. ' DoOpenInt([1]+[2]);',
  11495. ' DoOpenInt(i+[1]);',
  11496. ' DoOpenInt([1]+i);',
  11497. ' DoSet([red]);',
  11498. ' DoSet([blue]+[green]);',
  11499. ' DoSet(s+[blue]);',
  11500. ' DoSet([red]+s);',
  11501. '']);
  11502. ConvertProgram;
  11503. CheckSource('TestArray_ArrayLitAsParam',
  11504. LinesToStr([ // statements
  11505. 'this.TArrSet$a = {',
  11506. ' "0": "red",',
  11507. ' red: 0,',
  11508. ' "1": "green",',
  11509. ' green: 1,',
  11510. ' "2": "blue",',
  11511. ' blue: 2',
  11512. '};',
  11513. 'this.DoInt = function (a) {',
  11514. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11515. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11516. ' $mod.DoOpenInt(a);',
  11517. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11518. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11519. '};',
  11520. 'this.DoOpenInt = function (a) {',
  11521. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11522. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11523. ' $mod.DoInt(a);',
  11524. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11525. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11526. '};',
  11527. 'this.DoSet = function (a) {',
  11528. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11529. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11530. '};',
  11531. 'this.i = [];',
  11532. 'this.s = [];',
  11533. '']),
  11534. LinesToStr([ // $mod.$main
  11535. '$mod.DoInt([1]);',
  11536. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11537. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11538. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11539. '$mod.DoOpenInt([1]);',
  11540. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11541. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11542. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11543. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11544. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11545. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11546. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11547. '']));
  11548. end;
  11549. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11550. begin
  11551. StartProgram(false);
  11552. Add([
  11553. '{$modeswitch arrayoperators}',
  11554. 'type',
  11555. ' integer = longint;',
  11556. ' TArrInt = array of integer;',
  11557. ' TArrArrInt = array of TArrInt;',
  11558. 'procedure DoInt(const a: TArrArrInt);',
  11559. 'begin',
  11560. ' DoInt(a+[[1]]);',
  11561. ' DoInt([[1]]+a);',
  11562. ' DoInt(a);',
  11563. 'end;',
  11564. 'var',
  11565. ' i: TArrInt;',
  11566. ' a: TArrArrInt;',
  11567. 'begin',
  11568. ' a:=[[1]];',
  11569. ' a:=[i];',
  11570. ' a:=a+[i];',
  11571. ' a:=[i]+a;',
  11572. ' a:=[[1]+i];',
  11573. ' a:=[[1]+[2]];',
  11574. ' a:=[i+[2]];',
  11575. ' DoInt([[1]]);',
  11576. ' DoInt([[1]+[2],[3,4],[5]]);',
  11577. ' DoInt([i+[1]]+a);',
  11578. ' DoInt([i]+a);',
  11579. '']);
  11580. ConvertProgram;
  11581. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11582. LinesToStr([ // statements
  11583. 'this.DoInt = function (a) {',
  11584. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11585. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11586. ' $mod.DoInt(a);',
  11587. '};',
  11588. 'this.i = [];',
  11589. 'this.a = [];',
  11590. '']),
  11591. LinesToStr([ // $mod.$main
  11592. '$mod.a = [[1]];',
  11593. '$mod.a = [$mod.i];',
  11594. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11595. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11596. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11597. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11598. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11599. '$mod.DoInt([[1]]);',
  11600. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11601. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11602. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11603. '']));
  11604. end;
  11605. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11606. begin
  11607. StartProgram(false);
  11608. Add([
  11609. '{$modeswitch arrayoperators}',
  11610. 'type',
  11611. ' integer = longint;',
  11612. ' TArrInt = array[1..2] of integer;',
  11613. ' TArrArrInt = array of TArrInt;',
  11614. 'procedure DoInt(const a: TArrArrInt);',
  11615. 'begin',
  11616. ' DoInt(a+[[1,2]]);',
  11617. ' DoInt([[1,2]]+a);',
  11618. ' DoInt(a);',
  11619. 'end;',
  11620. 'var',
  11621. ' i: TArrInt;',
  11622. ' a: TArrArrInt;',
  11623. 'begin',
  11624. ' a:=[[1,1]];',
  11625. ' a:=[i];',
  11626. ' a:=a+[i];',
  11627. ' a:=[i]+a;',
  11628. ' DoInt([[1,1]]);',
  11629. ' DoInt([[1,2],[3,4]]);',
  11630. '']);
  11631. ConvertProgram;
  11632. CheckSource('TestArray_ArrayLitStaticAsParam',
  11633. LinesToStr([ // statements
  11634. 'this.DoInt = function (a) {',
  11635. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11636. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11637. ' $mod.DoInt(a);',
  11638. '};',
  11639. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11640. 'this.a = [];',
  11641. '']),
  11642. LinesToStr([ // $mod.$main
  11643. '$mod.a = [[1, 1]];',
  11644. '$mod.a = [$mod.i.slice(0)];',
  11645. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11646. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11647. '$mod.DoInt([[1, 1]]);',
  11648. '$mod.DoInt([[1, 2], [3, 4]]);',
  11649. '']));
  11650. end;
  11651. procedure TTestModule.TestArray_ForInArrOfString;
  11652. begin
  11653. StartProgram(false);
  11654. Add([
  11655. 'type',
  11656. 'type',
  11657. ' TMonthNameArray = array [1..12] of string;',
  11658. ' TMonthNames = TMonthNameArray;',
  11659. ' TObject = class',
  11660. ' private',
  11661. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11662. ' public',
  11663. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11664. ' end;',
  11665. 'var',
  11666. ' f: TObject;',
  11667. ' Month: string;',
  11668. ' Names: array of string = (''a'',''foo'',''bar'');',
  11669. ' i: longint;',
  11670. 'begin',
  11671. ' for Month in f.LongMonthNames do ;',
  11672. ' for Month in Names do ;',
  11673. ' for i:=low(Names) to high(Names) do ;',
  11674. '']);
  11675. ConvertProgram;
  11676. CheckSource('TestArray_ForInArrOfString',
  11677. LinesToStr([ // statements
  11678. 'rtl.createClass(this, "TObject", null, function () {',
  11679. ' this.$init = function () {',
  11680. ' };',
  11681. ' this.$final = function () {',
  11682. ' };',
  11683. '});',
  11684. 'this.f = null;',
  11685. 'this.Month = "";',
  11686. 'this.Names = ["a", "foo", "bar"];',
  11687. 'this.i = 0;',
  11688. '']),
  11689. LinesToStr([ // $mod.$main
  11690. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11691. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11692. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11693. '']));
  11694. end;
  11695. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11696. begin
  11697. StartProgram(false);
  11698. Add([
  11699. '{$modeswitch externalclass}',
  11700. 'type',
  11701. ' TJSObject = class external name ''Object''',
  11702. ' end;',
  11703. ' TJSArray = class external name ''Array''',
  11704. ' class function isArray(Value: JSValue) : boolean;',
  11705. ' function concat() : TJSArray; varargs;',
  11706. ' end;',
  11707. 'var',
  11708. ' aObj: TJSArray;',
  11709. ' a: array of longint;',
  11710. ' o: TJSObject;',
  11711. 'begin',
  11712. ' if TJSArray.isArray(65) then ;',
  11713. ' aObj:=TJSArray(a).concat(a);',
  11714. ' o:=TJSObject(a);',
  11715. ' aObj:=TJSArray([''bird'',''ant'']);',
  11716. '']);
  11717. ConvertProgram;
  11718. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11719. LinesToStr([ // statements
  11720. 'this.aObj = null;',
  11721. 'this.a = [];',
  11722. 'this.o = null;',
  11723. '']),
  11724. LinesToStr([ // $mod.$main
  11725. 'if (Array.isArray(65)) ;',
  11726. '$mod.aObj = $mod.a.concat($mod.a);',
  11727. '$mod.o = $mod.a;',
  11728. '$mod.aObj = ["bird", "ant"];',
  11729. '']));
  11730. end;
  11731. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11732. begin
  11733. StartProgram(false);
  11734. Add([
  11735. '{$modeswitch externalclass}',
  11736. 'type',
  11737. ' TArrStr = array of string;',
  11738. ' TJSArray = class external name ''Array''',
  11739. ' end;',
  11740. ' TJSObject = class external name ''Object''',
  11741. ' end;',
  11742. 'var',
  11743. ' aObj: TJSArray;',
  11744. ' a: TArrStr;',
  11745. ' jo: TJSObject;',
  11746. 'begin',
  11747. ' a:=TArrStr(aObj);',
  11748. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11749. ' a:=TarrStr(jo);',
  11750. '']);
  11751. ConvertProgram;
  11752. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11753. LinesToStr([ // statements
  11754. 'this.aObj = null;',
  11755. 'this.a = [];',
  11756. 'this.jo = null;',
  11757. '']),
  11758. LinesToStr([ // $mod.$main
  11759. '$mod.a = $mod.aObj;',
  11760. '$mod.aObj[1] = $mod.aObj[2];',
  11761. '$mod.a = $mod.jo;',
  11762. '']));
  11763. end;
  11764. procedure TTestModule.TestArrayOfConst_TVarRec;
  11765. begin
  11766. StartProgram(true,[supTVarRec]);
  11767. Add([
  11768. 'procedure Say(args: array of const);',
  11769. 'var',
  11770. ' i: longint;',
  11771. ' v: TVarRec;',
  11772. 'begin',
  11773. ' for i:=low(args) to high(args) do begin',
  11774. ' v:=args[i];',
  11775. ' case v.vtype of',
  11776. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11777. ' end;',
  11778. ' end;',
  11779. ' for v in args do ;',
  11780. ' args:=nil;',
  11781. ' SetLength(args,2);',
  11782. 'end;',
  11783. 'begin']);
  11784. ConvertProgram;
  11785. CheckSource('TestArrayOfConst_TVarRec',
  11786. LinesToStr([ // statements
  11787. 'this.Say = function (args) {',
  11788. ' var i = 0;',
  11789. ' var v = pas.system.TVarRec.$new();',
  11790. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11791. ' i = $l;',
  11792. ' v.$assign(args[i]);',
  11793. ' var $tmp = v.VType;',
  11794. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11795. ' };',
  11796. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11797. ' args = [];',
  11798. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11799. '};',
  11800. '']),
  11801. LinesToStr([ // $mod.$main
  11802. ]));
  11803. end;
  11804. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11805. begin
  11806. StartProgram(true,[supTVarRec]);
  11807. Add([
  11808. 'procedure Say(args: array of const);',
  11809. 'begin',
  11810. ' Say(args);',
  11811. 'end;',
  11812. 'var',
  11813. ' p: Pointer;',
  11814. ' j: jsvalue;',
  11815. ' c: currency;',
  11816. 'begin',
  11817. ' Say([]);',
  11818. ' Say([1]);',
  11819. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11820. '']);
  11821. ConvertProgram;
  11822. CheckSource('TestArrayOfConst_PassBaseTypes',
  11823. LinesToStr([ // statements
  11824. 'this.Say = function (args) {',
  11825. ' $mod.Say(args);',
  11826. '};',
  11827. 'this.p = null;',
  11828. 'this.j = undefined;',
  11829. 'this.c = 0;',
  11830. '']),
  11831. LinesToStr([ // $mod.$main
  11832. '$mod.Say([]);',
  11833. '$mod.Say(pas.system.VarRecs(0, 1));',
  11834. '$mod.Say(pas.system.VarRecs(',
  11835. ' 9,',
  11836. ' "c",',
  11837. ' 18,',
  11838. ' "foo",',
  11839. ' 5,',
  11840. ' null,',
  11841. ' 1,',
  11842. ' true,',
  11843. ' 3,',
  11844. ' 1.3,',
  11845. ' 5,',
  11846. ' $mod.p,',
  11847. ' 20,',
  11848. ' $mod.j,',
  11849. ' 12,',
  11850. ' $mod.c',
  11851. ' ));',
  11852. '']));
  11853. end;
  11854. procedure TTestModule.TestArrayOfConst_PassObj;
  11855. begin
  11856. StartProgram(true,[supTVarRec]);
  11857. Add([
  11858. '{$interfaces corba}',
  11859. 'type',
  11860. ' TObject = class',
  11861. ' end;',
  11862. ' TClass = class of TObject;',
  11863. ' IUnknown = interface',
  11864. ' end;',
  11865. 'procedure Say(args: array of const);',
  11866. 'begin',
  11867. 'end;',
  11868. 'var',
  11869. ' o: TObject;',
  11870. ' c: TClass;',
  11871. ' i: IUnknown;',
  11872. 'begin',
  11873. ' Say([o,c,TObject]);',
  11874. ' Say([nil,i]);',
  11875. '']);
  11876. ConvertProgram;
  11877. CheckSource('TestArrayOfConst_PassObj',
  11878. LinesToStr([ // statements
  11879. 'rtl.createClass(this, "TObject", null, function () {',
  11880. ' this.$init = function () {',
  11881. ' };',
  11882. ' this.$final = function () {',
  11883. ' };',
  11884. '});',
  11885. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11886. 'this.Say = function (args) {',
  11887. '};',
  11888. 'this.o = null;',
  11889. 'this.c = null;',
  11890. 'this.i = null;',
  11891. '']),
  11892. LinesToStr([ // $mod.$main
  11893. '$mod.Say(pas.system.VarRecs(',
  11894. ' 7,',
  11895. ' $mod.o,',
  11896. ' 8,',
  11897. ' $mod.c,',
  11898. ' 8,',
  11899. ' $mod.TObject',
  11900. '));',
  11901. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11902. '']));
  11903. end;
  11904. procedure TTestModule.TestRecord_Empty;
  11905. begin
  11906. StartProgram(false);
  11907. Add([
  11908. 'type',
  11909. ' TRecA = record',
  11910. ' end;',
  11911. 'var a,b: TRecA;',
  11912. 'begin',
  11913. ' if a=b then ;']);
  11914. ConvertProgram;
  11915. CheckSource('TestRecord_Empty',
  11916. LinesToStr([ // statements
  11917. 'rtl.recNewT(this, "TRecA", function () {',
  11918. ' this.$eq = function (b) {',
  11919. ' return true;',
  11920. ' };',
  11921. ' this.$assign = function (s) {',
  11922. ' return this;',
  11923. ' };',
  11924. '});',
  11925. 'this.a = this.TRecA.$new();',
  11926. 'this.b = this.TRecA.$new();',
  11927. '']),
  11928. LinesToStr([ // $mod.$main
  11929. 'if ($mod.a.$eq($mod.b)) ;'
  11930. ]));
  11931. end;
  11932. procedure TTestModule.TestRecord_Var;
  11933. begin
  11934. StartProgram(false);
  11935. Add('type');
  11936. Add(' TRecA = record');
  11937. Add(' Bold: longint;');
  11938. Add(' end;');
  11939. Add('var Rec: TRecA;');
  11940. Add('begin');
  11941. Add(' rec.bold:=123');
  11942. ConvertProgram;
  11943. CheckSource('TestRecord_Var',
  11944. LinesToStr([ // statements
  11945. 'rtl.recNewT(this, "TRecA", function () {',
  11946. ' this.Bold = 0;',
  11947. ' this.$eq = function (b) {',
  11948. ' return this.Bold === b.Bold;',
  11949. ' };',
  11950. ' this.$assign = function (s) {',
  11951. ' this.Bold = s.Bold;',
  11952. ' return this;',
  11953. ' };',
  11954. '});',
  11955. 'this.Rec = this.TRecA.$new();',
  11956. '']),
  11957. LinesToStr([ // $mod.$main
  11958. '$mod.Rec.Bold = 123;'
  11959. ]));
  11960. end;
  11961. procedure TTestModule.TestRecord_VarExternal;
  11962. begin
  11963. StartProgram(false);
  11964. Add([
  11965. '{$modeswitch externalclass}',
  11966. 'type',
  11967. ' TRecA = record',
  11968. ' i: byte;',
  11969. ' length_: longint external name ''length'';',
  11970. ' end;',
  11971. 'var Rec: TRecA;',
  11972. 'begin',
  11973. ' rec.length_ := rec.length_',
  11974. '']);
  11975. ConvertProgram;
  11976. CheckSource('TestRecord_VarExternal',
  11977. LinesToStr([ // statements
  11978. 'rtl.recNewT(this, "TRecA", function () {',
  11979. ' this.i = 0;',
  11980. ' this.$eq = function (b) {',
  11981. ' return (this.i === b.i) && (this.length === b.length);',
  11982. ' };',
  11983. ' this.$assign = function (s) {',
  11984. ' this.i = s.i;',
  11985. ' this.length = s.length;',
  11986. ' return this;',
  11987. ' };',
  11988. '});',
  11989. 'this.Rec = this.TRecA.$new();',
  11990. '']),
  11991. LinesToStr([ // $mod.$main
  11992. '$mod.Rec.length = $mod.Rec.length;'
  11993. ]));
  11994. end;
  11995. procedure TTestModule.TestRecord_WithDo;
  11996. begin
  11997. StartProgram(false);
  11998. Add('type');
  11999. Add(' TRec = record');
  12000. Add(' vI: longint;');
  12001. Add(' end;');
  12002. Add('var');
  12003. Add(' Int: longint;');
  12004. Add(' r: TRec;');
  12005. Add('begin');
  12006. Add(' with r do');
  12007. Add(' int:=vi;');
  12008. Add(' with r do begin');
  12009. Add(' int:=vi;');
  12010. Add(' vi:=int;');
  12011. Add(' end;');
  12012. ConvertProgram;
  12013. CheckSource('TestWithRecordDo',
  12014. LinesToStr([ // statements
  12015. 'rtl.recNewT(this, "TRec", function () {',
  12016. ' this.vI = 0;',
  12017. ' this.$eq = function (b) {',
  12018. ' return this.vI === b.vI;',
  12019. ' };',
  12020. ' this.$assign = function (s) {',
  12021. ' this.vI = s.vI;',
  12022. ' return this;',
  12023. ' };',
  12024. '});',
  12025. 'this.Int = 0;',
  12026. 'this.r = this.TRec.$new();',
  12027. '']),
  12028. LinesToStr([ // $mod.$main
  12029. 'var $with = $mod.r;',
  12030. '$mod.Int = $with.vI;',
  12031. 'var $with1 = $mod.r;',
  12032. '$mod.Int = $with1.vI;',
  12033. '$with1.vI = $mod.Int;'
  12034. ]));
  12035. end;
  12036. procedure TTestModule.TestRecord_Assign;
  12037. begin
  12038. StartProgram(false);
  12039. Add([
  12040. 'type',
  12041. ' TEnum = (red,green);',
  12042. ' TEnums = set of TEnum;',
  12043. ' TSmallRec = record',
  12044. ' N: longint;',
  12045. ' end;',
  12046. ' TBigRec = record',
  12047. ' Int: longint;',
  12048. ' D: double;',
  12049. ' Arr: array of longint;',
  12050. ' Arr2: array[1..2] of longint;',
  12051. ' Small: TSmallRec;',
  12052. ' Enums: TEnums;',
  12053. ' end;',
  12054. 'var',
  12055. ' r, s: TBigRec;',
  12056. 'begin',
  12057. ' r:=s;',
  12058. ' r:=default(TBigRec);',
  12059. ' r:=default(s);',
  12060. '']);
  12061. ConvertProgram;
  12062. CheckSource('TestRecord_Assign',
  12063. LinesToStr([ // statements
  12064. 'this.TEnum = {',
  12065. ' "0": "red",',
  12066. ' red: 0,',
  12067. ' "1": "green",',
  12068. ' green: 1',
  12069. '};',
  12070. 'rtl.recNewT(this, "TSmallRec", function () {',
  12071. ' this.N = 0;',
  12072. ' this.$eq = function (b) {',
  12073. ' return this.N === b.N;',
  12074. ' };',
  12075. ' this.$assign = function (s) {',
  12076. ' this.N = s.N;',
  12077. ' return this;',
  12078. ' };',
  12079. '});',
  12080. 'rtl.recNewT(this, "TBigRec", function () {',
  12081. ' this.Int = 0;',
  12082. ' this.D = 0.0;',
  12083. ' this.$new = function () {',
  12084. ' var r = Object.create(this);',
  12085. ' r.Arr = [];',
  12086. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12087. ' r.Small = $mod.TSmallRec.$new();',
  12088. ' r.Enums = {};',
  12089. ' return r;',
  12090. ' };',
  12091. ' this.$eq = function (b) {',
  12092. ' 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);',
  12093. ' };',
  12094. ' this.$assign = function (s) {',
  12095. ' this.Int = s.Int;',
  12096. ' this.D = s.D;',
  12097. ' this.Arr = rtl.arrayRef(s.Arr);',
  12098. ' this.Arr2 = s.Arr2.slice(0);',
  12099. ' this.Small.$assign(s.Small);',
  12100. ' this.Enums = rtl.refSet(s.Enums);',
  12101. ' return this;',
  12102. ' };',
  12103. '});',
  12104. 'this.r = this.TBigRec.$new();',
  12105. 'this.s = this.TBigRec.$new();',
  12106. '']),
  12107. LinesToStr([ // $mod.$main
  12108. '$mod.r.$assign($mod.s);',
  12109. '$mod.r.$assign($mod.TBigRec.$new());',
  12110. '$mod.r.$assign($mod.TBigRec.$new());',
  12111. '']));
  12112. end;
  12113. procedure TTestModule.TestRecord_AsParams;
  12114. begin
  12115. StartProgram(false);
  12116. Add([
  12117. 'type',
  12118. ' integer = longint;',
  12119. ' TRecord = record',
  12120. ' i: integer;',
  12121. ' end;',
  12122. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12123. 'var vL: TRecord;',
  12124. 'begin',
  12125. ' vd:=vd;',
  12126. ' vd.i:=vd.i;',
  12127. ' vl:=vc;',
  12128. ' vv:=vv;',
  12129. ' vv.i:=vv.i;',
  12130. ' U:=vl;',
  12131. ' U:=vd;',
  12132. ' U:=vc;',
  12133. ' U:=vv;',
  12134. ' vl:=TRecord(U);',
  12135. ' vd:=TRecord(U);',
  12136. ' vv:=TRecord(U);',
  12137. ' doit(vd,vd,vd,vd);',
  12138. ' doit(vc,vc,vl,vl);',
  12139. ' doit(vv,vv,vv,vv);',
  12140. ' doit(vl,vl,vl,vl);',
  12141. ' TRecord(U).i:=3;',
  12142. 'end;',
  12143. 'var i: TRecord;',
  12144. 'begin',
  12145. ' doit(i,i,i,i);',
  12146. '']);
  12147. ConvertProgram;
  12148. CheckSource('TestRecord_AsParams',
  12149. LinesToStr([ // statements
  12150. 'rtl.recNewT(this, "TRecord", function () {',
  12151. ' this.i = 0;',
  12152. ' this.$eq = function (b) {',
  12153. ' return this.i === b.i;',
  12154. ' };',
  12155. ' this.$assign = function (s) {',
  12156. ' this.i = s.i;',
  12157. ' return this;',
  12158. ' };',
  12159. '});',
  12160. 'this.DoIt = function (vD, vC, vV, U) {',
  12161. ' var vL = $mod.TRecord.$new();',
  12162. ' vD.$assign(vD);',
  12163. ' vD.i = vD.i;',
  12164. ' vL.$assign(vC);',
  12165. ' vV.$assign(vV);',
  12166. ' vV.i = vV.i;',
  12167. ' U.$assign(vL);',
  12168. ' U.$assign(vD);',
  12169. ' U.$assign(vC);',
  12170. ' U.$assign(vV);',
  12171. ' vL.$assign(U);',
  12172. ' vD.$assign(U);',
  12173. ' vV.$assign(U);',
  12174. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12175. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12176. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12177. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12178. ' U.i = 3;',
  12179. '};',
  12180. 'this.i = this.TRecord.$new();'
  12181. ]),
  12182. LinesToStr([
  12183. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12184. '']));
  12185. end;
  12186. procedure TTestModule.TestRecord_ConstRef;
  12187. begin
  12188. StartProgram(false);
  12189. Add([
  12190. 'type TRec = record i: word; end;',
  12191. 'procedure Run(constref a: TRec);',
  12192. 'begin',
  12193. 'end;',
  12194. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12195. 'var l: TRec;',
  12196. 'begin',
  12197. ' Run(l);',
  12198. ' Run(a);',
  12199. ' Run(b);',
  12200. ' Run(c);',
  12201. ' Run(d);',
  12202. ' Run(e);',
  12203. 'end;',
  12204. 'begin',
  12205. '']);
  12206. ConvertProgram;
  12207. CheckResolverUnexpectedHints();
  12208. CheckSource('TestRecord_ConstRef',
  12209. LinesToStr([ // statements
  12210. 'rtl.recNewT(this, "TRec", function () {',
  12211. ' this.i = 0;',
  12212. ' this.$eq = function (b) {',
  12213. ' return this.i === b.i;',
  12214. ' };',
  12215. ' this.$assign = function (s) {',
  12216. ' this.i = s.i;',
  12217. ' return this;',
  12218. ' };',
  12219. '});',
  12220. 'this.Run = function (a) {',
  12221. '};',
  12222. 'this.Fly = function (a, b, c, d, e) {',
  12223. ' var l = $mod.TRec.$new();',
  12224. ' $mod.Run(l);',
  12225. ' $mod.Run(a);',
  12226. ' $mod.Run(b);',
  12227. ' $mod.Run(c);',
  12228. ' $mod.Run(d);',
  12229. ' $mod.Run(e);',
  12230. '};',
  12231. '']),
  12232. LinesToStr([
  12233. '']));
  12234. end;
  12235. procedure TTestModule.TestRecordElement_AsParams;
  12236. begin
  12237. StartProgram(false);
  12238. Add('type');
  12239. Add(' integer = longint;');
  12240. Add(' TRecord = record');
  12241. Add(' i: integer;');
  12242. Add(' end;');
  12243. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12244. Add('var vJ: TRecord;');
  12245. Add('begin');
  12246. Add(' doit(vj.i,vj.i,vj.i);');
  12247. Add('end;');
  12248. Add('var r: TRecord;');
  12249. Add('begin');
  12250. Add(' doit(r.i,r.i,r.i);');
  12251. ConvertProgram;
  12252. CheckSource('TestRecordElement_AsParams',
  12253. LinesToStr([ // statements
  12254. 'rtl.recNewT(this, "TRecord", function () {',
  12255. ' this.i = 0;',
  12256. ' this.$eq = function (b) {',
  12257. ' return this.i === b.i;',
  12258. ' };',
  12259. ' this.$assign = function (s) {',
  12260. ' this.i = s.i;',
  12261. ' return this;',
  12262. ' };',
  12263. '});',
  12264. 'this.DoIt = function (vG,vH,vI) {',
  12265. ' var vJ = $mod.TRecord.$new();',
  12266. ' $mod.DoIt(vJ.i, vJ.i, {',
  12267. ' p: vJ,',
  12268. ' get: function () {',
  12269. ' return this.p.i;',
  12270. ' },',
  12271. ' set: function (v) {',
  12272. ' this.p.i = v;',
  12273. ' }',
  12274. ' });',
  12275. '};',
  12276. 'this.r = this.TRecord.$new();'
  12277. ]),
  12278. LinesToStr([
  12279. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12280. ' p: $mod.r,',
  12281. ' get: function () {',
  12282. ' return this.p.i;',
  12283. ' },',
  12284. ' set: function (v) {',
  12285. ' this.p.i = v;',
  12286. ' }',
  12287. '});'
  12288. ]));
  12289. end;
  12290. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12291. begin
  12292. StartProgram(false);
  12293. Add('type');
  12294. Add(' integer = longint;');
  12295. Add(' TRecord = record');
  12296. Add(' i: integer;');
  12297. Add(' end;');
  12298. Add('function GetRec(vB: integer = 0): TRecord;');
  12299. Add('begin');
  12300. Add('end;');
  12301. Add('procedure DoIt(vG: integer; const vH: integer);');
  12302. Add('begin');
  12303. Add('end;');
  12304. Add('begin');
  12305. Add(' doit(getrec.i,getrec.i);');
  12306. Add(' doit(getrec().i,getrec().i);');
  12307. Add(' doit(getrec(1).i,getrec(2).i);');
  12308. ConvertProgram;
  12309. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12310. LinesToStr([ // statements
  12311. 'rtl.recNewT(this, "TRecord", function () {',
  12312. ' this.i = 0;',
  12313. ' this.$eq = function (b) {',
  12314. ' return this.i === b.i;',
  12315. ' };',
  12316. ' this.$assign = function (s) {',
  12317. ' this.i = s.i;',
  12318. ' return this;',
  12319. ' };',
  12320. '});',
  12321. 'this.GetRec = function (vB) {',
  12322. ' var Result = $mod.TRecord.$new();',
  12323. ' return Result;',
  12324. '};',
  12325. 'this.DoIt = function (vG, vH) {',
  12326. '};',
  12327. '']),
  12328. LinesToStr([
  12329. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12330. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12331. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12332. '']));
  12333. end;
  12334. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12335. begin
  12336. StartProgram(false);
  12337. Add('type');
  12338. Add(' integer = longint;');
  12339. Add(' TRecord = record');
  12340. Add(' i: integer;');
  12341. Add(' end;');
  12342. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12343. Add('begin');
  12344. Add('end;');
  12345. Add('var r: trecord;');
  12346. Add('begin');
  12347. Add(' with r do ');
  12348. Add(' doit(i,i,i);');
  12349. ConvertProgram;
  12350. CheckSource('TestRecordElementFromWith_AsParams',
  12351. LinesToStr([ // statements
  12352. 'rtl.recNewT(this, "TRecord", function () {',
  12353. ' this.i = 0;',
  12354. ' this.$eq = function (b) {',
  12355. ' return this.i === b.i;',
  12356. ' };',
  12357. ' this.$assign = function (s) {',
  12358. ' this.i = s.i;',
  12359. ' return this;',
  12360. ' };',
  12361. '});',
  12362. 'this.DoIt = function (vG,vH,vI) {',
  12363. '};',
  12364. 'this.r = this.TRecord.$new();'
  12365. ]),
  12366. LinesToStr([
  12367. 'var $with = $mod.r;',
  12368. '$mod.DoIt($with.i,$with.i,{',
  12369. ' p: $with,',
  12370. ' get: function () {',
  12371. ' return this.p.i;',
  12372. ' },',
  12373. ' set: function (v) {',
  12374. ' this.p.i = v;',
  12375. ' }',
  12376. '});',
  12377. '']));
  12378. end;
  12379. procedure TTestModule.TestRecord_Equal;
  12380. begin
  12381. StartProgram(false);
  12382. Add('type');
  12383. Add(' integer = longint;');
  12384. Add(' TFlag = (red,blue);');
  12385. Add(' TFlags = set of TFlag;');
  12386. Add(' TProc = procedure;');
  12387. Add(' TRecord = record');
  12388. Add(' i: integer;');
  12389. Add(' Event: TProc;');
  12390. Add(' f: TFlags;');
  12391. Add(' end;');
  12392. Add(' TNested = record');
  12393. Add(' r: TRecord;');
  12394. Add(' end;');
  12395. Add('var');
  12396. Add(' b: boolean;');
  12397. Add(' r,s: trecord;');
  12398. Add('begin');
  12399. Add(' b:=r=s;');
  12400. Add(' b:=r<>s;');
  12401. ConvertProgram;
  12402. CheckSource('TestRecord_Equal',
  12403. LinesToStr([ // statements
  12404. 'this.TFlag = {',
  12405. ' "0": "red",',
  12406. ' red: 0,',
  12407. ' "1": "blue",',
  12408. ' blue: 1',
  12409. '};',
  12410. 'rtl.recNewT(this, "TRecord", function () {',
  12411. ' this.i = 0;',
  12412. ' this.Event = null;',
  12413. ' this.$new = function () {',
  12414. ' var r = Object.create(this);',
  12415. ' r.f = {};',
  12416. ' return r;',
  12417. ' };',
  12418. ' this.$eq = function (b) {',
  12419. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12420. ' };',
  12421. ' this.$assign = function (s) {',
  12422. ' this.i = s.i;',
  12423. ' this.Event = s.Event;',
  12424. ' this.f = rtl.refSet(s.f);',
  12425. ' return this;',
  12426. ' };',
  12427. '});',
  12428. 'rtl.recNewT(this, "TNested", function () {',
  12429. ' this.$new = function () {',
  12430. ' var r = Object.create(this);',
  12431. ' r.r = $mod.TRecord.$new();',
  12432. ' return r;',
  12433. ' };',
  12434. ' this.$eq = function (b) {',
  12435. ' return this.r.$eq(b.r);',
  12436. ' };',
  12437. ' this.$assign = function (s) {',
  12438. ' this.r.$assign(s.r);',
  12439. ' return this;',
  12440. ' };',
  12441. '});',
  12442. 'this.b = false;',
  12443. 'this.r = this.TRecord.$new();',
  12444. 'this.s = this.TRecord.$new();',
  12445. '']),
  12446. LinesToStr([
  12447. '$mod.b = $mod.r.$eq($mod.s);',
  12448. '$mod.b = !$mod.r.$eq($mod.s);',
  12449. '']));
  12450. end;
  12451. procedure TTestModule.TestRecord_JSValue;
  12452. begin
  12453. StartProgram(false);
  12454. Add([
  12455. 'type',
  12456. ' TRecord = record',
  12457. ' i: longint;',
  12458. ' end;',
  12459. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12460. 'begin',
  12461. 'end;',
  12462. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12463. 'begin',
  12464. ' if jsvalue(d) then ;',
  12465. ' if jsvalue(c) then ;',
  12466. ' if jsvalue(v) then ;',
  12467. 'end;',
  12468. 'var',
  12469. ' Jv: jsvalue;',
  12470. ' Rec: trecord;',
  12471. 'begin',
  12472. ' rec:=trecord(jv);',
  12473. ' jv:=rec;',
  12474. ' Fly(rec,rec);',
  12475. ' Fly(@rec,@rec);',
  12476. ' if jsvalue(Rec) then ;',
  12477. ' Run(trecord(jv),trecord(jv),rec);',
  12478. '']);
  12479. ConvertProgram;
  12480. CheckSource('TestRecord_JSValue',
  12481. LinesToStr([ // statements
  12482. 'rtl.recNewT(this, "TRecord", function () {',
  12483. ' this.i = 0;',
  12484. ' this.$eq = function (b) {',
  12485. ' return this.i === b.i;',
  12486. ' };',
  12487. ' this.$assign = function (s) {',
  12488. ' this.i = s.i;',
  12489. ' return this;',
  12490. ' };',
  12491. '});',
  12492. 'this.Fly = function (d, c) {',
  12493. '};',
  12494. 'this.Run = function (d, c, v) {',
  12495. ' if (d) ;',
  12496. ' if (c) ;',
  12497. ' if (v) ;',
  12498. '};',
  12499. 'this.Jv = undefined;',
  12500. 'this.Rec = this.TRecord.$new();',
  12501. '']),
  12502. LinesToStr([
  12503. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12504. '$mod.Jv = $mod.Rec;',
  12505. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12506. '$mod.Fly($mod.Rec, $mod.Rec);',
  12507. 'if ($mod.Rec) ;',
  12508. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12509. '']));
  12510. end;
  12511. procedure TTestModule.TestRecord_VariantFail;
  12512. begin
  12513. StartProgram(false);
  12514. Add([
  12515. 'type',
  12516. ' TRec = record',
  12517. ' case word of',
  12518. ' 0: (b0, b1: Byte);',
  12519. ' 1: (i: word);',
  12520. ' end;',
  12521. 'begin']);
  12522. SetExpectedPasResolverError('Not supported: variant record',
  12523. nNotSupportedX);
  12524. ConvertProgram;
  12525. end;
  12526. procedure TTestModule.TestRecord_FieldArray;
  12527. begin
  12528. StartProgram(false);
  12529. Add([
  12530. 'type',
  12531. ' TArrInt = array[3..4] of longint;',
  12532. ' TArrArrInt = array[3..4] of longint;',
  12533. ' TRec = record',
  12534. ' a: array of longint;',
  12535. ' s: array[1..2] of longint;',
  12536. ' m: array[1..2,3..4] of longint;',
  12537. ' o: TArrArrInt;',
  12538. ' end;',
  12539. 'begin']);
  12540. ConvertProgram;
  12541. CheckSource('TestRecord_FieldArray',
  12542. LinesToStr([ // statements
  12543. 'rtl.recNewT(this, "TRec", function () {',
  12544. ' this.m$a$clone = function (a) {',
  12545. ' var b = [];',
  12546. ' b.length = 2;',
  12547. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12548. ' return b;',
  12549. ' };',
  12550. ' this.$new = function () {',
  12551. ' var r = Object.create(this);',
  12552. ' r.a = [];',
  12553. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12554. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12555. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12556. ' return r;',
  12557. ' };',
  12558. ' this.$eq = function (b) {',
  12559. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12560. ' };',
  12561. ' this.$assign = function (s) {',
  12562. ' this.a = rtl.arrayRef(s.a);',
  12563. ' this.s = s.s.slice(0);',
  12564. ' this.m = this.m$a$clone(s.m);',
  12565. ' this.o = s.o.slice(0);',
  12566. ' return this;',
  12567. ' };',
  12568. '});',
  12569. '']),
  12570. LinesToStr([ // $mod.$main
  12571. '']));
  12572. end;
  12573. procedure TTestModule.TestRecord_Const;
  12574. begin
  12575. StartProgram(false);
  12576. Add([
  12577. 'type',
  12578. ' TArrInt = array[3..4] of longint;',
  12579. ' TPoint = record x,y: longint; end;',
  12580. ' TRec = record',
  12581. ' i: longint;',
  12582. ' a: array of longint;',
  12583. ' s: array[1..2] of longint;',
  12584. ' m: array[1..2,3..4] of longint;',
  12585. ' p: TPoint;',
  12586. ' end;',
  12587. ' TPoints = array of TPoint;',
  12588. 'const',
  12589. ' r: TRec = (',
  12590. ' i:1;',
  12591. ' a:(2,3);',
  12592. ' s:(4,5);',
  12593. ' m:( (11,12), (13,14) );',
  12594. ' p: (x:21; y:22)',
  12595. ' );',
  12596. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12597. 'begin']);
  12598. ConvertProgram;
  12599. CheckSource('TestRecord_Const',
  12600. LinesToStr([ // statements
  12601. 'rtl.recNewT(this, "TPoint", function () {',
  12602. ' this.x = 0;',
  12603. ' this.y = 0;',
  12604. ' this.$eq = function (b) {',
  12605. ' return (this.x === b.x) && (this.y === b.y);',
  12606. ' };',
  12607. ' this.$assign = function (s) {',
  12608. ' this.x = s.x;',
  12609. ' this.y = s.y;',
  12610. ' return this;',
  12611. ' };',
  12612. '});',
  12613. 'rtl.recNewT(this, "TRec", function () {',
  12614. ' this.i = 0;',
  12615. ' this.m$a$clone = function (a) {',
  12616. ' var b = [];',
  12617. ' b.length = 2;',
  12618. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12619. ' return b;',
  12620. ' };',
  12621. ' this.$new = function () {',
  12622. ' var r = Object.create(this);',
  12623. ' r.a = [];',
  12624. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12625. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12626. ' r.p = $mod.TPoint.$new();',
  12627. ' return r;',
  12628. ' };',
  12629. ' this.$eq = function (b) {',
  12630. ' 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);',
  12631. ' };',
  12632. ' this.$assign = function (s) {',
  12633. ' this.i = s.i;',
  12634. ' this.a = rtl.arrayRef(s.a);',
  12635. ' this.s = s.s.slice(0);',
  12636. ' this.m = this.m$a$clone(s.m);',
  12637. ' this.p.$assign(s.p);',
  12638. ' return this;',
  12639. ' };',
  12640. '});',
  12641. 'this.r = this.TRec.$clone({',
  12642. ' i: 1,',
  12643. ' a: [2, 3],',
  12644. ' s: [4, 5],',
  12645. ' m: [[11, 12], [13, 14]],',
  12646. ' p: this.TPoint.$clone({',
  12647. ' x: 21,',
  12648. ' y: 22',
  12649. ' })',
  12650. '});',
  12651. 'this.p = [this.TPoint.$clone({',
  12652. ' x: 1,',
  12653. ' y: 2',
  12654. '}), this.TPoint.$clone({',
  12655. ' x: 3,',
  12656. ' y: 4',
  12657. '})];',
  12658. '']),
  12659. LinesToStr([ // $mod.$main
  12660. '']));
  12661. end;
  12662. procedure TTestModule.TestRecord_TypecastFail;
  12663. begin
  12664. StartProgram(false);
  12665. Add([
  12666. 'type',
  12667. ' TPoint = record x,y: longint; end;',
  12668. ' TRec = record l: longint end;',
  12669. 'var p: TPoint;',
  12670. 'begin',
  12671. ' if TRec(p).l=2 then ;']);
  12672. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12673. nIllegalTypeConversionTo);
  12674. ConvertProgram;
  12675. end;
  12676. procedure TTestModule.TestRecord_InFunction;
  12677. begin
  12678. StartProgram(false);
  12679. Add([
  12680. 'var TPoint: longint = 3;',
  12681. 'procedure DoIt;',
  12682. 'type',
  12683. ' TPoint = record x,y: longint; end;',
  12684. ' TPoints = array of TPoint;',
  12685. 'var',
  12686. ' r: TPoint;',
  12687. ' p: TPoints;',
  12688. 'begin',
  12689. ' SetLength(p,2);',
  12690. 'end;',
  12691. 'begin']);
  12692. ConvertProgram;
  12693. CheckSource('TestRecord_InFunction',
  12694. LinesToStr([ // statements
  12695. 'this.TPoint = 3;',
  12696. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12697. ' this.x = 0;',
  12698. ' this.y = 0;',
  12699. ' this.$eq = function (b) {',
  12700. ' return (this.x === b.x) && (this.y === b.y);',
  12701. ' };',
  12702. ' this.$assign = function (s) {',
  12703. ' this.x = s.x;',
  12704. ' this.y = s.y;',
  12705. ' return this;',
  12706. ' };',
  12707. '});',
  12708. 'this.DoIt = function () {',
  12709. ' var r = TPoint$1.$new();',
  12710. ' var p = [];',
  12711. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12712. '};',
  12713. '']),
  12714. LinesToStr([ // $mod.$main
  12715. '']));
  12716. end;
  12717. procedure TTestModule.TestRecord_ArrayConstMultiline;
  12718. begin
  12719. StartProgram(false);
  12720. Add([
  12721. '{$mode delphi}',
  12722. 'type',
  12723. ' TBird = record Wing: string; end;',
  12724. 'const',
  12725. ' Birds: array[1..2] of TBird = (',
  12726. ' (Wing: ''''''',
  12727. ' First',
  12728. ' Second',
  12729. ' Third',
  12730. ' ''''''),',
  12731. ' (Wing: ''''''',
  12732. ' Value:=''Im in quotes''; ',
  12733. ' '''''')',
  12734. ' );',
  12735. 'begin']);
  12736. ConvertProgram;
  12737. CheckSource('TestRecord_ArrayConstMultiline',
  12738. LinesToStr([ // statements
  12739. 'rtl.recNewT(this, "TBird", function () {',
  12740. ' this.Wing = "";',
  12741. ' this.$eq = function (b) {',
  12742. ' return this.Wing === b.Wing;',
  12743. ' };',
  12744. ' this.$assign = function (s) {',
  12745. ' this.Wing = s.Wing;',
  12746. ' return this;',
  12747. ' };',
  12748. '});',
  12749. 'this.Birds$a$clone = function (a) {',
  12750. ' var b = [];',
  12751. ' b.length = 2;',
  12752. ' for (var c = 0; c < 2; c++) b[c] = $mod.TBird.$clone(a[c]);',
  12753. ' return b;',
  12754. '};',
  12755. 'this.Birds = [this.TBird.$clone({',
  12756. ' Wing: " First\n Second\n Third"',
  12757. '}), this.TBird.$clone({',
  12758. ' Wing: " Value:=''Im in quotes''; "',
  12759. '})];',
  12760. '']),
  12761. LinesToStr([ // $mod.$main
  12762. '']));
  12763. end;
  12764. procedure TTestModule.TestRecordAnonym_Field;
  12765. begin
  12766. StartProgram(false);
  12767. Add(['',
  12768. 'var Rec: record',
  12769. ' Bold: longint;',
  12770. ' end;',
  12771. 'begin',
  12772. ' rec.bold:=123;',
  12773. ' rec.bold:=rec.bold+7;',
  12774. '']);
  12775. ConvertProgram;
  12776. CheckSource('TestRecordAnonym_Field',
  12777. LinesToStr([ // statements
  12778. 'rtl.recNewT(this, "Rec$a", function () {',
  12779. ' this.Bold = 0;',
  12780. ' this.$eq = function (b) {',
  12781. ' return this.Bold === b.Bold;',
  12782. ' };',
  12783. ' this.$assign = function (s) {',
  12784. ' this.Bold = s.Bold;',
  12785. ' return this;',
  12786. ' };',
  12787. '});',
  12788. 'this.Rec = this.Rec$a.$new();',
  12789. '']),
  12790. LinesToStr([ // $mod.$main
  12791. '$mod.Rec.Bold = 123;',
  12792. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12793. '']));
  12794. end;
  12795. procedure TTestModule.TestRecordAnonym_Assign;
  12796. begin
  12797. StartProgram(false);
  12798. Add(['',
  12799. 'var S,T: record',
  12800. ' Bold: longint;',
  12801. ' end;',
  12802. ' b: boolean;',
  12803. 'begin',
  12804. ' S:=T;',
  12805. ' b:=s=t;',
  12806. '']);
  12807. ConvertProgram;
  12808. CheckSource('TestRecordAnonym_Assign',
  12809. LinesToStr([ // statements
  12810. 'rtl.recNewT(this, "T$a", function () {',
  12811. ' this.Bold = 0;',
  12812. ' this.$eq = function (b) {',
  12813. ' return this.Bold === b.Bold;',
  12814. ' };',
  12815. ' this.$assign = function (s) {',
  12816. ' this.Bold = s.Bold;',
  12817. ' return this;',
  12818. ' };',
  12819. '});',
  12820. 'this.S = this.T$a.$new();',
  12821. 'this.T = this.T$a.$new();',
  12822. 'this.b = false;',
  12823. '']),
  12824. LinesToStr([ // $mod.$main
  12825. '$mod.S.$assign($mod.T);',
  12826. '$mod.b = $mod.S.$eq($mod.T);',
  12827. '']));
  12828. end;
  12829. procedure TTestModule.TestRecordAnonym_Nested;
  12830. begin
  12831. StartProgram(false);
  12832. Add(['',
  12833. 'var S,T: record',
  12834. ' Bold: longint;',
  12835. ' Sub: record',
  12836. ' Color: word;',
  12837. ' end;',
  12838. ' end;',
  12839. ' b: boolean;',
  12840. 'begin',
  12841. ' S:=T;',
  12842. ' S.Sub:=T.Sub;',
  12843. ' S.Sub.Color:=T.Sub.Color+3;',
  12844. ' b:=s=t;',
  12845. ' b:=s.Sub=t.Sub;',
  12846. '']);
  12847. ConvertProgram;
  12848. CheckSource('TestRecordAnonym_Nested',
  12849. LinesToStr([ // statements
  12850. 'rtl.recNewT(this, "T$a", function () {',
  12851. ' this.Bold = 0;',
  12852. ' rtl.recNewT(this, "Sub$a", function () {',
  12853. ' this.Color = 0;',
  12854. ' this.$eq = function (b) {',
  12855. ' return this.Color === b.Color;',
  12856. ' };',
  12857. ' this.$assign = function (s) {',
  12858. ' this.Color = s.Color;',
  12859. ' return this;',
  12860. ' };',
  12861. ' });',
  12862. ' this.$new = function () {',
  12863. ' var r = Object.create(this);',
  12864. ' r.Sub = this.Sub$a.$new();',
  12865. ' return r;',
  12866. ' };',
  12867. ' this.$eq = function (b) {',
  12868. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12869. ' };',
  12870. ' this.$assign = function (s) {',
  12871. ' this.Bold = s.Bold;',
  12872. ' this.Sub.$assign(s.Sub);',
  12873. ' return this;',
  12874. ' };',
  12875. '}, true);',
  12876. 'this.S = this.T$a.$new();',
  12877. 'this.T = this.T$a.$new();',
  12878. 'this.b = false;',
  12879. '']),
  12880. LinesToStr([ // $mod.$main
  12881. '$mod.S.$assign($mod.T);',
  12882. '$mod.S.Sub.$assign($mod.T.Sub);',
  12883. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12884. '$mod.b = $mod.S.$eq($mod.T);',
  12885. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12886. '']));
  12887. end;
  12888. procedure TTestModule.TestRecordAnonym_Const;
  12889. begin
  12890. StartProgram(false);
  12891. Add(['',
  12892. 'var T: record',
  12893. ' Bold: longint;',
  12894. ' Sub: record',
  12895. ' Color: word;',
  12896. ' end;',
  12897. ' end = (Bold: 2; Sub: (Color: 3));',
  12898. 'begin',
  12899. '']);
  12900. ConvertProgram;
  12901. CheckSource('TestRecordAnonym_Const',
  12902. LinesToStr([ // statements
  12903. 'rtl.recNewT(this, "T$a", function () {',
  12904. ' this.Bold = 0;',
  12905. ' rtl.recNewT(this, "Sub$a", function () {',
  12906. ' this.Color = 0;',
  12907. ' this.$eq = function (b) {',
  12908. ' return this.Color === b.Color;',
  12909. ' };',
  12910. ' this.$assign = function (s) {',
  12911. ' this.Color = s.Color;',
  12912. ' return this;',
  12913. ' };',
  12914. ' });',
  12915. ' this.$new = function () {',
  12916. ' var r = Object.create(this);',
  12917. ' r.Sub = this.Sub$a.$new();',
  12918. ' return r;',
  12919. ' };',
  12920. ' this.$eq = function (b) {',
  12921. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12922. ' };',
  12923. ' this.$assign = function (s) {',
  12924. ' this.Bold = s.Bold;',
  12925. ' this.Sub.$assign(s.Sub);',
  12926. ' return this;',
  12927. ' };',
  12928. '}, true);',
  12929. 'this.T = this.T$a.$clone({',
  12930. ' Bold: 2,',
  12931. ' Sub: this.T$a.Sub$a.$clone({',
  12932. ' Color: 3',
  12933. ' })',
  12934. '});',
  12935. '']),
  12936. LinesToStr([ // $mod.$main
  12937. '']));
  12938. end;
  12939. procedure TTestModule.TestRecordAnonym_InFunction;
  12940. begin
  12941. StartProgram(false);
  12942. Add(['',
  12943. 'procedure Fly;',
  12944. 'var T: record',
  12945. ' Bold: longint;',
  12946. ' Sub: record',
  12947. ' Color: word;',
  12948. ' end;',
  12949. ' end = (Bold: 2; Sub: (Color: 3));',
  12950. 'begin',
  12951. 'end;',
  12952. 'begin',
  12953. '']);
  12954. ConvertProgram;
  12955. CheckSource('TestRecordAnonym_InFunction',
  12956. LinesToStr([ // statements
  12957. 'var T$a = rtl.recNewT(null, "", function () {',
  12958. ' this.Bold = 0;',
  12959. ' rtl.recNewT(this, "Sub$a", function () {',
  12960. ' this.Color = 0;',
  12961. ' this.$eq = function (b) {',
  12962. ' return this.Color === b.Color;',
  12963. ' };',
  12964. ' this.$assign = function (s) {',
  12965. ' this.Color = s.Color;',
  12966. ' return this;',
  12967. ' };',
  12968. ' });',
  12969. ' this.$new = function () {',
  12970. ' var r = Object.create(this);',
  12971. ' r.Sub = this.Sub$a.$new();',
  12972. ' return r;',
  12973. ' };',
  12974. ' this.$eq = function (b) {',
  12975. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12976. ' };',
  12977. ' this.$assign = function (s) {',
  12978. ' this.Bold = s.Bold;',
  12979. ' this.Sub.$assign(s.Sub);',
  12980. ' return this;',
  12981. ' };',
  12982. '}, true);',
  12983. 'this.Fly = function () {',
  12984. ' var T = T$a.$clone({',
  12985. ' Bold: 2,',
  12986. ' Sub: T$a.Sub$a.$clone({',
  12987. ' Color: 3',
  12988. ' })',
  12989. ' });',
  12990. '};',
  12991. '']),
  12992. LinesToStr([ // $mod.$main
  12993. '']));
  12994. end;
  12995. procedure TTestModule.TestAdvRecord_Function;
  12996. begin
  12997. StartProgram(false);
  12998. Parser.Options:=Parser.Options+[po_cassignments];
  12999. Add([
  13000. '{$modeswitch AdvancedRecords}',
  13001. 'type',
  13002. ' TPoint = record',
  13003. ' x,y: word;',
  13004. ' function Add(const apt: TPoint): TPoint;',
  13005. ' end;',
  13006. 'function TPoint.Add(const apt: TPoint): TPoint;',
  13007. 'begin',
  13008. ' Result:=Self;',
  13009. ' Result.x+=apt.x;',
  13010. ' Result.y:=Result.y+apt.y;',
  13011. ' Self:=apt;',
  13012. 'end;',
  13013. 'var p,q: TPoint;',
  13014. 'begin',
  13015. ' p.add(q);',
  13016. ' p:=default(TPoint);',
  13017. ' p:=q;',
  13018. '']);
  13019. ConvertProgram;
  13020. CheckSource('TestAdvRecord_Function',
  13021. LinesToStr([ // statements
  13022. 'rtl.recNewT(this, "TPoint", function () {',
  13023. ' this.x = 0;',
  13024. ' this.y = 0;',
  13025. ' this.$eq = function (b) {',
  13026. ' return (this.x === b.x) && (this.y === b.y);',
  13027. ' };',
  13028. ' this.$assign = function (s) {',
  13029. ' this.x = s.x;',
  13030. ' this.y = s.y;',
  13031. ' return this;',
  13032. ' };',
  13033. ' this.Add = function (apt) {',
  13034. ' var Result = $mod.TPoint.$new();',
  13035. ' Result.$assign(this);',
  13036. ' Result.x += apt.x;',
  13037. ' Result.y = Result.y + apt.y;',
  13038. ' this.$assign(apt);',
  13039. ' return Result;',
  13040. ' };',
  13041. '});',
  13042. 'this.p = this.TPoint.$new();',
  13043. 'this.q = this.TPoint.$new();',
  13044. '']),
  13045. LinesToStr([ // $mod.$main
  13046. '$mod.p.Add($mod.q);',
  13047. '$mod.p.$assign($mod.TPoint.$new());',
  13048. '$mod.p.$assign($mod.q);',
  13049. '']));
  13050. end;
  13051. procedure TTestModule.TestAdvRecord_Property;
  13052. begin
  13053. StartProgram(false);
  13054. Add([
  13055. '{$modeswitch AdvancedRecords}',
  13056. 'type',
  13057. ' TPoint = record',
  13058. ' x,y: word;',
  13059. ' strict private',
  13060. ' function GetSize: longword;',
  13061. ' procedure SetSize(Value: longword);',
  13062. ' public',
  13063. ' property Size: longword read GetSize write SetSize;',
  13064. ' property Left: word read x write y;',
  13065. ' end;',
  13066. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13067. 'function TPoint.GetSize: longword;',
  13068. 'begin',
  13069. ' x:=y;',
  13070. ' Size:=Size;',
  13071. ' Left:=Left;',
  13072. 'end;',
  13073. 'procedure TPoint.SetSize(Value: longword);',
  13074. 'begin',
  13075. 'end;',
  13076. 'var p,q: TPoint;',
  13077. 'begin',
  13078. ' p.Size:=q.Size;',
  13079. ' p.Left:=q.Left;',
  13080. '']);
  13081. ConvertProgram;
  13082. CheckSource('TestAdvRecord_Property',
  13083. LinesToStr([ // statements
  13084. 'rtl.recNewT(this, "TPoint", function () {',
  13085. ' this.x = 0;',
  13086. ' this.y = 0;',
  13087. ' this.$eq = function (b) {',
  13088. ' return (this.x === b.x) && (this.y === b.y);',
  13089. ' };',
  13090. ' this.$assign = function (s) {',
  13091. ' this.x = s.x;',
  13092. ' this.y = s.y;',
  13093. ' return this;',
  13094. ' };',
  13095. ' this.GetSize = function () {',
  13096. ' var Result = 0;',
  13097. ' this.x = this.y;',
  13098. ' this.SetSize(this.GetSize());',
  13099. ' this.y = this.x;',
  13100. ' return Result;',
  13101. ' };',
  13102. ' this.SetSize = function (Value) {',
  13103. ' };',
  13104. '});',
  13105. 'this.SetSize = function (Value) {',
  13106. '};',
  13107. 'this.p = this.TPoint.$new();',
  13108. 'this.q = this.TPoint.$new();',
  13109. '']),
  13110. LinesToStr([ // $mod.$main
  13111. '$mod.p.SetSize($mod.q.GetSize());',
  13112. '$mod.p.y = $mod.q.x;',
  13113. '']));
  13114. end;
  13115. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13116. begin
  13117. StartProgram(false);
  13118. Add([
  13119. '{$modeswitch AdvancedRecords}',
  13120. 'type',
  13121. ' TPoint = record',
  13122. ' strict private',
  13123. ' function GetItems(Index: word): word;',
  13124. ' procedure SetItems(Index: word; Value: word);',
  13125. ' public',
  13126. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13127. ' end;',
  13128. 'function TPoint.GetItems(Index: word): word;',
  13129. 'begin',
  13130. ' Items[index]:=Items[index];',
  13131. ' self.Items[index]:=self.Items[index];',
  13132. 'end;',
  13133. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13134. 'begin',
  13135. 'end;',
  13136. 'var p: TPoint;',
  13137. 'begin',
  13138. ' p[1]:=p[2];',
  13139. ' p.Items[3]:=p.Items[4];',
  13140. '']);
  13141. ConvertProgram;
  13142. CheckSource('TestAdvRecord_PropertyDefault',
  13143. LinesToStr([ // statements
  13144. 'rtl.recNewT(this, "TPoint", function () {',
  13145. ' this.$eq = function (b) {',
  13146. ' return true;',
  13147. ' };',
  13148. ' this.$assign = function (s) {',
  13149. ' return this;',
  13150. ' };',
  13151. ' this.GetItems = function (Index) {',
  13152. ' var Result = 0;',
  13153. ' this.SetItems(Index, this.GetItems(Index));',
  13154. ' this.SetItems(Index, this.GetItems(Index));',
  13155. ' return Result;',
  13156. ' };',
  13157. ' this.SetItems = function (Index, Value) {',
  13158. ' };',
  13159. '});',
  13160. 'this.p = this.TPoint.$new();',
  13161. '']),
  13162. LinesToStr([ // $mod.$main
  13163. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13164. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13165. '']));
  13166. end;
  13167. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13168. begin
  13169. StartProgram(false);
  13170. Add([
  13171. '{$modeswitch AdvancedRecords}',
  13172. 'type',
  13173. ' TRec = record',
  13174. ' class var',
  13175. ' Fx: longint;',
  13176. ' Fy: longint;',
  13177. ' class function GetInt: longint; static;',
  13178. ' class procedure SetInt(Value: longint); static;',
  13179. ' class procedure DoIt; static;',
  13180. ' class property IntA: longint read Fx write Fy;',
  13181. ' class property IntB: longint read GetInt write SetInt;',
  13182. ' end;',
  13183. 'class function trec.getint: longint;',
  13184. 'begin',
  13185. ' result:=fx;',
  13186. 'end;',
  13187. 'class procedure trec.setint(value: longint);',
  13188. 'begin',
  13189. 'end;',
  13190. 'class procedure trec.doit;',
  13191. 'begin',
  13192. ' IntA:=IntA+1;',
  13193. ' IntB:=IntB+1;',
  13194. 'end;',
  13195. 'var r: trec;',
  13196. 'begin',
  13197. ' trec.inta:=trec.inta+1;',
  13198. ' if trec.intb=2 then;',
  13199. ' trec.intb:=trec.intb+2;',
  13200. ' trec.setint(trec.inta);',
  13201. ' r.inta:=r.inta+1;',
  13202. ' if r.intb=2 then;',
  13203. ' r.intb:=r.intb+2;',
  13204. ' r.setint(r.inta);']);
  13205. ConvertProgram;
  13206. CheckSource('TestAdvRecord_Property_ClassMethod',
  13207. LinesToStr([ // statements
  13208. 'rtl.recNewT(this, "TRec", function () {',
  13209. ' this.Fx = 0;',
  13210. ' this.Fy = 0;',
  13211. ' this.$eq = function (b) {',
  13212. ' return true;',
  13213. ' };',
  13214. ' this.$assign = function (s) {',
  13215. ' return this;',
  13216. ' };',
  13217. ' this.GetInt = function () {',
  13218. ' var Result = 0;',
  13219. ' Result = $mod.TRec.Fx;',
  13220. ' return Result;',
  13221. ' };',
  13222. ' this.SetInt = function (Value) {',
  13223. ' };',
  13224. ' this.DoIt = function () {',
  13225. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13226. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13227. ' };',
  13228. '}, true);',
  13229. 'this.r = this.TRec.$new();',
  13230. '']),
  13231. LinesToStr([ // $mod.$main
  13232. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13233. 'if ($mod.TRec.GetInt() === 2) ;',
  13234. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13235. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13236. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13237. 'if ($mod.TRec.GetInt() === 2) ;',
  13238. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13239. '$mod.TRec.SetInt($mod.r.Fx);',
  13240. '']));
  13241. end;
  13242. procedure TTestModule.TestAdvRecord_Const;
  13243. begin
  13244. StartProgram(false);
  13245. Add([
  13246. '{$modeswitch AdvancedRecords}',
  13247. 'type',
  13248. ' TArrInt = array[3..4] of longint;',
  13249. ' TPoint = record',
  13250. ' x,y: longint;',
  13251. ' class var Count: nativeint;',
  13252. ' end;',
  13253. ' TRec = record',
  13254. ' i: longint;',
  13255. ' a: array of longint;',
  13256. ' s: array[1..2] of longint;',
  13257. ' m: array[1..2,3..4] of longint;',
  13258. ' p: TPoint;',
  13259. ' end;',
  13260. ' TPoints = array of TPoint;',
  13261. 'const',
  13262. ' r: TRec = (',
  13263. ' i:1;',
  13264. ' a:(2,3);',
  13265. ' s:(4,5);',
  13266. ' m:( (11,12), (13,14) );',
  13267. ' p: (x:21)',
  13268. ' );',
  13269. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13270. 'begin']);
  13271. ConvertProgram;
  13272. CheckSource('TestAdvRecord_Const',
  13273. LinesToStr([ // statements
  13274. 'rtl.recNewT(this, "TPoint", function () {',
  13275. ' this.x = 0;',
  13276. ' this.y = 0;',
  13277. ' this.Count = 0;',
  13278. ' this.$eq = function (b) {',
  13279. ' return (this.x === b.x) && (this.y === b.y);',
  13280. ' };',
  13281. ' this.$assign = function (s) {',
  13282. ' this.x = s.x;',
  13283. ' this.y = s.y;',
  13284. ' return this;',
  13285. ' };',
  13286. '}, true);',
  13287. 'rtl.recNewT(this, "TRec", function () {',
  13288. ' this.i = 0;',
  13289. ' this.m$a$clone = function (a) {',
  13290. ' var b = [];',
  13291. ' b.length = 2;',
  13292. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13293. ' return b;',
  13294. ' };',
  13295. ' this.$new = function () {',
  13296. ' var r = Object.create(this);',
  13297. ' r.a = [];',
  13298. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13299. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13300. ' r.p = $mod.TPoint.$new();',
  13301. ' return r;',
  13302. ' };',
  13303. ' this.$eq = function (b) {',
  13304. ' 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);',
  13305. ' };',
  13306. ' this.$assign = function (s) {',
  13307. ' this.i = s.i;',
  13308. ' this.a = rtl.arrayRef(s.a);',
  13309. ' this.s = s.s.slice(0);',
  13310. ' this.m = this.m$a$clone(s.m);',
  13311. ' this.p.$assign(s.p);',
  13312. ' return this;',
  13313. ' };',
  13314. '});',
  13315. 'this.r = this.TRec.$clone({',
  13316. ' i: 1,',
  13317. ' a: [2, 3],',
  13318. ' s: [4, 5],',
  13319. ' m: [[11, 12], [13, 14]],',
  13320. ' p: this.TPoint.$clone({',
  13321. ' x: 21,',
  13322. ' y: 0',
  13323. ' })',
  13324. '});',
  13325. 'this.p = [this.TPoint.$clone({',
  13326. ' x: 1,',
  13327. ' y: 2',
  13328. '}), this.TPoint.$clone({',
  13329. ' x: 3,',
  13330. ' y: 4',
  13331. '})];',
  13332. '']),
  13333. LinesToStr([ // $mod.$main
  13334. '']));
  13335. end;
  13336. procedure TTestModule.TestAdvRecord_ExternalField;
  13337. begin
  13338. StartProgram(false);
  13339. Add([
  13340. '{$modeswitch AdvancedRecords}',
  13341. '{$modeswitch externalclass}',
  13342. 'type',
  13343. ' TCar = record',
  13344. ' public',
  13345. ' Intern: longint external name ''$Intern'';',
  13346. ' Intern2: longint external name ''$Intern2'';',
  13347. ' Bracket: longint external name ''["A B"]'';',
  13348. ' procedure DoIt;',
  13349. ' end;',
  13350. 'procedure tcar.doit;',
  13351. 'begin',
  13352. ' Intern:=Intern+1;',
  13353. ' Intern2:=Intern2+2;',
  13354. ' Bracket:=Bracket+3;',
  13355. 'end;',
  13356. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13357. 'begin',
  13358. ' Rec.intern:=Rec.intern+1;',
  13359. ' Rec.intern2:=Rec.intern2+2;',
  13360. ' Rec.Bracket:=Rec.Bracket+3;',
  13361. ' with Rec do begin',
  13362. ' intern:=intern+1;',
  13363. ' intern2:=intern2+2;',
  13364. ' Bracket:=Bracket+3;',
  13365. ' end;']);
  13366. ConvertProgram;
  13367. CheckSource('TestAdvRecord_ExternalField',
  13368. LinesToStr([ // statements
  13369. 'rtl.recNewT(this, "TCar", function () {',
  13370. ' this.$eq = function (b) {',
  13371. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13372. ' };',
  13373. ' this.$assign = function (s) {',
  13374. ' this.$Intern = s.$Intern;',
  13375. ' this.$Intern2 = s.$Intern2;',
  13376. ' this["A B"] = s["A B"];',
  13377. ' return this;',
  13378. ' };',
  13379. ' this.DoIt = function () {',
  13380. ' this.$Intern = this.$Intern + 1;',
  13381. ' this.$Intern2 = this.$Intern2 + 2;',
  13382. ' this["A B"] = this["A B"] + 3;',
  13383. ' };',
  13384. '});',
  13385. 'this.Rec = this.TCar.$clone({',
  13386. ' $Intern: 11,',
  13387. ' $Intern2: 12,',
  13388. ' "A B": 13',
  13389. '});',
  13390. '']),
  13391. LinesToStr([ // $mod.$main
  13392. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13393. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13394. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13395. 'var $with = $mod.Rec;',
  13396. '$with.$Intern = $with.$Intern + 1;',
  13397. '$with.$Intern2 = $with.$Intern2 + 2;',
  13398. '$with["A B"] = $with["A B"] + 3;',
  13399. '']));
  13400. end;
  13401. procedure TTestModule.TestAdvRecord_SubRecord;
  13402. begin
  13403. StartProgram(false);
  13404. Add([
  13405. '{$modeswitch AdvancedRecords}',
  13406. 'type',
  13407. ' TRec = record',
  13408. ' type',
  13409. ' TPoint = record',
  13410. ' x,y: longint;',
  13411. ' class var Count: nativeint;',
  13412. ' procedure DoIt;',
  13413. ' class procedure DoThat; static;',
  13414. ' end;',
  13415. ' var',
  13416. ' i: longint;',
  13417. ' p: TPoint;',
  13418. ' procedure DoSome;',
  13419. ' end;',
  13420. 'const',
  13421. ' r: TRec = (',
  13422. ' i:1;',
  13423. ' p: (x:21;y:22)',
  13424. ' );',
  13425. 'procedure TRec.DoSome;',
  13426. 'begin',
  13427. ' p.x:=p.y+1;',
  13428. ' p.Count:=p.Count+2;',
  13429. 'end;',
  13430. 'procedure TRec.TPoint.DoIt;',
  13431. 'begin',
  13432. ' Count:=Count+3;',
  13433. 'end;',
  13434. 'class procedure TRec.TPoint.DoThat;',
  13435. 'begin',
  13436. ' Count:=Count+4;',
  13437. 'end;',
  13438. 'begin']);
  13439. ConvertProgram;
  13440. CheckSource('TestAdvRecord_SubRecord',
  13441. LinesToStr([ // statements
  13442. 'rtl.recNewT(this, "TRec", function () {',
  13443. ' rtl.recNewT(this, "TPoint", function () {',
  13444. ' this.x = 0;',
  13445. ' this.y = 0;',
  13446. ' this.Count = 0;',
  13447. ' this.$eq = function (b) {',
  13448. ' return (this.x === b.x) && (this.y === b.y);',
  13449. ' };',
  13450. ' this.$assign = function (s) {',
  13451. ' this.x = s.x;',
  13452. ' this.y = s.y;',
  13453. ' return this;',
  13454. ' };',
  13455. ' this.DoIt = function () {',
  13456. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13457. ' };',
  13458. ' this.DoThat = function () {',
  13459. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13460. ' };',
  13461. ' }, true);',
  13462. ' this.i = 0;',
  13463. ' this.$new = function () {',
  13464. ' var r = Object.create(this);',
  13465. ' r.p = this.TPoint.$new();',
  13466. ' return r;',
  13467. ' };',
  13468. ' this.$eq = function (b) {',
  13469. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13470. ' };',
  13471. ' this.$assign = function (s) {',
  13472. ' this.i = s.i;',
  13473. ' this.p.$assign(s.p);',
  13474. ' return this;',
  13475. ' };',
  13476. ' this.DoSome = function () {',
  13477. ' this.p.x = this.p.y + 1;',
  13478. ' this.TPoint.Count = this.p.Count + 2;',
  13479. ' };',
  13480. '}, true);',
  13481. 'this.r = this.TRec.$clone({',
  13482. ' i: 1,',
  13483. ' p: this.TRec.TPoint.$clone({',
  13484. ' x: 21,',
  13485. ' y: 22',
  13486. ' })',
  13487. '});',
  13488. '']),
  13489. LinesToStr([ // $mod.$main
  13490. '']));
  13491. end;
  13492. procedure TTestModule.TestAdvRecord_SubClass;
  13493. begin
  13494. StartProgram(false);
  13495. Add([
  13496. '{$modeswitch AdvancedRecords}',
  13497. 'type',
  13498. ' TObject = class end;',
  13499. ' TPoint = record',
  13500. ' type',
  13501. ' TBird = class',
  13502. ' procedure DoIt;',
  13503. ' class procedure Glob;',
  13504. ' end;',
  13505. ' procedure DoIt(b: TBird);',
  13506. ' end;',
  13507. 'procedure TPoint.TBird.DoIt;',
  13508. 'begin',
  13509. ' doit;',
  13510. ' self.doit;',
  13511. ' glob;',
  13512. ' self.glob;',
  13513. 'end;',
  13514. 'class procedure TPoint.TBird.Glob;',
  13515. 'begin',
  13516. ' glob;',
  13517. ' self.glob;',
  13518. 'end;',
  13519. 'procedure TPoint.DoIt(b: TBird);',
  13520. 'begin',
  13521. ' b.doit;',
  13522. ' b.glob;',
  13523. ' TBird.glob;',
  13524. 'end;',
  13525. 'begin',
  13526. '']);
  13527. ConvertProgram;
  13528. CheckSource('TestAdvRecord_SubClass',
  13529. LinesToStr([ // statements
  13530. 'rtl.createClass(this, "TObject", null, function () {',
  13531. ' this.$init = function () {',
  13532. ' };',
  13533. ' this.$final = function () {',
  13534. ' };',
  13535. '});',
  13536. 'rtl.recNewT(this, "TPoint", function () {',
  13537. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13538. ' this.DoIt = function () {',
  13539. ' this.DoIt();',
  13540. ' this.DoIt();',
  13541. ' this.$class.Glob();',
  13542. ' this.$class.Glob();',
  13543. ' };',
  13544. ' this.Glob = function () {',
  13545. ' this.Glob();',
  13546. ' this.Glob();',
  13547. ' };',
  13548. ' }, "TPoint.TBird");',
  13549. ' this.$eq = function (b) {',
  13550. ' return true;',
  13551. ' };',
  13552. ' this.$assign = function (s) {',
  13553. ' return this;',
  13554. ' };',
  13555. ' this.DoIt = function (b) {',
  13556. ' b.DoIt();',
  13557. ' b.$class.Glob();',
  13558. ' this.TBird.Glob();',
  13559. ' };',
  13560. '}, true);',
  13561. '']),
  13562. LinesToStr([ // $mod.$main
  13563. '']));
  13564. end;
  13565. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13566. begin
  13567. StartProgram(false);
  13568. Add([
  13569. '{$modeswitch AdvancedRecords}',
  13570. 'type',
  13571. ' IUnknown = interface end;',
  13572. ' TPoint = record',
  13573. ' type IBird = interface end;',
  13574. ' end;',
  13575. 'begin',
  13576. '']);
  13577. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13578. nNotYetImplemented);
  13579. ParseProgram;
  13580. end;
  13581. procedure TTestModule.TestAdvRecord_Constructor;
  13582. begin
  13583. StartProgram(false);
  13584. Add([
  13585. '{$modeswitch AdvancedRecords}',
  13586. 'type',
  13587. ' TPoint = record',
  13588. ' x,y: longint;',
  13589. ' class procedure Run(w: longint = 13); static;',
  13590. ' constructor Create(ax: longint; ay: longint = -1);',
  13591. ' end;',
  13592. 'class procedure tpoint.run(w: longint);',
  13593. 'begin',
  13594. ' run;',
  13595. ' run();',
  13596. 'end;',
  13597. 'constructor tpoint.create(ax,ay: longint);',
  13598. 'begin',
  13599. ' x:=ax;',
  13600. ' self.y:=ay;',
  13601. ' run;',
  13602. ' run(ax);',
  13603. 'end;',
  13604. 'var r: TPoint;',
  13605. 'begin',
  13606. ' r:=TPoint.Create(1,2);',
  13607. ' with TPoint do r:=Create(1,2);',
  13608. ' r.Create(3);',
  13609. ' r:=r.Create(4);',
  13610. '']);
  13611. ConvertProgram;
  13612. CheckSource('TestAdvRecord_Constructor',
  13613. LinesToStr([ // statements
  13614. 'rtl.recNewT(this, "TPoint", function () {',
  13615. ' this.x = 0;',
  13616. ' this.y = 0;',
  13617. ' this.$eq = function (b) {',
  13618. ' return (this.x === b.x) && (this.y === b.y);',
  13619. ' };',
  13620. ' this.$assign = function (s) {',
  13621. ' this.x = s.x;',
  13622. ' this.y = s.y;',
  13623. ' return this;',
  13624. ' };',
  13625. ' this.Run = function (w) {',
  13626. ' $mod.TPoint.Run(13);',
  13627. ' $mod.TPoint.Run(13);',
  13628. ' };',
  13629. ' this.Create = function (ax, ay) {',
  13630. ' this.x = ax;',
  13631. ' this.y = ay;',
  13632. ' this.Run(13);',
  13633. ' this.Run(ax);',
  13634. ' return this;',
  13635. ' };',
  13636. '});',
  13637. 'this.r = this.TPoint.$new();',
  13638. '']),
  13639. LinesToStr([ // $mod.$main
  13640. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13641. 'var $with = $mod.TPoint;',
  13642. '$mod.r.$assign($with.$new().Create(1, 2));',
  13643. '$mod.r.Create(3, -1);',
  13644. '$mod.r.$assign($mod.r.Create(4, -1));',
  13645. '']));
  13646. end;
  13647. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13648. begin
  13649. StartProgram(false);
  13650. Add([
  13651. '{$modeswitch AdvancedRecords}',
  13652. 'type',
  13653. ' TPoint = record',
  13654. ' class var x: longint;',
  13655. ' class procedure Fly; static;',
  13656. ' class constructor Init;',
  13657. ' end;',
  13658. 'var count: word;',
  13659. 'class procedure Tpoint.Fly;',
  13660. 'begin',
  13661. 'end;',
  13662. 'class constructor tpoint.init;',
  13663. 'begin',
  13664. ' count:=count+1;',
  13665. ' x:=x+3;',
  13666. ' tpoint.x:=tpoint.x+4;',
  13667. ' fly;',
  13668. ' tpoint.fly;',
  13669. 'end;',
  13670. 'var r: TPoint;',
  13671. 'begin',
  13672. ' r.x:=r.x+10;',
  13673. ' r.Fly;',
  13674. ' r.Fly();',
  13675. '']);
  13676. ConvertProgram;
  13677. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13678. LinesToStr([ // statements
  13679. 'rtl.recNewT(this, "TPoint", function () {',
  13680. ' this.x = 0;',
  13681. ' this.$eq = function (b) {',
  13682. ' return true;',
  13683. ' };',
  13684. ' this.$assign = function (s) {',
  13685. ' return this;',
  13686. ' };',
  13687. ' this.Fly = function () {',
  13688. ' };',
  13689. '}, true);',
  13690. 'this.count = 0;',
  13691. 'this.r = this.TPoint.$new();',
  13692. '']),
  13693. LinesToStr([ // $mod.$main
  13694. '(function () {',
  13695. ' $mod.count = $mod.count + 1;',
  13696. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13697. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13698. ' $mod.TPoint.Fly();',
  13699. ' $mod.TPoint.Fly();',
  13700. '})();',
  13701. '$mod.TPoint.x = $mod.r.x + 10;',
  13702. '$mod.TPoint.Fly();',
  13703. '$mod.TPoint.Fly();',
  13704. '']));
  13705. end;
  13706. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13707. begin
  13708. StartUnit(false);
  13709. Add([
  13710. 'interface',
  13711. '{$modeswitch AdvancedRecords}',
  13712. 'type',
  13713. ' TPoint = record',
  13714. ' class var x: longint;',
  13715. ' class procedure Fly; static;',
  13716. ' class constructor Init;',
  13717. ' end;',
  13718. 'implementation',
  13719. 'var count: word;',
  13720. 'class procedure Tpoint.Fly;',
  13721. 'begin',
  13722. 'end;',
  13723. 'class constructor tpoint.init;',
  13724. 'begin',
  13725. ' count:=count+1;',
  13726. ' x:=3;',
  13727. ' tpoint.x:=4;',
  13728. ' fly;',
  13729. ' tpoint.fly;',
  13730. 'end;',
  13731. '']);
  13732. ConvertUnit;
  13733. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13734. LinesToStr([ // statements
  13735. 'var $impl = $mod.$impl;',
  13736. 'rtl.recNewT(this, "TPoint", function () {',
  13737. ' this.x = 0;',
  13738. ' this.$eq = function (b) {',
  13739. ' return true;',
  13740. ' };',
  13741. ' this.$assign = function (s) {',
  13742. ' return this;',
  13743. ' };',
  13744. ' this.Fly = function () {',
  13745. ' };',
  13746. '}, true);',
  13747. '']),
  13748. LinesToStr([ // $mod.$init
  13749. '(function () {',
  13750. ' $impl.count = $impl.count + 1;',
  13751. ' $mod.TPoint.x = 3;',
  13752. ' $mod.TPoint.x = 4;',
  13753. ' $mod.TPoint.Fly();',
  13754. ' $mod.TPoint.Fly();',
  13755. '})();',
  13756. '']),
  13757. LinesToStr([ // $mod.$main
  13758. '$impl.count = 0;',
  13759. '']));
  13760. end;
  13761. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13762. begin
  13763. StartProgram(false);
  13764. Add(['type',
  13765. ' TObject = class',
  13766. ' public',
  13767. ' constructor Create;',
  13768. ' destructor Destroy;',
  13769. ' end;',
  13770. ' TBird = TObject;',
  13771. 'constructor tobject.create;',
  13772. 'begin end;',
  13773. 'destructor tobject.destroy;',
  13774. 'begin end;',
  13775. 'var Obj: tobject;',
  13776. 'begin',
  13777. ' obj:=tobject.create;',
  13778. ' obj:=tobject.create();',
  13779. ' obj:=tbird.create;',
  13780. ' obj:=tbird.create();',
  13781. ' obj:=obj.create();',
  13782. ' obj.destroy;',
  13783. '']);
  13784. ConvertProgram;
  13785. CheckSource('TestClass_TObjectDefaultConstructor',
  13786. LinesToStr([ // statements
  13787. 'rtl.createClass(this,"TObject",null,function(){',
  13788. ' this.$init = function () {',
  13789. ' };',
  13790. ' this.$final = function () {',
  13791. ' };',
  13792. ' this.Create = function(){',
  13793. ' return this;',
  13794. ' };',
  13795. ' this.Destroy = function(){',
  13796. ' };',
  13797. '});',
  13798. 'this.Obj = null;'
  13799. ]),
  13800. LinesToStr([ // $mod.$main
  13801. '$mod.Obj = $mod.TObject.$create("Create");',
  13802. '$mod.Obj = $mod.TObject.$create("Create");',
  13803. '$mod.Obj = $mod.TObject.$create("Create");',
  13804. '$mod.Obj = $mod.TObject.$create("Create");',
  13805. '$mod.Obj = $mod.Obj.Create();',
  13806. '$mod.Obj.$destroy("Destroy");',
  13807. '']));
  13808. end;
  13809. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13810. begin
  13811. StartProgram(false);
  13812. Add('type');
  13813. Add(' TObject = class');
  13814. Add(' public');
  13815. Add(' constructor Create(Par: longint);');
  13816. Add(' end;');
  13817. Add('constructor tobject.create(par: longint);');
  13818. Add('begin end;');
  13819. Add('var Obj: tobject;');
  13820. Add('begin');
  13821. Add(' obj:=tobject.create(3);');
  13822. ConvertProgram;
  13823. CheckSource('TestClass_TObjectConstructorWithParams',
  13824. LinesToStr([ // statements
  13825. 'rtl.createClass(this,"TObject",null,function(){',
  13826. ' this.$init = function () {',
  13827. ' };',
  13828. ' this.$final = function () {',
  13829. ' };',
  13830. ' this.Create = function(Par){',
  13831. ' return this;',
  13832. ' };',
  13833. '});',
  13834. 'this.Obj = null;'
  13835. ]),
  13836. LinesToStr([ // $mod.$main
  13837. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13838. ]));
  13839. end;
  13840. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13841. begin
  13842. StartProgram(false);
  13843. Add('type');
  13844. Add(' TObject = class');
  13845. Add(' public');
  13846. Add(' constructor Create;');
  13847. Add(' end;');
  13848. Add(' TTest = class(TObject)');
  13849. Add(' public');
  13850. Add(' constructor Create(const Par: longint = 1);');
  13851. Add(' end;');
  13852. Add('constructor tobject.create;');
  13853. Add('begin end;');
  13854. Add('constructor ttest.create(const par: longint);');
  13855. Add('begin end;');
  13856. Add('var t: ttest;');
  13857. Add('begin');
  13858. Add(' t:=ttest.create;');
  13859. Add(' t:=ttest.create(2);');
  13860. ConvertProgram;
  13861. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13862. LinesToStr([ // statements
  13863. 'rtl.createClass(this,"TObject",null,function(){',
  13864. ' this.$init = function () {',
  13865. ' };',
  13866. ' this.$final = function () {',
  13867. ' };',
  13868. ' this.Create = function(){',
  13869. ' return this;',
  13870. ' };',
  13871. '});',
  13872. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13873. ' this.Create$1 = function (Par) {',
  13874. ' return this;',
  13875. ' };',
  13876. '});',
  13877. 'this.t = null;'
  13878. ]),
  13879. LinesToStr([ // $mod.$main
  13880. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13881. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13882. ]));
  13883. end;
  13884. procedure TTestModule.TestClass_Var;
  13885. begin
  13886. StartProgram(false);
  13887. Add([
  13888. 'type',
  13889. ' TObject = class',
  13890. ' public',
  13891. ' vI: longint;',
  13892. ' constructor Create(Par: longint);',
  13893. ' end;',
  13894. 'constructor tobject.create(par: longint);',
  13895. 'begin',
  13896. ' vi:=par+3',
  13897. 'end;',
  13898. 'var Obj: tobject;',
  13899. 'begin',
  13900. ' obj:=tobject.create(4);',
  13901. ' obj.vi:=obj.VI+5;']);
  13902. ConvertProgram;
  13903. CheckSource('TestClass_Var',
  13904. LinesToStr([ // statements
  13905. 'rtl.createClass(this,"TObject",null,function(){',
  13906. ' this.$init = function () {',
  13907. ' this.vI = 0;',
  13908. ' };',
  13909. ' this.$final = function () {',
  13910. ' };',
  13911. ' this.Create = function(Par){',
  13912. ' this.vI = Par+3;',
  13913. ' return this;',
  13914. ' };',
  13915. '});',
  13916. 'this.Obj = null;'
  13917. ]),
  13918. LinesToStr([ // $mod.$main
  13919. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13920. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13921. ]));
  13922. end;
  13923. procedure TTestModule.TestClass_Method;
  13924. begin
  13925. StartProgram(false);
  13926. Add('type');
  13927. Add(' TObject = class');
  13928. Add(' public');
  13929. Add(' vI: longint;');
  13930. Add(' Sub: TObject;');
  13931. Add(' constructor Create;');
  13932. Add(' function GetIt(Par: longint): tobject;');
  13933. Add(' end;');
  13934. Add('constructor tobject.create; begin end;');
  13935. Add('function tobject.getit(par: longint): tobject;');
  13936. Add('begin');
  13937. Add(' Self.vi:=par+3;');
  13938. Add(' Result:=self.sub;');
  13939. Add('end;');
  13940. Add('var Obj: tobject;');
  13941. Add('begin');
  13942. Add(' obj:=tobject.create;');
  13943. Add(' obj.getit(4);');
  13944. Add(' obj.sub.sub:=nil;');
  13945. Add(' obj.sub.getit(5);');
  13946. Add(' obj.sub.getit(6).SUB:=nil;');
  13947. Add(' obj.sub.getit(7).GETIT(8);');
  13948. Add(' obj.sub.getit(9).SuB.getit(10);');
  13949. ConvertProgram;
  13950. CheckSource('TestClass_Method',
  13951. LinesToStr([ // statements
  13952. 'rtl.createClass(this,"TObject",null,function(){',
  13953. ' this.$init = function () {',
  13954. ' this.vI = 0;',
  13955. ' this.Sub = null;',
  13956. ' };',
  13957. ' this.$final = function () {',
  13958. ' this.Sub = undefined;',
  13959. ' };',
  13960. ' this.Create = function(){',
  13961. ' return this;',
  13962. ' };',
  13963. ' this.GetIt = function(Par){',
  13964. ' var Result = null;',
  13965. ' this.vI = Par + 3;',
  13966. ' Result = this.Sub;',
  13967. ' return Result;',
  13968. ' };',
  13969. '});',
  13970. 'this.Obj = null;'
  13971. ]),
  13972. LinesToStr([ // $mod.$main
  13973. '$mod.Obj = $mod.TObject.$create("Create");',
  13974. '$mod.Obj.GetIt(4);',
  13975. '$mod.Obj.Sub.Sub=null;',
  13976. '$mod.Obj.Sub.GetIt(5);',
  13977. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13978. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13979. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13980. ]));
  13981. end;
  13982. procedure TTestModule.TestClass_Implementation;
  13983. begin
  13984. StartUnit(false);
  13985. Add([
  13986. 'interface',
  13987. 'type',
  13988. ' TObject = class',
  13989. ' constructor Create;',
  13990. ' end;',
  13991. 'implementation',
  13992. 'type',
  13993. ' TIntClass = class',
  13994. ' constructor Create; reintroduce;',
  13995. ' class procedure DoGlob;',
  13996. ' end;',
  13997. 'constructor tintclass.create;',
  13998. 'begin',
  13999. ' inherited;',
  14000. ' inherited create;',
  14001. ' doglob;',
  14002. 'end;',
  14003. 'class procedure tintclass.doglob;',
  14004. 'begin',
  14005. 'end;',
  14006. 'constructor tobject.create;',
  14007. 'var',
  14008. ' iC: tintclass;',
  14009. 'begin',
  14010. ' ic:=tintclass.create;',
  14011. ' tintclass.doglob;',
  14012. ' ic.doglob;',
  14013. 'end;',
  14014. 'initialization',
  14015. ' tintclass.doglob;',
  14016. '']);
  14017. ConvertUnit;
  14018. CheckSource('TestClass_Implementation',
  14019. LinesToStr([ // statements
  14020. 'var $impl = $mod.$impl;',
  14021. 'rtl.createClass(this, "TObject", null, function () {',
  14022. ' this.$init = function () {',
  14023. ' };',
  14024. ' this.$final = function () {',
  14025. ' };',
  14026. ' this.Create = function () {',
  14027. ' var iC = null;',
  14028. ' iC = $impl.TIntClass.$create("Create$1");',
  14029. ' $impl.TIntClass.DoGlob();',
  14030. ' iC.$class.DoGlob();',
  14031. ' return this;',
  14032. ' };',
  14033. '});',
  14034. '']),
  14035. LinesToStr([ // $mod.$main
  14036. '$impl.TIntClass.DoGlob();',
  14037. '']),
  14038. LinesToStr([
  14039. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  14040. ' this.Create$1 = function () {',
  14041. ' $mod.TObject.Create.call(this);',
  14042. ' $mod.TObject.Create.call(this);',
  14043. ' this.$class.DoGlob();',
  14044. ' return this;',
  14045. ' };',
  14046. ' this.DoGlob = function () {',
  14047. ' };',
  14048. '});',
  14049. '']));
  14050. end;
  14051. procedure TTestModule.TestClass_Inheritance;
  14052. begin
  14053. StartProgram(false);
  14054. Add('type');
  14055. Add(' TObject = class');
  14056. Add(' public');
  14057. Add(' constructor Create;');
  14058. Add(' end;');
  14059. Add(' TClassA = class');
  14060. Add(' end;');
  14061. Add(' TClassB = class(TObject)');
  14062. Add(' procedure ProcB;');
  14063. Add(' end;');
  14064. Add('constructor tobject.create; begin end;');
  14065. Add('procedure tclassb.procb; begin end;');
  14066. Add('var');
  14067. Add(' oO: TObject;');
  14068. Add(' oA: TClassA;');
  14069. Add(' oB: TClassB;');
  14070. Add('begin');
  14071. Add(' oO:=tobject.Create;');
  14072. Add(' oA:=tclassa.Create;');
  14073. Add(' ob:=tclassb.Create;');
  14074. Add(' if oo is tclassa then ;');
  14075. Add(' ob:=oo as tclassb;');
  14076. Add(' (oo as tclassb).procb;');
  14077. ConvertProgram;
  14078. CheckSource('TestClass_Inheritance',
  14079. LinesToStr([ // statements
  14080. 'rtl.createClass(this,"TObject",null,function(){',
  14081. ' this.$init = function () {',
  14082. ' };',
  14083. ' this.$final = function () {',
  14084. ' };',
  14085. ' this.Create = function () {',
  14086. ' return this;',
  14087. ' };',
  14088. '});',
  14089. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14090. '});',
  14091. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14092. ' this.ProcB = function () {',
  14093. ' };',
  14094. '});',
  14095. 'this.oO = null;',
  14096. 'this.oA = null;',
  14097. 'this.oB = null;'
  14098. ]),
  14099. LinesToStr([ // $mod.$main
  14100. '$mod.oO = $mod.TObject.$create("Create");',
  14101. '$mod.oA = $mod.TClassA.$create("Create");',
  14102. '$mod.oB = $mod.TClassB.$create("Create");',
  14103. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14104. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14105. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14106. ]));
  14107. end;
  14108. procedure TTestModule.TestClass_TypeAlias;
  14109. begin
  14110. StartProgram(false);
  14111. Add([
  14112. '{$interfaces corba}',
  14113. 'type',
  14114. ' IObject = interface',
  14115. ' end;',
  14116. ' IBird = type IObject;',
  14117. ' TObject = class',
  14118. ' end;',
  14119. ' TBird = type TObject;',
  14120. 'var',
  14121. ' oObj: TObject;',
  14122. ' oBird: TBird;',
  14123. ' IntfObj: IObject;',
  14124. ' IntfBird: IBird;',
  14125. 'begin',
  14126. ' oObj:=oBird;',
  14127. '']);
  14128. ConvertProgram;
  14129. CheckSource('TestClass_TypeAlias',
  14130. LinesToStr([ // statements
  14131. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14132. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14133. 'rtl.createClass(this, "TObject", null, function () {',
  14134. ' this.$init = function () {',
  14135. ' };',
  14136. ' this.$final = function () {',
  14137. ' };',
  14138. '});',
  14139. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14140. '});',
  14141. 'this.oObj = null;',
  14142. 'this.oBird = null;',
  14143. 'this.IntfObj = null;',
  14144. 'this.IntfBird = null;',
  14145. '']),
  14146. LinesToStr([ // $mod.$main
  14147. '$mod.oObj = $mod.oBird;',
  14148. '']));
  14149. end;
  14150. procedure TTestModule.TestClass_AbstractMethod;
  14151. begin
  14152. StartProgram(false);
  14153. Add('type');
  14154. Add(' TObject = class');
  14155. Add(' public');
  14156. Add(' procedure DoIt; virtual; abstract;');
  14157. Add(' end;');
  14158. Add('begin');
  14159. ConvertProgram;
  14160. CheckSource('TestClass_AbstractMethod',
  14161. LinesToStr([ // statements
  14162. 'rtl.createClass(this,"TObject",null,function(){',
  14163. ' this.$init = function () {',
  14164. ' };',
  14165. ' this.$final = function () {',
  14166. ' };',
  14167. '});'
  14168. ]),
  14169. LinesToStr([ // this.$main
  14170. ''
  14171. ]));
  14172. end;
  14173. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14174. begin
  14175. StartProgram(false);
  14176. Add([
  14177. 'type',
  14178. ' TObject = class',
  14179. ' procedure DoAbstract; virtual; abstract;',
  14180. ' procedure DoVirtual; virtual;',
  14181. ' procedure DoIt;',
  14182. ' end;',
  14183. ' TA = class',
  14184. ' procedure doabstract; override;',
  14185. ' procedure dovirtual; override;',
  14186. ' procedure DoSome;',
  14187. ' end;',
  14188. 'procedure tobject.dovirtual;',
  14189. 'begin',
  14190. ' inherited; // call non existing ancestor -> ignore silently',
  14191. 'end;',
  14192. 'procedure tobject.doit;',
  14193. 'begin',
  14194. 'end;',
  14195. 'procedure ta.doabstract;',
  14196. 'begin',
  14197. ' inherited dovirtual; // call TObject.DoVirtual',
  14198. 'end;',
  14199. 'procedure ta.dovirtual;',
  14200. 'begin',
  14201. ' inherited; // call TObject.DoVirtual',
  14202. ' inherited dovirtual; // call TObject.DoVirtual',
  14203. ' inherited dovirtual(); // call TObject.DoVirtual',
  14204. ' doit;',
  14205. ' doit();',
  14206. 'end;',
  14207. 'procedure ta.dosome;',
  14208. 'begin',
  14209. ' inherited; // call non existing ancestor method -> silently ignore',
  14210. 'end;',
  14211. 'begin']);
  14212. ConvertProgram;
  14213. CheckSource('TestClass_CallInherited_ProcNoParams',
  14214. LinesToStr([ // statements
  14215. 'rtl.createClass(this,"TObject",null,function(){',
  14216. ' this.$init = function () {',
  14217. ' };',
  14218. ' this.$final = function () {',
  14219. ' };',
  14220. ' this.DoVirtual = function () {',
  14221. ' };',
  14222. ' this.DoIt = function () {',
  14223. ' };',
  14224. '});',
  14225. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14226. ' this.DoAbstract = function () {',
  14227. ' $mod.TObject.DoVirtual.call(this);',
  14228. ' };',
  14229. ' this.DoVirtual = function () {',
  14230. ' $mod.TObject.DoVirtual.call(this);',
  14231. ' $mod.TObject.DoVirtual.call(this);',
  14232. ' $mod.TObject.DoVirtual.call(this);',
  14233. ' this.DoIt();',
  14234. ' this.DoIt();',
  14235. ' };',
  14236. ' this.DoSome = function () {',
  14237. ' };',
  14238. '});'
  14239. ]),
  14240. LinesToStr([ // this.$main
  14241. ''
  14242. ]));
  14243. end;
  14244. procedure TTestModule.TestClass_CallInherited_WithParams;
  14245. begin
  14246. StartProgram(false);
  14247. Add([
  14248. 'type',
  14249. ' TObject = class',
  14250. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14251. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14252. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14253. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14254. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14255. ' end;',
  14256. ' TClassA = class',
  14257. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14258. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14259. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14260. ' end;',
  14261. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14262. 'begin',
  14263. 'end;',
  14264. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14265. 'begin',
  14266. 'end;',
  14267. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14268. 'begin',
  14269. 'end;',
  14270. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14271. 'begin',
  14272. 'end;',
  14273. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14274. 'begin',
  14275. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14276. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14277. 'end;',
  14278. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14279. 'begin',
  14280. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14281. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14282. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14283. ' doit(pa,pb);',
  14284. ' doit(pa);',
  14285. ' doit2(pa);',
  14286. ' doit2;',
  14287. 'end;',
  14288. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14289. 'begin',
  14290. ' pa:=inherited;',
  14291. 'end;',
  14292. 'begin']);
  14293. ConvertProgram;
  14294. CheckSource('TestClass_CallInherited_WithParams',
  14295. LinesToStr([ // statements
  14296. 'rtl.createClass(this,"TObject",null,function(){',
  14297. ' this.$init = function () {',
  14298. ' };',
  14299. ' this.$final = function () {',
  14300. ' };',
  14301. ' this.DoVirtual = function (pA,pB) {',
  14302. ' };',
  14303. ' this.DoIt = function (pA,pB) {',
  14304. ' };',
  14305. ' this.DoIt2 = function (pA,pB) {',
  14306. ' };',
  14307. ' this.GetIt = function (pA, pB) {',
  14308. ' var Result = 0;',
  14309. ' return Result;',
  14310. ' };',
  14311. '});',
  14312. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14313. ' this.DoAbstract = function (pA,pB) {',
  14314. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14315. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14316. ' };',
  14317. ' this.DoVirtual = function (pA,pB) {',
  14318. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14319. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14320. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14321. ' this.DoIt(pA,pB);',
  14322. ' this.DoIt(pA,0);',
  14323. ' this.DoIt2(pA,2);',
  14324. ' this.DoIt2(1,2);',
  14325. ' };',
  14326. ' this.GetIt$1 = function (pA, pB) {',
  14327. ' var Result = 0;',
  14328. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14329. ' return Result;',
  14330. ' };',
  14331. '});'
  14332. ]),
  14333. LinesToStr([ // this.$main
  14334. ''
  14335. ]));
  14336. end;
  14337. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14338. begin
  14339. StartProgram(false);
  14340. Add('type');
  14341. Add(' TObject = class');
  14342. Add(' constructor Create; virtual;');
  14343. Add(' constructor CreateWithB(b: boolean);');
  14344. Add(' end;');
  14345. Add(' TA = class');
  14346. Add(' constructor Create; override;');
  14347. Add(' constructor CreateWithC(c: char);');
  14348. Add(' procedure DoIt;');
  14349. Add(' class function DoSome: TObject;');
  14350. Add(' end;');
  14351. Add('constructor tobject.create;');
  14352. Add('begin');
  14353. Add(' inherited; // call non existing ancestor -> ignore silently');
  14354. Add('end;');
  14355. Add('constructor tobject.createwithb(b: boolean);');
  14356. Add('begin');
  14357. Add(' inherited; // call non existing ancestor -> ignore silently');
  14358. Add(' create; // normal call');
  14359. Add('end;');
  14360. Add('constructor ta.create;');
  14361. Add('begin');
  14362. Add(' inherited; // normal call TObject.Create');
  14363. Add(' inherited create; // normal call TObject.Create');
  14364. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14365. Add('end;');
  14366. Add('constructor ta.createwithc(c: char);');
  14367. Add('begin');
  14368. Add(' inherited create; // call TObject.Create');
  14369. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14370. Add(' doit;');
  14371. Add(' doit();');
  14372. Add(' dosome;');
  14373. Add('end;');
  14374. Add('procedure ta.doit;');
  14375. Add('begin');
  14376. Add(' create; // normal call');
  14377. Add(' createwithb(false); // normal call');
  14378. Add(' createwithc(''c''); // normal call');
  14379. Add('end;');
  14380. Add('class function ta.dosome: TObject;');
  14381. Add('begin');
  14382. Add(' Result:=create; // constructor');
  14383. Add(' Result:=createwithb(true); // constructor');
  14384. Add(' Result:=createwithc(''c''); // constructor');
  14385. Add('end;');
  14386. Add('begin');
  14387. ConvertProgram;
  14388. CheckSource('TestClass_CallInheritedConstructor',
  14389. LinesToStr([ // statements
  14390. 'rtl.createClass(this,"TObject",null,function(){',
  14391. ' this.$init = function () {',
  14392. ' };',
  14393. ' this.$final = function () {',
  14394. ' };',
  14395. ' this.Create = function () {',
  14396. ' return this;',
  14397. ' };',
  14398. ' this.CreateWithB = function (b) {',
  14399. ' this.Create();',
  14400. ' return this;',
  14401. ' };',
  14402. '});',
  14403. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14404. ' this.Create = function () {',
  14405. ' $mod.TObject.Create.call(this);',
  14406. ' $mod.TObject.Create.call(this);',
  14407. ' $mod.TObject.CreateWithB.call(this, false);',
  14408. ' return this;',
  14409. ' };',
  14410. ' this.CreateWithC = function (c) {',
  14411. ' $mod.TObject.Create.call(this);',
  14412. ' $mod.TObject.CreateWithB.call(this, true);',
  14413. ' this.DoIt();',
  14414. ' this.DoIt();',
  14415. ' this.$class.DoSome();',
  14416. ' return this;',
  14417. ' };',
  14418. ' this.DoIt = function () {',
  14419. ' this.Create();',
  14420. ' this.CreateWithB(false);',
  14421. ' this.CreateWithC("c");',
  14422. ' };',
  14423. ' this.DoSome = function () {',
  14424. ' var Result = null;',
  14425. ' Result = this.$create("Create");',
  14426. ' Result = this.$create("CreateWithB", [true]);',
  14427. ' Result = this.$create("CreateWithC", ["c"]);',
  14428. ' return Result;',
  14429. ' };',
  14430. '});'
  14431. ]),
  14432. LinesToStr([ // this.$main
  14433. ''
  14434. ]));
  14435. end;
  14436. procedure TTestModule.TestClass_ClassVar_Assign;
  14437. begin
  14438. StartProgram(false);
  14439. Add([
  14440. 'type',
  14441. ' TObject = class',
  14442. ' public',
  14443. ' class var vI: longint;',
  14444. ' class var Sub: TObject;',
  14445. ' constructor Create;',
  14446. ' class function GetIt(var Par: longint): tobject;',
  14447. ' end;',
  14448. 'constructor tobject.create;',
  14449. 'begin',
  14450. ' vi:=vi+1;',
  14451. ' Self.vi:=Self.vi+1;',
  14452. ' inc(vi);',
  14453. 'end;',
  14454. 'class function tobject.getit(var par: longint): tobject;',
  14455. 'begin',
  14456. ' vi:=vi+3;',
  14457. ' Self.vi:=Self.vi+4;',
  14458. ' inc(vi);',
  14459. ' Result:=self.sub;',
  14460. ' GetIt(vi);',
  14461. 'end;',
  14462. 'var Obj: tobject;',
  14463. 'begin',
  14464. ' obj:=tobject.create;',
  14465. ' tobject.vi:=3;',
  14466. ' if tobject.vi=4 then ;',
  14467. ' tobject.sub:=nil;',
  14468. ' obj.sub:=nil;',
  14469. ' obj.sub.sub:=nil;']);
  14470. ConvertProgram;
  14471. CheckSource('TestClass_ClassVar_Assign',
  14472. LinesToStr([ // statements
  14473. 'rtl.createClass(this,"TObject",null,function(){',
  14474. ' this.vI = 0;',
  14475. ' this.Sub = null;',
  14476. ' this.$init = function () {',
  14477. ' };',
  14478. ' this.$final = function () {',
  14479. ' };',
  14480. ' this.Create = function(){',
  14481. ' $mod.TObject.vI = this.vI+1;',
  14482. ' $mod.TObject.vI = this.vI+1;',
  14483. ' $mod.TObject.vI += 1;',
  14484. ' return this;',
  14485. ' };',
  14486. ' this.GetIt = function(Par){',
  14487. ' var Result = null;',
  14488. ' $mod.TObject.vI = this.vI + 3;',
  14489. ' $mod.TObject.vI = this.vI + 4;',
  14490. ' $mod.TObject.vI += 1;',
  14491. ' Result = this.Sub;',
  14492. ' this.GetIt({',
  14493. ' p: $mod.TObject,',
  14494. ' get: function () {',
  14495. ' return this.p.vI;',
  14496. ' },',
  14497. ' set: function (v) {',
  14498. ' this.p.vI = v;',
  14499. ' }',
  14500. ' });',
  14501. ' return Result;',
  14502. ' };',
  14503. '});',
  14504. 'this.Obj = null;'
  14505. ]),
  14506. LinesToStr([ // $mod.$main
  14507. '$mod.Obj = $mod.TObject.$create("Create");',
  14508. '$mod.TObject.vI = 3;',
  14509. 'if ($mod.TObject.vI === 4);',
  14510. '$mod.TObject.Sub=null;',
  14511. '$mod.TObject.Sub=null;',
  14512. '$mod.TObject.Sub=null;',
  14513. '']));
  14514. end;
  14515. procedure TTestModule.TestClass_CallClassMethod;
  14516. begin
  14517. StartProgram(false);
  14518. Add('type');
  14519. Add(' TObject = class');
  14520. Add(' public');
  14521. Add(' class var vI: longint;');
  14522. Add(' class var Sub: TObject;');
  14523. Add(' constructor Create;');
  14524. Add(' function GetMore(Par: longint): longint;');
  14525. Add(' class function GetIt(Par: longint): tobject;');
  14526. Add(' end;');
  14527. Add('constructor tobject.create;');
  14528. Add('begin');
  14529. Add(' sub:=getit(3);');
  14530. Add(' vi:=getmore(4);');
  14531. Add(' sub:=Self.getit(5);');
  14532. Add(' vi:=Self.getmore(6);');
  14533. Add('end;');
  14534. Add('function tobject.getmore(par: longint): longint;');
  14535. Add('begin');
  14536. Add(' sub:=getit(11);');
  14537. Add(' vi:=getmore(12);');
  14538. Add(' sub:=self.getit(13);');
  14539. Add(' vi:=self.getmore(14);');
  14540. Add('end;');
  14541. Add('class function tobject.getit(par: longint): tobject;');
  14542. Add('begin');
  14543. Add(' sub:=getit(21);');
  14544. Add(' vi:=sub.getmore(22);');
  14545. Add(' sub:=self.getit(23);');
  14546. Add(' vi:=self.sub.getmore(24);');
  14547. Add('end;');
  14548. Add('var Obj: tobject;');
  14549. Add('begin');
  14550. Add(' obj:=tobject.create;');
  14551. Add(' tobject.getit(5);');
  14552. Add(' obj.getit(6);');
  14553. Add(' obj.sub.getit(7);');
  14554. Add(' obj.sub.getit(8).SUB:=nil;');
  14555. Add(' obj.sub.getit(9).GETIT(10);');
  14556. Add(' obj.sub.getit(11).SuB.getit(12);');
  14557. ConvertProgram;
  14558. CheckSource('TestClass_CallClassMethod',
  14559. LinesToStr([ // statements
  14560. 'rtl.createClass(this,"TObject",null,function(){',
  14561. ' this.vI = 0;',
  14562. ' this.Sub = null;',
  14563. ' this.$init = function () {',
  14564. ' };',
  14565. ' this.$final = function () {',
  14566. ' };',
  14567. ' this.Create = function(){',
  14568. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14569. ' $mod.TObject.vI = this.GetMore(4);',
  14570. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14571. ' $mod.TObject.vI = this.GetMore(6);',
  14572. ' return this;',
  14573. ' };',
  14574. ' this.GetMore = function(Par){',
  14575. ' var Result = 0;',
  14576. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14577. ' $mod.TObject.vI = this.GetMore(12);',
  14578. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14579. ' $mod.TObject.vI = this.GetMore(14);',
  14580. ' return Result;',
  14581. ' };',
  14582. ' this.GetIt = function(Par){',
  14583. ' var Result = null;',
  14584. ' $mod.TObject.Sub = this.GetIt(21);',
  14585. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14586. ' $mod.TObject.Sub = this.GetIt(23);',
  14587. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14588. ' return Result;',
  14589. ' };',
  14590. '});',
  14591. 'this.Obj = null;'
  14592. ]),
  14593. LinesToStr([ // $mod.$main
  14594. '$mod.Obj = $mod.TObject.$create("Create");',
  14595. '$mod.TObject.GetIt(5);',
  14596. '$mod.Obj.$class.GetIt(6);',
  14597. '$mod.Obj.Sub.$class.GetIt(7);',
  14598. '$mod.TObject.Sub=null;',
  14599. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14600. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14601. '']));
  14602. end;
  14603. procedure TTestModule.TestClass_CallClassMethodStatic;
  14604. begin
  14605. StartProgram(false);
  14606. Add([
  14607. 'type',
  14608. ' TObject = class',
  14609. ' public',
  14610. ' class function Fly: tobject; static;',
  14611. ' end;',
  14612. 'class function tobject.Fly: tobject;',
  14613. 'begin',
  14614. ' Result.Fly;',
  14615. ' Result.Fly();',
  14616. ' Fly;',
  14617. ' Fly();',
  14618. ' Fly.Fly;',
  14619. ' Fly.Fly();',
  14620. 'end;',
  14621. 'var Obj: tobject;',
  14622. 'begin',
  14623. ' obj.Fly;',
  14624. ' obj.Fly();',
  14625. ' with obj do begin',
  14626. ' Fly;',
  14627. ' Fly();',
  14628. ' end;',
  14629. '']);
  14630. ConvertProgram;
  14631. CheckSource('TestClass_CallClassMethodStatic',
  14632. LinesToStr([ // statements
  14633. 'rtl.createClass(this, "TObject", null, function () {',
  14634. ' this.$init = function () {',
  14635. ' };',
  14636. ' this.$final = function () {',
  14637. ' };',
  14638. ' this.Fly = function () {',
  14639. ' var Result = null;',
  14640. ' $mod.TObject.Fly();',
  14641. ' $mod.TObject.Fly();',
  14642. ' $mod.TObject.Fly();',
  14643. ' $mod.TObject.Fly();',
  14644. ' $mod.TObject.Fly();',
  14645. ' $mod.TObject.Fly();',
  14646. ' return Result;',
  14647. ' };',
  14648. '});',
  14649. 'this.Obj = null;'
  14650. ]),
  14651. LinesToStr([ // $mod.$main
  14652. '$mod.TObject.Fly();',
  14653. '$mod.TObject.Fly();',
  14654. 'var $with = $mod.Obj;',
  14655. '$with.Fly();',
  14656. '$with.Fly();',
  14657. '']));
  14658. end;
  14659. procedure TTestModule.TestClass_Property;
  14660. begin
  14661. StartProgram(false);
  14662. Add('type');
  14663. Add(' TObject = class');
  14664. Add(' Fx: longint;');
  14665. Add(' Fy: longint;');
  14666. Add(' function GetInt: longint;');
  14667. Add(' procedure SetInt(Value: longint);');
  14668. Add(' procedure DoIt;');
  14669. Add(' property IntA: longint read Fx write Fy;');
  14670. Add(' property IntB: longint read GetInt write SetInt;');
  14671. Add(' end;');
  14672. Add('function tobject.getint: longint;');
  14673. Add('begin');
  14674. Add(' result:=fx;');
  14675. Add('end;');
  14676. Add('procedure tobject.setint(value: longint);');
  14677. Add('begin');
  14678. Add(' if value=fy then exit;');
  14679. Add(' fy:=value;');
  14680. Add('end;');
  14681. Add('procedure tobject.doit;');
  14682. Add('begin');
  14683. Add(' IntA:=IntA+1;');
  14684. Add(' Self.IntA:=Self.IntA+1;');
  14685. Add(' IntB:=IntB+1;');
  14686. Add(' Self.IntB:=Self.IntB+1;');
  14687. Add('end;');
  14688. Add('var Obj: tobject;');
  14689. Add('begin');
  14690. Add(' obj.inta:=obj.inta+1;');
  14691. Add(' if obj.intb=2 then;');
  14692. Add(' obj.intb:=obj.intb+2;');
  14693. Add(' obj.setint(obj.inta);');
  14694. ConvertProgram;
  14695. CheckSource('TestClass_Property',
  14696. LinesToStr([ // statements
  14697. 'rtl.createClass(this, "TObject", null, function () {',
  14698. ' this.$init = function () {',
  14699. ' this.Fx = 0;',
  14700. ' this.Fy = 0;',
  14701. ' };',
  14702. ' this.$final = function () {',
  14703. ' };',
  14704. ' this.GetInt = function () {',
  14705. ' var Result = 0;',
  14706. ' Result = this.Fx;',
  14707. ' return Result;',
  14708. ' };',
  14709. ' this.SetInt = function (Value) {',
  14710. ' if (Value === this.Fy) return;',
  14711. ' this.Fy = Value;',
  14712. ' };',
  14713. ' this.DoIt = function () {',
  14714. ' this.Fy = this.Fx + 1;',
  14715. ' this.Fy = this.Fx + 1;',
  14716. ' this.SetInt(this.GetInt() + 1);',
  14717. ' this.SetInt(this.GetInt() + 1);',
  14718. ' };',
  14719. '});',
  14720. 'this.Obj = null;'
  14721. ]),
  14722. LinesToStr([ // $mod.$main
  14723. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14724. 'if ($mod.Obj.GetInt() === 2);',
  14725. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14726. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14727. ]));
  14728. end;
  14729. procedure TTestModule.TestClass_Property_ClassMethod;
  14730. begin
  14731. StartProgram(false);
  14732. Add([
  14733. 'type',
  14734. ' TObject = class',
  14735. ' class var Fx: longint;',
  14736. ' class var Fy: longint;',
  14737. ' class function GetInt: longint;',
  14738. ' class procedure SetInt(Value: longint);',
  14739. ' end;',
  14740. ' TBird = class',
  14741. ' class procedure DoIt;',
  14742. ' class property IntA: longint read Fx write Fy;',
  14743. ' class property IntB: longint read GetInt write SetInt;',
  14744. ' end;',
  14745. 'class function tobject.getint: longint;',
  14746. 'begin',
  14747. ' result:=fx;',
  14748. 'end;',
  14749. 'class procedure tobject.setint(value: longint);',
  14750. 'begin',
  14751. 'end;',
  14752. 'class procedure tbird.doit;',
  14753. 'begin',
  14754. ' FX:=3;',
  14755. ' IntA:=IntA+1;',
  14756. ' Self.IntA:=Self.IntA+1;',
  14757. ' IntB:=IntB+1;',
  14758. ' Self.IntB:=Self.IntB+1;',
  14759. ' with Self do begin',
  14760. ' FX:=11;',
  14761. ' IntA:=IntA+12;',
  14762. ' IntB:=IntB+13;',
  14763. ' end;',
  14764. 'end;',
  14765. 'var Obj: tbird;',
  14766. 'begin',
  14767. ' tbird.fx:=tbird.fx+1;',
  14768. ' tbird.inta:=tbird.inta+1;',
  14769. ' if tbird.intb=2 then;',
  14770. ' tbird.intb:=tbird.intb+2;',
  14771. ' tbird.setint(tbird.inta);',
  14772. ' obj.inta:=obj.inta+1;',
  14773. ' if obj.intb=2 then;',
  14774. ' obj.intb:=obj.intb+2;',
  14775. ' obj.setint(obj.inta);',
  14776. ' with Tbird do begin',
  14777. ' FX:=FY+1;',
  14778. ' inta:=inta+2;',
  14779. ' intb:=intb+3;',
  14780. ' end;',
  14781. ' with Obj do begin',
  14782. ' FX:=FY+1;',
  14783. ' inta:=inta+2;',
  14784. ' intb:=intb+3;',
  14785. ' end;',
  14786. '']);
  14787. ConvertProgram;
  14788. CheckSource('TestClass_Property_ClassMethod',
  14789. LinesToStr([ // statements
  14790. 'rtl.createClass(this, "TObject", null, function () {',
  14791. ' this.Fx = 0;',
  14792. ' this.Fy = 0;',
  14793. ' this.$init = function () {',
  14794. ' };',
  14795. ' this.$final = function () {',
  14796. ' };',
  14797. ' this.GetInt = function () {',
  14798. ' var Result = 0;',
  14799. ' Result = this.Fx;',
  14800. ' return Result;',
  14801. ' };',
  14802. ' this.SetInt = function (Value) {',
  14803. ' };',
  14804. '});',
  14805. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14806. ' this.DoIt = function () {',
  14807. ' $mod.TObject.Fx = 3;',
  14808. ' $mod.TObject.Fy = this.Fx + 1;',
  14809. ' $mod.TObject.Fy = this.Fx + 1;',
  14810. ' this.SetInt(this.GetInt() + 1);',
  14811. ' this.SetInt(this.GetInt() + 1);',
  14812. ' $mod.TObject.Fx = 11;',
  14813. ' $mod.TObject.Fy = this.Fx + 12;',
  14814. ' this.SetInt(this.GetInt() + 13);',
  14815. ' };',
  14816. '});',
  14817. 'this.Obj = null;'
  14818. ]),
  14819. LinesToStr([ // $mod.$main
  14820. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14821. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14822. 'if ($mod.TBird.GetInt() === 2);',
  14823. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14824. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14825. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14826. 'if ($mod.Obj.$class.GetInt() === 2);',
  14827. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14828. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14829. 'var $with = $mod.TBird;',
  14830. '$mod.TObject.Fx = $with.Fy + 1;',
  14831. '$mod.TObject.Fy = $with.Fx + 2;',
  14832. '$with.SetInt($with.GetInt() + 3);',
  14833. 'var $with1 = $mod.Obj;',
  14834. '$mod.TObject.Fx = $with1.Fy + 1;',
  14835. '$mod.TObject.Fy = $with1.Fx + 2;',
  14836. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14837. '']));
  14838. end;
  14839. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14840. begin
  14841. StartProgram(false);
  14842. Add([
  14843. 'type',
  14844. ' TObject = class',
  14845. ' class function GetInt: longint; static;',
  14846. ' class procedure SetInt(Value: longint); static;',
  14847. ' class function GetItems(Index: word): longint; static;',
  14848. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14849. ' end;',
  14850. ' TBird = class',
  14851. ' class procedure Fly;',
  14852. ' class property IntA: longint read GetInt write SetInt;',
  14853. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14854. ' end;',
  14855. 'class function tobject.getint: longint;',
  14856. 'begin',
  14857. 'end;',
  14858. 'class procedure tobject.setint(value: longint);',
  14859. 'begin',
  14860. 'end;',
  14861. 'class function tobject.GetItems(Index: word): longint;',
  14862. 'begin',
  14863. 'end;',
  14864. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14865. 'begin',
  14866. 'end;',
  14867. 'class procedure tbird.fly;',
  14868. 'var w: longint;',
  14869. 'begin',
  14870. ' inta:=inta+51;',
  14871. ' w:=items[52];',
  14872. ' items[53]:=54;',
  14873. 'end;',
  14874. 'var Obj: tbird;',
  14875. ' i: longint;',
  14876. 'begin',
  14877. ' tbird.inta:=tbird.inta+1;',
  14878. ' i:=tbird.items[2];',
  14879. ' tbird.items[3]:=4;',
  14880. ' obj.inta:=obj.inta+11;',
  14881. ' i:=obj.items[12];',
  14882. ' obj.items[13]:=14;',
  14883. ' with Tbird do begin',
  14884. ' inta:=inta+21;',
  14885. ' i:=items[22];',
  14886. ' items[23]:=24;',
  14887. ' end;',
  14888. ' with Obj do begin',
  14889. ' inta:=inta+31;',
  14890. ' i:=items[32];',
  14891. ' items[33]:=34;',
  14892. ' end;',
  14893. '']);
  14894. ConvertProgram;
  14895. CheckSource('TestClass_Property_ClassMethod',
  14896. LinesToStr([ // statements
  14897. 'rtl.createClass(this, "TObject", null, function () {',
  14898. ' this.$init = function () {',
  14899. ' };',
  14900. ' this.$final = function () {',
  14901. ' };',
  14902. ' this.GetInt = function () {',
  14903. ' var Result = 0;',
  14904. ' return Result;',
  14905. ' };',
  14906. ' this.SetInt = function (Value) {',
  14907. ' };',
  14908. ' this.GetItems = function (Index) {',
  14909. ' var Result = 0;',
  14910. ' return Result;',
  14911. ' };',
  14912. ' this.SetItems = function (Index, Value) {',
  14913. ' };',
  14914. '});',
  14915. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14916. ' this.Fly = function () {',
  14917. ' var w = 0;',
  14918. ' this.SetInt(this.GetInt() + 51);',
  14919. ' w = this.GetItems(52);',
  14920. ' this.SetItems(53, 54);',
  14921. ' };',
  14922. '});',
  14923. 'this.Obj = null;',
  14924. 'this.i = 0;',
  14925. '']),
  14926. LinesToStr([ // $mod.$main
  14927. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14928. '$mod.i = $mod.TObject.GetItems(2);',
  14929. '$mod.TObject.SetItems(3, 4);',
  14930. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14931. '$mod.i = $mod.TObject.GetItems(12);',
  14932. '$mod.TObject.SetItems(13, 14);',
  14933. 'var $with = $mod.TBird;',
  14934. '$with.SetInt($with.GetInt() + 21);',
  14935. '$mod.i = $with.GetItems(22);',
  14936. '$with.SetItems(23, 24);',
  14937. 'var $with1 = $mod.Obj;',
  14938. '$with1.SetInt($with1.GetInt() + 31);',
  14939. '$mod.i = $with1.GetItems(32);',
  14940. '$with1.SetItems(33, 34);',
  14941. '']));
  14942. end;
  14943. procedure TTestModule.TestClass_Property_Indexed;
  14944. begin
  14945. StartProgram(false);
  14946. Add([
  14947. 'type',
  14948. ' TObject = class',
  14949. ' FItems: array of longint;',
  14950. ' function GetItems(Index: longint): longint;',
  14951. ' procedure SetItems(Index: longint; Value: longint);',
  14952. ' procedure DoIt;',
  14953. ' property Items[Index: longint]: longint read getitems write setitems;',
  14954. ' end;',
  14955. 'function tobject.getitems(index: longint): longint;',
  14956. 'begin',
  14957. ' Result:=fitems[index];',
  14958. 'end;',
  14959. 'procedure tobject.setitems(index: longint; value: longint);',
  14960. 'begin',
  14961. ' fitems[index]:=value;',
  14962. 'end;',
  14963. 'procedure tobject.doit;',
  14964. 'begin',
  14965. ' items[1]:=2;',
  14966. ' items[3]:=items[4];',
  14967. ' self.items[5]:=self.items[6];',
  14968. ' items[items[7]]:=items[items[8]];',
  14969. 'end;',
  14970. 'var Obj: tobject;',
  14971. 'begin',
  14972. ' obj.Items[11]:=obj.Items[12];',
  14973. '']);
  14974. ConvertProgram;
  14975. CheckSource('TestClass_Property_Indexed',
  14976. LinesToStr([ // statements
  14977. 'rtl.createClass(this, "TObject", null, function () {',
  14978. ' this.$init = function () {',
  14979. ' this.FItems = [];',
  14980. ' };',
  14981. ' this.$final = function () {',
  14982. ' this.FItems = undefined;',
  14983. ' };',
  14984. ' this.GetItems = function (Index) {',
  14985. ' var Result = 0;',
  14986. ' Result = this.FItems[Index];',
  14987. ' return Result;',
  14988. ' };',
  14989. ' this.SetItems = function (Index, Value) {',
  14990. ' this.FItems[Index] = Value;',
  14991. ' };',
  14992. ' this.DoIt = function () {',
  14993. ' this.SetItems(1, 2);',
  14994. ' this.SetItems(3,this.GetItems(4));',
  14995. ' this.SetItems(5,this.GetItems(6));',
  14996. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14997. ' };',
  14998. '});',
  14999. 'this.Obj = null;'
  15000. ]),
  15001. LinesToStr([ // $mod.$main
  15002. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  15003. ]));
  15004. end;
  15005. procedure TTestModule.TestClass_Property_IndexSpec;
  15006. begin
  15007. StartProgram(false);
  15008. Add([
  15009. 'type',
  15010. ' TEnum = (red, blue);',
  15011. ' TObject = class',
  15012. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  15013. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  15014. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  15015. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  15016. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  15017. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  15018. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  15019. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  15020. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  15021. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  15022. ' end;',
  15023. 'procedure DoIt(b: boolean); begin end;',
  15024. 'var',
  15025. ' o: TObject;',
  15026. 'begin',
  15027. ' o.B1:=o.B1;',
  15028. ' o.B2:=o.B2;',
  15029. ' o.B3:=o.B3;',
  15030. ' o.I1[''a'']:=o.I1[''b''];',
  15031. ' doit(o.b1);',
  15032. ' doit(o.b2);',
  15033. ' doit(o.i1[''c'']);',
  15034. '']);
  15035. ConvertProgram;
  15036. CheckSource('TestClass_Property_IndexSpec',
  15037. LinesToStr([ // statements
  15038. 'this.TEnum = {',
  15039. ' "0": "red",',
  15040. ' red: 0,',
  15041. ' "1": "blue",',
  15042. ' blue: 1',
  15043. '};',
  15044. 'rtl.createClass(this, "TObject", null, function () {',
  15045. ' this.$init = function () {',
  15046. ' };',
  15047. ' this.$final = function () {',
  15048. ' };',
  15049. '});',
  15050. 'this.DoIt = function (b) {',
  15051. '};',
  15052. 'this.o = null;',
  15053. '']),
  15054. LinesToStr([ // $mod.$main
  15055. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15056. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15057. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15058. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15059. '$mod.DoIt($mod.o.GetIntBool(1));',
  15060. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15061. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15062. '']));
  15063. end;
  15064. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15065. begin
  15066. StartProgram(false);
  15067. Add('type');
  15068. Add(' TArray = array of longint;');
  15069. Add(' TObject = class');
  15070. Add(' FItems: TArray;');
  15071. Add(' function GetItems: tarray;');
  15072. Add(' procedure SetItems(Value: tarray);');
  15073. Add(' property Items: tarray read getitems write setitems;');
  15074. Add(' procedure SetNumbers(const Value: tarray);');
  15075. Add(' property Numbers: tarray write setnumbers;');
  15076. Add(' end;');
  15077. Add('function tobject.getitems: tarray;');
  15078. Add('begin');
  15079. Add(' Result:=fitems;');
  15080. Add('end;');
  15081. Add('procedure tobject.setitems(value: tarray);');
  15082. Add('begin');
  15083. Add(' fitems:=value;');
  15084. Add(' fitems:=nil;');
  15085. Add(' Items:=nil;');
  15086. Add(' Items:=Items;');
  15087. Add(' Items[1]:=2;');
  15088. Add(' fitems[3]:=Items[4];');
  15089. Add(' Items[5]:=Items[6];');
  15090. Add(' Self.Items[7]:=8;');
  15091. Add(' Self.Items[9]:=Self.Items[10];');
  15092. Add(' Items[Items[11]]:=Items[Items[12]];');
  15093. Add('end;');
  15094. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15095. Add('begin;');
  15096. Add(' Numbers:=nil;');
  15097. Add(' Numbers:=Value;');
  15098. Add(' Self.Numbers:=Value;');
  15099. Add('end;');
  15100. Add('var Obj: tobject;');
  15101. Add('begin');
  15102. Add(' obj.items:=nil;');
  15103. Add(' obj.items:=obj.items;');
  15104. Add(' obj.items[11]:=obj.items[12];');
  15105. ConvertProgram;
  15106. CheckSource('TestClass_PropertyOfTypeArray',
  15107. LinesToStr([ // statements
  15108. 'rtl.createClass(this, "TObject", null, function () {',
  15109. ' this.$init = function () {',
  15110. ' this.FItems = [];',
  15111. ' };',
  15112. ' this.$final = function () {',
  15113. ' this.FItems = undefined;',
  15114. ' };',
  15115. ' this.GetItems = function () {',
  15116. ' var Result = [];',
  15117. ' Result = rtl.arrayRef(this.FItems);',
  15118. ' return Result;',
  15119. ' };',
  15120. ' this.SetItems = function (Value) {',
  15121. ' this.FItems = rtl.arrayRef(Value);',
  15122. ' this.FItems = [];',
  15123. ' this.SetItems([]);',
  15124. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15125. ' this.GetItems()[1] = 2;',
  15126. ' this.FItems[3] = this.GetItems()[4];',
  15127. ' this.GetItems()[5] = this.GetItems()[6];',
  15128. ' this.GetItems()[7] = 8;',
  15129. ' this.GetItems()[9] = this.GetItems()[10];',
  15130. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15131. ' };',
  15132. ' this.SetNumbers = function (Value) {',
  15133. ' this.SetNumbers([]);',
  15134. ' this.SetNumbers(Value);',
  15135. ' this.SetNumbers(Value);',
  15136. ' };',
  15137. '});',
  15138. 'this.Obj = null;'
  15139. ]),
  15140. LinesToStr([ // $mod.$main
  15141. '$mod.Obj.SetItems([]);',
  15142. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15143. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15144. ]));
  15145. end;
  15146. procedure TTestModule.TestClass_PropertyDefault;
  15147. begin
  15148. StartProgram(false);
  15149. Add([
  15150. 'type',
  15151. ' TArray = array of longint;',
  15152. ' TObject = class',
  15153. ' end;',
  15154. ' TBird = class',
  15155. ' FItems: TArray;',
  15156. ' function GetItems(Index: longint): longint;',
  15157. ' procedure SetItems(Index, Value: longint);',
  15158. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15159. ' end;',
  15160. 'function TBird.getitems(index: longint): longint;',
  15161. 'begin',
  15162. 'end;',
  15163. 'procedure TBird.setitems(index, value: longint);',
  15164. 'begin',
  15165. ' Self[1]:=2;',
  15166. ' Self[3]:=Self[index];',
  15167. ' Self[index]:=Self[Self[value]];',
  15168. ' Self[Self[4]]:=value;',
  15169. 'end;',
  15170. 'var',
  15171. ' Bird: TBird;',
  15172. ' Obj: TObject;',
  15173. 'begin',
  15174. ' bird[11]:=12;',
  15175. ' bird[13]:=bird[14];',
  15176. ' bird[Bird[15]]:=bird[Bird[15]];',
  15177. ' TBird(obj)[16]:=TBird(obj)[17];',
  15178. ' (obj as tbird)[18]:=19;',
  15179. '']);
  15180. ConvertProgram;
  15181. CheckSource('TestClass_PropertyDefault',
  15182. LinesToStr([ // statements
  15183. 'rtl.createClass(this, "TObject", null, function () {',
  15184. ' this.$init = function () {',
  15185. ' };',
  15186. ' this.$final = function () {',
  15187. ' };',
  15188. '});',
  15189. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15190. ' this.$init = function () {',
  15191. ' $mod.TObject.$init.call(this);',
  15192. ' this.FItems = [];',
  15193. ' };',
  15194. ' this.$final = function () {',
  15195. ' this.FItems = undefined;',
  15196. ' $mod.TObject.$final.call(this);',
  15197. ' };',
  15198. ' this.GetItems = function (Index) {',
  15199. ' var Result = 0;',
  15200. ' return Result;',
  15201. ' };',
  15202. ' this.SetItems = function (Index, Value) {',
  15203. ' this.SetItems(1, 2);',
  15204. ' this.SetItems(3, this.GetItems(Index));',
  15205. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15206. ' this.SetItems(this.GetItems(4), Value);',
  15207. ' };',
  15208. '});',
  15209. 'this.Bird = null;',
  15210. 'this.Obj = null;',
  15211. '']),
  15212. LinesToStr([ // $mod.$main
  15213. '$mod.Bird.SetItems(11, 12);',
  15214. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15215. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15216. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15217. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15218. '']));
  15219. end;
  15220. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15221. begin
  15222. StartProgram(false);
  15223. Add([
  15224. 'type',
  15225. ' TObject = class end;',
  15226. ' TAlphaList = class',
  15227. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15228. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15229. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15230. ' end;',
  15231. ' TBetaList = class',
  15232. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15233. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15234. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15235. ' end;',
  15236. ' TBird = class',
  15237. ' procedure DoIt;',
  15238. ' end;',
  15239. 'procedure TBird.DoIt;',
  15240. 'var',
  15241. ' List: TAlphaList;',
  15242. 'begin',
  15243. ' if TBetaList(List[true])[3]=nil then ;',
  15244. ' TBetaList(List[false])[5]:=nil;',
  15245. 'end;',
  15246. 'var',
  15247. ' List: TAlphaList;',
  15248. 'begin',
  15249. ' if TBetaList(List[true])[3]=nil then ;',
  15250. ' TBetaList(List[false])[5]:=nil;',
  15251. '']);
  15252. ConvertProgram;
  15253. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15254. LinesToStr([ // statements
  15255. 'rtl.createClass(this, "TObject", null, function () {',
  15256. ' this.$init = function () {',
  15257. ' };',
  15258. ' this.$final = function () {',
  15259. ' };',
  15260. '});',
  15261. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15262. '});',
  15263. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15264. '});',
  15265. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15266. ' this.DoIt = function () {',
  15267. ' var List = null;',
  15268. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15269. ' List.GetAlphas(false).SetBetas(5, null);',
  15270. ' };',
  15271. '});',
  15272. 'this.List = null;',
  15273. '']),
  15274. LinesToStr([ // $mod.$main
  15275. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15276. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15277. '']));
  15278. end;
  15279. procedure TTestModule.TestClass_PropertyOverride;
  15280. begin
  15281. StartProgram(false);
  15282. Add('type');
  15283. Add(' integer = longint;');
  15284. Add(' TObject = class');
  15285. Add(' FItem: integer;');
  15286. Add(' function GetItem: integer; external name ''GetItem'';');
  15287. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15288. Add(' property Item: integer read getitem write setitem;');
  15289. Add(' end;');
  15290. Add(' TCar = class');
  15291. Add(' FBag: integer;');
  15292. Add(' function GetBag: integer; external name ''GetBag'';');
  15293. Add(' property Item read getbag;');
  15294. Add(' end;');
  15295. Add('var');
  15296. Add(' Obj: tobject;');
  15297. Add(' Car: tcar;');
  15298. Add('begin');
  15299. Add(' Obj.Item:=Obj.Item;');
  15300. Add(' Car.Item:=Car.Item;');
  15301. ConvertProgram;
  15302. CheckSource('TestClass_PropertyOverride',
  15303. LinesToStr([ // statements
  15304. 'rtl.createClass(this, "TObject", null, function () {',
  15305. ' this.$init = function () {',
  15306. ' this.FItem = 0;',
  15307. ' };',
  15308. ' this.$final = function () {',
  15309. ' };',
  15310. '});',
  15311. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15312. ' this.$init = function () {',
  15313. ' $mod.TObject.$init.call(this);',
  15314. ' this.FBag = 0;',
  15315. ' };',
  15316. '});',
  15317. 'this.Obj = null;',
  15318. 'this.Car = null;',
  15319. '']),
  15320. LinesToStr([ // $mod.$main
  15321. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15322. '$mod.Car.SetItem($mod.Car.GetBag());',
  15323. '']));
  15324. end;
  15325. procedure TTestModule.TestClass_PropertyIncVisibility;
  15326. begin
  15327. AddModuleWithIntfImplSrc('unit1.pp',
  15328. LinesToStr([
  15329. 'type',
  15330. ' TNumber = longint;',
  15331. ' TInteger = longint;',
  15332. ' TObject = class',
  15333. ' private',
  15334. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15335. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15336. ' protected',
  15337. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15338. ' end;']),
  15339. LinesToStr([
  15340. '']));
  15341. StartProgram(true);
  15342. Add([
  15343. 'uses unit1;',
  15344. 'type',
  15345. ' TBird = class',
  15346. ' public',
  15347. ' property Items;',
  15348. ' end;',
  15349. 'procedure DoIt(i: TInteger);',
  15350. 'begin',
  15351. 'end;',
  15352. 'var b: TBird;',
  15353. 'begin',
  15354. ' b.Items[1]:=2;',
  15355. ' b.Items[3]:=b.Items[4];',
  15356. ' DoIt(b.Items[5]);',
  15357. '']);
  15358. ConvertProgram;
  15359. CheckSource('TestClass_PropertyIncVisibility',
  15360. LinesToStr([ // statements
  15361. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15362. '});',
  15363. 'this.DoIt = function (i) {',
  15364. '};',
  15365. 'this.b = null;'
  15366. ]),
  15367. LinesToStr([ // $mod.$main
  15368. '$mod.b.SetItems(1, 2);',
  15369. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15370. '$mod.DoIt($mod.b.GetItems(5));'
  15371. ]));
  15372. end;
  15373. procedure TTestModule.TestClass_Assigned;
  15374. begin
  15375. StartProgram(false);
  15376. Add('type');
  15377. Add(' TObject = class');
  15378. Add(' end;');
  15379. Add('var');
  15380. Add(' Obj: tobject;');
  15381. Add(' b: boolean;');
  15382. Add('begin');
  15383. Add(' if Assigned(obj) then ;');
  15384. Add(' b:=Assigned(obj) or false;');
  15385. ConvertProgram;
  15386. CheckSource('TestClass_Assigned',
  15387. LinesToStr([ // statements
  15388. 'rtl.createClass(this, "TObject", null, function () {',
  15389. ' this.$init = function () {',
  15390. ' };',
  15391. ' this.$final = function () {',
  15392. ' };',
  15393. '});',
  15394. 'this.Obj = null;',
  15395. 'this.b = false;'
  15396. ]),
  15397. LinesToStr([ // $mod.$main
  15398. 'if ($mod.Obj != null);',
  15399. '$mod.b = ($mod.Obj != null) || false;'
  15400. ]));
  15401. end;
  15402. procedure TTestModule.TestClass_WithClassDoCreate;
  15403. begin
  15404. StartProgram(false);
  15405. Add('type');
  15406. Add(' TObject = class');
  15407. Add(' aBool: boolean;');
  15408. Add(' Arr: array of boolean;');
  15409. Add(' constructor Create;');
  15410. Add(' end;');
  15411. Add('constructor TObject.Create; begin end;');
  15412. Add('var');
  15413. Add(' Obj: tobject;');
  15414. Add(' b: boolean;');
  15415. Add('begin');
  15416. Add(' with tobject.create do begin');
  15417. Add(' b:=abool;');
  15418. Add(' abool:=b;');
  15419. Add(' b:=arr[1];');
  15420. Add(' arr[2]:=b;');
  15421. Add(' end;');
  15422. Add(' with tobject do');
  15423. Add(' obj:=create;');
  15424. Add(' with obj do begin');
  15425. Add(' create;');
  15426. Add(' b:=abool;');
  15427. Add(' abool:=b;');
  15428. Add(' b:=arr[3];');
  15429. Add(' arr[4]:=b;');
  15430. Add(' end;');
  15431. ConvertProgram;
  15432. CheckSource('TestClass_WithClassDoCreate',
  15433. LinesToStr([ // statements
  15434. 'rtl.createClass(this, "TObject", null, function () {',
  15435. ' this.$init = function () {',
  15436. ' this.aBool = false;',
  15437. ' this.Arr = [];',
  15438. ' };',
  15439. ' this.$final = function () {',
  15440. ' this.Arr = undefined;',
  15441. ' };',
  15442. ' this.Create = function () {',
  15443. ' return this;',
  15444. ' };',
  15445. '});',
  15446. 'this.Obj = null;',
  15447. 'this.b = false;'
  15448. ]),
  15449. LinesToStr([ // $mod.$main
  15450. 'var $with = $mod.TObject.$create("Create");',
  15451. '$mod.b = $with.aBool;',
  15452. '$with.aBool = $mod.b;',
  15453. '$mod.b = $with.Arr[1];',
  15454. '$with.Arr[2] = $mod.b;',
  15455. 'var $with1 = $mod.TObject;',
  15456. '$mod.Obj = $with1.$create("Create");',
  15457. 'var $with2 = $mod.Obj;',
  15458. '$with2.Create();',
  15459. '$mod.b = $with2.aBool;',
  15460. '$with2.aBool = $mod.b;',
  15461. '$mod.b = $with2.Arr[3];',
  15462. '$with2.Arr[4] = $mod.b;',
  15463. '']));
  15464. end;
  15465. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15466. begin
  15467. StartProgram(false);
  15468. Add('type');
  15469. Add(' TObject = class');
  15470. Add(' FInt: longint;');
  15471. Add(' constructor Create;');
  15472. Add(' function GetSize: longint;');
  15473. Add(' procedure SetSize(Value: longint);');
  15474. Add(' property Int: longint read FInt write FInt;');
  15475. Add(' property Size: longint read GetSize write SetSize;');
  15476. Add(' end;');
  15477. Add('constructor TObject.Create; begin end;');
  15478. Add('function TObject.GetSize: longint; begin; end;');
  15479. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15480. Add('var');
  15481. Add(' Obj: tobject;');
  15482. Add(' i: longint;');
  15483. Add('begin');
  15484. Add(' with TObject.Create do begin');
  15485. Add(' i:=int;');
  15486. Add(' int:=i;');
  15487. Add(' i:=size;');
  15488. Add(' size:=i;');
  15489. Add(' end;');
  15490. Add(' with obj do begin');
  15491. Add(' i:=int;');
  15492. Add(' int:=i;');
  15493. Add(' i:=size;');
  15494. Add(' size:=i;');
  15495. Add(' end;');
  15496. ConvertProgram;
  15497. CheckSource('TestClass_WithClassInstDoProperty',
  15498. LinesToStr([ // statements
  15499. 'rtl.createClass(this, "TObject", null, function () {',
  15500. ' this.$init = function () {',
  15501. ' this.FInt = 0;',
  15502. ' };',
  15503. ' this.$final = function () {',
  15504. ' };',
  15505. ' this.Create = function () {',
  15506. ' return this;',
  15507. ' };',
  15508. ' this.GetSize = function () {',
  15509. ' var Result = 0;',
  15510. ' return Result;',
  15511. ' };',
  15512. ' this.SetSize = function (Value) {',
  15513. ' };',
  15514. '});',
  15515. 'this.Obj = null;',
  15516. 'this.i = 0;'
  15517. ]),
  15518. LinesToStr([ // $mod.$main
  15519. 'var $with = $mod.TObject.$create("Create");',
  15520. '$mod.i = $with.FInt;',
  15521. '$with.FInt = $mod.i;',
  15522. '$mod.i = $with.GetSize();',
  15523. '$with.SetSize($mod.i);',
  15524. 'var $with1 = $mod.Obj;',
  15525. '$mod.i = $with1.FInt;',
  15526. '$with1.FInt = $mod.i;',
  15527. '$mod.i = $with1.GetSize();',
  15528. '$with1.SetSize($mod.i);',
  15529. '']));
  15530. end;
  15531. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15532. begin
  15533. StartProgram(false);
  15534. Add('type');
  15535. Add(' TObject = class');
  15536. Add(' constructor Create;');
  15537. Add(' function GetItems(Index: longint): longint;');
  15538. Add(' procedure SetItems(Index, Value: longint);');
  15539. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15540. Add(' end;');
  15541. Add('constructor TObject.Create; begin end;');
  15542. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15543. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15544. Add('var');
  15545. Add(' Obj: tobject;');
  15546. Add(' i: longint;');
  15547. Add('begin');
  15548. Add(' with TObject.Create do begin');
  15549. Add(' i:=Items[1];');
  15550. Add(' Items[2]:=i;');
  15551. Add(' end;');
  15552. Add(' with obj do begin');
  15553. Add(' i:=Items[3];');
  15554. Add(' Items[4]:=i;');
  15555. Add(' end;');
  15556. ConvertProgram;
  15557. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15558. LinesToStr([ // statements
  15559. 'rtl.createClass(this, "TObject", null, function () {',
  15560. ' this.$init = function () {',
  15561. ' };',
  15562. ' this.$final = function () {',
  15563. ' };',
  15564. ' this.Create = function () {',
  15565. ' return this;',
  15566. ' };',
  15567. ' this.GetItems = function (Index) {',
  15568. ' var Result = 0;',
  15569. ' return Result;',
  15570. ' };',
  15571. ' this.SetItems = function (Index, Value) {',
  15572. ' };',
  15573. '});',
  15574. 'this.Obj = null;',
  15575. 'this.i = 0;'
  15576. ]),
  15577. LinesToStr([ // $mod.$main
  15578. 'var $with = $mod.TObject.$create("Create");',
  15579. '$mod.i = $with.GetItems(1);',
  15580. '$with.SetItems(2, $mod.i);',
  15581. 'var $with1 = $mod.Obj;',
  15582. '$mod.i = $with1.GetItems(3);',
  15583. '$with1.SetItems(4, $mod.i);',
  15584. '']));
  15585. end;
  15586. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15587. begin
  15588. StartProgram(false);
  15589. Add('type');
  15590. Add(' TObject = class');
  15591. Add(' constructor Create;');
  15592. Add(' function GetSize: longint;');
  15593. Add(' procedure SetSize(Value: longint);');
  15594. Add(' end;');
  15595. Add('constructor TObject.Create; begin end;');
  15596. Add('function TObject.GetSize: longint; begin; end;');
  15597. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15598. Add('var');
  15599. Add(' Obj: tobject;');
  15600. Add(' i: longint;');
  15601. Add('begin');
  15602. Add(' with TObject.Create do begin');
  15603. Add(' i:=GetSize;');
  15604. Add(' i:=GetSize();');
  15605. Add(' SetSize(i);');
  15606. Add(' end;');
  15607. Add(' with obj do begin');
  15608. Add(' i:=GetSize;');
  15609. Add(' i:=GetSize();');
  15610. Add(' SetSize(i);');
  15611. Add(' end;');
  15612. ConvertProgram;
  15613. CheckSource('TestClass_WithClassInstDoFunc',
  15614. LinesToStr([ // statements
  15615. 'rtl.createClass(this, "TObject", null, function () {',
  15616. ' this.$init = function () {',
  15617. ' };',
  15618. ' this.$final = function () {',
  15619. ' };',
  15620. ' this.Create = function () {',
  15621. ' return this;',
  15622. ' };',
  15623. ' this.GetSize = function () {',
  15624. ' var Result = 0;',
  15625. ' return Result;',
  15626. ' };',
  15627. ' this.SetSize = function (Value) {',
  15628. ' };',
  15629. '});',
  15630. 'this.Obj = null;',
  15631. 'this.i = 0;'
  15632. ]),
  15633. LinesToStr([ // $mod.$main
  15634. 'var $with = $mod.TObject.$create("Create");',
  15635. '$mod.i = $with.GetSize();',
  15636. '$mod.i = $with.GetSize();',
  15637. '$with.SetSize($mod.i);',
  15638. 'var $with1 = $mod.Obj;',
  15639. '$mod.i = $with1.GetSize();',
  15640. '$mod.i = $with1.GetSize();',
  15641. '$with1.SetSize($mod.i);',
  15642. '']));
  15643. end;
  15644. procedure TTestModule.TestClass_TypeCast;
  15645. begin
  15646. StartProgram(false);
  15647. Add('type');
  15648. Add(' TObject = class');
  15649. Add(' Next: TObject;');
  15650. Add(' constructor Create;');
  15651. Add(' end;');
  15652. Add(' TControl = class(TObject)');
  15653. Add(' Arr: array of TObject;');
  15654. Add(' function GetIt(vI: longint = 0): TObject;');
  15655. Add(' end;');
  15656. Add('constructor tobject.create; begin end;');
  15657. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15658. Add('var');
  15659. Add(' Obj: tobject;');
  15660. Add('begin');
  15661. Add(' obj:=tcontrol(obj).next;');
  15662. Add(' tcontrol(obj):=nil;');
  15663. Add(' obj:=tcontrol(obj);');
  15664. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15665. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15666. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15667. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15668. Add(' obj:=tcontrol(nil);');
  15669. ConvertProgram;
  15670. CheckSource('TestClass_TypeCast',
  15671. LinesToStr([ // statements
  15672. 'rtl.createClass(this, "TObject", null, function () {',
  15673. ' this.$init = function () {',
  15674. ' this.Next = null;',
  15675. ' };',
  15676. ' this.$final = function () {',
  15677. ' this.Next = undefined;',
  15678. ' };',
  15679. ' this.Create = function () {',
  15680. ' return this;',
  15681. ' };',
  15682. '});',
  15683. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15684. ' this.$init = function () {',
  15685. ' $mod.TObject.$init.call(this);',
  15686. ' this.Arr = [];',
  15687. ' };',
  15688. ' this.$final = function () {',
  15689. ' this.Arr = undefined;',
  15690. ' $mod.TObject.$final.call(this);',
  15691. ' };',
  15692. ' this.GetIt = function (vI) {',
  15693. ' var Result = null;',
  15694. ' return Result;',
  15695. ' };',
  15696. '});',
  15697. 'this.Obj = null;'
  15698. ]),
  15699. LinesToStr([ // $mod.$main
  15700. '$mod.Obj = $mod.Obj.Next;',
  15701. '$mod.Obj = null;',
  15702. '$mod.Obj = $mod.Obj;',
  15703. '$mod.Obj = $mod.Obj.GetIt(0);',
  15704. '$mod.Obj = $mod.Obj.GetIt(0);',
  15705. '$mod.Obj = $mod.Obj.GetIt(1);',
  15706. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15707. '$mod.Obj = null;',
  15708. '']));
  15709. end;
  15710. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15711. begin
  15712. StartProgram(false);
  15713. Add('type');
  15714. Add(' TObject = class end;');
  15715. Add('procedure ProcA(var A);');
  15716. Add('begin');
  15717. Add(' TObject(A):=nil;');
  15718. Add(' TObject(A):=TObject(A);');
  15719. Add(' if TObject(A)=nil then ;');
  15720. Add(' if nil=TObject(A) then ;');
  15721. Add('end;');
  15722. Add('procedure ProcB(out A);');
  15723. Add('begin');
  15724. Add(' TObject(A):=nil;');
  15725. Add(' TObject(A):=TObject(A);');
  15726. Add(' if TObject(A)=nil then ;');
  15727. Add(' if nil=TObject(A) then ;');
  15728. Add('end;');
  15729. Add('procedure ProcC(const A);');
  15730. Add('begin');
  15731. Add(' if TObject(A)=nil then ;');
  15732. Add(' if nil=TObject(A) then ;');
  15733. Add('end;');
  15734. Add('var o: TObject;');
  15735. Add('begin');
  15736. Add(' ProcA(o);');
  15737. Add(' ProcB(o);');
  15738. Add(' ProcC(o);');
  15739. ConvertProgram;
  15740. CheckSource('TestClass_TypeCastUntypedParam',
  15741. LinesToStr([ // statements
  15742. 'rtl.createClass(this, "TObject", null, function () {',
  15743. ' this.$init = function () {',
  15744. ' };',
  15745. ' this.$final = function () {',
  15746. ' };',
  15747. '});',
  15748. 'this.ProcA = function (A) {',
  15749. ' A.set(null);',
  15750. ' A.set(A.get());',
  15751. ' if (A.get() === null);',
  15752. ' if (null === A.get());',
  15753. '};',
  15754. 'this.ProcB = function (A) {',
  15755. ' A.set(null);',
  15756. ' A.set(A.get());',
  15757. ' if (A.get() === null);',
  15758. ' if (null === A.get());',
  15759. '};',
  15760. 'this.ProcC = function (A) {',
  15761. ' if (A === null);',
  15762. ' if (null === A);',
  15763. '};',
  15764. 'this.o = null;',
  15765. '']),
  15766. LinesToStr([ // $mod.$main
  15767. '$mod.ProcA({',
  15768. ' p: $mod,',
  15769. ' get: function () {',
  15770. ' return this.p.o;',
  15771. ' },',
  15772. ' set: function (v) {',
  15773. ' this.p.o = v;',
  15774. ' }',
  15775. '});',
  15776. '$mod.ProcB({',
  15777. ' p: $mod,',
  15778. ' get: function () {',
  15779. ' return this.p.o;',
  15780. ' },',
  15781. ' set: function (v) {',
  15782. ' this.p.o = v;',
  15783. ' }',
  15784. '});',
  15785. '$mod.ProcC($mod.o);',
  15786. '']));
  15787. end;
  15788. procedure TTestModule.TestClass_Overloads;
  15789. begin
  15790. StartProgram(false);
  15791. Add('type');
  15792. Add(' TObject = class');
  15793. Add(' procedure DoIt;');
  15794. Add(' procedure DoIt(vI: longint);');
  15795. Add(' end;');
  15796. Add('procedure TObject.DoIt;');
  15797. Add('begin');
  15798. Add(' DoIt;');
  15799. Add(' DoIt(1);');
  15800. Add('end;');
  15801. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15802. Add('begin');
  15803. ConvertProgram;
  15804. CheckSource('TestClass_Overloads',
  15805. LinesToStr([ // statements
  15806. 'rtl.createClass(this, "TObject", null, function () {',
  15807. ' this.$init = function () {',
  15808. ' };',
  15809. ' this.$final = function () {',
  15810. ' };',
  15811. ' this.DoIt = function () {',
  15812. ' this.DoIt();',
  15813. ' this.DoIt$1(1);',
  15814. ' };',
  15815. ' this.DoIt$1 = function (vI) {',
  15816. ' };',
  15817. '});',
  15818. '']),
  15819. LinesToStr([ // $mod.$main
  15820. '']));
  15821. end;
  15822. procedure TTestModule.TestClass_OverloadsAncestor;
  15823. begin
  15824. StartProgram(false);
  15825. Add('type');
  15826. Add(' TObject = class;');
  15827. Add(' TObject = class');
  15828. Add(' procedure DoIt(vA: longint);');
  15829. Add(' procedure DoIt(vA, vB: longint);');
  15830. Add(' end;');
  15831. Add(' TCar = class;');
  15832. Add(' TCar = class');
  15833. Add(' procedure DoIt(vA: longint);');
  15834. Add(' procedure DoIt(vA, vB: longint);');
  15835. Add(' end;');
  15836. Add('procedure tobject.doit(va: longint);');
  15837. Add('begin');
  15838. Add(' doit(1);');
  15839. Add(' doit(1,2);');
  15840. Add('end;');
  15841. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15842. Add('procedure tcar.doit(va: longint);');
  15843. Add('begin');
  15844. Add(' doit(1);');
  15845. Add(' doit(1,2);');
  15846. Add(' inherited doit(1);');
  15847. Add(' inherited doit(1,2);');
  15848. Add('end;');
  15849. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15850. Add('begin');
  15851. ConvertProgram;
  15852. CheckSource('TestClass_OverloadsAncestor',
  15853. LinesToStr([ // statements
  15854. 'rtl.createClass(this, "TObject", null, function () {',
  15855. ' this.$init = function () {',
  15856. ' };',
  15857. ' this.$final = function () {',
  15858. ' };',
  15859. ' this.DoIt = function (vA) {',
  15860. ' this.DoIt(1);',
  15861. ' this.DoIt$1(1,2);',
  15862. ' };',
  15863. ' this.DoIt$1 = function (vA, vB) {',
  15864. ' };',
  15865. '});',
  15866. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15867. ' this.DoIt$2 = function (vA) {',
  15868. ' this.DoIt$2(1);',
  15869. ' this.DoIt$3(1, 2);',
  15870. ' $mod.TObject.DoIt.call(this, 1);',
  15871. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15872. ' };',
  15873. ' this.DoIt$3 = function (vA, vB) {',
  15874. ' };',
  15875. '});',
  15876. '']),
  15877. LinesToStr([ // $mod.$main
  15878. '']));
  15879. end;
  15880. procedure TTestModule.TestClass_OverloadConstructor;
  15881. begin
  15882. StartProgram(false);
  15883. Add('type');
  15884. Add(' TObject = class');
  15885. Add(' constructor Create(vA: longint);');
  15886. Add(' constructor Create(vA, vB: longint);');
  15887. Add(' end;');
  15888. Add(' TCar = class');
  15889. Add(' constructor Create(vA: longint);');
  15890. Add(' constructor Create(vA, vB: longint);');
  15891. Add(' end;');
  15892. Add('constructor tobject.create(va: longint);');
  15893. Add('begin');
  15894. Add(' create(1);');
  15895. Add(' create(1,2);');
  15896. Add('end;');
  15897. Add('constructor tobject.create(va, vb: longint); begin end;');
  15898. Add('constructor tcar.create(va: longint);');
  15899. Add('begin');
  15900. Add(' create(1);');
  15901. Add(' create(1,2);');
  15902. Add(' inherited create(1);');
  15903. Add(' inherited create(1,2);');
  15904. Add('end;');
  15905. Add('constructor tcar.create(va, vb: longint); begin end;');
  15906. Add('begin');
  15907. Add(' tobject.create(1);');
  15908. Add(' tobject.create(1,2);');
  15909. Add(' tcar.create(1);');
  15910. Add(' tcar.create(1,2);');
  15911. ConvertProgram;
  15912. CheckSource('TestClass_OverloadConstructor',
  15913. LinesToStr([ // statements
  15914. 'rtl.createClass(this, "TObject", null, function () {',
  15915. ' this.$init = function () {',
  15916. ' };',
  15917. ' this.$final = function () {',
  15918. ' };',
  15919. ' this.Create = function (vA) {',
  15920. ' this.Create(1);',
  15921. ' this.Create$1(1,2);',
  15922. ' return this;',
  15923. ' };',
  15924. ' this.Create$1 = function (vA, vB) {',
  15925. ' return this;',
  15926. ' };',
  15927. '});',
  15928. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15929. ' this.Create$2 = function (vA) {',
  15930. ' this.Create$2(1);',
  15931. ' this.Create$3(1, 2);',
  15932. ' $mod.TObject.Create.call(this, 1);',
  15933. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15934. ' return this;',
  15935. ' };',
  15936. ' this.Create$3 = function (vA, vB) {',
  15937. ' return this;',
  15938. ' };',
  15939. '});',
  15940. '']),
  15941. LinesToStr([ // $mod.$main
  15942. '$mod.TObject.$create("Create", [1]);',
  15943. '$mod.TObject.$create("Create$1", [1, 2]);',
  15944. '$mod.TCar.$create("Create$2", [1]);',
  15945. '$mod.TCar.$create("Create$3", [1, 2]);',
  15946. '']));
  15947. end;
  15948. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15949. begin
  15950. StartProgram(false);
  15951. Add([
  15952. '{$mode delphi}',
  15953. 'type',
  15954. ' TObject = class end;',
  15955. ' TBird = class',
  15956. ' function {#a}GetValue: longint; overload; virtual;',
  15957. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15958. ' end;',
  15959. ' TEagle = class(TBird)',
  15960. ' function {#c}GetValue: longint; overload; override;',
  15961. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15962. ' end;',
  15963. 'function TBird.GetValue: longint;',
  15964. 'begin',
  15965. ' if 3={@a}GetValue then ;',
  15966. ' if 4={@b}GetValue(5) then ;',
  15967. 'end;',
  15968. 'function TBird.GetValue(AValue: longint): longint;',
  15969. 'begin',
  15970. 'end;',
  15971. 'function TEagle.GetValue: longint;',
  15972. 'begin',
  15973. ' if 13={@c}GetValue then ;',
  15974. ' if 14={@d}GetValue(15) then ;',
  15975. ' if 15=inherited {@a}GetValue then ;',
  15976. ' if 16=inherited {@b}GetValue(17) then ;',
  15977. 'end;',
  15978. 'function TEagle.GetValue(AValue: longint): longint;',
  15979. 'begin',
  15980. 'end;',
  15981. 'var',
  15982. ' e: TEagle;',
  15983. 'begin',
  15984. ' if 23=e.{@c}GetValue then ;',
  15985. ' if 24=e.{@d}GetValue(25) then ;']);
  15986. ConvertProgram;
  15987. CheckSource('TestClass_OverloadDelphiOverride',
  15988. LinesToStr([ // statements
  15989. 'rtl.createClass(this, "TObject", null, function () {',
  15990. ' this.$init = function () {',
  15991. ' };',
  15992. ' this.$final = function () {',
  15993. ' };',
  15994. '});',
  15995. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15996. ' this.GetValue = function () {',
  15997. ' var Result = 0;',
  15998. ' if (3 === this.GetValue()) ;',
  15999. ' if (4 === this.GetValue$1(5)) ;',
  16000. ' return Result;',
  16001. ' };',
  16002. ' this.GetValue$1 = function (AValue) {',
  16003. ' var Result = 0;',
  16004. ' return Result;',
  16005. ' };',
  16006. '});',
  16007. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16008. ' this.GetValue = function () {',
  16009. ' var Result = 0;',
  16010. ' if (13 === this.GetValue()) ;',
  16011. ' if (14 === this.GetValue$1(15)) ;',
  16012. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  16013. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  16014. ' return Result;',
  16015. ' };',
  16016. ' this.GetValue$1 = function (AValue) {',
  16017. ' var Result = 0;',
  16018. ' return Result;',
  16019. ' };',
  16020. '});',
  16021. 'this.e = null;',
  16022. '']),
  16023. LinesToStr([ // $mod.$main
  16024. 'if (23 === $mod.e.GetValue()) ;',
  16025. 'if (24 === $mod.e.GetValue$1(25)) ;',
  16026. '']));
  16027. end;
  16028. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  16029. begin
  16030. StartProgram(false);
  16031. Add([
  16032. '{$mode delphi}',
  16033. 'type',
  16034. ' TObject = class end;',
  16035. ' TAnimal = class',
  16036. ' public',
  16037. ' {#animal_a}A: longint;',
  16038. ' function {#animal_b}B: longint;',
  16039. ' end;',
  16040. ' TBird = class(TAnimal)',
  16041. ' public',
  16042. ' {#bird_a}A: double;',
  16043. ' {#bird_b}B: boolean;',
  16044. ' end;',
  16045. ' TEagle = class(TBird)',
  16046. ' public',
  16047. ' function {#eagle_a}A: boolean;',
  16048. ' {#eagle_b}B: double;',
  16049. ' end;',
  16050. 'function TAnimal.B: longint;',
  16051. 'begin',
  16052. 'end;',
  16053. 'function TEagle.A: boolean;',
  16054. 'begin',
  16055. ' {@eagle_b}B:=3.3;',
  16056. ' {@eagle_a}A();',
  16057. ' TBird(Self).{@bird_b}B:=true;',
  16058. ' TAnimal(Self).{@animal_a}A:=17;',
  16059. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16060. 'end;',
  16061. 'var',
  16062. ' e: TEagle;',
  16063. 'begin',
  16064. ' e.{@eagle_b}B:=5.3;',
  16065. ' if e.{@eagle_a}A then ;',
  16066. '']);
  16067. ConvertProgram;
  16068. CheckSource('TestClass_ReintroduceVarDelphi',
  16069. LinesToStr([ // statements
  16070. 'rtl.createClass(this, "TObject", null, function () {',
  16071. ' this.$init = function () {',
  16072. ' };',
  16073. ' this.$final = function () {',
  16074. ' };',
  16075. '});',
  16076. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16077. ' this.$init = function () {',
  16078. ' $mod.TObject.$init.call(this);',
  16079. ' this.A = 0;',
  16080. ' };',
  16081. ' this.B = function () {',
  16082. ' var Result = 0;',
  16083. ' return Result;',
  16084. ' };',
  16085. '});',
  16086. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16087. ' this.$init = function () {',
  16088. ' $mod.TAnimal.$init.call(this);',
  16089. ' this.A$1 = 0.0;',
  16090. ' this.B$1 = false;',
  16091. ' };',
  16092. '});',
  16093. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16094. ' this.$init = function () {',
  16095. ' $mod.TBird.$init.call(this);',
  16096. ' this.B$2 = 0.0;',
  16097. ' };',
  16098. ' this.A$2 = function () {',
  16099. ' var Result = false;',
  16100. ' this.B$2 = 3.3;',
  16101. ' this.A$2();',
  16102. ' this.B$1 = true;',
  16103. ' this.A = 17;',
  16104. ' this.B$1 = this.A$1 > 1;',
  16105. ' return Result;',
  16106. ' };',
  16107. '});',
  16108. 'this.e = null;',
  16109. '']),
  16110. LinesToStr([ // $mod.$main
  16111. '$mod.e.B$2 = 5.3;',
  16112. 'if ($mod.e.A$2()) ;',
  16113. '']));
  16114. end;
  16115. procedure TTestModule.TestClass_ReintroducedVar;
  16116. begin
  16117. StartProgram(false);
  16118. Add('type');
  16119. Add(' TObject = class');
  16120. Add(' strict private');
  16121. Add(' Some: longint;');
  16122. Add(' end;');
  16123. Add(' TMobile = class');
  16124. Add(' strict private');
  16125. Add(' Some: string;');
  16126. Add(' end;');
  16127. Add(' TCar = class(tmobile)');
  16128. Add(' procedure Some;');
  16129. Add(' procedure Some(vA: longint);');
  16130. Add(' end;');
  16131. Add('procedure tcar.some;');
  16132. Add('begin');
  16133. Add(' Some;');
  16134. Add(' Some(1);');
  16135. Add('end;');
  16136. Add('procedure tcar.some(va: longint); begin end;');
  16137. Add('begin');
  16138. ConvertProgram;
  16139. CheckSource('TestClass_ReintroducedVar',
  16140. LinesToStr([ // statements
  16141. 'rtl.createClass(this, "TObject", null, function () {',
  16142. ' this.$init = function () {',
  16143. ' this.Some = 0;',
  16144. ' };',
  16145. ' this.$final = function () {',
  16146. ' };',
  16147. '});',
  16148. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16149. ' this.$init = function () {',
  16150. ' $mod.TObject.$init.call(this);',
  16151. ' this.Some$1 = "";',
  16152. ' };',
  16153. '});',
  16154. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16155. ' this.Some$2 = function () {',
  16156. ' this.Some$2();',
  16157. ' this.Some$3(1);',
  16158. ' };',
  16159. ' this.Some$3 = function (vA) {',
  16160. ' };',
  16161. '});',
  16162. '']),
  16163. LinesToStr([ // $mod.$main
  16164. '']));
  16165. end;
  16166. procedure TTestModule.TestClass_RaiseDescendant;
  16167. begin
  16168. StartProgram(false);
  16169. Add([
  16170. 'type',
  16171. ' TObject = class',
  16172. ' constructor Create(Msg: string);',
  16173. ' end;',
  16174. ' Exception = class',
  16175. ' end;',
  16176. ' EConvertError = class(Exception)',
  16177. ' end;',
  16178. 'constructor TObject.Create(Msg: string); begin end;',
  16179. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16180. 'begin',
  16181. ' raise Exception.Create(''Bar1'');',
  16182. ' raise EConvertError.Create(''Bar2'');',
  16183. ' raise AssertConv(''Bar2'');',
  16184. ' raise AssertConv;',
  16185. '']);
  16186. ConvertProgram;
  16187. CheckSource('TestClass_RaiseDescendant',
  16188. LinesToStr([ // statements
  16189. 'rtl.createClass(this, "TObject", null, function () {',
  16190. ' this.$init = function () {',
  16191. ' };',
  16192. ' this.$final = function () {',
  16193. ' };',
  16194. ' this.Create = function (Msg) {',
  16195. ' return this;',
  16196. ' };',
  16197. '});',
  16198. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16199. '});',
  16200. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16201. '});',
  16202. 'this.AssertConv = function (Msg) {',
  16203. ' var Result = null;',
  16204. ' return Result;',
  16205. '};',
  16206. '']),
  16207. LinesToStr([ // $mod.$main
  16208. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16209. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16210. 'throw $mod.AssertConv("Bar2");',
  16211. 'throw $mod.AssertConv("def");',
  16212. '']));
  16213. end;
  16214. procedure TTestModule.TestClass_ExternalMethod;
  16215. begin
  16216. AddModuleWithIntfImplSrc('unit2.pas',
  16217. LinesToStr([
  16218. 'type',
  16219. ' TObject = class',
  16220. ' public',
  16221. ' procedure Intern; external name ''$DoIntern'';',
  16222. ' end;',
  16223. '']),
  16224. LinesToStr([
  16225. '']));
  16226. StartUnit(true);
  16227. Add('interface');
  16228. Add('uses unit2;');
  16229. Add('type');
  16230. Add(' TCar = class(TObject)');
  16231. Add(' public');
  16232. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16233. Add(' procedure DoIt;');
  16234. Add(' end;');
  16235. Add('implementation');
  16236. Add('procedure tcar.doit;');
  16237. Add('begin');
  16238. Add(' Intern;');
  16239. Add(' Intern();');
  16240. Add(' Intern2;');
  16241. Add(' Intern2();');
  16242. Add('end;');
  16243. Add('var Obj: TCar;');
  16244. Add('begin');
  16245. Add(' obj.intern;');
  16246. Add(' obj.intern();');
  16247. Add(' obj.intern2;');
  16248. Add(' obj.intern2();');
  16249. Add(' obj.doit;');
  16250. Add(' obj.doit();');
  16251. Add(' with obj do begin');
  16252. Add(' Intern;');
  16253. Add(' Intern();');
  16254. Add(' Intern2;');
  16255. Add(' Intern2();');
  16256. Add(' end;');
  16257. ConvertUnit;
  16258. CheckSource('TestClass_ExternalMethod',
  16259. LinesToStr([
  16260. 'var $impl = $mod.$impl;',
  16261. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16262. ' this.DoIt = function () {',
  16263. ' this.$DoIntern();',
  16264. ' this.$DoIntern();',
  16265. ' this.$DoIntern2();',
  16266. ' this.$DoIntern2();',
  16267. ' };',
  16268. ' });',
  16269. '']),
  16270. LinesToStr([ // this.$init
  16271. '$impl.Obj.$DoIntern();',
  16272. '$impl.Obj.$DoIntern();',
  16273. '$impl.Obj.$DoIntern2();',
  16274. '$impl.Obj.$DoIntern2();',
  16275. '$impl.Obj.DoIt();',
  16276. '$impl.Obj.DoIt();',
  16277. 'var $with = $impl.Obj;',
  16278. '$with.$DoIntern();',
  16279. '$with.$DoIntern();',
  16280. '$with.$DoIntern2();',
  16281. '$with.$DoIntern2();',
  16282. '']),
  16283. LinesToStr([ // implementation
  16284. '$impl.Obj = null;',
  16285. '']) );
  16286. end;
  16287. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16288. begin
  16289. StartProgram(false);
  16290. Add('type');
  16291. Add(' TObject = class');
  16292. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16293. Add(' end;');
  16294. Add('begin');
  16295. SetExpectedPasResolverError('Virtual method name must match external',
  16296. nVirtualMethodNameMustMatchExternal);
  16297. ConvertProgram;
  16298. end;
  16299. procedure TTestModule.TestClass_ExternalOverrideFail;
  16300. begin
  16301. StartProgram(false);
  16302. Add('type');
  16303. Add(' TObject = class');
  16304. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16305. Add(' end;');
  16306. Add(' TCar = class');
  16307. Add(' procedure DoIt; override; external name ''DoIt'';');
  16308. Add(' end;');
  16309. Add('begin');
  16310. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16311. nInvalidXModifierY);
  16312. ConvertProgram;
  16313. end;
  16314. procedure TTestModule.TestClass_ExternalVar;
  16315. begin
  16316. AddModuleWithIntfImplSrc('unit2.pas',
  16317. LinesToStr([
  16318. '{$modeswitch externalclass}',
  16319. 'type',
  16320. ' TObject = class',
  16321. ' public',
  16322. ' Intern: longint external name ''$Intern'';',
  16323. ' Bracket: longint external name ''["A B"]'';',
  16324. ' end;',
  16325. '']),
  16326. LinesToStr([
  16327. '']));
  16328. StartUnit(true);
  16329. Add([
  16330. 'interface',
  16331. 'uses unit2;',
  16332. '{$modeswitch externalclass}',
  16333. 'type',
  16334. ' TCar = class(tobject)',
  16335. ' public',
  16336. ' Intern2: longint external name ''$Intern2'';',
  16337. ' procedure DoIt;',
  16338. ' end;',
  16339. 'implementation',
  16340. 'procedure tcar.doit;',
  16341. 'begin',
  16342. ' Intern:=Intern+1;',
  16343. ' Intern2:=Intern2+2;',
  16344. ' Bracket:=Bracket+3;',
  16345. 'end;',
  16346. 'var Obj: TCar;',
  16347. 'begin',
  16348. ' obj.intern:=obj.intern+1;',
  16349. ' obj.intern2:=obj.intern2+2;',
  16350. ' obj.Bracket:=obj.Bracket+3;',
  16351. ' with obj do begin',
  16352. ' intern:=intern+1;',
  16353. ' intern2:=intern2+2;',
  16354. ' Bracket:=Bracket+3;',
  16355. ' end;']);
  16356. ConvertUnit;
  16357. CheckSource('TestClass_ExternalVar',
  16358. LinesToStr([
  16359. 'var $impl = $mod.$impl;',
  16360. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16361. ' this.DoIt = function () {',
  16362. ' this.$Intern = this.$Intern + 1;',
  16363. ' this.$Intern2 = this.$Intern2 + 2;',
  16364. ' this["A B"] = this["A B"] + 3;',
  16365. ' };',
  16366. ' });',
  16367. '']),
  16368. LinesToStr([
  16369. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16370. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16371. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16372. 'var $with = $impl.Obj;',
  16373. '$with.$Intern = $with.$Intern + 1;',
  16374. '$with.$Intern2 = $with.$Intern2 + 2;',
  16375. '$with["A B"] = $with["A B"] + 3;',
  16376. '']),
  16377. LinesToStr([ // implementation
  16378. '$impl.Obj = null;',
  16379. '']));
  16380. end;
  16381. procedure TTestModule.TestClass_Const;
  16382. begin
  16383. StartProgram(false);
  16384. Add([
  16385. 'type',
  16386. ' integer = longint;',
  16387. ' TClass = class of TObject;',
  16388. ' TObject = class',
  16389. ' public',
  16390. ' const cI: integer = 3;',
  16391. ' procedure DoIt;',
  16392. ' class procedure DoMore;',
  16393. ' end;',
  16394. 'procedure tobject.doit;',
  16395. 'begin',
  16396. ' if cI=4 then;',
  16397. ' if 5=cI then;',
  16398. ' if Self.cI=6 then;',
  16399. ' if 7=Self.cI then;',
  16400. ' with Self do begin',
  16401. ' if cI=11 then;',
  16402. ' if 12=cI then;',
  16403. ' end;',
  16404. 'end;',
  16405. 'class procedure tobject.domore;',
  16406. 'begin',
  16407. ' if cI=8 then;',
  16408. ' if Self.cI=9 then;',
  16409. ' if 10=cI then;',
  16410. ' if 11=Self.cI then;',
  16411. ' with Self do begin',
  16412. ' if cI=13 then;',
  16413. ' if 14=cI then;',
  16414. ' end;',
  16415. 'end;',
  16416. 'var',
  16417. ' Obj: TObject;',
  16418. ' Cla: TClass;',
  16419. 'begin',
  16420. ' if TObject.cI=21 then ;',
  16421. ' if Obj.cI=22 then ;',
  16422. ' if Cla.cI=23 then ;',
  16423. ' with obj do if ci=24 then;',
  16424. ' with TObject do if ci=25 then;',
  16425. ' with Cla do if ci=26 then;']);
  16426. ConvertProgram;
  16427. CheckSource('TestClass_Const',
  16428. LinesToStr([
  16429. 'rtl.createClass(this, "TObject", null, function () {',
  16430. ' this.cI = 3;',
  16431. ' this.$init = function () {',
  16432. ' };',
  16433. ' this.$final = function () {',
  16434. ' };',
  16435. ' this.DoIt = function () {',
  16436. ' if (this.cI === 4) ;',
  16437. ' if (5 === this.cI) ;',
  16438. ' if (this.cI === 6) ;',
  16439. ' if (7 === this.cI) ;',
  16440. ' if (this.cI === 11) ;',
  16441. ' if (12 === this.cI) ;',
  16442. ' };',
  16443. ' this.DoMore = function () {',
  16444. ' if (this.cI === 8) ;',
  16445. ' if (this.cI === 9) ;',
  16446. ' if (10 === this.cI) ;',
  16447. ' if (11 === this.cI) ;',
  16448. ' if (this.cI === 13) ;',
  16449. ' if (14 === this.cI) ;',
  16450. ' };',
  16451. '});',
  16452. 'this.Obj = null;',
  16453. 'this.Cla = null;',
  16454. '']),
  16455. LinesToStr([
  16456. 'if ($mod.TObject.cI === 21) ;',
  16457. 'if ($mod.Obj.cI === 22) ;',
  16458. 'if ($mod.Cla.cI === 23) ;',
  16459. 'var $with = $mod.Obj;',
  16460. 'if ($with.cI === 24) ;',
  16461. 'var $with1 = $mod.TObject;',
  16462. 'if ($with1.cI === 25) ;',
  16463. 'var $with2 = $mod.Cla;',
  16464. 'if ($with2.cI === 26) ;',
  16465. '']));
  16466. end;
  16467. procedure TTestModule.TestClass_ConstEnum;
  16468. begin
  16469. StartProgram(false);
  16470. Add([
  16471. 'type',
  16472. ' TEnum = (red,blue);',
  16473. ' TObject = class',
  16474. ' end;',
  16475. ' TAnimal = class',
  16476. ' public',
  16477. ' type TSubEnum = (light,dark);',
  16478. ' const a = high(TEnum);',
  16479. ' const b = high(TSubEnum);',
  16480. ' end;',
  16481. ' TBird = class(TAnimal)',
  16482. ' public',
  16483. ' const c = high(TEnum);',
  16484. ' const d = high(TSubEnum);',
  16485. ' end;',
  16486. ' TAnt = class',
  16487. ' public',
  16488. ' const e = high(TEnum);',
  16489. ' const f = high(TBird.TSubEnum);',
  16490. ' end;',
  16491. 'begin',
  16492. '']);
  16493. ConvertProgram;
  16494. CheckSource('TestClass_ConstEnum',
  16495. LinesToStr([
  16496. 'this.TEnum = {',
  16497. ' "0": "red",',
  16498. ' red: 0,',
  16499. ' "1": "blue",',
  16500. ' blue: 1',
  16501. '};',
  16502. 'rtl.createClass(this, "TObject", null, function () {',
  16503. ' this.$init = function () {',
  16504. ' };',
  16505. ' this.$final = function () {',
  16506. ' };',
  16507. '});',
  16508. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16509. ' this.TSubEnum = {',
  16510. ' "0": "light",',
  16511. ' light: 0,',
  16512. ' "1": "dark",',
  16513. ' dark: 1',
  16514. ' };',
  16515. ' this.a = $mod.TEnum.blue;',
  16516. ' this.b = this.TSubEnum.dark;',
  16517. '});',
  16518. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16519. ' this.c = $mod.TEnum.blue;',
  16520. ' this.d = this.TSubEnum.dark;',
  16521. '});',
  16522. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16523. ' this.e = $mod.TEnum.blue;',
  16524. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16525. '});',
  16526. '']),
  16527. LinesToStr([
  16528. '']));
  16529. end;
  16530. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16531. begin
  16532. StartProgram(false);
  16533. Add([
  16534. 'type',
  16535. ' TObject = class',
  16536. ' const cI: longint = 3;',
  16537. ' procedure Fly;',
  16538. ' procedure Run;',
  16539. ' end;',
  16540. ' TBird = class',
  16541. ' procedure Go;',
  16542. ' end;',
  16543. 'procedure tobject.fly;',
  16544. 'const cI: word = 4;',
  16545. 'begin',
  16546. ' if cI=Self.cI then ;',
  16547. 'end;',
  16548. 'procedure tobject.run;',
  16549. 'const cI: word = 5;',
  16550. 'begin',
  16551. ' if cI=Self.cI then ;',
  16552. 'end;',
  16553. 'procedure tbird.go;',
  16554. 'const cI: word = 6;',
  16555. 'begin',
  16556. ' if cI=Self.cI then ;',
  16557. 'end;',
  16558. 'begin',
  16559. '']);
  16560. ConvertProgram;
  16561. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16562. LinesToStr([
  16563. 'rtl.createClass(this, "TObject", null, function () {',
  16564. ' this.cI = 3;',
  16565. ' this.$init = function () {',
  16566. ' };',
  16567. ' this.$final = function () {',
  16568. ' };',
  16569. ' var cI$1 = 4;',
  16570. ' this.Fly = function () {',
  16571. ' if (cI$1 === this.cI) ;',
  16572. ' };',
  16573. ' var cI$2 = 5;',
  16574. ' this.Run = function () {',
  16575. ' if (cI$2 === this.cI) ;',
  16576. ' };',
  16577. '});',
  16578. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16579. ' var cI$3 = 6;',
  16580. ' this.Go = function () {',
  16581. ' if (cI$3 === this.cI) ;',
  16582. ' };',
  16583. '});',
  16584. '']),
  16585. LinesToStr([
  16586. '']));
  16587. end;
  16588. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16589. begin
  16590. StartUnit(false);
  16591. Add([
  16592. 'interface',
  16593. 'type',
  16594. ' TObject = class',
  16595. ' const cI: longint = 3;',
  16596. ' procedure Fly;',
  16597. ' procedure Run;',
  16598. ' end;',
  16599. ' TBird = class',
  16600. ' procedure Go;',
  16601. ' end;',
  16602. 'implementation',
  16603. 'procedure tobject.fly;',
  16604. 'const cI: word = 4;',
  16605. 'begin',
  16606. ' if cI=Self.cI then ;',
  16607. 'end;',
  16608. 'procedure tobject.run;',
  16609. 'const cI: word = 5;',
  16610. 'begin',
  16611. ' if cI=Self.cI then ;',
  16612. 'end;',
  16613. 'procedure tbird.go;',
  16614. 'const cI: word = 6;',
  16615. 'begin',
  16616. ' if cI=Self.cI then ;',
  16617. 'end;',
  16618. '']);
  16619. ConvertUnit;
  16620. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16621. LinesToStr([
  16622. 'rtl.createClass(this, "TObject", null, function () {',
  16623. ' this.cI = 3;',
  16624. ' this.$init = function () {',
  16625. ' };',
  16626. ' this.$final = function () {',
  16627. ' };',
  16628. ' var cI$1 = 4;',
  16629. ' this.Fly = function () {',
  16630. ' if (cI$1 === this.cI) ;',
  16631. ' };',
  16632. ' var cI$2 = 5;',
  16633. ' this.Run = function () {',
  16634. ' if (cI$2 === this.cI) ;',
  16635. ' };',
  16636. '});',
  16637. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16638. ' var cI$3 = 6;',
  16639. ' this.Go = function () {',
  16640. ' if (cI$3 === this.cI) ;',
  16641. ' };',
  16642. '});',
  16643. '']),
  16644. '',
  16645. '');
  16646. end;
  16647. procedure TTestModule.TestClass_LocalVarSelfFail;
  16648. begin
  16649. StartProgram(false);
  16650. Add([
  16651. 'type',
  16652. ' TObject = class',
  16653. ' constructor Create;',
  16654. ' end;',
  16655. 'constructor tobject.create;',
  16656. 'var self: longint;',
  16657. 'begin',
  16658. 'end',
  16659. 'begin',
  16660. '']);
  16661. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16662. ConvertProgram;
  16663. end;
  16664. procedure TTestModule.TestClass_ArgSelfFail;
  16665. begin
  16666. StartProgram(false);
  16667. Add([
  16668. 'type',
  16669. ' TObject = class',
  16670. ' procedure DoIt(Self: longint);',
  16671. ' end;',
  16672. 'procedure tobject.doit(self: longint);',
  16673. 'begin',
  16674. 'end',
  16675. 'begin',
  16676. '']);
  16677. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16678. ConvertProgram;
  16679. end;
  16680. procedure TTestModule.TestClass_NestedProcSelf;
  16681. begin
  16682. StartProgram(false);
  16683. Add([
  16684. 'type',
  16685. ' TObject = class',
  16686. ' Key: longint;',
  16687. ' class var State: longint;',
  16688. ' procedure DoIt;',
  16689. ' function GetSize: longint; virtual; abstract;',
  16690. ' procedure SetSize(Value: longint); virtual; abstract;',
  16691. ' property Size: longint read GetSize write SetSize;',
  16692. ' end;',
  16693. 'procedure tobject.doit;',
  16694. ' procedure Sub;',
  16695. ' begin',
  16696. ' key:=key+2;',
  16697. ' self.key:=self.key+3;',
  16698. ' state:=state+4;',
  16699. ' self.state:=self.state+5;',
  16700. ' tobject.state:=tobject.state+6;',
  16701. ' size:=size+7;',
  16702. ' self.size:=self.size+8;',
  16703. ' end;',
  16704. 'begin',
  16705. ' sub;',
  16706. ' key:=key+12;',
  16707. ' self.key:=self.key+13;',
  16708. ' state:=state+14;',
  16709. ' self.state:=self.state+15;',
  16710. ' tobject.state:=tobject.state+16;',
  16711. ' size:=size+17;',
  16712. ' self.size:=self.size+18;',
  16713. 'end;',
  16714. 'begin',
  16715. '']);
  16716. ConvertProgram;
  16717. CheckSource('TestClass_NestedProcSelf',
  16718. LinesToStr([ // statements
  16719. 'rtl.createClass(this, "TObject", null, function () {',
  16720. ' this.State = 0;',
  16721. ' this.$init = function () {',
  16722. ' this.Key = 0;',
  16723. ' };',
  16724. ' this.$final = function () {',
  16725. ' };',
  16726. ' this.DoIt = function () {',
  16727. ' var $Self = this;',
  16728. ' function Sub() {',
  16729. ' $Self.Key = $Self.Key + 2;',
  16730. ' $Self.Key = $Self.Key + 3;',
  16731. ' $mod.TObject.State = $Self.State + 4;',
  16732. ' $mod.TObject.State = $Self.State + 5;',
  16733. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16734. ' $Self.SetSize($Self.GetSize() + 7);',
  16735. ' $Self.SetSize($Self.GetSize() + 8);',
  16736. ' };',
  16737. ' Sub();',
  16738. ' this.Key = this.Key + 12;',
  16739. ' $Self.Key = $Self.Key + 13;',
  16740. ' $mod.TObject.State = this.State + 14;',
  16741. ' $mod.TObject.State = $Self.State + 15;',
  16742. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16743. ' this.SetSize(this.GetSize() + 17);',
  16744. ' $Self.SetSize($Self.GetSize() + 18);',
  16745. ' };',
  16746. '});',
  16747. '']),
  16748. LinesToStr([ // $mod.$main
  16749. '']));
  16750. end;
  16751. procedure TTestModule.TestClass_NestedProcSelf2;
  16752. begin
  16753. StartProgram(false);
  16754. Add([
  16755. 'type',
  16756. ' TObject = class',
  16757. ' Key: longint;',
  16758. ' class var State: longint;',
  16759. ' function GetSize: longint; virtual; abstract;',
  16760. ' procedure SetSize(Value: longint); virtual; abstract;',
  16761. ' property Size: longint read GetSize write SetSize;',
  16762. ' end;',
  16763. ' TBird = class',
  16764. ' procedure DoIt;',
  16765. ' end;',
  16766. 'procedure tbird.doit;',
  16767. ' procedure Sub;',
  16768. ' begin',
  16769. ' key:=key+2;',
  16770. ' self.key:=self.key+3;',
  16771. ' state:=state+4;',
  16772. ' self.state:=self.state+5;',
  16773. ' tobject.state:=tobject.state+6;',
  16774. ' size:=size+7;',
  16775. ' self.size:=self.size+8;',
  16776. ' end;',
  16777. 'begin',
  16778. ' sub;',
  16779. ' key:=key+12;',
  16780. ' self.key:=self.key+13;',
  16781. ' state:=state+14;',
  16782. ' self.state:=self.state+15;',
  16783. ' tobject.state:=tobject.state+16;',
  16784. ' size:=size+17;',
  16785. ' self.size:=self.size+18;',
  16786. 'end;',
  16787. 'begin',
  16788. '']);
  16789. ConvertProgram;
  16790. CheckSource('TestClass_NestedProcSelf2',
  16791. LinesToStr([ // statements
  16792. 'rtl.createClass(this, "TObject", null, function () {',
  16793. ' this.State = 0;',
  16794. ' this.$init = function () {',
  16795. ' this.Key = 0;',
  16796. ' };',
  16797. ' this.$final = function () {',
  16798. ' };',
  16799. '});',
  16800. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16801. ' this.DoIt = function () {',
  16802. ' var $Self = this;',
  16803. ' function Sub() {',
  16804. ' $Self.Key = $Self.Key + 2;',
  16805. ' $Self.Key = $Self.Key + 3;',
  16806. ' $mod.TObject.State = $Self.State + 4;',
  16807. ' $mod.TObject.State = $Self.State + 5;',
  16808. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16809. ' $Self.SetSize($Self.GetSize() + 7);',
  16810. ' $Self.SetSize($Self.GetSize() + 8);',
  16811. ' };',
  16812. ' Sub();',
  16813. ' this.Key = this.Key + 12;',
  16814. ' $Self.Key = $Self.Key + 13;',
  16815. ' $mod.TObject.State = this.State + 14;',
  16816. ' $mod.TObject.State = $Self.State + 15;',
  16817. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16818. ' this.SetSize(this.GetSize() + 17);',
  16819. ' $Self.SetSize($Self.GetSize() + 18);',
  16820. ' };',
  16821. '});',
  16822. '']),
  16823. LinesToStr([ // $mod.$main
  16824. '']));
  16825. end;
  16826. procedure TTestModule.TestClass_NestedProcClassSelf;
  16827. begin
  16828. StartProgram(false);
  16829. Add([
  16830. 'type',
  16831. ' TObject = class',
  16832. ' class var State: longint;',
  16833. ' class procedure DoIt;',
  16834. ' class function GetSize: longint; virtual; abstract;',
  16835. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16836. ' class property Size: longint read GetSize write SetSize;',
  16837. ' end;',
  16838. 'class procedure tobject.doit;',
  16839. ' procedure Sub;',
  16840. ' begin',
  16841. ' state:=state+2;',
  16842. ' self.state:=self.state+3;',
  16843. ' tobject.state:=tobject.state+4;',
  16844. ' size:=size+5;',
  16845. ' self.size:=self.size+6;',
  16846. ' tobject.size:=tobject.size+7;',
  16847. ' end;',
  16848. 'begin',
  16849. ' sub;',
  16850. ' state:=state+12;',
  16851. ' self.state:=self.state+13;',
  16852. ' tobject.state:=tobject.state+14;',
  16853. ' size:=size+15;',
  16854. ' self.size:=self.size+16;',
  16855. ' tobject.size:=tobject.size+17;',
  16856. 'end;',
  16857. 'begin',
  16858. '']);
  16859. ConvertProgram;
  16860. CheckSource('TestClass_NestedProcClassSelf',
  16861. LinesToStr([ // statements
  16862. 'rtl.createClass(this, "TObject", null, function () {',
  16863. ' this.State = 0;',
  16864. ' this.$init = function () {',
  16865. ' };',
  16866. ' this.$final = function () {',
  16867. ' };',
  16868. ' this.DoIt = function () {',
  16869. ' var $Self = this;',
  16870. ' function Sub() {',
  16871. ' $mod.TObject.State = $Self.State + 2;',
  16872. ' $mod.TObject.State = $Self.State + 3;',
  16873. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16874. ' $Self.SetSize($Self.GetSize() + 5);',
  16875. ' $Self.SetSize($Self.GetSize() + 6);',
  16876. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16877. ' };',
  16878. ' Sub();',
  16879. ' $mod.TObject.State = this.State + 12;',
  16880. ' $mod.TObject.State = $Self.State + 13;',
  16881. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16882. ' this.SetSize(this.GetSize() + 15);',
  16883. ' $Self.SetSize($Self.GetSize() + 16);',
  16884. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16885. ' };',
  16886. '});',
  16887. '']),
  16888. LinesToStr([ // $mod.$main
  16889. '']));
  16890. end;
  16891. procedure TTestModule.TestClass_NestedProcCallInherited;
  16892. begin
  16893. StartProgram(false);
  16894. Add([
  16895. 'type',
  16896. ' TObject = class',
  16897. ' function DoIt(k: boolean): longint; virtual;',
  16898. ' end;',
  16899. ' TBird = class',
  16900. ' function DoIt(k: boolean): longint; override;',
  16901. ' end;',
  16902. 'function tobject.doit(k: boolean): longint;',
  16903. 'begin',
  16904. 'end;',
  16905. 'function tbird.doit(k: boolean): longint;',
  16906. ' procedure Sub;',
  16907. ' begin',
  16908. ' inherited DoIt(true);',
  16909. //' if inherited DoIt(false)=4 then ;',
  16910. ' end;',
  16911. 'begin',
  16912. ' Sub;',
  16913. ' inherited;',
  16914. ' inherited DoIt(true);',
  16915. //' if inherited DoIt(false)=14 then ;',
  16916. 'end;',
  16917. 'begin',
  16918. '']);
  16919. ConvertProgram;
  16920. CheckSource('TestClass_NestedProcCallInherited',
  16921. LinesToStr([ // statements
  16922. 'rtl.createClass(this, "TObject", null, function () {',
  16923. ' this.$init = function () {',
  16924. ' };',
  16925. ' this.$final = function () {',
  16926. ' };',
  16927. ' this.DoIt = function (k) {',
  16928. ' var Result = 0;',
  16929. ' return Result;',
  16930. ' };',
  16931. '});',
  16932. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16933. ' this.DoIt = function (k) {',
  16934. ' var $Self = this;',
  16935. ' var Result = 0;',
  16936. ' function Sub() {',
  16937. ' $mod.TObject.DoIt.call($Self, true);',
  16938. ' };',
  16939. ' Sub();',
  16940. ' $mod.TObject.DoIt.apply(this, arguments);',
  16941. ' $mod.TObject.DoIt.call(this, true);',
  16942. ' return Result;',
  16943. ' };',
  16944. '});',
  16945. '']),
  16946. LinesToStr([ // $mod.$main
  16947. '']));
  16948. end;
  16949. procedure TTestModule.TestClass_TObjectFree;
  16950. begin
  16951. StartProgram(false);
  16952. Add([
  16953. 'type',
  16954. ' TObject = class',
  16955. ' Obj: tobject;',
  16956. ' procedure Free;',
  16957. ' procedure Release;',
  16958. ' end;',
  16959. 'procedure tobject.free;',
  16960. 'begin',
  16961. 'end;',
  16962. 'procedure tobject.release;',
  16963. 'begin',
  16964. ' free;',
  16965. ' if true then free;',
  16966. 'end;',
  16967. 'function DoIt(o: tobject): tobject;',
  16968. 'var l: tobject;',
  16969. 'begin',
  16970. ' o.free;',
  16971. ' o.free();',
  16972. ' l.free;',
  16973. ' l.free();',
  16974. ' o.obj.free;',
  16975. ' o.obj.free();',
  16976. ' with o do obj.free;',
  16977. ' with o do obj.free();',
  16978. ' result.Free;',
  16979. ' result.Free();',
  16980. 'end;',
  16981. 'var o: tobject;',
  16982. ' a: array of tobject;',
  16983. 'begin',
  16984. ' o.free;',
  16985. ' o.obj.free;',
  16986. ' a[1+2].free;',
  16987. '']);
  16988. ConvertProgram;
  16989. CheckSource('TestClass_TObjectFree',
  16990. LinesToStr([ // statements
  16991. 'rtl.createClass(this, "TObject", null, function () {',
  16992. ' this.$init = function () {',
  16993. ' this.Obj = null;',
  16994. ' };',
  16995. ' this.$final = function () {',
  16996. ' this.Obj = undefined;',
  16997. ' };',
  16998. ' this.Free = function () {',
  16999. ' };',
  17000. ' this.Release = function () {',
  17001. ' this.Free();',
  17002. ' if (true) this.Free();',
  17003. ' };',
  17004. '});',
  17005. 'this.DoIt = function (o) {',
  17006. ' var Result = null;',
  17007. ' var l = null;',
  17008. ' o = rtl.freeLoc(o);',
  17009. ' o = rtl.freeLoc(o);',
  17010. ' l = rtl.freeLoc(l);',
  17011. ' l = rtl.freeLoc(l);',
  17012. ' rtl.free(o, "Obj");',
  17013. ' rtl.free(o, "Obj");',
  17014. ' rtl.free(o, "Obj");',
  17015. ' rtl.free(o, "Obj");',
  17016. ' Result = rtl.freeLoc(Result);',
  17017. ' Result = rtl.freeLoc(Result);',
  17018. ' return Result;',
  17019. '};',
  17020. 'this.o = null;',
  17021. 'this.a = [];',
  17022. '']),
  17023. LinesToStr([ // $mod.$main
  17024. 'rtl.free($mod, "o");',
  17025. 'rtl.free($mod.o, "Obj");',
  17026. 'rtl.free($mod.a, 1 + 2);',
  17027. '']));
  17028. end;
  17029. procedure TTestModule.TestClass_TObjectFree_VarArg;
  17030. begin
  17031. StartProgram(false);
  17032. Add([
  17033. 'type',
  17034. ' TObject = class',
  17035. ' Obj: tobject;',
  17036. ' procedure Free;',
  17037. ' end;',
  17038. 'procedure tobject.free;',
  17039. 'begin',
  17040. 'end;',
  17041. 'procedure DoIt(var o: tobject);',
  17042. 'begin',
  17043. ' o.free;',
  17044. ' o.free();',
  17045. 'end;',
  17046. 'begin',
  17047. '']);
  17048. ConvertProgram;
  17049. CheckSource('TestClass_TObjectFree_VarArg',
  17050. LinesToStr([ // statements
  17051. 'rtl.createClass(this, "TObject", null, function () {',
  17052. ' this.$init = function () {',
  17053. ' this.Obj = null;',
  17054. ' };',
  17055. ' this.$final = function () {',
  17056. ' this.Obj = undefined;',
  17057. ' };',
  17058. ' this.Free = function () {',
  17059. ' };',
  17060. '});',
  17061. 'this.DoIt = function (o) {',
  17062. ' o.set(rtl.freeLoc(o.get()));',
  17063. ' o.set(rtl.freeLoc(o.get()));',
  17064. '};',
  17065. '']),
  17066. LinesToStr([ // $mod.$main
  17067. '']));
  17068. end;
  17069. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17070. begin
  17071. StartProgram(false);
  17072. Add([
  17073. 'type',
  17074. ' TObject = class',
  17075. ' constructor Create;',
  17076. ' procedure Free;',
  17077. ' end;',
  17078. 'constructor TObject.Create; begin end;',
  17079. 'procedure tobject.free; begin end;',
  17080. 'begin',
  17081. ' with tobject.create do free;',
  17082. '']);
  17083. ConvertProgram;
  17084. CheckSource('TestClass_TObjectFreeNewInstance',
  17085. LinesToStr([ // statements
  17086. 'rtl.createClass(this, "TObject", null, function () {',
  17087. ' this.$init = function () {',
  17088. ' };',
  17089. ' this.$final = function () {',
  17090. ' };',
  17091. ' this.Create = function () {',
  17092. ' return this;',
  17093. ' };',
  17094. ' this.Free = function () {',
  17095. ' };',
  17096. '});',
  17097. '']),
  17098. LinesToStr([ // $mod.$main
  17099. 'var $with = $mod.TObject.$create("Create");',
  17100. '$with=rtl.freeLoc($with);',
  17101. '']));
  17102. end;
  17103. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17104. begin
  17105. StartProgram(false);
  17106. Add([
  17107. 'type',
  17108. ' TObject = class',
  17109. ' destructor Destroy;',
  17110. ' procedure Free;',
  17111. ' end;',
  17112. 'destructor TObject.Destroy; begin end;',
  17113. 'procedure tobject.free; begin end;',
  17114. 'var o: tobject;',
  17115. 'begin',
  17116. ' o.free;',
  17117. '']);
  17118. Converter.UseLowerCase:=true;
  17119. ConvertProgram;
  17120. CheckSource('TestClass_TObjectFreeLowerCase',
  17121. LinesToStr([ // statements
  17122. 'rtl.createClass(this, "tobject", null, function () {',
  17123. ' this.$init = function () {',
  17124. ' };',
  17125. ' this.$final = function () {',
  17126. ' };',
  17127. ' rtl.tObjectDestroy = "destroy";',
  17128. ' this.destroy = function () {',
  17129. ' };',
  17130. ' this.free = function () {',
  17131. ' };',
  17132. '});',
  17133. 'this.o = null;',
  17134. '']),
  17135. LinesToStr([ // $mod.$main
  17136. 'rtl.free($mod, "o");',
  17137. '']));
  17138. end;
  17139. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17140. begin
  17141. StartProgram(false);
  17142. Add([
  17143. 'type',
  17144. ' TObject = class',
  17145. ' procedure Free;',
  17146. ' function GetObj: tobject; virtual; abstract;',
  17147. ' end;',
  17148. 'procedure tobject.free;',
  17149. 'begin',
  17150. 'end;',
  17151. 'var o: tobject;',
  17152. 'begin',
  17153. ' o.getobj.free;',
  17154. '']);
  17155. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17156. ConvertProgram;
  17157. end;
  17158. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17159. begin
  17160. StartProgram(false);
  17161. Add([
  17162. 'type',
  17163. ' TObject = class',
  17164. ' procedure Free;',
  17165. ' FObj: TObject;',
  17166. ' property Obj: tobject read FObj write FObj;',
  17167. ' end;',
  17168. 'procedure tobject.free;',
  17169. 'begin',
  17170. 'end;',
  17171. 'var o: tobject;',
  17172. 'begin',
  17173. ' o.obj.free;',
  17174. '']);
  17175. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17176. ConvertProgram;
  17177. end;
  17178. procedure TTestModule.TestClass_ForIn;
  17179. begin
  17180. StartProgram(false);
  17181. Add([
  17182. 'type',
  17183. ' TObject = class end;',
  17184. ' TItem = TObject;',
  17185. ' TEnumerator = class',
  17186. ' FCurrent: TItem;',
  17187. ' property Current: TItem read FCurrent;',
  17188. ' function MoveNext: boolean;',
  17189. ' end;',
  17190. ' TBird = class',
  17191. ' function GetEnumerator: TEnumerator;',
  17192. ' end;',
  17193. 'function TEnumerator.MoveNext: boolean;',
  17194. 'begin',
  17195. 'end;',
  17196. 'function TBird.GetEnumerator: TEnumerator;',
  17197. 'begin',
  17198. 'end;',
  17199. 'var',
  17200. ' b: TBird;',
  17201. ' i, i2: TItem;',
  17202. 'begin',
  17203. ' for i in b do i2:=i;']);
  17204. ConvertProgram;
  17205. CheckSource('TestClass_ForIn',
  17206. LinesToStr([ // statements
  17207. 'rtl.createClass(this, "TObject", null, function () {',
  17208. ' this.$init = function () {',
  17209. ' };',
  17210. ' this.$final = function () {',
  17211. ' };',
  17212. '});',
  17213. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17214. ' this.$init = function () {',
  17215. ' $mod.TObject.$init.call(this);',
  17216. ' this.FCurrent = null;',
  17217. ' };',
  17218. ' this.$final = function () {',
  17219. ' this.FCurrent = undefined;',
  17220. ' $mod.TObject.$final.call(this);',
  17221. ' };',
  17222. ' this.MoveNext = function () {',
  17223. ' var Result = false;',
  17224. ' return Result;',
  17225. ' };',
  17226. '});',
  17227. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17228. ' this.GetEnumerator = function () {',
  17229. ' var Result = null;',
  17230. ' return Result;',
  17231. ' };',
  17232. '});',
  17233. 'this.b = null;',
  17234. 'this.i = null;',
  17235. 'this.i2 = null;'
  17236. ]),
  17237. LinesToStr([ // $mod.$main
  17238. 'var $in = $mod.b.GetEnumerator();',
  17239. 'try {',
  17240. ' while ($in.MoveNext()){',
  17241. ' $mod.i = $in.FCurrent;',
  17242. ' $mod.i2 = $mod.i;',
  17243. ' }',
  17244. '} finally {',
  17245. ' $in = rtl.freeLoc($in)',
  17246. '};',
  17247. '']));
  17248. end;
  17249. procedure TTestModule.TestClass_DispatchMessage;
  17250. begin
  17251. StartProgram(false);
  17252. Add([
  17253. 'type',
  17254. ' {$DispatchField DispInt}',
  17255. ' {$DispatchStrField DispStr}',
  17256. ' TObject = class',
  17257. ' procedure Dispatch(var Msg); virtual; abstract;',
  17258. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17259. ' end;',
  17260. ' THopMsg = record',
  17261. ' DispInt: longint;',
  17262. ' end;',
  17263. ' TPutMsg = record',
  17264. ' DispStr: string;',
  17265. ' end;',
  17266. ' TBird = class',
  17267. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17268. ' procedure Run; overload; virtual; abstract;',
  17269. ' procedure Run(var Msg); overload; message ''Fast'';',
  17270. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17271. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17272. ' end;',
  17273. 'procedure TBird.Run(var Msg);',
  17274. 'begin',
  17275. 'end;',
  17276. 'begin',
  17277. '']);
  17278. ConvertProgram;
  17279. CheckResolverUnexpectedHints(true);
  17280. CheckSource('TestClass_Message',
  17281. LinesToStr([ // statements
  17282. 'rtl.createClass(this, "TObject", null, function () {',
  17283. ' this.$init = function () {',
  17284. ' };',
  17285. ' this.$final = function () {',
  17286. ' };',
  17287. '});',
  17288. 'rtl.recNewT(this, "THopMsg", function () {',
  17289. ' this.DispInt = 0;',
  17290. ' this.$eq = function (b) {',
  17291. ' return this.DispInt === b.DispInt;',
  17292. ' };',
  17293. ' this.$assign = function (s) {',
  17294. ' this.DispInt = s.DispInt;',
  17295. ' return this;',
  17296. ' };',
  17297. '});',
  17298. 'rtl.recNewT(this, "TPutMsg", function () {',
  17299. ' this.DispStr = "";',
  17300. ' this.$eq = function (b) {',
  17301. ' return this.DispStr === b.DispStr;',
  17302. ' };',
  17303. ' this.$assign = function (s) {',
  17304. ' this.DispStr = s.DispStr;',
  17305. ' return this;',
  17306. ' };',
  17307. '});',
  17308. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17309. ' this.Run$1 = function (Msg) {',
  17310. ' };',
  17311. ' this.$msgint = {',
  17312. ' "2": "Fly",',
  17313. ' "3": "Hop"',
  17314. ' };',
  17315. ' this.$msgstr = {',
  17316. ' Fast: "Run$1",',
  17317. ' foo: "Put"',
  17318. ' };',
  17319. '});',
  17320. '']),
  17321. LinesToStr([ // $mod.$main
  17322. '']));
  17323. end;
  17324. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17325. begin
  17326. StartProgram(false);
  17327. Add([
  17328. 'type',
  17329. ' TObject = class',
  17330. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17331. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17332. ' end;',
  17333. 'begin',
  17334. '']);
  17335. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17336. ConvertProgram;
  17337. end;
  17338. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17339. begin
  17340. StartProgram(false);
  17341. Add([
  17342. 'type',
  17343. ' TObject = class',
  17344. ' procedure Dispatch(var Msg); virtual; abstract;',
  17345. ' end;',
  17346. ' TFlyMsg = record',
  17347. ' FlyId: longint;',
  17348. ' end;',
  17349. ' TBird = class',
  17350. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17351. ' end;',
  17352. 'begin',
  17353. '']);
  17354. ConvertProgram;
  17355. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17356. end;
  17357. procedure TTestModule.TestClassOf_Create;
  17358. begin
  17359. StartProgram(false);
  17360. Add('type');
  17361. Add(' TObject = class');
  17362. Add(' constructor Create;');
  17363. Add(' end;');
  17364. Add(' TClass = class of TObject;');
  17365. Add('constructor tobject.create; begin end;');
  17366. Add('var');
  17367. Add(' Obj: tobject;');
  17368. Add(' C: tclass;');
  17369. Add('begin');
  17370. Add(' obj:=C.create;');
  17371. Add(' with c do obj:=create;');
  17372. ConvertProgram;
  17373. CheckSource('TestClassOf_Create',
  17374. LinesToStr([ // statements
  17375. 'rtl.createClass(this, "TObject", null, function () {',
  17376. ' this.$init = function () {',
  17377. ' };',
  17378. ' this.$final = function () {',
  17379. ' };',
  17380. ' this.Create = function () {',
  17381. ' return this;',
  17382. ' };',
  17383. '});',
  17384. 'this.Obj = null;',
  17385. 'this.C = null;'
  17386. ]),
  17387. LinesToStr([ // $mod.$main
  17388. '$mod.Obj = $mod.C.$create("Create");',
  17389. 'var $with = $mod.C;',
  17390. '$mod.Obj = $with.$create("Create");',
  17391. '']));
  17392. end;
  17393. procedure TTestModule.TestClassOf_Call;
  17394. begin
  17395. StartProgram(false);
  17396. Add('type');
  17397. Add(' TObject = class');
  17398. Add(' class procedure DoIt;');
  17399. Add(' end;');
  17400. Add(' TClass = class of TObject;');
  17401. Add('class procedure tobject.doit; begin end;');
  17402. Add('var');
  17403. Add(' C: tclass;');
  17404. Add('begin');
  17405. Add(' c.doit;');
  17406. Add(' with c do doit;');
  17407. ConvertProgram;
  17408. CheckSource('TestClassOf_Call',
  17409. LinesToStr([ // statements
  17410. 'rtl.createClass(this, "TObject", null, function () {',
  17411. ' this.$init = function () {',
  17412. ' };',
  17413. ' this.$final = function () {',
  17414. ' };',
  17415. ' this.DoIt = function () {',
  17416. ' };',
  17417. '});',
  17418. 'this.C = null;'
  17419. ]),
  17420. LinesToStr([ // $mod.$main
  17421. '$mod.C.DoIt();',
  17422. 'var $with = $mod.C;',
  17423. '$with.DoIt();',
  17424. '']));
  17425. end;
  17426. procedure TTestModule.TestClassOf_Assign;
  17427. begin
  17428. StartProgram(false);
  17429. Add('type');
  17430. Add(' TClass = class of TObject;');
  17431. Add(' TObject = class');
  17432. Add(' ClassType: TClass; ');
  17433. Add(' end;');
  17434. Add('var');
  17435. Add(' Obj: tobject;');
  17436. Add(' C: tclass;');
  17437. Add('begin');
  17438. Add(' c:=nil;');
  17439. Add(' c:=obj.classtype;');
  17440. ConvertProgram;
  17441. CheckSource('TestClassOf_Assign',
  17442. LinesToStr([ // statements
  17443. 'rtl.createClass(this, "TObject", null, function () {',
  17444. ' this.$init = function () {',
  17445. ' this.ClassType = null;',
  17446. ' };',
  17447. ' this.$final = function () {',
  17448. ' this.ClassType = undefined;',
  17449. ' };',
  17450. '});',
  17451. 'this.Obj = null;',
  17452. 'this.C = null;'
  17453. ]),
  17454. LinesToStr([ // $mod.$main
  17455. '$mod.C = null;',
  17456. '$mod.C = $mod.Obj.ClassType;',
  17457. '']));
  17458. end;
  17459. procedure TTestModule.TestClassOf_Is;
  17460. begin
  17461. StartProgram(false);
  17462. Add('type');
  17463. Add(' TClass = class of TObject;');
  17464. Add(' TObject = class');
  17465. Add(' end;');
  17466. Add(' TCar = class');
  17467. Add(' end;');
  17468. Add(' TCars = class of TCar;');
  17469. Add('var');
  17470. Add(' Obj: tobject;');
  17471. Add(' C: tclass;');
  17472. Add(' Cars: tcars;');
  17473. Add('begin');
  17474. Add(' if c is tcar then ;');
  17475. Add(' if c is tcars then ;');
  17476. ConvertProgram;
  17477. CheckSource('TestClassOf_Is',
  17478. LinesToStr([ // statements
  17479. 'rtl.createClass(this, "TObject", null, function () {',
  17480. ' this.$init = function () {',
  17481. ' };',
  17482. ' this.$final = function () {',
  17483. ' };',
  17484. '});',
  17485. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17486. '});',
  17487. 'this.Obj = null;',
  17488. 'this.C = null;',
  17489. 'this.Cars = null;'
  17490. ]),
  17491. LinesToStr([ // $mod.$main
  17492. 'if(rtl.is($mod.C,$mod.TCar));',
  17493. 'if(rtl.is($mod.C,$mod.TCar));',
  17494. '']));
  17495. end;
  17496. procedure TTestModule.TestClassOf_Compare;
  17497. begin
  17498. StartProgram(false);
  17499. Add('type');
  17500. Add(' TClass = class of TObject;');
  17501. Add(' TObject = class');
  17502. Add(' ClassType: TClass; ');
  17503. Add(' end;');
  17504. Add('var');
  17505. Add(' b: boolean;');
  17506. Add(' Obj: tobject;');
  17507. Add(' C: tclass;');
  17508. Add('begin');
  17509. Add(' b:=c=nil;');
  17510. Add(' b:=nil=c;');
  17511. Add(' b:=c=obj.classtype;');
  17512. Add(' b:=obj.classtype=c;');
  17513. Add(' b:=c=TObject;');
  17514. Add(' b:=TObject=c;');
  17515. Add(' b:=c<>nil;');
  17516. Add(' b:=nil<>c;');
  17517. Add(' b:=c<>obj.classtype;');
  17518. Add(' b:=obj.classtype<>c;');
  17519. Add(' b:=c<>TObject;');
  17520. Add(' b:=TObject<>c;');
  17521. ConvertProgram;
  17522. CheckSource('TestClassOf_Compare',
  17523. LinesToStr([ // statements
  17524. 'rtl.createClass(this, "TObject", null, function () {',
  17525. ' this.$init = function () {',
  17526. ' this.ClassType = null;',
  17527. ' };',
  17528. ' this.$final = function () {',
  17529. ' this.ClassType = undefined;',
  17530. ' };',
  17531. '});',
  17532. 'this.b = false;',
  17533. 'this.Obj = null;',
  17534. 'this.C = null;'
  17535. ]),
  17536. LinesToStr([ // $mod.$main
  17537. '$mod.b = $mod.C === null;',
  17538. '$mod.b = null === $mod.C;',
  17539. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17540. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17541. '$mod.b = $mod.C === $mod.TObject;',
  17542. '$mod.b = $mod.TObject === $mod.C;',
  17543. '$mod.b = $mod.C !== null;',
  17544. '$mod.b = null !== $mod.C;',
  17545. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17546. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17547. '$mod.b = $mod.C !== $mod.TObject;',
  17548. '$mod.b = $mod.TObject !== $mod.C;',
  17549. '']));
  17550. end;
  17551. procedure TTestModule.TestClassOf_ClassVar;
  17552. begin
  17553. StartProgram(false);
  17554. Add('type');
  17555. Add(' TObject = class');
  17556. Add(' class var id: longint;');
  17557. Add(' end;');
  17558. Add(' TClass = class of TObject;');
  17559. Add('var');
  17560. Add(' C: tclass;');
  17561. Add('begin');
  17562. Add(' C.id:=C.id;');
  17563. ConvertProgram;
  17564. CheckSource('TestClassOf_ClassVar',
  17565. LinesToStr([ // statements
  17566. 'rtl.createClass(this, "TObject", null, function () {',
  17567. ' this.id = 0;',
  17568. ' this.$init = function () {',
  17569. ' };',
  17570. ' this.$final = function () {',
  17571. ' };',
  17572. '});',
  17573. 'this.C = null;'
  17574. ]),
  17575. LinesToStr([ // $mod.$main
  17576. '$mod.TObject.id = $mod.C.id;',
  17577. '']));
  17578. end;
  17579. procedure TTestModule.TestClassOf_ClassMethod;
  17580. begin
  17581. StartProgram(false);
  17582. Add('type');
  17583. Add(' TObject = class');
  17584. Add(' class function DoIt(i: longint = 0): longint;');
  17585. Add(' end;');
  17586. Add(' TClass = class of TObject;');
  17587. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17588. Add('var');
  17589. Add(' i: longint;');
  17590. Add(' C: tclass;');
  17591. Add('begin');
  17592. Add(' C.DoIt;');
  17593. Add(' C.DoIt();');
  17594. Add(' i:=C.DoIt;');
  17595. Add(' i:=C.DoIt();');
  17596. ConvertProgram;
  17597. CheckSource('TestClassOf_ClassMethod',
  17598. LinesToStr([ // statements
  17599. 'rtl.createClass(this, "TObject", null, function () {',
  17600. ' this.$init = function () {',
  17601. ' };',
  17602. ' this.$final = function () {',
  17603. ' };',
  17604. ' this.DoIt = function (i) {',
  17605. ' var Result = 0;',
  17606. ' return Result;',
  17607. ' };',
  17608. '});',
  17609. 'this.i = 0;',
  17610. 'this.C = null;'
  17611. ]),
  17612. LinesToStr([ // $mod.$main
  17613. '$mod.C.DoIt(0);',
  17614. '$mod.C.DoIt(0);',
  17615. '$mod.i = $mod.C.DoIt(0);',
  17616. '$mod.i = $mod.C.DoIt(0);',
  17617. '']));
  17618. end;
  17619. procedure TTestModule.TestClassOf_ClassProperty;
  17620. begin
  17621. StartProgram(false);
  17622. Add([
  17623. 'type',
  17624. ' TObject = class',
  17625. ' class var FA: longint;',
  17626. ' class function GetA: longint;',
  17627. ' class procedure SetA(Value: longint);',
  17628. ' class property pA: longint read fa write fa;',
  17629. ' class property pB: longint read geta write seta;',
  17630. ' end;',
  17631. ' TObjectClass = class of tobject;',
  17632. 'class function tobject.geta: longint; begin end;',
  17633. 'class procedure tobject.seta(value: longint); begin end;',
  17634. 'var',
  17635. ' b: boolean;',
  17636. ' Obj: tobject;',
  17637. ' Cla: tobjectclass;',
  17638. 'begin',
  17639. ' obj.pa:=obj.pa;',
  17640. ' obj.pb:=obj.pb;',
  17641. ' b:=obj.pa=4;',
  17642. ' b:=obj.pb=obj.pb;',
  17643. ' b:=5=obj.pa;',
  17644. ' cla.pa:=6;',
  17645. ' cla.pa:=cla.pa;',
  17646. ' cla.pb:=cla.pb;',
  17647. ' b:=cla.pa=7;',
  17648. ' b:=cla.pb=cla.pb;',
  17649. ' b:=8=cla.pa;',
  17650. ' tobject.pa:=9;',
  17651. ' tobject.pb:=tobject.pb;',
  17652. ' b:=tobject.pa=10;',
  17653. ' b:=11=tobject.pa;',
  17654. '']);
  17655. ConvertProgram;
  17656. CheckSource('TestClassOf_ClassProperty',
  17657. LinesToStr([ // statements
  17658. 'rtl.createClass(this, "TObject", null, function () {',
  17659. ' this.FA = 0;',
  17660. ' this.$init = function () {',
  17661. ' };',
  17662. ' this.$final = function () {',
  17663. ' };',
  17664. ' this.GetA = function () {',
  17665. ' var Result = 0;',
  17666. ' return Result;',
  17667. ' };',
  17668. ' this.SetA = function (Value) {',
  17669. ' };',
  17670. '});',
  17671. 'this.b = false;',
  17672. 'this.Obj = null;',
  17673. 'this.Cla = null;'
  17674. ]),
  17675. LinesToStr([ // $mod.$main
  17676. '$mod.TObject.FA = $mod.Obj.FA;',
  17677. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17678. '$mod.b = $mod.Obj.FA === 4;',
  17679. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17680. '$mod.b = 5 === $mod.Obj.FA;',
  17681. '$mod.TObject.FA = 6;',
  17682. '$mod.TObject.FA = $mod.Cla.FA;',
  17683. '$mod.Cla.SetA($mod.Cla.GetA());',
  17684. '$mod.b = $mod.Cla.FA === 7;',
  17685. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17686. '$mod.b = 8 === $mod.Cla.FA;',
  17687. '$mod.TObject.FA = 9;',
  17688. '$mod.TObject.SetA($mod.TObject.GetA());',
  17689. '$mod.b = $mod.TObject.FA === 10;',
  17690. '$mod.b = 11 === $mod.TObject.FA;',
  17691. '']));
  17692. end;
  17693. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17694. begin
  17695. StartProgram(false);
  17696. Add('type');
  17697. Add(' TObject = class');
  17698. Add(' class var GlobalId: longint;');
  17699. Add(' class procedure ProcA;');
  17700. Add(' end;');
  17701. Add('class procedure tobject.proca;');
  17702. Add('var b: boolean;');
  17703. Add('begin');
  17704. Add(' b:=self=nil;');
  17705. Add(' b:=self.globalid=3;');
  17706. Add(' b:=4=self.globalid;');
  17707. Add(' self.globalid:=5;');
  17708. Add(' self.proca;');
  17709. Add('end;');
  17710. Add('begin');
  17711. ConvertProgram;
  17712. CheckSource('TestClassOf_ClassMethodSelf',
  17713. LinesToStr([ // statements
  17714. 'rtl.createClass(this, "TObject", null, function () {',
  17715. ' this.GlobalId = 0;',
  17716. ' this.$init = function () {',
  17717. ' };',
  17718. ' this.$final = function () {',
  17719. ' };',
  17720. ' this.ProcA = function () {',
  17721. ' var b = false;',
  17722. ' b = this === null;',
  17723. ' b = this.GlobalId === 3;',
  17724. ' b = 4 === this.GlobalId;',
  17725. ' $mod.TObject.GlobalId = 5;',
  17726. ' this.ProcA();',
  17727. ' };',
  17728. '});'
  17729. ]),
  17730. LinesToStr([ // $mod.$main
  17731. '']));
  17732. end;
  17733. procedure TTestModule.TestClassOf_TypeCast;
  17734. begin
  17735. StartProgram(false);
  17736. Add('type');
  17737. Add(' TObject = class');
  17738. Add(' class procedure {#TObject_DoIt}DoIt;');
  17739. Add(' end;');
  17740. Add(' TClass = class of TObject;');
  17741. Add(' TMobile = class');
  17742. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17743. Add(' end;');
  17744. Add(' TMobileClass = class of TMobile;');
  17745. Add(' TCar = class(TMobile)');
  17746. Add(' class procedure {#TCar_DoIt}DoIt;');
  17747. Add(' end;');
  17748. Add(' TCarClass = class of TCar;');
  17749. Add('class procedure TObject.DoIt;');
  17750. Add('begin');
  17751. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17752. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17753. Add('end;');
  17754. Add('class procedure TMobile.DoIt;');
  17755. Add('begin');
  17756. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17757. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17758. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17759. Add('end;');
  17760. Add('class procedure TCar.DoIt; begin end;');
  17761. Add('var');
  17762. Add(' ObjC: TClass;');
  17763. Add(' MobileC: TMobileClass;');
  17764. Add(' CarC: TCarClass;');
  17765. Add('begin');
  17766. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17767. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17768. Add(' CarC.{@TCar_DoIt}DoIt;');
  17769. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17770. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17771. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17772. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17773. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17774. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17775. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17776. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17777. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17778. ConvertProgram;
  17779. CheckSource('TestClassOf_TypeCast',
  17780. LinesToStr([ // statements
  17781. 'rtl.createClass(this, "TObject", null, function () {',
  17782. ' this.$init = function () {',
  17783. ' };',
  17784. ' this.$final = function () {',
  17785. ' };',
  17786. ' this.DoIt = function () {',
  17787. ' this.DoIt();',
  17788. ' this.DoIt$1();',
  17789. ' };',
  17790. '});',
  17791. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17792. ' this.DoIt$1 = function () {',
  17793. ' this.DoIt();',
  17794. ' this.DoIt$1();',
  17795. ' this.DoIt$2();',
  17796. ' };',
  17797. '});',
  17798. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17799. ' this.DoIt$2 = function () {',
  17800. ' };',
  17801. '});',
  17802. 'this.ObjC = null;',
  17803. 'this.MobileC = null;',
  17804. 'this.CarC = null;',
  17805. '']),
  17806. LinesToStr([ // $mod.$main
  17807. '$mod.ObjC.DoIt();',
  17808. '$mod.MobileC.DoIt$1();',
  17809. '$mod.CarC.DoIt$2();',
  17810. '$mod.ObjC.DoIt();',
  17811. '$mod.ObjC.DoIt$1();',
  17812. '$mod.ObjC.DoIt$2();',
  17813. '$mod.MobileC.DoIt();',
  17814. '$mod.MobileC.DoIt$1();',
  17815. '$mod.MobileC.DoIt$2();',
  17816. '$mod.CarC.DoIt();',
  17817. '$mod.CarC.DoIt$1();',
  17818. '$mod.CarC.DoIt$2();',
  17819. '']));
  17820. end;
  17821. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17822. begin
  17823. StartProgram(false);
  17824. Add('type');
  17825. Add(' TObject = class');
  17826. Add(' function CurNow: longint; ');
  17827. Add(' class function Now: longint; ');
  17828. Add(' end;');
  17829. Add('function TObject.CurNow: longint; begin end;');
  17830. Add('class function TObject.Now: longint; begin end;');
  17831. Add('var');
  17832. Add(' Obj: tobject;');
  17833. Add(' vI: longint;');
  17834. Add('begin');
  17835. Add(' obj.curnow;');
  17836. Add(' vi:=obj.curnow;');
  17837. Add(' tobject.now;');
  17838. Add(' vi:=tobject.now;');
  17839. ConvertProgram;
  17840. CheckSource('TestClassOf_ImplicitFunctionCall',
  17841. LinesToStr([ // statements
  17842. 'rtl.createClass(this, "TObject", null, function () {',
  17843. ' this.$init = function () {',
  17844. ' };',
  17845. ' this.$final = function () {',
  17846. ' };',
  17847. ' this.CurNow = function () {',
  17848. ' var Result = 0;',
  17849. ' return Result;',
  17850. ' };',
  17851. ' this.Now = function () {',
  17852. ' var Result = 0;',
  17853. ' return Result;',
  17854. ' };',
  17855. '});',
  17856. 'this.Obj = null;',
  17857. 'this.vI = 0;',
  17858. '']),
  17859. LinesToStr([ // $mod.$main
  17860. '$mod.Obj.CurNow();',
  17861. '$mod.vI = $mod.Obj.CurNow();',
  17862. '$mod.TObject.Now();',
  17863. '$mod.vI = $mod.TObject.Now();',
  17864. '']));
  17865. end;
  17866. procedure TTestModule.TestClassOf_Const;
  17867. begin
  17868. StartProgram(false);
  17869. Add([
  17870. 'type',
  17871. ' TObject = class',
  17872. ' end;',
  17873. ' TBird = TObject;',
  17874. ' TBirds = class of TBird;',
  17875. ' TEagles = TBirds;',
  17876. ' THawk = class(TBird);',
  17877. 'const',
  17878. ' Hawk: TEagles = THawk;',
  17879. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17880. ' TBird,',
  17881. ' THawk',
  17882. ' );',
  17883. 'begin']);
  17884. ConvertProgram;
  17885. CheckSource('TestClassOf_Const',
  17886. LinesToStr([ // statements
  17887. 'rtl.createClass(this, "TObject", null, function () {',
  17888. ' this.$init = function () {',
  17889. ' };',
  17890. ' this.$final = function () {',
  17891. ' };',
  17892. '});',
  17893. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17894. '});',
  17895. 'this.Hawk = this.THawk;',
  17896. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17897. '']),
  17898. LinesToStr([ // $mod.$main
  17899. '']));
  17900. end;
  17901. procedure TTestModule.TestNestedClass_Alias;
  17902. begin
  17903. WithTypeInfo:=true;
  17904. StartProgram(false);
  17905. Add([
  17906. 'type',
  17907. ' TObject = class',
  17908. ' type TNested = type longint;',
  17909. ' end;',
  17910. 'type TAlias = type tobject.tnested;',
  17911. 'var i: tobject.tnested = 3;',
  17912. 'var j: TAlias = 4;',
  17913. 'begin',
  17914. ' if typeinfo(TAlias)=nil then ;',
  17915. ' if typeinfo(tobject.tnested)=nil then ;',
  17916. '']);
  17917. ConvertProgram;
  17918. CheckSource('TestNestedClass_Alias',
  17919. LinesToStr([ // statements
  17920. 'rtl.createClass(this, "TObject", null, function () {',
  17921. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17922. ' this.$init = function () {',
  17923. ' };',
  17924. ' this.$final = function () {',
  17925. ' };',
  17926. '});',
  17927. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17928. 'this.i = 3;',
  17929. 'this.j = 4;',
  17930. '']),
  17931. LinesToStr([ // $mod.$main
  17932. 'if ($mod.$rtti["TAlias"] === null) ;',
  17933. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17934. '']));
  17935. end;
  17936. procedure TTestModule.TestNestedClass_Record;
  17937. begin
  17938. WithTypeInfo:=true;
  17939. StartProgram(false);
  17940. Add([
  17941. 'type',
  17942. ' TObject = class',
  17943. ' type TPoint = record',
  17944. ' x,y: byte;',
  17945. ' end;',
  17946. ' procedure DoIt(t: TPoint);',
  17947. ' end;',
  17948. 'procedure tobject.DoIt(t: TPoint);',
  17949. 'var p: TPoint;',
  17950. 'begin',
  17951. ' t.x:=t.y;',
  17952. ' p:=t;',
  17953. 'end;',
  17954. 'var',
  17955. ' p: tobject.tpoint = (x:2; y:4);',
  17956. ' o: TObject;',
  17957. 'begin',
  17958. ' p:=p;',
  17959. ' o.doit(p);',
  17960. '']);
  17961. ConvertProgram;
  17962. CheckSource('TestNestedClass_Record',
  17963. LinesToStr([ // statements
  17964. 'rtl.createClass(this, "TObject", null, function () {',
  17965. ' rtl.recNewT(this, "TPoint", function () {',
  17966. ' this.x = 0;',
  17967. ' this.y = 0;',
  17968. ' this.$eq = function (b) {',
  17969. ' return (this.x === b.x) && (this.y === b.y);',
  17970. ' };',
  17971. ' this.$assign = function (s) {',
  17972. ' this.x = s.x;',
  17973. ' this.y = s.y;',
  17974. ' return this;',
  17975. ' };',
  17976. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17977. ' $r.addField("x", rtl.byte);',
  17978. ' $r.addField("y", rtl.byte);',
  17979. ' });',
  17980. ' this.$init = function () {',
  17981. ' };',
  17982. ' this.$final = function () {',
  17983. ' };',
  17984. ' this.DoIt = function (t) {',
  17985. ' var p = this.TPoint.$new();',
  17986. ' t.x = t.y;',
  17987. ' p.$assign(t);',
  17988. ' };',
  17989. '});',
  17990. 'this.p = this.TObject.TPoint.$clone({',
  17991. ' x: 2,',
  17992. ' y: 4',
  17993. '});',
  17994. 'this.o = null;',
  17995. '']),
  17996. LinesToStr([ // $mod.$main
  17997. '$mod.p.$assign($mod.p);',
  17998. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17999. '']));
  18000. end;
  18001. procedure TTestModule.TestNestedClass_Class;
  18002. begin
  18003. StartProgram(false);
  18004. Add([
  18005. 'type',
  18006. ' TObject = class end;',
  18007. ' TBird = class',
  18008. ' type TLeg = class',
  18009. ' FId: longint;',
  18010. ' constructor Create;',
  18011. ' function Create(i: longint): TLeg;',
  18012. ' end;',
  18013. ' function DoIt(b: TBird): Tleg;',
  18014. ' end;',
  18015. 'constructor tbird.tleg.create;',
  18016. 'begin',
  18017. ' FId:=3;',
  18018. 'end;',
  18019. 'function tbird.tleg.Create(i: longint): TLeg;',
  18020. 'begin',
  18021. ' Create;',
  18022. ' Result:=TLeg.Create;',
  18023. ' Result:=TBird.TLeg.Create;',
  18024. ' Result:=Create(3);',
  18025. ' FId:=i;',
  18026. 'end;',
  18027. 'function tbird.DoIt(b: tbird): tleg;',
  18028. 'begin',
  18029. ' Result.Create;',
  18030. ' Result:=TLeg.Create;',
  18031. ' Result:=TBird.TLeg.Create;',
  18032. ' Result:=Result.Create(3);',
  18033. 'end;',
  18034. 'var',
  18035. ' b: Tbird.tleg;',
  18036. 'begin',
  18037. ' b.Create;',
  18038. ' b:=TBird.TLeg.Create;',
  18039. ' b:=b.Create(3);',
  18040. '']);
  18041. ConvertProgram;
  18042. CheckSource('TestNestedClass_Class',
  18043. LinesToStr([ // statements
  18044. 'rtl.createClass(this, "TObject", null, function () {',
  18045. ' this.$init = function () {',
  18046. ' };',
  18047. ' this.$final = function () {',
  18048. ' };',
  18049. '});',
  18050. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18051. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  18052. ' this.$init = function () {',
  18053. ' $mod.TObject.$init.call(this);',
  18054. ' this.FId = 0;',
  18055. ' };',
  18056. ' this.Create = function () {',
  18057. ' this.FId = 3;',
  18058. ' return this;',
  18059. ' };',
  18060. ' this.Create$1 = function (i) {',
  18061. ' var Result = null;',
  18062. ' this.Create();',
  18063. ' Result = $mod.TBird.TLeg.$create("Create");',
  18064. ' Result = $mod.TBird.TLeg.$create("Create");',
  18065. ' Result = this.Create$1(3);',
  18066. ' this.FId = i;',
  18067. ' return Result;',
  18068. ' };',
  18069. ' }, "TBird.TLeg");',
  18070. ' this.DoIt = function (b) {',
  18071. ' var Result = null;',
  18072. ' Result.Create();',
  18073. ' Result = this.TLeg.$create("Create");',
  18074. ' Result = $mod.TBird.TLeg.$create("Create");',
  18075. ' Result = Result.Create$1(3);',
  18076. ' return Result;',
  18077. ' };',
  18078. '});',
  18079. 'this.b = null;',
  18080. '']),
  18081. LinesToStr([ // $mod.$main
  18082. '$mod.b.Create();',
  18083. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18084. '$mod.b = $mod.b.Create$1(3);',
  18085. '']));
  18086. end;
  18087. procedure TTestModule.TestNestedClass_CallInherited;
  18088. begin
  18089. StartProgram(false);
  18090. Add([
  18091. 'type',
  18092. ' TObject = class end;',
  18093. ' TBird = class',
  18094. ' type',
  18095. ' TWing = class',
  18096. ' function Fly(w: word = 17): word; virtual;',
  18097. ' end;',
  18098. ' end;',
  18099. ' TEagle = class(TBird)',
  18100. ' type',
  18101. ' TEagleWing = class(TWing)',
  18102. ' function Fly(w: word): word; override;',
  18103. ' end;',
  18104. ' end;',
  18105. 'function TBird.TWing.Fly(w: word): word;',
  18106. 'begin',
  18107. 'end;',
  18108. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18109. 'begin',
  18110. ' inherited;',
  18111. ' inherited Fly;',
  18112. ' inherited Fly(3);',
  18113. ' Result:=inherited Fly;',
  18114. ' Result:=inherited Fly(4);',
  18115. 'end;',
  18116. 'begin',
  18117. '']);
  18118. ConvertProgram;
  18119. CheckSource('TestNestedClass_CallInherited',
  18120. LinesToStr([ // statements
  18121. 'rtl.createClass(this, "TObject", null, function () {',
  18122. ' this.$init = function () {',
  18123. ' };',
  18124. ' this.$final = function () {',
  18125. ' };',
  18126. '});',
  18127. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18128. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18129. ' this.Fly = function (w) {',
  18130. ' var Result = 0;',
  18131. ' return Result;',
  18132. ' };',
  18133. ' }, "TBird.TWing");',
  18134. '});',
  18135. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18136. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18137. ' this.Fly = function (w) {',
  18138. ' var Result = 0;',
  18139. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18140. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18141. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18142. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18143. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18144. ' return Result;',
  18145. ' };',
  18146. ' }, "TEagle.TEagleWing");',
  18147. '});',
  18148. '']),
  18149. LinesToStr([ // $mod.$main
  18150. '']));
  18151. end;
  18152. procedure TTestModule.TestExternalClass_Var;
  18153. begin
  18154. StartProgram(false);
  18155. Add([
  18156. '{$modeswitch externalclass}',
  18157. 'type',
  18158. ' TExtA = class external name ''ExtObj''',
  18159. ' Id: longint external name ''$Id'';',
  18160. ' B: longint;',
  18161. ' end;',
  18162. 'var Obj: TExtA;',
  18163. 'begin',
  18164. ' obj.id:=obj.id+1;',
  18165. ' obj.B:=obj.B+1;']);
  18166. ConvertProgram;
  18167. CheckSource('TestExternalClass_Var',
  18168. LinesToStr([ // statements
  18169. 'this.Obj = null;',
  18170. '']),
  18171. LinesToStr([ // $mod.$main
  18172. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18173. '$mod.Obj.B = $mod.Obj.B + 1;',
  18174. '']));
  18175. end;
  18176. procedure TTestModule.TestExternalClass_Const;
  18177. begin
  18178. StartProgram(false);
  18179. Add([
  18180. '{$modeswitch externalclass}',
  18181. 'type',
  18182. ' TExtA = class external name ''ExtObj''',
  18183. ' const Two: longint = 2;',
  18184. ' const Three = 3;',
  18185. ' const Id: longint;',
  18186. ' end;',
  18187. ' TExtB = class external name ''ExtB''',
  18188. ' A: TExtA;',
  18189. ' end;',
  18190. 'var',
  18191. ' A: texta;',
  18192. ' B: textb;',
  18193. ' i: longint;',
  18194. 'begin',
  18195. ' i:=a.two;',
  18196. ' i:=texta.two;',
  18197. ' i:=a.three;',
  18198. ' i:=texta.three;',
  18199. ' i:=a.id;',
  18200. ' i:=texta.id;',
  18201. '']);
  18202. ConvertProgram;
  18203. CheckSource('TestExternalClass_Const',
  18204. LinesToStr([ // statements
  18205. 'this.A = null;',
  18206. 'this.B = null;',
  18207. 'this.i = 0;',
  18208. '']),
  18209. LinesToStr([ // $mod.$main
  18210. '$mod.i = 2;',
  18211. '$mod.i = 2;',
  18212. '$mod.i = 3;',
  18213. '$mod.i = 3;',
  18214. '$mod.i = $mod.A.Id;',
  18215. '$mod.i = ExtObj.Id;',
  18216. '']));
  18217. end;
  18218. procedure TTestModule.TestExternalClass_Dollar;
  18219. begin
  18220. StartProgram(false);
  18221. Add([
  18222. '{$modeswitch externalclass}',
  18223. 'type',
  18224. ' TExtA = class external name ''$''',
  18225. ' Id: longint external name ''$'';',
  18226. ' function Bla(i: longint): longint; external name ''$'';',
  18227. ' end;',
  18228. 'function dollar(k: longint): longint; external name ''$'';',
  18229. 'var Obj: TExtA;',
  18230. 'begin',
  18231. ' dollar(1);',
  18232. ' obj.id:=obj.id+2;',
  18233. ' obj.Bla(3);',
  18234. '']);
  18235. ConvertProgram;
  18236. CheckSource('TestExternalClass_Dollar',
  18237. LinesToStr([ // statements
  18238. 'this.Obj = null;',
  18239. '']),
  18240. LinesToStr([ // $mod.$main
  18241. '$(1);',
  18242. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18243. '$mod.Obj.$(3);',
  18244. '']));
  18245. end;
  18246. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18247. begin
  18248. StartProgram(false);
  18249. Add('{$modeswitch externalclass}');
  18250. Add('type');
  18251. Add(' TExtA = class external name ''ExtA''');
  18252. Add(' Id: longint external name ''$Id'';');
  18253. Add(' end;');
  18254. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18255. Add(' Id: longint;');
  18256. Add(' end;');
  18257. Add('begin');
  18258. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18259. ConvertProgram;
  18260. end;
  18261. procedure TTestModule.TestExternalClass_Method;
  18262. begin
  18263. StartProgram(false);
  18264. Add(['{$modeswitch externalclass}',
  18265. 'type',
  18266. ' TExtA = class external name ''ExtObj''',
  18267. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18268. ' procedure DoSome(Id: longint = 1);',
  18269. ' end;',
  18270. 'var Obj: texta;',
  18271. 'begin',
  18272. ' obj.doit;',
  18273. ' obj.doit();',
  18274. ' obj.doit(2);',
  18275. ' with obj do begin',
  18276. ' doit;',
  18277. ' doit();',
  18278. ' doit(3);',
  18279. ' end;']);
  18280. ConvertProgram;
  18281. CheckSource('TestExternalClass_Method',
  18282. LinesToStr([ // statements
  18283. 'this.Obj = null;',
  18284. '']),
  18285. LinesToStr([ // $mod.$main
  18286. '$mod.Obj.$Execute(1);',
  18287. '$mod.Obj.$Execute(1);',
  18288. '$mod.Obj.$Execute(2);',
  18289. 'var $with = $mod.Obj;',
  18290. '$with.$Execute(1);',
  18291. '$with.$Execute(1);',
  18292. '$with.$Execute(3);',
  18293. '']));
  18294. end;
  18295. procedure TTestModule.TestExternalClass_ClassMethod;
  18296. begin
  18297. StartProgram(false);
  18298. Add([
  18299. '{$modeswitch externalclass}',
  18300. 'type',
  18301. ' TExtA = class external name ''ExtObj''',
  18302. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18303. ' end;',
  18304. ' TExtB = TExtA;',
  18305. 'var p: Pointer;',
  18306. 'begin',
  18307. ' texta.doit;',
  18308. ' texta.doit();',
  18309. ' texta.doit(2);',
  18310. ' p:[email protected];',
  18311. ' with texta do begin',
  18312. ' doit;',
  18313. ' doit();',
  18314. ' doit(3);',
  18315. ' p:=@DoIt;',
  18316. ' end;',
  18317. ' textb.doit;',
  18318. ' textb.doit();',
  18319. ' textb.doit(4);',
  18320. ' with textb do begin',
  18321. ' doit;',
  18322. ' doit();',
  18323. ' doit(5);',
  18324. ' end;',
  18325. '']);
  18326. ConvertProgram;
  18327. CheckSource('TestExternalClass_ClassMethod',
  18328. LinesToStr([ // statements
  18329. 'this.p = null;',
  18330. '']),
  18331. LinesToStr([ // $mod.$main
  18332. 'ExtObj.$Execute(1);',
  18333. 'ExtObj.$Execute(1);',
  18334. 'ExtObj.$Execute(2);',
  18335. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18336. 'ExtObj.$Execute(1);',
  18337. 'ExtObj.$Execute(1);',
  18338. 'ExtObj.$Execute(3);',
  18339. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18340. 'ExtObj.$Execute(1);',
  18341. 'ExtObj.$Execute(1);',
  18342. 'ExtObj.$Execute(4);',
  18343. 'ExtObj.$Execute(1);',
  18344. 'ExtObj.$Execute(1);',
  18345. 'ExtObj.$Execute(5);',
  18346. '']));
  18347. end;
  18348. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18349. begin
  18350. StartProgram(false);
  18351. Add([
  18352. '{$modeswitch externalclass}',
  18353. 'type',
  18354. ' TExtA = class external name ''ExtObj''',
  18355. ' class procedure DoIt(Id: longint = 1); static;',
  18356. ' end;',
  18357. 'var p: Pointer;',
  18358. 'begin',
  18359. ' texta.doit;',
  18360. ' texta.doit();',
  18361. ' texta.doit(2);',
  18362. ' p:[email protected];',
  18363. ' with texta do begin',
  18364. ' doit;',
  18365. ' doit();',
  18366. ' doit(3);',
  18367. ' p:=@DoIt;',
  18368. ' end;',
  18369. '']);
  18370. ConvertProgram;
  18371. CheckSource('TestExternalClass_ClassMethodStatic',
  18372. LinesToStr([ // statements
  18373. 'this.p = null;',
  18374. '']),
  18375. LinesToStr([ // $mod.$main
  18376. 'ExtObj.DoIt(1);',
  18377. 'ExtObj.DoIt(1);',
  18378. 'ExtObj.DoIt(2);',
  18379. '$mod.p = ExtObj.DoIt;',
  18380. 'ExtObj.DoIt(1);',
  18381. 'ExtObj.DoIt(1);',
  18382. 'ExtObj.DoIt(3);',
  18383. '$mod.p = ExtObj.DoIt;',
  18384. '']));
  18385. end;
  18386. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18387. begin
  18388. StartProgram(false);
  18389. Add([
  18390. '{$modeswitch externalclass}',
  18391. 'type',
  18392. ' TBird = class external name ''Array''',
  18393. ' end;',
  18394. 'function GetPtr: Pointer;',
  18395. 'begin',
  18396. 'end;',
  18397. 'procedure Write(const p);',
  18398. 'begin',
  18399. 'end;',
  18400. 'procedure WriteLn; varargs;',
  18401. 'begin',
  18402. 'end;',
  18403. 'begin',
  18404. ' if TBird(GetPtr)=nil then ;',
  18405. ' Write(GetPtr);',
  18406. ' WriteLn(GetPtr);',
  18407. ' Write(TBird(GetPtr));',
  18408. ' WriteLn(TBird(GetPtr));',
  18409. '']);
  18410. ConvertProgram;
  18411. CheckSource('TestFunctionResultInTypeCast',
  18412. LinesToStr([ // statements
  18413. 'this.GetPtr = function () {',
  18414. ' var Result = null;',
  18415. ' return Result;',
  18416. '};',
  18417. 'this.Write = function (p) {',
  18418. '};',
  18419. 'this.WriteLn = function () {',
  18420. '};',
  18421. '']),
  18422. LinesToStr([
  18423. 'if ($mod.GetPtr() === null) ;',
  18424. '$mod.Write($mod.GetPtr());',
  18425. '$mod.WriteLn($mod.GetPtr());',
  18426. '$mod.Write($mod.GetPtr());',
  18427. '$mod.WriteLn($mod.GetPtr());',
  18428. '']));
  18429. end;
  18430. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18431. begin
  18432. StartProgram(false);
  18433. Add([
  18434. '{$modeswitch externalclass}',
  18435. 'type',
  18436. ' TExtA = class external name ''ExtObjA''',
  18437. ' procedure ProcA; virtual;',
  18438. ' procedure ProcB; virtual;',
  18439. ' end;',
  18440. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18441. ' end;',
  18442. ' TExtC = class (TExtB)',
  18443. ' procedure ProcA; override;',
  18444. ' end;',
  18445. 'procedure TExtC.ProcA;',
  18446. 'begin',
  18447. ' ProcA;',
  18448. ' Self.ProcA;',
  18449. ' ProcB;',
  18450. ' Self.ProcB;',
  18451. 'end;',
  18452. 'var',
  18453. ' A: texta;',
  18454. ' B: textb;',
  18455. ' C: textc;',
  18456. 'begin',
  18457. ' a.proca;',
  18458. ' b.proca;',
  18459. ' c.proca;']);
  18460. ConvertProgram;
  18461. CheckSource('TestExternalClass_NonExternalOverride',
  18462. LinesToStr([ // statements
  18463. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18464. ' this.$init = function () {',
  18465. ' };',
  18466. ' this.$final = function () {',
  18467. ' };',
  18468. ' this.ProcA = function () {',
  18469. ' this.ProcA();',
  18470. ' this.ProcA();',
  18471. ' this.ProcB();',
  18472. ' this.ProcB();',
  18473. ' };',
  18474. '});',
  18475. 'this.A = null;',
  18476. 'this.B = null;',
  18477. 'this.C = null;',
  18478. '']),
  18479. LinesToStr([ // $mod.$main
  18480. '$mod.A.ProcA();',
  18481. '$mod.B.ProcA();',
  18482. '$mod.C.ProcA();',
  18483. '']));
  18484. end;
  18485. procedure TTestModule.TestExternalClass_OverloadHint;
  18486. begin
  18487. StartProgram(false);
  18488. Add([
  18489. '{$modeswitch externalclass}',
  18490. 'type',
  18491. ' TExtA = class external name ''ExtObjA''',
  18492. ' procedure DoIt;',
  18493. ' procedure DoIt(i: longint);',
  18494. ' end;',
  18495. 'begin',
  18496. '']);
  18497. ConvertProgram;
  18498. CheckResolverUnexpectedHints(true);
  18499. CheckSource('TestExternalClass_OverloadHint',
  18500. LinesToStr([ // statements
  18501. '']),
  18502. LinesToStr([ // $mod.$main
  18503. '']));
  18504. end;
  18505. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18506. begin
  18507. WithTypeInfo:=true;
  18508. StartProgram(false);
  18509. Add([
  18510. '{$modeswitch externalclass}',
  18511. 'type',
  18512. ' JSwiper = class external name ''Swiper''',
  18513. ' constructor New;',
  18514. ' end;',
  18515. ' TObject = class',
  18516. ' private',
  18517. ' FSwiper: JSwiper;',
  18518. ' published',
  18519. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18520. ' end;',
  18521. 'begin',
  18522. ' JSwiper.new;',
  18523. '']);
  18524. ConvertProgram;
  18525. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18526. LinesToStr([ // statements
  18527. 'this.$rtti.$ExtClass("JSwiper", {',
  18528. ' jsclass: "Swiper"',
  18529. '});',
  18530. 'rtl.createClass(this, "TObject", null, function () {',
  18531. ' this.$init = function () {',
  18532. ' this.FSwiper = null;',
  18533. ' };',
  18534. ' this.$final = function () {',
  18535. ' this.FSwiper = undefined;',
  18536. ' };',
  18537. ' var $r = this.$rtti;',
  18538. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18539. '});',
  18540. '']),
  18541. LinesToStr([ // $mod.$main
  18542. 'new Swiper();',
  18543. '']));
  18544. end;
  18545. procedure TTestModule.TestExternalClass_Property;
  18546. begin
  18547. StartProgram(false);
  18548. Add([
  18549. '{$modeswitch externalclass}',
  18550. 'type',
  18551. ' TExtA = class external name ''ExtA''',
  18552. ' function getYear: longint;',
  18553. ' procedure setYear(Value: longint);',
  18554. ' property Year: longint read getyear write setyear;',
  18555. ' end;',
  18556. ' TExtB = class (TExtA)',
  18557. ' procedure OtherSetYear(Value: longint);',
  18558. ' property year write othersetyear;',
  18559. ' end;',
  18560. 'procedure textb.othersetyear(value: longint);',
  18561. 'begin',
  18562. ' setYear(Value+4);',
  18563. 'end;',
  18564. 'var',
  18565. ' A: texta;',
  18566. ' B: textb;',
  18567. 'begin',
  18568. ' a.year:=a.year+1;',
  18569. ' b.year:=b.year+2;']);
  18570. ConvertProgram;
  18571. CheckSource('TestExternalClass_NonExternalOverride',
  18572. LinesToStr([ // statements
  18573. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18574. ' this.$init = function () {',
  18575. ' };',
  18576. ' this.$final = function () {',
  18577. ' };',
  18578. ' this.OtherSetYear = function (Value) {',
  18579. ' this.setYear(Value+4);',
  18580. ' };',
  18581. '});',
  18582. 'this.A = null;',
  18583. 'this.B = null;',
  18584. '']),
  18585. LinesToStr([ // $mod.$main
  18586. '$mod.A.setYear($mod.A.getYear()+1);',
  18587. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18588. '']));
  18589. end;
  18590. procedure TTestModule.TestExternalClass_PropertyDate;
  18591. begin
  18592. StartProgram(false);
  18593. Add([
  18594. '{$modeswitch externalclass}',
  18595. 'type',
  18596. ' TExtA = class external name ''ExtA''',
  18597. ' end;',
  18598. ' TExtB = class (TExtA)',
  18599. ' FDate: string;',
  18600. ' property Date: string read FDate write FDate;',
  18601. ' property ExtA: string read FDate write FDate;',
  18602. ' end;',
  18603. ' {$M+}',
  18604. ' TObject = class',
  18605. ' FDate: string;',
  18606. ' published',
  18607. ' property Date: string read FDate write FDate;',
  18608. ' property ExtA: string read FDate write FDate;',
  18609. ' end;',
  18610. 'var',
  18611. ' B: textb;',
  18612. ' o: TObject;',
  18613. 'begin',
  18614. ' b.date:=b.exta;',
  18615. ' o.date:=o.exta;']);
  18616. ConvertProgram;
  18617. CheckSource('TestExternalClass_PropertyDate',
  18618. LinesToStr([ // statements
  18619. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18620. ' this.$init = function () {',
  18621. ' this.FDate = "";',
  18622. ' };',
  18623. ' this.$final = function () {',
  18624. ' };',
  18625. '});',
  18626. 'rtl.createClass(this, "TObject", null, function () {',
  18627. ' this.$init = function () {',
  18628. ' this.FDate = "";',
  18629. ' };',
  18630. ' this.$final = function () {',
  18631. ' };',
  18632. ' var $r = this.$rtti;',
  18633. ' $r.addField("FDate", rtl.string);',
  18634. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18635. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18636. '});',
  18637. 'this.B = null;',
  18638. 'this.o = null;',
  18639. '']),
  18640. LinesToStr([ // $mod.$main
  18641. '$mod.B.FDate = $mod.B.FDate;',
  18642. '$mod.o.FDate = $mod.o.FDate;',
  18643. '']));
  18644. end;
  18645. procedure TTestModule.TestExternalClass_ClassProperty;
  18646. begin
  18647. StartProgram(false);
  18648. Add('{$modeswitch externalclass}');
  18649. Add('type');
  18650. Add(' TExtA = class external name ''ExtA''');
  18651. Add(' class function getYear: longint;');
  18652. Add(' class procedure setYear(Value: longint);');
  18653. Add(' class property Year: longint read getyear write setyear;');
  18654. Add(' end;');
  18655. Add(' TExtB = class (TExtA)');
  18656. Add(' class function GetCentury: longint;');
  18657. Add(' class procedure SetCentury(Value: longint);');
  18658. Add(' class property Century: longint read getcentury write setcentury;');
  18659. Add(' end;');
  18660. Add('class function textb.getcentury: longint;');
  18661. Add('begin');
  18662. Add('end;');
  18663. Add('class procedure textb.setcentury(value: longint);');
  18664. Add('begin');
  18665. Add(' setyear(value+11);');
  18666. Add(' texta.year:=texta.year+12;');
  18667. Add(' year:=year+13;');
  18668. Add(' textb.century:=textb.century+14;');
  18669. Add(' century:=century+15;');
  18670. Add('end;');
  18671. Add('var');
  18672. Add(' A: texta;');
  18673. Add(' B: textb;');
  18674. Add('begin');
  18675. Add(' texta.year:=texta.year+1;');
  18676. Add(' textb.year:=textb.year+2;');
  18677. Add(' TextA.year:=TextA.year+3;');
  18678. Add(' b.year:=b.year+4;');
  18679. Add(' textb.century:=textb.century+5;');
  18680. Add(' b.century:=b.century+6;');
  18681. ConvertProgram;
  18682. CheckSource('TestExternalClass_ClassProperty',
  18683. LinesToStr([ // statements
  18684. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18685. ' this.$init = function () {',
  18686. ' };',
  18687. ' this.$final = function () {',
  18688. ' };',
  18689. ' this.GetCentury = function () {',
  18690. ' var Result = 0;',
  18691. ' return Result;',
  18692. ' };',
  18693. ' this.SetCentury = function (Value) {',
  18694. ' this.setYear(Value + 11);',
  18695. ' ExtA.setYear(ExtA.getYear() + 12);',
  18696. ' this.setYear(this.getYear() + 13);',
  18697. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18698. ' this.SetCentury(this.GetCentury() + 15);',
  18699. ' };',
  18700. '});',
  18701. 'this.A = null;',
  18702. 'this.B = null;',
  18703. '']),
  18704. LinesToStr([ // $mod.$main
  18705. 'ExtA.setYear(ExtA.getYear() + 1);',
  18706. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18707. 'ExtA.setYear(ExtA.getYear() + 3);',
  18708. '$mod.B.setYear($mod.B.getYear() + 4);',
  18709. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18710. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18711. '']));
  18712. end;
  18713. procedure TTestModule.TestExternalClass_ClassOf;
  18714. begin
  18715. StartProgram(false);
  18716. Add('{$modeswitch externalclass}');
  18717. Add('type');
  18718. Add(' TExtA = class external name ''ExtA''');
  18719. Add(' procedure ProcA; virtual;');
  18720. Add(' procedure ProcB; virtual;');
  18721. Add(' end;');
  18722. Add(' TExtAClass = class of TExtA;');
  18723. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18724. Add(' end;');
  18725. Add(' TExtBClass = class of TExtB;');
  18726. Add(' TExtC = class (TExtB)');
  18727. Add(' procedure ProcA; override;');
  18728. Add(' end;');
  18729. Add(' TExtCClass = class of TExtC;');
  18730. Add('procedure TExtC.ProcA; begin end;');
  18731. Add('var');
  18732. Add(' A: texta; ClA: TExtAClass;');
  18733. Add(' B: textb; ClB: TExtBClass;');
  18734. Add(' C: textc; ClC: TExtCClass;');
  18735. Add('begin');
  18736. Add(' ClA:=texta;');
  18737. Add(' ClA:=textb;');
  18738. Add(' ClA:=textc;');
  18739. Add(' ClB:=textb;');
  18740. Add(' ClB:=textc;');
  18741. Add(' ClC:=textc;');
  18742. ConvertProgram;
  18743. CheckSource('TestExternalClass_ClassOf',
  18744. LinesToStr([ // statements
  18745. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18746. ' this.$init = function () {',
  18747. ' };',
  18748. ' this.$final = function () {',
  18749. ' };',
  18750. ' this.ProcA = function () {',
  18751. ' };',
  18752. '});',
  18753. 'this.A = null;',
  18754. 'this.ClA = null;',
  18755. 'this.B = null;',
  18756. 'this.ClB = null;',
  18757. 'this.C = null;',
  18758. 'this.ClC = null;',
  18759. '']),
  18760. LinesToStr([ // $mod.$main
  18761. '$mod.ClA = ExtA;',
  18762. '$mod.ClA = ExtB;',
  18763. '$mod.ClA = $mod.TExtC;',
  18764. '$mod.ClB = ExtB;',
  18765. '$mod.ClB = $mod.TExtC;',
  18766. '$mod.ClC = $mod.TExtC;',
  18767. '']));
  18768. end;
  18769. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18770. begin
  18771. AddModuleWithIntfImplSrc('unit2.pas',
  18772. LinesToStr([
  18773. '{$modeswitch externalclass}',
  18774. 'type',
  18775. ' TExtA = class external name ''ExtA''',
  18776. ' class var Id: longint;',
  18777. ' end;',
  18778. '']),
  18779. '');
  18780. StartUnit(true);
  18781. Add('interface');
  18782. Add('uses unit2;');
  18783. Add('implementation');
  18784. Add('begin');
  18785. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18786. ConvertUnit;
  18787. CheckSource('TestExternalClass_ClassOtherUnit',
  18788. LinesToStr([
  18789. '']),
  18790. LinesToStr([
  18791. 'ExtA.Id = ExtA.Id + 1;',
  18792. '']));
  18793. end;
  18794. procedure TTestModule.TestExternalClass_Is;
  18795. begin
  18796. StartProgram(false);
  18797. Add([
  18798. '{$modeswitch externalclass}',
  18799. 'type',
  18800. ' TExtA = class external name ''ExtA''',
  18801. ' end;',
  18802. ' TExtAClass = class of TExtA;',
  18803. ' TExtB = class external name ''ExtB'' (TExtA)',
  18804. ' end;',
  18805. ' TExtBClass = class of TExtB;',
  18806. ' TExtC = class (TExtB)',
  18807. ' end;',
  18808. ' TExtCClass = class of TExtC;',
  18809. 'var',
  18810. ' A: texta; ClA: TExtAClass;',
  18811. ' B: textb; ClB: TExtBClass;',
  18812. ' C: textc; ClC: TExtCClass;',
  18813. 'begin',
  18814. ' if a is textb then ;',
  18815. ' if a is textc then ;',
  18816. ' if b is textc then ;',
  18817. ' if cla is textb then ;',
  18818. ' if cla is textc then ;',
  18819. ' if clb is textc then ;',
  18820. ' try',
  18821. ' except',
  18822. ' on TExtA do ;',
  18823. ' on e: TExtB do ;',
  18824. ' end;',
  18825. '']);
  18826. ConvertProgram;
  18827. CheckSource('TestExternalClass_Is',
  18828. LinesToStr([ // statements
  18829. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18830. ' this.$init = function () {',
  18831. ' };',
  18832. ' this.$final = function () {',
  18833. ' };',
  18834. '});',
  18835. 'this.A = null;',
  18836. 'this.ClA = null;',
  18837. 'this.B = null;',
  18838. 'this.ClB = null;',
  18839. 'this.C = null;',
  18840. 'this.ClC = null;',
  18841. '']),
  18842. LinesToStr([ // $mod.$main
  18843. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18844. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18845. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18846. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18847. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18848. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18849. 'try {} catch ($e) {',
  18850. ' if (rtl.isExt($e,ExtA)) {}',
  18851. ' else if (rtl.isExt($e,ExtB)) {',
  18852. ' var e = $e;',
  18853. ' } else throw $e',
  18854. '};',
  18855. '']));
  18856. end;
  18857. procedure TTestModule.TestExternalClass_As;
  18858. begin
  18859. StartProgram(false);
  18860. Add('{$modeswitch externalclass}');
  18861. Add('type');
  18862. Add(' TExtA = class external name ''ExtA''');
  18863. Add(' end;');
  18864. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18865. Add(' end;');
  18866. Add(' TExtC = class (TExtB)');
  18867. Add(' end;');
  18868. Add('var');
  18869. Add(' A: texta;');
  18870. Add(' B: textb;');
  18871. Add(' C: textc;');
  18872. Add('begin');
  18873. Add(' b:=a as textb;');
  18874. Add(' c:=a as textc;');
  18875. Add(' c:=b as textc;');
  18876. ConvertProgram;
  18877. CheckSource('TestExternalClass_Is',
  18878. LinesToStr([ // statements
  18879. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18880. ' this.$init = function () {',
  18881. ' };',
  18882. ' this.$final = function () {',
  18883. ' };',
  18884. '});',
  18885. 'this.A = null;',
  18886. 'this.B = null;',
  18887. 'this.C = null;',
  18888. '']),
  18889. LinesToStr([ // $mod.$main
  18890. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18891. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18892. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18893. '']));
  18894. end;
  18895. procedure TTestModule.TestExternalClass_DestructorFail;
  18896. begin
  18897. StartProgram(false);
  18898. Add('{$modeswitch externalclass}');
  18899. Add('type');
  18900. Add(' TExtA = class external name ''ExtA''');
  18901. Add(' destructor Free;');
  18902. Add(' end;');
  18903. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18904. nPasElementNotSupported);
  18905. ConvertProgram;
  18906. end;
  18907. procedure TTestModule.TestExternalClass_New;
  18908. begin
  18909. StartProgram(false);
  18910. Add([
  18911. '{$modeswitch externalclass}',
  18912. 'type',
  18913. ' TExtA = class external name ''ExtA''',
  18914. ' constructor New;',
  18915. ' constructor New(i: longint; j: longint = 2);',
  18916. ' end;',
  18917. 'var',
  18918. ' A: texta;',
  18919. 'begin',
  18920. ' a:=texta.new;',
  18921. ' a:=texta(texta.new);',
  18922. ' a:=texta.new();',
  18923. ' a:=texta.new(1);',
  18924. ' with texta do begin',
  18925. ' a:=new;',
  18926. ' a:=new();',
  18927. ' a:=new(2);',
  18928. ' end;',
  18929. ' a:=test1.texta.new;',
  18930. ' a:=test1.texta.new();',
  18931. ' a:=test1.texta.new(3);',
  18932. '']);
  18933. ConvertProgram;
  18934. CheckSource('TestExternalClass_New',
  18935. LinesToStr([ // statements
  18936. 'this.A = null;',
  18937. '']),
  18938. LinesToStr([ // $mod.$main
  18939. '$mod.A = new ExtA();',
  18940. '$mod.A = new ExtA();',
  18941. '$mod.A = new ExtA();',
  18942. '$mod.A = new ExtA(1,2);',
  18943. '$mod.A = new ExtA();',
  18944. '$mod.A = new ExtA();',
  18945. '$mod.A = new ExtA(2,2);',
  18946. '$mod.A = new ExtA();',
  18947. '$mod.A = new ExtA();',
  18948. '$mod.A = new ExtA(3,2);',
  18949. '']));
  18950. end;
  18951. procedure TTestModule.TestExternalClass_ClassOf_New;
  18952. begin
  18953. StartProgram(false);
  18954. Add('{$modeswitch externalclass}');
  18955. Add('type');
  18956. Add(' TExtAClass = class of TExtA;');
  18957. Add(' TExtA = class external name ''ExtA''');
  18958. Add(' C: TExtAClass;');
  18959. Add(' constructor New;');
  18960. Add(' end;');
  18961. Add('var');
  18962. Add(' A: texta;');
  18963. Add(' C: textaclass;');
  18964. Add('begin');
  18965. Add(' a:=c.new;');
  18966. Add(' a:=c.new();');
  18967. Add(' with C do begin');
  18968. Add(' a:=new;');
  18969. Add(' a:=new();');
  18970. Add(' end;');
  18971. Add(' a:=test1.c.new;');
  18972. Add(' a:=test1.c.new();');
  18973. Add(' a:=A.c.new();');
  18974. ConvertProgram;
  18975. CheckSource('TestExternalClass_ClassOf_New',
  18976. LinesToStr([ // statements
  18977. 'this.A = null;',
  18978. 'this.C = null;',
  18979. '']),
  18980. LinesToStr([ // $mod.$main
  18981. '$mod.A = new $mod.C();',
  18982. '$mod.A = new $mod.C();',
  18983. 'var $with = $mod.C;',
  18984. '$mod.A = new $with();',
  18985. '$mod.A = new $with();',
  18986. '$mod.A = new $mod.C();',
  18987. '$mod.A = new $mod.C();',
  18988. '$mod.A = new $mod.A.C();',
  18989. '']));
  18990. end;
  18991. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18992. begin
  18993. StartProgram(false);
  18994. Add([
  18995. '{$modeswitch externalclass}',
  18996. 'type',
  18997. ' TExtAClass = class of TExtA;',
  18998. ' TExtA = class external name ''ExtA''',
  18999. ' constructor New;',
  19000. ' end;',
  19001. 'function GetCreator: TExtAClass;',
  19002. 'begin',
  19003. ' Result:=TExtA;',
  19004. 'end;',
  19005. 'var',
  19006. ' A: texta;',
  19007. 'begin',
  19008. ' a:=getcreator.new;',
  19009. ' a:=getcreator().new;',
  19010. ' a:=getcreator().new();',
  19011. ' a:=getcreator.new();',
  19012. ' with getcreator do begin',
  19013. ' a:=new;',
  19014. ' a:=new();',
  19015. ' end;']);
  19016. ConvertProgram;
  19017. CheckSource('TestExternalClass_FuncClassOf_New',
  19018. LinesToStr([ // statements
  19019. 'this.GetCreator = function () {',
  19020. ' var Result = null;',
  19021. ' Result = ExtA;',
  19022. ' return Result;',
  19023. '};',
  19024. 'this.A = null;',
  19025. '']),
  19026. LinesToStr([ // $mod.$main
  19027. '$mod.A = new ($mod.GetCreator())();',
  19028. '$mod.A = new ($mod.GetCreator())();',
  19029. '$mod.A = new ($mod.GetCreator())();',
  19030. '$mod.A = new ($mod.GetCreator())();',
  19031. 'var $with = $mod.GetCreator();',
  19032. '$mod.A = new $with();',
  19033. '$mod.A = new $with();',
  19034. '']));
  19035. end;
  19036. procedure TTestModule.TestExternalClass_New_PasClassFail;
  19037. begin
  19038. StartProgram(false);
  19039. Add([
  19040. '{$modeswitch externalclass}',
  19041. 'type',
  19042. ' TExtA = class external name ''ExtA''',
  19043. ' constructor New;',
  19044. ' end;',
  19045. ' TBird = class(TExtA)',
  19046. ' end;',
  19047. 'begin',
  19048. ' TBird.new;',
  19049. '']);
  19050. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19051. ConvertProgram;
  19052. end;
  19053. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  19054. begin
  19055. StartProgram(false);
  19056. Add([
  19057. '{$modeswitch externalclass}',
  19058. 'type',
  19059. ' TExtA = class external name ''ExtA''',
  19060. ' constructor New;',
  19061. ' end;',
  19062. ' TBird = class(TExtA)',
  19063. ' end;',
  19064. 'begin',
  19065. ' TBird.new();',
  19066. '']);
  19067. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19068. ConvertProgram;
  19069. end;
  19070. procedure TTestModule.TestExternalClass_NewExtName;
  19071. begin
  19072. StartProgram(false);
  19073. Add([
  19074. '{$modeswitch externalclass}',
  19075. 'type',
  19076. ' TExtA = class external name ''ExtA''',
  19077. ' constructor New; external name ''Other'';',
  19078. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19079. ' end;',
  19080. 'var',
  19081. ' A: texta;',
  19082. 'begin',
  19083. ' a:=texta.new;',
  19084. ' a:=texta(texta.new);',
  19085. ' a:=texta.new();',
  19086. ' a:=texta.new(1);',
  19087. ' with texta do begin',
  19088. ' a:=new;',
  19089. ' a:=new();',
  19090. ' a:=new(2);',
  19091. ' end;',
  19092. ' a:=test1.texta.new;',
  19093. ' a:=test1.texta.new();',
  19094. ' a:=test1.texta.new(3);',
  19095. '']);
  19096. ConvertProgram;
  19097. CheckSource('TestExternalClass_NewExtName',
  19098. LinesToStr([ // statements
  19099. 'this.A = null;',
  19100. '']),
  19101. LinesToStr([ // $mod.$main
  19102. '$mod.A = new Other();',
  19103. '$mod.A = new Other();',
  19104. '$mod.A = new Other();',
  19105. '$mod.A = new A.B(1,2);',
  19106. '$mod.A = new Other();',
  19107. '$mod.A = new Other();',
  19108. '$mod.A = new A.B(2,2);',
  19109. '$mod.A = new Other();',
  19110. '$mod.A = new Other();',
  19111. '$mod.A = new A.B(3,2);',
  19112. '']));
  19113. end;
  19114. procedure TTestModule.TestExternalClass_Constructor;
  19115. begin
  19116. StartProgram(false);
  19117. Add([
  19118. '{$modeswitch externalclass}',
  19119. 'type',
  19120. ' TExtA = class external name ''ExtA''',
  19121. ' public type',
  19122. ' TExtB = class external name ''ExtB''',
  19123. ' public type',
  19124. ' TExtC = class external name ''ExtC''',
  19125. ' constructor New;',
  19126. ' constructor New(i: word);',
  19127. ' end;',
  19128. ' end;',
  19129. ' constructor Create;',
  19130. ' constructor Create(i: longint; j: longint = 2);',
  19131. ' end;',
  19132. 'var',
  19133. ' A: texta;',
  19134. ' C: texta.textb.textc;',
  19135. 'begin',
  19136. ' a:=texta.create;',
  19137. ' a:=texta(texta.create);',
  19138. ' a:=texta.create();',
  19139. ' a:=texta.create(1);',
  19140. ' with texta do begin',
  19141. ' a:=create;',
  19142. ' a:=create();',
  19143. ' a:=create(2);',
  19144. ' end;',
  19145. ' a:=test1.texta.create;',
  19146. ' a:=test1.texta.create();',
  19147. ' a:=test1.texta.create(3);',
  19148. ' c:=texta.textb.textc.new;',
  19149. ' c:=texta.textb.textc.new();',
  19150. ' c:=texta.textb.textc.new(4);',
  19151. '']);
  19152. ConvertProgram;
  19153. CheckSource('TestExternalClass_Constructor',
  19154. LinesToStr([ // statements
  19155. 'this.A = null;',
  19156. 'this.C = null;',
  19157. '']),
  19158. LinesToStr([ // $mod.$main
  19159. '$mod.A = new ExtA.Create();',
  19160. '$mod.A = new ExtA.Create();',
  19161. '$mod.A = new ExtA.Create();',
  19162. '$mod.A = new ExtA.Create(1,2);',
  19163. '$mod.A = new ExtA.Create();',
  19164. '$mod.A = new ExtA.Create();',
  19165. '$mod.A = new ExtA.Create(2,2);',
  19166. '$mod.A = new ExtA.Create();',
  19167. '$mod.A = new ExtA.Create();',
  19168. '$mod.A = new ExtA.Create(3,2);',
  19169. '$mod.C = new ExtA.ExtB.ExtC();',
  19170. '$mod.C = new ExtA.ExtB.ExtC();',
  19171. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19172. '']));
  19173. end;
  19174. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19175. begin
  19176. StartProgram(false);
  19177. Add([
  19178. '{$modeswitch externalclass}',
  19179. 'type',
  19180. ' TExtA = class external name ''ExtA''',
  19181. ' constructor Create; external name ''{}'';',
  19182. ' end;',
  19183. 'var',
  19184. ' A: texta;',
  19185. 'begin',
  19186. ' a:=texta.create;',
  19187. ' a:=texta(texta.create);',
  19188. ' a:=texta.create();',
  19189. ' with texta do begin',
  19190. ' a:=create;',
  19191. ' a:=create();',
  19192. ' end;',
  19193. ' a:=test1.texta.create;',
  19194. ' a:=test1.texta.create();',
  19195. '']);
  19196. ConvertProgram;
  19197. CheckSource('TestExternalClass_ConstructorBrackets',
  19198. LinesToStr([ // statements
  19199. 'this.A = null;',
  19200. '']),
  19201. LinesToStr([ // $mod.$main
  19202. '$mod.A = {};',
  19203. '$mod.A = {};',
  19204. '$mod.A = {};',
  19205. '$mod.A = {};',
  19206. '$mod.A = {};',
  19207. '$mod.A = {};',
  19208. '$mod.A = {};',
  19209. '']));
  19210. end;
  19211. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19212. begin
  19213. StartProgram(false);
  19214. Add('{$modeswitch externalclass}');
  19215. Add('type');
  19216. Add(' TExtA = class external name ''ExtA''');
  19217. Add(' constructor New;');
  19218. Add(' end;');
  19219. Add('function DoIt: longint;');
  19220. Add('const ExtA: longint = 3;');
  19221. Add('begin');
  19222. Add(' Result:=ExtA;');
  19223. Add('end;');
  19224. Add('var');
  19225. Add(' A: texta;');
  19226. Add('begin');
  19227. Add(' a:=texta.new;');
  19228. ConvertProgram;
  19229. CheckSource('TestExternalClass_LocalConstSameName',
  19230. LinesToStr([ // statements
  19231. 'var ExtA$1 = 3;',
  19232. 'this.DoIt = function () {',
  19233. ' var Result = 0;',
  19234. ' Result = ExtA$1;',
  19235. ' return Result;',
  19236. '};',
  19237. 'this.A = null;',
  19238. '']),
  19239. LinesToStr([ // $mod.$main
  19240. '$mod.A = new ExtA();',
  19241. '']));
  19242. end;
  19243. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19244. begin
  19245. StartProgram(false);
  19246. Add('{$modeswitch externalclass}');
  19247. Add('type');
  19248. Add(' TExtA = class external name ''ExtA''');
  19249. Add(' procedure DoIt;');
  19250. Add(' end;');
  19251. Add(' TMyA = class(TExtA)');
  19252. Add(' procedure DoIt;');
  19253. Add(' end;');
  19254. Add('procedure TMyA.DoIt; begin end;');
  19255. Add('begin');
  19256. ConvertProgram;
  19257. CheckSource('TestExternalClass_ReintroduceOverload',
  19258. LinesToStr([ // statements
  19259. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19260. ' this.$init = function () {',
  19261. ' };',
  19262. ' this.$final = function () {',
  19263. ' };',
  19264. ' this.DoIt$1 = function () {',
  19265. ' };',
  19266. '});',
  19267. '']),
  19268. LinesToStr([ // $mod.$main
  19269. '']));
  19270. end;
  19271. procedure TTestModule.TestExternalClass_Inherited;
  19272. begin
  19273. StartProgram(false);
  19274. Add('{$modeswitch externalclass}');
  19275. Add('type');
  19276. Add(' TExtA = class external name ''ExtA''');
  19277. Add(' procedure DoIt(i: longint = 1); virtual;');
  19278. Add(' procedure DoSome(j: longint = 2);');
  19279. Add(' end;');
  19280. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19281. Add(' end;');
  19282. Add(' TMyC = class(TExtB)');
  19283. Add(' procedure DoIt(i: longint = 1); override;');
  19284. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19285. Add(' end;');
  19286. Add('procedure TMyC.DoIt(i: longint);');
  19287. Add('begin');
  19288. Add(' inherited;');
  19289. Add(' inherited DoIt;');
  19290. Add(' inherited DoIt();');
  19291. Add(' inherited DoIt(3);');
  19292. Add(' inherited DoSome;');
  19293. Add(' inherited DoSome();');
  19294. Add(' inherited DoSome(4);');
  19295. Add('end;');
  19296. Add('procedure TMyC.DoSome(j: longint);');
  19297. Add('begin');
  19298. Add(' inherited;');
  19299. Add('end;');
  19300. Add('begin');
  19301. ConvertProgram;
  19302. CheckSource('TestExternalClass_ReintroduceOverload',
  19303. LinesToStr([ // statements
  19304. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19305. ' this.$init = function () {',
  19306. ' };',
  19307. ' this.$final = function () {',
  19308. ' };',
  19309. ' this.DoIt = function (i) {',
  19310. ' ExtB.DoIt.apply(this, arguments);',
  19311. ' ExtB.DoIt.call(this, 1);',
  19312. ' ExtB.DoIt.call(this, 1);',
  19313. ' ExtB.DoIt.call(this, 3);',
  19314. ' ExtB.DoSome.call(this, 2);',
  19315. ' ExtB.DoSome.call(this, 2);',
  19316. ' ExtB.DoSome.call(this, 4);',
  19317. ' };',
  19318. ' this.DoSome$1 = function (j) {',
  19319. ' ExtB.DoSome.apply(this, arguments);',
  19320. ' };',
  19321. '});',
  19322. '']),
  19323. LinesToStr([ // $mod.$main
  19324. '']));
  19325. end;
  19326. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19327. begin
  19328. StartProgram(false);
  19329. Add('{$modeswitch externalclass}');
  19330. Add('type');
  19331. Add(' TObject = class');
  19332. Add(' end;');
  19333. Add(' TExtA = class external name ''ExtA''(TObject)');
  19334. Add(' end;');
  19335. Add('begin');
  19336. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19337. ConvertProgram;
  19338. end;
  19339. procedure TTestModule.TestExternalClass_NewInstance;
  19340. begin
  19341. StartProgram(false);
  19342. Add('{$modeswitch externalclass}');
  19343. Add('type');
  19344. Add(' TExtA = class external name ''ExtA''');
  19345. Add(' end;');
  19346. Add(' TMyB = class(TExtA)');
  19347. Add(' protected');
  19348. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19349. Add(' end;');
  19350. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19351. Add('begin end;');
  19352. Add('begin');
  19353. ConvertProgram;
  19354. CheckSource('TestExternalClass_NewInstance',
  19355. LinesToStr([ // statements
  19356. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19357. ' this.$init = function () {',
  19358. ' };',
  19359. ' this.$final = function () {',
  19360. ' };',
  19361. ' this.NewInstance = function (fnname, paramarray) {',
  19362. ' var Result = null;',
  19363. ' return Result;',
  19364. ' };',
  19365. '});',
  19366. '']),
  19367. LinesToStr([ // $mod.$main
  19368. '']));
  19369. end;
  19370. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19371. begin
  19372. StartProgram(false);
  19373. Add('{$modeswitch externalclass}');
  19374. Add('type');
  19375. Add(' TExtA = class external name ''ExtA''');
  19376. Add(' end;');
  19377. Add(' TMyB = class(TExtA)');
  19378. Add(' protected');
  19379. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19380. Add(' end;');
  19381. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19382. Add('begin end;');
  19383. Add('begin');
  19384. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19385. ConvertProgram;
  19386. end;
  19387. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19388. begin
  19389. StartProgram(false);
  19390. Add('{$modeswitch externalclass}');
  19391. Add('type');
  19392. Add(' TExtA = class external name ''ExtA''');
  19393. Add(' end;');
  19394. Add(' TMyB = class(TExtA)');
  19395. Add(' protected');
  19396. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19397. Add(' end;');
  19398. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19399. Add('begin end;');
  19400. Add('begin');
  19401. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19402. nIncompatibleTypeArgNo);
  19403. ConvertProgram;
  19404. end;
  19405. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19406. begin
  19407. StartProgram(false);
  19408. Add('{$modeswitch externalclass}');
  19409. Add('type');
  19410. Add(' TExtA = class external name ''ExtA''');
  19411. Add(' end;');
  19412. Add(' TMyB = class(TExtA)');
  19413. Add(' protected');
  19414. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19415. Add(' end;');
  19416. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19417. Add('begin end;');
  19418. Add('begin');
  19419. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19420. nIncompatibleTypeArgNo);
  19421. ConvertProgram;
  19422. end;
  19423. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19424. begin
  19425. StartProgram(false);
  19426. Add([
  19427. '{$modeswitch externalclass}',
  19428. 'type',
  19429. ' TJSFunction = class external name ''Function''',
  19430. ' end;',
  19431. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19432. ' constructor New(w: word);',
  19433. ' end;',
  19434. ' TBird = class (TExtA)',
  19435. ' public',
  19436. ' Size: word;',
  19437. ' class var Legs: word;',
  19438. ' constructor Create(a: word);',
  19439. ' end;',
  19440. ' TEagle = class (TBird)',
  19441. ' public',
  19442. ' constructor Create(b: word); reintroduce;',
  19443. ' end;',
  19444. 'constructor TBird.Create(a: word);',
  19445. 'begin',
  19446. ' inherited;', // silently ignored
  19447. ' inherited New(a);', // this.$func(a)
  19448. 'end;',
  19449. 'constructor TEagle.Create(b: word);',
  19450. 'begin',
  19451. ' inherited Create(b);',
  19452. 'end;',
  19453. 'var',
  19454. ' Bird: TBird;',
  19455. ' Eagle: TEagle;',
  19456. 'begin',
  19457. ' Bird:=TBird.Create(3);',
  19458. ' Eagle:=TEagle.Create(4);',
  19459. ' Bird.Size:=Bird.Size+5;',
  19460. ' Bird.Legs:=Bird.Legs+6;',
  19461. ' Eagle.Size:=Eagle.Size+5;',
  19462. ' Eagle.Legs:=Eagle.Legs+6;',
  19463. '']);
  19464. ConvertProgram;
  19465. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19466. LinesToStr([ // statements
  19467. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19468. ' this.Legs = 0;',
  19469. ' this.$init = function () {',
  19470. ' this.Size = 0;',
  19471. ' };',
  19472. ' this.$final = function () {',
  19473. ' };',
  19474. ' this.Create = function (a) {',
  19475. ' this.$ancestorfunc(a);',
  19476. ' return this;',
  19477. ' };',
  19478. '});',
  19479. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19480. ' this.Create$1 = function (b) {',
  19481. ' $mod.TBird.Create.call(this, b);',
  19482. ' return this;',
  19483. ' };',
  19484. '});',
  19485. 'this.Bird = null;',
  19486. 'this.Eagle = null;',
  19487. '']),
  19488. LinesToStr([ // $mod.$main
  19489. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19490. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19491. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19492. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19493. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19494. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19495. '']));
  19496. end;
  19497. procedure TTestModule.TestExternalClass_PascalProperty;
  19498. begin
  19499. StartProgram(false);
  19500. Add('{$modeswitch externalclass}');
  19501. Add('type');
  19502. Add(' TJSElement = class;');
  19503. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19504. Add(' TJSElement = class external name ''ExtA''');
  19505. Add(' end;');
  19506. Add(' TControl = class(TJSElement)');
  19507. Add(' private');
  19508. Add(' FOnClick: TJSNotifyEvent;');
  19509. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19510. Add(' procedure Click(Sender: TJSElement);');
  19511. Add(' end;');
  19512. Add('procedure TControl.Click(Sender: TJSElement);');
  19513. Add('begin');
  19514. Add(' OnClick(Self);');
  19515. Add('end;');
  19516. Add('var');
  19517. Add(' Ctrl: TControl;');
  19518. Add('begin');
  19519. Add(' Ctrl.OnClick:[email protected];');
  19520. Add(' Ctrl.OnClick(Ctrl);');
  19521. ConvertProgram;
  19522. CheckSource('TestExternalClass_PascalProperty',
  19523. LinesToStr([ // statements
  19524. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19525. ' this.$init = function () {',
  19526. ' this.FOnClick = null;',
  19527. ' };',
  19528. ' this.$final = function () {',
  19529. ' this.FOnClick = undefined;',
  19530. ' };',
  19531. ' this.Click = function (Sender) {',
  19532. ' this.FOnClick(this);',
  19533. ' };',
  19534. '});',
  19535. 'this.Ctrl = null;',
  19536. '']),
  19537. LinesToStr([ // $mod.$main
  19538. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19539. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19540. '']));
  19541. end;
  19542. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19543. begin
  19544. StartProgram(false);
  19545. Add([
  19546. '{$modeswitch externalclass}',
  19547. 'type',
  19548. ' IUnknown = interface end;',
  19549. ' TObject = class',
  19550. ' end;',
  19551. ' TChild = class',
  19552. ' end;',
  19553. ' TExtRootA = class external name ''ExtRootA''',
  19554. ' end;',
  19555. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19556. ' end;',
  19557. ' TExtRootB = class external name ''ExtRootB''',
  19558. ' end;',
  19559. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19560. ' end;',
  19561. ' TExtString = class external name ''String''',
  19562. ' function charAt(aIndex : NativeInt) : string;',
  19563. ' end;',
  19564. 'var',
  19565. ' Obj: TObject;',
  19566. ' Child: TChild;',
  19567. ' RootA: TExtRootA;',
  19568. ' ChildA: TExtChildA;',
  19569. ' RootB: TExtRootB;',
  19570. ' ChildB: TExtChildB;',
  19571. ' i: IUnknown;',
  19572. ' s: string;',
  19573. ' v: jsvalue;',
  19574. 'begin',
  19575. ' obj:=tobject(roota);',
  19576. ' obj:=tobject(childa);',
  19577. ' child:=tchild(tobject(roota));',
  19578. ' roota:=textroota(obj);',
  19579. ' roota:=textroota(child);',
  19580. ' roota:=textroota(rootb);',
  19581. ' roota:=textroota(childb);',
  19582. ' childa:=textchilda(textroota(obj));',
  19583. ' roota:=TExtRootA(i);',
  19584. ' s:=TExtString(s).charAt(7);',
  19585. ' s:=TExtString(v).charAt(8);',
  19586. '']);
  19587. ConvertProgram;
  19588. CheckSource('TestExternalClass_TypeCastToRootClass',
  19589. LinesToStr([ // statements
  19590. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19591. 'rtl.createClass(this, "TObject", null, function () {',
  19592. ' this.$init = function () {',
  19593. ' };',
  19594. ' this.$final = function () {',
  19595. ' };',
  19596. '});',
  19597. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19598. '});',
  19599. 'this.Obj = null;',
  19600. 'this.Child = null;',
  19601. 'this.RootA = null;',
  19602. 'this.ChildA = null;',
  19603. 'this.RootB = null;',
  19604. 'this.ChildB = null;',
  19605. 'this.i = null;',
  19606. 'this.s = "";',
  19607. 'this.v = undefined;',
  19608. '']),
  19609. LinesToStr([ // $mod.$main
  19610. '$mod.Obj = $mod.RootA;',
  19611. '$mod.Obj = $mod.ChildA;',
  19612. '$mod.Child = $mod.RootA;',
  19613. '$mod.RootA = $mod.Obj;',
  19614. '$mod.RootA = $mod.Child;',
  19615. '$mod.RootA = $mod.RootB;',
  19616. '$mod.RootA = $mod.ChildB;',
  19617. '$mod.ChildA = $mod.Obj;',
  19618. '$mod.RootA = $mod.i;',
  19619. '$mod.s = $mod.s.charAt(7);',
  19620. '$mod.s = $mod.v.charAt(8);',
  19621. '']));
  19622. end;
  19623. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19624. begin
  19625. StartProgram(false);
  19626. Add([
  19627. '{$modeswitch externalclass}',
  19628. 'type',
  19629. ' IUnknown = interface end;',
  19630. ' IBird = interface(IUnknown) end;',
  19631. ' TClass = class of TObject;',
  19632. ' TObject = class',
  19633. ' end;',
  19634. ' TChild = class',
  19635. ' end;',
  19636. ' TJSObject = class external name ''Object''',
  19637. ' end;',
  19638. ' TRec = record end;',
  19639. 'var',
  19640. ' Obj: TObject;',
  19641. ' Child: TChild;',
  19642. ' i: IUnknown;',
  19643. ' Bird: IBird;',
  19644. ' j: TJSObject;',
  19645. ' r: TRec;',
  19646. ' c: TClass;',
  19647. 'begin',
  19648. ' j:=tjsobject(IUnknown);',
  19649. ' j:=tjsobject(IBird);',
  19650. ' j:=tjsobject(TObject);',
  19651. ' j:=tjsobject(TChild);',
  19652. ' j:=tjsobject(TRec);',
  19653. ' j:=tjsobject(Obj);',
  19654. ' j:=tjsobject(Child);',
  19655. ' j:=tjsobject(i);',
  19656. ' j:=tjsobject(Bird);',
  19657. ' j:=tjsobject(r);',
  19658. ' j:=tjsobject(c);',
  19659. '']);
  19660. ConvertProgram;
  19661. CheckSource('TestExternalClass_TypeCastToJSObject',
  19662. LinesToStr([ // statements
  19663. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19664. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19665. 'rtl.createClass(this, "TObject", null, function () {',
  19666. ' this.$init = function () {',
  19667. ' };',
  19668. ' this.$final = function () {',
  19669. ' };',
  19670. '});',
  19671. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19672. '});',
  19673. 'rtl.recNewT(this, "TRec", function () {',
  19674. ' this.$eq = function (b) {',
  19675. ' return true;',
  19676. ' };',
  19677. ' this.$assign = function (s) {',
  19678. ' return this;',
  19679. ' };',
  19680. '});',
  19681. 'this.Obj = null;',
  19682. 'this.Child = null;',
  19683. 'this.i = null;',
  19684. 'this.Bird = null;',
  19685. 'this.j = null;',
  19686. 'this.r = this.TRec.$new();',
  19687. 'this.c = null;',
  19688. '']),
  19689. LinesToStr([ // $mod.$main
  19690. '$mod.j = $mod.IUnknown;',
  19691. '$mod.j = $mod.IBird;',
  19692. '$mod.j = $mod.TObject;',
  19693. '$mod.j = $mod.TChild;',
  19694. '$mod.j = $mod.TRec;',
  19695. '$mod.j = $mod.Obj;',
  19696. '$mod.j = $mod.Child;',
  19697. '$mod.j = $mod.i;',
  19698. '$mod.j = $mod.Bird;',
  19699. '$mod.j = $mod.r;',
  19700. '$mod.j = $mod.c;',
  19701. '']));
  19702. end;
  19703. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19704. begin
  19705. StartProgram(false);
  19706. Add('{$modeswitch externalclass}');
  19707. Add('type');
  19708. Add(' TJSString = class external name ''String''');
  19709. Add(' class function fromCharCode() : string; varargs;');
  19710. Add(' function anchor(const aName : string) : string;');
  19711. Add(' end;');
  19712. Add('var');
  19713. Add(' s: string;');
  19714. Add('begin');
  19715. Add(' s:=TJSString.fromCharCode(65,66);');
  19716. Add(' s:=TJSString(s).anchor(s);');
  19717. Add(' s:=TJSString(''foo'').anchor(s);');
  19718. ConvertProgram;
  19719. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19720. LinesToStr([ // statements
  19721. 'this.s = "";',
  19722. '']),
  19723. LinesToStr([ // $mod.$main
  19724. '$mod.s = String.fromCharCode(65, 66);',
  19725. '$mod.s = $mod.s.anchor($mod.s);',
  19726. '$mod.s = "foo".anchor($mod.s);',
  19727. '']));
  19728. end;
  19729. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19730. begin
  19731. StartProgram(false);
  19732. Add([
  19733. '{$modeswitch externalclass}',
  19734. 'type',
  19735. ' TJSObject = class external name ''Object'' end;',
  19736. ' TJSFunction = class external name ''Function''',
  19737. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19738. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19739. ' end;',
  19740. ' TObject = class',
  19741. ' procedure DoIt(i: longint);',
  19742. ' end;',
  19743. ' TFuncInt = function(o: TObject): longint;',
  19744. 'function GetIt(o: TObject): longint;',
  19745. ' procedure Sub; begin end;',
  19746. 'var',
  19747. ' f: TJSFunction;',
  19748. ' fi: TFuncInt;',
  19749. 'begin',
  19750. ' fi:=TFuncInt(f);',
  19751. ' f:=TJSFunction(fi);',
  19752. ' f:=TJSFunction(@GetIt);',
  19753. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19754. ' f:=TJSFunction(@Sub);',
  19755. ' f:=TJSFunction(@o.doit);',
  19756. ' f:=TJSFunction(fi).bind(nil,4)',
  19757. 'end;',
  19758. 'procedure TObject.DoIt(i: longint);',
  19759. ' procedure Sub; begin end;',
  19760. 'var f: TJSFunction;',
  19761. 'begin',
  19762. ' f:=TJSFunction(@DoIt);',
  19763. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19764. ' f:=TJSFunction(@Sub);',
  19765. ' f:=TJSFunction(@GetIt);',
  19766. 'end;',
  19767. 'begin']);
  19768. ConvertProgram;
  19769. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19770. LinesToStr([ // statements
  19771. 'rtl.createClass(this, "TObject", null, function () {',
  19772. ' this.$init = function () {',
  19773. ' };',
  19774. ' this.$final = function () {',
  19775. ' };',
  19776. ' this.DoIt = function (i) {',
  19777. ' var $Self = this;',
  19778. ' function Sub() {',
  19779. ' };',
  19780. ' var f = null;',
  19781. ' f = this.DoIt;',
  19782. ' f = this.DoIt.bind(null, 13);',
  19783. ' f = Sub;',
  19784. ' f = $mod.GetIt;',
  19785. ' };',
  19786. '});',
  19787. 'this.GetIt = function (o) {',
  19788. ' var Result = 0;',
  19789. ' function Sub() {',
  19790. ' };',
  19791. ' var f = null;',
  19792. ' var fi = null;',
  19793. ' fi = f;',
  19794. ' f = fi;',
  19795. ' f = $mod.GetIt;',
  19796. ' f = $mod.GetIt.bind(null, 3);',
  19797. ' f = Sub;',
  19798. ' f = $mod.TObject.DoIt;',
  19799. ' f = fi.bind(null, 4);',
  19800. ' return Result;',
  19801. '};',
  19802. '']),
  19803. LinesToStr([ // $mod.$main
  19804. '']));
  19805. end;
  19806. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19807. begin
  19808. StartProgram(false);
  19809. Add([
  19810. '{$mode delphi}',
  19811. '{$modeswitch externalclass}',
  19812. 'type',
  19813. ' TJSObject = class external name ''Object'' end;',
  19814. ' TJSWindow = class external name ''Window''(TJSObject)',
  19815. ' procedure Open;',
  19816. ' end;',
  19817. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19818. ' procedure Execute;',
  19819. ' end;',
  19820. 'procedure Fly;',
  19821. 'var',
  19822. ' w: TJSWindow;',
  19823. ' e: TJSEventTarget;',
  19824. 'begin',
  19825. ' w:=TJSWindow(e);',
  19826. ' e:=TJSEventTarget(w);',
  19827. 'end;',
  19828. 'begin']);
  19829. ConvertProgram;
  19830. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19831. LinesToStr([ // statements
  19832. 'this.Fly = function () {',
  19833. ' var w = null;',
  19834. ' var e = null;',
  19835. ' w = e;',
  19836. ' e = w;',
  19837. '};',
  19838. '']),
  19839. LinesToStr([ // $mod.$main
  19840. '']));
  19841. end;
  19842. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19843. begin
  19844. StartProgram(false);
  19845. Add('{$modeswitch externalclass}');
  19846. Add('type');
  19847. Add(' TJSString = class external name ''String''');
  19848. Add(' class function fromCharCode() : string; varargs;');
  19849. Add(' end;');
  19850. Add('var');
  19851. Add(' s: string;');
  19852. Add(' sObj: TJSString;');
  19853. Add('begin');
  19854. Add(' s:=sObj.fromCharCode(65,66);');
  19855. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19856. nExternalClassInstanceCannotAccessStaticX);
  19857. ConvertProgram;
  19858. end;
  19859. procedure TTestModule.TestExternalClass_BracketAccessor;
  19860. begin
  19861. StartProgram(false);
  19862. Add([
  19863. '{$modeswitch externalclass}',
  19864. 'type',
  19865. ' TJSArray = class external name ''Array2''',
  19866. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19867. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19868. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19869. ' end;',
  19870. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19871. 'begin end;',
  19872. 'var',
  19873. ' Arr: tjsarray;',
  19874. ' s: string;',
  19875. ' i: longint;',
  19876. ' v: jsvalue;',
  19877. 'begin',
  19878. ' v:=arr[0];',
  19879. ' v:=arr.items[1];',
  19880. ' arr[2]:=s;',
  19881. ' arr.items[3]:=s;',
  19882. ' arr[4]:=i;',
  19883. ' arr[5]:=arr[6];',
  19884. ' arr.items[7]:=arr.items[8];',
  19885. ' with arr do items[9]:=items[10];',
  19886. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19887. ' with arr do begin',
  19888. ' v:=GetItems(14);',
  19889. ' setitems(15,16);',
  19890. ' end;',
  19891. ' v:=test1.arr.items[17];',
  19892. ' test1.arr.items[18]:=v;',
  19893. '']);
  19894. ConvertProgram;
  19895. CheckSource('TestExternalClass_BracketAccessor',
  19896. LinesToStr([ // statements
  19897. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19898. '};',
  19899. 'this.Arr = null;',
  19900. 'this.s = "";',
  19901. 'this.i = 0;',
  19902. 'this.v = undefined;',
  19903. '']),
  19904. LinesToStr([ // $mod.$main
  19905. '$mod.v = $mod.Arr[0];',
  19906. '$mod.v = $mod.Arr[1];',
  19907. '$mod.Arr[2] = $mod.s;',
  19908. '$mod.Arr[3] = $mod.s;',
  19909. '$mod.Arr[4] = $mod.i;',
  19910. '$mod.Arr[5] = $mod.Arr[6];',
  19911. '$mod.Arr[7] = $mod.Arr[8];',
  19912. 'var $with = $mod.Arr;',
  19913. '$with[9] = $with[10];',
  19914. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19915. ' a: 9,',
  19916. ' p: $mod.Arr,',
  19917. ' get: function () {',
  19918. ' return this.p[this.a];',
  19919. ' },',
  19920. ' set: function (v) {',
  19921. ' this.p[this.a] = v;',
  19922. ' }',
  19923. '}, {',
  19924. ' a: 10,',
  19925. ' p: $mod.Arr,',
  19926. ' get: function () {',
  19927. ' return this.p[this.a];',
  19928. ' },',
  19929. ' set: function (v) {',
  19930. ' this.p[this.a] = v;',
  19931. ' }',
  19932. '});',
  19933. 'var $with1 = $mod.Arr;',
  19934. '$mod.v = $with1[14];',
  19935. '$with1[15] = 16;',
  19936. '$mod.v = $mod.Arr[17];',
  19937. '$mod.Arr[18] = $mod.v;',
  19938. '']));
  19939. end;
  19940. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19941. begin
  19942. StartProgram(false);
  19943. Add([
  19944. '{$modeswitch externalclass}',
  19945. 'type',
  19946. ' TJSArray = class external name ''Array2''',
  19947. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19948. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19949. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19950. ' end;',
  19951. ' TMyArr = class(TJSArray)',
  19952. ' procedure DoIt;',
  19953. ' end;',
  19954. 'procedure tmyarr.DoIt;',
  19955. 'begin',
  19956. ' Items[1]:=Items[2];',
  19957. ' SetItems(3,getItems(4));',
  19958. 'end;',
  19959. 'var',
  19960. ' Arr: tmyarr;',
  19961. ' s: string;',
  19962. ' i: longint;',
  19963. ' v: jsvalue;',
  19964. 'begin',
  19965. ' v:=arr[0];',
  19966. ' v:=arr.items[1];',
  19967. ' arr[2]:=s;',
  19968. ' arr.items[3]:=s;',
  19969. ' arr[4]:=i;',
  19970. ' arr[5]:=arr[6];',
  19971. ' arr.items[7]:=arr.items[8];',
  19972. ' with arr do items[9]:=items[10];',
  19973. ' with arr do begin',
  19974. ' v:=GetItems(14);',
  19975. ' setitems(15,16);',
  19976. ' end;',
  19977. '']);
  19978. ConvertProgram;
  19979. CheckSource('TestExternalClass_BracketAccessor_Call',
  19980. LinesToStr([ // statements
  19981. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19982. ' this.$init = function () {',
  19983. ' };',
  19984. ' this.$final = function () {',
  19985. ' };',
  19986. ' this.DoIt = function () {',
  19987. ' this[1] = this[2];',
  19988. ' this[3] = this[4];',
  19989. ' };',
  19990. '});',
  19991. 'this.Arr = null;',
  19992. 'this.s = "";',
  19993. 'this.i = 0;',
  19994. 'this.v = undefined;',
  19995. '']),
  19996. LinesToStr([ // $mod.$main
  19997. '$mod.v = $mod.Arr[0];',
  19998. '$mod.v = $mod.Arr[1];',
  19999. '$mod.Arr[2] = $mod.s;',
  20000. '$mod.Arr[3] = $mod.s;',
  20001. '$mod.Arr[4] = $mod.i;',
  20002. '$mod.Arr[5] = $mod.Arr[6];',
  20003. '$mod.Arr[7] = $mod.Arr[8];',
  20004. 'var $with = $mod.Arr;',
  20005. '$with[9] = $with[10];',
  20006. 'var $with1 = $mod.Arr;',
  20007. '$mod.v = $with1[14];',
  20008. '$with1[15] = 16;',
  20009. '']));
  20010. end;
  20011. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  20012. begin
  20013. StartProgram(false);
  20014. Add('{$modeswitch externalclass}');
  20015. Add('type');
  20016. Add(' TJSArray = class external name ''Array2''');
  20017. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  20018. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  20019. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  20020. Add(' end;');
  20021. Add('begin');
  20022. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  20023. nBracketAccessorOfExternalClassMustHaveOneParameter);
  20024. ConvertProgram;
  20025. end;
  20026. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  20027. begin
  20028. StartProgram(false);
  20029. Add('{$modeswitch externalclass}');
  20030. Add('type');
  20031. Add(' TJSArray = class external name ''Array2''');
  20032. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20033. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  20034. Add(' end;');
  20035. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  20036. Add('begin end;');
  20037. Add('var');
  20038. Add(' Arr: tjsarray;');
  20039. Add(' v: jsvalue;');
  20040. Add('begin');
  20041. Add(' v:=arr[0];');
  20042. Add(' v:=arr.items[1];');
  20043. Add(' with arr do v:=items[2];');
  20044. Add(' doit(arr[3],arr[4]);');
  20045. ConvertProgram;
  20046. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  20047. LinesToStr([ // statements
  20048. 'this.DoIt = function (vI, vJ) {',
  20049. '};',
  20050. 'this.Arr = null;',
  20051. 'this.v = undefined;',
  20052. '']),
  20053. LinesToStr([ // $mod.$main
  20054. '$mod.v = $mod.Arr[0];',
  20055. '$mod.v = $mod.Arr[1];',
  20056. 'var $with = $mod.Arr;',
  20057. '$mod.v = $with[2];',
  20058. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20059. '']));
  20060. end;
  20061. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20062. begin
  20063. StartProgram(false);
  20064. Add('{$modeswitch externalclass}');
  20065. Add('type');
  20066. Add(' TJSArray = class external name ''Array2''');
  20067. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20068. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20069. Add(' end;');
  20070. Add('var');
  20071. Add(' Arr: tjsarray;');
  20072. Add(' s: string;');
  20073. Add(' i: longint;');
  20074. Add(' v: jsvalue;');
  20075. Add('begin');
  20076. Add(' arr[2]:=s;');
  20077. Add(' arr.items[3]:=s;');
  20078. Add(' arr[4]:=i;');
  20079. Add(' with arr do items[5]:=i;');
  20080. ConvertProgram;
  20081. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20082. LinesToStr([ // statements
  20083. 'this.Arr = null;',
  20084. 'this.s = "";',
  20085. 'this.i = 0;',
  20086. 'this.v = undefined;',
  20087. '']),
  20088. LinesToStr([ // $mod.$main
  20089. '$mod.Arr[2] = $mod.s;',
  20090. '$mod.Arr[3] = $mod.s;',
  20091. '$mod.Arr[4] = $mod.i;',
  20092. 'var $with = $mod.Arr;',
  20093. '$with[5] = $mod.i;',
  20094. '']));
  20095. end;
  20096. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20097. begin
  20098. StartProgram(false);
  20099. Add('{$modeswitch externalclass}');
  20100. Add('type');
  20101. Add(' TJSArray = class external name ''Array2''');
  20102. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20103. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20104. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20105. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20106. Add(' end;');
  20107. Add('var');
  20108. Add(' Arr: tjsarray;');
  20109. Add(' s: string;');
  20110. Add(' i: longint;');
  20111. Add(' v: jsvalue;');
  20112. Add('begin');
  20113. Add(' arr[2]:=s;');
  20114. Add(' arr.items[3]:=s;');
  20115. Add(' arr.numbers[4]:=i;');
  20116. Add(' with arr do items[5]:=i;');
  20117. Add(' with arr do numbers[6]:=i;');
  20118. ConvertProgram;
  20119. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20120. LinesToStr([ // statements
  20121. 'this.Arr = null;',
  20122. 'this.s = "";',
  20123. 'this.i = 0;',
  20124. 'this.v = undefined;',
  20125. '']),
  20126. LinesToStr([ // $mod.$main
  20127. '$mod.Arr[2] = $mod.s;',
  20128. '$mod.Arr[3] = $mod.s;',
  20129. '$mod.Arr[4] = $mod.i;',
  20130. 'var $with = $mod.Arr;',
  20131. '$with[5] = $mod.i;',
  20132. 'var $with1 = $mod.Arr;',
  20133. '$with1[6] = $mod.i;',
  20134. '']));
  20135. end;
  20136. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20137. begin
  20138. StartProgram(false);
  20139. Add('{$modeswitch externalclass}');
  20140. Add('type');
  20141. Add(' TJSArray = class external name ''Array2''');
  20142. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20143. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20144. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20145. Add(' end;');
  20146. Add('var');
  20147. Add(' Arr: tjsarray;');
  20148. Add(' i: longint;');
  20149. Add(' IntArr: array of longint;');
  20150. Add(' v: jsvalue;');
  20151. Add('begin');
  20152. Add(' v:=arr.items[i];');
  20153. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20154. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20155. ConvertProgram;
  20156. CheckSource('TestExternalClass_BracketAccessor_Index',
  20157. LinesToStr([ // statements
  20158. 'this.Arr = null;',
  20159. 'this.i = 0;',
  20160. 'this.IntArr = [];',
  20161. 'this.v = undefined;',
  20162. '']),
  20163. LinesToStr([ // $mod.$main
  20164. '$mod.v = $mod.Arr[$mod.i];',
  20165. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20166. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20167. '']));
  20168. end;
  20169. procedure TTestModule.TestExternalClass_ForInJSObject;
  20170. begin
  20171. StartProgram(false);
  20172. Add([
  20173. '{$modeswitch externalclass}',
  20174. 'type',
  20175. ' TJSObject = class external name ''Object''',
  20176. ' end;',
  20177. 'var',
  20178. ' o: TJSObject;',
  20179. ' key: string;',
  20180. 'begin',
  20181. ' for key in o do',
  20182. ' if key=''abc'' then ;',
  20183. '']);
  20184. ConvertProgram;
  20185. CheckSource('TestExternalClass_ForInJSObject',
  20186. LinesToStr([ // statements
  20187. 'this.o = null;',
  20188. 'this.key = "";',
  20189. '']),
  20190. LinesToStr([ // $mod.$main
  20191. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20192. '']));
  20193. end;
  20194. procedure TTestModule.TestExternalClass_ForInJSArray;
  20195. begin
  20196. StartProgram(false);
  20197. Add([
  20198. '{$modeswitch externalclass}',
  20199. 'type',
  20200. ' TJSInt8Array = class external name ''Int8Array''',
  20201. ' private',
  20202. ' flength: NativeInt external name ''length'';',
  20203. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20204. ' public',
  20205. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20206. ' property Length: NativeInt read flength;',
  20207. ' end;',
  20208. 'var',
  20209. ' a: TJSInt8Array;',
  20210. ' value: shortint;',
  20211. 'begin',
  20212. ' for value in a do',
  20213. ' if value=3 then ;',
  20214. '']);
  20215. ConvertProgram;
  20216. CheckSource('TestExternalClass_ForInJSArray',
  20217. LinesToStr([ // statements
  20218. 'this.a = null;',
  20219. 'this.value = 0;',
  20220. '']),
  20221. LinesToStr([ // $mod.$main
  20222. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20223. ' $mod.value = $in[$l];',
  20224. ' if ($mod.value === 3) ;',
  20225. '};',
  20226. '']));
  20227. end;
  20228. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20229. begin
  20230. AddModuleWithIntfImplSrc('unit2.pas',
  20231. LinesToStr([
  20232. '{$modeswitch externalclass}',
  20233. 'type',
  20234. ' TJSBufferSource = class external name ''BufferSource''',
  20235. ' end;',
  20236. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20237. '']),
  20238. '');
  20239. AddModuleWithIntfImplSrc('unit3.pas',
  20240. LinesToStr([
  20241. '{$modeswitch externalclass}',
  20242. 'type',
  20243. ' TJSBufferSource = class external name ''BufferSource''',
  20244. ' end;',
  20245. '']),
  20246. '');
  20247. StartUnit(true);
  20248. Add([
  20249. 'interface',
  20250. 'uses unit2, unit3;',
  20251. 'procedure DoSome(s: TJSBufferSource);',
  20252. 'implementation',
  20253. 'procedure DoSome(s: TJSBufferSource);',
  20254. 'begin',
  20255. ' DoIt(s);',
  20256. 'end;',
  20257. '']);
  20258. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20259. nIncompatibleTypeArgNo);
  20260. ConvertUnit;
  20261. end;
  20262. procedure TTestModule.TestExternalClass_NestedConstructor;
  20263. begin
  20264. StartProgram(false);
  20265. Add([
  20266. '{$modeswitch externalclass}',
  20267. 'type',
  20268. ' TJSObject = class external name ''Object''',
  20269. ' type TBird = class external name ''Bird''',
  20270. ' type TWing = class external name ''Wing''',
  20271. ' constructor New;',
  20272. ' constructor Create(w: word = 3);',
  20273. ' end;',
  20274. ' end;',
  20275. ' end;',
  20276. 'var',
  20277. ' w: TJSObject.TBird.TWing;',
  20278. 'begin',
  20279. ' w:=tjsobject.tbird.twing.new;',
  20280. ' w:=tjsobject.tbird.twing.new();',
  20281. ' w:=tjsobject.tbird.twing.create;',
  20282. ' w:=tjsobject.tbird.twing.create(4);',
  20283. ' with tjsobject do begin',
  20284. ' w:=tbird.twing.new;',
  20285. ' w:=tbird.twing.new();',
  20286. ' w:=tbird.twing.create;',
  20287. ' w:=tbird.twing.create(11);',
  20288. ' end;',
  20289. ' with tjsobject.tbird do begin',
  20290. ' w:=twing.new;',
  20291. ' w:=twing.new();',
  20292. ' w:=twing.create;',
  20293. ' w:=twing.create(21);',
  20294. ' end;',
  20295. ' with tjsobject.tbird.twing do begin',
  20296. ' w:=new;',
  20297. ' w:=new();',
  20298. ' w:=create;',
  20299. ' w:=create(31);',
  20300. ' end;',
  20301. '']);
  20302. ConvertProgram;
  20303. CheckSource('TestExternalClass_NestedConstructor',
  20304. LinesToStr([ // statements
  20305. 'this.w = null;',
  20306. '']),
  20307. LinesToStr([ // $mod.$main
  20308. '$mod.w = new Object.Bird.Wing();',
  20309. '$mod.w = new Object.Bird.Wing();',
  20310. '$mod.w = new Object.Bird.Wing.Create();',
  20311. '$mod.w = new Object.Bird.Wing.Create(4);',
  20312. '$mod.w = new Object.Bird.Wing();',
  20313. '$mod.w = new Object.Bird.Wing();',
  20314. '$mod.w = new Object.Bird.Wing.Create();',
  20315. '$mod.w = new Object.Bird.Wing.Create(11);',
  20316. 'var $with = Object.Bird;',
  20317. '$mod.w = new Object.Bird.Wing();',
  20318. '$mod.w = new Object.Bird.Wing();',
  20319. '$mod.w = new Object.Bird.Wing.Create();',
  20320. '$mod.w = new Object.Bird.Wing.Create(21);',
  20321. 'var $with1 = Object.Bird.Wing;',
  20322. '$mod.w = new $with1();',
  20323. '$mod.w = new $with1();',
  20324. '$mod.w = new Object.Bird.Wing.Create();',
  20325. '$mod.w = new Object.Bird.Wing.Create(31);',
  20326. '']));
  20327. end;
  20328. procedure TTestModule.TestClassInterface_Corba;
  20329. begin
  20330. StartProgram(false);
  20331. Add([
  20332. '{$interfaces corba}',
  20333. 'type',
  20334. ' IUnknown = interface;',
  20335. ' IUnknown = interface',
  20336. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20337. ' end;',
  20338. ' IInterface = IUnknown;',
  20339. ' IBird = interface(IInterface)',
  20340. ' function GetSize: longint;',
  20341. ' procedure SetSize(i: longint);',
  20342. ' property Size: longint read GetSize write SetSize;',
  20343. ' procedure DoIt(i: longint);',
  20344. ' end;',
  20345. ' TObject = class',
  20346. ' end;',
  20347. ' TBird = class(TObject,IBird)',
  20348. ' function GetSize: longint; virtual; abstract;',
  20349. ' procedure SetSize(i: longint); virtual; abstract;',
  20350. ' procedure DoIt(i: longint); virtual; abstract;',
  20351. ' end;',
  20352. 'var',
  20353. ' BirdIntf: IBird;',
  20354. 'begin',
  20355. ' BirdIntf.Size:=BirdIntf.Size;',
  20356. '']);
  20357. ConvertProgram;
  20358. CheckSource('TestClassInterface_Corba',
  20359. LinesToStr([ // statements
  20360. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20361. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20362. 'rtl.createClass(this, "TObject", null, function () {',
  20363. ' this.$init = function () {',
  20364. ' };',
  20365. ' this.$final = function () {',
  20366. ' };',
  20367. '});',
  20368. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20369. ' rtl.addIntf(this, $mod.IBird);',
  20370. '});',
  20371. 'this.BirdIntf = null;',
  20372. '']),
  20373. LinesToStr([ // $mod.$main
  20374. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20375. '']));
  20376. end;
  20377. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20378. begin
  20379. StartProgram(false);
  20380. Add([
  20381. '{$interfaces corba}',
  20382. 'type',
  20383. ' IUnknown = interface',
  20384. ' procedure DoIt; external name ''foo'';',
  20385. ' end;',
  20386. 'begin']);
  20387. SetExpectedParserError(
  20388. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20389. nParserNoFieldsAllowed);
  20390. ConvertProgram;
  20391. end;
  20392. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20393. begin
  20394. StartProgram(false);
  20395. Add([
  20396. '{$interfaces corba}',
  20397. 'type',
  20398. ' integer = longint;',
  20399. ' IUnknown = interface',
  20400. ' procedure DoIt(i: integer);',
  20401. ' procedure DoIt(s: string);',
  20402. ' end;',
  20403. ' IBird = interface(IUnknown)',
  20404. ' procedure DoIt(b: boolean); overload;',
  20405. ' end;',
  20406. ' TObject = class',
  20407. ' end;',
  20408. ' TBird = class(TObject,IBird)',
  20409. ' procedure DoIt(o: TObject);',
  20410. ' procedure DoIt(s: string);',
  20411. ' procedure DoIt(i: integer);',
  20412. ' procedure DoIt(b: boolean);',
  20413. ' end;',
  20414. 'procedure TBird.DoIt(o: TObject); begin end;',
  20415. 'procedure TBird.DoIt(s: string); begin end;',
  20416. 'procedure TBird.DoIt(i: integer); begin end;',
  20417. 'procedure TBird.DoIt(b: boolean); begin end;',
  20418. 'var',
  20419. ' BirdIntf: IBird;',
  20420. 'begin',
  20421. ' BirdIntf.DoIt(3);',
  20422. ' BirdIntf.DoIt(''abc'');',
  20423. ' BirdIntf.DoIt(true);',
  20424. '']);
  20425. ConvertProgram;
  20426. CheckSource('TestClassInterface_Overloads',
  20427. LinesToStr([ // statements
  20428. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20429. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20430. 'rtl.createClass(this, "TObject", null, function () {',
  20431. ' this.$init = function () {',
  20432. ' };',
  20433. ' this.$final = function () {',
  20434. ' };',
  20435. '});',
  20436. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20437. ' this.DoIt = function (o) {',
  20438. ' };',
  20439. ' this.DoIt$1 = function (s) {',
  20440. ' };',
  20441. ' this.DoIt$2 = function (i) {',
  20442. ' };',
  20443. ' this.DoIt$3 = function (b) {',
  20444. ' };',
  20445. ' rtl.addIntf(this, $mod.IBird, {',
  20446. ' DoIt$2: "DoIt$3",',
  20447. ' DoIt: "DoIt$2"',
  20448. ' });',
  20449. '});',
  20450. 'this.BirdIntf = null;',
  20451. '']),
  20452. LinesToStr([ // $mod.$main
  20453. '$mod.BirdIntf.DoIt(3);',
  20454. '$mod.BirdIntf.DoIt$1("abc");',
  20455. '$mod.BirdIntf.DoIt$2(true);',
  20456. '']));
  20457. end;
  20458. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20459. begin
  20460. StartProgram(false);
  20461. Add([
  20462. '{$interfaces corba}',
  20463. 'type',
  20464. ' IBird = interface',
  20465. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20466. ' end;',
  20467. ' IDog = interface',
  20468. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20469. ' end;',
  20470. ' TObject = class(IBird,IDog)',
  20471. ' end;',
  20472. 'begin']);
  20473. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20474. nDuplicateGUIDXInYZ);
  20475. ConvertProgram;
  20476. end;
  20477. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20478. begin
  20479. StartProgram(false);
  20480. Add([
  20481. '{$interfaces corba}',
  20482. 'type',
  20483. ' IAnimal = interface',
  20484. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20485. ' end;',
  20486. ' IBird = interface(IAnimal)',
  20487. ' end;',
  20488. ' IHawk = interface(IBird)',
  20489. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20490. ' end;',
  20491. 'begin']);
  20492. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20493. nDuplicateGUIDXInYZ);
  20494. ConvertProgram;
  20495. end;
  20496. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20497. begin
  20498. StartProgram(false);
  20499. Add([
  20500. '{$interfaces corba}',
  20501. 'type',
  20502. ' integer = longint;',
  20503. ' IUnknown = interface',
  20504. ' procedure DoIt(i: integer);',
  20505. ' end;',
  20506. ' IBird = interface',
  20507. ' procedure Fly(i: integer);',
  20508. ' end;',
  20509. ' TObject = class(IUnknown)',
  20510. ' procedure DoIt(i: integer);',
  20511. ' end;',
  20512. ' TBird = class(IBird)',
  20513. ' procedure Fly(i: integer);',
  20514. ' end;',
  20515. 'procedure TObject.DoIt(i: integer); begin end;',
  20516. 'procedure TBird.Fly(i: integer); begin end;',
  20517. 'begin',
  20518. '']);
  20519. ConvertProgram;
  20520. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20521. LinesToStr([ // statements
  20522. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20523. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20524. 'rtl.createClass(this, "TObject", null, function () {',
  20525. ' this.$init = function () {',
  20526. ' };',
  20527. ' this.$final = function () {',
  20528. ' };',
  20529. ' this.DoIt = function (i) {',
  20530. ' };',
  20531. ' rtl.addIntf(this, $mod.IUnknown);',
  20532. '});',
  20533. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20534. ' this.Fly = function (i) {',
  20535. ' };',
  20536. ' rtl.addIntf(this, $mod.IBird);',
  20537. ' rtl.addIntf(this, $mod.IUnknown);',
  20538. '});',
  20539. '']),
  20540. LinesToStr([ // $mod.$main
  20541. '']));
  20542. end;
  20543. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20544. begin
  20545. StartProgram(false);
  20546. Add([
  20547. '{$interfaces corba}',
  20548. 'type',
  20549. ' integer = longint;',
  20550. ' IBird = interface',
  20551. ' procedure DoIt(i: integer);',
  20552. ' end;',
  20553. ' TObject = class',
  20554. ' procedure DoIt(i: integer);',
  20555. ' end;',
  20556. ' TBird = class(IBird)',
  20557. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20558. ' end;',
  20559. 'procedure TObject.DoIt(i: integer); begin end;',
  20560. 'procedure TBird.DoIt(i: integer); begin end;',
  20561. 'begin',
  20562. '']);
  20563. ConvertProgram;
  20564. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20565. LinesToStr([ // statements
  20566. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20567. 'rtl.createClass(this, "TObject", null, function () {',
  20568. ' this.$init = function () {',
  20569. ' };',
  20570. ' this.$final = function () {',
  20571. ' };',
  20572. ' this.DoIt = function (i) {',
  20573. ' };',
  20574. '});',
  20575. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20576. ' this.DoIt$1 = function (i) {',
  20577. ' };',
  20578. ' rtl.addIntf(this, $mod.IBird, {',
  20579. ' DoIt: "DoIt$1"',
  20580. ' });',
  20581. '});',
  20582. '']),
  20583. LinesToStr([ // $mod.$main
  20584. '']));
  20585. end;
  20586. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20587. begin
  20588. StartProgram(false);
  20589. Add([
  20590. '{$interfaces corba}',
  20591. 'type',
  20592. ' IUnknown = interface',
  20593. ' procedure Walk(i: longint);',
  20594. ' end;',
  20595. ' IBird = interface(IUnknown)',
  20596. ' procedure Walk(b: boolean); overload;',
  20597. ' procedure Fly(s: string);',
  20598. ' end;',
  20599. ' TObject = class',
  20600. ' end;',
  20601. ' TBird = class(TObject,IBird)',
  20602. ' procedure IBird.Fly = Move;',
  20603. ' procedure IBird.Walk = Hop;',
  20604. ' procedure Hop(i: longint);',
  20605. ' procedure Move(s: string);',
  20606. ' procedure Hop(b: boolean);',
  20607. ' end;',
  20608. 'procedure TBird.Move(s: string); begin end;',
  20609. 'procedure TBird.Hop(i: longint); begin end;',
  20610. 'procedure TBird.Hop(b: boolean); begin end;',
  20611. 'var',
  20612. ' BirdIntf: IBird;',
  20613. 'begin',
  20614. ' BirdIntf.Walk(3);',
  20615. ' BirdIntf.Walk(true);',
  20616. ' BirdIntf.Fly(''abc'');',
  20617. '']);
  20618. ConvertProgram;
  20619. CheckSource('TestClassInterface_Corba_MethodResolution',
  20620. LinesToStr([ // statements
  20621. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20622. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20623. 'rtl.createClass(this, "TObject", null, function () {',
  20624. ' this.$init = function () {',
  20625. ' };',
  20626. ' this.$final = function () {',
  20627. ' };',
  20628. '});',
  20629. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20630. ' this.Hop = function (i) {',
  20631. ' };',
  20632. ' this.Move = function (s) {',
  20633. ' };',
  20634. ' this.Hop$1 = function (b) {',
  20635. ' };',
  20636. ' rtl.addIntf(this, $mod.IBird, {',
  20637. ' Walk$1: "Hop$1",',
  20638. ' Fly: "Move",',
  20639. ' Walk: "Hop"',
  20640. ' });',
  20641. '});',
  20642. 'this.BirdIntf = null;',
  20643. '']),
  20644. LinesToStr([ // $mod.$main
  20645. '$mod.BirdIntf.Walk(3);',
  20646. '$mod.BirdIntf.Walk$1(true);',
  20647. '$mod.BirdIntf.Fly("abc");',
  20648. '']));
  20649. end;
  20650. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20651. begin
  20652. StartProgram(false);
  20653. Add([
  20654. '{$interfaces com}',
  20655. 'type',
  20656. ' IUnknown = interface',
  20657. ' function _AddRef: longint;',
  20658. ' procedure Walk;',
  20659. ' end;',
  20660. ' IBird = interface end;',
  20661. ' IDog = interface end;',
  20662. ' TObject = class(IBird,IDog)',
  20663. ' function _AddRef: longint; virtual; abstract;',
  20664. ' procedure Walk; virtual; abstract;',
  20665. ' end;',
  20666. ' TBird = class(IUnknown)',
  20667. ' end;',
  20668. 'begin',
  20669. '']);
  20670. ConvertProgram;
  20671. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20672. LinesToStr([ // statements
  20673. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20674. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20675. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20676. 'rtl.createClass(this, "TObject", null, function () {',
  20677. ' this.$init = function () {',
  20678. ' };',
  20679. ' this.$final = function () {',
  20680. ' };',
  20681. ' rtl.addIntf(this, $mod.IBird);',
  20682. ' rtl.addIntf(this, $mod.IDog);',
  20683. '});',
  20684. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20685. ' rtl.addIntf(this, $mod.IUnknown);',
  20686. ' rtl.addIntf(this, $mod.IBird);',
  20687. ' rtl.addIntf(this, $mod.IDog);',
  20688. '});',
  20689. '']),
  20690. LinesToStr([ // $mod.$main
  20691. '']));
  20692. end;
  20693. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20694. begin
  20695. StartProgram(false);
  20696. Add([
  20697. '{$interfaces corba}',
  20698. 'type',
  20699. ' IUnknown = interface',
  20700. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20701. ' procedure Go;',
  20702. ' end;',
  20703. ' TObject = class(IUnknown)',
  20704. ' procedure Go; virtual; abstract;',
  20705. ' end;',
  20706. ' TBird = class',
  20707. ' procedure Go; override;',
  20708. ' end;',
  20709. ' TCat = class(TObject)',
  20710. ' procedure Go; override;',
  20711. ' end;',
  20712. ' TDog = class(TObject, IUnknown)',
  20713. ' procedure Go; override;',
  20714. ' end;',
  20715. 'procedure TBird.Go; begin end;',
  20716. 'procedure TCat.Go; begin end;',
  20717. 'procedure TDog.Go; begin end;',
  20718. 'begin',
  20719. '']);
  20720. ConvertProgram;
  20721. CheckSource('TestClassInterface_Corba_MethodOverride',
  20722. LinesToStr([ // statements
  20723. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20724. 'rtl.createClass(this, "TObject", null, function () {',
  20725. ' this.$init = function () {',
  20726. ' };',
  20727. ' this.$final = function () {',
  20728. ' };',
  20729. ' rtl.addIntf(this, $mod.IUnknown);',
  20730. '});',
  20731. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20732. ' this.Go = function () {',
  20733. ' };',
  20734. ' rtl.addIntf(this, $mod.IUnknown);',
  20735. '});',
  20736. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20737. ' this.Go = function () {',
  20738. ' };',
  20739. ' rtl.addIntf(this, $mod.IUnknown);',
  20740. '});',
  20741. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20742. ' this.Go = function () {',
  20743. ' };',
  20744. ' rtl.addIntf(this, $mod.IUnknown);',
  20745. '});',
  20746. '']),
  20747. LinesToStr([ // $mod.$main
  20748. '']));
  20749. end;
  20750. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20751. begin
  20752. StartProgram(false);
  20753. Add([
  20754. '{$interfaces corba}',
  20755. 'type',
  20756. ' IUnknown = interface',
  20757. ' end;',
  20758. ' IBird = interface(IUnknown)',
  20759. ' procedure Fly(s: string);',
  20760. ' end;',
  20761. ' IEagle = interface(IBird)',
  20762. ' end;',
  20763. ' IDove = interface(IBird)',
  20764. ' end;',
  20765. ' ISwallow = interface(IBird)',
  20766. ' end;',
  20767. ' TObject = class',
  20768. ' end;',
  20769. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20770. ' procedure Fly(s: string); virtual; abstract;',
  20771. ' end;',
  20772. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20773. ' FBirdIntf: IBird;',
  20774. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20775. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20776. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20777. ' FDoveObj: TBird;',
  20778. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20779. ' function GetSwallowObj: TBird; virtual; abstract;',
  20780. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20781. ' end;',
  20782. 'begin',
  20783. '']);
  20784. ConvertProgram;
  20785. CheckSource('TestClassInterface_Corba_Delegation',
  20786. LinesToStr([ // statements
  20787. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20788. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20789. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20790. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20791. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20792. 'rtl.createClass(this, "TObject", null, function () {',
  20793. ' this.$init = function () {',
  20794. ' };',
  20795. ' this.$final = function () {',
  20796. ' };',
  20797. '});',
  20798. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20799. ' rtl.addIntf(this, $mod.IBird);',
  20800. ' rtl.addIntf(this, $mod.IEagle);',
  20801. ' rtl.addIntf(this, $mod.IDove);',
  20802. ' rtl.addIntf(this, $mod.ISwallow);',
  20803. '});',
  20804. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20805. ' this.$init = function () {',
  20806. ' $mod.TObject.$init.call(this);',
  20807. ' this.FBirdIntf = null;',
  20808. ' this.FDoveObj = null;',
  20809. ' };',
  20810. ' this.$final = function () {',
  20811. ' this.FBirdIntf = undefined;',
  20812. ' this.FDoveObj = undefined;',
  20813. ' $mod.TObject.$final.call(this);',
  20814. ' };',
  20815. ' this.$intfmaps = {',
  20816. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20817. ' return this.FBirdIntf;',
  20818. ' },',
  20819. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20820. ' return this.GetEagleIntf();',
  20821. ' },',
  20822. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20823. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20824. ' },',
  20825. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20826. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20827. ' }',
  20828. ' };',
  20829. '});',
  20830. '']),
  20831. LinesToStr([ // $mod.$main
  20832. '']));
  20833. end;
  20834. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20835. begin
  20836. StartProgram(false);
  20837. Add([
  20838. '{$interfaces corba}',
  20839. 'type',
  20840. ' IUnknown = interface',
  20841. ' end;',
  20842. ' IBird = interface(IUnknown)',
  20843. ' procedure Fly(s: string);',
  20844. ' end;',
  20845. ' IEagle = interface(IBird)',
  20846. ' end;',
  20847. ' IDove = interface(IBird)',
  20848. ' end;',
  20849. ' ISwallow = interface(IBird)',
  20850. ' end;',
  20851. ' TObject = class',
  20852. ' end;',
  20853. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20854. ' procedure Fly(s: string); virtual; abstract;',
  20855. ' end;',
  20856. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20857. ' private',
  20858. ' class var FBirdIntf: IBird;',
  20859. ' class var FDoveObj: TBird;',
  20860. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20861. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20862. ' protected',
  20863. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20864. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20865. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20866. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20867. ' end;',
  20868. 'begin',
  20869. '']);
  20870. ConvertProgram;
  20871. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20872. LinesToStr([ // statements
  20873. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20874. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20875. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20876. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20877. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20878. 'rtl.createClass(this, "TObject", null, function () {',
  20879. ' this.$init = function () {',
  20880. ' };',
  20881. ' this.$final = function () {',
  20882. ' };',
  20883. '});',
  20884. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20885. ' rtl.addIntf(this, $mod.IBird);',
  20886. ' rtl.addIntf(this, $mod.IEagle);',
  20887. ' rtl.addIntf(this, $mod.IDove);',
  20888. ' rtl.addIntf(this, $mod.ISwallow);',
  20889. '});',
  20890. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20891. ' this.FBirdIntf = null;',
  20892. ' this.FDoveObj = null;',
  20893. ' this.$intfmaps = {',
  20894. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20895. ' return this.FBirdIntf;',
  20896. ' },',
  20897. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20898. ' return this.GetEagleIntf();',
  20899. ' },',
  20900. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20901. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20902. ' },',
  20903. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20904. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20905. ' }',
  20906. ' };',
  20907. '});',
  20908. '']),
  20909. LinesToStr([ // $mod.$main
  20910. '']));
  20911. end;
  20912. procedure TTestModule.TestClassInterface_Corba_Operators;
  20913. begin
  20914. StartProgram(false);
  20915. Add([
  20916. '{$interfaces corba}',
  20917. 'type',
  20918. ' IUnknown = interface',
  20919. ' end;',
  20920. ' IBird = interface(IUnknown)',
  20921. ' function GetItems(Index: longint): longint;',
  20922. ' procedure SetItems(Index: longint; Value: longint);',
  20923. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20924. ' end;',
  20925. ' TObject = class',
  20926. ' end;',
  20927. ' TBird = class(TObject,IBird)',
  20928. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20929. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20930. ' end;',
  20931. 'var',
  20932. ' IntfVar: IBird = nil;',
  20933. ' IntfVar2: IBird;',
  20934. ' ObjVar: TBird;',
  20935. ' v: JSValue;',
  20936. 'begin',
  20937. ' IntfVar:=nil;',
  20938. ' IntfVar[3]:=IntfVar[4];',
  20939. ' if Assigned(IntfVar) then ;',
  20940. ' IntfVar:=IntfVar2;',
  20941. ' IntfVar:=ObjVar;',
  20942. ' if IntfVar=IntfVar2 then ;',
  20943. ' if IntfVar<>IntfVar2 then ;',
  20944. ' if IntfVar is IBird then ;',
  20945. ' if IntfVar is TBird then ;',
  20946. ' if ObjVar is IBird then ;',
  20947. ' IntfVar:=IntfVar2 as IBird;',
  20948. ' ObjVar:=IntfVar2 as TBird;',
  20949. ' IntfVar:=ObjVar as IBird;',
  20950. ' IntfVar:=IBird(IntfVar2);',
  20951. ' ObjVar:=TBird(IntfVar);',
  20952. ' IntfVar:=IBird(ObjVar);',
  20953. ' v:=IntfVar;',
  20954. ' IntfVar:=IBird(v);',
  20955. ' if v is IBird then ;',
  20956. ' v:=JSValue(IntfVar);',
  20957. ' v:=IBird;',
  20958. '']);
  20959. ConvertProgram;
  20960. CheckSource('TestClassInterface_Corba_Operators',
  20961. LinesToStr([ // statements
  20962. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20963. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20964. 'rtl.createClass(this, "TObject", null, function () {',
  20965. ' this.$init = function () {',
  20966. ' };',
  20967. ' this.$final = function () {',
  20968. ' };',
  20969. '});',
  20970. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20971. ' rtl.addIntf(this, $mod.IBird);',
  20972. '});',
  20973. 'this.IntfVar = null;',
  20974. 'this.IntfVar2 = null;',
  20975. 'this.ObjVar = null;',
  20976. 'this.v = undefined;',
  20977. '']),
  20978. LinesToStr([ // $mod.$main
  20979. '$mod.IntfVar = null;',
  20980. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20981. 'if ($mod.IntfVar != null) ;',
  20982. '$mod.IntfVar = $mod.IntfVar2;',
  20983. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20984. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20985. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20986. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20987. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20988. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20989. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20990. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20991. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20992. '$mod.IntfVar = $mod.IntfVar2;',
  20993. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20994. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20995. '$mod.v = $mod.IntfVar;',
  20996. '$mod.IntfVar = rtl.getObject($mod.v);',
  20997. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20998. '$mod.v = $mod.IntfVar;',
  20999. '$mod.v = $mod.IBird;',
  21000. '']));
  21001. end;
  21002. procedure TTestModule.TestClassInterface_Corba_Args;
  21003. begin
  21004. StartProgram(false);
  21005. Add([
  21006. '{$interfaces corba}',
  21007. 'type',
  21008. ' IUnknown = interface',
  21009. ' end;',
  21010. ' IBird = interface(IUnknown)',
  21011. ' end;',
  21012. ' TObject = class',
  21013. ' end;',
  21014. ' TBird = class(TObject,IBird)',
  21015. ' end;',
  21016. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  21017. 'begin',
  21018. ' DoIt(i,i,i);',
  21019. 'end;',
  21020. 'procedure Change(var i: IBird; out j: IBird);',
  21021. 'begin',
  21022. ' DoIt(i,i,i);',
  21023. ' Change(i,i);',
  21024. 'end;',
  21025. 'var',
  21026. ' i: IBird;',
  21027. ' o: TBird;',
  21028. 'begin',
  21029. ' DoIt(i,i,i);',
  21030. ' Change(i,i);',
  21031. ' DoIt(o,o,o);',
  21032. '']);
  21033. ConvertProgram;
  21034. CheckSource('TestClassInterface_Corba_Args',
  21035. LinesToStr([ // statements
  21036. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21037. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  21038. 'rtl.createClass(this, "TObject", null, function () {',
  21039. ' this.$init = function () {',
  21040. ' };',
  21041. ' this.$final = function () {',
  21042. ' };',
  21043. '});',
  21044. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21045. ' rtl.addIntf(this, $mod.IBird);',
  21046. '});',
  21047. 'this.DoIt = function (u, i, j) {',
  21048. ' $mod.DoIt({',
  21049. ' get: function () {',
  21050. ' return i;',
  21051. ' },',
  21052. ' set: function (v) {',
  21053. ' i = v;',
  21054. ' }',
  21055. ' }, i, i);',
  21056. '};',
  21057. 'this.Change = function (i, j) {',
  21058. ' $mod.DoIt(i, i.get(), i.get());',
  21059. ' $mod.Change(i, i);',
  21060. '};',
  21061. 'this.i = null;',
  21062. 'this.o = null;',
  21063. '']),
  21064. LinesToStr([ // $mod.$main
  21065. '$mod.DoIt({',
  21066. ' p: $mod,',
  21067. ' get: function () {',
  21068. ' return this.p.i;',
  21069. ' },',
  21070. ' set: function (v) {',
  21071. ' this.p.i = v;',
  21072. ' }',
  21073. '}, $mod.i, $mod.i);',
  21074. '$mod.Change({',
  21075. ' p: $mod,',
  21076. ' get: function () {',
  21077. ' return this.p.i;',
  21078. ' },',
  21079. ' set: function (v) {',
  21080. ' this.p.i = v;',
  21081. ' }',
  21082. '}, {',
  21083. ' p: $mod,',
  21084. ' get: function () {',
  21085. ' return this.p.i;',
  21086. ' },',
  21087. ' set: function (v) {',
  21088. ' this.p.i = v;',
  21089. ' }',
  21090. '});',
  21091. '$mod.DoIt({',
  21092. ' p: $mod,',
  21093. ' get: function () {',
  21094. ' return this.p.o;',
  21095. ' },',
  21096. ' set: function (v) {',
  21097. ' this.p.o = v;',
  21098. ' }',
  21099. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21100. '']));
  21101. end;
  21102. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21103. begin
  21104. StartProgram(false);
  21105. Add([
  21106. '{$interfaces corba}',
  21107. 'type',
  21108. ' IUnknown = interface end;',
  21109. ' TObject = class',
  21110. ' Id: longint;',
  21111. ' end;',
  21112. ' IEnumerator = interface(IUnknown)',
  21113. ' function GetCurrent: TObject;',
  21114. ' function MoveNext: Boolean;',
  21115. ' property Current: TObject read GetCurrent;',
  21116. ' end;',
  21117. ' IEnumerable = interface(IUnknown)',
  21118. ' function GetEnumerator: IEnumerator;',
  21119. ' end;',
  21120. 'var',
  21121. ' o: TObject;',
  21122. ' i: IEnumerable;',
  21123. 'begin',
  21124. ' for o in i do o.Id:=3;',
  21125. '']);
  21126. ConvertProgram;
  21127. CheckSource('TestClassInterface_Corba_ForIn',
  21128. LinesToStr([ // statements
  21129. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21130. 'rtl.createClass(this, "TObject", null, function () {',
  21131. ' this.$init = function () {',
  21132. ' this.Id = 0;',
  21133. ' };',
  21134. ' this.$final = function () {',
  21135. ' };',
  21136. '});',
  21137. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21138. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21139. 'this.o = null;',
  21140. 'this.i = null;',
  21141. '']),
  21142. LinesToStr([ // $mod.$main
  21143. 'var $in = $mod.i.GetEnumerator();',
  21144. 'while ($in.MoveNext()) {',
  21145. ' $mod.o = $in.GetCurrent();',
  21146. ' $mod.o.Id = 3;',
  21147. '};',
  21148. '']));
  21149. end;
  21150. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21151. begin
  21152. StartProgram(false);
  21153. Add([
  21154. '{$interfaces corba}',
  21155. 'type',
  21156. ' IUnknown = interface end;',
  21157. ' IBird = interface(IUnknown)',
  21158. ' function Fly(w: word): word;',
  21159. ' end;',
  21160. ' TBirdArray = array of IBird;',
  21161. 'var',
  21162. ' i: IBird;',
  21163. ' a: TBirdArray;',
  21164. 'begin',
  21165. ' SetLength(a,3);',
  21166. ' i:=a[1];',
  21167. ' a[2]:=i;',
  21168. ' for i in a do i.fly(3);',
  21169. '']);
  21170. ConvertProgram;
  21171. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21172. LinesToStr([ // statements
  21173. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21174. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21175. 'this.i = null;',
  21176. 'this.a = [];',
  21177. '']),
  21178. LinesToStr([ // $mod.$main
  21179. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21180. '$mod.i = $mod.a[1];',
  21181. '$mod.a[2] = $mod.i;',
  21182. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21183. ' $mod.i = $in[$l];',
  21184. ' $mod.i.Fly(3);',
  21185. '};',
  21186. '']));
  21187. end;
  21188. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21189. begin
  21190. StartProgram(false);
  21191. Add([
  21192. '{$interfaces com}',
  21193. 'type',
  21194. ' IUnknown = interface',
  21195. ' function _AddRef: longint;',
  21196. ' function _Release: longint;',
  21197. ' end;',
  21198. ' TObject = class(IUnknown)',
  21199. ' function _AddRef: longint; virtual; abstract;',
  21200. ' function _Release: longint; virtual; abstract;',
  21201. ' end;',
  21202. 'var',
  21203. ' i: IUnknown;',
  21204. 'procedure DoGlobal(o: TObject);',
  21205. 'begin',
  21206. ' i:=nil;',
  21207. ' i:=o;',
  21208. ' i:=i;',
  21209. 'end;',
  21210. 'procedure DoLocal(o: TObject);',
  21211. 'const k: IUnknown = nil;',
  21212. 'var j: IUnknown;',
  21213. 'begin',
  21214. ' k:=o;',
  21215. ' k:=i;',
  21216. ' j:=o;',
  21217. ' j:=i;',
  21218. 'end;',
  21219. 'var o: TObject;',
  21220. 'begin',
  21221. ' i:=nil;',
  21222. ' i:=o;',
  21223. '']);
  21224. ConvertProgram;
  21225. CheckSource('TestClassInterface_COM_AssignVar',
  21226. LinesToStr([ // statements
  21227. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21228. 'rtl.createClass(this, "TObject", null, function () {',
  21229. ' this.$init = function () {',
  21230. ' };',
  21231. ' this.$final = function () {',
  21232. ' };',
  21233. ' rtl.addIntf(this, $mod.IUnknown);',
  21234. '});',
  21235. 'this.i = null;',
  21236. 'this.DoGlobal = function (o) {',
  21237. ' rtl.setIntfP($mod, "i", null);',
  21238. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21239. ' rtl.setIntfP($mod, "i", $mod.i);',
  21240. '};',
  21241. 'var k = null;',
  21242. 'this.DoLocal = function (o) {',
  21243. ' var j = null;',
  21244. ' try{',
  21245. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21246. ' k = rtl.setIntfL(k, $mod.i);',
  21247. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21248. ' j = rtl.setIntfL(j, $mod.i);',
  21249. ' }finally{',
  21250. ' rtl._Release(j);',
  21251. ' };',
  21252. '};',
  21253. 'this.o = null;',
  21254. '']),
  21255. LinesToStr([ // $mod.$main
  21256. 'rtl.setIntfP($mod, "i", null);',
  21257. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21258. '']));
  21259. end;
  21260. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21261. begin
  21262. StartProgram(false);
  21263. Add([
  21264. '{$interfaces com}',
  21265. 'type',
  21266. ' IUnknown = interface',
  21267. ' function _AddRef: longint;',
  21268. ' function _Release: longint;',
  21269. ' end;',
  21270. ' TObject = class(IUnknown)',
  21271. ' function _AddRef: longint; virtual; abstract;',
  21272. ' function _Release: longint; virtual; abstract;',
  21273. ' end;',
  21274. 'procedure DoDefault(i, j: IUnknown);',
  21275. 'begin',
  21276. ' i:=nil;',
  21277. ' i:=j;',
  21278. 'end;',
  21279. 'begin',
  21280. '']);
  21281. ConvertProgram;
  21282. CheckSource('TestClassInterface_COM_AssignArg',
  21283. LinesToStr([ // statements
  21284. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21285. 'rtl.createClass(this, "TObject", null, function () {',
  21286. ' this.$init = function () {',
  21287. ' };',
  21288. ' this.$final = function () {',
  21289. ' };',
  21290. ' rtl.addIntf(this, $mod.IUnknown);',
  21291. '});',
  21292. 'this.DoDefault = function (i, j) {',
  21293. ' rtl._AddRef(i);',
  21294. ' try {',
  21295. ' i = rtl.setIntfL(i, null);',
  21296. ' i = rtl.setIntfL(i, j);',
  21297. ' } finally {',
  21298. ' rtl._Release(i);',
  21299. ' };',
  21300. '};',
  21301. '']),
  21302. LinesToStr([ // $mod.$main
  21303. '']));
  21304. end;
  21305. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21306. begin
  21307. StartProgram(false);
  21308. Add([
  21309. '{$interfaces com}',
  21310. 'type',
  21311. ' IUnknown = interface',
  21312. ' function _AddRef: longint;',
  21313. ' function _Release: longint;',
  21314. ' end;',
  21315. ' TObject = class(IUnknown)',
  21316. ' function _AddRef: longint; virtual; abstract;',
  21317. ' function _Release: longint; virtual; abstract;',
  21318. ' end;',
  21319. 'function DoDefault(i: IUnknown): IUnknown;',
  21320. 'begin',
  21321. ' Result:=i;',
  21322. ' if Result<>nil then exit;',
  21323. 'end;',
  21324. 'begin',
  21325. '']);
  21326. ConvertProgram;
  21327. CheckSource('TestClassInterface_COM_FunctionResult',
  21328. LinesToStr([ // statements
  21329. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21330. 'rtl.createClass(this, "TObject", null, function () {',
  21331. ' this.$init = function () {',
  21332. ' };',
  21333. ' this.$final = function () {',
  21334. ' };',
  21335. ' rtl.addIntf(this, $mod.IUnknown);',
  21336. '});',
  21337. 'this.DoDefault = function (i) {',
  21338. ' var Result = null;',
  21339. ' var $ok = false;',
  21340. ' try {',
  21341. ' Result = rtl.setIntfL(Result, i);',
  21342. ' if(Result !== null){',
  21343. ' $ok = true;',
  21344. ' return Result;',
  21345. ' };',
  21346. ' $ok = true;',
  21347. ' } finally {',
  21348. ' if(!$ok) rtl._Release(Result);',
  21349. ' };',
  21350. ' return Result;',
  21351. '};',
  21352. '']),
  21353. LinesToStr([ // $mod.$main
  21354. '']));
  21355. end;
  21356. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21357. begin
  21358. StartProgram(false);
  21359. Add([
  21360. '{$interfaces com}',
  21361. 'type',
  21362. ' IUnknown = interface',
  21363. ' function _AddRef: longint;',
  21364. ' function _Release: longint;',
  21365. ' end;',
  21366. ' TObject = class(IUnknown)',
  21367. ' function _AddRef: longint; virtual; abstract;',
  21368. ' function _Release: longint; virtual; abstract;',
  21369. ' function GetIntf: IUnknown; virtual;',
  21370. ' end;',
  21371. ' TMouse = class',
  21372. ' function GetIntf: IUnknown; override;',
  21373. ' end;',
  21374. 'function TObject.GetIntf: IUnknown; begin end;',
  21375. 'function TMouse.GetIntf: IUnknown;',
  21376. 'var i: IUnknown;',
  21377. 'begin',
  21378. ' inherited;',
  21379. ' inherited GetIntf;',
  21380. ' inherited GetIntf();',
  21381. ' Result:=inherited GetIntf;',
  21382. ' Result:=inherited GetIntf();',
  21383. ' i:=inherited GetIntf;',
  21384. ' i:=inherited GetIntf();',
  21385. 'end;',
  21386. 'begin',
  21387. '']);
  21388. ConvertProgram;
  21389. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21390. LinesToStr([ // statements
  21391. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21392. 'rtl.createClass(this, "TObject", null, function () {',
  21393. ' this.$init = function () {',
  21394. ' };',
  21395. ' this.$final = function () {',
  21396. ' };',
  21397. ' this.GetIntf = function () {',
  21398. ' var Result = null;',
  21399. ' return Result;',
  21400. ' };',
  21401. ' rtl.addIntf(this, $mod.IUnknown);',
  21402. '});',
  21403. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21404. ' this.GetIntf = function () {',
  21405. ' var Result = null;',
  21406. ' var i = null;',
  21407. ' var $ir = rtl.createIntfRefs();',
  21408. ' var $ok = false;',
  21409. ' try {',
  21410. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21411. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21412. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21413. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21414. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21415. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21416. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21417. ' $ok = true;',
  21418. ' } finally {',
  21419. ' $ir.free();',
  21420. ' rtl._Release(i);',
  21421. ' if (!$ok) rtl._Release(Result);',
  21422. ' };',
  21423. ' return Result;',
  21424. ' };',
  21425. ' rtl.addIntf(this, $mod.IUnknown);',
  21426. '});',
  21427. '']),
  21428. LinesToStr([ // $mod.$main
  21429. '']));
  21430. end;
  21431. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21432. begin
  21433. StartProgram(false);
  21434. Add([
  21435. '{$interfaces com}',
  21436. 'type',
  21437. ' IUnknown = interface',
  21438. ' function _AddRef: longint;',
  21439. ' function _Release: longint;',
  21440. ' end;',
  21441. ' TObject = class(IUnknown)',
  21442. ' function _AddRef: longint; virtual; abstract;',
  21443. ' function _Release: longint; virtual; abstract;',
  21444. ' end;',
  21445. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21446. 'begin',
  21447. ' if i is IUnknown then ;',
  21448. ' if o is IUnknown then ;',
  21449. ' if i is TObject then ;',
  21450. ' i:=j as IUnknown;',
  21451. ' i:=o as IUnknown;',
  21452. ' o:=j as TObject;',
  21453. ' i:=IUnknown(j);',
  21454. ' i:=IUnknown(o);',
  21455. ' o:=TObject(i);',
  21456. 'end;',
  21457. 'begin',
  21458. '']);
  21459. ConvertProgram;
  21460. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21461. LinesToStr([ // statements
  21462. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21463. 'rtl.createClass(this, "TObject", null, function () {',
  21464. ' this.$init = function () {',
  21465. ' };',
  21466. ' this.$final = function () {',
  21467. ' };',
  21468. ' rtl.addIntf(this, $mod.IUnknown);',
  21469. '});',
  21470. 'this.DoDefault = function (i, j, o) {',
  21471. ' rtl._AddRef(i);',
  21472. ' try {',
  21473. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21474. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21475. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21476. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21477. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21478. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21479. ' i = rtl.setIntfL(i, j);',
  21480. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21481. ' o = rtl.intfToClass(i, $mod.TObject);',
  21482. ' } finally {',
  21483. ' rtl._Release(i);',
  21484. ' };',
  21485. '};',
  21486. '']),
  21487. LinesToStr([ // $mod.$main
  21488. '']));
  21489. end;
  21490. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21491. begin
  21492. StartProgram(false);
  21493. Add([
  21494. '{$interfaces com}',
  21495. 'type',
  21496. ' IUnknown = interface',
  21497. ' function _AddRef: longint;',
  21498. ' function _Release: longint;',
  21499. ' end;',
  21500. ' TObject = class(IUnknown)',
  21501. ' function _AddRef: longint; virtual; abstract;',
  21502. ' function _Release: longint; virtual; abstract;',
  21503. ' end;',
  21504. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21505. 'var o: TObject;',
  21506. 'begin',
  21507. ' DoIt(v,v,v,v);',
  21508. ' DoIt(o,o,k,k);',
  21509. 'end;',
  21510. 'procedure DoSome;',
  21511. 'var v: IUnknown;',
  21512. 'begin',
  21513. ' DoIt(v,v,v,v);',
  21514. 'end;',
  21515. 'var i: IUnknown;',
  21516. 'begin',
  21517. ' DoIt(i,i,i,i);',
  21518. '']);
  21519. ConvertProgram;
  21520. CheckSource('TestClassInterface_COM_PassAsArg',
  21521. LinesToStr([ // statements
  21522. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21523. 'rtl.createClass(this, "TObject", null, function () {',
  21524. ' this.$init = function () {',
  21525. ' };',
  21526. ' this.$final = function () {',
  21527. ' };',
  21528. ' rtl.addIntf(this, $mod.IUnknown);',
  21529. '});',
  21530. 'this.DoIt = function (v, j, k, l) {',
  21531. ' var o = null;',
  21532. ' var $ir = rtl.createIntfRefs();',
  21533. ' rtl._AddRef(v);',
  21534. ' try {',
  21535. ' $mod.DoIt(v, v, {',
  21536. ' get: function () {',
  21537. ' return v;',
  21538. ' },',
  21539. ' set: function (w) {',
  21540. ' v = rtl.setIntfL(v, w);',
  21541. ' }',
  21542. ' }, {',
  21543. ' get: function () {',
  21544. ' return v;',
  21545. ' },',
  21546. ' set: function (w) {',
  21547. ' v = rtl.setIntfL(v, w);',
  21548. ' }',
  21549. ' });',
  21550. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21551. ' } finally {',
  21552. ' $ir.free();',
  21553. ' rtl._Release(v);',
  21554. ' };',
  21555. '};',
  21556. 'this.DoSome = function () {',
  21557. ' var v = null;',
  21558. ' try {',
  21559. ' $mod.DoIt(v, v, {',
  21560. ' get: function () {',
  21561. ' return v;',
  21562. ' },',
  21563. ' set: function (w) {',
  21564. ' v = rtl.setIntfL(v, w);',
  21565. ' }',
  21566. ' }, {',
  21567. ' get: function () {',
  21568. ' return v;',
  21569. ' },',
  21570. ' set: function (w) {',
  21571. ' v = rtl.setIntfL(v, w);',
  21572. ' }',
  21573. ' });',
  21574. ' } finally {',
  21575. ' rtl._Release(v);',
  21576. ' };',
  21577. '};',
  21578. 'this.i = null;',
  21579. '']),
  21580. LinesToStr([ // $mod.$main
  21581. '$mod.DoIt($mod.i, $mod.i, {',
  21582. ' p: $mod,',
  21583. ' get: function () {',
  21584. ' return this.p.i;',
  21585. ' },',
  21586. ' set: function (v) {',
  21587. ' rtl.setIntfP(this.p, "i", v);',
  21588. ' }',
  21589. '}, {',
  21590. ' p: $mod,',
  21591. ' get: function () {',
  21592. ' return this.p.i;',
  21593. ' },',
  21594. ' set: function (v) {',
  21595. ' rtl.setIntfP(this.p, "i", v);',
  21596. ' }',
  21597. '});',
  21598. '']));
  21599. end;
  21600. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21601. begin
  21602. StartProgram(false);
  21603. Add([
  21604. '{$interfaces com}',
  21605. 'type',
  21606. ' IUnknown = interface',
  21607. ' function _AddRef: longint;',
  21608. ' function _Release: longint;',
  21609. ' end;',
  21610. ' TObject = class(IUnknown)',
  21611. ' function _AddRef: longint; virtual; abstract;',
  21612. ' function _Release: longint; virtual; abstract;',
  21613. ' end;',
  21614. 'procedure DoIt(out i);',
  21615. 'begin end;',
  21616. 'procedure DoSome;',
  21617. 'var v: IUnknown;',
  21618. 'begin',
  21619. ' DoIt(v);',
  21620. 'end;',
  21621. 'function GetIt: IUnknown;',
  21622. 'begin',
  21623. ' DoIt(Result);',
  21624. 'end;',
  21625. 'var i: IUnknown;',
  21626. 'begin',
  21627. ' DoIt(i);',
  21628. '']);
  21629. ConvertProgram;
  21630. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21631. LinesToStr([ // statements
  21632. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21633. 'rtl.createClass(this, "TObject", null, function () {',
  21634. ' this.$init = function () {',
  21635. ' };',
  21636. ' this.$final = function () {',
  21637. ' };',
  21638. ' rtl.addIntf(this, $mod.IUnknown);',
  21639. '});',
  21640. 'this.DoIt = function (i) {',
  21641. '};',
  21642. 'this.DoSome = function () {',
  21643. ' var v = null;',
  21644. ' try {',
  21645. ' $mod.DoIt({',
  21646. ' get: function () {',
  21647. ' return v;',
  21648. ' },',
  21649. ' set: function (w) {',
  21650. ' v = w;',
  21651. ' }',
  21652. ' });',
  21653. ' } finally {',
  21654. ' rtl._Release(v);',
  21655. ' };',
  21656. '};',
  21657. 'this.GetIt = function () {',
  21658. ' var Result = null;',
  21659. ' var $ok = false;',
  21660. ' try {',
  21661. ' $mod.DoIt({',
  21662. ' get: function () {',
  21663. ' return Result;',
  21664. ' },',
  21665. ' set: function (v) {',
  21666. ' Result = v;',
  21667. ' }',
  21668. ' });',
  21669. ' $ok = true;',
  21670. ' } finally {',
  21671. ' if (!$ok) rtl._Release(Result);',
  21672. ' };',
  21673. ' return Result;',
  21674. '};',
  21675. 'this.i = null;',
  21676. '']),
  21677. LinesToStr([ // $mod.$main
  21678. 'try {',
  21679. ' $mod.DoIt({',
  21680. ' p: $mod,',
  21681. ' get: function () {',
  21682. ' return this.p.i;',
  21683. ' },',
  21684. ' set: function (v) {',
  21685. ' this.p.i = v;',
  21686. ' }',
  21687. ' });',
  21688. '} finally {',
  21689. ' rtl._Release($mod.i);',
  21690. '};',
  21691. '']));
  21692. end;
  21693. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21694. begin
  21695. StartProgram(false);
  21696. Add([
  21697. '{$interfaces com}',
  21698. 'type',
  21699. ' IUnknown = interface',
  21700. ' function _AddRef: longint;',
  21701. ' function _Release: longint;',
  21702. ' end;',
  21703. ' TObject = class(IUnknown)',
  21704. ' function _AddRef: longint; virtual; abstract;',
  21705. ' function _Release: longint; virtual; abstract;',
  21706. ' end;',
  21707. 'function GetIt: IUnknown;',
  21708. 'begin',
  21709. 'end;',
  21710. 'procedure DoSome;',
  21711. 'var v: IUnknown;',
  21712. ' i: longint;',
  21713. 'begin',
  21714. ' v:=GetIt;',
  21715. ' v:=GetIt();',
  21716. ' GetIt()._AddRef;',
  21717. ' i:=GetIt()._AddRef;',
  21718. 'end;',
  21719. 'var v: IUnknown;',
  21720. ' i: longint;',
  21721. 'begin',
  21722. ' v:=GetIt;',
  21723. ' v:=GetIt();',
  21724. ' GetIt()._AddRef;',
  21725. ' i:=GetIt()._AddRef;',
  21726. '']);
  21727. ConvertProgram;
  21728. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21729. LinesToStr([ // statements
  21730. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21731. 'rtl.createClass(this, "TObject", null, function () {',
  21732. ' this.$init = function () {',
  21733. ' };',
  21734. ' this.$final = function () {',
  21735. ' };',
  21736. ' rtl.addIntf(this, $mod.IUnknown);',
  21737. '});',
  21738. 'this.GetIt = function () {',
  21739. ' var Result = null;',
  21740. ' return Result;',
  21741. '};',
  21742. 'this.DoSome = function () {',
  21743. ' var v = null;',
  21744. ' var i = 0;',
  21745. ' var $ir = rtl.createIntfRefs();',
  21746. ' try {',
  21747. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21748. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21749. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21750. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21751. ' } finally {',
  21752. ' $ir.free();',
  21753. ' rtl._Release(v);',
  21754. ' };',
  21755. '};',
  21756. 'this.v = null;',
  21757. 'this.i = 0;',
  21758. '']),
  21759. LinesToStr([ // $mod.$main
  21760. 'var $ir = rtl.createIntfRefs();',
  21761. 'try {',
  21762. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21763. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21764. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21765. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21766. '} finally {',
  21767. ' $ir.free();',
  21768. '};',
  21769. '']));
  21770. end;
  21771. procedure TTestModule.TestClassInterface_COM_Property;
  21772. begin
  21773. StartProgram(false);
  21774. Add([
  21775. '{$interfaces com}',
  21776. 'type',
  21777. ' IUnknown = interface',
  21778. ' function _AddRef: longint;',
  21779. ' function _Release: longint;',
  21780. ' end;',
  21781. ' TObject = class(IUnknown)',
  21782. ' FAnt: IUnknown;',
  21783. ' function _AddRef: longint; virtual; abstract;',
  21784. ' function _Release: longint; virtual; abstract;',
  21785. ' function GetBird: IUnknown; virtual; abstract;',
  21786. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21787. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21788. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21789. ' property Ant: IUnknown read FAnt write FAnt;',
  21790. ' property Bird: IUnknown read GetBird write SetBird;',
  21791. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21792. ' end;',
  21793. 'procedure DoIt;',
  21794. 'var',
  21795. ' o: TObject;',
  21796. ' v: IUnknown;',
  21797. 'begin',
  21798. ' v:=o.Ant;',
  21799. ' o.Ant:=v;',
  21800. ' o.Ant:=o.Ant;',
  21801. ' v:=o.Bird;',
  21802. ' o.Bird:=v;',
  21803. ' o.Bird:=o.Bird;',
  21804. ' v:=o.Items[1];',
  21805. ' o.Items[2]:=v;',
  21806. ' o.Items[3]:=o.Items[4];',
  21807. ' v:=o[5];',
  21808. ' o[6]:=v;',
  21809. ' o[7]:=o[8];',
  21810. 'end;',
  21811. 'begin',
  21812. '']);
  21813. ConvertProgram;
  21814. CheckSource('TestClassInterface_COM_Property',
  21815. LinesToStr([ // statements
  21816. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21817. 'rtl.createClass(this, "TObject", null, function () {',
  21818. ' this.$init = function () {',
  21819. ' this.FAnt = null;',
  21820. ' };',
  21821. ' this.$final = function () {',
  21822. ' rtl.setIntfP(this, "FAnt", null);',
  21823. ' };',
  21824. ' rtl.addIntf(this, $mod.IUnknown);',
  21825. '});',
  21826. 'this.DoIt = function () {',
  21827. ' var o = null;',
  21828. ' var v = null;',
  21829. ' var $ir = rtl.createIntfRefs();',
  21830. ' try {',
  21831. ' v = rtl.setIntfL(v, o.FAnt);',
  21832. ' rtl.setIntfP(o, "FAnt", v);',
  21833. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21834. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21835. ' o.SetBird(v);',
  21836. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21837. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21838. ' o.SetItems(2, v);',
  21839. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21840. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21841. ' o.SetItems(6, v);',
  21842. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21843. ' } finally {',
  21844. ' $ir.free();',
  21845. ' rtl._Release(v);',
  21846. ' };',
  21847. '};',
  21848. '']),
  21849. LinesToStr([ // $mod.$main
  21850. '']));
  21851. end;
  21852. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21853. begin
  21854. StartProgram(false);
  21855. Add([
  21856. '{$interfaces com}',
  21857. 'type',
  21858. ' IUnknown = interface',
  21859. ' function _AddRef: longint;',
  21860. ' function _Release: longint;',
  21861. ' function GetBird: IUnknown;',
  21862. ' procedure SetBird(Value: IUnknown);',
  21863. ' function GetItems(Index: longint): IUnknown;',
  21864. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21865. ' property Bird: IUnknown read GetBird write SetBird;',
  21866. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21867. ' end;',
  21868. ' TObject = class(IUnknown)',
  21869. ' function _AddRef: longint; virtual; abstract;',
  21870. ' function _Release: longint; virtual; abstract;',
  21871. ' function GetBird: IUnknown; virtual; abstract;',
  21872. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21873. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21874. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21875. ' end;',
  21876. 'procedure DoIt;',
  21877. 'var',
  21878. ' o: TObject;',
  21879. ' v: IUnknown;',
  21880. 'begin',
  21881. ' v:=v.Items[1];',
  21882. ' v.Items[2]:=v;',
  21883. ' v.Items[3]:=v.Items[4];',
  21884. ' v:=v[5];',
  21885. ' v[6]:=v;',
  21886. ' v[7]:=v[8];',
  21887. ' v[9].Bird.Bird:=v;',
  21888. ' v:=v.Bird[10].Bird',
  21889. 'end;',
  21890. 'begin',
  21891. '']);
  21892. ConvertProgram;
  21893. CheckSource('TestClassInterface_COM_IntfProperty',
  21894. LinesToStr([ // statements
  21895. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21896. ' "_AddRef",',
  21897. ' "_Release",',
  21898. ' "GetBird",',
  21899. ' "SetBird",',
  21900. ' "GetItems",',
  21901. ' "SetItems"',
  21902. '], null);',
  21903. 'rtl.createClass(this, "TObject", null, function () {',
  21904. ' this.$init = function () {',
  21905. ' };',
  21906. ' this.$final = function () {',
  21907. ' };',
  21908. ' rtl.addIntf(this, $mod.IUnknown);',
  21909. '});',
  21910. 'this.DoIt = function () {',
  21911. ' var o = null;',
  21912. ' var v = null;',
  21913. ' var $ir = rtl.createIntfRefs();',
  21914. ' try {',
  21915. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21916. ' v.SetItems(2, v);',
  21917. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21918. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21919. ' v.SetItems(6, v);',
  21920. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21921. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21922. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21923. ' } finally {',
  21924. ' $ir.free();',
  21925. ' rtl._Release(v);',
  21926. ' };',
  21927. '};',
  21928. '']),
  21929. LinesToStr([ // $mod.$main
  21930. '']));
  21931. end;
  21932. procedure TTestModule.TestClassInterface_COM_Delegation;
  21933. begin
  21934. StartProgram(false);
  21935. Add([
  21936. '{$interfaces com}',
  21937. 'type',
  21938. ' IUnknown = interface',
  21939. ' function _AddRef: longint;',
  21940. ' function _Release: longint;',
  21941. ' end;',
  21942. ' IBird = interface(IUnknown)',
  21943. ' procedure Fly(s: string);',
  21944. ' end;',
  21945. ' IEagle = interface(IBird) end;',
  21946. ' IDove = interface(IBird) end;',
  21947. ' ISwallow = interface(IBird) end;',
  21948. ' TObject = class',
  21949. ' end;',
  21950. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21951. ' function _AddRef: longint; virtual; abstract;',
  21952. ' function _Release: longint; virtual; abstract;',
  21953. ' procedure Fly(s: string); virtual; abstract;',
  21954. ' end;',
  21955. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21956. ' function _AddRef: longint; virtual; abstract;',
  21957. ' function _Release: longint; virtual; abstract;',
  21958. ' FBirdIntf: IBird;',
  21959. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21960. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21961. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21962. ' FDoveObj: TBird;',
  21963. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21964. ' function GetSwallowObj: TBird; virtual; abstract;',
  21965. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21966. ' end;',
  21967. 'begin',
  21968. '']);
  21969. ConvertProgram;
  21970. CheckSource('TestClassInterface_COM_Delegation',
  21971. LinesToStr([ // statements
  21972. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21973. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21974. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21975. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21976. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21977. 'rtl.createClass(this, "TObject", null, function () {',
  21978. ' this.$init = function () {',
  21979. ' };',
  21980. ' this.$final = function () {',
  21981. ' };',
  21982. '});',
  21983. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21984. ' rtl.addIntf(this, $mod.IBird);',
  21985. ' rtl.addIntf(this, $mod.IEagle);',
  21986. ' rtl.addIntf(this, $mod.IDove);',
  21987. ' rtl.addIntf(this, $mod.ISwallow);',
  21988. '});',
  21989. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21990. ' this.$init = function () {',
  21991. ' $mod.TObject.$init.call(this);',
  21992. ' this.FBirdIntf = null;',
  21993. ' this.FDoveObj = null;',
  21994. ' };',
  21995. ' this.$final = function () {',
  21996. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21997. ' this.FDoveObj = undefined;',
  21998. ' $mod.TObject.$final.call(this);',
  21999. ' };',
  22000. ' this.$intfmaps = {',
  22001. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  22002. ' return rtl._AddRef(this.FBirdIntf);',
  22003. ' },',
  22004. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  22005. ' return this.GetEagleIntf();',
  22006. ' },',
  22007. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  22008. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  22009. ' },',
  22010. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  22011. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  22012. ' }',
  22013. ' };',
  22014. '});',
  22015. '']),
  22016. LinesToStr([ // $mod.$main
  22017. '']));
  22018. end;
  22019. procedure TTestModule.TestClassInterface_COM_With;
  22020. begin
  22021. StartProgram(false);
  22022. Add([
  22023. '{$interfaces com}',
  22024. 'type',
  22025. ' IUnknown = interface',
  22026. ' function _AddRef: longint;',
  22027. ' function _Release: longint;',
  22028. ' function GetAnt: IUnknown;',
  22029. ' property Ant: IUnknown read GetAnt;',
  22030. ' end;',
  22031. ' TObject = class(IUnknown)',
  22032. ' function _AddRef: longint; virtual; abstract;',
  22033. ' function _Release: longint; virtual; abstract;',
  22034. ' function GetAnt: IUnknown; virtual; abstract;',
  22035. ' property Ant: IUnknown read GetAnt;',
  22036. ' end;',
  22037. 'procedure DoIt;',
  22038. 'var',
  22039. ' i: IUnknown;',
  22040. 'begin',
  22041. ' with i do ',
  22042. ' GetAnt;',
  22043. ' with i.Ant, Ant do ',
  22044. ' GetAnt;',
  22045. 'end;',
  22046. 'begin',
  22047. '']);
  22048. ConvertProgram;
  22049. CheckSource('TestClassInterface_COM_With',
  22050. LinesToStr([ // statements
  22051. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  22052. 'rtl.createClass(this, "TObject", null, function () {',
  22053. ' this.$init = function () {',
  22054. ' };',
  22055. ' this.$final = function () {',
  22056. ' };',
  22057. ' rtl.addIntf(this, $mod.IUnknown);',
  22058. '});',
  22059. 'this.DoIt = function () {',
  22060. ' var i = null;',
  22061. ' var $ir = rtl.createIntfRefs();',
  22062. ' try {',
  22063. ' $ir.ref(1, i.GetAnt());',
  22064. ' var $with = $ir.ref(2, i.GetAnt());',
  22065. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22066. ' $ir.ref(4, $with1.GetAnt());',
  22067. ' } finally {',
  22068. ' $ir.free();',
  22069. ' };',
  22070. '};',
  22071. '']),
  22072. LinesToStr([ // $mod.$main
  22073. '']));
  22074. end;
  22075. procedure TTestModule.TestClassInterface_COM_ForIn;
  22076. begin
  22077. StartProgram(false);
  22078. Add([
  22079. '{$interfaces com}',
  22080. 'type',
  22081. ' IUnknown = interface end;',
  22082. ' TObject = class',
  22083. ' Id: longint;',
  22084. ' end;',
  22085. ' IEnumerator = interface(IUnknown)',
  22086. ' function GetCurrent: TObject;',
  22087. ' function MoveNext: Boolean;',
  22088. ' property Current: TObject read GetCurrent;',
  22089. ' end;',
  22090. ' IEnumerable = interface(IUnknown)',
  22091. ' function GetEnumerator: IEnumerator;',
  22092. ' end;',
  22093. 'var',
  22094. ' o: TObject;',
  22095. ' i: IEnumerable;',
  22096. 'begin',
  22097. ' for o in i do o.Id:=3;',
  22098. '']);
  22099. ConvertProgram;
  22100. CheckSource('TestClassInterface_COM_ForIn',
  22101. LinesToStr([ // statements
  22102. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22103. 'rtl.createClass(this, "TObject", null, function () {',
  22104. ' this.$init = function () {',
  22105. ' this.Id = 0;',
  22106. ' };',
  22107. ' this.$final = function () {',
  22108. ' };',
  22109. '});',
  22110. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22111. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22112. 'this.o = null;',
  22113. 'this.i = null;',
  22114. '']),
  22115. LinesToStr([ // $mod.$main
  22116. 'var $in = $mod.i.GetEnumerator();',
  22117. 'try {',
  22118. ' while ($in.MoveNext()) {',
  22119. ' $mod.o = $in.GetCurrent();',
  22120. ' $mod.o.Id = 3;',
  22121. ' }',
  22122. '} finally {',
  22123. ' rtl._Release($in)',
  22124. '};',
  22125. '']));
  22126. end;
  22127. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22128. begin
  22129. {$IFNDEF EnableCOMArrayOfIntf}
  22130. exit;
  22131. {$ENDIF}
  22132. StartProgram(false);
  22133. Add([
  22134. '{$interfaces com}',
  22135. 'type',
  22136. ' IUnknown = interface end;',
  22137. ' IBird = interface(IUnknown)',
  22138. ' function Fly(w: word): word;',
  22139. ' end;',
  22140. ' TBirdArray = array of IBird;',
  22141. 'procedure Run;',
  22142. 'var',
  22143. ' i: IBird;',
  22144. ' a,b: TBirdArray;',
  22145. 'begin',
  22146. //' SetLength(a,3);',
  22147. ' a:=b;',
  22148. ' i:=a[1];',
  22149. ' a[2]:=i;',
  22150. //' for i in a do i.fly(3);',
  22151. // a:=copy(b,1,2);
  22152. // a:=concat(b,a);
  22153. // insert(i,b,1);
  22154. // a:=[i,i];
  22155. 'end;',
  22156. // ToDo: pass TBirdArray as arg
  22157. 'begin',
  22158. '']);
  22159. ConvertProgram;
  22160. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22161. LinesToStr([ // statements
  22162. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22163. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22164. 'this.Run = function () {',
  22165. ' var i = null;',
  22166. ' var a = [];',
  22167. ' var b = [];',
  22168. ' try {',
  22169. ' a = rtl.arrayRef(b);',
  22170. ' i = rtl.setIntfL(i, a[1]);',
  22171. ' rtl.setIntfP(a, 2, i);',
  22172. ' } finally {',
  22173. ' rtl._Release(i);',
  22174. ' rtl._ReleaseArray(a,1);',
  22175. ' };',
  22176. '};',
  22177. '']),
  22178. LinesToStr([ // $mod.$main
  22179. '']));
  22180. end;
  22181. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22182. begin
  22183. StartProgram(false);
  22184. Add([
  22185. '{$interfaces com}',
  22186. 'type',
  22187. ' IUnknown = interface',
  22188. ' function _AddRef: longint;',
  22189. ' function _Release: longint;',
  22190. ' end;',
  22191. ' TObject = class',
  22192. ' end;',
  22193. ' TArrOfIntf = array of IUnknown;',
  22194. 'begin',
  22195. '']);
  22196. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22197. ConvertProgram;
  22198. end;
  22199. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22200. begin
  22201. StartProgram(false);
  22202. Add([
  22203. '{$interfaces com}',
  22204. 'type',
  22205. ' IUnknown = interface',
  22206. ' function _AddRef: longint;',
  22207. ' function _Release: longint;',
  22208. ' end;',
  22209. ' TRec = record',
  22210. ' i: IUnknown;',
  22211. ' end;',
  22212. 'begin',
  22213. '']);
  22214. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22215. ConvertProgram;
  22216. end;
  22217. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22218. begin
  22219. StartUnit(false);
  22220. Add([
  22221. '{$interfaces com}',
  22222. 'interface',
  22223. 'implementation',
  22224. 'type',
  22225. ' IUnknown = interface',
  22226. ' function _AddRef: longint;',
  22227. ' end;',
  22228. ' TObject = class(IUnknown)',
  22229. ' function _AddRef: longint;',
  22230. ' end;',
  22231. 'function TObject._AddRef: longint; begin end;',
  22232. 'var i: IUnknown;',
  22233. ' o: TObject;',
  22234. 'initialization',
  22235. ' i:=nil;',
  22236. ' i:=i;',
  22237. ' i:=o;',
  22238. ' if (o as IUnknown)=nil then ;',
  22239. '']);
  22240. ConvertUnit;
  22241. CheckSource('TestClassInterface_COM_UnitInitialization',
  22242. LinesToStr([ // statements
  22243. 'var $impl = $mod.$impl;',
  22244. '']),
  22245. LinesToStr([ // this.$init
  22246. 'var $ir = rtl.createIntfRefs();',
  22247. 'try {',
  22248. ' rtl.setIntfP($impl, "i", null);',
  22249. ' rtl.setIntfP($impl, "i", $impl.i);',
  22250. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22251. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22252. '} finally {',
  22253. ' $ir.free();',
  22254. '};',
  22255. '']),
  22256. LinesToStr([ // implementation
  22257. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22258. 'rtl.createClass($impl, "TObject", null, function () {',
  22259. ' this.$init = function () {',
  22260. ' };',
  22261. ' this.$final = function () {',
  22262. ' };',
  22263. ' this._AddRef = function () {',
  22264. ' var Result = 0;',
  22265. ' return Result;',
  22266. ' };',
  22267. ' rtl.addIntf(this, $impl.IUnknown);',
  22268. '});',
  22269. '$impl.i = null;',
  22270. '$impl.o = null;',
  22271. ''])
  22272. );
  22273. end;
  22274. procedure TTestModule.TestClassInterface_Corba_GUID;
  22275. begin
  22276. StartProgram(false);
  22277. Add([
  22278. '{$interfaces corba}',
  22279. 'type',
  22280. ' IUnknown = interface',
  22281. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22282. ' end;',
  22283. ' TObject = class end;',
  22284. ' TGUID = record D1, D2, D3, D4: word; end;',
  22285. ' TAliasGUID = TGUID;',
  22286. ' TGUIDString = type string;',
  22287. ' TAliasGUIDString = TGUIDString;',
  22288. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22289. 'begin end;',
  22290. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22291. 'begin end;',
  22292. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22293. 'begin end;',
  22294. 'var',
  22295. ' i: IUnknown;',
  22296. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22297. ' s: TAliasGUIDString;',
  22298. 'begin',
  22299. ' DoConstGUIDIt(IUnknown);',
  22300. ' DoDefGUID(IUnknown);',
  22301. ' DoStr(IUnknown);',
  22302. ' DoConstGUIDIt(i);',
  22303. ' DoDefGUID(i);',
  22304. ' DoStr(i);',
  22305. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22306. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22307. ' DoStr(g);',
  22308. ' g:=i;',
  22309. ' g:=IUnknown;',
  22310. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22311. ' s:=i;',
  22312. ' s:=IUnknown;',
  22313. ' s:=g;',
  22314. ' if g=i then ;',
  22315. ' if i=g then ;',
  22316. ' if g=IUnknown then ;',
  22317. ' if IUnknown=g then ;',
  22318. ' if s=i then ;',
  22319. ' if i=s then ;',
  22320. ' if s=IUnknown then ;',
  22321. ' if IUnknown=s then ;',
  22322. ' if s=g then ;',
  22323. ' if g=s then ;',
  22324. '']);
  22325. ConvertProgram;
  22326. CheckSource('TestClassInterface_Corba_GUID',
  22327. LinesToStr([ // statements
  22328. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22329. 'rtl.createClass(this, "TObject", null, function () {',
  22330. ' this.$init = function () {',
  22331. ' };',
  22332. ' this.$final = function () {',
  22333. ' };',
  22334. '});',
  22335. 'rtl.recNewT(this, "TGUID", function () {',
  22336. ' this.D1 = 0;',
  22337. ' this.D2 = 0;',
  22338. ' this.D3 = 0;',
  22339. ' this.D4 = 0;',
  22340. ' this.$eq = function (b) {',
  22341. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22342. ' };',
  22343. ' this.$assign = function (s) {',
  22344. ' this.D1 = s.D1;',
  22345. ' this.D2 = s.D2;',
  22346. ' this.D3 = s.D3;',
  22347. ' this.D4 = s.D4;',
  22348. ' return this;',
  22349. ' };',
  22350. '});',
  22351. 'this.DoConstGUIDIt = function (g) {',
  22352. '};',
  22353. 'this.DoDefGUID = function (g) {',
  22354. '};',
  22355. 'this.DoStr = function (s) {',
  22356. '};',
  22357. 'this.i = null;',
  22358. 'this.g = this.TGUID.$clone({',
  22359. ' D1: 0xD91C9AF4,',
  22360. ' D2: 0x3C93,',
  22361. ' D3: 0x420F,',
  22362. ' D4: [',
  22363. ' 0xA3,',
  22364. ' 0x03,',
  22365. ' 0xBF,',
  22366. ' 0x5B,',
  22367. ' 0xA8,',
  22368. ' 0x2B,',
  22369. ' 0xFD,',
  22370. ' 0x23',
  22371. ' ]',
  22372. '});',
  22373. 'this.s = "";',
  22374. '']),
  22375. LinesToStr([ // $mod.$main
  22376. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22377. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22378. '$mod.DoStr($mod.IUnknown.$guid);',
  22379. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22380. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22381. '$mod.DoStr($mod.i.$guid);',
  22382. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22383. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22384. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22385. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22386. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22387. '$mod.g.$assign({',
  22388. ' D1: 0xD91C9AF4,',
  22389. ' D2: 0x3C93,',
  22390. ' D3: 0x420F,',
  22391. ' D4: [',
  22392. ' 0xA3,',
  22393. ' 0x03,',
  22394. ' 0xBF,',
  22395. ' 0x5B,',
  22396. ' 0xA8,',
  22397. ' 0x2B,',
  22398. ' 0xFD,',
  22399. ' 0x23',
  22400. ' ]',
  22401. '});',
  22402. '$mod.s = $mod.i.$guid;',
  22403. '$mod.s = $mod.IUnknown.$guid;',
  22404. '$mod.s = rtl.guidrToStr($mod.g);',
  22405. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22406. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22407. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22408. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22409. 'if ($mod.s === $mod.i.$guid) ;',
  22410. 'if ($mod.i.$guid === $mod.s) ;',
  22411. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22412. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22413. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22414. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22415. '']));
  22416. end;
  22417. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22418. begin
  22419. StartProgram(false);
  22420. Add([
  22421. '{$interfaces corba}',
  22422. 'type',
  22423. ' IUnknown = interface',
  22424. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22425. ' end;',
  22426. ' TGUID = record D1, D2, D3, D4: word; end;',
  22427. ' TAliasGUID = TGUID;',
  22428. ' TGUIDString = type string;',
  22429. ' TAliasGUIDString = TGUIDString;',
  22430. ' TObject = class',
  22431. ' function GetG: TAliasGUID; virtual; abstract;',
  22432. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22433. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22434. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22435. ' property g: TAliasGUID read GetG write SetG;',
  22436. ' property s: TAliasGUIDString read GetS write SetS;',
  22437. ' end;',
  22438. 'var o: TObject;',
  22439. 'begin',
  22440. ' o.g:=IUnknown;',
  22441. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22442. ' o.s:=IUnknown;',
  22443. ' o.s:=o.g;',
  22444. '']);
  22445. ConvertProgram;
  22446. CheckSource('TestClassInterface_GUIDProperty',
  22447. LinesToStr([ // statements
  22448. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22449. 'rtl.recNewT(this, "TGUID", function () {',
  22450. ' this.D1 = 0;',
  22451. ' this.D2 = 0;',
  22452. ' this.D3 = 0;',
  22453. ' this.D4 = 0;',
  22454. ' this.$eq = function (b) {',
  22455. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22456. ' };',
  22457. ' this.$assign = function (s) {',
  22458. ' this.D1 = s.D1;',
  22459. ' this.D2 = s.D2;',
  22460. ' this.D3 = s.D3;',
  22461. ' this.D4 = s.D4;',
  22462. ' return this;',
  22463. ' };',
  22464. '});',
  22465. 'rtl.createClass(this, "TObject", null, function () {',
  22466. ' this.$init = function () {',
  22467. ' };',
  22468. ' this.$final = function () {',
  22469. ' };',
  22470. '});',
  22471. 'this.o = null;',
  22472. '']),
  22473. LinesToStr([ // $mod.$main
  22474. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22475. '$mod.o.SetG({',
  22476. ' D1: 0xD91C9AF4,',
  22477. ' D2: 0x3C93,',
  22478. ' D3: 0x420F,',
  22479. ' D4: [',
  22480. ' 0xA3,',
  22481. ' 0x03,',
  22482. ' 0xBF,',
  22483. ' 0x5B,',
  22484. ' 0xA8,',
  22485. ' 0x2B,',
  22486. ' 0xFD,',
  22487. ' 0x23',
  22488. ' ]',
  22489. '});',
  22490. '$mod.o.SetS($mod.IUnknown.$guid);',
  22491. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22492. '']));
  22493. end;
  22494. procedure TTestModule.TestClassHelper_ClassVar;
  22495. begin
  22496. StartProgram(false);
  22497. Add([
  22498. 'type',
  22499. ' TObject = class',
  22500. ' end;',
  22501. ' THelper = class helper for TObject',
  22502. ' const',
  22503. ' One = 1;',
  22504. ' Two: word = 2;',
  22505. ' class var',
  22506. ' Glob: word;',
  22507. ' function Foo(w: word): word;',
  22508. ' class function Bar(w: word): word;',
  22509. ' end;',
  22510. 'function THelper.foo(w: word): word;',
  22511. 'begin',
  22512. ' Result:=w;',
  22513. ' Two:=One+w;',
  22514. ' Glob:=Glob;',
  22515. ' Result:=Self.Glob;',
  22516. ' Self.Glob:=Self.Glob;',
  22517. ' with Self do Glob:=Glob;',
  22518. 'end;',
  22519. 'class function THelper.bar(w: word): word;',
  22520. 'begin',
  22521. ' Result:=w;',
  22522. ' Two:=One;',
  22523. ' Glob:=Glob;',
  22524. ' Self.Glob:=Self.Glob;',
  22525. ' with Self do Glob:=Glob;',
  22526. 'end;',
  22527. 'var o: TObject;',
  22528. 'begin',
  22529. ' tobject.two:=tobject.one;',
  22530. ' tobject.Glob:=tobject.Glob;',
  22531. ' with tobject do begin',
  22532. ' two:=one;',
  22533. ' Glob:=Glob;',
  22534. ' end;',
  22535. ' o.two:=o.one;',
  22536. ' o.Glob:=o.Glob;',
  22537. ' with o do begin',
  22538. ' two:=one;',
  22539. ' Glob:=Glob;',
  22540. ' end;',
  22541. '']);
  22542. ConvertProgram;
  22543. CheckSource('TestClassHelper_ClassVar',
  22544. LinesToStr([ // statements
  22545. 'rtl.createClass(this, "TObject", null, function () {',
  22546. ' this.$init = function () {',
  22547. ' };',
  22548. ' this.$final = function () {',
  22549. ' };',
  22550. '});',
  22551. 'rtl.createHelper(this, "THelper", null, function () {',
  22552. ' this.One = 1;',
  22553. ' this.Two = 2;',
  22554. ' this.Glob = 0;',
  22555. ' this.Foo = function (w) {',
  22556. ' var Result = 0;',
  22557. ' Result = w;',
  22558. ' $mod.THelper.Two = 1 + w;',
  22559. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22560. ' Result = $mod.THelper.Glob;',
  22561. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22562. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22563. ' return Result;',
  22564. ' };',
  22565. ' this.Bar = function (w) {',
  22566. ' var Result = 0;',
  22567. ' Result = w;',
  22568. ' $mod.THelper.Two = 1;',
  22569. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22570. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22571. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22572. ' return Result;',
  22573. ' };',
  22574. '});',
  22575. 'this.o = null;',
  22576. '']),
  22577. LinesToStr([ // $mod.$main
  22578. '$mod.THelper.Two = 1;',
  22579. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22580. 'var $with = $mod.TObject;',
  22581. '$mod.THelper.Two = 1;',
  22582. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22583. '$mod.THelper.Two = 1;',
  22584. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22585. 'var $with1 = $mod.o;',
  22586. '$mod.THelper.Two = 1;',
  22587. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22588. '']));
  22589. end;
  22590. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22591. begin
  22592. StartProgram(false);
  22593. Add([
  22594. 'type',
  22595. ' TObject = class',
  22596. ' FSize: word;',
  22597. ' property Size: word read FSize write FSize;',
  22598. ' end;',
  22599. ' THelper = class helper for TObject',
  22600. ' function Foo(w: word = 1): word;',
  22601. ' end;',
  22602. 'function THelper.foo(w: word): word;',
  22603. 'begin',
  22604. ' Result:=Size;',
  22605. ' Size:=Size+2;',
  22606. ' Self.Size:=Self.Size+3;',
  22607. ' FSize:=FSize+4;',
  22608. ' Self.FSize:=Self.FSize+5;',
  22609. ' with Self do begin',
  22610. ' Size:=Size+6;',
  22611. ' FSize:=FSize+7;',
  22612. ' FSize:=FSize+8;',
  22613. ' end;',
  22614. 'end;',
  22615. 'begin',
  22616. '']);
  22617. ConvertProgram;
  22618. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22619. LinesToStr([ // statements
  22620. 'rtl.createClass(this, "TObject", null, function () {',
  22621. ' this.$init = function () {',
  22622. ' this.FSize = 0;',
  22623. ' };',
  22624. ' this.$final = function () {',
  22625. ' };',
  22626. '});',
  22627. 'rtl.createHelper(this, "THelper", null, function () {',
  22628. ' this.Foo = function (w) {',
  22629. ' var Result = 0;',
  22630. ' Result = this.FSize;',
  22631. ' this.FSize = this.FSize + 2;',
  22632. ' this.FSize = this.FSize + 3;',
  22633. ' this.FSize = this.FSize + 4;',
  22634. ' this.FSize = this.FSize + 5;',
  22635. ' this.FSize = this.FSize + 6;',
  22636. ' this.FSize = this.FSize + 7;',
  22637. ' this.FSize = this.FSize + 8;',
  22638. ' return Result;',
  22639. ' };',
  22640. '});',
  22641. '']),
  22642. LinesToStr([ // $mod.$main
  22643. '']));
  22644. end;
  22645. procedure TTestModule.TestClassHelper_Method_Call;
  22646. begin
  22647. StartProgram(false);
  22648. Add([
  22649. 'type',
  22650. ' TObject = class',
  22651. ' procedure Run(w: word = 10);',
  22652. ' end;',
  22653. ' THelper = class helper for TObject',
  22654. ' function Foo(w: word = 1): word;',
  22655. ' end;',
  22656. 'procedure TObject.Run(w: word);',
  22657. 'var o: TObject;',
  22658. 'begin',
  22659. ' Foo;',
  22660. ' Foo();',
  22661. ' Foo(2);',
  22662. ' Self.Foo;',
  22663. ' Self.Foo();',
  22664. ' Self.Foo(3);',
  22665. ' with Self do begin',
  22666. ' Foo;',
  22667. ' Foo();',
  22668. ' Foo(4);',
  22669. ' end;',
  22670. ' with o do Foo(5);',
  22671. 'end;',
  22672. 'function THelper.foo(w: word): word;',
  22673. 'begin',
  22674. ' Run;',
  22675. ' Run();',
  22676. ' Run(11);',
  22677. ' Foo;',
  22678. ' Foo();',
  22679. ' Foo(12);',
  22680. ' Self.Foo;',
  22681. ' Self.Foo();',
  22682. ' Self.Foo(13);',
  22683. ' with Self do begin',
  22684. ' Foo;',
  22685. ' Foo();',
  22686. ' Foo(14);',
  22687. ' end;',
  22688. 'end;',
  22689. 'var Obj: TObject;',
  22690. 'begin',
  22691. ' obj.Foo;',
  22692. ' obj.Foo();',
  22693. ' obj.Foo(21);',
  22694. ' with obj do begin',
  22695. ' Foo;',
  22696. ' Foo();',
  22697. ' Foo(22);',
  22698. ' end;',
  22699. '']);
  22700. ConvertProgram;
  22701. CheckSource('TestClassHelper_Method_Call',
  22702. LinesToStr([ // statements
  22703. 'rtl.createClass(this, "TObject", null, function () {',
  22704. ' this.$init = function () {',
  22705. ' };',
  22706. ' this.$final = function () {',
  22707. ' };',
  22708. ' this.Run = function (w) {',
  22709. ' var o = null;',
  22710. ' $mod.THelper.Foo.call(this, 1);',
  22711. ' $mod.THelper.Foo.call(this, 1);',
  22712. ' $mod.THelper.Foo.call(this, 2);',
  22713. ' $mod.THelper.Foo.call(this, 1);',
  22714. ' $mod.THelper.Foo.call(this, 1);',
  22715. ' $mod.THelper.Foo.call(this, 3);',
  22716. ' $mod.THelper.Foo.call(this, 1);',
  22717. ' $mod.THelper.Foo.call(this, 1);',
  22718. ' $mod.THelper.Foo.call(this, 4);',
  22719. ' $mod.THelper.Foo.call(o, 5);',
  22720. ' };',
  22721. '});',
  22722. 'rtl.createHelper(this, "THelper", null, function () {',
  22723. ' this.Foo = function (w) {',
  22724. ' var Result = 0;',
  22725. ' this.Run(10);',
  22726. ' this.Run(10);',
  22727. ' this.Run(11);',
  22728. ' $mod.THelper.Foo.call(this, 1);',
  22729. ' $mod.THelper.Foo.call(this, 1);',
  22730. ' $mod.THelper.Foo.call(this, 12);',
  22731. ' $mod.THelper.Foo.call(this, 1);',
  22732. ' $mod.THelper.Foo.call(this, 1);',
  22733. ' $mod.THelper.Foo.call(this, 13);',
  22734. ' $mod.THelper.Foo.call(this, 1);',
  22735. ' $mod.THelper.Foo.call(this, 1);',
  22736. ' $mod.THelper.Foo.call(this, 14);',
  22737. ' return Result;',
  22738. ' };',
  22739. '});',
  22740. 'this.Obj = null;',
  22741. '']),
  22742. LinesToStr([ // $mod.$main
  22743. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22744. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22745. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22746. 'var $with = $mod.Obj;',
  22747. '$mod.THelper.Foo.call($with, 1);',
  22748. '$mod.THelper.Foo.call($with, 1);',
  22749. '$mod.THelper.Foo.call($with, 22);',
  22750. '']));
  22751. end;
  22752. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22753. begin
  22754. StartProgram(false);
  22755. Add([
  22756. 'type',
  22757. ' TObject = class',
  22758. ' procedure Run(w: word = 10);',
  22759. ' end;',
  22760. ' THelper = class helper for TObject',
  22761. ' function Foo(w: word = 1): word;',
  22762. ' end;',
  22763. 'procedure TObject.Run(w: word);',
  22764. ' procedure Sub(Self: TObject);',
  22765. ' begin',
  22766. ' Foo;',
  22767. ' Foo();',
  22768. ' Self.Foo;',
  22769. ' Self.Foo();',
  22770. ' with Self do begin',
  22771. ' Foo;',
  22772. ' Foo();',
  22773. ' end;',
  22774. ' end;',
  22775. 'begin',
  22776. 'end;',
  22777. 'function THelper.foo(w: word): word;',
  22778. ' procedure Sub(Self: TObject);',
  22779. ' begin',
  22780. ' Run;',
  22781. ' Run();',
  22782. ' Foo;',
  22783. ' Foo();',
  22784. ' Self.Foo;',
  22785. ' Self.Foo();',
  22786. ' with Self do begin',
  22787. ' Foo;',
  22788. ' Foo();',
  22789. ' end;',
  22790. ' end;',
  22791. 'begin',
  22792. 'end;',
  22793. 'begin',
  22794. '']);
  22795. ConvertProgram;
  22796. CheckSource('TestClassHelper_Method_Nested_Call',
  22797. LinesToStr([ // statements
  22798. 'rtl.createClass(this, "TObject", null, function () {',
  22799. ' this.$init = function () {',
  22800. ' };',
  22801. ' this.$final = function () {',
  22802. ' };',
  22803. ' this.Run = function (w) {',
  22804. ' var $Self = this;',
  22805. ' function Sub(Self) {',
  22806. ' $mod.THelper.Foo.call($Self, 1);',
  22807. ' $mod.THelper.Foo.call($Self, 1);',
  22808. ' $mod.THelper.Foo.call(Self, 1);',
  22809. ' $mod.THelper.Foo.call(Self, 1);',
  22810. ' $mod.THelper.Foo.call(Self, 1);',
  22811. ' $mod.THelper.Foo.call(Self, 1);',
  22812. ' };',
  22813. ' };',
  22814. '});',
  22815. 'rtl.createHelper(this, "THelper", null, function () {',
  22816. ' this.Foo = function (w) {',
  22817. ' var $Self = this;',
  22818. ' var Result = 0;',
  22819. ' function Sub(Self) {',
  22820. ' $Self.Run(10);',
  22821. ' $Self.Run(10);',
  22822. ' $mod.THelper.Foo.call($Self, 1);',
  22823. ' $mod.THelper.Foo.call($Self, 1);',
  22824. ' $mod.THelper.Foo.call(Self, 1);',
  22825. ' $mod.THelper.Foo.call(Self, 1);',
  22826. ' $mod.THelper.Foo.call(Self, 1);',
  22827. ' $mod.THelper.Foo.call(Self, 1);',
  22828. ' };',
  22829. ' return Result;',
  22830. ' };',
  22831. '});',
  22832. '']),
  22833. LinesToStr([ // $mod.$main
  22834. '']));
  22835. end;
  22836. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22837. begin
  22838. StartProgram(false);
  22839. Add([
  22840. 'type',
  22841. ' TObject = class',
  22842. ' class procedure Run(w: word = 10);',
  22843. ' end;',
  22844. ' THelper = class helper for TObject',
  22845. ' class function Foo(w: word = 1): word;',
  22846. ' end;',
  22847. 'class procedure TObject.Run(w: word);',
  22848. 'begin',
  22849. ' Foo;',
  22850. ' Foo();',
  22851. ' Self.Foo;',
  22852. ' Self.Foo();',
  22853. ' with Self do begin',
  22854. ' Foo;',
  22855. ' Foo();',
  22856. ' end;',
  22857. 'end;',
  22858. 'class function THelper.foo(w: word): word;',
  22859. 'begin',
  22860. ' Run;',
  22861. ' Run();',
  22862. ' Foo;',
  22863. ' Foo();',
  22864. ' Self.Foo;',
  22865. ' Self.Foo();',
  22866. ' with Self do begin',
  22867. ' Foo;',
  22868. ' Foo();',
  22869. ' end;',
  22870. 'end;',
  22871. 'var',
  22872. ' Obj: TObject;',
  22873. 'begin',
  22874. ' obj.Foo;',
  22875. ' obj.Foo();',
  22876. ' with obj do begin',
  22877. ' Foo;',
  22878. ' Foo();',
  22879. ' end;',
  22880. ' tobject.Foo;',
  22881. ' tobject.Foo();',
  22882. ' with tobject do begin',
  22883. ' Foo;',
  22884. ' Foo();',
  22885. ' end;',
  22886. '']);
  22887. ConvertProgram;
  22888. CheckSource('TestClassHelper_ClassMethod_Call',
  22889. LinesToStr([ // statements
  22890. 'rtl.createClass(this, "TObject", null, function () {',
  22891. ' this.$init = function () {',
  22892. ' };',
  22893. ' this.$final = function () {',
  22894. ' };',
  22895. ' this.Run = function (w) {',
  22896. ' $mod.THelper.Foo.call(this, 1);',
  22897. ' $mod.THelper.Foo.call(this, 1);',
  22898. ' $mod.THelper.Foo.call(this, 1);',
  22899. ' $mod.THelper.Foo.call(this, 1);',
  22900. ' $mod.THelper.Foo.call(this, 1);',
  22901. ' $mod.THelper.Foo.call(this, 1);',
  22902. ' };',
  22903. '});',
  22904. 'rtl.createHelper(this, "THelper", null, function () {',
  22905. ' this.Foo = function (w) {',
  22906. ' var Result = 0;',
  22907. ' this.Run(10);',
  22908. ' this.Run(10);',
  22909. ' $mod.THelper.Foo.call(this, 1);',
  22910. ' $mod.THelper.Foo.call(this, 1);',
  22911. ' $mod.THelper.Foo.call(this, 1);',
  22912. ' $mod.THelper.Foo.call(this, 1);',
  22913. ' $mod.THelper.Foo.call(this, 1);',
  22914. ' $mod.THelper.Foo.call(this, 1);',
  22915. ' return Result;',
  22916. ' };',
  22917. '});',
  22918. 'this.Obj = null;',
  22919. '']),
  22920. LinesToStr([ // $mod.$main
  22921. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22922. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22923. 'var $with = $mod.Obj;',
  22924. '$mod.THelper.Foo.call($with.$class, 1);',
  22925. '$mod.THelper.Foo.call($with.$class, 1);',
  22926. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22927. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22928. 'var $with1 = $mod.TObject;',
  22929. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22930. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22931. '']));
  22932. end;
  22933. procedure TTestModule.TestClassHelper_ClassOf;
  22934. begin
  22935. StartProgram(false);
  22936. Add([
  22937. 'type',
  22938. ' TObject = class',
  22939. ' end;',
  22940. ' TClass = class of TObject;',
  22941. ' THelper = class helper for TObject',
  22942. ' class function Foo(w: word = 1): word;',
  22943. ' end;',
  22944. 'class function THelper.foo(w: word): word;',
  22945. 'begin',
  22946. 'end;',
  22947. 'var',
  22948. ' c: TClass;',
  22949. 'begin',
  22950. ' c.Foo;',
  22951. ' c.Foo();',
  22952. ' with c do begin',
  22953. ' Foo;',
  22954. ' Foo();',
  22955. ' end;',
  22956. '']);
  22957. ConvertProgram;
  22958. CheckSource('TestClassHelper_ClassOf',
  22959. LinesToStr([ // statements
  22960. 'rtl.createClass(this, "TObject", null, function () {',
  22961. ' this.$init = function () {',
  22962. ' };',
  22963. ' this.$final = function () {',
  22964. ' };',
  22965. '});',
  22966. 'rtl.createHelper(this, "THelper", null, function () {',
  22967. ' this.Foo = function (w) {',
  22968. ' var Result = 0;',
  22969. ' return Result;',
  22970. ' };',
  22971. '});',
  22972. 'this.c = null;',
  22973. '']),
  22974. LinesToStr([ // $mod.$main
  22975. '$mod.THelper.Foo.call($mod.c, 1);',
  22976. '$mod.THelper.Foo.call($mod.c, 1);',
  22977. 'var $with = $mod.c;',
  22978. '$mod.THelper.Foo.call($with, 1);',
  22979. '$mod.THelper.Foo.call($with, 1);',
  22980. '']));
  22981. end;
  22982. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22983. begin
  22984. StartProgram(false);
  22985. Add([
  22986. '{$mode objfpc}',
  22987. 'type',
  22988. ' TObject = class',
  22989. ' procedure DoIt;',
  22990. ' end;',
  22991. ' THelper = class helper for TObject',
  22992. ' procedure Fly(w: word = 1);',
  22993. ' class procedure Glide(w: word = 1);',
  22994. ' class procedure Run(w: word = 1); static;',
  22995. ' end;',
  22996. ' TFly = procedure(w: word) of object;',
  22997. ' TGlide = TFly;',
  22998. ' TRun = procedure(w: word);',
  22999. 'var',
  23000. ' f: TFly;',
  23001. ' g: TGlide;',
  23002. ' r: TRun;',
  23003. 'procedure TObject.DoIt;',
  23004. 'begin',
  23005. ' f:=@fly;',
  23006. ' g:=@glide;',
  23007. ' r:=@run;',
  23008. ' f:[email protected];',
  23009. ' g:[email protected];',
  23010. ' r:[email protected];',
  23011. ' with self do begin',
  23012. ' f:=@fly;',
  23013. ' g:=@glide;',
  23014. ' r:=@run;',
  23015. ' end;',
  23016. 'end;',
  23017. 'procedure THelper.fly(w: word);',
  23018. 'begin',
  23019. ' f:=@fly;',
  23020. ' g:=@glide;',
  23021. ' r:=@run;',
  23022. 'end;',
  23023. 'class procedure THelper.glide(w: word);',
  23024. 'begin',
  23025. ' g:=@glide;',
  23026. ' r:=@run;',
  23027. 'end;',
  23028. 'class procedure THelper.run(w: word);',
  23029. 'begin',
  23030. ' g:=@glide;',
  23031. ' r:=@run;',
  23032. 'end;',
  23033. 'var',
  23034. ' Obj: TObject;',
  23035. 'begin',
  23036. ' f:[email protected];',
  23037. ' g:[email protected];',
  23038. ' r:[email protected];',
  23039. ' with obj do begin',
  23040. ' f:=@fly;',
  23041. ' g:=@glide;',
  23042. ' r:=@run;',
  23043. ' end;',
  23044. ' g:[email protected];',
  23045. ' r:[email protected];',
  23046. ' with tobject do begin',
  23047. ' g:=@glide;',
  23048. ' r:=@run;',
  23049. ' end;',
  23050. '']);
  23051. ConvertProgram;
  23052. CheckSource('TestClassHelper_MethodRefObjFPC',
  23053. LinesToStr([ // statements
  23054. 'rtl.createClass(this, "TObject", null, function () {',
  23055. ' this.$init = function () {',
  23056. ' };',
  23057. ' this.$final = function () {',
  23058. ' };',
  23059. ' this.DoIt = function () {',
  23060. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23061. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23062. ' $mod.r = $mod.THelper.Run;',
  23063. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23064. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23065. ' $mod.r = $mod.THelper.Run;',
  23066. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23067. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23068. ' $mod.r = $mod.THelper.Run;',
  23069. ' };',
  23070. '});',
  23071. 'rtl.createHelper(this, "THelper", null, function () {',
  23072. ' this.Fly = function (w) {',
  23073. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23074. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23075. ' $mod.r = $mod.THelper.Run;',
  23076. ' };',
  23077. ' this.Glide = function (w) {',
  23078. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23079. ' $mod.r = $mod.THelper.Run;',
  23080. ' };',
  23081. ' this.Run = function (w) {',
  23082. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23083. ' $mod.r = $mod.THelper.Run;',
  23084. ' };',
  23085. '});',
  23086. 'this.f = null;',
  23087. 'this.g = null;',
  23088. 'this.r = null;',
  23089. 'this.Obj = null;',
  23090. '']),
  23091. LinesToStr([ // $mod.$main
  23092. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23093. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23094. '$mod.r = $mod.THelper.Run;',
  23095. 'var $with = $mod.Obj;',
  23096. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23097. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23098. '$mod.r = $mod.THelper.Run;',
  23099. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23100. '$mod.r = $mod.THelper.Run;',
  23101. 'var $with1 = $mod.TObject;',
  23102. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23103. '$mod.r = $mod.THelper.Run;',
  23104. '']));
  23105. end;
  23106. procedure TTestModule.TestClassHelper_Constructor;
  23107. begin
  23108. StartProgram(false);
  23109. Add([
  23110. 'type',
  23111. ' TObject = class',
  23112. ' constructor Create;',
  23113. ' end;',
  23114. ' TClass = class of TObject;',
  23115. ' THelper = class helper for TObject',
  23116. ' constructor NewHlp(w: word);',
  23117. ' end;',
  23118. 'var',
  23119. ' obj: TObject;',
  23120. ' c: TClass;',
  23121. 'constructor TObject.Create;',
  23122. 'begin',
  23123. ' NewHlp(2);', // normal call
  23124. ' tobject.NewHlp(3);', // new instance
  23125. ' c.newhlp(4);', // new instance
  23126. 'end;',
  23127. 'constructor THelper.NewHlp(w: word);',
  23128. 'begin',
  23129. ' create;', // normal call
  23130. ' tobject.create;', // new instance
  23131. ' NewHlp(2);', // normal call
  23132. ' tobject.NewHlp(3);', // new instance
  23133. ' c.newhlp(4);', // new instance
  23134. 'end;',
  23135. 'begin',
  23136. ' obj.newhlp(2);', // normal call
  23137. ' with Obj do newhlp(12);', // normal call
  23138. ' tobject.newhlp(3);', // new instance
  23139. ' with tobject do newhlp(13);', // new instance
  23140. ' c.newhlp(4);', // new instance
  23141. ' with c do newhlp(14);', // new instance
  23142. '']);
  23143. ConvertProgram;
  23144. CheckSource('TestClassHelper_Constructor',
  23145. LinesToStr([ // statements
  23146. 'rtl.createClass(this, "TObject", null, function () {',
  23147. ' this.$init = function () {',
  23148. ' };',
  23149. ' this.$final = function () {',
  23150. ' };',
  23151. ' this.Create = function () {',
  23152. ' $mod.THelper.NewHlp.call(this, 2);',
  23153. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23154. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23155. ' return this;',
  23156. ' };',
  23157. '});',
  23158. 'rtl.createHelper(this, "THelper", null, function () {',
  23159. ' this.NewHlp = function (w) {',
  23160. ' this.Create();',
  23161. ' $mod.TObject.$create("Create");',
  23162. ' $mod.THelper.NewHlp.call(this, 2);',
  23163. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23164. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23165. ' return this;',
  23166. ' };',
  23167. '});',
  23168. 'this.obj = null;',
  23169. 'this.c = null;',
  23170. '']),
  23171. LinesToStr([ // $mod.$main
  23172. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23173. 'var $with = $mod.obj;',
  23174. '$mod.THelper.NewHlp.call($with, 12);',
  23175. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23176. 'var $with1 = $mod.TObject;',
  23177. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23178. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23179. 'var $with2 = $mod.c;',
  23180. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23181. '']));
  23182. end;
  23183. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23184. begin
  23185. StartProgram(false);
  23186. Add([
  23187. 'type',
  23188. ' TObject = class',
  23189. ' procedure Fly;',
  23190. ' end;',
  23191. ' TObjHelper = class helper for TObject',
  23192. ' procedure Fly;',
  23193. ' end;',
  23194. ' TBird = class',
  23195. ' procedure Fly;',
  23196. ' end;',
  23197. ' TBirdHelper = class helper for TBird',
  23198. ' procedure Fly;',
  23199. ' procedure Walk(w: word);',
  23200. ' end;',
  23201. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23202. ' procedure Fly;',
  23203. ' procedure Walk(w: word);',
  23204. ' end;',
  23205. 'procedure Tobject.fly;',
  23206. 'begin',
  23207. ' inherited;', // ignore
  23208. 'end;',
  23209. 'procedure Tobjhelper.fly;',
  23210. 'begin',
  23211. ' {@TObject_Fly}inherited;',
  23212. ' inherited {@TObject_Fly}Fly;',
  23213. 'end;',
  23214. 'procedure Tbird.fly;',
  23215. 'begin',
  23216. ' {@TObjHelper_Fly}inherited;',
  23217. ' inherited {@TObjHelper_Fly}Fly;',
  23218. 'end;',
  23219. 'procedure Tbirdhelper.fly;',
  23220. 'begin',
  23221. ' {@TBird_Fly}inherited;',
  23222. ' inherited {@TBird_Fly}Fly;',
  23223. 'end;',
  23224. 'procedure Tbirdhelper.walk(w: word);',
  23225. 'begin',
  23226. 'end;',
  23227. 'procedure teagleHelper.fly;',
  23228. 'begin',
  23229. ' {@TBird_Fly}inherited;',
  23230. ' inherited {@TBird_Fly}Fly;',
  23231. 'end;',
  23232. 'procedure teagleHelper.walk(w: word);',
  23233. 'begin',
  23234. ' {@TBirdHelper_Walk}inherited;',
  23235. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23236. 'end;',
  23237. 'begin',
  23238. '']);
  23239. ConvertProgram;
  23240. CheckSource('TestClassHelper_InheritedObjFPC',
  23241. LinesToStr([ // statements
  23242. 'rtl.createClass(this, "TObject", null, function () {',
  23243. ' this.$init = function () {',
  23244. ' };',
  23245. ' this.$final = function () {',
  23246. ' };',
  23247. ' this.Fly = function () {',
  23248. ' };',
  23249. '});',
  23250. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23251. ' this.Fly = function () {',
  23252. ' $mod.TObject.Fly.call(this);',
  23253. ' $mod.TObject.Fly.call(this);',
  23254. ' };',
  23255. '});',
  23256. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23257. ' this.Fly$1 = function () {',
  23258. ' $mod.TObjHelper.Fly.call(this);',
  23259. ' $mod.TObjHelper.Fly.call(this);',
  23260. ' };',
  23261. '});',
  23262. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23263. ' this.Fly = function () {',
  23264. ' $mod.TBird.Fly$1.call(this);',
  23265. ' $mod.TBird.Fly$1.call(this);',
  23266. ' };',
  23267. ' this.Walk = function (w) {',
  23268. ' };',
  23269. '});',
  23270. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23271. ' this.Fly$1 = function () {',
  23272. ' $mod.TBird.Fly$1.call(this);',
  23273. ' $mod.TBird.Fly$1.call(this);',
  23274. ' };',
  23275. ' this.Walk$1 = function (w) {',
  23276. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23277. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23278. ' };',
  23279. '});',
  23280. '']),
  23281. LinesToStr([ // $mod.$main
  23282. '']));
  23283. end;
  23284. procedure TTestModule.TestClassHelper_Property;
  23285. begin
  23286. StartProgram(false);
  23287. Add([
  23288. 'type',
  23289. ' TObject = class',
  23290. ' FSize: word;',
  23291. ' function GetSpeed: word;',
  23292. ' procedure SetSpeed(Value: word);',
  23293. ' end;',
  23294. ' TObjHelper = class helper for TObject',
  23295. ' function GetLeft: word;',
  23296. ' procedure SetLeft(Value: word);',
  23297. ' property Size: word read FSize write FSize;',
  23298. ' property Speed: word read GetSpeed write SetSpeed;',
  23299. ' property Left: word read GetLeft write SetLeft;',
  23300. ' end;',
  23301. ' TBird = class',
  23302. ' property NotRight: word read GetLeft write SetLeft;',
  23303. ' procedure DoIt;',
  23304. ' end;',
  23305. 'var',
  23306. ' b: TBird;',
  23307. 'function Tobject.GetSpeed: word;',
  23308. 'begin',
  23309. ' Size:=Size+11;',
  23310. ' Speed:=Speed+12;',
  23311. ' Result:=Left+13;',
  23312. ' Left:=13;',
  23313. ' Left:=Left+13;',
  23314. ' Self.Size:=Self.Size+21;',
  23315. ' Self.Speed:=Self.Speed+22;',
  23316. ' Self.Left:=Self.Left+23;',
  23317. ' with Self do begin',
  23318. ' Size:=Size+31;',
  23319. ' Speed:=Speed+32;',
  23320. ' Left:=Left+33;',
  23321. ' end;',
  23322. 'end;',
  23323. 'procedure Tobject.SetSpeed(Value: word);',
  23324. 'begin',
  23325. 'end;',
  23326. 'function TObjHelper.GetLeft: word;',
  23327. 'begin',
  23328. ' Size:=Size+11;',
  23329. ' Speed:=Speed+12;',
  23330. ' Left:=Left+13;',
  23331. ' Self.Size:=Self.Size+21;',
  23332. ' Self.Speed:=Self.Speed+22;',
  23333. ' Self.Left:=Self.Left+23;',
  23334. ' with Self do begin',
  23335. ' Size:=Size+31;',
  23336. ' Speed:=Speed+32;',
  23337. ' Left:=Left+33;',
  23338. ' end;',
  23339. 'end;',
  23340. 'procedure TObjHelper.SetLeft(Value: word);',
  23341. 'begin',
  23342. 'end;',
  23343. 'procedure TBird.DoIt;',
  23344. 'begin',
  23345. ' NotRight:=NotRight+11;',
  23346. ' Self.NotRight:=Self.NotRight+21;',
  23347. ' with Self do begin',
  23348. ' NotRight:=NotRight+31;',
  23349. ' end;',
  23350. 'end;',
  23351. 'begin',
  23352. ' b.Size:=b.Size+11;',
  23353. ' b.Speed:=b.Speed+12;',
  23354. ' b.Left:=b.Left+13;',
  23355. ' b.NotRight:=b.NotRight+14;',
  23356. ' with b do begin',
  23357. ' Size:=Size+31;',
  23358. ' Speed:=Speed+32;',
  23359. ' Left:=Left+33;',
  23360. ' NotRight:=NotRight+34;',
  23361. ' end;',
  23362. '']);
  23363. ConvertProgram;
  23364. CheckSource('TestClassHelper_Property',
  23365. LinesToStr([ // statements
  23366. 'rtl.createClass(this, "TObject", null, function () {',
  23367. ' this.$init = function () {',
  23368. ' this.FSize = 0;',
  23369. ' };',
  23370. ' this.$final = function () {',
  23371. ' };',
  23372. ' this.GetSpeed = function () {',
  23373. ' var Result = 0;',
  23374. ' this.FSize = this.FSize + 11;',
  23375. ' this.SetSpeed(this.GetSpeed() + 12);',
  23376. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23377. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23378. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23379. ' this.FSize = this.FSize + 21;',
  23380. ' this.SetSpeed(this.GetSpeed() + 22);',
  23381. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23382. ' this.FSize = this.FSize + 31;',
  23383. ' this.SetSpeed(this.GetSpeed() + 32);',
  23384. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23385. ' return Result;',
  23386. ' };',
  23387. ' this.SetSpeed = function (Value) {',
  23388. ' };',
  23389. '});',
  23390. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23391. ' this.GetLeft = function () {',
  23392. ' var Result = 0;',
  23393. ' this.FSize = this.FSize + 11;',
  23394. ' this.SetSpeed(this.GetSpeed() + 12);',
  23395. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23396. ' this.FSize = this.FSize + 21;',
  23397. ' this.SetSpeed(this.GetSpeed() + 22);',
  23398. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23399. ' this.FSize = this.FSize + 31;',
  23400. ' this.SetSpeed(this.GetSpeed() + 32);',
  23401. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23402. ' return Result;',
  23403. ' };',
  23404. ' this.SetLeft = function (Value) {',
  23405. ' };',
  23406. '});',
  23407. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23408. ' this.DoIt = function () {',
  23409. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23410. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23411. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23412. ' };',
  23413. '});',
  23414. 'this.b = null;',
  23415. '']),
  23416. LinesToStr([ // $mod.$main
  23417. '$mod.b.FSize = $mod.b.FSize + 11;',
  23418. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23419. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23420. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23421. 'var $with = $mod.b;',
  23422. '$with.FSize = $with.FSize + 31;',
  23423. '$with.SetSpeed($with.GetSpeed() + 32);',
  23424. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23425. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23426. '']));
  23427. end;
  23428. procedure TTestModule.TestClassHelper_Property_Array;
  23429. begin
  23430. StartProgram(false);
  23431. Add([
  23432. 'type',
  23433. ' TObject = class',
  23434. ' function GetSpeed(Index: boolean): word;',
  23435. ' procedure SetSpeed(Index: boolean; Value: word);',
  23436. ' end;',
  23437. ' TObjHelper = class helper for TObject',
  23438. ' function GetSize(Index: boolean): word;',
  23439. ' procedure SetSize(Index: boolean; Value: word);',
  23440. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23441. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23442. ' end;',
  23443. ' TBird = class',
  23444. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23445. ' procedure DoIt;',
  23446. ' end;',
  23447. 'var',
  23448. ' b: TBird;',
  23449. 'function Tobject.GetSpeed(Index: boolean): word;',
  23450. 'begin',
  23451. ' Result:=Size[false];',
  23452. ' Size[true]:=Size[false]+11;',
  23453. ' Speed[true]:=Speed[false]+12;',
  23454. ' Self.Size[true]:=Self.Size[false]+21;',
  23455. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23456. ' with Self do begin',
  23457. ' Size[true]:=Size[false]+31;',
  23458. ' Speed[true]:=Speed[false]+32;',
  23459. ' end;',
  23460. 'end;',
  23461. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23462. 'begin',
  23463. 'end;',
  23464. 'function TObjHelper.GetSize(Index: boolean): word;',
  23465. 'begin',
  23466. ' Size[true]:=Size[false]+11;',
  23467. ' Speed[true]:=Speed[false]+12;',
  23468. ' Self.Size[true]:=Self.Size[false]+21;',
  23469. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23470. ' with Self do begin',
  23471. ' Size[true]:=Size[false]+31;',
  23472. ' Speed[true]:=Speed[false]+32;',
  23473. ' end;',
  23474. 'end;',
  23475. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23476. 'begin',
  23477. 'end;',
  23478. 'procedure TBird.DoIt;',
  23479. 'begin',
  23480. ' Items[true]:=Items[false]+11;',
  23481. ' Self.Items[true]:=Self.Items[false]+21;',
  23482. ' with Self do Items[true]:=Items[false]+31;',
  23483. 'end;',
  23484. 'begin',
  23485. ' b.Size[true]:=b.Size[false]+11;',
  23486. ' b.Speed[true]:=b.Speed[false]+12;',
  23487. ' b.Items[true]:=b.Items[false]+13;',
  23488. ' with b do begin',
  23489. ' Size[true]:=Size[false]+21;',
  23490. ' Speed[true]:=Speed[false]+22;',
  23491. ' Items[true]:=Items[false]+23;',
  23492. ' end;',
  23493. '']);
  23494. ConvertProgram;
  23495. CheckSource('TestClassHelper_Property_Array',
  23496. LinesToStr([ // statements
  23497. 'rtl.createClass(this, "TObject", null, function () {',
  23498. ' this.$init = function () {',
  23499. ' };',
  23500. ' this.$final = function () {',
  23501. ' };',
  23502. ' this.GetSpeed = function (Index) {',
  23503. ' var Result = 0;',
  23504. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23505. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23506. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23507. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23508. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23509. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23510. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23511. ' return Result;',
  23512. ' };',
  23513. ' this.SetSpeed = function (Index, Value) {',
  23514. ' };',
  23515. '});',
  23516. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23517. ' this.GetSize = function (Index) {',
  23518. ' var Result = 0;',
  23519. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23520. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23521. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23522. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23523. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23524. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23525. ' return Result;',
  23526. ' };',
  23527. ' this.SetSize = function (Index, Value) {',
  23528. ' };',
  23529. '});',
  23530. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23531. ' this.DoIt = function () {',
  23532. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23533. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23534. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23535. ' };',
  23536. '});',
  23537. 'this.b = null;',
  23538. '']),
  23539. LinesToStr([ // $mod.$main
  23540. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23541. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23542. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23543. 'var $with = $mod.b;',
  23544. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23545. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23546. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23547. '']));
  23548. end;
  23549. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23550. begin
  23551. StartProgram(false);
  23552. Add([
  23553. 'type',
  23554. ' TObject = class',
  23555. ' function GetSpeed(Index: boolean): word;',
  23556. ' procedure SetSpeed(Index: boolean; Value: word);',
  23557. ' end;',
  23558. ' TObjHelper = class helper for TObject',
  23559. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23560. ' end;',
  23561. ' TBird = class',
  23562. ' end;',
  23563. ' TBirdHelper = class helper for TBird',
  23564. ' function GetSize(Index: word): boolean;',
  23565. ' procedure SetSize(Index: word; Value: boolean);',
  23566. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23567. ' end;',
  23568. 'function Tobject.GetSpeed(Index: boolean): word;',
  23569. 'begin',
  23570. ' Self[true]:=Self[false]+1;',
  23571. 'end;',
  23572. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23573. 'begin',
  23574. 'end;',
  23575. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23576. 'begin',
  23577. ' Self[1]:=not Self[2];',
  23578. 'end;',
  23579. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23580. 'begin',
  23581. 'end;',
  23582. 'var',
  23583. ' o: TObject;',
  23584. ' b: TBird;',
  23585. 'begin',
  23586. ' o[true]:=o[false]+1;',
  23587. ' b[3]:=not b[4];',
  23588. '']);
  23589. ConvertProgram;
  23590. CheckSource('TestClassHelper_Property_Array_Default',
  23591. LinesToStr([ // statements
  23592. 'rtl.createClass(this, "TObject", null, function () {',
  23593. ' this.$init = function () {',
  23594. ' };',
  23595. ' this.$final = function () {',
  23596. ' };',
  23597. ' this.GetSpeed = function (Index) {',
  23598. ' var Result = 0;',
  23599. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23600. ' return Result;',
  23601. ' };',
  23602. ' this.SetSpeed = function (Index, Value) {',
  23603. ' };',
  23604. '});',
  23605. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23606. '});',
  23607. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23608. '});',
  23609. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23610. ' this.GetSize = function (Index) {',
  23611. ' var Result = false;',
  23612. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23613. ' return Result;',
  23614. ' };',
  23615. ' this.SetSize = function (Index, Value) {',
  23616. ' };',
  23617. '});',
  23618. 'this.o = null;',
  23619. 'this.b = null;',
  23620. '']),
  23621. LinesToStr([ // $mod.$main
  23622. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23623. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23624. '']));
  23625. end;
  23626. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23627. begin
  23628. StartProgram(false);
  23629. Add([
  23630. 'type',
  23631. ' TObject = class',
  23632. ' end;',
  23633. ' TObjHelper = class helper for TObject',
  23634. ' function GetItems(Index: word): TObject;',
  23635. ' procedure SetItems(Index: word; Value: TObject);',
  23636. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23637. ' end;',
  23638. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23639. 'begin',
  23640. ' Self[1][2]:=Self[3][4];',
  23641. 'end;',
  23642. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23643. 'begin',
  23644. 'end;',
  23645. 'var',
  23646. ' o: TObject;',
  23647. 'begin',
  23648. ' o[1][2]:=o[3][4];',
  23649. '']);
  23650. ConvertProgram;
  23651. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23652. LinesToStr([ // statements
  23653. 'rtl.createClass(this, "TObject", null, function () {',
  23654. ' this.$init = function () {',
  23655. ' };',
  23656. ' this.$final = function () {',
  23657. ' };',
  23658. '});',
  23659. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23660. ' this.GetItems = function (Index) {',
  23661. ' var Result = null;',
  23662. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23663. ' return Result;',
  23664. ' };',
  23665. ' this.SetItems = function (Index, Value) {',
  23666. ' };',
  23667. '});',
  23668. 'this.o = null;',
  23669. '']),
  23670. LinesToStr([ // $mod.$main
  23671. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23672. '']));
  23673. end;
  23674. procedure TTestModule.TestClassHelper_ClassProperty;
  23675. begin
  23676. StartProgram(false);
  23677. Add([
  23678. 'type',
  23679. ' TObject = class',
  23680. ' class var FSize: word;',
  23681. ' class function GetSpeed: word;',
  23682. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23683. ' end;',
  23684. ' TObjHelper = class helper for TObject',
  23685. ' class function GetLeft: word;',
  23686. ' class procedure SetLeft(Value: word);',
  23687. ' class property Size: word read FSize write FSize;',
  23688. ' class property Speed: word read GetSpeed write SetSpeed;',
  23689. ' class property Left: word read GetLeft write SetLeft;',
  23690. ' end;',
  23691. ' TBird = class',
  23692. ' class property NotRight: word read GetLeft write SetLeft;',
  23693. ' class procedure DoIt;',
  23694. ' end;',
  23695. ' TBirdClass = class of TBird;',
  23696. 'class function Tobject.GetSpeed: word;',
  23697. 'begin',
  23698. ' Size:=Size+11;',
  23699. ' Speed:=Speed+12;',
  23700. ' Left:=Left+13;',
  23701. ' Self.Size:=Self.Size+21;',
  23702. ' Self.Speed:=Self.Speed+22;',
  23703. ' Self.Left:=Self.Left+23;',
  23704. ' with Self do begin',
  23705. ' Size:=Size+31;',
  23706. ' Speed:=Speed+32;',
  23707. ' Left:=Left+33;',
  23708. ' end;',
  23709. 'end;',
  23710. 'class function TObjHelper.GetLeft: word;',
  23711. 'begin',
  23712. ' Size:=Size+11;',
  23713. ' Speed:=Speed+12;',
  23714. ' Left:=Left+13;',
  23715. ' Self.Size:=Self.Size+21;',
  23716. ' Self.Speed:=Self.Speed+22;',
  23717. ' Self.Left:=Self.Left+23;',
  23718. ' with Self do begin',
  23719. ' Size:=Size+31;',
  23720. ' Speed:=Speed+32;',
  23721. ' Left:=Left+33;',
  23722. ' end;',
  23723. 'end;',
  23724. 'class procedure TObjHelper.SetLeft(Value: word);',
  23725. 'begin',
  23726. 'end;',
  23727. 'class procedure TBird.DoIt;',
  23728. 'begin',
  23729. ' NotRight:=NotRight+11;',
  23730. ' Self.NotRight:=Self.NotRight+21;',
  23731. ' with Self do NotRight:=NotRight+31;',
  23732. 'end;',
  23733. 'var',
  23734. ' b: TBird;',
  23735. ' c: TBirdClass;',
  23736. 'begin',
  23737. ' b.Size:=b.Size+11;',
  23738. ' b.Speed:=b.Speed+12;',
  23739. ' b.Left:=b.Left+13;',
  23740. ' b.NotRight:=b.NotRight+14;',
  23741. ' with b do begin',
  23742. ' Size:=Size+31;',
  23743. ' Speed:=Speed+32;',
  23744. ' Left:=Left+33;',
  23745. ' NotRight:=NotRight+34;',
  23746. ' end;',
  23747. ' c.Size:=c.Size+11;',
  23748. ' c.Speed:=c.Speed+12;',
  23749. ' c.Left:=c.Left+13;',
  23750. ' c.NotRight:=c.NotRight+14;',
  23751. ' with c do begin',
  23752. ' Size:=Size+31;',
  23753. ' Speed:=Speed+32;',
  23754. ' Left:=Left+33;',
  23755. ' NotRight:=NotRight+34;',
  23756. ' end;',
  23757. ' tbird.Size:=tbird.Size+11;',
  23758. ' tbird.Speed:=tbird.Speed+12;',
  23759. ' tbird.Left:=tbird.Left+13;',
  23760. ' tbird.NotRight:=tbird.NotRight+14;',
  23761. ' with tbird do begin',
  23762. ' Size:=Size+31;',
  23763. ' Speed:=Speed+32;',
  23764. ' Left:=Left+33;',
  23765. ' NotRight:=NotRight+34;',
  23766. ' end;',
  23767. '']);
  23768. ConvertProgram;
  23769. CheckSource('TestClassHelper_ClassProperty',
  23770. LinesToStr([ // statements
  23771. 'rtl.createClass(this, "TObject", null, function () {',
  23772. ' this.FSize = 0;',
  23773. ' this.$init = function () {',
  23774. ' };',
  23775. ' this.$final = function () {',
  23776. ' };',
  23777. ' this.GetSpeed = function () {',
  23778. ' var Result = 0;',
  23779. ' $mod.TObject.FSize = this.FSize + 11;',
  23780. ' this.SetSpeed(this.GetSpeed() + 12);',
  23781. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23782. ' $mod.TObject.FSize = this.FSize + 21;',
  23783. ' this.SetSpeed(this.GetSpeed() + 22);',
  23784. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23785. ' $mod.TObject.FSize = this.FSize + 31;',
  23786. ' this.SetSpeed(this.GetSpeed() + 32);',
  23787. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23788. ' return Result;',
  23789. ' };',
  23790. '});',
  23791. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23792. ' this.GetLeft = function () {',
  23793. ' var Result = 0;',
  23794. ' $mod.TObject.FSize = this.FSize + 11;',
  23795. ' this.SetSpeed(this.GetSpeed() + 12);',
  23796. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23797. ' $mod.TObject.FSize = this.FSize + 21;',
  23798. ' this.SetSpeed(this.GetSpeed() + 22);',
  23799. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23800. ' $mod.TObject.FSize = this.FSize + 31;',
  23801. ' this.SetSpeed(this.GetSpeed() + 32);',
  23802. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23803. ' return Result;',
  23804. ' };',
  23805. ' this.SetLeft = function (Value) {',
  23806. ' };',
  23807. '});',
  23808. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23809. ' this.DoIt = function () {',
  23810. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23811. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23812. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23813. ' };',
  23814. '});',
  23815. 'this.b = null;',
  23816. 'this.c = null;',
  23817. '']),
  23818. LinesToStr([ // $mod.$main
  23819. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23820. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23821. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23822. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23823. 'var $with = $mod.b;',
  23824. '$mod.TObject.FSize = $with.FSize + 31;',
  23825. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23826. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23827. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23828. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23829. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23830. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23831. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23832. 'var $with1 = $mod.c;',
  23833. '$mod.TObject.FSize = $with1.FSize + 31;',
  23834. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23835. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23836. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23837. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23838. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23839. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23840. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23841. 'var $with2 = $mod.TBird;',
  23842. '$mod.TObject.FSize = $with2.FSize + 31;',
  23843. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23844. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23845. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23846. '']));
  23847. end;
  23848. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23849. begin
  23850. StartProgram(false);
  23851. Add([
  23852. 'type',
  23853. ' TObject = class',
  23854. ' class function GetSpeed: word; static;',
  23855. ' class procedure SetSpeed(Value: word); static;',
  23856. ' end;',
  23857. ' TObjHelper = class helper for TObject',
  23858. ' class function GetLeft: word; static;',
  23859. ' class procedure SetLeft(Value: word); static;',
  23860. ' class property Speed: word read GetSpeed write SetSpeed;',
  23861. ' class property Left: word read GetLeft write SetLeft;',
  23862. ' end;',
  23863. ' TBird = class',
  23864. ' class property NotRight: word read GetLeft write SetLeft;',
  23865. ' class procedure DoIt; static;',
  23866. ' class procedure DoSome;',
  23867. ' end;',
  23868. ' TBirdClass = class of TBird;',
  23869. 'class function Tobject.GetSpeed: word;',
  23870. 'begin',
  23871. ' Speed:=Speed+12;',
  23872. ' Left:=Left+13;',
  23873. 'end;',
  23874. 'class procedure TObject.SetSpeed(Value: word);',
  23875. 'begin',
  23876. 'end;',
  23877. 'class function TObjHelper.GetLeft: word;',
  23878. 'begin',
  23879. ' Speed:=Speed+12;',
  23880. ' Left:=Left+13;',
  23881. 'end;',
  23882. 'class procedure TObjHelper.SetLeft(Value: word);',
  23883. 'begin',
  23884. 'end;',
  23885. 'class procedure TBird.DoIt;',
  23886. 'begin',
  23887. ' NotRight:=NotRight+11;',
  23888. 'end;',
  23889. 'class procedure TBird.DoSome;',
  23890. 'begin',
  23891. ' Speed:=Speed+12;',
  23892. ' Left:=Left+13;',
  23893. ' Self.Speed:=Self.Speed+22;',
  23894. ' Self.Left:=Self.Left+23;',
  23895. ' with Self do begin',
  23896. ' Speed:=Speed+32;',
  23897. ' Left:=Left+33;',
  23898. ' end;',
  23899. ' NotRight:=NotRight+11;',
  23900. ' Self.NotRight:=Self.NotRight+21;',
  23901. ' with Self do NotRight:=NotRight+31;',
  23902. 'end;',
  23903. 'var',
  23904. ' b: TBird;',
  23905. ' c: TBirdClass;',
  23906. 'begin',
  23907. ' b.Speed:=b.Speed+12;',
  23908. ' b.Left:=b.Left+13;',
  23909. ' b.NotRight:=b.NotRight+14;',
  23910. ' with b do begin',
  23911. ' Speed:=Speed+32;',
  23912. ' Left:=Left+33;',
  23913. ' NotRight:=NotRight+34;',
  23914. ' end;',
  23915. ' c.Speed:=c.Speed+12;',
  23916. ' c.Left:=c.Left+13;',
  23917. ' c.NotRight:=c.NotRight+14;',
  23918. ' with c do begin',
  23919. ' Speed:=Speed+32;',
  23920. ' Left:=Left+33;',
  23921. ' NotRight:=NotRight+34;',
  23922. ' end;',
  23923. ' tbird.Speed:=tbird.Speed+12;',
  23924. ' tbird.Left:=tbird.Left+13;',
  23925. ' tbird.NotRight:=tbird.NotRight+14;',
  23926. ' with tbird do begin',
  23927. ' Speed:=Speed+32;',
  23928. ' Left:=Left+33;',
  23929. ' NotRight:=NotRight+34;',
  23930. ' end;',
  23931. '']);
  23932. ConvertProgram;
  23933. CheckSource('TestClassHelper_ClassPropertyStatic',
  23934. LinesToStr([ // statements
  23935. 'rtl.createClass(this, "TObject", null, function () {',
  23936. ' this.$init = function () {',
  23937. ' };',
  23938. ' this.$final = function () {',
  23939. ' };',
  23940. ' this.GetSpeed = function () {',
  23941. ' var Result = 0;',
  23942. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23943. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23944. ' return Result;',
  23945. ' };',
  23946. ' this.SetSpeed = function (Value) {',
  23947. ' };',
  23948. '});',
  23949. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23950. ' this.GetLeft = function () {',
  23951. ' var Result = 0;',
  23952. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23953. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23954. ' return Result;',
  23955. ' };',
  23956. ' this.SetLeft = function (Value) {',
  23957. ' };',
  23958. '});',
  23959. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23960. ' this.DoIt = function () {',
  23961. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23962. ' };',
  23963. ' this.DoSome = function () {',
  23964. ' this.SetSpeed(this.GetSpeed() + 12);',
  23965. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23966. ' this.SetSpeed(this.GetSpeed() + 22);',
  23967. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23968. ' this.SetSpeed(this.GetSpeed() + 32);',
  23969. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23970. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23971. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23972. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23973. ' };',
  23974. '});',
  23975. 'this.b = null;',
  23976. 'this.c = null;',
  23977. '']),
  23978. LinesToStr([ // $mod.$main
  23979. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23980. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23981. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23982. 'var $with = $mod.b;',
  23983. '$with.SetSpeed($with.GetSpeed() + 32);',
  23984. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23985. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23986. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23987. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23988. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23989. 'var $with1 = $mod.c;',
  23990. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23991. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23992. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23993. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23994. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23995. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23996. 'var $with2 = $mod.TBird;',
  23997. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23998. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23999. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24000. '']));
  24001. end;
  24002. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  24003. begin
  24004. StartProgram(false);
  24005. Add([
  24006. 'type',
  24007. ' TObject = class',
  24008. ' class function GetSpeed(Index: boolean): word;',
  24009. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  24010. ' end;',
  24011. ' TObjHelper = class helper for TObject',
  24012. ' class function GetSize(Index: boolean): word;',
  24013. ' class procedure SetSize(Index: boolean; Value: word);',
  24014. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  24015. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  24016. ' end;',
  24017. ' TBird = class',
  24018. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  24019. ' class procedure DoIt;',
  24020. ' end;',
  24021. ' TBirdClass = class of TBird;',
  24022. 'class function Tobject.GetSpeed(Index: boolean): word;',
  24023. 'begin',
  24024. ' Size[true]:=Size[false]+11;',
  24025. ' Speed[true]:=Speed[false]+12;',
  24026. ' Self.Size[true]:=Self.Size[false]+21;',
  24027. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24028. ' with Self do begin',
  24029. ' Size[true]:=Size[false]+31;',
  24030. ' Speed[true]:=Speed[false]+32;',
  24031. ' end;',
  24032. 'end;',
  24033. 'class function TObjHelper.GetSize(Index: boolean): word;',
  24034. 'begin',
  24035. ' Size[true]:=Size[false]+11;',
  24036. ' Speed[true]:=Speed[false]+12;',
  24037. ' Self.Size[true]:=Self.Size[false]+21;',
  24038. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24039. ' with Self do begin',
  24040. ' Size[true]:=Size[false]+31;',
  24041. ' Speed[true]:=Speed[false]+32;',
  24042. ' end;',
  24043. 'end;',
  24044. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  24045. 'begin',
  24046. 'end;',
  24047. 'class procedure TBird.DoIt;',
  24048. 'begin',
  24049. ' Items[true]:=Items[false]+11;',
  24050. ' Self.Items[true]:=Self.Items[false]+21;',
  24051. ' with Self do Items[true]:=Items[false]+31;',
  24052. 'end;',
  24053. 'var',
  24054. ' b: TBird;',
  24055. ' c: TBirdClass;',
  24056. 'begin',
  24057. ' b.Size[true]:=b.Size[false]+11;',
  24058. ' b.Speed[true]:=b.Speed[false]+12;',
  24059. ' b.Items[true]:=b.Items[false]+13;',
  24060. ' with b do begin',
  24061. ' Size[true]:=Size[false]+21;',
  24062. ' Speed[true]:=Speed[false]+22;',
  24063. ' Items[true]:=Items[false]+23;',
  24064. ' end;',
  24065. ' c.Size[true]:=c.Size[false]+11;',
  24066. ' c.Speed[true]:=c.Speed[false]+12;',
  24067. ' c.Items[true]:=c.Items[false]+13;',
  24068. ' with c do begin',
  24069. ' Size[true]:=Size[false]+21;',
  24070. ' Speed[true]:=Speed[false]+22;',
  24071. ' Items[true]:=Items[false]+23;',
  24072. ' end;',
  24073. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24074. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24075. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24076. ' with TBird do begin',
  24077. ' Size[true]:=Size[false]+21;',
  24078. ' Speed[true]:=Speed[false]+22;',
  24079. ' Items[true]:=Items[false]+23;',
  24080. ' end;',
  24081. '']);
  24082. ConvertProgram;
  24083. CheckSource('TestClassHelper_ClassProperty_Array',
  24084. LinesToStr([ // statements
  24085. 'rtl.createClass(this, "TObject", null, function () {',
  24086. ' this.$init = function () {',
  24087. ' };',
  24088. ' this.$final = function () {',
  24089. ' };',
  24090. ' this.GetSpeed = function (Index) {',
  24091. ' var Result = 0;',
  24092. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24093. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24094. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24095. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24096. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24097. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24098. ' return Result;',
  24099. ' };',
  24100. '});',
  24101. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24102. ' this.GetSize = function (Index) {',
  24103. ' var Result = 0;',
  24104. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24105. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24106. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24107. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24108. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24109. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24110. ' return Result;',
  24111. ' };',
  24112. ' this.SetSize = function (Index, Value) {',
  24113. ' };',
  24114. '});',
  24115. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24116. ' this.DoIt = function () {',
  24117. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24118. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24119. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24120. ' };',
  24121. '});',
  24122. 'this.b = null;',
  24123. 'this.c = null;',
  24124. '']),
  24125. LinesToStr([ // $mod.$main
  24126. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24127. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24128. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24129. 'var $with = $mod.b;',
  24130. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24131. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24132. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24133. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24134. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24135. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24136. 'var $with1 = $mod.c;',
  24137. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24138. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24139. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24140. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24141. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24142. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24143. 'var $with2 = $mod.TBird;',
  24144. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24145. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24146. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24147. '']));
  24148. end;
  24149. procedure TTestModule.TestClassHelper_ForIn;
  24150. begin
  24151. StartProgram(false);
  24152. Add([
  24153. 'type',
  24154. ' TObject = class end;',
  24155. ' TItem = TObject;',
  24156. ' TEnumerator = class',
  24157. ' FCurrent: TItem;',
  24158. ' property Current: TItem read FCurrent;',
  24159. ' function MoveNext: boolean;',
  24160. ' end;',
  24161. ' TBird = class',
  24162. ' end;',
  24163. ' TBirdHelper = class helper for TBird',
  24164. ' function GetEnumerator: TEnumerator;',
  24165. ' end;',
  24166. 'function TEnumerator.MoveNext: boolean;',
  24167. 'begin',
  24168. 'end;',
  24169. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24170. 'begin',
  24171. 'end;',
  24172. 'var',
  24173. ' b: TBird;',
  24174. ' i, i2: TItem;',
  24175. 'begin',
  24176. ' for i in b do i2:=i;']);
  24177. ConvertProgram;
  24178. CheckSource('TestClassHelper_ForIn',
  24179. LinesToStr([ // statements
  24180. 'rtl.createClass(this, "TObject", null, function () {',
  24181. ' this.$init = function () {',
  24182. ' };',
  24183. ' this.$final = function () {',
  24184. ' };',
  24185. '});',
  24186. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24187. ' this.$init = function () {',
  24188. ' $mod.TObject.$init.call(this);',
  24189. ' this.FCurrent = null;',
  24190. ' };',
  24191. ' this.$final = function () {',
  24192. ' this.FCurrent = undefined;',
  24193. ' $mod.TObject.$final.call(this);',
  24194. ' };',
  24195. ' this.MoveNext = function () {',
  24196. ' var Result = false;',
  24197. ' return Result;',
  24198. ' };',
  24199. '});',
  24200. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24201. '});',
  24202. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24203. ' this.GetEnumerator = function () {',
  24204. ' var Result = null;',
  24205. ' return Result;',
  24206. ' };',
  24207. '});',
  24208. 'this.b = null;',
  24209. 'this.i = null;',
  24210. 'this.i2 = null;'
  24211. ]),
  24212. LinesToStr([ // $mod.$main
  24213. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24214. 'try {',
  24215. ' while ($in.MoveNext()){',
  24216. ' $mod.i = $in.FCurrent;',
  24217. ' $mod.i2 = $mod.i;',
  24218. ' }',
  24219. '} finally {',
  24220. ' $in = rtl.freeLoc($in)',
  24221. '};',
  24222. '']));
  24223. end;
  24224. procedure TTestModule.TestClassHelper_PassProperty;
  24225. begin
  24226. StartProgram(false);
  24227. Add([
  24228. 'type',
  24229. ' TObject = class',
  24230. ' FField: TObject;',
  24231. ' property Field: TObject read FField write FField;',
  24232. ' end;',
  24233. ' THelper = class helper for TObject',
  24234. ' procedure Fly;',
  24235. ' class procedure Run;',
  24236. ' class procedure Jump; static;',
  24237. ' end;',
  24238. 'procedure THelper.Fly;',
  24239. 'begin',
  24240. ' Field.Fly;',
  24241. ' Field.Run;',
  24242. ' Field.Jump;',
  24243. ' with Field do begin',
  24244. ' Fly;',
  24245. ' Run;',
  24246. ' Jump;',
  24247. ' end;',
  24248. 'end;',
  24249. 'class procedure THelper.Run;',
  24250. 'begin',
  24251. 'end;',
  24252. 'class procedure THelper.Jump;',
  24253. 'begin',
  24254. 'end;',
  24255. 'var',
  24256. ' b: TObject;',
  24257. 'begin',
  24258. ' b.Field.Fly;',
  24259. ' b.Field.Run;',
  24260. ' b.Field.Jump;',
  24261. ' with b do begin',
  24262. ' Field.Run;',
  24263. ' Field.Fly;',
  24264. ' Field.Jump;',
  24265. ' end;',
  24266. ' with b.Field do begin',
  24267. ' Run;',
  24268. ' Fly;',
  24269. ' Jump;',
  24270. ' end;',
  24271. '']);
  24272. ConvertProgram;
  24273. CheckSource('TestClassHelper_PassProperty',
  24274. LinesToStr([ // statements
  24275. 'rtl.createClass(this, "TObject", null, function () {',
  24276. ' this.$init = function () {',
  24277. ' this.FField = null;',
  24278. ' };',
  24279. ' this.$final = function () {',
  24280. ' this.FField = undefined;',
  24281. ' };',
  24282. '});',
  24283. 'rtl.createHelper(this, "THelper", null, function () {',
  24284. ' this.Fly = function () {',
  24285. ' $mod.THelper.Fly.call(this.FField);',
  24286. ' $mod.THelper.Run.call(this.FField.$class);',
  24287. ' $mod.THelper.Jump();',
  24288. ' var $with = this.FField;',
  24289. ' $mod.THelper.Fly.call($with);',
  24290. ' $mod.THelper.Run.call($with.$class);',
  24291. ' $mod.THelper.Jump();',
  24292. ' };',
  24293. ' this.Run = function () {',
  24294. ' };',
  24295. ' this.Jump = function () {',
  24296. ' };',
  24297. '});',
  24298. 'this.b = null;',
  24299. '']),
  24300. LinesToStr([ // $mod.$main
  24301. '$mod.THelper.Fly.call($mod.b.FField);',
  24302. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24303. '$mod.THelper.Jump();',
  24304. 'var $with = $mod.b;',
  24305. '$mod.THelper.Run.call($with.FField.$class);',
  24306. '$mod.THelper.Fly.call($with.FField);',
  24307. '$mod.THelper.Jump();',
  24308. 'var $with1 = $mod.b.FField;',
  24309. '$mod.THelper.Run.call($with1.$class);',
  24310. '$mod.THelper.Fly.call($with1);',
  24311. '$mod.THelper.Jump();',
  24312. '']));
  24313. end;
  24314. procedure TTestModule.TestExtClassHelper_ClassVar;
  24315. begin
  24316. StartProgram(false);
  24317. Add([
  24318. '{$modeswitch externalclass}',
  24319. 'type',
  24320. ' TExtA = class external name ''ExtObj''',
  24321. ' end;',
  24322. ' THelper = class helper for TExtA',
  24323. ' const',
  24324. ' One = 1;',
  24325. ' Two: word = 2;',
  24326. ' class var',
  24327. ' Glob: word;',
  24328. ' function Foo(w: word): word;',
  24329. ' class function Bar(w: word): word; static;',
  24330. ' end;',
  24331. 'function THelper.foo(w: word): word;',
  24332. 'begin',
  24333. ' Result:=w;',
  24334. ' Two:=One+w;',
  24335. ' Glob:=Glob;',
  24336. ' Result:=Self.Glob;',
  24337. ' Self.Glob:=Self.Glob;',
  24338. ' with Self do Glob:=Glob;',
  24339. 'end;',
  24340. 'class function THelper.bar(w: word): word;',
  24341. 'begin',
  24342. ' Result:=w;',
  24343. ' Two:=One;',
  24344. ' Glob:=Glob;',
  24345. 'end;',
  24346. 'var o: TExtA;',
  24347. 'begin',
  24348. ' texta.two:=texta.one;',
  24349. ' texta.Glob:=texta.Glob;',
  24350. ' with texta do begin',
  24351. ' two:=one;',
  24352. ' Glob:=Glob;',
  24353. ' end;',
  24354. ' o.two:=o.one;',
  24355. ' o.Glob:=o.Glob;',
  24356. ' with o do begin',
  24357. ' two:=one;',
  24358. ' Glob:=Glob;',
  24359. ' end;',
  24360. '']);
  24361. ConvertProgram;
  24362. CheckSource('TestExtClassHelper_ClassVar',
  24363. LinesToStr([ // statements
  24364. 'rtl.createHelper(this, "THelper", null, function () {',
  24365. ' this.One = 1;',
  24366. ' this.Two = 2;',
  24367. ' this.Glob = 0;',
  24368. ' this.Foo = function (w) {',
  24369. ' var Result = 0;',
  24370. ' Result = w;',
  24371. ' $mod.THelper.Two = 1 + w;',
  24372. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24373. ' Result = $mod.THelper.Glob;',
  24374. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24375. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24376. ' return Result;',
  24377. ' };',
  24378. ' this.Bar = function (w) {',
  24379. ' var Result = 0;',
  24380. ' Result = w;',
  24381. ' $mod.THelper.Two = 1;',
  24382. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24383. ' return Result;',
  24384. ' };',
  24385. '});',
  24386. 'this.o = null;',
  24387. '']),
  24388. LinesToStr([ // $mod.$main
  24389. '$mod.THelper.Two = 1;',
  24390. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24391. '$mod.THelper.Two = 1;',
  24392. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24393. '$mod.THelper.Two = 1;',
  24394. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24395. 'var $with = $mod.o;',
  24396. '$mod.THelper.Two = 1;',
  24397. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24398. '']));
  24399. end;
  24400. procedure TTestModule.TestExtClassHelper_Method_Call;
  24401. begin
  24402. StartProgram(false);
  24403. Add([
  24404. '{$modeswitch externalclass}',
  24405. 'type',
  24406. ' TFly = function(w: word): word of object;',
  24407. ' TExtA = class external name ''ExtObj''',
  24408. ' procedure Run(w: word = 10);',
  24409. ' end;',
  24410. ' THelper = class helper for TExtA',
  24411. ' function Foo(w: word = 1): word;',
  24412. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24413. ' end;',
  24414. 'var p: TFly;',
  24415. 'function THelper.foo(w: word): word;',
  24416. 'begin',
  24417. ' Run;',
  24418. ' Run();',
  24419. ' Run(11);',
  24420. ' Foo;',
  24421. ' Foo();',
  24422. ' Foo(12);',
  24423. ' Self.Foo;',
  24424. ' Self.Foo();',
  24425. ' Self.Foo(13);',
  24426. ' Fly;',
  24427. ' Fly();',
  24428. ' with Self do begin',
  24429. ' Foo;',
  24430. ' Foo();',
  24431. ' Foo(14);',
  24432. ' Fly;',
  24433. ' Fly();',
  24434. ' end;',
  24435. ' p:=@Fly;',
  24436. 'end;',
  24437. 'var Obj: TExtA;',
  24438. 'begin',
  24439. ' obj.Foo;',
  24440. ' obj.Foo();',
  24441. ' obj.Foo(21);',
  24442. ' obj.Fly;',
  24443. ' obj.Fly();',
  24444. ' with obj do begin',
  24445. ' Foo;',
  24446. ' Foo();',
  24447. ' Foo(22);',
  24448. ' Fly;',
  24449. ' Fly();',
  24450. ' end;',
  24451. ' p:[email protected];',
  24452. '']);
  24453. ConvertProgram;
  24454. CheckSource('TestExtClassHelper_Method_Call',
  24455. LinesToStr([ // statements
  24456. 'rtl.createHelper(this, "THelper", null, function () {',
  24457. ' this.Foo = function (w) {',
  24458. ' var Result = 0;',
  24459. ' this.Run(10);',
  24460. ' this.Run(10);',
  24461. ' this.Run(11);',
  24462. ' $mod.THelper.Foo.call(this, 1);',
  24463. ' $mod.THelper.Foo.call(this, 1);',
  24464. ' $mod.THelper.Foo.call(this, 12);',
  24465. ' $mod.THelper.Foo.call(this, 1);',
  24466. ' $mod.THelper.Foo.call(this, 1);',
  24467. ' $mod.THelper.Foo.call(this, 13);',
  24468. ' this.Fly(2);',
  24469. ' this.Fly(2);',
  24470. ' $mod.THelper.Foo.call(this, 1);',
  24471. ' $mod.THelper.Foo.call(this, 1);',
  24472. ' $mod.THelper.Foo.call(this, 14);',
  24473. ' this.Fly(2);',
  24474. ' this.Fly(2);',
  24475. ' $mod.p = rtl.createCallback(this, "Fly");',
  24476. ' return Result;',
  24477. ' };',
  24478. '});',
  24479. 'this.p = null;',
  24480. 'this.Obj = null;',
  24481. '']),
  24482. LinesToStr([ // $mod.$main
  24483. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24484. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24485. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24486. '$mod.Obj.Fly(2);',
  24487. '$mod.Obj.Fly(2);',
  24488. 'var $with = $mod.Obj;',
  24489. '$mod.THelper.Foo.call($with, 1);',
  24490. '$mod.THelper.Foo.call($with, 1);',
  24491. '$mod.THelper.Foo.call($with, 22);',
  24492. '$with.Fly(2);',
  24493. '$with.Fly(2);',
  24494. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24495. '']));
  24496. end;
  24497. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24498. begin
  24499. StartProgram(false);
  24500. Add([
  24501. '{$modeswitch externalclass}',
  24502. 'type',
  24503. ' TExtA = class external name ''ExtObj''',
  24504. ' procedure Run(w: word = 10);',
  24505. ' end;',
  24506. ' THelper = class helper for TExtA',
  24507. ' class procedure Fly;',
  24508. ' end;',
  24509. 'class procedure THelper.Fly;',
  24510. 'begin end;',
  24511. 'begin',
  24512. '']);
  24513. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24514. nHelperClassMethodForExtClassMustBeStatic);
  24515. ConvertProgram;
  24516. end;
  24517. procedure TTestModule.TestRecordHelper_ClassVar;
  24518. begin
  24519. StartProgram(false);
  24520. Add([
  24521. 'type',
  24522. ' TRec = record',
  24523. ' end;',
  24524. ' THelper = record helper for TRec',
  24525. ' const',
  24526. ' One = 1;',
  24527. ' Two: word = 2;',
  24528. ' class var',
  24529. ' Glob: word;',
  24530. ' function Foo(w: word): word;',
  24531. ' class function Bar(w: word): word; static;',
  24532. ' end;',
  24533. 'function THelper.foo(w: word): word;',
  24534. 'begin',
  24535. ' Result:=w;',
  24536. ' Two:=One+w;',
  24537. ' Glob:=Glob;',
  24538. ' Result:=Self.Glob;',
  24539. ' Self.Glob:=Self.Glob;',
  24540. ' with Self do Glob:=Glob;',
  24541. ' Self:=Self;',
  24542. 'end;',
  24543. 'class function THelper.bar(w: word): word;',
  24544. 'begin',
  24545. ' Result:=w;',
  24546. ' Two:=One;',
  24547. ' Glob:=Glob;',
  24548. 'end;',
  24549. 'var r: TRec;',
  24550. 'begin',
  24551. ' trec.two:=trec.one;',
  24552. ' trec.Glob:=trec.Glob;',
  24553. ' with trec do begin',
  24554. ' two:=one;',
  24555. ' Glob:=Glob;',
  24556. ' end;',
  24557. ' r.two:=r.one;',
  24558. ' r.Glob:=r.Glob;',
  24559. ' with r do begin',
  24560. ' two:=one;',
  24561. ' Glob:=Glob;',
  24562. ' end;',
  24563. '']);
  24564. ConvertProgram;
  24565. CheckSource('TestRecordHelper_ClassVar',
  24566. LinesToStr([ // statements
  24567. 'rtl.recNewT(this, "TRec", function () {',
  24568. ' this.$eq = function (b) {',
  24569. ' return true;',
  24570. ' };',
  24571. ' this.$assign = function (s) {',
  24572. ' return this;',
  24573. ' };',
  24574. '});',
  24575. 'rtl.createHelper(this, "THelper", null, function () {',
  24576. ' this.One = 1;',
  24577. ' this.Two = 2;',
  24578. ' this.Glob = 0;',
  24579. ' this.Foo = function (w) {',
  24580. ' var Result = 0;',
  24581. ' Result = w;',
  24582. ' $mod.THelper.Two = 1 + w;',
  24583. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24584. ' Result = $mod.THelper.Glob;',
  24585. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24586. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24587. ' this.$assign(this);',
  24588. ' return Result;',
  24589. ' };',
  24590. ' this.Bar = function (w) {',
  24591. ' var Result = 0;',
  24592. ' Result = w;',
  24593. ' $mod.THelper.Two = 1;',
  24594. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24595. ' return Result;',
  24596. ' };',
  24597. '});',
  24598. 'this.r = this.TRec.$new();',
  24599. '']),
  24600. LinesToStr([ // $mod.$main
  24601. '$mod.THelper.Two = 1;',
  24602. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24603. 'var $with = $mod.TRec;',
  24604. '$mod.THelper.Two = 1;',
  24605. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24606. '$mod.THelper.Two = 1;',
  24607. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24608. 'var $with1 = $mod.r;',
  24609. '$mod.THelper.Two = 1;',
  24610. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24611. '']));
  24612. end;
  24613. procedure TTestModule.TestRecordHelper_Method_Call;
  24614. begin
  24615. StartProgram(false);
  24616. Add([
  24617. '{$modeswitch AdvancedRecords}',
  24618. 'type',
  24619. ' TRec = record',
  24620. ' procedure Run(w: word = 10);',
  24621. ' end;',
  24622. ' THelper = record helper for TRec',
  24623. ' function Foo(w: word = 1): word;',
  24624. ' end;',
  24625. 'procedure TRec.Run(w: word);',
  24626. 'begin',
  24627. ' Foo;',
  24628. ' Foo();',
  24629. ' Foo(2);',
  24630. ' Self.Foo;',
  24631. ' Self.Foo();',
  24632. ' Self.Foo(3);',
  24633. ' with Self do begin',
  24634. ' Foo;',
  24635. ' Foo();',
  24636. ' Foo(4);',
  24637. ' end;',
  24638. 'end;',
  24639. 'function THelper.foo(w: word): word;',
  24640. 'begin',
  24641. ' Run;',
  24642. ' Run();',
  24643. ' Run(11);',
  24644. ' Foo;',
  24645. ' Foo();',
  24646. ' Foo(12);',
  24647. ' Self.Foo;',
  24648. ' Self.Foo();',
  24649. ' Self.Foo(13);',
  24650. ' with Self do begin',
  24651. ' Foo;',
  24652. ' Foo();',
  24653. ' Foo(14);',
  24654. ' end;',
  24655. 'end;',
  24656. 'var Rec: TRec;',
  24657. 'begin',
  24658. ' Rec.Foo;',
  24659. ' Rec.Foo();',
  24660. ' Rec.Foo(21);',
  24661. ' with Rec do begin',
  24662. ' Foo;',
  24663. ' Foo();',
  24664. ' Foo(22);',
  24665. ' end;',
  24666. '']);
  24667. ConvertProgram;
  24668. CheckSource('TestRecordHelper_Method_Call',
  24669. LinesToStr([ // statements
  24670. 'rtl.recNewT(this, "TRec", function () {',
  24671. ' this.$eq = function (b) {',
  24672. ' return true;',
  24673. ' };',
  24674. ' this.$assign = function (s) {',
  24675. ' return this;',
  24676. ' };',
  24677. ' this.Run = function (w) {',
  24678. ' $mod.THelper.Foo.call(this, 1);',
  24679. ' $mod.THelper.Foo.call(this, 1);',
  24680. ' $mod.THelper.Foo.call(this, 2);',
  24681. ' $mod.THelper.Foo.call(this, 1);',
  24682. ' $mod.THelper.Foo.call(this, 1);',
  24683. ' $mod.THelper.Foo.call(this, 3);',
  24684. ' $mod.THelper.Foo.call(this, 1);',
  24685. ' $mod.THelper.Foo.call(this, 1);',
  24686. ' $mod.THelper.Foo.call(this, 4);',
  24687. ' };',
  24688. '});',
  24689. 'rtl.createHelper(this, "THelper", null, function () {',
  24690. ' this.Foo = function (w) {',
  24691. ' var Result = 0;',
  24692. ' this.Run(10);',
  24693. ' this.Run(10);',
  24694. ' this.Run(11);',
  24695. ' $mod.THelper.Foo.call(this, 1);',
  24696. ' $mod.THelper.Foo.call(this, 1);',
  24697. ' $mod.THelper.Foo.call(this, 12);',
  24698. ' $mod.THelper.Foo.call(this, 1);',
  24699. ' $mod.THelper.Foo.call(this, 1);',
  24700. ' $mod.THelper.Foo.call(this, 13);',
  24701. ' $mod.THelper.Foo.call(this, 1);',
  24702. ' $mod.THelper.Foo.call(this, 1);',
  24703. ' $mod.THelper.Foo.call(this, 14);',
  24704. ' return Result;',
  24705. ' };',
  24706. '});',
  24707. 'this.Rec = this.TRec.$new();',
  24708. '']),
  24709. LinesToStr([ // $mod.$main
  24710. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24711. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24712. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24713. 'var $with = $mod.Rec;',
  24714. '$mod.THelper.Foo.call($with, 1);',
  24715. '$mod.THelper.Foo.call($with, 1);',
  24716. '$mod.THelper.Foo.call($with, 22);',
  24717. '']));
  24718. end;
  24719. procedure TTestModule.TestRecordHelper_Constructor;
  24720. begin
  24721. StartProgram(false);
  24722. Add([
  24723. '{$modeswitch AdvancedRecords}',
  24724. 'type',
  24725. ' TRec = record',
  24726. ' constructor Create(w: word);',
  24727. ' end;',
  24728. ' THelper = record helper for TRec',
  24729. ' constructor NewHlp(w: word);',
  24730. ' end;',
  24731. 'var',
  24732. ' Rec: TRec;',
  24733. 'constructor TRec.Create(w: word);',
  24734. 'begin',
  24735. ' NewHlp(2);', // normal call
  24736. ' trec.NewHlp(3);', // new instance
  24737. 'end;',
  24738. 'constructor THelper.NewHlp(w: word);',
  24739. 'begin',
  24740. ' create(2);', // normal call
  24741. ' trec.create(3);', // new instance
  24742. ' NewHlp(4);', // normal call
  24743. ' trec.NewHlp(5);', // new instance
  24744. 'end;',
  24745. 'begin',
  24746. ' rec.newhlp(2);', // normal call
  24747. ' with rec do newhlp(12);', // normal call
  24748. ' trec.newhlp(3);', // new instance
  24749. ' with trec do newhlp(13);', // new instance
  24750. '']);
  24751. ConvertProgram;
  24752. CheckSource('TestRecordHelper_Constructor',
  24753. LinesToStr([ // statements
  24754. 'rtl.recNewT(this, "TRec", function () {',
  24755. ' this.$eq = function (b) {',
  24756. ' return true;',
  24757. ' };',
  24758. ' this.$assign = function (s) {',
  24759. ' return this;',
  24760. ' };',
  24761. ' this.Create = function (w) {',
  24762. ' $mod.THelper.NewHlp.call(this, 2);',
  24763. ' $mod.THelper.$new("NewHlp", [3]);',
  24764. ' return this;',
  24765. ' };',
  24766. '});',
  24767. 'rtl.createHelper(this, "THelper", null, function () {',
  24768. ' this.NewHlp = function (w) {',
  24769. ' this.Create(2);',
  24770. ' $mod.TRec.$new().Create(3);',
  24771. ' $mod.THelper.NewHlp.call(this, 4);',
  24772. ' $mod.THelper.$new("NewHlp", [5]);',
  24773. ' return this;',
  24774. ' };',
  24775. ' this.$new = function (fn, args) {',
  24776. ' return this[fn].apply($mod.TRec.$new(), args);',
  24777. ' };',
  24778. '});',
  24779. 'this.Rec = this.TRec.$new();',
  24780. '']),
  24781. LinesToStr([ // $mod.$main
  24782. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24783. 'var $with = $mod.Rec;',
  24784. '$mod.THelper.NewHlp.call($with, 12);',
  24785. '$mod.THelper.$new("NewHlp", [3]);',
  24786. 'var $with1 = $mod.TRec;',
  24787. '$mod.THelper.$new("NewHlp", [13]);',
  24788. '']));
  24789. end;
  24790. procedure TTestModule.TestTypeHelper_ClassVar;
  24791. begin
  24792. StartProgram(false);
  24793. Add([
  24794. '{$modeswitch typehelpers}',
  24795. 'type',
  24796. ' THelper = type helper for byte',
  24797. ' const',
  24798. ' One = 1;',
  24799. ' Two: word = 2;',
  24800. ' class var',
  24801. ' Glob: word;',
  24802. ' function Foo(w: word): word;',
  24803. ' class function Bar(w: word): word; static;',
  24804. ' end;',
  24805. 'function THelper.foo(w: word): word;',
  24806. 'begin',
  24807. ' Result:=w;',
  24808. ' Two:=One+w;',
  24809. ' Glob:=Glob;',
  24810. ' Result:=Self.Glob;',
  24811. ' Self.Glob:=Self.Glob;',
  24812. ' with Self do Glob:=Glob;',
  24813. 'end;',
  24814. 'class function THelper.bar(w: word): word;',
  24815. 'begin',
  24816. ' Result:=w;',
  24817. ' Two:=One;',
  24818. ' Glob:=Glob;',
  24819. 'end;',
  24820. 'var b: byte;',
  24821. 'begin',
  24822. ' byte.two:=byte.one;',
  24823. ' byte.Glob:=byte.Glob;',
  24824. ' with byte do begin',
  24825. ' two:=one;',
  24826. ' Glob:=Glob;',
  24827. ' end;',
  24828. ' b.two:=b.one;',
  24829. ' b.Glob:=b.Glob;',
  24830. ' with b do begin',
  24831. ' two:=one;',
  24832. ' Glob:=Glob;',
  24833. ' end;',
  24834. '']);
  24835. ConvertProgram;
  24836. CheckSource('TestTypeHelper_ClassVar',
  24837. LinesToStr([ // statements
  24838. 'rtl.createHelper(this, "THelper", null, function () {',
  24839. ' this.One = 1;',
  24840. ' this.Two = 2;',
  24841. ' this.Glob = 0;',
  24842. ' this.Foo = function (w) {',
  24843. ' var Result = 0;',
  24844. ' Result = w;',
  24845. ' $mod.THelper.Two = 1 + w;',
  24846. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24847. ' Result = $mod.THelper.Glob;',
  24848. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24849. ' var $with = this.get();',
  24850. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24851. ' return Result;',
  24852. ' };',
  24853. ' this.Bar = function (w) {',
  24854. ' var Result = 0;',
  24855. ' Result = w;',
  24856. ' $mod.THelper.Two = 1;',
  24857. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24858. ' return Result;',
  24859. ' };',
  24860. '});',
  24861. 'this.b = 0;',
  24862. '']),
  24863. LinesToStr([ // $mod.$main
  24864. '$mod.THelper.Two = 1;',
  24865. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24866. '$mod.THelper.Two = 1;',
  24867. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24868. '$mod.THelper.Two = 1;',
  24869. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24870. 'var $with = $mod.b;',
  24871. '$mod.THelper.Two = 1;',
  24872. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24873. '']));
  24874. end;
  24875. procedure TTestModule.TestTypeHelper_PassResultElement;
  24876. begin
  24877. StartProgram(false);
  24878. Add([
  24879. '{$modeswitch typehelpers}',
  24880. 'type',
  24881. ' THelper = type helper for word',
  24882. ' procedure DoIt(e: byte = 123);',
  24883. ' class procedure DoSome(e: byte = 456); static;',
  24884. ' end;',
  24885. 'procedure THelper.DoIt(e: byte);',
  24886. 'begin',
  24887. 'end;',
  24888. 'class procedure THelper.DoSome(e: byte);',
  24889. 'begin',
  24890. 'end;',
  24891. 'function Foo(w: word): word;',
  24892. 'begin',
  24893. ' Result.DoIt;',
  24894. ' Result.DoIt();',
  24895. ' Result.DoSome;',
  24896. ' Result.DoSome();',
  24897. ' with Result do begin',
  24898. ' DoIt;',
  24899. ' DoIt();',
  24900. ' DoSome;',
  24901. ' DoSome();',
  24902. ' end;',
  24903. 'end;',
  24904. 'begin',
  24905. '']);
  24906. ConvertProgram;
  24907. CheckSource('TestTypeHelper_PassResultElement',
  24908. LinesToStr([ // statements
  24909. 'rtl.createHelper(this, "THelper", null, function () {',
  24910. ' this.DoIt = function (e) {',
  24911. ' };',
  24912. ' this.DoSome = function (e) {',
  24913. ' };',
  24914. '});',
  24915. 'this.Foo = function (w) {',
  24916. ' var Result = 0;',
  24917. ' $mod.THelper.DoIt.call({',
  24918. ' get: function () {',
  24919. ' return Result;',
  24920. ' },',
  24921. ' set: function (v) {',
  24922. ' Result = v;',
  24923. ' }',
  24924. ' }, 123);',
  24925. ' $mod.THelper.DoIt.call({',
  24926. ' get: function () {',
  24927. ' return Result;',
  24928. ' },',
  24929. ' set: function (v) {',
  24930. ' Result = v;',
  24931. ' }',
  24932. ' }, 123);',
  24933. ' $mod.THelper.DoSome(456);',
  24934. ' $mod.THelper.DoSome(456);',
  24935. ' $mod.THelper.DoIt.call({',
  24936. ' get: function () {',
  24937. ' return Result;',
  24938. ' },',
  24939. ' set: function (v) {',
  24940. ' Result = v;',
  24941. ' }',
  24942. ' }, 123);',
  24943. ' $mod.THelper.DoIt.call({',
  24944. ' get: function () {',
  24945. ' return Result;',
  24946. ' },',
  24947. ' set: function (v) {',
  24948. ' Result = v;',
  24949. ' }',
  24950. ' }, 123);',
  24951. ' $mod.THelper.DoSome(456);',
  24952. ' $mod.THelper.DoSome(456);',
  24953. ' return Result;',
  24954. '};',
  24955. '']),
  24956. LinesToStr([ // $mod.$main
  24957. '']));
  24958. end;
  24959. procedure TTestModule.TestTypeHelper_PassArgs;
  24960. begin
  24961. StartProgram(false);
  24962. Add([
  24963. '{$modeswitch typehelpers}',
  24964. 'type',
  24965. ' THelper = type helper for word',
  24966. ' procedure DoIt(e: byte = 123);',
  24967. ' end;',
  24968. 'procedure THelper.DoIt(e: byte);',
  24969. 'begin',
  24970. 'end;',
  24971. 'procedure FooDefault(a: word);',
  24972. 'begin',
  24973. ' a.DoIt;',
  24974. ' with a do DoIt;',
  24975. 'end;',
  24976. 'procedure FooConst(const a: word);',
  24977. 'begin',
  24978. ' a.DoIt;',
  24979. ' with a do DoIt;',
  24980. 'end;',
  24981. 'procedure FooVar(var a: word);',
  24982. 'begin',
  24983. ' a.DoIt;',
  24984. ' with a do DoIt;',
  24985. 'end;',
  24986. 'begin',
  24987. '']);
  24988. ConvertProgram;
  24989. CheckSource('TestTypeHelper_PassArgs',
  24990. LinesToStr([ // statements
  24991. 'rtl.createHelper(this, "THelper", null, function () {',
  24992. ' this.DoIt = function (e) {',
  24993. ' };',
  24994. '});',
  24995. 'this.FooDefault = function (a) {',
  24996. ' $mod.THelper.DoIt.call({',
  24997. ' get: function () {',
  24998. ' return a;',
  24999. ' },',
  25000. ' set: function (v) {',
  25001. ' a = v;',
  25002. ' }',
  25003. ' }, 123);',
  25004. ' $mod.THelper.DoIt.call({',
  25005. ' get: function () {',
  25006. ' return a;',
  25007. ' },',
  25008. ' set: function (v) {',
  25009. ' a = v;',
  25010. ' }',
  25011. ' }, 123);',
  25012. '};',
  25013. 'this.FooConst = function (a) {',
  25014. ' $mod.THelper.DoIt.call({',
  25015. ' get: function () {',
  25016. ' return a;',
  25017. ' },',
  25018. ' set: function (v) {',
  25019. ' rtl.raiseE("EPropReadOnly");',
  25020. ' }',
  25021. ' }, 123);',
  25022. ' $mod.THelper.DoIt.call({',
  25023. ' get: function () {',
  25024. ' return a;',
  25025. ' },',
  25026. ' set: function () {',
  25027. ' rtl.raiseE("EPropReadOnly");',
  25028. ' }',
  25029. ' }, 123);',
  25030. '};',
  25031. 'this.FooVar = function (a) {',
  25032. ' $mod.THelper.DoIt.call(a, 123);',
  25033. ' var $with = a.get();',
  25034. ' $mod.THelper.DoIt.call(a, 123);',
  25035. '};',
  25036. '']),
  25037. LinesToStr([ // $mod.$main
  25038. '']));
  25039. end;
  25040. procedure TTestModule.TestTypeHelper_PassVarConst;
  25041. begin
  25042. StartProgram(false);
  25043. Add([
  25044. '{$modeswitch typehelpers}',
  25045. 'type',
  25046. ' THelper = type helper for word',
  25047. ' procedure DoIt(e: byte = 123);',
  25048. ' end;',
  25049. 'procedure THelper.DoIt(e: byte);',
  25050. 'begin',
  25051. 'end;',
  25052. 'var a: word;',
  25053. 'const c: word = 2;',
  25054. '{$writeableconst off}',
  25055. 'const r: word = 3;',
  25056. 'begin',
  25057. ' a.DoIt;',
  25058. ' with a do DoIt;',
  25059. ' c.DoIt;',
  25060. ' with c do DoIt;',
  25061. ' r.DoIt;',
  25062. ' with r do DoIt;',
  25063. '']);
  25064. ConvertProgram;
  25065. CheckSource('TestTypeHelper_PassVarConst',
  25066. LinesToStr([ // statements
  25067. 'rtl.createHelper(this, "THelper", null, function () {',
  25068. ' this.DoIt = function (e) {',
  25069. ' };',
  25070. '});',
  25071. 'this.a = 0;',
  25072. 'this.c = 2;',
  25073. 'this.r = 3;',
  25074. '']),
  25075. LinesToStr([ // $mod.$main
  25076. '$mod.THelper.DoIt.call({',
  25077. ' p: $mod,',
  25078. ' get: function () {',
  25079. ' return this.p.a;',
  25080. ' },',
  25081. ' set: function (v) {',
  25082. ' this.p.a = v;',
  25083. ' }',
  25084. '}, 123);',
  25085. 'var $with = $mod.a;',
  25086. '$mod.THelper.DoIt.call({',
  25087. ' get: function () {',
  25088. ' return $with;',
  25089. ' },',
  25090. ' set: function (v) {',
  25091. ' $with = v;',
  25092. ' }',
  25093. '}, 123);',
  25094. '$mod.THelper.DoIt.call({',
  25095. ' p: $mod,',
  25096. ' get: function () {',
  25097. ' return this.p.c;',
  25098. ' },',
  25099. ' set: function (v) {',
  25100. ' this.p.c = v;',
  25101. ' }',
  25102. '}, 123);',
  25103. 'var $with1 = $mod.c;',
  25104. '$mod.THelper.DoIt.call({',
  25105. ' get: function () {',
  25106. ' return $with1;',
  25107. ' },',
  25108. ' set: function (v) {',
  25109. ' $with1 = v;',
  25110. ' }',
  25111. '}, 123);',
  25112. '$mod.THelper.DoIt.call({',
  25113. ' get: function () {',
  25114. ' return 3;',
  25115. ' },',
  25116. ' set: function (v) {',
  25117. ' rtl.raiseE("EPropReadOnly");',
  25118. ' }',
  25119. '}, 123);',
  25120. 'var $with2 = 3;',
  25121. ' $mod.THelper.DoIt.call({',
  25122. ' get: function () {',
  25123. ' return $with2;',
  25124. ' },',
  25125. ' set: function () {',
  25126. ' rtl.raiseE("EPropReadOnly");',
  25127. ' }',
  25128. ' }, 123);',
  25129. '']));
  25130. end;
  25131. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25132. begin
  25133. StartProgram(false);
  25134. Add([
  25135. '{$modeswitch typehelpers}',
  25136. 'type',
  25137. ' THelper = type helper for word',
  25138. ' procedure DoIt(e: byte = 123);',
  25139. ' end;',
  25140. 'procedure THelper.DoIt(e: byte);',
  25141. 'begin',
  25142. 'end;',
  25143. 'function Foo(b: byte = 1): word;',
  25144. 'begin',
  25145. 'end;',
  25146. 'begin',
  25147. ' Foo.DoIt;',
  25148. ' Foo().DoIt;',
  25149. ' with Foo do DoIt;',
  25150. ' with Foo() do DoIt;',
  25151. '']);
  25152. ConvertProgram;
  25153. CheckSource('TestTypeHelper_PassFuncResult',
  25154. LinesToStr([ // statements
  25155. 'rtl.createHelper(this, "THelper", null, function () {',
  25156. ' this.DoIt = function (e) {',
  25157. ' };',
  25158. '});',
  25159. 'this.Foo = function (b) {',
  25160. ' var Result = 0;',
  25161. ' return Result;',
  25162. '};',
  25163. '']),
  25164. LinesToStr([ // $mod.$main
  25165. '$mod.THelper.DoIt.call({',
  25166. ' a: $mod.Foo(1),',
  25167. ' get: function () {',
  25168. ' return this.a;',
  25169. ' },',
  25170. ' set: function (v) {',
  25171. ' this.a = v;',
  25172. ' }',
  25173. '}, 123);',
  25174. '$mod.THelper.DoIt.call({',
  25175. ' a: $mod.Foo(1),',
  25176. ' get: function () {',
  25177. ' return this.a;',
  25178. ' },',
  25179. ' set: function (v) {',
  25180. ' this.a = v;',
  25181. ' }',
  25182. '}, 123);',
  25183. 'var $with = $mod.Foo(1);',
  25184. '$mod.THelper.DoIt.call({',
  25185. ' get: function () {',
  25186. ' return $with;',
  25187. ' },',
  25188. ' set: function (v) {',
  25189. ' $with = v;',
  25190. ' }',
  25191. '}, 123);',
  25192. 'var $with1 = $mod.Foo(1);',
  25193. '$mod.THelper.DoIt.call({',
  25194. ' get: function () {',
  25195. ' return $with1;',
  25196. ' },',
  25197. ' set: function (v) {',
  25198. ' $with1 = v;',
  25199. ' }',
  25200. '}, 123);',
  25201. '']));
  25202. end;
  25203. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25204. begin
  25205. StartProgram(false);
  25206. Add([
  25207. '{$modeswitch typehelpers}',
  25208. 'type',
  25209. ' TObject = class',
  25210. ' FField: word;',
  25211. ' procedure SetField(Value: word);',
  25212. ' property Field: word read FField write SetField;',
  25213. ' end;',
  25214. ' THelper = type helper for word',
  25215. ' procedure Fly;',
  25216. ' class procedure Run; static;',
  25217. ' end;',
  25218. 'procedure TObject.SetField(Value: word);',
  25219. 'begin',
  25220. ' Field.Fly;',
  25221. ' Field.Run;',
  25222. ' Self.Field.Fly;',
  25223. ' Self.Field.Run;',
  25224. ' with Self do begin',
  25225. ' Field.Fly;',
  25226. ' Field.Run;',
  25227. ' end;',
  25228. ' with Self.Field do begin',
  25229. ' Fly;',
  25230. ' Run;',
  25231. ' end;',
  25232. 'end;',
  25233. 'procedure THelper.Fly;',
  25234. 'begin',
  25235. 'end;',
  25236. 'class procedure THelper.Run;',
  25237. 'begin',
  25238. 'end;',
  25239. 'var',
  25240. ' o: TObject;',
  25241. 'begin',
  25242. ' o.Field.Fly;',
  25243. ' o.Field.Run;',
  25244. ' with o do begin',
  25245. ' Field.Fly;',
  25246. ' Field.Run;',
  25247. ' end;',
  25248. ' with o.Field do begin',
  25249. ' Fly;',
  25250. ' Run;',
  25251. ' end;',
  25252. '']);
  25253. ConvertProgram;
  25254. CheckSource('TestTypeHelper_PassPropertyField',
  25255. LinesToStr([ // statements
  25256. 'rtl.createClass(this, "TObject", null, function () {',
  25257. ' this.$init = function () {',
  25258. ' this.FField = 0;',
  25259. ' };',
  25260. ' this.$final = function () {',
  25261. ' };',
  25262. ' this.SetField = function (Value) {',
  25263. ' $mod.THelper.Fly.call({',
  25264. ' p: this,',
  25265. ' get: function () {',
  25266. ' return this.p.FField;',
  25267. ' },',
  25268. ' set: function (v) {',
  25269. ' this.p.FField = v;',
  25270. ' }',
  25271. ' });',
  25272. ' $mod.THelper.Run();',
  25273. ' $mod.THelper.Fly.call({',
  25274. ' p: this,',
  25275. ' get: function () {',
  25276. ' return this.p.FField;',
  25277. ' },',
  25278. ' set: function (v) {',
  25279. ' this.p.FField = v;',
  25280. ' }',
  25281. ' });',
  25282. ' $mod.THelper.Run();',
  25283. ' $mod.THelper.Fly.call({',
  25284. ' p: this,',
  25285. ' get: function () {',
  25286. ' return this.p.FField;',
  25287. ' },',
  25288. ' set: function (v) {',
  25289. ' this.p.FField = v;',
  25290. ' }',
  25291. ' });',
  25292. ' $mod.THelper.Run();',
  25293. ' var $with = this.FField;',
  25294. ' $mod.THelper.Fly.call({',
  25295. ' get: function () {',
  25296. ' return $with;',
  25297. ' },',
  25298. ' set: function (v) {',
  25299. ' $with = v;',
  25300. ' }',
  25301. ' });',
  25302. ' $mod.THelper.Run();',
  25303. ' };',
  25304. '});',
  25305. 'rtl.createHelper(this, "THelper", null, function () {',
  25306. ' this.Fly = function () {',
  25307. ' };',
  25308. ' this.Run = function () {',
  25309. ' };',
  25310. '});',
  25311. 'this.o = null;',
  25312. '']),
  25313. LinesToStr([ // $mod.$main
  25314. '$mod.THelper.Fly.call({',
  25315. ' p: $mod.o,',
  25316. ' get: function () {',
  25317. ' return this.p.FField;',
  25318. ' },',
  25319. ' set: function (v) {',
  25320. ' this.p.FField = v;',
  25321. ' }',
  25322. '});',
  25323. '$mod.THelper.Run();',
  25324. 'var $with = $mod.o;',
  25325. '$mod.THelper.Fly.call({',
  25326. ' p: $with,',
  25327. ' get: function () {',
  25328. ' return this.p.FField;',
  25329. ' },',
  25330. ' set: function (v) {',
  25331. ' this.p.FField = v;',
  25332. ' }',
  25333. '});',
  25334. '$mod.THelper.Run();',
  25335. 'var $with1 = $mod.o.FField;',
  25336. '$mod.THelper.Fly.call({',
  25337. ' get: function () {',
  25338. ' return $with1;',
  25339. ' },',
  25340. ' set: function (v) {',
  25341. ' $with1 = v;',
  25342. ' }',
  25343. '});',
  25344. '$mod.THelper.Run();',
  25345. '']));
  25346. end;
  25347. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25348. begin
  25349. StartProgram(false);
  25350. Add([
  25351. '{$modeswitch typehelpers}',
  25352. 'type',
  25353. ' TObject = class',
  25354. ' FField: word;',
  25355. ' function GetField: word;',
  25356. ' property Field: word read GetField write FField;',
  25357. ' end;',
  25358. ' THelper = type helper for word',
  25359. ' procedure Fly;',
  25360. ' class procedure Run; static;',
  25361. ' end;',
  25362. 'function TObject.GetField: word;',
  25363. 'begin',
  25364. ' Field.Fly;',
  25365. ' Field.Run;',
  25366. ' Self.Field.Fly;',
  25367. ' Self.Field.Run;',
  25368. ' with Self do begin',
  25369. ' Field.Fly;',
  25370. ' Field.Run;',
  25371. ' end;',
  25372. ' with Self.Field do begin',
  25373. ' Fly;',
  25374. ' Run;',
  25375. ' end;',
  25376. 'end;',
  25377. 'procedure THelper.Fly;',
  25378. 'begin',
  25379. 'end;',
  25380. 'class procedure THelper.Run;',
  25381. 'begin',
  25382. 'end;',
  25383. 'var',
  25384. ' o: TObject;',
  25385. 'begin',
  25386. ' o.Field.Fly;',
  25387. ' o.Field.Run;',
  25388. ' with o do begin',
  25389. ' Field.Fly;',
  25390. ' Field.Run;',
  25391. ' end;',
  25392. ' with o.Field do begin',
  25393. ' Fly;',
  25394. ' Run;',
  25395. ' end;',
  25396. '']);
  25397. ConvertProgram;
  25398. CheckSource('TestTypeHelper_PassPropertyGetter',
  25399. LinesToStr([ // statements
  25400. 'rtl.createClass(this, "TObject", null, function () {',
  25401. ' this.$init = function () {',
  25402. ' this.FField = 0;',
  25403. ' };',
  25404. ' this.$final = function () {',
  25405. ' };',
  25406. ' this.GetField = function () {',
  25407. ' var Result = 0;',
  25408. ' $mod.THelper.Fly.call({',
  25409. ' p: this.GetField(),',
  25410. ' get: function () {',
  25411. ' return this.p;',
  25412. ' },',
  25413. ' set: function (v) {',
  25414. ' this.p = v;',
  25415. ' }',
  25416. ' });',
  25417. ' $mod.THelper.Run();',
  25418. ' $mod.THelper.Fly.call({',
  25419. ' p: this.GetField(),',
  25420. ' get: function () {',
  25421. ' return this.p;',
  25422. ' },',
  25423. ' set: function (v) {',
  25424. ' this.p = v;',
  25425. ' }',
  25426. ' });',
  25427. ' $mod.THelper.Run();',
  25428. ' $mod.THelper.Fly.call({',
  25429. ' p: this.GetField(),',
  25430. ' get: function () {',
  25431. ' return this.p;',
  25432. ' },',
  25433. ' set: function (v) {',
  25434. ' this.p = v;',
  25435. ' }',
  25436. ' });',
  25437. ' $mod.THelper.Run();',
  25438. ' var $with = this.GetField();',
  25439. ' $mod.THelper.Fly.call({',
  25440. ' get: function () {',
  25441. ' return $with;',
  25442. ' },',
  25443. ' set: function (v) {',
  25444. ' $with = v;',
  25445. ' }',
  25446. ' });',
  25447. ' $mod.THelper.Run();',
  25448. ' return Result;',
  25449. ' };',
  25450. '});',
  25451. 'rtl.createHelper(this, "THelper", null, function () {',
  25452. ' this.Fly = function () {',
  25453. ' };',
  25454. ' this.Run = function () {',
  25455. ' };',
  25456. '});',
  25457. 'this.o = null;',
  25458. '']),
  25459. LinesToStr([ // $mod.$main
  25460. '$mod.THelper.Fly.call({',
  25461. ' p: $mod.o.GetField(),',
  25462. ' get: function () {',
  25463. ' return this.p;',
  25464. ' },',
  25465. ' set: function (v) {',
  25466. ' this.p = v;',
  25467. ' }',
  25468. '});',
  25469. '$mod.THelper.Run();',
  25470. 'var $with = $mod.o;',
  25471. '$mod.THelper.Fly.call({',
  25472. ' p: $with.GetField(),',
  25473. ' get: function () {',
  25474. ' return this.p;',
  25475. ' },',
  25476. ' set: function (v) {',
  25477. ' this.p = v;',
  25478. ' }',
  25479. '});',
  25480. '$mod.THelper.Run();',
  25481. 'var $with1 = $mod.o.GetField();',
  25482. '$mod.THelper.Fly.call({',
  25483. ' get: function () {',
  25484. ' return $with1;',
  25485. ' },',
  25486. ' set: function (v) {',
  25487. ' $with1 = v;',
  25488. ' }',
  25489. '});',
  25490. '$mod.THelper.Run();',
  25491. '']));
  25492. end;
  25493. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25494. begin
  25495. StartProgram(false);
  25496. Add([
  25497. '{$modeswitch typehelpers}',
  25498. 'type',
  25499. ' TObject = class',
  25500. ' class var FField: word;',
  25501. ' class procedure SetField(Value: word);',
  25502. ' class property Field: word read FField write SetField;',
  25503. ' end;',
  25504. ' THelper = type helper for word',
  25505. ' procedure Fly(n: byte);',
  25506. ' end;',
  25507. 'class procedure TObject.SetField(Value: word);',
  25508. 'begin',
  25509. ' Field.Fly(1);',
  25510. ' Self.Field.Fly(2);',
  25511. ' with Self do Field.Fly(3);',
  25512. ' with Self.Field do Fly(4);',
  25513. ' TObject.Field.Fly(5);',
  25514. ' with TObject do Field.Fly(6);',
  25515. ' with TObject.Field do Fly(7);',
  25516. 'end;',
  25517. 'procedure THelper.Fly(n: byte);',
  25518. 'begin',
  25519. 'end;',
  25520. 'var',
  25521. ' o: TObject;',
  25522. 'begin',
  25523. ' o.Field.Fly(11);',
  25524. ' with o do Field.Fly(12);',
  25525. ' with o.Field do Fly(13);',
  25526. ' TObject.Field.Fly(14);',
  25527. ' with TObject do Field.Fly(15);',
  25528. ' with TObject.Field do Fly(16);',
  25529. '']);
  25530. ConvertProgram;
  25531. CheckSource('TestTypeHelper_PassClassPropertyField',
  25532. LinesToStr([ // statements
  25533. 'rtl.createClass(this, "TObject", null, function () {',
  25534. ' this.FField = 0;',
  25535. ' this.$init = function () {',
  25536. ' };',
  25537. ' this.$final = function () {',
  25538. ' };',
  25539. ' this.SetField = function (Value) {',
  25540. ' $mod.THelper.Fly.call({',
  25541. ' p: this,',
  25542. ' get: function () {',
  25543. ' return this.p.FField;',
  25544. ' },',
  25545. ' set: function (v) {',
  25546. ' $mod.TObject.FField = v;',
  25547. ' }',
  25548. ' }, 1);',
  25549. ' $mod.THelper.Fly.call({',
  25550. ' p: this,',
  25551. ' get: function () {',
  25552. ' return this.p.FField;',
  25553. ' },',
  25554. ' set: function (v) {',
  25555. ' $mod.TObject.FField = v;',
  25556. ' }',
  25557. ' }, 2);',
  25558. ' $mod.THelper.Fly.call({',
  25559. ' p: this,',
  25560. ' get: function () {',
  25561. ' return this.p.FField;',
  25562. ' },',
  25563. ' set: function (v) {',
  25564. ' $mod.TObject.FField = v;',
  25565. ' }',
  25566. ' }, 3);',
  25567. ' var $with = this.FField;',
  25568. ' $mod.THelper.Fly.call({',
  25569. ' get: function () {',
  25570. ' return $with;',
  25571. ' },',
  25572. ' set: function (v) {',
  25573. ' $with = v;',
  25574. ' }',
  25575. ' }, 4);',
  25576. ' $mod.THelper.Fly.call({',
  25577. ' p: $mod.TObject,',
  25578. ' get: function () {',
  25579. ' return this.p.FField;',
  25580. ' },',
  25581. ' set: function (v) {',
  25582. ' $mod.TObject.FField = v;',
  25583. ' }',
  25584. ' }, 5);',
  25585. ' var $with1 = $mod.TObject;',
  25586. ' $mod.THelper.Fly.call({',
  25587. ' p: $with1,',
  25588. ' get: function () {',
  25589. ' return this.p.FField;',
  25590. ' },',
  25591. ' set: function (v) {',
  25592. ' $mod.TObject.FField = v;',
  25593. ' }',
  25594. ' }, 6);',
  25595. ' var $with2 = $mod.TObject.FField;',
  25596. ' $mod.THelper.Fly.call({',
  25597. ' get: function () {',
  25598. ' return $with2;',
  25599. ' },',
  25600. ' set: function (v) {',
  25601. ' $with2 = v;',
  25602. ' }',
  25603. ' }, 7);',
  25604. ' };',
  25605. '});',
  25606. 'rtl.createHelper(this, "THelper", null, function () {',
  25607. ' this.Fly = function (n) {',
  25608. ' };',
  25609. '});',
  25610. 'this.o = null;',
  25611. '']),
  25612. LinesToStr([ // $mod.$main
  25613. '$mod.THelper.Fly.call({',
  25614. ' p: $mod.o,',
  25615. ' get: function () {',
  25616. ' return this.p.FField;',
  25617. ' },',
  25618. ' set: function (v) {',
  25619. ' $mod.TObject.FField = v;',
  25620. ' }',
  25621. '}, 11);',
  25622. 'var $with = $mod.o;',
  25623. '$mod.THelper.Fly.call({',
  25624. ' p: $with,',
  25625. ' get: function () {',
  25626. ' return this.p.FField;',
  25627. ' },',
  25628. ' set: function (v) {',
  25629. ' $mod.TObject.FField = v;',
  25630. ' }',
  25631. '}, 12);',
  25632. 'var $with1 = $mod.o.FField;',
  25633. '$mod.THelper.Fly.call({',
  25634. ' get: function () {',
  25635. ' return $with1;',
  25636. ' },',
  25637. ' set: function (v) {',
  25638. ' $with1 = v;',
  25639. ' }',
  25640. '}, 13);',
  25641. '$mod.THelper.Fly.call({',
  25642. ' p: $mod.TObject,',
  25643. ' get: function () {',
  25644. ' return this.p.FField;',
  25645. ' },',
  25646. ' set: function (v) {',
  25647. ' $mod.TObject.FField = v;',
  25648. ' }',
  25649. '}, 14);',
  25650. 'var $with2 = $mod.TObject;',
  25651. '$mod.THelper.Fly.call({',
  25652. ' p: $with2,',
  25653. ' get: function () {',
  25654. ' return this.p.FField;',
  25655. ' },',
  25656. ' set: function (v) {',
  25657. ' $mod.TObject.FField = v;',
  25658. ' }',
  25659. '}, 15);',
  25660. 'var $with3 = $mod.TObject.FField;',
  25661. '$mod.THelper.Fly.call({',
  25662. ' get: function () {',
  25663. ' return $with3;',
  25664. ' },',
  25665. ' set: function (v) {',
  25666. ' $with3 = v;',
  25667. ' }',
  25668. '}, 16);',
  25669. '']));
  25670. end;
  25671. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25672. begin
  25673. StartProgram(false);
  25674. Add([
  25675. '{$modeswitch typehelpers}',
  25676. 'type',
  25677. ' TObject = class',
  25678. ' class var FField: word;',
  25679. ' class function GetField: word; static;',
  25680. ' class property Field: word read GetField write FField;',
  25681. ' end;',
  25682. ' THelper = type helper for word',
  25683. ' procedure Fly(n: byte);',
  25684. ' end;',
  25685. 'class function TObject.GetField: word;',
  25686. 'begin',
  25687. ' Field.Fly(1);',
  25688. ' TObject.Field.Fly(5);',
  25689. ' with TObject do Field.Fly(6);',
  25690. ' with TObject.Field do Fly(7);',
  25691. 'end;',
  25692. 'procedure THelper.Fly(n: byte);',
  25693. 'begin',
  25694. 'end;',
  25695. 'var',
  25696. ' o: TObject;',
  25697. 'begin',
  25698. ' o.Field.Fly(11);',
  25699. ' with o do Field.Fly(12);',
  25700. ' with o.Field do Fly(13);',
  25701. '']);
  25702. ConvertProgram;
  25703. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25704. LinesToStr([ // statements
  25705. 'rtl.createClass(this, "TObject", null, function () {',
  25706. ' this.FField = 0;',
  25707. ' this.$init = function () {',
  25708. ' };',
  25709. ' this.$final = function () {',
  25710. ' };',
  25711. ' this.GetField = function () {',
  25712. ' var Result = 0;',
  25713. ' $mod.THelper.Fly.call({',
  25714. ' p: $mod.TObject.GetField(),',
  25715. ' get: function () {',
  25716. ' return this.p;',
  25717. ' },',
  25718. ' set: function (v) {',
  25719. ' this.p = v;',
  25720. ' }',
  25721. ' }, 1);',
  25722. ' $mod.THelper.Fly.call({',
  25723. ' p: $mod.TObject.GetField(),',
  25724. ' get: function () {',
  25725. ' return this.p;',
  25726. ' },',
  25727. ' set: function (v) {',
  25728. ' this.p = v;',
  25729. ' }',
  25730. ' }, 5);',
  25731. ' var $with = $mod.TObject;',
  25732. ' $mod.THelper.Fly.call({',
  25733. ' p: $with.GetField(),',
  25734. ' get: function () {',
  25735. ' return this.p;',
  25736. ' },',
  25737. ' set: function (v) {',
  25738. ' this.p = v;',
  25739. ' }',
  25740. ' }, 6);',
  25741. ' var $with1 = $mod.TObject.GetField();',
  25742. ' $mod.THelper.Fly.call({',
  25743. ' get: function () {',
  25744. ' return $with1;',
  25745. ' },',
  25746. ' set: function (v) {',
  25747. ' $with1 = v;',
  25748. ' }',
  25749. ' }, 7);',
  25750. ' return Result;',
  25751. ' };',
  25752. '});',
  25753. 'rtl.createHelper(this, "THelper", null, function () {',
  25754. ' this.Fly = function (n) {',
  25755. ' };',
  25756. '});',
  25757. 'this.o = null;',
  25758. '']),
  25759. LinesToStr([ // $mod.$main
  25760. '$mod.THelper.Fly.call({',
  25761. ' p: $mod.TObject.GetField(),',
  25762. ' get: function () {',
  25763. ' return this.p;',
  25764. ' },',
  25765. ' set: function (v) {',
  25766. ' this.p = v;',
  25767. ' }',
  25768. '}, 11);',
  25769. 'var $with = $mod.o;',
  25770. '$mod.THelper.Fly.call({',
  25771. ' p: $with.GetField(),',
  25772. ' get: function () {',
  25773. ' return this.p;',
  25774. ' },',
  25775. ' set: function (v) {',
  25776. ' this.p = v;',
  25777. ' }',
  25778. '}, 12);',
  25779. 'var $with1 = $mod.TObject.GetField();',
  25780. '$mod.THelper.Fly.call({',
  25781. ' get: function () {',
  25782. ' return $with1;',
  25783. ' },',
  25784. ' set: function (v) {',
  25785. ' $with1 = v;',
  25786. ' }',
  25787. '}, 13);',
  25788. '']));
  25789. end;
  25790. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25791. begin
  25792. StartProgram(false);
  25793. Add([
  25794. '{$modeswitch typehelpers}',
  25795. 'type',
  25796. ' TObject = class',
  25797. ' class var FField: word;',
  25798. ' class function GetField: word;',
  25799. ' class property Field: word read GetField write FField;',
  25800. ' end;',
  25801. ' TClass = class of TObject;',
  25802. ' THelper = type helper for word',
  25803. ' procedure Fly(n: byte);',
  25804. ' end;',
  25805. 'class function TObject.GetField: word;',
  25806. 'begin',
  25807. ' Field.Fly(1);',
  25808. ' Self.Field.Fly(5);',
  25809. ' with Self do Field.Fly(6);',
  25810. ' with Self.Field do Fly(7);',
  25811. 'end;',
  25812. 'procedure THelper.Fly(n: byte);',
  25813. 'begin',
  25814. 'end;',
  25815. 'var',
  25816. ' o: TObject;',
  25817. ' c: TClass;',
  25818. 'begin',
  25819. ' o.Field.Fly(11);',
  25820. ' with o do Field.Fly(12);',
  25821. ' with o.Field do Fly(13);',
  25822. ' c.Field.Fly(14);',
  25823. ' with c do Field.Fly(15);',
  25824. ' with c.Field do Fly(16);',
  25825. '']);
  25826. ConvertProgram;
  25827. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25828. LinesToStr([ // statements
  25829. 'rtl.createClass(this, "TObject", null, function () {',
  25830. ' this.FField = 0;',
  25831. ' this.$init = function () {',
  25832. ' };',
  25833. ' this.$final = function () {',
  25834. ' };',
  25835. ' this.GetField = function () {',
  25836. ' var Result = 0;',
  25837. ' $mod.THelper.Fly.call({',
  25838. ' p: this.GetField(),',
  25839. ' get: function () {',
  25840. ' return this.p;',
  25841. ' },',
  25842. ' set: function (v) {',
  25843. ' this.p = v;',
  25844. ' }',
  25845. ' }, 1);',
  25846. ' $mod.THelper.Fly.call({',
  25847. ' p: this.GetField(),',
  25848. ' get: function () {',
  25849. ' return this.p;',
  25850. ' },',
  25851. ' set: function (v) {',
  25852. ' this.p = v;',
  25853. ' }',
  25854. ' }, 5);',
  25855. ' $mod.THelper.Fly.call({',
  25856. ' p: this.GetField(),',
  25857. ' get: function () {',
  25858. ' return this.p;',
  25859. ' },',
  25860. ' set: function (v) {',
  25861. ' this.p = v;',
  25862. ' }',
  25863. ' }, 6);',
  25864. ' var $with = this.GetField();',
  25865. ' $mod.THelper.Fly.call({',
  25866. ' get: function () {',
  25867. ' return $with;',
  25868. ' },',
  25869. ' set: function (v) {',
  25870. ' $with = v;',
  25871. ' }',
  25872. ' }, 7);',
  25873. ' return Result;',
  25874. ' };',
  25875. '});',
  25876. 'rtl.createHelper(this, "THelper", null, function () {',
  25877. ' this.Fly = function (n) {',
  25878. ' };',
  25879. '});',
  25880. 'this.o = null;',
  25881. 'this.c = null;',
  25882. '']),
  25883. LinesToStr([ // $mod.$main
  25884. '$mod.THelper.Fly.call({',
  25885. ' p: $mod.o.$class.GetField(),',
  25886. ' get: function () {',
  25887. ' return this.p;',
  25888. ' },',
  25889. ' set: function (v) {',
  25890. ' this.p = v;',
  25891. ' }',
  25892. '}, 11);',
  25893. 'var $with = $mod.o;',
  25894. '$mod.THelper.Fly.call({',
  25895. ' p: $with.$class.GetField(),',
  25896. ' get: function () {',
  25897. ' return this.p;',
  25898. ' },',
  25899. ' set: function (v) {',
  25900. ' this.p = v;',
  25901. ' }',
  25902. '}, 12);',
  25903. 'var $with1 = $mod.o.$class.GetField();',
  25904. '$mod.THelper.Fly.call({',
  25905. ' get: function () {',
  25906. ' return $with1;',
  25907. ' },',
  25908. ' set: function (v) {',
  25909. ' $with1 = v;',
  25910. ' }',
  25911. '}, 13);',
  25912. '$mod.THelper.Fly.call({',
  25913. ' p: $mod.c.GetField(),',
  25914. ' get: function () {',
  25915. ' return this.p;',
  25916. ' },',
  25917. ' set: function (v) {',
  25918. ' this.p = v;',
  25919. ' }',
  25920. '}, 14);',
  25921. 'var $with2 = $mod.c;',
  25922. '$mod.THelper.Fly.call({',
  25923. ' p: $with2.GetField(),',
  25924. ' get: function () {',
  25925. ' return this.p;',
  25926. ' },',
  25927. ' set: function (v) {',
  25928. ' this.p = v;',
  25929. ' }',
  25930. '}, 15);',
  25931. 'var $with3 = $mod.c.GetField();',
  25932. '$mod.THelper.Fly.call({',
  25933. ' get: function () {',
  25934. ' return $with3;',
  25935. ' },',
  25936. ' set: function (v) {',
  25937. ' $with3 = v;',
  25938. ' }',
  25939. '}, 16);',
  25940. '']));
  25941. end;
  25942. procedure TTestModule.TestTypeHelper_Property;
  25943. begin
  25944. StartProgram(false);
  25945. Add([
  25946. '{$modeswitch typehelpers}',
  25947. 'type',
  25948. ' THelper = type helper for word',
  25949. ' function GetSize: longint;',
  25950. ' procedure SetSize(Value: longint);',
  25951. ' property Size: longint read GetSize write SetSize;',
  25952. ' end;',
  25953. 'function THelper.GetSize: longint;',
  25954. 'begin',
  25955. ' Result:=Size+1;',
  25956. ' Size:=2;',
  25957. ' Result:=Self.Size+3;',
  25958. ' Self.Size:=4;',
  25959. ' with Self do begin',
  25960. ' Result:=Size+5;',
  25961. ' Size:=6;',
  25962. ' end;',
  25963. 'end;',
  25964. 'procedure THelper.SetSize(Value: longint);',
  25965. 'begin',
  25966. 'end;',
  25967. 'var w: word;',
  25968. 'begin',
  25969. ' w:=w.Size+7;',
  25970. ' w.Size:=w+8;',
  25971. ' with w do begin',
  25972. ' w:=Size+9;',
  25973. ' Size:=w+10;',
  25974. ' end;',
  25975. '']);
  25976. ConvertProgram;
  25977. CheckSource('TestTypeHelper_Property',
  25978. LinesToStr([ // statements
  25979. 'rtl.createHelper(this, "THelper", null, function () {',
  25980. ' this.GetSize = function () {',
  25981. ' var Result = 0;',
  25982. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25983. ' $mod.THelper.SetSize.call(this, 2);',
  25984. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25985. ' $mod.THelper.SetSize.call(this, 4);',
  25986. ' var $with = this.get();',
  25987. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25988. ' $mod.THelper.SetSize.call(this, 6);',
  25989. ' return Result;',
  25990. ' };',
  25991. ' this.SetSize = function (Value) {',
  25992. ' };',
  25993. '});',
  25994. 'this.w = 0;',
  25995. '']),
  25996. LinesToStr([ // $mod.$main
  25997. '$mod.w = $mod.THelper.GetSize.call({',
  25998. ' p: $mod,',
  25999. ' get: function () {',
  26000. ' return this.p.w;',
  26001. ' },',
  26002. ' set: function (v) {',
  26003. ' this.p.w = v;',
  26004. ' }',
  26005. '}) + 7;',
  26006. '$mod.THelper.SetSize.call({',
  26007. ' p: $mod,',
  26008. ' get: function () {',
  26009. ' return this.p.w;',
  26010. ' },',
  26011. ' set: function (v) {',
  26012. ' this.p.w = v;',
  26013. ' }',
  26014. '}, $mod.w + 8);',
  26015. 'var $with = $mod.w;',
  26016. '$mod.w = $mod.THelper.GetSize.call({',
  26017. ' get: function () {',
  26018. ' return $with;',
  26019. ' },',
  26020. ' set: function (v) {',
  26021. ' $with = v;',
  26022. ' }',
  26023. '}) + 9;',
  26024. '$mod.THelper.SetSize.call({',
  26025. ' get: function () {',
  26026. ' return $with;',
  26027. ' },',
  26028. ' set: function (v) {',
  26029. ' $with = v;',
  26030. ' }',
  26031. '}, $mod.w + 10);',
  26032. '']));
  26033. end;
  26034. procedure TTestModule.TestTypeHelper_Property_Array;
  26035. begin
  26036. StartProgram(false);
  26037. Add([
  26038. '{$modeswitch typehelpers}',
  26039. 'type',
  26040. ' THelper = type helper for word',
  26041. ' function GetItems(Index: byte): boolean;',
  26042. ' procedure SetItems(Index: byte; Value: boolean);',
  26043. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  26044. ' end;',
  26045. 'function THelper.GetItems(Index: byte): boolean;',
  26046. 'begin',
  26047. ' Result:=Items[1];',
  26048. ' Items[2]:=false;',
  26049. ' Result:=Self.Items[3];',
  26050. ' Self.Items[4]:=true;',
  26051. ' with Self do begin',
  26052. ' Result:=Items[5];',
  26053. ' Items[6]:=false;',
  26054. ' end;',
  26055. 'end;',
  26056. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26057. 'begin',
  26058. 'end;',
  26059. 'var',
  26060. ' w: word;',
  26061. ' b: boolean;',
  26062. 'begin',
  26063. ' b:=w.Items[1];',
  26064. ' w.Items[2]:=b;',
  26065. ' with w do begin',
  26066. ' b:=Items[3];',
  26067. ' Items[4]:=b;',
  26068. ' end;',
  26069. '']);
  26070. ConvertProgram;
  26071. CheckSource('TestTypeHelper_Property_Array',
  26072. LinesToStr([ // statements
  26073. 'rtl.createHelper(this, "THelper", null, function () {',
  26074. ' this.GetItems = function (Index) {',
  26075. ' var Result = false;',
  26076. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26077. ' $mod.THelper.SetItems.call(this, 2, false);',
  26078. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26079. ' $mod.THelper.SetItems.call(this, 4, true);',
  26080. ' var $with = this.get();',
  26081. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26082. ' $mod.THelper.SetItems.call(this, 6, false);',
  26083. ' return Result;',
  26084. ' };',
  26085. ' this.SetItems = function (Index, Value) {',
  26086. ' };',
  26087. '});',
  26088. 'this.w = 0;',
  26089. 'this.b = false;',
  26090. '']),
  26091. LinesToStr([ // $mod.$main
  26092. '$mod.b = $mod.THelper.GetItems.call({',
  26093. ' p: $mod,',
  26094. ' get: function () {',
  26095. ' return this.p.w;',
  26096. ' },',
  26097. ' set: function (v) {',
  26098. ' this.p.w = v;',
  26099. ' }',
  26100. '}, 1);',
  26101. '$mod.THelper.SetItems.call({',
  26102. ' p: $mod,',
  26103. ' get: function () {',
  26104. ' return this.p.w;',
  26105. ' },',
  26106. ' set: function (v) {',
  26107. ' this.p.w = v;',
  26108. ' }',
  26109. '}, 2, $mod.b);',
  26110. 'var $with = $mod.w;',
  26111. '$mod.b = $mod.THelper.GetItems.call({',
  26112. ' get: function () {',
  26113. ' return $with;',
  26114. ' },',
  26115. ' set: function (v) {',
  26116. ' $with = v;',
  26117. ' }',
  26118. '}, 3);',
  26119. '$mod.THelper.SetItems.call({',
  26120. ' get: function () {',
  26121. ' return $with;',
  26122. ' },',
  26123. ' set: function (v) {',
  26124. ' $with = v;',
  26125. ' }',
  26126. '}, 4, $mod.b);',
  26127. '']));
  26128. end;
  26129. procedure TTestModule.TestTypeHelper_ClassProperty;
  26130. begin
  26131. StartProgram(false);
  26132. Add([
  26133. '{$modeswitch typehelpers}',
  26134. 'type',
  26135. ' THelper = type helper for word',
  26136. ' class function GetSize: longint; static;',
  26137. ' class procedure SetSize(Value: longint); static;',
  26138. ' class property Size: longint read GetSize write SetSize;',
  26139. ' end;',
  26140. 'class function THelper.GetSize: longint;',
  26141. 'begin',
  26142. ' Result:=Size+1;',
  26143. ' Size:=2;',
  26144. 'end;',
  26145. 'class procedure THelper.SetSize(Value: longint);',
  26146. 'begin',
  26147. 'end;',
  26148. 'begin',
  26149. '']);
  26150. ConvertProgram;
  26151. CheckSource('TestTypeHelper_ClassProperty',
  26152. LinesToStr([ // statements
  26153. 'rtl.createHelper(this, "THelper", null, function () {',
  26154. ' this.GetSize = function () {',
  26155. ' var Result = 0;',
  26156. ' Result = $mod.THelper.GetSize() + 1;',
  26157. ' $mod.THelper.SetSize(2);',
  26158. ' return Result;',
  26159. ' };',
  26160. ' this.SetSize = function (Value) {',
  26161. ' };',
  26162. '});',
  26163. '']),
  26164. LinesToStr([ // $mod.$main
  26165. '']));
  26166. end;
  26167. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26168. begin
  26169. StartProgram(false);
  26170. Add([
  26171. '{$modeswitch typehelpers}',
  26172. 'type',
  26173. ' THelper = type helper for word',
  26174. ' class function GetItems(Index: byte): boolean; static;',
  26175. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26176. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26177. ' end;',
  26178. 'class function THelper.GetItems(Index: byte): boolean;',
  26179. 'begin',
  26180. ' Result:=Items[1];',
  26181. ' Items[2]:=false;',
  26182. 'end;',
  26183. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26184. 'begin',
  26185. 'end;',
  26186. 'var',
  26187. ' w: word;',
  26188. ' b: boolean;',
  26189. 'begin',
  26190. ' b:=w.Items[1];',
  26191. ' w.Items[2]:=b;',
  26192. ' with w do begin',
  26193. ' b:=Items[3];',
  26194. ' Items[4]:=b;',
  26195. ' end;',
  26196. '']);
  26197. ConvertProgram;
  26198. CheckSource('TestTypeHelper_ClassProperty_Array',
  26199. LinesToStr([ // statements
  26200. 'rtl.createHelper(this, "THelper", null, function () {',
  26201. ' this.GetItems = function (Index) {',
  26202. ' var Result = false;',
  26203. ' Result = $mod.THelper.GetItems(1);',
  26204. ' $mod.THelper.SetItems(2, false);',
  26205. ' return Result;',
  26206. ' };',
  26207. ' this.SetItems = function (Index, Value) {',
  26208. ' };',
  26209. '});',
  26210. 'this.w = 0;',
  26211. 'this.b = false;',
  26212. '']),
  26213. LinesToStr([ // $mod.$main
  26214. '$mod.b = $mod.THelper.GetItems(1);',
  26215. '$mod.THelper.SetItems(2, $mod.b);',
  26216. 'var $with = $mod.w;',
  26217. '$mod.b = $mod.THelper.GetItems(3);',
  26218. '$mod.THelper.SetItems(4, $mod.b);',
  26219. '']));
  26220. end;
  26221. procedure TTestModule.TestTypeHelper_ClassMethod;
  26222. begin
  26223. StartProgram(false);
  26224. Add([
  26225. '{$modeswitch typehelpers}',
  26226. 'type',
  26227. ' THelper = type helper for word',
  26228. ' class procedure DoStatic; static;',
  26229. ' end;',
  26230. 'class procedure THelper.DoStatic;',
  26231. 'begin',
  26232. ' DoStatic;',
  26233. ' DoStatic();',
  26234. 'end;',
  26235. 'var w: word;',
  26236. 'begin',
  26237. ' w.DoStatic;',
  26238. ' w.DoStatic();',
  26239. '']);
  26240. ConvertProgram;
  26241. CheckSource('TestTypeHelper_ClassMethod',
  26242. LinesToStr([ // statements
  26243. 'rtl.createHelper(this, "THelper", null, function () {',
  26244. ' this.DoStatic = function () {',
  26245. ' $mod.THelper.DoStatic();',
  26246. ' $mod.THelper.DoStatic();',
  26247. ' };',
  26248. '});',
  26249. 'this.w = 0;',
  26250. '']),
  26251. LinesToStr([ // $mod.$main
  26252. '$mod.THelper.DoStatic();',
  26253. '$mod.THelper.DoStatic();',
  26254. '']));
  26255. end;
  26256. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26257. begin
  26258. StartProgram(false);
  26259. Add([
  26260. '{$modeswitch typehelpers}',
  26261. 'type',
  26262. ' THelper = type helper for word',
  26263. ' procedure Run; external name ''Run'';',
  26264. ' end;',
  26265. 'var w: word;',
  26266. 'begin',
  26267. ' w.Run;',
  26268. '']);
  26269. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26270. ConvertProgram;
  26271. end;
  26272. procedure TTestModule.TestTypeHelper_Constructor;
  26273. begin
  26274. StartProgram(false);
  26275. Add([
  26276. '{$modeswitch typehelpers}',
  26277. 'type',
  26278. ' THelper = type helper for word',
  26279. ' constructor Init(e: longint);',
  26280. ' end;',
  26281. 'constructor THelper.Init(e: longint);',
  26282. 'begin',
  26283. ' Self:=e;',
  26284. ' Init(e+1);',
  26285. 'end;',
  26286. 'var w: word;',
  26287. 'begin',
  26288. ' w:=word.Init(2);',
  26289. ' w:=w.Init(3);',
  26290. ' with word do w:=Init(4);',
  26291. ' with w do w:=Init(5);',
  26292. '']);
  26293. ConvertProgram;
  26294. CheckSource('TestTypeHelper_Constructor',
  26295. LinesToStr([ // statements
  26296. 'rtl.createHelper(this, "THelper", null, function () {',
  26297. ' this.Init = function (e) {',
  26298. ' this.set(e);',
  26299. ' $mod.THelper.Init.call(this, e + 1);',
  26300. ' return this.get();',
  26301. ' };',
  26302. ' this.$new = function (fn, args) {',
  26303. ' return this[fn].apply({',
  26304. ' p: 0,',
  26305. ' get: function () {',
  26306. ' return this.p;',
  26307. ' },',
  26308. ' set: function (v) {',
  26309. ' this.p = v;',
  26310. ' }',
  26311. ' }, args);',
  26312. ' };',
  26313. '});',
  26314. 'this.w = 0;',
  26315. '']),
  26316. LinesToStr([ // $mod.$main
  26317. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26318. '$mod.w = $mod.THelper.Init.call({',
  26319. ' p: $mod,',
  26320. ' get: function () {',
  26321. ' return this.p.w;',
  26322. ' },',
  26323. ' set: function (v) {',
  26324. ' this.p.w = v;',
  26325. ' }',
  26326. '}, 3);',
  26327. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26328. 'var $with = $mod.w;',
  26329. '$mod.w = $mod.THelper.Init.call({',
  26330. ' get: function () {',
  26331. ' return $with;',
  26332. ' },',
  26333. ' set: function (v) {',
  26334. ' $with = v;',
  26335. ' }',
  26336. '}, 5);',
  26337. '']));
  26338. end;
  26339. procedure TTestModule.TestTypeHelper_Word;
  26340. begin
  26341. StartProgram(false);
  26342. Add([
  26343. '{$modeswitch typehelpers}',
  26344. 'type',
  26345. ' THelper = type helper for word',
  26346. ' procedure DoIt(e: byte = 123);',
  26347. ' end;',
  26348. 'procedure THelper.DoIt(e: byte);',
  26349. 'begin',
  26350. ' Self:=e;',
  26351. ' Self:=Self+1;',
  26352. ' with Self do Doit;',
  26353. 'end;',
  26354. 'begin',
  26355. ' word(3).DoIt;',
  26356. '']);
  26357. ConvertProgram;
  26358. CheckSource('TestTypeHelper_Word',
  26359. LinesToStr([ // statements
  26360. 'rtl.createHelper(this, "THelper", null, function () {',
  26361. ' this.DoIt = function (e) {',
  26362. ' this.set(e);',
  26363. ' this.set(this.get() + 1);',
  26364. ' var $with = this.get();',
  26365. ' $mod.THelper.DoIt.call(this, 123);',
  26366. ' };',
  26367. '});',
  26368. '']),
  26369. LinesToStr([ // $mod.$main
  26370. '$mod.THelper.DoIt.call({',
  26371. ' get: function () {',
  26372. ' return 3;',
  26373. ' },',
  26374. ' set: function (v) {',
  26375. ' rtl.raiseE("EPropReadOnly");',
  26376. ' }',
  26377. '}, 123);',
  26378. '']));
  26379. end;
  26380. procedure TTestModule.TestTypeHelper_Boolean;
  26381. begin
  26382. StartProgram(false);
  26383. Add([
  26384. '{$modeswitch typehelpers}',
  26385. 'type',
  26386. ' Integer = longint;',
  26387. ' THelper = type helper for boolean',
  26388. ' procedure Run(e: wordbool = true);',
  26389. ' end;',
  26390. 'procedure THelper.Run(e: wordbool);',
  26391. 'begin',
  26392. ' Self:=e;',
  26393. ' Self:=not Self;',
  26394. ' with Self do Run;',
  26395. ' if Integer(Self)=0 then ;',
  26396. 'end;',
  26397. 'begin',
  26398. ' boolean(3).Run;',
  26399. '']);
  26400. ConvertProgram;
  26401. CheckSource('TestTypeHelper_Boolean',
  26402. LinesToStr([ // statements
  26403. 'rtl.createHelper(this, "THelper", null, function () {',
  26404. ' this.Run = function (e) {',
  26405. ' this.set(e);',
  26406. ' this.set(!this.get());',
  26407. ' var $with = this.get();',
  26408. ' $mod.THelper.Run.call(this, true);',
  26409. ' if ((this.get() ? 1 : 0) === 0) ;',
  26410. ' };',
  26411. '});',
  26412. '']),
  26413. LinesToStr([ // $mod.$main
  26414. '$mod.THelper.Run.call({',
  26415. ' a: 3 != 0,',
  26416. ' get: function () {',
  26417. ' return this.a;',
  26418. ' },',
  26419. ' set: function (v) {',
  26420. ' rtl.raiseE("EPropReadOnly");',
  26421. ' }',
  26422. '}, true);',
  26423. '']));
  26424. end;
  26425. procedure TTestModule.TestTypeHelper_WordBool;
  26426. begin
  26427. StartProgram(false);
  26428. Add([
  26429. '{$modeswitch typehelpers}',
  26430. 'type',
  26431. ' Integer = longint;',
  26432. ' THelper = type helper for WordBool',
  26433. ' procedure Run(e: wordbool = true);',
  26434. ' end;',
  26435. 'procedure THelper.Run(e: wordbool);',
  26436. 'var i: integer;',
  26437. 'begin',
  26438. ' i:=Integer(Self);',
  26439. 'end;',
  26440. 'var w: wordbool;',
  26441. 'begin',
  26442. ' w.Run;',
  26443. ' wordbool(3).Run;',
  26444. '']);
  26445. ConvertProgram;
  26446. CheckSource('TestTypeHelper_WordBool',
  26447. LinesToStr([ // statements
  26448. 'rtl.createHelper(this, "THelper", null, function () {',
  26449. ' this.Run = function (e) {',
  26450. ' var i = 0;',
  26451. ' i = (this.get() ? 1 : 0);',
  26452. ' };',
  26453. '});',
  26454. 'this.w = false;',
  26455. '']),
  26456. LinesToStr([ // $mod.$main
  26457. '$mod.THelper.Run.call({',
  26458. ' p: $mod,',
  26459. ' get: function () {',
  26460. ' return this.p.w;',
  26461. ' },',
  26462. ' set: function (v) {',
  26463. ' this.p.w = v;',
  26464. ' }',
  26465. '}, true);',
  26466. '$mod.THelper.Run.call({',
  26467. ' a: 3 != 0,',
  26468. ' get: function () {',
  26469. ' return this.a;',
  26470. ' },',
  26471. ' set: function (v) {',
  26472. ' rtl.raiseE("EPropReadOnly");',
  26473. ' }',
  26474. '}, true);',
  26475. '']));
  26476. end;
  26477. procedure TTestModule.TestTypeHelper_Double;
  26478. begin
  26479. StartProgram(false);
  26480. Add([
  26481. '{$modeswitch typehelpers}',
  26482. 'type',
  26483. ' Float = type double;',
  26484. ' THelper = type helper for Float',
  26485. ' const NPI = 3.141592;',
  26486. ' function ToStr: String;',
  26487. ' end;',
  26488. 'function THelper.ToStr: String;',
  26489. 'begin',
  26490. 'end;',
  26491. 'procedure DoIt(s: string);',
  26492. 'begin',
  26493. 'end;',
  26494. 'var f: Float;',
  26495. 'begin',
  26496. ' DoIt(f.toStr);',
  26497. ' DoIt(f.toStr());',
  26498. ' (f*f).toStr;',
  26499. ' DoIt((f*f).toStr);',
  26500. '']);
  26501. ConvertProgram;
  26502. CheckSource('TestTypeHelper_Double',
  26503. LinesToStr([ // statements
  26504. 'rtl.createHelper(this, "THelper", null, function () {',
  26505. ' this.NPI = 3.141592;',
  26506. ' this.ToStr = function () {',
  26507. ' var Result = "";',
  26508. ' return Result;',
  26509. ' };',
  26510. '});',
  26511. 'this.DoIt = function (s) {',
  26512. '};',
  26513. 'this.f = 0.0;',
  26514. '']),
  26515. LinesToStr([ // $mod.$main
  26516. '$mod.DoIt($mod.THelper.ToStr.call({',
  26517. ' p: $mod,',
  26518. ' get: function () {',
  26519. ' return this.p.f;',
  26520. ' },',
  26521. ' set: function (v) {',
  26522. ' this.p.f = v;',
  26523. ' }',
  26524. '}));',
  26525. '$mod.DoIt($mod.THelper.ToStr.call({',
  26526. ' p: $mod,',
  26527. ' get: function () {',
  26528. ' return this.p.f;',
  26529. ' },',
  26530. ' set: function (v) {',
  26531. ' this.p.f = v;',
  26532. ' }',
  26533. '}));',
  26534. '$mod.THelper.ToStr.call({',
  26535. ' a: $mod.f * $mod.f,',
  26536. ' get: function () {',
  26537. ' return this.a;',
  26538. ' },',
  26539. ' set: function (v) {',
  26540. ' rtl.raiseE("EPropReadOnly");',
  26541. ' }',
  26542. '});',
  26543. '$mod.DoIt($mod.THelper.ToStr.call({',
  26544. ' a: $mod.f * $mod.f,',
  26545. ' get: function () {',
  26546. ' return this.a;',
  26547. ' },',
  26548. ' set: function (v) {',
  26549. ' rtl.raiseE("EPropReadOnly");',
  26550. ' }',
  26551. '}));',
  26552. '']));
  26553. end;
  26554. procedure TTestModule.TestTypeHelper_NativeInt;
  26555. begin
  26556. StartProgram(false);
  26557. Add([
  26558. '{$modeswitch typehelpers}',
  26559. 'type',
  26560. ' MaxInt = type nativeint;',
  26561. ' THelperI = type helper for MaxInt',
  26562. ' function ToStr: String;',
  26563. ' end;',
  26564. ' MaxUInt = type nativeuint;',
  26565. ' THelperU = type helper for MaxUInt',
  26566. ' function ToStr: String;',
  26567. ' end;',
  26568. 'function THelperI.ToStr: String;',
  26569. 'begin',
  26570. ' Result:=str(Self);',
  26571. 'end;',
  26572. 'function THelperU.ToStr: String;',
  26573. 'begin',
  26574. ' Result:=str(Self);',
  26575. 'end;',
  26576. 'procedure DoIt(s: string);',
  26577. 'begin',
  26578. 'end;',
  26579. 'var i: MaxInt;',
  26580. 'begin',
  26581. ' DoIt(i.toStr);',
  26582. ' DoIt(i.toStr());',
  26583. ' (i*i).toStr;',
  26584. ' DoIt((i*i).toStr);',
  26585. '']);
  26586. ConvertProgram;
  26587. CheckSource('TestTypeHelper_NativeInt',
  26588. LinesToStr([ // statements
  26589. 'rtl.createHelper(this, "THelperI", null, function () {',
  26590. ' this.ToStr = function () {',
  26591. ' var Result = "";',
  26592. ' Result = "" + this.get();',
  26593. ' return Result;',
  26594. ' };',
  26595. '});',
  26596. 'rtl.createHelper(this, "THelperU", null, function () {',
  26597. ' this.ToStr = function () {',
  26598. ' var Result = "";',
  26599. ' Result = "" + this.get();',
  26600. ' return Result;',
  26601. ' };',
  26602. '});',
  26603. 'this.DoIt = function (s) {',
  26604. '};',
  26605. 'this.i = 0;',
  26606. '']),
  26607. LinesToStr([ // $mod.$main
  26608. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26609. ' p: $mod,',
  26610. ' get: function () {',
  26611. ' return this.p.i;',
  26612. ' },',
  26613. ' set: function (v) {',
  26614. ' this.p.i = v;',
  26615. ' }',
  26616. '}));',
  26617. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26618. ' p: $mod,',
  26619. ' get: function () {',
  26620. ' return this.p.i;',
  26621. ' },',
  26622. ' set: function (v) {',
  26623. ' this.p.i = v;',
  26624. ' }',
  26625. '}));',
  26626. '$mod.THelperI.ToStr.call({',
  26627. ' a: $mod.i * $mod.i,',
  26628. ' get: function () {',
  26629. ' return this.a;',
  26630. ' },',
  26631. ' set: function (v) {',
  26632. ' rtl.raiseE("EPropReadOnly");',
  26633. ' }',
  26634. '});',
  26635. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26636. ' a: $mod.i * $mod.i,',
  26637. ' get: function () {',
  26638. ' return this.a;',
  26639. ' },',
  26640. ' set: function (v) {',
  26641. ' rtl.raiseE("EPropReadOnly");',
  26642. ' }',
  26643. '}));',
  26644. '']));
  26645. end;
  26646. procedure TTestModule.TestTypeHelper_StringChar;
  26647. begin
  26648. StartProgram(false);
  26649. Add([
  26650. '{$modeswitch typehelpers}',
  26651. 'type',
  26652. ' TStringHelper = type helper for string',
  26653. ' procedure DoIt(e: byte = 123);',
  26654. ' end;',
  26655. ' TCharHelper = type helper for char',
  26656. ' procedure Fly;',
  26657. ' end;',
  26658. 'procedure TStringHelper.DoIt(e: byte);',
  26659. 'begin',
  26660. ' Self[1]:=''c'';',
  26661. ' Self[2]:=Self[3];',
  26662. 'end;',
  26663. 'procedure TCharHelper.Fly;',
  26664. 'begin',
  26665. ' Self:=''c'';',
  26666. 'end;',
  26667. 'begin',
  26668. ' ''abc''.DoIt;',
  26669. ' ''xyz''.DoIt();',
  26670. ' ''c''.Fly();',
  26671. '']);
  26672. ConvertProgram;
  26673. CheckSource('TestTypeHelper_StringChar',
  26674. LinesToStr([ // statements
  26675. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26676. ' this.DoIt = function (e) {',
  26677. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26678. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26679. ' };',
  26680. '});',
  26681. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26682. ' this.Fly = function () {',
  26683. ' this.set("c");',
  26684. ' };',
  26685. '});',
  26686. '']),
  26687. LinesToStr([ // $mod.$main
  26688. '$mod.TStringHelper.DoIt.call({',
  26689. ' get: function () {',
  26690. ' return "abc";',
  26691. ' },',
  26692. ' set: function (v) {',
  26693. ' rtl.raiseE("EPropReadOnly");',
  26694. ' }',
  26695. '}, 123);',
  26696. '$mod.TStringHelper.DoIt.call({',
  26697. ' get: function () {',
  26698. ' return "xyz";',
  26699. ' },',
  26700. ' set: function (v) {',
  26701. ' rtl.raiseE("EPropReadOnly");',
  26702. ' }',
  26703. '}, 123);',
  26704. '$mod.TCharHelper.Fly.call({',
  26705. ' get: function () {',
  26706. ' return "c";',
  26707. ' },',
  26708. ' set: function (v) {',
  26709. ' rtl.raiseE("EPropReadOnly");',
  26710. ' }',
  26711. '});',
  26712. '']));
  26713. end;
  26714. procedure TTestModule.TestTypeHelper_JSValue;
  26715. begin
  26716. StartProgram(false);
  26717. Add([
  26718. '{$modeswitch typehelpers}',
  26719. 'type',
  26720. ' TExtValue = type jsvalue;',
  26721. ' THelper = type helper for TExtValue',
  26722. ' function ToStr: String;',
  26723. ' end;',
  26724. 'function THelper.ToStr: String;',
  26725. 'begin',
  26726. 'end;',
  26727. 'var',
  26728. ' s: string;',
  26729. ' v: TExtValue;',
  26730. 'begin',
  26731. ' s:=v.toStr;',
  26732. ' s:=v.toStr();',
  26733. ' TExtValue(s).toStr;',
  26734. '']);
  26735. ConvertProgram;
  26736. CheckSource('TestTypeHelper_JSValue',
  26737. LinesToStr([ // statements
  26738. 'rtl.createHelper(this, "THelper", null, function () {',
  26739. ' this.ToStr = function () {',
  26740. ' var Result = "";',
  26741. ' return Result;',
  26742. ' };',
  26743. '});',
  26744. 'this.s = "";',
  26745. 'this.v = undefined;',
  26746. '']),
  26747. LinesToStr([ // $mod.$main
  26748. '$mod.s = $mod.THelper.ToStr.call({',
  26749. ' p: $mod,',
  26750. ' get: function () {',
  26751. ' return this.p.v;',
  26752. ' },',
  26753. ' set: function (v) {',
  26754. ' this.p.v = v;',
  26755. ' }',
  26756. '});',
  26757. '$mod.s = $mod.THelper.ToStr.call({',
  26758. ' p: $mod,',
  26759. ' get: function () {',
  26760. ' return this.p.v;',
  26761. ' },',
  26762. ' set: function (v) {',
  26763. ' this.p.v = v;',
  26764. ' }',
  26765. '});',
  26766. '$mod.THelper.ToStr.call({',
  26767. ' p: $mod,',
  26768. ' get: function () {',
  26769. ' return this.p.s;',
  26770. ' },',
  26771. ' set: function (v) {',
  26772. ' rtl.raiseE("EPropReadOnly");',
  26773. ' }',
  26774. '});',
  26775. '']));
  26776. end;
  26777. procedure TTestModule.TestTypeHelper_Array;
  26778. begin
  26779. StartProgram(false);
  26780. Add([
  26781. '{$modeswitch typehelpers}',
  26782. 'type',
  26783. ' TArrOfBool = array of boolean;',
  26784. ' TArrOfJS = array of jsvalue;',
  26785. ' THelper = type helper for TArrOfBool',
  26786. ' procedure DoIt(e: byte = 123);',
  26787. ' end;',
  26788. 'procedure THelper.DoIt(e: byte);',
  26789. 'begin',
  26790. ' Self[1]:=true;',
  26791. ' Self[2]:=not Self[3];',
  26792. ' SetLength(Self,4);',
  26793. ' Self:=Concat(Self,[true]);',
  26794. 'end;',
  26795. 'var',
  26796. ' b: TArrOfBool;',
  26797. ' j: TArrOfJS;',
  26798. 'begin',
  26799. ' b.DoIt;',
  26800. ' TArrOfBool(j).DoIt();',
  26801. '']);
  26802. ConvertProgram;
  26803. CheckSource('TestTypeHelper_Array',
  26804. LinesToStr([ // statements
  26805. 'rtl.createHelper(this, "THelper", null, function () {',
  26806. ' this.DoIt = function (e) {',
  26807. ' this.get()[1] = true;',
  26808. ' this.get()[2] = !this.get()[3];',
  26809. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26810. ' this.set(rtl.arrayPushN(this.get(), true));',
  26811. ' };',
  26812. '});',
  26813. 'this.b = [];',
  26814. 'this.j = [];',
  26815. '']),
  26816. LinesToStr([ // $mod.$main
  26817. '$mod.THelper.DoIt.call({',
  26818. ' p: $mod,',
  26819. ' get: function () {',
  26820. ' return this.p.b;',
  26821. ' },',
  26822. ' set: function (v) {',
  26823. ' this.p.b = v;',
  26824. ' }',
  26825. '}, 123);',
  26826. '$mod.THelper.DoIt.call({',
  26827. ' p: $mod,',
  26828. ' get: function () {',
  26829. ' return this.p.j;',
  26830. ' },',
  26831. ' set: function (v) {',
  26832. ' this.p.j = v;',
  26833. ' }',
  26834. '}, 123);',
  26835. '']));
  26836. end;
  26837. procedure TTestModule.TestTypeHelper_EnumType;
  26838. begin
  26839. StartProgram(false);
  26840. Add([
  26841. '{$modeswitch typehelpers}',
  26842. 'type',
  26843. ' TEnum = (red,blue);',
  26844. ' THelper = type helper for TEnum',
  26845. ' procedure DoIt(e: byte = 123);',
  26846. ' class procedure Swing(w: word); static;',
  26847. ' end;',
  26848. 'procedure THelper.DoIt(e: byte);',
  26849. 'begin',
  26850. ' Self:=red;',
  26851. ' Self:=succ(Self);',
  26852. ' with Self do Doit;',
  26853. 'end;',
  26854. 'class procedure THelper.Swing(w: word);',
  26855. 'begin',
  26856. 'end;',
  26857. 'var e: TEnum;',
  26858. 'begin',
  26859. ' e.DoIt;',
  26860. ' red.DoIt;',
  26861. ' TEnum.blue.DoIt;',
  26862. ' TEnum(1).DoIt;',
  26863. ' TEnum.Swing(3);',
  26864. '']);
  26865. ConvertProgram;
  26866. CheckSource('TestTypeHelper_EnumType',
  26867. LinesToStr([ // statements
  26868. 'this.TEnum = {',
  26869. ' "0": "red",',
  26870. ' red: 0,',
  26871. ' "1": "blue",',
  26872. ' blue: 1',
  26873. '};',
  26874. 'rtl.createHelper(this, "THelper", null, function () {',
  26875. ' this.DoIt = function (e) {',
  26876. ' this.set($mod.TEnum.red);',
  26877. ' this.set(this.get() + 1);',
  26878. ' var $with = this.get();',
  26879. ' $mod.THelper.DoIt.call(this, 123);',
  26880. ' };',
  26881. ' this.Swing = function (w) {',
  26882. ' };',
  26883. '});',
  26884. 'this.e = 0;',
  26885. '']),
  26886. LinesToStr([ // $mod.$main
  26887. '$mod.THelper.DoIt.call({',
  26888. ' p: $mod,',
  26889. ' get: function () {',
  26890. ' return this.p.e;',
  26891. ' },',
  26892. ' set: function (v) {',
  26893. ' this.p.e = v;',
  26894. ' }',
  26895. '}, 123);',
  26896. '$mod.THelper.DoIt.call({',
  26897. ' p: $mod.TEnum,',
  26898. ' get: function () {',
  26899. ' return this.p.red;',
  26900. ' },',
  26901. ' set: function (v) {',
  26902. ' rtl.raiseE("EPropReadOnly");',
  26903. ' }',
  26904. '}, 123);',
  26905. '$mod.THelper.DoIt.call({',
  26906. ' p: $mod.TEnum,',
  26907. ' get: function () {',
  26908. ' return this.p.blue;',
  26909. ' },',
  26910. ' set: function (v) {',
  26911. ' rtl.raiseE("EPropReadOnly");',
  26912. ' }',
  26913. '}, 123);',
  26914. '$mod.THelper.DoIt.call({',
  26915. ' get: function () {',
  26916. ' return 1;',
  26917. ' },',
  26918. ' set: function (v) {',
  26919. ' rtl.raiseE("EPropReadOnly");',
  26920. ' }',
  26921. '}, 123);',
  26922. '$mod.THelper.Swing(3);',
  26923. '']));
  26924. end;
  26925. procedure TTestModule.TestTypeHelper_SetType;
  26926. begin
  26927. StartProgram(false);
  26928. Add([
  26929. '{$modeswitch typehelpers}',
  26930. 'type',
  26931. ' TEnum = (red,blue);',
  26932. ' TSetOfEnum = set of TEnum;',
  26933. ' THelper = type helper for TSetOfEnum',
  26934. ' procedure DoIt(e: byte = 123);',
  26935. ' constructor Init(e: TEnum);',
  26936. ' constructor InitEmpty;',
  26937. ' end;',
  26938. 'procedure THelper.DoIt(e: byte);',
  26939. 'begin',
  26940. ' Self:=[];',
  26941. ' Self:=[red];',
  26942. ' Include(Self,blue);',
  26943. 'end;',
  26944. 'constructor THelper.Init(e: TEnum);',
  26945. 'begin',
  26946. ' Self:=[];',
  26947. ' Self:=[e];',
  26948. ' Include(Self,blue);',
  26949. 'end;',
  26950. 'constructor THelper.InitEmpty;',
  26951. 'begin',
  26952. 'end;',
  26953. 'var s: TSetOfEnum;',
  26954. 'begin',
  26955. ' s.DoIt;',
  26956. //' [red].DoIt;',
  26957. //' with s do DoIt;',
  26958. //' with [red,blue] do DoIt;',
  26959. ' s:=TSetOfEnum.Init(blue);',
  26960. ' s:=s.Init(blue);',
  26961. '']);
  26962. ConvertProgram;
  26963. CheckSource('TestTypeHelper_SetType',
  26964. LinesToStr([ // statements
  26965. 'this.TEnum = {',
  26966. ' "0": "red",',
  26967. ' red: 0,',
  26968. ' "1": "blue",',
  26969. ' blue: 1',
  26970. '};',
  26971. 'rtl.createHelper(this, "THelper", null, function () {',
  26972. ' this.DoIt = function (e) {',
  26973. ' this.set({});',
  26974. ' this.set(rtl.createSet($mod.TEnum.red));',
  26975. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26976. ' };',
  26977. ' this.Init = function (e) {',
  26978. ' this.set({});',
  26979. ' this.set(rtl.createSet(e));',
  26980. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26981. ' return this.get();',
  26982. ' };',
  26983. ' this.InitEmpty = function () {',
  26984. ' return this.get();',
  26985. ' };',
  26986. ' this.$new = function (fn, args) {',
  26987. ' return this[fn].apply({',
  26988. ' p: {},',
  26989. ' get: function () {',
  26990. ' return this.p;',
  26991. ' },',
  26992. ' set: function (v) {',
  26993. ' this.p = v;',
  26994. ' }',
  26995. ' }, args);',
  26996. ' };',
  26997. '});',
  26998. 'this.s = {};',
  26999. '']),
  27000. LinesToStr([ // $mod.$main
  27001. '$mod.THelper.DoIt.call({',
  27002. ' p: $mod,',
  27003. ' get: function () {',
  27004. ' return this.p.s;',
  27005. ' },',
  27006. ' set: function (v) {',
  27007. ' this.p.s = v;',
  27008. ' }',
  27009. '}, 123);',
  27010. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  27011. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  27012. ' p: $mod,',
  27013. ' get: function () {',
  27014. ' return this.p.s;',
  27015. ' },',
  27016. ' set: function (v) {',
  27017. ' this.p.s = v;',
  27018. ' }',
  27019. '}, $mod.TEnum.blue));',
  27020. '']));
  27021. end;
  27022. procedure TTestModule.TestTypeHelper_InterfaceType;
  27023. begin
  27024. StartProgram(false);
  27025. Add([
  27026. '{$interfaces com}',
  27027. '{$modeswitch typehelpers}',
  27028. 'type',
  27029. ' IUnknown = interface',
  27030. ' function _AddRef: longint;',
  27031. ' function _Release: longint;',
  27032. ' end;',
  27033. ' TObject = class(IUnknown)',
  27034. ' function _AddRef: longint; virtual; abstract;',
  27035. ' function _Release: longint; virtual; abstract;',
  27036. ' end;',
  27037. ' THelper = type helper for IUnknown',
  27038. ' procedure Fly(e: byte = 123);',
  27039. ' class procedure Run; static;',
  27040. ' end;',
  27041. 'var',
  27042. ' i: IUnknown;',
  27043. ' o: TObject;',
  27044. 'procedure THelper.Fly(e: byte);',
  27045. 'begin',
  27046. ' i:=Self;',
  27047. ' o:=Self as TObject;',
  27048. ' Self:=nil;',
  27049. ' Self:=i;',
  27050. ' Self:=o;',
  27051. ' with Self do begin',
  27052. ' Fly;',
  27053. ' Fly();',
  27054. ' end;',
  27055. 'end;',
  27056. 'class procedure THelper.Run;',
  27057. 'var l: IUnknown;',
  27058. 'begin',
  27059. ' l.Fly;',
  27060. ' l.Fly();',
  27061. 'end;',
  27062. 'begin',
  27063. ' i.Fly;',
  27064. ' i.Fly();',
  27065. ' i.Run;',
  27066. ' i.Run();',
  27067. ' IUnknown.Run;',
  27068. ' IUnknown.Run();',
  27069. '']);
  27070. ConvertProgram;
  27071. CheckSource('TestTypeHelper_InterfaceType',
  27072. LinesToStr([ // statements
  27073. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27074. 'rtl.createClass(this, "TObject", null, function () {',
  27075. ' this.$init = function () {',
  27076. ' };',
  27077. ' this.$final = function () {',
  27078. ' };',
  27079. ' rtl.addIntf(this, $mod.IUnknown);',
  27080. '});',
  27081. 'rtl.createHelper(this, "THelper", null, function () {',
  27082. ' this.Fly = function (e) {',
  27083. ' var $ir = rtl.createIntfRefs();',
  27084. ' try {',
  27085. ' rtl.setIntfP($mod, "i", this.get());',
  27086. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27087. ' this.set(null);',
  27088. ' this.set($mod.i);',
  27089. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27090. ' var $with = this.get();',
  27091. ' $mod.THelper.Fly.call(this, 123);',
  27092. ' $mod.THelper.Fly.call(this, 123);',
  27093. ' } finally {',
  27094. ' $ir.free();',
  27095. ' };',
  27096. ' };',
  27097. ' this.Run = function () {',
  27098. ' var l = null;',
  27099. ' try {',
  27100. ' $mod.THelper.Fly.call({',
  27101. ' get: function () {',
  27102. ' return l;',
  27103. ' },',
  27104. ' set: function (v) {',
  27105. ' l = rtl.setIntfL(l, v);',
  27106. ' }',
  27107. ' }, 123);',
  27108. ' $mod.THelper.Fly.call({',
  27109. ' get: function () {',
  27110. ' return l;',
  27111. ' },',
  27112. ' set: function (v) {',
  27113. ' l = rtl.setIntfL(l, v);',
  27114. ' }',
  27115. ' }, 123);',
  27116. ' } finally {',
  27117. ' rtl._Release(l);',
  27118. ' };',
  27119. ' };',
  27120. '});',
  27121. 'this.i = null;',
  27122. 'this.o = null;',
  27123. '']),
  27124. LinesToStr([ // $mod.$main
  27125. '$mod.THelper.Fly.call({',
  27126. ' p: $mod,',
  27127. ' get: function () {',
  27128. ' return this.p.i;',
  27129. ' },',
  27130. ' set: function (v) {',
  27131. ' rtl.setIntfP(this.p, "i", v);',
  27132. ' }',
  27133. '}, 123);',
  27134. '$mod.THelper.Fly.call({',
  27135. ' p: $mod,',
  27136. ' get: function () {',
  27137. ' return this.p.i;',
  27138. ' },',
  27139. ' set: function (v) {',
  27140. ' rtl.setIntfP(this.p, "i", v);',
  27141. ' }',
  27142. '}, 123);',
  27143. '$mod.THelper.Run();',
  27144. '$mod.THelper.Run();',
  27145. '$mod.THelper.Run();',
  27146. '$mod.THelper.Run();',
  27147. '']));
  27148. end;
  27149. procedure TTestModule.TestTypeHelper_NestedSelf;
  27150. begin
  27151. StartProgram(false);
  27152. Add([
  27153. '{$modeswitch typehelpers}',
  27154. 'type',
  27155. ' THelper = type helper for string',
  27156. ' procedure Run(Value: string);',
  27157. ' end;',
  27158. 'procedure THelper.Run(Value: string);',
  27159. ' function Sub(i: nativeint): boolean;',
  27160. ' begin',
  27161. ' Result:=Self[i+1]=Value[i];',
  27162. ' end;',
  27163. 'begin',
  27164. ' if Self[3]=Value[4] then ;',
  27165. 'end;',
  27166. 'begin',
  27167. '']);
  27168. ConvertProgram;
  27169. CheckSource('TestTypeHelper_NestedSelf',
  27170. LinesToStr([ // statements
  27171. 'rtl.createHelper(this, "THelper", null, function () {',
  27172. ' this.Run = function (Value) {',
  27173. ' var $Self = this;',
  27174. ' function Sub(i) {',
  27175. ' var Result = false;',
  27176. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27177. ' return Result;',
  27178. ' };',
  27179. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27180. ' };',
  27181. '});',
  27182. '']),
  27183. LinesToStr([ // $mod.$main
  27184. '']));
  27185. end;
  27186. procedure TTestModule.TestProcType;
  27187. begin
  27188. StartProgram(false);
  27189. Add([
  27190. 'type',
  27191. ' TProcInt = procedure(vI: longint = 1);',
  27192. 'procedure DoIt(vJ: longint);',
  27193. 'begin end;',
  27194. 'var',
  27195. ' b: boolean;',
  27196. ' vP, vQ: tprocint;',
  27197. 'begin',
  27198. ' vp:=nil;',
  27199. ' vp:=vp;',
  27200. ' vp:=@doit;',
  27201. ' vp;',
  27202. ' vp();',
  27203. ' vp(2);',
  27204. ' b:=vp=nil;',
  27205. ' b:=nil=vp;',
  27206. ' b:=vp=vq;',
  27207. ' b:=vp=@doit;',
  27208. ' b:=@doit=vp;',
  27209. ' b:=vp<>nil;',
  27210. ' b:=nil<>vp;',
  27211. ' b:=vp<>vq;',
  27212. ' b:=vp<>@doit;',
  27213. ' b:=@doit<>vp;',
  27214. ' b:=Assigned(vp);',
  27215. ' if Assigned(vp) then ;']);
  27216. ConvertProgram;
  27217. CheckSource('TestProcType',
  27218. LinesToStr([ // statements
  27219. 'this.DoIt = function(vJ) {',
  27220. '};',
  27221. 'this.b = false;',
  27222. 'this.vP = null;',
  27223. 'this.vQ = null;'
  27224. ]),
  27225. LinesToStr([ // $mod.$main
  27226. '$mod.vP = null;',
  27227. '$mod.vP = $mod.vP;',
  27228. '$mod.vP = $mod.DoIt;',
  27229. '$mod.vP(1);',
  27230. '$mod.vP(1);',
  27231. '$mod.vP(2);',
  27232. '$mod.b = $mod.vP === null;',
  27233. '$mod.b = null === $mod.vP;',
  27234. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27235. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27236. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27237. '$mod.b = $mod.vP !== null;',
  27238. '$mod.b = null !== $mod.vP;',
  27239. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27240. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27241. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27242. '$mod.b = $mod.vP != null;',
  27243. 'if ($mod.vP != null) ;',
  27244. '']));
  27245. end;
  27246. procedure TTestModule.TestProcType_Arg;
  27247. begin
  27248. StartProgram(false);
  27249. Add([
  27250. 'type',
  27251. ' TProcInt = procedure(vI: longint = 1);',
  27252. 'procedure DoIt(vJ: longint); begin end;',
  27253. 'procedure DoSome(vP, vQ: TProcInt);',
  27254. 'var',
  27255. ' b: boolean;',
  27256. 'begin',
  27257. ' vp:=nil;',
  27258. ' vp:=vp;',
  27259. ' vp:=@doit;',
  27260. ' vp;',
  27261. ' vp();',
  27262. ' vp(2);',
  27263. ' b:=vp=nil;',
  27264. ' b:=nil=vp;',
  27265. ' b:=vp=vq;',
  27266. ' b:=vp=@doit;',
  27267. ' b:=@doit=vp;',
  27268. ' b:=vp<>nil;',
  27269. ' b:=nil<>vp;',
  27270. ' b:=vp<>vq;',
  27271. ' b:=vp<>@doit;',
  27272. ' b:=@doit<>vp;',
  27273. ' b:=Assigned(vp);',
  27274. ' if Assigned(vp) then ;',
  27275. 'end;',
  27276. 'begin',
  27277. ' DoSome(@DoIt,nil);']);
  27278. ConvertProgram;
  27279. CheckSource('TestProcType_Arg',
  27280. LinesToStr([ // statements
  27281. 'this.DoIt = function(vJ) {',
  27282. '};',
  27283. 'this.DoSome = function(vP, vQ) {',
  27284. ' var b = false;',
  27285. ' vP = null;',
  27286. ' vP = vP;',
  27287. ' vP = $mod.DoIt;',
  27288. ' vP(1);',
  27289. ' vP(1);',
  27290. ' vP(2);',
  27291. ' b = vP === null;',
  27292. ' b = null === vP;',
  27293. ' b = rtl.eqCallback(vP,vQ);',
  27294. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27295. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27296. ' b = vP !== null;',
  27297. ' b = null !== vP;',
  27298. ' b = !rtl.eqCallback(vP, vQ);',
  27299. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27300. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27301. ' b = vP != null;',
  27302. ' if (vP != null) ;',
  27303. '};',
  27304. '']),
  27305. LinesToStr([ // $mod.$main
  27306. '$mod.DoSome($mod.DoIt,null);',
  27307. '']));
  27308. end;
  27309. procedure TTestModule.TestProcType_FunctionFPC;
  27310. begin
  27311. StartProgram(false);
  27312. Add('type');
  27313. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27314. Add('function DoIt(vI: longint): longint;');
  27315. Add('begin end;');
  27316. Add('var');
  27317. Add(' b: boolean;');
  27318. Add(' vP, vQ: tfuncint;');
  27319. Add('begin');
  27320. Add(' vp:=nil;');
  27321. Add(' vp:=vp;');
  27322. Add(' vp:=@doit;'); // ok in fpc and delphi
  27323. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27324. Add(' vp;'); // ok in fpc and delphi
  27325. Add(' vp();');
  27326. Add(' vp(2);');
  27327. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27328. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27329. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27330. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27331. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27332. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27333. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27334. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27335. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27336. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27337. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27338. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27339. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27340. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27341. Add(' b:=Assigned(vp);');
  27342. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27343. Add(' doit(vp());'); // ok in fpc and delphi
  27344. Add(' doit(vp(2));'); // ok in fpc and delphi
  27345. ConvertProgram;
  27346. CheckSource('TestProcType_FunctionFPC',
  27347. LinesToStr([ // statements
  27348. 'this.DoIt = function(vI) {',
  27349. ' var Result = 0;',
  27350. ' return Result;',
  27351. '};',
  27352. 'this.b = false;',
  27353. 'this.vP = null;',
  27354. 'this.vQ = null;'
  27355. ]),
  27356. LinesToStr([ // $mod.$main
  27357. '$mod.vP = null;',
  27358. '$mod.vP = $mod.vP;',
  27359. '$mod.vP = $mod.DoIt;',
  27360. '$mod.vP(1);',
  27361. '$mod.vP(1);',
  27362. '$mod.vP(2);',
  27363. '$mod.b = $mod.vP === null;',
  27364. '$mod.b = null === $mod.vP;',
  27365. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27366. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27367. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27368. '$mod.b = 4 === $mod.vP(1);',
  27369. '$mod.b = $mod.vP !== null;',
  27370. '$mod.b = null !== $mod.vP;',
  27371. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27372. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27373. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27374. '$mod.b = 6 !== $mod.vP(1);',
  27375. '$mod.b = $mod.vP != null;',
  27376. '$mod.DoIt($mod.vP(1));',
  27377. '$mod.DoIt($mod.vP(2));',
  27378. '']));
  27379. end;
  27380. procedure TTestModule.TestProcType_FunctionDelphi;
  27381. begin
  27382. StartProgram(false);
  27383. Add('{$mode Delphi}');
  27384. Add('type');
  27385. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27386. Add('function DoIt(vI: longint): longint;');
  27387. Add('begin end;');
  27388. Add('var');
  27389. Add(' b: boolean;');
  27390. Add(' vP, vQ: tfuncint;');
  27391. Add('begin');
  27392. Add(' vp:=nil;');
  27393. Add(' vp:=vp;');
  27394. Add(' vp:=@doit;'); // ok in fpc and delphi
  27395. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27396. Add(' vp;'); // ok in fpc and delphi
  27397. Add(' vp();');
  27398. Add(' vp(2);');
  27399. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27400. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27401. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27402. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27403. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27404. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27405. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27406. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27407. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27408. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27409. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27410. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27411. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27412. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27413. Add(' b:=Assigned(vp);');
  27414. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27415. Add(' doit(vp());'); // ok in fpc and delphi
  27416. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27417. ConvertProgram;
  27418. CheckSource('TestProcType_FunctionDelphi',
  27419. LinesToStr([ // statements
  27420. 'this.DoIt = function(vI) {',
  27421. ' var Result = 0;',
  27422. ' return Result;',
  27423. '};',
  27424. 'this.b = false;',
  27425. 'this.vP = null;',
  27426. 'this.vQ = null;'
  27427. ]),
  27428. LinesToStr([ // $mod.$main
  27429. '$mod.vP = null;',
  27430. '$mod.vP = $mod.vP;',
  27431. '$mod.vP = $mod.DoIt;',
  27432. '$mod.vP = $mod.DoIt;',
  27433. '$mod.vP(1);',
  27434. '$mod.vP(1);',
  27435. '$mod.vP(2);',
  27436. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27437. '$mod.b = $mod.vP(1) === 3;',
  27438. '$mod.b = 4 === $mod.vP(1);',
  27439. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27440. '$mod.b = $mod.vP(1) !== 5;',
  27441. '$mod.b = 6 !== $mod.vP(1);',
  27442. '$mod.b = $mod.vP != null;',
  27443. '$mod.DoIt($mod.vP(1));',
  27444. '$mod.DoIt($mod.vP(1));',
  27445. '$mod.DoIt($mod.vP(2));',
  27446. '']));
  27447. end;
  27448. procedure TTestModule.TestProcType_ProcedureDelphi;
  27449. begin
  27450. StartProgram(false);
  27451. Add('{$mode Delphi}');
  27452. Add('type');
  27453. Add(' TProc = procedure;');
  27454. Add('procedure DoIt;');
  27455. Add('begin end;');
  27456. Add('var');
  27457. Add(' b: boolean;');
  27458. Add(' vP, vQ: tproc;');
  27459. Add('begin');
  27460. Add(' vp:=nil;');
  27461. Add(' vp:=vp;');
  27462. Add(' vp:=vq;');
  27463. 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
  27464. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27465. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27466. Add(' vp;'); // ok in fpc and delphi
  27467. Add(' vp();');
  27468. // equal
  27469. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27470. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27471. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27472. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27473. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27474. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27475. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27476. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27477. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27478. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27479. // unequal
  27480. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27481. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27482. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27483. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27484. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27485. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27486. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27487. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27488. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27489. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27490. Add(' b:=Assigned(vp);');
  27491. ConvertProgram;
  27492. CheckSource('TestProcType_ProcedureDelphi',
  27493. LinesToStr([ // statements
  27494. 'this.DoIt = function() {',
  27495. '};',
  27496. 'this.b = false;',
  27497. 'this.vP = null;',
  27498. 'this.vQ = null;'
  27499. ]),
  27500. LinesToStr([ // $mod.$main
  27501. '$mod.vP = null;',
  27502. '$mod.vP = $mod.vP;',
  27503. '$mod.vP = $mod.vQ;',
  27504. '$mod.vP = $mod.DoIt;',
  27505. '$mod.vP = $mod.DoIt;',
  27506. '$mod.vP();',
  27507. '$mod.vP();',
  27508. '$mod.b = $mod.vP === null;',
  27509. '$mod.b = null === $mod.vP;',
  27510. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27511. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27512. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27513. '$mod.b = $mod.vP !== null;',
  27514. '$mod.b = null !== $mod.vP;',
  27515. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27516. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27517. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27518. '$mod.b = $mod.vP != null;',
  27519. '']));
  27520. end;
  27521. procedure TTestModule.TestProcType_AsParam;
  27522. begin
  27523. StartProgram(false);
  27524. Add('type');
  27525. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27526. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27527. Add('var vJ: tfuncint;');
  27528. Add('begin');
  27529. Add(' vg:=vg;');
  27530. Add(' vj:=vh;');
  27531. Add(' vi:=vi;');
  27532. Add(' doit(vg,vg,vg);');
  27533. Add(' doit(vh,vh,vj);');
  27534. Add(' doit(vi,vi,vi);');
  27535. Add(' doit(vj,vj,vj);');
  27536. Add('end;');
  27537. Add('var i: tfuncint;');
  27538. Add('begin');
  27539. Add(' doit(i,i,i);');
  27540. ConvertProgram;
  27541. CheckSource('TestProcType_AsParam',
  27542. LinesToStr([ // statements
  27543. 'this.DoIt = function (vG,vH,vI) {',
  27544. ' var vJ = null;',
  27545. ' vG = vG;',
  27546. ' vJ = vH;',
  27547. ' vI.set(vI.get());',
  27548. ' $mod.DoIt(vG, vG, {',
  27549. ' get: function () {',
  27550. ' return vG;',
  27551. ' },',
  27552. ' set: function (v) {',
  27553. ' vG = v;',
  27554. ' }',
  27555. ' });',
  27556. ' $mod.DoIt(vH, vH, {',
  27557. ' get: function () {',
  27558. ' return vJ;',
  27559. ' },',
  27560. ' set: function (v) {',
  27561. ' vJ = v;',
  27562. ' }',
  27563. ' });',
  27564. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27565. ' $mod.DoIt(vJ, vJ, {',
  27566. ' get: function () {',
  27567. ' return vJ;',
  27568. ' },',
  27569. ' set: function (v) {',
  27570. ' vJ = v;',
  27571. ' }',
  27572. ' });',
  27573. '};',
  27574. 'this.i = null;'
  27575. ]),
  27576. LinesToStr([
  27577. '$mod.DoIt($mod.i,$mod.i,{',
  27578. ' p: $mod,',
  27579. ' get: function () {',
  27580. ' return this.p.i;',
  27581. ' },',
  27582. ' set: function (v) {',
  27583. ' this.p.i = v;',
  27584. ' }',
  27585. '});'
  27586. ]));
  27587. end;
  27588. procedure TTestModule.TestProcType_MethodFPC;
  27589. begin
  27590. StartProgram(false);
  27591. Add('type');
  27592. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27593. Add(' TObject = class');
  27594. Add(' function DoIt(vA: longint = 1): longint;');
  27595. Add(' end;');
  27596. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27597. Add('begin');
  27598. Add('end;');
  27599. Add('var');
  27600. Add(' Obj: TObject;');
  27601. Add(' vP: tfuncint;');
  27602. Add(' b: boolean;');
  27603. Add('begin');
  27604. Add(' vp:[email protected];'); // ok in fpc and delphi
  27605. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27606. Add(' vp;'); // ok in fpc and delphi
  27607. Add(' vp();');
  27608. Add(' vp(2);');
  27609. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27610. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27611. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27612. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27613. ConvertProgram;
  27614. CheckSource('TestProcType_MethodFPC',
  27615. LinesToStr([ // statements
  27616. 'rtl.createClass(this, "TObject", null, function () {',
  27617. ' this.$init = function () {',
  27618. ' };',
  27619. ' this.$final = function () {',
  27620. ' };',
  27621. ' this.DoIt = function (vA) {',
  27622. ' var Result = 0;',
  27623. ' return Result;',
  27624. ' };',
  27625. '});',
  27626. 'this.Obj = null;',
  27627. 'this.vP = null;',
  27628. 'this.b = false;'
  27629. ]),
  27630. LinesToStr([
  27631. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27632. '$mod.vP(1);',
  27633. '$mod.vP(1);',
  27634. '$mod.vP(2);',
  27635. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27636. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27637. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27638. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27639. '']));
  27640. end;
  27641. procedure TTestModule.TestProcType_MethodDelphi;
  27642. begin
  27643. StartProgram(false);
  27644. Add([
  27645. '{$mode delphi}',
  27646. 'type',
  27647. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27648. ' TObject = class',
  27649. ' function DoIt(vA: longint = 1): longint;',
  27650. ' end;',
  27651. 'function TObject.DoIt(vA: longint = 1): longint;',
  27652. 'begin',
  27653. 'end;',
  27654. 'var',
  27655. ' Obj: TObject;',
  27656. ' vP: tfuncint;',
  27657. ' b: boolean;',
  27658. 'begin',
  27659. ' vp:[email protected];', // ok in fpc and delphi
  27660. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27661. ' vp;', // ok in fpc and delphi
  27662. ' vp();',
  27663. ' vp(2);',
  27664. //' b:[email protected];', // ok in fpc, illegal in delphi
  27665. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27666. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27667. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27668. '']);
  27669. ConvertProgram;
  27670. CheckSource('TestProcType_MethodDelphi',
  27671. LinesToStr([ // statements
  27672. 'rtl.createClass(this, "TObject", null, function () {',
  27673. ' this.$init = function () {',
  27674. ' };',
  27675. ' this.$final = function () {',
  27676. ' };',
  27677. ' this.DoIt = function (vA) {',
  27678. ' var Result = 0;',
  27679. ' return Result;',
  27680. ' };',
  27681. '});',
  27682. 'this.Obj = null;',
  27683. 'this.vP = null;',
  27684. 'this.b = false;'
  27685. ]),
  27686. LinesToStr([
  27687. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27688. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27689. '$mod.vP(1);',
  27690. '$mod.vP(1);',
  27691. '$mod.vP(2);',
  27692. '']));
  27693. end;
  27694. procedure TTestModule.TestProcType_PropertyFPC;
  27695. begin
  27696. StartProgram(false);
  27697. Add('type');
  27698. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27699. Add(' TObject = class');
  27700. Add(' FOnFoo: TFuncInt;');
  27701. Add(' function DoIt(vA: longint = 1): longint;');
  27702. Add(' function GetFoo: TFuncInt;');
  27703. Add(' procedure SetFoo(const Value: TFuncInt);');
  27704. Add(' function GetEvents(Index: longint): TFuncInt;');
  27705. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27706. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27707. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27708. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27709. Add(' end;');
  27710. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27711. Add('function tobject.getfoo: tfuncint; begin end;');
  27712. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27713. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27714. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27715. Add('var');
  27716. Add(' Obj: TObject;');
  27717. Add(' vP: tfuncint;');
  27718. Add(' b: boolean;');
  27719. Add('begin');
  27720. Add(' obj.onfoo:=nil;');
  27721. Add(' obj.onbar:=nil;');
  27722. Add(' obj.events[1]:=nil;');
  27723. Add(' obj.onfoo:=obj.onfoo;');
  27724. Add(' obj.onbar:=obj.onbar;');
  27725. Add(' obj.events[2]:=obj.events[3];');
  27726. Add(' obj.onfoo:[email protected];');
  27727. Add(' obj.onbar:[email protected];');
  27728. Add(' obj.events[4]:[email protected];');
  27729. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27730. //Add(' obj.onbar:=obj.doit;'); // delphi
  27731. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27732. Add(' obj.onfoo;');
  27733. Add(' obj.onbar;');
  27734. //Add(' obj.events[5];'); ToDo in pasresolver
  27735. Add(' obj.onfoo();');
  27736. Add(' obj.onbar();');
  27737. Add(' obj.events[6]();');
  27738. Add(' b:=obj.onfoo=nil;');
  27739. Add(' b:=obj.onbar=nil;');
  27740. Add(' b:=obj.events[7]=nil;');
  27741. Add(' b:=obj.onfoo<>nil;');
  27742. Add(' b:=obj.onbar<>nil;');
  27743. Add(' b:=obj.events[8]<>nil;');
  27744. Add(' b:=obj.onfoo=vp;');
  27745. Add(' b:=obj.onbar=vp;');
  27746. Add(' b:=obj.events[9]=vp;');
  27747. Add(' b:=obj.onfoo=obj.onfoo;');
  27748. Add(' b:=obj.onbar=obj.onfoo;');
  27749. Add(' b:=obj.events[10]=obj.onfoo;');
  27750. Add(' b:=obj.onfoo<>obj.onfoo;');
  27751. Add(' b:=obj.onbar<>obj.onfoo;');
  27752. Add(' b:=obj.events[11]<>obj.onfoo;');
  27753. Add(' b:[email protected];');
  27754. Add(' b:[email protected];');
  27755. Add(' b:=obj.events[12][email protected];');
  27756. Add(' b:=obj.onfoo<>@obj.doit;');
  27757. Add(' b:=obj.onbar<>@obj.doit;');
  27758. Add(' b:=obj.events[12]<>@obj.doit;');
  27759. Add(' b:=Assigned(obj.onfoo);');
  27760. Add(' b:=Assigned(obj.onbar);');
  27761. Add(' b:=Assigned(obj.events[13]);');
  27762. ConvertProgram;
  27763. CheckSource('TestProcType_PropertyFPC',
  27764. LinesToStr([ // statements
  27765. 'rtl.createClass(this, "TObject", null, function () {',
  27766. ' this.$init = function () {',
  27767. ' this.FOnFoo = null;',
  27768. ' };',
  27769. ' this.$final = function () {',
  27770. ' this.FOnFoo = undefined;',
  27771. ' };',
  27772. ' this.DoIt = function (vA) {',
  27773. ' var Result = 0;',
  27774. ' return Result;',
  27775. ' };',
  27776. 'this.GetFoo = function () {',
  27777. ' var Result = null;',
  27778. ' return Result;',
  27779. '};',
  27780. 'this.SetFoo = function (Value) {',
  27781. '};',
  27782. 'this.GetEvents = function (Index) {',
  27783. ' var Result = null;',
  27784. ' return Result;',
  27785. '};',
  27786. 'this.SetEvents = function (Index, Value) {',
  27787. '};',
  27788. '});',
  27789. 'this.Obj = null;',
  27790. 'this.vP = null;',
  27791. 'this.b = false;'
  27792. ]),
  27793. LinesToStr([
  27794. '$mod.Obj.FOnFoo = null;',
  27795. '$mod.Obj.SetFoo(null);',
  27796. '$mod.Obj.SetEvents(1, null);',
  27797. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27798. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27799. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27800. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27801. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27802. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27803. '$mod.Obj.FOnFoo(1);',
  27804. '$mod.Obj.GetFoo();',
  27805. '$mod.Obj.FOnFoo(1);',
  27806. '$mod.Obj.GetFoo()(1);',
  27807. '$mod.Obj.GetEvents(6)(1);',
  27808. '$mod.b = $mod.Obj.FOnFoo === null;',
  27809. '$mod.b = $mod.Obj.GetFoo() === null;',
  27810. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27811. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27812. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27813. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27814. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27815. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27816. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27817. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27818. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27819. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27820. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27821. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27822. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27823. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27824. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27825. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27826. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27827. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27828. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27829. '$mod.b = $mod.Obj.FOnFoo != null;',
  27830. '$mod.b = $mod.Obj.GetFoo() != null;',
  27831. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27832. '']));
  27833. end;
  27834. procedure TTestModule.TestProcType_PropertyDelphi;
  27835. begin
  27836. StartProgram(false);
  27837. Add('{$mode delphi}');
  27838. Add('type');
  27839. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27840. Add(' TObject = class');
  27841. Add(' FOnFoo: TFuncInt;');
  27842. Add(' function DoIt(vA: longint = 1): longint;');
  27843. Add(' function GetFoo: TFuncInt;');
  27844. Add(' procedure SetFoo(const Value: TFuncInt);');
  27845. Add(' function GetEvents(Index: longint): TFuncInt;');
  27846. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27847. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27848. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27849. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27850. Add(' end;');
  27851. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27852. Add('function tobject.getfoo: tfuncint; begin end;');
  27853. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27854. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27855. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27856. Add('var');
  27857. Add(' Obj: TObject;');
  27858. Add(' vP: tfuncint;');
  27859. Add(' b: boolean;');
  27860. Add('begin');
  27861. Add(' obj.onfoo:=nil;');
  27862. Add(' obj.onbar:=nil;');
  27863. Add(' obj.events[1]:=nil;');
  27864. Add(' obj.onfoo:=obj.onfoo;');
  27865. Add(' obj.onbar:=obj.onbar;');
  27866. Add(' obj.events[2]:=obj.events[3];');
  27867. Add(' obj.onfoo:[email protected];');
  27868. Add(' obj.onbar:[email protected];');
  27869. Add(' obj.events[4]:[email protected];');
  27870. Add(' obj.onfoo:=obj.doit;'); // delphi
  27871. Add(' obj.onbar:=obj.doit;'); // delphi
  27872. Add(' obj.events[4]:=obj.doit;'); // delphi
  27873. Add(' obj.onfoo;');
  27874. Add(' obj.onbar;');
  27875. //Add(' obj.events[5];'); ToDo in pasresolver
  27876. Add(' obj.onfoo();');
  27877. Add(' obj.onbar();');
  27878. Add(' obj.events[6]();');
  27879. //Add(' b:=obj.onfoo=nil;'); // fpc
  27880. //Add(' b:=obj.onbar=nil;'); // fpc
  27881. //Add(' b:=obj.events[7]=nil;'); // fpc
  27882. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27883. //Add(' b:=obj.onbar<>nil;'); // fpc
  27884. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27885. Add(' b:=obj.onfoo=vp;');
  27886. Add(' b:=obj.onbar=vp;');
  27887. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27888. Add(' b:=obj.onfoo=obj.onfoo;');
  27889. Add(' b:=obj.onbar=obj.onfoo;');
  27890. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27891. Add(' b:=obj.onfoo<>obj.onfoo;');
  27892. Add(' b:=obj.onbar<>obj.onfoo;');
  27893. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27894. //Add(' b:[email protected];'); // fpc
  27895. //Add(' b:[email protected];'); // fpc
  27896. //Add(' b:=obj.events[12][email protected];'); // fpc
  27897. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27898. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27899. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27900. Add(' b:=Assigned(obj.onfoo);');
  27901. Add(' b:=Assigned(obj.onbar);');
  27902. Add(' b:=Assigned(obj.events[13]);');
  27903. ConvertProgram;
  27904. CheckSource('TestProcType_PropertyDelphi',
  27905. LinesToStr([ // statements
  27906. 'rtl.createClass(this, "TObject", null, function () {',
  27907. ' this.$init = function () {',
  27908. ' this.FOnFoo = null;',
  27909. ' };',
  27910. ' this.$final = function () {',
  27911. ' this.FOnFoo = undefined;',
  27912. ' };',
  27913. ' this.DoIt = function (vA) {',
  27914. ' var Result = 0;',
  27915. ' return Result;',
  27916. ' };',
  27917. 'this.GetFoo = function () {',
  27918. ' var Result = null;',
  27919. ' return Result;',
  27920. '};',
  27921. 'this.SetFoo = function (Value) {',
  27922. '};',
  27923. 'this.GetEvents = function (Index) {',
  27924. ' var Result = null;',
  27925. ' return Result;',
  27926. '};',
  27927. 'this.SetEvents = function (Index, Value) {',
  27928. '};',
  27929. '});',
  27930. 'this.Obj = null;',
  27931. 'this.vP = null;',
  27932. 'this.b = false;'
  27933. ]),
  27934. LinesToStr([
  27935. '$mod.Obj.FOnFoo = null;',
  27936. '$mod.Obj.SetFoo(null);',
  27937. '$mod.Obj.SetEvents(1, null);',
  27938. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27939. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27940. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27941. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27942. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27943. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27944. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27945. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27946. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27947. '$mod.Obj.FOnFoo(1);',
  27948. '$mod.Obj.GetFoo();',
  27949. '$mod.Obj.FOnFoo(1);',
  27950. '$mod.Obj.GetFoo()(1);',
  27951. '$mod.Obj.GetEvents(6)(1);',
  27952. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27953. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27954. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27955. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27956. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27957. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27958. '$mod.b = $mod.Obj.FOnFoo != null;',
  27959. '$mod.b = $mod.Obj.GetFoo() != null;',
  27960. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27961. '']));
  27962. end;
  27963. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27964. begin
  27965. StartProgram(false);
  27966. Add('type');
  27967. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27968. Add(' TObject = class');
  27969. Add(' FOnFoo: TFuncInt;');
  27970. Add(' function DoIt(vA: longint = 1): longint;');
  27971. Add(' function GetFoo: TFuncInt;');
  27972. Add(' procedure SetFoo(const Value: TFuncInt);');
  27973. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27974. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27975. Add(' end;');
  27976. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27977. Add('function tobject.getfoo: tfuncint; begin end;');
  27978. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27979. Add('var');
  27980. Add(' Obj: TObject;');
  27981. Add(' vP: tfuncint;');
  27982. Add(' b: boolean;');
  27983. Add('begin');
  27984. Add('with obj do begin');
  27985. Add(' fonfoo:=nil;');
  27986. Add(' onfoo:=nil;');
  27987. Add(' onbar:=nil;');
  27988. Add(' fonfoo:=fonfoo;');
  27989. Add(' onfoo:=onfoo;');
  27990. Add(' onbar:=onbar;');
  27991. Add(' fonfoo:=@doit;');
  27992. Add(' onfoo:=@doit;');
  27993. Add(' onbar:=@doit;');
  27994. //Add(' fonfoo:=doit;'); // delphi
  27995. //Add(' onfoo:=doit;'); // delphi
  27996. //Add(' onbar:=doit;'); // delphi
  27997. Add(' fonfoo;');
  27998. Add(' onfoo;');
  27999. Add(' onbar;');
  28000. Add(' fonfoo();');
  28001. Add(' onfoo();');
  28002. Add(' onbar();');
  28003. Add(' b:=fonfoo=nil;');
  28004. Add(' b:=onfoo=nil;');
  28005. Add(' b:=onbar=nil;');
  28006. Add(' b:=fonfoo<>nil;');
  28007. Add(' b:=onfoo<>nil;');
  28008. Add(' b:=onbar<>nil;');
  28009. Add(' b:=fonfoo=vp;');
  28010. Add(' b:=onfoo=vp;');
  28011. Add(' b:=onbar=vp;');
  28012. Add(' b:=fonfoo=fonfoo;');
  28013. Add(' b:=onfoo=onfoo;');
  28014. Add(' b:=onbar=onfoo;');
  28015. Add(' b:=fonfoo<>fonfoo;');
  28016. Add(' b:=onfoo<>onfoo;');
  28017. Add(' b:=onbar<>onfoo;');
  28018. Add(' b:=fonfoo=@doit;');
  28019. Add(' b:=onfoo=@doit;');
  28020. Add(' b:=onbar=@doit;');
  28021. Add(' b:=fonfoo<>@doit;');
  28022. Add(' b:=onfoo<>@doit;');
  28023. Add(' b:=onbar<>@doit;');
  28024. Add(' b:=Assigned(fonfoo);');
  28025. Add(' b:=Assigned(onfoo);');
  28026. Add(' b:=Assigned(onbar);');
  28027. Add('end;');
  28028. ConvertProgram;
  28029. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  28030. LinesToStr([ // statements
  28031. 'rtl.createClass(this, "TObject", null, function () {',
  28032. ' this.$init = function () {',
  28033. ' this.FOnFoo = null;',
  28034. ' };',
  28035. ' this.$final = function () {',
  28036. ' this.FOnFoo = undefined;',
  28037. ' };',
  28038. ' this.DoIt = function (vA) {',
  28039. ' var Result = 0;',
  28040. ' return Result;',
  28041. ' };',
  28042. ' this.GetFoo = function () {',
  28043. ' var Result = null;',
  28044. ' return Result;',
  28045. ' };',
  28046. ' this.SetFoo = function (Value) {',
  28047. ' };',
  28048. '});',
  28049. 'this.Obj = null;',
  28050. 'this.vP = null;',
  28051. 'this.b = false;'
  28052. ]),
  28053. LinesToStr([
  28054. 'var $with = $mod.Obj;',
  28055. '$with.FOnFoo = null;',
  28056. '$with.FOnFoo = null;',
  28057. '$with.SetFoo(null);',
  28058. '$with.FOnFoo = $with.FOnFoo;',
  28059. '$with.FOnFoo = $with.FOnFoo;',
  28060. '$with.SetFoo($with.GetFoo());',
  28061. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28062. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28063. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28064. '$with.FOnFoo(1);',
  28065. '$with.FOnFoo(1);',
  28066. '$with.GetFoo();',
  28067. '$with.FOnFoo(1);',
  28068. '$with.FOnFoo(1);',
  28069. '$with.GetFoo()(1);',
  28070. '$mod.b = $with.FOnFoo === null;',
  28071. '$mod.b = $with.FOnFoo === null;',
  28072. '$mod.b = $with.GetFoo() === null;',
  28073. '$mod.b = $with.FOnFoo !== null;',
  28074. '$mod.b = $with.FOnFoo !== null;',
  28075. '$mod.b = $with.GetFoo() !== null;',
  28076. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28077. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28078. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28079. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28080. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28081. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28082. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28083. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28084. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28085. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28086. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28087. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28088. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28089. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28090. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28091. '$mod.b = $with.FOnFoo != null;',
  28092. '$mod.b = $with.FOnFoo != null;',
  28093. '$mod.b = $with.GetFoo() != null;',
  28094. '']));
  28095. end;
  28096. procedure TTestModule.TestProcType_Nested;
  28097. begin
  28098. StartProgram(false);
  28099. Add([
  28100. 'type',
  28101. ' TProcInt = procedure(vI: longint = 1);',
  28102. 'procedure DoIt(vJ: longint);',
  28103. 'var aProc: TProcInt;',
  28104. ' b: boolean;',
  28105. ' procedure Sub(vK: longint);',
  28106. ' var aSub: TProcInt;',
  28107. ' procedure SubSub(vK: longint);',
  28108. ' var aSubSub: TProcInt;',
  28109. ' begin;',
  28110. ' aProc:=@DoIt;',
  28111. ' aSub:=@DoIt;',
  28112. ' aSubSub:=@DoIt;',
  28113. ' aProc:=@Sub;',
  28114. ' aSub:=@Sub;',
  28115. ' aSubSub:=@Sub;',
  28116. ' aProc:=@SubSub;',
  28117. ' aSub:=@SubSub;',
  28118. ' aSubSub:=@SubSub;',
  28119. ' end;',
  28120. ' begin;',
  28121. ' end;',
  28122. 'begin;',
  28123. ' aProc:=@Sub;',
  28124. ' b:=aProc=@Sub;',
  28125. ' b:=@Sub=aProc;',
  28126. 'end;',
  28127. 'begin',
  28128. '']);
  28129. ConvertProgram;
  28130. CheckSource('TestProcType_Nested',
  28131. LinesToStr([ // statements
  28132. 'this.DoIt = function (vJ) {',
  28133. ' var aProc = null;',
  28134. ' var b = false;',
  28135. ' function Sub(vK) {',
  28136. ' var aSub = null;',
  28137. ' function SubSub(vK) {',
  28138. ' var aSubSub = null;',
  28139. ' aProc = $mod.DoIt;',
  28140. ' aSub = $mod.DoIt;',
  28141. ' aSubSub = $mod.DoIt;',
  28142. ' aProc = Sub;',
  28143. ' aSub = Sub;',
  28144. ' aSubSub = Sub;',
  28145. ' aProc = SubSub;',
  28146. ' aSub = SubSub;',
  28147. ' aSubSub = SubSub;',
  28148. ' };',
  28149. ' };',
  28150. ' aProc = Sub;',
  28151. ' b = rtl.eqCallback(aProc, Sub);',
  28152. ' b = rtl.eqCallback(Sub, aProc);',
  28153. '};',
  28154. '']),
  28155. LinesToStr([ // $mod.$main
  28156. '']));
  28157. end;
  28158. procedure TTestModule.TestProcType_NestedOfObject;
  28159. begin
  28160. StartProgram(false);
  28161. Add([
  28162. 'type',
  28163. ' TProcInt = procedure(vI: longint = 1) of object;',
  28164. ' TObject = class',
  28165. ' procedure DoIt(vJ: longint);',
  28166. ' end;',
  28167. 'procedure TObject.DoIt(vJ: longint);',
  28168. 'var aProc: TProcInt;',
  28169. ' b: boolean;',
  28170. ' procedure Sub(vK: longint);',
  28171. ' var aSub: TProcInt;',
  28172. ' procedure SubSub(vK: longint);',
  28173. ' var aSubSub: TProcInt;',
  28174. ' begin;',
  28175. ' aProc:=@DoIt;',
  28176. ' aSub:=@DoIt;',
  28177. ' aSubSub:=@DoIt;',
  28178. ' aProc:=@Sub;',
  28179. ' aSub:=@Sub;',
  28180. ' aSubSub:=@Sub;',
  28181. ' aProc:=@SubSub;',
  28182. ' aSub:=@SubSub;',
  28183. ' aSubSub:=@SubSub;',
  28184. ' end;',
  28185. ' begin;',
  28186. ' end;',
  28187. 'begin;',
  28188. ' aProc:=@Sub;',
  28189. ' b:=aProc=@Sub;',
  28190. ' b:=@Sub=aProc;',
  28191. 'end;',
  28192. 'begin',
  28193. '']);
  28194. ConvertProgram;
  28195. CheckSource('TestProcType_Nested',
  28196. LinesToStr([ // statements
  28197. 'rtl.createClass(this, "TObject", null, function () {',
  28198. ' this.$init = function () {',
  28199. ' };',
  28200. ' this.$final = function () {',
  28201. ' };',
  28202. ' this.DoIt = function (vJ) {',
  28203. ' var $Self = this;',
  28204. ' var aProc = null;',
  28205. ' var b = false;',
  28206. ' function Sub(vK) {',
  28207. ' var aSub = null;',
  28208. ' function SubSub(vK) {',
  28209. ' var aSubSub = null;',
  28210. ' aProc = rtl.createCallback($Self, "DoIt");',
  28211. ' aSub = rtl.createCallback($Self, "DoIt");',
  28212. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28213. ' aProc = Sub;',
  28214. ' aSub = Sub;',
  28215. ' aSubSub = Sub;',
  28216. ' aProc = SubSub;',
  28217. ' aSub = SubSub;',
  28218. ' aSubSub = SubSub;',
  28219. ' };',
  28220. ' };',
  28221. ' aProc = Sub;',
  28222. ' b = rtl.eqCallback(aProc, Sub);',
  28223. ' b = rtl.eqCallback(Sub, aProc);',
  28224. ' };',
  28225. '});',
  28226. '']),
  28227. LinesToStr([ // $mod.$main
  28228. '']));
  28229. end;
  28230. procedure TTestModule.TestProcType_ReferenceToProc;
  28231. begin
  28232. StartProgram(false);
  28233. Add([
  28234. 'type',
  28235. ' TProcRef = reference to procedure(i: longint = 0);',
  28236. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28237. 'var',
  28238. ' p: TProcRef;',
  28239. ' f: TFuncRef;',
  28240. 'procedure DoIt(i: longint);',
  28241. 'begin',
  28242. 'end;',
  28243. 'function GetIt(i: longint): longint;',
  28244. 'begin',
  28245. ' p:=@DoIt;',
  28246. ' f:=@GetIt;',
  28247. ' f;',
  28248. ' f();',
  28249. ' f(1);',
  28250. 'end;',
  28251. 'begin',
  28252. ' p:=@DoIt;',
  28253. ' f:=@GetIt;',
  28254. ' f;',
  28255. ' f();',
  28256. ' f(1);',
  28257. ' p:=TProcRef(f);',
  28258. '']);
  28259. ConvertProgram;
  28260. CheckSource('TestProcType_ReferenceToProc',
  28261. LinesToStr([ // statements
  28262. 'this.p = null;',
  28263. 'this.f = null;',
  28264. 'this.DoIt = function (i) {',
  28265. '};',
  28266. 'this.GetIt = function (i) {',
  28267. ' var Result = 0;',
  28268. ' $mod.p = $mod.DoIt;',
  28269. ' $mod.f = $mod.GetIt;',
  28270. ' $mod.f(0);',
  28271. ' $mod.f(0);',
  28272. ' $mod.f(1);',
  28273. ' return Result;',
  28274. '};',
  28275. '']),
  28276. LinesToStr([ // $mod.$main
  28277. '$mod.p = $mod.DoIt;',
  28278. '$mod.f = $mod.GetIt;',
  28279. '$mod.f(0);',
  28280. '$mod.f(0);',
  28281. '$mod.f(1);',
  28282. '$mod.p = $mod.f;',
  28283. '']));
  28284. end;
  28285. procedure TTestModule.TestProcType_ReferenceToMethod;
  28286. begin
  28287. StartProgram(false);
  28288. Add([
  28289. 'type',
  28290. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28291. ' TObject = class',
  28292. ' function Grow(s: longint): longint;',
  28293. ' end;',
  28294. 'var',
  28295. ' f: tfuncref;',
  28296. 'function tobject.grow(s: longint): longint;',
  28297. ' function GrowSub(i: longint): longint;',
  28298. ' begin',
  28299. ' f:=@grow;',
  28300. ' f:=@growsub;',
  28301. ' end;',
  28302. 'begin',
  28303. ' f:=@grow;',
  28304. ' f:=@growsub;',
  28305. 'end;',
  28306. 'begin',
  28307. '']);
  28308. ConvertProgram;
  28309. CheckSource('TestProcType_ReferenceToMethod',
  28310. LinesToStr([ // statements
  28311. 'rtl.createClass(this, "TObject", null, function () {',
  28312. ' this.$init = function () {',
  28313. ' };',
  28314. ' this.$final = function () {',
  28315. ' };',
  28316. ' this.Grow = function (s) {',
  28317. ' var $Self = this;',
  28318. ' var Result = 0;',
  28319. ' function GrowSub(i) {',
  28320. ' var Result = 0;',
  28321. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28322. ' $mod.f = GrowSub;',
  28323. ' return Result;',
  28324. ' };',
  28325. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28326. ' $mod.f = GrowSub;',
  28327. ' return Result;',
  28328. ' };',
  28329. '});',
  28330. 'this.f = null;',
  28331. '']),
  28332. LinesToStr([ // $mod.$main
  28333. '']));
  28334. end;
  28335. procedure TTestModule.TestProcType_Typecast;
  28336. begin
  28337. StartProgram(false);
  28338. Add([
  28339. 'type',
  28340. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28341. ' TEvent = procedure of object;',
  28342. ' TGetter = function:longint of object;',
  28343. ' TProcA = procedure(i: longint);',
  28344. ' TFuncB = function(i, j: longint): longint;',
  28345. 'procedure DoIt(); varargs; begin end;',
  28346. 'var',
  28347. ' Notify: tnotifyevent;',
  28348. ' Event: tevent;',
  28349. ' Getter: tgetter;',
  28350. ' ProcA: tproca;',
  28351. ' FuncB: tfuncb;',
  28352. ' p: pointer;',
  28353. 'begin',
  28354. ' notify:=tnotifyevent(event);',
  28355. ' event:=tevent(event);',
  28356. ' event:=tevent(notify);',
  28357. ' event:=tevent(getter);',
  28358. ' event:=tevent(proca);',
  28359. ' proca:=tproca(funcb);',
  28360. ' funcb:=tfuncb(funcb);',
  28361. ' funcb:=tfuncb(proca);',
  28362. ' funcb:=tfuncb(getter);',
  28363. ' proca:=tproca(p);',
  28364. ' funcb:=tfuncb(p);',
  28365. ' getter:=tgetter(p);',
  28366. ' p:=pointer(notify);',
  28367. ' p:=notify;',
  28368. ' p:=pointer(proca);',
  28369. ' p:=proca;',
  28370. ' p:=pointer(funcb);',
  28371. ' p:=funcb;',
  28372. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28373. '']);
  28374. ConvertProgram;
  28375. CheckSource('TestProcType_Typecast',
  28376. LinesToStr([ // statements
  28377. 'this.DoIt = function () {',
  28378. '};',
  28379. 'this.Notify = null;',
  28380. 'this.Event = null;',
  28381. 'this.Getter = null;',
  28382. 'this.ProcA = null;',
  28383. 'this.FuncB = null;',
  28384. 'this.p = null;',
  28385. '']),
  28386. LinesToStr([ // $mod.$main
  28387. '$mod.Notify = $mod.Event;',
  28388. '$mod.Event = $mod.Event;',
  28389. '$mod.Event = $mod.Notify;',
  28390. '$mod.Event = $mod.Getter;',
  28391. '$mod.Event = $mod.ProcA;',
  28392. '$mod.ProcA = $mod.FuncB;',
  28393. '$mod.FuncB = $mod.FuncB;',
  28394. '$mod.FuncB = $mod.ProcA;',
  28395. '$mod.FuncB = $mod.Getter;',
  28396. '$mod.ProcA = $mod.p;',
  28397. '$mod.FuncB = $mod.p;',
  28398. '$mod.Getter = $mod.p;',
  28399. '$mod.p = $mod.Notify;',
  28400. '$mod.p = $mod.Notify;',
  28401. '$mod.p = $mod.ProcA;',
  28402. '$mod.p = $mod.ProcA;',
  28403. '$mod.p = $mod.FuncB;',
  28404. '$mod.p = $mod.FuncB;',
  28405. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28406. '']));
  28407. end;
  28408. procedure TTestModule.TestProcType_PassProcToUntyped;
  28409. begin
  28410. StartProgram(false);
  28411. Add([
  28412. 'type',
  28413. ' TEvent = procedure of object;',
  28414. ' TFunc = function: longint;',
  28415. 'procedure DoIt(); varargs; begin end;',
  28416. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28417. 'var',
  28418. ' Event: tevent;',
  28419. ' Func: TFunc;',
  28420. 'begin',
  28421. ' doit(event,func);',
  28422. ' dosome(event,event,event);',
  28423. ' dosome(func,func,func);',
  28424. '']);
  28425. ConvertProgram;
  28426. CheckSource('TestProcType_PassProcToUntyped',
  28427. LinesToStr([ // statements
  28428. 'this.DoIt = function () {',
  28429. '};',
  28430. 'this.DoSome = function (a, b, p) {',
  28431. '};',
  28432. 'this.Event = null;',
  28433. 'this.Func = null;',
  28434. '']),
  28435. LinesToStr([ // $mod.$main
  28436. '$mod.DoIt($mod.Event, $mod.Func);',
  28437. '$mod.DoSome($mod.Event, {',
  28438. ' p: $mod,',
  28439. ' get: function () {',
  28440. ' return this.p.Event;',
  28441. ' },',
  28442. ' set: function (v) {',
  28443. ' this.p.Event = v;',
  28444. ' }',
  28445. '}, $mod.Event);',
  28446. '$mod.DoSome($mod.Func, {',
  28447. ' p: $mod,',
  28448. ' get: function () {',
  28449. ' return this.p.Func;',
  28450. ' },',
  28451. ' set: function (v) {',
  28452. ' this.p.Func = v;',
  28453. ' }',
  28454. '}, $mod.Func);',
  28455. '']));
  28456. end;
  28457. procedure TTestModule.TestProcType_PassProcToArray;
  28458. begin
  28459. StartProgram(false);
  28460. Add([
  28461. 'type',
  28462. ' TFunc = function: longint;',
  28463. ' TArrFunc = array of TFunc;',
  28464. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28465. 'function GetIt: longint; begin end;',
  28466. 'var',
  28467. ' Func: tfunc;',
  28468. 'begin',
  28469. ' doit([]);',
  28470. ' doit([@GetIt]);',
  28471. ' doit([Func]);',
  28472. '']);
  28473. ConvertProgram;
  28474. CheckSource('TestProcType_PassProcToArray',
  28475. LinesToStr([ // statements
  28476. 'this.DoIt = function (Arr) {',
  28477. '};',
  28478. 'this.GetIt = function () {',
  28479. ' var Result = 0;',
  28480. ' return Result;',
  28481. '};',
  28482. 'this.Func = null;',
  28483. '']),
  28484. LinesToStr([ // $mod.$main
  28485. '$mod.DoIt([]);',
  28486. '$mod.DoIt([$mod.GetIt]);',
  28487. '$mod.DoIt([$mod.Func]);',
  28488. '']));
  28489. end;
  28490. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28491. begin
  28492. StartProgram(false);
  28493. Add([
  28494. '{$modeswitch externalclass}',
  28495. 'type',
  28496. ' TProc = reference to procedure(i: longint); safecall;',
  28497. ' TEvent = procedure(i: longint) of object; safecall;',
  28498. ' TExtA = class external name ''ExtObj''',
  28499. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28500. ' procedure DoSome(Id: longint = 1);',
  28501. ' procedure SetOnClick(const e: TEvent);',
  28502. ' property OnClick: TEvent write SetOnClick;',
  28503. ' class procedure Fly(Id: longint = 1); static;',
  28504. ' procedure SetOnShow(const p: TProc);',
  28505. ' property OnShow: TProc write SetOnShow;',
  28506. ' end;',
  28507. 'procedure Run(i: longint = 1);',
  28508. 'begin',
  28509. 'end;',
  28510. 'var',
  28511. ' Obj: texta;',
  28512. ' e: TEvent;',
  28513. ' p: TProc;',
  28514. 'begin',
  28515. ' e:=e;',
  28516. ' e:[email protected];',
  28517. ' e:[email protected];',
  28518. ' e:=TEvent(@obj.dosome);', // no safecall
  28519. ' obj.OnClick:[email protected];',
  28520. ' obj.OnClick:[email protected];',
  28521. ' obj.setonclick(@obj.doit);',
  28522. ' obj.setonclick(@obj.dosome);',
  28523. ' p:=@Run;',
  28524. ' p:[email protected];',
  28525. ' obj.OnShow:=@Run;',
  28526. ' obj.OnShow:[email protected];',
  28527. ' obj.setOnShow(@Run);',
  28528. ' obj.setOnShow(@TExtA.Fly);',
  28529. ' with obj do begin',
  28530. ' e:=@doit;',
  28531. ' e:=@dosome;',
  28532. ' OnClick:=@doit;',
  28533. ' OnClick:=@dosome;',
  28534. ' setonclick(@doit);',
  28535. ' setonclick(@dosome);',
  28536. ' OnShow:=@Run;',
  28537. ' setOnShow(@Run);',
  28538. ' end;']);
  28539. ConvertProgram;
  28540. CheckSource('TestProcType_SafeCallObjFPC',
  28541. LinesToStr([ // statements
  28542. 'this.Run = function (i) {',
  28543. '};',
  28544. 'this.Obj = null;',
  28545. 'this.e = null;',
  28546. 'this.p = null;',
  28547. '']),
  28548. LinesToStr([ // $mod.$main
  28549. '$mod.e = $mod.e;',
  28550. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28551. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28552. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28553. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28554. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28555. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28556. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28557. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28558. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28559. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28560. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28561. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28562. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28563. 'var $with = $mod.Obj;',
  28564. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28565. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28566. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28567. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28568. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28569. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28570. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28571. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28572. '']));
  28573. end;
  28574. procedure TTestModule.TestProcType_SafeCallDelphi;
  28575. begin
  28576. StartProgram(false);
  28577. Add([
  28578. '{$mode delphi}',
  28579. '{$modeswitch externalclass}',
  28580. 'type',
  28581. ' TProc = reference to procedure(i: longint); safecall;',
  28582. ' TEvent = procedure(i: longint) of object; safecall;',
  28583. ' TExtA = class external name ''ExtObj''',
  28584. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28585. ' procedure DoSome(Id: longint = 1);',
  28586. ' procedure SetOnClick(const e: TEvent);',
  28587. ' property OnClick: TEvent write SetOnClick;',
  28588. ' class procedure Fly(Id: longint = 1); static;',
  28589. ' procedure SetOnShow(const p: TProc);',
  28590. ' property OnShow: TProc write SetOnShow;',
  28591. ' end;',
  28592. 'procedure Run(i: longint = 1);',
  28593. 'begin',
  28594. 'end;',
  28595. 'var',
  28596. ' Obj: texta;',
  28597. ' e: TEvent;',
  28598. ' p: TProc;',
  28599. 'begin',
  28600. ' e:=e;',
  28601. ' e:=obj.doit;',
  28602. ' e:=obj.dosome;',
  28603. ' e:=TEvent(@obj.dosome);', // no safecall
  28604. ' obj.OnClick:=obj.doit;',
  28605. ' obj.OnClick:=obj.dosome;',
  28606. ' obj.setonclick(obj.doit);',
  28607. ' obj.setonclick(obj.dosome);',
  28608. ' p:=Run;',
  28609. ' p:=TExtA.Fly;',
  28610. ' obj.OnShow:=Run;',
  28611. ' obj.OnShow:=TExtA.Fly;',
  28612. ' obj.setOnShow(Run);',
  28613. ' obj.setOnShow(TExtA.Fly);',
  28614. ' with obj do begin',
  28615. ' e:=doit;',
  28616. ' e:=dosome;',
  28617. ' OnClick:=doit;',
  28618. ' OnClick:=dosome;',
  28619. ' setonclick(doit);',
  28620. ' setonclick(dosome);',
  28621. ' OnShow:=@Run;',
  28622. ' setOnShow(@Run);',
  28623. ' end;']);
  28624. ConvertProgram;
  28625. CheckSource('TestProcType_SafeCallDelphi',
  28626. LinesToStr([ // statements
  28627. 'this.Run = function (i) {',
  28628. '};',
  28629. 'this.Obj = null;',
  28630. 'this.e = null;',
  28631. 'this.p = null;',
  28632. '']),
  28633. LinesToStr([ // $mod.$main
  28634. '$mod.e = $mod.e;',
  28635. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28636. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28637. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28638. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28639. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28640. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28641. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28642. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28643. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28644. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28645. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28646. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28647. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28648. 'var $with = $mod.Obj;',
  28649. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28650. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28651. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28652. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28653. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28654. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28655. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28656. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28657. '']));
  28658. end;
  28659. procedure TTestModule.TestProcType_SafeCall_Arg;
  28660. begin
  28661. StartProgram(false);
  28662. Add([
  28663. 'type',
  28664. ' TSafecallProc = reference to procedure; safecall;',
  28665. 'procedure Fly(const aHandler: TSafecallProc);',
  28666. 'var',
  28667. ' P: TSafecallProc;',
  28668. 'begin',
  28669. ' P := aHandler;',
  28670. ' Fly(P);',
  28671. ' Fly(aHandler);',
  28672. 'end;',
  28673. 'begin',
  28674. ' Fly(nil);',
  28675. '']);
  28676. ConvertProgram;
  28677. CheckSource('TestProcType_SafeCall_Arg',
  28678. LinesToStr([ // statements
  28679. 'this.Fly = function (aHandler) {',
  28680. ' var P = null;',
  28681. ' P = aHandler;',
  28682. ' $mod.Fly(P);',
  28683. ' $mod.Fly(aHandler);',
  28684. '};',
  28685. '']),
  28686. LinesToStr([ // $mod.$main
  28687. '$mod.Fly(null);',
  28688. '']));
  28689. end;
  28690. procedure TTestModule.TestPointer;
  28691. begin
  28692. StartProgram(false);
  28693. Add(['type',
  28694. ' TObject = class end;',
  28695. ' TClass = class of TObject;',
  28696. ' TArrInt = array of longint;',
  28697. 'const',
  28698. ' n = nil;',
  28699. 'var',
  28700. ' v: jsvalue;',
  28701. ' Obj: tobject;',
  28702. ' C: tclass;',
  28703. ' a: tarrint;',
  28704. ' p: Pointer = nil;',
  28705. ' s: string;',
  28706. 'begin',
  28707. ' p:=p;',
  28708. ' p:=nil;',
  28709. ' if p=nil then;',
  28710. ' if nil=p then;',
  28711. ' if Assigned(p) then;',
  28712. ' p:=Pointer(v);',
  28713. ' p:=obj;',
  28714. ' p:=c;',
  28715. ' p:=a;',
  28716. ' p:=tobject;',
  28717. ' obj:=TObject(p);',
  28718. ' c:=TClass(p);',
  28719. ' a:=TArrInt(p);',
  28720. ' p:=n;',
  28721. ' p:=Pointer(a);',
  28722. ' p:=pointer(s);',
  28723. ' s:=string(p);',
  28724. '']);
  28725. ConvertProgram;
  28726. CheckSource('TestPointer',
  28727. LinesToStr([ // statements
  28728. 'rtl.createClass(this, "TObject", null, function () {',
  28729. ' this.$init = function () {',
  28730. ' };',
  28731. ' this.$final = function () {',
  28732. ' };',
  28733. '});',
  28734. 'this.n = null;',
  28735. 'this.v = undefined;',
  28736. 'this.Obj = null;',
  28737. 'this.C = null;',
  28738. 'this.a = [];',
  28739. 'this.p = null;',
  28740. 'this.s = "";',
  28741. '']),
  28742. LinesToStr([ // $mod.$main
  28743. '$mod.p = $mod.p;',
  28744. '$mod.p = null;',
  28745. 'if ($mod.p === null) ;',
  28746. 'if (null === $mod.p) ;',
  28747. 'if ($mod.p != null) ;',
  28748. '$mod.p = $mod.v;',
  28749. '$mod.p = $mod.Obj;',
  28750. '$mod.p = $mod.C;',
  28751. '$mod.p = $mod.a;',
  28752. '$mod.p = $mod.TObject;',
  28753. '$mod.Obj = $mod.p;',
  28754. '$mod.C = $mod.p;',
  28755. '$mod.a = $mod.p;',
  28756. '$mod.p = null;',
  28757. '$mod.p = $mod.a;',
  28758. '$mod.p = $mod.s;',
  28759. '$mod.s = $mod.p;',
  28760. '']));
  28761. end;
  28762. procedure TTestModule.TestPointer_Proc;
  28763. begin
  28764. StartProgram(false);
  28765. Add('type');
  28766. Add(' TObject = class');
  28767. Add(' procedure DoIt; virtual; abstract;');
  28768. Add(' end;');
  28769. Add('procedure DoSome; begin end;');
  28770. Add('var');
  28771. Add(' o: TObject;');
  28772. Add(' p: Pointer;');
  28773. Add('begin');
  28774. Add(' p:=@DoSome;');
  28775. Add(' p:[email protected];');
  28776. ConvertProgram;
  28777. CheckSource('TestPointer_Proc',
  28778. LinesToStr([ // statements
  28779. 'rtl.createClass(this, "TObject", null, function () {',
  28780. ' this.$init = function () {',
  28781. ' };',
  28782. ' this.$final = function () {',
  28783. ' };',
  28784. '});',
  28785. 'this.DoSome = function () {',
  28786. '};',
  28787. 'this.o = null;',
  28788. 'this.p = null;',
  28789. '']),
  28790. LinesToStr([ // $mod.$main
  28791. '$mod.p = $mod.DoSome;',
  28792. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28793. '']));
  28794. end;
  28795. procedure TTestModule.TestPointer_AssignRecordFail;
  28796. begin
  28797. StartProgram(false);
  28798. Add('type');
  28799. Add(' TRec = record end;');
  28800. Add('var');
  28801. Add(' p: Pointer;');
  28802. Add(' r: TRec;');
  28803. Add('begin');
  28804. Add(' p:=r;');
  28805. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28806. nIncompatibleTypesGotExpected);
  28807. ConvertProgram;
  28808. end;
  28809. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28810. begin
  28811. StartProgram(false);
  28812. Add('type');
  28813. Add(' TArr = array[boolean] of longint;');
  28814. Add('var');
  28815. Add(' p: Pointer;');
  28816. Add(' a: TArr;');
  28817. Add('begin');
  28818. Add(' p:=a;');
  28819. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28820. nIncompatibleTypesGotExpected);
  28821. ConvertProgram;
  28822. end;
  28823. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28824. begin
  28825. StartProgram(false);
  28826. Add([
  28827. 'procedure DoIt(args: array of jsvalue); begin end;',
  28828. 'procedure DoAll; varargs; begin end;',
  28829. 'var',
  28830. ' v: jsvalue;',
  28831. 'begin',
  28832. ' DoIt([pointer(v)]);',
  28833. ' DoAll(pointer(v));',
  28834. '']);
  28835. ConvertProgram;
  28836. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28837. LinesToStr([ // statements
  28838. 'this.DoIt = function (args) {',
  28839. '};',
  28840. 'this.DoAll = function () {',
  28841. '};',
  28842. 'this.v = undefined;',
  28843. '']),
  28844. LinesToStr([ // $mod.$main
  28845. '$mod.DoIt([$mod.v]);',
  28846. '$mod.DoAll($mod.v);',
  28847. '']));
  28848. end;
  28849. procedure TTestModule.TestPointer_NonRecordFail;
  28850. begin
  28851. StartProgram(false);
  28852. Add([
  28853. 'type',
  28854. ' p = ^longint;',
  28855. 'begin',
  28856. '']);
  28857. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28858. ConvertProgram;
  28859. end;
  28860. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28861. begin
  28862. StartProgram(false);
  28863. Add([
  28864. 'procedure DoIt(p: ^longint); begin end;',
  28865. 'begin',
  28866. '']);
  28867. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28868. ConvertProgram;
  28869. end;
  28870. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28871. begin
  28872. StartProgram(false);
  28873. Add([
  28874. 'var p: ^longint;',
  28875. 'begin',
  28876. '']);
  28877. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28878. ConvertProgram;
  28879. end;
  28880. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28881. begin
  28882. StartProgram(false);
  28883. Add([
  28884. 'function DoIt: ^longint; begin end;',
  28885. 'begin',
  28886. '']);
  28887. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28888. ConvertProgram;
  28889. end;
  28890. procedure TTestModule.TestPointer_AddrOperatorFail;
  28891. begin
  28892. StartProgram(false);
  28893. Add([
  28894. 'var i: longint;',
  28895. 'begin',
  28896. ' if @i=nil then ;',
  28897. '']);
  28898. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28899. ConvertProgram;
  28900. end;
  28901. procedure TTestModule.TestPointer_ArrayParamsFail;
  28902. begin
  28903. StartProgram(false);
  28904. Add([
  28905. 'var',
  28906. ' p: Pointer;',
  28907. 'begin',
  28908. ' p:=p[1];',
  28909. '']);
  28910. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28911. ConvertProgram;
  28912. end;
  28913. procedure TTestModule.TestPointer_PointerAddFail;
  28914. begin
  28915. StartProgram(false);
  28916. Add([
  28917. 'var',
  28918. ' p: Pointer;',
  28919. 'begin',
  28920. ' p:=p+1;',
  28921. '']);
  28922. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28923. ConvertProgram;
  28924. end;
  28925. procedure TTestModule.TestPointer_IncPointerFail;
  28926. begin
  28927. StartProgram(false);
  28928. Add([
  28929. 'var',
  28930. ' p: Pointer;',
  28931. 'begin',
  28932. ' inc(p,1);',
  28933. '']);
  28934. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28935. nIncompatibleTypeArgNo);
  28936. ConvertProgram;
  28937. end;
  28938. procedure TTestModule.TestPointer_Record;
  28939. begin
  28940. StartProgram(false);
  28941. Add([
  28942. 'type',
  28943. ' TRec = record x: longint; end;',
  28944. ' PRec = ^TRec;',
  28945. 'var',
  28946. ' r: TRec;',
  28947. ' p: PRec;',
  28948. ' q: ^TRec;',
  28949. ' Ptr: pointer;',
  28950. 'begin',
  28951. ' new(p);',
  28952. ' p:=@r;',
  28953. ' r:=p^;',
  28954. ' r.x:=p^.x;',
  28955. ' p^.x:=r.x;',
  28956. ' if p^.x=3 then ;',
  28957. ' if 4=p^.x then ;',
  28958. ' dispose(p);',
  28959. ' new(q);',
  28960. ' dispose(q);',
  28961. ' Ptr:=p;',
  28962. ' p:=PRec(ptr);',
  28963. '']);
  28964. ConvertProgram;
  28965. CheckSource('TestPointer_Record',
  28966. LinesToStr([ // statements
  28967. 'rtl.recNewT(this, "TRec", function () {',
  28968. ' this.x = 0;',
  28969. ' this.$eq = function (b) {',
  28970. ' return this.x === b.x;',
  28971. ' };',
  28972. ' this.$assign = function (s) {',
  28973. ' this.x = s.x;',
  28974. ' return this;',
  28975. ' };',
  28976. '});',
  28977. 'this.r = this.TRec.$new();',
  28978. 'this.p = null;',
  28979. 'this.q = null;',
  28980. 'this.Ptr = null;',
  28981. '']),
  28982. LinesToStr([ // $mod.$main
  28983. '$mod.p = $mod.TRec.$new();',
  28984. '$mod.p = $mod.r;',
  28985. '$mod.r.$assign($mod.p);',
  28986. '$mod.r.x = $mod.p.x;',
  28987. '$mod.p.x = $mod.r.x;',
  28988. 'if ($mod.p.x === 3) ;',
  28989. 'if (4 === $mod.p.x) ;',
  28990. '$mod.p = null;',
  28991. '$mod.q = $mod.TRec.$new();',
  28992. '$mod.q = null;',
  28993. '$mod.Ptr = $mod.p;',
  28994. '$mod.p = $mod.Ptr;',
  28995. '']));
  28996. end;
  28997. procedure TTestModule.TestPointer_RecordArg;
  28998. begin
  28999. StartProgram(false);
  29000. Add([
  29001. '{$modeswitch autoderef}',
  29002. 'type',
  29003. ' TRec = record x: longint; end;',
  29004. ' PRec = ^TRec;',
  29005. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  29006. 'begin',
  29007. ' a.x:=a.x;',
  29008. ' a^.x:=a^.x;',
  29009. ' with a^ do',
  29010. ' x:=x;',
  29011. 'end;',
  29012. 'function GetIt(p: PRec): PRec;',
  29013. 'begin',
  29014. ' p.x:=p.x;',
  29015. ' p^.x:=p^.x;',
  29016. ' with p^ do',
  29017. ' x:=x;',
  29018. 'end;',
  29019. 'var',
  29020. ' r: TRec;',
  29021. ' p: PRec;',
  29022. 'begin',
  29023. ' p:=GetIt(p);',
  29024. ' p^:=GetIt(@r)^;',
  29025. ' DoIt(p,p,p);',
  29026. ' DoIt(@r,p,p);',
  29027. '']);
  29028. ConvertProgram;
  29029. CheckSource('TestPointer_RecordArg',
  29030. LinesToStr([ // statements
  29031. 'rtl.recNewT(this, "TRec", function () {',
  29032. ' this.x = 0;',
  29033. ' this.$eq = function (b) {',
  29034. ' return this.x === b.x;',
  29035. ' };',
  29036. ' this.$assign = function (s) {',
  29037. ' this.x = s.x;',
  29038. ' return this;',
  29039. ' };',
  29040. '});',
  29041. 'this.DoIt = function (a, b, c) {',
  29042. ' var Result = $mod.TRec.$new();',
  29043. ' a.x = a.x;',
  29044. ' a.x = a.x;',
  29045. ' a.x = a.x;',
  29046. ' return Result;',
  29047. '};',
  29048. 'this.GetIt = function (p) {',
  29049. ' var Result = null;',
  29050. ' p.x = p.x;',
  29051. ' p.x = p.x;',
  29052. ' p.x = p.x;',
  29053. ' return Result;',
  29054. '};',
  29055. 'this.r = this.TRec.$new();',
  29056. 'this.p = null;',
  29057. '']),
  29058. LinesToStr([ // $mod.$main
  29059. '$mod.p = $mod.GetIt($mod.p);',
  29060. '$mod.p.$assign($mod.GetIt($mod.r));',
  29061. '$mod.DoIt($mod.p, {',
  29062. ' p: $mod,',
  29063. ' get: function () {',
  29064. ' return this.p.p;',
  29065. ' },',
  29066. ' set: function (v) {',
  29067. ' this.p.p = v;',
  29068. ' }',
  29069. '}, {',
  29070. ' p: $mod,',
  29071. ' get: function () {',
  29072. ' return this.p.p;',
  29073. ' },',
  29074. ' set: function (v) {',
  29075. ' this.p.p = v;',
  29076. ' }',
  29077. '});',
  29078. '$mod.DoIt($mod.r, {',
  29079. ' p: $mod,',
  29080. ' get: function () {',
  29081. ' return this.p.p;',
  29082. ' },',
  29083. ' set: function (v) {',
  29084. ' this.p.p = v;',
  29085. ' }',
  29086. '}, {',
  29087. ' p: $mod,',
  29088. ' get: function () {',
  29089. ' return this.p.p;',
  29090. ' },',
  29091. ' set: function (v) {',
  29092. ' this.p.p = v;',
  29093. ' }',
  29094. '});',
  29095. '']));
  29096. end;
  29097. procedure TTestModule.TestJSValue_AssignToJSValue;
  29098. begin
  29099. StartProgram(false);
  29100. Add('var');
  29101. Add(' v: jsvalue;');
  29102. Add(' i: longint;');
  29103. Add(' s: string;');
  29104. Add(' b: boolean;');
  29105. Add(' d: double;');
  29106. Add(' p: pointer;');
  29107. Add('begin');
  29108. Add(' v:=v;');
  29109. Add(' v:=1;');
  29110. Add(' v:=i;');
  29111. Add(' v:='''';');
  29112. Add(' v:=''c'';');
  29113. Add(' v:=''foo'';');
  29114. Add(' v:=s;');
  29115. Add(' v:=false;');
  29116. Add(' v:=true;');
  29117. Add(' v:=b;');
  29118. Add(' v:=0.1;');
  29119. Add(' v:=d;');
  29120. Add(' v:=nil;');
  29121. Add(' v:=p;');
  29122. ConvertProgram;
  29123. CheckSource('TestJSValue_AssignToJSValue',
  29124. LinesToStr([ // statements
  29125. 'this.v = undefined;',
  29126. 'this.i = 0;',
  29127. 'this.s = "";',
  29128. 'this.b = false;',
  29129. 'this.d = 0.0;',
  29130. 'this.p = null;',
  29131. '']),
  29132. LinesToStr([ // $mod.$main
  29133. '$mod.v = $mod.v;',
  29134. '$mod.v = 1;',
  29135. '$mod.v = $mod.i;',
  29136. '$mod.v = "";',
  29137. '$mod.v = "c";',
  29138. '$mod.v = "foo";',
  29139. '$mod.v = $mod.s;',
  29140. '$mod.v = false;',
  29141. '$mod.v = true;',
  29142. '$mod.v = $mod.b;',
  29143. '$mod.v = 0.1;',
  29144. '$mod.v = $mod.d;',
  29145. '$mod.v = null;',
  29146. '$mod.v = $mod.p;',
  29147. '']));
  29148. end;
  29149. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29150. begin
  29151. StartProgram(false);
  29152. Add('type');
  29153. Add(' integer = longint;');
  29154. Add(' TYesNo = boolean;');
  29155. Add(' TFloat = double;');
  29156. Add(' TCaption = string;');
  29157. Add(' TChar = char;');
  29158. Add('var');
  29159. Add(' v: jsvalue;');
  29160. Add(' i: integer;');
  29161. Add(' s: TCaption;');
  29162. Add(' b: TYesNo;');
  29163. Add(' d: TFloat;');
  29164. Add(' c: char;');
  29165. Add('begin');
  29166. Add(' i:=longint(v);');
  29167. Add(' i:=integer(v);');
  29168. Add(' s:=string(v);');
  29169. Add(' s:=TCaption(v);');
  29170. Add(' b:=boolean(v);');
  29171. Add(' b:=TYesNo(v);');
  29172. Add(' d:=double(v);');
  29173. Add(' d:=TFloat(v);');
  29174. Add(' c:=char(v);');
  29175. Add(' c:=TChar(v);');
  29176. ConvertProgram;
  29177. CheckSource('TestJSValue_TypeCastToBaseType',
  29178. LinesToStr([ // statements
  29179. 'this.v = undefined;',
  29180. 'this.i = 0;',
  29181. 'this.s = "";',
  29182. 'this.b = false;',
  29183. 'this.d = 0.0;',
  29184. 'this.c = "\x00";',
  29185. '']),
  29186. LinesToStr([ // $mod.$main
  29187. '$mod.i = rtl.trunc($mod.v);',
  29188. '$mod.i = rtl.trunc($mod.v);',
  29189. '$mod.s = "" + $mod.v;',
  29190. '$mod.s = "" + $mod.v;',
  29191. '$mod.b = !($mod.v == false);',
  29192. '$mod.b = !($mod.v == false);',
  29193. '$mod.d = rtl.getNumber($mod.v);',
  29194. '$mod.d = rtl.getNumber($mod.v);',
  29195. '$mod.c = rtl.getChar($mod.v);',
  29196. '$mod.c = rtl.getChar($mod.v);',
  29197. '']));
  29198. end;
  29199. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29200. begin
  29201. StartProgram(false);
  29202. Add([
  29203. 'type',
  29204. ' TArr = array of word;',
  29205. ' TRec = record end;',
  29206. ' TSet = set of boolean;',
  29207. 'procedure Fly(v: jsvalue);',
  29208. 'begin',
  29209. 'end;',
  29210. 'var',
  29211. ' a: TArr;',
  29212. ' r: TRec;',
  29213. ' s: TSet;',
  29214. 'begin',
  29215. ' Fly(jsvalue(a));',
  29216. ' Fly(jsvalue(r));',
  29217. ' Fly(jsvalue(s));',
  29218. '']);
  29219. ConvertProgram;
  29220. CheckSource('TestJSValue_TypecastToJSValue',
  29221. LinesToStr([ // statements
  29222. 'rtl.recNewT(this, "TRec", function () {',
  29223. ' this.$eq = function (b) {',
  29224. ' return true;',
  29225. ' };',
  29226. ' this.$assign = function (s) {',
  29227. ' return this;',
  29228. ' };',
  29229. '});',
  29230. 'this.Fly = function (v) {',
  29231. '};',
  29232. 'this.a = [];',
  29233. 'this.r = this.TRec.$new();',
  29234. 'this.s = {};',
  29235. '']),
  29236. LinesToStr([ // $mod.$main
  29237. '$mod.Fly($mod.a);',
  29238. '$mod.Fly($mod.r);',
  29239. '$mod.Fly($mod.s);',
  29240. '']));
  29241. end;
  29242. procedure TTestModule.TestJSValue_Equal;
  29243. begin
  29244. StartProgram(false);
  29245. Add('type');
  29246. Add(' integer = longint;');
  29247. Add(' TYesNo = boolean;');
  29248. Add(' TFloat = double;');
  29249. Add(' TCaption = string;');
  29250. Add(' TChar = char;');
  29251. Add(' TMulti = JSValue;');
  29252. Add('var');
  29253. Add(' v: jsvalue;');
  29254. Add(' i: integer;');
  29255. Add(' s: TCaption;');
  29256. Add(' b: TYesNo;');
  29257. Add(' d: TFloat;');
  29258. Add(' c: char;');
  29259. Add(' m: TMulti;');
  29260. Add('begin');
  29261. Add(' b:=v=v;');
  29262. Add(' b:=v<>v;');
  29263. Add(' b:=v=1;');
  29264. Add(' b:=v<>1;');
  29265. Add(' b:=2=v;');
  29266. Add(' b:=2<>v;');
  29267. Add(' b:=v=i;');
  29268. Add(' b:=i=v;');
  29269. Add(' b:=v=nil;');
  29270. Add(' b:=nil=v;');
  29271. Add(' b:=v=false;');
  29272. Add(' b:=true=v;');
  29273. Add(' b:=v=b;');
  29274. Add(' b:=b=v;');
  29275. Add(' b:=v=s;');
  29276. Add(' b:=s=v;');
  29277. Add(' b:=v=''foo'';');
  29278. Add(' b:=''''=v;');
  29279. Add(' b:=v=d;');
  29280. Add(' b:=d=v;');
  29281. Add(' b:=v=3.4;');
  29282. Add(' b:=5.6=v;');
  29283. Add(' b:=v=c;');
  29284. Add(' b:=c=v;');
  29285. Add(' b:=m=m;');
  29286. Add(' b:=v=m;');
  29287. Add(' b:=m=v;');
  29288. ConvertProgram;
  29289. CheckSource('TestJSValue_Equal',
  29290. LinesToStr([ // statements
  29291. 'this.v = undefined;',
  29292. 'this.i = 0;',
  29293. 'this.s = "";',
  29294. 'this.b = false;',
  29295. 'this.d = 0.0;',
  29296. 'this.c = "\x00";',
  29297. 'this.m = undefined;',
  29298. '']),
  29299. LinesToStr([ // $mod.$main
  29300. '$mod.b = $mod.v == $mod.v;',
  29301. '$mod.b = $mod.v != $mod.v;',
  29302. '$mod.b = $mod.v == 1;',
  29303. '$mod.b = $mod.v != 1;',
  29304. '$mod.b = 2 == $mod.v;',
  29305. '$mod.b = 2 != $mod.v;',
  29306. '$mod.b = $mod.v == $mod.i;',
  29307. '$mod.b = $mod.i == $mod.v;',
  29308. '$mod.b = $mod.v == null;',
  29309. '$mod.b = null == $mod.v;',
  29310. '$mod.b = $mod.v == false;',
  29311. '$mod.b = true == $mod.v;',
  29312. '$mod.b = $mod.v == $mod.b;',
  29313. '$mod.b = $mod.b == $mod.v;',
  29314. '$mod.b = $mod.v == $mod.s;',
  29315. '$mod.b = $mod.s == $mod.v;',
  29316. '$mod.b = $mod.v == "foo";',
  29317. '$mod.b = "" == $mod.v;',
  29318. '$mod.b = $mod.v == $mod.d;',
  29319. '$mod.b = $mod.d == $mod.v;',
  29320. '$mod.b = $mod.v == 3.4;',
  29321. '$mod.b = 5.6 == $mod.v;',
  29322. '$mod.b = $mod.v == $mod.c;',
  29323. '$mod.b = $mod.c == $mod.v;',
  29324. '$mod.b = $mod.m == $mod.m;',
  29325. '$mod.b = $mod.v == $mod.m;',
  29326. '$mod.b = $mod.m == $mod.v;',
  29327. '']));
  29328. end;
  29329. procedure TTestModule.TestJSValue_If;
  29330. begin
  29331. StartProgram(false);
  29332. Add([
  29333. 'procedure Fly(var u);',
  29334. 'begin',
  29335. ' if jsvalue(u) then ;',
  29336. 'end;',
  29337. 'var',
  29338. ' v: jsvalue;',
  29339. 'begin',
  29340. ' if v then ;',
  29341. ' while v do ;',
  29342. ' repeat until v;',
  29343. '']);
  29344. ConvertProgram;
  29345. CheckSource('TestJSValue_If',
  29346. LinesToStr([ // statements
  29347. 'this.Fly = function (u) {',
  29348. ' if (u.get()) ;',
  29349. '};',
  29350. 'this.v = undefined;',
  29351. '']),
  29352. LinesToStr([ // $mod.$main
  29353. 'if ($mod.v) ;',
  29354. 'while($mod.v){',
  29355. '};',
  29356. 'do{',
  29357. '} while(!$mod.v);',
  29358. '']));
  29359. end;
  29360. procedure TTestModule.TestJSValue_Not;
  29361. begin
  29362. StartProgram(false);
  29363. Add([
  29364. 'var',
  29365. ' v: jsvalue;',
  29366. ' b: boolean;',
  29367. 'begin',
  29368. ' b:=not v;',
  29369. ' if not v then ;',
  29370. ' while not v do ;',
  29371. ' repeat until not v;',
  29372. '']);
  29373. ConvertProgram;
  29374. CheckSource('TestJSValue_If',
  29375. LinesToStr([ // statements
  29376. 'this.v = undefined;',
  29377. 'this.b = false;',
  29378. '']),
  29379. LinesToStr([ // $mod.$main
  29380. '$mod.b=!$mod.v;',
  29381. 'if (!$mod.v) ;',
  29382. 'while(!$mod.v){',
  29383. '};',
  29384. 'do{',
  29385. '} while($mod.v);',
  29386. '']));
  29387. end;
  29388. procedure TTestModule.TestJSValue_Enum;
  29389. begin
  29390. StartProgram(false);
  29391. Add('type');
  29392. Add(' TColor = (red, blue);');
  29393. Add(' TRedBlue = TColor;');
  29394. Add('var');
  29395. Add(' v: jsvalue;');
  29396. Add(' e: TColor;');
  29397. Add('begin');
  29398. Add(' v:=e;');
  29399. Add(' v:=TColor(e);');
  29400. Add(' v:=TRedBlue(e);');
  29401. Add(' e:=TColor(v);');
  29402. Add(' e:=TRedBlue(v);');
  29403. ConvertProgram;
  29404. CheckSource('TestJSValue_Enum',
  29405. LinesToStr([ // statements
  29406. 'this.TColor = {',
  29407. ' "0": "red",',
  29408. ' red: 0,',
  29409. ' "1": "blue",',
  29410. ' blue: 1',
  29411. '};',
  29412. 'this.v = undefined;',
  29413. 'this.e = 0;',
  29414. '']),
  29415. LinesToStr([ // $mod.$main
  29416. '$mod.v = $mod.e;',
  29417. '$mod.v = $mod.e;',
  29418. '$mod.v = $mod.e;',
  29419. '$mod.e = $mod.v;',
  29420. '$mod.e = $mod.v;',
  29421. '']));
  29422. end;
  29423. procedure TTestModule.TestJSValue_ClassInstance;
  29424. begin
  29425. StartProgram(false);
  29426. Add([
  29427. 'type',
  29428. ' TObject = class',
  29429. ' end;',
  29430. ' TBirdObject = TObject;',
  29431. 'var',
  29432. ' v: jsvalue;',
  29433. ' o: TObject;',
  29434. 'begin',
  29435. ' v:=o;',
  29436. ' v:=TObject(o);',
  29437. ' v:=TBirdObject(o);',
  29438. ' o:=TObject(v);',
  29439. ' o:=TBirdObject(v);',
  29440. ' if v is TObject then ;',
  29441. '']);
  29442. ConvertProgram;
  29443. CheckSource('TestJSValue_ClassInstance',
  29444. LinesToStr([ // statements
  29445. 'rtl.createClass(this, "TObject", null, function () {',
  29446. ' this.$init = function () {',
  29447. ' };',
  29448. ' this.$final = function () {',
  29449. ' };',
  29450. '});',
  29451. 'this.v = undefined;',
  29452. 'this.o = null;',
  29453. '']),
  29454. LinesToStr([ // $mod.$main
  29455. '$mod.v = $mod.o;',
  29456. '$mod.v = $mod.o;',
  29457. '$mod.v = $mod.o;',
  29458. '$mod.o = rtl.getObject($mod.v);',
  29459. '$mod.o = rtl.getObject($mod.v);',
  29460. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29461. '']));
  29462. end;
  29463. procedure TTestModule.TestJSValue_ClassOf;
  29464. begin
  29465. StartProgram(false);
  29466. Add([
  29467. 'type',
  29468. ' TClass = class of TObject;',
  29469. ' TObject = class',
  29470. ' end;',
  29471. ' TBirds = class of TBird;',
  29472. ' TBird = class(TObject) end;',
  29473. 'var',
  29474. ' v: jsvalue;',
  29475. ' c: TClass;',
  29476. 'begin',
  29477. ' v:=c;',
  29478. ' v:=TObject;',
  29479. ' v:=TClass(c);',
  29480. ' v:=TBirds(c);',
  29481. ' c:=TClass(v);',
  29482. ' c:=TBirds(v);',
  29483. ' if v is TClass then ;',
  29484. '']);
  29485. ConvertProgram;
  29486. CheckSource('TestJSValue_ClassOf',
  29487. LinesToStr([ // statements
  29488. 'rtl.createClass(this, "TObject", null, function () {',
  29489. ' this.$init = function () {',
  29490. ' };',
  29491. ' this.$final = function () {',
  29492. ' };',
  29493. '});',
  29494. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29495. '});',
  29496. 'this.v = undefined;',
  29497. 'this.c = null;',
  29498. '']),
  29499. LinesToStr([ // $mod.$main
  29500. '$mod.v = $mod.c;',
  29501. '$mod.v = $mod.TObject;',
  29502. '$mod.v = $mod.c;',
  29503. '$mod.v = $mod.c;',
  29504. '$mod.c = rtl.getObject($mod.v);',
  29505. '$mod.c = rtl.getObject($mod.v);',
  29506. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29507. '']));
  29508. end;
  29509. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29510. begin
  29511. StartProgram(false);
  29512. Add([
  29513. 'type',
  29514. ' integer = longint;',
  29515. ' TArray = array of JSValue;',
  29516. ' TArrgh = tarray;',
  29517. ' TArrInt = array of integer;',
  29518. 'var',
  29519. ' v: jsvalue;',
  29520. ' TheArray: tarray = (1,''2'');',
  29521. ' Arr: tarrgh;',
  29522. ' i: integer;',
  29523. ' ArrInt: tarrint;',
  29524. 'begin',
  29525. ' arr:=thearray;',
  29526. ' thearray:=arr;',
  29527. ' setlength(arr,2);',
  29528. ' setlength(thearray,3);',
  29529. ' arr[4]:=v;',
  29530. ' arr[5]:=length(thearray);',
  29531. ' arr[6]:=nil;',
  29532. ' arr[7]:=thearray[8];',
  29533. ' arr[low(arr)]:=high(thearray);',
  29534. ' arr:=arrint;',
  29535. ' arrInt:=tarrint(arr);',
  29536. ' if TheArray = nil then ;',
  29537. ' if nil = TheArray then ;',
  29538. ' if TheArray <> nil then ;',
  29539. ' if nil <> TheArray then ;',
  29540. '']);
  29541. ConvertProgram;
  29542. CheckSource('TestJSValue_ArrayOfJSValue',
  29543. LinesToStr([ // statements
  29544. 'this.v = undefined;',
  29545. 'this.TheArray = [1, "2"];',
  29546. 'this.Arr = [];',
  29547. 'this.i = 0;',
  29548. 'this.ArrInt = [];',
  29549. '']),
  29550. LinesToStr([ // $mod.$main
  29551. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29552. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29553. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29554. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29555. '$mod.Arr[4] = $mod.v;',
  29556. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29557. '$mod.Arr[6] = null;',
  29558. '$mod.Arr[7] = $mod.TheArray[8];',
  29559. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29560. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29561. '$mod.ArrInt = $mod.Arr;',
  29562. 'if (rtl.length($mod.TheArray) === 0) ;',
  29563. 'if (rtl.length($mod.TheArray) === 0) ;',
  29564. 'if (rtl.length($mod.TheArray) > 0) ;',
  29565. 'if (rtl.length($mod.TheArray) > 0) ;',
  29566. '']));
  29567. end;
  29568. procedure TTestModule.TestJSValue_ArrayLit;
  29569. begin
  29570. StartProgram(false);
  29571. Add([
  29572. 'type',
  29573. ' TFlag = (big,small);',
  29574. ' TArray = array of JSValue;',
  29575. ' TObject = class end;',
  29576. ' TClass = class of TObject;',
  29577. 'var',
  29578. ' v: jsvalue;',
  29579. ' a: TArray;',
  29580. ' o: TObject;',
  29581. 'begin',
  29582. ' a:=[];',
  29583. ' a:=[1];',
  29584. ' a:=[1,2];',
  29585. ' a:=[big];',
  29586. ' a:=[1,big];',
  29587. ' a:=[o,nil];',
  29588. '']);
  29589. ConvertProgram;
  29590. CheckSource('TestJSValue_ArrayLit',
  29591. LinesToStr([ // statements
  29592. 'this.TFlag = {',
  29593. ' "0": "big",',
  29594. ' big: 0,',
  29595. ' "1": "small",',
  29596. ' small: 1',
  29597. '};',
  29598. 'rtl.createClass(this, "TObject", null, function () {',
  29599. ' this.$init = function () {',
  29600. ' };',
  29601. ' this.$final = function () {',
  29602. ' };',
  29603. '});',
  29604. 'this.v = undefined;',
  29605. 'this.a = [];',
  29606. 'this.o = null;',
  29607. '']),
  29608. LinesToStr([ // $mod.$main
  29609. '$mod.a = [];',
  29610. '$mod.a = [1];',
  29611. '$mod.a = [1, 2];',
  29612. '$mod.a = [$mod.TFlag.big];',
  29613. '$mod.a = [1, $mod.TFlag.big];',
  29614. '$mod.a = [$mod.o, null];',
  29615. '']));
  29616. end;
  29617. procedure TTestModule.TestJSValue_Params;
  29618. begin
  29619. StartProgram(false);
  29620. Add('type');
  29621. Add(' integer = longint;');
  29622. Add(' TYesNo = boolean;');
  29623. Add(' TFloat = double;');
  29624. Add(' TCaption = string;');
  29625. Add(' TChar = char;');
  29626. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29627. Add('var');
  29628. Add(' l: jsvalue;');
  29629. Add('begin');
  29630. Add(' a:=a;');
  29631. Add(' l:=b;');
  29632. Add(' c:=c;');
  29633. Add(' d:=d;');
  29634. Add(' Result:=l;');
  29635. Add('end;');
  29636. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29637. Add('var');
  29638. Add(' v: jsvalue;');
  29639. Add(' i: integer;');
  29640. Add(' b: TYesNo;');
  29641. Add(' d: TFloat;');
  29642. Add(' s: TCaption;');
  29643. Add(' c: TChar;');
  29644. Add('begin');
  29645. Add(' v:=doit(v,v,v,v);');
  29646. Add(' i:=integer(dosome(i,i));');
  29647. Add(' b:=TYesNo(dosome(b,b));');
  29648. Add(' d:=TFloat(dosome(d,d));');
  29649. Add(' s:=TCaption(dosome(s,s));');
  29650. Add(' c:=TChar(dosome(c,c));');
  29651. ConvertProgram;
  29652. CheckSource('TestJSValue_Params',
  29653. LinesToStr([ // statements
  29654. 'this.DoIt = function (a, b, c, d) {',
  29655. ' var Result = undefined;',
  29656. ' var l = undefined;',
  29657. ' a = a;',
  29658. ' l = b;',
  29659. ' c.set(c.get());',
  29660. ' d.set(d.get());',
  29661. ' Result = l;',
  29662. ' return Result;',
  29663. '};',
  29664. 'this.DoSome = function (a, b) {',
  29665. ' var Result = undefined;',
  29666. ' return Result;',
  29667. '};',
  29668. 'this.v = undefined;',
  29669. 'this.i = 0;',
  29670. 'this.b = false;',
  29671. 'this.d = 0.0;',
  29672. 'this.s = "";',
  29673. 'this.c = "\x00";',
  29674. '']),
  29675. LinesToStr([ // $mod.$main
  29676. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29677. ' p: $mod,',
  29678. ' get: function () {',
  29679. ' return this.p.v;',
  29680. ' },',
  29681. ' set: function (v) {',
  29682. ' this.p.v = v;',
  29683. ' }',
  29684. '}, {',
  29685. ' p: $mod,',
  29686. ' get: function () {',
  29687. ' return this.p.v;',
  29688. ' },',
  29689. ' set: function (v) {',
  29690. ' this.p.v = v;',
  29691. ' }',
  29692. '});',
  29693. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29694. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29695. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29696. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29697. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29698. '']));
  29699. end;
  29700. procedure TTestModule.TestJSValue_UntypedParam;
  29701. begin
  29702. StartProgram(false);
  29703. Add('function DoIt(const a; var b; out c): jsvalue;');
  29704. Add('begin');
  29705. Add(' Result:=a;');
  29706. Add(' Result:=b;');
  29707. Add(' Result:=c;');
  29708. Add(' b:=Result;');
  29709. Add(' c:=Result;');
  29710. Add('end;');
  29711. Add('var i: longint;');
  29712. Add('begin');
  29713. Add(' doit(i,i,i);');
  29714. ConvertProgram;
  29715. CheckSource('TestJSValue_UntypedParam',
  29716. LinesToStr([ // statements
  29717. 'this.DoIt = function (a, b, c) {',
  29718. ' var Result = undefined;',
  29719. ' Result = a;',
  29720. ' Result = b.get();',
  29721. ' Result = c.get();',
  29722. ' b.set(Result);',
  29723. ' c.set(Result);',
  29724. ' return Result;',
  29725. '};',
  29726. 'this.i = 0;',
  29727. '']),
  29728. LinesToStr([ // $mod.$main
  29729. '$mod.DoIt($mod.i, {',
  29730. ' p: $mod,',
  29731. ' get: function () {',
  29732. ' return this.p.i;',
  29733. ' },',
  29734. ' set: function (v) {',
  29735. ' this.p.i = v;',
  29736. ' }',
  29737. '}, {',
  29738. ' p: $mod,',
  29739. ' get: function () {',
  29740. ' return this.p.i;',
  29741. ' },',
  29742. ' set: function (v) {',
  29743. ' this.p.i = v;',
  29744. ' }',
  29745. '});',
  29746. '']));
  29747. end;
  29748. procedure TTestModule.TestJSValue_FuncResultType;
  29749. begin
  29750. StartProgram(false);
  29751. Add('type');
  29752. Add(' integer = longint;');
  29753. Add(' TJSValueArray = array of JSValue;');
  29754. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29755. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29756. Add('begin');
  29757. Add(' while Compare(P,aList[0])>0 do ;');
  29758. Add('end;');
  29759. Add('var');
  29760. Add(' Compare: TListSortCompare;');
  29761. Add(' V: JSValue;');
  29762. Add(' i: integer;');
  29763. Add('begin');
  29764. Add(' if Compare(V,V)>0 then ;');
  29765. Add(' if Compare(i,i)>1 then ;');
  29766. Add(' if Compare(nil,false)>2 then ;');
  29767. Add(' if Compare(1,true)>3 then ;');
  29768. ConvertProgram;
  29769. CheckSource('TestJSValue_UntypedParam',
  29770. LinesToStr([ // statements
  29771. 'this.Sort = function (P, aList, Compare) {',
  29772. ' while (Compare(P, aList[0]) > 0) {',
  29773. ' };',
  29774. '};',
  29775. 'this.Compare = null;',
  29776. 'this.V = undefined;',
  29777. 'this.i = 0;',
  29778. '']),
  29779. LinesToStr([ // $mod.$main
  29780. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29781. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29782. 'if ($mod.Compare(null, false) > 2) ;',
  29783. 'if ($mod.Compare(1, true) > 3) ;',
  29784. '']));
  29785. end;
  29786. procedure TTestModule.TestJSValue_ProcType_Assign;
  29787. begin
  29788. StartProgram(false);
  29789. Add('type');
  29790. Add(' integer = longint;');
  29791. Add(' TObject = class');
  29792. Add(' class function GetGlob: integer;');
  29793. Add(' function Getter: integer;');
  29794. Add(' end;');
  29795. Add('class function TObject.GetGlob: integer;');
  29796. Add('var v1: jsvalue;');
  29797. Add('begin');
  29798. Add(' v1:=@GetGlob;');
  29799. Add(' v1:[email protected];');
  29800. Add('end;');
  29801. Add('function TObject.Getter: integer;');
  29802. Add('var v2: jsvalue;');
  29803. Add('begin');
  29804. Add(' v2:=@Getter;');
  29805. Add(' v2:[email protected];');
  29806. Add(' v2:=@GetGlob;');
  29807. Add(' v2:[email protected];');
  29808. Add('end;');
  29809. Add('function GetIt(i: integer): integer;');
  29810. Add('var v3: jsvalue;');
  29811. Add('begin');
  29812. Add(' v3:=@GetIt;');
  29813. Add('end;');
  29814. Add('var');
  29815. Add(' V: JSValue;');
  29816. Add(' o: TObject;');
  29817. Add('begin');
  29818. Add(' v:=@GetIt;');
  29819. Add(' v:[email protected];');
  29820. Add(' v:[email protected];');
  29821. ConvertProgram;
  29822. CheckSource('TestJSValue_ProcType_Assign',
  29823. LinesToStr([ // statements
  29824. 'rtl.createClass(this, "TObject", null, function () {',
  29825. ' this.$init = function () {',
  29826. ' };',
  29827. ' this.$final = function () {',
  29828. ' };',
  29829. ' this.GetGlob = function () {',
  29830. ' var Result = 0;',
  29831. ' var v1 = undefined;',
  29832. ' v1 = rtl.createCallback(this, "GetGlob");',
  29833. ' v1 = rtl.createCallback(this, "GetGlob");',
  29834. ' return Result;',
  29835. ' };',
  29836. ' this.Getter = function () {',
  29837. ' var Result = 0;',
  29838. ' var v2 = undefined;',
  29839. ' v2 = rtl.createCallback(this, "Getter");',
  29840. ' v2 = rtl.createCallback(this, "Getter");',
  29841. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29842. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29843. ' return Result;',
  29844. ' };',
  29845. '});',
  29846. 'this.GetIt = function (i) {',
  29847. ' var Result = 0;',
  29848. ' var v3 = undefined;',
  29849. ' v3 = $mod.GetIt;',
  29850. ' return Result;',
  29851. '};',
  29852. 'this.V = undefined;',
  29853. 'this.o = null;',
  29854. '']),
  29855. LinesToStr([ // $mod.$main
  29856. '$mod.V = $mod.GetIt;',
  29857. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29858. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29859. '']));
  29860. end;
  29861. procedure TTestModule.TestJSValue_ProcType_Equal;
  29862. begin
  29863. StartProgram(false);
  29864. Add('type');
  29865. Add(' integer = longint;');
  29866. Add(' TObject = class');
  29867. Add(' class function GetGlob: integer;');
  29868. Add(' function Getter: integer;');
  29869. Add(' end;');
  29870. Add('class function TObject.GetGlob: integer;');
  29871. Add('var v1: jsvalue;');
  29872. Add('begin');
  29873. Add(' if v1=@GetGlob then;');
  29874. Add(' if [email protected] then ;');
  29875. Add('end;');
  29876. Add('function TObject.Getter: integer;');
  29877. Add('var v2: jsvalue;');
  29878. Add('begin');
  29879. Add(' if v2=@Getter then;');
  29880. Add(' if [email protected] then ;');
  29881. Add(' if v2=@GetGlob then;');
  29882. Add(' if [email protected] then;');
  29883. Add('end;');
  29884. Add('function GetIt(i: integer): integer;');
  29885. Add('var v3: jsvalue;');
  29886. Add('begin');
  29887. Add(' if v3=@GetIt then;');
  29888. Add('end;');
  29889. Add('var');
  29890. Add(' V: JSValue;');
  29891. Add(' o: TObject;');
  29892. Add('begin');
  29893. Add(' if v=@GetIt then;');
  29894. Add(' if [email protected] then;');
  29895. Add(' if [email protected] then;');
  29896. Add(' if @GetIt=v then;');
  29897. Add(' if @o.Getter=v then;');
  29898. Add(' if @o.GetGlob=v then;');
  29899. ConvertProgram;
  29900. CheckSource('TestJSValue_ProcType_Equal',
  29901. LinesToStr([ // statements
  29902. 'rtl.createClass(this, "TObject", null, function () {',
  29903. ' this.$init = function () {',
  29904. ' };',
  29905. ' this.$final = function () {',
  29906. ' };',
  29907. ' this.GetGlob = function () {',
  29908. ' var Result = 0;',
  29909. ' var v1 = undefined;',
  29910. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29911. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29912. ' return Result;',
  29913. ' };',
  29914. ' this.Getter = function () {',
  29915. ' var Result = 0;',
  29916. ' var v2 = undefined;',
  29917. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29918. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29919. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29920. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29921. ' return Result;',
  29922. ' };',
  29923. '});',
  29924. 'this.GetIt = function (i) {',
  29925. ' var Result = 0;',
  29926. ' var v3 = undefined;',
  29927. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29928. ' return Result;',
  29929. '};',
  29930. 'this.V = undefined;',
  29931. 'this.o = null;',
  29932. '']),
  29933. LinesToStr([ // $mod.$main
  29934. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29935. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29936. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29937. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29938. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29939. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29940. '']));
  29941. end;
  29942. procedure TTestModule.TestJSValue_ProcType_Param;
  29943. begin
  29944. StartProgram(false);
  29945. Add([
  29946. 'type',
  29947. ' variant = jsvalue;',
  29948. ' TArrVariant = array of variant;',
  29949. ' TArrVar2 = TArrVariant;',
  29950. ' TFuncInt = function: longint;',
  29951. 'function GetIt: longint;',
  29952. 'begin',
  29953. 'end;',
  29954. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29955. 'var v: variant;',
  29956. 'begin',
  29957. ' v:=arr[1];',
  29958. 'end;',
  29959. 'var s: string;',
  29960. 'begin',
  29961. ' DoIt(GetIt,[]);',
  29962. ' DoIt(@GetIt,[]);',
  29963. ' DoIt(1,[s,GetIt]);',
  29964. ' DoIt(1,[s,@GetIt]);',
  29965. '']);
  29966. ConvertProgram;
  29967. CheckSource('TestJSValue_ProcType_Param',
  29968. LinesToStr([ // statements
  29969. 'this.GetIt = function () {',
  29970. ' var Result = 0;',
  29971. ' return Result;',
  29972. '};',
  29973. 'this.DoIt = function (p, Arr) {',
  29974. ' var v = undefined;',
  29975. ' v = Arr[1];',
  29976. '};',
  29977. 'this.s = "";',
  29978. '']),
  29979. LinesToStr([ // $mod.$main
  29980. '$mod.DoIt($mod.GetIt(), []);',
  29981. '$mod.DoIt($mod.GetIt, []);',
  29982. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29983. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29984. '']));
  29985. end;
  29986. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29987. begin
  29988. StartProgram(false);
  29989. Add([
  29990. 'var',
  29991. ' v: JSValue;',
  29992. ' p: Pointer;',
  29993. 'begin',
  29994. ' p:=v;',
  29995. '']);
  29996. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29997. nIncompatibleTypesGotExpected);
  29998. ConvertProgram;
  29999. end;
  30000. procedure TTestModule.TestJSValue_OverloadDouble;
  30001. begin
  30002. StartProgram(false);
  30003. Add([
  30004. 'type',
  30005. ' integer = longint;',
  30006. ' tdatetime = double;',
  30007. 'procedure DoIt(d: double); begin end;',
  30008. 'procedure DoIt(v: jsvalue); begin end;',
  30009. 'var',
  30010. ' d: double;',
  30011. ' dt: tdatetime;',
  30012. ' i: integer;',
  30013. ' b: byte;',
  30014. ' shi: shortint;',
  30015. ' w: word;',
  30016. ' smi: smallint;',
  30017. ' lw: longword;',
  30018. ' li: longint;',
  30019. ' ni: nativeint;',
  30020. ' nu: nativeuint;',
  30021. 'begin',
  30022. ' DoIt(d);',
  30023. ' DoIt(dt);',
  30024. ' DoIt(i);',
  30025. ' DoIt(b);',
  30026. ' DoIt(shi);',
  30027. ' DoIt(w);',
  30028. ' DoIt(smi);',
  30029. ' DoIt(lw);',
  30030. ' DoIt(li);',
  30031. ' DoIt(ni);',
  30032. ' DoIt(nu);',
  30033. '']);
  30034. ConvertProgram;
  30035. CheckSource('TestJSValue_OverloadDouble',
  30036. LinesToStr([ // statements
  30037. 'this.DoIt = function (d) {',
  30038. '};',
  30039. 'this.DoIt$1 = function (v) {',
  30040. '};',
  30041. 'this.d = 0.0;',
  30042. 'this.dt = 0.0;',
  30043. 'this.i = 0;',
  30044. 'this.b = 0;',
  30045. 'this.shi = 0;',
  30046. 'this.w = 0;',
  30047. 'this.smi = 0;',
  30048. 'this.lw = 0;',
  30049. 'this.li = 0;',
  30050. 'this.ni = 0;',
  30051. 'this.nu = 0;',
  30052. '']),
  30053. LinesToStr([ // $mod.$main
  30054. '$mod.DoIt($mod.d);',
  30055. '$mod.DoIt($mod.dt);',
  30056. '$mod.DoIt$1($mod.i);',
  30057. '$mod.DoIt$1($mod.b);',
  30058. '$mod.DoIt$1($mod.shi);',
  30059. '$mod.DoIt$1($mod.w);',
  30060. '$mod.DoIt$1($mod.smi);',
  30061. '$mod.DoIt$1($mod.lw);',
  30062. '$mod.DoIt$1($mod.li);',
  30063. '$mod.DoIt$1($mod.ni);',
  30064. '$mod.DoIt$1($mod.nu);',
  30065. '']));
  30066. end;
  30067. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30068. begin
  30069. StartProgram(false);
  30070. Add([
  30071. 'type',
  30072. ' integer = longint;',
  30073. ' int53 = nativeint;',
  30074. ' tdatetime = double;',
  30075. 'procedure DoIt(n: nativeint); begin end;',
  30076. 'procedure DoIt(v: jsvalue); begin end;',
  30077. 'var',
  30078. ' d: double;',
  30079. ' dt: tdatetime;',
  30080. ' i: integer;',
  30081. ' b: byte;',
  30082. ' shi: shortint;',
  30083. ' w: word;',
  30084. ' smi: smallint;',
  30085. ' lw: longword;',
  30086. ' li: longint;',
  30087. ' ni: nativeint;',
  30088. ' nu: nativeuint;',
  30089. 'begin',
  30090. ' DoIt(d);',
  30091. ' DoIt(dt);',
  30092. ' DoIt(i);',
  30093. ' DoIt(b);',
  30094. ' DoIt(shi);',
  30095. ' DoIt(w);',
  30096. ' DoIt(smi);',
  30097. ' DoIt(lw);',
  30098. ' DoIt(li);',
  30099. ' DoIt(ni);',
  30100. ' DoIt(nu);',
  30101. '']);
  30102. ConvertProgram;
  30103. CheckSource('TestJSValue_OverloadNativeInt',
  30104. LinesToStr([ // statements
  30105. 'this.DoIt = function (n) {',
  30106. '};',
  30107. 'this.DoIt$1 = function (v) {',
  30108. '};',
  30109. 'this.d = 0.0;',
  30110. 'this.dt = 0.0;',
  30111. 'this.i = 0;',
  30112. 'this.b = 0;',
  30113. 'this.shi = 0;',
  30114. 'this.w = 0;',
  30115. 'this.smi = 0;',
  30116. 'this.lw = 0;',
  30117. 'this.li = 0;',
  30118. 'this.ni = 0;',
  30119. 'this.nu = 0;',
  30120. '']),
  30121. LinesToStr([ // $mod.$main
  30122. '$mod.DoIt$1($mod.d);',
  30123. '$mod.DoIt$1($mod.dt);',
  30124. '$mod.DoIt($mod.i);',
  30125. '$mod.DoIt($mod.b);',
  30126. '$mod.DoIt($mod.shi);',
  30127. '$mod.DoIt($mod.w);',
  30128. '$mod.DoIt($mod.smi);',
  30129. '$mod.DoIt($mod.lw);',
  30130. '$mod.DoIt($mod.li);',
  30131. '$mod.DoIt($mod.ni);',
  30132. '$mod.DoIt($mod.nu);',
  30133. '']));
  30134. end;
  30135. procedure TTestModule.TestJSValue_OverloadWord;
  30136. begin
  30137. StartProgram(false);
  30138. Add([
  30139. 'type',
  30140. ' integer = longint;',
  30141. ' int53 = nativeint;',
  30142. ' tdatetime = double;',
  30143. 'procedure DoIt(w: word); begin end;',
  30144. 'procedure DoIt(v: jsvalue); begin end;',
  30145. 'var',
  30146. ' d: double;',
  30147. ' dt: tdatetime;',
  30148. ' i: integer;',
  30149. ' b: byte;',
  30150. ' shi: shortint;',
  30151. ' w: word;',
  30152. ' smi: smallint;',
  30153. ' lw: longword;',
  30154. ' li: longint;',
  30155. ' ni: nativeint;',
  30156. ' nu: nativeuint;',
  30157. 'begin',
  30158. ' DoIt(d);',
  30159. ' DoIt(dt);',
  30160. ' DoIt(i);',
  30161. ' DoIt(b);',
  30162. ' DoIt(shi);',
  30163. ' DoIt(w);',
  30164. ' DoIt(smi);',
  30165. ' DoIt(lw);',
  30166. ' DoIt(li);',
  30167. ' DoIt(ni);',
  30168. ' DoIt(nu);',
  30169. '']);
  30170. ConvertProgram;
  30171. CheckSource('TestJSValue_OverloadWord',
  30172. LinesToStr([ // statements
  30173. 'this.DoIt = function (w) {',
  30174. '};',
  30175. 'this.DoIt$1 = function (v) {',
  30176. '};',
  30177. 'this.d = 0.0;',
  30178. 'this.dt = 0.0;',
  30179. 'this.i = 0;',
  30180. 'this.b = 0;',
  30181. 'this.shi = 0;',
  30182. 'this.w = 0;',
  30183. 'this.smi = 0;',
  30184. 'this.lw = 0;',
  30185. 'this.li = 0;',
  30186. 'this.ni = 0;',
  30187. 'this.nu = 0;',
  30188. '']),
  30189. LinesToStr([ // $mod.$main
  30190. '$mod.DoIt$1($mod.d);',
  30191. '$mod.DoIt$1($mod.dt);',
  30192. '$mod.DoIt$1($mod.i);',
  30193. '$mod.DoIt($mod.b);',
  30194. '$mod.DoIt($mod.shi);',
  30195. '$mod.DoIt($mod.w);',
  30196. '$mod.DoIt$1($mod.smi);',
  30197. '$mod.DoIt$1($mod.lw);',
  30198. '$mod.DoIt$1($mod.li);',
  30199. '$mod.DoIt$1($mod.ni);',
  30200. '$mod.DoIt$1($mod.nu);',
  30201. '']));
  30202. end;
  30203. procedure TTestModule.TestJSValue_OverloadString;
  30204. begin
  30205. StartProgram(false);
  30206. Add([
  30207. 'type',
  30208. ' uni = string;',
  30209. ' WChar = char;',
  30210. 'procedure DoIt(s: string); begin end;',
  30211. 'procedure DoIt(v: jsvalue); begin end;',
  30212. 'var',
  30213. ' s: string;',
  30214. ' c: char;',
  30215. ' u: uni;',
  30216. 'begin',
  30217. ' DoIt(s);',
  30218. ' DoIt(c);',
  30219. ' DoIt(u);',
  30220. '']);
  30221. ConvertProgram;
  30222. CheckSource('TestJSValue_OverloadString',
  30223. LinesToStr([ // statements
  30224. 'this.DoIt = function (s) {',
  30225. '};',
  30226. 'this.DoIt$1 = function (v) {',
  30227. '};',
  30228. 'this.s = "";',
  30229. 'this.c = "\x00";',
  30230. 'this.u = "";',
  30231. '']),
  30232. LinesToStr([ // $mod.$main
  30233. '$mod.DoIt($mod.s);',
  30234. '$mod.DoIt($mod.c);',
  30235. '$mod.DoIt($mod.u);',
  30236. '']));
  30237. end;
  30238. procedure TTestModule.TestJSValue_OverloadChar;
  30239. begin
  30240. StartProgram(false);
  30241. Add([
  30242. 'type',
  30243. ' uni = string;',
  30244. ' WChar = char;',
  30245. 'procedure DoIt(c: char); begin end;',
  30246. 'procedure DoIt(v: jsvalue); begin end;',
  30247. 'var',
  30248. ' s: string;',
  30249. ' c: char;',
  30250. ' u: uni;',
  30251. 'begin',
  30252. ' DoIt(s);',
  30253. ' DoIt(c);',
  30254. ' DoIt(u);',
  30255. '']);
  30256. ConvertProgram;
  30257. CheckSource('TestJSValue_OverloadChar',
  30258. LinesToStr([ // statements
  30259. 'this.DoIt = function (c) {',
  30260. '};',
  30261. 'this.DoIt$1 = function (v) {',
  30262. '};',
  30263. 'this.s = "";',
  30264. 'this.c = "\x00";',
  30265. 'this.u = "";',
  30266. '']),
  30267. LinesToStr([ // $mod.$main
  30268. '$mod.DoIt$1($mod.s);',
  30269. '$mod.DoIt($mod.c);',
  30270. '$mod.DoIt$1($mod.u);',
  30271. '']));
  30272. end;
  30273. procedure TTestModule.TestJSValue_OverloadPointer;
  30274. begin
  30275. StartProgram(false);
  30276. Add([
  30277. 'type',
  30278. ' TObject = class end;',
  30279. 'procedure DoIt(p: pointer); begin end;',
  30280. 'procedure DoIt(v: jsvalue); begin end;',
  30281. 'var',
  30282. ' o: TObject;',
  30283. 'begin',
  30284. ' DoIt(o);',
  30285. '']);
  30286. ConvertProgram;
  30287. CheckSource('TestJSValue_OverloadPointer',
  30288. LinesToStr([ // statements
  30289. 'rtl.createClass(this, "TObject", null, function () {',
  30290. ' this.$init = function () {',
  30291. ' };',
  30292. ' this.$final = function () {',
  30293. ' };',
  30294. '});',
  30295. 'this.DoIt = function (p) {',
  30296. '};',
  30297. 'this.DoIt$1 = function (v) {',
  30298. '};',
  30299. 'this.o = null;',
  30300. '']),
  30301. LinesToStr([ // $mod.$main
  30302. '$mod.DoIt($mod.o);',
  30303. '']));
  30304. end;
  30305. procedure TTestModule.TestJSValue_ForIn;
  30306. begin
  30307. StartProgram(false);
  30308. Add([
  30309. 'var',
  30310. ' v: JSValue;',
  30311. ' key: string;',
  30312. 'begin',
  30313. ' for key in v do begin',
  30314. ' if key=''abc'' then ;',
  30315. ' end;',
  30316. '']);
  30317. ConvertProgram;
  30318. CheckSource('TestJSValue_ForIn',
  30319. LinesToStr([ // statements
  30320. 'this.v = undefined;',
  30321. 'this.key = "";',
  30322. '']),
  30323. LinesToStr([ // $mod.$main
  30324. 'for ($mod.key in $mod.v) {',
  30325. ' if ($mod.key === "abc") ;',
  30326. '};',
  30327. '']));
  30328. end;
  30329. procedure TTestModule.TestRTTI_IntRange;
  30330. begin
  30331. WithTypeInfo:=true;
  30332. StartProgram(true,[supTypeInfo]);
  30333. Add([
  30334. '{$modeswitch externalclass}',
  30335. 'type',
  30336. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30337. ' TColor = type TGraphicsColor;',
  30338. 'var',
  30339. ' p: TTypeInfo;',
  30340. ' k: TTypeKind;',
  30341. 'begin',
  30342. ' p:=typeinfo(TGraphicsColor);',
  30343. ' p:=typeinfo(TColor);',
  30344. ' k:=GetTypeKind(TGraphicsColor);',
  30345. ' k:=GetTypeKind(TColor);',
  30346. '']);
  30347. ConvertProgram;
  30348. CheckSource('TestRTTI_IntRange',
  30349. LinesToStr([ // statements
  30350. 'this.$rtti.$Int("TGraphicsColor", {',
  30351. ' minvalue: -2147483648,',
  30352. ' maxvalue: 2147483647,',
  30353. ' ordtype: 4',
  30354. '});',
  30355. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30356. 'this.p = null;',
  30357. 'this.k = 0;',
  30358. '']),
  30359. LinesToStr([ // $mod.$main
  30360. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30361. '$mod.p = $mod.$rtti["TColor"];',
  30362. '$mod.k = 1;',
  30363. '$mod.k = 1;',
  30364. '']));
  30365. end;
  30366. procedure TTestModule.TestRTTI_Double;
  30367. begin
  30368. WithTypeInfo:=true;
  30369. StartProgram(true,[supTypeInfo]);
  30370. Add([
  30371. '{$modeswitch externalclass}',
  30372. 'type',
  30373. ' TFloat = type double;',
  30374. 'var',
  30375. ' p: TTypeInfo;',
  30376. 'begin',
  30377. ' p:=typeinfo(double);',
  30378. ' p:=typeinfo(TFloat);',
  30379. '']);
  30380. ConvertProgram;
  30381. CheckSource('TestRTTI_Double',
  30382. LinesToStr([ // statements
  30383. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30384. 'this.p = null;',
  30385. '']),
  30386. LinesToStr([ // $mod.$main
  30387. '$mod.p = rtl.double;',
  30388. '$mod.p = $mod.$rtti["TFloat"];',
  30389. '']));
  30390. end;
  30391. procedure TTestModule.TestRTTI_ProcType;
  30392. begin
  30393. WithTypeInfo:=true;
  30394. StartProgram(false);
  30395. Add('type');
  30396. Add(' TProcA = procedure;');
  30397. Add(' TMethodB = procedure of object;');
  30398. Add(' TProcC = procedure; varargs;');
  30399. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30400. Add(' TProcE = function: nativeint;');
  30401. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30402. Add('var p: pointer;');
  30403. Add('begin');
  30404. Add(' p:=typeinfo(tproca);');
  30405. ConvertProgram;
  30406. CheckSource('TestRTTI_ProcType',
  30407. LinesToStr([ // statements
  30408. 'this.$rtti.$ProcVar("TProcA", {',
  30409. ' procsig: rtl.newTIProcSig([])',
  30410. '});',
  30411. 'this.$rtti.$MethodVar("TMethodB", {',
  30412. ' procsig: rtl.newTIProcSig([]),',
  30413. ' methodkind: 0',
  30414. '});',
  30415. 'this.$rtti.$ProcVar("TProcC", {',
  30416. ' procsig: rtl.newTIProcSig([], null, 2)',
  30417. '});',
  30418. 'this.$rtti.$ProcVar("TProcD", {',
  30419. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30420. '});',
  30421. 'this.$rtti.$ProcVar("TProcE", {',
  30422. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30423. '});',
  30424. 'this.$rtti.$ProcVar("TProcF", {',
  30425. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30426. '});',
  30427. 'this.p = null;',
  30428. '']),
  30429. LinesToStr([ // $mod.$main
  30430. '$mod.p = $mod.$rtti["TProcA"];',
  30431. '']));
  30432. end;
  30433. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30434. begin
  30435. WithTypeInfo:=true;
  30436. AddModuleWithIntfImplSrc('unit2.pas',
  30437. LinesToStr([
  30438. 'type',
  30439. ' TObject = class end;'
  30440. ]),
  30441. '');
  30442. StartUnit(true);
  30443. Add('interface');
  30444. Add('uses unit2;');
  30445. Add('type');
  30446. Add(' TProcA = function(o: tobject): tobject;');
  30447. Add('implementation');
  30448. Add('type');
  30449. Add(' TProcB = function(o: tobject): tobject;');
  30450. Add('var p: Pointer;');
  30451. Add('initialization');
  30452. Add(' p:=typeinfo(tproca);');
  30453. Add(' p:=typeinfo(tprocb);');
  30454. ConvertUnit;
  30455. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30456. LinesToStr([ // statements
  30457. 'var $impl = $mod.$impl;',
  30458. 'this.$rtti.$ProcVar("TProcA", {',
  30459. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30460. '});',
  30461. '']),
  30462. LinesToStr([ // this.$init
  30463. '$impl.p = $mod.$rtti["TProcA"];',
  30464. '$impl.p = $mod.$rtti["TProcB"];',
  30465. '']),
  30466. LinesToStr([ // implementation
  30467. '$mod.$rtti.$ProcVar("TProcB", {',
  30468. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30469. '});',
  30470. '$impl.p = null;',
  30471. '']) );
  30472. end;
  30473. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30474. begin
  30475. WithTypeInfo:=true;
  30476. StartProgram(false);
  30477. Add(['var',
  30478. ' ProcA: procedure;',
  30479. ' MethodB: procedure of object;',
  30480. ' ProcC: procedure; varargs;',
  30481. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30482. ' ProcE: function: nativeint;',
  30483. ' p: pointer;',
  30484. 'begin',
  30485. ' p:=typeinfo(proca);']);
  30486. ConvertProgram;
  30487. CheckSource('TestRTTI_ProcTypeAnonymous',
  30488. LinesToStr([ // statements
  30489. 'this.$rtti.$ProcVar("ProcA$a", {',
  30490. ' procsig: rtl.newTIProcSig([])',
  30491. '});',
  30492. 'this.ProcA = null;',
  30493. 'this.$rtti.$MethodVar("MethodB$a", {',
  30494. ' procsig: rtl.newTIProcSig([]),',
  30495. ' methodkind: 0',
  30496. '});',
  30497. 'this.MethodB = null;',
  30498. 'this.$rtti.$ProcVar("ProcC$a", {',
  30499. ' procsig: rtl.newTIProcSig([], null, 2)',
  30500. '});',
  30501. 'this.ProcC = null;',
  30502. 'this.$rtti.$ProcVar("ProcD$a", {',
  30503. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30504. '});',
  30505. 'this.ProcD = null;',
  30506. 'this.$rtti.$ProcVar("ProcE$a", {',
  30507. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30508. '});',
  30509. 'this.ProcE = null;',
  30510. 'this.p = null;',
  30511. '']),
  30512. LinesToStr([ // $mod.$main
  30513. '$mod.p = $mod.$rtti["ProcA$a"];',
  30514. '']));
  30515. end;
  30516. procedure TTestModule.TestRTTI_EnumAndSetType;
  30517. begin
  30518. WithTypeInfo:=true;
  30519. StartProgram(false);
  30520. Add('type');
  30521. Add(' TFlag = (light,dark);');
  30522. Add(' TFlags = set of TFlag;');
  30523. Add(' TProc = function(f: TFlags): TFlag;');
  30524. Add('var p: pointer;');
  30525. Add('begin');
  30526. Add(' p:=typeinfo(tflag);');
  30527. Add(' p:=typeinfo(tflags);');
  30528. ConvertProgram;
  30529. CheckSource('TestRTTI_EnumAndType',
  30530. LinesToStr([ // statements
  30531. 'this.TFlag = {',
  30532. ' "0": "light",',
  30533. ' light: 0,',
  30534. ' "1": "dark",',
  30535. ' dark: 1',
  30536. '};',
  30537. 'this.$rtti.$Enum("TFlag", {',
  30538. ' minvalue: 0,',
  30539. ' maxvalue: 1,',
  30540. ' ordtype: 1,',
  30541. ' enumtype: this.TFlag',
  30542. '});',
  30543. 'this.$rtti.$Set("TFlags", {',
  30544. ' comptype: this.$rtti["TFlag"]',
  30545. '});',
  30546. 'this.$rtti.$ProcVar("TProc", {',
  30547. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30548. '});',
  30549. 'this.p = null;',
  30550. '']),
  30551. LinesToStr([ // $mod.$main
  30552. '$mod.p = $mod.$rtti["TFlag"];',
  30553. '$mod.p = $mod.$rtti["TFlags"];',
  30554. '']));
  30555. end;
  30556. procedure TTestModule.TestRTTI_EnumRange;
  30557. begin
  30558. WithTypeInfo:=true;
  30559. StartProgram(false);
  30560. Add([
  30561. 'type',
  30562. ' TCol = (red,green,blue);',
  30563. ' TColRg = green..blue;',
  30564. ' TSetOfColRg = set of TColRg;',
  30565. 'var p: pointer;',
  30566. 'begin',
  30567. ' p:=typeinfo(tcolrg);',
  30568. ' p:=typeinfo(tsetofcolrg);',
  30569. '']);
  30570. ConvertProgram;
  30571. end;
  30572. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30573. begin
  30574. WithTypeInfo:=true;
  30575. StartProgram(false);
  30576. Add('type');
  30577. Add(' TFlags = set of (red, green);');
  30578. Add('var');
  30579. Add(' f: TFlags;');
  30580. Add('begin');
  30581. Add(' Include(f,red);');
  30582. ConvertProgram;
  30583. CheckSource('TestRTTI_AnonymousEnumType',
  30584. LinesToStr([ // statements
  30585. 'this.TFlags$a = {',
  30586. ' "0": "red",',
  30587. ' red: 0,',
  30588. ' "1": "green",',
  30589. ' green: 1',
  30590. '};',
  30591. 'this.$rtti.$Enum("TFlags$a", {',
  30592. ' minvalue: 0,',
  30593. ' maxvalue: 1,',
  30594. ' ordtype: 1,',
  30595. ' enumtype: this.TFlags$a',
  30596. '});',
  30597. 'this.$rtti.$Set("TFlags", {',
  30598. ' comptype: this.$rtti["TFlags$a"]',
  30599. '});',
  30600. 'this.f = {};',
  30601. '']),
  30602. LinesToStr([
  30603. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30604. '']));
  30605. end;
  30606. procedure TTestModule.TestRTTI_StaticArray;
  30607. begin
  30608. WithTypeInfo:=true;
  30609. StartProgram(false);
  30610. Add('type');
  30611. Add(' TFlag = (light,dark);');
  30612. Add(' TFlagNames = array[TFlag] of string;');
  30613. Add(' TBoolNames = array[boolean] of string;');
  30614. Add(' TByteArray = array[1..32768] of byte;');
  30615. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30616. Add('var p: pointer;');
  30617. Add('begin');
  30618. Add(' p:=typeinfo(TFlagNames);');
  30619. Add(' p:=typeinfo(TBoolNames);');
  30620. ConvertProgram;
  30621. CheckSource('TestRTTI_StaticArray',
  30622. LinesToStr([ // statements
  30623. 'this.TFlag = {',
  30624. ' "0": "light",',
  30625. ' light: 0,',
  30626. ' "1": "dark",',
  30627. ' dark: 1',
  30628. '};',
  30629. 'this.$rtti.$Enum("TFlag", {',
  30630. ' minvalue: 0,',
  30631. ' maxvalue: 1,',
  30632. ' ordtype: 1,',
  30633. ' enumtype: this.TFlag',
  30634. '});',
  30635. 'this.$rtti.$StaticArray("TFlagNames", {',
  30636. ' dims: [2],',
  30637. ' eltype: rtl.string',
  30638. '});',
  30639. 'this.$rtti.$StaticArray("TBoolNames", {',
  30640. ' dims: [2],',
  30641. ' eltype: rtl.string',
  30642. '});',
  30643. 'this.$rtti.$StaticArray("TByteArray", {',
  30644. ' dims: [32768],',
  30645. ' eltype: rtl.byte',
  30646. '});',
  30647. 'this.$rtti.$ProcVar("TProc", {',
  30648. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30649. '});',
  30650. 'this.p = null;',
  30651. '']),
  30652. LinesToStr([ // $mod.$main
  30653. '$mod.p = $mod.$rtti["TFlagNames"];',
  30654. '$mod.p = $mod.$rtti["TBoolNames"];',
  30655. '']));
  30656. end;
  30657. procedure TTestModule.TestRTTI_DynArray;
  30658. begin
  30659. WithTypeInfo:=true;
  30660. StartProgram(false);
  30661. Add('type');
  30662. Add(' TArrStr = array of string;');
  30663. Add(' TArr2Dim = array of tarrstr;');
  30664. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30665. Add('var p: pointer;');
  30666. Add('begin');
  30667. Add(' p:=typeinfo(tarrstr);');
  30668. Add(' p:=typeinfo(tarr2dim);');
  30669. ConvertProgram;
  30670. CheckSource('TestRTTI_DynArray',
  30671. LinesToStr([ // statements
  30672. 'this.$rtti.$DynArray("TArrStr", {',
  30673. ' eltype: rtl.string',
  30674. '});',
  30675. 'this.$rtti.$DynArray("TArr2Dim", {',
  30676. ' eltype: this.$rtti["TArrStr"]',
  30677. '});',
  30678. 'this.$rtti.$ProcVar("TProc", {',
  30679. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30680. '});',
  30681. 'this.p = null;',
  30682. '']),
  30683. LinesToStr([ // $mod.$main
  30684. '$mod.p = $mod.$rtti["TArrStr"];',
  30685. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30686. '']));
  30687. end;
  30688. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30689. begin
  30690. WithTypeInfo:=true;
  30691. StartProgram(false);
  30692. Add('type');
  30693. Add(' TArr = array of array of longint;');
  30694. Add('var a: TArr;');
  30695. Add('begin');
  30696. ConvertProgram;
  30697. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30698. LinesToStr([ // statements
  30699. 'this.$rtti.$DynArray("TArr$a", {',
  30700. ' eltype: rtl.longint',
  30701. '});',
  30702. 'this.$rtti.$DynArray("TArr", {',
  30703. ' eltype: this.$rtti["TArr$a"]',
  30704. '});',
  30705. 'this.a = [];',
  30706. '']),
  30707. LinesToStr([ // $mod.$main
  30708. ]));
  30709. end;
  30710. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30711. begin
  30712. WithTypeInfo:=true;
  30713. StartProgram(false);
  30714. Add('type');
  30715. Add(' TObject = class');
  30716. Add(' published');
  30717. Add(' procedure Proc; virtual; abstract;');
  30718. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30719. Add(' end;');
  30720. Add('begin');
  30721. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30722. nDuplicatePublishedMethodXAtY);
  30723. ConvertProgram;
  30724. end;
  30725. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30726. begin
  30727. WithTypeInfo:=true;
  30728. StartUnit(false);
  30729. Add([
  30730. 'interface',
  30731. 'type',
  30732. ' TObject = class',
  30733. ' end;',
  30734. ' {$M+}',
  30735. ' TBird = class',
  30736. ' procedure Fly;',
  30737. ' end;',
  30738. ' {$M-}',
  30739. 'type',
  30740. ' TEagle = class(TBird)',
  30741. ' procedure Fly;',
  30742. ' end;',
  30743. 'implementation',
  30744. 'procedure TBird.Fly;',
  30745. 'begin',
  30746. 'end;',
  30747. 'procedure TEagle.Fly;',
  30748. 'begin',
  30749. 'end;',
  30750. '']);
  30751. ConvertUnit;
  30752. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30753. LinesToStr([ // statements
  30754. 'rtl.createClass(this, "TObject", null, function () {',
  30755. ' this.$init = function () {',
  30756. ' };',
  30757. ' this.$final = function () {',
  30758. ' };',
  30759. '});',
  30760. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30761. ' this.Fly = function () {',
  30762. ' };',
  30763. ' var $r = this.$rtti;',
  30764. ' $r.addMethod("Fly", 0, []);',
  30765. '});',
  30766. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30767. ' this.Fly = function () {',
  30768. ' };',
  30769. ' var $r = this.$rtti;',
  30770. ' $r.addMethod("Fly", 0, []);',
  30771. '});',
  30772. '']),
  30773. LinesToStr([ // $mod.$main
  30774. ]));
  30775. CheckResolverUnexpectedHints(true);
  30776. end;
  30777. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30778. begin
  30779. WithTypeInfo:=true;
  30780. StartProgram(false);
  30781. Add('type');
  30782. Add(' TObject = class');
  30783. Add(' published');
  30784. Add(' procedure Proc; external name ''foo'';');
  30785. Add(' end;');
  30786. Add('begin');
  30787. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30788. nPublishedNameMustMatchExternal);
  30789. ConvertProgram;
  30790. end;
  30791. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30792. begin
  30793. WithTypeInfo:=true;
  30794. StartProgram(false);
  30795. Add('type');
  30796. Add(' TObject = class');
  30797. Add(' class var FA: longint;');
  30798. Add(' published');
  30799. Add(' class property A: longint read FA;');
  30800. Add(' end;');
  30801. Add('begin');
  30802. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30803. nInvalidXModifierY);
  30804. ConvertProgram;
  30805. end;
  30806. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30807. begin
  30808. WithTypeInfo:=true;
  30809. StartProgram(false);
  30810. Add('type');
  30811. Add(' TObject = class');
  30812. Add(' published');
  30813. Add(' class var FA: longint;');
  30814. Add(' end;');
  30815. Add('begin');
  30816. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30817. nSymbolCannotBePublished);
  30818. ConvertProgram;
  30819. end;
  30820. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30821. begin
  30822. WithTypeInfo:=true;
  30823. StartProgram(false);
  30824. Add('{$modeswitch externalclass}');
  30825. Add('type');
  30826. Add(' TObject = class');
  30827. Add(' published');
  30828. Add(' V: longint; external name ''foo'';');
  30829. Add(' end;');
  30830. Add('begin');
  30831. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30832. nPublishedNameMustMatchExternal);
  30833. ConvertProgram;
  30834. end;
  30835. procedure TTestModule.TestRTTI_Class_Field;
  30836. begin
  30837. WithTypeInfo:=true;
  30838. StartProgram(false);
  30839. Add('{$modeswitch externalclass}');
  30840. Add('type');
  30841. Add(' TObject = class');
  30842. Add(' private');
  30843. Add(' FPropA: string;');
  30844. Add(' published');
  30845. Add(' VarLI: longint;');
  30846. Add(' VarC: char;');
  30847. Add(' VarS: string;');
  30848. Add(' VarD: double;');
  30849. Add(' VarB: boolean;');
  30850. Add(' VarLW: longword;');
  30851. Add(' VarSmI: smallint;');
  30852. Add(' VarW: word;');
  30853. Add(' VarShI: shortint;');
  30854. Add(' VarBy: byte;');
  30855. Add(' VarExt: longint external name ''VarExt'';');
  30856. Add(' ArrA, ArrB: array of byte;');
  30857. Add(' end;');
  30858. Add('var p: pointer;');
  30859. Add(' Obj: tobject;');
  30860. Add('begin');
  30861. Add(' p:=typeinfo(tobject);');
  30862. Add(' p:=typeinfo(p);');
  30863. Add(' p:=typeinfo(obj);');
  30864. ConvertProgram;
  30865. CheckSource('TestRTTI_Class_Field',
  30866. LinesToStr([ // statements
  30867. 'rtl.createClass(this, "TObject", null, function () {',
  30868. ' this.$init = function () {',
  30869. ' this.FPropA = "";',
  30870. ' this.VarLI = 0;',
  30871. ' this.VarC = "\x00";',
  30872. ' this.VarS = "";',
  30873. ' this.VarD = 0.0;',
  30874. ' this.VarB = false;',
  30875. ' this.VarLW = 0;',
  30876. ' this.VarSmI = 0;',
  30877. ' this.VarW = 0;',
  30878. ' this.VarShI = 0;',
  30879. ' this.VarBy = 0;',
  30880. ' this.ArrA = [];',
  30881. ' this.ArrB = [];',
  30882. ' };',
  30883. ' this.$final = function () {',
  30884. ' this.ArrA = undefined;',
  30885. ' this.ArrB = undefined;',
  30886. ' };',
  30887. ' var $r = this.$rtti;',
  30888. ' $r.addField("VarLI", rtl.longint);',
  30889. ' $r.addField("VarC", rtl.char);',
  30890. ' $r.addField("VarS", rtl.string);',
  30891. ' $r.addField("VarD", rtl.double);',
  30892. ' $r.addField("VarB", rtl.boolean);',
  30893. ' $r.addField("VarLW", rtl.longword);',
  30894. ' $r.addField("VarSmI", rtl.smallint);',
  30895. ' $r.addField("VarW", rtl.word);',
  30896. ' $r.addField("VarShI", rtl.shortint);',
  30897. ' $r.addField("VarBy", rtl.byte);',
  30898. ' $r.addField("VarExt", rtl.longint);',
  30899. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30900. ' eltype: rtl.byte',
  30901. ' });',
  30902. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30903. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30904. '});',
  30905. 'this.p = null;',
  30906. 'this.Obj = null;',
  30907. '']),
  30908. LinesToStr([ // $mod.$main
  30909. '$mod.p = $mod.$rtti["TObject"];',
  30910. '$mod.p = rtl.pointer;',
  30911. '$mod.p = $mod.Obj.$rtti;',
  30912. '']));
  30913. end;
  30914. procedure TTestModule.TestRTTI_Class_Method;
  30915. begin
  30916. WithTypeInfo:=true;
  30917. StartProgram(false);
  30918. Add([
  30919. 'type',
  30920. ' TObject = class',
  30921. ' private',
  30922. ' procedure Internal; external name ''$intern'';',
  30923. ' published',
  30924. ' procedure Click; virtual; abstract;',
  30925. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30926. ' function GetNotify: boolean; external name ''GetNotify'';',
  30927. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30928. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30929. ' end;',
  30930. 'begin']);
  30931. ConvertProgram;
  30932. CheckSource('TestRTTI_Class_Method',
  30933. LinesToStr([ // statements
  30934. 'rtl.createClass(this, "TObject", null, function () {',
  30935. ' this.$init = function () {',
  30936. ' };',
  30937. ' this.$final = function () {',
  30938. ' };',
  30939. ' var $r = this.$rtti;',
  30940. ' $r.addMethod("Click", 0, []);',
  30941. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30942. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30943. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30944. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30945. '});',
  30946. '']),
  30947. LinesToStr([ // $mod.$main
  30948. '']));
  30949. end;
  30950. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30951. begin
  30952. WithTypeInfo:=true;
  30953. StartProgram(false);
  30954. Add('type');
  30955. Add(' TObject = class');
  30956. Add(' published');
  30957. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30958. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30959. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30960. Add(' end;');
  30961. Add('begin');
  30962. ConvertProgram;
  30963. CheckSource('TestRTTI_Class_MethodOpenArray',
  30964. LinesToStr([ // statements
  30965. 'rtl.createClass(this, "TObject", null, function () {',
  30966. ' this.$init = function () {',
  30967. ' };',
  30968. ' this.$final = function () {',
  30969. ' };',
  30970. ' var $r = this.$rtti;',
  30971. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30972. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30973. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30974. '});',
  30975. '']),
  30976. LinesToStr([ // $mod.$main
  30977. '']));
  30978. end;
  30979. procedure TTestModule.TestRTTI_Class_Property;
  30980. begin
  30981. WithTypeInfo:=true;
  30982. StartProgram(false);
  30983. Add('{$modeswitch externalclass}');
  30984. Add('type');
  30985. Add(' TObject = class');
  30986. Add(' private');
  30987. Add(' FColor: longint;');
  30988. Add(' FColorStored: boolean;');
  30989. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30990. Add(' function GetColor: longint; virtual; abstract;');
  30991. Add(' function GetColorStored: boolean; virtual; abstract;');
  30992. Add(' FExtSize: longint external name ''$extSize'';');
  30993. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30994. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30995. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30996. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30997. Add(' published');
  30998. Add(' property ColorA: longint read FColor;');
  30999. Add(' property ColorB: longint write FColor;');
  31000. Add(' property ColorC: longint read GetColor write SetColor;');
  31001. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  31002. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  31003. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  31004. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  31005. Add(' end;');
  31006. Add('begin');
  31007. ConvertProgram;
  31008. CheckSource('TestRTTI_Class_Property',
  31009. LinesToStr([ // statements
  31010. 'rtl.createClass(this, "TObject", null, function () {',
  31011. ' this.$init = function () {',
  31012. ' this.FColor = 0;',
  31013. ' this.FColorStored = false;',
  31014. ' };',
  31015. ' this.$final = function () {',
  31016. ' };',
  31017. ' var $r = this.$rtti;',
  31018. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  31019. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  31020. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  31021. ' $r.addProperty(',
  31022. ' "ColorD",',
  31023. ' 8,',
  31024. ' rtl.longint,',
  31025. ' "FColor",',
  31026. ' "FColor",',
  31027. ' {',
  31028. ' stored: "FColorStored"',
  31029. ' }',
  31030. ' );',
  31031. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  31032. ' $r.addProperty(',
  31033. ' "ExtSizeB",',
  31034. ' 11,',
  31035. ' rtl.longint,',
  31036. ' "$getSize",',
  31037. ' "$setSize",',
  31038. ' {',
  31039. ' stored: "$extSizeStored"',
  31040. ' }',
  31041. ' );',
  31042. ' $r.addProperty(',
  31043. ' "ExtSizeC",',
  31044. ' 12,',
  31045. ' rtl.longint,',
  31046. ' "$extSize",',
  31047. ' "$extSize",',
  31048. ' {',
  31049. ' stored: "$getExtSizeStored"',
  31050. ' }',
  31051. ' );',
  31052. '});',
  31053. '']),
  31054. LinesToStr([ // $mod.$main
  31055. '']));
  31056. end;
  31057. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31058. begin
  31059. WithTypeInfo:=true;
  31060. StartProgram(false);
  31061. Add('{$modeswitch externalclass}');
  31062. Add('type');
  31063. Add(' integer = longint;');
  31064. Add(' TObject = class');
  31065. Add(' private');
  31066. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31067. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31068. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31069. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31070. Add(' published');
  31071. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31072. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31073. Add(' end;');
  31074. Add('begin');
  31075. ConvertProgram;
  31076. CheckSource('TestRTTI_Class_PropertyParams',
  31077. LinesToStr([ // statements
  31078. 'rtl.createClass(this, "TObject", null, function () {',
  31079. ' this.$init = function () {',
  31080. ' };',
  31081. ' this.$final = function () {',
  31082. ' };',
  31083. ' var $r = this.$rtti;',
  31084. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31085. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31086. '});',
  31087. '']),
  31088. LinesToStr([ // $mod.$main
  31089. '']));
  31090. end;
  31091. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31092. begin
  31093. WithTypeInfo:=true;
  31094. AddModuleWithIntfImplSrc('unit1.pas',
  31095. 'type TColor = -5..5;',
  31096. '');
  31097. StartProgram(true);
  31098. Add([
  31099. 'uses unit1;',
  31100. 'type',
  31101. ' TColorAlias = TColor;',
  31102. ' TColorTypeAlias = type TColor;',
  31103. ' TObject = class',
  31104. ' private',
  31105. ' fColor: TColor;',
  31106. ' fAlias: TColorAlias;',
  31107. ' fTypeAlias: TColorTypeAlias;',
  31108. ' published',
  31109. ' property Color: TColor read fcolor;',
  31110. ' property Alias: TColorAlias read falias;',
  31111. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31112. ' end;',
  31113. 'begin',
  31114. '']);
  31115. ConvertProgram;
  31116. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31117. LinesToStr([ // statements
  31118. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31119. 'rtl.createClass(this, "TObject", null, function () {',
  31120. ' this.$init = function () {',
  31121. ' this.fColor = 0;',
  31122. ' this.fAlias = 0;',
  31123. ' this.fTypeAlias = 0;',
  31124. ' };',
  31125. ' this.$final = function () {',
  31126. ' };',
  31127. ' var $r = this.$rtti;',
  31128. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31129. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31130. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31131. '});',
  31132. '']),
  31133. LinesToStr([ // $mod.$main
  31134. '']));
  31135. end;
  31136. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31137. begin
  31138. WithTypeInfo:=true;
  31139. StartProgram(false);
  31140. Add([
  31141. '{$modeswitch omitrtti}',
  31142. 'type',
  31143. ' TObject = class',
  31144. ' private',
  31145. ' FA: byte;',
  31146. ' published',
  31147. ' property A: byte read FA write FA;',
  31148. ' end;',
  31149. 'begin']);
  31150. ConvertProgram;
  31151. CheckSource('TestRTTI_Class_OmitRTTI',
  31152. LinesToStr([ // statements
  31153. 'rtl.createClass(this, "TObject", null, function () {',
  31154. ' this.$init = function () {',
  31155. ' this.FA = 0;',
  31156. ' };',
  31157. ' this.$final = function () {',
  31158. ' };',
  31159. '});',
  31160. '']),
  31161. LinesToStr([ // $mod.$main
  31162. '']));
  31163. end;
  31164. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31165. begin
  31166. WithTypeInfo:=true;
  31167. StartUnit(true,[supTObject]);
  31168. Add([
  31169. 'interface',
  31170. 'type',
  31171. ' {$M+}',
  31172. ' TBird = class',
  31173. ' published',
  31174. ' Swarm: array of TBird;',
  31175. ' end;',
  31176. 'implementation',
  31177. '']);
  31178. ConvertUnit;
  31179. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31180. LinesToStr([ // statements
  31181. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31182. ' this.$init = function () {',
  31183. ' pas.system.TObject.$init.call(this);',
  31184. ' this.Swarm = [];',
  31185. ' };',
  31186. ' this.$final = function () {',
  31187. ' this.Swarm = undefined;',
  31188. ' pas.system.TObject.$final.call(this);',
  31189. ' };',
  31190. ' var $r = this.$rtti;',
  31191. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31192. ' eltype: $r',
  31193. ' });',
  31194. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  31195. '});',
  31196. '']),
  31197. LinesToStr([ // $mod.$main
  31198. '']));
  31199. end;
  31200. procedure TTestModule.TestRTTI_IndexModifier;
  31201. begin
  31202. WithTypeInfo:=true;
  31203. StartProgram(false);
  31204. Add([
  31205. 'type',
  31206. ' TEnum = (red, blue);',
  31207. ' TObject = class',
  31208. ' FB: boolean;',
  31209. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31210. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31211. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31212. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31213. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31214. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31215. ' published',
  31216. ' property B1: boolean index 1 read FB write SetIntBool;',
  31217. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31218. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31219. ' end;',
  31220. 'begin']);
  31221. ConvertProgram;
  31222. CheckSource('TestRTTI_IndexModifier',
  31223. LinesToStr([ // statements
  31224. 'this.TEnum = {',
  31225. ' "0": "red",',
  31226. ' red: 0,',
  31227. ' "1": "blue",',
  31228. ' blue: 1',
  31229. '};',
  31230. 'this.$rtti.$Enum("TEnum", {',
  31231. ' minvalue: 0,',
  31232. ' maxvalue: 1,',
  31233. ' ordtype: 1,',
  31234. ' enumtype: this.TEnum',
  31235. '});',
  31236. 'rtl.createClass(this, "TObject", null, function () {',
  31237. ' this.$init = function () {',
  31238. ' this.FB = false;',
  31239. ' };',
  31240. ' this.$final = function () {',
  31241. ' };',
  31242. ' var $r = this.$rtti;',
  31243. ' $r.addProperty(',
  31244. ' "B1",',
  31245. ' 18,',
  31246. ' rtl.boolean,',
  31247. ' "FB",',
  31248. ' "SetIntBool",',
  31249. ' {',
  31250. ' index: 1',
  31251. ' }',
  31252. ' );',
  31253. ' $r.addProperty(',
  31254. ' "B2",',
  31255. ' 17,',
  31256. ' rtl.boolean,',
  31257. ' "GetEnumBool",',
  31258. ' "FB",',
  31259. ' {',
  31260. ' index: $mod.TEnum.blue',
  31261. ' }',
  31262. ' );',
  31263. ' $r.addProperty(',
  31264. ' "I1",',
  31265. ' 19,',
  31266. ' rtl.boolean,',
  31267. ' "GetStrIntBool",',
  31268. ' "SetStrIntBool",',
  31269. ' {',
  31270. ' index: 2',
  31271. ' }',
  31272. ' );',
  31273. '});',
  31274. '']),
  31275. LinesToStr([ // $mod.$main
  31276. '']));
  31277. end;
  31278. procedure TTestModule.TestRTTI_StoredModifier;
  31279. begin
  31280. WithTypeInfo:=true;
  31281. StartProgram(false);
  31282. Add([
  31283. 'const',
  31284. ' ConstB = true;',
  31285. 'type',
  31286. ' TObject = class',
  31287. ' private',
  31288. ' FB: boolean;',
  31289. ' function IsBStored: boolean; virtual; abstract;',
  31290. ' published',
  31291. ' property BoolA: boolean read FB stored true;',
  31292. ' property BoolB: boolean read FB stored false;',
  31293. ' property BoolC: boolean read FB stored FB;',
  31294. ' property BoolD: boolean read FB stored ConstB;',
  31295. ' property BoolE: boolean read FB stored IsBStored;',
  31296. ' end;',
  31297. 'begin']);
  31298. ConvertProgram;
  31299. CheckSource('TestRTTI_StoredModifier',
  31300. LinesToStr([ // statements
  31301. 'this.ConstB = true;',
  31302. 'rtl.createClass(this, "TObject", null, function () {',
  31303. ' this.$init = function () {',
  31304. ' this.FB = false;',
  31305. ' };',
  31306. ' this.$final = function () {',
  31307. ' };',
  31308. ' var $r = this.$rtti;',
  31309. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31310. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31311. ' $r.addProperty(',
  31312. ' "BoolC",',
  31313. ' 8,',
  31314. ' rtl.boolean,',
  31315. ' "FB",',
  31316. ' "",',
  31317. ' {',
  31318. ' stored: "FB"',
  31319. ' }',
  31320. ' );',
  31321. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31322. ' $r.addProperty(',
  31323. ' "BoolE",',
  31324. ' 12,',
  31325. ' rtl.boolean,',
  31326. ' "FB",',
  31327. ' "",',
  31328. ' {',
  31329. ' stored: "IsBStored"',
  31330. ' }',
  31331. ' );',
  31332. '});',
  31333. '']),
  31334. LinesToStr([ // $mod.$main
  31335. '']));
  31336. end;
  31337. procedure TTestModule.TestRTTI_DefaultValue;
  31338. begin
  31339. WithTypeInfo:=true;
  31340. StartProgram(false);
  31341. Add([
  31342. 'type',
  31343. ' TEnum = (red, blue);',
  31344. 'const',
  31345. ' CB = true or false;',
  31346. ' CI = 1+2;',
  31347. 'type',
  31348. ' TObject = class',
  31349. ' FB: boolean;',
  31350. ' FI: longint;',
  31351. ' FE: TEnum;',
  31352. ' published',
  31353. ' property B1: boolean read FB default true;',
  31354. ' property B2: boolean read FB default CB;',
  31355. ' property B3: boolean read FB default test1.cb;',
  31356. ' property I1: longint read FI default 2;',
  31357. ' property I2: longint read FI default CI;',
  31358. ' property E1: TEnum read FE default red;',
  31359. ' property E2: TEnum read FE default TEnum.blue;',
  31360. ' end;',
  31361. 'begin']);
  31362. ConvertProgram;
  31363. CheckSource('TestRTTI_DefaultValue',
  31364. LinesToStr([ // statements
  31365. 'this.TEnum = {',
  31366. ' "0": "red",',
  31367. ' red: 0,',
  31368. ' "1": "blue",',
  31369. ' blue: 1',
  31370. '};',
  31371. 'this.$rtti.$Enum("TEnum", {',
  31372. ' minvalue: 0,',
  31373. ' maxvalue: 1,',
  31374. ' ordtype: 1,',
  31375. ' enumtype: this.TEnum',
  31376. '});',
  31377. 'this.CB = true || false;',
  31378. 'this.CI = 1 + 2;',
  31379. 'rtl.createClass(this, "TObject", null, function () {',
  31380. ' this.$init = function () {',
  31381. ' this.FB = false;',
  31382. ' this.FI = 0;',
  31383. ' this.FE = 0;',
  31384. ' };',
  31385. ' this.$final = function () {',
  31386. ' };',
  31387. ' var $r = this.$rtti;',
  31388. ' $r.addProperty(',
  31389. ' "B1",',
  31390. ' 0,',
  31391. ' rtl.boolean,',
  31392. ' "FB",',
  31393. ' "",',
  31394. ' {',
  31395. ' Default: true',
  31396. ' }',
  31397. ' );',
  31398. ' $r.addProperty(',
  31399. ' "B2",',
  31400. ' 0,',
  31401. ' rtl.boolean,',
  31402. ' "FB",',
  31403. ' "",',
  31404. ' {',
  31405. ' Default: true',
  31406. ' }',
  31407. ' );',
  31408. ' $r.addProperty(',
  31409. ' "B3",',
  31410. ' 0,',
  31411. ' rtl.boolean,',
  31412. ' "FB",',
  31413. ' "",',
  31414. ' {',
  31415. ' Default: true',
  31416. ' }',
  31417. ' );',
  31418. ' $r.addProperty(',
  31419. ' "I1",',
  31420. ' 0,',
  31421. ' rtl.longint,',
  31422. ' "FI",',
  31423. ' "",',
  31424. ' {',
  31425. ' Default: 2',
  31426. ' }',
  31427. ' );',
  31428. ' $r.addProperty(',
  31429. ' "I2",',
  31430. ' 0,',
  31431. ' rtl.longint,',
  31432. ' "FI",',
  31433. ' "",',
  31434. ' {',
  31435. ' Default: 3',
  31436. ' }',
  31437. ' );',
  31438. ' $r.addProperty(',
  31439. ' "E1",',
  31440. ' 0,',
  31441. ' $mod.$rtti["TEnum"],',
  31442. ' "FE",',
  31443. ' "",',
  31444. ' {',
  31445. ' Default: $mod.TEnum.red',
  31446. ' }',
  31447. ' );',
  31448. ' $r.addProperty(',
  31449. ' "E2",',
  31450. ' 0,',
  31451. ' $mod.$rtti["TEnum"],',
  31452. ' "FE",',
  31453. ' "",',
  31454. ' {',
  31455. ' Default: $mod.TEnum.blue',
  31456. ' }',
  31457. ' );',
  31458. '});',
  31459. '']),
  31460. LinesToStr([ // $mod.$main
  31461. '']));
  31462. end;
  31463. procedure TTestModule.TestRTTI_DefaultValueSet;
  31464. begin
  31465. WithTypeInfo:=true;
  31466. StartProgram(false);
  31467. Add([
  31468. 'type',
  31469. ' TEnum = (red, blue);',
  31470. ' TSet = set of TEnum;',
  31471. 'const',
  31472. ' CSet = [red,blue];',
  31473. 'type',
  31474. ' TObject = class',
  31475. ' FSet: TSet;',
  31476. ' published',
  31477. ' property Set1: TSet read FSet default [];',
  31478. ' property Set2: TSet read FSet default [red];',
  31479. ' property Set3: TSet read FSet default [red,blue];',
  31480. ' property Set4: TSet read FSet default CSet;',
  31481. ' end;',
  31482. 'begin']);
  31483. ConvertProgram;
  31484. CheckSource('TestRTTI_DefaultValueSet',
  31485. LinesToStr([ // statements
  31486. 'this.TEnum = {',
  31487. ' "0": "red",',
  31488. ' red: 0,',
  31489. ' "1": "blue",',
  31490. ' blue: 1',
  31491. '};',
  31492. 'this.$rtti.$Enum("TEnum", {',
  31493. ' minvalue: 0,',
  31494. ' maxvalue: 1,',
  31495. ' ordtype: 1,',
  31496. ' enumtype: this.TEnum',
  31497. '});',
  31498. 'this.$rtti.$Set("TSet", {',
  31499. ' comptype: this.$rtti["TEnum"]',
  31500. '});',
  31501. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31502. 'rtl.createClass(this, "TObject", null, function () {',
  31503. ' this.$init = function () {',
  31504. ' this.FSet = {};',
  31505. ' };',
  31506. ' this.$final = function () {',
  31507. ' this.FSet = undefined;',
  31508. ' };',
  31509. ' var $r = this.$rtti;',
  31510. ' $r.addProperty(',
  31511. ' "Set1",',
  31512. ' 0,',
  31513. ' $mod.$rtti["TSet"],',
  31514. ' "FSet",',
  31515. ' "",',
  31516. ' {',
  31517. ' Default: {}',
  31518. ' }',
  31519. ' );',
  31520. ' $r.addProperty(',
  31521. ' "Set2",',
  31522. ' 0,',
  31523. ' $mod.$rtti["TSet"],',
  31524. ' "FSet",',
  31525. ' "",',
  31526. ' {',
  31527. ' Default: rtl.createSet($mod.TEnum.red)',
  31528. ' }',
  31529. ' );',
  31530. ' $r.addProperty(',
  31531. ' "Set3",',
  31532. ' 0,',
  31533. ' $mod.$rtti["TSet"],',
  31534. ' "FSet",',
  31535. ' "",',
  31536. ' {',
  31537. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31538. ' }',
  31539. ' );',
  31540. ' $r.addProperty(',
  31541. ' "Set4",',
  31542. ' 0,',
  31543. ' $mod.$rtti["TSet"],',
  31544. ' "FSet",',
  31545. ' "",',
  31546. ' {',
  31547. ' Default: $mod.CSet',
  31548. ' }',
  31549. ' );',
  31550. '});',
  31551. '']),
  31552. LinesToStr([ // $mod.$main
  31553. '']));
  31554. end;
  31555. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31556. begin
  31557. WithTypeInfo:=true;
  31558. StartProgram(false);
  31559. Add([
  31560. 'type',
  31561. ' TRg = -1..1;',
  31562. 'const',
  31563. ' l = low(TRg);',
  31564. ' h = high(TRg);',
  31565. 'type',
  31566. ' TObject = class',
  31567. ' FV: TRg;',
  31568. ' published',
  31569. ' property V1: TRg read FV default -1;',
  31570. ' end;',
  31571. 'begin']);
  31572. ConvertProgram;
  31573. CheckSource('TestRTTI_DefaultValueRangeType',
  31574. LinesToStr([ // statements
  31575. 'this.$rtti.$Int("TRg", {',
  31576. ' minvalue: -1,',
  31577. ' maxvalue: 1,',
  31578. ' ordtype: 0',
  31579. '});',
  31580. 'this.l = -1;',
  31581. 'this.h = 1;',
  31582. 'rtl.createClass(this, "TObject", null, function () {',
  31583. ' this.$init = function () {',
  31584. ' this.FV = 0;',
  31585. ' };',
  31586. ' this.$final = function () {',
  31587. ' };',
  31588. ' var $r = this.$rtti;',
  31589. ' $r.addProperty(',
  31590. ' "V1",',
  31591. ' 0,',
  31592. ' $mod.$rtti["TRg"],',
  31593. ' "FV",',
  31594. ' "",',
  31595. ' {',
  31596. ' Default: -1',
  31597. ' }',
  31598. ' );',
  31599. '});',
  31600. '']),
  31601. LinesToStr([ // $mod.$main
  31602. '']));
  31603. end;
  31604. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31605. begin
  31606. WithTypeInfo:=true;
  31607. StartProgram(false);
  31608. Add([
  31609. 'type',
  31610. ' TObject = class',
  31611. ' FA, FB: byte;',
  31612. ' property A: byte read FA default 1;',
  31613. ' property B: byte read FB default 2;',
  31614. ' end;',
  31615. ' TBird = class',
  31616. ' published',
  31617. ' property A;',
  31618. ' property B nodefault;',
  31619. ' end;',
  31620. 'begin']);
  31621. ConvertProgram;
  31622. CheckSource('TestRTTI_DefaultValueInherit',
  31623. LinesToStr([ // statements
  31624. 'rtl.createClass(this, "TObject", null, function () {',
  31625. ' this.$init = function () {',
  31626. ' this.FA = 0;',
  31627. ' this.FB = 0;',
  31628. ' };',
  31629. ' this.$final = function () {',
  31630. ' };',
  31631. '});',
  31632. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31633. ' var $r = this.$rtti;',
  31634. ' $r.addProperty(',
  31635. ' "A",',
  31636. ' 0,',
  31637. ' rtl.byte,',
  31638. ' "FA",',
  31639. ' "",',
  31640. ' {',
  31641. ' Default: 1',
  31642. ' }',
  31643. ' );',
  31644. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31645. '});',
  31646. '']),
  31647. LinesToStr([ // $mod.$main
  31648. '']));
  31649. end;
  31650. procedure TTestModule.TestRTTI_OverrideMethod;
  31651. begin
  31652. WithTypeInfo:=true;
  31653. StartProgram(false);
  31654. Add('type');
  31655. Add(' TObject = class');
  31656. Add(' published');
  31657. Add(' procedure DoIt; virtual; abstract;');
  31658. Add(' end;');
  31659. Add(' TSky = class');
  31660. Add(' published');
  31661. Add(' procedure DoIt; override;');
  31662. Add(' end;');
  31663. Add('procedure TSky.DoIt; begin end;');
  31664. Add('begin');
  31665. ConvertProgram;
  31666. CheckSource('TestRTTI_OverrideMethod',
  31667. LinesToStr([ // statements
  31668. 'rtl.createClass(this, "TObject", null, function () {',
  31669. ' this.$init = function () {',
  31670. ' };',
  31671. ' this.$final = function () {',
  31672. ' };',
  31673. ' var $r = this.$rtti;',
  31674. ' $r.addMethod("DoIt", 0, []);',
  31675. '});',
  31676. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31677. ' this.DoIt = function () {',
  31678. ' };',
  31679. '});',
  31680. '']),
  31681. LinesToStr([ // $mod.$main
  31682. '']));
  31683. end;
  31684. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31685. begin
  31686. WithTypeInfo:=true;
  31687. StartProgram(false);
  31688. Add([
  31689. 'type',
  31690. ' TObject = class',
  31691. ' published',
  31692. ' procedure DoIt;',
  31693. ' end;',
  31694. ' TSky = class',
  31695. ' published',
  31696. ' procedure DoIt; reintroduce;',
  31697. ' end;',
  31698. 'procedure TObject.DoIt; begin end;',
  31699. 'procedure TSky.DoIt;',
  31700. 'begin',
  31701. ' inherited DoIt;',
  31702. 'end;',
  31703. 'begin']);
  31704. ConvertProgram;
  31705. CheckSource('TestRTTI_ReintroduceMethod',
  31706. LinesToStr([ // statements
  31707. 'rtl.createClass(this, "TObject", null, function () {',
  31708. ' this.$init = function () {',
  31709. ' };',
  31710. ' this.$final = function () {',
  31711. ' };',
  31712. ' this.DoIt = function () {',
  31713. ' };',
  31714. ' var $r = this.$rtti;',
  31715. ' $r.addMethod("DoIt", 0, []);',
  31716. '});',
  31717. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31718. ' this.DoIt = function () {',
  31719. ' $mod.TObject.DoIt.call(this);',
  31720. ' };',
  31721. ' var $r = this.$rtti;',
  31722. ' $r.addMethod("DoIt", 0, []);',
  31723. '});',
  31724. '']),
  31725. LinesToStr([ // $mod.$main
  31726. '']));
  31727. end;
  31728. procedure TTestModule.TestRTTI_OverloadProperty;
  31729. begin
  31730. WithTypeInfo:=true;
  31731. StartProgram(false);
  31732. Add('type');
  31733. Add(' TObject = class');
  31734. Add(' protected');
  31735. Add(' FFlag: longint;');
  31736. Add(' published');
  31737. Add(' property Flag: longint read fflag;');
  31738. Add(' end;');
  31739. Add(' TSky = class');
  31740. Add(' published');
  31741. Add(' property FLAG: longint write fflag;');
  31742. Add(' end;');
  31743. Add('begin');
  31744. ConvertProgram;
  31745. CheckSource('TestRTTI_OverrideMethod',
  31746. LinesToStr([ // statements
  31747. 'rtl.createClass(this, "TObject", null, function () {',
  31748. ' this.$init = function () {',
  31749. ' this.FFlag = 0;',
  31750. ' };',
  31751. ' this.$final = function () {',
  31752. ' };',
  31753. ' var $r = this.$rtti;',
  31754. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31755. '});',
  31756. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31757. ' var $r = this.$rtti;',
  31758. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31759. '});',
  31760. '']),
  31761. LinesToStr([ // $mod.$main
  31762. '']));
  31763. end;
  31764. procedure TTestModule.TestRTTI_ClassForward;
  31765. begin
  31766. WithTypeInfo:=true;
  31767. StartProgram(false);
  31768. Add('type');
  31769. Add(' TObject = class end;');
  31770. Add(' tbridge = class;');
  31771. Add(' TProc = function: tbridge;');
  31772. Add(' TOger = class');
  31773. Add(' published');
  31774. Add(' FBridge: tbridge;');
  31775. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31776. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31777. Add(' end;');
  31778. Add(' TBridge = class');
  31779. Add(' FOger: toger;');
  31780. Add(' end;');
  31781. Add('var p: Pointer;');
  31782. Add(' b: tbridge;');
  31783. Add('begin');
  31784. Add(' p:=typeinfo(tbridge);');
  31785. Add(' p:=typeinfo(b);');
  31786. ConvertProgram;
  31787. CheckSource('TestRTTI_ClassForward',
  31788. LinesToStr([ // statements
  31789. 'rtl.createClass(this, "TObject", null, function () {',
  31790. ' this.$init = function () {',
  31791. ' };',
  31792. ' this.$final = function () {',
  31793. ' };',
  31794. '});',
  31795. 'this.$rtti.$Class("TBridge");',
  31796. 'this.$rtti.$ProcVar("TProc", {',
  31797. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31798. '});',
  31799. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31800. ' this.$init = function () {',
  31801. ' $mod.TObject.$init.call(this);',
  31802. ' this.FBridge = null;',
  31803. ' };',
  31804. ' this.$final = function () {',
  31805. ' this.FBridge = undefined;',
  31806. ' $mod.TObject.$final.call(this);',
  31807. ' };',
  31808. ' var $r = this.$rtti;',
  31809. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31810. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31811. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31812. '});',
  31813. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31814. ' this.$init = function () {',
  31815. ' $mod.TObject.$init.call(this);',
  31816. ' this.FOger = null;',
  31817. ' };',
  31818. ' this.$final = function () {',
  31819. ' this.FOger = undefined;',
  31820. ' $mod.TObject.$final.call(this);',
  31821. ' };',
  31822. '});',
  31823. 'this.p = null;',
  31824. 'this.b = null;',
  31825. '']),
  31826. LinesToStr([ // $mod.$main
  31827. '$mod.p = $mod.$rtti["TBridge"];',
  31828. '$mod.p = $mod.b.$rtti;',
  31829. '']));
  31830. end;
  31831. procedure TTestModule.TestRTTI_ClassOf;
  31832. begin
  31833. WithTypeInfo:=true;
  31834. StartProgram(false);
  31835. Add('type');
  31836. Add(' TClass = class of tobject;');
  31837. Add(' TProcA = function: TClass;');
  31838. Add(' TObject = class');
  31839. Add(' published');
  31840. Add(' C: tclass;');
  31841. Add(' end;');
  31842. Add(' tfox = class;');
  31843. Add(' TBird = class end;');
  31844. Add(' TBirds = class of tbird;');
  31845. Add(' TFox = class end;');
  31846. Add(' TFoxes = class of tfox;');
  31847. Add(' TCows = class of TCow;');
  31848. Add(' TCow = class;');
  31849. Add(' TCow = class end;');
  31850. Add('begin');
  31851. ConvertProgram;
  31852. CheckSource('TestRTTI_ClassOf',
  31853. LinesToStr([ // statements
  31854. 'this.$rtti.$Class("TObject");',
  31855. 'this.$rtti.$ClassRef("TClass", {',
  31856. ' instancetype: this.$rtti["TObject"]',
  31857. '});',
  31858. 'this.$rtti.$ProcVar("TProcA", {',
  31859. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31860. '});',
  31861. 'rtl.createClass(this, "TObject", null, function () {',
  31862. ' this.$init = function () {',
  31863. ' this.C = null;',
  31864. ' };',
  31865. ' this.$final = function () {',
  31866. ' this.C = undefined;',
  31867. ' };',
  31868. ' var $r = this.$rtti;',
  31869. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31870. '});',
  31871. 'this.$rtti.$Class("TFox");',
  31872. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31873. '});',
  31874. 'this.$rtti.$ClassRef("TBirds", {',
  31875. ' instancetype: this.$rtti["TBird"]',
  31876. '});',
  31877. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31878. '});',
  31879. 'this.$rtti.$ClassRef("TFoxes", {',
  31880. ' instancetype: this.$rtti["TFox"]',
  31881. '});',
  31882. 'this.$rtti.$Class("TCow");',
  31883. 'this.$rtti.$ClassRef("TCows", {',
  31884. ' instancetype: this.$rtti["TCow"]',
  31885. '});',
  31886. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31887. '});',
  31888. '']),
  31889. LinesToStr([ // $mod.$main
  31890. '']));
  31891. end;
  31892. procedure TTestModule.TestRTTI_Record;
  31893. begin
  31894. WithTypeInfo:=true;
  31895. StartProgram(false);
  31896. Add('type');
  31897. Add(' integer = longint;');
  31898. Add(' TPoint = record');
  31899. Add(' x,y: integer;');
  31900. Add(' end;');
  31901. Add('var p: pointer;');
  31902. Add(' r: tpoint;');
  31903. Add('begin');
  31904. Add(' p:=typeinfo(tpoint);');
  31905. Add(' p:=typeinfo(r);');
  31906. Add(' p:=typeinfo(r.x);');
  31907. ConvertProgram;
  31908. CheckSource('TestRTTI_Record',
  31909. LinesToStr([ // statements
  31910. 'rtl.recNewT(this, "TPoint", function () {',
  31911. ' this.x = 0;',
  31912. ' this.y = 0;',
  31913. ' this.$eq = function (b) {',
  31914. ' return (this.x === b.x) && (this.y === b.y);',
  31915. ' };',
  31916. ' this.$assign = function (s) {',
  31917. ' this.x = s.x;',
  31918. ' this.y = s.y;',
  31919. ' return this;',
  31920. ' };',
  31921. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31922. ' $r.addField("x", rtl.longint);',
  31923. ' $r.addField("y", rtl.longint);',
  31924. '});',
  31925. 'this.p = null;',
  31926. 'this.r = this.TPoint.$new();',
  31927. '']),
  31928. LinesToStr([ // $mod.$main
  31929. '$mod.p = $mod.$rtti["TPoint"];',
  31930. '$mod.p = $mod.$rtti["TPoint"];',
  31931. '$mod.p = rtl.longint;',
  31932. '']));
  31933. end;
  31934. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31935. begin
  31936. WithTypeInfo:=true;
  31937. StartProgram(false);
  31938. Add('type');
  31939. Add(' TFloatRec = record');
  31940. Add(' c,d: array of char;');
  31941. // Add(' i: array of array of longint;');
  31942. Add(' end;');
  31943. Add('var p: pointer;');
  31944. Add(' r: tfloatrec;');
  31945. Add('begin');
  31946. Add(' p:=typeinfo(tfloatrec);');
  31947. Add(' p:=typeinfo(r);');
  31948. Add(' p:=typeinfo(r.d);');
  31949. ConvertProgram;
  31950. CheckSource('TestRTTI_Record',
  31951. LinesToStr([ // statements
  31952. 'rtl.recNewT(this, "TFloatRec", function () {',
  31953. ' this.$new = function () {',
  31954. ' var r = Object.create(this);',
  31955. ' r.c = [];',
  31956. ' r.d = [];',
  31957. ' return r;',
  31958. ' };',
  31959. ' this.$eq = function (b) {',
  31960. ' return (this.c === b.c) && (this.d === b.d);',
  31961. ' };',
  31962. ' this.$assign = function (s) {',
  31963. ' this.c = rtl.arrayRef(s.c);',
  31964. ' this.d = rtl.arrayRef(s.d);',
  31965. ' return this;',
  31966. ' };',
  31967. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31968. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31969. ' eltype: rtl.char',
  31970. ' });',
  31971. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31972. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31973. '});',
  31974. 'this.p = null;',
  31975. 'this.r = this.TFloatRec.$new();',
  31976. '']),
  31977. LinesToStr([ // $mod.$main
  31978. '$mod.p = $mod.$rtti["TFloatRec"];',
  31979. '$mod.p = $mod.$rtti["TFloatRec"];',
  31980. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31981. '']));
  31982. end;
  31983. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31984. begin
  31985. WithTypeInfo:=true;
  31986. StartProgram(false);
  31987. Add([
  31988. '{$modeswitch AdvancedRecords}',
  31989. 'type',
  31990. ' TPoint = record',
  31991. ' type TProc = procedure(w: word);',
  31992. ' class var p: TProc;',
  31993. ' end;',
  31994. 'begin',
  31995. '']);
  31996. ConvertProgram;
  31997. CheckSource('TestRTTI_Record_ClassVarType',
  31998. LinesToStr([ // statements
  31999. 'rtl.recNewT(this, "TPoint", function () {',
  32000. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  32001. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  32002. ' });',
  32003. ' this.p = null;',
  32004. ' this.$eq = function (b) {',
  32005. ' return true;',
  32006. ' };',
  32007. ' this.$assign = function (s) {',
  32008. ' return this;',
  32009. ' };',
  32010. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  32011. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  32012. '}, true);',
  32013. '']),
  32014. LinesToStr([ // $mod.$main
  32015. '']));
  32016. end;
  32017. procedure TTestModule.TestRTTI_LocalTypes;
  32018. begin
  32019. WithTypeInfo:=true;
  32020. StartProgram(false);
  32021. Add([
  32022. 'procedure DoIt;',
  32023. 'type',
  32024. ' integer = longint;',
  32025. ' TPoint = record',
  32026. ' x,y: integer;',
  32027. ' end;',
  32028. 'var p: TPoint;',
  32029. 'begin',
  32030. 'end;',
  32031. 'begin']);
  32032. ConvertProgram;
  32033. CheckSource('TestRTTI_LocalTypes',
  32034. LinesToStr([ // statements
  32035. 'var TPoint = rtl.recNewT(null, "", function () {',
  32036. ' this.x = 0;',
  32037. ' this.y = 0;',
  32038. ' this.$eq = function (b) {',
  32039. ' return (this.x === b.x) && (this.y === b.y);',
  32040. ' };',
  32041. ' this.$assign = function (s) {',
  32042. ' this.x = s.x;',
  32043. ' this.y = s.y;',
  32044. ' return this;',
  32045. ' };',
  32046. '});',
  32047. 'this.DoIt = function () {',
  32048. ' var p = TPoint.$new();',
  32049. '};',
  32050. '']),
  32051. LinesToStr([ // $mod.$main
  32052. '']));
  32053. end;
  32054. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  32055. begin
  32056. WithTypeInfo:=true;
  32057. StartProgram(false);
  32058. Add([
  32059. 'type',
  32060. ' TCaption = string;',
  32061. ' TYesNo = boolean;',
  32062. ' TLetter = char;',
  32063. ' TFloat = double;',
  32064. ' TPtr = pointer;',
  32065. ' TShortInt = shortint;',
  32066. ' TByte = byte;',
  32067. ' TSmallInt = smallint;',
  32068. ' TWord = word;',
  32069. ' TInt32 = longint;',
  32070. ' TDWord = longword;',
  32071. ' TValue = jsvalue;',
  32072. 'var p: TPtr;',
  32073. 'begin',
  32074. ' p:=typeinfo(string);',
  32075. ' p:=typeinfo(tcaption);',
  32076. ' p:=typeinfo(boolean);',
  32077. ' p:=typeinfo(tyesno);',
  32078. ' p:=typeinfo(char);',
  32079. ' p:=typeinfo(tletter);',
  32080. ' p:=typeinfo(double);',
  32081. ' p:=typeinfo(tfloat);',
  32082. ' p:=typeinfo(pointer);',
  32083. ' p:=typeinfo(tptr);',
  32084. ' p:=typeinfo(shortint);',
  32085. ' p:=typeinfo(tshortint);',
  32086. ' p:=typeinfo(byte);',
  32087. ' p:=typeinfo(tbyte);',
  32088. ' p:=typeinfo(smallint);',
  32089. ' p:=typeinfo(tsmallint);',
  32090. ' p:=typeinfo(word);',
  32091. ' p:=typeinfo(tword);',
  32092. ' p:=typeinfo(longword);',
  32093. ' p:=typeinfo(tdword);',
  32094. ' p:=typeinfo(jsvalue);',
  32095. ' p:=typeinfo(tvalue);',
  32096. '']);
  32097. ConvertProgram;
  32098. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32099. LinesToStr([ // statements
  32100. 'this.p = null;',
  32101. '']),
  32102. LinesToStr([ // $mod.$main
  32103. '$mod.p = rtl.string;',
  32104. '$mod.p = rtl.string;',
  32105. '$mod.p = rtl.boolean;',
  32106. '$mod.p = rtl.boolean;',
  32107. '$mod.p = rtl.char;',
  32108. '$mod.p = rtl.char;',
  32109. '$mod.p = rtl.double;',
  32110. '$mod.p = rtl.double;',
  32111. '$mod.p = rtl.pointer;',
  32112. '$mod.p = rtl.pointer;',
  32113. '$mod.p = rtl.shortint;',
  32114. '$mod.p = rtl.shortint;',
  32115. '$mod.p = rtl.byte;',
  32116. '$mod.p = rtl.byte;',
  32117. '$mod.p = rtl.smallint;',
  32118. '$mod.p = rtl.smallint;',
  32119. '$mod.p = rtl.word;',
  32120. '$mod.p = rtl.word;',
  32121. '$mod.p = rtl.longword;',
  32122. '$mod.p = rtl.longword;',
  32123. '$mod.p = rtl.jsvalue;',
  32124. '$mod.p = rtl.jsvalue;',
  32125. '']));
  32126. end;
  32127. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32128. begin
  32129. WithTypeInfo:=true;
  32130. StartProgram(false);
  32131. Add([
  32132. 'type',
  32133. ' TCaption = type string;',
  32134. ' TYesNo = type boolean;',
  32135. ' TLetter = type char;',
  32136. ' TFloat = type double;',
  32137. ' TPtr = type pointer;',
  32138. ' TShortInt = type shortint;',
  32139. ' TByte = type byte;',
  32140. ' TSmallInt = type smallint;',
  32141. ' TWord = type word;',
  32142. ' TInt32 = type longint;',
  32143. ' TDWord = type longword;',
  32144. ' TValue = type jsvalue;',
  32145. ' TAliasValue = type TValue;',
  32146. 'var',
  32147. ' p: TPtr;',
  32148. ' a: TAliasValue;',
  32149. 'begin',
  32150. ' p:=typeinfo(tcaption);',
  32151. ' p:=typeinfo(tyesno);',
  32152. ' p:=typeinfo(tletter);',
  32153. ' p:=typeinfo(tfloat);',
  32154. ' p:=typeinfo(tptr);',
  32155. ' p:=typeinfo(tshortint);',
  32156. ' p:=typeinfo(tbyte);',
  32157. ' p:=typeinfo(tsmallint);',
  32158. ' p:=typeinfo(tword);',
  32159. ' p:=typeinfo(tdword);',
  32160. ' p:=typeinfo(tvalue);',
  32161. ' p:=typeinfo(taliasvalue);',
  32162. ' p:=typeinfo(a);',
  32163. '']);
  32164. ConvertProgram;
  32165. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32166. LinesToStr([ // statements
  32167. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32168. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32169. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32170. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32171. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32172. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32173. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32174. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32175. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32176. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32177. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32178. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32179. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32180. 'this.p = null;',
  32181. 'this.a = undefined;',
  32182. '']),
  32183. LinesToStr([ // $mod.$main
  32184. '$mod.p = $mod.$rtti["TCaption"];',
  32185. '$mod.p = $mod.$rtti["TYesNo"];',
  32186. '$mod.p = $mod.$rtti["TLetter"];',
  32187. '$mod.p = $mod.$rtti["TFloat"];',
  32188. '$mod.p = $mod.$rtti["TPtr"];',
  32189. '$mod.p = $mod.$rtti["TShortInt"];',
  32190. '$mod.p = $mod.$rtti["TByte"];',
  32191. '$mod.p = $mod.$rtti["TSmallInt"];',
  32192. '$mod.p = $mod.$rtti["TWord"];',
  32193. '$mod.p = $mod.$rtti["TDWord"];',
  32194. '$mod.p = $mod.$rtti["TValue"];',
  32195. '$mod.p = $mod.$rtti["TAliasValue"];',
  32196. '$mod.p = $mod.$rtti["TAliasValue"];',
  32197. '']));
  32198. end;
  32199. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32200. begin
  32201. WithTypeInfo:=true;
  32202. StartProgram(false);
  32203. Add('procedure DoIt;');
  32204. Add('type');
  32205. Add(' integer = longint;');
  32206. Add(' TPoint = record');
  32207. Add(' x,y: integer;');
  32208. Add(' end;');
  32209. Add('var p: pointer;');
  32210. Add('begin');
  32211. Add(' p:=typeinfo(tpoint);');
  32212. Add('end;');
  32213. Add('begin');
  32214. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32215. ConvertProgram;
  32216. end;
  32217. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32218. begin
  32219. WithTypeInfo:=true;
  32220. StartProgram(true,[supTypeInfo]);
  32221. Add([
  32222. '{$modeswitch externalclass}',
  32223. 'type',
  32224. ' TFlag = (up,down);',
  32225. ' TFlags = set of TFlag;',
  32226. 'var',
  32227. ' ti: TTypeInfo;',
  32228. ' tiInt: TTypeInfoInteger;',
  32229. ' tiEnum: TTypeInfoEnum;',
  32230. ' tiSet: TTypeInfoSet;',
  32231. 'begin',
  32232. ' ti:=typeinfo(string);',
  32233. ' ti:=typeinfo(boolean);',
  32234. ' ti:=typeinfo(char);',
  32235. ' ti:=typeinfo(double);',
  32236. ' tiInt:=typeinfo(shortint);',
  32237. ' tiInt:=typeinfo(byte);',
  32238. ' tiInt:=typeinfo(smallint);',
  32239. ' tiInt:=typeinfo(word);',
  32240. ' tiInt:=typeinfo(longint);',
  32241. ' tiInt:=typeinfo(longword);',
  32242. ' ti:=typeinfo(jsvalue);',
  32243. ' tiEnum:=typeinfo(tflag);',
  32244. ' tiSet:=typeinfo(tflags);']);
  32245. ConvertProgram;
  32246. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32247. LinesToStr([ // statements
  32248. 'this.TFlag = {',
  32249. ' "0": "up",',
  32250. ' up: 0,',
  32251. ' "1": "down",',
  32252. ' down: 1',
  32253. '};',
  32254. 'this.$rtti.$Enum("TFlag", {',
  32255. ' minvalue: 0,',
  32256. ' maxvalue: 1,',
  32257. ' ordtype: 1,',
  32258. ' enumtype: this.TFlag',
  32259. '});',
  32260. 'this.$rtti.$Set("TFlags", {',
  32261. ' comptype: this.$rtti["TFlag"]',
  32262. '});',
  32263. 'this.ti = null;',
  32264. 'this.tiInt = null;',
  32265. 'this.tiEnum = null;',
  32266. 'this.tiSet = null;',
  32267. '']),
  32268. LinesToStr([ // $mod.$main
  32269. '$mod.ti = rtl.string;',
  32270. '$mod.ti = rtl.boolean;',
  32271. '$mod.ti = rtl.char;',
  32272. '$mod.ti = rtl.double;',
  32273. '$mod.tiInt = rtl.shortint;',
  32274. '$mod.tiInt = rtl.byte;',
  32275. '$mod.tiInt = rtl.smallint;',
  32276. '$mod.tiInt = rtl.word;',
  32277. '$mod.tiInt = rtl.longint;',
  32278. '$mod.tiInt = rtl.longword;',
  32279. '$mod.ti = rtl.jsvalue;',
  32280. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32281. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32282. '']));
  32283. end;
  32284. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32285. begin
  32286. WithTypeInfo:=true;
  32287. StartProgram(true,[supTypeInfo]);
  32288. Add('{$modeswitch externalclass}');
  32289. Add('type');
  32290. Add(' TStaticArr = array[boolean] of string;');
  32291. Add(' TDynArr = array of string;');
  32292. Add(' TProc = procedure;');
  32293. Add(' TMethod = procedure of object;');
  32294. Add('var');
  32295. Add(' StaticArray: TStaticArr;');
  32296. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32297. Add(' DynArray: TDynArr;');
  32298. Add(' tiDynArray: TTypeInfoDynArray;');
  32299. Add(' ProcVar: TProc;');
  32300. Add(' tiProcVar: TTypeInfoProcVar;');
  32301. Add(' MethodVar: TMethod;');
  32302. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32303. Add('begin');
  32304. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32305. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32306. Add(' tiDynArray:=typeinfo(DynArray);');
  32307. Add(' tiDynArray:=typeinfo(TDynArr);');
  32308. Add(' tiProcVar:=typeinfo(ProcVar);');
  32309. Add(' tiProcVar:=typeinfo(TProc);');
  32310. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32311. Add(' tiMethodVar:=typeinfo(TMethod);');
  32312. ConvertProgram;
  32313. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32314. LinesToStr([ // statements
  32315. 'this.$rtti.$StaticArray("TStaticArr", {',
  32316. ' dims: [2],',
  32317. ' eltype: rtl.string',
  32318. '});',
  32319. 'this.$rtti.$DynArray("TDynArr", {',
  32320. ' eltype: rtl.string',
  32321. '});',
  32322. 'this.$rtti.$ProcVar("TProc", {',
  32323. ' procsig: rtl.newTIProcSig([])',
  32324. '});',
  32325. 'this.$rtti.$MethodVar("TMethod", {',
  32326. ' procsig: rtl.newTIProcSig([]),',
  32327. ' methodkind: 0',
  32328. '});',
  32329. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32330. 'this.tiStaticArray = null;',
  32331. 'this.DynArray = [];',
  32332. 'this.tiDynArray = null;',
  32333. 'this.ProcVar = null;',
  32334. 'this.tiProcVar = null;',
  32335. 'this.MethodVar = null;',
  32336. 'this.tiMethodVar = null;',
  32337. '']),
  32338. LinesToStr([ // $mod.$main
  32339. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32340. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32341. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32342. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32343. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32344. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32345. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32346. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32347. '']));
  32348. end;
  32349. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32350. begin
  32351. WithTypeInfo:=true;
  32352. StartProgram(true,[supTypeInfo]);
  32353. Add('{$modeswitch externalclass}');
  32354. Add('type');
  32355. Add(' TRec = record end;');
  32356. // ToDo: ^TRec
  32357. Add(' TObject = class end;');
  32358. Add(' TClass = class of tobject;');
  32359. Add('var');
  32360. Add(' Rec: trec;');
  32361. Add(' tiRecord: ttypeinforecord;');
  32362. Add(' Obj: tobject;');
  32363. Add(' tiClass: ttypeinfoclass;');
  32364. Add(' aClass: tclass;');
  32365. Add(' tiClassRef: ttypeinfoclassref;');
  32366. // ToDo: ^TRec
  32367. Add(' tiPointer: ttypeinfopointer;');
  32368. Add('begin');
  32369. Add(' tirecord:=typeinfo(trec);');
  32370. Add(' tirecord:=typeinfo(trec);');
  32371. Add(' ticlass:=typeinfo(obj);');
  32372. Add(' ticlass:=typeinfo(tobject);');
  32373. Add(' ticlass:=typeinfo(aclass);');
  32374. Add(' ticlassref:=typeinfo(tclass);');
  32375. ConvertProgram;
  32376. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32377. LinesToStr([ // statements
  32378. 'rtl.recNewT(this, "TRec", function () {',
  32379. ' this.$eq = function (b) {',
  32380. ' return true;',
  32381. ' };',
  32382. ' this.$assign = function (s) {',
  32383. ' return this;',
  32384. ' };',
  32385. ' $mod.$rtti.$Record("TRec", {});',
  32386. '});',
  32387. 'rtl.createClass(this, "TObject", null, function () {',
  32388. ' this.$init = function () {',
  32389. ' };',
  32390. ' this.$final = function () {',
  32391. ' };',
  32392. '});',
  32393. 'this.$rtti.$ClassRef("TClass", {',
  32394. ' instancetype: this.$rtti["TObject"]',
  32395. '});',
  32396. 'this.Rec = this.TRec.$new();',
  32397. 'this.tiRecord = null;',
  32398. 'this.Obj = null;',
  32399. 'this.tiClass = null;',
  32400. 'this.aClass = null;',
  32401. 'this.tiClassRef = null;',
  32402. 'this.tiPointer = null;',
  32403. '']),
  32404. LinesToStr([ // $mod.$main
  32405. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32406. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32407. '$mod.tiClass = $mod.Obj.$rtti;',
  32408. '$mod.tiClass = $mod.$rtti["TObject"];',
  32409. '$mod.tiClass = $mod.aClass.$rtti;',
  32410. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32411. '']));
  32412. end;
  32413. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32414. begin
  32415. WithTypeInfo:=true;
  32416. StartProgram(true,[supTypeInfo]);
  32417. Add([
  32418. '{$modeswitch externalclass}',
  32419. 'type',
  32420. ' TClass = class of tobject;',
  32421. ' TObject = class',
  32422. ' function MyClass: TClass;',
  32423. ' class function ClassType: TClass;',
  32424. ' end;',
  32425. 'function TObject.MyClass: TClass;',
  32426. 'var t: TTypeInfoClass;',
  32427. 'begin',
  32428. ' t:=TypeInfo(Self);',
  32429. ' t:=TypeInfo(Result);',
  32430. ' t:=TypeInfo(TObject);',
  32431. 'end;',
  32432. 'class function TObject.ClassType: TClass;',
  32433. 'var t: TTypeInfoClass;',
  32434. 'begin',
  32435. ' t:=TypeInfo(Self);',
  32436. ' t:=TypeInfo(Result);',
  32437. 'end;',
  32438. 'var',
  32439. ' Obj: TObject;',
  32440. ' t: TTypeInfoClass;',
  32441. 'begin',
  32442. ' t:=TypeInfo(TObject.ClassType);',
  32443. ' t:=TypeInfo(Obj.ClassType);',
  32444. ' t:=TypeInfo(Obj.MyClass);',
  32445. '']);
  32446. ConvertProgram;
  32447. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32448. LinesToStr([ // statements
  32449. 'this.$rtti.$Class("TObject");',
  32450. 'this.$rtti.$ClassRef("TClass", {',
  32451. ' instancetype: this.$rtti["TObject"]',
  32452. '});',
  32453. 'rtl.createClass(this, "TObject", null, function () {',
  32454. ' this.$init = function () {',
  32455. ' };',
  32456. ' this.$final = function () {',
  32457. ' };',
  32458. ' this.MyClass = function () {',
  32459. ' var Result = null;',
  32460. ' var t = null;',
  32461. ' t = this.$rtti;',
  32462. ' t = Result.$rtti;',
  32463. ' t = $mod.$rtti["TObject"];',
  32464. ' return Result;',
  32465. ' };',
  32466. ' this.ClassType = function () {',
  32467. ' var Result = null;',
  32468. ' var t = null;',
  32469. ' t = this.$rtti;',
  32470. ' t = Result.$rtti;',
  32471. ' return Result;',
  32472. ' };',
  32473. '});',
  32474. 'this.Obj = null;',
  32475. 'this.t = null;',
  32476. '']),
  32477. LinesToStr([ // $mod.$main
  32478. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32479. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32480. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32481. '']));
  32482. end;
  32483. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32484. begin
  32485. WithTypeInfo:=true;
  32486. AddModuleWithIntfImplSrc('typinfo.pas',
  32487. LinesToStr([
  32488. '{$modeswitch externalclass}',
  32489. 'type',
  32490. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32491. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32492. '']),
  32493. '');
  32494. AddModuleWithIntfImplSrc('unit2.pas',
  32495. LinesToStr([
  32496. 'uses typinfo;',
  32497. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32498. 'procedure DoPtr(p: PTypeInfo);',
  32499. 'procedure DoInfo(t: TTypeInfo);',
  32500. 'procedure DoInt(t: TTypeInfoInteger);',
  32501. '']),
  32502. LinesToStr([
  32503. 'procedure DoPtr(p: PTypeInfo);',
  32504. 'begin end;',
  32505. 'procedure DoInfo(t: TTypeInfo);',
  32506. 'begin end;',
  32507. 'procedure DoInt(t: TTypeInfoInteger);',
  32508. 'begin end;',
  32509. '']));
  32510. StartUnit(true);
  32511. Add([
  32512. 'interface',
  32513. 'uses unit2;', // does not use unit typinfo
  32514. 'implementation',
  32515. 'var',
  32516. ' i: byte;',
  32517. ' p: pointer;',
  32518. ' t: PTypeInfo;',
  32519. 'initialization',
  32520. ' p:=typeinfo(i);',
  32521. ' t:=typeinfo(i);',
  32522. ' if p=t then ;',
  32523. ' if p=typeinfo(i) then ;',
  32524. ' if typeinfo(i)=p then ;',
  32525. ' if t=typeinfo(i) then ;',
  32526. ' if typeinfo(i)=t then ;',
  32527. ' DoPtr(p);',
  32528. ' DoPtr(t);',
  32529. ' DoPtr(typeinfo(i));',
  32530. ' DoInfo(p);',
  32531. ' DoInfo(t);',
  32532. ' DoInfo(typeinfo(i));',
  32533. ' DoInt(typeinfo(i));',
  32534. '']);
  32535. ConvertUnit;
  32536. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32537. LinesToStr([ // statements
  32538. 'var $impl = $mod.$impl;',
  32539. '']),
  32540. LinesToStr([ // this.$init
  32541. '$impl.p = rtl.byte;',
  32542. '$impl.t = rtl.byte;',
  32543. 'if ($impl.p === $impl.t) ;',
  32544. 'if ($impl.p === rtl.byte) ;',
  32545. 'if (rtl.byte === $impl.p) ;',
  32546. 'if ($impl.t === rtl.byte) ;',
  32547. 'if (rtl.byte === $impl.t) ;',
  32548. 'pas.unit2.DoPtr($impl.p);',
  32549. 'pas.unit2.DoPtr($impl.t);',
  32550. 'pas.unit2.DoPtr(rtl.byte);',
  32551. 'pas.unit2.DoInfo($impl.p);',
  32552. 'pas.unit2.DoInfo($impl.t);',
  32553. 'pas.unit2.DoInfo(rtl.byte);',
  32554. 'pas.unit2.DoInt(rtl.byte);',
  32555. '']),
  32556. LinesToStr([ // implementation
  32557. '$impl.i = 0;',
  32558. '$impl.p = null;',
  32559. '$impl.t = null;',
  32560. '']) );
  32561. end;
  32562. procedure TTestModule.TestRTTI_Interface_Corba;
  32563. begin
  32564. WithTypeInfo:=true;
  32565. StartProgram(true,[supTypeInfo]);
  32566. Add([
  32567. '{$interfaces corba}',
  32568. '{$modeswitch externalclass}',
  32569. 'type',
  32570. ' IUnknown = interface',
  32571. ' end;',
  32572. ' IBird = interface',
  32573. ' function GetItem: longint;',
  32574. ' procedure SetItem(Value: longint);',
  32575. ' property Item: longint read GetItem write SetItem;',
  32576. ' end;',
  32577. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32578. 'var',
  32579. ' i: IBird;',
  32580. ' t: TTypeInfoInterface;',
  32581. 'begin',
  32582. ' t:=TypeInfo(IBird);',
  32583. ' t:=TypeInfo(i);',
  32584. ' DoIt(t);',
  32585. ' DoIt(TypeInfo(IBird));',
  32586. '']);
  32587. ConvertProgram;
  32588. CheckSource('TestRTTI_Interface_Corba',
  32589. LinesToStr([ // statements
  32590. 'rtl.createInterface(',
  32591. ' this,',
  32592. ' "IUnknown",',
  32593. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32594. ' [],',
  32595. ' null,',
  32596. ' function () {',
  32597. ' }',
  32598. ');',
  32599. 'rtl.createInterface(',
  32600. ' this,',
  32601. ' "IBird",',
  32602. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32603. ' ["GetItem", "SetItem"],',
  32604. ' null,',
  32605. ' function () {',
  32606. ' var $r = this.$rtti;',
  32607. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32608. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32609. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32610. ' }',
  32611. ');',
  32612. 'this.DoIt = function (t) {',
  32613. '}; ',
  32614. 'this.i = null;',
  32615. 'this.t = null;',
  32616. '']),
  32617. LinesToStr([ // $mod.$main
  32618. '$mod.t = $mod.$rtti["IBird"];',
  32619. '$mod.t = $mod.i.$rtti;',
  32620. '$mod.DoIt($mod.t);',
  32621. '$mod.DoIt($mod.$rtti["IBird"]);',
  32622. '']));
  32623. end;
  32624. procedure TTestModule.TestRTTI_Interface_COM;
  32625. begin
  32626. WithTypeInfo:=true;
  32627. StartProgram(true,[supTypeInfo]);
  32628. Add([
  32629. '{$interfaces com}',
  32630. '{$modeswitch externalclass}',
  32631. 'type',
  32632. ' TGuid = record end;',
  32633. ' integer = longint;',
  32634. ' IUnknown = interface',
  32635. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32636. ' function _AddRef: Integer;',
  32637. ' function _Release: Integer;',
  32638. ' end;',
  32639. ' IBird = interface',
  32640. ' function GetItem: longint;',
  32641. ' procedure SetItem(Value: longint);',
  32642. ' property Item: longint read GetItem write SetItem;',
  32643. ' end;',
  32644. 'var',
  32645. ' i: IBird;',
  32646. ' t: TTypeInfoInterface;',
  32647. 'begin',
  32648. ' t:=TypeInfo(IBird);',
  32649. ' t:=TypeInfo(i);',
  32650. '']);
  32651. ConvertProgram;
  32652. CheckSource('TestRTTI_Interface_COM',
  32653. LinesToStr([ // statements
  32654. 'rtl.recNewT(this, "TGuid", function () {',
  32655. ' this.$eq = function (b) {',
  32656. ' return true;',
  32657. ' };',
  32658. ' this.$assign = function (s) {',
  32659. ' return this;',
  32660. ' };',
  32661. ' $mod.$rtti.$Record("TGuid", {});',
  32662. '});',
  32663. 'rtl.createInterface(',
  32664. ' this,',
  32665. ' "IUnknown",',
  32666. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32667. ' ["QueryInterface", "_AddRef", "_Release"],',
  32668. ' null,',
  32669. ' function () {',
  32670. ' this.$kind = "com";',
  32671. ' var $r = this.$rtti;',
  32672. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32673. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32674. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32675. ' }',
  32676. ');',
  32677. 'rtl.createInterface(',
  32678. ' this,',
  32679. ' "IBird",',
  32680. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32681. ' ["GetItem", "SetItem"],',
  32682. ' this.IUnknown,',
  32683. ' function () {',
  32684. ' var $r = this.$rtti;',
  32685. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32686. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32687. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32688. ' }',
  32689. ');',
  32690. 'this.i = null;',
  32691. 'this.t = null;',
  32692. '']),
  32693. LinesToStr([ // $mod.$main
  32694. '$mod.t = $mod.$rtti["IBird"];',
  32695. '$mod.t = $mod.i.$rtti;',
  32696. '']));
  32697. end;
  32698. procedure TTestModule.TestRTTI_ClassHelper;
  32699. begin
  32700. WithTypeInfo:=true;
  32701. StartProgram(true,[supTypeInfo]);
  32702. Add([
  32703. '{$interfaces com}',
  32704. '{$modeswitch externalclass}',
  32705. 'type',
  32706. ' TObject = class',
  32707. ' end;',
  32708. ' THelper = class helper for TObject',
  32709. ' published',
  32710. ' function GetItem: longint;',
  32711. ' property Item: longint read GetItem;',
  32712. ' end;',
  32713. 'function THelper.GetItem: longint;',
  32714. 'begin',
  32715. 'end;',
  32716. 'var',
  32717. ' t: TTypeInfoHelper;',
  32718. 'begin',
  32719. ' t:=TypeInfo(THelper);',
  32720. '']);
  32721. ConvertProgram;
  32722. CheckSource('TestRTTI_ClassHelper',
  32723. LinesToStr([ // statements
  32724. 'rtl.createClass(this, "TObject", null, function () {',
  32725. ' this.$init = function () {',
  32726. ' };',
  32727. ' this.$final = function () {',
  32728. ' };',
  32729. '});',
  32730. 'rtl.createHelper(this, "THelper", null, function () {',
  32731. ' this.GetItem = function () {',
  32732. ' var Result = 0;',
  32733. ' return Result;',
  32734. ' };',
  32735. ' var $r = this.$rtti;',
  32736. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32737. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32738. '});',
  32739. 'this.t = null;',
  32740. '']),
  32741. LinesToStr([ // $mod.$main
  32742. '$mod.t = $mod.$rtti["THelper"];',
  32743. '']));
  32744. end;
  32745. procedure TTestModule.TestRTTI_ExternalClass;
  32746. begin
  32747. WithTypeInfo:=true;
  32748. StartProgram(true,[supTypeInfo]);
  32749. Add([
  32750. '{$modeswitch externalclass}',
  32751. 'type',
  32752. ' TJSObject = class external name ''Object''',
  32753. ' end;',
  32754. ' TJSArray = class external name ''Array'' (TJSObject)',
  32755. ' end;',
  32756. 'var',
  32757. ' p: Pointer;',
  32758. ' tc: TTypeInfoExtClass;',
  32759. 'begin',
  32760. ' p:=typeinfo(TJSArray);']);
  32761. ConvertProgram;
  32762. CheckSource('TestRTTI_ExternalClass',
  32763. LinesToStr([ // statements
  32764. 'this.$rtti.$ExtClass("TJSObject", {',
  32765. ' jsclass: "Object"',
  32766. '});',
  32767. 'this.$rtti.$ExtClass("TJSArray", {',
  32768. ' ancestor: this.$rtti["TJSObject"],',
  32769. ' jsclass: "Array"',
  32770. '});',
  32771. 'this.p = null;',
  32772. 'this.tc = null;',
  32773. '']),
  32774. LinesToStr([ // $mod.$main
  32775. '$mod.p = $mod.$rtti["TJSArray"];',
  32776. '']));
  32777. end;
  32778. procedure TTestModule.TestRTTI_Unit;
  32779. begin
  32780. WithTypeInfo:=true;
  32781. AddModuleWithIntfImplSrc('unit2.pas',
  32782. LinesToStr([
  32783. '{$mode delphi}',
  32784. 'type',
  32785. ' TWordArray = array of word;',
  32786. ' TArray<T> = array of T;',
  32787. '']),
  32788. '');
  32789. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32790. Add([
  32791. '{$mode delphi}',
  32792. 'interface',
  32793. 'uses unit2;',
  32794. 'type',
  32795. ' IBird = interface',
  32796. ' function Swoop: TWordArray;',
  32797. ' function Glide: TArray<word>;',
  32798. ' end;',
  32799. 'procedure Fly;',
  32800. 'implementation',
  32801. 'procedure Fly;',
  32802. 'var',
  32803. ' ta: tTypeInfoDynArray;',
  32804. ' ti: tTypeInfoInterface;',
  32805. 'begin',
  32806. ' ta:=typeinfo(TWordArray);',
  32807. ' ta:=typeinfo(TArray<word>);',
  32808. ' ti:=typeinfo(IBird);',
  32809. 'end;',
  32810. '']);
  32811. ConvertUnit;
  32812. CheckSource('TestRTTI_ExternalClass',
  32813. LinesToStr([ // statements
  32814. 'rtl.createInterface(',
  32815. ' this,',
  32816. ' "IBird",',
  32817. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32818. ' ["Swoop", "Glide"],',
  32819. ' pas.system.IUnknown,',
  32820. ' function () {',
  32821. ' var $r = this.$rtti;',
  32822. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32823. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32824. ' }',
  32825. ');',
  32826. 'this.Fly = function () {',
  32827. ' var ta = null;',
  32828. ' var ti = null;',
  32829. ' ta = pas.unit2.$rtti["TWordArray"];',
  32830. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32831. ' ti = $mod.$rtti["IBird"];',
  32832. '};',
  32833. '']),
  32834. LinesToStr([ // $mod.$main
  32835. '']));
  32836. end;
  32837. procedure TTestModule.TestResourcestringProgram;
  32838. begin
  32839. AddModuleWithIntfImplSrc('unit2.pas',
  32840. LinesToStr([
  32841. 'resourcestring Title = ''Nice'';',
  32842. '']),
  32843. '');
  32844. StartProgram(true);
  32845. Add([
  32846. 'uses unit2;',
  32847. 'const Bar = ''bar'';',
  32848. 'resourcestring',
  32849. ' Red = ''red'';',
  32850. ' Foobar = ''fOo''+bar;',
  32851. 'var s: string;',
  32852. ' c: char;',
  32853. 'begin',
  32854. ' s:=red;',
  32855. ' s:=test1.red;',
  32856. ' s:=Title;',
  32857. ' c:=red[1];',
  32858. ' c:=test1.red[2];',
  32859. ' if red=foobar then ;',
  32860. ' if red[3]=red[4] then ;']);
  32861. ConvertProgram;
  32862. CheckSource('TestResourcestringProgram',
  32863. LinesToStr([ // statements
  32864. 'this.Bar = "bar";',
  32865. 'this.s = "";',
  32866. 'this.c = "\x00";',
  32867. '$mod.$resourcestrings = {',
  32868. ' Red: {',
  32869. ' org: "red"',
  32870. ' },',
  32871. ' Foobar: {',
  32872. ' org: "fOobar"',
  32873. ' }',
  32874. '};',
  32875. '']),
  32876. LinesToStr([ // $mod.$main
  32877. '$mod.s = rtl.getResStr($mod, "Red");',
  32878. '$mod.s = rtl.getResStr($mod, "Red");',
  32879. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32880. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32881. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32882. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32883. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32884. '']));
  32885. end;
  32886. procedure TTestModule.TestResourcestringUnit;
  32887. begin
  32888. AddModuleWithIntfImplSrc('unit2.pas',
  32889. LinesToStr([
  32890. 'resourcestring Title = ''Nice'';',
  32891. '']),
  32892. '');
  32893. StartUnit(true);
  32894. Add([
  32895. 'interface',
  32896. 'uses unit2;',
  32897. 'const Red = ''rEd'';',
  32898. 'resourcestring',
  32899. ' Blue = ''blue'';',
  32900. ' NotRed = ''not''+Red;',
  32901. 'var s: string;',
  32902. 'implementation',
  32903. 'resourcestring',
  32904. ' ImplGreen = ''green'';',
  32905. 'initialization',
  32906. ' s:=blue+ImplGreen;',
  32907. ' s:=test1.blue+test1.implgreen;',
  32908. ' s:=blue[1]+implgreen[2];',
  32909. ' s:=Title;',
  32910. '']);
  32911. ConvertUnit;
  32912. CheckSource('TestResourcestringUnit',
  32913. LinesToStr([ // statements
  32914. 'this.Red = "rEd";',
  32915. 'this.s = "";',
  32916. '$mod.$resourcestrings = {',
  32917. ' Blue: {',
  32918. ' org: "blue"',
  32919. ' },',
  32920. ' NotRed: {',
  32921. ' org: "notrEd"',
  32922. ' },',
  32923. ' ImplGreen: {',
  32924. ' org: "green"',
  32925. ' }',
  32926. '};',
  32927. '']),
  32928. LinesToStr([ // $mod.$main
  32929. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32930. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32931. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32932. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32933. '']));
  32934. end;
  32935. procedure TTestModule.TestResourcestringImplementation;
  32936. begin
  32937. StartUnit(false);
  32938. Add([
  32939. 'interface',
  32940. 'implementation',
  32941. 'resourcestring',
  32942. ' ImplRed = ''red'';']);
  32943. ConvertUnit;
  32944. CheckSource('TestResourcestringImplementation',
  32945. LinesToStr([ // intf statements
  32946. 'var $impl = $mod.$impl;']),
  32947. LinesToStr([ // $mod.$init
  32948. '']),
  32949. LinesToStr([ // impl statements
  32950. '$mod.$resourcestrings = {',
  32951. ' ImplRed: {',
  32952. ' org: "red"',
  32953. ' }',
  32954. '};',
  32955. '']));
  32956. end;
  32957. procedure TTestModule.TestAttributes_Members;
  32958. begin
  32959. WithTypeInfo:=true;
  32960. StartProgram(false);
  32961. Add([
  32962. '{$modeswitch PrefixedAttributes}',
  32963. 'type',
  32964. ' TObject = class',
  32965. ' constructor Create;',
  32966. ' end;',
  32967. ' TCustomAttribute = class',
  32968. ' constructor Create(Id: word);',
  32969. ' end;',
  32970. ' [Missing]',
  32971. ' TBird = class',
  32972. ' published',
  32973. ' [Tcustom]',
  32974. ' FField: word;',
  32975. ' [tcustom(14)]',
  32976. ' property Size: word read FField;',
  32977. ' [Tcustom(15)]',
  32978. ' procedure Fly; virtual; abstract;',
  32979. ' end;',
  32980. ' TRec = record',
  32981. ' [Tcustom,tcustom(14)]',
  32982. ' Size: word;',
  32983. ' end;',
  32984. 'constructor TObject.Create; begin end;',
  32985. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32986. 'begin',
  32987. '']);
  32988. ConvertProgram;
  32989. CheckSource('TestAttributes_Members',
  32990. LinesToStr([ // statements
  32991. 'rtl.createClass(this, "TObject", null, function () {',
  32992. ' this.$init = function () {',
  32993. ' };',
  32994. ' this.$final = function () {',
  32995. ' };',
  32996. ' this.Create = function () {',
  32997. ' return this;',
  32998. ' };',
  32999. '});',
  33000. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33001. ' this.Create$1 = function (Id) {',
  33002. ' return this;',
  33003. ' };',
  33004. '});',
  33005. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33006. ' this.$init = function () {',
  33007. ' $mod.TObject.$init.call(this);',
  33008. ' this.FField = 0;',
  33009. ' };',
  33010. ' var $r = this.$rtti;',
  33011. ' $r.addField("FField", rtl.word, {',
  33012. ' attr: [$mod.TCustomAttribute, "Create"]',
  33013. ' });',
  33014. ' $r.addProperty(',
  33015. ' "Size",',
  33016. ' 0,',
  33017. ' rtl.word,',
  33018. ' "FField",',
  33019. ' "",',
  33020. ' {',
  33021. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  33022. ' }',
  33023. ' );',
  33024. ' $r.addMethod("Fly", 0, [], null, 0, {',
  33025. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  33026. ' });',
  33027. '});',
  33028. 'rtl.recNewT(this, "TRec", function () {',
  33029. ' this.Size = 0;',
  33030. ' this.$eq = function (b) {',
  33031. ' return this.Size === b.Size;',
  33032. ' };',
  33033. ' this.$assign = function (s) {',
  33034. ' this.Size = s.Size;',
  33035. ' return this;',
  33036. ' };',
  33037. ' var $r = $mod.$rtti.$Record("TRec", {});',
  33038. ' $r.addField("Size", rtl.word, {',
  33039. ' attr: [',
  33040. ' $mod.TCustomAttribute,',
  33041. ' "Create",',
  33042. ' $mod.TCustomAttribute,',
  33043. ' "Create$1",',
  33044. ' [14]',
  33045. ' ]',
  33046. ' });',
  33047. '});',
  33048. '']),
  33049. LinesToStr([ // $mod.$main
  33050. '']));
  33051. end;
  33052. procedure TTestModule.TestAttributes_Types;
  33053. begin
  33054. WithTypeInfo:=true;
  33055. StartProgram(false);
  33056. Add([
  33057. '{$modeswitch PrefixedAttributes}',
  33058. 'type',
  33059. ' TObject = class',
  33060. ' constructor Create(Id: word);',
  33061. ' end;',
  33062. ' TCustomAttribute = class',
  33063. ' end;',
  33064. ' [TCustom(1)]',
  33065. ' TMyClass = class',
  33066. ' end;',
  33067. ' [TCustom(11)]',
  33068. ' TMyDescendant = class(TMyClass)',
  33069. ' end;',
  33070. ' [TCustom(2)]',
  33071. ' TRec = record',
  33072. ' end;',
  33073. ' [TCustom(3)]',
  33074. ' TInt = type word;',
  33075. 'constructor TObject.Create(Id: word);',
  33076. 'begin',
  33077. 'end;',
  33078. 'var p: pointer;',
  33079. 'begin',
  33080. ' p:=typeinfo(TMyClass);',
  33081. ' p:=typeinfo(TRec);',
  33082. ' p:=typeinfo(TInt);',
  33083. '']);
  33084. ConvertProgram;
  33085. CheckSource('TestAttributes_Types',
  33086. LinesToStr([ // statements
  33087. 'rtl.createClass(this, "TObject", null, function () {',
  33088. ' this.$init = function () {',
  33089. ' };',
  33090. ' this.$final = function () {',
  33091. ' };',
  33092. ' this.Create = function (Id) {',
  33093. ' return this;',
  33094. ' };',
  33095. '});',
  33096. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33097. '});',
  33098. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33099. ' var $r = this.$rtti;',
  33100. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33101. '});',
  33102. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33103. ' var $r = this.$rtti;',
  33104. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33105. '});',
  33106. 'rtl.recNewT(this, "TRec", function () {',
  33107. ' this.$eq = function (b) {',
  33108. ' return true;',
  33109. ' };',
  33110. ' this.$assign = function (s) {',
  33111. ' return this;',
  33112. ' };',
  33113. ' $mod.$rtti.$Record("TRec", {',
  33114. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33115. ' });',
  33116. '});',
  33117. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33118. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33119. '});',
  33120. 'this.p = null;',
  33121. '']),
  33122. LinesToStr([ // $mod.$main
  33123. '$mod.p = $mod.$rtti["TMyClass"];',
  33124. '$mod.p = $mod.$rtti["TRec"];',
  33125. '$mod.p = $mod.$rtti["TInt"];',
  33126. '']));
  33127. end;
  33128. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33129. begin
  33130. WithTypeInfo:=true;
  33131. StartProgram(false);
  33132. Add([
  33133. '{$modeswitch PrefixedAttributes}',
  33134. 'type',
  33135. ' TObject = class',
  33136. ' constructor Create;',
  33137. ' end;',
  33138. ' TCustomAttribute = class',
  33139. ' end;',
  33140. ' THelper = class helper for TCustomAttribute',
  33141. ' constructor Create(Id: word);',
  33142. ' end;',
  33143. ' [TCustom(3)]',
  33144. ' TMyInt = word;',
  33145. 'constructor TObject.Create; begin end;',
  33146. 'constructor THelper.Create(Id: word); begin end;',
  33147. 'begin',
  33148. ' if typeinfo(TMyInt)=nil then ;']);
  33149. ConvertProgram;
  33150. end;
  33151. procedure TTestModule.TestAttributes_InterfacesList;
  33152. begin
  33153. WithTypeInfo:=true;
  33154. StartProgram(false);
  33155. Add([
  33156. '{$mode Delphi}',
  33157. 'type',
  33158. ' TObject = class',
  33159. ' constructor Create;',
  33160. ' end;',
  33161. ' IInterface = interface end;',
  33162. ' TCustomAttribute = class',
  33163. ' end;',
  33164. ' Red = class(TCustomAttribute);',
  33165. ' Blue = class(TCustomAttribute);',
  33166. ' [Red]',
  33167. ' IBird<T> = interface',
  33168. ' procedure Fly;',
  33169. ' end;',
  33170. ' [Blue]',
  33171. ' IEagle = interface(IBird<Word>)',
  33172. ' procedure Dive;',
  33173. ' end;',
  33174. ' TAnt = class(TObject, IEagle)',
  33175. ' procedure Fly; virtual; abstract;',
  33176. ' procedure Dive; virtual; abstract;',
  33177. ' end;',
  33178. 'constructor TObject.Create;',
  33179. 'begin',
  33180. 'end;',
  33181. 'begin',
  33182. '']);
  33183. ConvertProgram;
  33184. CheckSource('TestAttributes_InterfacesList',
  33185. LinesToStr([ // statements
  33186. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33187. 'rtl.createClass(this, "TObject", null, function () {',
  33188. ' this.$init = function () {',
  33189. ' };',
  33190. ' this.$final = function () {',
  33191. ' };',
  33192. ' this.Create = function () {',
  33193. ' return this;',
  33194. ' };',
  33195. '});',
  33196. 'rtl.createInterface(',
  33197. ' this,',
  33198. ' "IInterface",',
  33199. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33200. ' [],',
  33201. ' null,',
  33202. ' function () {',
  33203. ' this.$kind = "com";',
  33204. ' }',
  33205. ');',
  33206. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33207. '});',
  33208. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33209. '});',
  33210. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33211. '});',
  33212. 'rtl.createInterface(',
  33213. ' this,',
  33214. ' "IBird$G1",',
  33215. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33216. ' ["Fly"],',
  33217. ' this.IInterface,',
  33218. ' function () {',
  33219. ' var $r = this.$rtti;',
  33220. ' $r.addMethod("Fly", 0, []);',
  33221. ' $r.attr = [$mod.Red, "Create"];',
  33222. ' },',
  33223. ' "IBird<System.Word>"',
  33224. ');',
  33225. 'rtl.createInterface(',
  33226. ' this,',
  33227. ' "IEagle",',
  33228. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33229. ' ["Dive"],',
  33230. ' this.IBird$G1,',
  33231. ' function () {',
  33232. ' var $r = this.$rtti;',
  33233. ' $r.addMethod("Dive", 0, []);',
  33234. ' $r.attr = [$mod.Blue, "Create"];',
  33235. ' }',
  33236. ');',
  33237. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33238. ' rtl.addIntf(this, $mod.IEagle);',
  33239. '});',
  33240. '']),
  33241. LinesToStr([ // $mod.$main
  33242. '']));
  33243. end;
  33244. procedure TTestModule.TestAssert;
  33245. begin
  33246. StartProgram(false);
  33247. Add([
  33248. 'procedure DoIt;',
  33249. 'var',
  33250. ' b: boolean;',
  33251. ' s: string;',
  33252. 'begin',
  33253. ' {$Assertions on}',
  33254. ' Assert(b);',
  33255. 'end;',
  33256. 'begin',
  33257. ' DoIt;',
  33258. '']);
  33259. ConvertProgram;
  33260. CheckSource('TestAssert',
  33261. LinesToStr([ // statements
  33262. 'this.DoIt = function () {',
  33263. ' var b = false;',
  33264. ' var s = "";',
  33265. ' if (!b) throw "assert failed";',
  33266. '};',
  33267. '']),
  33268. LinesToStr([ // $mod.$main
  33269. '$mod.DoIt();',
  33270. '']));
  33271. end;
  33272. procedure TTestModule.TestAssert_SysUtils;
  33273. begin
  33274. AddModuleWithIntfImplSrc('SysUtils.pas',
  33275. LinesToStr([
  33276. 'type',
  33277. ' TObject = class',
  33278. ' constructor Create;',
  33279. ' end;',
  33280. ' EAssertionFailed = class',
  33281. ' constructor Create(s: string);',
  33282. ' end;',
  33283. '']),
  33284. LinesToStr([
  33285. 'constructor TObject.Create;',
  33286. 'begin end;',
  33287. 'constructor EAssertionFailed.Create(s: string);',
  33288. 'begin end;',
  33289. '']) );
  33290. StartProgram(true);
  33291. Add([
  33292. 'uses sysutils;',
  33293. 'procedure DoIt;',
  33294. 'var',
  33295. ' b: boolean;',
  33296. ' s: string;',
  33297. 'begin',
  33298. ' {$Assertions on}',
  33299. ' Assert(b);',
  33300. ' Assert(b,''msg'');',
  33301. 'end;',
  33302. 'begin',
  33303. ' DoIt;',
  33304. '']);
  33305. ConvertProgram;
  33306. CheckSource('TestAssert_SysUtils',
  33307. LinesToStr([ // statements
  33308. 'this.DoIt = function () {',
  33309. ' var b = false;',
  33310. ' var s = "";',
  33311. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33312. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33313. '};',
  33314. '']),
  33315. LinesToStr([ // $mod.$main
  33316. '$mod.DoIt();',
  33317. '']));
  33318. end;
  33319. procedure TTestModule.TestObjectChecks;
  33320. begin
  33321. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33322. StartProgram(false);
  33323. Add([
  33324. 'type',
  33325. ' TObject = class',
  33326. ' procedure DoIt;',
  33327. ' end;',
  33328. ' TClass = class of tobject;',
  33329. ' TBird = class',
  33330. ' end;',
  33331. ' TBirdClass = class of TBird;',
  33332. 'var',
  33333. ' o : TObject;',
  33334. ' c: TClass;',
  33335. ' b: TBird;',
  33336. ' bc: TBirdClass;',
  33337. 'procedure TObject.DoIt;',
  33338. 'begin',
  33339. ' b:=TBird(o);',
  33340. 'end;',
  33341. 'begin',
  33342. ' o.DoIt;',
  33343. ' b:=TBird(o);',
  33344. ' bc:=TBirdClass(c);',
  33345. '']);
  33346. ConvertProgram;
  33347. CheckSource('TestCheckMethodCall',
  33348. LinesToStr([ // statements
  33349. 'rtl.createClass(this, "TObject", null, function () {',
  33350. ' this.$init = function () {',
  33351. ' };',
  33352. ' this.$final = function () {',
  33353. ' };',
  33354. ' this.DoIt = function () {',
  33355. ' rtl.checkMethodCall(this,$mod.TObject);',
  33356. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33357. ' };',
  33358. '});',
  33359. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33360. '});',
  33361. 'this.o = null;',
  33362. 'this.c = null;',
  33363. 'this.b = null;',
  33364. 'this.bc = null;',
  33365. '']),
  33366. LinesToStr([ // $mod.$main
  33367. '$mod.o.DoIt();',
  33368. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33369. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33370. '']));
  33371. end;
  33372. procedure TTestModule.TestOverflowChecks_Int;
  33373. begin
  33374. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33375. StartProgram(false);
  33376. Add([
  33377. 'procedure DoIt;',
  33378. 'var',
  33379. ' b: byte;',
  33380. ' n: nativeint;',
  33381. ' u: nativeuint;',
  33382. ' c: currency;',
  33383. 'begin',
  33384. ' n:=n+n;',
  33385. ' n:=n-n;',
  33386. ' n:=n+b;',
  33387. ' n:=b-n;',
  33388. ' n:=n*n;',
  33389. ' n:=n*u;',
  33390. ' c:=c+b;',
  33391. ' c:=b+c;',
  33392. ' c:=c*b;',
  33393. ' c:=b*c;',
  33394. 'end;',
  33395. 'begin',
  33396. '']);
  33397. ConvertProgram;
  33398. CheckSource('TestOverflowChecks_Int',
  33399. LinesToStr([ // statements
  33400. 'this.DoIt = function () {',
  33401. ' var b = 0;',
  33402. ' var n = 0;',
  33403. ' var u = 0;',
  33404. ' var c = 0;',
  33405. ' n = rtl.oc(n + n);',
  33406. ' n = rtl.oc(n - n);',
  33407. ' n = rtl.oc(n + b);',
  33408. ' n = rtl.oc(b - n);',
  33409. ' n = rtl.oc(n * n);',
  33410. ' n = rtl.oc(n * u);',
  33411. ' c = rtl.oc(c + (b * 10000));',
  33412. ' c = rtl.oc((b * 10000) + c);',
  33413. ' c = rtl.oc(c * b);',
  33414. ' c = rtl.oc(b * c);',
  33415. '};',
  33416. '']),
  33417. LinesToStr([ // $mod.$main
  33418. '']));
  33419. end;
  33420. procedure TTestModule.TestRangeChecks_AssignInt;
  33421. begin
  33422. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33423. StartProgram(false);
  33424. Add([
  33425. '{$R+}',
  33426. 'var',
  33427. ' b: byte = 2;',
  33428. ' w: word = 3;',
  33429. 'procedure DoIt(p: byte);',
  33430. 'begin',
  33431. ' b:=w;',
  33432. ' b+=w;',
  33433. ' b:=1;',
  33434. 'end;',
  33435. '{$R-}',
  33436. 'procedure DoSome;',
  33437. 'begin',
  33438. ' DoIt(w);',
  33439. ' b:=w;',
  33440. ' b:=2;',
  33441. 'end;',
  33442. 'begin',
  33443. '{$R+}',
  33444. '']);
  33445. ConvertProgram;
  33446. CheckSource('TestRangeChecks_AssignInt',
  33447. LinesToStr([ // statements
  33448. 'this.b = 2;',
  33449. 'this.w = 3;',
  33450. 'this.DoIt = function (p) {',
  33451. ' rtl.rc(p, 0, 255);',
  33452. ' $mod.b = rtl.rc($mod.w,0,255);',
  33453. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33454. ' $mod.b = 1;',
  33455. '};',
  33456. 'this.DoSome = function () {',
  33457. ' $mod.DoIt($mod.w);',
  33458. ' $mod.b = $mod.w;',
  33459. ' $mod.b = 2;',
  33460. '};',
  33461. '']),
  33462. LinesToStr([ // $mod.$main
  33463. '']));
  33464. end;
  33465. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33466. begin
  33467. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33468. StartProgram(false);
  33469. Add([
  33470. '{$R+}',
  33471. 'type Ten = 1..10;',
  33472. 'var',
  33473. ' b: Ten = 2;',
  33474. ' w: Ten = 3;',
  33475. 'procedure DoIt(p: Ten);',
  33476. 'begin',
  33477. ' b:=w;',
  33478. ' b+=w;',
  33479. ' b:=1;',
  33480. 'end;',
  33481. '{$R-}',
  33482. 'procedure DoSome;',
  33483. 'begin',
  33484. ' DoIt(w);',
  33485. ' b:=w;',
  33486. ' b:=2;',
  33487. 'end;',
  33488. 'begin',
  33489. '{$R+}',
  33490. '']);
  33491. ConvertProgram;
  33492. CheckSource('TestRangeChecks_AssignIntRange',
  33493. LinesToStr([ // statements
  33494. 'this.b = 2;',
  33495. 'this.w = 3;',
  33496. 'this.DoIt = function (p) {',
  33497. ' rtl.rc(p, 1, 10);',
  33498. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33499. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33500. ' $mod.b = 1;',
  33501. '};',
  33502. 'this.DoSome = function () {',
  33503. ' $mod.DoIt($mod.w);',
  33504. ' $mod.b = $mod.w;',
  33505. ' $mod.b = 2;',
  33506. '};',
  33507. '']),
  33508. LinesToStr([ // $mod.$main
  33509. '']));
  33510. end;
  33511. procedure TTestModule.TestRangeChecks_AssignEnum;
  33512. begin
  33513. StartProgram(false);
  33514. Add([
  33515. '{$R+}',
  33516. 'type TEnum = (red,green);',
  33517. 'var',
  33518. ' e: TEnum = red;',
  33519. 'procedure DoIt(p: TEnum);',
  33520. 'begin',
  33521. ' e:=p;',
  33522. ' p:=TEnum(0);',
  33523. ' p:=succ(e);',
  33524. 'end;',
  33525. '{$R-}',
  33526. 'procedure DoSome;',
  33527. 'begin',
  33528. ' DoIt(e);',
  33529. ' e:=TEnum(1);',
  33530. ' e:=pred(e);',
  33531. 'end;',
  33532. 'begin',
  33533. '{$R+}',
  33534. '']);
  33535. ConvertProgram;
  33536. CheckSource('TestRangeChecks_AssignEnum',
  33537. LinesToStr([ // statements
  33538. 'this.TEnum = {',
  33539. ' "0": "red",',
  33540. ' red: 0,',
  33541. ' "1": "green",',
  33542. ' green: 1',
  33543. '};',
  33544. 'this.e = this.TEnum.red;',
  33545. 'this.DoIt = function (p) {',
  33546. ' rtl.rc(p, 0, 1);',
  33547. ' $mod.e = rtl.rc(p, 0, 1);',
  33548. ' p = 0;',
  33549. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33550. '};',
  33551. 'this.DoSome = function () {',
  33552. ' $mod.DoIt($mod.e);',
  33553. ' $mod.e = 1;',
  33554. ' $mod.e = $mod.e - 1;',
  33555. '};',
  33556. '']),
  33557. LinesToStr([ // $mod.$main
  33558. '']));
  33559. end;
  33560. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33561. begin
  33562. StartProgram(false);
  33563. Add([
  33564. '{$R+}',
  33565. 'type',
  33566. ' TEnum = (red,green);',
  33567. ' TEnumRg = red..green;',
  33568. 'var',
  33569. ' e: TEnumRg = red;',
  33570. 'procedure DoIt(p: TEnumRg);',
  33571. 'begin',
  33572. ' e:=p;',
  33573. ' p:=TEnumRg(0);',
  33574. ' p:=succ(e);',
  33575. 'end;',
  33576. '{$R-}',
  33577. 'procedure DoSome;',
  33578. 'begin',
  33579. ' DoIt(e);',
  33580. ' e:=TEnum(1);',
  33581. ' e:=pred(e);',
  33582. 'end;',
  33583. 'begin',
  33584. '{$R+}',
  33585. '']);
  33586. ConvertProgram;
  33587. CheckSource('TestRangeChecks_AssignEnumRange',
  33588. LinesToStr([ // statements
  33589. 'this.TEnum = {',
  33590. ' "0": "red",',
  33591. ' red: 0,',
  33592. ' "1": "green",',
  33593. ' green: 1',
  33594. '};',
  33595. 'this.e = this.TEnum.red;',
  33596. 'this.DoIt = function (p) {',
  33597. ' rtl.rc(p, 0, 1);',
  33598. ' $mod.e = rtl.rc(p, 0, 1);',
  33599. ' p = 0;',
  33600. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33601. '};',
  33602. 'this.DoSome = function () {',
  33603. ' $mod.DoIt($mod.e);',
  33604. ' $mod.e = 1;',
  33605. ' $mod.e = $mod.e - 1;',
  33606. '};',
  33607. '']),
  33608. LinesToStr([ // $mod.$main
  33609. '']));
  33610. end;
  33611. procedure TTestModule.TestRangeChecks_AssignChar;
  33612. begin
  33613. StartProgram(false);
  33614. Add([
  33615. '{$R+}',
  33616. 'type',
  33617. ' TLetter = char;',
  33618. 'var',
  33619. ' b: TLetter = ''2'';',
  33620. ' w: TLetter = ''3'';',
  33621. 'procedure DoIt(p: TLetter);',
  33622. 'begin',
  33623. ' b:=w;',
  33624. ' b:=''1'';',
  33625. 'end;',
  33626. '{$R-}',
  33627. 'procedure DoSome;',
  33628. 'begin',
  33629. ' DoIt(w);',
  33630. ' b:=w;',
  33631. ' b:=''2'';',
  33632. 'end;',
  33633. 'begin',
  33634. '{$R+}',
  33635. '']);
  33636. ConvertProgram;
  33637. CheckSource('TestRangeChecks_AssignChar',
  33638. LinesToStr([ // statements
  33639. 'this.b = "2";',
  33640. 'this.w = "3";',
  33641. 'this.DoIt = function (p) {',
  33642. ' rtl.rcc(p, 0, 65535);',
  33643. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33644. ' $mod.b = "1";',
  33645. '};',
  33646. 'this.DoSome = function () {',
  33647. ' $mod.DoIt($mod.w);',
  33648. ' $mod.b = $mod.w;',
  33649. ' $mod.b = "2";',
  33650. '};',
  33651. '']),
  33652. LinesToStr([ // $mod.$main
  33653. '']));
  33654. end;
  33655. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33656. begin
  33657. StartProgram(false);
  33658. Add([
  33659. '{$R+}',
  33660. 'type TDigit = ''0''..''9'';',
  33661. 'var',
  33662. ' b: TDigit = ''2'';',
  33663. ' w: TDigit = ''3'';',
  33664. 'procedure DoIt(p: TDigit);',
  33665. 'begin',
  33666. ' b:=w;',
  33667. ' b:=''1'';',
  33668. 'end;',
  33669. '{$R-}',
  33670. 'procedure DoSome;',
  33671. 'begin',
  33672. ' DoIt(w);',
  33673. ' b:=w;',
  33674. ' b:=''2'';',
  33675. 'end;',
  33676. 'begin',
  33677. '{$R+}',
  33678. '']);
  33679. ConvertProgram;
  33680. CheckSource('TestRangeChecks_AssignCharRange',
  33681. LinesToStr([ // statements
  33682. 'this.b = "2";',
  33683. 'this.w = "3";',
  33684. 'this.DoIt = function (p) {',
  33685. ' rtl.rcc(p, 48, 57);',
  33686. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33687. ' $mod.b = "1";',
  33688. '};',
  33689. 'this.DoSome = function () {',
  33690. ' $mod.DoIt($mod.w);',
  33691. ' $mod.b = $mod.w;',
  33692. ' $mod.b = "2";',
  33693. '};',
  33694. '']),
  33695. LinesToStr([ // $mod.$main
  33696. '']));
  33697. end;
  33698. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33699. begin
  33700. StartProgram(false);
  33701. Add([
  33702. '{$R+}',
  33703. 'type',
  33704. ' Ten = 1..10;',
  33705. ' TArr = array of Ten;',
  33706. ' TArrArr = array of TArr;',
  33707. ' TArrByte = array[byte] of Ten;',
  33708. ' TArrChar = array[''0''..''9''] of Ten;',
  33709. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33710. ' TObject = class',
  33711. ' A: TArr;',
  33712. ' end;',
  33713. 'procedure DoIt;',
  33714. 'var',
  33715. ' Arr: TArr;',
  33716. ' ArrArr: TArrArr;',
  33717. ' ArrByte: TArrByte;',
  33718. ' ArrChar: TArrChar;',
  33719. ' ArrByteChar: TArrByteChar;',
  33720. ' i: Ten;',
  33721. ' c: char;',
  33722. ' o: tobject;',
  33723. 'begin',
  33724. ' i:=Arr[1];',
  33725. ' i:=ArrByteChar[1,''2''];',
  33726. ' Arr[1]:=Arr[1];',
  33727. ' Arr[i]:=Arr[i];',
  33728. ' ArrByte[3]:=ArrByte[3];',
  33729. ' ArrByte[i]:=ArrByte[i];',
  33730. ' ArrChar[''5'']:=ArrChar[''5''];',
  33731. ' ArrChar[c]:=ArrChar[c];',
  33732. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33733. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33734. ' o.a[i]:=o.a[i];',
  33735. 'end;',
  33736. 'begin',
  33737. '']);
  33738. ConvertProgram;
  33739. CheckSource('TestRangeChecks_ArrayIndex',
  33740. LinesToStr([ // statements
  33741. 'this.TArrByteChar$clone = function (a) {',
  33742. ' var b = [];',
  33743. ' b.length = 256;',
  33744. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33745. ' return b;',
  33746. '};',
  33747. 'rtl.createClass(this, "TObject", null, function () {',
  33748. ' this.$init = function () {',
  33749. ' this.A = [];',
  33750. ' };',
  33751. ' this.$final = function () {',
  33752. ' this.A = undefined;',
  33753. ' };',
  33754. '});',
  33755. 'this.DoIt = function () {',
  33756. ' var Arr = [];',
  33757. ' var ArrArr = [];',
  33758. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33759. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33760. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33761. ' var i = 0;',
  33762. ' var c = "\x00";',
  33763. ' var o = null;',
  33764. ' i = rtl.rc(Arr[1], 1, 10);',
  33765. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33766. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33767. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33768. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33769. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33770. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33771. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33772. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33773. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33774. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33775. '};',
  33776. '']),
  33777. LinesToStr([ // $mod.$main
  33778. '']));
  33779. end;
  33780. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33781. begin
  33782. StartProgram(false);
  33783. Add([
  33784. '{$R+}',
  33785. 'type',
  33786. ' Ten = 1..10;',
  33787. ' TRec = record x: Ten end;',
  33788. ' TArr = array of TRec;',
  33789. ' TArrArr = array of TArr;',
  33790. ' TObject = class',
  33791. ' A: TArr;',
  33792. ' end;',
  33793. 'procedure DoIt;',
  33794. 'var',
  33795. ' Arr: TArr;',
  33796. ' ArrArr: TArrArr;',
  33797. ' i: Ten;',
  33798. ' o: tobject;',
  33799. 'begin',
  33800. ' Arr[1]:=Arr[1];',
  33801. ' Arr[i]:=Arr[i+1];',
  33802. ' o.a[i]:=o.a[i+2];',
  33803. 'end;',
  33804. 'begin',
  33805. '']);
  33806. ConvertProgram;
  33807. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33808. LinesToStr([ // statements
  33809. 'rtl.recNewT(this, "TRec", function () {',
  33810. ' this.x = 0;',
  33811. ' this.$eq = function (b) {',
  33812. ' return this.x === b.x;',
  33813. ' };',
  33814. ' this.$assign = function (s) {',
  33815. ' this.x = s.x;',
  33816. ' return this;',
  33817. ' };',
  33818. '});',
  33819. 'rtl.createClass(this, "TObject", null, function () {',
  33820. ' this.$init = function () {',
  33821. ' this.A = [];',
  33822. ' };',
  33823. ' this.$final = function () {',
  33824. ' this.A = undefined;',
  33825. ' };',
  33826. '});',
  33827. 'this.DoIt = function () {',
  33828. ' var Arr = [];',
  33829. ' var ArrArr = [];',
  33830. ' var i = 0;',
  33831. ' var o = null;',
  33832. ' Arr[1].$assign(Arr[1]);',
  33833. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33834. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33835. '};',
  33836. '']),
  33837. LinesToStr([ // $mod.$main
  33838. '']));
  33839. end;
  33840. procedure TTestModule.TestRangeChecks_StringIndex;
  33841. begin
  33842. StartProgram(false);
  33843. Add([
  33844. 'type',
  33845. ' TObject = class',
  33846. ' S: string;',
  33847. ' end;',
  33848. '{$R+}',
  33849. 'procedure DoIt(var h: string);',
  33850. 'var',
  33851. ' s: string;',
  33852. ' i: longint;',
  33853. ' c: char;',
  33854. ' o: tobject;',
  33855. 'begin',
  33856. ' c:=s[1];',
  33857. ' s[i]:=s[i];',
  33858. ' h[i]:=h[i];',
  33859. ' c:=o.s[i];',
  33860. ' o.s[i]:=c;',
  33861. 'end;',
  33862. 'begin',
  33863. '']);
  33864. ConvertProgram;
  33865. CheckSource('TestRangeChecks_StringIndex',
  33866. LinesToStr([ // statements
  33867. 'rtl.createClass(this, "TObject", null, function () {',
  33868. ' this.$init = function () {',
  33869. ' this.S = "";',
  33870. ' };',
  33871. ' this.$final = function () {',
  33872. ' };',
  33873. '});',
  33874. 'this.DoIt = function (h) {',
  33875. ' var s = "";',
  33876. ' var i = 0;',
  33877. ' var c = "\x00";',
  33878. ' var o = null;',
  33879. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33880. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33881. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33882. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33883. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33884. '};',
  33885. '']),
  33886. LinesToStr([ // $mod.$main
  33887. '']));
  33888. end;
  33889. procedure TTestModule.TestRangeChecks_TypecastInt;
  33890. begin
  33891. StartProgram(false);
  33892. Add([
  33893. '{$R+}',
  33894. 'var',
  33895. ' i: nativeint;',
  33896. ' b: byte;',
  33897. ' sh: shortint;',
  33898. ' w: word;',
  33899. ' sm: smallint;',
  33900. ' lw: longword;',
  33901. ' li: longint;',
  33902. 'begin',
  33903. ' b:=12+byte(i);',
  33904. ' sh:=12+shortint(i);',
  33905. ' w:=12+word(i);',
  33906. ' sm:=12+smallint(i);',
  33907. ' lw:=12+longword(i);',
  33908. ' li:=12+longint(i);',
  33909. '']);
  33910. ConvertProgram;
  33911. CheckSource('TestRangeChecks_TypecastInt',
  33912. LinesToStr([
  33913. 'this.i = 0;',
  33914. 'this.b = 0;',
  33915. 'this.sh = 0;',
  33916. 'this.w = 0;',
  33917. 'this.sm = 0;',
  33918. 'this.lw = 0;',
  33919. 'this.li = 0;',
  33920. '']),
  33921. LinesToStr([
  33922. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33923. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33924. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33925. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33926. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33927. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33928. '']));
  33929. end;
  33930. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33931. begin
  33932. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33933. StartProgram(false);
  33934. Add([
  33935. '{$modeswitch typehelpers}',
  33936. '{$R+}',
  33937. 'type',
  33938. ' TObject = class',
  33939. ' FSize: byte;',
  33940. ' property Size: byte read FSize;',
  33941. ' end;',
  33942. ' THelper = type helper for byte',
  33943. ' procedure SetIt(w: word);',
  33944. ' end;',
  33945. 'procedure THelper.SetIt(w: word);',
  33946. 'begin',
  33947. ' Self:=w;',
  33948. 'end;',
  33949. 'function GetIt: byte;',
  33950. 'begin',
  33951. ' Result.SetIt(2);',
  33952. 'end;',
  33953. 'var',
  33954. ' b: byte = 3;',
  33955. ' o: TObject;',
  33956. 'begin',
  33957. ' b.SetIt(14);',
  33958. ' with b do SetIt(15);',
  33959. ' o.Size.SetIt(16);',
  33960. '']);
  33961. ConvertProgram;
  33962. CheckSource('TestRangeChecks_AssignInt',
  33963. LinesToStr([ // statements
  33964. 'rtl.createClass(this, "TObject", null, function () {',
  33965. ' this.$init = function () {',
  33966. ' this.FSize = 0;',
  33967. ' };',
  33968. ' this.$final = function () {',
  33969. ' };',
  33970. '});',
  33971. 'rtl.createHelper(this, "THelper", null, function () {',
  33972. ' this.SetIt = function (w) {',
  33973. ' rtl.rc(w, 0, 65535);',
  33974. ' this.set(w);',
  33975. ' };',
  33976. '});',
  33977. 'this.GetIt = function () {',
  33978. ' var Result = 0;',
  33979. ' $mod.THelper.SetIt.call({',
  33980. ' get: function () {',
  33981. ' return Result;',
  33982. ' },',
  33983. ' set: function (v) {',
  33984. ' rtl.rc(v, 0, 255);',
  33985. ' Result = v;',
  33986. ' }',
  33987. ' }, 2);',
  33988. ' return Result;',
  33989. '};',
  33990. 'this.b = 3;',
  33991. 'this.o = null;',
  33992. '']),
  33993. LinesToStr([ // $mod.$main
  33994. '$mod.THelper.SetIt.call({',
  33995. ' p: $mod,',
  33996. ' get: function () {',
  33997. ' return this.p.b;',
  33998. ' },',
  33999. ' set: function (v) {',
  34000. ' rtl.rc(v, 0, 255);',
  34001. ' this.p.b = v;',
  34002. ' }',
  34003. '}, 14);',
  34004. 'var $with = $mod.b;',
  34005. '$mod.THelper.SetIt.call({',
  34006. ' get: function () {',
  34007. ' return $with;',
  34008. ' },',
  34009. ' set: function (v) {',
  34010. ' rtl.rc(v, 0, 255);',
  34011. ' $with = v;',
  34012. ' }',
  34013. '}, 15);',
  34014. '$mod.THelper.SetIt.call({',
  34015. ' p: $mod.o,',
  34016. ' get: function () {',
  34017. ' return this.p.FSize;',
  34018. ' },',
  34019. ' set: function (v) {',
  34020. ' rtl.rc(v, 0, 255);',
  34021. ' this.p.FSize = v;',
  34022. ' }',
  34023. '}, 16);',
  34024. '']));
  34025. end;
  34026. procedure TTestModule.TestRangeChecks_AssignCurrency;
  34027. begin
  34028. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34029. StartProgram(false);
  34030. Add([
  34031. '{$R+}',
  34032. 'var',
  34033. ' c: currency = 2.34;',
  34034. ' i: double;',
  34035. 'procedure DoIt(p: currency);',
  34036. 'begin',
  34037. ' c:=i;',
  34038. ' c+=i;',
  34039. ' c:=1;',
  34040. 'end;',
  34041. '{$R-}',
  34042. 'procedure DoSome;',
  34043. 'begin',
  34044. ' DoIt(i);',
  34045. ' c:=i;',
  34046. ' c:=2;',
  34047. 'end;',
  34048. 'begin',
  34049. '{$R+}',
  34050. '']);
  34051. ConvertProgram;
  34052. CheckSource('TestRangeChecks_AssignCurrency',
  34053. LinesToStr([ // statements
  34054. 'this.c = 2.34;',
  34055. 'this.i = 0.0;',
  34056. 'this.DoIt = function (p) {',
  34057. ' rtl.rc(p, -922337203685477, 922337203685477);',
  34058. ' $mod.c = rtl.rc(rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34059. ' rtl.rc($mod.c += rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34060. ' $mod.c = 10000;',
  34061. '};',
  34062. 'this.DoSome = function () {',
  34063. ' $mod.DoIt($mod.i * 10000);',
  34064. ' $mod.c = rtl.trunc($mod.i * 10000);',
  34065. ' $mod.c = 20000;',
  34066. '};',
  34067. '']),
  34068. LinesToStr([ // $mod.$main
  34069. '']));
  34070. end;
  34071. procedure TTestModule.TestAsync_Proc;
  34072. begin
  34073. StartProgram(false);
  34074. Add([
  34075. 'procedure Fly(w: word = 1); async; forward;',
  34076. 'procedure Run(w: word = 2); async;',
  34077. 'begin',
  34078. ' Fly(w);',
  34079. ' Fly;',
  34080. ' await(Fly(w));',
  34081. ' await(Fly);',
  34082. 'end;',
  34083. 'procedure Fly(w: word); ',
  34084. 'begin',
  34085. 'end;',
  34086. 'begin',
  34087. ' Run;',
  34088. ' Run(3);',
  34089. '']);
  34090. CheckResolverUnexpectedHints();
  34091. ConvertProgram;
  34092. CheckSource('TestAsync_Proc',
  34093. LinesToStr([ // statements
  34094. 'this.Run = async function (w) {',
  34095. ' $mod.Fly(w);',
  34096. ' $mod.Fly(1);',
  34097. ' await $mod.Fly(w);',
  34098. ' await $mod.Fly(1);',
  34099. '};',
  34100. 'this.Fly = async function (w) {',
  34101. '};',
  34102. '']),
  34103. LinesToStr([
  34104. '$mod.Run(2);',
  34105. '$mod.Run(3);',
  34106. '']));
  34107. end;
  34108. procedure TTestModule.TestAsync_CallResultIsPromise;
  34109. begin
  34110. StartProgram(false);
  34111. Add([
  34112. '{$modeswitch externalclass}',
  34113. 'type',
  34114. ' TObject = class',
  34115. ' end;',
  34116. ' TJSPromise = class external name ''Promise''',
  34117. ' end;',
  34118. ' TBird = class',
  34119. ' function Fly: word; async; ',
  34120. ' end;',
  34121. 'function TBird.Fly: word; async; ',
  34122. 'begin',
  34123. ' Result:=3;',
  34124. ' Fly:=4+Result;',
  34125. ' if Result=5 then ;',
  34126. ' exit(6);',
  34127. 'end;',
  34128. 'function Run: word; async;',
  34129. 'begin',
  34130. ' Result:=11+Result;',
  34131. ' inc(Result);',
  34132. 'end;',
  34133. 'var',
  34134. ' p: TJSPromise;',
  34135. ' o: TBird;',
  34136. 'begin',
  34137. ' p:=Run;',
  34138. ' p:=Run();',
  34139. ' if Run=p then ;',
  34140. ' if p=Run then ;',
  34141. ' if Run()=p then ;',
  34142. ' if p=Run() then ;',
  34143. ' p:=o.Fly;',
  34144. ' p:=o.Fly();',
  34145. ' if o.Fly=p then ;',
  34146. ' if o.Fly()=p then ;',
  34147. ' with o do begin',
  34148. ' p:=Fly;',
  34149. ' p:=Fly();',
  34150. ' if Fly=p then ;',
  34151. ' if Fly()=p then ;',
  34152. ' end;',
  34153. '']);
  34154. CheckResolverUnexpectedHints();
  34155. ConvertProgram;
  34156. CheckSource('TestAsync_CallResultIsPromise',
  34157. LinesToStr([ // statements
  34158. 'rtl.createClass(this, "TObject", null, function () {',
  34159. ' this.$init = function () {',
  34160. ' };',
  34161. ' this.$final = function () {',
  34162. ' };',
  34163. '});',
  34164. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34165. ' this.Fly = async function () {',
  34166. ' var Result = 0;',
  34167. ' Result = 3;',
  34168. ' Result = 4 + Result;',
  34169. ' if (Result === 5) ;',
  34170. ' return 6;',
  34171. ' return Result;',
  34172. ' };',
  34173. '});',
  34174. 'this.Run = async function () {',
  34175. ' var Result = 0;',
  34176. ' Result = 11 + Result;',
  34177. ' Result += 1;',
  34178. ' return Result;',
  34179. '};',
  34180. 'this.p = null;',
  34181. 'this.o = null;',
  34182. '']),
  34183. LinesToStr([
  34184. '$mod.p = $mod.Run();',
  34185. '$mod.p = $mod.Run();',
  34186. 'if ($mod.Run() === $mod.p) ;',
  34187. 'if ($mod.p === $mod.Run()) ;',
  34188. 'if ($mod.Run() === $mod.p) ;',
  34189. 'if ($mod.p === $mod.Run()) ;',
  34190. '$mod.p = $mod.o.Fly();',
  34191. '$mod.p = $mod.o.Fly();',
  34192. 'if ($mod.o.Fly() === $mod.p) ;',
  34193. 'if ($mod.o.Fly() === $mod.p) ;',
  34194. 'var $with = $mod.o;',
  34195. '$mod.p = $with.Fly();',
  34196. '$mod.p = $with.Fly();',
  34197. 'if ($with.Fly() === $mod.p) ;',
  34198. 'if ($with.Fly() === $mod.p) ;',
  34199. '']));
  34200. end;
  34201. procedure TTestModule.TestAsync_ConstructorFail;
  34202. begin
  34203. StartProgram(false);
  34204. Add([
  34205. 'type',
  34206. ' TObject = class',
  34207. ' end;',
  34208. ' TBird = class',
  34209. ' constructor Create; async;',
  34210. ' end;',
  34211. 'constructor TBird.Create; async;',
  34212. 'begin',
  34213. 'end;',
  34214. 'begin',
  34215. '']);
  34216. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34217. ConvertProgram;
  34218. end;
  34219. procedure TTestModule.TestAsync_PropertyGetterFail;
  34220. begin
  34221. StartProgram(false);
  34222. Add([
  34223. 'type',
  34224. ' TObject = class',
  34225. ' end;',
  34226. ' TBird = class',
  34227. ' function GetSize: word; async;',
  34228. ' property Size: word read GetSize;',
  34229. ' end;',
  34230. 'function TBird.GetSize: word; async;',
  34231. 'begin',
  34232. 'end;',
  34233. 'begin',
  34234. '']);
  34235. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34236. ConvertProgram;
  34237. end;
  34238. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34239. begin
  34240. StartProgram(false);
  34241. Add([
  34242. 'procedure Run; async;',
  34243. 'begin',
  34244. ' await(word,1);',
  34245. 'end;',
  34246. 'begin',
  34247. '']);
  34248. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34249. ConvertProgram;
  34250. end;
  34251. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34252. begin
  34253. StartProgram(false);
  34254. Add([
  34255. 'type',
  34256. ' TObject = class',
  34257. ' end;',
  34258. ' TBird = class',
  34259. ' end;',
  34260. 'function Fly: TObject; async;',
  34261. 'begin',
  34262. 'end;',
  34263. 'procedure Run; async;',
  34264. 'begin',
  34265. ' await(TBird,Fly);',
  34266. 'end;',
  34267. 'begin',
  34268. '']);
  34269. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34270. ConvertProgram;
  34271. end;
  34272. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34273. begin
  34274. StartProgram(false);
  34275. Add([
  34276. 'procedure Crawl(w: double); ',
  34277. 'begin',
  34278. 'end;',
  34279. 'procedure Run(w: double);',
  34280. 'begin',
  34281. ' await(Crawl(w));',
  34282. 'end;',
  34283. 'begin',
  34284. ' Run(1);']);
  34285. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34286. ConvertProgram;
  34287. end;
  34288. procedure TTestModule.TestAWait_IntegerFail;
  34289. begin
  34290. StartProgram(false);
  34291. Add([
  34292. 'function Run: word;',
  34293. 'begin',
  34294. 'end;',
  34295. 'procedure Fly(w: word); async;',
  34296. 'begin',
  34297. ' await(Run());',
  34298. 'end;',
  34299. 'begin',
  34300. ' Fly(1);']);
  34301. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  34302. ConvertProgram;
  34303. end;
  34304. procedure TTestModule.TestAWait_ExternalClassPromise;
  34305. begin
  34306. StartProgram(false);
  34307. Add([
  34308. '{$modeswitch externalclass}',
  34309. 'type',
  34310. ' TJSPromise = class external name ''Promise''',
  34311. ' end;',
  34312. ' TJSThenable = class external name ''Thenable''',
  34313. ' end;',
  34314. 'function Fly(w: word): TJSPromise;',
  34315. 'begin',
  34316. 'end;',
  34317. 'function Jump(w: word): word; async;',
  34318. 'begin',
  34319. 'end;',
  34320. 'function Eat(w: word): TJSPromise; async;',
  34321. 'begin',
  34322. 'end;',
  34323. 'function Run(d: double): word; async;',
  34324. 'var',
  34325. ' p: TJSPromise;',
  34326. 'begin',
  34327. ' Result:=await(word,p);', // promise needs type
  34328. ' Result:=await(word,Fly(3));', // promise needs type
  34329. ' Result:=await(Jump(4));', // async non promise must omit the type
  34330. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34331. ' Result:=await(word,Eat(6));', // promise needs type
  34332. 'end;',
  34333. 'begin',
  34334. '']);
  34335. ConvertProgram;
  34336. CheckSource('TestAWait_ExternalClassPromise',
  34337. LinesToStr([ // statements
  34338. 'this.Fly = function (w) {',
  34339. ' var Result = null;',
  34340. ' return Result;',
  34341. '};',
  34342. 'this.Jump = async function (w) {',
  34343. ' var Result = 0;',
  34344. ' return Result;',
  34345. '};',
  34346. 'this.Eat = async function (w) {',
  34347. ' var Result = null;',
  34348. ' return Result;',
  34349. '};',
  34350. 'this.Run = async function (d) {',
  34351. ' var Result = 0;',
  34352. ' var p = null;',
  34353. ' Result = await p;',
  34354. ' Result = await $mod.Fly(3);',
  34355. ' Result = await $mod.Jump(4);',
  34356. ' Result = await $mod.Jump(5);',
  34357. ' Result = await $mod.Eat(6);',
  34358. ' return Result;',
  34359. '};',
  34360. '']),
  34361. LinesToStr([
  34362. ]));
  34363. CheckResolverUnexpectedHints();
  34364. end;
  34365. procedure TTestModule.TestAWait_JSValue;
  34366. begin
  34367. StartProgram(false);
  34368. Add([
  34369. '{$modeswitch externalclass}',
  34370. 'type',
  34371. ' TJSPromise = class external name ''Promise''',
  34372. ' end;',
  34373. 'function Fly(w: word): jsvalue; async;',
  34374. 'begin',
  34375. 'end;',
  34376. 'function Run(d: jsvalue; var e): word; async;',
  34377. 'begin',
  34378. ' Result:=await(word,d);', // promise needs type
  34379. ' d:=await(Fly(4));', // async non promise must omit the type
  34380. ' Result:=await(word,e);', // promise needs type
  34381. 'end;',
  34382. 'begin',
  34383. '']);
  34384. ConvertProgram;
  34385. CheckSource('TestAWait_JSValue',
  34386. LinesToStr([ // statements
  34387. 'this.Fly = async function (w) {',
  34388. ' var Result = undefined;',
  34389. ' return Result;',
  34390. '};',
  34391. 'this.Run = async function (d, e) {',
  34392. ' var Result = 0;',
  34393. ' Result = await d;',
  34394. ' d = await $mod.Fly(4);',
  34395. ' Result = await e.get();',
  34396. ' return Result;',
  34397. '};',
  34398. '']),
  34399. LinesToStr([
  34400. ]));
  34401. CheckResolverUnexpectedHints();
  34402. end;
  34403. procedure TTestModule.TestAWait_Result;
  34404. begin
  34405. StartProgram(false);
  34406. Add([
  34407. '{$modeswitch externalclass}',
  34408. 'type',
  34409. ' TJSPromise = class external name ''Promise''',
  34410. ' end;',
  34411. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34412. 'begin',
  34413. 'end;',
  34414. 'function Run(d: double = 1.6): word; async;',
  34415. 'begin',
  34416. ' Result:=await(word,Crawl);',
  34417. ' Result:=await(word,Crawl(4.5));',
  34418. ' Result:=await(Run);',
  34419. ' Result:=await(Run(6.7));',
  34420. 'end;',
  34421. 'begin',
  34422. ' Run(1);']);
  34423. ConvertProgram;
  34424. CheckSource('TestAWait_Result',
  34425. LinesToStr([ // statements
  34426. 'this.Crawl = function (d) {',
  34427. ' var Result = null;',
  34428. ' return Result;',
  34429. '};',
  34430. 'this.Run = async function (d) {',
  34431. ' var Result = 0;',
  34432. ' Result = await $mod.Crawl(1.3);',
  34433. ' Result = await $mod.Crawl(4.5);',
  34434. ' Result = await $mod.Run(1.6);',
  34435. ' Result = await $mod.Run(6.7);',
  34436. ' return Result;',
  34437. '};',
  34438. '']),
  34439. LinesToStr([
  34440. '$mod.Run(1);'
  34441. ]));
  34442. CheckResolverUnexpectedHints();
  34443. end;
  34444. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34445. begin
  34446. StartProgram(false);
  34447. Add([
  34448. '{$mode objfpc}',
  34449. '{$modeswitch externalclass}',
  34450. 'type',
  34451. ' TJSPromise = class external name ''Promise''',
  34452. ' end;',
  34453. 'function Run: TJSPromise; async;',
  34454. 'begin',
  34455. 'end;',
  34456. 'procedure Fly(w: word); async;',
  34457. 'begin',
  34458. ' await(Run());',
  34459. 'end;',
  34460. 'begin',
  34461. ' Fly(1);']);
  34462. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34463. nWrongNumberOfParametersForCallTo);
  34464. ConvertProgram;
  34465. end;
  34466. procedure TTestModule.TestAsync_AnonymousProc;
  34467. begin
  34468. StartProgram(false);
  34469. Add([
  34470. '{$mode objfpc}',
  34471. '{$modeswitch externalclass}',
  34472. 'type',
  34473. ' TJSPromise = class external name ''Promise''',
  34474. ' end;',
  34475. 'type',
  34476. ' TFunc = reference to function(x: double): word; async;',
  34477. 'function Crawl(d: double = 1.3): word; async;',
  34478. 'begin',
  34479. 'end;',
  34480. 'var Func: TFunc;',
  34481. 'begin',
  34482. ' Func:=function(c:double):word async begin',
  34483. ' Result:=await(Crawl(c));',
  34484. ' end;',
  34485. ' Func:=function(c:double):word async assembler asm',
  34486. ' end;',
  34487. '']);
  34488. ConvertProgram;
  34489. CheckSource('TestAsync_AnonymousProc',
  34490. LinesToStr([ // statements
  34491. 'this.Crawl = async function (d) {',
  34492. ' var Result = 0;',
  34493. ' return Result;',
  34494. '};',
  34495. 'this.Func = null;',
  34496. '']),
  34497. LinesToStr([
  34498. '$mod.Func = async function (c) {',
  34499. ' var Result = 0;',
  34500. ' Result = await $mod.Crawl(c);',
  34501. ' return Result;',
  34502. '};',
  34503. '$mod.Func = async function (c) {',
  34504. '};',
  34505. '']));
  34506. CheckResolverUnexpectedHints();
  34507. end;
  34508. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34509. begin
  34510. StartProgram(false);
  34511. Add([
  34512. '{$mode objfpc}',
  34513. '{$modeswitch externalclass}',
  34514. 'type',
  34515. ' TJSPromise = class external name ''Promise''',
  34516. ' end;',
  34517. ' TObject = class',
  34518. ' public',
  34519. ' procedure Fly(Prom: TJSPromise);',
  34520. ' end;',
  34521. ' TFunc = reference to procedure(Bird: TObject);',
  34522. 'procedure TObject.Fly(Prom: TJSPromise);',
  34523. 'begin',
  34524. 'end;',
  34525. 'function Crawl: jsvalue; async;',
  34526. 'begin',
  34527. 'end;',
  34528. 'procedure Add(Func: TFunc);',
  34529. 'begin',
  34530. 'end;',
  34531. 'begin',
  34532. ' Add(procedure(Bird: TObject)',
  34533. ' begin',
  34534. ' Bird.Fly(Crawl());',
  34535. ' end);',
  34536. '']);
  34537. ConvertProgram;
  34538. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34539. LinesToStr([ // statements
  34540. 'rtl.createClass(this, "TObject", null, function () {',
  34541. ' this.$init = function () {',
  34542. ' };',
  34543. ' this.$final = function () {',
  34544. ' };',
  34545. ' this.Fly = function (Prom) {',
  34546. ' };',
  34547. '});',
  34548. 'this.Crawl = async function () {',
  34549. ' var Result = undefined;',
  34550. ' return Result;',
  34551. '};',
  34552. 'this.Add = function (Func) {',
  34553. '};',
  34554. '']),
  34555. LinesToStr([
  34556. '$mod.Add(function (Bird) {',
  34557. ' Bird.Fly($mod.Crawl());',
  34558. '});',
  34559. '']));
  34560. end;
  34561. procedure TTestModule.TestAsync_ProcType;
  34562. begin
  34563. StartProgram(false);
  34564. Add([
  34565. '{$mode objfpc}',
  34566. 'type',
  34567. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34568. ' TFunc = function(x: double = 1.1): word; async;',
  34569. ' TProc = procedure(x: longint = 7); async;',
  34570. 'function Crawl(d: double): word; async;',
  34571. 'begin',
  34572. 'end;',
  34573. 'procedure Run(e:longint); async;',
  34574. 'begin',
  34575. 'end;',
  34576. 'procedure Fly(p: TProc); async;',
  34577. 'begin',
  34578. ' await(p);',
  34579. ' await(p());',
  34580. 'end;',
  34581. 'var',
  34582. ' RefFunc: TRefFunc;',
  34583. ' Func: TFunc;',
  34584. ' Proc, ProcB: TProc;',
  34585. 'begin',
  34586. ' Func:=@Crawl;',
  34587. ' RefFunc:=@Crawl;',
  34588. ' RefFunc:=function(c:double):word async begin',
  34589. ' Result:=await(RefFunc);',
  34590. ' Result:=await(RefFunc());',
  34591. ' Result:=await(Func);',
  34592. ' Result:=await(Func());',
  34593. ' await(Proc);',
  34594. ' await(Proc());',
  34595. ' await(Proc(13));',
  34596. ' end;',
  34597. ' Proc:=@Run;',
  34598. ' if Proc=ProcB then ;',
  34599. ' ']);
  34600. ConvertProgram;
  34601. CheckResolverUnexpectedHints();
  34602. CheckSource('TestAsync_ProcType',
  34603. LinesToStr([ // statements
  34604. 'this.Crawl = async function (d) {',
  34605. ' var Result = 0;',
  34606. ' return Result;',
  34607. '};',
  34608. 'this.Run = async function (e) {',
  34609. '};',
  34610. 'this.Fly = async function (p) {',
  34611. ' await p(7);',
  34612. ' await p(7);',
  34613. '};',
  34614. 'this.RefFunc = null;',
  34615. 'this.Func = null;',
  34616. 'this.Proc = null;',
  34617. 'this.ProcB = null;',
  34618. '']),
  34619. LinesToStr([
  34620. '$mod.Func = $mod.Crawl;',
  34621. '$mod.RefFunc = $mod.Crawl;',
  34622. '$mod.RefFunc = async function (c) {',
  34623. ' var Result = 0;',
  34624. ' Result = await $mod.RefFunc(1.3);',
  34625. ' Result = await $mod.RefFunc(1.3);',
  34626. ' Result = await $mod.Func(1.1);',
  34627. ' Result = await $mod.Func(1.1);',
  34628. ' await $mod.Proc(7);',
  34629. ' await $mod.Proc(7);',
  34630. ' await $mod.Proc(13);',
  34631. ' return Result;',
  34632. '};',
  34633. '$mod.Proc = $mod.Run;',
  34634. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34635. '']));
  34636. end;
  34637. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34638. begin
  34639. StartProgram(false);
  34640. Add([
  34641. '{$mode objfpc}',
  34642. 'type',
  34643. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34644. 'function Crawl(d: double): word; async;',
  34645. 'begin',
  34646. 'end;',
  34647. 'var',
  34648. ' RefFunc: TRefFunc;',
  34649. 'begin',
  34650. ' RefFunc:=@Crawl;',
  34651. ' ']);
  34652. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34653. ConvertProgram;
  34654. end;
  34655. procedure TTestModule.TestAsync_Inherited;
  34656. begin
  34657. StartProgram(false);
  34658. Add([
  34659. '{$mode objfpc}',
  34660. '{$modeswitch externalclass}',
  34661. 'type',
  34662. ' TJSPromise = class external name ''Promise''',
  34663. ' end;',
  34664. ' TObject = class',
  34665. ' function Run(w: word = 3): word; async; virtual;',
  34666. ' end;',
  34667. ' TBird = class',
  34668. ' function Run(w: word = 3): word; async; override;',
  34669. ' end;',
  34670. 'function TObject.Run(w: word = 3): word; async;',
  34671. 'begin',
  34672. 'end;',
  34673. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34674. 'var p: TJSPromise;',
  34675. 'begin',
  34676. ' p:=inherited;',
  34677. ' p:=inherited Run;',
  34678. ' p:=inherited Run();',
  34679. ' p:=inherited Run(4);',
  34680. ' exit(p);',
  34681. ' exit(inherited);',
  34682. ' exit(inherited Run);',
  34683. ' exit(inherited Run(5));',
  34684. ' exit(6);',
  34685. 'end;',
  34686. 'begin',
  34687. ' ']);
  34688. ConvertProgram;
  34689. CheckSource('TestAsync_Inherited',
  34690. LinesToStr([ // statements
  34691. 'rtl.createClass(this, "TObject", null, function () {',
  34692. ' this.$init = function () {',
  34693. ' };',
  34694. ' this.$final = function () {',
  34695. ' };',
  34696. ' this.Run = async function (w) {',
  34697. ' var Result = 0;',
  34698. ' return Result;',
  34699. ' };',
  34700. '});',
  34701. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34702. ' this.Run = async function (w) {',
  34703. ' var Result = 0;',
  34704. ' var p = null;',
  34705. ' p = $mod.TObject.Run.apply(this, arguments);',
  34706. ' p = $mod.TObject.Run.call(this, 3);',
  34707. ' p = $mod.TObject.Run.call(this, 3);',
  34708. ' p = $mod.TObject.Run.call(this, 4);',
  34709. ' return p;',
  34710. ' return $mod.TObject.Run.apply(this, arguments);',
  34711. ' return $mod.TObject.Run.call(this, 3);',
  34712. ' return $mod.TObject.Run.call(this, 5);',
  34713. ' return 6;',
  34714. ' return Result;',
  34715. ' };',
  34716. '});',
  34717. '']),
  34718. LinesToStr([
  34719. '']));
  34720. CheckResolverUnexpectedHints();
  34721. end;
  34722. procedure TTestModule.TestAsync_ClassInterface;
  34723. begin
  34724. StartProgram(false);
  34725. Add([
  34726. '{$mode objfpc}',
  34727. '{$modeswitch externalclass}',
  34728. 'type',
  34729. ' TJSPromise = class external name ''Promise''',
  34730. ' end;',
  34731. ' IUnknown = interface',
  34732. ' function _AddRef: longint;',
  34733. ' function _Release: longint;',
  34734. ' end;',
  34735. 'function Say(i: IUnknown): IUnknown; async;',
  34736. 'begin',
  34737. 'end;',
  34738. 'function Run: IUnknown; async;',
  34739. 'begin',
  34740. ' Result:=await(Run);',
  34741. ' Result:=await(Run());',
  34742. ' Result:=await(Run) as IUnknown;',
  34743. ' Result:=await(Say(nil));',
  34744. ' Result:=await(Say(await(Run())));',
  34745. ' Result:=await(Say(await(Run()) as IUnknown));',
  34746. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34747. 'end;',
  34748. 'procedure Fly;',
  34749. 'var p: TJSPromise;',
  34750. 'begin',
  34751. ' Run;',
  34752. ' Run();',
  34753. ' p:=Run;',
  34754. ' p:=Run();',
  34755. 'end;',
  34756. 'begin',
  34757. ' ']);
  34758. ConvertProgram;
  34759. CheckSource('TestAsync_ClassInterface',
  34760. LinesToStr([ // statements
  34761. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34762. 'this.Say = async function (i) {',
  34763. ' var Result = null;',
  34764. ' return Result;',
  34765. '};',
  34766. 'this.Run = async function () {',
  34767. ' var Result = null;',
  34768. ' var $ok = false;',
  34769. ' try {',
  34770. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34771. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34772. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34773. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34774. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34775. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34776. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34777. ' $ok = true;',
  34778. ' } finally {',
  34779. ' if (!$ok) rtl._Release(Result);',
  34780. ' };',
  34781. ' return Result;',
  34782. '};',
  34783. 'this.Fly = function () {',
  34784. ' var p = null;',
  34785. ' $mod.Run();',
  34786. ' $mod.Run();',
  34787. ' p = $mod.Run();',
  34788. ' p = $mod.Run();',
  34789. '};',
  34790. '']),
  34791. LinesToStr([
  34792. '']));
  34793. CheckResolverUnexpectedHints();
  34794. end;
  34795. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34796. begin
  34797. StartProgram(true,[supTInterfacedObject]);
  34798. Add([
  34799. '{$mode objfpc}',
  34800. '{$modeswitch externalclass}',
  34801. 'type',
  34802. ' TJSPromise = class external name ''Promise''',
  34803. ' end;',
  34804. ' IBird = interface',
  34805. ' procedure Run;',
  34806. ' end;',
  34807. ' TBird = class(TInterfacedObject,IBird)',
  34808. ' procedure Run; async;',
  34809. ' end;',
  34810. 'procedure TBird.Run;',
  34811. 'begin',
  34812. 'end;',
  34813. 'begin',
  34814. ' ']);
  34815. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34816. ConvertProgram;
  34817. end;
  34818. procedure TTestModule.TestAWait_ClassAs;
  34819. begin
  34820. StartProgram(false);
  34821. Add([
  34822. '{$mode objfpc}',
  34823. '{$modeswitch externalclass}',
  34824. 'type',
  34825. ' TJSPromise = class external name ''Promise''',
  34826. ' end;',
  34827. ' TObject = class',
  34828. ' function Run: TObject; async;',
  34829. ' end;',
  34830. ' TBird = class',
  34831. ' function Fly: TBird; async;',
  34832. ' end;',
  34833. 'function TObject.Run: TObject; async;',
  34834. 'begin',
  34835. 'end;',
  34836. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34837. 'var o: TObject;',
  34838. 'begin',
  34839. ' o:=await(TObject,Run);',
  34840. ' o:=await(TObject,Fly);',
  34841. ' o:=await(TBird,Fly);',
  34842. ' o:=await(TObject,inherited Run);',
  34843. ' o:=await(TObject,inherited Run) as TBird;',
  34844. 'end;',
  34845. 'begin',
  34846. ' ']);
  34847. ConvertProgram;
  34848. CheckSource('TestAWait_ClassAs',
  34849. LinesToStr([ // statements
  34850. 'rtl.createClass(this, "TObject", null, function () {',
  34851. ' this.$init = function () {',
  34852. ' };',
  34853. ' this.$final = function () {',
  34854. ' };',
  34855. ' this.Run = async function () {',
  34856. ' var Result = null;',
  34857. ' return Result;',
  34858. ' };',
  34859. '});',
  34860. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34861. ' this.Fly = async function () {',
  34862. ' var Result = null;',
  34863. ' var o = null;',
  34864. ' o = await this.Run();',
  34865. ' o = await this.Fly();',
  34866. ' o = await this.Fly();',
  34867. ' o = await $mod.TObject.Run.call(this);',
  34868. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34869. ' return Result;',
  34870. ' };',
  34871. '});',
  34872. '']),
  34873. LinesToStr([
  34874. '']));
  34875. CheckResolverUnexpectedHints();
  34876. end;
  34877. procedure TTestModule.TestLibrary_Empty;
  34878. begin
  34879. StartLibrary(false);
  34880. Add([
  34881. '']);
  34882. ConvertLibrary;
  34883. CheckFullSource('TestLibrary_Empty',
  34884. LinesToStr([ // statements
  34885. 'rtl.module("library", [], function () {',
  34886. ' var $mod = this;',
  34887. ' $mod.$main = function () {',
  34888. ' };',
  34889. '});',
  34890. 'rtl.run("library");',
  34891. '']));
  34892. CheckResolverUnexpectedHints();
  34893. end;
  34894. procedure TTestModule.TestLibrary_ExportFunc;
  34895. begin
  34896. StartLibrary(false);
  34897. Add([
  34898. 'procedure Run(w: word);',
  34899. 'begin',
  34900. 'end;',
  34901. 'exports',
  34902. ' Run;',
  34903. ' run name ''Foo'';',
  34904. ' test1.run name ''Test1Run'';',
  34905. '']);
  34906. ConvertLibrary;
  34907. CheckFullSource('TestLibrary_ExportFunc',
  34908. LinesToStr([ // statements
  34909. 'rtl.module("library", [], function () {',
  34910. ' var $mod = this;',
  34911. ' this.Run = function (w) {',
  34912. ' };',
  34913. ' $mod.$main = function () {',
  34914. ' };',
  34915. '});',
  34916. 'rtl.run("library");',
  34917. 'export const Run = pas.library.Run;',
  34918. 'export const Foo = pas.library.Run;',
  34919. 'export const Test1Run = pas.library.Run;',
  34920. '']));
  34921. CheckResolverUnexpectedHints();
  34922. end;
  34923. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34924. begin
  34925. StartLibrary(false);
  34926. Add([
  34927. 'procedure Run(w: word); overload;',
  34928. 'begin',
  34929. 'end;',
  34930. 'procedure Run(s: string); overload;',
  34931. 'begin',
  34932. 'end;',
  34933. 'exports',
  34934. ' Run;',
  34935. '']);
  34936. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34937. nCantDetermineWhichOverloadedFunctionToCall);
  34938. ConvertLibrary;
  34939. end;
  34940. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34941. begin
  34942. StartLibrary(false);
  34943. Add([
  34944. 'procedure Run(w: word);',
  34945. 'begin',
  34946. 'end;',
  34947. 'exports',
  34948. ' Run index 3;',
  34949. '']);
  34950. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34951. ConvertLibrary;
  34952. end;
  34953. procedure TTestModule.TestLibrary_ExportVar;
  34954. begin
  34955. StartLibrary(false);
  34956. Add([
  34957. 'var Wing: word;',
  34958. 'exports',
  34959. ' Wing, wing name ''BirdArm'';',
  34960. '']);
  34961. ConvertLibrary;
  34962. CheckFullSource('TestLibrary_ExportVar',
  34963. LinesToStr([ // statements
  34964. 'rtl.module("library", [], function () {',
  34965. ' var $mod = this;',
  34966. ' this.Wing = 0;',
  34967. ' $mod.$main = function () {',
  34968. ' };',
  34969. '});',
  34970. 'rtl.run("library");',
  34971. 'export const vars = {};',
  34972. 'Object.defineProperties(vars, {',
  34973. ' Wing: {',
  34974. ' enumerable: true,',
  34975. ' get: function () {',
  34976. ' return pas.library.Wing;',
  34977. ' },',
  34978. ' set: function (v) {',
  34979. ' pas.library.Wing = v;',
  34980. ' }',
  34981. ' },',
  34982. ' BirdArm: {',
  34983. ' enumerable: true,',
  34984. ' get: function () {',
  34985. ' return pas.library.Wing;',
  34986. ' },',
  34987. ' set: function (v) {',
  34988. ' pas.library.Wing = v;',
  34989. ' }',
  34990. ' }',
  34991. '});',
  34992. '']));
  34993. CheckResolverUnexpectedHints();
  34994. end;
  34995. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34996. begin
  34997. AddModuleWithIntfImplSrc('Unit1.pas',
  34998. LinesToStr([
  34999. 'type',
  35000. ' TAnt = class',
  35001. ' class function Crawl: word; static;',
  35002. ' end;',
  35003. 'function Fly: word;',
  35004. '']),
  35005. LinesToStr([
  35006. 'function Fly: word;',
  35007. 'begin',
  35008. 'end;',
  35009. 'class function TAnt.Crawl: word;',
  35010. 'begin',
  35011. 'end;',
  35012. '']));
  35013. StartLibrary(true,[supTObject]);
  35014. Add([
  35015. 'uses unit1;',
  35016. 'exports',
  35017. ' Fly;',
  35018. ' TAnt.Crawl;',
  35019. '']);
  35020. ConvertLibrary;
  35021. CheckFullSource('TestLibrary_ExportUnitFunc',
  35022. LinesToStr([ // statements
  35023. 'rtl.module("library", ["system", "Unit1"], function () {',
  35024. ' var $mod = this;',
  35025. ' $mod.$main = function () {',
  35026. ' };',
  35027. '});',
  35028. 'rtl.run("library");',
  35029. 'export const Fly = pas.Unit1.Fly;',
  35030. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  35031. '']));
  35032. CheckResolverUnexpectedHints();
  35033. end;
  35034. Initialization
  35035. RegisterTests([TTestModule]);
  35036. end.