tcmodules.pas 854 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [coNoTypeInfo];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject
  47. );
  48. TSystemUnitParts = set of TSystemUnitPart;
  49. { TTestHintMessage }
  50. TTestHintMessage = class
  51. public
  52. Id: int64;
  53. MsgType: TMessageType;
  54. MsgNumber: integer;
  55. Msg: string;
  56. SourcePos: TPasSourcePos;
  57. end;
  58. { TTestPasParser }
  59. TTestPasParser = Class(TPasParser)
  60. end;
  61. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  62. { TTestEnginePasResolver }
  63. TTestEnginePasResolver = class(TPas2JsResolver)
  64. private
  65. FFilename: string;
  66. FModule: TPasModule;
  67. FOnFindUnit: TOnFindUnit;
  68. FParser: TTestPasParser;
  69. FStreamResolver: TStreamResolver;
  70. FScanner: TPas2jsPasScanner;
  71. FSource: string;
  72. public
  73. destructor Destroy; override;
  74. function FindUnit(const AName, InFilename: String; NameExpr,
  75. InFileExpr: TPasExpr): TPasModule; override;
  76. procedure UsedInterfacesFinished(Section: TPasSection); override;
  77. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  78. property Filename: string read FFilename write FFilename;
  79. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  80. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  81. property Parser: TTestPasParser read FParser write FParser;
  82. property Source: string read FSource write FSource;
  83. property Module: TPasModule read FModule;
  84. end;
  85. { TCustomTestModule }
  86. TCustomTestModule = Class(TTestCase)
  87. private
  88. FConverter: TPasToJSConverter;
  89. FEngine: TTestEnginePasResolver;
  90. FExpectedErrorClass: ExceptClass;
  91. FExpectedErrorMsg: string;
  92. FExpectedErrorNumber: integer;
  93. FFilename: string;
  94. FFileResolver: TStreamResolver;
  95. FHub: TPas2JSResolverHub;
  96. FJSImplementationSrc: TJSSourceElements;
  97. FJSImplementationUses: TJSArrayLiteral;
  98. FJSInitBody: TJSFunctionBody;
  99. FJSImplentationUses: TJSArrayLiteral;
  100. FJSInterfaceUses: TJSArrayLiteral;
  101. FJSModule: TJSSourceElements;
  102. FJSModuleSrc: TJSSourceElements;
  103. FJSSource: TStringList;
  104. FModule: TPasModule;
  105. FJSModuleCallArgs: TJSArguments;
  106. FModules: TObjectList;// list of TTestEnginePasResolver
  107. FParser: TTestPasParser;
  108. FPasProgram: TPasProgram;
  109. FHintMsgs: TObjectList; // list of TTestHintMessage
  110. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  111. FJSRegModuleCall: TJSCallExpression;
  112. FScanner: TPas2jsPasScanner;
  113. FSkipTests: boolean;
  114. FSource: TStringList;
  115. FFirstPasStatement: TPasImplBlock;
  116. {$IFDEF EnablePasTreeGlobalRefCount}
  117. FElementRefCountAtSetup: int64;
  118. {$ENDIF}
  119. function GetMsgCount: integer;
  120. function GetMsgs(Index: integer): TTestHintMessage;
  121. function GetResolverCount: integer;
  122. function GetResolvers(Index: integer): TTestEnginePasResolver;
  123. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  124. procedure OnParserLog(Sender: TObject; const Msg: String);
  125. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  126. procedure OnScannerLog(Sender: TObject; const Msg: String);
  127. protected
  128. procedure SetUp; override;
  129. function CreateConverter: TPasToJSConverter; virtual;
  130. function LoadUnit(const aUnitName: String): TPasModule;
  131. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  132. procedure TearDown; override;
  133. Procedure Add(Line: string); virtual;
  134. Procedure Add(const Lines: array of string);
  135. Procedure StartParsing; virtual;
  136. procedure ParseModuleQueue; virtual;
  137. procedure ParseModule; virtual;
  138. procedure ParseProgram; virtual;
  139. procedure ParseUnit; virtual;
  140. protected
  141. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  142. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  143. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  144. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  145. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  146. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  147. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  148. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  149. procedure ConvertModule; virtual;
  150. procedure ConvertProgram; virtual;
  151. procedure ConvertUnit; virtual;
  152. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  153. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  154. function GetDottedIdentifier(El: TJSElement): string;
  155. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  156. ImplStatements: string = ''); virtual;
  157. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  158. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  159. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  160. Msg: string; Marker: PSrcMarker = nil); virtual;
  161. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  162. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  163. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  164. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  165. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  166. function IsErrorExpected(E: Exception): boolean;
  167. procedure HandleScannerError(E: EScannerError);
  168. procedure HandleParserError(E: EParserError);
  169. procedure HandlePasResolveError(E: EPasResolve);
  170. procedure HandlePas2JSError(E: EPas2JS);
  171. procedure HandleException(E: Exception);
  172. procedure FailException(E: Exception);
  173. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  174. function IndexOfResolver(const Filename: string): integer;
  175. function GetResolver(const Filename: string): TTestEnginePasResolver;
  176. function GetDefaultNamespace: string;
  177. property PasProgram: TPasProgram Read FPasProgram;
  178. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  179. property ResolverCount: integer read GetResolverCount;
  180. property Engine: TTestEnginePasResolver read FEngine;
  181. property Filename: string read FFilename;
  182. Property Module: TPasModule Read FModule;
  183. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  184. property Converter: TPasToJSConverter read FConverter;
  185. property JSSource: TStringList read FJSSource;
  186. property JSModule: TJSSourceElements read FJSModule;
  187. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  188. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  189. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  190. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  191. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  192. property JSInitBody: TJSFunctionBody read FJSInitBody;
  193. property JSImplementationSrc: TJSSourceElements read FJSImplementationSrc;
  194. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  195. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  196. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  197. property SkipTests: boolean read FSkipTests write FSkipTests;
  198. public
  199. constructor Create; override;
  200. destructor Destroy; override;
  201. property Hub: TPas2JSResolverHub read FHub;
  202. property Source: TStringList read FSource;
  203. property FileResolver: TStreamResolver read FFileResolver;
  204. property Scanner: TPas2jsPasScanner read FScanner;
  205. property Parser: TTestPasParser read FParser;
  206. property MsgCount: integer read GetMsgCount;
  207. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  208. end;
  209. { TTestModule }
  210. TTestModule = class(TCustomTestModule)
  211. Published
  212. Procedure TestReservedWords;
  213. // program/units
  214. Procedure TestEmptyProgram;
  215. Procedure TestEmptyProgramUseStrict;
  216. Procedure TestEmptyUnit;
  217. Procedure TestEmptyUnitUseStrict;
  218. Procedure TestDottedUnitNames;
  219. Procedure TestDottedUnitNameImpl;
  220. Procedure TestDottedUnitExpr;
  221. Procedure Test_ModeFPCFail;
  222. Procedure Test_ModeSwitchCBlocksFail;
  223. Procedure TestUnit_UseSystem;
  224. Procedure TestUnit_Intf1Impl2Intf1;
  225. Procedure TestIncludeVersion;
  226. // vars/const
  227. Procedure TestVarInt;
  228. Procedure TestVarBaseTypes;
  229. Procedure TestBaseTypeSingleFail;
  230. Procedure TestBaseTypeExtendedFail;
  231. Procedure TestConstBaseTypes;
  232. Procedure TestUnitImplVars;
  233. Procedure TestUnitImplConsts;
  234. Procedure TestUnitImplRecord;
  235. Procedure TestRenameJSNameConflict;
  236. Procedure TestLocalConst;
  237. Procedure TestVarExternal;
  238. Procedure TestVarExternalOtherUnit;
  239. Procedure TestVarAbsoluteFail;
  240. Procedure TestConstExternal;
  241. // numbers
  242. Procedure TestDouble;
  243. Procedure TestInteger;
  244. Procedure TestIntegerRange;
  245. Procedure TestIntegerTypecasts;
  246. Procedure TestInteger_BitwiseShrNativeInt;
  247. Procedure TestInteger_BitwiseShlNativeInt;
  248. Procedure TestInteger_SystemFunc;
  249. Procedure TestCurrency;
  250. Procedure TestForBoolDo;
  251. Procedure TestForIntDo;
  252. Procedure TestForIntInDo;
  253. // strings
  254. Procedure TestCharConst;
  255. Procedure TestChar_Compare;
  256. Procedure TestChar_BuiltInProcs;
  257. Procedure TestStringConst;
  258. Procedure TestStringConstSurrogate;
  259. Procedure TestString_Length;
  260. Procedure TestString_Compare;
  261. Procedure TestString_SetLength;
  262. Procedure TestString_CharAt;
  263. Procedure TestStringHMinusFail;
  264. Procedure TestStr;
  265. Procedure TestBaseType_AnsiStringFail;
  266. Procedure TestBaseType_WideStringFail;
  267. Procedure TestBaseType_ShortStringFail;
  268. Procedure TestBaseType_RawByteStringFail;
  269. Procedure TestTypeShortstring_Fail;
  270. Procedure TestCharSet_Custom;
  271. Procedure TestForCharDo;
  272. Procedure TestForCharInDo;
  273. // alias types
  274. Procedure TestAliasTypeRef;
  275. Procedure TestTypeCast_BaseTypes;
  276. Procedure TestTypeCast_AliasBaseTypes;
  277. // functions
  278. Procedure TestEmptyProc;
  279. Procedure TestProcOneParam;
  280. Procedure TestFunctionWithoutParams;
  281. Procedure TestProcedureWithoutParams;
  282. Procedure TestPrgProcVar;
  283. Procedure TestProcTwoArgs;
  284. Procedure TestProc_DefaultValue;
  285. Procedure TestUnitProcVar;
  286. Procedure TestImplProc;
  287. Procedure TestFunctionResult;
  288. Procedure TestNestedProc;
  289. Procedure TestNestedProc_ResultString;
  290. Procedure TestForwardProc;
  291. Procedure TestNestedForwardProc;
  292. Procedure TestAssignFunctionResult;
  293. Procedure TestFunctionResultInCondition;
  294. Procedure TestFunctionResultInForLoop;
  295. Procedure TestFunctionResultInTypeCast;
  296. Procedure TestExit;
  297. Procedure TestExit_ResultInFinally;
  298. Procedure TestBreak;
  299. Procedure TestBreakAsVar;
  300. Procedure TestContinue;
  301. Procedure TestProc_External;
  302. Procedure TestProc_ExternalOtherUnit;
  303. Procedure TestProc_Asm;
  304. Procedure TestProc_Assembler;
  305. Procedure TestProc_VarParam;
  306. Procedure TestProc_VarParamString;
  307. Procedure TestProc_VarParamV;
  308. Procedure TestProc_Overload;
  309. Procedure TestProc_OverloadForward;
  310. Procedure TestProc_OverloadIntfImpl;
  311. Procedure TestProc_OverloadNested;
  312. Procedure TestProc_OverloadNestedForward;
  313. Procedure TestProc_OverloadUnitCycle;
  314. Procedure TestProc_Varargs;
  315. Procedure TestProc_ConstOrder;
  316. Procedure TestProc_DuplicateConst;
  317. Procedure TestProc_LocalVarAbsolute;
  318. Procedure TestProc_LocalVarInit;
  319. Procedure TestProc_ReservedWords;
  320. Procedure TestProc_ConstRefWord;
  321. // anonymous functions
  322. Procedure TestAnonymousProc_Assign_ObjFPC;
  323. Procedure TestAnonymousProc_Assign_Delphi;
  324. Procedure TestAnonymousProc_Arg;
  325. Procedure TestAnonymousProc_Typecast;
  326. Procedure TestAnonymousProc_With;
  327. Procedure TestAnonymousProc_ExceptOn;
  328. Procedure TestAnonymousProc_Nested;
  329. Procedure TestAnonymousProc_NestedAssignResult;
  330. Procedure TestAnonymousProc_Class;
  331. Procedure TestAnonymousProc_ForLoop;
  332. // enums, sets
  333. Procedure TestEnum_Name;
  334. Procedure TestEnum_Number;
  335. Procedure TestEnum_ConstFail;
  336. Procedure TestEnum_Functions;
  337. Procedure TestEnum_AsParams;
  338. Procedure TestEnumRange_Array;
  339. Procedure TestEnum_ForIn;
  340. Procedure TestEnum_ScopedNumber;
  341. Procedure TestEnum_InFunction;
  342. Procedure TestSet_Enum;
  343. Procedure TestSet_Operators;
  344. Procedure TestSet_Operator_In;
  345. Procedure TestSet_Functions;
  346. Procedure TestSet_PassAsArgClone;
  347. Procedure TestSet_AsParams;
  348. Procedure TestSet_Property;
  349. Procedure TestSet_EnumConst;
  350. Procedure TestSet_IntConst;
  351. Procedure TestSet_AnonymousEnumType;
  352. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  353. Procedure TestSet_ConstEnum;
  354. Procedure TestSet_ConstChar;
  355. Procedure TestSet_ConstInt;
  356. Procedure TestSet_InFunction;
  357. Procedure TestSet_ForIn;
  358. // statements
  359. Procedure TestNestBegin;
  360. Procedure TestIncDec;
  361. Procedure TestLoHiFpcMode;
  362. Procedure TestLoHiDelphiMode;
  363. Procedure TestAssignments;
  364. Procedure TestArithmeticOperators1;
  365. Procedure TestLogicalOperators;
  366. Procedure TestBitwiseOperators;
  367. Procedure TestBitwiseOperatorsLongword;
  368. Procedure TestFunctionInt;
  369. Procedure TestFunctionString;
  370. Procedure TestIfThen;
  371. Procedure TestForLoop;
  372. Procedure TestForLoopInsideFunction;
  373. Procedure TestForLoop_ReadVarAfter;
  374. Procedure TestForLoop_Nested;
  375. Procedure TestRepeatUntil;
  376. Procedure TestAsmBlock;
  377. Procedure TestAsmPas_Impl; // ToDo
  378. Procedure TestTryFinally;
  379. Procedure TestTryExcept;
  380. Procedure TestTryExcept_ReservedWords;
  381. Procedure TestIfThenRaiseElse;
  382. Procedure TestCaseOf;
  383. Procedure TestCaseOf_UseSwitch;
  384. Procedure TestCaseOfNoElse;
  385. Procedure TestCaseOfNoElse_UseSwitch;
  386. Procedure TestCaseOfRange;
  387. Procedure TestCaseOfString;
  388. Procedure TestCaseOfChar;
  389. Procedure TestCaseOfExternalClassConst;
  390. Procedure TestDebugger;
  391. // arrays
  392. Procedure TestArray_Dynamic;
  393. Procedure TestArray_Dynamic_Nil;
  394. Procedure TestArray_DynMultiDimensional;
  395. Procedure TestArray_DynamicAssign;
  396. Procedure TestArray_StaticInt;
  397. Procedure TestArray_StaticBool;
  398. Procedure TestArray_StaticChar;
  399. Procedure TestArray_StaticMultiDim;
  400. Procedure TestArray_StaticInFunction;
  401. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  402. Procedure TestArrayOfRecord;
  403. Procedure TestArray_StaticRecord;
  404. Procedure TestArrayOfSet;
  405. Procedure TestArray_DynAsParam;
  406. Procedure TestArray_StaticAsParam;
  407. Procedure TestArrayElement_AsParams;
  408. Procedure TestArrayElementFromFuncResult_AsParams;
  409. Procedure TestArrayEnumTypeRange;
  410. Procedure TestArray_SetLengthOutArg;
  411. Procedure TestArray_SetLengthProperty;
  412. Procedure TestArray_SetLengthMultiDim;
  413. Procedure TestArray_SetLengthDynOfStatic;
  414. Procedure TestArray_OpenArrayOfString;
  415. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  416. Procedure TestArray_ConstRef;
  417. Procedure TestArray_Concat;
  418. Procedure TestArray_Copy;
  419. Procedure TestArray_InsertDelete;
  420. Procedure TestArray_DynArrayConstObjFPC;
  421. Procedure TestArray_DynArrayConstDelphi;
  422. Procedure TestArray_ArrayLitAsParam;
  423. Procedure TestArray_ArrayLitMultiDimAsParam;
  424. Procedure TestArray_ArrayLitStaticAsParam;
  425. Procedure TestArray_ForInArrOfString;
  426. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  427. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  428. Procedure TestArrayOfConst_TVarRec;
  429. Procedure TestArrayOfConst_PassBaseTypes;
  430. Procedure TestArrayOfConst_PassObj;
  431. // record
  432. Procedure TestRecord_Empty;
  433. Procedure TestRecord_Var;
  434. Procedure TestRecord_VarExternal;
  435. Procedure TestRecord_WithDo;
  436. Procedure TestRecord_Assign;
  437. Procedure TestRecord_AsParams;
  438. Procedure TestRecord_ConstRef;
  439. Procedure TestRecordElement_AsParams;
  440. Procedure TestRecordElementFromFuncResult_AsParams;
  441. Procedure TestRecordElementFromWith_AsParams;
  442. Procedure TestRecord_Equal;
  443. Procedure TestRecord_JSValue;
  444. Procedure TestRecord_VariantFail;
  445. Procedure TestRecord_FieldArray;
  446. Procedure TestRecord_Const;
  447. Procedure TestRecord_TypecastFail;
  448. Procedure TestRecord_InFunction;
  449. Procedure TestRecord_AnonymousFail;
  450. // advanced record
  451. Procedure TestAdvRecord_Function;
  452. Procedure TestAdvRecord_Property;
  453. Procedure TestAdvRecord_PropertyDefault;
  454. Procedure TestAdvRecord_Property_ClassMethod;
  455. Procedure TestAdvRecord_Const;
  456. Procedure TestAdvRecord_ExternalField;
  457. Procedure TestAdvRecord_SubRecord;
  458. Procedure TestAdvRecord_SubClass;
  459. Procedure TestAdvRecord_SubInterfaceFail;
  460. Procedure TestAdvRecord_Constructor;
  461. Procedure TestAdvRecord_ClassConstructor_Program;
  462. Procedure TestAdvRecord_ClassConstructor_Unit;
  463. // classes
  464. Procedure TestClass_TObjectDefaultConstructor;
  465. Procedure TestClass_TObjectConstructorWithParams;
  466. Procedure TestClass_TObjectConstructorWithDefaultParam;
  467. Procedure TestClass_Var;
  468. Procedure TestClass_Method;
  469. Procedure TestClass_Implementation;
  470. Procedure TestClass_Inheritance;
  471. Procedure TestClass_TypeAlias;
  472. Procedure TestClass_AbstractMethod;
  473. Procedure TestClass_CallInherited_ProcNoParams;
  474. Procedure TestClass_CallInherited_WithParams;
  475. Procedure TestClasS_CallInheritedConstructor;
  476. Procedure TestClass_ClassVar_Assign;
  477. Procedure TestClass_CallClassMethod;
  478. Procedure TestClass_Property;
  479. Procedure TestClass_Property_ClassMethod;
  480. Procedure TestClass_Property_Indexed;
  481. Procedure TestClass_Property_IndexSpec;
  482. Procedure TestClass_PropertyOfTypeArray;
  483. Procedure TestClass_PropertyDefault;
  484. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  485. //Procedure TestClass_PropertyDefault;
  486. Procedure TestClass_PropertyOverride;
  487. Procedure TestClass_PropertyIncVisibility;
  488. Procedure TestClass_Assigned;
  489. Procedure TestClass_WithClassDoCreate;
  490. Procedure TestClass_WithClassInstDoProperty;
  491. Procedure TestClass_WithClassInstDoPropertyWithParams;
  492. Procedure TestClass_WithClassInstDoFunc;
  493. Procedure TestClass_TypeCast;
  494. Procedure TestClass_TypeCastUntypedParam;
  495. Procedure TestClass_Overloads;
  496. Procedure TestClass_OverloadsAncestor;
  497. Procedure TestClass_OverloadConstructor;
  498. Procedure TestClass_OverloadDelphiOverride;
  499. Procedure TestClass_ReintroduceVarDelphi;
  500. Procedure TestClass_ReintroducedVar;
  501. Procedure TestClass_RaiseDescendant;
  502. Procedure TestClass_ExternalMethod;
  503. Procedure TestClass_ExternalVirtualNameMismatchFail;
  504. Procedure TestClass_ExternalOverrideFail;
  505. Procedure TestClass_ExternalVar;
  506. Procedure TestClass_Const;
  507. Procedure TestClass_LocalConstDuplicate_Prg;
  508. Procedure TestClass_LocalConstDuplicate_Unit;
  509. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  510. Procedure TestClass_LocalVarSelfFail;
  511. Procedure TestClass_ArgSelfFail;
  512. Procedure TestClass_NestedProcSelf;
  513. Procedure TestClass_NestedProcSelf2;
  514. Procedure TestClass_NestedProcClassSelf;
  515. Procedure TestClass_NestedProcCallInherited;
  516. Procedure TestClass_TObjectFree;
  517. Procedure TestClass_TObjectFree_VarArg;
  518. Procedure TestClass_TObjectFreeNewInstance;
  519. Procedure TestClass_TObjectFreeLowerCase;
  520. Procedure TestClass_TObjectFreeFunctionFail;
  521. Procedure TestClass_TObjectFreePropertyFail;
  522. Procedure TestClass_ForIn;
  523. Procedure TestClass_DispatchMessage;
  524. Procedure TestClass_Message_DuplicateIntFail;
  525. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  526. // class of
  527. Procedure TestClassOf_Create;
  528. Procedure TestClassOf_Call;
  529. Procedure TestClassOf_Assign;
  530. Procedure TestClassOf_Is;
  531. Procedure TestClassOf_Compare;
  532. Procedure TestClassOf_ClassVar;
  533. Procedure TestClassOf_ClassMethod;
  534. Procedure TestClassOf_ClassProperty;
  535. Procedure TestClassOf_ClassMethodSelf;
  536. Procedure TestClassOf_TypeCast;
  537. Procedure TestClassOf_ImplicitFunctionCall;
  538. Procedure TestClassOf_Const;
  539. // nested class
  540. Procedure TestNestedClass_Alias;
  541. Procedure TestNestedClass_Record;
  542. Procedure TestNestedClass_Class;
  543. // external class
  544. Procedure TestExternalClass_Var;
  545. Procedure TestExternalClass_Const;
  546. Procedure TestExternalClass_Dollar;
  547. Procedure TestExternalClass_DuplicateVarFail;
  548. Procedure TestExternalClass_Method;
  549. Procedure TestExternalClass_ClassMethod;
  550. Procedure TestExternalClass_ClassMethodStatic;
  551. Procedure TestExternalClass_FunctionResultInTypeCast;
  552. Procedure TestExternalClass_NonExternalOverride;
  553. Procedure TestExternalClass_OverloadHint;
  554. Procedure TestExternalClass_SameNamePublishedProperty;
  555. Procedure TestExternalClass_Property;
  556. Procedure TestExternalClass_PropertyDate;
  557. Procedure TestExternalClass_ClassProperty;
  558. Procedure TestExternalClass_ClassOf;
  559. Procedure TestExternalClass_ClassOtherUnit;
  560. Procedure TestExternalClass_Is;
  561. Procedure TestExternalClass_As;
  562. Procedure TestExternalClass_DestructorFail;
  563. Procedure TestExternalClass_New;
  564. Procedure TestExternalClass_ClassOf_New;
  565. Procedure TestExternalClass_FuncClassOf_New;
  566. Procedure TestExternalClass_New_PasClassFail;
  567. Procedure TestExternalClass_New_PasClassBracketsFail;
  568. Procedure TestExternalClass_NewExtName;
  569. Procedure TestExternalClass_Constructor;
  570. Procedure TestExternalClass_ConstructorBrackets;
  571. Procedure TestExternalClass_LocalConstSameName;
  572. Procedure TestExternalClass_ReintroduceOverload;
  573. Procedure TestExternalClass_Inherited;
  574. Procedure TestExternalClass_PascalAncestorFail;
  575. Procedure TestExternalClass_NewInstance;
  576. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  577. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  578. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  579. Procedure TestExternalClass_JSFunctionPasDescendant;
  580. Procedure TestExternalClass_PascalProperty;
  581. Procedure TestExternalClass_TypeCastToRootClass;
  582. Procedure TestExternalClass_TypeCastToJSObject;
  583. Procedure TestExternalClass_TypeCastStringToExternalString;
  584. Procedure TestExternalClass_TypeCastToJSFunction;
  585. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  586. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  587. Procedure TestExternalClass_BracketAccessor;
  588. Procedure TestExternalClass_BracketAccessor_Call;
  589. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  590. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  591. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  592. Procedure TestExternalClass_BracketAccessor_MultiType;
  593. Procedure TestExternalClass_BracketAccessor_Index;
  594. Procedure TestExternalClass_ForInJSObject;
  595. Procedure TestExternalClass_ForInJSArray;
  596. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  597. // class interfaces
  598. Procedure TestClassInterface_Corba;
  599. Procedure TestClassInterface_ProcExternalFail;
  600. Procedure TestClassInterface_Overloads;
  601. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  602. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  603. Procedure TestClassInterface_AncestorImpl;
  604. Procedure TestClassInterface_ImplReintroduce;
  605. Procedure TestClassInterface_MethodResolution;
  606. Procedure TestClassInterface_AncestorMoreInterfaces;
  607. Procedure TestClassInterface_MethodOverride;
  608. Procedure TestClassInterface_Corba_Delegation;
  609. Procedure TestClassInterface_Corba_DelegationStatic;
  610. Procedure TestClassInterface_Corba_Operators;
  611. Procedure TestClassInterface_Corba_Args;
  612. Procedure TestClassInterface_Corba_ForIn;
  613. Procedure TestClassInterface_COM_AssignVar;
  614. Procedure TestClassInterface_COM_AssignArg;
  615. Procedure TestClassInterface_COM_FunctionResult;
  616. Procedure TestClassInterface_COM_InheritedFuncResult;
  617. Procedure TestClassInterface_COM_IsAsTypeCasts;
  618. Procedure TestClassInterface_COM_PassAsArg;
  619. Procedure TestClassInterface_COM_PassToUntypedParam;
  620. Procedure TestClassInterface_COM_FunctionInExpr;
  621. Procedure TestClassInterface_COM_Property;
  622. Procedure TestClassInterface_COM_IntfProperty;
  623. Procedure TestClassInterface_COM_Delegation;
  624. Procedure TestClassInterface_COM_With;
  625. Procedure TestClassInterface_COM_ForIn;
  626. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  627. Procedure TestClassInterface_COM_RecordIntfFail;
  628. Procedure TestClassInterface_COM_UnitInitialization;
  629. Procedure TestClassInterface_GUID;
  630. Procedure TestClassInterface_GUIDProperty;
  631. // helpers
  632. Procedure TestClassHelper_ClassVar;
  633. Procedure TestClassHelper_Method_AccessInstanceFields;
  634. Procedure TestClassHelper_Method_Call;
  635. Procedure TestClassHelper_Method_Nested_Call;
  636. Procedure TestClassHelper_ClassMethod_Call;
  637. Procedure TestClassHelper_ClassOf;
  638. Procedure TestClassHelper_MethodRefObjFPC;
  639. Procedure TestClassHelper_Constructor;
  640. Procedure TestClassHelper_InheritedObjFPC;
  641. Procedure TestClassHelper_Property;
  642. Procedure TestClassHelper_Property_Array;
  643. Procedure TestClassHelper_Property_Array_Default;
  644. Procedure TestClassHelper_Property_Array_DefaultDefault;
  645. Procedure TestClassHelper_ClassProperty;
  646. Procedure TestClassHelper_ClassPropertyStatic;
  647. Procedure TestClassHelper_ClassProperty_Array;
  648. Procedure TestClassHelper_ForIn;
  649. Procedure TestClassHelper_PassProperty;
  650. Procedure TestExtClassHelper_ClassVar;
  651. Procedure TestExtClassHelper_Method_Call;
  652. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  653. Procedure TestRecordHelper_ClassVar;
  654. Procedure TestRecordHelper_Method_Call;
  655. Procedure TestRecordHelper_Constructor;
  656. Procedure TestTypeHelper_ClassVar;
  657. Procedure TestTypeHelper_PassResultElement;
  658. Procedure TestTypeHelper_PassArgs;
  659. Procedure TestTypeHelper_PassVarConst;
  660. Procedure TestTypeHelper_PassFuncResult;
  661. Procedure TestTypeHelper_PassPropertyField;
  662. Procedure TestTypeHelper_PassPropertyGetter;
  663. Procedure TestTypeHelper_PassClassPropertyField;
  664. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  665. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  666. Procedure TestTypeHelper_Property;
  667. Procedure TestTypeHelper_Property_Array;
  668. Procedure TestTypeHelper_ClassProperty;
  669. Procedure TestTypeHelper_ClassProperty_Array;
  670. Procedure TestTypeHelper_ClassMethod;
  671. Procedure TestTypeHelper_ExtClassMethodFail;
  672. Procedure TestTypeHelper_Constructor;
  673. Procedure TestTypeHelper_Word;
  674. Procedure TestTypeHelper_Boolean;
  675. Procedure TestTypeHelper_WordBool;
  676. Procedure TestTypeHelper_Double;
  677. Procedure TestTypeHelper_NativeInt;
  678. Procedure TestTypeHelper_StringChar;
  679. Procedure TestTypeHelper_JSValue;
  680. Procedure TestTypeHelper_Array;
  681. Procedure TestTypeHelper_EnumType;
  682. Procedure TestTypeHelper_SetType;
  683. Procedure TestTypeHelper_InterfaceType;
  684. Procedure TestTypeHelper_NestedSelf;
  685. // proc types
  686. Procedure TestProcType;
  687. Procedure TestProcType_Arg;
  688. Procedure TestProcType_FunctionFPC;
  689. Procedure TestProcType_FunctionDelphi;
  690. Procedure TestProcType_ProcedureDelphi;
  691. Procedure TestProcType_AsParam;
  692. Procedure TestProcType_MethodFPC;
  693. Procedure TestProcType_MethodDelphi;
  694. Procedure TestProcType_PropertyFPC;
  695. Procedure TestProcType_PropertyDelphi;
  696. Procedure TestProcType_WithClassInstDoPropertyFPC;
  697. Procedure TestProcType_Nested;
  698. Procedure TestProcType_NestedOfObject;
  699. Procedure TestProcType_ReferenceToProc;
  700. Procedure TestProcType_ReferenceToMethod;
  701. Procedure TestProcType_Typecast;
  702. Procedure TestProcType_PassProcToUntyped;
  703. Procedure TestProcType_PassProcToArray;
  704. Procedure TestProcType_SafeCallObjFPC;
  705. Procedure TestProcType_SafeCallDelphi;
  706. // pointer
  707. Procedure TestPointer;
  708. Procedure TestPointer_Proc;
  709. Procedure TestPointer_AssignRecordFail;
  710. Procedure TestPointer_AssignStaticArrayFail;
  711. Procedure TestPointer_TypeCastJSValueToPointer;
  712. Procedure TestPointer_NonRecordFail;
  713. Procedure TestPointer_AnonymousArgTypeFail;
  714. Procedure TestPointer_AnonymousVarTypeFail;
  715. Procedure TestPointer_AnonymousResultTypeFail;
  716. Procedure TestPointer_AddrOperatorFail;
  717. Procedure TestPointer_ArrayParamsFail;
  718. Procedure TestPointer_PointerAddFail;
  719. Procedure TestPointer_IncPointerFail;
  720. Procedure TestPointer_Record;
  721. Procedure TestPointer_RecordArg;
  722. // jsvalue
  723. Procedure TestJSValue_AssignToJSValue;
  724. Procedure TestJSValue_TypeCastToBaseType;
  725. Procedure TestJSValue_TypecastToJSValue;
  726. Procedure TestJSValue_Equal;
  727. Procedure TestJSValue_If;
  728. Procedure TestJSValue_Not;
  729. Procedure TestJSValue_Enum;
  730. Procedure TestJSValue_ClassInstance;
  731. Procedure TestJSValue_ClassOf;
  732. Procedure TestJSValue_ArrayOfJSValue;
  733. Procedure TestJSValue_ArrayLit;
  734. Procedure TestJSValue_Params;
  735. Procedure TestJSValue_UntypedParam;
  736. Procedure TestJSValue_FuncResultType;
  737. Procedure TestJSValue_ProcType_Assign;
  738. Procedure TestJSValue_ProcType_Equal;
  739. Procedure TestJSValue_ProcType_Param;
  740. Procedure TestJSValue_AssignToPointerFail;
  741. Procedure TestJSValue_OverloadDouble;
  742. Procedure TestJSValue_OverloadNativeInt;
  743. Procedure TestJSValue_OverloadWord;
  744. Procedure TestJSValue_OverloadString;
  745. Procedure TestJSValue_OverloadChar;
  746. Procedure TestJSValue_OverloadPointer;
  747. Procedure TestJSValue_ForIn;
  748. // RTTI
  749. Procedure TestRTTI_IntRange;
  750. Procedure TestRTTI_Double;
  751. Procedure TestRTTI_ProcType;
  752. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  753. Procedure TestRTTI_EnumAndSetType;
  754. Procedure TestRTTI_EnumRange;
  755. Procedure TestRTTI_AnonymousEnumType;
  756. Procedure TestRTTI_StaticArray;
  757. Procedure TestRTTI_DynArray;
  758. Procedure TestRTTI_ArrayNestedAnonymous;
  759. Procedure TestRTTI_PublishedMethodOverloadFail;
  760. Procedure TestRTTI_PublishedMethodExternalFail;
  761. Procedure TestRTTI_PublishedClassPropertyFail;
  762. Procedure TestRTTI_PublishedClassFieldFail;
  763. Procedure TestRTTI_PublishedFieldExternalFail;
  764. Procedure TestRTTI_Class_Field;
  765. Procedure TestRTTI_Class_Method;
  766. Procedure TestRTTI_Class_MethodArgFlags;
  767. Procedure TestRTTI_Class_Property;
  768. Procedure TestRTTI_Class_PropertyParams;
  769. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  770. Procedure TestRTTI_Class_OmitRTTI;
  771. Procedure TestRTTI_IndexModifier;
  772. Procedure TestRTTI_StoredModifier;
  773. Procedure TestRTTI_DefaultValue;
  774. Procedure TestRTTI_DefaultValueSet;
  775. Procedure TestRTTI_DefaultValueRangeType;
  776. Procedure TestRTTI_DefaultValueInherit;
  777. Procedure TestRTTI_OverrideMethod;
  778. Procedure TestRTTI_ReintroduceMethod;
  779. Procedure TestRTTI_OverloadProperty;
  780. // ToDo: array argument
  781. Procedure TestRTTI_ClassForward;
  782. Procedure TestRTTI_ClassOf;
  783. Procedure TestRTTI_Record;
  784. Procedure TestRTTI_RecordAnonymousArray;
  785. Procedure TestRTTI_LocalTypes;
  786. Procedure TestRTTI_TypeInfo_BaseTypes;
  787. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  788. Procedure TestRTTI_TypeInfo_LocalFail;
  789. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  790. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  791. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  792. Procedure TestRTTI_TypeInfo_FunctionClassType;
  793. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  794. Procedure TestRTTI_Interface_Corba;
  795. Procedure TestRTTI_Interface_COM;
  796. Procedure TestRTTI_ClassHelper;
  797. Procedure TestRTTI_ExternalClass;
  798. // Resourcestring
  799. Procedure TestResourcestringProgram;
  800. Procedure TestResourcestringUnit;
  801. Procedure TestResourcestringImplementation;
  802. // Attributes
  803. Procedure TestAttributes_Members;
  804. Procedure TestAttributes_Types;
  805. Procedure TestAttributes_HelperConstructor_Fail;
  806. // Assertions, checks
  807. procedure TestAssert;
  808. procedure TestAssert_SysUtils;
  809. procedure TestObjectChecks;
  810. procedure TestOverflowChecks_Int;
  811. procedure TestRangeChecks_AssignInt;
  812. procedure TestRangeChecks_AssignIntRange;
  813. procedure TestRangeChecks_AssignEnum;
  814. procedure TestRangeChecks_AssignEnumRange;
  815. procedure TestRangeChecks_AssignChar;
  816. procedure TestRangeChecks_AssignCharRange;
  817. procedure TestRangeChecks_ArrayIndex;
  818. procedure TestRangeChecks_ArrayOfRecIndex;
  819. procedure TestRangeChecks_StringIndex;
  820. procedure TestRangeChecks_TypecastInt;
  821. procedure TestRangeChecks_TypeHelperInt;
  822. // Async/AWait
  823. Procedure TestAsync_Proc;
  824. Procedure TestAsync_CallResultIsPromise;
  825. Procedure TestAsync_ConstructorFail;
  826. Procedure TestAsync_PropertyGetterFail;
  827. Procedure TestAwait_NonPromiseWithTypeFail;
  828. Procedure TestAWait_OutsideAsyncFail;
  829. Procedure TestAWait_Result;
  830. Procedure TestAWait_ExternalClassPromise;
  831. Procedure TestAsync_AnonymousProc;
  832. Procedure TestAsync_ProcType;
  833. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  834. Procedure TestAsync_Inherited;
  835. end;
  836. function LinesToStr(Args: array of const): string;
  837. function ExtractFileUnitName(aFilename: string): string;
  838. function JSToStr(El: TJSElement): string;
  839. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  840. implementation
  841. function LinesToStr(Args: array of const): string;
  842. var
  843. s: String;
  844. i: Integer;
  845. begin
  846. s:='';
  847. for i:=Low(Args) to High(Args) do
  848. case Args[i].VType of
  849. vtChar: s += Args[i].VChar+LineEnding;
  850. vtString: s += Args[i].VString^+LineEnding;
  851. vtPChar: s += Args[i].VPChar+LineEnding;
  852. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  853. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  854. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  855. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  856. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  857. end;
  858. Result:=s;
  859. end;
  860. function ExtractFileUnitName(aFilename: string): string;
  861. var
  862. p: Integer;
  863. begin
  864. Result:=ExtractFileName(aFilename);
  865. if Result='' then exit;
  866. for p:=length(Result) downto 1 do
  867. case Result[p] of
  868. '/','\': exit;
  869. '.':
  870. begin
  871. Delete(Result,p,length(Result));
  872. exit;
  873. end;
  874. end;
  875. end;
  876. function JSToStr(El: TJSElement): string;
  877. var
  878. aWriter: TBufferWriter;
  879. aJSWriter: TJSWriter;
  880. begin
  881. aJSWriter:=nil;
  882. aWriter:=TBufferWriter.Create(1000);
  883. try
  884. aJSWriter:=TJSWriter.Create(aWriter);
  885. aJSWriter.IndentSize:=2;
  886. aJSWriter.WriteJS(El);
  887. Result:=aWriter.AsString;
  888. finally
  889. aJSWriter.Free;
  890. aWriter.Free;
  891. end;
  892. end;
  893. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  894. // search diff, ignore changes in spaces
  895. const
  896. SpaceChars = [#9,#10,#13,' '];
  897. var
  898. ExpectedP, ActualP: PChar;
  899. function FindLineEnd(p: PChar): PChar;
  900. begin
  901. Result:=p;
  902. while not (Result^ in [#0,#10,#13]) do inc(Result);
  903. end;
  904. function FindLineStart(p, MinP: PChar): PChar;
  905. begin
  906. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  907. Result:=p;
  908. end;
  909. procedure SkipLineEnd(var p: PChar);
  910. begin
  911. if p^ in [#10,#13] then
  912. begin
  913. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  914. inc(p,2)
  915. else
  916. inc(p);
  917. end;
  918. end;
  919. procedure DiffFound;
  920. var
  921. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  922. ExpLine, ActLine: String;
  923. i, LineNo, DiffLineNo: Integer;
  924. begin
  925. writeln('Diff found "',Msg,'". Lines:');
  926. // write correct lines
  927. p:=PChar(Expected);
  928. LineNo:=0;
  929. DiffLineNo:=0;
  930. repeat
  931. StartPos:=p;
  932. while not (p^ in [#0,#10,#13]) do inc(p);
  933. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  934. SkipLineEnd(p);
  935. inc(LineNo);
  936. if (p<=ExpectedP) and (p^<>#0) then
  937. begin
  938. writeln('= ',ExpLine);
  939. end else begin
  940. // diff line
  941. if DiffLineNo=0 then DiffLineNo:=LineNo;
  942. // write actual line
  943. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  944. ActLineEndP:=FindLineEnd(ActualP);
  945. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  946. writeln('- ',ActLine);
  947. // write expected line
  948. writeln('+ ',ExpLine);
  949. // write empty line with pointer ^
  950. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  951. writeln('^');
  952. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  953. CheckSrcDiff:=false;
  954. // write up to three following actual lines to get some context
  955. for i:=1 to 3 do begin
  956. ActLineStartP:=ActLineEndP;
  957. SkipLineEnd(ActLineStartP);
  958. if ActLineStartP^=#0 then break;
  959. ActLineEndP:=FindLineEnd(ActLineStartP);
  960. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  961. writeln('~ ',ActLine);
  962. end;
  963. exit;
  964. end;
  965. until p^=#0;
  966. writeln('DiffFound Actual:-----------------------');
  967. writeln(Actual);
  968. writeln('DiffFound Expected:---------------------');
  969. writeln(Expected);
  970. writeln('DiffFound ------------------------------');
  971. Msg:='diff found, but lines are the same, internal error';
  972. CheckSrcDiff:=false;
  973. end;
  974. var
  975. IsSpaceNeeded: Boolean;
  976. LastChar, Quote: Char;
  977. begin
  978. Result:=true;
  979. Msg:='';
  980. if Expected='' then Expected:=' ';
  981. if Actual='' then Actual:=' ';
  982. ExpectedP:=PChar(Expected);
  983. ActualP:=PChar(Actual);
  984. repeat
  985. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  986. case ExpectedP^ of
  987. #0:
  988. begin
  989. // check that rest of Actual has only spaces
  990. while ActualP^ in SpaceChars do inc(ActualP);
  991. if ActualP^<>#0 then
  992. begin
  993. DiffFound;
  994. exit;
  995. end;
  996. exit(true);
  997. end;
  998. ' ',#9,#10,#13:
  999. begin
  1000. // skip space in Expected
  1001. IsSpaceNeeded:=false;
  1002. if ExpectedP>PChar(Expected) then
  1003. LastChar:=ExpectedP[-1]
  1004. else
  1005. LastChar:=#0;
  1006. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1007. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1008. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1009. IsSpaceNeeded:=true;
  1010. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1011. begin
  1012. DiffFound;
  1013. exit;
  1014. end;
  1015. while ActualP^ in SpaceChars do inc(ActualP);
  1016. end;
  1017. '''','"':
  1018. begin
  1019. while ActualP^ in SpaceChars do inc(ActualP);
  1020. if ExpectedP^<>ActualP^ then
  1021. begin
  1022. DiffFound;
  1023. exit;
  1024. end;
  1025. Quote:=ExpectedP^;
  1026. repeat
  1027. inc(ExpectedP);
  1028. inc(ActualP);
  1029. if ExpectedP^<>ActualP^ then
  1030. begin
  1031. DiffFound;
  1032. exit;
  1033. end;
  1034. if (ExpectedP^ in [#0,#10,#13]) then
  1035. break
  1036. else if (ExpectedP^=Quote) then
  1037. begin
  1038. inc(ExpectedP);
  1039. inc(ActualP);
  1040. break;
  1041. end;
  1042. until false;
  1043. end;
  1044. else
  1045. while ActualP^ in SpaceChars do inc(ActualP);
  1046. if ExpectedP^<>ActualP^ then
  1047. begin
  1048. DiffFound;
  1049. exit;
  1050. end;
  1051. inc(ExpectedP);
  1052. inc(ActualP);
  1053. end;
  1054. until false;
  1055. end;
  1056. { TTestEnginePasResolver }
  1057. destructor TTestEnginePasResolver.Destroy;
  1058. begin
  1059. FreeAndNil(FStreamResolver);
  1060. FreeAndNil(FParser);
  1061. FreeAndNil(FScanner);
  1062. FreeAndNil(FStreamResolver);
  1063. if Module<>nil then
  1064. begin
  1065. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1066. FModule:=nil;
  1067. end;
  1068. inherited Destroy;
  1069. end;
  1070. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1071. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1072. begin
  1073. Result:=nil;
  1074. if InFilename<>'' then
  1075. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1076. if Assigned(OnFindUnit) then
  1077. Result:=OnFindUnit(AName);
  1078. if NameExpr=nil then ;
  1079. end;
  1080. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1081. begin
  1082. // do not parse recursively
  1083. // parse via the queue
  1084. if Section=nil then ;
  1085. end;
  1086. { TCustomTestModule }
  1087. function TCustomTestModule.GetMsgCount: integer;
  1088. begin
  1089. Result:=FHintMsgs.Count;
  1090. end;
  1091. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1092. begin
  1093. Result:=TTestHintMessage(FHintMsgs[Index]);
  1094. end;
  1095. function TCustomTestModule.GetResolverCount: integer;
  1096. begin
  1097. Result:=FModules.Count;
  1098. end;
  1099. function TCustomTestModule.GetResolvers(Index: integer
  1100. ): TTestEnginePasResolver;
  1101. begin
  1102. Result:=TTestEnginePasResolver(FModules[Index]);
  1103. end;
  1104. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1105. ): TPasModule;
  1106. var
  1107. DefNamespace: String;
  1108. begin
  1109. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1110. if (Pos('.',aUnitName)<1) then
  1111. begin
  1112. DefNamespace:=GetDefaultNamespace;
  1113. if DefNamespace<>'' then
  1114. begin
  1115. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1116. if Result<>nil then exit;
  1117. end;
  1118. end;
  1119. Result:=LoadUnit(aUnitName);
  1120. if Result<>nil then exit;
  1121. {$IFDEF VerbosePas2JS}
  1122. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1123. {$ENDIF}
  1124. Fail('can''t find unit "'+aUnitName+'"');
  1125. end;
  1126. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1127. var
  1128. aParser: TPasParser;
  1129. Item: TTestHintMessage;
  1130. begin
  1131. aParser:=Sender as TPasParser;
  1132. Item:=TTestHintMessage.Create;
  1133. Item.Id:=aParser.LastMsgNumber;
  1134. Item.MsgType:=aParser.LastMsgType;
  1135. Item.MsgNumber:=aParser.LastMsgNumber;
  1136. Item.Msg:=Msg;
  1137. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1138. {$IFDEF VerbosePas2JS}
  1139. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1140. {$ENDIF}
  1141. FHintMsgs.Add(Item);
  1142. end;
  1143. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1144. );
  1145. var
  1146. aResolver: TTestEnginePasResolver;
  1147. Item: TTestHintMessage;
  1148. begin
  1149. aResolver:=Sender as TTestEnginePasResolver;
  1150. Item:=TTestHintMessage.Create;
  1151. Item.Id:=aResolver.LastMsgId;
  1152. Item.MsgType:=aResolver.LastMsgType;
  1153. Item.MsgNumber:=aResolver.LastMsgNumber;
  1154. Item.Msg:=Msg;
  1155. Item.SourcePos:=aResolver.LastSourcePos;
  1156. {$IFDEF VerbosePas2JS}
  1157. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1158. {$ENDIF}
  1159. FHintMsgs.Add(Item);
  1160. end;
  1161. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1162. var
  1163. Item: TTestHintMessage;
  1164. aScanner: TPas2jsPasScanner;
  1165. begin
  1166. aScanner:=Sender as TPas2jsPasScanner;
  1167. Item:=TTestHintMessage.Create;
  1168. Item.Id:=aScanner.LastMsgNumber;
  1169. Item.MsgType:=aScanner.LastMsgType;
  1170. Item.MsgNumber:=aScanner.LastMsgNumber;
  1171. Item.Msg:=Msg;
  1172. Item.SourcePos:=aScanner.CurSourcePos;
  1173. {$IFDEF VerbosePas2JS}
  1174. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1175. {$ENDIF}
  1176. FHintMsgs.Add(Item);
  1177. end;
  1178. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1179. var
  1180. i: Integer;
  1181. CurEngine: TTestEnginePasResolver;
  1182. CurUnitName: String;
  1183. begin
  1184. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1185. Result:=nil;
  1186. if (Module.ClassType=TPasModule)
  1187. and (CompareText(Module.Name,aUnitName)=0) then
  1188. exit(Module);
  1189. for i:=0 to ResolverCount-1 do
  1190. begin
  1191. CurEngine:=Resolvers[i];
  1192. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1193. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1194. if CompareText(aUnitName,CurUnitName)=0 then
  1195. begin
  1196. Result:=CurEngine.Module;
  1197. if Result<>nil then exit;
  1198. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1199. FileResolver.FindSourceFile(aUnitName);
  1200. CurEngine.StreamResolver:=TStreamResolver.Create;
  1201. CurEngine.StreamResolver.OwnsStreams:=True;
  1202. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1203. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1204. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1205. InitScanner(CurEngine.Scanner);
  1206. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1207. CurEngine.Parser.Options:=po_tcmodules;
  1208. if CompareText(CurUnitName,'System')=0 then
  1209. CurEngine.Parser.ImplicitUses.Clear;
  1210. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1211. try
  1212. CurEngine.Parser.NextToken;
  1213. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1214. except
  1215. on E: Exception do
  1216. HandleException(E);
  1217. end;
  1218. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1219. Result:=CurEngine.Module;
  1220. exit;
  1221. end;
  1222. end;
  1223. end;
  1224. procedure TCustomTestModule.SetUp;
  1225. begin
  1226. {$IFDEF EnablePasTreeGlobalRefCount}
  1227. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1228. {$ENDIF}
  1229. if FModules<>nil then
  1230. begin
  1231. writeln('TCustomTestModule.SetUp FModules<>nil');
  1232. Halt;
  1233. end;
  1234. inherited SetUp;
  1235. FSkipTests:=false;
  1236. FSource:=TStringList.Create;
  1237. FHub:=TPas2JSResolverHub.Create(Self);
  1238. FModules:=TObjectList.Create(true);
  1239. FFilename:='test1.pp';
  1240. FFileResolver:=TStreamResolver.Create;
  1241. FFileResolver.OwnsStreams:=True;
  1242. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1243. InitScanner(FScanner);
  1244. FEngine:=AddModule(Filename);
  1245. FEngine.Scanner:=FScanner;
  1246. FScanner.Resolver:=FEngine;
  1247. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1248. FParser.OnLog:=@OnParserLog;
  1249. FEngine.Parser:=FParser;
  1250. Parser.Options:=po_tcmodules;
  1251. FModule:=Nil;
  1252. FConverter:=CreateConverter;
  1253. FExpectedErrorClass:=nil;
  1254. end;
  1255. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1256. begin
  1257. Result:=TPasToJSConverter.Create;
  1258. Result.Options:=co_tcmodules;
  1259. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1260. end;
  1261. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1262. begin
  1263. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1264. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1265. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1266. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1267. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1268. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1269. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1270. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1271. aScanner.OnLog:=@OnScannerLog;
  1272. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1273. end;
  1274. procedure TCustomTestModule.TearDown;
  1275. {$IFDEF CheckPasTreeRefCount}
  1276. var
  1277. El: TPasElement;
  1278. {$ENDIF}
  1279. var
  1280. i: Integer;
  1281. CurModule: TPasModule;
  1282. begin
  1283. FHintMsgs.Clear;
  1284. FHintMsgsGood.Clear;
  1285. FSkipTests:=false;
  1286. FJSRegModuleCall:=nil;
  1287. FJSModuleCallArgs:=nil;
  1288. FJSImplentationUses:=nil;
  1289. FJSInterfaceUses:=nil;
  1290. FJSModuleSrc:=nil;
  1291. FJSInitBody:=nil;
  1292. FreeAndNil(FJSSource);
  1293. FreeAndNil(FJSModule);
  1294. FreeAndNil(FConverter);
  1295. Engine.Clear;
  1296. FreeAndNil(FSource);
  1297. FreeAndNil(FFileResolver);
  1298. if FModules<>nil then
  1299. begin
  1300. for i:=0 to FModules.Count-1 do
  1301. begin
  1302. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1303. if CurModule=nil then continue;
  1304. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1305. CurModule.ReleaseUsedUnits;
  1306. end;
  1307. if FModule<>nil then
  1308. FModule.ReleaseUsedUnits;
  1309. for i:=0 to FModules.Count-1 do
  1310. begin
  1311. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1312. if CurModule=nil then continue;
  1313. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1314. end;
  1315. FreeAndNil(FModules);
  1316. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1317. FEngine:=nil;
  1318. end;
  1319. FreeAndNil(FHub);
  1320. inherited TearDown;
  1321. {$IFDEF EnablePasTreeGlobalRefCount}
  1322. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1323. begin
  1324. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1325. {$IFDEF CheckPasTreeRefCount}
  1326. El:=TPasElement.FirstRefEl;
  1327. while El<>nil do
  1328. begin
  1329. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1330. for i:=0 to El.RefIds.Count-1 do
  1331. writeln(' ',El.RefIds[i]);
  1332. El:=El.NextRefEl;
  1333. end;
  1334. {$ENDIF}
  1335. Halt;
  1336. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1337. end;
  1338. {$ENDIF}
  1339. end;
  1340. procedure TCustomTestModule.Add(Line: string);
  1341. begin
  1342. Source.Add(Line);
  1343. end;
  1344. procedure TCustomTestModule.Add(const Lines: array of string);
  1345. var
  1346. i: Integer;
  1347. begin
  1348. for i:=low(Lines) to high(Lines) do
  1349. Add(Lines[i]);
  1350. end;
  1351. procedure TCustomTestModule.StartParsing;
  1352. var
  1353. Src: String;
  1354. begin
  1355. Src:=Source.Text;
  1356. FEngine.Source:=Src;
  1357. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1358. Scanner.OpenFile(FileName);
  1359. Writeln('// Test : ',Self.TestName);
  1360. Writeln(Src);
  1361. end;
  1362. procedure TCustomTestModule.ParseModuleQueue;
  1363. var
  1364. i: Integer;
  1365. CurResolver: TTestEnginePasResolver;
  1366. Found: Boolean;
  1367. Section: TPasSection;
  1368. begin
  1369. // parse til exception or all modules finished
  1370. while not SkipTests do
  1371. begin
  1372. Found:=false;
  1373. for i:=0 to ResolverCount-1 do
  1374. begin
  1375. CurResolver:=Resolvers[i];
  1376. if CurResolver.CurrentParser=nil then continue;
  1377. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1378. continue;
  1379. CurResolver.Parser.ParseContinue;
  1380. Found:=true;
  1381. break;
  1382. end;
  1383. if not Found then break;
  1384. end;
  1385. for i:=0 to ResolverCount-1 do
  1386. begin
  1387. CurResolver:=Resolvers[i];
  1388. if CurResolver.Parser=nil then
  1389. begin
  1390. if CurResolver.CurrentParser<>nil then
  1391. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1392. continue;
  1393. end;
  1394. if CurResolver.Parser.CurModule<>nil then
  1395. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1396. end;
  1397. end;
  1398. procedure TCustomTestModule.ParseModule;
  1399. begin
  1400. if SkipTests then exit;
  1401. FFirstPasStatement:=nil;
  1402. try
  1403. StartParsing;
  1404. Parser.ParseMain(FModule);
  1405. ParseModuleQueue;
  1406. except
  1407. on E: Exception do
  1408. HandleException(E);
  1409. end;
  1410. if SkipTests then exit;
  1411. AssertNotNull('Module resulted in Module',Module);
  1412. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1413. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1414. end;
  1415. procedure TCustomTestModule.ParseProgram;
  1416. begin
  1417. if SkipTests then exit;
  1418. ParseModule;
  1419. if SkipTests then exit;
  1420. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1421. FPasProgram:=TPasProgram(Module);
  1422. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1423. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1424. if (PasProgram.InitializationSection.Elements.Count>0) then
  1425. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1426. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1427. end;
  1428. procedure TCustomTestModule.ParseUnit;
  1429. begin
  1430. if SkipTests then exit;
  1431. ParseModule;
  1432. if SkipTests then exit;
  1433. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1434. AssertNotNull('Has interface section',Module.InterfaceSection);
  1435. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1436. if (Module.InitializationSection<>nil)
  1437. and (Module.InitializationSection.Elements.Count>0)
  1438. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1439. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1440. end;
  1441. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1442. ): TTestEnginePasResolver;
  1443. var
  1444. i: Integer;
  1445. begin
  1446. for i:=0 to ResolverCount-1 do
  1447. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1448. exit(Resolvers[i]);
  1449. Result:=nil;
  1450. end;
  1451. function TCustomTestModule.AddModule(aFilename: string
  1452. ): TTestEnginePasResolver;
  1453. begin
  1454. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1455. if FindModuleWithFilename(aFilename)<>nil then
  1456. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1457. Result:=TTestEnginePasResolver.Create;
  1458. Result.Filename:=aFilename;
  1459. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1460. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1461. Result.OnLog:=@OnPasResolverLog;
  1462. Result.Hub:=Hub;
  1463. FModules.Add(Result);
  1464. end;
  1465. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1466. ): TTestEnginePasResolver;
  1467. begin
  1468. Result:=AddModule(aFilename);
  1469. Result.Source:=Src;
  1470. end;
  1471. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1472. ImplementationSrc: string): TTestEnginePasResolver;
  1473. var
  1474. Src: String;
  1475. begin
  1476. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1477. Src+=LineEnding;
  1478. Src+='interface'+LineEnding;
  1479. Src+=LineEnding;
  1480. Src+=InterfaceSrc;
  1481. Src+='implementation'+LineEnding;
  1482. Src+=LineEnding;
  1483. Src+=ImplementationSrc;
  1484. Src+='end.'+LineEnding;
  1485. Result:=AddModuleWithSrc(aFilename,Src);
  1486. end;
  1487. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1488. var
  1489. Intf, Impl: TStringList;
  1490. begin
  1491. Intf:=TStringList.Create;
  1492. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1493. // unit interface
  1494. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1495. Intf.Add('{$modeswitch externalclass}');
  1496. Intf.Add('type');
  1497. Intf.Add(' integer=longint;');
  1498. Intf.Add(' sizeint=nativeint;');
  1499. //'const',
  1500. //' LineEnding = #10;',
  1501. //' DirectorySeparator = ''/'';',
  1502. //' DriveSeparator = '''';',
  1503. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1504. //' AllowDriveSeparators : set of char = [];',
  1505. if supTObject in Parts then
  1506. Intf.AddStrings([
  1507. 'type',
  1508. ' TClass = class of TObject;',
  1509. ' TObject = class',
  1510. ' constructor Create;',
  1511. ' destructor Destroy; virtual;',
  1512. ' class function ClassType: TClass; assembler;',
  1513. ' class function ClassName: String; assembler;',
  1514. ' class function ClassNameIs(const Name: string): boolean;',
  1515. ' class function ClassParent: TClass; assembler;',
  1516. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1517. ' class function UnitName: String; assembler;',
  1518. ' procedure AfterConstruction; virtual;',
  1519. ' procedure BeforeDestruction;virtual;',
  1520. ' function Equals(Obj: TObject): boolean; virtual;',
  1521. ' function ToString: String; virtual;',
  1522. ' end;']);
  1523. if supTInterfacedObject in Parts then
  1524. Intf.AddStrings([
  1525. ' {$Interfaces COM}',
  1526. ' IUnknown = interface',
  1527. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1528. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1529. ' function _AddRef: Integer;',
  1530. ' function _Release: Integer;',
  1531. ' end;',
  1532. ' IInterface = IUnknown;',
  1533. ' TInterfacedObject = class(TObject,IUnknown)',
  1534. ' protected',
  1535. ' fRefCount: Integer;',
  1536. ' { implement methods of IUnknown }',
  1537. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1538. ' function _AddRef: Integer; virtual;',
  1539. ' function _Release: Integer; virtual;',
  1540. ' end;',
  1541. ' TInterfacedClass = class of TInterfacedObject;',
  1542. '',
  1543. '']);
  1544. if supTVarRec in Parts then
  1545. Intf.AddStrings([
  1546. 'const',
  1547. ' vtInteger = 0;',
  1548. ' vtBoolean = 1;',
  1549. ' vtJSValue = 19;',
  1550. 'type',
  1551. ' PVarRec = ^TVarRec;',
  1552. ' TVarRec = record',
  1553. ' VType : byte;',
  1554. ' VJSValue: JSValue;',
  1555. ' vInteger: longint external name ''VJSValue'';',
  1556. ' vBoolean: boolean external name ''VJSValue'';',
  1557. ' end;',
  1558. ' TVarRecArray = array of TVarRec;',
  1559. 'function VarRecs: TVarRecArray; varargs;',
  1560. '']);
  1561. if supTypeInfo in Parts then
  1562. begin
  1563. Intf.AddStrings([
  1564. 'type',
  1565. ' TTypeKind = (',
  1566. ' tkUnknown, // 0',
  1567. ' tkInteger, // 1',
  1568. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1569. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1570. ' tkEnumeration, // 4',
  1571. ' tkSet, // 5',
  1572. ' tkDouble, // 6',
  1573. ' tkBool, // 7',
  1574. ' tkProcVar, // 8 function or procedure',
  1575. ' tkMethod, // 9 proc var of object',
  1576. ' tkArray, // 10 static array',
  1577. ' tkDynArray, // 11',
  1578. ' tkRecord, // 12',
  1579. ' tkClass, // 13',
  1580. ' tkClassRef, // 14',
  1581. ' tkPointer, // 15',
  1582. ' tkJSValue, // 16',
  1583. ' tkRefToProcVar, // 17 variable of procedure type',
  1584. ' tkInterface, // 18',
  1585. ' //tkObject,',
  1586. ' //tkSString,tkLString,tkAString,tkWString,',
  1587. ' //tkVariant,',
  1588. ' //tkWChar,',
  1589. ' //tkInt64,',
  1590. ' //tkQWord,',
  1591. ' //tkInterfaceRaw,',
  1592. ' //tkUString,tkUChar,',
  1593. ' tkHelper, // 19',
  1594. ' //tkFile,',
  1595. ' tkExtClass // 20',
  1596. ' );',
  1597. ' TTypeKinds = set of TTypeKind;',
  1598. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1599. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1600. ' end;',
  1601. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1602. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1603. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1604. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1605. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1606. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1607. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1608. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1609. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1610. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1611. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1612. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1613. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1614. '']);
  1615. end;
  1616. Intf.Add('var');
  1617. Intf.Add(' ExitCode: Longint = 0;');
  1618. // unit implementation
  1619. Impl:=TStringList.Create;
  1620. if supTObject in Parts then
  1621. Impl.AddStrings([
  1622. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1623. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1624. 'asm',
  1625. 'end;',
  1626. 'constructor TObject.Create; begin end;',
  1627. 'destructor TObject.Destroy; begin end;',
  1628. 'class function TObject.ClassType: TClass; assembler;',
  1629. 'asm',
  1630. 'end;',
  1631. 'class function TObject.ClassName: String; assembler;',
  1632. 'asm',
  1633. 'end;',
  1634. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1635. 'begin',
  1636. ' Result:=SameText(Name,ClassName);',
  1637. 'end;',
  1638. 'class function TObject.ClassParent: TClass; assembler;',
  1639. 'asm',
  1640. 'end;',
  1641. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1642. 'asm',
  1643. 'end;',
  1644. 'class function TObject.UnitName: String; assembler;',
  1645. 'asm',
  1646. 'end;',
  1647. 'procedure TObject.AfterConstruction; begin end;',
  1648. 'procedure TObject.BeforeDestruction; begin end;',
  1649. 'function TObject.Equals(Obj: TObject): boolean;',
  1650. 'begin',
  1651. ' Result:=Obj=Self;',
  1652. 'end;',
  1653. 'function TObject.ToString: String;',
  1654. 'begin',
  1655. ' Result:=ClassName;',
  1656. 'end;'
  1657. ]);
  1658. if supTInterfacedObject in Parts then
  1659. Impl.AddStrings([
  1660. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1661. //'begin',
  1662. //'end;',
  1663. 'function TInterfacedObject._AddRef: Integer;',
  1664. 'begin',
  1665. 'end;',
  1666. 'function TInterfacedObject._Release: Integer;',
  1667. 'begin',
  1668. 'end;',
  1669. '']);
  1670. if supTVarRec in Parts then
  1671. Impl.AddStrings([
  1672. 'function VarRecs: TVarRecArray; varargs;',
  1673. 'var',
  1674. ' v: PVarRec;',
  1675. 'begin',
  1676. ' v^.VType:=1;',
  1677. ' v^.VJSValue:=2;',
  1678. 'end;',
  1679. '']);
  1680. try
  1681. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1682. finally
  1683. Intf.Free;
  1684. Impl.Free;
  1685. end;
  1686. end;
  1687. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1688. SystemUnitParts: TSystemUnitParts);
  1689. begin
  1690. if NeedSystemUnit then
  1691. AddSystemUnit(SystemUnitParts)
  1692. else
  1693. Parser.ImplicitUses.Clear;
  1694. Add('program '+ExtractFileUnitName(Filename)+';');
  1695. Add('');
  1696. end;
  1697. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1698. SystemUnitParts: TSystemUnitParts);
  1699. begin
  1700. if NeedSystemUnit then
  1701. AddSystemUnit(SystemUnitParts)
  1702. else
  1703. Parser.ImplicitUses.Clear;
  1704. Add('unit Test1;');
  1705. Add('');
  1706. end;
  1707. procedure TCustomTestModule.ConvertModule;
  1708. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1709. out UsesLit: TJSArrayLiteral);
  1710. var
  1711. i: Integer;
  1712. Item: TJSElement;
  1713. Lit: TJSLiteral;
  1714. begin
  1715. UsesLit:=nil;
  1716. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1717. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1718. exit; // null is ok
  1719. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1720. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1721. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1722. begin
  1723. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1724. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1725. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1726. Lit:=TJSLiteral(Item);
  1727. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1728. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1729. end;
  1730. end;
  1731. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1732. out Src: TJSSourceElements);
  1733. var
  1734. FunDecl: TJSFunctionDeclarationStatement;
  1735. FunDef: TJSFuncDef;
  1736. FunBody: TJSFunctionBody;
  1737. begin
  1738. Src:=nil;
  1739. AssertNotNull(ParamName,Arg.Expr);
  1740. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1741. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1742. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1743. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1744. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1745. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1746. AssertNotNull(ParamName+' body',FunDef.Body);
  1747. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1748. FunBody:=FunDef.Body as TJSFunctionBody;
  1749. AssertNotNull(ParamName+' body.A',FunBody.A);
  1750. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1751. Src:=FunBody.A as TJSSourceElements;
  1752. end;
  1753. var
  1754. ModuleNameExpr: TJSLiteral;
  1755. InitFunction: TJSFunctionDeclarationStatement;
  1756. InitAssign: TJSSimpleAssignStatement;
  1757. InitName: String;
  1758. LastNode: TJSElement;
  1759. Arg: TJSArrayLiteralElement;
  1760. begin
  1761. if SkipTests then exit;
  1762. try
  1763. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1764. except
  1765. on E: Exception do
  1766. HandleException(E);
  1767. end;
  1768. if SkipTests then exit;
  1769. if ExpectedErrorClass<>nil then
  1770. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1771. FJSSource:=TStringList.Create;
  1772. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1773. {$IFDEF VerbosePas2JS}
  1774. writeln('TTestModule.ConvertModule JS:');
  1775. write(FJSSource.Text);
  1776. {$ENDIF}
  1777. // rtl.module(...
  1778. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1779. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1780. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1781. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1782. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1783. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1784. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1785. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1786. // parameter 'unitname'
  1787. if JSModuleCallArgs.Elements.Count<1 then
  1788. Fail('rtl.module first param unit missing');
  1789. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1790. AssertNotNull('module name param',Arg.Expr);
  1791. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1792. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1793. if Module is TPasProgram then
  1794. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1795. else
  1796. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1797. // main uses section
  1798. if JSModuleCallArgs.Elements.Count<2 then
  1799. Fail('rtl.module second param main uses missing');
  1800. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1801. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1802. // program/library/interface function()
  1803. if JSModuleCallArgs.Elements.Count<3 then
  1804. Fail('rtl.module third param intf-function missing');
  1805. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1806. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1807. // search for $mod.$init or $mod.$main - the last statement
  1808. if Module is TPasProgram then
  1809. begin
  1810. InitName:='$main';
  1811. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1812. end
  1813. else
  1814. InitName:='$init';
  1815. FJSInitBody:=nil;
  1816. if JSModuleSrc.Statements.Count>0 then
  1817. begin
  1818. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1819. if LastNode is TJSSimpleAssignStatement then
  1820. begin
  1821. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1822. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1823. begin
  1824. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1825. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1826. end
  1827. else if Module is TPasProgram then
  1828. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1829. end;
  1830. end;
  1831. // optional: implementation uses section
  1832. if JSModuleCallArgs.Elements.Count<4 then
  1833. exit;
  1834. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1835. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1836. // optional: implementation function()
  1837. if JSModuleCallArgs.Elements.Count<5 then
  1838. exit;
  1839. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1840. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1841. end;
  1842. procedure TCustomTestModule.ConvertProgram;
  1843. begin
  1844. Add('end.');
  1845. ParseProgram;
  1846. ConvertModule;
  1847. end;
  1848. procedure TCustomTestModule.ConvertUnit;
  1849. begin
  1850. Add('end.');
  1851. ParseUnit;
  1852. ConvertModule;
  1853. end;
  1854. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1855. begin
  1856. Result:=tcmodules.JSToStr(El);
  1857. end;
  1858. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1859. DottedName: string);
  1860. begin
  1861. if DottedName='' then
  1862. begin
  1863. AssertNull(Msg,El);
  1864. end
  1865. else
  1866. begin
  1867. AssertNotNull(Msg,El);
  1868. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1869. end;
  1870. end;
  1871. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1872. begin
  1873. if El=nil then
  1874. Result:=''
  1875. else if El is TJSPrimaryExpressionIdent then
  1876. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1877. else if El is TJSDotMemberExpression then
  1878. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1879. else
  1880. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1881. end;
  1882. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1883. InitStatements: string; ImplStatements: string);
  1884. var
  1885. ActualSrc, ExpectedSrc, InitName: String;
  1886. begin
  1887. ActualSrc:=JSToStr(JSModuleSrc);
  1888. ExpectedSrc:=
  1889. 'var $mod = this;'+LineEnding
  1890. +Statements;
  1891. if coUseStrict in Converter.Options then
  1892. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1893. if Module is TPasProgram then
  1894. InitName:='$main'
  1895. else
  1896. InitName:='$init';
  1897. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1898. ExpectedSrc:=ExpectedSrc+LineEnding
  1899. +'$mod.'+InitName+' = function () {'+LineEnding
  1900. +InitStatements
  1901. +'};'+LineEnding;
  1902. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1903. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1904. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1905. if (JSImplementationSrc<>nil) then
  1906. begin
  1907. ActualSrc:=JSToStr(JSImplementationSrc);
  1908. ExpectedSrc:=
  1909. 'var $mod = this;'+LineEnding
  1910. +'var $impl = $mod.$impl;'+LineEnding
  1911. +ImplStatements;
  1912. end
  1913. else
  1914. begin
  1915. ActualSrc:='';
  1916. ExpectedSrc:=ImplStatements;
  1917. end;
  1918. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1919. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1920. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1921. end;
  1922. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1923. // search diff, ignore changes in spaces
  1924. var
  1925. s: string;
  1926. begin
  1927. if CheckSrcDiff(Expected,Actual,s) then exit;
  1928. Fail(Msg+': '+s);
  1929. end;
  1930. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1931. var
  1932. aResolver: TTestEnginePasResolver;
  1933. aConverter: TPasToJSConverter;
  1934. aJSModule: TJSSourceElements;
  1935. ActualSrc: String;
  1936. begin
  1937. aResolver:=GetResolver(Filename);
  1938. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1939. {$IFDEF VerbosePas2JS}
  1940. writeln('CheckUnit '+Filename+' converting ...');
  1941. {$ENDIF}
  1942. aConverter:=CreateConverter;
  1943. aJSModule:=nil;
  1944. try
  1945. try
  1946. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1947. except
  1948. on E: Exception do
  1949. HandleException(E);
  1950. end;
  1951. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1952. {$IFDEF VerbosePas2JS}
  1953. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1954. write(aResolver.Source);
  1955. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1956. write(ActualSrc);
  1957. {$ENDIF}
  1958. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1959. finally
  1960. aJSModule.Free;
  1961. aConverter.Free;
  1962. end;
  1963. end;
  1964. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1965. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1966. var
  1967. i: Integer;
  1968. Item: TTestHintMessage;
  1969. Expected,Actual: string;
  1970. begin
  1971. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1972. for i:=0 to MsgCount-1 do
  1973. begin
  1974. Item:=Msgs[i];
  1975. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1976. if (Marker<>nil) then
  1977. begin
  1978. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1979. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1980. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1981. end;
  1982. // found
  1983. FHintMsgsGood.Add(Item);
  1984. str(Item.MsgType,Actual);
  1985. str(MsgType,Expected);
  1986. AssertEquals('MsgType',Expected,Actual);
  1987. exit;
  1988. end;
  1989. // needed message missing -> show emitted messages
  1990. WriteSources('',0,0);
  1991. for i:=0 to MsgCount-1 do
  1992. begin
  1993. Item:=Msgs[i];
  1994. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1995. ' ('+IntToStr(Item.MsgNumber),')');
  1996. if Marker<>nil then
  1997. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1998. writeln(' {',Item.Msg,'}');
  1999. end;
  2000. str(MsgType,Expected);
  2001. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2002. if Marker<>nil then
  2003. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2004. Actual:=Actual+' '+Msg;
  2005. Fail(Actual);
  2006. end;
  2007. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2008. );
  2009. var
  2010. i: Integer;
  2011. s, Txt: String;
  2012. Msg: TTestHintMessage;
  2013. begin
  2014. for i:=0 to MsgCount-1 do
  2015. begin
  2016. Msg:=Msgs[i];
  2017. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2018. s:='';
  2019. str(Msg.MsgType,s);
  2020. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2021. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2022. if WithSourcePos then
  2023. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2024. Txt:=Txt+' {'+Msg.Msg+'}';
  2025. Fail(Txt);
  2026. end;
  2027. end;
  2028. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2029. MsgNumber: integer);
  2030. begin
  2031. ExpectedErrorClass:=EScannerError;
  2032. ExpectedErrorMsg:=Msg;
  2033. ExpectedErrorNumber:=MsgNumber;
  2034. end;
  2035. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2036. MsgNumber: integer);
  2037. begin
  2038. ExpectedErrorClass:=EParserError;
  2039. ExpectedErrorMsg:=Msg;
  2040. ExpectedErrorNumber:=MsgNumber;
  2041. end;
  2042. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2043. MsgNumber: integer);
  2044. begin
  2045. ExpectedErrorClass:=EPasResolve;
  2046. ExpectedErrorMsg:=Msg;
  2047. ExpectedErrorNumber:=MsgNumber;
  2048. end;
  2049. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2050. MsgNumber: integer);
  2051. begin
  2052. ExpectedErrorClass:=EPas2JS;
  2053. ExpectedErrorMsg:=Msg;
  2054. ExpectedErrorNumber:=MsgNumber;
  2055. end;
  2056. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2057. var
  2058. MsgNumber: Integer;
  2059. Msg: String;
  2060. begin
  2061. Result:=false;
  2062. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2063. Msg:=E.Message;
  2064. if E is EPas2JS then
  2065. MsgNumber:=EPas2JS(E).MsgNumber
  2066. else if E is EPasResolve then
  2067. MsgNumber:=EPasResolve(E).MsgNumber
  2068. else if E is EParserError then
  2069. MsgNumber:=Parser.LastMsgNumber
  2070. else if E is EScannerError then
  2071. begin
  2072. MsgNumber:=Scanner.LastMsgNumber;
  2073. Msg:=Scanner.LastMsg;
  2074. end
  2075. else
  2076. MsgNumber:=0;
  2077. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2078. if Result then
  2079. SkipTests:=true;
  2080. end;
  2081. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2082. begin
  2083. if IsErrorExpected(E) then exit;
  2084. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2085. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2086. +' '+Scanner.CurFilename
  2087. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2088. FailException(E);
  2089. end;
  2090. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2091. begin
  2092. if IsErrorExpected(E) then exit;
  2093. WriteSources(E.Filename,E.Row,E.Column);
  2094. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2095. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2096. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2097. );
  2098. FailException(E);
  2099. end;
  2100. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2101. var
  2102. P: TPasSourcePos;
  2103. begin
  2104. if IsErrorExpected(E) then exit;
  2105. P:=E.SourcePos;
  2106. WriteSources(P.FileName,P.Row,P.Column);
  2107. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2108. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2109. FailException(E);
  2110. end;
  2111. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2112. var
  2113. Row, Col: integer;
  2114. begin
  2115. if IsErrorExpected(E) then exit;
  2116. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2117. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2118. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2119. +' '+E.PasElement.SourceFilename
  2120. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2121. FailException(E);
  2122. end;
  2123. procedure TCustomTestModule.HandleException(E: Exception);
  2124. begin
  2125. if E is EScannerError then
  2126. HandleScannerError(EScannerError(E))
  2127. else if E is EParserError then
  2128. HandleParserError(EParserError(E))
  2129. else if E is EPasResolve then
  2130. HandlePasResolveError(EPasResolve(E))
  2131. else if E is EPas2JS then
  2132. HandlePas2JSError(EPas2JS(E))
  2133. else
  2134. begin
  2135. if IsErrorExpected(E) then exit;
  2136. if not (E is EAssertionFailedError) then
  2137. begin
  2138. WriteSources('',0,0);
  2139. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2140. end;
  2141. FailException(E);
  2142. end;
  2143. end;
  2144. procedure TCustomTestModule.FailException(E: Exception);
  2145. var
  2146. MsgNumber: Integer;
  2147. begin
  2148. if ExpectedErrorClass<>nil then
  2149. begin
  2150. if FExpectedErrorClass=E.ClassType then
  2151. begin
  2152. if E is EPas2JS then
  2153. MsgNumber:=EPas2JS(E).MsgNumber
  2154. else if E is EPasResolve then
  2155. MsgNumber:=EPasResolve(E).MsgNumber
  2156. else if E is EParserError then
  2157. MsgNumber:=Parser.LastMsgNumber
  2158. else if E is EScannerError then
  2159. MsgNumber:=Scanner.LastMsgNumber
  2160. else
  2161. MsgNumber:=0;
  2162. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2163. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2164. ExpectedErrorNumber,MsgNumber);
  2165. end else begin
  2166. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2167. end;
  2168. end;
  2169. Fail(E.Message);
  2170. end;
  2171. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2172. aCol: integer);
  2173. var
  2174. IsSrc: Boolean;
  2175. i, j: Integer;
  2176. SrcLines: TStringList;
  2177. Line: string;
  2178. aModule: TTestEnginePasResolver;
  2179. begin
  2180. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2181. for i:=0 to ResolverCount-1 do
  2182. begin
  2183. aModule:=Resolvers[i];
  2184. SrcLines:=TStringList.Create;
  2185. try
  2186. SrcLines.Text:=aModule.Source;
  2187. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2188. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2189. for j:=1 to SrcLines.Count do
  2190. begin
  2191. Line:=SrcLines[j-1];
  2192. if IsSrc and (j=aRow) then
  2193. begin
  2194. write('*');
  2195. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2196. end;
  2197. writeln(Format('%:4d: ',[j]),Line);
  2198. end;
  2199. finally
  2200. SrcLines.Free;
  2201. end;
  2202. end;
  2203. end;
  2204. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2205. var
  2206. i: Integer;
  2207. begin
  2208. for i:=0 to ResolverCount-1 do
  2209. if Filename=Resolvers[i].Filename then exit(i);
  2210. Result:=-1;
  2211. end;
  2212. function TCustomTestModule.GetResolver(const Filename: string
  2213. ): TTestEnginePasResolver;
  2214. var
  2215. i: Integer;
  2216. begin
  2217. i:=IndexOfResolver(Filename);
  2218. if i<0 then exit(nil);
  2219. Result:=Resolvers[i];
  2220. end;
  2221. function TCustomTestModule.GetDefaultNamespace: string;
  2222. var
  2223. C: TClass;
  2224. begin
  2225. Result:='';
  2226. if FModule=nil then exit;
  2227. C:=FModule.ClassType;
  2228. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2229. Result:=Engine.DefaultNameSpace;
  2230. end;
  2231. constructor TCustomTestModule.Create;
  2232. begin
  2233. inherited Create;
  2234. FHintMsgs:=TObjectList.Create(true);
  2235. FHintMsgsGood:=TFPList.Create;
  2236. end;
  2237. destructor TCustomTestModule.Destroy;
  2238. begin
  2239. FreeAndNil(FHintMsgs);
  2240. FreeAndNil(FHintMsgsGood);
  2241. inherited Destroy;
  2242. end;
  2243. { TTestModule }
  2244. procedure TTestModule.TestReservedWords;
  2245. var
  2246. i: integer;
  2247. begin
  2248. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2249. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2250. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2251. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2252. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2253. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2254. end;
  2255. procedure TTestModule.TestEmptyProgram;
  2256. begin
  2257. StartProgram(false);
  2258. Add('begin');
  2259. ConvertProgram;
  2260. CheckSource('TestEmptyProgram','','');
  2261. end;
  2262. procedure TTestModule.TestEmptyProgramUseStrict;
  2263. begin
  2264. Converter.Options:=Converter.Options+[coUseStrict];
  2265. StartProgram(false);
  2266. Add('begin');
  2267. ConvertProgram;
  2268. CheckSource('TestEmptyProgramUseStrict','','');
  2269. end;
  2270. procedure TTestModule.TestEmptyUnit;
  2271. begin
  2272. StartUnit(false);
  2273. Add('interface');
  2274. Add('implementation');
  2275. ConvertUnit;
  2276. CheckSource('TestEmptyUnit',
  2277. LinesToStr([
  2278. ]),
  2279. '');
  2280. end;
  2281. procedure TTestModule.TestEmptyUnitUseStrict;
  2282. begin
  2283. Converter.Options:=Converter.Options+[coUseStrict];
  2284. StartUnit(false);
  2285. Add('interface');
  2286. Add('implementation');
  2287. ConvertUnit;
  2288. CheckSource('TestEmptyUnitUseStrict',
  2289. LinesToStr([
  2290. ''
  2291. ]),
  2292. '');
  2293. end;
  2294. procedure TTestModule.TestDottedUnitNames;
  2295. begin
  2296. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2297. LinesToStr([
  2298. 'var iV: longint;'
  2299. ]),
  2300. '');
  2301. FFilename:='ns1.test1.pp';
  2302. StartProgram(true);
  2303. Add('uses unIt2;');
  2304. Add('var');
  2305. Add(' i: longint;');
  2306. Add('begin');
  2307. Add(' i:=iv;');
  2308. Add(' i:=uNit2.iv;');
  2309. Add(' i:=Ns1.TEst1.i;');
  2310. ConvertProgram;
  2311. CheckSource('TestDottedUnitNames',
  2312. LinesToStr([
  2313. 'this.i = 0;',
  2314. '']),
  2315. LinesToStr([ // this.$init
  2316. '$mod.i = pas["NS1.Unit2"].iV;',
  2317. '$mod.i = pas["NS1.Unit2"].iV;',
  2318. '$mod.i = $mod.i;',
  2319. '']) );
  2320. end;
  2321. procedure TTestModule.TestDottedUnitNameImpl;
  2322. begin
  2323. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2324. LinesToStr([
  2325. 'type',
  2326. ' TObject = class end;',
  2327. ' TTestA = class',
  2328. ' end;'
  2329. ]),
  2330. LinesToStr(['uses TEST.UnitB;'])
  2331. );
  2332. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2333. LinesToStr([
  2334. 'uses TEST.UnitA;',
  2335. 'type TTestB = class(TTestA);'
  2336. ]),
  2337. ''
  2338. );
  2339. StartProgram(true);
  2340. Add('uses TEST.UnitA;');
  2341. Add('begin');
  2342. ConvertProgram;
  2343. CheckSource('TestDottedUnitNameImpl',
  2344. LinesToStr([
  2345. '']),
  2346. LinesToStr([ // this.$init
  2347. '']) );
  2348. CheckUnit('TEST.UnitA.pas',
  2349. LinesToStr([
  2350. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2351. ' var $mod = this;',
  2352. ' rtl.createClass($mod, "TObject", null, function () {',
  2353. ' this.$init = function () {',
  2354. ' };',
  2355. ' this.$final = function () {',
  2356. ' };',
  2357. ' });',
  2358. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2359. ' });',
  2360. '}, ["TEST.UnitB"]);'
  2361. ]));
  2362. CheckUnit('TEST.UnitB.pas',
  2363. LinesToStr([
  2364. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2365. ' var $mod = this;',
  2366. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2367. ' });',
  2368. '});'
  2369. ]));
  2370. end;
  2371. procedure TTestModule.TestDottedUnitExpr;
  2372. begin
  2373. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2374. LinesToStr([
  2375. 'procedure DoIt;'
  2376. ]),
  2377. 'procedure DoIt; begin end;');
  2378. FFilename:='Ns1.SubNs1.Test1.pp';
  2379. StartProgram(true);
  2380. Add('uses Ns2.sUbnS2.unIt2;');
  2381. Add('var');
  2382. Add(' i: longint;');
  2383. Add('begin');
  2384. Add(' ns2.subns2.unit2.doit;');
  2385. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2386. ConvertProgram;
  2387. CheckSource('TestDottedUnitExpr',
  2388. LinesToStr([
  2389. 'this.i = 0;',
  2390. '']),
  2391. LinesToStr([ // this.$init
  2392. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2393. '$mod.i = $mod.i;',
  2394. '']) );
  2395. end;
  2396. procedure TTestModule.Test_ModeFPCFail;
  2397. begin
  2398. StartProgram(false);
  2399. Add('{$mode FPC}');
  2400. Add('begin');
  2401. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2402. ConvertProgram;
  2403. end;
  2404. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2405. begin
  2406. StartProgram(false);
  2407. Add('{$modeswitch cblocks-}');
  2408. Add('begin');
  2409. ConvertProgram;
  2410. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2411. CheckResolverUnexpectedHints();
  2412. end;
  2413. procedure TTestModule.TestUnit_UseSystem;
  2414. begin
  2415. StartUnit(true);
  2416. Add([
  2417. 'interface',
  2418. 'var i: integer;',
  2419. 'implementation']);
  2420. ConvertUnit;
  2421. CheckSource('TestUnit_UseSystem',
  2422. LinesToStr([
  2423. 'this.i = 0;',
  2424. '']),
  2425. LinesToStr([
  2426. '']) );
  2427. end;
  2428. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2429. begin
  2430. AddModuleWithIntfImplSrc('unit1.pp',
  2431. LinesToStr([
  2432. 'type number = longint;']),
  2433. LinesToStr([
  2434. 'uses test1;',
  2435. 'procedure DoIt;',
  2436. 'begin',
  2437. ' i:=3;',
  2438. 'end;']));
  2439. StartUnit(true);
  2440. Add([
  2441. 'interface',
  2442. 'uses unit1;',
  2443. 'var i: number;',
  2444. 'implementation']);
  2445. ConvertUnit;
  2446. CheckSource('TestUnit_Intf1Impl2Intf1',
  2447. LinesToStr([
  2448. 'this.i = 0;',
  2449. '']),
  2450. LinesToStr([
  2451. '']) );
  2452. end;
  2453. procedure TTestModule.TestIncludeVersion;
  2454. begin
  2455. StartProgram(false);
  2456. Add([
  2457. 'var',
  2458. ' s: string;',
  2459. ' i: word;',
  2460. 'begin',
  2461. ' s:={$I %line%};',
  2462. ' i:={$I %linenum%};',
  2463. ' s:={$I %currentroutine%};',
  2464. ' s:={$I %pas2jsversion%};',
  2465. ' s:={$I %pas2jstarget%};',
  2466. ' s:={$I %pas2jstargetos%};',
  2467. ' s:={$I %pas2jstargetcpu%};',
  2468. ' s:={$I %file%};',
  2469. '']);
  2470. ConvertProgram;
  2471. CheckSource('TestIncludeVersion',
  2472. LinesToStr([
  2473. 'this.s="";',
  2474. 'this.i = 0;']),
  2475. LinesToStr([
  2476. '$mod.s = "7";',
  2477. '$mod.i = 8;',
  2478. '$mod.s = "<anonymous>";',
  2479. '$mod.s = "Comp.Ver.tcmodules";',
  2480. '$mod.s = "Browser";',
  2481. '$mod.s = "Browser";',
  2482. '$mod.s = "ECMAScript5";',
  2483. '$mod.s = "test1.pp";',
  2484. '']));
  2485. end;
  2486. procedure TTestModule.TestVarInt;
  2487. begin
  2488. StartProgram(false);
  2489. Add('var MyI: longint;');
  2490. Add('begin');
  2491. ConvertProgram;
  2492. CheckSource('TestVarInt','this.MyI=0;','');
  2493. end;
  2494. procedure TTestModule.TestVarBaseTypes;
  2495. begin
  2496. StartProgram(false);
  2497. Add('var');
  2498. Add(' i: longint;');
  2499. Add(' s: string;');
  2500. Add(' c: char;');
  2501. Add(' b: boolean;');
  2502. Add(' d: double;');
  2503. Add(' i2: longint = 3;');
  2504. Add(' s2: string = ''foo'';');
  2505. Add(' c2: char = ''4'';');
  2506. Add(' b2: boolean = true;');
  2507. Add(' d2: double = 5.6;');
  2508. Add(' i3: longint = $707;');
  2509. Add(' i4: nativeint = 9007199254740991;');
  2510. Add(' i5: nativeint = -9007199254740991-1;');
  2511. Add(' i6: nativeint = $fffffffffffff;');
  2512. Add(' i7: nativeint = -$fffffffffffff-1;');
  2513. Add(' i8: byte = 00;');
  2514. Add(' u8: nativeuint = $fffffffffffff;');
  2515. Add(' u9: nativeuint = $0000000000000;');
  2516. Add(' u10: nativeuint = $00ff00;');
  2517. Add('begin');
  2518. ConvertProgram;
  2519. CheckSource('TestVarBaseTypes',
  2520. LinesToStr([
  2521. 'this.i = 0;',
  2522. 'this.s = "";',
  2523. 'this.c = "";',
  2524. 'this.b = false;',
  2525. 'this.d = 0.0;',
  2526. 'this.i2 = 3;',
  2527. 'this.s2 = "foo";',
  2528. 'this.c2 = "4";',
  2529. 'this.b2 = true;',
  2530. 'this.d2 = 5.6;',
  2531. 'this.i3 = 0x707;',
  2532. 'this.i4 = 9007199254740991;',
  2533. 'this.i5 = -9007199254740991-1;',
  2534. 'this.i6 = 0xfffffffffffff;',
  2535. 'this.i7 =-0xfffffffffffff-1;',
  2536. 'this.i8 = 0;',
  2537. 'this.u8 = 0xfffffffffffff;',
  2538. 'this.u9 = 0x0;',
  2539. 'this.u10 = 0xff00;'
  2540. ]),
  2541. '');
  2542. end;
  2543. procedure TTestModule.TestBaseTypeSingleFail;
  2544. begin
  2545. StartProgram(false);
  2546. Add('var s: single;');
  2547. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2548. ConvertProgram;
  2549. end;
  2550. procedure TTestModule.TestBaseTypeExtendedFail;
  2551. begin
  2552. StartProgram(false);
  2553. Add('var e: extended;');
  2554. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2555. ConvertProgram;
  2556. end;
  2557. procedure TTestModule.TestConstBaseTypes;
  2558. begin
  2559. StartProgram(false);
  2560. Add('const');
  2561. Add(' i: longint = 3;');
  2562. Add(' s: string = ''foo'';');
  2563. Add(' c: char = ''4'';');
  2564. Add(' b: boolean = true;');
  2565. Add(' d: double = 5.6;');
  2566. Add(' e = low(word);');
  2567. Add(' f = high(word);');
  2568. Add('begin');
  2569. ConvertProgram;
  2570. CheckSource('TestVarBaseTypes',
  2571. LinesToStr([
  2572. 'this.i=3;',
  2573. 'this.s="foo";',
  2574. 'this.c="4";',
  2575. 'this.b=true;',
  2576. 'this.d=5.6;',
  2577. 'this.e = 0;',
  2578. 'this.f = 65535;'
  2579. ]),
  2580. '');
  2581. end;
  2582. procedure TTestModule.TestAliasTypeRef;
  2583. begin
  2584. StartProgram(false);
  2585. Add('type');
  2586. Add(' a=longint;');
  2587. Add(' b=a;');
  2588. Add('var');
  2589. Add(' c: A;');
  2590. Add(' d: B;');
  2591. Add('begin');
  2592. ConvertProgram;
  2593. CheckSource('TestAliasTypeRef',
  2594. LinesToStr([ // statements
  2595. 'this.c = 0;',
  2596. 'this.d = 0;'
  2597. ]),
  2598. LinesToStr([ // this.$main
  2599. ''
  2600. ]));
  2601. end;
  2602. procedure TTestModule.TestTypeCast_BaseTypes;
  2603. begin
  2604. StartProgram(false);
  2605. Add([
  2606. 'var',
  2607. ' i: longint;',
  2608. ' b: boolean;',
  2609. ' d: double;',
  2610. ' s: string;',
  2611. ' c: char;',
  2612. 'begin',
  2613. ' i:=longint(i);',
  2614. ' i:=longint(b);',
  2615. ' b:=boolean(b);',
  2616. ' b:=boolean(i);',
  2617. ' d:=double(d);',
  2618. ' d:=double(i);',
  2619. ' s:=string(s);',
  2620. ' s:=string(c);',
  2621. ' c:=char(c);',
  2622. ' c:=char(i);',
  2623. ' c:=char(65);',
  2624. ' c:=char(#10);',
  2625. ' c:=char(#$E000);',
  2626. '']);
  2627. ConvertProgram;
  2628. CheckSource('TestAliasTypeRef',
  2629. LinesToStr([ // statements
  2630. 'this.i = 0;',
  2631. 'this.b = false;',
  2632. 'this.d = 0.0;',
  2633. 'this.s = "";',
  2634. 'this.c = "";',
  2635. '']),
  2636. LinesToStr([ // this.$main
  2637. '$mod.i = $mod.i;',
  2638. '$mod.i = ($mod.b ? 1 : 0);',
  2639. '$mod.b = $mod.b;',
  2640. '$mod.b = $mod.i != 0;',
  2641. '$mod.d = $mod.d;',
  2642. '$mod.d = $mod.i;',
  2643. '$mod.s = $mod.s;',
  2644. '$mod.s = $mod.c;',
  2645. '$mod.c = $mod.c;',
  2646. '$mod.c = String.fromCharCode($mod.i);',
  2647. '$mod.c = "A";',
  2648. '$mod.c = "\n";',
  2649. '$mod.c = "";',
  2650. '']));
  2651. end;
  2652. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2653. begin
  2654. StartProgram(false);
  2655. Add('type');
  2656. Add(' integer = longint;');
  2657. Add(' TYesNo = boolean;');
  2658. Add(' TFloat = double;');
  2659. Add(' TCaption = string;');
  2660. Add(' TChar = char;');
  2661. Add('var');
  2662. Add(' i: integer;');
  2663. Add(' b: TYesNo;');
  2664. Add(' d: TFloat;');
  2665. Add(' s: TCaption;');
  2666. Add(' c: TChar;');
  2667. Add('begin');
  2668. Add(' i:=integer(i);');
  2669. Add(' i:=integer(b);');
  2670. Add(' b:=TYesNo(b);');
  2671. Add(' b:=TYesNo(i);');
  2672. Add(' d:=TFloat(d);');
  2673. Add(' d:=TFloat(i);');
  2674. Add(' s:=TCaption(s);');
  2675. Add(' s:=TCaption(c);');
  2676. Add(' c:=TChar(c);');
  2677. ConvertProgram;
  2678. CheckSource('TestAliasTypeRef',
  2679. LinesToStr([ // statements
  2680. 'this.i = 0;',
  2681. 'this.b = false;',
  2682. 'this.d = 0.0;',
  2683. 'this.s = "";',
  2684. 'this.c = "";',
  2685. '']),
  2686. LinesToStr([ // this.$main
  2687. '$mod.i = $mod.i;',
  2688. '$mod.i = ($mod.b ? 1 : 0);',
  2689. '$mod.b = $mod.b;',
  2690. '$mod.b = $mod.i != 0;',
  2691. '$mod.d = $mod.d;',
  2692. '$mod.d = $mod.i;',
  2693. '$mod.s = $mod.s;',
  2694. '$mod.s = $mod.c;',
  2695. '$mod.c = $mod.c;',
  2696. '']));
  2697. end;
  2698. procedure TTestModule.TestEmptyProc;
  2699. begin
  2700. StartProgram(false);
  2701. Add('procedure Test;');
  2702. Add('begin');
  2703. Add('end;');
  2704. Add('begin');
  2705. ConvertProgram;
  2706. CheckSource('TestEmptyProc',
  2707. LinesToStr([ // statements
  2708. 'this.Test = function () {',
  2709. '};'
  2710. ]),
  2711. LinesToStr([ // this.$main
  2712. ''
  2713. ]));
  2714. end;
  2715. procedure TTestModule.TestProcOneParam;
  2716. begin
  2717. StartProgram(false);
  2718. Add('procedure ProcA(i: longint);');
  2719. Add('begin');
  2720. Add('end;');
  2721. Add('begin');
  2722. Add(' PROCA(3);');
  2723. ConvertProgram;
  2724. CheckSource('TestProcOneParam',
  2725. LinesToStr([ // statements
  2726. 'this.ProcA = function (i) {',
  2727. '};'
  2728. ]),
  2729. LinesToStr([ // this.$main
  2730. '$mod.ProcA(3);'
  2731. ]));
  2732. end;
  2733. procedure TTestModule.TestFunctionWithoutParams;
  2734. begin
  2735. StartProgram(false);
  2736. Add('function FuncA: longint;');
  2737. Add('begin');
  2738. Add('end;');
  2739. Add('var i: longint;');
  2740. Add('begin');
  2741. Add(' I:=FUNCA();');
  2742. Add(' I:=FUNCA;');
  2743. Add(' FUNCA();');
  2744. Add(' FUNCA;');
  2745. ConvertProgram;
  2746. CheckSource('TestProcWithoutParams',
  2747. LinesToStr([ // statements
  2748. 'this.FuncA = function () {',
  2749. ' var Result = 0;',
  2750. ' return Result;',
  2751. '};',
  2752. 'this.i=0;'
  2753. ]),
  2754. LinesToStr([ // this.$main
  2755. '$mod.i=$mod.FuncA();',
  2756. '$mod.i=$mod.FuncA();',
  2757. '$mod.FuncA();',
  2758. '$mod.FuncA();'
  2759. ]));
  2760. end;
  2761. procedure TTestModule.TestProcedureWithoutParams;
  2762. begin
  2763. StartProgram(false);
  2764. Add('procedure ProcA;');
  2765. Add('begin');
  2766. Add('end;');
  2767. Add('begin');
  2768. Add(' PROCA();');
  2769. Add(' PROCA;');
  2770. ConvertProgram;
  2771. CheckSource('TestProcWithoutParams',
  2772. LinesToStr([ // statements
  2773. 'this.ProcA = function () {',
  2774. '};'
  2775. ]),
  2776. LinesToStr([ // this.$main
  2777. '$mod.ProcA();',
  2778. '$mod.ProcA();'
  2779. ]));
  2780. end;
  2781. procedure TTestModule.TestIncDec;
  2782. begin
  2783. StartProgram(false);
  2784. Add([
  2785. 'procedure DoIt(var i: longint);',
  2786. 'begin',
  2787. ' inc(i);',
  2788. ' inc(i,2);',
  2789. 'end;',
  2790. 'var',
  2791. ' Bar: longint;',
  2792. 'begin',
  2793. ' inc(bar);',
  2794. ' inc(bar,2);',
  2795. ' dec(bar);',
  2796. ' dec(bar,3);',
  2797. '']);
  2798. ConvertProgram;
  2799. CheckSource('TestIncDec',
  2800. LinesToStr([ // statements
  2801. 'this.DoIt = function (i) {',
  2802. ' i.set(i.get()+1);',
  2803. ' i.set(i.get()+2);',
  2804. '};',
  2805. 'this.Bar = 0;'
  2806. ]),
  2807. LinesToStr([ // this.$main
  2808. '$mod.Bar+=1;',
  2809. '$mod.Bar+=2;',
  2810. '$mod.Bar-=1;',
  2811. '$mod.Bar-=3;'
  2812. ]));
  2813. end;
  2814. procedure TTestModule.TestLoHiFpcMode;
  2815. begin
  2816. StartProgram(false);
  2817. Add([
  2818. '{$mode objfpc}',
  2819. 'const',
  2820. ' LoByte1 = Lo(Word($1234));',
  2821. ' HiByte1 = Hi(Word($1234));',
  2822. ' LoByte2 = Lo(SmallInt($1234));',
  2823. ' HiByte2 = Hi(SmallInt($1234));',
  2824. ' LoWord1 = Lo($1234CDEF);',
  2825. ' HiWord1 = Hi($1234CDEF);',
  2826. ' LoWord2 = Lo(-$1234CDEF);',
  2827. ' HiWord2 = Hi(-$1234CDEF);',
  2828. ' lo4:byte=lo(byte($34));',
  2829. ' hi4:byte=hi(byte($34));',
  2830. ' lo5:byte=lo(shortint(-$34));',
  2831. ' hi5:byte=hi(shortint(-$34));',
  2832. ' lo6:longword=lo($123456789ABCD);',
  2833. ' hi6:longword=hi($123456789ABCD);',
  2834. ' lo7:longword=lo(-$123456789ABCD);',
  2835. ' hi7:longword=hi(-$123456789ABCD);',
  2836. 'var',
  2837. ' b: Byte;',
  2838. ' ss: shortint;',
  2839. ' w: Word;',
  2840. ' si: SmallInt;',
  2841. ' lw: LongWord;',
  2842. ' li: LongInt;',
  2843. ' b2: Byte;',
  2844. ' ni: nativeint;',
  2845. 'begin',
  2846. ' w := $1234;',
  2847. ' ss := -$12;',
  2848. ' b := lo(ss);',
  2849. ' b := HI(ss);',
  2850. ' b := lo(w);',
  2851. ' b := HI(w);',
  2852. ' b2 := lo(b);',
  2853. ' b2 := hi(b);',
  2854. ' lw := $1234CDEF;',
  2855. ' w := lo(lw);',
  2856. ' w := hi(lw);',
  2857. ' ni := $123456789ABCD;',
  2858. ' lw := lo(ni);',
  2859. ' lw := hi(ni);',
  2860. '']);
  2861. ConvertProgram;
  2862. CheckSource('TestLoHiFpcMode',
  2863. LinesToStr([ // statements
  2864. 'this.LoByte1 = 0x1234 & 0xFF;',
  2865. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2866. 'this.LoByte2 = 0x1234 & 0xFF;',
  2867. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2868. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2869. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2870. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2871. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2872. 'this.lo4 = 0x34 & 0xF;',
  2873. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2874. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2875. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2876. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2877. 'this.hi6 = 74565 >>> 0;',
  2878. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2879. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2880. 'this.b = 0;',
  2881. 'this.ss = 0;',
  2882. 'this.w = 0;',
  2883. 'this.si = 0;',
  2884. 'this.lw = 0;',
  2885. 'this.li = 0;',
  2886. 'this.b2 = 0;',
  2887. 'this.ni = 0;',
  2888. '']),
  2889. LinesToStr([ // this.$main
  2890. '$mod.w = 0x1234;',
  2891. '$mod.ss = -0x12;',
  2892. '$mod.b = $mod.ss & 0xFF;',
  2893. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2894. '$mod.b = $mod.w & 0xFF;',
  2895. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2896. '$mod.b2 = $mod.b & 0xF;',
  2897. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2898. '$mod.lw = 0x1234CDEF;',
  2899. '$mod.w = $mod.lw & 0xFFFF;',
  2900. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2901. '$mod.ni = 0x123456789ABCD;',
  2902. '$mod.lw = $mod.ni >>> 0;',
  2903. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2904. '']));
  2905. end;
  2906. procedure TTestModule.TestLoHiDelphiMode;
  2907. begin
  2908. StartProgram(false);
  2909. Add([
  2910. '{$mode delphi}',
  2911. 'const',
  2912. ' LoByte1 = Lo(Word($1234));',
  2913. ' HiByte1 = Hi(Word($1234));',
  2914. ' LoByte2 = Lo(SmallInt($1234));',
  2915. ' HiByte2 = Hi(SmallInt($1234));',
  2916. ' LoByte3 = Lo($1234CDEF);',
  2917. ' HiByte3 = Hi($1234CDEF);',
  2918. ' LoByte4 = Lo(-$1234CDEF);',
  2919. ' HiByte4 = Hi(-$1234CDEF);',
  2920. 'var',
  2921. ' b: Byte;',
  2922. ' w: Word;',
  2923. ' si: SmallInt;',
  2924. ' lw: LongWord;',
  2925. ' li: LongInt;',
  2926. 'begin',
  2927. ' w := $1234;',
  2928. ' b := lo(w);',
  2929. ' b := HI(w);',
  2930. ' lw := $1234CDEF;',
  2931. ' b := lo(lw);',
  2932. ' b := hi(lw);',
  2933. '']);
  2934. ConvertProgram;
  2935. CheckSource('TestLoHiDelphiMode',
  2936. LinesToStr([ // statements
  2937. 'this.LoByte1 = 0x1234 & 0xFF;',
  2938. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2939. 'this.LoByte2 = 0x1234 & 0xFF;',
  2940. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2941. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2942. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2943. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2944. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2945. 'this.b = 0;',
  2946. 'this.w = 0;',
  2947. 'this.si = 0;',
  2948. 'this.lw = 0;',
  2949. 'this.li = 0;'
  2950. ]),
  2951. LinesToStr([ // this.$main
  2952. '$mod.w = 0x1234;',
  2953. '$mod.b = $mod.w & 0xFF;',
  2954. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2955. '$mod.lw = 0x1234CDEF;',
  2956. '$mod.b = $mod.lw & 0xFF;',
  2957. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2958. ]));
  2959. end;
  2960. procedure TTestModule.TestAssignments;
  2961. begin
  2962. StartProgram(false);
  2963. Parser.Options:=Parser.Options+[po_cassignments];
  2964. Add('var');
  2965. Add(' Bar:longint;');
  2966. Add('begin');
  2967. Add(' bar:=3;');
  2968. Add(' bar+=4;');
  2969. Add(' bar-=5;');
  2970. Add(' bar*=6;');
  2971. ConvertProgram;
  2972. CheckSource('TestAssignments',
  2973. LinesToStr([ // statements
  2974. 'this.Bar = 0;'
  2975. ]),
  2976. LinesToStr([ // this.$main
  2977. '$mod.Bar=3;',
  2978. '$mod.Bar+=4;',
  2979. '$mod.Bar-=5;',
  2980. '$mod.Bar*=6;'
  2981. ]));
  2982. end;
  2983. procedure TTestModule.TestArithmeticOperators1;
  2984. begin
  2985. StartProgram(false);
  2986. Add('var');
  2987. Add(' vA,vB,vC:longint;');
  2988. Add('begin');
  2989. Add(' va:=1;');
  2990. Add(' vb:=va+va;');
  2991. Add(' vb:=va div vb;');
  2992. Add(' vb:=va mod vb;');
  2993. Add(' vb:=va+va*vb+va div vb;');
  2994. Add(' vc:=-va;');
  2995. Add(' va:=va-vb;');
  2996. Add(' vb:=va;');
  2997. Add(' if va<vb then vc:=va else vc:=vb;');
  2998. ConvertProgram;
  2999. CheckSource('TestArithmeticOperators1',
  3000. LinesToStr([ // statements
  3001. 'this.vA = 0;',
  3002. 'this.vB = 0;',
  3003. 'this.vC = 0;'
  3004. ]),
  3005. LinesToStr([ // this.$main
  3006. '$mod.vA = 1;',
  3007. '$mod.vB = $mod.vA + $mod.vA;',
  3008. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  3009. '$mod.vB = $mod.vA % $mod.vB;',
  3010. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  3011. '$mod.vC = -$mod.vA;',
  3012. '$mod.vA = $mod.vA - $mod.vB;',
  3013. '$mod.vB = $mod.vA;',
  3014. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3015. ]));
  3016. end;
  3017. procedure TTestModule.TestLogicalOperators;
  3018. begin
  3019. StartProgram(false);
  3020. Add('var');
  3021. Add(' vA,vB,vC:boolean;');
  3022. Add('begin');
  3023. Add(' va:=vb and vc;');
  3024. Add(' va:=vb or vc;');
  3025. Add(' va:=vb xor vc;');
  3026. Add(' va:=true and vc;');
  3027. Add(' va:=(vb and vc) or (va and vb);');
  3028. Add(' va:=not vb;');
  3029. ConvertProgram;
  3030. CheckSource('TestLogicalOperators',
  3031. LinesToStr([ // statements
  3032. 'this.vA = false;',
  3033. 'this.vB = false;',
  3034. 'this.vC = false;'
  3035. ]),
  3036. LinesToStr([ // this.$main
  3037. '$mod.vA = $mod.vB && $mod.vC;',
  3038. '$mod.vA = $mod.vB || $mod.vC;',
  3039. '$mod.vA = $mod.vB ^ $mod.vC;',
  3040. '$mod.vA = true && $mod.vC;',
  3041. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3042. '$mod.vA = !$mod.vB;'
  3043. ]));
  3044. end;
  3045. procedure TTestModule.TestBitwiseOperators;
  3046. begin
  3047. StartProgram(false);
  3048. Add([
  3049. 'var',
  3050. ' vA,vB,vC:longint;',
  3051. ' X,Y,Z: nativeint;',
  3052. 'begin',
  3053. ' va:=vb and vc;',
  3054. ' va:=vb or vc;',
  3055. ' va:=vb xor vc;',
  3056. ' va:=vb shl vc;',
  3057. ' va:=vb shr vc;',
  3058. ' va:=3 and vc;',
  3059. ' va:=(vb and vc) or (va and vb);',
  3060. ' va:=not vb;',
  3061. ' X:=Y and Z;',
  3062. ' X:=Y and va;',
  3063. ' X:=Y or Z;',
  3064. ' X:=Y or va;',
  3065. ' X:=Y xor Z;',
  3066. ' X:=Y xor va;',
  3067. '']);
  3068. ConvertProgram;
  3069. CheckSource('TestBitwiseOperators',
  3070. LinesToStr([ // statements
  3071. 'this.vA = 0;',
  3072. 'this.vB = 0;',
  3073. 'this.vC = 0;',
  3074. 'this.X = 0;',
  3075. 'this.Y = 0;',
  3076. 'this.Z = 0;',
  3077. '']),
  3078. LinesToStr([ // this.$main
  3079. '$mod.vA = $mod.vB & $mod.vC;',
  3080. '$mod.vA = $mod.vB | $mod.vC;',
  3081. '$mod.vA = $mod.vB ^ $mod.vC;',
  3082. '$mod.vA = $mod.vB << $mod.vC;',
  3083. '$mod.vA = $mod.vB >>> $mod.vC;',
  3084. '$mod.vA = 3 & $mod.vC;',
  3085. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3086. '$mod.vA = ~$mod.vB;',
  3087. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3088. '$mod.X = $mod.Y & $mod.vA;',
  3089. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3090. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3091. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3092. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3093. '']));
  3094. end;
  3095. procedure TTestModule.TestBitwiseOperatorsLongword;
  3096. begin
  3097. StartProgram(false);
  3098. Add([
  3099. 'var',
  3100. ' a,b,c:longword;',
  3101. ' i: longint;',
  3102. 'begin',
  3103. ' a:=$12345678;',
  3104. ' b:=$EDCBA987;',
  3105. ' c:=not a;',
  3106. ' c:=a and b;',
  3107. ' c:=a and $ffff0000;',
  3108. ' c:=a or b;',
  3109. ' c:=a or $ff00ff00;',
  3110. ' c:=a xor b;',
  3111. ' c:=a xor $f0f0f0f0;',
  3112. ' c:=a shl 1;',
  3113. ' c:=a shl 16;',
  3114. ' c:=a shl 24;',
  3115. ' c:=a shl b;',
  3116. ' c:=a shr 1;',
  3117. ' c:=a shr 16;',
  3118. ' c:=a shr 24;',
  3119. ' c:=a shr b;',
  3120. ' c:=(b and c) or (a and b);',
  3121. ' c:=i and a;',
  3122. ' c:=i or a;',
  3123. ' c:=i xor a;',
  3124. '']);
  3125. ConvertProgram;
  3126. CheckSource('TestBitwiseOperatorsLongword',
  3127. LinesToStr([ // statements
  3128. 'this.a = 0;',
  3129. 'this.b = 0;',
  3130. 'this.c = 0;',
  3131. 'this.i = 0;',
  3132. '']),
  3133. LinesToStr([ // this.$main
  3134. '$mod.a = 0x12345678;',
  3135. '$mod.b = 0xEDCBA987;',
  3136. '$mod.c = rtl.lw(~$mod.a);',
  3137. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3138. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3139. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3140. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3141. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3142. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3143. '$mod.c = rtl.lw($mod.a << 1);',
  3144. '$mod.c = rtl.lw($mod.a << 16);',
  3145. '$mod.c = rtl.lw($mod.a << 24);',
  3146. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3147. '$mod.c = rtl.lw($mod.a >>> 1);',
  3148. '$mod.c = rtl.lw($mod.a >>> 16);',
  3149. '$mod.c = rtl.lw($mod.a >>> 24);',
  3150. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3151. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3152. '$mod.c = $mod.i & $mod.a;',
  3153. '$mod.c = $mod.i | $mod.a;',
  3154. '$mod.c = $mod.i ^ $mod.a;',
  3155. '']));
  3156. end;
  3157. procedure TTestModule.TestPrgProcVar;
  3158. begin
  3159. StartProgram(false);
  3160. Add('procedure Proc1;');
  3161. Add('type');
  3162. Add(' t1=longint;');
  3163. Add('var');
  3164. Add(' vA:t1;');
  3165. Add('begin');
  3166. Add('end;');
  3167. Add('begin');
  3168. ConvertProgram;
  3169. CheckSource('TestPrgProcVar',
  3170. LinesToStr([ // statements
  3171. 'this.Proc1 = function () {',
  3172. ' var vA=0;',
  3173. '};'
  3174. ]),
  3175. LinesToStr([ // this.$main
  3176. ''
  3177. ]));
  3178. end;
  3179. procedure TTestModule.TestUnitProcVar;
  3180. begin
  3181. StartUnit(false);
  3182. Add('interface');
  3183. Add('');
  3184. Add('type tA=string; // unit scope');
  3185. Add('procedure Proc1;');
  3186. Add('');
  3187. Add('implementation');
  3188. Add('');
  3189. Add('procedure Proc1;');
  3190. Add('type tA=longint; // local proc scope');
  3191. Add('var v1:tA; // using local tA');
  3192. Add('begin');
  3193. Add('end;');
  3194. Add('var v2:tA; // using interface tA');
  3195. ConvertUnit;
  3196. CheckSource('TestUnitProcVar',
  3197. LinesToStr([ // statements
  3198. 'var $impl = $mod.$impl;',
  3199. 'this.Proc1 = function () {',
  3200. ' var v1 = 0;',
  3201. '};',
  3202. '']),
  3203. // this.$init
  3204. '',
  3205. // implementation
  3206. LinesToStr([
  3207. '$impl.v2 = "";',
  3208. '']));
  3209. end;
  3210. procedure TTestModule.TestImplProc;
  3211. begin
  3212. StartUnit(false);
  3213. Add('interface');
  3214. Add('');
  3215. Add('procedure Proc1;');
  3216. Add('');
  3217. Add('implementation');
  3218. Add('');
  3219. Add('procedure Proc1; begin end;');
  3220. Add('procedure Proc2; begin end;');
  3221. Add('initialization');
  3222. Add(' Proc1;');
  3223. Add(' Proc2;');
  3224. ConvertUnit;
  3225. CheckSource('TestImplProc',
  3226. LinesToStr([ // statements
  3227. 'var $impl = $mod.$impl;',
  3228. 'this.Proc1 = function () {',
  3229. '};',
  3230. '']),
  3231. LinesToStr([ // this.$init
  3232. '$mod.Proc1();',
  3233. '$impl.Proc2();',
  3234. '']),
  3235. LinesToStr([ // implementation
  3236. '$impl.Proc2 = function () {',
  3237. '};',
  3238. ''])
  3239. );
  3240. end;
  3241. procedure TTestModule.TestFunctionResult;
  3242. begin
  3243. StartProgram(false);
  3244. Add('function Func1: longint;');
  3245. Add('begin');
  3246. Add(' Result:=3;');
  3247. Add(' Func1:=4;');
  3248. Add('end;');
  3249. Add('begin');
  3250. ConvertProgram;
  3251. CheckSource('TestFunctionResult',
  3252. LinesToStr([ // statements
  3253. 'this.Func1 = function () {',
  3254. ' var Result = 0;',
  3255. ' Result = 3;',
  3256. ' Result = 4;',
  3257. ' return Result;',
  3258. '};'
  3259. ]),
  3260. '');
  3261. end;
  3262. procedure TTestModule.TestNestedProc;
  3263. begin
  3264. StartProgram(false);
  3265. Add([
  3266. 'var vInUnit: longint;',
  3267. 'function DoIt(pA,pD: longint): longint;',
  3268. 'var',
  3269. ' vB: longint;',
  3270. ' vC: longint;',
  3271. ' function Nesty(pA: longint): longint; ',
  3272. ' var vB: longint;',
  3273. ' begin',
  3274. ' Result:=pa+vb+vc+pd+vInUnit;',
  3275. ' nesty:=3;',
  3276. ' doit:=4;',
  3277. ' exit;',
  3278. ' end;',
  3279. 'begin',
  3280. ' Result:=pa+vb+vc;',
  3281. ' doit:=6;',
  3282. ' exit;',
  3283. 'end;',
  3284. 'begin']);
  3285. ConvertProgram;
  3286. CheckSource('TestNestedProc',
  3287. LinesToStr([ // statements
  3288. 'this.vInUnit = 0;',
  3289. 'this.DoIt = function (pA, pD) {',
  3290. ' var Result = 0;',
  3291. ' var vB = 0;',
  3292. ' var vC = 0;',
  3293. ' function Nesty(pA) {',
  3294. ' var Result$1 = 0;',
  3295. ' var vB = 0;',
  3296. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3297. ' Result$1 = 3;',
  3298. ' Result = 4;',
  3299. ' return Result$1;',
  3300. ' return Result$1;',
  3301. ' };',
  3302. ' Result = pA + vB + vC;',
  3303. ' Result = 6;',
  3304. ' return Result;',
  3305. ' return Result;',
  3306. '};'
  3307. ]),
  3308. '');
  3309. end;
  3310. procedure TTestModule.TestNestedProc_ResultString;
  3311. begin
  3312. StartProgram(false);
  3313. Add([
  3314. 'function DoIt: string;',
  3315. ' function Nesty: string; ',
  3316. ' begin',
  3317. ' nesty:=#65#66;',
  3318. ' nesty[1]:=#67;',
  3319. ' doit:=#68;',
  3320. ' doit[2]:=#69;',
  3321. ' end;',
  3322. 'begin',
  3323. ' doit:=#70;',
  3324. ' doit[3]:=#71;',
  3325. 'end;',
  3326. 'begin']);
  3327. ConvertProgram;
  3328. CheckSource('TestNestedProc_ResultString',
  3329. LinesToStr([ // statements
  3330. 'this.DoIt = function () {',
  3331. ' var Result = "";',
  3332. ' function Nesty() {',
  3333. ' var Result$1 = "";',
  3334. ' Result$1 = "AB";',
  3335. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3336. ' Result = "D";',
  3337. ' Result = rtl.setCharAt(Result, 1, "E");',
  3338. ' return Result$1;',
  3339. ' };',
  3340. ' Result = "F";',
  3341. ' Result = rtl.setCharAt(Result, 2, "G");',
  3342. ' return Result;',
  3343. '};'
  3344. ]),
  3345. '');
  3346. end;
  3347. procedure TTestModule.TestForwardProc;
  3348. begin
  3349. StartProgram(false);
  3350. Add('procedure FuncA(Bar: longint); forward;');
  3351. Add('procedure FuncB(Bar: longint);');
  3352. Add('begin');
  3353. Add(' funca(bar);');
  3354. Add('end;');
  3355. Add('procedure funca(bar: longint);');
  3356. Add('begin');
  3357. Add(' if bar=3 then ;');
  3358. Add('end;');
  3359. Add('begin');
  3360. Add(' funca(4);');
  3361. Add(' funcb(5);');
  3362. ConvertProgram;
  3363. CheckSource('TestForwardProc',
  3364. LinesToStr([ // statements'
  3365. 'this.FuncB = function (Bar) {',
  3366. ' $mod.FuncA(Bar);',
  3367. '};',
  3368. 'this.FuncA = function (Bar) {',
  3369. ' if (Bar === 3);',
  3370. '};'
  3371. ]),
  3372. LinesToStr([
  3373. '$mod.FuncA(4);',
  3374. '$mod.FuncB(5);'
  3375. ])
  3376. );
  3377. end;
  3378. procedure TTestModule.TestNestedForwardProc;
  3379. begin
  3380. StartProgram(false);
  3381. Add('procedure FuncA;');
  3382. Add(' procedure FuncB(i: longint); forward;');
  3383. Add(' procedure FuncC(i: longint);');
  3384. Add(' begin');
  3385. Add(' funcb(i);');
  3386. Add(' end;');
  3387. Add(' procedure FuncB(i: longint);');
  3388. Add(' begin');
  3389. Add(' if i=3 then ;');
  3390. Add(' end;');
  3391. Add('begin');
  3392. Add(' funcc(4)');
  3393. Add('end;');
  3394. Add('begin');
  3395. Add(' funca;');
  3396. ConvertProgram;
  3397. CheckSource('TestNestedForwardProc',
  3398. LinesToStr([ // statements'
  3399. 'this.FuncA = function () {',
  3400. ' function FuncC(i) {',
  3401. ' FuncB(i);',
  3402. ' };',
  3403. ' function FuncB(i) {',
  3404. ' if (i === 3);',
  3405. ' };',
  3406. ' FuncC(4);',
  3407. '};'
  3408. ]),
  3409. LinesToStr([
  3410. '$mod.FuncA();'
  3411. ])
  3412. );
  3413. end;
  3414. procedure TTestModule.TestAssignFunctionResult;
  3415. begin
  3416. StartProgram(false);
  3417. Add('function Func1: longint;');
  3418. Add('begin');
  3419. Add('end;');
  3420. Add('var i: longint;');
  3421. Add('begin');
  3422. Add(' i:=func1();');
  3423. Add(' i:=func1()+func1();');
  3424. ConvertProgram;
  3425. CheckSource('TestAssignFunctionResult',
  3426. LinesToStr([ // statements
  3427. 'this.Func1 = function () {',
  3428. ' var Result = 0;',
  3429. ' return Result;',
  3430. '};',
  3431. 'this.i = 0;'
  3432. ]),
  3433. LinesToStr([
  3434. '$mod.i = $mod.Func1();',
  3435. '$mod.i = $mod.Func1() + $mod.Func1();'
  3436. ]));
  3437. end;
  3438. procedure TTestModule.TestFunctionResultInCondition;
  3439. begin
  3440. StartProgram(false);
  3441. Add('function Func1: longint;');
  3442. Add('begin');
  3443. Add('end;');
  3444. Add('function Func2: boolean;');
  3445. Add('begin');
  3446. Add('end;');
  3447. Add('var i: longint;');
  3448. Add('begin');
  3449. Add(' if func2 then ;');
  3450. Add(' if i=func1() then ;');
  3451. Add(' if i=func1 then ;');
  3452. ConvertProgram;
  3453. CheckSource('TestFunctionResultInCondition',
  3454. LinesToStr([ // statements
  3455. 'this.Func1 = function () {',
  3456. ' var Result = 0;',
  3457. ' return Result;',
  3458. '};',
  3459. 'this.Func2 = function () {',
  3460. ' var Result = false;',
  3461. ' return Result;',
  3462. '};',
  3463. 'this.i = 0;'
  3464. ]),
  3465. LinesToStr([
  3466. 'if ($mod.Func2());',
  3467. 'if ($mod.i === $mod.Func1());',
  3468. 'if ($mod.i === $mod.Func1());'
  3469. ]));
  3470. end;
  3471. procedure TTestModule.TestFunctionResultInForLoop;
  3472. begin
  3473. StartProgram(false);
  3474. Add([
  3475. 'function Func1(a: array of longint): longint;',
  3476. 'begin',
  3477. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3478. ' for Result in a do if a[Result]=0 then exit;',
  3479. 'end;',
  3480. 'begin',
  3481. ' Func1([1,2,3])']);
  3482. ConvertProgram;
  3483. CheckSource('TestFunctionResultInForLoop',
  3484. LinesToStr([ // statements
  3485. 'this.Func1 = function (a) {',
  3486. ' var Result = 0;',
  3487. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3488. ' Result = $l;',
  3489. ' if (a[Result] === 0) return Result;',
  3490. ' };',
  3491. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3492. ' Result = $in[$l1];',
  3493. ' if (a[Result] === 0) return Result;',
  3494. ' };',
  3495. ' return Result;',
  3496. '};',
  3497. '']),
  3498. LinesToStr([
  3499. '$mod.Func1([1, 2, 3]);'
  3500. ]));
  3501. end;
  3502. procedure TTestModule.TestFunctionResultInTypeCast;
  3503. begin
  3504. StartProgram(false);
  3505. Add([
  3506. 'function GetInt: longint;',
  3507. 'begin',
  3508. 'end;',
  3509. 'begin',
  3510. ' if Byte(GetInt)=0 then ;',
  3511. '']);
  3512. ConvertProgram;
  3513. CheckSource('TestFunctionResultInTypeCast',
  3514. LinesToStr([ // statements
  3515. 'this.GetInt = function () {',
  3516. ' var Result = 0;',
  3517. ' return Result;',
  3518. '};',
  3519. '']),
  3520. LinesToStr([
  3521. 'if (($mod.GetInt() & 255) === 0) ;'
  3522. ]));
  3523. end;
  3524. procedure TTestModule.TestExit;
  3525. begin
  3526. StartProgram(false);
  3527. Add('procedure ProcA;');
  3528. Add('begin');
  3529. Add(' exit;');
  3530. Add('end;');
  3531. Add('function FuncB: longint;');
  3532. Add('begin');
  3533. Add(' exit;');
  3534. Add(' exit(3);');
  3535. Add('end;');
  3536. Add('function FuncC: string;');
  3537. Add('begin');
  3538. Add(' exit;');
  3539. Add(' exit(''a'');');
  3540. Add(' exit(''abc'');');
  3541. Add('end;');
  3542. Add('begin');
  3543. Add(' exit;');
  3544. Add(' exit(1);');
  3545. ConvertProgram;
  3546. CheckSource('TestExit',
  3547. LinesToStr([ // statements
  3548. 'this.ProcA = function () {',
  3549. ' return;',
  3550. '};',
  3551. 'this.FuncB = function () {',
  3552. ' var Result = 0;',
  3553. ' return Result;',
  3554. ' return 3;',
  3555. ' return Result;',
  3556. '};',
  3557. 'this.FuncC = function () {',
  3558. ' var Result = "";',
  3559. ' return Result;',
  3560. ' return "a";',
  3561. ' return "abc";',
  3562. ' return Result;',
  3563. '};'
  3564. ]),
  3565. LinesToStr([
  3566. 'return;',
  3567. 'return 1;',
  3568. '']));
  3569. end;
  3570. procedure TTestModule.TestExit_ResultInFinally;
  3571. begin
  3572. StartProgram(false);
  3573. Add([
  3574. 'function Run: word;',
  3575. 'begin',
  3576. ' try',
  3577. ' exit(3);', // no Result in finally -> use return 3
  3578. ' finally',
  3579. ' end;',
  3580. 'end;',
  3581. 'function Fly: word;',
  3582. 'begin',
  3583. ' try',
  3584. ' exit(3);',
  3585. ' finally',
  3586. ' if Result>0 then ;',
  3587. ' end;',
  3588. 'end;',
  3589. 'function Jump: word;',
  3590. 'begin',
  3591. ' try',
  3592. ' try',
  3593. ' exit(4);',
  3594. ' finally',
  3595. ' end;',
  3596. ' finally',
  3597. ' if Result>0 then ;',
  3598. ' end;',
  3599. 'end;',
  3600. 'begin',
  3601. '']);
  3602. ConvertProgram;
  3603. CheckSource('TestExit_ResultInFinally',
  3604. LinesToStr([ // statements
  3605. 'this.Run = function () {',
  3606. ' var Result = 0;',
  3607. ' try {',
  3608. ' return 3;',
  3609. ' } finally {',
  3610. ' };',
  3611. ' return Result;',
  3612. '};',
  3613. 'this.Fly = function () {',
  3614. ' var Result = 0;',
  3615. ' try {',
  3616. ' Result = 3;',
  3617. ' return Result;',
  3618. ' } finally {',
  3619. ' if (Result > 0) ;',
  3620. ' };',
  3621. ' return Result;',
  3622. '};',
  3623. 'this.Jump = function () {',
  3624. ' var Result = 0;',
  3625. ' try {',
  3626. ' try {',
  3627. ' Result = 4;',
  3628. ' return Result;',
  3629. ' } finally {',
  3630. ' };',
  3631. ' } finally {',
  3632. ' if (Result > 0) ;',
  3633. ' };',
  3634. ' return Result;',
  3635. '};',
  3636. '']),
  3637. LinesToStr([
  3638. '']));
  3639. end;
  3640. procedure TTestModule.TestBreak;
  3641. begin
  3642. StartProgram(false);
  3643. Add([
  3644. 'var',
  3645. ' i: longint;',
  3646. 'begin',
  3647. ' repeat',
  3648. ' break;',
  3649. ' until true;',
  3650. ' while true do',
  3651. ' break;',
  3652. ' for i:=1 to 2 do',
  3653. ' break;']);
  3654. ConvertProgram;
  3655. CheckSource('TestBreak',
  3656. LinesToStr([ // statements
  3657. 'this.i = 0;'
  3658. ]),
  3659. LinesToStr([
  3660. 'do {',
  3661. ' break;',
  3662. '} while (!true);',
  3663. 'while (true) break;',
  3664. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3665. '']));
  3666. end;
  3667. procedure TTestModule.TestBreakAsVar;
  3668. begin
  3669. StartProgram(false);
  3670. Add([
  3671. 'procedure DoIt(break: boolean);',
  3672. 'begin',
  3673. ' if break then ;',
  3674. 'end;',
  3675. 'var',
  3676. ' break: boolean;',
  3677. 'begin',
  3678. ' if break then ;']);
  3679. ConvertProgram;
  3680. CheckSource('TestBreakAsVar',
  3681. LinesToStr([ // statements
  3682. 'this.DoIt = function (Break) {',
  3683. ' if (Break) ;',
  3684. '};',
  3685. 'this.Break = false;',
  3686. '']),
  3687. LinesToStr([
  3688. 'if($mod.Break) ;',
  3689. '']));
  3690. end;
  3691. procedure TTestModule.TestContinue;
  3692. begin
  3693. StartProgram(false);
  3694. Add('var i: longint;');
  3695. Add('begin');
  3696. Add(' repeat');
  3697. Add(' continue;');
  3698. Add(' until true;');
  3699. Add(' while true do');
  3700. Add(' continue;');
  3701. Add(' for i:=1 to 2 do');
  3702. Add(' continue;');
  3703. ConvertProgram;
  3704. CheckSource('TestContinue',
  3705. LinesToStr([ // statements
  3706. 'this.i = 0;'
  3707. ]),
  3708. LinesToStr([
  3709. 'do {',
  3710. ' continue;',
  3711. '} while (!true);',
  3712. 'while (true) continue;',
  3713. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3714. '']));
  3715. end;
  3716. procedure TTestModule.TestProc_External;
  3717. begin
  3718. StartProgram(false);
  3719. Add('procedure Foo; external name ''console.log'';');
  3720. Add('function Bar: longint; external name ''get.item'';');
  3721. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3722. Add('var');
  3723. Add(' i: longint;');
  3724. Add('begin');
  3725. Add(' Foo;');
  3726. Add(' i:=Bar;');
  3727. Add(' i:=Bla(''abc'');');
  3728. ConvertProgram;
  3729. CheckSource('TestProc_External',
  3730. LinesToStr([ // statements
  3731. 'this.i = 0;'
  3732. ]),
  3733. LinesToStr([
  3734. 'console.log();',
  3735. '$mod.i = get.item();',
  3736. '$mod.i = apply.something("abc");'
  3737. ]));
  3738. end;
  3739. procedure TTestModule.TestProc_ExternalOtherUnit;
  3740. begin
  3741. AddModuleWithIntfImplSrc('unit2.pas',
  3742. LinesToStr([
  3743. 'procedure Now; external name ''Date.now'';',
  3744. 'procedure DoIt;'
  3745. ]),
  3746. 'procedure doit; begin end;');
  3747. StartUnit(true);
  3748. Add('interface');
  3749. Add('uses unit2;');
  3750. Add('implementation');
  3751. Add('begin');
  3752. Add(' now;');
  3753. Add(' now();');
  3754. Add(' uNit2.now;');
  3755. Add(' uNit2.now();');
  3756. Add(' doit;');
  3757. Add(' uNit2.doit;');
  3758. ConvertUnit;
  3759. CheckSource('TestProc_ExternalOtherUnit',
  3760. LinesToStr([
  3761. '']),
  3762. LinesToStr([
  3763. 'Date.now();',
  3764. 'Date.now();',
  3765. 'Date.now();',
  3766. 'Date.now();',
  3767. 'pas.unit2.DoIt();',
  3768. 'pas.unit2.DoIt();',
  3769. '']));
  3770. end;
  3771. procedure TTestModule.TestProc_Asm;
  3772. begin
  3773. StartProgram(false);
  3774. Add([
  3775. '{$mode delphi}',
  3776. 'function DoIt: longint;',
  3777. 'begin;',
  3778. ' asm',
  3779. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3780. ' end;',
  3781. ' asm console.log(); end;',
  3782. ' asm',
  3783. ' s = "'' ";',
  3784. ' s = ''" '';',
  3785. ' s = s + "world" + "''";',
  3786. ' // end',
  3787. ' s = ''end'';',
  3788. ' s = "end";',
  3789. ' s = "foo\"bar";',
  3790. ' s = ''a\''b'';',
  3791. ' s = `${expr}\`-"-''-`;',
  3792. ' s = `multi',
  3793. 'line`;',
  3794. ' end;',
  3795. 'end;',
  3796. 'procedure Fly;',
  3797. 'asm',
  3798. ' return;',
  3799. 'end;',
  3800. 'begin']);
  3801. ConvertProgram;
  3802. CheckSource('TestProc_Asm',
  3803. LinesToStr([ // statements
  3804. 'this.DoIt = function () {',
  3805. ' var Result = 0;',
  3806. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3807. ' console.log();',
  3808. ' s = "'' ";',
  3809. ' s = ''" '';',
  3810. ' s = s + "world" + "''";',
  3811. ' // end',
  3812. ' s = ''end'';',
  3813. ' s = "end";',
  3814. ' s = "foo\"bar";',
  3815. ' s = ''a\''b'';',
  3816. ' s = `${expr}\`-"-''-`;',
  3817. ' s = `multi',
  3818. 'line`;',
  3819. ' return Result;',
  3820. '};',
  3821. 'this.Fly = function () {',
  3822. ' return;',
  3823. '};',
  3824. '']),
  3825. LinesToStr([
  3826. ''
  3827. ]));
  3828. end;
  3829. procedure TTestModule.TestProc_Assembler;
  3830. begin
  3831. StartProgram(false);
  3832. Add('function DoIt: longint; assembler;');
  3833. Add('asm');
  3834. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3835. Add('end;');
  3836. Add('begin');
  3837. ConvertProgram;
  3838. CheckSource('TestProc_Assembler',
  3839. LinesToStr([ // statements
  3840. 'this.DoIt = function () {',
  3841. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3842. '};'
  3843. ]),
  3844. LinesToStr([
  3845. ''
  3846. ]));
  3847. end;
  3848. procedure TTestModule.TestProc_VarParam;
  3849. begin
  3850. StartProgram(false);
  3851. Add('type integer = longint;');
  3852. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3853. Add('var vJ: integer;');
  3854. Add('begin');
  3855. Add(' vg:=vg+1;');
  3856. Add(' vj:=vh+2;');
  3857. Add(' vi:=vi+3;');
  3858. Add(' doit(vg,vg,vg);');
  3859. Add(' doit(vh,vh,vj);');
  3860. Add(' doit(vi,vi,vi);');
  3861. Add(' doit(vj,vj,vj);');
  3862. Add('end;');
  3863. Add('var i: integer;');
  3864. Add('begin');
  3865. Add(' doit(i,i,i);');
  3866. ConvertProgram;
  3867. CheckSource('TestProc_VarParam',
  3868. LinesToStr([ // statements
  3869. 'this.DoIt = function (vG,vH,vI) {',
  3870. ' var vJ = 0;',
  3871. ' vG = vG + 1;',
  3872. ' vJ = vH + 2;',
  3873. ' vI.set(vI.get()+3);',
  3874. ' $mod.DoIt(vG, vG, {',
  3875. ' get: function () {',
  3876. ' return vG;',
  3877. ' },',
  3878. ' set: function (v) {',
  3879. ' vG = v;',
  3880. ' }',
  3881. ' });',
  3882. ' $mod.DoIt(vH, vH, {',
  3883. ' get: function () {',
  3884. ' return vJ;',
  3885. ' },',
  3886. ' set: function (v) {',
  3887. ' vJ = v;',
  3888. ' }',
  3889. ' });',
  3890. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3891. ' $mod.DoIt(vJ, vJ, {',
  3892. ' get: function () {',
  3893. ' return vJ;',
  3894. ' },',
  3895. ' set: function (v) {',
  3896. ' vJ = v;',
  3897. ' }',
  3898. ' });',
  3899. '};',
  3900. 'this.i = 0;'
  3901. ]),
  3902. LinesToStr([
  3903. '$mod.DoIt($mod.i,$mod.i,{',
  3904. ' p: $mod,',
  3905. ' get: function () {',
  3906. ' return this.p.i;',
  3907. ' },',
  3908. ' set: function (v) {',
  3909. ' this.p.i = v;',
  3910. ' }',
  3911. '});'
  3912. ]));
  3913. end;
  3914. procedure TTestModule.TestProc_VarParamString;
  3915. begin
  3916. StartProgram(false);
  3917. Add(['type TCaption = string;',
  3918. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3919. 'var c: char;',
  3920. 'begin',
  3921. ' va[1]:=c;',
  3922. ' vb[2]:=c;',
  3923. ' vc[3]:=c;',
  3924. 'end;',
  3925. 'begin']);
  3926. ConvertProgram;
  3927. CheckSource('TestProc_VarParamString',
  3928. LinesToStr([ // statements
  3929. 'this.DoIt = function (vA,vB,vC) {',
  3930. ' var c = "";',
  3931. ' vA = rtl.setCharAt(vA, 0, c);',
  3932. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3933. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3934. '};',
  3935. '']),
  3936. LinesToStr([
  3937. ]));
  3938. end;
  3939. procedure TTestModule.TestProc_VarParamV;
  3940. begin
  3941. StartProgram(false);
  3942. Add([
  3943. 'procedure Inc2(var i: longint);',
  3944. 'begin',
  3945. ' i:=i+2;',
  3946. 'end;',
  3947. 'procedure DoIt(v: longint);',
  3948. 'var p: array of longint;',
  3949. 'begin',
  3950. ' Inc2(v);',
  3951. ' Inc2(p[v]);',
  3952. 'end;',
  3953. 'begin']);
  3954. ConvertProgram;
  3955. CheckSource('TestProc_VarParamV',
  3956. LinesToStr([ // statements
  3957. 'this.Inc2 = function (i) {',
  3958. ' i.set(i.get()+2);',
  3959. '};',
  3960. 'this.DoIt = function (v) {',
  3961. ' var p = [];',
  3962. ' $mod.Inc2({get: function () {',
  3963. ' return v;',
  3964. ' }, set: function (w) {',
  3965. ' v = w;',
  3966. ' }});',
  3967. ' $mod.Inc2({',
  3968. ' a: v,',
  3969. ' p: p,',
  3970. ' get: function () {',
  3971. ' return this.p[this.a];',
  3972. ' },',
  3973. ' set: function (v) {',
  3974. ' this.p[this.a] = v;',
  3975. ' }',
  3976. ' });',
  3977. '};',
  3978. '']),
  3979. LinesToStr([
  3980. '']));
  3981. end;
  3982. procedure TTestModule.TestProc_Overload;
  3983. begin
  3984. StartProgram(false);
  3985. Add('procedure DoIt(vI: longint); begin end;');
  3986. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3987. Add('procedure DoIt(vD: double); begin end;');
  3988. Add('begin');
  3989. Add(' DoIt(1);');
  3990. Add(' DoIt(2,3);');
  3991. Add(' DoIt(4.5);');
  3992. ConvertProgram;
  3993. CheckSource('TestProcedureOverload',
  3994. LinesToStr([ // statements
  3995. 'this.DoIt = function (vI) {',
  3996. '};',
  3997. 'this.DoIt$1 = function (vI, vJ) {',
  3998. '};',
  3999. 'this.DoIt$2 = function (vD) {',
  4000. '};',
  4001. '']),
  4002. LinesToStr([
  4003. '$mod.DoIt(1);',
  4004. '$mod.DoIt$1(2, 3);',
  4005. '$mod.DoIt$2(4.5);',
  4006. '']));
  4007. end;
  4008. procedure TTestModule.TestProc_OverloadForward;
  4009. begin
  4010. StartProgram(false);
  4011. Add('procedure DoIt(vI: longint); forward;');
  4012. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4013. Add('procedure doit(vi: longint); begin end;');
  4014. Add('begin');
  4015. Add(' doit(1);');
  4016. Add(' doit(2,3);');
  4017. ConvertProgram;
  4018. CheckSource('TestProcedureOverloadForward',
  4019. LinesToStr([ // statements
  4020. 'this.DoIt$1 = function (vI, vJ) {',
  4021. '};',
  4022. 'this.DoIt = function (vI) {',
  4023. '};',
  4024. '']),
  4025. LinesToStr([
  4026. '$mod.DoIt(1);',
  4027. '$mod.DoIt$1(2, 3);',
  4028. '']));
  4029. end;
  4030. procedure TTestModule.TestProc_OverloadIntfImpl;
  4031. begin
  4032. StartUnit(false);
  4033. Add('interface');
  4034. Add('procedure DoIt(vI: longint);');
  4035. Add('procedure DoIt(vI, vJ: longint);');
  4036. Add('implementation');
  4037. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4038. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4039. Add('procedure DoIt(vi: longint); begin end;');
  4040. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4041. Add('procedure DoIt(vi, vj: longint); begin end;');
  4042. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4043. Add('begin');
  4044. Add(' doit(1);');
  4045. Add(' doit(2,3);');
  4046. Add(' doit(4,5,6);');
  4047. Add(' doit(7,8,9,10);');
  4048. Add(' doit(11,12,13,14,15);');
  4049. ConvertUnit;
  4050. CheckSource('TestProcedureOverloadUnit',
  4051. LinesToStr([ // statements
  4052. 'var $impl = $mod.$impl;',
  4053. 'this.DoIt = function (vI) {',
  4054. '};',
  4055. 'this.DoIt$1 = function (vI, vJ) {',
  4056. '};',
  4057. '']),
  4058. LinesToStr([ // this.$init
  4059. '$mod.DoIt(1);',
  4060. '$mod.DoIt$1(2, 3);',
  4061. '$impl.DoIt$3(4,5,6);',
  4062. '$impl.DoIt$4(7,8,9,10);',
  4063. '$impl.DoIt$2(11,12,13,14,15);',
  4064. '']),
  4065. LinesToStr([ // implementation
  4066. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4067. '};',
  4068. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4069. '};',
  4070. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4071. '};',
  4072. '']));
  4073. end;
  4074. procedure TTestModule.TestProc_OverloadNested;
  4075. begin
  4076. StartProgram(false);
  4077. Add([
  4078. 'procedure doit(vA: longint);',
  4079. ' procedure DoIt(vA, vB: longint); overload;',
  4080. ' begin',
  4081. ' doit(1);',
  4082. ' doit(1,2);',
  4083. ' end;',
  4084. ' procedure doit(vA, vB, vC: longint);',
  4085. ' begin',
  4086. ' doit(1);',
  4087. ' doit(1,2);',
  4088. ' doit(1,2,3);',
  4089. ' end;',
  4090. 'begin',
  4091. ' doit(1);',
  4092. ' doit(1,2);',
  4093. ' doit(1,2,3);',
  4094. 'end;',
  4095. 'begin // main',
  4096. ' doit(1);']);
  4097. ConvertProgram;
  4098. CheckSource('TestProcedureOverloadNested',
  4099. LinesToStr([ // statements
  4100. 'this.doit = function (vA) {',
  4101. ' function DoIt$1(vA, vB) {',
  4102. ' $mod.doit(1);',
  4103. ' DoIt$1(1, 2);',
  4104. ' };',
  4105. ' function doit$2(vA, vB, vC) {',
  4106. ' $mod.doit(1);',
  4107. ' DoIt$1(1, 2);',
  4108. ' doit$2(1, 2, 3);',
  4109. ' };',
  4110. ' $mod.doit(1);',
  4111. ' DoIt$1(1, 2);',
  4112. ' doit$2(1, 2, 3);',
  4113. '};',
  4114. '']),
  4115. LinesToStr([
  4116. '$mod.doit(1);',
  4117. '']));
  4118. end;
  4119. procedure TTestModule.TestProc_OverloadNestedForward;
  4120. begin
  4121. StartProgram(false);
  4122. Add([
  4123. 'procedure DoIt(vA: longint); overload; forward;',
  4124. 'procedure DoIt(vB, vC: longint); overload;',
  4125. 'begin // 2 param overload',
  4126. ' doit(1);',
  4127. ' doit(1,2);',
  4128. 'end;',
  4129. 'procedure doit(vA: longint);',
  4130. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4131. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4132. ' begin // 4 param overload',
  4133. ' doit(1);',
  4134. ' doit(1,2);',
  4135. ' doit(1,2,3);',
  4136. ' doit(1,2,3,4);',
  4137. ' end;',
  4138. ' procedure doit(vA, vB, vC: longint);',
  4139. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4140. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4141. ' begin // 6 param overload',
  4142. ' doit(1);',
  4143. ' doit(1,2);',
  4144. ' doit(1,2,3);',
  4145. ' doit(1,2,3,4);',
  4146. ' doit(1,2,3,4,5);',
  4147. ' doit(1,2,3,4,5,6);',
  4148. ' end;',
  4149. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4150. ' begin // 5 param overload',
  4151. ' doit(1);',
  4152. ' doit(1,2);',
  4153. ' doit(1,2,3);',
  4154. ' doit(1,2,3,4);',
  4155. ' doit(1,2,3,4,5);',
  4156. ' doit(1,2,3,4,5,6);',
  4157. ' end;',
  4158. ' begin // 3 param overload',
  4159. ' doit(1);',
  4160. ' doit(1,2);',
  4161. ' doit(1,2,3);',
  4162. ' doit(1,2,3,4);',
  4163. ' doit(1,2,3,4,5);',
  4164. ' doit(1,2,3,4,5,6);',
  4165. ' end;',
  4166. 'begin // 1 param overload',
  4167. ' doit(1);',
  4168. ' doit(1,2);',
  4169. ' doit(1,2,3);',
  4170. ' doit(1,2,3,4);',
  4171. 'end;',
  4172. 'begin // main',
  4173. ' doit(1);',
  4174. ' doit(1,2);']);
  4175. ConvertProgram;
  4176. CheckSource('TestProc_OverloadNestedForward',
  4177. LinesToStr([ // statements
  4178. 'this.DoIt$1 = function (vB, vC) {',
  4179. ' $mod.DoIt(1);',
  4180. ' $mod.DoIt$1(1, 2);',
  4181. '};',
  4182. 'this.DoIt = function (vA) {',
  4183. ' function DoIt$3(vA, vB, vC, vD) {',
  4184. ' $mod.DoIt(1);',
  4185. ' $mod.DoIt$1(1, 2);',
  4186. ' DoIt$2(1, 2, 3);',
  4187. ' DoIt$3(1, 2, 3, 4);',
  4188. ' };',
  4189. ' function DoIt$2(vA, vB, vC) {',
  4190. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4191. ' $mod.DoIt(1);',
  4192. ' $mod.DoIt$1(1, 2);',
  4193. ' DoIt$2(1, 2, 3);',
  4194. ' DoIt$3(1, 2, 3, 4);',
  4195. ' DoIt$4(1, 2, 3, 4, 5);',
  4196. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4197. ' };',
  4198. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4199. ' $mod.DoIt(1);',
  4200. ' $mod.DoIt$1(1, 2);',
  4201. ' DoIt$2(1, 2, 3);',
  4202. ' DoIt$3(1, 2, 3, 4);',
  4203. ' DoIt$4(1, 2, 3, 4, 5);',
  4204. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4205. ' };',
  4206. ' $mod.DoIt(1);',
  4207. ' $mod.DoIt$1(1, 2);',
  4208. ' DoIt$2(1, 2, 3);',
  4209. ' DoIt$3(1, 2, 3, 4);',
  4210. ' DoIt$4(1, 2, 3, 4, 5);',
  4211. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4212. ' };',
  4213. ' $mod.DoIt(1);',
  4214. ' $mod.DoIt$1(1, 2);',
  4215. ' DoIt$2(1, 2, 3);',
  4216. ' DoIt$3(1, 2, 3, 4);',
  4217. '};',
  4218. '']),
  4219. LinesToStr([
  4220. '$mod.DoIt(1);',
  4221. '$mod.DoIt$1(1, 2);',
  4222. '']));
  4223. end;
  4224. procedure TTestModule.TestProc_OverloadUnitCycle;
  4225. begin
  4226. AddModuleWithIntfImplSrc('Unit2.pas',
  4227. LinesToStr([
  4228. 'type',
  4229. ' TObject = class',
  4230. ' procedure DoIt(b: boolean); virtual; abstract;',
  4231. ' procedure DoIt(i: longint); virtual; abstract;',
  4232. ' end;',
  4233. '']),
  4234. 'uses test1;');
  4235. StartUnit(true);
  4236. Add([
  4237. 'interface',
  4238. 'uses unit2;',
  4239. 'type',
  4240. ' TEagle = class(TObject)',
  4241. ' procedure DoIt(b: boolean); override;',
  4242. ' procedure DoIt(i: longint); override;',
  4243. ' end;',
  4244. 'implementation',
  4245. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4246. 'procedure TEagle.DoIt(i: longint); begin end;',
  4247. '']);
  4248. ConvertUnit;
  4249. CheckSource('TestProc_OverloadUnitCycle',
  4250. LinesToStr([ // statements
  4251. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  4252. ' this.DoIt = function (b) {',
  4253. ' };',
  4254. ' this.DoIt$1 = function (i) {',
  4255. ' };',
  4256. '});',
  4257. '']),
  4258. '',
  4259. LinesToStr([
  4260. '']));
  4261. end;
  4262. procedure TTestModule.TestProc_Varargs;
  4263. begin
  4264. StartProgram(false);
  4265. Add([
  4266. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4267. 'procedure ProcB; varargs; external name ''ProcB'';',
  4268. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4269. 'function GetIt: longint; begin end;',
  4270. 'begin',
  4271. ' ProcA(1);',
  4272. ' ProcA(1,2);',
  4273. ' ProcA(1,2.0);',
  4274. ' ProcA(1,2,3);',
  4275. ' ProcA(1,''2'');',
  4276. ' ProcA(2,'''');',
  4277. ' ProcA(3,false);',
  4278. ' ProcB;',
  4279. ' ProcB();',
  4280. ' ProcB(4);',
  4281. ' ProcB(''foo'');',
  4282. ' ProcC;',
  4283. ' ProcC();',
  4284. ' ProcC(4);',
  4285. ' ProcC(5,''foo'');',
  4286. ' ProcB(GetIt);',
  4287. ' ProcB(GetIt());',
  4288. ' ProcB(GetIt,GetIt());']);
  4289. ConvertProgram;
  4290. CheckSource('TestProc_Varargs',
  4291. LinesToStr([ // statements
  4292. 'this.GetIt = function () {',
  4293. ' var Result = 0;',
  4294. ' return Result;',
  4295. '};',
  4296. '']),
  4297. LinesToStr([
  4298. 'ProcA(1);',
  4299. 'ProcA(1, 2);',
  4300. 'ProcA(1, 2.0);',
  4301. 'ProcA(1, 2, 3);',
  4302. 'ProcA(1, "2");',
  4303. 'ProcA(2, "");',
  4304. 'ProcA(3, false);',
  4305. 'ProcB();',
  4306. 'ProcB();',
  4307. 'ProcB(4);',
  4308. 'ProcB("foo");',
  4309. 'ProcC(17);',
  4310. 'ProcC(17);',
  4311. 'ProcC(4);',
  4312. 'ProcC(5, "foo");',
  4313. 'ProcB($mod.GetIt());',
  4314. 'ProcB($mod.GetIt());',
  4315. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4316. '']));
  4317. end;
  4318. procedure TTestModule.TestProc_ConstOrder;
  4319. begin
  4320. StartProgram(false);
  4321. Add([
  4322. 'const A = 3;',
  4323. 'const B = A+1;',
  4324. 'procedure DoIt;',
  4325. 'const C = A+1;',
  4326. 'const D = B+1;',
  4327. 'const E = D+C+B+A;',
  4328. 'begin',
  4329. 'end;',
  4330. 'begin'
  4331. ]);
  4332. ConvertProgram;
  4333. CheckSource('TestProc_ConstOrder',
  4334. LinesToStr([ // statements
  4335. 'this.A = 3;',
  4336. 'this.B = 3 + 1;',
  4337. 'var C = 3 + 1;',
  4338. 'var D = 4 + 1;',
  4339. 'var E = 5 + 4 + 4 + 3;',
  4340. 'this.DoIt = function () {',
  4341. '};',
  4342. '']),
  4343. LinesToStr([
  4344. ''
  4345. ]));
  4346. end;
  4347. procedure TTestModule.TestProc_DuplicateConst;
  4348. begin
  4349. StartProgram(false);
  4350. Add([
  4351. 'const A = 1;',
  4352. 'procedure DoIt;',
  4353. 'const A = 2;',
  4354. ' procedure SubIt;',
  4355. ' const A = 21;',
  4356. ' begin',
  4357. ' end;',
  4358. 'begin',
  4359. 'end;',
  4360. 'procedure DoSome;',
  4361. 'const A = 3;',
  4362. 'begin',
  4363. 'end;',
  4364. 'begin'
  4365. ]);
  4366. ConvertProgram;
  4367. CheckSource('TestProc_DuplicateConst',
  4368. LinesToStr([ // statements
  4369. 'this.A = 1;',
  4370. 'var A$1 = 2;',
  4371. 'var A$2 = 21;',
  4372. 'this.DoIt = function () {',
  4373. ' function SubIt() {',
  4374. ' };',
  4375. '};',
  4376. 'var A$3 = 3;',
  4377. 'this.DoSome = function () {',
  4378. '};',
  4379. '']),
  4380. LinesToStr([
  4381. ''
  4382. ]));
  4383. end;
  4384. procedure TTestModule.TestProc_LocalVarAbsolute;
  4385. begin
  4386. StartProgram(false);
  4387. Add([
  4388. 'type',
  4389. ' TObject = class',
  4390. ' Index: longint;',
  4391. ' procedure DoAbs(Item: pointer);',
  4392. ' end;',
  4393. 'procedure TObject.DoAbs(Item: pointer);',
  4394. 'var',
  4395. ' o: TObject absolute Item;',
  4396. 'begin',
  4397. ' if o.Index<o.Index then o.Index:=o.Index;',
  4398. 'end;',
  4399. 'procedure DoIt(i: longint; p: pointer);',
  4400. 'var',
  4401. ' d: double absolute i;',
  4402. ' s: string absolute d;',
  4403. ' oi: TObject absolute i;',
  4404. ' op: TObject absolute p;',
  4405. 'begin',
  4406. ' if d=d then d:=d;',
  4407. ' if s=s then s:=s;',
  4408. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4409. ' if op.Index=op.Index then op.Index:=op.Index;',
  4410. 'end;',
  4411. 'begin']);
  4412. ConvertProgram;
  4413. CheckSource('TestProc_LocalVarAbsolute',
  4414. LinesToStr([ // statements
  4415. 'rtl.createClass($mod, "TObject", null, function () {',
  4416. ' this.$init = function () {',
  4417. ' this.Index = 0;',
  4418. ' };',
  4419. ' this.$final = function () {',
  4420. ' };',
  4421. ' this.DoAbs = function (Item) {',
  4422. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4423. ' };',
  4424. '});',
  4425. 'this.DoIt = function (i, p) {',
  4426. ' if (i === i) i = i;',
  4427. ' if (i === i) i = i;',
  4428. ' if (i.Index < i.Index) i.Index = i.Index;',
  4429. ' if (p.Index === p.Index) p.Index = p.Index;',
  4430. '};'
  4431. ]),
  4432. LinesToStr([
  4433. ]));
  4434. end;
  4435. procedure TTestModule.TestProc_LocalVarInit;
  4436. begin
  4437. StartProgram(false);
  4438. Add([
  4439. 'type TBytes = array of byte;',
  4440. 'procedure DoIt;',
  4441. 'const c = 4;',
  4442. 'var',
  4443. ' b: byte = 1;',
  4444. ' w: word = 2+c;',
  4445. ' p: pointer = nil;',
  4446. ' Buffer: TBytes = nil;',
  4447. 'begin',
  4448. 'end;',
  4449. 'begin']);
  4450. ConvertProgram;
  4451. CheckSource('TestProc_LocalVarInit',
  4452. LinesToStr([ // statements
  4453. 'var c = 4;',
  4454. 'this.DoIt = function () {',
  4455. ' var b = 1;',
  4456. ' var w = 2 + 4;',
  4457. ' var p = null;',
  4458. ' var Buffer = [];',
  4459. '};',
  4460. '']),
  4461. LinesToStr([
  4462. ]));
  4463. end;
  4464. procedure TTestModule.TestProc_ReservedWords;
  4465. begin
  4466. StartProgram(false);
  4467. Add([
  4468. 'procedure Date(ArrayBuffer: longint);',
  4469. 'const',
  4470. ' NaN: longint = 3;',
  4471. 'var',
  4472. ' &Boolean: longint;',
  4473. ' procedure Error(ArrayBuffer: longint);',
  4474. ' begin',
  4475. ' end;',
  4476. 'begin',
  4477. ' Nan:=&bOolean;',
  4478. 'end;',
  4479. 'begin',
  4480. ' Date(1);']);
  4481. ConvertProgram;
  4482. CheckSource('TestProc_ReservedWords',
  4483. LinesToStr([ // statements
  4484. 'var naN = 3;',
  4485. 'this.Date = function (arrayBuffer) {',
  4486. ' var boolean = 0;',
  4487. ' function error(arrayBuffer) {',
  4488. ' };',
  4489. ' naN = boolean;',
  4490. '};',
  4491. '']),
  4492. LinesToStr([
  4493. ' $mod.Date(1);'
  4494. ]));
  4495. end;
  4496. procedure TTestModule.TestProc_ConstRefWord;
  4497. begin
  4498. StartProgram(false);
  4499. Add([
  4500. 'procedure Run(constref w: word);',
  4501. 'var l: word;',
  4502. 'begin',
  4503. ' l:=w;',
  4504. ' Run(w);',
  4505. ' Run(l);',
  4506. 'end;',
  4507. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4508. 'begin',
  4509. ' Run(a);',
  4510. ' Run(b);',
  4511. ' Run(c);',
  4512. ' Run(d);',
  4513. ' Run(e);',
  4514. 'end;',
  4515. 'begin',
  4516. ' Run(1);']);
  4517. ConvertProgram;
  4518. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4519. CheckSource('TestProc_ConstRefWord',
  4520. LinesToStr([ // statements
  4521. 'this.Run = function (w) {',
  4522. ' var l = 0;',
  4523. ' l = w;',
  4524. ' $mod.Run(w);',
  4525. ' $mod.Run(l);',
  4526. '};',
  4527. 'this.Fly = function (a, b, c, d, e) {',
  4528. ' $mod.Run(a);',
  4529. ' $mod.Run(b.get());',
  4530. ' $mod.Run(c.get());',
  4531. ' $mod.Run(d);',
  4532. ' $mod.Run(e);',
  4533. '};',
  4534. '']),
  4535. LinesToStr([
  4536. '$mod.Run(1);'
  4537. ]));
  4538. end;
  4539. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4540. begin
  4541. StartProgram(false);
  4542. Add([
  4543. '{$mode objfpc}',
  4544. 'type',
  4545. ' TFunc = reference to function(x: word): word;',
  4546. 'var Func: TFunc;',
  4547. 'procedure DoIt(a: word);',
  4548. 'begin',
  4549. ' Func:=function(b:word): word',
  4550. ' begin',
  4551. ' Result:=a+b;',
  4552. ' exit(b);',
  4553. ' exit(Result);',
  4554. ' end;',// test semicolon
  4555. ' a:=3;',
  4556. 'end;',
  4557. 'begin',
  4558. ' Func:=function(c:word):word begin',
  4559. ' Result:=3+c;',
  4560. ' exit(c);',
  4561. ' exit(Result);',
  4562. ' end;']);
  4563. ConvertProgram;
  4564. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4565. LinesToStr([ // statements
  4566. 'this.Func = null;',
  4567. 'this.DoIt = function (a) {',
  4568. ' $mod.Func = function (b) {',
  4569. ' var Result = 0;',
  4570. ' Result = a + b;',
  4571. ' return b;',
  4572. ' return Result;',
  4573. ' return Result;',
  4574. ' };',
  4575. ' a = 3;',
  4576. '};',
  4577. '']),
  4578. LinesToStr([
  4579. '$mod.Func = function (c) {',
  4580. ' var Result = 0;',
  4581. ' Result = 3 + c;',
  4582. ' return c;',
  4583. ' return Result;',
  4584. ' return Result;',
  4585. '};',
  4586. '']));
  4587. end;
  4588. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4589. begin
  4590. StartProgram(false);
  4591. Add([
  4592. '{$mode delphi}',
  4593. 'type',
  4594. ' TProc = reference to procedure(x: word);',
  4595. 'procedure DoIt(a: word);',
  4596. 'var Proc: TProc;',
  4597. 'begin',
  4598. ' Proc:=procedure(b:word) begin end;',
  4599. 'end;',
  4600. 'var Proc: TProc;',
  4601. 'begin',
  4602. ' Proc:=procedure(c:word) begin end;',
  4603. '']);
  4604. ConvertProgram;
  4605. CheckSource('TestAnonymousProc_Assign_Delphi',
  4606. LinesToStr([ // statements
  4607. 'this.DoIt = function (a) {',
  4608. ' var Proc = null;',
  4609. ' Proc = function (b) {',
  4610. ' };',
  4611. '};',
  4612. 'this.Proc = null;',
  4613. '']),
  4614. LinesToStr([
  4615. '$mod.Proc = function (c) {',
  4616. '};',
  4617. '']));
  4618. end;
  4619. procedure TTestModule.TestAnonymousProc_Arg;
  4620. begin
  4621. StartProgram(false);
  4622. Add([
  4623. 'type',
  4624. ' TProc = reference to procedure;',
  4625. ' TFunc = reference to function(x: word): word;',
  4626. 'procedure DoMore(f,g: TProc);',
  4627. 'begin',
  4628. 'end;',
  4629. 'procedure DoOdd(v: jsvalue);',
  4630. 'begin',
  4631. 'end;',
  4632. 'procedure DoIt(f: TFunc);',
  4633. 'begin',
  4634. ' DoIt(function(b:word): word',
  4635. ' begin',
  4636. ' Result:=1+b;',
  4637. ' end);',
  4638. ' DoMore(procedure begin end, procedure begin end);',
  4639. ' DoOdd(procedure begin end);',
  4640. 'end;',
  4641. 'begin',
  4642. ' DoMore(procedure begin end,',
  4643. ' procedure assembler asm',
  4644. ' console.log("c");',
  4645. ' end);',
  4646. '']);
  4647. ConvertProgram;
  4648. CheckSource('TestAnonymousProc_Arg',
  4649. LinesToStr([ // statements
  4650. 'this.DoMore = function (f, g) {',
  4651. '};',
  4652. 'this.DoOdd = function (v) {',
  4653. '};',
  4654. 'this.DoIt = function (f) {',
  4655. ' $mod.DoIt(function (b) {',
  4656. ' var Result = 0;',
  4657. ' Result = 1 + b;',
  4658. ' return Result;',
  4659. ' });',
  4660. ' $mod.DoMore(function () {',
  4661. ' }, function () {',
  4662. ' });',
  4663. ' $mod.DoOdd(function () {',
  4664. ' });',
  4665. '};',
  4666. '']),
  4667. LinesToStr([
  4668. '$mod.DoMore(function () {',
  4669. '}, function () {',
  4670. ' console.log("c");',
  4671. '});',
  4672. '']));
  4673. end;
  4674. procedure TTestModule.TestAnonymousProc_Typecast;
  4675. begin
  4676. StartProgram(false);
  4677. Add([
  4678. 'type',
  4679. ' TProc = reference to procedure(w: word);',
  4680. ' TArr = array of word;',
  4681. ' TFuncArr = reference to function: TArr;',
  4682. 'procedure DoIt(p: TProc);',
  4683. 'var',
  4684. ' w: word;',
  4685. ' a: TArr;',
  4686. 'begin',
  4687. ' p:=TProc(procedure(b: smallint) begin end);',
  4688. ' a:=TFuncArr(function: TArr begin end)();',
  4689. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4690. 'end;',
  4691. 'begin']);
  4692. ConvertProgram;
  4693. CheckSource('TestAnonymousProc_Typecast',
  4694. LinesToStr([ // statements
  4695. 'this.DoIt = function (p) {',
  4696. ' var w = 0;',
  4697. ' var a = [];',
  4698. ' p = function (b) {',
  4699. ' };',
  4700. ' a = function () {',
  4701. ' var Result = [];',
  4702. ' return Result;',
  4703. ' }();',
  4704. ' w = function () {',
  4705. ' var Result = [];',
  4706. ' return Result;',
  4707. ' }()[3];',
  4708. '};',
  4709. '']),
  4710. LinesToStr([
  4711. '']));
  4712. end;
  4713. procedure TTestModule.TestAnonymousProc_With;
  4714. begin
  4715. StartProgram(false);
  4716. Add([
  4717. 'type',
  4718. ' TProc = reference to procedure(w: word);',
  4719. ' TObject = class',
  4720. ' b: boolean;',
  4721. ' end;',
  4722. 'var',
  4723. ' p: TProc;',
  4724. ' bird: TObject;',
  4725. 'begin',
  4726. ' with bird do',
  4727. ' p:=procedure(w: word)',
  4728. ' begin',
  4729. ' b:=w>2;',
  4730. ' end;',
  4731. '']);
  4732. ConvertProgram;
  4733. CheckSource('TestAnonymousProc_With',
  4734. LinesToStr([ // statements
  4735. 'rtl.createClass($mod, "TObject", null, function () {',
  4736. ' this.$init = function () {',
  4737. ' this.b = false;',
  4738. ' };',
  4739. ' this.$final = function () {',
  4740. ' };',
  4741. '});',
  4742. 'this.p = null;',
  4743. 'this.bird = null;',
  4744. '']),
  4745. LinesToStr([
  4746. 'var $with = $mod.bird;',
  4747. '$mod.p = function (w) {',
  4748. ' $with.b = w > 2;',
  4749. '};',
  4750. '']));
  4751. end;
  4752. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4753. begin
  4754. StartProgram(false);
  4755. Add([
  4756. 'type',
  4757. ' TProc = reference to procedure;',
  4758. ' TObject = class',
  4759. ' b: boolean;',
  4760. ' end;',
  4761. 'procedure DoIt;',
  4762. 'var',
  4763. ' p: TProc;',
  4764. 'begin',
  4765. ' try',
  4766. ' except',
  4767. ' on E: TObject do',
  4768. ' p:=procedure',
  4769. ' begin',
  4770. ' E.b:=true;',
  4771. ' end;',
  4772. ' end;',
  4773. 'end;',
  4774. 'begin']);
  4775. ConvertProgram;
  4776. CheckSource('TestAnonymousProc_ExceptOn',
  4777. LinesToStr([ // statements
  4778. 'rtl.createClass($mod, "TObject", null, function () {',
  4779. ' this.$init = function () {',
  4780. ' this.b = false;',
  4781. ' };',
  4782. ' this.$final = function () {',
  4783. ' };',
  4784. '});',
  4785. 'this.DoIt = function () {',
  4786. ' var p = null;',
  4787. ' try {} catch ($e) {',
  4788. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4789. ' var E = $e;',
  4790. ' p = function () {',
  4791. ' E.b = true;',
  4792. ' };',
  4793. ' } else throw $e',
  4794. ' };',
  4795. '};',
  4796. '']),
  4797. LinesToStr([
  4798. '']));
  4799. end;
  4800. procedure TTestModule.TestAnonymousProc_Nested;
  4801. begin
  4802. StartProgram(false);
  4803. Add([
  4804. 'type',
  4805. ' TProc = reference to procedure;',
  4806. ' TObject = class',
  4807. ' i: byte;',
  4808. ' procedure DoIt;',
  4809. ' end;',
  4810. 'procedure TObject.DoIt;',
  4811. 'var',
  4812. ' p: TProc;',
  4813. ' procedure Sub;',
  4814. ' begin',
  4815. ' p:=procedure',
  4816. ' begin',
  4817. ' i:=3;',
  4818. ' Self.i:=4;',
  4819. ' p:=procedure',
  4820. ' procedure SubSub;',
  4821. ' begin',
  4822. ' i:=13;',
  4823. ' Self.i:=14;',
  4824. ' end;',
  4825. ' begin',
  4826. ' i:=13;',
  4827. ' Self.i:=14;',
  4828. ' end;',
  4829. ' end;',
  4830. ' end;',
  4831. 'begin',
  4832. 'end;',
  4833. 'begin']);
  4834. ConvertProgram;
  4835. CheckSource('TestAnonymousProc_Nested',
  4836. LinesToStr([ // statements
  4837. 'rtl.createClass($mod, "TObject", null, function () {',
  4838. ' this.$init = function () {',
  4839. ' this.i = 0;',
  4840. ' };',
  4841. ' this.$final = function () {',
  4842. ' };',
  4843. ' this.DoIt = function () {',
  4844. ' var $Self = this;',
  4845. ' var p = null;',
  4846. ' function Sub() {',
  4847. ' p = function () {',
  4848. ' $Self.i = 3;',
  4849. ' $Self.i = 4;',
  4850. ' p = function () {',
  4851. ' function SubSub() {',
  4852. ' $Self.i = 13;',
  4853. ' $Self.i = 14;',
  4854. ' };',
  4855. ' $Self.i = 13;',
  4856. ' $Self.i = 14;',
  4857. ' };',
  4858. ' };',
  4859. ' };',
  4860. ' };',
  4861. '});',
  4862. '']),
  4863. LinesToStr([
  4864. '']));
  4865. end;
  4866. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4867. begin
  4868. StartProgram(false);
  4869. Add([
  4870. 'type',
  4871. ' TProc = reference to procedure;',
  4872. 'function DoIt: TProc;',
  4873. ' function Sub: TProc;',
  4874. ' begin',
  4875. ' Result:=procedure',
  4876. ' begin',
  4877. ' Sub:=procedure',
  4878. ' procedure SubSub;',
  4879. ' begin',
  4880. ' Result:=nil;',
  4881. ' Sub:=nil;',
  4882. ' DoIt:=nil;',
  4883. ' end;',
  4884. ' begin',
  4885. ' Result:=nil;',
  4886. ' Sub:=nil;',
  4887. ' DoIt:=nil;',
  4888. ' end;',
  4889. ' end;',
  4890. ' end;',
  4891. 'begin',
  4892. 'end;',
  4893. 'begin']);
  4894. ConvertProgram;
  4895. CheckSource('TestAnonymousProc_NestedAssignResult',
  4896. LinesToStr([ // statements
  4897. 'this.DoIt = function () {',
  4898. ' var Result = null;',
  4899. ' function Sub() {',
  4900. ' var Result$1 = null;',
  4901. ' Result$1 = function () {',
  4902. ' Result$1 = function () {',
  4903. ' function SubSub() {',
  4904. ' Result$1 = null;',
  4905. ' Result$1 = null;',
  4906. ' Result = null;',
  4907. ' };',
  4908. ' Result$1 = null;',
  4909. ' Result$1 = null;',
  4910. ' Result = null;',
  4911. ' };',
  4912. ' };',
  4913. ' return Result$1;',
  4914. ' };',
  4915. ' return Result;',
  4916. '};',
  4917. '']),
  4918. LinesToStr([
  4919. '']));
  4920. end;
  4921. procedure TTestModule.TestAnonymousProc_Class;
  4922. begin
  4923. StartProgram(false);
  4924. Add([
  4925. 'type',
  4926. ' TProc = reference to procedure;',
  4927. ' TEvent = procedure of object;',
  4928. ' TObject = class',
  4929. ' Size: word;',
  4930. ' function GetIt: TProc;',
  4931. ' procedure DoIt; virtual; abstract;',
  4932. ' end;',
  4933. 'function TObject.GetIt: TProc;',
  4934. 'begin',
  4935. ' Result:=procedure',
  4936. ' var p: TEvent;',
  4937. ' begin',
  4938. ' Size:=Size;',
  4939. ' Size:=Self.Size;',
  4940. ' p:=@DoIt;',
  4941. ' p:[email protected];',
  4942. ' end;',
  4943. 'end;',
  4944. 'begin']);
  4945. ConvertProgram;
  4946. CheckSource('TestAnonymousProc_Class',
  4947. LinesToStr([ // statements
  4948. 'rtl.createClass($mod, "TObject", null, function () {',
  4949. ' this.$init = function () {',
  4950. ' this.Size = 0;',
  4951. ' };',
  4952. ' this.$final = function () {',
  4953. ' };',
  4954. ' this.GetIt = function () {',
  4955. ' var $Self = this;',
  4956. ' var Result = null;',
  4957. ' Result = function () {',
  4958. ' var p = null;',
  4959. ' $Self.Size = $Self.Size;',
  4960. ' $Self.Size = $Self.Size;',
  4961. ' p = rtl.createCallback($Self, "DoIt");',
  4962. ' p = rtl.createCallback($Self, "DoIt");',
  4963. ' };',
  4964. ' return Result;',
  4965. ' };',
  4966. '});',
  4967. '']),
  4968. LinesToStr([
  4969. '']));
  4970. end;
  4971. procedure TTestModule.TestAnonymousProc_ForLoop;
  4972. begin
  4973. StartProgram(false);
  4974. Add([
  4975. 'type TProc = reference to procedure;',
  4976. 'procedure Foo(p: TProc);',
  4977. 'begin',
  4978. 'end;',
  4979. 'procedure DoIt;',
  4980. 'var i: word;',
  4981. ' a: word;',
  4982. 'begin',
  4983. ' for i:=1 to 10 do begin',
  4984. ' Foo(procedure begin a:=3; end);',
  4985. ' end;',
  4986. 'end;',
  4987. 'begin',
  4988. ' DoIt;']);
  4989. ConvertProgram;
  4990. CheckSource('TestAnonymousProc_ForLoop',
  4991. LinesToStr([ // statements
  4992. 'this.Foo = function (p) {',
  4993. '};',
  4994. 'this.DoIt = function () {',
  4995. ' var i = 0;',
  4996. ' var a = 0;',
  4997. ' for (i = 1; i <= 10; i++) {',
  4998. ' $mod.Foo(function () {',
  4999. ' a = 3;',
  5000. ' });',
  5001. ' };',
  5002. '};',
  5003. '']),
  5004. LinesToStr([
  5005. '$mod.DoIt();'
  5006. ]));
  5007. end;
  5008. procedure TTestModule.TestEnum_Name;
  5009. begin
  5010. StartProgram(false);
  5011. Add('type TMyEnum = (Red, Green, Blue);');
  5012. Add('var e: TMyEnum;');
  5013. Add('var f: TMyEnum = Blue;');
  5014. Add('begin');
  5015. Add(' e:=green;');
  5016. Add(' e:=default(TMyEnum);');
  5017. ConvertProgram;
  5018. CheckSource('TestEnumName',
  5019. LinesToStr([ // statements
  5020. 'this.TMyEnum = {',
  5021. ' "0":"Red",',
  5022. ' Red:0,',
  5023. ' "1":"Green",',
  5024. ' Green:1,',
  5025. ' "2":"Blue",',
  5026. ' Blue:2',
  5027. ' };',
  5028. 'this.e = 0;',
  5029. 'this.f = $mod.TMyEnum.Blue;'
  5030. ]),
  5031. LinesToStr([
  5032. '$mod.e=$mod.TMyEnum.Green;',
  5033. '$mod.e=$mod.TMyEnum.Red;'
  5034. ]));
  5035. end;
  5036. procedure TTestModule.TestEnum_Number;
  5037. begin
  5038. Converter.Options:=Converter.Options+[coEnumNumbers];
  5039. StartProgram(false);
  5040. Add('type TMyEnum = (Red, Green);');
  5041. Add('var');
  5042. Add(' e: TMyEnum;');
  5043. Add(' f: TMyEnum = Green;');
  5044. Add(' i: longint;');
  5045. Add('begin');
  5046. Add(' e:=green;');
  5047. Add(' i:=longint(e);');
  5048. ConvertProgram;
  5049. CheckSource('TestEnumNumber',
  5050. LinesToStr([ // statements
  5051. 'this.TMyEnum = {',
  5052. ' "0":"Red",',
  5053. ' Red:0,',
  5054. ' "1":"Green",',
  5055. ' Green:1',
  5056. ' };',
  5057. 'this.e = 0;',
  5058. 'this.f = 1;',
  5059. 'this.i = 0;'
  5060. ]),
  5061. LinesToStr([
  5062. '$mod.e=1;',
  5063. '$mod.i=$mod.e;'
  5064. ]));
  5065. end;
  5066. procedure TTestModule.TestEnum_ConstFail;
  5067. begin
  5068. StartProgram(false);
  5069. Add([
  5070. 'type TMyEnum = (Red = 100, Green = 101);',
  5071. 'var',
  5072. ' e: TMyEnum;',
  5073. ' f: TMyEnum = Green;',
  5074. 'begin',
  5075. ' e:=green;']);
  5076. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5077. ConvertProgram;
  5078. end;
  5079. procedure TTestModule.TestEnum_Functions;
  5080. begin
  5081. StartProgram(false);
  5082. Add([
  5083. 'type TMyEnum = (Red, Green);',
  5084. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5085. 'var',
  5086. ' v: longint;',
  5087. ' s: string;',
  5088. 'begin',
  5089. ' val(s,e,v);',
  5090. ' val(s,e,i);',
  5091. 'end;',
  5092. 'var',
  5093. ' e: TMyEnum;',
  5094. ' i: longint;',
  5095. ' s: string;',
  5096. ' b: boolean;',
  5097. 'begin',
  5098. ' i:=ord(red);',
  5099. ' i:=ord(green);',
  5100. ' i:=ord(e);',
  5101. ' i:=ord(b);',
  5102. ' e:=low(tmyenum);',
  5103. ' e:=low(e);',
  5104. ' b:=low(boolean);',
  5105. ' e:=high(tmyenum);',
  5106. ' e:=high(e);',
  5107. ' b:=high(boolean);',
  5108. ' e:=pred(green);',
  5109. ' e:=pred(e);',
  5110. ' b:=pred(b);',
  5111. ' e:=succ(red);',
  5112. ' e:=succ(e);',
  5113. ' b:=succ(b);',
  5114. ' e:=tmyenum(1);',
  5115. ' e:=tmyenum(i);',
  5116. ' s:=str(e);',
  5117. ' str(e,s);',
  5118. ' str(red,s);',
  5119. ' s:=str(e:3);',
  5120. ' writestr(s,e:3,red);',
  5121. ' val(s,e,i);',
  5122. ' e:=TMyEnum(i);',
  5123. ' i:=longint(e);']);
  5124. ConvertProgram;
  5125. CheckSource('TestEnum_Functions',
  5126. LinesToStr([ // statements
  5127. 'this.TMyEnum = {',
  5128. ' "0":"Red",',
  5129. ' Red:0,',
  5130. ' "1":"Green",',
  5131. ' Green:1',
  5132. ' };',
  5133. 'this.DoIt = function (e, i) {',
  5134. ' var v = 0;',
  5135. ' var s = "";',
  5136. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5137. ' v = w;',
  5138. ' }));',
  5139. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5140. '};',
  5141. 'this.e = 0;',
  5142. 'this.i = 0;',
  5143. 'this.s = "";',
  5144. 'this.b = false;',
  5145. '']),
  5146. LinesToStr([
  5147. '$mod.i=$mod.TMyEnum.Red;',
  5148. '$mod.i=$mod.TMyEnum.Green;',
  5149. '$mod.i=$mod.e;',
  5150. '$mod.i=$mod.b+0;',
  5151. '$mod.e=$mod.TMyEnum.Red;',
  5152. '$mod.e=$mod.TMyEnum.Red;',
  5153. '$mod.b=false;',
  5154. '$mod.e=$mod.TMyEnum.Green;',
  5155. '$mod.e=$mod.TMyEnum.Green;',
  5156. '$mod.b=true;',
  5157. '$mod.e=$mod.TMyEnum.Green-1;',
  5158. '$mod.e=$mod.e-1;',
  5159. '$mod.b=false;',
  5160. '$mod.e=$mod.TMyEnum.Red+1;',
  5161. '$mod.e=$mod.e+1;',
  5162. '$mod.b=true;',
  5163. '$mod.e=1;',
  5164. '$mod.e=$mod.i;',
  5165. '$mod.s = $mod.TMyEnum[$mod.e];',
  5166. '$mod.s = $mod.TMyEnum[$mod.e];',
  5167. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5168. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5169. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5170. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5171. ' $mod.i = v;',
  5172. '});',
  5173. '$mod.e=$mod.i;',
  5174. '$mod.i=$mod.e;',
  5175. '']));
  5176. end;
  5177. procedure TTestModule.TestEnum_AsParams;
  5178. begin
  5179. StartProgram(false);
  5180. Add('type TEnum = (Red,Blue);');
  5181. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5182. Add('var vJ: TEnum;');
  5183. Add('begin');
  5184. Add(' vg:=vg;');
  5185. Add(' vj:=vh;');
  5186. Add(' vi:=vi;');
  5187. Add(' doit(vg,vg,vg);');
  5188. Add(' doit(vh,vh,vj);');
  5189. Add(' doit(vi,vi,vi);');
  5190. Add(' doit(vj,vj,vj);');
  5191. Add('end;');
  5192. Add('var i: TEnum;');
  5193. Add('begin');
  5194. Add(' doit(i,i,i);');
  5195. ConvertProgram;
  5196. CheckSource('TestEnum_AsParams',
  5197. LinesToStr([ // statements
  5198. 'this.TEnum = {',
  5199. ' "0": "Red",',
  5200. ' Red: 0,',
  5201. ' "1": "Blue",',
  5202. ' Blue: 1',
  5203. '};',
  5204. 'this.DoIt = function (vG,vH,vI) {',
  5205. ' var vJ = 0;',
  5206. ' vG = vG;',
  5207. ' vJ = vH;',
  5208. ' vI.set(vI.get());',
  5209. ' $mod.DoIt(vG, vG, {',
  5210. ' get: function () {',
  5211. ' return vG;',
  5212. ' },',
  5213. ' set: function (v) {',
  5214. ' vG = v;',
  5215. ' }',
  5216. ' });',
  5217. ' $mod.DoIt(vH, vH, {',
  5218. ' get: function () {',
  5219. ' return vJ;',
  5220. ' },',
  5221. ' set: function (v) {',
  5222. ' vJ = v;',
  5223. ' }',
  5224. ' });',
  5225. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5226. ' $mod.DoIt(vJ, vJ, {',
  5227. ' get: function () {',
  5228. ' return vJ;',
  5229. ' },',
  5230. ' set: function (v) {',
  5231. ' vJ = v;',
  5232. ' }',
  5233. ' });',
  5234. '};',
  5235. 'this.i = 0;'
  5236. ]),
  5237. LinesToStr([
  5238. '$mod.DoIt($mod.i,$mod.i,{',
  5239. ' p: $mod,',
  5240. ' get: function () {',
  5241. ' return this.p.i;',
  5242. ' },',
  5243. ' set: function (v) {',
  5244. ' this.p.i = v;',
  5245. ' }',
  5246. '});'
  5247. ]));
  5248. end;
  5249. procedure TTestModule.TestEnumRange_Array;
  5250. begin
  5251. StartProgram(false);
  5252. Add([
  5253. 'type',
  5254. ' TEnum = (Red, Green, Blue);',
  5255. ' TEnumRg = green..blue;',
  5256. ' TArr = array[TEnumRg] of byte;',
  5257. ' TArr2 = array[green..blue] of byte;',
  5258. 'var',
  5259. ' a: TArr;',
  5260. ' b: TArr = (3,4);',
  5261. ' c: TArr2 = (5,6);',
  5262. 'begin',
  5263. ' a[green] := b[blue];',
  5264. ' c[green] := c[blue];',
  5265. '']);
  5266. ConvertProgram;
  5267. CheckSource('TestEnumRange_Array',
  5268. LinesToStr([ // statements
  5269. 'this.TEnum = {',
  5270. ' "0": "Red",',
  5271. ' Red: 0,',
  5272. ' "1": "Green",',
  5273. ' Green: 1,',
  5274. ' "2": "Blue",',
  5275. ' Blue: 2',
  5276. '};',
  5277. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5278. 'this.b = [3, 4];',
  5279. 'this.c = [5, 6];',
  5280. '']),
  5281. LinesToStr([
  5282. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5283. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5284. '']));
  5285. end;
  5286. procedure TTestModule.TestEnum_ForIn;
  5287. begin
  5288. StartProgram(false);
  5289. Add([
  5290. 'type',
  5291. ' TEnum = (Red, Green, Blue);',
  5292. ' TEnumRg = green..blue;',
  5293. ' TArr = array[TEnum] of byte;',
  5294. ' TArrRg = array[TEnumRg] of byte;',
  5295. 'var',
  5296. ' e: TEnum;',
  5297. ' a1: TArr = (3,4,5);',
  5298. ' a2: TArrRg = (11,12);',
  5299. ' b: byte;',
  5300. 'begin',
  5301. ' for e in TEnum do ;',
  5302. ' for e in TEnumRg do ;',
  5303. ' for e in TArr do ;',
  5304. ' for e in TArrRg do ;',
  5305. ' for b in a1 do ;',
  5306. ' for b in a2 do ;',
  5307. '']);
  5308. ConvertProgram;
  5309. CheckSource('TestEnum_ForIn',
  5310. LinesToStr([ // statements
  5311. 'this.TEnum = {',
  5312. ' "0": "Red",',
  5313. ' Red: 0,',
  5314. ' "1": "Green",',
  5315. ' Green: 1,',
  5316. ' "2": "Blue",',
  5317. ' Blue: 2',
  5318. '};',
  5319. 'this.e = 0;',
  5320. 'this.a1 = [3, 4, 5];',
  5321. 'this.a2 = [11, 12];',
  5322. 'this.b = 0;',
  5323. '']),
  5324. LinesToStr([
  5325. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5326. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5327. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5328. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5329. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5330. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5331. '']));
  5332. end;
  5333. procedure TTestModule.TestEnum_ScopedNumber;
  5334. begin
  5335. Converter.Options:=Converter.Options+[coEnumNumbers];
  5336. StartProgram(false);
  5337. Add([
  5338. 'type',
  5339. ' TEnum = (Red, Green);',
  5340. 'var',
  5341. ' e: TEnum;',
  5342. 'begin',
  5343. ' e:=TEnum.Green;',
  5344. '']);
  5345. ConvertProgram;
  5346. CheckSource('TestEnum_ScopedNumber',
  5347. LinesToStr([ // statements
  5348. 'this.TEnum = {',
  5349. ' "0": "Red",',
  5350. ' Red: 0,',
  5351. ' "1": "Green",',
  5352. ' Green: 1',
  5353. '};',
  5354. 'this.e = 0;',
  5355. '']),
  5356. LinesToStr([
  5357. '$mod.e = 1;']));
  5358. end;
  5359. procedure TTestModule.TestEnum_InFunction;
  5360. begin
  5361. StartProgram(false);
  5362. Add([
  5363. 'const TEnum = 3;',
  5364. 'procedure DoIt;',
  5365. 'type',
  5366. ' TEnum = (Red, Green, Blue);',
  5367. ' procedure Sub;',
  5368. ' type',
  5369. ' TEnumSub = (Left, Right);',
  5370. ' var',
  5371. ' es: TEnumSub;',
  5372. ' begin',
  5373. ' es:=Left;',
  5374. ' end;',
  5375. 'var',
  5376. ' e, e2: TEnum;',
  5377. 'begin',
  5378. ' if e in [red,blue] then e2:=e;',
  5379. 'end;',
  5380. 'begin']);
  5381. ConvertProgram;
  5382. CheckSource('TestEnum_InFunction',
  5383. LinesToStr([ // statements
  5384. 'this.TEnum = 3;',
  5385. 'var TEnum$1 = {',
  5386. ' "0":"Red",',
  5387. ' Red:0,',
  5388. ' "1":"Green",',
  5389. ' Green:1,',
  5390. ' "2":"Blue",',
  5391. ' Blue:2',
  5392. ' };',
  5393. 'var TEnumSub = {',
  5394. ' "0": "Left",',
  5395. ' Left: 0,',
  5396. ' "1": "Right",',
  5397. ' Right: 1',
  5398. '};',
  5399. 'this.DoIt = function () {',
  5400. ' function Sub() {',
  5401. ' var es = 0;',
  5402. ' es = TEnumSub.Left;',
  5403. ' };',
  5404. ' var e = 0;',
  5405. ' var e2 = 0;',
  5406. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5407. '};',
  5408. '']),
  5409. LinesToStr([
  5410. '']));
  5411. end;
  5412. procedure TTestModule.TestSet_Enum;
  5413. begin
  5414. StartProgram(false);
  5415. Add([
  5416. 'type',
  5417. ' TColor = (Red, Green, Blue);',
  5418. ' TColors = set of TColor;',
  5419. 'var',
  5420. ' c: TColor;',
  5421. ' s: TColors;',
  5422. ' t: TColors = [];',
  5423. ' u: TColors = [Red];',
  5424. 'begin',
  5425. ' s:=[];',
  5426. ' s:=[Green];',
  5427. ' s:=[Green,Blue];',
  5428. ' s:=[Red..Blue];',
  5429. ' s:=[Red,Green..Blue];',
  5430. ' s:=[Red,c];',
  5431. ' s:=t;',
  5432. ' s:=default(TColors);',
  5433. '']);
  5434. ConvertProgram;
  5435. CheckSource('TestSet',
  5436. LinesToStr([ // statements
  5437. 'this.TColor = {',
  5438. ' "0":"Red",',
  5439. ' Red:0,',
  5440. ' "1":"Green",',
  5441. ' Green:1,',
  5442. ' "2":"Blue",',
  5443. ' Blue:2',
  5444. ' };',
  5445. 'this.c = 0;',
  5446. 'this.s = {};',
  5447. 'this.t = {};',
  5448. 'this.u = rtl.createSet($mod.TColor.Red);'
  5449. ]),
  5450. LinesToStr([
  5451. '$mod.s={};',
  5452. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5453. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5454. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5455. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5456. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5457. '$mod.s=rtl.refSet($mod.t);',
  5458. '$mod.s={};',
  5459. '']));
  5460. end;
  5461. procedure TTestModule.TestSet_Operators;
  5462. begin
  5463. StartProgram(false);
  5464. Add('type');
  5465. Add(' TColor = (Red, Green, Blue);');
  5466. Add(' TColors = set of tcolor;');
  5467. Add('var');
  5468. Add(' vC: TColor;');
  5469. Add(' vS: TColors;');
  5470. Add(' vT: TColors;');
  5471. Add(' vU: TColors;');
  5472. Add(' B: boolean;');
  5473. Add('begin');
  5474. Add(' include(vs,green);');
  5475. Add(' exclude(vs,vc);');
  5476. Add(' vs:=vt+vu;');
  5477. Add(' vs:=vt+[red];');
  5478. Add(' vs:=[red]+vt;');
  5479. Add(' vs:=[red]+[green];');
  5480. Add(' vs:=vt-vu;');
  5481. Add(' vs:=vt-[red];');
  5482. Add(' vs:=[red]-vt;');
  5483. Add(' vs:=[red]-[green];');
  5484. Add(' vs:=vt*vu;');
  5485. Add(' vs:=vt*[red];');
  5486. Add(' vs:=[red]*vt;');
  5487. Add(' vs:=[red]*[green];');
  5488. Add(' vs:=vt><vu;');
  5489. Add(' vs:=vt><[red];');
  5490. Add(' vs:=[red]><vt;');
  5491. Add(' vs:=[red]><[green];');
  5492. Add(' b:=vt=vu;');
  5493. Add(' b:=vt=[red];');
  5494. Add(' b:=[red]=vt;');
  5495. Add(' b:=[red]=[green];');
  5496. Add(' b:=vt<>vu;');
  5497. Add(' b:=vt<>[red];');
  5498. Add(' b:=[red]<>vt;');
  5499. Add(' b:=[red]<>[green];');
  5500. Add(' b:=vt<=vu;');
  5501. Add(' b:=vt<=[red];');
  5502. Add(' b:=[red]<=vt;');
  5503. Add(' b:=[red]<=[green];');
  5504. Add(' b:=vt>=vu;');
  5505. Add(' b:=vt>=[red];');
  5506. Add(' b:=[red]>=vt;');
  5507. Add(' b:=[red]>=[green];');
  5508. ConvertProgram;
  5509. CheckSource('TestSet_Operators',
  5510. LinesToStr([ // statements
  5511. 'this.TColor = {',
  5512. ' "0":"Red",',
  5513. ' Red:0,',
  5514. ' "1":"Green",',
  5515. ' Green:1,',
  5516. ' "2":"Blue",',
  5517. ' Blue:2',
  5518. ' };',
  5519. 'this.vC = 0;',
  5520. 'this.vS = {};',
  5521. 'this.vT = {};',
  5522. 'this.vU = {};',
  5523. 'this.B = false;'
  5524. ]),
  5525. LinesToStr([
  5526. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5527. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5528. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5529. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5530. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5531. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5532. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5533. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5534. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5535. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5536. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5537. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5538. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5539. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5540. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5541. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5542. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5543. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5544. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5545. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5546. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5547. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5548. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5549. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5550. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5551. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5552. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5553. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5554. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5555. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5556. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5557. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5558. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5559. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5560. '']));
  5561. end;
  5562. procedure TTestModule.TestSet_Operator_In;
  5563. begin
  5564. StartProgram(false);
  5565. Add('type');
  5566. Add(' TColor = (Red, Green, Blue);');
  5567. Add(' TColors = set of tcolor;');
  5568. Add('var');
  5569. Add(' vC: tcolor;');
  5570. Add(' vT: tcolors;');
  5571. Add(' B: boolean;');
  5572. Add('begin');
  5573. Add(' b:=red in vt;');
  5574. Add(' b:=vc in vt;');
  5575. Add(' b:=green in [red..blue];');
  5576. Add(' b:=vc in [red..blue];');
  5577. Add(' ');
  5578. Add(' if red in vt then ;');
  5579. Add(' while vC in vt do ;');
  5580. Add(' repeat');
  5581. Add(' until vC in vt;');
  5582. ConvertProgram;
  5583. CheckSource('TestSet_Operator_In',
  5584. LinesToStr([ // statements
  5585. 'this.TColor = {',
  5586. ' "0":"Red",',
  5587. ' Red:0,',
  5588. ' "1":"Green",',
  5589. ' Green:1,',
  5590. ' "2":"Blue",',
  5591. ' Blue:2',
  5592. ' };',
  5593. 'this.vC = 0;',
  5594. 'this.vT = {};',
  5595. 'this.B = false;'
  5596. ]),
  5597. LinesToStr([
  5598. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5599. '$mod.B = $mod.vC in $mod.vT;',
  5600. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5601. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5602. 'if ($mod.TColor.Red in $mod.vT) ;',
  5603. 'while ($mod.vC in $mod.vT) {',
  5604. '};',
  5605. 'do {',
  5606. '} while (!($mod.vC in $mod.vT));',
  5607. '']));
  5608. end;
  5609. procedure TTestModule.TestSet_Functions;
  5610. begin
  5611. StartProgram(false);
  5612. Add('type');
  5613. Add(' TMyEnum = (Red, Green);');
  5614. Add(' TMyEnums = set of TMyEnum;');
  5615. Add('var');
  5616. Add(' e: TMyEnum;');
  5617. Add(' s: TMyEnums;');
  5618. Add('begin');
  5619. Add(' e:=Low(TMyEnums);');
  5620. Add(' e:=Low(s);');
  5621. Add(' e:=High(TMyEnums);');
  5622. Add(' e:=High(s);');
  5623. ConvertProgram;
  5624. CheckSource('TestSetFunctions',
  5625. LinesToStr([ // statements
  5626. 'this.TMyEnum = {',
  5627. ' "0":"Red",',
  5628. ' Red:0,',
  5629. ' "1":"Green",',
  5630. ' Green:1',
  5631. ' };',
  5632. 'this.e = 0;',
  5633. 'this.s = {};'
  5634. ]),
  5635. LinesToStr([
  5636. '$mod.e=$mod.TMyEnum.Red;',
  5637. '$mod.e=$mod.TMyEnum.Red;',
  5638. '$mod.e=$mod.TMyEnum.Green;',
  5639. '$mod.e=$mod.TMyEnum.Green;',
  5640. '']));
  5641. end;
  5642. procedure TTestModule.TestSet_PassAsArgClone;
  5643. begin
  5644. StartProgram(false);
  5645. Add('type');
  5646. Add(' TMyEnum = (Red, Green);');
  5647. Add(' TMyEnums = set of TMyEnum;');
  5648. Add('procedure DoDefault(s: tmyenums); begin end;');
  5649. Add('procedure DoConst(const s: tmyenums); begin end;');
  5650. Add('var');
  5651. Add(' aSet: tmyenums;');
  5652. Add('begin');
  5653. Add(' dodefault(aset);');
  5654. Add(' doconst(aset);');
  5655. ConvertProgram;
  5656. CheckSource('TestSetFunctions',
  5657. LinesToStr([ // statements
  5658. 'this.TMyEnum = {',
  5659. ' "0":"Red",',
  5660. ' Red:0,',
  5661. ' "1":"Green",',
  5662. ' Green:1',
  5663. ' };',
  5664. 'this.DoDefault = function (s) {',
  5665. '};',
  5666. 'this.DoConst = function (s) {',
  5667. '};',
  5668. 'this.aSet = {};'
  5669. ]),
  5670. LinesToStr([
  5671. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5672. '$mod.DoConst($mod.aSet);',
  5673. '']));
  5674. end;
  5675. procedure TTestModule.TestSet_AsParams;
  5676. begin
  5677. StartProgram(false);
  5678. Add([
  5679. 'type TEnum = (Red,Blue);',
  5680. 'type TEnums = set of TEnum;',
  5681. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5682. 'var vJ: TEnums;',
  5683. 'begin',
  5684. ' Include(vg,red);',
  5685. ' Include(result,blue);',
  5686. ' vg:=vg;',
  5687. ' vj:=vh;',
  5688. ' vi:=vi;',
  5689. ' doit(vg,vg,vg);',
  5690. ' doit(vh,vh,vj);',
  5691. ' doit(vi,vi,vi);',
  5692. ' doit(vj,vj,vj);',
  5693. 'end;',
  5694. 'var i: TEnums;',
  5695. 'begin',
  5696. ' doit(i,i,i);']);
  5697. ConvertProgram;
  5698. CheckSource('TestSet_AsParams',
  5699. LinesToStr([ // statements
  5700. 'this.TEnum = {',
  5701. ' "0": "Red",',
  5702. ' Red: 0,',
  5703. ' "1": "Blue",',
  5704. ' Blue: 1',
  5705. '};',
  5706. 'this.DoIt = function (vG,vH,vI) {',
  5707. ' var Result = {};',
  5708. ' var vJ = {};',
  5709. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5710. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5711. ' vG = rtl.refSet(vG);',
  5712. ' vJ = rtl.refSet(vH);',
  5713. ' vI.set(rtl.refSet(vI.get()));',
  5714. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5715. ' get: function () {',
  5716. ' return vG;',
  5717. ' },',
  5718. ' set: function (v) {',
  5719. ' vG = v;',
  5720. ' }',
  5721. ' });',
  5722. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5723. ' get: function () {',
  5724. ' return vJ;',
  5725. ' },',
  5726. ' set: function (v) {',
  5727. ' vJ = v;',
  5728. ' }',
  5729. ' });',
  5730. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5731. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5732. ' get: function () {',
  5733. ' return vJ;',
  5734. ' },',
  5735. ' set: function (v) {',
  5736. ' vJ = v;',
  5737. ' }',
  5738. ' });',
  5739. ' return Result;',
  5740. '};',
  5741. 'this.i = {};'
  5742. ]),
  5743. LinesToStr([
  5744. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5745. ' p: $mod,',
  5746. ' get: function () {',
  5747. ' return this.p.i;',
  5748. ' },',
  5749. ' set: function (v) {',
  5750. ' this.p.i = v;',
  5751. ' }',
  5752. '});'
  5753. ]));
  5754. end;
  5755. procedure TTestModule.TestSet_Property;
  5756. begin
  5757. StartProgram(false);
  5758. Add('type');
  5759. Add(' TEnum = (Red,Blue);');
  5760. Add(' TEnums = set of TEnum;');
  5761. Add(' TObject = class');
  5762. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5763. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5764. Add(' property Colors: TEnums read GetColors write SetColors;');
  5765. Add(' end;');
  5766. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5767. Add('begin end;');
  5768. Add('var Obj: TObject;');
  5769. Add('begin');
  5770. Add(' Include(Obj.Colors,Red);');
  5771. Add(' Exclude(Obj.Colors,Red);');
  5772. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5773. ConvertProgram;
  5774. CheckSource('TestSet_Property',
  5775. LinesToStr([ // statements
  5776. 'this.TEnum = {',
  5777. ' "0": "Red",',
  5778. ' Red: 0,',
  5779. ' "1": "Blue",',
  5780. ' Blue: 1',
  5781. '};',
  5782. 'rtl.createClass($mod, "TObject", null, function () {',
  5783. ' this.$init = function () {',
  5784. ' };',
  5785. ' this.$final = function () {',
  5786. ' };',
  5787. '});',
  5788. 'this.DoIt = function (i, j, k, l) {',
  5789. '};',
  5790. 'this.Obj = null;',
  5791. '']),
  5792. LinesToStr([
  5793. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5794. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5795. '']));
  5796. end;
  5797. procedure TTestModule.TestSet_EnumConst;
  5798. begin
  5799. StartProgram(false);
  5800. Add([
  5801. 'type',
  5802. ' TEnum = (Red,Blue);',
  5803. ' TEnums = set of TEnum;',
  5804. 'const',
  5805. ' Orange = red;',
  5806. 'var',
  5807. ' Enum: tenum;',
  5808. ' Enums: tenums;',
  5809. 'begin',
  5810. ' Include(enums,orange);',
  5811. ' Exclude(enums,orange);',
  5812. ' if orange in enums then;',
  5813. ' if orange in [orange,red] then;']);
  5814. ConvertProgram;
  5815. CheckSource('TestSet_EnumConst',
  5816. LinesToStr([ // statements
  5817. 'this.TEnum = {',
  5818. ' "0": "Red",',
  5819. ' Red: 0,',
  5820. ' "1": "Blue",',
  5821. ' Blue: 1',
  5822. '};',
  5823. 'this.Orange = $mod.TEnum.Red;',
  5824. 'this.Enum = 0;',
  5825. 'this.Enums = {};',
  5826. '']),
  5827. LinesToStr([
  5828. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5829. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5830. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5831. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5832. '']));
  5833. end;
  5834. procedure TTestModule.TestSet_IntConst;
  5835. begin
  5836. StartProgram(false);
  5837. Add([
  5838. 'type',
  5839. ' TEnums = set of Byte;',
  5840. 'const',
  5841. ' Orange = 0;',
  5842. 'var',
  5843. ' Enum: byte;',
  5844. ' Enums: tenums;',
  5845. 'begin',
  5846. ' Enums:=[];',
  5847. ' Enums:=[0];',
  5848. ' Enums:=[1..2];',
  5849. //' Include(enums,orange);',
  5850. //' Exclude(enums,orange);',
  5851. ' if orange in enums then;',
  5852. ' if orange in [orange,1] then;']);
  5853. ConvertProgram;
  5854. CheckSource('TestSet_IntConst',
  5855. LinesToStr([ // statements
  5856. 'this.Orange = 0;',
  5857. 'this.Enum = 0;',
  5858. 'this.Enums = {};',
  5859. '']),
  5860. LinesToStr([
  5861. '$mod.Enums = {};',
  5862. '$mod.Enums = rtl.createSet(0);',
  5863. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5864. 'if (0 in $mod.Enums) ;',
  5865. 'if (0 in rtl.createSet(0, 1)) ;',
  5866. '']));
  5867. end;
  5868. procedure TTestModule.TestSet_AnonymousEnumType;
  5869. begin
  5870. StartProgram(false);
  5871. Add('type');
  5872. Add(' TFlags = set of (red, green);');
  5873. Add('const');
  5874. Add(' favorite = red;');
  5875. Add('var');
  5876. Add(' f: TFlags;');
  5877. Add(' i: longint;');
  5878. Add('begin');
  5879. Add(' Include(f,red);');
  5880. Add(' Include(f,favorite);');
  5881. Add(' i:=ord(red);');
  5882. Add(' i:=ord(favorite);');
  5883. Add(' i:=ord(low(TFlags));');
  5884. Add(' i:=ord(low(f));');
  5885. Add(' i:=ord(low(favorite));');
  5886. Add(' i:=ord(high(TFlags));');
  5887. Add(' i:=ord(high(f));');
  5888. Add(' i:=ord(high(favorite));');
  5889. Add(' f:=[green,favorite];');
  5890. ConvertProgram;
  5891. CheckSource('TestSet_AnonymousEnumType',
  5892. LinesToStr([ // statements
  5893. 'this.TFlags$a = {',
  5894. ' "0": "red",',
  5895. ' red: 0,',
  5896. ' "1": "green",',
  5897. ' green: 1',
  5898. '};',
  5899. 'this.favorite = $mod.TFlags$a.red;',
  5900. 'this.f = {};',
  5901. 'this.i = 0;',
  5902. '']),
  5903. LinesToStr([
  5904. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5905. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5906. '$mod.i = $mod.TFlags$a.red;',
  5907. '$mod.i = $mod.TFlags$a.red;',
  5908. '$mod.i = $mod.TFlags$a.red;',
  5909. '$mod.i = $mod.TFlags$a.red;',
  5910. '$mod.i = $mod.TFlags$a.red;',
  5911. '$mod.i = $mod.TFlags$a.green;',
  5912. '$mod.i = $mod.TFlags$a.green;',
  5913. '$mod.i = $mod.TFlags$a.green;',
  5914. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5915. '']));
  5916. end;
  5917. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5918. begin
  5919. exit;
  5920. StartProgram(false);
  5921. Add([
  5922. 'type',
  5923. ' TAtoZ = ''A''..''Z'';',
  5924. ' TSetOfAZ = set of TAtoZ;',
  5925. 'var',
  5926. ' c: char;',
  5927. ' a: TAtoZ;',
  5928. ' s: TSetOfAZ = [''P'',''A''];',
  5929. ' i: longint;',
  5930. 'begin',
  5931. ' Include(s,''S'');',
  5932. ' Include(s,c);',
  5933. ' Include(s,a);',
  5934. ' c:=low(TAtoZ);',
  5935. ' i:=ord(low(TAtoZ));',
  5936. ' a:=high(TAtoZ);',
  5937. ' a:=high(TSetOfAtoZ);',
  5938. ' s:=[a,c,''M''];',
  5939. '']);
  5940. ConvertProgram;
  5941. CheckSource('TestSet_AnonymousEnumTypeChar',
  5942. LinesToStr([ // statements
  5943. '']),
  5944. LinesToStr([
  5945. '']));
  5946. end;
  5947. procedure TTestModule.TestSet_ConstEnum;
  5948. begin
  5949. StartProgram(false);
  5950. Add([
  5951. 'type',
  5952. ' TEnum = (red,blue,green);',
  5953. ' TEnums = set of TEnum;',
  5954. 'const',
  5955. ' teAny = [low(TEnum)..high(TEnum)];',
  5956. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5957. 'var',
  5958. ' e: TEnum;',
  5959. ' s: TEnums;',
  5960. 'begin',
  5961. ' if blue in teAny then;',
  5962. ' if blue in teAny+[e] then;',
  5963. ' if blue in teAny+teRedBlue then;',
  5964. ' if e in [red,blue] then;',
  5965. ' s:=teAny;',
  5966. ' s:=teAny+[e];',
  5967. ' s:=[e]+teAny;',
  5968. ' s:=teAny+teRedBlue;',
  5969. ' s:=teAny+teRedBlue+[e];',
  5970. '']);
  5971. ConvertProgram;
  5972. CheckSource('TestSet_ConstEnum',
  5973. LinesToStr([ // statements
  5974. 'this.TEnum = {',
  5975. ' "0": "red",',
  5976. ' red: 0,',
  5977. ' "1": "blue",',
  5978. ' blue: 1,',
  5979. ' "2": "green",',
  5980. ' green: 2',
  5981. '};',
  5982. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5983. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5984. 'this.e = 0;',
  5985. 'this.s = {};',
  5986. '']),
  5987. LinesToStr([
  5988. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5989. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5990. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5991. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5992. '$mod.s = rtl.refSet($mod.teAny);',
  5993. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5994. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5995. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5996. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5997. '']));
  5998. end;
  5999. procedure TTestModule.TestSet_ConstChar;
  6000. begin
  6001. StartProgram(false);
  6002. Add([
  6003. 'const',
  6004. ' LowChars = [''a''..''z''];',
  6005. ' Chars = LowChars+[''A''..''Z''];',
  6006. ' sc = [''А'', ''Я''];',
  6007. 'var',
  6008. ' c: char;',
  6009. ' s: string;',
  6010. 'begin',
  6011. ' if c in lowchars then ;',
  6012. ' if ''a'' in lowchars then ;',
  6013. ' if s[1] in lowchars then ;',
  6014. ' if c in chars then ;',
  6015. ' if c in [''a''..''z'',''_''] then ;',
  6016. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6017. ' if ''Я'' in sc then ;',
  6018. ' if 3=ord('' '') then ;',
  6019. '']);
  6020. ConvertProgram;
  6021. CheckSource('TestSet_ConstChar',
  6022. LinesToStr([ // statements
  6023. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6024. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  6025. 'this.sc = rtl.createSet(1040, 1071);',
  6026. 'this.c = "";',
  6027. 'this.s = "";',
  6028. '']),
  6029. LinesToStr([
  6030. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6031. 'if (97 in $mod.LowChars) ;',
  6032. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6033. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6034. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6035. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6036. 'if (1071 in $mod.sc) ;',
  6037. 'if (3 === 32) ;',
  6038. '']));
  6039. end;
  6040. procedure TTestModule.TestSet_ConstInt;
  6041. begin
  6042. StartProgram(false);
  6043. Add([
  6044. 'const',
  6045. ' Months = [1..12];',
  6046. ' Mirror = [-12..-1]+Months;',
  6047. 'var',
  6048. ' i: smallint;',
  6049. 'begin',
  6050. ' if 3 in Months then;',
  6051. ' if i in Months+[i] then;',
  6052. ' if i in Months+Mirror then;',
  6053. ' if i in [4..6,8] then;',
  6054. '']);
  6055. ConvertProgram;
  6056. CheckSource('TestSet_ConstInt',
  6057. LinesToStr([ // statements
  6058. 'this.Months = rtl.createSet(null, 1, 12);',
  6059. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  6060. 'this.i = 0;',
  6061. '']),
  6062. LinesToStr([
  6063. 'if (3 in $mod.Months) ;',
  6064. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6065. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6066. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6067. '']));
  6068. end;
  6069. procedure TTestModule.TestSet_InFunction;
  6070. begin
  6071. StartProgram(false);
  6072. Add([
  6073. 'const',
  6074. ' TEnum = 3;',
  6075. ' TSetOfEnum = 4;',
  6076. ' TSetOfAno = 5;',
  6077. 'procedure DoIt;',
  6078. 'type',
  6079. ' TEnum = (red, blue);',
  6080. ' TSetOfEnum = set of TEnum;',
  6081. ' TSetOfAno = set of (up,down);',
  6082. 'var',
  6083. ' e: TEnum;',
  6084. ' se: TSetOfEnum;',
  6085. ' sa: TSetOfAno;',
  6086. 'begin',
  6087. ' se:=[e];',
  6088. ' sa:=[up];',
  6089. 'end;',
  6090. 'begin',
  6091. '']);
  6092. ConvertProgram;
  6093. CheckSource('TestSet_InFunction',
  6094. LinesToStr([ // statements
  6095. 'this.TEnum = 3;',
  6096. 'this.TSetOfEnum = 4;',
  6097. 'this.TSetOfAno = 5;',
  6098. 'var TEnum$1 = {',
  6099. ' "0": "red",',
  6100. ' red: 0,',
  6101. ' "1": "blue",',
  6102. ' blue: 1',
  6103. '};',
  6104. 'var TSetOfAno$a = {',
  6105. ' "0": "up",',
  6106. ' up: 0,',
  6107. ' "1": "down",',
  6108. ' down: 1',
  6109. '};',
  6110. 'this.DoIt = function () {',
  6111. ' var e = 0;',
  6112. ' var se = {};',
  6113. ' var sa = {};',
  6114. ' se = rtl.createSet(e);',
  6115. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6116. '};',
  6117. '']),
  6118. LinesToStr([
  6119. '']));
  6120. end;
  6121. procedure TTestModule.TestSet_ForIn;
  6122. begin
  6123. StartProgram(false);
  6124. Add([
  6125. 'type',
  6126. ' TEnum = (Red, Green, Blue);',
  6127. ' TEnumRg = green..blue;',
  6128. ' TSetOfEnum = set of TEnum;',
  6129. ' TSetOfEnumRg = set of TEnumRg;',
  6130. 'var',
  6131. ' e, e2: TEnum;',
  6132. ' er: TEnum;',
  6133. ' s: TSetOfEnum;',
  6134. 'begin',
  6135. ' for e in TSetOfEnum do ;',
  6136. ' for e in TSetOfEnumRg do ;',
  6137. ' for e in [] do e2:=e;',
  6138. ' for e in [red..green] do e2:=e;',
  6139. ' for e in [green,blue] do e2:=e;',
  6140. ' for e in [red,blue] do e2:=e;',
  6141. ' for e in s do e2:=e;',
  6142. ' for er in TSetOfEnumRg do ;',
  6143. '']);
  6144. ConvertProgram;
  6145. CheckSource('TestSet_ForIn',
  6146. LinesToStr([ // statements
  6147. 'this.TEnum = {',
  6148. ' "0":"Red",',
  6149. ' Red:0,',
  6150. ' "1":"Green",',
  6151. ' Green:1,',
  6152. ' "2":"Blue",',
  6153. ' Blue:2',
  6154. ' };',
  6155. 'this.e = 0;',
  6156. 'this.e2 = 0;',
  6157. 'this.er = 0;',
  6158. 'this.s = {};',
  6159. '']),
  6160. LinesToStr([
  6161. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6162. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6163. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6164. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6165. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6166. 'for (var $l in $mod.s){',
  6167. ' $mod.e = +$l;',
  6168. ' $mod.e2 = $mod.e;',
  6169. '};',
  6170. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6171. '']));
  6172. end;
  6173. procedure TTestModule.TestNestBegin;
  6174. begin
  6175. StartProgram(false);
  6176. Add('begin');
  6177. Add(' begin');
  6178. Add(' begin');
  6179. Add(' end;');
  6180. Add(' begin');
  6181. Add(' if true then ;');
  6182. Add(' end;');
  6183. Add(' end;');
  6184. ConvertProgram;
  6185. CheckSource('TestNestBegin',
  6186. '',
  6187. 'if (true) ;');
  6188. end;
  6189. procedure TTestModule.TestUnitImplVars;
  6190. begin
  6191. StartUnit(false);
  6192. Add('interface');
  6193. Add('implementation');
  6194. Add('var');
  6195. Add(' V1:longint;');
  6196. Add(' V2:longint = 3;');
  6197. Add(' V3:string = ''abc'';');
  6198. ConvertUnit;
  6199. CheckSource('TestUnitImplVars',
  6200. LinesToStr([ // statements
  6201. 'var $impl = $mod.$impl;',
  6202. '']),
  6203. '', // this.$init
  6204. LinesToStr([ // implementation
  6205. '$impl.V1 = 0;',
  6206. '$impl.V2 = 3;',
  6207. '$impl.V3 = "abc";',
  6208. '']) );
  6209. end;
  6210. procedure TTestModule.TestUnitImplConsts;
  6211. begin
  6212. StartUnit(false);
  6213. Add('interface');
  6214. Add('implementation');
  6215. Add('const');
  6216. Add(' v1 = 3;');
  6217. Add(' v2:longint = 4;');
  6218. Add(' v3:string = ''abc'';');
  6219. ConvertUnit;
  6220. CheckSource('TestUnitImplConsts',
  6221. LinesToStr([ // statements
  6222. 'var $impl = $mod.$impl;',
  6223. '']),
  6224. '', // this.$init
  6225. LinesToStr([ // implementation
  6226. '$impl.v1 = 3;',
  6227. '$impl.v2 = 4;',
  6228. '$impl.v3 = "abc";',
  6229. '']) );
  6230. end;
  6231. procedure TTestModule.TestUnitImplRecord;
  6232. begin
  6233. StartUnit(false);
  6234. Add('interface');
  6235. Add('implementation');
  6236. Add('type');
  6237. Add(' TMyRecord = record');
  6238. Add(' i: longint;');
  6239. Add(' end;');
  6240. Add('var aRec: TMyRecord;');
  6241. Add('initialization');
  6242. Add(' arec.i:=3;');
  6243. ConvertUnit;
  6244. CheckSource('TestUnitImplRecord',
  6245. LinesToStr([ // statements
  6246. 'var $impl = $mod.$impl;',
  6247. '']),
  6248. // this.$init
  6249. '$impl.aRec.i = 3;',
  6250. LinesToStr([ // implementation
  6251. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6252. ' this.i = 0;',
  6253. ' this.$eq = function (b) {',
  6254. ' return this.i === b.i;',
  6255. ' };',
  6256. ' this.$assign = function (s) {',
  6257. ' this.i = s.i;',
  6258. ' return this;',
  6259. ' };',
  6260. '});',
  6261. '$impl.aRec = $impl.TMyRecord.$new();',
  6262. '']) );
  6263. end;
  6264. procedure TTestModule.TestRenameJSNameConflict;
  6265. begin
  6266. StartProgram(false);
  6267. Add('var apply: longint;');
  6268. Add('var bind: longint;');
  6269. Add('var call: longint;');
  6270. Add('begin');
  6271. ConvertProgram;
  6272. CheckSource('TestRenameJSNameConflict',
  6273. LinesToStr([ // statements
  6274. 'this.Apply = 0;',
  6275. 'this.Bind = 0;',
  6276. 'this.Call = 0;'
  6277. ]),
  6278. LinesToStr([ // this.$main
  6279. ''
  6280. ]));
  6281. end;
  6282. procedure TTestModule.TestLocalConst;
  6283. begin
  6284. StartProgram(false);
  6285. Add('procedure DoIt;');
  6286. Add('const');
  6287. Add(' cA: longint = 1;');
  6288. Add(' cB = 2;');
  6289. Add(' procedure Sub;');
  6290. Add(' const');
  6291. Add(' csA = 3;');
  6292. Add(' cB: double = 4;');
  6293. Add(' begin');
  6294. Add(' cb:=cb+csa;');
  6295. Add(' ca:=ca+csa+5;');
  6296. Add(' end;');
  6297. Add('begin');
  6298. Add(' ca:=ca+cb+6;');
  6299. Add('end;');
  6300. Add('begin');
  6301. ConvertProgram;
  6302. CheckSource('TestLocalConst',
  6303. LinesToStr([
  6304. 'var cA = 1;',
  6305. 'var cB = 2;',
  6306. 'var csA = 3;',
  6307. 'var cB$1 = 4;',
  6308. 'this.DoIt = function () {',
  6309. ' function Sub() {',
  6310. ' cB$1 = cB$1 + 3;',
  6311. ' cA = cA + 3 + 5;',
  6312. ' };',
  6313. ' cA = cA + 2 + 6;',
  6314. '};'
  6315. ]),
  6316. LinesToStr([
  6317. ]));
  6318. end;
  6319. procedure TTestModule.TestVarExternal;
  6320. begin
  6321. StartProgram(false);
  6322. Add('var');
  6323. Add(' NaN: double; external name ''Global.NaN'';');
  6324. Add(' d: double;');
  6325. Add('begin');
  6326. Add(' d:=NaN;');
  6327. ConvertProgram;
  6328. CheckSource('TestVarExternal',
  6329. LinesToStr([
  6330. 'this.d = 0.0;'
  6331. ]),
  6332. LinesToStr([
  6333. '$mod.d = Global.NaN;'
  6334. ]));
  6335. end;
  6336. procedure TTestModule.TestVarExternalOtherUnit;
  6337. begin
  6338. AddModuleWithIntfImplSrc('unit2.pas',
  6339. LinesToStr([
  6340. 'var NaN: double; external name ''Global.NaN'';',
  6341. 'var iV: longint;'
  6342. ]),
  6343. '');
  6344. StartUnit(true);
  6345. Add('interface');
  6346. Add('uses unit2;');
  6347. Add('implementation');
  6348. Add('var');
  6349. Add(' d: double;');
  6350. Add(' i: longint; external name ''$i'';');
  6351. Add('begin');
  6352. Add(' d:=nan;');
  6353. Add(' d:=uNit2.nan;');
  6354. Add(' d:=test1.d;');
  6355. Add(' i:=iv;');
  6356. Add(' i:=uNit2.iv;');
  6357. Add(' i:=test1.i;');
  6358. ConvertUnit;
  6359. CheckSource('TestVarExternalOtherUnit',
  6360. LinesToStr([
  6361. 'var $impl = $mod.$impl;',
  6362. '']),
  6363. LinesToStr([ // this.$init
  6364. '$impl.d = Global.NaN;',
  6365. '$impl.d = Global.NaN;',
  6366. '$impl.d = $impl.d;',
  6367. '$i = pas.unit2.iV;',
  6368. '$i = pas.unit2.iV;',
  6369. '$i = $i;',
  6370. '']),
  6371. LinesToStr([ // implementation
  6372. '$impl.d = 0.0;',
  6373. '']) );
  6374. end;
  6375. procedure TTestModule.TestVarAbsoluteFail;
  6376. begin
  6377. StartProgram(false);
  6378. Add([
  6379. 'var',
  6380. ' a: longint;',
  6381. ' b: longword absolute a;',
  6382. 'begin']);
  6383. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6384. ConvertProgram;
  6385. end;
  6386. procedure TTestModule.TestConstExternal;
  6387. begin
  6388. StartProgram(false);
  6389. Add([
  6390. 'const',
  6391. ' PI: double; external name ''Global.PI'';',
  6392. ' Tau = 2*pi;',
  6393. 'var d: double;',
  6394. 'begin',
  6395. ' d:=pi;',
  6396. ' d:=tau+pi;']);
  6397. ConvertProgram;
  6398. CheckSource('TestConstExternal',
  6399. LinesToStr([
  6400. 'this.Tau = 2*Global.PI;',
  6401. 'this.d = 0.0;'
  6402. ]),
  6403. LinesToStr([
  6404. '$mod.d = Global.PI;',
  6405. '$mod.d = $mod.Tau + Global.PI;'
  6406. ]));
  6407. end;
  6408. procedure TTestModule.TestDouble;
  6409. begin
  6410. StartProgram(false);
  6411. Add([
  6412. 'type',
  6413. ' TDateTime = double;',
  6414. 'const',
  6415. ' a = TDateTime(2.7);',
  6416. ' b = a + TDateTime(1.7);',
  6417. ' c = 0.9 + 0.1;',
  6418. ' f0_1 = 0.1;',
  6419. ' f0_3 = 0.3;',
  6420. ' fn0_1 = -0.1;',
  6421. ' fn0_3 = -0.3;',
  6422. ' fn0_003 = -0.003;',
  6423. ' fn0_123456789 = -0.123456789;',
  6424. ' fn300_0 = -300.0;',
  6425. ' fn123456_0 = -123456.0;',
  6426. ' fn1234567_8 = -1234567.8;',
  6427. ' fn12345678_9 = -12345678.9;',
  6428. ' f1_0En12 = 1E-12;',
  6429. ' fn1_0En12 = -1E-12;',
  6430. ' maxdouble = 1.7e+308;',
  6431. ' mindouble = -1.7e+308;',
  6432. ' MinSafeIntDouble = -$1fffffffffffff;',
  6433. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6434. ' MaxSafeIntDouble = $1fffffffffffff;',
  6435. ' DZeroResolution = 1E-12;',
  6436. ' Minus1 = -1E-12;',
  6437. ' EPS = 1E-9;',
  6438. ' DELTA = 0.001;',
  6439. ' Big = 129.789E+100;',
  6440. ' Test0_15 = 0.15;',
  6441. ' Test999 = 2.9999999999999;',
  6442. ' Test111999 = 211199999999999000.0;',
  6443. ' TestMinus111999 = -211199999999999000.0;',
  6444. 'var',
  6445. ' d: double = b;',
  6446. 'begin',
  6447. ' d:=1.0;',
  6448. ' d:=1.0/3.0;',
  6449. ' d:=1/3;',
  6450. ' d:=5.0E-324;',
  6451. ' d:=1.7E308;',
  6452. ' d:=001.00E00;',
  6453. ' d:=002.00E001;',
  6454. ' d:=003.000E000;',
  6455. ' d:=-004.00E-00;',
  6456. ' d:=-005.00E-001;',
  6457. ' d:=10**3;',
  6458. ' d:=10 mod 3;',
  6459. ' d:=10 div 3;',
  6460. ' d:=c;',
  6461. ' d:=f0_1;',
  6462. ' d:=f0_3;',
  6463. ' d:=fn0_1;',
  6464. ' d:=fn0_3;',
  6465. ' d:=fn0_003;',
  6466. ' d:=fn0_123456789;',
  6467. ' d:=fn300_0;',
  6468. ' d:=fn123456_0;',
  6469. ' d:=fn1234567_8;',
  6470. ' d:=fn12345678_9;',
  6471. ' d:=f1_0En12;',
  6472. ' d:=fn1_0En12;',
  6473. ' d:=maxdouble;',
  6474. ' d:=mindouble;',
  6475. ' d:=MinSafeIntDouble;',
  6476. ' d:=double(MinSafeIntDouble);',
  6477. ' d:=MinSafeIntDouble2;',
  6478. ' d:=double(MinSafeIntDouble2);',
  6479. ' d:=MaxSafeIntDouble;',
  6480. ' d:=default(double);',
  6481. '']);
  6482. ConvertProgram;
  6483. CheckSource('TestDouble',
  6484. LinesToStr([
  6485. 'this.a = 2.7;',
  6486. 'this.b = 2.7 + 1.7;',
  6487. 'this.c = 0.9 + 0.1;',
  6488. 'this.f0_1 = 0.1;',
  6489. 'this.f0_3 = 0.3;',
  6490. 'this.fn0_1 = -0.1;',
  6491. 'this.fn0_3 = -0.3;',
  6492. 'this.fn0_003 = -0.003;',
  6493. 'this.fn0_123456789 = -0.123456789;',
  6494. 'this.fn300_0 = -300.0;',
  6495. 'this.fn123456_0 = -123456.0;',
  6496. 'this.fn1234567_8 = -1234567.8;',
  6497. 'this.fn12345678_9 = -12345678.9;',
  6498. 'this.f1_0En12 = 1E-12;',
  6499. 'this.fn1_0En12 = -1E-12;',
  6500. 'this.maxdouble = 1.7e+308;',
  6501. 'this.mindouble = -1.7e+308;',
  6502. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6503. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6504. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6505. 'this.DZeroResolution = 1E-12;',
  6506. 'this.Minus1 = -1E-12;',
  6507. 'this.EPS = 1E-9;',
  6508. 'this.DELTA = 0.001;',
  6509. 'this.Big = 129.789E+100;',
  6510. 'this.Test0_15 = 0.15;',
  6511. 'this.Test999 = 2.9999999999999;',
  6512. 'this.Test111999 = 211199999999999000.0;',
  6513. 'this.TestMinus111999 = -211199999999999000.0;',
  6514. 'this.d = 4.4;'
  6515. ]),
  6516. LinesToStr([
  6517. '$mod.d = 1.0;',
  6518. '$mod.d = 1.0 / 3.0;',
  6519. '$mod.d = 1 / 3;',
  6520. '$mod.d = 5.0E-324;',
  6521. '$mod.d = 1.7E308;',
  6522. '$mod.d = 1.00E0;',
  6523. '$mod.d = 2.00E1;',
  6524. '$mod.d = 3.000E0;',
  6525. '$mod.d = -4.00E-0;',
  6526. '$mod.d = -5.00E-1;',
  6527. '$mod.d = Math.pow(10, 3);',
  6528. '$mod.d = 10 % 3;',
  6529. '$mod.d = Math.floor(10 / 3);',
  6530. '$mod.d = 1;',
  6531. '$mod.d = 0.1;',
  6532. '$mod.d = 0.3;',
  6533. '$mod.d = -0.1;',
  6534. '$mod.d = -0.3;',
  6535. '$mod.d = -0.003;',
  6536. '$mod.d = -0.123456789;',
  6537. '$mod.d = -300;',
  6538. '$mod.d = -123456;',
  6539. '$mod.d = -1234567.8;',
  6540. '$mod.d = -1.23456789E7;',
  6541. '$mod.d = 1E-12;',
  6542. '$mod.d = -1E-12;',
  6543. '$mod.d = 1.7E308;',
  6544. '$mod.d = -1.7E308;',
  6545. '$mod.d = -9007199254740991;',
  6546. '$mod.d = -9007199254740991;',
  6547. '$mod.d = -9.007199254740992E15;',
  6548. '$mod.d = -9.007199254740992E15;',
  6549. '$mod.d = 9007199254740991;',
  6550. '$mod.d = 0.0;',
  6551. '']));
  6552. end;
  6553. procedure TTestModule.TestInteger;
  6554. begin
  6555. StartProgram(false);
  6556. Add([
  6557. 'const',
  6558. ' MinInt = low(NativeInt);',
  6559. ' MaxInt = high(NativeInt);',
  6560. 'type',
  6561. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6562. 'const',
  6563. ' a = low(TMyInt)+High(TMyInt);',
  6564. 'var',
  6565. ' i: TMyInt;',
  6566. 'begin',
  6567. ' i:=-MinInt;',
  6568. ' i:=default(TMyInt);',
  6569. ' i:=low(i)+high(i);',
  6570. '']);
  6571. ConvertProgram;
  6572. CheckSource('TestIntegerRange',
  6573. LinesToStr([
  6574. 'this.MinInt = -9007199254740991;',
  6575. 'this.MaxInt = 9007199254740991;',
  6576. 'this.a = -9007199254740991 + 9007199254740991;',
  6577. 'this.i = 0;',
  6578. '']),
  6579. LinesToStr([
  6580. '$mod.i = - -9007199254740991;',
  6581. '$mod.i = -9007199254740991;',
  6582. '$mod.i = -9007199254740991 + 9007199254740991;',
  6583. '']));
  6584. end;
  6585. procedure TTestModule.TestIntegerRange;
  6586. begin
  6587. StartProgram(false);
  6588. Add([
  6589. 'const',
  6590. ' MinInt = -1;',
  6591. ' MaxInt = +1;',
  6592. 'type',
  6593. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6594. ' TInt2 = 1..3;',
  6595. 'const',
  6596. ' a = low(TMyInt)+High(TMyInt);',
  6597. ' b = low(TInt2)+High(TInt2);',
  6598. ' s1 = [1];',
  6599. ' s2 = [1,2];',
  6600. ' s3 = [1..3];',
  6601. ' s4 = [low(shortint)..high(shortint)];',
  6602. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6603. ' s6 = 1 in s2;',
  6604. 'var',
  6605. ' i: TMyInt;',
  6606. ' i2: TInt2;',
  6607. 'begin',
  6608. ' i:=i2;',
  6609. ' i:=default(TMyInt);',
  6610. ' if i=i2 then ;']);
  6611. ConvertProgram;
  6612. CheckSource('TestIntegerRange',
  6613. LinesToStr([
  6614. 'this.MinInt = -1;',
  6615. 'this.MaxInt = +1;',
  6616. 'this.a = -1 + 1;',
  6617. 'this.b = 1 + 3;',
  6618. 'this.s1 = rtl.createSet(1);',
  6619. 'this.s2 = rtl.createSet(1, 2);',
  6620. 'this.s3 = rtl.createSet(null, 1, 3);',
  6621. 'this.s4 = rtl.createSet(null, -128, 127);',
  6622. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6623. 'this.s6 = 1 in $mod.s2;',
  6624. 'this.i = 0;',
  6625. 'this.i2 = 0;',
  6626. '']),
  6627. LinesToStr([
  6628. '$mod.i = $mod.i2;',
  6629. '$mod.i = -1;',
  6630. 'if ($mod.i === $mod.i2) ;',
  6631. '']));
  6632. end;
  6633. procedure TTestModule.TestIntegerTypecasts;
  6634. begin
  6635. StartProgram(false);
  6636. Add([
  6637. 'var',
  6638. ' i: nativeint;',
  6639. ' b: byte;',
  6640. ' sh: shortint;',
  6641. ' w: word;',
  6642. ' sm: smallint;',
  6643. ' lw: longword;',
  6644. ' li: longint;',
  6645. 'begin',
  6646. ' b:=byte(i);',
  6647. ' sh:=shortint(i);',
  6648. ' w:=word(i);',
  6649. ' sm:=smallint(i);',
  6650. ' lw:=longword(i);',
  6651. ' li:=longint(i);',
  6652. '']);
  6653. ConvertProgram;
  6654. CheckSource('TestIntegerTypecasts',
  6655. LinesToStr([
  6656. 'this.i = 0;',
  6657. 'this.b = 0;',
  6658. 'this.sh = 0;',
  6659. 'this.w = 0;',
  6660. 'this.sm = 0;',
  6661. 'this.lw = 0;',
  6662. 'this.li = 0;',
  6663. '']),
  6664. LinesToStr([
  6665. '$mod.b = $mod.i & 255;',
  6666. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6667. '$mod.w = $mod.i & 65535;',
  6668. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6669. '$mod.lw = $mod.i >>> 0;',
  6670. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6671. '']));
  6672. end;
  6673. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6674. begin
  6675. StartProgram(false);
  6676. Add([
  6677. 'var',
  6678. ' i,j: nativeint;',
  6679. 'begin',
  6680. ' i:=i shr 0;',
  6681. ' i:=i shr 1;',
  6682. ' i:=i shr 3;',
  6683. ' i:=i shr 54;',
  6684. ' i:=j shr i;',
  6685. '']);
  6686. ConvertProgram;
  6687. CheckResolverUnexpectedHints;
  6688. CheckSource('TestInteger_BitwiseShrNativeInt',
  6689. LinesToStr([
  6690. 'this.i = 0;',
  6691. 'this.j = 0;',
  6692. '']),
  6693. LinesToStr([
  6694. '$mod.i = $mod.i;',
  6695. '$mod.i = Math.floor($mod.i / 2);',
  6696. '$mod.i = Math.floor($mod.i / 8);',
  6697. '$mod.i = 0;',
  6698. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6699. '']));
  6700. end;
  6701. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6702. begin
  6703. StartProgram(false);
  6704. Add([
  6705. 'var',
  6706. ' i: nativeint;',
  6707. 'begin',
  6708. ' i:=i shl 0;',
  6709. ' i:=i shl 54;',
  6710. ' i:=123456789012 shl 1;',
  6711. ' i:=i shl 1;',
  6712. '']);
  6713. ConvertProgram;
  6714. CheckResolverUnexpectedHints;
  6715. CheckSource('TestInteger_BitwiseShrNativeInt',
  6716. LinesToStr([
  6717. 'this.i = 0;',
  6718. '']),
  6719. LinesToStr([
  6720. '$mod.i = $mod.i;',
  6721. '$mod.i = 0;',
  6722. '$mod.i = 246913578024;',
  6723. '$mod.i = rtl.shl($mod.i, 1);',
  6724. '']));
  6725. end;
  6726. procedure TTestModule.TestInteger_SystemFunc;
  6727. begin
  6728. StartProgram(true);
  6729. Add([
  6730. 'var',
  6731. ' i: byte;',
  6732. ' s: string;',
  6733. 'begin',
  6734. ' system.inc(i);',
  6735. ' system.str(i,s);',
  6736. ' s:=system.str(i);',
  6737. ' i:=system.low(i);',
  6738. ' i:=system.high(i);',
  6739. ' i:=system.pred(i);',
  6740. ' i:=system.succ(i);',
  6741. '']);
  6742. ConvertProgram;
  6743. CheckResolverUnexpectedHints;
  6744. CheckSource('TestInteger_SystemFunc',
  6745. LinesToStr([
  6746. 'this.i = 0;',
  6747. 'this.s = "";',
  6748. '']),
  6749. LinesToStr([
  6750. '$mod.i += 1;',
  6751. '$mod.s = "" + $mod.i;',
  6752. '$mod.s = "" + $mod.i;',
  6753. '$mod.i = 0;',
  6754. '$mod.i = 255;',
  6755. '$mod.i = $mod.i - 1;',
  6756. '$mod.i = $mod.i + 1;',
  6757. '']));
  6758. end;
  6759. procedure TTestModule.TestCurrency;
  6760. begin
  6761. StartProgram(false);
  6762. Add([
  6763. 'type',
  6764. ' TCoin = currency;',
  6765. 'const',
  6766. ' a = TCoin(2.7);',
  6767. ' b = a + TCoin(1.7);',
  6768. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6769. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6770. 'var',
  6771. ' c: TCoin = b;',
  6772. ' i: nativeint;',
  6773. ' d: double;',
  6774. ' j: jsvalue;',
  6775. 'function DoIt(c: currency): currency; begin end;',
  6776. 'function GetIt(d: double): double; begin end;',
  6777. 'procedure Write(v: jsvalue); begin end;',
  6778. 'begin',
  6779. ' c:=1.0;',
  6780. ' c:=0.1;',
  6781. ' c:=1.0/3.0;',
  6782. ' c:=1/3;',
  6783. ' c:=a;',
  6784. ' d:=c;',
  6785. ' c:=d;',
  6786. ' c:=currency(c);',
  6787. ' c:=currency(d);',
  6788. ' d:=double(c);',
  6789. ' c:=i;',
  6790. ' c:=currency(i);',
  6791. //' i:=c;', not allowed
  6792. ' i:=nativeint(c);',
  6793. ' c:=c+a;',
  6794. ' c:=-c-a;',
  6795. ' c:=d+c;',
  6796. ' c:=c+d;',
  6797. ' c:=d-c;',
  6798. ' c:=c-d;',
  6799. ' c:=c*a;',
  6800. ' c:=a*c;',
  6801. ' c:=d*c;',
  6802. ' c:=c*d;',
  6803. ' c:=c/a;',
  6804. ' c:=a/c;',
  6805. ' c:=d/c;',
  6806. ' c:=c/d;',
  6807. ' c:=c**a;',
  6808. ' c:=a**c;',
  6809. ' c:=d**c;',
  6810. ' c:=c**d;',
  6811. ' if c=c then ;',
  6812. ' if c=a then ;',
  6813. ' if a=c then ;',
  6814. ' if d=c then ;',
  6815. ' if c=d then ;',
  6816. ' c:=DoIt(c);',
  6817. ' c:=DoIt(i);',
  6818. ' c:=DoIt(d);',
  6819. ' c:=GetIt(c);',
  6820. ' j:=c;',
  6821. ' Write(c);',
  6822. ' c:=default(currency);',
  6823. ' j:=str(c);',
  6824. ' j:=str(c:0:3);',
  6825. '']);
  6826. ConvertProgram;
  6827. CheckSource('TestCurrency',
  6828. LinesToStr([
  6829. 'this.a = 27000;',
  6830. 'this.b = $mod.a + 17000;',
  6831. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6832. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6833. 'this.c = $mod.b;',
  6834. 'this.i = 0;',
  6835. 'this.d = 0.0;',
  6836. 'this.j = undefined;',
  6837. 'this.DoIt = function (c) {',
  6838. ' var Result = 0;',
  6839. ' return Result;',
  6840. '};',
  6841. 'this.GetIt = function (d) {',
  6842. ' var Result = 0.0;',
  6843. ' return Result;',
  6844. '};',
  6845. 'this.Write = function (v) {',
  6846. '};',
  6847. '']),
  6848. LinesToStr([
  6849. '$mod.c = 10000;',
  6850. '$mod.c = 1000;',
  6851. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6852. '$mod.c = Math.floor((1 / 3) * 10000);',
  6853. '$mod.c = $mod.a;',
  6854. '$mod.d = $mod.c / 10000;',
  6855. '$mod.c = Math.floor($mod.d * 10000);',
  6856. '$mod.c = $mod.c;',
  6857. '$mod.c = $mod.d * 10000;',
  6858. '$mod.d = $mod.c / 10000;',
  6859. '$mod.c = $mod.i * 10000;',
  6860. '$mod.c = $mod.i * 10000;',
  6861. '$mod.i = Math.floor($mod.c / 10000);',
  6862. '$mod.c = $mod.c + $mod.a;',
  6863. '$mod.c = -$mod.c - $mod.a;',
  6864. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6865. '$mod.c = $mod.c + ($mod.d * 10000);',
  6866. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6867. '$mod.c = $mod.c - ($mod.d * 10000);',
  6868. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6869. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6870. '$mod.c = $mod.d * $mod.c;',
  6871. '$mod.c = $mod.c * $mod.d;',
  6872. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6873. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6874. '$mod.c = Math.floor($mod.d / $mod.c);',
  6875. '$mod.c = Math.floor($mod.c / $mod.d);',
  6876. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6877. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6878. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6879. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6880. 'if ($mod.c === $mod.c) ;',
  6881. 'if ($mod.c === $mod.a) ;',
  6882. 'if ($mod.a === $mod.c) ;',
  6883. 'if (($mod.d * 10000) === $mod.c) ;',
  6884. 'if ($mod.c === ($mod.d * 10000)) ;',
  6885. '$mod.c = $mod.DoIt($mod.c);',
  6886. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6887. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6888. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6889. '$mod.j = $mod.c / 10000;',
  6890. '$mod.Write($mod.c / 10000);',
  6891. '$mod.c = 0;',
  6892. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6893. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6894. '']));
  6895. end;
  6896. procedure TTestModule.TestForBoolDo;
  6897. begin
  6898. StartProgram(false);
  6899. Add([
  6900. 'var b: boolean;',
  6901. 'begin',
  6902. ' for b:=false to true do ;',
  6903. ' for b:=b downto false do ;',
  6904. ' for b in boolean do ;',
  6905. '']);
  6906. ConvertProgram;
  6907. CheckSource('TestForBoolDo',
  6908. LinesToStr([ // statements
  6909. 'this.b = false;']),
  6910. LinesToStr([ // this.$main
  6911. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  6912. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  6913. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  6914. '']));
  6915. end;
  6916. procedure TTestModule.TestForIntDo;
  6917. begin
  6918. StartProgram(false);
  6919. Add([
  6920. 'var i: longint;',
  6921. 'begin',
  6922. ' for i:=3 to 5 do ;',
  6923. ' for i:=i downto 2 do ;',
  6924. ' for i in byte do ;',
  6925. '']);
  6926. ConvertProgram;
  6927. CheckSource('TestForIntDo',
  6928. LinesToStr([ // statements
  6929. 'this.i = 0;']),
  6930. LinesToStr([ // this.$main
  6931. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6932. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  6933. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6934. '']));
  6935. end;
  6936. procedure TTestModule.TestForIntInDo;
  6937. begin
  6938. StartProgram(false);
  6939. Add([
  6940. 'type',
  6941. ' TSetOfInt = set of byte;',
  6942. ' TIntRg = 3..7;',
  6943. ' TSetOfIntRg = set of TIntRg;',
  6944. 'var',
  6945. ' i,i2: longint;',
  6946. ' a1: array of byte;',
  6947. ' a2: array[1..3] of byte;',
  6948. ' soi: TSetOfInt;',
  6949. ' soir: TSetOfIntRg;',
  6950. ' ir: TIntRg;',
  6951. 'begin',
  6952. ' for i in byte do ;',
  6953. ' for i in a1 do ;',
  6954. ' for i in a2 do ;',
  6955. ' for i in [11..13] do ;',
  6956. ' for i in TSetOfInt do ;',
  6957. ' for i in TIntRg do ;',
  6958. ' for i in soi do i2:=i;',
  6959. ' for i in TSetOfIntRg do ;',
  6960. ' for i in soir do ;',
  6961. ' for ir in TIntRg do ;',
  6962. ' for ir in TSetOfIntRg do ;',
  6963. ' for ir in soir do ;',
  6964. '']);
  6965. ConvertProgram;
  6966. CheckSource('TestForIntInDo',
  6967. LinesToStr([ // statements
  6968. 'this.i = 0;',
  6969. 'this.i2 = 0;',
  6970. 'this.a1 = [];',
  6971. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6972. 'this.soi = {};',
  6973. 'this.soir = {};',
  6974. 'this.ir = 0;',
  6975. '']),
  6976. LinesToStr([ // this.$main
  6977. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  6978. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  6979. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  6980. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  6981. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  6982. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  6983. 'for (var $l6 in $mod.soi) {',
  6984. ' $mod.i = +$l6;',
  6985. ' $mod.i2 = $mod.i;',
  6986. '};',
  6987. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  6988. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  6989. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  6990. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  6991. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  6992. '']));
  6993. end;
  6994. procedure TTestModule.TestCharConst;
  6995. begin
  6996. StartProgram(false);
  6997. Add([
  6998. 'const',
  6999. ' a = #$00F3;',
  7000. ' c: char = ''1'';',
  7001. 'begin',
  7002. ' c:=#0;',
  7003. ' c:=#1;',
  7004. ' c:=#9;',
  7005. ' c:=#10;',
  7006. ' c:=#13;',
  7007. ' c:=#31;',
  7008. ' c:=#32;',
  7009. ' c:=#$A;',
  7010. ' c:=#$0A;',
  7011. ' c:=#$b;',
  7012. ' c:=#$0b;',
  7013. ' c:=^A;',
  7014. ' c:=''"'';',
  7015. ' c:=default(char);',
  7016. ' c:=#$00E4;', // ä
  7017. ' c:=''ä'';',
  7018. ' c:=#$E4;', // ä
  7019. ' c:=#$D800;', // invalid UTF-16
  7020. ' c:=#$DFFF;', // invalid UTF-16
  7021. ' c:=#$FFFF;', // last UCS-2
  7022. ' c:=high(c);', // last UCS-2
  7023. '']);
  7024. ConvertProgram;
  7025. CheckSource('TestCharConst',
  7026. LinesToStr([
  7027. 'this.a="ó";',
  7028. 'this.c="1";'
  7029. ]),
  7030. LinesToStr([
  7031. '$mod.c="\x00";',
  7032. '$mod.c="\x01";',
  7033. '$mod.c="\t";',
  7034. '$mod.c="\n";',
  7035. '$mod.c="\r";',
  7036. '$mod.c="\x1F";',
  7037. '$mod.c=" ";',
  7038. '$mod.c="\n";',
  7039. '$mod.c="\n";',
  7040. '$mod.c="\x0B";',
  7041. '$mod.c="\x0B";',
  7042. '$mod.c="\x01";',
  7043. '$mod.c=''"'';',
  7044. '$mod.c="\x00";',
  7045. '$mod.c = "ä";',
  7046. '$mod.c = "ä";',
  7047. '$mod.c = "ä";',
  7048. '$mod.c="\uD800";',
  7049. '$mod.c="\uDFFF";',
  7050. '$mod.c="\uFFFF";',
  7051. '$mod.c="\uFFFF";',
  7052. '']));
  7053. end;
  7054. procedure TTestModule.TestChar_Compare;
  7055. begin
  7056. StartProgram(false);
  7057. Add('var');
  7058. Add(' c: char;');
  7059. Add(' b: boolean;');
  7060. Add('begin');
  7061. Add(' b:=c=''1'';');
  7062. Add(' b:=''2''=c;');
  7063. Add(' b:=''3''=''4'';');
  7064. Add(' b:=c<>''5'';');
  7065. Add(' b:=''6''<>c;');
  7066. Add(' b:=c>''7'';');
  7067. Add(' b:=''8''>c;');
  7068. Add(' b:=c>=''9'';');
  7069. Add(' b:=''A''>=c;');
  7070. Add(' b:=c<''B'';');
  7071. Add(' b:=''C''<c;');
  7072. Add(' b:=c<=''D'';');
  7073. Add(' b:=''E''<=c;');
  7074. ConvertProgram;
  7075. CheckSource('TestChar_Compare',
  7076. LinesToStr([
  7077. 'this.c="";',
  7078. 'this.b = false;'
  7079. ]),
  7080. LinesToStr([
  7081. '$mod.b = $mod.c === "1";',
  7082. '$mod.b = "2" === $mod.c;',
  7083. '$mod.b = "3" === "4";',
  7084. '$mod.b = $mod.c !== "5";',
  7085. '$mod.b = "6" !== $mod.c;',
  7086. '$mod.b = $mod.c > "7";',
  7087. '$mod.b = "8" > $mod.c;',
  7088. '$mod.b = $mod.c >= "9";',
  7089. '$mod.b = "A" >= $mod.c;',
  7090. '$mod.b = $mod.c < "B";',
  7091. '$mod.b = "C" < $mod.c;',
  7092. '$mod.b = $mod.c <= "D";',
  7093. '$mod.b = "E" <= $mod.c;',
  7094. '']));
  7095. end;
  7096. procedure TTestModule.TestChar_BuiltInProcs;
  7097. begin
  7098. StartProgram(false);
  7099. Add([
  7100. 'var',
  7101. ' c: char;',
  7102. ' i: longint;',
  7103. ' s: string;',
  7104. 'begin',
  7105. ' i:=ord(c);',
  7106. ' i:=ord(s[i]);',
  7107. ' c:=chr(i);',
  7108. ' c:=pred(c);',
  7109. ' c:=succ(c);',
  7110. ' c:=low(c);',
  7111. ' c:=high(c);',
  7112. ' i:=byte(c);',
  7113. ' i:=word(c);',
  7114. ' i:=longint(c);',
  7115. '']);
  7116. ConvertProgram;
  7117. CheckSource('TestChar_BuiltInProcs',
  7118. LinesToStr([
  7119. 'this.c = "";',
  7120. 'this.i = 0;',
  7121. 'this.s = "";'
  7122. ]),
  7123. LinesToStr([
  7124. '$mod.i = $mod.c.charCodeAt();',
  7125. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7126. '$mod.c = String.fromCharCode($mod.i);',
  7127. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7128. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7129. '$mod.c = "\x00";',
  7130. '$mod.c = "\uFFFF";',
  7131. '$mod.i = $mod.c.charCodeAt() & 255;',
  7132. '$mod.i = $mod.c.charCodeAt();',
  7133. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7134. '']));
  7135. end;
  7136. procedure TTestModule.TestStringConst;
  7137. begin
  7138. StartProgram(false);
  7139. Add([
  7140. '{$H+}',
  7141. 'const',
  7142. ' a = #$00F3#$017C;', // first <256, then >=256
  7143. ' b = string(''a'');',
  7144. ' c = string(''ä'');',
  7145. ' d = UnicodeString(''b'');',
  7146. ' e = UnicodeString(''ö'');',
  7147. 'var',
  7148. ' s: string = ''abc'';',
  7149. 'begin',
  7150. ' s:='''';',
  7151. ' s:=#13#10;',
  7152. ' s:=#9''foo'';',
  7153. ' s:=#$A9;',
  7154. ' s:=''foo''#13''bar'';',
  7155. ' s:=''"'';',
  7156. ' s:=''"''''"'';',
  7157. ' s:=#$20AC;', // euro
  7158. ' s:=#$10437;', // outside BMP
  7159. ' s:=default(string);',
  7160. ' s:=concat(s);',
  7161. ' s:=concat(s,''a'',s)',
  7162. '']);
  7163. ConvertProgram;
  7164. CheckSource('TestStringConst',
  7165. LinesToStr([
  7166. 'this.a = "óż";',
  7167. 'this.b = "a";',
  7168. 'this.c = "ä";',
  7169. 'this.d = "b";',
  7170. 'this.e = "ö";',
  7171. 'this.s="abc";',
  7172. '']),
  7173. LinesToStr([
  7174. '$mod.s="";',
  7175. '$mod.s="\r\n";',
  7176. '$mod.s="\tfoo";',
  7177. '$mod.s="©";',
  7178. '$mod.s="foo\rbar";',
  7179. '$mod.s=''"'';',
  7180. '$mod.s=''"\''"'';',
  7181. '$mod.s="€";',
  7182. '$mod.s="'#$F0#$90#$90#$B7'";',
  7183. '$mod.s="";',
  7184. '$mod.s = $mod.s;',
  7185. '$mod.s = $mod.s.concat("a", $mod.s);',
  7186. '']));
  7187. end;
  7188. procedure TTestModule.TestStringConstSurrogate;
  7189. begin
  7190. StartProgram(false);
  7191. Add([
  7192. 'var',
  7193. ' s: string;',
  7194. 'begin',
  7195. ' s:=''😊'';', // 1F60A
  7196. '']);
  7197. ConvertProgram;
  7198. CheckSource('TestStringConstSurrogate',
  7199. LinesToStr([
  7200. 'this.s="";'
  7201. ]),
  7202. LinesToStr([
  7203. '$mod.s="😊";'
  7204. ]));
  7205. end;
  7206. procedure TTestModule.TestString_Length;
  7207. begin
  7208. StartProgram(false);
  7209. Add('const c = ''foo'';');
  7210. Add('var');
  7211. Add(' s: string;');
  7212. Add(' i: longint;');
  7213. Add('begin');
  7214. Add(' i:=length(s);');
  7215. Add(' i:=length(s+s);');
  7216. Add(' i:=length(''abc'');');
  7217. Add(' i:=length(c);');
  7218. ConvertProgram;
  7219. CheckSource('TestString_Length',
  7220. LinesToStr([
  7221. 'this.c = "foo";',
  7222. 'this.s = "";',
  7223. 'this.i = 0;',
  7224. '']),
  7225. LinesToStr([
  7226. '$mod.i = $mod.s.length;',
  7227. '$mod.i = ($mod.s+$mod.s).length;',
  7228. '$mod.i = "abc".length;',
  7229. '$mod.i = $mod.c.length;',
  7230. '']));
  7231. end;
  7232. procedure TTestModule.TestString_Compare;
  7233. begin
  7234. StartProgram(false);
  7235. Add('var');
  7236. Add(' s, t: string;');
  7237. Add(' b: boolean;');
  7238. Add('begin');
  7239. Add(' b:=s=t;');
  7240. Add(' b:=s<>t;');
  7241. Add(' b:=s>t;');
  7242. Add(' b:=s>=t;');
  7243. Add(' b:=s<t;');
  7244. Add(' b:=s<=t;');
  7245. ConvertProgram;
  7246. CheckSource('TestString_Compare',
  7247. LinesToStr([ // statements
  7248. 'this.s = "";',
  7249. 'this.t = "";',
  7250. 'this.b =false;'
  7251. ]),
  7252. LinesToStr([ // this.$main
  7253. '$mod.b = $mod.s === $mod.t;',
  7254. '$mod.b = $mod.s !== $mod.t;',
  7255. '$mod.b = $mod.s > $mod.t;',
  7256. '$mod.b = $mod.s >= $mod.t;',
  7257. '$mod.b = $mod.s < $mod.t;',
  7258. '$mod.b = $mod.s <= $mod.t;',
  7259. '']));
  7260. end;
  7261. procedure TTestModule.TestString_SetLength;
  7262. begin
  7263. StartProgram(false);
  7264. Add([
  7265. 'procedure DoIt(var s: string);',
  7266. 'begin',
  7267. ' SetLength(s,2);',
  7268. 'end;',
  7269. 'var s: string;',
  7270. 'begin',
  7271. ' SetLength(s,3);',
  7272. '']);
  7273. ConvertProgram;
  7274. CheckSource('TestString_SetLength',
  7275. LinesToStr([ // statements
  7276. 'this.DoIt = function (s) {',
  7277. ' s.set(rtl.strSetLength(s.get(), 2));',
  7278. '};',
  7279. 'this.s = "";',
  7280. '']),
  7281. LinesToStr([ // this.$main
  7282. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7283. ]));
  7284. end;
  7285. procedure TTestModule.TestString_CharAt;
  7286. begin
  7287. StartProgram(false);
  7288. Add([
  7289. 'var',
  7290. ' s: string;',
  7291. ' c: char;',
  7292. ' b: boolean;',
  7293. 'begin',
  7294. ' b:= s[1] = c;',
  7295. ' b:= c = s[1];',
  7296. ' b:= c <> s[1];',
  7297. ' b:= c > s[1];',
  7298. ' b:= c >= s[1];',
  7299. ' b:= c < s[2];',
  7300. ' b:= c <= s[1];',
  7301. ' s[1] := c;',
  7302. ' s[2+3] := c;']);
  7303. ConvertProgram;
  7304. CheckSource('TestString_CharAt',
  7305. LinesToStr([ // statements
  7306. 'this.s = "";',
  7307. 'this.c = "";',
  7308. 'this.b = false;'
  7309. ]),
  7310. LinesToStr([ // this.$main
  7311. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7312. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7313. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7314. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7315. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7316. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7317. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7318. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7319. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7320. '']));
  7321. end;
  7322. procedure TTestModule.TestStringHMinusFail;
  7323. begin
  7324. StartProgram(false);
  7325. Add([
  7326. '{$H-}',
  7327. 'var s: string;',
  7328. 'begin']);
  7329. ConvertProgram;
  7330. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7331. end;
  7332. procedure TTestModule.TestStr;
  7333. begin
  7334. StartProgram(false);
  7335. Add('var');
  7336. Add(' b: boolean;');
  7337. Add(' i: longint;');
  7338. Add(' d: double;');
  7339. Add(' s: string;');
  7340. Add('begin');
  7341. Add(' str(b,s);');
  7342. Add(' str(i,s);');
  7343. Add(' str(d,s);');
  7344. Add(' str(i:3,s);');
  7345. Add(' str(d:3:2,s);');
  7346. Add(' Str(12.456:12:1,s);');
  7347. Add(' Str(12.456:12,s);');
  7348. Add(' s:=str(b);');
  7349. Add(' s:=str(i);');
  7350. Add(' s:=str(d);');
  7351. Add(' s:=str(i,i);');
  7352. Add(' s:=str(i:3);');
  7353. Add(' s:=str(d:3:2);');
  7354. Add(' s:=str(i:4,i);');
  7355. Add(' s:=str(i,i:5);');
  7356. Add(' s:=str(i:4,i:5);');
  7357. Add(' s:=str(s,s);');
  7358. Add(' s:=str(s,''foo'');');
  7359. ConvertProgram;
  7360. CheckSource('TestStr',
  7361. LinesToStr([ // statements
  7362. 'this.b = false;',
  7363. 'this.i = 0;',
  7364. 'this.d = 0.0;',
  7365. 'this.s = "";',
  7366. '']),
  7367. LinesToStr([ // this.$main
  7368. '$mod.s = ""+$mod.b;',
  7369. '$mod.s = ""+$mod.i;',
  7370. '$mod.s = rtl.floatToStr($mod.d);',
  7371. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7372. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7373. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7374. '$mod.s = rtl.floatToStr(12.456,12);',
  7375. '$mod.s = ""+$mod.b;',
  7376. '$mod.s = ""+$mod.i;',
  7377. '$mod.s = rtl.floatToStr($mod.d);',
  7378. '$mod.s = ""+$mod.i+$mod.i;',
  7379. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7380. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7381. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7382. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7383. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7384. '$mod.s = $mod.s + $mod.s;',
  7385. '$mod.s = $mod.s + "foo";',
  7386. '']));
  7387. end;
  7388. procedure TTestModule.TestBaseType_AnsiStringFail;
  7389. begin
  7390. StartProgram(false);
  7391. Add('var s: AnsiString');
  7392. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7393. ConvertProgram;
  7394. end;
  7395. procedure TTestModule.TestBaseType_WideStringFail;
  7396. begin
  7397. StartProgram(false);
  7398. Add('var s: WideString');
  7399. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7400. ConvertProgram;
  7401. end;
  7402. procedure TTestModule.TestBaseType_ShortStringFail;
  7403. begin
  7404. StartProgram(false);
  7405. Add('var s: ShortString');
  7406. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7407. ConvertProgram;
  7408. end;
  7409. procedure TTestModule.TestBaseType_RawByteStringFail;
  7410. begin
  7411. StartProgram(false);
  7412. Add('var s: RawByteString');
  7413. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7414. ConvertProgram;
  7415. end;
  7416. procedure TTestModule.TestTypeShortstring_Fail;
  7417. begin
  7418. StartProgram(false);
  7419. Add('type t = string[12];');
  7420. Add('var s: t;');
  7421. Add('begin');
  7422. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7423. ConvertProgram;
  7424. end;
  7425. procedure TTestModule.TestCharSet_Custom;
  7426. begin
  7427. StartProgram(false);
  7428. Add([
  7429. 'type',
  7430. ' TCharRg = ''a''..''z'';',
  7431. ' TSetOfCharRg = set of TCharRg;',
  7432. ' TCharRg2 = ''m''..''p'';',
  7433. 'const',
  7434. ' crg: TCharRg = ''b'';',
  7435. 'var',
  7436. ' c: char;',
  7437. ' crg2: TCharRg2;',
  7438. ' s: TSetOfCharRg;',
  7439. 'begin',
  7440. ' c:=crg;',
  7441. ' crg:=c;',
  7442. ' crg2:=crg;',
  7443. ' if c=crg then ;',
  7444. ' if crg=c then ;',
  7445. ' if crg=crg2 then ;',
  7446. ' if c in s then ;',
  7447. ' if crg2 in s then ;',
  7448. ' c:=default(TCharRg);',
  7449. '']);
  7450. ConvertProgram;
  7451. CheckSource('TestCharSet_Custom',
  7452. LinesToStr([ // statements
  7453. 'this.crg = "b";',
  7454. 'this.c = "";',
  7455. 'this.crg2 = "m";',
  7456. 'this.s = {};',
  7457. '']),
  7458. LinesToStr([ // this.$main
  7459. '$mod.c = $mod.crg;',
  7460. '$mod.crg = $mod.c;',
  7461. '$mod.crg2 = $mod.crg;',
  7462. 'if ($mod.c === $mod.crg) ;',
  7463. 'if ($mod.crg === $mod.c) ;',
  7464. 'if ($mod.crg === $mod.crg2) ;',
  7465. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7466. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7467. '$mod.c = "a";',
  7468. '']));
  7469. end;
  7470. procedure TTestModule.TestForCharDo;
  7471. begin
  7472. StartProgram(false);
  7473. Add([
  7474. 'var c: char;',
  7475. 'begin',
  7476. ' for c:=''a'' to ''c'' do ;',
  7477. ' for c:=c downto ''a'' do ;',
  7478. ' for c:=''Б'' to ''Я'' do ;',
  7479. '']);
  7480. ConvertProgram;
  7481. CheckSource('TestForCharDo',
  7482. LinesToStr([ // statements
  7483. 'this.c = "";']),
  7484. LinesToStr([ // this.$main
  7485. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7486. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7487. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7488. '']));
  7489. end;
  7490. procedure TTestModule.TestForCharInDo;
  7491. begin
  7492. StartProgram(false);
  7493. Add([
  7494. 'type',
  7495. ' TSetOfChar = set of char;',
  7496. ' TCharRg = ''a''..''z'';',
  7497. ' TSetOfCharRg = set of TCharRg;',
  7498. 'const Foo = ''foo'';',
  7499. 'var',
  7500. ' c,c2: char;',
  7501. ' s: string;',
  7502. ' a1: array of char;',
  7503. ' a2: array[1..3] of char;',
  7504. ' soc: TSetOfChar;',
  7505. ' socr: TSetOfCharRg;',
  7506. ' cr: TCharRg;',
  7507. 'begin',
  7508. ' for c in foo do ;',
  7509. ' for c in s do ;',
  7510. ' for c in char do ;',
  7511. ' for c in a1 do ;',
  7512. ' for c in a2 do ;',
  7513. ' for c in [''1''..''3''] do ;',
  7514. ' for c in TSetOfChar do ;',
  7515. ' for c in TCharRg do ;',
  7516. ' for c in soc do c2:=c;',
  7517. ' for c in TSetOfCharRg do ;',
  7518. ' for c in socr do ;',
  7519. ' for cr in TCharRg do ;',
  7520. ' for cr in TSetOfCharRg do ;',
  7521. ' for cr in socr do ;',
  7522. '']);
  7523. ConvertProgram;
  7524. CheckSource('TestForCharInDo',
  7525. LinesToStr([ // statements
  7526. 'this.Foo = "foo";',
  7527. 'this.c = "";',
  7528. 'this.c2 = "";',
  7529. 'this.s = "";',
  7530. 'this.a1 = [];',
  7531. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7532. 'this.soc = {};',
  7533. 'this.socr = {};',
  7534. 'this.cr = "a";',
  7535. '']),
  7536. LinesToStr([ // this.$main
  7537. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7538. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7539. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7540. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7541. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7542. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7543. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7544. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7545. 'for (var $l8 in $mod.soc) {',
  7546. ' $mod.c = String.fromCharCode($l8);',
  7547. ' $mod.c2 = $mod.c;',
  7548. '};',
  7549. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  7550. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  7551. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  7552. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  7553. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  7554. '']));
  7555. end;
  7556. procedure TTestModule.TestProcTwoArgs;
  7557. begin
  7558. StartProgram(false);
  7559. Add('procedure Test(a,b: longint);');
  7560. Add('begin');
  7561. Add('end;');
  7562. Add('begin');
  7563. ConvertProgram;
  7564. CheckSource('TestProcTwoArgs',
  7565. LinesToStr([ // statements
  7566. 'this.Test = function (a,b) {',
  7567. '};'
  7568. ]),
  7569. LinesToStr([ // this.$main
  7570. ''
  7571. ]));
  7572. end;
  7573. procedure TTestModule.TestProc_DefaultValue;
  7574. begin
  7575. StartProgram(false);
  7576. Add('procedure p1(i: longint = 1);');
  7577. Add('begin');
  7578. Add('end;');
  7579. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7580. Add('begin');
  7581. Add('end;');
  7582. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7583. Add('begin');
  7584. Add('end;');
  7585. Add('begin');
  7586. Add(' p1;');
  7587. Add(' p1();');
  7588. Add(' p1(11);');
  7589. Add(' p2;');
  7590. Add(' p2();');
  7591. Add(' p2(12);');
  7592. Add(' p2(13,''b'');');
  7593. Add(' p3();');
  7594. ConvertProgram;
  7595. CheckSource('TestProc_DefaultValue',
  7596. LinesToStr([ // statements
  7597. 'this.p1 = function (i) {',
  7598. '};',
  7599. 'this.p2 = function (i,c) {',
  7600. '};',
  7601. 'this.p3 = function (d,b,s) {',
  7602. '};'
  7603. ]),
  7604. LinesToStr([ // this.$main
  7605. ' $mod.p1(1);',
  7606. ' $mod.p1(1);',
  7607. ' $mod.p1(11);',
  7608. ' $mod.p2(1,"a");',
  7609. ' $mod.p2(1,"a");',
  7610. ' $mod.p2(12,"a");',
  7611. ' $mod.p2(13,"b");',
  7612. ' $mod.p3(1.0,false,"abc");'
  7613. ]));
  7614. end;
  7615. procedure TTestModule.TestFunctionInt;
  7616. begin
  7617. StartProgram(false);
  7618. Add('function MyTest(Bar: longint): longint;');
  7619. Add('begin');
  7620. Add(' Result:=2*bar');
  7621. Add('end;');
  7622. Add('begin');
  7623. ConvertProgram;
  7624. CheckSource('TestFunctionInt',
  7625. LinesToStr([ // statements
  7626. 'this.MyTest = function (Bar) {',
  7627. ' var Result = 0;',
  7628. ' Result = 2*Bar;',
  7629. ' return Result;',
  7630. '};'
  7631. ]),
  7632. LinesToStr([ // this.$main
  7633. ''
  7634. ]));
  7635. end;
  7636. procedure TTestModule.TestFunctionString;
  7637. begin
  7638. StartProgram(false);
  7639. Add('function Test(Bar: string): string;');
  7640. Add('begin');
  7641. Add(' Result:=bar+BAR');
  7642. Add('end;');
  7643. Add('begin');
  7644. ConvertProgram;
  7645. CheckSource('TestFunctionString',
  7646. LinesToStr([ // statements
  7647. 'this.Test = function (Bar) {',
  7648. ' var Result = "";',
  7649. ' Result = Bar+Bar;',
  7650. ' return Result;',
  7651. '};'
  7652. ]),
  7653. LinesToStr([ // this.$main
  7654. ''
  7655. ]));
  7656. end;
  7657. procedure TTestModule.TestIfThen;
  7658. begin
  7659. StartProgram(false);
  7660. Add([
  7661. 'var b: boolean;',
  7662. 'begin',
  7663. ' if b then ;',
  7664. ' if b then else ;']);
  7665. ConvertProgram;
  7666. CheckSource('TestIfThen',
  7667. LinesToStr([ // statements
  7668. 'this.b = false;',
  7669. '']),
  7670. LinesToStr([ // this.$main
  7671. 'if ($mod.b) ;',
  7672. 'if ($mod.b) ;',
  7673. '']));
  7674. end;
  7675. procedure TTestModule.TestForLoop;
  7676. begin
  7677. StartProgram(false);
  7678. Add('var');
  7679. Add(' vI, vJ, vN: longint;');
  7680. Add('begin');
  7681. Add(' VJ:=0;');
  7682. Add(' VN:=3;');
  7683. Add(' for VI:=1 to VN do');
  7684. Add(' begin');
  7685. Add(' VJ:=VJ+VI;');
  7686. Add(' end;');
  7687. ConvertProgram;
  7688. CheckSource('TestForLoop',
  7689. LinesToStr([ // statements
  7690. 'this.vI = 0;',
  7691. 'this.vJ = 0;',
  7692. 'this.vN = 0;'
  7693. ]),
  7694. LinesToStr([ // this.$main
  7695. ' $mod.vJ = 0;',
  7696. ' $mod.vN = 3;',
  7697. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  7698. ' $mod.vI = $l;',
  7699. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7700. ' };',
  7701. '']));
  7702. end;
  7703. procedure TTestModule.TestForLoopInsideFunction;
  7704. begin
  7705. StartProgram(false);
  7706. Add('function SumNumbers(Count: longint): longint;');
  7707. Add('var');
  7708. Add(' vI, vJ: longint;');
  7709. Add('begin');
  7710. Add(' vj:=0;');
  7711. Add(' for vi:=1 to count do');
  7712. Add(' begin');
  7713. Add(' vj:=vj+vi;');
  7714. Add(' end;');
  7715. Add('end;');
  7716. Add('begin');
  7717. Add(' sumnumbers(3);');
  7718. ConvertProgram;
  7719. CheckSource('TestForLoopInsideFunction',
  7720. LinesToStr([ // statements
  7721. 'this.SumNumbers = function (Count) {',
  7722. ' var Result = 0;',
  7723. ' var vI = 0;',
  7724. ' var vJ = 0;',
  7725. ' vJ = 0;',
  7726. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  7727. ' vI = $l;',
  7728. ' vJ = vJ + vI;',
  7729. ' };',
  7730. ' return Result;',
  7731. '};'
  7732. ]),
  7733. LinesToStr([ // $mod.$main
  7734. ' $mod.SumNumbers(3);'
  7735. ]));
  7736. end;
  7737. procedure TTestModule.TestForLoop_ReadVarAfter;
  7738. begin
  7739. StartProgram(false);
  7740. Add('var');
  7741. Add(' vI: longint;');
  7742. Add('begin');
  7743. Add(' for vi:=1 to 2 do ;');
  7744. Add(' if vi=3 then ;');
  7745. ConvertProgram;
  7746. CheckSource('TestForLoop',
  7747. LinesToStr([ // statements
  7748. 'this.vI = 0;'
  7749. ]),
  7750. LinesToStr([ // this.$main
  7751. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7752. ' if ($mod.vI===3) ;'
  7753. ]));
  7754. end;
  7755. procedure TTestModule.TestForLoop_Nested;
  7756. begin
  7757. StartProgram(false);
  7758. Add('function SumNumbers(Count: longint): longint;');
  7759. Add('var');
  7760. Add(' vI, vJ, vK: longint;');
  7761. Add('begin');
  7762. Add(' VK:=0;');
  7763. Add(' for VI:=1 to count do');
  7764. Add(' begin');
  7765. Add(' for vj:=1 to vi do');
  7766. Add(' begin');
  7767. Add(' vk:=VK+VI;');
  7768. Add(' end;');
  7769. Add(' end;');
  7770. Add('end;');
  7771. Add('begin');
  7772. Add(' sumnumbers(3);');
  7773. ConvertProgram;
  7774. CheckSource('TestForLoopInFunction',
  7775. LinesToStr([ // statements
  7776. 'this.SumNumbers = function (Count) {',
  7777. ' var Result = 0;',
  7778. ' var vI = 0;',
  7779. ' var vJ = 0;',
  7780. ' var vK = 0;',
  7781. ' vK = 0;',
  7782. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  7783. ' vI = $l;',
  7784. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  7785. ' vJ = $l1;',
  7786. ' vK = vK + vI;',
  7787. ' };',
  7788. ' };',
  7789. ' return Result;',
  7790. '};'
  7791. ]),
  7792. LinesToStr([ // $mod.$main
  7793. ' $mod.SumNumbers(3);'
  7794. ]));
  7795. end;
  7796. procedure TTestModule.TestRepeatUntil;
  7797. begin
  7798. StartProgram(false);
  7799. Add('var');
  7800. Add(' vI, vJ, vN: longint;');
  7801. Add('begin');
  7802. Add(' vn:=3;');
  7803. Add(' vj:=0;');
  7804. Add(' VI:=0;');
  7805. Add(' repeat');
  7806. Add(' VI:=vi+1;');
  7807. Add(' vj:=VJ+vI;');
  7808. Add(' until vi>=vn');
  7809. ConvertProgram;
  7810. CheckSource('TestRepeatUntil',
  7811. LinesToStr([ // statements
  7812. 'this.vI = 0;',
  7813. 'this.vJ = 0;',
  7814. 'this.vN = 0;'
  7815. ]),
  7816. LinesToStr([ // $mod.$main
  7817. ' $mod.vN = 3;',
  7818. ' $mod.vJ = 0;',
  7819. ' $mod.vI = 0;',
  7820. ' do{',
  7821. ' $mod.vI = $mod.vI + 1;',
  7822. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7823. ' }while(!($mod.vI>=$mod.vN));'
  7824. ]));
  7825. end;
  7826. procedure TTestModule.TestAsmBlock;
  7827. begin
  7828. StartProgram(false);
  7829. Add([
  7830. 'var',
  7831. ' vI: longint;',
  7832. 'begin',
  7833. ' vi:=1;',
  7834. ' asm',
  7835. ' if (vI===1) {',
  7836. ' vI=2;',
  7837. //' console.log(''end;'');', ToDo
  7838. ' }',
  7839. ' if (vI===2){ vI=3; }',
  7840. ' end;',
  7841. ' VI:=4;']);
  7842. ConvertProgram;
  7843. CheckSource('TestAsmBlock',
  7844. LinesToStr([ // statements
  7845. 'this.vI = 0;'
  7846. ]),
  7847. LinesToStr([ // $mod.$main
  7848. '$mod.vI = 1;',
  7849. 'if (vI===1) {',
  7850. ' vI=2;',
  7851. '}',
  7852. 'if (vI===2){ vI=3; }',
  7853. ';',
  7854. '$mod.vI = 4;'
  7855. ]));
  7856. end;
  7857. procedure TTestModule.TestAsmPas_Impl;
  7858. begin
  7859. StartUnit(false);
  7860. Add('interface');
  7861. Add('const cIntf: longint = 1;');
  7862. Add('var vIntf: longint;');
  7863. Add('implementation');
  7864. Add('const cImpl: longint = 2;');
  7865. Add('var vImpl: longint;');
  7866. Add('procedure DoIt;');
  7867. Add('const cLoc: longint = 3;');
  7868. Add('var vLoc: longint;');
  7869. Add('begin;');
  7870. Add(' asm');
  7871. //Add(' pas(vIntf)=pas(cIntf);');
  7872. //Add(' pas(vImpl)=pas(cImpl);');
  7873. //Add(' pas(vLoc)=pas(cLoc);');
  7874. Add(' end;');
  7875. Add('end;');
  7876. ConvertUnit;
  7877. CheckSource('TestAsmPas_Impl',
  7878. LinesToStr([
  7879. 'var $impl = $mod.$impl;',
  7880. 'this.cIntf = 1;',
  7881. 'this.vIntf = 0;',
  7882. '']),
  7883. '', // this.$init
  7884. LinesToStr([ // implementation
  7885. '$impl.cImpl = 2;',
  7886. '$impl.vImpl = 0;',
  7887. 'var cLoc = 3;',
  7888. '$impl.DoIt = function () {',
  7889. ' var vLoc = 0;',
  7890. '};',
  7891. '']) );
  7892. end;
  7893. procedure TTestModule.TestTryFinally;
  7894. begin
  7895. StartProgram(false);
  7896. Add('var i: longint;');
  7897. Add('begin');
  7898. Add(' try');
  7899. Add(' i:=0; i:=2 div i;');
  7900. Add(' finally');
  7901. Add(' i:=3');
  7902. Add(' end;');
  7903. ConvertProgram;
  7904. CheckSource('TestTryFinally',
  7905. LinesToStr([ // statements
  7906. 'this.i = 0;'
  7907. ]),
  7908. LinesToStr([ // $mod.$main
  7909. 'try {',
  7910. ' $mod.i = 0;',
  7911. ' $mod.i = Math.floor(2 / $mod.i);',
  7912. '} finally {',
  7913. ' $mod.i = 3;',
  7914. '};'
  7915. ]));
  7916. end;
  7917. procedure TTestModule.TestTryExcept;
  7918. begin
  7919. StartProgram(false);
  7920. Add([
  7921. 'type',
  7922. ' TObject = class end;',
  7923. ' Exception = class Msg: string; end;',
  7924. ' EInvalidCast = class(Exception) end;',
  7925. 'var vI: longint;',
  7926. 'begin',
  7927. ' try',
  7928. ' vi:=1;',
  7929. ' except',
  7930. ' vi:=2',
  7931. ' end;',
  7932. ' try',
  7933. ' vi:=3;',
  7934. ' except',
  7935. ' raise;',
  7936. ' end;',
  7937. ' try',
  7938. ' VI:=4;',
  7939. ' except',
  7940. ' on einvalidcast do',
  7941. ' raise;',
  7942. ' on E: exception do',
  7943. ' if e.msg='''' then',
  7944. ' raise e;',
  7945. ' else',
  7946. ' vi:=5',
  7947. ' end;',
  7948. ' try',
  7949. ' VI:=6;',
  7950. ' except',
  7951. ' on einvalidcast do ;',
  7952. ' end;',
  7953. '']);
  7954. ConvertProgram;
  7955. CheckSource('TestTryExcept',
  7956. LinesToStr([ // statements
  7957. 'rtl.createClass($mod, "TObject", null, function () {',
  7958. ' this.$init = function () {',
  7959. ' };',
  7960. ' this.$final = function () {',
  7961. ' };',
  7962. '});',
  7963. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7964. ' this.$init = function () {',
  7965. ' $mod.TObject.$init.call(this);',
  7966. ' this.Msg = "";',
  7967. ' };',
  7968. '});',
  7969. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7970. '});',
  7971. 'this.vI = 0;'
  7972. ]),
  7973. LinesToStr([ // $mod.$main
  7974. 'try {',
  7975. ' $mod.vI = 1;',
  7976. '} catch ($e) {',
  7977. ' $mod.vI = 2;',
  7978. '};',
  7979. 'try {',
  7980. ' $mod.vI = 3;',
  7981. '} catch ($e) {',
  7982. ' throw $e;',
  7983. '};',
  7984. 'try {',
  7985. ' $mod.vI = 4;',
  7986. '} catch ($e) {',
  7987. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7988. ' throw $e',
  7989. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7990. ' var E = $e;',
  7991. ' if (E.Msg === "") throw E;',
  7992. ' } else {',
  7993. ' $mod.vI = 5;',
  7994. ' }',
  7995. '};',
  7996. 'try {',
  7997. ' $mod.vI = 6;',
  7998. '} catch ($e) {',
  7999. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8000. ' } else throw $e',
  8001. '};',
  8002. '']));
  8003. end;
  8004. procedure TTestModule.TestTryExcept_ReservedWords;
  8005. begin
  8006. StartProgram(false);
  8007. Add([
  8008. 'type',
  8009. ' TObject = class end;',
  8010. ' Exception = class',
  8011. ' Symbol: string;',
  8012. ' end;',
  8013. 'var &try: longint;',
  8014. 'begin',
  8015. ' try',
  8016. ' &try:=4;',
  8017. ' except',
  8018. ' on Error: exception do',
  8019. ' if errOR.symBol='''' then',
  8020. ' raise ERRor;',
  8021. ' end;',
  8022. '']);
  8023. ConvertProgram;
  8024. CheckSource('TestTryExcept_ReservedWords',
  8025. LinesToStr([ // statements
  8026. 'rtl.createClass($mod, "TObject", null, function () {',
  8027. ' this.$init = function () {',
  8028. ' };',
  8029. ' this.$final = function () {',
  8030. ' };',
  8031. '});',
  8032. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  8033. ' this.$init = function () {',
  8034. ' $mod.TObject.$init.call(this);',
  8035. ' this.Symbol = "";',
  8036. ' };',
  8037. '});',
  8038. 'this.Try = 0;',
  8039. '']),
  8040. LinesToStr([ // $mod.$main
  8041. 'try {',
  8042. ' $mod.Try = 4;',
  8043. '} catch ($e) {',
  8044. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8045. ' var error = $e;',
  8046. ' if (error.Symbol === "") throw error;',
  8047. ' } else throw $e',
  8048. '};',
  8049. '']));
  8050. end;
  8051. procedure TTestModule.TestIfThenRaiseElse;
  8052. begin
  8053. StartProgram(false);
  8054. Add([
  8055. 'type',
  8056. ' TObject = class',
  8057. ' constructor Create;',
  8058. ' end;',
  8059. 'constructor TObject.Create;',
  8060. 'begin',
  8061. 'end;',
  8062. 'var b: boolean;',
  8063. 'begin',
  8064. ' if b then',
  8065. ' raise TObject.Create',
  8066. ' else',
  8067. ' b:=false;',
  8068. '']);
  8069. ConvertProgram;
  8070. CheckSource('TestIfThenRaiseElse',
  8071. LinesToStr([ // statements
  8072. 'rtl.createClass($mod, "TObject", null, function () {',
  8073. ' this.$init = function () {',
  8074. ' };',
  8075. ' this.$final = function () {',
  8076. ' };',
  8077. ' this.Create = function () {',
  8078. ' return this;',
  8079. ' };',
  8080. '});',
  8081. 'this.b = false;',
  8082. '']),
  8083. LinesToStr([ // $mod.$main
  8084. 'if ($mod.b) {',
  8085. ' throw $mod.TObject.$create("Create")}',
  8086. ' else $mod.b = false;',
  8087. '']));
  8088. end;
  8089. procedure TTestModule.TestCaseOf;
  8090. begin
  8091. StartProgram(false);
  8092. Add([
  8093. 'const e: longint; external name ''$e'';',
  8094. 'var vI: longint;',
  8095. 'begin',
  8096. ' case vi of',
  8097. ' 1: ;',
  8098. ' 2: vi:=3;',
  8099. ' e: ;',
  8100. ' else',
  8101. ' VI:=4',
  8102. ' end;']);
  8103. ConvertProgram;
  8104. CheckSource('TestCaseOf',
  8105. LinesToStr([ // statements
  8106. 'this.vI = 0;'
  8107. ]),
  8108. LinesToStr([ // $mod.$main
  8109. 'var $tmp = $mod.vI;',
  8110. 'if ($tmp === 1) {}',
  8111. 'else if ($tmp === 2) {',
  8112. ' $mod.vI = 3}',
  8113. ' else if ($tmp === $e) {}',
  8114. 'else {',
  8115. ' $mod.vI = 4;',
  8116. '};'
  8117. ]));
  8118. end;
  8119. procedure TTestModule.TestCaseOf_UseSwitch;
  8120. begin
  8121. StartProgram(false);
  8122. Converter.UseSwitchStatement:=true;
  8123. Add('var Vi: longint;');
  8124. Add('begin');
  8125. Add(' case vi of');
  8126. Add(' 1: ;');
  8127. Add(' 2: VI:=3;');
  8128. Add(' else');
  8129. Add(' vi:=4');
  8130. Add(' end;');
  8131. ConvertProgram;
  8132. CheckSource('TestCaseOf_UseSwitch',
  8133. LinesToStr([ // statements
  8134. 'this.Vi = 0;'
  8135. ]),
  8136. LinesToStr([ // $mod.$main
  8137. 'switch ($mod.Vi) {',
  8138. 'case 1:',
  8139. ' break;',
  8140. 'case 2:',
  8141. ' $mod.Vi = 3;',
  8142. ' break;',
  8143. 'default:',
  8144. ' $mod.Vi = 4;',
  8145. '};'
  8146. ]));
  8147. end;
  8148. procedure TTestModule.TestCaseOfNoElse;
  8149. begin
  8150. StartProgram(false);
  8151. Add('var Vi: longint;');
  8152. Add('begin');
  8153. Add(' case vi of');
  8154. Add(' 1: begin vi:=2; VI:=3; end;');
  8155. Add(' end;');
  8156. ConvertProgram;
  8157. CheckSource('TestCaseOfNoElse',
  8158. LinesToStr([ // statements
  8159. 'this.Vi = 0;'
  8160. ]),
  8161. LinesToStr([ // $mod.$main
  8162. 'var $tmp = $mod.Vi;',
  8163. 'if ($tmp === 1) {',
  8164. ' $mod.Vi = 2;',
  8165. ' $mod.Vi = 3;',
  8166. '};'
  8167. ]));
  8168. end;
  8169. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8170. begin
  8171. StartProgram(false);
  8172. Converter.UseSwitchStatement:=true;
  8173. Add('var vI: longint;');
  8174. Add('begin');
  8175. Add(' case vi of');
  8176. Add(' 1: begin VI:=2; vi:=3; end;');
  8177. Add(' end;');
  8178. ConvertProgram;
  8179. CheckSource('TestCaseOfNoElse_UseSwitch',
  8180. LinesToStr([ // statements
  8181. 'this.vI = 0;'
  8182. ]),
  8183. LinesToStr([ // $mod.$main
  8184. 'switch ($mod.vI) {',
  8185. 'case 1:',
  8186. ' $mod.vI = 2;',
  8187. ' $mod.vI = 3;',
  8188. ' break;',
  8189. '};'
  8190. ]));
  8191. end;
  8192. procedure TTestModule.TestCaseOfRange;
  8193. begin
  8194. StartProgram(false);
  8195. Add('var vI: longint;');
  8196. Add('begin');
  8197. Add(' case vi of');
  8198. Add(' 1..3: vi:=14;');
  8199. Add(' 4,5: vi:=16;');
  8200. Add(' 6..7,9..10: ;');
  8201. Add(' else ;');
  8202. Add(' end;');
  8203. ConvertProgram;
  8204. CheckSource('TestCaseOfRange',
  8205. LinesToStr([ // statements
  8206. 'this.vI = 0;'
  8207. ]),
  8208. LinesToStr([ // $mod.$main
  8209. 'var $tmp = $mod.vI;',
  8210. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8211. ' $mod.vI = 14',
  8212. '} else if (($tmp === 4) || ($tmp === 5)){',
  8213. ' $mod.vI = 16',
  8214. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8215. ]));
  8216. end;
  8217. procedure TTestModule.TestCaseOfString;
  8218. begin
  8219. StartProgram(false);
  8220. Add([
  8221. 'var s,h: string;',
  8222. 'begin',
  8223. ' case s of',
  8224. ' ''foo'': s:=h;',
  8225. ' ''a''..''z'': h:=s;',
  8226. ' ''ў'', ''ё'': ;',
  8227. ' ''Б''..''Я'': ;',
  8228. ' end;',
  8229. '']);
  8230. ConvertProgram;
  8231. CheckSource('TestCaseOfString',
  8232. LinesToStr([ // statements
  8233. 'this.s = "";',
  8234. 'this.h = "";',
  8235. '']),
  8236. LinesToStr([ // $mod.$main
  8237. 'var $tmp = $mod.s;',
  8238. 'if ($tmp === "foo") {',
  8239. ' $mod.s = $mod.h}',
  8240. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8241. ' $mod.h = $mod.s}',
  8242. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8243. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8244. '']));
  8245. end;
  8246. procedure TTestModule.TestCaseOfChar;
  8247. begin
  8248. StartProgram(false);
  8249. Add([
  8250. 'var s,h: char;',
  8251. 'begin',
  8252. ' case s of',
  8253. ' ''a''..''z'': h:=s;',
  8254. ' ''ä'': ;',
  8255. ' ''ў'', ''ё'': ;',
  8256. ' ''Б''..''Я'': ;',
  8257. ' end;',
  8258. '']);
  8259. ConvertProgram;
  8260. CheckSource('TestCaseOfString',
  8261. LinesToStr([ // statements
  8262. 'this.s = "";',
  8263. 'this.h = "";',
  8264. '']),
  8265. LinesToStr([ // $mod.$main
  8266. 'var $tmp = $mod.s;',
  8267. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8268. ' $mod.h = $mod.s}',
  8269. ' else if ($tmp === "ä") {}',
  8270. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8271. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8272. '']));
  8273. end;
  8274. procedure TTestModule.TestCaseOfExternalClassConst;
  8275. begin
  8276. StartProgram(false);
  8277. Add([
  8278. '{$modeswitch externalclass}',
  8279. 'type',
  8280. ' TBird = class external name ''Bird''',
  8281. ' const e: longint;',
  8282. ' end;',
  8283. 'var vI: longint;',
  8284. 'begin',
  8285. ' case vi of',
  8286. ' 1: vi:=3;',
  8287. ' TBird.e: ;',
  8288. ' end;']);
  8289. ConvertProgram;
  8290. CheckSource('TestCaseOfExternalClassConst',
  8291. LinesToStr([ // statements
  8292. 'this.vI = 0;'
  8293. ]),
  8294. LinesToStr([ // $mod.$main
  8295. 'var $tmp = $mod.vI;',
  8296. 'if ($tmp === 1) {',
  8297. ' $mod.vI = 3}',
  8298. ' else if ($tmp === Bird.e) ;'
  8299. ]));
  8300. end;
  8301. procedure TTestModule.TestDebugger;
  8302. begin
  8303. StartProgram(false);
  8304. Add([
  8305. 'procedure DoIt;',
  8306. 'begin',
  8307. ' deBugger;',
  8308. ' DeBugger();',
  8309. 'end;',
  8310. 'begin',
  8311. ' Debugger;']);
  8312. ConvertProgram;
  8313. CheckSource('TestDebugger',
  8314. LinesToStr([ // statements
  8315. 'this.DoIt = function () {',
  8316. ' debugger;',
  8317. ' debugger;',
  8318. '};',
  8319. '']),
  8320. LinesToStr([ // $mod.$main
  8321. 'debugger;',
  8322. '']));
  8323. end;
  8324. procedure TTestModule.TestArray_Dynamic;
  8325. begin
  8326. StartProgram(false);
  8327. Add([
  8328. 'type',
  8329. ' TArrayInt = array of longint;',
  8330. 'var',
  8331. ' Arr: TArrayInt;',
  8332. ' i: longint;',
  8333. ' b: boolean;',
  8334. 'begin',
  8335. ' SetLength(arr,3);',
  8336. ' arr[0]:=4;',
  8337. ' arr[1]:=length(arr)+arr[0];',
  8338. ' arr[i]:=5;',
  8339. ' arr[arr[i]]:=arr[6];',
  8340. ' i:=low(arr);',
  8341. ' i:=high(arr);',
  8342. ' b:=Assigned(arr);',
  8343. ' Arr:=default(TArrayInt);']);
  8344. ConvertProgram;
  8345. CheckSource('TestArray_Dynamic',
  8346. LinesToStr([ // statements
  8347. 'this.Arr = [];',
  8348. 'this.i = 0;',
  8349. 'this.b = false;'
  8350. ]),
  8351. LinesToStr([ // $mod.$main
  8352. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8353. '$mod.Arr[0] = 4;',
  8354. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8355. '$mod.Arr[$mod.i] = 5;',
  8356. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8357. '$mod.i = 0;',
  8358. '$mod.i = rtl.length($mod.Arr) - 1;',
  8359. '$mod.b = rtl.length($mod.Arr) > 0;',
  8360. '$mod.Arr = [];',
  8361. '']));
  8362. end;
  8363. procedure TTestModule.TestArray_Dynamic_Nil;
  8364. begin
  8365. StartProgram(false);
  8366. Add('type');
  8367. Add(' TArrayInt = array of longint;');
  8368. Add('var');
  8369. Add(' Arr: TArrayInt;');
  8370. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8371. Add('begin');
  8372. Add(' arr:=nil;');
  8373. Add(' if arr=nil then;');
  8374. Add(' if nil=arr then;');
  8375. Add(' if arr<>nil then;');
  8376. Add(' if nil<>arr then;');
  8377. Add(' DoIt(nil,nil);');
  8378. ConvertProgram;
  8379. CheckSource('TestArray_Dynamic',
  8380. LinesToStr([ // statements
  8381. 'this.Arr = [];',
  8382. 'this.DoIt = function(i,j){',
  8383. '};'
  8384. ]),
  8385. LinesToStr([ // $mod.$main
  8386. '$mod.Arr = [];',
  8387. 'if (rtl.length($mod.Arr) === 0) ;',
  8388. 'if (rtl.length($mod.Arr) === 0) ;',
  8389. 'if (rtl.length($mod.Arr) > 0) ;',
  8390. 'if (rtl.length($mod.Arr) > 0) ;',
  8391. '$mod.DoIt([],[]);',
  8392. '']));
  8393. end;
  8394. procedure TTestModule.TestArray_DynMultiDimensional;
  8395. begin
  8396. StartProgram(false);
  8397. Add([
  8398. 'type',
  8399. ' TArrayInt = array of longint;',
  8400. ' TArrayArrayInt = array of TArrayInt;',
  8401. 'var',
  8402. ' Arr: TArrayInt;',
  8403. ' Arr2: TArrayArrayInt;',
  8404. ' i: longint;',
  8405. 'begin',
  8406. ' arr2:=nil;',
  8407. ' if arr2=nil then;',
  8408. ' if nil=arr2 then;',
  8409. ' i:=low(arr2);',
  8410. ' i:=low(arr2[1]);',
  8411. ' i:=high(arr2);',
  8412. ' i:=high(arr2[2]);',
  8413. ' arr2[3]:=arr;',
  8414. ' arr2[4][5]:=i;',
  8415. ' i:=arr2[6][7];',
  8416. ' arr2[8,9]:=i;',
  8417. ' i:=arr2[10,11];',
  8418. ' SetLength(arr2,14);',
  8419. ' SetLength(arr2[15],16);']);
  8420. ConvertProgram;
  8421. CheckSource('TestArray_Dynamic',
  8422. LinesToStr([ // statements
  8423. 'this.Arr = [];',
  8424. 'this.Arr2 = [];',
  8425. 'this.i = 0;'
  8426. ]),
  8427. LinesToStr([ // $mod.$main
  8428. '$mod.Arr2 = [];',
  8429. 'if (rtl.length($mod.Arr2) === 0) ;',
  8430. 'if (rtl.length($mod.Arr2) === 0) ;',
  8431. '$mod.i = 0;',
  8432. '$mod.i = 0;',
  8433. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8434. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8435. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8436. '$mod.Arr2[4][5] = $mod.i;',
  8437. '$mod.i = $mod.Arr2[6][7];',
  8438. '$mod.Arr2[8][9] = $mod.i;',
  8439. '$mod.i = $mod.Arr2[10][11];',
  8440. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8441. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8442. '']));
  8443. end;
  8444. procedure TTestModule.TestArray_DynamicAssign;
  8445. begin
  8446. StartProgram(false);
  8447. Add([
  8448. 'type',
  8449. ' TArrayInt = array of longint;',
  8450. ' TArrayArrayInt = array of TArrayInt;',
  8451. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8452. 'begin',
  8453. 'end;',
  8454. 'procedure Fly(var a: TArrayInt);',
  8455. 'begin',
  8456. 'end;',
  8457. 'var',
  8458. ' Arr: TArrayInt;',
  8459. ' Arr2: TArrayArrayInt;',
  8460. 'begin',
  8461. ' arr:=nil;',
  8462. ' arr2:=nil;',
  8463. ' arr2[1]:=nil;',
  8464. ' arr2[2]:=arr;',
  8465. ' Run(arr,arr,arr);',
  8466. ' Fly(arr);',
  8467. ' Run(arr2[4],arr2[5],arr2[6]);',
  8468. ' Fly(arr2[7]);',
  8469. '']);
  8470. ConvertProgram;
  8471. CheckSource('TestArray_DynamicAssign',
  8472. LinesToStr([ // statements
  8473. 'this.Run = function (a, b, c) {',
  8474. '};',
  8475. 'this.Fly = function (a) {',
  8476. '};',
  8477. 'this.Arr = [];',
  8478. 'this.Arr2 = [];',
  8479. '']),
  8480. LinesToStr([ // $mod.$main
  8481. '$mod.Arr = [];',
  8482. '$mod.Arr2 = [];',
  8483. '$mod.Arr2[1] = [];',
  8484. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8485. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8486. '$mod.Fly({',
  8487. ' p: $mod,',
  8488. ' get: function () {',
  8489. ' return this.p.Arr;',
  8490. ' },',
  8491. ' set: function (v) {',
  8492. ' this.p.Arr = v;',
  8493. ' }',
  8494. '});',
  8495. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8496. '$mod.Fly({',
  8497. ' a: 7,',
  8498. ' p: $mod.Arr2,',
  8499. ' get: function () {',
  8500. ' return this.p[this.a];',
  8501. ' },',
  8502. ' set: function (v) {',
  8503. ' this.p[this.a] = v;',
  8504. ' }',
  8505. '});',
  8506. '']));
  8507. end;
  8508. procedure TTestModule.TestArray_StaticInt;
  8509. begin
  8510. StartProgram(false);
  8511. Add('type');
  8512. Add(' TArrayInt = array[2..4] of longint;');
  8513. Add('var');
  8514. Add(' Arr: TArrayInt;');
  8515. Add(' Arr2: TArrayInt = (5,6,7);');
  8516. Add(' i: longint;');
  8517. Add(' b: boolean;');
  8518. Add('begin');
  8519. Add(' arr[2]:=4;');
  8520. Add(' arr[3]:=arr[2]+arr[3];');
  8521. Add(' arr[i]:=5;');
  8522. Add(' arr[arr[i]]:=arr[high(arr)];');
  8523. Add(' i:=low(arr);');
  8524. Add(' i:=high(arr);');
  8525. Add(' b:=arr[2]=arr[3];');
  8526. Add(' arr:=default(TArrayInt);');
  8527. ConvertProgram;
  8528. CheckSource('TestArray_StaticInt',
  8529. LinesToStr([ // statements
  8530. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8531. 'this.Arr2 = [5, 6, 7];',
  8532. 'this.i = 0;',
  8533. 'this.b = false;'
  8534. ]),
  8535. LinesToStr([ // $mod.$main
  8536. '$mod.Arr[0] = 4;',
  8537. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8538. '$mod.Arr[$mod.i-2] = 5;',
  8539. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8540. '$mod.i = 2;',
  8541. '$mod.i = 4;',
  8542. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8543. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8544. '']));
  8545. end;
  8546. procedure TTestModule.TestArray_StaticBool;
  8547. begin
  8548. StartProgram(false);
  8549. Add('type');
  8550. Add(' TBools = array[boolean] of boolean;');
  8551. Add(' TBool2 = array[true..true] of boolean;');
  8552. Add('var');
  8553. Add(' Arr: TBools;');
  8554. Add(' Arr2: TBool2;');
  8555. Add(' Arr3: TBools = (true,false);');
  8556. Add(' b: boolean;');
  8557. Add('begin');
  8558. Add(' b:=low(arr);');
  8559. Add(' b:=high(arr);');
  8560. Add(' arr[true]:=false;');
  8561. Add(' arr[false]:=arr[b] or arr[true];');
  8562. Add(' arr[b]:=true;');
  8563. Add(' arr[arr[b]]:=arr[high(arr)];');
  8564. Add(' b:=arr[false]=arr[true];');
  8565. Add(' b:=low(arr2);');
  8566. Add(' b:=high(arr2);');
  8567. Add(' arr2[true]:=true;');
  8568. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8569. Add(' arr2[b]:=false;');
  8570. ConvertProgram;
  8571. CheckSource('TestArray_StaticBool',
  8572. LinesToStr([ // statements
  8573. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8574. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8575. 'this.Arr3 = [true, false];',
  8576. 'this.b = false;'
  8577. ]),
  8578. LinesToStr([ // $mod.$main
  8579. '$mod.b = false;',
  8580. '$mod.b = true;',
  8581. '$mod.Arr[1] = false;',
  8582. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8583. '$mod.Arr[+$mod.b] = true;',
  8584. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8585. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8586. '$mod.b = true;',
  8587. '$mod.b = true;',
  8588. '$mod.Arr2[0] = true;',
  8589. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8590. '$mod.Arr2[1-$mod.b] = false;',
  8591. '']));
  8592. end;
  8593. procedure TTestModule.TestArray_StaticChar;
  8594. begin
  8595. StartProgram(false);
  8596. Add([
  8597. 'type',
  8598. ' TChars = array[char] of char;',
  8599. ' TChars2 = array[''a''..''z''] of char;',
  8600. 'var',
  8601. ' Arr: TChars;',
  8602. ' Arr2: TChars2;',
  8603. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8604. ' Arr4: array[11..13] of char = ''pas'';',
  8605. ' Arr5: array[21..22] of char = ''äö'';',
  8606. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8607. ' c: char;',
  8608. ' b: boolean;',
  8609. 'begin',
  8610. ' c:=low(arr);',
  8611. ' c:=high(arr);',
  8612. ' arr[''B'']:=''a'';',
  8613. ' arr[''D'']:=arr[c];',
  8614. ' arr[c]:=arr[''d''];',
  8615. ' arr[arr[c]]:=arr[high(arr)];',
  8616. ' b:=arr[low(arr)]=arr[''e''];',
  8617. ' c:=low(arr2);',
  8618. ' c:=high(arr2);',
  8619. ' arr2[''b'']:=''f'';',
  8620. ' arr2[''a'']:=arr2[c];',
  8621. ' arr2[c]:=arr2[''g''];']);
  8622. ConvertProgram;
  8623. CheckSource('TestArray_StaticChar',
  8624. LinesToStr([ // statements
  8625. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8626. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8627. 'this.Arr3 = ["p", "a", "s"];',
  8628. 'this.Arr4 = ["p", "a", "s"];',
  8629. 'this.Arr5 = ["ä", "ö"];',
  8630. 'this.Arr6 = ["ä", "ö"];',
  8631. 'this.c = "";',
  8632. 'this.b = false;',
  8633. '']),
  8634. LinesToStr([ // $mod.$main
  8635. '$mod.c = "\x00";',
  8636. '$mod.c = "\uFFFF";',
  8637. '$mod.Arr[66] = "a";',
  8638. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8639. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8640. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8641. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8642. '$mod.c = "a";',
  8643. '$mod.c = "z";',
  8644. '$mod.Arr2[1] = "f";',
  8645. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8646. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8647. '']));
  8648. end;
  8649. procedure TTestModule.TestArray_StaticMultiDim;
  8650. begin
  8651. StartProgram(false);
  8652. Add([
  8653. 'type',
  8654. ' TArrayInt = array[1..3] of longint;',
  8655. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8656. 'var',
  8657. ' Arr: TArrayInt;',
  8658. ' Arr2: TArrayArrayInt;',
  8659. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8660. ' i: longint;',
  8661. 'begin',
  8662. ' i:=low(arr);',
  8663. ' i:=low(arr2);',
  8664. ' i:=low(arr2[5]);',
  8665. ' i:=high(arr);',
  8666. ' i:=high(arr2);',
  8667. ' i:=high(arr2[6]);',
  8668. ' arr2[5]:=arr;',
  8669. ' arr2[6][2]:=i;',
  8670. ' i:=arr2[6][3];',
  8671. ' arr2[6,3]:=i;',
  8672. ' i:=arr2[5,2];',
  8673. ' arr2:=arr2;',// clone multi dim static array
  8674. //' arr3:=arr3;',// clone anonymous multi dim static array
  8675. '']);
  8676. ConvertProgram;
  8677. CheckSource('TestArray_StaticMultiDim',
  8678. LinesToStr([ // statements
  8679. 'this.TArrayArrayInt$clone = function (a) {',
  8680. ' var r = [];',
  8681. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8682. ' return r;',
  8683. '};',
  8684. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8685. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8686. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8687. 'this.i = 0;'
  8688. ]),
  8689. LinesToStr([ // $mod.$main
  8690. '$mod.i = 1;',
  8691. '$mod.i = 5;',
  8692. '$mod.i = 1;',
  8693. '$mod.i = 3;',
  8694. '$mod.i = 6;',
  8695. '$mod.i = 3;',
  8696. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8697. '$mod.Arr2[1][1] = $mod.i;',
  8698. '$mod.i = $mod.Arr2[1][2];',
  8699. '$mod.Arr2[1][2] = $mod.i;',
  8700. '$mod.i = $mod.Arr2[0][1];',
  8701. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8702. '']));
  8703. end;
  8704. procedure TTestModule.TestArray_StaticInFunction;
  8705. begin
  8706. StartProgram(false);
  8707. Add([
  8708. 'const TArrayInt = 3;',
  8709. 'const TArrayArrayInt = 4;',
  8710. 'procedure DoIt;',
  8711. 'type',
  8712. ' TArrayInt = array[1..3] of longint;',
  8713. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8714. 'var',
  8715. ' Arr: TArrayInt;',
  8716. ' Arr2: TArrayArrayInt;',
  8717. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8718. ' i: longint;',
  8719. 'begin',
  8720. ' arr2[5]:=arr;',
  8721. ' arr2:=arr2;',// clone multi dim static array
  8722. 'end;',
  8723. 'begin',
  8724. '']);
  8725. ConvertProgram;
  8726. CheckSource('TestArray_StaticInFunction',
  8727. LinesToStr([ // statements
  8728. 'this.TArrayInt = 3;',
  8729. 'this.TArrayArrayInt = 4;',
  8730. 'var TArrayArrayInt$1$clone = function (a) {',
  8731. ' var r = [];',
  8732. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8733. ' return r;',
  8734. '};',
  8735. 'this.DoIt = function () {',
  8736. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8737. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8738. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8739. ' var i = 0;',
  8740. ' Arr2[0] = Arr.slice(0);',
  8741. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8742. '};',
  8743. '']),
  8744. LinesToStr([ // $mod.$main
  8745. '']));
  8746. end;
  8747. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  8748. begin
  8749. StartProgram(false);
  8750. Add([
  8751. 'type',
  8752. ' TArrayInt = array[1..3,1..2] of longint;',
  8753. 'var',
  8754. ' a,b: TArrayInt;',
  8755. 'begin',
  8756. ' if a=b then ;',
  8757. '']);
  8758. SetExpectedPasResolverError('compare static array is not supported',
  8759. nXIsNotSupported);
  8760. ConvertProgram;
  8761. end;
  8762. procedure TTestModule.TestArrayOfRecord;
  8763. begin
  8764. StartProgram(false);
  8765. Add([
  8766. 'type',
  8767. ' TRec = record',
  8768. ' Int: longint;',
  8769. ' end;',
  8770. ' TArrayRec = array of TRec;',
  8771. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8772. 'begin',
  8773. 'end;',
  8774. 'var',
  8775. ' Arr: TArrayRec;',
  8776. ' r: TRec;',
  8777. ' i: longint;',
  8778. 'begin',
  8779. ' SetLength(arr,3);',
  8780. ' arr[0].int:=4;',
  8781. ' arr[1].int:=length(arr)+arr[2].int;',
  8782. ' arr[arr[i].int].int:=arr[5].int;',
  8783. ' arr[7]:=r;',
  8784. ' r:=arr[8];',
  8785. ' i:=low(arr);',
  8786. ' i:=high(arr);',
  8787. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8788. ConvertProgram;
  8789. CheckSource('TestArrayOfRecord',
  8790. LinesToStr([ // statements
  8791. 'rtl.recNewT($mod, "TRec", function () {',
  8792. ' this.Int = 0;',
  8793. ' this.$eq = function (b) {',
  8794. ' return this.Int === b.Int;',
  8795. ' };',
  8796. ' this.$assign = function (s) {',
  8797. ' this.Int = s.Int;',
  8798. ' return this;',
  8799. ' };',
  8800. '});',
  8801. 'this.DoIt = function (vd, vc, vv) {',
  8802. '};',
  8803. 'this.Arr = [];',
  8804. 'this.r = $mod.TRec.$new();',
  8805. 'this.i = 0;'
  8806. ]),
  8807. LinesToStr([ // $mod.$main
  8808. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8809. '$mod.Arr[0].Int = 4;',
  8810. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8811. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8812. '$mod.Arr[7].$assign($mod.r);',
  8813. '$mod.r.$assign($mod.Arr[8]);',
  8814. '$mod.i = 0;',
  8815. '$mod.i = rtl.length($mod.Arr)-1;',
  8816. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8817. '']));
  8818. end;
  8819. procedure TTestModule.TestArray_StaticRecord;
  8820. begin
  8821. StartProgram(false);
  8822. Add([
  8823. 'type',
  8824. ' TRec = record',
  8825. ' Int: longint;',
  8826. ' end;',
  8827. ' TArrayRec = array[1..2] of TRec;',
  8828. 'var',
  8829. ' Arr: TArrayRec;',
  8830. 'begin',
  8831. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8832. '']);
  8833. ConvertProgram;
  8834. CheckSource('TestArray_StaticRecord',
  8835. LinesToStr([ // statements
  8836. 'rtl.recNewT($mod, "TRec", function () {',
  8837. ' this.Int = 0;',
  8838. ' this.$eq = function (b) {',
  8839. ' return this.Int === b.Int;',
  8840. ' };',
  8841. ' this.$assign = function (s) {',
  8842. ' this.Int = s.Int;',
  8843. ' return this;',
  8844. ' };',
  8845. '});',
  8846. 'this.TArrayRec$clone = function (a) {',
  8847. ' var r = [];',
  8848. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8849. ' return r;',
  8850. '};',
  8851. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8852. '']),
  8853. LinesToStr([ // $mod.$main
  8854. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8855. end;
  8856. procedure TTestModule.TestArrayOfSet;
  8857. begin
  8858. StartProgram(false);
  8859. Add([
  8860. 'type',
  8861. ' TFlag = (big,small);',
  8862. ' TSetOfFlag = set of tflag;',
  8863. ' TArrayFlag = array of TSetOfFlag;',
  8864. 'procedure DoIt(const a: Tarrayflag);',
  8865. 'begin',
  8866. 'end;',
  8867. 'var',
  8868. ' f: TFlag;',
  8869. ' s: TSetOfFlag;',
  8870. ' Arr: TArrayFlag;',
  8871. ' i: longint;',
  8872. 'begin',
  8873. ' SetLength(arr,3);',
  8874. ' arr[0]:=s;',
  8875. ' arr[1]:=[big];',
  8876. ' arr[2]:=[big]+s;',
  8877. ' arr[3]:=s+[big];',
  8878. ' arr[4]:=arr[5];',
  8879. ' s:=arr[6];',
  8880. ' i:=low(arr);',
  8881. ' i:=high(arr);',
  8882. ' DoIt(arr);',
  8883. ' DoIt([s]);',
  8884. ' DoIt([[],s]);',
  8885. ' DoIt([s,[]]);',
  8886. '']);
  8887. ConvertProgram;
  8888. CheckSource('TestArrayOfSet',
  8889. LinesToStr([ // statements
  8890. 'this.TFlag = {',
  8891. ' "0": "big",',
  8892. ' big: 0,',
  8893. ' "1": "small",',
  8894. ' small: 1',
  8895. '};',
  8896. 'this.DoIt = function (a) {',
  8897. '};',
  8898. 'this.f = 0;',
  8899. 'this.s = {};',
  8900. 'this.Arr = [];',
  8901. 'this.i = 0;',
  8902. '']),
  8903. LinesToStr([ // $mod.$main
  8904. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8905. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8906. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8907. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8908. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8909. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8910. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8911. '$mod.i = 0;',
  8912. '$mod.i = rtl.length($mod.Arr) - 1;',
  8913. '$mod.DoIt($mod.Arr);',
  8914. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8915. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8916. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8917. '']));
  8918. end;
  8919. procedure TTestModule.TestArray_DynAsParam;
  8920. begin
  8921. StartProgram(false);
  8922. Add([
  8923. 'type integer = longint;',
  8924. 'type TArrInt = array of integer;',
  8925. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8926. 'var vJ: TArrInt;',
  8927. 'begin',
  8928. ' vg:=vg;',
  8929. ' vj:=vh;',
  8930. ' vi:=vi;',
  8931. ' doit(vg,vg,vg);',
  8932. ' doit(vh,vh,vj);',
  8933. ' doit(vi,vi,vi);',
  8934. ' doit(vj,vj,vj);',
  8935. 'end;',
  8936. 'var i: TArrInt;',
  8937. 'begin',
  8938. ' doit(i,i,i);']);
  8939. ConvertProgram;
  8940. CheckSource('TestArray_DynAsParams',
  8941. LinesToStr([ // statements
  8942. 'this.DoIt = function (vG,vH,vI) {',
  8943. ' var vJ = [];',
  8944. ' vG = rtl.arrayRef(vG);',
  8945. ' vJ = rtl.arrayRef(vH);',
  8946. ' vI.set(rtl.arrayRef(vI.get()));',
  8947. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  8948. ' get: function () {',
  8949. ' return vG;',
  8950. ' },',
  8951. ' set: function (v) {',
  8952. ' vG = v;',
  8953. ' }',
  8954. ' });',
  8955. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  8956. ' get: function () {',
  8957. ' return vJ;',
  8958. ' },',
  8959. ' set: function (v) {',
  8960. ' vJ = v;',
  8961. ' }',
  8962. ' });',
  8963. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  8964. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  8965. ' get: function () {',
  8966. ' return vJ;',
  8967. ' },',
  8968. ' set: function (v) {',
  8969. ' vJ = v;',
  8970. ' }',
  8971. ' });',
  8972. '};',
  8973. 'this.i = [];'
  8974. ]),
  8975. LinesToStr([
  8976. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  8977. ' p: $mod,',
  8978. ' get: function () {',
  8979. ' return this.p.i;',
  8980. ' },',
  8981. ' set: function (v) {',
  8982. ' this.p.i = v;',
  8983. ' }',
  8984. '});'
  8985. ]));
  8986. end;
  8987. procedure TTestModule.TestArray_StaticAsParam;
  8988. begin
  8989. StartProgram(false);
  8990. Add([
  8991. 'type integer = longint;',
  8992. 'type TArrInt = array[1..2] of integer;',
  8993. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8994. 'var vJ: TArrInt;',
  8995. 'begin',
  8996. ' vg:=vg;',
  8997. ' vj:=vh;',
  8998. ' vi:=vi;',
  8999. ' doit(vg,vg,vg);',
  9000. ' doit(vh,vh,vj);',
  9001. ' doit(vi,vi,vi);',
  9002. ' doit(vj,vj,vj);',
  9003. 'end;',
  9004. 'var i: TArrInt;',
  9005. 'begin',
  9006. ' doit(i,i,i);']);
  9007. ConvertProgram;
  9008. CheckSource('TestArray_StaticAsParams',
  9009. LinesToStr([ // statements
  9010. 'this.DoIt = function (vG,vH,vI) {',
  9011. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9012. ' vG = vG.slice(0);',
  9013. ' vJ = vH.slice(0);',
  9014. ' vI.set(vI.get().slice(0));',
  9015. ' $mod.DoIt(vG.slice(0), vG, {',
  9016. ' get: function () {',
  9017. ' return vG;',
  9018. ' },',
  9019. ' set: function (v) {',
  9020. ' vG = v;',
  9021. ' }',
  9022. ' });',
  9023. ' $mod.DoIt(vH.slice(0), vH, {',
  9024. ' get: function () {',
  9025. ' return vJ;',
  9026. ' },',
  9027. ' set: function (v) {',
  9028. ' vJ = v;',
  9029. ' }',
  9030. ' });',
  9031. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9032. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9033. ' get: function () {',
  9034. ' return vJ;',
  9035. ' },',
  9036. ' set: function (v) {',
  9037. ' vJ = v;',
  9038. ' }',
  9039. ' });',
  9040. '};',
  9041. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9042. ]),
  9043. LinesToStr([
  9044. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9045. ' p: $mod,',
  9046. ' get: function () {',
  9047. ' return this.p.i;',
  9048. ' },',
  9049. ' set: function (v) {',
  9050. ' this.p.i = v;',
  9051. ' }',
  9052. '});'
  9053. ]));
  9054. end;
  9055. procedure TTestModule.TestArrayElement_AsParams;
  9056. begin
  9057. StartProgram(false);
  9058. Add('type integer = longint;');
  9059. Add('type TArrayInt = array of integer;');
  9060. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9061. Add('var vJ: tarrayint;');
  9062. Add('begin');
  9063. Add(' vi:=vi;');
  9064. Add(' doit(vi,vi,vi);');
  9065. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9066. Add('end;');
  9067. Add('var a: TArrayInt;');
  9068. Add('begin');
  9069. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9070. ConvertProgram;
  9071. CheckSource('TestArrayElement_AsParams',
  9072. LinesToStr([ // statements
  9073. 'this.DoIt = function (vG,vH,vI) {',
  9074. ' var vJ = [];',
  9075. ' vI.set(vI.get());',
  9076. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9077. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9078. ' a:1+3,',
  9079. ' p:vJ,',
  9080. ' get: function () {',
  9081. ' return this.p[this.a];',
  9082. ' },',
  9083. ' set: function (v) {',
  9084. ' this.p[this.a] = v;',
  9085. ' }',
  9086. ' });',
  9087. '};',
  9088. 'this.a = [];'
  9089. ]),
  9090. LinesToStr([
  9091. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9092. ' a: 1+6,',
  9093. ' p: $mod.a,',
  9094. ' get: function () {',
  9095. ' return this.p[this.a];',
  9096. ' },',
  9097. ' set: function (v) {',
  9098. ' this.p[this.a] = v;',
  9099. ' }',
  9100. '});'
  9101. ]));
  9102. end;
  9103. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9104. begin
  9105. StartProgram(false);
  9106. Add('type Integer = longint;');
  9107. Add('type TArrayInt = array of integer;');
  9108. Add('function GetArr(vB: integer = 0): tarrayint;');
  9109. Add('begin');
  9110. Add('end;');
  9111. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9112. Add('begin');
  9113. Add('end;');
  9114. Add('begin');
  9115. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9116. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9117. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9118. ConvertProgram;
  9119. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9120. LinesToStr([ // statements
  9121. 'this.GetArr = function (vB) {',
  9122. ' var Result = [];',
  9123. ' return Result;',
  9124. '};',
  9125. 'this.DoIt = function (vG,vH,vI) {',
  9126. '};'
  9127. ]),
  9128. LinesToStr([
  9129. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9130. ' a: 1+3,',
  9131. ' p: $mod.GetArr(0),',
  9132. ' get: function () {',
  9133. ' return this.p[this.a];',
  9134. ' },',
  9135. ' set: function (v) {',
  9136. ' this.p[this.a] = v;',
  9137. ' }',
  9138. '});',
  9139. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9140. ' a: 2+3,',
  9141. ' p: $mod.GetArr(0),',
  9142. ' get: function () {',
  9143. ' return this.p[this.a];',
  9144. ' },',
  9145. ' set: function (v) {',
  9146. ' this.p[this.a] = v;',
  9147. ' }',
  9148. '});',
  9149. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9150. ' a: 3+3,',
  9151. ' p: $mod.GetArr(9),',
  9152. ' get: function () {',
  9153. ' return this.p[this.a];',
  9154. ' },',
  9155. ' set: function (v) {',
  9156. ' this.p[this.a] = v;',
  9157. ' }',
  9158. '});',
  9159. '']));
  9160. end;
  9161. procedure TTestModule.TestArrayEnumTypeRange;
  9162. begin
  9163. StartProgram(false);
  9164. Add([
  9165. 'type',
  9166. ' TEnum = (red,blue);',
  9167. ' TEnumArray = array[TEnum] of longint;',
  9168. 'var',
  9169. ' e: TEnum;',
  9170. ' i: longint;',
  9171. ' a: TEnumArray;',
  9172. ' numbers: TEnumArray = (1,2);',
  9173. ' names: array[TEnum] of string = (''red'',''blue'');',
  9174. 'begin',
  9175. ' e:=low(a);',
  9176. ' e:=high(a);',
  9177. ' i:=a[red];',
  9178. ' a[e]:=a[e];']);
  9179. ConvertProgram;
  9180. CheckSource('TestArrayEnumTypeRange',
  9181. LinesToStr([ // statements
  9182. ' this.TEnum = {',
  9183. ' "0": "red",',
  9184. ' red: 0,',
  9185. ' "1": "blue",',
  9186. ' blue: 1',
  9187. '};',
  9188. 'this.e = 0;',
  9189. 'this.i = 0;',
  9190. 'this.a = rtl.arraySetLength(null,0,2);',
  9191. 'this.numbers = [1, 2];',
  9192. 'this.names = ["red", "blue"];',
  9193. '']),
  9194. LinesToStr([ // $mod.$main
  9195. '$mod.e = $mod.TEnum.red;',
  9196. '$mod.e = $mod.TEnum.blue;',
  9197. '$mod.i = $mod.a[$mod.TEnum.red];',
  9198. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9199. '']));
  9200. end;
  9201. procedure TTestModule.TestArray_SetLengthOutArg;
  9202. begin
  9203. StartProgram(false);
  9204. Add([
  9205. 'type TArrInt = array of longint;',
  9206. 'procedure DoIt(out a: TArrInt);',
  9207. 'begin',
  9208. ' SetLength(a,2);',
  9209. 'end;',
  9210. 'begin',
  9211. '']);
  9212. ConvertProgram;
  9213. CheckSource('TestArray_SetLengthOutArg',
  9214. LinesToStr([ // statements
  9215. 'this.DoIt = function (a) {',
  9216. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9217. '};',
  9218. '']),
  9219. LinesToStr([
  9220. '']));
  9221. end;
  9222. procedure TTestModule.TestArray_SetLengthProperty;
  9223. begin
  9224. StartProgram(false);
  9225. Add('type');
  9226. Add(' TArrInt = array of longint;');
  9227. Add(' TObject = class');
  9228. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9229. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9230. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9231. Add(' end;');
  9232. Add('var Obj: TObject;');
  9233. Add('begin');
  9234. Add(' SetLength(Obj.Colors,2);');
  9235. ConvertProgram;
  9236. CheckSource('TestArray_SetLengthProperty',
  9237. LinesToStr([ // statements
  9238. 'rtl.createClass($mod, "TObject", null, function () {',
  9239. ' this.$init = function () {',
  9240. ' };',
  9241. ' this.$final = function () {',
  9242. ' };',
  9243. '});',
  9244. 'this.Obj = null;',
  9245. '']),
  9246. LinesToStr([
  9247. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9248. '']));
  9249. end;
  9250. procedure TTestModule.TestArray_SetLengthMultiDim;
  9251. begin
  9252. StartProgram(false);
  9253. Add([
  9254. 'type',
  9255. ' TArrArrInt = array of array of longint;',
  9256. ' TArrStaInt = array of array[1..2] of longint;',
  9257. 'var',
  9258. ' a: TArrArrInt;',
  9259. ' b: TArrStaInt;',
  9260. 'begin',
  9261. ' SetLength(a,2);',
  9262. ' SetLength(a,3,4);',
  9263. ' SetLength(b,5);',
  9264. '']);
  9265. ConvertProgram;
  9266. CheckSource('TestArray_SetLengthMultiDim',
  9267. LinesToStr([ // statements
  9268. 'this.a = [];',
  9269. 'this.b = [];',
  9270. '']),
  9271. LinesToStr([
  9272. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9273. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9274. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9275. '']));
  9276. end;
  9277. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9278. begin
  9279. StartProgram(false);
  9280. Add([
  9281. 'type',
  9282. ' TStaArr1 = array[1..3] of boolean;',
  9283. //' TStaArr2 = array[5..6] of TStaArr1;',
  9284. ' TDynArr1StaArr1 = array of TStaArr1;',
  9285. //' TDynArr1StaArr2 = array of TStaArr2;',
  9286. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9287. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9288. 'var',
  9289. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9290. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9291. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9292. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9293. 'begin',
  9294. ' SetLength(DynArr1StaArr1,11);',
  9295. ' SetLength(DynArr2StaArr1,12);',
  9296. ' SetLength(DynArr2StaArr1[13],14);',
  9297. ' SetLength(DynArr2StaArr1,15,16);',
  9298. //' SetLength(DynArr1StaArr2,21);',
  9299. //' SetLength(DynArr2StaArr2,22);',
  9300. //' SetLength(DynArr2StaArr2[23],24);',
  9301. //' SetLength(DynArr2StaArr2,25,26);',
  9302. '']);
  9303. ConvertProgram;
  9304. CheckSource('TestArray_DynOfStatic',
  9305. LinesToStr([ // statements
  9306. 'this.DynArr1StaArr1 = [];',
  9307. 'this.DynArr2StaArr1 = [];',
  9308. '']),
  9309. LinesToStr([ // $mod.$main
  9310. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9311. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9312. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9313. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9314. ' $mod.DynArr2StaArr1,',
  9315. ' false,',
  9316. ' 15,',
  9317. ' 16,',
  9318. ' "s",',
  9319. ' 3',
  9320. ');',
  9321. '']));
  9322. end;
  9323. procedure TTestModule.TestArray_OpenArrayOfString;
  9324. begin
  9325. StartProgram(false);
  9326. Add('procedure DoIt(const a: array of String);');
  9327. Add('var');
  9328. Add(' i: longint;');
  9329. Add(' s: string;');
  9330. Add('begin');
  9331. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9332. Add('end;');
  9333. Add('var s: string;');
  9334. Add('begin');
  9335. Add(' DoIt([]);');
  9336. Add(' DoIt([s,''foo'','''',s+s]);');
  9337. ConvertProgram;
  9338. CheckSource('TestArray_OpenArrayOfString',
  9339. LinesToStr([ // statements
  9340. 'this.DoIt = function (a) {',
  9341. ' var i = 0;',
  9342. ' var s = "";',
  9343. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9344. ' i = $l;',
  9345. ' s = a[rtl.length(a) - i - 1];',
  9346. ' };',
  9347. '};',
  9348. 'this.s = "";',
  9349. '']),
  9350. LinesToStr([
  9351. '$mod.DoIt([]);',
  9352. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9353. '']));
  9354. end;
  9355. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9356. begin
  9357. StartProgram(false);
  9358. Add([
  9359. 'type TArr = array of char;',
  9360. 'var',
  9361. ' c: char;',
  9362. ' s: string;',
  9363. ' a: TArr;',
  9364. 'procedure Run(const a: array of char);',
  9365. 'begin',
  9366. ' Run(c);',
  9367. ' Run(s);',
  9368. 'end;',
  9369. 'begin',
  9370. ' a:=c;',
  9371. ' a:=s;',
  9372. ' a:=#13;',
  9373. ' a:=''Foo'';',
  9374. ' Run(c);',
  9375. ' Run(s);',
  9376. '']);
  9377. ConvertProgram;
  9378. CheckSource('TestArray_ArrayOfCharAssignString',
  9379. LinesToStr([ // statements
  9380. 'this.c = "";',
  9381. 'this.s = "";',
  9382. 'this.a = [];',
  9383. 'this.Run = function (a) {',
  9384. ' $mod.Run($mod.c.split(""));',
  9385. ' $mod.Run($mod.s.split(""));',
  9386. '};',
  9387. '']),
  9388. LinesToStr([
  9389. '$mod.a = $mod.c.split("");',
  9390. '$mod.a = $mod.s.split("");',
  9391. '$mod.a = "\r".split("");',
  9392. '$mod.a = "Foo".split("");',
  9393. '$mod.Run($mod.c.split(""));',
  9394. '$mod.Run($mod.s.split(""));',
  9395. '']));
  9396. end;
  9397. procedure TTestModule.TestArray_ConstRef;
  9398. begin
  9399. StartProgram(false);
  9400. Add([
  9401. 'type TArr = array of word;',
  9402. 'procedure Run(constref a: TArr);',
  9403. 'begin',
  9404. 'end;',
  9405. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9406. 'var l: TArr;',
  9407. 'begin',
  9408. ' Run(l);',
  9409. ' Run(a);',
  9410. ' Run(b);',
  9411. ' Run(c);',
  9412. ' Run(d);',
  9413. ' Run(e);',
  9414. 'end;',
  9415. 'begin',
  9416. '']);
  9417. ConvertProgram;
  9418. CheckResolverUnexpectedHints();
  9419. CheckSource('TestArray_ConstRef',
  9420. LinesToStr([ // statements
  9421. 'this.Run = function (a) {',
  9422. '};',
  9423. 'this.Fly = function (a, b, c, d, e) {',
  9424. ' var l = [];',
  9425. ' $mod.Run(l);',
  9426. ' $mod.Run(a);',
  9427. ' $mod.Run(b.get());',
  9428. ' $mod.Run(c.get());',
  9429. ' $mod.Run(d);',
  9430. ' $mod.Run(e);',
  9431. '};',
  9432. '']),
  9433. LinesToStr([
  9434. '']));
  9435. end;
  9436. procedure TTestModule.TestArray_Concat;
  9437. begin
  9438. StartProgram(false);
  9439. Add([
  9440. 'type',
  9441. ' integer = longint;',
  9442. ' TFlag = (big,small);',
  9443. ' TFlags = set of TFlag;',
  9444. ' TRec = record',
  9445. ' i: integer;',
  9446. ' end;',
  9447. ' TArrInt = array of integer;',
  9448. ' TArrRec = array of TRec;',
  9449. ' TArrFlag = array of TFlag;',
  9450. ' TArrSet = array of TFlags;',
  9451. ' TArrJSValue = array of jsvalue;',
  9452. 'var',
  9453. ' ArrInt: tarrint;',
  9454. ' ArrRec: tarrrec;',
  9455. ' ArrFlag: tarrflag;',
  9456. ' ArrSet: tarrset;',
  9457. ' ArrJSValue: tarrjsvalue;',
  9458. 'begin',
  9459. ' arrint:=concat(arrint);',
  9460. ' arrint:=concat(arrint,arrint);',
  9461. ' arrint:=concat(arrint,arrint,arrint);',
  9462. ' arrrec:=concat(arrrec);',
  9463. ' arrrec:=concat(arrrec,arrrec);',
  9464. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9465. ' arrset:=concat(arrset);',
  9466. ' arrset:=concat(arrset,arrset);',
  9467. ' arrset:=concat(arrset,arrset,arrset);',
  9468. ' arrjsvalue:=concat(arrjsvalue);',
  9469. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9470. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9471. ' arrint:=concat([1],arrint);',
  9472. ' arrflag:=concat([big]);',
  9473. ' arrflag:=concat([big],arrflag);',
  9474. ' arrflag:=concat(arrflag,[small]);',
  9475. '']);
  9476. ConvertProgram;
  9477. CheckSource('TestArray_Concat',
  9478. LinesToStr([ // statements
  9479. 'this.TFlag = {',
  9480. ' "0": "big",',
  9481. ' big: 0,',
  9482. ' "1": "small",',
  9483. ' small: 1',
  9484. '};',
  9485. 'rtl.recNewT($mod, "TRec", function () {',
  9486. ' this.i = 0;',
  9487. ' this.$eq = function (b) {',
  9488. ' return this.i === b.i;',
  9489. ' };',
  9490. ' this.$assign = function (s) {',
  9491. ' this.i = s.i;',
  9492. ' return this;',
  9493. ' };',
  9494. '});',
  9495. 'this.ArrInt = [];',
  9496. 'this.ArrRec = [];',
  9497. 'this.ArrFlag = [];',
  9498. 'this.ArrSet = [];',
  9499. 'this.ArrJSValue = [];',
  9500. '']),
  9501. LinesToStr([ // $mod.$main
  9502. '$mod.ArrInt = $mod.ArrInt;',
  9503. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9504. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9505. '$mod.ArrRec = $mod.ArrRec;',
  9506. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9507. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9508. '$mod.ArrSet = $mod.ArrSet;',
  9509. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9510. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9511. '$mod.ArrJSValue = $mod.ArrJSValue;',
  9512. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9513. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9514. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9515. '$mod.ArrFlag = [$mod.TFlag.big];',
  9516. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9517. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9518. '']));
  9519. end;
  9520. procedure TTestModule.TestArray_Copy;
  9521. begin
  9522. StartProgram(false);
  9523. Add([
  9524. 'type',
  9525. ' integer = longint;',
  9526. ' TFlag = (big,small);',
  9527. ' TFlags = set of TFlag;',
  9528. ' TRec = record',
  9529. ' i: integer;',
  9530. ' end;',
  9531. ' TArrInt = array of integer;',
  9532. ' TArrRec = array of TRec;',
  9533. ' TArrSet = array of TFlags;',
  9534. ' TArrJSValue = array of jsvalue;',
  9535. 'var',
  9536. ' ArrInt: tarrint;',
  9537. ' ArrRec: tarrrec;',
  9538. ' ArrSet: tarrset;',
  9539. ' ArrJSValue: tarrjsvalue;',
  9540. 'begin',
  9541. ' arrint:=copy(arrint);',
  9542. ' arrint:=copy(arrint,2);',
  9543. ' arrint:=copy(arrint,3,4);',
  9544. ' arrint:=copy([1,1],1,2);',
  9545. ' arrrec:=copy(arrrec);',
  9546. ' arrrec:=copy(arrrec,5);',
  9547. ' arrrec:=copy(arrrec,6,7);',
  9548. ' arrset:=copy(arrset);',
  9549. ' arrset:=copy(arrset,8);',
  9550. ' arrset:=copy(arrset,9,10);',
  9551. ' arrjsvalue:=copy(arrjsvalue);',
  9552. ' arrjsvalue:=copy(arrjsvalue,11);',
  9553. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9554. ' ']);
  9555. ConvertProgram;
  9556. CheckSource('TestArray_Copy',
  9557. LinesToStr([ // statements
  9558. 'this.TFlag = {',
  9559. ' "0": "big",',
  9560. ' big: 0,',
  9561. ' "1": "small",',
  9562. ' small: 1',
  9563. '};',
  9564. 'rtl.recNewT($mod, "TRec", function () {',
  9565. ' this.i = 0;',
  9566. ' this.$eq = function (b) {',
  9567. ' return this.i === b.i;',
  9568. ' };',
  9569. ' this.$assign = function (s) {',
  9570. ' this.i = s.i;',
  9571. ' return this;',
  9572. ' };',
  9573. '});',
  9574. 'this.ArrInt = [];',
  9575. 'this.ArrRec = [];',
  9576. 'this.ArrSet = [];',
  9577. 'this.ArrJSValue = [];',
  9578. '']),
  9579. LinesToStr([ // $mod.$main
  9580. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9581. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9582. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9583. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9584. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9585. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9586. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9587. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9588. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9589. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9590. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9591. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9592. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9593. '']));
  9594. end;
  9595. procedure TTestModule.TestArray_InsertDelete;
  9596. begin
  9597. StartProgram(false);
  9598. Add([
  9599. 'type',
  9600. ' integer = longint;',
  9601. ' TFlag = (big,small);',
  9602. ' TFlags = set of TFlag;',
  9603. ' TRec = record',
  9604. ' i: integer;',
  9605. ' end;',
  9606. ' TArrInt = array of integer;',
  9607. ' TArrRec = array of TRec;',
  9608. ' TArrSet = array of TFlags;',
  9609. ' TArrJSValue = array of jsvalue;',
  9610. ' TArrArrInt = array of TArrInt;',
  9611. 'var',
  9612. ' ArrInt: tarrint;',
  9613. ' ArrRec: tarrrec;',
  9614. ' ArrSet: tarrset;',
  9615. ' ArrJSValue: tarrjsvalue;',
  9616. ' ArrArrInt: TArrArrInt;',
  9617. 'begin',
  9618. ' Insert(1,arrint,2);',
  9619. ' Insert(arrint[3],arrint,4);',
  9620. ' Insert(arrrec[5],arrrec,6);',
  9621. ' Insert(arrset[7],arrset,7);',
  9622. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9623. ' Insert(10,arrjsvalue,11);',
  9624. ' Insert([23],arrarrint,22);',
  9625. ' Delete(arrint,12,13);',
  9626. ' Delete(arrrec,14,15);',
  9627. ' Delete(arrset,17,18);',
  9628. ' Delete(arrjsvalue,19,10);']);
  9629. ConvertProgram;
  9630. CheckSource('TestArray_InsertDelete',
  9631. LinesToStr([ // statements
  9632. 'this.TFlag = {',
  9633. ' "0": "big",',
  9634. ' big: 0,',
  9635. ' "1": "small",',
  9636. ' small: 1',
  9637. '};',
  9638. 'rtl.recNewT($mod, "TRec", function () {',
  9639. ' this.i = 0;',
  9640. ' this.$eq = function (b) {',
  9641. ' return this.i === b.i;',
  9642. ' };',
  9643. ' this.$assign = function (s) {',
  9644. ' this.i = s.i;',
  9645. ' return this;',
  9646. ' };',
  9647. '});',
  9648. 'this.ArrInt = [];',
  9649. 'this.ArrRec = [];',
  9650. 'this.ArrSet = [];',
  9651. 'this.ArrJSValue = [];',
  9652. 'this.ArrArrInt = [];',
  9653. '']),
  9654. LinesToStr([ // $mod.$main
  9655. '$mod.ArrInt.splice(2, 0, 1);',
  9656. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9657. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9658. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9659. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9660. '$mod.ArrJSValue.splice(11, 0, 10);',
  9661. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9662. '$mod.ArrInt.splice(12, 13);',
  9663. '$mod.ArrRec.splice(14, 15);',
  9664. '$mod.ArrSet.splice(17, 18);',
  9665. '$mod.ArrJSValue.splice(19, 10);',
  9666. '']));
  9667. end;
  9668. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9669. begin
  9670. StartProgram(false);
  9671. Add([
  9672. '{$modeswitch arrayoperators}',
  9673. 'type',
  9674. ' integer = longint;',
  9675. ' TArrInt = array of integer;',
  9676. ' TArrStr = array of string;',
  9677. 'const',
  9678. ' Ints: TArrInt = (1,2,3);',
  9679. ' Aliases: TarrStr = (''foo'',''b'');',
  9680. ' OneInt: TArrInt = (7);',
  9681. ' OneStr: array of integer = (7);',
  9682. ' Chars: array of char = ''aoc'';',
  9683. ' Names: array of string = (''a'',''foo'');',
  9684. ' NameCount = low(Names)+high(Names)+length(Names);',
  9685. 'var i: integer;',
  9686. 'begin',
  9687. ' Ints:=[];',
  9688. ' Ints:=[1,1];',
  9689. ' Ints:=[1]+[2];',
  9690. ' Ints:=[2];',
  9691. ' Ints:=[]+ints;',
  9692. ' Ints:=Ints+[];',
  9693. ' Ints:=Ints+OneInt;',
  9694. ' Ints:=Ints+[1,1];',
  9695. ' Ints:=[i,i]+Ints;',
  9696. ' Ints:=[1]+[i]+[3];',
  9697. '']);
  9698. ConvertProgram;
  9699. CheckSource('TestArray_DynArrayConstObjFPC',
  9700. LinesToStr([ // statements
  9701. 'this.Ints = [1, 2, 3];',
  9702. 'this.Aliases = ["foo", "b"];',
  9703. 'this.OneInt = [7];',
  9704. 'this.OneStr = [7];',
  9705. 'this.Chars = ["a", "o", "c"];',
  9706. 'this.Names = ["a", "foo"];',
  9707. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9708. 'this.i = 0;',
  9709. '']),
  9710. LinesToStr([ // $mod.$main
  9711. '$mod.Ints = [];',
  9712. '$mod.Ints = [1, 1];',
  9713. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9714. '$mod.Ints = [2];',
  9715. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9716. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9717. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9718. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9719. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9720. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9721. '']));
  9722. end;
  9723. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9724. begin
  9725. StartProgram(false);
  9726. // Note: const c = [1,1]; defines a set!
  9727. Add([
  9728. '{$mode delphi}',
  9729. 'type',
  9730. ' integer = longint;',
  9731. ' TArrInt = array of integer;',
  9732. ' TArrStr = array of string;',
  9733. 'const',
  9734. ' Ints: TArrInt = [1,1,2];',
  9735. ' Aliases: TarrStr = [''foo'',''b''];',
  9736. ' OneInt: TArrInt = [7];',
  9737. ' OneStr: array of integer = [7]+[8];',
  9738. ' Chars: array of char = ''aoc'';',
  9739. ' Names: array of string = [''a'',''a''];',
  9740. ' NameCount = low(Names)+high(Names)+length(Names);',
  9741. 'begin',
  9742. '']);
  9743. ConvertProgram;
  9744. CheckSource('TestArray_DynArrayConstDelphi',
  9745. LinesToStr([ // statements
  9746. 'this.Ints = [1, 1, 2];',
  9747. 'this.Aliases = ["foo", "b"];',
  9748. 'this.OneInt = [7];',
  9749. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9750. 'this.Chars = ["a", "o", "c"];',
  9751. 'this.Names = ["a", "a"];',
  9752. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9753. '']),
  9754. LinesToStr([ // $mod.$main
  9755. '']));
  9756. end;
  9757. procedure TTestModule.TestArray_ArrayLitAsParam;
  9758. begin
  9759. StartProgram(false);
  9760. Add([
  9761. '{$modeswitch arrayoperators}',
  9762. 'type',
  9763. ' integer = longint;',
  9764. ' TArrInt = array of integer;',
  9765. ' TArrSet = array of (red,green,blue);',
  9766. 'procedure DoOpenInt(const a: array of integer); forward;',
  9767. 'procedure DoInt(const a: TArrInt);',
  9768. 'begin',
  9769. ' DoInt(a+[1]);',
  9770. ' DoInt([1]+a);',
  9771. ' DoOpenInt(a);',
  9772. ' DoOpenInt(a+[1]);',
  9773. ' DoOpenInt([1]+a);',
  9774. 'end;',
  9775. 'procedure DoOpenInt(const a: array of integer);',
  9776. 'begin',
  9777. ' DoOpenInt(a+[1]);',
  9778. ' DoOpenInt([1]+a);',
  9779. ' DoInt(a);',
  9780. ' DoInt(a+[1]);',
  9781. ' DoInt([1]+a);',
  9782. 'end;',
  9783. 'procedure DoSet(const a: TArrSet);',
  9784. 'begin',
  9785. ' DoSet(a+[red]);',
  9786. ' DoSet([blue]+a);',
  9787. 'end;',
  9788. 'var',
  9789. ' i: TArrInt;',
  9790. ' s: TArrSet;',
  9791. 'begin',
  9792. ' DoInt([1]);',
  9793. ' DoInt([1]+[2]);',
  9794. ' DoInt(i+[1]);',
  9795. ' DoInt([1]+i);',
  9796. ' DoOpenInt([1]);',
  9797. ' DoOpenInt([1]+[2]);',
  9798. ' DoOpenInt(i+[1]);',
  9799. ' DoOpenInt([1]+i);',
  9800. ' DoSet([red]);',
  9801. ' DoSet([blue]+[green]);',
  9802. ' DoSet(s+[blue]);',
  9803. ' DoSet([red]+s);',
  9804. '']);
  9805. ConvertProgram;
  9806. CheckSource('TestArray_ArrayLitAsParam',
  9807. LinesToStr([ // statements
  9808. 'this.TArrSet$a = {',
  9809. ' "0": "red",',
  9810. ' red: 0,',
  9811. ' "1": "green",',
  9812. ' green: 1,',
  9813. ' "2": "blue",',
  9814. ' blue: 2',
  9815. '};',
  9816. 'this.DoInt = function (a) {',
  9817. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9818. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9819. ' $mod.DoOpenInt(a);',
  9820. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9821. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9822. '};',
  9823. 'this.DoOpenInt = function (a) {',
  9824. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9825. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9826. ' $mod.DoInt(a);',
  9827. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9828. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9829. '};',
  9830. 'this.DoSet = function (a) {',
  9831. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9832. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9833. '};',
  9834. 'this.i = [];',
  9835. 'this.s = [];',
  9836. '']),
  9837. LinesToStr([ // $mod.$main
  9838. '$mod.DoInt([1]);',
  9839. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9840. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9841. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9842. '$mod.DoOpenInt([1]);',
  9843. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9844. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9845. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9846. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9847. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9848. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9849. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9850. '']));
  9851. end;
  9852. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9853. begin
  9854. StartProgram(false);
  9855. Add([
  9856. '{$modeswitch arrayoperators}',
  9857. 'type',
  9858. ' integer = longint;',
  9859. ' TArrInt = array of integer;',
  9860. ' TArrArrInt = array of TArrInt;',
  9861. 'procedure DoInt(const a: TArrArrInt);',
  9862. 'begin',
  9863. ' DoInt(a+[[1]]);',
  9864. ' DoInt([[1]]+a);',
  9865. ' DoInt(a);',
  9866. 'end;',
  9867. 'var',
  9868. ' i: TArrInt;',
  9869. ' a: TArrArrInt;',
  9870. 'begin',
  9871. ' a:=[[1]];',
  9872. ' a:=[i];',
  9873. ' a:=a+[i];',
  9874. ' a:=[i]+a;',
  9875. ' a:=[[1]+i];',
  9876. ' a:=[[1]+[2]];',
  9877. ' a:=[i+[2]];',
  9878. ' DoInt([[1]]);',
  9879. ' DoInt([[1]+[2],[3,4],[5]]);',
  9880. ' DoInt([i+[1]]+a);',
  9881. ' DoInt([i]+a);',
  9882. '']);
  9883. ConvertProgram;
  9884. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9885. LinesToStr([ // statements
  9886. 'this.DoInt = function (a) {',
  9887. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9888. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9889. ' $mod.DoInt(a);',
  9890. '};',
  9891. 'this.i = [];',
  9892. 'this.a = [];',
  9893. '']),
  9894. LinesToStr([ // $mod.$main
  9895. '$mod.a = [[1]];',
  9896. '$mod.a = [$mod.i];',
  9897. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9898. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9899. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9900. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9901. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9902. '$mod.DoInt([[1]]);',
  9903. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9904. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9905. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9906. '']));
  9907. end;
  9908. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9909. begin
  9910. StartProgram(false);
  9911. Add([
  9912. '{$modeswitch arrayoperators}',
  9913. 'type',
  9914. ' integer = longint;',
  9915. ' TArrInt = array[1..2] of integer;',
  9916. ' TArrArrInt = array of TArrInt;',
  9917. 'procedure DoInt(const a: TArrArrInt);',
  9918. 'begin',
  9919. ' DoInt(a+[[1,2]]);',
  9920. ' DoInt([[1,2]]+a);',
  9921. ' DoInt(a);',
  9922. 'end;',
  9923. 'var',
  9924. ' i: TArrInt;',
  9925. ' a: TArrArrInt;',
  9926. 'begin',
  9927. ' a:=[[1,1]];',
  9928. ' a:=[i];',
  9929. ' a:=a+[i];',
  9930. ' a:=[i]+a;',
  9931. ' DoInt([[1,1]]);',
  9932. ' DoInt([[1,2],[3,4]]);',
  9933. '']);
  9934. ConvertProgram;
  9935. CheckSource('TestArray_ArrayLitStaticAsParam',
  9936. LinesToStr([ // statements
  9937. 'this.DoInt = function (a) {',
  9938. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9939. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9940. ' $mod.DoInt(a);',
  9941. '};',
  9942. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9943. 'this.a = [];',
  9944. '']),
  9945. LinesToStr([ // $mod.$main
  9946. '$mod.a = [[1, 1]];',
  9947. '$mod.a = [$mod.i.slice(0)];',
  9948. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9949. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9950. '$mod.DoInt([[1, 1]]);',
  9951. '$mod.DoInt([[1, 2], [3, 4]]);',
  9952. '']));
  9953. end;
  9954. procedure TTestModule.TestArray_ForInArrOfString;
  9955. begin
  9956. StartProgram(false);
  9957. Add([
  9958. 'type',
  9959. 'type',
  9960. ' TMonthNameArray = array [1..12] of string;',
  9961. ' TMonthNames = TMonthNameArray;',
  9962. ' TObject = class',
  9963. ' private',
  9964. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9965. ' public',
  9966. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9967. ' end;',
  9968. 'var',
  9969. ' f: TObject;',
  9970. ' Month: string;',
  9971. ' Names: array of string = (''a'',''foo'',''bar'');',
  9972. ' i: longint;',
  9973. 'begin',
  9974. ' for Month in f.LongMonthNames do ;',
  9975. ' for Month in Names do ;',
  9976. ' for i:=low(Names) to high(Names) do ;',
  9977. '']);
  9978. ConvertProgram;
  9979. CheckSource('TestArray_ForInArrOfString',
  9980. LinesToStr([ // statements
  9981. 'rtl.createClass($mod, "TObject", null, function () {',
  9982. ' this.$init = function () {',
  9983. ' };',
  9984. ' this.$final = function () {',
  9985. ' };',
  9986. '});',
  9987. 'this.f = null;',
  9988. 'this.Month = "";',
  9989. 'this.Names = ["a", "foo", "bar"];',
  9990. 'this.i = 0;',
  9991. '']),
  9992. LinesToStr([ // $mod.$main
  9993. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  9994. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  9995. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  9996. '']));
  9997. end;
  9998. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9999. begin
  10000. StartProgram(false);
  10001. Add([
  10002. '{$modeswitch externalclass}',
  10003. 'type',
  10004. ' TJSObject = class external name ''Object''',
  10005. ' end;',
  10006. ' TJSArray = class external name ''Array''',
  10007. ' class function isArray(Value: JSValue) : boolean;',
  10008. ' function concat() : TJSArray; varargs;',
  10009. ' end;',
  10010. 'var',
  10011. ' aObj: TJSArray;',
  10012. ' a: array of longint;',
  10013. ' o: TJSObject;',
  10014. 'begin',
  10015. ' if TJSArray.isArray(65) then ;',
  10016. ' aObj:=TJSArray(a).concat(a);',
  10017. ' o:=TJSObject(a);']);
  10018. ConvertProgram;
  10019. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10020. LinesToStr([ // statements
  10021. 'this.aObj = null;',
  10022. 'this.a = [];',
  10023. 'this.o = null;',
  10024. '']),
  10025. LinesToStr([ // $mod.$main
  10026. 'if (Array.isArray(65)) ;',
  10027. '$mod.aObj = $mod.a.concat($mod.a);',
  10028. '$mod.o = $mod.a;',
  10029. '']));
  10030. end;
  10031. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10032. begin
  10033. StartProgram(false);
  10034. Add([
  10035. '{$modeswitch externalclass}',
  10036. 'type',
  10037. ' TArrStr = array of string;',
  10038. ' TJSArray = class external name ''Array''',
  10039. ' end;',
  10040. ' TJSObject = class external name ''Object''',
  10041. ' end;',
  10042. 'var',
  10043. ' aObj: TJSArray;',
  10044. ' a: TArrStr;',
  10045. ' jo: TJSObject;',
  10046. 'begin',
  10047. ' a:=TArrStr(aObj);',
  10048. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10049. ' a:=TarrStr(jo);',
  10050. '']);
  10051. ConvertProgram;
  10052. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10053. LinesToStr([ // statements
  10054. 'this.aObj = null;',
  10055. 'this.a = [];',
  10056. 'this.jo = null;',
  10057. '']),
  10058. LinesToStr([ // $mod.$main
  10059. '$mod.a = $mod.aObj;',
  10060. '$mod.aObj[1] = $mod.aObj[2];',
  10061. '$mod.a = $mod.jo;',
  10062. '']));
  10063. end;
  10064. procedure TTestModule.TestArrayOfConst_TVarRec;
  10065. begin
  10066. StartProgram(true,[supTVarRec]);
  10067. Add([
  10068. 'procedure Say(args: array of const);',
  10069. 'var',
  10070. ' i: longint;',
  10071. ' v: TVarRec;',
  10072. 'begin',
  10073. ' for i:=low(args) to high(args) do begin',
  10074. ' v:=args[i];',
  10075. ' case v.vtype of',
  10076. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10077. ' end;',
  10078. ' end;',
  10079. ' for v in args do ;',
  10080. ' args:=nil;',
  10081. ' SetLength(args,2);',
  10082. 'end;',
  10083. 'begin']);
  10084. ConvertProgram;
  10085. CheckSource('TestArrayOfConst_TVarRec',
  10086. LinesToStr([ // statements
  10087. 'this.Say = function (args) {',
  10088. ' var i = 0;',
  10089. ' var v = pas.system.TVarRec.$new();',
  10090. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10091. ' i = $l;',
  10092. ' v.$assign(args[i]);',
  10093. ' var $tmp = v.VType;',
  10094. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10095. ' };',
  10096. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10097. ' args = [];',
  10098. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10099. '};',
  10100. '']),
  10101. LinesToStr([ // $mod.$main
  10102. ]));
  10103. end;
  10104. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10105. begin
  10106. StartProgram(true,[supTVarRec]);
  10107. Add([
  10108. 'procedure Say(args: array of const);',
  10109. 'begin',
  10110. ' Say(args);',
  10111. 'end;',
  10112. 'var',
  10113. ' p: Pointer;',
  10114. ' j: jsvalue;',
  10115. ' c: currency;',
  10116. 'begin',
  10117. ' Say([]);',
  10118. ' Say([1]);',
  10119. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10120. '']);
  10121. ConvertProgram;
  10122. CheckSource('TestArrayOfConst_PassBaseTypes',
  10123. LinesToStr([ // statements
  10124. 'this.Say = function (args) {',
  10125. ' $mod.Say(args);',
  10126. '};',
  10127. 'this.p = null;',
  10128. 'this.j = undefined;',
  10129. 'this.c = 0;',
  10130. '']),
  10131. LinesToStr([ // $mod.$main
  10132. '$mod.Say([]);',
  10133. '$mod.Say(pas.system.VarRecs(0, 1));',
  10134. '$mod.Say(pas.system.VarRecs(',
  10135. ' 9,',
  10136. ' "c",',
  10137. ' 18,',
  10138. ' "foo",',
  10139. ' 5,',
  10140. ' null,',
  10141. ' 1,',
  10142. ' true,',
  10143. ' 3,',
  10144. ' 1.3,',
  10145. ' 5,',
  10146. ' $mod.p,',
  10147. ' 20,',
  10148. ' $mod.j,',
  10149. ' 12,',
  10150. ' $mod.c',
  10151. ' ));',
  10152. '']));
  10153. end;
  10154. procedure TTestModule.TestArrayOfConst_PassObj;
  10155. begin
  10156. StartProgram(true,[supTVarRec]);
  10157. Add([
  10158. '{$interfaces corba}',
  10159. 'type',
  10160. ' TObject = class',
  10161. ' end;',
  10162. ' TClass = class of TObject;',
  10163. ' IUnknown = interface',
  10164. ' end;',
  10165. 'procedure Say(args: array of const);',
  10166. 'begin',
  10167. 'end;',
  10168. 'var',
  10169. ' o: TObject;',
  10170. ' c: TClass;',
  10171. ' i: IUnknown;',
  10172. 'begin',
  10173. ' Say([o,c,TObject]);',
  10174. ' Say([nil,i]);',
  10175. '']);
  10176. ConvertProgram;
  10177. CheckSource('TestArrayOfConst_PassObj',
  10178. LinesToStr([ // statements
  10179. 'rtl.createClass($mod, "TObject", null, function () {',
  10180. ' this.$init = function () {',
  10181. ' };',
  10182. ' this.$final = function () {',
  10183. ' };',
  10184. '});',
  10185. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10186. 'this.Say = function (args) {',
  10187. '};',
  10188. 'this.o = null;',
  10189. 'this.c = null;',
  10190. 'this.i = null;',
  10191. '']),
  10192. LinesToStr([ // $mod.$main
  10193. '$mod.Say(pas.system.VarRecs(',
  10194. ' 7,',
  10195. ' $mod.o,',
  10196. ' 8,',
  10197. ' $mod.c,',
  10198. ' 8,',
  10199. ' $mod.TObject',
  10200. '));',
  10201. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10202. '']));
  10203. end;
  10204. procedure TTestModule.TestRecord_Empty;
  10205. begin
  10206. StartProgram(false);
  10207. Add([
  10208. 'type',
  10209. ' TRecA = record',
  10210. ' end;',
  10211. 'var a,b: TRecA;',
  10212. 'begin',
  10213. ' if a=b then ;']);
  10214. ConvertProgram;
  10215. CheckSource('TestRecord_Empty',
  10216. LinesToStr([ // statements
  10217. 'rtl.recNewT($mod, "TRecA", function () {',
  10218. ' this.$eq = function (b) {',
  10219. ' return true;',
  10220. ' };',
  10221. ' this.$assign = function (s) {',
  10222. ' return this;',
  10223. ' };',
  10224. '});',
  10225. 'this.a = $mod.TRecA.$new();',
  10226. 'this.b = $mod.TRecA.$new();',
  10227. '']),
  10228. LinesToStr([ // $mod.$main
  10229. 'if ($mod.a.$eq($mod.b)) ;'
  10230. ]));
  10231. end;
  10232. procedure TTestModule.TestRecord_Var;
  10233. begin
  10234. StartProgram(false);
  10235. Add('type');
  10236. Add(' TRecA = record');
  10237. Add(' Bold: longint;');
  10238. Add(' end;');
  10239. Add('var Rec: TRecA;');
  10240. Add('begin');
  10241. Add(' rec.bold:=123');
  10242. ConvertProgram;
  10243. CheckSource('TestRecord_Var',
  10244. LinesToStr([ // statements
  10245. 'rtl.recNewT($mod, "TRecA", function () {',
  10246. ' this.Bold = 0;',
  10247. ' this.$eq = function (b) {',
  10248. ' return this.Bold === b.Bold;',
  10249. ' };',
  10250. ' this.$assign = function (s) {',
  10251. ' this.Bold = s.Bold;',
  10252. ' return this;',
  10253. ' };',
  10254. '});',
  10255. 'this.Rec = $mod.TRecA.$new();',
  10256. '']),
  10257. LinesToStr([ // $mod.$main
  10258. '$mod.Rec.Bold = 123;'
  10259. ]));
  10260. end;
  10261. procedure TTestModule.TestRecord_VarExternal;
  10262. begin
  10263. StartProgram(false);
  10264. Add([
  10265. '{$modeswitch externalclass}',
  10266. 'type',
  10267. ' TRecA = record',
  10268. ' i: byte;',
  10269. ' length_: longint external name ''length'';',
  10270. ' end;',
  10271. 'var Rec: TRecA;',
  10272. 'begin',
  10273. ' rec.length_ := rec.length_',
  10274. '']);
  10275. ConvertProgram;
  10276. CheckSource('TestRecord_VarExternal',
  10277. LinesToStr([ // statements
  10278. 'rtl.recNewT($mod, "TRecA", function () {',
  10279. ' this.i = 0;',
  10280. ' this.$eq = function (b) {',
  10281. ' return (this.i === b.i) && (this.length === b.length);',
  10282. ' };',
  10283. ' this.$assign = function (s) {',
  10284. ' this.i = s.i;',
  10285. ' this.length = s.length;',
  10286. ' return this;',
  10287. ' };',
  10288. '});',
  10289. 'this.Rec = $mod.TRecA.$new();',
  10290. '']),
  10291. LinesToStr([ // $mod.$main
  10292. '$mod.Rec.length = $mod.Rec.length;'
  10293. ]));
  10294. end;
  10295. procedure TTestModule.TestRecord_WithDo;
  10296. begin
  10297. StartProgram(false);
  10298. Add('type');
  10299. Add(' TRec = record');
  10300. Add(' vI: longint;');
  10301. Add(' end;');
  10302. Add('var');
  10303. Add(' Int: longint;');
  10304. Add(' r: TRec;');
  10305. Add('begin');
  10306. Add(' with r do');
  10307. Add(' int:=vi;');
  10308. Add(' with r do begin');
  10309. Add(' int:=vi;');
  10310. Add(' vi:=int;');
  10311. Add(' end;');
  10312. ConvertProgram;
  10313. CheckSource('TestWithRecordDo',
  10314. LinesToStr([ // statements
  10315. 'rtl.recNewT($mod, "TRec", function () {',
  10316. ' this.vI = 0;',
  10317. ' this.$eq = function (b) {',
  10318. ' return this.vI === b.vI;',
  10319. ' };',
  10320. ' this.$assign = function (s) {',
  10321. ' this.vI = s.vI;',
  10322. ' return this;',
  10323. ' };',
  10324. '});',
  10325. 'this.Int = 0;',
  10326. 'this.r = $mod.TRec.$new();',
  10327. '']),
  10328. LinesToStr([ // $mod.$main
  10329. 'var $with = $mod.r;',
  10330. '$mod.Int = $with.vI;',
  10331. 'var $with1 = $mod.r;',
  10332. '$mod.Int = $with1.vI;',
  10333. '$with1.vI = $mod.Int;'
  10334. ]));
  10335. end;
  10336. procedure TTestModule.TestRecord_Assign;
  10337. begin
  10338. StartProgram(false);
  10339. Add('type');
  10340. Add(' TEnum = (red,green);');
  10341. Add(' TEnums = set of TEnum;');
  10342. Add(' TSmallRec = record');
  10343. Add(' N: longint;');
  10344. Add(' end;');
  10345. Add(' TBigRec = record');
  10346. Add(' Int: longint;');
  10347. Add(' D: double;');
  10348. Add(' Arr: array of longint;');
  10349. Add(' Arr2: array[1..2] of longint;');
  10350. Add(' Small: TSmallRec;');
  10351. Add(' Enums: TEnums;');
  10352. Add(' end;');
  10353. Add('var');
  10354. Add(' r, s: TBigRec;');
  10355. Add('begin');
  10356. Add(' r:=s;');
  10357. Add(' r:=default(TBigRec);');
  10358. Add(' r:=default(s);');
  10359. ConvertProgram;
  10360. CheckSource('TestRecord_Assign',
  10361. LinesToStr([ // statements
  10362. 'this.TEnum = {',
  10363. ' "0": "red",',
  10364. ' red: 0,',
  10365. ' "1": "green",',
  10366. ' green: 1',
  10367. '};',
  10368. 'rtl.recNewT($mod, "TSmallRec", function () {',
  10369. ' this.N = 0;',
  10370. ' this.$eq = function (b) {',
  10371. ' return this.N === b.N;',
  10372. ' };',
  10373. ' this.$assign = function (s) {',
  10374. ' this.N = s.N;',
  10375. ' return this;',
  10376. ' };',
  10377. '});',
  10378. 'rtl.recNewT($mod, "TBigRec", function () {',
  10379. ' this.Int = 0;',
  10380. ' this.D = 0.0;',
  10381. ' this.$new = function () {',
  10382. ' var r = Object.create(this);',
  10383. ' r.Arr = [];',
  10384. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10385. ' r.Small = $mod.TSmallRec.$new();',
  10386. ' r.Enums = {};',
  10387. ' return r;',
  10388. ' };',
  10389. ' this.$eq = function (b) {',
  10390. ' 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);',
  10391. ' };',
  10392. ' this.$assign = function (s) {',
  10393. ' this.Int = s.Int;',
  10394. ' this.D = s.D;',
  10395. ' this.Arr = rtl.arrayRef(s.Arr);',
  10396. ' this.Arr2 = s.Arr2.slice(0);',
  10397. ' this.Small.$assign(s.Small);',
  10398. ' this.Enums = rtl.refSet(s.Enums);',
  10399. ' return this;',
  10400. ' };',
  10401. '});',
  10402. 'this.r = $mod.TBigRec.$new();',
  10403. 'this.s = $mod.TBigRec.$new();',
  10404. '']),
  10405. LinesToStr([ // $mod.$main
  10406. '$mod.r.$assign($mod.s);',
  10407. '$mod.r.$assign($mod.TBigRec.$new());',
  10408. '$mod.r.$assign($mod.TBigRec.$new());',
  10409. '']));
  10410. end;
  10411. procedure TTestModule.TestRecord_AsParams;
  10412. begin
  10413. StartProgram(false);
  10414. Add([
  10415. 'type',
  10416. ' integer = longint;',
  10417. ' TRecord = record',
  10418. ' i: integer;',
  10419. ' end;',
  10420. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10421. 'var vL: TRecord;',
  10422. 'begin',
  10423. ' vd:=vd;',
  10424. ' vd.i:=vd.i;',
  10425. ' vl:=vc;',
  10426. ' vv:=vv;',
  10427. ' vv.i:=vv.i;',
  10428. ' U:=vl;',
  10429. ' U:=vd;',
  10430. ' U:=vc;',
  10431. ' U:=vv;',
  10432. ' vl:=TRecord(U);',
  10433. ' vd:=TRecord(U);',
  10434. ' vv:=TRecord(U);',
  10435. ' doit(vd,vd,vd,vd);',
  10436. ' doit(vc,vc,vl,vl);',
  10437. ' doit(vv,vv,vv,vv);',
  10438. ' doit(vl,vl,vl,vl);',
  10439. ' TRecord(U).i:=3;',
  10440. 'end;',
  10441. 'var i: TRecord;',
  10442. 'begin',
  10443. ' doit(i,i,i,i);',
  10444. '']);
  10445. ConvertProgram;
  10446. CheckSource('TestRecord_AsParams',
  10447. LinesToStr([ // statements
  10448. 'rtl.recNewT($mod, "TRecord", function () {',
  10449. ' this.i = 0;',
  10450. ' this.$eq = function (b) {',
  10451. ' return this.i === b.i;',
  10452. ' };',
  10453. ' this.$assign = function (s) {',
  10454. ' this.i = s.i;',
  10455. ' return this;',
  10456. ' };',
  10457. '});',
  10458. 'this.DoIt = function (vD, vC, vV, U) {',
  10459. ' var vL = $mod.TRecord.$new();',
  10460. ' vD.$assign(vD);',
  10461. ' vD.i = vD.i;',
  10462. ' vL.$assign(vC);',
  10463. ' vV.$assign(vV);',
  10464. ' vV.i = vV.i;',
  10465. ' U.$assign(vL);',
  10466. ' U.$assign(vD);',
  10467. ' U.$assign(vC);',
  10468. ' U.$assign(vV);',
  10469. ' vL.$assign(U);',
  10470. ' vD.$assign(U);',
  10471. ' vV.$assign(U);',
  10472. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10473. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10474. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10475. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10476. ' U.i = 3;',
  10477. '};',
  10478. 'this.i = $mod.TRecord.$new();'
  10479. ]),
  10480. LinesToStr([
  10481. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10482. '']));
  10483. end;
  10484. procedure TTestModule.TestRecord_ConstRef;
  10485. begin
  10486. StartProgram(false);
  10487. Add([
  10488. 'type TRec = record i: word; end;',
  10489. 'procedure Run(constref a: TRec);',
  10490. 'begin',
  10491. 'end;',
  10492. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10493. 'var l: TRec;',
  10494. 'begin',
  10495. ' Run(l);',
  10496. ' Run(a);',
  10497. ' Run(b);',
  10498. ' Run(c);',
  10499. ' Run(d);',
  10500. ' Run(e);',
  10501. 'end;',
  10502. 'begin',
  10503. '']);
  10504. ConvertProgram;
  10505. CheckResolverUnexpectedHints();
  10506. CheckSource('TestRecord_ConstRef',
  10507. LinesToStr([ // statements
  10508. 'rtl.recNewT($mod, "TRec", function () {',
  10509. ' this.i = 0;',
  10510. ' this.$eq = function (b) {',
  10511. ' return this.i === b.i;',
  10512. ' };',
  10513. ' this.$assign = function (s) {',
  10514. ' this.i = s.i;',
  10515. ' return this;',
  10516. ' };',
  10517. '});',
  10518. 'this.Run = function (a) {',
  10519. '};',
  10520. 'this.Fly = function (a, b, c, d, e) {',
  10521. ' var l = $mod.TRec.$new();',
  10522. ' $mod.Run(l);',
  10523. ' $mod.Run(a);',
  10524. ' $mod.Run(b);',
  10525. ' $mod.Run(c);',
  10526. ' $mod.Run(d);',
  10527. ' $mod.Run(e);',
  10528. '};',
  10529. '']),
  10530. LinesToStr([
  10531. '']));
  10532. end;
  10533. procedure TTestModule.TestRecordElement_AsParams;
  10534. begin
  10535. StartProgram(false);
  10536. Add('type');
  10537. Add(' integer = longint;');
  10538. Add(' TRecord = record');
  10539. Add(' i: integer;');
  10540. Add(' end;');
  10541. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10542. Add('var vJ: TRecord;');
  10543. Add('begin');
  10544. Add(' doit(vj.i,vj.i,vj.i);');
  10545. Add('end;');
  10546. Add('var r: TRecord;');
  10547. Add('begin');
  10548. Add(' doit(r.i,r.i,r.i);');
  10549. ConvertProgram;
  10550. CheckSource('TestRecordElement_AsParams',
  10551. LinesToStr([ // statements
  10552. 'rtl.recNewT($mod, "TRecord", function () {',
  10553. ' this.i = 0;',
  10554. ' this.$eq = function (b) {',
  10555. ' return this.i === b.i;',
  10556. ' };',
  10557. ' this.$assign = function (s) {',
  10558. ' this.i = s.i;',
  10559. ' return this;',
  10560. ' };',
  10561. '});',
  10562. 'this.DoIt = function (vG,vH,vI) {',
  10563. ' var vJ = $mod.TRecord.$new();',
  10564. ' $mod.DoIt(vJ.i, vJ.i, {',
  10565. ' p: vJ,',
  10566. ' get: function () {',
  10567. ' return this.p.i;',
  10568. ' },',
  10569. ' set: function (v) {',
  10570. ' this.p.i = v;',
  10571. ' }',
  10572. ' });',
  10573. '};',
  10574. 'this.r = $mod.TRecord.$new();'
  10575. ]),
  10576. LinesToStr([
  10577. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10578. ' p: $mod.r,',
  10579. ' get: function () {',
  10580. ' return this.p.i;',
  10581. ' },',
  10582. ' set: function (v) {',
  10583. ' this.p.i = v;',
  10584. ' }',
  10585. '});'
  10586. ]));
  10587. end;
  10588. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10589. begin
  10590. StartProgram(false);
  10591. Add('type');
  10592. Add(' integer = longint;');
  10593. Add(' TRecord = record');
  10594. Add(' i: integer;');
  10595. Add(' end;');
  10596. Add('function GetRec(vB: integer = 0): TRecord;');
  10597. Add('begin');
  10598. Add('end;');
  10599. Add('procedure DoIt(vG: integer; const vH: integer);');
  10600. Add('begin');
  10601. Add('end;');
  10602. Add('begin');
  10603. Add(' doit(getrec.i,getrec.i);');
  10604. Add(' doit(getrec().i,getrec().i);');
  10605. Add(' doit(getrec(1).i,getrec(2).i);');
  10606. ConvertProgram;
  10607. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10608. LinesToStr([ // statements
  10609. 'rtl.recNewT($mod, "TRecord", function () {',
  10610. ' this.i = 0;',
  10611. ' this.$eq = function (b) {',
  10612. ' return this.i === b.i;',
  10613. ' };',
  10614. ' this.$assign = function (s) {',
  10615. ' this.i = s.i;',
  10616. ' return this;',
  10617. ' };',
  10618. '});',
  10619. 'this.GetRec = function (vB) {',
  10620. ' var Result = $mod.TRecord.$new();',
  10621. ' return Result;',
  10622. '};',
  10623. 'this.DoIt = function (vG, vH) {',
  10624. '};',
  10625. '']),
  10626. LinesToStr([
  10627. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10628. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10629. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10630. '']));
  10631. end;
  10632. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10633. begin
  10634. StartProgram(false);
  10635. Add('type');
  10636. Add(' integer = longint;');
  10637. Add(' TRecord = record');
  10638. Add(' i: integer;');
  10639. Add(' end;');
  10640. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10641. Add('begin');
  10642. Add('end;');
  10643. Add('var r: trecord;');
  10644. Add('begin');
  10645. Add(' with r do ');
  10646. Add(' doit(i,i,i);');
  10647. ConvertProgram;
  10648. CheckSource('TestRecordElementFromWith_AsParams',
  10649. LinesToStr([ // statements
  10650. 'rtl.recNewT($mod, "TRecord", function () {',
  10651. ' this.i = 0;',
  10652. ' this.$eq = function (b) {',
  10653. ' return this.i === b.i;',
  10654. ' };',
  10655. ' this.$assign = function (s) {',
  10656. ' this.i = s.i;',
  10657. ' return this;',
  10658. ' };',
  10659. '});',
  10660. 'this.DoIt = function (vG,vH,vI) {',
  10661. '};',
  10662. 'this.r = $mod.TRecord.$new();'
  10663. ]),
  10664. LinesToStr([
  10665. 'var $with = $mod.r;',
  10666. '$mod.DoIt($with.i,$with.i,{',
  10667. ' p: $with,',
  10668. ' get: function () {',
  10669. ' return this.p.i;',
  10670. ' },',
  10671. ' set: function (v) {',
  10672. ' this.p.i = v;',
  10673. ' }',
  10674. '});',
  10675. '']));
  10676. end;
  10677. procedure TTestModule.TestRecord_Equal;
  10678. begin
  10679. StartProgram(false);
  10680. Add('type');
  10681. Add(' integer = longint;');
  10682. Add(' TFlag = (red,blue);');
  10683. Add(' TFlags = set of TFlag;');
  10684. Add(' TProc = procedure;');
  10685. Add(' TRecord = record');
  10686. Add(' i: integer;');
  10687. Add(' Event: TProc;');
  10688. Add(' f: TFlags;');
  10689. Add(' end;');
  10690. Add(' TNested = record');
  10691. Add(' r: TRecord;');
  10692. Add(' end;');
  10693. Add('var');
  10694. Add(' b: boolean;');
  10695. Add(' r,s: trecord;');
  10696. Add('begin');
  10697. Add(' b:=r=s;');
  10698. Add(' b:=r<>s;');
  10699. ConvertProgram;
  10700. CheckSource('TestRecord_Equal',
  10701. LinesToStr([ // statements
  10702. 'this.TFlag = {',
  10703. ' "0": "red",',
  10704. ' red: 0,',
  10705. ' "1": "blue",',
  10706. ' blue: 1',
  10707. '};',
  10708. 'rtl.recNewT($mod, "TRecord", function () {',
  10709. ' this.i = 0;',
  10710. ' this.Event = null;',
  10711. ' this.$new = function () {',
  10712. ' var r = Object.create(this);',
  10713. ' r.f = {};',
  10714. ' return r;',
  10715. ' };',
  10716. ' this.$eq = function (b) {',
  10717. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10718. ' };',
  10719. ' this.$assign = function (s) {',
  10720. ' this.i = s.i;',
  10721. ' this.Event = s.Event;',
  10722. ' this.f = rtl.refSet(s.f);',
  10723. ' return this;',
  10724. ' };',
  10725. '});',
  10726. 'rtl.recNewT($mod, "TNested", function () {',
  10727. ' this.$new = function () {',
  10728. ' var r = Object.create(this);',
  10729. ' r.r = $mod.TRecord.$new();',
  10730. ' return r;',
  10731. ' };',
  10732. ' this.$eq = function (b) {',
  10733. ' return this.r.$eq(b.r);',
  10734. ' };',
  10735. ' this.$assign = function (s) {',
  10736. ' this.r.$assign(s.r);',
  10737. ' return this;',
  10738. ' };',
  10739. '});',
  10740. 'this.b = false;',
  10741. 'this.r = $mod.TRecord.$new();',
  10742. 'this.s = $mod.TRecord.$new();',
  10743. '']),
  10744. LinesToStr([
  10745. '$mod.b = $mod.r.$eq($mod.s);',
  10746. '$mod.b = !$mod.r.$eq($mod.s);',
  10747. '']));
  10748. end;
  10749. procedure TTestModule.TestRecord_JSValue;
  10750. begin
  10751. StartProgram(false);
  10752. Add([
  10753. 'type',
  10754. ' TRecord = record',
  10755. ' i: longint;',
  10756. ' end;',
  10757. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  10758. 'begin',
  10759. 'end;',
  10760. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  10761. 'begin',
  10762. ' if jsvalue(d) then ;',
  10763. ' if jsvalue(c) then ;',
  10764. ' if jsvalue(v) then ;',
  10765. 'end;',
  10766. 'var',
  10767. ' Jv: jsvalue;',
  10768. ' Rec: trecord;',
  10769. 'begin',
  10770. ' rec:=trecord(jv);',
  10771. ' jv:=rec;',
  10772. ' Fly(rec,rec);',
  10773. ' Fly(@rec,@rec);',
  10774. ' if jsvalue(Rec) then ;',
  10775. ' Run(trecord(jv),trecord(jv),rec);',
  10776. '']);
  10777. ConvertProgram;
  10778. CheckSource('TestRecord_JSValue',
  10779. LinesToStr([ // statements
  10780. 'rtl.recNewT($mod, "TRecord", function () {',
  10781. ' this.i = 0;',
  10782. ' this.$eq = function (b) {',
  10783. ' return this.i === b.i;',
  10784. ' };',
  10785. ' this.$assign = function (s) {',
  10786. ' this.i = s.i;',
  10787. ' return this;',
  10788. ' };',
  10789. '});',
  10790. 'this.Fly = function (d, c) {',
  10791. '};',
  10792. 'this.Run = function (d, c, v) {',
  10793. ' if (d) ;',
  10794. ' if (c) ;',
  10795. ' if (v) ;',
  10796. '};',
  10797. 'this.Jv = undefined;',
  10798. 'this.Rec = $mod.TRecord.$new();',
  10799. '']),
  10800. LinesToStr([
  10801. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10802. '$mod.Jv = $mod.Rec;',
  10803. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  10804. '$mod.Fly($mod.Rec, $mod.Rec);',
  10805. 'if ($mod.Rec) ;',
  10806. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  10807. '']));
  10808. end;
  10809. procedure TTestModule.TestRecord_VariantFail;
  10810. begin
  10811. StartProgram(false);
  10812. Add([
  10813. 'type',
  10814. ' TRec = record',
  10815. ' case word of',
  10816. ' 0: (b0, b1: Byte);',
  10817. ' 1: (i: word);',
  10818. ' end;',
  10819. 'begin']);
  10820. SetExpectedPasResolverError('variant record is not supported',
  10821. nXIsNotSupported);
  10822. ConvertProgram;
  10823. end;
  10824. procedure TTestModule.TestRecord_FieldArray;
  10825. begin
  10826. StartProgram(false);
  10827. Add([
  10828. 'type',
  10829. ' TArrInt = array[3..4] of longint;',
  10830. ' TArrArrInt = array[3..4] of longint;',
  10831. ' TRec = record',
  10832. ' a: array of longint;',
  10833. ' s: array[1..2] of longint;',
  10834. ' m: array[1..2,3..4] of longint;',
  10835. ' o: TArrArrInt;',
  10836. ' end;',
  10837. 'begin']);
  10838. ConvertProgram;
  10839. CheckSource('TestRecord_FieldArray',
  10840. LinesToStr([ // statements
  10841. 'rtl.recNewT($mod, "TRec", function () {',
  10842. ' this.$new = function () {',
  10843. ' var r = Object.create(this);',
  10844. ' r.a = [];',
  10845. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10846. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10847. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10848. ' return r;',
  10849. ' };',
  10850. ' this.$eq = function (b) {',
  10851. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10852. ' };',
  10853. ' this.$assign = function (s) {',
  10854. ' this.a = rtl.arrayRef(s.a);',
  10855. ' this.s = s.s.slice(0);',
  10856. ' this.m = s.m.slice(0);',
  10857. ' this.o = s.o.slice(0);',
  10858. ' return this;',
  10859. ' };',
  10860. '});',
  10861. '']),
  10862. LinesToStr([ // $mod.$main
  10863. '']));
  10864. end;
  10865. procedure TTestModule.TestRecord_Const;
  10866. begin
  10867. StartProgram(false);
  10868. Add([
  10869. 'type',
  10870. ' TArrInt = array[3..4] of longint;',
  10871. ' TPoint = record x,y: longint; end;',
  10872. ' TRec = record',
  10873. ' i: longint;',
  10874. ' a: array of longint;',
  10875. ' s: array[1..2] of longint;',
  10876. ' m: array[1..2,3..4] of longint;',
  10877. ' p: TPoint;',
  10878. ' end;',
  10879. ' TPoints = array of TPoint;',
  10880. 'const',
  10881. ' r: TRec = (',
  10882. ' i:1;',
  10883. ' a:(2,3);',
  10884. ' s:(4,5);',
  10885. ' m:( (11,12), (13,14) );',
  10886. ' p: (x:21; y:22)',
  10887. ' );',
  10888. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10889. 'begin']);
  10890. ConvertProgram;
  10891. CheckSource('TestRecord_Const',
  10892. LinesToStr([ // statements
  10893. 'rtl.recNewT($mod, "TPoint", function () {',
  10894. ' this.x = 0;',
  10895. ' this.y = 0;',
  10896. ' this.$eq = function (b) {',
  10897. ' return (this.x === b.x) && (this.y === b.y);',
  10898. ' };',
  10899. ' this.$assign = function (s) {',
  10900. ' this.x = s.x;',
  10901. ' this.y = s.y;',
  10902. ' return this;',
  10903. ' };',
  10904. '});',
  10905. 'rtl.recNewT($mod, "TRec", function () {',
  10906. ' this.i = 0;',
  10907. ' this.$new = function () {',
  10908. ' var r = Object.create(this);',
  10909. ' r.a = [];',
  10910. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10911. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10912. ' r.p = $mod.TPoint.$new();',
  10913. ' return r;',
  10914. ' };',
  10915. ' this.$eq = function (b) {',
  10916. ' 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);',
  10917. ' };',
  10918. ' this.$assign = function (s) {',
  10919. ' this.i = s.i;',
  10920. ' this.a = rtl.arrayRef(s.a);',
  10921. ' this.s = s.s.slice(0);',
  10922. ' this.m = s.m.slice(0);',
  10923. ' this.p.$assign(s.p);',
  10924. ' return this;',
  10925. ' };',
  10926. '});',
  10927. 'this.r = $mod.TRec.$clone({',
  10928. ' i: 1,',
  10929. ' a: [2, 3],',
  10930. ' s: [4, 5],',
  10931. ' m: [[11, 12], [13, 14]],',
  10932. ' p: $mod.TPoint.$clone({',
  10933. ' x: 21,',
  10934. ' y: 22',
  10935. ' })',
  10936. '});',
  10937. 'this.p = [$mod.TPoint.$clone({',
  10938. ' x: 1,',
  10939. ' y: 2',
  10940. '}), $mod.TPoint.$clone({',
  10941. ' x: 3,',
  10942. ' y: 4',
  10943. '})];',
  10944. '']),
  10945. LinesToStr([ // $mod.$main
  10946. '']));
  10947. end;
  10948. procedure TTestModule.TestRecord_TypecastFail;
  10949. begin
  10950. StartProgram(false);
  10951. Add([
  10952. 'type',
  10953. ' TPoint = record x,y: longint; end;',
  10954. ' TRec = record l: longint end;',
  10955. 'var p: TPoint;',
  10956. 'begin',
  10957. ' if TRec(p).l=2 then ;']);
  10958. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10959. nIllegalTypeConversionTo);
  10960. ConvertProgram;
  10961. end;
  10962. procedure TTestModule.TestRecord_InFunction;
  10963. begin
  10964. StartProgram(false);
  10965. Add([
  10966. 'var TPoint: longint = 3;',
  10967. 'procedure DoIt;',
  10968. 'type',
  10969. ' TPoint = record x,y: longint; end;',
  10970. ' TPoints = array of TPoint;',
  10971. 'var',
  10972. ' r: TPoint;',
  10973. ' p: TPoints;',
  10974. 'begin',
  10975. ' SetLength(p,2);',
  10976. 'end;',
  10977. 'begin']);
  10978. ConvertProgram;
  10979. CheckSource('TestRecord_InFunction',
  10980. LinesToStr([ // statements
  10981. 'this.TPoint = 3;',
  10982. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10983. ' this.x = 0;',
  10984. ' this.y = 0;',
  10985. ' this.$eq = function (b) {',
  10986. ' return (this.x === b.x) && (this.y === b.y);',
  10987. ' };',
  10988. ' this.$assign = function (s) {',
  10989. ' this.x = s.x;',
  10990. ' this.y = s.y;',
  10991. ' return this;',
  10992. ' };',
  10993. '});',
  10994. 'this.DoIt = function () {',
  10995. ' var r = TPoint$1.$new();',
  10996. ' var p = [];',
  10997. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10998. '};',
  10999. '']),
  11000. LinesToStr([ // $mod.$main
  11001. '']));
  11002. end;
  11003. procedure TTestModule.TestRecord_AnonymousFail;
  11004. begin
  11005. StartProgram(false);
  11006. Add([
  11007. 'var',
  11008. ' r: record x: word end;',
  11009. 'begin']);
  11010. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11011. nNotYetImplemented);
  11012. ConvertProgram;
  11013. end;
  11014. procedure TTestModule.TestAdvRecord_Function;
  11015. begin
  11016. StartProgram(false);
  11017. Parser.Options:=Parser.Options+[po_cassignments];
  11018. Add([
  11019. '{$modeswitch AdvancedRecords}',
  11020. 'type',
  11021. ' TPoint = record',
  11022. ' x,y: word;',
  11023. ' function Add(const apt: TPoint): TPoint;',
  11024. ' end;',
  11025. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11026. 'begin',
  11027. ' Result:=Self;',
  11028. ' Result.x+=apt.x;',
  11029. ' Result.y:=Result.y+apt.y;',
  11030. ' Self:=apt;',
  11031. 'end;',
  11032. 'var p,q: TPoint;',
  11033. 'begin',
  11034. ' p.add(q);',
  11035. ' p:=default(TPoint);',
  11036. ' p:=q;',
  11037. '']);
  11038. ConvertProgram;
  11039. CheckSource('TestAdvRecord_Function',
  11040. LinesToStr([ // statements
  11041. 'rtl.recNewT($mod, "TPoint", function () {',
  11042. ' this.x = 0;',
  11043. ' this.y = 0;',
  11044. ' this.$eq = function (b) {',
  11045. ' return (this.x === b.x) && (this.y === b.y);',
  11046. ' };',
  11047. ' this.$assign = function (s) {',
  11048. ' this.x = s.x;',
  11049. ' this.y = s.y;',
  11050. ' return this;',
  11051. ' };',
  11052. ' this.Add = function (apt) {',
  11053. ' var Result = $mod.TPoint.$new();',
  11054. ' Result.$assign(this);',
  11055. ' Result.x += apt.x;',
  11056. ' Result.y = Result.y + apt.y;',
  11057. ' this.$assign(apt);',
  11058. ' return Result;',
  11059. ' };',
  11060. '});',
  11061. 'this.p = $mod.TPoint.$new();',
  11062. 'this.q = $mod.TPoint.$new();',
  11063. '']),
  11064. LinesToStr([ // $mod.$main
  11065. '$mod.p.Add($mod.q);',
  11066. '$mod.p.$assign($mod.TPoint.$new());',
  11067. '$mod.p.$assign($mod.q);',
  11068. '']));
  11069. end;
  11070. procedure TTestModule.TestAdvRecord_Property;
  11071. begin
  11072. StartProgram(false);
  11073. Add([
  11074. '{$modeswitch AdvancedRecords}',
  11075. 'type',
  11076. ' TPoint = record',
  11077. ' x,y: word;',
  11078. ' strict private',
  11079. ' function GetSize: longword;',
  11080. ' procedure SetSize(Value: longword);',
  11081. ' public',
  11082. ' property Size: longword read GetSize write SetSize;',
  11083. ' property Left: word read x write y;',
  11084. ' end;',
  11085. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11086. 'function TPoint.GetSize: longword;',
  11087. 'begin',
  11088. ' x:=y;',
  11089. ' Size:=Size;',
  11090. ' Left:=Left;',
  11091. 'end;',
  11092. 'procedure TPoint.SetSize(Value: longword);',
  11093. 'begin',
  11094. 'end;',
  11095. 'var p,q: TPoint;',
  11096. 'begin',
  11097. ' p.Size:=q.Size;',
  11098. ' p.Left:=q.Left;',
  11099. '']);
  11100. ConvertProgram;
  11101. CheckSource('TestAdvRecord_Property',
  11102. LinesToStr([ // statements
  11103. 'rtl.recNewT($mod, "TPoint", function () {',
  11104. ' this.x = 0;',
  11105. ' this.y = 0;',
  11106. ' this.$eq = function (b) {',
  11107. ' return (this.x === b.x) && (this.y === b.y);',
  11108. ' };',
  11109. ' this.$assign = function (s) {',
  11110. ' this.x = s.x;',
  11111. ' this.y = s.y;',
  11112. ' return this;',
  11113. ' };',
  11114. ' this.GetSize = function () {',
  11115. ' var Result = 0;',
  11116. ' this.x = this.y;',
  11117. ' this.SetSize(this.GetSize());',
  11118. ' this.y = this.x;',
  11119. ' return Result;',
  11120. ' };',
  11121. ' this.SetSize = function (Value) {',
  11122. ' };',
  11123. '});',
  11124. 'this.SetSize = function (Value) {',
  11125. '};',
  11126. 'this.p = $mod.TPoint.$new();',
  11127. 'this.q = $mod.TPoint.$new();',
  11128. '']),
  11129. LinesToStr([ // $mod.$main
  11130. '$mod.p.SetSize($mod.q.GetSize());',
  11131. '$mod.p.y = $mod.q.x;',
  11132. '']));
  11133. end;
  11134. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11135. begin
  11136. StartProgram(false);
  11137. Add([
  11138. '{$modeswitch AdvancedRecords}',
  11139. 'type',
  11140. ' TPoint = record',
  11141. ' strict private',
  11142. ' function GetItems(Index: word): word;',
  11143. ' procedure SetItems(Index: word; Value: word);',
  11144. ' public',
  11145. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11146. ' end;',
  11147. 'function TPoint.GetItems(Index: word): word;',
  11148. 'begin',
  11149. ' Items[index]:=Items[index];',
  11150. ' self.Items[index]:=self.Items[index];',
  11151. 'end;',
  11152. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11153. 'begin',
  11154. 'end;',
  11155. 'var p: TPoint;',
  11156. 'begin',
  11157. ' p[1]:=p[2];',
  11158. ' p.Items[3]:=p.Items[4];',
  11159. '']);
  11160. ConvertProgram;
  11161. CheckSource('TestAdvRecord_PropertyDefault',
  11162. LinesToStr([ // statements
  11163. 'rtl.recNewT($mod, "TPoint", function () {',
  11164. ' this.$eq = function (b) {',
  11165. ' return true;',
  11166. ' };',
  11167. ' this.$assign = function (s) {',
  11168. ' return this;',
  11169. ' };',
  11170. ' this.GetItems = function (Index) {',
  11171. ' var Result = 0;',
  11172. ' this.SetItems(Index, this.GetItems(Index));',
  11173. ' this.SetItems(Index, this.GetItems(Index));',
  11174. ' return Result;',
  11175. ' };',
  11176. ' this.SetItems = function (Index, Value) {',
  11177. ' };',
  11178. '});',
  11179. 'this.p = $mod.TPoint.$new();',
  11180. '']),
  11181. LinesToStr([ // $mod.$main
  11182. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11183. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11184. '']));
  11185. end;
  11186. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11187. begin
  11188. StartProgram(false);
  11189. Add([
  11190. '{$modeswitch AdvancedRecords}',
  11191. 'type',
  11192. ' TRec = record',
  11193. ' class var',
  11194. ' Fx: longint;',
  11195. ' Fy: longint;',
  11196. ' class function GetInt: longint; static;',
  11197. ' class procedure SetInt(Value: longint); static;',
  11198. ' class procedure DoIt; static;',
  11199. ' class property IntA: longint read Fx write Fy;',
  11200. ' class property IntB: longint read GetInt write SetInt;',
  11201. ' end;',
  11202. 'class function trec.getint: longint;',
  11203. 'begin',
  11204. ' result:=fx;',
  11205. 'end;',
  11206. 'class procedure trec.setint(value: longint);',
  11207. 'begin',
  11208. 'end;',
  11209. 'class procedure trec.doit;',
  11210. 'begin',
  11211. ' IntA:=IntA+1;',
  11212. ' IntB:=IntB+1;',
  11213. 'end;',
  11214. 'var r: trec;',
  11215. 'begin',
  11216. ' trec.inta:=trec.inta+1;',
  11217. ' if trec.intb=2 then;',
  11218. ' trec.intb:=trec.intb+2;',
  11219. ' trec.setint(trec.inta);',
  11220. ' r.inta:=r.inta+1;',
  11221. ' if r.intb=2 then;',
  11222. ' r.intb:=r.intb+2;',
  11223. ' r.setint(r.inta);']);
  11224. ConvertProgram;
  11225. CheckSource('TestAdvRecord_Property_ClassMethod',
  11226. LinesToStr([ // statements
  11227. 'rtl.recNewT($mod, "TRec", function () {',
  11228. ' this.Fx = 0;',
  11229. ' this.Fy = 0;',
  11230. ' this.$eq = function (b) {',
  11231. ' return true;',
  11232. ' };',
  11233. ' this.$assign = function (s) {',
  11234. ' return this;',
  11235. ' };',
  11236. ' this.GetInt = function () {',
  11237. ' var Result = 0;',
  11238. ' Result = $mod.TRec.Fx;',
  11239. ' return Result;',
  11240. ' };',
  11241. ' this.SetInt = function (Value) {',
  11242. ' };',
  11243. ' this.DoIt = function () {',
  11244. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11245. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11246. ' };',
  11247. '}, true);',
  11248. 'this.r = $mod.TRec.$new();',
  11249. '']),
  11250. LinesToStr([ // $mod.$main
  11251. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11252. 'if ($mod.TRec.GetInt() === 2) ;',
  11253. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11254. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11255. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11256. 'if ($mod.r.GetInt() === 2) ;',
  11257. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  11258. '$mod.r.SetInt($mod.r.Fx);',
  11259. '']));
  11260. end;
  11261. procedure TTestModule.TestAdvRecord_Const;
  11262. begin
  11263. StartProgram(false);
  11264. Add([
  11265. '{$modeswitch AdvancedRecords}',
  11266. 'type',
  11267. ' TArrInt = array[3..4] of longint;',
  11268. ' TPoint = record',
  11269. ' x,y: longint;',
  11270. ' class var Count: nativeint;',
  11271. ' end;',
  11272. ' TRec = record',
  11273. ' i: longint;',
  11274. ' a: array of longint;',
  11275. ' s: array[1..2] of longint;',
  11276. ' m: array[1..2,3..4] of longint;',
  11277. ' p: TPoint;',
  11278. ' end;',
  11279. ' TPoints = array of TPoint;',
  11280. 'const',
  11281. ' r: TRec = (',
  11282. ' i:1;',
  11283. ' a:(2,3);',
  11284. ' s:(4,5);',
  11285. ' m:( (11,12), (13,14) );',
  11286. ' p: (x:21)',
  11287. ' );',
  11288. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11289. 'begin']);
  11290. ConvertProgram;
  11291. CheckSource('TestAdvRecord_Const',
  11292. LinesToStr([ // statements
  11293. 'rtl.recNewT($mod, "TPoint", function () {',
  11294. ' this.x = 0;',
  11295. ' this.y = 0;',
  11296. ' this.Count = 0;',
  11297. ' this.$eq = function (b) {',
  11298. ' return (this.x === b.x) && (this.y === b.y);',
  11299. ' };',
  11300. ' this.$assign = function (s) {',
  11301. ' this.x = s.x;',
  11302. ' this.y = s.y;',
  11303. ' return this;',
  11304. ' };',
  11305. '}, true);',
  11306. 'rtl.recNewT($mod, "TRec", function () {',
  11307. ' this.i = 0;',
  11308. ' this.$new = function () {',
  11309. ' var r = Object.create(this);',
  11310. ' r.a = [];',
  11311. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11312. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11313. ' r.p = $mod.TPoint.$new();',
  11314. ' return r;',
  11315. ' };',
  11316. ' this.$eq = function (b) {',
  11317. ' 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);',
  11318. ' };',
  11319. ' this.$assign = function (s) {',
  11320. ' this.i = s.i;',
  11321. ' this.a = rtl.arrayRef(s.a);',
  11322. ' this.s = s.s.slice(0);',
  11323. ' this.m = s.m.slice(0);',
  11324. ' this.p.$assign(s.p);',
  11325. ' return this;',
  11326. ' };',
  11327. '});',
  11328. 'this.r = $mod.TRec.$clone({',
  11329. ' i: 1,',
  11330. ' a: [2, 3],',
  11331. ' s: [4, 5],',
  11332. ' m: [[11, 12], [13, 14]],',
  11333. ' p: $mod.TPoint.$clone({',
  11334. ' x: 21,',
  11335. ' y: 0',
  11336. ' })',
  11337. '});',
  11338. 'this.p = [$mod.TPoint.$clone({',
  11339. ' x: 1,',
  11340. ' y: 2',
  11341. '}), $mod.TPoint.$clone({',
  11342. ' x: 3,',
  11343. ' y: 4',
  11344. '})];',
  11345. '']),
  11346. LinesToStr([ // $mod.$main
  11347. '']));
  11348. end;
  11349. procedure TTestModule.TestAdvRecord_ExternalField;
  11350. begin
  11351. StartProgram(false);
  11352. Add([
  11353. '{$modeswitch AdvancedRecords}',
  11354. '{$modeswitch externalclass}',
  11355. 'type',
  11356. ' TCar = record',
  11357. ' public',
  11358. ' Intern: longint external name ''$Intern'';',
  11359. ' Intern2: longint external name ''$Intern2'';',
  11360. ' Bracket: longint external name ''["A B"]'';',
  11361. ' procedure DoIt;',
  11362. ' end;',
  11363. 'procedure tcar.doit;',
  11364. 'begin',
  11365. ' Intern:=Intern+1;',
  11366. ' Intern2:=Intern2+2;',
  11367. ' Bracket:=Bracket+3;',
  11368. 'end;',
  11369. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11370. 'begin',
  11371. ' Rec.intern:=Rec.intern+1;',
  11372. ' Rec.intern2:=Rec.intern2+2;',
  11373. ' Rec.Bracket:=Rec.Bracket+3;',
  11374. ' with Rec do begin',
  11375. ' intern:=intern+1;',
  11376. ' intern2:=intern2+2;',
  11377. ' Bracket:=Bracket+3;',
  11378. ' end;']);
  11379. ConvertProgram;
  11380. CheckSource('TestAdvRecord_ExternalField',
  11381. LinesToStr([ // statements
  11382. 'rtl.recNewT($mod, "TCar", function () {',
  11383. ' this.$eq = function (b) {',
  11384. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11385. ' };',
  11386. ' this.$assign = function (s) {',
  11387. ' this.$Intern = s.$Intern;',
  11388. ' this.$Intern2 = s.$Intern2;',
  11389. ' this["A B"] = s["A B"];',
  11390. ' return this;',
  11391. ' };',
  11392. ' this.DoIt = function () {',
  11393. ' this.$Intern = this.$Intern + 1;',
  11394. ' this.$Intern2 = this.$Intern2 + 2;',
  11395. ' this["A B"] = this["A B"] + 3;',
  11396. ' };',
  11397. '});',
  11398. 'this.Rec = $mod.TCar.$clone({',
  11399. ' $Intern: 11,',
  11400. ' $Intern2: 12,',
  11401. ' "A B": 13',
  11402. '});',
  11403. '']),
  11404. LinesToStr([ // $mod.$main
  11405. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11406. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11407. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11408. 'var $with = $mod.Rec;',
  11409. '$with.$Intern = $with.$Intern + 1;',
  11410. '$with.$Intern2 = $with.$Intern2 + 2;',
  11411. '$with["A B"] = $with["A B"] + 3;',
  11412. '']));
  11413. end;
  11414. procedure TTestModule.TestAdvRecord_SubRecord;
  11415. begin
  11416. StartProgram(false);
  11417. Add([
  11418. '{$modeswitch AdvancedRecords}',
  11419. 'type',
  11420. ' TRec = record',
  11421. ' type',
  11422. ' TPoint = record',
  11423. ' x,y: longint;',
  11424. ' class var Count: nativeint;',
  11425. ' procedure DoIt;',
  11426. ' class procedure DoThat; static;',
  11427. ' end;',
  11428. ' var',
  11429. ' i: longint;',
  11430. ' p: TPoint;',
  11431. ' procedure DoSome;',
  11432. ' end;',
  11433. 'const',
  11434. ' r: TRec = (',
  11435. ' i:1;',
  11436. ' p: (x:21;y:22)',
  11437. ' );',
  11438. 'procedure TRec.DoSome;',
  11439. 'begin',
  11440. ' p.x:=p.y+1;',
  11441. ' p.Count:=p.Count+2;',
  11442. 'end;',
  11443. 'procedure TRec.TPoint.DoIt;',
  11444. 'begin',
  11445. ' Count:=Count+3;',
  11446. 'end;',
  11447. 'class procedure TRec.TPoint.DoThat;',
  11448. 'begin',
  11449. ' Count:=Count+4;',
  11450. 'end;',
  11451. 'begin']);
  11452. ConvertProgram;
  11453. CheckSource('TestAdvRecord_SubRecord',
  11454. LinesToStr([ // statements
  11455. 'rtl.recNewT($mod, "TRec", function () {',
  11456. ' rtl.recNewT(this, "TPoint", function () {',
  11457. ' this.x = 0;',
  11458. ' this.y = 0;',
  11459. ' this.Count = 0;',
  11460. ' this.$eq = function (b) {',
  11461. ' return (this.x === b.x) && (this.y === b.y);',
  11462. ' };',
  11463. ' this.$assign = function (s) {',
  11464. ' this.x = s.x;',
  11465. ' this.y = s.y;',
  11466. ' return this;',
  11467. ' };',
  11468. ' this.DoIt = function () {',
  11469. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11470. ' };',
  11471. ' this.DoThat = function () {',
  11472. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11473. ' };',
  11474. ' }, true);',
  11475. ' this.i = 0;',
  11476. ' this.$new = function () {',
  11477. ' var r = Object.create(this);',
  11478. ' r.p = this.TPoint.$new();',
  11479. ' return r;',
  11480. ' };',
  11481. ' this.$eq = function (b) {',
  11482. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11483. ' };',
  11484. ' this.$assign = function (s) {',
  11485. ' this.i = s.i;',
  11486. ' this.p.$assign(s.p);',
  11487. ' return this;',
  11488. ' };',
  11489. ' this.DoSome = function () {',
  11490. ' this.p.x = this.p.y + 1;',
  11491. ' this.TPoint.Count = this.p.Count + 2;',
  11492. ' };',
  11493. '}, true);',
  11494. 'this.r = $mod.TRec.$clone({',
  11495. ' i: 1,',
  11496. ' p: $mod.TRec.TPoint.$clone({',
  11497. ' x: 21,',
  11498. ' y: 22',
  11499. ' })',
  11500. '});',
  11501. '']),
  11502. LinesToStr([ // $mod.$main
  11503. '']));
  11504. end;
  11505. procedure TTestModule.TestAdvRecord_SubClass;
  11506. begin
  11507. StartProgram(false);
  11508. Add([
  11509. '{$modeswitch AdvancedRecords}',
  11510. 'type',
  11511. ' TObject = class end;',
  11512. ' TPoint = record',
  11513. ' type',
  11514. ' TBird = class',
  11515. ' procedure DoIt;',
  11516. ' class procedure Glob;',
  11517. ' end;',
  11518. ' procedure DoIt(b: TBird);',
  11519. ' end;',
  11520. 'procedure TPoint.TBird.DoIt;',
  11521. 'begin',
  11522. ' doit;',
  11523. ' self.doit;',
  11524. ' glob;',
  11525. ' self.glob;',
  11526. 'end;',
  11527. 'class procedure TPoint.TBird.Glob;',
  11528. 'begin',
  11529. ' glob;',
  11530. ' self.glob;',
  11531. 'end;',
  11532. 'procedure TPoint.DoIt(b: TBird);',
  11533. 'begin',
  11534. ' b.doit;',
  11535. ' b.glob;',
  11536. ' TBird.glob;',
  11537. 'end;',
  11538. 'begin',
  11539. '']);
  11540. ConvertProgram;
  11541. CheckSource('TestAdvRecord_SubClass',
  11542. LinesToStr([ // statements
  11543. 'rtl.createClass($mod, "TObject", null, function () {',
  11544. ' this.$init = function () {',
  11545. ' };',
  11546. ' this.$final = function () {',
  11547. ' };',
  11548. '});',
  11549. 'rtl.recNewT($mod, "TPoint", function () {',
  11550. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11551. ' this.DoIt = function () {',
  11552. ' this.DoIt();',
  11553. ' this.DoIt();',
  11554. ' this.$class.Glob();',
  11555. ' this.$class.Glob();',
  11556. ' };',
  11557. ' this.Glob = function () {',
  11558. ' this.Glob();',
  11559. ' this.Glob();',
  11560. ' };',
  11561. ' });',
  11562. ' this.$eq = function (b) {',
  11563. ' return true;',
  11564. ' };',
  11565. ' this.$assign = function (s) {',
  11566. ' return this;',
  11567. ' };',
  11568. ' this.DoIt = function (b) {',
  11569. ' b.DoIt();',
  11570. ' b.$class.Glob();',
  11571. ' this.TBird.Glob();',
  11572. ' };',
  11573. '}, true);',
  11574. '']),
  11575. LinesToStr([ // $mod.$main
  11576. '']));
  11577. end;
  11578. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11579. begin
  11580. StartProgram(false);
  11581. Add([
  11582. '{$modeswitch AdvancedRecords}',
  11583. 'type',
  11584. ' IUnknown = interface end;',
  11585. ' TPoint = record',
  11586. ' type IBird = interface end;',
  11587. ' end;',
  11588. 'begin',
  11589. '']);
  11590. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11591. nNotYetImplemented);
  11592. ParseProgram;
  11593. end;
  11594. procedure TTestModule.TestAdvRecord_Constructor;
  11595. begin
  11596. StartProgram(false);
  11597. Add([
  11598. '{$modeswitch AdvancedRecords}',
  11599. 'type',
  11600. ' TPoint = record',
  11601. ' x,y: longint;',
  11602. ' constructor Create(ax: longint; ay: longint = -1);',
  11603. ' end;',
  11604. 'constructor tpoint.create(ax,ay: longint);',
  11605. 'begin',
  11606. ' x:=ax;',
  11607. ' self.y:=ay;',
  11608. 'end;',
  11609. 'var r: TPoint;',
  11610. 'begin',
  11611. ' r:=TPoint.Create(1,2);',
  11612. ' with TPoint do r:=Create(1,2);',
  11613. ' r.Create(3);',
  11614. ' r:=r.Create(4);',
  11615. '']);
  11616. ConvertProgram;
  11617. CheckSource('TestAdvRecord_Constructor',
  11618. LinesToStr([ // statements
  11619. 'rtl.recNewT($mod, "TPoint", function () {',
  11620. ' this.x = 0;',
  11621. ' this.y = 0;',
  11622. ' this.$eq = function (b) {',
  11623. ' return (this.x === b.x) && (this.y === b.y);',
  11624. ' };',
  11625. ' this.$assign = function (s) {',
  11626. ' this.x = s.x;',
  11627. ' this.y = s.y;',
  11628. ' return this;',
  11629. ' };',
  11630. ' this.Create = function (ax, ay) {',
  11631. ' this.x = ax;',
  11632. ' this.y = ay;',
  11633. ' return this;',
  11634. ' };',
  11635. '}, true);',
  11636. 'this.r = $mod.TPoint.$new();',
  11637. '']),
  11638. LinesToStr([ // $mod.$main
  11639. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11640. 'var $with = $mod.TPoint;',
  11641. '$mod.r.$assign($with.$new().Create(1, 2));',
  11642. '$mod.r.Create(3, -1);',
  11643. '$mod.r.$assign($mod.r.Create(4, -1));',
  11644. '']));
  11645. end;
  11646. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11647. begin
  11648. StartProgram(false);
  11649. Add([
  11650. '{$modeswitch AdvancedRecords}',
  11651. 'type',
  11652. ' TPoint = record',
  11653. ' class var x: longint;',
  11654. ' class procedure Fly; static;',
  11655. ' class constructor Init;',
  11656. ' end;',
  11657. 'var count: word;',
  11658. 'class procedure Tpoint.Fly;',
  11659. 'begin',
  11660. 'end;',
  11661. 'class constructor tpoint.init;',
  11662. 'begin',
  11663. ' count:=count+1;',
  11664. ' x:=x+3;',
  11665. ' tpoint.x:=tpoint.x+4;',
  11666. ' fly;',
  11667. ' tpoint.fly;',
  11668. 'end;',
  11669. 'var r: TPoint;',
  11670. 'begin',
  11671. ' r.x:=r.x+10;',
  11672. ' r.Fly;',
  11673. ' r.Fly();',
  11674. '']);
  11675. ConvertProgram;
  11676. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11677. LinesToStr([ // statements
  11678. 'rtl.recNewT($mod, "TPoint", function () {',
  11679. ' this.x = 0;',
  11680. ' this.$eq = function (b) {',
  11681. ' return true;',
  11682. ' };',
  11683. ' this.$assign = function (s) {',
  11684. ' return this;',
  11685. ' };',
  11686. ' this.Fly = function () {',
  11687. ' };',
  11688. '}, true);',
  11689. 'this.count = 0;',
  11690. 'this.r = $mod.TPoint.$new();',
  11691. '']),
  11692. LinesToStr([ // $mod.$main
  11693. '(function () {',
  11694. ' $mod.count = $mod.count + 1;',
  11695. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11696. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11697. ' $mod.TPoint.Fly();',
  11698. ' $mod.TPoint.Fly();',
  11699. '})();',
  11700. '$mod.TPoint.x = $mod.r.x + 10;',
  11701. '$mod.r.Fly();',
  11702. '$mod.r.Fly();',
  11703. '']));
  11704. end;
  11705. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  11706. begin
  11707. StartUnit(false);
  11708. Add([
  11709. 'interface',
  11710. '{$modeswitch AdvancedRecords}',
  11711. 'type',
  11712. ' TPoint = record',
  11713. ' class var x: longint;',
  11714. ' class procedure Fly; static;',
  11715. ' class constructor Init;',
  11716. ' end;',
  11717. 'implementation',
  11718. 'var count: word;',
  11719. 'class procedure Tpoint.Fly;',
  11720. 'begin',
  11721. 'end;',
  11722. 'class constructor tpoint.init;',
  11723. 'begin',
  11724. ' count:=count+1;',
  11725. ' x:=3;',
  11726. ' tpoint.x:=4;',
  11727. ' fly;',
  11728. ' tpoint.fly;',
  11729. 'end;',
  11730. '']);
  11731. ConvertUnit;
  11732. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  11733. LinesToStr([ // statements
  11734. 'var $impl = $mod.$impl;',
  11735. 'rtl.recNewT($mod, "TPoint", function () {',
  11736. ' this.x = 0;',
  11737. ' this.$eq = function (b) {',
  11738. ' return true;',
  11739. ' };',
  11740. ' this.$assign = function (s) {',
  11741. ' return this;',
  11742. ' };',
  11743. ' this.Fly = function () {',
  11744. ' };',
  11745. '}, true);',
  11746. '']),
  11747. LinesToStr([ // $mod.$init
  11748. '(function () {',
  11749. ' $impl.count = $impl.count + 1;',
  11750. ' $mod.TPoint.x = 3;',
  11751. ' $mod.TPoint.x = 4;',
  11752. ' $mod.TPoint.Fly();',
  11753. ' $mod.TPoint.Fly();',
  11754. '})();',
  11755. '']),
  11756. LinesToStr([ // $mod.$main
  11757. '$impl.count = 0;',
  11758. '']));
  11759. end;
  11760. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11761. begin
  11762. StartProgram(false);
  11763. Add(['type',
  11764. ' TObject = class',
  11765. ' public',
  11766. ' constructor Create;',
  11767. ' destructor Destroy;',
  11768. ' end;',
  11769. ' TBird = TObject;',
  11770. 'constructor tobject.create;',
  11771. 'begin end;',
  11772. 'destructor tobject.destroy;',
  11773. 'begin end;',
  11774. 'var Obj: tobject;',
  11775. 'begin',
  11776. ' obj:=tobject.create;',
  11777. ' obj:=tobject.create();',
  11778. ' obj:=tbird.create;',
  11779. ' obj:=tbird.create();',
  11780. ' obj:=obj.create();',
  11781. ' obj.destroy;',
  11782. '']);
  11783. ConvertProgram;
  11784. CheckSource('TestClass_TObjectDefaultConstructor',
  11785. LinesToStr([ // statements
  11786. 'rtl.createClass($mod,"TObject",null,function(){',
  11787. ' this.$init = function () {',
  11788. ' };',
  11789. ' this.$final = function () {',
  11790. ' };',
  11791. ' this.Create = function(){',
  11792. ' return this;',
  11793. ' };',
  11794. ' this.Destroy = function(){',
  11795. ' };',
  11796. '});',
  11797. 'this.Obj = null;'
  11798. ]),
  11799. LinesToStr([ // $mod.$main
  11800. '$mod.Obj = $mod.TObject.$create("Create");',
  11801. '$mod.Obj = $mod.TObject.$create("Create");',
  11802. '$mod.Obj = $mod.TObject.$create("Create");',
  11803. '$mod.Obj = $mod.TObject.$create("Create");',
  11804. '$mod.Obj = $mod.Obj.Create();',
  11805. '$mod.Obj.$destroy("Destroy");',
  11806. '']));
  11807. end;
  11808. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11809. begin
  11810. StartProgram(false);
  11811. Add('type');
  11812. Add(' TObject = class');
  11813. Add(' public');
  11814. Add(' constructor Create(Par: longint);');
  11815. Add(' end;');
  11816. Add('constructor tobject.create(par: longint);');
  11817. Add('begin end;');
  11818. Add('var Obj: tobject;');
  11819. Add('begin');
  11820. Add(' obj:=tobject.create(3);');
  11821. ConvertProgram;
  11822. CheckSource('TestClass_TObjectConstructorWithParams',
  11823. LinesToStr([ // statements
  11824. 'rtl.createClass($mod,"TObject",null,function(){',
  11825. ' this.$init = function () {',
  11826. ' };',
  11827. ' this.$final = function () {',
  11828. ' };',
  11829. ' this.Create = function(Par){',
  11830. ' return this;',
  11831. ' };',
  11832. '});',
  11833. 'this.Obj = null;'
  11834. ]),
  11835. LinesToStr([ // $mod.$main
  11836. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11837. ]));
  11838. end;
  11839. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11840. begin
  11841. StartProgram(false);
  11842. Add('type');
  11843. Add(' TObject = class');
  11844. Add(' public');
  11845. Add(' constructor Create;');
  11846. Add(' end;');
  11847. Add(' TTest = class(TObject)');
  11848. Add(' public');
  11849. Add(' constructor Create(const Par: longint = 1);');
  11850. Add(' end;');
  11851. Add('constructor tobject.create;');
  11852. Add('begin end;');
  11853. Add('constructor ttest.create(const par: longint);');
  11854. Add('begin end;');
  11855. Add('var t: ttest;');
  11856. Add('begin');
  11857. Add(' t:=ttest.create;');
  11858. Add(' t:=ttest.create(2);');
  11859. ConvertProgram;
  11860. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11861. LinesToStr([ // statements
  11862. 'rtl.createClass($mod,"TObject",null,function(){',
  11863. ' this.$init = function () {',
  11864. ' };',
  11865. ' this.$final = function () {',
  11866. ' };',
  11867. ' this.Create = function(){',
  11868. ' return this;',
  11869. ' };',
  11870. '});',
  11871. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11872. ' this.Create$1 = function (Par) {',
  11873. ' return this;',
  11874. ' };',
  11875. '});',
  11876. 'this.t = null;'
  11877. ]),
  11878. LinesToStr([ // $mod.$main
  11879. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11880. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11881. ]));
  11882. end;
  11883. procedure TTestModule.TestClass_Var;
  11884. begin
  11885. StartProgram(false);
  11886. Add([
  11887. 'type',
  11888. ' TObject = class',
  11889. ' public',
  11890. ' vI: longint;',
  11891. ' constructor Create(Par: longint);',
  11892. ' end;',
  11893. 'constructor tobject.create(par: longint);',
  11894. 'begin',
  11895. ' vi:=par+3',
  11896. 'end;',
  11897. 'var Obj: tobject;',
  11898. 'begin',
  11899. ' obj:=tobject.create(4);',
  11900. ' obj.vi:=obj.VI+5;']);
  11901. ConvertProgram;
  11902. CheckSource('TestClass_Var',
  11903. LinesToStr([ // statements
  11904. 'rtl.createClass($mod,"TObject",null,function(){',
  11905. ' this.$init = function () {',
  11906. ' this.vI = 0;',
  11907. ' };',
  11908. ' this.$final = function () {',
  11909. ' };',
  11910. ' this.Create = function(Par){',
  11911. ' this.vI = Par+3;',
  11912. ' return this;',
  11913. ' };',
  11914. '});',
  11915. 'this.Obj = null;'
  11916. ]),
  11917. LinesToStr([ // $mod.$main
  11918. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11919. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11920. ]));
  11921. end;
  11922. procedure TTestModule.TestClass_Method;
  11923. begin
  11924. StartProgram(false);
  11925. Add('type');
  11926. Add(' TObject = class');
  11927. Add(' public');
  11928. Add(' vI: longint;');
  11929. Add(' Sub: TObject;');
  11930. Add(' constructor Create;');
  11931. Add(' function GetIt(Par: longint): tobject;');
  11932. Add(' end;');
  11933. Add('constructor tobject.create; begin end;');
  11934. Add('function tobject.getit(par: longint): tobject;');
  11935. Add('begin');
  11936. Add(' Self.vi:=par+3;');
  11937. Add(' Result:=self.sub;');
  11938. Add('end;');
  11939. Add('var Obj: tobject;');
  11940. Add('begin');
  11941. Add(' obj:=tobject.create;');
  11942. Add(' obj.getit(4);');
  11943. Add(' obj.sub.sub:=nil;');
  11944. Add(' obj.sub.getit(5);');
  11945. Add(' obj.sub.getit(6).SUB:=nil;');
  11946. Add(' obj.sub.getit(7).GETIT(8);');
  11947. Add(' obj.sub.getit(9).SuB.getit(10);');
  11948. ConvertProgram;
  11949. CheckSource('TestClass_Method',
  11950. LinesToStr([ // statements
  11951. 'rtl.createClass($mod,"TObject",null,function(){',
  11952. ' this.$init = function () {',
  11953. ' this.vI = 0;',
  11954. ' this.Sub = null;',
  11955. ' };',
  11956. ' this.$final = function () {',
  11957. ' this.Sub = undefined;',
  11958. ' };',
  11959. ' this.Create = function(){',
  11960. ' return this;',
  11961. ' };',
  11962. ' this.GetIt = function(Par){',
  11963. ' var Result = null;',
  11964. ' this.vI = Par + 3;',
  11965. ' Result = this.Sub;',
  11966. ' return Result;',
  11967. ' };',
  11968. '});',
  11969. 'this.Obj = null;'
  11970. ]),
  11971. LinesToStr([ // $mod.$main
  11972. '$mod.Obj = $mod.TObject.$create("Create");',
  11973. '$mod.Obj.GetIt(4);',
  11974. '$mod.Obj.Sub.Sub=null;',
  11975. '$mod.Obj.Sub.GetIt(5);',
  11976. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11977. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11978. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11979. ]));
  11980. end;
  11981. procedure TTestModule.TestClass_Implementation;
  11982. begin
  11983. StartUnit(false);
  11984. Add([
  11985. 'interface',
  11986. 'type',
  11987. ' TObject = class',
  11988. ' constructor Create;',
  11989. ' end;',
  11990. 'implementation',
  11991. 'type',
  11992. ' TIntClass = class',
  11993. ' constructor Create; reintroduce;',
  11994. ' class procedure DoGlob;',
  11995. ' end;',
  11996. 'constructor tintclass.create;',
  11997. 'begin',
  11998. ' inherited;',
  11999. ' inherited create;',
  12000. ' doglob;',
  12001. 'end;',
  12002. 'class procedure tintclass.doglob;',
  12003. 'begin',
  12004. 'end;',
  12005. 'constructor tobject.create;',
  12006. 'var',
  12007. ' iC: tintclass;',
  12008. 'begin',
  12009. ' ic:=tintclass.create;',
  12010. ' tintclass.doglob;',
  12011. ' ic.doglob;',
  12012. 'end;',
  12013. 'initialization',
  12014. ' tintclass.doglob;',
  12015. '']);
  12016. ConvertUnit;
  12017. CheckSource('TestClass_Implementation',
  12018. LinesToStr([ // statements
  12019. 'var $impl = $mod.$impl;',
  12020. 'rtl.createClass($mod, "TObject", null, function () {',
  12021. ' this.$init = function () {',
  12022. ' };',
  12023. ' this.$final = function () {',
  12024. ' };',
  12025. ' this.Create = function () {',
  12026. ' var iC = null;',
  12027. ' iC = $impl.TIntClass.$create("Create$1");',
  12028. ' $impl.TIntClass.DoGlob();',
  12029. ' iC.$class.DoGlob();',
  12030. ' return this;',
  12031. ' };',
  12032. '});',
  12033. '']),
  12034. LinesToStr([ // $mod.$main
  12035. '$impl.TIntClass.DoGlob();',
  12036. '']),
  12037. LinesToStr([
  12038. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12039. ' this.Create$1 = function () {',
  12040. ' $mod.TObject.Create.call(this);',
  12041. ' $mod.TObject.Create.call(this);',
  12042. ' this.$class.DoGlob();',
  12043. ' return this;',
  12044. ' };',
  12045. ' this.DoGlob = function () {',
  12046. ' };',
  12047. '});',
  12048. '']));
  12049. end;
  12050. procedure TTestModule.TestClass_Inheritance;
  12051. begin
  12052. StartProgram(false);
  12053. Add('type');
  12054. Add(' TObject = class');
  12055. Add(' public');
  12056. Add(' constructor Create;');
  12057. Add(' end;');
  12058. Add(' TClassA = class');
  12059. Add(' end;');
  12060. Add(' TClassB = class(TObject)');
  12061. Add(' procedure ProcB;');
  12062. Add(' end;');
  12063. Add('constructor tobject.create; begin end;');
  12064. Add('procedure tclassb.procb; begin end;');
  12065. Add('var');
  12066. Add(' oO: TObject;');
  12067. Add(' oA: TClassA;');
  12068. Add(' oB: TClassB;');
  12069. Add('begin');
  12070. Add(' oO:=tobject.Create;');
  12071. Add(' oA:=tclassa.Create;');
  12072. Add(' ob:=tclassb.Create;');
  12073. Add(' if oo is tclassa then ;');
  12074. Add(' ob:=oo as tclassb;');
  12075. Add(' (oo as tclassb).procb;');
  12076. ConvertProgram;
  12077. CheckSource('TestClass_Inheritance',
  12078. LinesToStr([ // statements
  12079. 'rtl.createClass($mod,"TObject",null,function(){',
  12080. ' this.$init = function () {',
  12081. ' };',
  12082. ' this.$final = function () {',
  12083. ' };',
  12084. ' this.Create = function () {',
  12085. ' return this;',
  12086. ' };',
  12087. '});',
  12088. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  12089. '});',
  12090. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  12091. ' this.ProcB = function () {',
  12092. ' };',
  12093. '});',
  12094. 'this.oO = null;',
  12095. 'this.oA = null;',
  12096. 'this.oB = null;'
  12097. ]),
  12098. LinesToStr([ // $mod.$main
  12099. '$mod.oO = $mod.TObject.$create("Create");',
  12100. '$mod.oA = $mod.TClassA.$create("Create");',
  12101. '$mod.oB = $mod.TClassB.$create("Create");',
  12102. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12103. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12104. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12105. ]));
  12106. end;
  12107. procedure TTestModule.TestClass_TypeAlias;
  12108. begin
  12109. StartProgram(false);
  12110. Add([
  12111. '{$interfaces corba}',
  12112. 'type',
  12113. ' IObject = interface',
  12114. ' end;',
  12115. ' IBird = type IObject;',
  12116. ' TObject = class',
  12117. ' end;',
  12118. ' TBird = type TObject;',
  12119. 'var',
  12120. ' oObj: TObject;',
  12121. ' oBird: TBird;',
  12122. ' IntfObj: IObject;',
  12123. ' IntfBird: IBird;',
  12124. 'begin',
  12125. ' oObj:=oBird;',
  12126. '']);
  12127. ConvertProgram;
  12128. CheckSource('TestClass_TypeAlias',
  12129. LinesToStr([ // statements
  12130. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12131. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  12132. 'rtl.createClass($mod, "TObject", null, function () {',
  12133. ' this.$init = function () {',
  12134. ' };',
  12135. ' this.$final = function () {',
  12136. ' };',
  12137. '});',
  12138. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12139. '});',
  12140. 'this.oObj = null;',
  12141. 'this.oBird = null;',
  12142. 'this.IntfObj = null;',
  12143. 'this.IntfBird = null;',
  12144. '']),
  12145. LinesToStr([ // $mod.$main
  12146. '$mod.oObj = $mod.oBird;',
  12147. '']));
  12148. end;
  12149. procedure TTestModule.TestClass_AbstractMethod;
  12150. begin
  12151. StartProgram(false);
  12152. Add('type');
  12153. Add(' TObject = class');
  12154. Add(' public');
  12155. Add(' procedure DoIt; virtual; abstract;');
  12156. Add(' end;');
  12157. Add('begin');
  12158. ConvertProgram;
  12159. CheckSource('TestClass_AbstractMethod',
  12160. LinesToStr([ // statements
  12161. 'rtl.createClass($mod,"TObject",null,function(){',
  12162. ' this.$init = function () {',
  12163. ' };',
  12164. ' this.$final = function () {',
  12165. ' };',
  12166. '});'
  12167. ]),
  12168. LinesToStr([ // this.$main
  12169. ''
  12170. ]));
  12171. end;
  12172. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12173. begin
  12174. StartProgram(false);
  12175. Add([
  12176. 'type',
  12177. ' TObject = class',
  12178. ' procedure DoAbstract; virtual; abstract;',
  12179. ' procedure DoVirtual; virtual;',
  12180. ' procedure DoIt;',
  12181. ' end;',
  12182. ' TA = class',
  12183. ' procedure doabstract; override;',
  12184. ' procedure dovirtual; override;',
  12185. ' procedure DoSome;',
  12186. ' end;',
  12187. 'procedure tobject.dovirtual;',
  12188. 'begin',
  12189. ' inherited; // call non existing ancestor -> ignore silently',
  12190. 'end;',
  12191. 'procedure tobject.doit;',
  12192. 'begin',
  12193. 'end;',
  12194. 'procedure ta.doabstract;',
  12195. 'begin',
  12196. ' inherited dovirtual; // call TObject.DoVirtual',
  12197. 'end;',
  12198. 'procedure ta.dovirtual;',
  12199. 'begin',
  12200. ' inherited; // call TObject.DoVirtual',
  12201. ' inherited dovirtual; // call TObject.DoVirtual',
  12202. ' inherited dovirtual(); // call TObject.DoVirtual',
  12203. ' doit;',
  12204. ' doit();',
  12205. 'end;',
  12206. 'procedure ta.dosome;',
  12207. 'begin',
  12208. ' inherited; // call non existing ancestor method -> silently ignore',
  12209. 'end;',
  12210. 'begin']);
  12211. ConvertProgram;
  12212. CheckSource('TestClass_CallInherited_ProcNoParams',
  12213. LinesToStr([ // statements
  12214. 'rtl.createClass($mod,"TObject",null,function(){',
  12215. ' this.$init = function () {',
  12216. ' };',
  12217. ' this.$final = function () {',
  12218. ' };',
  12219. ' this.DoVirtual = function () {',
  12220. ' };',
  12221. ' this.DoIt = function () {',
  12222. ' };',
  12223. '});',
  12224. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  12225. ' this.DoAbstract = function () {',
  12226. ' $mod.TObject.DoVirtual.call(this);',
  12227. ' };',
  12228. ' this.DoVirtual = function () {',
  12229. ' $mod.TObject.DoVirtual.call(this);',
  12230. ' $mod.TObject.DoVirtual.call(this);',
  12231. ' $mod.TObject.DoVirtual.call(this);',
  12232. ' this.DoIt();',
  12233. ' this.DoIt();',
  12234. ' };',
  12235. ' this.DoSome = function () {',
  12236. ' };',
  12237. '});'
  12238. ]),
  12239. LinesToStr([ // this.$main
  12240. ''
  12241. ]));
  12242. end;
  12243. procedure TTestModule.TestClass_CallInherited_WithParams;
  12244. begin
  12245. StartProgram(false);
  12246. Add([
  12247. 'type',
  12248. ' TObject = class',
  12249. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12250. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12251. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12252. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12253. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12254. ' end;',
  12255. ' TClassA = class',
  12256. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12257. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12258. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12259. ' end;',
  12260. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12261. 'begin',
  12262. 'end;',
  12263. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12264. 'begin',
  12265. 'end;',
  12266. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12267. 'begin',
  12268. 'end;',
  12269. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12270. 'begin',
  12271. 'end;',
  12272. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12273. 'begin',
  12274. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12275. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12276. 'end;',
  12277. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12278. 'begin',
  12279. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12280. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12281. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12282. ' doit(pa,pb);',
  12283. ' doit(pa);',
  12284. ' doit2(pa);',
  12285. ' doit2;',
  12286. 'end;',
  12287. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12288. 'begin',
  12289. ' pa:=inherited;',
  12290. 'end;',
  12291. 'begin']);
  12292. ConvertProgram;
  12293. CheckSource('TestClass_CallInherited_WithParams',
  12294. LinesToStr([ // statements
  12295. 'rtl.createClass($mod,"TObject",null,function(){',
  12296. ' this.$init = function () {',
  12297. ' };',
  12298. ' this.$final = function () {',
  12299. ' };',
  12300. ' this.DoVirtual = function (pA,pB) {',
  12301. ' };',
  12302. ' this.DoIt = function (pA,pB) {',
  12303. ' };',
  12304. ' this.DoIt2 = function (pA,pB) {',
  12305. ' };',
  12306. ' this.GetIt = function (pA, pB) {',
  12307. ' var Result = 0;',
  12308. ' return Result;',
  12309. ' };',
  12310. '});',
  12311. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  12312. ' this.DoAbstract = function (pA,pB) {',
  12313. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12314. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12315. ' };',
  12316. ' this.DoVirtual = function (pA,pB) {',
  12317. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12318. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12319. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12320. ' this.DoIt(pA,pB);',
  12321. ' this.DoIt(pA,0);',
  12322. ' this.DoIt2(pA,2);',
  12323. ' this.DoIt2(1,2);',
  12324. ' };',
  12325. ' this.GetIt$1 = function (pA, pB) {',
  12326. ' var Result = 0;',
  12327. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12328. ' return Result;',
  12329. ' };',
  12330. '});'
  12331. ]),
  12332. LinesToStr([ // this.$main
  12333. ''
  12334. ]));
  12335. end;
  12336. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12337. begin
  12338. StartProgram(false);
  12339. Add('type');
  12340. Add(' TObject = class');
  12341. Add(' constructor Create; virtual;');
  12342. Add(' constructor CreateWithB(b: boolean);');
  12343. Add(' end;');
  12344. Add(' TA = class');
  12345. Add(' constructor Create; override;');
  12346. Add(' constructor CreateWithC(c: char);');
  12347. Add(' procedure DoIt;');
  12348. Add(' class function DoSome: TObject;');
  12349. Add(' end;');
  12350. Add('constructor tobject.create;');
  12351. Add('begin');
  12352. Add(' inherited; // call non existing ancestor -> ignore silently');
  12353. Add('end;');
  12354. Add('constructor tobject.createwithb(b: boolean);');
  12355. Add('begin');
  12356. Add(' inherited; // call non existing ancestor -> ignore silently');
  12357. Add(' create; // normal call');
  12358. Add('end;');
  12359. Add('constructor ta.create;');
  12360. Add('begin');
  12361. Add(' inherited; // normal call TObject.Create');
  12362. Add(' inherited create; // normal call TObject.Create');
  12363. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12364. Add('end;');
  12365. Add('constructor ta.createwithc(c: char);');
  12366. Add('begin');
  12367. Add(' inherited create; // call TObject.Create');
  12368. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12369. Add(' doit;');
  12370. Add(' doit();');
  12371. Add(' dosome;');
  12372. Add('end;');
  12373. Add('procedure ta.doit;');
  12374. Add('begin');
  12375. Add(' create; // normal call');
  12376. Add(' createwithb(false); // normal call');
  12377. Add(' createwithc(''c''); // normal call');
  12378. Add('end;');
  12379. Add('class function ta.dosome: TObject;');
  12380. Add('begin');
  12381. Add(' Result:=create; // constructor');
  12382. Add(' Result:=createwithb(true); // constructor');
  12383. Add(' Result:=createwithc(''c''); // constructor');
  12384. Add('end;');
  12385. Add('begin');
  12386. ConvertProgram;
  12387. CheckSource('TestClass_CallInheritedConstructor',
  12388. LinesToStr([ // statements
  12389. 'rtl.createClass($mod,"TObject",null,function(){',
  12390. ' this.$init = function () {',
  12391. ' };',
  12392. ' this.$final = function () {',
  12393. ' };',
  12394. ' this.Create = function () {',
  12395. ' return this;',
  12396. ' };',
  12397. ' this.CreateWithB = function (b) {',
  12398. ' this.Create();',
  12399. ' return this;',
  12400. ' };',
  12401. '});',
  12402. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  12403. ' this.Create = function () {',
  12404. ' $mod.TObject.Create.call(this);',
  12405. ' $mod.TObject.Create.call(this);',
  12406. ' $mod.TObject.CreateWithB.call(this, false);',
  12407. ' return this;',
  12408. ' };',
  12409. ' this.CreateWithC = function (c) {',
  12410. ' $mod.TObject.Create.call(this);',
  12411. ' $mod.TObject.CreateWithB.call(this, true);',
  12412. ' this.DoIt();',
  12413. ' this.DoIt();',
  12414. ' this.$class.DoSome();',
  12415. ' return this;',
  12416. ' };',
  12417. ' this.DoIt = function () {',
  12418. ' this.Create();',
  12419. ' this.CreateWithB(false);',
  12420. ' this.CreateWithC("c");',
  12421. ' };',
  12422. ' this.DoSome = function () {',
  12423. ' var Result = null;',
  12424. ' Result = this.$create("Create");',
  12425. ' Result = this.$create("CreateWithB", [true]);',
  12426. ' Result = this.$create("CreateWithC", ["c"]);',
  12427. ' return Result;',
  12428. ' };',
  12429. '});'
  12430. ]),
  12431. LinesToStr([ // this.$main
  12432. ''
  12433. ]));
  12434. end;
  12435. procedure TTestModule.TestClass_ClassVar_Assign;
  12436. begin
  12437. StartProgram(false);
  12438. Add([
  12439. 'type',
  12440. ' TObject = class',
  12441. ' public',
  12442. ' class var vI: longint;',
  12443. ' class var Sub: TObject;',
  12444. ' constructor Create;',
  12445. ' class function GetIt(var Par: longint): tobject;',
  12446. ' end;',
  12447. 'constructor tobject.create;',
  12448. 'begin',
  12449. ' vi:=vi+1;',
  12450. ' Self.vi:=Self.vi+1;',
  12451. ' inc(vi);',
  12452. 'end;',
  12453. 'class function tobject.getit(var par: longint): tobject;',
  12454. 'begin',
  12455. ' vi:=vi+3;',
  12456. ' Self.vi:=Self.vi+4;',
  12457. ' inc(vi);',
  12458. ' Result:=self.sub;',
  12459. ' GetIt(vi);',
  12460. 'end;',
  12461. 'var Obj: tobject;',
  12462. 'begin',
  12463. ' obj:=tobject.create;',
  12464. ' tobject.vi:=3;',
  12465. ' if tobject.vi=4 then ;',
  12466. ' tobject.sub:=nil;',
  12467. ' obj.sub:=nil;',
  12468. ' obj.sub.sub:=nil;']);
  12469. ConvertProgram;
  12470. CheckSource('TestClass_ClassVar_Assign',
  12471. LinesToStr([ // statements
  12472. 'rtl.createClass($mod,"TObject",null,function(){',
  12473. ' this.vI = 0;',
  12474. ' this.Sub = null;',
  12475. ' this.$init = function () {',
  12476. ' };',
  12477. ' this.$final = function () {',
  12478. ' };',
  12479. ' this.Create = function(){',
  12480. ' $mod.TObject.vI = this.vI+1;',
  12481. ' $mod.TObject.vI = this.vI+1;',
  12482. ' $mod.TObject.vI += 1;',
  12483. ' return this;',
  12484. ' };',
  12485. ' this.GetIt = function(Par){',
  12486. ' var Result = null;',
  12487. ' $mod.TObject.vI = this.vI + 3;',
  12488. ' $mod.TObject.vI = this.vI + 4;',
  12489. ' $mod.TObject.vI += 1;',
  12490. ' Result = this.Sub;',
  12491. ' this.GetIt({',
  12492. ' p: $mod.TObject,',
  12493. ' get: function () {',
  12494. ' return this.p.vI;',
  12495. ' },',
  12496. ' set: function (v) {',
  12497. ' this.p.vI = v;',
  12498. ' }',
  12499. ' });',
  12500. ' return Result;',
  12501. ' };',
  12502. '});',
  12503. 'this.Obj = null;'
  12504. ]),
  12505. LinesToStr([ // $mod.$main
  12506. '$mod.Obj = $mod.TObject.$create("Create");',
  12507. '$mod.TObject.vI = 3;',
  12508. 'if ($mod.TObject.vI === 4);',
  12509. '$mod.TObject.Sub=null;',
  12510. '$mod.TObject.Sub=null;',
  12511. '$mod.TObject.Sub=null;',
  12512. '']));
  12513. end;
  12514. procedure TTestModule.TestClass_CallClassMethod;
  12515. begin
  12516. StartProgram(false);
  12517. Add('type');
  12518. Add(' TObject = class');
  12519. Add(' public');
  12520. Add(' class var vI: longint;');
  12521. Add(' class var Sub: TObject;');
  12522. Add(' constructor Create;');
  12523. Add(' function GetMore(Par: longint): longint;');
  12524. Add(' class function GetIt(Par: longint): tobject;');
  12525. Add(' end;');
  12526. Add('constructor tobject.create;');
  12527. Add('begin');
  12528. Add(' sub:=getit(3);');
  12529. Add(' vi:=getmore(4);');
  12530. Add(' sub:=Self.getit(5);');
  12531. Add(' vi:=Self.getmore(6);');
  12532. Add('end;');
  12533. Add('function tobject.getmore(par: longint): longint;');
  12534. Add('begin');
  12535. Add(' sub:=getit(11);');
  12536. Add(' vi:=getmore(12);');
  12537. Add(' sub:=self.getit(13);');
  12538. Add(' vi:=self.getmore(14);');
  12539. Add('end;');
  12540. Add('class function tobject.getit(par: longint): tobject;');
  12541. Add('begin');
  12542. Add(' sub:=getit(21);');
  12543. Add(' vi:=sub.getmore(22);');
  12544. Add(' sub:=self.getit(23);');
  12545. Add(' vi:=self.sub.getmore(24);');
  12546. Add('end;');
  12547. Add('var Obj: tobject;');
  12548. Add('begin');
  12549. Add(' obj:=tobject.create;');
  12550. Add(' tobject.getit(5);');
  12551. Add(' obj.getit(6);');
  12552. Add(' obj.sub.getit(7);');
  12553. Add(' obj.sub.getit(8).SUB:=nil;');
  12554. Add(' obj.sub.getit(9).GETIT(10);');
  12555. Add(' obj.sub.getit(11).SuB.getit(12);');
  12556. ConvertProgram;
  12557. CheckSource('TestClass_CallClassMethod',
  12558. LinesToStr([ // statements
  12559. 'rtl.createClass($mod,"TObject",null,function(){',
  12560. ' this.vI = 0;',
  12561. ' this.Sub = null;',
  12562. ' this.$init = function () {',
  12563. ' };',
  12564. ' this.$final = function () {',
  12565. ' };',
  12566. ' this.Create = function(){',
  12567. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12568. ' $mod.TObject.vI = this.GetMore(4);',
  12569. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12570. ' $mod.TObject.vI = this.GetMore(6);',
  12571. ' return this;',
  12572. ' };',
  12573. ' this.GetMore = function(Par){',
  12574. ' var Result = 0;',
  12575. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12576. ' $mod.TObject.vI = this.GetMore(12);',
  12577. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12578. ' $mod.TObject.vI = this.GetMore(14);',
  12579. ' return Result;',
  12580. ' };',
  12581. ' this.GetIt = function(Par){',
  12582. ' var Result = null;',
  12583. ' $mod.TObject.Sub = this.GetIt(21);',
  12584. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12585. ' $mod.TObject.Sub = this.GetIt(23);',
  12586. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12587. ' return Result;',
  12588. ' };',
  12589. '});',
  12590. 'this.Obj = null;'
  12591. ]),
  12592. LinesToStr([ // $mod.$main
  12593. '$mod.Obj = $mod.TObject.$create("Create");',
  12594. '$mod.TObject.GetIt(5);',
  12595. '$mod.Obj.$class.GetIt(6);',
  12596. '$mod.Obj.Sub.$class.GetIt(7);',
  12597. '$mod.TObject.Sub=null;',
  12598. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12599. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12600. '']));
  12601. end;
  12602. procedure TTestModule.TestClass_Property;
  12603. begin
  12604. StartProgram(false);
  12605. Add('type');
  12606. Add(' TObject = class');
  12607. Add(' Fx: longint;');
  12608. Add(' Fy: longint;');
  12609. Add(' function GetInt: longint;');
  12610. Add(' procedure SetInt(Value: longint);');
  12611. Add(' procedure DoIt;');
  12612. Add(' property IntA: longint read Fx write Fy;');
  12613. Add(' property IntB: longint read GetInt write SetInt;');
  12614. Add(' end;');
  12615. Add('function tobject.getint: longint;');
  12616. Add('begin');
  12617. Add(' result:=fx;');
  12618. Add('end;');
  12619. Add('procedure tobject.setint(value: longint);');
  12620. Add('begin');
  12621. Add(' if value=fy then exit;');
  12622. Add(' fy:=value;');
  12623. Add('end;');
  12624. Add('procedure tobject.doit;');
  12625. Add('begin');
  12626. Add(' IntA:=IntA+1;');
  12627. Add(' Self.IntA:=Self.IntA+1;');
  12628. Add(' IntB:=IntB+1;');
  12629. Add(' Self.IntB:=Self.IntB+1;');
  12630. Add('end;');
  12631. Add('var Obj: tobject;');
  12632. Add('begin');
  12633. Add(' obj.inta:=obj.inta+1;');
  12634. Add(' if obj.intb=2 then;');
  12635. Add(' obj.intb:=obj.intb+2;');
  12636. Add(' obj.setint(obj.inta);');
  12637. ConvertProgram;
  12638. CheckSource('TestClass_Property',
  12639. LinesToStr([ // statements
  12640. 'rtl.createClass($mod, "TObject", null, function () {',
  12641. ' this.$init = function () {',
  12642. ' this.Fx = 0;',
  12643. ' this.Fy = 0;',
  12644. ' };',
  12645. ' this.$final = function () {',
  12646. ' };',
  12647. ' this.GetInt = function () {',
  12648. ' var Result = 0;',
  12649. ' Result = this.Fx;',
  12650. ' return Result;',
  12651. ' };',
  12652. ' this.SetInt = function (Value) {',
  12653. ' if (Value === this.Fy) return;',
  12654. ' this.Fy = Value;',
  12655. ' };',
  12656. ' this.DoIt = function () {',
  12657. ' this.Fy = this.Fx + 1;',
  12658. ' this.Fy = this.Fx + 1;',
  12659. ' this.SetInt(this.GetInt() + 1);',
  12660. ' this.SetInt(this.GetInt() + 1);',
  12661. ' };',
  12662. '});',
  12663. 'this.Obj = null;'
  12664. ]),
  12665. LinesToStr([ // $mod.$main
  12666. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12667. 'if ($mod.Obj.GetInt() === 2);',
  12668. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12669. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12670. ]));
  12671. end;
  12672. procedure TTestModule.TestClass_Property_ClassMethod;
  12673. begin
  12674. StartProgram(false);
  12675. Add([
  12676. 'type',
  12677. ' TObject = class',
  12678. ' class var Fx: longint;',
  12679. ' class var Fy: longint;',
  12680. ' class function GetInt: longint;',
  12681. ' class procedure SetInt(Value: longint);',
  12682. ' end;',
  12683. ' TBird = class',
  12684. ' class procedure DoIt;',
  12685. ' class property IntA: longint read Fx write Fy;',
  12686. ' class property IntB: longint read GetInt write SetInt;',
  12687. ' end;',
  12688. 'class function tobject.getint: longint;',
  12689. 'begin',
  12690. ' result:=fx;',
  12691. 'end;',
  12692. 'class procedure tobject.setint(value: longint);',
  12693. 'begin',
  12694. 'end;',
  12695. 'class procedure tbird.doit;',
  12696. 'begin',
  12697. ' FX:=3;',
  12698. ' IntA:=IntA+1;',
  12699. ' Self.IntA:=Self.IntA+1;',
  12700. ' IntB:=IntB+1;',
  12701. ' Self.IntB:=Self.IntB+1;',
  12702. ' with Self do begin',
  12703. ' FX:=11;',
  12704. ' IntA:=IntA+12;',
  12705. ' IntB:=IntB+13;',
  12706. ' end;',
  12707. 'end;',
  12708. 'var Obj: tbird;',
  12709. 'begin',
  12710. ' tbird.fx:=tbird.fx+1;',
  12711. ' tbird.inta:=tbird.inta+1;',
  12712. ' if tbird.intb=2 then;',
  12713. ' tbird.intb:=tbird.intb+2;',
  12714. ' tbird.setint(tbird.inta);',
  12715. ' obj.inta:=obj.inta+1;',
  12716. ' if obj.intb=2 then;',
  12717. ' obj.intb:=obj.intb+2;',
  12718. ' obj.setint(obj.inta);',
  12719. ' with Tbird do begin',
  12720. ' FX:=FY+1;',
  12721. ' inta:=inta+2;',
  12722. ' intb:=intb+3;',
  12723. ' end;',
  12724. ' with Obj do begin',
  12725. ' FX:=FY+1;',
  12726. ' inta:=inta+2;',
  12727. ' intb:=intb+3;',
  12728. ' end;',
  12729. '']);
  12730. ConvertProgram;
  12731. CheckSource('TestClass_Property_ClassMethod',
  12732. LinesToStr([ // statements
  12733. 'rtl.createClass($mod, "TObject", null, function () {',
  12734. ' this.Fx = 0;',
  12735. ' this.Fy = 0;',
  12736. ' this.$init = function () {',
  12737. ' };',
  12738. ' this.$final = function () {',
  12739. ' };',
  12740. ' this.GetInt = function () {',
  12741. ' var Result = 0;',
  12742. ' Result = this.Fx;',
  12743. ' return Result;',
  12744. ' };',
  12745. ' this.SetInt = function (Value) {',
  12746. ' };',
  12747. '});',
  12748. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12749. ' this.DoIt = function () {',
  12750. ' $mod.TObject.Fx = 3;',
  12751. ' $mod.TObject.Fy = this.Fx + 1;',
  12752. ' $mod.TObject.Fy = this.Fx + 1;',
  12753. ' this.SetInt(this.GetInt() + 1);',
  12754. ' this.SetInt(this.GetInt() + 1);',
  12755. ' $mod.TObject.Fx = 11;',
  12756. ' $mod.TObject.Fy = this.Fx + 12;',
  12757. ' this.SetInt(this.GetInt() + 13);',
  12758. ' };',
  12759. '});',
  12760. 'this.Obj = null;'
  12761. ]),
  12762. LinesToStr([ // $mod.$main
  12763. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12764. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12765. 'if ($mod.TBird.GetInt() === 2);',
  12766. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12767. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12768. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12769. 'if ($mod.Obj.$class.GetInt() === 2);',
  12770. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12771. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12772. 'var $with = $mod.TBird;',
  12773. '$mod.TObject.Fx = $with.Fy + 1;',
  12774. '$mod.TObject.Fy = $with.Fx + 2;',
  12775. '$with.SetInt($with.GetInt() + 3);',
  12776. 'var $with1 = $mod.Obj;',
  12777. '$mod.TObject.Fx = $with1.Fy + 1;',
  12778. '$mod.TObject.Fy = $with1.Fx + 2;',
  12779. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  12780. '']));
  12781. end;
  12782. procedure TTestModule.TestClass_Property_Indexed;
  12783. begin
  12784. StartProgram(false);
  12785. Add([
  12786. 'type',
  12787. ' TObject = class',
  12788. ' FItems: array of longint;',
  12789. ' function GetItems(Index: longint): longint;',
  12790. ' procedure SetItems(Index: longint; Value: longint);',
  12791. ' procedure DoIt;',
  12792. ' property Items[Index: longint]: longint read getitems write setitems;',
  12793. ' end;',
  12794. 'function tobject.getitems(index: longint): longint;',
  12795. 'begin',
  12796. ' Result:=fitems[index];',
  12797. 'end;',
  12798. 'procedure tobject.setitems(index: longint; value: longint);',
  12799. 'begin',
  12800. ' fitems[index]:=value;',
  12801. 'end;',
  12802. 'procedure tobject.doit;',
  12803. 'begin',
  12804. ' items[1]:=2;',
  12805. ' items[3]:=items[4];',
  12806. ' self.items[5]:=self.items[6];',
  12807. ' items[items[7]]:=items[items[8]];',
  12808. 'end;',
  12809. 'var Obj: tobject;',
  12810. 'begin',
  12811. ' obj.Items[11]:=obj.Items[12];',
  12812. '']);
  12813. ConvertProgram;
  12814. CheckSource('TestClass_Property_Indexed',
  12815. LinesToStr([ // statements
  12816. 'rtl.createClass($mod, "TObject", null, function () {',
  12817. ' this.$init = function () {',
  12818. ' this.FItems = [];',
  12819. ' };',
  12820. ' this.$final = function () {',
  12821. ' this.FItems = undefined;',
  12822. ' };',
  12823. ' this.GetItems = function (Index) {',
  12824. ' var Result = 0;',
  12825. ' Result = this.FItems[Index];',
  12826. ' return Result;',
  12827. ' };',
  12828. ' this.SetItems = function (Index, Value) {',
  12829. ' this.FItems[Index] = Value;',
  12830. ' };',
  12831. ' this.DoIt = function () {',
  12832. ' this.SetItems(1, 2);',
  12833. ' this.SetItems(3,this.GetItems(4));',
  12834. ' this.SetItems(5,this.GetItems(6));',
  12835. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12836. ' };',
  12837. '});',
  12838. 'this.Obj = null;'
  12839. ]),
  12840. LinesToStr([ // $mod.$main
  12841. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12842. ]));
  12843. end;
  12844. procedure TTestModule.TestClass_Property_IndexSpec;
  12845. begin
  12846. StartProgram(false);
  12847. Add([
  12848. 'type',
  12849. ' TEnum = (red, blue);',
  12850. ' TObject = class',
  12851. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12852. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12853. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12854. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12855. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12856. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12857. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12858. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12859. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12860. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12861. ' end;',
  12862. 'procedure DoIt(b: boolean); begin end;',
  12863. 'var',
  12864. ' o: TObject;',
  12865. 'begin',
  12866. ' o.B1:=o.B1;',
  12867. ' o.B2:=o.B2;',
  12868. ' o.B3:=o.B3;',
  12869. ' o.I1[''a'']:=o.I1[''b''];',
  12870. ' doit(o.b1);',
  12871. ' doit(o.b2);',
  12872. ' doit(o.i1[''c'']);',
  12873. '']);
  12874. ConvertProgram;
  12875. CheckSource('TestClass_Property_IndexSpec',
  12876. LinesToStr([ // statements
  12877. 'this.TEnum = {',
  12878. ' "0": "red",',
  12879. ' red: 0,',
  12880. ' "1": "blue",',
  12881. ' blue: 1',
  12882. '};',
  12883. 'rtl.createClass($mod, "TObject", null, function () {',
  12884. ' this.$init = function () {',
  12885. ' };',
  12886. ' this.$final = function () {',
  12887. ' };',
  12888. '});',
  12889. 'this.DoIt = function (b) {',
  12890. '};',
  12891. 'this.o = null;',
  12892. '']),
  12893. LinesToStr([ // $mod.$main
  12894. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12895. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12896. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12897. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12898. '$mod.DoIt($mod.o.GetIntBool(1));',
  12899. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12900. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12901. '']));
  12902. end;
  12903. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12904. begin
  12905. StartProgram(false);
  12906. Add('type');
  12907. Add(' TArray = array of longint;');
  12908. Add(' TObject = class');
  12909. Add(' FItems: TArray;');
  12910. Add(' function GetItems: tarray;');
  12911. Add(' procedure SetItems(Value: tarray);');
  12912. Add(' property Items: tarray read getitems write setitems;');
  12913. Add(' procedure SetNumbers(const Value: tarray);');
  12914. Add(' property Numbers: tarray write setnumbers;');
  12915. Add(' end;');
  12916. Add('function tobject.getitems: tarray;');
  12917. Add('begin');
  12918. Add(' Result:=fitems;');
  12919. Add('end;');
  12920. Add('procedure tobject.setitems(value: tarray);');
  12921. Add('begin');
  12922. Add(' fitems:=value;');
  12923. Add(' fitems:=nil;');
  12924. Add(' Items:=nil;');
  12925. Add(' Items:=Items;');
  12926. Add(' Items[1]:=2;');
  12927. Add(' fitems[3]:=Items[4];');
  12928. Add(' Items[5]:=Items[6];');
  12929. Add(' Self.Items[7]:=8;');
  12930. Add(' Self.Items[9]:=Self.Items[10];');
  12931. Add(' Items[Items[11]]:=Items[Items[12]];');
  12932. Add('end;');
  12933. Add('procedure tobject.SetNumbers(const Value: tarray);');
  12934. Add('begin;');
  12935. Add(' Numbers:=nil;');
  12936. Add(' Numbers:=Value;');
  12937. Add(' Self.Numbers:=Value;');
  12938. Add('end;');
  12939. Add('var Obj: tobject;');
  12940. Add('begin');
  12941. Add(' obj.items:=nil;');
  12942. Add(' obj.items:=obj.items;');
  12943. Add(' obj.items[11]:=obj.items[12];');
  12944. ConvertProgram;
  12945. CheckSource('TestClass_PropertyOfTypeArray',
  12946. LinesToStr([ // statements
  12947. 'rtl.createClass($mod, "TObject", null, function () {',
  12948. ' this.$init = function () {',
  12949. ' this.FItems = [];',
  12950. ' };',
  12951. ' this.$final = function () {',
  12952. ' this.FItems = undefined;',
  12953. ' };',
  12954. ' this.GetItems = function () {',
  12955. ' var Result = [];',
  12956. ' Result = rtl.arrayRef(this.FItems);',
  12957. ' return Result;',
  12958. ' };',
  12959. ' this.SetItems = function (Value) {',
  12960. ' this.FItems = rtl.arrayRef(Value);',
  12961. ' this.FItems = [];',
  12962. ' this.SetItems([]);',
  12963. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  12964. ' this.GetItems()[1] = 2;',
  12965. ' this.FItems[3] = this.GetItems()[4];',
  12966. ' this.GetItems()[5] = this.GetItems()[6];',
  12967. ' this.GetItems()[7] = 8;',
  12968. ' this.GetItems()[9] = this.GetItems()[10];',
  12969. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12970. ' };',
  12971. ' this.SetNumbers = function (Value) {',
  12972. ' this.SetNumbers([]);',
  12973. ' this.SetNumbers(Value);',
  12974. ' this.SetNumbers(Value);',
  12975. ' };',
  12976. '});',
  12977. 'this.Obj = null;'
  12978. ]),
  12979. LinesToStr([ // $mod.$main
  12980. '$mod.Obj.SetItems([]);',
  12981. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12982. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12983. ]));
  12984. end;
  12985. procedure TTestModule.TestClass_PropertyDefault;
  12986. begin
  12987. StartProgram(false);
  12988. Add([
  12989. 'type',
  12990. ' TArray = array of longint;',
  12991. ' TObject = class',
  12992. ' end;',
  12993. ' TBird = class',
  12994. ' FItems: TArray;',
  12995. ' function GetItems(Index: longint): longint;',
  12996. ' procedure SetItems(Index, Value: longint);',
  12997. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12998. ' end;',
  12999. 'function TBird.getitems(index: longint): longint;',
  13000. 'begin',
  13001. 'end;',
  13002. 'procedure TBird.setitems(index, value: longint);',
  13003. 'begin',
  13004. ' Self[1]:=2;',
  13005. ' Self[3]:=Self[index];',
  13006. ' Self[index]:=Self[Self[value]];',
  13007. ' Self[Self[4]]:=value;',
  13008. 'end;',
  13009. 'var',
  13010. ' Bird: TBird;',
  13011. ' Obj: TObject;',
  13012. 'begin',
  13013. ' bird[11]:=12;',
  13014. ' bird[13]:=bird[14];',
  13015. ' bird[Bird[15]]:=bird[Bird[15]];',
  13016. ' TBird(obj)[16]:=TBird(obj)[17];',
  13017. ' (obj as tbird)[18]:=19;',
  13018. '']);
  13019. ConvertProgram;
  13020. CheckSource('TestClass_PropertyDefault',
  13021. LinesToStr([ // statements
  13022. 'rtl.createClass($mod, "TObject", null, function () {',
  13023. ' this.$init = function () {',
  13024. ' };',
  13025. ' this.$final = function () {',
  13026. ' };',
  13027. '});',
  13028. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13029. ' this.$init = function () {',
  13030. ' $mod.TObject.$init.call(this);',
  13031. ' this.FItems = [];',
  13032. ' };',
  13033. ' this.$final = function () {',
  13034. ' this.FItems = undefined;',
  13035. ' $mod.TObject.$final.call(this);',
  13036. ' };',
  13037. ' this.GetItems = function (Index) {',
  13038. ' var Result = 0;',
  13039. ' return Result;',
  13040. ' };',
  13041. ' this.SetItems = function (Index, Value) {',
  13042. ' this.SetItems(1, 2);',
  13043. ' this.SetItems(3, this.GetItems(Index));',
  13044. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13045. ' this.SetItems(this.GetItems(4), Value);',
  13046. ' };',
  13047. '});',
  13048. 'this.Bird = null;',
  13049. 'this.Obj = null;',
  13050. '']),
  13051. LinesToStr([ // $mod.$main
  13052. '$mod.Bird.SetItems(11, 12);',
  13053. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13054. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13055. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13056. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13057. '']));
  13058. end;
  13059. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13060. begin
  13061. StartProgram(false);
  13062. Add([
  13063. 'type',
  13064. ' TObject = class end;',
  13065. ' TAlphaList = class',
  13066. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13067. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13068. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13069. ' end;',
  13070. ' TBetaList = class',
  13071. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13072. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13073. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13074. ' end;',
  13075. ' TBird = class',
  13076. ' procedure DoIt;',
  13077. ' end;',
  13078. 'procedure TBird.DoIt;',
  13079. 'var',
  13080. ' List: TAlphaList;',
  13081. 'begin',
  13082. ' if TBetaList(List[true])[3]=nil then ;',
  13083. ' TBetaList(List[false])[5]:=nil;',
  13084. 'end;',
  13085. 'var',
  13086. ' List: TAlphaList;',
  13087. 'begin',
  13088. ' if TBetaList(List[true])[3]=nil then ;',
  13089. ' TBetaList(List[false])[5]:=nil;',
  13090. '']);
  13091. ConvertProgram;
  13092. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13093. LinesToStr([ // statements
  13094. 'rtl.createClass($mod, "TObject", null, function () {',
  13095. ' this.$init = function () {',
  13096. ' };',
  13097. ' this.$final = function () {',
  13098. ' };',
  13099. '});',
  13100. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  13101. '});',
  13102. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  13103. '});',
  13104. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13105. ' this.DoIt = function () {',
  13106. ' var List = null;',
  13107. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13108. ' List.GetAlphas(false).SetBetas(5, null);',
  13109. ' };',
  13110. '});',
  13111. 'this.List = null;',
  13112. '']),
  13113. LinesToStr([ // $mod.$main
  13114. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13115. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13116. '']));
  13117. end;
  13118. procedure TTestModule.TestClass_PropertyOverride;
  13119. begin
  13120. StartProgram(false);
  13121. Add('type');
  13122. Add(' integer = longint;');
  13123. Add(' TObject = class');
  13124. Add(' FItem: integer;');
  13125. Add(' function GetItem: integer; external name ''GetItem'';');
  13126. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13127. Add(' property Item: integer read getitem write setitem;');
  13128. Add(' end;');
  13129. Add(' TCar = class');
  13130. Add(' FBag: integer;');
  13131. Add(' function GetBag: integer; external name ''GetBag'';');
  13132. Add(' property Item read getbag;');
  13133. Add(' end;');
  13134. Add('var');
  13135. Add(' Obj: tobject;');
  13136. Add(' Car: tcar;');
  13137. Add('begin');
  13138. Add(' Obj.Item:=Obj.Item;');
  13139. Add(' Car.Item:=Car.Item;');
  13140. ConvertProgram;
  13141. CheckSource('TestClass_PropertyOverride',
  13142. LinesToStr([ // statements
  13143. 'rtl.createClass($mod, "TObject", null, function () {',
  13144. ' this.$init = function () {',
  13145. ' this.FItem = 0;',
  13146. ' };',
  13147. ' this.$final = function () {',
  13148. ' };',
  13149. '});',
  13150. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13151. ' this.$init = function () {',
  13152. ' $mod.TObject.$init.call(this);',
  13153. ' this.FBag = 0;',
  13154. ' };',
  13155. '});',
  13156. 'this.Obj = null;',
  13157. 'this.Car = null;',
  13158. '']),
  13159. LinesToStr([ // $mod.$main
  13160. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13161. '$mod.Car.SetItem($mod.Car.GetBag());',
  13162. '']));
  13163. end;
  13164. procedure TTestModule.TestClass_PropertyIncVisibility;
  13165. begin
  13166. AddModuleWithIntfImplSrc('unit1.pp',
  13167. LinesToStr([
  13168. 'type',
  13169. ' TNumber = longint;',
  13170. ' TInteger = longint;',
  13171. ' TObject = class',
  13172. ' private',
  13173. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13174. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13175. ' protected',
  13176. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13177. ' end;']),
  13178. LinesToStr([
  13179. '']));
  13180. StartProgram(true);
  13181. Add([
  13182. 'uses unit1;',
  13183. 'type',
  13184. ' TBird = class',
  13185. ' public',
  13186. ' property Items;',
  13187. ' end;',
  13188. 'procedure DoIt(i: TInteger);',
  13189. 'begin',
  13190. 'end;',
  13191. 'var b: TBird;',
  13192. 'begin',
  13193. ' b.Items[1]:=2;',
  13194. ' b.Items[3]:=b.Items[4];',
  13195. ' DoIt(b.Items[5]);',
  13196. '']);
  13197. ConvertProgram;
  13198. CheckSource('TestClass_PropertyIncVisibility',
  13199. LinesToStr([ // statements
  13200. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  13201. '});',
  13202. 'this.DoIt = function (i) {',
  13203. '};',
  13204. 'this.b = null;'
  13205. ]),
  13206. LinesToStr([ // $mod.$main
  13207. '$mod.b.SetItems(1, 2);',
  13208. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13209. '$mod.DoIt($mod.b.GetItems(5));'
  13210. ]));
  13211. end;
  13212. procedure TTestModule.TestClass_Assigned;
  13213. begin
  13214. StartProgram(false);
  13215. Add('type');
  13216. Add(' TObject = class');
  13217. Add(' end;');
  13218. Add('var');
  13219. Add(' Obj: tobject;');
  13220. Add(' b: boolean;');
  13221. Add('begin');
  13222. Add(' if Assigned(obj) then ;');
  13223. Add(' b:=Assigned(obj) or false;');
  13224. ConvertProgram;
  13225. CheckSource('TestClass_Assigned',
  13226. LinesToStr([ // statements
  13227. 'rtl.createClass($mod, "TObject", null, function () {',
  13228. ' this.$init = function () {',
  13229. ' };',
  13230. ' this.$final = function () {',
  13231. ' };',
  13232. '});',
  13233. 'this.Obj = null;',
  13234. 'this.b = false;'
  13235. ]),
  13236. LinesToStr([ // $mod.$main
  13237. 'if ($mod.Obj != null);',
  13238. '$mod.b = ($mod.Obj != null) || false;'
  13239. ]));
  13240. end;
  13241. procedure TTestModule.TestClass_WithClassDoCreate;
  13242. begin
  13243. StartProgram(false);
  13244. Add('type');
  13245. Add(' TObject = class');
  13246. Add(' aBool: boolean;');
  13247. Add(' Arr: array of boolean;');
  13248. Add(' constructor Create;');
  13249. Add(' end;');
  13250. Add('constructor TObject.Create; begin end;');
  13251. Add('var');
  13252. Add(' Obj: tobject;');
  13253. Add(' b: boolean;');
  13254. Add('begin');
  13255. Add(' with tobject.create do begin');
  13256. Add(' b:=abool;');
  13257. Add(' abool:=b;');
  13258. Add(' b:=arr[1];');
  13259. Add(' arr[2]:=b;');
  13260. Add(' end;');
  13261. Add(' with tobject do');
  13262. Add(' obj:=create;');
  13263. Add(' with obj do begin');
  13264. Add(' create;');
  13265. Add(' b:=abool;');
  13266. Add(' abool:=b;');
  13267. Add(' b:=arr[3];');
  13268. Add(' arr[4]:=b;');
  13269. Add(' end;');
  13270. ConvertProgram;
  13271. CheckSource('TestClass_WithClassDoCreate',
  13272. LinesToStr([ // statements
  13273. 'rtl.createClass($mod, "TObject", null, function () {',
  13274. ' this.$init = function () {',
  13275. ' this.aBool = false;',
  13276. ' this.Arr = [];',
  13277. ' };',
  13278. ' this.$final = function () {',
  13279. ' this.Arr = undefined;',
  13280. ' };',
  13281. ' this.Create = function () {',
  13282. ' return this;',
  13283. ' };',
  13284. '});',
  13285. 'this.Obj = null;',
  13286. 'this.b = false;'
  13287. ]),
  13288. LinesToStr([ // $mod.$main
  13289. 'var $with = $mod.TObject.$create("Create");',
  13290. '$mod.b = $with.aBool;',
  13291. '$with.aBool = $mod.b;',
  13292. '$mod.b = $with.Arr[1];',
  13293. '$with.Arr[2] = $mod.b;',
  13294. 'var $with1 = $mod.TObject;',
  13295. '$mod.Obj = $with1.$create("Create");',
  13296. 'var $with2 = $mod.Obj;',
  13297. '$with2.Create();',
  13298. '$mod.b = $with2.aBool;',
  13299. '$with2.aBool = $mod.b;',
  13300. '$mod.b = $with2.Arr[3];',
  13301. '$with2.Arr[4] = $mod.b;',
  13302. '']));
  13303. end;
  13304. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13305. begin
  13306. StartProgram(false);
  13307. Add('type');
  13308. Add(' TObject = class');
  13309. Add(' FInt: longint;');
  13310. Add(' constructor Create;');
  13311. Add(' function GetSize: longint;');
  13312. Add(' procedure SetSize(Value: longint);');
  13313. Add(' property Int: longint read FInt write FInt;');
  13314. Add(' property Size: longint read GetSize write SetSize;');
  13315. Add(' end;');
  13316. Add('constructor TObject.Create; begin end;');
  13317. Add('function TObject.GetSize: longint; begin; end;');
  13318. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13319. Add('var');
  13320. Add(' Obj: tobject;');
  13321. Add(' i: longint;');
  13322. Add('begin');
  13323. Add(' with TObject.Create do begin');
  13324. Add(' i:=int;');
  13325. Add(' int:=i;');
  13326. Add(' i:=size;');
  13327. Add(' size:=i;');
  13328. Add(' end;');
  13329. Add(' with obj do begin');
  13330. Add(' i:=int;');
  13331. Add(' int:=i;');
  13332. Add(' i:=size;');
  13333. Add(' size:=i;');
  13334. Add(' end;');
  13335. ConvertProgram;
  13336. CheckSource('TestClass_WithClassInstDoProperty',
  13337. LinesToStr([ // statements
  13338. 'rtl.createClass($mod, "TObject", null, function () {',
  13339. ' this.$init = function () {',
  13340. ' this.FInt = 0;',
  13341. ' };',
  13342. ' this.$final = function () {',
  13343. ' };',
  13344. ' this.Create = function () {',
  13345. ' return this;',
  13346. ' };',
  13347. ' this.GetSize = function () {',
  13348. ' var Result = 0;',
  13349. ' return Result;',
  13350. ' };',
  13351. ' this.SetSize = function (Value) {',
  13352. ' };',
  13353. '});',
  13354. 'this.Obj = null;',
  13355. 'this.i = 0;'
  13356. ]),
  13357. LinesToStr([ // $mod.$main
  13358. 'var $with = $mod.TObject.$create("Create");',
  13359. '$mod.i = $with.FInt;',
  13360. '$with.FInt = $mod.i;',
  13361. '$mod.i = $with.GetSize();',
  13362. '$with.SetSize($mod.i);',
  13363. 'var $with1 = $mod.Obj;',
  13364. '$mod.i = $with1.FInt;',
  13365. '$with1.FInt = $mod.i;',
  13366. '$mod.i = $with1.GetSize();',
  13367. '$with1.SetSize($mod.i);',
  13368. '']));
  13369. end;
  13370. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13371. begin
  13372. StartProgram(false);
  13373. Add('type');
  13374. Add(' TObject = class');
  13375. Add(' constructor Create;');
  13376. Add(' function GetItems(Index: longint): longint;');
  13377. Add(' procedure SetItems(Index, Value: longint);');
  13378. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13379. Add(' end;');
  13380. Add('constructor TObject.Create; begin end;');
  13381. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13382. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13383. Add('var');
  13384. Add(' Obj: tobject;');
  13385. Add(' i: longint;');
  13386. Add('begin');
  13387. Add(' with TObject.Create do begin');
  13388. Add(' i:=Items[1];');
  13389. Add(' Items[2]:=i;');
  13390. Add(' end;');
  13391. Add(' with obj do begin');
  13392. Add(' i:=Items[3];');
  13393. Add(' Items[4]:=i;');
  13394. Add(' end;');
  13395. ConvertProgram;
  13396. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13397. LinesToStr([ // statements
  13398. 'rtl.createClass($mod, "TObject", null, function () {',
  13399. ' this.$init = function () {',
  13400. ' };',
  13401. ' this.$final = function () {',
  13402. ' };',
  13403. ' this.Create = function () {',
  13404. ' return this;',
  13405. ' };',
  13406. ' this.GetItems = function (Index) {',
  13407. ' var Result = 0;',
  13408. ' return Result;',
  13409. ' };',
  13410. ' this.SetItems = function (Index, Value) {',
  13411. ' };',
  13412. '});',
  13413. 'this.Obj = null;',
  13414. 'this.i = 0;'
  13415. ]),
  13416. LinesToStr([ // $mod.$main
  13417. 'var $with = $mod.TObject.$create("Create");',
  13418. '$mod.i = $with.GetItems(1);',
  13419. '$with.SetItems(2, $mod.i);',
  13420. 'var $with1 = $mod.Obj;',
  13421. '$mod.i = $with1.GetItems(3);',
  13422. '$with1.SetItems(4, $mod.i);',
  13423. '']));
  13424. end;
  13425. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13426. begin
  13427. StartProgram(false);
  13428. Add('type');
  13429. Add(' TObject = class');
  13430. Add(' constructor Create;');
  13431. Add(' function GetSize: longint;');
  13432. Add(' procedure SetSize(Value: longint);');
  13433. Add(' end;');
  13434. Add('constructor TObject.Create; begin end;');
  13435. Add('function TObject.GetSize: longint; begin; end;');
  13436. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13437. Add('var');
  13438. Add(' Obj: tobject;');
  13439. Add(' i: longint;');
  13440. Add('begin');
  13441. Add(' with TObject.Create do begin');
  13442. Add(' i:=GetSize;');
  13443. Add(' i:=GetSize();');
  13444. Add(' SetSize(i);');
  13445. Add(' end;');
  13446. Add(' with obj do begin');
  13447. Add(' i:=GetSize;');
  13448. Add(' i:=GetSize();');
  13449. Add(' SetSize(i);');
  13450. Add(' end;');
  13451. ConvertProgram;
  13452. CheckSource('TestClass_WithClassInstDoFunc',
  13453. LinesToStr([ // statements
  13454. 'rtl.createClass($mod, "TObject", null, function () {',
  13455. ' this.$init = function () {',
  13456. ' };',
  13457. ' this.$final = function () {',
  13458. ' };',
  13459. ' this.Create = function () {',
  13460. ' return this;',
  13461. ' };',
  13462. ' this.GetSize = function () {',
  13463. ' var Result = 0;',
  13464. ' return Result;',
  13465. ' };',
  13466. ' this.SetSize = function (Value) {',
  13467. ' };',
  13468. '});',
  13469. 'this.Obj = null;',
  13470. 'this.i = 0;'
  13471. ]),
  13472. LinesToStr([ // $mod.$main
  13473. 'var $with = $mod.TObject.$create("Create");',
  13474. '$mod.i = $with.GetSize();',
  13475. '$mod.i = $with.GetSize();',
  13476. '$with.SetSize($mod.i);',
  13477. 'var $with1 = $mod.Obj;',
  13478. '$mod.i = $with1.GetSize();',
  13479. '$mod.i = $with1.GetSize();',
  13480. '$with1.SetSize($mod.i);',
  13481. '']));
  13482. end;
  13483. procedure TTestModule.TestClass_TypeCast;
  13484. begin
  13485. StartProgram(false);
  13486. Add('type');
  13487. Add(' TObject = class');
  13488. Add(' Next: TObject;');
  13489. Add(' constructor Create;');
  13490. Add(' end;');
  13491. Add(' TControl = class(TObject)');
  13492. Add(' Arr: array of TObject;');
  13493. Add(' function GetIt(vI: longint = 0): TObject;');
  13494. Add(' end;');
  13495. Add('constructor tobject.create; begin end;');
  13496. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13497. Add('var');
  13498. Add(' Obj: tobject;');
  13499. Add('begin');
  13500. Add(' obj:=tcontrol(obj).next;');
  13501. Add(' tcontrol(obj):=nil;');
  13502. Add(' obj:=tcontrol(obj);');
  13503. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13504. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13505. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13506. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13507. Add(' obj:=tcontrol(nil);');
  13508. ConvertProgram;
  13509. CheckSource('TestClass_TypeCast',
  13510. LinesToStr([ // statements
  13511. 'rtl.createClass($mod, "TObject", null, function () {',
  13512. ' this.$init = function () {',
  13513. ' this.Next = null;',
  13514. ' };',
  13515. ' this.$final = function () {',
  13516. ' this.Next = undefined;',
  13517. ' };',
  13518. ' this.Create = function () {',
  13519. ' return this;',
  13520. ' };',
  13521. '});',
  13522. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  13523. ' this.$init = function () {',
  13524. ' $mod.TObject.$init.call(this);',
  13525. ' this.Arr = [];',
  13526. ' };',
  13527. ' this.$final = function () {',
  13528. ' this.Arr = undefined;',
  13529. ' $mod.TObject.$final.call(this);',
  13530. ' };',
  13531. ' this.GetIt = function (vI) {',
  13532. ' var Result = null;',
  13533. ' return Result;',
  13534. ' };',
  13535. '});',
  13536. 'this.Obj = null;'
  13537. ]),
  13538. LinesToStr([ // $mod.$main
  13539. '$mod.Obj = $mod.Obj.Next;',
  13540. '$mod.Obj = null;',
  13541. '$mod.Obj = $mod.Obj;',
  13542. '$mod.Obj = $mod.Obj.GetIt(0);',
  13543. '$mod.Obj = $mod.Obj.GetIt(0);',
  13544. '$mod.Obj = $mod.Obj.GetIt(1);',
  13545. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13546. '$mod.Obj = null;',
  13547. '']));
  13548. end;
  13549. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13550. begin
  13551. StartProgram(false);
  13552. Add('type');
  13553. Add(' TObject = class end;');
  13554. Add('procedure ProcA(var A);');
  13555. Add('begin');
  13556. Add(' TObject(A):=nil;');
  13557. Add(' TObject(A):=TObject(A);');
  13558. Add(' if TObject(A)=nil then ;');
  13559. Add(' if nil=TObject(A) then ;');
  13560. Add('end;');
  13561. Add('procedure ProcB(out A);');
  13562. Add('begin');
  13563. Add(' TObject(A):=nil;');
  13564. Add(' TObject(A):=TObject(A);');
  13565. Add(' if TObject(A)=nil then ;');
  13566. Add(' if nil=TObject(A) then ;');
  13567. Add('end;');
  13568. Add('procedure ProcC(const A);');
  13569. Add('begin');
  13570. Add(' if TObject(A)=nil then ;');
  13571. Add(' if nil=TObject(A) then ;');
  13572. Add('end;');
  13573. Add('var o: TObject;');
  13574. Add('begin');
  13575. Add(' ProcA(o);');
  13576. Add(' ProcB(o);');
  13577. Add(' ProcC(o);');
  13578. ConvertProgram;
  13579. CheckSource('TestClass_TypeCastUntypedParam',
  13580. LinesToStr([ // statements
  13581. 'rtl.createClass($mod, "TObject", null, function () {',
  13582. ' this.$init = function () {',
  13583. ' };',
  13584. ' this.$final = function () {',
  13585. ' };',
  13586. '});',
  13587. 'this.ProcA = function (A) {',
  13588. ' A.set(null);',
  13589. ' A.set(A.get());',
  13590. ' if (A.get() === null);',
  13591. ' if (null === A.get());',
  13592. '};',
  13593. 'this.ProcB = function (A) {',
  13594. ' A.set(null);',
  13595. ' A.set(A.get());',
  13596. ' if (A.get() === null);',
  13597. ' if (null === A.get());',
  13598. '};',
  13599. 'this.ProcC = function (A) {',
  13600. ' if (A === null);',
  13601. ' if (null === A);',
  13602. '};',
  13603. 'this.o = null;',
  13604. '']),
  13605. LinesToStr([ // $mod.$main
  13606. '$mod.ProcA({',
  13607. ' p: $mod,',
  13608. ' get: function () {',
  13609. ' return this.p.o;',
  13610. ' },',
  13611. ' set: function (v) {',
  13612. ' this.p.o = v;',
  13613. ' }',
  13614. '});',
  13615. '$mod.ProcB({',
  13616. ' p: $mod,',
  13617. ' get: function () {',
  13618. ' return this.p.o;',
  13619. ' },',
  13620. ' set: function (v) {',
  13621. ' this.p.o = v;',
  13622. ' }',
  13623. '});',
  13624. '$mod.ProcC($mod.o);',
  13625. '']));
  13626. end;
  13627. procedure TTestModule.TestClass_Overloads;
  13628. begin
  13629. StartProgram(false);
  13630. Add('type');
  13631. Add(' TObject = class');
  13632. Add(' procedure DoIt;');
  13633. Add(' procedure DoIt(vI: longint);');
  13634. Add(' end;');
  13635. Add('procedure TObject.DoIt;');
  13636. Add('begin');
  13637. Add(' DoIt;');
  13638. Add(' DoIt(1);');
  13639. Add('end;');
  13640. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13641. Add('begin');
  13642. ConvertProgram;
  13643. CheckSource('TestClass_Overloads',
  13644. LinesToStr([ // statements
  13645. 'rtl.createClass($mod, "TObject", null, function () {',
  13646. ' this.$init = function () {',
  13647. ' };',
  13648. ' this.$final = function () {',
  13649. ' };',
  13650. ' this.DoIt = function () {',
  13651. ' this.DoIt();',
  13652. ' this.DoIt$1(1);',
  13653. ' };',
  13654. ' this.DoIt$1 = function (vI) {',
  13655. ' };',
  13656. '});',
  13657. '']),
  13658. LinesToStr([ // $mod.$main
  13659. '']));
  13660. end;
  13661. procedure TTestModule.TestClass_OverloadsAncestor;
  13662. begin
  13663. StartProgram(false);
  13664. Add('type');
  13665. Add(' TObject = class;');
  13666. Add(' TObject = class');
  13667. Add(' procedure DoIt(vA: longint);');
  13668. Add(' procedure DoIt(vA, vB: longint);');
  13669. Add(' end;');
  13670. Add(' TCar = class;');
  13671. Add(' TCar = class');
  13672. Add(' procedure DoIt(vA: longint);');
  13673. Add(' procedure DoIt(vA, vB: longint);');
  13674. Add(' end;');
  13675. Add('procedure tobject.doit(va: longint);');
  13676. Add('begin');
  13677. Add(' doit(1);');
  13678. Add(' doit(1,2);');
  13679. Add('end;');
  13680. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13681. Add('procedure tcar.doit(va: longint);');
  13682. Add('begin');
  13683. Add(' doit(1);');
  13684. Add(' doit(1,2);');
  13685. Add(' inherited doit(1);');
  13686. Add(' inherited doit(1,2);');
  13687. Add('end;');
  13688. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13689. Add('begin');
  13690. ConvertProgram;
  13691. CheckSource('TestClass_OverloadsAncestor',
  13692. LinesToStr([ // statements
  13693. 'rtl.createClass($mod, "TObject", null, function () {',
  13694. ' this.$init = function () {',
  13695. ' };',
  13696. ' this.$final = function () {',
  13697. ' };',
  13698. ' this.DoIt = function (vA) {',
  13699. ' this.DoIt(1);',
  13700. ' this.DoIt$1(1,2);',
  13701. ' };',
  13702. ' this.DoIt$1 = function (vA, vB) {',
  13703. ' };',
  13704. '});',
  13705. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13706. ' this.DoIt$2 = function (vA) {',
  13707. ' this.DoIt$2(1);',
  13708. ' this.DoIt$3(1, 2);',
  13709. ' $mod.TObject.DoIt.call(this, 1);',
  13710. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13711. ' };',
  13712. ' this.DoIt$3 = function (vA, vB) {',
  13713. ' };',
  13714. '});',
  13715. '']),
  13716. LinesToStr([ // $mod.$main
  13717. '']));
  13718. end;
  13719. procedure TTestModule.TestClass_OverloadConstructor;
  13720. begin
  13721. StartProgram(false);
  13722. Add('type');
  13723. Add(' TObject = class');
  13724. Add(' constructor Create(vA: longint);');
  13725. Add(' constructor Create(vA, vB: longint);');
  13726. Add(' end;');
  13727. Add(' TCar = class');
  13728. Add(' constructor Create(vA: longint);');
  13729. Add(' constructor Create(vA, vB: longint);');
  13730. Add(' end;');
  13731. Add('constructor tobject.create(va: longint);');
  13732. Add('begin');
  13733. Add(' create(1);');
  13734. Add(' create(1,2);');
  13735. Add('end;');
  13736. Add('constructor tobject.create(va, vb: longint); begin end;');
  13737. Add('constructor tcar.create(va: longint);');
  13738. Add('begin');
  13739. Add(' create(1);');
  13740. Add(' create(1,2);');
  13741. Add(' inherited create(1);');
  13742. Add(' inherited create(1,2);');
  13743. Add('end;');
  13744. Add('constructor tcar.create(va, vb: longint); begin end;');
  13745. Add('begin');
  13746. Add(' tobject.create(1);');
  13747. Add(' tobject.create(1,2);');
  13748. Add(' tcar.create(1);');
  13749. Add(' tcar.create(1,2);');
  13750. ConvertProgram;
  13751. CheckSource('TestClass_OverloadConstructor',
  13752. LinesToStr([ // statements
  13753. 'rtl.createClass($mod, "TObject", null, function () {',
  13754. ' this.$init = function () {',
  13755. ' };',
  13756. ' this.$final = function () {',
  13757. ' };',
  13758. ' this.Create = function (vA) {',
  13759. ' this.Create(1);',
  13760. ' this.Create$1(1,2);',
  13761. ' return this;',
  13762. ' };',
  13763. ' this.Create$1 = function (vA, vB) {',
  13764. ' return this;',
  13765. ' };',
  13766. '});',
  13767. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13768. ' this.Create$2 = function (vA) {',
  13769. ' this.Create$2(1);',
  13770. ' this.Create$3(1, 2);',
  13771. ' $mod.TObject.Create.call(this, 1);',
  13772. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13773. ' return this;',
  13774. ' };',
  13775. ' this.Create$3 = function (vA, vB) {',
  13776. ' return this;',
  13777. ' };',
  13778. '});',
  13779. '']),
  13780. LinesToStr([ // $mod.$main
  13781. '$mod.TObject.$create("Create", [1]);',
  13782. '$mod.TObject.$create("Create$1", [1, 2]);',
  13783. '$mod.TCar.$create("Create$2", [1]);',
  13784. '$mod.TCar.$create("Create$3", [1, 2]);',
  13785. '']));
  13786. end;
  13787. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13788. begin
  13789. StartProgram(false);
  13790. Add([
  13791. '{$mode delphi}',
  13792. 'type',
  13793. ' TObject = class end;',
  13794. ' TBird = class',
  13795. ' function {#a}GetValue: longint; overload; virtual;',
  13796. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  13797. ' end;',
  13798. ' TEagle = class(TBird)',
  13799. ' function {#c}GetValue: longint; overload; override;',
  13800. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  13801. ' end;',
  13802. 'function TBird.GetValue: longint;',
  13803. 'begin',
  13804. ' if 3={@a}GetValue then ;',
  13805. ' if 4={@b}GetValue(5) then ;',
  13806. 'end;',
  13807. 'function TBird.GetValue(AValue: longint): longint;',
  13808. 'begin',
  13809. 'end;',
  13810. 'function TEagle.GetValue: longint;',
  13811. 'begin',
  13812. ' if 13={@c}GetValue then ;',
  13813. ' if 14={@d}GetValue(15) then ;',
  13814. ' if 15=inherited {@a}GetValue then ;',
  13815. ' if 16=inherited {@b}GetValue(17) then ;',
  13816. 'end;',
  13817. 'function TEagle.GetValue(AValue: longint): longint;',
  13818. 'begin',
  13819. 'end;',
  13820. 'var',
  13821. ' e: TEagle;',
  13822. 'begin',
  13823. ' if 23=e.{@c}GetValue then ;',
  13824. ' if 24=e.{@d}GetValue(25) then ;']);
  13825. ConvertProgram;
  13826. CheckSource('TestClass_OverloadDelphiOverride',
  13827. LinesToStr([ // statements
  13828. 'rtl.createClass($mod, "TObject", null, function () {',
  13829. ' this.$init = function () {',
  13830. ' };',
  13831. ' this.$final = function () {',
  13832. ' };',
  13833. '});',
  13834. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13835. ' this.GetValue = function () {',
  13836. ' var Result = 0;',
  13837. ' if (3 === this.GetValue()) ;',
  13838. ' if (4 === this.GetValue$1(5)) ;',
  13839. ' return Result;',
  13840. ' };',
  13841. ' this.GetValue$1 = function (AValue) {',
  13842. ' var Result = 0;',
  13843. ' return Result;',
  13844. ' };',
  13845. '});',
  13846. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13847. ' this.GetValue = function () {',
  13848. ' var Result = 0;',
  13849. ' if (13 === this.GetValue()) ;',
  13850. ' if (14 === this.GetValue$1(15)) ;',
  13851. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13852. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13853. ' return Result;',
  13854. ' };',
  13855. ' this.GetValue$1 = function (AValue) {',
  13856. ' var Result = 0;',
  13857. ' return Result;',
  13858. ' };',
  13859. '});',
  13860. 'this.e = null;',
  13861. '']),
  13862. LinesToStr([ // $mod.$main
  13863. 'if (23 === $mod.e.GetValue()) ;',
  13864. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13865. '']));
  13866. end;
  13867. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  13868. begin
  13869. StartProgram(false);
  13870. Add([
  13871. '{$mode delphi}',
  13872. 'type',
  13873. ' TObject = class end;',
  13874. ' TAnimal = class',
  13875. ' public',
  13876. ' {#animal_a}A: longint;',
  13877. ' function {#animal_b}B: longint;',
  13878. ' end;',
  13879. ' TBird = class(TAnimal)',
  13880. ' public',
  13881. ' {#bird_a}A: double;',
  13882. ' {#bird_b}B: boolean;',
  13883. ' end;',
  13884. ' TEagle = class(TBird)',
  13885. ' public',
  13886. ' function {#eagle_a}A: boolean;',
  13887. ' {#eagle_b}B: double;',
  13888. ' end;',
  13889. 'function TAnimal.B: longint;',
  13890. 'begin',
  13891. 'end;',
  13892. 'function TEagle.A: boolean;',
  13893. 'begin',
  13894. ' {@eagle_b}B:=3.3;',
  13895. ' {@eagle_a}A();',
  13896. ' TBird(Self).{@bird_b}B:=true;',
  13897. ' TAnimal(Self).{@animal_a}A:=17;',
  13898. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  13899. 'end;',
  13900. 'var',
  13901. ' e: TEagle;',
  13902. 'begin',
  13903. ' e.{@eagle_b}B:=5.3;',
  13904. ' if e.{@eagle_a}A then ;',
  13905. '']);
  13906. ConvertProgram;
  13907. CheckSource('TestClass_ReintroduceVarDelphi',
  13908. LinesToStr([ // statements
  13909. 'rtl.createClass($mod, "TObject", null, function () {',
  13910. ' this.$init = function () {',
  13911. ' };',
  13912. ' this.$final = function () {',
  13913. ' };',
  13914. '});',
  13915. 'rtl.createClass($mod, "TAnimal", $mod.TObject, function () {',
  13916. ' this.$init = function () {',
  13917. ' $mod.TObject.$init.call(this);',
  13918. ' this.A = 0;',
  13919. ' };',
  13920. ' this.B = function () {',
  13921. ' var Result = 0;',
  13922. ' return Result;',
  13923. ' };',
  13924. '});',
  13925. 'rtl.createClass($mod, "TBird", $mod.TAnimal, function () {',
  13926. ' this.$init = function () {',
  13927. ' $mod.TAnimal.$init.call(this);',
  13928. ' this.A$1 = 0.0;',
  13929. ' this.B$1 = false;',
  13930. ' };',
  13931. '});',
  13932. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13933. ' this.$init = function () {',
  13934. ' $mod.TBird.$init.call(this);',
  13935. ' this.B$2 = 0.0;',
  13936. ' };',
  13937. ' this.A$2 = function () {',
  13938. ' var Result = false;',
  13939. ' this.B$2 = 3.3;',
  13940. ' this.A$2();',
  13941. ' this.B$1 = true;',
  13942. ' this.A = 17;',
  13943. ' this.B$1 = this.A$1 > 1;',
  13944. ' return Result;',
  13945. ' };',
  13946. '});',
  13947. 'this.e = null;',
  13948. '']),
  13949. LinesToStr([ // $mod.$main
  13950. '$mod.e.B$2 = 5.3;',
  13951. 'if ($mod.e.A$2()) ;',
  13952. '']));
  13953. end;
  13954. procedure TTestModule.TestClass_ReintroducedVar;
  13955. begin
  13956. StartProgram(false);
  13957. Add('type');
  13958. Add(' TObject = class');
  13959. Add(' strict private');
  13960. Add(' Some: longint;');
  13961. Add(' end;');
  13962. Add(' TMobile = class');
  13963. Add(' strict private');
  13964. Add(' Some: string;');
  13965. Add(' end;');
  13966. Add(' TCar = class(tmobile)');
  13967. Add(' procedure Some;');
  13968. Add(' procedure Some(vA: longint);');
  13969. Add(' end;');
  13970. Add('procedure tcar.some;');
  13971. Add('begin');
  13972. Add(' Some;');
  13973. Add(' Some(1);');
  13974. Add('end;');
  13975. Add('procedure tcar.some(va: longint); begin end;');
  13976. Add('begin');
  13977. ConvertProgram;
  13978. CheckSource('TestClass_ReintroducedVar',
  13979. LinesToStr([ // statements
  13980. 'rtl.createClass($mod, "TObject", null, function () {',
  13981. ' this.$init = function () {',
  13982. ' this.Some = 0;',
  13983. ' };',
  13984. ' this.$final = function () {',
  13985. ' };',
  13986. '});',
  13987. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13988. ' this.$init = function () {',
  13989. ' $mod.TObject.$init.call(this);',
  13990. ' this.Some$1 = "";',
  13991. ' };',
  13992. '});',
  13993. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13994. ' this.Some$2 = function () {',
  13995. ' this.Some$2();',
  13996. ' this.Some$3(1);',
  13997. ' };',
  13998. ' this.Some$3 = function (vA) {',
  13999. ' };',
  14000. '});',
  14001. '']),
  14002. LinesToStr([ // $mod.$main
  14003. '']));
  14004. end;
  14005. procedure TTestModule.TestClass_RaiseDescendant;
  14006. begin
  14007. StartProgram(false);
  14008. Add([
  14009. 'type',
  14010. ' TObject = class',
  14011. ' constructor Create(Msg: string);',
  14012. ' end;',
  14013. ' Exception = class',
  14014. ' end;',
  14015. ' EConvertError = class(Exception)',
  14016. ' end;',
  14017. 'constructor TObject.Create(Msg: string); begin end;',
  14018. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14019. 'begin',
  14020. ' raise Exception.Create(''Bar1'');',
  14021. ' raise EConvertError.Create(''Bar2'');',
  14022. ' raise AssertConv(''Bar2'');',
  14023. ' raise AssertConv;',
  14024. '']);
  14025. ConvertProgram;
  14026. CheckSource('TestClass_RaiseDescendant',
  14027. LinesToStr([ // statements
  14028. 'rtl.createClass($mod, "TObject", null, function () {',
  14029. ' this.$init = function () {',
  14030. ' };',
  14031. ' this.$final = function () {',
  14032. ' };',
  14033. ' this.Create = function (Msg) {',
  14034. ' return this;',
  14035. ' };',
  14036. '});',
  14037. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  14038. '});',
  14039. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  14040. '});',
  14041. 'this.AssertConv = function (Msg) {',
  14042. ' var Result = null;',
  14043. ' return Result;',
  14044. '};',
  14045. '']),
  14046. LinesToStr([ // $mod.$main
  14047. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14048. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14049. 'throw $mod.AssertConv("Bar2");',
  14050. 'throw $mod.AssertConv("def");',
  14051. '']));
  14052. end;
  14053. procedure TTestModule.TestClass_ExternalMethod;
  14054. begin
  14055. AddModuleWithIntfImplSrc('unit2.pas',
  14056. LinesToStr([
  14057. 'type',
  14058. ' TObject = class',
  14059. ' public',
  14060. ' procedure Intern; external name ''$DoIntern'';',
  14061. ' end;',
  14062. '']),
  14063. LinesToStr([
  14064. '']));
  14065. StartUnit(true);
  14066. Add('interface');
  14067. Add('uses unit2;');
  14068. Add('type');
  14069. Add(' TCar = class(TObject)');
  14070. Add(' public');
  14071. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14072. Add(' procedure DoIt;');
  14073. Add(' end;');
  14074. Add('implementation');
  14075. Add('procedure tcar.doit;');
  14076. Add('begin');
  14077. Add(' Intern;');
  14078. Add(' Intern();');
  14079. Add(' Intern2;');
  14080. Add(' Intern2();');
  14081. Add('end;');
  14082. Add('var Obj: TCar;');
  14083. Add('begin');
  14084. Add(' obj.intern;');
  14085. Add(' obj.intern();');
  14086. Add(' obj.intern2;');
  14087. Add(' obj.intern2();');
  14088. Add(' obj.doit;');
  14089. Add(' obj.doit();');
  14090. Add(' with obj do begin');
  14091. Add(' Intern;');
  14092. Add(' Intern();');
  14093. Add(' Intern2;');
  14094. Add(' Intern2();');
  14095. Add(' end;');
  14096. ConvertUnit;
  14097. CheckSource('TestClass_ExternalMethod',
  14098. LinesToStr([
  14099. 'var $impl = $mod.$impl;',
  14100. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  14101. ' this.DoIt = function () {',
  14102. ' this.$DoIntern();',
  14103. ' this.$DoIntern();',
  14104. ' this.$DoIntern2();',
  14105. ' this.$DoIntern2();',
  14106. ' };',
  14107. ' });',
  14108. '']),
  14109. LinesToStr([ // this.$init
  14110. '$impl.Obj.$DoIntern();',
  14111. '$impl.Obj.$DoIntern();',
  14112. '$impl.Obj.$DoIntern2();',
  14113. '$impl.Obj.$DoIntern2();',
  14114. '$impl.Obj.DoIt();',
  14115. '$impl.Obj.DoIt();',
  14116. 'var $with = $impl.Obj;',
  14117. '$with.$DoIntern();',
  14118. '$with.$DoIntern();',
  14119. '$with.$DoIntern2();',
  14120. '$with.$DoIntern2();',
  14121. '']),
  14122. LinesToStr([ // implementation
  14123. '$impl.Obj = null;',
  14124. '']) );
  14125. end;
  14126. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14127. begin
  14128. StartProgram(false);
  14129. Add('type');
  14130. Add(' TObject = class');
  14131. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14132. Add(' end;');
  14133. Add('begin');
  14134. SetExpectedPasResolverError('Virtual method name must match external',
  14135. nVirtualMethodNameMustMatchExternal);
  14136. ConvertProgram;
  14137. end;
  14138. procedure TTestModule.TestClass_ExternalOverrideFail;
  14139. begin
  14140. StartProgram(false);
  14141. Add('type');
  14142. Add(' TObject = class');
  14143. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14144. Add(' end;');
  14145. Add(' TCar = class');
  14146. Add(' procedure DoIt; override; external name ''DoIt'';');
  14147. Add(' end;');
  14148. Add('begin');
  14149. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14150. nInvalidXModifierY);
  14151. ConvertProgram;
  14152. end;
  14153. procedure TTestModule.TestClass_ExternalVar;
  14154. begin
  14155. AddModuleWithIntfImplSrc('unit2.pas',
  14156. LinesToStr([
  14157. '{$modeswitch externalclass}',
  14158. 'type',
  14159. ' TObject = class',
  14160. ' public',
  14161. ' Intern: longint external name ''$Intern'';',
  14162. ' Bracket: longint external name ''["A B"]'';',
  14163. ' end;',
  14164. '']),
  14165. LinesToStr([
  14166. '']));
  14167. StartUnit(true);
  14168. Add([
  14169. 'interface',
  14170. 'uses unit2;',
  14171. '{$modeswitch externalclass}',
  14172. 'type',
  14173. ' TCar = class(tobject)',
  14174. ' public',
  14175. ' Intern2: longint external name ''$Intern2'';',
  14176. ' procedure DoIt;',
  14177. ' end;',
  14178. 'implementation',
  14179. 'procedure tcar.doit;',
  14180. 'begin',
  14181. ' Intern:=Intern+1;',
  14182. ' Intern2:=Intern2+2;',
  14183. ' Bracket:=Bracket+3;',
  14184. 'end;',
  14185. 'var Obj: TCar;',
  14186. 'begin',
  14187. ' obj.intern:=obj.intern+1;',
  14188. ' obj.intern2:=obj.intern2+2;',
  14189. ' obj.Bracket:=obj.Bracket+3;',
  14190. ' with obj do begin',
  14191. ' intern:=intern+1;',
  14192. ' intern2:=intern2+2;',
  14193. ' Bracket:=Bracket+3;',
  14194. ' end;']);
  14195. ConvertUnit;
  14196. CheckSource('TestClass_ExternalVar',
  14197. LinesToStr([
  14198. 'var $impl = $mod.$impl;',
  14199. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  14200. ' this.DoIt = function () {',
  14201. ' this.$Intern = this.$Intern + 1;',
  14202. ' this.$Intern2 = this.$Intern2 + 2;',
  14203. ' this["A B"] = this["A B"] + 3;',
  14204. ' };',
  14205. ' });',
  14206. '']),
  14207. LinesToStr([
  14208. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14209. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14210. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14211. 'var $with = $impl.Obj;',
  14212. '$with.$Intern = $with.$Intern + 1;',
  14213. '$with.$Intern2 = $with.$Intern2 + 2;',
  14214. '$with["A B"] = $with["A B"] + 3;',
  14215. '']),
  14216. LinesToStr([ // implementation
  14217. '$impl.Obj = null;',
  14218. '']));
  14219. end;
  14220. procedure TTestModule.TestClass_Const;
  14221. begin
  14222. StartProgram(false);
  14223. Add('type');
  14224. Add(' integer = longint;');
  14225. Add(' TClass = class of TObject;');
  14226. Add(' TObject = class');
  14227. Add(' public');
  14228. Add(' const cI: integer = 3;');
  14229. Add(' procedure DoIt;');
  14230. Add(' class procedure DoMore;');
  14231. Add(' end;');
  14232. Add('procedure tobject.doit;');
  14233. Add('begin');
  14234. Add(' if cI=4 then;');
  14235. Add(' if 5=cI then;');
  14236. Add(' if Self.cI=6 then;');
  14237. Add(' if 7=Self.cI then;');
  14238. Add(' with Self do begin');
  14239. Add(' if cI=11 then;');
  14240. Add(' if 12=cI then;');
  14241. Add(' end;');
  14242. Add('end;');
  14243. Add('class procedure tobject.domore;');
  14244. Add('begin');
  14245. Add(' if cI=8 then;');
  14246. Add(' if Self.cI=9 then;');
  14247. Add(' if 10=cI then;');
  14248. Add(' if 11=Self.cI then;');
  14249. Add(' with Self do begin');
  14250. Add(' if cI=13 then;');
  14251. Add(' if 14=cI then;');
  14252. Add(' end;');
  14253. Add('end;');
  14254. Add('var');
  14255. Add(' Obj: TObject;');
  14256. Add(' Cla: TClass;');
  14257. Add('begin');
  14258. Add(' if TObject.cI=21 then ;');
  14259. Add(' if Obj.cI=22 then ;');
  14260. Add(' if Cla.cI=23 then ;');
  14261. Add(' with obj do if ci=24 then;');
  14262. Add(' with TObject do if ci=25 then;');
  14263. Add(' with Cla do if ci=26 then;');
  14264. ConvertProgram;
  14265. CheckSource('TestClass_Const',
  14266. LinesToStr([
  14267. 'rtl.createClass($mod, "TObject", null, function () {',
  14268. ' this.cI = 3;',
  14269. ' this.$init = function () {',
  14270. ' };',
  14271. ' this.$final = function () {',
  14272. ' };',
  14273. ' this.DoIt = function () {',
  14274. ' if (this.cI === 4) ;',
  14275. ' if (5 === this.cI) ;',
  14276. ' if (this.cI === 6) ;',
  14277. ' if (7 === this.cI) ;',
  14278. ' if (this.cI === 11) ;',
  14279. ' if (12 === this.cI) ;',
  14280. ' };',
  14281. ' this.DoMore = function () {',
  14282. ' if (this.cI === 8) ;',
  14283. ' if (this.cI === 9) ;',
  14284. ' if (10 === this.cI) ;',
  14285. ' if (11 === this.cI) ;',
  14286. ' if (this.cI === 13) ;',
  14287. ' if (14 === this.cI) ;',
  14288. ' };',
  14289. '});',
  14290. 'this.Obj = null;',
  14291. 'this.Cla = null;',
  14292. '']),
  14293. LinesToStr([
  14294. 'if ($mod.TObject.cI === 21) ;',
  14295. 'if ($mod.Obj.cI === 22) ;',
  14296. 'if ($mod.Cla.cI === 23) ;',
  14297. 'var $with = $mod.Obj;',
  14298. 'if ($with.cI === 24) ;',
  14299. 'var $with1 = $mod.TObject;',
  14300. 'if ($with1.cI === 25) ;',
  14301. 'var $with2 = $mod.Cla;',
  14302. 'if ($with2.cI === 26) ;',
  14303. '']));
  14304. end;
  14305. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14306. begin
  14307. StartProgram(false);
  14308. Add([
  14309. 'type',
  14310. ' TObject = class',
  14311. ' const cI: longint = 3;',
  14312. ' procedure Fly;',
  14313. ' procedure Run;',
  14314. ' end;',
  14315. ' TBird = class',
  14316. ' procedure Go;',
  14317. ' end;',
  14318. 'procedure tobject.fly;',
  14319. 'const cI: word = 4;',
  14320. 'begin',
  14321. ' if cI=Self.cI then ;',
  14322. 'end;',
  14323. 'procedure tobject.run;',
  14324. 'const cI: word = 5;',
  14325. 'begin',
  14326. ' if cI=Self.cI then ;',
  14327. 'end;',
  14328. 'procedure tbird.go;',
  14329. 'const cI: word = 6;',
  14330. 'begin',
  14331. ' if cI=Self.cI then ;',
  14332. 'end;',
  14333. 'begin',
  14334. '']);
  14335. ConvertProgram;
  14336. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14337. LinesToStr([
  14338. 'rtl.createClass($mod, "TObject", null, function () {',
  14339. ' this.cI = 3;',
  14340. ' this.$init = function () {',
  14341. ' };',
  14342. ' this.$final = function () {',
  14343. ' };',
  14344. ' var cI$1 = 4;',
  14345. ' this.Fly = function () {',
  14346. ' if (cI$1 === this.cI) ;',
  14347. ' };',
  14348. ' var cI$2 = 5;',
  14349. ' this.Run = function () {',
  14350. ' if (cI$2 === this.cI) ;',
  14351. ' };',
  14352. '});',
  14353. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14354. ' var cI$3 = 6;',
  14355. ' this.Go = function () {',
  14356. ' if (cI$3 === this.cI) ;',
  14357. ' };',
  14358. '});',
  14359. '']),
  14360. LinesToStr([
  14361. '']));
  14362. end;
  14363. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14364. begin
  14365. StartUnit(false);
  14366. Add([
  14367. 'interface',
  14368. 'type',
  14369. ' TObject = class',
  14370. ' const cI: longint = 3;',
  14371. ' procedure Fly;',
  14372. ' procedure Run;',
  14373. ' end;',
  14374. ' TBird = class',
  14375. ' procedure Go;',
  14376. ' end;',
  14377. 'implementation',
  14378. 'procedure tobject.fly;',
  14379. 'const cI: word = 4;',
  14380. 'begin',
  14381. ' if cI=Self.cI then ;',
  14382. 'end;',
  14383. 'procedure tobject.run;',
  14384. 'const cI: word = 5;',
  14385. 'begin',
  14386. ' if cI=Self.cI then ;',
  14387. 'end;',
  14388. 'procedure tbird.go;',
  14389. 'const cI: word = 6;',
  14390. 'begin',
  14391. ' if cI=Self.cI then ;',
  14392. 'end;',
  14393. '']);
  14394. ConvertUnit;
  14395. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14396. LinesToStr([
  14397. 'rtl.createClass($mod, "TObject", null, function () {',
  14398. ' this.cI = 3;',
  14399. ' this.$init = function () {',
  14400. ' };',
  14401. ' this.$final = function () {',
  14402. ' };',
  14403. ' var cI$1 = 4;',
  14404. ' this.Fly = function () {',
  14405. ' if (cI$1 === this.cI) ;',
  14406. ' };',
  14407. ' var cI$2 = 5;',
  14408. ' this.Run = function () {',
  14409. ' if (cI$2 === this.cI) ;',
  14410. ' };',
  14411. '});',
  14412. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14413. ' var cI$3 = 6;',
  14414. ' this.Go = function () {',
  14415. ' if (cI$3 === this.cI) ;',
  14416. ' };',
  14417. '});',
  14418. '']),
  14419. '',
  14420. '');
  14421. end;
  14422. procedure TTestModule.TestClass_LocalVarSelfFail;
  14423. begin
  14424. StartProgram(false);
  14425. Add([
  14426. 'type',
  14427. ' TObject = class',
  14428. ' constructor Create;',
  14429. ' end;',
  14430. 'constructor tobject.create;',
  14431. 'var self: longint;',
  14432. 'begin',
  14433. 'end',
  14434. 'begin',
  14435. '']);
  14436. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14437. ConvertProgram;
  14438. end;
  14439. procedure TTestModule.TestClass_ArgSelfFail;
  14440. begin
  14441. StartProgram(false);
  14442. Add([
  14443. 'type',
  14444. ' TObject = class',
  14445. ' procedure DoIt(Self: longint);',
  14446. ' end;',
  14447. 'procedure tobject.doit(self: longint);',
  14448. 'begin',
  14449. 'end',
  14450. 'begin',
  14451. '']);
  14452. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14453. ConvertProgram;
  14454. end;
  14455. procedure TTestModule.TestClass_NestedProcSelf;
  14456. begin
  14457. StartProgram(false);
  14458. Add([
  14459. 'type',
  14460. ' TObject = class',
  14461. ' Key: longint;',
  14462. ' class var State: longint;',
  14463. ' procedure DoIt;',
  14464. ' function GetSize: longint; virtual; abstract;',
  14465. ' procedure SetSize(Value: longint); virtual; abstract;',
  14466. ' property Size: longint read GetSize write SetSize;',
  14467. ' end;',
  14468. 'procedure tobject.doit;',
  14469. ' procedure Sub;',
  14470. ' begin',
  14471. ' key:=key+2;',
  14472. ' self.key:=self.key+3;',
  14473. ' state:=state+4;',
  14474. ' self.state:=self.state+5;',
  14475. ' tobject.state:=tobject.state+6;',
  14476. ' size:=size+7;',
  14477. ' self.size:=self.size+8;',
  14478. ' end;',
  14479. 'begin',
  14480. ' sub;',
  14481. ' key:=key+12;',
  14482. ' self.key:=self.key+13;',
  14483. ' state:=state+14;',
  14484. ' self.state:=self.state+15;',
  14485. ' tobject.state:=tobject.state+16;',
  14486. ' size:=size+17;',
  14487. ' self.size:=self.size+18;',
  14488. 'end;',
  14489. 'begin',
  14490. '']);
  14491. ConvertProgram;
  14492. CheckSource('TestClass_NestedProcSelf',
  14493. LinesToStr([ // statements
  14494. 'rtl.createClass($mod, "TObject", null, function () {',
  14495. ' this.State = 0;',
  14496. ' this.$init = function () {',
  14497. ' this.Key = 0;',
  14498. ' };',
  14499. ' this.$final = function () {',
  14500. ' };',
  14501. ' this.DoIt = function () {',
  14502. ' var $Self = this;',
  14503. ' function Sub() {',
  14504. ' $Self.Key = $Self.Key + 2;',
  14505. ' $Self.Key = $Self.Key + 3;',
  14506. ' $mod.TObject.State = $Self.State + 4;',
  14507. ' $mod.TObject.State = $Self.State + 5;',
  14508. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14509. ' $Self.SetSize($Self.GetSize() + 7);',
  14510. ' $Self.SetSize($Self.GetSize() + 8);',
  14511. ' };',
  14512. ' Sub();',
  14513. ' $Self.Key = $Self.Key + 12;',
  14514. ' $Self.Key = $Self.Key + 13;',
  14515. ' $mod.TObject.State = $Self.State + 14;',
  14516. ' $mod.TObject.State = $Self.State + 15;',
  14517. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14518. ' $Self.SetSize($Self.GetSize() + 17);',
  14519. ' $Self.SetSize($Self.GetSize() + 18);',
  14520. ' };',
  14521. '});',
  14522. '']),
  14523. LinesToStr([ // $mod.$main
  14524. '']));
  14525. end;
  14526. procedure TTestModule.TestClass_NestedProcSelf2;
  14527. begin
  14528. StartProgram(false);
  14529. Add([
  14530. 'type',
  14531. ' TObject = class',
  14532. ' Key: longint;',
  14533. ' class var State: longint;',
  14534. ' function GetSize: longint; virtual; abstract;',
  14535. ' procedure SetSize(Value: longint); virtual; abstract;',
  14536. ' property Size: longint read GetSize write SetSize;',
  14537. ' end;',
  14538. ' TBird = class',
  14539. ' procedure DoIt;',
  14540. ' end;',
  14541. 'procedure tbird.doit;',
  14542. ' procedure Sub;',
  14543. ' begin',
  14544. ' key:=key+2;',
  14545. ' self.key:=self.key+3;',
  14546. ' state:=state+4;',
  14547. ' self.state:=self.state+5;',
  14548. ' tobject.state:=tobject.state+6;',
  14549. ' size:=size+7;',
  14550. ' self.size:=self.size+8;',
  14551. ' end;',
  14552. 'begin',
  14553. ' sub;',
  14554. ' key:=key+12;',
  14555. ' self.key:=self.key+13;',
  14556. ' state:=state+14;',
  14557. ' self.state:=self.state+15;',
  14558. ' tobject.state:=tobject.state+16;',
  14559. ' size:=size+17;',
  14560. ' self.size:=self.size+18;',
  14561. 'end;',
  14562. 'begin',
  14563. '']);
  14564. ConvertProgram;
  14565. CheckSource('TestClass_NestedProcSelf2',
  14566. LinesToStr([ // statements
  14567. 'rtl.createClass($mod, "TObject", null, function () {',
  14568. ' this.State = 0;',
  14569. ' this.$init = function () {',
  14570. ' this.Key = 0;',
  14571. ' };',
  14572. ' this.$final = function () {',
  14573. ' };',
  14574. '});',
  14575. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14576. ' this.DoIt = function () {',
  14577. ' var $Self = this;',
  14578. ' function Sub() {',
  14579. ' $Self.Key = $Self.Key + 2;',
  14580. ' $Self.Key = $Self.Key + 3;',
  14581. ' $mod.TObject.State = $Self.State + 4;',
  14582. ' $mod.TObject.State = $Self.State + 5;',
  14583. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14584. ' $Self.SetSize($Self.GetSize() + 7);',
  14585. ' $Self.SetSize($Self.GetSize() + 8);',
  14586. ' };',
  14587. ' Sub();',
  14588. ' $Self.Key = $Self.Key + 12;',
  14589. ' $Self.Key = $Self.Key + 13;',
  14590. ' $mod.TObject.State = $Self.State + 14;',
  14591. ' $mod.TObject.State = $Self.State + 15;',
  14592. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14593. ' $Self.SetSize($Self.GetSize() + 17);',
  14594. ' $Self.SetSize($Self.GetSize() + 18);',
  14595. ' };',
  14596. '});',
  14597. '']),
  14598. LinesToStr([ // $mod.$main
  14599. '']));
  14600. end;
  14601. procedure TTestModule.TestClass_NestedProcClassSelf;
  14602. begin
  14603. StartProgram(false);
  14604. Add([
  14605. 'type',
  14606. ' TObject = class',
  14607. ' class var State: longint;',
  14608. ' class procedure DoIt;',
  14609. ' class function GetSize: longint; virtual; abstract;',
  14610. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14611. ' class property Size: longint read GetSize write SetSize;',
  14612. ' end;',
  14613. 'class procedure tobject.doit;',
  14614. ' procedure Sub;',
  14615. ' begin',
  14616. ' state:=state+2;',
  14617. ' self.state:=self.state+3;',
  14618. ' tobject.state:=tobject.state+4;',
  14619. ' size:=size+5;',
  14620. ' self.size:=self.size+6;',
  14621. ' tobject.size:=tobject.size+7;',
  14622. ' end;',
  14623. 'begin',
  14624. ' sub;',
  14625. ' state:=state+12;',
  14626. ' self.state:=self.state+13;',
  14627. ' tobject.state:=tobject.state+14;',
  14628. ' size:=size+15;',
  14629. ' self.size:=self.size+16;',
  14630. ' tobject.size:=tobject.size+17;',
  14631. 'end;',
  14632. 'begin',
  14633. '']);
  14634. ConvertProgram;
  14635. CheckSource('TestClass_NestedProcClassSelf',
  14636. LinesToStr([ // statements
  14637. 'rtl.createClass($mod, "TObject", null, function () {',
  14638. ' this.State = 0;',
  14639. ' this.$init = function () {',
  14640. ' };',
  14641. ' this.$final = function () {',
  14642. ' };',
  14643. ' this.DoIt = function () {',
  14644. ' var $Self = this;',
  14645. ' function Sub() {',
  14646. ' $mod.TObject.State = $Self.State + 2;',
  14647. ' $mod.TObject.State = $Self.State + 3;',
  14648. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14649. ' $Self.SetSize($Self.GetSize() + 5);',
  14650. ' $Self.SetSize($Self.GetSize() + 6);',
  14651. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  14652. ' };',
  14653. ' Sub();',
  14654. ' $mod.TObject.State = $Self.State + 12;',
  14655. ' $mod.TObject.State = $Self.State + 13;',
  14656. ' $mod.TObject.State = $mod.TObject.State + 14;',
  14657. ' $Self.SetSize($Self.GetSize() + 15);',
  14658. ' $Self.SetSize($Self.GetSize() + 16);',
  14659. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  14660. ' };',
  14661. '});',
  14662. '']),
  14663. LinesToStr([ // $mod.$main
  14664. '']));
  14665. end;
  14666. procedure TTestModule.TestClass_NestedProcCallInherited;
  14667. begin
  14668. StartProgram(false);
  14669. Add([
  14670. 'type',
  14671. ' TObject = class',
  14672. ' function DoIt(k: boolean): longint; virtual;',
  14673. ' end;',
  14674. ' TBird = class',
  14675. ' function DoIt(k: boolean): longint; override;',
  14676. ' end;',
  14677. 'function tobject.doit(k: boolean): longint;',
  14678. 'begin',
  14679. 'end;',
  14680. 'function tbird.doit(k: boolean): longint;',
  14681. ' procedure Sub;',
  14682. ' begin',
  14683. ' inherited DoIt(true);',
  14684. //' if inherited DoIt(false)=4 then ;',
  14685. ' end;',
  14686. 'begin',
  14687. ' Sub;',
  14688. ' inherited;',
  14689. ' inherited DoIt(true);',
  14690. //' if inherited DoIt(false)=14 then ;',
  14691. 'end;',
  14692. 'begin',
  14693. '']);
  14694. ConvertProgram;
  14695. CheckSource('TestClass_NestedProcCallInherited',
  14696. LinesToStr([ // statements
  14697. 'rtl.createClass($mod, "TObject", null, function () {',
  14698. ' this.$init = function () {',
  14699. ' };',
  14700. ' this.$final = function () {',
  14701. ' };',
  14702. ' this.DoIt = function (k) {',
  14703. ' var Result = 0;',
  14704. ' return Result;',
  14705. ' };',
  14706. '});',
  14707. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14708. ' this.DoIt = function (k) {',
  14709. ' var $Self = this;',
  14710. ' var Result = 0;',
  14711. ' function Sub() {',
  14712. ' $mod.TObject.DoIt.call($Self, true);',
  14713. ' };',
  14714. ' Sub();',
  14715. ' $mod.TObject.DoIt.apply($Self, arguments);',
  14716. ' $mod.TObject.DoIt.call($Self, true);',
  14717. ' return Result;',
  14718. ' };',
  14719. '});',
  14720. '']),
  14721. LinesToStr([ // $mod.$main
  14722. '']));
  14723. end;
  14724. procedure TTestModule.TestClass_TObjectFree;
  14725. begin
  14726. StartProgram(false);
  14727. Add([
  14728. 'type',
  14729. ' TObject = class',
  14730. ' Obj: tobject;',
  14731. ' procedure Free;',
  14732. ' procedure Release;',
  14733. ' end;',
  14734. 'procedure tobject.free;',
  14735. 'begin',
  14736. 'end;',
  14737. 'procedure tobject.release;',
  14738. 'begin',
  14739. ' free;',
  14740. ' if true then free;',
  14741. 'end;',
  14742. 'function DoIt(o: tobject): tobject;',
  14743. 'var l: tobject;',
  14744. 'begin',
  14745. ' o.free;',
  14746. ' o.free();',
  14747. ' l.free;',
  14748. ' l.free();',
  14749. ' o.obj.free;',
  14750. ' o.obj.free();',
  14751. ' with o do obj.free;',
  14752. ' with o do obj.free();',
  14753. ' result.Free;',
  14754. ' result.Free();',
  14755. 'end;',
  14756. 'var o: tobject;',
  14757. ' a: array of tobject;',
  14758. 'begin',
  14759. ' o.free;',
  14760. ' o.obj.free;',
  14761. ' a[1+2].free;',
  14762. '']);
  14763. ConvertProgram;
  14764. CheckSource('TestClass_TObjectFree',
  14765. LinesToStr([ // statements
  14766. 'rtl.createClass($mod, "TObject", null, function () {',
  14767. ' this.$init = function () {',
  14768. ' this.Obj = null;',
  14769. ' };',
  14770. ' this.$final = function () {',
  14771. ' this.Obj = undefined;',
  14772. ' };',
  14773. ' this.Free = function () {',
  14774. ' };',
  14775. ' this.Release = function () {',
  14776. ' this.Free();',
  14777. ' if (true) this.Free();',
  14778. ' };',
  14779. '});',
  14780. 'this.DoIt = function (o) {',
  14781. ' var Result = null;',
  14782. ' var l = null;',
  14783. ' o = rtl.freeLoc(o);',
  14784. ' o = rtl.freeLoc(o);',
  14785. ' l = rtl.freeLoc(l);',
  14786. ' l = rtl.freeLoc(l);',
  14787. ' rtl.free(o, "Obj");',
  14788. ' rtl.free(o, "Obj");',
  14789. ' rtl.free(o, "Obj");',
  14790. ' rtl.free(o, "Obj");',
  14791. ' Result = rtl.freeLoc(Result);',
  14792. ' Result = rtl.freeLoc(Result);',
  14793. ' return Result;',
  14794. '};',
  14795. 'this.o = null;',
  14796. 'this.a = [];',
  14797. '']),
  14798. LinesToStr([ // $mod.$main
  14799. 'rtl.free($mod, "o");',
  14800. 'rtl.free($mod.o, "Obj");',
  14801. 'rtl.free($mod.a, 1 + 2);',
  14802. '']));
  14803. end;
  14804. procedure TTestModule.TestClass_TObjectFree_VarArg;
  14805. begin
  14806. StartProgram(false);
  14807. Add([
  14808. 'type',
  14809. ' TObject = class',
  14810. ' Obj: tobject;',
  14811. ' procedure Free;',
  14812. ' end;',
  14813. 'procedure tobject.free;',
  14814. 'begin',
  14815. 'end;',
  14816. 'procedure DoIt(var o: tobject);',
  14817. 'begin',
  14818. ' o.free;',
  14819. ' o.free();',
  14820. 'end;',
  14821. 'begin',
  14822. '']);
  14823. ConvertProgram;
  14824. CheckSource('TestClass_TObjectFree_VarArg',
  14825. LinesToStr([ // statements
  14826. 'rtl.createClass($mod, "TObject", null, function () {',
  14827. ' this.$init = function () {',
  14828. ' this.Obj = null;',
  14829. ' };',
  14830. ' this.$final = function () {',
  14831. ' this.Obj = undefined;',
  14832. ' };',
  14833. ' this.Free = function () {',
  14834. ' };',
  14835. '});',
  14836. 'this.DoIt = function (o) {',
  14837. ' o.set(rtl.freeLoc(o.get()));',
  14838. ' o.set(rtl.freeLoc(o.get()));',
  14839. '};',
  14840. '']),
  14841. LinesToStr([ // $mod.$main
  14842. '']));
  14843. end;
  14844. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  14845. begin
  14846. StartProgram(false);
  14847. Add([
  14848. 'type',
  14849. ' TObject = class',
  14850. ' constructor Create;',
  14851. ' procedure Free;',
  14852. ' end;',
  14853. 'constructor TObject.Create; begin end;',
  14854. 'procedure tobject.free; begin end;',
  14855. 'begin',
  14856. ' with tobject.create do free;',
  14857. '']);
  14858. ConvertProgram;
  14859. CheckSource('TestClass_TObjectFreeNewInstance',
  14860. LinesToStr([ // statements
  14861. 'rtl.createClass($mod, "TObject", null, function () {',
  14862. ' this.$init = function () {',
  14863. ' };',
  14864. ' this.$final = function () {',
  14865. ' };',
  14866. ' this.Create = function () {',
  14867. ' return this;',
  14868. ' };',
  14869. ' this.Free = function () {',
  14870. ' };',
  14871. '});',
  14872. '']),
  14873. LinesToStr([ // $mod.$main
  14874. 'var $with = $mod.TObject.$create("Create");',
  14875. '$with=rtl.freeLoc($with);',
  14876. '']));
  14877. end;
  14878. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  14879. begin
  14880. StartProgram(false);
  14881. Add([
  14882. 'type',
  14883. ' TObject = class',
  14884. ' destructor Destroy;',
  14885. ' procedure Free;',
  14886. ' end;',
  14887. 'destructor TObject.Destroy; begin end;',
  14888. 'procedure tobject.free; begin end;',
  14889. 'var o: tobject;',
  14890. 'begin',
  14891. ' o.free;',
  14892. '']);
  14893. Converter.UseLowerCase:=true;
  14894. ConvertProgram;
  14895. CheckSource('TestClass_TObjectFreeLowerCase',
  14896. LinesToStr([ // statements
  14897. 'rtl.createClass($mod, "tobject", null, function () {',
  14898. ' this.$init = function () {',
  14899. ' };',
  14900. ' this.$final = function () {',
  14901. ' };',
  14902. ' rtl.tObjectDestroy = "destroy";',
  14903. ' this.destroy = function () {',
  14904. ' };',
  14905. ' this.free = function () {',
  14906. ' };',
  14907. '});',
  14908. 'this.o = null;',
  14909. '']),
  14910. LinesToStr([ // $mod.$main
  14911. 'rtl.free($mod, "o");',
  14912. '']));
  14913. end;
  14914. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  14915. begin
  14916. StartProgram(false);
  14917. Add([
  14918. 'type',
  14919. ' TObject = class',
  14920. ' procedure Free;',
  14921. ' function GetObj: tobject; virtual; abstract;',
  14922. ' end;',
  14923. 'procedure tobject.free;',
  14924. 'begin',
  14925. 'end;',
  14926. 'var o: tobject;',
  14927. 'begin',
  14928. ' o.getobj.free;',
  14929. '']);
  14930. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14931. ConvertProgram;
  14932. end;
  14933. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  14934. begin
  14935. StartProgram(false);
  14936. Add([
  14937. 'type',
  14938. ' TObject = class',
  14939. ' procedure Free;',
  14940. ' FObj: TObject;',
  14941. ' property Obj: tobject read FObj write FObj;',
  14942. ' end;',
  14943. 'procedure tobject.free;',
  14944. 'begin',
  14945. 'end;',
  14946. 'var o: tobject;',
  14947. 'begin',
  14948. ' o.obj.free;',
  14949. '']);
  14950. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14951. ConvertProgram;
  14952. end;
  14953. procedure TTestModule.TestClass_ForIn;
  14954. begin
  14955. StartProgram(false);
  14956. Add([
  14957. 'type',
  14958. ' TObject = class end;',
  14959. ' TItem = TObject;',
  14960. ' TEnumerator = class',
  14961. ' FCurrent: TItem;',
  14962. ' property Current: TItem read FCurrent;',
  14963. ' function MoveNext: boolean;',
  14964. ' end;',
  14965. ' TBird = class',
  14966. ' function GetEnumerator: TEnumerator;',
  14967. ' end;',
  14968. 'function TEnumerator.MoveNext: boolean;',
  14969. 'begin',
  14970. 'end;',
  14971. 'function TBird.GetEnumerator: TEnumerator;',
  14972. 'begin',
  14973. 'end;',
  14974. 'var',
  14975. ' b: TBird;',
  14976. ' i, i2: TItem;',
  14977. 'begin',
  14978. ' for i in b do i2:=i;']);
  14979. ConvertProgram;
  14980. CheckSource('TestClass_ForIn',
  14981. LinesToStr([ // statements
  14982. 'rtl.createClass($mod, "TObject", null, function () {',
  14983. ' this.$init = function () {',
  14984. ' };',
  14985. ' this.$final = function () {',
  14986. ' };',
  14987. '});',
  14988. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  14989. ' this.$init = function () {',
  14990. ' $mod.TObject.$init.call(this);',
  14991. ' this.FCurrent = null;',
  14992. ' };',
  14993. ' this.$final = function () {',
  14994. ' this.FCurrent = undefined;',
  14995. ' $mod.TObject.$final.call(this);',
  14996. ' };',
  14997. ' this.MoveNext = function () {',
  14998. ' var Result = false;',
  14999. ' return Result;',
  15000. ' };',
  15001. '});',
  15002. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15003. ' this.GetEnumerator = function () {',
  15004. ' var Result = null;',
  15005. ' return Result;',
  15006. ' };',
  15007. '});',
  15008. 'this.b = null;',
  15009. 'this.i = null;',
  15010. 'this.i2 = null;'
  15011. ]),
  15012. LinesToStr([ // $mod.$main
  15013. 'var $in = $mod.b.GetEnumerator();',
  15014. 'try {',
  15015. ' while ($in.MoveNext()){',
  15016. ' $mod.i = $in.FCurrent;',
  15017. ' $mod.i2 = $mod.i;',
  15018. ' }',
  15019. '} finally {',
  15020. ' $in = rtl.freeLoc($in)',
  15021. '};',
  15022. '']));
  15023. end;
  15024. procedure TTestModule.TestClass_DispatchMessage;
  15025. begin
  15026. StartProgram(false);
  15027. Add([
  15028. 'type',
  15029. ' TObject = class',
  15030. ' {$DispatchField DispInt}',
  15031. ' procedure Dispatch(var Msg); virtual; abstract;',
  15032. ' {$DispatchStrField DispStr}',
  15033. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15034. ' end;',
  15035. ' THopMsg = record',
  15036. ' DispInt: longint;',
  15037. ' end;',
  15038. ' TPutMsg = record',
  15039. ' DispStr: string;',
  15040. ' end;',
  15041. ' TBird = class',
  15042. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15043. ' procedure Run; overload; virtual; abstract;',
  15044. ' procedure Run(var Msg); overload; message ''Fast'';',
  15045. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15046. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15047. ' end;',
  15048. 'procedure TBird.Run(var Msg);',
  15049. 'begin',
  15050. 'end;',
  15051. 'begin',
  15052. '']);
  15053. ConvertProgram;
  15054. CheckSource('TestClass_Message',
  15055. LinesToStr([ // statements
  15056. 'rtl.createClass($mod, "TObject", null, function () {',
  15057. ' this.$init = function () {',
  15058. ' };',
  15059. ' this.$final = function () {',
  15060. ' };',
  15061. '});',
  15062. 'rtl.recNewT($mod, "THopMsg", function () {',
  15063. ' this.DispInt = 0;',
  15064. ' this.$eq = function (b) {',
  15065. ' return this.DispInt === b.DispInt;',
  15066. ' };',
  15067. ' this.$assign = function (s) {',
  15068. ' this.DispInt = s.DispInt;',
  15069. ' return this;',
  15070. ' };',
  15071. '});',
  15072. 'rtl.recNewT($mod, "TPutMsg", function () {',
  15073. ' this.DispStr = "";',
  15074. ' this.$eq = function (b) {',
  15075. ' return this.DispStr === b.DispStr;',
  15076. ' };',
  15077. ' this.$assign = function (s) {',
  15078. ' this.DispStr = s.DispStr;',
  15079. ' return this;',
  15080. ' };',
  15081. '});',
  15082. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15083. ' this.Run$1 = function (Msg) {',
  15084. ' };',
  15085. ' this.$msgint = {',
  15086. ' "2": "Fly",',
  15087. ' "3": "Hop"',
  15088. ' };',
  15089. ' this.$msgstr = {',
  15090. ' Fast: "Run$1",',
  15091. ' foo: "Put"',
  15092. ' };',
  15093. '});',
  15094. '']),
  15095. LinesToStr([ // $mod.$main
  15096. '']));
  15097. end;
  15098. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15099. begin
  15100. StartProgram(false);
  15101. Add([
  15102. 'type',
  15103. ' TObject = class',
  15104. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15105. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15106. ' end;',
  15107. 'begin',
  15108. '']);
  15109. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15110. ConvertProgram;
  15111. end;
  15112. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15113. begin
  15114. StartProgram(false);
  15115. Add([
  15116. 'type',
  15117. ' TObject = class',
  15118. ' {$dispatchfield Msg}',
  15119. ' procedure Dispatch(var Msg); virtual; abstract;',
  15120. ' end;',
  15121. ' TFlyMsg = record',
  15122. ' FlyId: longint;',
  15123. ' end;',
  15124. ' TBird = class',
  15125. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15126. ' end;',
  15127. 'begin',
  15128. '']);
  15129. ConvertProgram;
  15130. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15131. end;
  15132. procedure TTestModule.TestClassOf_Create;
  15133. begin
  15134. StartProgram(false);
  15135. Add('type');
  15136. Add(' TObject = class');
  15137. Add(' constructor Create;');
  15138. Add(' end;');
  15139. Add(' TClass = class of TObject;');
  15140. Add('constructor tobject.create; begin end;');
  15141. Add('var');
  15142. Add(' Obj: tobject;');
  15143. Add(' C: tclass;');
  15144. Add('begin');
  15145. Add(' obj:=C.create;');
  15146. Add(' with c do obj:=create;');
  15147. ConvertProgram;
  15148. CheckSource('TestClassOf_Create',
  15149. LinesToStr([ // statements
  15150. 'rtl.createClass($mod, "TObject", null, function () {',
  15151. ' this.$init = function () {',
  15152. ' };',
  15153. ' this.$final = function () {',
  15154. ' };',
  15155. ' this.Create = function () {',
  15156. ' return this;',
  15157. ' };',
  15158. '});',
  15159. 'this.Obj = null;',
  15160. 'this.C = null;'
  15161. ]),
  15162. LinesToStr([ // $mod.$main
  15163. '$mod.Obj = $mod.C.$create("Create");',
  15164. 'var $with = $mod.C;',
  15165. '$mod.Obj = $with.$create("Create");',
  15166. '']));
  15167. end;
  15168. procedure TTestModule.TestClassOf_Call;
  15169. begin
  15170. StartProgram(false);
  15171. Add('type');
  15172. Add(' TObject = class');
  15173. Add(' class procedure DoIt;');
  15174. Add(' end;');
  15175. Add(' TClass = class of TObject;');
  15176. Add('class procedure tobject.doit; begin end;');
  15177. Add('var');
  15178. Add(' C: tclass;');
  15179. Add('begin');
  15180. Add(' c.doit;');
  15181. Add(' with c do doit;');
  15182. ConvertProgram;
  15183. CheckSource('TestClassOf_Call',
  15184. LinesToStr([ // statements
  15185. 'rtl.createClass($mod, "TObject", null, function () {',
  15186. ' this.$init = function () {',
  15187. ' };',
  15188. ' this.$final = function () {',
  15189. ' };',
  15190. ' this.DoIt = function () {',
  15191. ' };',
  15192. '});',
  15193. 'this.C = null;'
  15194. ]),
  15195. LinesToStr([ // $mod.$main
  15196. '$mod.C.DoIt();',
  15197. 'var $with = $mod.C;',
  15198. '$with.DoIt();',
  15199. '']));
  15200. end;
  15201. procedure TTestModule.TestClassOf_Assign;
  15202. begin
  15203. StartProgram(false);
  15204. Add('type');
  15205. Add(' TClass = class of TObject;');
  15206. Add(' TObject = class');
  15207. Add(' ClassType: TClass; ');
  15208. Add(' end;');
  15209. Add('var');
  15210. Add(' Obj: tobject;');
  15211. Add(' C: tclass;');
  15212. Add('begin');
  15213. Add(' c:=nil;');
  15214. Add(' c:=obj.classtype;');
  15215. ConvertProgram;
  15216. CheckSource('TestClassOf_Assign',
  15217. LinesToStr([ // statements
  15218. 'rtl.createClass($mod, "TObject", null, function () {',
  15219. ' this.$init = function () {',
  15220. ' this.ClassType = null;',
  15221. ' };',
  15222. ' this.$final = function () {',
  15223. ' this.ClassType = undefined;',
  15224. ' };',
  15225. '});',
  15226. 'this.Obj = null;',
  15227. 'this.C = null;'
  15228. ]),
  15229. LinesToStr([ // $mod.$main
  15230. '$mod.C = null;',
  15231. '$mod.C = $mod.Obj.ClassType;',
  15232. '']));
  15233. end;
  15234. procedure TTestModule.TestClassOf_Is;
  15235. begin
  15236. StartProgram(false);
  15237. Add('type');
  15238. Add(' TClass = class of TObject;');
  15239. Add(' TObject = class');
  15240. Add(' end;');
  15241. Add(' TCar = class');
  15242. Add(' end;');
  15243. Add(' TCars = class of TCar;');
  15244. Add('var');
  15245. Add(' Obj: tobject;');
  15246. Add(' C: tclass;');
  15247. Add(' Cars: tcars;');
  15248. Add('begin');
  15249. Add(' if c is tcar then ;');
  15250. Add(' if c is tcars then ;');
  15251. ConvertProgram;
  15252. CheckSource('TestClassOf_Is',
  15253. LinesToStr([ // statements
  15254. 'rtl.createClass($mod, "TObject", null, function () {',
  15255. ' this.$init = function () {',
  15256. ' };',
  15257. ' this.$final = function () {',
  15258. ' };',
  15259. '});',
  15260. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  15261. '});',
  15262. 'this.Obj = null;',
  15263. 'this.C = null;',
  15264. 'this.Cars = null;'
  15265. ]),
  15266. LinesToStr([ // $mod.$main
  15267. 'if(rtl.is($mod.C,$mod.TCar));',
  15268. 'if(rtl.is($mod.C,$mod.TCar));',
  15269. '']));
  15270. end;
  15271. procedure TTestModule.TestClassOf_Compare;
  15272. begin
  15273. StartProgram(false);
  15274. Add('type');
  15275. Add(' TClass = class of TObject;');
  15276. Add(' TObject = class');
  15277. Add(' ClassType: TClass; ');
  15278. Add(' end;');
  15279. Add('var');
  15280. Add(' b: boolean;');
  15281. Add(' Obj: tobject;');
  15282. Add(' C: tclass;');
  15283. Add('begin');
  15284. Add(' b:=c=nil;');
  15285. Add(' b:=nil=c;');
  15286. Add(' b:=c=obj.classtype;');
  15287. Add(' b:=obj.classtype=c;');
  15288. Add(' b:=c=TObject;');
  15289. Add(' b:=TObject=c;');
  15290. Add(' b:=c<>nil;');
  15291. Add(' b:=nil<>c;');
  15292. Add(' b:=c<>obj.classtype;');
  15293. Add(' b:=obj.classtype<>c;');
  15294. Add(' b:=c<>TObject;');
  15295. Add(' b:=TObject<>c;');
  15296. ConvertProgram;
  15297. CheckSource('TestClassOf_Compare',
  15298. LinesToStr([ // statements
  15299. 'rtl.createClass($mod, "TObject", null, function () {',
  15300. ' this.$init = function () {',
  15301. ' this.ClassType = null;',
  15302. ' };',
  15303. ' this.$final = function () {',
  15304. ' this.ClassType = undefined;',
  15305. ' };',
  15306. '});',
  15307. 'this.b = false;',
  15308. 'this.Obj = null;',
  15309. 'this.C = null;'
  15310. ]),
  15311. LinesToStr([ // $mod.$main
  15312. '$mod.b = $mod.C === null;',
  15313. '$mod.b = null === $mod.C;',
  15314. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15315. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15316. '$mod.b = $mod.C === $mod.TObject;',
  15317. '$mod.b = $mod.TObject === $mod.C;',
  15318. '$mod.b = $mod.C !== null;',
  15319. '$mod.b = null !== $mod.C;',
  15320. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15321. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15322. '$mod.b = $mod.C !== $mod.TObject;',
  15323. '$mod.b = $mod.TObject !== $mod.C;',
  15324. '']));
  15325. end;
  15326. procedure TTestModule.TestClassOf_ClassVar;
  15327. begin
  15328. StartProgram(false);
  15329. Add('type');
  15330. Add(' TObject = class');
  15331. Add(' class var id: longint;');
  15332. Add(' end;');
  15333. Add(' TClass = class of TObject;');
  15334. Add('var');
  15335. Add(' C: tclass;');
  15336. Add('begin');
  15337. Add(' C.id:=C.id;');
  15338. ConvertProgram;
  15339. CheckSource('TestClassOf_ClassVar',
  15340. LinesToStr([ // statements
  15341. 'rtl.createClass($mod, "TObject", null, function () {',
  15342. ' this.id = 0;',
  15343. ' this.$init = function () {',
  15344. ' };',
  15345. ' this.$final = function () {',
  15346. ' };',
  15347. '});',
  15348. 'this.C = null;'
  15349. ]),
  15350. LinesToStr([ // $mod.$main
  15351. '$mod.TObject.id = $mod.C.id;',
  15352. '']));
  15353. end;
  15354. procedure TTestModule.TestClassOf_ClassMethod;
  15355. begin
  15356. StartProgram(false);
  15357. Add('type');
  15358. Add(' TObject = class');
  15359. Add(' class function DoIt(i: longint = 0): longint;');
  15360. Add(' end;');
  15361. Add(' TClass = class of TObject;');
  15362. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15363. Add('var');
  15364. Add(' i: longint;');
  15365. Add(' C: tclass;');
  15366. Add('begin');
  15367. Add(' C.DoIt;');
  15368. Add(' C.DoIt();');
  15369. Add(' i:=C.DoIt;');
  15370. Add(' i:=C.DoIt();');
  15371. ConvertProgram;
  15372. CheckSource('TestClassOf_ClassMethod',
  15373. LinesToStr([ // statements
  15374. 'rtl.createClass($mod, "TObject", null, function () {',
  15375. ' this.$init = function () {',
  15376. ' };',
  15377. ' this.$final = function () {',
  15378. ' };',
  15379. ' this.DoIt = function (i) {',
  15380. ' var Result = 0;',
  15381. ' return Result;',
  15382. ' };',
  15383. '});',
  15384. 'this.i = 0;',
  15385. 'this.C = null;'
  15386. ]),
  15387. LinesToStr([ // $mod.$main
  15388. '$mod.C.DoIt(0);',
  15389. '$mod.C.DoIt(0);',
  15390. '$mod.i = $mod.C.DoIt(0);',
  15391. '$mod.i = $mod.C.DoIt(0);',
  15392. '']));
  15393. end;
  15394. procedure TTestModule.TestClassOf_ClassProperty;
  15395. begin
  15396. StartProgram(false);
  15397. Add([
  15398. 'type',
  15399. ' TObject = class',
  15400. ' class var FA: longint;',
  15401. ' class function GetA: longint;',
  15402. ' class procedure SetA(Value: longint);',
  15403. ' class property pA: longint read fa write fa;',
  15404. ' class property pB: longint read geta write seta;',
  15405. ' end;',
  15406. ' TObjectClass = class of tobject;',
  15407. 'class function tobject.geta: longint; begin end;',
  15408. 'class procedure tobject.seta(value: longint); begin end;',
  15409. 'var',
  15410. ' b: boolean;',
  15411. ' Obj: tobject;',
  15412. ' Cla: tobjectclass;',
  15413. 'begin',
  15414. ' obj.pa:=obj.pa;',
  15415. ' obj.pb:=obj.pb;',
  15416. ' b:=obj.pa=4;',
  15417. ' b:=obj.pb=obj.pb;',
  15418. ' b:=5=obj.pa;',
  15419. ' cla.pa:=6;',
  15420. ' cla.pa:=cla.pa;',
  15421. ' cla.pb:=cla.pb;',
  15422. ' b:=cla.pa=7;',
  15423. ' b:=cla.pb=cla.pb;',
  15424. ' b:=8=cla.pa;',
  15425. ' tobject.pa:=9;',
  15426. ' tobject.pb:=tobject.pb;',
  15427. ' b:=tobject.pa=10;',
  15428. ' b:=11=tobject.pa;',
  15429. '']);
  15430. ConvertProgram;
  15431. CheckSource('TestClassOf_ClassProperty',
  15432. LinesToStr([ // statements
  15433. 'rtl.createClass($mod, "TObject", null, function () {',
  15434. ' this.FA = 0;',
  15435. ' this.$init = function () {',
  15436. ' };',
  15437. ' this.$final = function () {',
  15438. ' };',
  15439. ' this.GetA = function () {',
  15440. ' var Result = 0;',
  15441. ' return Result;',
  15442. ' };',
  15443. ' this.SetA = function (Value) {',
  15444. ' };',
  15445. '});',
  15446. 'this.b = false;',
  15447. 'this.Obj = null;',
  15448. 'this.Cla = null;'
  15449. ]),
  15450. LinesToStr([ // $mod.$main
  15451. '$mod.TObject.FA = $mod.Obj.FA;',
  15452. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15453. '$mod.b = $mod.Obj.FA === 4;',
  15454. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15455. '$mod.b = 5 === $mod.Obj.FA;',
  15456. '$mod.TObject.FA = 6;',
  15457. '$mod.TObject.FA = $mod.Cla.FA;',
  15458. '$mod.Cla.SetA($mod.Cla.GetA());',
  15459. '$mod.b = $mod.Cla.FA === 7;',
  15460. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15461. '$mod.b = 8 === $mod.Cla.FA;',
  15462. '$mod.TObject.FA = 9;',
  15463. '$mod.TObject.SetA($mod.TObject.GetA());',
  15464. '$mod.b = $mod.TObject.FA === 10;',
  15465. '$mod.b = 11 === $mod.TObject.FA;',
  15466. '']));
  15467. end;
  15468. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15469. begin
  15470. StartProgram(false);
  15471. Add('type');
  15472. Add(' TObject = class');
  15473. Add(' class var GlobalId: longint;');
  15474. Add(' class procedure ProcA;');
  15475. Add(' end;');
  15476. Add('class procedure tobject.proca;');
  15477. Add('var b: boolean;');
  15478. Add('begin');
  15479. Add(' b:=self=nil;');
  15480. Add(' b:=self.globalid=3;');
  15481. Add(' b:=4=self.globalid;');
  15482. Add(' self.globalid:=5;');
  15483. Add(' self.proca;');
  15484. Add('end;');
  15485. Add('begin');
  15486. ConvertProgram;
  15487. CheckSource('TestClassOf_ClassMethodSelf',
  15488. LinesToStr([ // statements
  15489. 'rtl.createClass($mod, "TObject", null, function () {',
  15490. ' this.GlobalId = 0;',
  15491. ' this.$init = function () {',
  15492. ' };',
  15493. ' this.$final = function () {',
  15494. ' };',
  15495. ' this.ProcA = function () {',
  15496. ' var b = false;',
  15497. ' b = this === null;',
  15498. ' b = this.GlobalId === 3;',
  15499. ' b = 4 === this.GlobalId;',
  15500. ' $mod.TObject.GlobalId = 5;',
  15501. ' this.ProcA();',
  15502. ' };',
  15503. '});'
  15504. ]),
  15505. LinesToStr([ // $mod.$main
  15506. '']));
  15507. end;
  15508. procedure TTestModule.TestClassOf_TypeCast;
  15509. begin
  15510. StartProgram(false);
  15511. Add('type');
  15512. Add(' TObject = class');
  15513. Add(' class procedure {#TObject_DoIt}DoIt;');
  15514. Add(' end;');
  15515. Add(' TClass = class of TObject;');
  15516. Add(' TMobile = class');
  15517. Add(' class procedure {#TMobile_DoIt}DoIt;');
  15518. Add(' end;');
  15519. Add(' TMobileClass = class of TMobile;');
  15520. Add(' TCar = class(TMobile)');
  15521. Add(' class procedure {#TCar_DoIt}DoIt;');
  15522. Add(' end;');
  15523. Add(' TCarClass = class of TCar;');
  15524. Add('class procedure TObject.DoIt;');
  15525. Add('begin');
  15526. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15527. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15528. Add('end;');
  15529. Add('class procedure TMobile.DoIt;');
  15530. Add('begin');
  15531. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15532. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15533. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  15534. Add('end;');
  15535. Add('class procedure TCar.DoIt; begin end;');
  15536. Add('var');
  15537. Add(' ObjC: TClass;');
  15538. Add(' MobileC: TMobileClass;');
  15539. Add(' CarC: TCarClass;');
  15540. Add('begin');
  15541. Add(' ObjC.{@TObject_DoIt}DoIt;');
  15542. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  15543. Add(' CarC.{@TCar_DoIt}DoIt;');
  15544. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  15545. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  15546. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  15547. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  15548. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  15549. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  15550. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  15551. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  15552. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  15553. ConvertProgram;
  15554. CheckSource('TestClassOf_TypeCast',
  15555. LinesToStr([ // statements
  15556. 'rtl.createClass($mod, "TObject", null, function () {',
  15557. ' this.$init = function () {',
  15558. ' };',
  15559. ' this.$final = function () {',
  15560. ' };',
  15561. ' this.DoIt = function () {',
  15562. ' this.DoIt();',
  15563. ' this.DoIt$1();',
  15564. ' };',
  15565. '});',
  15566. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  15567. ' this.DoIt$1 = function () {',
  15568. ' this.DoIt();',
  15569. ' this.DoIt$1();',
  15570. ' this.DoIt$2();',
  15571. ' };',
  15572. '});',
  15573. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  15574. ' this.DoIt$2 = function () {',
  15575. ' };',
  15576. '});',
  15577. 'this.ObjC = null;',
  15578. 'this.MobileC = null;',
  15579. 'this.CarC = null;',
  15580. '']),
  15581. LinesToStr([ // $mod.$main
  15582. '$mod.ObjC.DoIt();',
  15583. '$mod.MobileC.DoIt$1();',
  15584. '$mod.CarC.DoIt$2();',
  15585. '$mod.ObjC.DoIt();',
  15586. '$mod.ObjC.DoIt$1();',
  15587. '$mod.ObjC.DoIt$2();',
  15588. '$mod.MobileC.DoIt();',
  15589. '$mod.MobileC.DoIt$1();',
  15590. '$mod.MobileC.DoIt$2();',
  15591. '$mod.CarC.DoIt();',
  15592. '$mod.CarC.DoIt$1();',
  15593. '$mod.CarC.DoIt$2();',
  15594. '']));
  15595. end;
  15596. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  15597. begin
  15598. StartProgram(false);
  15599. Add('type');
  15600. Add(' TObject = class');
  15601. Add(' function CurNow: longint; ');
  15602. Add(' class function Now: longint; ');
  15603. Add(' end;');
  15604. Add('function TObject.CurNow: longint; begin end;');
  15605. Add('class function TObject.Now: longint; begin end;');
  15606. Add('var');
  15607. Add(' Obj: tobject;');
  15608. Add(' vI: longint;');
  15609. Add('begin');
  15610. Add(' obj.curnow;');
  15611. Add(' vi:=obj.curnow;');
  15612. Add(' tobject.now;');
  15613. Add(' vi:=tobject.now;');
  15614. ConvertProgram;
  15615. CheckSource('TestClassOf_ImplicitFunctionCall',
  15616. LinesToStr([ // statements
  15617. 'rtl.createClass($mod, "TObject", null, function () {',
  15618. ' this.$init = function () {',
  15619. ' };',
  15620. ' this.$final = function () {',
  15621. ' };',
  15622. ' this.CurNow = function () {',
  15623. ' var Result = 0;',
  15624. ' return Result;',
  15625. ' };',
  15626. ' this.Now = function () {',
  15627. ' var Result = 0;',
  15628. ' return Result;',
  15629. ' };',
  15630. '});',
  15631. 'this.Obj = null;',
  15632. 'this.vI = 0;',
  15633. '']),
  15634. LinesToStr([ // $mod.$main
  15635. '$mod.Obj.CurNow();',
  15636. '$mod.vI = $mod.Obj.CurNow();',
  15637. '$mod.TObject.Now();',
  15638. '$mod.vI = $mod.TObject.Now();',
  15639. '']));
  15640. end;
  15641. procedure TTestModule.TestClassOf_Const;
  15642. begin
  15643. StartProgram(false);
  15644. Add([
  15645. 'type',
  15646. ' TObject = class',
  15647. ' end;',
  15648. ' TBird = TObject;',
  15649. ' TBirds = class of TBird;',
  15650. ' TEagles = TBirds;',
  15651. ' THawk = class(TBird);',
  15652. 'const',
  15653. ' Hawk: TEagles = THawk;',
  15654. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  15655. ' TBird,',
  15656. ' THawk',
  15657. ' );',
  15658. 'begin']);
  15659. ConvertProgram;
  15660. CheckSource('TestClassOf_Const',
  15661. LinesToStr([ // statements
  15662. 'rtl.createClass($mod, "TObject", null, function () {',
  15663. ' this.$init = function () {',
  15664. ' };',
  15665. ' this.$final = function () {',
  15666. ' };',
  15667. '});',
  15668. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  15669. '});',
  15670. 'this.Hawk = $mod.THawk;',
  15671. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  15672. '']),
  15673. LinesToStr([ // $mod.$main
  15674. '']));
  15675. end;
  15676. procedure TTestModule.TestNestedClass_Alias;
  15677. begin
  15678. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15679. StartProgram(false);
  15680. Add([
  15681. 'type',
  15682. ' TObject = class',
  15683. ' type TNested = type longint;',
  15684. ' end;',
  15685. 'type TAlias = type tobject.tnested;',
  15686. 'var i: tobject.tnested = 3;',
  15687. 'var j: TAlias = 4;',
  15688. 'begin',
  15689. ' if typeinfo(TAlias)=nil then ;',
  15690. ' if typeinfo(tobject.tnested)=nil then ;',
  15691. '']);
  15692. ConvertProgram;
  15693. CheckSource('TestNestedClass_Alias',
  15694. LinesToStr([ // statements
  15695. 'rtl.createClass($mod, "TObject", null, function () {',
  15696. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  15697. ' this.$init = function () {',
  15698. ' };',
  15699. ' this.$final = function () {',
  15700. ' };',
  15701. '});',
  15702. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  15703. 'this.i = 3;',
  15704. 'this.j = 4;',
  15705. '']),
  15706. LinesToStr([ // $mod.$main
  15707. 'if ($mod.$rtti["TAlias"] === null) ;',
  15708. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  15709. '']));
  15710. end;
  15711. procedure TTestModule.TestNestedClass_Record;
  15712. begin
  15713. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15714. StartProgram(false);
  15715. Add([
  15716. 'type',
  15717. ' TObject = class',
  15718. ' type TPoint = record',
  15719. ' x,y: byte;',
  15720. ' end;',
  15721. ' procedure DoIt(t: TPoint);',
  15722. ' end;',
  15723. 'procedure tobject.DoIt(t: TPoint);',
  15724. 'var p: TPoint;',
  15725. 'begin',
  15726. ' t.x:=t.y;',
  15727. ' p:=t;',
  15728. 'end;',
  15729. 'var',
  15730. ' p: tobject.tpoint = (x:2; y:4);',
  15731. ' o: TObject;',
  15732. 'begin',
  15733. ' p:=p;',
  15734. ' o.doit(p);',
  15735. '']);
  15736. ConvertProgram;
  15737. CheckSource('TestNestedClass_Record',
  15738. LinesToStr([ // statements
  15739. 'rtl.createClass($mod, "TObject", null, function () {',
  15740. ' rtl.recNewT(this, "TPoint", function () {',
  15741. ' this.x = 0;',
  15742. ' this.y = 0;',
  15743. ' this.$eq = function (b) {',
  15744. ' return (this.x === b.x) && (this.y === b.y);',
  15745. ' };',
  15746. ' this.$assign = function (s) {',
  15747. ' this.x = s.x;',
  15748. ' this.y = s.y;',
  15749. ' return this;',
  15750. ' };',
  15751. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  15752. ' $r.addField("x", rtl.byte);',
  15753. ' $r.addField("y", rtl.byte);',
  15754. ' });',
  15755. ' this.$init = function () {',
  15756. ' };',
  15757. ' this.$final = function () {',
  15758. ' };',
  15759. ' this.DoIt = function (t) {',
  15760. ' var p = this.TPoint.$new();',
  15761. ' t.x = t.y;',
  15762. ' p.$assign(t);',
  15763. ' };',
  15764. '});',
  15765. 'this.p = $mod.TObject.TPoint.$clone({',
  15766. ' x: 2,',
  15767. ' y: 4',
  15768. '});',
  15769. 'this.o = null;',
  15770. '']),
  15771. LinesToStr([ // $mod.$main
  15772. '$mod.p.$assign($mod.p);',
  15773. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  15774. '']));
  15775. end;
  15776. procedure TTestModule.TestNestedClass_Class;
  15777. begin
  15778. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15779. StartProgram(false);
  15780. Add([
  15781. 'type',
  15782. ' TObject = class end;',
  15783. ' TBird = class',
  15784. ' type TLeg = class',
  15785. ' FId: longint;',
  15786. ' constructor Create;',
  15787. ' function Create(i: longint): TLeg;',
  15788. ' end;',
  15789. ' function DoIt(b: TBird): Tleg;',
  15790. ' end;',
  15791. 'constructor tbird.tleg.create;',
  15792. 'begin',
  15793. ' FId:=3;',
  15794. 'end;',
  15795. 'function tbird.tleg.Create(i: longint): TLeg;',
  15796. 'begin',
  15797. ' Create;',
  15798. ' Result:=TLeg.Create;',
  15799. ' Result:=TBird.TLeg.Create;',
  15800. ' Result:=Create(3);',
  15801. ' FId:=i;',
  15802. 'end;',
  15803. 'function tbird.DoIt(b: tbird): tleg;',
  15804. 'begin',
  15805. ' Result.Create;',
  15806. ' Result:=TLeg.Create;',
  15807. ' Result:=TBird.TLeg.Create;',
  15808. ' Result:=Result.Create(3);',
  15809. 'end;',
  15810. 'var',
  15811. ' b: Tbird.tleg;',
  15812. 'begin',
  15813. ' b.Create;',
  15814. ' b:=TBird.TLeg.Create;',
  15815. ' b:=b.Create(3);',
  15816. '']);
  15817. ConvertProgram;
  15818. CheckSource('TestNestedClass_Class',
  15819. LinesToStr([ // statements
  15820. 'rtl.createClass($mod, "TObject", null, function () {',
  15821. ' this.$init = function () {',
  15822. ' };',
  15823. ' this.$final = function () {',
  15824. ' };',
  15825. '});',
  15826. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15827. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  15828. ' this.$init = function () {',
  15829. ' $mod.TObject.$init.call(this);',
  15830. ' this.FId = 0;',
  15831. ' };',
  15832. ' this.Create = function () {',
  15833. ' this.FId = 3;',
  15834. ' return this;',
  15835. ' };',
  15836. ' this.Create$1 = function (i) {',
  15837. ' var Result = null;',
  15838. ' this.Create();',
  15839. ' Result = $mod.TBird.TLeg.$create("Create");',
  15840. ' Result = $mod.TBird.TLeg.$create("Create");',
  15841. ' Result = this.Create$1(3);',
  15842. ' this.FId = i;',
  15843. ' return Result;',
  15844. ' };',
  15845. ' });',
  15846. ' this.DoIt = function (b) {',
  15847. ' var Result = null;',
  15848. ' Result.Create();',
  15849. ' Result = this.TLeg.$create("Create");',
  15850. ' Result = $mod.TBird.TLeg.$create("Create");',
  15851. ' Result = Result.Create$1(3);',
  15852. ' return Result;',
  15853. ' };',
  15854. '});',
  15855. 'this.b = null;',
  15856. '']),
  15857. LinesToStr([ // $mod.$main
  15858. '$mod.b.Create();',
  15859. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  15860. '$mod.b = $mod.b.Create$1(3);',
  15861. '']));
  15862. end;
  15863. procedure TTestModule.TestExternalClass_Var;
  15864. begin
  15865. StartProgram(false);
  15866. Add([
  15867. '{$modeswitch externalclass}',
  15868. 'type',
  15869. ' TExtA = class external name ''ExtObj''',
  15870. ' Id: longint external name ''$Id'';',
  15871. ' B: longint;',
  15872. ' end;',
  15873. 'var Obj: TExtA;',
  15874. 'begin',
  15875. ' obj.id:=obj.id+1;',
  15876. ' obj.B:=obj.B+1;']);
  15877. ConvertProgram;
  15878. CheckSource('TestExternalClass_Var',
  15879. LinesToStr([ // statements
  15880. 'this.Obj = null;',
  15881. '']),
  15882. LinesToStr([ // $mod.$main
  15883. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  15884. '$mod.Obj.B = $mod.Obj.B + 1;',
  15885. '']));
  15886. end;
  15887. procedure TTestModule.TestExternalClass_Const;
  15888. begin
  15889. StartProgram(false);
  15890. Add([
  15891. '{$modeswitch externalclass}',
  15892. 'type',
  15893. ' TExtA = class external name ''ExtObj''',
  15894. ' const Two: longint = 2;',
  15895. ' const Three = 3;',
  15896. ' const Id: longint;',
  15897. ' end;',
  15898. ' TExtB = class external name ''ExtB''',
  15899. ' A: TExtA;',
  15900. ' end;',
  15901. 'var',
  15902. ' A: texta;',
  15903. ' B: textb;',
  15904. ' i: longint;',
  15905. 'begin',
  15906. ' i:=a.two;',
  15907. ' i:=texta.two;',
  15908. ' i:=a.three;',
  15909. ' i:=texta.three;',
  15910. ' i:=a.id;',
  15911. ' i:=texta.id;',
  15912. '']);
  15913. ConvertProgram;
  15914. CheckSource('TestExternalClass_Const',
  15915. LinesToStr([ // statements
  15916. 'this.A = null;',
  15917. 'this.B = null;',
  15918. 'this.i = 0;',
  15919. '']),
  15920. LinesToStr([ // $mod.$main
  15921. '$mod.i = 2;',
  15922. '$mod.i = 2;',
  15923. '$mod.i = 3;',
  15924. '$mod.i = 3;',
  15925. '$mod.i = $mod.A.Id;',
  15926. '$mod.i = ExtObj.Id;',
  15927. '']));
  15928. end;
  15929. procedure TTestModule.TestExternalClass_Dollar;
  15930. begin
  15931. StartProgram(false);
  15932. Add([
  15933. '{$modeswitch externalclass}',
  15934. 'type',
  15935. ' TExtA = class external name ''$''',
  15936. ' Id: longint external name ''$'';',
  15937. ' function Bla(i: longint): longint; external name ''$'';',
  15938. ' end;',
  15939. 'function dollar(k: longint): longint; external name ''$'';',
  15940. 'var Obj: TExtA;',
  15941. 'begin',
  15942. ' dollar(1);',
  15943. ' obj.id:=obj.id+2;',
  15944. ' obj.Bla(3);',
  15945. '']);
  15946. ConvertProgram;
  15947. CheckSource('TestExternalClass_Dollar',
  15948. LinesToStr([ // statements
  15949. 'this.Obj = null;',
  15950. '']),
  15951. LinesToStr([ // $mod.$main
  15952. '$(1);',
  15953. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  15954. '$mod.Obj.$(3);',
  15955. '']));
  15956. end;
  15957. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  15958. begin
  15959. StartProgram(false);
  15960. Add('{$modeswitch externalclass}');
  15961. Add('type');
  15962. Add(' TExtA = class external name ''ExtA''');
  15963. Add(' Id: longint external name ''$Id'';');
  15964. Add(' end;');
  15965. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  15966. Add(' Id: longint;');
  15967. Add(' end;');
  15968. Add('begin');
  15969. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  15970. ConvertProgram;
  15971. end;
  15972. procedure TTestModule.TestExternalClass_Method;
  15973. begin
  15974. StartProgram(false);
  15975. Add(['{$modeswitch externalclass}',
  15976. 'type',
  15977. ' TExtA = class external name ''ExtObj''',
  15978. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15979. ' procedure DoSome(Id: longint = 1);',
  15980. ' end;',
  15981. 'var Obj: texta;',
  15982. 'begin',
  15983. ' obj.doit;',
  15984. ' obj.doit();',
  15985. ' obj.doit(2);',
  15986. ' with obj do begin',
  15987. ' doit;',
  15988. ' doit();',
  15989. ' doit(3);',
  15990. ' end;']);
  15991. ConvertProgram;
  15992. CheckSource('TestExternalClass_Method',
  15993. LinesToStr([ // statements
  15994. 'this.Obj = null;',
  15995. '']),
  15996. LinesToStr([ // $mod.$main
  15997. '$mod.Obj.$Execute(1);',
  15998. '$mod.Obj.$Execute(1);',
  15999. '$mod.Obj.$Execute(2);',
  16000. 'var $with = $mod.Obj;',
  16001. '$with.$Execute(1);',
  16002. '$with.$Execute(1);',
  16003. '$with.$Execute(3);',
  16004. '']));
  16005. end;
  16006. procedure TTestModule.TestExternalClass_ClassMethod;
  16007. begin
  16008. StartProgram(false);
  16009. Add([
  16010. '{$modeswitch externalclass}',
  16011. 'type',
  16012. ' TExtA = class external name ''ExtObj''',
  16013. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16014. ' end;',
  16015. ' TExtB = TExtA;',
  16016. 'var p: Pointer;',
  16017. 'begin',
  16018. ' texta.doit;',
  16019. ' texta.doit();',
  16020. ' texta.doit(2);',
  16021. ' p:[email protected];',
  16022. ' with texta do begin',
  16023. ' doit;',
  16024. ' doit();',
  16025. ' doit(3);',
  16026. ' p:=@DoIt;',
  16027. ' end;',
  16028. ' textb.doit;',
  16029. ' textb.doit();',
  16030. ' textb.doit(4);',
  16031. ' with textb do begin',
  16032. ' doit;',
  16033. ' doit();',
  16034. ' doit(5);',
  16035. ' end;',
  16036. '']);
  16037. ConvertProgram;
  16038. CheckSource('TestExternalClass_ClassMethod',
  16039. LinesToStr([ // statements
  16040. 'this.p = null;',
  16041. '']),
  16042. LinesToStr([ // $mod.$main
  16043. 'ExtObj.$Execute(1);',
  16044. 'ExtObj.$Execute(1);',
  16045. 'ExtObj.$Execute(2);',
  16046. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16047. 'ExtObj.$Execute(1);',
  16048. 'ExtObj.$Execute(1);',
  16049. 'ExtObj.$Execute(3);',
  16050. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16051. 'ExtObj.$Execute(1);',
  16052. 'ExtObj.$Execute(1);',
  16053. 'ExtObj.$Execute(4);',
  16054. 'ExtObj.$Execute(1);',
  16055. 'ExtObj.$Execute(1);',
  16056. 'ExtObj.$Execute(5);',
  16057. '']));
  16058. end;
  16059. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16060. begin
  16061. StartProgram(false);
  16062. Add([
  16063. '{$modeswitch externalclass}',
  16064. 'type',
  16065. ' TExtA = class external name ''ExtObj''',
  16066. ' class procedure DoIt(Id: longint = 1); static;',
  16067. ' end;',
  16068. 'var p: Pointer;',
  16069. 'begin',
  16070. ' texta.doit;',
  16071. ' texta.doit();',
  16072. ' texta.doit(2);',
  16073. ' p:[email protected];',
  16074. ' with texta do begin',
  16075. ' doit;',
  16076. ' doit();',
  16077. ' doit(3);',
  16078. ' p:=@DoIt;',
  16079. ' end;',
  16080. '']);
  16081. ConvertProgram;
  16082. CheckSource('TestExternalClass_ClassMethodStatic',
  16083. LinesToStr([ // statements
  16084. 'this.p = null;',
  16085. '']),
  16086. LinesToStr([ // $mod.$main
  16087. 'ExtObj.DoIt(1);',
  16088. 'ExtObj.DoIt(1);',
  16089. 'ExtObj.DoIt(2);',
  16090. '$mod.p = ExtObj.DoIt;',
  16091. 'ExtObj.DoIt(1);',
  16092. 'ExtObj.DoIt(1);',
  16093. 'ExtObj.DoIt(3);',
  16094. '$mod.p = ExtObj.DoIt;',
  16095. '']));
  16096. end;
  16097. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16098. begin
  16099. StartProgram(false);
  16100. Add([
  16101. '{$modeswitch externalclass}',
  16102. 'type',
  16103. ' TBird = class external name ''Array''',
  16104. ' end;',
  16105. 'function GetPtr: Pointer;',
  16106. 'begin',
  16107. 'end;',
  16108. 'procedure Write(const p);',
  16109. 'begin',
  16110. 'end;',
  16111. 'procedure WriteLn; varargs;',
  16112. 'begin',
  16113. 'end;',
  16114. 'begin',
  16115. ' if TBird(GetPtr)=nil then ;',
  16116. ' Write(GetPtr);',
  16117. ' WriteLn(GetPtr);',
  16118. ' Write(TBird(GetPtr));',
  16119. ' WriteLn(TBird(GetPtr));',
  16120. '']);
  16121. ConvertProgram;
  16122. CheckSource('TestFunctionResultInTypeCast',
  16123. LinesToStr([ // statements
  16124. 'this.GetPtr = function () {',
  16125. ' var Result = null;',
  16126. ' return Result;',
  16127. '};',
  16128. 'this.Write = function (p) {',
  16129. '};',
  16130. 'this.WriteLn = function () {',
  16131. '};',
  16132. '']),
  16133. LinesToStr([
  16134. 'if ($mod.GetPtr() === null) ;',
  16135. '$mod.Write($mod.GetPtr());',
  16136. '$mod.WriteLn($mod.GetPtr());',
  16137. '$mod.Write($mod.GetPtr());',
  16138. '$mod.WriteLn($mod.GetPtr());',
  16139. '']));
  16140. end;
  16141. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16142. begin
  16143. StartProgram(false);
  16144. Add([
  16145. '{$modeswitch externalclass}',
  16146. 'type',
  16147. ' TExtA = class external name ''ExtObjA''',
  16148. ' procedure ProcA; virtual;',
  16149. ' procedure ProcB; virtual;',
  16150. ' end;',
  16151. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16152. ' end;',
  16153. ' TExtC = class (TExtB)',
  16154. ' procedure ProcA; override;',
  16155. ' end;',
  16156. 'procedure TExtC.ProcA;',
  16157. 'begin',
  16158. ' ProcA;',
  16159. ' Self.ProcA;',
  16160. ' ProcB;',
  16161. ' Self.ProcB;',
  16162. 'end;',
  16163. 'var',
  16164. ' A: texta;',
  16165. ' B: textb;',
  16166. ' C: textc;',
  16167. 'begin',
  16168. ' a.proca;',
  16169. ' b.proca;',
  16170. ' c.proca;']);
  16171. ConvertProgram;
  16172. CheckSource('TestExternalClass_NonExternalOverride',
  16173. LinesToStr([ // statements
  16174. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  16175. ' this.$init = function () {',
  16176. ' };',
  16177. ' this.$final = function () {',
  16178. ' };',
  16179. ' this.ProcA = function () {',
  16180. ' this.ProcA();',
  16181. ' this.ProcA();',
  16182. ' this.ProcB();',
  16183. ' this.ProcB();',
  16184. ' };',
  16185. '});',
  16186. 'this.A = null;',
  16187. 'this.B = null;',
  16188. 'this.C = null;',
  16189. '']),
  16190. LinesToStr([ // $mod.$main
  16191. '$mod.A.ProcA();',
  16192. '$mod.B.ProcA();',
  16193. '$mod.C.ProcA();',
  16194. '']));
  16195. end;
  16196. procedure TTestModule.TestExternalClass_OverloadHint;
  16197. begin
  16198. StartProgram(false);
  16199. Add([
  16200. '{$modeswitch externalclass}',
  16201. 'type',
  16202. ' TExtA = class external name ''ExtObjA''',
  16203. ' procedure DoIt;',
  16204. ' procedure DoIt(i: longint);',
  16205. ' end;',
  16206. 'begin',
  16207. '']);
  16208. ConvertProgram;
  16209. CheckResolverUnexpectedHints(true);
  16210. CheckSource('TestExternalClass_OverloadHint',
  16211. LinesToStr([ // statements
  16212. '']),
  16213. LinesToStr([ // $mod.$main
  16214. '']));
  16215. end;
  16216. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16217. begin
  16218. StartProgram(false);
  16219. Add([
  16220. '{$modeswitch externalclass}',
  16221. 'type',
  16222. ' JSwiper = class external name ''Swiper''',
  16223. ' constructor New;',
  16224. ' end;',
  16225. ' TObject = class',
  16226. ' private',
  16227. ' FSwiper: JSwiper;',
  16228. ' published',
  16229. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16230. ' end;',
  16231. 'begin',
  16232. ' JSwiper.new;',
  16233. '']);
  16234. ConvertProgram;
  16235. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16236. LinesToStr([ // statements
  16237. 'rtl.createClass($mod, "TObject", null, function () {',
  16238. ' this.$init = function () {',
  16239. ' this.FSwiper = null;',
  16240. ' };',
  16241. ' this.$final = function () {',
  16242. ' this.FSwiper = undefined;',
  16243. ' };',
  16244. ' var $r = this.$rtti;',
  16245. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16246. '});',
  16247. '']),
  16248. LinesToStr([ // $mod.$main
  16249. 'new Swiper();',
  16250. '']));
  16251. end;
  16252. procedure TTestModule.TestExternalClass_Property;
  16253. begin
  16254. StartProgram(false);
  16255. Add([
  16256. '{$modeswitch externalclass}',
  16257. 'type',
  16258. ' TExtA = class external name ''ExtA''',
  16259. ' function getYear: longint;',
  16260. ' procedure setYear(Value: longint);',
  16261. ' property Year: longint read getyear write setyear;',
  16262. ' end;',
  16263. ' TExtB = class (TExtA)',
  16264. ' procedure OtherSetYear(Value: longint);',
  16265. ' property year write othersetyear;',
  16266. ' end;',
  16267. 'procedure textb.othersetyear(value: longint);',
  16268. 'begin',
  16269. ' setYear(Value+4);',
  16270. 'end;',
  16271. 'var',
  16272. ' A: texta;',
  16273. ' B: textb;',
  16274. 'begin',
  16275. ' a.year:=a.year+1;',
  16276. ' b.year:=b.year+2;']);
  16277. ConvertProgram;
  16278. CheckSource('TestExternalClass_NonExternalOverride',
  16279. LinesToStr([ // statements
  16280. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16281. ' this.$init = function () {',
  16282. ' };',
  16283. ' this.$final = function () {',
  16284. ' };',
  16285. ' this.OtherSetYear = function (Value) {',
  16286. ' this.setYear(Value+4);',
  16287. ' };',
  16288. '});',
  16289. 'this.A = null;',
  16290. 'this.B = null;',
  16291. '']),
  16292. LinesToStr([ // $mod.$main
  16293. '$mod.A.setYear($mod.A.getYear()+1);',
  16294. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16295. '']));
  16296. end;
  16297. procedure TTestModule.TestExternalClass_PropertyDate;
  16298. begin
  16299. StartProgram(false);
  16300. Add([
  16301. '{$modeswitch externalclass}',
  16302. 'type',
  16303. ' TExtA = class external name ''ExtA''',
  16304. ' end;',
  16305. ' TExtB = class (TExtA)',
  16306. ' FDate: string;',
  16307. ' property Date: string read FDate write FDate;',
  16308. ' property ExtA: string read FDate write FDate;',
  16309. ' end;',
  16310. ' {$M+}',
  16311. ' TObject = class',
  16312. ' FDate: string;',
  16313. ' published',
  16314. ' property Date: string read FDate write FDate;',
  16315. ' property ExtA: string read FDate write FDate;',
  16316. ' end;',
  16317. 'var',
  16318. ' B: textb;',
  16319. ' o: TObject;',
  16320. 'begin',
  16321. ' b.date:=b.exta;',
  16322. ' o.date:=o.exta;']);
  16323. ConvertProgram;
  16324. CheckSource('TestExternalClass_PropertyDate',
  16325. LinesToStr([ // statements
  16326. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16327. ' this.$init = function () {',
  16328. ' this.FDate = "";',
  16329. ' };',
  16330. ' this.$final = function () {',
  16331. ' };',
  16332. '});',
  16333. 'rtl.createClass($mod, "TObject", null, function () {',
  16334. ' this.$init = function () {',
  16335. ' this.FDate = "";',
  16336. ' };',
  16337. ' this.$final = function () {',
  16338. ' };',
  16339. ' var $r = this.$rtti;',
  16340. ' $r.addField("FDate", rtl.string);',
  16341. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16342. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16343. '});',
  16344. 'this.B = null;',
  16345. 'this.o = null;',
  16346. '']),
  16347. LinesToStr([ // $mod.$main
  16348. '$mod.B.FDate = $mod.B.FDate;',
  16349. '$mod.o.FDate = $mod.o.FDate;',
  16350. '']));
  16351. end;
  16352. procedure TTestModule.TestExternalClass_ClassProperty;
  16353. begin
  16354. StartProgram(false);
  16355. Add('{$modeswitch externalclass}');
  16356. Add('type');
  16357. Add(' TExtA = class external name ''ExtA''');
  16358. Add(' class function getYear: longint;');
  16359. Add(' class procedure setYear(Value: longint);');
  16360. Add(' class property Year: longint read getyear write setyear;');
  16361. Add(' end;');
  16362. Add(' TExtB = class (TExtA)');
  16363. Add(' class function GetCentury: longint;');
  16364. Add(' class procedure SetCentury(Value: longint);');
  16365. Add(' class property Century: longint read getcentury write setcentury;');
  16366. Add(' end;');
  16367. Add('class function textb.getcentury: longint;');
  16368. Add('begin');
  16369. Add('end;');
  16370. Add('class procedure textb.setcentury(value: longint);');
  16371. Add('begin');
  16372. Add(' setyear(value+11);');
  16373. Add(' texta.year:=texta.year+12;');
  16374. Add(' year:=year+13;');
  16375. Add(' textb.century:=textb.century+14;');
  16376. Add(' century:=century+15;');
  16377. Add('end;');
  16378. Add('var');
  16379. Add(' A: texta;');
  16380. Add(' B: textb;');
  16381. Add('begin');
  16382. Add(' texta.year:=texta.year+1;');
  16383. Add(' textb.year:=textb.year+2;');
  16384. Add(' TextA.year:=TextA.year+3;');
  16385. Add(' b.year:=b.year+4;');
  16386. Add(' textb.century:=textb.century+5;');
  16387. Add(' b.century:=b.century+6;');
  16388. ConvertProgram;
  16389. CheckSource('TestExternalClass_ClassProperty',
  16390. LinesToStr([ // statements
  16391. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16392. ' this.$init = function () {',
  16393. ' };',
  16394. ' this.$final = function () {',
  16395. ' };',
  16396. ' this.GetCentury = function () {',
  16397. ' var Result = 0;',
  16398. ' return Result;',
  16399. ' };',
  16400. ' this.SetCentury = function (Value) {',
  16401. ' this.setYear(Value + 11);',
  16402. ' ExtA.setYear(ExtA.getYear() + 12);',
  16403. ' this.setYear(this.getYear() + 13);',
  16404. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16405. ' this.SetCentury(this.GetCentury() + 15);',
  16406. ' };',
  16407. '});',
  16408. 'this.A = null;',
  16409. 'this.B = null;',
  16410. '']),
  16411. LinesToStr([ // $mod.$main
  16412. 'ExtA.setYear(ExtA.getYear() + 1);',
  16413. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16414. 'ExtA.setYear(ExtA.getYear() + 3);',
  16415. '$mod.B.setYear($mod.B.getYear() + 4);',
  16416. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16417. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16418. '']));
  16419. end;
  16420. procedure TTestModule.TestExternalClass_ClassOf;
  16421. begin
  16422. StartProgram(false);
  16423. Add('{$modeswitch externalclass}');
  16424. Add('type');
  16425. Add(' TExtA = class external name ''ExtA''');
  16426. Add(' procedure ProcA; virtual;');
  16427. Add(' procedure ProcB; virtual;');
  16428. Add(' end;');
  16429. Add(' TExtAClass = class of TExtA;');
  16430. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16431. Add(' end;');
  16432. Add(' TExtBClass = class of TExtB;');
  16433. Add(' TExtC = class (TExtB)');
  16434. Add(' procedure ProcA; override;');
  16435. Add(' end;');
  16436. Add(' TExtCClass = class of TExtC;');
  16437. Add('procedure TExtC.ProcA; begin end;');
  16438. Add('var');
  16439. Add(' A: texta; ClA: TExtAClass;');
  16440. Add(' B: textb; ClB: TExtBClass;');
  16441. Add(' C: textc; ClC: TExtCClass;');
  16442. Add('begin');
  16443. Add(' ClA:=texta;');
  16444. Add(' ClA:=textb;');
  16445. Add(' ClA:=textc;');
  16446. Add(' ClB:=textb;');
  16447. Add(' ClB:=textc;');
  16448. Add(' ClC:=textc;');
  16449. ConvertProgram;
  16450. CheckSource('TestExternalClass_ClassOf',
  16451. LinesToStr([ // statements
  16452. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16453. ' this.$init = function () {',
  16454. ' };',
  16455. ' this.$final = function () {',
  16456. ' };',
  16457. ' this.ProcA = function () {',
  16458. ' };',
  16459. '});',
  16460. 'this.A = null;',
  16461. 'this.ClA = null;',
  16462. 'this.B = null;',
  16463. 'this.ClB = null;',
  16464. 'this.C = null;',
  16465. 'this.ClC = null;',
  16466. '']),
  16467. LinesToStr([ // $mod.$main
  16468. '$mod.ClA = ExtA;',
  16469. '$mod.ClA = ExtB;',
  16470. '$mod.ClA = $mod.TExtC;',
  16471. '$mod.ClB = ExtB;',
  16472. '$mod.ClB = $mod.TExtC;',
  16473. '$mod.ClC = $mod.TExtC;',
  16474. '']));
  16475. end;
  16476. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16477. begin
  16478. AddModuleWithIntfImplSrc('unit2.pas',
  16479. LinesToStr([
  16480. '{$modeswitch externalclass}',
  16481. 'type',
  16482. ' TExtA = class external name ''ExtA''',
  16483. ' class var Id: longint;',
  16484. ' end;',
  16485. '']),
  16486. '');
  16487. StartUnit(true);
  16488. Add('interface');
  16489. Add('uses unit2;');
  16490. Add('implementation');
  16491. Add('begin');
  16492. Add(' unit2.texta.id:=unit2.texta.id+1;');
  16493. ConvertUnit;
  16494. CheckSource('TestExternalClass_ClassOtherUnit',
  16495. LinesToStr([
  16496. '']),
  16497. LinesToStr([
  16498. 'ExtA.Id = ExtA.Id + 1;',
  16499. '']));
  16500. end;
  16501. procedure TTestModule.TestExternalClass_Is;
  16502. begin
  16503. StartProgram(false);
  16504. Add([
  16505. '{$modeswitch externalclass}',
  16506. 'type',
  16507. ' TExtA = class external name ''ExtA''',
  16508. ' end;',
  16509. ' TExtAClass = class of TExtA;',
  16510. ' TExtB = class external name ''ExtB'' (TExtA)',
  16511. ' end;',
  16512. ' TExtBClass = class of TExtB;',
  16513. ' TExtC = class (TExtB)',
  16514. ' end;',
  16515. ' TExtCClass = class of TExtC;',
  16516. 'var',
  16517. ' A: texta; ClA: TExtAClass;',
  16518. ' B: textb; ClB: TExtBClass;',
  16519. ' C: textc; ClC: TExtCClass;',
  16520. 'begin',
  16521. ' if a is textb then ;',
  16522. ' if a is textc then ;',
  16523. ' if b is textc then ;',
  16524. ' if cla is textb then ;',
  16525. ' if cla is textc then ;',
  16526. ' if clb is textc then ;',
  16527. ' try',
  16528. ' except',
  16529. ' on TExtA do ;',
  16530. ' on e: TExtB do ;',
  16531. ' end;',
  16532. '']);
  16533. ConvertProgram;
  16534. CheckSource('TestExternalClass_Is',
  16535. LinesToStr([ // statements
  16536. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16537. ' this.$init = function () {',
  16538. ' };',
  16539. ' this.$final = function () {',
  16540. ' };',
  16541. '});',
  16542. 'this.A = null;',
  16543. 'this.ClA = null;',
  16544. 'this.B = null;',
  16545. 'this.ClB = null;',
  16546. 'this.C = null;',
  16547. 'this.ClC = null;',
  16548. '']),
  16549. LinesToStr([ // $mod.$main
  16550. 'if (rtl.isExt($mod.A, ExtB)) ;',
  16551. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  16552. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  16553. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  16554. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  16555. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  16556. 'try {} catch ($e) {',
  16557. ' if (rtl.isExt($e,ExtA)) {}',
  16558. ' else if (rtl.isExt($e,ExtB)) {',
  16559. ' var e = $e;',
  16560. ' } else throw $e',
  16561. '};',
  16562. '']));
  16563. end;
  16564. procedure TTestModule.TestExternalClass_As;
  16565. begin
  16566. StartProgram(false);
  16567. Add('{$modeswitch externalclass}');
  16568. Add('type');
  16569. Add(' TExtA = class external name ''ExtA''');
  16570. Add(' end;');
  16571. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16572. Add(' end;');
  16573. Add(' TExtC = class (TExtB)');
  16574. Add(' end;');
  16575. Add('var');
  16576. Add(' A: texta;');
  16577. Add(' B: textb;');
  16578. Add(' C: textc;');
  16579. Add('begin');
  16580. Add(' b:=a as textb;');
  16581. Add(' c:=a as textc;');
  16582. Add(' c:=b as textc;');
  16583. ConvertProgram;
  16584. CheckSource('TestExternalClass_Is',
  16585. LinesToStr([ // statements
  16586. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16587. ' this.$init = function () {',
  16588. ' };',
  16589. ' this.$final = function () {',
  16590. ' };',
  16591. '});',
  16592. 'this.A = null;',
  16593. 'this.B = null;',
  16594. 'this.C = null;',
  16595. '']),
  16596. LinesToStr([ // $mod.$main
  16597. '$mod.B = rtl.asExt($mod.A, ExtB);',
  16598. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  16599. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  16600. '']));
  16601. end;
  16602. procedure TTestModule.TestExternalClass_DestructorFail;
  16603. begin
  16604. StartProgram(false);
  16605. Add('{$modeswitch externalclass}');
  16606. Add('type');
  16607. Add(' TExtA = class external name ''ExtA''');
  16608. Add(' destructor Free;');
  16609. Add(' end;');
  16610. SetExpectedPasResolverError('Pascal element not supported: destructor',
  16611. nPasElementNotSupported);
  16612. ConvertProgram;
  16613. end;
  16614. procedure TTestModule.TestExternalClass_New;
  16615. begin
  16616. StartProgram(false);
  16617. Add([
  16618. '{$modeswitch externalclass}',
  16619. 'type',
  16620. ' TExtA = class external name ''ExtA''',
  16621. ' constructor New;',
  16622. ' constructor New(i: longint; j: longint = 2);',
  16623. ' end;',
  16624. 'var',
  16625. ' A: texta;',
  16626. 'begin',
  16627. ' a:=texta.new;',
  16628. ' a:=texta(texta.new);',
  16629. ' a:=texta.new();',
  16630. ' a:=texta.new(1);',
  16631. ' with texta do begin',
  16632. ' a:=new;',
  16633. ' a:=new();',
  16634. ' a:=new(2);',
  16635. ' end;',
  16636. ' a:=test1.texta.new;',
  16637. ' a:=test1.texta.new();',
  16638. ' a:=test1.texta.new(3);',
  16639. '']);
  16640. ConvertProgram;
  16641. CheckSource('TestExternalClass_New',
  16642. LinesToStr([ // statements
  16643. 'this.A = null;',
  16644. '']),
  16645. LinesToStr([ // $mod.$main
  16646. '$mod.A = new ExtA();',
  16647. '$mod.A = new ExtA();',
  16648. '$mod.A = new ExtA();',
  16649. '$mod.A = new ExtA(1,2);',
  16650. '$mod.A = new ExtA();',
  16651. '$mod.A = new ExtA();',
  16652. '$mod.A = new ExtA(2,2);',
  16653. '$mod.A = new ExtA();',
  16654. '$mod.A = new ExtA();',
  16655. '$mod.A = new ExtA(3,2);',
  16656. '']));
  16657. end;
  16658. procedure TTestModule.TestExternalClass_ClassOf_New;
  16659. begin
  16660. StartProgram(false);
  16661. Add('{$modeswitch externalclass}');
  16662. Add('type');
  16663. Add(' TExtAClass = class of TExtA;');
  16664. Add(' TExtA = class external name ''ExtA''');
  16665. Add(' C: TExtAClass;');
  16666. Add(' constructor New;');
  16667. Add(' end;');
  16668. Add('var');
  16669. Add(' A: texta;');
  16670. Add(' C: textaclass;');
  16671. Add('begin');
  16672. Add(' a:=c.new;');
  16673. Add(' a:=c.new();');
  16674. Add(' with C do begin');
  16675. Add(' a:=new;');
  16676. Add(' a:=new();');
  16677. Add(' end;');
  16678. Add(' a:=test1.c.new;');
  16679. Add(' a:=test1.c.new();');
  16680. Add(' a:=A.c.new();');
  16681. ConvertProgram;
  16682. CheckSource('TestExternalClass_ClassOf_New',
  16683. LinesToStr([ // statements
  16684. 'this.A = null;',
  16685. 'this.C = null;',
  16686. '']),
  16687. LinesToStr([ // $mod.$main
  16688. '$mod.A = new $mod.C();',
  16689. '$mod.A = new $mod.C();',
  16690. 'var $with = $mod.C;',
  16691. '$mod.A = new $with();',
  16692. '$mod.A = new $with();',
  16693. '$mod.A = new $mod.C();',
  16694. '$mod.A = new $mod.C();',
  16695. '$mod.A = new $mod.A.C();',
  16696. '']));
  16697. end;
  16698. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  16699. begin
  16700. StartProgram(false);
  16701. Add([
  16702. '{$modeswitch externalclass}',
  16703. 'type',
  16704. ' TExtAClass = class of TExtA;',
  16705. ' TExtA = class external name ''ExtA''',
  16706. ' constructor New;',
  16707. ' end;',
  16708. 'function GetCreator: TExtAClass;',
  16709. 'begin',
  16710. ' Result:=TExtA;',
  16711. 'end;',
  16712. 'var',
  16713. ' A: texta;',
  16714. 'begin',
  16715. ' a:=getcreator.new;',
  16716. ' a:=getcreator().new;',
  16717. ' a:=getcreator().new();',
  16718. ' a:=getcreator.new();',
  16719. ' with getcreator do begin',
  16720. ' a:=new;',
  16721. ' a:=new();',
  16722. ' end;']);
  16723. ConvertProgram;
  16724. CheckSource('TestExternalClass_FuncClassOf_New',
  16725. LinesToStr([ // statements
  16726. 'this.GetCreator = function () {',
  16727. ' var Result = null;',
  16728. ' Result = ExtA;',
  16729. ' return Result;',
  16730. '};',
  16731. 'this.A = null;',
  16732. '']),
  16733. LinesToStr([ // $mod.$main
  16734. '$mod.A = new ($mod.GetCreator())();',
  16735. '$mod.A = new ($mod.GetCreator())();',
  16736. '$mod.A = new ($mod.GetCreator())();',
  16737. '$mod.A = new ($mod.GetCreator())();',
  16738. 'var $with = $mod.GetCreator();',
  16739. '$mod.A = new $with();',
  16740. '$mod.A = new $with();',
  16741. '']));
  16742. end;
  16743. procedure TTestModule.TestExternalClass_New_PasClassFail;
  16744. begin
  16745. StartProgram(false);
  16746. Add([
  16747. '{$modeswitch externalclass}',
  16748. 'type',
  16749. ' TExtA = class external name ''ExtA''',
  16750. ' constructor New;',
  16751. ' end;',
  16752. ' TBird = class(TExtA)',
  16753. ' end;',
  16754. 'begin',
  16755. ' TBird.new;',
  16756. '']);
  16757. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16758. ConvertProgram;
  16759. end;
  16760. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  16761. begin
  16762. StartProgram(false);
  16763. Add([
  16764. '{$modeswitch externalclass}',
  16765. 'type',
  16766. ' TExtA = class external name ''ExtA''',
  16767. ' constructor New;',
  16768. ' end;',
  16769. ' TBird = class(TExtA)',
  16770. ' end;',
  16771. 'begin',
  16772. ' TBird.new();',
  16773. '']);
  16774. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16775. ConvertProgram;
  16776. end;
  16777. procedure TTestModule.TestExternalClass_NewExtName;
  16778. begin
  16779. StartProgram(false);
  16780. Add([
  16781. '{$modeswitch externalclass}',
  16782. 'type',
  16783. ' TExtA = class external name ''ExtA''',
  16784. ' constructor New; external name ''Other'';',
  16785. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  16786. ' end;',
  16787. 'var',
  16788. ' A: texta;',
  16789. 'begin',
  16790. ' a:=texta.new;',
  16791. ' a:=texta(texta.new);',
  16792. ' a:=texta.new();',
  16793. ' a:=texta.new(1);',
  16794. ' with texta do begin',
  16795. ' a:=new;',
  16796. ' a:=new();',
  16797. ' a:=new(2);',
  16798. ' end;',
  16799. ' a:=test1.texta.new;',
  16800. ' a:=test1.texta.new();',
  16801. ' a:=test1.texta.new(3);',
  16802. '']);
  16803. ConvertProgram;
  16804. CheckSource('TestExternalClass_NewExtName',
  16805. LinesToStr([ // statements
  16806. 'this.A = null;',
  16807. '']),
  16808. LinesToStr([ // $mod.$main
  16809. '$mod.A = new Other();',
  16810. '$mod.A = new Other();',
  16811. '$mod.A = new Other();',
  16812. '$mod.A = new A.B(1,2);',
  16813. '$mod.A = new Other();',
  16814. '$mod.A = new Other();',
  16815. '$mod.A = new A.B(2,2);',
  16816. '$mod.A = new Other();',
  16817. '$mod.A = new Other();',
  16818. '$mod.A = new A.B(3,2);',
  16819. '']));
  16820. end;
  16821. procedure TTestModule.TestExternalClass_Constructor;
  16822. begin
  16823. StartProgram(false);
  16824. Add([
  16825. '{$modeswitch externalclass}',
  16826. 'type',
  16827. ' TExtA = class external name ''ExtA''',
  16828. ' constructor Create;',
  16829. ' constructor Create(i: longint; j: longint = 2);',
  16830. ' end;',
  16831. 'var',
  16832. ' A: texta;',
  16833. 'begin',
  16834. ' a:=texta.create;',
  16835. ' a:=texta(texta.create);',
  16836. ' a:=texta.create();',
  16837. ' a:=texta.create(1);',
  16838. ' with texta do begin',
  16839. ' a:=create;',
  16840. ' a:=create();',
  16841. ' a:=create(2);',
  16842. ' end;',
  16843. ' a:=test1.texta.create;',
  16844. ' a:=test1.texta.create();',
  16845. ' a:=test1.texta.create(3);',
  16846. '']);
  16847. ConvertProgram;
  16848. CheckSource('TestExternalClass_Constructor',
  16849. LinesToStr([ // statements
  16850. 'this.A = null;',
  16851. '']),
  16852. LinesToStr([ // $mod.$main
  16853. '$mod.A = new ExtA.Create();',
  16854. '$mod.A = new ExtA.Create();',
  16855. '$mod.A = new ExtA.Create();',
  16856. '$mod.A = new ExtA.Create(1,2);',
  16857. '$mod.A = new ExtA.Create();',
  16858. '$mod.A = new ExtA.Create();',
  16859. '$mod.A = new ExtA.Create(2,2);',
  16860. '$mod.A = new ExtA.Create();',
  16861. '$mod.A = new ExtA.Create();',
  16862. '$mod.A = new ExtA.Create(3,2);',
  16863. '']));
  16864. end;
  16865. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  16866. begin
  16867. StartProgram(false);
  16868. Add([
  16869. '{$modeswitch externalclass}',
  16870. 'type',
  16871. ' TExtA = class external name ''ExtA''',
  16872. ' constructor Create; external name ''{}'';',
  16873. ' end;',
  16874. 'var',
  16875. ' A: texta;',
  16876. 'begin',
  16877. ' a:=texta.create;',
  16878. ' a:=texta(texta.create);',
  16879. ' a:=texta.create();',
  16880. ' with texta do begin',
  16881. ' a:=create;',
  16882. ' a:=create();',
  16883. ' end;',
  16884. ' a:=test1.texta.create;',
  16885. ' a:=test1.texta.create();',
  16886. '']);
  16887. ConvertProgram;
  16888. CheckSource('TestExternalClass_ConstructorBrackets',
  16889. LinesToStr([ // statements
  16890. 'this.A = null;',
  16891. '']),
  16892. LinesToStr([ // $mod.$main
  16893. '$mod.A = {};',
  16894. '$mod.A = {};',
  16895. '$mod.A = {};',
  16896. '$mod.A = {};',
  16897. '$mod.A = {};',
  16898. '$mod.A = {};',
  16899. '$mod.A = {};',
  16900. '']));
  16901. end;
  16902. procedure TTestModule.TestExternalClass_LocalConstSameName;
  16903. begin
  16904. StartProgram(false);
  16905. Add('{$modeswitch externalclass}');
  16906. Add('type');
  16907. Add(' TExtA = class external name ''ExtA''');
  16908. Add(' constructor New;');
  16909. Add(' end;');
  16910. Add('function DoIt: longint;');
  16911. Add('const ExtA: longint = 3;');
  16912. Add('begin');
  16913. Add(' Result:=ExtA;');
  16914. Add('end;');
  16915. Add('var');
  16916. Add(' A: texta;');
  16917. Add('begin');
  16918. Add(' a:=texta.new;');
  16919. ConvertProgram;
  16920. CheckSource('TestExternalClass_LocalConstSameName',
  16921. LinesToStr([ // statements
  16922. 'var ExtA$1 = 3;',
  16923. 'this.DoIt = function () {',
  16924. ' var Result = 0;',
  16925. ' Result = ExtA$1;',
  16926. ' return Result;',
  16927. '};',
  16928. 'this.A = null;',
  16929. '']),
  16930. LinesToStr([ // $mod.$main
  16931. '$mod.A = new ExtA();',
  16932. '']));
  16933. end;
  16934. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  16935. begin
  16936. StartProgram(false);
  16937. Add('{$modeswitch externalclass}');
  16938. Add('type');
  16939. Add(' TExtA = class external name ''ExtA''');
  16940. Add(' procedure DoIt;');
  16941. Add(' end;');
  16942. Add(' TMyA = class(TExtA)');
  16943. Add(' procedure DoIt;');
  16944. Add(' end;');
  16945. Add('procedure TMyA.DoIt; begin end;');
  16946. Add('begin');
  16947. ConvertProgram;
  16948. CheckSource('TestExternalClass_ReintroduceOverload',
  16949. LinesToStr([ // statements
  16950. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  16951. ' this.$init = function () {',
  16952. ' };',
  16953. ' this.$final = function () {',
  16954. ' };',
  16955. ' this.DoIt$1 = function () {',
  16956. ' };',
  16957. '});',
  16958. '']),
  16959. LinesToStr([ // $mod.$main
  16960. '']));
  16961. end;
  16962. procedure TTestModule.TestExternalClass_Inherited;
  16963. begin
  16964. StartProgram(false);
  16965. Add('{$modeswitch externalclass}');
  16966. Add('type');
  16967. Add(' TExtA = class external name ''ExtA''');
  16968. Add(' procedure DoIt(i: longint = 1); virtual;');
  16969. Add(' procedure DoSome(j: longint = 2);');
  16970. Add(' end;');
  16971. Add(' TExtB = class external name ''ExtB''(TExtA)');
  16972. Add(' end;');
  16973. Add(' TMyC = class(TExtB)');
  16974. Add(' procedure DoIt(i: longint = 1); override;');
  16975. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  16976. Add(' end;');
  16977. Add('procedure TMyC.DoIt(i: longint);');
  16978. Add('begin');
  16979. Add(' inherited;');
  16980. Add(' inherited DoIt;');
  16981. Add(' inherited DoIt();');
  16982. Add(' inherited DoIt(3);');
  16983. Add(' inherited DoSome;');
  16984. Add(' inherited DoSome();');
  16985. Add(' inherited DoSome(4);');
  16986. Add('end;');
  16987. Add('procedure TMyC.DoSome(j: longint);');
  16988. Add('begin');
  16989. Add(' inherited;');
  16990. Add('end;');
  16991. Add('begin');
  16992. ConvertProgram;
  16993. CheckSource('TestExternalClass_ReintroduceOverload',
  16994. LinesToStr([ // statements
  16995. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  16996. ' this.$init = function () {',
  16997. ' };',
  16998. ' this.$final = function () {',
  16999. ' };',
  17000. ' this.DoIt = function (i) {',
  17001. ' ExtB.DoIt.apply(this, arguments);',
  17002. ' ExtB.DoIt.call(this, 1);',
  17003. ' ExtB.DoIt.call(this, 1);',
  17004. ' ExtB.DoIt.call(this, 3);',
  17005. ' ExtB.DoSome.call(this, 2);',
  17006. ' ExtB.DoSome.call(this, 2);',
  17007. ' ExtB.DoSome.call(this, 4);',
  17008. ' };',
  17009. ' this.DoSome$1 = function (j) {',
  17010. ' ExtB.DoSome.apply(this, arguments);',
  17011. ' };',
  17012. '});',
  17013. '']),
  17014. LinesToStr([ // $mod.$main
  17015. '']));
  17016. end;
  17017. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17018. begin
  17019. StartProgram(false);
  17020. Add('{$modeswitch externalclass}');
  17021. Add('type');
  17022. Add(' TObject = class');
  17023. Add(' end;');
  17024. Add(' TExtA = class external name ''ExtA''(TObject)');
  17025. Add(' end;');
  17026. Add('begin');
  17027. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17028. ConvertProgram;
  17029. end;
  17030. procedure TTestModule.TestExternalClass_NewInstance;
  17031. begin
  17032. StartProgram(false);
  17033. Add('{$modeswitch externalclass}');
  17034. Add('type');
  17035. Add(' TExtA = class external name ''ExtA''');
  17036. Add(' end;');
  17037. Add(' TMyB = class(TExtA)');
  17038. Add(' protected');
  17039. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17040. Add(' end;');
  17041. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17042. Add('begin end;');
  17043. Add('begin');
  17044. ConvertProgram;
  17045. CheckSource('TestExternalClass_NewInstance',
  17046. LinesToStr([ // statements
  17047. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  17048. ' this.$init = function () {',
  17049. ' };',
  17050. ' this.$final = function () {',
  17051. ' };',
  17052. ' this.NewInstance = function (fnname, paramarray) {',
  17053. ' var Result = null;',
  17054. ' return Result;',
  17055. ' };',
  17056. '});',
  17057. '']),
  17058. LinesToStr([ // $mod.$main
  17059. '']));
  17060. end;
  17061. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17062. begin
  17063. StartProgram(false);
  17064. Add('{$modeswitch externalclass}');
  17065. Add('type');
  17066. Add(' TExtA = class external name ''ExtA''');
  17067. Add(' end;');
  17068. Add(' TMyB = class(TExtA)');
  17069. Add(' protected');
  17070. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17071. Add(' end;');
  17072. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17073. Add('begin end;');
  17074. Add('begin');
  17075. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17076. ConvertProgram;
  17077. end;
  17078. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17079. begin
  17080. StartProgram(false);
  17081. Add('{$modeswitch externalclass}');
  17082. Add('type');
  17083. Add(' TExtA = class external name ''ExtA''');
  17084. Add(' end;');
  17085. Add(' TMyB = class(TExtA)');
  17086. Add(' protected');
  17087. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17088. Add(' end;');
  17089. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17090. Add('begin end;');
  17091. Add('begin');
  17092. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17093. nIncompatibleTypeArgNo);
  17094. ConvertProgram;
  17095. end;
  17096. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17097. begin
  17098. StartProgram(false);
  17099. Add('{$modeswitch externalclass}');
  17100. Add('type');
  17101. Add(' TExtA = class external name ''ExtA''');
  17102. Add(' end;');
  17103. Add(' TMyB = class(TExtA)');
  17104. Add(' protected');
  17105. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17106. Add(' end;');
  17107. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17108. Add('begin end;');
  17109. Add('begin');
  17110. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17111. nIncompatibleTypeArgNo);
  17112. ConvertProgram;
  17113. end;
  17114. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17115. begin
  17116. StartProgram(false);
  17117. Add([
  17118. '{$modeswitch externalclass}',
  17119. 'type',
  17120. ' TJSFunction = class external name ''Function''',
  17121. ' end;',
  17122. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17123. ' constructor New(w: word);',
  17124. ' end;',
  17125. ' TBird = class (TExtA)',
  17126. ' public',
  17127. ' Size: word;',
  17128. ' class var Legs: word;',
  17129. ' constructor Create(a: word);',
  17130. ' end;',
  17131. ' TEagle = class (TBird)',
  17132. ' public',
  17133. ' constructor Create(b: word); reintroduce;',
  17134. ' end;',
  17135. 'constructor TBird.Create(a: word);',
  17136. 'begin',
  17137. ' inherited;', // silently ignored
  17138. ' inherited New(a);', // this.$func(a)
  17139. 'end;',
  17140. 'constructor TEagle.Create(b: word);',
  17141. 'begin',
  17142. ' inherited Create(b);',
  17143. 'end;',
  17144. 'var',
  17145. ' Bird: TBird;',
  17146. ' Eagle: TEagle;',
  17147. 'begin',
  17148. ' Bird:=TBird.Create(3);',
  17149. ' Eagle:=TEagle.Create(4);',
  17150. ' Bird.Size:=Bird.Size+5;',
  17151. ' Bird.Legs:=Bird.Legs+6;',
  17152. ' Eagle.Size:=Eagle.Size+5;',
  17153. ' Eagle.Legs:=Eagle.Legs+6;',
  17154. '']);
  17155. ConvertProgram;
  17156. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17157. LinesToStr([ // statements
  17158. 'rtl.createClassExt($mod, "TBird", ExtA, "", function () {',
  17159. ' this.Legs = 0;',
  17160. ' this.$init = function () {',
  17161. ' this.Size = 0;',
  17162. ' };',
  17163. ' this.$final = function () {',
  17164. ' };',
  17165. ' this.Create = function (a) {',
  17166. ' this.$ancestorfunc(a);',
  17167. ' return this;',
  17168. ' };',
  17169. '});',
  17170. 'rtl.createClassExt($mod, "TEagle", $mod.TBird, "", function () {',
  17171. ' this.Create$1 = function (b) {',
  17172. ' $mod.TBird.Create.call(this, b);',
  17173. ' return this;',
  17174. ' };',
  17175. '});',
  17176. 'this.Bird = null;',
  17177. 'this.Eagle = null;',
  17178. '']),
  17179. LinesToStr([ // $mod.$main
  17180. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17181. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17182. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17183. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17184. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17185. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17186. '']));
  17187. end;
  17188. procedure TTestModule.TestExternalClass_PascalProperty;
  17189. begin
  17190. StartProgram(false);
  17191. Add('{$modeswitch externalclass}');
  17192. Add('type');
  17193. Add(' TJSElement = class;');
  17194. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17195. Add(' TJSElement = class external name ''ExtA''');
  17196. Add(' end;');
  17197. Add(' TControl = class(TJSElement)');
  17198. Add(' private');
  17199. Add(' FOnClick: TJSNotifyEvent;');
  17200. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17201. Add(' procedure Click(Sender: TJSElement);');
  17202. Add(' end;');
  17203. Add('procedure TControl.Click(Sender: TJSElement);');
  17204. Add('begin');
  17205. Add(' OnClick(Self);');
  17206. Add('end;');
  17207. Add('var');
  17208. Add(' Ctrl: TControl;');
  17209. Add('begin');
  17210. Add(' Ctrl.OnClick:[email protected];');
  17211. Add(' Ctrl.OnClick(Ctrl);');
  17212. ConvertProgram;
  17213. CheckSource('TestExternalClass_PascalProperty',
  17214. LinesToStr([ // statements
  17215. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  17216. ' this.$init = function () {',
  17217. ' this.FOnClick = null;',
  17218. ' };',
  17219. ' this.$final = function () {',
  17220. ' this.FOnClick = undefined;',
  17221. ' };',
  17222. ' this.Click = function (Sender) {',
  17223. ' this.FOnClick(this);',
  17224. ' };',
  17225. '});',
  17226. 'this.Ctrl = null;',
  17227. '']),
  17228. LinesToStr([ // $mod.$main
  17229. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17230. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17231. '']));
  17232. end;
  17233. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17234. begin
  17235. StartProgram(false);
  17236. Add([
  17237. '{$modeswitch externalclass}',
  17238. 'type',
  17239. ' IUnknown = interface end;',
  17240. ' TObject = class',
  17241. ' end;',
  17242. ' TChild = class',
  17243. ' end;',
  17244. ' TExtRootA = class external name ''ExtRootA''',
  17245. ' end;',
  17246. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17247. ' end;',
  17248. ' TExtRootB = class external name ''ExtRootB''',
  17249. ' end;',
  17250. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17251. ' end;',
  17252. 'var',
  17253. ' Obj: TObject;',
  17254. ' Child: TChild;',
  17255. ' RootA: TExtRootA;',
  17256. ' ChildA: TExtChildA;',
  17257. ' RootB: TExtRootB;',
  17258. ' ChildB: TExtChildB;',
  17259. ' i: IUnknown;',
  17260. 'begin',
  17261. ' obj:=tobject(roota);',
  17262. ' obj:=tobject(childa);',
  17263. ' child:=tchild(tobject(roota));',
  17264. ' roota:=textroota(obj);',
  17265. ' roota:=textroota(child);',
  17266. ' roota:=textroota(rootb);',
  17267. ' roota:=textroota(childb);',
  17268. ' childa:=textchilda(textroota(obj));',
  17269. ' roota:=TExtRootA(i)',
  17270. '']);
  17271. ConvertProgram;
  17272. CheckSource('TestExternalClass_TypeCastToRootClass',
  17273. LinesToStr([ // statements
  17274. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17275. 'rtl.createClass($mod, "TObject", null, function () {',
  17276. ' this.$init = function () {',
  17277. ' };',
  17278. ' this.$final = function () {',
  17279. ' };',
  17280. '});',
  17281. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  17282. '});',
  17283. 'this.Obj = null;',
  17284. 'this.Child = null;',
  17285. 'this.RootA = null;',
  17286. 'this.ChildA = null;',
  17287. 'this.RootB = null;',
  17288. 'this.ChildB = null;',
  17289. 'this.i = null;',
  17290. '']),
  17291. LinesToStr([ // $mod.$main
  17292. '$mod.Obj = $mod.RootA;',
  17293. '$mod.Obj = $mod.ChildA;',
  17294. '$mod.Child = $mod.RootA;',
  17295. '$mod.RootA = $mod.Obj;',
  17296. '$mod.RootA = $mod.Child;',
  17297. '$mod.RootA = $mod.RootB;',
  17298. '$mod.RootA = $mod.ChildB;',
  17299. '$mod.ChildA = $mod.Obj;',
  17300. '$mod.RootA = $mod.i;',
  17301. '']));
  17302. end;
  17303. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17304. begin
  17305. StartProgram(false);
  17306. Add([
  17307. '{$modeswitch externalclass}',
  17308. 'type',
  17309. ' IUnknown = interface end;',
  17310. ' IBird = interface(IUnknown) end;',
  17311. ' TClass = class of TObject;',
  17312. ' TObject = class',
  17313. ' end;',
  17314. ' TChild = class',
  17315. ' end;',
  17316. ' TJSObject = class external name ''Object''',
  17317. ' end;',
  17318. ' TRec = record end;',
  17319. 'var',
  17320. ' Obj: TObject;',
  17321. ' Child: TChild;',
  17322. ' i: IUnknown;',
  17323. ' Bird: IBird;',
  17324. ' j: TJSObject;',
  17325. ' r: TRec;',
  17326. ' c: TClass;',
  17327. 'begin',
  17328. ' j:=tjsobject(IUnknown);',
  17329. ' j:=tjsobject(IBird);',
  17330. ' j:=tjsobject(TObject);',
  17331. ' j:=tjsobject(TChild);',
  17332. ' j:=tjsobject(TRec);',
  17333. ' j:=tjsobject(Obj);',
  17334. ' j:=tjsobject(Child);',
  17335. ' j:=tjsobject(i);',
  17336. ' j:=tjsobject(Bird);',
  17337. ' j:=tjsobject(r);',
  17338. ' j:=tjsobject(c);',
  17339. '']);
  17340. ConvertProgram;
  17341. CheckSource('TestExternalClass_TypeCastToJSObject',
  17342. LinesToStr([ // statements
  17343. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17344. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  17345. 'rtl.createClass($mod, "TObject", null, function () {',
  17346. ' this.$init = function () {',
  17347. ' };',
  17348. ' this.$final = function () {',
  17349. ' };',
  17350. '});',
  17351. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  17352. '});',
  17353. 'rtl.recNewT($mod, "TRec", function () {',
  17354. ' this.$eq = function (b) {',
  17355. ' return true;',
  17356. ' };',
  17357. ' this.$assign = function (s) {',
  17358. ' return this;',
  17359. ' };',
  17360. '});',
  17361. 'this.Obj = null;',
  17362. 'this.Child = null;',
  17363. 'this.i = null;',
  17364. 'this.Bird = null;',
  17365. 'this.j = null;',
  17366. 'this.r = $mod.TRec.$new();',
  17367. 'this.c = null;',
  17368. '']),
  17369. LinesToStr([ // $mod.$main
  17370. '$mod.j = $mod.IUnknown;',
  17371. '$mod.j = $mod.IBird;',
  17372. '$mod.j = $mod.TObject;',
  17373. '$mod.j = $mod.TChild;',
  17374. '$mod.j = $mod.TRec;',
  17375. '$mod.j = $mod.Obj;',
  17376. '$mod.j = $mod.Child;',
  17377. '$mod.j = $mod.i;',
  17378. '$mod.j = $mod.Bird;',
  17379. '$mod.j = $mod.r;',
  17380. '$mod.j = $mod.c;',
  17381. '']));
  17382. end;
  17383. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17384. begin
  17385. StartProgram(false);
  17386. Add('{$modeswitch externalclass}');
  17387. Add('type');
  17388. Add(' TJSString = class external name ''String''');
  17389. Add(' class function fromCharCode() : string; varargs;');
  17390. Add(' function anchor(const aName : string) : string;');
  17391. Add(' end;');
  17392. Add('var');
  17393. Add(' s: string;');
  17394. Add('begin');
  17395. Add(' s:=TJSString.fromCharCode(65,66);');
  17396. Add(' s:=TJSString(s).anchor(s);');
  17397. Add(' s:=TJSString(''foo'').anchor(s);');
  17398. ConvertProgram;
  17399. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17400. LinesToStr([ // statements
  17401. 'this.s = "";',
  17402. '']),
  17403. LinesToStr([ // $mod.$main
  17404. '$mod.s = String.fromCharCode(65, 66);',
  17405. '$mod.s = $mod.s.anchor($mod.s);',
  17406. '$mod.s = "foo".anchor($mod.s);',
  17407. '']));
  17408. end;
  17409. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17410. begin
  17411. StartProgram(false);
  17412. Add([
  17413. '{$modeswitch externalclass}',
  17414. 'type',
  17415. ' TJSObject = class external name ''Object'' end;',
  17416. ' TJSFunction = class external name ''Function''',
  17417. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17418. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17419. ' end;',
  17420. ' TObject = class',
  17421. ' procedure DoIt(i: longint);',
  17422. ' end;',
  17423. ' TFuncInt = function(o: TObject): longint;',
  17424. 'function GetIt(o: TObject): longint;',
  17425. ' procedure Sub; begin end;',
  17426. 'var',
  17427. ' f: TJSFunction;',
  17428. ' fi: TFuncInt;',
  17429. 'begin',
  17430. ' fi:=TFuncInt(f);',
  17431. ' f:=TJSFunction(fi);',
  17432. ' f:=TJSFunction(@GetIt);',
  17433. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  17434. ' f:=TJSFunction(@Sub);',
  17435. ' f:=TJSFunction(@o.doit);',
  17436. ' f:=TJSFunction(fi).bind(nil,4)',
  17437. 'end;',
  17438. 'procedure TObject.DoIt(i: longint);',
  17439. ' procedure Sub; begin end;',
  17440. 'var f: TJSFunction;',
  17441. 'begin',
  17442. ' f:=TJSFunction(@DoIt);',
  17443. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  17444. ' f:=TJSFunction(@Sub);',
  17445. ' f:=TJSFunction(@GetIt);',
  17446. 'end;',
  17447. 'begin']);
  17448. ConvertProgram;
  17449. CheckSource('TestExternalClass_TypeCastToJSFunction',
  17450. LinesToStr([ // statements
  17451. 'rtl.createClass($mod, "TObject", null, function () {',
  17452. ' this.$init = function () {',
  17453. ' };',
  17454. ' this.$final = function () {',
  17455. ' };',
  17456. ' this.DoIt = function (i) {',
  17457. ' var $Self = this;',
  17458. ' function Sub() {',
  17459. ' };',
  17460. ' var f = null;',
  17461. ' f = $Self.DoIt;',
  17462. ' f = $Self.DoIt.bind(null, 13);',
  17463. ' f = Sub;',
  17464. ' f = $mod.GetIt;',
  17465. ' };',
  17466. '});',
  17467. 'this.GetIt = function (o) {',
  17468. ' var Result = 0;',
  17469. ' function Sub() {',
  17470. ' };',
  17471. ' var f = null;',
  17472. ' var fi = null;',
  17473. ' fi = f;',
  17474. ' f = fi;',
  17475. ' f = $mod.GetIt;',
  17476. ' f = $mod.GetIt.bind(null, 3);',
  17477. ' f = Sub;',
  17478. ' f = $mod.TObject.DoIt;',
  17479. ' f = fi.bind(null, 4);',
  17480. ' return Result;',
  17481. '};',
  17482. '']),
  17483. LinesToStr([ // $mod.$main
  17484. '']));
  17485. end;
  17486. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  17487. begin
  17488. StartProgram(false);
  17489. Add([
  17490. '{$mode delphi}',
  17491. '{$modeswitch externalclass}',
  17492. 'type',
  17493. ' TJSObject = class external name ''Object'' end;',
  17494. ' TJSWindow = class external name ''Window''(TJSObject)',
  17495. ' procedure Open;',
  17496. ' end;',
  17497. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  17498. ' procedure Execute;',
  17499. ' end;',
  17500. 'procedure Fly;',
  17501. 'var',
  17502. ' w: TJSWindow;',
  17503. ' e: TJSEventTarget;',
  17504. 'begin',
  17505. ' w:=TJSWindow(e);',
  17506. ' e:=TJSEventTarget(w);',
  17507. 'end;',
  17508. 'begin']);
  17509. ConvertProgram;
  17510. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  17511. LinesToStr([ // statements
  17512. 'this.Fly = function () {',
  17513. ' var w = null;',
  17514. ' var e = null;',
  17515. ' w = e;',
  17516. ' e = w;',
  17517. '};',
  17518. '']),
  17519. LinesToStr([ // $mod.$main
  17520. '']));
  17521. end;
  17522. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  17523. begin
  17524. StartProgram(false);
  17525. Add('{$modeswitch externalclass}');
  17526. Add('type');
  17527. Add(' TJSString = class external name ''String''');
  17528. Add(' class function fromCharCode() : string; varargs;');
  17529. Add(' end;');
  17530. Add('var');
  17531. Add(' s: string;');
  17532. Add(' sObj: TJSString;');
  17533. Add('begin');
  17534. Add(' s:=sObj.fromCharCode(65,66);');
  17535. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  17536. nExternalClassInstanceCannotAccessStaticX);
  17537. ConvertProgram;
  17538. end;
  17539. procedure TTestModule.TestExternalClass_BracketAccessor;
  17540. begin
  17541. StartProgram(false);
  17542. Add([
  17543. '{$modeswitch externalclass}',
  17544. 'type',
  17545. ' TJSArray = class external name ''Array2''',
  17546. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17547. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17548. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17549. ' end;',
  17550. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  17551. 'begin end;',
  17552. 'var',
  17553. ' Arr: tjsarray;',
  17554. ' s: string;',
  17555. ' i: longint;',
  17556. ' v: jsvalue;',
  17557. 'begin',
  17558. ' v:=arr[0];',
  17559. ' v:=arr.items[1];',
  17560. ' arr[2]:=s;',
  17561. ' arr.items[3]:=s;',
  17562. ' arr[4]:=i;',
  17563. ' arr[5]:=arr[6];',
  17564. ' arr.items[7]:=arr.items[8];',
  17565. ' with arr do items[9]:=items[10];',
  17566. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  17567. ' with arr do begin',
  17568. ' v:=GetItems(14);',
  17569. ' setitems(15,16);',
  17570. ' end;',
  17571. ' v:=test1.arr.items[17];',
  17572. ' test1.arr.items[18]:=v;',
  17573. '']);
  17574. ConvertProgram;
  17575. CheckSource('TestExternalClass_BracketAccessor',
  17576. LinesToStr([ // statements
  17577. 'this.DoIt = function (vI, vJ, vK, vL) {',
  17578. '};',
  17579. 'this.Arr = null;',
  17580. 'this.s = "";',
  17581. 'this.i = 0;',
  17582. 'this.v = undefined;',
  17583. '']),
  17584. LinesToStr([ // $mod.$main
  17585. '$mod.v = $mod.Arr[0];',
  17586. '$mod.v = $mod.Arr[1];',
  17587. '$mod.Arr[2] = $mod.s;',
  17588. '$mod.Arr[3] = $mod.s;',
  17589. '$mod.Arr[4] = $mod.i;',
  17590. '$mod.Arr[5] = $mod.Arr[6];',
  17591. '$mod.Arr[7] = $mod.Arr[8];',
  17592. 'var $with = $mod.Arr;',
  17593. '$with[9] = $with[10];',
  17594. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  17595. ' a: 9,',
  17596. ' p: $mod.Arr,',
  17597. ' get: function () {',
  17598. ' return this.p[this.a];',
  17599. ' },',
  17600. ' set: function (v) {',
  17601. ' this.p[this.a] = v;',
  17602. ' }',
  17603. '}, {',
  17604. ' a: 10,',
  17605. ' p: $mod.Arr,',
  17606. ' get: function () {',
  17607. ' return this.p[this.a];',
  17608. ' },',
  17609. ' set: function (v) {',
  17610. ' this.p[this.a] = v;',
  17611. ' }',
  17612. '});',
  17613. 'var $with1 = $mod.Arr;',
  17614. '$mod.v = $with1[14];',
  17615. '$with1[15] = 16;',
  17616. '$mod.v = $mod.Arr[17];',
  17617. '$mod.Arr[18] = $mod.v;',
  17618. '']));
  17619. end;
  17620. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  17621. begin
  17622. StartProgram(false);
  17623. Add([
  17624. '{$modeswitch externalclass}',
  17625. 'type',
  17626. ' TJSArray = class external name ''Array2''',
  17627. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17628. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17629. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17630. ' end;',
  17631. ' TMyArr = class(TJSArray)',
  17632. ' procedure DoIt;',
  17633. ' end;',
  17634. 'procedure tmyarr.DoIt;',
  17635. 'begin',
  17636. ' Items[1]:=Items[2];',
  17637. ' SetItems(3,getItems(4));',
  17638. 'end;',
  17639. 'var',
  17640. ' Arr: tmyarr;',
  17641. ' s: string;',
  17642. ' i: longint;',
  17643. ' v: jsvalue;',
  17644. 'begin',
  17645. ' v:=arr[0];',
  17646. ' v:=arr.items[1];',
  17647. ' arr[2]:=s;',
  17648. ' arr.items[3]:=s;',
  17649. ' arr[4]:=i;',
  17650. ' arr[5]:=arr[6];',
  17651. ' arr.items[7]:=arr.items[8];',
  17652. ' with arr do items[9]:=items[10];',
  17653. ' with arr do begin',
  17654. ' v:=GetItems(14);',
  17655. ' setitems(15,16);',
  17656. ' end;',
  17657. '']);
  17658. ConvertProgram;
  17659. CheckSource('TestExternalClass_BracketAccessor_Call',
  17660. LinesToStr([ // statements
  17661. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  17662. ' this.$init = function () {',
  17663. ' };',
  17664. ' this.$final = function () {',
  17665. ' };',
  17666. ' this.DoIt = function () {',
  17667. ' this[1] = this[2];',
  17668. ' this[3] = this[4];',
  17669. ' };',
  17670. '});',
  17671. 'this.Arr = null;',
  17672. 'this.s = "";',
  17673. 'this.i = 0;',
  17674. 'this.v = undefined;',
  17675. '']),
  17676. LinesToStr([ // $mod.$main
  17677. '$mod.v = $mod.Arr[0];',
  17678. '$mod.v = $mod.Arr[1];',
  17679. '$mod.Arr[2] = $mod.s;',
  17680. '$mod.Arr[3] = $mod.s;',
  17681. '$mod.Arr[4] = $mod.i;',
  17682. '$mod.Arr[5] = $mod.Arr[6];',
  17683. '$mod.Arr[7] = $mod.Arr[8];',
  17684. 'var $with = $mod.Arr;',
  17685. '$with[9] = $with[10];',
  17686. 'var $with1 = $mod.Arr;',
  17687. '$mod.v = $with1[14];',
  17688. '$with1[15] = 16;',
  17689. '']));
  17690. end;
  17691. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  17692. begin
  17693. StartProgram(false);
  17694. Add('{$modeswitch externalclass}');
  17695. Add('type');
  17696. Add(' TJSArray = class external name ''Array2''');
  17697. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  17698. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  17699. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  17700. Add(' end;');
  17701. Add('begin');
  17702. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  17703. nBracketAccessorOfExternalClassMustHaveOneParameter);
  17704. ConvertProgram;
  17705. end;
  17706. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  17707. begin
  17708. StartProgram(false);
  17709. Add('{$modeswitch externalclass}');
  17710. Add('type');
  17711. Add(' TJSArray = class external name ''Array2''');
  17712. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17713. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  17714. Add(' end;');
  17715. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  17716. Add('begin end;');
  17717. Add('var');
  17718. Add(' Arr: tjsarray;');
  17719. Add(' v: jsvalue;');
  17720. Add('begin');
  17721. Add(' v:=arr[0];');
  17722. Add(' v:=arr.items[1];');
  17723. Add(' with arr do v:=items[2];');
  17724. Add(' doit(arr[3],arr[4]);');
  17725. ConvertProgram;
  17726. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  17727. LinesToStr([ // statements
  17728. 'this.DoIt = function (vI, vJ) {',
  17729. '};',
  17730. 'this.Arr = null;',
  17731. 'this.v = undefined;',
  17732. '']),
  17733. LinesToStr([ // $mod.$main
  17734. '$mod.v = $mod.Arr[0];',
  17735. '$mod.v = $mod.Arr[1];',
  17736. 'var $with = $mod.Arr;',
  17737. '$mod.v = $with[2];',
  17738. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  17739. '']));
  17740. end;
  17741. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  17742. begin
  17743. StartProgram(false);
  17744. Add('{$modeswitch externalclass}');
  17745. Add('type');
  17746. Add(' TJSArray = class external name ''Array2''');
  17747. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17748. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17749. Add(' end;');
  17750. Add('var');
  17751. Add(' Arr: tjsarray;');
  17752. Add(' s: string;');
  17753. Add(' i: longint;');
  17754. Add(' v: jsvalue;');
  17755. Add('begin');
  17756. Add(' arr[2]:=s;');
  17757. Add(' arr.items[3]:=s;');
  17758. Add(' arr[4]:=i;');
  17759. Add(' with arr do items[5]:=i;');
  17760. ConvertProgram;
  17761. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  17762. LinesToStr([ // statements
  17763. 'this.Arr = null;',
  17764. 'this.s = "";',
  17765. 'this.i = 0;',
  17766. 'this.v = undefined;',
  17767. '']),
  17768. LinesToStr([ // $mod.$main
  17769. '$mod.Arr[2] = $mod.s;',
  17770. '$mod.Arr[3] = $mod.s;',
  17771. '$mod.Arr[4] = $mod.i;',
  17772. 'var $with = $mod.Arr;',
  17773. '$with[5] = $mod.i;',
  17774. '']));
  17775. end;
  17776. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  17777. begin
  17778. StartProgram(false);
  17779. Add('{$modeswitch externalclass}');
  17780. Add('type');
  17781. Add(' TJSArray = class external name ''Array2''');
  17782. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17783. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17784. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  17785. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  17786. Add(' end;');
  17787. Add('var');
  17788. Add(' Arr: tjsarray;');
  17789. Add(' s: string;');
  17790. Add(' i: longint;');
  17791. Add(' v: jsvalue;');
  17792. Add('begin');
  17793. Add(' arr[2]:=s;');
  17794. Add(' arr.items[3]:=s;');
  17795. Add(' arr.numbers[4]:=i;');
  17796. Add(' with arr do items[5]:=i;');
  17797. Add(' with arr do numbers[6]:=i;');
  17798. ConvertProgram;
  17799. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  17800. LinesToStr([ // statements
  17801. 'this.Arr = null;',
  17802. 'this.s = "";',
  17803. 'this.i = 0;',
  17804. 'this.v = undefined;',
  17805. '']),
  17806. LinesToStr([ // $mod.$main
  17807. '$mod.Arr[2] = $mod.s;',
  17808. '$mod.Arr[3] = $mod.s;',
  17809. '$mod.Arr[4] = $mod.i;',
  17810. 'var $with = $mod.Arr;',
  17811. '$with[5] = $mod.i;',
  17812. 'var $with1 = $mod.Arr;',
  17813. '$with1[6] = $mod.i;',
  17814. '']));
  17815. end;
  17816. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  17817. begin
  17818. StartProgram(false);
  17819. Add('{$modeswitch externalclass}');
  17820. Add('type');
  17821. Add(' TJSArray = class external name ''Array2''');
  17822. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17823. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17824. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  17825. Add(' end;');
  17826. Add('var');
  17827. Add(' Arr: tjsarray;');
  17828. Add(' i: longint;');
  17829. Add(' IntArr: array of longint;');
  17830. Add(' v: jsvalue;');
  17831. Add('begin');
  17832. Add(' v:=arr.items[i];');
  17833. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  17834. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  17835. ConvertProgram;
  17836. CheckSource('TestExternalClass_BracketAccessor_Index',
  17837. LinesToStr([ // statements
  17838. 'this.Arr = null;',
  17839. 'this.i = 0;',
  17840. 'this.IntArr = [];',
  17841. 'this.v = undefined;',
  17842. '']),
  17843. LinesToStr([ // $mod.$main
  17844. '$mod.v = $mod.Arr[$mod.i];',
  17845. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  17846. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  17847. '']));
  17848. end;
  17849. procedure TTestModule.TestExternalClass_ForInJSObject;
  17850. begin
  17851. StartProgram(false);
  17852. Add([
  17853. '{$modeswitch externalclass}',
  17854. 'type',
  17855. ' TJSObject = class external name ''Object''',
  17856. ' end;',
  17857. 'var',
  17858. ' o: TJSObject;',
  17859. ' key: string;',
  17860. 'begin',
  17861. ' for key in o do',
  17862. ' if key=''abc'' then ;',
  17863. '']);
  17864. ConvertProgram;
  17865. CheckSource('TestExternalClass_ForInJSObject',
  17866. LinesToStr([ // statements
  17867. 'this.o = null;',
  17868. 'this.key = "";',
  17869. '']),
  17870. LinesToStr([ // $mod.$main
  17871. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  17872. '']));
  17873. end;
  17874. procedure TTestModule.TestExternalClass_ForInJSArray;
  17875. begin
  17876. StartProgram(false);
  17877. Add([
  17878. '{$modeswitch externalclass}',
  17879. 'type',
  17880. ' TJSInt8Array = class external name ''Int8Array''',
  17881. ' private',
  17882. ' flength: NativeInt external name ''length'';',
  17883. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  17884. ' public',
  17885. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  17886. ' property Length: NativeInt read flength;',
  17887. ' end;',
  17888. 'var',
  17889. ' a: TJSInt8Array;',
  17890. ' value: shortint;',
  17891. 'begin',
  17892. ' for value in a do',
  17893. ' if value=3 then ;',
  17894. '']);
  17895. ConvertProgram;
  17896. CheckSource('TestExternalClass_ForInJSArray',
  17897. LinesToStr([ // statements
  17898. 'this.a = null;',
  17899. 'this.value = 0;',
  17900. '']),
  17901. LinesToStr([ // $mod.$main
  17902. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  17903. ' $mod.value = $in[$l];',
  17904. ' if ($mod.value === 3) ;',
  17905. '};',
  17906. '']));
  17907. end;
  17908. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  17909. begin
  17910. AddModuleWithIntfImplSrc('unit2.pas',
  17911. LinesToStr([
  17912. '{$modeswitch externalclass}',
  17913. 'type',
  17914. ' TJSBufferSource = class external name ''BufferSource''',
  17915. ' end;',
  17916. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  17917. '']),
  17918. '');
  17919. AddModuleWithIntfImplSrc('unit3.pas',
  17920. LinesToStr([
  17921. '{$modeswitch externalclass}',
  17922. 'type',
  17923. ' TJSBufferSource = class external name ''BufferSource''',
  17924. ' end;',
  17925. '']),
  17926. '');
  17927. StartUnit(true);
  17928. Add([
  17929. 'interface',
  17930. 'uses unit2, unit3;',
  17931. 'procedure DoSome(s: TJSBufferSource);',
  17932. 'implementation',
  17933. 'procedure DoSome(s: TJSBufferSource);',
  17934. 'begin',
  17935. ' DoIt(s);',
  17936. 'end;',
  17937. '']);
  17938. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  17939. nIncompatibleTypeArgNo);
  17940. ConvertUnit;
  17941. end;
  17942. procedure TTestModule.TestClassInterface_Corba;
  17943. begin
  17944. StartProgram(false);
  17945. Add([
  17946. '{$interfaces corba}',
  17947. 'type',
  17948. ' IUnknown = interface;',
  17949. ' IUnknown = interface',
  17950. ' [''{00000000-0000-0000-C000-000000000046}'']',
  17951. ' end;',
  17952. ' IInterface = IUnknown;',
  17953. ' IBird = interface(IInterface)',
  17954. ' function GetSize: longint;',
  17955. ' procedure SetSize(i: longint);',
  17956. ' property Size: longint read GetSize write SetSize;',
  17957. ' procedure DoIt(i: longint);',
  17958. ' end;',
  17959. ' TObject = class',
  17960. ' end;',
  17961. ' TBird = class(TObject,IBird)',
  17962. ' function GetSize: longint; virtual; abstract;',
  17963. ' procedure SetSize(i: longint); virtual; abstract;',
  17964. ' procedure DoIt(i: longint); virtual; abstract;',
  17965. ' end;',
  17966. 'var',
  17967. ' BirdIntf: IBird;',
  17968. 'begin',
  17969. ' BirdIntf.Size:=BirdIntf.Size;',
  17970. '']);
  17971. ConvertProgram;
  17972. CheckSource('TestClassInterface_Corba',
  17973. LinesToStr([ // statements
  17974. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  17975. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  17976. 'rtl.createClass($mod, "TObject", null, function () {',
  17977. ' this.$init = function () {',
  17978. ' };',
  17979. ' this.$final = function () {',
  17980. ' };',
  17981. '});',
  17982. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17983. ' rtl.addIntf(this, $mod.IBird);',
  17984. '});',
  17985. 'this.BirdIntf = null;',
  17986. '']),
  17987. LinesToStr([ // $mod.$main
  17988. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  17989. '']));
  17990. end;
  17991. procedure TTestModule.TestClassInterface_ProcExternalFail;
  17992. begin
  17993. StartProgram(false);
  17994. Add([
  17995. '{$interfaces corba}',
  17996. 'type',
  17997. ' IUnknown = interface',
  17998. ' procedure DoIt; external name ''foo'';',
  17999. ' end;',
  18000. 'begin']);
  18001. SetExpectedParserError(
  18002. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18003. nParserNoFieldsAllowed);
  18004. ConvertProgram;
  18005. end;
  18006. procedure TTestModule.TestClassInterface_Overloads;
  18007. begin
  18008. StartProgram(false);
  18009. Add([
  18010. '{$interfaces corba}',
  18011. 'type',
  18012. ' integer = longint;',
  18013. ' IUnknown = interface',
  18014. ' procedure DoIt(i: integer);',
  18015. ' procedure DoIt(s: string);',
  18016. ' end;',
  18017. ' IBird = interface(IUnknown)',
  18018. ' procedure DoIt(b: boolean); overload;',
  18019. ' end;',
  18020. ' TObject = class',
  18021. ' end;',
  18022. ' TBird = class(TObject,IBird)',
  18023. ' procedure DoIt(o: TObject);',
  18024. ' procedure DoIt(s: string);',
  18025. ' procedure DoIt(i: integer);',
  18026. ' procedure DoIt(b: boolean);',
  18027. ' end;',
  18028. 'procedure TBird.DoIt(o: TObject); begin end;',
  18029. 'procedure TBird.DoIt(s: string); begin end;',
  18030. 'procedure TBird.DoIt(i: integer); begin end;',
  18031. 'procedure TBird.DoIt(b: boolean); begin end;',
  18032. 'var',
  18033. ' BirdIntf: IBird;',
  18034. 'begin',
  18035. ' BirdIntf.DoIt(3);',
  18036. ' BirdIntf.DoIt(''abc'');',
  18037. ' BirdIntf.DoIt(true);',
  18038. '']);
  18039. ConvertProgram;
  18040. CheckSource('TestClassInterface_Overloads',
  18041. LinesToStr([ // statements
  18042. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18043. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  18044. 'rtl.createClass($mod, "TObject", null, function () {',
  18045. ' this.$init = function () {',
  18046. ' };',
  18047. ' this.$final = function () {',
  18048. ' };',
  18049. '});',
  18050. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18051. ' this.DoIt = function (o) {',
  18052. ' };',
  18053. ' this.DoIt$1 = function (s) {',
  18054. ' };',
  18055. ' this.DoIt$2 = function (i) {',
  18056. ' };',
  18057. ' this.DoIt$3 = function (b) {',
  18058. ' };',
  18059. ' rtl.addIntf(this, $mod.IBird, {',
  18060. ' DoIt$2: "DoIt$3",',
  18061. ' DoIt: "DoIt$2"',
  18062. ' });',
  18063. '});',
  18064. 'this.BirdIntf = null;',
  18065. '']),
  18066. LinesToStr([ // $mod.$main
  18067. '$mod.BirdIntf.DoIt(3);',
  18068. '$mod.BirdIntf.DoIt$1("abc");',
  18069. '$mod.BirdIntf.DoIt$2(true);',
  18070. '']));
  18071. end;
  18072. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18073. begin
  18074. StartProgram(false);
  18075. Add([
  18076. '{$interfaces corba}',
  18077. 'type',
  18078. ' IBird = interface',
  18079. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18080. ' end;',
  18081. ' IDog = interface',
  18082. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18083. ' end;',
  18084. ' TObject = class(IBird,IDog)',
  18085. ' end;',
  18086. 'begin']);
  18087. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18088. nDuplicateGUIDXInYZ);
  18089. ConvertProgram;
  18090. end;
  18091. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18092. begin
  18093. StartProgram(false);
  18094. Add([
  18095. '{$interfaces corba}',
  18096. 'type',
  18097. ' IAnimal = interface',
  18098. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18099. ' end;',
  18100. ' IBird = interface(IAnimal)',
  18101. ' end;',
  18102. ' IHawk = interface(IBird)',
  18103. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18104. ' end;',
  18105. 'begin']);
  18106. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18107. nDuplicateGUIDXInYZ);
  18108. ConvertProgram;
  18109. end;
  18110. procedure TTestModule.TestClassInterface_AncestorImpl;
  18111. begin
  18112. StartProgram(false);
  18113. Add([
  18114. '{$interfaces corba}',
  18115. 'type',
  18116. ' integer = longint;',
  18117. ' IUnknown = interface',
  18118. ' procedure DoIt(i: integer);',
  18119. ' end;',
  18120. ' IBird = interface',
  18121. ' procedure Fly(i: integer);',
  18122. ' end;',
  18123. ' TObject = class(IUnknown)',
  18124. ' procedure DoIt(i: integer);',
  18125. ' end;',
  18126. ' TBird = class(IBird)',
  18127. ' procedure Fly(i: integer);',
  18128. ' end;',
  18129. 'procedure TObject.DoIt(i: integer); begin end;',
  18130. 'procedure TBird.Fly(i: integer); begin end;',
  18131. 'begin',
  18132. '']);
  18133. ConvertProgram;
  18134. CheckSource('TestClassInterface_AncestorIntf',
  18135. LinesToStr([ // statements
  18136. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18137. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18138. 'rtl.createClass($mod, "TObject", null, function () {',
  18139. ' this.$init = function () {',
  18140. ' };',
  18141. ' this.$final = function () {',
  18142. ' };',
  18143. ' this.DoIt = function (i) {',
  18144. ' };',
  18145. ' rtl.addIntf(this, $mod.IUnknown);',
  18146. '});',
  18147. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18148. ' this.Fly = function (i) {',
  18149. ' };',
  18150. ' rtl.addIntf(this, $mod.IBird);',
  18151. ' rtl.addIntf(this, $mod.IUnknown);',
  18152. '});',
  18153. '']),
  18154. LinesToStr([ // $mod.$main
  18155. '']));
  18156. end;
  18157. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18158. begin
  18159. StartProgram(false);
  18160. Add([
  18161. '{$interfaces corba}',
  18162. 'type',
  18163. ' integer = longint;',
  18164. ' IBird = interface',
  18165. ' procedure DoIt(i: integer);',
  18166. ' end;',
  18167. ' TObject = class',
  18168. ' procedure DoIt(i: integer);',
  18169. ' end;',
  18170. ' TBird = class(IBird)',
  18171. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18172. ' end;',
  18173. 'procedure TObject.DoIt(i: integer); begin end;',
  18174. 'procedure TBird.DoIt(i: integer); begin end;',
  18175. 'begin',
  18176. '']);
  18177. ConvertProgram;
  18178. CheckSource('TestClassInterface_ImplReintroduce',
  18179. LinesToStr([ // statements
  18180. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18181. 'rtl.createClass($mod, "TObject", null, function () {',
  18182. ' this.$init = function () {',
  18183. ' };',
  18184. ' this.$final = function () {',
  18185. ' };',
  18186. ' this.DoIt = function (i) {',
  18187. ' };',
  18188. '});',
  18189. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18190. ' this.DoIt$1 = function (i) {',
  18191. ' };',
  18192. ' rtl.addIntf(this, $mod.IBird, {',
  18193. ' DoIt: "DoIt$1"',
  18194. ' });',
  18195. '});',
  18196. '']),
  18197. LinesToStr([ // $mod.$main
  18198. '']));
  18199. end;
  18200. procedure TTestModule.TestClassInterface_MethodResolution;
  18201. begin
  18202. StartProgram(false);
  18203. Add([
  18204. '{$interfaces corba}',
  18205. 'type',
  18206. ' IUnknown = interface',
  18207. ' procedure Walk(i: longint);',
  18208. ' end;',
  18209. ' IBird = interface(IUnknown)',
  18210. ' procedure Walk(b: boolean); overload;',
  18211. ' procedure Fly(s: string);',
  18212. ' end;',
  18213. ' TObject = class',
  18214. ' end;',
  18215. ' TBird = class(TObject,IBird)',
  18216. ' procedure IBird.Fly = Move;',
  18217. ' procedure IBird.Walk = Hop;',
  18218. ' procedure Hop(i: longint);',
  18219. ' procedure Move(s: string);',
  18220. ' procedure Hop(b: boolean);',
  18221. ' end;',
  18222. 'procedure TBird.Move(s: string); begin end;',
  18223. 'procedure TBird.Hop(i: longint); begin end;',
  18224. 'procedure TBird.Hop(b: boolean); begin end;',
  18225. 'var',
  18226. ' BirdIntf: IBird;',
  18227. 'begin',
  18228. ' BirdIntf.Walk(3);',
  18229. ' BirdIntf.Walk(true);',
  18230. ' BirdIntf.Fly(''abc'');',
  18231. '']);
  18232. ConvertProgram;
  18233. CheckSource('TestClassInterface_MethodResolution',
  18234. LinesToStr([ // statements
  18235. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18236. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  18237. 'rtl.createClass($mod, "TObject", null, function () {',
  18238. ' this.$init = function () {',
  18239. ' };',
  18240. ' this.$final = function () {',
  18241. ' };',
  18242. '});',
  18243. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18244. ' this.Hop = function (i) {',
  18245. ' };',
  18246. ' this.Move = function (s) {',
  18247. ' };',
  18248. ' this.Hop$1 = function (b) {',
  18249. ' };',
  18250. ' rtl.addIntf(this, $mod.IBird, {',
  18251. ' Walk$1: "Hop$1",',
  18252. ' Fly: "Move",',
  18253. ' Walk: "Hop"',
  18254. ' });',
  18255. '});',
  18256. 'this.BirdIntf = null;',
  18257. '']),
  18258. LinesToStr([ // $mod.$main
  18259. '$mod.BirdIntf.Walk(3);',
  18260. '$mod.BirdIntf.Walk$1(true);',
  18261. '$mod.BirdIntf.Fly("abc");',
  18262. '']));
  18263. end;
  18264. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18265. begin
  18266. StartProgram(false);
  18267. Add([
  18268. '{$interfaces com}',
  18269. 'type',
  18270. ' IUnknown = interface',
  18271. ' function _AddRef: longint;',
  18272. ' procedure Walk;',
  18273. ' end;',
  18274. ' IBird = interface end;',
  18275. ' IDog = interface end;',
  18276. ' TObject = class(IBird,IDog)',
  18277. ' function _AddRef: longint; virtual; abstract;',
  18278. ' procedure Walk; virtual; abstract;',
  18279. ' end;',
  18280. ' TBird = class(IUnknown)',
  18281. ' end;',
  18282. 'begin',
  18283. '']);
  18284. ConvertProgram;
  18285. CheckSource('TestClassInterface_COM_AncestorLess',
  18286. LinesToStr([ // statements
  18287. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18288. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  18289. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  18290. 'rtl.createClass($mod, "TObject", null, function () {',
  18291. ' this.$init = function () {',
  18292. ' };',
  18293. ' this.$final = function () {',
  18294. ' };',
  18295. ' rtl.addIntf(this, $mod.IBird);',
  18296. ' rtl.addIntf(this, $mod.IDog);',
  18297. '});',
  18298. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18299. ' rtl.addIntf(this, $mod.IUnknown);',
  18300. ' rtl.addIntf(this, $mod.IBird);',
  18301. ' rtl.addIntf(this, $mod.IDog);',
  18302. '});',
  18303. '']),
  18304. LinesToStr([ // $mod.$main
  18305. '']));
  18306. end;
  18307. procedure TTestModule.TestClassInterface_MethodOverride;
  18308. begin
  18309. StartProgram(false);
  18310. Add([
  18311. '{$interfaces corba}',
  18312. 'type',
  18313. ' IUnknown = interface',
  18314. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18315. ' procedure Go;',
  18316. ' end;',
  18317. ' TObject = class(IUnknown)',
  18318. ' procedure Go; virtual; abstract;',
  18319. ' end;',
  18320. ' TBird = class',
  18321. ' procedure Go; override;',
  18322. ' end;',
  18323. ' TCat = class(TObject)',
  18324. ' procedure Go; override;',
  18325. ' end;',
  18326. ' TDog = class(TObject, IUnknown)',
  18327. ' procedure Go; override;',
  18328. ' end;',
  18329. 'procedure TBird.Go; begin end;',
  18330. 'procedure TCat.Go; begin end;',
  18331. 'procedure TDog.Go; begin end;',
  18332. 'begin',
  18333. '']);
  18334. ConvertProgram;
  18335. CheckSource('TestClassInterface_MethodOverride',
  18336. LinesToStr([ // statements
  18337. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18338. 'rtl.createClass($mod, "TObject", null, function () {',
  18339. ' this.$init = function () {',
  18340. ' };',
  18341. ' this.$final = function () {',
  18342. ' };',
  18343. ' rtl.addIntf(this, $mod.IUnknown);',
  18344. '});',
  18345. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18346. ' this.Go = function () {',
  18347. ' };',
  18348. ' rtl.addIntf(this, $mod.IUnknown);',
  18349. '});',
  18350. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  18351. ' this.Go = function () {',
  18352. ' };',
  18353. ' rtl.addIntf(this, $mod.IUnknown);',
  18354. '});',
  18355. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  18356. ' this.Go = function () {',
  18357. ' };',
  18358. ' rtl.addIntf(this, $mod.IUnknown);',
  18359. '});',
  18360. '']),
  18361. LinesToStr([ // $mod.$main
  18362. '']));
  18363. end;
  18364. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18365. begin
  18366. StartProgram(false);
  18367. Add([
  18368. '{$interfaces corba}',
  18369. 'type',
  18370. ' IUnknown = interface',
  18371. ' end;',
  18372. ' IBird = interface(IUnknown)',
  18373. ' procedure Fly(s: string);',
  18374. ' end;',
  18375. ' IEagle = interface(IBird)',
  18376. ' end;',
  18377. ' IDove = interface(IBird)',
  18378. ' end;',
  18379. ' ISwallow = interface(IBird)',
  18380. ' end;',
  18381. ' TObject = class',
  18382. ' end;',
  18383. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18384. ' procedure Fly(s: string); virtual; abstract;',
  18385. ' end;',
  18386. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18387. ' FBirdIntf: IBird;',
  18388. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18389. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18390. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18391. ' FDoveObj: TBird;',
  18392. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18393. ' function GetSwallowObj: TBird; virtual; abstract;',
  18394. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18395. ' end;',
  18396. 'begin',
  18397. '']);
  18398. ConvertProgram;
  18399. CheckSource('TestClassInterface_Delegation',
  18400. LinesToStr([ // statements
  18401. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18402. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  18403. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  18404. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  18405. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  18406. 'rtl.createClass($mod, "TObject", null, function () {',
  18407. ' this.$init = function () {',
  18408. ' };',
  18409. ' this.$final = function () {',
  18410. ' };',
  18411. '});',
  18412. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18413. ' rtl.addIntf(this, $mod.IBird);',
  18414. ' rtl.addIntf(this, $mod.IEagle);',
  18415. ' rtl.addIntf(this, $mod.IDove);',
  18416. ' rtl.addIntf(this, $mod.ISwallow);',
  18417. '});',
  18418. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18419. ' this.$init = function () {',
  18420. ' $mod.TObject.$init.call(this);',
  18421. ' this.FBirdIntf = null;',
  18422. ' this.FDoveObj = null;',
  18423. ' };',
  18424. ' this.$final = function () {',
  18425. ' this.FBirdIntf = undefined;',
  18426. ' this.FDoveObj = undefined;',
  18427. ' $mod.TObject.$final.call(this);',
  18428. ' };',
  18429. ' this.$intfmaps = {',
  18430. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18431. ' return this.FBirdIntf;',
  18432. ' },',
  18433. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18434. ' return this.GetEagleIntf();',
  18435. ' },',
  18436. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18437. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18438. ' },',
  18439. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18440. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18441. ' }',
  18442. ' };',
  18443. '});',
  18444. '']),
  18445. LinesToStr([ // $mod.$main
  18446. '']));
  18447. end;
  18448. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  18449. begin
  18450. StartProgram(false);
  18451. Add([
  18452. '{$interfaces corba}',
  18453. 'type',
  18454. ' IUnknown = interface',
  18455. ' end;',
  18456. ' IBird = interface(IUnknown)',
  18457. ' procedure Fly(s: string);',
  18458. ' end;',
  18459. ' IEagle = interface(IBird)',
  18460. ' end;',
  18461. ' IDove = interface(IBird)',
  18462. ' end;',
  18463. ' ISwallow = interface(IBird)',
  18464. ' end;',
  18465. ' TObject = class',
  18466. ' end;',
  18467. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18468. ' procedure Fly(s: string); virtual; abstract;',
  18469. ' end;',
  18470. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18471. ' private',
  18472. ' class var FBirdIntf: IBird;',
  18473. ' class var FDoveObj: TBird;',
  18474. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  18475. ' class function GetSwallowObj: TBird; virtual; abstract;',
  18476. ' protected',
  18477. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  18478. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18479. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  18480. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18481. ' end;',
  18482. 'begin',
  18483. '']);
  18484. ConvertProgram;
  18485. CheckSource('TestClassInterface_DelegationStatic',
  18486. LinesToStr([ // statements
  18487. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18488. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  18489. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  18490. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  18491. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  18492. 'rtl.createClass($mod, "TObject", null, function () {',
  18493. ' this.$init = function () {',
  18494. ' };',
  18495. ' this.$final = function () {',
  18496. ' };',
  18497. '});',
  18498. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18499. ' rtl.addIntf(this, $mod.IBird);',
  18500. ' rtl.addIntf(this, $mod.IEagle);',
  18501. ' rtl.addIntf(this, $mod.IDove);',
  18502. ' rtl.addIntf(this, $mod.ISwallow);',
  18503. '});',
  18504. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18505. ' this.FBirdIntf = null;',
  18506. ' this.FDoveObj = null;',
  18507. ' this.$intfmaps = {',
  18508. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18509. ' return this.FBirdIntf;',
  18510. ' },',
  18511. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18512. ' return this.GetEagleIntf();',
  18513. ' },',
  18514. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18515. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18516. ' },',
  18517. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18518. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18519. ' }',
  18520. ' };',
  18521. '});',
  18522. '']),
  18523. LinesToStr([ // $mod.$main
  18524. '']));
  18525. end;
  18526. procedure TTestModule.TestClassInterface_Corba_Operators;
  18527. begin
  18528. StartProgram(false);
  18529. Add([
  18530. '{$interfaces corba}',
  18531. 'type',
  18532. ' IUnknown = interface',
  18533. ' end;',
  18534. ' IBird = interface(IUnknown)',
  18535. ' function GetItems(Index: longint): longint;',
  18536. ' procedure SetItems(Index: longint; Value: longint);',
  18537. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  18538. ' end;',
  18539. ' TObject = class',
  18540. ' end;',
  18541. ' TBird = class(TObject,IBird)',
  18542. ' function GetItems(Index: longint): longint; virtual; abstract;',
  18543. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  18544. ' end;',
  18545. 'var',
  18546. ' IntfVar: IBird = nil;',
  18547. ' IntfVar2: IBird;',
  18548. ' ObjVar: TBird;',
  18549. ' v: JSValue;',
  18550. 'begin',
  18551. ' IntfVar:=nil;',
  18552. ' IntfVar[3]:=IntfVar[4];',
  18553. ' if Assigned(IntfVar) then ;',
  18554. ' IntfVar:=IntfVar2;',
  18555. ' IntfVar:=ObjVar;',
  18556. ' if IntfVar=IntfVar2 then ;',
  18557. ' if IntfVar<>IntfVar2 then ;',
  18558. ' if IntfVar is IBird then ;',
  18559. ' if IntfVar is TBird then ;',
  18560. ' if ObjVar is IBird then ;',
  18561. ' IntfVar:=IntfVar2 as IBird;',
  18562. ' ObjVar:=IntfVar2 as TBird;',
  18563. ' IntfVar:=ObjVar as IBird;',
  18564. ' IntfVar:=IBird(IntfVar2);',
  18565. ' ObjVar:=TBird(IntfVar);',
  18566. ' IntfVar:=IBird(ObjVar);',
  18567. ' v:=IntfVar;',
  18568. ' IntfVar:=IBird(v);',
  18569. ' if v is IBird then ;',
  18570. ' v:=JSValue(IntfVar);',
  18571. ' v:=IBird;',
  18572. '']);
  18573. ConvertProgram;
  18574. CheckSource('TestClassInterface_Corba_Operators',
  18575. LinesToStr([ // statements
  18576. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18577. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  18578. 'rtl.createClass($mod, "TObject", null, function () {',
  18579. ' this.$init = function () {',
  18580. ' };',
  18581. ' this.$final = function () {',
  18582. ' };',
  18583. '});',
  18584. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18585. ' rtl.addIntf(this, $mod.IBird);',
  18586. '});',
  18587. 'this.IntfVar = null;',
  18588. 'this.IntfVar2 = null;',
  18589. 'this.ObjVar = null;',
  18590. 'this.v = undefined;',
  18591. '']),
  18592. LinesToStr([ // $mod.$main
  18593. '$mod.IntfVar = null;',
  18594. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  18595. 'if ($mod.IntfVar != null) ;',
  18596. '$mod.IntfVar = $mod.IntfVar2;',
  18597. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  18598. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  18599. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  18600. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  18601. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  18602. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  18603. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  18604. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  18605. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18606. '$mod.IntfVar = $mod.IntfVar2;',
  18607. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  18608. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18609. '$mod.v = $mod.IntfVar;',
  18610. '$mod.IntfVar = rtl.getObject($mod.v);',
  18611. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  18612. '$mod.v = $mod.IntfVar;',
  18613. '$mod.v = $mod.IBird;',
  18614. '']));
  18615. end;
  18616. procedure TTestModule.TestClassInterface_Corba_Args;
  18617. begin
  18618. StartProgram(false);
  18619. Add([
  18620. '{$interfaces corba}',
  18621. 'type',
  18622. ' IUnknown = interface',
  18623. ' end;',
  18624. ' IBird = interface(IUnknown)',
  18625. ' end;',
  18626. ' TObject = class',
  18627. ' end;',
  18628. ' TBird = class(TObject,IBird)',
  18629. ' end;',
  18630. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  18631. 'begin',
  18632. ' DoIt(i,i,i);',
  18633. 'end;',
  18634. 'procedure Change(var i: IBird; out j: IBird);',
  18635. 'begin',
  18636. ' DoIt(i,i,i);',
  18637. ' Change(i,i);',
  18638. 'end;',
  18639. 'var',
  18640. ' i: IBird;',
  18641. ' o: TBird;',
  18642. 'begin',
  18643. ' DoIt(i,i,i);',
  18644. ' Change(i,i);',
  18645. ' DoIt(o,o,o);',
  18646. '']);
  18647. ConvertProgram;
  18648. CheckSource('TestClassInterface_Corba_Args',
  18649. LinesToStr([ // statements
  18650. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18651. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  18652. 'rtl.createClass($mod, "TObject", null, function () {',
  18653. ' this.$init = function () {',
  18654. ' };',
  18655. ' this.$final = function () {',
  18656. ' };',
  18657. '});',
  18658. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18659. ' rtl.addIntf(this, $mod.IBird);',
  18660. '});',
  18661. 'this.DoIt = function (u, i, j) {',
  18662. ' $mod.DoIt({',
  18663. ' get: function () {',
  18664. ' return i;',
  18665. ' },',
  18666. ' set: function (v) {',
  18667. ' i = v;',
  18668. ' }',
  18669. ' }, i, i);',
  18670. '};',
  18671. 'this.Change = function (i, j) {',
  18672. ' $mod.DoIt(i, i.get(), i.get());',
  18673. ' $mod.Change(i, i);',
  18674. '};',
  18675. 'this.i = null;',
  18676. 'this.o = null;',
  18677. '']),
  18678. LinesToStr([ // $mod.$main
  18679. '$mod.DoIt({',
  18680. ' p: $mod,',
  18681. ' get: function () {',
  18682. ' return this.p.i;',
  18683. ' },',
  18684. ' set: function (v) {',
  18685. ' this.p.i = v;',
  18686. ' }',
  18687. '}, $mod.i, $mod.i);',
  18688. '$mod.Change({',
  18689. ' p: $mod,',
  18690. ' get: function () {',
  18691. ' return this.p.i;',
  18692. ' },',
  18693. ' set: function (v) {',
  18694. ' this.p.i = v;',
  18695. ' }',
  18696. '}, {',
  18697. ' p: $mod,',
  18698. ' get: function () {',
  18699. ' return this.p.i;',
  18700. ' },',
  18701. ' set: function (v) {',
  18702. ' this.p.i = v;',
  18703. ' }',
  18704. '});',
  18705. '$mod.DoIt({',
  18706. ' p: $mod,',
  18707. ' get: function () {',
  18708. ' return this.p.o;',
  18709. ' },',
  18710. ' set: function (v) {',
  18711. ' this.p.o = v;',
  18712. ' }',
  18713. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  18714. '']));
  18715. end;
  18716. procedure TTestModule.TestClassInterface_Corba_ForIn;
  18717. begin
  18718. StartProgram(false);
  18719. Add([
  18720. '{$interfaces corba}',
  18721. 'type',
  18722. ' IUnknown = interface end;',
  18723. ' TObject = class',
  18724. ' Id: longint;',
  18725. ' end;',
  18726. ' IEnumerator = interface(IUnknown)',
  18727. ' function GetCurrent: TObject;',
  18728. ' function MoveNext: Boolean;',
  18729. ' property Current: TObject read GetCurrent;',
  18730. ' end;',
  18731. ' IEnumerable = interface(IUnknown)',
  18732. ' function GetEnumerator: IEnumerator;',
  18733. ' end;',
  18734. 'var',
  18735. ' o: TObject;',
  18736. ' i: IEnumerable;',
  18737. 'begin',
  18738. ' for o in i do o.Id:=3;',
  18739. '']);
  18740. ConvertProgram;
  18741. CheckSource('TestClassInterface_Corba_ForIn',
  18742. LinesToStr([ // statements
  18743. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18744. 'rtl.createClass($mod, "TObject", null, function () {',
  18745. ' this.$init = function () {',
  18746. ' this.Id = 0;',
  18747. ' };',
  18748. ' this.$final = function () {',
  18749. ' };',
  18750. '});',
  18751. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18752. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18753. 'this.o = null;',
  18754. 'this.i = null;',
  18755. '']),
  18756. LinesToStr([ // $mod.$main
  18757. 'var $in = $mod.i.GetEnumerator();',
  18758. 'while ($in.MoveNext()) {',
  18759. ' $mod.o = $in.GetCurrent();',
  18760. ' $mod.o.Id = 3;',
  18761. '};',
  18762. '']));
  18763. end;
  18764. procedure TTestModule.TestClassInterface_COM_AssignVar;
  18765. begin
  18766. StartProgram(false);
  18767. Add([
  18768. '{$interfaces com}',
  18769. 'type',
  18770. ' IUnknown = interface',
  18771. ' function _AddRef: longint;',
  18772. ' function _Release: longint;',
  18773. ' end;',
  18774. ' TObject = class(IUnknown)',
  18775. ' function _AddRef: longint; virtual; abstract;',
  18776. ' function _Release: longint; virtual; abstract;',
  18777. ' end;',
  18778. 'var',
  18779. ' i: IUnknown;',
  18780. 'procedure DoGlobal(o: TObject);',
  18781. 'begin',
  18782. ' i:=nil;',
  18783. ' i:=o;',
  18784. ' i:=i;',
  18785. 'end;',
  18786. 'procedure DoLocal(o: TObject);',
  18787. 'const k: IUnknown = nil;',
  18788. 'var j: IUnknown;',
  18789. 'begin',
  18790. ' k:=o;',
  18791. ' k:=i;',
  18792. ' j:=o;',
  18793. ' j:=i;',
  18794. 'end;',
  18795. 'var o: TObject;',
  18796. 'begin',
  18797. ' i:=nil;',
  18798. ' i:=o;',
  18799. '']);
  18800. ConvertProgram;
  18801. CheckSource('TestClassInterface_COM_AssignVar',
  18802. LinesToStr([ // statements
  18803. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18804. 'rtl.createClass($mod, "TObject", null, function () {',
  18805. ' this.$init = function () {',
  18806. ' };',
  18807. ' this.$final = function () {',
  18808. ' };',
  18809. ' rtl.addIntf(this, $mod.IUnknown);',
  18810. '});',
  18811. 'this.i = null;',
  18812. 'this.DoGlobal = function (o) {',
  18813. ' rtl.setIntfP($mod, "i", null);',
  18814. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  18815. ' rtl.setIntfP($mod, "i", $mod.i);',
  18816. '};',
  18817. 'var k = null;',
  18818. 'this.DoLocal = function (o) {',
  18819. ' var j = null;',
  18820. ' try{',
  18821. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18822. ' k = rtl.setIntfL(k, $mod.i);',
  18823. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18824. ' j = rtl.setIntfL(j, $mod.i);',
  18825. ' }finally{',
  18826. ' rtl._Release(j);',
  18827. ' };',
  18828. '};',
  18829. 'this.o = null;',
  18830. '']),
  18831. LinesToStr([ // $mod.$main
  18832. 'rtl.setIntfP($mod, "i", null);',
  18833. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  18834. '']));
  18835. end;
  18836. procedure TTestModule.TestClassInterface_COM_AssignArg;
  18837. begin
  18838. StartProgram(false);
  18839. Add([
  18840. '{$interfaces com}',
  18841. 'type',
  18842. ' IUnknown = interface',
  18843. ' function _AddRef: longint;',
  18844. ' function _Release: longint;',
  18845. ' end;',
  18846. ' TObject = class(IUnknown)',
  18847. ' function _AddRef: longint; virtual; abstract;',
  18848. ' function _Release: longint; virtual; abstract;',
  18849. ' end;',
  18850. 'procedure DoDefault(i, j: IUnknown);',
  18851. 'begin',
  18852. ' i:=nil;',
  18853. ' i:=j;',
  18854. 'end;',
  18855. 'begin',
  18856. '']);
  18857. ConvertProgram;
  18858. CheckSource('TestClassInterface_COM_AssignArg',
  18859. LinesToStr([ // statements
  18860. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18861. 'rtl.createClass($mod, "TObject", null, function () {',
  18862. ' this.$init = function () {',
  18863. ' };',
  18864. ' this.$final = function () {',
  18865. ' };',
  18866. ' rtl.addIntf(this, $mod.IUnknown);',
  18867. '});',
  18868. 'this.DoDefault = function (i, j) {',
  18869. ' rtl._AddRef(i);',
  18870. ' try {',
  18871. ' i = rtl.setIntfL(i, null);',
  18872. ' i = rtl.setIntfL(i, j);',
  18873. ' } finally {',
  18874. ' rtl._Release(i);',
  18875. ' };',
  18876. '};',
  18877. '']),
  18878. LinesToStr([ // $mod.$main
  18879. '']));
  18880. end;
  18881. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  18882. begin
  18883. StartProgram(false);
  18884. Add([
  18885. '{$interfaces com}',
  18886. 'type',
  18887. ' IUnknown = interface',
  18888. ' function _AddRef: longint;',
  18889. ' function _Release: longint;',
  18890. ' end;',
  18891. ' TObject = class(IUnknown)',
  18892. ' function _AddRef: longint; virtual; abstract;',
  18893. ' function _Release: longint; virtual; abstract;',
  18894. ' end;',
  18895. 'function DoDefault(i: IUnknown): IUnknown;',
  18896. 'begin',
  18897. ' Result:=i;',
  18898. ' if Result<>nil then exit;',
  18899. 'end;',
  18900. 'begin',
  18901. '']);
  18902. ConvertProgram;
  18903. CheckSource('TestClassInterface_COM_FunctionResult',
  18904. LinesToStr([ // statements
  18905. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18906. 'rtl.createClass($mod, "TObject", null, function () {',
  18907. ' this.$init = function () {',
  18908. ' };',
  18909. ' this.$final = function () {',
  18910. ' };',
  18911. ' rtl.addIntf(this, $mod.IUnknown);',
  18912. '});',
  18913. 'this.DoDefault = function (i) {',
  18914. ' var Result = null;',
  18915. ' var $ok = false;',
  18916. ' try {',
  18917. ' Result = rtl.setIntfL(Result, i);',
  18918. ' if(Result !== null){',
  18919. ' $ok = true;',
  18920. ' return Result;',
  18921. ' };',
  18922. ' $ok = true;',
  18923. ' } finally {',
  18924. ' if(!$ok) rtl._Release(Result);',
  18925. ' };',
  18926. ' return Result;',
  18927. '};',
  18928. '']),
  18929. LinesToStr([ // $mod.$main
  18930. '']));
  18931. end;
  18932. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  18933. begin
  18934. StartProgram(false);
  18935. Add([
  18936. '{$interfaces com}',
  18937. 'type',
  18938. ' IUnknown = interface',
  18939. ' function _AddRef: longint;',
  18940. ' function _Release: longint;',
  18941. ' end;',
  18942. ' TObject = class(IUnknown)',
  18943. ' function _AddRef: longint; virtual; abstract;',
  18944. ' function _Release: longint; virtual; abstract;',
  18945. ' function GetIntf: IUnknown; virtual;',
  18946. ' end;',
  18947. ' TMouse = class',
  18948. ' function GetIntf: IUnknown; override;',
  18949. ' end;',
  18950. 'function TObject.GetIntf: IUnknown; begin end;',
  18951. 'function TMouse.GetIntf: IUnknown;',
  18952. 'var i: IUnknown;',
  18953. 'begin',
  18954. ' inherited;',
  18955. ' inherited GetIntf;',
  18956. ' inherited GetIntf();',
  18957. ' Result:=inherited GetIntf;',
  18958. ' Result:=inherited GetIntf();',
  18959. ' i:=inherited GetIntf;',
  18960. ' i:=inherited GetIntf();',
  18961. 'end;',
  18962. 'begin',
  18963. '']);
  18964. ConvertProgram;
  18965. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  18966. LinesToStr([ // statements
  18967. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18968. 'rtl.createClass($mod, "TObject", null, function () {',
  18969. ' this.$init = function () {',
  18970. ' };',
  18971. ' this.$final = function () {',
  18972. ' };',
  18973. ' this.GetIntf = function () {',
  18974. ' var Result = null;',
  18975. ' return Result;',
  18976. ' };',
  18977. ' rtl.addIntf(this, $mod.IUnknown);',
  18978. '});',
  18979. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  18980. ' this.GetIntf = function () {',
  18981. ' var Result = null;',
  18982. ' var i = null;',
  18983. ' var $ir = rtl.createIntfRefs();',
  18984. ' var $ok = false;',
  18985. ' try {',
  18986. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  18987. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  18988. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  18989. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18990. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18991. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18992. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18993. ' $ok = true;',
  18994. ' } finally {',
  18995. ' $ir.free();',
  18996. ' rtl._Release(i);',
  18997. ' if (!$ok) rtl._Release(Result);',
  18998. ' };',
  18999. ' return Result;',
  19000. ' };',
  19001. ' rtl.addIntf(this, $mod.IUnknown);',
  19002. '});',
  19003. '']),
  19004. LinesToStr([ // $mod.$main
  19005. '']));
  19006. end;
  19007. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19008. begin
  19009. StartProgram(false);
  19010. Add([
  19011. '{$interfaces com}',
  19012. 'type',
  19013. ' IUnknown = interface',
  19014. ' function _AddRef: longint;',
  19015. ' function _Release: longint;',
  19016. ' end;',
  19017. ' TObject = class(IUnknown)',
  19018. ' function _AddRef: longint; virtual; abstract;',
  19019. ' function _Release: longint; virtual; abstract;',
  19020. ' end;',
  19021. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19022. 'begin',
  19023. ' if i is IUnknown then ;',
  19024. ' if o is IUnknown then ;',
  19025. ' if i is TObject then ;',
  19026. ' i:=j as IUnknown;',
  19027. ' i:=o as IUnknown;',
  19028. ' o:=j as TObject;',
  19029. ' i:=IUnknown(j);',
  19030. ' i:=IUnknown(o);',
  19031. ' o:=TObject(i);',
  19032. 'end;',
  19033. 'begin',
  19034. '']);
  19035. ConvertProgram;
  19036. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19037. LinesToStr([ // statements
  19038. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19039. 'rtl.createClass($mod, "TObject", null, function () {',
  19040. ' this.$init = function () {',
  19041. ' };',
  19042. ' this.$final = function () {',
  19043. ' };',
  19044. ' rtl.addIntf(this, $mod.IUnknown);',
  19045. '});',
  19046. 'this.DoDefault = function (i, j, o) {',
  19047. ' rtl._AddRef(i);',
  19048. ' try {',
  19049. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19050. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19051. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19052. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19053. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19054. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19055. ' i = rtl.setIntfL(i, j);',
  19056. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19057. ' o = rtl.intfToClass(i, $mod.TObject);',
  19058. ' } finally {',
  19059. ' rtl._Release(i);',
  19060. ' };',
  19061. '};',
  19062. '']),
  19063. LinesToStr([ // $mod.$main
  19064. '']));
  19065. end;
  19066. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19067. begin
  19068. StartProgram(false);
  19069. Add([
  19070. '{$interfaces com}',
  19071. 'type',
  19072. ' IUnknown = interface',
  19073. ' function _AddRef: longint;',
  19074. ' function _Release: longint;',
  19075. ' end;',
  19076. ' TObject = class(IUnknown)',
  19077. ' function _AddRef: longint; virtual; abstract;',
  19078. ' function _Release: longint; virtual; abstract;',
  19079. ' end;',
  19080. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19081. 'var o: TObject;',
  19082. 'begin',
  19083. ' DoIt(v,v,v,v);',
  19084. ' DoIt(o,o,k,k);',
  19085. 'end;',
  19086. 'procedure DoSome;',
  19087. 'var v: IUnknown;',
  19088. 'begin',
  19089. ' DoIt(v,v,v,v);',
  19090. 'end;',
  19091. 'var i: IUnknown;',
  19092. 'begin',
  19093. ' DoIt(i,i,i,i);',
  19094. '']);
  19095. ConvertProgram;
  19096. CheckSource('TestClassInterface_COM_PassAsArg',
  19097. LinesToStr([ // statements
  19098. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19099. 'rtl.createClass($mod, "TObject", null, function () {',
  19100. ' this.$init = function () {',
  19101. ' };',
  19102. ' this.$final = function () {',
  19103. ' };',
  19104. ' rtl.addIntf(this, $mod.IUnknown);',
  19105. '});',
  19106. 'this.DoIt = function (v, j, k, l) {',
  19107. ' var o = null;',
  19108. ' var $ir = rtl.createIntfRefs();',
  19109. ' rtl._AddRef(v);',
  19110. ' try {',
  19111. ' $mod.DoIt(v, v, {',
  19112. ' get: function () {',
  19113. ' return v;',
  19114. ' },',
  19115. ' set: function (w) {',
  19116. ' v = rtl.setIntfL(v, w);',
  19117. ' }',
  19118. ' }, {',
  19119. ' get: function () {',
  19120. ' return v;',
  19121. ' },',
  19122. ' set: function (w) {',
  19123. ' v = rtl.setIntfL(v, w);',
  19124. ' }',
  19125. ' });',
  19126. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19127. ' } finally {',
  19128. ' $ir.free();',
  19129. ' rtl._Release(v);',
  19130. ' };',
  19131. '};',
  19132. 'this.DoSome = function () {',
  19133. ' var v = null;',
  19134. ' try {',
  19135. ' $mod.DoIt(v, v, {',
  19136. ' get: function () {',
  19137. ' return v;',
  19138. ' },',
  19139. ' set: function (w) {',
  19140. ' v = rtl.setIntfL(v, w);',
  19141. ' }',
  19142. ' }, {',
  19143. ' get: function () {',
  19144. ' return v;',
  19145. ' },',
  19146. ' set: function (w) {',
  19147. ' v = rtl.setIntfL(v, w);',
  19148. ' }',
  19149. ' });',
  19150. ' } finally {',
  19151. ' rtl._Release(v);',
  19152. ' };',
  19153. '};',
  19154. 'this.i = null;',
  19155. '']),
  19156. LinesToStr([ // $mod.$main
  19157. '$mod.DoIt($mod.i, $mod.i, {',
  19158. ' p: $mod,',
  19159. ' get: function () {',
  19160. ' return this.p.i;',
  19161. ' },',
  19162. ' set: function (v) {',
  19163. ' rtl.setIntfP(this.p, "i", v);',
  19164. ' }',
  19165. '}, {',
  19166. ' p: $mod,',
  19167. ' get: function () {',
  19168. ' return this.p.i;',
  19169. ' },',
  19170. ' set: function (v) {',
  19171. ' rtl.setIntfP(this.p, "i", v);',
  19172. ' }',
  19173. '});',
  19174. '']));
  19175. end;
  19176. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19177. begin
  19178. StartProgram(false);
  19179. Add([
  19180. '{$interfaces com}',
  19181. 'type',
  19182. ' IUnknown = interface',
  19183. ' function _AddRef: longint;',
  19184. ' function _Release: longint;',
  19185. ' end;',
  19186. ' TObject = class(IUnknown)',
  19187. ' function _AddRef: longint; virtual; abstract;',
  19188. ' function _Release: longint; virtual; abstract;',
  19189. ' end;',
  19190. 'procedure DoIt(out i);',
  19191. 'begin end;',
  19192. 'procedure DoSome;',
  19193. 'var v: IUnknown;',
  19194. 'begin',
  19195. ' DoIt(v);',
  19196. 'end;',
  19197. 'function GetIt: IUnknown;',
  19198. 'begin',
  19199. ' DoIt(Result);',
  19200. 'end;',
  19201. 'var i: IUnknown;',
  19202. 'begin',
  19203. ' DoIt(i);',
  19204. '']);
  19205. ConvertProgram;
  19206. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19207. LinesToStr([ // statements
  19208. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19209. 'rtl.createClass($mod, "TObject", null, function () {',
  19210. ' this.$init = function () {',
  19211. ' };',
  19212. ' this.$final = function () {',
  19213. ' };',
  19214. ' rtl.addIntf(this, $mod.IUnknown);',
  19215. '});',
  19216. 'this.DoIt = function (i) {',
  19217. '};',
  19218. 'this.DoSome = function () {',
  19219. ' var v = null;',
  19220. ' try {',
  19221. ' $mod.DoIt({',
  19222. ' get: function () {',
  19223. ' return v;',
  19224. ' },',
  19225. ' set: function (w) {',
  19226. ' v = w;',
  19227. ' }',
  19228. ' });',
  19229. ' } finally {',
  19230. ' rtl._Release(v);',
  19231. ' };',
  19232. '};',
  19233. 'this.GetIt = function () {',
  19234. ' var Result = null;',
  19235. ' var $ok = false;',
  19236. ' try {',
  19237. ' $mod.DoIt({',
  19238. ' get: function () {',
  19239. ' return Result;',
  19240. ' },',
  19241. ' set: function (v) {',
  19242. ' Result = v;',
  19243. ' }',
  19244. ' });',
  19245. ' $ok = true;',
  19246. ' } finally {',
  19247. ' if (!$ok) rtl._Release(Result);',
  19248. ' };',
  19249. ' return Result;',
  19250. '};',
  19251. 'this.i = null;',
  19252. '']),
  19253. LinesToStr([ // $mod.$main
  19254. 'try {',
  19255. ' $mod.DoIt({',
  19256. ' p: $mod,',
  19257. ' get: function () {',
  19258. ' return this.p.i;',
  19259. ' },',
  19260. ' set: function (v) {',
  19261. ' this.p.i = v;',
  19262. ' }',
  19263. ' });',
  19264. '} finally {',
  19265. ' rtl._Release($mod.i);',
  19266. '};',
  19267. '']));
  19268. end;
  19269. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19270. begin
  19271. StartProgram(false);
  19272. Add([
  19273. '{$interfaces com}',
  19274. 'type',
  19275. ' IUnknown = interface',
  19276. ' function _AddRef: longint;',
  19277. ' function _Release: longint;',
  19278. ' end;',
  19279. ' TObject = class(IUnknown)',
  19280. ' function _AddRef: longint; virtual; abstract;',
  19281. ' function _Release: longint; virtual; abstract;',
  19282. ' end;',
  19283. 'function GetIt: IUnknown;',
  19284. 'begin',
  19285. 'end;',
  19286. 'procedure DoSome;',
  19287. 'var v: IUnknown;',
  19288. ' i: longint;',
  19289. 'begin',
  19290. ' v:=GetIt;',
  19291. ' v:=GetIt();',
  19292. ' GetIt()._AddRef;',
  19293. ' i:=GetIt()._AddRef;',
  19294. 'end;',
  19295. 'var v: IUnknown;',
  19296. ' i: longint;',
  19297. 'begin',
  19298. ' v:=GetIt;',
  19299. ' v:=GetIt();',
  19300. ' GetIt()._AddRef;',
  19301. ' i:=GetIt()._AddRef;',
  19302. '']);
  19303. ConvertProgram;
  19304. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19305. LinesToStr([ // statements
  19306. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19307. 'rtl.createClass($mod, "TObject", null, function () {',
  19308. ' this.$init = function () {',
  19309. ' };',
  19310. ' this.$final = function () {',
  19311. ' };',
  19312. ' rtl.addIntf(this, $mod.IUnknown);',
  19313. '});',
  19314. 'this.GetIt = function () {',
  19315. ' var Result = null;',
  19316. ' return Result;',
  19317. '};',
  19318. 'this.DoSome = function () {',
  19319. ' var v = null;',
  19320. ' var i = 0;',
  19321. ' var $ir = rtl.createIntfRefs();',
  19322. ' try {',
  19323. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19324. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19325. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19326. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19327. ' } finally {',
  19328. ' $ir.free();',
  19329. ' rtl._Release(v);',
  19330. ' };',
  19331. '};',
  19332. 'this.v = null;',
  19333. 'this.i = 0;',
  19334. '']),
  19335. LinesToStr([ // $mod.$main
  19336. 'var $ir = rtl.createIntfRefs();',
  19337. 'try {',
  19338. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19339. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19340. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19341. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19342. '} finally {',
  19343. ' $ir.free();',
  19344. '};',
  19345. '']));
  19346. end;
  19347. procedure TTestModule.TestClassInterface_COM_Property;
  19348. begin
  19349. StartProgram(false);
  19350. Add([
  19351. '{$interfaces com}',
  19352. 'type',
  19353. ' IUnknown = interface',
  19354. ' function _AddRef: longint;',
  19355. ' function _Release: longint;',
  19356. ' end;',
  19357. ' TObject = class(IUnknown)',
  19358. ' FAnt: IUnknown;',
  19359. ' function _AddRef: longint; virtual; abstract;',
  19360. ' function _Release: longint; virtual; abstract;',
  19361. ' function GetBird: IUnknown; virtual; abstract;',
  19362. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19363. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19364. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19365. ' property Ant: IUnknown read FAnt write FAnt;',
  19366. ' property Bird: IUnknown read GetBird write SetBird;',
  19367. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19368. ' end;',
  19369. 'procedure DoIt;',
  19370. 'var',
  19371. ' o: TObject;',
  19372. ' v: IUnknown;',
  19373. 'begin',
  19374. ' v:=o.Ant;',
  19375. ' o.Ant:=v;',
  19376. ' o.Ant:=o.Ant;',
  19377. ' v:=o.Bird;',
  19378. ' o.Bird:=v;',
  19379. ' o.Bird:=o.Bird;',
  19380. ' v:=o.Items[1];',
  19381. ' o.Items[2]:=v;',
  19382. ' o.Items[3]:=o.Items[4];',
  19383. ' v:=o[5];',
  19384. ' o[6]:=v;',
  19385. ' o[7]:=o[8];',
  19386. 'end;',
  19387. 'begin',
  19388. '']);
  19389. ConvertProgram;
  19390. CheckSource('TestClassInterface_COM_Property',
  19391. LinesToStr([ // statements
  19392. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19393. 'rtl.createClass($mod, "TObject", null, function () {',
  19394. ' this.$init = function () {',
  19395. ' this.FAnt = null;',
  19396. ' };',
  19397. ' this.$final = function () {',
  19398. ' this.FAnt = undefined;',
  19399. ' };',
  19400. ' rtl.addIntf(this, $mod.IUnknown);',
  19401. '});',
  19402. 'this.DoIt = function () {',
  19403. ' var o = null;',
  19404. ' var v = null;',
  19405. ' var $ir = rtl.createIntfRefs();',
  19406. ' try {',
  19407. ' v = rtl.setIntfL(v, o.FAnt);',
  19408. ' rtl.setIntfP(o, "FAnt", v);',
  19409. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19410. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19411. ' o.SetBird(v);',
  19412. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19413. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19414. ' o.SetItems(2, v);',
  19415. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19416. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19417. ' o.SetItems(6, v);',
  19418. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19419. ' } finally {',
  19420. ' $ir.free();',
  19421. ' rtl._Release(v);',
  19422. ' };',
  19423. '};',
  19424. '']),
  19425. LinesToStr([ // $mod.$main
  19426. '']));
  19427. end;
  19428. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  19429. begin
  19430. StartProgram(false);
  19431. Add([
  19432. '{$interfaces com}',
  19433. 'type',
  19434. ' IUnknown = interface',
  19435. ' function _AddRef: longint;',
  19436. ' function _Release: longint;',
  19437. ' function GetBird: IUnknown;',
  19438. ' procedure SetBird(Value: IUnknown);',
  19439. ' function GetItems(Index: longint): IUnknown;',
  19440. ' procedure SetItems(Index: longint; Value: IUnknown);',
  19441. ' property Bird: IUnknown read GetBird write SetBird;',
  19442. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19443. ' end;',
  19444. ' TObject = class(IUnknown)',
  19445. ' function _AddRef: longint; virtual; abstract;',
  19446. ' function _Release: longint; virtual; abstract;',
  19447. ' function GetBird: IUnknown; virtual; abstract;',
  19448. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19449. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19450. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19451. ' end;',
  19452. 'procedure DoIt;',
  19453. 'var',
  19454. ' o: TObject;',
  19455. ' v: IUnknown;',
  19456. 'begin',
  19457. ' v:=v.Items[1];',
  19458. ' v.Items[2]:=v;',
  19459. ' v.Items[3]:=v.Items[4];',
  19460. ' v:=v[5];',
  19461. ' v[6]:=v;',
  19462. ' v[7]:=v[8];',
  19463. ' v[9].Bird.Bird:=v;',
  19464. ' v:=v.Bird[10].Bird',
  19465. 'end;',
  19466. 'begin',
  19467. '']);
  19468. ConvertProgram;
  19469. CheckSource('TestClassInterface_COM_IntfProperty',
  19470. LinesToStr([ // statements
  19471. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  19472. ' "_AddRef",',
  19473. ' "_Release",',
  19474. ' "GetBird",',
  19475. ' "SetBird",',
  19476. ' "GetItems",',
  19477. ' "SetItems"',
  19478. '], null);',
  19479. 'rtl.createClass($mod, "TObject", null, function () {',
  19480. ' this.$init = function () {',
  19481. ' };',
  19482. ' this.$final = function () {',
  19483. ' };',
  19484. ' rtl.addIntf(this, $mod.IUnknown);',
  19485. '});',
  19486. 'this.DoIt = function () {',
  19487. ' var o = null;',
  19488. ' var v = null;',
  19489. ' var $ir = rtl.createIntfRefs();',
  19490. ' try {',
  19491. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  19492. ' v.SetItems(2, v);',
  19493. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  19494. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  19495. ' v.SetItems(6, v);',
  19496. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  19497. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  19498. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  19499. ' } finally {',
  19500. ' $ir.free();',
  19501. ' rtl._Release(v);',
  19502. ' };',
  19503. '};',
  19504. '']),
  19505. LinesToStr([ // $mod.$main
  19506. '']));
  19507. end;
  19508. procedure TTestModule.TestClassInterface_COM_Delegation;
  19509. begin
  19510. StartProgram(false);
  19511. Add([
  19512. '{$interfaces com}',
  19513. 'type',
  19514. ' IUnknown = interface',
  19515. ' function _AddRef: longint;',
  19516. ' function _Release: longint;',
  19517. ' end;',
  19518. ' IBird = interface(IUnknown)',
  19519. ' procedure Fly(s: string);',
  19520. ' end;',
  19521. ' IEagle = interface(IBird) end;',
  19522. ' IDove = interface(IBird) end;',
  19523. ' ISwallow = interface(IBird) end;',
  19524. ' TObject = class',
  19525. ' end;',
  19526. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19527. ' function _AddRef: longint; virtual; abstract;',
  19528. ' function _Release: longint; virtual; abstract;',
  19529. ' procedure Fly(s: string); virtual; abstract;',
  19530. ' end;',
  19531. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19532. ' function _AddRef: longint; virtual; abstract;',
  19533. ' function _Release: longint; virtual; abstract;',
  19534. ' FBirdIntf: IBird;',
  19535. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19536. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19537. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19538. ' FDoveObj: TBird;',
  19539. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19540. ' function GetSwallowObj: TBird; virtual; abstract;',
  19541. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19542. ' end;',
  19543. 'begin',
  19544. '']);
  19545. ConvertProgram;
  19546. CheckSource('TestClassInterface_COM_Delegation',
  19547. LinesToStr([ // statements
  19548. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19549. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  19550. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  19551. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  19552. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  19553. 'rtl.createClass($mod, "TObject", null, function () {',
  19554. ' this.$init = function () {',
  19555. ' };',
  19556. ' this.$final = function () {',
  19557. ' };',
  19558. '});',
  19559. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19560. ' rtl.addIntf(this, $mod.IBird);',
  19561. ' rtl.addIntf(this, $mod.IEagle);',
  19562. ' rtl.addIntf(this, $mod.IDove);',
  19563. ' rtl.addIntf(this, $mod.ISwallow);',
  19564. '});',
  19565. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  19566. ' this.$init = function () {',
  19567. ' $mod.TObject.$init.call(this);',
  19568. ' this.FBirdIntf = null;',
  19569. ' this.FDoveObj = null;',
  19570. ' };',
  19571. ' this.$final = function () {',
  19572. ' this.FBirdIntf = undefined;',
  19573. ' this.FDoveObj = undefined;',
  19574. ' $mod.TObject.$final.call(this);',
  19575. ' };',
  19576. ' this.$intfmaps = {',
  19577. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  19578. ' return rtl._AddRef(this.FBirdIntf);',
  19579. ' },',
  19580. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  19581. ' return this.GetEagleIntf();',
  19582. ' },',
  19583. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  19584. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  19585. ' },',
  19586. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  19587. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19588. ' }',
  19589. ' };',
  19590. '});',
  19591. '']),
  19592. LinesToStr([ // $mod.$main
  19593. '']));
  19594. end;
  19595. procedure TTestModule.TestClassInterface_COM_With;
  19596. begin
  19597. StartProgram(false);
  19598. Add([
  19599. '{$interfaces com}',
  19600. 'type',
  19601. ' IUnknown = interface',
  19602. ' function _AddRef: longint;',
  19603. ' function _Release: longint;',
  19604. ' function GetAnt: IUnknown;',
  19605. ' property Ant: IUnknown read GetAnt;',
  19606. ' end;',
  19607. ' TObject = class(IUnknown)',
  19608. ' function _AddRef: longint; virtual; abstract;',
  19609. ' function _Release: longint; virtual; abstract;',
  19610. ' function GetAnt: IUnknown; virtual; abstract;',
  19611. ' property Ant: IUnknown read GetAnt;',
  19612. ' end;',
  19613. 'procedure DoIt;',
  19614. 'var',
  19615. ' i: IUnknown;',
  19616. 'begin',
  19617. ' with i do ',
  19618. ' GetAnt;',
  19619. ' with i.Ant, Ant do ',
  19620. ' GetAnt;',
  19621. 'end;',
  19622. 'begin',
  19623. '']);
  19624. ConvertProgram;
  19625. CheckSource('TestClassInterface_COM_With',
  19626. LinesToStr([ // statements
  19627. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  19628. 'rtl.createClass($mod, "TObject", null, function () {',
  19629. ' this.$init = function () {',
  19630. ' };',
  19631. ' this.$final = function () {',
  19632. ' };',
  19633. ' rtl.addIntf(this, $mod.IUnknown);',
  19634. '});',
  19635. 'this.DoIt = function () {',
  19636. ' var i = null;',
  19637. ' var $ir = rtl.createIntfRefs();',
  19638. ' try {',
  19639. ' $ir.ref(1, i.GetAnt());',
  19640. ' var $with = $ir.ref(2, i.GetAnt());',
  19641. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  19642. ' $ir.ref(4, $with1.GetAnt());',
  19643. ' } finally {',
  19644. ' $ir.free();',
  19645. ' };',
  19646. '};',
  19647. '']),
  19648. LinesToStr([ // $mod.$main
  19649. '']));
  19650. end;
  19651. procedure TTestModule.TestClassInterface_COM_ForIn;
  19652. begin
  19653. StartProgram(false);
  19654. Add([
  19655. '{$interfaces com}',
  19656. 'type',
  19657. ' IUnknown = interface end;',
  19658. ' TObject = class',
  19659. ' Id: longint;',
  19660. ' end;',
  19661. ' IEnumerator = interface(IUnknown)',
  19662. ' function GetCurrent: TObject;',
  19663. ' function MoveNext: Boolean;',
  19664. ' property Current: TObject read GetCurrent;',
  19665. ' end;',
  19666. ' IEnumerable = interface(IUnknown)',
  19667. ' function GetEnumerator: IEnumerator;',
  19668. ' end;',
  19669. 'var',
  19670. ' o: TObject;',
  19671. ' i: IEnumerable;',
  19672. 'begin',
  19673. ' for o in i do o.Id:=3;',
  19674. '']);
  19675. ConvertProgram;
  19676. CheckSource('TestClassInterface_COM_ForIn',
  19677. LinesToStr([ // statements
  19678. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19679. 'rtl.createClass($mod, "TObject", null, function () {',
  19680. ' this.$init = function () {',
  19681. ' this.Id = 0;',
  19682. ' };',
  19683. ' this.$final = function () {',
  19684. ' };',
  19685. '});',
  19686. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  19687. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  19688. 'this.o = null;',
  19689. 'this.i = null;',
  19690. '']),
  19691. LinesToStr([ // $mod.$main
  19692. 'var $in = $mod.i.GetEnumerator();',
  19693. 'try {',
  19694. ' while ($in.MoveNext()) {',
  19695. ' $mod.o = $in.GetCurrent();',
  19696. ' $mod.o.Id = 3;',
  19697. ' }',
  19698. '} finally {',
  19699. ' rtl._Release($in)',
  19700. '};',
  19701. '']));
  19702. end;
  19703. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  19704. begin
  19705. StartProgram(false);
  19706. Add([
  19707. '{$interfaces com}',
  19708. 'type',
  19709. ' IUnknown = interface',
  19710. ' function _AddRef: longint;',
  19711. ' function _Release: longint;',
  19712. ' end;',
  19713. ' TObject = class',
  19714. ' end;',
  19715. ' TArrOfIntf = array of IUnknown;',
  19716. 'begin',
  19717. '']);
  19718. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  19719. ConvertProgram;
  19720. end;
  19721. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  19722. begin
  19723. StartProgram(false);
  19724. Add([
  19725. '{$interfaces com}',
  19726. 'type',
  19727. ' IUnknown = interface',
  19728. ' function _AddRef: longint;',
  19729. ' function _Release: longint;',
  19730. ' end;',
  19731. ' TRec = record',
  19732. ' i: IUnknown;',
  19733. ' end;',
  19734. 'begin',
  19735. '']);
  19736. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  19737. ConvertProgram;
  19738. end;
  19739. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  19740. begin
  19741. StartUnit(false);
  19742. Add([
  19743. '{$interfaces com}',
  19744. 'interface',
  19745. 'implementation',
  19746. 'type',
  19747. ' IUnknown = interface',
  19748. ' function _AddRef: longint;',
  19749. ' end;',
  19750. ' TObject = class(IUnknown)',
  19751. ' function _AddRef: longint;',
  19752. ' end;',
  19753. 'function TObject._AddRef: longint; begin end;',
  19754. 'var i: IUnknown;',
  19755. ' o: TObject;',
  19756. 'initialization',
  19757. ' i:=nil;',
  19758. ' i:=i;',
  19759. ' i:=o;',
  19760. ' if (o as IUnknown)=nil then ;',
  19761. '']);
  19762. ConvertUnit;
  19763. CheckSource('TestClassInterface_COM_UnitInitialization',
  19764. LinesToStr([ // statements
  19765. 'var $impl = $mod.$impl;',
  19766. '']),
  19767. LinesToStr([ // this.$init
  19768. 'var $ir = rtl.createIntfRefs();',
  19769. 'try {',
  19770. ' rtl.setIntfP($impl, "i", null);',
  19771. ' rtl.setIntfP($impl, "i", $impl.i);',
  19772. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  19773. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  19774. '} finally {',
  19775. ' $ir.free();',
  19776. '};',
  19777. '']),
  19778. LinesToStr([ // implementation
  19779. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  19780. 'rtl.createClass($impl, "TObject", null, function () {',
  19781. ' this.$init = function () {',
  19782. ' };',
  19783. ' this.$final = function () {',
  19784. ' };',
  19785. ' this._AddRef = function () {',
  19786. ' var Result = 0;',
  19787. ' return Result;',
  19788. ' };',
  19789. ' rtl.addIntf(this, $impl.IUnknown);',
  19790. '});',
  19791. '$impl.i = null;',
  19792. '$impl.o = null;',
  19793. ''])
  19794. );
  19795. end;
  19796. procedure TTestModule.TestClassInterface_GUID;
  19797. begin
  19798. StartProgram(false);
  19799. Add([
  19800. '{$interfaces corba}',
  19801. 'type',
  19802. ' IUnknown = interface',
  19803. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19804. ' end;',
  19805. ' TObject = class end;',
  19806. ' TGUID = record D1, D2, D3, D4: word; end;',
  19807. ' TAliasGUID = TGUID;',
  19808. ' TGUIDString = type string;',
  19809. ' TAliasGUIDString = TGUIDString;',
  19810. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  19811. 'begin end;',
  19812. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  19813. 'begin end;',
  19814. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  19815. 'begin end;',
  19816. 'var',
  19817. ' i: IUnknown;',
  19818. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  19819. ' s: TAliasGUIDString;',
  19820. 'begin',
  19821. ' DoConstGUIDIt(IUnknown);',
  19822. ' DoDefGUID(IUnknown);',
  19823. ' DoStr(IUnknown);',
  19824. ' DoConstGUIDIt(i);',
  19825. ' DoDefGUID(i);',
  19826. ' DoStr(i);',
  19827. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19828. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19829. ' DoStr(g);',
  19830. ' g:=i;',
  19831. ' g:=IUnknown;',
  19832. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19833. ' s:=i;',
  19834. ' s:=IUnknown;',
  19835. ' s:=g;',
  19836. ' if g=i then ;',
  19837. ' if i=g then ;',
  19838. ' if g=IUnknown then ;',
  19839. ' if IUnknown=g then ;',
  19840. ' if s=i then ;',
  19841. ' if i=s then ;',
  19842. ' if s=IUnknown then ;',
  19843. ' if IUnknown=s then ;',
  19844. ' if s=g then ;',
  19845. ' if g=s then ;',
  19846. '']);
  19847. ConvertProgram;
  19848. CheckSource('TestClassInterface_GUID',
  19849. LinesToStr([ // statements
  19850. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19851. 'rtl.createClass($mod, "TObject", null, function () {',
  19852. ' this.$init = function () {',
  19853. ' };',
  19854. ' this.$final = function () {',
  19855. ' };',
  19856. '});',
  19857. 'rtl.recNewT($mod, "TGUID", function () {',
  19858. ' this.D1 = 0;',
  19859. ' this.D2 = 0;',
  19860. ' this.D3 = 0;',
  19861. ' this.D4 = 0;',
  19862. ' this.$eq = function (b) {',
  19863. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19864. ' };',
  19865. ' this.$assign = function (s) {',
  19866. ' this.D1 = s.D1;',
  19867. ' this.D2 = s.D2;',
  19868. ' this.D3 = s.D3;',
  19869. ' this.D4 = s.D4;',
  19870. ' return this;',
  19871. ' };',
  19872. '});',
  19873. 'this.DoConstGUIDIt = function (g) {',
  19874. '};',
  19875. 'this.DoDefGUID = function (g) {',
  19876. '};',
  19877. 'this.DoStr = function (s) {',
  19878. '};',
  19879. 'this.i = null;',
  19880. 'this.g = $mod.TGUID.$clone({',
  19881. ' D1: 0xD91C9AF4,',
  19882. ' D2: 0x3C93,',
  19883. ' D3: 0x420F,',
  19884. ' D4: [',
  19885. ' 0xA3,',
  19886. ' 0x03,',
  19887. ' 0xBF,',
  19888. ' 0x5B,',
  19889. ' 0xA8,',
  19890. ' 0x2B,',
  19891. ' 0xFD,',
  19892. ' 0x23',
  19893. ' ]',
  19894. '});',
  19895. 'this.s = "";',
  19896. '']),
  19897. LinesToStr([ // $mod.$main
  19898. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  19899. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  19900. '$mod.DoStr($mod.IUnknown.$guid);',
  19901. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  19902. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  19903. '$mod.DoStr($mod.i.$guid);',
  19904. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19905. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19906. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  19907. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  19908. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  19909. '$mod.g.$assign({',
  19910. ' D1: 0xD91C9AF4,',
  19911. ' D2: 0x3C93,',
  19912. ' D3: 0x420F,',
  19913. ' D4: [',
  19914. ' 0xA3,',
  19915. ' 0x03,',
  19916. ' 0xBF,',
  19917. ' 0x5B,',
  19918. ' 0xA8,',
  19919. ' 0x2B,',
  19920. ' 0xFD,',
  19921. ' 0x23',
  19922. ' ]',
  19923. '});',
  19924. '$mod.s = $mod.i.$guid;',
  19925. '$mod.s = $mod.IUnknown.$guid;',
  19926. '$mod.s = rtl.guidrToStr($mod.g);',
  19927. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19928. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19929. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19930. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19931. 'if ($mod.s === $mod.i.$guid) ;',
  19932. 'if ($mod.i.$guid === $mod.s) ;',
  19933. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  19934. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  19935. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19936. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19937. '']));
  19938. end;
  19939. procedure TTestModule.TestClassInterface_GUIDProperty;
  19940. begin
  19941. StartProgram(false);
  19942. Add([
  19943. '{$interfaces corba}',
  19944. 'type',
  19945. ' IUnknown = interface',
  19946. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19947. ' end;',
  19948. ' TGUID = record D1, D2, D3, D4: word; end;',
  19949. ' TAliasGUID = TGUID;',
  19950. ' TGUIDString = type string;',
  19951. ' TAliasGUIDString = TGUIDString;',
  19952. ' TObject = class',
  19953. ' function GetG: TAliasGUID; virtual; abstract;',
  19954. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  19955. ' function GetS: TAliasGUIDString; virtual; abstract;',
  19956. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  19957. ' property g: TAliasGUID read GetG write SetG;',
  19958. ' property s: TAliasGUIDString read GetS write SetS;',
  19959. ' end;',
  19960. 'var o: TObject;',
  19961. 'begin',
  19962. ' o.g:=IUnknown;',
  19963. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19964. ' o.s:=IUnknown;',
  19965. ' o.s:=o.g;',
  19966. '']);
  19967. ConvertProgram;
  19968. CheckSource('TestClassInterface_GUIDProperty',
  19969. LinesToStr([ // statements
  19970. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19971. 'rtl.recNewT($mod, "TGUID", function () {',
  19972. ' this.D1 = 0;',
  19973. ' this.D2 = 0;',
  19974. ' this.D3 = 0;',
  19975. ' this.D4 = 0;',
  19976. ' this.$eq = function (b) {',
  19977. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19978. ' };',
  19979. ' this.$assign = function (s) {',
  19980. ' this.D1 = s.D1;',
  19981. ' this.D2 = s.D2;',
  19982. ' this.D3 = s.D3;',
  19983. ' this.D4 = s.D4;',
  19984. ' return this;',
  19985. ' };',
  19986. '});',
  19987. 'rtl.createClass($mod, "TObject", null, function () {',
  19988. ' this.$init = function () {',
  19989. ' };',
  19990. ' this.$final = function () {',
  19991. ' };',
  19992. '});',
  19993. 'this.o = null;',
  19994. '']),
  19995. LinesToStr([ // $mod.$main
  19996. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  19997. '$mod.o.SetG({',
  19998. ' D1: 0xD91C9AF4,',
  19999. ' D2: 0x3C93,',
  20000. ' D3: 0x420F,',
  20001. ' D4: [',
  20002. ' 0xA3,',
  20003. ' 0x03,',
  20004. ' 0xBF,',
  20005. ' 0x5B,',
  20006. ' 0xA8,',
  20007. ' 0x2B,',
  20008. ' 0xFD,',
  20009. ' 0x23',
  20010. ' ]',
  20011. '});',
  20012. '$mod.o.SetS($mod.IUnknown.$guid);',
  20013. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20014. '']));
  20015. end;
  20016. procedure TTestModule.TestClassHelper_ClassVar;
  20017. begin
  20018. StartProgram(false);
  20019. Add([
  20020. 'type',
  20021. ' TObject = class',
  20022. ' end;',
  20023. ' THelper = class helper for TObject',
  20024. ' const',
  20025. ' One = 1;',
  20026. ' Two: word = 2;',
  20027. ' class var',
  20028. ' Glob: word;',
  20029. ' function Foo(w: word): word;',
  20030. ' class function Bar(w: word): word;',
  20031. ' end;',
  20032. 'function THelper.foo(w: word): word;',
  20033. 'begin',
  20034. ' Result:=w;',
  20035. ' Two:=One+w;',
  20036. ' Glob:=Glob;',
  20037. ' Result:=Self.Glob;',
  20038. ' Self.Glob:=Self.Glob;',
  20039. ' with Self do Glob:=Glob;',
  20040. 'end;',
  20041. 'class function THelper.bar(w: word): word;',
  20042. 'begin',
  20043. ' Result:=w;',
  20044. ' Two:=One;',
  20045. ' Glob:=Glob;',
  20046. ' Self.Glob:=Self.Glob;',
  20047. ' with Self do Glob:=Glob;',
  20048. 'end;',
  20049. 'var o: TObject;',
  20050. 'begin',
  20051. ' tobject.two:=tobject.one;',
  20052. ' tobject.Glob:=tobject.Glob;',
  20053. ' with tobject do begin',
  20054. ' two:=one;',
  20055. ' Glob:=Glob;',
  20056. ' end;',
  20057. ' o.two:=o.one;',
  20058. ' o.Glob:=o.Glob;',
  20059. ' with o do begin',
  20060. ' two:=one;',
  20061. ' Glob:=Glob;',
  20062. ' end;',
  20063. '']);
  20064. ConvertProgram;
  20065. CheckSource('TestClassHelper_ClassVar',
  20066. LinesToStr([ // statements
  20067. 'rtl.createClass($mod, "TObject", null, function () {',
  20068. ' this.$init = function () {',
  20069. ' };',
  20070. ' this.$final = function () {',
  20071. ' };',
  20072. '});',
  20073. 'rtl.createHelper($mod, "THelper", null, function () {',
  20074. ' this.One = 1;',
  20075. ' this.Two = 2;',
  20076. ' this.Glob = 0;',
  20077. ' this.Foo = function (w) {',
  20078. ' var Result = 0;',
  20079. ' Result = w;',
  20080. ' $mod.THelper.Two = 1 + w;',
  20081. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20082. ' Result = $mod.THelper.Glob;',
  20083. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20084. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20085. ' return Result;',
  20086. ' };',
  20087. ' this.Bar = function (w) {',
  20088. ' var Result = 0;',
  20089. ' Result = w;',
  20090. ' $mod.THelper.Two = 1;',
  20091. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20092. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20093. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20094. ' return Result;',
  20095. ' };',
  20096. '});',
  20097. 'this.o = null;',
  20098. '']),
  20099. LinesToStr([ // $mod.$main
  20100. '$mod.THelper.Two = 1;',
  20101. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20102. 'var $with = $mod.TObject;',
  20103. '$mod.THelper.Two = 1;',
  20104. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20105. '$mod.THelper.Two = 1;',
  20106. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20107. 'var $with1 = $mod.o;',
  20108. '$mod.THelper.Two = 1;',
  20109. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20110. '']));
  20111. end;
  20112. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20113. begin
  20114. StartProgram(false);
  20115. Add([
  20116. 'type',
  20117. ' TObject = class',
  20118. ' FSize: word;',
  20119. ' property Size: word read FSize write FSize;',
  20120. ' end;',
  20121. ' THelper = class helper for TObject',
  20122. ' function Foo(w: word = 1): word;',
  20123. ' end;',
  20124. 'function THelper.foo(w: word): word;',
  20125. 'begin',
  20126. ' Result:=Size;',
  20127. ' Size:=Size+2;',
  20128. ' Self.Size:=Self.Size+3;',
  20129. ' FSize:=FSize+4;',
  20130. ' Self.FSize:=Self.FSize+5;',
  20131. ' with Self do begin',
  20132. ' Size:=Size+6;',
  20133. ' FSize:=FSize+7;',
  20134. ' FSize:=FSize+8;',
  20135. ' end;',
  20136. 'end;',
  20137. 'begin',
  20138. '']);
  20139. ConvertProgram;
  20140. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20141. LinesToStr([ // statements
  20142. 'rtl.createClass($mod, "TObject", null, function () {',
  20143. ' this.$init = function () {',
  20144. ' this.FSize = 0;',
  20145. ' };',
  20146. ' this.$final = function () {',
  20147. ' };',
  20148. '});',
  20149. 'rtl.createHelper($mod, "THelper", null, function () {',
  20150. ' this.Foo = function (w) {',
  20151. ' var Result = 0;',
  20152. ' Result = this.FSize;',
  20153. ' this.FSize = this.FSize + 2;',
  20154. ' this.FSize = this.FSize + 3;',
  20155. ' this.FSize = this.FSize + 4;',
  20156. ' this.FSize = this.FSize + 5;',
  20157. ' this.FSize = this.FSize + 6;',
  20158. ' this.FSize = this.FSize + 7;',
  20159. ' this.FSize = this.FSize + 8;',
  20160. ' return Result;',
  20161. ' };',
  20162. '});',
  20163. '']),
  20164. LinesToStr([ // $mod.$main
  20165. '']));
  20166. end;
  20167. procedure TTestModule.TestClassHelper_Method_Call;
  20168. begin
  20169. StartProgram(false);
  20170. Add([
  20171. 'type',
  20172. ' TObject = class',
  20173. ' procedure Run(w: word = 10);',
  20174. ' end;',
  20175. ' THelper = class helper for TObject',
  20176. ' function Foo(w: word = 1): word;',
  20177. ' end;',
  20178. 'procedure TObject.Run(w: word);',
  20179. 'var o: TObject;',
  20180. 'begin',
  20181. ' Foo;',
  20182. ' Foo();',
  20183. ' Foo(2);',
  20184. ' Self.Foo;',
  20185. ' Self.Foo();',
  20186. ' Self.Foo(3);',
  20187. ' with Self do begin',
  20188. ' Foo;',
  20189. ' Foo();',
  20190. ' Foo(4);',
  20191. ' end;',
  20192. ' with o do Foo(5);',
  20193. 'end;',
  20194. 'function THelper.foo(w: word): word;',
  20195. 'begin',
  20196. ' Run;',
  20197. ' Run();',
  20198. ' Run(11);',
  20199. ' Foo;',
  20200. ' Foo();',
  20201. ' Foo(12);',
  20202. ' Self.Foo;',
  20203. ' Self.Foo();',
  20204. ' Self.Foo(13);',
  20205. ' with Self do begin',
  20206. ' Foo;',
  20207. ' Foo();',
  20208. ' Foo(14);',
  20209. ' end;',
  20210. 'end;',
  20211. 'var Obj: TObject;',
  20212. 'begin',
  20213. ' obj.Foo;',
  20214. ' obj.Foo();',
  20215. ' obj.Foo(21);',
  20216. ' with obj do begin',
  20217. ' Foo;',
  20218. ' Foo();',
  20219. ' Foo(22);',
  20220. ' end;',
  20221. '']);
  20222. ConvertProgram;
  20223. CheckSource('TestClassHelper_Method_Call',
  20224. LinesToStr([ // statements
  20225. 'rtl.createClass($mod, "TObject", null, function () {',
  20226. ' this.$init = function () {',
  20227. ' };',
  20228. ' this.$final = function () {',
  20229. ' };',
  20230. ' this.Run = function (w) {',
  20231. ' var o = null;',
  20232. ' $mod.THelper.Foo.call(this, 1);',
  20233. ' $mod.THelper.Foo.call(this, 1);',
  20234. ' $mod.THelper.Foo.call(this, 2);',
  20235. ' $mod.THelper.Foo.call(this, 1);',
  20236. ' $mod.THelper.Foo.call(this, 1);',
  20237. ' $mod.THelper.Foo.call(this, 3);',
  20238. ' $mod.THelper.Foo.call(this, 1);',
  20239. ' $mod.THelper.Foo.call(this, 1);',
  20240. ' $mod.THelper.Foo.call(this, 4);',
  20241. ' $mod.THelper.Foo.call(o, 5);',
  20242. ' };',
  20243. '});',
  20244. 'rtl.createHelper($mod, "THelper", null, function () {',
  20245. ' this.Foo = function (w) {',
  20246. ' var Result = 0;',
  20247. ' this.Run(10);',
  20248. ' this.Run(10);',
  20249. ' this.Run(11);',
  20250. ' $mod.THelper.Foo.call(this, 1);',
  20251. ' $mod.THelper.Foo.call(this, 1);',
  20252. ' $mod.THelper.Foo.call(this, 12);',
  20253. ' $mod.THelper.Foo.call(this, 1);',
  20254. ' $mod.THelper.Foo.call(this, 1);',
  20255. ' $mod.THelper.Foo.call(this, 13);',
  20256. ' $mod.THelper.Foo.call(this, 1);',
  20257. ' $mod.THelper.Foo.call(this, 1);',
  20258. ' $mod.THelper.Foo.call(this, 14);',
  20259. ' return Result;',
  20260. ' };',
  20261. '});',
  20262. 'this.Obj = null;',
  20263. '']),
  20264. LinesToStr([ // $mod.$main
  20265. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20266. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20267. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20268. 'var $with = $mod.Obj;',
  20269. '$mod.THelper.Foo.call($with, 1);',
  20270. '$mod.THelper.Foo.call($with, 1);',
  20271. '$mod.THelper.Foo.call($with, 22);',
  20272. '']));
  20273. end;
  20274. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20275. begin
  20276. StartProgram(false);
  20277. Add([
  20278. 'type',
  20279. ' TObject = class',
  20280. ' procedure Run(w: word = 10);',
  20281. ' end;',
  20282. ' THelper = class helper for TObject',
  20283. ' function Foo(w: word = 1): word;',
  20284. ' end;',
  20285. 'procedure TObject.Run(w: word);',
  20286. ' procedure Sub(Self: TObject);',
  20287. ' begin',
  20288. ' Foo;',
  20289. ' Foo();',
  20290. ' Self.Foo;',
  20291. ' Self.Foo();',
  20292. ' with Self do begin',
  20293. ' Foo;',
  20294. ' Foo();',
  20295. ' end;',
  20296. ' end;',
  20297. 'begin',
  20298. 'end;',
  20299. 'function THelper.foo(w: word): word;',
  20300. ' procedure Sub(Self: TObject);',
  20301. ' begin',
  20302. ' Run;',
  20303. ' Run();',
  20304. ' Foo;',
  20305. ' Foo();',
  20306. ' Self.Foo;',
  20307. ' Self.Foo();',
  20308. ' with Self do begin',
  20309. ' Foo;',
  20310. ' Foo();',
  20311. ' end;',
  20312. ' end;',
  20313. 'begin',
  20314. 'end;',
  20315. 'begin',
  20316. '']);
  20317. ConvertProgram;
  20318. CheckSource('TestClassHelper_Method_Nested_Call',
  20319. LinesToStr([ // statements
  20320. 'rtl.createClass($mod, "TObject", null, function () {',
  20321. ' this.$init = function () {',
  20322. ' };',
  20323. ' this.$final = function () {',
  20324. ' };',
  20325. ' this.Run = function (w) {',
  20326. ' var $Self = this;',
  20327. ' function Sub(Self) {',
  20328. ' $mod.THelper.Foo.call($Self, 1);',
  20329. ' $mod.THelper.Foo.call($Self, 1);',
  20330. ' $mod.THelper.Foo.call(Self, 1);',
  20331. ' $mod.THelper.Foo.call(Self, 1);',
  20332. ' $mod.THelper.Foo.call(Self, 1);',
  20333. ' $mod.THelper.Foo.call(Self, 1);',
  20334. ' };',
  20335. ' };',
  20336. '});',
  20337. 'rtl.createHelper($mod, "THelper", null, function () {',
  20338. ' this.Foo = function (w) {',
  20339. ' var $Self = this;',
  20340. ' var Result = 0;',
  20341. ' function Sub(Self) {',
  20342. ' $Self.Run(10);',
  20343. ' $Self.Run(10);',
  20344. ' $mod.THelper.Foo.call($Self, 1);',
  20345. ' $mod.THelper.Foo.call($Self, 1);',
  20346. ' $mod.THelper.Foo.call(Self, 1);',
  20347. ' $mod.THelper.Foo.call(Self, 1);',
  20348. ' $mod.THelper.Foo.call(Self, 1);',
  20349. ' $mod.THelper.Foo.call(Self, 1);',
  20350. ' };',
  20351. ' return Result;',
  20352. ' };',
  20353. '});',
  20354. '']),
  20355. LinesToStr([ // $mod.$main
  20356. '']));
  20357. end;
  20358. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20359. begin
  20360. StartProgram(false);
  20361. Add([
  20362. 'type',
  20363. ' TObject = class',
  20364. ' class procedure Run(w: word = 10);',
  20365. ' end;',
  20366. ' THelper = class helper for TObject',
  20367. ' class function Foo(w: word = 1): word;',
  20368. ' end;',
  20369. 'class procedure TObject.Run(w: word);',
  20370. 'begin',
  20371. ' Foo;',
  20372. ' Foo();',
  20373. ' Self.Foo;',
  20374. ' Self.Foo();',
  20375. ' with Self do begin',
  20376. ' Foo;',
  20377. ' Foo();',
  20378. ' end;',
  20379. 'end;',
  20380. 'class function THelper.foo(w: word): word;',
  20381. 'begin',
  20382. ' Run;',
  20383. ' Run();',
  20384. ' Foo;',
  20385. ' Foo();',
  20386. ' Self.Foo;',
  20387. ' Self.Foo();',
  20388. ' with Self do begin',
  20389. ' Foo;',
  20390. ' Foo();',
  20391. ' end;',
  20392. 'end;',
  20393. 'var',
  20394. ' Obj: TObject;',
  20395. 'begin',
  20396. ' obj.Foo;',
  20397. ' obj.Foo();',
  20398. ' with obj do begin',
  20399. ' Foo;',
  20400. ' Foo();',
  20401. ' end;',
  20402. ' tobject.Foo;',
  20403. ' tobject.Foo();',
  20404. ' with tobject do begin',
  20405. ' Foo;',
  20406. ' Foo();',
  20407. ' end;',
  20408. '']);
  20409. ConvertProgram;
  20410. CheckSource('TestClassHelper_ClassMethod_Call',
  20411. LinesToStr([ // statements
  20412. 'rtl.createClass($mod, "TObject", null, function () {',
  20413. ' this.$init = function () {',
  20414. ' };',
  20415. ' this.$final = function () {',
  20416. ' };',
  20417. ' this.Run = function (w) {',
  20418. ' $mod.THelper.Foo.call(this, 1);',
  20419. ' $mod.THelper.Foo.call(this, 1);',
  20420. ' $mod.THelper.Foo.call(this, 1);',
  20421. ' $mod.THelper.Foo.call(this, 1);',
  20422. ' $mod.THelper.Foo.call(this, 1);',
  20423. ' $mod.THelper.Foo.call(this, 1);',
  20424. ' };',
  20425. '});',
  20426. 'rtl.createHelper($mod, "THelper", null, function () {',
  20427. ' this.Foo = function (w) {',
  20428. ' var Result = 0;',
  20429. ' this.Run(10);',
  20430. ' this.Run(10);',
  20431. ' $mod.THelper.Foo.call(this, 1);',
  20432. ' $mod.THelper.Foo.call(this, 1);',
  20433. ' $mod.THelper.Foo.call(this, 1);',
  20434. ' $mod.THelper.Foo.call(this, 1);',
  20435. ' $mod.THelper.Foo.call(this, 1);',
  20436. ' $mod.THelper.Foo.call(this, 1);',
  20437. ' return Result;',
  20438. ' };',
  20439. '});',
  20440. 'this.Obj = null;',
  20441. '']),
  20442. LinesToStr([ // $mod.$main
  20443. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20444. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20445. 'var $with = $mod.Obj;',
  20446. '$mod.THelper.Foo.call($with.$class, 1);',
  20447. '$mod.THelper.Foo.call($with.$class, 1);',
  20448. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20449. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20450. 'var $with1 = $mod.TObject;',
  20451. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20452. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20453. '']));
  20454. end;
  20455. procedure TTestModule.TestClassHelper_ClassOf;
  20456. begin
  20457. StartProgram(false);
  20458. Add([
  20459. 'type',
  20460. ' TObject = class',
  20461. ' end;',
  20462. ' TClass = class of TObject;',
  20463. ' THelper = class helper for TObject',
  20464. ' class function Foo(w: word = 1): word;',
  20465. ' end;',
  20466. 'class function THelper.foo(w: word): word;',
  20467. 'begin',
  20468. 'end;',
  20469. 'var',
  20470. ' c: TClass;',
  20471. 'begin',
  20472. ' c.Foo;',
  20473. ' c.Foo();',
  20474. ' with c do begin',
  20475. ' Foo;',
  20476. ' Foo();',
  20477. ' end;',
  20478. '']);
  20479. ConvertProgram;
  20480. CheckSource('TestClassHelper_ClassOf',
  20481. LinesToStr([ // statements
  20482. 'rtl.createClass($mod, "TObject", null, function () {',
  20483. ' this.$init = function () {',
  20484. ' };',
  20485. ' this.$final = function () {',
  20486. ' };',
  20487. '});',
  20488. 'rtl.createHelper($mod, "THelper", null, function () {',
  20489. ' this.Foo = function (w) {',
  20490. ' var Result = 0;',
  20491. ' return Result;',
  20492. ' };',
  20493. '});',
  20494. 'this.c = null;',
  20495. '']),
  20496. LinesToStr([ // $mod.$main
  20497. '$mod.THelper.Foo.call($mod.c, 1);',
  20498. '$mod.THelper.Foo.call($mod.c, 1);',
  20499. 'var $with = $mod.c;',
  20500. '$mod.THelper.Foo.call($with, 1);',
  20501. '$mod.THelper.Foo.call($with, 1);',
  20502. '']));
  20503. end;
  20504. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  20505. begin
  20506. StartProgram(false);
  20507. Add([
  20508. '{$mode objfpc}',
  20509. 'type',
  20510. ' TObject = class',
  20511. ' procedure DoIt;',
  20512. ' end;',
  20513. ' THelper = class helper for TObject',
  20514. ' procedure Fly(w: word = 1);',
  20515. ' class procedure Glide(w: word = 1);',
  20516. ' class procedure Run(w: word = 1); static;',
  20517. ' end;',
  20518. ' TFly = procedure(w: word) of object;',
  20519. ' TGlide = TFly;',
  20520. ' TRun = procedure(w: word);',
  20521. 'var',
  20522. ' f: TFly;',
  20523. ' g: TGlide;',
  20524. ' r: TRun;',
  20525. 'procedure TObject.DoIt;',
  20526. 'begin',
  20527. ' f:=@fly;',
  20528. ' g:=@glide;',
  20529. ' r:=@run;',
  20530. ' f:[email protected];',
  20531. ' g:[email protected];',
  20532. ' r:[email protected];',
  20533. ' with self do begin',
  20534. ' f:=@fly;',
  20535. ' g:=@glide;',
  20536. ' r:=@run;',
  20537. ' end;',
  20538. 'end;',
  20539. 'procedure THelper.fly(w: word);',
  20540. 'begin',
  20541. ' f:=@fly;',
  20542. ' g:=@glide;',
  20543. ' r:=@run;',
  20544. 'end;',
  20545. 'class procedure THelper.glide(w: word);',
  20546. 'begin',
  20547. ' g:=@glide;',
  20548. ' r:=@run;',
  20549. 'end;',
  20550. 'class procedure THelper.run(w: word);',
  20551. 'begin',
  20552. ' g:=@glide;',
  20553. ' r:=@run;',
  20554. 'end;',
  20555. 'var',
  20556. ' Obj: TObject;',
  20557. 'begin',
  20558. ' f:[email protected];',
  20559. ' g:[email protected];',
  20560. ' r:[email protected];',
  20561. ' with obj do begin',
  20562. ' f:=@fly;',
  20563. ' g:=@glide;',
  20564. ' r:=@run;',
  20565. ' end;',
  20566. ' g:[email protected];',
  20567. ' r:[email protected];',
  20568. ' with tobject do begin',
  20569. ' g:=@glide;',
  20570. ' r:=@run;',
  20571. ' end;',
  20572. '']);
  20573. ConvertProgram;
  20574. CheckSource('TestClassHelper_MethodRefObjFPC',
  20575. LinesToStr([ // statements
  20576. 'rtl.createClass($mod, "TObject", null, function () {',
  20577. ' this.$init = function () {',
  20578. ' };',
  20579. ' this.$final = function () {',
  20580. ' };',
  20581. ' this.DoIt = function () {',
  20582. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20583. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20584. ' $mod.r = $mod.THelper.Run;',
  20585. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20586. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20587. ' $mod.r = $mod.THelper.Run;',
  20588. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20589. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20590. ' $mod.r = $mod.THelper.Run;',
  20591. ' };',
  20592. '});',
  20593. 'rtl.createHelper($mod, "THelper", null, function () {',
  20594. ' this.Fly = function (w) {',
  20595. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20596. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20597. ' $mod.r = $mod.THelper.Run;',
  20598. ' };',
  20599. ' this.Glide = function (w) {',
  20600. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  20601. ' $mod.r = $mod.THelper.Run;',
  20602. ' };',
  20603. ' this.Run = function (w) {',
  20604. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  20605. ' $mod.r = $mod.THelper.Run;',
  20606. ' };',
  20607. '});',
  20608. 'this.f = null;',
  20609. 'this.g = null;',
  20610. 'this.r = null;',
  20611. 'this.Obj = null;',
  20612. '']),
  20613. LinesToStr([ // $mod.$main
  20614. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  20615. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  20616. '$mod.r = $mod.THelper.Run;',
  20617. 'var $with = $mod.Obj;',
  20618. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  20619. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  20620. '$mod.r = $mod.THelper.Run;',
  20621. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  20622. '$mod.r = $mod.THelper.Run;',
  20623. 'var $with1 = $mod.TObject;',
  20624. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  20625. '$mod.r = $mod.THelper.Run;',
  20626. '']));
  20627. end;
  20628. procedure TTestModule.TestClassHelper_Constructor;
  20629. begin
  20630. StartProgram(false);
  20631. Add([
  20632. 'type',
  20633. ' TObject = class',
  20634. ' constructor Create;',
  20635. ' end;',
  20636. ' TClass = class of TObject;',
  20637. ' THelper = class helper for TObject',
  20638. ' constructor NewHlp(w: word);',
  20639. ' end;',
  20640. 'var',
  20641. ' obj: TObject;',
  20642. ' c: TClass;',
  20643. 'constructor TObject.Create;',
  20644. 'begin',
  20645. ' NewHlp(2);', // normal call
  20646. ' tobject.NewHlp(3);', // new instance
  20647. ' c.newhlp(4);', // new instance
  20648. 'end;',
  20649. 'constructor THelper.NewHlp(w: word);',
  20650. 'begin',
  20651. ' create;', // normal call
  20652. ' tobject.create;', // new instance
  20653. ' NewHlp(2);', // normal call
  20654. ' tobject.NewHlp(3);', // new instance
  20655. ' c.newhlp(4);', // new instance
  20656. 'end;',
  20657. 'begin',
  20658. ' obj.newhlp(2);', // normal call
  20659. ' with Obj do newhlp(12);', // normal call
  20660. ' tobject.newhlp(3);', // new instance
  20661. ' with tobject do newhlp(13);', // new instance
  20662. ' c.newhlp(4);', // new instance
  20663. ' with c do newhlp(14);', // new instance
  20664. '']);
  20665. ConvertProgram;
  20666. CheckSource('TestClassHelper_Constructor',
  20667. LinesToStr([ // statements
  20668. 'rtl.createClass($mod, "TObject", null, function () {',
  20669. ' this.$init = function () {',
  20670. ' };',
  20671. ' this.$final = function () {',
  20672. ' };',
  20673. ' this.Create = function () {',
  20674. ' $mod.THelper.NewHlp.call(this, 2);',
  20675. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20676. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20677. ' return this;',
  20678. ' };',
  20679. '});',
  20680. 'rtl.createHelper($mod, "THelper", null, function () {',
  20681. ' this.NewHlp = function (w) {',
  20682. ' this.Create();',
  20683. ' $mod.TObject.$create("Create");',
  20684. ' $mod.THelper.NewHlp.call(this, 2);',
  20685. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20686. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20687. ' return this;',
  20688. ' };',
  20689. '});',
  20690. 'this.obj = null;',
  20691. 'this.c = null;',
  20692. '']),
  20693. LinesToStr([ // $mod.$main
  20694. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  20695. 'var $with = $mod.obj;',
  20696. '$mod.THelper.NewHlp.call($with, 12);',
  20697. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20698. 'var $with1 = $mod.TObject;',
  20699. '$with1.$create($mod.THelper.NewHlp, [13]);',
  20700. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  20701. 'var $with2 = $mod.c;',
  20702. '$with2.$create($mod.THelper.NewHlp, [14]);',
  20703. '']));
  20704. end;
  20705. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  20706. begin
  20707. StartProgram(false);
  20708. Add([
  20709. 'type',
  20710. ' TObject = class',
  20711. ' procedure Fly;',
  20712. ' end;',
  20713. ' TObjHelper = class helper for TObject',
  20714. ' procedure Fly;',
  20715. ' end;',
  20716. ' TBird = class',
  20717. ' procedure Fly;',
  20718. ' end;',
  20719. ' TBirdHelper = class helper for TBird',
  20720. ' procedure Fly;',
  20721. ' procedure Walk(w: word);',
  20722. ' end;',
  20723. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  20724. ' procedure Fly;',
  20725. ' procedure Walk(w: word);',
  20726. ' end;',
  20727. 'procedure Tobject.fly;',
  20728. 'begin',
  20729. ' inherited;', // ignore
  20730. 'end;',
  20731. 'procedure Tobjhelper.fly;',
  20732. 'begin',
  20733. ' {@TObject_Fly}inherited;',
  20734. ' inherited {@TObject_Fly}Fly;',
  20735. 'end;',
  20736. 'procedure Tbird.fly;',
  20737. 'begin',
  20738. ' {@TObjHelper_Fly}inherited;',
  20739. ' inherited {@TObjHelper_Fly}Fly;',
  20740. 'end;',
  20741. 'procedure Tbirdhelper.fly;',
  20742. 'begin',
  20743. ' {@TBird_Fly}inherited;',
  20744. ' inherited {@TBird_Fly}Fly;',
  20745. 'end;',
  20746. 'procedure Tbirdhelper.walk(w: word);',
  20747. 'begin',
  20748. 'end;',
  20749. 'procedure teagleHelper.fly;',
  20750. 'begin',
  20751. ' {@TBird_Fly}inherited;',
  20752. ' inherited {@TBird_Fly}Fly;',
  20753. 'end;',
  20754. 'procedure teagleHelper.walk(w: word);',
  20755. 'begin',
  20756. ' {@TBirdHelper_Walk}inherited;',
  20757. ' inherited {@TBirdHelper_Walk}Walk(3);',
  20758. 'end;',
  20759. 'begin',
  20760. '']);
  20761. ConvertProgram;
  20762. CheckSource('TestClassHelper_InheritedObjFPC',
  20763. LinesToStr([ // statements
  20764. 'rtl.createClass($mod, "TObject", null, function () {',
  20765. ' this.$init = function () {',
  20766. ' };',
  20767. ' this.$final = function () {',
  20768. ' };',
  20769. ' this.Fly = function () {',
  20770. ' };',
  20771. '});',
  20772. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20773. ' this.Fly = function () {',
  20774. ' $mod.TObject.Fly.call(this);',
  20775. ' $mod.TObject.Fly.call(this);',
  20776. ' };',
  20777. '});',
  20778. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20779. ' this.Fly$1 = function () {',
  20780. ' $mod.TObjHelper.Fly.call(this);',
  20781. ' $mod.TObjHelper.Fly.call(this);',
  20782. ' };',
  20783. '});',
  20784. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20785. ' this.Fly = function () {',
  20786. ' $mod.TBird.Fly$1.call(this);',
  20787. ' $mod.TBird.Fly$1.call(this);',
  20788. ' };',
  20789. ' this.Walk = function (w) {',
  20790. ' };',
  20791. '});',
  20792. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  20793. ' this.Fly$1 = function () {',
  20794. ' $mod.TBird.Fly$1.call(this);',
  20795. ' $mod.TBird.Fly$1.call(this);',
  20796. ' };',
  20797. ' this.Walk$1 = function (w) {',
  20798. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  20799. ' $mod.TBirdHelper.Walk.call(this, 3);',
  20800. ' };',
  20801. '});',
  20802. '']),
  20803. LinesToStr([ // $mod.$main
  20804. '']));
  20805. end;
  20806. procedure TTestModule.TestClassHelper_Property;
  20807. begin
  20808. StartProgram(false);
  20809. Add([
  20810. 'type',
  20811. ' TObject = class',
  20812. ' FSize: word;',
  20813. ' function GetSpeed: word;',
  20814. ' procedure SetSpeed(Value: word);',
  20815. ' end;',
  20816. ' TObjHelper = class helper for TObject',
  20817. ' function GetLeft: word;',
  20818. ' procedure SetLeft(Value: word);',
  20819. ' property Size: word read FSize write FSize;',
  20820. ' property Speed: word read GetSpeed write SetSpeed;',
  20821. ' property Left: word read GetLeft write SetLeft;',
  20822. ' end;',
  20823. ' TBird = class',
  20824. ' property NotRight: word read GetLeft write SetLeft;',
  20825. ' procedure DoIt;',
  20826. ' end;',
  20827. 'var',
  20828. ' b: TBird;',
  20829. 'function Tobject.GetSpeed: word;',
  20830. 'begin',
  20831. ' Size:=Size+11;',
  20832. ' Speed:=Speed+12;',
  20833. ' Result:=Left+13;',
  20834. ' Left:=13;',
  20835. ' Left:=Left+13;',
  20836. ' Self.Size:=Self.Size+21;',
  20837. ' Self.Speed:=Self.Speed+22;',
  20838. ' Self.Left:=Self.Left+23;',
  20839. ' with Self do begin',
  20840. ' Size:=Size+31;',
  20841. ' Speed:=Speed+32;',
  20842. ' Left:=Left+33;',
  20843. ' end;',
  20844. 'end;',
  20845. 'procedure Tobject.SetSpeed(Value: word);',
  20846. 'begin',
  20847. 'end;',
  20848. 'function TObjHelper.GetLeft: word;',
  20849. 'begin',
  20850. ' Size:=Size+11;',
  20851. ' Speed:=Speed+12;',
  20852. ' Left:=Left+13;',
  20853. ' Self.Size:=Self.Size+21;',
  20854. ' Self.Speed:=Self.Speed+22;',
  20855. ' Self.Left:=Self.Left+23;',
  20856. ' with Self do begin',
  20857. ' Size:=Size+31;',
  20858. ' Speed:=Speed+32;',
  20859. ' Left:=Left+33;',
  20860. ' end;',
  20861. 'end;',
  20862. 'procedure TObjHelper.SetLeft(Value: word);',
  20863. 'begin',
  20864. 'end;',
  20865. 'procedure TBird.DoIt;',
  20866. 'begin',
  20867. ' NotRight:=NotRight+11;',
  20868. ' Self.NotRight:=Self.NotRight+21;',
  20869. ' with Self do begin',
  20870. ' NotRight:=NotRight+31;',
  20871. ' end;',
  20872. 'end;',
  20873. 'begin',
  20874. ' b.Size:=b.Size+11;',
  20875. ' b.Speed:=b.Speed+12;',
  20876. ' b.Left:=b.Left+13;',
  20877. ' b.NotRight:=b.NotRight+14;',
  20878. ' with b do begin',
  20879. ' Size:=Size+31;',
  20880. ' Speed:=Speed+32;',
  20881. ' Left:=Left+33;',
  20882. ' NotRight:=NotRight+34;',
  20883. ' end;',
  20884. '']);
  20885. ConvertProgram;
  20886. CheckSource('TestClassHelper_Property',
  20887. LinesToStr([ // statements
  20888. 'rtl.createClass($mod, "TObject", null, function () {',
  20889. ' this.$init = function () {',
  20890. ' this.FSize = 0;',
  20891. ' };',
  20892. ' this.$final = function () {',
  20893. ' };',
  20894. ' this.GetSpeed = function () {',
  20895. ' var Result = 0;',
  20896. ' this.FSize = this.FSize + 11;',
  20897. ' this.SetSpeed(this.GetSpeed() + 12);',
  20898. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  20899. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  20900. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20901. ' this.FSize = this.FSize + 21;',
  20902. ' this.SetSpeed(this.GetSpeed() + 22);',
  20903. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20904. ' this.FSize = this.FSize + 31;',
  20905. ' this.SetSpeed(this.GetSpeed() + 32);',
  20906. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20907. ' return Result;',
  20908. ' };',
  20909. ' this.SetSpeed = function (Value) {',
  20910. ' };',
  20911. '});',
  20912. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20913. ' this.GetLeft = function () {',
  20914. ' var Result = 0;',
  20915. ' this.FSize = this.FSize + 11;',
  20916. ' this.SetSpeed(this.GetSpeed() + 12);',
  20917. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20918. ' this.FSize = this.FSize + 21;',
  20919. ' this.SetSpeed(this.GetSpeed() + 22);',
  20920. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20921. ' this.FSize = this.FSize + 31;',
  20922. ' this.SetSpeed(this.GetSpeed() + 32);',
  20923. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20924. ' return Result;',
  20925. ' };',
  20926. ' this.SetLeft = function (Value) {',
  20927. ' };',
  20928. '});',
  20929. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20930. ' this.DoIt = function () {',
  20931. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20932. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20933. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20934. ' };',
  20935. '});',
  20936. 'this.b = null;',
  20937. '']),
  20938. LinesToStr([ // $mod.$main
  20939. '$mod.b.FSize = $mod.b.FSize + 11;',
  20940. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20941. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  20942. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  20943. 'var $with = $mod.b;',
  20944. '$with.FSize = $with.FSize + 31;',
  20945. '$with.SetSpeed($with.GetSpeed() + 32);',
  20946. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  20947. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  20948. '']));
  20949. end;
  20950. procedure TTestModule.TestClassHelper_Property_Array;
  20951. begin
  20952. StartProgram(false);
  20953. Add([
  20954. 'type',
  20955. ' TObject = class',
  20956. ' function GetSpeed(Index: boolean): word;',
  20957. ' procedure SetSpeed(Index: boolean; Value: word);',
  20958. ' end;',
  20959. ' TObjHelper = class helper for TObject',
  20960. ' function GetSize(Index: boolean): word;',
  20961. ' procedure SetSize(Index: boolean; Value: word);',
  20962. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  20963. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20964. ' end;',
  20965. ' TBird = class',
  20966. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  20967. ' procedure DoIt;',
  20968. ' end;',
  20969. 'var',
  20970. ' b: TBird;',
  20971. 'function Tobject.GetSpeed(Index: boolean): word;',
  20972. 'begin',
  20973. ' Result:=Size[false];',
  20974. ' Size[true]:=Size[false]+11;',
  20975. ' Speed[true]:=Speed[false]+12;',
  20976. ' Self.Size[true]:=Self.Size[false]+21;',
  20977. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20978. ' with Self do begin',
  20979. ' Size[true]:=Size[false]+31;',
  20980. ' Speed[true]:=Speed[false]+32;',
  20981. ' end;',
  20982. 'end;',
  20983. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20984. 'begin',
  20985. 'end;',
  20986. 'function TObjHelper.GetSize(Index: boolean): word;',
  20987. 'begin',
  20988. ' Size[true]:=Size[false]+11;',
  20989. ' Speed[true]:=Speed[false]+12;',
  20990. ' Self.Size[true]:=Self.Size[false]+21;',
  20991. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20992. ' with Self do begin',
  20993. ' Size[true]:=Size[false]+31;',
  20994. ' Speed[true]:=Speed[false]+32;',
  20995. ' end;',
  20996. 'end;',
  20997. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20998. 'begin',
  20999. 'end;',
  21000. 'procedure TBird.DoIt;',
  21001. 'begin',
  21002. ' Items[true]:=Items[false]+11;',
  21003. ' Self.Items[true]:=Self.Items[false]+21;',
  21004. ' with Self do Items[true]:=Items[false]+31;',
  21005. 'end;',
  21006. 'begin',
  21007. ' b.Size[true]:=b.Size[false]+11;',
  21008. ' b.Speed[true]:=b.Speed[false]+12;',
  21009. ' b.Items[true]:=b.Items[false]+13;',
  21010. ' with b do begin',
  21011. ' Size[true]:=Size[false]+21;',
  21012. ' Speed[true]:=Speed[false]+22;',
  21013. ' Items[true]:=Items[false]+23;',
  21014. ' end;',
  21015. '']);
  21016. ConvertProgram;
  21017. CheckSource('TestClassHelper_Property_Array',
  21018. LinesToStr([ // statements
  21019. 'rtl.createClass($mod, "TObject", null, function () {',
  21020. ' this.$init = function () {',
  21021. ' };',
  21022. ' this.$final = function () {',
  21023. ' };',
  21024. ' this.GetSpeed = function (Index) {',
  21025. ' var Result = 0;',
  21026. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21027. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21028. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21029. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21030. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21031. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21032. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21033. ' return Result;',
  21034. ' };',
  21035. ' this.SetSpeed = function (Index, Value) {',
  21036. ' };',
  21037. '});',
  21038. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21039. ' this.GetSize = function (Index) {',
  21040. ' var Result = 0;',
  21041. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21042. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21043. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21044. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21045. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21046. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21047. ' return Result;',
  21048. ' };',
  21049. ' this.SetSize = function (Index, Value) {',
  21050. ' };',
  21051. '});',
  21052. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21053. ' this.DoIt = function () {',
  21054. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21055. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21056. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21057. ' };',
  21058. '});',
  21059. 'this.b = null;',
  21060. '']),
  21061. LinesToStr([ // $mod.$main
  21062. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21063. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21064. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21065. 'var $with = $mod.b;',
  21066. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21067. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21068. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21069. '']));
  21070. end;
  21071. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21072. begin
  21073. StartProgram(false);
  21074. Add([
  21075. 'type',
  21076. ' TObject = class',
  21077. ' function GetSpeed(Index: boolean): word;',
  21078. ' procedure SetSpeed(Index: boolean; Value: word);',
  21079. ' end;',
  21080. ' TObjHelper = class helper for TObject',
  21081. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21082. ' end;',
  21083. ' TBird = class',
  21084. ' end;',
  21085. ' TBirdHelper = class helper for TBird',
  21086. ' function GetSize(Index: word): boolean;',
  21087. ' procedure SetSize(Index: word; Value: boolean);',
  21088. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21089. ' end;',
  21090. 'function Tobject.GetSpeed(Index: boolean): word;',
  21091. 'begin',
  21092. ' Self[true]:=Self[false]+1;',
  21093. 'end;',
  21094. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21095. 'begin',
  21096. 'end;',
  21097. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21098. 'begin',
  21099. ' Self[1]:=not Self[2];',
  21100. 'end;',
  21101. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21102. 'begin',
  21103. 'end;',
  21104. 'var',
  21105. ' o: TObject;',
  21106. ' b: TBird;',
  21107. 'begin',
  21108. ' o[true]:=o[false]+1;',
  21109. ' b[3]:=not b[4];',
  21110. '']);
  21111. ConvertProgram;
  21112. CheckSource('TestClassHelper_Property_Array_Default',
  21113. LinesToStr([ // statements
  21114. 'rtl.createClass($mod, "TObject", null, function () {',
  21115. ' this.$init = function () {',
  21116. ' };',
  21117. ' this.$final = function () {',
  21118. ' };',
  21119. ' this.GetSpeed = function (Index) {',
  21120. ' var Result = 0;',
  21121. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21122. ' return Result;',
  21123. ' };',
  21124. ' this.SetSpeed = function (Index, Value) {',
  21125. ' };',
  21126. '});',
  21127. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21128. '});',
  21129. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21130. '});',
  21131. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  21132. ' this.GetSize = function (Index) {',
  21133. ' var Result = false;',
  21134. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21135. ' return Result;',
  21136. ' };',
  21137. ' this.SetSize = function (Index, Value) {',
  21138. ' };',
  21139. '});',
  21140. 'this.o = null;',
  21141. 'this.b = null;',
  21142. '']),
  21143. LinesToStr([ // $mod.$main
  21144. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21145. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21146. '']));
  21147. end;
  21148. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21149. begin
  21150. StartProgram(false);
  21151. Add([
  21152. 'type',
  21153. ' TObject = class',
  21154. ' end;',
  21155. ' TObjHelper = class helper for TObject',
  21156. ' function GetItems(Index: word): TObject;',
  21157. ' procedure SetItems(Index: word; Value: TObject);',
  21158. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21159. ' end;',
  21160. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21161. 'begin',
  21162. ' Self[1][2]:=Self[3][4];',
  21163. 'end;',
  21164. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21165. 'begin',
  21166. 'end;',
  21167. 'var',
  21168. ' o: TObject;',
  21169. 'begin',
  21170. ' o[1][2]:=o[3][4];',
  21171. '']);
  21172. ConvertProgram;
  21173. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21174. LinesToStr([ // statements
  21175. 'rtl.createClass($mod, "TObject", null, function () {',
  21176. ' this.$init = function () {',
  21177. ' };',
  21178. ' this.$final = function () {',
  21179. ' };',
  21180. '});',
  21181. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21182. ' this.GetItems = function (Index) {',
  21183. ' var Result = null;',
  21184. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21185. ' return Result;',
  21186. ' };',
  21187. ' this.SetItems = function (Index, Value) {',
  21188. ' };',
  21189. '});',
  21190. 'this.o = null;',
  21191. '']),
  21192. LinesToStr([ // $mod.$main
  21193. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21194. '']));
  21195. end;
  21196. procedure TTestModule.TestClassHelper_ClassProperty;
  21197. begin
  21198. StartProgram(false);
  21199. Add([
  21200. 'type',
  21201. ' TObject = class',
  21202. ' class var FSize: word;',
  21203. ' class function GetSpeed: word;',
  21204. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21205. ' end;',
  21206. ' TObjHelper = class helper for TObject',
  21207. ' class function GetLeft: word;',
  21208. ' class procedure SetLeft(Value: word);',
  21209. ' class property Size: word read FSize write FSize;',
  21210. ' class property Speed: word read GetSpeed write SetSpeed;',
  21211. ' class property Left: word read GetLeft write SetLeft;',
  21212. ' end;',
  21213. ' TBird = class',
  21214. ' class property NotRight: word read GetLeft write SetLeft;',
  21215. ' class procedure DoIt;',
  21216. ' end;',
  21217. ' TBirdClass = class of TBird;',
  21218. 'class function Tobject.GetSpeed: word;',
  21219. 'begin',
  21220. ' Size:=Size+11;',
  21221. ' Speed:=Speed+12;',
  21222. ' Left:=Left+13;',
  21223. ' Self.Size:=Self.Size+21;',
  21224. ' Self.Speed:=Self.Speed+22;',
  21225. ' Self.Left:=Self.Left+23;',
  21226. ' with Self do begin',
  21227. ' Size:=Size+31;',
  21228. ' Speed:=Speed+32;',
  21229. ' Left:=Left+33;',
  21230. ' end;',
  21231. 'end;',
  21232. 'class function TObjHelper.GetLeft: word;',
  21233. 'begin',
  21234. ' Size:=Size+11;',
  21235. ' Speed:=Speed+12;',
  21236. ' Left:=Left+13;',
  21237. ' Self.Size:=Self.Size+21;',
  21238. ' Self.Speed:=Self.Speed+22;',
  21239. ' Self.Left:=Self.Left+23;',
  21240. ' with Self do begin',
  21241. ' Size:=Size+31;',
  21242. ' Speed:=Speed+32;',
  21243. ' Left:=Left+33;',
  21244. ' end;',
  21245. 'end;',
  21246. 'class procedure TObjHelper.SetLeft(Value: word);',
  21247. 'begin',
  21248. 'end;',
  21249. 'class procedure TBird.DoIt;',
  21250. 'begin',
  21251. ' NotRight:=NotRight+11;',
  21252. ' Self.NotRight:=Self.NotRight+21;',
  21253. ' with Self do NotRight:=NotRight+31;',
  21254. 'end;',
  21255. 'var',
  21256. ' b: TBird;',
  21257. ' c: TBirdClass;',
  21258. 'begin',
  21259. ' b.Size:=b.Size+11;',
  21260. ' b.Speed:=b.Speed+12;',
  21261. ' b.Left:=b.Left+13;',
  21262. ' b.NotRight:=b.NotRight+14;',
  21263. ' with b do begin',
  21264. ' Size:=Size+31;',
  21265. ' Speed:=Speed+32;',
  21266. ' Left:=Left+33;',
  21267. ' NotRight:=NotRight+34;',
  21268. ' end;',
  21269. ' c.Size:=c.Size+11;',
  21270. ' c.Speed:=c.Speed+12;',
  21271. ' c.Left:=c.Left+13;',
  21272. ' c.NotRight:=c.NotRight+14;',
  21273. ' with c do begin',
  21274. ' Size:=Size+31;',
  21275. ' Speed:=Speed+32;',
  21276. ' Left:=Left+33;',
  21277. ' NotRight:=NotRight+34;',
  21278. ' end;',
  21279. ' tbird.Size:=tbird.Size+11;',
  21280. ' tbird.Speed:=tbird.Speed+12;',
  21281. ' tbird.Left:=tbird.Left+13;',
  21282. ' tbird.NotRight:=tbird.NotRight+14;',
  21283. ' with tbird do begin',
  21284. ' Size:=Size+31;',
  21285. ' Speed:=Speed+32;',
  21286. ' Left:=Left+33;',
  21287. ' NotRight:=NotRight+34;',
  21288. ' end;',
  21289. '']);
  21290. ConvertProgram;
  21291. CheckSource('TestClassHelper_ClassProperty',
  21292. LinesToStr([ // statements
  21293. 'rtl.createClass($mod, "TObject", null, function () {',
  21294. ' this.FSize = 0;',
  21295. ' this.$init = function () {',
  21296. ' };',
  21297. ' this.$final = function () {',
  21298. ' };',
  21299. ' this.GetSpeed = function () {',
  21300. ' var Result = 0;',
  21301. ' $mod.TObject.FSize = this.FSize + 11;',
  21302. ' this.SetSpeed(this.GetSpeed() + 12);',
  21303. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21304. ' $mod.TObject.FSize = this.FSize + 21;',
  21305. ' this.SetSpeed(this.GetSpeed() + 22);',
  21306. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21307. ' $mod.TObject.FSize = this.FSize + 31;',
  21308. ' this.SetSpeed(this.GetSpeed() + 32);',
  21309. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21310. ' return Result;',
  21311. ' };',
  21312. '});',
  21313. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21314. ' this.GetLeft = function () {',
  21315. ' var Result = 0;',
  21316. ' $mod.TObject.FSize = this.FSize + 11;',
  21317. ' this.SetSpeed(this.GetSpeed() + 12);',
  21318. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21319. ' $mod.TObject.FSize = this.FSize + 21;',
  21320. ' this.SetSpeed(this.GetSpeed() + 22);',
  21321. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21322. ' $mod.TObject.FSize = this.FSize + 31;',
  21323. ' this.SetSpeed(this.GetSpeed() + 32);',
  21324. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21325. ' return Result;',
  21326. ' };',
  21327. ' this.SetLeft = function (Value) {',
  21328. ' };',
  21329. '});',
  21330. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21331. ' this.DoIt = function () {',
  21332. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21333. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21334. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21335. ' };',
  21336. '});',
  21337. 'this.b = null;',
  21338. 'this.c = null;',
  21339. '']),
  21340. LinesToStr([ // $mod.$main
  21341. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21342. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21343. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21344. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21345. 'var $with = $mod.b;',
  21346. '$mod.TObject.FSize = $with.FSize + 31;',
  21347. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21348. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21349. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21350. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21351. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21352. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21353. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21354. 'var $with1 = $mod.c;',
  21355. '$mod.TObject.FSize = $with1.FSize + 31;',
  21356. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21357. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21358. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21359. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21360. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21361. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21362. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21363. 'var $with2 = $mod.TBird;',
  21364. '$mod.TObject.FSize = $with2.FSize + 31;',
  21365. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21366. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21367. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21368. '']));
  21369. end;
  21370. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21371. begin
  21372. StartProgram(false);
  21373. Add([
  21374. 'type',
  21375. ' TObject = class',
  21376. ' class function GetSpeed: word; static;',
  21377. ' class procedure SetSpeed(Value: word); static;',
  21378. ' end;',
  21379. ' TObjHelper = class helper for TObject',
  21380. ' class function GetLeft: word; static;',
  21381. ' class procedure SetLeft(Value: word); static;',
  21382. ' class property Speed: word read GetSpeed write SetSpeed;',
  21383. ' class property Left: word read GetLeft write SetLeft;',
  21384. ' end;',
  21385. ' TBird = class',
  21386. ' class property NotRight: word read GetLeft write SetLeft;',
  21387. ' class procedure DoIt; static;',
  21388. ' class procedure DoSome;',
  21389. ' end;',
  21390. ' TBirdClass = class of TBird;',
  21391. 'class function Tobject.GetSpeed: word;',
  21392. 'begin',
  21393. ' Speed:=Speed+12;',
  21394. ' Left:=Left+13;',
  21395. 'end;',
  21396. 'class procedure TObject.SetSpeed(Value: word);',
  21397. 'begin',
  21398. 'end;',
  21399. 'class function TObjHelper.GetLeft: word;',
  21400. 'begin',
  21401. ' Speed:=Speed+12;',
  21402. ' Left:=Left+13;',
  21403. 'end;',
  21404. 'class procedure TObjHelper.SetLeft(Value: word);',
  21405. 'begin',
  21406. 'end;',
  21407. 'class procedure TBird.DoIt;',
  21408. 'begin',
  21409. ' NotRight:=NotRight+11;',
  21410. 'end;',
  21411. 'class procedure TBird.DoSome;',
  21412. 'begin',
  21413. ' Speed:=Speed+12;',
  21414. ' Left:=Left+13;',
  21415. ' Self.Speed:=Self.Speed+22;',
  21416. ' Self.Left:=Self.Left+23;',
  21417. ' with Self do begin',
  21418. ' Speed:=Speed+32;',
  21419. ' Left:=Left+33;',
  21420. ' end;',
  21421. ' NotRight:=NotRight+11;',
  21422. ' Self.NotRight:=Self.NotRight+21;',
  21423. ' with Self do NotRight:=NotRight+31;',
  21424. 'end;',
  21425. 'var',
  21426. ' b: TBird;',
  21427. ' c: TBirdClass;',
  21428. 'begin',
  21429. ' b.Speed:=b.Speed+12;',
  21430. ' b.Left:=b.Left+13;',
  21431. ' b.NotRight:=b.NotRight+14;',
  21432. ' with b do begin',
  21433. ' Speed:=Speed+32;',
  21434. ' Left:=Left+33;',
  21435. ' NotRight:=NotRight+34;',
  21436. ' end;',
  21437. ' c.Speed:=c.Speed+12;',
  21438. ' c.Left:=c.Left+13;',
  21439. ' c.NotRight:=c.NotRight+14;',
  21440. ' with c do begin',
  21441. ' Speed:=Speed+32;',
  21442. ' Left:=Left+33;',
  21443. ' NotRight:=NotRight+34;',
  21444. ' end;',
  21445. ' tbird.Speed:=tbird.Speed+12;',
  21446. ' tbird.Left:=tbird.Left+13;',
  21447. ' tbird.NotRight:=tbird.NotRight+14;',
  21448. ' with tbird do begin',
  21449. ' Speed:=Speed+32;',
  21450. ' Left:=Left+33;',
  21451. ' NotRight:=NotRight+34;',
  21452. ' end;',
  21453. '']);
  21454. ConvertProgram;
  21455. CheckSource('TestClassHelper_ClassPropertyStatic',
  21456. LinesToStr([ // statements
  21457. 'rtl.createClass($mod, "TObject", null, function () {',
  21458. ' this.$init = function () {',
  21459. ' };',
  21460. ' this.$final = function () {',
  21461. ' };',
  21462. ' this.GetSpeed = function () {',
  21463. ' var Result = 0;',
  21464. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21465. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21466. ' return Result;',
  21467. ' };',
  21468. ' this.SetSpeed = function (Value) {',
  21469. ' };',
  21470. '});',
  21471. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21472. ' this.GetLeft = function () {',
  21473. ' var Result = 0;',
  21474. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21475. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21476. ' return Result;',
  21477. ' };',
  21478. ' this.SetLeft = function (Value) {',
  21479. ' };',
  21480. '});',
  21481. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21482. ' this.DoIt = function () {',
  21483. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21484. ' };',
  21485. ' this.DoSome = function () {',
  21486. ' this.SetSpeed(this.GetSpeed() + 12);',
  21487. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21488. ' this.SetSpeed(this.GetSpeed() + 22);',
  21489. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  21490. ' this.SetSpeed(this.GetSpeed() + 32);',
  21491. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21492. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21493. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  21494. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  21495. ' };',
  21496. '});',
  21497. 'this.b = null;',
  21498. 'this.c = null;',
  21499. '']),
  21500. LinesToStr([ // $mod.$main
  21501. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21502. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21503. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21504. 'var $with = $mod.b;',
  21505. '$with.SetSpeed($with.GetSpeed() + 32);',
  21506. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21507. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21508. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21509. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21510. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21511. 'var $with1 = $mod.c;',
  21512. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21513. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21514. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21515. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21516. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21517. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21518. 'var $with2 = $mod.TBird;',
  21519. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21520. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21521. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21522. '']));
  21523. end;
  21524. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  21525. begin
  21526. StartProgram(false);
  21527. Add([
  21528. 'type',
  21529. ' TObject = class',
  21530. ' class function GetSpeed(Index: boolean): word;',
  21531. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  21532. ' end;',
  21533. ' TObjHelper = class helper for TObject',
  21534. ' class function GetSize(Index: boolean): word;',
  21535. ' class procedure SetSize(Index: boolean; Value: word);',
  21536. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  21537. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21538. ' end;',
  21539. ' TBird = class',
  21540. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  21541. ' class procedure DoIt;',
  21542. ' end;',
  21543. ' TBirdClass = class of TBird;',
  21544. 'class function Tobject.GetSpeed(Index: boolean): word;',
  21545. 'begin',
  21546. ' Size[true]:=Size[false]+11;',
  21547. ' Speed[true]:=Speed[false]+12;',
  21548. ' Self.Size[true]:=Self.Size[false]+21;',
  21549. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21550. ' with Self do begin',
  21551. ' Size[true]:=Size[false]+31;',
  21552. ' Speed[true]:=Speed[false]+32;',
  21553. ' end;',
  21554. 'end;',
  21555. 'class function TObjHelper.GetSize(Index: boolean): word;',
  21556. 'begin',
  21557. ' Size[true]:=Size[false]+11;',
  21558. ' Speed[true]:=Speed[false]+12;',
  21559. ' Self.Size[true]:=Self.Size[false]+21;',
  21560. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21561. ' with Self do begin',
  21562. ' Size[true]:=Size[false]+31;',
  21563. ' Speed[true]:=Speed[false]+32;',
  21564. ' end;',
  21565. 'end;',
  21566. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21567. 'begin',
  21568. 'end;',
  21569. 'class procedure TBird.DoIt;',
  21570. 'begin',
  21571. ' Items[true]:=Items[false]+11;',
  21572. ' Self.Items[true]:=Self.Items[false]+21;',
  21573. ' with Self do Items[true]:=Items[false]+31;',
  21574. 'end;',
  21575. 'var',
  21576. ' b: TBird;',
  21577. ' c: TBirdClass;',
  21578. 'begin',
  21579. ' b.Size[true]:=b.Size[false]+11;',
  21580. ' b.Speed[true]:=b.Speed[false]+12;',
  21581. ' b.Items[true]:=b.Items[false]+13;',
  21582. ' with b do begin',
  21583. ' Size[true]:=Size[false]+21;',
  21584. ' Speed[true]:=Speed[false]+22;',
  21585. ' Items[true]:=Items[false]+23;',
  21586. ' end;',
  21587. ' c.Size[true]:=c.Size[false]+11;',
  21588. ' c.Speed[true]:=c.Speed[false]+12;',
  21589. ' c.Items[true]:=c.Items[false]+13;',
  21590. ' with c do begin',
  21591. ' Size[true]:=Size[false]+21;',
  21592. ' Speed[true]:=Speed[false]+22;',
  21593. ' Items[true]:=Items[false]+23;',
  21594. ' end;',
  21595. ' TBird.Size[true]:=TBird.Size[false]+11;',
  21596. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  21597. ' TBird.Items[true]:=TBird.Items[false]+13;',
  21598. ' with TBird do begin',
  21599. ' Size[true]:=Size[false]+21;',
  21600. ' Speed[true]:=Speed[false]+22;',
  21601. ' Items[true]:=Items[false]+23;',
  21602. ' end;',
  21603. '']);
  21604. ConvertProgram;
  21605. CheckSource('TestClassHelper_ClassProperty_Array',
  21606. LinesToStr([ // statements
  21607. 'rtl.createClass($mod, "TObject", null, function () {',
  21608. ' this.$init = function () {',
  21609. ' };',
  21610. ' this.$final = function () {',
  21611. ' };',
  21612. ' this.GetSpeed = function (Index) {',
  21613. ' var Result = 0;',
  21614. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21615. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21616. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21617. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21618. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21619. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21620. ' return Result;',
  21621. ' };',
  21622. '});',
  21623. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21624. ' this.GetSize = function (Index) {',
  21625. ' var Result = 0;',
  21626. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21627. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21628. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21629. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21630. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21631. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21632. ' return Result;',
  21633. ' };',
  21634. ' this.SetSize = function (Index, Value) {',
  21635. ' };',
  21636. '});',
  21637. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21638. ' this.DoIt = function () {',
  21639. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21640. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21641. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21642. ' };',
  21643. '});',
  21644. 'this.b = null;',
  21645. 'this.c = null;',
  21646. '']),
  21647. LinesToStr([ // $mod.$main
  21648. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  21649. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  21650. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  21651. 'var $with = $mod.b;',
  21652. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  21653. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  21654. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  21655. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  21656. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  21657. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  21658. 'var $with1 = $mod.c;',
  21659. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  21660. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  21661. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  21662. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  21663. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  21664. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  21665. 'var $with2 = $mod.TBird;',
  21666. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  21667. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  21668. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  21669. '']));
  21670. end;
  21671. procedure TTestModule.TestClassHelper_ForIn;
  21672. begin
  21673. StartProgram(false);
  21674. Add([
  21675. 'type',
  21676. ' TObject = class end;',
  21677. ' TItem = TObject;',
  21678. ' TEnumerator = class',
  21679. ' FCurrent: TItem;',
  21680. ' property Current: TItem read FCurrent;',
  21681. ' function MoveNext: boolean;',
  21682. ' end;',
  21683. ' TBird = class',
  21684. ' end;',
  21685. ' TBirdHelper = class helper for TBird',
  21686. ' function GetEnumerator: TEnumerator;',
  21687. ' end;',
  21688. 'function TEnumerator.MoveNext: boolean;',
  21689. 'begin',
  21690. 'end;',
  21691. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  21692. 'begin',
  21693. 'end;',
  21694. 'var',
  21695. ' b: TBird;',
  21696. ' i, i2: TItem;',
  21697. 'begin',
  21698. ' for i in b do i2:=i;']);
  21699. ConvertProgram;
  21700. CheckSource('TestClassHelper_ForIn',
  21701. LinesToStr([ // statements
  21702. 'rtl.createClass($mod, "TObject", null, function () {',
  21703. ' this.$init = function () {',
  21704. ' };',
  21705. ' this.$final = function () {',
  21706. ' };',
  21707. '});',
  21708. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  21709. ' this.$init = function () {',
  21710. ' $mod.TObject.$init.call(this);',
  21711. ' this.FCurrent = null;',
  21712. ' };',
  21713. ' this.$final = function () {',
  21714. ' this.FCurrent = undefined;',
  21715. ' $mod.TObject.$final.call(this);',
  21716. ' };',
  21717. ' this.MoveNext = function () {',
  21718. ' var Result = false;',
  21719. ' return Result;',
  21720. ' };',
  21721. '});',
  21722. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21723. '});',
  21724. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  21725. ' this.GetEnumerator = function () {',
  21726. ' var Result = null;',
  21727. ' return Result;',
  21728. ' };',
  21729. '});',
  21730. 'this.b = null;',
  21731. 'this.i = null;',
  21732. 'this.i2 = null;'
  21733. ]),
  21734. LinesToStr([ // $mod.$main
  21735. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  21736. 'try {',
  21737. ' while ($in.MoveNext()){',
  21738. ' $mod.i = $in.FCurrent;',
  21739. ' $mod.i2 = $mod.i;',
  21740. ' }',
  21741. '} finally {',
  21742. ' $in = rtl.freeLoc($in)',
  21743. '};',
  21744. '']));
  21745. end;
  21746. procedure TTestModule.TestClassHelper_PassProperty;
  21747. begin
  21748. StartProgram(false);
  21749. Add([
  21750. 'type',
  21751. ' TObject = class',
  21752. ' FField: TObject;',
  21753. ' property Field: TObject read FField write FField;',
  21754. ' end;',
  21755. ' THelper = class helper for TObject',
  21756. ' procedure Fly;',
  21757. ' class procedure Run;',
  21758. ' class procedure Jump; static;',
  21759. ' end;',
  21760. 'procedure THelper.Fly;',
  21761. 'begin',
  21762. ' Field.Fly;',
  21763. ' Field.Run;',
  21764. ' Field.Jump;',
  21765. ' with Field do begin',
  21766. ' Fly;',
  21767. ' Run;',
  21768. ' Jump;',
  21769. ' end;',
  21770. 'end;',
  21771. 'class procedure THelper.Run;',
  21772. 'begin',
  21773. 'end;',
  21774. 'class procedure THelper.Jump;',
  21775. 'begin',
  21776. 'end;',
  21777. 'var',
  21778. ' b: TObject;',
  21779. 'begin',
  21780. ' b.Field.Fly;',
  21781. ' b.Field.Run;',
  21782. ' b.Field.Jump;',
  21783. ' with b do begin',
  21784. ' Field.Run;',
  21785. ' Field.Fly;',
  21786. ' Field.Jump;',
  21787. ' end;',
  21788. ' with b.Field do begin',
  21789. ' Run;',
  21790. ' Fly;',
  21791. ' Jump;',
  21792. ' end;',
  21793. '']);
  21794. ConvertProgram;
  21795. CheckSource('TestClassHelper_PassProperty',
  21796. LinesToStr([ // statements
  21797. 'rtl.createClass($mod, "TObject", null, function () {',
  21798. ' this.$init = function () {',
  21799. ' this.FField = null;',
  21800. ' };',
  21801. ' this.$final = function () {',
  21802. ' this.FField = undefined;',
  21803. ' };',
  21804. '});',
  21805. 'rtl.createHelper($mod, "THelper", null, function () {',
  21806. ' this.Fly = function () {',
  21807. ' $mod.THelper.Fly.call(this.FField);',
  21808. ' $mod.THelper.Run.call(this.FField.$class);',
  21809. ' $mod.THelper.Jump();',
  21810. ' var $with = this.FField;',
  21811. ' $mod.THelper.Fly.call($with);',
  21812. ' $mod.THelper.Run.call($with.$class);',
  21813. ' $mod.THelper.Jump();',
  21814. ' };',
  21815. ' this.Run = function () {',
  21816. ' };',
  21817. ' this.Jump = function () {',
  21818. ' };',
  21819. '});',
  21820. 'this.b = null;',
  21821. '']),
  21822. LinesToStr([ // $mod.$main
  21823. '$mod.THelper.Fly.call($mod.b.FField);',
  21824. '$mod.THelper.Run.call($mod.b.FField.$class);',
  21825. '$mod.THelper.Jump();',
  21826. 'var $with = $mod.b;',
  21827. '$mod.THelper.Run.call($with.FField.$class);',
  21828. '$mod.THelper.Fly.call($with.FField);',
  21829. '$mod.THelper.Jump();',
  21830. 'var $with1 = $mod.b.FField;',
  21831. '$mod.THelper.Run.call($with1.$class);',
  21832. '$mod.THelper.Fly.call($with1);',
  21833. '$mod.THelper.Jump();',
  21834. '']));
  21835. end;
  21836. procedure TTestModule.TestExtClassHelper_ClassVar;
  21837. begin
  21838. StartProgram(false);
  21839. Add([
  21840. '{$modeswitch externalclass}',
  21841. 'type',
  21842. ' TExtA = class external name ''ExtObj''',
  21843. ' end;',
  21844. ' THelper = class helper for TExtA',
  21845. ' const',
  21846. ' One = 1;',
  21847. ' Two: word = 2;',
  21848. ' class var',
  21849. ' Glob: word;',
  21850. ' function Foo(w: word): word;',
  21851. ' class function Bar(w: word): word; static;',
  21852. ' end;',
  21853. 'function THelper.foo(w: word): word;',
  21854. 'begin',
  21855. ' Result:=w;',
  21856. ' Two:=One+w;',
  21857. ' Glob:=Glob;',
  21858. ' Result:=Self.Glob;',
  21859. ' Self.Glob:=Self.Glob;',
  21860. ' with Self do Glob:=Glob;',
  21861. 'end;',
  21862. 'class function THelper.bar(w: word): word;',
  21863. 'begin',
  21864. ' Result:=w;',
  21865. ' Two:=One;',
  21866. ' Glob:=Glob;',
  21867. 'end;',
  21868. 'var o: TExtA;',
  21869. 'begin',
  21870. ' texta.two:=texta.one;',
  21871. ' texta.Glob:=texta.Glob;',
  21872. ' with texta do begin',
  21873. ' two:=one;',
  21874. ' Glob:=Glob;',
  21875. ' end;',
  21876. ' o.two:=o.one;',
  21877. ' o.Glob:=o.Glob;',
  21878. ' with o do begin',
  21879. ' two:=one;',
  21880. ' Glob:=Glob;',
  21881. ' end;',
  21882. '']);
  21883. ConvertProgram;
  21884. CheckSource('TestExtClassHelper_ClassVar',
  21885. LinesToStr([ // statements
  21886. 'rtl.createHelper($mod, "THelper", null, function () {',
  21887. ' this.One = 1;',
  21888. ' this.Two = 2;',
  21889. ' this.Glob = 0;',
  21890. ' this.Foo = function (w) {',
  21891. ' var Result = 0;',
  21892. ' Result = w;',
  21893. ' $mod.THelper.Two = 1 + w;',
  21894. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21895. ' Result = $mod.THelper.Glob;',
  21896. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21897. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21898. ' return Result;',
  21899. ' };',
  21900. ' this.Bar = function (w) {',
  21901. ' var Result = 0;',
  21902. ' Result = w;',
  21903. ' $mod.THelper.Two = 1;',
  21904. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21905. ' return Result;',
  21906. ' };',
  21907. '});',
  21908. 'this.o = null;',
  21909. '']),
  21910. LinesToStr([ // $mod.$main
  21911. '$mod.THelper.Two = 1;',
  21912. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21913. '$mod.THelper.Two = 1;',
  21914. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21915. '$mod.THelper.Two = 1;',
  21916. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21917. 'var $with = $mod.o;',
  21918. '$mod.THelper.Two = 1;',
  21919. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21920. '']));
  21921. end;
  21922. procedure TTestModule.TestExtClassHelper_Method_Call;
  21923. begin
  21924. StartProgram(false);
  21925. Add([
  21926. '{$modeswitch externalclass}',
  21927. 'type',
  21928. ' TFly = function(w: word): word of object;',
  21929. ' TExtA = class external name ''ExtObj''',
  21930. ' procedure Run(w: word = 10);',
  21931. ' end;',
  21932. ' THelper = class helper for TExtA',
  21933. ' function Foo(w: word = 1): word;',
  21934. ' function Fly(w: word = 2): word; external name ''Fly'';',
  21935. ' end;',
  21936. 'var p: TFly;',
  21937. 'function THelper.foo(w: word): word;',
  21938. 'begin',
  21939. ' Run;',
  21940. ' Run();',
  21941. ' Run(11);',
  21942. ' Foo;',
  21943. ' Foo();',
  21944. ' Foo(12);',
  21945. ' Self.Foo;',
  21946. ' Self.Foo();',
  21947. ' Self.Foo(13);',
  21948. ' Fly;',
  21949. ' Fly();',
  21950. ' with Self do begin',
  21951. ' Foo;',
  21952. ' Foo();',
  21953. ' Foo(14);',
  21954. ' Fly;',
  21955. ' Fly();',
  21956. ' end;',
  21957. ' p:=@Fly;',
  21958. 'end;',
  21959. 'var Obj: TExtA;',
  21960. 'begin',
  21961. ' obj.Foo;',
  21962. ' obj.Foo();',
  21963. ' obj.Foo(21);',
  21964. ' obj.Fly;',
  21965. ' obj.Fly();',
  21966. ' with obj do begin',
  21967. ' Foo;',
  21968. ' Foo();',
  21969. ' Foo(22);',
  21970. ' Fly;',
  21971. ' Fly();',
  21972. ' end;',
  21973. ' p:[email protected];',
  21974. '']);
  21975. ConvertProgram;
  21976. CheckSource('TestExtClassHelper_Method_Call',
  21977. LinesToStr([ // statements
  21978. 'rtl.createHelper($mod, "THelper", null, function () {',
  21979. ' this.Foo = function (w) {',
  21980. ' var Result = 0;',
  21981. ' this.Run(10);',
  21982. ' this.Run(10);',
  21983. ' this.Run(11);',
  21984. ' $mod.THelper.Foo.call(this, 1);',
  21985. ' $mod.THelper.Foo.call(this, 1);',
  21986. ' $mod.THelper.Foo.call(this, 12);',
  21987. ' $mod.THelper.Foo.call(this, 1);',
  21988. ' $mod.THelper.Foo.call(this, 1);',
  21989. ' $mod.THelper.Foo.call(this, 13);',
  21990. ' this.Fly(2);',
  21991. ' this.Fly(2);',
  21992. ' $mod.THelper.Foo.call(this, 1);',
  21993. ' $mod.THelper.Foo.call(this, 1);',
  21994. ' $mod.THelper.Foo.call(this, 14);',
  21995. ' this.Fly(2);',
  21996. ' this.Fly(2);',
  21997. ' $mod.p = rtl.createCallback(this, "Fly");',
  21998. ' return Result;',
  21999. ' };',
  22000. '});',
  22001. 'this.p = null;',
  22002. 'this.Obj = null;',
  22003. '']),
  22004. LinesToStr([ // $mod.$main
  22005. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22006. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22007. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22008. '$mod.Obj.Fly(2);',
  22009. '$mod.Obj.Fly(2);',
  22010. 'var $with = $mod.Obj;',
  22011. '$mod.THelper.Foo.call($with, 1);',
  22012. '$mod.THelper.Foo.call($with, 1);',
  22013. '$mod.THelper.Foo.call($with, 22);',
  22014. '$with.Fly(2);',
  22015. '$with.Fly(2);',
  22016. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22017. '']));
  22018. end;
  22019. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22020. begin
  22021. StartProgram(false);
  22022. Add([
  22023. '{$modeswitch externalclass}',
  22024. 'type',
  22025. ' TExtA = class external name ''ExtObj''',
  22026. ' procedure Run(w: word = 10);',
  22027. ' end;',
  22028. ' THelper = class helper for TExtA',
  22029. ' class procedure Fly;',
  22030. ' end;',
  22031. 'class procedure THelper.Fly;',
  22032. 'begin end;',
  22033. 'begin',
  22034. '']);
  22035. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22036. nHelperClassMethodForExtClassMustBeStatic);
  22037. ConvertProgram;
  22038. end;
  22039. procedure TTestModule.TestRecordHelper_ClassVar;
  22040. begin
  22041. StartProgram(false);
  22042. Add([
  22043. 'type',
  22044. ' TRec = record',
  22045. ' end;',
  22046. ' THelper = record helper for TRec',
  22047. ' const',
  22048. ' One = 1;',
  22049. ' Two: word = 2;',
  22050. ' class var',
  22051. ' Glob: word;',
  22052. ' function Foo(w: word): word;',
  22053. ' class function Bar(w: word): word; static;',
  22054. ' end;',
  22055. 'function THelper.foo(w: word): word;',
  22056. 'begin',
  22057. ' Result:=w;',
  22058. ' Two:=One+w;',
  22059. ' Glob:=Glob;',
  22060. ' Result:=Self.Glob;',
  22061. ' Self.Glob:=Self.Glob;',
  22062. ' with Self do Glob:=Glob;',
  22063. ' Self:=Self;',
  22064. 'end;',
  22065. 'class function THelper.bar(w: word): word;',
  22066. 'begin',
  22067. ' Result:=w;',
  22068. ' Two:=One;',
  22069. ' Glob:=Glob;',
  22070. 'end;',
  22071. 'var r: TRec;',
  22072. 'begin',
  22073. ' trec.two:=trec.one;',
  22074. ' trec.Glob:=trec.Glob;',
  22075. ' with trec do begin',
  22076. ' two:=one;',
  22077. ' Glob:=Glob;',
  22078. ' end;',
  22079. ' r.two:=r.one;',
  22080. ' r.Glob:=r.Glob;',
  22081. ' with r do begin',
  22082. ' two:=one;',
  22083. ' Glob:=Glob;',
  22084. ' end;',
  22085. '']);
  22086. ConvertProgram;
  22087. CheckSource('TestRecordHelper_ClassVar',
  22088. LinesToStr([ // statements
  22089. 'rtl.recNewT($mod, "TRec", function () {',
  22090. ' this.$eq = function (b) {',
  22091. ' return true;',
  22092. ' };',
  22093. ' this.$assign = function (s) {',
  22094. ' return this;',
  22095. ' };',
  22096. '});',
  22097. 'rtl.createHelper($mod, "THelper", null, function () {',
  22098. ' this.One = 1;',
  22099. ' this.Two = 2;',
  22100. ' this.Glob = 0;',
  22101. ' this.Foo = function (w) {',
  22102. ' var Result = 0;',
  22103. ' Result = w;',
  22104. ' $mod.THelper.Two = 1 + w;',
  22105. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22106. ' Result = $mod.THelper.Glob;',
  22107. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22108. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22109. ' this.$assign(this);',
  22110. ' return Result;',
  22111. ' };',
  22112. ' this.Bar = function (w) {',
  22113. ' var Result = 0;',
  22114. ' Result = w;',
  22115. ' $mod.THelper.Two = 1;',
  22116. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22117. ' return Result;',
  22118. ' };',
  22119. '});',
  22120. 'this.r = $mod.TRec.$new();',
  22121. '']),
  22122. LinesToStr([ // $mod.$main
  22123. '$mod.THelper.Two = 1;',
  22124. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22125. 'var $with = $mod.TRec;',
  22126. '$mod.THelper.Two = 1;',
  22127. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22128. '$mod.THelper.Two = 1;',
  22129. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22130. 'var $with1 = $mod.r;',
  22131. '$mod.THelper.Two = 1;',
  22132. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22133. '']));
  22134. end;
  22135. procedure TTestModule.TestRecordHelper_Method_Call;
  22136. begin
  22137. StartProgram(false);
  22138. Add([
  22139. '{$modeswitch AdvancedRecords}',
  22140. 'type',
  22141. ' TRec = record',
  22142. ' procedure Run(w: word = 10);',
  22143. ' end;',
  22144. ' THelper = record helper for TRec',
  22145. ' function Foo(w: word = 1): word;',
  22146. ' end;',
  22147. 'procedure TRec.Run(w: word);',
  22148. 'begin',
  22149. ' Foo;',
  22150. ' Foo();',
  22151. ' Foo(2);',
  22152. ' Self.Foo;',
  22153. ' Self.Foo();',
  22154. ' Self.Foo(3);',
  22155. ' with Self do begin',
  22156. ' Foo;',
  22157. ' Foo();',
  22158. ' Foo(4);',
  22159. ' end;',
  22160. 'end;',
  22161. 'function THelper.foo(w: word): word;',
  22162. 'begin',
  22163. ' Run;',
  22164. ' Run();',
  22165. ' Run(11);',
  22166. ' Foo;',
  22167. ' Foo();',
  22168. ' Foo(12);',
  22169. ' Self.Foo;',
  22170. ' Self.Foo();',
  22171. ' Self.Foo(13);',
  22172. ' with Self do begin',
  22173. ' Foo;',
  22174. ' Foo();',
  22175. ' Foo(14);',
  22176. ' end;',
  22177. 'end;',
  22178. 'var Rec: TRec;',
  22179. 'begin',
  22180. ' Rec.Foo;',
  22181. ' Rec.Foo();',
  22182. ' Rec.Foo(21);',
  22183. ' with Rec do begin',
  22184. ' Foo;',
  22185. ' Foo();',
  22186. ' Foo(22);',
  22187. ' end;',
  22188. '']);
  22189. ConvertProgram;
  22190. CheckSource('TestRecordHelper_Method_Call',
  22191. LinesToStr([ // statements
  22192. 'rtl.recNewT($mod, "TRec", function () {',
  22193. ' this.$eq = function (b) {',
  22194. ' return true;',
  22195. ' };',
  22196. ' this.$assign = function (s) {',
  22197. ' return this;',
  22198. ' };',
  22199. ' this.Run = function (w) {',
  22200. ' $mod.THelper.Foo.call(this, 1);',
  22201. ' $mod.THelper.Foo.call(this, 1);',
  22202. ' $mod.THelper.Foo.call(this, 2);',
  22203. ' $mod.THelper.Foo.call(this, 1);',
  22204. ' $mod.THelper.Foo.call(this, 1);',
  22205. ' $mod.THelper.Foo.call(this, 3);',
  22206. ' $mod.THelper.Foo.call(this, 1);',
  22207. ' $mod.THelper.Foo.call(this, 1);',
  22208. ' $mod.THelper.Foo.call(this, 4);',
  22209. ' };',
  22210. '});',
  22211. 'rtl.createHelper($mod, "THelper", null, function () {',
  22212. ' this.Foo = function (w) {',
  22213. ' var Result = 0;',
  22214. ' this.Run(10);',
  22215. ' this.Run(10);',
  22216. ' this.Run(11);',
  22217. ' $mod.THelper.Foo.call(this, 1);',
  22218. ' $mod.THelper.Foo.call(this, 1);',
  22219. ' $mod.THelper.Foo.call(this, 12);',
  22220. ' $mod.THelper.Foo.call(this, 1);',
  22221. ' $mod.THelper.Foo.call(this, 1);',
  22222. ' $mod.THelper.Foo.call(this, 13);',
  22223. ' $mod.THelper.Foo.call(this, 1);',
  22224. ' $mod.THelper.Foo.call(this, 1);',
  22225. ' $mod.THelper.Foo.call(this, 14);',
  22226. ' return Result;',
  22227. ' };',
  22228. '});',
  22229. 'this.Rec = $mod.TRec.$new();',
  22230. '']),
  22231. LinesToStr([ // $mod.$main
  22232. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22233. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22234. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22235. 'var $with = $mod.Rec;',
  22236. '$mod.THelper.Foo.call($with, 1);',
  22237. '$mod.THelper.Foo.call($with, 1);',
  22238. '$mod.THelper.Foo.call($with, 22);',
  22239. '']));
  22240. end;
  22241. procedure TTestModule.TestRecordHelper_Constructor;
  22242. begin
  22243. StartProgram(false);
  22244. Add([
  22245. '{$modeswitch AdvancedRecords}',
  22246. 'type',
  22247. ' TRec = record',
  22248. ' constructor Create(w: word);',
  22249. ' end;',
  22250. ' THelper = record helper for TRec',
  22251. ' constructor NewHlp(w: word);',
  22252. ' end;',
  22253. 'var',
  22254. ' Rec: TRec;',
  22255. 'constructor TRec.Create(w: word);',
  22256. 'begin',
  22257. ' NewHlp(2);', // normal call
  22258. ' trec.NewHlp(3);', // new instance
  22259. 'end;',
  22260. 'constructor THelper.NewHlp(w: word);',
  22261. 'begin',
  22262. ' create(2);', // normal call
  22263. ' trec.create(3);', // new instance
  22264. ' NewHlp(4);', // normal call
  22265. ' trec.NewHlp(5);', // new instance
  22266. 'end;',
  22267. 'begin',
  22268. ' rec.newhlp(2);', // normal call
  22269. ' with rec do newhlp(12);', // normal call
  22270. ' trec.newhlp(3);', // new instance
  22271. ' with trec do newhlp(13);', // new instance
  22272. '']);
  22273. ConvertProgram;
  22274. CheckSource('TestRecordHelper_Constructor',
  22275. LinesToStr([ // statements
  22276. 'rtl.recNewT($mod, "TRec", function () {',
  22277. ' this.$eq = function (b) {',
  22278. ' return true;',
  22279. ' };',
  22280. ' this.$assign = function (s) {',
  22281. ' return this;',
  22282. ' };',
  22283. ' this.Create = function (w) {',
  22284. ' $mod.THelper.NewHlp.call(this, 2);',
  22285. ' $mod.THelper.$new("NewHlp", [3]);',
  22286. ' return this;',
  22287. ' };',
  22288. '}, true);',
  22289. 'rtl.createHelper($mod, "THelper", null, function () {',
  22290. ' this.NewHlp = function (w) {',
  22291. ' this.Create(2);',
  22292. ' $mod.TRec.$new().Create(3);',
  22293. ' $mod.THelper.NewHlp.call(this, 4);',
  22294. ' $mod.THelper.$new("NewHlp", [5]);',
  22295. ' return this;',
  22296. ' };',
  22297. ' this.$new = function (fn, args) {',
  22298. ' return this[fn].apply($mod.TRec.$new(), args);',
  22299. ' };',
  22300. '});',
  22301. 'this.Rec = $mod.TRec.$new();',
  22302. '']),
  22303. LinesToStr([ // $mod.$main
  22304. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22305. 'var $with = $mod.Rec;',
  22306. '$mod.THelper.NewHlp.call($with, 12);',
  22307. '$mod.THelper.$new("NewHlp", [3]);',
  22308. 'var $with1 = $mod.TRec;',
  22309. '$mod.THelper.$new("NewHlp", [13]);',
  22310. '']));
  22311. end;
  22312. procedure TTestModule.TestTypeHelper_ClassVar;
  22313. begin
  22314. StartProgram(false);
  22315. Add([
  22316. '{$modeswitch typehelpers}',
  22317. 'type',
  22318. ' THelper = type helper for byte',
  22319. ' const',
  22320. ' One = 1;',
  22321. ' Two: word = 2;',
  22322. ' class var',
  22323. ' Glob: word;',
  22324. ' function Foo(w: word): word;',
  22325. ' class function Bar(w: word): word; static;',
  22326. ' end;',
  22327. 'function THelper.foo(w: word): word;',
  22328. 'begin',
  22329. ' Result:=w;',
  22330. ' Two:=One+w;',
  22331. ' Glob:=Glob;',
  22332. ' Result:=Self.Glob;',
  22333. ' Self.Glob:=Self.Glob;',
  22334. ' with Self do Glob:=Glob;',
  22335. 'end;',
  22336. 'class function THelper.bar(w: word): word;',
  22337. 'begin',
  22338. ' Result:=w;',
  22339. ' Two:=One;',
  22340. ' Glob:=Glob;',
  22341. 'end;',
  22342. 'var b: byte;',
  22343. 'begin',
  22344. ' byte.two:=byte.one;',
  22345. ' byte.Glob:=byte.Glob;',
  22346. ' with byte do begin',
  22347. ' two:=one;',
  22348. ' Glob:=Glob;',
  22349. ' end;',
  22350. ' b.two:=b.one;',
  22351. ' b.Glob:=b.Glob;',
  22352. ' with b do begin',
  22353. ' two:=one;',
  22354. ' Glob:=Glob;',
  22355. ' end;',
  22356. '']);
  22357. ConvertProgram;
  22358. CheckSource('TestTypeHelper_ClassVar',
  22359. LinesToStr([ // statements
  22360. 'rtl.createHelper($mod, "THelper", null, function () {',
  22361. ' this.One = 1;',
  22362. ' this.Two = 2;',
  22363. ' this.Glob = 0;',
  22364. ' this.Foo = function (w) {',
  22365. ' var Result = 0;',
  22366. ' Result = w;',
  22367. ' $mod.THelper.Two = 1 + w;',
  22368. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22369. ' Result = $mod.THelper.Glob;',
  22370. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22371. ' var $with = this.get();',
  22372. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22373. ' return Result;',
  22374. ' };',
  22375. ' this.Bar = function (w) {',
  22376. ' var Result = 0;',
  22377. ' Result = w;',
  22378. ' $mod.THelper.Two = 1;',
  22379. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22380. ' return Result;',
  22381. ' };',
  22382. '});',
  22383. 'this.b = 0;',
  22384. '']),
  22385. LinesToStr([ // $mod.$main
  22386. '$mod.THelper.Two = 1;',
  22387. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22388. '$mod.THelper.Two = 1;',
  22389. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22390. '$mod.THelper.Two = 1;',
  22391. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22392. 'var $with = $mod.b;',
  22393. '$mod.THelper.Two = 1;',
  22394. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22395. '']));
  22396. end;
  22397. procedure TTestModule.TestTypeHelper_PassResultElement;
  22398. begin
  22399. StartProgram(false);
  22400. Add([
  22401. '{$modeswitch typehelpers}',
  22402. 'type',
  22403. ' THelper = type helper for word',
  22404. ' procedure DoIt(e: byte = 123);',
  22405. ' class procedure DoSome(e: byte = 456); static;',
  22406. ' end;',
  22407. 'procedure THelper.DoIt(e: byte);',
  22408. 'begin',
  22409. 'end;',
  22410. 'class procedure THelper.DoSome(e: byte);',
  22411. 'begin',
  22412. 'end;',
  22413. 'function Foo(w: word): word;',
  22414. 'begin',
  22415. ' Result.DoIt;',
  22416. ' Result.DoIt();',
  22417. ' Result.DoSome;',
  22418. ' Result.DoSome();',
  22419. ' with Result do begin',
  22420. ' DoIt;',
  22421. ' DoIt();',
  22422. ' DoSome;',
  22423. ' DoSome();',
  22424. ' end;',
  22425. 'end;',
  22426. 'begin',
  22427. '']);
  22428. ConvertProgram;
  22429. CheckSource('TestTypeHelper_PassResultElement',
  22430. LinesToStr([ // statements
  22431. 'rtl.createHelper($mod, "THelper", null, function () {',
  22432. ' this.DoIt = function (e) {',
  22433. ' };',
  22434. ' this.DoSome = function (e) {',
  22435. ' };',
  22436. '});',
  22437. 'this.Foo = function (w) {',
  22438. ' var Result = 0;',
  22439. ' $mod.THelper.DoIt.call({',
  22440. ' get: function () {',
  22441. ' return Result;',
  22442. ' },',
  22443. ' set: function (v) {',
  22444. ' Result = v;',
  22445. ' }',
  22446. ' }, 123);',
  22447. ' $mod.THelper.DoIt.call({',
  22448. ' get: function () {',
  22449. ' return Result;',
  22450. ' },',
  22451. ' set: function (v) {',
  22452. ' Result = v;',
  22453. ' }',
  22454. ' }, 123);',
  22455. ' $mod.THelper.DoSome(456);',
  22456. ' $mod.THelper.DoSome(456);',
  22457. ' $mod.THelper.DoIt.call({',
  22458. ' get: function () {',
  22459. ' return Result;',
  22460. ' },',
  22461. ' set: function (v) {',
  22462. ' Result = v;',
  22463. ' }',
  22464. ' }, 123);',
  22465. ' $mod.THelper.DoIt.call({',
  22466. ' get: function () {',
  22467. ' return Result;',
  22468. ' },',
  22469. ' set: function (v) {',
  22470. ' Result = v;',
  22471. ' }',
  22472. ' }, 123);',
  22473. ' $mod.THelper.DoSome(456);',
  22474. ' $mod.THelper.DoSome(456);',
  22475. ' return Result;',
  22476. '};',
  22477. '']),
  22478. LinesToStr([ // $mod.$main
  22479. '']));
  22480. end;
  22481. procedure TTestModule.TestTypeHelper_PassArgs;
  22482. begin
  22483. StartProgram(false);
  22484. Add([
  22485. '{$modeswitch typehelpers}',
  22486. 'type',
  22487. ' THelper = type helper for word',
  22488. ' procedure DoIt(e: byte = 123);',
  22489. ' end;',
  22490. 'procedure THelper.DoIt(e: byte);',
  22491. 'begin',
  22492. 'end;',
  22493. 'procedure FooDefault(a: word);',
  22494. 'begin',
  22495. ' a.DoIt;',
  22496. ' with a do DoIt;',
  22497. 'end;',
  22498. 'procedure FooConst(const a: word);',
  22499. 'begin',
  22500. ' a.DoIt;',
  22501. ' with a do DoIt;',
  22502. 'end;',
  22503. 'procedure FooVar(var a: word);',
  22504. 'begin',
  22505. ' a.DoIt;',
  22506. ' with a do DoIt;',
  22507. 'end;',
  22508. 'begin',
  22509. '']);
  22510. ConvertProgram;
  22511. CheckSource('TestTypeHelper_PassArgs',
  22512. LinesToStr([ // statements
  22513. 'rtl.createHelper($mod, "THelper", null, function () {',
  22514. ' this.DoIt = function (e) {',
  22515. ' };',
  22516. '});',
  22517. 'this.FooDefault = function (a) {',
  22518. ' $mod.THelper.DoIt.call({',
  22519. ' get: function () {',
  22520. ' return a;',
  22521. ' },',
  22522. ' set: function (v) {',
  22523. ' a = v;',
  22524. ' }',
  22525. ' }, 123);',
  22526. ' $mod.THelper.DoIt.call({',
  22527. ' get: function () {',
  22528. ' return a;',
  22529. ' },',
  22530. ' set: function (v) {',
  22531. ' a = v;',
  22532. ' }',
  22533. ' }, 123);',
  22534. '};',
  22535. 'this.FooConst = function (a) {',
  22536. ' $mod.THelper.DoIt.call({',
  22537. ' get: function () {',
  22538. ' return a;',
  22539. ' },',
  22540. ' set: function (v) {',
  22541. ' rtl.raiseE("EPropReadOnly");',
  22542. ' }',
  22543. ' }, 123);',
  22544. ' $mod.THelper.DoIt.call({',
  22545. ' get: function () {',
  22546. ' return a;',
  22547. ' },',
  22548. ' set: function () {',
  22549. ' rtl.raiseE("EPropReadOnly");',
  22550. ' }',
  22551. ' }, 123);',
  22552. '};',
  22553. 'this.FooVar = function (a) {',
  22554. ' $mod.THelper.DoIt.call(a, 123);',
  22555. ' var $with = a.get();',
  22556. ' $mod.THelper.DoIt.call(a, 123);',
  22557. '};',
  22558. '']),
  22559. LinesToStr([ // $mod.$main
  22560. '']));
  22561. end;
  22562. procedure TTestModule.TestTypeHelper_PassVarConst;
  22563. begin
  22564. StartProgram(false);
  22565. Add([
  22566. '{$modeswitch typehelpers}',
  22567. 'type',
  22568. ' THelper = type helper for word',
  22569. ' procedure DoIt(e: byte = 123);',
  22570. ' end;',
  22571. 'procedure THelper.DoIt(e: byte);',
  22572. 'begin',
  22573. 'end;',
  22574. 'var a: word;',
  22575. 'const c: word = 2;',
  22576. '{$writeableconst off}',
  22577. 'const r: word = 3;',
  22578. 'begin',
  22579. ' a.DoIt;',
  22580. ' with a do DoIt;',
  22581. ' c.DoIt;',
  22582. ' with c do DoIt;',
  22583. ' r.DoIt;',
  22584. ' with r do DoIt;',
  22585. '']);
  22586. ConvertProgram;
  22587. CheckSource('TestTypeHelper_PassVarConst',
  22588. LinesToStr([ // statements
  22589. 'rtl.createHelper($mod, "THelper", null, function () {',
  22590. ' this.DoIt = function (e) {',
  22591. ' };',
  22592. '});',
  22593. 'this.a = 0;',
  22594. 'this.c = 2;',
  22595. 'this.r = 3;',
  22596. '']),
  22597. LinesToStr([ // $mod.$main
  22598. '$mod.THelper.DoIt.call({',
  22599. ' p: $mod,',
  22600. ' get: function () {',
  22601. ' return this.p.a;',
  22602. ' },',
  22603. ' set: function (v) {',
  22604. ' this.p.a = v;',
  22605. ' }',
  22606. '}, 123);',
  22607. 'var $with = $mod.a;',
  22608. '$mod.THelper.DoIt.call({',
  22609. ' get: function () {',
  22610. ' return $with;',
  22611. ' },',
  22612. ' set: function (v) {',
  22613. ' $with = v;',
  22614. ' }',
  22615. '}, 123);',
  22616. '$mod.THelper.DoIt.call({',
  22617. ' p: $mod,',
  22618. ' get: function () {',
  22619. ' return this.p.c;',
  22620. ' },',
  22621. ' set: function (v) {',
  22622. ' this.p.c = v;',
  22623. ' }',
  22624. '}, 123);',
  22625. 'var $with1 = $mod.c;',
  22626. '$mod.THelper.DoIt.call({',
  22627. ' get: function () {',
  22628. ' return $with1;',
  22629. ' },',
  22630. ' set: function (v) {',
  22631. ' $with1 = v;',
  22632. ' }',
  22633. '}, 123);',
  22634. '$mod.THelper.DoIt.call({',
  22635. ' get: function () {',
  22636. ' return 3;',
  22637. ' },',
  22638. ' set: function (v) {',
  22639. ' rtl.raiseE("EPropReadOnly");',
  22640. ' }',
  22641. '}, 123);',
  22642. 'var $with2 = 3;',
  22643. ' $mod.THelper.DoIt.call({',
  22644. ' get: function () {',
  22645. ' return $with2;',
  22646. ' },',
  22647. ' set: function () {',
  22648. ' rtl.raiseE("EPropReadOnly");',
  22649. ' }',
  22650. ' }, 123);',
  22651. '']));
  22652. end;
  22653. procedure TTestModule.TestTypeHelper_PassFuncResult;
  22654. begin
  22655. StartProgram(false);
  22656. Add([
  22657. '{$modeswitch typehelpers}',
  22658. 'type',
  22659. ' THelper = type helper for word',
  22660. ' procedure DoIt(e: byte = 123);',
  22661. ' end;',
  22662. 'procedure THelper.DoIt(e: byte);',
  22663. 'begin',
  22664. 'end;',
  22665. 'function Foo(b: byte = 1): word;',
  22666. 'begin',
  22667. 'end;',
  22668. 'begin',
  22669. ' Foo.DoIt;',
  22670. ' Foo().DoIt;',
  22671. ' with Foo do DoIt;',
  22672. ' with Foo() do DoIt;',
  22673. '']);
  22674. ConvertProgram;
  22675. CheckSource('TestTypeHelper_PassFuncResult',
  22676. LinesToStr([ // statements
  22677. 'rtl.createHelper($mod, "THelper", null, function () {',
  22678. ' this.DoIt = function (e) {',
  22679. ' };',
  22680. '});',
  22681. 'this.Foo = function (b) {',
  22682. ' var Result = 0;',
  22683. ' return Result;',
  22684. '};',
  22685. '']),
  22686. LinesToStr([ // $mod.$main
  22687. '$mod.THelper.DoIt.call({',
  22688. ' a: $mod.Foo(1),',
  22689. ' get: function () {',
  22690. ' return this.a;',
  22691. ' },',
  22692. ' set: function (v) {',
  22693. ' this.a = v;',
  22694. ' }',
  22695. '}, 123);',
  22696. '$mod.THelper.DoIt.call({',
  22697. ' a: $mod.Foo(1),',
  22698. ' get: function () {',
  22699. ' return this.a;',
  22700. ' },',
  22701. ' set: function (v) {',
  22702. ' this.a = v;',
  22703. ' }',
  22704. '}, 123);',
  22705. 'var $with = $mod.Foo(1);',
  22706. '$mod.THelper.DoIt.call({',
  22707. ' get: function () {',
  22708. ' return $with;',
  22709. ' },',
  22710. ' set: function (v) {',
  22711. ' $with = v;',
  22712. ' }',
  22713. '}, 123);',
  22714. 'var $with1 = $mod.Foo(1);',
  22715. '$mod.THelper.DoIt.call({',
  22716. ' get: function () {',
  22717. ' return $with1;',
  22718. ' },',
  22719. ' set: function (v) {',
  22720. ' $with1 = v;',
  22721. ' }',
  22722. '}, 123);',
  22723. '']));
  22724. end;
  22725. procedure TTestModule.TestTypeHelper_PassPropertyField;
  22726. begin
  22727. StartProgram(false);
  22728. Add([
  22729. '{$modeswitch typehelpers}',
  22730. 'type',
  22731. ' TObject = class',
  22732. ' FField: word;',
  22733. ' procedure SetField(Value: word);',
  22734. ' property Field: word read FField write SetField;',
  22735. ' end;',
  22736. ' THelper = type helper for word',
  22737. ' procedure Fly;',
  22738. ' class procedure Run; static;',
  22739. ' end;',
  22740. 'procedure TObject.SetField(Value: word);',
  22741. 'begin',
  22742. ' Field.Fly;',
  22743. ' Field.Run;',
  22744. ' Self.Field.Fly;',
  22745. ' Self.Field.Run;',
  22746. ' with Self do begin',
  22747. ' Field.Fly;',
  22748. ' Field.Run;',
  22749. ' end;',
  22750. ' with Self.Field do begin',
  22751. ' Fly;',
  22752. ' Run;',
  22753. ' end;',
  22754. 'end;',
  22755. 'procedure THelper.Fly;',
  22756. 'begin',
  22757. 'end;',
  22758. 'class procedure THelper.Run;',
  22759. 'begin',
  22760. 'end;',
  22761. 'var',
  22762. ' o: TObject;',
  22763. 'begin',
  22764. ' o.Field.Fly;',
  22765. ' o.Field.Run;',
  22766. ' with o do begin',
  22767. ' Field.Fly;',
  22768. ' Field.Run;',
  22769. ' end;',
  22770. ' with o.Field do begin',
  22771. ' Fly;',
  22772. ' Run;',
  22773. ' end;',
  22774. '']);
  22775. ConvertProgram;
  22776. CheckSource('TestTypeHelper_PassPropertyField',
  22777. LinesToStr([ // statements
  22778. 'rtl.createClass($mod, "TObject", null, function () {',
  22779. ' this.$init = function () {',
  22780. ' this.FField = 0;',
  22781. ' };',
  22782. ' this.$final = function () {',
  22783. ' };',
  22784. ' this.SetField = function (Value) {',
  22785. ' $mod.THelper.Fly.call({',
  22786. ' p: this,',
  22787. ' get: function () {',
  22788. ' return this.p.FField;',
  22789. ' },',
  22790. ' set: function (v) {',
  22791. ' this.p.FField = v;',
  22792. ' }',
  22793. ' });',
  22794. ' $mod.THelper.Run();',
  22795. ' $mod.THelper.Fly.call({',
  22796. ' p: this,',
  22797. ' get: function () {',
  22798. ' return this.p.FField;',
  22799. ' },',
  22800. ' set: function (v) {',
  22801. ' this.p.FField = v;',
  22802. ' }',
  22803. ' });',
  22804. ' $mod.THelper.Run();',
  22805. ' $mod.THelper.Fly.call({',
  22806. ' p: this,',
  22807. ' get: function () {',
  22808. ' return this.p.FField;',
  22809. ' },',
  22810. ' set: function (v) {',
  22811. ' this.p.FField = v;',
  22812. ' }',
  22813. ' });',
  22814. ' $mod.THelper.Run();',
  22815. ' var $with = this.FField;',
  22816. ' $mod.THelper.Fly.call({',
  22817. ' get: function () {',
  22818. ' return $with;',
  22819. ' },',
  22820. ' set: function (v) {',
  22821. ' $with = v;',
  22822. ' }',
  22823. ' });',
  22824. ' $mod.THelper.Run();',
  22825. ' };',
  22826. '});',
  22827. 'rtl.createHelper($mod, "THelper", null, function () {',
  22828. ' this.Fly = function () {',
  22829. ' };',
  22830. ' this.Run = function () {',
  22831. ' };',
  22832. '});',
  22833. 'this.o = null;',
  22834. '']),
  22835. LinesToStr([ // $mod.$main
  22836. '$mod.THelper.Fly.call({',
  22837. ' p: $mod.o,',
  22838. ' get: function () {',
  22839. ' return this.p.FField;',
  22840. ' },',
  22841. ' set: function (v) {',
  22842. ' this.p.FField = v;',
  22843. ' }',
  22844. '});',
  22845. '$mod.THelper.Run();',
  22846. 'var $with = $mod.o;',
  22847. '$mod.THelper.Fly.call({',
  22848. ' p: $with,',
  22849. ' get: function () {',
  22850. ' return this.p.FField;',
  22851. ' },',
  22852. ' set: function (v) {',
  22853. ' this.p.FField = v;',
  22854. ' }',
  22855. '});',
  22856. '$mod.THelper.Run();',
  22857. 'var $with1 = $mod.o.FField;',
  22858. '$mod.THelper.Fly.call({',
  22859. ' get: function () {',
  22860. ' return $with1;',
  22861. ' },',
  22862. ' set: function (v) {',
  22863. ' $with1 = v;',
  22864. ' }',
  22865. '});',
  22866. '$mod.THelper.Run();',
  22867. '']));
  22868. end;
  22869. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  22870. begin
  22871. StartProgram(false);
  22872. Add([
  22873. '{$modeswitch typehelpers}',
  22874. 'type',
  22875. ' TObject = class',
  22876. ' FField: word;',
  22877. ' function GetField: word;',
  22878. ' property Field: word read GetField write FField;',
  22879. ' end;',
  22880. ' THelper = type helper for word',
  22881. ' procedure Fly;',
  22882. ' class procedure Run; static;',
  22883. ' end;',
  22884. 'function TObject.GetField: word;',
  22885. 'begin',
  22886. ' Field.Fly;',
  22887. ' Field.Run;',
  22888. ' Self.Field.Fly;',
  22889. ' Self.Field.Run;',
  22890. ' with Self do begin',
  22891. ' Field.Fly;',
  22892. ' Field.Run;',
  22893. ' end;',
  22894. ' with Self.Field do begin',
  22895. ' Fly;',
  22896. ' Run;',
  22897. ' end;',
  22898. 'end;',
  22899. 'procedure THelper.Fly;',
  22900. 'begin',
  22901. 'end;',
  22902. 'class procedure THelper.Run;',
  22903. 'begin',
  22904. 'end;',
  22905. 'var',
  22906. ' o: TObject;',
  22907. 'begin',
  22908. ' o.Field.Fly;',
  22909. ' o.Field.Run;',
  22910. ' with o do begin',
  22911. ' Field.Fly;',
  22912. ' Field.Run;',
  22913. ' end;',
  22914. ' with o.Field do begin',
  22915. ' Fly;',
  22916. ' Run;',
  22917. ' end;',
  22918. '']);
  22919. ConvertProgram;
  22920. CheckSource('TestTypeHelper_PassPropertyGetter',
  22921. LinesToStr([ // statements
  22922. 'rtl.createClass($mod, "TObject", null, function () {',
  22923. ' this.$init = function () {',
  22924. ' this.FField = 0;',
  22925. ' };',
  22926. ' this.$final = function () {',
  22927. ' };',
  22928. ' this.GetField = function () {',
  22929. ' var Result = 0;',
  22930. ' $mod.THelper.Fly.call({',
  22931. ' p: this.GetField(),',
  22932. ' get: function () {',
  22933. ' return this.p;',
  22934. ' },',
  22935. ' set: function (v) {',
  22936. ' this.p = v;',
  22937. ' }',
  22938. ' });',
  22939. ' $mod.THelper.Run();',
  22940. ' $mod.THelper.Fly.call({',
  22941. ' p: this.GetField(),',
  22942. ' get: function () {',
  22943. ' return this.p;',
  22944. ' },',
  22945. ' set: function (v) {',
  22946. ' this.p = v;',
  22947. ' }',
  22948. ' });',
  22949. ' $mod.THelper.Run();',
  22950. ' $mod.THelper.Fly.call({',
  22951. ' p: this.GetField(),',
  22952. ' get: function () {',
  22953. ' return this.p;',
  22954. ' },',
  22955. ' set: function (v) {',
  22956. ' this.p = v;',
  22957. ' }',
  22958. ' });',
  22959. ' $mod.THelper.Run();',
  22960. ' var $with = this.GetField();',
  22961. ' $mod.THelper.Fly.call({',
  22962. ' get: function () {',
  22963. ' return $with;',
  22964. ' },',
  22965. ' set: function (v) {',
  22966. ' $with = v;',
  22967. ' }',
  22968. ' });',
  22969. ' $mod.THelper.Run();',
  22970. ' return Result;',
  22971. ' };',
  22972. '});',
  22973. 'rtl.createHelper($mod, "THelper", null, function () {',
  22974. ' this.Fly = function () {',
  22975. ' };',
  22976. ' this.Run = function () {',
  22977. ' };',
  22978. '});',
  22979. 'this.o = null;',
  22980. '']),
  22981. LinesToStr([ // $mod.$main
  22982. '$mod.THelper.Fly.call({',
  22983. ' p: $mod.o.GetField(),',
  22984. ' get: function () {',
  22985. ' return this.p;',
  22986. ' },',
  22987. ' set: function (v) {',
  22988. ' this.p = v;',
  22989. ' }',
  22990. '});',
  22991. '$mod.THelper.Run();',
  22992. 'var $with = $mod.o;',
  22993. '$mod.THelper.Fly.call({',
  22994. ' p: $with.GetField(),',
  22995. ' get: function () {',
  22996. ' return this.p;',
  22997. ' },',
  22998. ' set: function (v) {',
  22999. ' this.p = v;',
  23000. ' }',
  23001. '});',
  23002. '$mod.THelper.Run();',
  23003. 'var $with1 = $mod.o.GetField();',
  23004. '$mod.THelper.Fly.call({',
  23005. ' get: function () {',
  23006. ' return $with1;',
  23007. ' },',
  23008. ' set: function (v) {',
  23009. ' $with1 = v;',
  23010. ' }',
  23011. '});',
  23012. '$mod.THelper.Run();',
  23013. '']));
  23014. end;
  23015. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23016. begin
  23017. StartProgram(false);
  23018. Add([
  23019. '{$modeswitch typehelpers}',
  23020. 'type',
  23021. ' TObject = class',
  23022. ' class var FField: word;',
  23023. ' class procedure SetField(Value: word);',
  23024. ' class property Field: word read FField write SetField;',
  23025. ' end;',
  23026. ' THelper = type helper for word',
  23027. ' procedure Fly(n: byte);',
  23028. ' end;',
  23029. 'class procedure TObject.SetField(Value: word);',
  23030. 'begin',
  23031. ' Field.Fly(1);',
  23032. ' Self.Field.Fly(2);',
  23033. ' with Self do Field.Fly(3);',
  23034. ' with Self.Field do Fly(4);',
  23035. ' TObject.Field.Fly(5);',
  23036. ' with TObject do Field.Fly(6);',
  23037. ' with TObject.Field do Fly(7);',
  23038. 'end;',
  23039. 'procedure THelper.Fly(n: byte);',
  23040. 'begin',
  23041. 'end;',
  23042. 'var',
  23043. ' o: TObject;',
  23044. 'begin',
  23045. ' o.Field.Fly(11);',
  23046. ' with o do Field.Fly(12);',
  23047. ' with o.Field do Fly(13);',
  23048. ' TObject.Field.Fly(14);',
  23049. ' with TObject do Field.Fly(15);',
  23050. ' with TObject.Field do Fly(16);',
  23051. '']);
  23052. ConvertProgram;
  23053. CheckSource('TestTypeHelper_PassClassPropertyField',
  23054. LinesToStr([ // statements
  23055. 'rtl.createClass($mod, "TObject", null, function () {',
  23056. ' this.FField = 0;',
  23057. ' this.$init = function () {',
  23058. ' };',
  23059. ' this.$final = function () {',
  23060. ' };',
  23061. ' this.SetField = function (Value) {',
  23062. ' $mod.THelper.Fly.call({',
  23063. ' p: this,',
  23064. ' get: function () {',
  23065. ' return this.p.FField;',
  23066. ' },',
  23067. ' set: function (v) {',
  23068. ' $mod.TObject.FField = v;',
  23069. ' }',
  23070. ' }, 1);',
  23071. ' $mod.THelper.Fly.call({',
  23072. ' p: this,',
  23073. ' get: function () {',
  23074. ' return this.p.FField;',
  23075. ' },',
  23076. ' set: function (v) {',
  23077. ' $mod.TObject.FField = v;',
  23078. ' }',
  23079. ' }, 2);',
  23080. ' $mod.THelper.Fly.call({',
  23081. ' p: this,',
  23082. ' get: function () {',
  23083. ' return this.p.FField;',
  23084. ' },',
  23085. ' set: function (v) {',
  23086. ' $mod.TObject.FField = v;',
  23087. ' }',
  23088. ' }, 3);',
  23089. ' var $with = this.FField;',
  23090. ' $mod.THelper.Fly.call({',
  23091. ' get: function () {',
  23092. ' return $with;',
  23093. ' },',
  23094. ' set: function (v) {',
  23095. ' $with = v;',
  23096. ' }',
  23097. ' }, 4);',
  23098. ' $mod.THelper.Fly.call({',
  23099. ' p: $mod.TObject,',
  23100. ' get: function () {',
  23101. ' return this.p.FField;',
  23102. ' },',
  23103. ' set: function (v) {',
  23104. ' $mod.TObject.FField = v;',
  23105. ' }',
  23106. ' }, 5);',
  23107. ' var $with1 = $mod.TObject;',
  23108. ' $mod.THelper.Fly.call({',
  23109. ' p: $with1,',
  23110. ' get: function () {',
  23111. ' return this.p.FField;',
  23112. ' },',
  23113. ' set: function (v) {',
  23114. ' $mod.TObject.FField = v;',
  23115. ' }',
  23116. ' }, 6);',
  23117. ' var $with2 = $mod.TObject.FField;',
  23118. ' $mod.THelper.Fly.call({',
  23119. ' get: function () {',
  23120. ' return $with2;',
  23121. ' },',
  23122. ' set: function (v) {',
  23123. ' $with2 = v;',
  23124. ' }',
  23125. ' }, 7);',
  23126. ' };',
  23127. '});',
  23128. 'rtl.createHelper($mod, "THelper", null, function () {',
  23129. ' this.Fly = function (n) {',
  23130. ' };',
  23131. '});',
  23132. 'this.o = null;',
  23133. '']),
  23134. LinesToStr([ // $mod.$main
  23135. '$mod.THelper.Fly.call({',
  23136. ' p: $mod.o,',
  23137. ' get: function () {',
  23138. ' return this.p.FField;',
  23139. ' },',
  23140. ' set: function (v) {',
  23141. ' $mod.TObject.FField = v;',
  23142. ' }',
  23143. '}, 11);',
  23144. 'var $with = $mod.o;',
  23145. '$mod.THelper.Fly.call({',
  23146. ' p: $with,',
  23147. ' get: function () {',
  23148. ' return this.p.FField;',
  23149. ' },',
  23150. ' set: function (v) {',
  23151. ' $mod.TObject.FField = v;',
  23152. ' }',
  23153. '}, 12);',
  23154. 'var $with1 = $mod.o.FField;',
  23155. '$mod.THelper.Fly.call({',
  23156. ' get: function () {',
  23157. ' return $with1;',
  23158. ' },',
  23159. ' set: function (v) {',
  23160. ' $with1 = v;',
  23161. ' }',
  23162. '}, 13);',
  23163. '$mod.THelper.Fly.call({',
  23164. ' p: $mod.TObject,',
  23165. ' get: function () {',
  23166. ' return this.p.FField;',
  23167. ' },',
  23168. ' set: function (v) {',
  23169. ' $mod.TObject.FField = v;',
  23170. ' }',
  23171. '}, 14);',
  23172. 'var $with2 = $mod.TObject;',
  23173. '$mod.THelper.Fly.call({',
  23174. ' p: $with2,',
  23175. ' get: function () {',
  23176. ' return this.p.FField;',
  23177. ' },',
  23178. ' set: function (v) {',
  23179. ' $mod.TObject.FField = v;',
  23180. ' }',
  23181. '}, 15);',
  23182. 'var $with3 = $mod.TObject.FField;',
  23183. '$mod.THelper.Fly.call({',
  23184. ' get: function () {',
  23185. ' return $with3;',
  23186. ' },',
  23187. ' set: function (v) {',
  23188. ' $with3 = v;',
  23189. ' }',
  23190. '}, 16);',
  23191. '']));
  23192. end;
  23193. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23194. begin
  23195. StartProgram(false);
  23196. Add([
  23197. '{$modeswitch typehelpers}',
  23198. 'type',
  23199. ' TObject = class',
  23200. ' class var FField: word;',
  23201. ' class function GetField: word; static;',
  23202. ' class property Field: word read GetField write FField;',
  23203. ' end;',
  23204. ' THelper = type helper for word',
  23205. ' procedure Fly(n: byte);',
  23206. ' end;',
  23207. 'class function TObject.GetField: word;',
  23208. 'begin',
  23209. ' Field.Fly(1);',
  23210. ' TObject.Field.Fly(5);',
  23211. ' with TObject do Field.Fly(6);',
  23212. ' with TObject.Field do Fly(7);',
  23213. 'end;',
  23214. 'procedure THelper.Fly(n: byte);',
  23215. 'begin',
  23216. 'end;',
  23217. 'var',
  23218. ' o: TObject;',
  23219. 'begin',
  23220. ' o.Field.Fly(11);',
  23221. ' with o do Field.Fly(12);',
  23222. ' with o.Field do Fly(13);',
  23223. '']);
  23224. ConvertProgram;
  23225. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23226. LinesToStr([ // statements
  23227. 'rtl.createClass($mod, "TObject", null, function () {',
  23228. ' this.FField = 0;',
  23229. ' this.$init = function () {',
  23230. ' };',
  23231. ' this.$final = function () {',
  23232. ' };',
  23233. ' this.GetField = function () {',
  23234. ' var Result = 0;',
  23235. ' $mod.THelper.Fly.call({',
  23236. ' p: $mod.TObject.GetField(),',
  23237. ' get: function () {',
  23238. ' return this.p;',
  23239. ' },',
  23240. ' set: function (v) {',
  23241. ' this.p = v;',
  23242. ' }',
  23243. ' }, 1);',
  23244. ' $mod.THelper.Fly.call({',
  23245. ' p: $mod.TObject.GetField(),',
  23246. ' get: function () {',
  23247. ' return this.p;',
  23248. ' },',
  23249. ' set: function (v) {',
  23250. ' this.p = v;',
  23251. ' }',
  23252. ' }, 5);',
  23253. ' var $with = $mod.TObject;',
  23254. ' $mod.THelper.Fly.call({',
  23255. ' p: $with.GetField(),',
  23256. ' get: function () {',
  23257. ' return this.p;',
  23258. ' },',
  23259. ' set: function (v) {',
  23260. ' this.p = v;',
  23261. ' }',
  23262. ' }, 6);',
  23263. ' var $with1 = $mod.TObject.GetField();',
  23264. ' $mod.THelper.Fly.call({',
  23265. ' get: function () {',
  23266. ' return $with1;',
  23267. ' },',
  23268. ' set: function (v) {',
  23269. ' $with1 = v;',
  23270. ' }',
  23271. ' }, 7);',
  23272. ' return Result;',
  23273. ' };',
  23274. '});',
  23275. 'rtl.createHelper($mod, "THelper", null, function () {',
  23276. ' this.Fly = function (n) {',
  23277. ' };',
  23278. '});',
  23279. 'this.o = null;',
  23280. '']),
  23281. LinesToStr([ // $mod.$main
  23282. '$mod.THelper.Fly.call({',
  23283. ' p: $mod.o.GetField(),',
  23284. ' get: function () {',
  23285. ' return this.p;',
  23286. ' },',
  23287. ' set: function (v) {',
  23288. ' this.p = v;',
  23289. ' }',
  23290. '}, 11);',
  23291. 'var $with = $mod.o;',
  23292. '$mod.THelper.Fly.call({',
  23293. ' p: $with.GetField(),',
  23294. ' get: function () {',
  23295. ' return this.p;',
  23296. ' },',
  23297. ' set: function (v) {',
  23298. ' this.p = v;',
  23299. ' }',
  23300. '}, 12);',
  23301. 'var $with1 = $mod.o.GetField();',
  23302. '$mod.THelper.Fly.call({',
  23303. ' get: function () {',
  23304. ' return $with1;',
  23305. ' },',
  23306. ' set: function (v) {',
  23307. ' $with1 = v;',
  23308. ' }',
  23309. '}, 13);',
  23310. '']));
  23311. end;
  23312. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23313. begin
  23314. StartProgram(false);
  23315. Add([
  23316. '{$modeswitch typehelpers}',
  23317. 'type',
  23318. ' TObject = class',
  23319. ' class var FField: word;',
  23320. ' class function GetField: word;',
  23321. ' class property Field: word read GetField write FField;',
  23322. ' end;',
  23323. ' TClass = class of TObject;',
  23324. ' THelper = type helper for word',
  23325. ' procedure Fly(n: byte);',
  23326. ' end;',
  23327. 'class function TObject.GetField: word;',
  23328. 'begin',
  23329. ' Field.Fly(1);',
  23330. ' Self.Field.Fly(5);',
  23331. ' with Self do Field.Fly(6);',
  23332. ' with Self.Field do Fly(7);',
  23333. 'end;',
  23334. 'procedure THelper.Fly(n: byte);',
  23335. 'begin',
  23336. 'end;',
  23337. 'var',
  23338. ' o: TObject;',
  23339. ' c: TClass;',
  23340. 'begin',
  23341. ' o.Field.Fly(11);',
  23342. ' with o do Field.Fly(12);',
  23343. ' with o.Field do Fly(13);',
  23344. ' c.Field.Fly(14);',
  23345. ' with c do Field.Fly(15);',
  23346. ' with c.Field do Fly(16);',
  23347. '']);
  23348. ConvertProgram;
  23349. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23350. LinesToStr([ // statements
  23351. 'rtl.createClass($mod, "TObject", null, function () {',
  23352. ' this.FField = 0;',
  23353. ' this.$init = function () {',
  23354. ' };',
  23355. ' this.$final = function () {',
  23356. ' };',
  23357. ' this.GetField = function () {',
  23358. ' var Result = 0;',
  23359. ' $mod.THelper.Fly.call({',
  23360. ' p: this.GetField(),',
  23361. ' get: function () {',
  23362. ' return this.p;',
  23363. ' },',
  23364. ' set: function (v) {',
  23365. ' this.p = v;',
  23366. ' }',
  23367. ' }, 1);',
  23368. ' $mod.THelper.Fly.call({',
  23369. ' p: this.GetField(),',
  23370. ' get: function () {',
  23371. ' return this.p;',
  23372. ' },',
  23373. ' set: function (v) {',
  23374. ' this.p = v;',
  23375. ' }',
  23376. ' }, 5);',
  23377. ' $mod.THelper.Fly.call({',
  23378. ' p: this.GetField(),',
  23379. ' get: function () {',
  23380. ' return this.p;',
  23381. ' },',
  23382. ' set: function (v) {',
  23383. ' this.p = v;',
  23384. ' }',
  23385. ' }, 6);',
  23386. ' var $with = this.GetField();',
  23387. ' $mod.THelper.Fly.call({',
  23388. ' get: function () {',
  23389. ' return $with;',
  23390. ' },',
  23391. ' set: function (v) {',
  23392. ' $with = v;',
  23393. ' }',
  23394. ' }, 7);',
  23395. ' return Result;',
  23396. ' };',
  23397. '});',
  23398. 'rtl.createHelper($mod, "THelper", null, function () {',
  23399. ' this.Fly = function (n) {',
  23400. ' };',
  23401. '});',
  23402. 'this.o = null;',
  23403. 'this.c = null;',
  23404. '']),
  23405. LinesToStr([ // $mod.$main
  23406. '$mod.THelper.Fly.call({',
  23407. ' p: $mod.o.$class.GetField(),',
  23408. ' get: function () {',
  23409. ' return this.p;',
  23410. ' },',
  23411. ' set: function (v) {',
  23412. ' this.p = v;',
  23413. ' }',
  23414. '}, 11);',
  23415. 'var $with = $mod.o;',
  23416. '$mod.THelper.Fly.call({',
  23417. ' p: $with.$class.GetField(),',
  23418. ' get: function () {',
  23419. ' return this.p;',
  23420. ' },',
  23421. ' set: function (v) {',
  23422. ' this.p = v;',
  23423. ' }',
  23424. '}, 12);',
  23425. 'var $with1 = $mod.o.$class.GetField();',
  23426. '$mod.THelper.Fly.call({',
  23427. ' get: function () {',
  23428. ' return $with1;',
  23429. ' },',
  23430. ' set: function (v) {',
  23431. ' $with1 = v;',
  23432. ' }',
  23433. '}, 13);',
  23434. '$mod.THelper.Fly.call({',
  23435. ' p: $mod.c.GetField(),',
  23436. ' get: function () {',
  23437. ' return this.p;',
  23438. ' },',
  23439. ' set: function (v) {',
  23440. ' this.p = v;',
  23441. ' }',
  23442. '}, 14);',
  23443. 'var $with2 = $mod.c;',
  23444. '$mod.THelper.Fly.call({',
  23445. ' p: $with2.GetField(),',
  23446. ' get: function () {',
  23447. ' return this.p;',
  23448. ' },',
  23449. ' set: function (v) {',
  23450. ' this.p = v;',
  23451. ' }',
  23452. '}, 15);',
  23453. 'var $with3 = $mod.c.GetField();',
  23454. '$mod.THelper.Fly.call({',
  23455. ' get: function () {',
  23456. ' return $with3;',
  23457. ' },',
  23458. ' set: function (v) {',
  23459. ' $with3 = v;',
  23460. ' }',
  23461. '}, 16);',
  23462. '']));
  23463. end;
  23464. procedure TTestModule.TestTypeHelper_Property;
  23465. begin
  23466. StartProgram(false);
  23467. Add([
  23468. '{$modeswitch typehelpers}',
  23469. 'type',
  23470. ' THelper = type helper for word',
  23471. ' function GetSize: longint;',
  23472. ' procedure SetSize(Value: longint);',
  23473. ' property Size: longint read GetSize write SetSize;',
  23474. ' end;',
  23475. 'function THelper.GetSize: longint;',
  23476. 'begin',
  23477. ' Result:=Size+1;',
  23478. ' Size:=2;',
  23479. ' Result:=Self.Size+3;',
  23480. ' Self.Size:=4;',
  23481. ' with Self do begin',
  23482. ' Result:=Size+5;',
  23483. ' Size:=6;',
  23484. ' end;',
  23485. 'end;',
  23486. 'procedure THelper.SetSize(Value: longint);',
  23487. 'begin',
  23488. 'end;',
  23489. 'var w: word;',
  23490. 'begin',
  23491. ' w:=w.Size+7;',
  23492. ' w.Size:=w+8;',
  23493. ' with w do begin',
  23494. ' w:=Size+9;',
  23495. ' Size:=w+10;',
  23496. ' end;',
  23497. '']);
  23498. ConvertProgram;
  23499. CheckSource('TestTypeHelper_Property',
  23500. LinesToStr([ // statements
  23501. 'rtl.createHelper($mod, "THelper", null, function () {',
  23502. ' this.GetSize = function () {',
  23503. ' var Result = 0;',
  23504. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  23505. ' $mod.THelper.SetSize.call(this, 2);',
  23506. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  23507. ' $mod.THelper.SetSize.call(this, 4);',
  23508. ' var $with = this.get();',
  23509. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  23510. ' $mod.THelper.SetSize.call(this, 6);',
  23511. ' return Result;',
  23512. ' };',
  23513. ' this.SetSize = function (Value) {',
  23514. ' };',
  23515. '});',
  23516. 'this.w = 0;',
  23517. '']),
  23518. LinesToStr([ // $mod.$main
  23519. '$mod.w = $mod.THelper.GetSize.call({',
  23520. ' p: $mod,',
  23521. ' get: function () {',
  23522. ' return this.p.w;',
  23523. ' },',
  23524. ' set: function (v) {',
  23525. ' this.p.w = v;',
  23526. ' }',
  23527. '}) + 7;',
  23528. '$mod.THelper.SetSize.call({',
  23529. ' p: $mod,',
  23530. ' get: function () {',
  23531. ' return this.p.w;',
  23532. ' },',
  23533. ' set: function (v) {',
  23534. ' this.p.w = v;',
  23535. ' }',
  23536. '}, $mod.w + 8);',
  23537. 'var $with = $mod.w;',
  23538. '$mod.w = $mod.THelper.GetSize.call({',
  23539. ' get: function () {',
  23540. ' return $with;',
  23541. ' },',
  23542. ' set: function (v) {',
  23543. ' $with = v;',
  23544. ' }',
  23545. '}) + 9;',
  23546. '$mod.THelper.SetSize.call({',
  23547. ' get: function () {',
  23548. ' return $with;',
  23549. ' },',
  23550. ' set: function (v) {',
  23551. ' $with = v;',
  23552. ' }',
  23553. '}, $mod.w + 10);',
  23554. '']));
  23555. end;
  23556. procedure TTestModule.TestTypeHelper_Property_Array;
  23557. begin
  23558. StartProgram(false);
  23559. Add([
  23560. '{$modeswitch typehelpers}',
  23561. 'type',
  23562. ' THelper = type helper for word',
  23563. ' function GetItems(Index: byte): boolean;',
  23564. ' procedure SetItems(Index: byte; Value: boolean);',
  23565. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  23566. ' end;',
  23567. 'function THelper.GetItems(Index: byte): boolean;',
  23568. 'begin',
  23569. ' Result:=Items[1];',
  23570. ' Items[2]:=false;',
  23571. ' Result:=Self.Items[3];',
  23572. ' Self.Items[4]:=true;',
  23573. ' with Self do begin',
  23574. ' Result:=Items[5];',
  23575. ' Items[6]:=false;',
  23576. ' end;',
  23577. 'end;',
  23578. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  23579. 'begin',
  23580. 'end;',
  23581. 'var',
  23582. ' w: word;',
  23583. ' b: boolean;',
  23584. 'begin',
  23585. ' b:=w.Items[1];',
  23586. ' w.Items[2]:=b;',
  23587. ' with w do begin',
  23588. ' b:=Items[3];',
  23589. ' Items[4]:=b;',
  23590. ' end;',
  23591. '']);
  23592. ConvertProgram;
  23593. CheckSource('TestTypeHelper_Property_Array',
  23594. LinesToStr([ // statements
  23595. 'rtl.createHelper($mod, "THelper", null, function () {',
  23596. ' this.GetItems = function (Index) {',
  23597. ' var Result = false;',
  23598. ' Result = $mod.THelper.GetItems.call(this, 1);',
  23599. ' $mod.THelper.SetItems.call(this, 2, false);',
  23600. ' Result = $mod.THelper.GetItems.call(this, 3);',
  23601. ' $mod.THelper.SetItems.call(this, 4, true);',
  23602. ' var $with = this.get();',
  23603. ' Result = $mod.THelper.GetItems.call(this, 5);',
  23604. ' $mod.THelper.SetItems.call(this, 6, false);',
  23605. ' return Result;',
  23606. ' };',
  23607. ' this.SetItems = function (Index, Value) {',
  23608. ' };',
  23609. '});',
  23610. 'this.w = 0;',
  23611. 'this.b = false;',
  23612. '']),
  23613. LinesToStr([ // $mod.$main
  23614. '$mod.b = $mod.THelper.GetItems.call({',
  23615. ' p: $mod,',
  23616. ' get: function () {',
  23617. ' return this.p.w;',
  23618. ' },',
  23619. ' set: function (v) {',
  23620. ' this.p.w = v;',
  23621. ' }',
  23622. '}, 1);',
  23623. '$mod.THelper.SetItems.call({',
  23624. ' p: $mod,',
  23625. ' get: function () {',
  23626. ' return this.p.w;',
  23627. ' },',
  23628. ' set: function (v) {',
  23629. ' this.p.w = v;',
  23630. ' }',
  23631. '}, 2, $mod.b);',
  23632. 'var $with = $mod.w;',
  23633. '$mod.b = $mod.THelper.GetItems.call({',
  23634. ' get: function () {',
  23635. ' return $with;',
  23636. ' },',
  23637. ' set: function (v) {',
  23638. ' $with = v;',
  23639. ' }',
  23640. '}, 3);',
  23641. '$mod.THelper.SetItems.call({',
  23642. ' get: function () {',
  23643. ' return $with;',
  23644. ' },',
  23645. ' set: function (v) {',
  23646. ' $with = v;',
  23647. ' }',
  23648. '}, 4, $mod.b);',
  23649. '']));
  23650. end;
  23651. procedure TTestModule.TestTypeHelper_ClassProperty;
  23652. begin
  23653. StartProgram(false);
  23654. Add([
  23655. '{$modeswitch typehelpers}',
  23656. 'type',
  23657. ' THelper = type helper for word',
  23658. ' class function GetSize: longint; static;',
  23659. ' class procedure SetSize(Value: longint); static;',
  23660. ' class property Size: longint read GetSize write SetSize;',
  23661. ' end;',
  23662. 'class function THelper.GetSize: longint;',
  23663. 'begin',
  23664. ' Result:=Size+1;',
  23665. ' Size:=2;',
  23666. 'end;',
  23667. 'class procedure THelper.SetSize(Value: longint);',
  23668. 'begin',
  23669. 'end;',
  23670. 'begin',
  23671. '']);
  23672. ConvertProgram;
  23673. CheckSource('TestTypeHelper_ClassProperty',
  23674. LinesToStr([ // statements
  23675. 'rtl.createHelper($mod, "THelper", null, function () {',
  23676. ' this.GetSize = function () {',
  23677. ' var Result = 0;',
  23678. ' Result = $mod.THelper.GetSize() + 1;',
  23679. ' $mod.THelper.SetSize(2);',
  23680. ' return Result;',
  23681. ' };',
  23682. ' this.SetSize = function (Value) {',
  23683. ' };',
  23684. '});',
  23685. '']),
  23686. LinesToStr([ // $mod.$main
  23687. '']));
  23688. end;
  23689. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  23690. begin
  23691. StartProgram(false);
  23692. Add([
  23693. '{$modeswitch typehelpers}',
  23694. 'type',
  23695. ' THelper = type helper for word',
  23696. ' class function GetItems(Index: byte): boolean; static;',
  23697. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  23698. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  23699. ' end;',
  23700. 'class function THelper.GetItems(Index: byte): boolean;',
  23701. 'begin',
  23702. ' Result:=Items[1];',
  23703. ' Items[2]:=false;',
  23704. 'end;',
  23705. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  23706. 'begin',
  23707. 'end;',
  23708. 'var',
  23709. ' w: word;',
  23710. ' b: boolean;',
  23711. 'begin',
  23712. ' b:=w.Items[1];',
  23713. ' w.Items[2]:=b;',
  23714. ' with w do begin',
  23715. ' b:=Items[3];',
  23716. ' Items[4]:=b;',
  23717. ' end;',
  23718. '']);
  23719. ConvertProgram;
  23720. CheckSource('TestTypeHelper_ClassProperty_Array',
  23721. LinesToStr([ // statements
  23722. 'rtl.createHelper($mod, "THelper", null, function () {',
  23723. ' this.GetItems = function (Index) {',
  23724. ' var Result = false;',
  23725. ' Result = $mod.THelper.GetItems(1);',
  23726. ' $mod.THelper.SetItems(2, false);',
  23727. ' return Result;',
  23728. ' };',
  23729. ' this.SetItems = function (Index, Value) {',
  23730. ' };',
  23731. '});',
  23732. 'this.w = 0;',
  23733. 'this.b = false;',
  23734. '']),
  23735. LinesToStr([ // $mod.$main
  23736. '$mod.b = $mod.THelper.GetItems(1);',
  23737. '$mod.THelper.SetItems(2, $mod.b);',
  23738. 'var $with = $mod.w;',
  23739. '$mod.b = $mod.THelper.GetItems(3);',
  23740. '$mod.THelper.SetItems(4, $mod.b);',
  23741. '']));
  23742. end;
  23743. procedure TTestModule.TestTypeHelper_ClassMethod;
  23744. begin
  23745. StartProgram(false);
  23746. Add([
  23747. '{$modeswitch typehelpers}',
  23748. 'type',
  23749. ' THelper = type helper for word',
  23750. ' class procedure DoStatic; static;',
  23751. ' end;',
  23752. 'class procedure THelper.DoStatic;',
  23753. 'begin',
  23754. ' DoStatic;',
  23755. ' DoStatic();',
  23756. 'end;',
  23757. 'var w: word;',
  23758. 'begin',
  23759. ' w.DoStatic;',
  23760. ' w.DoStatic();',
  23761. '']);
  23762. ConvertProgram;
  23763. CheckSource('TestTypeHelper_ClassMethod',
  23764. LinesToStr([ // statements
  23765. 'rtl.createHelper($mod, "THelper", null, function () {',
  23766. ' this.DoStatic = function () {',
  23767. ' $mod.THelper.DoStatic();',
  23768. ' $mod.THelper.DoStatic();',
  23769. ' };',
  23770. '});',
  23771. 'this.w = 0;',
  23772. '']),
  23773. LinesToStr([ // $mod.$main
  23774. '$mod.THelper.DoStatic();',
  23775. '$mod.THelper.DoStatic();',
  23776. '']));
  23777. end;
  23778. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  23779. begin
  23780. StartProgram(false);
  23781. Add([
  23782. '{$modeswitch typehelpers}',
  23783. 'type',
  23784. ' THelper = type helper for word',
  23785. ' procedure Run; external name ''Run'';',
  23786. ' end;',
  23787. 'var w: word;',
  23788. 'begin',
  23789. ' w.Run;',
  23790. '']);
  23791. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  23792. ConvertProgram;
  23793. end;
  23794. procedure TTestModule.TestTypeHelper_Constructor;
  23795. begin
  23796. StartProgram(false);
  23797. Add([
  23798. '{$modeswitch typehelpers}',
  23799. 'type',
  23800. ' THelper = type helper for word',
  23801. ' constructor Init(e: longint);',
  23802. ' end;',
  23803. 'constructor THelper.Init(e: longint);',
  23804. 'begin',
  23805. ' Self:=e;',
  23806. ' Init(e+1);',
  23807. 'end;',
  23808. 'var w: word;',
  23809. 'begin',
  23810. ' w:=word.Init(2);',
  23811. ' w:=w.Init(3);',
  23812. ' with word do w:=Init(4);',
  23813. ' with w do w:=Init(5);',
  23814. '']);
  23815. ConvertProgram;
  23816. CheckSource('TestTypeHelper_Constructor',
  23817. LinesToStr([ // statements
  23818. 'rtl.createHelper($mod, "THelper", null, function () {',
  23819. ' this.Init = function (e) {',
  23820. ' this.set(e);',
  23821. ' $mod.THelper.Init.call(this, e + 1);',
  23822. ' return this.get();',
  23823. ' };',
  23824. ' this.$new = function (fn, args) {',
  23825. ' return this[fn].apply({',
  23826. ' p: 0,',
  23827. ' get: function () {',
  23828. ' return this.p;',
  23829. ' },',
  23830. ' set: function (v) {',
  23831. ' this.p = v;',
  23832. ' }',
  23833. ' }, args);',
  23834. ' };',
  23835. '});',
  23836. 'this.w = 0;',
  23837. '']),
  23838. LinesToStr([ // $mod.$main
  23839. '$mod.w = $mod.THelper.$new("Init", [2]);',
  23840. '$mod.w = $mod.THelper.Init.call({',
  23841. ' p: $mod,',
  23842. ' get: function () {',
  23843. ' return this.p.w;',
  23844. ' },',
  23845. ' set: function (v) {',
  23846. ' this.p.w = v;',
  23847. ' }',
  23848. '}, 3);',
  23849. '$mod.w = $mod.THelper.$new("Init", [4]);',
  23850. 'var $with = $mod.w;',
  23851. '$mod.w = $mod.THelper.Init.call({',
  23852. ' get: function () {',
  23853. ' return $with;',
  23854. ' },',
  23855. ' set: function (v) {',
  23856. ' $with = v;',
  23857. ' }',
  23858. '}, 5);',
  23859. '']));
  23860. end;
  23861. procedure TTestModule.TestTypeHelper_Word;
  23862. begin
  23863. StartProgram(false);
  23864. Add([
  23865. '{$modeswitch typehelpers}',
  23866. 'type',
  23867. ' THelper = type helper for word',
  23868. ' procedure DoIt(e: byte = 123);',
  23869. ' end;',
  23870. 'procedure THelper.DoIt(e: byte);',
  23871. 'begin',
  23872. ' Self:=e;',
  23873. ' Self:=Self+1;',
  23874. ' with Self do Doit;',
  23875. 'end;',
  23876. 'begin',
  23877. ' word(3).DoIt;',
  23878. '']);
  23879. ConvertProgram;
  23880. CheckSource('TestTypeHelper_Word',
  23881. LinesToStr([ // statements
  23882. 'rtl.createHelper($mod, "THelper", null, function () {',
  23883. ' this.DoIt = function (e) {',
  23884. ' this.set(e);',
  23885. ' this.set(this.get() + 1);',
  23886. ' var $with = this.get();',
  23887. ' $mod.THelper.DoIt.call(this, 123);',
  23888. ' };',
  23889. '});',
  23890. '']),
  23891. LinesToStr([ // $mod.$main
  23892. '$mod.THelper.DoIt.call({',
  23893. ' get: function () {',
  23894. ' return 3;',
  23895. ' },',
  23896. ' set: function (v) {',
  23897. ' rtl.raiseE("EPropReadOnly");',
  23898. ' }',
  23899. '}, 123);',
  23900. '']));
  23901. end;
  23902. procedure TTestModule.TestTypeHelper_Boolean;
  23903. begin
  23904. StartProgram(false);
  23905. Add([
  23906. '{$modeswitch typehelpers}',
  23907. 'type',
  23908. ' Integer = longint;',
  23909. ' THelper = type helper for boolean',
  23910. ' procedure Run(e: wordbool = true);',
  23911. ' end;',
  23912. 'procedure THelper.Run(e: wordbool);',
  23913. 'begin',
  23914. ' Self:=e;',
  23915. ' Self:=not Self;',
  23916. ' with Self do Run;',
  23917. ' if Integer(Self)=0 then ;',
  23918. 'end;',
  23919. 'begin',
  23920. ' boolean(3).Run;',
  23921. '']);
  23922. ConvertProgram;
  23923. CheckSource('TestTypeHelper_Boolean',
  23924. LinesToStr([ // statements
  23925. 'rtl.createHelper($mod, "THelper", null, function () {',
  23926. ' this.Run = function (e) {',
  23927. ' this.set(e);',
  23928. ' this.set(!this.get());',
  23929. ' var $with = this.get();',
  23930. ' $mod.THelper.Run.call(this, true);',
  23931. ' if ((this.get() ? 1 : 0) === 0) ;',
  23932. ' };',
  23933. '});',
  23934. '']),
  23935. LinesToStr([ // $mod.$main
  23936. '$mod.THelper.Run.call({',
  23937. ' a: 3 != 0,',
  23938. ' get: function () {',
  23939. ' return this.a;',
  23940. ' },',
  23941. ' set: function (v) {',
  23942. ' rtl.raiseE("EPropReadOnly");',
  23943. ' }',
  23944. '}, true);',
  23945. '']));
  23946. end;
  23947. procedure TTestModule.TestTypeHelper_WordBool;
  23948. begin
  23949. StartProgram(false);
  23950. Add([
  23951. '{$modeswitch typehelpers}',
  23952. 'type',
  23953. ' Integer = longint;',
  23954. ' THelper = type helper for WordBool',
  23955. ' procedure Run(e: wordbool = true);',
  23956. ' end;',
  23957. 'procedure THelper.Run(e: wordbool);',
  23958. 'var i: integer;',
  23959. 'begin',
  23960. ' i:=Integer(Self);',
  23961. 'end;',
  23962. 'var w: wordbool;',
  23963. 'begin',
  23964. ' w.Run;',
  23965. ' wordbool(3).Run;',
  23966. '']);
  23967. ConvertProgram;
  23968. CheckSource('TestTypeHelper_WordBool',
  23969. LinesToStr([ // statements
  23970. 'rtl.createHelper($mod, "THelper", null, function () {',
  23971. ' this.Run = function (e) {',
  23972. ' var i = 0;',
  23973. ' i = (this.get() ? 1 : 0);',
  23974. ' };',
  23975. '});',
  23976. 'this.w = false;',
  23977. '']),
  23978. LinesToStr([ // $mod.$main
  23979. '$mod.THelper.Run.call({',
  23980. ' p: $mod,',
  23981. ' get: function () {',
  23982. ' return this.p.w;',
  23983. ' },',
  23984. ' set: function (v) {',
  23985. ' this.p.w = v;',
  23986. ' }',
  23987. '}, true);',
  23988. '$mod.THelper.Run.call({',
  23989. ' a: 3 != 0,',
  23990. ' get: function () {',
  23991. ' return this.a;',
  23992. ' },',
  23993. ' set: function (v) {',
  23994. ' rtl.raiseE("EPropReadOnly");',
  23995. ' }',
  23996. '}, true);',
  23997. '']));
  23998. end;
  23999. procedure TTestModule.TestTypeHelper_Double;
  24000. begin
  24001. StartProgram(false);
  24002. Add([
  24003. '{$modeswitch typehelpers}',
  24004. 'type',
  24005. ' Float = type double;',
  24006. ' THelper = type helper for Float',
  24007. ' const NPI = 3.141592;',
  24008. ' function ToStr: String;',
  24009. ' end;',
  24010. 'function THelper.ToStr: String;',
  24011. 'begin',
  24012. 'end;',
  24013. 'procedure DoIt(s: string);',
  24014. 'begin',
  24015. 'end;',
  24016. 'var f: Float;',
  24017. 'begin',
  24018. ' DoIt(f.toStr);',
  24019. ' DoIt(f.toStr());',
  24020. ' (f*f).toStr;',
  24021. ' DoIt((f*f).toStr);',
  24022. '']);
  24023. ConvertProgram;
  24024. CheckSource('TestTypeHelper_Double',
  24025. LinesToStr([ // statements
  24026. 'rtl.createHelper($mod, "THelper", null, function () {',
  24027. ' this.NPI = 3.141592;',
  24028. ' this.ToStr = function () {',
  24029. ' var Result = "";',
  24030. ' return Result;',
  24031. ' };',
  24032. '});',
  24033. 'this.DoIt = function (s) {',
  24034. '};',
  24035. 'this.f = 0.0;',
  24036. '']),
  24037. LinesToStr([ // $mod.$main
  24038. '$mod.DoIt($mod.THelper.ToStr.call({',
  24039. ' p: $mod,',
  24040. ' get: function () {',
  24041. ' return this.p.f;',
  24042. ' },',
  24043. ' set: function (v) {',
  24044. ' this.p.f = v;',
  24045. ' }',
  24046. '}));',
  24047. '$mod.DoIt($mod.THelper.ToStr.call({',
  24048. ' p: $mod,',
  24049. ' get: function () {',
  24050. ' return this.p.f;',
  24051. ' },',
  24052. ' set: function (v) {',
  24053. ' this.p.f = v;',
  24054. ' }',
  24055. '}));',
  24056. '$mod.THelper.ToStr.call({',
  24057. ' a: $mod.f * $mod.f,',
  24058. ' get: function () {',
  24059. ' return this.a;',
  24060. ' },',
  24061. ' set: function (v) {',
  24062. ' rtl.raiseE("EPropReadOnly");',
  24063. ' }',
  24064. '});',
  24065. '$mod.DoIt($mod.THelper.ToStr.call({',
  24066. ' a: $mod.f * $mod.f,',
  24067. ' get: function () {',
  24068. ' return this.a;',
  24069. ' },',
  24070. ' set: function (v) {',
  24071. ' rtl.raiseE("EPropReadOnly");',
  24072. ' }',
  24073. '}));',
  24074. '']));
  24075. end;
  24076. procedure TTestModule.TestTypeHelper_NativeInt;
  24077. begin
  24078. StartProgram(false);
  24079. Add([
  24080. '{$modeswitch typehelpers}',
  24081. 'type',
  24082. ' MaxInt = type nativeint;',
  24083. ' THelperI = type helper for MaxInt',
  24084. ' function ToStr: String;',
  24085. ' end;',
  24086. ' MaxUInt = type nativeuint;',
  24087. ' THelperU = type helper for MaxUInt',
  24088. ' function ToStr: String;',
  24089. ' end;',
  24090. 'function THelperI.ToStr: String;',
  24091. 'begin',
  24092. ' Result:=str(Self);',
  24093. 'end;',
  24094. 'function THelperU.ToStr: String;',
  24095. 'begin',
  24096. ' Result:=str(Self);',
  24097. 'end;',
  24098. 'procedure DoIt(s: string);',
  24099. 'begin',
  24100. 'end;',
  24101. 'var i: MaxInt;',
  24102. 'begin',
  24103. ' DoIt(i.toStr);',
  24104. ' DoIt(i.toStr());',
  24105. ' (i*i).toStr;',
  24106. ' DoIt((i*i).toStr);',
  24107. '']);
  24108. ConvertProgram;
  24109. CheckSource('TestTypeHelper_NativeInt',
  24110. LinesToStr([ // statements
  24111. 'rtl.createHelper($mod, "THelperI", null, function () {',
  24112. ' this.ToStr = function () {',
  24113. ' var Result = "";',
  24114. ' Result = "" + this.get();',
  24115. ' return Result;',
  24116. ' };',
  24117. '});',
  24118. 'rtl.createHelper($mod, "THelperU", null, function () {',
  24119. ' this.ToStr = function () {',
  24120. ' var Result = "";',
  24121. ' Result = "" + this.get();',
  24122. ' return Result;',
  24123. ' };',
  24124. '});',
  24125. 'this.DoIt = function (s) {',
  24126. '};',
  24127. 'this.i = 0;',
  24128. '']),
  24129. LinesToStr([ // $mod.$main
  24130. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24131. ' p: $mod,',
  24132. ' get: function () {',
  24133. ' return this.p.i;',
  24134. ' },',
  24135. ' set: function (v) {',
  24136. ' this.p.i = v;',
  24137. ' }',
  24138. '}));',
  24139. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24140. ' p: $mod,',
  24141. ' get: function () {',
  24142. ' return this.p.i;',
  24143. ' },',
  24144. ' set: function (v) {',
  24145. ' this.p.i = v;',
  24146. ' }',
  24147. '}));',
  24148. '$mod.THelperI.ToStr.call({',
  24149. ' a: $mod.i * $mod.i,',
  24150. ' get: function () {',
  24151. ' return this.a;',
  24152. ' },',
  24153. ' set: function (v) {',
  24154. ' rtl.raiseE("EPropReadOnly");',
  24155. ' }',
  24156. '});',
  24157. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24158. ' a: $mod.i * $mod.i,',
  24159. ' get: function () {',
  24160. ' return this.a;',
  24161. ' },',
  24162. ' set: function (v) {',
  24163. ' rtl.raiseE("EPropReadOnly");',
  24164. ' }',
  24165. '}));',
  24166. '']));
  24167. end;
  24168. procedure TTestModule.TestTypeHelper_StringChar;
  24169. begin
  24170. StartProgram(false);
  24171. Add([
  24172. '{$modeswitch typehelpers}',
  24173. 'type',
  24174. ' TStringHelper = type helper for string',
  24175. ' procedure DoIt(e: byte = 123);',
  24176. ' end;',
  24177. ' TCharHelper = type helper for char',
  24178. ' procedure Fly;',
  24179. ' end;',
  24180. 'procedure TStringHelper.DoIt(e: byte);',
  24181. 'begin',
  24182. ' Self[1]:=''c'';',
  24183. ' Self[2]:=Self[3];',
  24184. 'end;',
  24185. 'procedure TCharHelper.Fly;',
  24186. 'begin',
  24187. ' Self:=''c'';',
  24188. 'end;',
  24189. 'begin',
  24190. ' ''abc''.DoIt;',
  24191. ' ''xyz''.DoIt();',
  24192. ' ''c''.Fly();',
  24193. '']);
  24194. ConvertProgram;
  24195. CheckSource('TestTypeHelper_StringChar',
  24196. LinesToStr([ // statements
  24197. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  24198. ' this.DoIt = function (e) {',
  24199. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24200. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24201. ' };',
  24202. '});',
  24203. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  24204. ' this.Fly = function () {',
  24205. ' this.set("c");',
  24206. ' };',
  24207. '});',
  24208. '']),
  24209. LinesToStr([ // $mod.$main
  24210. '$mod.TStringHelper.DoIt.call({',
  24211. ' get: function () {',
  24212. ' return "abc";',
  24213. ' },',
  24214. ' set: function (v) {',
  24215. ' rtl.raiseE("EPropReadOnly");',
  24216. ' }',
  24217. '}, 123);',
  24218. '$mod.TStringHelper.DoIt.call({',
  24219. ' get: function () {',
  24220. ' return "xyz";',
  24221. ' },',
  24222. ' set: function (v) {',
  24223. ' rtl.raiseE("EPropReadOnly");',
  24224. ' }',
  24225. '}, 123);',
  24226. '$mod.TCharHelper.Fly.call({',
  24227. ' get: function () {',
  24228. ' return "c";',
  24229. ' },',
  24230. ' set: function (v) {',
  24231. ' rtl.raiseE("EPropReadOnly");',
  24232. ' }',
  24233. '});',
  24234. '']));
  24235. end;
  24236. procedure TTestModule.TestTypeHelper_JSValue;
  24237. begin
  24238. StartProgram(false);
  24239. Add([
  24240. '{$modeswitch typehelpers}',
  24241. 'type',
  24242. ' TExtValue = type jsvalue;',
  24243. ' THelper = type helper for TExtValue',
  24244. ' function ToStr: String;',
  24245. ' end;',
  24246. 'function THelper.ToStr: String;',
  24247. 'begin',
  24248. 'end;',
  24249. 'var',
  24250. ' s: string;',
  24251. ' v: TExtValue;',
  24252. 'begin',
  24253. ' s:=v.toStr;',
  24254. ' s:=v.toStr();',
  24255. ' TExtValue(s).toStr;',
  24256. '']);
  24257. ConvertProgram;
  24258. CheckSource('TestTypeHelper_JSValue',
  24259. LinesToStr([ // statements
  24260. 'rtl.createHelper($mod, "THelper", null, function () {',
  24261. ' this.ToStr = function () {',
  24262. ' var Result = "";',
  24263. ' return Result;',
  24264. ' };',
  24265. '});',
  24266. 'this.s = "";',
  24267. 'this.v = undefined;',
  24268. '']),
  24269. LinesToStr([ // $mod.$main
  24270. '$mod.s = $mod.THelper.ToStr.call({',
  24271. ' p: $mod,',
  24272. ' get: function () {',
  24273. ' return this.p.v;',
  24274. ' },',
  24275. ' set: function (v) {',
  24276. ' this.p.v = v;',
  24277. ' }',
  24278. '});',
  24279. '$mod.s = $mod.THelper.ToStr.call({',
  24280. ' p: $mod,',
  24281. ' get: function () {',
  24282. ' return this.p.v;',
  24283. ' },',
  24284. ' set: function (v) {',
  24285. ' this.p.v = v;',
  24286. ' }',
  24287. '});',
  24288. '$mod.THelper.ToStr.call({',
  24289. ' p: $mod,',
  24290. ' get: function () {',
  24291. ' return this.p.s;',
  24292. ' },',
  24293. ' set: function (v) {',
  24294. ' rtl.raiseE("EPropReadOnly");',
  24295. ' }',
  24296. '});',
  24297. '']));
  24298. end;
  24299. procedure TTestModule.TestTypeHelper_Array;
  24300. begin
  24301. StartProgram(false);
  24302. Add([
  24303. '{$modeswitch typehelpers}',
  24304. 'type',
  24305. ' TArrOfBool = array of boolean;',
  24306. ' TArrOfJS = array of jsvalue;',
  24307. ' THelper = type helper for TArrOfBool',
  24308. ' procedure DoIt(e: byte = 123);',
  24309. ' end;',
  24310. 'procedure THelper.DoIt(e: byte);',
  24311. 'begin',
  24312. ' Self[1]:=true;',
  24313. ' Self[2]:=not Self[3];',
  24314. ' SetLength(Self,4);',
  24315. 'end;',
  24316. 'var',
  24317. ' b: TArrOfBool;',
  24318. ' j: TArrOfJS;',
  24319. 'begin',
  24320. ' b.DoIt;',
  24321. ' TArrOfBool(j).DoIt();',
  24322. '']);
  24323. ConvertProgram;
  24324. CheckSource('TestTypeHelper_Array',
  24325. LinesToStr([ // statements
  24326. 'rtl.createHelper($mod, "THelper", null, function () {',
  24327. ' this.DoIt = function (e) {',
  24328. ' this.get()[1] = true;',
  24329. ' this.get()[2] = !this.get()[3];',
  24330. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24331. ' };',
  24332. '});',
  24333. 'this.b = [];',
  24334. 'this.j = [];',
  24335. '']),
  24336. LinesToStr([ // $mod.$main
  24337. '$mod.THelper.DoIt.call({',
  24338. ' p: $mod,',
  24339. ' get: function () {',
  24340. ' return this.p.b;',
  24341. ' },',
  24342. ' set: function (v) {',
  24343. ' this.p.b = v;',
  24344. ' }',
  24345. '}, 123);',
  24346. '$mod.THelper.DoIt.call({',
  24347. ' p: $mod,',
  24348. ' get: function () {',
  24349. ' return this.p.j;',
  24350. ' },',
  24351. ' set: function (v) {',
  24352. ' this.p.j = v;',
  24353. ' }',
  24354. '}, 123);',
  24355. '']));
  24356. end;
  24357. procedure TTestModule.TestTypeHelper_EnumType;
  24358. begin
  24359. StartProgram(false);
  24360. Add([
  24361. '{$modeswitch typehelpers}',
  24362. 'type',
  24363. ' TEnum = (red,blue);',
  24364. ' THelper = type helper for TEnum',
  24365. ' procedure DoIt(e: byte = 123);',
  24366. ' class procedure Swing(w: word); static;',
  24367. ' end;',
  24368. 'procedure THelper.DoIt(e: byte);',
  24369. 'begin',
  24370. ' Self:=red;',
  24371. ' Self:=succ(Self);',
  24372. ' with Self do Doit;',
  24373. 'end;',
  24374. 'class procedure THelper.Swing(w: word);',
  24375. 'begin',
  24376. 'end;',
  24377. 'var e: TEnum;',
  24378. 'begin',
  24379. ' e.DoIt;',
  24380. ' red.DoIt;',
  24381. ' TEnum.blue.DoIt;',
  24382. ' TEnum(1).DoIt;',
  24383. ' TEnum.Swing(3);',
  24384. '']);
  24385. ConvertProgram;
  24386. CheckSource('TestTypeHelper_EnumType',
  24387. LinesToStr([ // statements
  24388. 'this.TEnum = {',
  24389. ' "0": "red",',
  24390. ' red: 0,',
  24391. ' "1": "blue",',
  24392. ' blue: 1',
  24393. '};',
  24394. 'rtl.createHelper($mod, "THelper", null, function () {',
  24395. ' this.DoIt = function (e) {',
  24396. ' this.set($mod.TEnum.red);',
  24397. ' this.set(this.get() + 1);',
  24398. ' var $with = this.get();',
  24399. ' $mod.THelper.DoIt.call(this, 123);',
  24400. ' };',
  24401. ' this.Swing = function (w) {',
  24402. ' };',
  24403. '});',
  24404. 'this.e = 0;',
  24405. '']),
  24406. LinesToStr([ // $mod.$main
  24407. '$mod.THelper.DoIt.call({',
  24408. ' p: $mod,',
  24409. ' get: function () {',
  24410. ' return this.p.e;',
  24411. ' },',
  24412. ' set: function (v) {',
  24413. ' this.p.e = v;',
  24414. ' }',
  24415. '}, 123);',
  24416. '$mod.THelper.DoIt.call({',
  24417. ' p: $mod.TEnum,',
  24418. ' get: function () {',
  24419. ' return this.p.red;',
  24420. ' },',
  24421. ' set: function (v) {',
  24422. ' rtl.raiseE("EPropReadOnly");',
  24423. ' }',
  24424. '}, 123);',
  24425. '$mod.THelper.DoIt.call({',
  24426. ' p: $mod.TEnum,',
  24427. ' get: function () {',
  24428. ' return this.p.blue;',
  24429. ' },',
  24430. ' set: function (v) {',
  24431. ' rtl.raiseE("EPropReadOnly");',
  24432. ' }',
  24433. '}, 123);',
  24434. '$mod.THelper.DoIt.call({',
  24435. ' get: function () {',
  24436. ' return 1;',
  24437. ' },',
  24438. ' set: function (v) {',
  24439. ' rtl.raiseE("EPropReadOnly");',
  24440. ' }',
  24441. '}, 123);',
  24442. '$mod.THelper.Swing(3);',
  24443. '']));
  24444. end;
  24445. procedure TTestModule.TestTypeHelper_SetType;
  24446. begin
  24447. StartProgram(false);
  24448. Add([
  24449. '{$modeswitch typehelpers}',
  24450. 'type',
  24451. ' TEnum = (red,blue);',
  24452. ' TSetOfEnum = set of TEnum;',
  24453. ' THelper = type helper for TSetOfEnum',
  24454. ' procedure DoIt(e: byte = 123);',
  24455. ' constructor Init(e: TEnum);',
  24456. ' constructor InitEmpty;',
  24457. ' end;',
  24458. 'procedure THelper.DoIt(e: byte);',
  24459. 'begin',
  24460. ' Self:=[];',
  24461. ' Self:=[red];',
  24462. ' Include(Self,blue);',
  24463. 'end;',
  24464. 'constructor THelper.Init(e: TEnum);',
  24465. 'begin',
  24466. ' Self:=[];',
  24467. ' Self:=[e];',
  24468. ' Include(Self,blue);',
  24469. 'end;',
  24470. 'constructor THelper.InitEmpty;',
  24471. 'begin',
  24472. 'end;',
  24473. 'var s: TSetOfEnum;',
  24474. 'begin',
  24475. ' s.DoIt;',
  24476. //' [red].DoIt;',
  24477. //' with s do DoIt;',
  24478. //' with [red,blue] do DoIt;',
  24479. ' s:=TSetOfEnum.Init(blue);',
  24480. ' s:=s.Init(blue);',
  24481. '']);
  24482. ConvertProgram;
  24483. CheckSource('TestTypeHelper_SetType',
  24484. LinesToStr([ // statements
  24485. 'this.TEnum = {',
  24486. ' "0": "red",',
  24487. ' red: 0,',
  24488. ' "1": "blue",',
  24489. ' blue: 1',
  24490. '};',
  24491. 'rtl.createHelper($mod, "THelper", null, function () {',
  24492. ' this.DoIt = function (e) {',
  24493. ' this.set({});',
  24494. ' this.set(rtl.createSet($mod.TEnum.red));',
  24495. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24496. ' };',
  24497. ' this.Init = function (e) {',
  24498. ' this.set({});',
  24499. ' this.set(rtl.createSet(e));',
  24500. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24501. ' return this.get();',
  24502. ' };',
  24503. ' this.InitEmpty = function () {',
  24504. ' return this.get();',
  24505. ' };',
  24506. ' this.$new = function (fn, args) {',
  24507. ' return this[fn].apply({',
  24508. ' p: {},',
  24509. ' get: function () {',
  24510. ' return this.p;',
  24511. ' },',
  24512. ' set: function (v) {',
  24513. ' this.p = v;',
  24514. ' }',
  24515. ' }, args);',
  24516. ' };',
  24517. '});',
  24518. 'this.s = {};',
  24519. '']),
  24520. LinesToStr([ // $mod.$main
  24521. '$mod.THelper.DoIt.call({',
  24522. ' p: $mod,',
  24523. ' get: function () {',
  24524. ' return this.p.s;',
  24525. ' },',
  24526. ' set: function (v) {',
  24527. ' this.p.s = v;',
  24528. ' }',
  24529. '}, 123);',
  24530. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  24531. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  24532. ' p: $mod,',
  24533. ' get: function () {',
  24534. ' return this.p.s;',
  24535. ' },',
  24536. ' set: function (v) {',
  24537. ' this.p.s = v;',
  24538. ' }',
  24539. '}, $mod.TEnum.blue));',
  24540. '']));
  24541. end;
  24542. procedure TTestModule.TestTypeHelper_InterfaceType;
  24543. begin
  24544. StartProgram(false);
  24545. Add([
  24546. '{$interfaces com}',
  24547. '{$modeswitch typehelpers}',
  24548. 'type',
  24549. ' IUnknown = interface',
  24550. ' function _AddRef: longint;',
  24551. ' function _Release: longint;',
  24552. ' end;',
  24553. ' TObject = class(IUnknown)',
  24554. ' function _AddRef: longint; virtual; abstract;',
  24555. ' function _Release: longint; virtual; abstract;',
  24556. ' end;',
  24557. ' THelper = type helper for IUnknown',
  24558. ' procedure Fly(e: byte = 123);',
  24559. ' class procedure Run; static;',
  24560. ' end;',
  24561. 'var',
  24562. ' i: IUnknown;',
  24563. ' o: TObject;',
  24564. 'procedure THelper.Fly(e: byte);',
  24565. 'begin',
  24566. ' i:=Self;',
  24567. ' o:=Self as TObject;',
  24568. ' Self:=nil;',
  24569. ' Self:=i;',
  24570. ' Self:=o;',
  24571. ' with Self do begin',
  24572. ' Fly;',
  24573. ' Fly();',
  24574. ' end;',
  24575. 'end;',
  24576. 'class procedure THelper.Run;',
  24577. 'var l: IUnknown;',
  24578. 'begin',
  24579. ' l.Fly;',
  24580. ' l.Fly();',
  24581. 'end;',
  24582. 'begin',
  24583. ' i.Fly;',
  24584. ' i.Fly();',
  24585. ' i.Run;',
  24586. ' i.Run();',
  24587. ' IUnknown.Run;',
  24588. ' IUnknown.Run();',
  24589. '']);
  24590. ConvertProgram;
  24591. CheckSource('TestTypeHelper_InterfaceType',
  24592. LinesToStr([ // statements
  24593. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  24594. 'rtl.createClass($mod, "TObject", null, function () {',
  24595. ' this.$init = function () {',
  24596. ' };',
  24597. ' this.$final = function () {',
  24598. ' };',
  24599. ' rtl.addIntf(this, $mod.IUnknown);',
  24600. '});',
  24601. 'rtl.createHelper($mod, "THelper", null, function () {',
  24602. ' this.Fly = function (e) {',
  24603. ' var $ir = rtl.createIntfRefs();',
  24604. ' try {',
  24605. ' rtl.setIntfP($mod, "i", this.get());',
  24606. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  24607. ' this.set(null);',
  24608. ' this.set($mod.i);',
  24609. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  24610. ' var $with = this.get();',
  24611. ' $mod.THelper.Fly.call(this, 123);',
  24612. ' $mod.THelper.Fly.call(this, 123);',
  24613. ' } finally {',
  24614. ' $ir.free();',
  24615. ' };',
  24616. ' };',
  24617. ' this.Run = function () {',
  24618. ' var l = null;',
  24619. ' try {',
  24620. ' $mod.THelper.Fly.call({',
  24621. ' get: function () {',
  24622. ' return l;',
  24623. ' },',
  24624. ' set: function (v) {',
  24625. ' l = rtl.setIntfL(l, v);',
  24626. ' }',
  24627. ' }, 123);',
  24628. ' $mod.THelper.Fly.call({',
  24629. ' get: function () {',
  24630. ' return l;',
  24631. ' },',
  24632. ' set: function (v) {',
  24633. ' l = rtl.setIntfL(l, v);',
  24634. ' }',
  24635. ' }, 123);',
  24636. ' } finally {',
  24637. ' rtl._Release(l);',
  24638. ' };',
  24639. ' };',
  24640. '});',
  24641. 'this.i = null;',
  24642. 'this.o = null;',
  24643. '']),
  24644. LinesToStr([ // $mod.$main
  24645. '$mod.THelper.Fly.call({',
  24646. ' p: $mod,',
  24647. ' get: function () {',
  24648. ' return this.p.i;',
  24649. ' },',
  24650. ' set: function (v) {',
  24651. ' rtl.setIntfP(this.p, "i", v);',
  24652. ' }',
  24653. '}, 123);',
  24654. '$mod.THelper.Fly.call({',
  24655. ' p: $mod,',
  24656. ' get: function () {',
  24657. ' return this.p.i;',
  24658. ' },',
  24659. ' set: function (v) {',
  24660. ' rtl.setIntfP(this.p, "i", v);',
  24661. ' }',
  24662. '}, 123);',
  24663. '$mod.THelper.Run();',
  24664. '$mod.THelper.Run();',
  24665. '$mod.THelper.Run();',
  24666. '$mod.THelper.Run();',
  24667. '']));
  24668. end;
  24669. procedure TTestModule.TestTypeHelper_NestedSelf;
  24670. begin
  24671. StartProgram(false);
  24672. Add([
  24673. '{$modeswitch typehelpers}',
  24674. 'type',
  24675. ' THelper = type helper for string',
  24676. ' procedure Run(Value: string);',
  24677. ' end;',
  24678. 'procedure THelper.Run(Value: string);',
  24679. ' function Sub(i: nativeint): boolean;',
  24680. ' begin',
  24681. ' Result:=Self[i+1]=Value[i];',
  24682. ' end;',
  24683. 'begin',
  24684. ' if Self[3]=Value[4] then ;',
  24685. 'end;',
  24686. 'begin',
  24687. '']);
  24688. ConvertProgram;
  24689. CheckSource('TestTypeHelper_NestedSelf',
  24690. LinesToStr([ // statements
  24691. 'rtl.createHelper($mod, "THelper", null, function () {',
  24692. ' this.Run = function (Value) {',
  24693. ' var $Self = this;',
  24694. ' function Sub(i) {',
  24695. ' var Result = false;',
  24696. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  24697. ' return Result;',
  24698. ' };',
  24699. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  24700. ' };',
  24701. '});',
  24702. '']),
  24703. LinesToStr([ // $mod.$main
  24704. '']));
  24705. end;
  24706. procedure TTestModule.TestProcType;
  24707. begin
  24708. StartProgram(false);
  24709. Add([
  24710. 'type',
  24711. ' TProcInt = procedure(vI: longint = 1);',
  24712. 'procedure DoIt(vJ: longint);',
  24713. 'begin end;',
  24714. 'var',
  24715. ' b: boolean;',
  24716. ' vP, vQ: tprocint;',
  24717. 'begin',
  24718. ' vp:=nil;',
  24719. ' vp:=vp;',
  24720. ' vp:=@doit;',
  24721. ' vp;',
  24722. ' vp();',
  24723. ' vp(2);',
  24724. ' b:=vp=nil;',
  24725. ' b:=nil=vp;',
  24726. ' b:=vp=vq;',
  24727. ' b:=vp=@doit;',
  24728. ' b:=@doit=vp;',
  24729. ' b:=vp<>nil;',
  24730. ' b:=nil<>vp;',
  24731. ' b:=vp<>vq;',
  24732. ' b:=vp<>@doit;',
  24733. ' b:=@doit<>vp;',
  24734. ' b:=Assigned(vp);',
  24735. ' if Assigned(vp) then ;']);
  24736. ConvertProgram;
  24737. CheckSource('TestProcType',
  24738. LinesToStr([ // statements
  24739. 'this.DoIt = function(vJ) {',
  24740. '};',
  24741. 'this.b = false;',
  24742. 'this.vP = null;',
  24743. 'this.vQ = null;'
  24744. ]),
  24745. LinesToStr([ // $mod.$main
  24746. '$mod.vP = null;',
  24747. '$mod.vP = $mod.vP;',
  24748. '$mod.vP = $mod.DoIt;',
  24749. '$mod.vP(1);',
  24750. '$mod.vP(1);',
  24751. '$mod.vP(2);',
  24752. '$mod.b = $mod.vP === null;',
  24753. '$mod.b = null === $mod.vP;',
  24754. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24755. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24756. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24757. '$mod.b = $mod.vP !== null;',
  24758. '$mod.b = null !== $mod.vP;',
  24759. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24760. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24761. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24762. '$mod.b = $mod.vP != null;',
  24763. 'if ($mod.vP != null) ;',
  24764. '']));
  24765. end;
  24766. procedure TTestModule.TestProcType_Arg;
  24767. begin
  24768. StartProgram(false);
  24769. Add([
  24770. 'type',
  24771. ' TProcInt = procedure(vI: longint = 1);',
  24772. 'procedure DoIt(vJ: longint); begin end;',
  24773. 'procedure DoSome(vP, vQ: TProcInt);',
  24774. 'var',
  24775. ' b: boolean;',
  24776. 'begin',
  24777. ' vp:=nil;',
  24778. ' vp:=vp;',
  24779. ' vp:=@doit;',
  24780. ' vp;',
  24781. ' vp();',
  24782. ' vp(2);',
  24783. ' b:=vp=nil;',
  24784. ' b:=nil=vp;',
  24785. ' b:=vp=vq;',
  24786. ' b:=vp=@doit;',
  24787. ' b:=@doit=vp;',
  24788. ' b:=vp<>nil;',
  24789. ' b:=nil<>vp;',
  24790. ' b:=vp<>vq;',
  24791. ' b:=vp<>@doit;',
  24792. ' b:=@doit<>vp;',
  24793. ' b:=Assigned(vp);',
  24794. ' if Assigned(vp) then ;',
  24795. 'end;',
  24796. 'begin',
  24797. ' DoSome(@DoIt,nil);']);
  24798. ConvertProgram;
  24799. CheckSource('TestProcType_Arg',
  24800. LinesToStr([ // statements
  24801. 'this.DoIt = function(vJ) {',
  24802. '};',
  24803. 'this.DoSome = function(vP, vQ) {',
  24804. ' var b = false;',
  24805. ' vP = null;',
  24806. ' vP = vP;',
  24807. ' vP = $mod.DoIt;',
  24808. ' vP(1);',
  24809. ' vP(1);',
  24810. ' vP(2);',
  24811. ' b = vP === null;',
  24812. ' b = null === vP;',
  24813. ' b = rtl.eqCallback(vP,vQ);',
  24814. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  24815. ' b = rtl.eqCallback($mod.DoIt, vP);',
  24816. ' b = vP !== null;',
  24817. ' b = null !== vP;',
  24818. ' b = !rtl.eqCallback(vP, vQ);',
  24819. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  24820. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  24821. ' b = vP != null;',
  24822. ' if (vP != null) ;',
  24823. '};',
  24824. '']),
  24825. LinesToStr([ // $mod.$main
  24826. '$mod.DoSome($mod.DoIt,null);',
  24827. '']));
  24828. end;
  24829. procedure TTestModule.TestProcType_FunctionFPC;
  24830. begin
  24831. StartProgram(false);
  24832. Add('type');
  24833. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24834. Add('function DoIt(vI: longint): longint;');
  24835. Add('begin end;');
  24836. Add('var');
  24837. Add(' b: boolean;');
  24838. Add(' vP, vQ: tfuncint;');
  24839. Add('begin');
  24840. Add(' vp:=nil;');
  24841. Add(' vp:=vp;');
  24842. Add(' vp:=@doit;'); // ok in fpc and delphi
  24843. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24844. Add(' vp;'); // ok in fpc and delphi
  24845. Add(' vp();');
  24846. Add(' vp(2);');
  24847. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24848. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24849. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24850. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24851. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24852. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  24853. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  24854. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24855. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24856. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24857. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24858. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24859. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  24860. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  24861. Add(' b:=Assigned(vp);');
  24862. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  24863. Add(' doit(vp());'); // ok in fpc and delphi
  24864. Add(' doit(vp(2));'); // ok in fpc and delphi
  24865. ConvertProgram;
  24866. CheckSource('TestProcType_FunctionFPC',
  24867. LinesToStr([ // statements
  24868. 'this.DoIt = function(vI) {',
  24869. ' var Result = 0;',
  24870. ' return Result;',
  24871. '};',
  24872. 'this.b = false;',
  24873. 'this.vP = null;',
  24874. 'this.vQ = null;'
  24875. ]),
  24876. LinesToStr([ // $mod.$main
  24877. '$mod.vP = null;',
  24878. '$mod.vP = $mod.vP;',
  24879. '$mod.vP = $mod.DoIt;',
  24880. '$mod.vP(1);',
  24881. '$mod.vP(1);',
  24882. '$mod.vP(2);',
  24883. '$mod.b = $mod.vP === null;',
  24884. '$mod.b = null === $mod.vP;',
  24885. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24886. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24887. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24888. '$mod.b = 4 === $mod.vP(1);',
  24889. '$mod.b = $mod.vP !== null;',
  24890. '$mod.b = null !== $mod.vP;',
  24891. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24892. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24893. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24894. '$mod.b = 6 !== $mod.vP(1);',
  24895. '$mod.b = $mod.vP != null;',
  24896. '$mod.DoIt($mod.vP(1));',
  24897. '$mod.DoIt($mod.vP(2));',
  24898. '']));
  24899. end;
  24900. procedure TTestModule.TestProcType_FunctionDelphi;
  24901. begin
  24902. StartProgram(false);
  24903. Add('{$mode Delphi}');
  24904. Add('type');
  24905. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24906. Add('function DoIt(vI: longint): longint;');
  24907. Add('begin end;');
  24908. Add('var');
  24909. Add(' b: boolean;');
  24910. Add(' vP, vQ: tfuncint;');
  24911. Add('begin');
  24912. Add(' vp:=nil;');
  24913. Add(' vp:=vp;');
  24914. Add(' vp:=@doit;'); // ok in fpc and delphi
  24915. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24916. Add(' vp;'); // ok in fpc and delphi
  24917. Add(' vp();');
  24918. Add(' vp(2);');
  24919. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24920. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24921. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24922. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24923. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24924. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  24925. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  24926. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24927. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24928. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24929. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24930. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24931. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  24932. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  24933. Add(' b:=Assigned(vp);');
  24934. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  24935. Add(' doit(vp());'); // ok in fpc and delphi
  24936. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  24937. ConvertProgram;
  24938. CheckSource('TestProcType_FunctionDelphi',
  24939. LinesToStr([ // statements
  24940. 'this.DoIt = function(vI) {',
  24941. ' var Result = 0;',
  24942. ' return Result;',
  24943. '};',
  24944. 'this.b = false;',
  24945. 'this.vP = null;',
  24946. 'this.vQ = null;'
  24947. ]),
  24948. LinesToStr([ // $mod.$main
  24949. '$mod.vP = null;',
  24950. '$mod.vP = $mod.vP;',
  24951. '$mod.vP = $mod.DoIt;',
  24952. '$mod.vP = $mod.DoIt;',
  24953. '$mod.vP(1);',
  24954. '$mod.vP(1);',
  24955. '$mod.vP(2);',
  24956. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  24957. '$mod.b = $mod.vP(1) === 3;',
  24958. '$mod.b = 4 === $mod.vP(1);',
  24959. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  24960. '$mod.b = $mod.vP(1) !== 5;',
  24961. '$mod.b = 6 !== $mod.vP(1);',
  24962. '$mod.b = $mod.vP != null;',
  24963. '$mod.DoIt($mod.vP(1));',
  24964. '$mod.DoIt($mod.vP(1));',
  24965. '$mod.DoIt($mod.vP(2));',
  24966. '']));
  24967. end;
  24968. procedure TTestModule.TestProcType_ProcedureDelphi;
  24969. begin
  24970. StartProgram(false);
  24971. Add('{$mode Delphi}');
  24972. Add('type');
  24973. Add(' TProc = procedure;');
  24974. Add('procedure DoIt;');
  24975. Add('begin end;');
  24976. Add('var');
  24977. Add(' b: boolean;');
  24978. Add(' vP, vQ: tproc;');
  24979. Add('begin');
  24980. Add(' vp:=nil;');
  24981. Add(' vp:=vp;');
  24982. Add(' vp:=vq;');
  24983. 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
  24984. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24985. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  24986. Add(' vp;'); // ok in fpc and delphi
  24987. Add(' vp();');
  24988. // equal
  24989. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24990. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  24991. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24992. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  24993. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  24994. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24995. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24996. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  24997. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24998. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  24999. // unequal
  25000. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25001. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25002. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25003. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25004. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25005. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25006. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25007. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25008. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25009. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25010. Add(' b:=Assigned(vp);');
  25011. ConvertProgram;
  25012. CheckSource('TestProcType_ProcedureDelphi',
  25013. LinesToStr([ // statements
  25014. 'this.DoIt = function() {',
  25015. '};',
  25016. 'this.b = false;',
  25017. 'this.vP = null;',
  25018. 'this.vQ = null;'
  25019. ]),
  25020. LinesToStr([ // $mod.$main
  25021. '$mod.vP = null;',
  25022. '$mod.vP = $mod.vP;',
  25023. '$mod.vP = $mod.vQ;',
  25024. '$mod.vP = $mod.DoIt;',
  25025. '$mod.vP = $mod.DoIt;',
  25026. '$mod.vP();',
  25027. '$mod.vP();',
  25028. '$mod.b = $mod.vP === null;',
  25029. '$mod.b = null === $mod.vP;',
  25030. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25031. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25032. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25033. '$mod.b = $mod.vP !== null;',
  25034. '$mod.b = null !== $mod.vP;',
  25035. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25036. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25037. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25038. '$mod.b = $mod.vP != null;',
  25039. '']));
  25040. end;
  25041. procedure TTestModule.TestProcType_AsParam;
  25042. begin
  25043. StartProgram(false);
  25044. Add('type');
  25045. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25046. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25047. Add('var vJ: tfuncint;');
  25048. Add('begin');
  25049. Add(' vg:=vg;');
  25050. Add(' vj:=vh;');
  25051. Add(' vi:=vi;');
  25052. Add(' doit(vg,vg,vg);');
  25053. Add(' doit(vh,vh,vj);');
  25054. Add(' doit(vi,vi,vi);');
  25055. Add(' doit(vj,vj,vj);');
  25056. Add('end;');
  25057. Add('var i: tfuncint;');
  25058. Add('begin');
  25059. Add(' doit(i,i,i);');
  25060. ConvertProgram;
  25061. CheckSource('TestProcType_AsParam',
  25062. LinesToStr([ // statements
  25063. 'this.DoIt = function (vG,vH,vI) {',
  25064. ' var vJ = null;',
  25065. ' vG = vG;',
  25066. ' vJ = vH;',
  25067. ' vI.set(vI.get());',
  25068. ' $mod.DoIt(vG, vG, {',
  25069. ' get: function () {',
  25070. ' return vG;',
  25071. ' },',
  25072. ' set: function (v) {',
  25073. ' vG = v;',
  25074. ' }',
  25075. ' });',
  25076. ' $mod.DoIt(vH, vH, {',
  25077. ' get: function () {',
  25078. ' return vJ;',
  25079. ' },',
  25080. ' set: function (v) {',
  25081. ' vJ = v;',
  25082. ' }',
  25083. ' });',
  25084. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25085. ' $mod.DoIt(vJ, vJ, {',
  25086. ' get: function () {',
  25087. ' return vJ;',
  25088. ' },',
  25089. ' set: function (v) {',
  25090. ' vJ = v;',
  25091. ' }',
  25092. ' });',
  25093. '};',
  25094. 'this.i = null;'
  25095. ]),
  25096. LinesToStr([
  25097. '$mod.DoIt($mod.i,$mod.i,{',
  25098. ' p: $mod,',
  25099. ' get: function () {',
  25100. ' return this.p.i;',
  25101. ' },',
  25102. ' set: function (v) {',
  25103. ' this.p.i = v;',
  25104. ' }',
  25105. '});'
  25106. ]));
  25107. end;
  25108. procedure TTestModule.TestProcType_MethodFPC;
  25109. begin
  25110. StartProgram(false);
  25111. Add('type');
  25112. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25113. Add(' TObject = class');
  25114. Add(' function DoIt(vA: longint = 1): longint;');
  25115. Add(' end;');
  25116. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25117. Add('begin');
  25118. Add('end;');
  25119. Add('var');
  25120. Add(' Obj: TObject;');
  25121. Add(' vP: tfuncint;');
  25122. Add(' b: boolean;');
  25123. Add('begin');
  25124. Add(' vp:[email protected];'); // ok in fpc and delphi
  25125. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25126. Add(' vp;'); // ok in fpc and delphi
  25127. Add(' vp();');
  25128. Add(' vp(2);');
  25129. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25130. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25131. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25132. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25133. ConvertProgram;
  25134. CheckSource('TestProcType_MethodFPC',
  25135. LinesToStr([ // statements
  25136. 'rtl.createClass($mod, "TObject", null, function () {',
  25137. ' this.$init = function () {',
  25138. ' };',
  25139. ' this.$final = function () {',
  25140. ' };',
  25141. ' this.DoIt = function (vA) {',
  25142. ' var Result = 0;',
  25143. ' return Result;',
  25144. ' };',
  25145. '});',
  25146. 'this.Obj = null;',
  25147. 'this.vP = null;',
  25148. 'this.b = false;'
  25149. ]),
  25150. LinesToStr([
  25151. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25152. '$mod.vP(1);',
  25153. '$mod.vP(1);',
  25154. '$mod.vP(2);',
  25155. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25156. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25157. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25158. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25159. '']));
  25160. end;
  25161. procedure TTestModule.TestProcType_MethodDelphi;
  25162. begin
  25163. StartProgram(false);
  25164. Add([
  25165. '{$mode delphi}',
  25166. 'type',
  25167. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25168. ' TObject = class',
  25169. ' function DoIt(vA: longint = 1): longint;',
  25170. ' end;',
  25171. 'function TObject.DoIt(vA: longint = 1): longint;',
  25172. 'begin',
  25173. 'end;',
  25174. 'var',
  25175. ' Obj: TObject;',
  25176. ' vP: tfuncint;',
  25177. ' b: boolean;',
  25178. 'begin',
  25179. ' vp:[email protected];', // ok in fpc and delphi
  25180. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25181. ' vp;', // ok in fpc and delphi
  25182. ' vp();',
  25183. ' vp(2);',
  25184. //' b:[email protected];', // ok in fpc, illegal in delphi
  25185. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25186. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25187. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25188. '']);
  25189. ConvertProgram;
  25190. CheckSource('TestProcType_MethodDelphi',
  25191. LinesToStr([ // statements
  25192. 'rtl.createClass($mod, "TObject", null, function () {',
  25193. ' this.$init = function () {',
  25194. ' };',
  25195. ' this.$final = function () {',
  25196. ' };',
  25197. ' this.DoIt = function (vA) {',
  25198. ' var Result = 0;',
  25199. ' return Result;',
  25200. ' };',
  25201. '});',
  25202. 'this.Obj = null;',
  25203. 'this.vP = null;',
  25204. 'this.b = false;'
  25205. ]),
  25206. LinesToStr([
  25207. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25208. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25209. '$mod.vP(1);',
  25210. '$mod.vP(1);',
  25211. '$mod.vP(2);',
  25212. '']));
  25213. end;
  25214. procedure TTestModule.TestProcType_PropertyFPC;
  25215. begin
  25216. StartProgram(false);
  25217. Add('type');
  25218. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25219. Add(' TObject = class');
  25220. Add(' FOnFoo: TFuncInt;');
  25221. Add(' function DoIt(vA: longint = 1): longint;');
  25222. Add(' function GetFoo: TFuncInt;');
  25223. Add(' procedure SetFoo(const Value: TFuncInt);');
  25224. Add(' function GetEvents(Index: longint): TFuncInt;');
  25225. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25226. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25227. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25228. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25229. Add(' end;');
  25230. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25231. Add('function tobject.getfoo: tfuncint; begin end;');
  25232. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25233. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25234. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25235. Add('var');
  25236. Add(' Obj: TObject;');
  25237. Add(' vP: tfuncint;');
  25238. Add(' b: boolean;');
  25239. Add('begin');
  25240. Add(' obj.onfoo:=nil;');
  25241. Add(' obj.onbar:=nil;');
  25242. Add(' obj.events[1]:=nil;');
  25243. Add(' obj.onfoo:=obj.onfoo;');
  25244. Add(' obj.onbar:=obj.onbar;');
  25245. Add(' obj.events[2]:=obj.events[3];');
  25246. Add(' obj.onfoo:[email protected];');
  25247. Add(' obj.onbar:[email protected];');
  25248. Add(' obj.events[4]:[email protected];');
  25249. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25250. //Add(' obj.onbar:=obj.doit;'); // delphi
  25251. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25252. Add(' obj.onfoo;');
  25253. Add(' obj.onbar;');
  25254. //Add(' obj.events[5];'); ToDo in pasresolver
  25255. Add(' obj.onfoo();');
  25256. Add(' obj.onbar();');
  25257. Add(' obj.events[6]();');
  25258. Add(' b:=obj.onfoo=nil;');
  25259. Add(' b:=obj.onbar=nil;');
  25260. Add(' b:=obj.events[7]=nil;');
  25261. Add(' b:=obj.onfoo<>nil;');
  25262. Add(' b:=obj.onbar<>nil;');
  25263. Add(' b:=obj.events[8]<>nil;');
  25264. Add(' b:=obj.onfoo=vp;');
  25265. Add(' b:=obj.onbar=vp;');
  25266. Add(' b:=obj.events[9]=vp;');
  25267. Add(' b:=obj.onfoo=obj.onfoo;');
  25268. Add(' b:=obj.onbar=obj.onfoo;');
  25269. Add(' b:=obj.events[10]=obj.onfoo;');
  25270. Add(' b:=obj.onfoo<>obj.onfoo;');
  25271. Add(' b:=obj.onbar<>obj.onfoo;');
  25272. Add(' b:=obj.events[11]<>obj.onfoo;');
  25273. Add(' b:[email protected];');
  25274. Add(' b:[email protected];');
  25275. Add(' b:=obj.events[12][email protected];');
  25276. Add(' b:=obj.onfoo<>@obj.doit;');
  25277. Add(' b:=obj.onbar<>@obj.doit;');
  25278. Add(' b:=obj.events[12]<>@obj.doit;');
  25279. Add(' b:=Assigned(obj.onfoo);');
  25280. Add(' b:=Assigned(obj.onbar);');
  25281. Add(' b:=Assigned(obj.events[13]);');
  25282. ConvertProgram;
  25283. CheckSource('TestProcType_PropertyFPC',
  25284. LinesToStr([ // statements
  25285. 'rtl.createClass($mod, "TObject", null, function () {',
  25286. ' this.$init = function () {',
  25287. ' this.FOnFoo = null;',
  25288. ' };',
  25289. ' this.$final = function () {',
  25290. ' this.FOnFoo = undefined;',
  25291. ' };',
  25292. ' this.DoIt = function (vA) {',
  25293. ' var Result = 0;',
  25294. ' return Result;',
  25295. ' };',
  25296. 'this.GetFoo = function () {',
  25297. ' var Result = null;',
  25298. ' return Result;',
  25299. '};',
  25300. 'this.SetFoo = function (Value) {',
  25301. '};',
  25302. 'this.GetEvents = function (Index) {',
  25303. ' var Result = null;',
  25304. ' return Result;',
  25305. '};',
  25306. 'this.SetEvents = function (Index, Value) {',
  25307. '};',
  25308. '});',
  25309. 'this.Obj = null;',
  25310. 'this.vP = null;',
  25311. 'this.b = false;'
  25312. ]),
  25313. LinesToStr([
  25314. '$mod.Obj.FOnFoo = null;',
  25315. '$mod.Obj.SetFoo(null);',
  25316. '$mod.Obj.SetEvents(1, null);',
  25317. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25318. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25319. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25320. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25321. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25322. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25323. '$mod.Obj.FOnFoo(1);',
  25324. '$mod.Obj.GetFoo();',
  25325. '$mod.Obj.FOnFoo(1);',
  25326. '$mod.Obj.GetFoo()(1);',
  25327. '$mod.Obj.GetEvents(6)(1);',
  25328. '$mod.b = $mod.Obj.FOnFoo === null;',
  25329. '$mod.b = $mod.Obj.GetFoo() === null;',
  25330. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25331. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25332. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25333. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25334. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25335. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25336. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25337. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25338. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25339. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25340. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25341. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25342. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25343. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25344. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25345. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25346. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25347. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25348. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25349. '$mod.b = $mod.Obj.FOnFoo != null;',
  25350. '$mod.b = $mod.Obj.GetFoo() != null;',
  25351. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25352. '']));
  25353. end;
  25354. procedure TTestModule.TestProcType_PropertyDelphi;
  25355. begin
  25356. StartProgram(false);
  25357. Add('{$mode delphi}');
  25358. Add('type');
  25359. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25360. Add(' TObject = class');
  25361. Add(' FOnFoo: TFuncInt;');
  25362. Add(' function DoIt(vA: longint = 1): longint;');
  25363. Add(' function GetFoo: TFuncInt;');
  25364. Add(' procedure SetFoo(const Value: TFuncInt);');
  25365. Add(' function GetEvents(Index: longint): TFuncInt;');
  25366. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25367. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25368. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25369. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25370. Add(' end;');
  25371. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25372. Add('function tobject.getfoo: tfuncint; begin end;');
  25373. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25374. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25375. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25376. Add('var');
  25377. Add(' Obj: TObject;');
  25378. Add(' vP: tfuncint;');
  25379. Add(' b: boolean;');
  25380. Add('begin');
  25381. Add(' obj.onfoo:=nil;');
  25382. Add(' obj.onbar:=nil;');
  25383. Add(' obj.events[1]:=nil;');
  25384. Add(' obj.onfoo:=obj.onfoo;');
  25385. Add(' obj.onbar:=obj.onbar;');
  25386. Add(' obj.events[2]:=obj.events[3];');
  25387. Add(' obj.onfoo:[email protected];');
  25388. Add(' obj.onbar:[email protected];');
  25389. Add(' obj.events[4]:[email protected];');
  25390. Add(' obj.onfoo:=obj.doit;'); // delphi
  25391. Add(' obj.onbar:=obj.doit;'); // delphi
  25392. Add(' obj.events[4]:=obj.doit;'); // delphi
  25393. Add(' obj.onfoo;');
  25394. Add(' obj.onbar;');
  25395. //Add(' obj.events[5];'); ToDo in pasresolver
  25396. Add(' obj.onfoo();');
  25397. Add(' obj.onbar();');
  25398. Add(' obj.events[6]();');
  25399. //Add(' b:=obj.onfoo=nil;'); // fpc
  25400. //Add(' b:=obj.onbar=nil;'); // fpc
  25401. //Add(' b:=obj.events[7]=nil;'); // fpc
  25402. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25403. //Add(' b:=obj.onbar<>nil;'); // fpc
  25404. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25405. Add(' b:=obj.onfoo=vp;');
  25406. Add(' b:=obj.onbar=vp;');
  25407. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25408. Add(' b:=obj.onfoo=obj.onfoo;');
  25409. Add(' b:=obj.onbar=obj.onfoo;');
  25410. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25411. Add(' b:=obj.onfoo<>obj.onfoo;');
  25412. Add(' b:=obj.onbar<>obj.onfoo;');
  25413. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25414. //Add(' b:[email protected];'); // fpc
  25415. //Add(' b:[email protected];'); // fpc
  25416. //Add(' b:=obj.events[12][email protected];'); // fpc
  25417. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25418. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25419. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25420. Add(' b:=Assigned(obj.onfoo);');
  25421. Add(' b:=Assigned(obj.onbar);');
  25422. Add(' b:=Assigned(obj.events[13]);');
  25423. ConvertProgram;
  25424. CheckSource('TestProcType_PropertyDelphi',
  25425. LinesToStr([ // statements
  25426. 'rtl.createClass($mod, "TObject", null, function () {',
  25427. ' this.$init = function () {',
  25428. ' this.FOnFoo = null;',
  25429. ' };',
  25430. ' this.$final = function () {',
  25431. ' this.FOnFoo = undefined;',
  25432. ' };',
  25433. ' this.DoIt = function (vA) {',
  25434. ' var Result = 0;',
  25435. ' return Result;',
  25436. ' };',
  25437. 'this.GetFoo = function () {',
  25438. ' var Result = null;',
  25439. ' return Result;',
  25440. '};',
  25441. 'this.SetFoo = function (Value) {',
  25442. '};',
  25443. 'this.GetEvents = function (Index) {',
  25444. ' var Result = null;',
  25445. ' return Result;',
  25446. '};',
  25447. 'this.SetEvents = function (Index, Value) {',
  25448. '};',
  25449. '});',
  25450. 'this.Obj = null;',
  25451. 'this.vP = null;',
  25452. 'this.b = false;'
  25453. ]),
  25454. LinesToStr([
  25455. '$mod.Obj.FOnFoo = null;',
  25456. '$mod.Obj.SetFoo(null);',
  25457. '$mod.Obj.SetEvents(1, null);',
  25458. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25459. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25460. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25461. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25462. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25463. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25464. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25465. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25466. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25467. '$mod.Obj.FOnFoo(1);',
  25468. '$mod.Obj.GetFoo();',
  25469. '$mod.Obj.FOnFoo(1);',
  25470. '$mod.Obj.GetFoo()(1);',
  25471. '$mod.Obj.GetEvents(6)(1);',
  25472. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  25473. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  25474. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  25475. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  25476. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  25477. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  25478. '$mod.b = $mod.Obj.FOnFoo != null;',
  25479. '$mod.b = $mod.Obj.GetFoo() != null;',
  25480. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25481. '']));
  25482. end;
  25483. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  25484. begin
  25485. StartProgram(false);
  25486. Add('type');
  25487. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25488. Add(' TObject = class');
  25489. Add(' FOnFoo: TFuncInt;');
  25490. Add(' function DoIt(vA: longint = 1): longint;');
  25491. Add(' function GetFoo: TFuncInt;');
  25492. Add(' procedure SetFoo(const Value: TFuncInt);');
  25493. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25494. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25495. Add(' end;');
  25496. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25497. Add('function tobject.getfoo: tfuncint; begin end;');
  25498. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25499. Add('var');
  25500. Add(' Obj: TObject;');
  25501. Add(' vP: tfuncint;');
  25502. Add(' b: boolean;');
  25503. Add('begin');
  25504. Add('with obj do begin');
  25505. Add(' fonfoo:=nil;');
  25506. Add(' onfoo:=nil;');
  25507. Add(' onbar:=nil;');
  25508. Add(' fonfoo:=fonfoo;');
  25509. Add(' onfoo:=onfoo;');
  25510. Add(' onbar:=onbar;');
  25511. Add(' fonfoo:=@doit;');
  25512. Add(' onfoo:=@doit;');
  25513. Add(' onbar:=@doit;');
  25514. //Add(' fonfoo:=doit;'); // delphi
  25515. //Add(' onfoo:=doit;'); // delphi
  25516. //Add(' onbar:=doit;'); // delphi
  25517. Add(' fonfoo;');
  25518. Add(' onfoo;');
  25519. Add(' onbar;');
  25520. Add(' fonfoo();');
  25521. Add(' onfoo();');
  25522. Add(' onbar();');
  25523. Add(' b:=fonfoo=nil;');
  25524. Add(' b:=onfoo=nil;');
  25525. Add(' b:=onbar=nil;');
  25526. Add(' b:=fonfoo<>nil;');
  25527. Add(' b:=onfoo<>nil;');
  25528. Add(' b:=onbar<>nil;');
  25529. Add(' b:=fonfoo=vp;');
  25530. Add(' b:=onfoo=vp;');
  25531. Add(' b:=onbar=vp;');
  25532. Add(' b:=fonfoo=fonfoo;');
  25533. Add(' b:=onfoo=onfoo;');
  25534. Add(' b:=onbar=onfoo;');
  25535. Add(' b:=fonfoo<>fonfoo;');
  25536. Add(' b:=onfoo<>onfoo;');
  25537. Add(' b:=onbar<>onfoo;');
  25538. Add(' b:=fonfoo=@doit;');
  25539. Add(' b:=onfoo=@doit;');
  25540. Add(' b:=onbar=@doit;');
  25541. Add(' b:=fonfoo<>@doit;');
  25542. Add(' b:=onfoo<>@doit;');
  25543. Add(' b:=onbar<>@doit;');
  25544. Add(' b:=Assigned(fonfoo);');
  25545. Add(' b:=Assigned(onfoo);');
  25546. Add(' b:=Assigned(onbar);');
  25547. Add('end;');
  25548. ConvertProgram;
  25549. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  25550. LinesToStr([ // statements
  25551. 'rtl.createClass($mod, "TObject", null, function () {',
  25552. ' this.$init = function () {',
  25553. ' this.FOnFoo = null;',
  25554. ' };',
  25555. ' this.$final = function () {',
  25556. ' this.FOnFoo = undefined;',
  25557. ' };',
  25558. ' this.DoIt = function (vA) {',
  25559. ' var Result = 0;',
  25560. ' return Result;',
  25561. ' };',
  25562. ' this.GetFoo = function () {',
  25563. ' var Result = null;',
  25564. ' return Result;',
  25565. ' };',
  25566. ' this.SetFoo = function (Value) {',
  25567. ' };',
  25568. '});',
  25569. 'this.Obj = null;',
  25570. 'this.vP = null;',
  25571. 'this.b = false;'
  25572. ]),
  25573. LinesToStr([
  25574. 'var $with = $mod.Obj;',
  25575. '$with.FOnFoo = null;',
  25576. '$with.FOnFoo = null;',
  25577. '$with.SetFoo(null);',
  25578. '$with.FOnFoo = $with.FOnFoo;',
  25579. '$with.FOnFoo = $with.FOnFoo;',
  25580. '$with.SetFoo($with.GetFoo());',
  25581. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25582. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25583. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  25584. '$with.FOnFoo(1);',
  25585. '$with.FOnFoo(1);',
  25586. '$with.GetFoo();',
  25587. '$with.FOnFoo(1);',
  25588. '$with.FOnFoo(1);',
  25589. '$with.GetFoo()(1);',
  25590. '$mod.b = $with.FOnFoo === null;',
  25591. '$mod.b = $with.FOnFoo === null;',
  25592. '$mod.b = $with.GetFoo() === null;',
  25593. '$mod.b = $with.FOnFoo !== null;',
  25594. '$mod.b = $with.FOnFoo !== null;',
  25595. '$mod.b = $with.GetFoo() !== null;',
  25596. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25597. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25598. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  25599. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25600. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25601. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25602. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25603. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25604. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25605. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25606. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25607. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25608. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25609. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25610. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25611. '$mod.b = $with.FOnFoo != null;',
  25612. '$mod.b = $with.FOnFoo != null;',
  25613. '$mod.b = $with.GetFoo() != null;',
  25614. '']));
  25615. end;
  25616. procedure TTestModule.TestProcType_Nested;
  25617. begin
  25618. StartProgram(false);
  25619. Add([
  25620. 'type',
  25621. ' TProcInt = procedure(vI: longint = 1);',
  25622. 'procedure DoIt(vJ: longint);',
  25623. 'var aProc: TProcInt;',
  25624. ' b: boolean;',
  25625. ' procedure Sub(vK: longint);',
  25626. ' var aSub: TProcInt;',
  25627. ' procedure SubSub(vK: longint);',
  25628. ' var aSubSub: TProcInt;',
  25629. ' begin;',
  25630. ' aProc:=@DoIt;',
  25631. ' aSub:=@DoIt;',
  25632. ' aSubSub:=@DoIt;',
  25633. ' aProc:=@Sub;',
  25634. ' aSub:=@Sub;',
  25635. ' aSubSub:=@Sub;',
  25636. ' aProc:=@SubSub;',
  25637. ' aSub:=@SubSub;',
  25638. ' aSubSub:=@SubSub;',
  25639. ' end;',
  25640. ' begin;',
  25641. ' end;',
  25642. 'begin;',
  25643. ' aProc:=@Sub;',
  25644. ' b:=aProc=@Sub;',
  25645. ' b:=@Sub=aProc;',
  25646. 'end;',
  25647. 'begin',
  25648. '']);
  25649. ConvertProgram;
  25650. CheckSource('TestProcType_Nested',
  25651. LinesToStr([ // statements
  25652. 'this.DoIt = function (vJ) {',
  25653. ' var aProc = null;',
  25654. ' var b = false;',
  25655. ' function Sub(vK) {',
  25656. ' var aSub = null;',
  25657. ' function SubSub(vK) {',
  25658. ' var aSubSub = null;',
  25659. ' aProc = $mod.DoIt;',
  25660. ' aSub = $mod.DoIt;',
  25661. ' aSubSub = $mod.DoIt;',
  25662. ' aProc = Sub;',
  25663. ' aSub = Sub;',
  25664. ' aSubSub = Sub;',
  25665. ' aProc = SubSub;',
  25666. ' aSub = SubSub;',
  25667. ' aSubSub = SubSub;',
  25668. ' };',
  25669. ' };',
  25670. ' aProc = Sub;',
  25671. ' b = rtl.eqCallback(aProc, Sub);',
  25672. ' b = rtl.eqCallback(Sub, aProc);',
  25673. '};',
  25674. '']),
  25675. LinesToStr([ // $mod.$main
  25676. '']));
  25677. end;
  25678. procedure TTestModule.TestProcType_NestedOfObject;
  25679. begin
  25680. StartProgram(false);
  25681. Add([
  25682. 'type',
  25683. ' TProcInt = procedure(vI: longint = 1) of object;',
  25684. ' TObject = class',
  25685. ' procedure DoIt(vJ: longint);',
  25686. ' end;',
  25687. 'procedure TObject.DoIt(vJ: longint);',
  25688. 'var aProc: TProcInt;',
  25689. ' b: boolean;',
  25690. ' procedure Sub(vK: longint);',
  25691. ' var aSub: TProcInt;',
  25692. ' procedure SubSub(vK: longint);',
  25693. ' var aSubSub: TProcInt;',
  25694. ' begin;',
  25695. ' aProc:=@DoIt;',
  25696. ' aSub:=@DoIt;',
  25697. ' aSubSub:=@DoIt;',
  25698. ' aProc:=@Sub;',
  25699. ' aSub:=@Sub;',
  25700. ' aSubSub:=@Sub;',
  25701. ' aProc:=@SubSub;',
  25702. ' aSub:=@SubSub;',
  25703. ' aSubSub:=@SubSub;',
  25704. ' end;',
  25705. ' begin;',
  25706. ' end;',
  25707. 'begin;',
  25708. ' aProc:=@Sub;',
  25709. ' b:=aProc=@Sub;',
  25710. ' b:=@Sub=aProc;',
  25711. 'end;',
  25712. 'begin',
  25713. '']);
  25714. ConvertProgram;
  25715. CheckSource('TestProcType_Nested',
  25716. LinesToStr([ // statements
  25717. 'rtl.createClass($mod, "TObject", null, function () {',
  25718. ' this.$init = function () {',
  25719. ' };',
  25720. ' this.$final = function () {',
  25721. ' };',
  25722. ' this.DoIt = function (vJ) {',
  25723. ' var $Self = this;',
  25724. ' var aProc = null;',
  25725. ' var b = false;',
  25726. ' function Sub(vK) {',
  25727. ' var aSub = null;',
  25728. ' function SubSub(vK) {',
  25729. ' var aSubSub = null;',
  25730. ' aProc = rtl.createCallback($Self, "DoIt");',
  25731. ' aSub = rtl.createCallback($Self, "DoIt");',
  25732. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  25733. ' aProc = Sub;',
  25734. ' aSub = Sub;',
  25735. ' aSubSub = Sub;',
  25736. ' aProc = SubSub;',
  25737. ' aSub = SubSub;',
  25738. ' aSubSub = SubSub;',
  25739. ' };',
  25740. ' };',
  25741. ' aProc = Sub;',
  25742. ' b = rtl.eqCallback(aProc, Sub);',
  25743. ' b = rtl.eqCallback(Sub, aProc);',
  25744. ' };',
  25745. '});',
  25746. '']),
  25747. LinesToStr([ // $mod.$main
  25748. '']));
  25749. end;
  25750. procedure TTestModule.TestProcType_ReferenceToProc;
  25751. begin
  25752. StartProgram(false);
  25753. Add([
  25754. 'type',
  25755. ' TProcRef = reference to procedure(i: longint = 0);',
  25756. ' TFuncRef = reference to function(i: longint = 0): longint;',
  25757. 'var',
  25758. ' p: TProcRef;',
  25759. ' f: TFuncRef;',
  25760. 'procedure DoIt(i: longint);',
  25761. 'begin',
  25762. 'end;',
  25763. 'function GetIt(i: longint): longint;',
  25764. 'begin',
  25765. ' p:=@DoIt;',
  25766. ' f:=@GetIt;',
  25767. ' f;',
  25768. ' f();',
  25769. ' f(1);',
  25770. 'end;',
  25771. 'begin',
  25772. ' p:=@DoIt;',
  25773. ' f:=@GetIt;',
  25774. ' f;',
  25775. ' f();',
  25776. ' f(1);',
  25777. ' p:=TProcRef(f);',
  25778. '']);
  25779. ConvertProgram;
  25780. CheckSource('TestProcType_ReferenceToProc',
  25781. LinesToStr([ // statements
  25782. 'this.p = null;',
  25783. 'this.f = null;',
  25784. 'this.DoIt = function (i) {',
  25785. '};',
  25786. 'this.GetIt = function (i) {',
  25787. ' var Result = 0;',
  25788. ' $mod.p = $mod.DoIt;',
  25789. ' $mod.f = $mod.GetIt;',
  25790. ' $mod.f(0);',
  25791. ' $mod.f(0);',
  25792. ' $mod.f(1);',
  25793. ' return Result;',
  25794. '};',
  25795. '']),
  25796. LinesToStr([ // $mod.$main
  25797. '$mod.p = $mod.DoIt;',
  25798. '$mod.f = $mod.GetIt;',
  25799. '$mod.f(0);',
  25800. '$mod.f(0);',
  25801. '$mod.f(1);',
  25802. '$mod.p = $mod.f;',
  25803. '']));
  25804. end;
  25805. procedure TTestModule.TestProcType_ReferenceToMethod;
  25806. begin
  25807. StartProgram(false);
  25808. Add([
  25809. 'type',
  25810. ' TFuncRef = reference to function(i: longint = 5): longint;',
  25811. ' TObject = class',
  25812. ' function Grow(s: longint): longint;',
  25813. ' end;',
  25814. 'var',
  25815. ' f: tfuncref;',
  25816. 'function tobject.grow(s: longint): longint;',
  25817. ' function GrowSub(i: longint): longint;',
  25818. ' begin',
  25819. ' f:=@grow;',
  25820. ' f:=@growsub;',
  25821. ' end;',
  25822. 'begin',
  25823. ' f:=@grow;',
  25824. ' f:=@growsub;',
  25825. 'end;',
  25826. 'begin',
  25827. '']);
  25828. ConvertProgram;
  25829. CheckSource('TestProcType_ReferenceToMethod',
  25830. LinesToStr([ // statements
  25831. 'rtl.createClass($mod, "TObject", null, function () {',
  25832. ' this.$init = function () {',
  25833. ' };',
  25834. ' this.$final = function () {',
  25835. ' };',
  25836. ' this.Grow = function (s) {',
  25837. ' var $Self = this;',
  25838. ' var Result = 0;',
  25839. ' function GrowSub(i) {',
  25840. ' var Result = 0;',
  25841. ' $mod.f = rtl.createCallback($Self, "Grow");',
  25842. ' $mod.f = GrowSub;',
  25843. ' return Result;',
  25844. ' };',
  25845. ' $mod.f = rtl.createCallback($Self, "Grow");',
  25846. ' $mod.f = GrowSub;',
  25847. ' return Result;',
  25848. ' };',
  25849. '});',
  25850. 'this.f = null;',
  25851. '']),
  25852. LinesToStr([ // $mod.$main
  25853. '']));
  25854. end;
  25855. procedure TTestModule.TestProcType_Typecast;
  25856. begin
  25857. StartProgram(false);
  25858. Add([
  25859. 'type',
  25860. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  25861. ' TEvent = procedure of object;',
  25862. ' TGetter = function:longint of object;',
  25863. ' TProcA = procedure(i: longint);',
  25864. ' TFuncB = function(i, j: longint): longint;',
  25865. 'procedure DoIt(); varargs; begin end;',
  25866. 'var',
  25867. ' Notify: tnotifyevent;',
  25868. ' Event: tevent;',
  25869. ' Getter: tgetter;',
  25870. ' ProcA: tproca;',
  25871. ' FuncB: tfuncb;',
  25872. ' p: pointer;',
  25873. 'begin',
  25874. ' notify:=tnotifyevent(event);',
  25875. ' event:=tevent(event);',
  25876. ' event:=tevent(notify);',
  25877. ' event:=tevent(getter);',
  25878. ' event:=tevent(proca);',
  25879. ' proca:=tproca(funcb);',
  25880. ' funcb:=tfuncb(funcb);',
  25881. ' funcb:=tfuncb(proca);',
  25882. ' funcb:=tfuncb(getter);',
  25883. ' proca:=tproca(p);',
  25884. ' funcb:=tfuncb(p);',
  25885. ' getter:=tgetter(p);',
  25886. ' p:=pointer(notify);',
  25887. ' p:=notify;',
  25888. ' p:=pointer(proca);',
  25889. ' p:=proca;',
  25890. ' p:=pointer(funcb);',
  25891. ' p:=funcb;',
  25892. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  25893. '']);
  25894. ConvertProgram;
  25895. CheckSource('TestProcType_Typecast',
  25896. LinesToStr([ // statements
  25897. 'this.DoIt = function () {',
  25898. '};',
  25899. 'this.Notify = null;',
  25900. 'this.Event = null;',
  25901. 'this.Getter = null;',
  25902. 'this.ProcA = null;',
  25903. 'this.FuncB = null;',
  25904. 'this.p = null;',
  25905. '']),
  25906. LinesToStr([ // $mod.$main
  25907. '$mod.Notify = $mod.Event;',
  25908. '$mod.Event = $mod.Event;',
  25909. '$mod.Event = $mod.Notify;',
  25910. '$mod.Event = $mod.Getter;',
  25911. '$mod.Event = $mod.ProcA;',
  25912. '$mod.ProcA = $mod.FuncB;',
  25913. '$mod.FuncB = $mod.FuncB;',
  25914. '$mod.FuncB = $mod.ProcA;',
  25915. '$mod.FuncB = $mod.Getter;',
  25916. '$mod.ProcA = $mod.p;',
  25917. '$mod.FuncB = $mod.p;',
  25918. '$mod.Getter = $mod.p;',
  25919. '$mod.p = $mod.Notify;',
  25920. '$mod.p = $mod.Notify;',
  25921. '$mod.p = $mod.ProcA;',
  25922. '$mod.p = $mod.ProcA;',
  25923. '$mod.p = $mod.FuncB;',
  25924. '$mod.p = $mod.FuncB;',
  25925. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  25926. '']));
  25927. end;
  25928. procedure TTestModule.TestProcType_PassProcToUntyped;
  25929. begin
  25930. StartProgram(false);
  25931. Add([
  25932. 'type',
  25933. ' TEvent = procedure of object;',
  25934. ' TFunc = function: longint;',
  25935. 'procedure DoIt(); varargs; begin end;',
  25936. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  25937. 'var',
  25938. ' Event: tevent;',
  25939. ' Func: TFunc;',
  25940. 'begin',
  25941. ' doit(event,func);',
  25942. ' dosome(event,event,event);',
  25943. ' dosome(func,func,func);',
  25944. '']);
  25945. ConvertProgram;
  25946. CheckSource('TestProcType_PassProcToUntyped',
  25947. LinesToStr([ // statements
  25948. 'this.DoIt = function () {',
  25949. '};',
  25950. 'this.DoSome = function (a, b, p) {',
  25951. '};',
  25952. 'this.Event = null;',
  25953. 'this.Func = null;',
  25954. '']),
  25955. LinesToStr([ // $mod.$main
  25956. '$mod.DoIt($mod.Event, $mod.Func);',
  25957. '$mod.DoSome($mod.Event, {',
  25958. ' p: $mod,',
  25959. ' get: function () {',
  25960. ' return this.p.Event;',
  25961. ' },',
  25962. ' set: function (v) {',
  25963. ' this.p.Event = v;',
  25964. ' }',
  25965. '}, $mod.Event);',
  25966. '$mod.DoSome($mod.Func, {',
  25967. ' p: $mod,',
  25968. ' get: function () {',
  25969. ' return this.p.Func;',
  25970. ' },',
  25971. ' set: function (v) {',
  25972. ' this.p.Func = v;',
  25973. ' }',
  25974. '}, $mod.Func);',
  25975. '']));
  25976. end;
  25977. procedure TTestModule.TestProcType_PassProcToArray;
  25978. begin
  25979. StartProgram(false);
  25980. Add([
  25981. 'type',
  25982. ' TFunc = function: longint;',
  25983. ' TArrFunc = array of TFunc;',
  25984. 'procedure DoIt(Arr: TArrFunc); begin end;',
  25985. 'function GetIt: longint; begin end;',
  25986. 'var',
  25987. ' Func: tfunc;',
  25988. 'begin',
  25989. ' doit([]);',
  25990. ' doit([@GetIt]);',
  25991. ' doit([Func]);',
  25992. '']);
  25993. ConvertProgram;
  25994. CheckSource('TestProcType_PassProcToArray',
  25995. LinesToStr([ // statements
  25996. 'this.DoIt = function (Arr) {',
  25997. '};',
  25998. 'this.GetIt = function () {',
  25999. ' var Result = 0;',
  26000. ' return Result;',
  26001. '};',
  26002. 'this.Func = null;',
  26003. '']),
  26004. LinesToStr([ // $mod.$main
  26005. '$mod.DoIt([]);',
  26006. '$mod.DoIt([$mod.GetIt]);',
  26007. '$mod.DoIt([$mod.Func]);',
  26008. '']));
  26009. end;
  26010. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26011. begin
  26012. StartProgram(false);
  26013. Add([
  26014. '{$modeswitch externalclass}',
  26015. 'type',
  26016. ' TProc = reference to procedure(i: longint); safecall;',
  26017. ' TEvent = procedure(i: longint) of object; safecall;',
  26018. ' TExtA = class external name ''ExtObj''',
  26019. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26020. ' procedure DoSome(Id: longint = 1);',
  26021. ' procedure SetOnClick(const e: TEvent);',
  26022. ' property OnClick: TEvent write SetOnClick;',
  26023. ' class procedure Fly(Id: longint = 1); static;',
  26024. ' procedure SetOnShow(const p: TProc);',
  26025. ' property OnShow: TProc write SetOnShow;',
  26026. ' end;',
  26027. 'procedure Run(i: longint = 1);',
  26028. 'begin',
  26029. 'end;',
  26030. 'var',
  26031. ' Obj: texta;',
  26032. ' e: TEvent;',
  26033. ' p: TProc;',
  26034. 'begin',
  26035. ' e:=e;',
  26036. ' e:[email protected];',
  26037. ' e:[email protected];',
  26038. ' e:=TEvent(@obj.dosome);', // no safecall
  26039. ' obj.OnClick:[email protected];',
  26040. ' obj.OnClick:[email protected];',
  26041. ' obj.setonclick(@obj.doit);',
  26042. ' obj.setonclick(@obj.dosome);',
  26043. ' p:=@Run;',
  26044. ' p:[email protected];',
  26045. ' obj.OnShow:=@Run;',
  26046. ' obj.OnShow:[email protected];',
  26047. ' obj.setOnShow(@Run);',
  26048. ' obj.setOnShow(@TExtA.Fly);',
  26049. ' with obj do begin',
  26050. ' e:=@doit;',
  26051. ' e:=@dosome;',
  26052. ' OnClick:=@doit;',
  26053. ' OnClick:=@dosome;',
  26054. ' setonclick(@doit);',
  26055. ' setonclick(@dosome);',
  26056. ' OnShow:=@Run;',
  26057. ' setOnShow(@Run);',
  26058. ' end;']);
  26059. ConvertProgram;
  26060. CheckSource('TestProcType_SafeCallObjFPC',
  26061. LinesToStr([ // statements
  26062. 'this.Run = function (i) {',
  26063. '};',
  26064. 'this.Obj = null;',
  26065. 'this.e = null;',
  26066. 'this.p = null;',
  26067. '']),
  26068. LinesToStr([ // $mod.$main
  26069. '$mod.e = $mod.e;',
  26070. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26071. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26072. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26073. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26074. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26075. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26076. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26077. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26078. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26079. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26080. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26081. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26082. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26083. 'var $with = $mod.Obj;',
  26084. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26085. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26086. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26087. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26088. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26089. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26090. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26091. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26092. '']));
  26093. end;
  26094. procedure TTestModule.TestProcType_SafeCallDelphi;
  26095. begin
  26096. StartProgram(false);
  26097. Add([
  26098. '{$mode delphi}',
  26099. '{$modeswitch externalclass}',
  26100. 'type',
  26101. ' TProc = reference to procedure(i: longint); safecall;',
  26102. ' TEvent = procedure(i: longint) of object; safecall;',
  26103. ' TExtA = class external name ''ExtObj''',
  26104. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26105. ' procedure DoSome(Id: longint = 1);',
  26106. ' procedure SetOnClick(const e: TEvent);',
  26107. ' property OnClick: TEvent write SetOnClick;',
  26108. ' class procedure Fly(Id: longint = 1); static;',
  26109. ' procedure SetOnShow(const p: TProc);',
  26110. ' property OnShow: TProc write SetOnShow;',
  26111. ' end;',
  26112. 'procedure Run(i: longint = 1);',
  26113. 'begin',
  26114. 'end;',
  26115. 'var',
  26116. ' Obj: texta;',
  26117. ' e: TEvent;',
  26118. ' p: TProc;',
  26119. 'begin',
  26120. ' e:=e;',
  26121. ' e:=obj.doit;',
  26122. ' e:=obj.dosome;',
  26123. ' e:=TEvent(@obj.dosome);', // no safecall
  26124. ' obj.OnClick:=obj.doit;',
  26125. ' obj.OnClick:=obj.dosome;',
  26126. ' obj.setonclick(obj.doit);',
  26127. ' obj.setonclick(obj.dosome);',
  26128. ' p:=Run;',
  26129. ' p:=TExtA.Fly;',
  26130. ' obj.OnShow:=Run;',
  26131. ' obj.OnShow:=TExtA.Fly;',
  26132. ' obj.setOnShow(Run);',
  26133. ' obj.setOnShow(TExtA.Fly);',
  26134. ' with obj do begin',
  26135. ' e:=doit;',
  26136. ' e:=dosome;',
  26137. ' OnClick:=doit;',
  26138. ' OnClick:=dosome;',
  26139. ' setonclick(doit);',
  26140. ' setonclick(dosome);',
  26141. ' OnShow:=@Run;',
  26142. ' setOnShow(@Run);',
  26143. ' end;']);
  26144. ConvertProgram;
  26145. CheckSource('TestProcType_SafeCallDelphi',
  26146. LinesToStr([ // statements
  26147. 'this.Run = function (i) {',
  26148. '};',
  26149. 'this.Obj = null;',
  26150. 'this.e = null;',
  26151. 'this.p = null;',
  26152. '']),
  26153. LinesToStr([ // $mod.$main
  26154. '$mod.e = $mod.e;',
  26155. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26156. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26157. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26158. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26159. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26160. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26161. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26162. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26163. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26164. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26165. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26166. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26167. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26168. 'var $with = $mod.Obj;',
  26169. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26170. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26171. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26172. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26173. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26174. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26175. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26176. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26177. '']));
  26178. end;
  26179. procedure TTestModule.TestPointer;
  26180. begin
  26181. StartProgram(false);
  26182. Add(['type',
  26183. ' TObject = class end;',
  26184. ' TClass = class of TObject;',
  26185. ' TArrInt = array of longint;',
  26186. 'const',
  26187. ' n = nil;',
  26188. 'var',
  26189. ' v: jsvalue;',
  26190. ' Obj: tobject;',
  26191. ' C: tclass;',
  26192. ' a: tarrint;',
  26193. ' p: Pointer = nil;',
  26194. ' s: string;',
  26195. 'begin',
  26196. ' p:=p;',
  26197. ' p:=nil;',
  26198. ' if p=nil then;',
  26199. ' if nil=p then;',
  26200. ' if Assigned(p) then;',
  26201. ' p:=Pointer(v);',
  26202. ' p:=obj;',
  26203. ' p:=c;',
  26204. ' p:=a;',
  26205. ' p:=tobject;',
  26206. ' obj:=TObject(p);',
  26207. ' c:=TClass(p);',
  26208. ' a:=TArrInt(p);',
  26209. ' p:=n;',
  26210. ' p:=Pointer(a);',
  26211. ' p:=pointer(s);',
  26212. ' s:=string(p);',
  26213. '']);
  26214. ConvertProgram;
  26215. CheckSource('TestPointer',
  26216. LinesToStr([ // statements
  26217. 'rtl.createClass($mod, "TObject", null, function () {',
  26218. ' this.$init = function () {',
  26219. ' };',
  26220. ' this.$final = function () {',
  26221. ' };',
  26222. '});',
  26223. 'this.n = null;',
  26224. 'this.v = undefined;',
  26225. 'this.Obj = null;',
  26226. 'this.C = null;',
  26227. 'this.a = [];',
  26228. 'this.p = null;',
  26229. 'this.s = "";',
  26230. '']),
  26231. LinesToStr([ // $mod.$main
  26232. '$mod.p = $mod.p;',
  26233. '$mod.p = null;',
  26234. 'if ($mod.p === null) ;',
  26235. 'if (null === $mod.p) ;',
  26236. 'if ($mod.p != null) ;',
  26237. '$mod.p = $mod.v;',
  26238. '$mod.p = $mod.Obj;',
  26239. '$mod.p = $mod.C;',
  26240. '$mod.p = $mod.a;',
  26241. '$mod.p = $mod.TObject;',
  26242. '$mod.Obj = $mod.p;',
  26243. '$mod.C = $mod.p;',
  26244. '$mod.a = $mod.p;',
  26245. '$mod.p = null;',
  26246. '$mod.p = $mod.a;',
  26247. '$mod.p = $mod.s;',
  26248. '$mod.s = $mod.p;',
  26249. '']));
  26250. end;
  26251. procedure TTestModule.TestPointer_Proc;
  26252. begin
  26253. StartProgram(false);
  26254. Add('type');
  26255. Add(' TObject = class');
  26256. Add(' procedure DoIt; virtual; abstract;');
  26257. Add(' end;');
  26258. Add('procedure DoSome; begin end;');
  26259. Add('var');
  26260. Add(' o: TObject;');
  26261. Add(' p: Pointer;');
  26262. Add('begin');
  26263. Add(' p:=@DoSome;');
  26264. Add(' p:[email protected];');
  26265. ConvertProgram;
  26266. CheckSource('TestPointer_Proc',
  26267. LinesToStr([ // statements
  26268. 'rtl.createClass($mod, "TObject", null, function () {',
  26269. ' this.$init = function () {',
  26270. ' };',
  26271. ' this.$final = function () {',
  26272. ' };',
  26273. '});',
  26274. 'this.DoSome = function () {',
  26275. '};',
  26276. 'this.o = null;',
  26277. 'this.p = null;',
  26278. '']),
  26279. LinesToStr([ // $mod.$main
  26280. '$mod.p = $mod.DoSome;',
  26281. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26282. '']));
  26283. end;
  26284. procedure TTestModule.TestPointer_AssignRecordFail;
  26285. begin
  26286. StartProgram(false);
  26287. Add('type');
  26288. Add(' TRec = record end;');
  26289. Add('var');
  26290. Add(' p: Pointer;');
  26291. Add(' r: TRec;');
  26292. Add('begin');
  26293. Add(' p:=r;');
  26294. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26295. nIncompatibleTypesGotExpected);
  26296. ConvertProgram;
  26297. end;
  26298. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26299. begin
  26300. StartProgram(false);
  26301. Add('type');
  26302. Add(' TArr = array[boolean] of longint;');
  26303. Add('var');
  26304. Add(' p: Pointer;');
  26305. Add(' a: TArr;');
  26306. Add('begin');
  26307. Add(' p:=a;');
  26308. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26309. nIncompatibleTypesGotExpected);
  26310. ConvertProgram;
  26311. end;
  26312. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26313. begin
  26314. StartProgram(false);
  26315. Add([
  26316. 'procedure DoIt(args: array of jsvalue); begin end;',
  26317. 'procedure DoAll; varargs; begin end;',
  26318. 'var',
  26319. ' v: jsvalue;',
  26320. 'begin',
  26321. ' DoIt([pointer(v)]);',
  26322. ' DoAll(pointer(v));',
  26323. '']);
  26324. ConvertProgram;
  26325. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26326. LinesToStr([ // statements
  26327. 'this.DoIt = function (args) {',
  26328. '};',
  26329. 'this.DoAll = function () {',
  26330. '};',
  26331. 'this.v = undefined;',
  26332. '']),
  26333. LinesToStr([ // $mod.$main
  26334. '$mod.DoIt([$mod.v]);',
  26335. '$mod.DoAll($mod.v);',
  26336. '']));
  26337. end;
  26338. procedure TTestModule.TestPointer_NonRecordFail;
  26339. begin
  26340. StartProgram(false);
  26341. Add([
  26342. 'type',
  26343. ' p = ^longint;',
  26344. 'begin',
  26345. '']);
  26346. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26347. ConvertProgram;
  26348. end;
  26349. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26350. begin
  26351. StartProgram(false);
  26352. Add([
  26353. 'procedure DoIt(p: ^longint); begin end;',
  26354. 'begin',
  26355. '']);
  26356. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26357. ConvertProgram;
  26358. end;
  26359. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26360. begin
  26361. StartProgram(false);
  26362. Add([
  26363. 'var p: ^longint;',
  26364. 'begin',
  26365. '']);
  26366. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26367. ConvertProgram;
  26368. end;
  26369. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26370. begin
  26371. StartProgram(false);
  26372. Add([
  26373. 'function DoIt: ^longint; begin end;',
  26374. 'begin',
  26375. '']);
  26376. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26377. ConvertProgram;
  26378. end;
  26379. procedure TTestModule.TestPointer_AddrOperatorFail;
  26380. begin
  26381. StartProgram(false);
  26382. Add([
  26383. 'var i: longint;',
  26384. 'begin',
  26385. ' if @i=nil then ;',
  26386. '']);
  26387. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26388. ConvertProgram;
  26389. end;
  26390. procedure TTestModule.TestPointer_ArrayParamsFail;
  26391. begin
  26392. StartProgram(false);
  26393. Add([
  26394. 'var',
  26395. ' p: Pointer;',
  26396. 'begin',
  26397. ' p:=p[1];',
  26398. '']);
  26399. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26400. ConvertProgram;
  26401. end;
  26402. procedure TTestModule.TestPointer_PointerAddFail;
  26403. begin
  26404. StartProgram(false);
  26405. Add([
  26406. 'var',
  26407. ' p: Pointer;',
  26408. 'begin',
  26409. ' p:=p+1;',
  26410. '']);
  26411. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26412. ConvertProgram;
  26413. end;
  26414. procedure TTestModule.TestPointer_IncPointerFail;
  26415. begin
  26416. StartProgram(false);
  26417. Add([
  26418. 'var',
  26419. ' p: Pointer;',
  26420. 'begin',
  26421. ' inc(p,1);',
  26422. '']);
  26423. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26424. nIncompatibleTypeArgNo);
  26425. ConvertProgram;
  26426. end;
  26427. procedure TTestModule.TestPointer_Record;
  26428. begin
  26429. StartProgram(false);
  26430. Add([
  26431. 'type',
  26432. ' TRec = record x: longint; end;',
  26433. ' PRec = ^TRec;',
  26434. 'var',
  26435. ' r: TRec;',
  26436. ' p: PRec;',
  26437. ' q: ^TRec;',
  26438. ' Ptr: pointer;',
  26439. 'begin',
  26440. ' new(p);',
  26441. ' p:=@r;',
  26442. ' r:=p^;',
  26443. ' r.x:=p^.x;',
  26444. ' p^.x:=r.x;',
  26445. ' if p^.x=3 then ;',
  26446. ' if 4=p^.x then ;',
  26447. ' dispose(p);',
  26448. ' new(q);',
  26449. ' dispose(q);',
  26450. ' Ptr:=p;',
  26451. ' p:=PRec(ptr);',
  26452. '']);
  26453. ConvertProgram;
  26454. CheckSource('TestPointer_Record',
  26455. LinesToStr([ // statements
  26456. 'rtl.recNewT($mod, "TRec", function () {',
  26457. ' this.x = 0;',
  26458. ' this.$eq = function (b) {',
  26459. ' return this.x === b.x;',
  26460. ' };',
  26461. ' this.$assign = function (s) {',
  26462. ' this.x = s.x;',
  26463. ' return this;',
  26464. ' };',
  26465. '});',
  26466. 'this.r = $mod.TRec.$new();',
  26467. 'this.p = null;',
  26468. 'this.q = null;',
  26469. 'this.Ptr = null;',
  26470. '']),
  26471. LinesToStr([ // $mod.$main
  26472. '$mod.p = $mod.TRec.$new();',
  26473. '$mod.p = $mod.r;',
  26474. '$mod.r.$assign($mod.p);',
  26475. '$mod.r.x = $mod.p.x;',
  26476. '$mod.p.x = $mod.r.x;',
  26477. 'if ($mod.p.x === 3) ;',
  26478. 'if (4 === $mod.p.x) ;',
  26479. '$mod.p = null;',
  26480. '$mod.q = $mod.TRec.$new();',
  26481. '$mod.q = null;',
  26482. '$mod.Ptr = $mod.p;',
  26483. '$mod.p = $mod.Ptr;',
  26484. '']));
  26485. end;
  26486. procedure TTestModule.TestPointer_RecordArg;
  26487. begin
  26488. StartProgram(false);
  26489. Add([
  26490. '{$modeswitch autoderef}',
  26491. 'type',
  26492. ' TRec = record x: longint; end;',
  26493. ' PRec = ^TRec;',
  26494. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  26495. 'begin',
  26496. ' a.x:=a.x;',
  26497. ' a^.x:=a^.x;',
  26498. ' with a^ do',
  26499. ' x:=x;',
  26500. 'end;',
  26501. 'function GetIt(p: PRec): PRec;',
  26502. 'begin',
  26503. ' p.x:=p.x;',
  26504. ' p^.x:=p^.x;',
  26505. ' with p^ do',
  26506. ' x:=x;',
  26507. 'end;',
  26508. 'var',
  26509. ' r: TRec;',
  26510. ' p: PRec;',
  26511. 'begin',
  26512. ' p:=GetIt(p);',
  26513. ' p^:=GetIt(@r)^;',
  26514. ' DoIt(p,p,p);',
  26515. ' DoIt(@r,p,p);',
  26516. '']);
  26517. ConvertProgram;
  26518. CheckSource('TestPointer_Record',
  26519. LinesToStr([ // statements
  26520. 'rtl.recNewT($mod, "TRec", function () {',
  26521. ' this.x = 0;',
  26522. ' this.$eq = function (b) {',
  26523. ' return this.x === b.x;',
  26524. ' };',
  26525. ' this.$assign = function (s) {',
  26526. ' this.x = s.x;',
  26527. ' return this;',
  26528. ' };',
  26529. '});',
  26530. 'this.DoIt = function (a, b, c) {',
  26531. ' var Result = $mod.TRec.$new();',
  26532. ' a.x = a.x;',
  26533. ' a.x = a.x;',
  26534. ' a.x = a.x;',
  26535. ' return Result;',
  26536. '};',
  26537. 'this.GetIt = function (p) {',
  26538. ' var Result = null;',
  26539. ' p.x = p.x;',
  26540. ' p.x = p.x;',
  26541. ' p.x = p.x;',
  26542. ' return Result;',
  26543. '};',
  26544. 'this.r = $mod.TRec.$new();',
  26545. 'this.p = null;',
  26546. '']),
  26547. LinesToStr([ // $mod.$main
  26548. '$mod.p = $mod.GetIt($mod.p);',
  26549. '$mod.p.$assign($mod.GetIt($mod.r));',
  26550. '$mod.DoIt($mod.p, {',
  26551. ' p: $mod,',
  26552. ' get: function () {',
  26553. ' return this.p.p;',
  26554. ' },',
  26555. ' set: function (v) {',
  26556. ' this.p.p = v;',
  26557. ' }',
  26558. '}, {',
  26559. ' p: $mod,',
  26560. ' get: function () {',
  26561. ' return this.p.p;',
  26562. ' },',
  26563. ' set: function (v) {',
  26564. ' this.p.p = v;',
  26565. ' }',
  26566. '});',
  26567. '$mod.DoIt($mod.r, {',
  26568. ' p: $mod,',
  26569. ' get: function () {',
  26570. ' return this.p.p;',
  26571. ' },',
  26572. ' set: function (v) {',
  26573. ' this.p.p = v;',
  26574. ' }',
  26575. '}, {',
  26576. ' p: $mod,',
  26577. ' get: function () {',
  26578. ' return this.p.p;',
  26579. ' },',
  26580. ' set: function (v) {',
  26581. ' this.p.p = v;',
  26582. ' }',
  26583. '});',
  26584. '']));
  26585. end;
  26586. procedure TTestModule.TestJSValue_AssignToJSValue;
  26587. begin
  26588. StartProgram(false);
  26589. Add('var');
  26590. Add(' v: jsvalue;');
  26591. Add(' i: longint;');
  26592. Add(' s: string;');
  26593. Add(' b: boolean;');
  26594. Add(' d: double;');
  26595. Add(' p: pointer;');
  26596. Add('begin');
  26597. Add(' v:=v;');
  26598. Add(' v:=1;');
  26599. Add(' v:=i;');
  26600. Add(' v:='''';');
  26601. Add(' v:=''c'';');
  26602. Add(' v:=''foo'';');
  26603. Add(' v:=s;');
  26604. Add(' v:=false;');
  26605. Add(' v:=true;');
  26606. Add(' v:=b;');
  26607. Add(' v:=0.1;');
  26608. Add(' v:=d;');
  26609. Add(' v:=nil;');
  26610. Add(' v:=p;');
  26611. ConvertProgram;
  26612. CheckSource('TestJSValue_AssignToJSValue',
  26613. LinesToStr([ // statements
  26614. 'this.v = undefined;',
  26615. 'this.i = 0;',
  26616. 'this.s = "";',
  26617. 'this.b = false;',
  26618. 'this.d = 0.0;',
  26619. 'this.p = null;',
  26620. '']),
  26621. LinesToStr([ // $mod.$main
  26622. '$mod.v = $mod.v;',
  26623. '$mod.v = 1;',
  26624. '$mod.v = $mod.i;',
  26625. '$mod.v = "";',
  26626. '$mod.v = "c";',
  26627. '$mod.v = "foo";',
  26628. '$mod.v = $mod.s;',
  26629. '$mod.v = false;',
  26630. '$mod.v = true;',
  26631. '$mod.v = $mod.b;',
  26632. '$mod.v = 0.1;',
  26633. '$mod.v = $mod.d;',
  26634. '$mod.v = null;',
  26635. '$mod.v = $mod.p;',
  26636. '']));
  26637. end;
  26638. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  26639. begin
  26640. StartProgram(false);
  26641. Add('type');
  26642. Add(' integer = longint;');
  26643. Add(' TYesNo = boolean;');
  26644. Add(' TFloat = double;');
  26645. Add(' TCaption = string;');
  26646. Add(' TChar = char;');
  26647. Add('var');
  26648. Add(' v: jsvalue;');
  26649. Add(' i: integer;');
  26650. Add(' s: TCaption;');
  26651. Add(' b: TYesNo;');
  26652. Add(' d: TFloat;');
  26653. Add(' c: char;');
  26654. Add('begin');
  26655. Add(' i:=longint(v);');
  26656. Add(' i:=integer(v);');
  26657. Add(' s:=string(v);');
  26658. Add(' s:=TCaption(v);');
  26659. Add(' b:=boolean(v);');
  26660. Add(' b:=TYesNo(v);');
  26661. Add(' d:=double(v);');
  26662. Add(' d:=TFloat(v);');
  26663. Add(' c:=char(v);');
  26664. Add(' c:=TChar(v);');
  26665. ConvertProgram;
  26666. CheckSource('TestJSValue_TypeCastToBaseType',
  26667. LinesToStr([ // statements
  26668. 'this.v = undefined;',
  26669. 'this.i = 0;',
  26670. 'this.s = "";',
  26671. 'this.b = false;',
  26672. 'this.d = 0.0;',
  26673. 'this.c = "";',
  26674. '']),
  26675. LinesToStr([ // $mod.$main
  26676. '$mod.i = Math.floor($mod.v);',
  26677. '$mod.i = Math.floor($mod.v);',
  26678. '$mod.s = "" + $mod.v;',
  26679. '$mod.s = "" + $mod.v;',
  26680. '$mod.b = !($mod.v == false);',
  26681. '$mod.b = !($mod.v == false);',
  26682. '$mod.d = rtl.getNumber($mod.v);',
  26683. '$mod.d = rtl.getNumber($mod.v);',
  26684. '$mod.c = rtl.getChar($mod.v);',
  26685. '$mod.c = rtl.getChar($mod.v);',
  26686. '']));
  26687. end;
  26688. procedure TTestModule.TestJSValue_TypecastToJSValue;
  26689. begin
  26690. StartProgram(false);
  26691. Add([
  26692. 'type',
  26693. ' TArr = array of word;',
  26694. ' TRec = record end;',
  26695. ' TSet = set of boolean;',
  26696. 'procedure Fly(v: jsvalue);',
  26697. 'begin',
  26698. 'end;',
  26699. 'var',
  26700. ' a: TArr;',
  26701. ' r: TRec;',
  26702. ' s: TSet;',
  26703. 'begin',
  26704. ' Fly(jsvalue(a));',
  26705. ' Fly(jsvalue(r));',
  26706. ' Fly(jsvalue(s));',
  26707. '']);
  26708. ConvertProgram;
  26709. CheckSource('TestJSValue_TypecastToJSValue',
  26710. LinesToStr([ // statements
  26711. 'rtl.recNewT($mod, "TRec", function () {',
  26712. ' this.$eq = function (b) {',
  26713. ' return true;',
  26714. ' };',
  26715. ' this.$assign = function (s) {',
  26716. ' return this;',
  26717. ' };',
  26718. '});',
  26719. 'this.Fly = function (v) {',
  26720. '};',
  26721. 'this.a = [];',
  26722. 'this.r = $mod.TRec.$new();',
  26723. 'this.s = {};',
  26724. '']),
  26725. LinesToStr([ // $mod.$main
  26726. '$mod.Fly($mod.a);',
  26727. '$mod.Fly($mod.r);',
  26728. '$mod.Fly($mod.s);',
  26729. '']));
  26730. end;
  26731. procedure TTestModule.TestJSValue_Equal;
  26732. begin
  26733. StartProgram(false);
  26734. Add('type');
  26735. Add(' integer = longint;');
  26736. Add(' TYesNo = boolean;');
  26737. Add(' TFloat = double;');
  26738. Add(' TCaption = string;');
  26739. Add(' TChar = char;');
  26740. Add(' TMulti = JSValue;');
  26741. Add('var');
  26742. Add(' v: jsvalue;');
  26743. Add(' i: integer;');
  26744. Add(' s: TCaption;');
  26745. Add(' b: TYesNo;');
  26746. Add(' d: TFloat;');
  26747. Add(' c: char;');
  26748. Add(' m: TMulti;');
  26749. Add('begin');
  26750. Add(' b:=v=v;');
  26751. Add(' b:=v<>v;');
  26752. Add(' b:=v=1;');
  26753. Add(' b:=v<>1;');
  26754. Add(' b:=2=v;');
  26755. Add(' b:=2<>v;');
  26756. Add(' b:=v=i;');
  26757. Add(' b:=i=v;');
  26758. Add(' b:=v=nil;');
  26759. Add(' b:=nil=v;');
  26760. Add(' b:=v=false;');
  26761. Add(' b:=true=v;');
  26762. Add(' b:=v=b;');
  26763. Add(' b:=b=v;');
  26764. Add(' b:=v=s;');
  26765. Add(' b:=s=v;');
  26766. Add(' b:=v=''foo'';');
  26767. Add(' b:=''''=v;');
  26768. Add(' b:=v=d;');
  26769. Add(' b:=d=v;');
  26770. Add(' b:=v=3.4;');
  26771. Add(' b:=5.6=v;');
  26772. Add(' b:=v=c;');
  26773. Add(' b:=c=v;');
  26774. Add(' b:=m=m;');
  26775. Add(' b:=v=m;');
  26776. Add(' b:=m=v;');
  26777. ConvertProgram;
  26778. CheckSource('TestJSValue_Equal',
  26779. LinesToStr([ // statements
  26780. 'this.v = undefined;',
  26781. 'this.i = 0;',
  26782. 'this.s = "";',
  26783. 'this.b = false;',
  26784. 'this.d = 0.0;',
  26785. 'this.c = "";',
  26786. 'this.m = undefined;',
  26787. '']),
  26788. LinesToStr([ // $mod.$main
  26789. '$mod.b = $mod.v == $mod.v;',
  26790. '$mod.b = $mod.v != $mod.v;',
  26791. '$mod.b = $mod.v == 1;',
  26792. '$mod.b = $mod.v != 1;',
  26793. '$mod.b = 2 == $mod.v;',
  26794. '$mod.b = 2 != $mod.v;',
  26795. '$mod.b = $mod.v == $mod.i;',
  26796. '$mod.b = $mod.i == $mod.v;',
  26797. '$mod.b = $mod.v == null;',
  26798. '$mod.b = null == $mod.v;',
  26799. '$mod.b = $mod.v == false;',
  26800. '$mod.b = true == $mod.v;',
  26801. '$mod.b = $mod.v == $mod.b;',
  26802. '$mod.b = $mod.b == $mod.v;',
  26803. '$mod.b = $mod.v == $mod.s;',
  26804. '$mod.b = $mod.s == $mod.v;',
  26805. '$mod.b = $mod.v == "foo";',
  26806. '$mod.b = "" == $mod.v;',
  26807. '$mod.b = $mod.v == $mod.d;',
  26808. '$mod.b = $mod.d == $mod.v;',
  26809. '$mod.b = $mod.v == 3.4;',
  26810. '$mod.b = 5.6 == $mod.v;',
  26811. '$mod.b = $mod.v == $mod.c;',
  26812. '$mod.b = $mod.c == $mod.v;',
  26813. '$mod.b = $mod.m == $mod.m;',
  26814. '$mod.b = $mod.v == $mod.m;',
  26815. '$mod.b = $mod.m == $mod.v;',
  26816. '']));
  26817. end;
  26818. procedure TTestModule.TestJSValue_If;
  26819. begin
  26820. StartProgram(false);
  26821. Add([
  26822. 'procedure Fly(var u);',
  26823. 'begin',
  26824. ' if jsvalue(u) then ;',
  26825. 'end;',
  26826. 'var',
  26827. ' v: jsvalue;',
  26828. 'begin',
  26829. ' if v then ;',
  26830. ' while v do ;',
  26831. ' repeat until v;',
  26832. '']);
  26833. ConvertProgram;
  26834. CheckSource('TestJSValue_If',
  26835. LinesToStr([ // statements
  26836. 'this.Fly = function (u) {',
  26837. ' if (u.get()) ;',
  26838. '};',
  26839. 'this.v = undefined;',
  26840. '']),
  26841. LinesToStr([ // $mod.$main
  26842. 'if ($mod.v) ;',
  26843. 'while($mod.v){',
  26844. '};',
  26845. 'do{',
  26846. '} while(!$mod.v);',
  26847. '']));
  26848. end;
  26849. procedure TTestModule.TestJSValue_Not;
  26850. begin
  26851. StartProgram(false);
  26852. Add([
  26853. 'var',
  26854. ' v: jsvalue;',
  26855. ' b: boolean;',
  26856. 'begin',
  26857. ' b:=not v;',
  26858. ' if not v then ;',
  26859. ' while not v do ;',
  26860. ' repeat until not v;',
  26861. '']);
  26862. ConvertProgram;
  26863. CheckSource('TestJSValue_If',
  26864. LinesToStr([ // statements
  26865. 'this.v = undefined;',
  26866. 'this.b = false;',
  26867. '']),
  26868. LinesToStr([ // $mod.$main
  26869. '$mod.b=!$mod.v;',
  26870. 'if (!$mod.v) ;',
  26871. 'while(!$mod.v){',
  26872. '};',
  26873. 'do{',
  26874. '} while($mod.v);',
  26875. '']));
  26876. end;
  26877. procedure TTestModule.TestJSValue_Enum;
  26878. begin
  26879. StartProgram(false);
  26880. Add('type');
  26881. Add(' TColor = (red, blue);');
  26882. Add(' TRedBlue = TColor;');
  26883. Add('var');
  26884. Add(' v: jsvalue;');
  26885. Add(' e: TColor;');
  26886. Add('begin');
  26887. Add(' v:=e;');
  26888. Add(' v:=TColor(e);');
  26889. Add(' v:=TRedBlue(e);');
  26890. Add(' e:=TColor(v);');
  26891. Add(' e:=TRedBlue(v);');
  26892. ConvertProgram;
  26893. CheckSource('TestJSValue_Enum',
  26894. LinesToStr([ // statements
  26895. 'this.TColor = {',
  26896. ' "0": "red",',
  26897. ' red: 0,',
  26898. ' "1": "blue",',
  26899. ' blue: 1',
  26900. '};',
  26901. 'this.v = undefined;',
  26902. 'this.e = 0;',
  26903. '']),
  26904. LinesToStr([ // $mod.$main
  26905. '$mod.v = $mod.e;',
  26906. '$mod.v = $mod.e;',
  26907. '$mod.v = $mod.e;',
  26908. '$mod.e = $mod.v;',
  26909. '$mod.e = $mod.v;',
  26910. '']));
  26911. end;
  26912. procedure TTestModule.TestJSValue_ClassInstance;
  26913. begin
  26914. StartProgram(false);
  26915. Add([
  26916. 'type',
  26917. ' TObject = class',
  26918. ' end;',
  26919. ' TBirdObject = TObject;',
  26920. 'var',
  26921. ' v: jsvalue;',
  26922. ' o: TObject;',
  26923. 'begin',
  26924. ' v:=o;',
  26925. ' v:=TObject(o);',
  26926. ' v:=TBirdObject(o);',
  26927. ' o:=TObject(v);',
  26928. ' o:=TBirdObject(v);',
  26929. ' if v is TObject then ;',
  26930. '']);
  26931. ConvertProgram;
  26932. CheckSource('TestJSValue_ClassInstance',
  26933. LinesToStr([ // statements
  26934. 'rtl.createClass($mod, "TObject", null, function () {',
  26935. ' this.$init = function () {',
  26936. ' };',
  26937. ' this.$final = function () {',
  26938. ' };',
  26939. '});',
  26940. 'this.v = undefined;',
  26941. 'this.o = null;',
  26942. '']),
  26943. LinesToStr([ // $mod.$main
  26944. '$mod.v = $mod.o;',
  26945. '$mod.v = $mod.o;',
  26946. '$mod.v = $mod.o;',
  26947. '$mod.o = rtl.getObject($mod.v);',
  26948. '$mod.o = rtl.getObject($mod.v);',
  26949. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  26950. '']));
  26951. end;
  26952. procedure TTestModule.TestJSValue_ClassOf;
  26953. begin
  26954. StartProgram(false);
  26955. Add([
  26956. 'type',
  26957. ' TClass = class of TObject;',
  26958. ' TObject = class',
  26959. ' end;',
  26960. ' TBirds = class of TBird;',
  26961. ' TBird = class(TObject) end;',
  26962. 'var',
  26963. ' v: jsvalue;',
  26964. ' c: TClass;',
  26965. 'begin',
  26966. ' v:=c;',
  26967. ' v:=TObject;',
  26968. ' v:=TClass(c);',
  26969. ' v:=TBirds(c);',
  26970. ' c:=TClass(v);',
  26971. ' c:=TBirds(v);',
  26972. ' if v is TClass then ;',
  26973. '']);
  26974. ConvertProgram;
  26975. CheckSource('TestJSValue_ClassOf',
  26976. LinesToStr([ // statements
  26977. 'rtl.createClass($mod, "TObject", null, function () {',
  26978. ' this.$init = function () {',
  26979. ' };',
  26980. ' this.$final = function () {',
  26981. ' };',
  26982. '});',
  26983. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  26984. '});',
  26985. 'this.v = undefined;',
  26986. 'this.c = null;',
  26987. '']),
  26988. LinesToStr([ // $mod.$main
  26989. '$mod.v = $mod.c;',
  26990. '$mod.v = $mod.TObject;',
  26991. '$mod.v = $mod.c;',
  26992. '$mod.v = $mod.c;',
  26993. '$mod.c = rtl.getObject($mod.v);',
  26994. '$mod.c = rtl.getObject($mod.v);',
  26995. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  26996. '']));
  26997. end;
  26998. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  26999. begin
  27000. StartProgram(false);
  27001. Add([
  27002. 'type',
  27003. ' integer = longint;',
  27004. ' TArray = array of JSValue;',
  27005. ' TArrgh = tarray;',
  27006. ' TArrInt = array of integer;',
  27007. 'var',
  27008. ' v: jsvalue;',
  27009. ' TheArray: tarray = (1,''2'');',
  27010. ' Arr: tarrgh;',
  27011. ' i: integer;',
  27012. ' ArrInt: tarrint;',
  27013. 'begin',
  27014. ' arr:=thearray;',
  27015. ' thearray:=arr;',
  27016. ' setlength(arr,2);',
  27017. ' setlength(thearray,3);',
  27018. ' arr[4]:=v;',
  27019. ' arr[5]:=length(thearray);',
  27020. ' arr[6]:=nil;',
  27021. ' arr[7]:=thearray[8];',
  27022. ' arr[low(arr)]:=high(thearray);',
  27023. ' arr:=arrint;',
  27024. ' arrInt:=tarrint(arr);',
  27025. ' if TheArray = nil then ;',
  27026. ' if nil = TheArray then ;',
  27027. ' if TheArray <> nil then ;',
  27028. ' if nil <> TheArray then ;',
  27029. '']);
  27030. ConvertProgram;
  27031. CheckSource('TestJSValue_ArrayOfJSValue',
  27032. LinesToStr([ // statements
  27033. 'this.v = undefined;',
  27034. 'this.TheArray = [1, "2"];',
  27035. 'this.Arr = [];',
  27036. 'this.i = 0;',
  27037. 'this.ArrInt = [];',
  27038. '']),
  27039. LinesToStr([ // $mod.$main
  27040. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27041. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27042. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27043. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27044. '$mod.Arr[4] = $mod.v;',
  27045. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27046. '$mod.Arr[6] = null;',
  27047. '$mod.Arr[7] = $mod.TheArray[8];',
  27048. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27049. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27050. '$mod.ArrInt = $mod.Arr;',
  27051. 'if (rtl.length($mod.TheArray) === 0) ;',
  27052. 'if (rtl.length($mod.TheArray) === 0) ;',
  27053. 'if (rtl.length($mod.TheArray) > 0) ;',
  27054. 'if (rtl.length($mod.TheArray) > 0) ;',
  27055. '']));
  27056. end;
  27057. procedure TTestModule.TestJSValue_ArrayLit;
  27058. begin
  27059. StartProgram(false);
  27060. Add([
  27061. 'type',
  27062. ' TFlag = (big,small);',
  27063. ' TArray = array of JSValue;',
  27064. ' TObject = class end;',
  27065. ' TClass = class of TObject;',
  27066. 'var',
  27067. ' v: jsvalue;',
  27068. ' a: TArray;',
  27069. ' o: TObject;',
  27070. 'begin',
  27071. ' a:=[];',
  27072. ' a:=[1];',
  27073. ' a:=[1,2];',
  27074. ' a:=[big];',
  27075. ' a:=[1,big];',
  27076. ' a:=[o,nil];',
  27077. '']);
  27078. ConvertProgram;
  27079. CheckSource('TestJSValue_ArrayLit',
  27080. LinesToStr([ // statements
  27081. 'this.TFlag = {',
  27082. ' "0": "big",',
  27083. ' big: 0,',
  27084. ' "1": "small",',
  27085. ' small: 1',
  27086. '};',
  27087. 'rtl.createClass($mod, "TObject", null, function () {',
  27088. ' this.$init = function () {',
  27089. ' };',
  27090. ' this.$final = function () {',
  27091. ' };',
  27092. '});',
  27093. 'this.v = undefined;',
  27094. 'this.a = [];',
  27095. 'this.o = null;',
  27096. '']),
  27097. LinesToStr([ // $mod.$main
  27098. '$mod.a = [];',
  27099. '$mod.a = [1];',
  27100. '$mod.a = [1, 2];',
  27101. '$mod.a = [$mod.TFlag.big];',
  27102. '$mod.a = [1, $mod.TFlag.big];',
  27103. '$mod.a = [$mod.o, null];',
  27104. '']));
  27105. end;
  27106. procedure TTestModule.TestJSValue_Params;
  27107. begin
  27108. StartProgram(false);
  27109. Add('type');
  27110. Add(' integer = longint;');
  27111. Add(' TYesNo = boolean;');
  27112. Add(' TFloat = double;');
  27113. Add(' TCaption = string;');
  27114. Add(' TChar = char;');
  27115. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27116. Add('var');
  27117. Add(' l: jsvalue;');
  27118. Add('begin');
  27119. Add(' a:=a;');
  27120. Add(' l:=b;');
  27121. Add(' c:=c;');
  27122. Add(' d:=d;');
  27123. Add(' Result:=l;');
  27124. Add('end;');
  27125. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27126. Add('var');
  27127. Add(' v: jsvalue;');
  27128. Add(' i: integer;');
  27129. Add(' b: TYesNo;');
  27130. Add(' d: TFloat;');
  27131. Add(' s: TCaption;');
  27132. Add(' c: TChar;');
  27133. Add('begin');
  27134. Add(' v:=doit(v,v,v,v);');
  27135. Add(' i:=integer(dosome(i,i));');
  27136. Add(' b:=TYesNo(dosome(b,b));');
  27137. Add(' d:=TFloat(dosome(d,d));');
  27138. Add(' s:=TCaption(dosome(s,s));');
  27139. Add(' c:=TChar(dosome(c,c));');
  27140. ConvertProgram;
  27141. CheckSource('TestJSValue_Params',
  27142. LinesToStr([ // statements
  27143. 'this.DoIt = function (a, b, c, d) {',
  27144. ' var Result = undefined;',
  27145. ' var l = undefined;',
  27146. ' a = a;',
  27147. ' l = b;',
  27148. ' c.set(c.get());',
  27149. ' d.set(d.get());',
  27150. ' Result = l;',
  27151. ' return Result;',
  27152. '};',
  27153. 'this.DoSome = function (a, b) {',
  27154. ' var Result = undefined;',
  27155. ' return Result;',
  27156. '};',
  27157. 'this.v = undefined;',
  27158. 'this.i = 0;',
  27159. 'this.b = false;',
  27160. 'this.d = 0.0;',
  27161. 'this.s = "";',
  27162. 'this.c = "";',
  27163. '']),
  27164. LinesToStr([ // $mod.$main
  27165. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27166. ' p: $mod,',
  27167. ' get: function () {',
  27168. ' return this.p.v;',
  27169. ' },',
  27170. ' set: function (v) {',
  27171. ' this.p.v = v;',
  27172. ' }',
  27173. '}, {',
  27174. ' p: $mod,',
  27175. ' get: function () {',
  27176. ' return this.p.v;',
  27177. ' },',
  27178. ' set: function (v) {',
  27179. ' this.p.v = v;',
  27180. ' }',
  27181. '});',
  27182. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  27183. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27184. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27185. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27186. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27187. '']));
  27188. end;
  27189. procedure TTestModule.TestJSValue_UntypedParam;
  27190. begin
  27191. StartProgram(false);
  27192. Add('function DoIt(const a; var b; out c): jsvalue;');
  27193. Add('begin');
  27194. Add(' Result:=a;');
  27195. Add(' Result:=b;');
  27196. Add(' Result:=c;');
  27197. Add(' b:=Result;');
  27198. Add(' c:=Result;');
  27199. Add('end;');
  27200. Add('var i: longint;');
  27201. Add('begin');
  27202. Add(' doit(i,i,i);');
  27203. ConvertProgram;
  27204. CheckSource('TestJSValue_UntypedParam',
  27205. LinesToStr([ // statements
  27206. 'this.DoIt = function (a, b, c) {',
  27207. ' var Result = undefined;',
  27208. ' Result = a;',
  27209. ' Result = b.get();',
  27210. ' Result = c.get();',
  27211. ' b.set(Result);',
  27212. ' c.set(Result);',
  27213. ' return Result;',
  27214. '};',
  27215. 'this.i = 0;',
  27216. '']),
  27217. LinesToStr([ // $mod.$main
  27218. '$mod.DoIt($mod.i, {',
  27219. ' p: $mod,',
  27220. ' get: function () {',
  27221. ' return this.p.i;',
  27222. ' },',
  27223. ' set: function (v) {',
  27224. ' this.p.i = v;',
  27225. ' }',
  27226. '}, {',
  27227. ' p: $mod,',
  27228. ' get: function () {',
  27229. ' return this.p.i;',
  27230. ' },',
  27231. ' set: function (v) {',
  27232. ' this.p.i = v;',
  27233. ' }',
  27234. '});',
  27235. '']));
  27236. end;
  27237. procedure TTestModule.TestJSValue_FuncResultType;
  27238. begin
  27239. StartProgram(false);
  27240. Add('type');
  27241. Add(' integer = longint;');
  27242. Add(' TJSValueArray = array of JSValue;');
  27243. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27244. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27245. Add('begin');
  27246. Add(' while Compare(P,aList[0])>0 do ;');
  27247. Add('end;');
  27248. Add('var');
  27249. Add(' Compare: TListSortCompare;');
  27250. Add(' V: JSValue;');
  27251. Add(' i: integer;');
  27252. Add('begin');
  27253. Add(' if Compare(V,V)>0 then ;');
  27254. Add(' if Compare(i,i)>1 then ;');
  27255. Add(' if Compare(nil,false)>2 then ;');
  27256. Add(' if Compare(1,true)>3 then ;');
  27257. ConvertProgram;
  27258. CheckSource('TestJSValue_UntypedParam',
  27259. LinesToStr([ // statements
  27260. 'this.Sort = function (P, aList, Compare) {',
  27261. ' while (Compare(P, aList[0]) > 0) {',
  27262. ' };',
  27263. '};',
  27264. 'this.Compare = null;',
  27265. 'this.V = undefined;',
  27266. 'this.i = 0;',
  27267. '']),
  27268. LinesToStr([ // $mod.$main
  27269. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27270. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27271. 'if ($mod.Compare(null, false) > 2) ;',
  27272. 'if ($mod.Compare(1, true) > 3) ;',
  27273. '']));
  27274. end;
  27275. procedure TTestModule.TestJSValue_ProcType_Assign;
  27276. begin
  27277. StartProgram(false);
  27278. Add('type');
  27279. Add(' integer = longint;');
  27280. Add(' TObject = class');
  27281. Add(' class function GetGlob: integer;');
  27282. Add(' function Getter: integer;');
  27283. Add(' end;');
  27284. Add('class function TObject.GetGlob: integer;');
  27285. Add('var v1: jsvalue;');
  27286. Add('begin');
  27287. Add(' v1:=@GetGlob;');
  27288. Add(' v1:[email protected];');
  27289. Add('end;');
  27290. Add('function TObject.Getter: integer;');
  27291. Add('var v2: jsvalue;');
  27292. Add('begin');
  27293. Add(' v2:=@Getter;');
  27294. Add(' v2:[email protected];');
  27295. Add(' v2:=@GetGlob;');
  27296. Add(' v2:[email protected];');
  27297. Add('end;');
  27298. Add('function GetIt(i: integer): integer;');
  27299. Add('var v3: jsvalue;');
  27300. Add('begin');
  27301. Add(' v3:=@GetIt;');
  27302. Add('end;');
  27303. Add('var');
  27304. Add(' V: JSValue;');
  27305. Add(' o: TObject;');
  27306. Add('begin');
  27307. Add(' v:=@GetIt;');
  27308. Add(' v:[email protected];');
  27309. Add(' v:[email protected];');
  27310. ConvertProgram;
  27311. CheckSource('TestJSValue_ProcType_Assign',
  27312. LinesToStr([ // statements
  27313. 'rtl.createClass($mod, "TObject", null, function () {',
  27314. ' this.$init = function () {',
  27315. ' };',
  27316. ' this.$final = function () {',
  27317. ' };',
  27318. ' this.GetGlob = function () {',
  27319. ' var Result = 0;',
  27320. ' var v1 = undefined;',
  27321. ' v1 = rtl.createCallback(this, "GetGlob");',
  27322. ' v1 = rtl.createCallback(this, "GetGlob");',
  27323. ' return Result;',
  27324. ' };',
  27325. ' this.Getter = function () {',
  27326. ' var Result = 0;',
  27327. ' var v2 = undefined;',
  27328. ' v2 = rtl.createCallback(this, "Getter");',
  27329. ' v2 = rtl.createCallback(this, "Getter");',
  27330. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27331. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27332. ' return Result;',
  27333. ' };',
  27334. '});',
  27335. 'this.GetIt = function (i) {',
  27336. ' var Result = 0;',
  27337. ' var v3 = undefined;',
  27338. ' v3 = $mod.GetIt;',
  27339. ' return Result;',
  27340. '};',
  27341. 'this.V = undefined;',
  27342. 'this.o = null;',
  27343. '']),
  27344. LinesToStr([ // $mod.$main
  27345. '$mod.V = $mod.GetIt;',
  27346. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27347. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27348. '']));
  27349. end;
  27350. procedure TTestModule.TestJSValue_ProcType_Equal;
  27351. begin
  27352. StartProgram(false);
  27353. Add('type');
  27354. Add(' integer = longint;');
  27355. Add(' TObject = class');
  27356. Add(' class function GetGlob: integer;');
  27357. Add(' function Getter: integer;');
  27358. Add(' end;');
  27359. Add('class function TObject.GetGlob: integer;');
  27360. Add('var v1: jsvalue;');
  27361. Add('begin');
  27362. Add(' if v1=@GetGlob then;');
  27363. Add(' if [email protected] then ;');
  27364. Add('end;');
  27365. Add('function TObject.Getter: integer;');
  27366. Add('var v2: jsvalue;');
  27367. Add('begin');
  27368. Add(' if v2=@Getter then;');
  27369. Add(' if [email protected] then ;');
  27370. Add(' if v2=@GetGlob then;');
  27371. Add(' if [email protected] then;');
  27372. Add('end;');
  27373. Add('function GetIt(i: integer): integer;');
  27374. Add('var v3: jsvalue;');
  27375. Add('begin');
  27376. Add(' if v3=@GetIt then;');
  27377. Add('end;');
  27378. Add('var');
  27379. Add(' V: JSValue;');
  27380. Add(' o: TObject;');
  27381. Add('begin');
  27382. Add(' if v=@GetIt then;');
  27383. Add(' if [email protected] then;');
  27384. Add(' if [email protected] then;');
  27385. Add(' if @GetIt=v then;');
  27386. Add(' if @o.Getter=v then;');
  27387. Add(' if @o.GetGlob=v then;');
  27388. ConvertProgram;
  27389. CheckSource('TestJSValue_ProcType_Equal',
  27390. LinesToStr([ // statements
  27391. 'rtl.createClass($mod, "TObject", null, function () {',
  27392. ' this.$init = function () {',
  27393. ' };',
  27394. ' this.$final = function () {',
  27395. ' };',
  27396. ' this.GetGlob = function () {',
  27397. ' var Result = 0;',
  27398. ' var v1 = undefined;',
  27399. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27400. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27401. ' return Result;',
  27402. ' };',
  27403. ' this.Getter = function () {',
  27404. ' var Result = 0;',
  27405. ' var v2 = undefined;',
  27406. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27407. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27408. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27409. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27410. ' return Result;',
  27411. ' };',
  27412. '});',
  27413. 'this.GetIt = function (i) {',
  27414. ' var Result = 0;',
  27415. ' var v3 = undefined;',
  27416. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27417. ' return Result;',
  27418. '};',
  27419. 'this.V = undefined;',
  27420. 'this.o = null;',
  27421. '']),
  27422. LinesToStr([ // $mod.$main
  27423. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27424. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27425. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27426. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27427. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  27428. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  27429. '']));
  27430. end;
  27431. procedure TTestModule.TestJSValue_ProcType_Param;
  27432. begin
  27433. StartProgram(false);
  27434. Add([
  27435. 'type',
  27436. ' variant = jsvalue;',
  27437. ' TArrVariant = array of variant;',
  27438. ' TArrVar2 = TArrVariant;',
  27439. ' TFuncInt = function: longint;',
  27440. 'function GetIt: longint;',
  27441. 'begin',
  27442. 'end;',
  27443. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  27444. 'var v: variant;',
  27445. 'begin',
  27446. ' v:=arr[1];',
  27447. 'end;',
  27448. 'var s: string;',
  27449. 'begin',
  27450. ' DoIt(GetIt,[]);',
  27451. ' DoIt(@GetIt,[]);',
  27452. ' DoIt(1,[s,GetIt]);',
  27453. ' DoIt(1,[s,@GetIt]);',
  27454. '']);
  27455. ConvertProgram;
  27456. CheckSource('TestJSValue_ProcType_Param',
  27457. LinesToStr([ // statements
  27458. 'this.GetIt = function () {',
  27459. ' var Result = 0;',
  27460. ' return Result;',
  27461. '};',
  27462. 'this.DoIt = function (p, Arr) {',
  27463. ' var v = undefined;',
  27464. ' v = Arr[1];',
  27465. '};',
  27466. 'this.s = "";',
  27467. '']),
  27468. LinesToStr([ // $mod.$main
  27469. '$mod.DoIt($mod.GetIt(), []);',
  27470. '$mod.DoIt($mod.GetIt, []);',
  27471. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  27472. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  27473. '']));
  27474. end;
  27475. procedure TTestModule.TestJSValue_AssignToPointerFail;
  27476. begin
  27477. StartProgram(false);
  27478. Add([
  27479. 'var',
  27480. ' v: JSValue;',
  27481. ' p: Pointer;',
  27482. 'begin',
  27483. ' p:=v;',
  27484. '']);
  27485. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  27486. nIncompatibleTypesGotExpected);
  27487. ConvertProgram;
  27488. end;
  27489. procedure TTestModule.TestJSValue_OverloadDouble;
  27490. begin
  27491. StartProgram(false);
  27492. Add([
  27493. 'type',
  27494. ' integer = longint;',
  27495. ' tdatetime = double;',
  27496. 'procedure DoIt(d: double); begin end;',
  27497. 'procedure DoIt(v: jsvalue); begin end;',
  27498. 'var',
  27499. ' d: double;',
  27500. ' dt: tdatetime;',
  27501. ' i: integer;',
  27502. ' b: byte;',
  27503. ' shi: shortint;',
  27504. ' w: word;',
  27505. ' smi: smallint;',
  27506. ' lw: longword;',
  27507. ' li: longint;',
  27508. ' ni: nativeint;',
  27509. ' nu: nativeuint;',
  27510. 'begin',
  27511. ' DoIt(d);',
  27512. ' DoIt(dt);',
  27513. ' DoIt(i);',
  27514. ' DoIt(b);',
  27515. ' DoIt(shi);',
  27516. ' DoIt(w);',
  27517. ' DoIt(smi);',
  27518. ' DoIt(lw);',
  27519. ' DoIt(li);',
  27520. ' DoIt(ni);',
  27521. ' DoIt(nu);',
  27522. '']);
  27523. ConvertProgram;
  27524. CheckSource('TestJSValue_OverloadDouble',
  27525. LinesToStr([ // statements
  27526. 'this.DoIt = function (d) {',
  27527. '};',
  27528. 'this.DoIt$1 = function (v) {',
  27529. '};',
  27530. 'this.d = 0.0;',
  27531. 'this.dt = 0.0;',
  27532. 'this.i = 0;',
  27533. 'this.b = 0;',
  27534. 'this.shi = 0;',
  27535. 'this.w = 0;',
  27536. 'this.smi = 0;',
  27537. 'this.lw = 0;',
  27538. 'this.li = 0;',
  27539. 'this.ni = 0;',
  27540. 'this.nu = 0;',
  27541. '']),
  27542. LinesToStr([ // $mod.$main
  27543. '$mod.DoIt($mod.d);',
  27544. '$mod.DoIt($mod.dt);',
  27545. '$mod.DoIt$1($mod.i);',
  27546. '$mod.DoIt$1($mod.b);',
  27547. '$mod.DoIt$1($mod.shi);',
  27548. '$mod.DoIt$1($mod.w);',
  27549. '$mod.DoIt$1($mod.smi);',
  27550. '$mod.DoIt$1($mod.lw);',
  27551. '$mod.DoIt$1($mod.li);',
  27552. '$mod.DoIt$1($mod.ni);',
  27553. '$mod.DoIt$1($mod.nu);',
  27554. '']));
  27555. end;
  27556. procedure TTestModule.TestJSValue_OverloadNativeInt;
  27557. begin
  27558. StartProgram(false);
  27559. Add([
  27560. 'type',
  27561. ' integer = longint;',
  27562. ' int53 = nativeint;',
  27563. ' tdatetime = double;',
  27564. 'procedure DoIt(n: nativeint); begin end;',
  27565. 'procedure DoIt(v: jsvalue); begin end;',
  27566. 'var',
  27567. ' d: double;',
  27568. ' dt: tdatetime;',
  27569. ' i: integer;',
  27570. ' b: byte;',
  27571. ' shi: shortint;',
  27572. ' w: word;',
  27573. ' smi: smallint;',
  27574. ' lw: longword;',
  27575. ' li: longint;',
  27576. ' ni: nativeint;',
  27577. ' nu: nativeuint;',
  27578. 'begin',
  27579. ' DoIt(d);',
  27580. ' DoIt(dt);',
  27581. ' DoIt(i);',
  27582. ' DoIt(b);',
  27583. ' DoIt(shi);',
  27584. ' DoIt(w);',
  27585. ' DoIt(smi);',
  27586. ' DoIt(lw);',
  27587. ' DoIt(li);',
  27588. ' DoIt(ni);',
  27589. ' DoIt(nu);',
  27590. '']);
  27591. ConvertProgram;
  27592. CheckSource('TestJSValue_OverloadNativeInt',
  27593. LinesToStr([ // statements
  27594. 'this.DoIt = function (n) {',
  27595. '};',
  27596. 'this.DoIt$1 = function (v) {',
  27597. '};',
  27598. 'this.d = 0.0;',
  27599. 'this.dt = 0.0;',
  27600. 'this.i = 0;',
  27601. 'this.b = 0;',
  27602. 'this.shi = 0;',
  27603. 'this.w = 0;',
  27604. 'this.smi = 0;',
  27605. 'this.lw = 0;',
  27606. 'this.li = 0;',
  27607. 'this.ni = 0;',
  27608. 'this.nu = 0;',
  27609. '']),
  27610. LinesToStr([ // $mod.$main
  27611. '$mod.DoIt$1($mod.d);',
  27612. '$mod.DoIt$1($mod.dt);',
  27613. '$mod.DoIt($mod.i);',
  27614. '$mod.DoIt($mod.b);',
  27615. '$mod.DoIt($mod.shi);',
  27616. '$mod.DoIt($mod.w);',
  27617. '$mod.DoIt($mod.smi);',
  27618. '$mod.DoIt($mod.lw);',
  27619. '$mod.DoIt($mod.li);',
  27620. '$mod.DoIt($mod.ni);',
  27621. '$mod.DoIt($mod.nu);',
  27622. '']));
  27623. end;
  27624. procedure TTestModule.TestJSValue_OverloadWord;
  27625. begin
  27626. StartProgram(false);
  27627. Add([
  27628. 'type',
  27629. ' integer = longint;',
  27630. ' int53 = nativeint;',
  27631. ' tdatetime = double;',
  27632. 'procedure DoIt(w: word); begin end;',
  27633. 'procedure DoIt(v: jsvalue); begin end;',
  27634. 'var',
  27635. ' d: double;',
  27636. ' dt: tdatetime;',
  27637. ' i: integer;',
  27638. ' b: byte;',
  27639. ' shi: shortint;',
  27640. ' w: word;',
  27641. ' smi: smallint;',
  27642. ' lw: longword;',
  27643. ' li: longint;',
  27644. ' ni: nativeint;',
  27645. ' nu: nativeuint;',
  27646. 'begin',
  27647. ' DoIt(d);',
  27648. ' DoIt(dt);',
  27649. ' DoIt(i);',
  27650. ' DoIt(b);',
  27651. ' DoIt(shi);',
  27652. ' DoIt(w);',
  27653. ' DoIt(smi);',
  27654. ' DoIt(lw);',
  27655. ' DoIt(li);',
  27656. ' DoIt(ni);',
  27657. ' DoIt(nu);',
  27658. '']);
  27659. ConvertProgram;
  27660. CheckSource('TestJSValue_OverloadWord',
  27661. LinesToStr([ // statements
  27662. 'this.DoIt = function (w) {',
  27663. '};',
  27664. 'this.DoIt$1 = function (v) {',
  27665. '};',
  27666. 'this.d = 0.0;',
  27667. 'this.dt = 0.0;',
  27668. 'this.i = 0;',
  27669. 'this.b = 0;',
  27670. 'this.shi = 0;',
  27671. 'this.w = 0;',
  27672. 'this.smi = 0;',
  27673. 'this.lw = 0;',
  27674. 'this.li = 0;',
  27675. 'this.ni = 0;',
  27676. 'this.nu = 0;',
  27677. '']),
  27678. LinesToStr([ // $mod.$main
  27679. '$mod.DoIt$1($mod.d);',
  27680. '$mod.DoIt$1($mod.dt);',
  27681. '$mod.DoIt$1($mod.i);',
  27682. '$mod.DoIt($mod.b);',
  27683. '$mod.DoIt($mod.shi);',
  27684. '$mod.DoIt($mod.w);',
  27685. '$mod.DoIt$1($mod.smi);',
  27686. '$mod.DoIt$1($mod.lw);',
  27687. '$mod.DoIt$1($mod.li);',
  27688. '$mod.DoIt$1($mod.ni);',
  27689. '$mod.DoIt$1($mod.nu);',
  27690. '']));
  27691. end;
  27692. procedure TTestModule.TestJSValue_OverloadString;
  27693. begin
  27694. StartProgram(false);
  27695. Add([
  27696. 'type',
  27697. ' uni = string;',
  27698. ' WChar = char;',
  27699. 'procedure DoIt(s: string); begin end;',
  27700. 'procedure DoIt(v: jsvalue); begin end;',
  27701. 'var',
  27702. ' s: string;',
  27703. ' c: char;',
  27704. ' u: uni;',
  27705. 'begin',
  27706. ' DoIt(s);',
  27707. ' DoIt(c);',
  27708. ' DoIt(u);',
  27709. '']);
  27710. ConvertProgram;
  27711. CheckSource('TestJSValue_OverloadString',
  27712. LinesToStr([ // statements
  27713. 'this.DoIt = function (s) {',
  27714. '};',
  27715. 'this.DoIt$1 = function (v) {',
  27716. '};',
  27717. 'this.s = "";',
  27718. 'this.c = "";',
  27719. 'this.u = "";',
  27720. '']),
  27721. LinesToStr([ // $mod.$main
  27722. '$mod.DoIt($mod.s);',
  27723. '$mod.DoIt($mod.c);',
  27724. '$mod.DoIt($mod.u);',
  27725. '']));
  27726. end;
  27727. procedure TTestModule.TestJSValue_OverloadChar;
  27728. begin
  27729. StartProgram(false);
  27730. Add([
  27731. 'type',
  27732. ' uni = string;',
  27733. ' WChar = char;',
  27734. 'procedure DoIt(c: char); begin end;',
  27735. 'procedure DoIt(v: jsvalue); begin end;',
  27736. 'var',
  27737. ' s: string;',
  27738. ' c: char;',
  27739. ' u: uni;',
  27740. 'begin',
  27741. ' DoIt(s);',
  27742. ' DoIt(c);',
  27743. ' DoIt(u);',
  27744. '']);
  27745. ConvertProgram;
  27746. CheckSource('TestJSValue_OverloadChar',
  27747. LinesToStr([ // statements
  27748. 'this.DoIt = function (c) {',
  27749. '};',
  27750. 'this.DoIt$1 = function (v) {',
  27751. '};',
  27752. 'this.s = "";',
  27753. 'this.c = "";',
  27754. 'this.u = "";',
  27755. '']),
  27756. LinesToStr([ // $mod.$main
  27757. '$mod.DoIt$1($mod.s);',
  27758. '$mod.DoIt($mod.c);',
  27759. '$mod.DoIt$1($mod.u);',
  27760. '']));
  27761. end;
  27762. procedure TTestModule.TestJSValue_OverloadPointer;
  27763. begin
  27764. StartProgram(false);
  27765. Add([
  27766. 'type',
  27767. ' TObject = class end;',
  27768. 'procedure DoIt(p: pointer); begin end;',
  27769. 'procedure DoIt(v: jsvalue); begin end;',
  27770. 'var',
  27771. ' o: TObject;',
  27772. 'begin',
  27773. ' DoIt(o);',
  27774. '']);
  27775. ConvertProgram;
  27776. CheckSource('TestJSValue_OverloadPointer',
  27777. LinesToStr([ // statements
  27778. 'rtl.createClass($mod, "TObject", null, function () {',
  27779. ' this.$init = function () {',
  27780. ' };',
  27781. ' this.$final = function () {',
  27782. ' };',
  27783. '});',
  27784. 'this.DoIt = function (p) {',
  27785. '};',
  27786. 'this.DoIt$1 = function (v) {',
  27787. '};',
  27788. 'this.o = null;',
  27789. '']),
  27790. LinesToStr([ // $mod.$main
  27791. '$mod.DoIt($mod.o);',
  27792. '']));
  27793. end;
  27794. procedure TTestModule.TestJSValue_ForIn;
  27795. begin
  27796. StartProgram(false);
  27797. Add([
  27798. 'var',
  27799. ' v: JSValue;',
  27800. ' key: string;',
  27801. 'begin',
  27802. ' for key in v do begin',
  27803. ' if key=''abc'' then ;',
  27804. ' end;',
  27805. '']);
  27806. ConvertProgram;
  27807. CheckSource('TestJSValue_ForIn',
  27808. LinesToStr([ // statements
  27809. 'this.v = undefined;',
  27810. 'this.key = "";',
  27811. '']),
  27812. LinesToStr([ // $mod.$main
  27813. 'for ($mod.key in $mod.v) {',
  27814. ' if ($mod.key === "abc") ;',
  27815. '};',
  27816. '']));
  27817. end;
  27818. procedure TTestModule.TestRTTI_IntRange;
  27819. begin
  27820. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27821. StartProgram(true,[supTypeInfo]);
  27822. Add([
  27823. '{$modeswitch externalclass}',
  27824. 'type',
  27825. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  27826. ' TColor = type TGraphicsColor;',
  27827. 'var',
  27828. ' p: TTypeInfo;',
  27829. ' k: TTypeKind;',
  27830. 'begin',
  27831. ' p:=typeinfo(TGraphicsColor);',
  27832. ' p:=typeinfo(TColor);',
  27833. ' k:=GetTypeKind(TGraphicsColor);',
  27834. ' k:=GetTypeKind(TColor);',
  27835. '']);
  27836. ConvertProgram;
  27837. CheckSource('TestRTTI_IntRange',
  27838. LinesToStr([ // statements
  27839. '$mod.$rtti.$Int("TGraphicsColor", {',
  27840. ' minvalue: -2147483648,',
  27841. ' maxvalue: 2147483647,',
  27842. ' ordtype: 4',
  27843. '});',
  27844. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  27845. 'this.p = null;',
  27846. 'this.k = 0;',
  27847. '']),
  27848. LinesToStr([ // $mod.$main
  27849. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  27850. '$mod.p = $mod.$rtti["TColor"];',
  27851. '$mod.k = 1;',
  27852. '$mod.k = 1;',
  27853. '']));
  27854. end;
  27855. procedure TTestModule.TestRTTI_Double;
  27856. begin
  27857. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27858. StartProgram(true,[supTypeInfo]);
  27859. Add([
  27860. '{$modeswitch externalclass}',
  27861. 'type',
  27862. ' TFloat = type double;',
  27863. 'var',
  27864. ' p: TTypeInfo;',
  27865. 'begin',
  27866. ' p:=typeinfo(double);',
  27867. ' p:=typeinfo(TFloat);',
  27868. '']);
  27869. ConvertProgram;
  27870. CheckSource('TestRTTI_Double',
  27871. LinesToStr([ // statements
  27872. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  27873. 'this.p = null;',
  27874. '']),
  27875. LinesToStr([ // $mod.$main
  27876. '$mod.p = rtl.double;',
  27877. '$mod.p = $mod.$rtti["TFloat"];',
  27878. '']));
  27879. end;
  27880. procedure TTestModule.TestRTTI_ProcType;
  27881. begin
  27882. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27883. StartProgram(false);
  27884. Add('type');
  27885. Add(' TProcA = procedure;');
  27886. Add(' TMethodB = procedure of object;');
  27887. Add(' TProcC = procedure; varargs;');
  27888. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  27889. Add(' TProcE = function: nativeint;');
  27890. Add(' TProcF = function(const p: TProcA): nativeuint;');
  27891. Add('var p: pointer;');
  27892. Add('begin');
  27893. Add(' p:=typeinfo(tproca);');
  27894. ConvertProgram;
  27895. CheckSource('TestRTTI_ProcType',
  27896. LinesToStr([ // statements
  27897. '$mod.$rtti.$ProcVar("TProcA", {',
  27898. ' procsig: rtl.newTIProcSig(null)',
  27899. '});',
  27900. '$mod.$rtti.$MethodVar("TMethodB", {',
  27901. ' procsig: rtl.newTIProcSig(null),',
  27902. ' methodkind: 0',
  27903. '});',
  27904. '$mod.$rtti.$ProcVar("TProcC", {',
  27905. ' procsig: rtl.newTIProcSig(null, 2)',
  27906. '});',
  27907. '$mod.$rtti.$ProcVar("TProcD", {',
  27908. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  27909. '});',
  27910. '$mod.$rtti.$ProcVar("TProcE", {',
  27911. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  27912. '});',
  27913. '$mod.$rtti.$ProcVar("TProcF", {',
  27914. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  27915. '});',
  27916. 'this.p = null;',
  27917. '']),
  27918. LinesToStr([ // $mod.$main
  27919. '$mod.p = $mod.$rtti["TProcA"];',
  27920. '']));
  27921. end;
  27922. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  27923. begin
  27924. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27925. AddModuleWithIntfImplSrc('unit2.pas',
  27926. LinesToStr([
  27927. 'type',
  27928. ' TObject = class end;'
  27929. ]),
  27930. '');
  27931. StartUnit(true);
  27932. Add('interface');
  27933. Add('uses unit2;');
  27934. Add('type');
  27935. Add(' TProcA = function(o: tobject): tobject;');
  27936. Add('implementation');
  27937. Add('type');
  27938. Add(' TProcB = function(o: tobject): tobject;');
  27939. Add('var p: Pointer;');
  27940. Add('initialization');
  27941. Add(' p:=typeinfo(tproca);');
  27942. Add(' p:=typeinfo(tprocb);');
  27943. ConvertUnit;
  27944. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  27945. LinesToStr([ // statements
  27946. 'var $impl = $mod.$impl;',
  27947. '$mod.$rtti.$ProcVar("TProcA", {',
  27948. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  27949. '});',
  27950. '']),
  27951. LinesToStr([ // this.$init
  27952. '$impl.p = $mod.$rtti["TProcA"];',
  27953. '$impl.p = $mod.$rtti["TProcB"];',
  27954. '']),
  27955. LinesToStr([ // implementation
  27956. '$mod.$rtti.$ProcVar("TProcB", {',
  27957. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  27958. '});',
  27959. '$impl.p = null;',
  27960. '']) );
  27961. end;
  27962. procedure TTestModule.TestRTTI_EnumAndSetType;
  27963. begin
  27964. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27965. StartProgram(false);
  27966. Add('type');
  27967. Add(' TFlag = (light,dark);');
  27968. Add(' TFlags = set of TFlag;');
  27969. Add(' TProc = function(f: TFlags): TFlag;');
  27970. Add('var p: pointer;');
  27971. Add('begin');
  27972. Add(' p:=typeinfo(tflag);');
  27973. Add(' p:=typeinfo(tflags);');
  27974. ConvertProgram;
  27975. CheckSource('TestRTTI_EnumAndType',
  27976. LinesToStr([ // statements
  27977. 'this.TFlag = {',
  27978. ' "0": "light",',
  27979. ' light: 0,',
  27980. ' "1": "dark",',
  27981. ' dark: 1',
  27982. '};',
  27983. '$mod.$rtti.$Enum("TFlag", {',
  27984. ' minvalue: 0,',
  27985. ' maxvalue: 1,',
  27986. ' ordtype: 1,',
  27987. ' enumtype: this.TFlag',
  27988. '});',
  27989. '$mod.$rtti.$Set("TFlags", {',
  27990. ' comptype: $mod.$rtti["TFlag"]',
  27991. '});',
  27992. '$mod.$rtti.$ProcVar("TProc", {',
  27993. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  27994. '});',
  27995. 'this.p = null;',
  27996. '']),
  27997. LinesToStr([ // $mod.$main
  27998. '$mod.p = $mod.$rtti["TFlag"];',
  27999. '$mod.p = $mod.$rtti["TFlags"];',
  28000. '']));
  28001. end;
  28002. procedure TTestModule.TestRTTI_EnumRange;
  28003. begin
  28004. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28005. StartProgram(false);
  28006. Add([
  28007. 'type',
  28008. ' TCol = (red,green,blue);',
  28009. ' TColRg = green..blue;',
  28010. ' TSetOfColRg = set of TColRg;',
  28011. 'var p: pointer;',
  28012. 'begin',
  28013. ' p:=typeinfo(tcolrg);',
  28014. ' p:=typeinfo(tsetofcolrg);',
  28015. '']);
  28016. ConvertProgram;
  28017. end;
  28018. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28019. begin
  28020. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28021. StartProgram(false);
  28022. Add('type');
  28023. Add(' TFlags = set of (red, green);');
  28024. Add('var');
  28025. Add(' f: TFlags;');
  28026. Add('begin');
  28027. Add(' Include(f,red);');
  28028. ConvertProgram;
  28029. CheckSource('TestRTTI_AnonymousEnumType',
  28030. LinesToStr([ // statements
  28031. 'this.TFlags$a = {',
  28032. ' "0": "red",',
  28033. ' red: 0,',
  28034. ' "1": "green",',
  28035. ' green: 1',
  28036. '};',
  28037. '$mod.$rtti.$Enum("TFlags$a", {',
  28038. ' minvalue: 0,',
  28039. ' maxvalue: 1,',
  28040. ' ordtype: 1,',
  28041. ' enumtype: this.TFlags$a',
  28042. '});',
  28043. '$mod.$rtti.$Set("TFlags", {',
  28044. ' comptype: $mod.$rtti["TFlags$a"]',
  28045. '});',
  28046. 'this.f = {};',
  28047. '']),
  28048. LinesToStr([
  28049. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28050. '']));
  28051. end;
  28052. procedure TTestModule.TestRTTI_StaticArray;
  28053. begin
  28054. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28055. StartProgram(false);
  28056. Add('type');
  28057. Add(' TFlag = (light,dark);');
  28058. Add(' TFlagNames = array[TFlag] of string;');
  28059. Add(' TBoolNames = array[boolean] of string;');
  28060. Add(' TByteArray = array[1..32768] of byte;');
  28061. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28062. Add('var p: pointer;');
  28063. Add('begin');
  28064. Add(' p:=typeinfo(TFlagNames);');
  28065. Add(' p:=typeinfo(TBoolNames);');
  28066. ConvertProgram;
  28067. CheckSource('TestRTTI_StaticArray',
  28068. LinesToStr([ // statements
  28069. 'this.TFlag = {',
  28070. ' "0": "light",',
  28071. ' light: 0,',
  28072. ' "1": "dark",',
  28073. ' dark: 1',
  28074. '};',
  28075. '$mod.$rtti.$Enum("TFlag", {',
  28076. ' minvalue: 0,',
  28077. ' maxvalue: 1,',
  28078. ' ordtype: 1,',
  28079. ' enumtype: this.TFlag',
  28080. '});',
  28081. '$mod.$rtti.$StaticArray("TFlagNames", {',
  28082. ' dims: [2],',
  28083. ' eltype: rtl.string',
  28084. '});',
  28085. '$mod.$rtti.$StaticArray("TBoolNames", {',
  28086. ' dims: [2],',
  28087. ' eltype: rtl.string',
  28088. '});',
  28089. '$mod.$rtti.$StaticArray("TByteArray", {',
  28090. ' dims: [32768],',
  28091. ' eltype: rtl.byte',
  28092. '});',
  28093. '$mod.$rtti.$ProcVar("TProc", {',
  28094. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  28095. '});',
  28096. 'this.p = null;',
  28097. '']),
  28098. LinesToStr([ // $mod.$main
  28099. '$mod.p = $mod.$rtti["TFlagNames"];',
  28100. '$mod.p = $mod.$rtti["TBoolNames"];',
  28101. '']));
  28102. end;
  28103. procedure TTestModule.TestRTTI_DynArray;
  28104. begin
  28105. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28106. StartProgram(false);
  28107. Add('type');
  28108. Add(' TArrStr = array of string;');
  28109. Add(' TArr2Dim = array of tarrstr;');
  28110. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28111. Add('var p: pointer;');
  28112. Add('begin');
  28113. Add(' p:=typeinfo(tarrstr);');
  28114. Add(' p:=typeinfo(tarr2dim);');
  28115. ConvertProgram;
  28116. CheckSource('TestRTTI_DynArray',
  28117. LinesToStr([ // statements
  28118. '$mod.$rtti.$DynArray("TArrStr", {',
  28119. ' eltype: rtl.string',
  28120. '});',
  28121. '$mod.$rtti.$DynArray("TArr2Dim", {',
  28122. ' eltype: $mod.$rtti["TArrStr"]',
  28123. '});',
  28124. '$mod.$rtti.$ProcVar("TProc", {',
  28125. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  28126. '});',
  28127. 'this.p = null;',
  28128. '']),
  28129. LinesToStr([ // $mod.$main
  28130. '$mod.p = $mod.$rtti["TArrStr"];',
  28131. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28132. '']));
  28133. end;
  28134. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28135. begin
  28136. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28137. StartProgram(false);
  28138. Add('type');
  28139. Add(' TArr = array of array of longint;');
  28140. Add('var a: TArr;');
  28141. Add('begin');
  28142. ConvertProgram;
  28143. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28144. LinesToStr([ // statements
  28145. '$mod.$rtti.$DynArray("TArr$a", {',
  28146. ' eltype: rtl.longint',
  28147. '});',
  28148. '$mod.$rtti.$DynArray("TArr", {',
  28149. ' eltype: $mod.$rtti["TArr$a"]',
  28150. '});',
  28151. 'this.a = [];',
  28152. '']),
  28153. LinesToStr([ // $mod.$main
  28154. ]));
  28155. end;
  28156. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28157. begin
  28158. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28159. StartProgram(false);
  28160. Add('type');
  28161. Add(' TObject = class');
  28162. Add(' published');
  28163. Add(' procedure Proc; virtual; abstract;');
  28164. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28165. Add(' end;');
  28166. Add('begin');
  28167. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28168. nDuplicatePublishedMethodXAtY);
  28169. ConvertProgram;
  28170. end;
  28171. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28172. begin
  28173. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28174. StartProgram(false);
  28175. Add('type');
  28176. Add(' TObject = class');
  28177. Add(' published');
  28178. Add(' procedure Proc; external name ''foo'';');
  28179. Add(' end;');
  28180. Add('begin');
  28181. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28182. nPublishedNameMustMatchExternal);
  28183. ConvertProgram;
  28184. end;
  28185. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28186. begin
  28187. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28188. StartProgram(false);
  28189. Add('type');
  28190. Add(' TObject = class');
  28191. Add(' class var FA: longint;');
  28192. Add(' published');
  28193. Add(' class property A: longint read FA;');
  28194. Add(' end;');
  28195. Add('begin');
  28196. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28197. nInvalidXModifierY);
  28198. ConvertProgram;
  28199. end;
  28200. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28201. begin
  28202. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28203. StartProgram(false);
  28204. Add('type');
  28205. Add(' TObject = class');
  28206. Add(' published');
  28207. Add(' class var FA: longint;');
  28208. Add(' end;');
  28209. Add('begin');
  28210. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28211. nSymbolCannotBePublished);
  28212. ConvertProgram;
  28213. end;
  28214. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28215. begin
  28216. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28217. StartProgram(false);
  28218. Add('{$modeswitch externalclass}');
  28219. Add('type');
  28220. Add(' TObject = class');
  28221. Add(' published');
  28222. Add(' V: longint; external name ''foo'';');
  28223. Add(' end;');
  28224. Add('begin');
  28225. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28226. nPublishedNameMustMatchExternal);
  28227. ConvertProgram;
  28228. end;
  28229. procedure TTestModule.TestRTTI_Class_Field;
  28230. begin
  28231. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28232. StartProgram(false);
  28233. Add('{$modeswitch externalclass}');
  28234. Add('type');
  28235. Add(' TObject = class');
  28236. Add(' private');
  28237. Add(' FPropA: string;');
  28238. Add(' published');
  28239. Add(' VarLI: longint;');
  28240. Add(' VarC: char;');
  28241. Add(' VarS: string;');
  28242. Add(' VarD: double;');
  28243. Add(' VarB: boolean;');
  28244. Add(' VarLW: longword;');
  28245. Add(' VarSmI: smallint;');
  28246. Add(' VarW: word;');
  28247. Add(' VarShI: shortint;');
  28248. Add(' VarBy: byte;');
  28249. Add(' VarExt: longint external name ''VarExt'';');
  28250. Add(' ArrA, ArrB: array of byte;');
  28251. Add(' end;');
  28252. Add('var p: pointer;');
  28253. Add(' Obj: tobject;');
  28254. Add('begin');
  28255. Add(' p:=typeinfo(tobject);');
  28256. Add(' p:=typeinfo(p);');
  28257. Add(' p:=typeinfo(obj);');
  28258. ConvertProgram;
  28259. CheckSource('TestRTTI_Class_Field',
  28260. LinesToStr([ // statements
  28261. 'rtl.createClass($mod, "TObject", null, function () {',
  28262. ' this.$init = function () {',
  28263. ' this.FPropA = "";',
  28264. ' this.VarLI = 0;',
  28265. ' this.VarC = "";',
  28266. ' this.VarS = "";',
  28267. ' this.VarD = 0.0;',
  28268. ' this.VarB = false;',
  28269. ' this.VarLW = 0;',
  28270. ' this.VarSmI = 0;',
  28271. ' this.VarW = 0;',
  28272. ' this.VarShI = 0;',
  28273. ' this.VarBy = 0;',
  28274. ' this.ArrA = [];',
  28275. ' this.ArrB = [];',
  28276. ' };',
  28277. ' this.$final = function () {',
  28278. ' this.ArrA = undefined;',
  28279. ' this.ArrB = undefined;',
  28280. ' };',
  28281. ' var $r = this.$rtti;',
  28282. ' $r.addField("VarLI", rtl.longint);',
  28283. ' $r.addField("VarC", rtl.char);',
  28284. ' $r.addField("VarS", rtl.string);',
  28285. ' $r.addField("VarD", rtl.double);',
  28286. ' $r.addField("VarB", rtl.boolean);',
  28287. ' $r.addField("VarLW", rtl.longword);',
  28288. ' $r.addField("VarSmI", rtl.smallint);',
  28289. ' $r.addField("VarW", rtl.word);',
  28290. ' $r.addField("VarShI", rtl.shortint);',
  28291. ' $r.addField("VarBy", rtl.byte);',
  28292. ' $r.addField("VarExt", rtl.longint);',
  28293. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28294. ' eltype: rtl.byte',
  28295. ' });',
  28296. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28297. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28298. '});',
  28299. 'this.p = null;',
  28300. 'this.Obj = null;',
  28301. '']),
  28302. LinesToStr([ // $mod.$main
  28303. '$mod.p = $mod.$rtti["TObject"];',
  28304. '$mod.p = rtl.pointer;',
  28305. '$mod.p = $mod.Obj.$rtti;',
  28306. '']));
  28307. end;
  28308. procedure TTestModule.TestRTTI_Class_Method;
  28309. begin
  28310. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28311. StartProgram(false);
  28312. Add('type');
  28313. Add(' TObject = class');
  28314. Add(' private');
  28315. Add(' procedure Internal; external name ''$intern'';');
  28316. Add(' published');
  28317. Add(' procedure Click; virtual; abstract;');
  28318. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28319. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28320. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28321. Add(' end;');
  28322. Add('begin');
  28323. ConvertProgram;
  28324. CheckSource('TestRTTI_Class_Method',
  28325. LinesToStr([ // statements
  28326. 'rtl.createClass($mod, "TObject", null, function () {',
  28327. ' this.$init = function () {',
  28328. ' };',
  28329. ' this.$final = function () {',
  28330. ' };',
  28331. ' var $r = this.$rtti;',
  28332. ' $r.addMethod("Click", 0, null);',
  28333. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28334. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28335. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28336. ' flags: 2',
  28337. ' });',
  28338. '});',
  28339. '']),
  28340. LinesToStr([ // $mod.$main
  28341. '']));
  28342. end;
  28343. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28344. begin
  28345. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28346. StartProgram(false);
  28347. Add('type');
  28348. Add(' TObject = class');
  28349. Add(' published');
  28350. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28351. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28352. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28353. Add(' end;');
  28354. Add('begin');
  28355. ConvertProgram;
  28356. CheckSource('TestRTTI_Class_MethodOpenArray',
  28357. LinesToStr([ // statements
  28358. 'rtl.createClass($mod, "TObject", null, function () {',
  28359. ' this.$init = function () {',
  28360. ' };',
  28361. ' this.$final = function () {',
  28362. ' };',
  28363. ' var $r = this.$rtti;',
  28364. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28365. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28366. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28367. '});',
  28368. '']),
  28369. LinesToStr([ // $mod.$main
  28370. '']));
  28371. end;
  28372. procedure TTestModule.TestRTTI_Class_Property;
  28373. begin
  28374. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28375. StartProgram(false);
  28376. Add('{$modeswitch externalclass}');
  28377. Add('type');
  28378. Add(' TObject = class');
  28379. Add(' private');
  28380. Add(' FColor: longint;');
  28381. Add(' FColorStored: boolean;');
  28382. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28383. Add(' function GetColor: longint; virtual; abstract;');
  28384. Add(' function GetColorStored: boolean; virtual; abstract;');
  28385. Add(' FExtSize: longint external name ''$extSize'';');
  28386. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28387. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28388. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28389. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28390. Add(' published');
  28391. Add(' property ColorA: longint read FColor;');
  28392. Add(' property ColorB: longint write FColor;');
  28393. Add(' property ColorC: longint read GetColor write SetColor;');
  28394. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28395. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28396. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28397. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28398. Add(' end;');
  28399. Add('begin');
  28400. ConvertProgram;
  28401. CheckSource('TestRTTI_Class_Property',
  28402. LinesToStr([ // statements
  28403. 'rtl.createClass($mod, "TObject", null, function () {',
  28404. ' this.$init = function () {',
  28405. ' this.FColor = 0;',
  28406. ' this.FColorStored = false;',
  28407. ' };',
  28408. ' this.$final = function () {',
  28409. ' };',
  28410. ' var $r = this.$rtti;',
  28411. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28412. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28413. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28414. ' $r.addProperty(',
  28415. ' "ColorD",',
  28416. ' 8,',
  28417. ' rtl.longint,',
  28418. ' "FColor",',
  28419. ' "FColor",',
  28420. ' {',
  28421. ' stored: "FColorStored"',
  28422. ' }',
  28423. ' );',
  28424. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28425. ' $r.addProperty(',
  28426. ' "ExtSizeB",',
  28427. ' 11,',
  28428. ' rtl.longint,',
  28429. ' "$getSize",',
  28430. ' "$setSize",',
  28431. ' {',
  28432. ' stored: "$extSizeStored"',
  28433. ' }',
  28434. ' );',
  28435. ' $r.addProperty(',
  28436. ' "ExtSizeC",',
  28437. ' 12,',
  28438. ' rtl.longint,',
  28439. ' "$extSize",',
  28440. ' "$extSize",',
  28441. ' {',
  28442. ' stored: "$getExtSizeStored"',
  28443. ' }',
  28444. ' );',
  28445. '});',
  28446. '']),
  28447. LinesToStr([ // $mod.$main
  28448. '']));
  28449. end;
  28450. procedure TTestModule.TestRTTI_Class_PropertyParams;
  28451. begin
  28452. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28453. StartProgram(false);
  28454. Add('{$modeswitch externalclass}');
  28455. Add('type');
  28456. Add(' integer = longint;');
  28457. Add(' TObject = class');
  28458. Add(' private');
  28459. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  28460. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  28461. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  28462. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  28463. Add(' published');
  28464. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  28465. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  28466. Add(' end;');
  28467. Add('begin');
  28468. ConvertProgram;
  28469. CheckSource('TestRTTI_Class_PropertyParams',
  28470. LinesToStr([ // statements
  28471. 'rtl.createClass($mod, "TObject", null, function () {',
  28472. ' this.$init = function () {',
  28473. ' };',
  28474. ' this.$final = function () {',
  28475. ' };',
  28476. ' var $r = this.$rtti;',
  28477. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  28478. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  28479. '});',
  28480. '']),
  28481. LinesToStr([ // $mod.$main
  28482. '']));
  28483. end;
  28484. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  28485. begin
  28486. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28487. AddModuleWithIntfImplSrc('unit1.pas',
  28488. 'type TColor = -5..5;',
  28489. '');
  28490. StartProgram(true);
  28491. Add([
  28492. 'uses unit1;',
  28493. 'type',
  28494. ' TColorAlias = TColor;',
  28495. ' TColorTypeAlias = type TColor;',
  28496. ' TObject = class',
  28497. ' private',
  28498. ' fColor: TColor;',
  28499. ' fAlias: TColorAlias;',
  28500. ' fTypeAlias: TColorTypeAlias;',
  28501. ' published',
  28502. ' property Color: TColor read fcolor;',
  28503. ' property Alias: TColorAlias read falias;',
  28504. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  28505. ' end;',
  28506. 'begin',
  28507. '']);
  28508. ConvertProgram;
  28509. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  28510. LinesToStr([ // statements
  28511. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  28512. 'rtl.createClass($mod, "TObject", null, function () {',
  28513. ' this.$init = function () {',
  28514. ' this.fColor = 0;',
  28515. ' this.fAlias = 0;',
  28516. ' this.fTypeAlias = 0;',
  28517. ' };',
  28518. ' this.$final = function () {',
  28519. ' };',
  28520. ' var $r = this.$rtti;',
  28521. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  28522. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  28523. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  28524. '});',
  28525. '']),
  28526. LinesToStr([ // $mod.$main
  28527. '']));
  28528. end;
  28529. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  28530. begin
  28531. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28532. StartProgram(false);
  28533. Add([
  28534. '{$modeswitch omitrtti}',
  28535. 'type',
  28536. ' TObject = class',
  28537. ' private',
  28538. ' FA: byte;',
  28539. ' published',
  28540. ' property A: byte read FA write FA;',
  28541. ' end;',
  28542. 'begin']);
  28543. ConvertProgram;
  28544. CheckSource('TestRTTI_Class_OmitRTTI',
  28545. LinesToStr([ // statements
  28546. 'rtl.createClass($mod, "TObject", null, function () {',
  28547. ' this.$init = function () {',
  28548. ' this.FA = 0;',
  28549. ' };',
  28550. ' this.$final = function () {',
  28551. ' };',
  28552. '});',
  28553. '']),
  28554. LinesToStr([ // $mod.$main
  28555. '']));
  28556. end;
  28557. procedure TTestModule.TestRTTI_IndexModifier;
  28558. begin
  28559. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28560. StartProgram(false);
  28561. Add([
  28562. 'type',
  28563. ' TEnum = (red, blue);',
  28564. ' TObject = class',
  28565. ' FB: boolean;',
  28566. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  28567. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  28568. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  28569. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  28570. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  28571. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  28572. ' published',
  28573. ' property B1: boolean index 1 read FB write SetIntBool;',
  28574. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  28575. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  28576. ' end;',
  28577. 'begin']);
  28578. ConvertProgram;
  28579. CheckSource('TestRTTI_IndexModifier',
  28580. LinesToStr([ // statements
  28581. 'this.TEnum = {',
  28582. ' "0": "red",',
  28583. ' red: 0,',
  28584. ' "1": "blue",',
  28585. ' blue: 1',
  28586. '};',
  28587. '$mod.$rtti.$Enum("TEnum", {',
  28588. ' minvalue: 0,',
  28589. ' maxvalue: 1,',
  28590. ' ordtype: 1,',
  28591. ' enumtype: this.TEnum',
  28592. '});',
  28593. 'rtl.createClass($mod, "TObject", null, function () {',
  28594. ' this.$init = function () {',
  28595. ' this.FB = false;',
  28596. ' };',
  28597. ' this.$final = function () {',
  28598. ' };',
  28599. ' var $r = this.$rtti;',
  28600. ' $r.addProperty(',
  28601. ' "B1",',
  28602. ' 18,',
  28603. ' rtl.boolean,',
  28604. ' "FB",',
  28605. ' "SetIntBool",',
  28606. ' {',
  28607. ' index: 1',
  28608. ' }',
  28609. ' );',
  28610. ' $r.addProperty(',
  28611. ' "B2",',
  28612. ' 17,',
  28613. ' rtl.boolean,',
  28614. ' "GetEnumBool",',
  28615. ' "FB",',
  28616. ' {',
  28617. ' index: $mod.TEnum.blue',
  28618. ' }',
  28619. ' );',
  28620. ' $r.addProperty(',
  28621. ' "I1",',
  28622. ' 19,',
  28623. ' rtl.boolean,',
  28624. ' "GetStrIntBool",',
  28625. ' "SetStrIntBool",',
  28626. ' {',
  28627. ' index: 2',
  28628. ' }',
  28629. ' );',
  28630. '});',
  28631. '']),
  28632. LinesToStr([ // $mod.$main
  28633. '']));
  28634. end;
  28635. procedure TTestModule.TestRTTI_StoredModifier;
  28636. begin
  28637. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28638. StartProgram(false);
  28639. Add([
  28640. 'const',
  28641. ' ConstB = true;',
  28642. 'type',
  28643. ' TObject = class',
  28644. ' private',
  28645. ' FB: boolean;',
  28646. ' function IsBStored: boolean; virtual; abstract;',
  28647. ' published',
  28648. ' property BoolA: boolean read FB stored true;',
  28649. ' property BoolB: boolean read FB stored false;',
  28650. ' property BoolC: boolean read FB stored FB;',
  28651. ' property BoolD: boolean read FB stored ConstB;',
  28652. ' property BoolE: boolean read FB stored IsBStored;',
  28653. ' end;',
  28654. 'begin']);
  28655. ConvertProgram;
  28656. CheckSource('TestRTTI_StoredModifier',
  28657. LinesToStr([ // statements
  28658. 'this.ConstB = true;',
  28659. 'rtl.createClass($mod, "TObject", null, function () {',
  28660. ' this.$init = function () {',
  28661. ' this.FB = false;',
  28662. ' };',
  28663. ' this.$final = function () {',
  28664. ' };',
  28665. ' var $r = this.$rtti;',
  28666. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  28667. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  28668. ' $r.addProperty(',
  28669. ' "BoolC",',
  28670. ' 8,',
  28671. ' rtl.boolean,',
  28672. ' "FB",',
  28673. ' "",',
  28674. ' {',
  28675. ' stored: "FB"',
  28676. ' }',
  28677. ' );',
  28678. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  28679. ' $r.addProperty(',
  28680. ' "BoolE",',
  28681. ' 12,',
  28682. ' rtl.boolean,',
  28683. ' "FB",',
  28684. ' "",',
  28685. ' {',
  28686. ' stored: "IsBStored"',
  28687. ' }',
  28688. ' );',
  28689. '});',
  28690. '']),
  28691. LinesToStr([ // $mod.$main
  28692. '']));
  28693. end;
  28694. procedure TTestModule.TestRTTI_DefaultValue;
  28695. begin
  28696. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28697. StartProgram(false);
  28698. Add([
  28699. 'type',
  28700. ' TEnum = (red, blue);',
  28701. 'const',
  28702. ' CB = true or false;',
  28703. ' CI = 1+2;',
  28704. 'type',
  28705. ' TObject = class',
  28706. ' FB: boolean;',
  28707. ' FI: longint;',
  28708. ' FE: TEnum;',
  28709. ' published',
  28710. ' property B1: boolean read FB default true;',
  28711. ' property B2: boolean read FB default CB;',
  28712. ' property B3: boolean read FB default test1.cb;',
  28713. ' property I1: longint read FI default 2;',
  28714. ' property I2: longint read FI default CI;',
  28715. ' property E1: TEnum read FE default red;',
  28716. ' property E2: TEnum read FE default TEnum.blue;',
  28717. ' end;',
  28718. 'begin']);
  28719. ConvertProgram;
  28720. CheckSource('TestRTTI_DefaultValue',
  28721. LinesToStr([ // statements
  28722. 'this.TEnum = {',
  28723. ' "0": "red",',
  28724. ' red: 0,',
  28725. ' "1": "blue",',
  28726. ' blue: 1',
  28727. '};',
  28728. '$mod.$rtti.$Enum("TEnum", {',
  28729. ' minvalue: 0,',
  28730. ' maxvalue: 1,',
  28731. ' ordtype: 1,',
  28732. ' enumtype: this.TEnum',
  28733. '});',
  28734. 'this.CB = true || false;',
  28735. 'this.CI = 1 + 2;',
  28736. 'rtl.createClass($mod, "TObject", null, function () {',
  28737. ' this.$init = function () {',
  28738. ' this.FB = false;',
  28739. ' this.FI = 0;',
  28740. ' this.FE = 0;',
  28741. ' };',
  28742. ' this.$final = function () {',
  28743. ' };',
  28744. ' var $r = this.$rtti;',
  28745. ' $r.addProperty(',
  28746. ' "B1",',
  28747. ' 0,',
  28748. ' rtl.boolean,',
  28749. ' "FB",',
  28750. ' "",',
  28751. ' {',
  28752. ' Default: true',
  28753. ' }',
  28754. ' );',
  28755. ' $r.addProperty(',
  28756. ' "B2",',
  28757. ' 0,',
  28758. ' rtl.boolean,',
  28759. ' "FB",',
  28760. ' "",',
  28761. ' {',
  28762. ' Default: true',
  28763. ' }',
  28764. ' );',
  28765. ' $r.addProperty(',
  28766. ' "B3",',
  28767. ' 0,',
  28768. ' rtl.boolean,',
  28769. ' "FB",',
  28770. ' "",',
  28771. ' {',
  28772. ' Default: true',
  28773. ' }',
  28774. ' );',
  28775. ' $r.addProperty(',
  28776. ' "I1",',
  28777. ' 0,',
  28778. ' rtl.longint,',
  28779. ' "FI",',
  28780. ' "",',
  28781. ' {',
  28782. ' Default: 2',
  28783. ' }',
  28784. ' );',
  28785. ' $r.addProperty(',
  28786. ' "I2",',
  28787. ' 0,',
  28788. ' rtl.longint,',
  28789. ' "FI",',
  28790. ' "",',
  28791. ' {',
  28792. ' Default: 3',
  28793. ' }',
  28794. ' );',
  28795. ' $r.addProperty(',
  28796. ' "E1",',
  28797. ' 0,',
  28798. ' $mod.$rtti["TEnum"],',
  28799. ' "FE",',
  28800. ' "",',
  28801. ' {',
  28802. ' Default: $mod.TEnum.red',
  28803. ' }',
  28804. ' );',
  28805. ' $r.addProperty(',
  28806. ' "E2",',
  28807. ' 0,',
  28808. ' $mod.$rtti["TEnum"],',
  28809. ' "FE",',
  28810. ' "",',
  28811. ' {',
  28812. ' Default: $mod.TEnum.blue',
  28813. ' }',
  28814. ' );',
  28815. '});',
  28816. '']),
  28817. LinesToStr([ // $mod.$main
  28818. '']));
  28819. end;
  28820. procedure TTestModule.TestRTTI_DefaultValueSet;
  28821. begin
  28822. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28823. StartProgram(false);
  28824. Add([
  28825. 'type',
  28826. ' TEnum = (red, blue);',
  28827. ' TSet = set of TEnum;',
  28828. 'const',
  28829. ' CSet = [red,blue];',
  28830. 'type',
  28831. ' TObject = class',
  28832. ' FSet: TSet;',
  28833. ' published',
  28834. ' property Set1: TSet read FSet default [];',
  28835. ' property Set2: TSet read FSet default [red];',
  28836. ' property Set3: TSet read FSet default [red,blue];',
  28837. ' property Set4: TSet read FSet default CSet;',
  28838. ' end;',
  28839. 'begin']);
  28840. ConvertProgram;
  28841. CheckSource('TestRTTI_DefaultValueSet',
  28842. LinesToStr([ // statements
  28843. 'this.TEnum = {',
  28844. ' "0": "red",',
  28845. ' red: 0,',
  28846. ' "1": "blue",',
  28847. ' blue: 1',
  28848. '};',
  28849. '$mod.$rtti.$Enum("TEnum", {',
  28850. ' minvalue: 0,',
  28851. ' maxvalue: 1,',
  28852. ' ordtype: 1,',
  28853. ' enumtype: this.TEnum',
  28854. '});',
  28855. '$mod.$rtti.$Set("TSet", {',
  28856. ' comptype: $mod.$rtti["TEnum"]',
  28857. '});',
  28858. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  28859. 'rtl.createClass($mod, "TObject", null, function () {',
  28860. ' this.$init = function () {',
  28861. ' this.FSet = {};',
  28862. ' };',
  28863. ' this.$final = function () {',
  28864. ' this.FSet = undefined;',
  28865. ' };',
  28866. ' var $r = this.$rtti;',
  28867. ' $r.addProperty(',
  28868. ' "Set1",',
  28869. ' 0,',
  28870. ' $mod.$rtti["TSet"],',
  28871. ' "FSet",',
  28872. ' "",',
  28873. ' {',
  28874. ' Default: {}',
  28875. ' }',
  28876. ' );',
  28877. ' $r.addProperty(',
  28878. ' "Set2",',
  28879. ' 0,',
  28880. ' $mod.$rtti["TSet"],',
  28881. ' "FSet",',
  28882. ' "",',
  28883. ' {',
  28884. ' Default: rtl.createSet($mod.TEnum.red)',
  28885. ' }',
  28886. ' );',
  28887. ' $r.addProperty(',
  28888. ' "Set3",',
  28889. ' 0,',
  28890. ' $mod.$rtti["TSet"],',
  28891. ' "FSet",',
  28892. ' "",',
  28893. ' {',
  28894. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  28895. ' }',
  28896. ' );',
  28897. ' $r.addProperty(',
  28898. ' "Set4",',
  28899. ' 0,',
  28900. ' $mod.$rtti["TSet"],',
  28901. ' "FSet",',
  28902. ' "",',
  28903. ' {',
  28904. ' Default: $mod.CSet',
  28905. ' }',
  28906. ' );',
  28907. '});',
  28908. '']),
  28909. LinesToStr([ // $mod.$main
  28910. '']));
  28911. end;
  28912. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  28913. begin
  28914. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28915. StartProgram(false);
  28916. Add([
  28917. 'type',
  28918. ' TRg = -1..1;',
  28919. 'const',
  28920. ' l = low(TRg);',
  28921. ' h = high(TRg);',
  28922. 'type',
  28923. ' TObject = class',
  28924. ' FV: TRg;',
  28925. ' published',
  28926. ' property V1: TRg read FV default -1;',
  28927. ' end;',
  28928. 'begin']);
  28929. ConvertProgram;
  28930. CheckSource('TestRTTI_DefaultValueRangeType',
  28931. LinesToStr([ // statements
  28932. '$mod.$rtti.$Int("TRg", {',
  28933. ' minvalue: -1,',
  28934. ' maxvalue: 1,',
  28935. ' ordtype: 0',
  28936. '});',
  28937. 'this.l = -1;',
  28938. 'this.h = 1;',
  28939. 'rtl.createClass($mod, "TObject", null, function () {',
  28940. ' this.$init = function () {',
  28941. ' this.FV = 0;',
  28942. ' };',
  28943. ' this.$final = function () {',
  28944. ' };',
  28945. ' var $r = this.$rtti;',
  28946. ' $r.addProperty(',
  28947. ' "V1",',
  28948. ' 0,',
  28949. ' $mod.$rtti["TRg"],',
  28950. ' "FV",',
  28951. ' "",',
  28952. ' {',
  28953. ' Default: -1',
  28954. ' }',
  28955. ' );',
  28956. '});',
  28957. '']),
  28958. LinesToStr([ // $mod.$main
  28959. '']));
  28960. end;
  28961. procedure TTestModule.TestRTTI_DefaultValueInherit;
  28962. begin
  28963. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28964. StartProgram(false);
  28965. Add([
  28966. 'type',
  28967. ' TObject = class',
  28968. ' FA, FB: byte;',
  28969. ' property A: byte read FA default 1;',
  28970. ' property B: byte read FB default 2;',
  28971. ' end;',
  28972. ' TBird = class',
  28973. ' published',
  28974. ' property A;',
  28975. ' property B nodefault;',
  28976. ' end;',
  28977. 'begin']);
  28978. ConvertProgram;
  28979. CheckSource('TestRTTI_DefaultValueInherit',
  28980. LinesToStr([ // statements
  28981. 'rtl.createClass($mod, "TObject", null, function () {',
  28982. ' this.$init = function () {',
  28983. ' this.FA = 0;',
  28984. ' this.FB = 0;',
  28985. ' };',
  28986. ' this.$final = function () {',
  28987. ' };',
  28988. '});',
  28989. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28990. ' var $r = this.$rtti;',
  28991. ' $r.addProperty(',
  28992. ' "A",',
  28993. ' 0,',
  28994. ' rtl.byte,',
  28995. ' "FA",',
  28996. ' "",',
  28997. ' {',
  28998. ' Default: 1',
  28999. ' }',
  29000. ' );',
  29001. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29002. '});',
  29003. '']),
  29004. LinesToStr([ // $mod.$main
  29005. '']));
  29006. end;
  29007. procedure TTestModule.TestRTTI_OverrideMethod;
  29008. begin
  29009. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29010. StartProgram(false);
  29011. Add('type');
  29012. Add(' TObject = class');
  29013. Add(' published');
  29014. Add(' procedure DoIt; virtual; abstract;');
  29015. Add(' end;');
  29016. Add(' TSky = class');
  29017. Add(' published');
  29018. Add(' procedure DoIt; override;');
  29019. Add(' end;');
  29020. Add('procedure TSky.DoIt; begin end;');
  29021. Add('begin');
  29022. ConvertProgram;
  29023. CheckSource('TestRTTI_OverrideMethod',
  29024. LinesToStr([ // statements
  29025. 'rtl.createClass($mod, "TObject", null, function () {',
  29026. ' this.$init = function () {',
  29027. ' };',
  29028. ' this.$final = function () {',
  29029. ' };',
  29030. ' var $r = this.$rtti;',
  29031. ' $r.addMethod("DoIt", 0, null);',
  29032. '});',
  29033. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  29034. ' this.DoIt = function () {',
  29035. ' };',
  29036. '});',
  29037. '']),
  29038. LinesToStr([ // $mod.$main
  29039. '']));
  29040. end;
  29041. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29042. begin
  29043. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29044. StartProgram(false);
  29045. Add([
  29046. 'type',
  29047. ' TObject = class',
  29048. ' published',
  29049. ' procedure DoIt;',
  29050. ' end;',
  29051. ' TSky = class',
  29052. ' published',
  29053. ' procedure DoIt; reintroduce;',
  29054. ' end;',
  29055. 'procedure TObject.DoIt; begin end;',
  29056. 'procedure TSky.DoIt;',
  29057. 'begin',
  29058. ' inherited DoIt;',
  29059. 'end;',
  29060. 'begin']);
  29061. ConvertProgram;
  29062. CheckSource('TestRTTI_ReintroduceMethod',
  29063. LinesToStr([ // statements
  29064. 'rtl.createClass($mod, "TObject", null, function () {',
  29065. ' this.$init = function () {',
  29066. ' };',
  29067. ' this.$final = function () {',
  29068. ' };',
  29069. ' this.DoIt = function () {',
  29070. ' };',
  29071. ' var $r = this.$rtti;',
  29072. ' $r.addMethod("DoIt", 0, null);',
  29073. '});',
  29074. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  29075. ' this.DoIt = function () {',
  29076. ' $mod.TObject.DoIt.call(this);',
  29077. ' };',
  29078. ' var $r = this.$rtti;',
  29079. ' $r.addMethod("DoIt", 0, null);',
  29080. '});',
  29081. '']),
  29082. LinesToStr([ // $mod.$main
  29083. '']));
  29084. end;
  29085. procedure TTestModule.TestRTTI_OverloadProperty;
  29086. begin
  29087. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29088. StartProgram(false);
  29089. Add('type');
  29090. Add(' TObject = class');
  29091. Add(' protected');
  29092. Add(' FFlag: longint;');
  29093. Add(' published');
  29094. Add(' property Flag: longint read fflag;');
  29095. Add(' end;');
  29096. Add(' TSky = class');
  29097. Add(' published');
  29098. Add(' property FLAG: longint write fflag;');
  29099. Add(' end;');
  29100. Add('begin');
  29101. ConvertProgram;
  29102. CheckSource('TestRTTI_OverrideMethod',
  29103. LinesToStr([ // statements
  29104. 'rtl.createClass($mod, "TObject", null, function () {',
  29105. ' this.$init = function () {',
  29106. ' this.FFlag = 0;',
  29107. ' };',
  29108. ' this.$final = function () {',
  29109. ' };',
  29110. ' var $r = this.$rtti;',
  29111. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29112. '});',
  29113. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  29114. ' var $r = this.$rtti;',
  29115. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29116. '});',
  29117. '']),
  29118. LinesToStr([ // $mod.$main
  29119. '']));
  29120. end;
  29121. procedure TTestModule.TestRTTI_ClassForward;
  29122. begin
  29123. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29124. StartProgram(false);
  29125. Add('type');
  29126. Add(' TObject = class end;');
  29127. Add(' tbridge = class;');
  29128. Add(' TProc = function: tbridge;');
  29129. Add(' TOger = class');
  29130. Add(' published');
  29131. Add(' FBridge: tbridge;');
  29132. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29133. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29134. Add(' end;');
  29135. Add(' TBridge = class');
  29136. Add(' FOger: toger;');
  29137. Add(' end;');
  29138. Add('var p: Pointer;');
  29139. Add(' b: tbridge;');
  29140. Add('begin');
  29141. Add(' p:=typeinfo(tbridge);');
  29142. Add(' p:=typeinfo(b);');
  29143. ConvertProgram;
  29144. CheckSource('TestRTTI_ClassForward',
  29145. LinesToStr([ // statements
  29146. 'rtl.createClass($mod, "TObject", null, function () {',
  29147. ' this.$init = function () {',
  29148. ' };',
  29149. ' this.$final = function () {',
  29150. ' };',
  29151. '});',
  29152. '$mod.$rtti.$Class("TBridge");',
  29153. '$mod.$rtti.$ProcVar("TProc", {',
  29154. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  29155. '});',
  29156. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  29157. ' this.$init = function () {',
  29158. ' $mod.TObject.$init.call(this);',
  29159. ' this.FBridge = null;',
  29160. ' };',
  29161. ' this.$final = function () {',
  29162. ' this.FBridge = undefined;',
  29163. ' $mod.TObject.$final.call(this);',
  29164. ' };',
  29165. ' var $r = this.$rtti;',
  29166. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29167. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29168. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29169. '});',
  29170. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  29171. ' this.$init = function () {',
  29172. ' $mod.TObject.$init.call(this);',
  29173. ' this.FOger = null;',
  29174. ' };',
  29175. ' this.$final = function () {',
  29176. ' this.FOger = undefined;',
  29177. ' $mod.TObject.$final.call(this);',
  29178. ' };',
  29179. '});',
  29180. 'this.p = null;',
  29181. 'this.b = null;',
  29182. '']),
  29183. LinesToStr([ // $mod.$main
  29184. '$mod.p = $mod.$rtti["TBridge"];',
  29185. '$mod.p = $mod.b.$rtti;',
  29186. '']));
  29187. end;
  29188. procedure TTestModule.TestRTTI_ClassOf;
  29189. begin
  29190. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29191. StartProgram(false);
  29192. Add('type');
  29193. Add(' TClass = class of tobject;');
  29194. Add(' TProcA = function: TClass;');
  29195. Add(' TObject = class');
  29196. Add(' published');
  29197. Add(' C: tclass;');
  29198. Add(' end;');
  29199. Add(' tfox = class;');
  29200. Add(' TBird = class end;');
  29201. Add(' TBirds = class of tbird;');
  29202. Add(' TFox = class end;');
  29203. Add(' TFoxes = class of tfox;');
  29204. Add(' TCows = class of TCow;');
  29205. Add(' TCow = class;');
  29206. Add(' TCow = class end;');
  29207. Add('begin');
  29208. ConvertProgram;
  29209. CheckSource('TestRTTI_ClassOf',
  29210. LinesToStr([ // statements
  29211. '$mod.$rtti.$Class("TObject");',
  29212. '$mod.$rtti.$ClassRef("TClass", {',
  29213. ' instancetype: $mod.$rtti["TObject"]',
  29214. '});',
  29215. '$mod.$rtti.$ProcVar("TProcA", {',
  29216. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  29217. '});',
  29218. 'rtl.createClass($mod, "TObject", null, function () {',
  29219. ' this.$init = function () {',
  29220. ' this.C = null;',
  29221. ' };',
  29222. ' this.$final = function () {',
  29223. ' this.C = undefined;',
  29224. ' };',
  29225. ' var $r = this.$rtti;',
  29226. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29227. '});',
  29228. '$mod.$rtti.$Class("TFox");',
  29229. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  29230. '});',
  29231. '$mod.$rtti.$ClassRef("TBirds", {',
  29232. ' instancetype: $mod.$rtti["TBird"]',
  29233. '});',
  29234. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  29235. '});',
  29236. '$mod.$rtti.$ClassRef("TFoxes", {',
  29237. ' instancetype: $mod.$rtti["TFox"]',
  29238. '});',
  29239. '$mod.$rtti.$Class("TCow");',
  29240. '$mod.$rtti.$ClassRef("TCows", {',
  29241. ' instancetype: $mod.$rtti["TCow"]',
  29242. '});',
  29243. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  29244. '});',
  29245. '']),
  29246. LinesToStr([ // $mod.$main
  29247. '']));
  29248. end;
  29249. procedure TTestModule.TestRTTI_Record;
  29250. begin
  29251. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29252. StartProgram(false);
  29253. Add('type');
  29254. Add(' integer = longint;');
  29255. Add(' TPoint = record');
  29256. Add(' x,y: integer;');
  29257. Add(' end;');
  29258. Add('var p: pointer;');
  29259. Add(' r: tpoint;');
  29260. Add('begin');
  29261. Add(' p:=typeinfo(tpoint);');
  29262. Add(' p:=typeinfo(r);');
  29263. Add(' p:=typeinfo(r.x);');
  29264. ConvertProgram;
  29265. CheckSource('TestRTTI_Record',
  29266. LinesToStr([ // statements
  29267. 'rtl.recNewT($mod, "TPoint", function () {',
  29268. ' this.x = 0;',
  29269. ' this.y = 0;',
  29270. ' this.$eq = function (b) {',
  29271. ' return (this.x === b.x) && (this.y === b.y);',
  29272. ' };',
  29273. ' this.$assign = function (s) {',
  29274. ' this.x = s.x;',
  29275. ' this.y = s.y;',
  29276. ' return this;',
  29277. ' };',
  29278. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29279. ' $r.addField("x", rtl.longint);',
  29280. ' $r.addField("y", rtl.longint);',
  29281. '});',
  29282. 'this.p = null;',
  29283. 'this.r = $mod.TPoint.$new();',
  29284. '']),
  29285. LinesToStr([ // $mod.$main
  29286. '$mod.p = $mod.$rtti["TPoint"];',
  29287. '$mod.p = $mod.$rtti["TPoint"];',
  29288. '$mod.p = rtl.longint;',
  29289. '']));
  29290. end;
  29291. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29292. begin
  29293. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29294. StartProgram(false);
  29295. Add('type');
  29296. Add(' TFloatRec = record');
  29297. Add(' c,d: array of char;');
  29298. // Add(' i: array of array of longint;');
  29299. Add(' end;');
  29300. Add('var p: pointer;');
  29301. Add(' r: tfloatrec;');
  29302. Add('begin');
  29303. Add(' p:=typeinfo(tfloatrec);');
  29304. Add(' p:=typeinfo(r);');
  29305. Add(' p:=typeinfo(r.d);');
  29306. ConvertProgram;
  29307. CheckSource('TestRTTI_Record',
  29308. LinesToStr([ // statements
  29309. 'rtl.recNewT($mod, "TFloatRec", function () {',
  29310. ' this.$new = function () {',
  29311. ' var r = Object.create(this);',
  29312. ' r.c = [];',
  29313. ' r.d = [];',
  29314. ' return r;',
  29315. ' };',
  29316. ' this.$eq = function (b) {',
  29317. ' return (this.c === b.c) && (this.d === b.d);',
  29318. ' };',
  29319. ' this.$assign = function (s) {',
  29320. ' this.c = rtl.arrayRef(s.c);',
  29321. ' this.d = rtl.arrayRef(s.d);',
  29322. ' return this;',
  29323. ' };',
  29324. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29325. ' eltype: rtl.char',
  29326. ' });',
  29327. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29328. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29329. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29330. '});',
  29331. 'this.p = null;',
  29332. 'this.r = $mod.TFloatRec.$new();',
  29333. '']),
  29334. LinesToStr([ // $mod.$main
  29335. '$mod.p = $mod.$rtti["TFloatRec"];',
  29336. '$mod.p = $mod.$rtti["TFloatRec"];',
  29337. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29338. '']));
  29339. end;
  29340. procedure TTestModule.TestRTTI_LocalTypes;
  29341. begin
  29342. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29343. StartProgram(false);
  29344. Add([
  29345. 'procedure DoIt;',
  29346. 'type',
  29347. ' integer = longint;',
  29348. ' TPoint = record',
  29349. ' x,y: integer;',
  29350. ' end;',
  29351. 'var p: TPoint;',
  29352. 'begin',
  29353. 'end;',
  29354. 'begin']);
  29355. ConvertProgram;
  29356. CheckSource('TestRTTI_LocalTypes',
  29357. LinesToStr([ // statements
  29358. 'var TPoint = rtl.recNewT(null, "", function () {',
  29359. ' this.x = 0;',
  29360. ' this.y = 0;',
  29361. ' this.$eq = function (b) {',
  29362. ' return (this.x === b.x) && (this.y === b.y);',
  29363. ' };',
  29364. ' this.$assign = function (s) {',
  29365. ' this.x = s.x;',
  29366. ' this.y = s.y;',
  29367. ' return this;',
  29368. ' };',
  29369. '});',
  29370. 'this.DoIt = function () {',
  29371. ' var p = TPoint.$new();',
  29372. '};',
  29373. '']),
  29374. LinesToStr([ // $mod.$main
  29375. '']));
  29376. end;
  29377. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29378. begin
  29379. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29380. StartProgram(false);
  29381. Add([
  29382. 'type',
  29383. ' TCaption = string;',
  29384. ' TYesNo = boolean;',
  29385. ' TLetter = char;',
  29386. ' TFloat = double;',
  29387. ' TPtr = pointer;',
  29388. ' TShortInt = shortint;',
  29389. ' TByte = byte;',
  29390. ' TSmallInt = smallint;',
  29391. ' TWord = word;',
  29392. ' TInt32 = longint;',
  29393. ' TDWord = longword;',
  29394. ' TValue = jsvalue;',
  29395. 'var p: TPtr;',
  29396. 'begin',
  29397. ' p:=typeinfo(string);',
  29398. ' p:=typeinfo(tcaption);',
  29399. ' p:=typeinfo(boolean);',
  29400. ' p:=typeinfo(tyesno);',
  29401. ' p:=typeinfo(char);',
  29402. ' p:=typeinfo(tletter);',
  29403. ' p:=typeinfo(double);',
  29404. ' p:=typeinfo(tfloat);',
  29405. ' p:=typeinfo(pointer);',
  29406. ' p:=typeinfo(tptr);',
  29407. ' p:=typeinfo(shortint);',
  29408. ' p:=typeinfo(tshortint);',
  29409. ' p:=typeinfo(byte);',
  29410. ' p:=typeinfo(tbyte);',
  29411. ' p:=typeinfo(smallint);',
  29412. ' p:=typeinfo(tsmallint);',
  29413. ' p:=typeinfo(word);',
  29414. ' p:=typeinfo(tword);',
  29415. ' p:=typeinfo(longword);',
  29416. ' p:=typeinfo(tdword);',
  29417. ' p:=typeinfo(jsvalue);',
  29418. ' p:=typeinfo(tvalue);',
  29419. '']);
  29420. ConvertProgram;
  29421. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  29422. LinesToStr([ // statements
  29423. 'this.p = null;',
  29424. '']),
  29425. LinesToStr([ // $mod.$main
  29426. '$mod.p = rtl.string;',
  29427. '$mod.p = rtl.string;',
  29428. '$mod.p = rtl.boolean;',
  29429. '$mod.p = rtl.boolean;',
  29430. '$mod.p = rtl.char;',
  29431. '$mod.p = rtl.char;',
  29432. '$mod.p = rtl.double;',
  29433. '$mod.p = rtl.double;',
  29434. '$mod.p = rtl.pointer;',
  29435. '$mod.p = rtl.pointer;',
  29436. '$mod.p = rtl.shortint;',
  29437. '$mod.p = rtl.shortint;',
  29438. '$mod.p = rtl.byte;',
  29439. '$mod.p = rtl.byte;',
  29440. '$mod.p = rtl.smallint;',
  29441. '$mod.p = rtl.smallint;',
  29442. '$mod.p = rtl.word;',
  29443. '$mod.p = rtl.word;',
  29444. '$mod.p = rtl.longword;',
  29445. '$mod.p = rtl.longword;',
  29446. '$mod.p = rtl.jsvalue;',
  29447. '$mod.p = rtl.jsvalue;',
  29448. '']));
  29449. end;
  29450. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  29451. begin
  29452. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29453. StartProgram(false);
  29454. Add([
  29455. 'type',
  29456. ' TCaption = type string;',
  29457. ' TYesNo = type boolean;',
  29458. ' TLetter = type char;',
  29459. ' TFloat = type double;',
  29460. ' TPtr = type pointer;',
  29461. ' TShortInt = type shortint;',
  29462. ' TByte = type byte;',
  29463. ' TSmallInt = type smallint;',
  29464. ' TWord = type word;',
  29465. ' TInt32 = type longint;',
  29466. ' TDWord = type longword;',
  29467. ' TValue = type jsvalue;',
  29468. ' TAliasValue = type TValue;',
  29469. 'var',
  29470. ' p: TPtr;',
  29471. ' a: TAliasValue;',
  29472. 'begin',
  29473. ' p:=typeinfo(tcaption);',
  29474. ' p:=typeinfo(tyesno);',
  29475. ' p:=typeinfo(tletter);',
  29476. ' p:=typeinfo(tfloat);',
  29477. ' p:=typeinfo(tptr);',
  29478. ' p:=typeinfo(tshortint);',
  29479. ' p:=typeinfo(tbyte);',
  29480. ' p:=typeinfo(tsmallint);',
  29481. ' p:=typeinfo(tword);',
  29482. ' p:=typeinfo(tdword);',
  29483. ' p:=typeinfo(tvalue);',
  29484. ' p:=typeinfo(taliasvalue);',
  29485. ' p:=typeinfo(a);',
  29486. '']);
  29487. ConvertProgram;
  29488. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  29489. LinesToStr([ // statements
  29490. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  29491. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  29492. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  29493. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  29494. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  29495. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  29496. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  29497. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  29498. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  29499. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  29500. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  29501. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  29502. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  29503. 'this.p = null;',
  29504. 'this.a = undefined;',
  29505. '']),
  29506. LinesToStr([ // $mod.$main
  29507. '$mod.p = $mod.$rtti["TCaption"];',
  29508. '$mod.p = $mod.$rtti["TYesNo"];',
  29509. '$mod.p = $mod.$rtti["TLetter"];',
  29510. '$mod.p = $mod.$rtti["TFloat"];',
  29511. '$mod.p = $mod.$rtti["TPtr"];',
  29512. '$mod.p = $mod.$rtti["TShortInt"];',
  29513. '$mod.p = $mod.$rtti["TByte"];',
  29514. '$mod.p = $mod.$rtti["TSmallInt"];',
  29515. '$mod.p = $mod.$rtti["TWord"];',
  29516. '$mod.p = $mod.$rtti["TDWord"];',
  29517. '$mod.p = $mod.$rtti["TValue"];',
  29518. '$mod.p = $mod.$rtti["TAliasValue"];',
  29519. '$mod.p = $mod.$rtti["TAliasValue"];',
  29520. '']));
  29521. end;
  29522. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  29523. begin
  29524. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29525. StartProgram(false);
  29526. Add('procedure DoIt;');
  29527. Add('type');
  29528. Add(' integer = longint;');
  29529. Add(' TPoint = record');
  29530. Add(' x,y: integer;');
  29531. Add(' end;');
  29532. Add('var p: pointer;');
  29533. Add('begin');
  29534. Add(' p:=typeinfo(tpoint);');
  29535. Add('end;');
  29536. Add('begin');
  29537. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  29538. ConvertProgram;
  29539. end;
  29540. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  29541. begin
  29542. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29543. StartProgram(true,[supTypeInfo]);
  29544. Add([
  29545. '{$modeswitch externalclass}',
  29546. 'type',
  29547. ' TFlag = (up,down);',
  29548. ' TFlags = set of TFlag;',
  29549. 'var',
  29550. ' ti: TTypeInfo;',
  29551. ' tiInt: TTypeInfoInteger;',
  29552. ' tiEnum: TTypeInfoEnum;',
  29553. ' tiSet: TTypeInfoSet;',
  29554. 'begin',
  29555. ' ti:=typeinfo(string);',
  29556. ' ti:=typeinfo(boolean);',
  29557. ' ti:=typeinfo(char);',
  29558. ' ti:=typeinfo(double);',
  29559. ' tiInt:=typeinfo(shortint);',
  29560. ' tiInt:=typeinfo(byte);',
  29561. ' tiInt:=typeinfo(smallint);',
  29562. ' tiInt:=typeinfo(word);',
  29563. ' tiInt:=typeinfo(longint);',
  29564. ' tiInt:=typeinfo(longword);',
  29565. ' ti:=typeinfo(jsvalue);',
  29566. ' tiEnum:=typeinfo(tflag);',
  29567. ' tiSet:=typeinfo(tflags);']);
  29568. ConvertProgram;
  29569. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  29570. LinesToStr([ // statements
  29571. 'this.TFlag = {',
  29572. ' "0": "up",',
  29573. ' up: 0,',
  29574. ' "1": "down",',
  29575. ' down: 1',
  29576. '};',
  29577. '$mod.$rtti.$Enum("TFlag", {',
  29578. ' minvalue: 0,',
  29579. ' maxvalue: 1,',
  29580. ' ordtype: 1,',
  29581. ' enumtype: this.TFlag',
  29582. '});',
  29583. '$mod.$rtti.$Set("TFlags", {',
  29584. ' comptype: $mod.$rtti["TFlag"]',
  29585. '});',
  29586. 'this.ti = null;',
  29587. 'this.tiInt = null;',
  29588. 'this.tiEnum = null;',
  29589. 'this.tiSet = null;',
  29590. '']),
  29591. LinesToStr([ // $mod.$main
  29592. '$mod.ti = rtl.string;',
  29593. '$mod.ti = rtl.boolean;',
  29594. '$mod.ti = rtl.char;',
  29595. '$mod.ti = rtl.double;',
  29596. '$mod.tiInt = rtl.shortint;',
  29597. '$mod.tiInt = rtl.byte;',
  29598. '$mod.tiInt = rtl.smallint;',
  29599. '$mod.tiInt = rtl.word;',
  29600. '$mod.tiInt = rtl.longint;',
  29601. '$mod.tiInt = rtl.longword;',
  29602. '$mod.ti = rtl.jsvalue;',
  29603. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  29604. '$mod.tiSet = $mod.$rtti["TFlags"];',
  29605. '']));
  29606. end;
  29607. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  29608. begin
  29609. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29610. StartProgram(true,[supTypeInfo]);
  29611. Add('{$modeswitch externalclass}');
  29612. Add('type');
  29613. Add(' TStaticArr = array[boolean] of string;');
  29614. Add(' TDynArr = array of string;');
  29615. Add(' TProc = procedure;');
  29616. Add(' TMethod = procedure of object;');
  29617. Add('var');
  29618. Add(' StaticArray: TStaticArr;');
  29619. Add(' tiStaticArray: TTypeInfoStaticArray;');
  29620. Add(' DynArray: TDynArr;');
  29621. Add(' tiDynArray: TTypeInfoDynArray;');
  29622. Add(' ProcVar: TProc;');
  29623. Add(' tiProcVar: TTypeInfoProcVar;');
  29624. Add(' MethodVar: TMethod;');
  29625. Add(' tiMethodVar: TTypeInfoMethodVar;');
  29626. Add('begin');
  29627. Add(' tiStaticArray:=typeinfo(StaticArray);');
  29628. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  29629. Add(' tiDynArray:=typeinfo(DynArray);');
  29630. Add(' tiDynArray:=typeinfo(TDynArr);');
  29631. Add(' tiProcVar:=typeinfo(ProcVar);');
  29632. Add(' tiProcVar:=typeinfo(TProc);');
  29633. Add(' tiMethodVar:=typeinfo(MethodVar);');
  29634. Add(' tiMethodVar:=typeinfo(TMethod);');
  29635. ConvertProgram;
  29636. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  29637. LinesToStr([ // statements
  29638. '$mod.$rtti.$StaticArray("TStaticArr", {',
  29639. ' dims: [2],',
  29640. ' eltype: rtl.string',
  29641. '});',
  29642. '$mod.$rtti.$DynArray("TDynArr", {',
  29643. ' eltype: rtl.string',
  29644. '});',
  29645. '$mod.$rtti.$ProcVar("TProc", {',
  29646. ' procsig: rtl.newTIProcSig(null)',
  29647. '});',
  29648. '$mod.$rtti.$MethodVar("TMethod", {',
  29649. ' procsig: rtl.newTIProcSig(null),',
  29650. ' methodkind: 0',
  29651. '});',
  29652. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  29653. 'this.tiStaticArray = null;',
  29654. 'this.DynArray = [];',
  29655. 'this.tiDynArray = null;',
  29656. 'this.ProcVar = null;',
  29657. 'this.tiProcVar = null;',
  29658. 'this.MethodVar = null;',
  29659. 'this.tiMethodVar = null;',
  29660. '']),
  29661. LinesToStr([ // $mod.$main
  29662. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29663. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29664. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29665. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29666. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29667. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29668. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29669. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29670. '']));
  29671. end;
  29672. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  29673. begin
  29674. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29675. StartProgram(true,[supTypeInfo]);
  29676. Add('{$modeswitch externalclass}');
  29677. Add('type');
  29678. Add(' TRec = record end;');
  29679. // ToDo: ^PRec
  29680. Add(' TObject = class end;');
  29681. Add(' TClass = class of tobject;');
  29682. Add('var');
  29683. Add(' Rec: trec;');
  29684. Add(' tiRecord: ttypeinforecord;');
  29685. Add(' Obj: tobject;');
  29686. Add(' tiClass: ttypeinfoclass;');
  29687. Add(' aClass: tclass;');
  29688. Add(' tiClassRef: ttypeinfoclassref;');
  29689. // ToDo: ^PRec
  29690. Add(' tiPointer: ttypeinfopointer;');
  29691. Add('begin');
  29692. Add(' tirecord:=typeinfo(trec);');
  29693. Add(' tirecord:=typeinfo(trec);');
  29694. Add(' ticlass:=typeinfo(obj);');
  29695. Add(' ticlass:=typeinfo(tobject);');
  29696. Add(' ticlass:=typeinfo(aclass);');
  29697. Add(' ticlassref:=typeinfo(tclass);');
  29698. ConvertProgram;
  29699. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  29700. LinesToStr([ // statements
  29701. 'rtl.recNewT($mod, "TRec", function () {',
  29702. ' this.$eq = function (b) {',
  29703. ' return true;',
  29704. ' };',
  29705. ' this.$assign = function (s) {',
  29706. ' return this;',
  29707. ' };',
  29708. ' $mod.$rtti.$Record("TRec", {});',
  29709. '});',
  29710. 'rtl.createClass($mod, "TObject", null, function () {',
  29711. ' this.$init = function () {',
  29712. ' };',
  29713. ' this.$final = function () {',
  29714. ' };',
  29715. '});',
  29716. '$mod.$rtti.$ClassRef("TClass", {',
  29717. ' instancetype: $mod.$rtti["TObject"]',
  29718. '});',
  29719. 'this.Rec = $mod.TRec.$new();',
  29720. 'this.tiRecord = null;',
  29721. 'this.Obj = null;',
  29722. 'this.tiClass = null;',
  29723. 'this.aClass = null;',
  29724. 'this.tiClassRef = null;',
  29725. 'this.tiPointer = null;',
  29726. '']),
  29727. LinesToStr([ // $mod.$main
  29728. '$mod.tiRecord = $mod.$rtti["TRec"];',
  29729. '$mod.tiRecord = $mod.$rtti["TRec"];',
  29730. '$mod.tiClass = $mod.Obj.$rtti;',
  29731. '$mod.tiClass = $mod.$rtti["TObject"];',
  29732. '$mod.tiClass = $mod.aClass.$rtti;',
  29733. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  29734. '']));
  29735. end;
  29736. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  29737. begin
  29738. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29739. StartProgram(true,[supTypeInfo]);
  29740. Add([
  29741. '{$modeswitch externalclass}',
  29742. 'type',
  29743. ' TClass = class of tobject;',
  29744. ' TObject = class',
  29745. ' function MyClass: TClass;',
  29746. ' class function ClassType: TClass;',
  29747. ' end;',
  29748. 'function TObject.MyClass: TClass;',
  29749. 'var t: TTypeInfoClass;',
  29750. 'begin',
  29751. ' t:=TypeInfo(Self);',
  29752. ' t:=TypeInfo(Result);',
  29753. ' t:=TypeInfo(TObject);',
  29754. 'end;',
  29755. 'class function TObject.ClassType: TClass;',
  29756. 'var t: TTypeInfoClass;',
  29757. 'begin',
  29758. ' t:=TypeInfo(Self);',
  29759. ' t:=TypeInfo(Result);',
  29760. 'end;',
  29761. 'var',
  29762. ' Obj: TObject;',
  29763. ' t: TTypeInfoClass;',
  29764. 'begin',
  29765. ' t:=TypeInfo(TObject.ClassType);',
  29766. ' t:=TypeInfo(Obj.ClassType);',
  29767. ' t:=TypeInfo(Obj.MyClass);',
  29768. '']);
  29769. ConvertProgram;
  29770. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  29771. LinesToStr([ // statements
  29772. '$mod.$rtti.$Class("TObject");',
  29773. '$mod.$rtti.$ClassRef("TClass", {',
  29774. ' instancetype: $mod.$rtti["TObject"]',
  29775. '});',
  29776. 'rtl.createClass($mod, "TObject", null, function () {',
  29777. ' this.$init = function () {',
  29778. ' };',
  29779. ' this.$final = function () {',
  29780. ' };',
  29781. ' this.MyClass = function () {',
  29782. ' var Result = null;',
  29783. ' var t = null;',
  29784. ' t = this.$rtti;',
  29785. ' t = Result.$rtti;',
  29786. ' t = $mod.$rtti["TObject"];',
  29787. ' return Result;',
  29788. ' };',
  29789. ' this.ClassType = function () {',
  29790. ' var Result = null;',
  29791. ' var t = null;',
  29792. ' t = this.$rtti;',
  29793. ' t = Result.$rtti;',
  29794. ' return Result;',
  29795. ' };',
  29796. '});',
  29797. 'this.Obj = null;',
  29798. 'this.t = null;',
  29799. '']),
  29800. LinesToStr([ // $mod.$main
  29801. '$mod.t = $mod.TObject.ClassType().$rtti;',
  29802. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  29803. '$mod.t = $mod.Obj.MyClass().$rtti;',
  29804. '']));
  29805. end;
  29806. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  29807. begin
  29808. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29809. AddModuleWithIntfImplSrc('typinfo.pas',
  29810. LinesToStr([
  29811. '{$modeswitch externalclass}',
  29812. 'type',
  29813. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  29814. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  29815. '']),
  29816. '');
  29817. AddModuleWithIntfImplSrc('unit2.pas',
  29818. LinesToStr([
  29819. 'uses typinfo;',
  29820. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  29821. 'procedure DoPtr(p: PTypeInfo);',
  29822. 'procedure DoInfo(t: TTypeInfo);',
  29823. 'procedure DoInt(t: TTypeInfoInteger);',
  29824. '']),
  29825. LinesToStr([
  29826. 'procedure DoPtr(p: PTypeInfo);',
  29827. 'begin end;',
  29828. 'procedure DoInfo(t: TTypeInfo);',
  29829. 'begin end;',
  29830. 'procedure DoInt(t: TTypeInfoInteger);',
  29831. 'begin end;',
  29832. '']));
  29833. StartUnit(true);
  29834. Add([
  29835. 'interface',
  29836. 'uses unit2;', // does not use unit typinfo
  29837. 'implementation',
  29838. 'var',
  29839. ' i: byte;',
  29840. ' p: pointer;',
  29841. ' t: PTypeInfo;',
  29842. 'initialization',
  29843. ' p:=typeinfo(i);',
  29844. ' t:=typeinfo(i);',
  29845. ' if p=t then ;',
  29846. ' if p=typeinfo(i) then ;',
  29847. ' if typeinfo(i)=p then ;',
  29848. ' if t=typeinfo(i) then ;',
  29849. ' if typeinfo(i)=t then ;',
  29850. ' DoPtr(p);',
  29851. ' DoPtr(t);',
  29852. ' DoPtr(typeinfo(i));',
  29853. ' DoInfo(p);',
  29854. ' DoInfo(t);',
  29855. ' DoInfo(typeinfo(i));',
  29856. ' DoInt(typeinfo(i));',
  29857. '']);
  29858. ConvertUnit;
  29859. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  29860. LinesToStr([ // statements
  29861. 'var $impl = $mod.$impl;',
  29862. '']),
  29863. LinesToStr([ // this.$init
  29864. '$impl.p = rtl.byte;',
  29865. '$impl.t = rtl.byte;',
  29866. 'if ($impl.p === $impl.t) ;',
  29867. 'if ($impl.p === rtl.byte) ;',
  29868. 'if (rtl.byte === $impl.p) ;',
  29869. 'if ($impl.t === rtl.byte) ;',
  29870. 'if (rtl.byte === $impl.t) ;',
  29871. 'pas.unit2.DoPtr($impl.p);',
  29872. 'pas.unit2.DoPtr($impl.t);',
  29873. 'pas.unit2.DoPtr(rtl.byte);',
  29874. 'pas.unit2.DoInfo($impl.p);',
  29875. 'pas.unit2.DoInfo($impl.t);',
  29876. 'pas.unit2.DoInfo(rtl.byte);',
  29877. 'pas.unit2.DoInt(rtl.byte);',
  29878. '']),
  29879. LinesToStr([ // implementation
  29880. '$impl.i = 0;',
  29881. '$impl.p = null;',
  29882. '$impl.t = null;',
  29883. '']) );
  29884. end;
  29885. procedure TTestModule.TestRTTI_Interface_Corba;
  29886. begin
  29887. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29888. StartProgram(true,[supTypeInfo]);
  29889. Add([
  29890. '{$interfaces corba}',
  29891. '{$modeswitch externalclass}',
  29892. 'type',
  29893. ' IUnknown = interface',
  29894. ' end;',
  29895. ' IBird = interface',
  29896. ' function GetItem: longint;',
  29897. ' procedure SetItem(Value: longint);',
  29898. ' property Item: longint read GetItem write SetItem;',
  29899. ' end;',
  29900. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  29901. 'var',
  29902. ' i: IBird;',
  29903. ' t: TTypeInfoInterface;',
  29904. 'begin',
  29905. ' t:=TypeInfo(IBird);',
  29906. ' t:=TypeInfo(i);',
  29907. ' DoIt(t);',
  29908. ' DoIt(TypeInfo(IBird));',
  29909. '']);
  29910. ConvertProgram;
  29911. CheckSource('TestRTTI_Interface_Corba',
  29912. LinesToStr([ // statements
  29913. 'rtl.createInterface(',
  29914. ' $mod,',
  29915. ' "IUnknown",',
  29916. ' "{B92D5841-758A-322B-B800-000000000000}",',
  29917. ' [],',
  29918. ' null,',
  29919. ' function () {',
  29920. ' }',
  29921. ');',
  29922. 'rtl.createInterface(',
  29923. ' $mod,',
  29924. ' "IBird",',
  29925. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  29926. ' ["GetItem", "SetItem"],',
  29927. ' null,',
  29928. ' function () {',
  29929. ' var $r = this.$rtti;',
  29930. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29931. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  29932. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  29933. ' }',
  29934. ');',
  29935. 'this.DoIt = function (t) {',
  29936. '}; ',
  29937. 'this.i = null;',
  29938. 'this.t = null;',
  29939. '']),
  29940. LinesToStr([ // $mod.$main
  29941. '$mod.t = $mod.$rtti["IBird"];',
  29942. '$mod.t = $mod.i.$rtti;',
  29943. '$mod.DoIt($mod.t);',
  29944. '$mod.DoIt($mod.$rtti["IBird"]);',
  29945. '']));
  29946. end;
  29947. procedure TTestModule.TestRTTI_Interface_COM;
  29948. begin
  29949. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29950. StartProgram(true,[supTypeInfo]);
  29951. Add([
  29952. '{$interfaces com}',
  29953. '{$modeswitch externalclass}',
  29954. 'type',
  29955. ' TGuid = record end;',
  29956. ' integer = longint;',
  29957. ' IUnknown = interface',
  29958. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  29959. ' function _AddRef: Integer;',
  29960. ' function _Release: Integer;',
  29961. ' end;',
  29962. ' IBird = interface',
  29963. ' function GetItem: longint;',
  29964. ' procedure SetItem(Value: longint);',
  29965. ' property Item: longint read GetItem write SetItem;',
  29966. ' end;',
  29967. 'var',
  29968. ' i: IBird;',
  29969. ' t: TTypeInfoInterface;',
  29970. 'begin',
  29971. ' t:=TypeInfo(IBird);',
  29972. ' t:=TypeInfo(i);',
  29973. '']);
  29974. ConvertProgram;
  29975. CheckSource('TestRTTI_Interface_COM',
  29976. LinesToStr([ // statements
  29977. 'rtl.recNewT($mod, "TGuid", function () {',
  29978. ' this.$eq = function (b) {',
  29979. ' return true;',
  29980. ' };',
  29981. ' this.$assign = function (s) {',
  29982. ' return this;',
  29983. ' };',
  29984. ' $mod.$rtti.$Record("TGuid", {});',
  29985. '});',
  29986. 'rtl.createInterface(',
  29987. ' $mod,',
  29988. ' "IUnknown",',
  29989. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  29990. ' ["QueryInterface", "_AddRef", "_Release"],',
  29991. ' null,',
  29992. ' function () {',
  29993. ' this.$kind = "com";',
  29994. ' var $r = this.$rtti;',
  29995. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  29996. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  29997. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  29998. ' }',
  29999. ');',
  30000. 'rtl.createInterface(',
  30001. ' $mod,',
  30002. ' "IBird",',
  30003. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30004. ' ["GetItem", "SetItem"],',
  30005. ' $mod.IUnknown,',
  30006. ' function () {',
  30007. ' var $r = this.$rtti;',
  30008. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30009. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30010. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30011. ' }',
  30012. ');',
  30013. 'this.i = null;',
  30014. 'this.t = null;',
  30015. '']),
  30016. LinesToStr([ // $mod.$main
  30017. '$mod.t = $mod.$rtti["IBird"];',
  30018. '$mod.t = $mod.i.$rtti;',
  30019. '']));
  30020. end;
  30021. procedure TTestModule.TestRTTI_ClassHelper;
  30022. begin
  30023. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30024. StartProgram(true,[supTypeInfo]);
  30025. Add([
  30026. '{$interfaces com}',
  30027. '{$modeswitch externalclass}',
  30028. 'type',
  30029. ' TObject = class',
  30030. ' end;',
  30031. ' THelper = class helper for TObject',
  30032. ' published',
  30033. ' function GetItem: longint;',
  30034. ' property Item: longint read GetItem;',
  30035. ' end;',
  30036. 'function THelper.GetItem: longint;',
  30037. 'begin',
  30038. 'end;',
  30039. 'var',
  30040. ' t: TTypeInfoHelper;',
  30041. 'begin',
  30042. ' t:=TypeInfo(THelper);',
  30043. '']);
  30044. ConvertProgram;
  30045. CheckSource('TestRTTI_ClassHelper',
  30046. LinesToStr([ // statements
  30047. 'rtl.createClass($mod, "TObject", null, function () {',
  30048. ' this.$init = function () {',
  30049. ' };',
  30050. ' this.$final = function () {',
  30051. ' };',
  30052. '});',
  30053. 'rtl.createHelper($mod, "THelper", null, function () {',
  30054. ' this.GetItem = function () {',
  30055. ' var Result = 0;',
  30056. ' return Result;',
  30057. ' };',
  30058. ' var $r = this.$rtti;',
  30059. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30060. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30061. '});',
  30062. 'this.t = null;',
  30063. '']),
  30064. LinesToStr([ // $mod.$main
  30065. '$mod.t = $mod.$rtti["THelper"];',
  30066. '']));
  30067. end;
  30068. procedure TTestModule.TestRTTI_ExternalClass;
  30069. begin
  30070. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30071. StartProgram(true,[supTypeInfo]);
  30072. Add([
  30073. '{$modeswitch externalclass}',
  30074. 'type',
  30075. ' TJSObject = class external name ''Object''',
  30076. ' end;',
  30077. ' TJSArray = class external name ''Array'' (TJSObject)',
  30078. ' end;',
  30079. 'var',
  30080. ' p: Pointer;',
  30081. ' tc: TTypeInfoExtClass;',
  30082. 'begin',
  30083. ' p:=typeinfo(TJSArray);']);
  30084. ConvertProgram;
  30085. CheckSource('TestRTTI_ExternalClass',
  30086. LinesToStr([ // statements
  30087. '$mod.$rtti.$ExtClass("TJSObject", {',
  30088. ' jsclass: "Object"',
  30089. '});',
  30090. '$mod.$rtti.$ExtClass("TJSArray", {',
  30091. ' ancestor: $mod.$rtti["TJSObject"],',
  30092. ' jsclass: "Array"',
  30093. '});',
  30094. 'this.p = null;',
  30095. 'this.tc = null;',
  30096. '']),
  30097. LinesToStr([ // $mod.$main
  30098. '$mod.p = $mod.$rtti["TJSArray"];',
  30099. '']));
  30100. end;
  30101. procedure TTestModule.TestResourcestringProgram;
  30102. begin
  30103. AddModuleWithIntfImplSrc('unit2.pas',
  30104. LinesToStr([
  30105. 'resourcestring Title = ''Nice'';',
  30106. '']),
  30107. '');
  30108. StartProgram(true);
  30109. Add([
  30110. 'uses unit2;',
  30111. 'const Bar = ''bar'';',
  30112. 'resourcestring',
  30113. ' Red = ''red'';',
  30114. ' Foobar = ''fOo''+bar;',
  30115. 'var s: string;',
  30116. ' c: char;',
  30117. 'begin',
  30118. ' s:=red;',
  30119. ' s:=test1.red;',
  30120. ' s:=Title;',
  30121. ' c:=red[1];',
  30122. ' c:=test1.red[2];',
  30123. ' if red=foobar then ;',
  30124. ' if red[3]=red[4] then ;']);
  30125. ConvertProgram;
  30126. CheckSource('TestResourcestringProgram',
  30127. LinesToStr([ // statements
  30128. 'this.Bar = "bar";',
  30129. 'this.s = "";',
  30130. 'this.c = "";',
  30131. '$mod.$resourcestrings = {',
  30132. ' Red: {',
  30133. ' org: "red"',
  30134. ' },',
  30135. ' Foobar: {',
  30136. ' org: "fOobar"',
  30137. ' }',
  30138. '};',
  30139. '']),
  30140. LinesToStr([ // $mod.$main
  30141. '$mod.s = rtl.getResStr($mod, "Red");',
  30142. '$mod.s = rtl.getResStr($mod, "Red");',
  30143. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30144. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30145. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30146. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30147. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30148. '']));
  30149. end;
  30150. procedure TTestModule.TestResourcestringUnit;
  30151. begin
  30152. AddModuleWithIntfImplSrc('unit2.pas',
  30153. LinesToStr([
  30154. 'resourcestring Title = ''Nice'';',
  30155. '']),
  30156. '');
  30157. StartUnit(true);
  30158. Add([
  30159. 'interface',
  30160. 'uses unit2;',
  30161. 'const Red = ''rEd'';',
  30162. 'resourcestring',
  30163. ' Blue = ''blue'';',
  30164. ' NotRed = ''not''+Red;',
  30165. 'var s: string;',
  30166. 'implementation',
  30167. 'resourcestring',
  30168. ' ImplGreen = ''green'';',
  30169. 'initialization',
  30170. ' s:=blue+ImplGreen;',
  30171. ' s:=test1.blue+test1.implgreen;',
  30172. ' s:=blue[1]+implgreen[2];',
  30173. ' s:=Title;',
  30174. '']);
  30175. ConvertUnit;
  30176. CheckSource('TestResourcestringUnit',
  30177. LinesToStr([ // statements
  30178. 'this.Red = "rEd";',
  30179. 'this.s = "";',
  30180. '$mod.$resourcestrings = {',
  30181. ' Blue: {',
  30182. ' org: "blue"',
  30183. ' },',
  30184. ' NotRed: {',
  30185. ' org: "notrEd"',
  30186. ' },',
  30187. ' ImplGreen: {',
  30188. ' org: "green"',
  30189. ' }',
  30190. '};',
  30191. '']),
  30192. LinesToStr([ // $mod.$main
  30193. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30194. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30195. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30196. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30197. '']));
  30198. end;
  30199. procedure TTestModule.TestResourcestringImplementation;
  30200. begin
  30201. StartUnit(false);
  30202. Add([
  30203. 'interface',
  30204. 'implementation',
  30205. 'resourcestring',
  30206. ' ImplRed = ''red'';']);
  30207. ConvertUnit;
  30208. CheckSource('TestResourcestringImplementation',
  30209. LinesToStr([ // intf statements
  30210. 'var $impl = $mod.$impl;']),
  30211. LinesToStr([ // $mod.$init
  30212. '']),
  30213. LinesToStr([ // impl statements
  30214. '$mod.$resourcestrings = {',
  30215. ' ImplRed: {',
  30216. ' org: "red"',
  30217. ' }',
  30218. '};',
  30219. '']));
  30220. end;
  30221. procedure TTestModule.TestAttributes_Members;
  30222. begin
  30223. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30224. StartProgram(false);
  30225. Add([
  30226. '{$modeswitch PrefixedAttributes}',
  30227. 'type',
  30228. ' TObject = class',
  30229. ' constructor Create;',
  30230. ' end;',
  30231. ' TCustomAttribute = class',
  30232. ' constructor Create(Id: word);',
  30233. ' end;',
  30234. ' [Missing]',
  30235. ' TBird = class',
  30236. ' published',
  30237. ' [Tcustom]',
  30238. ' FField: word;',
  30239. ' [tcustom(14)]',
  30240. ' property Size: word read FField;',
  30241. ' [Tcustom(15)]',
  30242. ' procedure Fly; virtual; abstract;',
  30243. ' end;',
  30244. ' TRec = record',
  30245. ' [Tcustom,tcustom(14)]',
  30246. ' Size: word;',
  30247. ' end;',
  30248. 'constructor TObject.Create; begin end;',
  30249. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30250. 'begin',
  30251. '']);
  30252. ConvertProgram;
  30253. CheckSource('TestAttributes_Members',
  30254. LinesToStr([ // statements
  30255. 'rtl.createClass($mod, "TObject", null, function () {',
  30256. ' this.$init = function () {',
  30257. ' };',
  30258. ' this.$final = function () {',
  30259. ' };',
  30260. ' this.Create = function () {',
  30261. ' return this;',
  30262. ' };',
  30263. '});',
  30264. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  30265. ' this.Create$1 = function (Id) {',
  30266. ' return this;',
  30267. ' };',
  30268. '});',
  30269. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  30270. ' this.$init = function () {',
  30271. ' $mod.TObject.$init.call(this);',
  30272. ' this.FField = 0;',
  30273. ' };',
  30274. ' var $r = this.$rtti;',
  30275. ' $r.addField("FField", rtl.word, {',
  30276. ' attr: [$mod.TCustomAttribute, "Create"]',
  30277. ' });',
  30278. ' $r.addProperty(',
  30279. ' "Size",',
  30280. ' 0,',
  30281. ' rtl.word,',
  30282. ' "FField",',
  30283. ' "",',
  30284. ' {',
  30285. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30286. ' }',
  30287. ' );',
  30288. ' $r.addMethod("Fly", 0, null, null, {',
  30289. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30290. ' });',
  30291. '});',
  30292. 'rtl.recNewT($mod, "TRec", function () {',
  30293. ' this.Size = 0;',
  30294. ' this.$eq = function (b) {',
  30295. ' return this.Size === b.Size;',
  30296. ' };',
  30297. ' this.$assign = function (s) {',
  30298. ' this.Size = s.Size;',
  30299. ' return this;',
  30300. ' };',
  30301. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30302. ' $r.addField("Size", rtl.word, {',
  30303. ' attr: [',
  30304. ' $mod.TCustomAttribute,',
  30305. ' "Create",',
  30306. ' $mod.TCustomAttribute,',
  30307. ' "Create$1",',
  30308. ' [14]',
  30309. ' ]',
  30310. ' });',
  30311. '});',
  30312. '']),
  30313. LinesToStr([ // $mod.$main
  30314. '']));
  30315. end;
  30316. procedure TTestModule.TestAttributes_Types;
  30317. begin
  30318. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30319. StartProgram(false);
  30320. Add([
  30321. '{$modeswitch PrefixedAttributes}',
  30322. 'type',
  30323. ' TObject = class',
  30324. ' constructor Create(Id: word);',
  30325. ' end;',
  30326. ' TCustomAttribute = class',
  30327. ' end;',
  30328. ' [TCustom(1)]',
  30329. ' TMyClass = class',
  30330. ' end;',
  30331. ' [TCustom(2)]',
  30332. ' TRec = record',
  30333. ' end;',
  30334. ' [TCustom(3)]',
  30335. ' TInt = type word;',
  30336. 'constructor TObject.Create(Id: word);',
  30337. 'begin',
  30338. 'end;',
  30339. 'var p: pointer;',
  30340. 'begin',
  30341. ' p:=typeinfo(TMyClass);',
  30342. ' p:=typeinfo(TRec);',
  30343. ' p:=typeinfo(TInt);',
  30344. '']);
  30345. ConvertProgram;
  30346. CheckSource('TestAttributes_Types',
  30347. LinesToStr([ // statements
  30348. 'rtl.createClass($mod, "TObject", null, function () {',
  30349. ' this.$init = function () {',
  30350. ' };',
  30351. ' this.$final = function () {',
  30352. ' };',
  30353. ' this.Create = function (Id) {',
  30354. ' return this;',
  30355. ' };',
  30356. '});',
  30357. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  30358. '});',
  30359. 'rtl.createClass($mod, "TMyClass", $mod.TObject, function () {',
  30360. ' var $r = this.$rtti;',
  30361. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  30362. '});',
  30363. 'rtl.recNewT($mod, "TRec", function () {',
  30364. ' this.$eq = function (b) {',
  30365. ' return true;',
  30366. ' };',
  30367. ' this.$assign = function (s) {',
  30368. ' return this;',
  30369. ' };',
  30370. ' $mod.$rtti.$Record("TRec", {',
  30371. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  30372. ' });',
  30373. '});',
  30374. '$mod.$rtti.$inherited("TInt", rtl.word, {',
  30375. ' attr: [$mod.TCustomAttribute, "Create", [3]]',
  30376. '});',
  30377. 'this.p = null;',
  30378. '']),
  30379. LinesToStr([ // $mod.$main
  30380. '$mod.p = $mod.$rtti["TMyClass"];',
  30381. '$mod.p = $mod.$rtti["TRec"];',
  30382. '$mod.p = $mod.$rtti["TInt"];',
  30383. '']));
  30384. end;
  30385. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  30386. begin
  30387. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30388. StartProgram(false);
  30389. Add([
  30390. '{$modeswitch PrefixedAttributes}',
  30391. 'type',
  30392. ' TObject = class',
  30393. ' constructor Create;',
  30394. ' end;',
  30395. ' TCustomAttribute = class',
  30396. ' end;',
  30397. ' THelper = class helper for TCustomAttribute',
  30398. ' constructor Create(Id: word);',
  30399. ' end;',
  30400. ' [TCustom(3)]',
  30401. ' TMyInt = word;',
  30402. 'constructor TObject.Create; begin end;',
  30403. 'constructor THelper.Create(Id: word); begin end;',
  30404. 'begin',
  30405. ' if typeinfo(TMyInt)=nil then ;']);
  30406. ConvertProgram;
  30407. end;
  30408. procedure TTestModule.TestAssert;
  30409. begin
  30410. StartProgram(false);
  30411. Add([
  30412. 'procedure DoIt;',
  30413. 'var',
  30414. ' b: boolean;',
  30415. ' s: string;',
  30416. 'begin',
  30417. ' {$Assertions on}',
  30418. ' Assert(b);',
  30419. 'end;',
  30420. 'begin',
  30421. ' DoIt;',
  30422. '']);
  30423. ConvertProgram;
  30424. CheckSource('TestAssert',
  30425. LinesToStr([ // statements
  30426. 'this.DoIt = function () {',
  30427. ' var b = false;',
  30428. ' var s = "";',
  30429. ' if (!b) throw "assert failed";',
  30430. '};',
  30431. '']),
  30432. LinesToStr([ // $mod.$main
  30433. '$mod.DoIt();',
  30434. '']));
  30435. end;
  30436. procedure TTestModule.TestAssert_SysUtils;
  30437. begin
  30438. AddModuleWithIntfImplSrc('SysUtils.pas',
  30439. LinesToStr([
  30440. 'type',
  30441. ' TObject = class',
  30442. ' constructor Create;',
  30443. ' end;',
  30444. ' EAssertionFailed = class',
  30445. ' constructor Create(s: string);',
  30446. ' end;',
  30447. '']),
  30448. LinesToStr([
  30449. 'constructor TObject.Create;',
  30450. 'begin end;',
  30451. 'constructor EAssertionFailed.Create(s: string);',
  30452. 'begin end;',
  30453. '']) );
  30454. StartProgram(true);
  30455. Add([
  30456. 'uses sysutils;',
  30457. 'procedure DoIt;',
  30458. 'var',
  30459. ' b: boolean;',
  30460. ' s: string;',
  30461. 'begin',
  30462. ' {$Assertions on}',
  30463. ' Assert(b);',
  30464. ' Assert(b,''msg'');',
  30465. 'end;',
  30466. 'begin',
  30467. ' DoIt;',
  30468. '']);
  30469. ConvertProgram;
  30470. CheckSource('TestAssert_SysUtils',
  30471. LinesToStr([ // statements
  30472. 'this.DoIt = function () {',
  30473. ' var b = false;',
  30474. ' var s = "";',
  30475. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  30476. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  30477. '};',
  30478. '']),
  30479. LinesToStr([ // $mod.$main
  30480. '$mod.DoIt();',
  30481. '']));
  30482. end;
  30483. procedure TTestModule.TestObjectChecks;
  30484. begin
  30485. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  30486. StartProgram(false);
  30487. Add([
  30488. 'type',
  30489. ' TObject = class',
  30490. ' procedure DoIt;',
  30491. ' end;',
  30492. ' TClass = class of tobject;',
  30493. ' TBird = class',
  30494. ' end;',
  30495. ' TBirdClass = class of TBird;',
  30496. 'var',
  30497. ' o : TObject;',
  30498. ' c: TClass;',
  30499. ' b: TBird;',
  30500. ' bc: TBirdClass;',
  30501. 'procedure TObject.DoIt;',
  30502. 'begin',
  30503. ' b:=TBird(o);',
  30504. 'end;',
  30505. 'begin',
  30506. ' o.DoIt;',
  30507. ' b:=TBird(o);',
  30508. ' bc:=TBirdClass(c);',
  30509. '']);
  30510. ConvertProgram;
  30511. CheckSource('TestCheckMethodCall',
  30512. LinesToStr([ // statements
  30513. 'rtl.createClass($mod, "TObject", null, function () {',
  30514. ' this.$init = function () {',
  30515. ' };',
  30516. ' this.$final = function () {',
  30517. ' };',
  30518. ' this.DoIt = function () {',
  30519. ' rtl.checkMethodCall(this,$mod.TObject);',
  30520. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  30521. ' };',
  30522. '});',
  30523. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  30524. '});',
  30525. 'this.o = null;',
  30526. 'this.c = null;',
  30527. 'this.b = null;',
  30528. 'this.bc = null;',
  30529. '']),
  30530. LinesToStr([ // $mod.$main
  30531. '$mod.o.DoIt();',
  30532. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  30533. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  30534. '']));
  30535. end;
  30536. procedure TTestModule.TestOverflowChecks_Int;
  30537. begin
  30538. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  30539. StartProgram(false);
  30540. Add([
  30541. 'procedure DoIt;',
  30542. 'var',
  30543. ' b: byte;',
  30544. ' n: nativeint;',
  30545. ' u: nativeuint;',
  30546. ' c: currency;',
  30547. 'begin',
  30548. ' n:=n+n;',
  30549. ' n:=n-n;',
  30550. ' n:=n+b;',
  30551. ' n:=b-n;',
  30552. ' n:=n*n;',
  30553. ' n:=n*u;',
  30554. ' c:=c+b;',
  30555. ' c:=b+c;',
  30556. ' c:=c*b;',
  30557. ' c:=b*c;',
  30558. 'end;',
  30559. 'begin',
  30560. '']);
  30561. ConvertProgram;
  30562. CheckSource('TestOverflowChecks_Int',
  30563. LinesToStr([ // statements
  30564. 'this.DoIt = function () {',
  30565. ' var b = 0;',
  30566. ' var n = 0;',
  30567. ' var u = 0;',
  30568. ' var c = 0;',
  30569. ' n = rtl.oc(n + n);',
  30570. ' n = rtl.oc(n - n);',
  30571. ' n = rtl.oc(n + b);',
  30572. ' n = rtl.oc(b - n);',
  30573. ' n = rtl.oc(n * n);',
  30574. ' n = rtl.oc(n * u);',
  30575. ' c = rtl.oc(c + (b * 10000));',
  30576. ' c = rtl.oc((b * 10000) + c);',
  30577. ' c = rtl.oc(c * b);',
  30578. ' c = rtl.oc(b * c);',
  30579. '};',
  30580. '']),
  30581. LinesToStr([ // $mod.$main
  30582. '']));
  30583. end;
  30584. procedure TTestModule.TestRangeChecks_AssignInt;
  30585. begin
  30586. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30587. StartProgram(false);
  30588. Add([
  30589. '{$R+}',
  30590. 'var',
  30591. ' b: byte = 2;',
  30592. ' w: word = 3;',
  30593. 'procedure DoIt(p: byte);',
  30594. 'begin',
  30595. ' b:=w;',
  30596. ' b+=w;',
  30597. ' b:=1;',
  30598. 'end;',
  30599. '{$R-}',
  30600. 'procedure DoSome;',
  30601. 'begin',
  30602. ' DoIt(w);',
  30603. ' b:=w;',
  30604. ' b:=2;',
  30605. 'end;',
  30606. 'begin',
  30607. '{$R+}',
  30608. '']);
  30609. ConvertProgram;
  30610. CheckSource('TestRangeChecks_AssignInt',
  30611. LinesToStr([ // statements
  30612. 'this.b = 2;',
  30613. 'this.w = 3;',
  30614. 'this.DoIt = function (p) {',
  30615. ' rtl.rc(p, 0, 255);',
  30616. ' $mod.b = rtl.rc($mod.w,0,255);',
  30617. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  30618. ' $mod.b = 1;',
  30619. '};',
  30620. 'this.DoSome = function () {',
  30621. ' $mod.DoIt($mod.w);',
  30622. ' $mod.b = $mod.w;',
  30623. ' $mod.b = 2;',
  30624. '};',
  30625. '']),
  30626. LinesToStr([ // $mod.$main
  30627. '']));
  30628. end;
  30629. procedure TTestModule.TestRangeChecks_AssignIntRange;
  30630. begin
  30631. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30632. StartProgram(false);
  30633. Add([
  30634. '{$R+}',
  30635. 'type Ten = 1..10;',
  30636. 'var',
  30637. ' b: Ten = 2;',
  30638. ' w: Ten = 3;',
  30639. 'procedure DoIt(p: Ten);',
  30640. 'begin',
  30641. ' b:=w;',
  30642. ' b+=w;',
  30643. ' b:=1;',
  30644. 'end;',
  30645. '{$R-}',
  30646. 'procedure DoSome;',
  30647. 'begin',
  30648. ' DoIt(w);',
  30649. ' b:=w;',
  30650. ' b:=2;',
  30651. 'end;',
  30652. 'begin',
  30653. '{$R+}',
  30654. '']);
  30655. ConvertProgram;
  30656. CheckSource('TestRangeChecks_AssignIntRange',
  30657. LinesToStr([ // statements
  30658. 'this.b = 2;',
  30659. 'this.w = 3;',
  30660. 'this.DoIt = function (p) {',
  30661. ' rtl.rc(p, 1, 10);',
  30662. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  30663. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  30664. ' $mod.b = 1;',
  30665. '};',
  30666. 'this.DoSome = function () {',
  30667. ' $mod.DoIt($mod.w);',
  30668. ' $mod.b = $mod.w;',
  30669. ' $mod.b = 2;',
  30670. '};',
  30671. '']),
  30672. LinesToStr([ // $mod.$main
  30673. '']));
  30674. end;
  30675. procedure TTestModule.TestRangeChecks_AssignEnum;
  30676. begin
  30677. StartProgram(false);
  30678. Add([
  30679. '{$R+}',
  30680. 'type TEnum = (red,green);',
  30681. 'var',
  30682. ' e: TEnum = red;',
  30683. 'procedure DoIt(p: TEnum);',
  30684. 'begin',
  30685. ' e:=p;',
  30686. ' p:=TEnum(0);',
  30687. ' p:=succ(e);',
  30688. 'end;',
  30689. '{$R-}',
  30690. 'procedure DoSome;',
  30691. 'begin',
  30692. ' DoIt(e);',
  30693. ' e:=TEnum(1);',
  30694. ' e:=pred(e);',
  30695. 'end;',
  30696. 'begin',
  30697. '{$R+}',
  30698. '']);
  30699. ConvertProgram;
  30700. CheckSource('TestRangeChecks_AssignEnum',
  30701. LinesToStr([ // statements
  30702. 'this.TEnum = {',
  30703. ' "0": "red",',
  30704. ' red: 0,',
  30705. ' "1": "green",',
  30706. ' green: 1',
  30707. '};',
  30708. 'this.e = $mod.TEnum.red;',
  30709. 'this.DoIt = function (p) {',
  30710. ' rtl.rc(p, 0, 1);',
  30711. ' $mod.e = rtl.rc(p, 0, 1);',
  30712. ' p = 0;',
  30713. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30714. '};',
  30715. 'this.DoSome = function () {',
  30716. ' $mod.DoIt($mod.e);',
  30717. ' $mod.e = 1;',
  30718. ' $mod.e = $mod.e - 1;',
  30719. '};',
  30720. '']),
  30721. LinesToStr([ // $mod.$main
  30722. '']));
  30723. end;
  30724. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  30725. begin
  30726. StartProgram(false);
  30727. Add([
  30728. '{$R+}',
  30729. 'type',
  30730. ' TEnum = (red,green);',
  30731. ' TEnumRg = red..green;',
  30732. 'var',
  30733. ' e: TEnumRg = red;',
  30734. 'procedure DoIt(p: TEnumRg);',
  30735. 'begin',
  30736. ' e:=p;',
  30737. ' p:=TEnumRg(0);',
  30738. ' p:=succ(e);',
  30739. 'end;',
  30740. '{$R-}',
  30741. 'procedure DoSome;',
  30742. 'begin',
  30743. ' DoIt(e);',
  30744. ' e:=TEnum(1);',
  30745. ' e:=pred(e);',
  30746. 'end;',
  30747. 'begin',
  30748. '{$R+}',
  30749. '']);
  30750. ConvertProgram;
  30751. CheckSource('TestRangeChecks_AssignEnumRange',
  30752. LinesToStr([ // statements
  30753. 'this.TEnum = {',
  30754. ' "0": "red",',
  30755. ' red: 0,',
  30756. ' "1": "green",',
  30757. ' green: 1',
  30758. '};',
  30759. 'this.e = $mod.TEnum.red;',
  30760. 'this.DoIt = function (p) {',
  30761. ' rtl.rc(p, 0, 1);',
  30762. ' $mod.e = rtl.rc(p, 0, 1);',
  30763. ' p = 0;',
  30764. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30765. '};',
  30766. 'this.DoSome = function () {',
  30767. ' $mod.DoIt($mod.e);',
  30768. ' $mod.e = 1;',
  30769. ' $mod.e = $mod.e - 1;',
  30770. '};',
  30771. '']),
  30772. LinesToStr([ // $mod.$main
  30773. '']));
  30774. end;
  30775. procedure TTestModule.TestRangeChecks_AssignChar;
  30776. begin
  30777. StartProgram(false);
  30778. Add([
  30779. '{$R+}',
  30780. 'type',
  30781. ' TLetter = char;',
  30782. 'var',
  30783. ' b: TLetter = ''2'';',
  30784. ' w: TLetter = ''3'';',
  30785. 'procedure DoIt(p: TLetter);',
  30786. 'begin',
  30787. ' b:=w;',
  30788. ' b:=''1'';',
  30789. 'end;',
  30790. '{$R-}',
  30791. 'procedure DoSome;',
  30792. 'begin',
  30793. ' DoIt(w);',
  30794. ' b:=w;',
  30795. ' b:=''2'';',
  30796. 'end;',
  30797. 'begin',
  30798. '{$R+}',
  30799. '']);
  30800. ConvertProgram;
  30801. CheckSource('TestRangeChecks_AssignChar',
  30802. LinesToStr([ // statements
  30803. 'this.b = "2";',
  30804. 'this.w = "3";',
  30805. 'this.DoIt = function (p) {',
  30806. ' rtl.rcc(p, 0, 65535);',
  30807. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  30808. ' $mod.b = "1";',
  30809. '};',
  30810. 'this.DoSome = function () {',
  30811. ' $mod.DoIt($mod.w);',
  30812. ' $mod.b = $mod.w;',
  30813. ' $mod.b = "2";',
  30814. '};',
  30815. '']),
  30816. LinesToStr([ // $mod.$main
  30817. '']));
  30818. end;
  30819. procedure TTestModule.TestRangeChecks_AssignCharRange;
  30820. begin
  30821. StartProgram(false);
  30822. Add([
  30823. '{$R+}',
  30824. 'type TDigit = ''0''..''9'';',
  30825. 'var',
  30826. ' b: TDigit = ''2'';',
  30827. ' w: TDigit = ''3'';',
  30828. 'procedure DoIt(p: TDigit);',
  30829. 'begin',
  30830. ' b:=w;',
  30831. ' b:=''1'';',
  30832. 'end;',
  30833. '{$R-}',
  30834. 'procedure DoSome;',
  30835. 'begin',
  30836. ' DoIt(w);',
  30837. ' b:=w;',
  30838. ' b:=''2'';',
  30839. 'end;',
  30840. 'begin',
  30841. '{$R+}',
  30842. '']);
  30843. ConvertProgram;
  30844. CheckSource('TestRangeChecks_AssignCharRange',
  30845. LinesToStr([ // statements
  30846. 'this.b = "2";',
  30847. 'this.w = "3";',
  30848. 'this.DoIt = function (p) {',
  30849. ' rtl.rcc(p, 48, 57);',
  30850. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  30851. ' $mod.b = "1";',
  30852. '};',
  30853. 'this.DoSome = function () {',
  30854. ' $mod.DoIt($mod.w);',
  30855. ' $mod.b = $mod.w;',
  30856. ' $mod.b = "2";',
  30857. '};',
  30858. '']),
  30859. LinesToStr([ // $mod.$main
  30860. '']));
  30861. end;
  30862. procedure TTestModule.TestRangeChecks_ArrayIndex;
  30863. begin
  30864. StartProgram(false);
  30865. Add([
  30866. '{$R+}',
  30867. 'type',
  30868. ' Ten = 1..10;',
  30869. ' TArr = array of Ten;',
  30870. ' TArrArr = array of TArr;',
  30871. ' TArrByte = array[byte] of Ten;',
  30872. ' TArrChar = array[''0''..''9''] of Ten;',
  30873. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  30874. ' TObject = class',
  30875. ' A: TArr;',
  30876. ' end;',
  30877. 'procedure DoIt;',
  30878. 'var',
  30879. ' Arr: TArr;',
  30880. ' ArrArr: TArrArr;',
  30881. ' ArrByte: TArrByte;',
  30882. ' ArrChar: TArrChar;',
  30883. ' ArrByteChar: TArrByteChar;',
  30884. ' i: Ten;',
  30885. ' c: char;',
  30886. ' o: tobject;',
  30887. 'begin',
  30888. ' i:=Arr[1];',
  30889. ' i:=ArrByteChar[1,''2''];',
  30890. ' Arr[1]:=Arr[1];',
  30891. ' Arr[i]:=Arr[i];',
  30892. ' ArrByte[3]:=ArrByte[3];',
  30893. ' ArrByte[i]:=ArrByte[i];',
  30894. ' ArrChar[''5'']:=ArrChar[''5''];',
  30895. ' ArrChar[c]:=ArrChar[c];',
  30896. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  30897. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  30898. ' o.a[i]:=o.a[i];',
  30899. 'end;',
  30900. 'begin',
  30901. '']);
  30902. ConvertProgram;
  30903. CheckSource('TestRangeChecks_ArrayIndex',
  30904. LinesToStr([ // statements
  30905. 'rtl.createClass($mod, "TObject", null, function () {',
  30906. ' this.$init = function () {',
  30907. ' this.A = [];',
  30908. ' };',
  30909. ' this.$final = function () {',
  30910. ' this.A = undefined;',
  30911. ' };',
  30912. '});',
  30913. 'this.DoIt = function () {',
  30914. ' var Arr = [];',
  30915. ' var ArrArr = [];',
  30916. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  30917. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  30918. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  30919. ' var i = 0;',
  30920. ' var c = "";',
  30921. ' var o = null;',
  30922. ' i = rtl.rc(Arr[1], 1, 10);',
  30923. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  30924. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  30925. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  30926. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  30927. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  30928. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  30929. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  30930. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  30931. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  30932. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  30933. '};',
  30934. '']),
  30935. LinesToStr([ // $mod.$main
  30936. '']));
  30937. end;
  30938. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  30939. begin
  30940. StartProgram(false);
  30941. Add([
  30942. '{$R+}',
  30943. 'type',
  30944. ' Ten = 1..10;',
  30945. ' TRec = record x: Ten end;',
  30946. ' TArr = array of TRec;',
  30947. ' TArrArr = array of TArr;',
  30948. ' TObject = class',
  30949. ' A: TArr;',
  30950. ' end;',
  30951. 'procedure DoIt;',
  30952. 'var',
  30953. ' Arr: TArr;',
  30954. ' ArrArr: TArrArr;',
  30955. ' i: Ten;',
  30956. ' o: tobject;',
  30957. 'begin',
  30958. ' Arr[1]:=Arr[1];',
  30959. ' Arr[i]:=Arr[i+1];',
  30960. ' o.a[i]:=o.a[i+2];',
  30961. 'end;',
  30962. 'begin',
  30963. '']);
  30964. ConvertProgram;
  30965. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  30966. LinesToStr([ // statements
  30967. 'rtl.recNewT($mod, "TRec", function () {',
  30968. ' this.x = 0;',
  30969. ' this.$eq = function (b) {',
  30970. ' return this.x === b.x;',
  30971. ' };',
  30972. ' this.$assign = function (s) {',
  30973. ' this.x = s.x;',
  30974. ' return this;',
  30975. ' };',
  30976. '});',
  30977. 'rtl.createClass($mod, "TObject", null, function () {',
  30978. ' this.$init = function () {',
  30979. ' this.A = [];',
  30980. ' };',
  30981. ' this.$final = function () {',
  30982. ' this.A = undefined;',
  30983. ' };',
  30984. '});',
  30985. 'this.DoIt = function () {',
  30986. ' var Arr = [];',
  30987. ' var ArrArr = [];',
  30988. ' var i = 0;',
  30989. ' var o = null;',
  30990. ' Arr[1].$assign(Arr[1]);',
  30991. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  30992. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  30993. '};',
  30994. '']),
  30995. LinesToStr([ // $mod.$main
  30996. '']));
  30997. end;
  30998. procedure TTestModule.TestRangeChecks_StringIndex;
  30999. begin
  31000. StartProgram(false);
  31001. Add([
  31002. 'type',
  31003. ' TObject = class',
  31004. ' S: string;',
  31005. ' end;',
  31006. '{$R+}',
  31007. 'procedure DoIt(var h: string);',
  31008. 'var',
  31009. ' s: string;',
  31010. ' i: longint;',
  31011. ' c: char;',
  31012. ' o: tobject;',
  31013. 'begin',
  31014. ' c:=s[1];',
  31015. ' s[i]:=s[i];',
  31016. ' h[i]:=h[i];',
  31017. ' c:=o.s[i];',
  31018. ' o.s[i]:=c;',
  31019. 'end;',
  31020. 'begin',
  31021. '']);
  31022. ConvertProgram;
  31023. CheckSource('TestRangeChecks_StringIndex',
  31024. LinesToStr([ // statements
  31025. 'rtl.createClass($mod, "TObject", null, function () {',
  31026. ' this.$init = function () {',
  31027. ' this.S = "";',
  31028. ' };',
  31029. ' this.$final = function () {',
  31030. ' };',
  31031. '});',
  31032. 'this.DoIt = function (h) {',
  31033. ' var s = "";',
  31034. ' var i = 0;',
  31035. ' var c = "";',
  31036. ' var o = null;',
  31037. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31038. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31039. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31040. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31041. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31042. '};',
  31043. '']),
  31044. LinesToStr([ // $mod.$main
  31045. '']));
  31046. end;
  31047. procedure TTestModule.TestRangeChecks_TypecastInt;
  31048. begin
  31049. StartProgram(false);
  31050. Add([
  31051. '{$R+}',
  31052. 'var',
  31053. ' i: nativeint;',
  31054. ' b: byte;',
  31055. ' sh: shortint;',
  31056. ' w: word;',
  31057. ' sm: smallint;',
  31058. ' lw: longword;',
  31059. ' li: longint;',
  31060. 'begin',
  31061. ' b:=12+byte(i);',
  31062. ' sh:=12+shortint(i);',
  31063. ' w:=12+word(i);',
  31064. ' sm:=12+smallint(i);',
  31065. ' lw:=12+longword(i);',
  31066. ' li:=12+longint(i);',
  31067. '']);
  31068. ConvertProgram;
  31069. CheckSource('TestRangeChecks_TypecastInt',
  31070. LinesToStr([
  31071. 'this.i = 0;',
  31072. 'this.b = 0;',
  31073. 'this.sh = 0;',
  31074. 'this.w = 0;',
  31075. 'this.sm = 0;',
  31076. 'this.lw = 0;',
  31077. 'this.li = 0;',
  31078. '']),
  31079. LinesToStr([
  31080. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31081. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31082. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31083. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31084. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31085. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31086. '']));
  31087. end;
  31088. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31089. begin
  31090. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31091. StartProgram(false);
  31092. Add([
  31093. '{$modeswitch typehelpers}',
  31094. '{$R+}',
  31095. 'type',
  31096. ' TObject = class',
  31097. ' FSize: byte;',
  31098. ' property Size: byte read FSize;',
  31099. ' end;',
  31100. ' THelper = type helper for byte',
  31101. ' procedure SetIt(w: word);',
  31102. ' end;',
  31103. 'procedure THelper.SetIt(w: word);',
  31104. 'begin',
  31105. ' Self:=w;',
  31106. 'end;',
  31107. 'function GetIt: byte;',
  31108. 'begin',
  31109. ' Result.SetIt(2);',
  31110. 'end;',
  31111. 'var',
  31112. ' b: byte = 3;',
  31113. ' o: TObject;',
  31114. 'begin',
  31115. ' b.SetIt(14);',
  31116. ' with b do SetIt(15);',
  31117. ' o.Size.SetIt(16);',
  31118. '']);
  31119. ConvertProgram;
  31120. CheckSource('TestRangeChecks_AssignInt',
  31121. LinesToStr([ // statements
  31122. 'rtl.createClass($mod, "TObject", null, function () {',
  31123. ' this.$init = function () {',
  31124. ' this.FSize = 0;',
  31125. ' };',
  31126. ' this.$final = function () {',
  31127. ' };',
  31128. '});',
  31129. 'rtl.createHelper($mod, "THelper", null, function () {',
  31130. ' this.SetIt = function (w) {',
  31131. ' rtl.rc(w, 0, 65535);',
  31132. ' this.set(w);',
  31133. ' };',
  31134. '});',
  31135. 'this.GetIt = function () {',
  31136. ' var Result = 0;',
  31137. ' $mod.THelper.SetIt.call({',
  31138. ' get: function () {',
  31139. ' return Result;',
  31140. ' },',
  31141. ' set: function (v) {',
  31142. ' rtl.rc(v, 0, 255);',
  31143. ' Result = v;',
  31144. ' }',
  31145. ' }, 2);',
  31146. ' return Result;',
  31147. '};',
  31148. 'this.b = 3;',
  31149. 'this.o = null;',
  31150. '']),
  31151. LinesToStr([ // $mod.$main
  31152. '$mod.THelper.SetIt.call({',
  31153. ' p: $mod,',
  31154. ' get: function () {',
  31155. ' return this.p.b;',
  31156. ' },',
  31157. ' set: function (v) {',
  31158. ' rtl.rc(v, 0, 255);',
  31159. ' this.p.b = v;',
  31160. ' }',
  31161. '}, 14);',
  31162. 'var $with = $mod.b;',
  31163. '$mod.THelper.SetIt.call({',
  31164. ' get: function () {',
  31165. ' return $with;',
  31166. ' },',
  31167. ' set: function (v) {',
  31168. ' rtl.rc(v, 0, 255);',
  31169. ' $with = v;',
  31170. ' }',
  31171. '}, 15);',
  31172. '$mod.THelper.SetIt.call({',
  31173. ' p: $mod.o,',
  31174. ' get: function () {',
  31175. ' return this.p.FSize;',
  31176. ' },',
  31177. ' set: function (v) {',
  31178. ' rtl.rc(v, 0, 255);',
  31179. ' this.p.FSize = v;',
  31180. ' }',
  31181. '}, 16);',
  31182. '']));
  31183. end;
  31184. procedure TTestModule.TestAsync_Proc;
  31185. begin
  31186. StartProgram(false);
  31187. Add([
  31188. 'procedure Fly(w: word = 1); async; forward;',
  31189. 'procedure Run(w: word = 2); async;',
  31190. 'begin',
  31191. ' Fly(w);',
  31192. ' Fly;',
  31193. ' await(Fly(w));',
  31194. ' await(Fly);',
  31195. 'end;',
  31196. 'procedure Fly(w: word); ',
  31197. 'begin',
  31198. 'end;',
  31199. 'begin',
  31200. ' Run;',
  31201. ' Run(3);',
  31202. '']);
  31203. ConvertProgram;
  31204. CheckSource('TestAsync_Proc',
  31205. LinesToStr([ // statements
  31206. 'this.Run = async function (w) {',
  31207. ' $mod.Fly(w);',
  31208. ' $mod.Fly(1);',
  31209. ' await $mod.Fly(w);',
  31210. ' await $mod.Fly(1);',
  31211. '};',
  31212. 'this.Fly = async function (w) {',
  31213. '};',
  31214. '']),
  31215. LinesToStr([
  31216. '$mod.Run(2);',
  31217. '$mod.Run(3);',
  31218. '']));
  31219. end;
  31220. procedure TTestModule.TestAsync_CallResultIsPromise;
  31221. begin
  31222. StartProgram(false);
  31223. Add([
  31224. '{$modeswitch externalclass}',
  31225. 'type',
  31226. ' TObject = class',
  31227. ' end;',
  31228. ' TJSPromise = class external name ''Promise''',
  31229. ' end;',
  31230. ' TBird = class',
  31231. ' function Fly: word; async; ',
  31232. ' end;',
  31233. 'function TBird.Fly: word; async; ',
  31234. 'begin',
  31235. ' Result:=3;',
  31236. ' Fly:=4+Result;',
  31237. ' if Result=5 then ;',
  31238. ' exit(6);',
  31239. 'end;',
  31240. 'function Run: word; async;',
  31241. 'begin',
  31242. ' Result:=11+Result;',
  31243. ' inc(Result);',
  31244. 'end;',
  31245. 'var',
  31246. ' p: TJSPromise;',
  31247. ' o: TBird;',
  31248. 'begin',
  31249. ' p:=Run;',
  31250. ' p:=Run();',
  31251. ' if Run=p then ;',
  31252. ' if p=Run then ;',
  31253. ' if Run()=p then ;',
  31254. ' if p=Run() then ;',
  31255. ' p:=o.Fly;',
  31256. ' p:=o.Fly();',
  31257. ' if o.Fly=p then ;',
  31258. ' if o.Fly()=p then ;',
  31259. ' with o do begin',
  31260. ' p:=Fly;',
  31261. ' p:=Fly();',
  31262. ' if Fly=p then ;',
  31263. ' if Fly()=p then ;',
  31264. ' end;',
  31265. '']);
  31266. ConvertProgram;
  31267. CheckSource('TestAsync_CallResultIsPromise',
  31268. LinesToStr([ // statements
  31269. 'rtl.createClass($mod, "TObject", null, function () {',
  31270. ' this.$init = function () {',
  31271. ' };',
  31272. ' this.$final = function () {',
  31273. ' };',
  31274. '});',
  31275. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  31276. ' this.Fly = async function () {',
  31277. ' var Result = 0;',
  31278. ' Result = 3;',
  31279. ' Result = 4 + Result;',
  31280. ' if (Result === 5) ;',
  31281. ' return 6;',
  31282. ' return Result;',
  31283. ' };',
  31284. '});',
  31285. 'this.Run = async function () {',
  31286. ' var Result = 0;',
  31287. ' Result = 11 + Result;',
  31288. ' Result += 1;',
  31289. ' return Result;',
  31290. '};',
  31291. 'this.p = null;',
  31292. 'this.o = null;',
  31293. '']),
  31294. LinesToStr([
  31295. '$mod.p = $mod.Run();',
  31296. '$mod.p = $mod.Run();',
  31297. 'if ($mod.Run() === $mod.p) ;',
  31298. 'if ($mod.p === $mod.Run()) ;',
  31299. 'if ($mod.Run() === $mod.p) ;',
  31300. 'if ($mod.p === $mod.Run()) ;',
  31301. '$mod.p = $mod.o.Fly();',
  31302. '$mod.p = $mod.o.Fly();',
  31303. 'if ($mod.o.Fly() === $mod.p) ;',
  31304. 'if ($mod.o.Fly() === $mod.p) ;',
  31305. 'var $with = $mod.o;',
  31306. '$mod.p = $with.Fly();',
  31307. '$mod.p = $with.Fly();',
  31308. 'if ($with.Fly() === $mod.p) ;',
  31309. 'if ($with.Fly() === $mod.p) ;',
  31310. '']));
  31311. end;
  31312. procedure TTestModule.TestAsync_ConstructorFail;
  31313. begin
  31314. StartProgram(false);
  31315. Add([
  31316. 'type',
  31317. ' TObject = class',
  31318. ' end;',
  31319. ' TBird = class',
  31320. ' constructor Create; async;',
  31321. ' end;',
  31322. 'constructor TBird.Create; async;',
  31323. 'begin',
  31324. 'end;',
  31325. 'begin',
  31326. '']);
  31327. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  31328. ConvertProgram;
  31329. end;
  31330. procedure TTestModule.TestAsync_PropertyGetterFail;
  31331. begin
  31332. StartProgram(false);
  31333. Add([
  31334. 'type',
  31335. ' TObject = class',
  31336. ' end;',
  31337. ' TBird = class',
  31338. ' function GetSize: word; async;',
  31339. ' property Size: word read GetSize;',
  31340. ' end;',
  31341. 'function TBird.GetSize: word; async;',
  31342. 'begin',
  31343. 'end;',
  31344. 'begin',
  31345. '']);
  31346. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  31347. ConvertProgram;
  31348. end;
  31349. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  31350. begin
  31351. StartProgram(false);
  31352. Add([
  31353. 'procedure Run; async;',
  31354. 'begin',
  31355. ' await(word,1);',
  31356. 'end;',
  31357. 'begin',
  31358. '']);
  31359. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  31360. ConvertProgram;
  31361. end;
  31362. procedure TTestModule.TestAWait_OutsideAsyncFail;
  31363. begin
  31364. StartProgram(false);
  31365. Add([
  31366. 'procedure Crawl(w: double); ',
  31367. 'begin',
  31368. 'end;',
  31369. 'procedure Run(w: double);',
  31370. 'begin',
  31371. ' await(Crawl(w));',
  31372. 'end;',
  31373. 'begin',
  31374. ' Run(1);']);
  31375. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  31376. ConvertProgram;
  31377. end;
  31378. procedure TTestModule.TestAWait_Result;
  31379. begin
  31380. StartProgram(false);
  31381. Add([
  31382. '{$modeswitch externalclass}',
  31383. 'type',
  31384. ' TJSPromise = class external name ''Promise''',
  31385. ' end;',
  31386. 'function Crawl(d: double = 1.3): word; ',
  31387. 'begin',
  31388. 'end;',
  31389. 'function Run(d: double = 1.6): word; async;',
  31390. 'begin',
  31391. ' Result:=await(1);',
  31392. ' Result:=await(Crawl);',
  31393. ' Result:=await(Crawl(4.5));',
  31394. ' Result:=await(Run);',
  31395. ' Result:=await(Run(6.7));',
  31396. 'end;',
  31397. 'begin',
  31398. ' Run(1);']);
  31399. ConvertProgram;
  31400. CheckSource('TestAWait_Result',
  31401. LinesToStr([ // statements
  31402. 'this.Crawl = function (d) {',
  31403. ' var Result = 0;',
  31404. ' return Result;',
  31405. '};',
  31406. 'this.Run = async function (d) {',
  31407. ' var Result = 0;',
  31408. ' Result = await 1;',
  31409. ' Result = await $mod.Crawl(1.3);',
  31410. ' Result = await $mod.Crawl(4.5);',
  31411. ' Result = await $mod.Run(1.6);',
  31412. ' Result = await $mod.Run(6.7);',
  31413. ' return Result;',
  31414. '};',
  31415. '']),
  31416. LinesToStr([
  31417. '$mod.Run(1);'
  31418. ]));
  31419. end;
  31420. procedure TTestModule.TestAWait_ExternalClassPromise;
  31421. begin
  31422. StartProgram(false);
  31423. Add([
  31424. '{$modeswitch externalclass}',
  31425. 'type',
  31426. ' TJSPromise = class external name ''Promise''',
  31427. ' end;',
  31428. 'function Fly(w: word): TJSPromise; async;',
  31429. 'begin',
  31430. 'end;',
  31431. 'function Jump(w: word): word; async;',
  31432. 'begin',
  31433. 'end;',
  31434. 'function Run(d: double): word; async;',
  31435. 'var',
  31436. ' p: TJSPromise;',
  31437. 'begin',
  31438. ' Result:=await(word,p);', // promise needs type
  31439. ' Result:=await(word,Fly(3));', // promise needs type
  31440. ' Result:=await(Jump(4));', // async non promise must omit the type
  31441. 'end;',
  31442. 'begin',
  31443. '']);
  31444. ConvertProgram;
  31445. CheckSource('TestAWait_ExternalClassPromise',
  31446. LinesToStr([ // statements
  31447. 'this.Fly = async function (w) {',
  31448. ' var Result = null;',
  31449. ' return Result;',
  31450. '};',
  31451. 'this.Jump = async function (w) {',
  31452. ' var Result = 0;',
  31453. ' return Result;',
  31454. '};',
  31455. 'this.Run = async function (d) {',
  31456. ' var Result = 0;',
  31457. ' var p = null;',
  31458. ' Result = await p;',
  31459. ' Result = await $mod.Fly(3);',
  31460. ' Result = await $mod.Jump(4);',
  31461. ' return Result;',
  31462. '};',
  31463. '']),
  31464. LinesToStr([
  31465. ]));
  31466. end;
  31467. procedure TTestModule.TestAsync_AnonymousProc;
  31468. begin
  31469. StartProgram(false);
  31470. Add([
  31471. '{$modeswitch externalclass}',
  31472. 'type',
  31473. ' TJSPromise = class external name ''Promise''',
  31474. ' end;',
  31475. '{$mode objfpc}',
  31476. 'type',
  31477. ' TFunc = reference to function(x: double): word; async;',
  31478. 'function Crawl(d: double = 1.3): word; async;',
  31479. 'begin',
  31480. 'end;',
  31481. 'var Func: TFunc;',
  31482. 'begin',
  31483. ' Func:=function(c:double):word async begin',
  31484. ' Result:=await(Crawl(c));',
  31485. ' end;',
  31486. ' Func:=function(c:double):word async assembler asm',
  31487. ' end;',
  31488. ' ']);
  31489. ConvertProgram;
  31490. CheckSource('TestAsync_AnonymousProc',
  31491. LinesToStr([ // statements
  31492. 'this.Crawl = async function (d) {',
  31493. ' var Result = 0;',
  31494. ' return Result;',
  31495. '};',
  31496. 'this.Func = null;',
  31497. '']),
  31498. LinesToStr([
  31499. '$mod.Func = async function (c) {',
  31500. ' var Result = 0;',
  31501. ' Result = await $mod.Crawl(c);',
  31502. ' return Result;',
  31503. '};',
  31504. '$mod.Func = async function (c) {',
  31505. '};',
  31506. '']));
  31507. end;
  31508. procedure TTestModule.TestAsync_ProcType;
  31509. begin
  31510. StartProgram(false);
  31511. Add([
  31512. '{$mode objfpc}',
  31513. 'type',
  31514. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  31515. ' TFunc = function(x: double = 1.1): word; async;',
  31516. ' TProc = procedure(x: longint = 7); async;',
  31517. 'function Crawl(d: double): word; async;',
  31518. 'begin',
  31519. 'end;',
  31520. 'procedure Run(e:longint); async;',
  31521. 'begin',
  31522. 'end;',
  31523. 'var',
  31524. ' RefFunc: TRefFunc;',
  31525. ' Func: TFunc;',
  31526. ' Proc, ProcB: TProc;',
  31527. 'begin',
  31528. ' Func:=@Crawl;',
  31529. ' RefFunc:=@Crawl;',
  31530. ' RefFunc:=function(c:double):word async begin',
  31531. ' Result:=await(RefFunc);',
  31532. ' Result:=await(RefFunc());',
  31533. ' Result:=await(Func);',
  31534. ' Result:=await(Func());',
  31535. ' await(Proc);',
  31536. ' await(Proc());',
  31537. ' await(Proc(13));',
  31538. ' end;',
  31539. ' Proc:=@Run;',
  31540. ' if Proc=ProcB then ;',
  31541. ' ']);
  31542. ConvertProgram;
  31543. CheckSource('TestAsync_ProcType',
  31544. LinesToStr([ // statements
  31545. 'this.Crawl = async function (d) {',
  31546. ' var Result = 0;',
  31547. ' return Result;',
  31548. '};',
  31549. 'this.Run = async function (e) {',
  31550. '};',
  31551. 'this.RefFunc = null;',
  31552. 'this.Func = null;',
  31553. 'this.Proc = null;',
  31554. 'this.ProcB = null;',
  31555. '']),
  31556. LinesToStr([
  31557. '$mod.Func = $mod.Crawl;',
  31558. '$mod.RefFunc = $mod.Crawl;',
  31559. '$mod.RefFunc = async function (c) {',
  31560. ' var Result = 0;',
  31561. ' Result = await $mod.RefFunc(1.3);',
  31562. ' Result = await $mod.RefFunc(1.3);',
  31563. ' Result = await $mod.Func(1.1);',
  31564. ' Result = await $mod.Func(1.1);',
  31565. ' await $mod.Proc(7);',
  31566. ' await $mod.Proc(7);',
  31567. ' await $mod.Proc(13);',
  31568. ' return Result;',
  31569. '};',
  31570. '$mod.Proc = $mod.Run;',
  31571. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  31572. '']));
  31573. end;
  31574. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  31575. begin
  31576. StartProgram(false);
  31577. Add([
  31578. '{$mode objfpc}',
  31579. 'type',
  31580. ' TRefFunc = reference to function(x: double = 1.3): word;',
  31581. 'function Crawl(d: double): word; async;',
  31582. 'begin',
  31583. 'end;',
  31584. 'var',
  31585. ' RefFunc: TRefFunc;',
  31586. 'begin',
  31587. ' RefFunc:=@Crawl;',
  31588. ' ']);
  31589. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  31590. ConvertProgram;
  31591. end;
  31592. procedure TTestModule.TestAsync_Inherited;
  31593. begin
  31594. StartProgram(false);
  31595. Add([
  31596. '{$mode objfpc}',
  31597. '{$modeswitch externalclass}',
  31598. 'type',
  31599. ' TJSPromise = class external name ''Promise''',
  31600. ' end;',
  31601. ' TObject = class',
  31602. ' function Run(w: word = 3): word; async; virtual;',
  31603. ' end;',
  31604. ' TBird = class',
  31605. ' function Run(w: word = 3): word; async; override;',
  31606. ' end;',
  31607. 'function TObject.Run(w: word = 3): word; async;',
  31608. 'begin',
  31609. 'end;',
  31610. 'function TBird.Run(w: word = 3): word; async;',
  31611. 'var p: TJSPromise;',
  31612. 'begin',
  31613. ' p:=inherited;',
  31614. ' p:=inherited Run;',
  31615. ' p:=inherited Run();',
  31616. ' p:=inherited Run(4);',
  31617. ' exit(p);',
  31618. ' exit(inherited);',
  31619. ' exit(inherited Run);',
  31620. ' exit(inherited Run(5));',
  31621. ' exit(6);',
  31622. 'end;',
  31623. 'begin',
  31624. ' ']);
  31625. ConvertProgram;
  31626. CheckSource('TestAsync_Inherited',
  31627. LinesToStr([ // statements
  31628. 'rtl.createClass($mod, "TObject", null, function () {',
  31629. ' this.$init = function () {',
  31630. ' };',
  31631. ' this.$final = function () {',
  31632. ' };',
  31633. ' this.Run = async function (w) {',
  31634. ' var Result = 0;',
  31635. ' return Result;',
  31636. ' };',
  31637. '});',
  31638. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  31639. ' this.Run = async function (w) {',
  31640. ' var Result = 0;',
  31641. ' var p = null;',
  31642. ' p = $mod.TObject.Run.apply(this, arguments);',
  31643. ' p = $mod.TObject.Run.call(this, 3);',
  31644. ' p = $mod.TObject.Run.call(this, 3);',
  31645. ' p = $mod.TObject.Run.call(this, 4);',
  31646. ' return p;',
  31647. ' return $mod.TObject.Run.apply(this, arguments);',
  31648. ' return $mod.TObject.Run.call(this, 3);',
  31649. ' return $mod.TObject.Run.call(this, 5);',
  31650. ' return 6;',
  31651. ' return Result;',
  31652. ' };',
  31653. '});',
  31654. '']),
  31655. LinesToStr([
  31656. '']));
  31657. end;
  31658. Initialization
  31659. RegisterTests([TTestModule]);
  31660. end.