tcmodules.pas 933 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415
  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. type
  27. TSrcMarkerKind = (
  28. mkLabel,
  29. mkResolverReference,
  30. mkDirectReference
  31. );
  32. const
  33. SrcMarker: array[TSrcMarkerKind] of char = (
  34. '#', // mkLabel
  35. '@', // mkResolverReference
  36. '=' // mkDirectReference
  37. );
  38. type
  39. PSrcMarker = ^TSrcMarker;
  40. TSrcMarker = record
  41. Kind: TSrcMarkerKind;
  42. Filename: string;
  43. Row: integer;
  44. StartCol, EndCol: integer; // token start, end column
  45. Identifier: string;
  46. Next: PSrcMarker;
  47. end;
  48. TSystemUnitPart = (
  49. supTObject,
  50. supTVarRec,
  51. supTypeInfo,
  52. supTInterfacedObject,
  53. supWriteln
  54. );
  55. TSystemUnitParts = set of TSystemUnitPart;
  56. { TTestHintMessage }
  57. TTestHintMessage = class
  58. public
  59. Id: int64;
  60. MsgType: TMessageType;
  61. MsgNumber: integer;
  62. Msg: string;
  63. SourcePos: TPasSourcePos;
  64. end;
  65. TTestResolverReferenceData = record
  66. Filename: string;
  67. Row: integer;
  68. StartCol: integer;
  69. EndCol: integer;
  70. Found: TFPList; // list of TPasElement at this token
  71. end;
  72. PTestResolverReferenceData = ^TTestResolverReferenceData;
  73. { TTestPasParser }
  74. TTestPasParser = Class(TPasParser)
  75. end;
  76. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  77. { TTestEnginePasResolver }
  78. TTestEnginePasResolver = class(TPas2JsResolver)
  79. private
  80. FFilename: string;
  81. FModule: TPasModule;
  82. FOnFindUnit: TOnFindUnit;
  83. FParser: TTestPasParser;
  84. FStreamResolver: TStreamResolver;
  85. FScanner: TPas2jsPasScanner;
  86. FSource: string;
  87. procedure SetModule(const AValue: TPasModule);
  88. public
  89. destructor Destroy; override;
  90. function CreateElement(AClass: TPTreeElement; const AName: String;
  91. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  92. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  93. overload; override;
  94. function FindUnit(const AName, InFilename: String; NameExpr,
  95. InFileExpr: TPasExpr): TPasModule; override;
  96. procedure UsedInterfacesFinished(Section: TPasSection); override;
  97. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  98. property Filename: string read FFilename write FFilename;
  99. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  100. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  101. property Parser: TTestPasParser read FParser write FParser;
  102. property Source: string read FSource write FSource;
  103. property Module: TPasModule read FModule write SetModule;
  104. end;
  105. { TCustomTestModule }
  106. TCustomTestModule = Class(TTestCase)
  107. private
  108. FWithTypeInfo: boolean;
  109. FSource: TStringList;
  110. FSkipTests: boolean;
  111. FScanner: TPas2jsPasScanner;
  112. FResolvers: TObjectList;// list of TTestEnginePasResolver
  113. FPasProgram: TPasProgram;
  114. FPasLibrary: TPasLibrary;
  115. FParser: TTestPasParser;
  116. FModule: TPasModule;
  117. FJSSource: TStringList;
  118. FJSRegModuleCall: TJSCallExpression;
  119. FJSModuleSrc: TJSSourceElements;
  120. FJSModuleCallArgs: TJSArguments;
  121. FJSModule: TJSSourceElements;
  122. FJSInterfaceUses: TJSArrayLiteral;
  123. FJSInitBody: TJSFunctionBody;
  124. FJSImplentationUses: TJSArrayLiteral;
  125. FJSImplementationUses: TJSArrayLiteral;
  126. FHub: TPas2JSResolverHub;
  127. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  128. FHintMsgs: TObjectList; // list of TTestHintMessage
  129. FFirstPasStatement: TPasImplBlock;
  130. FFileResolver: TStreamResolver;
  131. FFilename: string;
  132. FExpectedErrorNumber: integer;
  133. FExpectedErrorMsg: string;
  134. FExpectedErrorClass: ExceptClass;
  135. FEngine: TTestEnginePasResolver;
  136. FConverter: TPasToJSConverter;
  137. {$IFDEF EnablePasTreeGlobalRefCount}
  138. FElementRefCountAtSetup: int64;
  139. {$ENDIF}
  140. procedure FreeSrcMarkers;
  141. function GetResolverCount: integer;
  142. function GetResolvers(Index: integer): TTestEnginePasResolver;
  143. function GetMsgCount: integer;
  144. function GetMsgs(Index: integer): TTestHintMessage;
  145. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  146. procedure OnParserLog(Sender: TObject; const Msg: String);
  147. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  148. procedure OnScannerLog(Sender: TObject; const Msg: String);
  149. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  150. procedure OnFindReference(El: TPasElement; FindData: pointer);
  151. procedure SetWithTypeInfo(const AValue: boolean);
  152. protected
  153. procedure SetUp; override;
  154. function CreateConverter: TPasToJSConverter; virtual;
  155. function LoadUnit(const aUnitName: String): TPasModule;
  156. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  157. procedure TearDown; override;
  158. Procedure Add(Line: string); virtual;
  159. Procedure Add(const Lines: array of string);
  160. Procedure StartParsing; virtual;
  161. procedure ParseModuleQueue; virtual;
  162. procedure ParseModule; virtual;
  163. procedure ParseProgram; virtual;
  164. procedure ParseLibrary; virtual;
  165. procedure ParseUnit; virtual;
  166. protected
  167. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  168. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  169. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  172. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  173. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  174. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  175. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure ConvertModule; virtual;
  178. procedure ConvertProgram; virtual;
  179. procedure ConvertLibrary; virtual;
  180. procedure ConvertUnit; virtual;
  181. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  182. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  183. function GetDottedIdentifier(El: TJSElement): string;
  184. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  185. ImplStatements: string = ''); virtual;
  186. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  187. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  188. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  189. procedure CheckReferenceDirectives; virtual;
  190. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  191. Msg: string; Marker: PSrcMarker = nil); virtual;
  192. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  193. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  194. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  197. function IsErrorExpected(E: Exception): boolean;
  198. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  199. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  200. procedure HandleScannerError(E: EScannerError);
  201. procedure HandleParserError(E: EParserError);
  202. procedure HandlePasResolveError(E: EPasResolve);
  203. procedure HandlePas2JSError(E: EPas2JS);
  204. procedure HandleException(E: Exception);
  205. procedure FailException(E: Exception);
  206. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  207. function IndexOfResolver(const Filename: string): integer;
  208. function GetResolver(const Filename: string): TTestEnginePasResolver;
  209. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  210. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  211. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  212. function FindSrcLabel(const Identifier: string): PSrcMarker;
  213. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  214. function GetDefaultNamespace: string;
  215. property PasProgram: TPasProgram Read FPasProgram;
  216. property PasLibrary: TPasLibrary Read FPasLibrary;
  217. property ResolverEngine: TTestEnginePasResolver read FEngine;
  218. property Filename: string read FFilename;
  219. Property Module: TPasModule Read FModule;
  220. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  221. property Converter: TPasToJSConverter read FConverter;
  222. property JSSource: TStringList read FJSSource;
  223. property JSModule: TJSSourceElements read FJSModule;
  224. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  225. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  226. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  227. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  228. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  229. property JSInitBody: TJSFunctionBody read FJSInitBody;
  230. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  231. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  232. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  233. property SkipTests: boolean read FSkipTests write FSkipTests;
  234. public
  235. constructor Create; override;
  236. destructor Destroy; override;
  237. property Hub: TPas2JSResolverHub read FHub;
  238. property Source: TStringList read FSource;
  239. property FileResolver: TStreamResolver read FFileResolver;
  240. property Scanner: TPas2jsPasScanner read FScanner;
  241. property Parser: TTestPasParser read FParser;
  242. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  243. property ResolverCount: integer read GetResolverCount;
  244. property MsgCount: integer read GetMsgCount;
  245. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  246. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  247. end;
  248. { TTestModule }
  249. TTestModule = class(TCustomTestModule)
  250. Published
  251. Procedure TestReservedWords;
  252. // program, units, includes
  253. Procedure TestEmptyProgram;
  254. Procedure TestEmptyProgramUseStrict;
  255. Procedure TestEmptyUnit;
  256. Procedure TestEmptyUnitUseStrict;
  257. Procedure TestDottedUnitNames;
  258. Procedure TestDottedUnitNameImpl;
  259. Procedure TestDottedUnitExpr;
  260. Procedure Test_ModeFPCFail;
  261. Procedure Test_ModeSwitchCBlocksFail;
  262. Procedure TestUnit_UseSystem;
  263. Procedure TestUnit_Intf1Impl2Intf1;
  264. Procedure TestIncludeVersion;
  265. // vars/const
  266. Procedure TestVarInt;
  267. Procedure TestVarBaseTypes;
  268. Procedure TestBaseTypeSingleFail;
  269. Procedure TestBaseTypeExtendedFail;
  270. Procedure TestConstBaseTypes;
  271. Procedure TestUnitImplVars;
  272. Procedure TestUnitImplConsts;
  273. Procedure TestUnitImplRecord;
  274. Procedure TestRenameJSNameConflict;
  275. Procedure TestLocalConst;
  276. Procedure TestVarExternal;
  277. Procedure TestVarExternalOtherUnit;
  278. Procedure TestVarAbsoluteFail;
  279. Procedure TestConstExternal;
  280. // numbers
  281. Procedure TestDouble;
  282. Procedure TestInteger;
  283. Procedure TestIntegerRange;
  284. Procedure TestIntegerTypecasts;
  285. Procedure TestInteger_BitwiseShrNativeInt;
  286. Procedure TestInteger_BitwiseShlNativeInt;
  287. Procedure TestInteger_SystemFunc;
  288. Procedure TestInteger_AssignOutsideConst;
  289. Procedure TestCurrency;
  290. Procedure TestForBoolDo;
  291. Procedure TestForIntDo;
  292. Procedure TestForIntInDo;
  293. // strings
  294. Procedure TestCharConst;
  295. Procedure TestChar_Compare;
  296. Procedure TestChar_BuiltInProcs;
  297. Procedure TestStringConst;
  298. Procedure TestStringConst_InvalidUTF16;
  299. Procedure TestStringConstSurrogate;
  300. Procedure TestStringConst_Multiline;
  301. Procedure TestString_Length;
  302. Procedure TestString_Compare;
  303. Procedure TestString_SetLength;
  304. Procedure TestString_CharAt;
  305. Procedure TestStringHMinusFail;
  306. Procedure TestStr;
  307. Procedure TestBaseType_AnsiStringFail;
  308. Procedure TestBaseType_WideStringFail;
  309. Procedure TestBaseType_ShortStringFail;
  310. Procedure TestBaseType_RawByteStringFail;
  311. Procedure TestTypeShortstring_Fail;
  312. Procedure TestCharSet_Custom;
  313. Procedure TestWideChar;
  314. Procedure TestForCharDo;
  315. Procedure TestForCharInDo;
  316. // alias types
  317. Procedure TestAliasTypeRef;
  318. Procedure TestTypeCast_BaseTypes;
  319. Procedure TestTypeCast_AliasBaseTypes;
  320. // functions
  321. Procedure TestEmptyProc;
  322. Procedure TestProcOneParam;
  323. Procedure TestFunctionWithoutParams;
  324. Procedure TestProcedureWithoutParams;
  325. Procedure TestPrgProcVar;
  326. Procedure TestProcTwoArgs;
  327. Procedure TestProc_DefaultValue;
  328. Procedure TestUnitProcVar;
  329. Procedure TestImplProc;
  330. Procedure TestFunctionResult;
  331. Procedure TestNestedProc;
  332. Procedure TestNestedProc_ResultString;
  333. Procedure TestForwardProc;
  334. Procedure TestNestedForwardProc;
  335. Procedure TestAssignFunctionResult;
  336. Procedure TestFunctionResultInCondition;
  337. Procedure TestFunctionResultInForLoop;
  338. Procedure TestFunctionResultInTypeCast;
  339. Procedure TestExit;
  340. Procedure TestExit_ResultInFinally;
  341. Procedure TestBreak;
  342. Procedure TestBreakAsVar;
  343. Procedure TestContinue;
  344. Procedure TestProc_External;
  345. Procedure TestProc_ExternalOtherUnit;
  346. Procedure TestProc_Asm;
  347. Procedure TestProc_AsmSubBlock;
  348. Procedure TestProc_Assembler;
  349. Procedure TestProc_VarParam;
  350. Procedure TestProc_VarParamString;
  351. Procedure TestProc_VarParamV;
  352. Procedure TestProc_Overload;
  353. Procedure TestProc_OverloadForward;
  354. Procedure TestProc_OverloadIntfImpl;
  355. Procedure TestProc_OverloadNested;
  356. Procedure TestProc_OverloadNestedForward;
  357. Procedure TestProc_OverloadUnitCycle;
  358. Procedure TestProc_Varargs;
  359. Procedure TestProc_ConstOrder;
  360. Procedure TestProc_DuplicateConst;
  361. Procedure TestProc_LocalVarAbsolute;
  362. Procedure TestProc_ResultAbsolute;
  363. Procedure TestProc_LocalVarInit;
  364. Procedure TestProc_ReservedWords;
  365. Procedure TestProc_ConstRefWord;
  366. // anonymous functions
  367. Procedure TestAnonymousProc_Assign_ObjFPC;
  368. Procedure TestAnonymousProc_Assign_Delphi;
  369. Procedure TestAnonymousProc_Arg;
  370. Procedure TestAnonymousProc_Typecast;
  371. Procedure TestAnonymousProc_With;
  372. Procedure TestAnonymousProc_ExceptOn;
  373. Procedure TestAnonymousProc_Nested;
  374. Procedure TestAnonymousProc_NestedAssignResult;
  375. Procedure TestAnonymousProc_Class;
  376. Procedure TestAnonymousProc_ForLoop;
  377. Procedure TestAnonymousProc_AsmDelphi;
  378. // enums, sets
  379. Procedure TestEnum_Name;
  380. Procedure TestEnum_Number;
  381. Procedure TestEnum_ConstFail;
  382. Procedure TestEnum_Functions;
  383. Procedure TestEnumRg_Functions;
  384. Procedure TestEnum_AsParams;
  385. Procedure TestEnumRange_Array;
  386. Procedure TestEnum_ForIn;
  387. Procedure TestEnum_ScopedNumber;
  388. Procedure TestEnum_InFunction;
  389. Procedure TestEnum_Name_Anonymous_Unit;
  390. Procedure TestSet_Enum;
  391. Procedure TestSet_Operators;
  392. Procedure TestSet_Operator_In;
  393. Procedure TestSet_Functions;
  394. Procedure TestSet_PassAsArgClone;
  395. Procedure TestSet_AsParams;
  396. Procedure TestSet_Property;
  397. Procedure TestSet_EnumConst;
  398. Procedure TestSet_IntConst;
  399. Procedure TestSet_IntRange;
  400. Procedure TestSet_AnonymousEnumType;
  401. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  402. Procedure TestSet_ConstEnum;
  403. Procedure TestSet_ConstChar;
  404. Procedure TestSet_ConstInt;
  405. Procedure TestSet_InFunction;
  406. Procedure TestSet_ForIn;
  407. // statements
  408. Procedure TestNestBegin;
  409. Procedure TestIncDec;
  410. Procedure TestLoHiFpcMode;
  411. Procedure TestLoHiDelphiMode;
  412. Procedure TestAssignments;
  413. Procedure TestArithmeticOperators1;
  414. Procedure TestMultiAdd;
  415. Procedure TestLogicalOperators;
  416. Procedure TestBitwiseOperators;
  417. Procedure TestBitwiseOperatorsLongword;
  418. Procedure TestFunctionInt;
  419. Procedure TestFunctionString;
  420. Procedure TestIfThen;
  421. Procedure TestForLoop;
  422. Procedure TestForLoopInsideFunction;
  423. Procedure TestForLoop_ReadVarAfter;
  424. Procedure TestForLoop_Nested;
  425. Procedure TestRepeatUntil;
  426. Procedure TestAsmBlock;
  427. Procedure TestAsmPas_Impl; // ToDo
  428. Procedure TestTryFinally;
  429. Procedure TestTryExcept;
  430. Procedure TestTryExcept_ReservedWords;
  431. Procedure TestIfThenRaiseElse;
  432. Procedure TestCaseOf;
  433. Procedure TestCaseOf_UseSwitch;
  434. Procedure TestCaseOfNoElse;
  435. Procedure TestCaseOfNoElse_UseSwitch;
  436. Procedure TestCaseOfRange;
  437. Procedure TestCaseOfString;
  438. Procedure TestCaseOfChar;
  439. Procedure TestCaseOfExternalClassConst;
  440. Procedure TestDebugger;
  441. // arrays
  442. Procedure TestArray_Dynamic;
  443. Procedure TestArray_Dynamic_Nil;
  444. Procedure TestArray_DynMultiDimensional;
  445. Procedure TestArray_DynamicAssign;
  446. Procedure TestArray_StaticInt;
  447. Procedure TestArray_StaticBool;
  448. Procedure TestArray_StaticChar;
  449. Procedure TestArray_StaticMultiDim;
  450. Procedure TestArray_StaticInFunction;
  451. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  452. Procedure TestArrayOfRecord;
  453. Procedure TestArray_StaticRecord;
  454. Procedure TestArrayOfSet;
  455. Procedure TestArray_DynAsParam;
  456. Procedure TestArray_StaticAsParam;
  457. Procedure TestArrayElement_AsParams;
  458. Procedure TestArrayElementFromFuncResult_AsParams;
  459. Procedure TestArrayEnumTypeRange;
  460. Procedure TestArray_SetLengthOutArg;
  461. Procedure TestArray_SetLengthProperty;
  462. Procedure TestArray_SetLengthMultiDim;
  463. Procedure TestArray_SetLengthDynOfStatic;
  464. Procedure TestArray_OpenArrayOfString;
  465. Procedure TestArray_ArrayOfCharAssignString;
  466. Procedure TestArray_ConstRef;
  467. Procedure TestArray_Concat;
  468. Procedure TestArray_Copy;
  469. Procedure TestArray_InsertDelete;
  470. Procedure TestArray_DynArrayConstObjFPC;
  471. Procedure TestArray_DynArrayConstDelphi;
  472. Procedure TestArray_ArrayLitAsParam;
  473. Procedure TestArray_ArrayLitMultiDimAsParam;
  474. Procedure TestArray_ArrayLitStaticAsParam;
  475. Procedure TestArray_ForInArrOfString;
  476. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  477. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  478. Procedure TestArrayOfConst_TVarRec;
  479. Procedure TestArrayOfConst_PassBaseTypes;
  480. Procedure TestArrayOfConst_PassObj;
  481. // record
  482. Procedure TestRecord_Empty;
  483. Procedure TestRecord_Var;
  484. Procedure TestRecord_VarExternal;
  485. Procedure TestRecord_WithDo;
  486. Procedure TestRecord_Assign;
  487. Procedure TestRecord_AsParams;
  488. Procedure TestRecord_ConstRef;
  489. Procedure TestRecordElement_AsParams;
  490. Procedure TestRecordElementFromFuncResult_AsParams;
  491. Procedure TestRecordElementFromWith_AsParams;
  492. Procedure TestRecord_Equal;
  493. Procedure TestRecord_JSValue;
  494. Procedure TestRecord_VariantFail;
  495. Procedure TestRecord_FieldArray;
  496. Procedure TestRecord_Const;
  497. Procedure TestRecord_TypecastFail;
  498. Procedure TestRecord_InFunction;
  499. // anonymous record
  500. Procedure TestRecordAnonym_Field;
  501. Procedure TestRecordAnonym_Assign;
  502. Procedure TestRecordAnonym_Nested;
  503. Procedure TestRecordAnonym_Const;
  504. Procedure TestRecordAnonym_InFunction;
  505. // advanced record
  506. Procedure TestAdvRecord_Function;
  507. Procedure TestAdvRecord_Property;
  508. Procedure TestAdvRecord_PropertyDefault;
  509. Procedure TestAdvRecord_Property_ClassMethod;
  510. Procedure TestAdvRecord_Const;
  511. Procedure TestAdvRecord_ExternalField;
  512. Procedure TestAdvRecord_SubRecord;
  513. Procedure TestAdvRecord_SubClass;
  514. Procedure TestAdvRecord_SubInterfaceFail;
  515. Procedure TestAdvRecord_Constructor;
  516. Procedure TestAdvRecord_ClassConstructor_Program;
  517. Procedure TestAdvRecord_ClassConstructor_Unit;
  518. // classes
  519. Procedure TestClass_TObjectDefaultConstructor;
  520. Procedure TestClass_TObjectConstructorWithParams;
  521. Procedure TestClass_TObjectConstructorWithDefaultParam;
  522. Procedure TestClass_Var;
  523. Procedure TestClass_Method;
  524. Procedure TestClass_Implementation;
  525. Procedure TestClass_Inheritance;
  526. Procedure TestClass_TypeAlias;
  527. Procedure TestClass_AbstractMethod;
  528. Procedure TestClass_CallInherited_ProcNoParams;
  529. Procedure TestClass_CallInherited_WithParams;
  530. Procedure TestClasS_CallInheritedConstructor;
  531. Procedure TestClass_ClassVar_Assign;
  532. Procedure TestClass_CallClassMethod;
  533. Procedure TestClass_CallClassMethodStatic;
  534. Procedure TestClass_Property;
  535. Procedure TestClass_Property_ClassMethod;
  536. Procedure TestClass_Property_ClassMethodStatic;
  537. Procedure TestClass_Property_Indexed;
  538. Procedure TestClass_Property_IndexSpec;
  539. Procedure TestClass_PropertyOfTypeArray;
  540. Procedure TestClass_PropertyDefault;
  541. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  542. //Procedure TestClass_PropertyDefault;
  543. Procedure TestClass_PropertyOverride;
  544. Procedure TestClass_PropertyIncVisibility;
  545. Procedure TestClass_Assigned;
  546. Procedure TestClass_WithClassDoCreate;
  547. Procedure TestClass_WithClassInstDoProperty;
  548. Procedure TestClass_WithClassInstDoPropertyWithParams;
  549. Procedure TestClass_WithClassInstDoFunc;
  550. Procedure TestClass_TypeCast;
  551. Procedure TestClass_TypeCastUntypedParam;
  552. Procedure TestClass_Overloads;
  553. Procedure TestClass_OverloadsAncestor;
  554. Procedure TestClass_OverloadConstructor;
  555. Procedure TestClass_OverloadDelphiOverride;
  556. Procedure TestClass_ReintroduceVarDelphi;
  557. Procedure TestClass_ReintroducedVar;
  558. Procedure TestClass_RaiseDescendant;
  559. Procedure TestClass_ExternalMethod;
  560. Procedure TestClass_ExternalVirtualNameMismatchFail;
  561. Procedure TestClass_ExternalOverrideFail;
  562. Procedure TestClass_ExternalVar;
  563. Procedure TestClass_Const;
  564. Procedure TestClass_ConstEnum;
  565. Procedure TestClass_LocalConstDuplicate_Prg;
  566. Procedure TestClass_LocalConstDuplicate_Unit;
  567. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  568. Procedure TestClass_LocalVarSelfFail;
  569. Procedure TestClass_ArgSelfFail;
  570. Procedure TestClass_NestedProcSelf;
  571. Procedure TestClass_NestedProcSelf2;
  572. Procedure TestClass_NestedProcClassSelf;
  573. Procedure TestClass_NestedProcCallInherited;
  574. Procedure TestClass_TObjectFree;
  575. Procedure TestClass_TObjectFree_VarArg;
  576. Procedure TestClass_TObjectFreeNewInstance;
  577. Procedure TestClass_TObjectFreeLowerCase;
  578. Procedure TestClass_TObjectFreeFunctionFail;
  579. Procedure TestClass_TObjectFreePropertyFail;
  580. Procedure TestClass_ForIn;
  581. Procedure TestClass_DispatchMessage;
  582. Procedure TestClass_Message_DuplicateIntFail;
  583. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  584. // class of
  585. Procedure TestClassOf_Create;
  586. Procedure TestClassOf_Call;
  587. Procedure TestClassOf_Assign;
  588. Procedure TestClassOf_Is;
  589. Procedure TestClassOf_Compare;
  590. Procedure TestClassOf_ClassVar;
  591. Procedure TestClassOf_ClassMethod;
  592. Procedure TestClassOf_ClassProperty;
  593. Procedure TestClassOf_ClassMethodSelf;
  594. Procedure TestClassOf_TypeCast;
  595. Procedure TestClassOf_ImplicitFunctionCall;
  596. Procedure TestClassOf_Const;
  597. // nested class
  598. Procedure TestNestedClass_Alias;
  599. Procedure TestNestedClass_Record;
  600. Procedure TestNestedClass_Class;
  601. Procedure TestNestedClass_CallInherited;
  602. // external class
  603. Procedure TestExternalClass_Var;
  604. Procedure TestExternalClass_Const;
  605. Procedure TestExternalClass_Dollar;
  606. Procedure TestExternalClass_DuplicateVarFail;
  607. Procedure TestExternalClass_Method;
  608. Procedure TestExternalClass_ClassMethod;
  609. Procedure TestExternalClass_ClassMethodStatic;
  610. Procedure TestExternalClass_FunctionResultInTypeCast;
  611. Procedure TestExternalClass_NonExternalOverride;
  612. Procedure TestExternalClass_OverloadHint;
  613. Procedure TestExternalClass_SameNamePublishedProperty;
  614. Procedure TestExternalClass_Property;
  615. Procedure TestExternalClass_PropertyDate;
  616. Procedure TestExternalClass_ClassProperty;
  617. Procedure TestExternalClass_ClassOf;
  618. Procedure TestExternalClass_ClassOtherUnit;
  619. Procedure TestExternalClass_Is;
  620. Procedure TestExternalClass_As;
  621. Procedure TestExternalClass_DestructorFail;
  622. Procedure TestExternalClass_New;
  623. Procedure TestExternalClass_ClassOf_New;
  624. Procedure TestExternalClass_FuncClassOf_New;
  625. Procedure TestExternalClass_New_PasClassFail;
  626. Procedure TestExternalClass_New_PasClassBracketsFail;
  627. Procedure TestExternalClass_NewExtName;
  628. Procedure TestExternalClass_Constructor;
  629. Procedure TestExternalClass_ConstructorBrackets;
  630. Procedure TestExternalClass_LocalConstSameName;
  631. Procedure TestExternalClass_ReintroduceOverload;
  632. Procedure TestExternalClass_Inherited;
  633. Procedure TestExternalClass_PascalAncestorFail;
  634. Procedure TestExternalClass_NewInstance;
  635. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  636. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  637. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  638. Procedure TestExternalClass_JSFunctionPasDescendant;
  639. Procedure TestExternalClass_PascalProperty;
  640. Procedure TestExternalClass_TypeCastToRootClass;
  641. Procedure TestExternalClass_TypeCastToJSObject;
  642. Procedure TestExternalClass_TypeCastStringToExternalString;
  643. Procedure TestExternalClass_TypeCastToJSFunction;
  644. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  645. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  646. Procedure TestExternalClass_BracketAccessor;
  647. Procedure TestExternalClass_BracketAccessor_Call;
  648. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  649. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  650. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  651. Procedure TestExternalClass_BracketAccessor_MultiType;
  652. Procedure TestExternalClass_BracketAccessor_Index;
  653. Procedure TestExternalClass_ForInJSObject;
  654. Procedure TestExternalClass_ForInJSArray;
  655. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  656. Procedure TestExternalClass_NestedConstructor;
  657. // class interfaces
  658. Procedure TestClassInterface_Corba;
  659. Procedure TestClassInterface_ProcExternalFail;
  660. Procedure TestClassInterface_Overloads;
  661. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  662. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  663. Procedure TestClassInterface_AncestorImpl;
  664. Procedure TestClassInterface_ImplReintroduce;
  665. Procedure TestClassInterface_MethodResolution;
  666. Procedure TestClassInterface_AncestorMoreInterfaces;
  667. Procedure TestClassInterface_MethodOverride;
  668. Procedure TestClassInterface_Corba_Delegation;
  669. Procedure TestClassInterface_Corba_DelegationStatic;
  670. Procedure TestClassInterface_Corba_Operators;
  671. Procedure TestClassInterface_Corba_Args;
  672. Procedure TestClassInterface_Corba_ForIn;
  673. Procedure TestClassInterface_COM_AssignVar;
  674. Procedure TestClassInterface_COM_AssignArg;
  675. Procedure TestClassInterface_COM_FunctionResult;
  676. Procedure TestClassInterface_COM_InheritedFuncResult;
  677. Procedure TestClassInterface_COM_IsAsTypeCasts;
  678. Procedure TestClassInterface_COM_PassAsArg;
  679. Procedure TestClassInterface_COM_PassToUntypedParam;
  680. Procedure TestClassInterface_COM_FunctionInExpr;
  681. Procedure TestClassInterface_COM_Property;
  682. Procedure TestClassInterface_COM_IntfProperty;
  683. Procedure TestClassInterface_COM_Delegation;
  684. Procedure TestClassInterface_COM_With;
  685. Procedure TestClassInterface_COM_ForIn;
  686. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  687. Procedure TestClassInterface_COM_RecordIntfFail;
  688. Procedure TestClassInterface_COM_UnitInitialization;
  689. Procedure TestClassInterface_GUID;
  690. Procedure TestClassInterface_GUIDProperty;
  691. // helpers
  692. Procedure TestClassHelper_ClassVar;
  693. Procedure TestClassHelper_Method_AccessInstanceFields;
  694. Procedure TestClassHelper_Method_Call;
  695. Procedure TestClassHelper_Method_Nested_Call;
  696. Procedure TestClassHelper_ClassMethod_Call;
  697. Procedure TestClassHelper_ClassOf;
  698. Procedure TestClassHelper_MethodRefObjFPC;
  699. Procedure TestClassHelper_Constructor;
  700. Procedure TestClassHelper_InheritedObjFPC;
  701. Procedure TestClassHelper_Property;
  702. Procedure TestClassHelper_Property_Array;
  703. Procedure TestClassHelper_Property_Array_Default;
  704. Procedure TestClassHelper_Property_Array_DefaultDefault;
  705. Procedure TestClassHelper_ClassProperty;
  706. Procedure TestClassHelper_ClassPropertyStatic;
  707. Procedure TestClassHelper_ClassProperty_Array;
  708. Procedure TestClassHelper_ForIn;
  709. Procedure TestClassHelper_PassProperty;
  710. Procedure TestExtClassHelper_ClassVar;
  711. Procedure TestExtClassHelper_Method_Call;
  712. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  713. Procedure TestRecordHelper_ClassVar;
  714. Procedure TestRecordHelper_Method_Call;
  715. Procedure TestRecordHelper_Constructor;
  716. Procedure TestTypeHelper_ClassVar;
  717. Procedure TestTypeHelper_PassResultElement;
  718. Procedure TestTypeHelper_PassArgs;
  719. Procedure TestTypeHelper_PassVarConst;
  720. Procedure TestTypeHelper_PassFuncResult;
  721. Procedure TestTypeHelper_PassPropertyField;
  722. Procedure TestTypeHelper_PassPropertyGetter;
  723. Procedure TestTypeHelper_PassClassPropertyField;
  724. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  725. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  726. Procedure TestTypeHelper_Property;
  727. Procedure TestTypeHelper_Property_Array;
  728. Procedure TestTypeHelper_ClassProperty;
  729. Procedure TestTypeHelper_ClassProperty_Array;
  730. Procedure TestTypeHelper_ClassMethod;
  731. Procedure TestTypeHelper_ExtClassMethodFail;
  732. Procedure TestTypeHelper_Constructor;
  733. Procedure TestTypeHelper_Word;
  734. Procedure TestTypeHelper_Boolean;
  735. Procedure TestTypeHelper_WordBool;
  736. Procedure TestTypeHelper_Double;
  737. Procedure TestTypeHelper_NativeInt;
  738. Procedure TestTypeHelper_StringChar;
  739. Procedure TestTypeHelper_JSValue;
  740. Procedure TestTypeHelper_Array;
  741. Procedure TestTypeHelper_EnumType;
  742. Procedure TestTypeHelper_SetType;
  743. Procedure TestTypeHelper_InterfaceType;
  744. Procedure TestTypeHelper_NestedSelf;
  745. // proc types
  746. Procedure TestProcType;
  747. Procedure TestProcType_Arg;
  748. Procedure TestProcType_FunctionFPC;
  749. Procedure TestProcType_FunctionDelphi;
  750. Procedure TestProcType_ProcedureDelphi;
  751. Procedure TestProcType_AsParam;
  752. Procedure TestProcType_MethodFPC;
  753. Procedure TestProcType_MethodDelphi;
  754. Procedure TestProcType_PropertyFPC;
  755. Procedure TestProcType_PropertyDelphi;
  756. Procedure TestProcType_WithClassInstDoPropertyFPC;
  757. Procedure TestProcType_Nested;
  758. Procedure TestProcType_NestedOfObject;
  759. Procedure TestProcType_ReferenceToProc;
  760. Procedure TestProcType_ReferenceToMethod;
  761. Procedure TestProcType_Typecast;
  762. Procedure TestProcType_PassProcToUntyped;
  763. Procedure TestProcType_PassProcToArray;
  764. Procedure TestProcType_SafeCallObjFPC;
  765. Procedure TestProcType_SafeCallDelphi;
  766. // pointer
  767. Procedure TestPointer;
  768. Procedure TestPointer_Proc;
  769. Procedure TestPointer_AssignRecordFail;
  770. Procedure TestPointer_AssignStaticArrayFail;
  771. Procedure TestPointer_TypeCastJSValueToPointer;
  772. Procedure TestPointer_NonRecordFail;
  773. Procedure TestPointer_AnonymousArgTypeFail;
  774. Procedure TestPointer_AnonymousVarTypeFail;
  775. Procedure TestPointer_AnonymousResultTypeFail;
  776. Procedure TestPointer_AddrOperatorFail;
  777. Procedure TestPointer_ArrayParamsFail;
  778. Procedure TestPointer_PointerAddFail;
  779. Procedure TestPointer_IncPointerFail;
  780. Procedure TestPointer_Record;
  781. Procedure TestPointer_RecordArg;
  782. // jsvalue
  783. Procedure TestJSValue_AssignToJSValue;
  784. Procedure TestJSValue_TypeCastToBaseType;
  785. Procedure TestJSValue_TypecastToJSValue;
  786. Procedure TestJSValue_Equal;
  787. Procedure TestJSValue_If;
  788. Procedure TestJSValue_Not;
  789. Procedure TestJSValue_Enum;
  790. Procedure TestJSValue_ClassInstance;
  791. Procedure TestJSValue_ClassOf;
  792. Procedure TestJSValue_ArrayOfJSValue;
  793. Procedure TestJSValue_ArrayLit;
  794. Procedure TestJSValue_Params;
  795. Procedure TestJSValue_UntypedParam;
  796. Procedure TestJSValue_FuncResultType;
  797. Procedure TestJSValue_ProcType_Assign;
  798. Procedure TestJSValue_ProcType_Equal;
  799. Procedure TestJSValue_ProcType_Param;
  800. Procedure TestJSValue_AssignToPointerFail;
  801. Procedure TestJSValue_OverloadDouble;
  802. Procedure TestJSValue_OverloadNativeInt;
  803. Procedure TestJSValue_OverloadWord;
  804. Procedure TestJSValue_OverloadString;
  805. Procedure TestJSValue_OverloadChar;
  806. Procedure TestJSValue_OverloadPointer;
  807. Procedure TestJSValue_ForIn;
  808. // RTTI
  809. Procedure TestRTTI_IntRange;
  810. Procedure TestRTTI_Double;
  811. Procedure TestRTTI_ProcType;
  812. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  813. Procedure TestRTTI_ProcTypeAnonymous;
  814. Procedure TestRTTI_EnumAndSetType;
  815. Procedure TestRTTI_EnumRange;
  816. Procedure TestRTTI_AnonymousEnumType;
  817. Procedure TestRTTI_StaticArray;
  818. Procedure TestRTTI_DynArray;
  819. Procedure TestRTTI_ArrayNestedAnonymous;
  820. Procedure TestRTTI_PublishedMethodOverloadFail;
  821. Procedure TestRTTI_PublishedMethodHideNoHint;
  822. Procedure TestRTTI_PublishedMethodExternalFail;
  823. Procedure TestRTTI_PublishedClassPropertyFail;
  824. Procedure TestRTTI_PublishedClassFieldFail;
  825. Procedure TestRTTI_PublishedFieldExternalFail;
  826. Procedure TestRTTI_Class_Field;
  827. Procedure TestRTTI_Class_Method;
  828. Procedure TestRTTI_Class_MethodArgFlags;
  829. Procedure TestRTTI_Class_Property;
  830. Procedure TestRTTI_Class_PropertyParams;
  831. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  832. Procedure TestRTTI_Class_OmitRTTI;
  833. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  834. Procedure TestRTTI_IndexModifier;
  835. Procedure TestRTTI_StoredModifier;
  836. Procedure TestRTTI_DefaultValue;
  837. Procedure TestRTTI_DefaultValueSet;
  838. Procedure TestRTTI_DefaultValueRangeType;
  839. Procedure TestRTTI_DefaultValueInherit;
  840. Procedure TestRTTI_OverrideMethod;
  841. Procedure TestRTTI_ReintroduceMethod;
  842. Procedure TestRTTI_OverloadProperty;
  843. // ToDo: array argument
  844. Procedure TestRTTI_ClassForward;
  845. Procedure TestRTTI_ClassOf;
  846. Procedure TestRTTI_Record;
  847. Procedure TestRTTI_RecordAnonymousArray;
  848. Procedure TestRTTI_Record_ClassVarType;
  849. Procedure TestRTTI_LocalTypes;
  850. Procedure TestRTTI_TypeInfo_BaseTypes;
  851. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  852. Procedure TestRTTI_TypeInfo_LocalFail;
  853. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  854. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  855. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  856. Procedure TestRTTI_TypeInfo_FunctionClassType;
  857. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  858. Procedure TestRTTI_Interface_Corba;
  859. Procedure TestRTTI_Interface_COM;
  860. Procedure TestRTTI_ClassHelper;
  861. Procedure TestRTTI_ExternalClass;
  862. Procedure TestRTTI_Unit;
  863. // Resourcestring
  864. Procedure TestResourcestringProgram;
  865. Procedure TestResourcestringUnit;
  866. Procedure TestResourcestringImplementation;
  867. // Attributes
  868. Procedure TestAttributes_Members;
  869. Procedure TestAttributes_Types;
  870. Procedure TestAttributes_HelperConstructor_Fail;
  871. Procedure TestAttributes_InterfacesList;
  872. // Assertions, checks
  873. procedure TestAssert;
  874. procedure TestAssert_SysUtils;
  875. procedure TestObjectChecks;
  876. procedure TestOverflowChecks_Int;
  877. procedure TestRangeChecks_AssignInt;
  878. procedure TestRangeChecks_AssignIntRange;
  879. procedure TestRangeChecks_AssignEnum;
  880. procedure TestRangeChecks_AssignEnumRange;
  881. procedure TestRangeChecks_AssignChar;
  882. procedure TestRangeChecks_AssignCharRange;
  883. procedure TestRangeChecks_ArrayIndex;
  884. procedure TestRangeChecks_ArrayOfRecIndex;
  885. procedure TestRangeChecks_StringIndex;
  886. procedure TestRangeChecks_TypecastInt;
  887. procedure TestRangeChecks_TypeHelperInt;
  888. // Async/AWait
  889. Procedure TestAsync_Proc;
  890. Procedure TestAsync_CallResultIsPromise;
  891. Procedure TestAsync_ConstructorFail;
  892. Procedure TestAsync_PropertyGetterFail;
  893. Procedure TestAwait_NonPromiseWithTypeFail;
  894. Procedure TestAwait_AsyncCallTypeMismatch;
  895. Procedure TestAWait_OutsideAsyncFail;
  896. Procedure TestAWait_IntegerFail;
  897. Procedure TestAWait_ExternalClassPromise;
  898. Procedure TestAWait_JSValue;
  899. Procedure TestAWait_Result;
  900. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  901. Procedure TestAsync_AnonymousProc;
  902. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  903. Procedure TestAsync_ProcType;
  904. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  905. Procedure TestAsync_Inherited;
  906. Procedure TestAsync_ClassInterface;
  907. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  908. Procedure TestAWait_ClassAs;
  909. // Library
  910. Procedure TestLibrary_Empty;
  911. Procedure TestLibrary_ExportFunc;
  912. Procedure TestLibrary_ExportFuncOverloadedFail;
  913. Procedure TestLibrary_Export_Index_Fail;
  914. Procedure TestLibrary_ExportVar;
  915. Procedure TestLibrary_ExportUnitFunc;
  916. end;
  917. function LinesToStr(Args: array of const): string;
  918. function ExtractFileUnitName(aFilename: string): string;
  919. function JSToStr(El: TJSElement): string;
  920. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  921. implementation
  922. function LinesToStr(Args: array of const): string;
  923. var
  924. s: String;
  925. i: Integer;
  926. begin
  927. s:='';
  928. for i:=Low(Args) to High(Args) do
  929. case Args[i].VType of
  930. vtChar: s += Args[i].VChar+LineEnding;
  931. vtString: s += Args[i].VString^+LineEnding;
  932. vtPChar: s += Args[i].VPChar+LineEnding;
  933. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  934. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  935. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  936. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  937. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  938. end;
  939. Result:=s;
  940. end;
  941. function ExtractFileUnitName(aFilename: string): string;
  942. var
  943. p: Integer;
  944. begin
  945. Result:=ExtractFileName(aFilename);
  946. if Result='' then exit;
  947. for p:=length(Result) downto 1 do
  948. case Result[p] of
  949. '/','\': exit;
  950. '.':
  951. begin
  952. Delete(Result,p,length(Result));
  953. exit;
  954. end;
  955. end;
  956. end;
  957. function JSToStr(El: TJSElement): string;
  958. var
  959. aWriter: TBufferWriter;
  960. aJSWriter: TJSWriter;
  961. begin
  962. aJSWriter:=nil;
  963. aWriter:=TBufferWriter.Create(1000);
  964. try
  965. aJSWriter:=TJSWriter.Create(aWriter);
  966. aJSWriter.IndentSize:=2;
  967. aJSWriter.WriteJS(El);
  968. Result:=aWriter.AsString;
  969. finally
  970. aJSWriter.Free;
  971. aWriter.Free;
  972. end;
  973. end;
  974. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  975. // search diff, ignore changes in spaces
  976. const
  977. SpaceChars = [#9,#10,#13,' '];
  978. var
  979. ExpectedP, ActualP: PChar;
  980. function FindLineEnd(p: PChar): PChar;
  981. begin
  982. Result:=p;
  983. while not (Result^ in [#0,#10,#13]) do inc(Result);
  984. end;
  985. function FindLineStart(p, MinP: PChar): PChar;
  986. begin
  987. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  988. Result:=p;
  989. end;
  990. procedure SkipLineEnd(var p: PChar);
  991. begin
  992. if p^ in [#10,#13] then
  993. begin
  994. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  995. inc(p,2)
  996. else
  997. inc(p);
  998. end;
  999. end;
  1000. function HasSpecialChar(s: string): boolean;
  1001. var
  1002. i: Integer;
  1003. begin
  1004. for i:=1 to length(s) do
  1005. if s[i] in [#0..#31,#127..#255] then
  1006. exit(true);
  1007. Result:=false;
  1008. end;
  1009. function HashSpecialChars(s: string): string;
  1010. var
  1011. i: Integer;
  1012. begin
  1013. Result:='';
  1014. for i:=1 to length(s) do
  1015. if s[i] in [#0..#31,#127..#255] then
  1016. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1017. else
  1018. Result:=Result+s[i];
  1019. end;
  1020. procedure DiffFound;
  1021. var
  1022. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1023. ExpLine, ActLine: String;
  1024. i, LineNo, DiffLineNo: Integer;
  1025. begin
  1026. writeln('Diff found "',Msg,'". Lines:');
  1027. // write correct lines
  1028. p:=PChar(Expected);
  1029. LineNo:=0;
  1030. DiffLineNo:=0;
  1031. repeat
  1032. StartPos:=p;
  1033. while not (p^ in [#0,#10,#13]) do inc(p);
  1034. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1035. SkipLineEnd(p);
  1036. inc(LineNo);
  1037. if (p<=ExpectedP) and (p^<>#0) then
  1038. begin
  1039. writeln('= ',ExpLine);
  1040. end else begin
  1041. // diff line
  1042. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1043. // write actual line
  1044. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1045. ActLineEndP:=FindLineEnd(ActualP);
  1046. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1047. writeln('- ',ActLine);
  1048. if HasSpecialChar(ActLine) then
  1049. writeln('- ',HashSpecialChars(ActLine));
  1050. // write expected line
  1051. writeln('+ ',ExpLine);
  1052. if HasSpecialChar(ExpLine) then
  1053. writeln('- ',HashSpecialChars(ExpLine));
  1054. // write empty line with pointer ^
  1055. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1056. writeln('^');
  1057. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1058. CheckSrcDiff:=false;
  1059. // write up to three following actual lines to get some context
  1060. for i:=1 to 3 do begin
  1061. ActLineStartP:=ActLineEndP;
  1062. SkipLineEnd(ActLineStartP);
  1063. if ActLineStartP^=#0 then break;
  1064. ActLineEndP:=FindLineEnd(ActLineStartP);
  1065. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1066. writeln('~ ',ActLine);
  1067. end;
  1068. exit;
  1069. end;
  1070. until p^=#0;
  1071. writeln('DiffFound Actual:-----------------------');
  1072. writeln(Actual);
  1073. writeln('DiffFound Expected:---------------------');
  1074. writeln(Expected);
  1075. writeln('DiffFound ------------------------------');
  1076. Msg:='diff found, but lines are the same, internal error';
  1077. CheckSrcDiff:=false;
  1078. end;
  1079. var
  1080. IsSpaceNeeded: Boolean;
  1081. LastChar, Quote: Char;
  1082. begin
  1083. Result:=true;
  1084. Msg:='';
  1085. if Expected='' then Expected:=' ';
  1086. if Actual='' then Actual:=' ';
  1087. ExpectedP:=PChar(Expected);
  1088. ActualP:=PChar(Actual);
  1089. repeat
  1090. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1091. case ExpectedP^ of
  1092. #0:
  1093. begin
  1094. // check that rest of Actual has only spaces
  1095. while ActualP^ in SpaceChars do inc(ActualP);
  1096. if ActualP^<>#0 then
  1097. begin
  1098. DiffFound;
  1099. exit;
  1100. end;
  1101. exit(true);
  1102. end;
  1103. ' ',#9,#10,#13:
  1104. begin
  1105. // skip space in Expected
  1106. IsSpaceNeeded:=false;
  1107. if ExpectedP>PChar(Expected) then
  1108. LastChar:=ExpectedP[-1]
  1109. else
  1110. LastChar:=#0;
  1111. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1112. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1113. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1114. IsSpaceNeeded:=true;
  1115. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1116. begin
  1117. DiffFound;
  1118. exit;
  1119. end;
  1120. while ActualP^ in SpaceChars do inc(ActualP);
  1121. end;
  1122. '''','"':
  1123. begin
  1124. while ActualP^ in SpaceChars do inc(ActualP);
  1125. if ExpectedP^<>ActualP^ then
  1126. begin
  1127. DiffFound;
  1128. exit;
  1129. end;
  1130. Quote:=ExpectedP^;
  1131. repeat
  1132. inc(ExpectedP);
  1133. inc(ActualP);
  1134. if ExpectedP^<>ActualP^ then
  1135. begin
  1136. DiffFound;
  1137. exit;
  1138. end;
  1139. if (ExpectedP^ in [#0,#10,#13]) then
  1140. break
  1141. else if (ExpectedP^=Quote) then
  1142. begin
  1143. inc(ExpectedP);
  1144. inc(ActualP);
  1145. break;
  1146. end;
  1147. until false;
  1148. end;
  1149. else
  1150. while ActualP^ in SpaceChars do inc(ActualP);
  1151. if ExpectedP^<>ActualP^ then
  1152. begin
  1153. DiffFound;
  1154. exit;
  1155. end;
  1156. inc(ExpectedP);
  1157. inc(ActualP);
  1158. end;
  1159. until false;
  1160. end;
  1161. { TTestEnginePasResolver }
  1162. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1163. begin
  1164. if FModule=AValue then Exit;
  1165. FModule:=AValue;
  1166. end;
  1167. destructor TTestEnginePasResolver.Destroy;
  1168. begin
  1169. FreeAndNil(FStreamResolver);
  1170. FreeAndNil(FParser);
  1171. FreeAndNil(FScanner);
  1172. FreeAndNil(FStreamResolver);
  1173. Module:=nil;
  1174. inherited Destroy;
  1175. end;
  1176. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1177. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1178. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1179. begin
  1180. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1181. TypeParams);
  1182. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1183. Module:=TPasModule(Result);
  1184. end;
  1185. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1186. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1187. begin
  1188. Result:=nil;
  1189. if InFilename<>'' then
  1190. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1191. if Assigned(OnFindUnit) then
  1192. Result:=OnFindUnit(AName);
  1193. if NameExpr=nil then ;
  1194. end;
  1195. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1196. begin
  1197. // do not parse recursively
  1198. // parse via the queue
  1199. if Section=nil then ;
  1200. end;
  1201. { TCustomTestModule }
  1202. procedure TCustomTestModule.FreeSrcMarkers;
  1203. var
  1204. aMarker, Last: PSrcMarker;
  1205. begin
  1206. aMarker:=FirstSrcMarker;
  1207. while aMarker<>nil do
  1208. begin
  1209. Last:=aMarker;
  1210. aMarker:=aMarker^.Next;
  1211. Dispose(Last);
  1212. end;
  1213. FirstSrcMarker:=nil;
  1214. LastSrcMarker:=nil;
  1215. end;
  1216. function TCustomTestModule.GetResolverCount: integer;
  1217. begin
  1218. Result:=FResolvers.Count;
  1219. end;
  1220. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1221. begin
  1222. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1223. end;
  1224. function TCustomTestModule.GetMsgCount: integer;
  1225. begin
  1226. Result:=FHintMsgs.Count;
  1227. end;
  1228. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1229. begin
  1230. Result:=TTestHintMessage(FHintMsgs[Index]);
  1231. end;
  1232. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1233. ): TPasModule;
  1234. var
  1235. DefNamespace: String;
  1236. begin
  1237. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1238. if (Pos('.',aUnitName)<1) then
  1239. begin
  1240. DefNamespace:=GetDefaultNamespace;
  1241. if DefNamespace<>'' then
  1242. begin
  1243. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1244. if Result<>nil then exit;
  1245. end;
  1246. end;
  1247. Result:=LoadUnit(aUnitName);
  1248. if Result<>nil then exit;
  1249. {$IFDEF VerbosePas2JS}
  1250. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1251. {$ENDIF}
  1252. Fail('can''t find unit "'+aUnitName+'"');
  1253. end;
  1254. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1255. var
  1256. aParser: TPasParser;
  1257. Item: TTestHintMessage;
  1258. begin
  1259. aParser:=Sender as TPasParser;
  1260. Item:=TTestHintMessage.Create;
  1261. Item.Id:=aParser.LastMsgNumber;
  1262. Item.MsgType:=aParser.LastMsgType;
  1263. Item.MsgNumber:=aParser.LastMsgNumber;
  1264. Item.Msg:=Msg;
  1265. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1266. {$IFDEF VerbosePas2JS}
  1267. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1268. {$ENDIF}
  1269. FHintMsgs.Add(Item);
  1270. end;
  1271. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1272. );
  1273. var
  1274. aResolver: TTestEnginePasResolver;
  1275. Item: TTestHintMessage;
  1276. begin
  1277. aResolver:=Sender as TTestEnginePasResolver;
  1278. Item:=TTestHintMessage.Create;
  1279. Item.Id:=aResolver.LastMsgId;
  1280. Item.MsgType:=aResolver.LastMsgType;
  1281. Item.MsgNumber:=aResolver.LastMsgNumber;
  1282. Item.Msg:=Msg;
  1283. Item.SourcePos:=aResolver.LastSourcePos;
  1284. {$IFDEF VerbosePas2JS}
  1285. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1286. {$ENDIF}
  1287. FHintMsgs.Add(Item);
  1288. end;
  1289. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1290. var
  1291. Item: TTestHintMessage;
  1292. aScanner: TPas2jsPasScanner;
  1293. begin
  1294. aScanner:=Sender as TPas2jsPasScanner;
  1295. Item:=TTestHintMessage.Create;
  1296. Item.Id:=aScanner.LastMsgNumber;
  1297. Item.MsgType:=aScanner.LastMsgType;
  1298. Item.MsgNumber:=aScanner.LastMsgNumber;
  1299. Item.Msg:=Msg;
  1300. Item.SourcePos:=aScanner.CurSourcePos;
  1301. {$IFDEF VerbosePas2JS}
  1302. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1303. {$ENDIF}
  1304. FHintMsgs.Add(Item);
  1305. end;
  1306. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1307. var
  1308. SubEl: TPasElement;
  1309. i: Integer;
  1310. procedure E(Msg: string);
  1311. var
  1312. s: String;
  1313. begin
  1314. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1315. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1316. writeln('ERROR: ',s);
  1317. Fail(s);
  1318. end;
  1319. begin
  1320. if arg=nil then ;
  1321. if El=nil then exit;
  1322. if El.Parent=El then
  1323. E('El.Parent=El='+GetObjName(El));
  1324. if El is TBinaryExpr then
  1325. begin
  1326. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1327. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1328. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1329. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1330. end
  1331. else if El is TParamsExpr then
  1332. begin
  1333. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1334. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1335. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1336. if TParamsExpr(El).Params[i].Parent<>El then
  1337. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1338. end
  1339. else if El is TProcedureExpr then
  1340. begin
  1341. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1342. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1343. end
  1344. else if El is TPasDeclarations then
  1345. begin
  1346. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1347. begin
  1348. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1349. if SubEl.Parent<>El then
  1350. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1351. end;
  1352. end
  1353. else if El is TPasImplBlock then
  1354. begin
  1355. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1356. begin
  1357. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1358. if SubEl.Parent<>El then
  1359. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1360. end;
  1361. end
  1362. else if El is TPasImplWithDo then
  1363. begin
  1364. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1365. begin
  1366. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1367. if SubEl.Parent<>El then
  1368. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1369. end;
  1370. end
  1371. else if El is TPasProcedure then
  1372. begin
  1373. if TPasProcedure(El).ProcType.Parent<>El then
  1374. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1375. end
  1376. else if El is TPasProcedureType then
  1377. begin
  1378. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1379. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1380. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1381. end;
  1382. end;
  1383. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1384. var
  1385. Data: PTestResolverReferenceData absolute FindData;
  1386. Line, Col: integer;
  1387. begin
  1388. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1389. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1390. if (Data^.Filename=El.SourceFilename)
  1391. and (Data^.Row=Line)
  1392. and (Data^.StartCol<=Col)
  1393. and (Data^.EndCol>=Col)
  1394. then
  1395. Data^.Found.Add(El);
  1396. end;
  1397. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1398. begin
  1399. if FWithTypeInfo=AValue then Exit;
  1400. FWithTypeInfo:=AValue;
  1401. if AValue then
  1402. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1403. else
  1404. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1405. end;
  1406. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1407. var
  1408. i: Integer;
  1409. CurEngine: TTestEnginePasResolver;
  1410. CurUnitName: String;
  1411. begin
  1412. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1413. Result:=nil;
  1414. if (Module.ClassType=TPasModule)
  1415. and (CompareText(Module.Name,aUnitName)=0) then
  1416. exit(Module);
  1417. for i:=0 to ResolverCount-1 do
  1418. begin
  1419. CurEngine:=Resolvers[i];
  1420. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1421. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1422. if CompareText(aUnitName,CurUnitName)=0 then
  1423. begin
  1424. Result:=CurEngine.Module;
  1425. if Result<>nil then exit;
  1426. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1427. FileResolver.FindSourceFile(aUnitName);
  1428. CurEngine.StreamResolver:=TStreamResolver.Create;
  1429. CurEngine.StreamResolver.OwnsStreams:=True;
  1430. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1431. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1432. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1433. InitScanner(CurEngine.Scanner);
  1434. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1435. CurEngine.Parser.Options:=po_tcmodules;
  1436. if CompareText(CurUnitName,'System')=0 then
  1437. CurEngine.Parser.ImplicitUses.Clear;
  1438. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1439. try
  1440. CurEngine.Parser.NextToken;
  1441. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1442. except
  1443. on E: Exception do
  1444. HandleException(E);
  1445. end;
  1446. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1447. Result:=CurEngine.Module;
  1448. exit;
  1449. end;
  1450. end;
  1451. end;
  1452. procedure TCustomTestModule.SetUp;
  1453. begin
  1454. {$IFDEF EnablePasTreeGlobalRefCount}
  1455. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1456. {$ENDIF}
  1457. if FResolvers<>nil then
  1458. begin
  1459. writeln('TCustomTestModule.SetUp FModules<>nil');
  1460. Halt;
  1461. end;
  1462. inherited SetUp;
  1463. FSkipTests:=false;
  1464. FWithTypeInfo:=false;
  1465. FSource:=TStringList.Create;
  1466. FHub:=TPas2JSResolverHub.Create(Self);
  1467. FResolvers:=TObjectList.Create(true);
  1468. FFilename:='test1.pp';
  1469. FFileResolver:=TStreamResolver.Create;
  1470. FFileResolver.OwnsStreams:=True;
  1471. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1472. InitScanner(FScanner);
  1473. FEngine:=AddModule(Filename);
  1474. FEngine.Scanner:=FScanner;
  1475. FScanner.Resolver:=FEngine;
  1476. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1477. FParser.OnLog:=@OnParserLog;
  1478. FEngine.Parser:=FParser;
  1479. Parser.Options:=po_tcmodules;
  1480. FModule:=Nil;
  1481. FConverter:=CreateConverter;
  1482. FExpectedErrorClass:=nil;
  1483. end;
  1484. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1485. var
  1486. Options: TPasToJsConverterOptions;
  1487. begin
  1488. Result:=TPasToJSConverter.Create;
  1489. Options:=co_tcmodules;
  1490. if WithTypeInfo then
  1491. Exclude(Options,coNoTypeInfo)
  1492. else
  1493. Include(Options,coNoTypeInfo);
  1494. Result.Options:=Options;
  1495. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1496. end;
  1497. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1498. begin
  1499. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1500. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1501. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1502. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1503. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1504. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1505. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1506. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1507. aScanner.OnLog:=@OnScannerLog;
  1508. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1509. end;
  1510. procedure TCustomTestModule.TearDown;
  1511. {$IFDEF CheckPasTreeRefCount}
  1512. var
  1513. El: TPasElement;
  1514. {$ENDIF}
  1515. var
  1516. i: Integer;
  1517. CurModule: TPasModule;
  1518. begin
  1519. FreeSrcMarkers;
  1520. FHintMsgs.Clear;
  1521. FHintMsgsGood.Clear;
  1522. FSkipTests:=false;
  1523. FWithTypeInfo:=false;
  1524. FJSRegModuleCall:=nil;
  1525. FJSModuleCallArgs:=nil;
  1526. FJSImplentationUses:=nil;
  1527. FJSInterfaceUses:=nil;
  1528. FJSModuleSrc:=nil;
  1529. FJSInitBody:=nil;
  1530. FreeAndNil(FJSSource);
  1531. FreeAndNil(FJSModule);
  1532. FreeAndNil(FConverter);
  1533. ResolverEngine.Clear;
  1534. FreeAndNil(FSource);
  1535. FreeAndNil(FFileResolver);
  1536. if FResolvers<>nil then
  1537. begin
  1538. for i:=0 to FResolvers.Count-1 do
  1539. begin
  1540. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1541. if CurModule=nil then continue;
  1542. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1543. {$IFNDEF EnablePasTreeFree}
  1544. CurModule.ReleaseUsedUnits;
  1545. {$ENDIF}
  1546. end;
  1547. {$IFNDEF EnablePasTreeFree}
  1548. if FModule<>nil then
  1549. FModule.ReleaseUsedUnits;
  1550. {$ENDIF}
  1551. for i:=0 to FResolvers.Count-1 do
  1552. begin
  1553. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1554. if CurModule=nil then continue;
  1555. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1556. end;
  1557. FreeAndNil(FResolvers);
  1558. {$IFDEF EnablePasTreeFree}
  1559. FModule:=nil;
  1560. {$ELSE}
  1561. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1562. {$ENDIF}
  1563. FEngine:=nil;
  1564. end;
  1565. FreeAndNil(FHub);
  1566. inherited TearDown;
  1567. {$IFDEF EnablePasTreeGlobalRefCount}
  1568. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1569. begin
  1570. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1571. {$IFDEF CheckPasTreeRefCount}
  1572. El:=TPasElement.FirstRefEl;
  1573. while El<>nil do
  1574. begin
  1575. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1576. for i:=0 to El.RefIds.Count-1 do
  1577. writeln(' ',El.RefIds[i]);
  1578. El:=El.NextRefEl;
  1579. end;
  1580. {$ENDIF}
  1581. Halt;
  1582. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1583. end;
  1584. {$ENDIF}
  1585. end;
  1586. procedure TCustomTestModule.Add(Line: string);
  1587. begin
  1588. Source.Add(Line);
  1589. end;
  1590. procedure TCustomTestModule.Add(const Lines: array of string);
  1591. var
  1592. i: Integer;
  1593. begin
  1594. for i:=low(Lines) to high(Lines) do
  1595. Add(Lines[i]);
  1596. end;
  1597. procedure TCustomTestModule.StartParsing;
  1598. var
  1599. Src: String;
  1600. begin
  1601. Src:=Source.Text;
  1602. FEngine.Source:=Src;
  1603. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1604. Scanner.OpenFile(FileName);
  1605. Writeln('// Test : ',Self.TestName);
  1606. Writeln(Src);
  1607. end;
  1608. procedure TCustomTestModule.ParseModuleQueue;
  1609. var
  1610. i: Integer;
  1611. CurResolver: TTestEnginePasResolver;
  1612. Found: Boolean;
  1613. Section: TPasSection;
  1614. begin
  1615. // parse til exception or all Resolvers finished
  1616. while not SkipTests do
  1617. begin
  1618. Found:=false;
  1619. for i:=0 to ResolverCount-1 do
  1620. begin
  1621. CurResolver:=Resolvers[i];
  1622. if CurResolver.CurrentParser=nil then continue;
  1623. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1624. continue;
  1625. CurResolver.Parser.ParseContinue;
  1626. Found:=true;
  1627. break;
  1628. end;
  1629. if not Found then break;
  1630. end;
  1631. for i:=0 to ResolverCount-1 do
  1632. begin
  1633. CurResolver:=Resolvers[i];
  1634. if CurResolver.Parser=nil then
  1635. begin
  1636. if CurResolver.CurrentParser<>nil then
  1637. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1638. continue;
  1639. end;
  1640. if CurResolver.Parser.CurModule<>nil then
  1641. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1642. end;
  1643. end;
  1644. procedure TCustomTestModule.ParseModule;
  1645. begin
  1646. if SkipTests then exit;
  1647. FFirstPasStatement:=nil;
  1648. try
  1649. StartParsing;
  1650. Parser.ParseMain(FModule);
  1651. ParseModuleQueue;
  1652. except
  1653. on E: Exception do
  1654. HandleException(E);
  1655. end;
  1656. if SkipTests then exit;
  1657. AssertNotNull('Module resulted in Module',Module);
  1658. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1659. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1660. end;
  1661. procedure TCustomTestModule.ParseProgram;
  1662. begin
  1663. if SkipTests then exit;
  1664. ParseModule;
  1665. if SkipTests then exit;
  1666. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1667. FPasProgram:=TPasProgram(Module);
  1668. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1669. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1670. if (PasProgram.InitializationSection.Elements.Count>0) then
  1671. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1672. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1673. end;
  1674. procedure TCustomTestModule.ParseLibrary;
  1675. var
  1676. Init: TInitializationSection;
  1677. begin
  1678. if SkipTests then exit;
  1679. ParseModule;
  1680. if SkipTests then exit;
  1681. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1682. FPasLibrary:=TPasLibrary(Module);
  1683. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1684. Init:=PasLibrary.InitializationSection;
  1685. if (Init<>nil) and (Init.Elements.Count>0) then
  1686. if TObject(Init.Elements[0]) is TPasImplBlock then
  1687. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1688. end;
  1689. procedure TCustomTestModule.ParseUnit;
  1690. begin
  1691. if SkipTests then exit;
  1692. ParseModule;
  1693. if SkipTests then exit;
  1694. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1695. AssertNotNull('Has interface section',Module.InterfaceSection);
  1696. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1697. if (Module.InitializationSection<>nil)
  1698. and (Module.InitializationSection.Elements.Count>0)
  1699. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1700. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1701. end;
  1702. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1703. ): TTestEnginePasResolver;
  1704. var
  1705. i: Integer;
  1706. begin
  1707. for i:=0 to ResolverCount-1 do
  1708. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1709. exit(Resolvers[i]);
  1710. Result:=nil;
  1711. end;
  1712. function TCustomTestModule.AddModule(aFilename: string
  1713. ): TTestEnginePasResolver;
  1714. begin
  1715. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1716. if FindModuleWithFilename(aFilename)<>nil then
  1717. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1718. Result:=TTestEnginePasResolver.Create;
  1719. Result.Filename:=aFilename;
  1720. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1721. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1722. Result.OnLog:=@OnPasResolverLog;
  1723. Result.Hub:=Hub;
  1724. FResolvers.Add(Result);
  1725. end;
  1726. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1727. ): TTestEnginePasResolver;
  1728. begin
  1729. Result:=AddModule(aFilename);
  1730. Result.Source:=Src;
  1731. end;
  1732. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1733. ImplementationSrc: string): TTestEnginePasResolver;
  1734. var
  1735. Src: String;
  1736. begin
  1737. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1738. Src+=LineEnding;
  1739. Src+='interface'+LineEnding;
  1740. Src+=LineEnding;
  1741. Src+=InterfaceSrc;
  1742. Src+='implementation'+LineEnding;
  1743. Src+=LineEnding;
  1744. Src+=ImplementationSrc;
  1745. Src+='end.'+LineEnding;
  1746. Result:=AddModuleWithSrc(aFilename,Src);
  1747. end;
  1748. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1749. var
  1750. Intf, Impl: TStringList;
  1751. begin
  1752. Intf:=TStringList.Create;
  1753. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1754. // unit interface
  1755. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1756. Intf.Add('{$modeswitch externalclass}');
  1757. Intf.Add('type');
  1758. Intf.Add(' integer=longint;');
  1759. Intf.Add(' sizeint=nativeint;');
  1760. //'const',
  1761. //' LineEnding = #10;',
  1762. //' DirectorySeparator = ''/'';',
  1763. //' DriveSeparator = '''';',
  1764. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1765. //' AllowDriveSeparators : set of char = [];',
  1766. if supTObject in Parts then
  1767. Intf.AddStrings([
  1768. 'type',
  1769. ' TClass = class of TObject;',
  1770. ' TObject = class',
  1771. ' constructor Create;',
  1772. ' destructor Destroy; virtual;',
  1773. ' class function ClassType: TClass; assembler;',
  1774. ' class function ClassName: String; assembler;',
  1775. ' class function ClassNameIs(const Name: string): boolean;',
  1776. ' class function ClassParent: TClass; assembler;',
  1777. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1778. ' class function UnitName: String; assembler;',
  1779. ' procedure AfterConstruction; virtual;',
  1780. ' procedure BeforeDestruction;virtual;',
  1781. ' function Equals(Obj: TObject): boolean; virtual;',
  1782. ' function ToString: String; virtual;',
  1783. ' end;']);
  1784. if supTInterfacedObject in Parts then
  1785. Intf.AddStrings([
  1786. ' {$Interfaces COM}',
  1787. ' IUnknown = interface',
  1788. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1789. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1790. ' function _AddRef: Integer;',
  1791. ' function _Release: Integer;',
  1792. ' end;',
  1793. ' IInterface = IUnknown;',
  1794. ' TInterfacedObject = class(TObject,IUnknown)',
  1795. ' protected',
  1796. ' fRefCount: Integer;',
  1797. ' { implement methods of IUnknown }',
  1798. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1799. ' function _AddRef: Integer; virtual;',
  1800. ' function _Release: Integer; virtual;',
  1801. ' end;',
  1802. ' TInterfacedClass = class of TInterfacedObject;',
  1803. '',
  1804. '']);
  1805. if supTVarRec in Parts then
  1806. Intf.AddStrings([
  1807. 'const',
  1808. ' vtInteger = 0;',
  1809. ' vtBoolean = 1;',
  1810. ' vtJSValue = 19;',
  1811. 'type',
  1812. ' PVarRec = ^TVarRec;',
  1813. ' TVarRec = record',
  1814. ' VType : byte;',
  1815. ' VJSValue: JSValue;',
  1816. ' vInteger: longint external name ''VJSValue'';',
  1817. ' vBoolean: boolean external name ''VJSValue'';',
  1818. ' end;',
  1819. ' TVarRecArray = array of TVarRec;',
  1820. 'function VarRecs: TVarRecArray; varargs;',
  1821. '']);
  1822. if supTypeInfo in Parts then
  1823. begin
  1824. Intf.AddStrings([
  1825. 'type',
  1826. ' TTypeKind = (',
  1827. ' tkUnknown, // 0',
  1828. ' tkInteger, // 1',
  1829. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1830. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1831. ' tkEnumeration, // 4',
  1832. ' tkSet, // 5',
  1833. ' tkDouble, // 6',
  1834. ' tkBool, // 7',
  1835. ' tkProcVar, // 8 function or procedure',
  1836. ' tkMethod, // 9 proc var of object',
  1837. ' tkArray, // 10 static array',
  1838. ' tkDynArray, // 11',
  1839. ' tkRecord, // 12',
  1840. ' tkClass, // 13',
  1841. ' tkClassRef, // 14',
  1842. ' tkPointer, // 15',
  1843. ' tkJSValue, // 16',
  1844. ' tkRefToProcVar, // 17 variable of procedure type',
  1845. ' tkInterface, // 18',
  1846. ' //tkObject,',
  1847. ' //tkSString,tkLString,tkAString,tkWString,',
  1848. ' //tkVariant,',
  1849. ' //tkWChar,',
  1850. ' //tkInt64,',
  1851. ' //tkQWord,',
  1852. ' //tkInterfaceRaw,',
  1853. ' //tkUString,tkUChar,',
  1854. ' tkHelper, // 19',
  1855. ' //tkFile,',
  1856. ' tkExtClass // 20',
  1857. ' );',
  1858. ' TTypeKinds = set of TTypeKind;',
  1859. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1860. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1861. ' end;',
  1862. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1863. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1864. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1865. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1866. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1867. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1868. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1869. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1870. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1871. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1872. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1873. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1874. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1875. '']);
  1876. end;
  1877. if supWriteln in Parts then
  1878. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1879. Intf.Add('var');
  1880. Intf.Add(' ExitCode: Longint = 0;');
  1881. // unit implementation
  1882. Impl:=TStringList.Create;
  1883. if supTObject in Parts then
  1884. Impl.AddStrings([
  1885. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1886. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1887. 'asm',
  1888. 'end;',
  1889. 'constructor TObject.Create; begin end;',
  1890. 'destructor TObject.Destroy; begin end;',
  1891. 'class function TObject.ClassType: TClass; assembler;',
  1892. 'asm',
  1893. 'end;',
  1894. 'class function TObject.ClassName: String; assembler;',
  1895. 'asm',
  1896. 'end;',
  1897. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1898. 'begin',
  1899. ' Result:=SameText(Name,ClassName);',
  1900. 'end;',
  1901. 'class function TObject.ClassParent: TClass; assembler;',
  1902. 'asm',
  1903. 'end;',
  1904. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1905. 'asm',
  1906. 'end;',
  1907. 'class function TObject.UnitName: String; assembler;',
  1908. 'asm',
  1909. 'end;',
  1910. 'procedure TObject.AfterConstruction; begin end;',
  1911. 'procedure TObject.BeforeDestruction; begin end;',
  1912. 'function TObject.Equals(Obj: TObject): boolean;',
  1913. 'begin',
  1914. ' Result:=Obj=Self;',
  1915. 'end;',
  1916. 'function TObject.ToString: String;',
  1917. 'begin',
  1918. ' Result:=ClassName;',
  1919. 'end;'
  1920. ]);
  1921. if supTInterfacedObject in Parts then
  1922. Impl.AddStrings([
  1923. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1924. //'begin',
  1925. //'end;',
  1926. 'function TInterfacedObject._AddRef: Integer;',
  1927. 'begin',
  1928. 'end;',
  1929. 'function TInterfacedObject._Release: Integer;',
  1930. 'begin',
  1931. 'end;',
  1932. '']);
  1933. if supTVarRec in Parts then
  1934. Impl.AddStrings([
  1935. 'function VarRecs: TVarRecArray; varargs;',
  1936. 'var',
  1937. ' v: PVarRec;',
  1938. 'begin',
  1939. ' v^.VType:=1;',
  1940. ' v^.VJSValue:=2;',
  1941. 'end;',
  1942. '']);
  1943. try
  1944. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1945. finally
  1946. Intf.Free;
  1947. Impl.Free;
  1948. end;
  1949. end;
  1950. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1951. SystemUnitParts: TSystemUnitParts);
  1952. begin
  1953. if NeedSystemUnit then
  1954. AddSystemUnit(SystemUnitParts)
  1955. else
  1956. Parser.ImplicitUses.Clear;
  1957. Add('program '+ExtractFileUnitName(Filename)+';');
  1958. Add('');
  1959. end;
  1960. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1961. SystemUnitParts: TSystemUnitParts);
  1962. begin
  1963. if NeedSystemUnit then
  1964. AddSystemUnit(SystemUnitParts)
  1965. else
  1966. Parser.ImplicitUses.Clear;
  1967. Add('library '+ExtractFileUnitName(Filename)+';');
  1968. Add('');
  1969. end;
  1970. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1971. SystemUnitParts: TSystemUnitParts);
  1972. begin
  1973. if NeedSystemUnit then
  1974. AddSystemUnit(SystemUnitParts)
  1975. else
  1976. Parser.ImplicitUses.Clear;
  1977. Add('unit Test1;');
  1978. Add('');
  1979. end;
  1980. procedure TCustomTestModule.ConvertModule;
  1981. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1982. out UsesLit: TJSArrayLiteral);
  1983. var
  1984. i: Integer;
  1985. Item: TJSElement;
  1986. Lit: TJSLiteral;
  1987. begin
  1988. UsesLit:=nil;
  1989. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1990. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1991. exit; // null is ok
  1992. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1993. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1994. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1995. begin
  1996. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1997. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1998. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1999. Lit:=TJSLiteral(Item);
  2000. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  2001. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  2002. end;
  2003. end;
  2004. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2005. out Src: TJSSourceElements);
  2006. var
  2007. FunDecl: TJSFunctionDeclarationStatement;
  2008. FunDef: TJSFuncDef;
  2009. FunBody: TJSFunctionBody;
  2010. begin
  2011. Src:=nil;
  2012. AssertNotNull(ParamName,Arg.Expr);
  2013. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2014. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2015. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2016. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2017. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2018. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2019. AssertNotNull(ParamName+' body',FunDef.Body);
  2020. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2021. FunBody:=FunDef.Body as TJSFunctionBody;
  2022. AssertNotNull(ParamName+' body.A',FunBody.A);
  2023. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2024. Src:=FunBody.A as TJSSourceElements;
  2025. end;
  2026. var
  2027. ModuleNameExpr: TJSLiteral;
  2028. InitFunction: TJSFunctionDeclarationStatement;
  2029. InitAssign: TJSSimpleAssignStatement;
  2030. InitName: String;
  2031. LastNode, FirstNode: TJSElement;
  2032. Arg: TJSArrayLiteralElement;
  2033. IsProg, IsLib: Boolean;
  2034. begin
  2035. if SkipTests then exit;
  2036. IsProg:=false;
  2037. IsLib:=false;
  2038. if Module is TPasProgram then
  2039. IsProg:=true
  2040. else if Module is TPasLibrary then
  2041. IsLib:=true;
  2042. try
  2043. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2044. except
  2045. on E: Exception do
  2046. HandleException(E);
  2047. end;
  2048. if SkipTests then exit;
  2049. if ExpectedErrorClass<>nil then
  2050. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2051. FJSSource:=TStringList.Create;
  2052. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2053. {$IFDEF VerbosePas2JS}
  2054. writeln('TTestModule.ConvertModule JS:');
  2055. write(FJSSource.Text);
  2056. {$ENDIF}
  2057. // rtl.module(...
  2058. if JSModule.Statements.Count<1 then
  2059. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2060. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2061. AssertNotNull('register module call',FirstNode);
  2062. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2063. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2064. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2065. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2066. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2067. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2068. // parameter 'unitname'
  2069. if JSModuleCallArgs.Elements.Count<1 then
  2070. Fail('rtl.module first param unit missing');
  2071. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2072. AssertNotNull('module name param',Arg.Expr);
  2073. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2074. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2075. if IsProg then
  2076. begin
  2077. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2078. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2079. end
  2080. else if IsLib then
  2081. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2082. else
  2083. begin
  2084. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2085. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2086. end;
  2087. // main uses section
  2088. if JSModuleCallArgs.Elements.Count<2 then
  2089. Fail('rtl.module second param main uses missing');
  2090. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2091. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2092. // program/library/interface function()
  2093. if JSModuleCallArgs.Elements.Count<3 then
  2094. Fail('rtl.module third param intf-function missing');
  2095. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2096. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2097. // search for $mod.$init or $mod.$main - the last statement
  2098. if IsProg or IsLib then
  2099. begin
  2100. InitName:='$main';
  2101. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2102. end
  2103. else
  2104. InitName:='$init';
  2105. InitAssign:=nil;
  2106. InitFunction:=nil;
  2107. FJSInitBody:=nil;
  2108. if JSModuleSrc.Statements.Count>0 then
  2109. begin
  2110. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2111. if LastNode is TJSSimpleAssignStatement then
  2112. begin
  2113. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2114. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2115. begin
  2116. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2117. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2118. end
  2119. else if IsProg or IsLib then
  2120. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2121. end;
  2122. end;
  2123. // optional: implementation uses section
  2124. if JSModuleCallArgs.Elements.Count<4 then
  2125. exit;
  2126. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2127. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2128. end;
  2129. procedure TCustomTestModule.ConvertProgram;
  2130. begin
  2131. Add('end.');
  2132. ParseProgram;
  2133. ConvertModule;
  2134. end;
  2135. procedure TCustomTestModule.ConvertLibrary;
  2136. begin
  2137. Add('end.');
  2138. ParseLibrary;
  2139. ConvertModule;
  2140. end;
  2141. procedure TCustomTestModule.ConvertUnit;
  2142. begin
  2143. Add('end.');
  2144. ParseUnit;
  2145. ConvertModule;
  2146. end;
  2147. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2148. begin
  2149. Result:=tcmodules.JSToStr(El);
  2150. end;
  2151. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2152. DottedName: string);
  2153. begin
  2154. if DottedName='' then
  2155. begin
  2156. AssertNull(Msg,El);
  2157. end
  2158. else
  2159. begin
  2160. AssertNotNull(Msg,El);
  2161. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2162. end;
  2163. end;
  2164. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2165. begin
  2166. if El=nil then
  2167. Result:=''
  2168. else if El is TJSPrimaryExpressionIdent then
  2169. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2170. else if El is TJSDotMemberExpression then
  2171. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2172. else
  2173. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2174. end;
  2175. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2176. InitStatements: string; ImplStatements: string);
  2177. var
  2178. ActualSrc, ExpectedSrc, InitName: String;
  2179. IsProg, IsLib: Boolean;
  2180. begin
  2181. ActualSrc:=JSToStr(JSModuleSrc);
  2182. if coUseStrict in Converter.Options then
  2183. ExpectedSrc:='"use strict";'+LineEnding
  2184. else
  2185. ExpectedSrc:='';
  2186. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2187. ExpectedSrc:=ExpectedSrc+Statements;
  2188. // unit implementation
  2189. if (Trim(ImplStatements)<>'') then
  2190. ExpectedSrc:=ExpectedSrc+LineEnding
  2191. +'$mod.$implcode = function () {'+LineEnding
  2192. +ImplStatements
  2193. +'};'+LineEnding;
  2194. // program main or unit initialization
  2195. IsProg:=false;
  2196. IsLib:=false;
  2197. if Module is TPasProgram then
  2198. IsProg:=true
  2199. else if Module is TPasLibrary then
  2200. IsLib:=true;
  2201. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2202. begin
  2203. if IsProg or IsLib then
  2204. InitName:='$main'
  2205. else
  2206. InitName:='$init';
  2207. ExpectedSrc:=ExpectedSrc+LineEnding
  2208. +'$mod.'+InitName+' = function () {'+LineEnding
  2209. +InitStatements
  2210. +'};'+LineEnding;
  2211. end;
  2212. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2213. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2214. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2215. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2216. end;
  2217. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2218. var
  2219. ActualSrc: String;
  2220. begin
  2221. ActualSrc:=JSToStr(JSModule);
  2222. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2223. end;
  2224. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2225. // search diff, ignore changes in spaces
  2226. var
  2227. s: string;
  2228. begin
  2229. if CheckSrcDiff(Expected,Actual,s) then exit;
  2230. Fail(Msg+': '+s);
  2231. end;
  2232. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2233. var
  2234. aResolver: TTestEnginePasResolver;
  2235. aConverter: TPasToJSConverter;
  2236. aJSModule: TJSSourceElements;
  2237. ActualSrc: String;
  2238. begin
  2239. aResolver:=GetResolver(Filename);
  2240. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2241. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2242. {$IFDEF VerbosePas2JS}
  2243. writeln('CheckUnit '+Filename+' converting ...');
  2244. {$ENDIF}
  2245. aConverter:=CreateConverter;
  2246. aJSModule:=nil;
  2247. try
  2248. try
  2249. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2250. except
  2251. on E: Exception do
  2252. HandleException(E);
  2253. end;
  2254. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2255. {$IFDEF VerbosePas2JS}
  2256. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2257. write(aResolver.Source);
  2258. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2259. write(ActualSrc);
  2260. {$ENDIF}
  2261. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2262. finally
  2263. aJSModule.Free;
  2264. aConverter.Free;
  2265. end;
  2266. end;
  2267. procedure TCustomTestModule.CheckReferenceDirectives;
  2268. var
  2269. CurFilename: string;
  2270. LineNumber: Integer;
  2271. SrcLine: String;
  2272. CommentStartP, CommentEndP: PChar;
  2273. procedure RaiseError(Msg: string; p: PChar);
  2274. begin
  2275. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2276. end;
  2277. procedure AddMarker(Marker: PSrcMarker);
  2278. begin
  2279. if LastSrcMarker<>nil then
  2280. LastSrcMarker^.Next:=Marker
  2281. else
  2282. FirstSrcMarker:=Marker;
  2283. LastSrcMarker:=Marker;
  2284. end;
  2285. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2286. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2287. begin
  2288. New(Result);
  2289. Result^.Kind:=Kind;
  2290. Result^.Filename:=aFilename;
  2291. Result^.Row:=aLine;
  2292. Result^.StartCol:=aStartCol;
  2293. Result^.EndCol:=aEndCol;
  2294. Result^.Identifier:=Identifier;
  2295. Result^.Next:=nil;
  2296. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2297. AddMarker(Result);
  2298. end;
  2299. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2300. const Identifier: string): PSrcMarker;
  2301. var
  2302. TokenStart, p: PChar;
  2303. begin
  2304. p:=CommentEndP;
  2305. ReadNextPascalToken(p,TokenStart,false,false);
  2306. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2307. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2308. end;
  2309. function ReadIdentifier(var p: PChar): string;
  2310. var
  2311. StartP: PChar;
  2312. begin
  2313. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2314. RaiseError('identifier expected',p);
  2315. StartP:=p;
  2316. inc(p);
  2317. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2318. Result:='';
  2319. SetLength(Result,p-StartP);
  2320. Move(StartP^,Result[1],length(Result));
  2321. end;
  2322. procedure AddLabel;
  2323. var
  2324. Identifier: String;
  2325. p: PChar;
  2326. begin
  2327. p:=CommentStartP+2;
  2328. Identifier:=ReadIdentifier(p);
  2329. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2330. if FindSrcLabel(Identifier)<>nil then
  2331. RaiseError('duplicate label "'+Identifier+'"',p);
  2332. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2333. end;
  2334. procedure AddResolverReference;
  2335. var
  2336. Identifier: String;
  2337. p: PChar;
  2338. begin
  2339. p:=CommentStartP+2;
  2340. Identifier:=ReadIdentifier(p);
  2341. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2342. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2343. end;
  2344. procedure AddDirectReference;
  2345. var
  2346. Identifier: String;
  2347. p: PChar;
  2348. begin
  2349. p:=CommentStartP+2;
  2350. Identifier:=ReadIdentifier(p);
  2351. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2352. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2353. end;
  2354. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2355. var
  2356. p: PChar;
  2357. IsDirective: Boolean;
  2358. begin
  2359. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2360. CurFilename:=aFilename;
  2361. // parse code, find all labels
  2362. LineNumber:=0;
  2363. while LineNumber<SrcLines.Count do
  2364. begin
  2365. inc(LineNumber);
  2366. SrcLine:=SrcLines[LineNumber-1];
  2367. if SrcLine='' then continue;
  2368. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2369. p:=PChar(SrcLine);
  2370. repeat
  2371. case p^ of
  2372. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2373. '{':
  2374. begin
  2375. CommentStartP:=p;
  2376. inc(p);
  2377. IsDirective:=p^ in ['#','@','='];
  2378. // skip to end of comment
  2379. repeat
  2380. case p^ of
  2381. #0:
  2382. if (p-PChar(SrcLine)=length(SrcLine)) then
  2383. begin
  2384. // multi line comment
  2385. if IsDirective then
  2386. RaiseError('directive missing closing bracket',CommentStartP);
  2387. repeat
  2388. inc(LineNumber);
  2389. if LineNumber>SrcLines.Count then exit;
  2390. SrcLine:=SrcLines[LineNumber-1];
  2391. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2392. until SrcLine<>'';
  2393. p:=PChar(SrcLine);
  2394. continue;
  2395. end;
  2396. '}':
  2397. begin
  2398. inc(p);
  2399. break;
  2400. end;
  2401. end;
  2402. inc(p);
  2403. until false;
  2404. CommentEndP:=p;
  2405. case CommentStartP[1] of
  2406. '#': AddLabel;
  2407. '@': AddResolverReference;
  2408. '=': AddDirectReference;
  2409. end;
  2410. p:=CommentEndP;
  2411. continue;
  2412. end;
  2413. '/':
  2414. if p[1]='/' then
  2415. break; // rest of line is comment -> skip
  2416. end;
  2417. inc(p);
  2418. until false;
  2419. end;
  2420. end;
  2421. procedure CheckResolverReference(aMarker: PSrcMarker);
  2422. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2423. var
  2424. aLabel: PSrcMarker;
  2425. ReferenceElements, LabelElements: TFPList;
  2426. i, j, aLine, aCol: Integer;
  2427. El, Ref, LabelEl: TPasElement;
  2428. begin
  2429. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2430. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2431. if aLabel=nil then
  2432. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2433. LabelElements:=nil;
  2434. ReferenceElements:=nil;
  2435. try
  2436. LabelElements:=FindElementsAt(aLabel);
  2437. ReferenceElements:=FindElementsAt(aMarker);
  2438. for i:=0 to ReferenceElements.Count-1 do
  2439. begin
  2440. El:=TPasElement(ReferenceElements[i]);
  2441. Ref:=nil;
  2442. if El.CustomData is TResolvedReference then
  2443. Ref:=TResolvedReference(El.CustomData).Declaration
  2444. else if El.CustomData is TPasPropertyScope then
  2445. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2446. else if El.CustomData is TPasSpecializeTypeData then
  2447. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2448. if Ref<>nil then
  2449. for j:=0 to LabelElements.Count-1 do
  2450. begin
  2451. LabelEl:=TPasElement(LabelElements[j]);
  2452. if Ref=LabelEl then
  2453. exit; // success
  2454. end;
  2455. end;
  2456. // failure write candidates
  2457. for i:=0 to ReferenceElements.Count-1 do
  2458. begin
  2459. El:=TPasElement(ReferenceElements[i]);
  2460. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2461. write(' El=',GetObjName(El));
  2462. if EL is TPrimitiveExpr then
  2463. begin
  2464. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2465. end;
  2466. Ref:=nil;
  2467. if El.CustomData is TResolvedReference then
  2468. Ref:=TResolvedReference(El.CustomData).Declaration
  2469. else if El.CustomData is TPasPropertyScope then
  2470. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2471. else if El.CustomData is TPasSpecializeTypeData then
  2472. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2473. if Ref<>nil then
  2474. begin
  2475. write(' Decl=',GetObjName(Ref));
  2476. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2477. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2478. end
  2479. else
  2480. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2481. writeln;
  2482. end;
  2483. for i:=0 to LabelElements.Count-1 do
  2484. begin
  2485. El:=TPasElement(LabelElements[i]);
  2486. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2487. write(' El=',GetObjName(El));
  2488. writeln;
  2489. end;
  2490. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2491. finally
  2492. LabelElements.Free;
  2493. ReferenceElements.Free;
  2494. end;
  2495. end;
  2496. procedure CheckDirectReference(aMarker: PSrcMarker);
  2497. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2498. var
  2499. aLabel: PSrcMarker;
  2500. ReferenceElements, LabelElements: TFPList;
  2501. i, LabelLine, LabelCol, j: Integer;
  2502. El, LabelEl: TPasElement;
  2503. DeclEl, TypeEl: TPasType;
  2504. begin
  2505. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2506. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2507. if aLabel=nil then
  2508. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2509. LabelElements:=nil;
  2510. ReferenceElements:=nil;
  2511. try
  2512. //writeln('CheckDirectReference finding elements at label ...');
  2513. LabelElements:=FindElementsAt(aLabel);
  2514. //writeln('CheckDirectReference finding elements at reference ...');
  2515. ReferenceElements:=FindElementsAt(aMarker);
  2516. for i:=0 to ReferenceElements.Count-1 do
  2517. begin
  2518. El:=TPasElement(ReferenceElements[i]);
  2519. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2520. if El.ClassType=TPasVariable then
  2521. begin
  2522. if TPasVariable(El).VarType=nil then
  2523. begin
  2524. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2525. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2526. end;
  2527. TypeEl:=TPasVariable(El).VarType;
  2528. for j:=0 to LabelElements.Count-1 do
  2529. begin
  2530. LabelEl:=TPasElement(LabelElements[j]);
  2531. if TypeEl=LabelEl then
  2532. exit; // success
  2533. end;
  2534. end
  2535. else if El is TPasAliasType then
  2536. begin
  2537. DeclEl:=TPasAliasType(El).DestType;
  2538. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2539. if (aLabel^.Filename=DeclEl.SourceFilename)
  2540. and (integer(aLabel^.Row)=LabelLine)
  2541. and (aLabel^.StartCol<=LabelCol)
  2542. and (aLabel^.EndCol>=LabelCol) then
  2543. exit; // success
  2544. end
  2545. else if El.ClassType=TPasArgument then
  2546. begin
  2547. TypeEl:=TPasArgument(El).ArgType;
  2548. for j:=0 to LabelElements.Count-1 do
  2549. begin
  2550. LabelEl:=TPasElement(LabelElements[j]);
  2551. if TypeEl=LabelEl then
  2552. exit; // success
  2553. end;
  2554. end;
  2555. end;
  2556. // failed -> show candidates
  2557. writeln('CheckDirectReference failed: Labels:');
  2558. for j:=0 to LabelElements.Count-1 do
  2559. begin
  2560. LabelEl:=TPasElement(LabelElements[j]);
  2561. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2562. end;
  2563. writeln('CheckDirectReference failed: References:');
  2564. for i:=0 to ReferenceElements.Count-1 do
  2565. begin
  2566. El:=TPasElement(ReferenceElements[i]);
  2567. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2568. //if EL is TPasVariable then
  2569. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2570. end;
  2571. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2572. finally
  2573. LabelElements.Free;
  2574. ReferenceElements.Free;
  2575. end;
  2576. end;
  2577. var
  2578. aMarker: PSrcMarker;
  2579. i: Integer;
  2580. SrcLines: TStringList;
  2581. begin
  2582. Module.ForEachCall(@OnCheckElementParent,nil);
  2583. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2584. // find all markers
  2585. for i:=0 to FileResolver.Streams.Count-1 do
  2586. begin
  2587. GetSrc(i,SrcLines,CurFilename);
  2588. ParseCode(SrcLines,CurFilename);
  2589. SrcLines.Free;
  2590. end;
  2591. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2592. // check references
  2593. aMarker:=FirstSrcMarker;
  2594. while aMarker<>nil do
  2595. begin
  2596. case aMarker^.Kind of
  2597. mkResolverReference: CheckResolverReference(aMarker);
  2598. mkDirectReference: CheckDirectReference(aMarker);
  2599. end;
  2600. aMarker:=aMarker^.Next;
  2601. end;
  2602. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2603. end;
  2604. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2605. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2606. var
  2607. i: Integer;
  2608. Item: TTestHintMessage;
  2609. Expected,Actual: string;
  2610. begin
  2611. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2612. for i:=0 to MsgCount-1 do
  2613. begin
  2614. Item:=Msgs[i];
  2615. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2616. if (Marker<>nil) then
  2617. begin
  2618. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2619. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2620. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2621. end;
  2622. // found
  2623. FHintMsgsGood.Add(Item);
  2624. str(Item.MsgType,Actual);
  2625. str(MsgType,Expected);
  2626. AssertEquals('MsgType',Expected,Actual);
  2627. exit;
  2628. end;
  2629. // needed message missing -> show emitted messages
  2630. WriteSources('',0,0);
  2631. for i:=0 to MsgCount-1 do
  2632. begin
  2633. Item:=Msgs[i];
  2634. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2635. ' ('+IntToStr(Item.MsgNumber),')');
  2636. if Marker<>nil then
  2637. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2638. writeln(' {',Item.Msg,'}');
  2639. end;
  2640. str(MsgType,Expected);
  2641. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2642. if Marker<>nil then
  2643. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2644. Actual:=Actual+' '+Msg;
  2645. Fail(Actual);
  2646. end;
  2647. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2648. );
  2649. var
  2650. i: Integer;
  2651. s, Txt: String;
  2652. Msg: TTestHintMessage;
  2653. begin
  2654. for i:=0 to MsgCount-1 do
  2655. begin
  2656. Msg:=Msgs[i];
  2657. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2658. s:='';
  2659. str(Msg.MsgType,s);
  2660. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2661. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2662. if WithSourcePos then
  2663. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2664. Txt:=Txt+' {'+Msg.Msg+'}';
  2665. Fail(Txt);
  2666. end;
  2667. end;
  2668. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2669. MsgNumber: integer);
  2670. begin
  2671. ExpectedErrorClass:=EScannerError;
  2672. ExpectedErrorMsg:=Msg;
  2673. ExpectedErrorNumber:=MsgNumber;
  2674. end;
  2675. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2676. MsgNumber: integer);
  2677. begin
  2678. ExpectedErrorClass:=EParserError;
  2679. ExpectedErrorMsg:=Msg;
  2680. ExpectedErrorNumber:=MsgNumber;
  2681. end;
  2682. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2683. MsgNumber: integer);
  2684. begin
  2685. ExpectedErrorClass:=EPasResolve;
  2686. ExpectedErrorMsg:=Msg;
  2687. ExpectedErrorNumber:=MsgNumber;
  2688. end;
  2689. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2690. MsgNumber: integer);
  2691. begin
  2692. ExpectedErrorClass:=EPas2JS;
  2693. ExpectedErrorMsg:=Msg;
  2694. ExpectedErrorNumber:=MsgNumber;
  2695. end;
  2696. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2697. var
  2698. MsgNumber: Integer;
  2699. Msg: String;
  2700. begin
  2701. Result:=false;
  2702. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2703. Msg:=E.Message;
  2704. if E is EPas2JS then
  2705. MsgNumber:=EPas2JS(E).MsgNumber
  2706. else if E is EPasResolve then
  2707. MsgNumber:=EPasResolve(E).MsgNumber
  2708. else if E is EParserError then
  2709. MsgNumber:=Parser.LastMsgNumber
  2710. else if E is EScannerError then
  2711. begin
  2712. MsgNumber:=Scanner.LastMsgNumber;
  2713. Msg:=Scanner.LastMsg;
  2714. end
  2715. else
  2716. MsgNumber:=0;
  2717. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2718. if Result then
  2719. SkipTests:=true;
  2720. end;
  2721. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2722. const aFilename: string; aRow, aCol: integer);
  2723. var
  2724. s: String;
  2725. begin
  2726. WriteSources(aFilename,aRow,aCol);
  2727. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2728. writeln('ERROR: ',s);
  2729. Fail(s);
  2730. end;
  2731. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2732. aMarker: PSrcMarker);
  2733. begin
  2734. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2735. end;
  2736. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2737. begin
  2738. if IsErrorExpected(E) then exit;
  2739. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2740. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2741. +' '+Scanner.CurFilename
  2742. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2743. FailException(E);
  2744. end;
  2745. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2746. begin
  2747. if IsErrorExpected(E) then exit;
  2748. WriteSources(E.Filename,E.Row,E.Column);
  2749. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2750. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2751. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2752. );
  2753. FailException(E);
  2754. end;
  2755. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2756. var
  2757. P: TPasSourcePos;
  2758. begin
  2759. if IsErrorExpected(E) then exit;
  2760. P:=E.SourcePos;
  2761. WriteSources(P.FileName,P.Row,P.Column);
  2762. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2763. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2764. FailException(E);
  2765. end;
  2766. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2767. var
  2768. Row, Col: integer;
  2769. begin
  2770. if IsErrorExpected(E) then exit;
  2771. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2772. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2773. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2774. +' '+E.PasElement.SourceFilename
  2775. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2776. FailException(E);
  2777. end;
  2778. procedure TCustomTestModule.HandleException(E: Exception);
  2779. begin
  2780. if E is EScannerError then
  2781. HandleScannerError(EScannerError(E))
  2782. else if E is EParserError then
  2783. HandleParserError(EParserError(E))
  2784. else if E is EPasResolve then
  2785. HandlePasResolveError(EPasResolve(E))
  2786. else if E is EPas2JS then
  2787. HandlePas2JSError(EPas2JS(E))
  2788. else
  2789. begin
  2790. if IsErrorExpected(E) then exit;
  2791. if not (E is EAssertionFailedError) then
  2792. begin
  2793. WriteSources('',0,0);
  2794. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2795. end;
  2796. FailException(E);
  2797. end;
  2798. end;
  2799. procedure TCustomTestModule.FailException(E: Exception);
  2800. var
  2801. MsgNumber: Integer;
  2802. begin
  2803. if ExpectedErrorClass<>nil then
  2804. begin
  2805. if FExpectedErrorClass=E.ClassType then
  2806. begin
  2807. if E is EPas2JS then
  2808. MsgNumber:=EPas2JS(E).MsgNumber
  2809. else if E is EPasResolve then
  2810. MsgNumber:=EPasResolve(E).MsgNumber
  2811. else if E is EParserError then
  2812. MsgNumber:=Parser.LastMsgNumber
  2813. else if E is EScannerError then
  2814. MsgNumber:=Scanner.LastMsgNumber
  2815. else
  2816. MsgNumber:=0;
  2817. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2818. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2819. ExpectedErrorNumber,MsgNumber);
  2820. end else begin
  2821. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2822. end;
  2823. end;
  2824. Fail(E.Message);
  2825. end;
  2826. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2827. aCol: integer);
  2828. var
  2829. IsSrc: Boolean;
  2830. i, j: Integer;
  2831. SrcLines: TStringList;
  2832. Line: string;
  2833. aModule: TTestEnginePasResolver;
  2834. begin
  2835. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2836. for i:=0 to ResolverCount-1 do
  2837. begin
  2838. aModule:=Resolvers[i];
  2839. SrcLines:=TStringList.Create;
  2840. try
  2841. SrcLines.Text:=aModule.Source;
  2842. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2843. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2844. for j:=1 to SrcLines.Count do
  2845. begin
  2846. Line:=SrcLines[j-1];
  2847. if IsSrc and (j=aRow) then
  2848. begin
  2849. write('*');
  2850. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2851. end;
  2852. writeln(Format('%:4d: ',[j]),Line);
  2853. end;
  2854. finally
  2855. SrcLines.Free;
  2856. end;
  2857. end;
  2858. end;
  2859. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2860. var
  2861. i: Integer;
  2862. begin
  2863. for i:=0 to ResolverCount-1 do
  2864. if Filename=Resolvers[i].Filename then exit(i);
  2865. Result:=-1;
  2866. end;
  2867. function TCustomTestModule.GetResolver(const Filename: string
  2868. ): TTestEnginePasResolver;
  2869. var
  2870. i: Integer;
  2871. begin
  2872. i:=IndexOfResolver(Filename);
  2873. if i<0 then exit(nil);
  2874. Result:=Resolvers[i];
  2875. end;
  2876. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2877. out aFilename: string);
  2878. var
  2879. aStream: TStream;
  2880. begin
  2881. SrcLines:=TStringList.Create;
  2882. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2883. aStream.Position:=0;
  2884. SrcLines.LoadFromStream(aStream);
  2885. aFilename:=FileResolver.Streams[Index];
  2886. end;
  2887. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2888. aEndCol: integer): TFPList;
  2889. var
  2890. ok: Boolean;
  2891. FoundRefs: TTestResolverReferenceData;
  2892. i: Integer;
  2893. CurResolver: TTestEnginePasResolver;
  2894. begin
  2895. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2896. FoundRefs:=Default(TTestResolverReferenceData);
  2897. FoundRefs.Filename:=aFilename;
  2898. FoundRefs.Row:=aLine;
  2899. FoundRefs.StartCol:=aStartCol;
  2900. FoundRefs.EndCol:=aEndCol;
  2901. FoundRefs.Found:=TFPList.Create;
  2902. ok:=false;
  2903. try
  2904. // find all markers
  2905. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2906. for i:=0 to ResolverCount-1 do
  2907. begin
  2908. CurResolver:=Resolvers[i];
  2909. if CurResolver.Module=Module then continue;
  2910. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2911. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2912. end;
  2913. ok:=true;
  2914. finally
  2915. if not ok then
  2916. FreeAndNil(FoundRefs.Found);
  2917. end;
  2918. Result:=FoundRefs.Found;
  2919. FoundRefs.Found:=nil;
  2920. end;
  2921. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2922. ErrorOnNoElements: boolean): TFPList;
  2923. begin
  2924. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2925. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2926. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2927. end;
  2928. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2929. begin
  2930. Result:=FirstSrcMarker;
  2931. while Result<>nil do
  2932. begin
  2933. if (Result^.Kind=mkLabel)
  2934. and (CompareText(Result^.Identifier,Identifier)=0) then
  2935. exit;
  2936. Result:=Result^.Next;
  2937. end;
  2938. end;
  2939. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2940. ErrorOnNoElements: boolean): TFPList;
  2941. var
  2942. SrcLabel: PSrcMarker;
  2943. begin
  2944. SrcLabel:=FindSrcLabel(Identifier);
  2945. if SrcLabel=nil then
  2946. Fail('missing label "'+Identifier+'"');
  2947. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2948. end;
  2949. function TCustomTestModule.GetDefaultNamespace: string;
  2950. var
  2951. C: TClass;
  2952. begin
  2953. Result:='';
  2954. if FModule=nil then exit;
  2955. C:=FModule.ClassType;
  2956. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2957. Result:=ResolverEngine.DefaultNameSpace;
  2958. end;
  2959. constructor TCustomTestModule.Create;
  2960. begin
  2961. inherited Create;
  2962. FHintMsgs:=TObjectList.Create(true);
  2963. FHintMsgsGood:=TFPList.Create;
  2964. end;
  2965. destructor TCustomTestModule.Destroy;
  2966. begin
  2967. FreeAndNil(FHintMsgs);
  2968. FreeAndNil(FHintMsgsGood);
  2969. inherited Destroy;
  2970. end;
  2971. { TTestModule }
  2972. procedure TTestModule.TestReservedWords;
  2973. var
  2974. i: integer;
  2975. begin
  2976. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2977. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2978. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2979. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2980. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2981. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2982. end;
  2983. procedure TTestModule.TestEmptyProgram;
  2984. begin
  2985. StartProgram(false);
  2986. Add('begin');
  2987. ConvertProgram;
  2988. CheckSource('TestEmptyProgram','','');
  2989. end;
  2990. procedure TTestModule.TestEmptyProgramUseStrict;
  2991. begin
  2992. Converter.Options:=Converter.Options+[coUseStrict];
  2993. StartProgram(false);
  2994. Add('begin');
  2995. ConvertProgram;
  2996. CheckSource('TestEmptyProgramUseStrict','','');
  2997. end;
  2998. procedure TTestModule.TestEmptyUnit;
  2999. begin
  3000. StartUnit(false);
  3001. Add('interface');
  3002. Add('implementation');
  3003. ConvertUnit;
  3004. CheckSource('TestEmptyUnit',
  3005. LinesToStr([
  3006. ]),
  3007. '');
  3008. end;
  3009. procedure TTestModule.TestEmptyUnitUseStrict;
  3010. begin
  3011. Converter.Options:=Converter.Options+[coUseStrict];
  3012. StartUnit(false);
  3013. Add('interface');
  3014. Add('implementation');
  3015. ConvertUnit;
  3016. CheckSource('TestEmptyUnitUseStrict',
  3017. LinesToStr([
  3018. ''
  3019. ]),
  3020. '');
  3021. end;
  3022. procedure TTestModule.TestDottedUnitNames;
  3023. begin
  3024. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3025. LinesToStr([
  3026. 'var iV: longint;'
  3027. ]),
  3028. '');
  3029. FFilename:='ns1.test1.pp';
  3030. StartProgram(true);
  3031. Add('uses unIt2;');
  3032. Add('var');
  3033. Add(' i: longint;');
  3034. Add('begin');
  3035. Add(' i:=iv;');
  3036. Add(' i:=uNit2.iv;');
  3037. Add(' i:=Ns1.TEst1.i;');
  3038. ConvertProgram;
  3039. CheckSource('TestDottedUnitNames',
  3040. LinesToStr([
  3041. 'this.i = 0;',
  3042. '']),
  3043. LinesToStr([ // this.$init
  3044. '$mod.i = pas["NS1.Unit2"].iV;',
  3045. '$mod.i = pas["NS1.Unit2"].iV;',
  3046. '$mod.i = $mod.i;',
  3047. '']) );
  3048. end;
  3049. procedure TTestModule.TestDottedUnitNameImpl;
  3050. begin
  3051. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3052. LinesToStr([
  3053. 'type',
  3054. ' TObject = class end;',
  3055. ' TTestA = class',
  3056. ' end;'
  3057. ]),
  3058. LinesToStr(['uses TEST.UnitB;'])
  3059. );
  3060. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3061. LinesToStr([
  3062. 'uses TEST.UnitA;',
  3063. 'type TTestB = class(TTestA);'
  3064. ]),
  3065. ''
  3066. );
  3067. StartProgram(true);
  3068. Add('uses TEST.UnitA;');
  3069. Add('begin');
  3070. ConvertProgram;
  3071. CheckSource('TestDottedUnitNameImpl',
  3072. LinesToStr([
  3073. '']),
  3074. LinesToStr([ // this.$init
  3075. '']) );
  3076. CheckUnit('TEST.UnitA.pas',
  3077. LinesToStr([
  3078. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3079. ' var $mod = this;',
  3080. ' rtl.createClass(this, "TObject", null, function () {',
  3081. ' this.$init = function () {',
  3082. ' };',
  3083. ' this.$final = function () {',
  3084. ' };',
  3085. ' });',
  3086. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3087. ' });',
  3088. '}, ["TEST.UnitB"]);'
  3089. ]));
  3090. CheckUnit('TEST.UnitB.pas',
  3091. LinesToStr([
  3092. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3093. ' var $mod = this;',
  3094. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3095. ' });',
  3096. '});'
  3097. ]));
  3098. end;
  3099. procedure TTestModule.TestDottedUnitExpr;
  3100. begin
  3101. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3102. LinesToStr([
  3103. 'procedure DoIt;'
  3104. ]),
  3105. 'procedure DoIt; begin end;');
  3106. FFilename:='Ns1.SubNs1.Test1.pp';
  3107. StartProgram(true);
  3108. Add('uses Ns2.sUbnS2.unIt2;');
  3109. Add('var');
  3110. Add(' i: longint;');
  3111. Add('begin');
  3112. Add(' ns2.subns2.unit2.doit;');
  3113. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3114. ConvertProgram;
  3115. CheckSource('TestDottedUnitExpr',
  3116. LinesToStr([
  3117. 'this.i = 0;',
  3118. '']),
  3119. LinesToStr([ // this.$init
  3120. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3121. '$mod.i = $mod.i;',
  3122. '']) );
  3123. end;
  3124. procedure TTestModule.Test_ModeFPCFail;
  3125. begin
  3126. StartProgram(false);
  3127. Add('{$mode FPC}');
  3128. Add('begin');
  3129. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3130. ConvertProgram;
  3131. end;
  3132. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3133. begin
  3134. StartProgram(false);
  3135. Add('{$modeswitch cblocks-}');
  3136. Add('begin');
  3137. ConvertProgram;
  3138. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3139. CheckResolverUnexpectedHints();
  3140. end;
  3141. procedure TTestModule.TestUnit_UseSystem;
  3142. begin
  3143. StartUnit(true);
  3144. Add([
  3145. 'interface',
  3146. 'var i: integer;',
  3147. 'implementation']);
  3148. ConvertUnit;
  3149. CheckSource('TestUnit_UseSystem',
  3150. LinesToStr([
  3151. 'this.i = 0;',
  3152. '']),
  3153. LinesToStr([
  3154. '']) );
  3155. end;
  3156. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3157. begin
  3158. AddModuleWithIntfImplSrc('unit1.pp',
  3159. LinesToStr([
  3160. 'type number = longint;']),
  3161. LinesToStr([
  3162. 'uses test1;',
  3163. 'procedure DoIt;',
  3164. 'begin',
  3165. ' i:=3;',
  3166. 'end;']));
  3167. StartUnit(true);
  3168. Add([
  3169. 'interface',
  3170. 'uses unit1;',
  3171. 'var i: number;',
  3172. 'implementation']);
  3173. ConvertUnit;
  3174. CheckSource('TestUnit_Intf1Impl2Intf1',
  3175. LinesToStr([
  3176. 'this.i = 0;',
  3177. '']),
  3178. LinesToStr([
  3179. '']) );
  3180. end;
  3181. procedure TTestModule.TestIncludeVersion;
  3182. begin
  3183. StartProgram(false);
  3184. Add([
  3185. 'var',
  3186. ' s: string;',
  3187. ' i: word;',
  3188. 'begin',
  3189. ' s:={$I %line%};',
  3190. ' i:={$I %linenum%};',
  3191. ' s:={$I %currentroutine%};',
  3192. ' s:={$I %pas2jsversion%};',
  3193. ' s:={$I %pas2jstarget%};',
  3194. ' s:={$I %pas2jstargetos%};',
  3195. ' s:={$I %pas2jstargetcpu%};',
  3196. ' s:={$I %file%};',
  3197. '']);
  3198. ConvertProgram;
  3199. CheckSource('TestIncludeVersion',
  3200. LinesToStr([
  3201. 'this.s="";',
  3202. 'this.i = 0;']),
  3203. LinesToStr([
  3204. '$mod.s = "7";',
  3205. '$mod.i = 8;',
  3206. '$mod.s = "<anonymous>";',
  3207. '$mod.s = "Comp.Ver.tcmodules";',
  3208. '$mod.s = "Browser";',
  3209. '$mod.s = "Browser";',
  3210. '$mod.s = "ECMAScript5";',
  3211. '$mod.s = "test1.pp";',
  3212. '']));
  3213. end;
  3214. procedure TTestModule.TestVarInt;
  3215. begin
  3216. StartProgram(false);
  3217. Add('var MyI: longint;');
  3218. Add('begin');
  3219. ConvertProgram;
  3220. CheckSource('TestVarInt','this.MyI=0;','');
  3221. end;
  3222. procedure TTestModule.TestVarBaseTypes;
  3223. begin
  3224. StartProgram(false);
  3225. Add('var');
  3226. Add(' i: longint;');
  3227. Add(' s: string;');
  3228. Add(' c: char;');
  3229. Add(' b: boolean;');
  3230. Add(' d: double;');
  3231. Add(' i2: longint = 3;');
  3232. Add(' s2: string = ''foo'';');
  3233. Add(' c2: char = ''4'';');
  3234. Add(' b2: boolean = true;');
  3235. Add(' d2: double = 5.6;');
  3236. Add(' i3: longint = $707;');
  3237. Add(' i4: nativeint = 9007199254740991;');
  3238. Add(' i5: nativeint = -9007199254740991-1;');
  3239. Add(' i6: nativeint = $fffffffffffff;');
  3240. Add(' i7: nativeint = -$fffffffffffff-1;');
  3241. Add(' i8: byte = 00;');
  3242. Add(' u8: nativeuint = $fffffffffffff;');
  3243. Add(' u9: nativeuint = $0000000000000;');
  3244. Add(' u10: nativeuint = $00ff00;');
  3245. Add('begin');
  3246. ConvertProgram;
  3247. CheckSource('TestVarBaseTypes',
  3248. LinesToStr([
  3249. 'this.i = 0;',
  3250. 'this.s = "";',
  3251. 'this.c = "";',
  3252. 'this.b = false;',
  3253. 'this.d = 0.0;',
  3254. 'this.i2 = 3;',
  3255. 'this.s2 = "foo";',
  3256. 'this.c2 = "4";',
  3257. 'this.b2 = true;',
  3258. 'this.d2 = 5.6;',
  3259. 'this.i3 = 0x707;',
  3260. 'this.i4 = 9007199254740991;',
  3261. 'this.i5 = -9007199254740991-1;',
  3262. 'this.i6 = 0xfffffffffffff;',
  3263. 'this.i7 =-0xfffffffffffff-1;',
  3264. 'this.i8 = 0;',
  3265. 'this.u8 = 0xfffffffffffff;',
  3266. 'this.u9 = 0x0;',
  3267. 'this.u10 = 0xff00;'
  3268. ]),
  3269. '');
  3270. end;
  3271. procedure TTestModule.TestBaseTypeSingleFail;
  3272. begin
  3273. StartProgram(false);
  3274. Add('var s: single;');
  3275. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3276. ConvertProgram;
  3277. end;
  3278. procedure TTestModule.TestBaseTypeExtendedFail;
  3279. begin
  3280. StartProgram(false);
  3281. Add('var e: extended;');
  3282. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3283. ConvertProgram;
  3284. end;
  3285. procedure TTestModule.TestConstBaseTypes;
  3286. begin
  3287. StartProgram(false);
  3288. Add('const');
  3289. Add(' i: longint = 3;');
  3290. Add(' s: string = ''foo'';');
  3291. Add(' c: char = ''4'';');
  3292. Add(' b: boolean = true;');
  3293. Add(' d: double = 5.6;');
  3294. Add(' e = low(word);');
  3295. Add(' f = high(word);');
  3296. Add('begin');
  3297. ConvertProgram;
  3298. CheckSource('TestVarBaseTypes',
  3299. LinesToStr([
  3300. 'this.i=3;',
  3301. 'this.s="foo";',
  3302. 'this.c="4";',
  3303. 'this.b=true;',
  3304. 'this.d=5.6;',
  3305. 'this.e = 0;',
  3306. 'this.f = 65535;'
  3307. ]),
  3308. '');
  3309. end;
  3310. procedure TTestModule.TestAliasTypeRef;
  3311. begin
  3312. StartProgram(false);
  3313. Add('type');
  3314. Add(' a=longint;');
  3315. Add(' b=a;');
  3316. Add('var');
  3317. Add(' c: A;');
  3318. Add(' d: B;');
  3319. Add('begin');
  3320. ConvertProgram;
  3321. CheckSource('TestAliasTypeRef',
  3322. LinesToStr([ // statements
  3323. 'this.c = 0;',
  3324. 'this.d = 0;'
  3325. ]),
  3326. LinesToStr([ // this.$main
  3327. ''
  3328. ]));
  3329. end;
  3330. procedure TTestModule.TestTypeCast_BaseTypes;
  3331. begin
  3332. StartProgram(false);
  3333. Add([
  3334. 'var',
  3335. ' i: longint;',
  3336. ' b: boolean;',
  3337. ' d: double;',
  3338. ' s: string;',
  3339. ' c: char;',
  3340. 'begin',
  3341. ' i:=longint(i);',
  3342. ' i:=longint(b);',
  3343. ' b:=boolean(b);',
  3344. ' b:=boolean(i);',
  3345. ' d:=double(d);',
  3346. ' d:=double(i);',
  3347. ' s:=string(s);',
  3348. ' s:=string(c);',
  3349. ' c:=char(c);',
  3350. ' c:=char(i);',
  3351. ' c:=char(65);',
  3352. ' c:=char(#10);',
  3353. ' c:=char(#$E000);',
  3354. '']);
  3355. ConvertProgram;
  3356. CheckSource('TestAliasTypeRef',
  3357. LinesToStr([ // statements
  3358. 'this.i = 0;',
  3359. 'this.b = false;',
  3360. 'this.d = 0.0;',
  3361. 'this.s = "";',
  3362. 'this.c = "";',
  3363. '']),
  3364. LinesToStr([ // this.$main
  3365. '$mod.i = $mod.i;',
  3366. '$mod.i = ($mod.b ? 1 : 0);',
  3367. '$mod.b = $mod.b;',
  3368. '$mod.b = $mod.i != 0;',
  3369. '$mod.d = $mod.d;',
  3370. '$mod.d = $mod.i;',
  3371. '$mod.s = $mod.s;',
  3372. '$mod.s = $mod.c;',
  3373. '$mod.c = $mod.c;',
  3374. '$mod.c = String.fromCharCode($mod.i);',
  3375. '$mod.c = "A";',
  3376. '$mod.c = "\n";',
  3377. '$mod.c = "";',
  3378. '']));
  3379. end;
  3380. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3381. begin
  3382. StartProgram(false);
  3383. Add('type');
  3384. Add(' integer = longint;');
  3385. Add(' TYesNo = boolean;');
  3386. Add(' TFloat = double;');
  3387. Add(' TCaption = string;');
  3388. Add(' TChar = char;');
  3389. Add('var');
  3390. Add(' i: integer;');
  3391. Add(' b: TYesNo;');
  3392. Add(' d: TFloat;');
  3393. Add(' s: TCaption;');
  3394. Add(' c: TChar;');
  3395. Add('begin');
  3396. Add(' i:=integer(i);');
  3397. Add(' i:=integer(b);');
  3398. Add(' b:=TYesNo(b);');
  3399. Add(' b:=TYesNo(i);');
  3400. Add(' d:=TFloat(d);');
  3401. Add(' d:=TFloat(i);');
  3402. Add(' s:=TCaption(s);');
  3403. Add(' s:=TCaption(c);');
  3404. Add(' c:=TChar(c);');
  3405. ConvertProgram;
  3406. CheckSource('TestAliasTypeRef',
  3407. LinesToStr([ // statements
  3408. 'this.i = 0;',
  3409. 'this.b = false;',
  3410. 'this.d = 0.0;',
  3411. 'this.s = "";',
  3412. 'this.c = "";',
  3413. '']),
  3414. LinesToStr([ // this.$main
  3415. '$mod.i = $mod.i;',
  3416. '$mod.i = ($mod.b ? 1 : 0);',
  3417. '$mod.b = $mod.b;',
  3418. '$mod.b = $mod.i != 0;',
  3419. '$mod.d = $mod.d;',
  3420. '$mod.d = $mod.i;',
  3421. '$mod.s = $mod.s;',
  3422. '$mod.s = $mod.c;',
  3423. '$mod.c = $mod.c;',
  3424. '']));
  3425. end;
  3426. procedure TTestModule.TestEmptyProc;
  3427. begin
  3428. StartProgram(false);
  3429. Add('procedure Test;');
  3430. Add('begin');
  3431. Add('end;');
  3432. Add('begin');
  3433. ConvertProgram;
  3434. CheckSource('TestEmptyProc',
  3435. LinesToStr([ // statements
  3436. 'this.Test = function () {',
  3437. '};'
  3438. ]),
  3439. LinesToStr([ // this.$main
  3440. ''
  3441. ]));
  3442. end;
  3443. procedure TTestModule.TestProcOneParam;
  3444. begin
  3445. StartProgram(false);
  3446. Add('procedure ProcA(i: longint);');
  3447. Add('begin');
  3448. Add('end;');
  3449. Add('begin');
  3450. Add(' PROCA(3);');
  3451. ConvertProgram;
  3452. CheckSource('TestProcOneParam',
  3453. LinesToStr([ // statements
  3454. 'this.ProcA = function (i) {',
  3455. '};'
  3456. ]),
  3457. LinesToStr([ // this.$main
  3458. '$mod.ProcA(3);'
  3459. ]));
  3460. end;
  3461. procedure TTestModule.TestFunctionWithoutParams;
  3462. begin
  3463. StartProgram(false);
  3464. Add('function FuncA: longint;');
  3465. Add('begin');
  3466. Add('end;');
  3467. Add('var i: longint;');
  3468. Add('begin');
  3469. Add(' I:=FUNCA();');
  3470. Add(' I:=FUNCA;');
  3471. Add(' FUNCA();');
  3472. Add(' FUNCA;');
  3473. ConvertProgram;
  3474. CheckSource('TestProcWithoutParams',
  3475. LinesToStr([ // statements
  3476. 'this.FuncA = function () {',
  3477. ' var Result = 0;',
  3478. ' return Result;',
  3479. '};',
  3480. 'this.i=0;'
  3481. ]),
  3482. LinesToStr([ // this.$main
  3483. '$mod.i=$mod.FuncA();',
  3484. '$mod.i=$mod.FuncA();',
  3485. '$mod.FuncA();',
  3486. '$mod.FuncA();'
  3487. ]));
  3488. end;
  3489. procedure TTestModule.TestProcedureWithoutParams;
  3490. begin
  3491. StartProgram(false);
  3492. Add('procedure ProcA;');
  3493. Add('begin');
  3494. Add('end;');
  3495. Add('begin');
  3496. Add(' PROCA();');
  3497. Add(' PROCA;');
  3498. ConvertProgram;
  3499. CheckSource('TestProcWithoutParams',
  3500. LinesToStr([ // statements
  3501. 'this.ProcA = function () {',
  3502. '};'
  3503. ]),
  3504. LinesToStr([ // this.$main
  3505. '$mod.ProcA();',
  3506. '$mod.ProcA();'
  3507. ]));
  3508. end;
  3509. procedure TTestModule.TestIncDec;
  3510. begin
  3511. StartProgram(false);
  3512. Add([
  3513. 'procedure DoIt(var i: longint);',
  3514. 'begin',
  3515. ' inc(i);',
  3516. ' inc(i,2);',
  3517. 'end;',
  3518. 'var',
  3519. ' Bar: longint;',
  3520. 'begin',
  3521. ' inc(bar);',
  3522. ' inc(bar,2);',
  3523. ' dec(bar);',
  3524. ' dec(bar,3);',
  3525. '']);
  3526. ConvertProgram;
  3527. CheckSource('TestIncDec',
  3528. LinesToStr([ // statements
  3529. 'this.DoIt = function (i) {',
  3530. ' i.set(i.get()+1);',
  3531. ' i.set(i.get()+2);',
  3532. '};',
  3533. 'this.Bar = 0;'
  3534. ]),
  3535. LinesToStr([ // this.$main
  3536. '$mod.Bar+=1;',
  3537. '$mod.Bar+=2;',
  3538. '$mod.Bar-=1;',
  3539. '$mod.Bar-=3;'
  3540. ]));
  3541. end;
  3542. procedure TTestModule.TestLoHiFpcMode;
  3543. begin
  3544. StartProgram(false);
  3545. Add([
  3546. '{$mode objfpc}',
  3547. 'const',
  3548. ' LoByte1 = Lo(Word($1234));',
  3549. ' HiByte1 = Hi(Word($1234));',
  3550. ' LoByte2 = Lo(SmallInt($1234));',
  3551. ' HiByte2 = Hi(SmallInt($1234));',
  3552. ' LoWord1 = Lo($1234CDEF);',
  3553. ' HiWord1 = Hi($1234CDEF);',
  3554. ' LoWord2 = Lo(-$1234CDEF);',
  3555. ' HiWord2 = Hi(-$1234CDEF);',
  3556. ' lo4:byte=lo(byte($34));',
  3557. ' hi4:byte=hi(byte($34));',
  3558. ' lo5:byte=lo(shortint(-$34));',
  3559. ' hi5:byte=hi(shortint(-$34));',
  3560. ' lo6:longword=lo($123456789ABCD);',
  3561. ' hi6:longword=hi($123456789ABCD);',
  3562. ' lo7:longword=lo(-$123456789ABCD);',
  3563. ' hi7:longword=hi(-$123456789ABCD);',
  3564. 'var',
  3565. ' b: Byte;',
  3566. ' ss: shortint;',
  3567. ' w: Word;',
  3568. ' si: SmallInt;',
  3569. ' lw: LongWord;',
  3570. ' li: LongInt;',
  3571. ' b2: Byte;',
  3572. ' ni: nativeint;',
  3573. 'begin',
  3574. ' w := $1234;',
  3575. ' ss := -$12;',
  3576. ' b := lo(ss);',
  3577. ' b := HI(ss);',
  3578. ' b := lo(w);',
  3579. ' b := HI(w);',
  3580. ' b2 := lo(b);',
  3581. ' b2 := hi(b);',
  3582. ' lw := $1234CDEF;',
  3583. ' w := lo(lw);',
  3584. ' w := hi(lw);',
  3585. ' ni := $123456789ABCD;',
  3586. ' lw := lo(ni);',
  3587. ' lw := hi(ni);',
  3588. '']);
  3589. ConvertProgram;
  3590. CheckSource('TestLoHiFpcMode',
  3591. LinesToStr([ // statements
  3592. 'this.LoByte1 = 0x1234 & 0xFF;',
  3593. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3594. 'this.LoByte2 = 0x1234 & 0xFF;',
  3595. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3596. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3597. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3598. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3599. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3600. 'this.lo4 = 0x34 & 0xF;',
  3601. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3602. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3603. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3604. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3605. 'this.hi6 = 74565 >>> 0;',
  3606. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3607. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3608. 'this.b = 0;',
  3609. 'this.ss = 0;',
  3610. 'this.w = 0;',
  3611. 'this.si = 0;',
  3612. 'this.lw = 0;',
  3613. 'this.li = 0;',
  3614. 'this.b2 = 0;',
  3615. 'this.ni = 0;',
  3616. '']),
  3617. LinesToStr([ // this.$main
  3618. '$mod.w = 0x1234;',
  3619. '$mod.ss = -0x12;',
  3620. '$mod.b = $mod.ss & 0xFF;',
  3621. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3622. '$mod.b = $mod.w & 0xFF;',
  3623. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3624. '$mod.b2 = $mod.b & 0xF;',
  3625. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3626. '$mod.lw = 0x1234CDEF;',
  3627. '$mod.w = $mod.lw & 0xFFFF;',
  3628. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3629. '$mod.ni = 0x123456789ABCD;',
  3630. '$mod.lw = $mod.ni >>> 0;',
  3631. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3632. '']));
  3633. end;
  3634. procedure TTestModule.TestLoHiDelphiMode;
  3635. begin
  3636. StartProgram(false);
  3637. Add([
  3638. '{$mode delphi}',
  3639. 'const',
  3640. ' LoByte1 = Lo(Word($1234));',
  3641. ' HiByte1 = Hi(Word($1234));',
  3642. ' LoByte2 = Lo(SmallInt($1234));',
  3643. ' HiByte2 = Hi(SmallInt($1234));',
  3644. ' LoByte3 = Lo($1234CDEF);',
  3645. ' HiByte3 = Hi($1234CDEF);',
  3646. ' LoByte4 = Lo(-$1234CDEF);',
  3647. ' HiByte4 = Hi(-$1234CDEF);',
  3648. 'var',
  3649. ' b: Byte;',
  3650. ' w: Word;',
  3651. ' si: SmallInt;',
  3652. ' lw: LongWord;',
  3653. ' li: LongInt;',
  3654. 'begin',
  3655. ' w := $1234;',
  3656. ' b := lo(w);',
  3657. ' b := HI(w);',
  3658. ' lw := $1234CDEF;',
  3659. ' b := lo(lw);',
  3660. ' b := hi(lw);',
  3661. '']);
  3662. ConvertProgram;
  3663. CheckSource('TestLoHiDelphiMode',
  3664. LinesToStr([ // statements
  3665. 'this.LoByte1 = 0x1234 & 0xFF;',
  3666. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3667. 'this.LoByte2 = 0x1234 & 0xFF;',
  3668. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3669. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3670. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3671. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3672. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3673. 'this.b = 0;',
  3674. 'this.w = 0;',
  3675. 'this.si = 0;',
  3676. 'this.lw = 0;',
  3677. 'this.li = 0;'
  3678. ]),
  3679. LinesToStr([ // this.$main
  3680. '$mod.w = 0x1234;',
  3681. '$mod.b = $mod.w & 0xFF;',
  3682. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3683. '$mod.lw = 0x1234CDEF;',
  3684. '$mod.b = $mod.lw & 0xFF;',
  3685. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3686. ]));
  3687. end;
  3688. procedure TTestModule.TestAssignments;
  3689. begin
  3690. StartProgram(false);
  3691. Parser.Options:=Parser.Options+[po_cassignments];
  3692. Add('var');
  3693. Add(' Bar:longint;');
  3694. Add('begin');
  3695. Add(' bar:=3;');
  3696. Add(' bar+=4;');
  3697. Add(' bar-=5;');
  3698. Add(' bar*=6;');
  3699. ConvertProgram;
  3700. CheckSource('TestAssignments',
  3701. LinesToStr([ // statements
  3702. 'this.Bar = 0;'
  3703. ]),
  3704. LinesToStr([ // this.$main
  3705. '$mod.Bar=3;',
  3706. '$mod.Bar+=4;',
  3707. '$mod.Bar-=5;',
  3708. '$mod.Bar*=6;'
  3709. ]));
  3710. end;
  3711. procedure TTestModule.TestArithmeticOperators1;
  3712. begin
  3713. StartProgram(false);
  3714. Add('var');
  3715. Add(' vA,vB,vC:longint;');
  3716. Add('begin');
  3717. Add(' va:=1;');
  3718. Add(' vb:=va+va;');
  3719. Add(' vb:=va div vb;');
  3720. Add(' vb:=va mod vb;');
  3721. Add(' vb:=va+va*vb+va div vb;');
  3722. Add(' vc:=-va;');
  3723. Add(' va:=va-vb;');
  3724. Add(' vb:=va;');
  3725. Add(' if va<vb then vc:=va else vc:=vb;');
  3726. ConvertProgram;
  3727. CheckSource('TestArithmeticOperators1',
  3728. LinesToStr([ // statements
  3729. 'this.vA = 0;',
  3730. 'this.vB = 0;',
  3731. 'this.vC = 0;'
  3732. ]),
  3733. LinesToStr([ // this.$main
  3734. '$mod.vA = 1;',
  3735. '$mod.vB = $mod.vA + $mod.vA;',
  3736. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3737. '$mod.vB = $mod.vA % $mod.vB;',
  3738. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3739. '$mod.vC = -$mod.vA;',
  3740. '$mod.vA = $mod.vA - $mod.vB;',
  3741. '$mod.vB = $mod.vA;',
  3742. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3743. ]));
  3744. end;
  3745. procedure TTestModule.TestMultiAdd;
  3746. begin
  3747. StartProgram(false);
  3748. Add([
  3749. 'function Fly: string; external name ''fly'';',
  3750. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3751. 'var',
  3752. ' Date: double;',
  3753. 'begin',
  3754. ' Result:=(Year>0) and (Year<10000) and',
  3755. ' (Month >= 1) and (Month<=12) and',
  3756. ' (Day>0) and (Day<=31);',
  3757. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3758. 'end;',
  3759. 'var s: string;',
  3760. 'begin',
  3761. ' s:=''a''+''b''+''c''+''d'';',
  3762. ' s:=s+Fly+''e'';',
  3763. ' s:=Fly+Fly+Fly;',
  3764. '']);
  3765. ConvertProgram;
  3766. CheckSource('TestMultiAdd',
  3767. LinesToStr([ // statements
  3768. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3769. ' var Result = false;',
  3770. ' var date = 0.0;',
  3771. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3772. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3773. ' return Result;',
  3774. '};',
  3775. 'this.s = "";',
  3776. '']),
  3777. LinesToStr([ // this.$main
  3778. '$mod.s = "a" + "b" + "c" + "d";',
  3779. '$mod.s = $mod.s + fly() + "e";',
  3780. '$mod.s = fly() + fly() + fly();',
  3781. '']));
  3782. end;
  3783. procedure TTestModule.TestLogicalOperators;
  3784. begin
  3785. StartProgram(false);
  3786. Add('var');
  3787. Add(' vA,vB,vC:boolean;');
  3788. Add('begin');
  3789. Add(' va:=vb and vc;');
  3790. Add(' va:=vb or vc;');
  3791. Add(' va:=vb xor vc;');
  3792. Add(' va:=true and vc;');
  3793. Add(' va:=(vb and vc) or (va and vb);');
  3794. Add(' va:=not vb;');
  3795. ConvertProgram;
  3796. CheckSource('TestLogicalOperators',
  3797. LinesToStr([ // statements
  3798. 'this.vA = false;',
  3799. 'this.vB = false;',
  3800. 'this.vC = false;'
  3801. ]),
  3802. LinesToStr([ // this.$main
  3803. '$mod.vA = $mod.vB && $mod.vC;',
  3804. '$mod.vA = $mod.vB || $mod.vC;',
  3805. '$mod.vA = $mod.vB ^ $mod.vC;',
  3806. '$mod.vA = true && $mod.vC;',
  3807. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3808. '$mod.vA = !$mod.vB;'
  3809. ]));
  3810. end;
  3811. procedure TTestModule.TestBitwiseOperators;
  3812. begin
  3813. StartProgram(false);
  3814. Add([
  3815. 'var',
  3816. ' vA,vB,vC:longint;',
  3817. ' X,Y,Z: nativeint;',
  3818. 'begin',
  3819. ' va:=vb and vc;',
  3820. ' va:=vb or vc;',
  3821. ' va:=vb xor vc;',
  3822. ' va:=vb shl vc;',
  3823. ' va:=vb shr vc;',
  3824. ' va:=3 and vc;',
  3825. ' va:=(vb and vc) or (va and vb);',
  3826. ' va:=not vb;',
  3827. ' X:=Y and Z;',
  3828. ' X:=Y and va;',
  3829. ' X:=Y or Z;',
  3830. ' X:=Y or va;',
  3831. ' X:=Y xor Z;',
  3832. ' X:=Y xor va;',
  3833. '']);
  3834. ConvertProgram;
  3835. CheckSource('TestBitwiseOperators',
  3836. LinesToStr([ // statements
  3837. 'this.vA = 0;',
  3838. 'this.vB = 0;',
  3839. 'this.vC = 0;',
  3840. 'this.X = 0;',
  3841. 'this.Y = 0;',
  3842. 'this.Z = 0;',
  3843. '']),
  3844. LinesToStr([ // this.$main
  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 = $mod.vB >>> $mod.vC;',
  3850. '$mod.vA = 3 & $mod.vC;',
  3851. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3852. '$mod.vA = ~$mod.vB;',
  3853. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3854. '$mod.X = $mod.Y & $mod.vA;',
  3855. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3856. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3857. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3858. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3859. '']));
  3860. end;
  3861. procedure TTestModule.TestBitwiseOperatorsLongword;
  3862. begin
  3863. StartProgram(false);
  3864. Add([
  3865. 'var',
  3866. ' a,b,c:longword;',
  3867. ' i: longint;',
  3868. 'begin',
  3869. ' a:=$12345678;',
  3870. ' b:=$EDCBA987;',
  3871. ' c:=not a;',
  3872. ' c:=a and b;',
  3873. ' c:=a and $ffff0000;',
  3874. ' c:=a or b;',
  3875. ' c:=a or $ff00ff00;',
  3876. ' c:=a xor b;',
  3877. ' c:=a xor $f0f0f0f0;',
  3878. ' c:=a shl 1;',
  3879. ' c:=a shl 16;',
  3880. ' c:=a shl 24;',
  3881. ' c:=a shl b;',
  3882. ' c:=a shr 1;',
  3883. ' c:=a shr 16;',
  3884. ' c:=a shr 24;',
  3885. ' c:=a shr b;',
  3886. ' c:=(b and c) or (a and b);',
  3887. ' c:=i and a;',
  3888. ' c:=i or a;',
  3889. ' c:=i xor a;',
  3890. '']);
  3891. ConvertProgram;
  3892. CheckSource('TestBitwiseOperatorsLongword',
  3893. LinesToStr([ // statements
  3894. 'this.a = 0;',
  3895. 'this.b = 0;',
  3896. 'this.c = 0;',
  3897. 'this.i = 0;',
  3898. '']),
  3899. LinesToStr([ // this.$main
  3900. '$mod.a = 0x12345678;',
  3901. '$mod.b = 0xEDCBA987;',
  3902. '$mod.c = rtl.lw(~$mod.a);',
  3903. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3904. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3905. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3906. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3907. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3908. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3909. '$mod.c = rtl.lw($mod.a << 1);',
  3910. '$mod.c = rtl.lw($mod.a << 16);',
  3911. '$mod.c = rtl.lw($mod.a << 24);',
  3912. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3913. '$mod.c = rtl.lw($mod.a >>> 1);',
  3914. '$mod.c = rtl.lw($mod.a >>> 16);',
  3915. '$mod.c = rtl.lw($mod.a >>> 24);',
  3916. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3917. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3918. '$mod.c = $mod.i & $mod.a;',
  3919. '$mod.c = $mod.i | $mod.a;',
  3920. '$mod.c = $mod.i ^ $mod.a;',
  3921. '']));
  3922. end;
  3923. procedure TTestModule.TestPrgProcVar;
  3924. begin
  3925. StartProgram(false);
  3926. Add('procedure Proc1;');
  3927. Add('type');
  3928. Add(' t1=longint;');
  3929. Add('var');
  3930. Add(' vA:t1;');
  3931. Add('begin');
  3932. Add('end;');
  3933. Add('begin');
  3934. ConvertProgram;
  3935. CheckSource('TestPrgProcVar',
  3936. LinesToStr([ // statements
  3937. 'this.Proc1 = function () {',
  3938. ' var vA=0;',
  3939. '};'
  3940. ]),
  3941. LinesToStr([ // this.$main
  3942. ''
  3943. ]));
  3944. end;
  3945. procedure TTestModule.TestUnitProcVar;
  3946. begin
  3947. StartUnit(false);
  3948. Add('interface');
  3949. Add('');
  3950. Add('type tA=string; // unit scope');
  3951. Add('procedure Proc1;');
  3952. Add('');
  3953. Add('implementation');
  3954. Add('');
  3955. Add('procedure Proc1;');
  3956. Add('type tA=longint; // local proc scope');
  3957. Add('var v1:tA; // using local tA');
  3958. Add('begin');
  3959. Add('end;');
  3960. Add('var v2:tA; // using interface tA');
  3961. ConvertUnit;
  3962. CheckSource('TestUnitProcVar',
  3963. LinesToStr([ // statements
  3964. 'var $impl = $mod.$impl;',
  3965. 'this.Proc1 = function () {',
  3966. ' var v1 = 0;',
  3967. '};',
  3968. '']),
  3969. // this.$init
  3970. '',
  3971. // implementation
  3972. LinesToStr([
  3973. '$impl.v2 = "";',
  3974. '']));
  3975. end;
  3976. procedure TTestModule.TestImplProc;
  3977. begin
  3978. StartUnit(false);
  3979. Add('interface');
  3980. Add('');
  3981. Add('procedure Proc1;');
  3982. Add('');
  3983. Add('implementation');
  3984. Add('');
  3985. Add('procedure Proc1; begin end;');
  3986. Add('procedure Proc2; begin end;');
  3987. Add('initialization');
  3988. Add(' Proc1;');
  3989. Add(' Proc2;');
  3990. ConvertUnit;
  3991. CheckSource('TestImplProc',
  3992. LinesToStr([ // statements
  3993. 'var $impl = $mod.$impl;',
  3994. 'this.Proc1 = function () {',
  3995. '};',
  3996. '']),
  3997. LinesToStr([ // this.$init
  3998. '$mod.Proc1();',
  3999. '$impl.Proc2();',
  4000. '']),
  4001. LinesToStr([ // implementation
  4002. '$impl.Proc2 = function () {',
  4003. '};',
  4004. ''])
  4005. );
  4006. end;
  4007. procedure TTestModule.TestFunctionResult;
  4008. begin
  4009. StartProgram(false);
  4010. Add('function Func1: longint;');
  4011. Add('begin');
  4012. Add(' Result:=3;');
  4013. Add(' Func1:=4;');
  4014. Add('end;');
  4015. Add('begin');
  4016. ConvertProgram;
  4017. CheckSource('TestFunctionResult',
  4018. LinesToStr([ // statements
  4019. 'this.Func1 = function () {',
  4020. ' var Result = 0;',
  4021. ' Result = 3;',
  4022. ' Result = 4;',
  4023. ' return Result;',
  4024. '};'
  4025. ]),
  4026. '');
  4027. end;
  4028. procedure TTestModule.TestNestedProc;
  4029. begin
  4030. StartProgram(false);
  4031. Add([
  4032. 'var vInUnit: longint;',
  4033. 'function DoIt(pA,pD: longint): longint;',
  4034. 'var',
  4035. ' vB: longint;',
  4036. ' vC: longint;',
  4037. ' function Nesty(pA: longint): longint; ',
  4038. ' var vB: longint;',
  4039. ' begin',
  4040. ' Result:=pa+vb+vc+pd+vInUnit;',
  4041. ' nesty:=3;',
  4042. ' doit:=4;',
  4043. ' exit;',
  4044. ' end;',
  4045. 'begin',
  4046. ' Result:=pa+vb+vc;',
  4047. ' doit:=6;',
  4048. ' exit;',
  4049. 'end;',
  4050. 'begin']);
  4051. ConvertProgram;
  4052. CheckSource('TestNestedProc',
  4053. LinesToStr([ // statements
  4054. 'this.vInUnit = 0;',
  4055. 'this.DoIt = function (pA, pD) {',
  4056. ' var Result = 0;',
  4057. ' var vB = 0;',
  4058. ' var vC = 0;',
  4059. ' function Nesty(pA) {',
  4060. ' var Result$1 = 0;',
  4061. ' var vB = 0;',
  4062. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4063. ' Result$1 = 3;',
  4064. ' Result = 4;',
  4065. ' return Result$1;',
  4066. ' return Result$1;',
  4067. ' };',
  4068. ' Result = pA + vB + vC;',
  4069. ' Result = 6;',
  4070. ' return Result;',
  4071. ' return Result;',
  4072. '};'
  4073. ]),
  4074. '');
  4075. end;
  4076. procedure TTestModule.TestNestedProc_ResultString;
  4077. begin
  4078. StartProgram(false);
  4079. Add([
  4080. 'function DoIt: string;',
  4081. ' function Nesty: string; ',
  4082. ' begin',
  4083. ' nesty:=#65#66;',
  4084. ' nesty[1]:=#67;',
  4085. ' doit:=#68;',
  4086. ' doit[2]:=#69;',
  4087. ' end;',
  4088. 'begin',
  4089. ' doit:=#70;',
  4090. ' doit[3]:=#71;',
  4091. 'end;',
  4092. 'begin']);
  4093. ConvertProgram;
  4094. CheckSource('TestNestedProc_ResultString',
  4095. LinesToStr([ // statements
  4096. 'this.DoIt = function () {',
  4097. ' var Result = "";',
  4098. ' function Nesty() {',
  4099. ' var Result$1 = "";',
  4100. ' Result$1 = "AB";',
  4101. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4102. ' Result = "D";',
  4103. ' Result = rtl.setCharAt(Result, 1, "E");',
  4104. ' return Result$1;',
  4105. ' };',
  4106. ' Result = "F";',
  4107. ' Result = rtl.setCharAt(Result, 2, "G");',
  4108. ' return Result;',
  4109. '};'
  4110. ]),
  4111. '');
  4112. end;
  4113. procedure TTestModule.TestForwardProc;
  4114. begin
  4115. StartProgram(false);
  4116. Add('procedure FuncA(Bar: longint); forward;');
  4117. Add('procedure FuncB(Bar: longint);');
  4118. Add('begin');
  4119. Add(' funca(bar);');
  4120. Add('end;');
  4121. Add('procedure funca(bar: longint);');
  4122. Add('begin');
  4123. Add(' if bar=3 then ;');
  4124. Add('end;');
  4125. Add('begin');
  4126. Add(' funca(4);');
  4127. Add(' funcb(5);');
  4128. ConvertProgram;
  4129. CheckSource('TestForwardProc',
  4130. LinesToStr([ // statements'
  4131. 'this.FuncB = function (Bar) {',
  4132. ' $mod.FuncA(Bar);',
  4133. '};',
  4134. 'this.FuncA = function (Bar) {',
  4135. ' if (Bar === 3);',
  4136. '};'
  4137. ]),
  4138. LinesToStr([
  4139. '$mod.FuncA(4);',
  4140. '$mod.FuncB(5);'
  4141. ])
  4142. );
  4143. end;
  4144. procedure TTestModule.TestNestedForwardProc;
  4145. begin
  4146. StartProgram(false);
  4147. Add('procedure FuncA;');
  4148. Add(' procedure FuncB(i: longint); forward;');
  4149. Add(' procedure FuncC(i: longint);');
  4150. Add(' begin');
  4151. Add(' funcb(i);');
  4152. Add(' end;');
  4153. Add(' procedure FuncB(i: longint);');
  4154. Add(' begin');
  4155. Add(' if i=3 then ;');
  4156. Add(' end;');
  4157. Add('begin');
  4158. Add(' funcc(4)');
  4159. Add('end;');
  4160. Add('begin');
  4161. Add(' funca;');
  4162. ConvertProgram;
  4163. CheckSource('TestNestedForwardProc',
  4164. LinesToStr([ // statements'
  4165. 'this.FuncA = function () {',
  4166. ' function FuncC(i) {',
  4167. ' FuncB(i);',
  4168. ' };',
  4169. ' function FuncB(i) {',
  4170. ' if (i === 3);',
  4171. ' };',
  4172. ' FuncC(4);',
  4173. '};'
  4174. ]),
  4175. LinesToStr([
  4176. '$mod.FuncA();'
  4177. ])
  4178. );
  4179. end;
  4180. procedure TTestModule.TestAssignFunctionResult;
  4181. begin
  4182. StartProgram(false);
  4183. Add('function Func1: longint;');
  4184. Add('begin');
  4185. Add('end;');
  4186. Add('var i: longint;');
  4187. Add('begin');
  4188. Add(' i:=func1();');
  4189. Add(' i:=func1()+func1();');
  4190. ConvertProgram;
  4191. CheckSource('TestAssignFunctionResult',
  4192. LinesToStr([ // statements
  4193. 'this.Func1 = function () {',
  4194. ' var Result = 0;',
  4195. ' return Result;',
  4196. '};',
  4197. 'this.i = 0;'
  4198. ]),
  4199. LinesToStr([
  4200. '$mod.i = $mod.Func1();',
  4201. '$mod.i = $mod.Func1() + $mod.Func1();'
  4202. ]));
  4203. end;
  4204. procedure TTestModule.TestFunctionResultInCondition;
  4205. begin
  4206. StartProgram(false);
  4207. Add('function Func1: longint;');
  4208. Add('begin');
  4209. Add('end;');
  4210. Add('function Func2: boolean;');
  4211. Add('begin');
  4212. Add('end;');
  4213. Add('var i: longint;');
  4214. Add('begin');
  4215. Add(' if func2 then ;');
  4216. Add(' if i=func1() then ;');
  4217. Add(' if i=func1 then ;');
  4218. ConvertProgram;
  4219. CheckSource('TestFunctionResultInCondition',
  4220. LinesToStr([ // statements
  4221. 'this.Func1 = function () {',
  4222. ' var Result = 0;',
  4223. ' return Result;',
  4224. '};',
  4225. 'this.Func2 = function () {',
  4226. ' var Result = false;',
  4227. ' return Result;',
  4228. '};',
  4229. 'this.i = 0;'
  4230. ]),
  4231. LinesToStr([
  4232. 'if ($mod.Func2());',
  4233. 'if ($mod.i === $mod.Func1());',
  4234. 'if ($mod.i === $mod.Func1());'
  4235. ]));
  4236. end;
  4237. procedure TTestModule.TestFunctionResultInForLoop;
  4238. begin
  4239. StartProgram(false);
  4240. Add([
  4241. 'function Func1(a: array of longint): longint;',
  4242. 'begin',
  4243. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4244. ' for Result in a do if a[Result]=0 then exit;',
  4245. 'end;',
  4246. 'begin',
  4247. ' Func1([1,2,3])']);
  4248. ConvertProgram;
  4249. CheckSource('TestFunctionResultInForLoop',
  4250. LinesToStr([ // statements
  4251. 'this.Func1 = function (a) {',
  4252. ' var Result = 0;',
  4253. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4254. ' Result = $l;',
  4255. ' if (a[Result] === 0) return Result;',
  4256. ' };',
  4257. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4258. ' Result = $in[$l1];',
  4259. ' if (a[Result] === 0) return Result;',
  4260. ' };',
  4261. ' return Result;',
  4262. '};',
  4263. '']),
  4264. LinesToStr([
  4265. '$mod.Func1([1, 2, 3]);'
  4266. ]));
  4267. end;
  4268. procedure TTestModule.TestFunctionResultInTypeCast;
  4269. begin
  4270. StartProgram(false);
  4271. Add([
  4272. 'function GetInt: longint;',
  4273. 'begin',
  4274. 'end;',
  4275. 'begin',
  4276. ' if Byte(GetInt)=0 then ;',
  4277. '']);
  4278. ConvertProgram;
  4279. CheckSource('TestFunctionResultInTypeCast',
  4280. LinesToStr([ // statements
  4281. 'this.GetInt = function () {',
  4282. ' var Result = 0;',
  4283. ' return Result;',
  4284. '};',
  4285. '']),
  4286. LinesToStr([
  4287. 'if (($mod.GetInt() & 255) === 0) ;'
  4288. ]));
  4289. end;
  4290. procedure TTestModule.TestExit;
  4291. begin
  4292. StartProgram(false);
  4293. Add('procedure ProcA;');
  4294. Add('begin');
  4295. Add(' exit;');
  4296. Add('end;');
  4297. Add('function FuncB: longint;');
  4298. Add('begin');
  4299. Add(' exit;');
  4300. Add(' exit(3);');
  4301. Add('end;');
  4302. Add('function FuncC: string;');
  4303. Add('begin');
  4304. Add(' exit;');
  4305. Add(' exit(''a'');');
  4306. Add(' exit(''abc'');');
  4307. Add('end;');
  4308. Add('begin');
  4309. Add(' exit;');
  4310. Add(' exit(1);');
  4311. ConvertProgram;
  4312. CheckSource('TestExit',
  4313. LinesToStr([ // statements
  4314. 'this.ProcA = function () {',
  4315. ' return;',
  4316. '};',
  4317. 'this.FuncB = function () {',
  4318. ' var Result = 0;',
  4319. ' return Result;',
  4320. ' return 3;',
  4321. ' return Result;',
  4322. '};',
  4323. 'this.FuncC = function () {',
  4324. ' var Result = "";',
  4325. ' return Result;',
  4326. ' return "a";',
  4327. ' return "abc";',
  4328. ' return Result;',
  4329. '};'
  4330. ]),
  4331. LinesToStr([
  4332. 'return;',
  4333. 'return 1;',
  4334. '']));
  4335. end;
  4336. procedure TTestModule.TestExit_ResultInFinally;
  4337. begin
  4338. StartProgram(false);
  4339. Add([
  4340. 'function Run: word;',
  4341. 'begin',
  4342. ' try',
  4343. ' exit(3);', // no Result in finally -> use return 3
  4344. ' finally',
  4345. ' end;',
  4346. 'end;',
  4347. 'function Fly: word;',
  4348. 'begin',
  4349. ' try',
  4350. ' exit(3);',
  4351. ' finally',
  4352. ' if Result>0 then ;',
  4353. ' end;',
  4354. 'end;',
  4355. 'function Jump: word;',
  4356. 'begin',
  4357. ' try',
  4358. ' try',
  4359. ' exit(4);',
  4360. ' finally',
  4361. ' end;',
  4362. ' finally',
  4363. ' if Result>0 then ;',
  4364. ' end;',
  4365. 'end;',
  4366. 'begin',
  4367. '']);
  4368. ConvertProgram;
  4369. CheckSource('TestExit_ResultInFinally',
  4370. LinesToStr([ // statements
  4371. 'this.Run = function () {',
  4372. ' var Result = 0;',
  4373. ' try {',
  4374. ' return 3;',
  4375. ' } finally {',
  4376. ' };',
  4377. ' return Result;',
  4378. '};',
  4379. 'this.Fly = function () {',
  4380. ' var Result = 0;',
  4381. ' try {',
  4382. ' Result = 3;',
  4383. ' return Result;',
  4384. ' } finally {',
  4385. ' if (Result > 0) ;',
  4386. ' };',
  4387. ' return Result;',
  4388. '};',
  4389. 'this.Jump = function () {',
  4390. ' var Result = 0;',
  4391. ' try {',
  4392. ' try {',
  4393. ' Result = 4;',
  4394. ' return Result;',
  4395. ' } finally {',
  4396. ' };',
  4397. ' } finally {',
  4398. ' if (Result > 0) ;',
  4399. ' };',
  4400. ' return Result;',
  4401. '};',
  4402. '']),
  4403. LinesToStr([
  4404. '']));
  4405. end;
  4406. procedure TTestModule.TestBreak;
  4407. begin
  4408. StartProgram(false);
  4409. Add([
  4410. 'var',
  4411. ' i: longint;',
  4412. 'begin',
  4413. ' repeat',
  4414. ' break;',
  4415. ' until true;',
  4416. ' while true do',
  4417. ' break;',
  4418. ' for i:=1 to 2 do',
  4419. ' break;']);
  4420. ConvertProgram;
  4421. CheckSource('TestBreak',
  4422. LinesToStr([ // statements
  4423. 'this.i = 0;'
  4424. ]),
  4425. LinesToStr([
  4426. 'do {',
  4427. ' break;',
  4428. '} while (!true);',
  4429. 'while (true) break;',
  4430. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4431. '']));
  4432. end;
  4433. procedure TTestModule.TestBreakAsVar;
  4434. begin
  4435. StartProgram(false);
  4436. Add([
  4437. 'procedure DoIt(break: boolean);',
  4438. 'begin',
  4439. ' if break then ;',
  4440. 'end;',
  4441. 'var',
  4442. ' break: boolean;',
  4443. 'begin',
  4444. ' if break then ;']);
  4445. ConvertProgram;
  4446. CheckSource('TestBreakAsVar',
  4447. LinesToStr([ // statements
  4448. 'this.DoIt = function (Break) {',
  4449. ' if (Break) ;',
  4450. '};',
  4451. 'this.Break = false;',
  4452. '']),
  4453. LinesToStr([
  4454. 'if($mod.Break) ;',
  4455. '']));
  4456. end;
  4457. procedure TTestModule.TestContinue;
  4458. begin
  4459. StartProgram(false);
  4460. Add('var i: longint;');
  4461. Add('begin');
  4462. Add(' repeat');
  4463. Add(' continue;');
  4464. Add(' until true;');
  4465. Add(' while true do');
  4466. Add(' continue;');
  4467. Add(' for i:=1 to 2 do');
  4468. Add(' continue;');
  4469. ConvertProgram;
  4470. CheckSource('TestContinue',
  4471. LinesToStr([ // statements
  4472. 'this.i = 0;'
  4473. ]),
  4474. LinesToStr([
  4475. 'do {',
  4476. ' continue;',
  4477. '} while (!true);',
  4478. 'while (true) continue;',
  4479. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4480. '']));
  4481. end;
  4482. procedure TTestModule.TestProc_External;
  4483. begin
  4484. StartProgram(false);
  4485. Add('procedure Foo; external name ''console.log'';');
  4486. Add('function Bar: longint; external name ''get.item'';');
  4487. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4488. Add('var');
  4489. Add(' i: longint;');
  4490. Add('begin');
  4491. Add(' Foo;');
  4492. Add(' i:=Bar;');
  4493. Add(' i:=Bla(''abc'');');
  4494. ConvertProgram;
  4495. CheckSource('TestProc_External',
  4496. LinesToStr([ // statements
  4497. 'this.i = 0;'
  4498. ]),
  4499. LinesToStr([
  4500. 'console.log();',
  4501. '$mod.i = get.item();',
  4502. '$mod.i = apply.something("abc");'
  4503. ]));
  4504. end;
  4505. procedure TTestModule.TestProc_ExternalOtherUnit;
  4506. begin
  4507. AddModuleWithIntfImplSrc('unit2.pas',
  4508. LinesToStr([
  4509. 'procedure Now; external name ''Date.now'';',
  4510. 'procedure DoIt;'
  4511. ]),
  4512. 'procedure doit; begin end;');
  4513. StartUnit(true);
  4514. Add('interface');
  4515. Add('uses unit2;');
  4516. Add('implementation');
  4517. Add('begin');
  4518. Add(' now;');
  4519. Add(' now();');
  4520. Add(' uNit2.now;');
  4521. Add(' uNit2.now();');
  4522. Add(' doit;');
  4523. Add(' uNit2.doit;');
  4524. ConvertUnit;
  4525. CheckSource('TestProc_ExternalOtherUnit',
  4526. LinesToStr([
  4527. '']),
  4528. LinesToStr([
  4529. 'Date.now();',
  4530. 'Date.now();',
  4531. 'Date.now();',
  4532. 'Date.now();',
  4533. 'pas.unit2.DoIt();',
  4534. 'pas.unit2.DoIt();',
  4535. '']));
  4536. end;
  4537. procedure TTestModule.TestProc_Asm;
  4538. begin
  4539. StartProgram(false);
  4540. Add([
  4541. '{$mode delphi}',
  4542. 'function DoIt: longint;',
  4543. 'begin;',
  4544. ' asm',
  4545. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4546. ' end;',
  4547. ' asm console.log(); end;',
  4548. ' asm',
  4549. ' s = "'' ";',
  4550. ' s = ''" '';',
  4551. ' s = s + "world" + "''";',
  4552. ' // end',
  4553. ' s = ''end'';',
  4554. ' s = "end";',
  4555. ' s = "foo\"bar";',
  4556. ' s = ''a\''b'';',
  4557. ' s = `${expr}\`-"-''-`;',
  4558. ' s = `multi',
  4559. 'line`;',
  4560. ' end;',
  4561. 'end;',
  4562. 'procedure Fly;',
  4563. 'asm',
  4564. ' return;',
  4565. 'end;',
  4566. 'begin']);
  4567. ConvertProgram;
  4568. CheckSource('TestProc_Asm',
  4569. LinesToStr([ // statements
  4570. 'this.DoIt = function () {',
  4571. ' var Result = 0;',
  4572. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4573. ' console.log();',
  4574. ' s = "'' ";',
  4575. ' s = ''" '';',
  4576. ' s = s + "world" + "''";',
  4577. ' // end',
  4578. ' s = ''end'';',
  4579. ' s = "end";',
  4580. ' s = "foo\"bar";',
  4581. ' s = ''a\''b'';',
  4582. ' s = `${expr}\`-"-''-`;',
  4583. ' s = `multi',
  4584. 'line`;',
  4585. ' return Result;',
  4586. '};',
  4587. 'this.Fly = function () {',
  4588. ' return;',
  4589. '};',
  4590. '']),
  4591. LinesToStr([
  4592. ''
  4593. ]));
  4594. end;
  4595. procedure TTestModule.TestProc_AsmSubBlock;
  4596. begin
  4597. StartProgram(true,[supTObject]);
  4598. Add([
  4599. '{$mode delphi}',
  4600. 'type',
  4601. ' TBird = class end;',
  4602. 'procedure Run(w: word);',
  4603. 'begin;',
  4604. ' if true then asm console.log(); end;',
  4605. ' if w>3 then asm',
  4606. ' var a = w+1;',
  4607. ' w = a+3;',
  4608. ' end;',
  4609. ' while (w>7) do asm',
  4610. ' w+=3; w*=2;',
  4611. ' end;',
  4612. ' try',
  4613. ' except',
  4614. ' on E: TBird do',
  4615. ' asm console.log(E); end;',
  4616. ' on E: TObject do',
  4617. ' asm var i=3; i--; end;',
  4618. ' else asm Fly; High; end;',
  4619. ' end;',
  4620. 'end;',
  4621. 'begin']);
  4622. ConvertProgram;
  4623. CheckSource('TestProc_AsmSubBlock',
  4624. LinesToStr([ // statements
  4625. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4626. '});',
  4627. 'this.Run = function (w) {',
  4628. ' if (true) console.log();',
  4629. ' if (w > 3) {',
  4630. ' var a = w+1;',
  4631. ' w = a+3;',
  4632. ' };',
  4633. ' while (w > 7) {',
  4634. ' w+=3; w*=2;',
  4635. ' };',
  4636. ' try {} catch ($e) {',
  4637. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4638. ' var E = $e;',
  4639. ' console.log(E);',
  4640. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4641. ' var E = $e;',
  4642. ' var i=3; i--;',
  4643. ' } else {',
  4644. ' Fly; High;',
  4645. ' }',
  4646. ' };',
  4647. '};',
  4648. '']),
  4649. LinesToStr([
  4650. ''
  4651. ]));
  4652. end;
  4653. procedure TTestModule.TestProc_Assembler;
  4654. begin
  4655. StartProgram(false);
  4656. Add('function DoIt: longint; assembler;');
  4657. Add('asm');
  4658. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4659. Add('end;');
  4660. Add('begin');
  4661. ConvertProgram;
  4662. CheckSource('TestProc_Assembler',
  4663. LinesToStr([ // statements
  4664. 'this.DoIt = function () {',
  4665. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4666. '};'
  4667. ]),
  4668. LinesToStr([
  4669. ''
  4670. ]));
  4671. end;
  4672. procedure TTestModule.TestProc_VarParam;
  4673. begin
  4674. StartProgram(false);
  4675. Add('type integer = longint;');
  4676. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4677. Add('var vJ: integer;');
  4678. Add('begin');
  4679. Add(' vg:=vg+1;');
  4680. Add(' vj:=vh+2;');
  4681. Add(' vi:=vi+3;');
  4682. Add(' doit(vg,vg,vg);');
  4683. Add(' doit(vh,vh,vj);');
  4684. Add(' doit(vi,vi,vi);');
  4685. Add(' doit(vj,vj,vj);');
  4686. Add('end;');
  4687. Add('var i: integer;');
  4688. Add('begin');
  4689. Add(' doit(i,i,i);');
  4690. ConvertProgram;
  4691. CheckSource('TestProc_VarParam',
  4692. LinesToStr([ // statements
  4693. 'this.DoIt = function (vG,vH,vI) {',
  4694. ' var vJ = 0;',
  4695. ' vG = vG + 1;',
  4696. ' vJ = vH + 2;',
  4697. ' vI.set(vI.get()+3);',
  4698. ' $mod.DoIt(vG, vG, {',
  4699. ' get: function () {',
  4700. ' return vG;',
  4701. ' },',
  4702. ' set: function (v) {',
  4703. ' vG = v;',
  4704. ' }',
  4705. ' });',
  4706. ' $mod.DoIt(vH, vH, {',
  4707. ' get: function () {',
  4708. ' return vJ;',
  4709. ' },',
  4710. ' set: function (v) {',
  4711. ' vJ = v;',
  4712. ' }',
  4713. ' });',
  4714. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4715. ' $mod.DoIt(vJ, vJ, {',
  4716. ' get: function () {',
  4717. ' return vJ;',
  4718. ' },',
  4719. ' set: function (v) {',
  4720. ' vJ = v;',
  4721. ' }',
  4722. ' });',
  4723. '};',
  4724. 'this.i = 0;'
  4725. ]),
  4726. LinesToStr([
  4727. '$mod.DoIt($mod.i,$mod.i,{',
  4728. ' p: $mod,',
  4729. ' get: function () {',
  4730. ' return this.p.i;',
  4731. ' },',
  4732. ' set: function (v) {',
  4733. ' this.p.i = v;',
  4734. ' }',
  4735. '});'
  4736. ]));
  4737. end;
  4738. procedure TTestModule.TestProc_VarParamString;
  4739. begin
  4740. StartProgram(false);
  4741. Add(['type TCaption = string;',
  4742. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4743. 'var c: char;',
  4744. 'begin',
  4745. ' va[1]:=c;',
  4746. ' vb[2]:=c;',
  4747. ' vc[3]:=c;',
  4748. 'end;',
  4749. 'begin']);
  4750. ConvertProgram;
  4751. CheckSource('TestProc_VarParamString',
  4752. LinesToStr([ // statements
  4753. 'this.DoIt = function (vA,vB,vC) {',
  4754. ' var c = "";',
  4755. ' vA = rtl.setCharAt(vA, 0, c);',
  4756. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4757. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4758. '};',
  4759. '']),
  4760. LinesToStr([
  4761. ]));
  4762. end;
  4763. procedure TTestModule.TestProc_VarParamV;
  4764. begin
  4765. StartProgram(false);
  4766. Add([
  4767. 'procedure Inc2(var i: longint);',
  4768. 'begin',
  4769. ' i:=i+2;',
  4770. 'end;',
  4771. 'procedure DoIt(v: longint);',
  4772. 'var p: array of longint;',
  4773. 'begin',
  4774. ' Inc2(v);',
  4775. ' Inc2(p[v]);',
  4776. 'end;',
  4777. 'begin']);
  4778. ConvertProgram;
  4779. CheckSource('TestProc_VarParamV',
  4780. LinesToStr([ // statements
  4781. 'this.Inc2 = function (i) {',
  4782. ' i.set(i.get()+2);',
  4783. '};',
  4784. 'this.DoIt = function (v) {',
  4785. ' var p = [];',
  4786. ' $mod.Inc2({get: function () {',
  4787. ' return v;',
  4788. ' }, set: function (w) {',
  4789. ' v = w;',
  4790. ' }});',
  4791. ' $mod.Inc2({',
  4792. ' a: v,',
  4793. ' p: p,',
  4794. ' get: function () {',
  4795. ' return this.p[this.a];',
  4796. ' },',
  4797. ' set: function (v) {',
  4798. ' this.p[this.a] = v;',
  4799. ' }',
  4800. ' });',
  4801. '};',
  4802. '']),
  4803. LinesToStr([
  4804. '']));
  4805. end;
  4806. procedure TTestModule.TestProc_Overload;
  4807. begin
  4808. StartProgram(false);
  4809. Add('procedure DoIt(vI: longint); begin end;');
  4810. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4811. Add('procedure DoIt(vD: double); begin end;');
  4812. Add('begin');
  4813. Add(' DoIt(1);');
  4814. Add(' DoIt(2,3);');
  4815. Add(' DoIt(4.5);');
  4816. ConvertProgram;
  4817. CheckSource('TestProcedureOverload',
  4818. LinesToStr([ // statements
  4819. 'this.DoIt = function (vI) {',
  4820. '};',
  4821. 'this.DoIt$1 = function (vI, vJ) {',
  4822. '};',
  4823. 'this.DoIt$2 = function (vD) {',
  4824. '};',
  4825. '']),
  4826. LinesToStr([
  4827. '$mod.DoIt(1);',
  4828. '$mod.DoIt$1(2, 3);',
  4829. '$mod.DoIt$2(4.5);',
  4830. '']));
  4831. end;
  4832. procedure TTestModule.TestProc_OverloadForward;
  4833. begin
  4834. StartProgram(false);
  4835. Add('procedure DoIt(vI: longint); forward;');
  4836. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4837. Add('procedure doit(vi: longint); begin end;');
  4838. Add('begin');
  4839. Add(' doit(1);');
  4840. Add(' doit(2,3);');
  4841. ConvertProgram;
  4842. CheckSource('TestProcedureOverloadForward',
  4843. LinesToStr([ // statements
  4844. 'this.DoIt$1 = function (vI, vJ) {',
  4845. '};',
  4846. 'this.DoIt = function (vI) {',
  4847. '};',
  4848. '']),
  4849. LinesToStr([
  4850. '$mod.DoIt(1);',
  4851. '$mod.DoIt$1(2, 3);',
  4852. '']));
  4853. end;
  4854. procedure TTestModule.TestProc_OverloadIntfImpl;
  4855. begin
  4856. StartUnit(false);
  4857. Add('interface');
  4858. Add('procedure DoIt(vI: longint);');
  4859. Add('procedure DoIt(vI, vJ: longint);');
  4860. Add('implementation');
  4861. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4862. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4863. Add('procedure DoIt(vi: longint); begin end;');
  4864. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4865. Add('procedure DoIt(vi, vj: longint); begin end;');
  4866. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4867. Add('begin');
  4868. Add(' doit(1);');
  4869. Add(' doit(2,3);');
  4870. Add(' doit(4,5,6);');
  4871. Add(' doit(7,8,9,10);');
  4872. Add(' doit(11,12,13,14,15);');
  4873. ConvertUnit;
  4874. CheckSource('TestProcedureOverloadUnit',
  4875. LinesToStr([ // statements
  4876. 'var $impl = $mod.$impl;',
  4877. 'this.DoIt = function (vI) {',
  4878. '};',
  4879. 'this.DoIt$1 = function (vI, vJ) {',
  4880. '};',
  4881. '']),
  4882. LinesToStr([ // this.$init
  4883. '$mod.DoIt(1);',
  4884. '$mod.DoIt$1(2, 3);',
  4885. '$impl.DoIt$3(4,5,6);',
  4886. '$impl.DoIt$4(7,8,9,10);',
  4887. '$impl.DoIt$2(11,12,13,14,15);',
  4888. '']),
  4889. LinesToStr([ // implementation
  4890. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4891. '};',
  4892. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4893. '};',
  4894. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4895. '};',
  4896. '']));
  4897. end;
  4898. procedure TTestModule.TestProc_OverloadNested;
  4899. begin
  4900. StartProgram(false);
  4901. Add([
  4902. 'procedure doit(vA: longint);',
  4903. ' procedure DoIt(vA, vB: longint); overload;',
  4904. ' begin',
  4905. ' doit(1);',
  4906. ' doit(1,2);',
  4907. ' end;',
  4908. ' procedure doit(vA, vB, vC: longint);',
  4909. ' begin',
  4910. ' doit(1);',
  4911. ' doit(1,2);',
  4912. ' doit(1,2,3);',
  4913. ' end;',
  4914. 'begin',
  4915. ' doit(1);',
  4916. ' doit(1,2);',
  4917. ' doit(1,2,3);',
  4918. 'end;',
  4919. 'begin // main',
  4920. ' doit(1);']);
  4921. ConvertProgram;
  4922. CheckSource('TestProcedureOverloadNested',
  4923. LinesToStr([ // statements
  4924. 'this.doit = function (vA) {',
  4925. ' function DoIt$1(vA, vB) {',
  4926. ' $mod.doit(1);',
  4927. ' DoIt$1(1, 2);',
  4928. ' };',
  4929. ' function doit$2(vA, vB, vC) {',
  4930. ' $mod.doit(1);',
  4931. ' DoIt$1(1, 2);',
  4932. ' doit$2(1, 2, 3);',
  4933. ' };',
  4934. ' $mod.doit(1);',
  4935. ' DoIt$1(1, 2);',
  4936. ' doit$2(1, 2, 3);',
  4937. '};',
  4938. '']),
  4939. LinesToStr([
  4940. '$mod.doit(1);',
  4941. '']));
  4942. end;
  4943. procedure TTestModule.TestProc_OverloadNestedForward;
  4944. begin
  4945. StartProgram(false);
  4946. Add([
  4947. 'procedure DoIt(vA: longint); overload; forward;',
  4948. 'procedure DoIt(vB, vC: longint); overload;',
  4949. 'begin // 2 param overload',
  4950. ' doit(1);',
  4951. ' doit(1,2);',
  4952. 'end;',
  4953. 'procedure doit(vA: longint);',
  4954. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4955. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4956. ' begin // 4 param overload',
  4957. ' doit(1);',
  4958. ' doit(1,2);',
  4959. ' doit(1,2,3);',
  4960. ' doit(1,2,3,4);',
  4961. ' end;',
  4962. ' procedure doit(vA, vB, vC: longint);',
  4963. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4964. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4965. ' begin // 6 param overload',
  4966. ' doit(1);',
  4967. ' doit(1,2);',
  4968. ' doit(1,2,3);',
  4969. ' doit(1,2,3,4);',
  4970. ' doit(1,2,3,4,5);',
  4971. ' doit(1,2,3,4,5,6);',
  4972. ' end;',
  4973. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4974. ' begin // 5 param overload',
  4975. ' doit(1);',
  4976. ' doit(1,2);',
  4977. ' doit(1,2,3);',
  4978. ' doit(1,2,3,4);',
  4979. ' doit(1,2,3,4,5);',
  4980. ' doit(1,2,3,4,5,6);',
  4981. ' end;',
  4982. ' begin // 3 param overload',
  4983. ' doit(1);',
  4984. ' doit(1,2);',
  4985. ' doit(1,2,3);',
  4986. ' doit(1,2,3,4);',
  4987. ' doit(1,2,3,4,5);',
  4988. ' doit(1,2,3,4,5,6);',
  4989. ' end;',
  4990. 'begin // 1 param overload',
  4991. ' doit(1);',
  4992. ' doit(1,2);',
  4993. ' doit(1,2,3);',
  4994. ' doit(1,2,3,4);',
  4995. 'end;',
  4996. 'begin // main',
  4997. ' doit(1);',
  4998. ' doit(1,2);']);
  4999. ConvertProgram;
  5000. CheckSource('TestProc_OverloadNestedForward',
  5001. LinesToStr([ // statements
  5002. 'this.DoIt$1 = function (vB, vC) {',
  5003. ' $mod.DoIt(1);',
  5004. ' $mod.DoIt$1(1, 2);',
  5005. '};',
  5006. 'this.DoIt = function (vA) {',
  5007. ' function DoIt$3(vA, vB, vC, vD) {',
  5008. ' $mod.DoIt(1);',
  5009. ' $mod.DoIt$1(1, 2);',
  5010. ' DoIt$2(1, 2, 3);',
  5011. ' DoIt$3(1, 2, 3, 4);',
  5012. ' };',
  5013. ' function DoIt$2(vA, vB, vC) {',
  5014. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5015. ' $mod.DoIt(1);',
  5016. ' $mod.DoIt$1(1, 2);',
  5017. ' DoIt$2(1, 2, 3);',
  5018. ' DoIt$3(1, 2, 3, 4);',
  5019. ' DoIt$4(1, 2, 3, 4, 5);',
  5020. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5021. ' };',
  5022. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5023. ' $mod.DoIt(1);',
  5024. ' $mod.DoIt$1(1, 2);',
  5025. ' DoIt$2(1, 2, 3);',
  5026. ' DoIt$3(1, 2, 3, 4);',
  5027. ' DoIt$4(1, 2, 3, 4, 5);',
  5028. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5029. ' };',
  5030. ' $mod.DoIt(1);',
  5031. ' $mod.DoIt$1(1, 2);',
  5032. ' DoIt$2(1, 2, 3);',
  5033. ' DoIt$3(1, 2, 3, 4);',
  5034. ' DoIt$4(1, 2, 3, 4, 5);',
  5035. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5036. ' };',
  5037. ' $mod.DoIt(1);',
  5038. ' $mod.DoIt$1(1, 2);',
  5039. ' DoIt$2(1, 2, 3);',
  5040. ' DoIt$3(1, 2, 3, 4);',
  5041. '};',
  5042. '']),
  5043. LinesToStr([
  5044. '$mod.DoIt(1);',
  5045. '$mod.DoIt$1(1, 2);',
  5046. '']));
  5047. end;
  5048. procedure TTestModule.TestProc_OverloadUnitCycle;
  5049. begin
  5050. AddModuleWithIntfImplSrc('Unit2.pas',
  5051. LinesToStr([
  5052. 'type',
  5053. ' TObject = class',
  5054. ' procedure DoIt(b: boolean); virtual; abstract;',
  5055. ' procedure DoIt(i: longint); virtual; abstract;',
  5056. ' end;',
  5057. '']),
  5058. 'uses test1;');
  5059. StartUnit(true);
  5060. Add([
  5061. 'interface',
  5062. 'uses unit2;',
  5063. 'type',
  5064. ' TEagle = class(TObject)',
  5065. ' procedure DoIt(b: boolean); override;',
  5066. ' procedure DoIt(i: longint); override;',
  5067. ' end;',
  5068. 'implementation',
  5069. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5070. 'procedure TEagle.DoIt(i: longint); begin end;',
  5071. '']);
  5072. ConvertUnit;
  5073. CheckSource('TestProc_OverloadUnitCycle',
  5074. LinesToStr([ // statements
  5075. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5076. ' this.DoIt = function (b) {',
  5077. ' };',
  5078. ' this.DoIt$1 = function (i) {',
  5079. ' };',
  5080. '});',
  5081. '']),
  5082. '',
  5083. LinesToStr([
  5084. '']));
  5085. end;
  5086. procedure TTestModule.TestProc_Varargs;
  5087. begin
  5088. StartProgram(false);
  5089. Add([
  5090. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5091. 'procedure ProcB; varargs; external name ''ProcB'';',
  5092. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5093. 'function GetIt: longint; begin end;',
  5094. 'begin',
  5095. ' ProcA(1);',
  5096. ' ProcA(1,2);',
  5097. ' ProcA(1,2.0);',
  5098. ' ProcA(1,2,3);',
  5099. ' ProcA(1,''2'');',
  5100. ' ProcA(2,'''');',
  5101. ' ProcA(3,false);',
  5102. ' ProcB;',
  5103. ' ProcB();',
  5104. ' ProcB(4);',
  5105. ' ProcB(''foo'');',
  5106. ' ProcC;',
  5107. ' ProcC();',
  5108. ' ProcC(4);',
  5109. ' ProcC(5,''foo'');',
  5110. ' ProcB(GetIt);',
  5111. ' ProcB(GetIt());',
  5112. ' ProcB(GetIt,GetIt());']);
  5113. ConvertProgram;
  5114. CheckSource('TestProc_Varargs',
  5115. LinesToStr([ // statements
  5116. 'this.GetIt = function () {',
  5117. ' var Result = 0;',
  5118. ' return Result;',
  5119. '};',
  5120. '']),
  5121. LinesToStr([
  5122. 'ProcA(1);',
  5123. 'ProcA(1, 2);',
  5124. 'ProcA(1, 2.0);',
  5125. 'ProcA(1, 2, 3);',
  5126. 'ProcA(1, "2");',
  5127. 'ProcA(2, "");',
  5128. 'ProcA(3, false);',
  5129. 'ProcB();',
  5130. 'ProcB();',
  5131. 'ProcB(4);',
  5132. 'ProcB("foo");',
  5133. 'ProcC(17);',
  5134. 'ProcC(17);',
  5135. 'ProcC(4);',
  5136. 'ProcC(5, "foo");',
  5137. 'ProcB($mod.GetIt());',
  5138. 'ProcB($mod.GetIt());',
  5139. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5140. '']));
  5141. end;
  5142. procedure TTestModule.TestProc_ConstOrder;
  5143. begin
  5144. StartProgram(false);
  5145. Add([
  5146. 'const A = 3;',
  5147. 'const B = A+1;',
  5148. 'procedure DoIt;',
  5149. 'const C = A+1;',
  5150. 'const D = B+1;',
  5151. 'const E = D+C+B+A;',
  5152. 'begin',
  5153. 'end;',
  5154. 'begin'
  5155. ]);
  5156. ConvertProgram;
  5157. CheckSource('TestProc_ConstOrder',
  5158. LinesToStr([ // statements
  5159. 'this.A = 3;',
  5160. 'this.B = 3 + 1;',
  5161. 'var C = 3 + 1;',
  5162. 'var D = 4 + 1;',
  5163. 'var E = 5 + 4 + 4 + 3;',
  5164. 'this.DoIt = function () {',
  5165. '};',
  5166. '']),
  5167. LinesToStr([
  5168. ''
  5169. ]));
  5170. end;
  5171. procedure TTestModule.TestProc_DuplicateConst;
  5172. begin
  5173. StartProgram(false);
  5174. Add([
  5175. 'const A = 1;',
  5176. 'procedure DoIt;',
  5177. 'const A = 2;',
  5178. ' procedure SubIt;',
  5179. ' const A = 21;',
  5180. ' begin',
  5181. ' end;',
  5182. 'begin',
  5183. 'end;',
  5184. 'procedure DoSome;',
  5185. 'const A = 3;',
  5186. 'begin',
  5187. 'end;',
  5188. 'begin'
  5189. ]);
  5190. ConvertProgram;
  5191. CheckSource('TestProc_DuplicateConst',
  5192. LinesToStr([ // statements
  5193. 'this.A = 1;',
  5194. 'var A$1 = 2;',
  5195. 'var A$2 = 21;',
  5196. 'this.DoIt = function () {',
  5197. ' function SubIt() {',
  5198. ' };',
  5199. '};',
  5200. 'var A$3 = 3;',
  5201. 'this.DoSome = function () {',
  5202. '};',
  5203. '']),
  5204. LinesToStr([
  5205. ''
  5206. ]));
  5207. end;
  5208. procedure TTestModule.TestProc_LocalVarAbsolute;
  5209. begin
  5210. StartProgram(false);
  5211. Add([
  5212. 'type',
  5213. ' TObject = class',
  5214. ' Index: longint;',
  5215. ' procedure DoAbs(Item: pointer);',
  5216. ' end;',
  5217. 'procedure TObject.DoAbs(Item: pointer);',
  5218. 'var',
  5219. ' o: TObject absolute Item;',
  5220. 'begin',
  5221. ' if o.Index<o.Index then o.Index:=o.Index;',
  5222. 'end;',
  5223. 'procedure DoIt(i: longint; p: pointer);',
  5224. 'var',
  5225. ' d: double absolute i;',
  5226. ' s: string absolute d;',
  5227. ' oi: TObject absolute i;',
  5228. ' op: TObject absolute p;',
  5229. 'begin',
  5230. ' if d=d then d:=d;',
  5231. ' if s=s then s:=s;',
  5232. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5233. ' if op.Index=op.Index then op.Index:=op.Index;',
  5234. 'end;',
  5235. 'begin']);
  5236. ConvertProgram;
  5237. CheckSource('TestProc_LocalVarAbsolute',
  5238. LinesToStr([ // statements
  5239. 'rtl.createClass(this, "TObject", null, function () {',
  5240. ' this.$init = function () {',
  5241. ' this.Index = 0;',
  5242. ' };',
  5243. ' this.$final = function () {',
  5244. ' };',
  5245. ' this.DoAbs = function (Item) {',
  5246. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5247. ' };',
  5248. '});',
  5249. 'this.DoIt = function (i, p) {',
  5250. ' if (i === i) i = i;',
  5251. ' if (i === i) i = i;',
  5252. ' if (i.Index < i.Index) i.Index = i.Index;',
  5253. ' if (p.Index === p.Index) p.Index = p.Index;',
  5254. '};'
  5255. ]),
  5256. LinesToStr([
  5257. ]));
  5258. end;
  5259. procedure TTestModule.TestProc_ResultAbsolute;
  5260. begin
  5261. StartProgram(false);
  5262. Add([
  5263. 'type',
  5264. ' TObject = class',
  5265. ' Index: longint;',
  5266. ' function DoAbs: pointer;',
  5267. ' end;',
  5268. 'function TObject.DoAbs: pointer;',
  5269. 'var',
  5270. ' o: TObject absolute Result;',
  5271. 'begin',
  5272. ' if o.Index<o.Index then o.Index:=o.Index;',
  5273. 'end;',
  5274. 'function DoIt: jsvalue;',
  5275. 'var',
  5276. ' d: double absolute Result;',
  5277. ' s: string absolute Result;',
  5278. ' o: TObject absolute Result;',
  5279. 'begin',
  5280. ' if d=d then d:=d;',
  5281. ' if s=s then s:=s;',
  5282. ' if o.Index<o.Index then o.Index:=o.Index;',
  5283. 'end;',
  5284. 'begin']);
  5285. ConvertProgram;
  5286. CheckSource('TestProc_ResultAbsolute',
  5287. LinesToStr([ // statements
  5288. 'rtl.createClass(this, "TObject", null, function () {',
  5289. ' this.$init = function () {',
  5290. ' this.Index = 0;',
  5291. ' };',
  5292. ' this.$final = function () {',
  5293. ' };',
  5294. ' this.DoAbs = function () {',
  5295. ' var Result = null;',
  5296. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5297. ' return Result;',
  5298. ' };',
  5299. '});',
  5300. 'this.DoIt = function () {',
  5301. ' var Result = undefined;',
  5302. ' if (Result === Result) Result = Result;',
  5303. ' if (Result === Result) Result = Result;',
  5304. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5305. ' return Result;',
  5306. '};',
  5307. '']),
  5308. LinesToStr([
  5309. ]));
  5310. end;
  5311. procedure TTestModule.TestProc_LocalVarInit;
  5312. begin
  5313. StartProgram(false);
  5314. Add([
  5315. 'type TBytes = array of byte;',
  5316. 'procedure DoIt;',
  5317. 'const c = 4;',
  5318. 'var',
  5319. ' b: byte = 1;',
  5320. ' w: word = 2+c;',
  5321. ' p: pointer = nil;',
  5322. ' Buffer: TBytes = nil;',
  5323. 'begin',
  5324. 'end;',
  5325. 'begin']);
  5326. ConvertProgram;
  5327. CheckSource('TestProc_LocalVarInit',
  5328. LinesToStr([ // statements
  5329. 'var c = 4;',
  5330. 'this.DoIt = function () {',
  5331. ' var b = 1;',
  5332. ' var w = 2 + 4;',
  5333. ' var p = null;',
  5334. ' var Buffer = [];',
  5335. '};',
  5336. '']),
  5337. LinesToStr([
  5338. ]));
  5339. end;
  5340. procedure TTestModule.TestProc_ReservedWords;
  5341. begin
  5342. StartProgram(false);
  5343. Add([
  5344. 'procedure Date(ArrayBuffer: longint);',
  5345. 'const',
  5346. ' NaN: longint = 3;',
  5347. 'var',
  5348. ' &Boolean: longint;',
  5349. ' procedure Error(ArrayBuffer: longint);',
  5350. ' begin',
  5351. ' end;',
  5352. 'begin',
  5353. ' Nan:=&bOolean;',
  5354. 'end;',
  5355. 'begin',
  5356. ' Date(1);']);
  5357. ConvertProgram;
  5358. CheckSource('TestProc_ReservedWords',
  5359. LinesToStr([ // statements
  5360. 'var naN = 3;',
  5361. 'this.Date = function (arrayBuffer) {',
  5362. ' var boolean = 0;',
  5363. ' function error(arrayBuffer) {',
  5364. ' };',
  5365. ' naN = boolean;',
  5366. '};',
  5367. '']),
  5368. LinesToStr([
  5369. ' $mod.Date(1);'
  5370. ]));
  5371. end;
  5372. procedure TTestModule.TestProc_ConstRefWord;
  5373. begin
  5374. StartProgram(false);
  5375. Add([
  5376. 'procedure Run(constref w: word);',
  5377. 'var l: word;',
  5378. 'begin',
  5379. ' l:=w;',
  5380. ' Run(w);',
  5381. ' Run(l);',
  5382. 'end;',
  5383. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5384. 'begin',
  5385. ' Run(a);',
  5386. ' Run(b);',
  5387. ' Run(c);',
  5388. ' Run(d);',
  5389. ' Run(e);',
  5390. 'end;',
  5391. 'begin',
  5392. ' Run(1);']);
  5393. ConvertProgram;
  5394. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5395. CheckSource('TestProc_ConstRefWord',
  5396. LinesToStr([ // statements
  5397. 'this.Run = function (w) {',
  5398. ' var l = 0;',
  5399. ' l = w;',
  5400. ' $mod.Run(w);',
  5401. ' $mod.Run(l);',
  5402. '};',
  5403. 'this.Fly = function (a, b, c, d, e) {',
  5404. ' $mod.Run(a);',
  5405. ' $mod.Run(b.get());',
  5406. ' $mod.Run(c.get());',
  5407. ' $mod.Run(d);',
  5408. ' $mod.Run(e);',
  5409. '};',
  5410. '']),
  5411. LinesToStr([
  5412. '$mod.Run(1);'
  5413. ]));
  5414. end;
  5415. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5416. begin
  5417. StartProgram(false);
  5418. Add([
  5419. '{$mode objfpc}',
  5420. 'type',
  5421. ' TFunc = reference to function(x: word): word;',
  5422. 'var Func: TFunc;',
  5423. 'procedure DoIt(a: word);',
  5424. 'begin',
  5425. ' Func:=function(b:word): word',
  5426. ' begin',
  5427. ' Result:=a+b;',
  5428. ' exit(b);',
  5429. ' exit(Result);',
  5430. ' end;',// test semicolon
  5431. ' a:=3;',
  5432. 'end;',
  5433. 'begin',
  5434. ' Func:=function(c:word):word begin',
  5435. ' Result:=3+c;',
  5436. ' exit(c);',
  5437. ' exit(Result);',
  5438. ' end;']);
  5439. ConvertProgram;
  5440. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5441. LinesToStr([ // statements
  5442. 'this.Func = null;',
  5443. 'this.DoIt = function (a) {',
  5444. ' $mod.Func = function (b) {',
  5445. ' var Result = 0;',
  5446. ' Result = a + b;',
  5447. ' return b;',
  5448. ' return Result;',
  5449. ' return Result;',
  5450. ' };',
  5451. ' a = 3;',
  5452. '};',
  5453. '']),
  5454. LinesToStr([
  5455. '$mod.Func = function (c) {',
  5456. ' var Result = 0;',
  5457. ' Result = 3 + c;',
  5458. ' return c;',
  5459. ' return Result;',
  5460. ' return Result;',
  5461. '};',
  5462. '']));
  5463. end;
  5464. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5465. begin
  5466. StartProgram(false);
  5467. Add([
  5468. '{$mode delphi}',
  5469. 'type',
  5470. ' TProc = reference to procedure(x: word);',
  5471. 'procedure DoIt(a: word);',
  5472. 'var Proc: TProc;',
  5473. 'begin',
  5474. ' Proc:=procedure(b:word) begin end;',
  5475. 'end;',
  5476. 'var Proc: TProc;',
  5477. 'begin',
  5478. ' Proc:=procedure(c:word) begin end;',
  5479. '']);
  5480. ConvertProgram;
  5481. CheckSource('TestAnonymousProc_Assign_Delphi',
  5482. LinesToStr([ // statements
  5483. 'this.DoIt = function (a) {',
  5484. ' var Proc = null;',
  5485. ' Proc = function (b) {',
  5486. ' };',
  5487. '};',
  5488. 'this.Proc = null;',
  5489. '']),
  5490. LinesToStr([
  5491. '$mod.Proc = function (c) {',
  5492. '};',
  5493. '']));
  5494. end;
  5495. procedure TTestModule.TestAnonymousProc_Arg;
  5496. begin
  5497. StartProgram(false);
  5498. Add([
  5499. 'type',
  5500. ' TProc = reference to procedure;',
  5501. ' TFunc = reference to function(x: word): word;',
  5502. 'procedure DoMore(f,g: TProc);',
  5503. 'begin',
  5504. 'end;',
  5505. 'procedure DoOdd(v: jsvalue);',
  5506. 'begin',
  5507. 'end;',
  5508. 'procedure DoIt(f: TFunc);',
  5509. 'begin',
  5510. ' DoIt(function(b:word): word',
  5511. ' begin',
  5512. ' Result:=1+b;',
  5513. ' end);',
  5514. ' DoMore(procedure begin end, procedure begin end);',
  5515. ' DoOdd(procedure begin end);',
  5516. 'end;',
  5517. 'begin',
  5518. ' DoMore(procedure begin end,',
  5519. ' procedure assembler asm',
  5520. ' console.log("c");',
  5521. ' end);',
  5522. '']);
  5523. ConvertProgram;
  5524. CheckSource('TestAnonymousProc_Arg',
  5525. LinesToStr([ // statements
  5526. 'this.DoMore = function (f, g) {',
  5527. '};',
  5528. 'this.DoOdd = function (v) {',
  5529. '};',
  5530. 'this.DoIt = function (f) {',
  5531. ' $mod.DoIt(function (b) {',
  5532. ' var Result = 0;',
  5533. ' Result = 1 + b;',
  5534. ' return Result;',
  5535. ' });',
  5536. ' $mod.DoMore(function () {',
  5537. ' }, function () {',
  5538. ' });',
  5539. ' $mod.DoOdd(function () {',
  5540. ' });',
  5541. '};',
  5542. '']),
  5543. LinesToStr([
  5544. '$mod.DoMore(function () {',
  5545. '}, function () {',
  5546. ' console.log("c");',
  5547. '});',
  5548. '']));
  5549. end;
  5550. procedure TTestModule.TestAnonymousProc_Typecast;
  5551. begin
  5552. StartProgram(false);
  5553. Add([
  5554. 'type',
  5555. ' TProc = reference to procedure(w: word);',
  5556. ' TArr = array of word;',
  5557. ' TFuncArr = reference to function: TArr;',
  5558. 'procedure DoIt(p: TProc);',
  5559. 'var',
  5560. ' w: word;',
  5561. ' a: TArr;',
  5562. 'begin',
  5563. ' p:=TProc(procedure(b: smallint) begin end);',
  5564. ' a:=TFuncArr(function: TArr begin end)();',
  5565. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5566. 'end;',
  5567. 'begin']);
  5568. ConvertProgram;
  5569. CheckSource('TestAnonymousProc_Typecast',
  5570. LinesToStr([ // statements
  5571. 'this.DoIt = function (p) {',
  5572. ' var w = 0;',
  5573. ' var a = [];',
  5574. ' p = function (b) {',
  5575. ' };',
  5576. ' a = function () {',
  5577. ' var Result = [];',
  5578. ' return Result;',
  5579. ' }();',
  5580. ' w = function () {',
  5581. ' var Result = [];',
  5582. ' return Result;',
  5583. ' }()[3];',
  5584. '};',
  5585. '']),
  5586. LinesToStr([
  5587. '']));
  5588. end;
  5589. procedure TTestModule.TestAnonymousProc_With;
  5590. begin
  5591. StartProgram(false);
  5592. Add([
  5593. 'type',
  5594. ' TProc = reference to procedure(w: word);',
  5595. ' TObject = class',
  5596. ' b: boolean;',
  5597. ' end;',
  5598. 'var',
  5599. ' p: TProc;',
  5600. ' bird: TObject;',
  5601. 'begin',
  5602. ' with bird do',
  5603. ' p:=procedure(w: word)',
  5604. ' begin',
  5605. ' b:=w>2;',
  5606. ' end;',
  5607. '']);
  5608. ConvertProgram;
  5609. CheckSource('TestAnonymousProc_With',
  5610. LinesToStr([ // statements
  5611. 'rtl.createClass(this, "TObject", null, function () {',
  5612. ' this.$init = function () {',
  5613. ' this.b = false;',
  5614. ' };',
  5615. ' this.$final = function () {',
  5616. ' };',
  5617. '});',
  5618. 'this.p = null;',
  5619. 'this.bird = null;',
  5620. '']),
  5621. LinesToStr([
  5622. 'var $with = $mod.bird;',
  5623. '$mod.p = function (w) {',
  5624. ' $with.b = w > 2;',
  5625. '};',
  5626. '']));
  5627. end;
  5628. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5629. begin
  5630. StartProgram(false);
  5631. Add([
  5632. 'type',
  5633. ' TProc = reference to procedure;',
  5634. ' TObject = class',
  5635. ' b: boolean;',
  5636. ' end;',
  5637. 'procedure DoIt;',
  5638. 'var',
  5639. ' p: TProc;',
  5640. 'begin',
  5641. ' try',
  5642. ' except',
  5643. ' on E: TObject do',
  5644. ' p:=procedure',
  5645. ' begin',
  5646. ' E.b:=true;',
  5647. ' end;',
  5648. ' end;',
  5649. 'end;',
  5650. 'begin']);
  5651. ConvertProgram;
  5652. CheckSource('TestAnonymousProc_ExceptOn',
  5653. LinesToStr([ // statements
  5654. 'rtl.createClass(this, "TObject", null, function () {',
  5655. ' this.$init = function () {',
  5656. ' this.b = false;',
  5657. ' };',
  5658. ' this.$final = function () {',
  5659. ' };',
  5660. '});',
  5661. 'this.DoIt = function () {',
  5662. ' var p = null;',
  5663. ' try {} catch ($e) {',
  5664. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5665. ' var E = $e;',
  5666. ' p = function () {',
  5667. ' E.b = true;',
  5668. ' };',
  5669. ' } else throw $e',
  5670. ' };',
  5671. '};',
  5672. '']),
  5673. LinesToStr([
  5674. '']));
  5675. end;
  5676. procedure TTestModule.TestAnonymousProc_Nested;
  5677. begin
  5678. StartProgram(false);
  5679. Add([
  5680. 'type',
  5681. ' TProc = reference to procedure;',
  5682. ' TObject = class',
  5683. ' i: byte;',
  5684. ' procedure DoIt;',
  5685. ' end;',
  5686. 'procedure TObject.DoIt;',
  5687. 'var',
  5688. ' p: TProc;',
  5689. ' procedure Sub;',
  5690. ' begin',
  5691. ' p:=procedure',
  5692. ' begin',
  5693. ' i:=3;',
  5694. ' Self.i:=4;',
  5695. ' p:=procedure',
  5696. ' procedure SubSub;',
  5697. ' begin',
  5698. ' i:=13;',
  5699. ' Self.i:=14;',
  5700. ' end;',
  5701. ' begin',
  5702. ' i:=13;',
  5703. ' Self.i:=14;',
  5704. ' end;',
  5705. ' end;',
  5706. ' end;',
  5707. 'begin',
  5708. 'end;',
  5709. 'begin']);
  5710. ConvertProgram;
  5711. CheckSource('TestAnonymousProc_Nested',
  5712. LinesToStr([ // statements
  5713. 'rtl.createClass(this, "TObject", null, function () {',
  5714. ' this.$init = function () {',
  5715. ' this.i = 0;',
  5716. ' };',
  5717. ' this.$final = function () {',
  5718. ' };',
  5719. ' this.DoIt = function () {',
  5720. ' var $Self = this;',
  5721. ' var p = null;',
  5722. ' function Sub() {',
  5723. ' p = function () {',
  5724. ' $Self.i = 3;',
  5725. ' $Self.i = 4;',
  5726. ' p = function () {',
  5727. ' function SubSub() {',
  5728. ' $Self.i = 13;',
  5729. ' $Self.i = 14;',
  5730. ' };',
  5731. ' $Self.i = 13;',
  5732. ' $Self.i = 14;',
  5733. ' };',
  5734. ' };',
  5735. ' };',
  5736. ' };',
  5737. '});',
  5738. '']),
  5739. LinesToStr([
  5740. '']));
  5741. end;
  5742. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5743. begin
  5744. StartProgram(false);
  5745. Add([
  5746. 'type',
  5747. ' TProc = reference to procedure;',
  5748. 'function DoIt: TProc;',
  5749. ' function Sub: TProc;',
  5750. ' begin',
  5751. ' Result:=procedure',
  5752. ' begin',
  5753. ' Sub:=procedure',
  5754. ' procedure SubSub;',
  5755. ' begin',
  5756. ' Result:=nil;',
  5757. ' Sub:=nil;',
  5758. ' DoIt:=nil;',
  5759. ' end;',
  5760. ' begin',
  5761. ' Result:=nil;',
  5762. ' Sub:=nil;',
  5763. ' DoIt:=nil;',
  5764. ' end;',
  5765. ' end;',
  5766. ' end;',
  5767. 'begin',
  5768. 'end;',
  5769. 'begin']);
  5770. ConvertProgram;
  5771. CheckSource('TestAnonymousProc_NestedAssignResult',
  5772. LinesToStr([ // statements
  5773. 'this.DoIt = function () {',
  5774. ' var Result = null;',
  5775. ' function Sub() {',
  5776. ' var Result$1 = null;',
  5777. ' Result$1 = function () {',
  5778. ' Result$1 = function () {',
  5779. ' function SubSub() {',
  5780. ' Result$1 = null;',
  5781. ' Result$1 = null;',
  5782. ' Result = null;',
  5783. ' };',
  5784. ' Result$1 = null;',
  5785. ' Result$1 = null;',
  5786. ' Result = null;',
  5787. ' };',
  5788. ' };',
  5789. ' return Result$1;',
  5790. ' };',
  5791. ' return Result;',
  5792. '};',
  5793. '']),
  5794. LinesToStr([
  5795. '']));
  5796. end;
  5797. procedure TTestModule.TestAnonymousProc_Class;
  5798. begin
  5799. StartProgram(false);
  5800. Add([
  5801. 'type',
  5802. ' TProc = reference to procedure;',
  5803. ' TEvent = procedure of object;',
  5804. ' TObject = class',
  5805. ' Size: word;',
  5806. ' function GetIt: TProc;',
  5807. ' procedure DoIt; virtual; abstract;',
  5808. ' end;',
  5809. 'function TObject.GetIt: TProc;',
  5810. 'begin',
  5811. ' Result:=procedure',
  5812. ' var p: TEvent;',
  5813. ' begin',
  5814. ' Size:=Size;',
  5815. ' Size:=Self.Size;',
  5816. ' p:=@DoIt;',
  5817. ' p:[email protected];',
  5818. ' end;',
  5819. 'end;',
  5820. 'begin']);
  5821. ConvertProgram;
  5822. CheckSource('TestAnonymousProc_Class',
  5823. LinesToStr([ // statements
  5824. 'rtl.createClass(this, "TObject", null, function () {',
  5825. ' this.$init = function () {',
  5826. ' this.Size = 0;',
  5827. ' };',
  5828. ' this.$final = function () {',
  5829. ' };',
  5830. ' this.GetIt = function () {',
  5831. ' var $Self = this;',
  5832. ' var Result = null;',
  5833. ' Result = function () {',
  5834. ' var p = null;',
  5835. ' $Self.Size = $Self.Size;',
  5836. ' $Self.Size = $Self.Size;',
  5837. ' p = rtl.createCallback($Self, "DoIt");',
  5838. ' p = rtl.createCallback($Self, "DoIt");',
  5839. ' };',
  5840. ' return Result;',
  5841. ' };',
  5842. '});',
  5843. '']),
  5844. LinesToStr([
  5845. '']));
  5846. end;
  5847. procedure TTestModule.TestAnonymousProc_ForLoop;
  5848. begin
  5849. StartProgram(false);
  5850. Add([
  5851. 'type TProc = reference to procedure;',
  5852. 'procedure Foo(p: TProc);',
  5853. 'begin',
  5854. 'end;',
  5855. 'procedure DoIt;',
  5856. 'var i: word;',
  5857. ' a: word;',
  5858. 'begin',
  5859. ' for i:=1 to 10 do begin',
  5860. ' Foo(procedure begin a:=3; end);',
  5861. ' end;',
  5862. 'end;',
  5863. 'begin',
  5864. ' DoIt;']);
  5865. ConvertProgram;
  5866. CheckSource('TestAnonymousProc_ForLoop',
  5867. LinesToStr([ // statements
  5868. 'this.Foo = function (p) {',
  5869. '};',
  5870. 'this.DoIt = function () {',
  5871. ' var i = 0;',
  5872. ' var a = 0;',
  5873. ' for (i = 1; i <= 10; i++) {',
  5874. ' $mod.Foo(function () {',
  5875. ' a = 3;',
  5876. ' });',
  5877. ' };',
  5878. '};',
  5879. '']),
  5880. LinesToStr([
  5881. '$mod.DoIt();'
  5882. ]));
  5883. end;
  5884. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5885. begin
  5886. StartProgram(false);
  5887. Add([
  5888. '{$mode delphi}',
  5889. 'type',
  5890. ' TProc = reference to procedure;',
  5891. ' TFunc = reference to function(x: word): word;',
  5892. 'procedure Run;',
  5893. 'asm',
  5894. 'end;',
  5895. 'procedure Walk(p: TProc; f: TFunc);',
  5896. 'begin',
  5897. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5898. 'end;',
  5899. 'begin',
  5900. ' Walk(procedure',
  5901. ' asm',
  5902. ' console.log("a");',
  5903. ' end,',
  5904. ' function(x: word): word asm',
  5905. ' console.log("c");',
  5906. ' end);',
  5907. '']);
  5908. ConvertProgram;
  5909. CheckSource('TestAnonymousProc_AsmDelphi',
  5910. LinesToStr([ // statements
  5911. 'this.Run = function () {',
  5912. '};',
  5913. 'this.Walk = function (p, f) {',
  5914. ' $mod.Walk(function () {',
  5915. ' }, function (b) {',
  5916. ' return 1+b;',
  5917. ' });',
  5918. '};',
  5919. '']),
  5920. LinesToStr([
  5921. '$mod.Walk(function () {',
  5922. ' console.log("a");',
  5923. '}, function (x) {',
  5924. ' console.log("c");',
  5925. '});',
  5926. '']));
  5927. end;
  5928. procedure TTestModule.TestEnum_Name;
  5929. begin
  5930. StartProgram(false);
  5931. Add('type TMyEnum = (Red, Green, Blue);');
  5932. Add('var e: TMyEnum;');
  5933. Add('var f: TMyEnum = Blue;');
  5934. Add('begin');
  5935. Add(' e:=green;');
  5936. Add(' e:=default(TMyEnum);');
  5937. ConvertProgram;
  5938. CheckSource('TestEnum_Name',
  5939. LinesToStr([ // statements
  5940. 'this.TMyEnum = {',
  5941. ' "0":"Red",',
  5942. ' Red:0,',
  5943. ' "1":"Green",',
  5944. ' Green:1,',
  5945. ' "2":"Blue",',
  5946. ' Blue:2',
  5947. ' };',
  5948. 'this.e = 0;',
  5949. 'this.f = this.TMyEnum.Blue;'
  5950. ]),
  5951. LinesToStr([
  5952. '$mod.e=$mod.TMyEnum.Green;',
  5953. '$mod.e=$mod.TMyEnum.Red;'
  5954. ]));
  5955. end;
  5956. procedure TTestModule.TestEnum_Number;
  5957. begin
  5958. Converter.Options:=Converter.Options+[coEnumNumbers];
  5959. StartProgram(false);
  5960. Add('type TMyEnum = (Red, Green);');
  5961. Add('var');
  5962. Add(' e: TMyEnum;');
  5963. Add(' f: TMyEnum = Green;');
  5964. Add(' i: longint;');
  5965. Add('begin');
  5966. Add(' e:=green;');
  5967. Add(' i:=longint(e);');
  5968. ConvertProgram;
  5969. CheckSource('TestEnumNumber',
  5970. LinesToStr([ // statements
  5971. 'this.TMyEnum = {',
  5972. ' "0":"Red",',
  5973. ' Red:0,',
  5974. ' "1":"Green",',
  5975. ' Green:1',
  5976. ' };',
  5977. 'this.e = 0;',
  5978. 'this.f = 1;',
  5979. 'this.i = 0;'
  5980. ]),
  5981. LinesToStr([
  5982. '$mod.e=1;',
  5983. '$mod.i=$mod.e;'
  5984. ]));
  5985. end;
  5986. procedure TTestModule.TestEnum_ConstFail;
  5987. begin
  5988. StartProgram(false);
  5989. Add([
  5990. 'type TMyEnum = (Red = 100, Green = 101);',
  5991. 'var',
  5992. ' e: TMyEnum;',
  5993. ' f: TMyEnum = Green;',
  5994. 'begin',
  5995. ' e:=green;']);
  5996. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5997. ConvertProgram;
  5998. end;
  5999. procedure TTestModule.TestEnum_Functions;
  6000. begin
  6001. StartProgram(false);
  6002. Add([
  6003. 'type TMyEnum = (Red, Green);',
  6004. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6005. 'var',
  6006. ' v: longint;',
  6007. ' s: string;',
  6008. 'begin',
  6009. ' val(s,e,v);',
  6010. ' val(s,e,i);',
  6011. 'end;',
  6012. 'var',
  6013. ' e: TMyEnum;',
  6014. ' i: longint;',
  6015. ' s: string;',
  6016. ' b: boolean;',
  6017. 'begin',
  6018. ' i:=ord(red);',
  6019. ' i:=ord(green);',
  6020. ' i:=ord(e);',
  6021. ' i:=ord(b);',
  6022. ' e:=low(tmyenum);',
  6023. ' e:=low(e);',
  6024. ' b:=low(boolean);',
  6025. ' e:=high(tmyenum);',
  6026. ' e:=high(e);',
  6027. ' b:=high(boolean);',
  6028. ' e:=pred(green);',
  6029. ' e:=pred(e);',
  6030. ' b:=pred(b);',
  6031. ' e:=succ(red);',
  6032. ' e:=succ(e);',
  6033. ' b:=succ(b);',
  6034. ' e:=tmyenum(1);',
  6035. ' e:=tmyenum(i);',
  6036. ' s:=str(e);',
  6037. ' str(e,s);',
  6038. ' str(red,s);',
  6039. ' s:=str(e:3);',
  6040. ' writestr(s,e:3,red);',
  6041. ' val(s,e,i);',
  6042. ' i:=longint(e);']);
  6043. ConvertProgram;
  6044. CheckSource('TestEnum_Functions',
  6045. LinesToStr([ // statements
  6046. 'this.TMyEnum = {',
  6047. ' "0":"Red",',
  6048. ' Red:0,',
  6049. ' "1":"Green",',
  6050. ' Green:1',
  6051. ' };',
  6052. 'this.DoIt = function (e, i) {',
  6053. ' var v = 0;',
  6054. ' var s = "";',
  6055. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6056. ' v = w;',
  6057. ' }));',
  6058. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6059. '};',
  6060. 'this.e = 0;',
  6061. 'this.i = 0;',
  6062. 'this.s = "";',
  6063. 'this.b = false;',
  6064. '']),
  6065. LinesToStr([
  6066. '$mod.i=$mod.TMyEnum.Red;',
  6067. '$mod.i=$mod.TMyEnum.Green;',
  6068. '$mod.i=$mod.e;',
  6069. '$mod.i=$mod.b+0;',
  6070. '$mod.e=$mod.TMyEnum.Red;',
  6071. '$mod.e=$mod.TMyEnum.Red;',
  6072. '$mod.b=false;',
  6073. '$mod.e=$mod.TMyEnum.Green;',
  6074. '$mod.e=$mod.TMyEnum.Green;',
  6075. '$mod.b=true;',
  6076. '$mod.e=$mod.TMyEnum.Green-1;',
  6077. '$mod.e=$mod.e-1;',
  6078. '$mod.b=false;',
  6079. '$mod.e=$mod.TMyEnum.Red+1;',
  6080. '$mod.e=$mod.e+1;',
  6081. '$mod.b=true;',
  6082. '$mod.e=1;',
  6083. '$mod.e=$mod.i;',
  6084. '$mod.s = $mod.TMyEnum[$mod.e];',
  6085. '$mod.s = $mod.TMyEnum[$mod.e];',
  6086. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6087. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6088. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6089. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6090. ' $mod.i = v;',
  6091. '});',
  6092. '$mod.i=$mod.e;',
  6093. '']));
  6094. end;
  6095. procedure TTestModule.TestEnumRg_Functions;
  6096. begin
  6097. StartProgram(false);
  6098. Add([
  6099. 'type',
  6100. ' TEnum = (Red, Green, Blue);',
  6101. ' TEnumRg = Green..Blue;',
  6102. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6103. 'var',
  6104. ' v: longint;',
  6105. ' s: string;',
  6106. 'begin',
  6107. ' val(s,e,v);',
  6108. ' val(s,e,i);',
  6109. 'end;',
  6110. 'var',
  6111. ' e: TEnumRg;',
  6112. ' i: longint;',
  6113. ' s: string;',
  6114. 'begin',
  6115. ' i:=ord(green);',
  6116. ' i:=ord(e);',
  6117. ' e:=low(tenumrg);',
  6118. ' e:=low(e);',
  6119. ' e:=high(tenumrg);',
  6120. ' e:=high(e);',
  6121. ' e:=pred(blue);',
  6122. ' e:=pred(e);',
  6123. ' e:=succ(green);',
  6124. ' e:=succ(e);',
  6125. ' e:=tenumrg(1);',
  6126. ' e:=tenumrg(i);',
  6127. ' s:=str(e);',
  6128. ' str(e,s);',
  6129. ' str(red,s);',
  6130. ' s:=str(e:3);',
  6131. ' writestr(s,e:3,blue);',
  6132. ' val(s,e,i);',
  6133. ' i:=longint(e);']);
  6134. ConvertProgram;
  6135. CheckSource('TestEnumRg_Functions',
  6136. LinesToStr([ // statements
  6137. 'this.TEnum = {',
  6138. ' "0":"Red",',
  6139. ' Red:0,',
  6140. ' "1":"Green",',
  6141. ' Green:1,',
  6142. ' "2":"Blue",',
  6143. ' Blue:2',
  6144. ' };',
  6145. 'this.DoIt = function (e, i) {',
  6146. ' var v = 0;',
  6147. ' var s = "";',
  6148. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6149. ' v = w;',
  6150. ' }));',
  6151. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6152. '};',
  6153. 'this.e = this.TEnum.Green;',
  6154. 'this.i = 0;',
  6155. 'this.s = "";',
  6156. '']),
  6157. LinesToStr([
  6158. '$mod.i=$mod.TEnum.Green;',
  6159. '$mod.i=$mod.e;',
  6160. '$mod.e=$mod.TEnum.Green;',
  6161. '$mod.e=$mod.TEnum.Green;',
  6162. '$mod.e=$mod.TEnum.Blue;',
  6163. '$mod.e=$mod.TEnum.Blue;',
  6164. '$mod.e=$mod.TEnum.Blue-1;',
  6165. '$mod.e=$mod.e-1;',
  6166. '$mod.e=$mod.TEnum.Green+1;',
  6167. '$mod.e=$mod.e+1;',
  6168. '$mod.e=1;',
  6169. '$mod.e=$mod.i;',
  6170. '$mod.s = $mod.TEnum[$mod.e];',
  6171. '$mod.s = $mod.TEnum[$mod.e];',
  6172. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6173. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6174. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6175. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6176. ' $mod.i = v;',
  6177. '});',
  6178. '$mod.i=$mod.e;',
  6179. '']));
  6180. end;
  6181. procedure TTestModule.TestEnum_AsParams;
  6182. begin
  6183. StartProgram(false);
  6184. Add('type TEnum = (Red,Blue);');
  6185. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6186. Add('var vJ: TEnum;');
  6187. Add('begin');
  6188. Add(' vg:=vg;');
  6189. Add(' vj:=vh;');
  6190. Add(' vi:=vi;');
  6191. Add(' doit(vg,vg,vg);');
  6192. Add(' doit(vh,vh,vj);');
  6193. Add(' doit(vi,vi,vi);');
  6194. Add(' doit(vj,vj,vj);');
  6195. Add('end;');
  6196. Add('var i: TEnum;');
  6197. Add('begin');
  6198. Add(' doit(i,i,i);');
  6199. ConvertProgram;
  6200. CheckSource('TestEnum_AsParams',
  6201. LinesToStr([ // statements
  6202. 'this.TEnum = {',
  6203. ' "0": "Red",',
  6204. ' Red: 0,',
  6205. ' "1": "Blue",',
  6206. ' Blue: 1',
  6207. '};',
  6208. 'this.DoIt = function (vG,vH,vI) {',
  6209. ' var vJ = 0;',
  6210. ' vG = vG;',
  6211. ' vJ = vH;',
  6212. ' vI.set(vI.get());',
  6213. ' $mod.DoIt(vG, vG, {',
  6214. ' get: function () {',
  6215. ' return vG;',
  6216. ' },',
  6217. ' set: function (v) {',
  6218. ' vG = v;',
  6219. ' }',
  6220. ' });',
  6221. ' $mod.DoIt(vH, vH, {',
  6222. ' get: function () {',
  6223. ' return vJ;',
  6224. ' },',
  6225. ' set: function (v) {',
  6226. ' vJ = v;',
  6227. ' }',
  6228. ' });',
  6229. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6230. ' $mod.DoIt(vJ, vJ, {',
  6231. ' get: function () {',
  6232. ' return vJ;',
  6233. ' },',
  6234. ' set: function (v) {',
  6235. ' vJ = v;',
  6236. ' }',
  6237. ' });',
  6238. '};',
  6239. 'this.i = 0;'
  6240. ]),
  6241. LinesToStr([
  6242. '$mod.DoIt($mod.i,$mod.i,{',
  6243. ' p: $mod,',
  6244. ' get: function () {',
  6245. ' return this.p.i;',
  6246. ' },',
  6247. ' set: function (v) {',
  6248. ' this.p.i = v;',
  6249. ' }',
  6250. '});'
  6251. ]));
  6252. end;
  6253. procedure TTestModule.TestEnumRange_Array;
  6254. begin
  6255. StartProgram(false);
  6256. Add([
  6257. 'type',
  6258. ' TEnum = (Red, Green, Blue);',
  6259. ' TEnumRg = green..blue;',
  6260. ' TArr = array[TEnumRg] of byte;',
  6261. ' TArr2 = array[green..blue] of byte;',
  6262. 'var',
  6263. ' a: TArr;',
  6264. ' b: TArr = (3,4);',
  6265. ' c: TArr2 = (5,6);',
  6266. 'begin',
  6267. ' a[green] := b[blue];',
  6268. ' c[green] := c[blue];',
  6269. '']);
  6270. ConvertProgram;
  6271. CheckSource('TestEnumRange_Array',
  6272. LinesToStr([ // statements
  6273. 'this.TEnum = {',
  6274. ' "0": "Red",',
  6275. ' Red: 0,',
  6276. ' "1": "Green",',
  6277. ' Green: 1,',
  6278. ' "2": "Blue",',
  6279. ' Blue: 2',
  6280. '};',
  6281. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6282. 'this.b = [3, 4];',
  6283. 'this.c = [5, 6];',
  6284. '']),
  6285. LinesToStr([
  6286. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6287. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6288. '']));
  6289. end;
  6290. procedure TTestModule.TestEnum_ForIn;
  6291. begin
  6292. StartProgram(false);
  6293. Add([
  6294. 'type',
  6295. ' TEnum = (Red, Green, Blue);',
  6296. ' TEnumRg = green..blue;',
  6297. ' TArr = array[TEnum] of byte;',
  6298. ' TArrRg = array[TEnumRg] of byte;',
  6299. 'var',
  6300. ' e: TEnum;',
  6301. ' a1: TArr = (3,4,5);',
  6302. ' a2: TArrRg = (11,12);',
  6303. ' b: byte;',
  6304. 'begin',
  6305. ' for e in TEnum do ;',
  6306. ' for e in TEnumRg do ;',
  6307. ' for e in TArr do ;',
  6308. ' for e in TArrRg do ;',
  6309. ' for b in a1 do ;',
  6310. ' for b in a2 do ;',
  6311. '']);
  6312. ConvertProgram;
  6313. CheckSource('TestEnum_ForIn',
  6314. LinesToStr([ // statements
  6315. 'this.TEnum = {',
  6316. ' "0": "Red",',
  6317. ' Red: 0,',
  6318. ' "1": "Green",',
  6319. ' Green: 1,',
  6320. ' "2": "Blue",',
  6321. ' Blue: 2',
  6322. '};',
  6323. 'this.e = 0;',
  6324. 'this.a1 = [3, 4, 5];',
  6325. 'this.a2 = [11, 12];',
  6326. 'this.b = 0;',
  6327. '']),
  6328. LinesToStr([
  6329. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6330. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6331. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6332. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6333. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6334. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6335. '']));
  6336. end;
  6337. procedure TTestModule.TestEnum_ScopedNumber;
  6338. begin
  6339. Converter.Options:=Converter.Options+[coEnumNumbers];
  6340. StartProgram(false);
  6341. Add([
  6342. 'type',
  6343. ' TEnum = (Red, Green);',
  6344. 'var',
  6345. ' e: TEnum;',
  6346. 'begin',
  6347. ' e:=TEnum.Green;',
  6348. '']);
  6349. ConvertProgram;
  6350. CheckSource('TestEnum_ScopedNumber',
  6351. LinesToStr([ // statements
  6352. 'this.TEnum = {',
  6353. ' "0": "Red",',
  6354. ' Red: 0,',
  6355. ' "1": "Green",',
  6356. ' Green: 1',
  6357. '};',
  6358. 'this.e = 0;',
  6359. '']),
  6360. LinesToStr([
  6361. '$mod.e = 1;']));
  6362. end;
  6363. procedure TTestModule.TestEnum_InFunction;
  6364. begin
  6365. StartProgram(false);
  6366. Add([
  6367. 'const TEnum = 3;',
  6368. 'procedure DoIt;',
  6369. 'type',
  6370. ' TEnum = (Red, Green, Blue);',
  6371. ' procedure Sub;',
  6372. ' type',
  6373. ' TEnumSub = (Left, Right);',
  6374. ' var',
  6375. ' es: TEnumSub;',
  6376. ' begin',
  6377. ' es:=Left;',
  6378. ' end;',
  6379. 'var',
  6380. ' e, e2: TEnum;',
  6381. 'begin',
  6382. ' if e in [red,blue] then e2:=e;',
  6383. 'end;',
  6384. 'begin']);
  6385. ConvertProgram;
  6386. CheckSource('TestEnum_InFunction',
  6387. LinesToStr([ // statements
  6388. 'this.TEnum = 3;',
  6389. 'var TEnum$1 = {',
  6390. ' "0":"Red",',
  6391. ' Red:0,',
  6392. ' "1":"Green",',
  6393. ' Green:1,',
  6394. ' "2":"Blue",',
  6395. ' Blue:2',
  6396. ' };',
  6397. 'var TEnumSub = {',
  6398. ' "0": "Left",',
  6399. ' Left: 0,',
  6400. ' "1": "Right",',
  6401. ' Right: 1',
  6402. '};',
  6403. 'this.DoIt = function () {',
  6404. ' function Sub() {',
  6405. ' var es = 0;',
  6406. ' es = TEnumSub.Left;',
  6407. ' };',
  6408. ' var e = 0;',
  6409. ' var e2 = 0;',
  6410. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6411. '};',
  6412. '']),
  6413. LinesToStr([
  6414. '']));
  6415. end;
  6416. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6417. begin
  6418. StartUnit(true);
  6419. Add([
  6420. 'interface',
  6421. 'var color: (red, green);',
  6422. 'implementation',
  6423. 'initialization',
  6424. ' color:=green;',
  6425. '']);
  6426. ConvertUnit;
  6427. CheckSource('TestEnum_Name_Anonymous_Unit',
  6428. LinesToStr([
  6429. 'this.color$a = {',
  6430. ' "0": "red",',
  6431. ' red: 0,',
  6432. ' "1": "green",',
  6433. ' green: 1',
  6434. '};',
  6435. 'this.color = 0;',
  6436. '']),
  6437. LinesToStr([ // this.$init
  6438. '$mod.color = $mod.color$a.green;',
  6439. '']),
  6440. LinesToStr([ // implementation
  6441. '']) );
  6442. end;
  6443. procedure TTestModule.TestSet_Enum;
  6444. begin
  6445. StartProgram(false);
  6446. Add([
  6447. 'type',
  6448. ' TColor = (Red, Green, Blue);',
  6449. ' TColors = set of TColor;',
  6450. 'var',
  6451. ' c: TColor;',
  6452. ' s: TColors;',
  6453. ' t: TColors = [];',
  6454. ' u: TColors = [Red];',
  6455. 'begin',
  6456. ' s:=[];',
  6457. ' s:=[Green];',
  6458. ' s:=[Green,Blue];',
  6459. ' s:=[Red..Blue];',
  6460. ' s:=[Red,Green..Blue];',
  6461. ' s:=[Red,c];',
  6462. ' s:=t;',
  6463. ' s:=default(TColors);',
  6464. '']);
  6465. ConvertProgram;
  6466. CheckSource('TestSet',
  6467. LinesToStr([ // statements
  6468. 'this.TColor = {',
  6469. ' "0":"Red",',
  6470. ' Red:0,',
  6471. ' "1":"Green",',
  6472. ' Green:1,',
  6473. ' "2":"Blue",',
  6474. ' Blue:2',
  6475. ' };',
  6476. 'this.c = 0;',
  6477. 'this.s = {};',
  6478. 'this.t = {};',
  6479. 'this.u = rtl.createSet(this.TColor.Red);'
  6480. ]),
  6481. LinesToStr([
  6482. '$mod.s={};',
  6483. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6484. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6485. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6486. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6487. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6488. '$mod.s=rtl.refSet($mod.t);',
  6489. '$mod.s={};',
  6490. '']));
  6491. end;
  6492. procedure TTestModule.TestSet_Operators;
  6493. begin
  6494. StartProgram(false);
  6495. Add('type');
  6496. Add(' TColor = (Red, Green, Blue);');
  6497. Add(' TColors = set of tcolor;');
  6498. Add('var');
  6499. Add(' vC: TColor;');
  6500. Add(' vS: TColors;');
  6501. Add(' vT: TColors;');
  6502. Add(' vU: TColors;');
  6503. Add(' B: boolean;');
  6504. Add('begin');
  6505. Add(' include(vs,green);');
  6506. Add(' exclude(vs,vc);');
  6507. Add(' vs:=vt+vu;');
  6508. Add(' vs:=vt+[red];');
  6509. Add(' vs:=[red]+vt;');
  6510. Add(' vs:=[red]+[green];');
  6511. Add(' vs:=vt-vu;');
  6512. Add(' vs:=vt-[red];');
  6513. Add(' vs:=[red]-vt;');
  6514. Add(' vs:=[red]-[green];');
  6515. Add(' vs:=vt*vu;');
  6516. Add(' vs:=vt*[red];');
  6517. Add(' vs:=[red]*vt;');
  6518. Add(' vs:=[red]*[green];');
  6519. Add(' vs:=vt><vu;');
  6520. Add(' vs:=vt><[red];');
  6521. Add(' vs:=[red]><vt;');
  6522. Add(' vs:=[red]><[green];');
  6523. Add(' b:=vt=vu;');
  6524. Add(' b:=vt=[red];');
  6525. Add(' b:=[red]=vt;');
  6526. Add(' b:=[red]=[green];');
  6527. Add(' b:=vt<>vu;');
  6528. Add(' b:=vt<>[red];');
  6529. Add(' b:=[red]<>vt;');
  6530. Add(' b:=[red]<>[green];');
  6531. Add(' b:=vt<=vu;');
  6532. Add(' b:=vt<=[red];');
  6533. Add(' b:=[red]<=vt;');
  6534. Add(' b:=[red]<=[green];');
  6535. Add(' b:=vt>=vu;');
  6536. Add(' b:=vt>=[red];');
  6537. Add(' b:=[red]>=vt;');
  6538. Add(' b:=[red]>=[green];');
  6539. ConvertProgram;
  6540. CheckSource('TestSet_Operators',
  6541. LinesToStr([ // statements
  6542. 'this.TColor = {',
  6543. ' "0":"Red",',
  6544. ' Red:0,',
  6545. ' "1":"Green",',
  6546. ' Green:1,',
  6547. ' "2":"Blue",',
  6548. ' Blue:2',
  6549. ' };',
  6550. 'this.vC = 0;',
  6551. 'this.vS = {};',
  6552. 'this.vT = {};',
  6553. 'this.vU = {};',
  6554. 'this.B = false;'
  6555. ]),
  6556. LinesToStr([
  6557. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6558. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6559. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6560. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6561. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6562. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6563. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6564. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6565. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6566. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6567. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6568. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6569. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6570. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6571. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6572. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6573. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6574. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6575. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6576. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6577. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6578. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6579. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6580. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6581. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6582. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6583. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6584. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6585. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6586. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6587. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6588. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6589. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6590. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6591. '']));
  6592. end;
  6593. procedure TTestModule.TestSet_Operator_In;
  6594. begin
  6595. StartProgram(false);
  6596. Add([
  6597. 'type',
  6598. ' TColor = (Red, Green, Blue);',
  6599. ' TColors = set of tcolor;',
  6600. ' TColorRg = green..blue;',
  6601. 'var',
  6602. ' vC: tcolor;',
  6603. ' vT: tcolors;',
  6604. ' B: boolean;',
  6605. ' rg: TColorRg;',
  6606. 'begin',
  6607. ' b:=red in vt;',
  6608. ' b:=vc in vt;',
  6609. ' b:=green in [red..blue];',
  6610. ' b:=vc in [red..blue];',
  6611. ' ',
  6612. ' if red in vt then ;',
  6613. ' while vC in vt do ;',
  6614. ' repeat',
  6615. ' until vC in vt;',
  6616. ' if rg in [green..blue] then ;',
  6617. '']);
  6618. ConvertProgram;
  6619. CheckSource('TestSet_Operator_In',
  6620. LinesToStr([ // statements
  6621. 'this.TColor = {',
  6622. ' "0":"Red",',
  6623. ' Red:0,',
  6624. ' "1":"Green",',
  6625. ' Green:1,',
  6626. ' "2":"Blue",',
  6627. ' Blue:2',
  6628. ' };',
  6629. 'this.vC = 0;',
  6630. 'this.vT = {};',
  6631. 'this.B = false;',
  6632. 'this.rg = this.TColor.Green;',
  6633. '']),
  6634. LinesToStr([
  6635. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6636. '$mod.B = $mod.vC in $mod.vT;',
  6637. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6638. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6639. 'if ($mod.TColor.Red in $mod.vT) ;',
  6640. 'while ($mod.vC in $mod.vT) {',
  6641. '};',
  6642. 'do {',
  6643. '} while (!($mod.vC in $mod.vT));',
  6644. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6645. '']));
  6646. end;
  6647. procedure TTestModule.TestSet_Functions;
  6648. begin
  6649. StartProgram(false);
  6650. Add('type');
  6651. Add(' TMyEnum = (Red, Green);');
  6652. Add(' TMyEnums = set of TMyEnum;');
  6653. Add('var');
  6654. Add(' e: TMyEnum;');
  6655. Add(' s: TMyEnums;');
  6656. Add('begin');
  6657. Add(' e:=Low(TMyEnums);');
  6658. Add(' e:=Low(s);');
  6659. Add(' e:=High(TMyEnums);');
  6660. Add(' e:=High(s);');
  6661. ConvertProgram;
  6662. CheckSource('TestSetFunctions',
  6663. LinesToStr([ // statements
  6664. 'this.TMyEnum = {',
  6665. ' "0":"Red",',
  6666. ' Red:0,',
  6667. ' "1":"Green",',
  6668. ' Green:1',
  6669. ' };',
  6670. 'this.e = 0;',
  6671. 'this.s = {};'
  6672. ]),
  6673. LinesToStr([
  6674. '$mod.e=$mod.TMyEnum.Red;',
  6675. '$mod.e=$mod.TMyEnum.Red;',
  6676. '$mod.e=$mod.TMyEnum.Green;',
  6677. '$mod.e=$mod.TMyEnum.Green;',
  6678. '']));
  6679. end;
  6680. procedure TTestModule.TestSet_PassAsArgClone;
  6681. begin
  6682. StartProgram(false);
  6683. Add('type');
  6684. Add(' TMyEnum = (Red, Green);');
  6685. Add(' TMyEnums = set of TMyEnum;');
  6686. Add('procedure DoDefault(s: tmyenums); begin end;');
  6687. Add('procedure DoConst(const s: tmyenums); begin end;');
  6688. Add('var');
  6689. Add(' aSet: tmyenums;');
  6690. Add('begin');
  6691. Add(' dodefault(aset);');
  6692. Add(' doconst(aset);');
  6693. ConvertProgram;
  6694. CheckSource('TestSetFunctions',
  6695. LinesToStr([ // statements
  6696. 'this.TMyEnum = {',
  6697. ' "0":"Red",',
  6698. ' Red:0,',
  6699. ' "1":"Green",',
  6700. ' Green:1',
  6701. ' };',
  6702. 'this.DoDefault = function (s) {',
  6703. '};',
  6704. 'this.DoConst = function (s) {',
  6705. '};',
  6706. 'this.aSet = {};'
  6707. ]),
  6708. LinesToStr([
  6709. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6710. '$mod.DoConst($mod.aSet);',
  6711. '']));
  6712. end;
  6713. procedure TTestModule.TestSet_AsParams;
  6714. begin
  6715. StartProgram(false);
  6716. Add([
  6717. 'type TEnum = (Red,Blue);',
  6718. 'type TEnums = set of TEnum;',
  6719. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6720. 'var vJ: TEnums;',
  6721. 'begin',
  6722. ' Include(vg,red);',
  6723. ' Include(result,blue);',
  6724. ' vg:=vg;',
  6725. ' vj:=vh;',
  6726. ' vi:=vi;',
  6727. ' doit(vg,vg,vg);',
  6728. ' doit(vh,vh,vj);',
  6729. ' doit(vi,vi,vi);',
  6730. ' doit(vj,vj,vj);',
  6731. 'end;',
  6732. 'var i: TEnums;',
  6733. 'begin',
  6734. ' doit(i,i,i);']);
  6735. ConvertProgram;
  6736. CheckSource('TestSet_AsParams',
  6737. LinesToStr([ // statements
  6738. 'this.TEnum = {',
  6739. ' "0": "Red",',
  6740. ' Red: 0,',
  6741. ' "1": "Blue",',
  6742. ' Blue: 1',
  6743. '};',
  6744. 'this.DoIt = function (vG,vH,vI) {',
  6745. ' var Result = {};',
  6746. ' var vJ = {};',
  6747. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6748. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6749. ' vG = rtl.refSet(vG);',
  6750. ' vJ = rtl.refSet(vH);',
  6751. ' vI.set(rtl.refSet(vI.get()));',
  6752. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6753. ' get: function () {',
  6754. ' return vG;',
  6755. ' },',
  6756. ' set: function (v) {',
  6757. ' vG = v;',
  6758. ' }',
  6759. ' });',
  6760. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6761. ' get: function () {',
  6762. ' return vJ;',
  6763. ' },',
  6764. ' set: function (v) {',
  6765. ' vJ = v;',
  6766. ' }',
  6767. ' });',
  6768. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6769. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6770. ' get: function () {',
  6771. ' return vJ;',
  6772. ' },',
  6773. ' set: function (v) {',
  6774. ' vJ = v;',
  6775. ' }',
  6776. ' });',
  6777. ' return Result;',
  6778. '};',
  6779. 'this.i = {};'
  6780. ]),
  6781. LinesToStr([
  6782. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6783. ' p: $mod,',
  6784. ' get: function () {',
  6785. ' return this.p.i;',
  6786. ' },',
  6787. ' set: function (v) {',
  6788. ' this.p.i = v;',
  6789. ' }',
  6790. '});'
  6791. ]));
  6792. end;
  6793. procedure TTestModule.TestSet_Property;
  6794. begin
  6795. StartProgram(false);
  6796. Add('type');
  6797. Add(' TEnum = (Red,Blue);');
  6798. Add(' TEnums = set of TEnum;');
  6799. Add(' TObject = class');
  6800. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6801. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6802. Add(' property Colors: TEnums read GetColors write SetColors;');
  6803. Add(' end;');
  6804. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6805. Add('begin end;');
  6806. Add('var Obj: TObject;');
  6807. Add('begin');
  6808. Add(' Include(Obj.Colors,Red);');
  6809. Add(' Exclude(Obj.Colors,Red);');
  6810. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6811. ConvertProgram;
  6812. CheckSource('TestSet_Property',
  6813. LinesToStr([ // statements
  6814. 'this.TEnum = {',
  6815. ' "0": "Red",',
  6816. ' Red: 0,',
  6817. ' "1": "Blue",',
  6818. ' Blue: 1',
  6819. '};',
  6820. 'rtl.createClass(this, "TObject", null, function () {',
  6821. ' this.$init = function () {',
  6822. ' };',
  6823. ' this.$final = function () {',
  6824. ' };',
  6825. '});',
  6826. 'this.DoIt = function (i, j, k, l) {',
  6827. '};',
  6828. 'this.Obj = null;',
  6829. '']),
  6830. LinesToStr([
  6831. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6832. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6833. '']));
  6834. end;
  6835. procedure TTestModule.TestSet_EnumConst;
  6836. begin
  6837. StartProgram(false);
  6838. Add([
  6839. 'type',
  6840. ' TEnum = (Red,Blue);',
  6841. ' TEnums = set of TEnum;',
  6842. 'const',
  6843. ' Orange = red;',
  6844. 'var',
  6845. ' Enum: tenum;',
  6846. ' Enums: tenums;',
  6847. 'begin',
  6848. ' Include(enums,orange);',
  6849. ' Exclude(enums,orange);',
  6850. ' if orange in enums then;',
  6851. ' if orange in [orange,red] then;']);
  6852. ConvertProgram;
  6853. CheckSource('TestSet_EnumConst',
  6854. LinesToStr([ // statements
  6855. 'this.TEnum = {',
  6856. ' "0": "Red",',
  6857. ' Red: 0,',
  6858. ' "1": "Blue",',
  6859. ' Blue: 1',
  6860. '};',
  6861. 'this.Orange = this.TEnum.Red;',
  6862. 'this.Enum = 0;',
  6863. 'this.Enums = {};',
  6864. '']),
  6865. LinesToStr([
  6866. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6867. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6868. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6869. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6870. '']));
  6871. end;
  6872. procedure TTestModule.TestSet_IntConst;
  6873. begin
  6874. StartProgram(false);
  6875. Add([
  6876. 'type',
  6877. ' TEnums = set of Byte;',
  6878. 'const',
  6879. ' Orange = 0;',
  6880. 'var',
  6881. ' Enum: byte;',
  6882. ' Enums: tenums;',
  6883. 'begin',
  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.Enums = {};',
  6900. '$mod.Enums = rtl.createSet(0);',
  6901. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6902. 'if (0 in $mod.Enums) ;',
  6903. 'if (0 in rtl.createSet(0, 1)) ;',
  6904. '']));
  6905. end;
  6906. procedure TTestModule.TestSet_IntRange;
  6907. begin
  6908. StartProgram(false);
  6909. Add([
  6910. 'type',
  6911. ' TRange = 1..3;',
  6912. ' TEnums = set of TRange;',
  6913. 'const',
  6914. ' Orange = 2;',
  6915. 'var',
  6916. ' Enum: byte;',
  6917. ' Enums: TEnums;',
  6918. 'begin',
  6919. ' Enums:=[];',
  6920. ' Enums:=[1];',
  6921. ' Enums:=[2..3];',
  6922. ' Include(enums,orange);',
  6923. ' Exclude(enums,orange);',
  6924. ' if orange in enums then;',
  6925. ' if orange in [orange,1] then;']);
  6926. ConvertProgram;
  6927. CheckSource('TestSet_IntRange',
  6928. LinesToStr([ // statements
  6929. 'this.Orange = 2;',
  6930. 'this.Enum = 0;',
  6931. 'this.Enums = {};',
  6932. '']),
  6933. LinesToStr([
  6934. '$mod.Enums = {};',
  6935. '$mod.Enums = rtl.createSet(1);',
  6936. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6937. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6938. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6939. 'if (2 in $mod.Enums) ;',
  6940. 'if (2 in rtl.createSet(2, 1)) ;',
  6941. '']));
  6942. end;
  6943. procedure TTestModule.TestSet_AnonymousEnumType;
  6944. begin
  6945. StartProgram(false);
  6946. Add('type');
  6947. Add(' TFlags = set of (red, green);');
  6948. Add('const');
  6949. Add(' favorite = red;');
  6950. Add('var');
  6951. Add(' f: TFlags;');
  6952. Add(' i: longint;');
  6953. Add('begin');
  6954. Add(' Include(f,red);');
  6955. Add(' Include(f,favorite);');
  6956. Add(' i:=ord(red);');
  6957. Add(' i:=ord(favorite);');
  6958. Add(' i:=ord(low(TFlags));');
  6959. Add(' i:=ord(low(f));');
  6960. Add(' i:=ord(low(favorite));');
  6961. Add(' i:=ord(high(TFlags));');
  6962. Add(' i:=ord(high(f));');
  6963. Add(' i:=ord(high(favorite));');
  6964. Add(' f:=[green,favorite];');
  6965. ConvertProgram;
  6966. CheckSource('TestSet_AnonymousEnumType',
  6967. LinesToStr([ // statements
  6968. 'this.TFlags$a = {',
  6969. ' "0": "red",',
  6970. ' red: 0,',
  6971. ' "1": "green",',
  6972. ' green: 1',
  6973. '};',
  6974. 'this.favorite = this.TFlags$a.red;',
  6975. 'this.f = {};',
  6976. 'this.i = 0;',
  6977. '']),
  6978. LinesToStr([
  6979. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6980. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6981. '$mod.i = $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.green;',
  6987. '$mod.i = $mod.TFlags$a.green;',
  6988. '$mod.i = $mod.TFlags$a.green;',
  6989. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6990. '']));
  6991. end;
  6992. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6993. begin
  6994. exit;
  6995. StartProgram(false);
  6996. Add([
  6997. 'type',
  6998. ' TAtoZ = ''A''..''Z'';',
  6999. ' TSetOfAZ = set of TAtoZ;',
  7000. 'var',
  7001. ' c: char;',
  7002. ' a: TAtoZ;',
  7003. ' s: TSetOfAZ = [''P'',''A''];',
  7004. ' i: longint;',
  7005. 'begin',
  7006. ' Include(s,''S'');',
  7007. ' Include(s,c);',
  7008. ' Include(s,a);',
  7009. ' c:=low(TAtoZ);',
  7010. ' i:=ord(low(TAtoZ));',
  7011. ' a:=high(TAtoZ);',
  7012. ' a:=high(TSetOfAtoZ);',
  7013. ' s:=[a,c,''M''];',
  7014. '']);
  7015. ConvertProgram;
  7016. CheckSource('TestSet_AnonymousEnumTypeChar',
  7017. LinesToStr([ // statements
  7018. '']),
  7019. LinesToStr([
  7020. '']));
  7021. end;
  7022. procedure TTestModule.TestSet_ConstEnum;
  7023. begin
  7024. StartProgram(false);
  7025. Add([
  7026. 'type',
  7027. ' TEnum = (red,blue,green);',
  7028. ' TEnums = set of TEnum;',
  7029. 'const',
  7030. ' teAny = [low(TEnum)..high(TEnum)];',
  7031. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7032. 'var',
  7033. ' e: TEnum;',
  7034. ' s: TEnums;',
  7035. 'begin',
  7036. ' if blue in teAny then;',
  7037. ' if blue in teAny+[e] then;',
  7038. ' if blue in teAny+teRedBlue then;',
  7039. ' if e in [red,blue] then;',
  7040. ' s:=teAny;',
  7041. ' s:=teAny+[e];',
  7042. ' s:=[e]+teAny;',
  7043. ' s:=teAny+teRedBlue;',
  7044. ' s:=teAny+teRedBlue+[e];',
  7045. '']);
  7046. ConvertProgram;
  7047. CheckSource('TestSet_ConstEnum',
  7048. LinesToStr([ // statements
  7049. 'this.TEnum = {',
  7050. ' "0": "red",',
  7051. ' red: 0,',
  7052. ' "1": "blue",',
  7053. ' blue: 1,',
  7054. ' "2": "green",',
  7055. ' green: 2',
  7056. '};',
  7057. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7058. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7059. 'this.e = 0;',
  7060. 'this.s = {};',
  7061. '']),
  7062. LinesToStr([
  7063. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7064. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7065. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7066. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7067. '$mod.s = rtl.refSet($mod.teAny);',
  7068. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7069. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7070. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7071. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7072. '']));
  7073. end;
  7074. procedure TTestModule.TestSet_ConstChar;
  7075. begin
  7076. StartProgram(false);
  7077. Add([
  7078. 'const',
  7079. ' LowChars = [''a''..''z''];',
  7080. ' Chars = LowChars+[''A''..''Z''];',
  7081. ' sc = [''А'', ''Я''];',
  7082. 'var',
  7083. ' c: char;',
  7084. ' s: string;',
  7085. 'begin',
  7086. ' if c in lowchars then ;',
  7087. ' if ''a'' in lowchars then ;',
  7088. ' if s[1] in lowchars then ;',
  7089. ' if c in chars then ;',
  7090. ' if c in [''a''..''z'',''_''] then ;',
  7091. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7092. ' if ''Я'' in sc then ;',
  7093. ' if 3=ord('' '') then ;',
  7094. '']);
  7095. ConvertProgram;
  7096. CheckSource('TestSet_ConstChar',
  7097. LinesToStr([ // statements
  7098. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7099. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7100. 'this.sc = rtl.createSet(1040, 1071);',
  7101. 'this.c = "";',
  7102. 'this.s = "";',
  7103. '']),
  7104. LinesToStr([
  7105. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7106. 'if (97 in $mod.LowChars) ;',
  7107. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7108. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7109. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7110. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7111. 'if (1071 in $mod.sc) ;',
  7112. 'if (3 === 32) ;',
  7113. '']));
  7114. end;
  7115. procedure TTestModule.TestSet_ConstInt;
  7116. begin
  7117. StartProgram(false);
  7118. Add([
  7119. 'const',
  7120. ' Months = [1..12];',
  7121. ' Mirror = [-12..-1]+Months;',
  7122. 'var',
  7123. ' i: smallint;',
  7124. 'begin',
  7125. ' if 3 in Months then;',
  7126. ' if i in Months+[i] then;',
  7127. ' if i in Months+Mirror then;',
  7128. ' if i in [4..6,8] then;',
  7129. '']);
  7130. ConvertProgram;
  7131. CheckSource('TestSet_ConstInt',
  7132. LinesToStr([ // statements
  7133. 'this.Months = rtl.createSet(null, 1, 12);',
  7134. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7135. 'this.i = 0;',
  7136. '']),
  7137. LinesToStr([
  7138. 'if (3 in $mod.Months) ;',
  7139. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7140. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7141. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7142. '']));
  7143. end;
  7144. procedure TTestModule.TestSet_InFunction;
  7145. begin
  7146. StartProgram(false);
  7147. Add([
  7148. 'const',
  7149. ' TEnum = 3;',
  7150. ' TSetOfEnum = 4;',
  7151. ' TSetOfAno = 5;',
  7152. 'procedure DoIt;',
  7153. 'type',
  7154. ' TEnum = (red, blue);',
  7155. ' TSetOfEnum = set of TEnum;',
  7156. ' TSetOfAno = set of (up,down);',
  7157. 'var',
  7158. ' e: TEnum;',
  7159. ' se: TSetOfEnum;',
  7160. ' sa: TSetOfAno;',
  7161. 'begin',
  7162. ' se:=[e];',
  7163. ' sa:=[up];',
  7164. 'end;',
  7165. 'begin',
  7166. '']);
  7167. ConvertProgram;
  7168. CheckSource('TestSet_InFunction',
  7169. LinesToStr([ // statements
  7170. 'this.TEnum = 3;',
  7171. 'this.TSetOfEnum = 4;',
  7172. 'this.TSetOfAno = 5;',
  7173. 'var TEnum$1 = {',
  7174. ' "0": "red",',
  7175. ' red: 0,',
  7176. ' "1": "blue",',
  7177. ' blue: 1',
  7178. '};',
  7179. 'var TSetOfAno$a = {',
  7180. ' "0": "up",',
  7181. ' up: 0,',
  7182. ' "1": "down",',
  7183. ' down: 1',
  7184. '};',
  7185. 'this.DoIt = function () {',
  7186. ' var e = 0;',
  7187. ' var se = {};',
  7188. ' var sa = {};',
  7189. ' se = rtl.createSet(e);',
  7190. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7191. '};',
  7192. '']),
  7193. LinesToStr([
  7194. '']));
  7195. end;
  7196. procedure TTestModule.TestSet_ForIn;
  7197. begin
  7198. StartProgram(false);
  7199. Add([
  7200. 'type',
  7201. ' TEnum = (Red, Green, Blue);',
  7202. ' TEnumRg = green..blue;',
  7203. ' TSetOfEnum = set of TEnum;',
  7204. ' TSetOfEnumRg = set of TEnumRg;',
  7205. 'var',
  7206. ' e, e2: TEnum;',
  7207. ' er: TEnum;',
  7208. ' s: TSetOfEnum;',
  7209. 'begin',
  7210. ' for e in TSetOfEnum do ;',
  7211. ' for e in TSetOfEnumRg do ;',
  7212. ' for e in [] do e2:=e;',
  7213. ' for e in [red..green] do e2:=e;',
  7214. ' for e in [green,blue] do e2:=e;',
  7215. ' for e in [red,blue] do e2:=e;',
  7216. ' for e in s do e2:=e;',
  7217. ' for er in TSetOfEnumRg do ;',
  7218. '']);
  7219. ConvertProgram;
  7220. CheckSource('TestSet_ForIn',
  7221. LinesToStr([ // statements
  7222. 'this.TEnum = {',
  7223. ' "0":"Red",',
  7224. ' Red:0,',
  7225. ' "1":"Green",',
  7226. ' Green:1,',
  7227. ' "2":"Blue",',
  7228. ' Blue:2',
  7229. ' };',
  7230. 'this.e = 0;',
  7231. 'this.e2 = 0;',
  7232. 'this.er = 0;',
  7233. 'this.s = {};',
  7234. '']),
  7235. LinesToStr([
  7236. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7237. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7238. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7239. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7240. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7241. 'for (var $l in $mod.s){',
  7242. ' $mod.e = +$l;',
  7243. ' $mod.e2 = $mod.e;',
  7244. '};',
  7245. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7246. '']));
  7247. end;
  7248. procedure TTestModule.TestNestBegin;
  7249. begin
  7250. StartProgram(false);
  7251. Add('begin');
  7252. Add(' begin');
  7253. Add(' begin');
  7254. Add(' end;');
  7255. Add(' begin');
  7256. Add(' if true then ;');
  7257. Add(' end;');
  7258. Add(' end;');
  7259. ConvertProgram;
  7260. CheckSource('TestNestBegin',
  7261. '',
  7262. 'if (true) ;');
  7263. end;
  7264. procedure TTestModule.TestUnitImplVars;
  7265. begin
  7266. StartUnit(false);
  7267. Add('interface');
  7268. Add('implementation');
  7269. Add('var');
  7270. Add(' V1:longint;');
  7271. Add(' V2:longint = 3;');
  7272. Add(' V3:string = ''abc'';');
  7273. ConvertUnit;
  7274. CheckSource('TestUnitImplVars',
  7275. LinesToStr([ // statements
  7276. 'var $impl = $mod.$impl;',
  7277. '']),
  7278. '', // this.$init
  7279. LinesToStr([ // implementation
  7280. '$impl.V1 = 0;',
  7281. '$impl.V2 = 3;',
  7282. '$impl.V3 = "abc";',
  7283. '']) );
  7284. end;
  7285. procedure TTestModule.TestUnitImplConsts;
  7286. begin
  7287. StartUnit(false);
  7288. Add('interface');
  7289. Add('implementation');
  7290. Add('const');
  7291. Add(' v1 = 3;');
  7292. Add(' v2:longint = 4;');
  7293. Add(' v3:string = ''abc'';');
  7294. ConvertUnit;
  7295. CheckSource('TestUnitImplConsts',
  7296. LinesToStr([ // statements
  7297. 'var $impl = $mod.$impl;',
  7298. '']),
  7299. '', // this.$init
  7300. LinesToStr([ // implementation
  7301. '$impl.v1 = 3;',
  7302. '$impl.v2 = 4;',
  7303. '$impl.v3 = "abc";',
  7304. '']) );
  7305. end;
  7306. procedure TTestModule.TestUnitImplRecord;
  7307. begin
  7308. StartUnit(false);
  7309. Add('interface');
  7310. Add('implementation');
  7311. Add('type');
  7312. Add(' TMyRecord = record');
  7313. Add(' i: longint;');
  7314. Add(' end;');
  7315. Add('var aRec: TMyRecord;');
  7316. Add('initialization');
  7317. Add(' arec.i:=3;');
  7318. ConvertUnit;
  7319. CheckSource('TestUnitImplRecord',
  7320. LinesToStr([ // statements
  7321. 'var $impl = $mod.$impl;',
  7322. '']),
  7323. // this.$init
  7324. '$impl.aRec.i = 3;',
  7325. LinesToStr([ // implementation
  7326. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7327. ' this.i = 0;',
  7328. ' this.$eq = function (b) {',
  7329. ' return this.i === b.i;',
  7330. ' };',
  7331. ' this.$assign = function (s) {',
  7332. ' this.i = s.i;',
  7333. ' return this;',
  7334. ' };',
  7335. '});',
  7336. '$impl.aRec = $impl.TMyRecord.$new();',
  7337. '']) );
  7338. end;
  7339. procedure TTestModule.TestRenameJSNameConflict;
  7340. begin
  7341. StartProgram(false);
  7342. Add('var apply: longint;');
  7343. Add('var bind: longint;');
  7344. Add('var call: longint;');
  7345. Add('begin');
  7346. ConvertProgram;
  7347. CheckSource('TestRenameJSNameConflict',
  7348. LinesToStr([ // statements
  7349. 'this.Apply = 0;',
  7350. 'this.Bind = 0;',
  7351. 'this.Call = 0;'
  7352. ]),
  7353. LinesToStr([ // this.$main
  7354. ''
  7355. ]));
  7356. end;
  7357. procedure TTestModule.TestLocalConst;
  7358. begin
  7359. StartProgram(false);
  7360. Add('procedure DoIt;');
  7361. Add('const');
  7362. Add(' cA: longint = 1;');
  7363. Add(' cB = 2;');
  7364. Add(' procedure Sub;');
  7365. Add(' const');
  7366. Add(' csA = 3;');
  7367. Add(' cB: double = 4;');
  7368. Add(' begin');
  7369. Add(' cb:=cb+csa;');
  7370. Add(' ca:=ca+csa+5;');
  7371. Add(' end;');
  7372. Add('begin');
  7373. Add(' ca:=ca+cb+6;');
  7374. Add('end;');
  7375. Add('begin');
  7376. ConvertProgram;
  7377. CheckSource('TestLocalConst',
  7378. LinesToStr([
  7379. 'var cA = 1;',
  7380. 'var cB = 2;',
  7381. 'var csA = 3;',
  7382. 'var cB$1 = 4;',
  7383. 'this.DoIt = function () {',
  7384. ' function Sub() {',
  7385. ' cB$1 = cB$1 + 3;',
  7386. ' cA = cA + 3 + 5;',
  7387. ' };',
  7388. ' cA = cA + 2 + 6;',
  7389. '};'
  7390. ]),
  7391. LinesToStr([
  7392. ]));
  7393. end;
  7394. procedure TTestModule.TestVarExternal;
  7395. begin
  7396. StartProgram(false);
  7397. Add('var');
  7398. Add(' NaN: double; external name ''Global.NaN'';');
  7399. Add(' d: double;');
  7400. Add('begin');
  7401. Add(' d:=NaN;');
  7402. ConvertProgram;
  7403. CheckSource('TestVarExternal',
  7404. LinesToStr([
  7405. 'this.d = 0.0;'
  7406. ]),
  7407. LinesToStr([
  7408. '$mod.d = Global.NaN;'
  7409. ]));
  7410. end;
  7411. procedure TTestModule.TestVarExternalOtherUnit;
  7412. begin
  7413. AddModuleWithIntfImplSrc('unit2.pas',
  7414. LinesToStr([
  7415. 'var NaN: double; external name ''Global.NaN'';',
  7416. 'var iV: longint;'
  7417. ]),
  7418. '');
  7419. StartUnit(true);
  7420. Add('interface');
  7421. Add('uses unit2;');
  7422. Add('implementation');
  7423. Add('var');
  7424. Add(' d: double;');
  7425. Add(' i: longint; external name ''$i'';');
  7426. Add('begin');
  7427. Add(' d:=nan;');
  7428. Add(' d:=uNit2.nan;');
  7429. Add(' d:=test1.d;');
  7430. Add(' i:=iv;');
  7431. Add(' i:=uNit2.iv;');
  7432. Add(' i:=test1.i;');
  7433. ConvertUnit;
  7434. CheckSource('TestVarExternalOtherUnit',
  7435. LinesToStr([
  7436. 'var $impl = $mod.$impl;',
  7437. '']),
  7438. LinesToStr([ // this.$init
  7439. '$impl.d = Global.NaN;',
  7440. '$impl.d = Global.NaN;',
  7441. '$impl.d = $impl.d;',
  7442. '$i = pas.unit2.iV;',
  7443. '$i = pas.unit2.iV;',
  7444. '$i = $i;',
  7445. '']),
  7446. LinesToStr([ // implementation
  7447. '$impl.d = 0.0;',
  7448. '']) );
  7449. end;
  7450. procedure TTestModule.TestVarAbsoluteFail;
  7451. begin
  7452. StartProgram(false);
  7453. Add([
  7454. 'var',
  7455. ' a: longint;',
  7456. ' b: longword absolute a;',
  7457. 'begin']);
  7458. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7459. ConvertProgram;
  7460. end;
  7461. procedure TTestModule.TestConstExternal;
  7462. begin
  7463. StartProgram(false);
  7464. Add([
  7465. 'const',
  7466. ' PI: double; external name ''Global.PI'';',
  7467. ' Tau = 2*pi;',
  7468. 'var d: double;',
  7469. 'begin',
  7470. ' d:=pi;',
  7471. ' d:=tau+pi;']);
  7472. ConvertProgram;
  7473. CheckSource('TestConstExternal',
  7474. LinesToStr([
  7475. 'this.Tau = 2*Global.PI;',
  7476. 'this.d = 0.0;'
  7477. ]),
  7478. LinesToStr([
  7479. '$mod.d = Global.PI;',
  7480. '$mod.d = $mod.Tau + Global.PI;'
  7481. ]));
  7482. end;
  7483. procedure TTestModule.TestDouble;
  7484. begin
  7485. StartProgram(false);
  7486. Add([
  7487. 'type',
  7488. ' TDateTime = double;',
  7489. 'const',
  7490. ' a = TDateTime(2.7);',
  7491. ' b = a + TDateTime(1.7);',
  7492. ' c = 0.9 + 0.1;',
  7493. ' f0_1 = 0.1;',
  7494. ' f0_3 = 0.3;',
  7495. ' fn0_1 = -0.1;',
  7496. ' fn0_3 = -0.3;',
  7497. ' fn0_003 = -0.003;',
  7498. ' fn0_123456789 = -0.123456789;',
  7499. ' fn300_0 = -300.0;',
  7500. ' fn123456_0 = -123456.0;',
  7501. ' fn1234567_8 = -1234567.8;',
  7502. ' fn12345678_9 = -12345678.9;',
  7503. ' f1_0En12 = 1E-12;',
  7504. ' fn1_0En12 = -1E-12;',
  7505. ' maxdouble = 1.7e+308;',
  7506. ' mindouble = -1.7e+308;',
  7507. ' MinSafeIntDouble = -$1fffffffffffff;',
  7508. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7509. ' MaxSafeIntDouble = $1fffffffffffff;',
  7510. ' DZeroResolution = 1E-12;',
  7511. ' Minus1 = -1E-12;',
  7512. ' EPS = 1E-9;',
  7513. ' DELTA = 0.001;',
  7514. ' Big = 129.789E+100;',
  7515. ' Test0_15 = 0.15;',
  7516. ' Test999 = 2.9999999999999;',
  7517. ' Test111999 = 211199999999999000.0;',
  7518. ' TestMinus111999 = -211199999999999000.0;',
  7519. ' Inf = 1.0 / 0.0;',
  7520. ' NegInf = -1.0 / 0.0;',
  7521. 'procedure Run(d: double); external name ''Run'';',
  7522. 'var',
  7523. ' d: double = b;',
  7524. 'begin',
  7525. ' d:=1.0;',
  7526. ' d:=1.0/3.0;',
  7527. ' d:=1.0/(3-2-1);',
  7528. ' d:=1/3;',
  7529. ' d:=5.0E-324;',
  7530. ' d:=1.7E308;',
  7531. ' d:=001.00E00;',
  7532. ' d:=002.00E001;',
  7533. ' d:=003.000E000;',
  7534. ' d:=-004.00E-00;',
  7535. ' d:=-005.00E-001;',
  7536. ' d:=10**3;',
  7537. ' d:=10 mod 3;',
  7538. ' d:=10 div 3;',
  7539. ' d:=c;',
  7540. ' d:=f0_1;',
  7541. ' d:=f0_3;',
  7542. ' d:=fn0_1;',
  7543. ' d:=fn0_3;',
  7544. ' d:=fn0_003;',
  7545. ' d:=fn0_123456789;',
  7546. ' d:=fn300_0;',
  7547. ' d:=fn123456_0;',
  7548. ' d:=fn1234567_8;',
  7549. ' d:=fn12345678_9;',
  7550. ' d:=f1_0En12;',
  7551. ' d:=fn1_0En12;',
  7552. ' d:=maxdouble;',
  7553. ' d:=mindouble;',
  7554. ' d:=MinSafeIntDouble;',
  7555. ' d:=double(MinSafeIntDouble);',
  7556. ' d:=MinSafeIntDouble2;',
  7557. ' d:=double(MinSafeIntDouble2);',
  7558. ' d:=MaxSafeIntDouble;',
  7559. ' d:=default(double);',
  7560. ' Run(Inf);',
  7561. ' Run(NegInf);',
  7562. '']);
  7563. ConvertProgram;
  7564. CheckSource('TestDouble',
  7565. LinesToStr([
  7566. 'this.a = 2.7;',
  7567. 'this.b = 2.7 + 1.7;',
  7568. 'this.c = 0.9 + 0.1;',
  7569. 'this.f0_1 = 0.1;',
  7570. 'this.f0_3 = 0.3;',
  7571. 'this.fn0_1 = -0.1;',
  7572. 'this.fn0_3 = -0.3;',
  7573. 'this.fn0_003 = -0.003;',
  7574. 'this.fn0_123456789 = -0.123456789;',
  7575. 'this.fn300_0 = -300.0;',
  7576. 'this.fn123456_0 = -123456.0;',
  7577. 'this.fn1234567_8 = -1234567.8;',
  7578. 'this.fn12345678_9 = -12345678.9;',
  7579. 'this.f1_0En12 = 1E-12;',
  7580. 'this.fn1_0En12 = -1E-12;',
  7581. 'this.maxdouble = 1.7e+308;',
  7582. 'this.mindouble = -1.7e+308;',
  7583. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7584. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7585. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7586. 'this.DZeroResolution = 1E-12;',
  7587. 'this.Minus1 = -1E-12;',
  7588. 'this.EPS = 1E-9;',
  7589. 'this.DELTA = 0.001;',
  7590. 'this.Big = 129.789E+100;',
  7591. 'this.Test0_15 = 0.15;',
  7592. 'this.Test999 = 2.9999999999999;',
  7593. 'this.Test111999 = 211199999999999000.0;',
  7594. 'this.TestMinus111999 = -211199999999999000.0;',
  7595. 'this.Inf = 1.0 / 0.0;',
  7596. 'this.NegInf = -1.0 / 0.0;',
  7597. 'this.d = 4.4;',
  7598. '']),
  7599. LinesToStr([
  7600. '$mod.d = 1.0;',
  7601. '$mod.d = 1.0 / 3.0;',
  7602. '$mod.d = 1.0 / (3 - 2 - 1);',
  7603. '$mod.d = 1 / 3;',
  7604. '$mod.d = 5.0E-324;',
  7605. '$mod.d = 1.7E308;',
  7606. '$mod.d = 1.00E0;',
  7607. '$mod.d = 2.00E1;',
  7608. '$mod.d = 3.000E0;',
  7609. '$mod.d = -4.00E-0;',
  7610. '$mod.d = -5.00E-1;',
  7611. '$mod.d = Math.pow(10, 3);',
  7612. '$mod.d = 10 % 3;',
  7613. '$mod.d = rtl.trunc(10 / 3);',
  7614. '$mod.d = 1;',
  7615. '$mod.d = 0.1;',
  7616. '$mod.d = 0.3;',
  7617. '$mod.d = -0.1;',
  7618. '$mod.d = -0.3;',
  7619. '$mod.d = -0.003;',
  7620. '$mod.d = -0.123456789;',
  7621. '$mod.d = -300;',
  7622. '$mod.d = -123456;',
  7623. '$mod.d = -1234567.8;',
  7624. '$mod.d = -1.23456789E7;',
  7625. '$mod.d = 1E-12;',
  7626. '$mod.d = -1E-12;',
  7627. '$mod.d = 1.7E308;',
  7628. '$mod.d = -1.7E308;',
  7629. '$mod.d = -9007199254740991;',
  7630. '$mod.d = -9007199254740991;',
  7631. '$mod.d = -9.007199254740992E15;',
  7632. '$mod.d = -9.007199254740992E15;',
  7633. '$mod.d = 9007199254740991;',
  7634. '$mod.d = 0.0;',
  7635. 'Run(1 / 0);',
  7636. 'Run(-1 / 0);',
  7637. '']));
  7638. end;
  7639. procedure TTestModule.TestInteger;
  7640. begin
  7641. StartProgram(false);
  7642. Add([
  7643. 'const',
  7644. ' MinInt = low(NativeInt);',
  7645. ' MaxInt = high(NativeInt);',
  7646. 'type',
  7647. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7648. 'const',
  7649. ' a = low(TMyInt)+High(TMyInt);',
  7650. 'var',
  7651. ' i: TMyInt;',
  7652. 'begin',
  7653. ' i:=-MinInt;',
  7654. ' i:=default(TMyInt);',
  7655. ' i:=low(i)+high(i);',
  7656. '']);
  7657. ConvertProgram;
  7658. CheckSource('TestIntegerRange',
  7659. LinesToStr([
  7660. 'this.MinInt = -9007199254740991;',
  7661. 'this.MaxInt = 9007199254740991;',
  7662. 'this.a = -9007199254740991 + 9007199254740991;',
  7663. 'this.i = 0;',
  7664. '']),
  7665. LinesToStr([
  7666. '$mod.i = - -9007199254740991;',
  7667. '$mod.i = -9007199254740991;',
  7668. '$mod.i = -9007199254740991 + 9007199254740991;',
  7669. '']));
  7670. end;
  7671. procedure TTestModule.TestIntegerRange;
  7672. begin
  7673. StartProgram(false);
  7674. Add([
  7675. 'const',
  7676. ' MinInt = -1;',
  7677. ' MaxInt = +1;',
  7678. 'type',
  7679. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7680. ' TInt2 = 1..3;',
  7681. 'const',
  7682. ' a = low(TMyInt)+High(TMyInt);',
  7683. ' b = low(TInt2)+High(TInt2);',
  7684. ' s1 = [1];',
  7685. ' s2 = [1,2];',
  7686. ' s3 = [1..3];',
  7687. ' s4 = [low(shortint)..high(shortint)];',
  7688. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7689. ' s6 = 1 in s2;',
  7690. 'var',
  7691. ' i: TMyInt;',
  7692. ' i2: TInt2;',
  7693. 'begin',
  7694. ' i:=i2;',
  7695. ' i:=default(TMyInt);',
  7696. ' if i=i2 then ;',
  7697. ' i:=ord(i2);',
  7698. '']);
  7699. ConvertProgram;
  7700. CheckSource('TestIntegerRange',
  7701. LinesToStr([
  7702. 'this.MinInt = -1;',
  7703. 'this.MaxInt = +1;',
  7704. 'this.a = -1 + 1;',
  7705. 'this.b = 1 + 3;',
  7706. 'this.s1 = rtl.createSet(1);',
  7707. 'this.s2 = rtl.createSet(1, 2);',
  7708. 'this.s3 = rtl.createSet(null, 1, 3);',
  7709. 'this.s4 = rtl.createSet(null, -128, 127);',
  7710. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7711. 'this.s6 = 1 in this.s2;',
  7712. 'this.i = 0;',
  7713. 'this.i2 = 0;',
  7714. '']),
  7715. LinesToStr([
  7716. '$mod.i = $mod.i2;',
  7717. '$mod.i = -1;',
  7718. 'if ($mod.i === $mod.i2) ;',
  7719. '$mod.i = $mod.i2;',
  7720. '']));
  7721. end;
  7722. procedure TTestModule.TestIntegerTypecasts;
  7723. begin
  7724. StartProgram(false);
  7725. Add([
  7726. 'var',
  7727. ' i: nativeint;',
  7728. ' b: byte;',
  7729. ' sh: shortint;',
  7730. ' w: word;',
  7731. ' sm: smallint;',
  7732. ' lw: longword;',
  7733. ' li: longint;',
  7734. 'begin',
  7735. ' b:=byte(i);',
  7736. ' sh:=shortint(i);',
  7737. ' w:=word(i);',
  7738. ' sm:=smallint(i);',
  7739. ' lw:=longword(i);',
  7740. ' li:=longint(i);',
  7741. '']);
  7742. ConvertProgram;
  7743. CheckSource('TestIntegerTypecasts',
  7744. LinesToStr([
  7745. 'this.i = 0;',
  7746. 'this.b = 0;',
  7747. 'this.sh = 0;',
  7748. 'this.w = 0;',
  7749. 'this.sm = 0;',
  7750. 'this.lw = 0;',
  7751. 'this.li = 0;',
  7752. '']),
  7753. LinesToStr([
  7754. '$mod.b = $mod.i & 255;',
  7755. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7756. '$mod.w = $mod.i & 65535;',
  7757. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7758. '$mod.lw = $mod.i >>> 0;',
  7759. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7760. '']));
  7761. end;
  7762. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7763. begin
  7764. StartProgram(false);
  7765. Add([
  7766. 'var',
  7767. ' i,j: nativeint;',
  7768. 'begin',
  7769. ' i:=i shr 0;',
  7770. ' i:=i shr 1;',
  7771. ' i:=i shr 3;',
  7772. ' i:=i shr 54;',
  7773. ' i:=j shr i;',
  7774. '']);
  7775. ConvertProgram;
  7776. CheckResolverUnexpectedHints;
  7777. CheckSource('TestInteger_BitwiseShrNativeInt',
  7778. LinesToStr([
  7779. 'this.i = 0;',
  7780. 'this.j = 0;',
  7781. '']),
  7782. LinesToStr([
  7783. '$mod.i = $mod.i;',
  7784. '$mod.i = Math.floor($mod.i / 2);',
  7785. '$mod.i = Math.floor($mod.i / 8);',
  7786. '$mod.i = 0;',
  7787. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7788. '']));
  7789. end;
  7790. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7791. begin
  7792. StartProgram(false);
  7793. Add([
  7794. 'var',
  7795. ' i: nativeint;',
  7796. 'begin',
  7797. ' i:=i shl 0;',
  7798. ' i:=i shl 54;',
  7799. ' i:=123456789012 shl 1;',
  7800. ' i:=i shl 1;',
  7801. '']);
  7802. ConvertProgram;
  7803. CheckResolverUnexpectedHints;
  7804. CheckSource('TestInteger_BitwiseShrNativeInt',
  7805. LinesToStr([
  7806. 'this.i = 0;',
  7807. '']),
  7808. LinesToStr([
  7809. '$mod.i = $mod.i;',
  7810. '$mod.i = 0;',
  7811. '$mod.i = 246913578024;',
  7812. '$mod.i = rtl.shl($mod.i, 1);',
  7813. '']));
  7814. end;
  7815. procedure TTestModule.TestInteger_SystemFunc;
  7816. begin
  7817. StartProgram(true);
  7818. Add([
  7819. 'var',
  7820. ' i: byte;',
  7821. ' s: string;',
  7822. 'begin',
  7823. ' system.inc(i);',
  7824. ' system.str(i,s);',
  7825. ' s:=system.str(i);',
  7826. ' i:=system.low(i);',
  7827. ' i:=system.high(i);',
  7828. ' i:=system.pred(i);',
  7829. ' i:=system.succ(i);',
  7830. ' i:=system.ord(i);',
  7831. '']);
  7832. ConvertProgram;
  7833. CheckResolverUnexpectedHints;
  7834. CheckSource('TestInteger_SystemFunc',
  7835. LinesToStr([
  7836. 'this.i = 0;',
  7837. 'this.s = "";',
  7838. '']),
  7839. LinesToStr([
  7840. '$mod.i += 1;',
  7841. '$mod.s = "" + $mod.i;',
  7842. '$mod.s = "" + $mod.i;',
  7843. '$mod.i = 0;',
  7844. '$mod.i = 255;',
  7845. '$mod.i = $mod.i - 1;',
  7846. '$mod.i = $mod.i + 1;',
  7847. '$mod.i = $mod.i;',
  7848. '']));
  7849. end;
  7850. procedure TTestModule.TestInteger_AssignOutsideConst;
  7851. begin
  7852. StartProgram(false);
  7853. Add([
  7854. 'const',
  7855. ' MinInt = low(longint);',
  7856. ' MaxInt = high(longint);',
  7857. 'type',
  7858. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7859. 'var',
  7860. ' i: TMyInt;',
  7861. ' aByte: byte;',
  7862. ' aShortInt: shortint;',
  7863. ' aWord: word;',
  7864. ' aSmallInt: smallint;',
  7865. ' aLongWord: longword;',
  7866. ' aLongInt: longint;',
  7867. ' aNativeInt: nativeint;',
  7868. ' aNativeUInt: nativeuint;',
  7869. 'begin',
  7870. ' aByte:=$FF;',
  7871. ' aByte:=$100;',
  7872. ' aByte:=-1;',
  7873. ' aByte:=-127;',
  7874. ' aByte:=-128;',
  7875. ' aByte:=-254;',
  7876. ' aByte:=-255;',
  7877. ' aByte:=-256;',
  7878. ' aShortInt:=127;',
  7879. ' aShortInt:=128;',
  7880. ' aShortInt:=-128;',
  7881. ' aShortInt:=-129;',
  7882. ' aWord:=$ffff;',
  7883. ' aWord:=$10000;',
  7884. ' aWord:=-1;',
  7885. ' aWord:=-$ffff;',
  7886. ' aWord:=-$10000;',
  7887. ' aWord:=-$10001;',
  7888. ' aSmallInt:=$7fff;',
  7889. ' aSmallInt:=$8000;',
  7890. ' aSmallInt:=-$8000;',
  7891. ' aSmallInt:=-$8001;',
  7892. ' aLongWord:=$ffffffff;',
  7893. ' aLongWord:=$100000000;',
  7894. ' aLongWord:=-1;',
  7895. ' aLongWord:=-$ffffffff;',
  7896. ' aNativeInt:=$1fffffffffffff;',
  7897. ' aNativeInt:=-$1fffffffffffff;',
  7898. ' aNativeUInt:=$1fffffffffffff;',
  7899. ' aNativeUInt:=-$1fffffffffffff;',
  7900. '']);
  7901. ConvertProgram;
  7902. CheckSource('TestInteger_AssignOutsideConst',
  7903. LinesToStr([
  7904. 'this.MinInt = -2147483648;',
  7905. 'this.MaxInt = 2147483647;',
  7906. 'this.i = 0;',
  7907. 'this.aByte = 0;',
  7908. 'this.aShortInt = 0;',
  7909. 'this.aWord = 0;',
  7910. 'this.aSmallInt = 0;',
  7911. 'this.aLongWord = 0;',
  7912. 'this.aLongInt = 0;',
  7913. 'this.aNativeInt = 0;',
  7914. 'this.aNativeUInt = 0;',
  7915. '']),
  7916. LinesToStr([
  7917. '$mod.aByte = 0xFF;',
  7918. '$mod.aByte = 0;',
  7919. '$mod.aByte = 255;',
  7920. '$mod.aByte = 129;',
  7921. '$mod.aByte = 128;',
  7922. '$mod.aByte = 2;',
  7923. '$mod.aByte = 1;',
  7924. '$mod.aByte = 0;',
  7925. '$mod.aShortInt = 127;',
  7926. '$mod.aShortInt = -128;',
  7927. '$mod.aShortInt = -128;',
  7928. '$mod.aShortInt = 127;',
  7929. '$mod.aWord = 0xffff;',
  7930. '$mod.aWord = 0;',
  7931. '$mod.aWord = 65535;',
  7932. '$mod.aWord = 1;',
  7933. '$mod.aWord = 0;',
  7934. '$mod.aWord = 65535;',
  7935. '$mod.aSmallInt = 0x7fff;',
  7936. '$mod.aSmallInt = -32768;',
  7937. '$mod.aSmallInt = -0x8000;',
  7938. '$mod.aSmallInt = 32767;',
  7939. '$mod.aLongWord = 0xffffffff;',
  7940. '$mod.aLongWord = 0;',
  7941. '$mod.aLongWord = 4294967295;',
  7942. '$mod.aLongWord = 1;',
  7943. '$mod.aNativeInt = 0x1fffffffffffff;',
  7944. '$mod.aNativeInt = -0x1fffffffffffff;',
  7945. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7946. '$mod.aNativeUInt = 1;',
  7947. '']));
  7948. end;
  7949. procedure TTestModule.TestCurrency;
  7950. begin
  7951. StartProgram(false);
  7952. Add([
  7953. 'type',
  7954. ' TCoin = currency;',
  7955. 'const',
  7956. ' a = TCoin(2.7);',
  7957. ' b = a + TCoin(1.7);',
  7958. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7959. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7960. 'var',
  7961. ' c: TCoin = b;',
  7962. ' i: nativeint;',
  7963. ' d: double;',
  7964. ' j: jsvalue;',
  7965. 'function DoIt(c: currency): currency; begin end;',
  7966. 'function GetIt(d: double): double; begin end;',
  7967. 'procedure Write(v: jsvalue); begin end;',
  7968. 'begin',
  7969. ' c:=1.0;',
  7970. ' c:=0.1;',
  7971. ' c:=1.0/3.0;',
  7972. ' c:=1/3;',
  7973. ' c:=a;',
  7974. ' d:=c;',
  7975. ' c:=d;',
  7976. ' c:=currency(c);',
  7977. ' c:=currency(d);',
  7978. ' d:=double(c);',
  7979. ' c:=i;',
  7980. ' c:=currency(i);',
  7981. //' i:=c;', not allowed
  7982. ' i:=nativeint(c);',
  7983. ' c:=c+a;',
  7984. ' c:=-c-a;',
  7985. ' c:=d+c;',
  7986. ' c:=c+d;',
  7987. ' c:=d-c;',
  7988. ' c:=c-d;',
  7989. ' c:=c*a;',
  7990. ' c:=a*c;',
  7991. ' c:=d*c;',
  7992. ' c:=c*d;',
  7993. ' c:=c/a;',
  7994. ' c:=a/c;',
  7995. ' c:=d/c;',
  7996. ' c:=c/d;',
  7997. ' c:=c**a;',
  7998. ' c:=a**c;',
  7999. ' c:=d**c;',
  8000. ' c:=c**d;',
  8001. ' if c=c then ;',
  8002. ' if c=a then ;',
  8003. ' if a=c then ;',
  8004. ' if d=c then ;',
  8005. ' if c=d then ;',
  8006. ' c:=DoIt(c);',
  8007. ' c:=DoIt(i);',
  8008. ' c:=DoIt(d);',
  8009. ' c:=GetIt(c);',
  8010. ' j:=c;',
  8011. ' Write(c);',
  8012. ' c:=default(currency);',
  8013. ' j:=str(c);',
  8014. ' j:=str(c:0:3);',
  8015. '']);
  8016. ConvertProgram;
  8017. CheckSource('TestCurrency',
  8018. LinesToStr([
  8019. 'this.a = 27000;',
  8020. 'this.b = this.a + 17000;',
  8021. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8022. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8023. 'this.c = this.b;',
  8024. 'this.i = 0;',
  8025. 'this.d = 0.0;',
  8026. 'this.j = undefined;',
  8027. 'this.DoIt = function (c) {',
  8028. ' var Result = 0;',
  8029. ' return Result;',
  8030. '};',
  8031. 'this.GetIt = function (d) {',
  8032. ' var Result = 0.0;',
  8033. ' return Result;',
  8034. '};',
  8035. 'this.Write = function (v) {',
  8036. '};',
  8037. '']),
  8038. LinesToStr([
  8039. '$mod.c = 10000;',
  8040. '$mod.c = 1000;',
  8041. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8042. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8043. '$mod.c = $mod.a;',
  8044. '$mod.d = $mod.c / 10000;',
  8045. '$mod.c = rtl.trunc($mod.d * 10000);',
  8046. '$mod.c = $mod.c;',
  8047. '$mod.c = $mod.d * 10000;',
  8048. '$mod.d = $mod.c / 10000;',
  8049. '$mod.c = $mod.i * 10000;',
  8050. '$mod.c = $mod.i * 10000;',
  8051. '$mod.i = rtl.trunc($mod.c / 10000);',
  8052. '$mod.c = $mod.c + $mod.a;',
  8053. '$mod.c = -$mod.c - $mod.a;',
  8054. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8055. '$mod.c = $mod.c + ($mod.d * 10000);',
  8056. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8057. '$mod.c = $mod.c - ($mod.d * 10000);',
  8058. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8059. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8060. '$mod.c = $mod.d * $mod.c;',
  8061. '$mod.c = $mod.c * $mod.d;',
  8062. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8063. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8064. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8065. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8066. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8067. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8068. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8069. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8070. 'if ($mod.c === $mod.c) ;',
  8071. 'if ($mod.c === $mod.a) ;',
  8072. 'if ($mod.a === $mod.c) ;',
  8073. 'if (($mod.d * 10000) === $mod.c) ;',
  8074. 'if ($mod.c === ($mod.d * 10000)) ;',
  8075. '$mod.c = $mod.DoIt($mod.c);',
  8076. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8077. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8078. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8079. '$mod.j = $mod.c / 10000;',
  8080. '$mod.Write($mod.c / 10000);',
  8081. '$mod.c = 0;',
  8082. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8083. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8084. '']));
  8085. end;
  8086. procedure TTestModule.TestForBoolDo;
  8087. begin
  8088. StartProgram(false);
  8089. Add([
  8090. 'var b: boolean;',
  8091. 'begin',
  8092. ' for b:=false to true do ;',
  8093. ' for b:=b downto false do ;',
  8094. ' for b in boolean do ;',
  8095. '']);
  8096. ConvertProgram;
  8097. CheckSource('TestForBoolDo',
  8098. LinesToStr([ // statements
  8099. 'this.b = false;']),
  8100. LinesToStr([ // this.$main
  8101. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8102. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8103. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8104. '']));
  8105. end;
  8106. procedure TTestModule.TestForIntDo;
  8107. begin
  8108. StartProgram(false);
  8109. Add([
  8110. 'var i: longint;',
  8111. 'begin',
  8112. ' for i:=3 to 5 do ;',
  8113. ' for i:=i downto 2 do ;',
  8114. ' for i in byte do ;',
  8115. '']);
  8116. ConvertProgram;
  8117. CheckSource('TestForIntDo',
  8118. LinesToStr([ // statements
  8119. 'this.i = 0;']),
  8120. LinesToStr([ // this.$main
  8121. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8122. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8123. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8124. '']));
  8125. end;
  8126. procedure TTestModule.TestForIntInDo;
  8127. begin
  8128. StartProgram(false);
  8129. Add([
  8130. 'type',
  8131. ' TSetOfInt = set of byte;',
  8132. ' TIntRg = 3..7;',
  8133. ' TSetOfIntRg = set of TIntRg;',
  8134. 'var',
  8135. ' i,i2: longint;',
  8136. ' a1: array of byte;',
  8137. ' a2: array[1..3] of byte;',
  8138. ' soi: TSetOfInt;',
  8139. ' soir: TSetOfIntRg;',
  8140. ' ir: TIntRg;',
  8141. 'begin',
  8142. ' for i in byte do ;',
  8143. ' for i in a1 do ;',
  8144. ' for i in a2 do ;',
  8145. ' for i in [11..13] do ;',
  8146. ' for i in TSetOfInt do ;',
  8147. ' for i in TIntRg do ;',
  8148. ' for i in soi do i2:=i;',
  8149. ' for i in TSetOfIntRg do ;',
  8150. ' for i in soir do ;',
  8151. ' for ir in TIntRg do ;',
  8152. ' for ir in TSetOfIntRg do ;',
  8153. ' for ir in soir do ;',
  8154. '']);
  8155. ConvertProgram;
  8156. CheckSource('TestForIntInDo',
  8157. LinesToStr([ // statements
  8158. 'this.i = 0;',
  8159. 'this.i2 = 0;',
  8160. 'this.a1 = [];',
  8161. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8162. 'this.soi = {};',
  8163. 'this.soir = {};',
  8164. 'this.ir = 0;',
  8165. '']),
  8166. LinesToStr([ // this.$main
  8167. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8168. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8169. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8170. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8171. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8172. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8173. 'for (var $l6 in $mod.soi) {',
  8174. ' $mod.i = +$l6;',
  8175. ' $mod.i2 = $mod.i;',
  8176. '};',
  8177. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8178. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8179. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8180. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8181. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8182. '']));
  8183. end;
  8184. procedure TTestModule.TestCharConst;
  8185. begin
  8186. StartProgram(false);
  8187. Add([
  8188. 'const',
  8189. ' a = #$00F3;',
  8190. ' c: char = ''1'';',
  8191. ' wc: widechar = ''ä'';',
  8192. 'begin',
  8193. ' c:=#0;',
  8194. ' c:=#1;',
  8195. ' c:=#9;',
  8196. ' c:=#10;',
  8197. ' c:=#13;',
  8198. ' c:=#31;',
  8199. ' c:=#32;',
  8200. ' c:=#$A;',
  8201. ' c:=#$0A;',
  8202. ' c:=#$b;',
  8203. ' c:=#$0b;',
  8204. ' c:=^A;',
  8205. ' c:=''"'';',
  8206. ' c:=default(char);',
  8207. ' c:=#$00E4;', // ä
  8208. ' c:=''ä'';',
  8209. ' c:=#$E4;', // ä
  8210. ' c:=#$D800;', // invalid UTF-16
  8211. ' c:=#$DFFF;', // invalid UTF-16
  8212. ' c:=#$FFFF;', // last UCS-2
  8213. ' c:=high(c);', // last UCS-2
  8214. ' c:=#269;',
  8215. '']);
  8216. ConvertProgram;
  8217. CheckSource('TestCharConst',
  8218. LinesToStr([
  8219. 'this.a="ó";',
  8220. 'this.c="1";',
  8221. 'this.wc="ä";'
  8222. ]),
  8223. LinesToStr([
  8224. '$mod.c="\x00";',
  8225. '$mod.c="\x01";',
  8226. '$mod.c="\t";',
  8227. '$mod.c="\n";',
  8228. '$mod.c="\r";',
  8229. '$mod.c="\x1F";',
  8230. '$mod.c=" ";',
  8231. '$mod.c="\n";',
  8232. '$mod.c="\n";',
  8233. '$mod.c="\x0B";',
  8234. '$mod.c="\x0B";',
  8235. '$mod.c="\x01";',
  8236. '$mod.c=''"'';',
  8237. '$mod.c="\x00";',
  8238. '$mod.c = "ä";',
  8239. '$mod.c = "ä";',
  8240. '$mod.c = "ä";',
  8241. '$mod.c="\uD800";',
  8242. '$mod.c="\uDFFF";',
  8243. '$mod.c="\uFFFF";',
  8244. '$mod.c="\uFFFF";',
  8245. '$mod.c = "č";',
  8246. '']));
  8247. end;
  8248. procedure TTestModule.TestChar_Compare;
  8249. begin
  8250. StartProgram(false);
  8251. Add('var');
  8252. Add(' c: char;');
  8253. Add(' b: boolean;');
  8254. Add('begin');
  8255. Add(' b:=c=''1'';');
  8256. Add(' b:=''2''=c;');
  8257. Add(' b:=''3''=''4'';');
  8258. Add(' b:=c<>''5'';');
  8259. Add(' b:=''6''<>c;');
  8260. Add(' b:=c>''7'';');
  8261. Add(' b:=''8''>c;');
  8262. Add(' b:=c>=''9'';');
  8263. Add(' b:=''A''>=c;');
  8264. Add(' b:=c<''B'';');
  8265. Add(' b:=''C''<c;');
  8266. Add(' b:=c<=''D'';');
  8267. Add(' b:=''E''<=c;');
  8268. ConvertProgram;
  8269. CheckSource('TestChar_Compare',
  8270. LinesToStr([
  8271. 'this.c="";',
  8272. 'this.b = false;'
  8273. ]),
  8274. LinesToStr([
  8275. '$mod.b = $mod.c === "1";',
  8276. '$mod.b = "2" === $mod.c;',
  8277. '$mod.b = "3" === "4";',
  8278. '$mod.b = $mod.c !== "5";',
  8279. '$mod.b = "6" !== $mod.c;',
  8280. '$mod.b = $mod.c > "7";',
  8281. '$mod.b = "8" > $mod.c;',
  8282. '$mod.b = $mod.c >= "9";',
  8283. '$mod.b = "A" >= $mod.c;',
  8284. '$mod.b = $mod.c < "B";',
  8285. '$mod.b = "C" < $mod.c;',
  8286. '$mod.b = $mod.c <= "D";',
  8287. '$mod.b = "E" <= $mod.c;',
  8288. '']));
  8289. end;
  8290. procedure TTestModule.TestChar_BuiltInProcs;
  8291. begin
  8292. StartProgram(false);
  8293. Add([
  8294. 'var',
  8295. ' c: char;',
  8296. ' i: longint;',
  8297. ' s: string;',
  8298. 'begin',
  8299. ' i:=ord(c);',
  8300. ' i:=ord(s[i]);',
  8301. ' c:=chr(i);',
  8302. ' c:=pred(c);',
  8303. ' c:=succ(c);',
  8304. ' c:=low(c);',
  8305. ' c:=high(c);',
  8306. ' i:=byte(c);',
  8307. ' i:=word(c);',
  8308. ' i:=longint(c);',
  8309. '']);
  8310. ConvertProgram;
  8311. CheckSource('TestChar_BuiltInProcs',
  8312. LinesToStr([
  8313. 'this.c = "";',
  8314. 'this.i = 0;',
  8315. 'this.s = "";'
  8316. ]),
  8317. LinesToStr([
  8318. '$mod.i = $mod.c.charCodeAt();',
  8319. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8320. '$mod.c = String.fromCharCode($mod.i);',
  8321. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8322. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8323. '$mod.c = "\x00";',
  8324. '$mod.c = "\uFFFF";',
  8325. '$mod.i = $mod.c.charCodeAt() & 255;',
  8326. '$mod.i = $mod.c.charCodeAt();',
  8327. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8328. '']));
  8329. end;
  8330. procedure TTestModule.TestStringConst;
  8331. begin
  8332. StartProgram(false);
  8333. Add([
  8334. '{$H+}',
  8335. 'const',
  8336. ' a = #$00F3#$017C;', // first <256, then >=256
  8337. ' b = string(''a'');',
  8338. ' c = string(''ä'');',
  8339. ' d = UnicodeString(''b'');',
  8340. ' e = UnicodeString(''ö'');',
  8341. ' f = low(a)+high(b);',
  8342. ' g: word = low(a);',
  8343. 'var',
  8344. ' s: string = ''abc'';',
  8345. ' i: longint;',
  8346. 'begin',
  8347. ' s:='''';',
  8348. ' s:=#13#10;',
  8349. ' s:=#9''foo'';',
  8350. ' s:=#$A9;',
  8351. ' s:=''foo''#13''bar'';',
  8352. ' s:=''"'';',
  8353. ' s:=''"''''"'';',
  8354. ' s:=#$20AC;', // euro
  8355. ' s:=#$10437;', // outside BMP
  8356. ' s:=''abc''#$20AC;', // ascii,#
  8357. ' s:=''ä''#$20AC;', // non ascii,#
  8358. ' s:=#$20AC''abc'';', // #, ascii
  8359. ' s:=#$20AC''ä'';', // #, non ascii
  8360. ' s:=default(string);',
  8361. ' s:=concat(s);',
  8362. ' s:=concat(s,''a'',s);',
  8363. ' s:=#250#269;',
  8364. ' i:=low(s)+high(a);',
  8365. ' s:=''a/b'';',
  8366. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8367. '']);
  8368. ConvertProgram;
  8369. CheckSource('TestStringConst',
  8370. LinesToStr([
  8371. 'this.a = "óż";',
  8372. 'this.b = "a";',
  8373. 'this.c = "ä";',
  8374. 'this.d = "b";',
  8375. 'this.e = "ö";',
  8376. 'this.f = 1 + this.b.length;',
  8377. 'this.g = 1;',
  8378. 'this.s="abc";',
  8379. 'this.i = 0;',
  8380. '']),
  8381. LinesToStr([
  8382. '$mod.s="";',
  8383. '$mod.s="\r\n";',
  8384. '$mod.s="\tfoo";',
  8385. '$mod.s="©";',
  8386. '$mod.s="foo\rbar";',
  8387. '$mod.s=''"'';',
  8388. '$mod.s=''"\''"'';',
  8389. '$mod.s="€";',
  8390. '$mod.s="'#$F0#$90#$90#$B7'";',
  8391. '$mod.s = "abc€";',
  8392. '$mod.s = "ä€";',
  8393. '$mod.s = "€abc";',
  8394. '$mod.s = "ۊ";',
  8395. '$mod.s="";',
  8396. '$mod.s = $mod.s;',
  8397. '$mod.s = $mod.s.concat("a", $mod.s);',
  8398. '$mod.s = "úč";',
  8399. '$mod.i = 1 + $mod.a.length;',
  8400. '$mod.s = "a/b";',
  8401. '']));
  8402. end;
  8403. procedure TTestModule.TestStringConst_InvalidUTF16;
  8404. begin
  8405. StartProgram(false);
  8406. Add([
  8407. 'const',
  8408. ' a: char = #$D87E;',
  8409. ' b: string = #$D87E;',
  8410. ' c: string = #$D87E#43;',
  8411. 'begin',
  8412. ' c:=''abc''#$D87E;',
  8413. ' c:=#0#1#2;',
  8414. ' c:=#127;',
  8415. ' c:=#128;',
  8416. ' c:=#255;',
  8417. ' c:=#256;',
  8418. '']);
  8419. ConvertProgram;
  8420. CheckSource('TestStringConst',
  8421. LinesToStr([
  8422. 'this.a = "\uD87E";',
  8423. 'this.b = "\uD87E";',
  8424. 'this.c = "\uD87E+";',
  8425. '']),
  8426. LinesToStr([
  8427. '$mod.c = "abc\uD87E";',
  8428. '$mod.c = "\x00\x01\x02";',
  8429. '$mod.c = "'#127'";',
  8430. '$mod.c = "'#$c2#$80'";',
  8431. '$mod.c = "'#$c3#$BF'";',
  8432. '$mod.c = "'#$c4#$80'";',
  8433. '']));
  8434. end;
  8435. procedure TTestModule.TestStringConstSurrogate;
  8436. begin
  8437. StartProgram(false);
  8438. Add([
  8439. 'var',
  8440. ' s: string;',
  8441. 'begin',
  8442. ' s:=''😊'';', // 1F60A
  8443. '']);
  8444. ConvertProgram;
  8445. CheckSource('TestStringConstSurrogate',
  8446. LinesToStr([
  8447. 'this.s="";'
  8448. ]),
  8449. LinesToStr([
  8450. '$mod.s="😊";'
  8451. ]));
  8452. end;
  8453. procedure TTestModule.TestStringConst_Multiline;
  8454. begin
  8455. StartProgram(false);
  8456. Add([
  8457. '{$modeswitch multilinestrings}',
  8458. 'const',
  8459. ' a = ``;',
  8460. ' b = `',
  8461. 'line`;',
  8462. ' c = `Single`;',
  8463. ' d = ````;',
  8464. ' e = `abc``xyz`;',
  8465. ' f = `first''line',
  8466. ' second''line`#10;',
  8467. 'begin',
  8468. '']);
  8469. ConvertProgram;
  8470. CheckSource('TestStringConst_Multiline',
  8471. LinesToStr([
  8472. 'this.a = "";',
  8473. 'this.b = "\nline";',
  8474. 'this.c = "Single";',
  8475. 'this.d = "`";',
  8476. 'this.e = "abc`xyz";',
  8477. 'this.f = "first''line\n second''line\n";',
  8478. '']),
  8479. LinesToStr([
  8480. ]));
  8481. end;
  8482. procedure TTestModule.TestString_Length;
  8483. begin
  8484. StartProgram(false);
  8485. Add('const c = ''foo'';');
  8486. Add('var');
  8487. Add(' s: string;');
  8488. Add(' i: longint;');
  8489. Add('begin');
  8490. Add(' i:=length(s);');
  8491. Add(' i:=length(s+s);');
  8492. Add(' i:=length(''abc'');');
  8493. Add(' i:=length(c);');
  8494. ConvertProgram;
  8495. CheckSource('TestString_Length',
  8496. LinesToStr([
  8497. 'this.c = "foo";',
  8498. 'this.s = "";',
  8499. 'this.i = 0;',
  8500. '']),
  8501. LinesToStr([
  8502. '$mod.i = $mod.s.length;',
  8503. '$mod.i = ($mod.s+$mod.s).length;',
  8504. '$mod.i = "abc".length;',
  8505. '$mod.i = $mod.c.length;',
  8506. '']));
  8507. end;
  8508. procedure TTestModule.TestString_Compare;
  8509. begin
  8510. StartProgram(false);
  8511. Add('var');
  8512. Add(' s, t: string;');
  8513. Add(' b: boolean;');
  8514. Add('begin');
  8515. Add(' b:=s=t;');
  8516. Add(' b:=s<>t;');
  8517. Add(' b:=s>t;');
  8518. Add(' b:=s>=t;');
  8519. Add(' b:=s<t;');
  8520. Add(' b:=s<=t;');
  8521. ConvertProgram;
  8522. CheckSource('TestString_Compare',
  8523. LinesToStr([ // statements
  8524. 'this.s = "";',
  8525. 'this.t = "";',
  8526. 'this.b =false;'
  8527. ]),
  8528. LinesToStr([ // this.$main
  8529. '$mod.b = $mod.s === $mod.t;',
  8530. '$mod.b = $mod.s !== $mod.t;',
  8531. '$mod.b = $mod.s > $mod.t;',
  8532. '$mod.b = $mod.s >= $mod.t;',
  8533. '$mod.b = $mod.s < $mod.t;',
  8534. '$mod.b = $mod.s <= $mod.t;',
  8535. '']));
  8536. end;
  8537. procedure TTestModule.TestString_SetLength;
  8538. begin
  8539. StartProgram(false);
  8540. Add([
  8541. 'procedure Fly(var s: string);',
  8542. 'begin',
  8543. ' SetLength(s,1);',
  8544. 'end;',
  8545. 'procedure Run(var s: unicodestring);',
  8546. 'begin',
  8547. ' SetLength(s,2);',
  8548. 'end;',
  8549. 'var s: string;',
  8550. ' u: unicodestring;',
  8551. 'begin',
  8552. ' SetLength(s,3);',
  8553. ' SetLength(u,4);',
  8554. '']);
  8555. ConvertProgram;
  8556. CheckSource('TestString_SetLength',
  8557. LinesToStr([ // statements
  8558. 'this.Fly = function (s) {',
  8559. ' s.set(rtl.strSetLength(s.get(), 1));',
  8560. '};',
  8561. 'this.Run = function (s) {',
  8562. ' s.set(rtl.strSetLength(s.get(), 2));',
  8563. '};',
  8564. 'this.s = "";',
  8565. 'this.u = "";',
  8566. '']),
  8567. LinesToStr([ // this.$main
  8568. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8569. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8570. ]));
  8571. end;
  8572. procedure TTestModule.TestString_CharAt;
  8573. begin
  8574. StartProgram(false);
  8575. Add([
  8576. 'var',
  8577. ' s: string;',
  8578. ' c: char;',
  8579. ' b: boolean;',
  8580. 'begin',
  8581. ' b:= s[1] = c;',
  8582. ' b:= c = s[1];',
  8583. ' b:= c <> s[1];',
  8584. ' b:= c > s[1];',
  8585. ' b:= c >= s[1];',
  8586. ' b:= c < s[2];',
  8587. ' b:= c <= s[1];',
  8588. ' s[1] := c;',
  8589. ' s[2+3] := c;']);
  8590. ConvertProgram;
  8591. CheckSource('TestString_CharAt',
  8592. LinesToStr([ // statements
  8593. 'this.s = "";',
  8594. 'this.c = "";',
  8595. 'this.b = false;'
  8596. ]),
  8597. LinesToStr([ // this.$main
  8598. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8599. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8600. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8601. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8602. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8603. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8604. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8605. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8606. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8607. '']));
  8608. end;
  8609. procedure TTestModule.TestStringHMinusFail;
  8610. begin
  8611. StartProgram(false);
  8612. Add([
  8613. '{$H-}',
  8614. 'var s: string;',
  8615. 'begin']);
  8616. ConvertProgram;
  8617. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8618. end;
  8619. procedure TTestModule.TestStr;
  8620. begin
  8621. StartProgram(false);
  8622. Add('var');
  8623. Add(' b: boolean;');
  8624. Add(' i: longint;');
  8625. Add(' d: double;');
  8626. Add(' s: string;');
  8627. Add('begin');
  8628. Add(' str(b,s);');
  8629. Add(' str(i,s);');
  8630. Add(' str(d,s);');
  8631. Add(' str(i:3,s);');
  8632. Add(' str(d:3:2,s);');
  8633. Add(' Str(12.456:12:1,s);');
  8634. Add(' Str(12.456:12,s);');
  8635. Add(' s:=str(b);');
  8636. Add(' s:=str(i);');
  8637. Add(' s:=str(d);');
  8638. Add(' s:=str(i,i);');
  8639. Add(' s:=str(i:3);');
  8640. Add(' s:=str(d:3:2);');
  8641. Add(' s:=str(i:4,i);');
  8642. Add(' s:=str(i,i:5);');
  8643. Add(' s:=str(i:4,i:5);');
  8644. Add(' s:=str(s,s);');
  8645. Add(' s:=str(s,''foo'');');
  8646. ConvertProgram;
  8647. CheckSource('TestStr',
  8648. LinesToStr([ // statements
  8649. 'this.b = false;',
  8650. 'this.i = 0;',
  8651. 'this.d = 0.0;',
  8652. 'this.s = "";',
  8653. '']),
  8654. LinesToStr([ // this.$main
  8655. '$mod.s = ""+$mod.b;',
  8656. '$mod.s = ""+$mod.i;',
  8657. '$mod.s = rtl.floatToStr($mod.d);',
  8658. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8659. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8660. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8661. '$mod.s = rtl.floatToStr(12.456,12);',
  8662. '$mod.s = ""+$mod.b;',
  8663. '$mod.s = ""+$mod.i;',
  8664. '$mod.s = rtl.floatToStr($mod.d);',
  8665. '$mod.s = ""+$mod.i+$mod.i;',
  8666. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8667. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8668. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8669. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8670. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8671. '$mod.s = $mod.s + $mod.s;',
  8672. '$mod.s = $mod.s + "foo";',
  8673. '']));
  8674. end;
  8675. procedure TTestModule.TestBaseType_AnsiStringFail;
  8676. begin
  8677. StartProgram(false);
  8678. Add('var s: AnsiString');
  8679. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8680. ConvertProgram;
  8681. end;
  8682. procedure TTestModule.TestBaseType_WideStringFail;
  8683. begin
  8684. StartProgram(false);
  8685. Add('var s: WideString');
  8686. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8687. ConvertProgram;
  8688. end;
  8689. procedure TTestModule.TestBaseType_ShortStringFail;
  8690. begin
  8691. StartProgram(false);
  8692. Add('var s: ShortString');
  8693. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8694. ConvertProgram;
  8695. end;
  8696. procedure TTestModule.TestBaseType_RawByteStringFail;
  8697. begin
  8698. StartProgram(false);
  8699. Add('var s: RawByteString');
  8700. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8701. ConvertProgram;
  8702. end;
  8703. procedure TTestModule.TestTypeShortstring_Fail;
  8704. begin
  8705. StartProgram(false);
  8706. Add('type t = string[12];');
  8707. Add('var s: t;');
  8708. Add('begin');
  8709. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8710. ConvertProgram;
  8711. end;
  8712. procedure TTestModule.TestCharSet_Custom;
  8713. begin
  8714. StartProgram(false);
  8715. Add([
  8716. 'type',
  8717. ' TCharRg = ''a''..''z'';',
  8718. ' TSetOfCharRg = set of TCharRg;',
  8719. ' TCharRg2 = ''m''..''p'';',
  8720. 'const',
  8721. ' crg: TCharRg = ''b'';',
  8722. 'var',
  8723. ' c: char;',
  8724. ' crg2: TCharRg2;',
  8725. ' s: TSetOfCharRg;',
  8726. 'begin',
  8727. ' c:=crg;',
  8728. ' crg:=c;',
  8729. ' crg2:=crg;',
  8730. ' if c=crg then ;',
  8731. ' if crg=c then ;',
  8732. ' if crg=crg2 then ;',
  8733. ' if c in s then ;',
  8734. ' if crg2 in s then ;',
  8735. ' c:=default(TCharRg);',
  8736. '']);
  8737. ConvertProgram;
  8738. CheckSource('TestCharSet_Custom',
  8739. LinesToStr([ // statements
  8740. 'this.crg = "b";',
  8741. 'this.c = "";',
  8742. 'this.crg2 = "m";',
  8743. 'this.s = {};',
  8744. '']),
  8745. LinesToStr([ // this.$main
  8746. '$mod.c = $mod.crg;',
  8747. '$mod.crg = $mod.c;',
  8748. '$mod.crg2 = $mod.crg;',
  8749. 'if ($mod.c === $mod.crg) ;',
  8750. 'if ($mod.crg === $mod.c) ;',
  8751. 'if ($mod.crg === $mod.crg2) ;',
  8752. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8753. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8754. '$mod.c = "a";',
  8755. '']));
  8756. end;
  8757. procedure TTestModule.TestWideChar;
  8758. begin
  8759. StartProgram(false);
  8760. Add([
  8761. 'procedure Fly(var c: char);',
  8762. 'begin',
  8763. 'end;',
  8764. 'procedure Run(var c: widechar);',
  8765. 'begin',
  8766. 'end;',
  8767. 'var',
  8768. ' c: char;',
  8769. ' wc: widechar;',
  8770. ' w: word;',
  8771. 'begin',
  8772. ' Fly(wc);',
  8773. ' Run(c);',
  8774. ' wc:=WideChar(w);',
  8775. ' w:=ord(wc);',
  8776. '']);
  8777. ConvertProgram;
  8778. CheckSource('TestWideChar_VarArg',
  8779. LinesToStr([ // statements
  8780. 'this.Fly = function (c) {',
  8781. '};',
  8782. 'this.Run = function (c) {',
  8783. '};',
  8784. 'this.c = "";',
  8785. 'this.wc = "";',
  8786. 'this.w = 0;',
  8787. '']),
  8788. LinesToStr([ // this.$main
  8789. '$mod.Fly({',
  8790. ' p: $mod,',
  8791. ' get: function () {',
  8792. ' return this.p.wc;',
  8793. ' },',
  8794. ' set: function (v) {',
  8795. ' this.p.wc = v;',
  8796. ' }',
  8797. '});',
  8798. '$mod.Run({',
  8799. ' p: $mod,',
  8800. ' get: function () {',
  8801. ' return this.p.c;',
  8802. ' },',
  8803. ' set: function (v) {',
  8804. ' this.p.c = v;',
  8805. ' }',
  8806. '});',
  8807. '$mod.wc = String.fromCharCode($mod.w);',
  8808. '$mod.w = $mod.wc.charCodeAt();',
  8809. '',
  8810. '']));
  8811. end;
  8812. procedure TTestModule.TestForCharDo;
  8813. begin
  8814. StartProgram(false);
  8815. Add([
  8816. 'var c: char;',
  8817. 'begin',
  8818. ' for c:=''a'' to ''c'' do ;',
  8819. ' for c:=c downto ''a'' do ;',
  8820. ' for c:=''Б'' to ''Я'' do ;',
  8821. '']);
  8822. ConvertProgram;
  8823. CheckSource('TestForCharDo',
  8824. LinesToStr([ // statements
  8825. 'this.c = "";']),
  8826. LinesToStr([ // this.$main
  8827. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8828. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8829. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8830. '']));
  8831. end;
  8832. procedure TTestModule.TestForCharInDo;
  8833. begin
  8834. StartProgram(false);
  8835. Add([
  8836. 'type',
  8837. ' TSetOfChar = set of char;',
  8838. ' TCharRg = ''a''..''z'';',
  8839. ' TSetOfCharRg = set of TCharRg;',
  8840. 'const Foo = ''foo'';',
  8841. 'var',
  8842. ' c,c2: char;',
  8843. ' s: string;',
  8844. ' a1: array of char;',
  8845. ' a2: array[1..3] of char;',
  8846. ' soc: TSetOfChar;',
  8847. ' socr: TSetOfCharRg;',
  8848. ' cr: TCharRg;',
  8849. 'begin',
  8850. ' for c in foo do ;',
  8851. ' for c in s do ;',
  8852. ' for c in char do ;',
  8853. ' for c in a1 do ;',
  8854. ' for c in a2 do ;',
  8855. ' for c in [''1''..''3''] do ;',
  8856. ' for c in TSetOfChar do ;',
  8857. ' for c in TCharRg do ;',
  8858. ' for c in soc do c2:=c;',
  8859. ' for c in TSetOfCharRg do ;',
  8860. ' for c in socr do ;',
  8861. ' for cr in TCharRg do ;',
  8862. ' for cr in TSetOfCharRg do ;',
  8863. ' for cr in socr do ;',
  8864. '']);
  8865. ConvertProgram;
  8866. CheckSource('TestForCharInDo',
  8867. LinesToStr([ // statements
  8868. 'this.Foo = "foo";',
  8869. 'this.c = "";',
  8870. 'this.c2 = "";',
  8871. 'this.s = "";',
  8872. 'this.a1 = [];',
  8873. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8874. 'this.soc = {};',
  8875. 'this.socr = {};',
  8876. 'this.cr = "a";',
  8877. '']),
  8878. LinesToStr([ // this.$main
  8879. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8880. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8881. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8882. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8883. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8884. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8885. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8886. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8887. 'for (var $l8 in $mod.soc) {',
  8888. ' $mod.c = String.fromCharCode($l8);',
  8889. ' $mod.c2 = $mod.c;',
  8890. '};',
  8891. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8892. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8893. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8894. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8895. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8896. '']));
  8897. end;
  8898. procedure TTestModule.TestProcTwoArgs;
  8899. begin
  8900. StartProgram(false);
  8901. Add('procedure Test(a,b: longint);');
  8902. Add('begin');
  8903. Add('end;');
  8904. Add('begin');
  8905. ConvertProgram;
  8906. CheckSource('TestProcTwoArgs',
  8907. LinesToStr([ // statements
  8908. 'this.Test = function (a,b) {',
  8909. '};'
  8910. ]),
  8911. LinesToStr([ // this.$main
  8912. ''
  8913. ]));
  8914. end;
  8915. procedure TTestModule.TestProc_DefaultValue;
  8916. begin
  8917. StartProgram(false);
  8918. Add('procedure p1(i: longint = 1);');
  8919. Add('begin');
  8920. Add('end;');
  8921. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8922. Add('begin');
  8923. Add('end;');
  8924. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8925. Add('begin');
  8926. Add('end;');
  8927. Add('begin');
  8928. Add(' p1;');
  8929. Add(' p1();');
  8930. Add(' p1(11);');
  8931. Add(' p2;');
  8932. Add(' p2();');
  8933. Add(' p2(12);');
  8934. Add(' p2(13,''b'');');
  8935. Add(' p3();');
  8936. ConvertProgram;
  8937. CheckSource('TestProc_DefaultValue',
  8938. LinesToStr([ // statements
  8939. 'this.p1 = function (i) {',
  8940. '};',
  8941. 'this.p2 = function (i,c) {',
  8942. '};',
  8943. 'this.p3 = function (d,b,s) {',
  8944. '};'
  8945. ]),
  8946. LinesToStr([ // this.$main
  8947. ' $mod.p1(1);',
  8948. ' $mod.p1(1);',
  8949. ' $mod.p1(11);',
  8950. ' $mod.p2(1,"a");',
  8951. ' $mod.p2(1,"a");',
  8952. ' $mod.p2(12,"a");',
  8953. ' $mod.p2(13,"b");',
  8954. ' $mod.p3(1.0,false,"abc");'
  8955. ]));
  8956. end;
  8957. procedure TTestModule.TestFunctionInt;
  8958. begin
  8959. StartProgram(false);
  8960. Add('function MyTest(Bar: longint): longint;');
  8961. Add('begin');
  8962. Add(' Result:=2*bar');
  8963. Add('end;');
  8964. Add('begin');
  8965. ConvertProgram;
  8966. CheckSource('TestFunctionInt',
  8967. LinesToStr([ // statements
  8968. 'this.MyTest = function (Bar) {',
  8969. ' var Result = 0;',
  8970. ' Result = 2*Bar;',
  8971. ' return Result;',
  8972. '};'
  8973. ]),
  8974. LinesToStr([ // this.$main
  8975. ''
  8976. ]));
  8977. end;
  8978. procedure TTestModule.TestFunctionString;
  8979. begin
  8980. StartProgram(false);
  8981. Add('function Test(Bar: string): string;');
  8982. Add('begin');
  8983. Add(' Result:=bar+BAR');
  8984. Add('end;');
  8985. Add('begin');
  8986. ConvertProgram;
  8987. CheckSource('TestFunctionString',
  8988. LinesToStr([ // statements
  8989. 'this.Test = function (Bar) {',
  8990. ' var Result = "";',
  8991. ' Result = Bar+Bar;',
  8992. ' return Result;',
  8993. '};'
  8994. ]),
  8995. LinesToStr([ // this.$main
  8996. ''
  8997. ]));
  8998. end;
  8999. procedure TTestModule.TestIfThen;
  9000. begin
  9001. StartProgram(false);
  9002. Add([
  9003. 'var b: boolean;',
  9004. 'begin',
  9005. ' if b then ;',
  9006. ' if b then else ;']);
  9007. ConvertProgram;
  9008. CheckSource('TestIfThen',
  9009. LinesToStr([ // statements
  9010. 'this.b = false;',
  9011. '']),
  9012. LinesToStr([ // this.$main
  9013. 'if ($mod.b) ;',
  9014. 'if ($mod.b) ;',
  9015. '']));
  9016. end;
  9017. procedure TTestModule.TestForLoop;
  9018. begin
  9019. StartProgram(false);
  9020. Add('var');
  9021. Add(' vI, vJ, vN: longint;');
  9022. Add('begin');
  9023. Add(' VJ:=0;');
  9024. Add(' VN:=3;');
  9025. Add(' for VI:=1 to VN do');
  9026. Add(' begin');
  9027. Add(' VJ:=VJ+VI;');
  9028. Add(' end;');
  9029. ConvertProgram;
  9030. CheckSource('TestForLoop',
  9031. LinesToStr([ // statements
  9032. 'this.vI = 0;',
  9033. 'this.vJ = 0;',
  9034. 'this.vN = 0;'
  9035. ]),
  9036. LinesToStr([ // this.$main
  9037. ' $mod.vJ = 0;',
  9038. ' $mod.vN = 3;',
  9039. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9040. ' $mod.vI = $l;',
  9041. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9042. ' };',
  9043. '']));
  9044. end;
  9045. procedure TTestModule.TestForLoopInsideFunction;
  9046. begin
  9047. StartProgram(false);
  9048. Add('function SumNumbers(Count: longint): longint;');
  9049. Add('var');
  9050. Add(' vI, vJ: longint;');
  9051. Add('begin');
  9052. Add(' vj:=0;');
  9053. Add(' for vi:=1 to count do');
  9054. Add(' begin');
  9055. Add(' vj:=vj+vi;');
  9056. Add(' end;');
  9057. Add('end;');
  9058. Add('begin');
  9059. Add(' sumnumbers(3);');
  9060. ConvertProgram;
  9061. CheckSource('TestForLoopInsideFunction',
  9062. LinesToStr([ // statements
  9063. 'this.SumNumbers = function (Count) {',
  9064. ' var Result = 0;',
  9065. ' var vI = 0;',
  9066. ' var vJ = 0;',
  9067. ' vJ = 0;',
  9068. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9069. ' vI = $l;',
  9070. ' vJ = vJ + vI;',
  9071. ' };',
  9072. ' return Result;',
  9073. '};'
  9074. ]),
  9075. LinesToStr([ // $mod.$main
  9076. ' $mod.SumNumbers(3);'
  9077. ]));
  9078. end;
  9079. procedure TTestModule.TestForLoop_ReadVarAfter;
  9080. begin
  9081. StartProgram(false);
  9082. Add('var');
  9083. Add(' vI: longint;');
  9084. Add('begin');
  9085. Add(' for vi:=1 to 2 do ;');
  9086. Add(' if vi=3 then ;');
  9087. ConvertProgram;
  9088. CheckSource('TestForLoop',
  9089. LinesToStr([ // statements
  9090. 'this.vI = 0;'
  9091. ]),
  9092. LinesToStr([ // this.$main
  9093. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9094. ' if ($mod.vI===3) ;'
  9095. ]));
  9096. end;
  9097. procedure TTestModule.TestForLoop_Nested;
  9098. begin
  9099. StartProgram(false);
  9100. Add('function SumNumbers(Count: longint): longint;');
  9101. Add('var');
  9102. Add(' vI, vJ, vK: longint;');
  9103. Add('begin');
  9104. Add(' VK:=0;');
  9105. Add(' for VI:=1 to count do');
  9106. Add(' begin');
  9107. Add(' for vj:=1 to vi do');
  9108. Add(' begin');
  9109. Add(' vk:=VK+VI;');
  9110. Add(' end;');
  9111. Add(' end;');
  9112. Add('end;');
  9113. Add('begin');
  9114. Add(' sumnumbers(3);');
  9115. ConvertProgram;
  9116. CheckSource('TestForLoopInFunction',
  9117. LinesToStr([ // statements
  9118. 'this.SumNumbers = function (Count) {',
  9119. ' var Result = 0;',
  9120. ' var vI = 0;',
  9121. ' var vJ = 0;',
  9122. ' var vK = 0;',
  9123. ' vK = 0;',
  9124. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9125. ' vI = $l;',
  9126. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9127. ' vJ = $l1;',
  9128. ' vK = vK + vI;',
  9129. ' };',
  9130. ' };',
  9131. ' return Result;',
  9132. '};'
  9133. ]),
  9134. LinesToStr([ // $mod.$main
  9135. ' $mod.SumNumbers(3);'
  9136. ]));
  9137. end;
  9138. procedure TTestModule.TestRepeatUntil;
  9139. begin
  9140. StartProgram(false);
  9141. Add('var');
  9142. Add(' vI, vJ, vN: longint;');
  9143. Add('begin');
  9144. Add(' vn:=3;');
  9145. Add(' vj:=0;');
  9146. Add(' VI:=0;');
  9147. Add(' repeat');
  9148. Add(' VI:=vi+1;');
  9149. Add(' vj:=VJ+vI;');
  9150. Add(' until vi>=vn');
  9151. ConvertProgram;
  9152. CheckSource('TestRepeatUntil',
  9153. LinesToStr([ // statements
  9154. 'this.vI = 0;',
  9155. 'this.vJ = 0;',
  9156. 'this.vN = 0;'
  9157. ]),
  9158. LinesToStr([ // $mod.$main
  9159. ' $mod.vN = 3;',
  9160. ' $mod.vJ = 0;',
  9161. ' $mod.vI = 0;',
  9162. ' do{',
  9163. ' $mod.vI = $mod.vI + 1;',
  9164. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9165. ' }while(!($mod.vI>=$mod.vN));'
  9166. ]));
  9167. end;
  9168. procedure TTestModule.TestAsmBlock;
  9169. begin
  9170. StartProgram(false);
  9171. Add([
  9172. 'var',
  9173. ' vI: longint;',
  9174. 'begin',
  9175. ' vi:=1;',
  9176. ' asm',
  9177. ' if (vI===1) {',
  9178. ' vI=2;',
  9179. //' console.log(''end;'');', ToDo
  9180. ' }',
  9181. ' if (vI===2){ vI=3; }',
  9182. ' end;',
  9183. ' VI:=4;']);
  9184. ConvertProgram;
  9185. CheckSource('TestAsmBlock',
  9186. LinesToStr([ // statements
  9187. 'this.vI = 0;'
  9188. ]),
  9189. LinesToStr([ // $mod.$main
  9190. '$mod.vI = 1;',
  9191. 'if (vI===1) {',
  9192. ' vI=2;',
  9193. '}',
  9194. 'if (vI===2){ vI=3; }',
  9195. ';',
  9196. '$mod.vI = 4;'
  9197. ]));
  9198. end;
  9199. procedure TTestModule.TestAsmPas_Impl;
  9200. begin
  9201. StartUnit(false);
  9202. Add('interface');
  9203. Add('const cIntf: longint = 1;');
  9204. Add('var vIntf: longint;');
  9205. Add('implementation');
  9206. Add('const cImpl: longint = 2;');
  9207. Add('var vImpl: longint;');
  9208. Add('procedure DoIt;');
  9209. Add('const cLoc: longint = 3;');
  9210. Add('var vLoc: longint;');
  9211. Add('begin;');
  9212. Add(' asm');
  9213. //Add(' pas(vIntf)=pas(cIntf);');
  9214. //Add(' pas(vImpl)=pas(cImpl);');
  9215. //Add(' pas(vLoc)=pas(cLoc);');
  9216. Add(' end;');
  9217. Add('end;');
  9218. ConvertUnit;
  9219. CheckSource('TestAsmPas_Impl',
  9220. LinesToStr([
  9221. 'var $impl = $mod.$impl;',
  9222. 'this.cIntf = 1;',
  9223. 'this.vIntf = 0;',
  9224. '']),
  9225. '', // this.$init
  9226. LinesToStr([ // implementation
  9227. '$impl.cImpl = 2;',
  9228. '$impl.vImpl = 0;',
  9229. 'var cLoc = 3;',
  9230. '$impl.DoIt = function () {',
  9231. ' var vLoc = 0;',
  9232. '};',
  9233. '']) );
  9234. end;
  9235. procedure TTestModule.TestTryFinally;
  9236. begin
  9237. StartProgram(false);
  9238. Add('var i: longint;');
  9239. Add('begin');
  9240. Add(' try');
  9241. Add(' i:=0; i:=2 div i;');
  9242. Add(' finally');
  9243. Add(' i:=3');
  9244. Add(' end;');
  9245. ConvertProgram;
  9246. CheckSource('TestTryFinally',
  9247. LinesToStr([ // statements
  9248. 'this.i = 0;'
  9249. ]),
  9250. LinesToStr([ // $mod.$main
  9251. 'try {',
  9252. ' $mod.i = 0;',
  9253. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9254. '} finally {',
  9255. ' $mod.i = 3;',
  9256. '};'
  9257. ]));
  9258. end;
  9259. procedure TTestModule.TestTryExcept;
  9260. begin
  9261. StartProgram(false);
  9262. Add([
  9263. 'type',
  9264. ' TObject = class end;',
  9265. ' Exception = class Msg: string; end;',
  9266. ' EInvalidCast = class(Exception) end;',
  9267. 'var vI: longint;',
  9268. 'begin',
  9269. ' try',
  9270. ' vi:=1;',
  9271. ' except',
  9272. ' vi:=2',
  9273. ' end;',
  9274. ' try',
  9275. ' vi:=3;',
  9276. ' except',
  9277. ' raise;',
  9278. ' end;',
  9279. ' try',
  9280. ' VI:=4;',
  9281. ' except',
  9282. ' on einvalidcast do',
  9283. ' raise;',
  9284. ' on E: exception do',
  9285. ' if e.msg='''' then',
  9286. ' raise e;',
  9287. ' else',
  9288. ' vi:=5',
  9289. ' end;',
  9290. ' try',
  9291. ' VI:=6;',
  9292. ' except',
  9293. ' on einvalidcast do ;',
  9294. ' end;',
  9295. '']);
  9296. ConvertProgram;
  9297. CheckSource('TestTryExcept',
  9298. LinesToStr([ // statements
  9299. 'rtl.createClass(this, "TObject", null, function () {',
  9300. ' this.$init = function () {',
  9301. ' };',
  9302. ' this.$final = function () {',
  9303. ' };',
  9304. '});',
  9305. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9306. ' this.$init = function () {',
  9307. ' $mod.TObject.$init.call(this);',
  9308. ' this.Msg = "";',
  9309. ' };',
  9310. '});',
  9311. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9312. '});',
  9313. 'this.vI = 0;'
  9314. ]),
  9315. LinesToStr([ // $mod.$main
  9316. 'try {',
  9317. ' $mod.vI = 1;',
  9318. '} catch ($e) {',
  9319. ' $mod.vI = 2;',
  9320. '};',
  9321. 'try {',
  9322. ' $mod.vI = 3;',
  9323. '} catch ($e) {',
  9324. ' throw $e;',
  9325. '};',
  9326. 'try {',
  9327. ' $mod.vI = 4;',
  9328. '} catch ($e) {',
  9329. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9330. ' throw $e',
  9331. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9332. ' var E = $e;',
  9333. ' if (E.Msg === "") throw E;',
  9334. ' } else {',
  9335. ' $mod.vI = 5;',
  9336. ' }',
  9337. '};',
  9338. 'try {',
  9339. ' $mod.vI = 6;',
  9340. '} catch ($e) {',
  9341. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9342. ' } else throw $e',
  9343. '};',
  9344. '']));
  9345. end;
  9346. procedure TTestModule.TestTryExcept_ReservedWords;
  9347. begin
  9348. StartProgram(false);
  9349. Add([
  9350. 'type',
  9351. ' TObject = class end;',
  9352. ' Exception = class',
  9353. ' Symbol: string;',
  9354. ' end;',
  9355. 'var &try: longint;',
  9356. 'begin',
  9357. ' try',
  9358. ' &try:=4;',
  9359. ' except',
  9360. ' on Error: exception do',
  9361. ' if errOR.symBol='''' then',
  9362. ' raise ERRor;',
  9363. ' end;',
  9364. '']);
  9365. ConvertProgram;
  9366. CheckSource('TestTryExcept_ReservedWords',
  9367. LinesToStr([ // statements
  9368. 'rtl.createClass(this, "TObject", null, function () {',
  9369. ' this.$init = function () {',
  9370. ' };',
  9371. ' this.$final = function () {',
  9372. ' };',
  9373. '});',
  9374. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9375. ' this.$init = function () {',
  9376. ' $mod.TObject.$init.call(this);',
  9377. ' this.Symbol = "";',
  9378. ' };',
  9379. '});',
  9380. 'this.Try = 0;',
  9381. '']),
  9382. LinesToStr([ // $mod.$main
  9383. 'try {',
  9384. ' $mod.Try = 4;',
  9385. '} catch ($e) {',
  9386. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9387. ' var error = $e;',
  9388. ' if (error.Symbol === "") throw error;',
  9389. ' } else throw $e',
  9390. '};',
  9391. '']));
  9392. end;
  9393. procedure TTestModule.TestIfThenRaiseElse;
  9394. begin
  9395. StartProgram(false);
  9396. Add([
  9397. 'type',
  9398. ' TObject = class',
  9399. ' constructor Create;',
  9400. ' end;',
  9401. 'constructor TObject.Create;',
  9402. 'begin',
  9403. 'end;',
  9404. 'var b: boolean;',
  9405. 'begin',
  9406. ' if b then',
  9407. ' raise TObject.Create',
  9408. ' else',
  9409. ' b:=false;',
  9410. '']);
  9411. ConvertProgram;
  9412. CheckSource('TestIfThenRaiseElse',
  9413. LinesToStr([ // statements
  9414. 'rtl.createClass(this, "TObject", null, function () {',
  9415. ' this.$init = function () {',
  9416. ' };',
  9417. ' this.$final = function () {',
  9418. ' };',
  9419. ' this.Create = function () {',
  9420. ' return this;',
  9421. ' };',
  9422. '});',
  9423. 'this.b = false;',
  9424. '']),
  9425. LinesToStr([ // $mod.$main
  9426. 'if ($mod.b) {',
  9427. ' throw $mod.TObject.$create("Create")}',
  9428. ' else $mod.b = false;',
  9429. '']));
  9430. end;
  9431. procedure TTestModule.TestCaseOf;
  9432. begin
  9433. StartProgram(false);
  9434. Add([
  9435. 'const e: longint; external name ''$e'';',
  9436. 'var vI: longint;',
  9437. 'begin',
  9438. ' case vi of',
  9439. ' 1: ;',
  9440. ' 2: vi:=3;',
  9441. ' e: ;',
  9442. ' else',
  9443. ' VI:=4',
  9444. ' end;']);
  9445. ConvertProgram;
  9446. CheckSource('TestCaseOf',
  9447. LinesToStr([ // statements
  9448. 'this.vI = 0;'
  9449. ]),
  9450. LinesToStr([ // $mod.$main
  9451. 'var $tmp = $mod.vI;',
  9452. 'if ($tmp === 1) {}',
  9453. 'else if ($tmp === 2) {',
  9454. ' $mod.vI = 3}',
  9455. ' else if ($tmp === $e) {}',
  9456. 'else {',
  9457. ' $mod.vI = 4;',
  9458. '};'
  9459. ]));
  9460. end;
  9461. procedure TTestModule.TestCaseOf_UseSwitch;
  9462. begin
  9463. StartProgram(false);
  9464. Converter.UseSwitchStatement:=true;
  9465. Add('var Vi: longint;');
  9466. Add('begin');
  9467. Add(' case vi of');
  9468. Add(' 1: ;');
  9469. Add(' 2: VI:=3;');
  9470. Add(' else');
  9471. Add(' vi:=4');
  9472. Add(' end;');
  9473. ConvertProgram;
  9474. CheckSource('TestCaseOf_UseSwitch',
  9475. LinesToStr([ // statements
  9476. 'this.Vi = 0;'
  9477. ]),
  9478. LinesToStr([ // $mod.$main
  9479. 'switch ($mod.Vi) {',
  9480. 'case 1:',
  9481. ' break;',
  9482. 'case 2:',
  9483. ' $mod.Vi = 3;',
  9484. ' break;',
  9485. 'default:',
  9486. ' $mod.Vi = 4;',
  9487. '};'
  9488. ]));
  9489. end;
  9490. procedure TTestModule.TestCaseOfNoElse;
  9491. begin
  9492. StartProgram(false);
  9493. Add('var Vi: longint;');
  9494. Add('begin');
  9495. Add(' case vi of');
  9496. Add(' 1: begin vi:=2; VI:=3; end;');
  9497. Add(' end;');
  9498. ConvertProgram;
  9499. CheckSource('TestCaseOfNoElse',
  9500. LinesToStr([ // statements
  9501. 'this.Vi = 0;'
  9502. ]),
  9503. LinesToStr([ // $mod.$main
  9504. 'var $tmp = $mod.Vi;',
  9505. 'if ($tmp === 1) {',
  9506. ' $mod.Vi = 2;',
  9507. ' $mod.Vi = 3;',
  9508. '};'
  9509. ]));
  9510. end;
  9511. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9512. begin
  9513. StartProgram(false);
  9514. Converter.UseSwitchStatement:=true;
  9515. Add('var vI: longint;');
  9516. Add('begin');
  9517. Add(' case vi of');
  9518. Add(' 1: begin VI:=2; vi:=3; end;');
  9519. Add(' end;');
  9520. ConvertProgram;
  9521. CheckSource('TestCaseOfNoElse_UseSwitch',
  9522. LinesToStr([ // statements
  9523. 'this.vI = 0;'
  9524. ]),
  9525. LinesToStr([ // $mod.$main
  9526. 'switch ($mod.vI) {',
  9527. 'case 1:',
  9528. ' $mod.vI = 2;',
  9529. ' $mod.vI = 3;',
  9530. ' break;',
  9531. '};'
  9532. ]));
  9533. end;
  9534. procedure TTestModule.TestCaseOfRange;
  9535. begin
  9536. StartProgram(false);
  9537. Add('var vI: longint;');
  9538. Add('begin');
  9539. Add(' case vi of');
  9540. Add(' 1..3: vi:=14;');
  9541. Add(' 4,5: vi:=16;');
  9542. Add(' 6..7,9..10: ;');
  9543. Add(' else ;');
  9544. Add(' end;');
  9545. ConvertProgram;
  9546. CheckSource('TestCaseOfRange',
  9547. LinesToStr([ // statements
  9548. 'this.vI = 0;'
  9549. ]),
  9550. LinesToStr([ // $mod.$main
  9551. 'var $tmp = $mod.vI;',
  9552. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9553. ' $mod.vI = 14',
  9554. '} else if (($tmp === 4) || ($tmp === 5)){',
  9555. ' $mod.vI = 16',
  9556. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9557. ]));
  9558. end;
  9559. procedure TTestModule.TestCaseOfString;
  9560. begin
  9561. StartProgram(false);
  9562. Add([
  9563. 'var s,h: string;',
  9564. 'begin',
  9565. ' case s of',
  9566. ' ''foo'': s:=h;',
  9567. ' ''a''..''z'': h:=s;',
  9568. ' ''ў'', ''ё'': ;',
  9569. ' ''Б''..''Я'': ;',
  9570. ' end;',
  9571. '']);
  9572. ConvertProgram;
  9573. CheckSource('TestCaseOfString',
  9574. LinesToStr([ // statements
  9575. 'this.s = "";',
  9576. 'this.h = "";',
  9577. '']),
  9578. LinesToStr([ // $mod.$main
  9579. 'var $tmp = $mod.s;',
  9580. 'if ($tmp === "foo") {',
  9581. ' $mod.s = $mod.h}',
  9582. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9583. ' $mod.h = $mod.s}',
  9584. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9585. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9586. '']));
  9587. end;
  9588. procedure TTestModule.TestCaseOfChar;
  9589. begin
  9590. StartProgram(false);
  9591. Add([
  9592. 'var s,h: char;',
  9593. 'begin',
  9594. ' case s of',
  9595. ' ''a''..''z'': h:=s;',
  9596. ' ''ä'': ;',
  9597. ' ''ў'', ''ё'': ;',
  9598. ' ''Б''..''Я'': ;',
  9599. ' end;',
  9600. '']);
  9601. ConvertProgram;
  9602. CheckSource('TestCaseOfString',
  9603. LinesToStr([ // statements
  9604. 'this.s = "";',
  9605. 'this.h = "";',
  9606. '']),
  9607. LinesToStr([ // $mod.$main
  9608. 'var $tmp = $mod.s;',
  9609. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9610. ' $mod.h = $mod.s}',
  9611. ' else if ($tmp === "ä") {}',
  9612. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9613. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9614. '']));
  9615. end;
  9616. procedure TTestModule.TestCaseOfExternalClassConst;
  9617. begin
  9618. StartProgram(false);
  9619. Add([
  9620. '{$modeswitch externalclass}',
  9621. 'type',
  9622. ' TBird = class external name ''Bird''',
  9623. ' const e: longint;',
  9624. ' end;',
  9625. 'var vI: longint;',
  9626. 'begin',
  9627. ' case vi of',
  9628. ' 1: vi:=3;',
  9629. ' TBird.e: ;',
  9630. ' end;']);
  9631. ConvertProgram;
  9632. CheckSource('TestCaseOfExternalClassConst',
  9633. LinesToStr([ // statements
  9634. 'this.vI = 0;'
  9635. ]),
  9636. LinesToStr([ // $mod.$main
  9637. 'var $tmp = $mod.vI;',
  9638. 'if ($tmp === 1) {',
  9639. ' $mod.vI = 3}',
  9640. ' else if ($tmp === Bird.e) ;'
  9641. ]));
  9642. end;
  9643. procedure TTestModule.TestDebugger;
  9644. begin
  9645. StartProgram(false);
  9646. Add([
  9647. 'procedure DoIt;',
  9648. 'begin',
  9649. ' deBugger;',
  9650. ' DeBugger();',
  9651. 'end;',
  9652. 'begin',
  9653. ' Debugger;']);
  9654. ConvertProgram;
  9655. CheckSource('TestDebugger',
  9656. LinesToStr([ // statements
  9657. 'this.DoIt = function () {',
  9658. ' debugger;',
  9659. ' debugger;',
  9660. '};',
  9661. '']),
  9662. LinesToStr([ // $mod.$main
  9663. 'debugger;',
  9664. '']));
  9665. end;
  9666. procedure TTestModule.TestArray_Dynamic;
  9667. begin
  9668. StartProgram(false);
  9669. Add([
  9670. 'type',
  9671. ' TArrayInt = array of longint;',
  9672. 'var',
  9673. ' Arr: TArrayInt;',
  9674. ' i: longint;',
  9675. ' b: boolean;',
  9676. 'begin',
  9677. ' SetLength(arr,3);',
  9678. ' arr[0]:=4;',
  9679. ' arr[1]:=length(arr)+arr[0];',
  9680. ' arr[i]:=5;',
  9681. ' arr[arr[i]]:=arr[6];',
  9682. ' i:=low(arr);',
  9683. ' i:=high(arr);',
  9684. ' b:=Assigned(arr);',
  9685. ' Arr:=default(TArrayInt);']);
  9686. ConvertProgram;
  9687. CheckSource('TestArray_Dynamic',
  9688. LinesToStr([ // statements
  9689. 'this.Arr = [];',
  9690. 'this.i = 0;',
  9691. 'this.b = false;'
  9692. ]),
  9693. LinesToStr([ // $mod.$main
  9694. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9695. '$mod.Arr[0] = 4;',
  9696. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9697. '$mod.Arr[$mod.i] = 5;',
  9698. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9699. '$mod.i = 0;',
  9700. '$mod.i = rtl.length($mod.Arr) - 1;',
  9701. '$mod.b = rtl.length($mod.Arr) > 0;',
  9702. '$mod.Arr = [];',
  9703. '']));
  9704. end;
  9705. procedure TTestModule.TestArray_Dynamic_Nil;
  9706. begin
  9707. StartProgram(false);
  9708. Add('type');
  9709. Add(' TArrayInt = array of longint;');
  9710. Add('var');
  9711. Add(' Arr: TArrayInt;');
  9712. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9713. Add('begin');
  9714. Add(' arr:=nil;');
  9715. Add(' if arr=nil then;');
  9716. Add(' if nil=arr then;');
  9717. Add(' if arr<>nil then;');
  9718. Add(' if nil<>arr then;');
  9719. Add(' DoIt(nil,nil);');
  9720. ConvertProgram;
  9721. CheckSource('TestArray_Dynamic',
  9722. LinesToStr([ // statements
  9723. 'this.Arr = [];',
  9724. 'this.DoIt = function(i,j){',
  9725. '};'
  9726. ]),
  9727. LinesToStr([ // $mod.$main
  9728. '$mod.Arr = [];',
  9729. 'if (rtl.length($mod.Arr) === 0) ;',
  9730. 'if (rtl.length($mod.Arr) === 0) ;',
  9731. 'if (rtl.length($mod.Arr) > 0) ;',
  9732. 'if (rtl.length($mod.Arr) > 0) ;',
  9733. '$mod.DoIt([],[]);',
  9734. '']));
  9735. end;
  9736. procedure TTestModule.TestArray_DynMultiDimensional;
  9737. begin
  9738. StartProgram(false);
  9739. Add([
  9740. 'type',
  9741. ' TArrayInt = array of longint;',
  9742. ' TArrayArrayInt = array of TArrayInt;',
  9743. 'var',
  9744. ' Arr: TArrayInt;',
  9745. ' Arr2: TArrayArrayInt;',
  9746. ' i: longint;',
  9747. 'begin',
  9748. ' arr2:=nil;',
  9749. ' if arr2=nil then;',
  9750. ' if nil=arr2 then;',
  9751. ' i:=low(arr2);',
  9752. ' i:=low(arr2[1]);',
  9753. ' i:=high(arr2);',
  9754. ' i:=high(arr2[2]);',
  9755. ' arr2[3]:=arr;',
  9756. ' arr2[4][5]:=i;',
  9757. ' i:=arr2[6][7];',
  9758. ' arr2[8,9]:=i;',
  9759. ' i:=arr2[10,11];',
  9760. ' SetLength(arr2,14);',
  9761. ' SetLength(arr2[15],16);']);
  9762. ConvertProgram;
  9763. CheckSource('TestArray_Dynamic',
  9764. LinesToStr([ // statements
  9765. 'this.Arr = [];',
  9766. 'this.Arr2 = [];',
  9767. 'this.i = 0;'
  9768. ]),
  9769. LinesToStr([ // $mod.$main
  9770. '$mod.Arr2 = [];',
  9771. 'if (rtl.length($mod.Arr2) === 0) ;',
  9772. 'if (rtl.length($mod.Arr2) === 0) ;',
  9773. '$mod.i = 0;',
  9774. '$mod.i = 0;',
  9775. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9776. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9777. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9778. '$mod.Arr2[4][5] = $mod.i;',
  9779. '$mod.i = $mod.Arr2[6][7];',
  9780. '$mod.Arr2[8][9] = $mod.i;',
  9781. '$mod.i = $mod.Arr2[10][11];',
  9782. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9783. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9784. '']));
  9785. end;
  9786. procedure TTestModule.TestArray_DynamicAssign;
  9787. begin
  9788. StartProgram(false);
  9789. Add([
  9790. 'type',
  9791. ' TArrayInt = array of longint;',
  9792. ' TArrayArrayInt = array of TArrayInt;',
  9793. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9794. 'begin',
  9795. 'end;',
  9796. 'procedure Fly(var a: TArrayInt);',
  9797. 'begin',
  9798. 'end;',
  9799. 'var',
  9800. ' Arr: TArrayInt;',
  9801. ' Arr2: TArrayArrayInt;',
  9802. 'begin',
  9803. ' arr:=nil;',
  9804. ' arr2:=nil;',
  9805. ' arr2[1]:=nil;',
  9806. ' arr2[2]:=arr;',
  9807. ' Run(arr,arr,arr);',
  9808. ' Fly(arr);',
  9809. ' Run(arr2[4],arr2[5],arr2[6]);',
  9810. ' Fly(arr2[7]);',
  9811. '']);
  9812. ConvertProgram;
  9813. CheckSource('TestArray_DynamicAssign',
  9814. LinesToStr([ // statements
  9815. 'this.Run = function (a, b, c) {',
  9816. '};',
  9817. 'this.Fly = function (a) {',
  9818. '};',
  9819. 'this.Arr = [];',
  9820. 'this.Arr2 = [];',
  9821. '']),
  9822. LinesToStr([ // $mod.$main
  9823. '$mod.Arr = [];',
  9824. '$mod.Arr2 = [];',
  9825. '$mod.Arr2[1] = [];',
  9826. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9827. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9828. '$mod.Fly({',
  9829. ' p: $mod,',
  9830. ' get: function () {',
  9831. ' return this.p.Arr;',
  9832. ' },',
  9833. ' set: function (v) {',
  9834. ' this.p.Arr = v;',
  9835. ' }',
  9836. '});',
  9837. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9838. '$mod.Fly({',
  9839. ' a: 7,',
  9840. ' p: $mod.Arr2,',
  9841. ' get: function () {',
  9842. ' return this.p[this.a];',
  9843. ' },',
  9844. ' set: function (v) {',
  9845. ' this.p[this.a] = v;',
  9846. ' }',
  9847. '});',
  9848. '']));
  9849. end;
  9850. procedure TTestModule.TestArray_StaticInt;
  9851. begin
  9852. StartProgram(false);
  9853. Add('type');
  9854. Add(' TArrayInt = array[2..4] of longint;');
  9855. Add('var');
  9856. Add(' Arr: TArrayInt;');
  9857. Add(' Arr2: TArrayInt = (5,6,7);');
  9858. Add(' i: longint;');
  9859. Add(' b: boolean;');
  9860. Add('begin');
  9861. Add(' arr[2]:=4;');
  9862. Add(' arr[3]:=arr[2]+arr[3];');
  9863. Add(' arr[i]:=5;');
  9864. Add(' arr[arr[i]]:=arr[high(arr)];');
  9865. Add(' i:=low(arr);');
  9866. Add(' i:=high(arr);');
  9867. Add(' b:=arr[2]=arr[3];');
  9868. Add(' arr:=default(TArrayInt);');
  9869. ConvertProgram;
  9870. CheckSource('TestArray_StaticInt',
  9871. LinesToStr([ // statements
  9872. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9873. 'this.Arr2 = [5, 6, 7];',
  9874. 'this.i = 0;',
  9875. 'this.b = false;'
  9876. ]),
  9877. LinesToStr([ // $mod.$main
  9878. '$mod.Arr[0] = 4;',
  9879. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9880. '$mod.Arr[$mod.i-2] = 5;',
  9881. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9882. '$mod.i = 2;',
  9883. '$mod.i = 4;',
  9884. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9885. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9886. '']));
  9887. end;
  9888. procedure TTestModule.TestArray_StaticBool;
  9889. begin
  9890. StartProgram(false);
  9891. Add('type');
  9892. Add(' TBools = array[boolean] of boolean;');
  9893. Add(' TBool2 = array[true..true] of boolean;');
  9894. Add('var');
  9895. Add(' Arr: TBools;');
  9896. Add(' Arr2: TBool2;');
  9897. Add(' Arr3: TBools = (true,false);');
  9898. Add(' b: boolean;');
  9899. Add('begin');
  9900. Add(' b:=low(arr);');
  9901. Add(' b:=high(arr);');
  9902. Add(' arr[true]:=false;');
  9903. Add(' arr[false]:=arr[b] or arr[true];');
  9904. Add(' arr[b]:=true;');
  9905. Add(' arr[arr[b]]:=arr[high(arr)];');
  9906. Add(' b:=arr[false]=arr[true];');
  9907. Add(' b:=low(arr2);');
  9908. Add(' b:=high(arr2);');
  9909. Add(' arr2[true]:=true;');
  9910. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9911. Add(' arr2[b]:=false;');
  9912. ConvertProgram;
  9913. CheckSource('TestArray_StaticBool',
  9914. LinesToStr([ // statements
  9915. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9916. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9917. 'this.Arr3 = [true, false];',
  9918. 'this.b = false;'
  9919. ]),
  9920. LinesToStr([ // $mod.$main
  9921. '$mod.b = false;',
  9922. '$mod.b = true;',
  9923. '$mod.Arr[1] = false;',
  9924. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9925. '$mod.Arr[+$mod.b] = true;',
  9926. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9927. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9928. '$mod.b = true;',
  9929. '$mod.b = true;',
  9930. '$mod.Arr2[0] = true;',
  9931. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9932. '$mod.Arr2[1-$mod.b] = false;',
  9933. '']));
  9934. end;
  9935. procedure TTestModule.TestArray_StaticChar;
  9936. begin
  9937. StartProgram(false);
  9938. Add([
  9939. 'type',
  9940. ' TChars = array[char] of char;',
  9941. ' TChars2 = array[''a''..''z''] of char;',
  9942. 'var',
  9943. ' Arr: TChars;',
  9944. ' Arr2: TChars2;',
  9945. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9946. ' Arr4: array[11..13] of char = ''pas'';',
  9947. ' Arr5: array[21..22] of char = ''äö'';',
  9948. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9949. ' c: char;',
  9950. ' b: boolean;',
  9951. 'begin',
  9952. ' c:=low(arr);',
  9953. ' c:=high(arr);',
  9954. ' arr[''B'']:=''a'';',
  9955. ' arr[''D'']:=arr[c];',
  9956. ' arr[c]:=arr[''d''];',
  9957. ' arr[arr[c]]:=arr[high(arr)];',
  9958. ' b:=arr[low(arr)]=arr[''e''];',
  9959. ' c:=low(arr2);',
  9960. ' c:=high(arr2);',
  9961. ' arr2[''b'']:=''f'';',
  9962. ' arr2[''a'']:=arr2[c];',
  9963. ' arr2[c]:=arr2[''g''];']);
  9964. ConvertProgram;
  9965. CheckSource('TestArray_StaticChar',
  9966. LinesToStr([ // statements
  9967. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9968. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9969. 'this.Arr3 = ["p", "a", "s"];',
  9970. 'this.Arr4 = ["p", "a", "s"];',
  9971. 'this.Arr5 = ["ä", "ö"];',
  9972. 'this.Arr6 = ["ä", "ö"];',
  9973. 'this.c = "";',
  9974. 'this.b = false;',
  9975. '']),
  9976. LinesToStr([ // $mod.$main
  9977. '$mod.c = "\x00";',
  9978. '$mod.c = "\uFFFF";',
  9979. '$mod.Arr[66] = "a";',
  9980. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9981. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9982. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9983. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9984. '$mod.c = "a";',
  9985. '$mod.c = "z";',
  9986. '$mod.Arr2[1] = "f";',
  9987. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9988. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9989. '']));
  9990. end;
  9991. procedure TTestModule.TestArray_StaticMultiDim;
  9992. begin
  9993. StartProgram(false);
  9994. Add([
  9995. 'type',
  9996. ' TArrayInt = array[1..3] of longint;',
  9997. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9998. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  9999. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10000. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10001. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10002. 'var',
  10003. ' Arr: TArrayInt;',
  10004. ' Arr2: TArrayArrayInt;',
  10005. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10006. ' Arr4: TArrayArrayInt;',
  10007. ' ArrDim2: TArrayDim2Int;',
  10008. ' ArrDim3: TArrayDim3Int;',
  10009. ' ArrDim4: TArrayDim4Int;',
  10010. ' i: longint;',
  10011. 'begin',
  10012. ' i:=low(arr);',
  10013. ' i:=low(arr2);',
  10014. ' i:=low(arr2[5]);',
  10015. ' i:=high(arr);',
  10016. ' i:=high(arr2);',
  10017. ' i:=high(arr2[6]);',
  10018. ' arr2[5]:=arr;',
  10019. ' arr2[6][2]:=i;',
  10020. ' i:=arr2[6][3];',
  10021. ' arr2[6,3]:=i;',
  10022. ' i:=arr2[5,2];',
  10023. ' arr2:=arr2;',// clone multi dim static array
  10024. ' arr3:=arr3;',// clone anonymous multi dim static array
  10025. ' arr4:=arr4;',
  10026. ' Arr:=Arr;',
  10027. ' ArrDim2:=ArrDim2;',
  10028. ' ArrDim3:=ArrDim3;',
  10029. ' ArrDim4:=ArrDim4;',
  10030. '']);
  10031. ConvertProgram;
  10032. CheckSource('TestArray_StaticMultiDim',
  10033. LinesToStr([ // statements
  10034. 'this.TArrayArrayInt$clone = function (a) {',
  10035. ' var b = [];',
  10036. ' b.length = 2;',
  10037. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10038. ' return b;',
  10039. '};',
  10040. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10041. ' var b = [];',
  10042. ' b.length = 2;',
  10043. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10044. ' return b;',
  10045. '};',
  10046. 'this.TArrayDim2Int$clone = function (a) {',
  10047. ' var b = [];',
  10048. ' b.length = 2;',
  10049. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10050. ' return b;',
  10051. '};',
  10052. 'this.TArrayDim3Int$clone = function (a) {',
  10053. ' var b = [];',
  10054. ' b.length = 2;',
  10055. ' for (var c = 0; c < 2; c++) {',
  10056. ' var d = b[c] = [];',
  10057. ' d.length = 3;',
  10058. ' var e = a[c];',
  10059. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10060. ' };',
  10061. ' return b;',
  10062. '};',
  10063. 'this.TArrayDim4Int$clone = function (a) {',
  10064. ' var b = [];',
  10065. ' b.length = 2;',
  10066. ' for (var c = 0; c < 2; c++) {',
  10067. ' var d = b[c] = [];',
  10068. ' d.length = 3;',
  10069. ' var e = a[c];',
  10070. ' for (var f = 0; f < 3; f++) {',
  10071. ' var g = d[f] = [];',
  10072. ' g.length = 4;',
  10073. ' var h = e[f];',
  10074. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10075. ' };',
  10076. ' };',
  10077. ' return b;',
  10078. '};',
  10079. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10080. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10081. 'this.Arr3$a$clone = function (a) {',
  10082. ' var b = [];',
  10083. ' b.length = 2;',
  10084. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10085. ' return b;',
  10086. '};',
  10087. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10088. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10089. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10090. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10091. 'this.ArrDim4 = rtl.arraySetLength(',
  10092. ' null,',
  10093. ' 0,',
  10094. ' 2,',
  10095. ' 3,',
  10096. ' 4,',
  10097. ' 5',
  10098. ');',
  10099. 'this.i = 0;'
  10100. ]),
  10101. LinesToStr([ // $mod.$main
  10102. '$mod.i = 1;',
  10103. '$mod.i = 5;',
  10104. '$mod.i = 1;',
  10105. '$mod.i = 3;',
  10106. '$mod.i = 6;',
  10107. '$mod.i = 3;',
  10108. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10109. '$mod.Arr2[1][1] = $mod.i;',
  10110. '$mod.i = $mod.Arr2[1][2];',
  10111. '$mod.Arr2[1][2] = $mod.i;',
  10112. '$mod.i = $mod.Arr2[0][1];',
  10113. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10114. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10115. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10116. '$mod.Arr = $mod.Arr.slice(0);',
  10117. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10118. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10119. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10120. '']));
  10121. end;
  10122. procedure TTestModule.TestArray_StaticInFunction;
  10123. begin
  10124. StartProgram(false);
  10125. Add([
  10126. 'const TArrayInt = 3;',
  10127. 'const TArrayArrayInt = 4;',
  10128. 'procedure DoIt;',
  10129. 'type',
  10130. ' TArrayInt = array[1..3] of longint;',
  10131. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10132. 'var',
  10133. ' Arr: TArrayInt;',
  10134. ' Arr2: TArrayArrayInt;',
  10135. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10136. ' i: longint;',
  10137. 'begin',
  10138. ' arr2[5]:=arr;',
  10139. ' arr2:=arr2;',// clone multi dim static array
  10140. ' arr3:=arr3;',// clone multi dim anonymous static array
  10141. 'end;',
  10142. 'begin',
  10143. '']);
  10144. ConvertProgram;
  10145. CheckSource('TestArray_StaticInFunction',
  10146. LinesToStr([ // statements
  10147. 'this.TArrayInt = 3;',
  10148. 'this.TArrayArrayInt = 4;',
  10149. 'var TArrayArrayInt$1$clone = function (a) {',
  10150. ' var b = [];',
  10151. ' b.length = 2;',
  10152. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10153. ' return b;',
  10154. '};',
  10155. 'var Arr3$a$clone = function (a) {',
  10156. ' var b = [];',
  10157. ' b.length = 2;',
  10158. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10159. ' return b;',
  10160. '};',
  10161. 'this.DoIt = function () {',
  10162. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10163. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10164. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10165. ' var i = 0;',
  10166. ' Arr2[0] = Arr.slice(0);',
  10167. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10168. ' Arr3 = Arr3$a$clone(Arr3);',
  10169. '};',
  10170. '']),
  10171. LinesToStr([ // $mod.$main
  10172. '']));
  10173. end;
  10174. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10175. begin
  10176. StartProgram(false);
  10177. Add([
  10178. 'type',
  10179. ' TArrayInt = array[1..3,1..2] of longint;',
  10180. 'var',
  10181. ' a,b: TArrayInt;',
  10182. 'begin',
  10183. ' if a=b then ;',
  10184. '']);
  10185. SetExpectedPasResolverError('compare static array is not supported',
  10186. nXIsNotSupported);
  10187. ConvertProgram;
  10188. end;
  10189. procedure TTestModule.TestArrayOfRecord;
  10190. begin
  10191. StartProgram(false);
  10192. Add([
  10193. 'type',
  10194. ' TRec = record',
  10195. ' Int: longint;',
  10196. ' end;',
  10197. ' TArrayRec = array of TRec;',
  10198. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10199. 'begin',
  10200. 'end;',
  10201. 'var',
  10202. ' Arr: TArrayRec;',
  10203. ' r: TRec;',
  10204. ' i: longint;',
  10205. 'begin',
  10206. ' SetLength(arr,3);',
  10207. ' arr[0].int:=4;',
  10208. ' arr[1].int:=length(arr)+arr[2].int;',
  10209. ' arr[arr[i].int].int:=arr[5].int;',
  10210. ' arr[7]:=r;',
  10211. ' r:=arr[8];',
  10212. ' i:=low(arr);',
  10213. ' i:=high(arr);',
  10214. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10215. ConvertProgram;
  10216. CheckSource('TestArrayOfRecord',
  10217. LinesToStr([ // statements
  10218. 'rtl.recNewT(this, "TRec", function () {',
  10219. ' this.Int = 0;',
  10220. ' this.$eq = function (b) {',
  10221. ' return this.Int === b.Int;',
  10222. ' };',
  10223. ' this.$assign = function (s) {',
  10224. ' this.Int = s.Int;',
  10225. ' return this;',
  10226. ' };',
  10227. '});',
  10228. 'this.DoIt = function (vd, vc, vv) {',
  10229. '};',
  10230. 'this.Arr = [];',
  10231. 'this.r = this.TRec.$new();',
  10232. 'this.i = 0;'
  10233. ]),
  10234. LinesToStr([ // $mod.$main
  10235. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10236. '$mod.Arr[0].Int = 4;',
  10237. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10238. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10239. '$mod.Arr[7].$assign($mod.r);',
  10240. '$mod.r.$assign($mod.Arr[8]);',
  10241. '$mod.i = 0;',
  10242. '$mod.i = rtl.length($mod.Arr)-1;',
  10243. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10244. '']));
  10245. end;
  10246. procedure TTestModule.TestArray_StaticRecord;
  10247. begin
  10248. StartProgram(false);
  10249. Add([
  10250. 'type',
  10251. ' TRec = record',
  10252. ' Int: longint;',
  10253. ' end;',
  10254. ' TArrayRec = array[1..2] of TRec;',
  10255. 'var',
  10256. ' Arr: TArrayRec;',
  10257. 'begin',
  10258. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10259. '']);
  10260. ConvertProgram;
  10261. CheckSource('TestArray_StaticRecord',
  10262. LinesToStr([ // statements
  10263. 'rtl.recNewT(this, "TRec", function () {',
  10264. ' this.Int = 0;',
  10265. ' this.$eq = function (b) {',
  10266. ' return this.Int === b.Int;',
  10267. ' };',
  10268. ' this.$assign = function (s) {',
  10269. ' this.Int = s.Int;',
  10270. ' return this;',
  10271. ' };',
  10272. '});',
  10273. 'this.TArrayRec$clone = function (a) {',
  10274. ' var b = [];',
  10275. ' b.length = 2;',
  10276. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10277. ' return b;',
  10278. '};',
  10279. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10280. '']),
  10281. LinesToStr([ // $mod.$main
  10282. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10283. end;
  10284. procedure TTestModule.TestArrayOfSet;
  10285. begin
  10286. StartProgram(false);
  10287. Add([
  10288. 'type',
  10289. ' TFlag = (big,small);',
  10290. ' TSetOfFlag = set of tflag;',
  10291. ' TArrayFlag = array of TSetOfFlag;',
  10292. 'procedure DoIt(const a: Tarrayflag);',
  10293. 'begin',
  10294. 'end;',
  10295. 'var',
  10296. ' f: TFlag;',
  10297. ' s: TSetOfFlag;',
  10298. ' Arr: TArrayFlag;',
  10299. ' i: longint;',
  10300. 'begin',
  10301. ' SetLength(arr,3);',
  10302. ' arr[0]:=s;',
  10303. ' arr[1]:=[big];',
  10304. ' arr[2]:=[big]+s;',
  10305. ' arr[3]:=s+[big];',
  10306. ' arr[4]:=arr[5];',
  10307. ' s:=arr[6];',
  10308. ' i:=low(arr);',
  10309. ' i:=high(arr);',
  10310. ' DoIt(arr);',
  10311. ' DoIt([s]);',
  10312. ' DoIt([[],s]);',
  10313. ' DoIt([s,[]]);',
  10314. '']);
  10315. ConvertProgram;
  10316. CheckSource('TestArrayOfSet',
  10317. LinesToStr([ // statements
  10318. 'this.TFlag = {',
  10319. ' "0": "big",',
  10320. ' big: 0,',
  10321. ' "1": "small",',
  10322. ' small: 1',
  10323. '};',
  10324. 'this.DoIt = function (a) {',
  10325. '};',
  10326. 'this.f = 0;',
  10327. 'this.s = {};',
  10328. 'this.Arr = [];',
  10329. 'this.i = 0;',
  10330. '']),
  10331. LinesToStr([ // $mod.$main
  10332. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10333. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10334. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10335. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10336. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10337. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10338. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10339. '$mod.i = 0;',
  10340. '$mod.i = rtl.length($mod.Arr) - 1;',
  10341. '$mod.DoIt($mod.Arr);',
  10342. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10343. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10344. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10345. '']));
  10346. end;
  10347. procedure TTestModule.TestArray_DynAsParam;
  10348. begin
  10349. StartProgram(false);
  10350. Add([
  10351. 'type integer = longint;',
  10352. 'type TArrInt = array of integer;',
  10353. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10354. 'var vJ: TArrInt;',
  10355. 'begin',
  10356. ' vg:=vg;',
  10357. ' vj:=vh;',
  10358. ' vi:=vi;',
  10359. ' doit(vg,vg,vg);',
  10360. ' doit(vh,vh,vj);',
  10361. ' doit(vi,vi,vi);',
  10362. ' doit(vj,vj,vj);',
  10363. 'end;',
  10364. 'var i: TArrInt;',
  10365. 'begin',
  10366. ' doit(i,i,i);']);
  10367. ConvertProgram;
  10368. CheckSource('TestArray_DynAsParams',
  10369. LinesToStr([ // statements
  10370. 'this.DoIt = function (vG,vH,vI) {',
  10371. ' var vJ = [];',
  10372. ' vG = rtl.arrayRef(vG);',
  10373. ' vJ = rtl.arrayRef(vH);',
  10374. ' vI.set(rtl.arrayRef(vI.get()));',
  10375. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10376. ' get: function () {',
  10377. ' return vG;',
  10378. ' },',
  10379. ' set: function (v) {',
  10380. ' vG = v;',
  10381. ' }',
  10382. ' });',
  10383. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10384. ' get: function () {',
  10385. ' return vJ;',
  10386. ' },',
  10387. ' set: function (v) {',
  10388. ' vJ = v;',
  10389. ' }',
  10390. ' });',
  10391. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10392. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10393. ' get: function () {',
  10394. ' return vJ;',
  10395. ' },',
  10396. ' set: function (v) {',
  10397. ' vJ = v;',
  10398. ' }',
  10399. ' });',
  10400. '};',
  10401. 'this.i = [];'
  10402. ]),
  10403. LinesToStr([
  10404. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10405. ' p: $mod,',
  10406. ' get: function () {',
  10407. ' return this.p.i;',
  10408. ' },',
  10409. ' set: function (v) {',
  10410. ' this.p.i = v;',
  10411. ' }',
  10412. '});'
  10413. ]));
  10414. end;
  10415. procedure TTestModule.TestArray_StaticAsParam;
  10416. begin
  10417. StartProgram(false);
  10418. Add([
  10419. 'type integer = longint;',
  10420. 'type TArrInt = array[1..2] of integer;',
  10421. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10422. 'var vJ: TArrInt;',
  10423. 'begin',
  10424. ' vg:=vg;',
  10425. ' vj:=vh;',
  10426. ' vi:=vi;',
  10427. ' doit(vg,vg,vg);',
  10428. ' doit(vh,vh,vj);',
  10429. ' doit(vi,vi,vi);',
  10430. ' doit(vj,vj,vj);',
  10431. 'end;',
  10432. 'var i: TArrInt;',
  10433. 'begin',
  10434. ' doit(i,i,i);']);
  10435. ConvertProgram;
  10436. CheckSource('TestArray_StaticAsParams',
  10437. LinesToStr([ // statements
  10438. 'this.DoIt = function (vG,vH,vI) {',
  10439. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10440. ' vG = vG.slice(0);',
  10441. ' vJ = vH.slice(0);',
  10442. ' vI.set(vI.get().slice(0));',
  10443. ' $mod.DoIt(vG.slice(0), vG, {',
  10444. ' get: function () {',
  10445. ' return vG;',
  10446. ' },',
  10447. ' set: function (v) {',
  10448. ' vG = v;',
  10449. ' }',
  10450. ' });',
  10451. ' $mod.DoIt(vH.slice(0), vH, {',
  10452. ' get: function () {',
  10453. ' return vJ;',
  10454. ' },',
  10455. ' set: function (v) {',
  10456. ' vJ = v;',
  10457. ' }',
  10458. ' });',
  10459. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10460. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10461. ' get: function () {',
  10462. ' return vJ;',
  10463. ' },',
  10464. ' set: function (v) {',
  10465. ' vJ = v;',
  10466. ' }',
  10467. ' });',
  10468. '};',
  10469. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10470. ]),
  10471. LinesToStr([
  10472. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10473. ' p: $mod,',
  10474. ' get: function () {',
  10475. ' return this.p.i;',
  10476. ' },',
  10477. ' set: function (v) {',
  10478. ' this.p.i = v;',
  10479. ' }',
  10480. '});'
  10481. ]));
  10482. end;
  10483. procedure TTestModule.TestArrayElement_AsParams;
  10484. begin
  10485. StartProgram(false);
  10486. Add('type integer = longint;');
  10487. Add('type TArrayInt = array of integer;');
  10488. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10489. Add('var vJ: tarrayint;');
  10490. Add('begin');
  10491. Add(' vi:=vi;');
  10492. Add(' doit(vi,vi,vi);');
  10493. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10494. Add('end;');
  10495. Add('var a: TArrayInt;');
  10496. Add('begin');
  10497. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10498. ConvertProgram;
  10499. CheckSource('TestArrayElement_AsParams',
  10500. LinesToStr([ // statements
  10501. 'this.DoIt = function (vG,vH,vI) {',
  10502. ' var vJ = [];',
  10503. ' vI.set(vI.get());',
  10504. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10505. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10506. ' a:1+3,',
  10507. ' p:vJ,',
  10508. ' get: function () {',
  10509. ' return this.p[this.a];',
  10510. ' },',
  10511. ' set: function (v) {',
  10512. ' this.p[this.a] = v;',
  10513. ' }',
  10514. ' });',
  10515. '};',
  10516. 'this.a = [];'
  10517. ]),
  10518. LinesToStr([
  10519. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10520. ' a: 1+6,',
  10521. ' p: $mod.a,',
  10522. ' get: function () {',
  10523. ' return this.p[this.a];',
  10524. ' },',
  10525. ' set: function (v) {',
  10526. ' this.p[this.a] = v;',
  10527. ' }',
  10528. '});'
  10529. ]));
  10530. end;
  10531. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10532. begin
  10533. StartProgram(false);
  10534. Add('type Integer = longint;');
  10535. Add('type TArrayInt = array of integer;');
  10536. Add('function GetArr(vB: integer = 0): tarrayint;');
  10537. Add('begin');
  10538. Add('end;');
  10539. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10540. Add('begin');
  10541. Add('end;');
  10542. Add('begin');
  10543. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10544. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10545. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10546. ConvertProgram;
  10547. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10548. LinesToStr([ // statements
  10549. 'this.GetArr = function (vB) {',
  10550. ' var Result = [];',
  10551. ' return Result;',
  10552. '};',
  10553. 'this.DoIt = function (vG,vH,vI) {',
  10554. '};'
  10555. ]),
  10556. LinesToStr([
  10557. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10558. ' a: 1+3,',
  10559. ' p: $mod.GetArr(0),',
  10560. ' get: function () {',
  10561. ' return this.p[this.a];',
  10562. ' },',
  10563. ' set: function (v) {',
  10564. ' this.p[this.a] = v;',
  10565. ' }',
  10566. '});',
  10567. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10568. ' a: 2+3,',
  10569. ' p: $mod.GetArr(0),',
  10570. ' get: function () {',
  10571. ' return this.p[this.a];',
  10572. ' },',
  10573. ' set: function (v) {',
  10574. ' this.p[this.a] = v;',
  10575. ' }',
  10576. '});',
  10577. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10578. ' a: 3+3,',
  10579. ' p: $mod.GetArr(9),',
  10580. ' get: function () {',
  10581. ' return this.p[this.a];',
  10582. ' },',
  10583. ' set: function (v) {',
  10584. ' this.p[this.a] = v;',
  10585. ' }',
  10586. '});',
  10587. '']));
  10588. end;
  10589. procedure TTestModule.TestArrayEnumTypeRange;
  10590. begin
  10591. StartProgram(false);
  10592. Add([
  10593. 'type',
  10594. ' TEnum = (red,blue);',
  10595. ' TEnumArray = array[TEnum] of longint;',
  10596. 'var',
  10597. ' e: TEnum;',
  10598. ' i: longint;',
  10599. ' a: TEnumArray;',
  10600. ' numbers: TEnumArray = (1,2);',
  10601. ' names: array[TEnum] of string = (''red'',''blue'');',
  10602. 'begin',
  10603. ' e:=low(a);',
  10604. ' e:=high(a);',
  10605. ' i:=a[red];',
  10606. ' a[e]:=a[e];']);
  10607. ConvertProgram;
  10608. CheckSource('TestArrayEnumTypeRange',
  10609. LinesToStr([ // statements
  10610. ' this.TEnum = {',
  10611. ' "0": "red",',
  10612. ' red: 0,',
  10613. ' "1": "blue",',
  10614. ' blue: 1',
  10615. '};',
  10616. 'this.e = 0;',
  10617. 'this.i = 0;',
  10618. 'this.a = rtl.arraySetLength(null,0,2);',
  10619. 'this.numbers = [1, 2];',
  10620. 'this.names = ["red", "blue"];',
  10621. '']),
  10622. LinesToStr([ // $mod.$main
  10623. '$mod.e = $mod.TEnum.red;',
  10624. '$mod.e = $mod.TEnum.blue;',
  10625. '$mod.i = $mod.a[$mod.TEnum.red];',
  10626. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10627. '']));
  10628. end;
  10629. procedure TTestModule.TestArray_SetLengthOutArg;
  10630. begin
  10631. StartProgram(false);
  10632. Add([
  10633. 'type TArrInt = array of longint;',
  10634. 'procedure DoIt(out a: TArrInt);',
  10635. 'begin',
  10636. ' SetLength(a,2);',
  10637. 'end;',
  10638. 'begin',
  10639. '']);
  10640. ConvertProgram;
  10641. CheckSource('TestArray_SetLengthOutArg',
  10642. LinesToStr([ // statements
  10643. 'this.DoIt = function (a) {',
  10644. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10645. '};',
  10646. '']),
  10647. LinesToStr([
  10648. '']));
  10649. end;
  10650. procedure TTestModule.TestArray_SetLengthProperty;
  10651. begin
  10652. StartProgram(false);
  10653. Add('type');
  10654. Add(' TArrInt = array of longint;');
  10655. Add(' TObject = class');
  10656. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10657. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10658. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10659. Add(' end;');
  10660. Add('var Obj: TObject;');
  10661. Add('begin');
  10662. Add(' SetLength(Obj.Colors,2);');
  10663. ConvertProgram;
  10664. CheckSource('TestArray_SetLengthProperty',
  10665. LinesToStr([ // statements
  10666. 'rtl.createClass(this, "TObject", null, function () {',
  10667. ' this.$init = function () {',
  10668. ' };',
  10669. ' this.$final = function () {',
  10670. ' };',
  10671. '});',
  10672. 'this.Obj = null;',
  10673. '']),
  10674. LinesToStr([
  10675. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10676. '']));
  10677. end;
  10678. procedure TTestModule.TestArray_SetLengthMultiDim;
  10679. begin
  10680. StartProgram(false);
  10681. Add([
  10682. 'type',
  10683. ' TArrArrInt = array of array of longint;',
  10684. ' TArrStaInt = array of array[1..2] of longint;',
  10685. 'var',
  10686. ' a: TArrArrInt;',
  10687. ' b: TArrStaInt;',
  10688. 'begin',
  10689. ' SetLength(a,2);',
  10690. ' SetLength(a,3,4);',
  10691. ' SetLength(b,5);',
  10692. '']);
  10693. ConvertProgram;
  10694. CheckSource('TestArray_SetLengthMultiDim',
  10695. LinesToStr([ // statements
  10696. 'this.a = [];',
  10697. 'this.b = [];',
  10698. '']),
  10699. LinesToStr([
  10700. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10701. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10702. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10703. '']));
  10704. end;
  10705. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10706. begin
  10707. StartProgram(false);
  10708. Add([
  10709. 'type',
  10710. ' TStaArr1 = array[1..3] of boolean;',
  10711. //' TStaArr2 = array[5..6] of TStaArr1;',
  10712. ' TDynArr1StaArr1 = array of TStaArr1;',
  10713. //' TDynArr1StaArr2 = array of TStaArr2;',
  10714. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10715. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10716. 'var',
  10717. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10718. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10719. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10720. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10721. 'begin',
  10722. ' SetLength(DynArr1StaArr1,11);',
  10723. ' SetLength(DynArr2StaArr1,12);',
  10724. ' SetLength(DynArr2StaArr1[13],14);',
  10725. ' SetLength(DynArr2StaArr1,15,16);',
  10726. //' SetLength(DynArr1StaArr2,21);',
  10727. //' SetLength(DynArr2StaArr2,22);',
  10728. //' SetLength(DynArr2StaArr2[23],24);',
  10729. //' SetLength(DynArr2StaArr2,25,26);',
  10730. '']);
  10731. ConvertProgram;
  10732. CheckSource('TestArray_DynOfStatic',
  10733. LinesToStr([ // statements
  10734. 'this.DynArr1StaArr1 = [];',
  10735. 'this.DynArr2StaArr1 = [];',
  10736. '']),
  10737. LinesToStr([ // $mod.$main
  10738. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10739. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10740. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10741. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10742. ' $mod.DynArr2StaArr1,',
  10743. ' false,',
  10744. ' 15,',
  10745. ' 16,',
  10746. ' "s",',
  10747. ' 3',
  10748. ');',
  10749. '']));
  10750. end;
  10751. procedure TTestModule.TestArray_OpenArrayOfString;
  10752. begin
  10753. StartProgram(false);
  10754. Add('procedure DoIt(const a: array of String);');
  10755. Add('var');
  10756. Add(' i: longint;');
  10757. Add(' s: string;');
  10758. Add('begin');
  10759. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10760. Add('end;');
  10761. Add('var s: string;');
  10762. Add('begin');
  10763. Add(' DoIt([]);');
  10764. Add(' DoIt([s,''foo'','''',s+s]);');
  10765. ConvertProgram;
  10766. CheckSource('TestArray_OpenArrayOfString',
  10767. LinesToStr([ // statements
  10768. 'this.DoIt = function (a) {',
  10769. ' var i = 0;',
  10770. ' var s = "";',
  10771. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10772. ' i = $l;',
  10773. ' s = a[rtl.length(a) - i - 1];',
  10774. ' };',
  10775. '};',
  10776. 'this.s = "";',
  10777. '']),
  10778. LinesToStr([
  10779. '$mod.DoIt([]);',
  10780. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10781. '']));
  10782. end;
  10783. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10784. begin
  10785. StartProgram(false);
  10786. Add([
  10787. 'type TArr = array of char;',
  10788. 'var',
  10789. ' c: char;',
  10790. ' s: string;',
  10791. ' a: TArr;',
  10792. 'procedure Run(const a: array of char);',
  10793. 'begin',
  10794. ' Run(c);',
  10795. ' Run(s);',
  10796. 'end;',
  10797. 'begin',
  10798. ' a:=c;',
  10799. ' a:=s;',
  10800. ' a:=#13;',
  10801. ' a:=''Foo'';',
  10802. ' Run(c);',
  10803. ' Run(s);',
  10804. '']);
  10805. ConvertProgram;
  10806. CheckSource('TestArray_ArrayOfCharAssignString',
  10807. LinesToStr([ // statements
  10808. 'this.c = "";',
  10809. 'this.s = "";',
  10810. 'this.a = [];',
  10811. 'this.Run = function (a) {',
  10812. ' $mod.Run($mod.c.split(""));',
  10813. ' $mod.Run($mod.s.split(""));',
  10814. '};',
  10815. '']),
  10816. LinesToStr([
  10817. '$mod.a = $mod.c.split("");',
  10818. '$mod.a = $mod.s.split("");',
  10819. '$mod.a = "\r".split("");',
  10820. '$mod.a = "Foo".split("");',
  10821. '$mod.Run($mod.c.split(""));',
  10822. '$mod.Run($mod.s.split(""));',
  10823. '']));
  10824. end;
  10825. procedure TTestModule.TestArray_ConstRef;
  10826. begin
  10827. StartProgram(false);
  10828. Add([
  10829. 'type TArr = array of word;',
  10830. 'procedure Run(constref a: TArr);',
  10831. 'begin',
  10832. 'end;',
  10833. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10834. 'var l: TArr;',
  10835. 'begin',
  10836. ' Run(l);',
  10837. ' Run(a);',
  10838. ' Run(b);',
  10839. ' Run(c);',
  10840. ' Run(d);',
  10841. ' Run(e);',
  10842. 'end;',
  10843. 'begin',
  10844. '']);
  10845. ConvertProgram;
  10846. CheckResolverUnexpectedHints();
  10847. CheckSource('TestArray_ConstRef',
  10848. LinesToStr([ // statements
  10849. 'this.Run = function (a) {',
  10850. '};',
  10851. 'this.Fly = function (a, b, c, d, e) {',
  10852. ' var l = [];',
  10853. ' $mod.Run(l);',
  10854. ' $mod.Run(a);',
  10855. ' $mod.Run(b.get());',
  10856. ' $mod.Run(c.get());',
  10857. ' $mod.Run(d);',
  10858. ' $mod.Run(e);',
  10859. '};',
  10860. '']),
  10861. LinesToStr([
  10862. '']));
  10863. end;
  10864. procedure TTestModule.TestArray_Concat;
  10865. begin
  10866. StartProgram(false);
  10867. Add([
  10868. 'type',
  10869. ' integer = longint;',
  10870. ' TFlag = (big,small);',
  10871. ' TFlags = set of TFlag;',
  10872. ' TRec = record',
  10873. ' i: integer;',
  10874. ' end;',
  10875. ' TArrInt = array of integer;',
  10876. ' TArrRec = array of TRec;',
  10877. ' TArrFlag = array of TFlag;',
  10878. ' TArrSet = array of TFlags;',
  10879. ' TArrJSValue = array of jsvalue;',
  10880. 'var',
  10881. ' ArrInt: tarrint;',
  10882. ' ArrRec: tarrrec;',
  10883. ' ArrFlag: tarrflag;',
  10884. ' ArrSet: tarrset;',
  10885. ' ArrJSValue: tarrjsvalue;',
  10886. 'begin',
  10887. ' arrint:=concat(arrint);',
  10888. ' arrint:=concat(arrint,arrint);',
  10889. ' arrint:=concat(arrint,arrint,arrint);',
  10890. ' arrrec:=concat(arrrec);',
  10891. ' arrrec:=concat(arrrec,arrrec);',
  10892. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  10893. ' arrset:=concat(arrset);',
  10894. ' arrset:=concat(arrset,arrset);',
  10895. ' arrset:=concat(arrset,arrset,arrset);',
  10896. ' arrjsvalue:=concat(arrjsvalue);',
  10897. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  10898. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  10899. ' arrint:=concat([1],arrint);',
  10900. ' arrflag:=concat([big]);',
  10901. ' arrflag:=concat([big],arrflag);',
  10902. ' arrflag:=concat(arrflag,[small]);',
  10903. '']);
  10904. ConvertProgram;
  10905. CheckSource('TestArray_Concat',
  10906. LinesToStr([ // statements
  10907. 'this.TFlag = {',
  10908. ' "0": "big",',
  10909. ' big: 0,',
  10910. ' "1": "small",',
  10911. ' small: 1',
  10912. '};',
  10913. 'rtl.recNewT(this, "TRec", function () {',
  10914. ' this.i = 0;',
  10915. ' this.$eq = function (b) {',
  10916. ' return this.i === b.i;',
  10917. ' };',
  10918. ' this.$assign = function (s) {',
  10919. ' this.i = s.i;',
  10920. ' return this;',
  10921. ' };',
  10922. '});',
  10923. 'this.ArrInt = [];',
  10924. 'this.ArrRec = [];',
  10925. 'this.ArrFlag = [];',
  10926. 'this.ArrSet = [];',
  10927. 'this.ArrJSValue = [];',
  10928. '']),
  10929. LinesToStr([ // $mod.$main
  10930. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10931. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10932. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10933. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10934. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10935. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10936. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10937. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10938. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10939. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10940. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10941. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10942. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10943. '$mod.ArrFlag = [$mod.TFlag.big];',
  10944. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10945. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10946. '']));
  10947. end;
  10948. procedure TTestModule.TestArray_Copy;
  10949. begin
  10950. StartProgram(false);
  10951. Add([
  10952. 'type',
  10953. ' integer = longint;',
  10954. ' TFlag = (big,small);',
  10955. ' TFlags = set of TFlag;',
  10956. ' TRec = record',
  10957. ' i: integer;',
  10958. ' end;',
  10959. ' TArrInt = array of integer;',
  10960. ' TArrRec = array of TRec;',
  10961. ' TArrSet = array of TFlags;',
  10962. ' TArrJSValue = array of jsvalue;',
  10963. 'var',
  10964. ' ArrInt: tarrint;',
  10965. ' ArrRec: tarrrec;',
  10966. ' ArrSet: tarrset;',
  10967. ' ArrJSValue: tarrjsvalue;',
  10968. 'begin',
  10969. ' arrint:=copy(arrint);',
  10970. ' arrint:=copy(arrint,2);',
  10971. ' arrint:=copy(arrint,3,4);',
  10972. ' arrint:=copy([1,1],1,2);',
  10973. ' arrrec:=copy(arrrec);',
  10974. ' arrrec:=copy(arrrec,5);',
  10975. ' arrrec:=copy(arrrec,6,7);',
  10976. ' arrset:=copy(arrset);',
  10977. ' arrset:=copy(arrset,8);',
  10978. ' arrset:=copy(arrset,9,10);',
  10979. ' arrjsvalue:=copy(arrjsvalue);',
  10980. ' arrjsvalue:=copy(arrjsvalue,11);',
  10981. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10982. ' ']);
  10983. ConvertProgram;
  10984. CheckSource('TestArray_Copy',
  10985. LinesToStr([ // statements
  10986. 'this.TFlag = {',
  10987. ' "0": "big",',
  10988. ' big: 0,',
  10989. ' "1": "small",',
  10990. ' small: 1',
  10991. '};',
  10992. 'rtl.recNewT(this, "TRec", function () {',
  10993. ' this.i = 0;',
  10994. ' this.$eq = function (b) {',
  10995. ' return this.i === b.i;',
  10996. ' };',
  10997. ' this.$assign = function (s) {',
  10998. ' this.i = s.i;',
  10999. ' return this;',
  11000. ' };',
  11001. '});',
  11002. 'this.ArrInt = [];',
  11003. 'this.ArrRec = [];',
  11004. 'this.ArrSet = [];',
  11005. 'this.ArrJSValue = [];',
  11006. '']),
  11007. LinesToStr([ // $mod.$main
  11008. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11009. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11010. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11011. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11012. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11013. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11014. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11015. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11016. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11017. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11018. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11019. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11020. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11021. '']));
  11022. end;
  11023. procedure TTestModule.TestArray_InsertDelete;
  11024. begin
  11025. StartProgram(false);
  11026. Add([
  11027. 'type',
  11028. ' integer = longint;',
  11029. ' TFlag = (big,small);',
  11030. ' TFlags = set of TFlag;',
  11031. ' TRec = record',
  11032. ' i: integer;',
  11033. ' end;',
  11034. ' TArrInt = array of integer;',
  11035. ' TArrRec = array of TRec;',
  11036. ' TArrSet = array of TFlags;',
  11037. ' TArrJSValue = array of jsvalue;',
  11038. ' TArrArrInt = array of TArrInt;',
  11039. 'var',
  11040. ' ArrInt: tarrint;',
  11041. ' ArrRec: tarrrec;',
  11042. ' ArrSet: tarrset;',
  11043. ' ArrJSValue: tarrjsvalue;',
  11044. ' ArrArrInt: TArrArrInt;',
  11045. 'begin',
  11046. ' Insert(1,arrint,2);',
  11047. ' Insert(arrint[3],arrint,4);',
  11048. ' Insert(arrrec[5],arrrec,6);',
  11049. ' Insert(arrset[7],arrset,7);',
  11050. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11051. ' Insert(10,arrjsvalue,11);',
  11052. ' Insert([23],arrarrint,22);',
  11053. ' Delete(arrint,12,13);',
  11054. ' Delete(arrrec,14,15);',
  11055. ' Delete(arrset,17,18);',
  11056. ' Delete(arrjsvalue,19,10);']);
  11057. ConvertProgram;
  11058. CheckSource('TestArray_InsertDelete',
  11059. LinesToStr([ // statements
  11060. 'this.TFlag = {',
  11061. ' "0": "big",',
  11062. ' big: 0,',
  11063. ' "1": "small",',
  11064. ' small: 1',
  11065. '};',
  11066. 'rtl.recNewT(this, "TRec", function () {',
  11067. ' this.i = 0;',
  11068. ' this.$eq = function (b) {',
  11069. ' return this.i === b.i;',
  11070. ' };',
  11071. ' this.$assign = function (s) {',
  11072. ' this.i = s.i;',
  11073. ' return this;',
  11074. ' };',
  11075. '});',
  11076. 'this.ArrInt = [];',
  11077. 'this.ArrRec = [];',
  11078. 'this.ArrSet = [];',
  11079. 'this.ArrJSValue = [];',
  11080. 'this.ArrArrInt = [];',
  11081. '']),
  11082. LinesToStr([ // $mod.$main
  11083. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11084. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11085. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11086. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11087. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11088. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11089. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11090. '$mod.ArrInt.splice(12, 13);',
  11091. '$mod.ArrRec.splice(14, 15);',
  11092. '$mod.ArrSet.splice(17, 18);',
  11093. '$mod.ArrJSValue.splice(19, 10);',
  11094. '']));
  11095. end;
  11096. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11097. begin
  11098. Parser.Options:=Parser.Options+[po_cassignments];
  11099. StartProgram(false);
  11100. Add([
  11101. '{$modeswitch arrayoperators}',
  11102. 'type',
  11103. ' integer = longint;',
  11104. ' TArrInt = array of integer;',
  11105. ' TArrStr = array of string;',
  11106. 'const',
  11107. ' Ints: TArrInt = (1,2,3);',
  11108. ' Aliases: TarrStr = (''foo'',''b'');',
  11109. ' OneInt: TArrInt = (7);',
  11110. ' OneStr: array of integer = (7);',
  11111. ' Chars: array of char = ''aoc'';',
  11112. ' Names: array of string = (''a'',''foo'');',
  11113. ' NameCount = low(Names)+high(Names)+length(Names);',
  11114. 'var i: integer;',
  11115. 'begin',
  11116. ' Ints:=[];',
  11117. ' Ints:=[1,1];',
  11118. ' Ints:=[1]+[2];',
  11119. ' Ints:=[2];',
  11120. ' Ints:=[]+ints;',
  11121. ' Ints:=Ints+[];',
  11122. ' Ints:=Ints+OneInt;',
  11123. ' Ints:=Ints+[1,1];',
  11124. ' Ints:=[i,i]+Ints;',
  11125. ' Ints:=[1]+[i]+[3];',
  11126. '']);
  11127. ConvertProgram;
  11128. CheckSource('TestArray_DynArrayConstObjFPC',
  11129. LinesToStr([ // statements
  11130. 'this.Ints = [1, 2, 3];',
  11131. 'this.Aliases = ["foo", "b"];',
  11132. 'this.OneInt = [7];',
  11133. 'this.OneStr = [7];',
  11134. 'this.Chars = ["a", "o", "c"];',
  11135. 'this.Names = ["a", "foo"];',
  11136. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11137. 'this.i = 0;',
  11138. '']),
  11139. LinesToStr([ // $mod.$main
  11140. '$mod.Ints = [];',
  11141. '$mod.Ints = [1, 1];',
  11142. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11143. '$mod.Ints = [2];',
  11144. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11145. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  11146. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11147. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  11148. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11149. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11150. '']));
  11151. end;
  11152. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11153. begin
  11154. StartProgram(false);
  11155. // Note: const c = [1,1]; defines a set!
  11156. Add([
  11157. '{$mode delphi}',
  11158. 'type',
  11159. ' integer = longint;',
  11160. ' TArrInt = array of integer;',
  11161. ' TArrStr = array of string;',
  11162. 'const',
  11163. ' Ints: TArrInt = [1,1,2];',
  11164. ' Aliases: TarrStr = [''foo'',''b''];',
  11165. ' OneInt: TArrInt = [7];',
  11166. ' OneStr: array of integer = [7]+[8];',
  11167. ' Chars: array of char = ''aoc'';',
  11168. ' Names: array of string = [''a'',''a''];',
  11169. ' NameCount = low(Names)+high(Names)+length(Names);',
  11170. 'begin',
  11171. '']);
  11172. ConvertProgram;
  11173. CheckSource('TestArray_DynArrayConstDelphi',
  11174. LinesToStr([ // statements
  11175. 'this.Ints = [1, 1, 2];',
  11176. 'this.Aliases = ["foo", "b"];',
  11177. 'this.OneInt = [7];',
  11178. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11179. 'this.Chars = ["a", "o", "c"];',
  11180. 'this.Names = ["a", "a"];',
  11181. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11182. '']),
  11183. LinesToStr([ // $mod.$main
  11184. '']));
  11185. end;
  11186. procedure TTestModule.TestArray_ArrayLitAsParam;
  11187. begin
  11188. StartProgram(false);
  11189. Add([
  11190. '{$modeswitch arrayoperators}',
  11191. 'type',
  11192. ' integer = longint;',
  11193. ' TArrInt = array of integer;',
  11194. ' TArrSet = array of (red,green,blue);',
  11195. 'procedure DoOpenInt(const a: array of integer); forward;',
  11196. 'procedure DoInt(const a: TArrInt);',
  11197. 'begin',
  11198. ' DoInt(a+[1]);',
  11199. ' DoInt([1]+a);',
  11200. ' DoOpenInt(a);',
  11201. ' DoOpenInt(a+[1]);',
  11202. ' DoOpenInt([1]+a);',
  11203. 'end;',
  11204. 'procedure DoOpenInt(const a: array of integer);',
  11205. 'begin',
  11206. ' DoOpenInt(a+[1]);',
  11207. ' DoOpenInt([1]+a);',
  11208. ' DoInt(a);',
  11209. ' DoInt(a+[1]);',
  11210. ' DoInt([1]+a);',
  11211. 'end;',
  11212. 'procedure DoSet(const a: TArrSet);',
  11213. 'begin',
  11214. ' DoSet(a+[red]);',
  11215. ' DoSet([blue]+a);',
  11216. 'end;',
  11217. 'var',
  11218. ' i: TArrInt;',
  11219. ' s: TArrSet;',
  11220. 'begin',
  11221. ' DoInt([1]);',
  11222. ' DoInt([1]+[2]);',
  11223. ' DoInt(i+[1]);',
  11224. ' DoInt([1]+i);',
  11225. ' DoOpenInt([1]);',
  11226. ' DoOpenInt([1]+[2]);',
  11227. ' DoOpenInt(i+[1]);',
  11228. ' DoOpenInt([1]+i);',
  11229. ' DoSet([red]);',
  11230. ' DoSet([blue]+[green]);',
  11231. ' DoSet(s+[blue]);',
  11232. ' DoSet([red]+s);',
  11233. '']);
  11234. ConvertProgram;
  11235. CheckSource('TestArray_ArrayLitAsParam',
  11236. LinesToStr([ // statements
  11237. 'this.TArrSet$a = {',
  11238. ' "0": "red",',
  11239. ' red: 0,',
  11240. ' "1": "green",',
  11241. ' green: 1,',
  11242. ' "2": "blue",',
  11243. ' blue: 2',
  11244. '};',
  11245. 'this.DoInt = function (a) {',
  11246. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11247. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11248. ' $mod.DoOpenInt(a);',
  11249. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11250. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11251. '};',
  11252. 'this.DoOpenInt = function (a) {',
  11253. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11254. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11255. ' $mod.DoInt(a);',
  11256. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11257. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11258. '};',
  11259. 'this.DoSet = function (a) {',
  11260. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11261. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11262. '};',
  11263. 'this.i = [];',
  11264. 'this.s = [];',
  11265. '']),
  11266. LinesToStr([ // $mod.$main
  11267. '$mod.DoInt([1]);',
  11268. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11269. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11270. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11271. '$mod.DoOpenInt([1]);',
  11272. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11273. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11274. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11275. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11276. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11277. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11278. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11279. '']));
  11280. end;
  11281. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11282. begin
  11283. StartProgram(false);
  11284. Add([
  11285. '{$modeswitch arrayoperators}',
  11286. 'type',
  11287. ' integer = longint;',
  11288. ' TArrInt = array of integer;',
  11289. ' TArrArrInt = array of TArrInt;',
  11290. 'procedure DoInt(const a: TArrArrInt);',
  11291. 'begin',
  11292. ' DoInt(a+[[1]]);',
  11293. ' DoInt([[1]]+a);',
  11294. ' DoInt(a);',
  11295. 'end;',
  11296. 'var',
  11297. ' i: TArrInt;',
  11298. ' a: TArrArrInt;',
  11299. 'begin',
  11300. ' a:=[[1]];',
  11301. ' a:=[i];',
  11302. ' a:=a+[i];',
  11303. ' a:=[i]+a;',
  11304. ' a:=[[1]+i];',
  11305. ' a:=[[1]+[2]];',
  11306. ' a:=[i+[2]];',
  11307. ' DoInt([[1]]);',
  11308. ' DoInt([[1]+[2],[3,4],[5]]);',
  11309. ' DoInt([i+[1]]+a);',
  11310. ' DoInt([i]+a);',
  11311. '']);
  11312. ConvertProgram;
  11313. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11314. LinesToStr([ // statements
  11315. 'this.DoInt = function (a) {',
  11316. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11317. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11318. ' $mod.DoInt(a);',
  11319. '};',
  11320. 'this.i = [];',
  11321. 'this.a = [];',
  11322. '']),
  11323. LinesToStr([ // $mod.$main
  11324. '$mod.a = [[1]];',
  11325. '$mod.a = [$mod.i];',
  11326. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  11327. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11328. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11329. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11330. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11331. '$mod.DoInt([[1]]);',
  11332. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11333. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11334. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11335. '']));
  11336. end;
  11337. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11338. begin
  11339. StartProgram(false);
  11340. Add([
  11341. '{$modeswitch arrayoperators}',
  11342. 'type',
  11343. ' integer = longint;',
  11344. ' TArrInt = array[1..2] of integer;',
  11345. ' TArrArrInt = array of TArrInt;',
  11346. 'procedure DoInt(const a: TArrArrInt);',
  11347. 'begin',
  11348. ' DoInt(a+[[1,2]]);',
  11349. ' DoInt([[1,2]]+a);',
  11350. ' DoInt(a);',
  11351. 'end;',
  11352. 'var',
  11353. ' i: TArrInt;',
  11354. ' a: TArrArrInt;',
  11355. 'begin',
  11356. ' a:=[[1,1]];',
  11357. ' a:=[i];',
  11358. ' a:=a+[i];',
  11359. ' a:=[i]+a;',
  11360. ' DoInt([[1,1]]);',
  11361. ' DoInt([[1,2],[3,4]]);',
  11362. '']);
  11363. ConvertProgram;
  11364. CheckSource('TestArray_ArrayLitStaticAsParam',
  11365. LinesToStr([ // statements
  11366. 'this.DoInt = function (a) {',
  11367. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  11368. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  11369. ' $mod.DoInt(a);',
  11370. '};',
  11371. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11372. 'this.a = [];',
  11373. '']),
  11374. LinesToStr([ // $mod.$main
  11375. '$mod.a = [[1, 1]];',
  11376. '$mod.a = [$mod.i.slice(0)];',
  11377. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  11378. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  11379. '$mod.DoInt([[1, 1]]);',
  11380. '$mod.DoInt([[1, 2], [3, 4]]);',
  11381. '']));
  11382. end;
  11383. procedure TTestModule.TestArray_ForInArrOfString;
  11384. begin
  11385. StartProgram(false);
  11386. Add([
  11387. 'type',
  11388. 'type',
  11389. ' TMonthNameArray = array [1..12] of string;',
  11390. ' TMonthNames = TMonthNameArray;',
  11391. ' TObject = class',
  11392. ' private',
  11393. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11394. ' public',
  11395. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11396. ' end;',
  11397. 'var',
  11398. ' f: TObject;',
  11399. ' Month: string;',
  11400. ' Names: array of string = (''a'',''foo'',''bar'');',
  11401. ' i: longint;',
  11402. 'begin',
  11403. ' for Month in f.LongMonthNames do ;',
  11404. ' for Month in Names do ;',
  11405. ' for i:=low(Names) to high(Names) do ;',
  11406. '']);
  11407. ConvertProgram;
  11408. CheckSource('TestArray_ForInArrOfString',
  11409. LinesToStr([ // statements
  11410. 'rtl.createClass(this, "TObject", null, function () {',
  11411. ' this.$init = function () {',
  11412. ' };',
  11413. ' this.$final = function () {',
  11414. ' };',
  11415. '});',
  11416. 'this.f = null;',
  11417. 'this.Month = "";',
  11418. 'this.Names = ["a", "foo", "bar"];',
  11419. 'this.i = 0;',
  11420. '']),
  11421. LinesToStr([ // $mod.$main
  11422. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11423. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11424. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11425. '']));
  11426. end;
  11427. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11428. begin
  11429. StartProgram(false);
  11430. Add([
  11431. '{$modeswitch externalclass}',
  11432. 'type',
  11433. ' TJSObject = class external name ''Object''',
  11434. ' end;',
  11435. ' TJSArray = class external name ''Array''',
  11436. ' class function isArray(Value: JSValue) : boolean;',
  11437. ' function concat() : TJSArray; varargs;',
  11438. ' end;',
  11439. 'var',
  11440. ' aObj: TJSArray;',
  11441. ' a: array of longint;',
  11442. ' o: TJSObject;',
  11443. 'begin',
  11444. ' if TJSArray.isArray(65) then ;',
  11445. ' aObj:=TJSArray(a).concat(a);',
  11446. ' o:=TJSObject(a);',
  11447. ' aObj:=TJSArray([''bird'',''ant'']);',
  11448. '']);
  11449. ConvertProgram;
  11450. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11451. LinesToStr([ // statements
  11452. 'this.aObj = null;',
  11453. 'this.a = [];',
  11454. 'this.o = null;',
  11455. '']),
  11456. LinesToStr([ // $mod.$main
  11457. 'if (Array.isArray(65)) ;',
  11458. '$mod.aObj = $mod.a.concat($mod.a);',
  11459. '$mod.o = $mod.a;',
  11460. '$mod.aObj = ["bird", "ant"];',
  11461. '']));
  11462. end;
  11463. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11464. begin
  11465. StartProgram(false);
  11466. Add([
  11467. '{$modeswitch externalclass}',
  11468. 'type',
  11469. ' TArrStr = array of string;',
  11470. ' TJSArray = class external name ''Array''',
  11471. ' end;',
  11472. ' TJSObject = class external name ''Object''',
  11473. ' end;',
  11474. 'var',
  11475. ' aObj: TJSArray;',
  11476. ' a: TArrStr;',
  11477. ' jo: TJSObject;',
  11478. 'begin',
  11479. ' a:=TArrStr(aObj);',
  11480. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11481. ' a:=TarrStr(jo);',
  11482. '']);
  11483. ConvertProgram;
  11484. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11485. LinesToStr([ // statements
  11486. 'this.aObj = null;',
  11487. 'this.a = [];',
  11488. 'this.jo = null;',
  11489. '']),
  11490. LinesToStr([ // $mod.$main
  11491. '$mod.a = $mod.aObj;',
  11492. '$mod.aObj[1] = $mod.aObj[2];',
  11493. '$mod.a = $mod.jo;',
  11494. '']));
  11495. end;
  11496. procedure TTestModule.TestArrayOfConst_TVarRec;
  11497. begin
  11498. StartProgram(true,[supTVarRec]);
  11499. Add([
  11500. 'procedure Say(args: array of const);',
  11501. 'var',
  11502. ' i: longint;',
  11503. ' v: TVarRec;',
  11504. 'begin',
  11505. ' for i:=low(args) to high(args) do begin',
  11506. ' v:=args[i];',
  11507. ' case v.vtype of',
  11508. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11509. ' end;',
  11510. ' end;',
  11511. ' for v in args do ;',
  11512. ' args:=nil;',
  11513. ' SetLength(args,2);',
  11514. 'end;',
  11515. 'begin']);
  11516. ConvertProgram;
  11517. CheckSource('TestArrayOfConst_TVarRec',
  11518. LinesToStr([ // statements
  11519. 'this.Say = function (args) {',
  11520. ' var i = 0;',
  11521. ' var v = pas.system.TVarRec.$new();',
  11522. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11523. ' i = $l;',
  11524. ' v.$assign(args[i]);',
  11525. ' var $tmp = v.VType;',
  11526. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11527. ' };',
  11528. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11529. ' args = [];',
  11530. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11531. '};',
  11532. '']),
  11533. LinesToStr([ // $mod.$main
  11534. ]));
  11535. end;
  11536. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11537. begin
  11538. StartProgram(true,[supTVarRec]);
  11539. Add([
  11540. 'procedure Say(args: array of const);',
  11541. 'begin',
  11542. ' Say(args);',
  11543. 'end;',
  11544. 'var',
  11545. ' p: Pointer;',
  11546. ' j: jsvalue;',
  11547. ' c: currency;',
  11548. 'begin',
  11549. ' Say([]);',
  11550. ' Say([1]);',
  11551. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11552. '']);
  11553. ConvertProgram;
  11554. CheckSource('TestArrayOfConst_PassBaseTypes',
  11555. LinesToStr([ // statements
  11556. 'this.Say = function (args) {',
  11557. ' $mod.Say(args);',
  11558. '};',
  11559. 'this.p = null;',
  11560. 'this.j = undefined;',
  11561. 'this.c = 0;',
  11562. '']),
  11563. LinesToStr([ // $mod.$main
  11564. '$mod.Say([]);',
  11565. '$mod.Say(pas.system.VarRecs(0, 1));',
  11566. '$mod.Say(pas.system.VarRecs(',
  11567. ' 9,',
  11568. ' "c",',
  11569. ' 18,',
  11570. ' "foo",',
  11571. ' 5,',
  11572. ' null,',
  11573. ' 1,',
  11574. ' true,',
  11575. ' 3,',
  11576. ' 1.3,',
  11577. ' 5,',
  11578. ' $mod.p,',
  11579. ' 20,',
  11580. ' $mod.j,',
  11581. ' 12,',
  11582. ' $mod.c',
  11583. ' ));',
  11584. '']));
  11585. end;
  11586. procedure TTestModule.TestArrayOfConst_PassObj;
  11587. begin
  11588. StartProgram(true,[supTVarRec]);
  11589. Add([
  11590. '{$interfaces corba}',
  11591. 'type',
  11592. ' TObject = class',
  11593. ' end;',
  11594. ' TClass = class of TObject;',
  11595. ' IUnknown = interface',
  11596. ' end;',
  11597. 'procedure Say(args: array of const);',
  11598. 'begin',
  11599. 'end;',
  11600. 'var',
  11601. ' o: TObject;',
  11602. ' c: TClass;',
  11603. ' i: IUnknown;',
  11604. 'begin',
  11605. ' Say([o,c,TObject]);',
  11606. ' Say([nil,i]);',
  11607. '']);
  11608. ConvertProgram;
  11609. CheckSource('TestArrayOfConst_PassObj',
  11610. LinesToStr([ // statements
  11611. 'rtl.createClass(this, "TObject", null, function () {',
  11612. ' this.$init = function () {',
  11613. ' };',
  11614. ' this.$final = function () {',
  11615. ' };',
  11616. '});',
  11617. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11618. 'this.Say = function (args) {',
  11619. '};',
  11620. 'this.o = null;',
  11621. 'this.c = null;',
  11622. 'this.i = null;',
  11623. '']),
  11624. LinesToStr([ // $mod.$main
  11625. '$mod.Say(pas.system.VarRecs(',
  11626. ' 7,',
  11627. ' $mod.o,',
  11628. ' 8,',
  11629. ' $mod.c,',
  11630. ' 8,',
  11631. ' $mod.TObject',
  11632. '));',
  11633. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11634. '']));
  11635. end;
  11636. procedure TTestModule.TestRecord_Empty;
  11637. begin
  11638. StartProgram(false);
  11639. Add([
  11640. 'type',
  11641. ' TRecA = record',
  11642. ' end;',
  11643. 'var a,b: TRecA;',
  11644. 'begin',
  11645. ' if a=b then ;']);
  11646. ConvertProgram;
  11647. CheckSource('TestRecord_Empty',
  11648. LinesToStr([ // statements
  11649. 'rtl.recNewT(this, "TRecA", function () {',
  11650. ' this.$eq = function (b) {',
  11651. ' return true;',
  11652. ' };',
  11653. ' this.$assign = function (s) {',
  11654. ' return this;',
  11655. ' };',
  11656. '});',
  11657. 'this.a = this.TRecA.$new();',
  11658. 'this.b = this.TRecA.$new();',
  11659. '']),
  11660. LinesToStr([ // $mod.$main
  11661. 'if ($mod.a.$eq($mod.b)) ;'
  11662. ]));
  11663. end;
  11664. procedure TTestModule.TestRecord_Var;
  11665. begin
  11666. StartProgram(false);
  11667. Add('type');
  11668. Add(' TRecA = record');
  11669. Add(' Bold: longint;');
  11670. Add(' end;');
  11671. Add('var Rec: TRecA;');
  11672. Add('begin');
  11673. Add(' rec.bold:=123');
  11674. ConvertProgram;
  11675. CheckSource('TestRecord_Var',
  11676. LinesToStr([ // statements
  11677. 'rtl.recNewT(this, "TRecA", function () {',
  11678. ' this.Bold = 0;',
  11679. ' this.$eq = function (b) {',
  11680. ' return this.Bold === b.Bold;',
  11681. ' };',
  11682. ' this.$assign = function (s) {',
  11683. ' this.Bold = s.Bold;',
  11684. ' return this;',
  11685. ' };',
  11686. '});',
  11687. 'this.Rec = this.TRecA.$new();',
  11688. '']),
  11689. LinesToStr([ // $mod.$main
  11690. '$mod.Rec.Bold = 123;'
  11691. ]));
  11692. end;
  11693. procedure TTestModule.TestRecord_VarExternal;
  11694. begin
  11695. StartProgram(false);
  11696. Add([
  11697. '{$modeswitch externalclass}',
  11698. 'type',
  11699. ' TRecA = record',
  11700. ' i: byte;',
  11701. ' length_: longint external name ''length'';',
  11702. ' end;',
  11703. 'var Rec: TRecA;',
  11704. 'begin',
  11705. ' rec.length_ := rec.length_',
  11706. '']);
  11707. ConvertProgram;
  11708. CheckSource('TestRecord_VarExternal',
  11709. LinesToStr([ // statements
  11710. 'rtl.recNewT(this, "TRecA", function () {',
  11711. ' this.i = 0;',
  11712. ' this.$eq = function (b) {',
  11713. ' return (this.i === b.i) && (this.length === b.length);',
  11714. ' };',
  11715. ' this.$assign = function (s) {',
  11716. ' this.i = s.i;',
  11717. ' this.length = s.length;',
  11718. ' return this;',
  11719. ' };',
  11720. '});',
  11721. 'this.Rec = this.TRecA.$new();',
  11722. '']),
  11723. LinesToStr([ // $mod.$main
  11724. '$mod.Rec.length = $mod.Rec.length;'
  11725. ]));
  11726. end;
  11727. procedure TTestModule.TestRecord_WithDo;
  11728. begin
  11729. StartProgram(false);
  11730. Add('type');
  11731. Add(' TRec = record');
  11732. Add(' vI: longint;');
  11733. Add(' end;');
  11734. Add('var');
  11735. Add(' Int: longint;');
  11736. Add(' r: TRec;');
  11737. Add('begin');
  11738. Add(' with r do');
  11739. Add(' int:=vi;');
  11740. Add(' with r do begin');
  11741. Add(' int:=vi;');
  11742. Add(' vi:=int;');
  11743. Add(' end;');
  11744. ConvertProgram;
  11745. CheckSource('TestWithRecordDo',
  11746. LinesToStr([ // statements
  11747. 'rtl.recNewT(this, "TRec", function () {',
  11748. ' this.vI = 0;',
  11749. ' this.$eq = function (b) {',
  11750. ' return this.vI === b.vI;',
  11751. ' };',
  11752. ' this.$assign = function (s) {',
  11753. ' this.vI = s.vI;',
  11754. ' return this;',
  11755. ' };',
  11756. '});',
  11757. 'this.Int = 0;',
  11758. 'this.r = this.TRec.$new();',
  11759. '']),
  11760. LinesToStr([ // $mod.$main
  11761. 'var $with = $mod.r;',
  11762. '$mod.Int = $with.vI;',
  11763. 'var $with1 = $mod.r;',
  11764. '$mod.Int = $with1.vI;',
  11765. '$with1.vI = $mod.Int;'
  11766. ]));
  11767. end;
  11768. procedure TTestModule.TestRecord_Assign;
  11769. begin
  11770. StartProgram(false);
  11771. Add([
  11772. 'type',
  11773. ' TEnum = (red,green);',
  11774. ' TEnums = set of TEnum;',
  11775. ' TSmallRec = record',
  11776. ' N: longint;',
  11777. ' end;',
  11778. ' TBigRec = record',
  11779. ' Int: longint;',
  11780. ' D: double;',
  11781. ' Arr: array of longint;',
  11782. ' Arr2: array[1..2] of longint;',
  11783. ' Small: TSmallRec;',
  11784. ' Enums: TEnums;',
  11785. ' end;',
  11786. 'var',
  11787. ' r, s: TBigRec;',
  11788. 'begin',
  11789. ' r:=s;',
  11790. ' r:=default(TBigRec);',
  11791. ' r:=default(s);',
  11792. '']);
  11793. ConvertProgram;
  11794. CheckSource('TestRecord_Assign',
  11795. LinesToStr([ // statements
  11796. 'this.TEnum = {',
  11797. ' "0": "red",',
  11798. ' red: 0,',
  11799. ' "1": "green",',
  11800. ' green: 1',
  11801. '};',
  11802. 'rtl.recNewT(this, "TSmallRec", function () {',
  11803. ' this.N = 0;',
  11804. ' this.$eq = function (b) {',
  11805. ' return this.N === b.N;',
  11806. ' };',
  11807. ' this.$assign = function (s) {',
  11808. ' this.N = s.N;',
  11809. ' return this;',
  11810. ' };',
  11811. '});',
  11812. 'rtl.recNewT(this, "TBigRec", function () {',
  11813. ' this.Int = 0;',
  11814. ' this.D = 0.0;',
  11815. ' this.$new = function () {',
  11816. ' var r = Object.create(this);',
  11817. ' r.Arr = [];',
  11818. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  11819. ' r.Small = $mod.TSmallRec.$new();',
  11820. ' r.Enums = {};',
  11821. ' return r;',
  11822. ' };',
  11823. ' this.$eq = function (b) {',
  11824. ' 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);',
  11825. ' };',
  11826. ' this.$assign = function (s) {',
  11827. ' this.Int = s.Int;',
  11828. ' this.D = s.D;',
  11829. ' this.Arr = rtl.arrayRef(s.Arr);',
  11830. ' this.Arr2 = s.Arr2.slice(0);',
  11831. ' this.Small.$assign(s.Small);',
  11832. ' this.Enums = rtl.refSet(s.Enums);',
  11833. ' return this;',
  11834. ' };',
  11835. '});',
  11836. 'this.r = this.TBigRec.$new();',
  11837. 'this.s = this.TBigRec.$new();',
  11838. '']),
  11839. LinesToStr([ // $mod.$main
  11840. '$mod.r.$assign($mod.s);',
  11841. '$mod.r.$assign($mod.TBigRec.$new());',
  11842. '$mod.r.$assign($mod.TBigRec.$new());',
  11843. '']));
  11844. end;
  11845. procedure TTestModule.TestRecord_AsParams;
  11846. begin
  11847. StartProgram(false);
  11848. Add([
  11849. 'type',
  11850. ' integer = longint;',
  11851. ' TRecord = record',
  11852. ' i: integer;',
  11853. ' end;',
  11854. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  11855. 'var vL: TRecord;',
  11856. 'begin',
  11857. ' vd:=vd;',
  11858. ' vd.i:=vd.i;',
  11859. ' vl:=vc;',
  11860. ' vv:=vv;',
  11861. ' vv.i:=vv.i;',
  11862. ' U:=vl;',
  11863. ' U:=vd;',
  11864. ' U:=vc;',
  11865. ' U:=vv;',
  11866. ' vl:=TRecord(U);',
  11867. ' vd:=TRecord(U);',
  11868. ' vv:=TRecord(U);',
  11869. ' doit(vd,vd,vd,vd);',
  11870. ' doit(vc,vc,vl,vl);',
  11871. ' doit(vv,vv,vv,vv);',
  11872. ' doit(vl,vl,vl,vl);',
  11873. ' TRecord(U).i:=3;',
  11874. 'end;',
  11875. 'var i: TRecord;',
  11876. 'begin',
  11877. ' doit(i,i,i,i);',
  11878. '']);
  11879. ConvertProgram;
  11880. CheckSource('TestRecord_AsParams',
  11881. LinesToStr([ // statements
  11882. 'rtl.recNewT(this, "TRecord", function () {',
  11883. ' this.i = 0;',
  11884. ' this.$eq = function (b) {',
  11885. ' return this.i === b.i;',
  11886. ' };',
  11887. ' this.$assign = function (s) {',
  11888. ' this.i = s.i;',
  11889. ' return this;',
  11890. ' };',
  11891. '});',
  11892. 'this.DoIt = function (vD, vC, vV, U) {',
  11893. ' var vL = $mod.TRecord.$new();',
  11894. ' vD.$assign(vD);',
  11895. ' vD.i = vD.i;',
  11896. ' vL.$assign(vC);',
  11897. ' vV.$assign(vV);',
  11898. ' vV.i = vV.i;',
  11899. ' U.$assign(vL);',
  11900. ' U.$assign(vD);',
  11901. ' U.$assign(vC);',
  11902. ' U.$assign(vV);',
  11903. ' vL.$assign(U);',
  11904. ' vD.$assign(U);',
  11905. ' vV.$assign(U);',
  11906. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  11907. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  11908. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  11909. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  11910. ' U.i = 3;',
  11911. '};',
  11912. 'this.i = this.TRecord.$new();'
  11913. ]),
  11914. LinesToStr([
  11915. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11916. '']));
  11917. end;
  11918. procedure TTestModule.TestRecord_ConstRef;
  11919. begin
  11920. StartProgram(false);
  11921. Add([
  11922. 'type TRec = record i: word; end;',
  11923. 'procedure Run(constref a: TRec);',
  11924. 'begin',
  11925. 'end;',
  11926. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11927. 'var l: TRec;',
  11928. 'begin',
  11929. ' Run(l);',
  11930. ' Run(a);',
  11931. ' Run(b);',
  11932. ' Run(c);',
  11933. ' Run(d);',
  11934. ' Run(e);',
  11935. 'end;',
  11936. 'begin',
  11937. '']);
  11938. ConvertProgram;
  11939. CheckResolverUnexpectedHints();
  11940. CheckSource('TestRecord_ConstRef',
  11941. LinesToStr([ // statements
  11942. 'rtl.recNewT(this, "TRec", function () {',
  11943. ' this.i = 0;',
  11944. ' this.$eq = function (b) {',
  11945. ' return this.i === b.i;',
  11946. ' };',
  11947. ' this.$assign = function (s) {',
  11948. ' this.i = s.i;',
  11949. ' return this;',
  11950. ' };',
  11951. '});',
  11952. 'this.Run = function (a) {',
  11953. '};',
  11954. 'this.Fly = function (a, b, c, d, e) {',
  11955. ' var l = $mod.TRec.$new();',
  11956. ' $mod.Run(l);',
  11957. ' $mod.Run(a);',
  11958. ' $mod.Run(b);',
  11959. ' $mod.Run(c);',
  11960. ' $mod.Run(d);',
  11961. ' $mod.Run(e);',
  11962. '};',
  11963. '']),
  11964. LinesToStr([
  11965. '']));
  11966. end;
  11967. procedure TTestModule.TestRecordElement_AsParams;
  11968. begin
  11969. StartProgram(false);
  11970. Add('type');
  11971. Add(' integer = longint;');
  11972. Add(' TRecord = record');
  11973. Add(' i: integer;');
  11974. Add(' end;');
  11975. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11976. Add('var vJ: TRecord;');
  11977. Add('begin');
  11978. Add(' doit(vj.i,vj.i,vj.i);');
  11979. Add('end;');
  11980. Add('var r: TRecord;');
  11981. Add('begin');
  11982. Add(' doit(r.i,r.i,r.i);');
  11983. ConvertProgram;
  11984. CheckSource('TestRecordElement_AsParams',
  11985. LinesToStr([ // statements
  11986. 'rtl.recNewT(this, "TRecord", function () {',
  11987. ' this.i = 0;',
  11988. ' this.$eq = function (b) {',
  11989. ' return this.i === b.i;',
  11990. ' };',
  11991. ' this.$assign = function (s) {',
  11992. ' this.i = s.i;',
  11993. ' return this;',
  11994. ' };',
  11995. '});',
  11996. 'this.DoIt = function (vG,vH,vI) {',
  11997. ' var vJ = $mod.TRecord.$new();',
  11998. ' $mod.DoIt(vJ.i, vJ.i, {',
  11999. ' p: vJ,',
  12000. ' get: function () {',
  12001. ' return this.p.i;',
  12002. ' },',
  12003. ' set: function (v) {',
  12004. ' this.p.i = v;',
  12005. ' }',
  12006. ' });',
  12007. '};',
  12008. 'this.r = this.TRecord.$new();'
  12009. ]),
  12010. LinesToStr([
  12011. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12012. ' p: $mod.r,',
  12013. ' get: function () {',
  12014. ' return this.p.i;',
  12015. ' },',
  12016. ' set: function (v) {',
  12017. ' this.p.i = v;',
  12018. ' }',
  12019. '});'
  12020. ]));
  12021. end;
  12022. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12023. begin
  12024. StartProgram(false);
  12025. Add('type');
  12026. Add(' integer = longint;');
  12027. Add(' TRecord = record');
  12028. Add(' i: integer;');
  12029. Add(' end;');
  12030. Add('function GetRec(vB: integer = 0): TRecord;');
  12031. Add('begin');
  12032. Add('end;');
  12033. Add('procedure DoIt(vG: integer; const vH: integer);');
  12034. Add('begin');
  12035. Add('end;');
  12036. Add('begin');
  12037. Add(' doit(getrec.i,getrec.i);');
  12038. Add(' doit(getrec().i,getrec().i);');
  12039. Add(' doit(getrec(1).i,getrec(2).i);');
  12040. ConvertProgram;
  12041. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12042. LinesToStr([ // statements
  12043. 'rtl.recNewT(this, "TRecord", function () {',
  12044. ' this.i = 0;',
  12045. ' this.$eq = function (b) {',
  12046. ' return this.i === b.i;',
  12047. ' };',
  12048. ' this.$assign = function (s) {',
  12049. ' this.i = s.i;',
  12050. ' return this;',
  12051. ' };',
  12052. '});',
  12053. 'this.GetRec = function (vB) {',
  12054. ' var Result = $mod.TRecord.$new();',
  12055. ' return Result;',
  12056. '};',
  12057. 'this.DoIt = function (vG, vH) {',
  12058. '};',
  12059. '']),
  12060. LinesToStr([
  12061. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12062. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12063. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12064. '']));
  12065. end;
  12066. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12067. begin
  12068. StartProgram(false);
  12069. Add('type');
  12070. Add(' integer = longint;');
  12071. Add(' TRecord = record');
  12072. Add(' i: integer;');
  12073. Add(' end;');
  12074. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12075. Add('begin');
  12076. Add('end;');
  12077. Add('var r: trecord;');
  12078. Add('begin');
  12079. Add(' with r do ');
  12080. Add(' doit(i,i,i);');
  12081. ConvertProgram;
  12082. CheckSource('TestRecordElementFromWith_AsParams',
  12083. LinesToStr([ // statements
  12084. 'rtl.recNewT(this, "TRecord", function () {',
  12085. ' this.i = 0;',
  12086. ' this.$eq = function (b) {',
  12087. ' return this.i === b.i;',
  12088. ' };',
  12089. ' this.$assign = function (s) {',
  12090. ' this.i = s.i;',
  12091. ' return this;',
  12092. ' };',
  12093. '});',
  12094. 'this.DoIt = function (vG,vH,vI) {',
  12095. '};',
  12096. 'this.r = this.TRecord.$new();'
  12097. ]),
  12098. LinesToStr([
  12099. 'var $with = $mod.r;',
  12100. '$mod.DoIt($with.i,$with.i,{',
  12101. ' p: $with,',
  12102. ' get: function () {',
  12103. ' return this.p.i;',
  12104. ' },',
  12105. ' set: function (v) {',
  12106. ' this.p.i = v;',
  12107. ' }',
  12108. '});',
  12109. '']));
  12110. end;
  12111. procedure TTestModule.TestRecord_Equal;
  12112. begin
  12113. StartProgram(false);
  12114. Add('type');
  12115. Add(' integer = longint;');
  12116. Add(' TFlag = (red,blue);');
  12117. Add(' TFlags = set of TFlag;');
  12118. Add(' TProc = procedure;');
  12119. Add(' TRecord = record');
  12120. Add(' i: integer;');
  12121. Add(' Event: TProc;');
  12122. Add(' f: TFlags;');
  12123. Add(' end;');
  12124. Add(' TNested = record');
  12125. Add(' r: TRecord;');
  12126. Add(' end;');
  12127. Add('var');
  12128. Add(' b: boolean;');
  12129. Add(' r,s: trecord;');
  12130. Add('begin');
  12131. Add(' b:=r=s;');
  12132. Add(' b:=r<>s;');
  12133. ConvertProgram;
  12134. CheckSource('TestRecord_Equal',
  12135. LinesToStr([ // statements
  12136. 'this.TFlag = {',
  12137. ' "0": "red",',
  12138. ' red: 0,',
  12139. ' "1": "blue",',
  12140. ' blue: 1',
  12141. '};',
  12142. 'rtl.recNewT(this, "TRecord", function () {',
  12143. ' this.i = 0;',
  12144. ' this.Event = null;',
  12145. ' this.$new = function () {',
  12146. ' var r = Object.create(this);',
  12147. ' r.f = {};',
  12148. ' return r;',
  12149. ' };',
  12150. ' this.$eq = function (b) {',
  12151. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12152. ' };',
  12153. ' this.$assign = function (s) {',
  12154. ' this.i = s.i;',
  12155. ' this.Event = s.Event;',
  12156. ' this.f = rtl.refSet(s.f);',
  12157. ' return this;',
  12158. ' };',
  12159. '});',
  12160. 'rtl.recNewT(this, "TNested", function () {',
  12161. ' this.$new = function () {',
  12162. ' var r = Object.create(this);',
  12163. ' r.r = $mod.TRecord.$new();',
  12164. ' return r;',
  12165. ' };',
  12166. ' this.$eq = function (b) {',
  12167. ' return this.r.$eq(b.r);',
  12168. ' };',
  12169. ' this.$assign = function (s) {',
  12170. ' this.r.$assign(s.r);',
  12171. ' return this;',
  12172. ' };',
  12173. '});',
  12174. 'this.b = false;',
  12175. 'this.r = this.TRecord.$new();',
  12176. 'this.s = this.TRecord.$new();',
  12177. '']),
  12178. LinesToStr([
  12179. '$mod.b = $mod.r.$eq($mod.s);',
  12180. '$mod.b = !$mod.r.$eq($mod.s);',
  12181. '']));
  12182. end;
  12183. procedure TTestModule.TestRecord_JSValue;
  12184. begin
  12185. StartProgram(false);
  12186. Add([
  12187. 'type',
  12188. ' TRecord = record',
  12189. ' i: longint;',
  12190. ' end;',
  12191. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12192. 'begin',
  12193. 'end;',
  12194. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12195. 'begin',
  12196. ' if jsvalue(d) then ;',
  12197. ' if jsvalue(c) then ;',
  12198. ' if jsvalue(v) then ;',
  12199. 'end;',
  12200. 'var',
  12201. ' Jv: jsvalue;',
  12202. ' Rec: trecord;',
  12203. 'begin',
  12204. ' rec:=trecord(jv);',
  12205. ' jv:=rec;',
  12206. ' Fly(rec,rec);',
  12207. ' Fly(@rec,@rec);',
  12208. ' if jsvalue(Rec) then ;',
  12209. ' Run(trecord(jv),trecord(jv),rec);',
  12210. '']);
  12211. ConvertProgram;
  12212. CheckSource('TestRecord_JSValue',
  12213. LinesToStr([ // statements
  12214. 'rtl.recNewT(this, "TRecord", function () {',
  12215. ' this.i = 0;',
  12216. ' this.$eq = function (b) {',
  12217. ' return this.i === b.i;',
  12218. ' };',
  12219. ' this.$assign = function (s) {',
  12220. ' this.i = s.i;',
  12221. ' return this;',
  12222. ' };',
  12223. '});',
  12224. 'this.Fly = function (d, c) {',
  12225. '};',
  12226. 'this.Run = function (d, c, v) {',
  12227. ' if (d) ;',
  12228. ' if (c) ;',
  12229. ' if (v) ;',
  12230. '};',
  12231. 'this.Jv = undefined;',
  12232. 'this.Rec = this.TRecord.$new();',
  12233. '']),
  12234. LinesToStr([
  12235. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12236. '$mod.Jv = $mod.Rec;',
  12237. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12238. '$mod.Fly($mod.Rec, $mod.Rec);',
  12239. 'if ($mod.Rec) ;',
  12240. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12241. '']));
  12242. end;
  12243. procedure TTestModule.TestRecord_VariantFail;
  12244. begin
  12245. StartProgram(false);
  12246. Add([
  12247. 'type',
  12248. ' TRec = record',
  12249. ' case word of',
  12250. ' 0: (b0, b1: Byte);',
  12251. ' 1: (i: word);',
  12252. ' end;',
  12253. 'begin']);
  12254. SetExpectedPasResolverError('Not supported: variant record',
  12255. nNotSupportedX);
  12256. ConvertProgram;
  12257. end;
  12258. procedure TTestModule.TestRecord_FieldArray;
  12259. begin
  12260. StartProgram(false);
  12261. Add([
  12262. 'type',
  12263. ' TArrInt = array[3..4] of longint;',
  12264. ' TArrArrInt = array[3..4] of longint;',
  12265. ' TRec = record',
  12266. ' a: array of longint;',
  12267. ' s: array[1..2] of longint;',
  12268. ' m: array[1..2,3..4] of longint;',
  12269. ' o: TArrArrInt;',
  12270. ' end;',
  12271. 'begin']);
  12272. ConvertProgram;
  12273. CheckSource('TestRecord_FieldArray',
  12274. LinesToStr([ // statements
  12275. 'rtl.recNewT(this, "TRec", function () {',
  12276. ' this.m$a$clone = function (a) {',
  12277. ' var b = [];',
  12278. ' b.length = 2;',
  12279. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12280. ' return b;',
  12281. ' };',
  12282. ' this.$new = function () {',
  12283. ' var r = Object.create(this);',
  12284. ' r.a = [];',
  12285. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12286. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12287. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12288. ' return r;',
  12289. ' };',
  12290. ' this.$eq = function (b) {',
  12291. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12292. ' };',
  12293. ' this.$assign = function (s) {',
  12294. ' this.a = rtl.arrayRef(s.a);',
  12295. ' this.s = s.s.slice(0);',
  12296. ' this.m = this.m$a$clone(s.m);',
  12297. ' this.o = s.o.slice(0);',
  12298. ' return this;',
  12299. ' };',
  12300. '});',
  12301. '']),
  12302. LinesToStr([ // $mod.$main
  12303. '']));
  12304. end;
  12305. procedure TTestModule.TestRecord_Const;
  12306. begin
  12307. StartProgram(false);
  12308. Add([
  12309. 'type',
  12310. ' TArrInt = array[3..4] of longint;',
  12311. ' TPoint = record x,y: longint; end;',
  12312. ' TRec = record',
  12313. ' i: longint;',
  12314. ' a: array of longint;',
  12315. ' s: array[1..2] of longint;',
  12316. ' m: array[1..2,3..4] of longint;',
  12317. ' p: TPoint;',
  12318. ' end;',
  12319. ' TPoints = array of TPoint;',
  12320. 'const',
  12321. ' r: TRec = (',
  12322. ' i:1;',
  12323. ' a:(2,3);',
  12324. ' s:(4,5);',
  12325. ' m:( (11,12), (13,14) );',
  12326. ' p: (x:21; y:22)',
  12327. ' );',
  12328. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12329. 'begin']);
  12330. ConvertProgram;
  12331. CheckSource('TestRecord_Const',
  12332. LinesToStr([ // statements
  12333. 'rtl.recNewT(this, "TPoint", function () {',
  12334. ' this.x = 0;',
  12335. ' this.y = 0;',
  12336. ' this.$eq = function (b) {',
  12337. ' return (this.x === b.x) && (this.y === b.y);',
  12338. ' };',
  12339. ' this.$assign = function (s) {',
  12340. ' this.x = s.x;',
  12341. ' this.y = s.y;',
  12342. ' return this;',
  12343. ' };',
  12344. '});',
  12345. 'rtl.recNewT(this, "TRec", function () {',
  12346. ' this.i = 0;',
  12347. ' this.m$a$clone = function (a) {',
  12348. ' var b = [];',
  12349. ' b.length = 2;',
  12350. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12351. ' return b;',
  12352. ' };',
  12353. ' this.$new = function () {',
  12354. ' var r = Object.create(this);',
  12355. ' r.a = [];',
  12356. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12357. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12358. ' r.p = $mod.TPoint.$new();',
  12359. ' return r;',
  12360. ' };',
  12361. ' this.$eq = function (b) {',
  12362. ' 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);',
  12363. ' };',
  12364. ' this.$assign = function (s) {',
  12365. ' this.i = s.i;',
  12366. ' this.a = rtl.arrayRef(s.a);',
  12367. ' this.s = s.s.slice(0);',
  12368. ' this.m = this.m$a$clone(s.m);',
  12369. ' this.p.$assign(s.p);',
  12370. ' return this;',
  12371. ' };',
  12372. '});',
  12373. 'this.r = this.TRec.$clone({',
  12374. ' i: 1,',
  12375. ' a: [2, 3],',
  12376. ' s: [4, 5],',
  12377. ' m: [[11, 12], [13, 14]],',
  12378. ' p: this.TPoint.$clone({',
  12379. ' x: 21,',
  12380. ' y: 22',
  12381. ' })',
  12382. '});',
  12383. 'this.p = [this.TPoint.$clone({',
  12384. ' x: 1,',
  12385. ' y: 2',
  12386. '}), this.TPoint.$clone({',
  12387. ' x: 3,',
  12388. ' y: 4',
  12389. '})];',
  12390. '']),
  12391. LinesToStr([ // $mod.$main
  12392. '']));
  12393. end;
  12394. procedure TTestModule.TestRecord_TypecastFail;
  12395. begin
  12396. StartProgram(false);
  12397. Add([
  12398. 'type',
  12399. ' TPoint = record x,y: longint; end;',
  12400. ' TRec = record l: longint end;',
  12401. 'var p: TPoint;',
  12402. 'begin',
  12403. ' if TRec(p).l=2 then ;']);
  12404. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12405. nIllegalTypeConversionTo);
  12406. ConvertProgram;
  12407. end;
  12408. procedure TTestModule.TestRecord_InFunction;
  12409. begin
  12410. StartProgram(false);
  12411. Add([
  12412. 'var TPoint: longint = 3;',
  12413. 'procedure DoIt;',
  12414. 'type',
  12415. ' TPoint = record x,y: longint; end;',
  12416. ' TPoints = array of TPoint;',
  12417. 'var',
  12418. ' r: TPoint;',
  12419. ' p: TPoints;',
  12420. 'begin',
  12421. ' SetLength(p,2);',
  12422. 'end;',
  12423. 'begin']);
  12424. ConvertProgram;
  12425. CheckSource('TestRecord_InFunction',
  12426. LinesToStr([ // statements
  12427. 'this.TPoint = 3;',
  12428. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12429. ' this.x = 0;',
  12430. ' this.y = 0;',
  12431. ' this.$eq = function (b) {',
  12432. ' return (this.x === b.x) && (this.y === b.y);',
  12433. ' };',
  12434. ' this.$assign = function (s) {',
  12435. ' this.x = s.x;',
  12436. ' this.y = s.y;',
  12437. ' return this;',
  12438. ' };',
  12439. '});',
  12440. 'this.DoIt = function () {',
  12441. ' var r = TPoint$1.$new();',
  12442. ' var p = [];',
  12443. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12444. '};',
  12445. '']),
  12446. LinesToStr([ // $mod.$main
  12447. '']));
  12448. end;
  12449. procedure TTestModule.TestRecordAnonym_Field;
  12450. begin
  12451. StartProgram(false);
  12452. Add(['',
  12453. 'var Rec: record',
  12454. ' Bold: longint;',
  12455. ' end;',
  12456. 'begin',
  12457. ' rec.bold:=123;',
  12458. ' rec.bold:=rec.bold+7;',
  12459. '']);
  12460. ConvertProgram;
  12461. CheckSource('TestRecordAnonym_Field',
  12462. LinesToStr([ // statements
  12463. 'rtl.recNewT(this, "Rec$a", function () {',
  12464. ' this.Bold = 0;',
  12465. ' this.$eq = function (b) {',
  12466. ' return this.Bold === b.Bold;',
  12467. ' };',
  12468. ' this.$assign = function (s) {',
  12469. ' this.Bold = s.Bold;',
  12470. ' return this;',
  12471. ' };',
  12472. '});',
  12473. 'this.Rec = this.Rec$a.$new();',
  12474. '']),
  12475. LinesToStr([ // $mod.$main
  12476. '$mod.Rec.Bold = 123;',
  12477. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12478. '']));
  12479. end;
  12480. procedure TTestModule.TestRecordAnonym_Assign;
  12481. begin
  12482. StartProgram(false);
  12483. Add(['',
  12484. 'var S,T: record',
  12485. ' Bold: longint;',
  12486. ' end;',
  12487. ' b: boolean;',
  12488. 'begin',
  12489. ' S:=T;',
  12490. ' b:=s=t;',
  12491. '']);
  12492. ConvertProgram;
  12493. CheckSource('TestRecordAnonym_Assign',
  12494. LinesToStr([ // statements
  12495. 'rtl.recNewT(this, "T$a", function () {',
  12496. ' this.Bold = 0;',
  12497. ' this.$eq = function (b) {',
  12498. ' return this.Bold === b.Bold;',
  12499. ' };',
  12500. ' this.$assign = function (s) {',
  12501. ' this.Bold = s.Bold;',
  12502. ' return this;',
  12503. ' };',
  12504. '});',
  12505. 'this.S = this.T$a.$new();',
  12506. 'this.T = this.T$a.$new();',
  12507. 'this.b = false;',
  12508. '']),
  12509. LinesToStr([ // $mod.$main
  12510. '$mod.S.$assign($mod.T);',
  12511. '$mod.b = $mod.S.$eq($mod.T);',
  12512. '']));
  12513. end;
  12514. procedure TTestModule.TestRecordAnonym_Nested;
  12515. begin
  12516. StartProgram(false);
  12517. Add(['',
  12518. 'var S,T: record',
  12519. ' Bold: longint;',
  12520. ' Sub: record',
  12521. ' Color: word;',
  12522. ' end;',
  12523. ' end;',
  12524. ' b: boolean;',
  12525. 'begin',
  12526. ' S:=T;',
  12527. ' S.Sub:=T.Sub;',
  12528. ' S.Sub.Color:=T.Sub.Color+3;',
  12529. ' b:=s=t;',
  12530. ' b:=s.Sub=t.Sub;',
  12531. '']);
  12532. ConvertProgram;
  12533. CheckSource('TestRecordAnonym_Nested',
  12534. LinesToStr([ // statements
  12535. 'rtl.recNewT(this, "T$a", function () {',
  12536. ' this.Bold = 0;',
  12537. ' rtl.recNewT(this, "Sub$a", function () {',
  12538. ' this.Color = 0;',
  12539. ' this.$eq = function (b) {',
  12540. ' return this.Color === b.Color;',
  12541. ' };',
  12542. ' this.$assign = function (s) {',
  12543. ' this.Color = s.Color;',
  12544. ' return this;',
  12545. ' };',
  12546. ' });',
  12547. ' this.$new = function () {',
  12548. ' var r = Object.create(this);',
  12549. ' r.Sub = this.Sub$a.$new();',
  12550. ' return r;',
  12551. ' };',
  12552. ' this.$eq = function (b) {',
  12553. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12554. ' };',
  12555. ' this.$assign = function (s) {',
  12556. ' this.Bold = s.Bold;',
  12557. ' this.Sub.$assign(s.Sub);',
  12558. ' return this;',
  12559. ' };',
  12560. '}, true);',
  12561. 'this.S = this.T$a.$new();',
  12562. 'this.T = this.T$a.$new();',
  12563. 'this.b = false;',
  12564. '']),
  12565. LinesToStr([ // $mod.$main
  12566. '$mod.S.$assign($mod.T);',
  12567. '$mod.S.Sub.$assign($mod.T.Sub);',
  12568. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12569. '$mod.b = $mod.S.$eq($mod.T);',
  12570. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12571. '']));
  12572. end;
  12573. procedure TTestModule.TestRecordAnonym_Const;
  12574. begin
  12575. StartProgram(false);
  12576. Add(['',
  12577. 'var T: record',
  12578. ' Bold: longint;',
  12579. ' Sub: record',
  12580. ' Color: word;',
  12581. ' end;',
  12582. ' end = (Bold: 2; Sub: (Color: 3));',
  12583. 'begin',
  12584. '']);
  12585. ConvertProgram;
  12586. CheckSource('TestRecordAnonym_Const',
  12587. LinesToStr([ // statements
  12588. 'rtl.recNewT(this, "T$a", function () {',
  12589. ' this.Bold = 0;',
  12590. ' rtl.recNewT(this, "Sub$a", function () {',
  12591. ' this.Color = 0;',
  12592. ' this.$eq = function (b) {',
  12593. ' return this.Color === b.Color;',
  12594. ' };',
  12595. ' this.$assign = function (s) {',
  12596. ' this.Color = s.Color;',
  12597. ' return this;',
  12598. ' };',
  12599. ' });',
  12600. ' this.$new = function () {',
  12601. ' var r = Object.create(this);',
  12602. ' r.Sub = this.Sub$a.$new();',
  12603. ' return r;',
  12604. ' };',
  12605. ' this.$eq = function (b) {',
  12606. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12607. ' };',
  12608. ' this.$assign = function (s) {',
  12609. ' this.Bold = s.Bold;',
  12610. ' this.Sub.$assign(s.Sub);',
  12611. ' return this;',
  12612. ' };',
  12613. '}, true);',
  12614. 'this.T = this.T$a.$clone({',
  12615. ' Bold: 2,',
  12616. ' Sub: this.T$a.Sub$a.$clone({',
  12617. ' Color: 3',
  12618. ' })',
  12619. '});',
  12620. '']),
  12621. LinesToStr([ // $mod.$main
  12622. '']));
  12623. end;
  12624. procedure TTestModule.TestRecordAnonym_InFunction;
  12625. begin
  12626. StartProgram(false);
  12627. Add(['',
  12628. 'procedure Fly;',
  12629. 'var T: record',
  12630. ' Bold: longint;',
  12631. ' Sub: record',
  12632. ' Color: word;',
  12633. ' end;',
  12634. ' end = (Bold: 2; Sub: (Color: 3));',
  12635. 'begin',
  12636. 'end;',
  12637. 'begin',
  12638. '']);
  12639. ConvertProgram;
  12640. CheckSource('TestRecordAnonym_InFunction',
  12641. LinesToStr([ // statements
  12642. 'var T$a = rtl.recNewT(null, "", function () {',
  12643. ' this.Bold = 0;',
  12644. ' rtl.recNewT(this, "Sub$a", function () {',
  12645. ' this.Color = 0;',
  12646. ' this.$eq = function (b) {',
  12647. ' return this.Color === b.Color;',
  12648. ' };',
  12649. ' this.$assign = function (s) {',
  12650. ' this.Color = s.Color;',
  12651. ' return this;',
  12652. ' };',
  12653. ' });',
  12654. ' this.$new = function () {',
  12655. ' var r = Object.create(this);',
  12656. ' r.Sub = this.Sub$a.$new();',
  12657. ' return r;',
  12658. ' };',
  12659. ' this.$eq = function (b) {',
  12660. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12661. ' };',
  12662. ' this.$assign = function (s) {',
  12663. ' this.Bold = s.Bold;',
  12664. ' this.Sub.$assign(s.Sub);',
  12665. ' return this;',
  12666. ' };',
  12667. '}, true);',
  12668. 'this.Fly = function () {',
  12669. ' var T = T$a.$clone({',
  12670. ' Bold: 2,',
  12671. ' Sub: T$a.Sub$a.$clone({',
  12672. ' Color: 3',
  12673. ' })',
  12674. ' });',
  12675. '};',
  12676. '']),
  12677. LinesToStr([ // $mod.$main
  12678. '']));
  12679. end;
  12680. procedure TTestModule.TestAdvRecord_Function;
  12681. begin
  12682. StartProgram(false);
  12683. Parser.Options:=Parser.Options+[po_cassignments];
  12684. Add([
  12685. '{$modeswitch AdvancedRecords}',
  12686. 'type',
  12687. ' TPoint = record',
  12688. ' x,y: word;',
  12689. ' function Add(const apt: TPoint): TPoint;',
  12690. ' end;',
  12691. 'function TPoint.Add(const apt: TPoint): TPoint;',
  12692. 'begin',
  12693. ' Result:=Self;',
  12694. ' Result.x+=apt.x;',
  12695. ' Result.y:=Result.y+apt.y;',
  12696. ' Self:=apt;',
  12697. 'end;',
  12698. 'var p,q: TPoint;',
  12699. 'begin',
  12700. ' p.add(q);',
  12701. ' p:=default(TPoint);',
  12702. ' p:=q;',
  12703. '']);
  12704. ConvertProgram;
  12705. CheckSource('TestAdvRecord_Function',
  12706. LinesToStr([ // statements
  12707. 'rtl.recNewT(this, "TPoint", function () {',
  12708. ' this.x = 0;',
  12709. ' this.y = 0;',
  12710. ' this.$eq = function (b) {',
  12711. ' return (this.x === b.x) && (this.y === b.y);',
  12712. ' };',
  12713. ' this.$assign = function (s) {',
  12714. ' this.x = s.x;',
  12715. ' this.y = s.y;',
  12716. ' return this;',
  12717. ' };',
  12718. ' this.Add = function (apt) {',
  12719. ' var Result = $mod.TPoint.$new();',
  12720. ' Result.$assign(this);',
  12721. ' Result.x += apt.x;',
  12722. ' Result.y = Result.y + apt.y;',
  12723. ' this.$assign(apt);',
  12724. ' return Result;',
  12725. ' };',
  12726. '});',
  12727. 'this.p = this.TPoint.$new();',
  12728. 'this.q = this.TPoint.$new();',
  12729. '']),
  12730. LinesToStr([ // $mod.$main
  12731. '$mod.p.Add($mod.q);',
  12732. '$mod.p.$assign($mod.TPoint.$new());',
  12733. '$mod.p.$assign($mod.q);',
  12734. '']));
  12735. end;
  12736. procedure TTestModule.TestAdvRecord_Property;
  12737. begin
  12738. StartProgram(false);
  12739. Add([
  12740. '{$modeswitch AdvancedRecords}',
  12741. 'type',
  12742. ' TPoint = record',
  12743. ' x,y: word;',
  12744. ' strict private',
  12745. ' function GetSize: longword;',
  12746. ' procedure SetSize(Value: longword);',
  12747. ' public',
  12748. ' property Size: longword read GetSize write SetSize;',
  12749. ' property Left: word read x write y;',
  12750. ' end;',
  12751. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  12752. 'function TPoint.GetSize: longword;',
  12753. 'begin',
  12754. ' x:=y;',
  12755. ' Size:=Size;',
  12756. ' Left:=Left;',
  12757. 'end;',
  12758. 'procedure TPoint.SetSize(Value: longword);',
  12759. 'begin',
  12760. 'end;',
  12761. 'var p,q: TPoint;',
  12762. 'begin',
  12763. ' p.Size:=q.Size;',
  12764. ' p.Left:=q.Left;',
  12765. '']);
  12766. ConvertProgram;
  12767. CheckSource('TestAdvRecord_Property',
  12768. LinesToStr([ // statements
  12769. 'rtl.recNewT(this, "TPoint", function () {',
  12770. ' this.x = 0;',
  12771. ' this.y = 0;',
  12772. ' this.$eq = function (b) {',
  12773. ' return (this.x === b.x) && (this.y === b.y);',
  12774. ' };',
  12775. ' this.$assign = function (s) {',
  12776. ' this.x = s.x;',
  12777. ' this.y = s.y;',
  12778. ' return this;',
  12779. ' };',
  12780. ' this.GetSize = function () {',
  12781. ' var Result = 0;',
  12782. ' this.x = this.y;',
  12783. ' this.SetSize(this.GetSize());',
  12784. ' this.y = this.x;',
  12785. ' return Result;',
  12786. ' };',
  12787. ' this.SetSize = function (Value) {',
  12788. ' };',
  12789. '});',
  12790. 'this.SetSize = function (Value) {',
  12791. '};',
  12792. 'this.p = this.TPoint.$new();',
  12793. 'this.q = this.TPoint.$new();',
  12794. '']),
  12795. LinesToStr([ // $mod.$main
  12796. '$mod.p.SetSize($mod.q.GetSize());',
  12797. '$mod.p.y = $mod.q.x;',
  12798. '']));
  12799. end;
  12800. procedure TTestModule.TestAdvRecord_PropertyDefault;
  12801. begin
  12802. StartProgram(false);
  12803. Add([
  12804. '{$modeswitch AdvancedRecords}',
  12805. 'type',
  12806. ' TPoint = record',
  12807. ' strict private',
  12808. ' function GetItems(Index: word): word;',
  12809. ' procedure SetItems(Index: word; Value: word);',
  12810. ' public',
  12811. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  12812. ' end;',
  12813. 'function TPoint.GetItems(Index: word): word;',
  12814. 'begin',
  12815. ' Items[index]:=Items[index];',
  12816. ' self.Items[index]:=self.Items[index];',
  12817. 'end;',
  12818. 'procedure TPoint.SetItems(Index: word; Value: word);',
  12819. 'begin',
  12820. 'end;',
  12821. 'var p: TPoint;',
  12822. 'begin',
  12823. ' p[1]:=p[2];',
  12824. ' p.Items[3]:=p.Items[4];',
  12825. '']);
  12826. ConvertProgram;
  12827. CheckSource('TestAdvRecord_PropertyDefault',
  12828. LinesToStr([ // statements
  12829. 'rtl.recNewT(this, "TPoint", function () {',
  12830. ' this.$eq = function (b) {',
  12831. ' return true;',
  12832. ' };',
  12833. ' this.$assign = function (s) {',
  12834. ' return this;',
  12835. ' };',
  12836. ' this.GetItems = function (Index) {',
  12837. ' var Result = 0;',
  12838. ' this.SetItems(Index, this.GetItems(Index));',
  12839. ' this.SetItems(Index, this.GetItems(Index));',
  12840. ' return Result;',
  12841. ' };',
  12842. ' this.SetItems = function (Index, Value) {',
  12843. ' };',
  12844. '});',
  12845. 'this.p = this.TPoint.$new();',
  12846. '']),
  12847. LinesToStr([ // $mod.$main
  12848. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  12849. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  12850. '']));
  12851. end;
  12852. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  12853. begin
  12854. StartProgram(false);
  12855. Add([
  12856. '{$modeswitch AdvancedRecords}',
  12857. 'type',
  12858. ' TRec = record',
  12859. ' class var',
  12860. ' Fx: longint;',
  12861. ' Fy: longint;',
  12862. ' class function GetInt: longint; static;',
  12863. ' class procedure SetInt(Value: longint); static;',
  12864. ' class procedure DoIt; static;',
  12865. ' class property IntA: longint read Fx write Fy;',
  12866. ' class property IntB: longint read GetInt write SetInt;',
  12867. ' end;',
  12868. 'class function trec.getint: longint;',
  12869. 'begin',
  12870. ' result:=fx;',
  12871. 'end;',
  12872. 'class procedure trec.setint(value: longint);',
  12873. 'begin',
  12874. 'end;',
  12875. 'class procedure trec.doit;',
  12876. 'begin',
  12877. ' IntA:=IntA+1;',
  12878. ' IntB:=IntB+1;',
  12879. 'end;',
  12880. 'var r: trec;',
  12881. 'begin',
  12882. ' trec.inta:=trec.inta+1;',
  12883. ' if trec.intb=2 then;',
  12884. ' trec.intb:=trec.intb+2;',
  12885. ' trec.setint(trec.inta);',
  12886. ' r.inta:=r.inta+1;',
  12887. ' if r.intb=2 then;',
  12888. ' r.intb:=r.intb+2;',
  12889. ' r.setint(r.inta);']);
  12890. ConvertProgram;
  12891. CheckSource('TestAdvRecord_Property_ClassMethod',
  12892. LinesToStr([ // statements
  12893. 'rtl.recNewT(this, "TRec", function () {',
  12894. ' this.Fx = 0;',
  12895. ' this.Fy = 0;',
  12896. ' this.$eq = function (b) {',
  12897. ' return true;',
  12898. ' };',
  12899. ' this.$assign = function (s) {',
  12900. ' return this;',
  12901. ' };',
  12902. ' this.GetInt = function () {',
  12903. ' var Result = 0;',
  12904. ' Result = $mod.TRec.Fx;',
  12905. ' return Result;',
  12906. ' };',
  12907. ' this.SetInt = function (Value) {',
  12908. ' };',
  12909. ' this.DoIt = function () {',
  12910. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12911. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  12912. ' };',
  12913. '}, true);',
  12914. 'this.r = this.TRec.$new();',
  12915. '']),
  12916. LinesToStr([ // $mod.$main
  12917. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12918. 'if ($mod.TRec.GetInt() === 2) ;',
  12919. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12920. '$mod.TRec.SetInt($mod.TRec.Fx);',
  12921. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  12922. 'if ($mod.TRec.GetInt() === 2) ;',
  12923. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12924. '$mod.TRec.SetInt($mod.r.Fx);',
  12925. '']));
  12926. end;
  12927. procedure TTestModule.TestAdvRecord_Const;
  12928. begin
  12929. StartProgram(false);
  12930. Add([
  12931. '{$modeswitch AdvancedRecords}',
  12932. 'type',
  12933. ' TArrInt = array[3..4] of longint;',
  12934. ' TPoint = record',
  12935. ' x,y: longint;',
  12936. ' class var Count: nativeint;',
  12937. ' end;',
  12938. ' TRec = record',
  12939. ' i: longint;',
  12940. ' a: array of longint;',
  12941. ' s: array[1..2] of longint;',
  12942. ' m: array[1..2,3..4] of longint;',
  12943. ' p: TPoint;',
  12944. ' end;',
  12945. ' TPoints = array of TPoint;',
  12946. 'const',
  12947. ' r: TRec = (',
  12948. ' i:1;',
  12949. ' a:(2,3);',
  12950. ' s:(4,5);',
  12951. ' m:( (11,12), (13,14) );',
  12952. ' p: (x:21)',
  12953. ' );',
  12954. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12955. 'begin']);
  12956. ConvertProgram;
  12957. CheckSource('TestAdvRecord_Const',
  12958. LinesToStr([ // statements
  12959. 'rtl.recNewT(this, "TPoint", function () {',
  12960. ' this.x = 0;',
  12961. ' this.y = 0;',
  12962. ' this.Count = 0;',
  12963. ' this.$eq = function (b) {',
  12964. ' return (this.x === b.x) && (this.y === b.y);',
  12965. ' };',
  12966. ' this.$assign = function (s) {',
  12967. ' this.x = s.x;',
  12968. ' this.y = s.y;',
  12969. ' return this;',
  12970. ' };',
  12971. '}, true);',
  12972. 'rtl.recNewT(this, "TRec", function () {',
  12973. ' this.i = 0;',
  12974. ' this.m$a$clone = function (a) {',
  12975. ' var b = [];',
  12976. ' b.length = 2;',
  12977. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12978. ' return b;',
  12979. ' };',
  12980. ' this.$new = function () {',
  12981. ' var r = Object.create(this);',
  12982. ' r.a = [];',
  12983. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12984. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12985. ' r.p = $mod.TPoint.$new();',
  12986. ' return r;',
  12987. ' };',
  12988. ' this.$eq = function (b) {',
  12989. ' 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);',
  12990. ' };',
  12991. ' this.$assign = function (s) {',
  12992. ' this.i = s.i;',
  12993. ' this.a = rtl.arrayRef(s.a);',
  12994. ' this.s = s.s.slice(0);',
  12995. ' this.m = this.m$a$clone(s.m);',
  12996. ' this.p.$assign(s.p);',
  12997. ' return this;',
  12998. ' };',
  12999. '});',
  13000. 'this.r = this.TRec.$clone({',
  13001. ' i: 1,',
  13002. ' a: [2, 3],',
  13003. ' s: [4, 5],',
  13004. ' m: [[11, 12], [13, 14]],',
  13005. ' p: this.TPoint.$clone({',
  13006. ' x: 21,',
  13007. ' y: 0',
  13008. ' })',
  13009. '});',
  13010. 'this.p = [this.TPoint.$clone({',
  13011. ' x: 1,',
  13012. ' y: 2',
  13013. '}), this.TPoint.$clone({',
  13014. ' x: 3,',
  13015. ' y: 4',
  13016. '})];',
  13017. '']),
  13018. LinesToStr([ // $mod.$main
  13019. '']));
  13020. end;
  13021. procedure TTestModule.TestAdvRecord_ExternalField;
  13022. begin
  13023. StartProgram(false);
  13024. Add([
  13025. '{$modeswitch AdvancedRecords}',
  13026. '{$modeswitch externalclass}',
  13027. 'type',
  13028. ' TCar = record',
  13029. ' public',
  13030. ' Intern: longint external name ''$Intern'';',
  13031. ' Intern2: longint external name ''$Intern2'';',
  13032. ' Bracket: longint external name ''["A B"]'';',
  13033. ' procedure DoIt;',
  13034. ' end;',
  13035. 'procedure tcar.doit;',
  13036. 'begin',
  13037. ' Intern:=Intern+1;',
  13038. ' Intern2:=Intern2+2;',
  13039. ' Bracket:=Bracket+3;',
  13040. 'end;',
  13041. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13042. 'begin',
  13043. ' Rec.intern:=Rec.intern+1;',
  13044. ' Rec.intern2:=Rec.intern2+2;',
  13045. ' Rec.Bracket:=Rec.Bracket+3;',
  13046. ' with Rec do begin',
  13047. ' intern:=intern+1;',
  13048. ' intern2:=intern2+2;',
  13049. ' Bracket:=Bracket+3;',
  13050. ' end;']);
  13051. ConvertProgram;
  13052. CheckSource('TestAdvRecord_ExternalField',
  13053. LinesToStr([ // statements
  13054. 'rtl.recNewT(this, "TCar", function () {',
  13055. ' this.$eq = function (b) {',
  13056. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13057. ' };',
  13058. ' this.$assign = function (s) {',
  13059. ' this.$Intern = s.$Intern;',
  13060. ' this.$Intern2 = s.$Intern2;',
  13061. ' this["A B"] = s["A B"];',
  13062. ' return this;',
  13063. ' };',
  13064. ' this.DoIt = function () {',
  13065. ' this.$Intern = this.$Intern + 1;',
  13066. ' this.$Intern2 = this.$Intern2 + 2;',
  13067. ' this["A B"] = this["A B"] + 3;',
  13068. ' };',
  13069. '});',
  13070. 'this.Rec = this.TCar.$clone({',
  13071. ' $Intern: 11,',
  13072. ' $Intern2: 12,',
  13073. ' "A B": 13',
  13074. '});',
  13075. '']),
  13076. LinesToStr([ // $mod.$main
  13077. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13078. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13079. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13080. 'var $with = $mod.Rec;',
  13081. '$with.$Intern = $with.$Intern + 1;',
  13082. '$with.$Intern2 = $with.$Intern2 + 2;',
  13083. '$with["A B"] = $with["A B"] + 3;',
  13084. '']));
  13085. end;
  13086. procedure TTestModule.TestAdvRecord_SubRecord;
  13087. begin
  13088. StartProgram(false);
  13089. Add([
  13090. '{$modeswitch AdvancedRecords}',
  13091. 'type',
  13092. ' TRec = record',
  13093. ' type',
  13094. ' TPoint = record',
  13095. ' x,y: longint;',
  13096. ' class var Count: nativeint;',
  13097. ' procedure DoIt;',
  13098. ' class procedure DoThat; static;',
  13099. ' end;',
  13100. ' var',
  13101. ' i: longint;',
  13102. ' p: TPoint;',
  13103. ' procedure DoSome;',
  13104. ' end;',
  13105. 'const',
  13106. ' r: TRec = (',
  13107. ' i:1;',
  13108. ' p: (x:21;y:22)',
  13109. ' );',
  13110. 'procedure TRec.DoSome;',
  13111. 'begin',
  13112. ' p.x:=p.y+1;',
  13113. ' p.Count:=p.Count+2;',
  13114. 'end;',
  13115. 'procedure TRec.TPoint.DoIt;',
  13116. 'begin',
  13117. ' Count:=Count+3;',
  13118. 'end;',
  13119. 'class procedure TRec.TPoint.DoThat;',
  13120. 'begin',
  13121. ' Count:=Count+4;',
  13122. 'end;',
  13123. 'begin']);
  13124. ConvertProgram;
  13125. CheckSource('TestAdvRecord_SubRecord',
  13126. LinesToStr([ // statements
  13127. 'rtl.recNewT(this, "TRec", function () {',
  13128. ' rtl.recNewT(this, "TPoint", function () {',
  13129. ' this.x = 0;',
  13130. ' this.y = 0;',
  13131. ' this.Count = 0;',
  13132. ' this.$eq = function (b) {',
  13133. ' return (this.x === b.x) && (this.y === b.y);',
  13134. ' };',
  13135. ' this.$assign = function (s) {',
  13136. ' this.x = s.x;',
  13137. ' this.y = s.y;',
  13138. ' return this;',
  13139. ' };',
  13140. ' this.DoIt = function () {',
  13141. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13142. ' };',
  13143. ' this.DoThat = function () {',
  13144. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13145. ' };',
  13146. ' }, true);',
  13147. ' this.i = 0;',
  13148. ' this.$new = function () {',
  13149. ' var r = Object.create(this);',
  13150. ' r.p = this.TPoint.$new();',
  13151. ' return r;',
  13152. ' };',
  13153. ' this.$eq = function (b) {',
  13154. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13155. ' };',
  13156. ' this.$assign = function (s) {',
  13157. ' this.i = s.i;',
  13158. ' this.p.$assign(s.p);',
  13159. ' return this;',
  13160. ' };',
  13161. ' this.DoSome = function () {',
  13162. ' this.p.x = this.p.y + 1;',
  13163. ' this.TPoint.Count = this.p.Count + 2;',
  13164. ' };',
  13165. '}, true);',
  13166. 'this.r = this.TRec.$clone({',
  13167. ' i: 1,',
  13168. ' p: this.TRec.TPoint.$clone({',
  13169. ' x: 21,',
  13170. ' y: 22',
  13171. ' })',
  13172. '});',
  13173. '']),
  13174. LinesToStr([ // $mod.$main
  13175. '']));
  13176. end;
  13177. procedure TTestModule.TestAdvRecord_SubClass;
  13178. begin
  13179. StartProgram(false);
  13180. Add([
  13181. '{$modeswitch AdvancedRecords}',
  13182. 'type',
  13183. ' TObject = class end;',
  13184. ' TPoint = record',
  13185. ' type',
  13186. ' TBird = class',
  13187. ' procedure DoIt;',
  13188. ' class procedure Glob;',
  13189. ' end;',
  13190. ' procedure DoIt(b: TBird);',
  13191. ' end;',
  13192. 'procedure TPoint.TBird.DoIt;',
  13193. 'begin',
  13194. ' doit;',
  13195. ' self.doit;',
  13196. ' glob;',
  13197. ' self.glob;',
  13198. 'end;',
  13199. 'class procedure TPoint.TBird.Glob;',
  13200. 'begin',
  13201. ' glob;',
  13202. ' self.glob;',
  13203. 'end;',
  13204. 'procedure TPoint.DoIt(b: TBird);',
  13205. 'begin',
  13206. ' b.doit;',
  13207. ' b.glob;',
  13208. ' TBird.glob;',
  13209. 'end;',
  13210. 'begin',
  13211. '']);
  13212. ConvertProgram;
  13213. CheckSource('TestAdvRecord_SubClass',
  13214. LinesToStr([ // statements
  13215. 'rtl.createClass(this, "TObject", null, function () {',
  13216. ' this.$init = function () {',
  13217. ' };',
  13218. ' this.$final = function () {',
  13219. ' };',
  13220. '});',
  13221. 'rtl.recNewT(this, "TPoint", function () {',
  13222. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13223. ' this.DoIt = function () {',
  13224. ' this.DoIt();',
  13225. ' this.DoIt();',
  13226. ' this.$class.Glob();',
  13227. ' this.$class.Glob();',
  13228. ' };',
  13229. ' this.Glob = function () {',
  13230. ' this.Glob();',
  13231. ' this.Glob();',
  13232. ' };',
  13233. ' }, "TPoint.TBird");',
  13234. ' this.$eq = function (b) {',
  13235. ' return true;',
  13236. ' };',
  13237. ' this.$assign = function (s) {',
  13238. ' return this;',
  13239. ' };',
  13240. ' this.DoIt = function (b) {',
  13241. ' b.DoIt();',
  13242. ' b.$class.Glob();',
  13243. ' this.TBird.Glob();',
  13244. ' };',
  13245. '}, true);',
  13246. '']),
  13247. LinesToStr([ // $mod.$main
  13248. '']));
  13249. end;
  13250. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13251. begin
  13252. StartProgram(false);
  13253. Add([
  13254. '{$modeswitch AdvancedRecords}',
  13255. 'type',
  13256. ' IUnknown = interface end;',
  13257. ' TPoint = record',
  13258. ' type IBird = interface end;',
  13259. ' end;',
  13260. 'begin',
  13261. '']);
  13262. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13263. nNotYetImplemented);
  13264. ParseProgram;
  13265. end;
  13266. procedure TTestModule.TestAdvRecord_Constructor;
  13267. begin
  13268. StartProgram(false);
  13269. Add([
  13270. '{$modeswitch AdvancedRecords}',
  13271. 'type',
  13272. ' TPoint = record',
  13273. ' x,y: longint;',
  13274. ' class procedure Run(w: longint = 13); static;',
  13275. ' constructor Create(ax: longint; ay: longint = -1);',
  13276. ' end;',
  13277. 'class procedure tpoint.run(w: longint);',
  13278. 'begin',
  13279. ' run;',
  13280. ' run();',
  13281. 'end;',
  13282. 'constructor tpoint.create(ax,ay: longint);',
  13283. 'begin',
  13284. ' x:=ax;',
  13285. ' self.y:=ay;',
  13286. ' run;',
  13287. ' run(ax);',
  13288. 'end;',
  13289. 'var r: TPoint;',
  13290. 'begin',
  13291. ' r:=TPoint.Create(1,2);',
  13292. ' with TPoint do r:=Create(1,2);',
  13293. ' r.Create(3);',
  13294. ' r:=r.Create(4);',
  13295. '']);
  13296. ConvertProgram;
  13297. CheckSource('TestAdvRecord_Constructor',
  13298. LinesToStr([ // statements
  13299. 'rtl.recNewT(this, "TPoint", function () {',
  13300. ' this.x = 0;',
  13301. ' this.y = 0;',
  13302. ' this.$eq = function (b) {',
  13303. ' return (this.x === b.x) && (this.y === b.y);',
  13304. ' };',
  13305. ' this.$assign = function (s) {',
  13306. ' this.x = s.x;',
  13307. ' this.y = s.y;',
  13308. ' return this;',
  13309. ' };',
  13310. ' this.Run = function (w) {',
  13311. ' $mod.TPoint.Run(13);',
  13312. ' $mod.TPoint.Run(13);',
  13313. ' };',
  13314. ' this.Create = function (ax, ay) {',
  13315. ' this.x = ax;',
  13316. ' this.y = ay;',
  13317. ' this.Run(13);',
  13318. ' this.Run(ax);',
  13319. ' return this;',
  13320. ' };',
  13321. '});',
  13322. 'this.r = this.TPoint.$new();',
  13323. '']),
  13324. LinesToStr([ // $mod.$main
  13325. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13326. 'var $with = $mod.TPoint;',
  13327. '$mod.r.$assign($with.$new().Create(1, 2));',
  13328. '$mod.r.Create(3, -1);',
  13329. '$mod.r.$assign($mod.r.Create(4, -1));',
  13330. '']));
  13331. end;
  13332. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13333. begin
  13334. StartProgram(false);
  13335. Add([
  13336. '{$modeswitch AdvancedRecords}',
  13337. 'type',
  13338. ' TPoint = record',
  13339. ' class var x: longint;',
  13340. ' class procedure Fly; static;',
  13341. ' class constructor Init;',
  13342. ' end;',
  13343. 'var count: word;',
  13344. 'class procedure Tpoint.Fly;',
  13345. 'begin',
  13346. 'end;',
  13347. 'class constructor tpoint.init;',
  13348. 'begin',
  13349. ' count:=count+1;',
  13350. ' x:=x+3;',
  13351. ' tpoint.x:=tpoint.x+4;',
  13352. ' fly;',
  13353. ' tpoint.fly;',
  13354. 'end;',
  13355. 'var r: TPoint;',
  13356. 'begin',
  13357. ' r.x:=r.x+10;',
  13358. ' r.Fly;',
  13359. ' r.Fly();',
  13360. '']);
  13361. ConvertProgram;
  13362. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13363. LinesToStr([ // statements
  13364. 'rtl.recNewT(this, "TPoint", function () {',
  13365. ' this.x = 0;',
  13366. ' this.$eq = function (b) {',
  13367. ' return true;',
  13368. ' };',
  13369. ' this.$assign = function (s) {',
  13370. ' return this;',
  13371. ' };',
  13372. ' this.Fly = function () {',
  13373. ' };',
  13374. '}, true);',
  13375. 'this.count = 0;',
  13376. 'this.r = this.TPoint.$new();',
  13377. '']),
  13378. LinesToStr([ // $mod.$main
  13379. '(function () {',
  13380. ' $mod.count = $mod.count + 1;',
  13381. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13382. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13383. ' $mod.TPoint.Fly();',
  13384. ' $mod.TPoint.Fly();',
  13385. '})();',
  13386. '$mod.TPoint.x = $mod.r.x + 10;',
  13387. '$mod.TPoint.Fly();',
  13388. '$mod.TPoint.Fly();',
  13389. '']));
  13390. end;
  13391. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13392. begin
  13393. StartUnit(false);
  13394. Add([
  13395. 'interface',
  13396. '{$modeswitch AdvancedRecords}',
  13397. 'type',
  13398. ' TPoint = record',
  13399. ' class var x: longint;',
  13400. ' class procedure Fly; static;',
  13401. ' class constructor Init;',
  13402. ' end;',
  13403. 'implementation',
  13404. 'var count: word;',
  13405. 'class procedure Tpoint.Fly;',
  13406. 'begin',
  13407. 'end;',
  13408. 'class constructor tpoint.init;',
  13409. 'begin',
  13410. ' count:=count+1;',
  13411. ' x:=3;',
  13412. ' tpoint.x:=4;',
  13413. ' fly;',
  13414. ' tpoint.fly;',
  13415. 'end;',
  13416. '']);
  13417. ConvertUnit;
  13418. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13419. LinesToStr([ // statements
  13420. 'var $impl = $mod.$impl;',
  13421. 'rtl.recNewT(this, "TPoint", function () {',
  13422. ' this.x = 0;',
  13423. ' this.$eq = function (b) {',
  13424. ' return true;',
  13425. ' };',
  13426. ' this.$assign = function (s) {',
  13427. ' return this;',
  13428. ' };',
  13429. ' this.Fly = function () {',
  13430. ' };',
  13431. '}, true);',
  13432. '']),
  13433. LinesToStr([ // $mod.$init
  13434. '(function () {',
  13435. ' $impl.count = $impl.count + 1;',
  13436. ' $mod.TPoint.x = 3;',
  13437. ' $mod.TPoint.x = 4;',
  13438. ' $mod.TPoint.Fly();',
  13439. ' $mod.TPoint.Fly();',
  13440. '})();',
  13441. '']),
  13442. LinesToStr([ // $mod.$main
  13443. '$impl.count = 0;',
  13444. '']));
  13445. end;
  13446. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13447. begin
  13448. StartProgram(false);
  13449. Add(['type',
  13450. ' TObject = class',
  13451. ' public',
  13452. ' constructor Create;',
  13453. ' destructor Destroy;',
  13454. ' end;',
  13455. ' TBird = TObject;',
  13456. 'constructor tobject.create;',
  13457. 'begin end;',
  13458. 'destructor tobject.destroy;',
  13459. 'begin end;',
  13460. 'var Obj: tobject;',
  13461. 'begin',
  13462. ' obj:=tobject.create;',
  13463. ' obj:=tobject.create();',
  13464. ' obj:=tbird.create;',
  13465. ' obj:=tbird.create();',
  13466. ' obj:=obj.create();',
  13467. ' obj.destroy;',
  13468. '']);
  13469. ConvertProgram;
  13470. CheckSource('TestClass_TObjectDefaultConstructor',
  13471. LinesToStr([ // statements
  13472. 'rtl.createClass(this,"TObject",null,function(){',
  13473. ' this.$init = function () {',
  13474. ' };',
  13475. ' this.$final = function () {',
  13476. ' };',
  13477. ' this.Create = function(){',
  13478. ' return this;',
  13479. ' };',
  13480. ' this.Destroy = function(){',
  13481. ' };',
  13482. '});',
  13483. 'this.Obj = null;'
  13484. ]),
  13485. LinesToStr([ // $mod.$main
  13486. '$mod.Obj = $mod.TObject.$create("Create");',
  13487. '$mod.Obj = $mod.TObject.$create("Create");',
  13488. '$mod.Obj = $mod.TObject.$create("Create");',
  13489. '$mod.Obj = $mod.TObject.$create("Create");',
  13490. '$mod.Obj = $mod.Obj.Create();',
  13491. '$mod.Obj.$destroy("Destroy");',
  13492. '']));
  13493. end;
  13494. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13495. begin
  13496. StartProgram(false);
  13497. Add('type');
  13498. Add(' TObject = class');
  13499. Add(' public');
  13500. Add(' constructor Create(Par: longint);');
  13501. Add(' end;');
  13502. Add('constructor tobject.create(par: longint);');
  13503. Add('begin end;');
  13504. Add('var Obj: tobject;');
  13505. Add('begin');
  13506. Add(' obj:=tobject.create(3);');
  13507. ConvertProgram;
  13508. CheckSource('TestClass_TObjectConstructorWithParams',
  13509. LinesToStr([ // statements
  13510. 'rtl.createClass(this,"TObject",null,function(){',
  13511. ' this.$init = function () {',
  13512. ' };',
  13513. ' this.$final = function () {',
  13514. ' };',
  13515. ' this.Create = function(Par){',
  13516. ' return this;',
  13517. ' };',
  13518. '});',
  13519. 'this.Obj = null;'
  13520. ]),
  13521. LinesToStr([ // $mod.$main
  13522. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13523. ]));
  13524. end;
  13525. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13526. begin
  13527. StartProgram(false);
  13528. Add('type');
  13529. Add(' TObject = class');
  13530. Add(' public');
  13531. Add(' constructor Create;');
  13532. Add(' end;');
  13533. Add(' TTest = class(TObject)');
  13534. Add(' public');
  13535. Add(' constructor Create(const Par: longint = 1);');
  13536. Add(' end;');
  13537. Add('constructor tobject.create;');
  13538. Add('begin end;');
  13539. Add('constructor ttest.create(const par: longint);');
  13540. Add('begin end;');
  13541. Add('var t: ttest;');
  13542. Add('begin');
  13543. Add(' t:=ttest.create;');
  13544. Add(' t:=ttest.create(2);');
  13545. ConvertProgram;
  13546. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13547. LinesToStr([ // statements
  13548. 'rtl.createClass(this,"TObject",null,function(){',
  13549. ' this.$init = function () {',
  13550. ' };',
  13551. ' this.$final = function () {',
  13552. ' };',
  13553. ' this.Create = function(){',
  13554. ' return this;',
  13555. ' };',
  13556. '});',
  13557. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13558. ' this.Create$1 = function (Par) {',
  13559. ' return this;',
  13560. ' };',
  13561. '});',
  13562. 'this.t = null;'
  13563. ]),
  13564. LinesToStr([ // $mod.$main
  13565. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13566. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13567. ]));
  13568. end;
  13569. procedure TTestModule.TestClass_Var;
  13570. begin
  13571. StartProgram(false);
  13572. Add([
  13573. 'type',
  13574. ' TObject = class',
  13575. ' public',
  13576. ' vI: longint;',
  13577. ' constructor Create(Par: longint);',
  13578. ' end;',
  13579. 'constructor tobject.create(par: longint);',
  13580. 'begin',
  13581. ' vi:=par+3',
  13582. 'end;',
  13583. 'var Obj: tobject;',
  13584. 'begin',
  13585. ' obj:=tobject.create(4);',
  13586. ' obj.vi:=obj.VI+5;']);
  13587. ConvertProgram;
  13588. CheckSource('TestClass_Var',
  13589. LinesToStr([ // statements
  13590. 'rtl.createClass(this,"TObject",null,function(){',
  13591. ' this.$init = function () {',
  13592. ' this.vI = 0;',
  13593. ' };',
  13594. ' this.$final = function () {',
  13595. ' };',
  13596. ' this.Create = function(Par){',
  13597. ' this.vI = Par+3;',
  13598. ' return this;',
  13599. ' };',
  13600. '});',
  13601. 'this.Obj = null;'
  13602. ]),
  13603. LinesToStr([ // $mod.$main
  13604. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13605. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13606. ]));
  13607. end;
  13608. procedure TTestModule.TestClass_Method;
  13609. begin
  13610. StartProgram(false);
  13611. Add('type');
  13612. Add(' TObject = class');
  13613. Add(' public');
  13614. Add(' vI: longint;');
  13615. Add(' Sub: TObject;');
  13616. Add(' constructor Create;');
  13617. Add(' function GetIt(Par: longint): tobject;');
  13618. Add(' end;');
  13619. Add('constructor tobject.create; begin end;');
  13620. Add('function tobject.getit(par: longint): tobject;');
  13621. Add('begin');
  13622. Add(' Self.vi:=par+3;');
  13623. Add(' Result:=self.sub;');
  13624. Add('end;');
  13625. Add('var Obj: tobject;');
  13626. Add('begin');
  13627. Add(' obj:=tobject.create;');
  13628. Add(' obj.getit(4);');
  13629. Add(' obj.sub.sub:=nil;');
  13630. Add(' obj.sub.getit(5);');
  13631. Add(' obj.sub.getit(6).SUB:=nil;');
  13632. Add(' obj.sub.getit(7).GETIT(8);');
  13633. Add(' obj.sub.getit(9).SuB.getit(10);');
  13634. ConvertProgram;
  13635. CheckSource('TestClass_Method',
  13636. LinesToStr([ // statements
  13637. 'rtl.createClass(this,"TObject",null,function(){',
  13638. ' this.$init = function () {',
  13639. ' this.vI = 0;',
  13640. ' this.Sub = null;',
  13641. ' };',
  13642. ' this.$final = function () {',
  13643. ' this.Sub = undefined;',
  13644. ' };',
  13645. ' this.Create = function(){',
  13646. ' return this;',
  13647. ' };',
  13648. ' this.GetIt = function(Par){',
  13649. ' var Result = null;',
  13650. ' this.vI = Par + 3;',
  13651. ' Result = this.Sub;',
  13652. ' return Result;',
  13653. ' };',
  13654. '});',
  13655. 'this.Obj = null;'
  13656. ]),
  13657. LinesToStr([ // $mod.$main
  13658. '$mod.Obj = $mod.TObject.$create("Create");',
  13659. '$mod.Obj.GetIt(4);',
  13660. '$mod.Obj.Sub.Sub=null;',
  13661. '$mod.Obj.Sub.GetIt(5);',
  13662. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  13663. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  13664. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  13665. ]));
  13666. end;
  13667. procedure TTestModule.TestClass_Implementation;
  13668. begin
  13669. StartUnit(false);
  13670. Add([
  13671. 'interface',
  13672. 'type',
  13673. ' TObject = class',
  13674. ' constructor Create;',
  13675. ' end;',
  13676. 'implementation',
  13677. 'type',
  13678. ' TIntClass = class',
  13679. ' constructor Create; reintroduce;',
  13680. ' class procedure DoGlob;',
  13681. ' end;',
  13682. 'constructor tintclass.create;',
  13683. 'begin',
  13684. ' inherited;',
  13685. ' inherited create;',
  13686. ' doglob;',
  13687. 'end;',
  13688. 'class procedure tintclass.doglob;',
  13689. 'begin',
  13690. 'end;',
  13691. 'constructor tobject.create;',
  13692. 'var',
  13693. ' iC: tintclass;',
  13694. 'begin',
  13695. ' ic:=tintclass.create;',
  13696. ' tintclass.doglob;',
  13697. ' ic.doglob;',
  13698. 'end;',
  13699. 'initialization',
  13700. ' tintclass.doglob;',
  13701. '']);
  13702. ConvertUnit;
  13703. CheckSource('TestClass_Implementation',
  13704. LinesToStr([ // statements
  13705. 'var $impl = $mod.$impl;',
  13706. 'rtl.createClass(this, "TObject", null, function () {',
  13707. ' this.$init = function () {',
  13708. ' };',
  13709. ' this.$final = function () {',
  13710. ' };',
  13711. ' this.Create = function () {',
  13712. ' var iC = null;',
  13713. ' iC = $impl.TIntClass.$create("Create$1");',
  13714. ' $impl.TIntClass.DoGlob();',
  13715. ' iC.$class.DoGlob();',
  13716. ' return this;',
  13717. ' };',
  13718. '});',
  13719. '']),
  13720. LinesToStr([ // $mod.$main
  13721. '$impl.TIntClass.DoGlob();',
  13722. '']),
  13723. LinesToStr([
  13724. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  13725. ' this.Create$1 = function () {',
  13726. ' $mod.TObject.Create.call(this);',
  13727. ' $mod.TObject.Create.call(this);',
  13728. ' this.$class.DoGlob();',
  13729. ' return this;',
  13730. ' };',
  13731. ' this.DoGlob = function () {',
  13732. ' };',
  13733. '});',
  13734. '']));
  13735. end;
  13736. procedure TTestModule.TestClass_Inheritance;
  13737. begin
  13738. StartProgram(false);
  13739. Add('type');
  13740. Add(' TObject = class');
  13741. Add(' public');
  13742. Add(' constructor Create;');
  13743. Add(' end;');
  13744. Add(' TClassA = class');
  13745. Add(' end;');
  13746. Add(' TClassB = class(TObject)');
  13747. Add(' procedure ProcB;');
  13748. Add(' end;');
  13749. Add('constructor tobject.create; begin end;');
  13750. Add('procedure tclassb.procb; begin end;');
  13751. Add('var');
  13752. Add(' oO: TObject;');
  13753. Add(' oA: TClassA;');
  13754. Add(' oB: TClassB;');
  13755. Add('begin');
  13756. Add(' oO:=tobject.Create;');
  13757. Add(' oA:=tclassa.Create;');
  13758. Add(' ob:=tclassb.Create;');
  13759. Add(' if oo is tclassa then ;');
  13760. Add(' ob:=oo as tclassb;');
  13761. Add(' (oo as tclassb).procb;');
  13762. ConvertProgram;
  13763. CheckSource('TestClass_Inheritance',
  13764. LinesToStr([ // statements
  13765. 'rtl.createClass(this,"TObject",null,function(){',
  13766. ' this.$init = function () {',
  13767. ' };',
  13768. ' this.$final = function () {',
  13769. ' };',
  13770. ' this.Create = function () {',
  13771. ' return this;',
  13772. ' };',
  13773. '});',
  13774. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  13775. '});',
  13776. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  13777. ' this.ProcB = function () {',
  13778. ' };',
  13779. '});',
  13780. 'this.oO = null;',
  13781. 'this.oA = null;',
  13782. 'this.oB = null;'
  13783. ]),
  13784. LinesToStr([ // $mod.$main
  13785. '$mod.oO = $mod.TObject.$create("Create");',
  13786. '$mod.oA = $mod.TClassA.$create("Create");',
  13787. '$mod.oB = $mod.TClassB.$create("Create");',
  13788. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  13789. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  13790. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  13791. ]));
  13792. end;
  13793. procedure TTestModule.TestClass_TypeAlias;
  13794. begin
  13795. StartProgram(false);
  13796. Add([
  13797. '{$interfaces corba}',
  13798. 'type',
  13799. ' IObject = interface',
  13800. ' end;',
  13801. ' IBird = type IObject;',
  13802. ' TObject = class',
  13803. ' end;',
  13804. ' TBird = type TObject;',
  13805. 'var',
  13806. ' oObj: TObject;',
  13807. ' oBird: TBird;',
  13808. ' IntfObj: IObject;',
  13809. ' IntfBird: IBird;',
  13810. 'begin',
  13811. ' oObj:=oBird;',
  13812. '']);
  13813. ConvertProgram;
  13814. CheckSource('TestClass_TypeAlias',
  13815. LinesToStr([ // statements
  13816. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  13817. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  13818. 'rtl.createClass(this, "TObject", null, function () {',
  13819. ' this.$init = function () {',
  13820. ' };',
  13821. ' this.$final = function () {',
  13822. ' };',
  13823. '});',
  13824. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13825. '});',
  13826. 'this.oObj = null;',
  13827. 'this.oBird = null;',
  13828. 'this.IntfObj = null;',
  13829. 'this.IntfBird = null;',
  13830. '']),
  13831. LinesToStr([ // $mod.$main
  13832. '$mod.oObj = $mod.oBird;',
  13833. '']));
  13834. end;
  13835. procedure TTestModule.TestClass_AbstractMethod;
  13836. begin
  13837. StartProgram(false);
  13838. Add('type');
  13839. Add(' TObject = class');
  13840. Add(' public');
  13841. Add(' procedure DoIt; virtual; abstract;');
  13842. Add(' end;');
  13843. Add('begin');
  13844. ConvertProgram;
  13845. CheckSource('TestClass_AbstractMethod',
  13846. LinesToStr([ // statements
  13847. 'rtl.createClass(this,"TObject",null,function(){',
  13848. ' this.$init = function () {',
  13849. ' };',
  13850. ' this.$final = function () {',
  13851. ' };',
  13852. '});'
  13853. ]),
  13854. LinesToStr([ // this.$main
  13855. ''
  13856. ]));
  13857. end;
  13858. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  13859. begin
  13860. StartProgram(false);
  13861. Add([
  13862. 'type',
  13863. ' TObject = class',
  13864. ' procedure DoAbstract; virtual; abstract;',
  13865. ' procedure DoVirtual; virtual;',
  13866. ' procedure DoIt;',
  13867. ' end;',
  13868. ' TA = class',
  13869. ' procedure doabstract; override;',
  13870. ' procedure dovirtual; override;',
  13871. ' procedure DoSome;',
  13872. ' end;',
  13873. 'procedure tobject.dovirtual;',
  13874. 'begin',
  13875. ' inherited; // call non existing ancestor -> ignore silently',
  13876. 'end;',
  13877. 'procedure tobject.doit;',
  13878. 'begin',
  13879. 'end;',
  13880. 'procedure ta.doabstract;',
  13881. 'begin',
  13882. ' inherited dovirtual; // call TObject.DoVirtual',
  13883. 'end;',
  13884. 'procedure ta.dovirtual;',
  13885. 'begin',
  13886. ' inherited; // call TObject.DoVirtual',
  13887. ' inherited dovirtual; // call TObject.DoVirtual',
  13888. ' inherited dovirtual(); // call TObject.DoVirtual',
  13889. ' doit;',
  13890. ' doit();',
  13891. 'end;',
  13892. 'procedure ta.dosome;',
  13893. 'begin',
  13894. ' inherited; // call non existing ancestor method -> silently ignore',
  13895. 'end;',
  13896. 'begin']);
  13897. ConvertProgram;
  13898. CheckSource('TestClass_CallInherited_ProcNoParams',
  13899. LinesToStr([ // statements
  13900. 'rtl.createClass(this,"TObject",null,function(){',
  13901. ' this.$init = function () {',
  13902. ' };',
  13903. ' this.$final = function () {',
  13904. ' };',
  13905. ' this.DoVirtual = function () {',
  13906. ' };',
  13907. ' this.DoIt = function () {',
  13908. ' };',
  13909. '});',
  13910. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13911. ' this.DoAbstract = function () {',
  13912. ' $mod.TObject.DoVirtual.call(this);',
  13913. ' };',
  13914. ' this.DoVirtual = function () {',
  13915. ' $mod.TObject.DoVirtual.call(this);',
  13916. ' $mod.TObject.DoVirtual.call(this);',
  13917. ' $mod.TObject.DoVirtual.call(this);',
  13918. ' this.DoIt();',
  13919. ' this.DoIt();',
  13920. ' };',
  13921. ' this.DoSome = function () {',
  13922. ' };',
  13923. '});'
  13924. ]),
  13925. LinesToStr([ // this.$main
  13926. ''
  13927. ]));
  13928. end;
  13929. procedure TTestModule.TestClass_CallInherited_WithParams;
  13930. begin
  13931. StartProgram(false);
  13932. Add([
  13933. 'type',
  13934. ' TObject = class',
  13935. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  13936. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  13937. ' procedure DoIt(pA: longint; pB: longint = 0);',
  13938. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  13939. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13940. ' end;',
  13941. ' TClassA = class',
  13942. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  13943. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  13944. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13945. ' end;',
  13946. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  13947. 'begin',
  13948. 'end;',
  13949. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  13950. 'begin',
  13951. 'end;',
  13952. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  13953. 'begin',
  13954. 'end;',
  13955. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  13956. 'begin',
  13957. 'end;',
  13958. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  13959. 'begin',
  13960. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13961. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13962. 'end;',
  13963. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  13964. 'begin',
  13965. ' inherited; // call TObject.DoVirtual(pA,pB)',
  13966. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13967. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13968. ' doit(pa,pb);',
  13969. ' doit(pa);',
  13970. ' doit2(pa);',
  13971. ' doit2;',
  13972. 'end;',
  13973. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  13974. 'begin',
  13975. ' pa:=inherited;',
  13976. 'end;',
  13977. 'begin']);
  13978. ConvertProgram;
  13979. CheckSource('TestClass_CallInherited_WithParams',
  13980. LinesToStr([ // statements
  13981. 'rtl.createClass(this,"TObject",null,function(){',
  13982. ' this.$init = function () {',
  13983. ' };',
  13984. ' this.$final = function () {',
  13985. ' };',
  13986. ' this.DoVirtual = function (pA,pB) {',
  13987. ' };',
  13988. ' this.DoIt = function (pA,pB) {',
  13989. ' };',
  13990. ' this.DoIt2 = function (pA,pB) {',
  13991. ' };',
  13992. ' this.GetIt = function (pA, pB) {',
  13993. ' var Result = 0;',
  13994. ' return Result;',
  13995. ' };',
  13996. '});',
  13997. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  13998. ' this.DoAbstract = function (pA,pB) {',
  13999. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14000. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14001. ' };',
  14002. ' this.DoVirtual = function (pA,pB) {',
  14003. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14004. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14005. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14006. ' this.DoIt(pA,pB);',
  14007. ' this.DoIt(pA,0);',
  14008. ' this.DoIt2(pA,2);',
  14009. ' this.DoIt2(1,2);',
  14010. ' };',
  14011. ' this.GetIt$1 = function (pA, pB) {',
  14012. ' var Result = 0;',
  14013. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14014. ' return Result;',
  14015. ' };',
  14016. '});'
  14017. ]),
  14018. LinesToStr([ // this.$main
  14019. ''
  14020. ]));
  14021. end;
  14022. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14023. begin
  14024. StartProgram(false);
  14025. Add('type');
  14026. Add(' TObject = class');
  14027. Add(' constructor Create; virtual;');
  14028. Add(' constructor CreateWithB(b: boolean);');
  14029. Add(' end;');
  14030. Add(' TA = class');
  14031. Add(' constructor Create; override;');
  14032. Add(' constructor CreateWithC(c: char);');
  14033. Add(' procedure DoIt;');
  14034. Add(' class function DoSome: TObject;');
  14035. Add(' end;');
  14036. Add('constructor tobject.create;');
  14037. Add('begin');
  14038. Add(' inherited; // call non existing ancestor -> ignore silently');
  14039. Add('end;');
  14040. Add('constructor tobject.createwithb(b: boolean);');
  14041. Add('begin');
  14042. Add(' inherited; // call non existing ancestor -> ignore silently');
  14043. Add(' create; // normal call');
  14044. Add('end;');
  14045. Add('constructor ta.create;');
  14046. Add('begin');
  14047. Add(' inherited; // normal call TObject.Create');
  14048. Add(' inherited create; // normal call TObject.Create');
  14049. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14050. Add('end;');
  14051. Add('constructor ta.createwithc(c: char);');
  14052. Add('begin');
  14053. Add(' inherited create; // call TObject.Create');
  14054. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14055. Add(' doit;');
  14056. Add(' doit();');
  14057. Add(' dosome;');
  14058. Add('end;');
  14059. Add('procedure ta.doit;');
  14060. Add('begin');
  14061. Add(' create; // normal call');
  14062. Add(' createwithb(false); // normal call');
  14063. Add(' createwithc(''c''); // normal call');
  14064. Add('end;');
  14065. Add('class function ta.dosome: TObject;');
  14066. Add('begin');
  14067. Add(' Result:=create; // constructor');
  14068. Add(' Result:=createwithb(true); // constructor');
  14069. Add(' Result:=createwithc(''c''); // constructor');
  14070. Add('end;');
  14071. Add('begin');
  14072. ConvertProgram;
  14073. CheckSource('TestClass_CallInheritedConstructor',
  14074. LinesToStr([ // statements
  14075. 'rtl.createClass(this,"TObject",null,function(){',
  14076. ' this.$init = function () {',
  14077. ' };',
  14078. ' this.$final = function () {',
  14079. ' };',
  14080. ' this.Create = function () {',
  14081. ' return this;',
  14082. ' };',
  14083. ' this.CreateWithB = function (b) {',
  14084. ' this.Create();',
  14085. ' return this;',
  14086. ' };',
  14087. '});',
  14088. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14089. ' this.Create = function () {',
  14090. ' $mod.TObject.Create.call(this);',
  14091. ' $mod.TObject.Create.call(this);',
  14092. ' $mod.TObject.CreateWithB.call(this, false);',
  14093. ' return this;',
  14094. ' };',
  14095. ' this.CreateWithC = function (c) {',
  14096. ' $mod.TObject.Create.call(this);',
  14097. ' $mod.TObject.CreateWithB.call(this, true);',
  14098. ' this.DoIt();',
  14099. ' this.DoIt();',
  14100. ' this.$class.DoSome();',
  14101. ' return this;',
  14102. ' };',
  14103. ' this.DoIt = function () {',
  14104. ' this.Create();',
  14105. ' this.CreateWithB(false);',
  14106. ' this.CreateWithC("c");',
  14107. ' };',
  14108. ' this.DoSome = function () {',
  14109. ' var Result = null;',
  14110. ' Result = this.$create("Create");',
  14111. ' Result = this.$create("CreateWithB", [true]);',
  14112. ' Result = this.$create("CreateWithC", ["c"]);',
  14113. ' return Result;',
  14114. ' };',
  14115. '});'
  14116. ]),
  14117. LinesToStr([ // this.$main
  14118. ''
  14119. ]));
  14120. end;
  14121. procedure TTestModule.TestClass_ClassVar_Assign;
  14122. begin
  14123. StartProgram(false);
  14124. Add([
  14125. 'type',
  14126. ' TObject = class',
  14127. ' public',
  14128. ' class var vI: longint;',
  14129. ' class var Sub: TObject;',
  14130. ' constructor Create;',
  14131. ' class function GetIt(var Par: longint): tobject;',
  14132. ' end;',
  14133. 'constructor tobject.create;',
  14134. 'begin',
  14135. ' vi:=vi+1;',
  14136. ' Self.vi:=Self.vi+1;',
  14137. ' inc(vi);',
  14138. 'end;',
  14139. 'class function tobject.getit(var par: longint): tobject;',
  14140. 'begin',
  14141. ' vi:=vi+3;',
  14142. ' Self.vi:=Self.vi+4;',
  14143. ' inc(vi);',
  14144. ' Result:=self.sub;',
  14145. ' GetIt(vi);',
  14146. 'end;',
  14147. 'var Obj: tobject;',
  14148. 'begin',
  14149. ' obj:=tobject.create;',
  14150. ' tobject.vi:=3;',
  14151. ' if tobject.vi=4 then ;',
  14152. ' tobject.sub:=nil;',
  14153. ' obj.sub:=nil;',
  14154. ' obj.sub.sub:=nil;']);
  14155. ConvertProgram;
  14156. CheckSource('TestClass_ClassVar_Assign',
  14157. LinesToStr([ // statements
  14158. 'rtl.createClass(this,"TObject",null,function(){',
  14159. ' this.vI = 0;',
  14160. ' this.Sub = null;',
  14161. ' this.$init = function () {',
  14162. ' };',
  14163. ' this.$final = function () {',
  14164. ' };',
  14165. ' this.Create = function(){',
  14166. ' $mod.TObject.vI = this.vI+1;',
  14167. ' $mod.TObject.vI = this.vI+1;',
  14168. ' $mod.TObject.vI += 1;',
  14169. ' return this;',
  14170. ' };',
  14171. ' this.GetIt = function(Par){',
  14172. ' var Result = null;',
  14173. ' $mod.TObject.vI = this.vI + 3;',
  14174. ' $mod.TObject.vI = this.vI + 4;',
  14175. ' $mod.TObject.vI += 1;',
  14176. ' Result = this.Sub;',
  14177. ' this.GetIt({',
  14178. ' p: $mod.TObject,',
  14179. ' get: function () {',
  14180. ' return this.p.vI;',
  14181. ' },',
  14182. ' set: function (v) {',
  14183. ' this.p.vI = v;',
  14184. ' }',
  14185. ' });',
  14186. ' return Result;',
  14187. ' };',
  14188. '});',
  14189. 'this.Obj = null;'
  14190. ]),
  14191. LinesToStr([ // $mod.$main
  14192. '$mod.Obj = $mod.TObject.$create("Create");',
  14193. '$mod.TObject.vI = 3;',
  14194. 'if ($mod.TObject.vI === 4);',
  14195. '$mod.TObject.Sub=null;',
  14196. '$mod.TObject.Sub=null;',
  14197. '$mod.TObject.Sub=null;',
  14198. '']));
  14199. end;
  14200. procedure TTestModule.TestClass_CallClassMethod;
  14201. begin
  14202. StartProgram(false);
  14203. Add('type');
  14204. Add(' TObject = class');
  14205. Add(' public');
  14206. Add(' class var vI: longint;');
  14207. Add(' class var Sub: TObject;');
  14208. Add(' constructor Create;');
  14209. Add(' function GetMore(Par: longint): longint;');
  14210. Add(' class function GetIt(Par: longint): tobject;');
  14211. Add(' end;');
  14212. Add('constructor tobject.create;');
  14213. Add('begin');
  14214. Add(' sub:=getit(3);');
  14215. Add(' vi:=getmore(4);');
  14216. Add(' sub:=Self.getit(5);');
  14217. Add(' vi:=Self.getmore(6);');
  14218. Add('end;');
  14219. Add('function tobject.getmore(par: longint): longint;');
  14220. Add('begin');
  14221. Add(' sub:=getit(11);');
  14222. Add(' vi:=getmore(12);');
  14223. Add(' sub:=self.getit(13);');
  14224. Add(' vi:=self.getmore(14);');
  14225. Add('end;');
  14226. Add('class function tobject.getit(par: longint): tobject;');
  14227. Add('begin');
  14228. Add(' sub:=getit(21);');
  14229. Add(' vi:=sub.getmore(22);');
  14230. Add(' sub:=self.getit(23);');
  14231. Add(' vi:=self.sub.getmore(24);');
  14232. Add('end;');
  14233. Add('var Obj: tobject;');
  14234. Add('begin');
  14235. Add(' obj:=tobject.create;');
  14236. Add(' tobject.getit(5);');
  14237. Add(' obj.getit(6);');
  14238. Add(' obj.sub.getit(7);');
  14239. Add(' obj.sub.getit(8).SUB:=nil;');
  14240. Add(' obj.sub.getit(9).GETIT(10);');
  14241. Add(' obj.sub.getit(11).SuB.getit(12);');
  14242. ConvertProgram;
  14243. CheckSource('TestClass_CallClassMethod',
  14244. LinesToStr([ // statements
  14245. 'rtl.createClass(this,"TObject",null,function(){',
  14246. ' this.vI = 0;',
  14247. ' this.Sub = null;',
  14248. ' this.$init = function () {',
  14249. ' };',
  14250. ' this.$final = function () {',
  14251. ' };',
  14252. ' this.Create = function(){',
  14253. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14254. ' $mod.TObject.vI = this.GetMore(4);',
  14255. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14256. ' $mod.TObject.vI = this.GetMore(6);',
  14257. ' return this;',
  14258. ' };',
  14259. ' this.GetMore = function(Par){',
  14260. ' var Result = 0;',
  14261. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14262. ' $mod.TObject.vI = this.GetMore(12);',
  14263. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14264. ' $mod.TObject.vI = this.GetMore(14);',
  14265. ' return Result;',
  14266. ' };',
  14267. ' this.GetIt = function(Par){',
  14268. ' var Result = null;',
  14269. ' $mod.TObject.Sub = this.GetIt(21);',
  14270. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14271. ' $mod.TObject.Sub = this.GetIt(23);',
  14272. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14273. ' return Result;',
  14274. ' };',
  14275. '});',
  14276. 'this.Obj = null;'
  14277. ]),
  14278. LinesToStr([ // $mod.$main
  14279. '$mod.Obj = $mod.TObject.$create("Create");',
  14280. '$mod.TObject.GetIt(5);',
  14281. '$mod.Obj.$class.GetIt(6);',
  14282. '$mod.Obj.Sub.$class.GetIt(7);',
  14283. '$mod.TObject.Sub=null;',
  14284. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14285. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14286. '']));
  14287. end;
  14288. procedure TTestModule.TestClass_CallClassMethodStatic;
  14289. begin
  14290. StartProgram(false);
  14291. Add([
  14292. 'type',
  14293. ' TObject = class',
  14294. ' public',
  14295. ' class function Fly: tobject; static;',
  14296. ' end;',
  14297. 'class function tobject.Fly: tobject;',
  14298. 'begin',
  14299. ' Result.Fly;',
  14300. ' Result.Fly();',
  14301. ' Fly;',
  14302. ' Fly();',
  14303. ' Fly.Fly;',
  14304. ' Fly.Fly();',
  14305. 'end;',
  14306. 'var Obj: tobject;',
  14307. 'begin',
  14308. ' obj.Fly;',
  14309. ' obj.Fly();',
  14310. ' with obj do begin',
  14311. ' Fly;',
  14312. ' Fly();',
  14313. ' end;',
  14314. '']);
  14315. ConvertProgram;
  14316. CheckSource('TestClass_CallClassMethodStatic',
  14317. LinesToStr([ // statements
  14318. 'rtl.createClass(this, "TObject", null, function () {',
  14319. ' this.$init = function () {',
  14320. ' };',
  14321. ' this.$final = function () {',
  14322. ' };',
  14323. ' this.Fly = function () {',
  14324. ' var Result = null;',
  14325. ' $mod.TObject.Fly();',
  14326. ' $mod.TObject.Fly();',
  14327. ' $mod.TObject.Fly();',
  14328. ' $mod.TObject.Fly();',
  14329. ' $mod.TObject.Fly();',
  14330. ' $mod.TObject.Fly();',
  14331. ' return Result;',
  14332. ' };',
  14333. '});',
  14334. 'this.Obj = null;'
  14335. ]),
  14336. LinesToStr([ // $mod.$main
  14337. '$mod.TObject.Fly();',
  14338. '$mod.TObject.Fly();',
  14339. 'var $with = $mod.Obj;',
  14340. '$with.Fly();',
  14341. '$with.Fly();',
  14342. '']));
  14343. end;
  14344. procedure TTestModule.TestClass_Property;
  14345. begin
  14346. StartProgram(false);
  14347. Add('type');
  14348. Add(' TObject = class');
  14349. Add(' Fx: longint;');
  14350. Add(' Fy: longint;');
  14351. Add(' function GetInt: longint;');
  14352. Add(' procedure SetInt(Value: longint);');
  14353. Add(' procedure DoIt;');
  14354. Add(' property IntA: longint read Fx write Fy;');
  14355. Add(' property IntB: longint read GetInt write SetInt;');
  14356. Add(' end;');
  14357. Add('function tobject.getint: longint;');
  14358. Add('begin');
  14359. Add(' result:=fx;');
  14360. Add('end;');
  14361. Add('procedure tobject.setint(value: longint);');
  14362. Add('begin');
  14363. Add(' if value=fy then exit;');
  14364. Add(' fy:=value;');
  14365. Add('end;');
  14366. Add('procedure tobject.doit;');
  14367. Add('begin');
  14368. Add(' IntA:=IntA+1;');
  14369. Add(' Self.IntA:=Self.IntA+1;');
  14370. Add(' IntB:=IntB+1;');
  14371. Add(' Self.IntB:=Self.IntB+1;');
  14372. Add('end;');
  14373. Add('var Obj: tobject;');
  14374. Add('begin');
  14375. Add(' obj.inta:=obj.inta+1;');
  14376. Add(' if obj.intb=2 then;');
  14377. Add(' obj.intb:=obj.intb+2;');
  14378. Add(' obj.setint(obj.inta);');
  14379. ConvertProgram;
  14380. CheckSource('TestClass_Property',
  14381. LinesToStr([ // statements
  14382. 'rtl.createClass(this, "TObject", null, function () {',
  14383. ' this.$init = function () {',
  14384. ' this.Fx = 0;',
  14385. ' this.Fy = 0;',
  14386. ' };',
  14387. ' this.$final = function () {',
  14388. ' };',
  14389. ' this.GetInt = function () {',
  14390. ' var Result = 0;',
  14391. ' Result = this.Fx;',
  14392. ' return Result;',
  14393. ' };',
  14394. ' this.SetInt = function (Value) {',
  14395. ' if (Value === this.Fy) return;',
  14396. ' this.Fy = Value;',
  14397. ' };',
  14398. ' this.DoIt = function () {',
  14399. ' this.Fy = this.Fx + 1;',
  14400. ' this.Fy = this.Fx + 1;',
  14401. ' this.SetInt(this.GetInt() + 1);',
  14402. ' this.SetInt(this.GetInt() + 1);',
  14403. ' };',
  14404. '});',
  14405. 'this.Obj = null;'
  14406. ]),
  14407. LinesToStr([ // $mod.$main
  14408. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14409. 'if ($mod.Obj.GetInt() === 2);',
  14410. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14411. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14412. ]));
  14413. end;
  14414. procedure TTestModule.TestClass_Property_ClassMethod;
  14415. begin
  14416. StartProgram(false);
  14417. Add([
  14418. 'type',
  14419. ' TObject = class',
  14420. ' class var Fx: longint;',
  14421. ' class var Fy: longint;',
  14422. ' class function GetInt: longint;',
  14423. ' class procedure SetInt(Value: longint);',
  14424. ' end;',
  14425. ' TBird = class',
  14426. ' class procedure DoIt;',
  14427. ' class property IntA: longint read Fx write Fy;',
  14428. ' class property IntB: longint read GetInt write SetInt;',
  14429. ' end;',
  14430. 'class function tobject.getint: longint;',
  14431. 'begin',
  14432. ' result:=fx;',
  14433. 'end;',
  14434. 'class procedure tobject.setint(value: longint);',
  14435. 'begin',
  14436. 'end;',
  14437. 'class procedure tbird.doit;',
  14438. 'begin',
  14439. ' FX:=3;',
  14440. ' IntA:=IntA+1;',
  14441. ' Self.IntA:=Self.IntA+1;',
  14442. ' IntB:=IntB+1;',
  14443. ' Self.IntB:=Self.IntB+1;',
  14444. ' with Self do begin',
  14445. ' FX:=11;',
  14446. ' IntA:=IntA+12;',
  14447. ' IntB:=IntB+13;',
  14448. ' end;',
  14449. 'end;',
  14450. 'var Obj: tbird;',
  14451. 'begin',
  14452. ' tbird.fx:=tbird.fx+1;',
  14453. ' tbird.inta:=tbird.inta+1;',
  14454. ' if tbird.intb=2 then;',
  14455. ' tbird.intb:=tbird.intb+2;',
  14456. ' tbird.setint(tbird.inta);',
  14457. ' obj.inta:=obj.inta+1;',
  14458. ' if obj.intb=2 then;',
  14459. ' obj.intb:=obj.intb+2;',
  14460. ' obj.setint(obj.inta);',
  14461. ' with Tbird do begin',
  14462. ' FX:=FY+1;',
  14463. ' inta:=inta+2;',
  14464. ' intb:=intb+3;',
  14465. ' end;',
  14466. ' with Obj do begin',
  14467. ' FX:=FY+1;',
  14468. ' inta:=inta+2;',
  14469. ' intb:=intb+3;',
  14470. ' end;',
  14471. '']);
  14472. ConvertProgram;
  14473. CheckSource('TestClass_Property_ClassMethod',
  14474. LinesToStr([ // statements
  14475. 'rtl.createClass(this, "TObject", null, function () {',
  14476. ' this.Fx = 0;',
  14477. ' this.Fy = 0;',
  14478. ' this.$init = function () {',
  14479. ' };',
  14480. ' this.$final = function () {',
  14481. ' };',
  14482. ' this.GetInt = function () {',
  14483. ' var Result = 0;',
  14484. ' Result = this.Fx;',
  14485. ' return Result;',
  14486. ' };',
  14487. ' this.SetInt = function (Value) {',
  14488. ' };',
  14489. '});',
  14490. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14491. ' this.DoIt = function () {',
  14492. ' $mod.TObject.Fx = 3;',
  14493. ' $mod.TObject.Fy = this.Fx + 1;',
  14494. ' $mod.TObject.Fy = this.Fx + 1;',
  14495. ' this.SetInt(this.GetInt() + 1);',
  14496. ' this.SetInt(this.GetInt() + 1);',
  14497. ' $mod.TObject.Fx = 11;',
  14498. ' $mod.TObject.Fy = this.Fx + 12;',
  14499. ' this.SetInt(this.GetInt() + 13);',
  14500. ' };',
  14501. '});',
  14502. 'this.Obj = null;'
  14503. ]),
  14504. LinesToStr([ // $mod.$main
  14505. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14506. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14507. 'if ($mod.TBird.GetInt() === 2);',
  14508. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14509. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14510. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14511. 'if ($mod.Obj.$class.GetInt() === 2);',
  14512. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14513. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14514. 'var $with = $mod.TBird;',
  14515. '$mod.TObject.Fx = $with.Fy + 1;',
  14516. '$mod.TObject.Fy = $with.Fx + 2;',
  14517. '$with.SetInt($with.GetInt() + 3);',
  14518. 'var $with1 = $mod.Obj;',
  14519. '$mod.TObject.Fx = $with1.Fy + 1;',
  14520. '$mod.TObject.Fy = $with1.Fx + 2;',
  14521. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14522. '']));
  14523. end;
  14524. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14525. begin
  14526. StartProgram(false);
  14527. Add([
  14528. 'type',
  14529. ' TObject = class',
  14530. ' class function GetInt: longint; static;',
  14531. ' class procedure SetInt(Value: longint); static;',
  14532. ' class function GetItems(Index: word): longint; static;',
  14533. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14534. ' end;',
  14535. ' TBird = class',
  14536. ' class procedure Fly;',
  14537. ' class property IntA: longint read GetInt write SetInt;',
  14538. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14539. ' end;',
  14540. 'class function tobject.getint: longint;',
  14541. 'begin',
  14542. 'end;',
  14543. 'class procedure tobject.setint(value: longint);',
  14544. 'begin',
  14545. 'end;',
  14546. 'class function tobject.GetItems(Index: word): longint;',
  14547. 'begin',
  14548. 'end;',
  14549. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14550. 'begin',
  14551. 'end;',
  14552. 'class procedure tbird.fly;',
  14553. 'var w: longint;',
  14554. 'begin',
  14555. ' inta:=inta+51;',
  14556. ' w:=items[52];',
  14557. ' items[53]:=54;',
  14558. 'end;',
  14559. 'var Obj: tbird;',
  14560. ' i: longint;',
  14561. 'begin',
  14562. ' tbird.inta:=tbird.inta+1;',
  14563. ' i:=tbird.items[2];',
  14564. ' tbird.items[3]:=4;',
  14565. ' obj.inta:=obj.inta+11;',
  14566. ' i:=obj.items[12];',
  14567. ' obj.items[13]:=14;',
  14568. ' with Tbird do begin',
  14569. ' inta:=inta+21;',
  14570. ' i:=items[22];',
  14571. ' items[23]:=24;',
  14572. ' end;',
  14573. ' with Obj do begin',
  14574. ' inta:=inta+31;',
  14575. ' i:=items[32];',
  14576. ' items[33]:=34;',
  14577. ' end;',
  14578. '']);
  14579. ConvertProgram;
  14580. CheckSource('TestClass_Property_ClassMethod',
  14581. LinesToStr([ // statements
  14582. 'rtl.createClass(this, "TObject", null, function () {',
  14583. ' this.$init = function () {',
  14584. ' };',
  14585. ' this.$final = function () {',
  14586. ' };',
  14587. ' this.GetInt = function () {',
  14588. ' var Result = 0;',
  14589. ' return Result;',
  14590. ' };',
  14591. ' this.SetInt = function (Value) {',
  14592. ' };',
  14593. ' this.GetItems = function (Index) {',
  14594. ' var Result = 0;',
  14595. ' return Result;',
  14596. ' };',
  14597. ' this.SetItems = function (Index, Value) {',
  14598. ' };',
  14599. '});',
  14600. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14601. ' this.Fly = function () {',
  14602. ' var w = 0;',
  14603. ' this.SetInt(this.GetInt() + 51);',
  14604. ' w = this.GetItems(52);',
  14605. ' this.SetItems(53, 54);',
  14606. ' };',
  14607. '});',
  14608. 'this.Obj = null;',
  14609. 'this.i = 0;',
  14610. '']),
  14611. LinesToStr([ // $mod.$main
  14612. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14613. '$mod.i = $mod.TObject.GetItems(2);',
  14614. '$mod.TObject.SetItems(3, 4);',
  14615. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14616. '$mod.i = $mod.TObject.GetItems(12);',
  14617. '$mod.TObject.SetItems(13, 14);',
  14618. 'var $with = $mod.TBird;',
  14619. '$with.SetInt($with.GetInt() + 21);',
  14620. '$mod.i = $with.GetItems(22);',
  14621. '$with.SetItems(23, 24);',
  14622. 'var $with1 = $mod.Obj;',
  14623. '$with1.SetInt($with1.GetInt() + 31);',
  14624. '$mod.i = $with1.GetItems(32);',
  14625. '$with1.SetItems(33, 34);',
  14626. '']));
  14627. end;
  14628. procedure TTestModule.TestClass_Property_Indexed;
  14629. begin
  14630. StartProgram(false);
  14631. Add([
  14632. 'type',
  14633. ' TObject = class',
  14634. ' FItems: array of longint;',
  14635. ' function GetItems(Index: longint): longint;',
  14636. ' procedure SetItems(Index: longint; Value: longint);',
  14637. ' procedure DoIt;',
  14638. ' property Items[Index: longint]: longint read getitems write setitems;',
  14639. ' end;',
  14640. 'function tobject.getitems(index: longint): longint;',
  14641. 'begin',
  14642. ' Result:=fitems[index];',
  14643. 'end;',
  14644. 'procedure tobject.setitems(index: longint; value: longint);',
  14645. 'begin',
  14646. ' fitems[index]:=value;',
  14647. 'end;',
  14648. 'procedure tobject.doit;',
  14649. 'begin',
  14650. ' items[1]:=2;',
  14651. ' items[3]:=items[4];',
  14652. ' self.items[5]:=self.items[6];',
  14653. ' items[items[7]]:=items[items[8]];',
  14654. 'end;',
  14655. 'var Obj: tobject;',
  14656. 'begin',
  14657. ' obj.Items[11]:=obj.Items[12];',
  14658. '']);
  14659. ConvertProgram;
  14660. CheckSource('TestClass_Property_Indexed',
  14661. LinesToStr([ // statements
  14662. 'rtl.createClass(this, "TObject", null, function () {',
  14663. ' this.$init = function () {',
  14664. ' this.FItems = [];',
  14665. ' };',
  14666. ' this.$final = function () {',
  14667. ' this.FItems = undefined;',
  14668. ' };',
  14669. ' this.GetItems = function (Index) {',
  14670. ' var Result = 0;',
  14671. ' Result = this.FItems[Index];',
  14672. ' return Result;',
  14673. ' };',
  14674. ' this.SetItems = function (Index, Value) {',
  14675. ' this.FItems[Index] = Value;',
  14676. ' };',
  14677. ' this.DoIt = function () {',
  14678. ' this.SetItems(1, 2);',
  14679. ' this.SetItems(3,this.GetItems(4));',
  14680. ' this.SetItems(5,this.GetItems(6));',
  14681. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  14682. ' };',
  14683. '});',
  14684. 'this.Obj = null;'
  14685. ]),
  14686. LinesToStr([ // $mod.$main
  14687. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  14688. ]));
  14689. end;
  14690. procedure TTestModule.TestClass_Property_IndexSpec;
  14691. begin
  14692. StartProgram(false);
  14693. Add([
  14694. 'type',
  14695. ' TEnum = (red, blue);',
  14696. ' TObject = class',
  14697. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  14698. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  14699. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  14700. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  14701. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  14702. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  14703. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  14704. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  14705. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  14706. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  14707. ' end;',
  14708. 'procedure DoIt(b: boolean); begin end;',
  14709. 'var',
  14710. ' o: TObject;',
  14711. 'begin',
  14712. ' o.B1:=o.B1;',
  14713. ' o.B2:=o.B2;',
  14714. ' o.B3:=o.B3;',
  14715. ' o.I1[''a'']:=o.I1[''b''];',
  14716. ' doit(o.b1);',
  14717. ' doit(o.b2);',
  14718. ' doit(o.i1[''c'']);',
  14719. '']);
  14720. ConvertProgram;
  14721. CheckSource('TestClass_Property_IndexSpec',
  14722. LinesToStr([ // statements
  14723. 'this.TEnum = {',
  14724. ' "0": "red",',
  14725. ' red: 0,',
  14726. ' "1": "blue",',
  14727. ' blue: 1',
  14728. '};',
  14729. 'rtl.createClass(this, "TObject", null, function () {',
  14730. ' this.$init = function () {',
  14731. ' };',
  14732. ' this.$final = function () {',
  14733. ' };',
  14734. '});',
  14735. 'this.DoIt = function (b) {',
  14736. '};',
  14737. 'this.o = null;',
  14738. '']),
  14739. LinesToStr([ // $mod.$main
  14740. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  14741. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  14742. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  14743. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  14744. '$mod.DoIt($mod.o.GetIntBool(1));',
  14745. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  14746. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  14747. '']));
  14748. end;
  14749. procedure TTestModule.TestClass_PropertyOfTypeArray;
  14750. begin
  14751. StartProgram(false);
  14752. Add('type');
  14753. Add(' TArray = array of longint;');
  14754. Add(' TObject = class');
  14755. Add(' FItems: TArray;');
  14756. Add(' function GetItems: tarray;');
  14757. Add(' procedure SetItems(Value: tarray);');
  14758. Add(' property Items: tarray read getitems write setitems;');
  14759. Add(' procedure SetNumbers(const Value: tarray);');
  14760. Add(' property Numbers: tarray write setnumbers;');
  14761. Add(' end;');
  14762. Add('function tobject.getitems: tarray;');
  14763. Add('begin');
  14764. Add(' Result:=fitems;');
  14765. Add('end;');
  14766. Add('procedure tobject.setitems(value: tarray);');
  14767. Add('begin');
  14768. Add(' fitems:=value;');
  14769. Add(' fitems:=nil;');
  14770. Add(' Items:=nil;');
  14771. Add(' Items:=Items;');
  14772. Add(' Items[1]:=2;');
  14773. Add(' fitems[3]:=Items[4];');
  14774. Add(' Items[5]:=Items[6];');
  14775. Add(' Self.Items[7]:=8;');
  14776. Add(' Self.Items[9]:=Self.Items[10];');
  14777. Add(' Items[Items[11]]:=Items[Items[12]];');
  14778. Add('end;');
  14779. Add('procedure tobject.SetNumbers(const Value: tarray);');
  14780. Add('begin;');
  14781. Add(' Numbers:=nil;');
  14782. Add(' Numbers:=Value;');
  14783. Add(' Self.Numbers:=Value;');
  14784. Add('end;');
  14785. Add('var Obj: tobject;');
  14786. Add('begin');
  14787. Add(' obj.items:=nil;');
  14788. Add(' obj.items:=obj.items;');
  14789. Add(' obj.items[11]:=obj.items[12];');
  14790. ConvertProgram;
  14791. CheckSource('TestClass_PropertyOfTypeArray',
  14792. LinesToStr([ // statements
  14793. 'rtl.createClass(this, "TObject", null, function () {',
  14794. ' this.$init = function () {',
  14795. ' this.FItems = [];',
  14796. ' };',
  14797. ' this.$final = function () {',
  14798. ' this.FItems = undefined;',
  14799. ' };',
  14800. ' this.GetItems = function () {',
  14801. ' var Result = [];',
  14802. ' Result = rtl.arrayRef(this.FItems);',
  14803. ' return Result;',
  14804. ' };',
  14805. ' this.SetItems = function (Value) {',
  14806. ' this.FItems = rtl.arrayRef(Value);',
  14807. ' this.FItems = [];',
  14808. ' this.SetItems([]);',
  14809. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  14810. ' this.GetItems()[1] = 2;',
  14811. ' this.FItems[3] = this.GetItems()[4];',
  14812. ' this.GetItems()[5] = this.GetItems()[6];',
  14813. ' this.GetItems()[7] = 8;',
  14814. ' this.GetItems()[9] = this.GetItems()[10];',
  14815. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  14816. ' };',
  14817. ' this.SetNumbers = function (Value) {',
  14818. ' this.SetNumbers([]);',
  14819. ' this.SetNumbers(Value);',
  14820. ' this.SetNumbers(Value);',
  14821. ' };',
  14822. '});',
  14823. 'this.Obj = null;'
  14824. ]),
  14825. LinesToStr([ // $mod.$main
  14826. '$mod.Obj.SetItems([]);',
  14827. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  14828. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  14829. ]));
  14830. end;
  14831. procedure TTestModule.TestClass_PropertyDefault;
  14832. begin
  14833. StartProgram(false);
  14834. Add([
  14835. 'type',
  14836. ' TArray = array of longint;',
  14837. ' TObject = class',
  14838. ' end;',
  14839. ' TBird = class',
  14840. ' FItems: TArray;',
  14841. ' function GetItems(Index: longint): longint;',
  14842. ' procedure SetItems(Index, Value: longint);',
  14843. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  14844. ' end;',
  14845. 'function TBird.getitems(index: longint): longint;',
  14846. 'begin',
  14847. 'end;',
  14848. 'procedure TBird.setitems(index, value: longint);',
  14849. 'begin',
  14850. ' Self[1]:=2;',
  14851. ' Self[3]:=Self[index];',
  14852. ' Self[index]:=Self[Self[value]];',
  14853. ' Self[Self[4]]:=value;',
  14854. 'end;',
  14855. 'var',
  14856. ' Bird: TBird;',
  14857. ' Obj: TObject;',
  14858. 'begin',
  14859. ' bird[11]:=12;',
  14860. ' bird[13]:=bird[14];',
  14861. ' bird[Bird[15]]:=bird[Bird[15]];',
  14862. ' TBird(obj)[16]:=TBird(obj)[17];',
  14863. ' (obj as tbird)[18]:=19;',
  14864. '']);
  14865. ConvertProgram;
  14866. CheckSource('TestClass_PropertyDefault',
  14867. LinesToStr([ // statements
  14868. 'rtl.createClass(this, "TObject", null, function () {',
  14869. ' this.$init = function () {',
  14870. ' };',
  14871. ' this.$final = function () {',
  14872. ' };',
  14873. '});',
  14874. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14875. ' this.$init = function () {',
  14876. ' $mod.TObject.$init.call(this);',
  14877. ' this.FItems = [];',
  14878. ' };',
  14879. ' this.$final = function () {',
  14880. ' this.FItems = undefined;',
  14881. ' $mod.TObject.$final.call(this);',
  14882. ' };',
  14883. ' this.GetItems = function (Index) {',
  14884. ' var Result = 0;',
  14885. ' return Result;',
  14886. ' };',
  14887. ' this.SetItems = function (Index, Value) {',
  14888. ' this.SetItems(1, 2);',
  14889. ' this.SetItems(3, this.GetItems(Index));',
  14890. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  14891. ' this.SetItems(this.GetItems(4), Value);',
  14892. ' };',
  14893. '});',
  14894. 'this.Bird = null;',
  14895. 'this.Obj = null;',
  14896. '']),
  14897. LinesToStr([ // $mod.$main
  14898. '$mod.Bird.SetItems(11, 12);',
  14899. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  14900. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  14901. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  14902. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  14903. '']));
  14904. end;
  14905. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  14906. begin
  14907. StartProgram(false);
  14908. Add([
  14909. 'type',
  14910. ' TObject = class end;',
  14911. ' TAlphaList = class',
  14912. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  14913. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  14914. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  14915. ' end;',
  14916. ' TBetaList = class',
  14917. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  14918. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  14919. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  14920. ' end;',
  14921. ' TBird = class',
  14922. ' procedure DoIt;',
  14923. ' end;',
  14924. 'procedure TBird.DoIt;',
  14925. 'var',
  14926. ' List: TAlphaList;',
  14927. 'begin',
  14928. ' if TBetaList(List[true])[3]=nil then ;',
  14929. ' TBetaList(List[false])[5]:=nil;',
  14930. 'end;',
  14931. 'var',
  14932. ' List: TAlphaList;',
  14933. 'begin',
  14934. ' if TBetaList(List[true])[3]=nil then ;',
  14935. ' TBetaList(List[false])[5]:=nil;',
  14936. '']);
  14937. ConvertProgram;
  14938. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  14939. LinesToStr([ // statements
  14940. 'rtl.createClass(this, "TObject", null, function () {',
  14941. ' this.$init = function () {',
  14942. ' };',
  14943. ' this.$final = function () {',
  14944. ' };',
  14945. '});',
  14946. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  14947. '});',
  14948. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  14949. '});',
  14950. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14951. ' this.DoIt = function () {',
  14952. ' var List = null;',
  14953. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  14954. ' List.GetAlphas(false).SetBetas(5, null);',
  14955. ' };',
  14956. '});',
  14957. 'this.List = null;',
  14958. '']),
  14959. LinesToStr([ // $mod.$main
  14960. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  14961. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  14962. '']));
  14963. end;
  14964. procedure TTestModule.TestClass_PropertyOverride;
  14965. begin
  14966. StartProgram(false);
  14967. Add('type');
  14968. Add(' integer = longint;');
  14969. Add(' TObject = class');
  14970. Add(' FItem: integer;');
  14971. Add(' function GetItem: integer; external name ''GetItem'';');
  14972. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  14973. Add(' property Item: integer read getitem write setitem;');
  14974. Add(' end;');
  14975. Add(' TCar = class');
  14976. Add(' FBag: integer;');
  14977. Add(' function GetBag: integer; external name ''GetBag'';');
  14978. Add(' property Item read getbag;');
  14979. Add(' end;');
  14980. Add('var');
  14981. Add(' Obj: tobject;');
  14982. Add(' Car: tcar;');
  14983. Add('begin');
  14984. Add(' Obj.Item:=Obj.Item;');
  14985. Add(' Car.Item:=Car.Item;');
  14986. ConvertProgram;
  14987. CheckSource('TestClass_PropertyOverride',
  14988. LinesToStr([ // statements
  14989. 'rtl.createClass(this, "TObject", null, function () {',
  14990. ' this.$init = function () {',
  14991. ' this.FItem = 0;',
  14992. ' };',
  14993. ' this.$final = function () {',
  14994. ' };',
  14995. '});',
  14996. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14997. ' this.$init = function () {',
  14998. ' $mod.TObject.$init.call(this);',
  14999. ' this.FBag = 0;',
  15000. ' };',
  15001. '});',
  15002. 'this.Obj = null;',
  15003. 'this.Car = null;',
  15004. '']),
  15005. LinesToStr([ // $mod.$main
  15006. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15007. '$mod.Car.SetItem($mod.Car.GetBag());',
  15008. '']));
  15009. end;
  15010. procedure TTestModule.TestClass_PropertyIncVisibility;
  15011. begin
  15012. AddModuleWithIntfImplSrc('unit1.pp',
  15013. LinesToStr([
  15014. 'type',
  15015. ' TNumber = longint;',
  15016. ' TInteger = longint;',
  15017. ' TObject = class',
  15018. ' private',
  15019. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15020. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15021. ' protected',
  15022. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15023. ' end;']),
  15024. LinesToStr([
  15025. '']));
  15026. StartProgram(true);
  15027. Add([
  15028. 'uses unit1;',
  15029. 'type',
  15030. ' TBird = class',
  15031. ' public',
  15032. ' property Items;',
  15033. ' end;',
  15034. 'procedure DoIt(i: TInteger);',
  15035. 'begin',
  15036. 'end;',
  15037. 'var b: TBird;',
  15038. 'begin',
  15039. ' b.Items[1]:=2;',
  15040. ' b.Items[3]:=b.Items[4];',
  15041. ' DoIt(b.Items[5]);',
  15042. '']);
  15043. ConvertProgram;
  15044. CheckSource('TestClass_PropertyIncVisibility',
  15045. LinesToStr([ // statements
  15046. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15047. '});',
  15048. 'this.DoIt = function (i) {',
  15049. '};',
  15050. 'this.b = null;'
  15051. ]),
  15052. LinesToStr([ // $mod.$main
  15053. '$mod.b.SetItems(1, 2);',
  15054. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15055. '$mod.DoIt($mod.b.GetItems(5));'
  15056. ]));
  15057. end;
  15058. procedure TTestModule.TestClass_Assigned;
  15059. begin
  15060. StartProgram(false);
  15061. Add('type');
  15062. Add(' TObject = class');
  15063. Add(' end;');
  15064. Add('var');
  15065. Add(' Obj: tobject;');
  15066. Add(' b: boolean;');
  15067. Add('begin');
  15068. Add(' if Assigned(obj) then ;');
  15069. Add(' b:=Assigned(obj) or false;');
  15070. ConvertProgram;
  15071. CheckSource('TestClass_Assigned',
  15072. LinesToStr([ // statements
  15073. 'rtl.createClass(this, "TObject", null, function () {',
  15074. ' this.$init = function () {',
  15075. ' };',
  15076. ' this.$final = function () {',
  15077. ' };',
  15078. '});',
  15079. 'this.Obj = null;',
  15080. 'this.b = false;'
  15081. ]),
  15082. LinesToStr([ // $mod.$main
  15083. 'if ($mod.Obj != null);',
  15084. '$mod.b = ($mod.Obj != null) || false;'
  15085. ]));
  15086. end;
  15087. procedure TTestModule.TestClass_WithClassDoCreate;
  15088. begin
  15089. StartProgram(false);
  15090. Add('type');
  15091. Add(' TObject = class');
  15092. Add(' aBool: boolean;');
  15093. Add(' Arr: array of boolean;');
  15094. Add(' constructor Create;');
  15095. Add(' end;');
  15096. Add('constructor TObject.Create; begin end;');
  15097. Add('var');
  15098. Add(' Obj: tobject;');
  15099. Add(' b: boolean;');
  15100. Add('begin');
  15101. Add(' with tobject.create do begin');
  15102. Add(' b:=abool;');
  15103. Add(' abool:=b;');
  15104. Add(' b:=arr[1];');
  15105. Add(' arr[2]:=b;');
  15106. Add(' end;');
  15107. Add(' with tobject do');
  15108. Add(' obj:=create;');
  15109. Add(' with obj do begin');
  15110. Add(' create;');
  15111. Add(' b:=abool;');
  15112. Add(' abool:=b;');
  15113. Add(' b:=arr[3];');
  15114. Add(' arr[4]:=b;');
  15115. Add(' end;');
  15116. ConvertProgram;
  15117. CheckSource('TestClass_WithClassDoCreate',
  15118. LinesToStr([ // statements
  15119. 'rtl.createClass(this, "TObject", null, function () {',
  15120. ' this.$init = function () {',
  15121. ' this.aBool = false;',
  15122. ' this.Arr = [];',
  15123. ' };',
  15124. ' this.$final = function () {',
  15125. ' this.Arr = undefined;',
  15126. ' };',
  15127. ' this.Create = function () {',
  15128. ' return this;',
  15129. ' };',
  15130. '});',
  15131. 'this.Obj = null;',
  15132. 'this.b = false;'
  15133. ]),
  15134. LinesToStr([ // $mod.$main
  15135. 'var $with = $mod.TObject.$create("Create");',
  15136. '$mod.b = $with.aBool;',
  15137. '$with.aBool = $mod.b;',
  15138. '$mod.b = $with.Arr[1];',
  15139. '$with.Arr[2] = $mod.b;',
  15140. 'var $with1 = $mod.TObject;',
  15141. '$mod.Obj = $with1.$create("Create");',
  15142. 'var $with2 = $mod.Obj;',
  15143. '$with2.Create();',
  15144. '$mod.b = $with2.aBool;',
  15145. '$with2.aBool = $mod.b;',
  15146. '$mod.b = $with2.Arr[3];',
  15147. '$with2.Arr[4] = $mod.b;',
  15148. '']));
  15149. end;
  15150. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15151. begin
  15152. StartProgram(false);
  15153. Add('type');
  15154. Add(' TObject = class');
  15155. Add(' FInt: longint;');
  15156. Add(' constructor Create;');
  15157. Add(' function GetSize: longint;');
  15158. Add(' procedure SetSize(Value: longint);');
  15159. Add(' property Int: longint read FInt write FInt;');
  15160. Add(' property Size: longint read GetSize write SetSize;');
  15161. Add(' end;');
  15162. Add('constructor TObject.Create; begin end;');
  15163. Add('function TObject.GetSize: longint; begin; end;');
  15164. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15165. Add('var');
  15166. Add(' Obj: tobject;');
  15167. Add(' i: longint;');
  15168. Add('begin');
  15169. Add(' with TObject.Create do begin');
  15170. Add(' i:=int;');
  15171. Add(' int:=i;');
  15172. Add(' i:=size;');
  15173. Add(' size:=i;');
  15174. Add(' end;');
  15175. Add(' with obj do begin');
  15176. Add(' i:=int;');
  15177. Add(' int:=i;');
  15178. Add(' i:=size;');
  15179. Add(' size:=i;');
  15180. Add(' end;');
  15181. ConvertProgram;
  15182. CheckSource('TestClass_WithClassInstDoProperty',
  15183. LinesToStr([ // statements
  15184. 'rtl.createClass(this, "TObject", null, function () {',
  15185. ' this.$init = function () {',
  15186. ' this.FInt = 0;',
  15187. ' };',
  15188. ' this.$final = function () {',
  15189. ' };',
  15190. ' this.Create = function () {',
  15191. ' return this;',
  15192. ' };',
  15193. ' this.GetSize = function () {',
  15194. ' var Result = 0;',
  15195. ' return Result;',
  15196. ' };',
  15197. ' this.SetSize = function (Value) {',
  15198. ' };',
  15199. '});',
  15200. 'this.Obj = null;',
  15201. 'this.i = 0;'
  15202. ]),
  15203. LinesToStr([ // $mod.$main
  15204. 'var $with = $mod.TObject.$create("Create");',
  15205. '$mod.i = $with.FInt;',
  15206. '$with.FInt = $mod.i;',
  15207. '$mod.i = $with.GetSize();',
  15208. '$with.SetSize($mod.i);',
  15209. 'var $with1 = $mod.Obj;',
  15210. '$mod.i = $with1.FInt;',
  15211. '$with1.FInt = $mod.i;',
  15212. '$mod.i = $with1.GetSize();',
  15213. '$with1.SetSize($mod.i);',
  15214. '']));
  15215. end;
  15216. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15217. begin
  15218. StartProgram(false);
  15219. Add('type');
  15220. Add(' TObject = class');
  15221. Add(' constructor Create;');
  15222. Add(' function GetItems(Index: longint): longint;');
  15223. Add(' procedure SetItems(Index, Value: longint);');
  15224. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15225. Add(' end;');
  15226. Add('constructor TObject.Create; begin end;');
  15227. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15228. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15229. Add('var');
  15230. Add(' Obj: tobject;');
  15231. Add(' i: longint;');
  15232. Add('begin');
  15233. Add(' with TObject.Create do begin');
  15234. Add(' i:=Items[1];');
  15235. Add(' Items[2]:=i;');
  15236. Add(' end;');
  15237. Add(' with obj do begin');
  15238. Add(' i:=Items[3];');
  15239. Add(' Items[4]:=i;');
  15240. Add(' end;');
  15241. ConvertProgram;
  15242. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15243. LinesToStr([ // statements
  15244. 'rtl.createClass(this, "TObject", null, function () {',
  15245. ' this.$init = function () {',
  15246. ' };',
  15247. ' this.$final = function () {',
  15248. ' };',
  15249. ' this.Create = function () {',
  15250. ' return this;',
  15251. ' };',
  15252. ' this.GetItems = function (Index) {',
  15253. ' var Result = 0;',
  15254. ' return Result;',
  15255. ' };',
  15256. ' this.SetItems = function (Index, Value) {',
  15257. ' };',
  15258. '});',
  15259. 'this.Obj = null;',
  15260. 'this.i = 0;'
  15261. ]),
  15262. LinesToStr([ // $mod.$main
  15263. 'var $with = $mod.TObject.$create("Create");',
  15264. '$mod.i = $with.GetItems(1);',
  15265. '$with.SetItems(2, $mod.i);',
  15266. 'var $with1 = $mod.Obj;',
  15267. '$mod.i = $with1.GetItems(3);',
  15268. '$with1.SetItems(4, $mod.i);',
  15269. '']));
  15270. end;
  15271. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15272. begin
  15273. StartProgram(false);
  15274. Add('type');
  15275. Add(' TObject = class');
  15276. Add(' constructor Create;');
  15277. Add(' function GetSize: longint;');
  15278. Add(' procedure SetSize(Value: longint);');
  15279. Add(' end;');
  15280. Add('constructor TObject.Create; begin end;');
  15281. Add('function TObject.GetSize: longint; begin; end;');
  15282. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15283. Add('var');
  15284. Add(' Obj: tobject;');
  15285. Add(' i: longint;');
  15286. Add('begin');
  15287. Add(' with TObject.Create do begin');
  15288. Add(' i:=GetSize;');
  15289. Add(' i:=GetSize();');
  15290. Add(' SetSize(i);');
  15291. Add(' end;');
  15292. Add(' with obj do begin');
  15293. Add(' i:=GetSize;');
  15294. Add(' i:=GetSize();');
  15295. Add(' SetSize(i);');
  15296. Add(' end;');
  15297. ConvertProgram;
  15298. CheckSource('TestClass_WithClassInstDoFunc',
  15299. LinesToStr([ // statements
  15300. 'rtl.createClass(this, "TObject", null, function () {',
  15301. ' this.$init = function () {',
  15302. ' };',
  15303. ' this.$final = function () {',
  15304. ' };',
  15305. ' this.Create = function () {',
  15306. ' return this;',
  15307. ' };',
  15308. ' this.GetSize = function () {',
  15309. ' var Result = 0;',
  15310. ' return Result;',
  15311. ' };',
  15312. ' this.SetSize = function (Value) {',
  15313. ' };',
  15314. '});',
  15315. 'this.Obj = null;',
  15316. 'this.i = 0;'
  15317. ]),
  15318. LinesToStr([ // $mod.$main
  15319. 'var $with = $mod.TObject.$create("Create");',
  15320. '$mod.i = $with.GetSize();',
  15321. '$mod.i = $with.GetSize();',
  15322. '$with.SetSize($mod.i);',
  15323. 'var $with1 = $mod.Obj;',
  15324. '$mod.i = $with1.GetSize();',
  15325. '$mod.i = $with1.GetSize();',
  15326. '$with1.SetSize($mod.i);',
  15327. '']));
  15328. end;
  15329. procedure TTestModule.TestClass_TypeCast;
  15330. begin
  15331. StartProgram(false);
  15332. Add('type');
  15333. Add(' TObject = class');
  15334. Add(' Next: TObject;');
  15335. Add(' constructor Create;');
  15336. Add(' end;');
  15337. Add(' TControl = class(TObject)');
  15338. Add(' Arr: array of TObject;');
  15339. Add(' function GetIt(vI: longint = 0): TObject;');
  15340. Add(' end;');
  15341. Add('constructor tobject.create; begin end;');
  15342. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15343. Add('var');
  15344. Add(' Obj: tobject;');
  15345. Add('begin');
  15346. Add(' obj:=tcontrol(obj).next;');
  15347. Add(' tcontrol(obj):=nil;');
  15348. Add(' obj:=tcontrol(obj);');
  15349. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15350. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15351. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15352. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15353. Add(' obj:=tcontrol(nil);');
  15354. ConvertProgram;
  15355. CheckSource('TestClass_TypeCast',
  15356. LinesToStr([ // statements
  15357. 'rtl.createClass(this, "TObject", null, function () {',
  15358. ' this.$init = function () {',
  15359. ' this.Next = null;',
  15360. ' };',
  15361. ' this.$final = function () {',
  15362. ' this.Next = undefined;',
  15363. ' };',
  15364. ' this.Create = function () {',
  15365. ' return this;',
  15366. ' };',
  15367. '});',
  15368. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15369. ' this.$init = function () {',
  15370. ' $mod.TObject.$init.call(this);',
  15371. ' this.Arr = [];',
  15372. ' };',
  15373. ' this.$final = function () {',
  15374. ' this.Arr = undefined;',
  15375. ' $mod.TObject.$final.call(this);',
  15376. ' };',
  15377. ' this.GetIt = function (vI) {',
  15378. ' var Result = null;',
  15379. ' return Result;',
  15380. ' };',
  15381. '});',
  15382. 'this.Obj = null;'
  15383. ]),
  15384. LinesToStr([ // $mod.$main
  15385. '$mod.Obj = $mod.Obj.Next;',
  15386. '$mod.Obj = null;',
  15387. '$mod.Obj = $mod.Obj;',
  15388. '$mod.Obj = $mod.Obj.GetIt(0);',
  15389. '$mod.Obj = $mod.Obj.GetIt(0);',
  15390. '$mod.Obj = $mod.Obj.GetIt(1);',
  15391. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15392. '$mod.Obj = null;',
  15393. '']));
  15394. end;
  15395. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15396. begin
  15397. StartProgram(false);
  15398. Add('type');
  15399. Add(' TObject = class end;');
  15400. Add('procedure ProcA(var A);');
  15401. Add('begin');
  15402. Add(' TObject(A):=nil;');
  15403. Add(' TObject(A):=TObject(A);');
  15404. Add(' if TObject(A)=nil then ;');
  15405. Add(' if nil=TObject(A) then ;');
  15406. Add('end;');
  15407. Add('procedure ProcB(out A);');
  15408. Add('begin');
  15409. Add(' TObject(A):=nil;');
  15410. Add(' TObject(A):=TObject(A);');
  15411. Add(' if TObject(A)=nil then ;');
  15412. Add(' if nil=TObject(A) then ;');
  15413. Add('end;');
  15414. Add('procedure ProcC(const A);');
  15415. Add('begin');
  15416. Add(' if TObject(A)=nil then ;');
  15417. Add(' if nil=TObject(A) then ;');
  15418. Add('end;');
  15419. Add('var o: TObject;');
  15420. Add('begin');
  15421. Add(' ProcA(o);');
  15422. Add(' ProcB(o);');
  15423. Add(' ProcC(o);');
  15424. ConvertProgram;
  15425. CheckSource('TestClass_TypeCastUntypedParam',
  15426. LinesToStr([ // statements
  15427. 'rtl.createClass(this, "TObject", null, function () {',
  15428. ' this.$init = function () {',
  15429. ' };',
  15430. ' this.$final = function () {',
  15431. ' };',
  15432. '});',
  15433. 'this.ProcA = function (A) {',
  15434. ' A.set(null);',
  15435. ' A.set(A.get());',
  15436. ' if (A.get() === null);',
  15437. ' if (null === A.get());',
  15438. '};',
  15439. 'this.ProcB = function (A) {',
  15440. ' A.set(null);',
  15441. ' A.set(A.get());',
  15442. ' if (A.get() === null);',
  15443. ' if (null === A.get());',
  15444. '};',
  15445. 'this.ProcC = function (A) {',
  15446. ' if (A === null);',
  15447. ' if (null === A);',
  15448. '};',
  15449. 'this.o = null;',
  15450. '']),
  15451. LinesToStr([ // $mod.$main
  15452. '$mod.ProcA({',
  15453. ' p: $mod,',
  15454. ' get: function () {',
  15455. ' return this.p.o;',
  15456. ' },',
  15457. ' set: function (v) {',
  15458. ' this.p.o = v;',
  15459. ' }',
  15460. '});',
  15461. '$mod.ProcB({',
  15462. ' p: $mod,',
  15463. ' get: function () {',
  15464. ' return this.p.o;',
  15465. ' },',
  15466. ' set: function (v) {',
  15467. ' this.p.o = v;',
  15468. ' }',
  15469. '});',
  15470. '$mod.ProcC($mod.o);',
  15471. '']));
  15472. end;
  15473. procedure TTestModule.TestClass_Overloads;
  15474. begin
  15475. StartProgram(false);
  15476. Add('type');
  15477. Add(' TObject = class');
  15478. Add(' procedure DoIt;');
  15479. Add(' procedure DoIt(vI: longint);');
  15480. Add(' end;');
  15481. Add('procedure TObject.DoIt;');
  15482. Add('begin');
  15483. Add(' DoIt;');
  15484. Add(' DoIt(1);');
  15485. Add('end;');
  15486. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15487. Add('begin');
  15488. ConvertProgram;
  15489. CheckSource('TestClass_Overloads',
  15490. LinesToStr([ // statements
  15491. 'rtl.createClass(this, "TObject", null, function () {',
  15492. ' this.$init = function () {',
  15493. ' };',
  15494. ' this.$final = function () {',
  15495. ' };',
  15496. ' this.DoIt = function () {',
  15497. ' this.DoIt();',
  15498. ' this.DoIt$1(1);',
  15499. ' };',
  15500. ' this.DoIt$1 = function (vI) {',
  15501. ' };',
  15502. '});',
  15503. '']),
  15504. LinesToStr([ // $mod.$main
  15505. '']));
  15506. end;
  15507. procedure TTestModule.TestClass_OverloadsAncestor;
  15508. begin
  15509. StartProgram(false);
  15510. Add('type');
  15511. Add(' TObject = class;');
  15512. Add(' TObject = class');
  15513. Add(' procedure DoIt(vA: longint);');
  15514. Add(' procedure DoIt(vA, vB: longint);');
  15515. Add(' end;');
  15516. Add(' TCar = class;');
  15517. Add(' TCar = class');
  15518. Add(' procedure DoIt(vA: longint);');
  15519. Add(' procedure DoIt(vA, vB: longint);');
  15520. Add(' end;');
  15521. Add('procedure tobject.doit(va: longint);');
  15522. Add('begin');
  15523. Add(' doit(1);');
  15524. Add(' doit(1,2);');
  15525. Add('end;');
  15526. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15527. Add('procedure tcar.doit(va: longint);');
  15528. Add('begin');
  15529. Add(' doit(1);');
  15530. Add(' doit(1,2);');
  15531. Add(' inherited doit(1);');
  15532. Add(' inherited doit(1,2);');
  15533. Add('end;');
  15534. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15535. Add('begin');
  15536. ConvertProgram;
  15537. CheckSource('TestClass_OverloadsAncestor',
  15538. LinesToStr([ // statements
  15539. 'rtl.createClass(this, "TObject", null, function () {',
  15540. ' this.$init = function () {',
  15541. ' };',
  15542. ' this.$final = function () {',
  15543. ' };',
  15544. ' this.DoIt = function (vA) {',
  15545. ' this.DoIt(1);',
  15546. ' this.DoIt$1(1,2);',
  15547. ' };',
  15548. ' this.DoIt$1 = function (vA, vB) {',
  15549. ' };',
  15550. '});',
  15551. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15552. ' this.DoIt$2 = function (vA) {',
  15553. ' this.DoIt$2(1);',
  15554. ' this.DoIt$3(1, 2);',
  15555. ' $mod.TObject.DoIt.call(this, 1);',
  15556. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15557. ' };',
  15558. ' this.DoIt$3 = function (vA, vB) {',
  15559. ' };',
  15560. '});',
  15561. '']),
  15562. LinesToStr([ // $mod.$main
  15563. '']));
  15564. end;
  15565. procedure TTestModule.TestClass_OverloadConstructor;
  15566. begin
  15567. StartProgram(false);
  15568. Add('type');
  15569. Add(' TObject = class');
  15570. Add(' constructor Create(vA: longint);');
  15571. Add(' constructor Create(vA, vB: longint);');
  15572. Add(' end;');
  15573. Add(' TCar = class');
  15574. Add(' constructor Create(vA: longint);');
  15575. Add(' constructor Create(vA, vB: longint);');
  15576. Add(' end;');
  15577. Add('constructor tobject.create(va: longint);');
  15578. Add('begin');
  15579. Add(' create(1);');
  15580. Add(' create(1,2);');
  15581. Add('end;');
  15582. Add('constructor tobject.create(va, vb: longint); begin end;');
  15583. Add('constructor tcar.create(va: longint);');
  15584. Add('begin');
  15585. Add(' create(1);');
  15586. Add(' create(1,2);');
  15587. Add(' inherited create(1);');
  15588. Add(' inherited create(1,2);');
  15589. Add('end;');
  15590. Add('constructor tcar.create(va, vb: longint); begin end;');
  15591. Add('begin');
  15592. Add(' tobject.create(1);');
  15593. Add(' tobject.create(1,2);');
  15594. Add(' tcar.create(1);');
  15595. Add(' tcar.create(1,2);');
  15596. ConvertProgram;
  15597. CheckSource('TestClass_OverloadConstructor',
  15598. LinesToStr([ // statements
  15599. 'rtl.createClass(this, "TObject", null, function () {',
  15600. ' this.$init = function () {',
  15601. ' };',
  15602. ' this.$final = function () {',
  15603. ' };',
  15604. ' this.Create = function (vA) {',
  15605. ' this.Create(1);',
  15606. ' this.Create$1(1,2);',
  15607. ' return this;',
  15608. ' };',
  15609. ' this.Create$1 = function (vA, vB) {',
  15610. ' return this;',
  15611. ' };',
  15612. '});',
  15613. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15614. ' this.Create$2 = function (vA) {',
  15615. ' this.Create$2(1);',
  15616. ' this.Create$3(1, 2);',
  15617. ' $mod.TObject.Create.call(this, 1);',
  15618. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15619. ' return this;',
  15620. ' };',
  15621. ' this.Create$3 = function (vA, vB) {',
  15622. ' return this;',
  15623. ' };',
  15624. '});',
  15625. '']),
  15626. LinesToStr([ // $mod.$main
  15627. '$mod.TObject.$create("Create", [1]);',
  15628. '$mod.TObject.$create("Create$1", [1, 2]);',
  15629. '$mod.TCar.$create("Create$2", [1]);',
  15630. '$mod.TCar.$create("Create$3", [1, 2]);',
  15631. '']));
  15632. end;
  15633. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15634. begin
  15635. StartProgram(false);
  15636. Add([
  15637. '{$mode delphi}',
  15638. 'type',
  15639. ' TObject = class end;',
  15640. ' TBird = class',
  15641. ' function {#a}GetValue: longint; overload; virtual;',
  15642. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15643. ' end;',
  15644. ' TEagle = class(TBird)',
  15645. ' function {#c}GetValue: longint; overload; override;',
  15646. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15647. ' end;',
  15648. 'function TBird.GetValue: longint;',
  15649. 'begin',
  15650. ' if 3={@a}GetValue then ;',
  15651. ' if 4={@b}GetValue(5) then ;',
  15652. 'end;',
  15653. 'function TBird.GetValue(AValue: longint): longint;',
  15654. 'begin',
  15655. 'end;',
  15656. 'function TEagle.GetValue: longint;',
  15657. 'begin',
  15658. ' if 13={@c}GetValue then ;',
  15659. ' if 14={@d}GetValue(15) then ;',
  15660. ' if 15=inherited {@a}GetValue then ;',
  15661. ' if 16=inherited {@b}GetValue(17) then ;',
  15662. 'end;',
  15663. 'function TEagle.GetValue(AValue: longint): longint;',
  15664. 'begin',
  15665. 'end;',
  15666. 'var',
  15667. ' e: TEagle;',
  15668. 'begin',
  15669. ' if 23=e.{@c}GetValue then ;',
  15670. ' if 24=e.{@d}GetValue(25) then ;']);
  15671. ConvertProgram;
  15672. CheckSource('TestClass_OverloadDelphiOverride',
  15673. LinesToStr([ // statements
  15674. 'rtl.createClass(this, "TObject", null, function () {',
  15675. ' this.$init = function () {',
  15676. ' };',
  15677. ' this.$final = function () {',
  15678. ' };',
  15679. '});',
  15680. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15681. ' this.GetValue = function () {',
  15682. ' var Result = 0;',
  15683. ' if (3 === this.GetValue()) ;',
  15684. ' if (4 === this.GetValue$1(5)) ;',
  15685. ' return Result;',
  15686. ' };',
  15687. ' this.GetValue$1 = function (AValue) {',
  15688. ' var Result = 0;',
  15689. ' return Result;',
  15690. ' };',
  15691. '});',
  15692. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15693. ' this.GetValue = function () {',
  15694. ' var Result = 0;',
  15695. ' if (13 === this.GetValue()) ;',
  15696. ' if (14 === this.GetValue$1(15)) ;',
  15697. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  15698. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  15699. ' return Result;',
  15700. ' };',
  15701. ' this.GetValue$1 = function (AValue) {',
  15702. ' var Result = 0;',
  15703. ' return Result;',
  15704. ' };',
  15705. '});',
  15706. 'this.e = null;',
  15707. '']),
  15708. LinesToStr([ // $mod.$main
  15709. 'if (23 === $mod.e.GetValue()) ;',
  15710. 'if (24 === $mod.e.GetValue$1(25)) ;',
  15711. '']));
  15712. end;
  15713. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  15714. begin
  15715. StartProgram(false);
  15716. Add([
  15717. '{$mode delphi}',
  15718. 'type',
  15719. ' TObject = class end;',
  15720. ' TAnimal = class',
  15721. ' public',
  15722. ' {#animal_a}A: longint;',
  15723. ' function {#animal_b}B: longint;',
  15724. ' end;',
  15725. ' TBird = class(TAnimal)',
  15726. ' public',
  15727. ' {#bird_a}A: double;',
  15728. ' {#bird_b}B: boolean;',
  15729. ' end;',
  15730. ' TEagle = class(TBird)',
  15731. ' public',
  15732. ' function {#eagle_a}A: boolean;',
  15733. ' {#eagle_b}B: double;',
  15734. ' end;',
  15735. 'function TAnimal.B: longint;',
  15736. 'begin',
  15737. 'end;',
  15738. 'function TEagle.A: boolean;',
  15739. 'begin',
  15740. ' {@eagle_b}B:=3.3;',
  15741. ' {@eagle_a}A();',
  15742. ' TBird(Self).{@bird_b}B:=true;',
  15743. ' TAnimal(Self).{@animal_a}A:=17;',
  15744. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  15745. 'end;',
  15746. 'var',
  15747. ' e: TEagle;',
  15748. 'begin',
  15749. ' e.{@eagle_b}B:=5.3;',
  15750. ' if e.{@eagle_a}A then ;',
  15751. '']);
  15752. ConvertProgram;
  15753. CheckSource('TestClass_ReintroduceVarDelphi',
  15754. LinesToStr([ // statements
  15755. 'rtl.createClass(this, "TObject", null, function () {',
  15756. ' this.$init = function () {',
  15757. ' };',
  15758. ' this.$final = function () {',
  15759. ' };',
  15760. '});',
  15761. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  15762. ' this.$init = function () {',
  15763. ' $mod.TObject.$init.call(this);',
  15764. ' this.A = 0;',
  15765. ' };',
  15766. ' this.B = function () {',
  15767. ' var Result = 0;',
  15768. ' return Result;',
  15769. ' };',
  15770. '});',
  15771. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  15772. ' this.$init = function () {',
  15773. ' $mod.TAnimal.$init.call(this);',
  15774. ' this.A$1 = 0.0;',
  15775. ' this.B$1 = false;',
  15776. ' };',
  15777. '});',
  15778. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  15779. ' this.$init = function () {',
  15780. ' $mod.TBird.$init.call(this);',
  15781. ' this.B$2 = 0.0;',
  15782. ' };',
  15783. ' this.A$2 = function () {',
  15784. ' var Result = false;',
  15785. ' this.B$2 = 3.3;',
  15786. ' this.A$2();',
  15787. ' this.B$1 = true;',
  15788. ' this.A = 17;',
  15789. ' this.B$1 = this.A$1 > 1;',
  15790. ' return Result;',
  15791. ' };',
  15792. '});',
  15793. 'this.e = null;',
  15794. '']),
  15795. LinesToStr([ // $mod.$main
  15796. '$mod.e.B$2 = 5.3;',
  15797. 'if ($mod.e.A$2()) ;',
  15798. '']));
  15799. end;
  15800. procedure TTestModule.TestClass_ReintroducedVar;
  15801. begin
  15802. StartProgram(false);
  15803. Add('type');
  15804. Add(' TObject = class');
  15805. Add(' strict private');
  15806. Add(' Some: longint;');
  15807. Add(' end;');
  15808. Add(' TMobile = class');
  15809. Add(' strict private');
  15810. Add(' Some: string;');
  15811. Add(' end;');
  15812. Add(' TCar = class(tmobile)');
  15813. Add(' procedure Some;');
  15814. Add(' procedure Some(vA: longint);');
  15815. Add(' end;');
  15816. Add('procedure tcar.some;');
  15817. Add('begin');
  15818. Add(' Some;');
  15819. Add(' Some(1);');
  15820. Add('end;');
  15821. Add('procedure tcar.some(va: longint); begin end;');
  15822. Add('begin');
  15823. ConvertProgram;
  15824. CheckSource('TestClass_ReintroducedVar',
  15825. LinesToStr([ // statements
  15826. 'rtl.createClass(this, "TObject", null, function () {',
  15827. ' this.$init = function () {',
  15828. ' this.Some = 0;',
  15829. ' };',
  15830. ' this.$final = function () {',
  15831. ' };',
  15832. '});',
  15833. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  15834. ' this.$init = function () {',
  15835. ' $mod.TObject.$init.call(this);',
  15836. ' this.Some$1 = "";',
  15837. ' };',
  15838. '});',
  15839. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  15840. ' this.Some$2 = function () {',
  15841. ' this.Some$2();',
  15842. ' this.Some$3(1);',
  15843. ' };',
  15844. ' this.Some$3 = function (vA) {',
  15845. ' };',
  15846. '});',
  15847. '']),
  15848. LinesToStr([ // $mod.$main
  15849. '']));
  15850. end;
  15851. procedure TTestModule.TestClass_RaiseDescendant;
  15852. begin
  15853. StartProgram(false);
  15854. Add([
  15855. 'type',
  15856. ' TObject = class',
  15857. ' constructor Create(Msg: string);',
  15858. ' end;',
  15859. ' Exception = class',
  15860. ' end;',
  15861. ' EConvertError = class(Exception)',
  15862. ' end;',
  15863. 'constructor TObject.Create(Msg: string); begin end;',
  15864. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  15865. 'begin',
  15866. ' raise Exception.Create(''Bar1'');',
  15867. ' raise EConvertError.Create(''Bar2'');',
  15868. ' raise AssertConv(''Bar2'');',
  15869. ' raise AssertConv;',
  15870. '']);
  15871. ConvertProgram;
  15872. CheckSource('TestClass_RaiseDescendant',
  15873. LinesToStr([ // statements
  15874. 'rtl.createClass(this, "TObject", null, function () {',
  15875. ' this.$init = function () {',
  15876. ' };',
  15877. ' this.$final = function () {',
  15878. ' };',
  15879. ' this.Create = function (Msg) {',
  15880. ' return this;',
  15881. ' };',
  15882. '});',
  15883. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  15884. '});',
  15885. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  15886. '});',
  15887. 'this.AssertConv = function (Msg) {',
  15888. ' var Result = null;',
  15889. ' return Result;',
  15890. '};',
  15891. '']),
  15892. LinesToStr([ // $mod.$main
  15893. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  15894. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  15895. 'throw $mod.AssertConv("Bar2");',
  15896. 'throw $mod.AssertConv("def");',
  15897. '']));
  15898. end;
  15899. procedure TTestModule.TestClass_ExternalMethod;
  15900. begin
  15901. AddModuleWithIntfImplSrc('unit2.pas',
  15902. LinesToStr([
  15903. 'type',
  15904. ' TObject = class',
  15905. ' public',
  15906. ' procedure Intern; external name ''$DoIntern'';',
  15907. ' end;',
  15908. '']),
  15909. LinesToStr([
  15910. '']));
  15911. StartUnit(true);
  15912. Add('interface');
  15913. Add('uses unit2;');
  15914. Add('type');
  15915. Add(' TCar = class(TObject)');
  15916. Add(' public');
  15917. Add(' procedure Intern2; external name ''$DoIntern2'';');
  15918. Add(' procedure DoIt;');
  15919. Add(' end;');
  15920. Add('implementation');
  15921. Add('procedure tcar.doit;');
  15922. Add('begin');
  15923. Add(' Intern;');
  15924. Add(' Intern();');
  15925. Add(' Intern2;');
  15926. Add(' Intern2();');
  15927. Add('end;');
  15928. Add('var Obj: TCar;');
  15929. Add('begin');
  15930. Add(' obj.intern;');
  15931. Add(' obj.intern();');
  15932. Add(' obj.intern2;');
  15933. Add(' obj.intern2();');
  15934. Add(' obj.doit;');
  15935. Add(' obj.doit();');
  15936. Add(' with obj do begin');
  15937. Add(' Intern;');
  15938. Add(' Intern();');
  15939. Add(' Intern2;');
  15940. Add(' Intern2();');
  15941. Add(' end;');
  15942. ConvertUnit;
  15943. CheckSource('TestClass_ExternalMethod',
  15944. LinesToStr([
  15945. 'var $impl = $mod.$impl;',
  15946. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15947. ' this.DoIt = function () {',
  15948. ' this.$DoIntern();',
  15949. ' this.$DoIntern();',
  15950. ' this.$DoIntern2();',
  15951. ' this.$DoIntern2();',
  15952. ' };',
  15953. ' });',
  15954. '']),
  15955. LinesToStr([ // this.$init
  15956. '$impl.Obj.$DoIntern();',
  15957. '$impl.Obj.$DoIntern();',
  15958. '$impl.Obj.$DoIntern2();',
  15959. '$impl.Obj.$DoIntern2();',
  15960. '$impl.Obj.DoIt();',
  15961. '$impl.Obj.DoIt();',
  15962. 'var $with = $impl.Obj;',
  15963. '$with.$DoIntern();',
  15964. '$with.$DoIntern();',
  15965. '$with.$DoIntern2();',
  15966. '$with.$DoIntern2();',
  15967. '']),
  15968. LinesToStr([ // implementation
  15969. '$impl.Obj = null;',
  15970. '']) );
  15971. end;
  15972. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  15973. begin
  15974. StartProgram(false);
  15975. Add('type');
  15976. Add(' TObject = class');
  15977. Add(' procedure DoIt; virtual; external name ''Foo'';');
  15978. Add(' end;');
  15979. Add('begin');
  15980. SetExpectedPasResolverError('Virtual method name must match external',
  15981. nVirtualMethodNameMustMatchExternal);
  15982. ConvertProgram;
  15983. end;
  15984. procedure TTestModule.TestClass_ExternalOverrideFail;
  15985. begin
  15986. StartProgram(false);
  15987. Add('type');
  15988. Add(' TObject = class');
  15989. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  15990. Add(' end;');
  15991. Add(' TCar = class');
  15992. Add(' procedure DoIt; override; external name ''DoIt'';');
  15993. Add(' end;');
  15994. Add('begin');
  15995. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  15996. nInvalidXModifierY);
  15997. ConvertProgram;
  15998. end;
  15999. procedure TTestModule.TestClass_ExternalVar;
  16000. begin
  16001. AddModuleWithIntfImplSrc('unit2.pas',
  16002. LinesToStr([
  16003. '{$modeswitch externalclass}',
  16004. 'type',
  16005. ' TObject = class',
  16006. ' public',
  16007. ' Intern: longint external name ''$Intern'';',
  16008. ' Bracket: longint external name ''["A B"]'';',
  16009. ' end;',
  16010. '']),
  16011. LinesToStr([
  16012. '']));
  16013. StartUnit(true);
  16014. Add([
  16015. 'interface',
  16016. 'uses unit2;',
  16017. '{$modeswitch externalclass}',
  16018. 'type',
  16019. ' TCar = class(tobject)',
  16020. ' public',
  16021. ' Intern2: longint external name ''$Intern2'';',
  16022. ' procedure DoIt;',
  16023. ' end;',
  16024. 'implementation',
  16025. 'procedure tcar.doit;',
  16026. 'begin',
  16027. ' Intern:=Intern+1;',
  16028. ' Intern2:=Intern2+2;',
  16029. ' Bracket:=Bracket+3;',
  16030. 'end;',
  16031. 'var Obj: TCar;',
  16032. 'begin',
  16033. ' obj.intern:=obj.intern+1;',
  16034. ' obj.intern2:=obj.intern2+2;',
  16035. ' obj.Bracket:=obj.Bracket+3;',
  16036. ' with obj do begin',
  16037. ' intern:=intern+1;',
  16038. ' intern2:=intern2+2;',
  16039. ' Bracket:=Bracket+3;',
  16040. ' end;']);
  16041. ConvertUnit;
  16042. CheckSource('TestClass_ExternalVar',
  16043. LinesToStr([
  16044. 'var $impl = $mod.$impl;',
  16045. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16046. ' this.DoIt = function () {',
  16047. ' this.$Intern = this.$Intern + 1;',
  16048. ' this.$Intern2 = this.$Intern2 + 2;',
  16049. ' this["A B"] = this["A B"] + 3;',
  16050. ' };',
  16051. ' });',
  16052. '']),
  16053. LinesToStr([
  16054. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16055. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16056. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16057. 'var $with = $impl.Obj;',
  16058. '$with.$Intern = $with.$Intern + 1;',
  16059. '$with.$Intern2 = $with.$Intern2 + 2;',
  16060. '$with["A B"] = $with["A B"] + 3;',
  16061. '']),
  16062. LinesToStr([ // implementation
  16063. '$impl.Obj = null;',
  16064. '']));
  16065. end;
  16066. procedure TTestModule.TestClass_Const;
  16067. begin
  16068. StartProgram(false);
  16069. Add([
  16070. 'type',
  16071. ' integer = longint;',
  16072. ' TClass = class of TObject;',
  16073. ' TObject = class',
  16074. ' public',
  16075. ' const cI: integer = 3;',
  16076. ' procedure DoIt;',
  16077. ' class procedure DoMore;',
  16078. ' end;',
  16079. 'procedure tobject.doit;',
  16080. 'begin',
  16081. ' if cI=4 then;',
  16082. ' if 5=cI then;',
  16083. ' if Self.cI=6 then;',
  16084. ' if 7=Self.cI then;',
  16085. ' with Self do begin',
  16086. ' if cI=11 then;',
  16087. ' if 12=cI then;',
  16088. ' end;',
  16089. 'end;',
  16090. 'class procedure tobject.domore;',
  16091. 'begin',
  16092. ' if cI=8 then;',
  16093. ' if Self.cI=9 then;',
  16094. ' if 10=cI then;',
  16095. ' if 11=Self.cI then;',
  16096. ' with Self do begin',
  16097. ' if cI=13 then;',
  16098. ' if 14=cI then;',
  16099. ' end;',
  16100. 'end;',
  16101. 'var',
  16102. ' Obj: TObject;',
  16103. ' Cla: TClass;',
  16104. 'begin',
  16105. ' if TObject.cI=21 then ;',
  16106. ' if Obj.cI=22 then ;',
  16107. ' if Cla.cI=23 then ;',
  16108. ' with obj do if ci=24 then;',
  16109. ' with TObject do if ci=25 then;',
  16110. ' with Cla do if ci=26 then;']);
  16111. ConvertProgram;
  16112. CheckSource('TestClass_Const',
  16113. LinesToStr([
  16114. 'rtl.createClass(this, "TObject", null, function () {',
  16115. ' this.cI = 3;',
  16116. ' this.$init = function () {',
  16117. ' };',
  16118. ' this.$final = function () {',
  16119. ' };',
  16120. ' this.DoIt = function () {',
  16121. ' if (this.cI === 4) ;',
  16122. ' if (5 === this.cI) ;',
  16123. ' if (this.cI === 6) ;',
  16124. ' if (7 === this.cI) ;',
  16125. ' if (this.cI === 11) ;',
  16126. ' if (12 === this.cI) ;',
  16127. ' };',
  16128. ' this.DoMore = function () {',
  16129. ' if (this.cI === 8) ;',
  16130. ' if (this.cI === 9) ;',
  16131. ' if (10 === this.cI) ;',
  16132. ' if (11 === this.cI) ;',
  16133. ' if (this.cI === 13) ;',
  16134. ' if (14 === this.cI) ;',
  16135. ' };',
  16136. '});',
  16137. 'this.Obj = null;',
  16138. 'this.Cla = null;',
  16139. '']),
  16140. LinesToStr([
  16141. 'if ($mod.TObject.cI === 21) ;',
  16142. 'if ($mod.Obj.cI === 22) ;',
  16143. 'if ($mod.Cla.cI === 23) ;',
  16144. 'var $with = $mod.Obj;',
  16145. 'if ($with.cI === 24) ;',
  16146. 'var $with1 = $mod.TObject;',
  16147. 'if ($with1.cI === 25) ;',
  16148. 'var $with2 = $mod.Cla;',
  16149. 'if ($with2.cI === 26) ;',
  16150. '']));
  16151. end;
  16152. procedure TTestModule.TestClass_ConstEnum;
  16153. begin
  16154. StartProgram(false);
  16155. Add([
  16156. 'type',
  16157. ' TEnum = (red,blue);',
  16158. ' TObject = class',
  16159. ' end;',
  16160. ' TAnimal = class',
  16161. ' public',
  16162. ' type TSubEnum = (light,dark);',
  16163. ' const a = high(TEnum);',
  16164. ' const b = high(TSubEnum);',
  16165. ' end;',
  16166. ' TBird = class(TAnimal)',
  16167. ' public',
  16168. ' const c = high(TEnum);',
  16169. ' const d = high(TSubEnum);',
  16170. ' end;',
  16171. ' TAnt = class',
  16172. ' public',
  16173. ' const e = high(TEnum);',
  16174. ' const f = high(TBird.TSubEnum);',
  16175. ' end;',
  16176. 'begin',
  16177. '']);
  16178. ConvertProgram;
  16179. CheckSource('TestClass_ConstEnum',
  16180. LinesToStr([
  16181. 'this.TEnum = {',
  16182. ' "0": "red",',
  16183. ' red: 0,',
  16184. ' "1": "blue",',
  16185. ' blue: 1',
  16186. '};',
  16187. 'rtl.createClass(this, "TObject", null, function () {',
  16188. ' this.$init = function () {',
  16189. ' };',
  16190. ' this.$final = function () {',
  16191. ' };',
  16192. '});',
  16193. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16194. ' this.TSubEnum = {',
  16195. ' "0": "light",',
  16196. ' light: 0,',
  16197. ' "1": "dark",',
  16198. ' dark: 1',
  16199. ' };',
  16200. ' this.a = $mod.TEnum.blue;',
  16201. ' this.b = this.TSubEnum.dark;',
  16202. '});',
  16203. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16204. ' this.c = $mod.TEnum.blue;',
  16205. ' this.d = this.TSubEnum.dark;',
  16206. '});',
  16207. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16208. ' this.e = $mod.TEnum.blue;',
  16209. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16210. '});',
  16211. '']),
  16212. LinesToStr([
  16213. '']));
  16214. end;
  16215. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16216. begin
  16217. StartProgram(false);
  16218. Add([
  16219. 'type',
  16220. ' TObject = class',
  16221. ' const cI: longint = 3;',
  16222. ' procedure Fly;',
  16223. ' procedure Run;',
  16224. ' end;',
  16225. ' TBird = class',
  16226. ' procedure Go;',
  16227. ' end;',
  16228. 'procedure tobject.fly;',
  16229. 'const cI: word = 4;',
  16230. 'begin',
  16231. ' if cI=Self.cI then ;',
  16232. 'end;',
  16233. 'procedure tobject.run;',
  16234. 'const cI: word = 5;',
  16235. 'begin',
  16236. ' if cI=Self.cI then ;',
  16237. 'end;',
  16238. 'procedure tbird.go;',
  16239. 'const cI: word = 6;',
  16240. 'begin',
  16241. ' if cI=Self.cI then ;',
  16242. 'end;',
  16243. 'begin',
  16244. '']);
  16245. ConvertProgram;
  16246. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16247. LinesToStr([
  16248. 'rtl.createClass(this, "TObject", null, function () {',
  16249. ' this.cI = 3;',
  16250. ' this.$init = function () {',
  16251. ' };',
  16252. ' this.$final = function () {',
  16253. ' };',
  16254. ' var cI$1 = 4;',
  16255. ' this.Fly = function () {',
  16256. ' if (cI$1 === this.cI) ;',
  16257. ' };',
  16258. ' var cI$2 = 5;',
  16259. ' this.Run = function () {',
  16260. ' if (cI$2 === this.cI) ;',
  16261. ' };',
  16262. '});',
  16263. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16264. ' var cI$3 = 6;',
  16265. ' this.Go = function () {',
  16266. ' if (cI$3 === this.cI) ;',
  16267. ' };',
  16268. '});',
  16269. '']),
  16270. LinesToStr([
  16271. '']));
  16272. end;
  16273. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16274. begin
  16275. StartUnit(false);
  16276. Add([
  16277. 'interface',
  16278. 'type',
  16279. ' TObject = class',
  16280. ' const cI: longint = 3;',
  16281. ' procedure Fly;',
  16282. ' procedure Run;',
  16283. ' end;',
  16284. ' TBird = class',
  16285. ' procedure Go;',
  16286. ' end;',
  16287. 'implementation',
  16288. 'procedure tobject.fly;',
  16289. 'const cI: word = 4;',
  16290. 'begin',
  16291. ' if cI=Self.cI then ;',
  16292. 'end;',
  16293. 'procedure tobject.run;',
  16294. 'const cI: word = 5;',
  16295. 'begin',
  16296. ' if cI=Self.cI then ;',
  16297. 'end;',
  16298. 'procedure tbird.go;',
  16299. 'const cI: word = 6;',
  16300. 'begin',
  16301. ' if cI=Self.cI then ;',
  16302. 'end;',
  16303. '']);
  16304. ConvertUnit;
  16305. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16306. LinesToStr([
  16307. 'rtl.createClass(this, "TObject", null, function () {',
  16308. ' this.cI = 3;',
  16309. ' this.$init = function () {',
  16310. ' };',
  16311. ' this.$final = function () {',
  16312. ' };',
  16313. ' var cI$1 = 4;',
  16314. ' this.Fly = function () {',
  16315. ' if (cI$1 === this.cI) ;',
  16316. ' };',
  16317. ' var cI$2 = 5;',
  16318. ' this.Run = function () {',
  16319. ' if (cI$2 === this.cI) ;',
  16320. ' };',
  16321. '});',
  16322. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16323. ' var cI$3 = 6;',
  16324. ' this.Go = function () {',
  16325. ' if (cI$3 === this.cI) ;',
  16326. ' };',
  16327. '});',
  16328. '']),
  16329. '',
  16330. '');
  16331. end;
  16332. procedure TTestModule.TestClass_LocalVarSelfFail;
  16333. begin
  16334. StartProgram(false);
  16335. Add([
  16336. 'type',
  16337. ' TObject = class',
  16338. ' constructor Create;',
  16339. ' end;',
  16340. 'constructor tobject.create;',
  16341. 'var self: longint;',
  16342. 'begin',
  16343. 'end',
  16344. 'begin',
  16345. '']);
  16346. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16347. ConvertProgram;
  16348. end;
  16349. procedure TTestModule.TestClass_ArgSelfFail;
  16350. begin
  16351. StartProgram(false);
  16352. Add([
  16353. 'type',
  16354. ' TObject = class',
  16355. ' procedure DoIt(Self: longint);',
  16356. ' end;',
  16357. 'procedure tobject.doit(self: longint);',
  16358. 'begin',
  16359. 'end',
  16360. 'begin',
  16361. '']);
  16362. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16363. ConvertProgram;
  16364. end;
  16365. procedure TTestModule.TestClass_NestedProcSelf;
  16366. begin
  16367. StartProgram(false);
  16368. Add([
  16369. 'type',
  16370. ' TObject = class',
  16371. ' Key: longint;',
  16372. ' class var State: longint;',
  16373. ' procedure DoIt;',
  16374. ' function GetSize: longint; virtual; abstract;',
  16375. ' procedure SetSize(Value: longint); virtual; abstract;',
  16376. ' property Size: longint read GetSize write SetSize;',
  16377. ' end;',
  16378. 'procedure tobject.doit;',
  16379. ' procedure Sub;',
  16380. ' begin',
  16381. ' key:=key+2;',
  16382. ' self.key:=self.key+3;',
  16383. ' state:=state+4;',
  16384. ' self.state:=self.state+5;',
  16385. ' tobject.state:=tobject.state+6;',
  16386. ' size:=size+7;',
  16387. ' self.size:=self.size+8;',
  16388. ' end;',
  16389. 'begin',
  16390. ' sub;',
  16391. ' key:=key+12;',
  16392. ' self.key:=self.key+13;',
  16393. ' state:=state+14;',
  16394. ' self.state:=self.state+15;',
  16395. ' tobject.state:=tobject.state+16;',
  16396. ' size:=size+17;',
  16397. ' self.size:=self.size+18;',
  16398. 'end;',
  16399. 'begin',
  16400. '']);
  16401. ConvertProgram;
  16402. CheckSource('TestClass_NestedProcSelf',
  16403. LinesToStr([ // statements
  16404. 'rtl.createClass(this, "TObject", null, function () {',
  16405. ' this.State = 0;',
  16406. ' this.$init = function () {',
  16407. ' this.Key = 0;',
  16408. ' };',
  16409. ' this.$final = function () {',
  16410. ' };',
  16411. ' this.DoIt = function () {',
  16412. ' var $Self = this;',
  16413. ' function Sub() {',
  16414. ' $Self.Key = $Self.Key + 2;',
  16415. ' $Self.Key = $Self.Key + 3;',
  16416. ' $mod.TObject.State = $Self.State + 4;',
  16417. ' $mod.TObject.State = $Self.State + 5;',
  16418. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16419. ' $Self.SetSize($Self.GetSize() + 7);',
  16420. ' $Self.SetSize($Self.GetSize() + 8);',
  16421. ' };',
  16422. ' Sub();',
  16423. ' this.Key = this.Key + 12;',
  16424. ' $Self.Key = $Self.Key + 13;',
  16425. ' $mod.TObject.State = this.State + 14;',
  16426. ' $mod.TObject.State = $Self.State + 15;',
  16427. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16428. ' this.SetSize(this.GetSize() + 17);',
  16429. ' $Self.SetSize($Self.GetSize() + 18);',
  16430. ' };',
  16431. '});',
  16432. '']),
  16433. LinesToStr([ // $mod.$main
  16434. '']));
  16435. end;
  16436. procedure TTestModule.TestClass_NestedProcSelf2;
  16437. begin
  16438. StartProgram(false);
  16439. Add([
  16440. 'type',
  16441. ' TObject = class',
  16442. ' Key: longint;',
  16443. ' class var State: longint;',
  16444. ' function GetSize: longint; virtual; abstract;',
  16445. ' procedure SetSize(Value: longint); virtual; abstract;',
  16446. ' property Size: longint read GetSize write SetSize;',
  16447. ' end;',
  16448. ' TBird = class',
  16449. ' procedure DoIt;',
  16450. ' end;',
  16451. 'procedure tbird.doit;',
  16452. ' procedure Sub;',
  16453. ' begin',
  16454. ' key:=key+2;',
  16455. ' self.key:=self.key+3;',
  16456. ' state:=state+4;',
  16457. ' self.state:=self.state+5;',
  16458. ' tobject.state:=tobject.state+6;',
  16459. ' size:=size+7;',
  16460. ' self.size:=self.size+8;',
  16461. ' end;',
  16462. 'begin',
  16463. ' sub;',
  16464. ' key:=key+12;',
  16465. ' self.key:=self.key+13;',
  16466. ' state:=state+14;',
  16467. ' self.state:=self.state+15;',
  16468. ' tobject.state:=tobject.state+16;',
  16469. ' size:=size+17;',
  16470. ' self.size:=self.size+18;',
  16471. 'end;',
  16472. 'begin',
  16473. '']);
  16474. ConvertProgram;
  16475. CheckSource('TestClass_NestedProcSelf2',
  16476. LinesToStr([ // statements
  16477. 'rtl.createClass(this, "TObject", null, function () {',
  16478. ' this.State = 0;',
  16479. ' this.$init = function () {',
  16480. ' this.Key = 0;',
  16481. ' };',
  16482. ' this.$final = function () {',
  16483. ' };',
  16484. '});',
  16485. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16486. ' this.DoIt = function () {',
  16487. ' var $Self = this;',
  16488. ' function Sub() {',
  16489. ' $Self.Key = $Self.Key + 2;',
  16490. ' $Self.Key = $Self.Key + 3;',
  16491. ' $mod.TObject.State = $Self.State + 4;',
  16492. ' $mod.TObject.State = $Self.State + 5;',
  16493. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16494. ' $Self.SetSize($Self.GetSize() + 7);',
  16495. ' $Self.SetSize($Self.GetSize() + 8);',
  16496. ' };',
  16497. ' Sub();',
  16498. ' this.Key = this.Key + 12;',
  16499. ' $Self.Key = $Self.Key + 13;',
  16500. ' $mod.TObject.State = this.State + 14;',
  16501. ' $mod.TObject.State = $Self.State + 15;',
  16502. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16503. ' this.SetSize(this.GetSize() + 17);',
  16504. ' $Self.SetSize($Self.GetSize() + 18);',
  16505. ' };',
  16506. '});',
  16507. '']),
  16508. LinesToStr([ // $mod.$main
  16509. '']));
  16510. end;
  16511. procedure TTestModule.TestClass_NestedProcClassSelf;
  16512. begin
  16513. StartProgram(false);
  16514. Add([
  16515. 'type',
  16516. ' TObject = class',
  16517. ' class var State: longint;',
  16518. ' class procedure DoIt;',
  16519. ' class function GetSize: longint; virtual; abstract;',
  16520. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16521. ' class property Size: longint read GetSize write SetSize;',
  16522. ' end;',
  16523. 'class procedure tobject.doit;',
  16524. ' procedure Sub;',
  16525. ' begin',
  16526. ' state:=state+2;',
  16527. ' self.state:=self.state+3;',
  16528. ' tobject.state:=tobject.state+4;',
  16529. ' size:=size+5;',
  16530. ' self.size:=self.size+6;',
  16531. ' tobject.size:=tobject.size+7;',
  16532. ' end;',
  16533. 'begin',
  16534. ' sub;',
  16535. ' state:=state+12;',
  16536. ' self.state:=self.state+13;',
  16537. ' tobject.state:=tobject.state+14;',
  16538. ' size:=size+15;',
  16539. ' self.size:=self.size+16;',
  16540. ' tobject.size:=tobject.size+17;',
  16541. 'end;',
  16542. 'begin',
  16543. '']);
  16544. ConvertProgram;
  16545. CheckSource('TestClass_NestedProcClassSelf',
  16546. LinesToStr([ // statements
  16547. 'rtl.createClass(this, "TObject", null, function () {',
  16548. ' this.State = 0;',
  16549. ' this.$init = function () {',
  16550. ' };',
  16551. ' this.$final = function () {',
  16552. ' };',
  16553. ' this.DoIt = function () {',
  16554. ' var $Self = this;',
  16555. ' function Sub() {',
  16556. ' $mod.TObject.State = $Self.State + 2;',
  16557. ' $mod.TObject.State = $Self.State + 3;',
  16558. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16559. ' $Self.SetSize($Self.GetSize() + 5);',
  16560. ' $Self.SetSize($Self.GetSize() + 6);',
  16561. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16562. ' };',
  16563. ' Sub();',
  16564. ' $mod.TObject.State = this.State + 12;',
  16565. ' $mod.TObject.State = $Self.State + 13;',
  16566. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16567. ' this.SetSize(this.GetSize() + 15);',
  16568. ' $Self.SetSize($Self.GetSize() + 16);',
  16569. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16570. ' };',
  16571. '});',
  16572. '']),
  16573. LinesToStr([ // $mod.$main
  16574. '']));
  16575. end;
  16576. procedure TTestModule.TestClass_NestedProcCallInherited;
  16577. begin
  16578. StartProgram(false);
  16579. Add([
  16580. 'type',
  16581. ' TObject = class',
  16582. ' function DoIt(k: boolean): longint; virtual;',
  16583. ' end;',
  16584. ' TBird = class',
  16585. ' function DoIt(k: boolean): longint; override;',
  16586. ' end;',
  16587. 'function tobject.doit(k: boolean): longint;',
  16588. 'begin',
  16589. 'end;',
  16590. 'function tbird.doit(k: boolean): longint;',
  16591. ' procedure Sub;',
  16592. ' begin',
  16593. ' inherited DoIt(true);',
  16594. //' if inherited DoIt(false)=4 then ;',
  16595. ' end;',
  16596. 'begin',
  16597. ' Sub;',
  16598. ' inherited;',
  16599. ' inherited DoIt(true);',
  16600. //' if inherited DoIt(false)=14 then ;',
  16601. 'end;',
  16602. 'begin',
  16603. '']);
  16604. ConvertProgram;
  16605. CheckSource('TestClass_NestedProcCallInherited',
  16606. LinesToStr([ // statements
  16607. 'rtl.createClass(this, "TObject", null, function () {',
  16608. ' this.$init = function () {',
  16609. ' };',
  16610. ' this.$final = function () {',
  16611. ' };',
  16612. ' this.DoIt = function (k) {',
  16613. ' var Result = 0;',
  16614. ' return Result;',
  16615. ' };',
  16616. '});',
  16617. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16618. ' this.DoIt = function (k) {',
  16619. ' var $Self = this;',
  16620. ' var Result = 0;',
  16621. ' function Sub() {',
  16622. ' $mod.TObject.DoIt.call($Self, true);',
  16623. ' };',
  16624. ' Sub();',
  16625. ' $mod.TObject.DoIt.apply(this, arguments);',
  16626. ' $mod.TObject.DoIt.call(this, true);',
  16627. ' return Result;',
  16628. ' };',
  16629. '});',
  16630. '']),
  16631. LinesToStr([ // $mod.$main
  16632. '']));
  16633. end;
  16634. procedure TTestModule.TestClass_TObjectFree;
  16635. begin
  16636. StartProgram(false);
  16637. Add([
  16638. 'type',
  16639. ' TObject = class',
  16640. ' Obj: tobject;',
  16641. ' procedure Free;',
  16642. ' procedure Release;',
  16643. ' end;',
  16644. 'procedure tobject.free;',
  16645. 'begin',
  16646. 'end;',
  16647. 'procedure tobject.release;',
  16648. 'begin',
  16649. ' free;',
  16650. ' if true then free;',
  16651. 'end;',
  16652. 'function DoIt(o: tobject): tobject;',
  16653. 'var l: tobject;',
  16654. 'begin',
  16655. ' o.free;',
  16656. ' o.free();',
  16657. ' l.free;',
  16658. ' l.free();',
  16659. ' o.obj.free;',
  16660. ' o.obj.free();',
  16661. ' with o do obj.free;',
  16662. ' with o do obj.free();',
  16663. ' result.Free;',
  16664. ' result.Free();',
  16665. 'end;',
  16666. 'var o: tobject;',
  16667. ' a: array of tobject;',
  16668. 'begin',
  16669. ' o.free;',
  16670. ' o.obj.free;',
  16671. ' a[1+2].free;',
  16672. '']);
  16673. ConvertProgram;
  16674. CheckSource('TestClass_TObjectFree',
  16675. LinesToStr([ // statements
  16676. 'rtl.createClass(this, "TObject", null, function () {',
  16677. ' this.$init = function () {',
  16678. ' this.Obj = null;',
  16679. ' };',
  16680. ' this.$final = function () {',
  16681. ' this.Obj = undefined;',
  16682. ' };',
  16683. ' this.Free = function () {',
  16684. ' };',
  16685. ' this.Release = function () {',
  16686. ' this.Free();',
  16687. ' if (true) this.Free();',
  16688. ' };',
  16689. '});',
  16690. 'this.DoIt = function (o) {',
  16691. ' var Result = null;',
  16692. ' var l = null;',
  16693. ' o = rtl.freeLoc(o);',
  16694. ' o = rtl.freeLoc(o);',
  16695. ' l = rtl.freeLoc(l);',
  16696. ' l = rtl.freeLoc(l);',
  16697. ' rtl.free(o, "Obj");',
  16698. ' rtl.free(o, "Obj");',
  16699. ' rtl.free(o, "Obj");',
  16700. ' rtl.free(o, "Obj");',
  16701. ' Result = rtl.freeLoc(Result);',
  16702. ' Result = rtl.freeLoc(Result);',
  16703. ' return Result;',
  16704. '};',
  16705. 'this.o = null;',
  16706. 'this.a = [];',
  16707. '']),
  16708. LinesToStr([ // $mod.$main
  16709. 'rtl.free($mod, "o");',
  16710. 'rtl.free($mod.o, "Obj");',
  16711. 'rtl.free($mod.a, 1 + 2);',
  16712. '']));
  16713. end;
  16714. procedure TTestModule.TestClass_TObjectFree_VarArg;
  16715. begin
  16716. StartProgram(false);
  16717. Add([
  16718. 'type',
  16719. ' TObject = class',
  16720. ' Obj: tobject;',
  16721. ' procedure Free;',
  16722. ' end;',
  16723. 'procedure tobject.free;',
  16724. 'begin',
  16725. 'end;',
  16726. 'procedure DoIt(var o: tobject);',
  16727. 'begin',
  16728. ' o.free;',
  16729. ' o.free();',
  16730. 'end;',
  16731. 'begin',
  16732. '']);
  16733. ConvertProgram;
  16734. CheckSource('TestClass_TObjectFree_VarArg',
  16735. LinesToStr([ // statements
  16736. 'rtl.createClass(this, "TObject", null, function () {',
  16737. ' this.$init = function () {',
  16738. ' this.Obj = null;',
  16739. ' };',
  16740. ' this.$final = function () {',
  16741. ' this.Obj = undefined;',
  16742. ' };',
  16743. ' this.Free = function () {',
  16744. ' };',
  16745. '});',
  16746. 'this.DoIt = function (o) {',
  16747. ' o.set(rtl.freeLoc(o.get()));',
  16748. ' o.set(rtl.freeLoc(o.get()));',
  16749. '};',
  16750. '']),
  16751. LinesToStr([ // $mod.$main
  16752. '']));
  16753. end;
  16754. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  16755. begin
  16756. StartProgram(false);
  16757. Add([
  16758. 'type',
  16759. ' TObject = class',
  16760. ' constructor Create;',
  16761. ' procedure Free;',
  16762. ' end;',
  16763. 'constructor TObject.Create; begin end;',
  16764. 'procedure tobject.free; begin end;',
  16765. 'begin',
  16766. ' with tobject.create do free;',
  16767. '']);
  16768. ConvertProgram;
  16769. CheckSource('TestClass_TObjectFreeNewInstance',
  16770. LinesToStr([ // statements
  16771. 'rtl.createClass(this, "TObject", null, function () {',
  16772. ' this.$init = function () {',
  16773. ' };',
  16774. ' this.$final = function () {',
  16775. ' };',
  16776. ' this.Create = function () {',
  16777. ' return this;',
  16778. ' };',
  16779. ' this.Free = function () {',
  16780. ' };',
  16781. '});',
  16782. '']),
  16783. LinesToStr([ // $mod.$main
  16784. 'var $with = $mod.TObject.$create("Create");',
  16785. '$with=rtl.freeLoc($with);',
  16786. '']));
  16787. end;
  16788. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  16789. begin
  16790. StartProgram(false);
  16791. Add([
  16792. 'type',
  16793. ' TObject = class',
  16794. ' destructor Destroy;',
  16795. ' procedure Free;',
  16796. ' end;',
  16797. 'destructor TObject.Destroy; begin end;',
  16798. 'procedure tobject.free; begin end;',
  16799. 'var o: tobject;',
  16800. 'begin',
  16801. ' o.free;',
  16802. '']);
  16803. Converter.UseLowerCase:=true;
  16804. ConvertProgram;
  16805. CheckSource('TestClass_TObjectFreeLowerCase',
  16806. LinesToStr([ // statements
  16807. 'rtl.createClass(this, "tobject", null, function () {',
  16808. ' this.$init = function () {',
  16809. ' };',
  16810. ' this.$final = function () {',
  16811. ' };',
  16812. ' rtl.tObjectDestroy = "destroy";',
  16813. ' this.destroy = function () {',
  16814. ' };',
  16815. ' this.free = function () {',
  16816. ' };',
  16817. '});',
  16818. 'this.o = null;',
  16819. '']),
  16820. LinesToStr([ // $mod.$main
  16821. 'rtl.free($mod, "o");',
  16822. '']));
  16823. end;
  16824. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  16825. begin
  16826. StartProgram(false);
  16827. Add([
  16828. 'type',
  16829. ' TObject = class',
  16830. ' procedure Free;',
  16831. ' function GetObj: tobject; virtual; abstract;',
  16832. ' end;',
  16833. 'procedure tobject.free;',
  16834. 'begin',
  16835. 'end;',
  16836. 'var o: tobject;',
  16837. 'begin',
  16838. ' o.getobj.free;',
  16839. '']);
  16840. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  16841. ConvertProgram;
  16842. end;
  16843. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  16844. begin
  16845. StartProgram(false);
  16846. Add([
  16847. 'type',
  16848. ' TObject = class',
  16849. ' procedure Free;',
  16850. ' FObj: TObject;',
  16851. ' property Obj: tobject read FObj write FObj;',
  16852. ' end;',
  16853. 'procedure tobject.free;',
  16854. 'begin',
  16855. 'end;',
  16856. 'var o: tobject;',
  16857. 'begin',
  16858. ' o.obj.free;',
  16859. '']);
  16860. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  16861. ConvertProgram;
  16862. end;
  16863. procedure TTestModule.TestClass_ForIn;
  16864. begin
  16865. StartProgram(false);
  16866. Add([
  16867. 'type',
  16868. ' TObject = class end;',
  16869. ' TItem = TObject;',
  16870. ' TEnumerator = class',
  16871. ' FCurrent: TItem;',
  16872. ' property Current: TItem read FCurrent;',
  16873. ' function MoveNext: boolean;',
  16874. ' end;',
  16875. ' TBird = class',
  16876. ' function GetEnumerator: TEnumerator;',
  16877. ' end;',
  16878. 'function TEnumerator.MoveNext: boolean;',
  16879. 'begin',
  16880. 'end;',
  16881. 'function TBird.GetEnumerator: TEnumerator;',
  16882. 'begin',
  16883. 'end;',
  16884. 'var',
  16885. ' b: TBird;',
  16886. ' i, i2: TItem;',
  16887. 'begin',
  16888. ' for i in b do i2:=i;']);
  16889. ConvertProgram;
  16890. CheckSource('TestClass_ForIn',
  16891. LinesToStr([ // statements
  16892. 'rtl.createClass(this, "TObject", null, function () {',
  16893. ' this.$init = function () {',
  16894. ' };',
  16895. ' this.$final = function () {',
  16896. ' };',
  16897. '});',
  16898. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  16899. ' this.$init = function () {',
  16900. ' $mod.TObject.$init.call(this);',
  16901. ' this.FCurrent = null;',
  16902. ' };',
  16903. ' this.$final = function () {',
  16904. ' this.FCurrent = undefined;',
  16905. ' $mod.TObject.$final.call(this);',
  16906. ' };',
  16907. ' this.MoveNext = function () {',
  16908. ' var Result = false;',
  16909. ' return Result;',
  16910. ' };',
  16911. '});',
  16912. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16913. ' this.GetEnumerator = function () {',
  16914. ' var Result = null;',
  16915. ' return Result;',
  16916. ' };',
  16917. '});',
  16918. 'this.b = null;',
  16919. 'this.i = null;',
  16920. 'this.i2 = null;'
  16921. ]),
  16922. LinesToStr([ // $mod.$main
  16923. 'var $in = $mod.b.GetEnumerator();',
  16924. 'try {',
  16925. ' while ($in.MoveNext()){',
  16926. ' $mod.i = $in.FCurrent;',
  16927. ' $mod.i2 = $mod.i;',
  16928. ' }',
  16929. '} finally {',
  16930. ' $in = rtl.freeLoc($in)',
  16931. '};',
  16932. '']));
  16933. end;
  16934. procedure TTestModule.TestClass_DispatchMessage;
  16935. begin
  16936. StartProgram(false);
  16937. Add([
  16938. 'type',
  16939. ' TObject = class',
  16940. ' {$DispatchField DispInt}',
  16941. ' procedure Dispatch(var Msg); virtual; abstract;',
  16942. ' {$DispatchStrField DispStr}',
  16943. ' procedure DispatchStr(var Msg); virtual; abstract;',
  16944. ' end;',
  16945. ' THopMsg = record',
  16946. ' DispInt: longint;',
  16947. ' end;',
  16948. ' TPutMsg = record',
  16949. ' DispStr: string;',
  16950. ' end;',
  16951. ' TBird = class',
  16952. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  16953. ' procedure Run; overload; virtual; abstract;',
  16954. ' procedure Run(var Msg); overload; message ''Fast'';',
  16955. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  16956. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  16957. ' end;',
  16958. 'procedure TBird.Run(var Msg);',
  16959. 'begin',
  16960. 'end;',
  16961. 'begin',
  16962. '']);
  16963. ConvertProgram;
  16964. CheckSource('TestClass_Message',
  16965. LinesToStr([ // statements
  16966. 'rtl.createClass(this, "TObject", null, function () {',
  16967. ' this.$init = function () {',
  16968. ' };',
  16969. ' this.$final = function () {',
  16970. ' };',
  16971. '});',
  16972. 'rtl.recNewT(this, "THopMsg", function () {',
  16973. ' this.DispInt = 0;',
  16974. ' this.$eq = function (b) {',
  16975. ' return this.DispInt === b.DispInt;',
  16976. ' };',
  16977. ' this.$assign = function (s) {',
  16978. ' this.DispInt = s.DispInt;',
  16979. ' return this;',
  16980. ' };',
  16981. '});',
  16982. 'rtl.recNewT(this, "TPutMsg", function () {',
  16983. ' this.DispStr = "";',
  16984. ' this.$eq = function (b) {',
  16985. ' return this.DispStr === b.DispStr;',
  16986. ' };',
  16987. ' this.$assign = function (s) {',
  16988. ' this.DispStr = s.DispStr;',
  16989. ' return this;',
  16990. ' };',
  16991. '});',
  16992. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16993. ' this.Run$1 = function (Msg) {',
  16994. ' };',
  16995. ' this.$msgint = {',
  16996. ' "2": "Fly",',
  16997. ' "3": "Hop"',
  16998. ' };',
  16999. ' this.$msgstr = {',
  17000. ' Fast: "Run$1",',
  17001. ' foo: "Put"',
  17002. ' };',
  17003. '});',
  17004. '']),
  17005. LinesToStr([ // $mod.$main
  17006. '']));
  17007. end;
  17008. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17009. begin
  17010. StartProgram(false);
  17011. Add([
  17012. 'type',
  17013. ' TObject = class',
  17014. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17015. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17016. ' end;',
  17017. 'begin',
  17018. '']);
  17019. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17020. ConvertProgram;
  17021. end;
  17022. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17023. begin
  17024. StartProgram(false);
  17025. Add([
  17026. 'type',
  17027. ' TObject = class',
  17028. ' {$dispatchfield Msg}',
  17029. ' procedure Dispatch(var Msg); virtual; abstract;',
  17030. ' end;',
  17031. ' TFlyMsg = record',
  17032. ' FlyId: longint;',
  17033. ' end;',
  17034. ' TBird = class',
  17035. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17036. ' end;',
  17037. 'begin',
  17038. '']);
  17039. ConvertProgram;
  17040. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17041. end;
  17042. procedure TTestModule.TestClassOf_Create;
  17043. begin
  17044. StartProgram(false);
  17045. Add('type');
  17046. Add(' TObject = class');
  17047. Add(' constructor Create;');
  17048. Add(' end;');
  17049. Add(' TClass = class of TObject;');
  17050. Add('constructor tobject.create; begin end;');
  17051. Add('var');
  17052. Add(' Obj: tobject;');
  17053. Add(' C: tclass;');
  17054. Add('begin');
  17055. Add(' obj:=C.create;');
  17056. Add(' with c do obj:=create;');
  17057. ConvertProgram;
  17058. CheckSource('TestClassOf_Create',
  17059. LinesToStr([ // statements
  17060. 'rtl.createClass(this, "TObject", null, function () {',
  17061. ' this.$init = function () {',
  17062. ' };',
  17063. ' this.$final = function () {',
  17064. ' };',
  17065. ' this.Create = function () {',
  17066. ' return this;',
  17067. ' };',
  17068. '});',
  17069. 'this.Obj = null;',
  17070. 'this.C = null;'
  17071. ]),
  17072. LinesToStr([ // $mod.$main
  17073. '$mod.Obj = $mod.C.$create("Create");',
  17074. 'var $with = $mod.C;',
  17075. '$mod.Obj = $with.$create("Create");',
  17076. '']));
  17077. end;
  17078. procedure TTestModule.TestClassOf_Call;
  17079. begin
  17080. StartProgram(false);
  17081. Add('type');
  17082. Add(' TObject = class');
  17083. Add(' class procedure DoIt;');
  17084. Add(' end;');
  17085. Add(' TClass = class of TObject;');
  17086. Add('class procedure tobject.doit; begin end;');
  17087. Add('var');
  17088. Add(' C: tclass;');
  17089. Add('begin');
  17090. Add(' c.doit;');
  17091. Add(' with c do doit;');
  17092. ConvertProgram;
  17093. CheckSource('TestClassOf_Call',
  17094. LinesToStr([ // statements
  17095. 'rtl.createClass(this, "TObject", null, function () {',
  17096. ' this.$init = function () {',
  17097. ' };',
  17098. ' this.$final = function () {',
  17099. ' };',
  17100. ' this.DoIt = function () {',
  17101. ' };',
  17102. '});',
  17103. 'this.C = null;'
  17104. ]),
  17105. LinesToStr([ // $mod.$main
  17106. '$mod.C.DoIt();',
  17107. 'var $with = $mod.C;',
  17108. '$with.DoIt();',
  17109. '']));
  17110. end;
  17111. procedure TTestModule.TestClassOf_Assign;
  17112. begin
  17113. StartProgram(false);
  17114. Add('type');
  17115. Add(' TClass = class of TObject;');
  17116. Add(' TObject = class');
  17117. Add(' ClassType: TClass; ');
  17118. Add(' end;');
  17119. Add('var');
  17120. Add(' Obj: tobject;');
  17121. Add(' C: tclass;');
  17122. Add('begin');
  17123. Add(' c:=nil;');
  17124. Add(' c:=obj.classtype;');
  17125. ConvertProgram;
  17126. CheckSource('TestClassOf_Assign',
  17127. LinesToStr([ // statements
  17128. 'rtl.createClass(this, "TObject", null, function () {',
  17129. ' this.$init = function () {',
  17130. ' this.ClassType = null;',
  17131. ' };',
  17132. ' this.$final = function () {',
  17133. ' this.ClassType = undefined;',
  17134. ' };',
  17135. '});',
  17136. 'this.Obj = null;',
  17137. 'this.C = null;'
  17138. ]),
  17139. LinesToStr([ // $mod.$main
  17140. '$mod.C = null;',
  17141. '$mod.C = $mod.Obj.ClassType;',
  17142. '']));
  17143. end;
  17144. procedure TTestModule.TestClassOf_Is;
  17145. begin
  17146. StartProgram(false);
  17147. Add('type');
  17148. Add(' TClass = class of TObject;');
  17149. Add(' TObject = class');
  17150. Add(' end;');
  17151. Add(' TCar = class');
  17152. Add(' end;');
  17153. Add(' TCars = class of TCar;');
  17154. Add('var');
  17155. Add(' Obj: tobject;');
  17156. Add(' C: tclass;');
  17157. Add(' Cars: tcars;');
  17158. Add('begin');
  17159. Add(' if c is tcar then ;');
  17160. Add(' if c is tcars then ;');
  17161. ConvertProgram;
  17162. CheckSource('TestClassOf_Is',
  17163. LinesToStr([ // statements
  17164. 'rtl.createClass(this, "TObject", null, function () {',
  17165. ' this.$init = function () {',
  17166. ' };',
  17167. ' this.$final = function () {',
  17168. ' };',
  17169. '});',
  17170. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17171. '});',
  17172. 'this.Obj = null;',
  17173. 'this.C = null;',
  17174. 'this.Cars = null;'
  17175. ]),
  17176. LinesToStr([ // $mod.$main
  17177. 'if(rtl.is($mod.C,$mod.TCar));',
  17178. 'if(rtl.is($mod.C,$mod.TCar));',
  17179. '']));
  17180. end;
  17181. procedure TTestModule.TestClassOf_Compare;
  17182. begin
  17183. StartProgram(false);
  17184. Add('type');
  17185. Add(' TClass = class of TObject;');
  17186. Add(' TObject = class');
  17187. Add(' ClassType: TClass; ');
  17188. Add(' end;');
  17189. Add('var');
  17190. Add(' b: boolean;');
  17191. Add(' Obj: tobject;');
  17192. Add(' C: tclass;');
  17193. Add('begin');
  17194. Add(' b:=c=nil;');
  17195. Add(' b:=nil=c;');
  17196. Add(' b:=c=obj.classtype;');
  17197. Add(' b:=obj.classtype=c;');
  17198. Add(' b:=c=TObject;');
  17199. Add(' b:=TObject=c;');
  17200. Add(' b:=c<>nil;');
  17201. Add(' b:=nil<>c;');
  17202. Add(' b:=c<>obj.classtype;');
  17203. Add(' b:=obj.classtype<>c;');
  17204. Add(' b:=c<>TObject;');
  17205. Add(' b:=TObject<>c;');
  17206. ConvertProgram;
  17207. CheckSource('TestClassOf_Compare',
  17208. LinesToStr([ // statements
  17209. 'rtl.createClass(this, "TObject", null, function () {',
  17210. ' this.$init = function () {',
  17211. ' this.ClassType = null;',
  17212. ' };',
  17213. ' this.$final = function () {',
  17214. ' this.ClassType = undefined;',
  17215. ' };',
  17216. '});',
  17217. 'this.b = false;',
  17218. 'this.Obj = null;',
  17219. 'this.C = null;'
  17220. ]),
  17221. LinesToStr([ // $mod.$main
  17222. '$mod.b = $mod.C === null;',
  17223. '$mod.b = null === $mod.C;',
  17224. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17225. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17226. '$mod.b = $mod.C === $mod.TObject;',
  17227. '$mod.b = $mod.TObject === $mod.C;',
  17228. '$mod.b = $mod.C !== null;',
  17229. '$mod.b = null !== $mod.C;',
  17230. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17231. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17232. '$mod.b = $mod.C !== $mod.TObject;',
  17233. '$mod.b = $mod.TObject !== $mod.C;',
  17234. '']));
  17235. end;
  17236. procedure TTestModule.TestClassOf_ClassVar;
  17237. begin
  17238. StartProgram(false);
  17239. Add('type');
  17240. Add(' TObject = class');
  17241. Add(' class var id: longint;');
  17242. Add(' end;');
  17243. Add(' TClass = class of TObject;');
  17244. Add('var');
  17245. Add(' C: tclass;');
  17246. Add('begin');
  17247. Add(' C.id:=C.id;');
  17248. ConvertProgram;
  17249. CheckSource('TestClassOf_ClassVar',
  17250. LinesToStr([ // statements
  17251. 'rtl.createClass(this, "TObject", null, function () {',
  17252. ' this.id = 0;',
  17253. ' this.$init = function () {',
  17254. ' };',
  17255. ' this.$final = function () {',
  17256. ' };',
  17257. '});',
  17258. 'this.C = null;'
  17259. ]),
  17260. LinesToStr([ // $mod.$main
  17261. '$mod.TObject.id = $mod.C.id;',
  17262. '']));
  17263. end;
  17264. procedure TTestModule.TestClassOf_ClassMethod;
  17265. begin
  17266. StartProgram(false);
  17267. Add('type');
  17268. Add(' TObject = class');
  17269. Add(' class function DoIt(i: longint = 0): longint;');
  17270. Add(' end;');
  17271. Add(' TClass = class of TObject;');
  17272. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17273. Add('var');
  17274. Add(' i: longint;');
  17275. Add(' C: tclass;');
  17276. Add('begin');
  17277. Add(' C.DoIt;');
  17278. Add(' C.DoIt();');
  17279. Add(' i:=C.DoIt;');
  17280. Add(' i:=C.DoIt();');
  17281. ConvertProgram;
  17282. CheckSource('TestClassOf_ClassMethod',
  17283. LinesToStr([ // statements
  17284. 'rtl.createClass(this, "TObject", null, function () {',
  17285. ' this.$init = function () {',
  17286. ' };',
  17287. ' this.$final = function () {',
  17288. ' };',
  17289. ' this.DoIt = function (i) {',
  17290. ' var Result = 0;',
  17291. ' return Result;',
  17292. ' };',
  17293. '});',
  17294. 'this.i = 0;',
  17295. 'this.C = null;'
  17296. ]),
  17297. LinesToStr([ // $mod.$main
  17298. '$mod.C.DoIt(0);',
  17299. '$mod.C.DoIt(0);',
  17300. '$mod.i = $mod.C.DoIt(0);',
  17301. '$mod.i = $mod.C.DoIt(0);',
  17302. '']));
  17303. end;
  17304. procedure TTestModule.TestClassOf_ClassProperty;
  17305. begin
  17306. StartProgram(false);
  17307. Add([
  17308. 'type',
  17309. ' TObject = class',
  17310. ' class var FA: longint;',
  17311. ' class function GetA: longint;',
  17312. ' class procedure SetA(Value: longint);',
  17313. ' class property pA: longint read fa write fa;',
  17314. ' class property pB: longint read geta write seta;',
  17315. ' end;',
  17316. ' TObjectClass = class of tobject;',
  17317. 'class function tobject.geta: longint; begin end;',
  17318. 'class procedure tobject.seta(value: longint); begin end;',
  17319. 'var',
  17320. ' b: boolean;',
  17321. ' Obj: tobject;',
  17322. ' Cla: tobjectclass;',
  17323. 'begin',
  17324. ' obj.pa:=obj.pa;',
  17325. ' obj.pb:=obj.pb;',
  17326. ' b:=obj.pa=4;',
  17327. ' b:=obj.pb=obj.pb;',
  17328. ' b:=5=obj.pa;',
  17329. ' cla.pa:=6;',
  17330. ' cla.pa:=cla.pa;',
  17331. ' cla.pb:=cla.pb;',
  17332. ' b:=cla.pa=7;',
  17333. ' b:=cla.pb=cla.pb;',
  17334. ' b:=8=cla.pa;',
  17335. ' tobject.pa:=9;',
  17336. ' tobject.pb:=tobject.pb;',
  17337. ' b:=tobject.pa=10;',
  17338. ' b:=11=tobject.pa;',
  17339. '']);
  17340. ConvertProgram;
  17341. CheckSource('TestClassOf_ClassProperty',
  17342. LinesToStr([ // statements
  17343. 'rtl.createClass(this, "TObject", null, function () {',
  17344. ' this.FA = 0;',
  17345. ' this.$init = function () {',
  17346. ' };',
  17347. ' this.$final = function () {',
  17348. ' };',
  17349. ' this.GetA = function () {',
  17350. ' var Result = 0;',
  17351. ' return Result;',
  17352. ' };',
  17353. ' this.SetA = function (Value) {',
  17354. ' };',
  17355. '});',
  17356. 'this.b = false;',
  17357. 'this.Obj = null;',
  17358. 'this.Cla = null;'
  17359. ]),
  17360. LinesToStr([ // $mod.$main
  17361. '$mod.TObject.FA = $mod.Obj.FA;',
  17362. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17363. '$mod.b = $mod.Obj.FA === 4;',
  17364. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17365. '$mod.b = 5 === $mod.Obj.FA;',
  17366. '$mod.TObject.FA = 6;',
  17367. '$mod.TObject.FA = $mod.Cla.FA;',
  17368. '$mod.Cla.SetA($mod.Cla.GetA());',
  17369. '$mod.b = $mod.Cla.FA === 7;',
  17370. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17371. '$mod.b = 8 === $mod.Cla.FA;',
  17372. '$mod.TObject.FA = 9;',
  17373. '$mod.TObject.SetA($mod.TObject.GetA());',
  17374. '$mod.b = $mod.TObject.FA === 10;',
  17375. '$mod.b = 11 === $mod.TObject.FA;',
  17376. '']));
  17377. end;
  17378. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17379. begin
  17380. StartProgram(false);
  17381. Add('type');
  17382. Add(' TObject = class');
  17383. Add(' class var GlobalId: longint;');
  17384. Add(' class procedure ProcA;');
  17385. Add(' end;');
  17386. Add('class procedure tobject.proca;');
  17387. Add('var b: boolean;');
  17388. Add('begin');
  17389. Add(' b:=self=nil;');
  17390. Add(' b:=self.globalid=3;');
  17391. Add(' b:=4=self.globalid;');
  17392. Add(' self.globalid:=5;');
  17393. Add(' self.proca;');
  17394. Add('end;');
  17395. Add('begin');
  17396. ConvertProgram;
  17397. CheckSource('TestClassOf_ClassMethodSelf',
  17398. LinesToStr([ // statements
  17399. 'rtl.createClass(this, "TObject", null, function () {',
  17400. ' this.GlobalId = 0;',
  17401. ' this.$init = function () {',
  17402. ' };',
  17403. ' this.$final = function () {',
  17404. ' };',
  17405. ' this.ProcA = function () {',
  17406. ' var b = false;',
  17407. ' b = this === null;',
  17408. ' b = this.GlobalId === 3;',
  17409. ' b = 4 === this.GlobalId;',
  17410. ' $mod.TObject.GlobalId = 5;',
  17411. ' this.ProcA();',
  17412. ' };',
  17413. '});'
  17414. ]),
  17415. LinesToStr([ // $mod.$main
  17416. '']));
  17417. end;
  17418. procedure TTestModule.TestClassOf_TypeCast;
  17419. begin
  17420. StartProgram(false);
  17421. Add('type');
  17422. Add(' TObject = class');
  17423. Add(' class procedure {#TObject_DoIt}DoIt;');
  17424. Add(' end;');
  17425. Add(' TClass = class of TObject;');
  17426. Add(' TMobile = class');
  17427. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17428. Add(' end;');
  17429. Add(' TMobileClass = class of TMobile;');
  17430. Add(' TCar = class(TMobile)');
  17431. Add(' class procedure {#TCar_DoIt}DoIt;');
  17432. Add(' end;');
  17433. Add(' TCarClass = class of TCar;');
  17434. Add('class procedure TObject.DoIt;');
  17435. Add('begin');
  17436. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17437. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17438. Add('end;');
  17439. Add('class procedure TMobile.DoIt;');
  17440. Add('begin');
  17441. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17442. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17443. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17444. Add('end;');
  17445. Add('class procedure TCar.DoIt; begin end;');
  17446. Add('var');
  17447. Add(' ObjC: TClass;');
  17448. Add(' MobileC: TMobileClass;');
  17449. Add(' CarC: TCarClass;');
  17450. Add('begin');
  17451. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17452. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17453. Add(' CarC.{@TCar_DoIt}DoIt;');
  17454. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17455. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17456. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17457. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17458. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17459. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17460. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17461. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17462. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17463. ConvertProgram;
  17464. CheckSource('TestClassOf_TypeCast',
  17465. LinesToStr([ // statements
  17466. 'rtl.createClass(this, "TObject", null, function () {',
  17467. ' this.$init = function () {',
  17468. ' };',
  17469. ' this.$final = function () {',
  17470. ' };',
  17471. ' this.DoIt = function () {',
  17472. ' this.DoIt();',
  17473. ' this.DoIt$1();',
  17474. ' };',
  17475. '});',
  17476. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17477. ' this.DoIt$1 = function () {',
  17478. ' this.DoIt();',
  17479. ' this.DoIt$1();',
  17480. ' this.DoIt$2();',
  17481. ' };',
  17482. '});',
  17483. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17484. ' this.DoIt$2 = function () {',
  17485. ' };',
  17486. '});',
  17487. 'this.ObjC = null;',
  17488. 'this.MobileC = null;',
  17489. 'this.CarC = null;',
  17490. '']),
  17491. LinesToStr([ // $mod.$main
  17492. '$mod.ObjC.DoIt();',
  17493. '$mod.MobileC.DoIt$1();',
  17494. '$mod.CarC.DoIt$2();',
  17495. '$mod.ObjC.DoIt();',
  17496. '$mod.ObjC.DoIt$1();',
  17497. '$mod.ObjC.DoIt$2();',
  17498. '$mod.MobileC.DoIt();',
  17499. '$mod.MobileC.DoIt$1();',
  17500. '$mod.MobileC.DoIt$2();',
  17501. '$mod.CarC.DoIt();',
  17502. '$mod.CarC.DoIt$1();',
  17503. '$mod.CarC.DoIt$2();',
  17504. '']));
  17505. end;
  17506. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17507. begin
  17508. StartProgram(false);
  17509. Add('type');
  17510. Add(' TObject = class');
  17511. Add(' function CurNow: longint; ');
  17512. Add(' class function Now: longint; ');
  17513. Add(' end;');
  17514. Add('function TObject.CurNow: longint; begin end;');
  17515. Add('class function TObject.Now: longint; begin end;');
  17516. Add('var');
  17517. Add(' Obj: tobject;');
  17518. Add(' vI: longint;');
  17519. Add('begin');
  17520. Add(' obj.curnow;');
  17521. Add(' vi:=obj.curnow;');
  17522. Add(' tobject.now;');
  17523. Add(' vi:=tobject.now;');
  17524. ConvertProgram;
  17525. CheckSource('TestClassOf_ImplicitFunctionCall',
  17526. LinesToStr([ // statements
  17527. 'rtl.createClass(this, "TObject", null, function () {',
  17528. ' this.$init = function () {',
  17529. ' };',
  17530. ' this.$final = function () {',
  17531. ' };',
  17532. ' this.CurNow = function () {',
  17533. ' var Result = 0;',
  17534. ' return Result;',
  17535. ' };',
  17536. ' this.Now = function () {',
  17537. ' var Result = 0;',
  17538. ' return Result;',
  17539. ' };',
  17540. '});',
  17541. 'this.Obj = null;',
  17542. 'this.vI = 0;',
  17543. '']),
  17544. LinesToStr([ // $mod.$main
  17545. '$mod.Obj.CurNow();',
  17546. '$mod.vI = $mod.Obj.CurNow();',
  17547. '$mod.TObject.Now();',
  17548. '$mod.vI = $mod.TObject.Now();',
  17549. '']));
  17550. end;
  17551. procedure TTestModule.TestClassOf_Const;
  17552. begin
  17553. StartProgram(false);
  17554. Add([
  17555. 'type',
  17556. ' TObject = class',
  17557. ' end;',
  17558. ' TBird = TObject;',
  17559. ' TBirds = class of TBird;',
  17560. ' TEagles = TBirds;',
  17561. ' THawk = class(TBird);',
  17562. 'const',
  17563. ' Hawk: TEagles = THawk;',
  17564. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17565. ' TBird,',
  17566. ' THawk',
  17567. ' );',
  17568. 'begin']);
  17569. ConvertProgram;
  17570. CheckSource('TestClassOf_Const',
  17571. LinesToStr([ // statements
  17572. 'rtl.createClass(this, "TObject", null, function () {',
  17573. ' this.$init = function () {',
  17574. ' };',
  17575. ' this.$final = function () {',
  17576. ' };',
  17577. '});',
  17578. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17579. '});',
  17580. 'this.Hawk = this.THawk;',
  17581. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17582. '']),
  17583. LinesToStr([ // $mod.$main
  17584. '']));
  17585. end;
  17586. procedure TTestModule.TestNestedClass_Alias;
  17587. begin
  17588. WithTypeInfo:=true;
  17589. StartProgram(false);
  17590. Add([
  17591. 'type',
  17592. ' TObject = class',
  17593. ' type TNested = type longint;',
  17594. ' end;',
  17595. 'type TAlias = type tobject.tnested;',
  17596. 'var i: tobject.tnested = 3;',
  17597. 'var j: TAlias = 4;',
  17598. 'begin',
  17599. ' if typeinfo(TAlias)=nil then ;',
  17600. ' if typeinfo(tobject.tnested)=nil then ;',
  17601. '']);
  17602. ConvertProgram;
  17603. CheckSource('TestNestedClass_Alias',
  17604. LinesToStr([ // statements
  17605. 'rtl.createClass(this, "TObject", null, function () {',
  17606. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17607. ' this.$init = function () {',
  17608. ' };',
  17609. ' this.$final = function () {',
  17610. ' };',
  17611. '});',
  17612. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17613. 'this.i = 3;',
  17614. 'this.j = 4;',
  17615. '']),
  17616. LinesToStr([ // $mod.$main
  17617. 'if ($mod.$rtti["TAlias"] === null) ;',
  17618. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17619. '']));
  17620. end;
  17621. procedure TTestModule.TestNestedClass_Record;
  17622. begin
  17623. WithTypeInfo:=true;
  17624. StartProgram(false);
  17625. Add([
  17626. 'type',
  17627. ' TObject = class',
  17628. ' type TPoint = record',
  17629. ' x,y: byte;',
  17630. ' end;',
  17631. ' procedure DoIt(t: TPoint);',
  17632. ' end;',
  17633. 'procedure tobject.DoIt(t: TPoint);',
  17634. 'var p: TPoint;',
  17635. 'begin',
  17636. ' t.x:=t.y;',
  17637. ' p:=t;',
  17638. 'end;',
  17639. 'var',
  17640. ' p: tobject.tpoint = (x:2; y:4);',
  17641. ' o: TObject;',
  17642. 'begin',
  17643. ' p:=p;',
  17644. ' o.doit(p);',
  17645. '']);
  17646. ConvertProgram;
  17647. CheckSource('TestNestedClass_Record',
  17648. LinesToStr([ // statements
  17649. 'rtl.createClass(this, "TObject", null, function () {',
  17650. ' rtl.recNewT(this, "TPoint", function () {',
  17651. ' this.x = 0;',
  17652. ' this.y = 0;',
  17653. ' this.$eq = function (b) {',
  17654. ' return (this.x === b.x) && (this.y === b.y);',
  17655. ' };',
  17656. ' this.$assign = function (s) {',
  17657. ' this.x = s.x;',
  17658. ' this.y = s.y;',
  17659. ' return this;',
  17660. ' };',
  17661. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  17662. ' $r.addField("x", rtl.byte);',
  17663. ' $r.addField("y", rtl.byte);',
  17664. ' });',
  17665. ' this.$init = function () {',
  17666. ' };',
  17667. ' this.$final = function () {',
  17668. ' };',
  17669. ' this.DoIt = function (t) {',
  17670. ' var p = this.TPoint.$new();',
  17671. ' t.x = t.y;',
  17672. ' p.$assign(t);',
  17673. ' };',
  17674. '});',
  17675. 'this.p = this.TObject.TPoint.$clone({',
  17676. ' x: 2,',
  17677. ' y: 4',
  17678. '});',
  17679. 'this.o = null;',
  17680. '']),
  17681. LinesToStr([ // $mod.$main
  17682. '$mod.p.$assign($mod.p);',
  17683. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  17684. '']));
  17685. end;
  17686. procedure TTestModule.TestNestedClass_Class;
  17687. begin
  17688. StartProgram(false);
  17689. Add([
  17690. 'type',
  17691. ' TObject = class end;',
  17692. ' TBird = class',
  17693. ' type TLeg = class',
  17694. ' FId: longint;',
  17695. ' constructor Create;',
  17696. ' function Create(i: longint): TLeg;',
  17697. ' end;',
  17698. ' function DoIt(b: TBird): Tleg;',
  17699. ' end;',
  17700. 'constructor tbird.tleg.create;',
  17701. 'begin',
  17702. ' FId:=3;',
  17703. 'end;',
  17704. 'function tbird.tleg.Create(i: longint): TLeg;',
  17705. 'begin',
  17706. ' Create;',
  17707. ' Result:=TLeg.Create;',
  17708. ' Result:=TBird.TLeg.Create;',
  17709. ' Result:=Create(3);',
  17710. ' FId:=i;',
  17711. 'end;',
  17712. 'function tbird.DoIt(b: tbird): tleg;',
  17713. 'begin',
  17714. ' Result.Create;',
  17715. ' Result:=TLeg.Create;',
  17716. ' Result:=TBird.TLeg.Create;',
  17717. ' Result:=Result.Create(3);',
  17718. 'end;',
  17719. 'var',
  17720. ' b: Tbird.tleg;',
  17721. 'begin',
  17722. ' b.Create;',
  17723. ' b:=TBird.TLeg.Create;',
  17724. ' b:=b.Create(3);',
  17725. '']);
  17726. ConvertProgram;
  17727. CheckSource('TestNestedClass_Class',
  17728. LinesToStr([ // statements
  17729. 'rtl.createClass(this, "TObject", null, function () {',
  17730. ' this.$init = function () {',
  17731. ' };',
  17732. ' this.$final = function () {',
  17733. ' };',
  17734. '});',
  17735. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17736. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  17737. ' this.$init = function () {',
  17738. ' $mod.TObject.$init.call(this);',
  17739. ' this.FId = 0;',
  17740. ' };',
  17741. ' this.Create = function () {',
  17742. ' this.FId = 3;',
  17743. ' return this;',
  17744. ' };',
  17745. ' this.Create$1 = function (i) {',
  17746. ' var Result = null;',
  17747. ' this.Create();',
  17748. ' Result = $mod.TBird.TLeg.$create("Create");',
  17749. ' Result = $mod.TBird.TLeg.$create("Create");',
  17750. ' Result = this.Create$1(3);',
  17751. ' this.FId = i;',
  17752. ' return Result;',
  17753. ' };',
  17754. ' }, "TBird.TLeg");',
  17755. ' this.DoIt = function (b) {',
  17756. ' var Result = null;',
  17757. ' Result.Create();',
  17758. ' Result = this.TLeg.$create("Create");',
  17759. ' Result = $mod.TBird.TLeg.$create("Create");',
  17760. ' Result = Result.Create$1(3);',
  17761. ' return Result;',
  17762. ' };',
  17763. '});',
  17764. 'this.b = null;',
  17765. '']),
  17766. LinesToStr([ // $mod.$main
  17767. '$mod.b.Create();',
  17768. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  17769. '$mod.b = $mod.b.Create$1(3);',
  17770. '']));
  17771. end;
  17772. procedure TTestModule.TestNestedClass_CallInherited;
  17773. begin
  17774. StartProgram(false);
  17775. Add([
  17776. 'type',
  17777. ' TObject = class end;',
  17778. ' TBird = class',
  17779. ' type',
  17780. ' TWing = class',
  17781. ' function Fly(w: word = 17): word; virtual;',
  17782. ' end;',
  17783. ' end;',
  17784. ' TEagle = class(TBird)',
  17785. ' type',
  17786. ' TEagleWing = class(TWing)',
  17787. ' function Fly(w: word): word; override;',
  17788. ' end;',
  17789. ' end;',
  17790. 'function TBird.TWing.Fly(w: word): word;',
  17791. 'begin',
  17792. 'end;',
  17793. 'function TEagle.TEagleWing.Fly(w: word): word;',
  17794. 'begin',
  17795. ' inherited;',
  17796. ' inherited Fly;',
  17797. ' inherited Fly(3);',
  17798. ' Result:=inherited Fly;',
  17799. ' Result:=inherited Fly(4);',
  17800. 'end;',
  17801. 'begin',
  17802. '']);
  17803. ConvertProgram;
  17804. CheckSource('TestNestedClass_CallInherited',
  17805. LinesToStr([ // statements
  17806. 'rtl.createClass(this, "TObject", null, function () {',
  17807. ' this.$init = function () {',
  17808. ' };',
  17809. ' this.$final = function () {',
  17810. ' };',
  17811. '});',
  17812. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17813. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  17814. ' this.Fly = function (w) {',
  17815. ' var Result = 0;',
  17816. ' return Result;',
  17817. ' };',
  17818. ' }, "TBird.TWing");',
  17819. '});',
  17820. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  17821. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  17822. ' this.Fly = function (w) {',
  17823. ' var Result = 0;',
  17824. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  17825. ' $mod.TBird.TWing.Fly.call(this, 17);',
  17826. ' $mod.TBird.TWing.Fly.call(this, 3);',
  17827. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  17828. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  17829. ' return Result;',
  17830. ' };',
  17831. ' }, "TEagle.TEagleWing");',
  17832. '});',
  17833. '']),
  17834. LinesToStr([ // $mod.$main
  17835. '']));
  17836. end;
  17837. procedure TTestModule.TestExternalClass_Var;
  17838. begin
  17839. StartProgram(false);
  17840. Add([
  17841. '{$modeswitch externalclass}',
  17842. 'type',
  17843. ' TExtA = class external name ''ExtObj''',
  17844. ' Id: longint external name ''$Id'';',
  17845. ' B: longint;',
  17846. ' end;',
  17847. 'var Obj: TExtA;',
  17848. 'begin',
  17849. ' obj.id:=obj.id+1;',
  17850. ' obj.B:=obj.B+1;']);
  17851. ConvertProgram;
  17852. CheckSource('TestExternalClass_Var',
  17853. LinesToStr([ // statements
  17854. 'this.Obj = null;',
  17855. '']),
  17856. LinesToStr([ // $mod.$main
  17857. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  17858. '$mod.Obj.B = $mod.Obj.B + 1;',
  17859. '']));
  17860. end;
  17861. procedure TTestModule.TestExternalClass_Const;
  17862. begin
  17863. StartProgram(false);
  17864. Add([
  17865. '{$modeswitch externalclass}',
  17866. 'type',
  17867. ' TExtA = class external name ''ExtObj''',
  17868. ' const Two: longint = 2;',
  17869. ' const Three = 3;',
  17870. ' const Id: longint;',
  17871. ' end;',
  17872. ' TExtB = class external name ''ExtB''',
  17873. ' A: TExtA;',
  17874. ' end;',
  17875. 'var',
  17876. ' A: texta;',
  17877. ' B: textb;',
  17878. ' i: longint;',
  17879. 'begin',
  17880. ' i:=a.two;',
  17881. ' i:=texta.two;',
  17882. ' i:=a.three;',
  17883. ' i:=texta.three;',
  17884. ' i:=a.id;',
  17885. ' i:=texta.id;',
  17886. '']);
  17887. ConvertProgram;
  17888. CheckSource('TestExternalClass_Const',
  17889. LinesToStr([ // statements
  17890. 'this.A = null;',
  17891. 'this.B = null;',
  17892. 'this.i = 0;',
  17893. '']),
  17894. LinesToStr([ // $mod.$main
  17895. '$mod.i = 2;',
  17896. '$mod.i = 2;',
  17897. '$mod.i = 3;',
  17898. '$mod.i = 3;',
  17899. '$mod.i = $mod.A.Id;',
  17900. '$mod.i = ExtObj.Id;',
  17901. '']));
  17902. end;
  17903. procedure TTestModule.TestExternalClass_Dollar;
  17904. begin
  17905. StartProgram(false);
  17906. Add([
  17907. '{$modeswitch externalclass}',
  17908. 'type',
  17909. ' TExtA = class external name ''$''',
  17910. ' Id: longint external name ''$'';',
  17911. ' function Bla(i: longint): longint; external name ''$'';',
  17912. ' end;',
  17913. 'function dollar(k: longint): longint; external name ''$'';',
  17914. 'var Obj: TExtA;',
  17915. 'begin',
  17916. ' dollar(1);',
  17917. ' obj.id:=obj.id+2;',
  17918. ' obj.Bla(3);',
  17919. '']);
  17920. ConvertProgram;
  17921. CheckSource('TestExternalClass_Dollar',
  17922. LinesToStr([ // statements
  17923. 'this.Obj = null;',
  17924. '']),
  17925. LinesToStr([ // $mod.$main
  17926. '$(1);',
  17927. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  17928. '$mod.Obj.$(3);',
  17929. '']));
  17930. end;
  17931. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  17932. begin
  17933. StartProgram(false);
  17934. Add('{$modeswitch externalclass}');
  17935. Add('type');
  17936. Add(' TExtA = class external name ''ExtA''');
  17937. Add(' Id: longint external name ''$Id'';');
  17938. Add(' end;');
  17939. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  17940. Add(' Id: longint;');
  17941. Add(' end;');
  17942. Add('begin');
  17943. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  17944. ConvertProgram;
  17945. end;
  17946. procedure TTestModule.TestExternalClass_Method;
  17947. begin
  17948. StartProgram(false);
  17949. Add(['{$modeswitch externalclass}',
  17950. 'type',
  17951. ' TExtA = class external name ''ExtObj''',
  17952. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17953. ' procedure DoSome(Id: longint = 1);',
  17954. ' end;',
  17955. 'var Obj: texta;',
  17956. 'begin',
  17957. ' obj.doit;',
  17958. ' obj.doit();',
  17959. ' obj.doit(2);',
  17960. ' with obj do begin',
  17961. ' doit;',
  17962. ' doit();',
  17963. ' doit(3);',
  17964. ' end;']);
  17965. ConvertProgram;
  17966. CheckSource('TestExternalClass_Method',
  17967. LinesToStr([ // statements
  17968. 'this.Obj = null;',
  17969. '']),
  17970. LinesToStr([ // $mod.$main
  17971. '$mod.Obj.$Execute(1);',
  17972. '$mod.Obj.$Execute(1);',
  17973. '$mod.Obj.$Execute(2);',
  17974. 'var $with = $mod.Obj;',
  17975. '$with.$Execute(1);',
  17976. '$with.$Execute(1);',
  17977. '$with.$Execute(3);',
  17978. '']));
  17979. end;
  17980. procedure TTestModule.TestExternalClass_ClassMethod;
  17981. begin
  17982. StartProgram(false);
  17983. Add([
  17984. '{$modeswitch externalclass}',
  17985. 'type',
  17986. ' TExtA = class external name ''ExtObj''',
  17987. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17988. ' end;',
  17989. ' TExtB = TExtA;',
  17990. 'var p: Pointer;',
  17991. 'begin',
  17992. ' texta.doit;',
  17993. ' texta.doit();',
  17994. ' texta.doit(2);',
  17995. ' p:[email protected];',
  17996. ' with texta do begin',
  17997. ' doit;',
  17998. ' doit();',
  17999. ' doit(3);',
  18000. ' p:=@DoIt;',
  18001. ' end;',
  18002. ' textb.doit;',
  18003. ' textb.doit();',
  18004. ' textb.doit(4);',
  18005. ' with textb do begin',
  18006. ' doit;',
  18007. ' doit();',
  18008. ' doit(5);',
  18009. ' end;',
  18010. '']);
  18011. ConvertProgram;
  18012. CheckSource('TestExternalClass_ClassMethod',
  18013. LinesToStr([ // statements
  18014. 'this.p = null;',
  18015. '']),
  18016. LinesToStr([ // $mod.$main
  18017. 'ExtObj.$Execute(1);',
  18018. 'ExtObj.$Execute(1);',
  18019. 'ExtObj.$Execute(2);',
  18020. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18021. 'ExtObj.$Execute(1);',
  18022. 'ExtObj.$Execute(1);',
  18023. 'ExtObj.$Execute(3);',
  18024. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18025. 'ExtObj.$Execute(1);',
  18026. 'ExtObj.$Execute(1);',
  18027. 'ExtObj.$Execute(4);',
  18028. 'ExtObj.$Execute(1);',
  18029. 'ExtObj.$Execute(1);',
  18030. 'ExtObj.$Execute(5);',
  18031. '']));
  18032. end;
  18033. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18034. begin
  18035. StartProgram(false);
  18036. Add([
  18037. '{$modeswitch externalclass}',
  18038. 'type',
  18039. ' TExtA = class external name ''ExtObj''',
  18040. ' class procedure DoIt(Id: longint = 1); static;',
  18041. ' end;',
  18042. 'var p: Pointer;',
  18043. 'begin',
  18044. ' texta.doit;',
  18045. ' texta.doit();',
  18046. ' texta.doit(2);',
  18047. ' p:[email protected];',
  18048. ' with texta do begin',
  18049. ' doit;',
  18050. ' doit();',
  18051. ' doit(3);',
  18052. ' p:=@DoIt;',
  18053. ' end;',
  18054. '']);
  18055. ConvertProgram;
  18056. CheckSource('TestExternalClass_ClassMethodStatic',
  18057. LinesToStr([ // statements
  18058. 'this.p = null;',
  18059. '']),
  18060. LinesToStr([ // $mod.$main
  18061. 'ExtObj.DoIt(1);',
  18062. 'ExtObj.DoIt(1);',
  18063. 'ExtObj.DoIt(2);',
  18064. '$mod.p = ExtObj.DoIt;',
  18065. 'ExtObj.DoIt(1);',
  18066. 'ExtObj.DoIt(1);',
  18067. 'ExtObj.DoIt(3);',
  18068. '$mod.p = ExtObj.DoIt;',
  18069. '']));
  18070. end;
  18071. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18072. begin
  18073. StartProgram(false);
  18074. Add([
  18075. '{$modeswitch externalclass}',
  18076. 'type',
  18077. ' TBird = class external name ''Array''',
  18078. ' end;',
  18079. 'function GetPtr: Pointer;',
  18080. 'begin',
  18081. 'end;',
  18082. 'procedure Write(const p);',
  18083. 'begin',
  18084. 'end;',
  18085. 'procedure WriteLn; varargs;',
  18086. 'begin',
  18087. 'end;',
  18088. 'begin',
  18089. ' if TBird(GetPtr)=nil then ;',
  18090. ' Write(GetPtr);',
  18091. ' WriteLn(GetPtr);',
  18092. ' Write(TBird(GetPtr));',
  18093. ' WriteLn(TBird(GetPtr));',
  18094. '']);
  18095. ConvertProgram;
  18096. CheckSource('TestFunctionResultInTypeCast',
  18097. LinesToStr([ // statements
  18098. 'this.GetPtr = function () {',
  18099. ' var Result = null;',
  18100. ' return Result;',
  18101. '};',
  18102. 'this.Write = function (p) {',
  18103. '};',
  18104. 'this.WriteLn = function () {',
  18105. '};',
  18106. '']),
  18107. LinesToStr([
  18108. 'if ($mod.GetPtr() === null) ;',
  18109. '$mod.Write($mod.GetPtr());',
  18110. '$mod.WriteLn($mod.GetPtr());',
  18111. '$mod.Write($mod.GetPtr());',
  18112. '$mod.WriteLn($mod.GetPtr());',
  18113. '']));
  18114. end;
  18115. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18116. begin
  18117. StartProgram(false);
  18118. Add([
  18119. '{$modeswitch externalclass}',
  18120. 'type',
  18121. ' TExtA = class external name ''ExtObjA''',
  18122. ' procedure ProcA; virtual;',
  18123. ' procedure ProcB; virtual;',
  18124. ' end;',
  18125. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18126. ' end;',
  18127. ' TExtC = class (TExtB)',
  18128. ' procedure ProcA; override;',
  18129. ' end;',
  18130. 'procedure TExtC.ProcA;',
  18131. 'begin',
  18132. ' ProcA;',
  18133. ' Self.ProcA;',
  18134. ' ProcB;',
  18135. ' Self.ProcB;',
  18136. 'end;',
  18137. 'var',
  18138. ' A: texta;',
  18139. ' B: textb;',
  18140. ' C: textc;',
  18141. 'begin',
  18142. ' a.proca;',
  18143. ' b.proca;',
  18144. ' c.proca;']);
  18145. ConvertProgram;
  18146. CheckSource('TestExternalClass_NonExternalOverride',
  18147. LinesToStr([ // statements
  18148. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18149. ' this.$init = function () {',
  18150. ' };',
  18151. ' this.$final = function () {',
  18152. ' };',
  18153. ' this.ProcA = function () {',
  18154. ' this.ProcA();',
  18155. ' this.ProcA();',
  18156. ' this.ProcB();',
  18157. ' this.ProcB();',
  18158. ' };',
  18159. '});',
  18160. 'this.A = null;',
  18161. 'this.B = null;',
  18162. 'this.C = null;',
  18163. '']),
  18164. LinesToStr([ // $mod.$main
  18165. '$mod.A.ProcA();',
  18166. '$mod.B.ProcA();',
  18167. '$mod.C.ProcA();',
  18168. '']));
  18169. end;
  18170. procedure TTestModule.TestExternalClass_OverloadHint;
  18171. begin
  18172. StartProgram(false);
  18173. Add([
  18174. '{$modeswitch externalclass}',
  18175. 'type',
  18176. ' TExtA = class external name ''ExtObjA''',
  18177. ' procedure DoIt;',
  18178. ' procedure DoIt(i: longint);',
  18179. ' end;',
  18180. 'begin',
  18181. '']);
  18182. ConvertProgram;
  18183. CheckResolverUnexpectedHints(true);
  18184. CheckSource('TestExternalClass_OverloadHint',
  18185. LinesToStr([ // statements
  18186. '']),
  18187. LinesToStr([ // $mod.$main
  18188. '']));
  18189. end;
  18190. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18191. begin
  18192. WithTypeInfo:=true;
  18193. StartProgram(false);
  18194. Add([
  18195. '{$modeswitch externalclass}',
  18196. 'type',
  18197. ' JSwiper = class external name ''Swiper''',
  18198. ' constructor New;',
  18199. ' end;',
  18200. ' TObject = class',
  18201. ' private',
  18202. ' FSwiper: JSwiper;',
  18203. ' published',
  18204. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18205. ' end;',
  18206. 'begin',
  18207. ' JSwiper.new;',
  18208. '']);
  18209. ConvertProgram;
  18210. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18211. LinesToStr([ // statements
  18212. 'this.$rtti.$ExtClass("JSwiper", {',
  18213. ' jsclass: "Swiper"',
  18214. '});',
  18215. 'rtl.createClass(this, "TObject", null, function () {',
  18216. ' this.$init = function () {',
  18217. ' this.FSwiper = null;',
  18218. ' };',
  18219. ' this.$final = function () {',
  18220. ' this.FSwiper = undefined;',
  18221. ' };',
  18222. ' var $r = this.$rtti;',
  18223. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18224. '});',
  18225. '']),
  18226. LinesToStr([ // $mod.$main
  18227. 'new Swiper();',
  18228. '']));
  18229. end;
  18230. procedure TTestModule.TestExternalClass_Property;
  18231. begin
  18232. StartProgram(false);
  18233. Add([
  18234. '{$modeswitch externalclass}',
  18235. 'type',
  18236. ' TExtA = class external name ''ExtA''',
  18237. ' function getYear: longint;',
  18238. ' procedure setYear(Value: longint);',
  18239. ' property Year: longint read getyear write setyear;',
  18240. ' end;',
  18241. ' TExtB = class (TExtA)',
  18242. ' procedure OtherSetYear(Value: longint);',
  18243. ' property year write othersetyear;',
  18244. ' end;',
  18245. 'procedure textb.othersetyear(value: longint);',
  18246. 'begin',
  18247. ' setYear(Value+4);',
  18248. 'end;',
  18249. 'var',
  18250. ' A: texta;',
  18251. ' B: textb;',
  18252. 'begin',
  18253. ' a.year:=a.year+1;',
  18254. ' b.year:=b.year+2;']);
  18255. ConvertProgram;
  18256. CheckSource('TestExternalClass_NonExternalOverride',
  18257. LinesToStr([ // statements
  18258. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18259. ' this.$init = function () {',
  18260. ' };',
  18261. ' this.$final = function () {',
  18262. ' };',
  18263. ' this.OtherSetYear = function (Value) {',
  18264. ' this.setYear(Value+4);',
  18265. ' };',
  18266. '});',
  18267. 'this.A = null;',
  18268. 'this.B = null;',
  18269. '']),
  18270. LinesToStr([ // $mod.$main
  18271. '$mod.A.setYear($mod.A.getYear()+1);',
  18272. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18273. '']));
  18274. end;
  18275. procedure TTestModule.TestExternalClass_PropertyDate;
  18276. begin
  18277. StartProgram(false);
  18278. Add([
  18279. '{$modeswitch externalclass}',
  18280. 'type',
  18281. ' TExtA = class external name ''ExtA''',
  18282. ' end;',
  18283. ' TExtB = class (TExtA)',
  18284. ' FDate: string;',
  18285. ' property Date: string read FDate write FDate;',
  18286. ' property ExtA: string read FDate write FDate;',
  18287. ' end;',
  18288. ' {$M+}',
  18289. ' TObject = class',
  18290. ' FDate: string;',
  18291. ' published',
  18292. ' property Date: string read FDate write FDate;',
  18293. ' property ExtA: string read FDate write FDate;',
  18294. ' end;',
  18295. 'var',
  18296. ' B: textb;',
  18297. ' o: TObject;',
  18298. 'begin',
  18299. ' b.date:=b.exta;',
  18300. ' o.date:=o.exta;']);
  18301. ConvertProgram;
  18302. CheckSource('TestExternalClass_PropertyDate',
  18303. LinesToStr([ // statements
  18304. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18305. ' this.$init = function () {',
  18306. ' this.FDate = "";',
  18307. ' };',
  18308. ' this.$final = function () {',
  18309. ' };',
  18310. '});',
  18311. 'rtl.createClass(this, "TObject", null, function () {',
  18312. ' this.$init = function () {',
  18313. ' this.FDate = "";',
  18314. ' };',
  18315. ' this.$final = function () {',
  18316. ' };',
  18317. ' var $r = this.$rtti;',
  18318. ' $r.addField("FDate", rtl.string);',
  18319. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18320. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18321. '});',
  18322. 'this.B = null;',
  18323. 'this.o = null;',
  18324. '']),
  18325. LinesToStr([ // $mod.$main
  18326. '$mod.B.FDate = $mod.B.FDate;',
  18327. '$mod.o.FDate = $mod.o.FDate;',
  18328. '']));
  18329. end;
  18330. procedure TTestModule.TestExternalClass_ClassProperty;
  18331. begin
  18332. StartProgram(false);
  18333. Add('{$modeswitch externalclass}');
  18334. Add('type');
  18335. Add(' TExtA = class external name ''ExtA''');
  18336. Add(' class function getYear: longint;');
  18337. Add(' class procedure setYear(Value: longint);');
  18338. Add(' class property Year: longint read getyear write setyear;');
  18339. Add(' end;');
  18340. Add(' TExtB = class (TExtA)');
  18341. Add(' class function GetCentury: longint;');
  18342. Add(' class procedure SetCentury(Value: longint);');
  18343. Add(' class property Century: longint read getcentury write setcentury;');
  18344. Add(' end;');
  18345. Add('class function textb.getcentury: longint;');
  18346. Add('begin');
  18347. Add('end;');
  18348. Add('class procedure textb.setcentury(value: longint);');
  18349. Add('begin');
  18350. Add(' setyear(value+11);');
  18351. Add(' texta.year:=texta.year+12;');
  18352. Add(' year:=year+13;');
  18353. Add(' textb.century:=textb.century+14;');
  18354. Add(' century:=century+15;');
  18355. Add('end;');
  18356. Add('var');
  18357. Add(' A: texta;');
  18358. Add(' B: textb;');
  18359. Add('begin');
  18360. Add(' texta.year:=texta.year+1;');
  18361. Add(' textb.year:=textb.year+2;');
  18362. Add(' TextA.year:=TextA.year+3;');
  18363. Add(' b.year:=b.year+4;');
  18364. Add(' textb.century:=textb.century+5;');
  18365. Add(' b.century:=b.century+6;');
  18366. ConvertProgram;
  18367. CheckSource('TestExternalClass_ClassProperty',
  18368. LinesToStr([ // statements
  18369. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18370. ' this.$init = function () {',
  18371. ' };',
  18372. ' this.$final = function () {',
  18373. ' };',
  18374. ' this.GetCentury = function () {',
  18375. ' var Result = 0;',
  18376. ' return Result;',
  18377. ' };',
  18378. ' this.SetCentury = function (Value) {',
  18379. ' this.setYear(Value + 11);',
  18380. ' ExtA.setYear(ExtA.getYear() + 12);',
  18381. ' this.setYear(this.getYear() + 13);',
  18382. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18383. ' this.SetCentury(this.GetCentury() + 15);',
  18384. ' };',
  18385. '});',
  18386. 'this.A = null;',
  18387. 'this.B = null;',
  18388. '']),
  18389. LinesToStr([ // $mod.$main
  18390. 'ExtA.setYear(ExtA.getYear() + 1);',
  18391. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18392. 'ExtA.setYear(ExtA.getYear() + 3);',
  18393. '$mod.B.setYear($mod.B.getYear() + 4);',
  18394. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18395. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18396. '']));
  18397. end;
  18398. procedure TTestModule.TestExternalClass_ClassOf;
  18399. begin
  18400. StartProgram(false);
  18401. Add('{$modeswitch externalclass}');
  18402. Add('type');
  18403. Add(' TExtA = class external name ''ExtA''');
  18404. Add(' procedure ProcA; virtual;');
  18405. Add(' procedure ProcB; virtual;');
  18406. Add(' end;');
  18407. Add(' TExtAClass = class of TExtA;');
  18408. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18409. Add(' end;');
  18410. Add(' TExtBClass = class of TExtB;');
  18411. Add(' TExtC = class (TExtB)');
  18412. Add(' procedure ProcA; override;');
  18413. Add(' end;');
  18414. Add(' TExtCClass = class of TExtC;');
  18415. Add('procedure TExtC.ProcA; begin end;');
  18416. Add('var');
  18417. Add(' A: texta; ClA: TExtAClass;');
  18418. Add(' B: textb; ClB: TExtBClass;');
  18419. Add(' C: textc; ClC: TExtCClass;');
  18420. Add('begin');
  18421. Add(' ClA:=texta;');
  18422. Add(' ClA:=textb;');
  18423. Add(' ClA:=textc;');
  18424. Add(' ClB:=textb;');
  18425. Add(' ClB:=textc;');
  18426. Add(' ClC:=textc;');
  18427. ConvertProgram;
  18428. CheckSource('TestExternalClass_ClassOf',
  18429. LinesToStr([ // statements
  18430. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18431. ' this.$init = function () {',
  18432. ' };',
  18433. ' this.$final = function () {',
  18434. ' };',
  18435. ' this.ProcA = function () {',
  18436. ' };',
  18437. '});',
  18438. 'this.A = null;',
  18439. 'this.ClA = null;',
  18440. 'this.B = null;',
  18441. 'this.ClB = null;',
  18442. 'this.C = null;',
  18443. 'this.ClC = null;',
  18444. '']),
  18445. LinesToStr([ // $mod.$main
  18446. '$mod.ClA = ExtA;',
  18447. '$mod.ClA = ExtB;',
  18448. '$mod.ClA = $mod.TExtC;',
  18449. '$mod.ClB = ExtB;',
  18450. '$mod.ClB = $mod.TExtC;',
  18451. '$mod.ClC = $mod.TExtC;',
  18452. '']));
  18453. end;
  18454. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18455. begin
  18456. AddModuleWithIntfImplSrc('unit2.pas',
  18457. LinesToStr([
  18458. '{$modeswitch externalclass}',
  18459. 'type',
  18460. ' TExtA = class external name ''ExtA''',
  18461. ' class var Id: longint;',
  18462. ' end;',
  18463. '']),
  18464. '');
  18465. StartUnit(true);
  18466. Add('interface');
  18467. Add('uses unit2;');
  18468. Add('implementation');
  18469. Add('begin');
  18470. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18471. ConvertUnit;
  18472. CheckSource('TestExternalClass_ClassOtherUnit',
  18473. LinesToStr([
  18474. '']),
  18475. LinesToStr([
  18476. 'ExtA.Id = ExtA.Id + 1;',
  18477. '']));
  18478. end;
  18479. procedure TTestModule.TestExternalClass_Is;
  18480. begin
  18481. StartProgram(false);
  18482. Add([
  18483. '{$modeswitch externalclass}',
  18484. 'type',
  18485. ' TExtA = class external name ''ExtA''',
  18486. ' end;',
  18487. ' TExtAClass = class of TExtA;',
  18488. ' TExtB = class external name ''ExtB'' (TExtA)',
  18489. ' end;',
  18490. ' TExtBClass = class of TExtB;',
  18491. ' TExtC = class (TExtB)',
  18492. ' end;',
  18493. ' TExtCClass = class of TExtC;',
  18494. 'var',
  18495. ' A: texta; ClA: TExtAClass;',
  18496. ' B: textb; ClB: TExtBClass;',
  18497. ' C: textc; ClC: TExtCClass;',
  18498. 'begin',
  18499. ' if a is textb then ;',
  18500. ' if a is textc then ;',
  18501. ' if b is textc then ;',
  18502. ' if cla is textb then ;',
  18503. ' if cla is textc then ;',
  18504. ' if clb is textc then ;',
  18505. ' try',
  18506. ' except',
  18507. ' on TExtA do ;',
  18508. ' on e: TExtB do ;',
  18509. ' end;',
  18510. '']);
  18511. ConvertProgram;
  18512. CheckSource('TestExternalClass_Is',
  18513. LinesToStr([ // statements
  18514. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18515. ' this.$init = function () {',
  18516. ' };',
  18517. ' this.$final = function () {',
  18518. ' };',
  18519. '});',
  18520. 'this.A = null;',
  18521. 'this.ClA = null;',
  18522. 'this.B = null;',
  18523. 'this.ClB = null;',
  18524. 'this.C = null;',
  18525. 'this.ClC = null;',
  18526. '']),
  18527. LinesToStr([ // $mod.$main
  18528. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18529. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18530. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18531. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18532. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18533. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18534. 'try {} catch ($e) {',
  18535. ' if (rtl.isExt($e,ExtA)) {}',
  18536. ' else if (rtl.isExt($e,ExtB)) {',
  18537. ' var e = $e;',
  18538. ' } else throw $e',
  18539. '};',
  18540. '']));
  18541. end;
  18542. procedure TTestModule.TestExternalClass_As;
  18543. begin
  18544. StartProgram(false);
  18545. Add('{$modeswitch externalclass}');
  18546. Add('type');
  18547. Add(' TExtA = class external name ''ExtA''');
  18548. Add(' end;');
  18549. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18550. Add(' end;');
  18551. Add(' TExtC = class (TExtB)');
  18552. Add(' end;');
  18553. Add('var');
  18554. Add(' A: texta;');
  18555. Add(' B: textb;');
  18556. Add(' C: textc;');
  18557. Add('begin');
  18558. Add(' b:=a as textb;');
  18559. Add(' c:=a as textc;');
  18560. Add(' c:=b as textc;');
  18561. ConvertProgram;
  18562. CheckSource('TestExternalClass_Is',
  18563. LinesToStr([ // statements
  18564. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18565. ' this.$init = function () {',
  18566. ' };',
  18567. ' this.$final = function () {',
  18568. ' };',
  18569. '});',
  18570. 'this.A = null;',
  18571. 'this.B = null;',
  18572. 'this.C = null;',
  18573. '']),
  18574. LinesToStr([ // $mod.$main
  18575. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18576. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18577. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18578. '']));
  18579. end;
  18580. procedure TTestModule.TestExternalClass_DestructorFail;
  18581. begin
  18582. StartProgram(false);
  18583. Add('{$modeswitch externalclass}');
  18584. Add('type');
  18585. Add(' TExtA = class external name ''ExtA''');
  18586. Add(' destructor Free;');
  18587. Add(' end;');
  18588. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18589. nPasElementNotSupported);
  18590. ConvertProgram;
  18591. end;
  18592. procedure TTestModule.TestExternalClass_New;
  18593. begin
  18594. StartProgram(false);
  18595. Add([
  18596. '{$modeswitch externalclass}',
  18597. 'type',
  18598. ' TExtA = class external name ''ExtA''',
  18599. ' constructor New;',
  18600. ' constructor New(i: longint; j: longint = 2);',
  18601. ' end;',
  18602. 'var',
  18603. ' A: texta;',
  18604. 'begin',
  18605. ' a:=texta.new;',
  18606. ' a:=texta(texta.new);',
  18607. ' a:=texta.new();',
  18608. ' a:=texta.new(1);',
  18609. ' with texta do begin',
  18610. ' a:=new;',
  18611. ' a:=new();',
  18612. ' a:=new(2);',
  18613. ' end;',
  18614. ' a:=test1.texta.new;',
  18615. ' a:=test1.texta.new();',
  18616. ' a:=test1.texta.new(3);',
  18617. '']);
  18618. ConvertProgram;
  18619. CheckSource('TestExternalClass_New',
  18620. LinesToStr([ // statements
  18621. 'this.A = null;',
  18622. '']),
  18623. LinesToStr([ // $mod.$main
  18624. '$mod.A = new ExtA();',
  18625. '$mod.A = new ExtA();',
  18626. '$mod.A = new ExtA();',
  18627. '$mod.A = new ExtA(1,2);',
  18628. '$mod.A = new ExtA();',
  18629. '$mod.A = new ExtA();',
  18630. '$mod.A = new ExtA(2,2);',
  18631. '$mod.A = new ExtA();',
  18632. '$mod.A = new ExtA();',
  18633. '$mod.A = new ExtA(3,2);',
  18634. '']));
  18635. end;
  18636. procedure TTestModule.TestExternalClass_ClassOf_New;
  18637. begin
  18638. StartProgram(false);
  18639. Add('{$modeswitch externalclass}');
  18640. Add('type');
  18641. Add(' TExtAClass = class of TExtA;');
  18642. Add(' TExtA = class external name ''ExtA''');
  18643. Add(' C: TExtAClass;');
  18644. Add(' constructor New;');
  18645. Add(' end;');
  18646. Add('var');
  18647. Add(' A: texta;');
  18648. Add(' C: textaclass;');
  18649. Add('begin');
  18650. Add(' a:=c.new;');
  18651. Add(' a:=c.new();');
  18652. Add(' with C do begin');
  18653. Add(' a:=new;');
  18654. Add(' a:=new();');
  18655. Add(' end;');
  18656. Add(' a:=test1.c.new;');
  18657. Add(' a:=test1.c.new();');
  18658. Add(' a:=A.c.new();');
  18659. ConvertProgram;
  18660. CheckSource('TestExternalClass_ClassOf_New',
  18661. LinesToStr([ // statements
  18662. 'this.A = null;',
  18663. 'this.C = null;',
  18664. '']),
  18665. LinesToStr([ // $mod.$main
  18666. '$mod.A = new $mod.C();',
  18667. '$mod.A = new $mod.C();',
  18668. 'var $with = $mod.C;',
  18669. '$mod.A = new $with();',
  18670. '$mod.A = new $with();',
  18671. '$mod.A = new $mod.C();',
  18672. '$mod.A = new $mod.C();',
  18673. '$mod.A = new $mod.A.C();',
  18674. '']));
  18675. end;
  18676. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  18677. begin
  18678. StartProgram(false);
  18679. Add([
  18680. '{$modeswitch externalclass}',
  18681. 'type',
  18682. ' TExtAClass = class of TExtA;',
  18683. ' TExtA = class external name ''ExtA''',
  18684. ' constructor New;',
  18685. ' end;',
  18686. 'function GetCreator: TExtAClass;',
  18687. 'begin',
  18688. ' Result:=TExtA;',
  18689. 'end;',
  18690. 'var',
  18691. ' A: texta;',
  18692. 'begin',
  18693. ' a:=getcreator.new;',
  18694. ' a:=getcreator().new;',
  18695. ' a:=getcreator().new();',
  18696. ' a:=getcreator.new();',
  18697. ' with getcreator do begin',
  18698. ' a:=new;',
  18699. ' a:=new();',
  18700. ' end;']);
  18701. ConvertProgram;
  18702. CheckSource('TestExternalClass_FuncClassOf_New',
  18703. LinesToStr([ // statements
  18704. 'this.GetCreator = function () {',
  18705. ' var Result = null;',
  18706. ' Result = ExtA;',
  18707. ' return Result;',
  18708. '};',
  18709. 'this.A = null;',
  18710. '']),
  18711. LinesToStr([ // $mod.$main
  18712. '$mod.A = new ($mod.GetCreator())();',
  18713. '$mod.A = new ($mod.GetCreator())();',
  18714. '$mod.A = new ($mod.GetCreator())();',
  18715. '$mod.A = new ($mod.GetCreator())();',
  18716. 'var $with = $mod.GetCreator();',
  18717. '$mod.A = new $with();',
  18718. '$mod.A = new $with();',
  18719. '']));
  18720. end;
  18721. procedure TTestModule.TestExternalClass_New_PasClassFail;
  18722. begin
  18723. StartProgram(false);
  18724. Add([
  18725. '{$modeswitch externalclass}',
  18726. 'type',
  18727. ' TExtA = class external name ''ExtA''',
  18728. ' constructor New;',
  18729. ' end;',
  18730. ' TBird = class(TExtA)',
  18731. ' end;',
  18732. 'begin',
  18733. ' TBird.new;',
  18734. '']);
  18735. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18736. ConvertProgram;
  18737. end;
  18738. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  18739. begin
  18740. StartProgram(false);
  18741. Add([
  18742. '{$modeswitch externalclass}',
  18743. 'type',
  18744. ' TExtA = class external name ''ExtA''',
  18745. ' constructor New;',
  18746. ' end;',
  18747. ' TBird = class(TExtA)',
  18748. ' end;',
  18749. 'begin',
  18750. ' TBird.new();',
  18751. '']);
  18752. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  18753. ConvertProgram;
  18754. end;
  18755. procedure TTestModule.TestExternalClass_NewExtName;
  18756. begin
  18757. StartProgram(false);
  18758. Add([
  18759. '{$modeswitch externalclass}',
  18760. 'type',
  18761. ' TExtA = class external name ''ExtA''',
  18762. ' constructor New; external name ''Other'';',
  18763. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  18764. ' end;',
  18765. 'var',
  18766. ' A: texta;',
  18767. 'begin',
  18768. ' a:=texta.new;',
  18769. ' a:=texta(texta.new);',
  18770. ' a:=texta.new();',
  18771. ' a:=texta.new(1);',
  18772. ' with texta do begin',
  18773. ' a:=new;',
  18774. ' a:=new();',
  18775. ' a:=new(2);',
  18776. ' end;',
  18777. ' a:=test1.texta.new;',
  18778. ' a:=test1.texta.new();',
  18779. ' a:=test1.texta.new(3);',
  18780. '']);
  18781. ConvertProgram;
  18782. CheckSource('TestExternalClass_NewExtName',
  18783. LinesToStr([ // statements
  18784. 'this.A = null;',
  18785. '']),
  18786. LinesToStr([ // $mod.$main
  18787. '$mod.A = new Other();',
  18788. '$mod.A = new Other();',
  18789. '$mod.A = new Other();',
  18790. '$mod.A = new A.B(1,2);',
  18791. '$mod.A = new Other();',
  18792. '$mod.A = new Other();',
  18793. '$mod.A = new A.B(2,2);',
  18794. '$mod.A = new Other();',
  18795. '$mod.A = new Other();',
  18796. '$mod.A = new A.B(3,2);',
  18797. '']));
  18798. end;
  18799. procedure TTestModule.TestExternalClass_Constructor;
  18800. begin
  18801. StartProgram(false);
  18802. Add([
  18803. '{$modeswitch externalclass}',
  18804. 'type',
  18805. ' TExtA = class external name ''ExtA''',
  18806. ' public type',
  18807. ' TExtB = class external name ''ExtB''',
  18808. ' public type',
  18809. ' TExtC = class external name ''ExtC''',
  18810. ' constructor New;',
  18811. ' constructor New(i: word);',
  18812. ' end;',
  18813. ' end;',
  18814. ' constructor Create;',
  18815. ' constructor Create(i: longint; j: longint = 2);',
  18816. ' end;',
  18817. 'var',
  18818. ' A: texta;',
  18819. ' C: texta.textb.textc;',
  18820. 'begin',
  18821. ' a:=texta.create;',
  18822. ' a:=texta(texta.create);',
  18823. ' a:=texta.create();',
  18824. ' a:=texta.create(1);',
  18825. ' with texta do begin',
  18826. ' a:=create;',
  18827. ' a:=create();',
  18828. ' a:=create(2);',
  18829. ' end;',
  18830. ' a:=test1.texta.create;',
  18831. ' a:=test1.texta.create();',
  18832. ' a:=test1.texta.create(3);',
  18833. ' c:=texta.textb.textc.new;',
  18834. ' c:=texta.textb.textc.new();',
  18835. ' c:=texta.textb.textc.new(4);',
  18836. '']);
  18837. ConvertProgram;
  18838. CheckSource('TestExternalClass_Constructor',
  18839. LinesToStr([ // statements
  18840. 'this.A = null;',
  18841. 'this.C = null;',
  18842. '']),
  18843. LinesToStr([ // $mod.$main
  18844. '$mod.A = new ExtA.Create();',
  18845. '$mod.A = new ExtA.Create();',
  18846. '$mod.A = new ExtA.Create();',
  18847. '$mod.A = new ExtA.Create(1,2);',
  18848. '$mod.A = new ExtA.Create();',
  18849. '$mod.A = new ExtA.Create();',
  18850. '$mod.A = new ExtA.Create(2,2);',
  18851. '$mod.A = new ExtA.Create();',
  18852. '$mod.A = new ExtA.Create();',
  18853. '$mod.A = new ExtA.Create(3,2);',
  18854. '$mod.C = new ExtA.ExtB.ExtC();',
  18855. '$mod.C = new ExtA.ExtB.ExtC();',
  18856. '$mod.C = new ExtA.ExtB.ExtC(4);',
  18857. '']));
  18858. end;
  18859. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  18860. begin
  18861. StartProgram(false);
  18862. Add([
  18863. '{$modeswitch externalclass}',
  18864. 'type',
  18865. ' TExtA = class external name ''ExtA''',
  18866. ' constructor Create; external name ''{}'';',
  18867. ' end;',
  18868. 'var',
  18869. ' A: texta;',
  18870. 'begin',
  18871. ' a:=texta.create;',
  18872. ' a:=texta(texta.create);',
  18873. ' a:=texta.create();',
  18874. ' with texta do begin',
  18875. ' a:=create;',
  18876. ' a:=create();',
  18877. ' end;',
  18878. ' a:=test1.texta.create;',
  18879. ' a:=test1.texta.create();',
  18880. '']);
  18881. ConvertProgram;
  18882. CheckSource('TestExternalClass_ConstructorBrackets',
  18883. LinesToStr([ // statements
  18884. 'this.A = null;',
  18885. '']),
  18886. LinesToStr([ // $mod.$main
  18887. '$mod.A = {};',
  18888. '$mod.A = {};',
  18889. '$mod.A = {};',
  18890. '$mod.A = {};',
  18891. '$mod.A = {};',
  18892. '$mod.A = {};',
  18893. '$mod.A = {};',
  18894. '']));
  18895. end;
  18896. procedure TTestModule.TestExternalClass_LocalConstSameName;
  18897. begin
  18898. StartProgram(false);
  18899. Add('{$modeswitch externalclass}');
  18900. Add('type');
  18901. Add(' TExtA = class external name ''ExtA''');
  18902. Add(' constructor New;');
  18903. Add(' end;');
  18904. Add('function DoIt: longint;');
  18905. Add('const ExtA: longint = 3;');
  18906. Add('begin');
  18907. Add(' Result:=ExtA;');
  18908. Add('end;');
  18909. Add('var');
  18910. Add(' A: texta;');
  18911. Add('begin');
  18912. Add(' a:=texta.new;');
  18913. ConvertProgram;
  18914. CheckSource('TestExternalClass_LocalConstSameName',
  18915. LinesToStr([ // statements
  18916. 'var ExtA$1 = 3;',
  18917. 'this.DoIt = function () {',
  18918. ' var Result = 0;',
  18919. ' Result = ExtA$1;',
  18920. ' return Result;',
  18921. '};',
  18922. 'this.A = null;',
  18923. '']),
  18924. LinesToStr([ // $mod.$main
  18925. '$mod.A = new ExtA();',
  18926. '']));
  18927. end;
  18928. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  18929. begin
  18930. StartProgram(false);
  18931. Add('{$modeswitch externalclass}');
  18932. Add('type');
  18933. Add(' TExtA = class external name ''ExtA''');
  18934. Add(' procedure DoIt;');
  18935. Add(' end;');
  18936. Add(' TMyA = class(TExtA)');
  18937. Add(' procedure DoIt;');
  18938. Add(' end;');
  18939. Add('procedure TMyA.DoIt; begin end;');
  18940. Add('begin');
  18941. ConvertProgram;
  18942. CheckSource('TestExternalClass_ReintroduceOverload',
  18943. LinesToStr([ // statements
  18944. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  18945. ' this.$init = function () {',
  18946. ' };',
  18947. ' this.$final = function () {',
  18948. ' };',
  18949. ' this.DoIt$1 = function () {',
  18950. ' };',
  18951. '});',
  18952. '']),
  18953. LinesToStr([ // $mod.$main
  18954. '']));
  18955. end;
  18956. procedure TTestModule.TestExternalClass_Inherited;
  18957. begin
  18958. StartProgram(false);
  18959. Add('{$modeswitch externalclass}');
  18960. Add('type');
  18961. Add(' TExtA = class external name ''ExtA''');
  18962. Add(' procedure DoIt(i: longint = 1); virtual;');
  18963. Add(' procedure DoSome(j: longint = 2);');
  18964. Add(' end;');
  18965. Add(' TExtB = class external name ''ExtB''(TExtA)');
  18966. Add(' end;');
  18967. Add(' TMyC = class(TExtB)');
  18968. Add(' procedure DoIt(i: longint = 1); override;');
  18969. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  18970. Add(' end;');
  18971. Add('procedure TMyC.DoIt(i: longint);');
  18972. Add('begin');
  18973. Add(' inherited;');
  18974. Add(' inherited DoIt;');
  18975. Add(' inherited DoIt();');
  18976. Add(' inherited DoIt(3);');
  18977. Add(' inherited DoSome;');
  18978. Add(' inherited DoSome();');
  18979. Add(' inherited DoSome(4);');
  18980. Add('end;');
  18981. Add('procedure TMyC.DoSome(j: longint);');
  18982. Add('begin');
  18983. Add(' inherited;');
  18984. Add('end;');
  18985. Add('begin');
  18986. ConvertProgram;
  18987. CheckSource('TestExternalClass_ReintroduceOverload',
  18988. LinesToStr([ // statements
  18989. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  18990. ' this.$init = function () {',
  18991. ' };',
  18992. ' this.$final = function () {',
  18993. ' };',
  18994. ' this.DoIt = function (i) {',
  18995. ' ExtB.DoIt.apply(this, arguments);',
  18996. ' ExtB.DoIt.call(this, 1);',
  18997. ' ExtB.DoIt.call(this, 1);',
  18998. ' ExtB.DoIt.call(this, 3);',
  18999. ' ExtB.DoSome.call(this, 2);',
  19000. ' ExtB.DoSome.call(this, 2);',
  19001. ' ExtB.DoSome.call(this, 4);',
  19002. ' };',
  19003. ' this.DoSome$1 = function (j) {',
  19004. ' ExtB.DoSome.apply(this, arguments);',
  19005. ' };',
  19006. '});',
  19007. '']),
  19008. LinesToStr([ // $mod.$main
  19009. '']));
  19010. end;
  19011. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19012. begin
  19013. StartProgram(false);
  19014. Add('{$modeswitch externalclass}');
  19015. Add('type');
  19016. Add(' TObject = class');
  19017. Add(' end;');
  19018. Add(' TExtA = class external name ''ExtA''(TObject)');
  19019. Add(' end;');
  19020. Add('begin');
  19021. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19022. ConvertProgram;
  19023. end;
  19024. procedure TTestModule.TestExternalClass_NewInstance;
  19025. begin
  19026. StartProgram(false);
  19027. Add('{$modeswitch externalclass}');
  19028. Add('type');
  19029. Add(' TExtA = class external name ''ExtA''');
  19030. Add(' end;');
  19031. Add(' TMyB = class(TExtA)');
  19032. Add(' protected');
  19033. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19034. Add(' end;');
  19035. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19036. Add('begin end;');
  19037. Add('begin');
  19038. ConvertProgram;
  19039. CheckSource('TestExternalClass_NewInstance',
  19040. LinesToStr([ // statements
  19041. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19042. ' this.$init = function () {',
  19043. ' };',
  19044. ' this.$final = function () {',
  19045. ' };',
  19046. ' this.NewInstance = function (fnname, paramarray) {',
  19047. ' var Result = null;',
  19048. ' return Result;',
  19049. ' };',
  19050. '});',
  19051. '']),
  19052. LinesToStr([ // $mod.$main
  19053. '']));
  19054. end;
  19055. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19056. begin
  19057. StartProgram(false);
  19058. Add('{$modeswitch externalclass}');
  19059. Add('type');
  19060. Add(' TExtA = class external name ''ExtA''');
  19061. Add(' end;');
  19062. Add(' TMyB = class(TExtA)');
  19063. Add(' protected');
  19064. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19065. Add(' end;');
  19066. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19067. Add('begin end;');
  19068. Add('begin');
  19069. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19070. ConvertProgram;
  19071. end;
  19072. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19073. begin
  19074. StartProgram(false);
  19075. Add('{$modeswitch externalclass}');
  19076. Add('type');
  19077. Add(' TExtA = class external name ''ExtA''');
  19078. Add(' end;');
  19079. Add(' TMyB = class(TExtA)');
  19080. Add(' protected');
  19081. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19082. Add(' end;');
  19083. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19084. Add('begin end;');
  19085. Add('begin');
  19086. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19087. nIncompatibleTypeArgNo);
  19088. ConvertProgram;
  19089. end;
  19090. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19091. begin
  19092. StartProgram(false);
  19093. Add('{$modeswitch externalclass}');
  19094. Add('type');
  19095. Add(' TExtA = class external name ''ExtA''');
  19096. Add(' end;');
  19097. Add(' TMyB = class(TExtA)');
  19098. Add(' protected');
  19099. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19100. Add(' end;');
  19101. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19102. Add('begin end;');
  19103. Add('begin');
  19104. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19105. nIncompatibleTypeArgNo);
  19106. ConvertProgram;
  19107. end;
  19108. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19109. begin
  19110. StartProgram(false);
  19111. Add([
  19112. '{$modeswitch externalclass}',
  19113. 'type',
  19114. ' TJSFunction = class external name ''Function''',
  19115. ' end;',
  19116. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19117. ' constructor New(w: word);',
  19118. ' end;',
  19119. ' TBird = class (TExtA)',
  19120. ' public',
  19121. ' Size: word;',
  19122. ' class var Legs: word;',
  19123. ' constructor Create(a: word);',
  19124. ' end;',
  19125. ' TEagle = class (TBird)',
  19126. ' public',
  19127. ' constructor Create(b: word); reintroduce;',
  19128. ' end;',
  19129. 'constructor TBird.Create(a: word);',
  19130. 'begin',
  19131. ' inherited;', // silently ignored
  19132. ' inherited New(a);', // this.$func(a)
  19133. 'end;',
  19134. 'constructor TEagle.Create(b: word);',
  19135. 'begin',
  19136. ' inherited Create(b);',
  19137. 'end;',
  19138. 'var',
  19139. ' Bird: TBird;',
  19140. ' Eagle: TEagle;',
  19141. 'begin',
  19142. ' Bird:=TBird.Create(3);',
  19143. ' Eagle:=TEagle.Create(4);',
  19144. ' Bird.Size:=Bird.Size+5;',
  19145. ' Bird.Legs:=Bird.Legs+6;',
  19146. ' Eagle.Size:=Eagle.Size+5;',
  19147. ' Eagle.Legs:=Eagle.Legs+6;',
  19148. '']);
  19149. ConvertProgram;
  19150. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19151. LinesToStr([ // statements
  19152. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19153. ' this.Legs = 0;',
  19154. ' this.$init = function () {',
  19155. ' this.Size = 0;',
  19156. ' };',
  19157. ' this.$final = function () {',
  19158. ' };',
  19159. ' this.Create = function (a) {',
  19160. ' this.$ancestorfunc(a);',
  19161. ' return this;',
  19162. ' };',
  19163. '});',
  19164. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19165. ' this.Create$1 = function (b) {',
  19166. ' $mod.TBird.Create.call(this, b);',
  19167. ' return this;',
  19168. ' };',
  19169. '});',
  19170. 'this.Bird = null;',
  19171. 'this.Eagle = null;',
  19172. '']),
  19173. LinesToStr([ // $mod.$main
  19174. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19175. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19176. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19177. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19178. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19179. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19180. '']));
  19181. end;
  19182. procedure TTestModule.TestExternalClass_PascalProperty;
  19183. begin
  19184. StartProgram(false);
  19185. Add('{$modeswitch externalclass}');
  19186. Add('type');
  19187. Add(' TJSElement = class;');
  19188. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19189. Add(' TJSElement = class external name ''ExtA''');
  19190. Add(' end;');
  19191. Add(' TControl = class(TJSElement)');
  19192. Add(' private');
  19193. Add(' FOnClick: TJSNotifyEvent;');
  19194. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19195. Add(' procedure Click(Sender: TJSElement);');
  19196. Add(' end;');
  19197. Add('procedure TControl.Click(Sender: TJSElement);');
  19198. Add('begin');
  19199. Add(' OnClick(Self);');
  19200. Add('end;');
  19201. Add('var');
  19202. Add(' Ctrl: TControl;');
  19203. Add('begin');
  19204. Add(' Ctrl.OnClick:[email protected];');
  19205. Add(' Ctrl.OnClick(Ctrl);');
  19206. ConvertProgram;
  19207. CheckSource('TestExternalClass_PascalProperty',
  19208. LinesToStr([ // statements
  19209. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19210. ' this.$init = function () {',
  19211. ' this.FOnClick = null;',
  19212. ' };',
  19213. ' this.$final = function () {',
  19214. ' this.FOnClick = undefined;',
  19215. ' };',
  19216. ' this.Click = function (Sender) {',
  19217. ' this.FOnClick(this);',
  19218. ' };',
  19219. '});',
  19220. 'this.Ctrl = null;',
  19221. '']),
  19222. LinesToStr([ // $mod.$main
  19223. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19224. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19225. '']));
  19226. end;
  19227. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19228. begin
  19229. StartProgram(false);
  19230. Add([
  19231. '{$modeswitch externalclass}',
  19232. 'type',
  19233. ' IUnknown = interface end;',
  19234. ' TObject = class',
  19235. ' end;',
  19236. ' TChild = class',
  19237. ' end;',
  19238. ' TExtRootA = class external name ''ExtRootA''',
  19239. ' end;',
  19240. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19241. ' end;',
  19242. ' TExtRootB = class external name ''ExtRootB''',
  19243. ' end;',
  19244. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19245. ' end;',
  19246. ' TExtString = class external name ''String''',
  19247. ' function charAt(aIndex : NativeInt) : string;',
  19248. ' end;',
  19249. 'var',
  19250. ' Obj: TObject;',
  19251. ' Child: TChild;',
  19252. ' RootA: TExtRootA;',
  19253. ' ChildA: TExtChildA;',
  19254. ' RootB: TExtRootB;',
  19255. ' ChildB: TExtChildB;',
  19256. ' i: IUnknown;',
  19257. ' s: string;',
  19258. ' v: jsvalue;',
  19259. 'begin',
  19260. ' obj:=tobject(roota);',
  19261. ' obj:=tobject(childa);',
  19262. ' child:=tchild(tobject(roota));',
  19263. ' roota:=textroota(obj);',
  19264. ' roota:=textroota(child);',
  19265. ' roota:=textroota(rootb);',
  19266. ' roota:=textroota(childb);',
  19267. ' childa:=textchilda(textroota(obj));',
  19268. ' roota:=TExtRootA(i);',
  19269. ' s:=TExtString(s).charAt(7);',
  19270. ' s:=TExtString(v).charAt(8);',
  19271. '']);
  19272. ConvertProgram;
  19273. CheckSource('TestExternalClass_TypeCastToRootClass',
  19274. LinesToStr([ // statements
  19275. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19276. 'rtl.createClass(this, "TObject", null, function () {',
  19277. ' this.$init = function () {',
  19278. ' };',
  19279. ' this.$final = function () {',
  19280. ' };',
  19281. '});',
  19282. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19283. '});',
  19284. 'this.Obj = null;',
  19285. 'this.Child = null;',
  19286. 'this.RootA = null;',
  19287. 'this.ChildA = null;',
  19288. 'this.RootB = null;',
  19289. 'this.ChildB = null;',
  19290. 'this.i = null;',
  19291. 'this.s = "";',
  19292. 'this.v = undefined;',
  19293. '']),
  19294. LinesToStr([ // $mod.$main
  19295. '$mod.Obj = $mod.RootA;',
  19296. '$mod.Obj = $mod.ChildA;',
  19297. '$mod.Child = $mod.RootA;',
  19298. '$mod.RootA = $mod.Obj;',
  19299. '$mod.RootA = $mod.Child;',
  19300. '$mod.RootA = $mod.RootB;',
  19301. '$mod.RootA = $mod.ChildB;',
  19302. '$mod.ChildA = $mod.Obj;',
  19303. '$mod.RootA = $mod.i;',
  19304. '$mod.s = $mod.s.charAt(7);',
  19305. '$mod.s = $mod.v.charAt(8);',
  19306. '']));
  19307. end;
  19308. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19309. begin
  19310. StartProgram(false);
  19311. Add([
  19312. '{$modeswitch externalclass}',
  19313. 'type',
  19314. ' IUnknown = interface end;',
  19315. ' IBird = interface(IUnknown) end;',
  19316. ' TClass = class of TObject;',
  19317. ' TObject = class',
  19318. ' end;',
  19319. ' TChild = class',
  19320. ' end;',
  19321. ' TJSObject = class external name ''Object''',
  19322. ' end;',
  19323. ' TRec = record end;',
  19324. 'var',
  19325. ' Obj: TObject;',
  19326. ' Child: TChild;',
  19327. ' i: IUnknown;',
  19328. ' Bird: IBird;',
  19329. ' j: TJSObject;',
  19330. ' r: TRec;',
  19331. ' c: TClass;',
  19332. 'begin',
  19333. ' j:=tjsobject(IUnknown);',
  19334. ' j:=tjsobject(IBird);',
  19335. ' j:=tjsobject(TObject);',
  19336. ' j:=tjsobject(TChild);',
  19337. ' j:=tjsobject(TRec);',
  19338. ' j:=tjsobject(Obj);',
  19339. ' j:=tjsobject(Child);',
  19340. ' j:=tjsobject(i);',
  19341. ' j:=tjsobject(Bird);',
  19342. ' j:=tjsobject(r);',
  19343. ' j:=tjsobject(c);',
  19344. '']);
  19345. ConvertProgram;
  19346. CheckSource('TestExternalClass_TypeCastToJSObject',
  19347. LinesToStr([ // statements
  19348. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19349. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19350. 'rtl.createClass(this, "TObject", null, function () {',
  19351. ' this.$init = function () {',
  19352. ' };',
  19353. ' this.$final = function () {',
  19354. ' };',
  19355. '});',
  19356. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19357. '});',
  19358. 'rtl.recNewT(this, "TRec", function () {',
  19359. ' this.$eq = function (b) {',
  19360. ' return true;',
  19361. ' };',
  19362. ' this.$assign = function (s) {',
  19363. ' return this;',
  19364. ' };',
  19365. '});',
  19366. 'this.Obj = null;',
  19367. 'this.Child = null;',
  19368. 'this.i = null;',
  19369. 'this.Bird = null;',
  19370. 'this.j = null;',
  19371. 'this.r = this.TRec.$new();',
  19372. 'this.c = null;',
  19373. '']),
  19374. LinesToStr([ // $mod.$main
  19375. '$mod.j = $mod.IUnknown;',
  19376. '$mod.j = $mod.IBird;',
  19377. '$mod.j = $mod.TObject;',
  19378. '$mod.j = $mod.TChild;',
  19379. '$mod.j = $mod.TRec;',
  19380. '$mod.j = $mod.Obj;',
  19381. '$mod.j = $mod.Child;',
  19382. '$mod.j = $mod.i;',
  19383. '$mod.j = $mod.Bird;',
  19384. '$mod.j = $mod.r;',
  19385. '$mod.j = $mod.c;',
  19386. '']));
  19387. end;
  19388. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19389. begin
  19390. StartProgram(false);
  19391. Add('{$modeswitch externalclass}');
  19392. Add('type');
  19393. Add(' TJSString = class external name ''String''');
  19394. Add(' class function fromCharCode() : string; varargs;');
  19395. Add(' function anchor(const aName : string) : string;');
  19396. Add(' end;');
  19397. Add('var');
  19398. Add(' s: string;');
  19399. Add('begin');
  19400. Add(' s:=TJSString.fromCharCode(65,66);');
  19401. Add(' s:=TJSString(s).anchor(s);');
  19402. Add(' s:=TJSString(''foo'').anchor(s);');
  19403. ConvertProgram;
  19404. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19405. LinesToStr([ // statements
  19406. 'this.s = "";',
  19407. '']),
  19408. LinesToStr([ // $mod.$main
  19409. '$mod.s = String.fromCharCode(65, 66);',
  19410. '$mod.s = $mod.s.anchor($mod.s);',
  19411. '$mod.s = "foo".anchor($mod.s);',
  19412. '']));
  19413. end;
  19414. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19415. begin
  19416. StartProgram(false);
  19417. Add([
  19418. '{$modeswitch externalclass}',
  19419. 'type',
  19420. ' TJSObject = class external name ''Object'' end;',
  19421. ' TJSFunction = class external name ''Function''',
  19422. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19423. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19424. ' end;',
  19425. ' TObject = class',
  19426. ' procedure DoIt(i: longint);',
  19427. ' end;',
  19428. ' TFuncInt = function(o: TObject): longint;',
  19429. 'function GetIt(o: TObject): longint;',
  19430. ' procedure Sub; begin end;',
  19431. 'var',
  19432. ' f: TJSFunction;',
  19433. ' fi: TFuncInt;',
  19434. 'begin',
  19435. ' fi:=TFuncInt(f);',
  19436. ' f:=TJSFunction(fi);',
  19437. ' f:=TJSFunction(@GetIt);',
  19438. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19439. ' f:=TJSFunction(@Sub);',
  19440. ' f:=TJSFunction(@o.doit);',
  19441. ' f:=TJSFunction(fi).bind(nil,4)',
  19442. 'end;',
  19443. 'procedure TObject.DoIt(i: longint);',
  19444. ' procedure Sub; begin end;',
  19445. 'var f: TJSFunction;',
  19446. 'begin',
  19447. ' f:=TJSFunction(@DoIt);',
  19448. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19449. ' f:=TJSFunction(@Sub);',
  19450. ' f:=TJSFunction(@GetIt);',
  19451. 'end;',
  19452. 'begin']);
  19453. ConvertProgram;
  19454. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19455. LinesToStr([ // statements
  19456. 'rtl.createClass(this, "TObject", null, function () {',
  19457. ' this.$init = function () {',
  19458. ' };',
  19459. ' this.$final = function () {',
  19460. ' };',
  19461. ' this.DoIt = function (i) {',
  19462. ' var $Self = this;',
  19463. ' function Sub() {',
  19464. ' };',
  19465. ' var f = null;',
  19466. ' f = this.DoIt;',
  19467. ' f = this.DoIt.bind(null, 13);',
  19468. ' f = Sub;',
  19469. ' f = $mod.GetIt;',
  19470. ' };',
  19471. '});',
  19472. 'this.GetIt = function (o) {',
  19473. ' var Result = 0;',
  19474. ' function Sub() {',
  19475. ' };',
  19476. ' var f = null;',
  19477. ' var fi = null;',
  19478. ' fi = f;',
  19479. ' f = fi;',
  19480. ' f = $mod.GetIt;',
  19481. ' f = $mod.GetIt.bind(null, 3);',
  19482. ' f = Sub;',
  19483. ' f = $mod.TObject.DoIt;',
  19484. ' f = fi.bind(null, 4);',
  19485. ' return Result;',
  19486. '};',
  19487. '']),
  19488. LinesToStr([ // $mod.$main
  19489. '']));
  19490. end;
  19491. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19492. begin
  19493. StartProgram(false);
  19494. Add([
  19495. '{$mode delphi}',
  19496. '{$modeswitch externalclass}',
  19497. 'type',
  19498. ' TJSObject = class external name ''Object'' end;',
  19499. ' TJSWindow = class external name ''Window''(TJSObject)',
  19500. ' procedure Open;',
  19501. ' end;',
  19502. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19503. ' procedure Execute;',
  19504. ' end;',
  19505. 'procedure Fly;',
  19506. 'var',
  19507. ' w: TJSWindow;',
  19508. ' e: TJSEventTarget;',
  19509. 'begin',
  19510. ' w:=TJSWindow(e);',
  19511. ' e:=TJSEventTarget(w);',
  19512. 'end;',
  19513. 'begin']);
  19514. ConvertProgram;
  19515. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19516. LinesToStr([ // statements
  19517. 'this.Fly = function () {',
  19518. ' var w = null;',
  19519. ' var e = null;',
  19520. ' w = e;',
  19521. ' e = w;',
  19522. '};',
  19523. '']),
  19524. LinesToStr([ // $mod.$main
  19525. '']));
  19526. end;
  19527. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19528. begin
  19529. StartProgram(false);
  19530. Add('{$modeswitch externalclass}');
  19531. Add('type');
  19532. Add(' TJSString = class external name ''String''');
  19533. Add(' class function fromCharCode() : string; varargs;');
  19534. Add(' end;');
  19535. Add('var');
  19536. Add(' s: string;');
  19537. Add(' sObj: TJSString;');
  19538. Add('begin');
  19539. Add(' s:=sObj.fromCharCode(65,66);');
  19540. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19541. nExternalClassInstanceCannotAccessStaticX);
  19542. ConvertProgram;
  19543. end;
  19544. procedure TTestModule.TestExternalClass_BracketAccessor;
  19545. begin
  19546. StartProgram(false);
  19547. Add([
  19548. '{$modeswitch externalclass}',
  19549. 'type',
  19550. ' TJSArray = class external name ''Array2''',
  19551. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19552. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19553. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19554. ' end;',
  19555. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19556. 'begin end;',
  19557. 'var',
  19558. ' Arr: tjsarray;',
  19559. ' s: string;',
  19560. ' i: longint;',
  19561. ' v: jsvalue;',
  19562. 'begin',
  19563. ' v:=arr[0];',
  19564. ' v:=arr.items[1];',
  19565. ' arr[2]:=s;',
  19566. ' arr.items[3]:=s;',
  19567. ' arr[4]:=i;',
  19568. ' arr[5]:=arr[6];',
  19569. ' arr.items[7]:=arr.items[8];',
  19570. ' with arr do items[9]:=items[10];',
  19571. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19572. ' with arr do begin',
  19573. ' v:=GetItems(14);',
  19574. ' setitems(15,16);',
  19575. ' end;',
  19576. ' v:=test1.arr.items[17];',
  19577. ' test1.arr.items[18]:=v;',
  19578. '']);
  19579. ConvertProgram;
  19580. CheckSource('TestExternalClass_BracketAccessor',
  19581. LinesToStr([ // statements
  19582. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19583. '};',
  19584. 'this.Arr = null;',
  19585. 'this.s = "";',
  19586. 'this.i = 0;',
  19587. 'this.v = undefined;',
  19588. '']),
  19589. LinesToStr([ // $mod.$main
  19590. '$mod.v = $mod.Arr[0];',
  19591. '$mod.v = $mod.Arr[1];',
  19592. '$mod.Arr[2] = $mod.s;',
  19593. '$mod.Arr[3] = $mod.s;',
  19594. '$mod.Arr[4] = $mod.i;',
  19595. '$mod.Arr[5] = $mod.Arr[6];',
  19596. '$mod.Arr[7] = $mod.Arr[8];',
  19597. 'var $with = $mod.Arr;',
  19598. '$with[9] = $with[10];',
  19599. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19600. ' a: 9,',
  19601. ' p: $mod.Arr,',
  19602. ' get: function () {',
  19603. ' return this.p[this.a];',
  19604. ' },',
  19605. ' set: function (v) {',
  19606. ' this.p[this.a] = v;',
  19607. ' }',
  19608. '}, {',
  19609. ' a: 10,',
  19610. ' p: $mod.Arr,',
  19611. ' get: function () {',
  19612. ' return this.p[this.a];',
  19613. ' },',
  19614. ' set: function (v) {',
  19615. ' this.p[this.a] = v;',
  19616. ' }',
  19617. '});',
  19618. 'var $with1 = $mod.Arr;',
  19619. '$mod.v = $with1[14];',
  19620. '$with1[15] = 16;',
  19621. '$mod.v = $mod.Arr[17];',
  19622. '$mod.Arr[18] = $mod.v;',
  19623. '']));
  19624. end;
  19625. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19626. begin
  19627. StartProgram(false);
  19628. Add([
  19629. '{$modeswitch externalclass}',
  19630. 'type',
  19631. ' TJSArray = class external name ''Array2''',
  19632. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19633. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19634. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19635. ' end;',
  19636. ' TMyArr = class(TJSArray)',
  19637. ' procedure DoIt;',
  19638. ' end;',
  19639. 'procedure tmyarr.DoIt;',
  19640. 'begin',
  19641. ' Items[1]:=Items[2];',
  19642. ' SetItems(3,getItems(4));',
  19643. 'end;',
  19644. 'var',
  19645. ' Arr: tmyarr;',
  19646. ' s: string;',
  19647. ' i: longint;',
  19648. ' v: jsvalue;',
  19649. 'begin',
  19650. ' v:=arr[0];',
  19651. ' v:=arr.items[1];',
  19652. ' arr[2]:=s;',
  19653. ' arr.items[3]:=s;',
  19654. ' arr[4]:=i;',
  19655. ' arr[5]:=arr[6];',
  19656. ' arr.items[7]:=arr.items[8];',
  19657. ' with arr do items[9]:=items[10];',
  19658. ' with arr do begin',
  19659. ' v:=GetItems(14);',
  19660. ' setitems(15,16);',
  19661. ' end;',
  19662. '']);
  19663. ConvertProgram;
  19664. CheckSource('TestExternalClass_BracketAccessor_Call',
  19665. LinesToStr([ // statements
  19666. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  19667. ' this.$init = function () {',
  19668. ' };',
  19669. ' this.$final = function () {',
  19670. ' };',
  19671. ' this.DoIt = function () {',
  19672. ' this[1] = this[2];',
  19673. ' this[3] = this[4];',
  19674. ' };',
  19675. '});',
  19676. 'this.Arr = null;',
  19677. 'this.s = "";',
  19678. 'this.i = 0;',
  19679. 'this.v = undefined;',
  19680. '']),
  19681. LinesToStr([ // $mod.$main
  19682. '$mod.v = $mod.Arr[0];',
  19683. '$mod.v = $mod.Arr[1];',
  19684. '$mod.Arr[2] = $mod.s;',
  19685. '$mod.Arr[3] = $mod.s;',
  19686. '$mod.Arr[4] = $mod.i;',
  19687. '$mod.Arr[5] = $mod.Arr[6];',
  19688. '$mod.Arr[7] = $mod.Arr[8];',
  19689. 'var $with = $mod.Arr;',
  19690. '$with[9] = $with[10];',
  19691. 'var $with1 = $mod.Arr;',
  19692. '$mod.v = $with1[14];',
  19693. '$with1[15] = 16;',
  19694. '']));
  19695. end;
  19696. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  19697. begin
  19698. StartProgram(false);
  19699. Add('{$modeswitch externalclass}');
  19700. Add('type');
  19701. Add(' TJSArray = class external name ''Array2''');
  19702. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  19703. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  19704. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  19705. Add(' end;');
  19706. Add('begin');
  19707. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  19708. nBracketAccessorOfExternalClassMustHaveOneParameter);
  19709. ConvertProgram;
  19710. end;
  19711. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  19712. begin
  19713. StartProgram(false);
  19714. Add('{$modeswitch externalclass}');
  19715. Add('type');
  19716. Add(' TJSArray = class external name ''Array2''');
  19717. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19718. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  19719. Add(' end;');
  19720. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  19721. Add('begin end;');
  19722. Add('var');
  19723. Add(' Arr: tjsarray;');
  19724. Add(' v: jsvalue;');
  19725. Add('begin');
  19726. Add(' v:=arr[0];');
  19727. Add(' v:=arr.items[1];');
  19728. Add(' with arr do v:=items[2];');
  19729. Add(' doit(arr[3],arr[4]);');
  19730. ConvertProgram;
  19731. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  19732. LinesToStr([ // statements
  19733. 'this.DoIt = function (vI, vJ) {',
  19734. '};',
  19735. 'this.Arr = null;',
  19736. 'this.v = undefined;',
  19737. '']),
  19738. LinesToStr([ // $mod.$main
  19739. '$mod.v = $mod.Arr[0];',
  19740. '$mod.v = $mod.Arr[1];',
  19741. 'var $with = $mod.Arr;',
  19742. '$mod.v = $with[2];',
  19743. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  19744. '']));
  19745. end;
  19746. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  19747. begin
  19748. StartProgram(false);
  19749. Add('{$modeswitch externalclass}');
  19750. Add('type');
  19751. Add(' TJSArray = class external name ''Array2''');
  19752. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19753. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  19754. Add(' end;');
  19755. Add('var');
  19756. Add(' Arr: tjsarray;');
  19757. Add(' s: string;');
  19758. Add(' i: longint;');
  19759. Add(' v: jsvalue;');
  19760. Add('begin');
  19761. Add(' arr[2]:=s;');
  19762. Add(' arr.items[3]:=s;');
  19763. Add(' arr[4]:=i;');
  19764. Add(' with arr do items[5]:=i;');
  19765. ConvertProgram;
  19766. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  19767. LinesToStr([ // statements
  19768. 'this.Arr = null;',
  19769. 'this.s = "";',
  19770. 'this.i = 0;',
  19771. 'this.v = undefined;',
  19772. '']),
  19773. LinesToStr([ // $mod.$main
  19774. '$mod.Arr[2] = $mod.s;',
  19775. '$mod.Arr[3] = $mod.s;',
  19776. '$mod.Arr[4] = $mod.i;',
  19777. 'var $with = $mod.Arr;',
  19778. '$with[5] = $mod.i;',
  19779. '']));
  19780. end;
  19781. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  19782. begin
  19783. StartProgram(false);
  19784. Add('{$modeswitch externalclass}');
  19785. Add('type');
  19786. Add(' TJSArray = class external name ''Array2''');
  19787. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19788. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  19789. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  19790. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  19791. Add(' end;');
  19792. Add('var');
  19793. Add(' Arr: tjsarray;');
  19794. Add(' s: string;');
  19795. Add(' i: longint;');
  19796. Add(' v: jsvalue;');
  19797. Add('begin');
  19798. Add(' arr[2]:=s;');
  19799. Add(' arr.items[3]:=s;');
  19800. Add(' arr.numbers[4]:=i;');
  19801. Add(' with arr do items[5]:=i;');
  19802. Add(' with arr do numbers[6]:=i;');
  19803. ConvertProgram;
  19804. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  19805. LinesToStr([ // statements
  19806. 'this.Arr = null;',
  19807. 'this.s = "";',
  19808. 'this.i = 0;',
  19809. 'this.v = undefined;',
  19810. '']),
  19811. LinesToStr([ // $mod.$main
  19812. '$mod.Arr[2] = $mod.s;',
  19813. '$mod.Arr[3] = $mod.s;',
  19814. '$mod.Arr[4] = $mod.i;',
  19815. 'var $with = $mod.Arr;',
  19816. '$with[5] = $mod.i;',
  19817. 'var $with1 = $mod.Arr;',
  19818. '$with1[6] = $mod.i;',
  19819. '']));
  19820. end;
  19821. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  19822. begin
  19823. StartProgram(false);
  19824. Add('{$modeswitch externalclass}');
  19825. Add('type');
  19826. Add(' TJSArray = class external name ''Array2''');
  19827. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  19828. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  19829. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  19830. Add(' end;');
  19831. Add('var');
  19832. Add(' Arr: tjsarray;');
  19833. Add(' i: longint;');
  19834. Add(' IntArr: array of longint;');
  19835. Add(' v: jsvalue;');
  19836. Add('begin');
  19837. Add(' v:=arr.items[i];');
  19838. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  19839. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  19840. ConvertProgram;
  19841. CheckSource('TestExternalClass_BracketAccessor_Index',
  19842. LinesToStr([ // statements
  19843. 'this.Arr = null;',
  19844. 'this.i = 0;',
  19845. 'this.IntArr = [];',
  19846. 'this.v = undefined;',
  19847. '']),
  19848. LinesToStr([ // $mod.$main
  19849. '$mod.v = $mod.Arr[$mod.i];',
  19850. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  19851. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  19852. '']));
  19853. end;
  19854. procedure TTestModule.TestExternalClass_ForInJSObject;
  19855. begin
  19856. StartProgram(false);
  19857. Add([
  19858. '{$modeswitch externalclass}',
  19859. 'type',
  19860. ' TJSObject = class external name ''Object''',
  19861. ' end;',
  19862. 'var',
  19863. ' o: TJSObject;',
  19864. ' key: string;',
  19865. 'begin',
  19866. ' for key in o do',
  19867. ' if key=''abc'' then ;',
  19868. '']);
  19869. ConvertProgram;
  19870. CheckSource('TestExternalClass_ForInJSObject',
  19871. LinesToStr([ // statements
  19872. 'this.o = null;',
  19873. 'this.key = "";',
  19874. '']),
  19875. LinesToStr([ // $mod.$main
  19876. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  19877. '']));
  19878. end;
  19879. procedure TTestModule.TestExternalClass_ForInJSArray;
  19880. begin
  19881. StartProgram(false);
  19882. Add([
  19883. '{$modeswitch externalclass}',
  19884. 'type',
  19885. ' TJSInt8Array = class external name ''Int8Array''',
  19886. ' private',
  19887. ' flength: NativeInt external name ''length'';',
  19888. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  19889. ' public',
  19890. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  19891. ' property Length: NativeInt read flength;',
  19892. ' end;',
  19893. 'var',
  19894. ' a: TJSInt8Array;',
  19895. ' value: shortint;',
  19896. 'begin',
  19897. ' for value in a do',
  19898. ' if value=3 then ;',
  19899. '']);
  19900. ConvertProgram;
  19901. CheckSource('TestExternalClass_ForInJSArray',
  19902. LinesToStr([ // statements
  19903. 'this.a = null;',
  19904. 'this.value = 0;',
  19905. '']),
  19906. LinesToStr([ // $mod.$main
  19907. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  19908. ' $mod.value = $in[$l];',
  19909. ' if ($mod.value === 3) ;',
  19910. '};',
  19911. '']));
  19912. end;
  19913. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  19914. begin
  19915. AddModuleWithIntfImplSrc('unit2.pas',
  19916. LinesToStr([
  19917. '{$modeswitch externalclass}',
  19918. 'type',
  19919. ' TJSBufferSource = class external name ''BufferSource''',
  19920. ' end;',
  19921. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  19922. '']),
  19923. '');
  19924. AddModuleWithIntfImplSrc('unit3.pas',
  19925. LinesToStr([
  19926. '{$modeswitch externalclass}',
  19927. 'type',
  19928. ' TJSBufferSource = class external name ''BufferSource''',
  19929. ' end;',
  19930. '']),
  19931. '');
  19932. StartUnit(true);
  19933. Add([
  19934. 'interface',
  19935. 'uses unit2, unit3;',
  19936. 'procedure DoSome(s: TJSBufferSource);',
  19937. 'implementation',
  19938. 'procedure DoSome(s: TJSBufferSource);',
  19939. 'begin',
  19940. ' DoIt(s);',
  19941. 'end;',
  19942. '']);
  19943. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  19944. nIncompatibleTypeArgNo);
  19945. ConvertUnit;
  19946. end;
  19947. procedure TTestModule.TestExternalClass_NestedConstructor;
  19948. begin
  19949. StartProgram(false);
  19950. Add([
  19951. '{$modeswitch externalclass}',
  19952. 'type',
  19953. ' TJSObject = class external name ''Object''',
  19954. ' type TBird = class external name ''Bird''',
  19955. ' type TWing = class external name ''Wing''',
  19956. ' constructor New;',
  19957. ' constructor Create(w: word = 3);',
  19958. ' end;',
  19959. ' end;',
  19960. ' end;',
  19961. 'var',
  19962. ' w: TJSObject.TBird.TWing;',
  19963. 'begin',
  19964. ' w:=tjsobject.tbird.twing.new;',
  19965. ' w:=tjsobject.tbird.twing.new();',
  19966. ' w:=tjsobject.tbird.twing.create;',
  19967. ' w:=tjsobject.tbird.twing.create(4);',
  19968. ' with tjsobject do begin',
  19969. ' w:=tbird.twing.new;',
  19970. ' w:=tbird.twing.new();',
  19971. ' w:=tbird.twing.create;',
  19972. ' w:=tbird.twing.create(11);',
  19973. ' end;',
  19974. ' with tjsobject.tbird do begin',
  19975. ' w:=twing.new;',
  19976. ' w:=twing.new();',
  19977. ' w:=twing.create;',
  19978. ' w:=twing.create(21);',
  19979. ' end;',
  19980. ' with tjsobject.tbird.twing do begin',
  19981. ' w:=new;',
  19982. ' w:=new();',
  19983. ' w:=create;',
  19984. ' w:=create(31);',
  19985. ' end;',
  19986. '']);
  19987. ConvertProgram;
  19988. CheckSource('TestExternalClass_NestedConstructor',
  19989. LinesToStr([ // statements
  19990. 'this.w = null;',
  19991. '']),
  19992. LinesToStr([ // $mod.$main
  19993. '$mod.w = new Object.Bird.Wing();',
  19994. '$mod.w = new Object.Bird.Wing();',
  19995. '$mod.w = new Object.Bird.Wing.Create();',
  19996. '$mod.w = new Object.Bird.Wing.Create(4);',
  19997. '$mod.w = new Object.Bird.Wing();',
  19998. '$mod.w = new Object.Bird.Wing();',
  19999. '$mod.w = new Object.Bird.Wing.Create();',
  20000. '$mod.w = new Object.Bird.Wing.Create(11);',
  20001. 'var $with = Object.Bird;',
  20002. '$mod.w = new Object.Bird.Wing();',
  20003. '$mod.w = new Object.Bird.Wing();',
  20004. '$mod.w = new Object.Bird.Wing.Create();',
  20005. '$mod.w = new Object.Bird.Wing.Create(21);',
  20006. 'var $with1 = Object.Bird.Wing;',
  20007. '$mod.w = new $with1();',
  20008. '$mod.w = new $with1();',
  20009. '$mod.w = new Object.Bird.Wing.Create();',
  20010. '$mod.w = new Object.Bird.Wing.Create(31);',
  20011. '']));
  20012. end;
  20013. procedure TTestModule.TestClassInterface_Corba;
  20014. begin
  20015. StartProgram(false);
  20016. Add([
  20017. '{$interfaces corba}',
  20018. 'type',
  20019. ' IUnknown = interface;',
  20020. ' IUnknown = interface',
  20021. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20022. ' end;',
  20023. ' IInterface = IUnknown;',
  20024. ' IBird = interface(IInterface)',
  20025. ' function GetSize: longint;',
  20026. ' procedure SetSize(i: longint);',
  20027. ' property Size: longint read GetSize write SetSize;',
  20028. ' procedure DoIt(i: longint);',
  20029. ' end;',
  20030. ' TObject = class',
  20031. ' end;',
  20032. ' TBird = class(TObject,IBird)',
  20033. ' function GetSize: longint; virtual; abstract;',
  20034. ' procedure SetSize(i: longint); virtual; abstract;',
  20035. ' procedure DoIt(i: longint); virtual; abstract;',
  20036. ' end;',
  20037. 'var',
  20038. ' BirdIntf: IBird;',
  20039. 'begin',
  20040. ' BirdIntf.Size:=BirdIntf.Size;',
  20041. '']);
  20042. ConvertProgram;
  20043. CheckSource('TestClassInterface_Corba',
  20044. LinesToStr([ // statements
  20045. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20046. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20047. 'rtl.createClass(this, "TObject", null, function () {',
  20048. ' this.$init = function () {',
  20049. ' };',
  20050. ' this.$final = function () {',
  20051. ' };',
  20052. '});',
  20053. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20054. ' rtl.addIntf(this, $mod.IBird);',
  20055. '});',
  20056. 'this.BirdIntf = null;',
  20057. '']),
  20058. LinesToStr([ // $mod.$main
  20059. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20060. '']));
  20061. end;
  20062. procedure TTestModule.TestClassInterface_ProcExternalFail;
  20063. begin
  20064. StartProgram(false);
  20065. Add([
  20066. '{$interfaces corba}',
  20067. 'type',
  20068. ' IUnknown = interface',
  20069. ' procedure DoIt; external name ''foo'';',
  20070. ' end;',
  20071. 'begin']);
  20072. SetExpectedParserError(
  20073. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20074. nParserNoFieldsAllowed);
  20075. ConvertProgram;
  20076. end;
  20077. procedure TTestModule.TestClassInterface_Overloads;
  20078. begin
  20079. StartProgram(false);
  20080. Add([
  20081. '{$interfaces corba}',
  20082. 'type',
  20083. ' integer = longint;',
  20084. ' IUnknown = interface',
  20085. ' procedure DoIt(i: integer);',
  20086. ' procedure DoIt(s: string);',
  20087. ' end;',
  20088. ' IBird = interface(IUnknown)',
  20089. ' procedure DoIt(b: boolean); overload;',
  20090. ' end;',
  20091. ' TObject = class',
  20092. ' end;',
  20093. ' TBird = class(TObject,IBird)',
  20094. ' procedure DoIt(o: TObject);',
  20095. ' procedure DoIt(s: string);',
  20096. ' procedure DoIt(i: integer);',
  20097. ' procedure DoIt(b: boolean);',
  20098. ' end;',
  20099. 'procedure TBird.DoIt(o: TObject); begin end;',
  20100. 'procedure TBird.DoIt(s: string); begin end;',
  20101. 'procedure TBird.DoIt(i: integer); begin end;',
  20102. 'procedure TBird.DoIt(b: boolean); begin end;',
  20103. 'var',
  20104. ' BirdIntf: IBird;',
  20105. 'begin',
  20106. ' BirdIntf.DoIt(3);',
  20107. ' BirdIntf.DoIt(''abc'');',
  20108. ' BirdIntf.DoIt(true);',
  20109. '']);
  20110. ConvertProgram;
  20111. CheckSource('TestClassInterface_Overloads',
  20112. LinesToStr([ // statements
  20113. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20114. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20115. 'rtl.createClass(this, "TObject", null, function () {',
  20116. ' this.$init = function () {',
  20117. ' };',
  20118. ' this.$final = function () {',
  20119. ' };',
  20120. '});',
  20121. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20122. ' this.DoIt = function (o) {',
  20123. ' };',
  20124. ' this.DoIt$1 = function (s) {',
  20125. ' };',
  20126. ' this.DoIt$2 = function (i) {',
  20127. ' };',
  20128. ' this.DoIt$3 = function (b) {',
  20129. ' };',
  20130. ' rtl.addIntf(this, $mod.IBird, {',
  20131. ' DoIt$2: "DoIt$3",',
  20132. ' DoIt: "DoIt$2"',
  20133. ' });',
  20134. '});',
  20135. 'this.BirdIntf = null;',
  20136. '']),
  20137. LinesToStr([ // $mod.$main
  20138. '$mod.BirdIntf.DoIt(3);',
  20139. '$mod.BirdIntf.DoIt$1("abc");',
  20140. '$mod.BirdIntf.DoIt$2(true);',
  20141. '']));
  20142. end;
  20143. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  20144. begin
  20145. StartProgram(false);
  20146. Add([
  20147. '{$interfaces corba}',
  20148. 'type',
  20149. ' IBird = interface',
  20150. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20151. ' end;',
  20152. ' IDog = interface',
  20153. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20154. ' end;',
  20155. ' TObject = class(IBird,IDog)',
  20156. ' end;',
  20157. 'begin']);
  20158. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20159. nDuplicateGUIDXInYZ);
  20160. ConvertProgram;
  20161. end;
  20162. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  20163. begin
  20164. StartProgram(false);
  20165. Add([
  20166. '{$interfaces corba}',
  20167. 'type',
  20168. ' IAnimal = interface',
  20169. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20170. ' end;',
  20171. ' IBird = interface(IAnimal)',
  20172. ' end;',
  20173. ' IHawk = interface(IBird)',
  20174. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20175. ' end;',
  20176. 'begin']);
  20177. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20178. nDuplicateGUIDXInYZ);
  20179. ConvertProgram;
  20180. end;
  20181. procedure TTestModule.TestClassInterface_AncestorImpl;
  20182. begin
  20183. StartProgram(false);
  20184. Add([
  20185. '{$interfaces corba}',
  20186. 'type',
  20187. ' integer = longint;',
  20188. ' IUnknown = interface',
  20189. ' procedure DoIt(i: integer);',
  20190. ' end;',
  20191. ' IBird = interface',
  20192. ' procedure Fly(i: integer);',
  20193. ' end;',
  20194. ' TObject = class(IUnknown)',
  20195. ' procedure DoIt(i: integer);',
  20196. ' end;',
  20197. ' TBird = class(IBird)',
  20198. ' procedure Fly(i: integer);',
  20199. ' end;',
  20200. 'procedure TObject.DoIt(i: integer); begin end;',
  20201. 'procedure TBird.Fly(i: integer); begin end;',
  20202. 'begin',
  20203. '']);
  20204. ConvertProgram;
  20205. CheckSource('TestClassInterface_AncestorIntf',
  20206. LinesToStr([ // statements
  20207. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20208. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20209. 'rtl.createClass(this, "TObject", null, function () {',
  20210. ' this.$init = function () {',
  20211. ' };',
  20212. ' this.$final = function () {',
  20213. ' };',
  20214. ' this.DoIt = function (i) {',
  20215. ' };',
  20216. ' rtl.addIntf(this, $mod.IUnknown);',
  20217. '});',
  20218. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20219. ' this.Fly = function (i) {',
  20220. ' };',
  20221. ' rtl.addIntf(this, $mod.IBird);',
  20222. ' rtl.addIntf(this, $mod.IUnknown);',
  20223. '});',
  20224. '']),
  20225. LinesToStr([ // $mod.$main
  20226. '']));
  20227. end;
  20228. procedure TTestModule.TestClassInterface_ImplReintroduce;
  20229. begin
  20230. StartProgram(false);
  20231. Add([
  20232. '{$interfaces corba}',
  20233. 'type',
  20234. ' integer = longint;',
  20235. ' IBird = interface',
  20236. ' procedure DoIt(i: integer);',
  20237. ' end;',
  20238. ' TObject = class',
  20239. ' procedure DoIt(i: integer);',
  20240. ' end;',
  20241. ' TBird = class(IBird)',
  20242. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20243. ' end;',
  20244. 'procedure TObject.DoIt(i: integer); begin end;',
  20245. 'procedure TBird.DoIt(i: integer); begin end;',
  20246. 'begin',
  20247. '']);
  20248. ConvertProgram;
  20249. CheckSource('TestClassInterface_ImplReintroduce',
  20250. LinesToStr([ // statements
  20251. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20252. 'rtl.createClass(this, "TObject", null, function () {',
  20253. ' this.$init = function () {',
  20254. ' };',
  20255. ' this.$final = function () {',
  20256. ' };',
  20257. ' this.DoIt = function (i) {',
  20258. ' };',
  20259. '});',
  20260. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20261. ' this.DoIt$1 = function (i) {',
  20262. ' };',
  20263. ' rtl.addIntf(this, $mod.IBird, {',
  20264. ' DoIt: "DoIt$1"',
  20265. ' });',
  20266. '});',
  20267. '']),
  20268. LinesToStr([ // $mod.$main
  20269. '']));
  20270. end;
  20271. procedure TTestModule.TestClassInterface_MethodResolution;
  20272. begin
  20273. StartProgram(false);
  20274. Add([
  20275. '{$interfaces corba}',
  20276. 'type',
  20277. ' IUnknown = interface',
  20278. ' procedure Walk(i: longint);',
  20279. ' end;',
  20280. ' IBird = interface(IUnknown)',
  20281. ' procedure Walk(b: boolean); overload;',
  20282. ' procedure Fly(s: string);',
  20283. ' end;',
  20284. ' TObject = class',
  20285. ' end;',
  20286. ' TBird = class(TObject,IBird)',
  20287. ' procedure IBird.Fly = Move;',
  20288. ' procedure IBird.Walk = Hop;',
  20289. ' procedure Hop(i: longint);',
  20290. ' procedure Move(s: string);',
  20291. ' procedure Hop(b: boolean);',
  20292. ' end;',
  20293. 'procedure TBird.Move(s: string); begin end;',
  20294. 'procedure TBird.Hop(i: longint); begin end;',
  20295. 'procedure TBird.Hop(b: boolean); begin end;',
  20296. 'var',
  20297. ' BirdIntf: IBird;',
  20298. 'begin',
  20299. ' BirdIntf.Walk(3);',
  20300. ' BirdIntf.Walk(true);',
  20301. ' BirdIntf.Fly(''abc'');',
  20302. '']);
  20303. ConvertProgram;
  20304. CheckSource('TestClassInterface_MethodResolution',
  20305. LinesToStr([ // statements
  20306. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20307. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20308. 'rtl.createClass(this, "TObject", null, function () {',
  20309. ' this.$init = function () {',
  20310. ' };',
  20311. ' this.$final = function () {',
  20312. ' };',
  20313. '});',
  20314. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20315. ' this.Hop = function (i) {',
  20316. ' };',
  20317. ' this.Move = function (s) {',
  20318. ' };',
  20319. ' this.Hop$1 = function (b) {',
  20320. ' };',
  20321. ' rtl.addIntf(this, $mod.IBird, {',
  20322. ' Walk$1: "Hop$1",',
  20323. ' Fly: "Move",',
  20324. ' Walk: "Hop"',
  20325. ' });',
  20326. '});',
  20327. 'this.BirdIntf = null;',
  20328. '']),
  20329. LinesToStr([ // $mod.$main
  20330. '$mod.BirdIntf.Walk(3);',
  20331. '$mod.BirdIntf.Walk$1(true);',
  20332. '$mod.BirdIntf.Fly("abc");',
  20333. '']));
  20334. end;
  20335. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  20336. begin
  20337. StartProgram(false);
  20338. Add([
  20339. '{$interfaces com}',
  20340. 'type',
  20341. ' IUnknown = interface',
  20342. ' function _AddRef: longint;',
  20343. ' procedure Walk;',
  20344. ' end;',
  20345. ' IBird = interface end;',
  20346. ' IDog = interface end;',
  20347. ' TObject = class(IBird,IDog)',
  20348. ' function _AddRef: longint; virtual; abstract;',
  20349. ' procedure Walk; virtual; abstract;',
  20350. ' end;',
  20351. ' TBird = class(IUnknown)',
  20352. ' end;',
  20353. 'begin',
  20354. '']);
  20355. ConvertProgram;
  20356. CheckSource('TestClassInterface_COM_AncestorLess',
  20357. LinesToStr([ // statements
  20358. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20359. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20360. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20361. 'rtl.createClass(this, "TObject", null, function () {',
  20362. ' this.$init = function () {',
  20363. ' };',
  20364. ' this.$final = function () {',
  20365. ' };',
  20366. ' rtl.addIntf(this, $mod.IBird);',
  20367. ' rtl.addIntf(this, $mod.IDog);',
  20368. '});',
  20369. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20370. ' rtl.addIntf(this, $mod.IUnknown);',
  20371. ' rtl.addIntf(this, $mod.IBird);',
  20372. ' rtl.addIntf(this, $mod.IDog);',
  20373. '});',
  20374. '']),
  20375. LinesToStr([ // $mod.$main
  20376. '']));
  20377. end;
  20378. procedure TTestModule.TestClassInterface_MethodOverride;
  20379. begin
  20380. StartProgram(false);
  20381. Add([
  20382. '{$interfaces corba}',
  20383. 'type',
  20384. ' IUnknown = interface',
  20385. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20386. ' procedure Go;',
  20387. ' end;',
  20388. ' TObject = class(IUnknown)',
  20389. ' procedure Go; virtual; abstract;',
  20390. ' end;',
  20391. ' TBird = class',
  20392. ' procedure Go; override;',
  20393. ' end;',
  20394. ' TCat = class(TObject)',
  20395. ' procedure Go; override;',
  20396. ' end;',
  20397. ' TDog = class(TObject, IUnknown)',
  20398. ' procedure Go; override;',
  20399. ' end;',
  20400. 'procedure TBird.Go; begin end;',
  20401. 'procedure TCat.Go; begin end;',
  20402. 'procedure TDog.Go; begin end;',
  20403. 'begin',
  20404. '']);
  20405. ConvertProgram;
  20406. CheckSource('TestClassInterface_MethodOverride',
  20407. LinesToStr([ // statements
  20408. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20409. 'rtl.createClass(this, "TObject", null, function () {',
  20410. ' this.$init = function () {',
  20411. ' };',
  20412. ' this.$final = function () {',
  20413. ' };',
  20414. ' rtl.addIntf(this, $mod.IUnknown);',
  20415. '});',
  20416. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20417. ' this.Go = function () {',
  20418. ' };',
  20419. ' rtl.addIntf(this, $mod.IUnknown);',
  20420. '});',
  20421. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20422. ' this.Go = function () {',
  20423. ' };',
  20424. ' rtl.addIntf(this, $mod.IUnknown);',
  20425. '});',
  20426. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20427. ' this.Go = function () {',
  20428. ' };',
  20429. ' rtl.addIntf(this, $mod.IUnknown);',
  20430. '});',
  20431. '']),
  20432. LinesToStr([ // $mod.$main
  20433. '']));
  20434. end;
  20435. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20436. begin
  20437. StartProgram(false);
  20438. Add([
  20439. '{$interfaces corba}',
  20440. 'type',
  20441. ' IUnknown = interface',
  20442. ' end;',
  20443. ' IBird = interface(IUnknown)',
  20444. ' procedure Fly(s: string);',
  20445. ' end;',
  20446. ' IEagle = interface(IBird)',
  20447. ' end;',
  20448. ' IDove = interface(IBird)',
  20449. ' end;',
  20450. ' ISwallow = interface(IBird)',
  20451. ' end;',
  20452. ' TObject = class',
  20453. ' end;',
  20454. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20455. ' procedure Fly(s: string); virtual; abstract;',
  20456. ' end;',
  20457. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20458. ' FBirdIntf: IBird;',
  20459. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20460. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20461. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20462. ' FDoveObj: TBird;',
  20463. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20464. ' function GetSwallowObj: TBird; virtual; abstract;',
  20465. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20466. ' end;',
  20467. 'begin',
  20468. '']);
  20469. ConvertProgram;
  20470. CheckSource('TestClassInterface_Corba_Delegation',
  20471. LinesToStr([ // statements
  20472. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20473. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20474. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20475. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20476. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20477. 'rtl.createClass(this, "TObject", null, function () {',
  20478. ' this.$init = function () {',
  20479. ' };',
  20480. ' this.$final = function () {',
  20481. ' };',
  20482. '});',
  20483. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20484. ' rtl.addIntf(this, $mod.IBird);',
  20485. ' rtl.addIntf(this, $mod.IEagle);',
  20486. ' rtl.addIntf(this, $mod.IDove);',
  20487. ' rtl.addIntf(this, $mod.ISwallow);',
  20488. '});',
  20489. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20490. ' this.$init = function () {',
  20491. ' $mod.TObject.$init.call(this);',
  20492. ' this.FBirdIntf = null;',
  20493. ' this.FDoveObj = null;',
  20494. ' };',
  20495. ' this.$final = function () {',
  20496. ' this.FBirdIntf = undefined;',
  20497. ' this.FDoveObj = undefined;',
  20498. ' $mod.TObject.$final.call(this);',
  20499. ' };',
  20500. ' this.$intfmaps = {',
  20501. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20502. ' return this.FBirdIntf;',
  20503. ' },',
  20504. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20505. ' return this.GetEagleIntf();',
  20506. ' },',
  20507. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20508. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20509. ' },',
  20510. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20511. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20512. ' }',
  20513. ' };',
  20514. '});',
  20515. '']),
  20516. LinesToStr([ // $mod.$main
  20517. '']));
  20518. end;
  20519. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20520. begin
  20521. StartProgram(false);
  20522. Add([
  20523. '{$interfaces corba}',
  20524. 'type',
  20525. ' IUnknown = interface',
  20526. ' end;',
  20527. ' IBird = interface(IUnknown)',
  20528. ' procedure Fly(s: string);',
  20529. ' end;',
  20530. ' IEagle = interface(IBird)',
  20531. ' end;',
  20532. ' IDove = interface(IBird)',
  20533. ' end;',
  20534. ' ISwallow = interface(IBird)',
  20535. ' end;',
  20536. ' TObject = class',
  20537. ' end;',
  20538. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20539. ' procedure Fly(s: string); virtual; abstract;',
  20540. ' end;',
  20541. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20542. ' private',
  20543. ' class var FBirdIntf: IBird;',
  20544. ' class var FDoveObj: TBird;',
  20545. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20546. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20547. ' protected',
  20548. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20549. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20550. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20551. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20552. ' end;',
  20553. 'begin',
  20554. '']);
  20555. ConvertProgram;
  20556. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20557. LinesToStr([ // statements
  20558. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20559. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20560. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20561. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20562. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20563. 'rtl.createClass(this, "TObject", null, function () {',
  20564. ' this.$init = function () {',
  20565. ' };',
  20566. ' this.$final = function () {',
  20567. ' };',
  20568. '});',
  20569. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20570. ' rtl.addIntf(this, $mod.IBird);',
  20571. ' rtl.addIntf(this, $mod.IEagle);',
  20572. ' rtl.addIntf(this, $mod.IDove);',
  20573. ' rtl.addIntf(this, $mod.ISwallow);',
  20574. '});',
  20575. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20576. ' this.FBirdIntf = null;',
  20577. ' this.FDoveObj = null;',
  20578. ' this.$intfmaps = {',
  20579. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20580. ' return this.FBirdIntf;',
  20581. ' },',
  20582. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20583. ' return this.GetEagleIntf();',
  20584. ' },',
  20585. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20586. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20587. ' },',
  20588. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20589. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20590. ' }',
  20591. ' };',
  20592. '});',
  20593. '']),
  20594. LinesToStr([ // $mod.$main
  20595. '']));
  20596. end;
  20597. procedure TTestModule.TestClassInterface_Corba_Operators;
  20598. begin
  20599. StartProgram(false);
  20600. Add([
  20601. '{$interfaces corba}',
  20602. 'type',
  20603. ' IUnknown = interface',
  20604. ' end;',
  20605. ' IBird = interface(IUnknown)',
  20606. ' function GetItems(Index: longint): longint;',
  20607. ' procedure SetItems(Index: longint; Value: longint);',
  20608. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20609. ' end;',
  20610. ' TObject = class',
  20611. ' end;',
  20612. ' TBird = class(TObject,IBird)',
  20613. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20614. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20615. ' end;',
  20616. 'var',
  20617. ' IntfVar: IBird = nil;',
  20618. ' IntfVar2: IBird;',
  20619. ' ObjVar: TBird;',
  20620. ' v: JSValue;',
  20621. 'begin',
  20622. ' IntfVar:=nil;',
  20623. ' IntfVar[3]:=IntfVar[4];',
  20624. ' if Assigned(IntfVar) then ;',
  20625. ' IntfVar:=IntfVar2;',
  20626. ' IntfVar:=ObjVar;',
  20627. ' if IntfVar=IntfVar2 then ;',
  20628. ' if IntfVar<>IntfVar2 then ;',
  20629. ' if IntfVar is IBird then ;',
  20630. ' if IntfVar is TBird then ;',
  20631. ' if ObjVar is IBird then ;',
  20632. ' IntfVar:=IntfVar2 as IBird;',
  20633. ' ObjVar:=IntfVar2 as TBird;',
  20634. ' IntfVar:=ObjVar as IBird;',
  20635. ' IntfVar:=IBird(IntfVar2);',
  20636. ' ObjVar:=TBird(IntfVar);',
  20637. ' IntfVar:=IBird(ObjVar);',
  20638. ' v:=IntfVar;',
  20639. ' IntfVar:=IBird(v);',
  20640. ' if v is IBird then ;',
  20641. ' v:=JSValue(IntfVar);',
  20642. ' v:=IBird;',
  20643. '']);
  20644. ConvertProgram;
  20645. CheckSource('TestClassInterface_Corba_Operators',
  20646. LinesToStr([ // statements
  20647. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20648. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20649. 'rtl.createClass(this, "TObject", null, function () {',
  20650. ' this.$init = function () {',
  20651. ' };',
  20652. ' this.$final = function () {',
  20653. ' };',
  20654. '});',
  20655. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20656. ' rtl.addIntf(this, $mod.IBird);',
  20657. '});',
  20658. 'this.IntfVar = null;',
  20659. 'this.IntfVar2 = null;',
  20660. 'this.ObjVar = null;',
  20661. 'this.v = undefined;',
  20662. '']),
  20663. LinesToStr([ // $mod.$main
  20664. '$mod.IntfVar = null;',
  20665. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  20666. 'if ($mod.IntfVar != null) ;',
  20667. '$mod.IntfVar = $mod.IntfVar2;',
  20668. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  20669. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  20670. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  20671. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  20672. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  20673. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  20674. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  20675. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  20676. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20677. '$mod.IntfVar = $mod.IntfVar2;',
  20678. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  20679. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  20680. '$mod.v = $mod.IntfVar;',
  20681. '$mod.IntfVar = rtl.getObject($mod.v);',
  20682. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  20683. '$mod.v = $mod.IntfVar;',
  20684. '$mod.v = $mod.IBird;',
  20685. '']));
  20686. end;
  20687. procedure TTestModule.TestClassInterface_Corba_Args;
  20688. begin
  20689. StartProgram(false);
  20690. Add([
  20691. '{$interfaces corba}',
  20692. 'type',
  20693. ' IUnknown = interface',
  20694. ' end;',
  20695. ' IBird = interface(IUnknown)',
  20696. ' end;',
  20697. ' TObject = class',
  20698. ' end;',
  20699. ' TBird = class(TObject,IBird)',
  20700. ' end;',
  20701. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  20702. 'begin',
  20703. ' DoIt(i,i,i);',
  20704. 'end;',
  20705. 'procedure Change(var i: IBird; out j: IBird);',
  20706. 'begin',
  20707. ' DoIt(i,i,i);',
  20708. ' Change(i,i);',
  20709. 'end;',
  20710. 'var',
  20711. ' i: IBird;',
  20712. ' o: TBird;',
  20713. 'begin',
  20714. ' DoIt(i,i,i);',
  20715. ' Change(i,i);',
  20716. ' DoIt(o,o,o);',
  20717. '']);
  20718. ConvertProgram;
  20719. CheckSource('TestClassInterface_Corba_Args',
  20720. LinesToStr([ // statements
  20721. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20722. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  20723. 'rtl.createClass(this, "TObject", null, function () {',
  20724. ' this.$init = function () {',
  20725. ' };',
  20726. ' this.$final = function () {',
  20727. ' };',
  20728. '});',
  20729. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20730. ' rtl.addIntf(this, $mod.IBird);',
  20731. '});',
  20732. 'this.DoIt = function (u, i, j) {',
  20733. ' $mod.DoIt({',
  20734. ' get: function () {',
  20735. ' return i;',
  20736. ' },',
  20737. ' set: function (v) {',
  20738. ' i = v;',
  20739. ' }',
  20740. ' }, i, i);',
  20741. '};',
  20742. 'this.Change = function (i, j) {',
  20743. ' $mod.DoIt(i, i.get(), i.get());',
  20744. ' $mod.Change(i, i);',
  20745. '};',
  20746. 'this.i = null;',
  20747. 'this.o = null;',
  20748. '']),
  20749. LinesToStr([ // $mod.$main
  20750. '$mod.DoIt({',
  20751. ' p: $mod,',
  20752. ' get: function () {',
  20753. ' return this.p.i;',
  20754. ' },',
  20755. ' set: function (v) {',
  20756. ' this.p.i = v;',
  20757. ' }',
  20758. '}, $mod.i, $mod.i);',
  20759. '$mod.Change({',
  20760. ' p: $mod,',
  20761. ' get: function () {',
  20762. ' return this.p.i;',
  20763. ' },',
  20764. ' set: function (v) {',
  20765. ' this.p.i = v;',
  20766. ' }',
  20767. '}, {',
  20768. ' p: $mod,',
  20769. ' get: function () {',
  20770. ' return this.p.i;',
  20771. ' },',
  20772. ' set: function (v) {',
  20773. ' this.p.i = v;',
  20774. ' }',
  20775. '});',
  20776. '$mod.DoIt({',
  20777. ' p: $mod,',
  20778. ' get: function () {',
  20779. ' return this.p.o;',
  20780. ' },',
  20781. ' set: function (v) {',
  20782. ' this.p.o = v;',
  20783. ' }',
  20784. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  20785. '']));
  20786. end;
  20787. procedure TTestModule.TestClassInterface_Corba_ForIn;
  20788. begin
  20789. StartProgram(false);
  20790. Add([
  20791. '{$interfaces corba}',
  20792. 'type',
  20793. ' IUnknown = interface end;',
  20794. ' TObject = class',
  20795. ' Id: longint;',
  20796. ' end;',
  20797. ' IEnumerator = interface(IUnknown)',
  20798. ' function GetCurrent: TObject;',
  20799. ' function MoveNext: Boolean;',
  20800. ' property Current: TObject read GetCurrent;',
  20801. ' end;',
  20802. ' IEnumerable = interface(IUnknown)',
  20803. ' function GetEnumerator: IEnumerator;',
  20804. ' end;',
  20805. 'var',
  20806. ' o: TObject;',
  20807. ' i: IEnumerable;',
  20808. 'begin',
  20809. ' for o in i do o.Id:=3;',
  20810. '']);
  20811. ConvertProgram;
  20812. CheckSource('TestClassInterface_Corba_ForIn',
  20813. LinesToStr([ // statements
  20814. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20815. 'rtl.createClass(this, "TObject", null, function () {',
  20816. ' this.$init = function () {',
  20817. ' this.Id = 0;',
  20818. ' };',
  20819. ' this.$final = function () {',
  20820. ' };',
  20821. '});',
  20822. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20823. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20824. 'this.o = null;',
  20825. 'this.i = null;',
  20826. '']),
  20827. LinesToStr([ // $mod.$main
  20828. 'var $in = $mod.i.GetEnumerator();',
  20829. 'while ($in.MoveNext()) {',
  20830. ' $mod.o = $in.GetCurrent();',
  20831. ' $mod.o.Id = 3;',
  20832. '};',
  20833. '']));
  20834. end;
  20835. procedure TTestModule.TestClassInterface_COM_AssignVar;
  20836. begin
  20837. StartProgram(false);
  20838. Add([
  20839. '{$interfaces com}',
  20840. 'type',
  20841. ' IUnknown = interface',
  20842. ' function _AddRef: longint;',
  20843. ' function _Release: longint;',
  20844. ' end;',
  20845. ' TObject = class(IUnknown)',
  20846. ' function _AddRef: longint; virtual; abstract;',
  20847. ' function _Release: longint; virtual; abstract;',
  20848. ' end;',
  20849. 'var',
  20850. ' i: IUnknown;',
  20851. 'procedure DoGlobal(o: TObject);',
  20852. 'begin',
  20853. ' i:=nil;',
  20854. ' i:=o;',
  20855. ' i:=i;',
  20856. 'end;',
  20857. 'procedure DoLocal(o: TObject);',
  20858. 'const k: IUnknown = nil;',
  20859. 'var j: IUnknown;',
  20860. 'begin',
  20861. ' k:=o;',
  20862. ' k:=i;',
  20863. ' j:=o;',
  20864. ' j:=i;',
  20865. 'end;',
  20866. 'var o: TObject;',
  20867. 'begin',
  20868. ' i:=nil;',
  20869. ' i:=o;',
  20870. '']);
  20871. ConvertProgram;
  20872. CheckSource('TestClassInterface_COM_AssignVar',
  20873. LinesToStr([ // statements
  20874. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20875. 'rtl.createClass(this, "TObject", null, function () {',
  20876. ' this.$init = function () {',
  20877. ' };',
  20878. ' this.$final = function () {',
  20879. ' };',
  20880. ' rtl.addIntf(this, $mod.IUnknown);',
  20881. '});',
  20882. 'this.i = null;',
  20883. 'this.DoGlobal = function (o) {',
  20884. ' rtl.setIntfP($mod, "i", null);',
  20885. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  20886. ' rtl.setIntfP($mod, "i", $mod.i);',
  20887. '};',
  20888. 'var k = null;',
  20889. 'this.DoLocal = function (o) {',
  20890. ' var j = null;',
  20891. ' try{',
  20892. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20893. ' k = rtl.setIntfL(k, $mod.i);',
  20894. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20895. ' j = rtl.setIntfL(j, $mod.i);',
  20896. ' }finally{',
  20897. ' rtl._Release(j);',
  20898. ' };',
  20899. '};',
  20900. 'this.o = null;',
  20901. '']),
  20902. LinesToStr([ // $mod.$main
  20903. 'rtl.setIntfP($mod, "i", null);',
  20904. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  20905. '']));
  20906. end;
  20907. procedure TTestModule.TestClassInterface_COM_AssignArg;
  20908. begin
  20909. StartProgram(false);
  20910. Add([
  20911. '{$interfaces com}',
  20912. 'type',
  20913. ' IUnknown = interface',
  20914. ' function _AddRef: longint;',
  20915. ' function _Release: longint;',
  20916. ' end;',
  20917. ' TObject = class(IUnknown)',
  20918. ' function _AddRef: longint; virtual; abstract;',
  20919. ' function _Release: longint; virtual; abstract;',
  20920. ' end;',
  20921. 'procedure DoDefault(i, j: IUnknown);',
  20922. 'begin',
  20923. ' i:=nil;',
  20924. ' i:=j;',
  20925. 'end;',
  20926. 'begin',
  20927. '']);
  20928. ConvertProgram;
  20929. CheckSource('TestClassInterface_COM_AssignArg',
  20930. LinesToStr([ // statements
  20931. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20932. 'rtl.createClass(this, "TObject", null, function () {',
  20933. ' this.$init = function () {',
  20934. ' };',
  20935. ' this.$final = function () {',
  20936. ' };',
  20937. ' rtl.addIntf(this, $mod.IUnknown);',
  20938. '});',
  20939. 'this.DoDefault = function (i, j) {',
  20940. ' rtl._AddRef(i);',
  20941. ' try {',
  20942. ' i = rtl.setIntfL(i, null);',
  20943. ' i = rtl.setIntfL(i, j);',
  20944. ' } finally {',
  20945. ' rtl._Release(i);',
  20946. ' };',
  20947. '};',
  20948. '']),
  20949. LinesToStr([ // $mod.$main
  20950. '']));
  20951. end;
  20952. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  20953. begin
  20954. StartProgram(false);
  20955. Add([
  20956. '{$interfaces com}',
  20957. 'type',
  20958. ' IUnknown = interface',
  20959. ' function _AddRef: longint;',
  20960. ' function _Release: longint;',
  20961. ' end;',
  20962. ' TObject = class(IUnknown)',
  20963. ' function _AddRef: longint; virtual; abstract;',
  20964. ' function _Release: longint; virtual; abstract;',
  20965. ' end;',
  20966. 'function DoDefault(i: IUnknown): IUnknown;',
  20967. 'begin',
  20968. ' Result:=i;',
  20969. ' if Result<>nil then exit;',
  20970. 'end;',
  20971. 'begin',
  20972. '']);
  20973. ConvertProgram;
  20974. CheckSource('TestClassInterface_COM_FunctionResult',
  20975. LinesToStr([ // statements
  20976. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20977. 'rtl.createClass(this, "TObject", null, function () {',
  20978. ' this.$init = function () {',
  20979. ' };',
  20980. ' this.$final = function () {',
  20981. ' };',
  20982. ' rtl.addIntf(this, $mod.IUnknown);',
  20983. '});',
  20984. 'this.DoDefault = function (i) {',
  20985. ' var Result = null;',
  20986. ' var $ok = false;',
  20987. ' try {',
  20988. ' Result = rtl.setIntfL(Result, i);',
  20989. ' if(Result !== null){',
  20990. ' $ok = true;',
  20991. ' return Result;',
  20992. ' };',
  20993. ' $ok = true;',
  20994. ' } finally {',
  20995. ' if(!$ok) rtl._Release(Result);',
  20996. ' };',
  20997. ' return Result;',
  20998. '};',
  20999. '']),
  21000. LinesToStr([ // $mod.$main
  21001. '']));
  21002. end;
  21003. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21004. begin
  21005. StartProgram(false);
  21006. Add([
  21007. '{$interfaces com}',
  21008. 'type',
  21009. ' IUnknown = interface',
  21010. ' function _AddRef: longint;',
  21011. ' function _Release: longint;',
  21012. ' end;',
  21013. ' TObject = class(IUnknown)',
  21014. ' function _AddRef: longint; virtual; abstract;',
  21015. ' function _Release: longint; virtual; abstract;',
  21016. ' function GetIntf: IUnknown; virtual;',
  21017. ' end;',
  21018. ' TMouse = class',
  21019. ' function GetIntf: IUnknown; override;',
  21020. ' end;',
  21021. 'function TObject.GetIntf: IUnknown; begin end;',
  21022. 'function TMouse.GetIntf: IUnknown;',
  21023. 'var i: IUnknown;',
  21024. 'begin',
  21025. ' inherited;',
  21026. ' inherited GetIntf;',
  21027. ' inherited GetIntf();',
  21028. ' Result:=inherited GetIntf;',
  21029. ' Result:=inherited GetIntf();',
  21030. ' i:=inherited GetIntf;',
  21031. ' i:=inherited GetIntf();',
  21032. 'end;',
  21033. 'begin',
  21034. '']);
  21035. ConvertProgram;
  21036. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21037. LinesToStr([ // statements
  21038. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21039. 'rtl.createClass(this, "TObject", null, function () {',
  21040. ' this.$init = function () {',
  21041. ' };',
  21042. ' this.$final = function () {',
  21043. ' };',
  21044. ' this.GetIntf = function () {',
  21045. ' var Result = null;',
  21046. ' return Result;',
  21047. ' };',
  21048. ' rtl.addIntf(this, $mod.IUnknown);',
  21049. '});',
  21050. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21051. ' this.GetIntf = function () {',
  21052. ' var Result = null;',
  21053. ' var i = null;',
  21054. ' var $ir = rtl.createIntfRefs();',
  21055. ' var $ok = false;',
  21056. ' try {',
  21057. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21058. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21059. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21060. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21061. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21062. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21063. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21064. ' $ok = true;',
  21065. ' } finally {',
  21066. ' $ir.free();',
  21067. ' rtl._Release(i);',
  21068. ' if (!$ok) rtl._Release(Result);',
  21069. ' };',
  21070. ' return Result;',
  21071. ' };',
  21072. ' rtl.addIntf(this, $mod.IUnknown);',
  21073. '});',
  21074. '']),
  21075. LinesToStr([ // $mod.$main
  21076. '']));
  21077. end;
  21078. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21079. begin
  21080. StartProgram(false);
  21081. Add([
  21082. '{$interfaces com}',
  21083. 'type',
  21084. ' IUnknown = interface',
  21085. ' function _AddRef: longint;',
  21086. ' function _Release: longint;',
  21087. ' end;',
  21088. ' TObject = class(IUnknown)',
  21089. ' function _AddRef: longint; virtual; abstract;',
  21090. ' function _Release: longint; virtual; abstract;',
  21091. ' end;',
  21092. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21093. 'begin',
  21094. ' if i is IUnknown then ;',
  21095. ' if o is IUnknown then ;',
  21096. ' if i is TObject then ;',
  21097. ' i:=j as IUnknown;',
  21098. ' i:=o as IUnknown;',
  21099. ' o:=j as TObject;',
  21100. ' i:=IUnknown(j);',
  21101. ' i:=IUnknown(o);',
  21102. ' o:=TObject(i);',
  21103. 'end;',
  21104. 'begin',
  21105. '']);
  21106. ConvertProgram;
  21107. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21108. LinesToStr([ // statements
  21109. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21110. 'rtl.createClass(this, "TObject", null, function () {',
  21111. ' this.$init = function () {',
  21112. ' };',
  21113. ' this.$final = function () {',
  21114. ' };',
  21115. ' rtl.addIntf(this, $mod.IUnknown);',
  21116. '});',
  21117. 'this.DoDefault = function (i, j, o) {',
  21118. ' rtl._AddRef(i);',
  21119. ' try {',
  21120. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21121. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21122. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21123. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21124. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21125. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21126. ' i = rtl.setIntfL(i, j);',
  21127. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21128. ' o = rtl.intfToClass(i, $mod.TObject);',
  21129. ' } finally {',
  21130. ' rtl._Release(i);',
  21131. ' };',
  21132. '};',
  21133. '']),
  21134. LinesToStr([ // $mod.$main
  21135. '']));
  21136. end;
  21137. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21138. begin
  21139. StartProgram(false);
  21140. Add([
  21141. '{$interfaces com}',
  21142. 'type',
  21143. ' IUnknown = interface',
  21144. ' function _AddRef: longint;',
  21145. ' function _Release: longint;',
  21146. ' end;',
  21147. ' TObject = class(IUnknown)',
  21148. ' function _AddRef: longint; virtual; abstract;',
  21149. ' function _Release: longint; virtual; abstract;',
  21150. ' end;',
  21151. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21152. 'var o: TObject;',
  21153. 'begin',
  21154. ' DoIt(v,v,v,v);',
  21155. ' DoIt(o,o,k,k);',
  21156. 'end;',
  21157. 'procedure DoSome;',
  21158. 'var v: IUnknown;',
  21159. 'begin',
  21160. ' DoIt(v,v,v,v);',
  21161. 'end;',
  21162. 'var i: IUnknown;',
  21163. 'begin',
  21164. ' DoIt(i,i,i,i);',
  21165. '']);
  21166. ConvertProgram;
  21167. CheckSource('TestClassInterface_COM_PassAsArg',
  21168. LinesToStr([ // statements
  21169. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21170. 'rtl.createClass(this, "TObject", null, function () {',
  21171. ' this.$init = function () {',
  21172. ' };',
  21173. ' this.$final = function () {',
  21174. ' };',
  21175. ' rtl.addIntf(this, $mod.IUnknown);',
  21176. '});',
  21177. 'this.DoIt = function (v, j, k, l) {',
  21178. ' var o = null;',
  21179. ' var $ir = rtl.createIntfRefs();',
  21180. ' rtl._AddRef(v);',
  21181. ' try {',
  21182. ' $mod.DoIt(v, v, {',
  21183. ' get: function () {',
  21184. ' return v;',
  21185. ' },',
  21186. ' set: function (w) {',
  21187. ' v = rtl.setIntfL(v, w);',
  21188. ' }',
  21189. ' }, {',
  21190. ' get: function () {',
  21191. ' return v;',
  21192. ' },',
  21193. ' set: function (w) {',
  21194. ' v = rtl.setIntfL(v, w);',
  21195. ' }',
  21196. ' });',
  21197. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21198. ' } finally {',
  21199. ' $ir.free();',
  21200. ' rtl._Release(v);',
  21201. ' };',
  21202. '};',
  21203. 'this.DoSome = function () {',
  21204. ' var v = null;',
  21205. ' try {',
  21206. ' $mod.DoIt(v, v, {',
  21207. ' get: function () {',
  21208. ' return v;',
  21209. ' },',
  21210. ' set: function (w) {',
  21211. ' v = rtl.setIntfL(v, w);',
  21212. ' }',
  21213. ' }, {',
  21214. ' get: function () {',
  21215. ' return v;',
  21216. ' },',
  21217. ' set: function (w) {',
  21218. ' v = rtl.setIntfL(v, w);',
  21219. ' }',
  21220. ' });',
  21221. ' } finally {',
  21222. ' rtl._Release(v);',
  21223. ' };',
  21224. '};',
  21225. 'this.i = null;',
  21226. '']),
  21227. LinesToStr([ // $mod.$main
  21228. '$mod.DoIt($mod.i, $mod.i, {',
  21229. ' p: $mod,',
  21230. ' get: function () {',
  21231. ' return this.p.i;',
  21232. ' },',
  21233. ' set: function (v) {',
  21234. ' rtl.setIntfP(this.p, "i", v);',
  21235. ' }',
  21236. '}, {',
  21237. ' p: $mod,',
  21238. ' get: function () {',
  21239. ' return this.p.i;',
  21240. ' },',
  21241. ' set: function (v) {',
  21242. ' rtl.setIntfP(this.p, "i", v);',
  21243. ' }',
  21244. '});',
  21245. '']));
  21246. end;
  21247. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21248. begin
  21249. StartProgram(false);
  21250. Add([
  21251. '{$interfaces com}',
  21252. 'type',
  21253. ' IUnknown = interface',
  21254. ' function _AddRef: longint;',
  21255. ' function _Release: longint;',
  21256. ' end;',
  21257. ' TObject = class(IUnknown)',
  21258. ' function _AddRef: longint; virtual; abstract;',
  21259. ' function _Release: longint; virtual; abstract;',
  21260. ' end;',
  21261. 'procedure DoIt(out i);',
  21262. 'begin end;',
  21263. 'procedure DoSome;',
  21264. 'var v: IUnknown;',
  21265. 'begin',
  21266. ' DoIt(v);',
  21267. 'end;',
  21268. 'function GetIt: IUnknown;',
  21269. 'begin',
  21270. ' DoIt(Result);',
  21271. 'end;',
  21272. 'var i: IUnknown;',
  21273. 'begin',
  21274. ' DoIt(i);',
  21275. '']);
  21276. ConvertProgram;
  21277. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21278. LinesToStr([ // statements
  21279. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21280. 'rtl.createClass(this, "TObject", null, function () {',
  21281. ' this.$init = function () {',
  21282. ' };',
  21283. ' this.$final = function () {',
  21284. ' };',
  21285. ' rtl.addIntf(this, $mod.IUnknown);',
  21286. '});',
  21287. 'this.DoIt = function (i) {',
  21288. '};',
  21289. 'this.DoSome = function () {',
  21290. ' var v = null;',
  21291. ' try {',
  21292. ' $mod.DoIt({',
  21293. ' get: function () {',
  21294. ' return v;',
  21295. ' },',
  21296. ' set: function (w) {',
  21297. ' v = w;',
  21298. ' }',
  21299. ' });',
  21300. ' } finally {',
  21301. ' rtl._Release(v);',
  21302. ' };',
  21303. '};',
  21304. 'this.GetIt = function () {',
  21305. ' var Result = null;',
  21306. ' var $ok = false;',
  21307. ' try {',
  21308. ' $mod.DoIt({',
  21309. ' get: function () {',
  21310. ' return Result;',
  21311. ' },',
  21312. ' set: function (v) {',
  21313. ' Result = v;',
  21314. ' }',
  21315. ' });',
  21316. ' $ok = true;',
  21317. ' } finally {',
  21318. ' if (!$ok) rtl._Release(Result);',
  21319. ' };',
  21320. ' return Result;',
  21321. '};',
  21322. 'this.i = null;',
  21323. '']),
  21324. LinesToStr([ // $mod.$main
  21325. 'try {',
  21326. ' $mod.DoIt({',
  21327. ' p: $mod,',
  21328. ' get: function () {',
  21329. ' return this.p.i;',
  21330. ' },',
  21331. ' set: function (v) {',
  21332. ' this.p.i = v;',
  21333. ' }',
  21334. ' });',
  21335. '} finally {',
  21336. ' rtl._Release($mod.i);',
  21337. '};',
  21338. '']));
  21339. end;
  21340. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21341. begin
  21342. StartProgram(false);
  21343. Add([
  21344. '{$interfaces com}',
  21345. 'type',
  21346. ' IUnknown = interface',
  21347. ' function _AddRef: longint;',
  21348. ' function _Release: longint;',
  21349. ' end;',
  21350. ' TObject = class(IUnknown)',
  21351. ' function _AddRef: longint; virtual; abstract;',
  21352. ' function _Release: longint; virtual; abstract;',
  21353. ' end;',
  21354. 'function GetIt: IUnknown;',
  21355. 'begin',
  21356. 'end;',
  21357. 'procedure DoSome;',
  21358. 'var v: IUnknown;',
  21359. ' i: longint;',
  21360. 'begin',
  21361. ' v:=GetIt;',
  21362. ' v:=GetIt();',
  21363. ' GetIt()._AddRef;',
  21364. ' i:=GetIt()._AddRef;',
  21365. 'end;',
  21366. 'var v: IUnknown;',
  21367. ' i: longint;',
  21368. 'begin',
  21369. ' v:=GetIt;',
  21370. ' v:=GetIt();',
  21371. ' GetIt()._AddRef;',
  21372. ' i:=GetIt()._AddRef;',
  21373. '']);
  21374. ConvertProgram;
  21375. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21376. LinesToStr([ // statements
  21377. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21378. 'rtl.createClass(this, "TObject", null, function () {',
  21379. ' this.$init = function () {',
  21380. ' };',
  21381. ' this.$final = function () {',
  21382. ' };',
  21383. ' rtl.addIntf(this, $mod.IUnknown);',
  21384. '});',
  21385. 'this.GetIt = function () {',
  21386. ' var Result = null;',
  21387. ' return Result;',
  21388. '};',
  21389. 'this.DoSome = function () {',
  21390. ' var v = null;',
  21391. ' var i = 0;',
  21392. ' var $ir = rtl.createIntfRefs();',
  21393. ' try {',
  21394. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21395. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21396. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21397. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21398. ' } finally {',
  21399. ' $ir.free();',
  21400. ' rtl._Release(v);',
  21401. ' };',
  21402. '};',
  21403. 'this.v = null;',
  21404. 'this.i = 0;',
  21405. '']),
  21406. LinesToStr([ // $mod.$main
  21407. 'var $ir = rtl.createIntfRefs();',
  21408. 'try {',
  21409. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21410. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21411. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21412. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21413. '} finally {',
  21414. ' $ir.free();',
  21415. '};',
  21416. '']));
  21417. end;
  21418. procedure TTestModule.TestClassInterface_COM_Property;
  21419. begin
  21420. StartProgram(false);
  21421. Add([
  21422. '{$interfaces com}',
  21423. 'type',
  21424. ' IUnknown = interface',
  21425. ' function _AddRef: longint;',
  21426. ' function _Release: longint;',
  21427. ' end;',
  21428. ' TObject = class(IUnknown)',
  21429. ' FAnt: IUnknown;',
  21430. ' function _AddRef: longint; virtual; abstract;',
  21431. ' function _Release: longint; virtual; abstract;',
  21432. ' function GetBird: IUnknown; virtual; abstract;',
  21433. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21434. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21435. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21436. ' property Ant: IUnknown read FAnt write FAnt;',
  21437. ' property Bird: IUnknown read GetBird write SetBird;',
  21438. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21439. ' end;',
  21440. 'procedure DoIt;',
  21441. 'var',
  21442. ' o: TObject;',
  21443. ' v: IUnknown;',
  21444. 'begin',
  21445. ' v:=o.Ant;',
  21446. ' o.Ant:=v;',
  21447. ' o.Ant:=o.Ant;',
  21448. ' v:=o.Bird;',
  21449. ' o.Bird:=v;',
  21450. ' o.Bird:=o.Bird;',
  21451. ' v:=o.Items[1];',
  21452. ' o.Items[2]:=v;',
  21453. ' o.Items[3]:=o.Items[4];',
  21454. ' v:=o[5];',
  21455. ' o[6]:=v;',
  21456. ' o[7]:=o[8];',
  21457. 'end;',
  21458. 'begin',
  21459. '']);
  21460. ConvertProgram;
  21461. CheckSource('TestClassInterface_COM_Property',
  21462. LinesToStr([ // statements
  21463. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21464. 'rtl.createClass(this, "TObject", null, function () {',
  21465. ' this.$init = function () {',
  21466. ' this.FAnt = null;',
  21467. ' };',
  21468. ' this.$final = function () {',
  21469. ' rtl.setIntfP(this, "FAnt", null);',
  21470. ' };',
  21471. ' rtl.addIntf(this, $mod.IUnknown);',
  21472. '});',
  21473. 'this.DoIt = function () {',
  21474. ' var o = null;',
  21475. ' var v = null;',
  21476. ' var $ir = rtl.createIntfRefs();',
  21477. ' try {',
  21478. ' v = rtl.setIntfL(v, o.FAnt);',
  21479. ' rtl.setIntfP(o, "FAnt", v);',
  21480. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21481. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21482. ' o.SetBird(v);',
  21483. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21484. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21485. ' o.SetItems(2, v);',
  21486. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21487. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21488. ' o.SetItems(6, v);',
  21489. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21490. ' } finally {',
  21491. ' $ir.free();',
  21492. ' rtl._Release(v);',
  21493. ' };',
  21494. '};',
  21495. '']),
  21496. LinesToStr([ // $mod.$main
  21497. '']));
  21498. end;
  21499. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21500. begin
  21501. StartProgram(false);
  21502. Add([
  21503. '{$interfaces com}',
  21504. 'type',
  21505. ' IUnknown = interface',
  21506. ' function _AddRef: longint;',
  21507. ' function _Release: longint;',
  21508. ' function GetBird: IUnknown;',
  21509. ' procedure SetBird(Value: IUnknown);',
  21510. ' function GetItems(Index: longint): IUnknown;',
  21511. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21512. ' property Bird: IUnknown read GetBird write SetBird;',
  21513. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21514. ' end;',
  21515. ' TObject = class(IUnknown)',
  21516. ' function _AddRef: longint; virtual; abstract;',
  21517. ' function _Release: longint; virtual; abstract;',
  21518. ' function GetBird: IUnknown; virtual; abstract;',
  21519. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21520. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21521. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21522. ' end;',
  21523. 'procedure DoIt;',
  21524. 'var',
  21525. ' o: TObject;',
  21526. ' v: IUnknown;',
  21527. 'begin',
  21528. ' v:=v.Items[1];',
  21529. ' v.Items[2]:=v;',
  21530. ' v.Items[3]:=v.Items[4];',
  21531. ' v:=v[5];',
  21532. ' v[6]:=v;',
  21533. ' v[7]:=v[8];',
  21534. ' v[9].Bird.Bird:=v;',
  21535. ' v:=v.Bird[10].Bird',
  21536. 'end;',
  21537. 'begin',
  21538. '']);
  21539. ConvertProgram;
  21540. CheckSource('TestClassInterface_COM_IntfProperty',
  21541. LinesToStr([ // statements
  21542. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21543. ' "_AddRef",',
  21544. ' "_Release",',
  21545. ' "GetBird",',
  21546. ' "SetBird",',
  21547. ' "GetItems",',
  21548. ' "SetItems"',
  21549. '], null);',
  21550. 'rtl.createClass(this, "TObject", null, function () {',
  21551. ' this.$init = function () {',
  21552. ' };',
  21553. ' this.$final = function () {',
  21554. ' };',
  21555. ' rtl.addIntf(this, $mod.IUnknown);',
  21556. '});',
  21557. 'this.DoIt = function () {',
  21558. ' var o = null;',
  21559. ' var v = null;',
  21560. ' var $ir = rtl.createIntfRefs();',
  21561. ' try {',
  21562. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  21563. ' v.SetItems(2, v);',
  21564. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  21565. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  21566. ' v.SetItems(6, v);',
  21567. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  21568. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  21569. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  21570. ' } finally {',
  21571. ' $ir.free();',
  21572. ' rtl._Release(v);',
  21573. ' };',
  21574. '};',
  21575. '']),
  21576. LinesToStr([ // $mod.$main
  21577. '']));
  21578. end;
  21579. procedure TTestModule.TestClassInterface_COM_Delegation;
  21580. begin
  21581. StartProgram(false);
  21582. Add([
  21583. '{$interfaces com}',
  21584. 'type',
  21585. ' IUnknown = interface',
  21586. ' function _AddRef: longint;',
  21587. ' function _Release: longint;',
  21588. ' end;',
  21589. ' IBird = interface(IUnknown)',
  21590. ' procedure Fly(s: string);',
  21591. ' end;',
  21592. ' IEagle = interface(IBird) end;',
  21593. ' IDove = interface(IBird) end;',
  21594. ' ISwallow = interface(IBird) end;',
  21595. ' TObject = class',
  21596. ' end;',
  21597. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  21598. ' function _AddRef: longint; virtual; abstract;',
  21599. ' function _Release: longint; virtual; abstract;',
  21600. ' procedure Fly(s: string); virtual; abstract;',
  21601. ' end;',
  21602. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  21603. ' function _AddRef: longint; virtual; abstract;',
  21604. ' function _Release: longint; virtual; abstract;',
  21605. ' FBirdIntf: IBird;',
  21606. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  21607. ' function GetEagleIntf: IEagle; virtual; abstract;',
  21608. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  21609. ' FDoveObj: TBird;',
  21610. ' property DoveObj: TBird read FDoveObj implements IDove;',
  21611. ' function GetSwallowObj: TBird; virtual; abstract;',
  21612. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  21613. ' end;',
  21614. 'begin',
  21615. '']);
  21616. ConvertProgram;
  21617. CheckSource('TestClassInterface_COM_Delegation',
  21618. LinesToStr([ // statements
  21619. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21620. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  21621. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  21622. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  21623. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  21624. 'rtl.createClass(this, "TObject", null, function () {',
  21625. ' this.$init = function () {',
  21626. ' };',
  21627. ' this.$final = function () {',
  21628. ' };',
  21629. '});',
  21630. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21631. ' rtl.addIntf(this, $mod.IBird);',
  21632. ' rtl.addIntf(this, $mod.IEagle);',
  21633. ' rtl.addIntf(this, $mod.IDove);',
  21634. ' rtl.addIntf(this, $mod.ISwallow);',
  21635. '});',
  21636. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  21637. ' this.$init = function () {',
  21638. ' $mod.TObject.$init.call(this);',
  21639. ' this.FBirdIntf = null;',
  21640. ' this.FDoveObj = null;',
  21641. ' };',
  21642. ' this.$final = function () {',
  21643. ' rtl.setIntfP(this, "FBirdIntf", null);',
  21644. ' this.FDoveObj = undefined;',
  21645. ' $mod.TObject.$final.call(this);',
  21646. ' };',
  21647. ' this.$intfmaps = {',
  21648. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  21649. ' return rtl._AddRef(this.FBirdIntf);',
  21650. ' },',
  21651. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  21652. ' return this.GetEagleIntf();',
  21653. ' },',
  21654. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  21655. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  21656. ' },',
  21657. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  21658. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  21659. ' }',
  21660. ' };',
  21661. '});',
  21662. '']),
  21663. LinesToStr([ // $mod.$main
  21664. '']));
  21665. end;
  21666. procedure TTestModule.TestClassInterface_COM_With;
  21667. begin
  21668. StartProgram(false);
  21669. Add([
  21670. '{$interfaces com}',
  21671. 'type',
  21672. ' IUnknown = interface',
  21673. ' function _AddRef: longint;',
  21674. ' function _Release: longint;',
  21675. ' function GetAnt: IUnknown;',
  21676. ' property Ant: IUnknown read GetAnt;',
  21677. ' end;',
  21678. ' TObject = class(IUnknown)',
  21679. ' function _AddRef: longint; virtual; abstract;',
  21680. ' function _Release: longint; virtual; abstract;',
  21681. ' function GetAnt: IUnknown; virtual; abstract;',
  21682. ' property Ant: IUnknown read GetAnt;',
  21683. ' end;',
  21684. 'procedure DoIt;',
  21685. 'var',
  21686. ' i: IUnknown;',
  21687. 'begin',
  21688. ' with i do ',
  21689. ' GetAnt;',
  21690. ' with i.Ant, Ant do ',
  21691. ' GetAnt;',
  21692. 'end;',
  21693. 'begin',
  21694. '']);
  21695. ConvertProgram;
  21696. CheckSource('TestClassInterface_COM_With',
  21697. LinesToStr([ // statements
  21698. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  21699. 'rtl.createClass(this, "TObject", null, function () {',
  21700. ' this.$init = function () {',
  21701. ' };',
  21702. ' this.$final = function () {',
  21703. ' };',
  21704. ' rtl.addIntf(this, $mod.IUnknown);',
  21705. '});',
  21706. 'this.DoIt = function () {',
  21707. ' var i = null;',
  21708. ' var $ir = rtl.createIntfRefs();',
  21709. ' try {',
  21710. ' $ir.ref(1, i.GetAnt());',
  21711. ' var $with = $ir.ref(2, i.GetAnt());',
  21712. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  21713. ' $ir.ref(4, $with1.GetAnt());',
  21714. ' } finally {',
  21715. ' $ir.free();',
  21716. ' };',
  21717. '};',
  21718. '']),
  21719. LinesToStr([ // $mod.$main
  21720. '']));
  21721. end;
  21722. procedure TTestModule.TestClassInterface_COM_ForIn;
  21723. begin
  21724. StartProgram(false);
  21725. Add([
  21726. '{$interfaces com}',
  21727. 'type',
  21728. ' IUnknown = interface end;',
  21729. ' TObject = class',
  21730. ' Id: longint;',
  21731. ' end;',
  21732. ' IEnumerator = interface(IUnknown)',
  21733. ' function GetCurrent: TObject;',
  21734. ' function MoveNext: Boolean;',
  21735. ' property Current: TObject read GetCurrent;',
  21736. ' end;',
  21737. ' IEnumerable = interface(IUnknown)',
  21738. ' function GetEnumerator: IEnumerator;',
  21739. ' end;',
  21740. 'var',
  21741. ' o: TObject;',
  21742. ' i: IEnumerable;',
  21743. 'begin',
  21744. ' for o in i do o.Id:=3;',
  21745. '']);
  21746. ConvertProgram;
  21747. CheckSource('TestClassInterface_COM_ForIn',
  21748. LinesToStr([ // statements
  21749. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21750. 'rtl.createClass(this, "TObject", null, function () {',
  21751. ' this.$init = function () {',
  21752. ' this.Id = 0;',
  21753. ' };',
  21754. ' this.$final = function () {',
  21755. ' };',
  21756. '});',
  21757. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21758. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21759. 'this.o = null;',
  21760. 'this.i = null;',
  21761. '']),
  21762. LinesToStr([ // $mod.$main
  21763. 'var $in = $mod.i.GetEnumerator();',
  21764. 'try {',
  21765. ' while ($in.MoveNext()) {',
  21766. ' $mod.o = $in.GetCurrent();',
  21767. ' $mod.o.Id = 3;',
  21768. ' }',
  21769. '} finally {',
  21770. ' rtl._Release($in)',
  21771. '};',
  21772. '']));
  21773. end;
  21774. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  21775. begin
  21776. StartProgram(false);
  21777. Add([
  21778. '{$interfaces com}',
  21779. 'type',
  21780. ' IUnknown = interface',
  21781. ' function _AddRef: longint;',
  21782. ' function _Release: longint;',
  21783. ' end;',
  21784. ' TObject = class',
  21785. ' end;',
  21786. ' TArrOfIntf = array of IUnknown;',
  21787. 'begin',
  21788. '']);
  21789. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  21790. ConvertProgram;
  21791. end;
  21792. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  21793. begin
  21794. StartProgram(false);
  21795. Add([
  21796. '{$interfaces com}',
  21797. 'type',
  21798. ' IUnknown = interface',
  21799. ' function _AddRef: longint;',
  21800. ' function _Release: longint;',
  21801. ' end;',
  21802. ' TRec = record',
  21803. ' i: IUnknown;',
  21804. ' end;',
  21805. 'begin',
  21806. '']);
  21807. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  21808. ConvertProgram;
  21809. end;
  21810. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  21811. begin
  21812. StartUnit(false);
  21813. Add([
  21814. '{$interfaces com}',
  21815. 'interface',
  21816. 'implementation',
  21817. 'type',
  21818. ' IUnknown = interface',
  21819. ' function _AddRef: longint;',
  21820. ' end;',
  21821. ' TObject = class(IUnknown)',
  21822. ' function _AddRef: longint;',
  21823. ' end;',
  21824. 'function TObject._AddRef: longint; begin end;',
  21825. 'var i: IUnknown;',
  21826. ' o: TObject;',
  21827. 'initialization',
  21828. ' i:=nil;',
  21829. ' i:=i;',
  21830. ' i:=o;',
  21831. ' if (o as IUnknown)=nil then ;',
  21832. '']);
  21833. ConvertUnit;
  21834. CheckSource('TestClassInterface_COM_UnitInitialization',
  21835. LinesToStr([ // statements
  21836. 'var $impl = $mod.$impl;',
  21837. '']),
  21838. LinesToStr([ // this.$init
  21839. 'var $ir = rtl.createIntfRefs();',
  21840. 'try {',
  21841. ' rtl.setIntfP($impl, "i", null);',
  21842. ' rtl.setIntfP($impl, "i", $impl.i);',
  21843. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  21844. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  21845. '} finally {',
  21846. ' $ir.free();',
  21847. '};',
  21848. '']),
  21849. LinesToStr([ // implementation
  21850. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  21851. 'rtl.createClass($impl, "TObject", null, function () {',
  21852. ' this.$init = function () {',
  21853. ' };',
  21854. ' this.$final = function () {',
  21855. ' };',
  21856. ' this._AddRef = function () {',
  21857. ' var Result = 0;',
  21858. ' return Result;',
  21859. ' };',
  21860. ' rtl.addIntf(this, $impl.IUnknown);',
  21861. '});',
  21862. '$impl.i = null;',
  21863. '$impl.o = null;',
  21864. ''])
  21865. );
  21866. end;
  21867. procedure TTestModule.TestClassInterface_GUID;
  21868. begin
  21869. StartProgram(false);
  21870. Add([
  21871. '{$interfaces corba}',
  21872. 'type',
  21873. ' IUnknown = interface',
  21874. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  21875. ' end;',
  21876. ' TObject = class end;',
  21877. ' TGUID = record D1, D2, D3, D4: word; end;',
  21878. ' TAliasGUID = TGUID;',
  21879. ' TGUIDString = type string;',
  21880. ' TAliasGUIDString = TGUIDString;',
  21881. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  21882. 'begin end;',
  21883. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  21884. 'begin end;',
  21885. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  21886. 'begin end;',
  21887. 'var',
  21888. ' i: IUnknown;',
  21889. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  21890. ' s: TAliasGUIDString;',
  21891. 'begin',
  21892. ' DoConstGUIDIt(IUnknown);',
  21893. ' DoDefGUID(IUnknown);',
  21894. ' DoStr(IUnknown);',
  21895. ' DoConstGUIDIt(i);',
  21896. ' DoDefGUID(i);',
  21897. ' DoStr(i);',
  21898. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  21899. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  21900. ' DoStr(g);',
  21901. ' g:=i;',
  21902. ' g:=IUnknown;',
  21903. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  21904. ' s:=i;',
  21905. ' s:=IUnknown;',
  21906. ' s:=g;',
  21907. ' if g=i then ;',
  21908. ' if i=g then ;',
  21909. ' if g=IUnknown then ;',
  21910. ' if IUnknown=g then ;',
  21911. ' if s=i then ;',
  21912. ' if i=s then ;',
  21913. ' if s=IUnknown then ;',
  21914. ' if IUnknown=s then ;',
  21915. ' if s=g then ;',
  21916. ' if g=s then ;',
  21917. '']);
  21918. ConvertProgram;
  21919. CheckSource('TestClassInterface_GUID',
  21920. LinesToStr([ // statements
  21921. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  21922. 'rtl.createClass(this, "TObject", null, function () {',
  21923. ' this.$init = function () {',
  21924. ' };',
  21925. ' this.$final = function () {',
  21926. ' };',
  21927. '});',
  21928. 'rtl.recNewT(this, "TGUID", function () {',
  21929. ' this.D1 = 0;',
  21930. ' this.D2 = 0;',
  21931. ' this.D3 = 0;',
  21932. ' this.D4 = 0;',
  21933. ' this.$eq = function (b) {',
  21934. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  21935. ' };',
  21936. ' this.$assign = function (s) {',
  21937. ' this.D1 = s.D1;',
  21938. ' this.D2 = s.D2;',
  21939. ' this.D3 = s.D3;',
  21940. ' this.D4 = s.D4;',
  21941. ' return this;',
  21942. ' };',
  21943. '});',
  21944. 'this.DoConstGUIDIt = function (g) {',
  21945. '};',
  21946. 'this.DoDefGUID = function (g) {',
  21947. '};',
  21948. 'this.DoStr = function (s) {',
  21949. '};',
  21950. 'this.i = null;',
  21951. 'this.g = this.TGUID.$clone({',
  21952. ' D1: 0xD91C9AF4,',
  21953. ' D2: 0x3C93,',
  21954. ' D3: 0x420F,',
  21955. ' D4: [',
  21956. ' 0xA3,',
  21957. ' 0x03,',
  21958. ' 0xBF,',
  21959. ' 0x5B,',
  21960. ' 0xA8,',
  21961. ' 0x2B,',
  21962. ' 0xFD,',
  21963. ' 0x23',
  21964. ' ]',
  21965. '});',
  21966. 'this.s = "";',
  21967. '']),
  21968. LinesToStr([ // $mod.$main
  21969. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  21970. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  21971. '$mod.DoStr($mod.IUnknown.$guid);',
  21972. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  21973. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  21974. '$mod.DoStr($mod.i.$guid);',
  21975. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  21976. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  21977. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  21978. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  21979. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  21980. '$mod.g.$assign({',
  21981. ' D1: 0xD91C9AF4,',
  21982. ' D2: 0x3C93,',
  21983. ' D3: 0x420F,',
  21984. ' D4: [',
  21985. ' 0xA3,',
  21986. ' 0x03,',
  21987. ' 0xBF,',
  21988. ' 0x5B,',
  21989. ' 0xA8,',
  21990. ' 0x2B,',
  21991. ' 0xFD,',
  21992. ' 0x23',
  21993. ' ]',
  21994. '});',
  21995. '$mod.s = $mod.i.$guid;',
  21996. '$mod.s = $mod.IUnknown.$guid;',
  21997. '$mod.s = rtl.guidrToStr($mod.g);',
  21998. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  21999. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22000. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22001. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22002. 'if ($mod.s === $mod.i.$guid) ;',
  22003. 'if ($mod.i.$guid === $mod.s) ;',
  22004. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22005. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22006. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22007. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22008. '']));
  22009. end;
  22010. procedure TTestModule.TestClassInterface_GUIDProperty;
  22011. begin
  22012. StartProgram(false);
  22013. Add([
  22014. '{$interfaces corba}',
  22015. 'type',
  22016. ' IUnknown = interface',
  22017. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22018. ' end;',
  22019. ' TGUID = record D1, D2, D3, D4: word; end;',
  22020. ' TAliasGUID = TGUID;',
  22021. ' TGUIDString = type string;',
  22022. ' TAliasGUIDString = TGUIDString;',
  22023. ' TObject = class',
  22024. ' function GetG: TAliasGUID; virtual; abstract;',
  22025. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22026. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22027. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22028. ' property g: TAliasGUID read GetG write SetG;',
  22029. ' property s: TAliasGUIDString read GetS write SetS;',
  22030. ' end;',
  22031. 'var o: TObject;',
  22032. 'begin',
  22033. ' o.g:=IUnknown;',
  22034. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22035. ' o.s:=IUnknown;',
  22036. ' o.s:=o.g;',
  22037. '']);
  22038. ConvertProgram;
  22039. CheckSource('TestClassInterface_GUIDProperty',
  22040. LinesToStr([ // statements
  22041. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22042. 'rtl.recNewT(this, "TGUID", function () {',
  22043. ' this.D1 = 0;',
  22044. ' this.D2 = 0;',
  22045. ' this.D3 = 0;',
  22046. ' this.D4 = 0;',
  22047. ' this.$eq = function (b) {',
  22048. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22049. ' };',
  22050. ' this.$assign = function (s) {',
  22051. ' this.D1 = s.D1;',
  22052. ' this.D2 = s.D2;',
  22053. ' this.D3 = s.D3;',
  22054. ' this.D4 = s.D4;',
  22055. ' return this;',
  22056. ' };',
  22057. '});',
  22058. 'rtl.createClass(this, "TObject", null, function () {',
  22059. ' this.$init = function () {',
  22060. ' };',
  22061. ' this.$final = function () {',
  22062. ' };',
  22063. '});',
  22064. 'this.o = null;',
  22065. '']),
  22066. LinesToStr([ // $mod.$main
  22067. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22068. '$mod.o.SetG({',
  22069. ' D1: 0xD91C9AF4,',
  22070. ' D2: 0x3C93,',
  22071. ' D3: 0x420F,',
  22072. ' D4: [',
  22073. ' 0xA3,',
  22074. ' 0x03,',
  22075. ' 0xBF,',
  22076. ' 0x5B,',
  22077. ' 0xA8,',
  22078. ' 0x2B,',
  22079. ' 0xFD,',
  22080. ' 0x23',
  22081. ' ]',
  22082. '});',
  22083. '$mod.o.SetS($mod.IUnknown.$guid);',
  22084. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22085. '']));
  22086. end;
  22087. procedure TTestModule.TestClassHelper_ClassVar;
  22088. begin
  22089. StartProgram(false);
  22090. Add([
  22091. 'type',
  22092. ' TObject = class',
  22093. ' end;',
  22094. ' THelper = class helper for TObject',
  22095. ' const',
  22096. ' One = 1;',
  22097. ' Two: word = 2;',
  22098. ' class var',
  22099. ' Glob: word;',
  22100. ' function Foo(w: word): word;',
  22101. ' class function Bar(w: word): word;',
  22102. ' end;',
  22103. 'function THelper.foo(w: word): word;',
  22104. 'begin',
  22105. ' Result:=w;',
  22106. ' Two:=One+w;',
  22107. ' Glob:=Glob;',
  22108. ' Result:=Self.Glob;',
  22109. ' Self.Glob:=Self.Glob;',
  22110. ' with Self do Glob:=Glob;',
  22111. 'end;',
  22112. 'class function THelper.bar(w: word): word;',
  22113. 'begin',
  22114. ' Result:=w;',
  22115. ' Two:=One;',
  22116. ' Glob:=Glob;',
  22117. ' Self.Glob:=Self.Glob;',
  22118. ' with Self do Glob:=Glob;',
  22119. 'end;',
  22120. 'var o: TObject;',
  22121. 'begin',
  22122. ' tobject.two:=tobject.one;',
  22123. ' tobject.Glob:=tobject.Glob;',
  22124. ' with tobject do begin',
  22125. ' two:=one;',
  22126. ' Glob:=Glob;',
  22127. ' end;',
  22128. ' o.two:=o.one;',
  22129. ' o.Glob:=o.Glob;',
  22130. ' with o do begin',
  22131. ' two:=one;',
  22132. ' Glob:=Glob;',
  22133. ' end;',
  22134. '']);
  22135. ConvertProgram;
  22136. CheckSource('TestClassHelper_ClassVar',
  22137. LinesToStr([ // statements
  22138. 'rtl.createClass(this, "TObject", null, function () {',
  22139. ' this.$init = function () {',
  22140. ' };',
  22141. ' this.$final = function () {',
  22142. ' };',
  22143. '});',
  22144. 'rtl.createHelper(this, "THelper", null, function () {',
  22145. ' this.One = 1;',
  22146. ' this.Two = 2;',
  22147. ' this.Glob = 0;',
  22148. ' this.Foo = function (w) {',
  22149. ' var Result = 0;',
  22150. ' Result = w;',
  22151. ' $mod.THelper.Two = 1 + w;',
  22152. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22153. ' Result = $mod.THelper.Glob;',
  22154. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22155. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22156. ' return Result;',
  22157. ' };',
  22158. ' this.Bar = function (w) {',
  22159. ' var Result = 0;',
  22160. ' Result = w;',
  22161. ' $mod.THelper.Two = 1;',
  22162. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22163. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22164. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22165. ' return Result;',
  22166. ' };',
  22167. '});',
  22168. 'this.o = null;',
  22169. '']),
  22170. LinesToStr([ // $mod.$main
  22171. '$mod.THelper.Two = 1;',
  22172. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22173. 'var $with = $mod.TObject;',
  22174. '$mod.THelper.Two = 1;',
  22175. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22176. '$mod.THelper.Two = 1;',
  22177. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22178. 'var $with1 = $mod.o;',
  22179. '$mod.THelper.Two = 1;',
  22180. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22181. '']));
  22182. end;
  22183. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22184. begin
  22185. StartProgram(false);
  22186. Add([
  22187. 'type',
  22188. ' TObject = class',
  22189. ' FSize: word;',
  22190. ' property Size: word read FSize write FSize;',
  22191. ' end;',
  22192. ' THelper = class helper for TObject',
  22193. ' function Foo(w: word = 1): word;',
  22194. ' end;',
  22195. 'function THelper.foo(w: word): word;',
  22196. 'begin',
  22197. ' Result:=Size;',
  22198. ' Size:=Size+2;',
  22199. ' Self.Size:=Self.Size+3;',
  22200. ' FSize:=FSize+4;',
  22201. ' Self.FSize:=Self.FSize+5;',
  22202. ' with Self do begin',
  22203. ' Size:=Size+6;',
  22204. ' FSize:=FSize+7;',
  22205. ' FSize:=FSize+8;',
  22206. ' end;',
  22207. 'end;',
  22208. 'begin',
  22209. '']);
  22210. ConvertProgram;
  22211. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22212. LinesToStr([ // statements
  22213. 'rtl.createClass(this, "TObject", null, function () {',
  22214. ' this.$init = function () {',
  22215. ' this.FSize = 0;',
  22216. ' };',
  22217. ' this.$final = function () {',
  22218. ' };',
  22219. '});',
  22220. 'rtl.createHelper(this, "THelper", null, function () {',
  22221. ' this.Foo = function (w) {',
  22222. ' var Result = 0;',
  22223. ' Result = this.FSize;',
  22224. ' this.FSize = this.FSize + 2;',
  22225. ' this.FSize = this.FSize + 3;',
  22226. ' this.FSize = this.FSize + 4;',
  22227. ' this.FSize = this.FSize + 5;',
  22228. ' this.FSize = this.FSize + 6;',
  22229. ' this.FSize = this.FSize + 7;',
  22230. ' this.FSize = this.FSize + 8;',
  22231. ' return Result;',
  22232. ' };',
  22233. '});',
  22234. '']),
  22235. LinesToStr([ // $mod.$main
  22236. '']));
  22237. end;
  22238. procedure TTestModule.TestClassHelper_Method_Call;
  22239. begin
  22240. StartProgram(false);
  22241. Add([
  22242. 'type',
  22243. ' TObject = class',
  22244. ' procedure Run(w: word = 10);',
  22245. ' end;',
  22246. ' THelper = class helper for TObject',
  22247. ' function Foo(w: word = 1): word;',
  22248. ' end;',
  22249. 'procedure TObject.Run(w: word);',
  22250. 'var o: TObject;',
  22251. 'begin',
  22252. ' Foo;',
  22253. ' Foo();',
  22254. ' Foo(2);',
  22255. ' Self.Foo;',
  22256. ' Self.Foo();',
  22257. ' Self.Foo(3);',
  22258. ' with Self do begin',
  22259. ' Foo;',
  22260. ' Foo();',
  22261. ' Foo(4);',
  22262. ' end;',
  22263. ' with o do Foo(5);',
  22264. 'end;',
  22265. 'function THelper.foo(w: word): word;',
  22266. 'begin',
  22267. ' Run;',
  22268. ' Run();',
  22269. ' Run(11);',
  22270. ' Foo;',
  22271. ' Foo();',
  22272. ' Foo(12);',
  22273. ' Self.Foo;',
  22274. ' Self.Foo();',
  22275. ' Self.Foo(13);',
  22276. ' with Self do begin',
  22277. ' Foo;',
  22278. ' Foo();',
  22279. ' Foo(14);',
  22280. ' end;',
  22281. 'end;',
  22282. 'var Obj: TObject;',
  22283. 'begin',
  22284. ' obj.Foo;',
  22285. ' obj.Foo();',
  22286. ' obj.Foo(21);',
  22287. ' with obj do begin',
  22288. ' Foo;',
  22289. ' Foo();',
  22290. ' Foo(22);',
  22291. ' end;',
  22292. '']);
  22293. ConvertProgram;
  22294. CheckSource('TestClassHelper_Method_Call',
  22295. LinesToStr([ // statements
  22296. 'rtl.createClass(this, "TObject", null, function () {',
  22297. ' this.$init = function () {',
  22298. ' };',
  22299. ' this.$final = function () {',
  22300. ' };',
  22301. ' this.Run = function (w) {',
  22302. ' var o = null;',
  22303. ' $mod.THelper.Foo.call(this, 1);',
  22304. ' $mod.THelper.Foo.call(this, 1);',
  22305. ' $mod.THelper.Foo.call(this, 2);',
  22306. ' $mod.THelper.Foo.call(this, 1);',
  22307. ' $mod.THelper.Foo.call(this, 1);',
  22308. ' $mod.THelper.Foo.call(this, 3);',
  22309. ' $mod.THelper.Foo.call(this, 1);',
  22310. ' $mod.THelper.Foo.call(this, 1);',
  22311. ' $mod.THelper.Foo.call(this, 4);',
  22312. ' $mod.THelper.Foo.call(o, 5);',
  22313. ' };',
  22314. '});',
  22315. 'rtl.createHelper(this, "THelper", null, function () {',
  22316. ' this.Foo = function (w) {',
  22317. ' var Result = 0;',
  22318. ' this.Run(10);',
  22319. ' this.Run(10);',
  22320. ' this.Run(11);',
  22321. ' $mod.THelper.Foo.call(this, 1);',
  22322. ' $mod.THelper.Foo.call(this, 1);',
  22323. ' $mod.THelper.Foo.call(this, 12);',
  22324. ' $mod.THelper.Foo.call(this, 1);',
  22325. ' $mod.THelper.Foo.call(this, 1);',
  22326. ' $mod.THelper.Foo.call(this, 13);',
  22327. ' $mod.THelper.Foo.call(this, 1);',
  22328. ' $mod.THelper.Foo.call(this, 1);',
  22329. ' $mod.THelper.Foo.call(this, 14);',
  22330. ' return Result;',
  22331. ' };',
  22332. '});',
  22333. 'this.Obj = null;',
  22334. '']),
  22335. LinesToStr([ // $mod.$main
  22336. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22337. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22338. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22339. 'var $with = $mod.Obj;',
  22340. '$mod.THelper.Foo.call($with, 1);',
  22341. '$mod.THelper.Foo.call($with, 1);',
  22342. '$mod.THelper.Foo.call($with, 22);',
  22343. '']));
  22344. end;
  22345. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22346. begin
  22347. StartProgram(false);
  22348. Add([
  22349. 'type',
  22350. ' TObject = class',
  22351. ' procedure Run(w: word = 10);',
  22352. ' end;',
  22353. ' THelper = class helper for TObject',
  22354. ' function Foo(w: word = 1): word;',
  22355. ' end;',
  22356. 'procedure TObject.Run(w: word);',
  22357. ' procedure Sub(Self: TObject);',
  22358. ' begin',
  22359. ' Foo;',
  22360. ' Foo();',
  22361. ' Self.Foo;',
  22362. ' Self.Foo();',
  22363. ' with Self do begin',
  22364. ' Foo;',
  22365. ' Foo();',
  22366. ' end;',
  22367. ' end;',
  22368. 'begin',
  22369. 'end;',
  22370. 'function THelper.foo(w: word): word;',
  22371. ' procedure Sub(Self: TObject);',
  22372. ' begin',
  22373. ' Run;',
  22374. ' Run();',
  22375. ' Foo;',
  22376. ' Foo();',
  22377. ' Self.Foo;',
  22378. ' Self.Foo();',
  22379. ' with Self do begin',
  22380. ' Foo;',
  22381. ' Foo();',
  22382. ' end;',
  22383. ' end;',
  22384. 'begin',
  22385. 'end;',
  22386. 'begin',
  22387. '']);
  22388. ConvertProgram;
  22389. CheckSource('TestClassHelper_Method_Nested_Call',
  22390. LinesToStr([ // statements
  22391. 'rtl.createClass(this, "TObject", null, function () {',
  22392. ' this.$init = function () {',
  22393. ' };',
  22394. ' this.$final = function () {',
  22395. ' };',
  22396. ' this.Run = function (w) {',
  22397. ' var $Self = this;',
  22398. ' function Sub(Self) {',
  22399. ' $mod.THelper.Foo.call($Self, 1);',
  22400. ' $mod.THelper.Foo.call($Self, 1);',
  22401. ' $mod.THelper.Foo.call(Self, 1);',
  22402. ' $mod.THelper.Foo.call(Self, 1);',
  22403. ' $mod.THelper.Foo.call(Self, 1);',
  22404. ' $mod.THelper.Foo.call(Self, 1);',
  22405. ' };',
  22406. ' };',
  22407. '});',
  22408. 'rtl.createHelper(this, "THelper", null, function () {',
  22409. ' this.Foo = function (w) {',
  22410. ' var $Self = this;',
  22411. ' var Result = 0;',
  22412. ' function Sub(Self) {',
  22413. ' $Self.Run(10);',
  22414. ' $Self.Run(10);',
  22415. ' $mod.THelper.Foo.call($Self, 1);',
  22416. ' $mod.THelper.Foo.call($Self, 1);',
  22417. ' $mod.THelper.Foo.call(Self, 1);',
  22418. ' $mod.THelper.Foo.call(Self, 1);',
  22419. ' $mod.THelper.Foo.call(Self, 1);',
  22420. ' $mod.THelper.Foo.call(Self, 1);',
  22421. ' };',
  22422. ' return Result;',
  22423. ' };',
  22424. '});',
  22425. '']),
  22426. LinesToStr([ // $mod.$main
  22427. '']));
  22428. end;
  22429. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  22430. begin
  22431. StartProgram(false);
  22432. Add([
  22433. 'type',
  22434. ' TObject = class',
  22435. ' class procedure Run(w: word = 10);',
  22436. ' end;',
  22437. ' THelper = class helper for TObject',
  22438. ' class function Foo(w: word = 1): word;',
  22439. ' end;',
  22440. 'class procedure TObject.Run(w: word);',
  22441. 'begin',
  22442. ' Foo;',
  22443. ' Foo();',
  22444. ' Self.Foo;',
  22445. ' Self.Foo();',
  22446. ' with Self do begin',
  22447. ' Foo;',
  22448. ' Foo();',
  22449. ' end;',
  22450. 'end;',
  22451. 'class function THelper.foo(w: word): word;',
  22452. 'begin',
  22453. ' Run;',
  22454. ' Run();',
  22455. ' Foo;',
  22456. ' Foo();',
  22457. ' Self.Foo;',
  22458. ' Self.Foo();',
  22459. ' with Self do begin',
  22460. ' Foo;',
  22461. ' Foo();',
  22462. ' end;',
  22463. 'end;',
  22464. 'var',
  22465. ' Obj: TObject;',
  22466. 'begin',
  22467. ' obj.Foo;',
  22468. ' obj.Foo();',
  22469. ' with obj do begin',
  22470. ' Foo;',
  22471. ' Foo();',
  22472. ' end;',
  22473. ' tobject.Foo;',
  22474. ' tobject.Foo();',
  22475. ' with tobject do begin',
  22476. ' Foo;',
  22477. ' Foo();',
  22478. ' end;',
  22479. '']);
  22480. ConvertProgram;
  22481. CheckSource('TestClassHelper_ClassMethod_Call',
  22482. LinesToStr([ // statements
  22483. 'rtl.createClass(this, "TObject", null, function () {',
  22484. ' this.$init = function () {',
  22485. ' };',
  22486. ' this.$final = function () {',
  22487. ' };',
  22488. ' this.Run = function (w) {',
  22489. ' $mod.THelper.Foo.call(this, 1);',
  22490. ' $mod.THelper.Foo.call(this, 1);',
  22491. ' $mod.THelper.Foo.call(this, 1);',
  22492. ' $mod.THelper.Foo.call(this, 1);',
  22493. ' $mod.THelper.Foo.call(this, 1);',
  22494. ' $mod.THelper.Foo.call(this, 1);',
  22495. ' };',
  22496. '});',
  22497. 'rtl.createHelper(this, "THelper", null, function () {',
  22498. ' this.Foo = function (w) {',
  22499. ' var Result = 0;',
  22500. ' this.Run(10);',
  22501. ' this.Run(10);',
  22502. ' $mod.THelper.Foo.call(this, 1);',
  22503. ' $mod.THelper.Foo.call(this, 1);',
  22504. ' $mod.THelper.Foo.call(this, 1);',
  22505. ' $mod.THelper.Foo.call(this, 1);',
  22506. ' $mod.THelper.Foo.call(this, 1);',
  22507. ' $mod.THelper.Foo.call(this, 1);',
  22508. ' return Result;',
  22509. ' };',
  22510. '});',
  22511. 'this.Obj = null;',
  22512. '']),
  22513. LinesToStr([ // $mod.$main
  22514. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22515. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  22516. 'var $with = $mod.Obj;',
  22517. '$mod.THelper.Foo.call($with.$class, 1);',
  22518. '$mod.THelper.Foo.call($with.$class, 1);',
  22519. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22520. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22521. 'var $with1 = $mod.TObject;',
  22522. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22523. '$mod.THelper.Foo.call($mod.TObject, 1);',
  22524. '']));
  22525. end;
  22526. procedure TTestModule.TestClassHelper_ClassOf;
  22527. begin
  22528. StartProgram(false);
  22529. Add([
  22530. 'type',
  22531. ' TObject = class',
  22532. ' end;',
  22533. ' TClass = class of TObject;',
  22534. ' THelper = class helper for TObject',
  22535. ' class function Foo(w: word = 1): word;',
  22536. ' end;',
  22537. 'class function THelper.foo(w: word): word;',
  22538. 'begin',
  22539. 'end;',
  22540. 'var',
  22541. ' c: TClass;',
  22542. 'begin',
  22543. ' c.Foo;',
  22544. ' c.Foo();',
  22545. ' with c do begin',
  22546. ' Foo;',
  22547. ' Foo();',
  22548. ' end;',
  22549. '']);
  22550. ConvertProgram;
  22551. CheckSource('TestClassHelper_ClassOf',
  22552. LinesToStr([ // statements
  22553. 'rtl.createClass(this, "TObject", null, function () {',
  22554. ' this.$init = function () {',
  22555. ' };',
  22556. ' this.$final = function () {',
  22557. ' };',
  22558. '});',
  22559. 'rtl.createHelper(this, "THelper", null, function () {',
  22560. ' this.Foo = function (w) {',
  22561. ' var Result = 0;',
  22562. ' return Result;',
  22563. ' };',
  22564. '});',
  22565. 'this.c = null;',
  22566. '']),
  22567. LinesToStr([ // $mod.$main
  22568. '$mod.THelper.Foo.call($mod.c, 1);',
  22569. '$mod.THelper.Foo.call($mod.c, 1);',
  22570. 'var $with = $mod.c;',
  22571. '$mod.THelper.Foo.call($with, 1);',
  22572. '$mod.THelper.Foo.call($with, 1);',
  22573. '']));
  22574. end;
  22575. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  22576. begin
  22577. StartProgram(false);
  22578. Add([
  22579. '{$mode objfpc}',
  22580. 'type',
  22581. ' TObject = class',
  22582. ' procedure DoIt;',
  22583. ' end;',
  22584. ' THelper = class helper for TObject',
  22585. ' procedure Fly(w: word = 1);',
  22586. ' class procedure Glide(w: word = 1);',
  22587. ' class procedure Run(w: word = 1); static;',
  22588. ' end;',
  22589. ' TFly = procedure(w: word) of object;',
  22590. ' TGlide = TFly;',
  22591. ' TRun = procedure(w: word);',
  22592. 'var',
  22593. ' f: TFly;',
  22594. ' g: TGlide;',
  22595. ' r: TRun;',
  22596. 'procedure TObject.DoIt;',
  22597. 'begin',
  22598. ' f:=@fly;',
  22599. ' g:=@glide;',
  22600. ' r:=@run;',
  22601. ' f:[email protected];',
  22602. ' g:[email protected];',
  22603. ' r:[email protected];',
  22604. ' with self do begin',
  22605. ' f:=@fly;',
  22606. ' g:=@glide;',
  22607. ' r:=@run;',
  22608. ' end;',
  22609. 'end;',
  22610. 'procedure THelper.fly(w: word);',
  22611. 'begin',
  22612. ' f:=@fly;',
  22613. ' g:=@glide;',
  22614. ' r:=@run;',
  22615. 'end;',
  22616. 'class procedure THelper.glide(w: word);',
  22617. 'begin',
  22618. ' g:=@glide;',
  22619. ' r:=@run;',
  22620. 'end;',
  22621. 'class procedure THelper.run(w: word);',
  22622. 'begin',
  22623. ' g:=@glide;',
  22624. ' r:=@run;',
  22625. 'end;',
  22626. 'var',
  22627. ' Obj: TObject;',
  22628. 'begin',
  22629. ' f:[email protected];',
  22630. ' g:[email protected];',
  22631. ' r:[email protected];',
  22632. ' with obj do begin',
  22633. ' f:=@fly;',
  22634. ' g:=@glide;',
  22635. ' r:=@run;',
  22636. ' end;',
  22637. ' g:[email protected];',
  22638. ' r:[email protected];',
  22639. ' with tobject do begin',
  22640. ' g:=@glide;',
  22641. ' r:=@run;',
  22642. ' end;',
  22643. '']);
  22644. ConvertProgram;
  22645. CheckSource('TestClassHelper_MethodRefObjFPC',
  22646. LinesToStr([ // statements
  22647. 'rtl.createClass(this, "TObject", null, function () {',
  22648. ' this.$init = function () {',
  22649. ' };',
  22650. ' this.$final = function () {',
  22651. ' };',
  22652. ' this.DoIt = function () {',
  22653. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22654. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22655. ' $mod.r = $mod.THelper.Run;',
  22656. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22657. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22658. ' $mod.r = $mod.THelper.Run;',
  22659. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22660. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22661. ' $mod.r = $mod.THelper.Run;',
  22662. ' };',
  22663. '});',
  22664. 'rtl.createHelper(this, "THelper", null, function () {',
  22665. ' this.Fly = function (w) {',
  22666. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  22667. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  22668. ' $mod.r = $mod.THelper.Run;',
  22669. ' };',
  22670. ' this.Glide = function (w) {',
  22671. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  22672. ' $mod.r = $mod.THelper.Run;',
  22673. ' };',
  22674. ' this.Run = function (w) {',
  22675. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  22676. ' $mod.r = $mod.THelper.Run;',
  22677. ' };',
  22678. '});',
  22679. 'this.f = null;',
  22680. 'this.g = null;',
  22681. 'this.r = null;',
  22682. 'this.Obj = null;',
  22683. '']),
  22684. LinesToStr([ // $mod.$main
  22685. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  22686. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  22687. '$mod.r = $mod.THelper.Run;',
  22688. 'var $with = $mod.Obj;',
  22689. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  22690. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  22691. '$mod.r = $mod.THelper.Run;',
  22692. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  22693. '$mod.r = $mod.THelper.Run;',
  22694. 'var $with1 = $mod.TObject;',
  22695. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  22696. '$mod.r = $mod.THelper.Run;',
  22697. '']));
  22698. end;
  22699. procedure TTestModule.TestClassHelper_Constructor;
  22700. begin
  22701. StartProgram(false);
  22702. Add([
  22703. 'type',
  22704. ' TObject = class',
  22705. ' constructor Create;',
  22706. ' end;',
  22707. ' TClass = class of TObject;',
  22708. ' THelper = class helper for TObject',
  22709. ' constructor NewHlp(w: word);',
  22710. ' end;',
  22711. 'var',
  22712. ' obj: TObject;',
  22713. ' c: TClass;',
  22714. 'constructor TObject.Create;',
  22715. 'begin',
  22716. ' NewHlp(2);', // normal call
  22717. ' tobject.NewHlp(3);', // new instance
  22718. ' c.newhlp(4);', // new instance
  22719. 'end;',
  22720. 'constructor THelper.NewHlp(w: word);',
  22721. 'begin',
  22722. ' create;', // normal call
  22723. ' tobject.create;', // new instance
  22724. ' NewHlp(2);', // normal call
  22725. ' tobject.NewHlp(3);', // new instance
  22726. ' c.newhlp(4);', // new instance
  22727. 'end;',
  22728. 'begin',
  22729. ' obj.newhlp(2);', // normal call
  22730. ' with Obj do newhlp(12);', // normal call
  22731. ' tobject.newhlp(3);', // new instance
  22732. ' with tobject do newhlp(13);', // new instance
  22733. ' c.newhlp(4);', // new instance
  22734. ' with c do newhlp(14);', // new instance
  22735. '']);
  22736. ConvertProgram;
  22737. CheckSource('TestClassHelper_Constructor',
  22738. LinesToStr([ // statements
  22739. 'rtl.createClass(this, "TObject", null, function () {',
  22740. ' this.$init = function () {',
  22741. ' };',
  22742. ' this.$final = function () {',
  22743. ' };',
  22744. ' this.Create = function () {',
  22745. ' $mod.THelper.NewHlp.call(this, 2);',
  22746. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22747. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  22748. ' return this;',
  22749. ' };',
  22750. '});',
  22751. 'rtl.createHelper(this, "THelper", null, function () {',
  22752. ' this.NewHlp = function (w) {',
  22753. ' this.Create();',
  22754. ' $mod.TObject.$create("Create");',
  22755. ' $mod.THelper.NewHlp.call(this, 2);',
  22756. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22757. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  22758. ' return this;',
  22759. ' };',
  22760. '});',
  22761. 'this.obj = null;',
  22762. 'this.c = null;',
  22763. '']),
  22764. LinesToStr([ // $mod.$main
  22765. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  22766. 'var $with = $mod.obj;',
  22767. '$mod.THelper.NewHlp.call($with, 12);',
  22768. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  22769. 'var $with1 = $mod.TObject;',
  22770. '$with1.$create($mod.THelper.NewHlp, [13]);',
  22771. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  22772. 'var $with2 = $mod.c;',
  22773. '$with2.$create($mod.THelper.NewHlp, [14]);',
  22774. '']));
  22775. end;
  22776. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  22777. begin
  22778. StartProgram(false);
  22779. Add([
  22780. 'type',
  22781. ' TObject = class',
  22782. ' procedure Fly;',
  22783. ' end;',
  22784. ' TObjHelper = class helper for TObject',
  22785. ' procedure Fly;',
  22786. ' end;',
  22787. ' TBird = class',
  22788. ' procedure Fly;',
  22789. ' end;',
  22790. ' TBirdHelper = class helper for TBird',
  22791. ' procedure Fly;',
  22792. ' procedure Walk(w: word);',
  22793. ' end;',
  22794. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  22795. ' procedure Fly;',
  22796. ' procedure Walk(w: word);',
  22797. ' end;',
  22798. 'procedure Tobject.fly;',
  22799. 'begin',
  22800. ' inherited;', // ignore
  22801. 'end;',
  22802. 'procedure Tobjhelper.fly;',
  22803. 'begin',
  22804. ' {@TObject_Fly}inherited;',
  22805. ' inherited {@TObject_Fly}Fly;',
  22806. 'end;',
  22807. 'procedure Tbird.fly;',
  22808. 'begin',
  22809. ' {@TObjHelper_Fly}inherited;',
  22810. ' inherited {@TObjHelper_Fly}Fly;',
  22811. 'end;',
  22812. 'procedure Tbirdhelper.fly;',
  22813. 'begin',
  22814. ' {@TBird_Fly}inherited;',
  22815. ' inherited {@TBird_Fly}Fly;',
  22816. 'end;',
  22817. 'procedure Tbirdhelper.walk(w: word);',
  22818. 'begin',
  22819. 'end;',
  22820. 'procedure teagleHelper.fly;',
  22821. 'begin',
  22822. ' {@TBird_Fly}inherited;',
  22823. ' inherited {@TBird_Fly}Fly;',
  22824. 'end;',
  22825. 'procedure teagleHelper.walk(w: word);',
  22826. 'begin',
  22827. ' {@TBirdHelper_Walk}inherited;',
  22828. ' inherited {@TBirdHelper_Walk}Walk(3);',
  22829. 'end;',
  22830. 'begin',
  22831. '']);
  22832. ConvertProgram;
  22833. CheckSource('TestClassHelper_InheritedObjFPC',
  22834. LinesToStr([ // statements
  22835. 'rtl.createClass(this, "TObject", null, function () {',
  22836. ' this.$init = function () {',
  22837. ' };',
  22838. ' this.$final = function () {',
  22839. ' };',
  22840. ' this.Fly = function () {',
  22841. ' };',
  22842. '});',
  22843. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22844. ' this.Fly = function () {',
  22845. ' $mod.TObject.Fly.call(this);',
  22846. ' $mod.TObject.Fly.call(this);',
  22847. ' };',
  22848. '});',
  22849. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22850. ' this.Fly$1 = function () {',
  22851. ' $mod.TObjHelper.Fly.call(this);',
  22852. ' $mod.TObjHelper.Fly.call(this);',
  22853. ' };',
  22854. '});',
  22855. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22856. ' this.Fly = function () {',
  22857. ' $mod.TBird.Fly$1.call(this);',
  22858. ' $mod.TBird.Fly$1.call(this);',
  22859. ' };',
  22860. ' this.Walk = function (w) {',
  22861. ' };',
  22862. '});',
  22863. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  22864. ' this.Fly$1 = function () {',
  22865. ' $mod.TBird.Fly$1.call(this);',
  22866. ' $mod.TBird.Fly$1.call(this);',
  22867. ' };',
  22868. ' this.Walk$1 = function (w) {',
  22869. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  22870. ' $mod.TBirdHelper.Walk.call(this, 3);',
  22871. ' };',
  22872. '});',
  22873. '']),
  22874. LinesToStr([ // $mod.$main
  22875. '']));
  22876. end;
  22877. procedure TTestModule.TestClassHelper_Property;
  22878. begin
  22879. StartProgram(false);
  22880. Add([
  22881. 'type',
  22882. ' TObject = class',
  22883. ' FSize: word;',
  22884. ' function GetSpeed: word;',
  22885. ' procedure SetSpeed(Value: word);',
  22886. ' end;',
  22887. ' TObjHelper = class helper for TObject',
  22888. ' function GetLeft: word;',
  22889. ' procedure SetLeft(Value: word);',
  22890. ' property Size: word read FSize write FSize;',
  22891. ' property Speed: word read GetSpeed write SetSpeed;',
  22892. ' property Left: word read GetLeft write SetLeft;',
  22893. ' end;',
  22894. ' TBird = class',
  22895. ' property NotRight: word read GetLeft write SetLeft;',
  22896. ' procedure DoIt;',
  22897. ' end;',
  22898. 'var',
  22899. ' b: TBird;',
  22900. 'function Tobject.GetSpeed: word;',
  22901. 'begin',
  22902. ' Size:=Size+11;',
  22903. ' Speed:=Speed+12;',
  22904. ' Result:=Left+13;',
  22905. ' Left:=13;',
  22906. ' Left:=Left+13;',
  22907. ' Self.Size:=Self.Size+21;',
  22908. ' Self.Speed:=Self.Speed+22;',
  22909. ' Self.Left:=Self.Left+23;',
  22910. ' with Self do begin',
  22911. ' Size:=Size+31;',
  22912. ' Speed:=Speed+32;',
  22913. ' Left:=Left+33;',
  22914. ' end;',
  22915. 'end;',
  22916. 'procedure Tobject.SetSpeed(Value: word);',
  22917. 'begin',
  22918. 'end;',
  22919. 'function TObjHelper.GetLeft: word;',
  22920. 'begin',
  22921. ' Size:=Size+11;',
  22922. ' Speed:=Speed+12;',
  22923. ' Left:=Left+13;',
  22924. ' Self.Size:=Self.Size+21;',
  22925. ' Self.Speed:=Self.Speed+22;',
  22926. ' Self.Left:=Self.Left+23;',
  22927. ' with Self do begin',
  22928. ' Size:=Size+31;',
  22929. ' Speed:=Speed+32;',
  22930. ' Left:=Left+33;',
  22931. ' end;',
  22932. 'end;',
  22933. 'procedure TObjHelper.SetLeft(Value: word);',
  22934. 'begin',
  22935. 'end;',
  22936. 'procedure TBird.DoIt;',
  22937. 'begin',
  22938. ' NotRight:=NotRight+11;',
  22939. ' Self.NotRight:=Self.NotRight+21;',
  22940. ' with Self do begin',
  22941. ' NotRight:=NotRight+31;',
  22942. ' end;',
  22943. 'end;',
  22944. 'begin',
  22945. ' b.Size:=b.Size+11;',
  22946. ' b.Speed:=b.Speed+12;',
  22947. ' b.Left:=b.Left+13;',
  22948. ' b.NotRight:=b.NotRight+14;',
  22949. ' with b do begin',
  22950. ' Size:=Size+31;',
  22951. ' Speed:=Speed+32;',
  22952. ' Left:=Left+33;',
  22953. ' NotRight:=NotRight+34;',
  22954. ' end;',
  22955. '']);
  22956. ConvertProgram;
  22957. CheckSource('TestClassHelper_Property',
  22958. LinesToStr([ // statements
  22959. 'rtl.createClass(this, "TObject", null, function () {',
  22960. ' this.$init = function () {',
  22961. ' this.FSize = 0;',
  22962. ' };',
  22963. ' this.$final = function () {',
  22964. ' };',
  22965. ' this.GetSpeed = function () {',
  22966. ' var Result = 0;',
  22967. ' this.FSize = this.FSize + 11;',
  22968. ' this.SetSpeed(this.GetSpeed() + 12);',
  22969. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  22970. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  22971. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22972. ' this.FSize = this.FSize + 21;',
  22973. ' this.SetSpeed(this.GetSpeed() + 22);',
  22974. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22975. ' this.FSize = this.FSize + 31;',
  22976. ' this.SetSpeed(this.GetSpeed() + 32);',
  22977. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22978. ' return Result;',
  22979. ' };',
  22980. ' this.SetSpeed = function (Value) {',
  22981. ' };',
  22982. '});',
  22983. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22984. ' this.GetLeft = function () {',
  22985. ' var Result = 0;',
  22986. ' this.FSize = this.FSize + 11;',
  22987. ' this.SetSpeed(this.GetSpeed() + 12);',
  22988. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22989. ' this.FSize = this.FSize + 21;',
  22990. ' this.SetSpeed(this.GetSpeed() + 22);',
  22991. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22992. ' this.FSize = this.FSize + 31;',
  22993. ' this.SetSpeed(this.GetSpeed() + 32);',
  22994. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22995. ' return Result;',
  22996. ' };',
  22997. ' this.SetLeft = function (Value) {',
  22998. ' };',
  22999. '});',
  23000. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23001. ' this.DoIt = function () {',
  23002. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23003. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23004. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23005. ' };',
  23006. '});',
  23007. 'this.b = null;',
  23008. '']),
  23009. LinesToStr([ // $mod.$main
  23010. '$mod.b.FSize = $mod.b.FSize + 11;',
  23011. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23012. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23013. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23014. 'var $with = $mod.b;',
  23015. '$with.FSize = $with.FSize + 31;',
  23016. '$with.SetSpeed($with.GetSpeed() + 32);',
  23017. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23018. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23019. '']));
  23020. end;
  23021. procedure TTestModule.TestClassHelper_Property_Array;
  23022. begin
  23023. StartProgram(false);
  23024. Add([
  23025. 'type',
  23026. ' TObject = class',
  23027. ' function GetSpeed(Index: boolean): word;',
  23028. ' procedure SetSpeed(Index: boolean; Value: word);',
  23029. ' end;',
  23030. ' TObjHelper = class helper for TObject',
  23031. ' function GetSize(Index: boolean): word;',
  23032. ' procedure SetSize(Index: boolean; Value: word);',
  23033. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23034. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23035. ' end;',
  23036. ' TBird = class',
  23037. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23038. ' procedure DoIt;',
  23039. ' end;',
  23040. 'var',
  23041. ' b: TBird;',
  23042. 'function Tobject.GetSpeed(Index: boolean): word;',
  23043. 'begin',
  23044. ' Result:=Size[false];',
  23045. ' Size[true]:=Size[false]+11;',
  23046. ' Speed[true]:=Speed[false]+12;',
  23047. ' Self.Size[true]:=Self.Size[false]+21;',
  23048. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23049. ' with Self do begin',
  23050. ' Size[true]:=Size[false]+31;',
  23051. ' Speed[true]:=Speed[false]+32;',
  23052. ' end;',
  23053. 'end;',
  23054. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23055. 'begin',
  23056. 'end;',
  23057. 'function TObjHelper.GetSize(Index: boolean): word;',
  23058. 'begin',
  23059. ' Size[true]:=Size[false]+11;',
  23060. ' Speed[true]:=Speed[false]+12;',
  23061. ' Self.Size[true]:=Self.Size[false]+21;',
  23062. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23063. ' with Self do begin',
  23064. ' Size[true]:=Size[false]+31;',
  23065. ' Speed[true]:=Speed[false]+32;',
  23066. ' end;',
  23067. 'end;',
  23068. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23069. 'begin',
  23070. 'end;',
  23071. 'procedure TBird.DoIt;',
  23072. 'begin',
  23073. ' Items[true]:=Items[false]+11;',
  23074. ' Self.Items[true]:=Self.Items[false]+21;',
  23075. ' with Self do Items[true]:=Items[false]+31;',
  23076. 'end;',
  23077. 'begin',
  23078. ' b.Size[true]:=b.Size[false]+11;',
  23079. ' b.Speed[true]:=b.Speed[false]+12;',
  23080. ' b.Items[true]:=b.Items[false]+13;',
  23081. ' with b do begin',
  23082. ' Size[true]:=Size[false]+21;',
  23083. ' Speed[true]:=Speed[false]+22;',
  23084. ' Items[true]:=Items[false]+23;',
  23085. ' end;',
  23086. '']);
  23087. ConvertProgram;
  23088. CheckSource('TestClassHelper_Property_Array',
  23089. LinesToStr([ // statements
  23090. 'rtl.createClass(this, "TObject", null, function () {',
  23091. ' this.$init = function () {',
  23092. ' };',
  23093. ' this.$final = function () {',
  23094. ' };',
  23095. ' this.GetSpeed = function (Index) {',
  23096. ' var Result = 0;',
  23097. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23098. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23099. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23100. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23101. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23102. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23103. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23104. ' return Result;',
  23105. ' };',
  23106. ' this.SetSpeed = function (Index, Value) {',
  23107. ' };',
  23108. '});',
  23109. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23110. ' this.GetSize = function (Index) {',
  23111. ' var Result = 0;',
  23112. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23113. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23114. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23115. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23116. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23117. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23118. ' return Result;',
  23119. ' };',
  23120. ' this.SetSize = function (Index, Value) {',
  23121. ' };',
  23122. '});',
  23123. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23124. ' this.DoIt = function () {',
  23125. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23126. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23127. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23128. ' };',
  23129. '});',
  23130. 'this.b = null;',
  23131. '']),
  23132. LinesToStr([ // $mod.$main
  23133. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23134. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23135. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23136. 'var $with = $mod.b;',
  23137. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23138. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23139. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23140. '']));
  23141. end;
  23142. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23143. begin
  23144. StartProgram(false);
  23145. Add([
  23146. 'type',
  23147. ' TObject = class',
  23148. ' function GetSpeed(Index: boolean): word;',
  23149. ' procedure SetSpeed(Index: boolean; Value: word);',
  23150. ' end;',
  23151. ' TObjHelper = class helper for TObject',
  23152. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23153. ' end;',
  23154. ' TBird = class',
  23155. ' end;',
  23156. ' TBirdHelper = class helper for TBird',
  23157. ' function GetSize(Index: word): boolean;',
  23158. ' procedure SetSize(Index: word; Value: boolean);',
  23159. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23160. ' end;',
  23161. 'function Tobject.GetSpeed(Index: boolean): word;',
  23162. 'begin',
  23163. ' Self[true]:=Self[false]+1;',
  23164. 'end;',
  23165. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23166. 'begin',
  23167. 'end;',
  23168. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23169. 'begin',
  23170. ' Self[1]:=not Self[2];',
  23171. 'end;',
  23172. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23173. 'begin',
  23174. 'end;',
  23175. 'var',
  23176. ' o: TObject;',
  23177. ' b: TBird;',
  23178. 'begin',
  23179. ' o[true]:=o[false]+1;',
  23180. ' b[3]:=not b[4];',
  23181. '']);
  23182. ConvertProgram;
  23183. CheckSource('TestClassHelper_Property_Array_Default',
  23184. LinesToStr([ // statements
  23185. 'rtl.createClass(this, "TObject", null, function () {',
  23186. ' this.$init = function () {',
  23187. ' };',
  23188. ' this.$final = function () {',
  23189. ' };',
  23190. ' this.GetSpeed = function (Index) {',
  23191. ' var Result = 0;',
  23192. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23193. ' return Result;',
  23194. ' };',
  23195. ' this.SetSpeed = function (Index, Value) {',
  23196. ' };',
  23197. '});',
  23198. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23199. '});',
  23200. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23201. '});',
  23202. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23203. ' this.GetSize = function (Index) {',
  23204. ' var Result = false;',
  23205. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23206. ' return Result;',
  23207. ' };',
  23208. ' this.SetSize = function (Index, Value) {',
  23209. ' };',
  23210. '});',
  23211. 'this.o = null;',
  23212. 'this.b = null;',
  23213. '']),
  23214. LinesToStr([ // $mod.$main
  23215. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23216. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23217. '']));
  23218. end;
  23219. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23220. begin
  23221. StartProgram(false);
  23222. Add([
  23223. 'type',
  23224. ' TObject = class',
  23225. ' end;',
  23226. ' TObjHelper = class helper for TObject',
  23227. ' function GetItems(Index: word): TObject;',
  23228. ' procedure SetItems(Index: word; Value: TObject);',
  23229. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23230. ' end;',
  23231. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23232. 'begin',
  23233. ' Self[1][2]:=Self[3][4];',
  23234. 'end;',
  23235. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23236. 'begin',
  23237. 'end;',
  23238. 'var',
  23239. ' o: TObject;',
  23240. 'begin',
  23241. ' o[1][2]:=o[3][4];',
  23242. '']);
  23243. ConvertProgram;
  23244. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23245. LinesToStr([ // statements
  23246. 'rtl.createClass(this, "TObject", null, function () {',
  23247. ' this.$init = function () {',
  23248. ' };',
  23249. ' this.$final = function () {',
  23250. ' };',
  23251. '});',
  23252. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23253. ' this.GetItems = function (Index) {',
  23254. ' var Result = null;',
  23255. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23256. ' return Result;',
  23257. ' };',
  23258. ' this.SetItems = function (Index, Value) {',
  23259. ' };',
  23260. '});',
  23261. 'this.o = null;',
  23262. '']),
  23263. LinesToStr([ // $mod.$main
  23264. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23265. '']));
  23266. end;
  23267. procedure TTestModule.TestClassHelper_ClassProperty;
  23268. begin
  23269. StartProgram(false);
  23270. Add([
  23271. 'type',
  23272. ' TObject = class',
  23273. ' class var FSize: word;',
  23274. ' class function GetSpeed: word;',
  23275. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23276. ' end;',
  23277. ' TObjHelper = class helper for TObject',
  23278. ' class function GetLeft: word;',
  23279. ' class procedure SetLeft(Value: word);',
  23280. ' class property Size: word read FSize write FSize;',
  23281. ' class property Speed: word read GetSpeed write SetSpeed;',
  23282. ' class property Left: word read GetLeft write SetLeft;',
  23283. ' end;',
  23284. ' TBird = class',
  23285. ' class property NotRight: word read GetLeft write SetLeft;',
  23286. ' class procedure DoIt;',
  23287. ' end;',
  23288. ' TBirdClass = class of TBird;',
  23289. 'class function Tobject.GetSpeed: word;',
  23290. 'begin',
  23291. ' Size:=Size+11;',
  23292. ' Speed:=Speed+12;',
  23293. ' Left:=Left+13;',
  23294. ' Self.Size:=Self.Size+21;',
  23295. ' Self.Speed:=Self.Speed+22;',
  23296. ' Self.Left:=Self.Left+23;',
  23297. ' with Self do begin',
  23298. ' Size:=Size+31;',
  23299. ' Speed:=Speed+32;',
  23300. ' Left:=Left+33;',
  23301. ' end;',
  23302. 'end;',
  23303. 'class function TObjHelper.GetLeft: word;',
  23304. 'begin',
  23305. ' Size:=Size+11;',
  23306. ' Speed:=Speed+12;',
  23307. ' Left:=Left+13;',
  23308. ' Self.Size:=Self.Size+21;',
  23309. ' Self.Speed:=Self.Speed+22;',
  23310. ' Self.Left:=Self.Left+23;',
  23311. ' with Self do begin',
  23312. ' Size:=Size+31;',
  23313. ' Speed:=Speed+32;',
  23314. ' Left:=Left+33;',
  23315. ' end;',
  23316. 'end;',
  23317. 'class procedure TObjHelper.SetLeft(Value: word);',
  23318. 'begin',
  23319. 'end;',
  23320. 'class procedure TBird.DoIt;',
  23321. 'begin',
  23322. ' NotRight:=NotRight+11;',
  23323. ' Self.NotRight:=Self.NotRight+21;',
  23324. ' with Self do NotRight:=NotRight+31;',
  23325. 'end;',
  23326. 'var',
  23327. ' b: TBird;',
  23328. ' c: TBirdClass;',
  23329. 'begin',
  23330. ' b.Size:=b.Size+11;',
  23331. ' b.Speed:=b.Speed+12;',
  23332. ' b.Left:=b.Left+13;',
  23333. ' b.NotRight:=b.NotRight+14;',
  23334. ' with b do begin',
  23335. ' Size:=Size+31;',
  23336. ' Speed:=Speed+32;',
  23337. ' Left:=Left+33;',
  23338. ' NotRight:=NotRight+34;',
  23339. ' end;',
  23340. ' c.Size:=c.Size+11;',
  23341. ' c.Speed:=c.Speed+12;',
  23342. ' c.Left:=c.Left+13;',
  23343. ' c.NotRight:=c.NotRight+14;',
  23344. ' with c do begin',
  23345. ' Size:=Size+31;',
  23346. ' Speed:=Speed+32;',
  23347. ' Left:=Left+33;',
  23348. ' NotRight:=NotRight+34;',
  23349. ' end;',
  23350. ' tbird.Size:=tbird.Size+11;',
  23351. ' tbird.Speed:=tbird.Speed+12;',
  23352. ' tbird.Left:=tbird.Left+13;',
  23353. ' tbird.NotRight:=tbird.NotRight+14;',
  23354. ' with tbird do begin',
  23355. ' Size:=Size+31;',
  23356. ' Speed:=Speed+32;',
  23357. ' Left:=Left+33;',
  23358. ' NotRight:=NotRight+34;',
  23359. ' end;',
  23360. '']);
  23361. ConvertProgram;
  23362. CheckSource('TestClassHelper_ClassProperty',
  23363. LinesToStr([ // statements
  23364. 'rtl.createClass(this, "TObject", null, function () {',
  23365. ' this.FSize = 0;',
  23366. ' this.$init = function () {',
  23367. ' };',
  23368. ' this.$final = function () {',
  23369. ' };',
  23370. ' this.GetSpeed = function () {',
  23371. ' var Result = 0;',
  23372. ' $mod.TObject.FSize = this.FSize + 11;',
  23373. ' this.SetSpeed(this.GetSpeed() + 12);',
  23374. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23375. ' $mod.TObject.FSize = this.FSize + 21;',
  23376. ' this.SetSpeed(this.GetSpeed() + 22);',
  23377. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23378. ' $mod.TObject.FSize = this.FSize + 31;',
  23379. ' this.SetSpeed(this.GetSpeed() + 32);',
  23380. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23381. ' return Result;',
  23382. ' };',
  23383. '});',
  23384. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23385. ' this.GetLeft = function () {',
  23386. ' var Result = 0;',
  23387. ' $mod.TObject.FSize = this.FSize + 11;',
  23388. ' this.SetSpeed(this.GetSpeed() + 12);',
  23389. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23390. ' $mod.TObject.FSize = this.FSize + 21;',
  23391. ' this.SetSpeed(this.GetSpeed() + 22);',
  23392. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23393. ' $mod.TObject.FSize = this.FSize + 31;',
  23394. ' this.SetSpeed(this.GetSpeed() + 32);',
  23395. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23396. ' return Result;',
  23397. ' };',
  23398. ' this.SetLeft = function (Value) {',
  23399. ' };',
  23400. '});',
  23401. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23402. ' this.DoIt = function () {',
  23403. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23404. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23405. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23406. ' };',
  23407. '});',
  23408. 'this.b = null;',
  23409. 'this.c = null;',
  23410. '']),
  23411. LinesToStr([ // $mod.$main
  23412. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23413. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23414. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23415. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23416. 'var $with = $mod.b;',
  23417. '$mod.TObject.FSize = $with.FSize + 31;',
  23418. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23419. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23420. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23421. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23422. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23423. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23424. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  23425. 'var $with1 = $mod.c;',
  23426. '$mod.TObject.FSize = $with1.FSize + 31;',
  23427. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23428. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  23429. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  23430. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  23431. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  23432. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  23433. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  23434. 'var $with2 = $mod.TBird;',
  23435. '$mod.TObject.FSize = $with2.FSize + 31;',
  23436. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23437. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  23438. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  23439. '']));
  23440. end;
  23441. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  23442. begin
  23443. StartProgram(false);
  23444. Add([
  23445. 'type',
  23446. ' TObject = class',
  23447. ' class function GetSpeed: word; static;',
  23448. ' class procedure SetSpeed(Value: word); static;',
  23449. ' end;',
  23450. ' TObjHelper = class helper for TObject',
  23451. ' class function GetLeft: word; static;',
  23452. ' class procedure SetLeft(Value: word); static;',
  23453. ' class property Speed: word read GetSpeed write SetSpeed;',
  23454. ' class property Left: word read GetLeft write SetLeft;',
  23455. ' end;',
  23456. ' TBird = class',
  23457. ' class property NotRight: word read GetLeft write SetLeft;',
  23458. ' class procedure DoIt; static;',
  23459. ' class procedure DoSome;',
  23460. ' end;',
  23461. ' TBirdClass = class of TBird;',
  23462. 'class function Tobject.GetSpeed: word;',
  23463. 'begin',
  23464. ' Speed:=Speed+12;',
  23465. ' Left:=Left+13;',
  23466. 'end;',
  23467. 'class procedure TObject.SetSpeed(Value: word);',
  23468. 'begin',
  23469. 'end;',
  23470. 'class function TObjHelper.GetLeft: word;',
  23471. 'begin',
  23472. ' Speed:=Speed+12;',
  23473. ' Left:=Left+13;',
  23474. 'end;',
  23475. 'class procedure TObjHelper.SetLeft(Value: word);',
  23476. 'begin',
  23477. 'end;',
  23478. 'class procedure TBird.DoIt;',
  23479. 'begin',
  23480. ' NotRight:=NotRight+11;',
  23481. 'end;',
  23482. 'class procedure TBird.DoSome;',
  23483. 'begin',
  23484. ' Speed:=Speed+12;',
  23485. ' Left:=Left+13;',
  23486. ' Self.Speed:=Self.Speed+22;',
  23487. ' Self.Left:=Self.Left+23;',
  23488. ' with Self do begin',
  23489. ' Speed:=Speed+32;',
  23490. ' Left:=Left+33;',
  23491. ' end;',
  23492. ' NotRight:=NotRight+11;',
  23493. ' Self.NotRight:=Self.NotRight+21;',
  23494. ' with Self do NotRight:=NotRight+31;',
  23495. 'end;',
  23496. 'var',
  23497. ' b: TBird;',
  23498. ' c: TBirdClass;',
  23499. 'begin',
  23500. ' b.Speed:=b.Speed+12;',
  23501. ' b.Left:=b.Left+13;',
  23502. ' b.NotRight:=b.NotRight+14;',
  23503. ' with b do begin',
  23504. ' Speed:=Speed+32;',
  23505. ' Left:=Left+33;',
  23506. ' NotRight:=NotRight+34;',
  23507. ' end;',
  23508. ' c.Speed:=c.Speed+12;',
  23509. ' c.Left:=c.Left+13;',
  23510. ' c.NotRight:=c.NotRight+14;',
  23511. ' with c do begin',
  23512. ' Speed:=Speed+32;',
  23513. ' Left:=Left+33;',
  23514. ' NotRight:=NotRight+34;',
  23515. ' end;',
  23516. ' tbird.Speed:=tbird.Speed+12;',
  23517. ' tbird.Left:=tbird.Left+13;',
  23518. ' tbird.NotRight:=tbird.NotRight+14;',
  23519. ' with tbird do begin',
  23520. ' Speed:=Speed+32;',
  23521. ' Left:=Left+33;',
  23522. ' NotRight:=NotRight+34;',
  23523. ' end;',
  23524. '']);
  23525. ConvertProgram;
  23526. CheckSource('TestClassHelper_ClassPropertyStatic',
  23527. LinesToStr([ // statements
  23528. 'rtl.createClass(this, "TObject", null, function () {',
  23529. ' this.$init = function () {',
  23530. ' };',
  23531. ' this.$final = function () {',
  23532. ' };',
  23533. ' this.GetSpeed = function () {',
  23534. ' var Result = 0;',
  23535. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23536. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23537. ' return Result;',
  23538. ' };',
  23539. ' this.SetSpeed = function (Value) {',
  23540. ' };',
  23541. '});',
  23542. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23543. ' this.GetLeft = function () {',
  23544. ' var Result = 0;',
  23545. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23546. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23547. ' return Result;',
  23548. ' };',
  23549. ' this.SetLeft = function (Value) {',
  23550. ' };',
  23551. '});',
  23552. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23553. ' this.DoIt = function () {',
  23554. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23555. ' };',
  23556. ' this.DoSome = function () {',
  23557. ' this.SetSpeed(this.GetSpeed() + 12);',
  23558. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23559. ' this.SetSpeed(this.GetSpeed() + 22);',
  23560. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  23561. ' this.SetSpeed(this.GetSpeed() + 32);',
  23562. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23563. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  23564. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  23565. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  23566. ' };',
  23567. '});',
  23568. 'this.b = null;',
  23569. 'this.c = null;',
  23570. '']),
  23571. LinesToStr([ // $mod.$main
  23572. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23573. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23574. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23575. 'var $with = $mod.b;',
  23576. '$with.SetSpeed($with.GetSpeed() + 32);',
  23577. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23578. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23579. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23580. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23581. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23582. 'var $with1 = $mod.c;',
  23583. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  23584. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23585. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23586. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  23587. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  23588. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  23589. 'var $with2 = $mod.TBird;',
  23590. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  23591. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  23592. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  23593. '']));
  23594. end;
  23595. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  23596. begin
  23597. StartProgram(false);
  23598. Add([
  23599. 'type',
  23600. ' TObject = class',
  23601. ' class function GetSpeed(Index: boolean): word;',
  23602. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  23603. ' end;',
  23604. ' TObjHelper = class helper for TObject',
  23605. ' class function GetSize(Index: boolean): word;',
  23606. ' class procedure SetSize(Index: boolean; Value: word);',
  23607. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  23608. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23609. ' end;',
  23610. ' TBird = class',
  23611. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  23612. ' class procedure DoIt;',
  23613. ' end;',
  23614. ' TBirdClass = class of TBird;',
  23615. 'class function Tobject.GetSpeed(Index: boolean): word;',
  23616. 'begin',
  23617. ' Size[true]:=Size[false]+11;',
  23618. ' Speed[true]:=Speed[false]+12;',
  23619. ' Self.Size[true]:=Self.Size[false]+21;',
  23620. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23621. ' with Self do begin',
  23622. ' Size[true]:=Size[false]+31;',
  23623. ' Speed[true]:=Speed[false]+32;',
  23624. ' end;',
  23625. 'end;',
  23626. 'class function TObjHelper.GetSize(Index: boolean): word;',
  23627. 'begin',
  23628. ' Size[true]:=Size[false]+11;',
  23629. ' Speed[true]:=Speed[false]+12;',
  23630. ' Self.Size[true]:=Self.Size[false]+21;',
  23631. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23632. ' with Self do begin',
  23633. ' Size[true]:=Size[false]+31;',
  23634. ' Speed[true]:=Speed[false]+32;',
  23635. ' end;',
  23636. 'end;',
  23637. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23638. 'begin',
  23639. 'end;',
  23640. 'class procedure TBird.DoIt;',
  23641. 'begin',
  23642. ' Items[true]:=Items[false]+11;',
  23643. ' Self.Items[true]:=Self.Items[false]+21;',
  23644. ' with Self do Items[true]:=Items[false]+31;',
  23645. 'end;',
  23646. 'var',
  23647. ' b: TBird;',
  23648. ' c: TBirdClass;',
  23649. 'begin',
  23650. ' b.Size[true]:=b.Size[false]+11;',
  23651. ' b.Speed[true]:=b.Speed[false]+12;',
  23652. ' b.Items[true]:=b.Items[false]+13;',
  23653. ' with b do begin',
  23654. ' Size[true]:=Size[false]+21;',
  23655. ' Speed[true]:=Speed[false]+22;',
  23656. ' Items[true]:=Items[false]+23;',
  23657. ' end;',
  23658. ' c.Size[true]:=c.Size[false]+11;',
  23659. ' c.Speed[true]:=c.Speed[false]+12;',
  23660. ' c.Items[true]:=c.Items[false]+13;',
  23661. ' with c do begin',
  23662. ' Size[true]:=Size[false]+21;',
  23663. ' Speed[true]:=Speed[false]+22;',
  23664. ' Items[true]:=Items[false]+23;',
  23665. ' end;',
  23666. ' TBird.Size[true]:=TBird.Size[false]+11;',
  23667. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  23668. ' TBird.Items[true]:=TBird.Items[false]+13;',
  23669. ' with TBird do begin',
  23670. ' Size[true]:=Size[false]+21;',
  23671. ' Speed[true]:=Speed[false]+22;',
  23672. ' Items[true]:=Items[false]+23;',
  23673. ' end;',
  23674. '']);
  23675. ConvertProgram;
  23676. CheckSource('TestClassHelper_ClassProperty_Array',
  23677. LinesToStr([ // statements
  23678. 'rtl.createClass(this, "TObject", null, function () {',
  23679. ' this.$init = function () {',
  23680. ' };',
  23681. ' this.$final = function () {',
  23682. ' };',
  23683. ' this.GetSpeed = function (Index) {',
  23684. ' var Result = 0;',
  23685. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23686. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23687. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23688. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23689. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23690. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23691. ' return Result;',
  23692. ' };',
  23693. '});',
  23694. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23695. ' this.GetSize = function (Index) {',
  23696. ' var Result = 0;',
  23697. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23698. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23699. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23700. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23701. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23702. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23703. ' return Result;',
  23704. ' };',
  23705. ' this.SetSize = function (Index, Value) {',
  23706. ' };',
  23707. '});',
  23708. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23709. ' this.DoIt = function () {',
  23710. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23711. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23712. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23713. ' };',
  23714. '});',
  23715. 'this.b = null;',
  23716. 'this.c = null;',
  23717. '']),
  23718. LinesToStr([ // $mod.$main
  23719. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  23720. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  23721. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  23722. 'var $with = $mod.b;',
  23723. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  23724. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  23725. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  23726. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  23727. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  23728. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  23729. 'var $with1 = $mod.c;',
  23730. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  23731. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  23732. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  23733. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  23734. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  23735. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  23736. 'var $with2 = $mod.TBird;',
  23737. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  23738. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  23739. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  23740. '']));
  23741. end;
  23742. procedure TTestModule.TestClassHelper_ForIn;
  23743. begin
  23744. StartProgram(false);
  23745. Add([
  23746. 'type',
  23747. ' TObject = class end;',
  23748. ' TItem = TObject;',
  23749. ' TEnumerator = class',
  23750. ' FCurrent: TItem;',
  23751. ' property Current: TItem read FCurrent;',
  23752. ' function MoveNext: boolean;',
  23753. ' end;',
  23754. ' TBird = class',
  23755. ' end;',
  23756. ' TBirdHelper = class helper for TBird',
  23757. ' function GetEnumerator: TEnumerator;',
  23758. ' end;',
  23759. 'function TEnumerator.MoveNext: boolean;',
  23760. 'begin',
  23761. 'end;',
  23762. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  23763. 'begin',
  23764. 'end;',
  23765. 'var',
  23766. ' b: TBird;',
  23767. ' i, i2: TItem;',
  23768. 'begin',
  23769. ' for i in b do i2:=i;']);
  23770. ConvertProgram;
  23771. CheckSource('TestClassHelper_ForIn',
  23772. LinesToStr([ // statements
  23773. 'rtl.createClass(this, "TObject", null, function () {',
  23774. ' this.$init = function () {',
  23775. ' };',
  23776. ' this.$final = function () {',
  23777. ' };',
  23778. '});',
  23779. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  23780. ' this.$init = function () {',
  23781. ' $mod.TObject.$init.call(this);',
  23782. ' this.FCurrent = null;',
  23783. ' };',
  23784. ' this.$final = function () {',
  23785. ' this.FCurrent = undefined;',
  23786. ' $mod.TObject.$final.call(this);',
  23787. ' };',
  23788. ' this.MoveNext = function () {',
  23789. ' var Result = false;',
  23790. ' return Result;',
  23791. ' };',
  23792. '});',
  23793. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23794. '});',
  23795. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23796. ' this.GetEnumerator = function () {',
  23797. ' var Result = null;',
  23798. ' return Result;',
  23799. ' };',
  23800. '});',
  23801. 'this.b = null;',
  23802. 'this.i = null;',
  23803. 'this.i2 = null;'
  23804. ]),
  23805. LinesToStr([ // $mod.$main
  23806. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  23807. 'try {',
  23808. ' while ($in.MoveNext()){',
  23809. ' $mod.i = $in.FCurrent;',
  23810. ' $mod.i2 = $mod.i;',
  23811. ' }',
  23812. '} finally {',
  23813. ' $in = rtl.freeLoc($in)',
  23814. '};',
  23815. '']));
  23816. end;
  23817. procedure TTestModule.TestClassHelper_PassProperty;
  23818. begin
  23819. StartProgram(false);
  23820. Add([
  23821. 'type',
  23822. ' TObject = class',
  23823. ' FField: TObject;',
  23824. ' property Field: TObject read FField write FField;',
  23825. ' end;',
  23826. ' THelper = class helper for TObject',
  23827. ' procedure Fly;',
  23828. ' class procedure Run;',
  23829. ' class procedure Jump; static;',
  23830. ' end;',
  23831. 'procedure THelper.Fly;',
  23832. 'begin',
  23833. ' Field.Fly;',
  23834. ' Field.Run;',
  23835. ' Field.Jump;',
  23836. ' with Field do begin',
  23837. ' Fly;',
  23838. ' Run;',
  23839. ' Jump;',
  23840. ' end;',
  23841. 'end;',
  23842. 'class procedure THelper.Run;',
  23843. 'begin',
  23844. 'end;',
  23845. 'class procedure THelper.Jump;',
  23846. 'begin',
  23847. 'end;',
  23848. 'var',
  23849. ' b: TObject;',
  23850. 'begin',
  23851. ' b.Field.Fly;',
  23852. ' b.Field.Run;',
  23853. ' b.Field.Jump;',
  23854. ' with b do begin',
  23855. ' Field.Run;',
  23856. ' Field.Fly;',
  23857. ' Field.Jump;',
  23858. ' end;',
  23859. ' with b.Field do begin',
  23860. ' Run;',
  23861. ' Fly;',
  23862. ' Jump;',
  23863. ' end;',
  23864. '']);
  23865. ConvertProgram;
  23866. CheckSource('TestClassHelper_PassProperty',
  23867. LinesToStr([ // statements
  23868. 'rtl.createClass(this, "TObject", null, function () {',
  23869. ' this.$init = function () {',
  23870. ' this.FField = null;',
  23871. ' };',
  23872. ' this.$final = function () {',
  23873. ' this.FField = undefined;',
  23874. ' };',
  23875. '});',
  23876. 'rtl.createHelper(this, "THelper", null, function () {',
  23877. ' this.Fly = function () {',
  23878. ' $mod.THelper.Fly.call(this.FField);',
  23879. ' $mod.THelper.Run.call(this.FField.$class);',
  23880. ' $mod.THelper.Jump();',
  23881. ' var $with = this.FField;',
  23882. ' $mod.THelper.Fly.call($with);',
  23883. ' $mod.THelper.Run.call($with.$class);',
  23884. ' $mod.THelper.Jump();',
  23885. ' };',
  23886. ' this.Run = function () {',
  23887. ' };',
  23888. ' this.Jump = function () {',
  23889. ' };',
  23890. '});',
  23891. 'this.b = null;',
  23892. '']),
  23893. LinesToStr([ // $mod.$main
  23894. '$mod.THelper.Fly.call($mod.b.FField);',
  23895. '$mod.THelper.Run.call($mod.b.FField.$class);',
  23896. '$mod.THelper.Jump();',
  23897. 'var $with = $mod.b;',
  23898. '$mod.THelper.Run.call($with.FField.$class);',
  23899. '$mod.THelper.Fly.call($with.FField);',
  23900. '$mod.THelper.Jump();',
  23901. 'var $with1 = $mod.b.FField;',
  23902. '$mod.THelper.Run.call($with1.$class);',
  23903. '$mod.THelper.Fly.call($with1);',
  23904. '$mod.THelper.Jump();',
  23905. '']));
  23906. end;
  23907. procedure TTestModule.TestExtClassHelper_ClassVar;
  23908. begin
  23909. StartProgram(false);
  23910. Add([
  23911. '{$modeswitch externalclass}',
  23912. 'type',
  23913. ' TExtA = class external name ''ExtObj''',
  23914. ' end;',
  23915. ' THelper = class helper for TExtA',
  23916. ' const',
  23917. ' One = 1;',
  23918. ' Two: word = 2;',
  23919. ' class var',
  23920. ' Glob: word;',
  23921. ' function Foo(w: word): word;',
  23922. ' class function Bar(w: word): word; static;',
  23923. ' end;',
  23924. 'function THelper.foo(w: word): word;',
  23925. 'begin',
  23926. ' Result:=w;',
  23927. ' Two:=One+w;',
  23928. ' Glob:=Glob;',
  23929. ' Result:=Self.Glob;',
  23930. ' Self.Glob:=Self.Glob;',
  23931. ' with Self do Glob:=Glob;',
  23932. 'end;',
  23933. 'class function THelper.bar(w: word): word;',
  23934. 'begin',
  23935. ' Result:=w;',
  23936. ' Two:=One;',
  23937. ' Glob:=Glob;',
  23938. 'end;',
  23939. 'var o: TExtA;',
  23940. 'begin',
  23941. ' texta.two:=texta.one;',
  23942. ' texta.Glob:=texta.Glob;',
  23943. ' with texta do begin',
  23944. ' two:=one;',
  23945. ' Glob:=Glob;',
  23946. ' end;',
  23947. ' o.two:=o.one;',
  23948. ' o.Glob:=o.Glob;',
  23949. ' with o do begin',
  23950. ' two:=one;',
  23951. ' Glob:=Glob;',
  23952. ' end;',
  23953. '']);
  23954. ConvertProgram;
  23955. CheckSource('TestExtClassHelper_ClassVar',
  23956. LinesToStr([ // statements
  23957. 'rtl.createHelper(this, "THelper", null, function () {',
  23958. ' this.One = 1;',
  23959. ' this.Two = 2;',
  23960. ' this.Glob = 0;',
  23961. ' this.Foo = function (w) {',
  23962. ' var Result = 0;',
  23963. ' Result = w;',
  23964. ' $mod.THelper.Two = 1 + w;',
  23965. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23966. ' Result = $mod.THelper.Glob;',
  23967. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23968. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23969. ' return Result;',
  23970. ' };',
  23971. ' this.Bar = function (w) {',
  23972. ' var Result = 0;',
  23973. ' Result = w;',
  23974. ' $mod.THelper.Two = 1;',
  23975. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23976. ' return Result;',
  23977. ' };',
  23978. '});',
  23979. 'this.o = null;',
  23980. '']),
  23981. LinesToStr([ // $mod.$main
  23982. '$mod.THelper.Two = 1;',
  23983. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23984. '$mod.THelper.Two = 1;',
  23985. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23986. '$mod.THelper.Two = 1;',
  23987. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23988. 'var $with = $mod.o;',
  23989. '$mod.THelper.Two = 1;',
  23990. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23991. '']));
  23992. end;
  23993. procedure TTestModule.TestExtClassHelper_Method_Call;
  23994. begin
  23995. StartProgram(false);
  23996. Add([
  23997. '{$modeswitch externalclass}',
  23998. 'type',
  23999. ' TFly = function(w: word): word of object;',
  24000. ' TExtA = class external name ''ExtObj''',
  24001. ' procedure Run(w: word = 10);',
  24002. ' end;',
  24003. ' THelper = class helper for TExtA',
  24004. ' function Foo(w: word = 1): word;',
  24005. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24006. ' end;',
  24007. 'var p: TFly;',
  24008. 'function THelper.foo(w: word): word;',
  24009. 'begin',
  24010. ' Run;',
  24011. ' Run();',
  24012. ' Run(11);',
  24013. ' Foo;',
  24014. ' Foo();',
  24015. ' Foo(12);',
  24016. ' Self.Foo;',
  24017. ' Self.Foo();',
  24018. ' Self.Foo(13);',
  24019. ' Fly;',
  24020. ' Fly();',
  24021. ' with Self do begin',
  24022. ' Foo;',
  24023. ' Foo();',
  24024. ' Foo(14);',
  24025. ' Fly;',
  24026. ' Fly();',
  24027. ' end;',
  24028. ' p:=@Fly;',
  24029. 'end;',
  24030. 'var Obj: TExtA;',
  24031. 'begin',
  24032. ' obj.Foo;',
  24033. ' obj.Foo();',
  24034. ' obj.Foo(21);',
  24035. ' obj.Fly;',
  24036. ' obj.Fly();',
  24037. ' with obj do begin',
  24038. ' Foo;',
  24039. ' Foo();',
  24040. ' Foo(22);',
  24041. ' Fly;',
  24042. ' Fly();',
  24043. ' end;',
  24044. ' p:[email protected];',
  24045. '']);
  24046. ConvertProgram;
  24047. CheckSource('TestExtClassHelper_Method_Call',
  24048. LinesToStr([ // statements
  24049. 'rtl.createHelper(this, "THelper", null, function () {',
  24050. ' this.Foo = function (w) {',
  24051. ' var Result = 0;',
  24052. ' this.Run(10);',
  24053. ' this.Run(10);',
  24054. ' this.Run(11);',
  24055. ' $mod.THelper.Foo.call(this, 1);',
  24056. ' $mod.THelper.Foo.call(this, 1);',
  24057. ' $mod.THelper.Foo.call(this, 12);',
  24058. ' $mod.THelper.Foo.call(this, 1);',
  24059. ' $mod.THelper.Foo.call(this, 1);',
  24060. ' $mod.THelper.Foo.call(this, 13);',
  24061. ' this.Fly(2);',
  24062. ' this.Fly(2);',
  24063. ' $mod.THelper.Foo.call(this, 1);',
  24064. ' $mod.THelper.Foo.call(this, 1);',
  24065. ' $mod.THelper.Foo.call(this, 14);',
  24066. ' this.Fly(2);',
  24067. ' this.Fly(2);',
  24068. ' $mod.p = rtl.createCallback(this, "Fly");',
  24069. ' return Result;',
  24070. ' };',
  24071. '});',
  24072. 'this.p = null;',
  24073. 'this.Obj = null;',
  24074. '']),
  24075. LinesToStr([ // $mod.$main
  24076. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24077. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24078. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24079. '$mod.Obj.Fly(2);',
  24080. '$mod.Obj.Fly(2);',
  24081. 'var $with = $mod.Obj;',
  24082. '$mod.THelper.Foo.call($with, 1);',
  24083. '$mod.THelper.Foo.call($with, 1);',
  24084. '$mod.THelper.Foo.call($with, 22);',
  24085. '$with.Fly(2);',
  24086. '$with.Fly(2);',
  24087. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24088. '']));
  24089. end;
  24090. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24091. begin
  24092. StartProgram(false);
  24093. Add([
  24094. '{$modeswitch externalclass}',
  24095. 'type',
  24096. ' TExtA = class external name ''ExtObj''',
  24097. ' procedure Run(w: word = 10);',
  24098. ' end;',
  24099. ' THelper = class helper for TExtA',
  24100. ' class procedure Fly;',
  24101. ' end;',
  24102. 'class procedure THelper.Fly;',
  24103. 'begin end;',
  24104. 'begin',
  24105. '']);
  24106. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24107. nHelperClassMethodForExtClassMustBeStatic);
  24108. ConvertProgram;
  24109. end;
  24110. procedure TTestModule.TestRecordHelper_ClassVar;
  24111. begin
  24112. StartProgram(false);
  24113. Add([
  24114. 'type',
  24115. ' TRec = record',
  24116. ' end;',
  24117. ' THelper = record helper for TRec',
  24118. ' const',
  24119. ' One = 1;',
  24120. ' Two: word = 2;',
  24121. ' class var',
  24122. ' Glob: word;',
  24123. ' function Foo(w: word): word;',
  24124. ' class function Bar(w: word): word; static;',
  24125. ' end;',
  24126. 'function THelper.foo(w: word): word;',
  24127. 'begin',
  24128. ' Result:=w;',
  24129. ' Two:=One+w;',
  24130. ' Glob:=Glob;',
  24131. ' Result:=Self.Glob;',
  24132. ' Self.Glob:=Self.Glob;',
  24133. ' with Self do Glob:=Glob;',
  24134. ' Self:=Self;',
  24135. 'end;',
  24136. 'class function THelper.bar(w: word): word;',
  24137. 'begin',
  24138. ' Result:=w;',
  24139. ' Two:=One;',
  24140. ' Glob:=Glob;',
  24141. 'end;',
  24142. 'var r: TRec;',
  24143. 'begin',
  24144. ' trec.two:=trec.one;',
  24145. ' trec.Glob:=trec.Glob;',
  24146. ' with trec do begin',
  24147. ' two:=one;',
  24148. ' Glob:=Glob;',
  24149. ' end;',
  24150. ' r.two:=r.one;',
  24151. ' r.Glob:=r.Glob;',
  24152. ' with r do begin',
  24153. ' two:=one;',
  24154. ' Glob:=Glob;',
  24155. ' end;',
  24156. '']);
  24157. ConvertProgram;
  24158. CheckSource('TestRecordHelper_ClassVar',
  24159. LinesToStr([ // statements
  24160. 'rtl.recNewT(this, "TRec", function () {',
  24161. ' this.$eq = function (b) {',
  24162. ' return true;',
  24163. ' };',
  24164. ' this.$assign = function (s) {',
  24165. ' return this;',
  24166. ' };',
  24167. '});',
  24168. 'rtl.createHelper(this, "THelper", null, function () {',
  24169. ' this.One = 1;',
  24170. ' this.Two = 2;',
  24171. ' this.Glob = 0;',
  24172. ' this.Foo = function (w) {',
  24173. ' var Result = 0;',
  24174. ' Result = w;',
  24175. ' $mod.THelper.Two = 1 + w;',
  24176. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24177. ' Result = $mod.THelper.Glob;',
  24178. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24179. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24180. ' this.$assign(this);',
  24181. ' return Result;',
  24182. ' };',
  24183. ' this.Bar = function (w) {',
  24184. ' var Result = 0;',
  24185. ' Result = w;',
  24186. ' $mod.THelper.Two = 1;',
  24187. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24188. ' return Result;',
  24189. ' };',
  24190. '});',
  24191. 'this.r = this.TRec.$new();',
  24192. '']),
  24193. LinesToStr([ // $mod.$main
  24194. '$mod.THelper.Two = 1;',
  24195. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24196. 'var $with = $mod.TRec;',
  24197. '$mod.THelper.Two = 1;',
  24198. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24199. '$mod.THelper.Two = 1;',
  24200. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24201. 'var $with1 = $mod.r;',
  24202. '$mod.THelper.Two = 1;',
  24203. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24204. '']));
  24205. end;
  24206. procedure TTestModule.TestRecordHelper_Method_Call;
  24207. begin
  24208. StartProgram(false);
  24209. Add([
  24210. '{$modeswitch AdvancedRecords}',
  24211. 'type',
  24212. ' TRec = record',
  24213. ' procedure Run(w: word = 10);',
  24214. ' end;',
  24215. ' THelper = record helper for TRec',
  24216. ' function Foo(w: word = 1): word;',
  24217. ' end;',
  24218. 'procedure TRec.Run(w: word);',
  24219. 'begin',
  24220. ' Foo;',
  24221. ' Foo();',
  24222. ' Foo(2);',
  24223. ' Self.Foo;',
  24224. ' Self.Foo();',
  24225. ' Self.Foo(3);',
  24226. ' with Self do begin',
  24227. ' Foo;',
  24228. ' Foo();',
  24229. ' Foo(4);',
  24230. ' end;',
  24231. 'end;',
  24232. 'function THelper.foo(w: word): word;',
  24233. 'begin',
  24234. ' Run;',
  24235. ' Run();',
  24236. ' Run(11);',
  24237. ' Foo;',
  24238. ' Foo();',
  24239. ' Foo(12);',
  24240. ' Self.Foo;',
  24241. ' Self.Foo();',
  24242. ' Self.Foo(13);',
  24243. ' with Self do begin',
  24244. ' Foo;',
  24245. ' Foo();',
  24246. ' Foo(14);',
  24247. ' end;',
  24248. 'end;',
  24249. 'var Rec: TRec;',
  24250. 'begin',
  24251. ' Rec.Foo;',
  24252. ' Rec.Foo();',
  24253. ' Rec.Foo(21);',
  24254. ' with Rec do begin',
  24255. ' Foo;',
  24256. ' Foo();',
  24257. ' Foo(22);',
  24258. ' end;',
  24259. '']);
  24260. ConvertProgram;
  24261. CheckSource('TestRecordHelper_Method_Call',
  24262. LinesToStr([ // statements
  24263. 'rtl.recNewT(this, "TRec", function () {',
  24264. ' this.$eq = function (b) {',
  24265. ' return true;',
  24266. ' };',
  24267. ' this.$assign = function (s) {',
  24268. ' return this;',
  24269. ' };',
  24270. ' this.Run = function (w) {',
  24271. ' $mod.THelper.Foo.call(this, 1);',
  24272. ' $mod.THelper.Foo.call(this, 1);',
  24273. ' $mod.THelper.Foo.call(this, 2);',
  24274. ' $mod.THelper.Foo.call(this, 1);',
  24275. ' $mod.THelper.Foo.call(this, 1);',
  24276. ' $mod.THelper.Foo.call(this, 3);',
  24277. ' $mod.THelper.Foo.call(this, 1);',
  24278. ' $mod.THelper.Foo.call(this, 1);',
  24279. ' $mod.THelper.Foo.call(this, 4);',
  24280. ' };',
  24281. '});',
  24282. 'rtl.createHelper(this, "THelper", null, function () {',
  24283. ' this.Foo = function (w) {',
  24284. ' var Result = 0;',
  24285. ' this.Run(10);',
  24286. ' this.Run(10);',
  24287. ' this.Run(11);',
  24288. ' $mod.THelper.Foo.call(this, 1);',
  24289. ' $mod.THelper.Foo.call(this, 1);',
  24290. ' $mod.THelper.Foo.call(this, 12);',
  24291. ' $mod.THelper.Foo.call(this, 1);',
  24292. ' $mod.THelper.Foo.call(this, 1);',
  24293. ' $mod.THelper.Foo.call(this, 13);',
  24294. ' $mod.THelper.Foo.call(this, 1);',
  24295. ' $mod.THelper.Foo.call(this, 1);',
  24296. ' $mod.THelper.Foo.call(this, 14);',
  24297. ' return Result;',
  24298. ' };',
  24299. '});',
  24300. 'this.Rec = this.TRec.$new();',
  24301. '']),
  24302. LinesToStr([ // $mod.$main
  24303. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24304. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24305. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24306. 'var $with = $mod.Rec;',
  24307. '$mod.THelper.Foo.call($with, 1);',
  24308. '$mod.THelper.Foo.call($with, 1);',
  24309. '$mod.THelper.Foo.call($with, 22);',
  24310. '']));
  24311. end;
  24312. procedure TTestModule.TestRecordHelper_Constructor;
  24313. begin
  24314. StartProgram(false);
  24315. Add([
  24316. '{$modeswitch AdvancedRecords}',
  24317. 'type',
  24318. ' TRec = record',
  24319. ' constructor Create(w: word);',
  24320. ' end;',
  24321. ' THelper = record helper for TRec',
  24322. ' constructor NewHlp(w: word);',
  24323. ' end;',
  24324. 'var',
  24325. ' Rec: TRec;',
  24326. 'constructor TRec.Create(w: word);',
  24327. 'begin',
  24328. ' NewHlp(2);', // normal call
  24329. ' trec.NewHlp(3);', // new instance
  24330. 'end;',
  24331. 'constructor THelper.NewHlp(w: word);',
  24332. 'begin',
  24333. ' create(2);', // normal call
  24334. ' trec.create(3);', // new instance
  24335. ' NewHlp(4);', // normal call
  24336. ' trec.NewHlp(5);', // new instance
  24337. 'end;',
  24338. 'begin',
  24339. ' rec.newhlp(2);', // normal call
  24340. ' with rec do newhlp(12);', // normal call
  24341. ' trec.newhlp(3);', // new instance
  24342. ' with trec do newhlp(13);', // new instance
  24343. '']);
  24344. ConvertProgram;
  24345. CheckSource('TestRecordHelper_Constructor',
  24346. LinesToStr([ // statements
  24347. 'rtl.recNewT(this, "TRec", function () {',
  24348. ' this.$eq = function (b) {',
  24349. ' return true;',
  24350. ' };',
  24351. ' this.$assign = function (s) {',
  24352. ' return this;',
  24353. ' };',
  24354. ' this.Create = function (w) {',
  24355. ' $mod.THelper.NewHlp.call(this, 2);',
  24356. ' $mod.THelper.$new("NewHlp", [3]);',
  24357. ' return this;',
  24358. ' };',
  24359. '});',
  24360. 'rtl.createHelper(this, "THelper", null, function () {',
  24361. ' this.NewHlp = function (w) {',
  24362. ' this.Create(2);',
  24363. ' $mod.TRec.$new().Create(3);',
  24364. ' $mod.THelper.NewHlp.call(this, 4);',
  24365. ' $mod.THelper.$new("NewHlp", [5]);',
  24366. ' return this;',
  24367. ' };',
  24368. ' this.$new = function (fn, args) {',
  24369. ' return this[fn].apply($mod.TRec.$new(), args);',
  24370. ' };',
  24371. '});',
  24372. 'this.Rec = this.TRec.$new();',
  24373. '']),
  24374. LinesToStr([ // $mod.$main
  24375. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24376. 'var $with = $mod.Rec;',
  24377. '$mod.THelper.NewHlp.call($with, 12);',
  24378. '$mod.THelper.$new("NewHlp", [3]);',
  24379. 'var $with1 = $mod.TRec;',
  24380. '$mod.THelper.$new("NewHlp", [13]);',
  24381. '']));
  24382. end;
  24383. procedure TTestModule.TestTypeHelper_ClassVar;
  24384. begin
  24385. StartProgram(false);
  24386. Add([
  24387. '{$modeswitch typehelpers}',
  24388. 'type',
  24389. ' THelper = type helper for byte',
  24390. ' const',
  24391. ' One = 1;',
  24392. ' Two: word = 2;',
  24393. ' class var',
  24394. ' Glob: word;',
  24395. ' function Foo(w: word): word;',
  24396. ' class function Bar(w: word): word; static;',
  24397. ' end;',
  24398. 'function THelper.foo(w: word): word;',
  24399. 'begin',
  24400. ' Result:=w;',
  24401. ' Two:=One+w;',
  24402. ' Glob:=Glob;',
  24403. ' Result:=Self.Glob;',
  24404. ' Self.Glob:=Self.Glob;',
  24405. ' with Self do Glob:=Glob;',
  24406. 'end;',
  24407. 'class function THelper.bar(w: word): word;',
  24408. 'begin',
  24409. ' Result:=w;',
  24410. ' Two:=One;',
  24411. ' Glob:=Glob;',
  24412. 'end;',
  24413. 'var b: byte;',
  24414. 'begin',
  24415. ' byte.two:=byte.one;',
  24416. ' byte.Glob:=byte.Glob;',
  24417. ' with byte do begin',
  24418. ' two:=one;',
  24419. ' Glob:=Glob;',
  24420. ' end;',
  24421. ' b.two:=b.one;',
  24422. ' b.Glob:=b.Glob;',
  24423. ' with b do begin',
  24424. ' two:=one;',
  24425. ' Glob:=Glob;',
  24426. ' end;',
  24427. '']);
  24428. ConvertProgram;
  24429. CheckSource('TestTypeHelper_ClassVar',
  24430. LinesToStr([ // statements
  24431. 'rtl.createHelper(this, "THelper", null, function () {',
  24432. ' this.One = 1;',
  24433. ' this.Two = 2;',
  24434. ' this.Glob = 0;',
  24435. ' this.Foo = function (w) {',
  24436. ' var Result = 0;',
  24437. ' Result = w;',
  24438. ' $mod.THelper.Two = 1 + w;',
  24439. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24440. ' Result = $mod.THelper.Glob;',
  24441. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24442. ' var $with = this.get();',
  24443. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24444. ' return Result;',
  24445. ' };',
  24446. ' this.Bar = function (w) {',
  24447. ' var Result = 0;',
  24448. ' Result = w;',
  24449. ' $mod.THelper.Two = 1;',
  24450. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24451. ' return Result;',
  24452. ' };',
  24453. '});',
  24454. 'this.b = 0;',
  24455. '']),
  24456. LinesToStr([ // $mod.$main
  24457. '$mod.THelper.Two = 1;',
  24458. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24459. '$mod.THelper.Two = 1;',
  24460. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24461. '$mod.THelper.Two = 1;',
  24462. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24463. 'var $with = $mod.b;',
  24464. '$mod.THelper.Two = 1;',
  24465. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24466. '']));
  24467. end;
  24468. procedure TTestModule.TestTypeHelper_PassResultElement;
  24469. begin
  24470. StartProgram(false);
  24471. Add([
  24472. '{$modeswitch typehelpers}',
  24473. 'type',
  24474. ' THelper = type helper for word',
  24475. ' procedure DoIt(e: byte = 123);',
  24476. ' class procedure DoSome(e: byte = 456); static;',
  24477. ' end;',
  24478. 'procedure THelper.DoIt(e: byte);',
  24479. 'begin',
  24480. 'end;',
  24481. 'class procedure THelper.DoSome(e: byte);',
  24482. 'begin',
  24483. 'end;',
  24484. 'function Foo(w: word): word;',
  24485. 'begin',
  24486. ' Result.DoIt;',
  24487. ' Result.DoIt();',
  24488. ' Result.DoSome;',
  24489. ' Result.DoSome();',
  24490. ' with Result do begin',
  24491. ' DoIt;',
  24492. ' DoIt();',
  24493. ' DoSome;',
  24494. ' DoSome();',
  24495. ' end;',
  24496. 'end;',
  24497. 'begin',
  24498. '']);
  24499. ConvertProgram;
  24500. CheckSource('TestTypeHelper_PassResultElement',
  24501. LinesToStr([ // statements
  24502. 'rtl.createHelper(this, "THelper", null, function () {',
  24503. ' this.DoIt = function (e) {',
  24504. ' };',
  24505. ' this.DoSome = function (e) {',
  24506. ' };',
  24507. '});',
  24508. 'this.Foo = function (w) {',
  24509. ' var Result = 0;',
  24510. ' $mod.THelper.DoIt.call({',
  24511. ' get: function () {',
  24512. ' return Result;',
  24513. ' },',
  24514. ' set: function (v) {',
  24515. ' Result = v;',
  24516. ' }',
  24517. ' }, 123);',
  24518. ' $mod.THelper.DoIt.call({',
  24519. ' get: function () {',
  24520. ' return Result;',
  24521. ' },',
  24522. ' set: function (v) {',
  24523. ' Result = v;',
  24524. ' }',
  24525. ' }, 123);',
  24526. ' $mod.THelper.DoSome(456);',
  24527. ' $mod.THelper.DoSome(456);',
  24528. ' $mod.THelper.DoIt.call({',
  24529. ' get: function () {',
  24530. ' return Result;',
  24531. ' },',
  24532. ' set: function (v) {',
  24533. ' Result = v;',
  24534. ' }',
  24535. ' }, 123);',
  24536. ' $mod.THelper.DoIt.call({',
  24537. ' get: function () {',
  24538. ' return Result;',
  24539. ' },',
  24540. ' set: function (v) {',
  24541. ' Result = v;',
  24542. ' }',
  24543. ' }, 123);',
  24544. ' $mod.THelper.DoSome(456);',
  24545. ' $mod.THelper.DoSome(456);',
  24546. ' return Result;',
  24547. '};',
  24548. '']),
  24549. LinesToStr([ // $mod.$main
  24550. '']));
  24551. end;
  24552. procedure TTestModule.TestTypeHelper_PassArgs;
  24553. begin
  24554. StartProgram(false);
  24555. Add([
  24556. '{$modeswitch typehelpers}',
  24557. 'type',
  24558. ' THelper = type helper for word',
  24559. ' procedure DoIt(e: byte = 123);',
  24560. ' end;',
  24561. 'procedure THelper.DoIt(e: byte);',
  24562. 'begin',
  24563. 'end;',
  24564. 'procedure FooDefault(a: word);',
  24565. 'begin',
  24566. ' a.DoIt;',
  24567. ' with a do DoIt;',
  24568. 'end;',
  24569. 'procedure FooConst(const a: word);',
  24570. 'begin',
  24571. ' a.DoIt;',
  24572. ' with a do DoIt;',
  24573. 'end;',
  24574. 'procedure FooVar(var a: word);',
  24575. 'begin',
  24576. ' a.DoIt;',
  24577. ' with a do DoIt;',
  24578. 'end;',
  24579. 'begin',
  24580. '']);
  24581. ConvertProgram;
  24582. CheckSource('TestTypeHelper_PassArgs',
  24583. LinesToStr([ // statements
  24584. 'rtl.createHelper(this, "THelper", null, function () {',
  24585. ' this.DoIt = function (e) {',
  24586. ' };',
  24587. '});',
  24588. 'this.FooDefault = function (a) {',
  24589. ' $mod.THelper.DoIt.call({',
  24590. ' get: function () {',
  24591. ' return a;',
  24592. ' },',
  24593. ' set: function (v) {',
  24594. ' a = v;',
  24595. ' }',
  24596. ' }, 123);',
  24597. ' $mod.THelper.DoIt.call({',
  24598. ' get: function () {',
  24599. ' return a;',
  24600. ' },',
  24601. ' set: function (v) {',
  24602. ' a = v;',
  24603. ' }',
  24604. ' }, 123);',
  24605. '};',
  24606. 'this.FooConst = function (a) {',
  24607. ' $mod.THelper.DoIt.call({',
  24608. ' get: function () {',
  24609. ' return a;',
  24610. ' },',
  24611. ' set: function (v) {',
  24612. ' rtl.raiseE("EPropReadOnly");',
  24613. ' }',
  24614. ' }, 123);',
  24615. ' $mod.THelper.DoIt.call({',
  24616. ' get: function () {',
  24617. ' return a;',
  24618. ' },',
  24619. ' set: function () {',
  24620. ' rtl.raiseE("EPropReadOnly");',
  24621. ' }',
  24622. ' }, 123);',
  24623. '};',
  24624. 'this.FooVar = function (a) {',
  24625. ' $mod.THelper.DoIt.call(a, 123);',
  24626. ' var $with = a.get();',
  24627. ' $mod.THelper.DoIt.call(a, 123);',
  24628. '};',
  24629. '']),
  24630. LinesToStr([ // $mod.$main
  24631. '']));
  24632. end;
  24633. procedure TTestModule.TestTypeHelper_PassVarConst;
  24634. begin
  24635. StartProgram(false);
  24636. Add([
  24637. '{$modeswitch typehelpers}',
  24638. 'type',
  24639. ' THelper = type helper for word',
  24640. ' procedure DoIt(e: byte = 123);',
  24641. ' end;',
  24642. 'procedure THelper.DoIt(e: byte);',
  24643. 'begin',
  24644. 'end;',
  24645. 'var a: word;',
  24646. 'const c: word = 2;',
  24647. '{$writeableconst off}',
  24648. 'const r: word = 3;',
  24649. 'begin',
  24650. ' a.DoIt;',
  24651. ' with a do DoIt;',
  24652. ' c.DoIt;',
  24653. ' with c do DoIt;',
  24654. ' r.DoIt;',
  24655. ' with r do DoIt;',
  24656. '']);
  24657. ConvertProgram;
  24658. CheckSource('TestTypeHelper_PassVarConst',
  24659. LinesToStr([ // statements
  24660. 'rtl.createHelper(this, "THelper", null, function () {',
  24661. ' this.DoIt = function (e) {',
  24662. ' };',
  24663. '});',
  24664. 'this.a = 0;',
  24665. 'this.c = 2;',
  24666. 'this.r = 3;',
  24667. '']),
  24668. LinesToStr([ // $mod.$main
  24669. '$mod.THelper.DoIt.call({',
  24670. ' p: $mod,',
  24671. ' get: function () {',
  24672. ' return this.p.a;',
  24673. ' },',
  24674. ' set: function (v) {',
  24675. ' this.p.a = v;',
  24676. ' }',
  24677. '}, 123);',
  24678. 'var $with = $mod.a;',
  24679. '$mod.THelper.DoIt.call({',
  24680. ' get: function () {',
  24681. ' return $with;',
  24682. ' },',
  24683. ' set: function (v) {',
  24684. ' $with = v;',
  24685. ' }',
  24686. '}, 123);',
  24687. '$mod.THelper.DoIt.call({',
  24688. ' p: $mod,',
  24689. ' get: function () {',
  24690. ' return this.p.c;',
  24691. ' },',
  24692. ' set: function (v) {',
  24693. ' this.p.c = v;',
  24694. ' }',
  24695. '}, 123);',
  24696. 'var $with1 = $mod.c;',
  24697. '$mod.THelper.DoIt.call({',
  24698. ' get: function () {',
  24699. ' return $with1;',
  24700. ' },',
  24701. ' set: function (v) {',
  24702. ' $with1 = v;',
  24703. ' }',
  24704. '}, 123);',
  24705. '$mod.THelper.DoIt.call({',
  24706. ' get: function () {',
  24707. ' return 3;',
  24708. ' },',
  24709. ' set: function (v) {',
  24710. ' rtl.raiseE("EPropReadOnly");',
  24711. ' }',
  24712. '}, 123);',
  24713. 'var $with2 = 3;',
  24714. ' $mod.THelper.DoIt.call({',
  24715. ' get: function () {',
  24716. ' return $with2;',
  24717. ' },',
  24718. ' set: function () {',
  24719. ' rtl.raiseE("EPropReadOnly");',
  24720. ' }',
  24721. ' }, 123);',
  24722. '']));
  24723. end;
  24724. procedure TTestModule.TestTypeHelper_PassFuncResult;
  24725. begin
  24726. StartProgram(false);
  24727. Add([
  24728. '{$modeswitch typehelpers}',
  24729. 'type',
  24730. ' THelper = type helper for word',
  24731. ' procedure DoIt(e: byte = 123);',
  24732. ' end;',
  24733. 'procedure THelper.DoIt(e: byte);',
  24734. 'begin',
  24735. 'end;',
  24736. 'function Foo(b: byte = 1): word;',
  24737. 'begin',
  24738. 'end;',
  24739. 'begin',
  24740. ' Foo.DoIt;',
  24741. ' Foo().DoIt;',
  24742. ' with Foo do DoIt;',
  24743. ' with Foo() do DoIt;',
  24744. '']);
  24745. ConvertProgram;
  24746. CheckSource('TestTypeHelper_PassFuncResult',
  24747. LinesToStr([ // statements
  24748. 'rtl.createHelper(this, "THelper", null, function () {',
  24749. ' this.DoIt = function (e) {',
  24750. ' };',
  24751. '});',
  24752. 'this.Foo = function (b) {',
  24753. ' var Result = 0;',
  24754. ' return Result;',
  24755. '};',
  24756. '']),
  24757. LinesToStr([ // $mod.$main
  24758. '$mod.THelper.DoIt.call({',
  24759. ' a: $mod.Foo(1),',
  24760. ' get: function () {',
  24761. ' return this.a;',
  24762. ' },',
  24763. ' set: function (v) {',
  24764. ' this.a = v;',
  24765. ' }',
  24766. '}, 123);',
  24767. '$mod.THelper.DoIt.call({',
  24768. ' a: $mod.Foo(1),',
  24769. ' get: function () {',
  24770. ' return this.a;',
  24771. ' },',
  24772. ' set: function (v) {',
  24773. ' this.a = v;',
  24774. ' }',
  24775. '}, 123);',
  24776. 'var $with = $mod.Foo(1);',
  24777. '$mod.THelper.DoIt.call({',
  24778. ' get: function () {',
  24779. ' return $with;',
  24780. ' },',
  24781. ' set: function (v) {',
  24782. ' $with = v;',
  24783. ' }',
  24784. '}, 123);',
  24785. 'var $with1 = $mod.Foo(1);',
  24786. '$mod.THelper.DoIt.call({',
  24787. ' get: function () {',
  24788. ' return $with1;',
  24789. ' },',
  24790. ' set: function (v) {',
  24791. ' $with1 = v;',
  24792. ' }',
  24793. '}, 123);',
  24794. '']));
  24795. end;
  24796. procedure TTestModule.TestTypeHelper_PassPropertyField;
  24797. begin
  24798. StartProgram(false);
  24799. Add([
  24800. '{$modeswitch typehelpers}',
  24801. 'type',
  24802. ' TObject = class',
  24803. ' FField: word;',
  24804. ' procedure SetField(Value: word);',
  24805. ' property Field: word read FField write SetField;',
  24806. ' end;',
  24807. ' THelper = type helper for word',
  24808. ' procedure Fly;',
  24809. ' class procedure Run; static;',
  24810. ' end;',
  24811. 'procedure TObject.SetField(Value: word);',
  24812. 'begin',
  24813. ' Field.Fly;',
  24814. ' Field.Run;',
  24815. ' Self.Field.Fly;',
  24816. ' Self.Field.Run;',
  24817. ' with Self do begin',
  24818. ' Field.Fly;',
  24819. ' Field.Run;',
  24820. ' end;',
  24821. ' with Self.Field do begin',
  24822. ' Fly;',
  24823. ' Run;',
  24824. ' end;',
  24825. 'end;',
  24826. 'procedure THelper.Fly;',
  24827. 'begin',
  24828. 'end;',
  24829. 'class procedure THelper.Run;',
  24830. 'begin',
  24831. 'end;',
  24832. 'var',
  24833. ' o: TObject;',
  24834. 'begin',
  24835. ' o.Field.Fly;',
  24836. ' o.Field.Run;',
  24837. ' with o do begin',
  24838. ' Field.Fly;',
  24839. ' Field.Run;',
  24840. ' end;',
  24841. ' with o.Field do begin',
  24842. ' Fly;',
  24843. ' Run;',
  24844. ' end;',
  24845. '']);
  24846. ConvertProgram;
  24847. CheckSource('TestTypeHelper_PassPropertyField',
  24848. LinesToStr([ // statements
  24849. 'rtl.createClass(this, "TObject", null, function () {',
  24850. ' this.$init = function () {',
  24851. ' this.FField = 0;',
  24852. ' };',
  24853. ' this.$final = function () {',
  24854. ' };',
  24855. ' this.SetField = function (Value) {',
  24856. ' $mod.THelper.Fly.call({',
  24857. ' p: this,',
  24858. ' get: function () {',
  24859. ' return this.p.FField;',
  24860. ' },',
  24861. ' set: function (v) {',
  24862. ' this.p.FField = v;',
  24863. ' }',
  24864. ' });',
  24865. ' $mod.THelper.Run();',
  24866. ' $mod.THelper.Fly.call({',
  24867. ' p: this,',
  24868. ' get: function () {',
  24869. ' return this.p.FField;',
  24870. ' },',
  24871. ' set: function (v) {',
  24872. ' this.p.FField = v;',
  24873. ' }',
  24874. ' });',
  24875. ' $mod.THelper.Run();',
  24876. ' $mod.THelper.Fly.call({',
  24877. ' p: this,',
  24878. ' get: function () {',
  24879. ' return this.p.FField;',
  24880. ' },',
  24881. ' set: function (v) {',
  24882. ' this.p.FField = v;',
  24883. ' }',
  24884. ' });',
  24885. ' $mod.THelper.Run();',
  24886. ' var $with = this.FField;',
  24887. ' $mod.THelper.Fly.call({',
  24888. ' get: function () {',
  24889. ' return $with;',
  24890. ' },',
  24891. ' set: function (v) {',
  24892. ' $with = v;',
  24893. ' }',
  24894. ' });',
  24895. ' $mod.THelper.Run();',
  24896. ' };',
  24897. '});',
  24898. 'rtl.createHelper(this, "THelper", null, function () {',
  24899. ' this.Fly = function () {',
  24900. ' };',
  24901. ' this.Run = function () {',
  24902. ' };',
  24903. '});',
  24904. 'this.o = null;',
  24905. '']),
  24906. LinesToStr([ // $mod.$main
  24907. '$mod.THelper.Fly.call({',
  24908. ' p: $mod.o,',
  24909. ' get: function () {',
  24910. ' return this.p.FField;',
  24911. ' },',
  24912. ' set: function (v) {',
  24913. ' this.p.FField = v;',
  24914. ' }',
  24915. '});',
  24916. '$mod.THelper.Run();',
  24917. 'var $with = $mod.o;',
  24918. '$mod.THelper.Fly.call({',
  24919. ' p: $with,',
  24920. ' get: function () {',
  24921. ' return this.p.FField;',
  24922. ' },',
  24923. ' set: function (v) {',
  24924. ' this.p.FField = v;',
  24925. ' }',
  24926. '});',
  24927. '$mod.THelper.Run();',
  24928. 'var $with1 = $mod.o.FField;',
  24929. '$mod.THelper.Fly.call({',
  24930. ' get: function () {',
  24931. ' return $with1;',
  24932. ' },',
  24933. ' set: function (v) {',
  24934. ' $with1 = v;',
  24935. ' }',
  24936. '});',
  24937. '$mod.THelper.Run();',
  24938. '']));
  24939. end;
  24940. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  24941. begin
  24942. StartProgram(false);
  24943. Add([
  24944. '{$modeswitch typehelpers}',
  24945. 'type',
  24946. ' TObject = class',
  24947. ' FField: word;',
  24948. ' function GetField: word;',
  24949. ' property Field: word read GetField write FField;',
  24950. ' end;',
  24951. ' THelper = type helper for word',
  24952. ' procedure Fly;',
  24953. ' class procedure Run; static;',
  24954. ' end;',
  24955. 'function TObject.GetField: word;',
  24956. 'begin',
  24957. ' Field.Fly;',
  24958. ' Field.Run;',
  24959. ' Self.Field.Fly;',
  24960. ' Self.Field.Run;',
  24961. ' with Self do begin',
  24962. ' Field.Fly;',
  24963. ' Field.Run;',
  24964. ' end;',
  24965. ' with Self.Field do begin',
  24966. ' Fly;',
  24967. ' Run;',
  24968. ' end;',
  24969. 'end;',
  24970. 'procedure THelper.Fly;',
  24971. 'begin',
  24972. 'end;',
  24973. 'class procedure THelper.Run;',
  24974. 'begin',
  24975. 'end;',
  24976. 'var',
  24977. ' o: TObject;',
  24978. 'begin',
  24979. ' o.Field.Fly;',
  24980. ' o.Field.Run;',
  24981. ' with o do begin',
  24982. ' Field.Fly;',
  24983. ' Field.Run;',
  24984. ' end;',
  24985. ' with o.Field do begin',
  24986. ' Fly;',
  24987. ' Run;',
  24988. ' end;',
  24989. '']);
  24990. ConvertProgram;
  24991. CheckSource('TestTypeHelper_PassPropertyGetter',
  24992. LinesToStr([ // statements
  24993. 'rtl.createClass(this, "TObject", null, function () {',
  24994. ' this.$init = function () {',
  24995. ' this.FField = 0;',
  24996. ' };',
  24997. ' this.$final = function () {',
  24998. ' };',
  24999. ' this.GetField = function () {',
  25000. ' var Result = 0;',
  25001. ' $mod.THelper.Fly.call({',
  25002. ' p: this.GetField(),',
  25003. ' get: function () {',
  25004. ' return this.p;',
  25005. ' },',
  25006. ' set: function (v) {',
  25007. ' this.p = v;',
  25008. ' }',
  25009. ' });',
  25010. ' $mod.THelper.Run();',
  25011. ' $mod.THelper.Fly.call({',
  25012. ' p: this.GetField(),',
  25013. ' get: function () {',
  25014. ' return this.p;',
  25015. ' },',
  25016. ' set: function (v) {',
  25017. ' this.p = v;',
  25018. ' }',
  25019. ' });',
  25020. ' $mod.THelper.Run();',
  25021. ' $mod.THelper.Fly.call({',
  25022. ' p: this.GetField(),',
  25023. ' get: function () {',
  25024. ' return this.p;',
  25025. ' },',
  25026. ' set: function (v) {',
  25027. ' this.p = v;',
  25028. ' }',
  25029. ' });',
  25030. ' $mod.THelper.Run();',
  25031. ' var $with = this.GetField();',
  25032. ' $mod.THelper.Fly.call({',
  25033. ' get: function () {',
  25034. ' return $with;',
  25035. ' },',
  25036. ' set: function (v) {',
  25037. ' $with = v;',
  25038. ' }',
  25039. ' });',
  25040. ' $mod.THelper.Run();',
  25041. ' return Result;',
  25042. ' };',
  25043. '});',
  25044. 'rtl.createHelper(this, "THelper", null, function () {',
  25045. ' this.Fly = function () {',
  25046. ' };',
  25047. ' this.Run = function () {',
  25048. ' };',
  25049. '});',
  25050. 'this.o = null;',
  25051. '']),
  25052. LinesToStr([ // $mod.$main
  25053. '$mod.THelper.Fly.call({',
  25054. ' p: $mod.o.GetField(),',
  25055. ' get: function () {',
  25056. ' return this.p;',
  25057. ' },',
  25058. ' set: function (v) {',
  25059. ' this.p = v;',
  25060. ' }',
  25061. '});',
  25062. '$mod.THelper.Run();',
  25063. 'var $with = $mod.o;',
  25064. '$mod.THelper.Fly.call({',
  25065. ' p: $with.GetField(),',
  25066. ' get: function () {',
  25067. ' return this.p;',
  25068. ' },',
  25069. ' set: function (v) {',
  25070. ' this.p = v;',
  25071. ' }',
  25072. '});',
  25073. '$mod.THelper.Run();',
  25074. 'var $with1 = $mod.o.GetField();',
  25075. '$mod.THelper.Fly.call({',
  25076. ' get: function () {',
  25077. ' return $with1;',
  25078. ' },',
  25079. ' set: function (v) {',
  25080. ' $with1 = v;',
  25081. ' }',
  25082. '});',
  25083. '$mod.THelper.Run();',
  25084. '']));
  25085. end;
  25086. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25087. begin
  25088. StartProgram(false);
  25089. Add([
  25090. '{$modeswitch typehelpers}',
  25091. 'type',
  25092. ' TObject = class',
  25093. ' class var FField: word;',
  25094. ' class procedure SetField(Value: word);',
  25095. ' class property Field: word read FField write SetField;',
  25096. ' end;',
  25097. ' THelper = type helper for word',
  25098. ' procedure Fly(n: byte);',
  25099. ' end;',
  25100. 'class procedure TObject.SetField(Value: word);',
  25101. 'begin',
  25102. ' Field.Fly(1);',
  25103. ' Self.Field.Fly(2);',
  25104. ' with Self do Field.Fly(3);',
  25105. ' with Self.Field do Fly(4);',
  25106. ' TObject.Field.Fly(5);',
  25107. ' with TObject do Field.Fly(6);',
  25108. ' with TObject.Field do Fly(7);',
  25109. 'end;',
  25110. 'procedure THelper.Fly(n: byte);',
  25111. 'begin',
  25112. 'end;',
  25113. 'var',
  25114. ' o: TObject;',
  25115. 'begin',
  25116. ' o.Field.Fly(11);',
  25117. ' with o do Field.Fly(12);',
  25118. ' with o.Field do Fly(13);',
  25119. ' TObject.Field.Fly(14);',
  25120. ' with TObject do Field.Fly(15);',
  25121. ' with TObject.Field do Fly(16);',
  25122. '']);
  25123. ConvertProgram;
  25124. CheckSource('TestTypeHelper_PassClassPropertyField',
  25125. LinesToStr([ // statements
  25126. 'rtl.createClass(this, "TObject", null, function () {',
  25127. ' this.FField = 0;',
  25128. ' this.$init = function () {',
  25129. ' };',
  25130. ' this.$final = function () {',
  25131. ' };',
  25132. ' this.SetField = function (Value) {',
  25133. ' $mod.THelper.Fly.call({',
  25134. ' p: this,',
  25135. ' get: function () {',
  25136. ' return this.p.FField;',
  25137. ' },',
  25138. ' set: function (v) {',
  25139. ' $mod.TObject.FField = v;',
  25140. ' }',
  25141. ' }, 1);',
  25142. ' $mod.THelper.Fly.call({',
  25143. ' p: this,',
  25144. ' get: function () {',
  25145. ' return this.p.FField;',
  25146. ' },',
  25147. ' set: function (v) {',
  25148. ' $mod.TObject.FField = v;',
  25149. ' }',
  25150. ' }, 2);',
  25151. ' $mod.THelper.Fly.call({',
  25152. ' p: this,',
  25153. ' get: function () {',
  25154. ' return this.p.FField;',
  25155. ' },',
  25156. ' set: function (v) {',
  25157. ' $mod.TObject.FField = v;',
  25158. ' }',
  25159. ' }, 3);',
  25160. ' var $with = this.FField;',
  25161. ' $mod.THelper.Fly.call({',
  25162. ' get: function () {',
  25163. ' return $with;',
  25164. ' },',
  25165. ' set: function (v) {',
  25166. ' $with = v;',
  25167. ' }',
  25168. ' }, 4);',
  25169. ' $mod.THelper.Fly.call({',
  25170. ' p: $mod.TObject,',
  25171. ' get: function () {',
  25172. ' return this.p.FField;',
  25173. ' },',
  25174. ' set: function (v) {',
  25175. ' $mod.TObject.FField = v;',
  25176. ' }',
  25177. ' }, 5);',
  25178. ' var $with1 = $mod.TObject;',
  25179. ' $mod.THelper.Fly.call({',
  25180. ' p: $with1,',
  25181. ' get: function () {',
  25182. ' return this.p.FField;',
  25183. ' },',
  25184. ' set: function (v) {',
  25185. ' $mod.TObject.FField = v;',
  25186. ' }',
  25187. ' }, 6);',
  25188. ' var $with2 = $mod.TObject.FField;',
  25189. ' $mod.THelper.Fly.call({',
  25190. ' get: function () {',
  25191. ' return $with2;',
  25192. ' },',
  25193. ' set: function (v) {',
  25194. ' $with2 = v;',
  25195. ' }',
  25196. ' }, 7);',
  25197. ' };',
  25198. '});',
  25199. 'rtl.createHelper(this, "THelper", null, function () {',
  25200. ' this.Fly = function (n) {',
  25201. ' };',
  25202. '});',
  25203. 'this.o = null;',
  25204. '']),
  25205. LinesToStr([ // $mod.$main
  25206. '$mod.THelper.Fly.call({',
  25207. ' p: $mod.o,',
  25208. ' get: function () {',
  25209. ' return this.p.FField;',
  25210. ' },',
  25211. ' set: function (v) {',
  25212. ' $mod.TObject.FField = v;',
  25213. ' }',
  25214. '}, 11);',
  25215. 'var $with = $mod.o;',
  25216. '$mod.THelper.Fly.call({',
  25217. ' p: $with,',
  25218. ' get: function () {',
  25219. ' return this.p.FField;',
  25220. ' },',
  25221. ' set: function (v) {',
  25222. ' $mod.TObject.FField = v;',
  25223. ' }',
  25224. '}, 12);',
  25225. 'var $with1 = $mod.o.FField;',
  25226. '$mod.THelper.Fly.call({',
  25227. ' get: function () {',
  25228. ' return $with1;',
  25229. ' },',
  25230. ' set: function (v) {',
  25231. ' $with1 = v;',
  25232. ' }',
  25233. '}, 13);',
  25234. '$mod.THelper.Fly.call({',
  25235. ' p: $mod.TObject,',
  25236. ' get: function () {',
  25237. ' return this.p.FField;',
  25238. ' },',
  25239. ' set: function (v) {',
  25240. ' $mod.TObject.FField = v;',
  25241. ' }',
  25242. '}, 14);',
  25243. 'var $with2 = $mod.TObject;',
  25244. '$mod.THelper.Fly.call({',
  25245. ' p: $with2,',
  25246. ' get: function () {',
  25247. ' return this.p.FField;',
  25248. ' },',
  25249. ' set: function (v) {',
  25250. ' $mod.TObject.FField = v;',
  25251. ' }',
  25252. '}, 15);',
  25253. 'var $with3 = $mod.TObject.FField;',
  25254. '$mod.THelper.Fly.call({',
  25255. ' get: function () {',
  25256. ' return $with3;',
  25257. ' },',
  25258. ' set: function (v) {',
  25259. ' $with3 = v;',
  25260. ' }',
  25261. '}, 16);',
  25262. '']));
  25263. end;
  25264. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25265. begin
  25266. StartProgram(false);
  25267. Add([
  25268. '{$modeswitch typehelpers}',
  25269. 'type',
  25270. ' TObject = class',
  25271. ' class var FField: word;',
  25272. ' class function GetField: word; static;',
  25273. ' class property Field: word read GetField write FField;',
  25274. ' end;',
  25275. ' THelper = type helper for word',
  25276. ' procedure Fly(n: byte);',
  25277. ' end;',
  25278. 'class function TObject.GetField: word;',
  25279. 'begin',
  25280. ' Field.Fly(1);',
  25281. ' TObject.Field.Fly(5);',
  25282. ' with TObject do Field.Fly(6);',
  25283. ' with TObject.Field do Fly(7);',
  25284. 'end;',
  25285. 'procedure THelper.Fly(n: byte);',
  25286. 'begin',
  25287. 'end;',
  25288. 'var',
  25289. ' o: TObject;',
  25290. 'begin',
  25291. ' o.Field.Fly(11);',
  25292. ' with o do Field.Fly(12);',
  25293. ' with o.Field do Fly(13);',
  25294. '']);
  25295. ConvertProgram;
  25296. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25297. LinesToStr([ // statements
  25298. 'rtl.createClass(this, "TObject", null, function () {',
  25299. ' this.FField = 0;',
  25300. ' this.$init = function () {',
  25301. ' };',
  25302. ' this.$final = function () {',
  25303. ' };',
  25304. ' this.GetField = function () {',
  25305. ' var Result = 0;',
  25306. ' $mod.THelper.Fly.call({',
  25307. ' p: $mod.TObject.GetField(),',
  25308. ' get: function () {',
  25309. ' return this.p;',
  25310. ' },',
  25311. ' set: function (v) {',
  25312. ' this.p = v;',
  25313. ' }',
  25314. ' }, 1);',
  25315. ' $mod.THelper.Fly.call({',
  25316. ' p: $mod.TObject.GetField(),',
  25317. ' get: function () {',
  25318. ' return this.p;',
  25319. ' },',
  25320. ' set: function (v) {',
  25321. ' this.p = v;',
  25322. ' }',
  25323. ' }, 5);',
  25324. ' var $with = $mod.TObject;',
  25325. ' $mod.THelper.Fly.call({',
  25326. ' p: $with.GetField(),',
  25327. ' get: function () {',
  25328. ' return this.p;',
  25329. ' },',
  25330. ' set: function (v) {',
  25331. ' this.p = v;',
  25332. ' }',
  25333. ' }, 6);',
  25334. ' var $with1 = $mod.TObject.GetField();',
  25335. ' $mod.THelper.Fly.call({',
  25336. ' get: function () {',
  25337. ' return $with1;',
  25338. ' },',
  25339. ' set: function (v) {',
  25340. ' $with1 = v;',
  25341. ' }',
  25342. ' }, 7);',
  25343. ' return Result;',
  25344. ' };',
  25345. '});',
  25346. 'rtl.createHelper(this, "THelper", null, function () {',
  25347. ' this.Fly = function (n) {',
  25348. ' };',
  25349. '});',
  25350. 'this.o = null;',
  25351. '']),
  25352. LinesToStr([ // $mod.$main
  25353. '$mod.THelper.Fly.call({',
  25354. ' p: $mod.TObject.GetField(),',
  25355. ' get: function () {',
  25356. ' return this.p;',
  25357. ' },',
  25358. ' set: function (v) {',
  25359. ' this.p = v;',
  25360. ' }',
  25361. '}, 11);',
  25362. 'var $with = $mod.o;',
  25363. '$mod.THelper.Fly.call({',
  25364. ' p: $with.GetField(),',
  25365. ' get: function () {',
  25366. ' return this.p;',
  25367. ' },',
  25368. ' set: function (v) {',
  25369. ' this.p = v;',
  25370. ' }',
  25371. '}, 12);',
  25372. 'var $with1 = $mod.TObject.GetField();',
  25373. '$mod.THelper.Fly.call({',
  25374. ' get: function () {',
  25375. ' return $with1;',
  25376. ' },',
  25377. ' set: function (v) {',
  25378. ' $with1 = v;',
  25379. ' }',
  25380. '}, 13);',
  25381. '']));
  25382. end;
  25383. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25384. begin
  25385. StartProgram(false);
  25386. Add([
  25387. '{$modeswitch typehelpers}',
  25388. 'type',
  25389. ' TObject = class',
  25390. ' class var FField: word;',
  25391. ' class function GetField: word;',
  25392. ' class property Field: word read GetField write FField;',
  25393. ' end;',
  25394. ' TClass = class of TObject;',
  25395. ' THelper = type helper for word',
  25396. ' procedure Fly(n: byte);',
  25397. ' end;',
  25398. 'class function TObject.GetField: word;',
  25399. 'begin',
  25400. ' Field.Fly(1);',
  25401. ' Self.Field.Fly(5);',
  25402. ' with Self do Field.Fly(6);',
  25403. ' with Self.Field do Fly(7);',
  25404. 'end;',
  25405. 'procedure THelper.Fly(n: byte);',
  25406. 'begin',
  25407. 'end;',
  25408. 'var',
  25409. ' o: TObject;',
  25410. ' c: TClass;',
  25411. 'begin',
  25412. ' o.Field.Fly(11);',
  25413. ' with o do Field.Fly(12);',
  25414. ' with o.Field do Fly(13);',
  25415. ' c.Field.Fly(14);',
  25416. ' with c do Field.Fly(15);',
  25417. ' with c.Field do Fly(16);',
  25418. '']);
  25419. ConvertProgram;
  25420. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25421. LinesToStr([ // statements
  25422. 'rtl.createClass(this, "TObject", null, function () {',
  25423. ' this.FField = 0;',
  25424. ' this.$init = function () {',
  25425. ' };',
  25426. ' this.$final = function () {',
  25427. ' };',
  25428. ' this.GetField = function () {',
  25429. ' var Result = 0;',
  25430. ' $mod.THelper.Fly.call({',
  25431. ' p: this.GetField(),',
  25432. ' get: function () {',
  25433. ' return this.p;',
  25434. ' },',
  25435. ' set: function (v) {',
  25436. ' this.p = v;',
  25437. ' }',
  25438. ' }, 1);',
  25439. ' $mod.THelper.Fly.call({',
  25440. ' p: this.GetField(),',
  25441. ' get: function () {',
  25442. ' return this.p;',
  25443. ' },',
  25444. ' set: function (v) {',
  25445. ' this.p = v;',
  25446. ' }',
  25447. ' }, 5);',
  25448. ' $mod.THelper.Fly.call({',
  25449. ' p: this.GetField(),',
  25450. ' get: function () {',
  25451. ' return this.p;',
  25452. ' },',
  25453. ' set: function (v) {',
  25454. ' this.p = v;',
  25455. ' }',
  25456. ' }, 6);',
  25457. ' var $with = this.GetField();',
  25458. ' $mod.THelper.Fly.call({',
  25459. ' get: function () {',
  25460. ' return $with;',
  25461. ' },',
  25462. ' set: function (v) {',
  25463. ' $with = v;',
  25464. ' }',
  25465. ' }, 7);',
  25466. ' return Result;',
  25467. ' };',
  25468. '});',
  25469. 'rtl.createHelper(this, "THelper", null, function () {',
  25470. ' this.Fly = function (n) {',
  25471. ' };',
  25472. '});',
  25473. 'this.o = null;',
  25474. 'this.c = null;',
  25475. '']),
  25476. LinesToStr([ // $mod.$main
  25477. '$mod.THelper.Fly.call({',
  25478. ' p: $mod.o.$class.GetField(),',
  25479. ' get: function () {',
  25480. ' return this.p;',
  25481. ' },',
  25482. ' set: function (v) {',
  25483. ' this.p = v;',
  25484. ' }',
  25485. '}, 11);',
  25486. 'var $with = $mod.o;',
  25487. '$mod.THelper.Fly.call({',
  25488. ' p: $with.$class.GetField(),',
  25489. ' get: function () {',
  25490. ' return this.p;',
  25491. ' },',
  25492. ' set: function (v) {',
  25493. ' this.p = v;',
  25494. ' }',
  25495. '}, 12);',
  25496. 'var $with1 = $mod.o.$class.GetField();',
  25497. '$mod.THelper.Fly.call({',
  25498. ' get: function () {',
  25499. ' return $with1;',
  25500. ' },',
  25501. ' set: function (v) {',
  25502. ' $with1 = v;',
  25503. ' }',
  25504. '}, 13);',
  25505. '$mod.THelper.Fly.call({',
  25506. ' p: $mod.c.GetField(),',
  25507. ' get: function () {',
  25508. ' return this.p;',
  25509. ' },',
  25510. ' set: function (v) {',
  25511. ' this.p = v;',
  25512. ' }',
  25513. '}, 14);',
  25514. 'var $with2 = $mod.c;',
  25515. '$mod.THelper.Fly.call({',
  25516. ' p: $with2.GetField(),',
  25517. ' get: function () {',
  25518. ' return this.p;',
  25519. ' },',
  25520. ' set: function (v) {',
  25521. ' this.p = v;',
  25522. ' }',
  25523. '}, 15);',
  25524. 'var $with3 = $mod.c.GetField();',
  25525. '$mod.THelper.Fly.call({',
  25526. ' get: function () {',
  25527. ' return $with3;',
  25528. ' },',
  25529. ' set: function (v) {',
  25530. ' $with3 = v;',
  25531. ' }',
  25532. '}, 16);',
  25533. '']));
  25534. end;
  25535. procedure TTestModule.TestTypeHelper_Property;
  25536. begin
  25537. StartProgram(false);
  25538. Add([
  25539. '{$modeswitch typehelpers}',
  25540. 'type',
  25541. ' THelper = type helper for word',
  25542. ' function GetSize: longint;',
  25543. ' procedure SetSize(Value: longint);',
  25544. ' property Size: longint read GetSize write SetSize;',
  25545. ' end;',
  25546. 'function THelper.GetSize: longint;',
  25547. 'begin',
  25548. ' Result:=Size+1;',
  25549. ' Size:=2;',
  25550. ' Result:=Self.Size+3;',
  25551. ' Self.Size:=4;',
  25552. ' with Self do begin',
  25553. ' Result:=Size+5;',
  25554. ' Size:=6;',
  25555. ' end;',
  25556. 'end;',
  25557. 'procedure THelper.SetSize(Value: longint);',
  25558. 'begin',
  25559. 'end;',
  25560. 'var w: word;',
  25561. 'begin',
  25562. ' w:=w.Size+7;',
  25563. ' w.Size:=w+8;',
  25564. ' with w do begin',
  25565. ' w:=Size+9;',
  25566. ' Size:=w+10;',
  25567. ' end;',
  25568. '']);
  25569. ConvertProgram;
  25570. CheckSource('TestTypeHelper_Property',
  25571. LinesToStr([ // statements
  25572. 'rtl.createHelper(this, "THelper", null, function () {',
  25573. ' this.GetSize = function () {',
  25574. ' var Result = 0;',
  25575. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  25576. ' $mod.THelper.SetSize.call(this, 2);',
  25577. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  25578. ' $mod.THelper.SetSize.call(this, 4);',
  25579. ' var $with = this.get();',
  25580. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  25581. ' $mod.THelper.SetSize.call(this, 6);',
  25582. ' return Result;',
  25583. ' };',
  25584. ' this.SetSize = function (Value) {',
  25585. ' };',
  25586. '});',
  25587. 'this.w = 0;',
  25588. '']),
  25589. LinesToStr([ // $mod.$main
  25590. '$mod.w = $mod.THelper.GetSize.call({',
  25591. ' p: $mod,',
  25592. ' get: function () {',
  25593. ' return this.p.w;',
  25594. ' },',
  25595. ' set: function (v) {',
  25596. ' this.p.w = v;',
  25597. ' }',
  25598. '}) + 7;',
  25599. '$mod.THelper.SetSize.call({',
  25600. ' p: $mod,',
  25601. ' get: function () {',
  25602. ' return this.p.w;',
  25603. ' },',
  25604. ' set: function (v) {',
  25605. ' this.p.w = v;',
  25606. ' }',
  25607. '}, $mod.w + 8);',
  25608. 'var $with = $mod.w;',
  25609. '$mod.w = $mod.THelper.GetSize.call({',
  25610. ' get: function () {',
  25611. ' return $with;',
  25612. ' },',
  25613. ' set: function (v) {',
  25614. ' $with = v;',
  25615. ' }',
  25616. '}) + 9;',
  25617. '$mod.THelper.SetSize.call({',
  25618. ' get: function () {',
  25619. ' return $with;',
  25620. ' },',
  25621. ' set: function (v) {',
  25622. ' $with = v;',
  25623. ' }',
  25624. '}, $mod.w + 10);',
  25625. '']));
  25626. end;
  25627. procedure TTestModule.TestTypeHelper_Property_Array;
  25628. begin
  25629. StartProgram(false);
  25630. Add([
  25631. '{$modeswitch typehelpers}',
  25632. 'type',
  25633. ' THelper = type helper for word',
  25634. ' function GetItems(Index: byte): boolean;',
  25635. ' procedure SetItems(Index: byte; Value: boolean);',
  25636. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  25637. ' end;',
  25638. 'function THelper.GetItems(Index: byte): boolean;',
  25639. 'begin',
  25640. ' Result:=Items[1];',
  25641. ' Items[2]:=false;',
  25642. ' Result:=Self.Items[3];',
  25643. ' Self.Items[4]:=true;',
  25644. ' with Self do begin',
  25645. ' Result:=Items[5];',
  25646. ' Items[6]:=false;',
  25647. ' end;',
  25648. 'end;',
  25649. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  25650. 'begin',
  25651. 'end;',
  25652. 'var',
  25653. ' w: word;',
  25654. ' b: boolean;',
  25655. 'begin',
  25656. ' b:=w.Items[1];',
  25657. ' w.Items[2]:=b;',
  25658. ' with w do begin',
  25659. ' b:=Items[3];',
  25660. ' Items[4]:=b;',
  25661. ' end;',
  25662. '']);
  25663. ConvertProgram;
  25664. CheckSource('TestTypeHelper_Property_Array',
  25665. LinesToStr([ // statements
  25666. 'rtl.createHelper(this, "THelper", null, function () {',
  25667. ' this.GetItems = function (Index) {',
  25668. ' var Result = false;',
  25669. ' Result = $mod.THelper.GetItems.call(this, 1);',
  25670. ' $mod.THelper.SetItems.call(this, 2, false);',
  25671. ' Result = $mod.THelper.GetItems.call(this, 3);',
  25672. ' $mod.THelper.SetItems.call(this, 4, true);',
  25673. ' var $with = this.get();',
  25674. ' Result = $mod.THelper.GetItems.call(this, 5);',
  25675. ' $mod.THelper.SetItems.call(this, 6, false);',
  25676. ' return Result;',
  25677. ' };',
  25678. ' this.SetItems = function (Index, Value) {',
  25679. ' };',
  25680. '});',
  25681. 'this.w = 0;',
  25682. 'this.b = false;',
  25683. '']),
  25684. LinesToStr([ // $mod.$main
  25685. '$mod.b = $mod.THelper.GetItems.call({',
  25686. ' p: $mod,',
  25687. ' get: function () {',
  25688. ' return this.p.w;',
  25689. ' },',
  25690. ' set: function (v) {',
  25691. ' this.p.w = v;',
  25692. ' }',
  25693. '}, 1);',
  25694. '$mod.THelper.SetItems.call({',
  25695. ' p: $mod,',
  25696. ' get: function () {',
  25697. ' return this.p.w;',
  25698. ' },',
  25699. ' set: function (v) {',
  25700. ' this.p.w = v;',
  25701. ' }',
  25702. '}, 2, $mod.b);',
  25703. 'var $with = $mod.w;',
  25704. '$mod.b = $mod.THelper.GetItems.call({',
  25705. ' get: function () {',
  25706. ' return $with;',
  25707. ' },',
  25708. ' set: function (v) {',
  25709. ' $with = v;',
  25710. ' }',
  25711. '}, 3);',
  25712. '$mod.THelper.SetItems.call({',
  25713. ' get: function () {',
  25714. ' return $with;',
  25715. ' },',
  25716. ' set: function (v) {',
  25717. ' $with = v;',
  25718. ' }',
  25719. '}, 4, $mod.b);',
  25720. '']));
  25721. end;
  25722. procedure TTestModule.TestTypeHelper_ClassProperty;
  25723. begin
  25724. StartProgram(false);
  25725. Add([
  25726. '{$modeswitch typehelpers}',
  25727. 'type',
  25728. ' THelper = type helper for word',
  25729. ' class function GetSize: longint; static;',
  25730. ' class procedure SetSize(Value: longint); static;',
  25731. ' class property Size: longint read GetSize write SetSize;',
  25732. ' end;',
  25733. 'class function THelper.GetSize: longint;',
  25734. 'begin',
  25735. ' Result:=Size+1;',
  25736. ' Size:=2;',
  25737. 'end;',
  25738. 'class procedure THelper.SetSize(Value: longint);',
  25739. 'begin',
  25740. 'end;',
  25741. 'begin',
  25742. '']);
  25743. ConvertProgram;
  25744. CheckSource('TestTypeHelper_ClassProperty',
  25745. LinesToStr([ // statements
  25746. 'rtl.createHelper(this, "THelper", null, function () {',
  25747. ' this.GetSize = function () {',
  25748. ' var Result = 0;',
  25749. ' Result = $mod.THelper.GetSize() + 1;',
  25750. ' $mod.THelper.SetSize(2);',
  25751. ' return Result;',
  25752. ' };',
  25753. ' this.SetSize = function (Value) {',
  25754. ' };',
  25755. '});',
  25756. '']),
  25757. LinesToStr([ // $mod.$main
  25758. '']));
  25759. end;
  25760. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  25761. begin
  25762. StartProgram(false);
  25763. Add([
  25764. '{$modeswitch typehelpers}',
  25765. 'type',
  25766. ' THelper = type helper for word',
  25767. ' class function GetItems(Index: byte): boolean; static;',
  25768. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  25769. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  25770. ' end;',
  25771. 'class function THelper.GetItems(Index: byte): boolean;',
  25772. 'begin',
  25773. ' Result:=Items[1];',
  25774. ' Items[2]:=false;',
  25775. 'end;',
  25776. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  25777. 'begin',
  25778. 'end;',
  25779. 'var',
  25780. ' w: word;',
  25781. ' b: boolean;',
  25782. 'begin',
  25783. ' b:=w.Items[1];',
  25784. ' w.Items[2]:=b;',
  25785. ' with w do begin',
  25786. ' b:=Items[3];',
  25787. ' Items[4]:=b;',
  25788. ' end;',
  25789. '']);
  25790. ConvertProgram;
  25791. CheckSource('TestTypeHelper_ClassProperty_Array',
  25792. LinesToStr([ // statements
  25793. 'rtl.createHelper(this, "THelper", null, function () {',
  25794. ' this.GetItems = function (Index) {',
  25795. ' var Result = false;',
  25796. ' Result = $mod.THelper.GetItems(1);',
  25797. ' $mod.THelper.SetItems(2, false);',
  25798. ' return Result;',
  25799. ' };',
  25800. ' this.SetItems = function (Index, Value) {',
  25801. ' };',
  25802. '});',
  25803. 'this.w = 0;',
  25804. 'this.b = false;',
  25805. '']),
  25806. LinesToStr([ // $mod.$main
  25807. '$mod.b = $mod.THelper.GetItems(1);',
  25808. '$mod.THelper.SetItems(2, $mod.b);',
  25809. 'var $with = $mod.w;',
  25810. '$mod.b = $mod.THelper.GetItems(3);',
  25811. '$mod.THelper.SetItems(4, $mod.b);',
  25812. '']));
  25813. end;
  25814. procedure TTestModule.TestTypeHelper_ClassMethod;
  25815. begin
  25816. StartProgram(false);
  25817. Add([
  25818. '{$modeswitch typehelpers}',
  25819. 'type',
  25820. ' THelper = type helper for word',
  25821. ' class procedure DoStatic; static;',
  25822. ' end;',
  25823. 'class procedure THelper.DoStatic;',
  25824. 'begin',
  25825. ' DoStatic;',
  25826. ' DoStatic();',
  25827. 'end;',
  25828. 'var w: word;',
  25829. 'begin',
  25830. ' w.DoStatic;',
  25831. ' w.DoStatic();',
  25832. '']);
  25833. ConvertProgram;
  25834. CheckSource('TestTypeHelper_ClassMethod',
  25835. LinesToStr([ // statements
  25836. 'rtl.createHelper(this, "THelper", null, function () {',
  25837. ' this.DoStatic = function () {',
  25838. ' $mod.THelper.DoStatic();',
  25839. ' $mod.THelper.DoStatic();',
  25840. ' };',
  25841. '});',
  25842. 'this.w = 0;',
  25843. '']),
  25844. LinesToStr([ // $mod.$main
  25845. '$mod.THelper.DoStatic();',
  25846. '$mod.THelper.DoStatic();',
  25847. '']));
  25848. end;
  25849. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  25850. begin
  25851. StartProgram(false);
  25852. Add([
  25853. '{$modeswitch typehelpers}',
  25854. 'type',
  25855. ' THelper = type helper for word',
  25856. ' procedure Run; external name ''Run'';',
  25857. ' end;',
  25858. 'var w: word;',
  25859. 'begin',
  25860. ' w.Run;',
  25861. '']);
  25862. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  25863. ConvertProgram;
  25864. end;
  25865. procedure TTestModule.TestTypeHelper_Constructor;
  25866. begin
  25867. StartProgram(false);
  25868. Add([
  25869. '{$modeswitch typehelpers}',
  25870. 'type',
  25871. ' THelper = type helper for word',
  25872. ' constructor Init(e: longint);',
  25873. ' end;',
  25874. 'constructor THelper.Init(e: longint);',
  25875. 'begin',
  25876. ' Self:=e;',
  25877. ' Init(e+1);',
  25878. 'end;',
  25879. 'var w: word;',
  25880. 'begin',
  25881. ' w:=word.Init(2);',
  25882. ' w:=w.Init(3);',
  25883. ' with word do w:=Init(4);',
  25884. ' with w do w:=Init(5);',
  25885. '']);
  25886. ConvertProgram;
  25887. CheckSource('TestTypeHelper_Constructor',
  25888. LinesToStr([ // statements
  25889. 'rtl.createHelper(this, "THelper", null, function () {',
  25890. ' this.Init = function (e) {',
  25891. ' this.set(e);',
  25892. ' $mod.THelper.Init.call(this, e + 1);',
  25893. ' return this.get();',
  25894. ' };',
  25895. ' this.$new = function (fn, args) {',
  25896. ' return this[fn].apply({',
  25897. ' p: 0,',
  25898. ' get: function () {',
  25899. ' return this.p;',
  25900. ' },',
  25901. ' set: function (v) {',
  25902. ' this.p = v;',
  25903. ' }',
  25904. ' }, args);',
  25905. ' };',
  25906. '});',
  25907. 'this.w = 0;',
  25908. '']),
  25909. LinesToStr([ // $mod.$main
  25910. '$mod.w = $mod.THelper.$new("Init", [2]);',
  25911. '$mod.w = $mod.THelper.Init.call({',
  25912. ' p: $mod,',
  25913. ' get: function () {',
  25914. ' return this.p.w;',
  25915. ' },',
  25916. ' set: function (v) {',
  25917. ' this.p.w = v;',
  25918. ' }',
  25919. '}, 3);',
  25920. '$mod.w = $mod.THelper.$new("Init", [4]);',
  25921. 'var $with = $mod.w;',
  25922. '$mod.w = $mod.THelper.Init.call({',
  25923. ' get: function () {',
  25924. ' return $with;',
  25925. ' },',
  25926. ' set: function (v) {',
  25927. ' $with = v;',
  25928. ' }',
  25929. '}, 5);',
  25930. '']));
  25931. end;
  25932. procedure TTestModule.TestTypeHelper_Word;
  25933. begin
  25934. StartProgram(false);
  25935. Add([
  25936. '{$modeswitch typehelpers}',
  25937. 'type',
  25938. ' THelper = type helper for word',
  25939. ' procedure DoIt(e: byte = 123);',
  25940. ' end;',
  25941. 'procedure THelper.DoIt(e: byte);',
  25942. 'begin',
  25943. ' Self:=e;',
  25944. ' Self:=Self+1;',
  25945. ' with Self do Doit;',
  25946. 'end;',
  25947. 'begin',
  25948. ' word(3).DoIt;',
  25949. '']);
  25950. ConvertProgram;
  25951. CheckSource('TestTypeHelper_Word',
  25952. LinesToStr([ // statements
  25953. 'rtl.createHelper(this, "THelper", null, function () {',
  25954. ' this.DoIt = function (e) {',
  25955. ' this.set(e);',
  25956. ' this.set(this.get() + 1);',
  25957. ' var $with = this.get();',
  25958. ' $mod.THelper.DoIt.call(this, 123);',
  25959. ' };',
  25960. '});',
  25961. '']),
  25962. LinesToStr([ // $mod.$main
  25963. '$mod.THelper.DoIt.call({',
  25964. ' get: function () {',
  25965. ' return 3;',
  25966. ' },',
  25967. ' set: function (v) {',
  25968. ' rtl.raiseE("EPropReadOnly");',
  25969. ' }',
  25970. '}, 123);',
  25971. '']));
  25972. end;
  25973. procedure TTestModule.TestTypeHelper_Boolean;
  25974. begin
  25975. StartProgram(false);
  25976. Add([
  25977. '{$modeswitch typehelpers}',
  25978. 'type',
  25979. ' Integer = longint;',
  25980. ' THelper = type helper for boolean',
  25981. ' procedure Run(e: wordbool = true);',
  25982. ' end;',
  25983. 'procedure THelper.Run(e: wordbool);',
  25984. 'begin',
  25985. ' Self:=e;',
  25986. ' Self:=not Self;',
  25987. ' with Self do Run;',
  25988. ' if Integer(Self)=0 then ;',
  25989. 'end;',
  25990. 'begin',
  25991. ' boolean(3).Run;',
  25992. '']);
  25993. ConvertProgram;
  25994. CheckSource('TestTypeHelper_Boolean',
  25995. LinesToStr([ // statements
  25996. 'rtl.createHelper(this, "THelper", null, function () {',
  25997. ' this.Run = function (e) {',
  25998. ' this.set(e);',
  25999. ' this.set(!this.get());',
  26000. ' var $with = this.get();',
  26001. ' $mod.THelper.Run.call(this, true);',
  26002. ' if ((this.get() ? 1 : 0) === 0) ;',
  26003. ' };',
  26004. '});',
  26005. '']),
  26006. LinesToStr([ // $mod.$main
  26007. '$mod.THelper.Run.call({',
  26008. ' a: 3 != 0,',
  26009. ' get: function () {',
  26010. ' return this.a;',
  26011. ' },',
  26012. ' set: function (v) {',
  26013. ' rtl.raiseE("EPropReadOnly");',
  26014. ' }',
  26015. '}, true);',
  26016. '']));
  26017. end;
  26018. procedure TTestModule.TestTypeHelper_WordBool;
  26019. begin
  26020. StartProgram(false);
  26021. Add([
  26022. '{$modeswitch typehelpers}',
  26023. 'type',
  26024. ' Integer = longint;',
  26025. ' THelper = type helper for WordBool',
  26026. ' procedure Run(e: wordbool = true);',
  26027. ' end;',
  26028. 'procedure THelper.Run(e: wordbool);',
  26029. 'var i: integer;',
  26030. 'begin',
  26031. ' i:=Integer(Self);',
  26032. 'end;',
  26033. 'var w: wordbool;',
  26034. 'begin',
  26035. ' w.Run;',
  26036. ' wordbool(3).Run;',
  26037. '']);
  26038. ConvertProgram;
  26039. CheckSource('TestTypeHelper_WordBool',
  26040. LinesToStr([ // statements
  26041. 'rtl.createHelper(this, "THelper", null, function () {',
  26042. ' this.Run = function (e) {',
  26043. ' var i = 0;',
  26044. ' i = (this.get() ? 1 : 0);',
  26045. ' };',
  26046. '});',
  26047. 'this.w = false;',
  26048. '']),
  26049. LinesToStr([ // $mod.$main
  26050. '$mod.THelper.Run.call({',
  26051. ' p: $mod,',
  26052. ' get: function () {',
  26053. ' return this.p.w;',
  26054. ' },',
  26055. ' set: function (v) {',
  26056. ' this.p.w = v;',
  26057. ' }',
  26058. '}, true);',
  26059. '$mod.THelper.Run.call({',
  26060. ' a: 3 != 0,',
  26061. ' get: function () {',
  26062. ' return this.a;',
  26063. ' },',
  26064. ' set: function (v) {',
  26065. ' rtl.raiseE("EPropReadOnly");',
  26066. ' }',
  26067. '}, true);',
  26068. '']));
  26069. end;
  26070. procedure TTestModule.TestTypeHelper_Double;
  26071. begin
  26072. StartProgram(false);
  26073. Add([
  26074. '{$modeswitch typehelpers}',
  26075. 'type',
  26076. ' Float = type double;',
  26077. ' THelper = type helper for Float',
  26078. ' const NPI = 3.141592;',
  26079. ' function ToStr: String;',
  26080. ' end;',
  26081. 'function THelper.ToStr: String;',
  26082. 'begin',
  26083. 'end;',
  26084. 'procedure DoIt(s: string);',
  26085. 'begin',
  26086. 'end;',
  26087. 'var f: Float;',
  26088. 'begin',
  26089. ' DoIt(f.toStr);',
  26090. ' DoIt(f.toStr());',
  26091. ' (f*f).toStr;',
  26092. ' DoIt((f*f).toStr);',
  26093. '']);
  26094. ConvertProgram;
  26095. CheckSource('TestTypeHelper_Double',
  26096. LinesToStr([ // statements
  26097. 'rtl.createHelper(this, "THelper", null, function () {',
  26098. ' this.NPI = 3.141592;',
  26099. ' this.ToStr = function () {',
  26100. ' var Result = "";',
  26101. ' return Result;',
  26102. ' };',
  26103. '});',
  26104. 'this.DoIt = function (s) {',
  26105. '};',
  26106. 'this.f = 0.0;',
  26107. '']),
  26108. LinesToStr([ // $mod.$main
  26109. '$mod.DoIt($mod.THelper.ToStr.call({',
  26110. ' p: $mod,',
  26111. ' get: function () {',
  26112. ' return this.p.f;',
  26113. ' },',
  26114. ' set: function (v) {',
  26115. ' this.p.f = v;',
  26116. ' }',
  26117. '}));',
  26118. '$mod.DoIt($mod.THelper.ToStr.call({',
  26119. ' p: $mod,',
  26120. ' get: function () {',
  26121. ' return this.p.f;',
  26122. ' },',
  26123. ' set: function (v) {',
  26124. ' this.p.f = v;',
  26125. ' }',
  26126. '}));',
  26127. '$mod.THelper.ToStr.call({',
  26128. ' a: $mod.f * $mod.f,',
  26129. ' get: function () {',
  26130. ' return this.a;',
  26131. ' },',
  26132. ' set: function (v) {',
  26133. ' rtl.raiseE("EPropReadOnly");',
  26134. ' }',
  26135. '});',
  26136. '$mod.DoIt($mod.THelper.ToStr.call({',
  26137. ' a: $mod.f * $mod.f,',
  26138. ' get: function () {',
  26139. ' return this.a;',
  26140. ' },',
  26141. ' set: function (v) {',
  26142. ' rtl.raiseE("EPropReadOnly");',
  26143. ' }',
  26144. '}));',
  26145. '']));
  26146. end;
  26147. procedure TTestModule.TestTypeHelper_NativeInt;
  26148. begin
  26149. StartProgram(false);
  26150. Add([
  26151. '{$modeswitch typehelpers}',
  26152. 'type',
  26153. ' MaxInt = type nativeint;',
  26154. ' THelperI = type helper for MaxInt',
  26155. ' function ToStr: String;',
  26156. ' end;',
  26157. ' MaxUInt = type nativeuint;',
  26158. ' THelperU = type helper for MaxUInt',
  26159. ' function ToStr: String;',
  26160. ' end;',
  26161. 'function THelperI.ToStr: String;',
  26162. 'begin',
  26163. ' Result:=str(Self);',
  26164. 'end;',
  26165. 'function THelperU.ToStr: String;',
  26166. 'begin',
  26167. ' Result:=str(Self);',
  26168. 'end;',
  26169. 'procedure DoIt(s: string);',
  26170. 'begin',
  26171. 'end;',
  26172. 'var i: MaxInt;',
  26173. 'begin',
  26174. ' DoIt(i.toStr);',
  26175. ' DoIt(i.toStr());',
  26176. ' (i*i).toStr;',
  26177. ' DoIt((i*i).toStr);',
  26178. '']);
  26179. ConvertProgram;
  26180. CheckSource('TestTypeHelper_NativeInt',
  26181. LinesToStr([ // statements
  26182. 'rtl.createHelper(this, "THelperI", null, function () {',
  26183. ' this.ToStr = function () {',
  26184. ' var Result = "";',
  26185. ' Result = "" + this.get();',
  26186. ' return Result;',
  26187. ' };',
  26188. '});',
  26189. 'rtl.createHelper(this, "THelperU", null, function () {',
  26190. ' this.ToStr = function () {',
  26191. ' var Result = "";',
  26192. ' Result = "" + this.get();',
  26193. ' return Result;',
  26194. ' };',
  26195. '});',
  26196. 'this.DoIt = function (s) {',
  26197. '};',
  26198. 'this.i = 0;',
  26199. '']),
  26200. LinesToStr([ // $mod.$main
  26201. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26202. ' p: $mod,',
  26203. ' get: function () {',
  26204. ' return this.p.i;',
  26205. ' },',
  26206. ' set: function (v) {',
  26207. ' this.p.i = v;',
  26208. ' }',
  26209. '}));',
  26210. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26211. ' p: $mod,',
  26212. ' get: function () {',
  26213. ' return this.p.i;',
  26214. ' },',
  26215. ' set: function (v) {',
  26216. ' this.p.i = v;',
  26217. ' }',
  26218. '}));',
  26219. '$mod.THelperI.ToStr.call({',
  26220. ' a: $mod.i * $mod.i,',
  26221. ' get: function () {',
  26222. ' return this.a;',
  26223. ' },',
  26224. ' set: function (v) {',
  26225. ' rtl.raiseE("EPropReadOnly");',
  26226. ' }',
  26227. '});',
  26228. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26229. ' a: $mod.i * $mod.i,',
  26230. ' get: function () {',
  26231. ' return this.a;',
  26232. ' },',
  26233. ' set: function (v) {',
  26234. ' rtl.raiseE("EPropReadOnly");',
  26235. ' }',
  26236. '}));',
  26237. '']));
  26238. end;
  26239. procedure TTestModule.TestTypeHelper_StringChar;
  26240. begin
  26241. StartProgram(false);
  26242. Add([
  26243. '{$modeswitch typehelpers}',
  26244. 'type',
  26245. ' TStringHelper = type helper for string',
  26246. ' procedure DoIt(e: byte = 123);',
  26247. ' end;',
  26248. ' TCharHelper = type helper for char',
  26249. ' procedure Fly;',
  26250. ' end;',
  26251. 'procedure TStringHelper.DoIt(e: byte);',
  26252. 'begin',
  26253. ' Self[1]:=''c'';',
  26254. ' Self[2]:=Self[3];',
  26255. 'end;',
  26256. 'procedure TCharHelper.Fly;',
  26257. 'begin',
  26258. ' Self:=''c'';',
  26259. 'end;',
  26260. 'begin',
  26261. ' ''abc''.DoIt;',
  26262. ' ''xyz''.DoIt();',
  26263. ' ''c''.Fly();',
  26264. '']);
  26265. ConvertProgram;
  26266. CheckSource('TestTypeHelper_StringChar',
  26267. LinesToStr([ // statements
  26268. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26269. ' this.DoIt = function (e) {',
  26270. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26271. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26272. ' };',
  26273. '});',
  26274. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26275. ' this.Fly = function () {',
  26276. ' this.set("c");',
  26277. ' };',
  26278. '});',
  26279. '']),
  26280. LinesToStr([ // $mod.$main
  26281. '$mod.TStringHelper.DoIt.call({',
  26282. ' get: function () {',
  26283. ' return "abc";',
  26284. ' },',
  26285. ' set: function (v) {',
  26286. ' rtl.raiseE("EPropReadOnly");',
  26287. ' }',
  26288. '}, 123);',
  26289. '$mod.TStringHelper.DoIt.call({',
  26290. ' get: function () {',
  26291. ' return "xyz";',
  26292. ' },',
  26293. ' set: function (v) {',
  26294. ' rtl.raiseE("EPropReadOnly");',
  26295. ' }',
  26296. '}, 123);',
  26297. '$mod.TCharHelper.Fly.call({',
  26298. ' get: function () {',
  26299. ' return "c";',
  26300. ' },',
  26301. ' set: function (v) {',
  26302. ' rtl.raiseE("EPropReadOnly");',
  26303. ' }',
  26304. '});',
  26305. '']));
  26306. end;
  26307. procedure TTestModule.TestTypeHelper_JSValue;
  26308. begin
  26309. StartProgram(false);
  26310. Add([
  26311. '{$modeswitch typehelpers}',
  26312. 'type',
  26313. ' TExtValue = type jsvalue;',
  26314. ' THelper = type helper for TExtValue',
  26315. ' function ToStr: String;',
  26316. ' end;',
  26317. 'function THelper.ToStr: String;',
  26318. 'begin',
  26319. 'end;',
  26320. 'var',
  26321. ' s: string;',
  26322. ' v: TExtValue;',
  26323. 'begin',
  26324. ' s:=v.toStr;',
  26325. ' s:=v.toStr();',
  26326. ' TExtValue(s).toStr;',
  26327. '']);
  26328. ConvertProgram;
  26329. CheckSource('TestTypeHelper_JSValue',
  26330. LinesToStr([ // statements
  26331. 'rtl.createHelper(this, "THelper", null, function () {',
  26332. ' this.ToStr = function () {',
  26333. ' var Result = "";',
  26334. ' return Result;',
  26335. ' };',
  26336. '});',
  26337. 'this.s = "";',
  26338. 'this.v = undefined;',
  26339. '']),
  26340. LinesToStr([ // $mod.$main
  26341. '$mod.s = $mod.THelper.ToStr.call({',
  26342. ' p: $mod,',
  26343. ' get: function () {',
  26344. ' return this.p.v;',
  26345. ' },',
  26346. ' set: function (v) {',
  26347. ' this.p.v = v;',
  26348. ' }',
  26349. '});',
  26350. '$mod.s = $mod.THelper.ToStr.call({',
  26351. ' p: $mod,',
  26352. ' get: function () {',
  26353. ' return this.p.v;',
  26354. ' },',
  26355. ' set: function (v) {',
  26356. ' this.p.v = v;',
  26357. ' }',
  26358. '});',
  26359. '$mod.THelper.ToStr.call({',
  26360. ' p: $mod,',
  26361. ' get: function () {',
  26362. ' return this.p.s;',
  26363. ' },',
  26364. ' set: function (v) {',
  26365. ' rtl.raiseE("EPropReadOnly");',
  26366. ' }',
  26367. '});',
  26368. '']));
  26369. end;
  26370. procedure TTestModule.TestTypeHelper_Array;
  26371. begin
  26372. StartProgram(false);
  26373. Add([
  26374. '{$modeswitch typehelpers}',
  26375. 'type',
  26376. ' TArrOfBool = array of boolean;',
  26377. ' TArrOfJS = array of jsvalue;',
  26378. ' THelper = type helper for TArrOfBool',
  26379. ' procedure DoIt(e: byte = 123);',
  26380. ' end;',
  26381. 'procedure THelper.DoIt(e: byte);',
  26382. 'begin',
  26383. ' Self[1]:=true;',
  26384. ' Self[2]:=not Self[3];',
  26385. ' SetLength(Self,4);',
  26386. 'end;',
  26387. 'var',
  26388. ' b: TArrOfBool;',
  26389. ' j: TArrOfJS;',
  26390. 'begin',
  26391. ' b.DoIt;',
  26392. ' TArrOfBool(j).DoIt();',
  26393. '']);
  26394. ConvertProgram;
  26395. CheckSource('TestTypeHelper_Array',
  26396. LinesToStr([ // statements
  26397. 'rtl.createHelper(this, "THelper", null, function () {',
  26398. ' this.DoIt = function (e) {',
  26399. ' this.get()[1] = true;',
  26400. ' this.get()[2] = !this.get()[3];',
  26401. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26402. ' };',
  26403. '});',
  26404. 'this.b = [];',
  26405. 'this.j = [];',
  26406. '']),
  26407. LinesToStr([ // $mod.$main
  26408. '$mod.THelper.DoIt.call({',
  26409. ' p: $mod,',
  26410. ' get: function () {',
  26411. ' return this.p.b;',
  26412. ' },',
  26413. ' set: function (v) {',
  26414. ' this.p.b = v;',
  26415. ' }',
  26416. '}, 123);',
  26417. '$mod.THelper.DoIt.call({',
  26418. ' p: $mod,',
  26419. ' get: function () {',
  26420. ' return this.p.j;',
  26421. ' },',
  26422. ' set: function (v) {',
  26423. ' this.p.j = v;',
  26424. ' }',
  26425. '}, 123);',
  26426. '']));
  26427. end;
  26428. procedure TTestModule.TestTypeHelper_EnumType;
  26429. begin
  26430. StartProgram(false);
  26431. Add([
  26432. '{$modeswitch typehelpers}',
  26433. 'type',
  26434. ' TEnum = (red,blue);',
  26435. ' THelper = type helper for TEnum',
  26436. ' procedure DoIt(e: byte = 123);',
  26437. ' class procedure Swing(w: word); static;',
  26438. ' end;',
  26439. 'procedure THelper.DoIt(e: byte);',
  26440. 'begin',
  26441. ' Self:=red;',
  26442. ' Self:=succ(Self);',
  26443. ' with Self do Doit;',
  26444. 'end;',
  26445. 'class procedure THelper.Swing(w: word);',
  26446. 'begin',
  26447. 'end;',
  26448. 'var e: TEnum;',
  26449. 'begin',
  26450. ' e.DoIt;',
  26451. ' red.DoIt;',
  26452. ' TEnum.blue.DoIt;',
  26453. ' TEnum(1).DoIt;',
  26454. ' TEnum.Swing(3);',
  26455. '']);
  26456. ConvertProgram;
  26457. CheckSource('TestTypeHelper_EnumType',
  26458. LinesToStr([ // statements
  26459. 'this.TEnum = {',
  26460. ' "0": "red",',
  26461. ' red: 0,',
  26462. ' "1": "blue",',
  26463. ' blue: 1',
  26464. '};',
  26465. 'rtl.createHelper(this, "THelper", null, function () {',
  26466. ' this.DoIt = function (e) {',
  26467. ' this.set($mod.TEnum.red);',
  26468. ' this.set(this.get() + 1);',
  26469. ' var $with = this.get();',
  26470. ' $mod.THelper.DoIt.call(this, 123);',
  26471. ' };',
  26472. ' this.Swing = function (w) {',
  26473. ' };',
  26474. '});',
  26475. 'this.e = 0;',
  26476. '']),
  26477. LinesToStr([ // $mod.$main
  26478. '$mod.THelper.DoIt.call({',
  26479. ' p: $mod,',
  26480. ' get: function () {',
  26481. ' return this.p.e;',
  26482. ' },',
  26483. ' set: function (v) {',
  26484. ' this.p.e = v;',
  26485. ' }',
  26486. '}, 123);',
  26487. '$mod.THelper.DoIt.call({',
  26488. ' p: $mod.TEnum,',
  26489. ' get: function () {',
  26490. ' return this.p.red;',
  26491. ' },',
  26492. ' set: function (v) {',
  26493. ' rtl.raiseE("EPropReadOnly");',
  26494. ' }',
  26495. '}, 123);',
  26496. '$mod.THelper.DoIt.call({',
  26497. ' p: $mod.TEnum,',
  26498. ' get: function () {',
  26499. ' return this.p.blue;',
  26500. ' },',
  26501. ' set: function (v) {',
  26502. ' rtl.raiseE("EPropReadOnly");',
  26503. ' }',
  26504. '}, 123);',
  26505. '$mod.THelper.DoIt.call({',
  26506. ' get: function () {',
  26507. ' return 1;',
  26508. ' },',
  26509. ' set: function (v) {',
  26510. ' rtl.raiseE("EPropReadOnly");',
  26511. ' }',
  26512. '}, 123);',
  26513. '$mod.THelper.Swing(3);',
  26514. '']));
  26515. end;
  26516. procedure TTestModule.TestTypeHelper_SetType;
  26517. begin
  26518. StartProgram(false);
  26519. Add([
  26520. '{$modeswitch typehelpers}',
  26521. 'type',
  26522. ' TEnum = (red,blue);',
  26523. ' TSetOfEnum = set of TEnum;',
  26524. ' THelper = type helper for TSetOfEnum',
  26525. ' procedure DoIt(e: byte = 123);',
  26526. ' constructor Init(e: TEnum);',
  26527. ' constructor InitEmpty;',
  26528. ' end;',
  26529. 'procedure THelper.DoIt(e: byte);',
  26530. 'begin',
  26531. ' Self:=[];',
  26532. ' Self:=[red];',
  26533. ' Include(Self,blue);',
  26534. 'end;',
  26535. 'constructor THelper.Init(e: TEnum);',
  26536. 'begin',
  26537. ' Self:=[];',
  26538. ' Self:=[e];',
  26539. ' Include(Self,blue);',
  26540. 'end;',
  26541. 'constructor THelper.InitEmpty;',
  26542. 'begin',
  26543. 'end;',
  26544. 'var s: TSetOfEnum;',
  26545. 'begin',
  26546. ' s.DoIt;',
  26547. //' [red].DoIt;',
  26548. //' with s do DoIt;',
  26549. //' with [red,blue] do DoIt;',
  26550. ' s:=TSetOfEnum.Init(blue);',
  26551. ' s:=s.Init(blue);',
  26552. '']);
  26553. ConvertProgram;
  26554. CheckSource('TestTypeHelper_SetType',
  26555. LinesToStr([ // statements
  26556. 'this.TEnum = {',
  26557. ' "0": "red",',
  26558. ' red: 0,',
  26559. ' "1": "blue",',
  26560. ' blue: 1',
  26561. '};',
  26562. 'rtl.createHelper(this, "THelper", null, function () {',
  26563. ' this.DoIt = function (e) {',
  26564. ' this.set({});',
  26565. ' this.set(rtl.createSet($mod.TEnum.red));',
  26566. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26567. ' };',
  26568. ' this.Init = function (e) {',
  26569. ' this.set({});',
  26570. ' this.set(rtl.createSet(e));',
  26571. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  26572. ' return this.get();',
  26573. ' };',
  26574. ' this.InitEmpty = function () {',
  26575. ' return this.get();',
  26576. ' };',
  26577. ' this.$new = function (fn, args) {',
  26578. ' return this[fn].apply({',
  26579. ' p: {},',
  26580. ' get: function () {',
  26581. ' return this.p;',
  26582. ' },',
  26583. ' set: function (v) {',
  26584. ' this.p = v;',
  26585. ' }',
  26586. ' }, args);',
  26587. ' };',
  26588. '});',
  26589. 'this.s = {};',
  26590. '']),
  26591. LinesToStr([ // $mod.$main
  26592. '$mod.THelper.DoIt.call({',
  26593. ' p: $mod,',
  26594. ' get: function () {',
  26595. ' return this.p.s;',
  26596. ' },',
  26597. ' set: function (v) {',
  26598. ' this.p.s = v;',
  26599. ' }',
  26600. '}, 123);',
  26601. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  26602. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  26603. ' p: $mod,',
  26604. ' get: function () {',
  26605. ' return this.p.s;',
  26606. ' },',
  26607. ' set: function (v) {',
  26608. ' this.p.s = v;',
  26609. ' }',
  26610. '}, $mod.TEnum.blue));',
  26611. '']));
  26612. end;
  26613. procedure TTestModule.TestTypeHelper_InterfaceType;
  26614. begin
  26615. StartProgram(false);
  26616. Add([
  26617. '{$interfaces com}',
  26618. '{$modeswitch typehelpers}',
  26619. 'type',
  26620. ' IUnknown = interface',
  26621. ' function _AddRef: longint;',
  26622. ' function _Release: longint;',
  26623. ' end;',
  26624. ' TObject = class(IUnknown)',
  26625. ' function _AddRef: longint; virtual; abstract;',
  26626. ' function _Release: longint; virtual; abstract;',
  26627. ' end;',
  26628. ' THelper = type helper for IUnknown',
  26629. ' procedure Fly(e: byte = 123);',
  26630. ' class procedure Run; static;',
  26631. ' end;',
  26632. 'var',
  26633. ' i: IUnknown;',
  26634. ' o: TObject;',
  26635. 'procedure THelper.Fly(e: byte);',
  26636. 'begin',
  26637. ' i:=Self;',
  26638. ' o:=Self as TObject;',
  26639. ' Self:=nil;',
  26640. ' Self:=i;',
  26641. ' Self:=o;',
  26642. ' with Self do begin',
  26643. ' Fly;',
  26644. ' Fly();',
  26645. ' end;',
  26646. 'end;',
  26647. 'class procedure THelper.Run;',
  26648. 'var l: IUnknown;',
  26649. 'begin',
  26650. ' l.Fly;',
  26651. ' l.Fly();',
  26652. 'end;',
  26653. 'begin',
  26654. ' i.Fly;',
  26655. ' i.Fly();',
  26656. ' i.Run;',
  26657. ' i.Run();',
  26658. ' IUnknown.Run;',
  26659. ' IUnknown.Run();',
  26660. '']);
  26661. ConvertProgram;
  26662. CheckSource('TestTypeHelper_InterfaceType',
  26663. LinesToStr([ // statements
  26664. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  26665. 'rtl.createClass(this, "TObject", null, function () {',
  26666. ' this.$init = function () {',
  26667. ' };',
  26668. ' this.$final = function () {',
  26669. ' };',
  26670. ' rtl.addIntf(this, $mod.IUnknown);',
  26671. '});',
  26672. 'rtl.createHelper(this, "THelper", null, function () {',
  26673. ' this.Fly = function (e) {',
  26674. ' var $ir = rtl.createIntfRefs();',
  26675. ' try {',
  26676. ' rtl.setIntfP($mod, "i", this.get());',
  26677. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  26678. ' this.set(null);',
  26679. ' this.set($mod.i);',
  26680. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  26681. ' var $with = this.get();',
  26682. ' $mod.THelper.Fly.call(this, 123);',
  26683. ' $mod.THelper.Fly.call(this, 123);',
  26684. ' } finally {',
  26685. ' $ir.free();',
  26686. ' };',
  26687. ' };',
  26688. ' this.Run = function () {',
  26689. ' var l = null;',
  26690. ' try {',
  26691. ' $mod.THelper.Fly.call({',
  26692. ' get: function () {',
  26693. ' return l;',
  26694. ' },',
  26695. ' set: function (v) {',
  26696. ' l = rtl.setIntfL(l, v);',
  26697. ' }',
  26698. ' }, 123);',
  26699. ' $mod.THelper.Fly.call({',
  26700. ' get: function () {',
  26701. ' return l;',
  26702. ' },',
  26703. ' set: function (v) {',
  26704. ' l = rtl.setIntfL(l, v);',
  26705. ' }',
  26706. ' }, 123);',
  26707. ' } finally {',
  26708. ' rtl._Release(l);',
  26709. ' };',
  26710. ' };',
  26711. '});',
  26712. 'this.i = null;',
  26713. 'this.o = null;',
  26714. '']),
  26715. LinesToStr([ // $mod.$main
  26716. '$mod.THelper.Fly.call({',
  26717. ' p: $mod,',
  26718. ' get: function () {',
  26719. ' return this.p.i;',
  26720. ' },',
  26721. ' set: function (v) {',
  26722. ' rtl.setIntfP(this.p, "i", v);',
  26723. ' }',
  26724. '}, 123);',
  26725. '$mod.THelper.Fly.call({',
  26726. ' p: $mod,',
  26727. ' get: function () {',
  26728. ' return this.p.i;',
  26729. ' },',
  26730. ' set: function (v) {',
  26731. ' rtl.setIntfP(this.p, "i", v);',
  26732. ' }',
  26733. '}, 123);',
  26734. '$mod.THelper.Run();',
  26735. '$mod.THelper.Run();',
  26736. '$mod.THelper.Run();',
  26737. '$mod.THelper.Run();',
  26738. '']));
  26739. end;
  26740. procedure TTestModule.TestTypeHelper_NestedSelf;
  26741. begin
  26742. StartProgram(false);
  26743. Add([
  26744. '{$modeswitch typehelpers}',
  26745. 'type',
  26746. ' THelper = type helper for string',
  26747. ' procedure Run(Value: string);',
  26748. ' end;',
  26749. 'procedure THelper.Run(Value: string);',
  26750. ' function Sub(i: nativeint): boolean;',
  26751. ' begin',
  26752. ' Result:=Self[i+1]=Value[i];',
  26753. ' end;',
  26754. 'begin',
  26755. ' if Self[3]=Value[4] then ;',
  26756. 'end;',
  26757. 'begin',
  26758. '']);
  26759. ConvertProgram;
  26760. CheckSource('TestTypeHelper_NestedSelf',
  26761. LinesToStr([ // statements
  26762. 'rtl.createHelper(this, "THelper", null, function () {',
  26763. ' this.Run = function (Value) {',
  26764. ' var $Self = this;',
  26765. ' function Sub(i) {',
  26766. ' var Result = false;',
  26767. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  26768. ' return Result;',
  26769. ' };',
  26770. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  26771. ' };',
  26772. '});',
  26773. '']),
  26774. LinesToStr([ // $mod.$main
  26775. '']));
  26776. end;
  26777. procedure TTestModule.TestProcType;
  26778. begin
  26779. StartProgram(false);
  26780. Add([
  26781. 'type',
  26782. ' TProcInt = procedure(vI: longint = 1);',
  26783. 'procedure DoIt(vJ: longint);',
  26784. 'begin end;',
  26785. 'var',
  26786. ' b: boolean;',
  26787. ' vP, vQ: tprocint;',
  26788. 'begin',
  26789. ' vp:=nil;',
  26790. ' vp:=vp;',
  26791. ' vp:=@doit;',
  26792. ' vp;',
  26793. ' vp();',
  26794. ' vp(2);',
  26795. ' b:=vp=nil;',
  26796. ' b:=nil=vp;',
  26797. ' b:=vp=vq;',
  26798. ' b:=vp=@doit;',
  26799. ' b:=@doit=vp;',
  26800. ' b:=vp<>nil;',
  26801. ' b:=nil<>vp;',
  26802. ' b:=vp<>vq;',
  26803. ' b:=vp<>@doit;',
  26804. ' b:=@doit<>vp;',
  26805. ' b:=Assigned(vp);',
  26806. ' if Assigned(vp) then ;']);
  26807. ConvertProgram;
  26808. CheckSource('TestProcType',
  26809. LinesToStr([ // statements
  26810. 'this.DoIt = function(vJ) {',
  26811. '};',
  26812. 'this.b = false;',
  26813. 'this.vP = null;',
  26814. 'this.vQ = null;'
  26815. ]),
  26816. LinesToStr([ // $mod.$main
  26817. '$mod.vP = null;',
  26818. '$mod.vP = $mod.vP;',
  26819. '$mod.vP = $mod.DoIt;',
  26820. '$mod.vP(1);',
  26821. '$mod.vP(1);',
  26822. '$mod.vP(2);',
  26823. '$mod.b = $mod.vP === null;',
  26824. '$mod.b = null === $mod.vP;',
  26825. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  26826. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26827. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26828. '$mod.b = $mod.vP !== null;',
  26829. '$mod.b = null !== $mod.vP;',
  26830. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  26831. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26832. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26833. '$mod.b = $mod.vP != null;',
  26834. 'if ($mod.vP != null) ;',
  26835. '']));
  26836. end;
  26837. procedure TTestModule.TestProcType_Arg;
  26838. begin
  26839. StartProgram(false);
  26840. Add([
  26841. 'type',
  26842. ' TProcInt = procedure(vI: longint = 1);',
  26843. 'procedure DoIt(vJ: longint); begin end;',
  26844. 'procedure DoSome(vP, vQ: TProcInt);',
  26845. 'var',
  26846. ' b: boolean;',
  26847. 'begin',
  26848. ' vp:=nil;',
  26849. ' vp:=vp;',
  26850. ' vp:=@doit;',
  26851. ' vp;',
  26852. ' vp();',
  26853. ' vp(2);',
  26854. ' b:=vp=nil;',
  26855. ' b:=nil=vp;',
  26856. ' b:=vp=vq;',
  26857. ' b:=vp=@doit;',
  26858. ' b:=@doit=vp;',
  26859. ' b:=vp<>nil;',
  26860. ' b:=nil<>vp;',
  26861. ' b:=vp<>vq;',
  26862. ' b:=vp<>@doit;',
  26863. ' b:=@doit<>vp;',
  26864. ' b:=Assigned(vp);',
  26865. ' if Assigned(vp) then ;',
  26866. 'end;',
  26867. 'begin',
  26868. ' DoSome(@DoIt,nil);']);
  26869. ConvertProgram;
  26870. CheckSource('TestProcType_Arg',
  26871. LinesToStr([ // statements
  26872. 'this.DoIt = function(vJ) {',
  26873. '};',
  26874. 'this.DoSome = function(vP, vQ) {',
  26875. ' var b = false;',
  26876. ' vP = null;',
  26877. ' vP = vP;',
  26878. ' vP = $mod.DoIt;',
  26879. ' vP(1);',
  26880. ' vP(1);',
  26881. ' vP(2);',
  26882. ' b = vP === null;',
  26883. ' b = null === vP;',
  26884. ' b = rtl.eqCallback(vP,vQ);',
  26885. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  26886. ' b = rtl.eqCallback($mod.DoIt, vP);',
  26887. ' b = vP !== null;',
  26888. ' b = null !== vP;',
  26889. ' b = !rtl.eqCallback(vP, vQ);',
  26890. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  26891. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  26892. ' b = vP != null;',
  26893. ' if (vP != null) ;',
  26894. '};',
  26895. '']),
  26896. LinesToStr([ // $mod.$main
  26897. '$mod.DoSome($mod.DoIt,null);',
  26898. '']));
  26899. end;
  26900. procedure TTestModule.TestProcType_FunctionFPC;
  26901. begin
  26902. StartProgram(false);
  26903. Add('type');
  26904. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26905. Add('function DoIt(vI: longint): longint;');
  26906. Add('begin end;');
  26907. Add('var');
  26908. Add(' b: boolean;');
  26909. Add(' vP, vQ: tfuncint;');
  26910. Add('begin');
  26911. Add(' vp:=nil;');
  26912. Add(' vp:=vp;');
  26913. Add(' vp:=@doit;'); // ok in fpc and delphi
  26914. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26915. Add(' vp;'); // ok in fpc and delphi
  26916. Add(' vp();');
  26917. Add(' vp(2);');
  26918. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26919. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26920. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26921. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26922. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26923. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  26924. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  26925. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26926. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26927. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26928. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26929. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26930. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  26931. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  26932. Add(' b:=Assigned(vp);');
  26933. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  26934. Add(' doit(vp());'); // ok in fpc and delphi
  26935. Add(' doit(vp(2));'); // ok in fpc and delphi
  26936. ConvertProgram;
  26937. CheckSource('TestProcType_FunctionFPC',
  26938. LinesToStr([ // statements
  26939. 'this.DoIt = function(vI) {',
  26940. ' var Result = 0;',
  26941. ' return Result;',
  26942. '};',
  26943. 'this.b = false;',
  26944. 'this.vP = null;',
  26945. 'this.vQ = null;'
  26946. ]),
  26947. LinesToStr([ // $mod.$main
  26948. '$mod.vP = null;',
  26949. '$mod.vP = $mod.vP;',
  26950. '$mod.vP = $mod.DoIt;',
  26951. '$mod.vP(1);',
  26952. '$mod.vP(1);',
  26953. '$mod.vP(2);',
  26954. '$mod.b = $mod.vP === null;',
  26955. '$mod.b = null === $mod.vP;',
  26956. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  26957. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26958. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26959. '$mod.b = 4 === $mod.vP(1);',
  26960. '$mod.b = $mod.vP !== null;',
  26961. '$mod.b = null !== $mod.vP;',
  26962. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  26963. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26964. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26965. '$mod.b = 6 !== $mod.vP(1);',
  26966. '$mod.b = $mod.vP != null;',
  26967. '$mod.DoIt($mod.vP(1));',
  26968. '$mod.DoIt($mod.vP(2));',
  26969. '']));
  26970. end;
  26971. procedure TTestModule.TestProcType_FunctionDelphi;
  26972. begin
  26973. StartProgram(false);
  26974. Add('{$mode Delphi}');
  26975. Add('type');
  26976. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26977. Add('function DoIt(vI: longint): longint;');
  26978. Add('begin end;');
  26979. Add('var');
  26980. Add(' b: boolean;');
  26981. Add(' vP, vQ: tfuncint;');
  26982. Add('begin');
  26983. Add(' vp:=nil;');
  26984. Add(' vp:=vp;');
  26985. Add(' vp:=@doit;'); // ok in fpc and delphi
  26986. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26987. Add(' vp;'); // ok in fpc and delphi
  26988. Add(' vp();');
  26989. Add(' vp(2);');
  26990. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26991. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26992. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26993. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26994. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26995. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  26996. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  26997. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26998. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26999. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27000. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27001. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27002. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27003. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27004. Add(' b:=Assigned(vp);');
  27005. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27006. Add(' doit(vp());'); // ok in fpc and delphi
  27007. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27008. ConvertProgram;
  27009. CheckSource('TestProcType_FunctionDelphi',
  27010. LinesToStr([ // statements
  27011. 'this.DoIt = function(vI) {',
  27012. ' var Result = 0;',
  27013. ' return Result;',
  27014. '};',
  27015. 'this.b = false;',
  27016. 'this.vP = null;',
  27017. 'this.vQ = null;'
  27018. ]),
  27019. LinesToStr([ // $mod.$main
  27020. '$mod.vP = null;',
  27021. '$mod.vP = $mod.vP;',
  27022. '$mod.vP = $mod.DoIt;',
  27023. '$mod.vP = $mod.DoIt;',
  27024. '$mod.vP(1);',
  27025. '$mod.vP(1);',
  27026. '$mod.vP(2);',
  27027. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27028. '$mod.b = $mod.vP(1) === 3;',
  27029. '$mod.b = 4 === $mod.vP(1);',
  27030. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27031. '$mod.b = $mod.vP(1) !== 5;',
  27032. '$mod.b = 6 !== $mod.vP(1);',
  27033. '$mod.b = $mod.vP != null;',
  27034. '$mod.DoIt($mod.vP(1));',
  27035. '$mod.DoIt($mod.vP(1));',
  27036. '$mod.DoIt($mod.vP(2));',
  27037. '']));
  27038. end;
  27039. procedure TTestModule.TestProcType_ProcedureDelphi;
  27040. begin
  27041. StartProgram(false);
  27042. Add('{$mode Delphi}');
  27043. Add('type');
  27044. Add(' TProc = procedure;');
  27045. Add('procedure DoIt;');
  27046. Add('begin end;');
  27047. Add('var');
  27048. Add(' b: boolean;');
  27049. Add(' vP, vQ: tproc;');
  27050. Add('begin');
  27051. Add(' vp:=nil;');
  27052. Add(' vp:=vp;');
  27053. Add(' vp:=vq;');
  27054. 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
  27055. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27056. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27057. Add(' vp;'); // ok in fpc and delphi
  27058. Add(' vp();');
  27059. // equal
  27060. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27061. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27062. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27063. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27064. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27065. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27066. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27067. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27068. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27069. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27070. // unequal
  27071. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27072. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27073. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27074. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27075. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27076. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27077. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27078. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27079. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27080. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27081. Add(' b:=Assigned(vp);');
  27082. ConvertProgram;
  27083. CheckSource('TestProcType_ProcedureDelphi',
  27084. LinesToStr([ // statements
  27085. 'this.DoIt = function() {',
  27086. '};',
  27087. 'this.b = false;',
  27088. 'this.vP = null;',
  27089. 'this.vQ = null;'
  27090. ]),
  27091. LinesToStr([ // $mod.$main
  27092. '$mod.vP = null;',
  27093. '$mod.vP = $mod.vP;',
  27094. '$mod.vP = $mod.vQ;',
  27095. '$mod.vP = $mod.DoIt;',
  27096. '$mod.vP = $mod.DoIt;',
  27097. '$mod.vP();',
  27098. '$mod.vP();',
  27099. '$mod.b = $mod.vP === null;',
  27100. '$mod.b = null === $mod.vP;',
  27101. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27102. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27103. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27104. '$mod.b = $mod.vP !== null;',
  27105. '$mod.b = null !== $mod.vP;',
  27106. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27107. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27108. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27109. '$mod.b = $mod.vP != null;',
  27110. '']));
  27111. end;
  27112. procedure TTestModule.TestProcType_AsParam;
  27113. begin
  27114. StartProgram(false);
  27115. Add('type');
  27116. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27117. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27118. Add('var vJ: tfuncint;');
  27119. Add('begin');
  27120. Add(' vg:=vg;');
  27121. Add(' vj:=vh;');
  27122. Add(' vi:=vi;');
  27123. Add(' doit(vg,vg,vg);');
  27124. Add(' doit(vh,vh,vj);');
  27125. Add(' doit(vi,vi,vi);');
  27126. Add(' doit(vj,vj,vj);');
  27127. Add('end;');
  27128. Add('var i: tfuncint;');
  27129. Add('begin');
  27130. Add(' doit(i,i,i);');
  27131. ConvertProgram;
  27132. CheckSource('TestProcType_AsParam',
  27133. LinesToStr([ // statements
  27134. 'this.DoIt = function (vG,vH,vI) {',
  27135. ' var vJ = null;',
  27136. ' vG = vG;',
  27137. ' vJ = vH;',
  27138. ' vI.set(vI.get());',
  27139. ' $mod.DoIt(vG, vG, {',
  27140. ' get: function () {',
  27141. ' return vG;',
  27142. ' },',
  27143. ' set: function (v) {',
  27144. ' vG = v;',
  27145. ' }',
  27146. ' });',
  27147. ' $mod.DoIt(vH, vH, {',
  27148. ' get: function () {',
  27149. ' return vJ;',
  27150. ' },',
  27151. ' set: function (v) {',
  27152. ' vJ = v;',
  27153. ' }',
  27154. ' });',
  27155. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27156. ' $mod.DoIt(vJ, vJ, {',
  27157. ' get: function () {',
  27158. ' return vJ;',
  27159. ' },',
  27160. ' set: function (v) {',
  27161. ' vJ = v;',
  27162. ' }',
  27163. ' });',
  27164. '};',
  27165. 'this.i = null;'
  27166. ]),
  27167. LinesToStr([
  27168. '$mod.DoIt($mod.i,$mod.i,{',
  27169. ' p: $mod,',
  27170. ' get: function () {',
  27171. ' return this.p.i;',
  27172. ' },',
  27173. ' set: function (v) {',
  27174. ' this.p.i = v;',
  27175. ' }',
  27176. '});'
  27177. ]));
  27178. end;
  27179. procedure TTestModule.TestProcType_MethodFPC;
  27180. begin
  27181. StartProgram(false);
  27182. Add('type');
  27183. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27184. Add(' TObject = class');
  27185. Add(' function DoIt(vA: longint = 1): longint;');
  27186. Add(' end;');
  27187. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27188. Add('begin');
  27189. Add('end;');
  27190. Add('var');
  27191. Add(' Obj: TObject;');
  27192. Add(' vP: tfuncint;');
  27193. Add(' b: boolean;');
  27194. Add('begin');
  27195. Add(' vp:[email protected];'); // ok in fpc and delphi
  27196. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27197. Add(' vp;'); // ok in fpc and delphi
  27198. Add(' vp();');
  27199. Add(' vp(2);');
  27200. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27201. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27202. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27203. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27204. ConvertProgram;
  27205. CheckSource('TestProcType_MethodFPC',
  27206. LinesToStr([ // statements
  27207. 'rtl.createClass(this, "TObject", null, function () {',
  27208. ' this.$init = function () {',
  27209. ' };',
  27210. ' this.$final = function () {',
  27211. ' };',
  27212. ' this.DoIt = function (vA) {',
  27213. ' var Result = 0;',
  27214. ' return Result;',
  27215. ' };',
  27216. '});',
  27217. 'this.Obj = null;',
  27218. 'this.vP = null;',
  27219. 'this.b = false;'
  27220. ]),
  27221. LinesToStr([
  27222. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27223. '$mod.vP(1);',
  27224. '$mod.vP(1);',
  27225. '$mod.vP(2);',
  27226. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27227. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27228. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27229. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27230. '']));
  27231. end;
  27232. procedure TTestModule.TestProcType_MethodDelphi;
  27233. begin
  27234. StartProgram(false);
  27235. Add([
  27236. '{$mode delphi}',
  27237. 'type',
  27238. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27239. ' TObject = class',
  27240. ' function DoIt(vA: longint = 1): longint;',
  27241. ' end;',
  27242. 'function TObject.DoIt(vA: longint = 1): longint;',
  27243. 'begin',
  27244. 'end;',
  27245. 'var',
  27246. ' Obj: TObject;',
  27247. ' vP: tfuncint;',
  27248. ' b: boolean;',
  27249. 'begin',
  27250. ' vp:[email protected];', // ok in fpc and delphi
  27251. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27252. ' vp;', // ok in fpc and delphi
  27253. ' vp();',
  27254. ' vp(2);',
  27255. //' b:[email protected];', // ok in fpc, illegal in delphi
  27256. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27257. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27258. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27259. '']);
  27260. ConvertProgram;
  27261. CheckSource('TestProcType_MethodDelphi',
  27262. LinesToStr([ // statements
  27263. 'rtl.createClass(this, "TObject", null, function () {',
  27264. ' this.$init = function () {',
  27265. ' };',
  27266. ' this.$final = function () {',
  27267. ' };',
  27268. ' this.DoIt = function (vA) {',
  27269. ' var Result = 0;',
  27270. ' return Result;',
  27271. ' };',
  27272. '});',
  27273. 'this.Obj = null;',
  27274. 'this.vP = null;',
  27275. 'this.b = false;'
  27276. ]),
  27277. LinesToStr([
  27278. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27279. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27280. '$mod.vP(1);',
  27281. '$mod.vP(1);',
  27282. '$mod.vP(2);',
  27283. '']));
  27284. end;
  27285. procedure TTestModule.TestProcType_PropertyFPC;
  27286. begin
  27287. StartProgram(false);
  27288. Add('type');
  27289. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27290. Add(' TObject = class');
  27291. Add(' FOnFoo: TFuncInt;');
  27292. Add(' function DoIt(vA: longint = 1): longint;');
  27293. Add(' function GetFoo: TFuncInt;');
  27294. Add(' procedure SetFoo(const Value: TFuncInt);');
  27295. Add(' function GetEvents(Index: longint): TFuncInt;');
  27296. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27297. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27298. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27299. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27300. Add(' end;');
  27301. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27302. Add('function tobject.getfoo: tfuncint; begin end;');
  27303. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27304. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27305. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27306. Add('var');
  27307. Add(' Obj: TObject;');
  27308. Add(' vP: tfuncint;');
  27309. Add(' b: boolean;');
  27310. Add('begin');
  27311. Add(' obj.onfoo:=nil;');
  27312. Add(' obj.onbar:=nil;');
  27313. Add(' obj.events[1]:=nil;');
  27314. Add(' obj.onfoo:=obj.onfoo;');
  27315. Add(' obj.onbar:=obj.onbar;');
  27316. Add(' obj.events[2]:=obj.events[3];');
  27317. Add(' obj.onfoo:[email protected];');
  27318. Add(' obj.onbar:[email protected];');
  27319. Add(' obj.events[4]:[email protected];');
  27320. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27321. //Add(' obj.onbar:=obj.doit;'); // delphi
  27322. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27323. Add(' obj.onfoo;');
  27324. Add(' obj.onbar;');
  27325. //Add(' obj.events[5];'); ToDo in pasresolver
  27326. Add(' obj.onfoo();');
  27327. Add(' obj.onbar();');
  27328. Add(' obj.events[6]();');
  27329. Add(' b:=obj.onfoo=nil;');
  27330. Add(' b:=obj.onbar=nil;');
  27331. Add(' b:=obj.events[7]=nil;');
  27332. Add(' b:=obj.onfoo<>nil;');
  27333. Add(' b:=obj.onbar<>nil;');
  27334. Add(' b:=obj.events[8]<>nil;');
  27335. Add(' b:=obj.onfoo=vp;');
  27336. Add(' b:=obj.onbar=vp;');
  27337. Add(' b:=obj.events[9]=vp;');
  27338. Add(' b:=obj.onfoo=obj.onfoo;');
  27339. Add(' b:=obj.onbar=obj.onfoo;');
  27340. Add(' b:=obj.events[10]=obj.onfoo;');
  27341. Add(' b:=obj.onfoo<>obj.onfoo;');
  27342. Add(' b:=obj.onbar<>obj.onfoo;');
  27343. Add(' b:=obj.events[11]<>obj.onfoo;');
  27344. Add(' b:[email protected];');
  27345. Add(' b:[email protected];');
  27346. Add(' b:=obj.events[12][email protected];');
  27347. Add(' b:=obj.onfoo<>@obj.doit;');
  27348. Add(' b:=obj.onbar<>@obj.doit;');
  27349. Add(' b:=obj.events[12]<>@obj.doit;');
  27350. Add(' b:=Assigned(obj.onfoo);');
  27351. Add(' b:=Assigned(obj.onbar);');
  27352. Add(' b:=Assigned(obj.events[13]);');
  27353. ConvertProgram;
  27354. CheckSource('TestProcType_PropertyFPC',
  27355. LinesToStr([ // statements
  27356. 'rtl.createClass(this, "TObject", null, function () {',
  27357. ' this.$init = function () {',
  27358. ' this.FOnFoo = null;',
  27359. ' };',
  27360. ' this.$final = function () {',
  27361. ' this.FOnFoo = undefined;',
  27362. ' };',
  27363. ' this.DoIt = function (vA) {',
  27364. ' var Result = 0;',
  27365. ' return Result;',
  27366. ' };',
  27367. 'this.GetFoo = function () {',
  27368. ' var Result = null;',
  27369. ' return Result;',
  27370. '};',
  27371. 'this.SetFoo = function (Value) {',
  27372. '};',
  27373. 'this.GetEvents = function (Index) {',
  27374. ' var Result = null;',
  27375. ' return Result;',
  27376. '};',
  27377. 'this.SetEvents = function (Index, Value) {',
  27378. '};',
  27379. '});',
  27380. 'this.Obj = null;',
  27381. 'this.vP = null;',
  27382. 'this.b = false;'
  27383. ]),
  27384. LinesToStr([
  27385. '$mod.Obj.FOnFoo = null;',
  27386. '$mod.Obj.SetFoo(null);',
  27387. '$mod.Obj.SetEvents(1, null);',
  27388. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27389. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27390. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27391. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27392. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27393. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27394. '$mod.Obj.FOnFoo(1);',
  27395. '$mod.Obj.GetFoo();',
  27396. '$mod.Obj.FOnFoo(1);',
  27397. '$mod.Obj.GetFoo()(1);',
  27398. '$mod.Obj.GetEvents(6)(1);',
  27399. '$mod.b = $mod.Obj.FOnFoo === null;',
  27400. '$mod.b = $mod.Obj.GetFoo() === null;',
  27401. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27402. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27403. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27404. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27405. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27406. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27407. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27408. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27409. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27410. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27411. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27412. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27413. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27414. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27415. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27416. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27417. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27418. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27419. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27420. '$mod.b = $mod.Obj.FOnFoo != null;',
  27421. '$mod.b = $mod.Obj.GetFoo() != null;',
  27422. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27423. '']));
  27424. end;
  27425. procedure TTestModule.TestProcType_PropertyDelphi;
  27426. begin
  27427. StartProgram(false);
  27428. Add('{$mode delphi}');
  27429. Add('type');
  27430. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27431. Add(' TObject = class');
  27432. Add(' FOnFoo: TFuncInt;');
  27433. Add(' function DoIt(vA: longint = 1): longint;');
  27434. Add(' function GetFoo: TFuncInt;');
  27435. Add(' procedure SetFoo(const Value: TFuncInt);');
  27436. Add(' function GetEvents(Index: longint): TFuncInt;');
  27437. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27438. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27439. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27440. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27441. Add(' end;');
  27442. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27443. Add('function tobject.getfoo: tfuncint; begin end;');
  27444. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27445. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27446. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27447. Add('var');
  27448. Add(' Obj: TObject;');
  27449. Add(' vP: tfuncint;');
  27450. Add(' b: boolean;');
  27451. Add('begin');
  27452. Add(' obj.onfoo:=nil;');
  27453. Add(' obj.onbar:=nil;');
  27454. Add(' obj.events[1]:=nil;');
  27455. Add(' obj.onfoo:=obj.onfoo;');
  27456. Add(' obj.onbar:=obj.onbar;');
  27457. Add(' obj.events[2]:=obj.events[3];');
  27458. Add(' obj.onfoo:[email protected];');
  27459. Add(' obj.onbar:[email protected];');
  27460. Add(' obj.events[4]:[email protected];');
  27461. Add(' obj.onfoo:=obj.doit;'); // delphi
  27462. Add(' obj.onbar:=obj.doit;'); // delphi
  27463. Add(' obj.events[4]:=obj.doit;'); // delphi
  27464. Add(' obj.onfoo;');
  27465. Add(' obj.onbar;');
  27466. //Add(' obj.events[5];'); ToDo in pasresolver
  27467. Add(' obj.onfoo();');
  27468. Add(' obj.onbar();');
  27469. Add(' obj.events[6]();');
  27470. //Add(' b:=obj.onfoo=nil;'); // fpc
  27471. //Add(' b:=obj.onbar=nil;'); // fpc
  27472. //Add(' b:=obj.events[7]=nil;'); // fpc
  27473. //Add(' b:=obj.onfoo<>nil;'); // fpc
  27474. //Add(' b:=obj.onbar<>nil;'); // fpc
  27475. //Add(' b:=obj.events[8]<>nil;'); // fpc
  27476. Add(' b:=obj.onfoo=vp;');
  27477. Add(' b:=obj.onbar=vp;');
  27478. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  27479. Add(' b:=obj.onfoo=obj.onfoo;');
  27480. Add(' b:=obj.onbar=obj.onfoo;');
  27481. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  27482. Add(' b:=obj.onfoo<>obj.onfoo;');
  27483. Add(' b:=obj.onbar<>obj.onfoo;');
  27484. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  27485. //Add(' b:[email protected];'); // fpc
  27486. //Add(' b:[email protected];'); // fpc
  27487. //Add(' b:=obj.events[12][email protected];'); // fpc
  27488. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  27489. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  27490. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  27491. Add(' b:=Assigned(obj.onfoo);');
  27492. Add(' b:=Assigned(obj.onbar);');
  27493. Add(' b:=Assigned(obj.events[13]);');
  27494. ConvertProgram;
  27495. CheckSource('TestProcType_PropertyDelphi',
  27496. LinesToStr([ // statements
  27497. 'rtl.createClass(this, "TObject", null, function () {',
  27498. ' this.$init = function () {',
  27499. ' this.FOnFoo = null;',
  27500. ' };',
  27501. ' this.$final = function () {',
  27502. ' this.FOnFoo = undefined;',
  27503. ' };',
  27504. ' this.DoIt = function (vA) {',
  27505. ' var Result = 0;',
  27506. ' return Result;',
  27507. ' };',
  27508. 'this.GetFoo = function () {',
  27509. ' var Result = null;',
  27510. ' return Result;',
  27511. '};',
  27512. 'this.SetFoo = function (Value) {',
  27513. '};',
  27514. 'this.GetEvents = function (Index) {',
  27515. ' var Result = null;',
  27516. ' return Result;',
  27517. '};',
  27518. 'this.SetEvents = function (Index, Value) {',
  27519. '};',
  27520. '});',
  27521. 'this.Obj = null;',
  27522. 'this.vP = null;',
  27523. 'this.b = false;'
  27524. ]),
  27525. LinesToStr([
  27526. '$mod.Obj.FOnFoo = null;',
  27527. '$mod.Obj.SetFoo(null);',
  27528. '$mod.Obj.SetEvents(1, null);',
  27529. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27530. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27531. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27532. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27533. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27534. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27535. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27536. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27537. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27538. '$mod.Obj.FOnFoo(1);',
  27539. '$mod.Obj.GetFoo();',
  27540. '$mod.Obj.FOnFoo(1);',
  27541. '$mod.Obj.GetFoo()(1);',
  27542. '$mod.Obj.GetEvents(6)(1);',
  27543. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  27544. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  27545. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  27546. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  27547. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  27548. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  27549. '$mod.b = $mod.Obj.FOnFoo != null;',
  27550. '$mod.b = $mod.Obj.GetFoo() != null;',
  27551. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  27552. '']));
  27553. end;
  27554. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  27555. begin
  27556. StartProgram(false);
  27557. Add('type');
  27558. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27559. Add(' TObject = class');
  27560. Add(' FOnFoo: TFuncInt;');
  27561. Add(' function DoIt(vA: longint = 1): longint;');
  27562. Add(' function GetFoo: TFuncInt;');
  27563. Add(' procedure SetFoo(const Value: TFuncInt);');
  27564. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27565. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27566. Add(' end;');
  27567. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27568. Add('function tobject.getfoo: tfuncint; begin end;');
  27569. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27570. Add('var');
  27571. Add(' Obj: TObject;');
  27572. Add(' vP: tfuncint;');
  27573. Add(' b: boolean;');
  27574. Add('begin');
  27575. Add('with obj do begin');
  27576. Add(' fonfoo:=nil;');
  27577. Add(' onfoo:=nil;');
  27578. Add(' onbar:=nil;');
  27579. Add(' fonfoo:=fonfoo;');
  27580. Add(' onfoo:=onfoo;');
  27581. Add(' onbar:=onbar;');
  27582. Add(' fonfoo:=@doit;');
  27583. Add(' onfoo:=@doit;');
  27584. Add(' onbar:=@doit;');
  27585. //Add(' fonfoo:=doit;'); // delphi
  27586. //Add(' onfoo:=doit;'); // delphi
  27587. //Add(' onbar:=doit;'); // delphi
  27588. Add(' fonfoo;');
  27589. Add(' onfoo;');
  27590. Add(' onbar;');
  27591. Add(' fonfoo();');
  27592. Add(' onfoo();');
  27593. Add(' onbar();');
  27594. Add(' b:=fonfoo=nil;');
  27595. Add(' b:=onfoo=nil;');
  27596. Add(' b:=onbar=nil;');
  27597. Add(' b:=fonfoo<>nil;');
  27598. Add(' b:=onfoo<>nil;');
  27599. Add(' b:=onbar<>nil;');
  27600. Add(' b:=fonfoo=vp;');
  27601. Add(' b:=onfoo=vp;');
  27602. Add(' b:=onbar=vp;');
  27603. Add(' b:=fonfoo=fonfoo;');
  27604. Add(' b:=onfoo=onfoo;');
  27605. Add(' b:=onbar=onfoo;');
  27606. Add(' b:=fonfoo<>fonfoo;');
  27607. Add(' b:=onfoo<>onfoo;');
  27608. Add(' b:=onbar<>onfoo;');
  27609. Add(' b:=fonfoo=@doit;');
  27610. Add(' b:=onfoo=@doit;');
  27611. Add(' b:=onbar=@doit;');
  27612. Add(' b:=fonfoo<>@doit;');
  27613. Add(' b:=onfoo<>@doit;');
  27614. Add(' b:=onbar<>@doit;');
  27615. Add(' b:=Assigned(fonfoo);');
  27616. Add(' b:=Assigned(onfoo);');
  27617. Add(' b:=Assigned(onbar);');
  27618. Add('end;');
  27619. ConvertProgram;
  27620. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  27621. LinesToStr([ // statements
  27622. 'rtl.createClass(this, "TObject", null, function () {',
  27623. ' this.$init = function () {',
  27624. ' this.FOnFoo = null;',
  27625. ' };',
  27626. ' this.$final = function () {',
  27627. ' this.FOnFoo = undefined;',
  27628. ' };',
  27629. ' this.DoIt = function (vA) {',
  27630. ' var Result = 0;',
  27631. ' return Result;',
  27632. ' };',
  27633. ' this.GetFoo = function () {',
  27634. ' var Result = null;',
  27635. ' return Result;',
  27636. ' };',
  27637. ' this.SetFoo = function (Value) {',
  27638. ' };',
  27639. '});',
  27640. 'this.Obj = null;',
  27641. 'this.vP = null;',
  27642. 'this.b = false;'
  27643. ]),
  27644. LinesToStr([
  27645. 'var $with = $mod.Obj;',
  27646. '$with.FOnFoo = null;',
  27647. '$with.FOnFoo = null;',
  27648. '$with.SetFoo(null);',
  27649. '$with.FOnFoo = $with.FOnFoo;',
  27650. '$with.FOnFoo = $with.FOnFoo;',
  27651. '$with.SetFoo($with.GetFoo());',
  27652. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  27653. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  27654. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  27655. '$with.FOnFoo(1);',
  27656. '$with.FOnFoo(1);',
  27657. '$with.GetFoo();',
  27658. '$with.FOnFoo(1);',
  27659. '$with.FOnFoo(1);',
  27660. '$with.GetFoo()(1);',
  27661. '$mod.b = $with.FOnFoo === null;',
  27662. '$mod.b = $with.FOnFoo === null;',
  27663. '$mod.b = $with.GetFoo() === null;',
  27664. '$mod.b = $with.FOnFoo !== null;',
  27665. '$mod.b = $with.FOnFoo !== null;',
  27666. '$mod.b = $with.GetFoo() !== null;',
  27667. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  27668. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  27669. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  27670. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27671. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27672. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  27673. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27674. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  27675. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  27676. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27677. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27678. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  27679. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27680. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  27681. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  27682. '$mod.b = $with.FOnFoo != null;',
  27683. '$mod.b = $with.FOnFoo != null;',
  27684. '$mod.b = $with.GetFoo() != null;',
  27685. '']));
  27686. end;
  27687. procedure TTestModule.TestProcType_Nested;
  27688. begin
  27689. StartProgram(false);
  27690. Add([
  27691. 'type',
  27692. ' TProcInt = procedure(vI: longint = 1);',
  27693. 'procedure DoIt(vJ: longint);',
  27694. 'var aProc: TProcInt;',
  27695. ' b: boolean;',
  27696. ' procedure Sub(vK: longint);',
  27697. ' var aSub: TProcInt;',
  27698. ' procedure SubSub(vK: longint);',
  27699. ' var aSubSub: TProcInt;',
  27700. ' begin;',
  27701. ' aProc:=@DoIt;',
  27702. ' aSub:=@DoIt;',
  27703. ' aSubSub:=@DoIt;',
  27704. ' aProc:=@Sub;',
  27705. ' aSub:=@Sub;',
  27706. ' aSubSub:=@Sub;',
  27707. ' aProc:=@SubSub;',
  27708. ' aSub:=@SubSub;',
  27709. ' aSubSub:=@SubSub;',
  27710. ' end;',
  27711. ' begin;',
  27712. ' end;',
  27713. 'begin;',
  27714. ' aProc:=@Sub;',
  27715. ' b:=aProc=@Sub;',
  27716. ' b:=@Sub=aProc;',
  27717. 'end;',
  27718. 'begin',
  27719. '']);
  27720. ConvertProgram;
  27721. CheckSource('TestProcType_Nested',
  27722. LinesToStr([ // statements
  27723. 'this.DoIt = function (vJ) {',
  27724. ' var aProc = null;',
  27725. ' var b = false;',
  27726. ' function Sub(vK) {',
  27727. ' var aSub = null;',
  27728. ' function SubSub(vK) {',
  27729. ' var aSubSub = null;',
  27730. ' aProc = $mod.DoIt;',
  27731. ' aSub = $mod.DoIt;',
  27732. ' aSubSub = $mod.DoIt;',
  27733. ' aProc = Sub;',
  27734. ' aSub = Sub;',
  27735. ' aSubSub = Sub;',
  27736. ' aProc = SubSub;',
  27737. ' aSub = SubSub;',
  27738. ' aSubSub = SubSub;',
  27739. ' };',
  27740. ' };',
  27741. ' aProc = Sub;',
  27742. ' b = rtl.eqCallback(aProc, Sub);',
  27743. ' b = rtl.eqCallback(Sub, aProc);',
  27744. '};',
  27745. '']),
  27746. LinesToStr([ // $mod.$main
  27747. '']));
  27748. end;
  27749. procedure TTestModule.TestProcType_NestedOfObject;
  27750. begin
  27751. StartProgram(false);
  27752. Add([
  27753. 'type',
  27754. ' TProcInt = procedure(vI: longint = 1) of object;',
  27755. ' TObject = class',
  27756. ' procedure DoIt(vJ: longint);',
  27757. ' end;',
  27758. 'procedure TObject.DoIt(vJ: longint);',
  27759. 'var aProc: TProcInt;',
  27760. ' b: boolean;',
  27761. ' procedure Sub(vK: longint);',
  27762. ' var aSub: TProcInt;',
  27763. ' procedure SubSub(vK: longint);',
  27764. ' var aSubSub: TProcInt;',
  27765. ' begin;',
  27766. ' aProc:=@DoIt;',
  27767. ' aSub:=@DoIt;',
  27768. ' aSubSub:=@DoIt;',
  27769. ' aProc:=@Sub;',
  27770. ' aSub:=@Sub;',
  27771. ' aSubSub:=@Sub;',
  27772. ' aProc:=@SubSub;',
  27773. ' aSub:=@SubSub;',
  27774. ' aSubSub:=@SubSub;',
  27775. ' end;',
  27776. ' begin;',
  27777. ' end;',
  27778. 'begin;',
  27779. ' aProc:=@Sub;',
  27780. ' b:=aProc=@Sub;',
  27781. ' b:=@Sub=aProc;',
  27782. 'end;',
  27783. 'begin',
  27784. '']);
  27785. ConvertProgram;
  27786. CheckSource('TestProcType_Nested',
  27787. LinesToStr([ // statements
  27788. 'rtl.createClass(this, "TObject", null, function () {',
  27789. ' this.$init = function () {',
  27790. ' };',
  27791. ' this.$final = function () {',
  27792. ' };',
  27793. ' this.DoIt = function (vJ) {',
  27794. ' var $Self = this;',
  27795. ' var aProc = null;',
  27796. ' var b = false;',
  27797. ' function Sub(vK) {',
  27798. ' var aSub = null;',
  27799. ' function SubSub(vK) {',
  27800. ' var aSubSub = null;',
  27801. ' aProc = rtl.createCallback($Self, "DoIt");',
  27802. ' aSub = rtl.createCallback($Self, "DoIt");',
  27803. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  27804. ' aProc = Sub;',
  27805. ' aSub = Sub;',
  27806. ' aSubSub = Sub;',
  27807. ' aProc = SubSub;',
  27808. ' aSub = SubSub;',
  27809. ' aSubSub = SubSub;',
  27810. ' };',
  27811. ' };',
  27812. ' aProc = Sub;',
  27813. ' b = rtl.eqCallback(aProc, Sub);',
  27814. ' b = rtl.eqCallback(Sub, aProc);',
  27815. ' };',
  27816. '});',
  27817. '']),
  27818. LinesToStr([ // $mod.$main
  27819. '']));
  27820. end;
  27821. procedure TTestModule.TestProcType_ReferenceToProc;
  27822. begin
  27823. StartProgram(false);
  27824. Add([
  27825. 'type',
  27826. ' TProcRef = reference to procedure(i: longint = 0);',
  27827. ' TFuncRef = reference to function(i: longint = 0): longint;',
  27828. 'var',
  27829. ' p: TProcRef;',
  27830. ' f: TFuncRef;',
  27831. 'procedure DoIt(i: longint);',
  27832. 'begin',
  27833. 'end;',
  27834. 'function GetIt(i: longint): longint;',
  27835. 'begin',
  27836. ' p:=@DoIt;',
  27837. ' f:=@GetIt;',
  27838. ' f;',
  27839. ' f();',
  27840. ' f(1);',
  27841. 'end;',
  27842. 'begin',
  27843. ' p:=@DoIt;',
  27844. ' f:=@GetIt;',
  27845. ' f;',
  27846. ' f();',
  27847. ' f(1);',
  27848. ' p:=TProcRef(f);',
  27849. '']);
  27850. ConvertProgram;
  27851. CheckSource('TestProcType_ReferenceToProc',
  27852. LinesToStr([ // statements
  27853. 'this.p = null;',
  27854. 'this.f = null;',
  27855. 'this.DoIt = function (i) {',
  27856. '};',
  27857. 'this.GetIt = function (i) {',
  27858. ' var Result = 0;',
  27859. ' $mod.p = $mod.DoIt;',
  27860. ' $mod.f = $mod.GetIt;',
  27861. ' $mod.f(0);',
  27862. ' $mod.f(0);',
  27863. ' $mod.f(1);',
  27864. ' return Result;',
  27865. '};',
  27866. '']),
  27867. LinesToStr([ // $mod.$main
  27868. '$mod.p = $mod.DoIt;',
  27869. '$mod.f = $mod.GetIt;',
  27870. '$mod.f(0);',
  27871. '$mod.f(0);',
  27872. '$mod.f(1);',
  27873. '$mod.p = $mod.f;',
  27874. '']));
  27875. end;
  27876. procedure TTestModule.TestProcType_ReferenceToMethod;
  27877. begin
  27878. StartProgram(false);
  27879. Add([
  27880. 'type',
  27881. ' TFuncRef = reference to function(i: longint = 5): longint;',
  27882. ' TObject = class',
  27883. ' function Grow(s: longint): longint;',
  27884. ' end;',
  27885. 'var',
  27886. ' f: tfuncref;',
  27887. 'function tobject.grow(s: longint): longint;',
  27888. ' function GrowSub(i: longint): longint;',
  27889. ' begin',
  27890. ' f:=@grow;',
  27891. ' f:=@growsub;',
  27892. ' end;',
  27893. 'begin',
  27894. ' f:=@grow;',
  27895. ' f:=@growsub;',
  27896. 'end;',
  27897. 'begin',
  27898. '']);
  27899. ConvertProgram;
  27900. CheckSource('TestProcType_ReferenceToMethod',
  27901. LinesToStr([ // statements
  27902. 'rtl.createClass(this, "TObject", null, function () {',
  27903. ' this.$init = function () {',
  27904. ' };',
  27905. ' this.$final = function () {',
  27906. ' };',
  27907. ' this.Grow = function (s) {',
  27908. ' var $Self = this;',
  27909. ' var Result = 0;',
  27910. ' function GrowSub(i) {',
  27911. ' var Result = 0;',
  27912. ' $mod.f = rtl.createCallback($Self, "Grow");',
  27913. ' $mod.f = GrowSub;',
  27914. ' return Result;',
  27915. ' };',
  27916. ' $mod.f = rtl.createCallback($Self, "Grow");',
  27917. ' $mod.f = GrowSub;',
  27918. ' return Result;',
  27919. ' };',
  27920. '});',
  27921. 'this.f = null;',
  27922. '']),
  27923. LinesToStr([ // $mod.$main
  27924. '']));
  27925. end;
  27926. procedure TTestModule.TestProcType_Typecast;
  27927. begin
  27928. StartProgram(false);
  27929. Add([
  27930. 'type',
  27931. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  27932. ' TEvent = procedure of object;',
  27933. ' TGetter = function:longint of object;',
  27934. ' TProcA = procedure(i: longint);',
  27935. ' TFuncB = function(i, j: longint): longint;',
  27936. 'procedure DoIt(); varargs; begin end;',
  27937. 'var',
  27938. ' Notify: tnotifyevent;',
  27939. ' Event: tevent;',
  27940. ' Getter: tgetter;',
  27941. ' ProcA: tproca;',
  27942. ' FuncB: tfuncb;',
  27943. ' p: pointer;',
  27944. 'begin',
  27945. ' notify:=tnotifyevent(event);',
  27946. ' event:=tevent(event);',
  27947. ' event:=tevent(notify);',
  27948. ' event:=tevent(getter);',
  27949. ' event:=tevent(proca);',
  27950. ' proca:=tproca(funcb);',
  27951. ' funcb:=tfuncb(funcb);',
  27952. ' funcb:=tfuncb(proca);',
  27953. ' funcb:=tfuncb(getter);',
  27954. ' proca:=tproca(p);',
  27955. ' funcb:=tfuncb(p);',
  27956. ' getter:=tgetter(p);',
  27957. ' p:=pointer(notify);',
  27958. ' p:=notify;',
  27959. ' p:=pointer(proca);',
  27960. ' p:=proca;',
  27961. ' p:=pointer(funcb);',
  27962. ' p:=funcb;',
  27963. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  27964. '']);
  27965. ConvertProgram;
  27966. CheckSource('TestProcType_Typecast',
  27967. LinesToStr([ // statements
  27968. 'this.DoIt = function () {',
  27969. '};',
  27970. 'this.Notify = null;',
  27971. 'this.Event = null;',
  27972. 'this.Getter = null;',
  27973. 'this.ProcA = null;',
  27974. 'this.FuncB = null;',
  27975. 'this.p = null;',
  27976. '']),
  27977. LinesToStr([ // $mod.$main
  27978. '$mod.Notify = $mod.Event;',
  27979. '$mod.Event = $mod.Event;',
  27980. '$mod.Event = $mod.Notify;',
  27981. '$mod.Event = $mod.Getter;',
  27982. '$mod.Event = $mod.ProcA;',
  27983. '$mod.ProcA = $mod.FuncB;',
  27984. '$mod.FuncB = $mod.FuncB;',
  27985. '$mod.FuncB = $mod.ProcA;',
  27986. '$mod.FuncB = $mod.Getter;',
  27987. '$mod.ProcA = $mod.p;',
  27988. '$mod.FuncB = $mod.p;',
  27989. '$mod.Getter = $mod.p;',
  27990. '$mod.p = $mod.Notify;',
  27991. '$mod.p = $mod.Notify;',
  27992. '$mod.p = $mod.ProcA;',
  27993. '$mod.p = $mod.ProcA;',
  27994. '$mod.p = $mod.FuncB;',
  27995. '$mod.p = $mod.FuncB;',
  27996. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  27997. '']));
  27998. end;
  27999. procedure TTestModule.TestProcType_PassProcToUntyped;
  28000. begin
  28001. StartProgram(false);
  28002. Add([
  28003. 'type',
  28004. ' TEvent = procedure of object;',
  28005. ' TFunc = function: longint;',
  28006. 'procedure DoIt(); varargs; begin end;',
  28007. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28008. 'var',
  28009. ' Event: tevent;',
  28010. ' Func: TFunc;',
  28011. 'begin',
  28012. ' doit(event,func);',
  28013. ' dosome(event,event,event);',
  28014. ' dosome(func,func,func);',
  28015. '']);
  28016. ConvertProgram;
  28017. CheckSource('TestProcType_PassProcToUntyped',
  28018. LinesToStr([ // statements
  28019. 'this.DoIt = function () {',
  28020. '};',
  28021. 'this.DoSome = function (a, b, p) {',
  28022. '};',
  28023. 'this.Event = null;',
  28024. 'this.Func = null;',
  28025. '']),
  28026. LinesToStr([ // $mod.$main
  28027. '$mod.DoIt($mod.Event, $mod.Func);',
  28028. '$mod.DoSome($mod.Event, {',
  28029. ' p: $mod,',
  28030. ' get: function () {',
  28031. ' return this.p.Event;',
  28032. ' },',
  28033. ' set: function (v) {',
  28034. ' this.p.Event = v;',
  28035. ' }',
  28036. '}, $mod.Event);',
  28037. '$mod.DoSome($mod.Func, {',
  28038. ' p: $mod,',
  28039. ' get: function () {',
  28040. ' return this.p.Func;',
  28041. ' },',
  28042. ' set: function (v) {',
  28043. ' this.p.Func = v;',
  28044. ' }',
  28045. '}, $mod.Func);',
  28046. '']));
  28047. end;
  28048. procedure TTestModule.TestProcType_PassProcToArray;
  28049. begin
  28050. StartProgram(false);
  28051. Add([
  28052. 'type',
  28053. ' TFunc = function: longint;',
  28054. ' TArrFunc = array of TFunc;',
  28055. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28056. 'function GetIt: longint; begin end;',
  28057. 'var',
  28058. ' Func: tfunc;',
  28059. 'begin',
  28060. ' doit([]);',
  28061. ' doit([@GetIt]);',
  28062. ' doit([Func]);',
  28063. '']);
  28064. ConvertProgram;
  28065. CheckSource('TestProcType_PassProcToArray',
  28066. LinesToStr([ // statements
  28067. 'this.DoIt = function (Arr) {',
  28068. '};',
  28069. 'this.GetIt = function () {',
  28070. ' var Result = 0;',
  28071. ' return Result;',
  28072. '};',
  28073. 'this.Func = null;',
  28074. '']),
  28075. LinesToStr([ // $mod.$main
  28076. '$mod.DoIt([]);',
  28077. '$mod.DoIt([$mod.GetIt]);',
  28078. '$mod.DoIt([$mod.Func]);',
  28079. '']));
  28080. end;
  28081. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28082. begin
  28083. StartProgram(false);
  28084. Add([
  28085. '{$modeswitch externalclass}',
  28086. 'type',
  28087. ' TProc = reference to procedure(i: longint); safecall;',
  28088. ' TEvent = procedure(i: longint) of object; safecall;',
  28089. ' TExtA = class external name ''ExtObj''',
  28090. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28091. ' procedure DoSome(Id: longint = 1);',
  28092. ' procedure SetOnClick(const e: TEvent);',
  28093. ' property OnClick: TEvent write SetOnClick;',
  28094. ' class procedure Fly(Id: longint = 1); static;',
  28095. ' procedure SetOnShow(const p: TProc);',
  28096. ' property OnShow: TProc write SetOnShow;',
  28097. ' end;',
  28098. 'procedure Run(i: longint = 1);',
  28099. 'begin',
  28100. 'end;',
  28101. 'var',
  28102. ' Obj: texta;',
  28103. ' e: TEvent;',
  28104. ' p: TProc;',
  28105. 'begin',
  28106. ' e:=e;',
  28107. ' e:[email protected];',
  28108. ' e:[email protected];',
  28109. ' e:=TEvent(@obj.dosome);', // no safecall
  28110. ' obj.OnClick:[email protected];',
  28111. ' obj.OnClick:[email protected];',
  28112. ' obj.setonclick(@obj.doit);',
  28113. ' obj.setonclick(@obj.dosome);',
  28114. ' p:=@Run;',
  28115. ' p:[email protected];',
  28116. ' obj.OnShow:=@Run;',
  28117. ' obj.OnShow:[email protected];',
  28118. ' obj.setOnShow(@Run);',
  28119. ' obj.setOnShow(@TExtA.Fly);',
  28120. ' with obj do begin',
  28121. ' e:=@doit;',
  28122. ' e:=@dosome;',
  28123. ' OnClick:=@doit;',
  28124. ' OnClick:=@dosome;',
  28125. ' setonclick(@doit);',
  28126. ' setonclick(@dosome);',
  28127. ' OnShow:=@Run;',
  28128. ' setOnShow(@Run);',
  28129. ' end;']);
  28130. ConvertProgram;
  28131. CheckSource('TestProcType_SafeCallObjFPC',
  28132. LinesToStr([ // statements
  28133. 'this.Run = function (i) {',
  28134. '};',
  28135. 'this.Obj = null;',
  28136. 'this.e = null;',
  28137. 'this.p = null;',
  28138. '']),
  28139. LinesToStr([ // $mod.$main
  28140. '$mod.e = $mod.e;',
  28141. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28142. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28143. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28144. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28145. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28146. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28147. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28148. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28149. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28150. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28151. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28152. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28153. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28154. 'var $with = $mod.Obj;',
  28155. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28156. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28157. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28158. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28159. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28160. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28161. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28162. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28163. '']));
  28164. end;
  28165. procedure TTestModule.TestProcType_SafeCallDelphi;
  28166. begin
  28167. StartProgram(false);
  28168. Add([
  28169. '{$mode delphi}',
  28170. '{$modeswitch externalclass}',
  28171. 'type',
  28172. ' TProc = reference to procedure(i: longint); safecall;',
  28173. ' TEvent = procedure(i: longint) of object; safecall;',
  28174. ' TExtA = class external name ''ExtObj''',
  28175. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28176. ' procedure DoSome(Id: longint = 1);',
  28177. ' procedure SetOnClick(const e: TEvent);',
  28178. ' property OnClick: TEvent write SetOnClick;',
  28179. ' class procedure Fly(Id: longint = 1); static;',
  28180. ' procedure SetOnShow(const p: TProc);',
  28181. ' property OnShow: TProc write SetOnShow;',
  28182. ' end;',
  28183. 'procedure Run(i: longint = 1);',
  28184. 'begin',
  28185. 'end;',
  28186. 'var',
  28187. ' Obj: texta;',
  28188. ' e: TEvent;',
  28189. ' p: TProc;',
  28190. 'begin',
  28191. ' e:=e;',
  28192. ' e:=obj.doit;',
  28193. ' e:=obj.dosome;',
  28194. ' e:=TEvent(@obj.dosome);', // no safecall
  28195. ' obj.OnClick:=obj.doit;',
  28196. ' obj.OnClick:=obj.dosome;',
  28197. ' obj.setonclick(obj.doit);',
  28198. ' obj.setonclick(obj.dosome);',
  28199. ' p:=Run;',
  28200. ' p:=TExtA.Fly;',
  28201. ' obj.OnShow:=Run;',
  28202. ' obj.OnShow:=TExtA.Fly;',
  28203. ' obj.setOnShow(Run);',
  28204. ' obj.setOnShow(TExtA.Fly);',
  28205. ' with obj do begin',
  28206. ' e:=doit;',
  28207. ' e:=dosome;',
  28208. ' OnClick:=doit;',
  28209. ' OnClick:=dosome;',
  28210. ' setonclick(doit);',
  28211. ' setonclick(dosome);',
  28212. ' OnShow:=@Run;',
  28213. ' setOnShow(@Run);',
  28214. ' end;']);
  28215. ConvertProgram;
  28216. CheckSource('TestProcType_SafeCallDelphi',
  28217. LinesToStr([ // statements
  28218. 'this.Run = function (i) {',
  28219. '};',
  28220. 'this.Obj = null;',
  28221. 'this.e = null;',
  28222. 'this.p = null;',
  28223. '']),
  28224. LinesToStr([ // $mod.$main
  28225. '$mod.e = $mod.e;',
  28226. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28227. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28228. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28229. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28230. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28231. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28232. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28233. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28234. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28235. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28236. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28237. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28238. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28239. 'var $with = $mod.Obj;',
  28240. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28241. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28242. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28243. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28244. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28245. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28246. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28247. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28248. '']));
  28249. end;
  28250. procedure TTestModule.TestPointer;
  28251. begin
  28252. StartProgram(false);
  28253. Add(['type',
  28254. ' TObject = class end;',
  28255. ' TClass = class of TObject;',
  28256. ' TArrInt = array of longint;',
  28257. 'const',
  28258. ' n = nil;',
  28259. 'var',
  28260. ' v: jsvalue;',
  28261. ' Obj: tobject;',
  28262. ' C: tclass;',
  28263. ' a: tarrint;',
  28264. ' p: Pointer = nil;',
  28265. ' s: string;',
  28266. 'begin',
  28267. ' p:=p;',
  28268. ' p:=nil;',
  28269. ' if p=nil then;',
  28270. ' if nil=p then;',
  28271. ' if Assigned(p) then;',
  28272. ' p:=Pointer(v);',
  28273. ' p:=obj;',
  28274. ' p:=c;',
  28275. ' p:=a;',
  28276. ' p:=tobject;',
  28277. ' obj:=TObject(p);',
  28278. ' c:=TClass(p);',
  28279. ' a:=TArrInt(p);',
  28280. ' p:=n;',
  28281. ' p:=Pointer(a);',
  28282. ' p:=pointer(s);',
  28283. ' s:=string(p);',
  28284. '']);
  28285. ConvertProgram;
  28286. CheckSource('TestPointer',
  28287. LinesToStr([ // statements
  28288. 'rtl.createClass(this, "TObject", null, function () {',
  28289. ' this.$init = function () {',
  28290. ' };',
  28291. ' this.$final = function () {',
  28292. ' };',
  28293. '});',
  28294. 'this.n = null;',
  28295. 'this.v = undefined;',
  28296. 'this.Obj = null;',
  28297. 'this.C = null;',
  28298. 'this.a = [];',
  28299. 'this.p = null;',
  28300. 'this.s = "";',
  28301. '']),
  28302. LinesToStr([ // $mod.$main
  28303. '$mod.p = $mod.p;',
  28304. '$mod.p = null;',
  28305. 'if ($mod.p === null) ;',
  28306. 'if (null === $mod.p) ;',
  28307. 'if ($mod.p != null) ;',
  28308. '$mod.p = $mod.v;',
  28309. '$mod.p = $mod.Obj;',
  28310. '$mod.p = $mod.C;',
  28311. '$mod.p = $mod.a;',
  28312. '$mod.p = $mod.TObject;',
  28313. '$mod.Obj = $mod.p;',
  28314. '$mod.C = $mod.p;',
  28315. '$mod.a = $mod.p;',
  28316. '$mod.p = null;',
  28317. '$mod.p = $mod.a;',
  28318. '$mod.p = $mod.s;',
  28319. '$mod.s = $mod.p;',
  28320. '']));
  28321. end;
  28322. procedure TTestModule.TestPointer_Proc;
  28323. begin
  28324. StartProgram(false);
  28325. Add('type');
  28326. Add(' TObject = class');
  28327. Add(' procedure DoIt; virtual; abstract;');
  28328. Add(' end;');
  28329. Add('procedure DoSome; begin end;');
  28330. Add('var');
  28331. Add(' o: TObject;');
  28332. Add(' p: Pointer;');
  28333. Add('begin');
  28334. Add(' p:=@DoSome;');
  28335. Add(' p:[email protected];');
  28336. ConvertProgram;
  28337. CheckSource('TestPointer_Proc',
  28338. LinesToStr([ // statements
  28339. 'rtl.createClass(this, "TObject", null, function () {',
  28340. ' this.$init = function () {',
  28341. ' };',
  28342. ' this.$final = function () {',
  28343. ' };',
  28344. '});',
  28345. 'this.DoSome = function () {',
  28346. '};',
  28347. 'this.o = null;',
  28348. 'this.p = null;',
  28349. '']),
  28350. LinesToStr([ // $mod.$main
  28351. '$mod.p = $mod.DoSome;',
  28352. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28353. '']));
  28354. end;
  28355. procedure TTestModule.TestPointer_AssignRecordFail;
  28356. begin
  28357. StartProgram(false);
  28358. Add('type');
  28359. Add(' TRec = record end;');
  28360. Add('var');
  28361. Add(' p: Pointer;');
  28362. Add(' r: TRec;');
  28363. Add('begin');
  28364. Add(' p:=r;');
  28365. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28366. nIncompatibleTypesGotExpected);
  28367. ConvertProgram;
  28368. end;
  28369. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28370. begin
  28371. StartProgram(false);
  28372. Add('type');
  28373. Add(' TArr = array[boolean] of longint;');
  28374. Add('var');
  28375. Add(' p: Pointer;');
  28376. Add(' a: TArr;');
  28377. Add('begin');
  28378. Add(' p:=a;');
  28379. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28380. nIncompatibleTypesGotExpected);
  28381. ConvertProgram;
  28382. end;
  28383. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28384. begin
  28385. StartProgram(false);
  28386. Add([
  28387. 'procedure DoIt(args: array of jsvalue); begin end;',
  28388. 'procedure DoAll; varargs; begin end;',
  28389. 'var',
  28390. ' v: jsvalue;',
  28391. 'begin',
  28392. ' DoIt([pointer(v)]);',
  28393. ' DoAll(pointer(v));',
  28394. '']);
  28395. ConvertProgram;
  28396. CheckSource('TestPointer_TypeCastJSValueToPointer',
  28397. LinesToStr([ // statements
  28398. 'this.DoIt = function (args) {',
  28399. '};',
  28400. 'this.DoAll = function () {',
  28401. '};',
  28402. 'this.v = undefined;',
  28403. '']),
  28404. LinesToStr([ // $mod.$main
  28405. '$mod.DoIt([$mod.v]);',
  28406. '$mod.DoAll($mod.v);',
  28407. '']));
  28408. end;
  28409. procedure TTestModule.TestPointer_NonRecordFail;
  28410. begin
  28411. StartProgram(false);
  28412. Add([
  28413. 'type',
  28414. ' p = ^longint;',
  28415. 'begin',
  28416. '']);
  28417. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  28418. ConvertProgram;
  28419. end;
  28420. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  28421. begin
  28422. StartProgram(false);
  28423. Add([
  28424. 'procedure DoIt(p: ^longint); begin end;',
  28425. 'begin',
  28426. '']);
  28427. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28428. ConvertProgram;
  28429. end;
  28430. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  28431. begin
  28432. StartProgram(false);
  28433. Add([
  28434. 'var p: ^longint;',
  28435. 'begin',
  28436. '']);
  28437. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28438. ConvertProgram;
  28439. end;
  28440. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  28441. begin
  28442. StartProgram(false);
  28443. Add([
  28444. 'function DoIt: ^longint; begin end;',
  28445. 'begin',
  28446. '']);
  28447. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  28448. ConvertProgram;
  28449. end;
  28450. procedure TTestModule.TestPointer_AddrOperatorFail;
  28451. begin
  28452. StartProgram(false);
  28453. Add([
  28454. 'var i: longint;',
  28455. 'begin',
  28456. ' if @i=nil then ;',
  28457. '']);
  28458. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  28459. ConvertProgram;
  28460. end;
  28461. procedure TTestModule.TestPointer_ArrayParamsFail;
  28462. begin
  28463. StartProgram(false);
  28464. Add([
  28465. 'var',
  28466. ' p: Pointer;',
  28467. 'begin',
  28468. ' p:=p[1];',
  28469. '']);
  28470. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  28471. ConvertProgram;
  28472. end;
  28473. procedure TTestModule.TestPointer_PointerAddFail;
  28474. begin
  28475. StartProgram(false);
  28476. Add([
  28477. 'var',
  28478. ' p: Pointer;',
  28479. 'begin',
  28480. ' p:=p+1;',
  28481. '']);
  28482. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  28483. ConvertProgram;
  28484. end;
  28485. procedure TTestModule.TestPointer_IncPointerFail;
  28486. begin
  28487. StartProgram(false);
  28488. Add([
  28489. 'var',
  28490. ' p: Pointer;',
  28491. 'begin',
  28492. ' inc(p,1);',
  28493. '']);
  28494. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  28495. nIncompatibleTypeArgNo);
  28496. ConvertProgram;
  28497. end;
  28498. procedure TTestModule.TestPointer_Record;
  28499. begin
  28500. StartProgram(false);
  28501. Add([
  28502. 'type',
  28503. ' TRec = record x: longint; end;',
  28504. ' PRec = ^TRec;',
  28505. 'var',
  28506. ' r: TRec;',
  28507. ' p: PRec;',
  28508. ' q: ^TRec;',
  28509. ' Ptr: pointer;',
  28510. 'begin',
  28511. ' new(p);',
  28512. ' p:=@r;',
  28513. ' r:=p^;',
  28514. ' r.x:=p^.x;',
  28515. ' p^.x:=r.x;',
  28516. ' if p^.x=3 then ;',
  28517. ' if 4=p^.x then ;',
  28518. ' dispose(p);',
  28519. ' new(q);',
  28520. ' dispose(q);',
  28521. ' Ptr:=p;',
  28522. ' p:=PRec(ptr);',
  28523. '']);
  28524. ConvertProgram;
  28525. CheckSource('TestPointer_Record',
  28526. LinesToStr([ // statements
  28527. 'rtl.recNewT(this, "TRec", function () {',
  28528. ' this.x = 0;',
  28529. ' this.$eq = function (b) {',
  28530. ' return this.x === b.x;',
  28531. ' };',
  28532. ' this.$assign = function (s) {',
  28533. ' this.x = s.x;',
  28534. ' return this;',
  28535. ' };',
  28536. '});',
  28537. 'this.r = this.TRec.$new();',
  28538. 'this.p = null;',
  28539. 'this.q = null;',
  28540. 'this.Ptr = null;',
  28541. '']),
  28542. LinesToStr([ // $mod.$main
  28543. '$mod.p = $mod.TRec.$new();',
  28544. '$mod.p = $mod.r;',
  28545. '$mod.r.$assign($mod.p);',
  28546. '$mod.r.x = $mod.p.x;',
  28547. '$mod.p.x = $mod.r.x;',
  28548. 'if ($mod.p.x === 3) ;',
  28549. 'if (4 === $mod.p.x) ;',
  28550. '$mod.p = null;',
  28551. '$mod.q = $mod.TRec.$new();',
  28552. '$mod.q = null;',
  28553. '$mod.Ptr = $mod.p;',
  28554. '$mod.p = $mod.Ptr;',
  28555. '']));
  28556. end;
  28557. procedure TTestModule.TestPointer_RecordArg;
  28558. begin
  28559. StartProgram(false);
  28560. Add([
  28561. '{$modeswitch autoderef}',
  28562. 'type',
  28563. ' TRec = record x: longint; end;',
  28564. ' PRec = ^TRec;',
  28565. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  28566. 'begin',
  28567. ' a.x:=a.x;',
  28568. ' a^.x:=a^.x;',
  28569. ' with a^ do',
  28570. ' x:=x;',
  28571. 'end;',
  28572. 'function GetIt(p: PRec): PRec;',
  28573. 'begin',
  28574. ' p.x:=p.x;',
  28575. ' p^.x:=p^.x;',
  28576. ' with p^ do',
  28577. ' x:=x;',
  28578. 'end;',
  28579. 'var',
  28580. ' r: TRec;',
  28581. ' p: PRec;',
  28582. 'begin',
  28583. ' p:=GetIt(p);',
  28584. ' p^:=GetIt(@r)^;',
  28585. ' DoIt(p,p,p);',
  28586. ' DoIt(@r,p,p);',
  28587. '']);
  28588. ConvertProgram;
  28589. CheckSource('TestPointer_RecordArg',
  28590. LinesToStr([ // statements
  28591. 'rtl.recNewT(this, "TRec", function () {',
  28592. ' this.x = 0;',
  28593. ' this.$eq = function (b) {',
  28594. ' return this.x === b.x;',
  28595. ' };',
  28596. ' this.$assign = function (s) {',
  28597. ' this.x = s.x;',
  28598. ' return this;',
  28599. ' };',
  28600. '});',
  28601. 'this.DoIt = function (a, b, c) {',
  28602. ' var Result = $mod.TRec.$new();',
  28603. ' a.x = a.x;',
  28604. ' a.x = a.x;',
  28605. ' a.x = a.x;',
  28606. ' return Result;',
  28607. '};',
  28608. 'this.GetIt = function (p) {',
  28609. ' var Result = null;',
  28610. ' p.x = p.x;',
  28611. ' p.x = p.x;',
  28612. ' p.x = p.x;',
  28613. ' return Result;',
  28614. '};',
  28615. 'this.r = this.TRec.$new();',
  28616. 'this.p = null;',
  28617. '']),
  28618. LinesToStr([ // $mod.$main
  28619. '$mod.p = $mod.GetIt($mod.p);',
  28620. '$mod.p.$assign($mod.GetIt($mod.r));',
  28621. '$mod.DoIt($mod.p, {',
  28622. ' p: $mod,',
  28623. ' get: function () {',
  28624. ' return this.p.p;',
  28625. ' },',
  28626. ' set: function (v) {',
  28627. ' this.p.p = v;',
  28628. ' }',
  28629. '}, {',
  28630. ' p: $mod,',
  28631. ' get: function () {',
  28632. ' return this.p.p;',
  28633. ' },',
  28634. ' set: function (v) {',
  28635. ' this.p.p = v;',
  28636. ' }',
  28637. '});',
  28638. '$mod.DoIt($mod.r, {',
  28639. ' p: $mod,',
  28640. ' get: function () {',
  28641. ' return this.p.p;',
  28642. ' },',
  28643. ' set: function (v) {',
  28644. ' this.p.p = v;',
  28645. ' }',
  28646. '}, {',
  28647. ' p: $mod,',
  28648. ' get: function () {',
  28649. ' return this.p.p;',
  28650. ' },',
  28651. ' set: function (v) {',
  28652. ' this.p.p = v;',
  28653. ' }',
  28654. '});',
  28655. '']));
  28656. end;
  28657. procedure TTestModule.TestJSValue_AssignToJSValue;
  28658. begin
  28659. StartProgram(false);
  28660. Add('var');
  28661. Add(' v: jsvalue;');
  28662. Add(' i: longint;');
  28663. Add(' s: string;');
  28664. Add(' b: boolean;');
  28665. Add(' d: double;');
  28666. Add(' p: pointer;');
  28667. Add('begin');
  28668. Add(' v:=v;');
  28669. Add(' v:=1;');
  28670. Add(' v:=i;');
  28671. Add(' v:='''';');
  28672. Add(' v:=''c'';');
  28673. Add(' v:=''foo'';');
  28674. Add(' v:=s;');
  28675. Add(' v:=false;');
  28676. Add(' v:=true;');
  28677. Add(' v:=b;');
  28678. Add(' v:=0.1;');
  28679. Add(' v:=d;');
  28680. Add(' v:=nil;');
  28681. Add(' v:=p;');
  28682. ConvertProgram;
  28683. CheckSource('TestJSValue_AssignToJSValue',
  28684. LinesToStr([ // statements
  28685. 'this.v = undefined;',
  28686. 'this.i = 0;',
  28687. 'this.s = "";',
  28688. 'this.b = false;',
  28689. 'this.d = 0.0;',
  28690. 'this.p = null;',
  28691. '']),
  28692. LinesToStr([ // $mod.$main
  28693. '$mod.v = $mod.v;',
  28694. '$mod.v = 1;',
  28695. '$mod.v = $mod.i;',
  28696. '$mod.v = "";',
  28697. '$mod.v = "c";',
  28698. '$mod.v = "foo";',
  28699. '$mod.v = $mod.s;',
  28700. '$mod.v = false;',
  28701. '$mod.v = true;',
  28702. '$mod.v = $mod.b;',
  28703. '$mod.v = 0.1;',
  28704. '$mod.v = $mod.d;',
  28705. '$mod.v = null;',
  28706. '$mod.v = $mod.p;',
  28707. '']));
  28708. end;
  28709. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  28710. begin
  28711. StartProgram(false);
  28712. Add('type');
  28713. Add(' integer = longint;');
  28714. Add(' TYesNo = boolean;');
  28715. Add(' TFloat = double;');
  28716. Add(' TCaption = string;');
  28717. Add(' TChar = char;');
  28718. Add('var');
  28719. Add(' v: jsvalue;');
  28720. Add(' i: integer;');
  28721. Add(' s: TCaption;');
  28722. Add(' b: TYesNo;');
  28723. Add(' d: TFloat;');
  28724. Add(' c: char;');
  28725. Add('begin');
  28726. Add(' i:=longint(v);');
  28727. Add(' i:=integer(v);');
  28728. Add(' s:=string(v);');
  28729. Add(' s:=TCaption(v);');
  28730. Add(' b:=boolean(v);');
  28731. Add(' b:=TYesNo(v);');
  28732. Add(' d:=double(v);');
  28733. Add(' d:=TFloat(v);');
  28734. Add(' c:=char(v);');
  28735. Add(' c:=TChar(v);');
  28736. ConvertProgram;
  28737. CheckSource('TestJSValue_TypeCastToBaseType',
  28738. LinesToStr([ // statements
  28739. 'this.v = undefined;',
  28740. 'this.i = 0;',
  28741. 'this.s = "";',
  28742. 'this.b = false;',
  28743. 'this.d = 0.0;',
  28744. 'this.c = "";',
  28745. '']),
  28746. LinesToStr([ // $mod.$main
  28747. '$mod.i = rtl.trunc($mod.v);',
  28748. '$mod.i = rtl.trunc($mod.v);',
  28749. '$mod.s = "" + $mod.v;',
  28750. '$mod.s = "" + $mod.v;',
  28751. '$mod.b = !($mod.v == false);',
  28752. '$mod.b = !($mod.v == false);',
  28753. '$mod.d = rtl.getNumber($mod.v);',
  28754. '$mod.d = rtl.getNumber($mod.v);',
  28755. '$mod.c = rtl.getChar($mod.v);',
  28756. '$mod.c = rtl.getChar($mod.v);',
  28757. '']));
  28758. end;
  28759. procedure TTestModule.TestJSValue_TypecastToJSValue;
  28760. begin
  28761. StartProgram(false);
  28762. Add([
  28763. 'type',
  28764. ' TArr = array of word;',
  28765. ' TRec = record end;',
  28766. ' TSet = set of boolean;',
  28767. 'procedure Fly(v: jsvalue);',
  28768. 'begin',
  28769. 'end;',
  28770. 'var',
  28771. ' a: TArr;',
  28772. ' r: TRec;',
  28773. ' s: TSet;',
  28774. 'begin',
  28775. ' Fly(jsvalue(a));',
  28776. ' Fly(jsvalue(r));',
  28777. ' Fly(jsvalue(s));',
  28778. '']);
  28779. ConvertProgram;
  28780. CheckSource('TestJSValue_TypecastToJSValue',
  28781. LinesToStr([ // statements
  28782. 'rtl.recNewT(this, "TRec", function () {',
  28783. ' this.$eq = function (b) {',
  28784. ' return true;',
  28785. ' };',
  28786. ' this.$assign = function (s) {',
  28787. ' return this;',
  28788. ' };',
  28789. '});',
  28790. 'this.Fly = function (v) {',
  28791. '};',
  28792. 'this.a = [];',
  28793. 'this.r = this.TRec.$new();',
  28794. 'this.s = {};',
  28795. '']),
  28796. LinesToStr([ // $mod.$main
  28797. '$mod.Fly($mod.a);',
  28798. '$mod.Fly($mod.r);',
  28799. '$mod.Fly($mod.s);',
  28800. '']));
  28801. end;
  28802. procedure TTestModule.TestJSValue_Equal;
  28803. begin
  28804. StartProgram(false);
  28805. Add('type');
  28806. Add(' integer = longint;');
  28807. Add(' TYesNo = boolean;');
  28808. Add(' TFloat = double;');
  28809. Add(' TCaption = string;');
  28810. Add(' TChar = char;');
  28811. Add(' TMulti = JSValue;');
  28812. Add('var');
  28813. Add(' v: jsvalue;');
  28814. Add(' i: integer;');
  28815. Add(' s: TCaption;');
  28816. Add(' b: TYesNo;');
  28817. Add(' d: TFloat;');
  28818. Add(' c: char;');
  28819. Add(' m: TMulti;');
  28820. Add('begin');
  28821. Add(' b:=v=v;');
  28822. Add(' b:=v<>v;');
  28823. Add(' b:=v=1;');
  28824. Add(' b:=v<>1;');
  28825. Add(' b:=2=v;');
  28826. Add(' b:=2<>v;');
  28827. Add(' b:=v=i;');
  28828. Add(' b:=i=v;');
  28829. Add(' b:=v=nil;');
  28830. Add(' b:=nil=v;');
  28831. Add(' b:=v=false;');
  28832. Add(' b:=true=v;');
  28833. Add(' b:=v=b;');
  28834. Add(' b:=b=v;');
  28835. Add(' b:=v=s;');
  28836. Add(' b:=s=v;');
  28837. Add(' b:=v=''foo'';');
  28838. Add(' b:=''''=v;');
  28839. Add(' b:=v=d;');
  28840. Add(' b:=d=v;');
  28841. Add(' b:=v=3.4;');
  28842. Add(' b:=5.6=v;');
  28843. Add(' b:=v=c;');
  28844. Add(' b:=c=v;');
  28845. Add(' b:=m=m;');
  28846. Add(' b:=v=m;');
  28847. Add(' b:=m=v;');
  28848. ConvertProgram;
  28849. CheckSource('TestJSValue_Equal',
  28850. LinesToStr([ // statements
  28851. 'this.v = undefined;',
  28852. 'this.i = 0;',
  28853. 'this.s = "";',
  28854. 'this.b = false;',
  28855. 'this.d = 0.0;',
  28856. 'this.c = "";',
  28857. 'this.m = undefined;',
  28858. '']),
  28859. LinesToStr([ // $mod.$main
  28860. '$mod.b = $mod.v == $mod.v;',
  28861. '$mod.b = $mod.v != $mod.v;',
  28862. '$mod.b = $mod.v == 1;',
  28863. '$mod.b = $mod.v != 1;',
  28864. '$mod.b = 2 == $mod.v;',
  28865. '$mod.b = 2 != $mod.v;',
  28866. '$mod.b = $mod.v == $mod.i;',
  28867. '$mod.b = $mod.i == $mod.v;',
  28868. '$mod.b = $mod.v == null;',
  28869. '$mod.b = null == $mod.v;',
  28870. '$mod.b = $mod.v == false;',
  28871. '$mod.b = true == $mod.v;',
  28872. '$mod.b = $mod.v == $mod.b;',
  28873. '$mod.b = $mod.b == $mod.v;',
  28874. '$mod.b = $mod.v == $mod.s;',
  28875. '$mod.b = $mod.s == $mod.v;',
  28876. '$mod.b = $mod.v == "foo";',
  28877. '$mod.b = "" == $mod.v;',
  28878. '$mod.b = $mod.v == $mod.d;',
  28879. '$mod.b = $mod.d == $mod.v;',
  28880. '$mod.b = $mod.v == 3.4;',
  28881. '$mod.b = 5.6 == $mod.v;',
  28882. '$mod.b = $mod.v == $mod.c;',
  28883. '$mod.b = $mod.c == $mod.v;',
  28884. '$mod.b = $mod.m == $mod.m;',
  28885. '$mod.b = $mod.v == $mod.m;',
  28886. '$mod.b = $mod.m == $mod.v;',
  28887. '']));
  28888. end;
  28889. procedure TTestModule.TestJSValue_If;
  28890. begin
  28891. StartProgram(false);
  28892. Add([
  28893. 'procedure Fly(var u);',
  28894. 'begin',
  28895. ' if jsvalue(u) then ;',
  28896. 'end;',
  28897. 'var',
  28898. ' v: jsvalue;',
  28899. 'begin',
  28900. ' if v then ;',
  28901. ' while v do ;',
  28902. ' repeat until v;',
  28903. '']);
  28904. ConvertProgram;
  28905. CheckSource('TestJSValue_If',
  28906. LinesToStr([ // statements
  28907. 'this.Fly = function (u) {',
  28908. ' if (u.get()) ;',
  28909. '};',
  28910. 'this.v = undefined;',
  28911. '']),
  28912. LinesToStr([ // $mod.$main
  28913. 'if ($mod.v) ;',
  28914. 'while($mod.v){',
  28915. '};',
  28916. 'do{',
  28917. '} while(!$mod.v);',
  28918. '']));
  28919. end;
  28920. procedure TTestModule.TestJSValue_Not;
  28921. begin
  28922. StartProgram(false);
  28923. Add([
  28924. 'var',
  28925. ' v: jsvalue;',
  28926. ' b: boolean;',
  28927. 'begin',
  28928. ' b:=not v;',
  28929. ' if not v then ;',
  28930. ' while not v do ;',
  28931. ' repeat until not v;',
  28932. '']);
  28933. ConvertProgram;
  28934. CheckSource('TestJSValue_If',
  28935. LinesToStr([ // statements
  28936. 'this.v = undefined;',
  28937. 'this.b = false;',
  28938. '']),
  28939. LinesToStr([ // $mod.$main
  28940. '$mod.b=!$mod.v;',
  28941. 'if (!$mod.v) ;',
  28942. 'while(!$mod.v){',
  28943. '};',
  28944. 'do{',
  28945. '} while($mod.v);',
  28946. '']));
  28947. end;
  28948. procedure TTestModule.TestJSValue_Enum;
  28949. begin
  28950. StartProgram(false);
  28951. Add('type');
  28952. Add(' TColor = (red, blue);');
  28953. Add(' TRedBlue = TColor;');
  28954. Add('var');
  28955. Add(' v: jsvalue;');
  28956. Add(' e: TColor;');
  28957. Add('begin');
  28958. Add(' v:=e;');
  28959. Add(' v:=TColor(e);');
  28960. Add(' v:=TRedBlue(e);');
  28961. Add(' e:=TColor(v);');
  28962. Add(' e:=TRedBlue(v);');
  28963. ConvertProgram;
  28964. CheckSource('TestJSValue_Enum',
  28965. LinesToStr([ // statements
  28966. 'this.TColor = {',
  28967. ' "0": "red",',
  28968. ' red: 0,',
  28969. ' "1": "blue",',
  28970. ' blue: 1',
  28971. '};',
  28972. 'this.v = undefined;',
  28973. 'this.e = 0;',
  28974. '']),
  28975. LinesToStr([ // $mod.$main
  28976. '$mod.v = $mod.e;',
  28977. '$mod.v = $mod.e;',
  28978. '$mod.v = $mod.e;',
  28979. '$mod.e = $mod.v;',
  28980. '$mod.e = $mod.v;',
  28981. '']));
  28982. end;
  28983. procedure TTestModule.TestJSValue_ClassInstance;
  28984. begin
  28985. StartProgram(false);
  28986. Add([
  28987. 'type',
  28988. ' TObject = class',
  28989. ' end;',
  28990. ' TBirdObject = TObject;',
  28991. 'var',
  28992. ' v: jsvalue;',
  28993. ' o: TObject;',
  28994. 'begin',
  28995. ' v:=o;',
  28996. ' v:=TObject(o);',
  28997. ' v:=TBirdObject(o);',
  28998. ' o:=TObject(v);',
  28999. ' o:=TBirdObject(v);',
  29000. ' if v is TObject then ;',
  29001. '']);
  29002. ConvertProgram;
  29003. CheckSource('TestJSValue_ClassInstance',
  29004. LinesToStr([ // statements
  29005. 'rtl.createClass(this, "TObject", null, function () {',
  29006. ' this.$init = function () {',
  29007. ' };',
  29008. ' this.$final = function () {',
  29009. ' };',
  29010. '});',
  29011. 'this.v = undefined;',
  29012. 'this.o = null;',
  29013. '']),
  29014. LinesToStr([ // $mod.$main
  29015. '$mod.v = $mod.o;',
  29016. '$mod.v = $mod.o;',
  29017. '$mod.v = $mod.o;',
  29018. '$mod.o = rtl.getObject($mod.v);',
  29019. '$mod.o = rtl.getObject($mod.v);',
  29020. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29021. '']));
  29022. end;
  29023. procedure TTestModule.TestJSValue_ClassOf;
  29024. begin
  29025. StartProgram(false);
  29026. Add([
  29027. 'type',
  29028. ' TClass = class of TObject;',
  29029. ' TObject = class',
  29030. ' end;',
  29031. ' TBirds = class of TBird;',
  29032. ' TBird = class(TObject) end;',
  29033. 'var',
  29034. ' v: jsvalue;',
  29035. ' c: TClass;',
  29036. 'begin',
  29037. ' v:=c;',
  29038. ' v:=TObject;',
  29039. ' v:=TClass(c);',
  29040. ' v:=TBirds(c);',
  29041. ' c:=TClass(v);',
  29042. ' c:=TBirds(v);',
  29043. ' if v is TClass then ;',
  29044. '']);
  29045. ConvertProgram;
  29046. CheckSource('TestJSValue_ClassOf',
  29047. LinesToStr([ // statements
  29048. 'rtl.createClass(this, "TObject", null, function () {',
  29049. ' this.$init = function () {',
  29050. ' };',
  29051. ' this.$final = function () {',
  29052. ' };',
  29053. '});',
  29054. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29055. '});',
  29056. 'this.v = undefined;',
  29057. 'this.c = null;',
  29058. '']),
  29059. LinesToStr([ // $mod.$main
  29060. '$mod.v = $mod.c;',
  29061. '$mod.v = $mod.TObject;',
  29062. '$mod.v = $mod.c;',
  29063. '$mod.v = $mod.c;',
  29064. '$mod.c = rtl.getObject($mod.v);',
  29065. '$mod.c = rtl.getObject($mod.v);',
  29066. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29067. '']));
  29068. end;
  29069. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29070. begin
  29071. StartProgram(false);
  29072. Add([
  29073. 'type',
  29074. ' integer = longint;',
  29075. ' TArray = array of JSValue;',
  29076. ' TArrgh = tarray;',
  29077. ' TArrInt = array of integer;',
  29078. 'var',
  29079. ' v: jsvalue;',
  29080. ' TheArray: tarray = (1,''2'');',
  29081. ' Arr: tarrgh;',
  29082. ' i: integer;',
  29083. ' ArrInt: tarrint;',
  29084. 'begin',
  29085. ' arr:=thearray;',
  29086. ' thearray:=arr;',
  29087. ' setlength(arr,2);',
  29088. ' setlength(thearray,3);',
  29089. ' arr[4]:=v;',
  29090. ' arr[5]:=length(thearray);',
  29091. ' arr[6]:=nil;',
  29092. ' arr[7]:=thearray[8];',
  29093. ' arr[low(arr)]:=high(thearray);',
  29094. ' arr:=arrint;',
  29095. ' arrInt:=tarrint(arr);',
  29096. ' if TheArray = nil then ;',
  29097. ' if nil = TheArray then ;',
  29098. ' if TheArray <> nil then ;',
  29099. ' if nil <> TheArray then ;',
  29100. '']);
  29101. ConvertProgram;
  29102. CheckSource('TestJSValue_ArrayOfJSValue',
  29103. LinesToStr([ // statements
  29104. 'this.v = undefined;',
  29105. 'this.TheArray = [1, "2"];',
  29106. 'this.Arr = [];',
  29107. 'this.i = 0;',
  29108. 'this.ArrInt = [];',
  29109. '']),
  29110. LinesToStr([ // $mod.$main
  29111. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29112. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29113. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29114. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29115. '$mod.Arr[4] = $mod.v;',
  29116. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29117. '$mod.Arr[6] = null;',
  29118. '$mod.Arr[7] = $mod.TheArray[8];',
  29119. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29120. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29121. '$mod.ArrInt = $mod.Arr;',
  29122. 'if (rtl.length($mod.TheArray) === 0) ;',
  29123. 'if (rtl.length($mod.TheArray) === 0) ;',
  29124. 'if (rtl.length($mod.TheArray) > 0) ;',
  29125. 'if (rtl.length($mod.TheArray) > 0) ;',
  29126. '']));
  29127. end;
  29128. procedure TTestModule.TestJSValue_ArrayLit;
  29129. begin
  29130. StartProgram(false);
  29131. Add([
  29132. 'type',
  29133. ' TFlag = (big,small);',
  29134. ' TArray = array of JSValue;',
  29135. ' TObject = class end;',
  29136. ' TClass = class of TObject;',
  29137. 'var',
  29138. ' v: jsvalue;',
  29139. ' a: TArray;',
  29140. ' o: TObject;',
  29141. 'begin',
  29142. ' a:=[];',
  29143. ' a:=[1];',
  29144. ' a:=[1,2];',
  29145. ' a:=[big];',
  29146. ' a:=[1,big];',
  29147. ' a:=[o,nil];',
  29148. '']);
  29149. ConvertProgram;
  29150. CheckSource('TestJSValue_ArrayLit',
  29151. LinesToStr([ // statements
  29152. 'this.TFlag = {',
  29153. ' "0": "big",',
  29154. ' big: 0,',
  29155. ' "1": "small",',
  29156. ' small: 1',
  29157. '};',
  29158. 'rtl.createClass(this, "TObject", null, function () {',
  29159. ' this.$init = function () {',
  29160. ' };',
  29161. ' this.$final = function () {',
  29162. ' };',
  29163. '});',
  29164. 'this.v = undefined;',
  29165. 'this.a = [];',
  29166. 'this.o = null;',
  29167. '']),
  29168. LinesToStr([ // $mod.$main
  29169. '$mod.a = [];',
  29170. '$mod.a = [1];',
  29171. '$mod.a = [1, 2];',
  29172. '$mod.a = [$mod.TFlag.big];',
  29173. '$mod.a = [1, $mod.TFlag.big];',
  29174. '$mod.a = [$mod.o, null];',
  29175. '']));
  29176. end;
  29177. procedure TTestModule.TestJSValue_Params;
  29178. begin
  29179. StartProgram(false);
  29180. Add('type');
  29181. Add(' integer = longint;');
  29182. Add(' TYesNo = boolean;');
  29183. Add(' TFloat = double;');
  29184. Add(' TCaption = string;');
  29185. Add(' TChar = char;');
  29186. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29187. Add('var');
  29188. Add(' l: jsvalue;');
  29189. Add('begin');
  29190. Add(' a:=a;');
  29191. Add(' l:=b;');
  29192. Add(' c:=c;');
  29193. Add(' d:=d;');
  29194. Add(' Result:=l;');
  29195. Add('end;');
  29196. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29197. Add('var');
  29198. Add(' v: jsvalue;');
  29199. Add(' i: integer;');
  29200. Add(' b: TYesNo;');
  29201. Add(' d: TFloat;');
  29202. Add(' s: TCaption;');
  29203. Add(' c: TChar;');
  29204. Add('begin');
  29205. Add(' v:=doit(v,v,v,v);');
  29206. Add(' i:=integer(dosome(i,i));');
  29207. Add(' b:=TYesNo(dosome(b,b));');
  29208. Add(' d:=TFloat(dosome(d,d));');
  29209. Add(' s:=TCaption(dosome(s,s));');
  29210. Add(' c:=TChar(dosome(c,c));');
  29211. ConvertProgram;
  29212. CheckSource('TestJSValue_Params',
  29213. LinesToStr([ // statements
  29214. 'this.DoIt = function (a, b, c, d) {',
  29215. ' var Result = undefined;',
  29216. ' var l = undefined;',
  29217. ' a = a;',
  29218. ' l = b;',
  29219. ' c.set(c.get());',
  29220. ' d.set(d.get());',
  29221. ' Result = l;',
  29222. ' return Result;',
  29223. '};',
  29224. 'this.DoSome = function (a, b) {',
  29225. ' var Result = undefined;',
  29226. ' return Result;',
  29227. '};',
  29228. 'this.v = undefined;',
  29229. 'this.i = 0;',
  29230. 'this.b = false;',
  29231. 'this.d = 0.0;',
  29232. 'this.s = "";',
  29233. 'this.c = "";',
  29234. '']),
  29235. LinesToStr([ // $mod.$main
  29236. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29237. ' p: $mod,',
  29238. ' get: function () {',
  29239. ' return this.p.v;',
  29240. ' },',
  29241. ' set: function (v) {',
  29242. ' this.p.v = v;',
  29243. ' }',
  29244. '}, {',
  29245. ' p: $mod,',
  29246. ' get: function () {',
  29247. ' return this.p.v;',
  29248. ' },',
  29249. ' set: function (v) {',
  29250. ' this.p.v = v;',
  29251. ' }',
  29252. '});',
  29253. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29254. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29255. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29256. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29257. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29258. '']));
  29259. end;
  29260. procedure TTestModule.TestJSValue_UntypedParam;
  29261. begin
  29262. StartProgram(false);
  29263. Add('function DoIt(const a; var b; out c): jsvalue;');
  29264. Add('begin');
  29265. Add(' Result:=a;');
  29266. Add(' Result:=b;');
  29267. Add(' Result:=c;');
  29268. Add(' b:=Result;');
  29269. Add(' c:=Result;');
  29270. Add('end;');
  29271. Add('var i: longint;');
  29272. Add('begin');
  29273. Add(' doit(i,i,i);');
  29274. ConvertProgram;
  29275. CheckSource('TestJSValue_UntypedParam',
  29276. LinesToStr([ // statements
  29277. 'this.DoIt = function (a, b, c) {',
  29278. ' var Result = undefined;',
  29279. ' Result = a;',
  29280. ' Result = b.get();',
  29281. ' Result = c.get();',
  29282. ' b.set(Result);',
  29283. ' c.set(Result);',
  29284. ' return Result;',
  29285. '};',
  29286. 'this.i = 0;',
  29287. '']),
  29288. LinesToStr([ // $mod.$main
  29289. '$mod.DoIt($mod.i, {',
  29290. ' p: $mod,',
  29291. ' get: function () {',
  29292. ' return this.p.i;',
  29293. ' },',
  29294. ' set: function (v) {',
  29295. ' this.p.i = v;',
  29296. ' }',
  29297. '}, {',
  29298. ' p: $mod,',
  29299. ' get: function () {',
  29300. ' return this.p.i;',
  29301. ' },',
  29302. ' set: function (v) {',
  29303. ' this.p.i = v;',
  29304. ' }',
  29305. '});',
  29306. '']));
  29307. end;
  29308. procedure TTestModule.TestJSValue_FuncResultType;
  29309. begin
  29310. StartProgram(false);
  29311. Add('type');
  29312. Add(' integer = longint;');
  29313. Add(' TJSValueArray = array of JSValue;');
  29314. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29315. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29316. Add('begin');
  29317. Add(' while Compare(P,aList[0])>0 do ;');
  29318. Add('end;');
  29319. Add('var');
  29320. Add(' Compare: TListSortCompare;');
  29321. Add(' V: JSValue;');
  29322. Add(' i: integer;');
  29323. Add('begin');
  29324. Add(' if Compare(V,V)>0 then ;');
  29325. Add(' if Compare(i,i)>1 then ;');
  29326. Add(' if Compare(nil,false)>2 then ;');
  29327. Add(' if Compare(1,true)>3 then ;');
  29328. ConvertProgram;
  29329. CheckSource('TestJSValue_UntypedParam',
  29330. LinesToStr([ // statements
  29331. 'this.Sort = function (P, aList, Compare) {',
  29332. ' while (Compare(P, aList[0]) > 0) {',
  29333. ' };',
  29334. '};',
  29335. 'this.Compare = null;',
  29336. 'this.V = undefined;',
  29337. 'this.i = 0;',
  29338. '']),
  29339. LinesToStr([ // $mod.$main
  29340. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29341. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29342. 'if ($mod.Compare(null, false) > 2) ;',
  29343. 'if ($mod.Compare(1, true) > 3) ;',
  29344. '']));
  29345. end;
  29346. procedure TTestModule.TestJSValue_ProcType_Assign;
  29347. begin
  29348. StartProgram(false);
  29349. Add('type');
  29350. Add(' integer = longint;');
  29351. Add(' TObject = class');
  29352. Add(' class function GetGlob: integer;');
  29353. Add(' function Getter: integer;');
  29354. Add(' end;');
  29355. Add('class function TObject.GetGlob: integer;');
  29356. Add('var v1: jsvalue;');
  29357. Add('begin');
  29358. Add(' v1:=@GetGlob;');
  29359. Add(' v1:[email protected];');
  29360. Add('end;');
  29361. Add('function TObject.Getter: integer;');
  29362. Add('var v2: jsvalue;');
  29363. Add('begin');
  29364. Add(' v2:=@Getter;');
  29365. Add(' v2:[email protected];');
  29366. Add(' v2:=@GetGlob;');
  29367. Add(' v2:[email protected];');
  29368. Add('end;');
  29369. Add('function GetIt(i: integer): integer;');
  29370. Add('var v3: jsvalue;');
  29371. Add('begin');
  29372. Add(' v3:=@GetIt;');
  29373. Add('end;');
  29374. Add('var');
  29375. Add(' V: JSValue;');
  29376. Add(' o: TObject;');
  29377. Add('begin');
  29378. Add(' v:=@GetIt;');
  29379. Add(' v:[email protected];');
  29380. Add(' v:[email protected];');
  29381. ConvertProgram;
  29382. CheckSource('TestJSValue_ProcType_Assign',
  29383. LinesToStr([ // statements
  29384. 'rtl.createClass(this, "TObject", null, function () {',
  29385. ' this.$init = function () {',
  29386. ' };',
  29387. ' this.$final = function () {',
  29388. ' };',
  29389. ' this.GetGlob = function () {',
  29390. ' var Result = 0;',
  29391. ' var v1 = undefined;',
  29392. ' v1 = rtl.createCallback(this, "GetGlob");',
  29393. ' v1 = rtl.createCallback(this, "GetGlob");',
  29394. ' return Result;',
  29395. ' };',
  29396. ' this.Getter = function () {',
  29397. ' var Result = 0;',
  29398. ' var v2 = undefined;',
  29399. ' v2 = rtl.createCallback(this, "Getter");',
  29400. ' v2 = rtl.createCallback(this, "Getter");',
  29401. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29402. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  29403. ' return Result;',
  29404. ' };',
  29405. '});',
  29406. 'this.GetIt = function (i) {',
  29407. ' var Result = 0;',
  29408. ' var v3 = undefined;',
  29409. ' v3 = $mod.GetIt;',
  29410. ' return Result;',
  29411. '};',
  29412. 'this.V = undefined;',
  29413. 'this.o = null;',
  29414. '']),
  29415. LinesToStr([ // $mod.$main
  29416. '$mod.V = $mod.GetIt;',
  29417. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  29418. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  29419. '']));
  29420. end;
  29421. procedure TTestModule.TestJSValue_ProcType_Equal;
  29422. begin
  29423. StartProgram(false);
  29424. Add('type');
  29425. Add(' integer = longint;');
  29426. Add(' TObject = class');
  29427. Add(' class function GetGlob: integer;');
  29428. Add(' function Getter: integer;');
  29429. Add(' end;');
  29430. Add('class function TObject.GetGlob: integer;');
  29431. Add('var v1: jsvalue;');
  29432. Add('begin');
  29433. Add(' if v1=@GetGlob then;');
  29434. Add(' if [email protected] then ;');
  29435. Add('end;');
  29436. Add('function TObject.Getter: integer;');
  29437. Add('var v2: jsvalue;');
  29438. Add('begin');
  29439. Add(' if v2=@Getter then;');
  29440. Add(' if [email protected] then ;');
  29441. Add(' if v2=@GetGlob then;');
  29442. Add(' if [email protected] then;');
  29443. Add('end;');
  29444. Add('function GetIt(i: integer): integer;');
  29445. Add('var v3: jsvalue;');
  29446. Add('begin');
  29447. Add(' if v3=@GetIt then;');
  29448. Add('end;');
  29449. Add('var');
  29450. Add(' V: JSValue;');
  29451. Add(' o: TObject;');
  29452. Add('begin');
  29453. Add(' if v=@GetIt then;');
  29454. Add(' if [email protected] then;');
  29455. Add(' if [email protected] then;');
  29456. Add(' if @GetIt=v then;');
  29457. Add(' if @o.Getter=v then;');
  29458. Add(' if @o.GetGlob=v then;');
  29459. ConvertProgram;
  29460. CheckSource('TestJSValue_ProcType_Equal',
  29461. LinesToStr([ // statements
  29462. 'rtl.createClass(this, "TObject", null, function () {',
  29463. ' this.$init = function () {',
  29464. ' };',
  29465. ' this.$final = function () {',
  29466. ' };',
  29467. ' this.GetGlob = function () {',
  29468. ' var Result = 0;',
  29469. ' var v1 = undefined;',
  29470. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29471. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  29472. ' return Result;',
  29473. ' };',
  29474. ' this.Getter = function () {',
  29475. ' var Result = 0;',
  29476. ' var v2 = undefined;',
  29477. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29478. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  29479. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29480. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  29481. ' return Result;',
  29482. ' };',
  29483. '});',
  29484. 'this.GetIt = function (i) {',
  29485. ' var Result = 0;',
  29486. ' var v3 = undefined;',
  29487. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  29488. ' return Result;',
  29489. '};',
  29490. 'this.V = undefined;',
  29491. 'this.o = null;',
  29492. '']),
  29493. LinesToStr([ // $mod.$main
  29494. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  29495. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  29496. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  29497. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  29498. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  29499. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  29500. '']));
  29501. end;
  29502. procedure TTestModule.TestJSValue_ProcType_Param;
  29503. begin
  29504. StartProgram(false);
  29505. Add([
  29506. 'type',
  29507. ' variant = jsvalue;',
  29508. ' TArrVariant = array of variant;',
  29509. ' TArrVar2 = TArrVariant;',
  29510. ' TFuncInt = function: longint;',
  29511. 'function GetIt: longint;',
  29512. 'begin',
  29513. 'end;',
  29514. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  29515. 'var v: variant;',
  29516. 'begin',
  29517. ' v:=arr[1];',
  29518. 'end;',
  29519. 'var s: string;',
  29520. 'begin',
  29521. ' DoIt(GetIt,[]);',
  29522. ' DoIt(@GetIt,[]);',
  29523. ' DoIt(1,[s,GetIt]);',
  29524. ' DoIt(1,[s,@GetIt]);',
  29525. '']);
  29526. ConvertProgram;
  29527. CheckSource('TestJSValue_ProcType_Param',
  29528. LinesToStr([ // statements
  29529. 'this.GetIt = function () {',
  29530. ' var Result = 0;',
  29531. ' return Result;',
  29532. '};',
  29533. 'this.DoIt = function (p, Arr) {',
  29534. ' var v = undefined;',
  29535. ' v = Arr[1];',
  29536. '};',
  29537. 'this.s = "";',
  29538. '']),
  29539. LinesToStr([ // $mod.$main
  29540. '$mod.DoIt($mod.GetIt(), []);',
  29541. '$mod.DoIt($mod.GetIt, []);',
  29542. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  29543. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  29544. '']));
  29545. end;
  29546. procedure TTestModule.TestJSValue_AssignToPointerFail;
  29547. begin
  29548. StartProgram(false);
  29549. Add([
  29550. 'var',
  29551. ' v: JSValue;',
  29552. ' p: Pointer;',
  29553. 'begin',
  29554. ' p:=v;',
  29555. '']);
  29556. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  29557. nIncompatibleTypesGotExpected);
  29558. ConvertProgram;
  29559. end;
  29560. procedure TTestModule.TestJSValue_OverloadDouble;
  29561. begin
  29562. StartProgram(false);
  29563. Add([
  29564. 'type',
  29565. ' integer = longint;',
  29566. ' tdatetime = double;',
  29567. 'procedure DoIt(d: double); begin end;',
  29568. 'procedure DoIt(v: jsvalue); begin end;',
  29569. 'var',
  29570. ' d: double;',
  29571. ' dt: tdatetime;',
  29572. ' i: integer;',
  29573. ' b: byte;',
  29574. ' shi: shortint;',
  29575. ' w: word;',
  29576. ' smi: smallint;',
  29577. ' lw: longword;',
  29578. ' li: longint;',
  29579. ' ni: nativeint;',
  29580. ' nu: nativeuint;',
  29581. 'begin',
  29582. ' DoIt(d);',
  29583. ' DoIt(dt);',
  29584. ' DoIt(i);',
  29585. ' DoIt(b);',
  29586. ' DoIt(shi);',
  29587. ' DoIt(w);',
  29588. ' DoIt(smi);',
  29589. ' DoIt(lw);',
  29590. ' DoIt(li);',
  29591. ' DoIt(ni);',
  29592. ' DoIt(nu);',
  29593. '']);
  29594. ConvertProgram;
  29595. CheckSource('TestJSValue_OverloadDouble',
  29596. LinesToStr([ // statements
  29597. 'this.DoIt = function (d) {',
  29598. '};',
  29599. 'this.DoIt$1 = function (v) {',
  29600. '};',
  29601. 'this.d = 0.0;',
  29602. 'this.dt = 0.0;',
  29603. 'this.i = 0;',
  29604. 'this.b = 0;',
  29605. 'this.shi = 0;',
  29606. 'this.w = 0;',
  29607. 'this.smi = 0;',
  29608. 'this.lw = 0;',
  29609. 'this.li = 0;',
  29610. 'this.ni = 0;',
  29611. 'this.nu = 0;',
  29612. '']),
  29613. LinesToStr([ // $mod.$main
  29614. '$mod.DoIt($mod.d);',
  29615. '$mod.DoIt($mod.dt);',
  29616. '$mod.DoIt$1($mod.i);',
  29617. '$mod.DoIt$1($mod.b);',
  29618. '$mod.DoIt$1($mod.shi);',
  29619. '$mod.DoIt$1($mod.w);',
  29620. '$mod.DoIt$1($mod.smi);',
  29621. '$mod.DoIt$1($mod.lw);',
  29622. '$mod.DoIt$1($mod.li);',
  29623. '$mod.DoIt$1($mod.ni);',
  29624. '$mod.DoIt$1($mod.nu);',
  29625. '']));
  29626. end;
  29627. procedure TTestModule.TestJSValue_OverloadNativeInt;
  29628. begin
  29629. StartProgram(false);
  29630. Add([
  29631. 'type',
  29632. ' integer = longint;',
  29633. ' int53 = nativeint;',
  29634. ' tdatetime = double;',
  29635. 'procedure DoIt(n: nativeint); begin end;',
  29636. 'procedure DoIt(v: jsvalue); begin end;',
  29637. 'var',
  29638. ' d: double;',
  29639. ' dt: tdatetime;',
  29640. ' i: integer;',
  29641. ' b: byte;',
  29642. ' shi: shortint;',
  29643. ' w: word;',
  29644. ' smi: smallint;',
  29645. ' lw: longword;',
  29646. ' li: longint;',
  29647. ' ni: nativeint;',
  29648. ' nu: nativeuint;',
  29649. 'begin',
  29650. ' DoIt(d);',
  29651. ' DoIt(dt);',
  29652. ' DoIt(i);',
  29653. ' DoIt(b);',
  29654. ' DoIt(shi);',
  29655. ' DoIt(w);',
  29656. ' DoIt(smi);',
  29657. ' DoIt(lw);',
  29658. ' DoIt(li);',
  29659. ' DoIt(ni);',
  29660. ' DoIt(nu);',
  29661. '']);
  29662. ConvertProgram;
  29663. CheckSource('TestJSValue_OverloadNativeInt',
  29664. LinesToStr([ // statements
  29665. 'this.DoIt = function (n) {',
  29666. '};',
  29667. 'this.DoIt$1 = function (v) {',
  29668. '};',
  29669. 'this.d = 0.0;',
  29670. 'this.dt = 0.0;',
  29671. 'this.i = 0;',
  29672. 'this.b = 0;',
  29673. 'this.shi = 0;',
  29674. 'this.w = 0;',
  29675. 'this.smi = 0;',
  29676. 'this.lw = 0;',
  29677. 'this.li = 0;',
  29678. 'this.ni = 0;',
  29679. 'this.nu = 0;',
  29680. '']),
  29681. LinesToStr([ // $mod.$main
  29682. '$mod.DoIt$1($mod.d);',
  29683. '$mod.DoIt$1($mod.dt);',
  29684. '$mod.DoIt($mod.i);',
  29685. '$mod.DoIt($mod.b);',
  29686. '$mod.DoIt($mod.shi);',
  29687. '$mod.DoIt($mod.w);',
  29688. '$mod.DoIt($mod.smi);',
  29689. '$mod.DoIt($mod.lw);',
  29690. '$mod.DoIt($mod.li);',
  29691. '$mod.DoIt($mod.ni);',
  29692. '$mod.DoIt($mod.nu);',
  29693. '']));
  29694. end;
  29695. procedure TTestModule.TestJSValue_OverloadWord;
  29696. begin
  29697. StartProgram(false);
  29698. Add([
  29699. 'type',
  29700. ' integer = longint;',
  29701. ' int53 = nativeint;',
  29702. ' tdatetime = double;',
  29703. 'procedure DoIt(w: word); begin end;',
  29704. 'procedure DoIt(v: jsvalue); begin end;',
  29705. 'var',
  29706. ' d: double;',
  29707. ' dt: tdatetime;',
  29708. ' i: integer;',
  29709. ' b: byte;',
  29710. ' shi: shortint;',
  29711. ' w: word;',
  29712. ' smi: smallint;',
  29713. ' lw: longword;',
  29714. ' li: longint;',
  29715. ' ni: nativeint;',
  29716. ' nu: nativeuint;',
  29717. 'begin',
  29718. ' DoIt(d);',
  29719. ' DoIt(dt);',
  29720. ' DoIt(i);',
  29721. ' DoIt(b);',
  29722. ' DoIt(shi);',
  29723. ' DoIt(w);',
  29724. ' DoIt(smi);',
  29725. ' DoIt(lw);',
  29726. ' DoIt(li);',
  29727. ' DoIt(ni);',
  29728. ' DoIt(nu);',
  29729. '']);
  29730. ConvertProgram;
  29731. CheckSource('TestJSValue_OverloadWord',
  29732. LinesToStr([ // statements
  29733. 'this.DoIt = function (w) {',
  29734. '};',
  29735. 'this.DoIt$1 = function (v) {',
  29736. '};',
  29737. 'this.d = 0.0;',
  29738. 'this.dt = 0.0;',
  29739. 'this.i = 0;',
  29740. 'this.b = 0;',
  29741. 'this.shi = 0;',
  29742. 'this.w = 0;',
  29743. 'this.smi = 0;',
  29744. 'this.lw = 0;',
  29745. 'this.li = 0;',
  29746. 'this.ni = 0;',
  29747. 'this.nu = 0;',
  29748. '']),
  29749. LinesToStr([ // $mod.$main
  29750. '$mod.DoIt$1($mod.d);',
  29751. '$mod.DoIt$1($mod.dt);',
  29752. '$mod.DoIt$1($mod.i);',
  29753. '$mod.DoIt($mod.b);',
  29754. '$mod.DoIt($mod.shi);',
  29755. '$mod.DoIt($mod.w);',
  29756. '$mod.DoIt$1($mod.smi);',
  29757. '$mod.DoIt$1($mod.lw);',
  29758. '$mod.DoIt$1($mod.li);',
  29759. '$mod.DoIt$1($mod.ni);',
  29760. '$mod.DoIt$1($mod.nu);',
  29761. '']));
  29762. end;
  29763. procedure TTestModule.TestJSValue_OverloadString;
  29764. begin
  29765. StartProgram(false);
  29766. Add([
  29767. 'type',
  29768. ' uni = string;',
  29769. ' WChar = char;',
  29770. 'procedure DoIt(s: string); begin end;',
  29771. 'procedure DoIt(v: jsvalue); begin end;',
  29772. 'var',
  29773. ' s: string;',
  29774. ' c: char;',
  29775. ' u: uni;',
  29776. 'begin',
  29777. ' DoIt(s);',
  29778. ' DoIt(c);',
  29779. ' DoIt(u);',
  29780. '']);
  29781. ConvertProgram;
  29782. CheckSource('TestJSValue_OverloadString',
  29783. LinesToStr([ // statements
  29784. 'this.DoIt = function (s) {',
  29785. '};',
  29786. 'this.DoIt$1 = function (v) {',
  29787. '};',
  29788. 'this.s = "";',
  29789. 'this.c = "";',
  29790. 'this.u = "";',
  29791. '']),
  29792. LinesToStr([ // $mod.$main
  29793. '$mod.DoIt($mod.s);',
  29794. '$mod.DoIt($mod.c);',
  29795. '$mod.DoIt($mod.u);',
  29796. '']));
  29797. end;
  29798. procedure TTestModule.TestJSValue_OverloadChar;
  29799. begin
  29800. StartProgram(false);
  29801. Add([
  29802. 'type',
  29803. ' uni = string;',
  29804. ' WChar = char;',
  29805. 'procedure DoIt(c: char); begin end;',
  29806. 'procedure DoIt(v: jsvalue); begin end;',
  29807. 'var',
  29808. ' s: string;',
  29809. ' c: char;',
  29810. ' u: uni;',
  29811. 'begin',
  29812. ' DoIt(s);',
  29813. ' DoIt(c);',
  29814. ' DoIt(u);',
  29815. '']);
  29816. ConvertProgram;
  29817. CheckSource('TestJSValue_OverloadChar',
  29818. LinesToStr([ // statements
  29819. 'this.DoIt = function (c) {',
  29820. '};',
  29821. 'this.DoIt$1 = function (v) {',
  29822. '};',
  29823. 'this.s = "";',
  29824. 'this.c = "";',
  29825. 'this.u = "";',
  29826. '']),
  29827. LinesToStr([ // $mod.$main
  29828. '$mod.DoIt$1($mod.s);',
  29829. '$mod.DoIt($mod.c);',
  29830. '$mod.DoIt$1($mod.u);',
  29831. '']));
  29832. end;
  29833. procedure TTestModule.TestJSValue_OverloadPointer;
  29834. begin
  29835. StartProgram(false);
  29836. Add([
  29837. 'type',
  29838. ' TObject = class end;',
  29839. 'procedure DoIt(p: pointer); begin end;',
  29840. 'procedure DoIt(v: jsvalue); begin end;',
  29841. 'var',
  29842. ' o: TObject;',
  29843. 'begin',
  29844. ' DoIt(o);',
  29845. '']);
  29846. ConvertProgram;
  29847. CheckSource('TestJSValue_OverloadPointer',
  29848. LinesToStr([ // statements
  29849. 'rtl.createClass(this, "TObject", null, function () {',
  29850. ' this.$init = function () {',
  29851. ' };',
  29852. ' this.$final = function () {',
  29853. ' };',
  29854. '});',
  29855. 'this.DoIt = function (p) {',
  29856. '};',
  29857. 'this.DoIt$1 = function (v) {',
  29858. '};',
  29859. 'this.o = null;',
  29860. '']),
  29861. LinesToStr([ // $mod.$main
  29862. '$mod.DoIt($mod.o);',
  29863. '']));
  29864. end;
  29865. procedure TTestModule.TestJSValue_ForIn;
  29866. begin
  29867. StartProgram(false);
  29868. Add([
  29869. 'var',
  29870. ' v: JSValue;',
  29871. ' key: string;',
  29872. 'begin',
  29873. ' for key in v do begin',
  29874. ' if key=''abc'' then ;',
  29875. ' end;',
  29876. '']);
  29877. ConvertProgram;
  29878. CheckSource('TestJSValue_ForIn',
  29879. LinesToStr([ // statements
  29880. 'this.v = undefined;',
  29881. 'this.key = "";',
  29882. '']),
  29883. LinesToStr([ // $mod.$main
  29884. 'for ($mod.key in $mod.v) {',
  29885. ' if ($mod.key === "abc") ;',
  29886. '};',
  29887. '']));
  29888. end;
  29889. procedure TTestModule.TestRTTI_IntRange;
  29890. begin
  29891. WithTypeInfo:=true;
  29892. StartProgram(true,[supTypeInfo]);
  29893. Add([
  29894. '{$modeswitch externalclass}',
  29895. 'type',
  29896. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  29897. ' TColor = type TGraphicsColor;',
  29898. 'var',
  29899. ' p: TTypeInfo;',
  29900. ' k: TTypeKind;',
  29901. 'begin',
  29902. ' p:=typeinfo(TGraphicsColor);',
  29903. ' p:=typeinfo(TColor);',
  29904. ' k:=GetTypeKind(TGraphicsColor);',
  29905. ' k:=GetTypeKind(TColor);',
  29906. '']);
  29907. ConvertProgram;
  29908. CheckSource('TestRTTI_IntRange',
  29909. LinesToStr([ // statements
  29910. 'this.$rtti.$Int("TGraphicsColor", {',
  29911. ' minvalue: -2147483648,',
  29912. ' maxvalue: 2147483647,',
  29913. ' ordtype: 4',
  29914. '});',
  29915. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  29916. 'this.p = null;',
  29917. 'this.k = 0;',
  29918. '']),
  29919. LinesToStr([ // $mod.$main
  29920. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  29921. '$mod.p = $mod.$rtti["TColor"];',
  29922. '$mod.k = 1;',
  29923. '$mod.k = 1;',
  29924. '']));
  29925. end;
  29926. procedure TTestModule.TestRTTI_Double;
  29927. begin
  29928. WithTypeInfo:=true;
  29929. StartProgram(true,[supTypeInfo]);
  29930. Add([
  29931. '{$modeswitch externalclass}',
  29932. 'type',
  29933. ' TFloat = type double;',
  29934. 'var',
  29935. ' p: TTypeInfo;',
  29936. 'begin',
  29937. ' p:=typeinfo(double);',
  29938. ' p:=typeinfo(TFloat);',
  29939. '']);
  29940. ConvertProgram;
  29941. CheckSource('TestRTTI_Double',
  29942. LinesToStr([ // statements
  29943. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  29944. 'this.p = null;',
  29945. '']),
  29946. LinesToStr([ // $mod.$main
  29947. '$mod.p = rtl.double;',
  29948. '$mod.p = $mod.$rtti["TFloat"];',
  29949. '']));
  29950. end;
  29951. procedure TTestModule.TestRTTI_ProcType;
  29952. begin
  29953. WithTypeInfo:=true;
  29954. StartProgram(false);
  29955. Add('type');
  29956. Add(' TProcA = procedure;');
  29957. Add(' TMethodB = procedure of object;');
  29958. Add(' TProcC = procedure; varargs;');
  29959. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  29960. Add(' TProcE = function: nativeint;');
  29961. Add(' TProcF = function(const p: TProcA): nativeuint;');
  29962. Add('var p: pointer;');
  29963. Add('begin');
  29964. Add(' p:=typeinfo(tproca);');
  29965. ConvertProgram;
  29966. CheckSource('TestRTTI_ProcType',
  29967. LinesToStr([ // statements
  29968. 'this.$rtti.$ProcVar("TProcA", {',
  29969. ' procsig: rtl.newTIProcSig([])',
  29970. '});',
  29971. 'this.$rtti.$MethodVar("TMethodB", {',
  29972. ' procsig: rtl.newTIProcSig([]),',
  29973. ' methodkind: 0',
  29974. '});',
  29975. 'this.$rtti.$ProcVar("TProcC", {',
  29976. ' procsig: rtl.newTIProcSig([], null, 2)',
  29977. '});',
  29978. 'this.$rtti.$ProcVar("TProcD", {',
  29979. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  29980. '});',
  29981. 'this.$rtti.$ProcVar("TProcE", {',
  29982. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  29983. '});',
  29984. 'this.$rtti.$ProcVar("TProcF", {',
  29985. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  29986. '});',
  29987. 'this.p = null;',
  29988. '']),
  29989. LinesToStr([ // $mod.$main
  29990. '$mod.p = $mod.$rtti["TProcA"];',
  29991. '']));
  29992. end;
  29993. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  29994. begin
  29995. WithTypeInfo:=true;
  29996. AddModuleWithIntfImplSrc('unit2.pas',
  29997. LinesToStr([
  29998. 'type',
  29999. ' TObject = class end;'
  30000. ]),
  30001. '');
  30002. StartUnit(true);
  30003. Add('interface');
  30004. Add('uses unit2;');
  30005. Add('type');
  30006. Add(' TProcA = function(o: tobject): tobject;');
  30007. Add('implementation');
  30008. Add('type');
  30009. Add(' TProcB = function(o: tobject): tobject;');
  30010. Add('var p: Pointer;');
  30011. Add('initialization');
  30012. Add(' p:=typeinfo(tproca);');
  30013. Add(' p:=typeinfo(tprocb);');
  30014. ConvertUnit;
  30015. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30016. LinesToStr([ // statements
  30017. 'var $impl = $mod.$impl;',
  30018. 'this.$rtti.$ProcVar("TProcA", {',
  30019. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30020. '});',
  30021. '']),
  30022. LinesToStr([ // this.$init
  30023. '$impl.p = $mod.$rtti["TProcA"];',
  30024. '$impl.p = $mod.$rtti["TProcB"];',
  30025. '']),
  30026. LinesToStr([ // implementation
  30027. '$mod.$rtti.$ProcVar("TProcB", {',
  30028. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30029. '});',
  30030. '$impl.p = null;',
  30031. '']) );
  30032. end;
  30033. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30034. begin
  30035. WithTypeInfo:=true;
  30036. StartProgram(false);
  30037. Add(['var',
  30038. ' ProcA: procedure;',
  30039. ' MethodB: procedure of object;',
  30040. ' ProcC: procedure; varargs;',
  30041. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30042. ' ProcE: function: nativeint;',
  30043. ' p: pointer;',
  30044. 'begin',
  30045. ' p:=typeinfo(proca);']);
  30046. ConvertProgram;
  30047. CheckSource('TestRTTI_ProcTypeAnonymous',
  30048. LinesToStr([ // statements
  30049. 'this.$rtti.$ProcVar("ProcA$a", {',
  30050. ' procsig: rtl.newTIProcSig([])',
  30051. '});',
  30052. 'this.ProcA = null;',
  30053. 'this.$rtti.$MethodVar("MethodB$a", {',
  30054. ' procsig: rtl.newTIProcSig([]),',
  30055. ' methodkind: 0',
  30056. '});',
  30057. 'this.MethodB = null;',
  30058. 'this.$rtti.$ProcVar("ProcC$a", {',
  30059. ' procsig: rtl.newTIProcSig([], null, 2)',
  30060. '});',
  30061. 'this.ProcC = null;',
  30062. 'this.$rtti.$ProcVar("ProcD$a", {',
  30063. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30064. '});',
  30065. 'this.ProcD = null;',
  30066. 'this.$rtti.$ProcVar("ProcE$a", {',
  30067. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30068. '});',
  30069. 'this.ProcE = null;',
  30070. 'this.p = null;',
  30071. '']),
  30072. LinesToStr([ // $mod.$main
  30073. '$mod.p = $mod.$rtti["ProcA$a"];',
  30074. '']));
  30075. end;
  30076. procedure TTestModule.TestRTTI_EnumAndSetType;
  30077. begin
  30078. WithTypeInfo:=true;
  30079. StartProgram(false);
  30080. Add('type');
  30081. Add(' TFlag = (light,dark);');
  30082. Add(' TFlags = set of TFlag;');
  30083. Add(' TProc = function(f: TFlags): TFlag;');
  30084. Add('var p: pointer;');
  30085. Add('begin');
  30086. Add(' p:=typeinfo(tflag);');
  30087. Add(' p:=typeinfo(tflags);');
  30088. ConvertProgram;
  30089. CheckSource('TestRTTI_EnumAndType',
  30090. LinesToStr([ // statements
  30091. 'this.TFlag = {',
  30092. ' "0": "light",',
  30093. ' light: 0,',
  30094. ' "1": "dark",',
  30095. ' dark: 1',
  30096. '};',
  30097. 'this.$rtti.$Enum("TFlag", {',
  30098. ' minvalue: 0,',
  30099. ' maxvalue: 1,',
  30100. ' ordtype: 1,',
  30101. ' enumtype: this.TFlag',
  30102. '});',
  30103. 'this.$rtti.$Set("TFlags", {',
  30104. ' comptype: this.$rtti["TFlag"]',
  30105. '});',
  30106. 'this.$rtti.$ProcVar("TProc", {',
  30107. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30108. '});',
  30109. 'this.p = null;',
  30110. '']),
  30111. LinesToStr([ // $mod.$main
  30112. '$mod.p = $mod.$rtti["TFlag"];',
  30113. '$mod.p = $mod.$rtti["TFlags"];',
  30114. '']));
  30115. end;
  30116. procedure TTestModule.TestRTTI_EnumRange;
  30117. begin
  30118. WithTypeInfo:=true;
  30119. StartProgram(false);
  30120. Add([
  30121. 'type',
  30122. ' TCol = (red,green,blue);',
  30123. ' TColRg = green..blue;',
  30124. ' TSetOfColRg = set of TColRg;',
  30125. 'var p: pointer;',
  30126. 'begin',
  30127. ' p:=typeinfo(tcolrg);',
  30128. ' p:=typeinfo(tsetofcolrg);',
  30129. '']);
  30130. ConvertProgram;
  30131. end;
  30132. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30133. begin
  30134. WithTypeInfo:=true;
  30135. StartProgram(false);
  30136. Add('type');
  30137. Add(' TFlags = set of (red, green);');
  30138. Add('var');
  30139. Add(' f: TFlags;');
  30140. Add('begin');
  30141. Add(' Include(f,red);');
  30142. ConvertProgram;
  30143. CheckSource('TestRTTI_AnonymousEnumType',
  30144. LinesToStr([ // statements
  30145. 'this.TFlags$a = {',
  30146. ' "0": "red",',
  30147. ' red: 0,',
  30148. ' "1": "green",',
  30149. ' green: 1',
  30150. '};',
  30151. 'this.$rtti.$Enum("TFlags$a", {',
  30152. ' minvalue: 0,',
  30153. ' maxvalue: 1,',
  30154. ' ordtype: 1,',
  30155. ' enumtype: this.TFlags$a',
  30156. '});',
  30157. 'this.$rtti.$Set("TFlags", {',
  30158. ' comptype: this.$rtti["TFlags$a"]',
  30159. '});',
  30160. 'this.f = {};',
  30161. '']),
  30162. LinesToStr([
  30163. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30164. '']));
  30165. end;
  30166. procedure TTestModule.TestRTTI_StaticArray;
  30167. begin
  30168. WithTypeInfo:=true;
  30169. StartProgram(false);
  30170. Add('type');
  30171. Add(' TFlag = (light,dark);');
  30172. Add(' TFlagNames = array[TFlag] of string;');
  30173. Add(' TBoolNames = array[boolean] of string;');
  30174. Add(' TByteArray = array[1..32768] of byte;');
  30175. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30176. Add('var p: pointer;');
  30177. Add('begin');
  30178. Add(' p:=typeinfo(TFlagNames);');
  30179. Add(' p:=typeinfo(TBoolNames);');
  30180. ConvertProgram;
  30181. CheckSource('TestRTTI_StaticArray',
  30182. LinesToStr([ // statements
  30183. 'this.TFlag = {',
  30184. ' "0": "light",',
  30185. ' light: 0,',
  30186. ' "1": "dark",',
  30187. ' dark: 1',
  30188. '};',
  30189. 'this.$rtti.$Enum("TFlag", {',
  30190. ' minvalue: 0,',
  30191. ' maxvalue: 1,',
  30192. ' ordtype: 1,',
  30193. ' enumtype: this.TFlag',
  30194. '});',
  30195. 'this.$rtti.$StaticArray("TFlagNames", {',
  30196. ' dims: [2],',
  30197. ' eltype: rtl.string',
  30198. '});',
  30199. 'this.$rtti.$StaticArray("TBoolNames", {',
  30200. ' dims: [2],',
  30201. ' eltype: rtl.string',
  30202. '});',
  30203. 'this.$rtti.$StaticArray("TByteArray", {',
  30204. ' dims: [32768],',
  30205. ' eltype: rtl.byte',
  30206. '});',
  30207. 'this.$rtti.$ProcVar("TProc", {',
  30208. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30209. '});',
  30210. 'this.p = null;',
  30211. '']),
  30212. LinesToStr([ // $mod.$main
  30213. '$mod.p = $mod.$rtti["TFlagNames"];',
  30214. '$mod.p = $mod.$rtti["TBoolNames"];',
  30215. '']));
  30216. end;
  30217. procedure TTestModule.TestRTTI_DynArray;
  30218. begin
  30219. WithTypeInfo:=true;
  30220. StartProgram(false);
  30221. Add('type');
  30222. Add(' TArrStr = array of string;');
  30223. Add(' TArr2Dim = array of tarrstr;');
  30224. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30225. Add('var p: pointer;');
  30226. Add('begin');
  30227. Add(' p:=typeinfo(tarrstr);');
  30228. Add(' p:=typeinfo(tarr2dim);');
  30229. ConvertProgram;
  30230. CheckSource('TestRTTI_DynArray',
  30231. LinesToStr([ // statements
  30232. 'this.$rtti.$DynArray("TArrStr", {',
  30233. ' eltype: rtl.string',
  30234. '});',
  30235. 'this.$rtti.$DynArray("TArr2Dim", {',
  30236. ' eltype: this.$rtti["TArrStr"]',
  30237. '});',
  30238. 'this.$rtti.$ProcVar("TProc", {',
  30239. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30240. '});',
  30241. 'this.p = null;',
  30242. '']),
  30243. LinesToStr([ // $mod.$main
  30244. '$mod.p = $mod.$rtti["TArrStr"];',
  30245. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30246. '']));
  30247. end;
  30248. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30249. begin
  30250. WithTypeInfo:=true;
  30251. StartProgram(false);
  30252. Add('type');
  30253. Add(' TArr = array of array of longint;');
  30254. Add('var a: TArr;');
  30255. Add('begin');
  30256. ConvertProgram;
  30257. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30258. LinesToStr([ // statements
  30259. 'this.$rtti.$DynArray("TArr$a", {',
  30260. ' eltype: rtl.longint',
  30261. '});',
  30262. 'this.$rtti.$DynArray("TArr", {',
  30263. ' eltype: this.$rtti["TArr$a"]',
  30264. '});',
  30265. 'this.a = [];',
  30266. '']),
  30267. LinesToStr([ // $mod.$main
  30268. ]));
  30269. end;
  30270. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30271. begin
  30272. WithTypeInfo:=true;
  30273. StartProgram(false);
  30274. Add('type');
  30275. Add(' TObject = class');
  30276. Add(' published');
  30277. Add(' procedure Proc; virtual; abstract;');
  30278. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30279. Add(' end;');
  30280. Add('begin');
  30281. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30282. nDuplicatePublishedMethodXAtY);
  30283. ConvertProgram;
  30284. end;
  30285. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30286. begin
  30287. WithTypeInfo:=true;
  30288. StartUnit(false);
  30289. Add([
  30290. 'interface',
  30291. 'type',
  30292. ' TObject = class',
  30293. ' end;',
  30294. ' {$M+}',
  30295. ' TBird = class',
  30296. ' procedure Fly;',
  30297. ' end;',
  30298. ' {$M-}',
  30299. 'type',
  30300. ' TEagle = class(TBird)',
  30301. ' procedure Fly;',
  30302. ' end;',
  30303. 'implementation',
  30304. 'procedure TBird.Fly;',
  30305. 'begin',
  30306. 'end;',
  30307. 'procedure TEagle.Fly;',
  30308. 'begin',
  30309. 'end;',
  30310. '']);
  30311. ConvertUnit;
  30312. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30313. LinesToStr([ // statements
  30314. 'rtl.createClass(this, "TObject", null, function () {',
  30315. ' this.$init = function () {',
  30316. ' };',
  30317. ' this.$final = function () {',
  30318. ' };',
  30319. '});',
  30320. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30321. ' this.Fly = function () {',
  30322. ' };',
  30323. ' var $r = this.$rtti;',
  30324. ' $r.addMethod("Fly", 0, []);',
  30325. '});',
  30326. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30327. ' this.Fly = function () {',
  30328. ' };',
  30329. ' var $r = this.$rtti;',
  30330. ' $r.addMethod("Fly", 0, []);',
  30331. '});',
  30332. '']),
  30333. LinesToStr([ // $mod.$main
  30334. ]));
  30335. CheckResolverUnexpectedHints(true);
  30336. end;
  30337. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30338. begin
  30339. WithTypeInfo:=true;
  30340. StartProgram(false);
  30341. Add('type');
  30342. Add(' TObject = class');
  30343. Add(' published');
  30344. Add(' procedure Proc; external name ''foo'';');
  30345. Add(' end;');
  30346. Add('begin');
  30347. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30348. nPublishedNameMustMatchExternal);
  30349. ConvertProgram;
  30350. end;
  30351. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30352. begin
  30353. WithTypeInfo:=true;
  30354. StartProgram(false);
  30355. Add('type');
  30356. Add(' TObject = class');
  30357. Add(' class var FA: longint;');
  30358. Add(' published');
  30359. Add(' class property A: longint read FA;');
  30360. Add(' end;');
  30361. Add('begin');
  30362. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30363. nInvalidXModifierY);
  30364. ConvertProgram;
  30365. end;
  30366. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30367. begin
  30368. WithTypeInfo:=true;
  30369. StartProgram(false);
  30370. Add('type');
  30371. Add(' TObject = class');
  30372. Add(' published');
  30373. Add(' class var FA: longint;');
  30374. Add(' end;');
  30375. Add('begin');
  30376. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30377. nSymbolCannotBePublished);
  30378. ConvertProgram;
  30379. end;
  30380. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30381. begin
  30382. WithTypeInfo:=true;
  30383. StartProgram(false);
  30384. Add('{$modeswitch externalclass}');
  30385. Add('type');
  30386. Add(' TObject = class');
  30387. Add(' published');
  30388. Add(' V: longint; external name ''foo'';');
  30389. Add(' end;');
  30390. Add('begin');
  30391. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30392. nPublishedNameMustMatchExternal);
  30393. ConvertProgram;
  30394. end;
  30395. procedure TTestModule.TestRTTI_Class_Field;
  30396. begin
  30397. WithTypeInfo:=true;
  30398. StartProgram(false);
  30399. Add('{$modeswitch externalclass}');
  30400. Add('type');
  30401. Add(' TObject = class');
  30402. Add(' private');
  30403. Add(' FPropA: string;');
  30404. Add(' published');
  30405. Add(' VarLI: longint;');
  30406. Add(' VarC: char;');
  30407. Add(' VarS: string;');
  30408. Add(' VarD: double;');
  30409. Add(' VarB: boolean;');
  30410. Add(' VarLW: longword;');
  30411. Add(' VarSmI: smallint;');
  30412. Add(' VarW: word;');
  30413. Add(' VarShI: shortint;');
  30414. Add(' VarBy: byte;');
  30415. Add(' VarExt: longint external name ''VarExt'';');
  30416. Add(' ArrA, ArrB: array of byte;');
  30417. Add(' end;');
  30418. Add('var p: pointer;');
  30419. Add(' Obj: tobject;');
  30420. Add('begin');
  30421. Add(' p:=typeinfo(tobject);');
  30422. Add(' p:=typeinfo(p);');
  30423. Add(' p:=typeinfo(obj);');
  30424. ConvertProgram;
  30425. CheckSource('TestRTTI_Class_Field',
  30426. LinesToStr([ // statements
  30427. 'rtl.createClass(this, "TObject", null, function () {',
  30428. ' this.$init = function () {',
  30429. ' this.FPropA = "";',
  30430. ' this.VarLI = 0;',
  30431. ' this.VarC = "";',
  30432. ' this.VarS = "";',
  30433. ' this.VarD = 0.0;',
  30434. ' this.VarB = false;',
  30435. ' this.VarLW = 0;',
  30436. ' this.VarSmI = 0;',
  30437. ' this.VarW = 0;',
  30438. ' this.VarShI = 0;',
  30439. ' this.VarBy = 0;',
  30440. ' this.ArrA = [];',
  30441. ' this.ArrB = [];',
  30442. ' };',
  30443. ' this.$final = function () {',
  30444. ' this.ArrA = undefined;',
  30445. ' this.ArrB = undefined;',
  30446. ' };',
  30447. ' var $r = this.$rtti;',
  30448. ' $r.addField("VarLI", rtl.longint);',
  30449. ' $r.addField("VarC", rtl.char);',
  30450. ' $r.addField("VarS", rtl.string);',
  30451. ' $r.addField("VarD", rtl.double);',
  30452. ' $r.addField("VarB", rtl.boolean);',
  30453. ' $r.addField("VarLW", rtl.longword);',
  30454. ' $r.addField("VarSmI", rtl.smallint);',
  30455. ' $r.addField("VarW", rtl.word);',
  30456. ' $r.addField("VarShI", rtl.shortint);',
  30457. ' $r.addField("VarBy", rtl.byte);',
  30458. ' $r.addField("VarExt", rtl.longint);',
  30459. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  30460. ' eltype: rtl.byte',
  30461. ' });',
  30462. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  30463. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  30464. '});',
  30465. 'this.p = null;',
  30466. 'this.Obj = null;',
  30467. '']),
  30468. LinesToStr([ // $mod.$main
  30469. '$mod.p = $mod.$rtti["TObject"];',
  30470. '$mod.p = rtl.pointer;',
  30471. '$mod.p = $mod.Obj.$rtti;',
  30472. '']));
  30473. end;
  30474. procedure TTestModule.TestRTTI_Class_Method;
  30475. begin
  30476. WithTypeInfo:=true;
  30477. StartProgram(false);
  30478. Add([
  30479. 'type',
  30480. ' TObject = class',
  30481. ' private',
  30482. ' procedure Internal; external name ''$intern'';',
  30483. ' published',
  30484. ' procedure Click; virtual; abstract;',
  30485. ' procedure Notify(Sender: TObject); virtual; abstract;',
  30486. ' function GetNotify: boolean; external name ''GetNotify'';',
  30487. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  30488. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  30489. ' end;',
  30490. 'begin']);
  30491. ConvertProgram;
  30492. CheckSource('TestRTTI_Class_Method',
  30493. LinesToStr([ // statements
  30494. 'rtl.createClass(this, "TObject", null, function () {',
  30495. ' this.$init = function () {',
  30496. ' };',
  30497. ' this.$final = function () {',
  30498. ' };',
  30499. ' var $r = this.$rtti;',
  30500. ' $r.addMethod("Click", 0, []);',
  30501. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  30502. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  30503. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  30504. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  30505. '});',
  30506. '']),
  30507. LinesToStr([ // $mod.$main
  30508. '']));
  30509. end;
  30510. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  30511. begin
  30512. WithTypeInfo:=true;
  30513. StartProgram(false);
  30514. Add('type');
  30515. Add(' TObject = class');
  30516. Add(' published');
  30517. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  30518. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  30519. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  30520. Add(' end;');
  30521. Add('begin');
  30522. ConvertProgram;
  30523. CheckSource('TestRTTI_Class_MethodOpenArray',
  30524. LinesToStr([ // statements
  30525. 'rtl.createClass(this, "TObject", null, function () {',
  30526. ' this.$init = function () {',
  30527. ' };',
  30528. ' this.$final = function () {',
  30529. ' };',
  30530. ' var $r = this.$rtti;',
  30531. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  30532. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  30533. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  30534. '});',
  30535. '']),
  30536. LinesToStr([ // $mod.$main
  30537. '']));
  30538. end;
  30539. procedure TTestModule.TestRTTI_Class_Property;
  30540. begin
  30541. WithTypeInfo:=true;
  30542. StartProgram(false);
  30543. Add('{$modeswitch externalclass}');
  30544. Add('type');
  30545. Add(' TObject = class');
  30546. Add(' private');
  30547. Add(' FColor: longint;');
  30548. Add(' FColorStored: boolean;');
  30549. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  30550. Add(' function GetColor: longint; virtual; abstract;');
  30551. Add(' function GetColorStored: boolean; virtual; abstract;');
  30552. Add(' FExtSize: longint external name ''$extSize'';');
  30553. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  30554. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  30555. Add(' function GetExtSize: longint; external name ''$getSize'';');
  30556. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  30557. Add(' published');
  30558. Add(' property ColorA: longint read FColor;');
  30559. Add(' property ColorB: longint write FColor;');
  30560. Add(' property ColorC: longint read GetColor write SetColor;');
  30561. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  30562. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  30563. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  30564. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  30565. Add(' end;');
  30566. Add('begin');
  30567. ConvertProgram;
  30568. CheckSource('TestRTTI_Class_Property',
  30569. LinesToStr([ // statements
  30570. 'rtl.createClass(this, "TObject", null, function () {',
  30571. ' this.$init = function () {',
  30572. ' this.FColor = 0;',
  30573. ' this.FColorStored = false;',
  30574. ' };',
  30575. ' this.$final = function () {',
  30576. ' };',
  30577. ' var $r = this.$rtti;',
  30578. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  30579. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  30580. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  30581. ' $r.addProperty(',
  30582. ' "ColorD",',
  30583. ' 8,',
  30584. ' rtl.longint,',
  30585. ' "FColor",',
  30586. ' "FColor",',
  30587. ' {',
  30588. ' stored: "FColorStored"',
  30589. ' }',
  30590. ' );',
  30591. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  30592. ' $r.addProperty(',
  30593. ' "ExtSizeB",',
  30594. ' 11,',
  30595. ' rtl.longint,',
  30596. ' "$getSize",',
  30597. ' "$setSize",',
  30598. ' {',
  30599. ' stored: "$extSizeStored"',
  30600. ' }',
  30601. ' );',
  30602. ' $r.addProperty(',
  30603. ' "ExtSizeC",',
  30604. ' 12,',
  30605. ' rtl.longint,',
  30606. ' "$extSize",',
  30607. ' "$extSize",',
  30608. ' {',
  30609. ' stored: "$getExtSizeStored"',
  30610. ' }',
  30611. ' );',
  30612. '});',
  30613. '']),
  30614. LinesToStr([ // $mod.$main
  30615. '']));
  30616. end;
  30617. procedure TTestModule.TestRTTI_Class_PropertyParams;
  30618. begin
  30619. WithTypeInfo:=true;
  30620. StartProgram(false);
  30621. Add('{$modeswitch externalclass}');
  30622. Add('type');
  30623. Add(' integer = longint;');
  30624. Add(' TObject = class');
  30625. Add(' private');
  30626. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  30627. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  30628. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  30629. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  30630. Add(' published');
  30631. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  30632. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  30633. Add(' end;');
  30634. Add('begin');
  30635. ConvertProgram;
  30636. CheckSource('TestRTTI_Class_PropertyParams',
  30637. LinesToStr([ // statements
  30638. 'rtl.createClass(this, "TObject", null, function () {',
  30639. ' this.$init = function () {',
  30640. ' };',
  30641. ' this.$final = function () {',
  30642. ' };',
  30643. ' var $r = this.$rtti;',
  30644. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  30645. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  30646. '});',
  30647. '']),
  30648. LinesToStr([ // $mod.$main
  30649. '']));
  30650. end;
  30651. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  30652. begin
  30653. WithTypeInfo:=true;
  30654. AddModuleWithIntfImplSrc('unit1.pas',
  30655. 'type TColor = -5..5;',
  30656. '');
  30657. StartProgram(true);
  30658. Add([
  30659. 'uses unit1;',
  30660. 'type',
  30661. ' TColorAlias = TColor;',
  30662. ' TColorTypeAlias = type TColor;',
  30663. ' TObject = class',
  30664. ' private',
  30665. ' fColor: TColor;',
  30666. ' fAlias: TColorAlias;',
  30667. ' fTypeAlias: TColorTypeAlias;',
  30668. ' published',
  30669. ' property Color: TColor read fcolor;',
  30670. ' property Alias: TColorAlias read falias;',
  30671. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  30672. ' end;',
  30673. 'begin',
  30674. '']);
  30675. ConvertProgram;
  30676. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  30677. LinesToStr([ // statements
  30678. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  30679. 'rtl.createClass(this, "TObject", null, function () {',
  30680. ' this.$init = function () {',
  30681. ' this.fColor = 0;',
  30682. ' this.fAlias = 0;',
  30683. ' this.fTypeAlias = 0;',
  30684. ' };',
  30685. ' this.$final = function () {',
  30686. ' };',
  30687. ' var $r = this.$rtti;',
  30688. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  30689. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  30690. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  30691. '});',
  30692. '']),
  30693. LinesToStr([ // $mod.$main
  30694. '']));
  30695. end;
  30696. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  30697. begin
  30698. WithTypeInfo:=true;
  30699. StartProgram(false);
  30700. Add([
  30701. '{$modeswitch omitrtti}',
  30702. 'type',
  30703. ' TObject = class',
  30704. ' private',
  30705. ' FA: byte;',
  30706. ' published',
  30707. ' property A: byte read FA write FA;',
  30708. ' end;',
  30709. 'begin']);
  30710. ConvertProgram;
  30711. CheckSource('TestRTTI_Class_OmitRTTI',
  30712. LinesToStr([ // statements
  30713. 'rtl.createClass(this, "TObject", null, function () {',
  30714. ' this.$init = function () {',
  30715. ' this.FA = 0;',
  30716. ' };',
  30717. ' this.$final = function () {',
  30718. ' };',
  30719. '});',
  30720. '']),
  30721. LinesToStr([ // $mod.$main
  30722. '']));
  30723. end;
  30724. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  30725. begin
  30726. WithTypeInfo:=true;
  30727. StartUnit(true,[supTObject]);
  30728. Add([
  30729. 'interface',
  30730. 'type',
  30731. ' {$M+}',
  30732. ' TBird = class',
  30733. ' published',
  30734. ' Swarm: array of TBird;',
  30735. ' end;',
  30736. 'implementation',
  30737. '']);
  30738. ConvertUnit;
  30739. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  30740. LinesToStr([ // statements
  30741. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  30742. ' this.$init = function () {',
  30743. ' pas.system.TObject.$init.call(this);',
  30744. ' this.Swarm = [];',
  30745. ' };',
  30746. ' this.$final = function () {',
  30747. ' this.Swarm = undefined;',
  30748. ' pas.system.TObject.$final.call(this);',
  30749. ' };',
  30750. ' var $r = this.$rtti;',
  30751. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  30752. ' eltype: $r',
  30753. ' });',
  30754. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  30755. '});',
  30756. '']),
  30757. LinesToStr([ // $mod.$main
  30758. '']));
  30759. end;
  30760. procedure TTestModule.TestRTTI_IndexModifier;
  30761. begin
  30762. WithTypeInfo:=true;
  30763. StartProgram(false);
  30764. Add([
  30765. 'type',
  30766. ' TEnum = (red, blue);',
  30767. ' TObject = class',
  30768. ' FB: boolean;',
  30769. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  30770. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  30771. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  30772. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  30773. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  30774. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  30775. ' published',
  30776. ' property B1: boolean index 1 read FB write SetIntBool;',
  30777. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  30778. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  30779. ' end;',
  30780. 'begin']);
  30781. ConvertProgram;
  30782. CheckSource('TestRTTI_IndexModifier',
  30783. LinesToStr([ // statements
  30784. 'this.TEnum = {',
  30785. ' "0": "red",',
  30786. ' red: 0,',
  30787. ' "1": "blue",',
  30788. ' blue: 1',
  30789. '};',
  30790. 'this.$rtti.$Enum("TEnum", {',
  30791. ' minvalue: 0,',
  30792. ' maxvalue: 1,',
  30793. ' ordtype: 1,',
  30794. ' enumtype: this.TEnum',
  30795. '});',
  30796. 'rtl.createClass(this, "TObject", null, function () {',
  30797. ' this.$init = function () {',
  30798. ' this.FB = false;',
  30799. ' };',
  30800. ' this.$final = function () {',
  30801. ' };',
  30802. ' var $r = this.$rtti;',
  30803. ' $r.addProperty(',
  30804. ' "B1",',
  30805. ' 18,',
  30806. ' rtl.boolean,',
  30807. ' "FB",',
  30808. ' "SetIntBool",',
  30809. ' {',
  30810. ' index: 1',
  30811. ' }',
  30812. ' );',
  30813. ' $r.addProperty(',
  30814. ' "B2",',
  30815. ' 17,',
  30816. ' rtl.boolean,',
  30817. ' "GetEnumBool",',
  30818. ' "FB",',
  30819. ' {',
  30820. ' index: $mod.TEnum.blue',
  30821. ' }',
  30822. ' );',
  30823. ' $r.addProperty(',
  30824. ' "I1",',
  30825. ' 19,',
  30826. ' rtl.boolean,',
  30827. ' "GetStrIntBool",',
  30828. ' "SetStrIntBool",',
  30829. ' {',
  30830. ' index: 2',
  30831. ' }',
  30832. ' );',
  30833. '});',
  30834. '']),
  30835. LinesToStr([ // $mod.$main
  30836. '']));
  30837. end;
  30838. procedure TTestModule.TestRTTI_StoredModifier;
  30839. begin
  30840. WithTypeInfo:=true;
  30841. StartProgram(false);
  30842. Add([
  30843. 'const',
  30844. ' ConstB = true;',
  30845. 'type',
  30846. ' TObject = class',
  30847. ' private',
  30848. ' FB: boolean;',
  30849. ' function IsBStored: boolean; virtual; abstract;',
  30850. ' published',
  30851. ' property BoolA: boolean read FB stored true;',
  30852. ' property BoolB: boolean read FB stored false;',
  30853. ' property BoolC: boolean read FB stored FB;',
  30854. ' property BoolD: boolean read FB stored ConstB;',
  30855. ' property BoolE: boolean read FB stored IsBStored;',
  30856. ' end;',
  30857. 'begin']);
  30858. ConvertProgram;
  30859. CheckSource('TestRTTI_StoredModifier',
  30860. LinesToStr([ // statements
  30861. 'this.ConstB = true;',
  30862. 'rtl.createClass(this, "TObject", null, function () {',
  30863. ' this.$init = function () {',
  30864. ' this.FB = false;',
  30865. ' };',
  30866. ' this.$final = function () {',
  30867. ' };',
  30868. ' var $r = this.$rtti;',
  30869. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  30870. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  30871. ' $r.addProperty(',
  30872. ' "BoolC",',
  30873. ' 8,',
  30874. ' rtl.boolean,',
  30875. ' "FB",',
  30876. ' "",',
  30877. ' {',
  30878. ' stored: "FB"',
  30879. ' }',
  30880. ' );',
  30881. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  30882. ' $r.addProperty(',
  30883. ' "BoolE",',
  30884. ' 12,',
  30885. ' rtl.boolean,',
  30886. ' "FB",',
  30887. ' "",',
  30888. ' {',
  30889. ' stored: "IsBStored"',
  30890. ' }',
  30891. ' );',
  30892. '});',
  30893. '']),
  30894. LinesToStr([ // $mod.$main
  30895. '']));
  30896. end;
  30897. procedure TTestModule.TestRTTI_DefaultValue;
  30898. begin
  30899. WithTypeInfo:=true;
  30900. StartProgram(false);
  30901. Add([
  30902. 'type',
  30903. ' TEnum = (red, blue);',
  30904. 'const',
  30905. ' CB = true or false;',
  30906. ' CI = 1+2;',
  30907. 'type',
  30908. ' TObject = class',
  30909. ' FB: boolean;',
  30910. ' FI: longint;',
  30911. ' FE: TEnum;',
  30912. ' published',
  30913. ' property B1: boolean read FB default true;',
  30914. ' property B2: boolean read FB default CB;',
  30915. ' property B3: boolean read FB default test1.cb;',
  30916. ' property I1: longint read FI default 2;',
  30917. ' property I2: longint read FI default CI;',
  30918. ' property E1: TEnum read FE default red;',
  30919. ' property E2: TEnum read FE default TEnum.blue;',
  30920. ' end;',
  30921. 'begin']);
  30922. ConvertProgram;
  30923. CheckSource('TestRTTI_DefaultValue',
  30924. LinesToStr([ // statements
  30925. 'this.TEnum = {',
  30926. ' "0": "red",',
  30927. ' red: 0,',
  30928. ' "1": "blue",',
  30929. ' blue: 1',
  30930. '};',
  30931. 'this.$rtti.$Enum("TEnum", {',
  30932. ' minvalue: 0,',
  30933. ' maxvalue: 1,',
  30934. ' ordtype: 1,',
  30935. ' enumtype: this.TEnum',
  30936. '});',
  30937. 'this.CB = true || false;',
  30938. 'this.CI = 1 + 2;',
  30939. 'rtl.createClass(this, "TObject", null, function () {',
  30940. ' this.$init = function () {',
  30941. ' this.FB = false;',
  30942. ' this.FI = 0;',
  30943. ' this.FE = 0;',
  30944. ' };',
  30945. ' this.$final = function () {',
  30946. ' };',
  30947. ' var $r = this.$rtti;',
  30948. ' $r.addProperty(',
  30949. ' "B1",',
  30950. ' 0,',
  30951. ' rtl.boolean,',
  30952. ' "FB",',
  30953. ' "",',
  30954. ' {',
  30955. ' Default: true',
  30956. ' }',
  30957. ' );',
  30958. ' $r.addProperty(',
  30959. ' "B2",',
  30960. ' 0,',
  30961. ' rtl.boolean,',
  30962. ' "FB",',
  30963. ' "",',
  30964. ' {',
  30965. ' Default: true',
  30966. ' }',
  30967. ' );',
  30968. ' $r.addProperty(',
  30969. ' "B3",',
  30970. ' 0,',
  30971. ' rtl.boolean,',
  30972. ' "FB",',
  30973. ' "",',
  30974. ' {',
  30975. ' Default: true',
  30976. ' }',
  30977. ' );',
  30978. ' $r.addProperty(',
  30979. ' "I1",',
  30980. ' 0,',
  30981. ' rtl.longint,',
  30982. ' "FI",',
  30983. ' "",',
  30984. ' {',
  30985. ' Default: 2',
  30986. ' }',
  30987. ' );',
  30988. ' $r.addProperty(',
  30989. ' "I2",',
  30990. ' 0,',
  30991. ' rtl.longint,',
  30992. ' "FI",',
  30993. ' "",',
  30994. ' {',
  30995. ' Default: 3',
  30996. ' }',
  30997. ' );',
  30998. ' $r.addProperty(',
  30999. ' "E1",',
  31000. ' 0,',
  31001. ' $mod.$rtti["TEnum"],',
  31002. ' "FE",',
  31003. ' "",',
  31004. ' {',
  31005. ' Default: $mod.TEnum.red',
  31006. ' }',
  31007. ' );',
  31008. ' $r.addProperty(',
  31009. ' "E2",',
  31010. ' 0,',
  31011. ' $mod.$rtti["TEnum"],',
  31012. ' "FE",',
  31013. ' "",',
  31014. ' {',
  31015. ' Default: $mod.TEnum.blue',
  31016. ' }',
  31017. ' );',
  31018. '});',
  31019. '']),
  31020. LinesToStr([ // $mod.$main
  31021. '']));
  31022. end;
  31023. procedure TTestModule.TestRTTI_DefaultValueSet;
  31024. begin
  31025. WithTypeInfo:=true;
  31026. StartProgram(false);
  31027. Add([
  31028. 'type',
  31029. ' TEnum = (red, blue);',
  31030. ' TSet = set of TEnum;',
  31031. 'const',
  31032. ' CSet = [red,blue];',
  31033. 'type',
  31034. ' TObject = class',
  31035. ' FSet: TSet;',
  31036. ' published',
  31037. ' property Set1: TSet read FSet default [];',
  31038. ' property Set2: TSet read FSet default [red];',
  31039. ' property Set3: TSet read FSet default [red,blue];',
  31040. ' property Set4: TSet read FSet default CSet;',
  31041. ' end;',
  31042. 'begin']);
  31043. ConvertProgram;
  31044. CheckSource('TestRTTI_DefaultValueSet',
  31045. LinesToStr([ // statements
  31046. 'this.TEnum = {',
  31047. ' "0": "red",',
  31048. ' red: 0,',
  31049. ' "1": "blue",',
  31050. ' blue: 1',
  31051. '};',
  31052. 'this.$rtti.$Enum("TEnum", {',
  31053. ' minvalue: 0,',
  31054. ' maxvalue: 1,',
  31055. ' ordtype: 1,',
  31056. ' enumtype: this.TEnum',
  31057. '});',
  31058. 'this.$rtti.$Set("TSet", {',
  31059. ' comptype: this.$rtti["TEnum"]',
  31060. '});',
  31061. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31062. 'rtl.createClass(this, "TObject", null, function () {',
  31063. ' this.$init = function () {',
  31064. ' this.FSet = {};',
  31065. ' };',
  31066. ' this.$final = function () {',
  31067. ' this.FSet = undefined;',
  31068. ' };',
  31069. ' var $r = this.$rtti;',
  31070. ' $r.addProperty(',
  31071. ' "Set1",',
  31072. ' 0,',
  31073. ' $mod.$rtti["TSet"],',
  31074. ' "FSet",',
  31075. ' "",',
  31076. ' {',
  31077. ' Default: {}',
  31078. ' }',
  31079. ' );',
  31080. ' $r.addProperty(',
  31081. ' "Set2",',
  31082. ' 0,',
  31083. ' $mod.$rtti["TSet"],',
  31084. ' "FSet",',
  31085. ' "",',
  31086. ' {',
  31087. ' Default: rtl.createSet($mod.TEnum.red)',
  31088. ' }',
  31089. ' );',
  31090. ' $r.addProperty(',
  31091. ' "Set3",',
  31092. ' 0,',
  31093. ' $mod.$rtti["TSet"],',
  31094. ' "FSet",',
  31095. ' "",',
  31096. ' {',
  31097. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31098. ' }',
  31099. ' );',
  31100. ' $r.addProperty(',
  31101. ' "Set4",',
  31102. ' 0,',
  31103. ' $mod.$rtti["TSet"],',
  31104. ' "FSet",',
  31105. ' "",',
  31106. ' {',
  31107. ' Default: $mod.CSet',
  31108. ' }',
  31109. ' );',
  31110. '});',
  31111. '']),
  31112. LinesToStr([ // $mod.$main
  31113. '']));
  31114. end;
  31115. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31116. begin
  31117. WithTypeInfo:=true;
  31118. StartProgram(false);
  31119. Add([
  31120. 'type',
  31121. ' TRg = -1..1;',
  31122. 'const',
  31123. ' l = low(TRg);',
  31124. ' h = high(TRg);',
  31125. 'type',
  31126. ' TObject = class',
  31127. ' FV: TRg;',
  31128. ' published',
  31129. ' property V1: TRg read FV default -1;',
  31130. ' end;',
  31131. 'begin']);
  31132. ConvertProgram;
  31133. CheckSource('TestRTTI_DefaultValueRangeType',
  31134. LinesToStr([ // statements
  31135. 'this.$rtti.$Int("TRg", {',
  31136. ' minvalue: -1,',
  31137. ' maxvalue: 1,',
  31138. ' ordtype: 0',
  31139. '});',
  31140. 'this.l = -1;',
  31141. 'this.h = 1;',
  31142. 'rtl.createClass(this, "TObject", null, function () {',
  31143. ' this.$init = function () {',
  31144. ' this.FV = 0;',
  31145. ' };',
  31146. ' this.$final = function () {',
  31147. ' };',
  31148. ' var $r = this.$rtti;',
  31149. ' $r.addProperty(',
  31150. ' "V1",',
  31151. ' 0,',
  31152. ' $mod.$rtti["TRg"],',
  31153. ' "FV",',
  31154. ' "",',
  31155. ' {',
  31156. ' Default: -1',
  31157. ' }',
  31158. ' );',
  31159. '});',
  31160. '']),
  31161. LinesToStr([ // $mod.$main
  31162. '']));
  31163. end;
  31164. procedure TTestModule.TestRTTI_DefaultValueInherit;
  31165. begin
  31166. WithTypeInfo:=true;
  31167. StartProgram(false);
  31168. Add([
  31169. 'type',
  31170. ' TObject = class',
  31171. ' FA, FB: byte;',
  31172. ' property A: byte read FA default 1;',
  31173. ' property B: byte read FB default 2;',
  31174. ' end;',
  31175. ' TBird = class',
  31176. ' published',
  31177. ' property A;',
  31178. ' property B nodefault;',
  31179. ' end;',
  31180. 'begin']);
  31181. ConvertProgram;
  31182. CheckSource('TestRTTI_DefaultValueInherit',
  31183. LinesToStr([ // statements
  31184. 'rtl.createClass(this, "TObject", null, function () {',
  31185. ' this.$init = function () {',
  31186. ' this.FA = 0;',
  31187. ' this.FB = 0;',
  31188. ' };',
  31189. ' this.$final = function () {',
  31190. ' };',
  31191. '});',
  31192. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31193. ' var $r = this.$rtti;',
  31194. ' $r.addProperty(',
  31195. ' "A",',
  31196. ' 0,',
  31197. ' rtl.byte,',
  31198. ' "FA",',
  31199. ' "",',
  31200. ' {',
  31201. ' Default: 1',
  31202. ' }',
  31203. ' );',
  31204. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  31205. '});',
  31206. '']),
  31207. LinesToStr([ // $mod.$main
  31208. '']));
  31209. end;
  31210. procedure TTestModule.TestRTTI_OverrideMethod;
  31211. begin
  31212. WithTypeInfo:=true;
  31213. StartProgram(false);
  31214. Add('type');
  31215. Add(' TObject = class');
  31216. Add(' published');
  31217. Add(' procedure DoIt; virtual; abstract;');
  31218. Add(' end;');
  31219. Add(' TSky = class');
  31220. Add(' published');
  31221. Add(' procedure DoIt; override;');
  31222. Add(' end;');
  31223. Add('procedure TSky.DoIt; begin end;');
  31224. Add('begin');
  31225. ConvertProgram;
  31226. CheckSource('TestRTTI_OverrideMethod',
  31227. LinesToStr([ // statements
  31228. 'rtl.createClass(this, "TObject", null, function () {',
  31229. ' this.$init = function () {',
  31230. ' };',
  31231. ' this.$final = function () {',
  31232. ' };',
  31233. ' var $r = this.$rtti;',
  31234. ' $r.addMethod("DoIt", 0, []);',
  31235. '});',
  31236. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31237. ' this.DoIt = function () {',
  31238. ' };',
  31239. '});',
  31240. '']),
  31241. LinesToStr([ // $mod.$main
  31242. '']));
  31243. end;
  31244. procedure TTestModule.TestRTTI_ReintroduceMethod;
  31245. begin
  31246. WithTypeInfo:=true;
  31247. StartProgram(false);
  31248. Add([
  31249. 'type',
  31250. ' TObject = class',
  31251. ' published',
  31252. ' procedure DoIt;',
  31253. ' end;',
  31254. ' TSky = class',
  31255. ' published',
  31256. ' procedure DoIt; reintroduce;',
  31257. ' end;',
  31258. 'procedure TObject.DoIt; begin end;',
  31259. 'procedure TSky.DoIt;',
  31260. 'begin',
  31261. ' inherited DoIt;',
  31262. 'end;',
  31263. 'begin']);
  31264. ConvertProgram;
  31265. CheckSource('TestRTTI_ReintroduceMethod',
  31266. LinesToStr([ // statements
  31267. 'rtl.createClass(this, "TObject", null, function () {',
  31268. ' this.$init = function () {',
  31269. ' };',
  31270. ' this.$final = function () {',
  31271. ' };',
  31272. ' this.DoIt = function () {',
  31273. ' };',
  31274. ' var $r = this.$rtti;',
  31275. ' $r.addMethod("DoIt", 0, []);',
  31276. '});',
  31277. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31278. ' this.DoIt = function () {',
  31279. ' $mod.TObject.DoIt.call(this);',
  31280. ' };',
  31281. ' var $r = this.$rtti;',
  31282. ' $r.addMethod("DoIt", 0, []);',
  31283. '});',
  31284. '']),
  31285. LinesToStr([ // $mod.$main
  31286. '']));
  31287. end;
  31288. procedure TTestModule.TestRTTI_OverloadProperty;
  31289. begin
  31290. WithTypeInfo:=true;
  31291. StartProgram(false);
  31292. Add('type');
  31293. Add(' TObject = class');
  31294. Add(' protected');
  31295. Add(' FFlag: longint;');
  31296. Add(' published');
  31297. Add(' property Flag: longint read fflag;');
  31298. Add(' end;');
  31299. Add(' TSky = class');
  31300. Add(' published');
  31301. Add(' property FLAG: longint write fflag;');
  31302. Add(' end;');
  31303. Add('begin');
  31304. ConvertProgram;
  31305. CheckSource('TestRTTI_OverrideMethod',
  31306. LinesToStr([ // statements
  31307. 'rtl.createClass(this, "TObject", null, function () {',
  31308. ' this.$init = function () {',
  31309. ' this.FFlag = 0;',
  31310. ' };',
  31311. ' this.$final = function () {',
  31312. ' };',
  31313. ' var $r = this.$rtti;',
  31314. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  31315. '});',
  31316. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  31317. ' var $r = this.$rtti;',
  31318. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  31319. '});',
  31320. '']),
  31321. LinesToStr([ // $mod.$main
  31322. '']));
  31323. end;
  31324. procedure TTestModule.TestRTTI_ClassForward;
  31325. begin
  31326. WithTypeInfo:=true;
  31327. StartProgram(false);
  31328. Add('type');
  31329. Add(' TObject = class end;');
  31330. Add(' tbridge = class;');
  31331. Add(' TProc = function: tbridge;');
  31332. Add(' TOger = class');
  31333. Add(' published');
  31334. Add(' FBridge: tbridge;');
  31335. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  31336. Add(' property Bridge: tbridge read fbridge write setbridge;');
  31337. Add(' end;');
  31338. Add(' TBridge = class');
  31339. Add(' FOger: toger;');
  31340. Add(' end;');
  31341. Add('var p: Pointer;');
  31342. Add(' b: tbridge;');
  31343. Add('begin');
  31344. Add(' p:=typeinfo(tbridge);');
  31345. Add(' p:=typeinfo(b);');
  31346. ConvertProgram;
  31347. CheckSource('TestRTTI_ClassForward',
  31348. LinesToStr([ // statements
  31349. 'rtl.createClass(this, "TObject", null, function () {',
  31350. ' this.$init = function () {',
  31351. ' };',
  31352. ' this.$final = function () {',
  31353. ' };',
  31354. '});',
  31355. 'this.$rtti.$Class("TBridge");',
  31356. 'this.$rtti.$ProcVar("TProc", {',
  31357. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  31358. '});',
  31359. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  31360. ' this.$init = function () {',
  31361. ' $mod.TObject.$init.call(this);',
  31362. ' this.FBridge = null;',
  31363. ' };',
  31364. ' this.$final = function () {',
  31365. ' this.FBridge = undefined;',
  31366. ' $mod.TObject.$final.call(this);',
  31367. ' };',
  31368. ' var $r = this.$rtti;',
  31369. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  31370. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  31371. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  31372. '});',
  31373. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  31374. ' this.$init = function () {',
  31375. ' $mod.TObject.$init.call(this);',
  31376. ' this.FOger = null;',
  31377. ' };',
  31378. ' this.$final = function () {',
  31379. ' this.FOger = undefined;',
  31380. ' $mod.TObject.$final.call(this);',
  31381. ' };',
  31382. '});',
  31383. 'this.p = null;',
  31384. 'this.b = null;',
  31385. '']),
  31386. LinesToStr([ // $mod.$main
  31387. '$mod.p = $mod.$rtti["TBridge"];',
  31388. '$mod.p = $mod.b.$rtti;',
  31389. '']));
  31390. end;
  31391. procedure TTestModule.TestRTTI_ClassOf;
  31392. begin
  31393. WithTypeInfo:=true;
  31394. StartProgram(false);
  31395. Add('type');
  31396. Add(' TClass = class of tobject;');
  31397. Add(' TProcA = function: TClass;');
  31398. Add(' TObject = class');
  31399. Add(' published');
  31400. Add(' C: tclass;');
  31401. Add(' end;');
  31402. Add(' tfox = class;');
  31403. Add(' TBird = class end;');
  31404. Add(' TBirds = class of tbird;');
  31405. Add(' TFox = class end;');
  31406. Add(' TFoxes = class of tfox;');
  31407. Add(' TCows = class of TCow;');
  31408. Add(' TCow = class;');
  31409. Add(' TCow = class end;');
  31410. Add('begin');
  31411. ConvertProgram;
  31412. CheckSource('TestRTTI_ClassOf',
  31413. LinesToStr([ // statements
  31414. 'this.$rtti.$Class("TObject");',
  31415. 'this.$rtti.$ClassRef("TClass", {',
  31416. ' instancetype: this.$rtti["TObject"]',
  31417. '});',
  31418. 'this.$rtti.$ProcVar("TProcA", {',
  31419. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  31420. '});',
  31421. 'rtl.createClass(this, "TObject", null, function () {',
  31422. ' this.$init = function () {',
  31423. ' this.C = null;',
  31424. ' };',
  31425. ' this.$final = function () {',
  31426. ' this.C = undefined;',
  31427. ' };',
  31428. ' var $r = this.$rtti;',
  31429. ' $r.addField("C", $mod.$rtti["TClass"]);',
  31430. '});',
  31431. 'this.$rtti.$Class("TFox");',
  31432. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31433. '});',
  31434. 'this.$rtti.$ClassRef("TBirds", {',
  31435. ' instancetype: this.$rtti["TBird"]',
  31436. '});',
  31437. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  31438. '});',
  31439. 'this.$rtti.$ClassRef("TFoxes", {',
  31440. ' instancetype: this.$rtti["TFox"]',
  31441. '});',
  31442. 'this.$rtti.$Class("TCow");',
  31443. 'this.$rtti.$ClassRef("TCows", {',
  31444. ' instancetype: this.$rtti["TCow"]',
  31445. '});',
  31446. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  31447. '});',
  31448. '']),
  31449. LinesToStr([ // $mod.$main
  31450. '']));
  31451. end;
  31452. procedure TTestModule.TestRTTI_Record;
  31453. begin
  31454. WithTypeInfo:=true;
  31455. StartProgram(false);
  31456. Add('type');
  31457. Add(' integer = longint;');
  31458. Add(' TPoint = record');
  31459. Add(' x,y: integer;');
  31460. Add(' end;');
  31461. Add('var p: pointer;');
  31462. Add(' r: tpoint;');
  31463. Add('begin');
  31464. Add(' p:=typeinfo(tpoint);');
  31465. Add(' p:=typeinfo(r);');
  31466. Add(' p:=typeinfo(r.x);');
  31467. ConvertProgram;
  31468. CheckSource('TestRTTI_Record',
  31469. LinesToStr([ // statements
  31470. 'rtl.recNewT(this, "TPoint", function () {',
  31471. ' this.x = 0;',
  31472. ' this.y = 0;',
  31473. ' this.$eq = function (b) {',
  31474. ' return (this.x === b.x) && (this.y === b.y);',
  31475. ' };',
  31476. ' this.$assign = function (s) {',
  31477. ' this.x = s.x;',
  31478. ' this.y = s.y;',
  31479. ' return this;',
  31480. ' };',
  31481. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31482. ' $r.addField("x", rtl.longint);',
  31483. ' $r.addField("y", rtl.longint);',
  31484. '});',
  31485. 'this.p = null;',
  31486. 'this.r = this.TPoint.$new();',
  31487. '']),
  31488. LinesToStr([ // $mod.$main
  31489. '$mod.p = $mod.$rtti["TPoint"];',
  31490. '$mod.p = $mod.$rtti["TPoint"];',
  31491. '$mod.p = rtl.longint;',
  31492. '']));
  31493. end;
  31494. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  31495. begin
  31496. WithTypeInfo:=true;
  31497. StartProgram(false);
  31498. Add('type');
  31499. Add(' TFloatRec = record');
  31500. Add(' c,d: array of char;');
  31501. // Add(' i: array of array of longint;');
  31502. Add(' end;');
  31503. Add('var p: pointer;');
  31504. Add(' r: tfloatrec;');
  31505. Add('begin');
  31506. Add(' p:=typeinfo(tfloatrec);');
  31507. Add(' p:=typeinfo(r);');
  31508. Add(' p:=typeinfo(r.d);');
  31509. ConvertProgram;
  31510. CheckSource('TestRTTI_Record',
  31511. LinesToStr([ // statements
  31512. 'rtl.recNewT(this, "TFloatRec", function () {',
  31513. ' this.$new = function () {',
  31514. ' var r = Object.create(this);',
  31515. ' r.c = [];',
  31516. ' r.d = [];',
  31517. ' return r;',
  31518. ' };',
  31519. ' this.$eq = function (b) {',
  31520. ' return (this.c === b.c) && (this.d === b.d);',
  31521. ' };',
  31522. ' this.$assign = function (s) {',
  31523. ' this.c = rtl.arrayRef(s.c);',
  31524. ' this.d = rtl.arrayRef(s.d);',
  31525. ' return this;',
  31526. ' };',
  31527. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  31528. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  31529. ' eltype: rtl.char',
  31530. ' });',
  31531. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  31532. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  31533. '});',
  31534. 'this.p = null;',
  31535. 'this.r = this.TFloatRec.$new();',
  31536. '']),
  31537. LinesToStr([ // $mod.$main
  31538. '$mod.p = $mod.$rtti["TFloatRec"];',
  31539. '$mod.p = $mod.$rtti["TFloatRec"];',
  31540. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  31541. '']));
  31542. end;
  31543. procedure TTestModule.TestRTTI_Record_ClassVarType;
  31544. begin
  31545. WithTypeInfo:=true;
  31546. StartProgram(false);
  31547. Add([
  31548. '{$modeswitch AdvancedRecords}',
  31549. 'type',
  31550. ' TPoint = record',
  31551. ' type TProc = procedure(w: word);',
  31552. ' class var p: TProc;',
  31553. ' end;',
  31554. 'begin',
  31555. '']);
  31556. ConvertProgram;
  31557. CheckSource('TestRTTI_Record_ClassVarType',
  31558. LinesToStr([ // statements
  31559. 'rtl.recNewT(this, "TPoint", function () {',
  31560. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  31561. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  31562. ' });',
  31563. ' this.p = null;',
  31564. ' this.$eq = function (b) {',
  31565. ' return true;',
  31566. ' };',
  31567. ' this.$assign = function (s) {',
  31568. ' return this;',
  31569. ' };',
  31570. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  31571. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  31572. '}, true);',
  31573. '']),
  31574. LinesToStr([ // $mod.$main
  31575. '']));
  31576. end;
  31577. procedure TTestModule.TestRTTI_LocalTypes;
  31578. begin
  31579. WithTypeInfo:=true;
  31580. StartProgram(false);
  31581. Add([
  31582. 'procedure DoIt;',
  31583. 'type',
  31584. ' integer = longint;',
  31585. ' TPoint = record',
  31586. ' x,y: integer;',
  31587. ' end;',
  31588. 'var p: TPoint;',
  31589. 'begin',
  31590. 'end;',
  31591. 'begin']);
  31592. ConvertProgram;
  31593. CheckSource('TestRTTI_LocalTypes',
  31594. LinesToStr([ // statements
  31595. 'var TPoint = rtl.recNewT(null, "", function () {',
  31596. ' this.x = 0;',
  31597. ' this.y = 0;',
  31598. ' this.$eq = function (b) {',
  31599. ' return (this.x === b.x) && (this.y === b.y);',
  31600. ' };',
  31601. ' this.$assign = function (s) {',
  31602. ' this.x = s.x;',
  31603. ' this.y = s.y;',
  31604. ' return this;',
  31605. ' };',
  31606. '});',
  31607. 'this.DoIt = function () {',
  31608. ' var p = TPoint.$new();',
  31609. '};',
  31610. '']),
  31611. LinesToStr([ // $mod.$main
  31612. '']));
  31613. end;
  31614. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  31615. begin
  31616. WithTypeInfo:=true;
  31617. StartProgram(false);
  31618. Add([
  31619. 'type',
  31620. ' TCaption = string;',
  31621. ' TYesNo = boolean;',
  31622. ' TLetter = char;',
  31623. ' TFloat = double;',
  31624. ' TPtr = pointer;',
  31625. ' TShortInt = shortint;',
  31626. ' TByte = byte;',
  31627. ' TSmallInt = smallint;',
  31628. ' TWord = word;',
  31629. ' TInt32 = longint;',
  31630. ' TDWord = longword;',
  31631. ' TValue = jsvalue;',
  31632. 'var p: TPtr;',
  31633. 'begin',
  31634. ' p:=typeinfo(string);',
  31635. ' p:=typeinfo(tcaption);',
  31636. ' p:=typeinfo(boolean);',
  31637. ' p:=typeinfo(tyesno);',
  31638. ' p:=typeinfo(char);',
  31639. ' p:=typeinfo(tletter);',
  31640. ' p:=typeinfo(double);',
  31641. ' p:=typeinfo(tfloat);',
  31642. ' p:=typeinfo(pointer);',
  31643. ' p:=typeinfo(tptr);',
  31644. ' p:=typeinfo(shortint);',
  31645. ' p:=typeinfo(tshortint);',
  31646. ' p:=typeinfo(byte);',
  31647. ' p:=typeinfo(tbyte);',
  31648. ' p:=typeinfo(smallint);',
  31649. ' p:=typeinfo(tsmallint);',
  31650. ' p:=typeinfo(word);',
  31651. ' p:=typeinfo(tword);',
  31652. ' p:=typeinfo(longword);',
  31653. ' p:=typeinfo(tdword);',
  31654. ' p:=typeinfo(jsvalue);',
  31655. ' p:=typeinfo(tvalue);',
  31656. '']);
  31657. ConvertProgram;
  31658. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  31659. LinesToStr([ // statements
  31660. 'this.p = null;',
  31661. '']),
  31662. LinesToStr([ // $mod.$main
  31663. '$mod.p = rtl.string;',
  31664. '$mod.p = rtl.string;',
  31665. '$mod.p = rtl.boolean;',
  31666. '$mod.p = rtl.boolean;',
  31667. '$mod.p = rtl.char;',
  31668. '$mod.p = rtl.char;',
  31669. '$mod.p = rtl.double;',
  31670. '$mod.p = rtl.double;',
  31671. '$mod.p = rtl.pointer;',
  31672. '$mod.p = rtl.pointer;',
  31673. '$mod.p = rtl.shortint;',
  31674. '$mod.p = rtl.shortint;',
  31675. '$mod.p = rtl.byte;',
  31676. '$mod.p = rtl.byte;',
  31677. '$mod.p = rtl.smallint;',
  31678. '$mod.p = rtl.smallint;',
  31679. '$mod.p = rtl.word;',
  31680. '$mod.p = rtl.word;',
  31681. '$mod.p = rtl.longword;',
  31682. '$mod.p = rtl.longword;',
  31683. '$mod.p = rtl.jsvalue;',
  31684. '$mod.p = rtl.jsvalue;',
  31685. '']));
  31686. end;
  31687. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  31688. begin
  31689. WithTypeInfo:=true;
  31690. StartProgram(false);
  31691. Add([
  31692. 'type',
  31693. ' TCaption = type string;',
  31694. ' TYesNo = type boolean;',
  31695. ' TLetter = type char;',
  31696. ' TFloat = type double;',
  31697. ' TPtr = type pointer;',
  31698. ' TShortInt = type shortint;',
  31699. ' TByte = type byte;',
  31700. ' TSmallInt = type smallint;',
  31701. ' TWord = type word;',
  31702. ' TInt32 = type longint;',
  31703. ' TDWord = type longword;',
  31704. ' TValue = type jsvalue;',
  31705. ' TAliasValue = type TValue;',
  31706. 'var',
  31707. ' p: TPtr;',
  31708. ' a: TAliasValue;',
  31709. 'begin',
  31710. ' p:=typeinfo(tcaption);',
  31711. ' p:=typeinfo(tyesno);',
  31712. ' p:=typeinfo(tletter);',
  31713. ' p:=typeinfo(tfloat);',
  31714. ' p:=typeinfo(tptr);',
  31715. ' p:=typeinfo(tshortint);',
  31716. ' p:=typeinfo(tbyte);',
  31717. ' p:=typeinfo(tsmallint);',
  31718. ' p:=typeinfo(tword);',
  31719. ' p:=typeinfo(tdword);',
  31720. ' p:=typeinfo(tvalue);',
  31721. ' p:=typeinfo(taliasvalue);',
  31722. ' p:=typeinfo(a);',
  31723. '']);
  31724. ConvertProgram;
  31725. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  31726. LinesToStr([ // statements
  31727. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  31728. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  31729. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  31730. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  31731. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  31732. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  31733. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  31734. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  31735. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  31736. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  31737. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  31738. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  31739. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  31740. 'this.p = null;',
  31741. 'this.a = undefined;',
  31742. '']),
  31743. LinesToStr([ // $mod.$main
  31744. '$mod.p = $mod.$rtti["TCaption"];',
  31745. '$mod.p = $mod.$rtti["TYesNo"];',
  31746. '$mod.p = $mod.$rtti["TLetter"];',
  31747. '$mod.p = $mod.$rtti["TFloat"];',
  31748. '$mod.p = $mod.$rtti["TPtr"];',
  31749. '$mod.p = $mod.$rtti["TShortInt"];',
  31750. '$mod.p = $mod.$rtti["TByte"];',
  31751. '$mod.p = $mod.$rtti["TSmallInt"];',
  31752. '$mod.p = $mod.$rtti["TWord"];',
  31753. '$mod.p = $mod.$rtti["TDWord"];',
  31754. '$mod.p = $mod.$rtti["TValue"];',
  31755. '$mod.p = $mod.$rtti["TAliasValue"];',
  31756. '$mod.p = $mod.$rtti["TAliasValue"];',
  31757. '']));
  31758. end;
  31759. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  31760. begin
  31761. WithTypeInfo:=true;
  31762. StartProgram(false);
  31763. Add('procedure DoIt;');
  31764. Add('type');
  31765. Add(' integer = longint;');
  31766. Add(' TPoint = record');
  31767. Add(' x,y: integer;');
  31768. Add(' end;');
  31769. Add('var p: pointer;');
  31770. Add('begin');
  31771. Add(' p:=typeinfo(tpoint);');
  31772. Add('end;');
  31773. Add('begin');
  31774. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  31775. ConvertProgram;
  31776. end;
  31777. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  31778. begin
  31779. WithTypeInfo:=true;
  31780. StartProgram(true,[supTypeInfo]);
  31781. Add([
  31782. '{$modeswitch externalclass}',
  31783. 'type',
  31784. ' TFlag = (up,down);',
  31785. ' TFlags = set of TFlag;',
  31786. 'var',
  31787. ' ti: TTypeInfo;',
  31788. ' tiInt: TTypeInfoInteger;',
  31789. ' tiEnum: TTypeInfoEnum;',
  31790. ' tiSet: TTypeInfoSet;',
  31791. 'begin',
  31792. ' ti:=typeinfo(string);',
  31793. ' ti:=typeinfo(boolean);',
  31794. ' ti:=typeinfo(char);',
  31795. ' ti:=typeinfo(double);',
  31796. ' tiInt:=typeinfo(shortint);',
  31797. ' tiInt:=typeinfo(byte);',
  31798. ' tiInt:=typeinfo(smallint);',
  31799. ' tiInt:=typeinfo(word);',
  31800. ' tiInt:=typeinfo(longint);',
  31801. ' tiInt:=typeinfo(longword);',
  31802. ' ti:=typeinfo(jsvalue);',
  31803. ' tiEnum:=typeinfo(tflag);',
  31804. ' tiSet:=typeinfo(tflags);']);
  31805. ConvertProgram;
  31806. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  31807. LinesToStr([ // statements
  31808. 'this.TFlag = {',
  31809. ' "0": "up",',
  31810. ' up: 0,',
  31811. ' "1": "down",',
  31812. ' down: 1',
  31813. '};',
  31814. 'this.$rtti.$Enum("TFlag", {',
  31815. ' minvalue: 0,',
  31816. ' maxvalue: 1,',
  31817. ' ordtype: 1,',
  31818. ' enumtype: this.TFlag',
  31819. '});',
  31820. 'this.$rtti.$Set("TFlags", {',
  31821. ' comptype: this.$rtti["TFlag"]',
  31822. '});',
  31823. 'this.ti = null;',
  31824. 'this.tiInt = null;',
  31825. 'this.tiEnum = null;',
  31826. 'this.tiSet = null;',
  31827. '']),
  31828. LinesToStr([ // $mod.$main
  31829. '$mod.ti = rtl.string;',
  31830. '$mod.ti = rtl.boolean;',
  31831. '$mod.ti = rtl.char;',
  31832. '$mod.ti = rtl.double;',
  31833. '$mod.tiInt = rtl.shortint;',
  31834. '$mod.tiInt = rtl.byte;',
  31835. '$mod.tiInt = rtl.smallint;',
  31836. '$mod.tiInt = rtl.word;',
  31837. '$mod.tiInt = rtl.longint;',
  31838. '$mod.tiInt = rtl.longword;',
  31839. '$mod.ti = rtl.jsvalue;',
  31840. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  31841. '$mod.tiSet = $mod.$rtti["TFlags"];',
  31842. '']));
  31843. end;
  31844. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  31845. begin
  31846. WithTypeInfo:=true;
  31847. StartProgram(true,[supTypeInfo]);
  31848. Add('{$modeswitch externalclass}');
  31849. Add('type');
  31850. Add(' TStaticArr = array[boolean] of string;');
  31851. Add(' TDynArr = array of string;');
  31852. Add(' TProc = procedure;');
  31853. Add(' TMethod = procedure of object;');
  31854. Add('var');
  31855. Add(' StaticArray: TStaticArr;');
  31856. Add(' tiStaticArray: TTypeInfoStaticArray;');
  31857. Add(' DynArray: TDynArr;');
  31858. Add(' tiDynArray: TTypeInfoDynArray;');
  31859. Add(' ProcVar: TProc;');
  31860. Add(' tiProcVar: TTypeInfoProcVar;');
  31861. Add(' MethodVar: TMethod;');
  31862. Add(' tiMethodVar: TTypeInfoMethodVar;');
  31863. Add('begin');
  31864. Add(' tiStaticArray:=typeinfo(StaticArray);');
  31865. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  31866. Add(' tiDynArray:=typeinfo(DynArray);');
  31867. Add(' tiDynArray:=typeinfo(TDynArr);');
  31868. Add(' tiProcVar:=typeinfo(ProcVar);');
  31869. Add(' tiProcVar:=typeinfo(TProc);');
  31870. Add(' tiMethodVar:=typeinfo(MethodVar);');
  31871. Add(' tiMethodVar:=typeinfo(TMethod);');
  31872. ConvertProgram;
  31873. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  31874. LinesToStr([ // statements
  31875. 'this.$rtti.$StaticArray("TStaticArr", {',
  31876. ' dims: [2],',
  31877. ' eltype: rtl.string',
  31878. '});',
  31879. 'this.$rtti.$DynArray("TDynArr", {',
  31880. ' eltype: rtl.string',
  31881. '});',
  31882. 'this.$rtti.$ProcVar("TProc", {',
  31883. ' procsig: rtl.newTIProcSig([])',
  31884. '});',
  31885. 'this.$rtti.$MethodVar("TMethod", {',
  31886. ' procsig: rtl.newTIProcSig([]),',
  31887. ' methodkind: 0',
  31888. '});',
  31889. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  31890. 'this.tiStaticArray = null;',
  31891. 'this.DynArray = [];',
  31892. 'this.tiDynArray = null;',
  31893. 'this.ProcVar = null;',
  31894. 'this.tiProcVar = null;',
  31895. 'this.MethodVar = null;',
  31896. 'this.tiMethodVar = null;',
  31897. '']),
  31898. LinesToStr([ // $mod.$main
  31899. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  31900. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  31901. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  31902. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  31903. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  31904. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  31905. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  31906. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  31907. '']));
  31908. end;
  31909. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  31910. begin
  31911. WithTypeInfo:=true;
  31912. StartProgram(true,[supTypeInfo]);
  31913. Add('{$modeswitch externalclass}');
  31914. Add('type');
  31915. Add(' TRec = record end;');
  31916. // ToDo: ^TRec
  31917. Add(' TObject = class end;');
  31918. Add(' TClass = class of tobject;');
  31919. Add('var');
  31920. Add(' Rec: trec;');
  31921. Add(' tiRecord: ttypeinforecord;');
  31922. Add(' Obj: tobject;');
  31923. Add(' tiClass: ttypeinfoclass;');
  31924. Add(' aClass: tclass;');
  31925. Add(' tiClassRef: ttypeinfoclassref;');
  31926. // ToDo: ^TRec
  31927. Add(' tiPointer: ttypeinfopointer;');
  31928. Add('begin');
  31929. Add(' tirecord:=typeinfo(trec);');
  31930. Add(' tirecord:=typeinfo(trec);');
  31931. Add(' ticlass:=typeinfo(obj);');
  31932. Add(' ticlass:=typeinfo(tobject);');
  31933. Add(' ticlass:=typeinfo(aclass);');
  31934. Add(' ticlassref:=typeinfo(tclass);');
  31935. ConvertProgram;
  31936. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  31937. LinesToStr([ // statements
  31938. 'rtl.recNewT(this, "TRec", function () {',
  31939. ' this.$eq = function (b) {',
  31940. ' return true;',
  31941. ' };',
  31942. ' this.$assign = function (s) {',
  31943. ' return this;',
  31944. ' };',
  31945. ' $mod.$rtti.$Record("TRec", {});',
  31946. '});',
  31947. 'rtl.createClass(this, "TObject", null, function () {',
  31948. ' this.$init = function () {',
  31949. ' };',
  31950. ' this.$final = function () {',
  31951. ' };',
  31952. '});',
  31953. 'this.$rtti.$ClassRef("TClass", {',
  31954. ' instancetype: this.$rtti["TObject"]',
  31955. '});',
  31956. 'this.Rec = this.TRec.$new();',
  31957. 'this.tiRecord = null;',
  31958. 'this.Obj = null;',
  31959. 'this.tiClass = null;',
  31960. 'this.aClass = null;',
  31961. 'this.tiClassRef = null;',
  31962. 'this.tiPointer = null;',
  31963. '']),
  31964. LinesToStr([ // $mod.$main
  31965. '$mod.tiRecord = $mod.$rtti["TRec"];',
  31966. '$mod.tiRecord = $mod.$rtti["TRec"];',
  31967. '$mod.tiClass = $mod.Obj.$rtti;',
  31968. '$mod.tiClass = $mod.$rtti["TObject"];',
  31969. '$mod.tiClass = $mod.aClass.$rtti;',
  31970. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  31971. '']));
  31972. end;
  31973. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  31974. begin
  31975. WithTypeInfo:=true;
  31976. StartProgram(true,[supTypeInfo]);
  31977. Add([
  31978. '{$modeswitch externalclass}',
  31979. 'type',
  31980. ' TClass = class of tobject;',
  31981. ' TObject = class',
  31982. ' function MyClass: TClass;',
  31983. ' class function ClassType: TClass;',
  31984. ' end;',
  31985. 'function TObject.MyClass: TClass;',
  31986. 'var t: TTypeInfoClass;',
  31987. 'begin',
  31988. ' t:=TypeInfo(Self);',
  31989. ' t:=TypeInfo(Result);',
  31990. ' t:=TypeInfo(TObject);',
  31991. 'end;',
  31992. 'class function TObject.ClassType: TClass;',
  31993. 'var t: TTypeInfoClass;',
  31994. 'begin',
  31995. ' t:=TypeInfo(Self);',
  31996. ' t:=TypeInfo(Result);',
  31997. 'end;',
  31998. 'var',
  31999. ' Obj: TObject;',
  32000. ' t: TTypeInfoClass;',
  32001. 'begin',
  32002. ' t:=TypeInfo(TObject.ClassType);',
  32003. ' t:=TypeInfo(Obj.ClassType);',
  32004. ' t:=TypeInfo(Obj.MyClass);',
  32005. '']);
  32006. ConvertProgram;
  32007. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32008. LinesToStr([ // statements
  32009. 'this.$rtti.$Class("TObject");',
  32010. 'this.$rtti.$ClassRef("TClass", {',
  32011. ' instancetype: this.$rtti["TObject"]',
  32012. '});',
  32013. 'rtl.createClass(this, "TObject", null, function () {',
  32014. ' this.$init = function () {',
  32015. ' };',
  32016. ' this.$final = function () {',
  32017. ' };',
  32018. ' this.MyClass = function () {',
  32019. ' var Result = null;',
  32020. ' var t = null;',
  32021. ' t = this.$rtti;',
  32022. ' t = Result.$rtti;',
  32023. ' t = $mod.$rtti["TObject"];',
  32024. ' return Result;',
  32025. ' };',
  32026. ' this.ClassType = function () {',
  32027. ' var Result = null;',
  32028. ' var t = null;',
  32029. ' t = this.$rtti;',
  32030. ' t = Result.$rtti;',
  32031. ' return Result;',
  32032. ' };',
  32033. '});',
  32034. 'this.Obj = null;',
  32035. 'this.t = null;',
  32036. '']),
  32037. LinesToStr([ // $mod.$main
  32038. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32039. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32040. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32041. '']));
  32042. end;
  32043. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32044. begin
  32045. WithTypeInfo:=true;
  32046. AddModuleWithIntfImplSrc('typinfo.pas',
  32047. LinesToStr([
  32048. '{$modeswitch externalclass}',
  32049. 'type',
  32050. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32051. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32052. '']),
  32053. '');
  32054. AddModuleWithIntfImplSrc('unit2.pas',
  32055. LinesToStr([
  32056. 'uses typinfo;',
  32057. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32058. 'procedure DoPtr(p: PTypeInfo);',
  32059. 'procedure DoInfo(t: TTypeInfo);',
  32060. 'procedure DoInt(t: TTypeInfoInteger);',
  32061. '']),
  32062. LinesToStr([
  32063. 'procedure DoPtr(p: PTypeInfo);',
  32064. 'begin end;',
  32065. 'procedure DoInfo(t: TTypeInfo);',
  32066. 'begin end;',
  32067. 'procedure DoInt(t: TTypeInfoInteger);',
  32068. 'begin end;',
  32069. '']));
  32070. StartUnit(true);
  32071. Add([
  32072. 'interface',
  32073. 'uses unit2;', // does not use unit typinfo
  32074. 'implementation',
  32075. 'var',
  32076. ' i: byte;',
  32077. ' p: pointer;',
  32078. ' t: PTypeInfo;',
  32079. 'initialization',
  32080. ' p:=typeinfo(i);',
  32081. ' t:=typeinfo(i);',
  32082. ' if p=t then ;',
  32083. ' if p=typeinfo(i) then ;',
  32084. ' if typeinfo(i)=p then ;',
  32085. ' if t=typeinfo(i) then ;',
  32086. ' if typeinfo(i)=t then ;',
  32087. ' DoPtr(p);',
  32088. ' DoPtr(t);',
  32089. ' DoPtr(typeinfo(i));',
  32090. ' DoInfo(p);',
  32091. ' DoInfo(t);',
  32092. ' DoInfo(typeinfo(i));',
  32093. ' DoInt(typeinfo(i));',
  32094. '']);
  32095. ConvertUnit;
  32096. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32097. LinesToStr([ // statements
  32098. 'var $impl = $mod.$impl;',
  32099. '']),
  32100. LinesToStr([ // this.$init
  32101. '$impl.p = rtl.byte;',
  32102. '$impl.t = rtl.byte;',
  32103. 'if ($impl.p === $impl.t) ;',
  32104. 'if ($impl.p === rtl.byte) ;',
  32105. 'if (rtl.byte === $impl.p) ;',
  32106. 'if ($impl.t === rtl.byte) ;',
  32107. 'if (rtl.byte === $impl.t) ;',
  32108. 'pas.unit2.DoPtr($impl.p);',
  32109. 'pas.unit2.DoPtr($impl.t);',
  32110. 'pas.unit2.DoPtr(rtl.byte);',
  32111. 'pas.unit2.DoInfo($impl.p);',
  32112. 'pas.unit2.DoInfo($impl.t);',
  32113. 'pas.unit2.DoInfo(rtl.byte);',
  32114. 'pas.unit2.DoInt(rtl.byte);',
  32115. '']),
  32116. LinesToStr([ // implementation
  32117. '$impl.i = 0;',
  32118. '$impl.p = null;',
  32119. '$impl.t = null;',
  32120. '']) );
  32121. end;
  32122. procedure TTestModule.TestRTTI_Interface_Corba;
  32123. begin
  32124. WithTypeInfo:=true;
  32125. StartProgram(true,[supTypeInfo]);
  32126. Add([
  32127. '{$interfaces corba}',
  32128. '{$modeswitch externalclass}',
  32129. 'type',
  32130. ' IUnknown = interface',
  32131. ' end;',
  32132. ' IBird = interface',
  32133. ' function GetItem: longint;',
  32134. ' procedure SetItem(Value: longint);',
  32135. ' property Item: longint read GetItem write SetItem;',
  32136. ' end;',
  32137. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32138. 'var',
  32139. ' i: IBird;',
  32140. ' t: TTypeInfoInterface;',
  32141. 'begin',
  32142. ' t:=TypeInfo(IBird);',
  32143. ' t:=TypeInfo(i);',
  32144. ' DoIt(t);',
  32145. ' DoIt(TypeInfo(IBird));',
  32146. '']);
  32147. ConvertProgram;
  32148. CheckSource('TestRTTI_Interface_Corba',
  32149. LinesToStr([ // statements
  32150. 'rtl.createInterface(',
  32151. ' this,',
  32152. ' "IUnknown",',
  32153. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32154. ' [],',
  32155. ' null,',
  32156. ' function () {',
  32157. ' }',
  32158. ');',
  32159. 'rtl.createInterface(',
  32160. ' this,',
  32161. ' "IBird",',
  32162. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  32163. ' ["GetItem", "SetItem"],',
  32164. ' null,',
  32165. ' function () {',
  32166. ' var $r = this.$rtti;',
  32167. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32168. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32169. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32170. ' }',
  32171. ');',
  32172. 'this.DoIt = function (t) {',
  32173. '}; ',
  32174. 'this.i = null;',
  32175. 'this.t = null;',
  32176. '']),
  32177. LinesToStr([ // $mod.$main
  32178. '$mod.t = $mod.$rtti["IBird"];',
  32179. '$mod.t = $mod.i.$rtti;',
  32180. '$mod.DoIt($mod.t);',
  32181. '$mod.DoIt($mod.$rtti["IBird"]);',
  32182. '']));
  32183. end;
  32184. procedure TTestModule.TestRTTI_Interface_COM;
  32185. begin
  32186. WithTypeInfo:=true;
  32187. StartProgram(true,[supTypeInfo]);
  32188. Add([
  32189. '{$interfaces com}',
  32190. '{$modeswitch externalclass}',
  32191. 'type',
  32192. ' TGuid = record end;',
  32193. ' integer = longint;',
  32194. ' IUnknown = interface',
  32195. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  32196. ' function _AddRef: Integer;',
  32197. ' function _Release: Integer;',
  32198. ' end;',
  32199. ' IBird = interface',
  32200. ' function GetItem: longint;',
  32201. ' procedure SetItem(Value: longint);',
  32202. ' property Item: longint read GetItem write SetItem;',
  32203. ' end;',
  32204. 'var',
  32205. ' i: IBird;',
  32206. ' t: TTypeInfoInterface;',
  32207. 'begin',
  32208. ' t:=TypeInfo(IBird);',
  32209. ' t:=TypeInfo(i);',
  32210. '']);
  32211. ConvertProgram;
  32212. CheckSource('TestRTTI_Interface_COM',
  32213. LinesToStr([ // statements
  32214. 'rtl.recNewT(this, "TGuid", function () {',
  32215. ' this.$eq = function (b) {',
  32216. ' return true;',
  32217. ' };',
  32218. ' this.$assign = function (s) {',
  32219. ' return this;',
  32220. ' };',
  32221. ' $mod.$rtti.$Record("TGuid", {});',
  32222. '});',
  32223. 'rtl.createInterface(',
  32224. ' this,',
  32225. ' "IUnknown",',
  32226. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  32227. ' ["QueryInterface", "_AddRef", "_Release"],',
  32228. ' null,',
  32229. ' function () {',
  32230. ' this.$kind = "com";',
  32231. ' var $r = this.$rtti;',
  32232. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  32233. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  32234. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  32235. ' }',
  32236. ');',
  32237. 'rtl.createInterface(',
  32238. ' this,',
  32239. ' "IBird",',
  32240. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  32241. ' ["GetItem", "SetItem"],',
  32242. ' this.IUnknown,',
  32243. ' function () {',
  32244. ' var $r = this.$rtti;',
  32245. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32246. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  32247. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  32248. ' }',
  32249. ');',
  32250. 'this.i = null;',
  32251. 'this.t = null;',
  32252. '']),
  32253. LinesToStr([ // $mod.$main
  32254. '$mod.t = $mod.$rtti["IBird"];',
  32255. '$mod.t = $mod.i.$rtti;',
  32256. '']));
  32257. end;
  32258. procedure TTestModule.TestRTTI_ClassHelper;
  32259. begin
  32260. WithTypeInfo:=true;
  32261. StartProgram(true,[supTypeInfo]);
  32262. Add([
  32263. '{$interfaces com}',
  32264. '{$modeswitch externalclass}',
  32265. 'type',
  32266. ' TObject = class',
  32267. ' end;',
  32268. ' THelper = class helper for TObject',
  32269. ' published',
  32270. ' function GetItem: longint;',
  32271. ' property Item: longint read GetItem;',
  32272. ' end;',
  32273. 'function THelper.GetItem: longint;',
  32274. 'begin',
  32275. 'end;',
  32276. 'var',
  32277. ' t: TTypeInfoHelper;',
  32278. 'begin',
  32279. ' t:=TypeInfo(THelper);',
  32280. '']);
  32281. ConvertProgram;
  32282. CheckSource('TestRTTI_ClassHelper',
  32283. LinesToStr([ // statements
  32284. 'rtl.createClass(this, "TObject", null, function () {',
  32285. ' this.$init = function () {',
  32286. ' };',
  32287. ' this.$final = function () {',
  32288. ' };',
  32289. '});',
  32290. 'rtl.createHelper(this, "THelper", null, function () {',
  32291. ' this.GetItem = function () {',
  32292. ' var Result = 0;',
  32293. ' return Result;',
  32294. ' };',
  32295. ' var $r = this.$rtti;',
  32296. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  32297. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  32298. '});',
  32299. 'this.t = null;',
  32300. '']),
  32301. LinesToStr([ // $mod.$main
  32302. '$mod.t = $mod.$rtti["THelper"];',
  32303. '']));
  32304. end;
  32305. procedure TTestModule.TestRTTI_ExternalClass;
  32306. begin
  32307. WithTypeInfo:=true;
  32308. StartProgram(true,[supTypeInfo]);
  32309. Add([
  32310. '{$modeswitch externalclass}',
  32311. 'type',
  32312. ' TJSObject = class external name ''Object''',
  32313. ' end;',
  32314. ' TJSArray = class external name ''Array'' (TJSObject)',
  32315. ' end;',
  32316. 'var',
  32317. ' p: Pointer;',
  32318. ' tc: TTypeInfoExtClass;',
  32319. 'begin',
  32320. ' p:=typeinfo(TJSArray);']);
  32321. ConvertProgram;
  32322. CheckSource('TestRTTI_ExternalClass',
  32323. LinesToStr([ // statements
  32324. 'this.$rtti.$ExtClass("TJSObject", {',
  32325. ' jsclass: "Object"',
  32326. '});',
  32327. 'this.$rtti.$ExtClass("TJSArray", {',
  32328. ' ancestor: this.$rtti["TJSObject"],',
  32329. ' jsclass: "Array"',
  32330. '});',
  32331. 'this.p = null;',
  32332. 'this.tc = null;',
  32333. '']),
  32334. LinesToStr([ // $mod.$main
  32335. '$mod.p = $mod.$rtti["TJSArray"];',
  32336. '']));
  32337. end;
  32338. procedure TTestModule.TestRTTI_Unit;
  32339. begin
  32340. WithTypeInfo:=true;
  32341. AddModuleWithIntfImplSrc('unit2.pas',
  32342. LinesToStr([
  32343. '{$mode delphi}',
  32344. 'type',
  32345. ' TWordArray = array of word;',
  32346. ' TArray<T> = array of T;',
  32347. '']),
  32348. '');
  32349. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  32350. Add([
  32351. '{$mode delphi}',
  32352. 'interface',
  32353. 'uses unit2;',
  32354. 'type',
  32355. ' IBird = interface',
  32356. ' function Swoop: TWordArray;',
  32357. ' function Glide: TArray<word>;',
  32358. ' end;',
  32359. 'procedure Fly;',
  32360. 'implementation',
  32361. 'procedure Fly;',
  32362. 'var',
  32363. ' ta: tTypeInfoDynArray;',
  32364. ' ti: tTypeInfoInterface;',
  32365. 'begin',
  32366. ' ta:=typeinfo(TWordArray);',
  32367. ' ta:=typeinfo(TArray<word>);',
  32368. ' ti:=typeinfo(IBird);',
  32369. 'end;',
  32370. '']);
  32371. ConvertUnit;
  32372. CheckSource('TestRTTI_ExternalClass',
  32373. LinesToStr([ // statements
  32374. 'rtl.createInterface(',
  32375. ' this,',
  32376. ' "IBird",',
  32377. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  32378. ' ["Swoop", "Glide"],',
  32379. ' pas.system.IUnknown,',
  32380. ' function () {',
  32381. ' var $r = this.$rtti;',
  32382. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  32383. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  32384. ' }',
  32385. ');',
  32386. 'this.Fly = function () {',
  32387. ' var ta = null;',
  32388. ' var ti = null;',
  32389. ' ta = pas.unit2.$rtti["TWordArray"];',
  32390. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  32391. ' ti = $mod.$rtti["IBird"];',
  32392. '};',
  32393. '']),
  32394. LinesToStr([ // $mod.$main
  32395. '']));
  32396. end;
  32397. procedure TTestModule.TestResourcestringProgram;
  32398. begin
  32399. AddModuleWithIntfImplSrc('unit2.pas',
  32400. LinesToStr([
  32401. 'resourcestring Title = ''Nice'';',
  32402. '']),
  32403. '');
  32404. StartProgram(true);
  32405. Add([
  32406. 'uses unit2;',
  32407. 'const Bar = ''bar'';',
  32408. 'resourcestring',
  32409. ' Red = ''red'';',
  32410. ' Foobar = ''fOo''+bar;',
  32411. 'var s: string;',
  32412. ' c: char;',
  32413. 'begin',
  32414. ' s:=red;',
  32415. ' s:=test1.red;',
  32416. ' s:=Title;',
  32417. ' c:=red[1];',
  32418. ' c:=test1.red[2];',
  32419. ' if red=foobar then ;',
  32420. ' if red[3]=red[4] then ;']);
  32421. ConvertProgram;
  32422. CheckSource('TestResourcestringProgram',
  32423. LinesToStr([ // statements
  32424. 'this.Bar = "bar";',
  32425. 'this.s = "";',
  32426. 'this.c = "";',
  32427. '$mod.$resourcestrings = {',
  32428. ' Red: {',
  32429. ' org: "red"',
  32430. ' },',
  32431. ' Foobar: {',
  32432. ' org: "fOobar"',
  32433. ' }',
  32434. '};',
  32435. '']),
  32436. LinesToStr([ // $mod.$main
  32437. '$mod.s = rtl.getResStr($mod, "Red");',
  32438. '$mod.s = rtl.getResStr($mod, "Red");',
  32439. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32440. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  32441. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  32442. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  32443. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  32444. '']));
  32445. end;
  32446. procedure TTestModule.TestResourcestringUnit;
  32447. begin
  32448. AddModuleWithIntfImplSrc('unit2.pas',
  32449. LinesToStr([
  32450. 'resourcestring Title = ''Nice'';',
  32451. '']),
  32452. '');
  32453. StartUnit(true);
  32454. Add([
  32455. 'interface',
  32456. 'uses unit2;',
  32457. 'const Red = ''rEd'';',
  32458. 'resourcestring',
  32459. ' Blue = ''blue'';',
  32460. ' NotRed = ''not''+Red;',
  32461. 'var s: string;',
  32462. 'implementation',
  32463. 'resourcestring',
  32464. ' ImplGreen = ''green'';',
  32465. 'initialization',
  32466. ' s:=blue+ImplGreen;',
  32467. ' s:=test1.blue+test1.implgreen;',
  32468. ' s:=blue[1]+implgreen[2];',
  32469. ' s:=Title;',
  32470. '']);
  32471. ConvertUnit;
  32472. CheckSource('TestResourcestringUnit',
  32473. LinesToStr([ // statements
  32474. 'this.Red = "rEd";',
  32475. 'this.s = "";',
  32476. '$mod.$resourcestrings = {',
  32477. ' Blue: {',
  32478. ' org: "blue"',
  32479. ' },',
  32480. ' NotRed: {',
  32481. ' org: "notrEd"',
  32482. ' },',
  32483. ' ImplGreen: {',
  32484. ' org: "green"',
  32485. ' }',
  32486. '};',
  32487. '']),
  32488. LinesToStr([ // $mod.$main
  32489. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32490. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  32491. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  32492. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  32493. '']));
  32494. end;
  32495. procedure TTestModule.TestResourcestringImplementation;
  32496. begin
  32497. StartUnit(false);
  32498. Add([
  32499. 'interface',
  32500. 'implementation',
  32501. 'resourcestring',
  32502. ' ImplRed = ''red'';']);
  32503. ConvertUnit;
  32504. CheckSource('TestResourcestringImplementation',
  32505. LinesToStr([ // intf statements
  32506. 'var $impl = $mod.$impl;']),
  32507. LinesToStr([ // $mod.$init
  32508. '']),
  32509. LinesToStr([ // impl statements
  32510. '$mod.$resourcestrings = {',
  32511. ' ImplRed: {',
  32512. ' org: "red"',
  32513. ' }',
  32514. '};',
  32515. '']));
  32516. end;
  32517. procedure TTestModule.TestAttributes_Members;
  32518. begin
  32519. WithTypeInfo:=true;
  32520. StartProgram(false);
  32521. Add([
  32522. '{$modeswitch PrefixedAttributes}',
  32523. 'type',
  32524. ' TObject = class',
  32525. ' constructor Create;',
  32526. ' end;',
  32527. ' TCustomAttribute = class',
  32528. ' constructor Create(Id: word);',
  32529. ' end;',
  32530. ' [Missing]',
  32531. ' TBird = class',
  32532. ' published',
  32533. ' [Tcustom]',
  32534. ' FField: word;',
  32535. ' [tcustom(14)]',
  32536. ' property Size: word read FField;',
  32537. ' [Tcustom(15)]',
  32538. ' procedure Fly; virtual; abstract;',
  32539. ' end;',
  32540. ' TRec = record',
  32541. ' [Tcustom,tcustom(14)]',
  32542. ' Size: word;',
  32543. ' end;',
  32544. 'constructor TObject.Create; begin end;',
  32545. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  32546. 'begin',
  32547. '']);
  32548. ConvertProgram;
  32549. CheckSource('TestAttributes_Members',
  32550. LinesToStr([ // statements
  32551. 'rtl.createClass(this, "TObject", null, function () {',
  32552. ' this.$init = function () {',
  32553. ' };',
  32554. ' this.$final = function () {',
  32555. ' };',
  32556. ' this.Create = function () {',
  32557. ' return this;',
  32558. ' };',
  32559. '});',
  32560. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32561. ' this.Create$1 = function (Id) {',
  32562. ' return this;',
  32563. ' };',
  32564. '});',
  32565. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32566. ' this.$init = function () {',
  32567. ' $mod.TObject.$init.call(this);',
  32568. ' this.FField = 0;',
  32569. ' };',
  32570. ' var $r = this.$rtti;',
  32571. ' $r.addField("FField", rtl.word, {',
  32572. ' attr: [$mod.TCustomAttribute, "Create"]',
  32573. ' });',
  32574. ' $r.addProperty(',
  32575. ' "Size",',
  32576. ' 0,',
  32577. ' rtl.word,',
  32578. ' "FField",',
  32579. ' "",',
  32580. ' {',
  32581. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  32582. ' }',
  32583. ' );',
  32584. ' $r.addMethod("Fly", 0, [], null, 0, {',
  32585. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  32586. ' });',
  32587. '});',
  32588. 'rtl.recNewT(this, "TRec", function () {',
  32589. ' this.Size = 0;',
  32590. ' this.$eq = function (b) {',
  32591. ' return this.Size === b.Size;',
  32592. ' };',
  32593. ' this.$assign = function (s) {',
  32594. ' this.Size = s.Size;',
  32595. ' return this;',
  32596. ' };',
  32597. ' var $r = $mod.$rtti.$Record("TRec", {});',
  32598. ' $r.addField("Size", rtl.word, {',
  32599. ' attr: [',
  32600. ' $mod.TCustomAttribute,',
  32601. ' "Create",',
  32602. ' $mod.TCustomAttribute,',
  32603. ' "Create$1",',
  32604. ' [14]',
  32605. ' ]',
  32606. ' });',
  32607. '});',
  32608. '']),
  32609. LinesToStr([ // $mod.$main
  32610. '']));
  32611. end;
  32612. procedure TTestModule.TestAttributes_Types;
  32613. begin
  32614. WithTypeInfo:=true;
  32615. StartProgram(false);
  32616. Add([
  32617. '{$modeswitch PrefixedAttributes}',
  32618. 'type',
  32619. ' TObject = class',
  32620. ' constructor Create(Id: word);',
  32621. ' end;',
  32622. ' TCustomAttribute = class',
  32623. ' end;',
  32624. ' [TCustom(1)]',
  32625. ' TMyClass = class',
  32626. ' end;',
  32627. ' [TCustom(11)]',
  32628. ' TMyDescendant = class(TMyClass)',
  32629. ' end;',
  32630. ' [TCustom(2)]',
  32631. ' TRec = record',
  32632. ' end;',
  32633. ' [TCustom(3)]',
  32634. ' TInt = type word;',
  32635. 'constructor TObject.Create(Id: word);',
  32636. 'begin',
  32637. 'end;',
  32638. 'var p: pointer;',
  32639. 'begin',
  32640. ' p:=typeinfo(TMyClass);',
  32641. ' p:=typeinfo(TRec);',
  32642. ' p:=typeinfo(TInt);',
  32643. '']);
  32644. ConvertProgram;
  32645. CheckSource('TestAttributes_Types',
  32646. LinesToStr([ // statements
  32647. 'rtl.createClass(this, "TObject", null, function () {',
  32648. ' this.$init = function () {',
  32649. ' };',
  32650. ' this.$final = function () {',
  32651. ' };',
  32652. ' this.Create = function (Id) {',
  32653. ' return this;',
  32654. ' };',
  32655. '});',
  32656. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32657. '});',
  32658. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  32659. ' var $r = this.$rtti;',
  32660. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  32661. '});',
  32662. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  32663. ' var $r = this.$rtti;',
  32664. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  32665. '});',
  32666. 'rtl.recNewT(this, "TRec", function () {',
  32667. ' this.$eq = function (b) {',
  32668. ' return true;',
  32669. ' };',
  32670. ' this.$assign = function (s) {',
  32671. ' return this;',
  32672. ' };',
  32673. ' $mod.$rtti.$Record("TRec", {',
  32674. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  32675. ' });',
  32676. '});',
  32677. 'this.$rtti.$inherited("TInt", rtl.word, {',
  32678. ' attr: [this.TCustomAttribute, "Create", [3]]',
  32679. '});',
  32680. 'this.p = null;',
  32681. '']),
  32682. LinesToStr([ // $mod.$main
  32683. '$mod.p = $mod.$rtti["TMyClass"];',
  32684. '$mod.p = $mod.$rtti["TRec"];',
  32685. '$mod.p = $mod.$rtti["TInt"];',
  32686. '']));
  32687. end;
  32688. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  32689. begin
  32690. WithTypeInfo:=true;
  32691. StartProgram(false);
  32692. Add([
  32693. '{$modeswitch PrefixedAttributes}',
  32694. 'type',
  32695. ' TObject = class',
  32696. ' constructor Create;',
  32697. ' end;',
  32698. ' TCustomAttribute = class',
  32699. ' end;',
  32700. ' THelper = class helper for TCustomAttribute',
  32701. ' constructor Create(Id: word);',
  32702. ' end;',
  32703. ' [TCustom(3)]',
  32704. ' TMyInt = word;',
  32705. 'constructor TObject.Create; begin end;',
  32706. 'constructor THelper.Create(Id: word); begin end;',
  32707. 'begin',
  32708. ' if typeinfo(TMyInt)=nil then ;']);
  32709. ConvertProgram;
  32710. end;
  32711. procedure TTestModule.TestAttributes_InterfacesList;
  32712. begin
  32713. WithTypeInfo:=true;
  32714. StartProgram(false);
  32715. Add([
  32716. '{$mode Delphi}',
  32717. 'type',
  32718. ' TObject = class',
  32719. ' constructor Create;',
  32720. ' end;',
  32721. ' IInterface = interface end;',
  32722. ' TCustomAttribute = class',
  32723. ' end;',
  32724. ' Red = class(TCustomAttribute);',
  32725. ' Blue = class(TCustomAttribute);',
  32726. ' [Red]',
  32727. ' IBird<T> = interface',
  32728. ' procedure Fly;',
  32729. ' end;',
  32730. ' [Blue]',
  32731. ' IEagle = interface(IBird<Word>)',
  32732. ' procedure Dive;',
  32733. ' end;',
  32734. ' TAnt = class(TObject, IEagle)',
  32735. ' procedure Fly; virtual; abstract;',
  32736. ' procedure Dive; virtual; abstract;',
  32737. ' end;',
  32738. 'constructor TObject.Create;',
  32739. 'begin',
  32740. 'end;',
  32741. 'begin',
  32742. '']);
  32743. ConvertProgram;
  32744. CheckSource('TestAttributes_InterfacesList',
  32745. LinesToStr([ // statements
  32746. '$mod.$rtti.$Interface("IBird<System.Word>");',
  32747. 'rtl.createClass(this, "TObject", null, function () {',
  32748. ' this.$init = function () {',
  32749. ' };',
  32750. ' this.$final = function () {',
  32751. ' };',
  32752. ' this.Create = function () {',
  32753. ' return this;',
  32754. ' };',
  32755. '});',
  32756. 'rtl.createInterface(',
  32757. ' this,',
  32758. ' "IInterface",',
  32759. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  32760. ' [],',
  32761. ' null,',
  32762. ' function () {',
  32763. ' this.$kind = "com";',
  32764. ' }',
  32765. ');',
  32766. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  32767. '});',
  32768. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  32769. '});',
  32770. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  32771. '});',
  32772. 'rtl.createInterface(',
  32773. ' this,',
  32774. ' "IBird$G1",',
  32775. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  32776. ' ["Fly"],',
  32777. ' this.IInterface,',
  32778. ' function () {',
  32779. ' var $r = this.$rtti;',
  32780. ' $r.addMethod("Fly", 0, []);',
  32781. ' $r.attr = [$mod.Red, "Create"];',
  32782. ' },',
  32783. ' "IBird<System.Word>"',
  32784. ');',
  32785. 'rtl.createInterface(',
  32786. ' this,',
  32787. ' "IEagle",',
  32788. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  32789. ' ["Dive"],',
  32790. ' this.IBird$G1,',
  32791. ' function () {',
  32792. ' var $r = this.$rtti;',
  32793. ' $r.addMethod("Dive", 0, []);',
  32794. ' $r.attr = [$mod.Blue, "Create"];',
  32795. ' }',
  32796. ');',
  32797. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  32798. ' rtl.addIntf(this, $mod.IEagle);',
  32799. '});',
  32800. '']),
  32801. LinesToStr([ // $mod.$main
  32802. '']));
  32803. end;
  32804. procedure TTestModule.TestAssert;
  32805. begin
  32806. StartProgram(false);
  32807. Add([
  32808. 'procedure DoIt;',
  32809. 'var',
  32810. ' b: boolean;',
  32811. ' s: string;',
  32812. 'begin',
  32813. ' {$Assertions on}',
  32814. ' Assert(b);',
  32815. 'end;',
  32816. 'begin',
  32817. ' DoIt;',
  32818. '']);
  32819. ConvertProgram;
  32820. CheckSource('TestAssert',
  32821. LinesToStr([ // statements
  32822. 'this.DoIt = function () {',
  32823. ' var b = false;',
  32824. ' var s = "";',
  32825. ' if (!b) throw "assert failed";',
  32826. '};',
  32827. '']),
  32828. LinesToStr([ // $mod.$main
  32829. '$mod.DoIt();',
  32830. '']));
  32831. end;
  32832. procedure TTestModule.TestAssert_SysUtils;
  32833. begin
  32834. AddModuleWithIntfImplSrc('SysUtils.pas',
  32835. LinesToStr([
  32836. 'type',
  32837. ' TObject = class',
  32838. ' constructor Create;',
  32839. ' end;',
  32840. ' EAssertionFailed = class',
  32841. ' constructor Create(s: string);',
  32842. ' end;',
  32843. '']),
  32844. LinesToStr([
  32845. 'constructor TObject.Create;',
  32846. 'begin end;',
  32847. 'constructor EAssertionFailed.Create(s: string);',
  32848. 'begin end;',
  32849. '']) );
  32850. StartProgram(true);
  32851. Add([
  32852. 'uses sysutils;',
  32853. 'procedure DoIt;',
  32854. 'var',
  32855. ' b: boolean;',
  32856. ' s: string;',
  32857. 'begin',
  32858. ' {$Assertions on}',
  32859. ' Assert(b);',
  32860. ' Assert(b,''msg'');',
  32861. 'end;',
  32862. 'begin',
  32863. ' DoIt;',
  32864. '']);
  32865. ConvertProgram;
  32866. CheckSource('TestAssert_SysUtils',
  32867. LinesToStr([ // statements
  32868. 'this.DoIt = function () {',
  32869. ' var b = false;',
  32870. ' var s = "";',
  32871. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  32872. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  32873. '};',
  32874. '']),
  32875. LinesToStr([ // $mod.$main
  32876. '$mod.DoIt();',
  32877. '']));
  32878. end;
  32879. procedure TTestModule.TestObjectChecks;
  32880. begin
  32881. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  32882. StartProgram(false);
  32883. Add([
  32884. 'type',
  32885. ' TObject = class',
  32886. ' procedure DoIt;',
  32887. ' end;',
  32888. ' TClass = class of tobject;',
  32889. ' TBird = class',
  32890. ' end;',
  32891. ' TBirdClass = class of TBird;',
  32892. 'var',
  32893. ' o : TObject;',
  32894. ' c: TClass;',
  32895. ' b: TBird;',
  32896. ' bc: TBirdClass;',
  32897. 'procedure TObject.DoIt;',
  32898. 'begin',
  32899. ' b:=TBird(o);',
  32900. 'end;',
  32901. 'begin',
  32902. ' o.DoIt;',
  32903. ' b:=TBird(o);',
  32904. ' bc:=TBirdClass(c);',
  32905. '']);
  32906. ConvertProgram;
  32907. CheckSource('TestCheckMethodCall',
  32908. LinesToStr([ // statements
  32909. 'rtl.createClass(this, "TObject", null, function () {',
  32910. ' this.$init = function () {',
  32911. ' };',
  32912. ' this.$final = function () {',
  32913. ' };',
  32914. ' this.DoIt = function () {',
  32915. ' rtl.checkMethodCall(this,$mod.TObject);',
  32916. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  32917. ' };',
  32918. '});',
  32919. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32920. '});',
  32921. 'this.o = null;',
  32922. 'this.c = null;',
  32923. 'this.b = null;',
  32924. 'this.bc = null;',
  32925. '']),
  32926. LinesToStr([ // $mod.$main
  32927. '$mod.o.DoIt();',
  32928. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  32929. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  32930. '']));
  32931. end;
  32932. procedure TTestModule.TestOverflowChecks_Int;
  32933. begin
  32934. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  32935. StartProgram(false);
  32936. Add([
  32937. 'procedure DoIt;',
  32938. 'var',
  32939. ' b: byte;',
  32940. ' n: nativeint;',
  32941. ' u: nativeuint;',
  32942. ' c: currency;',
  32943. 'begin',
  32944. ' n:=n+n;',
  32945. ' n:=n-n;',
  32946. ' n:=n+b;',
  32947. ' n:=b-n;',
  32948. ' n:=n*n;',
  32949. ' n:=n*u;',
  32950. ' c:=c+b;',
  32951. ' c:=b+c;',
  32952. ' c:=c*b;',
  32953. ' c:=b*c;',
  32954. 'end;',
  32955. 'begin',
  32956. '']);
  32957. ConvertProgram;
  32958. CheckSource('TestOverflowChecks_Int',
  32959. LinesToStr([ // statements
  32960. 'this.DoIt = function () {',
  32961. ' var b = 0;',
  32962. ' var n = 0;',
  32963. ' var u = 0;',
  32964. ' var c = 0;',
  32965. ' n = rtl.oc(n + n);',
  32966. ' n = rtl.oc(n - n);',
  32967. ' n = rtl.oc(n + b);',
  32968. ' n = rtl.oc(b - n);',
  32969. ' n = rtl.oc(n * n);',
  32970. ' n = rtl.oc(n * u);',
  32971. ' c = rtl.oc(c + (b * 10000));',
  32972. ' c = rtl.oc((b * 10000) + c);',
  32973. ' c = rtl.oc(c * b);',
  32974. ' c = rtl.oc(b * c);',
  32975. '};',
  32976. '']),
  32977. LinesToStr([ // $mod.$main
  32978. '']));
  32979. end;
  32980. procedure TTestModule.TestRangeChecks_AssignInt;
  32981. begin
  32982. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32983. StartProgram(false);
  32984. Add([
  32985. '{$R+}',
  32986. 'var',
  32987. ' b: byte = 2;',
  32988. ' w: word = 3;',
  32989. 'procedure DoIt(p: byte);',
  32990. 'begin',
  32991. ' b:=w;',
  32992. ' b+=w;',
  32993. ' b:=1;',
  32994. 'end;',
  32995. '{$R-}',
  32996. 'procedure DoSome;',
  32997. 'begin',
  32998. ' DoIt(w);',
  32999. ' b:=w;',
  33000. ' b:=2;',
  33001. 'end;',
  33002. 'begin',
  33003. '{$R+}',
  33004. '']);
  33005. ConvertProgram;
  33006. CheckSource('TestRangeChecks_AssignInt',
  33007. LinesToStr([ // statements
  33008. 'this.b = 2;',
  33009. 'this.w = 3;',
  33010. 'this.DoIt = function (p) {',
  33011. ' rtl.rc(p, 0, 255);',
  33012. ' $mod.b = rtl.rc($mod.w,0,255);',
  33013. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33014. ' $mod.b = 1;',
  33015. '};',
  33016. 'this.DoSome = function () {',
  33017. ' $mod.DoIt($mod.w);',
  33018. ' $mod.b = $mod.w;',
  33019. ' $mod.b = 2;',
  33020. '};',
  33021. '']),
  33022. LinesToStr([ // $mod.$main
  33023. '']));
  33024. end;
  33025. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33026. begin
  33027. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33028. StartProgram(false);
  33029. Add([
  33030. '{$R+}',
  33031. 'type Ten = 1..10;',
  33032. 'var',
  33033. ' b: Ten = 2;',
  33034. ' w: Ten = 3;',
  33035. 'procedure DoIt(p: Ten);',
  33036. 'begin',
  33037. ' b:=w;',
  33038. ' b+=w;',
  33039. ' b:=1;',
  33040. 'end;',
  33041. '{$R-}',
  33042. 'procedure DoSome;',
  33043. 'begin',
  33044. ' DoIt(w);',
  33045. ' b:=w;',
  33046. ' b:=2;',
  33047. 'end;',
  33048. 'begin',
  33049. '{$R+}',
  33050. '']);
  33051. ConvertProgram;
  33052. CheckSource('TestRangeChecks_AssignIntRange',
  33053. LinesToStr([ // statements
  33054. 'this.b = 2;',
  33055. 'this.w = 3;',
  33056. 'this.DoIt = function (p) {',
  33057. ' rtl.rc(p, 1, 10);',
  33058. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33059. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33060. ' $mod.b = 1;',
  33061. '};',
  33062. 'this.DoSome = function () {',
  33063. ' $mod.DoIt($mod.w);',
  33064. ' $mod.b = $mod.w;',
  33065. ' $mod.b = 2;',
  33066. '};',
  33067. '']),
  33068. LinesToStr([ // $mod.$main
  33069. '']));
  33070. end;
  33071. procedure TTestModule.TestRangeChecks_AssignEnum;
  33072. begin
  33073. StartProgram(false);
  33074. Add([
  33075. '{$R+}',
  33076. 'type TEnum = (red,green);',
  33077. 'var',
  33078. ' e: TEnum = red;',
  33079. 'procedure DoIt(p: TEnum);',
  33080. 'begin',
  33081. ' e:=p;',
  33082. ' p:=TEnum(0);',
  33083. ' p:=succ(e);',
  33084. 'end;',
  33085. '{$R-}',
  33086. 'procedure DoSome;',
  33087. 'begin',
  33088. ' DoIt(e);',
  33089. ' e:=TEnum(1);',
  33090. ' e:=pred(e);',
  33091. 'end;',
  33092. 'begin',
  33093. '{$R+}',
  33094. '']);
  33095. ConvertProgram;
  33096. CheckSource('TestRangeChecks_AssignEnum',
  33097. LinesToStr([ // statements
  33098. 'this.TEnum = {',
  33099. ' "0": "red",',
  33100. ' red: 0,',
  33101. ' "1": "green",',
  33102. ' green: 1',
  33103. '};',
  33104. 'this.e = this.TEnum.red;',
  33105. 'this.DoIt = function (p) {',
  33106. ' rtl.rc(p, 0, 1);',
  33107. ' $mod.e = rtl.rc(p, 0, 1);',
  33108. ' p = 0;',
  33109. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33110. '};',
  33111. 'this.DoSome = function () {',
  33112. ' $mod.DoIt($mod.e);',
  33113. ' $mod.e = 1;',
  33114. ' $mod.e = $mod.e - 1;',
  33115. '};',
  33116. '']),
  33117. LinesToStr([ // $mod.$main
  33118. '']));
  33119. end;
  33120. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33121. begin
  33122. StartProgram(false);
  33123. Add([
  33124. '{$R+}',
  33125. 'type',
  33126. ' TEnum = (red,green);',
  33127. ' TEnumRg = red..green;',
  33128. 'var',
  33129. ' e: TEnumRg = red;',
  33130. 'procedure DoIt(p: TEnumRg);',
  33131. 'begin',
  33132. ' e:=p;',
  33133. ' p:=TEnumRg(0);',
  33134. ' p:=succ(e);',
  33135. 'end;',
  33136. '{$R-}',
  33137. 'procedure DoSome;',
  33138. 'begin',
  33139. ' DoIt(e);',
  33140. ' e:=TEnum(1);',
  33141. ' e:=pred(e);',
  33142. 'end;',
  33143. 'begin',
  33144. '{$R+}',
  33145. '']);
  33146. ConvertProgram;
  33147. CheckSource('TestRangeChecks_AssignEnumRange',
  33148. LinesToStr([ // statements
  33149. 'this.TEnum = {',
  33150. ' "0": "red",',
  33151. ' red: 0,',
  33152. ' "1": "green",',
  33153. ' green: 1',
  33154. '};',
  33155. 'this.e = this.TEnum.red;',
  33156. 'this.DoIt = function (p) {',
  33157. ' rtl.rc(p, 0, 1);',
  33158. ' $mod.e = rtl.rc(p, 0, 1);',
  33159. ' p = 0;',
  33160. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33161. '};',
  33162. 'this.DoSome = function () {',
  33163. ' $mod.DoIt($mod.e);',
  33164. ' $mod.e = 1;',
  33165. ' $mod.e = $mod.e - 1;',
  33166. '};',
  33167. '']),
  33168. LinesToStr([ // $mod.$main
  33169. '']));
  33170. end;
  33171. procedure TTestModule.TestRangeChecks_AssignChar;
  33172. begin
  33173. StartProgram(false);
  33174. Add([
  33175. '{$R+}',
  33176. 'type',
  33177. ' TLetter = char;',
  33178. 'var',
  33179. ' b: TLetter = ''2'';',
  33180. ' w: TLetter = ''3'';',
  33181. 'procedure DoIt(p: TLetter);',
  33182. 'begin',
  33183. ' b:=w;',
  33184. ' b:=''1'';',
  33185. 'end;',
  33186. '{$R-}',
  33187. 'procedure DoSome;',
  33188. 'begin',
  33189. ' DoIt(w);',
  33190. ' b:=w;',
  33191. ' b:=''2'';',
  33192. 'end;',
  33193. 'begin',
  33194. '{$R+}',
  33195. '']);
  33196. ConvertProgram;
  33197. CheckSource('TestRangeChecks_AssignChar',
  33198. LinesToStr([ // statements
  33199. 'this.b = "2";',
  33200. 'this.w = "3";',
  33201. 'this.DoIt = function (p) {',
  33202. ' rtl.rcc(p, 0, 65535);',
  33203. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  33204. ' $mod.b = "1";',
  33205. '};',
  33206. 'this.DoSome = function () {',
  33207. ' $mod.DoIt($mod.w);',
  33208. ' $mod.b = $mod.w;',
  33209. ' $mod.b = "2";',
  33210. '};',
  33211. '']),
  33212. LinesToStr([ // $mod.$main
  33213. '']));
  33214. end;
  33215. procedure TTestModule.TestRangeChecks_AssignCharRange;
  33216. begin
  33217. StartProgram(false);
  33218. Add([
  33219. '{$R+}',
  33220. 'type TDigit = ''0''..''9'';',
  33221. 'var',
  33222. ' b: TDigit = ''2'';',
  33223. ' w: TDigit = ''3'';',
  33224. 'procedure DoIt(p: TDigit);',
  33225. 'begin',
  33226. ' b:=w;',
  33227. ' b:=''1'';',
  33228. 'end;',
  33229. '{$R-}',
  33230. 'procedure DoSome;',
  33231. 'begin',
  33232. ' DoIt(w);',
  33233. ' b:=w;',
  33234. ' b:=''2'';',
  33235. 'end;',
  33236. 'begin',
  33237. '{$R+}',
  33238. '']);
  33239. ConvertProgram;
  33240. CheckSource('TestRangeChecks_AssignCharRange',
  33241. LinesToStr([ // statements
  33242. 'this.b = "2";',
  33243. 'this.w = "3";',
  33244. 'this.DoIt = function (p) {',
  33245. ' rtl.rcc(p, 48, 57);',
  33246. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  33247. ' $mod.b = "1";',
  33248. '};',
  33249. 'this.DoSome = function () {',
  33250. ' $mod.DoIt($mod.w);',
  33251. ' $mod.b = $mod.w;',
  33252. ' $mod.b = "2";',
  33253. '};',
  33254. '']),
  33255. LinesToStr([ // $mod.$main
  33256. '']));
  33257. end;
  33258. procedure TTestModule.TestRangeChecks_ArrayIndex;
  33259. begin
  33260. StartProgram(false);
  33261. Add([
  33262. '{$R+}',
  33263. 'type',
  33264. ' Ten = 1..10;',
  33265. ' TArr = array of Ten;',
  33266. ' TArrArr = array of TArr;',
  33267. ' TArrByte = array[byte] of Ten;',
  33268. ' TArrChar = array[''0''..''9''] of Ten;',
  33269. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  33270. ' TObject = class',
  33271. ' A: TArr;',
  33272. ' end;',
  33273. 'procedure DoIt;',
  33274. 'var',
  33275. ' Arr: TArr;',
  33276. ' ArrArr: TArrArr;',
  33277. ' ArrByte: TArrByte;',
  33278. ' ArrChar: TArrChar;',
  33279. ' ArrByteChar: TArrByteChar;',
  33280. ' i: Ten;',
  33281. ' c: char;',
  33282. ' o: tobject;',
  33283. 'begin',
  33284. ' i:=Arr[1];',
  33285. ' i:=ArrByteChar[1,''2''];',
  33286. ' Arr[1]:=Arr[1];',
  33287. ' Arr[i]:=Arr[i];',
  33288. ' ArrByte[3]:=ArrByte[3];',
  33289. ' ArrByte[i]:=ArrByte[i];',
  33290. ' ArrChar[''5'']:=ArrChar[''5''];',
  33291. ' ArrChar[c]:=ArrChar[c];',
  33292. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  33293. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  33294. ' o.a[i]:=o.a[i];',
  33295. 'end;',
  33296. 'begin',
  33297. '']);
  33298. ConvertProgram;
  33299. CheckSource('TestRangeChecks_ArrayIndex',
  33300. LinesToStr([ // statements
  33301. 'this.TArrByteChar$clone = function (a) {',
  33302. ' var b = [];',
  33303. ' b.length = 256;',
  33304. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  33305. ' return b;',
  33306. '};',
  33307. 'rtl.createClass(this, "TObject", null, function () {',
  33308. ' this.$init = function () {',
  33309. ' this.A = [];',
  33310. ' };',
  33311. ' this.$final = function () {',
  33312. ' this.A = undefined;',
  33313. ' };',
  33314. '});',
  33315. 'this.DoIt = function () {',
  33316. ' var Arr = [];',
  33317. ' var ArrArr = [];',
  33318. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  33319. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  33320. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  33321. ' var i = 0;',
  33322. ' var c = "";',
  33323. ' var o = null;',
  33324. ' i = rtl.rc(Arr[1], 1, 10);',
  33325. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  33326. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  33327. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  33328. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  33329. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  33330. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  33331. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  33332. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  33333. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  33334. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  33335. '};',
  33336. '']),
  33337. LinesToStr([ // $mod.$main
  33338. '']));
  33339. end;
  33340. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  33341. begin
  33342. StartProgram(false);
  33343. Add([
  33344. '{$R+}',
  33345. 'type',
  33346. ' Ten = 1..10;',
  33347. ' TRec = record x: Ten end;',
  33348. ' TArr = array of TRec;',
  33349. ' TArrArr = array of TArr;',
  33350. ' TObject = class',
  33351. ' A: TArr;',
  33352. ' end;',
  33353. 'procedure DoIt;',
  33354. 'var',
  33355. ' Arr: TArr;',
  33356. ' ArrArr: TArrArr;',
  33357. ' i: Ten;',
  33358. ' o: tobject;',
  33359. 'begin',
  33360. ' Arr[1]:=Arr[1];',
  33361. ' Arr[i]:=Arr[i+1];',
  33362. ' o.a[i]:=o.a[i+2];',
  33363. 'end;',
  33364. 'begin',
  33365. '']);
  33366. ConvertProgram;
  33367. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  33368. LinesToStr([ // statements
  33369. 'rtl.recNewT(this, "TRec", function () {',
  33370. ' this.x = 0;',
  33371. ' this.$eq = function (b) {',
  33372. ' return this.x === b.x;',
  33373. ' };',
  33374. ' this.$assign = function (s) {',
  33375. ' this.x = s.x;',
  33376. ' return this;',
  33377. ' };',
  33378. '});',
  33379. 'rtl.createClass(this, "TObject", null, function () {',
  33380. ' this.$init = function () {',
  33381. ' this.A = [];',
  33382. ' };',
  33383. ' this.$final = function () {',
  33384. ' this.A = undefined;',
  33385. ' };',
  33386. '});',
  33387. 'this.DoIt = function () {',
  33388. ' var Arr = [];',
  33389. ' var ArrArr = [];',
  33390. ' var i = 0;',
  33391. ' var o = null;',
  33392. ' Arr[1].$assign(Arr[1]);',
  33393. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  33394. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  33395. '};',
  33396. '']),
  33397. LinesToStr([ // $mod.$main
  33398. '']));
  33399. end;
  33400. procedure TTestModule.TestRangeChecks_StringIndex;
  33401. begin
  33402. StartProgram(false);
  33403. Add([
  33404. 'type',
  33405. ' TObject = class',
  33406. ' S: string;',
  33407. ' end;',
  33408. '{$R+}',
  33409. 'procedure DoIt(var h: string);',
  33410. 'var',
  33411. ' s: string;',
  33412. ' i: longint;',
  33413. ' c: char;',
  33414. ' o: tobject;',
  33415. 'begin',
  33416. ' c:=s[1];',
  33417. ' s[i]:=s[i];',
  33418. ' h[i]:=h[i];',
  33419. ' c:=o.s[i];',
  33420. ' o.s[i]:=c;',
  33421. 'end;',
  33422. 'begin',
  33423. '']);
  33424. ConvertProgram;
  33425. CheckSource('TestRangeChecks_StringIndex',
  33426. LinesToStr([ // statements
  33427. 'rtl.createClass(this, "TObject", null, function () {',
  33428. ' this.$init = function () {',
  33429. ' this.S = "";',
  33430. ' };',
  33431. ' this.$final = function () {',
  33432. ' };',
  33433. '});',
  33434. 'this.DoIt = function (h) {',
  33435. ' var s = "";',
  33436. ' var i = 0;',
  33437. ' var c = "";',
  33438. ' var o = null;',
  33439. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  33440. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  33441. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  33442. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  33443. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  33444. '};',
  33445. '']),
  33446. LinesToStr([ // $mod.$main
  33447. '']));
  33448. end;
  33449. procedure TTestModule.TestRangeChecks_TypecastInt;
  33450. begin
  33451. StartProgram(false);
  33452. Add([
  33453. '{$R+}',
  33454. 'var',
  33455. ' i: nativeint;',
  33456. ' b: byte;',
  33457. ' sh: shortint;',
  33458. ' w: word;',
  33459. ' sm: smallint;',
  33460. ' lw: longword;',
  33461. ' li: longint;',
  33462. 'begin',
  33463. ' b:=12+byte(i);',
  33464. ' sh:=12+shortint(i);',
  33465. ' w:=12+word(i);',
  33466. ' sm:=12+smallint(i);',
  33467. ' lw:=12+longword(i);',
  33468. ' li:=12+longint(i);',
  33469. '']);
  33470. ConvertProgram;
  33471. CheckSource('TestRangeChecks_TypecastInt',
  33472. LinesToStr([
  33473. 'this.i = 0;',
  33474. 'this.b = 0;',
  33475. 'this.sh = 0;',
  33476. 'this.w = 0;',
  33477. 'this.sm = 0;',
  33478. 'this.lw = 0;',
  33479. 'this.li = 0;',
  33480. '']),
  33481. LinesToStr([
  33482. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  33483. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  33484. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  33485. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  33486. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  33487. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  33488. '']));
  33489. end;
  33490. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  33491. begin
  33492. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33493. StartProgram(false);
  33494. Add([
  33495. '{$modeswitch typehelpers}',
  33496. '{$R+}',
  33497. 'type',
  33498. ' TObject = class',
  33499. ' FSize: byte;',
  33500. ' property Size: byte read FSize;',
  33501. ' end;',
  33502. ' THelper = type helper for byte',
  33503. ' procedure SetIt(w: word);',
  33504. ' end;',
  33505. 'procedure THelper.SetIt(w: word);',
  33506. 'begin',
  33507. ' Self:=w;',
  33508. 'end;',
  33509. 'function GetIt: byte;',
  33510. 'begin',
  33511. ' Result.SetIt(2);',
  33512. 'end;',
  33513. 'var',
  33514. ' b: byte = 3;',
  33515. ' o: TObject;',
  33516. 'begin',
  33517. ' b.SetIt(14);',
  33518. ' with b do SetIt(15);',
  33519. ' o.Size.SetIt(16);',
  33520. '']);
  33521. ConvertProgram;
  33522. CheckSource('TestRangeChecks_AssignInt',
  33523. LinesToStr([ // statements
  33524. 'rtl.createClass(this, "TObject", null, function () {',
  33525. ' this.$init = function () {',
  33526. ' this.FSize = 0;',
  33527. ' };',
  33528. ' this.$final = function () {',
  33529. ' };',
  33530. '});',
  33531. 'rtl.createHelper(this, "THelper", null, function () {',
  33532. ' this.SetIt = function (w) {',
  33533. ' rtl.rc(w, 0, 65535);',
  33534. ' this.set(w);',
  33535. ' };',
  33536. '});',
  33537. 'this.GetIt = function () {',
  33538. ' var Result = 0;',
  33539. ' $mod.THelper.SetIt.call({',
  33540. ' get: function () {',
  33541. ' return Result;',
  33542. ' },',
  33543. ' set: function (v) {',
  33544. ' rtl.rc(v, 0, 255);',
  33545. ' Result = v;',
  33546. ' }',
  33547. ' }, 2);',
  33548. ' return Result;',
  33549. '};',
  33550. 'this.b = 3;',
  33551. 'this.o = null;',
  33552. '']),
  33553. LinesToStr([ // $mod.$main
  33554. '$mod.THelper.SetIt.call({',
  33555. ' p: $mod,',
  33556. ' get: function () {',
  33557. ' return this.p.b;',
  33558. ' },',
  33559. ' set: function (v) {',
  33560. ' rtl.rc(v, 0, 255);',
  33561. ' this.p.b = v;',
  33562. ' }',
  33563. '}, 14);',
  33564. 'var $with = $mod.b;',
  33565. '$mod.THelper.SetIt.call({',
  33566. ' get: function () {',
  33567. ' return $with;',
  33568. ' },',
  33569. ' set: function (v) {',
  33570. ' rtl.rc(v, 0, 255);',
  33571. ' $with = v;',
  33572. ' }',
  33573. '}, 15);',
  33574. '$mod.THelper.SetIt.call({',
  33575. ' p: $mod.o,',
  33576. ' get: function () {',
  33577. ' return this.p.FSize;',
  33578. ' },',
  33579. ' set: function (v) {',
  33580. ' rtl.rc(v, 0, 255);',
  33581. ' this.p.FSize = v;',
  33582. ' }',
  33583. '}, 16);',
  33584. '']));
  33585. end;
  33586. procedure TTestModule.TestAsync_Proc;
  33587. begin
  33588. StartProgram(false);
  33589. Add([
  33590. 'procedure Fly(w: word = 1); async; forward;',
  33591. 'procedure Run(w: word = 2); async;',
  33592. 'begin',
  33593. ' Fly(w);',
  33594. ' Fly;',
  33595. ' await(Fly(w));',
  33596. ' await(Fly);',
  33597. 'end;',
  33598. 'procedure Fly(w: word); ',
  33599. 'begin',
  33600. 'end;',
  33601. 'begin',
  33602. ' Run;',
  33603. ' Run(3);',
  33604. '']);
  33605. CheckResolverUnexpectedHints();
  33606. ConvertProgram;
  33607. CheckSource('TestAsync_Proc',
  33608. LinesToStr([ // statements
  33609. 'this.Run = async function (w) {',
  33610. ' $mod.Fly(w);',
  33611. ' $mod.Fly(1);',
  33612. ' await $mod.Fly(w);',
  33613. ' await $mod.Fly(1);',
  33614. '};',
  33615. 'this.Fly = async function (w) {',
  33616. '};',
  33617. '']),
  33618. LinesToStr([
  33619. '$mod.Run(2);',
  33620. '$mod.Run(3);',
  33621. '']));
  33622. end;
  33623. procedure TTestModule.TestAsync_CallResultIsPromise;
  33624. begin
  33625. StartProgram(false);
  33626. Add([
  33627. '{$modeswitch externalclass}',
  33628. 'type',
  33629. ' TObject = class',
  33630. ' end;',
  33631. ' TJSPromise = class external name ''Promise''',
  33632. ' end;',
  33633. ' TBird = class',
  33634. ' function Fly: word; async; ',
  33635. ' end;',
  33636. 'function TBird.Fly: word; async; ',
  33637. 'begin',
  33638. ' Result:=3;',
  33639. ' Fly:=4+Result;',
  33640. ' if Result=5 then ;',
  33641. ' exit(6);',
  33642. 'end;',
  33643. 'function Run: word; async;',
  33644. 'begin',
  33645. ' Result:=11+Result;',
  33646. ' inc(Result);',
  33647. 'end;',
  33648. 'var',
  33649. ' p: TJSPromise;',
  33650. ' o: TBird;',
  33651. 'begin',
  33652. ' p:=Run;',
  33653. ' p:=Run();',
  33654. ' if Run=p then ;',
  33655. ' if p=Run then ;',
  33656. ' if Run()=p then ;',
  33657. ' if p=Run() then ;',
  33658. ' p:=o.Fly;',
  33659. ' p:=o.Fly();',
  33660. ' if o.Fly=p then ;',
  33661. ' if o.Fly()=p then ;',
  33662. ' with o do begin',
  33663. ' p:=Fly;',
  33664. ' p:=Fly();',
  33665. ' if Fly=p then ;',
  33666. ' if Fly()=p then ;',
  33667. ' end;',
  33668. '']);
  33669. CheckResolverUnexpectedHints();
  33670. ConvertProgram;
  33671. CheckSource('TestAsync_CallResultIsPromise',
  33672. LinesToStr([ // statements
  33673. 'rtl.createClass(this, "TObject", null, function () {',
  33674. ' this.$init = function () {',
  33675. ' };',
  33676. ' this.$final = function () {',
  33677. ' };',
  33678. '});',
  33679. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33680. ' this.Fly = async function () {',
  33681. ' var Result = 0;',
  33682. ' Result = 3;',
  33683. ' Result = 4 + Result;',
  33684. ' if (Result === 5) ;',
  33685. ' return 6;',
  33686. ' return Result;',
  33687. ' };',
  33688. '});',
  33689. 'this.Run = async function () {',
  33690. ' var Result = 0;',
  33691. ' Result = 11 + Result;',
  33692. ' Result += 1;',
  33693. ' return Result;',
  33694. '};',
  33695. 'this.p = null;',
  33696. 'this.o = null;',
  33697. '']),
  33698. LinesToStr([
  33699. '$mod.p = $mod.Run();',
  33700. '$mod.p = $mod.Run();',
  33701. 'if ($mod.Run() === $mod.p) ;',
  33702. 'if ($mod.p === $mod.Run()) ;',
  33703. 'if ($mod.Run() === $mod.p) ;',
  33704. 'if ($mod.p === $mod.Run()) ;',
  33705. '$mod.p = $mod.o.Fly();',
  33706. '$mod.p = $mod.o.Fly();',
  33707. 'if ($mod.o.Fly() === $mod.p) ;',
  33708. 'if ($mod.o.Fly() === $mod.p) ;',
  33709. 'var $with = $mod.o;',
  33710. '$mod.p = $with.Fly();',
  33711. '$mod.p = $with.Fly();',
  33712. 'if ($with.Fly() === $mod.p) ;',
  33713. 'if ($with.Fly() === $mod.p) ;',
  33714. '']));
  33715. end;
  33716. procedure TTestModule.TestAsync_ConstructorFail;
  33717. begin
  33718. StartProgram(false);
  33719. Add([
  33720. 'type',
  33721. ' TObject = class',
  33722. ' end;',
  33723. ' TBird = class',
  33724. ' constructor Create; async;',
  33725. ' end;',
  33726. 'constructor TBird.Create; async;',
  33727. 'begin',
  33728. 'end;',
  33729. 'begin',
  33730. '']);
  33731. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  33732. ConvertProgram;
  33733. end;
  33734. procedure TTestModule.TestAsync_PropertyGetterFail;
  33735. begin
  33736. StartProgram(false);
  33737. Add([
  33738. 'type',
  33739. ' TObject = class',
  33740. ' end;',
  33741. ' TBird = class',
  33742. ' function GetSize: word; async;',
  33743. ' property Size: word read GetSize;',
  33744. ' end;',
  33745. 'function TBird.GetSize: word; async;',
  33746. 'begin',
  33747. 'end;',
  33748. 'begin',
  33749. '']);
  33750. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  33751. ConvertProgram;
  33752. end;
  33753. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  33754. begin
  33755. StartProgram(false);
  33756. Add([
  33757. 'procedure Run; async;',
  33758. 'begin',
  33759. ' await(word,1);',
  33760. 'end;',
  33761. 'begin',
  33762. '']);
  33763. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  33764. ConvertProgram;
  33765. end;
  33766. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  33767. begin
  33768. StartProgram(false);
  33769. Add([
  33770. 'type',
  33771. ' TObject = class',
  33772. ' end;',
  33773. ' TBird = class',
  33774. ' end;',
  33775. 'function Fly: TObject; async;',
  33776. 'begin',
  33777. 'end;',
  33778. 'procedure Run; async;',
  33779. 'begin',
  33780. ' await(TBird,Fly);',
  33781. 'end;',
  33782. 'begin',
  33783. '']);
  33784. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  33785. ConvertProgram;
  33786. end;
  33787. procedure TTestModule.TestAWait_OutsideAsyncFail;
  33788. begin
  33789. StartProgram(false);
  33790. Add([
  33791. 'procedure Crawl(w: double); ',
  33792. 'begin',
  33793. 'end;',
  33794. 'procedure Run(w: double);',
  33795. 'begin',
  33796. ' await(Crawl(w));',
  33797. 'end;',
  33798. 'begin',
  33799. ' Run(1);']);
  33800. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  33801. ConvertProgram;
  33802. end;
  33803. procedure TTestModule.TestAWait_IntegerFail;
  33804. begin
  33805. StartProgram(false);
  33806. Add([
  33807. 'function Run: word;',
  33808. 'begin',
  33809. 'end;',
  33810. 'procedure Fly(w: word); async;',
  33811. 'begin',
  33812. ' await(Run());',
  33813. 'end;',
  33814. 'begin',
  33815. ' Fly(1);']);
  33816. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  33817. ConvertProgram;
  33818. end;
  33819. procedure TTestModule.TestAWait_ExternalClassPromise;
  33820. begin
  33821. StartProgram(false);
  33822. Add([
  33823. '{$modeswitch externalclass}',
  33824. 'type',
  33825. ' TJSPromise = class external name ''Promise''',
  33826. ' end;',
  33827. ' TJSThenable = class external name ''Thenable''',
  33828. ' end;',
  33829. 'function Fly(w: word): TJSPromise;',
  33830. 'begin',
  33831. 'end;',
  33832. 'function Jump(w: word): word; async;',
  33833. 'begin',
  33834. 'end;',
  33835. 'function Eat(w: word): TJSPromise; async;',
  33836. 'begin',
  33837. 'end;',
  33838. 'function Run(d: double): word; async;',
  33839. 'var',
  33840. ' p: TJSPromise;',
  33841. 'begin',
  33842. ' Result:=await(word,p);', // promise needs type
  33843. ' Result:=await(word,Fly(3));', // promise needs type
  33844. ' Result:=await(Jump(4));', // async non promise must omit the type
  33845. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  33846. ' Result:=await(word,Eat(6));', // promise needs type
  33847. 'end;',
  33848. 'begin',
  33849. '']);
  33850. ConvertProgram;
  33851. CheckSource('TestAWait_ExternalClassPromise',
  33852. LinesToStr([ // statements
  33853. 'this.Fly = function (w) {',
  33854. ' var Result = null;',
  33855. ' return Result;',
  33856. '};',
  33857. 'this.Jump = async function (w) {',
  33858. ' var Result = 0;',
  33859. ' return Result;',
  33860. '};',
  33861. 'this.Eat = async function (w) {',
  33862. ' var Result = null;',
  33863. ' return Result;',
  33864. '};',
  33865. 'this.Run = async function (d) {',
  33866. ' var Result = 0;',
  33867. ' var p = null;',
  33868. ' Result = await p;',
  33869. ' Result = await $mod.Fly(3);',
  33870. ' Result = await $mod.Jump(4);',
  33871. ' Result = await $mod.Jump(5);',
  33872. ' Result = await $mod.Eat(6);',
  33873. ' return Result;',
  33874. '};',
  33875. '']),
  33876. LinesToStr([
  33877. ]));
  33878. CheckResolverUnexpectedHints();
  33879. end;
  33880. procedure TTestModule.TestAWait_JSValue;
  33881. begin
  33882. StartProgram(false);
  33883. Add([
  33884. '{$modeswitch externalclass}',
  33885. 'type',
  33886. ' TJSPromise = class external name ''Promise''',
  33887. ' end;',
  33888. 'function Fly(w: word): jsvalue; async;',
  33889. 'begin',
  33890. 'end;',
  33891. 'function Run(d: jsvalue; var e): word; async;',
  33892. 'begin',
  33893. ' Result:=await(word,d);', // promise needs type
  33894. ' d:=await(Fly(4));', // async non promise must omit the type
  33895. ' Result:=await(word,e);', // promise needs type
  33896. 'end;',
  33897. 'begin',
  33898. '']);
  33899. ConvertProgram;
  33900. CheckSource('TestAWait_JSValue',
  33901. LinesToStr([ // statements
  33902. 'this.Fly = async function (w) {',
  33903. ' var Result = undefined;',
  33904. ' return Result;',
  33905. '};',
  33906. 'this.Run = async function (d, e) {',
  33907. ' var Result = 0;',
  33908. ' Result = await d;',
  33909. ' d = await $mod.Fly(4);',
  33910. ' Result = await e.get();',
  33911. ' return Result;',
  33912. '};',
  33913. '']),
  33914. LinesToStr([
  33915. ]));
  33916. CheckResolverUnexpectedHints();
  33917. end;
  33918. procedure TTestModule.TestAWait_Result;
  33919. begin
  33920. StartProgram(false);
  33921. Add([
  33922. '{$modeswitch externalclass}',
  33923. 'type',
  33924. ' TJSPromise = class external name ''Promise''',
  33925. ' end;',
  33926. 'function Crawl(d: double = 1.3): TJSPromise; ',
  33927. 'begin',
  33928. 'end;',
  33929. 'function Run(d: double = 1.6): word; async;',
  33930. 'begin',
  33931. ' Result:=await(word,Crawl);',
  33932. ' Result:=await(word,Crawl(4.5));',
  33933. ' Result:=await(Run);',
  33934. ' Result:=await(Run(6.7));',
  33935. 'end;',
  33936. 'begin',
  33937. ' Run(1);']);
  33938. ConvertProgram;
  33939. CheckSource('TestAWait_Result',
  33940. LinesToStr([ // statements
  33941. 'this.Crawl = function (d) {',
  33942. ' var Result = null;',
  33943. ' return Result;',
  33944. '};',
  33945. 'this.Run = async function (d) {',
  33946. ' var Result = 0;',
  33947. ' Result = await $mod.Crawl(1.3);',
  33948. ' Result = await $mod.Crawl(4.5);',
  33949. ' Result = await $mod.Run(1.6);',
  33950. ' Result = await $mod.Run(6.7);',
  33951. ' return Result;',
  33952. '};',
  33953. '']),
  33954. LinesToStr([
  33955. '$mod.Run(1);'
  33956. ]));
  33957. CheckResolverUnexpectedHints();
  33958. end;
  33959. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  33960. begin
  33961. StartProgram(false);
  33962. Add([
  33963. '{$mode objfpc}',
  33964. '{$modeswitch externalclass}',
  33965. 'type',
  33966. ' TJSPromise = class external name ''Promise''',
  33967. ' end;',
  33968. 'function Run: TJSPromise; async;',
  33969. 'begin',
  33970. 'end;',
  33971. 'procedure Fly(w: word); async;',
  33972. 'begin',
  33973. ' await(Run());',
  33974. 'end;',
  33975. 'begin',
  33976. ' Fly(1);']);
  33977. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  33978. nWrongNumberOfParametersForCallTo);
  33979. ConvertProgram;
  33980. end;
  33981. procedure TTestModule.TestAsync_AnonymousProc;
  33982. begin
  33983. StartProgram(false);
  33984. Add([
  33985. '{$mode objfpc}',
  33986. '{$modeswitch externalclass}',
  33987. 'type',
  33988. ' TJSPromise = class external name ''Promise''',
  33989. ' end;',
  33990. 'type',
  33991. ' TFunc = reference to function(x: double): word; async;',
  33992. 'function Crawl(d: double = 1.3): word; async;',
  33993. 'begin',
  33994. 'end;',
  33995. 'var Func: TFunc;',
  33996. 'begin',
  33997. ' Func:=function(c:double):word async begin',
  33998. ' Result:=await(Crawl(c));',
  33999. ' end;',
  34000. ' Func:=function(c:double):word async assembler asm',
  34001. ' end;',
  34002. '']);
  34003. ConvertProgram;
  34004. CheckSource('TestAsync_AnonymousProc',
  34005. LinesToStr([ // statements
  34006. 'this.Crawl = async function (d) {',
  34007. ' var Result = 0;',
  34008. ' return Result;',
  34009. '};',
  34010. 'this.Func = null;',
  34011. '']),
  34012. LinesToStr([
  34013. '$mod.Func = async function (c) {',
  34014. ' var Result = 0;',
  34015. ' Result = await $mod.Crawl(c);',
  34016. ' return Result;',
  34017. '};',
  34018. '$mod.Func = async function (c) {',
  34019. '};',
  34020. '']));
  34021. CheckResolverUnexpectedHints();
  34022. end;
  34023. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34024. begin
  34025. StartProgram(false);
  34026. Add([
  34027. '{$mode objfpc}',
  34028. '{$modeswitch externalclass}',
  34029. 'type',
  34030. ' TJSPromise = class external name ''Promise''',
  34031. ' end;',
  34032. ' TObject = class',
  34033. ' public',
  34034. ' procedure Fly(Prom: TJSPromise);',
  34035. ' end;',
  34036. ' TFunc = reference to procedure(Bird: TObject);',
  34037. 'procedure TObject.Fly(Prom: TJSPromise);',
  34038. 'begin',
  34039. 'end;',
  34040. 'function Crawl: jsvalue; async;',
  34041. 'begin',
  34042. 'end;',
  34043. 'procedure Add(Func: TFunc);',
  34044. 'begin',
  34045. 'end;',
  34046. 'begin',
  34047. ' Add(procedure(Bird: TObject)',
  34048. ' begin',
  34049. ' Bird.Fly(Crawl());',
  34050. ' end);',
  34051. '']);
  34052. ConvertProgram;
  34053. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34054. LinesToStr([ // statements
  34055. 'rtl.createClass(this, "TObject", null, function () {',
  34056. ' this.$init = function () {',
  34057. ' };',
  34058. ' this.$final = function () {',
  34059. ' };',
  34060. ' this.Fly = function (Prom) {',
  34061. ' };',
  34062. '});',
  34063. 'this.Crawl = async function () {',
  34064. ' var Result = undefined;',
  34065. ' return Result;',
  34066. '};',
  34067. 'this.Add = function (Func) {',
  34068. '};',
  34069. '']),
  34070. LinesToStr([
  34071. '$mod.Add(function (Bird) {',
  34072. ' Bird.Fly($mod.Crawl());',
  34073. '});',
  34074. '']));
  34075. end;
  34076. procedure TTestModule.TestAsync_ProcType;
  34077. begin
  34078. StartProgram(false);
  34079. Add([
  34080. '{$mode objfpc}',
  34081. 'type',
  34082. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34083. ' TFunc = function(x: double = 1.1): word; async;',
  34084. ' TProc = procedure(x: longint = 7); async;',
  34085. 'function Crawl(d: double): word; async;',
  34086. 'begin',
  34087. 'end;',
  34088. 'procedure Run(e:longint); async;',
  34089. 'begin',
  34090. 'end;',
  34091. 'procedure Fly(p: TProc); async;',
  34092. 'begin',
  34093. ' await(p);',
  34094. ' await(p());',
  34095. 'end;',
  34096. 'var',
  34097. ' RefFunc: TRefFunc;',
  34098. ' Func: TFunc;',
  34099. ' Proc, ProcB: TProc;',
  34100. 'begin',
  34101. ' Func:=@Crawl;',
  34102. ' RefFunc:=@Crawl;',
  34103. ' RefFunc:=function(c:double):word async begin',
  34104. ' Result:=await(RefFunc);',
  34105. ' Result:=await(RefFunc());',
  34106. ' Result:=await(Func);',
  34107. ' Result:=await(Func());',
  34108. ' await(Proc);',
  34109. ' await(Proc());',
  34110. ' await(Proc(13));',
  34111. ' end;',
  34112. ' Proc:=@Run;',
  34113. ' if Proc=ProcB then ;',
  34114. ' ']);
  34115. ConvertProgram;
  34116. CheckResolverUnexpectedHints();
  34117. CheckSource('TestAsync_ProcType',
  34118. LinesToStr([ // statements
  34119. 'this.Crawl = async function (d) {',
  34120. ' var Result = 0;',
  34121. ' return Result;',
  34122. '};',
  34123. 'this.Run = async function (e) {',
  34124. '};',
  34125. 'this.Fly = async function (p) {',
  34126. ' await p(7);',
  34127. ' await p(7);',
  34128. '};',
  34129. 'this.RefFunc = null;',
  34130. 'this.Func = null;',
  34131. 'this.Proc = null;',
  34132. 'this.ProcB = null;',
  34133. '']),
  34134. LinesToStr([
  34135. '$mod.Func = $mod.Crawl;',
  34136. '$mod.RefFunc = $mod.Crawl;',
  34137. '$mod.RefFunc = async function (c) {',
  34138. ' var Result = 0;',
  34139. ' Result = await $mod.RefFunc(1.3);',
  34140. ' Result = await $mod.RefFunc(1.3);',
  34141. ' Result = await $mod.Func(1.1);',
  34142. ' Result = await $mod.Func(1.1);',
  34143. ' await $mod.Proc(7);',
  34144. ' await $mod.Proc(7);',
  34145. ' await $mod.Proc(13);',
  34146. ' return Result;',
  34147. '};',
  34148. '$mod.Proc = $mod.Run;',
  34149. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  34150. '']));
  34151. end;
  34152. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  34153. begin
  34154. StartProgram(false);
  34155. Add([
  34156. '{$mode objfpc}',
  34157. 'type',
  34158. ' TRefFunc = reference to function(x: double = 1.3): word;',
  34159. 'function Crawl(d: double): word; async;',
  34160. 'begin',
  34161. 'end;',
  34162. 'var',
  34163. ' RefFunc: TRefFunc;',
  34164. 'begin',
  34165. ' RefFunc:=@Crawl;',
  34166. ' ']);
  34167. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34168. ConvertProgram;
  34169. end;
  34170. procedure TTestModule.TestAsync_Inherited;
  34171. begin
  34172. StartProgram(false);
  34173. Add([
  34174. '{$mode objfpc}',
  34175. '{$modeswitch externalclass}',
  34176. 'type',
  34177. ' TJSPromise = class external name ''Promise''',
  34178. ' end;',
  34179. ' TObject = class',
  34180. ' function Run(w: word = 3): word; async; virtual;',
  34181. ' end;',
  34182. ' TBird = class',
  34183. ' function Run(w: word = 3): word; async; override;',
  34184. ' end;',
  34185. 'function TObject.Run(w: word = 3): word; async;',
  34186. 'begin',
  34187. 'end;',
  34188. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  34189. 'var p: TJSPromise;',
  34190. 'begin',
  34191. ' p:=inherited;',
  34192. ' p:=inherited Run;',
  34193. ' p:=inherited Run();',
  34194. ' p:=inherited Run(4);',
  34195. ' exit(p);',
  34196. ' exit(inherited);',
  34197. ' exit(inherited Run);',
  34198. ' exit(inherited Run(5));',
  34199. ' exit(6);',
  34200. 'end;',
  34201. 'begin',
  34202. ' ']);
  34203. ConvertProgram;
  34204. CheckSource('TestAsync_Inherited',
  34205. LinesToStr([ // statements
  34206. 'rtl.createClass(this, "TObject", null, function () {',
  34207. ' this.$init = function () {',
  34208. ' };',
  34209. ' this.$final = function () {',
  34210. ' };',
  34211. ' this.Run = async function (w) {',
  34212. ' var Result = 0;',
  34213. ' return Result;',
  34214. ' };',
  34215. '});',
  34216. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34217. ' this.Run = async function (w) {',
  34218. ' var Result = 0;',
  34219. ' var p = null;',
  34220. ' p = $mod.TObject.Run.apply(this, arguments);',
  34221. ' p = $mod.TObject.Run.call(this, 3);',
  34222. ' p = $mod.TObject.Run.call(this, 3);',
  34223. ' p = $mod.TObject.Run.call(this, 4);',
  34224. ' return p;',
  34225. ' return $mod.TObject.Run.apply(this, arguments);',
  34226. ' return $mod.TObject.Run.call(this, 3);',
  34227. ' return $mod.TObject.Run.call(this, 5);',
  34228. ' return 6;',
  34229. ' return Result;',
  34230. ' };',
  34231. '});',
  34232. '']),
  34233. LinesToStr([
  34234. '']));
  34235. CheckResolverUnexpectedHints();
  34236. end;
  34237. procedure TTestModule.TestAsync_ClassInterface;
  34238. begin
  34239. StartProgram(false);
  34240. Add([
  34241. '{$mode objfpc}',
  34242. '{$modeswitch externalclass}',
  34243. 'type',
  34244. ' TJSPromise = class external name ''Promise''',
  34245. ' end;',
  34246. ' IUnknown = interface',
  34247. ' function _AddRef: longint;',
  34248. ' function _Release: longint;',
  34249. ' end;',
  34250. 'function Say(i: IUnknown): IUnknown; async;',
  34251. 'begin',
  34252. 'end;',
  34253. 'function Run: IUnknown; async;',
  34254. 'begin',
  34255. ' Result:=await(Run);',
  34256. ' Result:=await(Run());',
  34257. ' Result:=await(Run) as IUnknown;',
  34258. ' Result:=await(Say(nil));',
  34259. ' Result:=await(Say(await(Run())));',
  34260. ' Result:=await(Say(await(Run()) as IUnknown));',
  34261. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  34262. 'end;',
  34263. 'procedure Fly;',
  34264. 'var p: TJSPromise;',
  34265. 'begin',
  34266. ' Run;',
  34267. ' Run();',
  34268. ' p:=Run;',
  34269. ' p:=Run();',
  34270. 'end;',
  34271. 'begin',
  34272. ' ']);
  34273. ConvertProgram;
  34274. CheckSource('TestAsync_ClassInterface',
  34275. LinesToStr([ // statements
  34276. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  34277. 'this.Say = async function (i) {',
  34278. ' var Result = null;',
  34279. ' return Result;',
  34280. '};',
  34281. 'this.Run = async function () {',
  34282. ' var Result = null;',
  34283. ' var $ok = false;',
  34284. ' try {',
  34285. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34286. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  34287. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  34288. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  34289. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  34290. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  34291. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  34292. ' $ok = true;',
  34293. ' } finally {',
  34294. ' if (!$ok) rtl._Release(Result);',
  34295. ' };',
  34296. ' return Result;',
  34297. '};',
  34298. 'this.Fly = function () {',
  34299. ' var p = null;',
  34300. ' $mod.Run();',
  34301. ' $mod.Run();',
  34302. ' p = $mod.Run();',
  34303. ' p = $mod.Run();',
  34304. '};',
  34305. '']),
  34306. LinesToStr([
  34307. '']));
  34308. CheckResolverUnexpectedHints();
  34309. end;
  34310. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  34311. begin
  34312. StartProgram(true,[supTInterfacedObject]);
  34313. Add([
  34314. '{$mode objfpc}',
  34315. '{$modeswitch externalclass}',
  34316. 'type',
  34317. ' TJSPromise = class external name ''Promise''',
  34318. ' end;',
  34319. ' IBird = interface',
  34320. ' procedure Run;',
  34321. ' end;',
  34322. ' TBird = class(TInterfacedObject,IBird)',
  34323. ' procedure Run; async;',
  34324. ' end;',
  34325. 'procedure TBird.Run;',
  34326. 'begin',
  34327. 'end;',
  34328. 'begin',
  34329. ' ']);
  34330. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  34331. ConvertProgram;
  34332. end;
  34333. procedure TTestModule.TestAWait_ClassAs;
  34334. begin
  34335. StartProgram(false);
  34336. Add([
  34337. '{$mode objfpc}',
  34338. '{$modeswitch externalclass}',
  34339. 'type',
  34340. ' TJSPromise = class external name ''Promise''',
  34341. ' end;',
  34342. ' TObject = class',
  34343. ' function Run: TObject; async;',
  34344. ' end;',
  34345. ' TBird = class',
  34346. ' function Fly: TBird; async;',
  34347. ' end;',
  34348. 'function TObject.Run: TObject; async;',
  34349. 'begin',
  34350. 'end;',
  34351. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  34352. 'var o: TObject;',
  34353. 'begin',
  34354. ' o:=await(TObject,Run);',
  34355. ' o:=await(TObject,Fly);',
  34356. ' o:=await(TBird,Fly);',
  34357. ' o:=await(TObject,inherited Run);',
  34358. ' o:=await(TObject,inherited Run) as TBird;',
  34359. 'end;',
  34360. 'begin',
  34361. ' ']);
  34362. ConvertProgram;
  34363. CheckSource('TestAWait_ClassAs',
  34364. LinesToStr([ // statements
  34365. 'rtl.createClass(this, "TObject", null, function () {',
  34366. ' this.$init = function () {',
  34367. ' };',
  34368. ' this.$final = function () {',
  34369. ' };',
  34370. ' this.Run = async function () {',
  34371. ' var Result = null;',
  34372. ' return Result;',
  34373. ' };',
  34374. '});',
  34375. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34376. ' this.Fly = async function () {',
  34377. ' var Result = null;',
  34378. ' var o = null;',
  34379. ' o = await this.Run();',
  34380. ' o = await this.Fly();',
  34381. ' o = await this.Fly();',
  34382. ' o = await $mod.TObject.Run.call(this);',
  34383. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  34384. ' return Result;',
  34385. ' };',
  34386. '});',
  34387. '']),
  34388. LinesToStr([
  34389. '']));
  34390. CheckResolverUnexpectedHints();
  34391. end;
  34392. procedure TTestModule.TestLibrary_Empty;
  34393. begin
  34394. StartLibrary(false);
  34395. Add([
  34396. '']);
  34397. ConvertLibrary;
  34398. CheckFullSource('TestLibrary_Empty',
  34399. LinesToStr([ // statements
  34400. 'rtl.module("library", [], function () {',
  34401. ' var $mod = this;',
  34402. ' $mod.$main = function () {',
  34403. ' };',
  34404. '});',
  34405. 'rtl.run("library");',
  34406. '']));
  34407. CheckResolverUnexpectedHints();
  34408. end;
  34409. procedure TTestModule.TestLibrary_ExportFunc;
  34410. begin
  34411. StartLibrary(false);
  34412. Add([
  34413. 'procedure Run(w: word);',
  34414. 'begin',
  34415. 'end;',
  34416. 'exports',
  34417. ' Run;',
  34418. ' run name ''Foo'';',
  34419. ' test1.run name ''Test1Run'';',
  34420. '']);
  34421. ConvertLibrary;
  34422. CheckFullSource('TestLibrary_ExportFunc',
  34423. LinesToStr([ // statements
  34424. 'rtl.module("library", [], function () {',
  34425. ' var $mod = this;',
  34426. ' this.Run = function (w) {',
  34427. ' };',
  34428. ' $mod.$main = function () {',
  34429. ' };',
  34430. '});',
  34431. 'rtl.run("library");',
  34432. 'export const Run = pas.library.Run;',
  34433. 'export const Foo = pas.library.Run;',
  34434. 'export const Test1Run = pas.library.Run;',
  34435. '']));
  34436. CheckResolverUnexpectedHints();
  34437. end;
  34438. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  34439. begin
  34440. StartLibrary(false);
  34441. Add([
  34442. 'procedure Run(w: word); overload;',
  34443. 'begin',
  34444. 'end;',
  34445. 'procedure Run(s: string); overload;',
  34446. 'begin',
  34447. 'end;',
  34448. 'exports',
  34449. ' Run;',
  34450. '']);
  34451. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  34452. nCantDetermineWhichOverloadedFunctionToCall);
  34453. ConvertLibrary;
  34454. end;
  34455. procedure TTestModule.TestLibrary_Export_Index_Fail;
  34456. begin
  34457. StartLibrary(false);
  34458. Add([
  34459. 'procedure Run(w: word);',
  34460. 'begin',
  34461. 'end;',
  34462. 'exports',
  34463. ' Run index 3;',
  34464. '']);
  34465. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  34466. ConvertLibrary;
  34467. end;
  34468. procedure TTestModule.TestLibrary_ExportVar;
  34469. begin
  34470. StartLibrary(false);
  34471. Add([
  34472. 'var Wing: word;',
  34473. 'exports',
  34474. ' Wing, wing name ''BirdArm'';',
  34475. '']);
  34476. ConvertLibrary;
  34477. CheckFullSource('TestLibrary_ExportVar',
  34478. LinesToStr([ // statements
  34479. 'rtl.module("library", [], function () {',
  34480. ' var $mod = this;',
  34481. ' this.Wing = 0;',
  34482. ' $mod.$main = function () {',
  34483. ' };',
  34484. '});',
  34485. 'rtl.run("library");',
  34486. 'export const vars = {};',
  34487. 'Object.defineProperties(vars, {',
  34488. ' Wing: {',
  34489. ' enumerable: true,',
  34490. ' get: function () {',
  34491. ' return pas.library.Wing;',
  34492. ' },',
  34493. ' set: function (v) {',
  34494. ' pas.library.Wing = v;',
  34495. ' }',
  34496. ' },',
  34497. ' BirdArm: {',
  34498. ' enumerable: true,',
  34499. ' get: function () {',
  34500. ' return pas.library.Wing;',
  34501. ' },',
  34502. ' set: function (v) {',
  34503. ' pas.library.Wing = v;',
  34504. ' }',
  34505. ' }',
  34506. '});',
  34507. '']));
  34508. CheckResolverUnexpectedHints();
  34509. end;
  34510. procedure TTestModule.TestLibrary_ExportUnitFunc;
  34511. begin
  34512. AddModuleWithIntfImplSrc('Unit1.pas',
  34513. LinesToStr([
  34514. 'type',
  34515. ' TAnt = class',
  34516. ' class function Crawl: word; static;',
  34517. ' end;',
  34518. 'function Fly: word;',
  34519. '']),
  34520. LinesToStr([
  34521. 'function Fly: word;',
  34522. 'begin',
  34523. 'end;',
  34524. 'class function TAnt.Crawl: word;',
  34525. 'begin',
  34526. 'end;',
  34527. '']));
  34528. StartLibrary(true,[supTObject]);
  34529. Add([
  34530. 'uses unit1;',
  34531. 'exports',
  34532. ' Fly;',
  34533. ' TAnt.Crawl;',
  34534. '']);
  34535. ConvertLibrary;
  34536. CheckFullSource('TestLibrary_ExportUnitFunc',
  34537. LinesToStr([ // statements
  34538. 'rtl.module("library", ["system", "Unit1"], function () {',
  34539. ' var $mod = this;',
  34540. ' $mod.$main = function () {',
  34541. ' };',
  34542. '});',
  34543. 'rtl.run("library");',
  34544. 'export const Fly = pas.Unit1.Fly;',
  34545. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  34546. '']));
  34547. CheckResolverUnexpectedHints();
  34548. end;
  34549. Initialization
  34550. RegisterTests([TTestModule]);
  34551. end.