tcmodules.pas 853 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392
  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. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1566. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1567. ' end;',
  1568. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1569. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1570. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1571. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1572. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1573. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1574. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1575. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1576. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1577. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1578. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1579. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1580. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1581. '']);
  1582. end;
  1583. Intf.Add('var');
  1584. Intf.Add(' ExitCode: Longint = 0;');
  1585. // unit implementation
  1586. Impl:=TStringList.Create;
  1587. if supTObject in Parts then
  1588. Impl.AddStrings([
  1589. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1590. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1591. 'asm',
  1592. 'end;',
  1593. 'constructor TObject.Create; begin end;',
  1594. 'destructor TObject.Destroy; begin end;',
  1595. 'class function TObject.ClassType: TClass; assembler;',
  1596. 'asm',
  1597. 'end;',
  1598. 'class function TObject.ClassName: String; assembler;',
  1599. 'asm',
  1600. 'end;',
  1601. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1602. 'begin',
  1603. ' Result:=SameText(Name,ClassName);',
  1604. 'end;',
  1605. 'class function TObject.ClassParent: TClass; assembler;',
  1606. 'asm',
  1607. 'end;',
  1608. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1609. 'asm',
  1610. 'end;',
  1611. 'class function TObject.UnitName: String; assembler;',
  1612. 'asm',
  1613. 'end;',
  1614. 'procedure TObject.AfterConstruction; begin end;',
  1615. 'procedure TObject.BeforeDestruction; begin end;',
  1616. 'function TObject.Equals(Obj: TObject): boolean;',
  1617. 'begin',
  1618. ' Result:=Obj=Self;',
  1619. 'end;',
  1620. 'function TObject.ToString: String;',
  1621. 'begin',
  1622. ' Result:=ClassName;',
  1623. 'end;'
  1624. ]);
  1625. if supTInterfacedObject in Parts then
  1626. Impl.AddStrings([
  1627. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1628. //'begin',
  1629. //'end;',
  1630. 'function TInterfacedObject._AddRef: Integer;',
  1631. 'begin',
  1632. 'end;',
  1633. 'function TInterfacedObject._Release: Integer;',
  1634. 'begin',
  1635. 'end;',
  1636. '']);
  1637. if supTVarRec in Parts then
  1638. Impl.AddStrings([
  1639. 'function VarRecs: TVarRecArray; varargs;',
  1640. 'var',
  1641. ' v: PVarRec;',
  1642. 'begin',
  1643. ' v^.VType:=1;',
  1644. ' v^.VJSValue:=2;',
  1645. 'end;',
  1646. '']);
  1647. try
  1648. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1649. finally
  1650. Intf.Free;
  1651. Impl.Free;
  1652. end;
  1653. end;
  1654. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1655. SystemUnitParts: TSystemUnitParts);
  1656. begin
  1657. if NeedSystemUnit then
  1658. AddSystemUnit(SystemUnitParts)
  1659. else
  1660. Parser.ImplicitUses.Clear;
  1661. Add('program '+ExtractFileUnitName(Filename)+';');
  1662. Add('');
  1663. end;
  1664. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1665. SystemUnitParts: TSystemUnitParts);
  1666. begin
  1667. if NeedSystemUnit then
  1668. AddSystemUnit(SystemUnitParts)
  1669. else
  1670. Parser.ImplicitUses.Clear;
  1671. Add('unit Test1;');
  1672. Add('');
  1673. end;
  1674. procedure TCustomTestModule.ConvertModule;
  1675. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1676. out UsesLit: TJSArrayLiteral);
  1677. var
  1678. i: Integer;
  1679. Item: TJSElement;
  1680. Lit: TJSLiteral;
  1681. begin
  1682. UsesLit:=nil;
  1683. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1684. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1685. exit; // null is ok
  1686. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1687. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1688. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1689. begin
  1690. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1691. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1692. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1693. Lit:=TJSLiteral(Item);
  1694. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1695. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1696. end;
  1697. end;
  1698. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1699. out Src: TJSSourceElements);
  1700. var
  1701. FunDecl: TJSFunctionDeclarationStatement;
  1702. FunDef: TJSFuncDef;
  1703. FunBody: TJSFunctionBody;
  1704. begin
  1705. Src:=nil;
  1706. AssertNotNull(ParamName,Arg.Expr);
  1707. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1708. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1709. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1710. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1711. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1712. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1713. AssertNotNull(ParamName+' body',FunDef.Body);
  1714. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1715. FunBody:=FunDef.Body as TJSFunctionBody;
  1716. AssertNotNull(ParamName+' body.A',FunBody.A);
  1717. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1718. Src:=FunBody.A as TJSSourceElements;
  1719. end;
  1720. var
  1721. ModuleNameExpr: TJSLiteral;
  1722. InitFunction: TJSFunctionDeclarationStatement;
  1723. InitAssign: TJSSimpleAssignStatement;
  1724. InitName: String;
  1725. LastNode: TJSElement;
  1726. Arg: TJSArrayLiteralElement;
  1727. begin
  1728. if SkipTests then exit;
  1729. try
  1730. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1731. except
  1732. on E: Exception do
  1733. HandleException(E);
  1734. end;
  1735. if SkipTests then exit;
  1736. if ExpectedErrorClass<>nil then
  1737. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1738. FJSSource:=TStringList.Create;
  1739. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1740. {$IFDEF VerbosePas2JS}
  1741. writeln('TTestModule.ConvertModule JS:');
  1742. write(FJSSource.Text);
  1743. {$ENDIF}
  1744. // rtl.module(...
  1745. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1746. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1747. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1748. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1749. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1750. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1751. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1752. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1753. // parameter 'unitname'
  1754. if JSModuleCallArgs.Elements.Count<1 then
  1755. Fail('rtl.module first param unit missing');
  1756. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1757. AssertNotNull('module name param',Arg.Expr);
  1758. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1759. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1760. if Module is TPasProgram then
  1761. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1762. else
  1763. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1764. // main uses section
  1765. if JSModuleCallArgs.Elements.Count<2 then
  1766. Fail('rtl.module second param main uses missing');
  1767. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1768. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1769. // program/library/interface function()
  1770. if JSModuleCallArgs.Elements.Count<3 then
  1771. Fail('rtl.module third param intf-function missing');
  1772. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1773. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1774. // search for $mod.$init or $mod.$main - the last statement
  1775. if Module is TPasProgram then
  1776. begin
  1777. InitName:='$main';
  1778. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1779. end
  1780. else
  1781. InitName:='$init';
  1782. FJSInitBody:=nil;
  1783. if JSModuleSrc.Statements.Count>0 then
  1784. begin
  1785. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1786. if LastNode is TJSSimpleAssignStatement then
  1787. begin
  1788. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1789. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1790. begin
  1791. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1792. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1793. end
  1794. else if Module is TPasProgram then
  1795. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1796. end;
  1797. end;
  1798. // optional: implementation uses section
  1799. if JSModuleCallArgs.Elements.Count<4 then
  1800. exit;
  1801. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1802. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1803. // optional: implementation function()
  1804. if JSModuleCallArgs.Elements.Count<5 then
  1805. exit;
  1806. Arg:=JSModuleCallArgs.Elements.Elements[4];
  1807. CheckFunctionParam('module impl-function',Arg,FJSImplementationSrc);
  1808. end;
  1809. procedure TCustomTestModule.ConvertProgram;
  1810. begin
  1811. Add('end.');
  1812. ParseProgram;
  1813. ConvertModule;
  1814. end;
  1815. procedure TCustomTestModule.ConvertUnit;
  1816. begin
  1817. Add('end.');
  1818. ParseUnit;
  1819. ConvertModule;
  1820. end;
  1821. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1822. begin
  1823. Result:=tcmodules.JSToStr(El);
  1824. end;
  1825. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1826. DottedName: string);
  1827. begin
  1828. if DottedName='' then
  1829. begin
  1830. AssertNull(Msg,El);
  1831. end
  1832. else
  1833. begin
  1834. AssertNotNull(Msg,El);
  1835. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1836. end;
  1837. end;
  1838. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1839. begin
  1840. if El=nil then
  1841. Result:=''
  1842. else if El is TJSPrimaryExpressionIdent then
  1843. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1844. else if El is TJSDotMemberExpression then
  1845. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1846. else
  1847. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1848. end;
  1849. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1850. InitStatements: string; ImplStatements: string);
  1851. var
  1852. ActualSrc, ExpectedSrc, InitName: String;
  1853. begin
  1854. ActualSrc:=JSToStr(JSModuleSrc);
  1855. ExpectedSrc:=
  1856. 'var $mod = this;'+LineEnding
  1857. +Statements;
  1858. if coUseStrict in Converter.Options then
  1859. ExpectedSrc:='"use strict";'+LineEnding+ExpectedSrc;
  1860. if Module is TPasProgram then
  1861. InitName:='$main'
  1862. else
  1863. InitName:='$init';
  1864. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1865. ExpectedSrc:=ExpectedSrc+LineEnding
  1866. +'$mod.'+InitName+' = function () {'+LineEnding
  1867. +InitStatements
  1868. +'};'+LineEnding;
  1869. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1870. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1871. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1872. if (JSImplementationSrc<>nil) then
  1873. begin
  1874. ActualSrc:=JSToStr(JSImplementationSrc);
  1875. ExpectedSrc:=
  1876. 'var $mod = this;'+LineEnding
  1877. +'var $impl = $mod.$impl;'+LineEnding
  1878. +ImplStatements;
  1879. end
  1880. else
  1881. begin
  1882. ActualSrc:='';
  1883. ExpectedSrc:=ImplStatements;
  1884. end;
  1885. //writeln('TTestModule.CheckSource InitStatements="',InitStatements,'"');
  1886. //writeln('TCustomTestModule.CheckSource Expected: ',ExpectedSrc);
  1887. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1888. end;
  1889. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1890. // search diff, ignore changes in spaces
  1891. var
  1892. s: string;
  1893. begin
  1894. if CheckSrcDiff(Expected,Actual,s) then exit;
  1895. Fail(Msg+': '+s);
  1896. end;
  1897. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1898. var
  1899. aResolver: TTestEnginePasResolver;
  1900. aConverter: TPasToJSConverter;
  1901. aJSModule: TJSSourceElements;
  1902. ActualSrc: String;
  1903. begin
  1904. aResolver:=GetResolver(Filename);
  1905. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1906. {$IFDEF VerbosePas2JS}
  1907. writeln('CheckUnit '+Filename+' converting ...');
  1908. {$ENDIF}
  1909. aConverter:=CreateConverter;
  1910. aJSModule:=nil;
  1911. try
  1912. try
  1913. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1914. except
  1915. on E: Exception do
  1916. HandleException(E);
  1917. end;
  1918. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1919. {$IFDEF VerbosePas2JS}
  1920. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  1921. write(aResolver.Source);
  1922. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  1923. write(ActualSrc);
  1924. {$ENDIF}
  1925. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  1926. finally
  1927. aJSModule.Free;
  1928. aConverter.Free;
  1929. end;
  1930. end;
  1931. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  1932. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  1933. var
  1934. i: Integer;
  1935. Item: TTestHintMessage;
  1936. Expected,Actual: string;
  1937. begin
  1938. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  1939. for i:=0 to MsgCount-1 do
  1940. begin
  1941. Item:=Msgs[i];
  1942. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  1943. if (Marker<>nil) then
  1944. begin
  1945. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  1946. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  1947. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  1948. end;
  1949. // found
  1950. FHintMsgsGood.Add(Item);
  1951. str(Item.MsgType,Actual);
  1952. str(MsgType,Expected);
  1953. AssertEquals('MsgType',Expected,Actual);
  1954. exit;
  1955. end;
  1956. // needed message missing -> show emitted messages
  1957. WriteSources('',0,0);
  1958. for i:=0 to MsgCount-1 do
  1959. begin
  1960. Item:=Msgs[i];
  1961. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  1962. ' ('+IntToStr(Item.MsgNumber),')');
  1963. if Marker<>nil then
  1964. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  1965. writeln(' {',Item.Msg,'}');
  1966. end;
  1967. str(MsgType,Expected);
  1968. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  1969. if Marker<>nil then
  1970. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  1971. Actual:=Actual+' '+Msg;
  1972. Fail(Actual);
  1973. end;
  1974. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  1975. );
  1976. var
  1977. i: Integer;
  1978. s, Txt: String;
  1979. Msg: TTestHintMessage;
  1980. begin
  1981. for i:=0 to MsgCount-1 do
  1982. begin
  1983. Msg:=Msgs[i];
  1984. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  1985. s:='';
  1986. str(Msg.MsgType,s);
  1987. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  1988. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  1989. if WithSourcePos then
  1990. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  1991. Txt:=Txt+' {'+Msg.Msg+'}';
  1992. Fail(Txt);
  1993. end;
  1994. end;
  1995. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  1996. MsgNumber: integer);
  1997. begin
  1998. ExpectedErrorClass:=EScannerError;
  1999. ExpectedErrorMsg:=Msg;
  2000. ExpectedErrorNumber:=MsgNumber;
  2001. end;
  2002. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2003. MsgNumber: integer);
  2004. begin
  2005. ExpectedErrorClass:=EParserError;
  2006. ExpectedErrorMsg:=Msg;
  2007. ExpectedErrorNumber:=MsgNumber;
  2008. end;
  2009. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2010. MsgNumber: integer);
  2011. begin
  2012. ExpectedErrorClass:=EPasResolve;
  2013. ExpectedErrorMsg:=Msg;
  2014. ExpectedErrorNumber:=MsgNumber;
  2015. end;
  2016. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2017. MsgNumber: integer);
  2018. begin
  2019. ExpectedErrorClass:=EPas2JS;
  2020. ExpectedErrorMsg:=Msg;
  2021. ExpectedErrorNumber:=MsgNumber;
  2022. end;
  2023. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2024. var
  2025. MsgNumber: Integer;
  2026. Msg: String;
  2027. begin
  2028. Result:=false;
  2029. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2030. Msg:=E.Message;
  2031. if E is EPas2JS then
  2032. MsgNumber:=EPas2JS(E).MsgNumber
  2033. else if E is EPasResolve then
  2034. MsgNumber:=EPasResolve(E).MsgNumber
  2035. else if E is EParserError then
  2036. MsgNumber:=Parser.LastMsgNumber
  2037. else if E is EScannerError then
  2038. begin
  2039. MsgNumber:=Scanner.LastMsgNumber;
  2040. Msg:=Scanner.LastMsg;
  2041. end
  2042. else
  2043. MsgNumber:=0;
  2044. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2045. if Result then
  2046. SkipTests:=true;
  2047. end;
  2048. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2049. begin
  2050. if IsErrorExpected(E) then exit;
  2051. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2052. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2053. +' '+Scanner.CurFilename
  2054. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2055. FailException(E);
  2056. end;
  2057. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2058. begin
  2059. if IsErrorExpected(E) then exit;
  2060. WriteSources(E.Filename,E.Row,E.Column);
  2061. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2062. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2063. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2064. );
  2065. FailException(E);
  2066. end;
  2067. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2068. var
  2069. P: TPasSourcePos;
  2070. begin
  2071. if IsErrorExpected(E) then exit;
  2072. P:=E.SourcePos;
  2073. WriteSources(P.FileName,P.Row,P.Column);
  2074. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2075. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2076. FailException(E);
  2077. end;
  2078. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2079. var
  2080. Row, Col: integer;
  2081. begin
  2082. if IsErrorExpected(E) then exit;
  2083. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2084. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2085. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2086. +' '+E.PasElement.SourceFilename
  2087. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2088. FailException(E);
  2089. end;
  2090. procedure TCustomTestModule.HandleException(E: Exception);
  2091. begin
  2092. if E is EScannerError then
  2093. HandleScannerError(EScannerError(E))
  2094. else if E is EParserError then
  2095. HandleParserError(EParserError(E))
  2096. else if E is EPasResolve then
  2097. HandlePasResolveError(EPasResolve(E))
  2098. else if E is EPas2JS then
  2099. HandlePas2JSError(EPas2JS(E))
  2100. else
  2101. begin
  2102. if IsErrorExpected(E) then exit;
  2103. if not (E is EAssertionFailedError) then
  2104. begin
  2105. WriteSources('',0,0);
  2106. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2107. end;
  2108. FailException(E);
  2109. end;
  2110. end;
  2111. procedure TCustomTestModule.FailException(E: Exception);
  2112. var
  2113. MsgNumber: Integer;
  2114. begin
  2115. if ExpectedErrorClass<>nil then
  2116. begin
  2117. if FExpectedErrorClass=E.ClassType then
  2118. begin
  2119. if E is EPas2JS then
  2120. MsgNumber:=EPas2JS(E).MsgNumber
  2121. else if E is EPasResolve then
  2122. MsgNumber:=EPasResolve(E).MsgNumber
  2123. else if E is EParserError then
  2124. MsgNumber:=Parser.LastMsgNumber
  2125. else if E is EScannerError then
  2126. MsgNumber:=Scanner.LastMsgNumber
  2127. else
  2128. MsgNumber:=0;
  2129. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2130. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2131. ExpectedErrorNumber,MsgNumber);
  2132. end else begin
  2133. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2134. end;
  2135. end;
  2136. Fail(E.Message);
  2137. end;
  2138. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2139. aCol: integer);
  2140. var
  2141. IsSrc: Boolean;
  2142. i, j: Integer;
  2143. SrcLines: TStringList;
  2144. Line: string;
  2145. aModule: TTestEnginePasResolver;
  2146. begin
  2147. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2148. for i:=0 to ResolverCount-1 do
  2149. begin
  2150. aModule:=Resolvers[i];
  2151. SrcLines:=TStringList.Create;
  2152. try
  2153. SrcLines.Text:=aModule.Source;
  2154. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2155. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2156. for j:=1 to SrcLines.Count do
  2157. begin
  2158. Line:=SrcLines[j-1];
  2159. if IsSrc and (j=aRow) then
  2160. begin
  2161. write('*');
  2162. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2163. end;
  2164. writeln(Format('%:4d: ',[j]),Line);
  2165. end;
  2166. finally
  2167. SrcLines.Free;
  2168. end;
  2169. end;
  2170. end;
  2171. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2172. var
  2173. i: Integer;
  2174. begin
  2175. for i:=0 to ResolverCount-1 do
  2176. if Filename=Resolvers[i].Filename then exit(i);
  2177. Result:=-1;
  2178. end;
  2179. function TCustomTestModule.GetResolver(const Filename: string
  2180. ): TTestEnginePasResolver;
  2181. var
  2182. i: Integer;
  2183. begin
  2184. i:=IndexOfResolver(Filename);
  2185. if i<0 then exit(nil);
  2186. Result:=Resolvers[i];
  2187. end;
  2188. function TCustomTestModule.GetDefaultNamespace: string;
  2189. var
  2190. C: TClass;
  2191. begin
  2192. Result:='';
  2193. if FModule=nil then exit;
  2194. C:=FModule.ClassType;
  2195. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2196. Result:=Engine.DefaultNameSpace;
  2197. end;
  2198. constructor TCustomTestModule.Create;
  2199. begin
  2200. inherited Create;
  2201. FHintMsgs:=TObjectList.Create(true);
  2202. FHintMsgsGood:=TFPList.Create;
  2203. end;
  2204. destructor TCustomTestModule.Destroy;
  2205. begin
  2206. FreeAndNil(FHintMsgs);
  2207. FreeAndNil(FHintMsgsGood);
  2208. inherited Destroy;
  2209. end;
  2210. { TTestModule }
  2211. procedure TTestModule.TestReservedWords;
  2212. var
  2213. i: integer;
  2214. begin
  2215. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2216. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2217. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2218. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2219. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2220. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2221. end;
  2222. procedure TTestModule.TestEmptyProgram;
  2223. begin
  2224. StartProgram(false);
  2225. Add('begin');
  2226. ConvertProgram;
  2227. CheckSource('TestEmptyProgram','','');
  2228. end;
  2229. procedure TTestModule.TestEmptyProgramUseStrict;
  2230. begin
  2231. Converter.Options:=Converter.Options+[coUseStrict];
  2232. StartProgram(false);
  2233. Add('begin');
  2234. ConvertProgram;
  2235. CheckSource('TestEmptyProgramUseStrict','','');
  2236. end;
  2237. procedure TTestModule.TestEmptyUnit;
  2238. begin
  2239. StartUnit(false);
  2240. Add('interface');
  2241. Add('implementation');
  2242. ConvertUnit;
  2243. CheckSource('TestEmptyUnit',
  2244. LinesToStr([
  2245. ]),
  2246. '');
  2247. end;
  2248. procedure TTestModule.TestEmptyUnitUseStrict;
  2249. begin
  2250. Converter.Options:=Converter.Options+[coUseStrict];
  2251. StartUnit(false);
  2252. Add('interface');
  2253. Add('implementation');
  2254. ConvertUnit;
  2255. CheckSource('TestEmptyUnitUseStrict',
  2256. LinesToStr([
  2257. ''
  2258. ]),
  2259. '');
  2260. end;
  2261. procedure TTestModule.TestDottedUnitNames;
  2262. begin
  2263. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2264. LinesToStr([
  2265. 'var iV: longint;'
  2266. ]),
  2267. '');
  2268. FFilename:='ns1.test1.pp';
  2269. StartProgram(true);
  2270. Add('uses unIt2;');
  2271. Add('var');
  2272. Add(' i: longint;');
  2273. Add('begin');
  2274. Add(' i:=iv;');
  2275. Add(' i:=uNit2.iv;');
  2276. Add(' i:=Ns1.TEst1.i;');
  2277. ConvertProgram;
  2278. CheckSource('TestDottedUnitNames',
  2279. LinesToStr([
  2280. 'this.i = 0;',
  2281. '']),
  2282. LinesToStr([ // this.$init
  2283. '$mod.i = pas["NS1.Unit2"].iV;',
  2284. '$mod.i = pas["NS1.Unit2"].iV;',
  2285. '$mod.i = $mod.i;',
  2286. '']) );
  2287. end;
  2288. procedure TTestModule.TestDottedUnitNameImpl;
  2289. begin
  2290. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2291. LinesToStr([
  2292. 'type',
  2293. ' TObject = class end;',
  2294. ' TTestA = class',
  2295. ' end;'
  2296. ]),
  2297. LinesToStr(['uses TEST.UnitB;'])
  2298. );
  2299. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2300. LinesToStr([
  2301. 'uses TEST.UnitA;',
  2302. 'type TTestB = class(TTestA);'
  2303. ]),
  2304. ''
  2305. );
  2306. StartProgram(true);
  2307. Add('uses TEST.UnitA;');
  2308. Add('begin');
  2309. ConvertProgram;
  2310. CheckSource('TestDottedUnitNameImpl',
  2311. LinesToStr([
  2312. '']),
  2313. LinesToStr([ // this.$init
  2314. '']) );
  2315. CheckUnit('TEST.UnitA.pas',
  2316. LinesToStr([
  2317. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2318. ' var $mod = this;',
  2319. ' rtl.createClass($mod, "TObject", null, function () {',
  2320. ' this.$init = function () {',
  2321. ' };',
  2322. ' this.$final = function () {',
  2323. ' };',
  2324. ' });',
  2325. ' rtl.createClass($mod, "TTestA", $mod.TObject, function () {',
  2326. ' });',
  2327. '}, ["TEST.UnitB"]);'
  2328. ]));
  2329. CheckUnit('TEST.UnitB.pas',
  2330. LinesToStr([
  2331. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2332. ' var $mod = this;',
  2333. ' rtl.createClass($mod, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2334. ' });',
  2335. '});'
  2336. ]));
  2337. end;
  2338. procedure TTestModule.TestDottedUnitExpr;
  2339. begin
  2340. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2341. LinesToStr([
  2342. 'procedure DoIt;'
  2343. ]),
  2344. 'procedure DoIt; begin end;');
  2345. FFilename:='Ns1.SubNs1.Test1.pp';
  2346. StartProgram(true);
  2347. Add('uses Ns2.sUbnS2.unIt2;');
  2348. Add('var');
  2349. Add(' i: longint;');
  2350. Add('begin');
  2351. Add(' ns2.subns2.unit2.doit;');
  2352. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2353. ConvertProgram;
  2354. CheckSource('TestDottedUnitExpr',
  2355. LinesToStr([
  2356. 'this.i = 0;',
  2357. '']),
  2358. LinesToStr([ // this.$init
  2359. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2360. '$mod.i = $mod.i;',
  2361. '']) );
  2362. end;
  2363. procedure TTestModule.Test_ModeFPCFail;
  2364. begin
  2365. StartProgram(false);
  2366. Add('{$mode FPC}');
  2367. Add('begin');
  2368. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2369. ConvertProgram;
  2370. end;
  2371. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2372. begin
  2373. StartProgram(false);
  2374. Add('{$modeswitch cblocks-}');
  2375. Add('begin');
  2376. ConvertProgram;
  2377. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2378. CheckResolverUnexpectedHints();
  2379. end;
  2380. procedure TTestModule.TestUnit_UseSystem;
  2381. begin
  2382. StartUnit(true);
  2383. Add([
  2384. 'interface',
  2385. 'var i: integer;',
  2386. 'implementation']);
  2387. ConvertUnit;
  2388. CheckSource('TestUnit_UseSystem',
  2389. LinesToStr([
  2390. 'this.i = 0;',
  2391. '']),
  2392. LinesToStr([
  2393. '']) );
  2394. end;
  2395. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2396. begin
  2397. AddModuleWithIntfImplSrc('unit1.pp',
  2398. LinesToStr([
  2399. 'type number = longint;']),
  2400. LinesToStr([
  2401. 'uses test1;',
  2402. 'procedure DoIt;',
  2403. 'begin',
  2404. ' i:=3;',
  2405. 'end;']));
  2406. StartUnit(true);
  2407. Add([
  2408. 'interface',
  2409. 'uses unit1;',
  2410. 'var i: number;',
  2411. 'implementation']);
  2412. ConvertUnit;
  2413. CheckSource('TestUnit_Intf1Impl2Intf1',
  2414. LinesToStr([
  2415. 'this.i = 0;',
  2416. '']),
  2417. LinesToStr([
  2418. '']) );
  2419. end;
  2420. procedure TTestModule.TestIncludeVersion;
  2421. begin
  2422. StartProgram(false);
  2423. Add([
  2424. 'var',
  2425. ' s: string;',
  2426. ' i: word;',
  2427. 'begin',
  2428. ' s:={$I %line%};',
  2429. ' i:={$I %linenum%};',
  2430. ' s:={$I %currentroutine%};',
  2431. ' s:={$I %pas2jsversion%};',
  2432. ' s:={$I %pas2jstarget%};',
  2433. ' s:={$I %pas2jstargetos%};',
  2434. ' s:={$I %pas2jstargetcpu%};',
  2435. ' s:={$I %file%};',
  2436. '']);
  2437. ConvertProgram;
  2438. CheckSource('TestIncludeVersion',
  2439. LinesToStr([
  2440. 'this.s="";',
  2441. 'this.i = 0;']),
  2442. LinesToStr([
  2443. '$mod.s = "7";',
  2444. '$mod.i = 8;',
  2445. '$mod.s = "<anonymous>";',
  2446. '$mod.s = "Comp.Ver.tcmodules";',
  2447. '$mod.s = "Browser";',
  2448. '$mod.s = "Browser";',
  2449. '$mod.s = "ECMAScript5";',
  2450. '$mod.s = "test1.pp";',
  2451. '']));
  2452. end;
  2453. procedure TTestModule.TestVarInt;
  2454. begin
  2455. StartProgram(false);
  2456. Add('var MyI: longint;');
  2457. Add('begin');
  2458. ConvertProgram;
  2459. CheckSource('TestVarInt','this.MyI=0;','');
  2460. end;
  2461. procedure TTestModule.TestVarBaseTypes;
  2462. begin
  2463. StartProgram(false);
  2464. Add('var');
  2465. Add(' i: longint;');
  2466. Add(' s: string;');
  2467. Add(' c: char;');
  2468. Add(' b: boolean;');
  2469. Add(' d: double;');
  2470. Add(' i2: longint = 3;');
  2471. Add(' s2: string = ''foo'';');
  2472. Add(' c2: char = ''4'';');
  2473. Add(' b2: boolean = true;');
  2474. Add(' d2: double = 5.6;');
  2475. Add(' i3: longint = $707;');
  2476. Add(' i4: nativeint = 9007199254740991;');
  2477. Add(' i5: nativeint = -9007199254740991-1;');
  2478. Add(' i6: nativeint = $fffffffffffff;');
  2479. Add(' i7: nativeint = -$fffffffffffff-1;');
  2480. Add(' i8: byte = 00;');
  2481. Add(' u8: nativeuint = $fffffffffffff;');
  2482. Add(' u9: nativeuint = $0000000000000;');
  2483. Add(' u10: nativeuint = $00ff00;');
  2484. Add('begin');
  2485. ConvertProgram;
  2486. CheckSource('TestVarBaseTypes',
  2487. LinesToStr([
  2488. 'this.i = 0;',
  2489. 'this.s = "";',
  2490. 'this.c = "";',
  2491. 'this.b = false;',
  2492. 'this.d = 0.0;',
  2493. 'this.i2 = 3;',
  2494. 'this.s2 = "foo";',
  2495. 'this.c2 = "4";',
  2496. 'this.b2 = true;',
  2497. 'this.d2 = 5.6;',
  2498. 'this.i3 = 0x707;',
  2499. 'this.i4 = 9007199254740991;',
  2500. 'this.i5 = -9007199254740991-1;',
  2501. 'this.i6 = 0xfffffffffffff;',
  2502. 'this.i7 =-0xfffffffffffff-1;',
  2503. 'this.i8 = 0;',
  2504. 'this.u8 = 0xfffffffffffff;',
  2505. 'this.u9 = 0x0;',
  2506. 'this.u10 = 0xff00;'
  2507. ]),
  2508. '');
  2509. end;
  2510. procedure TTestModule.TestBaseTypeSingleFail;
  2511. begin
  2512. StartProgram(false);
  2513. Add('var s: single;');
  2514. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2515. ConvertProgram;
  2516. end;
  2517. procedure TTestModule.TestBaseTypeExtendedFail;
  2518. begin
  2519. StartProgram(false);
  2520. Add('var e: extended;');
  2521. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2522. ConvertProgram;
  2523. end;
  2524. procedure TTestModule.TestConstBaseTypes;
  2525. begin
  2526. StartProgram(false);
  2527. Add('const');
  2528. Add(' i: longint = 3;');
  2529. Add(' s: string = ''foo'';');
  2530. Add(' c: char = ''4'';');
  2531. Add(' b: boolean = true;');
  2532. Add(' d: double = 5.6;');
  2533. Add(' e = low(word);');
  2534. Add(' f = high(word);');
  2535. Add('begin');
  2536. ConvertProgram;
  2537. CheckSource('TestVarBaseTypes',
  2538. LinesToStr([
  2539. 'this.i=3;',
  2540. 'this.s="foo";',
  2541. 'this.c="4";',
  2542. 'this.b=true;',
  2543. 'this.d=5.6;',
  2544. 'this.e = 0;',
  2545. 'this.f = 65535;'
  2546. ]),
  2547. '');
  2548. end;
  2549. procedure TTestModule.TestAliasTypeRef;
  2550. begin
  2551. StartProgram(false);
  2552. Add('type');
  2553. Add(' a=longint;');
  2554. Add(' b=a;');
  2555. Add('var');
  2556. Add(' c: A;');
  2557. Add(' d: B;');
  2558. Add('begin');
  2559. ConvertProgram;
  2560. CheckSource('TestAliasTypeRef',
  2561. LinesToStr([ // statements
  2562. 'this.c = 0;',
  2563. 'this.d = 0;'
  2564. ]),
  2565. LinesToStr([ // this.$main
  2566. ''
  2567. ]));
  2568. end;
  2569. procedure TTestModule.TestTypeCast_BaseTypes;
  2570. begin
  2571. StartProgram(false);
  2572. Add([
  2573. 'var',
  2574. ' i: longint;',
  2575. ' b: boolean;',
  2576. ' d: double;',
  2577. ' s: string;',
  2578. ' c: char;',
  2579. 'begin',
  2580. ' i:=longint(i);',
  2581. ' i:=longint(b);',
  2582. ' b:=boolean(b);',
  2583. ' b:=boolean(i);',
  2584. ' d:=double(d);',
  2585. ' d:=double(i);',
  2586. ' s:=string(s);',
  2587. ' s:=string(c);',
  2588. ' c:=char(c);',
  2589. ' c:=char(i);',
  2590. ' c:=char(65);',
  2591. ' c:=char(#10);',
  2592. ' c:=char(#$E000);',
  2593. '']);
  2594. ConvertProgram;
  2595. CheckSource('TestAliasTypeRef',
  2596. LinesToStr([ // statements
  2597. 'this.i = 0;',
  2598. 'this.b = false;',
  2599. 'this.d = 0.0;',
  2600. 'this.s = "";',
  2601. 'this.c = "";',
  2602. '']),
  2603. LinesToStr([ // this.$main
  2604. '$mod.i = $mod.i;',
  2605. '$mod.i = ($mod.b ? 1 : 0);',
  2606. '$mod.b = $mod.b;',
  2607. '$mod.b = $mod.i != 0;',
  2608. '$mod.d = $mod.d;',
  2609. '$mod.d = $mod.i;',
  2610. '$mod.s = $mod.s;',
  2611. '$mod.s = $mod.c;',
  2612. '$mod.c = $mod.c;',
  2613. '$mod.c = String.fromCharCode($mod.i);',
  2614. '$mod.c = "A";',
  2615. '$mod.c = "\n";',
  2616. '$mod.c = "";',
  2617. '']));
  2618. end;
  2619. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2620. begin
  2621. StartProgram(false);
  2622. Add('type');
  2623. Add(' integer = longint;');
  2624. Add(' TYesNo = boolean;');
  2625. Add(' TFloat = double;');
  2626. Add(' TCaption = string;');
  2627. Add(' TChar = char;');
  2628. Add('var');
  2629. Add(' i: integer;');
  2630. Add(' b: TYesNo;');
  2631. Add(' d: TFloat;');
  2632. Add(' s: TCaption;');
  2633. Add(' c: TChar;');
  2634. Add('begin');
  2635. Add(' i:=integer(i);');
  2636. Add(' i:=integer(b);');
  2637. Add(' b:=TYesNo(b);');
  2638. Add(' b:=TYesNo(i);');
  2639. Add(' d:=TFloat(d);');
  2640. Add(' d:=TFloat(i);');
  2641. Add(' s:=TCaption(s);');
  2642. Add(' s:=TCaption(c);');
  2643. Add(' c:=TChar(c);');
  2644. ConvertProgram;
  2645. CheckSource('TestAliasTypeRef',
  2646. LinesToStr([ // statements
  2647. 'this.i = 0;',
  2648. 'this.b = false;',
  2649. 'this.d = 0.0;',
  2650. 'this.s = "";',
  2651. 'this.c = "";',
  2652. '']),
  2653. LinesToStr([ // this.$main
  2654. '$mod.i = $mod.i;',
  2655. '$mod.i = ($mod.b ? 1 : 0);',
  2656. '$mod.b = $mod.b;',
  2657. '$mod.b = $mod.i != 0;',
  2658. '$mod.d = $mod.d;',
  2659. '$mod.d = $mod.i;',
  2660. '$mod.s = $mod.s;',
  2661. '$mod.s = $mod.c;',
  2662. '$mod.c = $mod.c;',
  2663. '']));
  2664. end;
  2665. procedure TTestModule.TestEmptyProc;
  2666. begin
  2667. StartProgram(false);
  2668. Add('procedure Test;');
  2669. Add('begin');
  2670. Add('end;');
  2671. Add('begin');
  2672. ConvertProgram;
  2673. CheckSource('TestEmptyProc',
  2674. LinesToStr([ // statements
  2675. 'this.Test = function () {',
  2676. '};'
  2677. ]),
  2678. LinesToStr([ // this.$main
  2679. ''
  2680. ]));
  2681. end;
  2682. procedure TTestModule.TestProcOneParam;
  2683. begin
  2684. StartProgram(false);
  2685. Add('procedure ProcA(i: longint);');
  2686. Add('begin');
  2687. Add('end;');
  2688. Add('begin');
  2689. Add(' PROCA(3);');
  2690. ConvertProgram;
  2691. CheckSource('TestProcOneParam',
  2692. LinesToStr([ // statements
  2693. 'this.ProcA = function (i) {',
  2694. '};'
  2695. ]),
  2696. LinesToStr([ // this.$main
  2697. '$mod.ProcA(3);'
  2698. ]));
  2699. end;
  2700. procedure TTestModule.TestFunctionWithoutParams;
  2701. begin
  2702. StartProgram(false);
  2703. Add('function FuncA: longint;');
  2704. Add('begin');
  2705. Add('end;');
  2706. Add('var i: longint;');
  2707. Add('begin');
  2708. Add(' I:=FUNCA();');
  2709. Add(' I:=FUNCA;');
  2710. Add(' FUNCA();');
  2711. Add(' FUNCA;');
  2712. ConvertProgram;
  2713. CheckSource('TestProcWithoutParams',
  2714. LinesToStr([ // statements
  2715. 'this.FuncA = function () {',
  2716. ' var Result = 0;',
  2717. ' return Result;',
  2718. '};',
  2719. 'this.i=0;'
  2720. ]),
  2721. LinesToStr([ // this.$main
  2722. '$mod.i=$mod.FuncA();',
  2723. '$mod.i=$mod.FuncA();',
  2724. '$mod.FuncA();',
  2725. '$mod.FuncA();'
  2726. ]));
  2727. end;
  2728. procedure TTestModule.TestProcedureWithoutParams;
  2729. begin
  2730. StartProgram(false);
  2731. Add('procedure ProcA;');
  2732. Add('begin');
  2733. Add('end;');
  2734. Add('begin');
  2735. Add(' PROCA();');
  2736. Add(' PROCA;');
  2737. ConvertProgram;
  2738. CheckSource('TestProcWithoutParams',
  2739. LinesToStr([ // statements
  2740. 'this.ProcA = function () {',
  2741. '};'
  2742. ]),
  2743. LinesToStr([ // this.$main
  2744. '$mod.ProcA();',
  2745. '$mod.ProcA();'
  2746. ]));
  2747. end;
  2748. procedure TTestModule.TestIncDec;
  2749. begin
  2750. StartProgram(false);
  2751. Add([
  2752. 'procedure DoIt(var i: longint);',
  2753. 'begin',
  2754. ' inc(i);',
  2755. ' inc(i,2);',
  2756. 'end;',
  2757. 'var',
  2758. ' Bar: longint;',
  2759. 'begin',
  2760. ' inc(bar);',
  2761. ' inc(bar,2);',
  2762. ' dec(bar);',
  2763. ' dec(bar,3);',
  2764. '']);
  2765. ConvertProgram;
  2766. CheckSource('TestIncDec',
  2767. LinesToStr([ // statements
  2768. 'this.DoIt = function (i) {',
  2769. ' i.set(i.get()+1);',
  2770. ' i.set(i.get()+2);',
  2771. '};',
  2772. 'this.Bar = 0;'
  2773. ]),
  2774. LinesToStr([ // this.$main
  2775. '$mod.Bar+=1;',
  2776. '$mod.Bar+=2;',
  2777. '$mod.Bar-=1;',
  2778. '$mod.Bar-=3;'
  2779. ]));
  2780. end;
  2781. procedure TTestModule.TestLoHiFpcMode;
  2782. begin
  2783. StartProgram(false);
  2784. Add([
  2785. '{$mode objfpc}',
  2786. 'const',
  2787. ' LoByte1 = Lo(Word($1234));',
  2788. ' HiByte1 = Hi(Word($1234));',
  2789. ' LoByte2 = Lo(SmallInt($1234));',
  2790. ' HiByte2 = Hi(SmallInt($1234));',
  2791. ' LoWord1 = Lo($1234CDEF);',
  2792. ' HiWord1 = Hi($1234CDEF);',
  2793. ' LoWord2 = Lo(-$1234CDEF);',
  2794. ' HiWord2 = Hi(-$1234CDEF);',
  2795. ' lo4:byte=lo(byte($34));',
  2796. ' hi4:byte=hi(byte($34));',
  2797. ' lo5:byte=lo(shortint(-$34));',
  2798. ' hi5:byte=hi(shortint(-$34));',
  2799. ' lo6:longword=lo($123456789ABCD);',
  2800. ' hi6:longword=hi($123456789ABCD);',
  2801. ' lo7:longword=lo(-$123456789ABCD);',
  2802. ' hi7:longword=hi(-$123456789ABCD);',
  2803. 'var',
  2804. ' b: Byte;',
  2805. ' ss: shortint;',
  2806. ' w: Word;',
  2807. ' si: SmallInt;',
  2808. ' lw: LongWord;',
  2809. ' li: LongInt;',
  2810. ' b2: Byte;',
  2811. ' ni: nativeint;',
  2812. 'begin',
  2813. ' w := $1234;',
  2814. ' ss := -$12;',
  2815. ' b := lo(ss);',
  2816. ' b := HI(ss);',
  2817. ' b := lo(w);',
  2818. ' b := HI(w);',
  2819. ' b2 := lo(b);',
  2820. ' b2 := hi(b);',
  2821. ' lw := $1234CDEF;',
  2822. ' w := lo(lw);',
  2823. ' w := hi(lw);',
  2824. ' ni := $123456789ABCD;',
  2825. ' lw := lo(ni);',
  2826. ' lw := hi(ni);',
  2827. '']);
  2828. ConvertProgram;
  2829. CheckSource('TestLoHiFpcMode',
  2830. LinesToStr([ // statements
  2831. 'this.LoByte1 = 0x1234 & 0xFF;',
  2832. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2833. 'this.LoByte2 = 0x1234 & 0xFF;',
  2834. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2835. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2836. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2837. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2838. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2839. 'this.lo4 = 0x34 & 0xF;',
  2840. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2841. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2842. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2843. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2844. 'this.hi6 = 74565 >>> 0;',
  2845. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2846. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2847. 'this.b = 0;',
  2848. 'this.ss = 0;',
  2849. 'this.w = 0;',
  2850. 'this.si = 0;',
  2851. 'this.lw = 0;',
  2852. 'this.li = 0;',
  2853. 'this.b2 = 0;',
  2854. 'this.ni = 0;',
  2855. '']),
  2856. LinesToStr([ // this.$main
  2857. '$mod.w = 0x1234;',
  2858. '$mod.ss = -0x12;',
  2859. '$mod.b = $mod.ss & 0xFF;',
  2860. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2861. '$mod.b = $mod.w & 0xFF;',
  2862. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2863. '$mod.b2 = $mod.b & 0xF;',
  2864. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2865. '$mod.lw = 0x1234CDEF;',
  2866. '$mod.w = $mod.lw & 0xFFFF;',
  2867. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2868. '$mod.ni = 0x123456789ABCD;',
  2869. '$mod.lw = $mod.ni >>> 0;',
  2870. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2871. '']));
  2872. end;
  2873. procedure TTestModule.TestLoHiDelphiMode;
  2874. begin
  2875. StartProgram(false);
  2876. Add([
  2877. '{$mode delphi}',
  2878. 'const',
  2879. ' LoByte1 = Lo(Word($1234));',
  2880. ' HiByte1 = Hi(Word($1234));',
  2881. ' LoByte2 = Lo(SmallInt($1234));',
  2882. ' HiByte2 = Hi(SmallInt($1234));',
  2883. ' LoByte3 = Lo($1234CDEF);',
  2884. ' HiByte3 = Hi($1234CDEF);',
  2885. ' LoByte4 = Lo(-$1234CDEF);',
  2886. ' HiByte4 = Hi(-$1234CDEF);',
  2887. 'var',
  2888. ' b: Byte;',
  2889. ' w: Word;',
  2890. ' si: SmallInt;',
  2891. ' lw: LongWord;',
  2892. ' li: LongInt;',
  2893. 'begin',
  2894. ' w := $1234;',
  2895. ' b := lo(w);',
  2896. ' b := HI(w);',
  2897. ' lw := $1234CDEF;',
  2898. ' b := lo(lw);',
  2899. ' b := hi(lw);',
  2900. '']);
  2901. ConvertProgram;
  2902. CheckSource('TestLoHiDelphiMode',
  2903. LinesToStr([ // statements
  2904. 'this.LoByte1 = 0x1234 & 0xFF;',
  2905. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2906. 'this.LoByte2 = 0x1234 & 0xFF;',
  2907. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2908. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2909. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2910. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2911. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2912. 'this.b = 0;',
  2913. 'this.w = 0;',
  2914. 'this.si = 0;',
  2915. 'this.lw = 0;',
  2916. 'this.li = 0;'
  2917. ]),
  2918. LinesToStr([ // this.$main
  2919. '$mod.w = 0x1234;',
  2920. '$mod.b = $mod.w & 0xFF;',
  2921. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2922. '$mod.lw = 0x1234CDEF;',
  2923. '$mod.b = $mod.lw & 0xFF;',
  2924. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  2925. ]));
  2926. end;
  2927. procedure TTestModule.TestAssignments;
  2928. begin
  2929. StartProgram(false);
  2930. Parser.Options:=Parser.Options+[po_cassignments];
  2931. Add('var');
  2932. Add(' Bar:longint;');
  2933. Add('begin');
  2934. Add(' bar:=3;');
  2935. Add(' bar+=4;');
  2936. Add(' bar-=5;');
  2937. Add(' bar*=6;');
  2938. ConvertProgram;
  2939. CheckSource('TestAssignments',
  2940. LinesToStr([ // statements
  2941. 'this.Bar = 0;'
  2942. ]),
  2943. LinesToStr([ // this.$main
  2944. '$mod.Bar=3;',
  2945. '$mod.Bar+=4;',
  2946. '$mod.Bar-=5;',
  2947. '$mod.Bar*=6;'
  2948. ]));
  2949. end;
  2950. procedure TTestModule.TestArithmeticOperators1;
  2951. begin
  2952. StartProgram(false);
  2953. Add('var');
  2954. Add(' vA,vB,vC:longint;');
  2955. Add('begin');
  2956. Add(' va:=1;');
  2957. Add(' vb:=va+va;');
  2958. Add(' vb:=va div vb;');
  2959. Add(' vb:=va mod vb;');
  2960. Add(' vb:=va+va*vb+va div vb;');
  2961. Add(' vc:=-va;');
  2962. Add(' va:=va-vb;');
  2963. Add(' vb:=va;');
  2964. Add(' if va<vb then vc:=va else vc:=vb;');
  2965. ConvertProgram;
  2966. CheckSource('TestArithmeticOperators1',
  2967. LinesToStr([ // statements
  2968. 'this.vA = 0;',
  2969. 'this.vB = 0;',
  2970. 'this.vC = 0;'
  2971. ]),
  2972. LinesToStr([ // this.$main
  2973. '$mod.vA = 1;',
  2974. '$mod.vB = $mod.vA + $mod.vA;',
  2975. '$mod.vB = Math.floor($mod.vA / $mod.vB);',
  2976. '$mod.vB = $mod.vA % $mod.vB;',
  2977. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + Math.floor($mod.vA / $mod.vB);',
  2978. '$mod.vC = -$mod.vA;',
  2979. '$mod.vA = $mod.vA - $mod.vB;',
  2980. '$mod.vB = $mod.vA;',
  2981. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  2982. ]));
  2983. end;
  2984. procedure TTestModule.TestLogicalOperators;
  2985. begin
  2986. StartProgram(false);
  2987. Add('var');
  2988. Add(' vA,vB,vC:boolean;');
  2989. Add('begin');
  2990. Add(' va:=vb and vc;');
  2991. Add(' va:=vb or vc;');
  2992. Add(' va:=vb xor vc;');
  2993. Add(' va:=true and vc;');
  2994. Add(' va:=(vb and vc) or (va and vb);');
  2995. Add(' va:=not vb;');
  2996. ConvertProgram;
  2997. CheckSource('TestLogicalOperators',
  2998. LinesToStr([ // statements
  2999. 'this.vA = false;',
  3000. 'this.vB = false;',
  3001. 'this.vC = false;'
  3002. ]),
  3003. LinesToStr([ // this.$main
  3004. '$mod.vA = $mod.vB && $mod.vC;',
  3005. '$mod.vA = $mod.vB || $mod.vC;',
  3006. '$mod.vA = $mod.vB ^ $mod.vC;',
  3007. '$mod.vA = true && $mod.vC;',
  3008. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3009. '$mod.vA = !$mod.vB;'
  3010. ]));
  3011. end;
  3012. procedure TTestModule.TestBitwiseOperators;
  3013. begin
  3014. StartProgram(false);
  3015. Add([
  3016. 'var',
  3017. ' vA,vB,vC:longint;',
  3018. ' X,Y,Z: nativeint;',
  3019. 'begin',
  3020. ' va:=vb and vc;',
  3021. ' va:=vb or vc;',
  3022. ' va:=vb xor vc;',
  3023. ' va:=vb shl vc;',
  3024. ' va:=vb shr vc;',
  3025. ' va:=3 and vc;',
  3026. ' va:=(vb and vc) or (va and vb);',
  3027. ' va:=not vb;',
  3028. ' X:=Y and Z;',
  3029. ' X:=Y and va;',
  3030. ' X:=Y or Z;',
  3031. ' X:=Y or va;',
  3032. ' X:=Y xor Z;',
  3033. ' X:=Y xor va;',
  3034. '']);
  3035. ConvertProgram;
  3036. CheckSource('TestBitwiseOperators',
  3037. LinesToStr([ // statements
  3038. 'this.vA = 0;',
  3039. 'this.vB = 0;',
  3040. 'this.vC = 0;',
  3041. 'this.X = 0;',
  3042. 'this.Y = 0;',
  3043. 'this.Z = 0;',
  3044. '']),
  3045. LinesToStr([ // this.$main
  3046. '$mod.vA = $mod.vB & $mod.vC;',
  3047. '$mod.vA = $mod.vB | $mod.vC;',
  3048. '$mod.vA = $mod.vB ^ $mod.vC;',
  3049. '$mod.vA = $mod.vB << $mod.vC;',
  3050. '$mod.vA = $mod.vB >>> $mod.vC;',
  3051. '$mod.vA = 3 & $mod.vC;',
  3052. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3053. '$mod.vA = ~$mod.vB;',
  3054. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3055. '$mod.X = $mod.Y & $mod.vA;',
  3056. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3057. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3058. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3059. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3060. '']));
  3061. end;
  3062. procedure TTestModule.TestBitwiseOperatorsLongword;
  3063. begin
  3064. StartProgram(false);
  3065. Add([
  3066. 'var',
  3067. ' a,b,c:longword;',
  3068. ' i: longint;',
  3069. 'begin',
  3070. ' a:=$12345678;',
  3071. ' b:=$EDCBA987;',
  3072. ' c:=not a;',
  3073. ' c:=a and b;',
  3074. ' c:=a and $ffff0000;',
  3075. ' c:=a or b;',
  3076. ' c:=a or $ff00ff00;',
  3077. ' c:=a xor b;',
  3078. ' c:=a xor $f0f0f0f0;',
  3079. ' c:=a shl 1;',
  3080. ' c:=a shl 16;',
  3081. ' c:=a shl 24;',
  3082. ' c:=a shl b;',
  3083. ' c:=a shr 1;',
  3084. ' c:=a shr 16;',
  3085. ' c:=a shr 24;',
  3086. ' c:=a shr b;',
  3087. ' c:=(b and c) or (a and b);',
  3088. ' c:=i and a;',
  3089. ' c:=i or a;',
  3090. ' c:=i xor a;',
  3091. '']);
  3092. ConvertProgram;
  3093. CheckSource('TestBitwiseOperatorsLongword',
  3094. LinesToStr([ // statements
  3095. 'this.a = 0;',
  3096. 'this.b = 0;',
  3097. 'this.c = 0;',
  3098. 'this.i = 0;',
  3099. '']),
  3100. LinesToStr([ // this.$main
  3101. '$mod.a = 0x12345678;',
  3102. '$mod.b = 0xEDCBA987;',
  3103. '$mod.c = rtl.lw(~$mod.a);',
  3104. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3105. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3106. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3107. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3108. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3109. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3110. '$mod.c = rtl.lw($mod.a << 1);',
  3111. '$mod.c = rtl.lw($mod.a << 16);',
  3112. '$mod.c = rtl.lw($mod.a << 24);',
  3113. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3114. '$mod.c = rtl.lw($mod.a >>> 1);',
  3115. '$mod.c = rtl.lw($mod.a >>> 16);',
  3116. '$mod.c = rtl.lw($mod.a >>> 24);',
  3117. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3118. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3119. '$mod.c = $mod.i & $mod.a;',
  3120. '$mod.c = $mod.i | $mod.a;',
  3121. '$mod.c = $mod.i ^ $mod.a;',
  3122. '']));
  3123. end;
  3124. procedure TTestModule.TestPrgProcVar;
  3125. begin
  3126. StartProgram(false);
  3127. Add('procedure Proc1;');
  3128. Add('type');
  3129. Add(' t1=longint;');
  3130. Add('var');
  3131. Add(' vA:t1;');
  3132. Add('begin');
  3133. Add('end;');
  3134. Add('begin');
  3135. ConvertProgram;
  3136. CheckSource('TestPrgProcVar',
  3137. LinesToStr([ // statements
  3138. 'this.Proc1 = function () {',
  3139. ' var vA=0;',
  3140. '};'
  3141. ]),
  3142. LinesToStr([ // this.$main
  3143. ''
  3144. ]));
  3145. end;
  3146. procedure TTestModule.TestUnitProcVar;
  3147. begin
  3148. StartUnit(false);
  3149. Add('interface');
  3150. Add('');
  3151. Add('type tA=string; // unit scope');
  3152. Add('procedure Proc1;');
  3153. Add('');
  3154. Add('implementation');
  3155. Add('');
  3156. Add('procedure Proc1;');
  3157. Add('type tA=longint; // local proc scope');
  3158. Add('var v1:tA; // using local tA');
  3159. Add('begin');
  3160. Add('end;');
  3161. Add('var v2:tA; // using interface tA');
  3162. ConvertUnit;
  3163. CheckSource('TestUnitProcVar',
  3164. LinesToStr([ // statements
  3165. 'var $impl = $mod.$impl;',
  3166. 'this.Proc1 = function () {',
  3167. ' var v1 = 0;',
  3168. '};',
  3169. '']),
  3170. // this.$init
  3171. '',
  3172. // implementation
  3173. LinesToStr([
  3174. '$impl.v2 = "";',
  3175. '']));
  3176. end;
  3177. procedure TTestModule.TestImplProc;
  3178. begin
  3179. StartUnit(false);
  3180. Add('interface');
  3181. Add('');
  3182. Add('procedure Proc1;');
  3183. Add('');
  3184. Add('implementation');
  3185. Add('');
  3186. Add('procedure Proc1; begin end;');
  3187. Add('procedure Proc2; begin end;');
  3188. Add('initialization');
  3189. Add(' Proc1;');
  3190. Add(' Proc2;');
  3191. ConvertUnit;
  3192. CheckSource('TestImplProc',
  3193. LinesToStr([ // statements
  3194. 'var $impl = $mod.$impl;',
  3195. 'this.Proc1 = function () {',
  3196. '};',
  3197. '']),
  3198. LinesToStr([ // this.$init
  3199. '$mod.Proc1();',
  3200. '$impl.Proc2();',
  3201. '']),
  3202. LinesToStr([ // implementation
  3203. '$impl.Proc2 = function () {',
  3204. '};',
  3205. ''])
  3206. );
  3207. end;
  3208. procedure TTestModule.TestFunctionResult;
  3209. begin
  3210. StartProgram(false);
  3211. Add('function Func1: longint;');
  3212. Add('begin');
  3213. Add(' Result:=3;');
  3214. Add(' Func1:=4;');
  3215. Add('end;');
  3216. Add('begin');
  3217. ConvertProgram;
  3218. CheckSource('TestFunctionResult',
  3219. LinesToStr([ // statements
  3220. 'this.Func1 = function () {',
  3221. ' var Result = 0;',
  3222. ' Result = 3;',
  3223. ' Result = 4;',
  3224. ' return Result;',
  3225. '};'
  3226. ]),
  3227. '');
  3228. end;
  3229. procedure TTestModule.TestNestedProc;
  3230. begin
  3231. StartProgram(false);
  3232. Add([
  3233. 'var vInUnit: longint;',
  3234. 'function DoIt(pA,pD: longint): longint;',
  3235. 'var',
  3236. ' vB: longint;',
  3237. ' vC: longint;',
  3238. ' function Nesty(pA: longint): longint; ',
  3239. ' var vB: longint;',
  3240. ' begin',
  3241. ' Result:=pa+vb+vc+pd+vInUnit;',
  3242. ' nesty:=3;',
  3243. ' doit:=4;',
  3244. ' exit;',
  3245. ' end;',
  3246. 'begin',
  3247. ' Result:=pa+vb+vc;',
  3248. ' doit:=6;',
  3249. ' exit;',
  3250. 'end;',
  3251. 'begin']);
  3252. ConvertProgram;
  3253. CheckSource('TestNestedProc',
  3254. LinesToStr([ // statements
  3255. 'this.vInUnit = 0;',
  3256. 'this.DoIt = function (pA, pD) {',
  3257. ' var Result = 0;',
  3258. ' var vB = 0;',
  3259. ' var vC = 0;',
  3260. ' function Nesty(pA) {',
  3261. ' var Result$1 = 0;',
  3262. ' var vB = 0;',
  3263. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3264. ' Result$1 = 3;',
  3265. ' Result = 4;',
  3266. ' return Result$1;',
  3267. ' return Result$1;',
  3268. ' };',
  3269. ' Result = pA + vB + vC;',
  3270. ' Result = 6;',
  3271. ' return Result;',
  3272. ' return Result;',
  3273. '};'
  3274. ]),
  3275. '');
  3276. end;
  3277. procedure TTestModule.TestNestedProc_ResultString;
  3278. begin
  3279. StartProgram(false);
  3280. Add([
  3281. 'function DoIt: string;',
  3282. ' function Nesty: string; ',
  3283. ' begin',
  3284. ' nesty:=#65#66;',
  3285. ' nesty[1]:=#67;',
  3286. ' doit:=#68;',
  3287. ' doit[2]:=#69;',
  3288. ' end;',
  3289. 'begin',
  3290. ' doit:=#70;',
  3291. ' doit[3]:=#71;',
  3292. 'end;',
  3293. 'begin']);
  3294. ConvertProgram;
  3295. CheckSource('TestNestedProc_ResultString',
  3296. LinesToStr([ // statements
  3297. 'this.DoIt = function () {',
  3298. ' var Result = "";',
  3299. ' function Nesty() {',
  3300. ' var Result$1 = "";',
  3301. ' Result$1 = "AB";',
  3302. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3303. ' Result = "D";',
  3304. ' Result = rtl.setCharAt(Result, 1, "E");',
  3305. ' return Result$1;',
  3306. ' };',
  3307. ' Result = "F";',
  3308. ' Result = rtl.setCharAt(Result, 2, "G");',
  3309. ' return Result;',
  3310. '};'
  3311. ]),
  3312. '');
  3313. end;
  3314. procedure TTestModule.TestForwardProc;
  3315. begin
  3316. StartProgram(false);
  3317. Add('procedure FuncA(Bar: longint); forward;');
  3318. Add('procedure FuncB(Bar: longint);');
  3319. Add('begin');
  3320. Add(' funca(bar);');
  3321. Add('end;');
  3322. Add('procedure funca(bar: longint);');
  3323. Add('begin');
  3324. Add(' if bar=3 then ;');
  3325. Add('end;');
  3326. Add('begin');
  3327. Add(' funca(4);');
  3328. Add(' funcb(5);');
  3329. ConvertProgram;
  3330. CheckSource('TestForwardProc',
  3331. LinesToStr([ // statements'
  3332. 'this.FuncB = function (Bar) {',
  3333. ' $mod.FuncA(Bar);',
  3334. '};',
  3335. 'this.FuncA = function (Bar) {',
  3336. ' if (Bar === 3);',
  3337. '};'
  3338. ]),
  3339. LinesToStr([
  3340. '$mod.FuncA(4);',
  3341. '$mod.FuncB(5);'
  3342. ])
  3343. );
  3344. end;
  3345. procedure TTestModule.TestNestedForwardProc;
  3346. begin
  3347. StartProgram(false);
  3348. Add('procedure FuncA;');
  3349. Add(' procedure FuncB(i: longint); forward;');
  3350. Add(' procedure FuncC(i: longint);');
  3351. Add(' begin');
  3352. Add(' funcb(i);');
  3353. Add(' end;');
  3354. Add(' procedure FuncB(i: longint);');
  3355. Add(' begin');
  3356. Add(' if i=3 then ;');
  3357. Add(' end;');
  3358. Add('begin');
  3359. Add(' funcc(4)');
  3360. Add('end;');
  3361. Add('begin');
  3362. Add(' funca;');
  3363. ConvertProgram;
  3364. CheckSource('TestNestedForwardProc',
  3365. LinesToStr([ // statements'
  3366. 'this.FuncA = function () {',
  3367. ' function FuncC(i) {',
  3368. ' FuncB(i);',
  3369. ' };',
  3370. ' function FuncB(i) {',
  3371. ' if (i === 3);',
  3372. ' };',
  3373. ' FuncC(4);',
  3374. '};'
  3375. ]),
  3376. LinesToStr([
  3377. '$mod.FuncA();'
  3378. ])
  3379. );
  3380. end;
  3381. procedure TTestModule.TestAssignFunctionResult;
  3382. begin
  3383. StartProgram(false);
  3384. Add('function Func1: longint;');
  3385. Add('begin');
  3386. Add('end;');
  3387. Add('var i: longint;');
  3388. Add('begin');
  3389. Add(' i:=func1();');
  3390. Add(' i:=func1()+func1();');
  3391. ConvertProgram;
  3392. CheckSource('TestAssignFunctionResult',
  3393. LinesToStr([ // statements
  3394. 'this.Func1 = function () {',
  3395. ' var Result = 0;',
  3396. ' return Result;',
  3397. '};',
  3398. 'this.i = 0;'
  3399. ]),
  3400. LinesToStr([
  3401. '$mod.i = $mod.Func1();',
  3402. '$mod.i = $mod.Func1() + $mod.Func1();'
  3403. ]));
  3404. end;
  3405. procedure TTestModule.TestFunctionResultInCondition;
  3406. begin
  3407. StartProgram(false);
  3408. Add('function Func1: longint;');
  3409. Add('begin');
  3410. Add('end;');
  3411. Add('function Func2: boolean;');
  3412. Add('begin');
  3413. Add('end;');
  3414. Add('var i: longint;');
  3415. Add('begin');
  3416. Add(' if func2 then ;');
  3417. Add(' if i=func1() then ;');
  3418. Add(' if i=func1 then ;');
  3419. ConvertProgram;
  3420. CheckSource('TestFunctionResultInCondition',
  3421. LinesToStr([ // statements
  3422. 'this.Func1 = function () {',
  3423. ' var Result = 0;',
  3424. ' return Result;',
  3425. '};',
  3426. 'this.Func2 = function () {',
  3427. ' var Result = false;',
  3428. ' return Result;',
  3429. '};',
  3430. 'this.i = 0;'
  3431. ]),
  3432. LinesToStr([
  3433. 'if ($mod.Func2());',
  3434. 'if ($mod.i === $mod.Func1());',
  3435. 'if ($mod.i === $mod.Func1());'
  3436. ]));
  3437. end;
  3438. procedure TTestModule.TestFunctionResultInForLoop;
  3439. begin
  3440. StartProgram(false);
  3441. Add([
  3442. 'function Func1(a: array of longint): longint;',
  3443. 'begin',
  3444. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3445. ' for Result in a do if a[Result]=0 then exit;',
  3446. 'end;',
  3447. 'begin',
  3448. ' Func1([1,2,3])']);
  3449. ConvertProgram;
  3450. CheckSource('TestFunctionResultInForLoop',
  3451. LinesToStr([ // statements
  3452. 'this.Func1 = function (a) {',
  3453. ' var Result = 0;',
  3454. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3455. ' Result = $l;',
  3456. ' if (a[Result] === 0) return Result;',
  3457. ' };',
  3458. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3459. ' Result = $in[$l1];',
  3460. ' if (a[Result] === 0) return Result;',
  3461. ' };',
  3462. ' return Result;',
  3463. '};',
  3464. '']),
  3465. LinesToStr([
  3466. '$mod.Func1([1, 2, 3]);'
  3467. ]));
  3468. end;
  3469. procedure TTestModule.TestFunctionResultInTypeCast;
  3470. begin
  3471. StartProgram(false);
  3472. Add([
  3473. 'function GetInt: longint;',
  3474. 'begin',
  3475. 'end;',
  3476. 'begin',
  3477. ' if Byte(GetInt)=0 then ;',
  3478. '']);
  3479. ConvertProgram;
  3480. CheckSource('TestFunctionResultInTypeCast',
  3481. LinesToStr([ // statements
  3482. 'this.GetInt = function () {',
  3483. ' var Result = 0;',
  3484. ' return Result;',
  3485. '};',
  3486. '']),
  3487. LinesToStr([
  3488. 'if (($mod.GetInt() & 255) === 0) ;'
  3489. ]));
  3490. end;
  3491. procedure TTestModule.TestExit;
  3492. begin
  3493. StartProgram(false);
  3494. Add('procedure ProcA;');
  3495. Add('begin');
  3496. Add(' exit;');
  3497. Add('end;');
  3498. Add('function FuncB: longint;');
  3499. Add('begin');
  3500. Add(' exit;');
  3501. Add(' exit(3);');
  3502. Add('end;');
  3503. Add('function FuncC: string;');
  3504. Add('begin');
  3505. Add(' exit;');
  3506. Add(' exit(''a'');');
  3507. Add(' exit(''abc'');');
  3508. Add('end;');
  3509. Add('begin');
  3510. Add(' exit;');
  3511. Add(' exit(1);');
  3512. ConvertProgram;
  3513. CheckSource('TestExit',
  3514. LinesToStr([ // statements
  3515. 'this.ProcA = function () {',
  3516. ' return;',
  3517. '};',
  3518. 'this.FuncB = function () {',
  3519. ' var Result = 0;',
  3520. ' return Result;',
  3521. ' return 3;',
  3522. ' return Result;',
  3523. '};',
  3524. 'this.FuncC = function () {',
  3525. ' var Result = "";',
  3526. ' return Result;',
  3527. ' return "a";',
  3528. ' return "abc";',
  3529. ' return Result;',
  3530. '};'
  3531. ]),
  3532. LinesToStr([
  3533. 'return;',
  3534. 'return 1;',
  3535. '']));
  3536. end;
  3537. procedure TTestModule.TestExit_ResultInFinally;
  3538. begin
  3539. StartProgram(false);
  3540. Add([
  3541. 'function Run: word;',
  3542. 'begin',
  3543. ' try',
  3544. ' exit(3);', // no Result in finally -> use return 3
  3545. ' finally',
  3546. ' end;',
  3547. 'end;',
  3548. 'function Fly: word;',
  3549. 'begin',
  3550. ' try',
  3551. ' exit(3);',
  3552. ' finally',
  3553. ' if Result>0 then ;',
  3554. ' end;',
  3555. 'end;',
  3556. 'function Jump: word;',
  3557. 'begin',
  3558. ' try',
  3559. ' try',
  3560. ' exit(4);',
  3561. ' finally',
  3562. ' end;',
  3563. ' finally',
  3564. ' if Result>0 then ;',
  3565. ' end;',
  3566. 'end;',
  3567. 'begin',
  3568. '']);
  3569. ConvertProgram;
  3570. CheckSource('TestExit_ResultInFinally',
  3571. LinesToStr([ // statements
  3572. 'this.Run = function () {',
  3573. ' var Result = 0;',
  3574. ' try {',
  3575. ' return 3;',
  3576. ' } finally {',
  3577. ' };',
  3578. ' return Result;',
  3579. '};',
  3580. 'this.Fly = function () {',
  3581. ' var Result = 0;',
  3582. ' try {',
  3583. ' Result = 3;',
  3584. ' return Result;',
  3585. ' } finally {',
  3586. ' if (Result > 0) ;',
  3587. ' };',
  3588. ' return Result;',
  3589. '};',
  3590. 'this.Jump = function () {',
  3591. ' var Result = 0;',
  3592. ' try {',
  3593. ' try {',
  3594. ' Result = 4;',
  3595. ' return Result;',
  3596. ' } finally {',
  3597. ' };',
  3598. ' } finally {',
  3599. ' if (Result > 0) ;',
  3600. ' };',
  3601. ' return Result;',
  3602. '};',
  3603. '']),
  3604. LinesToStr([
  3605. '']));
  3606. end;
  3607. procedure TTestModule.TestBreak;
  3608. begin
  3609. StartProgram(false);
  3610. Add([
  3611. 'var',
  3612. ' i: longint;',
  3613. 'begin',
  3614. ' repeat',
  3615. ' break;',
  3616. ' until true;',
  3617. ' while true do',
  3618. ' break;',
  3619. ' for i:=1 to 2 do',
  3620. ' break;']);
  3621. ConvertProgram;
  3622. CheckSource('TestBreak',
  3623. LinesToStr([ // statements
  3624. 'this.i = 0;'
  3625. ]),
  3626. LinesToStr([
  3627. 'do {',
  3628. ' break;',
  3629. '} while (!true);',
  3630. 'while (true) break;',
  3631. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3632. '']));
  3633. end;
  3634. procedure TTestModule.TestBreakAsVar;
  3635. begin
  3636. StartProgram(false);
  3637. Add([
  3638. 'procedure DoIt(break: boolean);',
  3639. 'begin',
  3640. ' if break then ;',
  3641. 'end;',
  3642. 'var',
  3643. ' break: boolean;',
  3644. 'begin',
  3645. ' if break then ;']);
  3646. ConvertProgram;
  3647. CheckSource('TestBreakAsVar',
  3648. LinesToStr([ // statements
  3649. 'this.DoIt = function (Break) {',
  3650. ' if (Break) ;',
  3651. '};',
  3652. 'this.Break = false;',
  3653. '']),
  3654. LinesToStr([
  3655. 'if($mod.Break) ;',
  3656. '']));
  3657. end;
  3658. procedure TTestModule.TestContinue;
  3659. begin
  3660. StartProgram(false);
  3661. Add('var i: longint;');
  3662. Add('begin');
  3663. Add(' repeat');
  3664. Add(' continue;');
  3665. Add(' until true;');
  3666. Add(' while true do');
  3667. Add(' continue;');
  3668. Add(' for i:=1 to 2 do');
  3669. Add(' continue;');
  3670. ConvertProgram;
  3671. CheckSource('TestContinue',
  3672. LinesToStr([ // statements
  3673. 'this.i = 0;'
  3674. ]),
  3675. LinesToStr([
  3676. 'do {',
  3677. ' continue;',
  3678. '} while (!true);',
  3679. 'while (true) continue;',
  3680. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3681. '']));
  3682. end;
  3683. procedure TTestModule.TestProc_External;
  3684. begin
  3685. StartProgram(false);
  3686. Add('procedure Foo; external name ''console.log'';');
  3687. Add('function Bar: longint; external name ''get.item'';');
  3688. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3689. Add('var');
  3690. Add(' i: longint;');
  3691. Add('begin');
  3692. Add(' Foo;');
  3693. Add(' i:=Bar;');
  3694. Add(' i:=Bla(''abc'');');
  3695. ConvertProgram;
  3696. CheckSource('TestProc_External',
  3697. LinesToStr([ // statements
  3698. 'this.i = 0;'
  3699. ]),
  3700. LinesToStr([
  3701. 'console.log();',
  3702. '$mod.i = get.item();',
  3703. '$mod.i = apply.something("abc");'
  3704. ]));
  3705. end;
  3706. procedure TTestModule.TestProc_ExternalOtherUnit;
  3707. begin
  3708. AddModuleWithIntfImplSrc('unit2.pas',
  3709. LinesToStr([
  3710. 'procedure Now; external name ''Date.now'';',
  3711. 'procedure DoIt;'
  3712. ]),
  3713. 'procedure doit; begin end;');
  3714. StartUnit(true);
  3715. Add('interface');
  3716. Add('uses unit2;');
  3717. Add('implementation');
  3718. Add('begin');
  3719. Add(' now;');
  3720. Add(' now();');
  3721. Add(' uNit2.now;');
  3722. Add(' uNit2.now();');
  3723. Add(' doit;');
  3724. Add(' uNit2.doit;');
  3725. ConvertUnit;
  3726. CheckSource('TestProc_ExternalOtherUnit',
  3727. LinesToStr([
  3728. '']),
  3729. LinesToStr([
  3730. 'Date.now();',
  3731. 'Date.now();',
  3732. 'Date.now();',
  3733. 'Date.now();',
  3734. 'pas.unit2.DoIt();',
  3735. 'pas.unit2.DoIt();',
  3736. '']));
  3737. end;
  3738. procedure TTestModule.TestProc_Asm;
  3739. begin
  3740. StartProgram(false);
  3741. Add([
  3742. '{$mode delphi}',
  3743. 'function DoIt: longint;',
  3744. 'begin;',
  3745. ' asm',
  3746. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3747. ' end;',
  3748. ' asm console.log(); end;',
  3749. ' asm',
  3750. ' s = "'' ";',
  3751. ' s = ''" '';',
  3752. ' s = s + "world" + "''";',
  3753. ' // end',
  3754. ' s = ''end'';',
  3755. ' s = "end";',
  3756. ' s = "foo\"bar";',
  3757. ' s = ''a\''b'';',
  3758. ' s = `${expr}\`-"-''-`;',
  3759. ' s = `multi',
  3760. 'line`;',
  3761. ' end;',
  3762. 'end;',
  3763. 'procedure Fly;',
  3764. 'asm',
  3765. ' return;',
  3766. 'end;',
  3767. 'begin']);
  3768. ConvertProgram;
  3769. CheckSource('TestProc_Asm',
  3770. LinesToStr([ // statements
  3771. 'this.DoIt = function () {',
  3772. ' var Result = 0;',
  3773. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3774. ' console.log();',
  3775. ' s = "'' ";',
  3776. ' s = ''" '';',
  3777. ' s = s + "world" + "''";',
  3778. ' // end',
  3779. ' s = ''end'';',
  3780. ' s = "end";',
  3781. ' s = "foo\"bar";',
  3782. ' s = ''a\''b'';',
  3783. ' s = `${expr}\`-"-''-`;',
  3784. ' s = `multi',
  3785. 'line`;',
  3786. ' return Result;',
  3787. '};',
  3788. 'this.Fly = function () {',
  3789. ' return;',
  3790. '};',
  3791. '']),
  3792. LinesToStr([
  3793. ''
  3794. ]));
  3795. end;
  3796. procedure TTestModule.TestProc_Assembler;
  3797. begin
  3798. StartProgram(false);
  3799. Add('function DoIt: longint; assembler;');
  3800. Add('asm');
  3801. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3802. Add('end;');
  3803. Add('begin');
  3804. ConvertProgram;
  3805. CheckSource('TestProc_Assembler',
  3806. LinesToStr([ // statements
  3807. 'this.DoIt = function () {',
  3808. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3809. '};'
  3810. ]),
  3811. LinesToStr([
  3812. ''
  3813. ]));
  3814. end;
  3815. procedure TTestModule.TestProc_VarParam;
  3816. begin
  3817. StartProgram(false);
  3818. Add('type integer = longint;');
  3819. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3820. Add('var vJ: integer;');
  3821. Add('begin');
  3822. Add(' vg:=vg+1;');
  3823. Add(' vj:=vh+2;');
  3824. Add(' vi:=vi+3;');
  3825. Add(' doit(vg,vg,vg);');
  3826. Add(' doit(vh,vh,vj);');
  3827. Add(' doit(vi,vi,vi);');
  3828. Add(' doit(vj,vj,vj);');
  3829. Add('end;');
  3830. Add('var i: integer;');
  3831. Add('begin');
  3832. Add(' doit(i,i,i);');
  3833. ConvertProgram;
  3834. CheckSource('TestProc_VarParam',
  3835. LinesToStr([ // statements
  3836. 'this.DoIt = function (vG,vH,vI) {',
  3837. ' var vJ = 0;',
  3838. ' vG = vG + 1;',
  3839. ' vJ = vH + 2;',
  3840. ' vI.set(vI.get()+3);',
  3841. ' $mod.DoIt(vG, vG, {',
  3842. ' get: function () {',
  3843. ' return vG;',
  3844. ' },',
  3845. ' set: function (v) {',
  3846. ' vG = v;',
  3847. ' }',
  3848. ' });',
  3849. ' $mod.DoIt(vH, vH, {',
  3850. ' get: function () {',
  3851. ' return vJ;',
  3852. ' },',
  3853. ' set: function (v) {',
  3854. ' vJ = v;',
  3855. ' }',
  3856. ' });',
  3857. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3858. ' $mod.DoIt(vJ, vJ, {',
  3859. ' get: function () {',
  3860. ' return vJ;',
  3861. ' },',
  3862. ' set: function (v) {',
  3863. ' vJ = v;',
  3864. ' }',
  3865. ' });',
  3866. '};',
  3867. 'this.i = 0;'
  3868. ]),
  3869. LinesToStr([
  3870. '$mod.DoIt($mod.i,$mod.i,{',
  3871. ' p: $mod,',
  3872. ' get: function () {',
  3873. ' return this.p.i;',
  3874. ' },',
  3875. ' set: function (v) {',
  3876. ' this.p.i = v;',
  3877. ' }',
  3878. '});'
  3879. ]));
  3880. end;
  3881. procedure TTestModule.TestProc_VarParamString;
  3882. begin
  3883. StartProgram(false);
  3884. Add(['type TCaption = string;',
  3885. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3886. 'var c: char;',
  3887. 'begin',
  3888. ' va[1]:=c;',
  3889. ' vb[2]:=c;',
  3890. ' vc[3]:=c;',
  3891. 'end;',
  3892. 'begin']);
  3893. ConvertProgram;
  3894. CheckSource('TestProc_VarParamString',
  3895. LinesToStr([ // statements
  3896. 'this.DoIt = function (vA,vB,vC) {',
  3897. ' var c = "";',
  3898. ' vA = rtl.setCharAt(vA, 0, c);',
  3899. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3900. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3901. '};',
  3902. '']),
  3903. LinesToStr([
  3904. ]));
  3905. end;
  3906. procedure TTestModule.TestProc_VarParamV;
  3907. begin
  3908. StartProgram(false);
  3909. Add([
  3910. 'procedure Inc2(var i: longint);',
  3911. 'begin',
  3912. ' i:=i+2;',
  3913. 'end;',
  3914. 'procedure DoIt(v: longint);',
  3915. 'var p: array of longint;',
  3916. 'begin',
  3917. ' Inc2(v);',
  3918. ' Inc2(p[v]);',
  3919. 'end;',
  3920. 'begin']);
  3921. ConvertProgram;
  3922. CheckSource('TestProc_VarParamV',
  3923. LinesToStr([ // statements
  3924. 'this.Inc2 = function (i) {',
  3925. ' i.set(i.get()+2);',
  3926. '};',
  3927. 'this.DoIt = function (v) {',
  3928. ' var p = [];',
  3929. ' $mod.Inc2({get: function () {',
  3930. ' return v;',
  3931. ' }, set: function (w) {',
  3932. ' v = w;',
  3933. ' }});',
  3934. ' $mod.Inc2({',
  3935. ' a: v,',
  3936. ' p: p,',
  3937. ' get: function () {',
  3938. ' return this.p[this.a];',
  3939. ' },',
  3940. ' set: function (v) {',
  3941. ' this.p[this.a] = v;',
  3942. ' }',
  3943. ' });',
  3944. '};',
  3945. '']),
  3946. LinesToStr([
  3947. '']));
  3948. end;
  3949. procedure TTestModule.TestProc_Overload;
  3950. begin
  3951. StartProgram(false);
  3952. Add('procedure DoIt(vI: longint); begin end;');
  3953. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3954. Add('procedure DoIt(vD: double); begin end;');
  3955. Add('begin');
  3956. Add(' DoIt(1);');
  3957. Add(' DoIt(2,3);');
  3958. Add(' DoIt(4.5);');
  3959. ConvertProgram;
  3960. CheckSource('TestProcedureOverload',
  3961. LinesToStr([ // statements
  3962. 'this.DoIt = function (vI) {',
  3963. '};',
  3964. 'this.DoIt$1 = function (vI, vJ) {',
  3965. '};',
  3966. 'this.DoIt$2 = function (vD) {',
  3967. '};',
  3968. '']),
  3969. LinesToStr([
  3970. '$mod.DoIt(1);',
  3971. '$mod.DoIt$1(2, 3);',
  3972. '$mod.DoIt$2(4.5);',
  3973. '']));
  3974. end;
  3975. procedure TTestModule.TestProc_OverloadForward;
  3976. begin
  3977. StartProgram(false);
  3978. Add('procedure DoIt(vI: longint); forward;');
  3979. Add('procedure DoIt(vI, vJ: longint); begin end;');
  3980. Add('procedure doit(vi: longint); begin end;');
  3981. Add('begin');
  3982. Add(' doit(1);');
  3983. Add(' doit(2,3);');
  3984. ConvertProgram;
  3985. CheckSource('TestProcedureOverloadForward',
  3986. LinesToStr([ // statements
  3987. 'this.DoIt$1 = function (vI, vJ) {',
  3988. '};',
  3989. 'this.DoIt = function (vI) {',
  3990. '};',
  3991. '']),
  3992. LinesToStr([
  3993. '$mod.DoIt(1);',
  3994. '$mod.DoIt$1(2, 3);',
  3995. '']));
  3996. end;
  3997. procedure TTestModule.TestProc_OverloadIntfImpl;
  3998. begin
  3999. StartUnit(false);
  4000. Add('interface');
  4001. Add('procedure DoIt(vI: longint);');
  4002. Add('procedure DoIt(vI, vJ: longint);');
  4003. Add('implementation');
  4004. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4005. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4006. Add('procedure DoIt(vi: longint); begin end;');
  4007. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4008. Add('procedure DoIt(vi, vj: longint); begin end;');
  4009. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4010. Add('begin');
  4011. Add(' doit(1);');
  4012. Add(' doit(2,3);');
  4013. Add(' doit(4,5,6);');
  4014. Add(' doit(7,8,9,10);');
  4015. Add(' doit(11,12,13,14,15);');
  4016. ConvertUnit;
  4017. CheckSource('TestProcedureOverloadUnit',
  4018. LinesToStr([ // statements
  4019. 'var $impl = $mod.$impl;',
  4020. 'this.DoIt = function (vI) {',
  4021. '};',
  4022. 'this.DoIt$1 = function (vI, vJ) {',
  4023. '};',
  4024. '']),
  4025. LinesToStr([ // this.$init
  4026. '$mod.DoIt(1);',
  4027. '$mod.DoIt$1(2, 3);',
  4028. '$impl.DoIt$3(4,5,6);',
  4029. '$impl.DoIt$4(7,8,9,10);',
  4030. '$impl.DoIt$2(11,12,13,14,15);',
  4031. '']),
  4032. LinesToStr([ // implementation
  4033. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4034. '};',
  4035. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4036. '};',
  4037. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4038. '};',
  4039. '']));
  4040. end;
  4041. procedure TTestModule.TestProc_OverloadNested;
  4042. begin
  4043. StartProgram(false);
  4044. Add([
  4045. 'procedure doit(vA: longint);',
  4046. ' procedure DoIt(vA, vB: longint); overload;',
  4047. ' begin',
  4048. ' doit(1);',
  4049. ' doit(1,2);',
  4050. ' end;',
  4051. ' procedure doit(vA, vB, vC: longint);',
  4052. ' begin',
  4053. ' doit(1);',
  4054. ' doit(1,2);',
  4055. ' doit(1,2,3);',
  4056. ' end;',
  4057. 'begin',
  4058. ' doit(1);',
  4059. ' doit(1,2);',
  4060. ' doit(1,2,3);',
  4061. 'end;',
  4062. 'begin // main',
  4063. ' doit(1);']);
  4064. ConvertProgram;
  4065. CheckSource('TestProcedureOverloadNested',
  4066. LinesToStr([ // statements
  4067. 'this.doit = function (vA) {',
  4068. ' function DoIt$1(vA, vB) {',
  4069. ' $mod.doit(1);',
  4070. ' DoIt$1(1, 2);',
  4071. ' };',
  4072. ' function doit$2(vA, vB, vC) {',
  4073. ' $mod.doit(1);',
  4074. ' DoIt$1(1, 2);',
  4075. ' doit$2(1, 2, 3);',
  4076. ' };',
  4077. ' $mod.doit(1);',
  4078. ' DoIt$1(1, 2);',
  4079. ' doit$2(1, 2, 3);',
  4080. '};',
  4081. '']),
  4082. LinesToStr([
  4083. '$mod.doit(1);',
  4084. '']));
  4085. end;
  4086. procedure TTestModule.TestProc_OverloadNestedForward;
  4087. begin
  4088. StartProgram(false);
  4089. Add([
  4090. 'procedure DoIt(vA: longint); overload; forward;',
  4091. 'procedure DoIt(vB, vC: longint); overload;',
  4092. 'begin // 2 param overload',
  4093. ' doit(1);',
  4094. ' doit(1,2);',
  4095. 'end;',
  4096. 'procedure doit(vA: longint);',
  4097. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4098. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4099. ' begin // 4 param overload',
  4100. ' doit(1);',
  4101. ' doit(1,2);',
  4102. ' doit(1,2,3);',
  4103. ' doit(1,2,3,4);',
  4104. ' end;',
  4105. ' procedure doit(vA, vB, vC: longint);',
  4106. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4107. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4108. ' begin // 6 param overload',
  4109. ' doit(1);',
  4110. ' doit(1,2);',
  4111. ' doit(1,2,3);',
  4112. ' doit(1,2,3,4);',
  4113. ' doit(1,2,3,4,5);',
  4114. ' doit(1,2,3,4,5,6);',
  4115. ' end;',
  4116. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4117. ' begin // 5 param overload',
  4118. ' doit(1);',
  4119. ' doit(1,2);',
  4120. ' doit(1,2,3);',
  4121. ' doit(1,2,3,4);',
  4122. ' doit(1,2,3,4,5);',
  4123. ' doit(1,2,3,4,5,6);',
  4124. ' end;',
  4125. ' begin // 3 param overload',
  4126. ' doit(1);',
  4127. ' doit(1,2);',
  4128. ' doit(1,2,3);',
  4129. ' doit(1,2,3,4);',
  4130. ' doit(1,2,3,4,5);',
  4131. ' doit(1,2,3,4,5,6);',
  4132. ' end;',
  4133. 'begin // 1 param overload',
  4134. ' doit(1);',
  4135. ' doit(1,2);',
  4136. ' doit(1,2,3);',
  4137. ' doit(1,2,3,4);',
  4138. 'end;',
  4139. 'begin // main',
  4140. ' doit(1);',
  4141. ' doit(1,2);']);
  4142. ConvertProgram;
  4143. CheckSource('TestProc_OverloadNestedForward',
  4144. LinesToStr([ // statements
  4145. 'this.DoIt$1 = function (vB, vC) {',
  4146. ' $mod.DoIt(1);',
  4147. ' $mod.DoIt$1(1, 2);',
  4148. '};',
  4149. 'this.DoIt = function (vA) {',
  4150. ' function DoIt$3(vA, vB, vC, vD) {',
  4151. ' $mod.DoIt(1);',
  4152. ' $mod.DoIt$1(1, 2);',
  4153. ' DoIt$2(1, 2, 3);',
  4154. ' DoIt$3(1, 2, 3, 4);',
  4155. ' };',
  4156. ' function DoIt$2(vA, vB, vC) {',
  4157. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4158. ' $mod.DoIt(1);',
  4159. ' $mod.DoIt$1(1, 2);',
  4160. ' DoIt$2(1, 2, 3);',
  4161. ' DoIt$3(1, 2, 3, 4);',
  4162. ' DoIt$4(1, 2, 3, 4, 5);',
  4163. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4164. ' };',
  4165. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4166. ' $mod.DoIt(1);',
  4167. ' $mod.DoIt$1(1, 2);',
  4168. ' DoIt$2(1, 2, 3);',
  4169. ' DoIt$3(1, 2, 3, 4);',
  4170. ' DoIt$4(1, 2, 3, 4, 5);',
  4171. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4172. ' };',
  4173. ' $mod.DoIt(1);',
  4174. ' $mod.DoIt$1(1, 2);',
  4175. ' DoIt$2(1, 2, 3);',
  4176. ' DoIt$3(1, 2, 3, 4);',
  4177. ' DoIt$4(1, 2, 3, 4, 5);',
  4178. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4179. ' };',
  4180. ' $mod.DoIt(1);',
  4181. ' $mod.DoIt$1(1, 2);',
  4182. ' DoIt$2(1, 2, 3);',
  4183. ' DoIt$3(1, 2, 3, 4);',
  4184. '};',
  4185. '']),
  4186. LinesToStr([
  4187. '$mod.DoIt(1);',
  4188. '$mod.DoIt$1(1, 2);',
  4189. '']));
  4190. end;
  4191. procedure TTestModule.TestProc_OverloadUnitCycle;
  4192. begin
  4193. AddModuleWithIntfImplSrc('Unit2.pas',
  4194. LinesToStr([
  4195. 'type',
  4196. ' TObject = class',
  4197. ' procedure DoIt(b: boolean); virtual; abstract;',
  4198. ' procedure DoIt(i: longint); virtual; abstract;',
  4199. ' end;',
  4200. '']),
  4201. 'uses test1;');
  4202. StartUnit(true);
  4203. Add([
  4204. 'interface',
  4205. 'uses unit2;',
  4206. 'type',
  4207. ' TEagle = class(TObject)',
  4208. ' procedure DoIt(b: boolean); override;',
  4209. ' procedure DoIt(i: longint); override;',
  4210. ' end;',
  4211. 'implementation',
  4212. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4213. 'procedure TEagle.DoIt(i: longint); begin end;',
  4214. '']);
  4215. ConvertUnit;
  4216. CheckSource('TestProc_OverloadUnitCycle',
  4217. LinesToStr([ // statements
  4218. 'rtl.createClass($mod, "TEagle", pas.Unit2.TObject, function () {',
  4219. ' this.DoIt = function (b) {',
  4220. ' };',
  4221. ' this.DoIt$1 = function (i) {',
  4222. ' };',
  4223. '});',
  4224. '']),
  4225. '',
  4226. LinesToStr([
  4227. '']));
  4228. end;
  4229. procedure TTestModule.TestProc_Varargs;
  4230. begin
  4231. StartProgram(false);
  4232. Add([
  4233. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4234. 'procedure ProcB; varargs; external name ''ProcB'';',
  4235. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4236. 'function GetIt: longint; begin end;',
  4237. 'begin',
  4238. ' ProcA(1);',
  4239. ' ProcA(1,2);',
  4240. ' ProcA(1,2.0);',
  4241. ' ProcA(1,2,3);',
  4242. ' ProcA(1,''2'');',
  4243. ' ProcA(2,'''');',
  4244. ' ProcA(3,false);',
  4245. ' ProcB;',
  4246. ' ProcB();',
  4247. ' ProcB(4);',
  4248. ' ProcB(''foo'');',
  4249. ' ProcC;',
  4250. ' ProcC();',
  4251. ' ProcC(4);',
  4252. ' ProcC(5,''foo'');',
  4253. ' ProcB(GetIt);',
  4254. ' ProcB(GetIt());',
  4255. ' ProcB(GetIt,GetIt());']);
  4256. ConvertProgram;
  4257. CheckSource('TestProc_Varargs',
  4258. LinesToStr([ // statements
  4259. 'this.GetIt = function () {',
  4260. ' var Result = 0;',
  4261. ' return Result;',
  4262. '};',
  4263. '']),
  4264. LinesToStr([
  4265. 'ProcA(1);',
  4266. 'ProcA(1, 2);',
  4267. 'ProcA(1, 2.0);',
  4268. 'ProcA(1, 2, 3);',
  4269. 'ProcA(1, "2");',
  4270. 'ProcA(2, "");',
  4271. 'ProcA(3, false);',
  4272. 'ProcB();',
  4273. 'ProcB();',
  4274. 'ProcB(4);',
  4275. 'ProcB("foo");',
  4276. 'ProcC(17);',
  4277. 'ProcC(17);',
  4278. 'ProcC(4);',
  4279. 'ProcC(5, "foo");',
  4280. 'ProcB($mod.GetIt());',
  4281. 'ProcB($mod.GetIt());',
  4282. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4283. '']));
  4284. end;
  4285. procedure TTestModule.TestProc_ConstOrder;
  4286. begin
  4287. StartProgram(false);
  4288. Add([
  4289. 'const A = 3;',
  4290. 'const B = A+1;',
  4291. 'procedure DoIt;',
  4292. 'const C = A+1;',
  4293. 'const D = B+1;',
  4294. 'const E = D+C+B+A;',
  4295. 'begin',
  4296. 'end;',
  4297. 'begin'
  4298. ]);
  4299. ConvertProgram;
  4300. CheckSource('TestProc_ConstOrder',
  4301. LinesToStr([ // statements
  4302. 'this.A = 3;',
  4303. 'this.B = 3 + 1;',
  4304. 'var C = 3 + 1;',
  4305. 'var D = 4 + 1;',
  4306. 'var E = 5 + 4 + 4 + 3;',
  4307. 'this.DoIt = function () {',
  4308. '};',
  4309. '']),
  4310. LinesToStr([
  4311. ''
  4312. ]));
  4313. end;
  4314. procedure TTestModule.TestProc_DuplicateConst;
  4315. begin
  4316. StartProgram(false);
  4317. Add([
  4318. 'const A = 1;',
  4319. 'procedure DoIt;',
  4320. 'const A = 2;',
  4321. ' procedure SubIt;',
  4322. ' const A = 21;',
  4323. ' begin',
  4324. ' end;',
  4325. 'begin',
  4326. 'end;',
  4327. 'procedure DoSome;',
  4328. 'const A = 3;',
  4329. 'begin',
  4330. 'end;',
  4331. 'begin'
  4332. ]);
  4333. ConvertProgram;
  4334. CheckSource('TestProc_DuplicateConst',
  4335. LinesToStr([ // statements
  4336. 'this.A = 1;',
  4337. 'var A$1 = 2;',
  4338. 'var A$2 = 21;',
  4339. 'this.DoIt = function () {',
  4340. ' function SubIt() {',
  4341. ' };',
  4342. '};',
  4343. 'var A$3 = 3;',
  4344. 'this.DoSome = function () {',
  4345. '};',
  4346. '']),
  4347. LinesToStr([
  4348. ''
  4349. ]));
  4350. end;
  4351. procedure TTestModule.TestProc_LocalVarAbsolute;
  4352. begin
  4353. StartProgram(false);
  4354. Add([
  4355. 'type',
  4356. ' TObject = class',
  4357. ' Index: longint;',
  4358. ' procedure DoAbs(Item: pointer);',
  4359. ' end;',
  4360. 'procedure TObject.DoAbs(Item: pointer);',
  4361. 'var',
  4362. ' o: TObject absolute Item;',
  4363. 'begin',
  4364. ' if o.Index<o.Index then o.Index:=o.Index;',
  4365. 'end;',
  4366. 'procedure DoIt(i: longint; p: pointer);',
  4367. 'var',
  4368. ' d: double absolute i;',
  4369. ' s: string absolute d;',
  4370. ' oi: TObject absolute i;',
  4371. ' op: TObject absolute p;',
  4372. 'begin',
  4373. ' if d=d then d:=d;',
  4374. ' if s=s then s:=s;',
  4375. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4376. ' if op.Index=op.Index then op.Index:=op.Index;',
  4377. 'end;',
  4378. 'begin']);
  4379. ConvertProgram;
  4380. CheckSource('TestProc_LocalVarAbsolute',
  4381. LinesToStr([ // statements
  4382. 'rtl.createClass($mod, "TObject", null, function () {',
  4383. ' this.$init = function () {',
  4384. ' this.Index = 0;',
  4385. ' };',
  4386. ' this.$final = function () {',
  4387. ' };',
  4388. ' this.DoAbs = function (Item) {',
  4389. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4390. ' };',
  4391. '});',
  4392. 'this.DoIt = function (i, p) {',
  4393. ' if (i === i) i = i;',
  4394. ' if (i === i) i = i;',
  4395. ' if (i.Index < i.Index) i.Index = i.Index;',
  4396. ' if (p.Index === p.Index) p.Index = p.Index;',
  4397. '};'
  4398. ]),
  4399. LinesToStr([
  4400. ]));
  4401. end;
  4402. procedure TTestModule.TestProc_LocalVarInit;
  4403. begin
  4404. StartProgram(false);
  4405. Add([
  4406. 'type TBytes = array of byte;',
  4407. 'procedure DoIt;',
  4408. 'const c = 4;',
  4409. 'var',
  4410. ' b: byte = 1;',
  4411. ' w: word = 2+c;',
  4412. ' p: pointer = nil;',
  4413. ' Buffer: TBytes = nil;',
  4414. 'begin',
  4415. 'end;',
  4416. 'begin']);
  4417. ConvertProgram;
  4418. CheckSource('TestProc_LocalVarInit',
  4419. LinesToStr([ // statements
  4420. 'var c = 4;',
  4421. 'this.DoIt = function () {',
  4422. ' var b = 1;',
  4423. ' var w = 2 + 4;',
  4424. ' var p = null;',
  4425. ' var Buffer = [];',
  4426. '};',
  4427. '']),
  4428. LinesToStr([
  4429. ]));
  4430. end;
  4431. procedure TTestModule.TestProc_ReservedWords;
  4432. begin
  4433. StartProgram(false);
  4434. Add([
  4435. 'procedure Date(ArrayBuffer: longint);',
  4436. 'const',
  4437. ' NaN: longint = 3;',
  4438. 'var',
  4439. ' &Boolean: longint;',
  4440. ' procedure Error(ArrayBuffer: longint);',
  4441. ' begin',
  4442. ' end;',
  4443. 'begin',
  4444. ' Nan:=&bOolean;',
  4445. 'end;',
  4446. 'begin',
  4447. ' Date(1);']);
  4448. ConvertProgram;
  4449. CheckSource('TestProc_ReservedWords',
  4450. LinesToStr([ // statements
  4451. 'var naN = 3;',
  4452. 'this.Date = function (arrayBuffer) {',
  4453. ' var boolean = 0;',
  4454. ' function error(arrayBuffer) {',
  4455. ' };',
  4456. ' naN = boolean;',
  4457. '};',
  4458. '']),
  4459. LinesToStr([
  4460. ' $mod.Date(1);'
  4461. ]));
  4462. end;
  4463. procedure TTestModule.TestProc_ConstRefWord;
  4464. begin
  4465. StartProgram(false);
  4466. Add([
  4467. 'procedure Run(constref w: word);',
  4468. 'var l: word;',
  4469. 'begin',
  4470. ' l:=w;',
  4471. ' Run(w);',
  4472. ' Run(l);',
  4473. 'end;',
  4474. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4475. 'begin',
  4476. ' Run(a);',
  4477. ' Run(b);',
  4478. ' Run(c);',
  4479. ' Run(d);',
  4480. ' Run(e);',
  4481. 'end;',
  4482. 'begin',
  4483. ' Run(1);']);
  4484. ConvertProgram;
  4485. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4486. CheckSource('TestProc_ConstRefWord',
  4487. LinesToStr([ // statements
  4488. 'this.Run = function (w) {',
  4489. ' var l = 0;',
  4490. ' l = w;',
  4491. ' $mod.Run(w);',
  4492. ' $mod.Run(l);',
  4493. '};',
  4494. 'this.Fly = function (a, b, c, d, e) {',
  4495. ' $mod.Run(a);',
  4496. ' $mod.Run(b.get());',
  4497. ' $mod.Run(c.get());',
  4498. ' $mod.Run(d);',
  4499. ' $mod.Run(e);',
  4500. '};',
  4501. '']),
  4502. LinesToStr([
  4503. '$mod.Run(1);'
  4504. ]));
  4505. end;
  4506. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4507. begin
  4508. StartProgram(false);
  4509. Add([
  4510. '{$mode objfpc}',
  4511. 'type',
  4512. ' TFunc = reference to function(x: word): word;',
  4513. 'var Func: TFunc;',
  4514. 'procedure DoIt(a: word);',
  4515. 'begin',
  4516. ' Func:=function(b:word): word',
  4517. ' begin',
  4518. ' Result:=a+b;',
  4519. ' exit(b);',
  4520. ' exit(Result);',
  4521. ' end;',// test semicolon
  4522. ' a:=3;',
  4523. 'end;',
  4524. 'begin',
  4525. ' Func:=function(c:word):word begin',
  4526. ' Result:=3+c;',
  4527. ' exit(c);',
  4528. ' exit(Result);',
  4529. ' end;']);
  4530. ConvertProgram;
  4531. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4532. LinesToStr([ // statements
  4533. 'this.Func = null;',
  4534. 'this.DoIt = function (a) {',
  4535. ' $mod.Func = function (b) {',
  4536. ' var Result = 0;',
  4537. ' Result = a + b;',
  4538. ' return b;',
  4539. ' return Result;',
  4540. ' return Result;',
  4541. ' };',
  4542. ' a = 3;',
  4543. '};',
  4544. '']),
  4545. LinesToStr([
  4546. '$mod.Func = function (c) {',
  4547. ' var Result = 0;',
  4548. ' Result = 3 + c;',
  4549. ' return c;',
  4550. ' return Result;',
  4551. ' return Result;',
  4552. '};',
  4553. '']));
  4554. end;
  4555. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4556. begin
  4557. StartProgram(false);
  4558. Add([
  4559. '{$mode delphi}',
  4560. 'type',
  4561. ' TProc = reference to procedure(x: word);',
  4562. 'procedure DoIt(a: word);',
  4563. 'var Proc: TProc;',
  4564. 'begin',
  4565. ' Proc:=procedure(b:word) begin end;',
  4566. 'end;',
  4567. 'var Proc: TProc;',
  4568. 'begin',
  4569. ' Proc:=procedure(c:word) begin end;',
  4570. '']);
  4571. ConvertProgram;
  4572. CheckSource('TestAnonymousProc_Assign_Delphi',
  4573. LinesToStr([ // statements
  4574. 'this.DoIt = function (a) {',
  4575. ' var Proc = null;',
  4576. ' Proc = function (b) {',
  4577. ' };',
  4578. '};',
  4579. 'this.Proc = null;',
  4580. '']),
  4581. LinesToStr([
  4582. '$mod.Proc = function (c) {',
  4583. '};',
  4584. '']));
  4585. end;
  4586. procedure TTestModule.TestAnonymousProc_Arg;
  4587. begin
  4588. StartProgram(false);
  4589. Add([
  4590. 'type',
  4591. ' TProc = reference to procedure;',
  4592. ' TFunc = reference to function(x: word): word;',
  4593. 'procedure DoMore(f,g: TProc);',
  4594. 'begin',
  4595. 'end;',
  4596. 'procedure DoOdd(v: jsvalue);',
  4597. 'begin',
  4598. 'end;',
  4599. 'procedure DoIt(f: TFunc);',
  4600. 'begin',
  4601. ' DoIt(function(b:word): word',
  4602. ' begin',
  4603. ' Result:=1+b;',
  4604. ' end);',
  4605. ' DoMore(procedure begin end, procedure begin end);',
  4606. ' DoOdd(procedure begin end);',
  4607. 'end;',
  4608. 'begin',
  4609. ' DoMore(procedure begin end,',
  4610. ' procedure assembler asm',
  4611. ' console.log("c");',
  4612. ' end);',
  4613. '']);
  4614. ConvertProgram;
  4615. CheckSource('TestAnonymousProc_Arg',
  4616. LinesToStr([ // statements
  4617. 'this.DoMore = function (f, g) {',
  4618. '};',
  4619. 'this.DoOdd = function (v) {',
  4620. '};',
  4621. 'this.DoIt = function (f) {',
  4622. ' $mod.DoIt(function (b) {',
  4623. ' var Result = 0;',
  4624. ' Result = 1 + b;',
  4625. ' return Result;',
  4626. ' });',
  4627. ' $mod.DoMore(function () {',
  4628. ' }, function () {',
  4629. ' });',
  4630. ' $mod.DoOdd(function () {',
  4631. ' });',
  4632. '};',
  4633. '']),
  4634. LinesToStr([
  4635. '$mod.DoMore(function () {',
  4636. '}, function () {',
  4637. ' console.log("c");',
  4638. '});',
  4639. '']));
  4640. end;
  4641. procedure TTestModule.TestAnonymousProc_Typecast;
  4642. begin
  4643. StartProgram(false);
  4644. Add([
  4645. 'type',
  4646. ' TProc = reference to procedure(w: word);',
  4647. ' TArr = array of word;',
  4648. ' TFuncArr = reference to function: TArr;',
  4649. 'procedure DoIt(p: TProc);',
  4650. 'var',
  4651. ' w: word;',
  4652. ' a: TArr;',
  4653. 'begin',
  4654. ' p:=TProc(procedure(b: smallint) begin end);',
  4655. ' a:=TFuncArr(function: TArr begin end)();',
  4656. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4657. 'end;',
  4658. 'begin']);
  4659. ConvertProgram;
  4660. CheckSource('TestAnonymousProc_Typecast',
  4661. LinesToStr([ // statements
  4662. 'this.DoIt = function (p) {',
  4663. ' var w = 0;',
  4664. ' var a = [];',
  4665. ' p = function (b) {',
  4666. ' };',
  4667. ' a = function () {',
  4668. ' var Result = [];',
  4669. ' return Result;',
  4670. ' }();',
  4671. ' w = function () {',
  4672. ' var Result = [];',
  4673. ' return Result;',
  4674. ' }()[3];',
  4675. '};',
  4676. '']),
  4677. LinesToStr([
  4678. '']));
  4679. end;
  4680. procedure TTestModule.TestAnonymousProc_With;
  4681. begin
  4682. StartProgram(false);
  4683. Add([
  4684. 'type',
  4685. ' TProc = reference to procedure(w: word);',
  4686. ' TObject = class',
  4687. ' b: boolean;',
  4688. ' end;',
  4689. 'var',
  4690. ' p: TProc;',
  4691. ' bird: TObject;',
  4692. 'begin',
  4693. ' with bird do',
  4694. ' p:=procedure(w: word)',
  4695. ' begin',
  4696. ' b:=w>2;',
  4697. ' end;',
  4698. '']);
  4699. ConvertProgram;
  4700. CheckSource('TestAnonymousProc_With',
  4701. LinesToStr([ // statements
  4702. 'rtl.createClass($mod, "TObject", null, function () {',
  4703. ' this.$init = function () {',
  4704. ' this.b = false;',
  4705. ' };',
  4706. ' this.$final = function () {',
  4707. ' };',
  4708. '});',
  4709. 'this.p = null;',
  4710. 'this.bird = null;',
  4711. '']),
  4712. LinesToStr([
  4713. 'var $with = $mod.bird;',
  4714. '$mod.p = function (w) {',
  4715. ' $with.b = w > 2;',
  4716. '};',
  4717. '']));
  4718. end;
  4719. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4720. begin
  4721. StartProgram(false);
  4722. Add([
  4723. 'type',
  4724. ' TProc = reference to procedure;',
  4725. ' TObject = class',
  4726. ' b: boolean;',
  4727. ' end;',
  4728. 'procedure DoIt;',
  4729. 'var',
  4730. ' p: TProc;',
  4731. 'begin',
  4732. ' try',
  4733. ' except',
  4734. ' on E: TObject do',
  4735. ' p:=procedure',
  4736. ' begin',
  4737. ' E.b:=true;',
  4738. ' end;',
  4739. ' end;',
  4740. 'end;',
  4741. 'begin']);
  4742. ConvertProgram;
  4743. CheckSource('TestAnonymousProc_ExceptOn',
  4744. LinesToStr([ // statements
  4745. 'rtl.createClass($mod, "TObject", null, function () {',
  4746. ' this.$init = function () {',
  4747. ' this.b = false;',
  4748. ' };',
  4749. ' this.$final = function () {',
  4750. ' };',
  4751. '});',
  4752. 'this.DoIt = function () {',
  4753. ' var p = null;',
  4754. ' try {} catch ($e) {',
  4755. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4756. ' var E = $e;',
  4757. ' p = function () {',
  4758. ' E.b = true;',
  4759. ' };',
  4760. ' } else throw $e',
  4761. ' };',
  4762. '};',
  4763. '']),
  4764. LinesToStr([
  4765. '']));
  4766. end;
  4767. procedure TTestModule.TestAnonymousProc_Nested;
  4768. begin
  4769. StartProgram(false);
  4770. Add([
  4771. 'type',
  4772. ' TProc = reference to procedure;',
  4773. ' TObject = class',
  4774. ' i: byte;',
  4775. ' procedure DoIt;',
  4776. ' end;',
  4777. 'procedure TObject.DoIt;',
  4778. 'var',
  4779. ' p: TProc;',
  4780. ' procedure Sub;',
  4781. ' begin',
  4782. ' p:=procedure',
  4783. ' begin',
  4784. ' i:=3;',
  4785. ' Self.i:=4;',
  4786. ' p:=procedure',
  4787. ' procedure SubSub;',
  4788. ' begin',
  4789. ' i:=13;',
  4790. ' Self.i:=14;',
  4791. ' end;',
  4792. ' begin',
  4793. ' i:=13;',
  4794. ' Self.i:=14;',
  4795. ' end;',
  4796. ' end;',
  4797. ' end;',
  4798. 'begin',
  4799. 'end;',
  4800. 'begin']);
  4801. ConvertProgram;
  4802. CheckSource('TestAnonymousProc_Nested',
  4803. LinesToStr([ // statements
  4804. 'rtl.createClass($mod, "TObject", null, function () {',
  4805. ' this.$init = function () {',
  4806. ' this.i = 0;',
  4807. ' };',
  4808. ' this.$final = function () {',
  4809. ' };',
  4810. ' this.DoIt = function () {',
  4811. ' var $Self = this;',
  4812. ' var p = null;',
  4813. ' function Sub() {',
  4814. ' p = function () {',
  4815. ' $Self.i = 3;',
  4816. ' $Self.i = 4;',
  4817. ' p = function () {',
  4818. ' function SubSub() {',
  4819. ' $Self.i = 13;',
  4820. ' $Self.i = 14;',
  4821. ' };',
  4822. ' $Self.i = 13;',
  4823. ' $Self.i = 14;',
  4824. ' };',
  4825. ' };',
  4826. ' };',
  4827. ' };',
  4828. '});',
  4829. '']),
  4830. LinesToStr([
  4831. '']));
  4832. end;
  4833. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4834. begin
  4835. StartProgram(false);
  4836. Add([
  4837. 'type',
  4838. ' TProc = reference to procedure;',
  4839. 'function DoIt: TProc;',
  4840. ' function Sub: TProc;',
  4841. ' begin',
  4842. ' Result:=procedure',
  4843. ' begin',
  4844. ' Sub:=procedure',
  4845. ' procedure SubSub;',
  4846. ' begin',
  4847. ' Result:=nil;',
  4848. ' Sub:=nil;',
  4849. ' DoIt:=nil;',
  4850. ' end;',
  4851. ' begin',
  4852. ' Result:=nil;',
  4853. ' Sub:=nil;',
  4854. ' DoIt:=nil;',
  4855. ' end;',
  4856. ' end;',
  4857. ' end;',
  4858. 'begin',
  4859. 'end;',
  4860. 'begin']);
  4861. ConvertProgram;
  4862. CheckSource('TestAnonymousProc_NestedAssignResult',
  4863. LinesToStr([ // statements
  4864. 'this.DoIt = function () {',
  4865. ' var Result = null;',
  4866. ' function Sub() {',
  4867. ' var Result$1 = null;',
  4868. ' Result$1 = function () {',
  4869. ' Result$1 = function () {',
  4870. ' function SubSub() {',
  4871. ' Result$1 = null;',
  4872. ' Result$1 = null;',
  4873. ' Result = null;',
  4874. ' };',
  4875. ' Result$1 = null;',
  4876. ' Result$1 = null;',
  4877. ' Result = null;',
  4878. ' };',
  4879. ' };',
  4880. ' return Result$1;',
  4881. ' };',
  4882. ' return Result;',
  4883. '};',
  4884. '']),
  4885. LinesToStr([
  4886. '']));
  4887. end;
  4888. procedure TTestModule.TestAnonymousProc_Class;
  4889. begin
  4890. StartProgram(false);
  4891. Add([
  4892. 'type',
  4893. ' TProc = reference to procedure;',
  4894. ' TEvent = procedure of object;',
  4895. ' TObject = class',
  4896. ' Size: word;',
  4897. ' function GetIt: TProc;',
  4898. ' procedure DoIt; virtual; abstract;',
  4899. ' end;',
  4900. 'function TObject.GetIt: TProc;',
  4901. 'begin',
  4902. ' Result:=procedure',
  4903. ' var p: TEvent;',
  4904. ' begin',
  4905. ' Size:=Size;',
  4906. ' Size:=Self.Size;',
  4907. ' p:=@DoIt;',
  4908. ' p:[email protected];',
  4909. ' end;',
  4910. 'end;',
  4911. 'begin']);
  4912. ConvertProgram;
  4913. CheckSource('TestAnonymousProc_Class',
  4914. LinesToStr([ // statements
  4915. 'rtl.createClass($mod, "TObject", null, function () {',
  4916. ' this.$init = function () {',
  4917. ' this.Size = 0;',
  4918. ' };',
  4919. ' this.$final = function () {',
  4920. ' };',
  4921. ' this.GetIt = function () {',
  4922. ' var $Self = this;',
  4923. ' var Result = null;',
  4924. ' Result = function () {',
  4925. ' var p = null;',
  4926. ' $Self.Size = $Self.Size;',
  4927. ' $Self.Size = $Self.Size;',
  4928. ' p = rtl.createCallback($Self, "DoIt");',
  4929. ' p = rtl.createCallback($Self, "DoIt");',
  4930. ' };',
  4931. ' return Result;',
  4932. ' };',
  4933. '});',
  4934. '']),
  4935. LinesToStr([
  4936. '']));
  4937. end;
  4938. procedure TTestModule.TestAnonymousProc_ForLoop;
  4939. begin
  4940. StartProgram(false);
  4941. Add([
  4942. 'type TProc = reference to procedure;',
  4943. 'procedure Foo(p: TProc);',
  4944. 'begin',
  4945. 'end;',
  4946. 'procedure DoIt;',
  4947. 'var i: word;',
  4948. ' a: word;',
  4949. 'begin',
  4950. ' for i:=1 to 10 do begin',
  4951. ' Foo(procedure begin a:=3; end);',
  4952. ' end;',
  4953. 'end;',
  4954. 'begin',
  4955. ' DoIt;']);
  4956. ConvertProgram;
  4957. CheckSource('TestAnonymousProc_ForLoop',
  4958. LinesToStr([ // statements
  4959. 'this.Foo = function (p) {',
  4960. '};',
  4961. 'this.DoIt = function () {',
  4962. ' var i = 0;',
  4963. ' var a = 0;',
  4964. ' for (i = 1; i <= 10; i++) {',
  4965. ' $mod.Foo(function () {',
  4966. ' a = 3;',
  4967. ' });',
  4968. ' };',
  4969. '};',
  4970. '']),
  4971. LinesToStr([
  4972. '$mod.DoIt();'
  4973. ]));
  4974. end;
  4975. procedure TTestModule.TestEnum_Name;
  4976. begin
  4977. StartProgram(false);
  4978. Add('type TMyEnum = (Red, Green, Blue);');
  4979. Add('var e: TMyEnum;');
  4980. Add('var f: TMyEnum = Blue;');
  4981. Add('begin');
  4982. Add(' e:=green;');
  4983. Add(' e:=default(TMyEnum);');
  4984. ConvertProgram;
  4985. CheckSource('TestEnumName',
  4986. LinesToStr([ // statements
  4987. 'this.TMyEnum = {',
  4988. ' "0":"Red",',
  4989. ' Red:0,',
  4990. ' "1":"Green",',
  4991. ' Green:1,',
  4992. ' "2":"Blue",',
  4993. ' Blue:2',
  4994. ' };',
  4995. 'this.e = 0;',
  4996. 'this.f = $mod.TMyEnum.Blue;'
  4997. ]),
  4998. LinesToStr([
  4999. '$mod.e=$mod.TMyEnum.Green;',
  5000. '$mod.e=$mod.TMyEnum.Red;'
  5001. ]));
  5002. end;
  5003. procedure TTestModule.TestEnum_Number;
  5004. begin
  5005. Converter.Options:=Converter.Options+[coEnumNumbers];
  5006. StartProgram(false);
  5007. Add('type TMyEnum = (Red, Green);');
  5008. Add('var');
  5009. Add(' e: TMyEnum;');
  5010. Add(' f: TMyEnum = Green;');
  5011. Add(' i: longint;');
  5012. Add('begin');
  5013. Add(' e:=green;');
  5014. Add(' i:=longint(e);');
  5015. ConvertProgram;
  5016. CheckSource('TestEnumNumber',
  5017. LinesToStr([ // statements
  5018. 'this.TMyEnum = {',
  5019. ' "0":"Red",',
  5020. ' Red:0,',
  5021. ' "1":"Green",',
  5022. ' Green:1',
  5023. ' };',
  5024. 'this.e = 0;',
  5025. 'this.f = 1;',
  5026. 'this.i = 0;'
  5027. ]),
  5028. LinesToStr([
  5029. '$mod.e=1;',
  5030. '$mod.i=$mod.e;'
  5031. ]));
  5032. end;
  5033. procedure TTestModule.TestEnum_ConstFail;
  5034. begin
  5035. StartProgram(false);
  5036. Add([
  5037. 'type TMyEnum = (Red = 100, Green = 101);',
  5038. 'var',
  5039. ' e: TMyEnum;',
  5040. ' f: TMyEnum = Green;',
  5041. 'begin',
  5042. ' e:=green;']);
  5043. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5044. ConvertProgram;
  5045. end;
  5046. procedure TTestModule.TestEnum_Functions;
  5047. begin
  5048. StartProgram(false);
  5049. Add([
  5050. 'type TMyEnum = (Red, Green);',
  5051. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5052. 'var',
  5053. ' v: longint;',
  5054. ' s: string;',
  5055. 'begin',
  5056. ' val(s,e,v);',
  5057. ' val(s,e,i);',
  5058. 'end;',
  5059. 'var',
  5060. ' e: TMyEnum;',
  5061. ' i: longint;',
  5062. ' s: string;',
  5063. ' b: boolean;',
  5064. 'begin',
  5065. ' i:=ord(red);',
  5066. ' i:=ord(green);',
  5067. ' i:=ord(e);',
  5068. ' i:=ord(b);',
  5069. ' e:=low(tmyenum);',
  5070. ' e:=low(e);',
  5071. ' b:=low(boolean);',
  5072. ' e:=high(tmyenum);',
  5073. ' e:=high(e);',
  5074. ' b:=high(boolean);',
  5075. ' e:=pred(green);',
  5076. ' e:=pred(e);',
  5077. ' b:=pred(b);',
  5078. ' e:=succ(red);',
  5079. ' e:=succ(e);',
  5080. ' b:=succ(b);',
  5081. ' e:=tmyenum(1);',
  5082. ' e:=tmyenum(i);',
  5083. ' s:=str(e);',
  5084. ' str(e,s);',
  5085. ' str(red,s);',
  5086. ' s:=str(e:3);',
  5087. ' writestr(s,e:3,red);',
  5088. ' val(s,e,i);',
  5089. ' e:=TMyEnum(i);',
  5090. ' i:=longint(e);']);
  5091. ConvertProgram;
  5092. CheckSource('TestEnum_Functions',
  5093. LinesToStr([ // statements
  5094. 'this.TMyEnum = {',
  5095. ' "0":"Red",',
  5096. ' Red:0,',
  5097. ' "1":"Green",',
  5098. ' Green:1',
  5099. ' };',
  5100. 'this.DoIt = function (e, i) {',
  5101. ' var v = 0;',
  5102. ' var s = "";',
  5103. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5104. ' v = w;',
  5105. ' }));',
  5106. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5107. '};',
  5108. 'this.e = 0;',
  5109. 'this.i = 0;',
  5110. 'this.s = "";',
  5111. 'this.b = false;',
  5112. '']),
  5113. LinesToStr([
  5114. '$mod.i=$mod.TMyEnum.Red;',
  5115. '$mod.i=$mod.TMyEnum.Green;',
  5116. '$mod.i=$mod.e;',
  5117. '$mod.i=$mod.b+0;',
  5118. '$mod.e=$mod.TMyEnum.Red;',
  5119. '$mod.e=$mod.TMyEnum.Red;',
  5120. '$mod.b=false;',
  5121. '$mod.e=$mod.TMyEnum.Green;',
  5122. '$mod.e=$mod.TMyEnum.Green;',
  5123. '$mod.b=true;',
  5124. '$mod.e=$mod.TMyEnum.Green-1;',
  5125. '$mod.e=$mod.e-1;',
  5126. '$mod.b=false;',
  5127. '$mod.e=$mod.TMyEnum.Red+1;',
  5128. '$mod.e=$mod.e+1;',
  5129. '$mod.b=true;',
  5130. '$mod.e=1;',
  5131. '$mod.e=$mod.i;',
  5132. '$mod.s = $mod.TMyEnum[$mod.e];',
  5133. '$mod.s = $mod.TMyEnum[$mod.e];',
  5134. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5135. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5136. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5137. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5138. ' $mod.i = v;',
  5139. '});',
  5140. '$mod.e=$mod.i;',
  5141. '$mod.i=$mod.e;',
  5142. '']));
  5143. end;
  5144. procedure TTestModule.TestEnum_AsParams;
  5145. begin
  5146. StartProgram(false);
  5147. Add('type TEnum = (Red,Blue);');
  5148. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5149. Add('var vJ: TEnum;');
  5150. Add('begin');
  5151. Add(' vg:=vg;');
  5152. Add(' vj:=vh;');
  5153. Add(' vi:=vi;');
  5154. Add(' doit(vg,vg,vg);');
  5155. Add(' doit(vh,vh,vj);');
  5156. Add(' doit(vi,vi,vi);');
  5157. Add(' doit(vj,vj,vj);');
  5158. Add('end;');
  5159. Add('var i: TEnum;');
  5160. Add('begin');
  5161. Add(' doit(i,i,i);');
  5162. ConvertProgram;
  5163. CheckSource('TestEnum_AsParams',
  5164. LinesToStr([ // statements
  5165. 'this.TEnum = {',
  5166. ' "0": "Red",',
  5167. ' Red: 0,',
  5168. ' "1": "Blue",',
  5169. ' Blue: 1',
  5170. '};',
  5171. 'this.DoIt = function (vG,vH,vI) {',
  5172. ' var vJ = 0;',
  5173. ' vG = vG;',
  5174. ' vJ = vH;',
  5175. ' vI.set(vI.get());',
  5176. ' $mod.DoIt(vG, vG, {',
  5177. ' get: function () {',
  5178. ' return vG;',
  5179. ' },',
  5180. ' set: function (v) {',
  5181. ' vG = v;',
  5182. ' }',
  5183. ' });',
  5184. ' $mod.DoIt(vH, vH, {',
  5185. ' get: function () {',
  5186. ' return vJ;',
  5187. ' },',
  5188. ' set: function (v) {',
  5189. ' vJ = v;',
  5190. ' }',
  5191. ' });',
  5192. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5193. ' $mod.DoIt(vJ, vJ, {',
  5194. ' get: function () {',
  5195. ' return vJ;',
  5196. ' },',
  5197. ' set: function (v) {',
  5198. ' vJ = v;',
  5199. ' }',
  5200. ' });',
  5201. '};',
  5202. 'this.i = 0;'
  5203. ]),
  5204. LinesToStr([
  5205. '$mod.DoIt($mod.i,$mod.i,{',
  5206. ' p: $mod,',
  5207. ' get: function () {',
  5208. ' return this.p.i;',
  5209. ' },',
  5210. ' set: function (v) {',
  5211. ' this.p.i = v;',
  5212. ' }',
  5213. '});'
  5214. ]));
  5215. end;
  5216. procedure TTestModule.TestEnumRange_Array;
  5217. begin
  5218. StartProgram(false);
  5219. Add([
  5220. 'type',
  5221. ' TEnum = (Red, Green, Blue);',
  5222. ' TEnumRg = green..blue;',
  5223. ' TArr = array[TEnumRg] of byte;',
  5224. ' TArr2 = array[green..blue] of byte;',
  5225. 'var',
  5226. ' a: TArr;',
  5227. ' b: TArr = (3,4);',
  5228. ' c: TArr2 = (5,6);',
  5229. 'begin',
  5230. ' a[green] := b[blue];',
  5231. ' c[green] := c[blue];',
  5232. '']);
  5233. ConvertProgram;
  5234. CheckSource('TestEnumRange_Array',
  5235. LinesToStr([ // statements
  5236. 'this.TEnum = {',
  5237. ' "0": "Red",',
  5238. ' Red: 0,',
  5239. ' "1": "Green",',
  5240. ' Green: 1,',
  5241. ' "2": "Blue",',
  5242. ' Blue: 2',
  5243. '};',
  5244. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5245. 'this.b = [3, 4];',
  5246. 'this.c = [5, 6];',
  5247. '']),
  5248. LinesToStr([
  5249. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5250. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5251. '']));
  5252. end;
  5253. procedure TTestModule.TestEnum_ForIn;
  5254. begin
  5255. StartProgram(false);
  5256. Add([
  5257. 'type',
  5258. ' TEnum = (Red, Green, Blue);',
  5259. ' TEnumRg = green..blue;',
  5260. ' TArr = array[TEnum] of byte;',
  5261. ' TArrRg = array[TEnumRg] of byte;',
  5262. 'var',
  5263. ' e: TEnum;',
  5264. ' a1: TArr = (3,4,5);',
  5265. ' a2: TArrRg = (11,12);',
  5266. ' b: byte;',
  5267. 'begin',
  5268. ' for e in TEnum do ;',
  5269. ' for e in TEnumRg do ;',
  5270. ' for e in TArr do ;',
  5271. ' for e in TArrRg do ;',
  5272. ' for b in a1 do ;',
  5273. ' for b in a2 do ;',
  5274. '']);
  5275. ConvertProgram;
  5276. CheckSource('TestEnum_ForIn',
  5277. LinesToStr([ // statements
  5278. 'this.TEnum = {',
  5279. ' "0": "Red",',
  5280. ' Red: 0,',
  5281. ' "1": "Green",',
  5282. ' Green: 1,',
  5283. ' "2": "Blue",',
  5284. ' Blue: 2',
  5285. '};',
  5286. 'this.e = 0;',
  5287. 'this.a1 = [3, 4, 5];',
  5288. 'this.a2 = [11, 12];',
  5289. 'this.b = 0;',
  5290. '']),
  5291. LinesToStr([
  5292. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5293. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5294. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5295. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5296. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5297. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5298. '']));
  5299. end;
  5300. procedure TTestModule.TestEnum_ScopedNumber;
  5301. begin
  5302. Converter.Options:=Converter.Options+[coEnumNumbers];
  5303. StartProgram(false);
  5304. Add([
  5305. 'type',
  5306. ' TEnum = (Red, Green);',
  5307. 'var',
  5308. ' e: TEnum;',
  5309. 'begin',
  5310. ' e:=TEnum.Green;',
  5311. '']);
  5312. ConvertProgram;
  5313. CheckSource('TestEnum_ScopedNumber',
  5314. LinesToStr([ // statements
  5315. 'this.TEnum = {',
  5316. ' "0": "Red",',
  5317. ' Red: 0,',
  5318. ' "1": "Green",',
  5319. ' Green: 1',
  5320. '};',
  5321. 'this.e = 0;',
  5322. '']),
  5323. LinesToStr([
  5324. '$mod.e = 1;']));
  5325. end;
  5326. procedure TTestModule.TestEnum_InFunction;
  5327. begin
  5328. StartProgram(false);
  5329. Add([
  5330. 'const TEnum = 3;',
  5331. 'procedure DoIt;',
  5332. 'type',
  5333. ' TEnum = (Red, Green, Blue);',
  5334. ' procedure Sub;',
  5335. ' type',
  5336. ' TEnumSub = (Left, Right);',
  5337. ' var',
  5338. ' es: TEnumSub;',
  5339. ' begin',
  5340. ' es:=Left;',
  5341. ' end;',
  5342. 'var',
  5343. ' e, e2: TEnum;',
  5344. 'begin',
  5345. ' if e in [red,blue] then e2:=e;',
  5346. 'end;',
  5347. 'begin']);
  5348. ConvertProgram;
  5349. CheckSource('TestEnum_InFunction',
  5350. LinesToStr([ // statements
  5351. 'this.TEnum = 3;',
  5352. 'var TEnum$1 = {',
  5353. ' "0":"Red",',
  5354. ' Red:0,',
  5355. ' "1":"Green",',
  5356. ' Green:1,',
  5357. ' "2":"Blue",',
  5358. ' Blue:2',
  5359. ' };',
  5360. 'var TEnumSub = {',
  5361. ' "0": "Left",',
  5362. ' Left: 0,',
  5363. ' "1": "Right",',
  5364. ' Right: 1',
  5365. '};',
  5366. 'this.DoIt = function () {',
  5367. ' function Sub() {',
  5368. ' var es = 0;',
  5369. ' es = TEnumSub.Left;',
  5370. ' };',
  5371. ' var e = 0;',
  5372. ' var e2 = 0;',
  5373. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5374. '};',
  5375. '']),
  5376. LinesToStr([
  5377. '']));
  5378. end;
  5379. procedure TTestModule.TestSet_Enum;
  5380. begin
  5381. StartProgram(false);
  5382. Add([
  5383. 'type',
  5384. ' TColor = (Red, Green, Blue);',
  5385. ' TColors = set of TColor;',
  5386. 'var',
  5387. ' c: TColor;',
  5388. ' s: TColors;',
  5389. ' t: TColors = [];',
  5390. ' u: TColors = [Red];',
  5391. 'begin',
  5392. ' s:=[];',
  5393. ' s:=[Green];',
  5394. ' s:=[Green,Blue];',
  5395. ' s:=[Red..Blue];',
  5396. ' s:=[Red,Green..Blue];',
  5397. ' s:=[Red,c];',
  5398. ' s:=t;',
  5399. ' s:=default(TColors);',
  5400. '']);
  5401. ConvertProgram;
  5402. CheckSource('TestSet',
  5403. LinesToStr([ // statements
  5404. 'this.TColor = {',
  5405. ' "0":"Red",',
  5406. ' Red:0,',
  5407. ' "1":"Green",',
  5408. ' Green:1,',
  5409. ' "2":"Blue",',
  5410. ' Blue:2',
  5411. ' };',
  5412. 'this.c = 0;',
  5413. 'this.s = {};',
  5414. 'this.t = {};',
  5415. 'this.u = rtl.createSet($mod.TColor.Red);'
  5416. ]),
  5417. LinesToStr([
  5418. '$mod.s={};',
  5419. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5420. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5421. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5422. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5423. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5424. '$mod.s=rtl.refSet($mod.t);',
  5425. '$mod.s={};',
  5426. '']));
  5427. end;
  5428. procedure TTestModule.TestSet_Operators;
  5429. begin
  5430. StartProgram(false);
  5431. Add('type');
  5432. Add(' TColor = (Red, Green, Blue);');
  5433. Add(' TColors = set of tcolor;');
  5434. Add('var');
  5435. Add(' vC: TColor;');
  5436. Add(' vS: TColors;');
  5437. Add(' vT: TColors;');
  5438. Add(' vU: TColors;');
  5439. Add(' B: boolean;');
  5440. Add('begin');
  5441. Add(' include(vs,green);');
  5442. Add(' exclude(vs,vc);');
  5443. Add(' vs:=vt+vu;');
  5444. Add(' vs:=vt+[red];');
  5445. Add(' vs:=[red]+vt;');
  5446. Add(' vs:=[red]+[green];');
  5447. Add(' vs:=vt-vu;');
  5448. Add(' vs:=vt-[red];');
  5449. Add(' vs:=[red]-vt;');
  5450. Add(' vs:=[red]-[green];');
  5451. Add(' vs:=vt*vu;');
  5452. Add(' vs:=vt*[red];');
  5453. Add(' vs:=[red]*vt;');
  5454. Add(' vs:=[red]*[green];');
  5455. Add(' vs:=vt><vu;');
  5456. Add(' vs:=vt><[red];');
  5457. Add(' vs:=[red]><vt;');
  5458. Add(' vs:=[red]><[green];');
  5459. Add(' b:=vt=vu;');
  5460. Add(' b:=vt=[red];');
  5461. Add(' b:=[red]=vt;');
  5462. Add(' b:=[red]=[green];');
  5463. Add(' b:=vt<>vu;');
  5464. Add(' b:=vt<>[red];');
  5465. Add(' b:=[red]<>vt;');
  5466. Add(' b:=[red]<>[green];');
  5467. Add(' b:=vt<=vu;');
  5468. Add(' b:=vt<=[red];');
  5469. Add(' b:=[red]<=vt;');
  5470. Add(' b:=[red]<=[green];');
  5471. Add(' b:=vt>=vu;');
  5472. Add(' b:=vt>=[red];');
  5473. Add(' b:=[red]>=vt;');
  5474. Add(' b:=[red]>=[green];');
  5475. ConvertProgram;
  5476. CheckSource('TestSet_Operators',
  5477. LinesToStr([ // statements
  5478. 'this.TColor = {',
  5479. ' "0":"Red",',
  5480. ' Red:0,',
  5481. ' "1":"Green",',
  5482. ' Green:1,',
  5483. ' "2":"Blue",',
  5484. ' Blue:2',
  5485. ' };',
  5486. 'this.vC = 0;',
  5487. 'this.vS = {};',
  5488. 'this.vT = {};',
  5489. 'this.vU = {};',
  5490. 'this.B = false;'
  5491. ]),
  5492. LinesToStr([
  5493. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5494. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5495. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5496. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5497. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5498. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5499. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5500. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5501. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5502. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5503. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5504. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5505. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5506. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5507. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5508. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5509. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5510. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5511. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5512. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5513. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5514. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5515. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5516. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5517. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5518. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5519. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5520. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5521. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5522. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5523. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5524. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5525. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5526. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5527. '']));
  5528. end;
  5529. procedure TTestModule.TestSet_Operator_In;
  5530. begin
  5531. StartProgram(false);
  5532. Add('type');
  5533. Add(' TColor = (Red, Green, Blue);');
  5534. Add(' TColors = set of tcolor;');
  5535. Add('var');
  5536. Add(' vC: tcolor;');
  5537. Add(' vT: tcolors;');
  5538. Add(' B: boolean;');
  5539. Add('begin');
  5540. Add(' b:=red in vt;');
  5541. Add(' b:=vc in vt;');
  5542. Add(' b:=green in [red..blue];');
  5543. Add(' b:=vc in [red..blue];');
  5544. Add(' ');
  5545. Add(' if red in vt then ;');
  5546. Add(' while vC in vt do ;');
  5547. Add(' repeat');
  5548. Add(' until vC in vt;');
  5549. ConvertProgram;
  5550. CheckSource('TestSet_Operator_In',
  5551. LinesToStr([ // statements
  5552. 'this.TColor = {',
  5553. ' "0":"Red",',
  5554. ' Red:0,',
  5555. ' "1":"Green",',
  5556. ' Green:1,',
  5557. ' "2":"Blue",',
  5558. ' Blue:2',
  5559. ' };',
  5560. 'this.vC = 0;',
  5561. 'this.vT = {};',
  5562. 'this.B = false;'
  5563. ]),
  5564. LinesToStr([
  5565. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5566. '$mod.B = $mod.vC in $mod.vT;',
  5567. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5568. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5569. 'if ($mod.TColor.Red in $mod.vT) ;',
  5570. 'while ($mod.vC in $mod.vT) {',
  5571. '};',
  5572. 'do {',
  5573. '} while (!($mod.vC in $mod.vT));',
  5574. '']));
  5575. end;
  5576. procedure TTestModule.TestSet_Functions;
  5577. begin
  5578. StartProgram(false);
  5579. Add('type');
  5580. Add(' TMyEnum = (Red, Green);');
  5581. Add(' TMyEnums = set of TMyEnum;');
  5582. Add('var');
  5583. Add(' e: TMyEnum;');
  5584. Add(' s: TMyEnums;');
  5585. Add('begin');
  5586. Add(' e:=Low(TMyEnums);');
  5587. Add(' e:=Low(s);');
  5588. Add(' e:=High(TMyEnums);');
  5589. Add(' e:=High(s);');
  5590. ConvertProgram;
  5591. CheckSource('TestSetFunctions',
  5592. LinesToStr([ // statements
  5593. 'this.TMyEnum = {',
  5594. ' "0":"Red",',
  5595. ' Red:0,',
  5596. ' "1":"Green",',
  5597. ' Green:1',
  5598. ' };',
  5599. 'this.e = 0;',
  5600. 'this.s = {};'
  5601. ]),
  5602. LinesToStr([
  5603. '$mod.e=$mod.TMyEnum.Red;',
  5604. '$mod.e=$mod.TMyEnum.Red;',
  5605. '$mod.e=$mod.TMyEnum.Green;',
  5606. '$mod.e=$mod.TMyEnum.Green;',
  5607. '']));
  5608. end;
  5609. procedure TTestModule.TestSet_PassAsArgClone;
  5610. begin
  5611. StartProgram(false);
  5612. Add('type');
  5613. Add(' TMyEnum = (Red, Green);');
  5614. Add(' TMyEnums = set of TMyEnum;');
  5615. Add('procedure DoDefault(s: tmyenums); begin end;');
  5616. Add('procedure DoConst(const s: tmyenums); begin end;');
  5617. Add('var');
  5618. Add(' aSet: tmyenums;');
  5619. Add('begin');
  5620. Add(' dodefault(aset);');
  5621. Add(' doconst(aset);');
  5622. ConvertProgram;
  5623. CheckSource('TestSetFunctions',
  5624. LinesToStr([ // statements
  5625. 'this.TMyEnum = {',
  5626. ' "0":"Red",',
  5627. ' Red:0,',
  5628. ' "1":"Green",',
  5629. ' Green:1',
  5630. ' };',
  5631. 'this.DoDefault = function (s) {',
  5632. '};',
  5633. 'this.DoConst = function (s) {',
  5634. '};',
  5635. 'this.aSet = {};'
  5636. ]),
  5637. LinesToStr([
  5638. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5639. '$mod.DoConst($mod.aSet);',
  5640. '']));
  5641. end;
  5642. procedure TTestModule.TestSet_AsParams;
  5643. begin
  5644. StartProgram(false);
  5645. Add([
  5646. 'type TEnum = (Red,Blue);',
  5647. 'type TEnums = set of TEnum;',
  5648. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5649. 'var vJ: TEnums;',
  5650. 'begin',
  5651. ' Include(vg,red);',
  5652. ' Include(result,blue);',
  5653. ' vg:=vg;',
  5654. ' vj:=vh;',
  5655. ' vi:=vi;',
  5656. ' doit(vg,vg,vg);',
  5657. ' doit(vh,vh,vj);',
  5658. ' doit(vi,vi,vi);',
  5659. ' doit(vj,vj,vj);',
  5660. 'end;',
  5661. 'var i: TEnums;',
  5662. 'begin',
  5663. ' doit(i,i,i);']);
  5664. ConvertProgram;
  5665. CheckSource('TestSet_AsParams',
  5666. LinesToStr([ // statements
  5667. 'this.TEnum = {',
  5668. ' "0": "Red",',
  5669. ' Red: 0,',
  5670. ' "1": "Blue",',
  5671. ' Blue: 1',
  5672. '};',
  5673. 'this.DoIt = function (vG,vH,vI) {',
  5674. ' var Result = {};',
  5675. ' var vJ = {};',
  5676. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5677. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5678. ' vG = rtl.refSet(vG);',
  5679. ' vJ = rtl.refSet(vH);',
  5680. ' vI.set(rtl.refSet(vI.get()));',
  5681. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5682. ' get: function () {',
  5683. ' return vG;',
  5684. ' },',
  5685. ' set: function (v) {',
  5686. ' vG = v;',
  5687. ' }',
  5688. ' });',
  5689. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5690. ' get: function () {',
  5691. ' return vJ;',
  5692. ' },',
  5693. ' set: function (v) {',
  5694. ' vJ = v;',
  5695. ' }',
  5696. ' });',
  5697. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5698. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5699. ' get: function () {',
  5700. ' return vJ;',
  5701. ' },',
  5702. ' set: function (v) {',
  5703. ' vJ = v;',
  5704. ' }',
  5705. ' });',
  5706. ' return Result;',
  5707. '};',
  5708. 'this.i = {};'
  5709. ]),
  5710. LinesToStr([
  5711. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5712. ' p: $mod,',
  5713. ' get: function () {',
  5714. ' return this.p.i;',
  5715. ' },',
  5716. ' set: function (v) {',
  5717. ' this.p.i = v;',
  5718. ' }',
  5719. '});'
  5720. ]));
  5721. end;
  5722. procedure TTestModule.TestSet_Property;
  5723. begin
  5724. StartProgram(false);
  5725. Add('type');
  5726. Add(' TEnum = (Red,Blue);');
  5727. Add(' TEnums = set of TEnum;');
  5728. Add(' TObject = class');
  5729. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5730. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5731. Add(' property Colors: TEnums read GetColors write SetColors;');
  5732. Add(' end;');
  5733. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5734. Add('begin end;');
  5735. Add('var Obj: TObject;');
  5736. Add('begin');
  5737. Add(' Include(Obj.Colors,Red);');
  5738. Add(' Exclude(Obj.Colors,Red);');
  5739. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5740. ConvertProgram;
  5741. CheckSource('TestSet_Property',
  5742. LinesToStr([ // statements
  5743. 'this.TEnum = {',
  5744. ' "0": "Red",',
  5745. ' Red: 0,',
  5746. ' "1": "Blue",',
  5747. ' Blue: 1',
  5748. '};',
  5749. 'rtl.createClass($mod, "TObject", null, function () {',
  5750. ' this.$init = function () {',
  5751. ' };',
  5752. ' this.$final = function () {',
  5753. ' };',
  5754. '});',
  5755. 'this.DoIt = function (i, j, k, l) {',
  5756. '};',
  5757. 'this.Obj = null;',
  5758. '']),
  5759. LinesToStr([
  5760. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5761. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5762. '']));
  5763. end;
  5764. procedure TTestModule.TestSet_EnumConst;
  5765. begin
  5766. StartProgram(false);
  5767. Add([
  5768. 'type',
  5769. ' TEnum = (Red,Blue);',
  5770. ' TEnums = set of TEnum;',
  5771. 'const',
  5772. ' Orange = red;',
  5773. 'var',
  5774. ' Enum: tenum;',
  5775. ' Enums: tenums;',
  5776. 'begin',
  5777. ' Include(enums,orange);',
  5778. ' Exclude(enums,orange);',
  5779. ' if orange in enums then;',
  5780. ' if orange in [orange,red] then;']);
  5781. ConvertProgram;
  5782. CheckSource('TestSet_EnumConst',
  5783. LinesToStr([ // statements
  5784. 'this.TEnum = {',
  5785. ' "0": "Red",',
  5786. ' Red: 0,',
  5787. ' "1": "Blue",',
  5788. ' Blue: 1',
  5789. '};',
  5790. 'this.Orange = $mod.TEnum.Red;',
  5791. 'this.Enum = 0;',
  5792. 'this.Enums = {};',
  5793. '']),
  5794. LinesToStr([
  5795. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  5796. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  5797. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  5798. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  5799. '']));
  5800. end;
  5801. procedure TTestModule.TestSet_IntConst;
  5802. begin
  5803. StartProgram(false);
  5804. Add([
  5805. 'type',
  5806. ' TEnums = set of Byte;',
  5807. 'const',
  5808. ' Orange = 0;',
  5809. 'var',
  5810. ' Enum: byte;',
  5811. ' Enums: tenums;',
  5812. 'begin',
  5813. ' Enums:=[];',
  5814. ' Enums:=[0];',
  5815. ' Enums:=[1..2];',
  5816. //' Include(enums,orange);',
  5817. //' Exclude(enums,orange);',
  5818. ' if orange in enums then;',
  5819. ' if orange in [orange,1] then;']);
  5820. ConvertProgram;
  5821. CheckSource('TestSet_IntConst',
  5822. LinesToStr([ // statements
  5823. 'this.Orange = 0;',
  5824. 'this.Enum = 0;',
  5825. 'this.Enums = {};',
  5826. '']),
  5827. LinesToStr([
  5828. '$mod.Enums = {};',
  5829. '$mod.Enums = rtl.createSet(0);',
  5830. '$mod.Enums = rtl.createSet(null, 1, 2);',
  5831. 'if (0 in $mod.Enums) ;',
  5832. 'if (0 in rtl.createSet(0, 1)) ;',
  5833. '']));
  5834. end;
  5835. procedure TTestModule.TestSet_AnonymousEnumType;
  5836. begin
  5837. StartProgram(false);
  5838. Add('type');
  5839. Add(' TFlags = set of (red, green);');
  5840. Add('const');
  5841. Add(' favorite = red;');
  5842. Add('var');
  5843. Add(' f: TFlags;');
  5844. Add(' i: longint;');
  5845. Add('begin');
  5846. Add(' Include(f,red);');
  5847. Add(' Include(f,favorite);');
  5848. Add(' i:=ord(red);');
  5849. Add(' i:=ord(favorite);');
  5850. Add(' i:=ord(low(TFlags));');
  5851. Add(' i:=ord(low(f));');
  5852. Add(' i:=ord(low(favorite));');
  5853. Add(' i:=ord(high(TFlags));');
  5854. Add(' i:=ord(high(f));');
  5855. Add(' i:=ord(high(favorite));');
  5856. Add(' f:=[green,favorite];');
  5857. ConvertProgram;
  5858. CheckSource('TestSet_AnonymousEnumType',
  5859. LinesToStr([ // statements
  5860. 'this.TFlags$a = {',
  5861. ' "0": "red",',
  5862. ' red: 0,',
  5863. ' "1": "green",',
  5864. ' green: 1',
  5865. '};',
  5866. 'this.favorite = $mod.TFlags$a.red;',
  5867. 'this.f = {};',
  5868. 'this.i = 0;',
  5869. '']),
  5870. LinesToStr([
  5871. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5872. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  5873. '$mod.i = $mod.TFlags$a.red;',
  5874. '$mod.i = $mod.TFlags$a.red;',
  5875. '$mod.i = $mod.TFlags$a.red;',
  5876. '$mod.i = $mod.TFlags$a.red;',
  5877. '$mod.i = $mod.TFlags$a.red;',
  5878. '$mod.i = $mod.TFlags$a.green;',
  5879. '$mod.i = $mod.TFlags$a.green;',
  5880. '$mod.i = $mod.TFlags$a.green;',
  5881. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  5882. '']));
  5883. end;
  5884. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  5885. begin
  5886. exit;
  5887. StartProgram(false);
  5888. Add([
  5889. 'type',
  5890. ' TAtoZ = ''A''..''Z'';',
  5891. ' TSetOfAZ = set of TAtoZ;',
  5892. 'var',
  5893. ' c: char;',
  5894. ' a: TAtoZ;',
  5895. ' s: TSetOfAZ = [''P'',''A''];',
  5896. ' i: longint;',
  5897. 'begin',
  5898. ' Include(s,''S'');',
  5899. ' Include(s,c);',
  5900. ' Include(s,a);',
  5901. ' c:=low(TAtoZ);',
  5902. ' i:=ord(low(TAtoZ));',
  5903. ' a:=high(TAtoZ);',
  5904. ' a:=high(TSetOfAtoZ);',
  5905. ' s:=[a,c,''M''];',
  5906. '']);
  5907. ConvertProgram;
  5908. CheckSource('TestSet_AnonymousEnumTypeChar',
  5909. LinesToStr([ // statements
  5910. '']),
  5911. LinesToStr([
  5912. '']));
  5913. end;
  5914. procedure TTestModule.TestSet_ConstEnum;
  5915. begin
  5916. StartProgram(false);
  5917. Add([
  5918. 'type',
  5919. ' TEnum = (red,blue,green);',
  5920. ' TEnums = set of TEnum;',
  5921. 'const',
  5922. ' teAny = [low(TEnum)..high(TEnum)];',
  5923. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  5924. 'var',
  5925. ' e: TEnum;',
  5926. ' s: TEnums;',
  5927. 'begin',
  5928. ' if blue in teAny then;',
  5929. ' if blue in teAny+[e] then;',
  5930. ' if blue in teAny+teRedBlue then;',
  5931. ' if e in [red,blue] then;',
  5932. ' s:=teAny;',
  5933. ' s:=teAny+[e];',
  5934. ' s:=[e]+teAny;',
  5935. ' s:=teAny+teRedBlue;',
  5936. ' s:=teAny+teRedBlue+[e];',
  5937. '']);
  5938. ConvertProgram;
  5939. CheckSource('TestSet_ConstEnum',
  5940. LinesToStr([ // statements
  5941. 'this.TEnum = {',
  5942. ' "0": "red",',
  5943. ' red: 0,',
  5944. ' "1": "blue",',
  5945. ' blue: 1,',
  5946. ' "2": "green",',
  5947. ' green: 2',
  5948. '};',
  5949. 'this.teAny = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green);',
  5950. 'this.teRedBlue = rtl.createSet(null, $mod.TEnum.red, $mod.TEnum.green - 1);',
  5951. 'this.e = 0;',
  5952. 'this.s = {};',
  5953. '']),
  5954. LinesToStr([
  5955. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  5956. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  5957. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  5958. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  5959. '$mod.s = rtl.refSet($mod.teAny);',
  5960. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  5961. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  5962. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  5963. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  5964. '']));
  5965. end;
  5966. procedure TTestModule.TestSet_ConstChar;
  5967. begin
  5968. StartProgram(false);
  5969. Add([
  5970. 'const',
  5971. ' LowChars = [''a''..''z''];',
  5972. ' Chars = LowChars+[''A''..''Z''];',
  5973. ' sc = [''А'', ''Я''];',
  5974. 'var',
  5975. ' c: char;',
  5976. ' s: string;',
  5977. 'begin',
  5978. ' if c in lowchars then ;',
  5979. ' if ''a'' in lowchars then ;',
  5980. ' if s[1] in lowchars then ;',
  5981. ' if c in chars then ;',
  5982. ' if c in [''a''..''z'',''_''] then ;',
  5983. ' if ''b'' in [''a''..''z'',''_''] then ;',
  5984. ' if ''Я'' in sc then ;',
  5985. ' if 3=ord('' '') then ;',
  5986. '']);
  5987. ConvertProgram;
  5988. CheckSource('TestSet_ConstChar',
  5989. LinesToStr([ // statements
  5990. 'this.LowChars = rtl.createSet(null, 97, 122);',
  5991. 'this.Chars = rtl.unionSet($mod.LowChars, rtl.createSet(null, 65, 90));',
  5992. 'this.sc = rtl.createSet(1040, 1071);',
  5993. 'this.c = "";',
  5994. 'this.s = "";',
  5995. '']),
  5996. LinesToStr([
  5997. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  5998. 'if (97 in $mod.LowChars) ;',
  5999. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6000. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6001. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6002. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6003. 'if (1071 in $mod.sc) ;',
  6004. 'if (3 === 32) ;',
  6005. '']));
  6006. end;
  6007. procedure TTestModule.TestSet_ConstInt;
  6008. begin
  6009. StartProgram(false);
  6010. Add([
  6011. 'const',
  6012. ' Months = [1..12];',
  6013. ' Mirror = [-12..-1]+Months;',
  6014. 'var',
  6015. ' i: smallint;',
  6016. 'begin',
  6017. ' if 3 in Months then;',
  6018. ' if i in Months+[i] then;',
  6019. ' if i in Months+Mirror then;',
  6020. ' if i in [4..6,8] then;',
  6021. '']);
  6022. ConvertProgram;
  6023. CheckSource('TestSet_ConstInt',
  6024. LinesToStr([ // statements
  6025. 'this.Months = rtl.createSet(null, 1, 12);',
  6026. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), $mod.Months);',
  6027. 'this.i = 0;',
  6028. '']),
  6029. LinesToStr([
  6030. 'if (3 in $mod.Months) ;',
  6031. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6032. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6033. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6034. '']));
  6035. end;
  6036. procedure TTestModule.TestSet_InFunction;
  6037. begin
  6038. StartProgram(false);
  6039. Add([
  6040. 'const',
  6041. ' TEnum = 3;',
  6042. ' TSetOfEnum = 4;',
  6043. ' TSetOfAno = 5;',
  6044. 'procedure DoIt;',
  6045. 'type',
  6046. ' TEnum = (red, blue);',
  6047. ' TSetOfEnum = set of TEnum;',
  6048. ' TSetOfAno = set of (up,down);',
  6049. 'var',
  6050. ' e: TEnum;',
  6051. ' se: TSetOfEnum;',
  6052. ' sa: TSetOfAno;',
  6053. 'begin',
  6054. ' se:=[e];',
  6055. ' sa:=[up];',
  6056. 'end;',
  6057. 'begin',
  6058. '']);
  6059. ConvertProgram;
  6060. CheckSource('TestSet_InFunction',
  6061. LinesToStr([ // statements
  6062. 'this.TEnum = 3;',
  6063. 'this.TSetOfEnum = 4;',
  6064. 'this.TSetOfAno = 5;',
  6065. 'var TEnum$1 = {',
  6066. ' "0": "red",',
  6067. ' red: 0,',
  6068. ' "1": "blue",',
  6069. ' blue: 1',
  6070. '};',
  6071. 'var TSetOfAno$a = {',
  6072. ' "0": "up",',
  6073. ' up: 0,',
  6074. ' "1": "down",',
  6075. ' down: 1',
  6076. '};',
  6077. 'this.DoIt = function () {',
  6078. ' var e = 0;',
  6079. ' var se = {};',
  6080. ' var sa = {};',
  6081. ' se = rtl.createSet(e);',
  6082. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6083. '};',
  6084. '']),
  6085. LinesToStr([
  6086. '']));
  6087. end;
  6088. procedure TTestModule.TestSet_ForIn;
  6089. begin
  6090. StartProgram(false);
  6091. Add([
  6092. 'type',
  6093. ' TEnum = (Red, Green, Blue);',
  6094. ' TEnumRg = green..blue;',
  6095. ' TSetOfEnum = set of TEnum;',
  6096. ' TSetOfEnumRg = set of TEnumRg;',
  6097. 'var',
  6098. ' e, e2: TEnum;',
  6099. ' er: TEnum;',
  6100. ' s: TSetOfEnum;',
  6101. 'begin',
  6102. ' for e in TSetOfEnum do ;',
  6103. ' for e in TSetOfEnumRg do ;',
  6104. ' for e in [] do e2:=e;',
  6105. ' for e in [red..green] do e2:=e;',
  6106. ' for e in [green,blue] do e2:=e;',
  6107. ' for e in [red,blue] do e2:=e;',
  6108. ' for e in s do e2:=e;',
  6109. ' for er in TSetOfEnumRg do ;',
  6110. '']);
  6111. ConvertProgram;
  6112. CheckSource('TestSet_ForIn',
  6113. LinesToStr([ // statements
  6114. 'this.TEnum = {',
  6115. ' "0":"Red",',
  6116. ' Red:0,',
  6117. ' "1":"Green",',
  6118. ' Green:1,',
  6119. ' "2":"Blue",',
  6120. ' Blue:2',
  6121. ' };',
  6122. 'this.e = 0;',
  6123. 'this.e2 = 0;',
  6124. 'this.er = 0;',
  6125. 'this.s = {};',
  6126. '']),
  6127. LinesToStr([
  6128. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6129. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6130. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6131. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6132. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6133. 'for (var $l in $mod.s){',
  6134. ' $mod.e = +$l;',
  6135. ' $mod.e2 = $mod.e;',
  6136. '};',
  6137. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6138. '']));
  6139. end;
  6140. procedure TTestModule.TestNestBegin;
  6141. begin
  6142. StartProgram(false);
  6143. Add('begin');
  6144. Add(' begin');
  6145. Add(' begin');
  6146. Add(' end;');
  6147. Add(' begin');
  6148. Add(' if true then ;');
  6149. Add(' end;');
  6150. Add(' end;');
  6151. ConvertProgram;
  6152. CheckSource('TestNestBegin',
  6153. '',
  6154. 'if (true) ;');
  6155. end;
  6156. procedure TTestModule.TestUnitImplVars;
  6157. begin
  6158. StartUnit(false);
  6159. Add('interface');
  6160. Add('implementation');
  6161. Add('var');
  6162. Add(' V1:longint;');
  6163. Add(' V2:longint = 3;');
  6164. Add(' V3:string = ''abc'';');
  6165. ConvertUnit;
  6166. CheckSource('TestUnitImplVars',
  6167. LinesToStr([ // statements
  6168. 'var $impl = $mod.$impl;',
  6169. '']),
  6170. '', // this.$init
  6171. LinesToStr([ // implementation
  6172. '$impl.V1 = 0;',
  6173. '$impl.V2 = 3;',
  6174. '$impl.V3 = "abc";',
  6175. '']) );
  6176. end;
  6177. procedure TTestModule.TestUnitImplConsts;
  6178. begin
  6179. StartUnit(false);
  6180. Add('interface');
  6181. Add('implementation');
  6182. Add('const');
  6183. Add(' v1 = 3;');
  6184. Add(' v2:longint = 4;');
  6185. Add(' v3:string = ''abc'';');
  6186. ConvertUnit;
  6187. CheckSource('TestUnitImplConsts',
  6188. LinesToStr([ // statements
  6189. 'var $impl = $mod.$impl;',
  6190. '']),
  6191. '', // this.$init
  6192. LinesToStr([ // implementation
  6193. '$impl.v1 = 3;',
  6194. '$impl.v2 = 4;',
  6195. '$impl.v3 = "abc";',
  6196. '']) );
  6197. end;
  6198. procedure TTestModule.TestUnitImplRecord;
  6199. begin
  6200. StartUnit(false);
  6201. Add('interface');
  6202. Add('implementation');
  6203. Add('type');
  6204. Add(' TMyRecord = record');
  6205. Add(' i: longint;');
  6206. Add(' end;');
  6207. Add('var aRec: TMyRecord;');
  6208. Add('initialization');
  6209. Add(' arec.i:=3;');
  6210. ConvertUnit;
  6211. CheckSource('TestUnitImplRecord',
  6212. LinesToStr([ // statements
  6213. 'var $impl = $mod.$impl;',
  6214. '']),
  6215. // this.$init
  6216. '$impl.aRec.i = 3;',
  6217. LinesToStr([ // implementation
  6218. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6219. ' this.i = 0;',
  6220. ' this.$eq = function (b) {',
  6221. ' return this.i === b.i;',
  6222. ' };',
  6223. ' this.$assign = function (s) {',
  6224. ' this.i = s.i;',
  6225. ' return this;',
  6226. ' };',
  6227. '});',
  6228. '$impl.aRec = $impl.TMyRecord.$new();',
  6229. '']) );
  6230. end;
  6231. procedure TTestModule.TestRenameJSNameConflict;
  6232. begin
  6233. StartProgram(false);
  6234. Add('var apply: longint;');
  6235. Add('var bind: longint;');
  6236. Add('var call: longint;');
  6237. Add('begin');
  6238. ConvertProgram;
  6239. CheckSource('TestRenameJSNameConflict',
  6240. LinesToStr([ // statements
  6241. 'this.Apply = 0;',
  6242. 'this.Bind = 0;',
  6243. 'this.Call = 0;'
  6244. ]),
  6245. LinesToStr([ // this.$main
  6246. ''
  6247. ]));
  6248. end;
  6249. procedure TTestModule.TestLocalConst;
  6250. begin
  6251. StartProgram(false);
  6252. Add('procedure DoIt;');
  6253. Add('const');
  6254. Add(' cA: longint = 1;');
  6255. Add(' cB = 2;');
  6256. Add(' procedure Sub;');
  6257. Add(' const');
  6258. Add(' csA = 3;');
  6259. Add(' cB: double = 4;');
  6260. Add(' begin');
  6261. Add(' cb:=cb+csa;');
  6262. Add(' ca:=ca+csa+5;');
  6263. Add(' end;');
  6264. Add('begin');
  6265. Add(' ca:=ca+cb+6;');
  6266. Add('end;');
  6267. Add('begin');
  6268. ConvertProgram;
  6269. CheckSource('TestLocalConst',
  6270. LinesToStr([
  6271. 'var cA = 1;',
  6272. 'var cB = 2;',
  6273. 'var csA = 3;',
  6274. 'var cB$1 = 4;',
  6275. 'this.DoIt = function () {',
  6276. ' function Sub() {',
  6277. ' cB$1 = cB$1 + 3;',
  6278. ' cA = cA + 3 + 5;',
  6279. ' };',
  6280. ' cA = cA + 2 + 6;',
  6281. '};'
  6282. ]),
  6283. LinesToStr([
  6284. ]));
  6285. end;
  6286. procedure TTestModule.TestVarExternal;
  6287. begin
  6288. StartProgram(false);
  6289. Add('var');
  6290. Add(' NaN: double; external name ''Global.NaN'';');
  6291. Add(' d: double;');
  6292. Add('begin');
  6293. Add(' d:=NaN;');
  6294. ConvertProgram;
  6295. CheckSource('TestVarExternal',
  6296. LinesToStr([
  6297. 'this.d = 0.0;'
  6298. ]),
  6299. LinesToStr([
  6300. '$mod.d = Global.NaN;'
  6301. ]));
  6302. end;
  6303. procedure TTestModule.TestVarExternalOtherUnit;
  6304. begin
  6305. AddModuleWithIntfImplSrc('unit2.pas',
  6306. LinesToStr([
  6307. 'var NaN: double; external name ''Global.NaN'';',
  6308. 'var iV: longint;'
  6309. ]),
  6310. '');
  6311. StartUnit(true);
  6312. Add('interface');
  6313. Add('uses unit2;');
  6314. Add('implementation');
  6315. Add('var');
  6316. Add(' d: double;');
  6317. Add(' i: longint; external name ''$i'';');
  6318. Add('begin');
  6319. Add(' d:=nan;');
  6320. Add(' d:=uNit2.nan;');
  6321. Add(' d:=test1.d;');
  6322. Add(' i:=iv;');
  6323. Add(' i:=uNit2.iv;');
  6324. Add(' i:=test1.i;');
  6325. ConvertUnit;
  6326. CheckSource('TestVarExternalOtherUnit',
  6327. LinesToStr([
  6328. 'var $impl = $mod.$impl;',
  6329. '']),
  6330. LinesToStr([ // this.$init
  6331. '$impl.d = Global.NaN;',
  6332. '$impl.d = Global.NaN;',
  6333. '$impl.d = $impl.d;',
  6334. '$i = pas.unit2.iV;',
  6335. '$i = pas.unit2.iV;',
  6336. '$i = $i;',
  6337. '']),
  6338. LinesToStr([ // implementation
  6339. '$impl.d = 0.0;',
  6340. '']) );
  6341. end;
  6342. procedure TTestModule.TestVarAbsoluteFail;
  6343. begin
  6344. StartProgram(false);
  6345. Add([
  6346. 'var',
  6347. ' a: longint;',
  6348. ' b: longword absolute a;',
  6349. 'begin']);
  6350. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6351. ConvertProgram;
  6352. end;
  6353. procedure TTestModule.TestConstExternal;
  6354. begin
  6355. StartProgram(false);
  6356. Add([
  6357. 'const',
  6358. ' PI: double; external name ''Global.PI'';',
  6359. ' Tau = 2*pi;',
  6360. 'var d: double;',
  6361. 'begin',
  6362. ' d:=pi;',
  6363. ' d:=tau+pi;']);
  6364. ConvertProgram;
  6365. CheckSource('TestConstExternal',
  6366. LinesToStr([
  6367. 'this.Tau = 2*Global.PI;',
  6368. 'this.d = 0.0;'
  6369. ]),
  6370. LinesToStr([
  6371. '$mod.d = Global.PI;',
  6372. '$mod.d = $mod.Tau + Global.PI;'
  6373. ]));
  6374. end;
  6375. procedure TTestModule.TestDouble;
  6376. begin
  6377. StartProgram(false);
  6378. Add([
  6379. 'type',
  6380. ' TDateTime = double;',
  6381. 'const',
  6382. ' a = TDateTime(2.7);',
  6383. ' b = a + TDateTime(1.7);',
  6384. ' c = 0.9 + 0.1;',
  6385. ' f0_1 = 0.1;',
  6386. ' f0_3 = 0.3;',
  6387. ' fn0_1 = -0.1;',
  6388. ' fn0_3 = -0.3;',
  6389. ' fn0_003 = -0.003;',
  6390. ' fn0_123456789 = -0.123456789;',
  6391. ' fn300_0 = -300.0;',
  6392. ' fn123456_0 = -123456.0;',
  6393. ' fn1234567_8 = -1234567.8;',
  6394. ' fn12345678_9 = -12345678.9;',
  6395. ' f1_0En12 = 1E-12;',
  6396. ' fn1_0En12 = -1E-12;',
  6397. ' maxdouble = 1.7e+308;',
  6398. ' mindouble = -1.7e+308;',
  6399. ' MinSafeIntDouble = -$1fffffffffffff;',
  6400. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6401. ' MaxSafeIntDouble = $1fffffffffffff;',
  6402. ' DZeroResolution = 1E-12;',
  6403. ' Minus1 = -1E-12;',
  6404. ' EPS = 1E-9;',
  6405. ' DELTA = 0.001;',
  6406. ' Big = 129.789E+100;',
  6407. ' Test0_15 = 0.15;',
  6408. ' Test999 = 2.9999999999999;',
  6409. ' Test111999 = 211199999999999000.0;',
  6410. ' TestMinus111999 = -211199999999999000.0;',
  6411. 'var',
  6412. ' d: double = b;',
  6413. 'begin',
  6414. ' d:=1.0;',
  6415. ' d:=1.0/3.0;',
  6416. ' d:=1/3;',
  6417. ' d:=5.0E-324;',
  6418. ' d:=1.7E308;',
  6419. ' d:=001.00E00;',
  6420. ' d:=002.00E001;',
  6421. ' d:=003.000E000;',
  6422. ' d:=-004.00E-00;',
  6423. ' d:=-005.00E-001;',
  6424. ' d:=10**3;',
  6425. ' d:=10 mod 3;',
  6426. ' d:=10 div 3;',
  6427. ' d:=c;',
  6428. ' d:=f0_1;',
  6429. ' d:=f0_3;',
  6430. ' d:=fn0_1;',
  6431. ' d:=fn0_3;',
  6432. ' d:=fn0_003;',
  6433. ' d:=fn0_123456789;',
  6434. ' d:=fn300_0;',
  6435. ' d:=fn123456_0;',
  6436. ' d:=fn1234567_8;',
  6437. ' d:=fn12345678_9;',
  6438. ' d:=f1_0En12;',
  6439. ' d:=fn1_0En12;',
  6440. ' d:=maxdouble;',
  6441. ' d:=mindouble;',
  6442. ' d:=MinSafeIntDouble;',
  6443. ' d:=double(MinSafeIntDouble);',
  6444. ' d:=MinSafeIntDouble2;',
  6445. ' d:=double(MinSafeIntDouble2);',
  6446. ' d:=MaxSafeIntDouble;',
  6447. ' d:=default(double);',
  6448. '']);
  6449. ConvertProgram;
  6450. CheckSource('TestDouble',
  6451. LinesToStr([
  6452. 'this.a = 2.7;',
  6453. 'this.b = 2.7 + 1.7;',
  6454. 'this.c = 0.9 + 0.1;',
  6455. 'this.f0_1 = 0.1;',
  6456. 'this.f0_3 = 0.3;',
  6457. 'this.fn0_1 = -0.1;',
  6458. 'this.fn0_3 = -0.3;',
  6459. 'this.fn0_003 = -0.003;',
  6460. 'this.fn0_123456789 = -0.123456789;',
  6461. 'this.fn300_0 = -300.0;',
  6462. 'this.fn123456_0 = -123456.0;',
  6463. 'this.fn1234567_8 = -1234567.8;',
  6464. 'this.fn12345678_9 = -12345678.9;',
  6465. 'this.f1_0En12 = 1E-12;',
  6466. 'this.fn1_0En12 = -1E-12;',
  6467. 'this.maxdouble = 1.7e+308;',
  6468. 'this.mindouble = -1.7e+308;',
  6469. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6470. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6471. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6472. 'this.DZeroResolution = 1E-12;',
  6473. 'this.Minus1 = -1E-12;',
  6474. 'this.EPS = 1E-9;',
  6475. 'this.DELTA = 0.001;',
  6476. 'this.Big = 129.789E+100;',
  6477. 'this.Test0_15 = 0.15;',
  6478. 'this.Test999 = 2.9999999999999;',
  6479. 'this.Test111999 = 211199999999999000.0;',
  6480. 'this.TestMinus111999 = -211199999999999000.0;',
  6481. 'this.d = 4.4;'
  6482. ]),
  6483. LinesToStr([
  6484. '$mod.d = 1.0;',
  6485. '$mod.d = 1.0 / 3.0;',
  6486. '$mod.d = 1 / 3;',
  6487. '$mod.d = 5.0E-324;',
  6488. '$mod.d = 1.7E308;',
  6489. '$mod.d = 1.00E0;',
  6490. '$mod.d = 2.00E1;',
  6491. '$mod.d = 3.000E0;',
  6492. '$mod.d = -4.00E-0;',
  6493. '$mod.d = -5.00E-1;',
  6494. '$mod.d = Math.pow(10, 3);',
  6495. '$mod.d = 10 % 3;',
  6496. '$mod.d = Math.floor(10 / 3);',
  6497. '$mod.d = 1;',
  6498. '$mod.d = 0.1;',
  6499. '$mod.d = 0.3;',
  6500. '$mod.d = -0.1;',
  6501. '$mod.d = -0.3;',
  6502. '$mod.d = -0.003;',
  6503. '$mod.d = -0.123456789;',
  6504. '$mod.d = -300;',
  6505. '$mod.d = -123456;',
  6506. '$mod.d = -1234567.8;',
  6507. '$mod.d = -1.23456789E7;',
  6508. '$mod.d = 1E-12;',
  6509. '$mod.d = -1E-12;',
  6510. '$mod.d = 1.7E308;',
  6511. '$mod.d = -1.7E308;',
  6512. '$mod.d = -9007199254740991;',
  6513. '$mod.d = -9007199254740991;',
  6514. '$mod.d = -9.007199254740992E15;',
  6515. '$mod.d = -9.007199254740992E15;',
  6516. '$mod.d = 9007199254740991;',
  6517. '$mod.d = 0.0;',
  6518. '']));
  6519. end;
  6520. procedure TTestModule.TestInteger;
  6521. begin
  6522. StartProgram(false);
  6523. Add([
  6524. 'const',
  6525. ' MinInt = low(NativeInt);',
  6526. ' MaxInt = high(NativeInt);',
  6527. 'type',
  6528. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6529. 'const',
  6530. ' a = low(TMyInt)+High(TMyInt);',
  6531. 'var',
  6532. ' i: TMyInt;',
  6533. 'begin',
  6534. ' i:=-MinInt;',
  6535. ' i:=default(TMyInt);',
  6536. ' i:=low(i)+high(i);',
  6537. '']);
  6538. ConvertProgram;
  6539. CheckSource('TestIntegerRange',
  6540. LinesToStr([
  6541. 'this.MinInt = -9007199254740991;',
  6542. 'this.MaxInt = 9007199254740991;',
  6543. 'this.a = -9007199254740991 + 9007199254740991;',
  6544. 'this.i = 0;',
  6545. '']),
  6546. LinesToStr([
  6547. '$mod.i = - -9007199254740991;',
  6548. '$mod.i = -9007199254740991;',
  6549. '$mod.i = -9007199254740991 + 9007199254740991;',
  6550. '']));
  6551. end;
  6552. procedure TTestModule.TestIntegerRange;
  6553. begin
  6554. StartProgram(false);
  6555. Add([
  6556. 'const',
  6557. ' MinInt = -1;',
  6558. ' MaxInt = +1;',
  6559. 'type',
  6560. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6561. ' TInt2 = 1..3;',
  6562. 'const',
  6563. ' a = low(TMyInt)+High(TMyInt);',
  6564. ' b = low(TInt2)+High(TInt2);',
  6565. ' s1 = [1];',
  6566. ' s2 = [1,2];',
  6567. ' s3 = [1..3];',
  6568. ' s4 = [low(shortint)..high(shortint)];',
  6569. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6570. ' s6 = 1 in s2;',
  6571. 'var',
  6572. ' i: TMyInt;',
  6573. ' i2: TInt2;',
  6574. 'begin',
  6575. ' i:=i2;',
  6576. ' i:=default(TMyInt);',
  6577. ' if i=i2 then ;']);
  6578. ConvertProgram;
  6579. CheckSource('TestIntegerRange',
  6580. LinesToStr([
  6581. 'this.MinInt = -1;',
  6582. 'this.MaxInt = +1;',
  6583. 'this.a = -1 + 1;',
  6584. 'this.b = 1 + 3;',
  6585. 'this.s1 = rtl.createSet(1);',
  6586. 'this.s2 = rtl.createSet(1, 2);',
  6587. 'this.s3 = rtl.createSet(null, 1, 3);',
  6588. 'this.s4 = rtl.createSet(null, -128, 127);',
  6589. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6590. 'this.s6 = 1 in $mod.s2;',
  6591. 'this.i = 0;',
  6592. 'this.i2 = 0;',
  6593. '']),
  6594. LinesToStr([
  6595. '$mod.i = $mod.i2;',
  6596. '$mod.i = -1;',
  6597. 'if ($mod.i === $mod.i2) ;',
  6598. '']));
  6599. end;
  6600. procedure TTestModule.TestIntegerTypecasts;
  6601. begin
  6602. StartProgram(false);
  6603. Add([
  6604. 'var',
  6605. ' i: nativeint;',
  6606. ' b: byte;',
  6607. ' sh: shortint;',
  6608. ' w: word;',
  6609. ' sm: smallint;',
  6610. ' lw: longword;',
  6611. ' li: longint;',
  6612. 'begin',
  6613. ' b:=byte(i);',
  6614. ' sh:=shortint(i);',
  6615. ' w:=word(i);',
  6616. ' sm:=smallint(i);',
  6617. ' lw:=longword(i);',
  6618. ' li:=longint(i);',
  6619. '']);
  6620. ConvertProgram;
  6621. CheckSource('TestIntegerTypecasts',
  6622. LinesToStr([
  6623. 'this.i = 0;',
  6624. 'this.b = 0;',
  6625. 'this.sh = 0;',
  6626. 'this.w = 0;',
  6627. 'this.sm = 0;',
  6628. 'this.lw = 0;',
  6629. 'this.li = 0;',
  6630. '']),
  6631. LinesToStr([
  6632. '$mod.b = $mod.i & 255;',
  6633. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6634. '$mod.w = $mod.i & 65535;',
  6635. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6636. '$mod.lw = $mod.i >>> 0;',
  6637. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6638. '']));
  6639. end;
  6640. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6641. begin
  6642. StartProgram(false);
  6643. Add([
  6644. 'var',
  6645. ' i,j: nativeint;',
  6646. 'begin',
  6647. ' i:=i shr 0;',
  6648. ' i:=i shr 1;',
  6649. ' i:=i shr 3;',
  6650. ' i:=i shr 54;',
  6651. ' i:=j shr i;',
  6652. '']);
  6653. ConvertProgram;
  6654. CheckResolverUnexpectedHints;
  6655. CheckSource('TestInteger_BitwiseShrNativeInt',
  6656. LinesToStr([
  6657. 'this.i = 0;',
  6658. 'this.j = 0;',
  6659. '']),
  6660. LinesToStr([
  6661. '$mod.i = $mod.i;',
  6662. '$mod.i = Math.floor($mod.i / 2);',
  6663. '$mod.i = Math.floor($mod.i / 8);',
  6664. '$mod.i = 0;',
  6665. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6666. '']));
  6667. end;
  6668. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6669. begin
  6670. StartProgram(false);
  6671. Add([
  6672. 'var',
  6673. ' i: nativeint;',
  6674. 'begin',
  6675. ' i:=i shl 0;',
  6676. ' i:=i shl 54;',
  6677. ' i:=123456789012 shl 1;',
  6678. ' i:=i shl 1;',
  6679. '']);
  6680. ConvertProgram;
  6681. CheckResolverUnexpectedHints;
  6682. CheckSource('TestInteger_BitwiseShrNativeInt',
  6683. LinesToStr([
  6684. 'this.i = 0;',
  6685. '']),
  6686. LinesToStr([
  6687. '$mod.i = $mod.i;',
  6688. '$mod.i = 0;',
  6689. '$mod.i = 246913578024;',
  6690. '$mod.i = rtl.shl($mod.i, 1);',
  6691. '']));
  6692. end;
  6693. procedure TTestModule.TestInteger_SystemFunc;
  6694. begin
  6695. StartProgram(true);
  6696. Add([
  6697. 'var',
  6698. ' i: byte;',
  6699. ' s: string;',
  6700. 'begin',
  6701. ' system.inc(i);',
  6702. ' system.str(i,s);',
  6703. ' s:=system.str(i);',
  6704. ' i:=system.low(i);',
  6705. ' i:=system.high(i);',
  6706. ' i:=system.pred(i);',
  6707. ' i:=system.succ(i);',
  6708. '']);
  6709. ConvertProgram;
  6710. CheckResolverUnexpectedHints;
  6711. CheckSource('TestInteger_SystemFunc',
  6712. LinesToStr([
  6713. 'this.i = 0;',
  6714. 'this.s = "";',
  6715. '']),
  6716. LinesToStr([
  6717. '$mod.i += 1;',
  6718. '$mod.s = "" + $mod.i;',
  6719. '$mod.s = "" + $mod.i;',
  6720. '$mod.i = 0;',
  6721. '$mod.i = 255;',
  6722. '$mod.i = $mod.i - 1;',
  6723. '$mod.i = $mod.i + 1;',
  6724. '']));
  6725. end;
  6726. procedure TTestModule.TestCurrency;
  6727. begin
  6728. StartProgram(false);
  6729. Add([
  6730. 'type',
  6731. ' TCoin = currency;',
  6732. 'const',
  6733. ' a = TCoin(2.7);',
  6734. ' b = a + TCoin(1.7);',
  6735. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6736. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6737. 'var',
  6738. ' c: TCoin = b;',
  6739. ' i: nativeint;',
  6740. ' d: double;',
  6741. ' j: jsvalue;',
  6742. 'function DoIt(c: currency): currency; begin end;',
  6743. 'function GetIt(d: double): double; begin end;',
  6744. 'procedure Write(v: jsvalue); begin end;',
  6745. 'begin',
  6746. ' c:=1.0;',
  6747. ' c:=0.1;',
  6748. ' c:=1.0/3.0;',
  6749. ' c:=1/3;',
  6750. ' c:=a;',
  6751. ' d:=c;',
  6752. ' c:=d;',
  6753. ' c:=currency(c);',
  6754. ' c:=currency(d);',
  6755. ' d:=double(c);',
  6756. ' c:=i;',
  6757. ' c:=currency(i);',
  6758. //' i:=c;', not allowed
  6759. ' i:=nativeint(c);',
  6760. ' c:=c+a;',
  6761. ' c:=-c-a;',
  6762. ' c:=d+c;',
  6763. ' c:=c+d;',
  6764. ' c:=d-c;',
  6765. ' c:=c-d;',
  6766. ' c:=c*a;',
  6767. ' c:=a*c;',
  6768. ' c:=d*c;',
  6769. ' c:=c*d;',
  6770. ' c:=c/a;',
  6771. ' c:=a/c;',
  6772. ' c:=d/c;',
  6773. ' c:=c/d;',
  6774. ' c:=c**a;',
  6775. ' c:=a**c;',
  6776. ' c:=d**c;',
  6777. ' c:=c**d;',
  6778. ' if c=c then ;',
  6779. ' if c=a then ;',
  6780. ' if a=c then ;',
  6781. ' if d=c then ;',
  6782. ' if c=d then ;',
  6783. ' c:=DoIt(c);',
  6784. ' c:=DoIt(i);',
  6785. ' c:=DoIt(d);',
  6786. ' c:=GetIt(c);',
  6787. ' j:=c;',
  6788. ' Write(c);',
  6789. ' c:=default(currency);',
  6790. ' j:=str(c);',
  6791. ' j:=str(c:0:3);',
  6792. '']);
  6793. ConvertProgram;
  6794. CheckSource('TestCurrency',
  6795. LinesToStr([
  6796. 'this.a = 27000;',
  6797. 'this.b = $mod.a + 17000;',
  6798. 'this.MinSafeIntCurrency = -92233720368.5477;',
  6799. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  6800. 'this.c = $mod.b;',
  6801. 'this.i = 0;',
  6802. 'this.d = 0.0;',
  6803. 'this.j = undefined;',
  6804. 'this.DoIt = function (c) {',
  6805. ' var Result = 0;',
  6806. ' return Result;',
  6807. '};',
  6808. 'this.GetIt = function (d) {',
  6809. ' var Result = 0.0;',
  6810. ' return Result;',
  6811. '};',
  6812. 'this.Write = function (v) {',
  6813. '};',
  6814. '']),
  6815. LinesToStr([
  6816. '$mod.c = 10000;',
  6817. '$mod.c = 1000;',
  6818. '$mod.c = Math.floor((1.0 / 3.0) * 10000);',
  6819. '$mod.c = Math.floor((1 / 3) * 10000);',
  6820. '$mod.c = $mod.a;',
  6821. '$mod.d = $mod.c / 10000;',
  6822. '$mod.c = Math.floor($mod.d * 10000);',
  6823. '$mod.c = $mod.c;',
  6824. '$mod.c = $mod.d * 10000;',
  6825. '$mod.d = $mod.c / 10000;',
  6826. '$mod.c = $mod.i * 10000;',
  6827. '$mod.c = $mod.i * 10000;',
  6828. '$mod.i = Math.floor($mod.c / 10000);',
  6829. '$mod.c = $mod.c + $mod.a;',
  6830. '$mod.c = -$mod.c - $mod.a;',
  6831. '$mod.c = ($mod.d * 10000) + $mod.c;',
  6832. '$mod.c = $mod.c + ($mod.d * 10000);',
  6833. '$mod.c = ($mod.d * 10000) - $mod.c;',
  6834. '$mod.c = $mod.c - ($mod.d * 10000);',
  6835. '$mod.c = ($mod.c * $mod.a) / 10000;',
  6836. '$mod.c = ($mod.a * $mod.c) / 10000;',
  6837. '$mod.c = $mod.d * $mod.c;',
  6838. '$mod.c = $mod.c * $mod.d;',
  6839. '$mod.c = Math.floor(($mod.c / $mod.a) * 10000);',
  6840. '$mod.c = Math.floor(($mod.a / $mod.c) * 10000);',
  6841. '$mod.c = Math.floor($mod.d / $mod.c);',
  6842. '$mod.c = Math.floor($mod.c / $mod.d);',
  6843. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  6844. '$mod.c = Math.floor(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  6845. '$mod.c = Math.floor(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  6846. '$mod.c = Math.floor(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  6847. 'if ($mod.c === $mod.c) ;',
  6848. 'if ($mod.c === $mod.a) ;',
  6849. 'if ($mod.a === $mod.c) ;',
  6850. 'if (($mod.d * 10000) === $mod.c) ;',
  6851. 'if ($mod.c === ($mod.d * 10000)) ;',
  6852. '$mod.c = $mod.DoIt($mod.c);',
  6853. '$mod.c = $mod.DoIt($mod.i * 10000);',
  6854. '$mod.c = $mod.DoIt($mod.d * 10000);',
  6855. '$mod.c = Math.floor($mod.GetIt($mod.c / 10000) * 10000);',
  6856. '$mod.j = $mod.c / 10000;',
  6857. '$mod.Write($mod.c / 10000);',
  6858. '$mod.c = 0;',
  6859. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  6860. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  6861. '']));
  6862. end;
  6863. procedure TTestModule.TestForBoolDo;
  6864. begin
  6865. StartProgram(false);
  6866. Add([
  6867. 'var b: boolean;',
  6868. 'begin',
  6869. ' for b:=false to true do ;',
  6870. ' for b:=b downto false do ;',
  6871. ' for b in boolean do ;',
  6872. '']);
  6873. ConvertProgram;
  6874. CheckSource('TestForBoolDo',
  6875. LinesToStr([ // statements
  6876. 'this.b = false;']),
  6877. LinesToStr([ // this.$main
  6878. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  6879. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  6880. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  6881. '']));
  6882. end;
  6883. procedure TTestModule.TestForIntDo;
  6884. begin
  6885. StartProgram(false);
  6886. Add([
  6887. 'var i: longint;',
  6888. 'begin',
  6889. ' for i:=3 to 5 do ;',
  6890. ' for i:=i downto 2 do ;',
  6891. ' for i in byte do ;',
  6892. '']);
  6893. ConvertProgram;
  6894. CheckSource('TestForIntDo',
  6895. LinesToStr([ // statements
  6896. 'this.i = 0;']),
  6897. LinesToStr([ // this.$main
  6898. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  6899. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  6900. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  6901. '']));
  6902. end;
  6903. procedure TTestModule.TestForIntInDo;
  6904. begin
  6905. StartProgram(false);
  6906. Add([
  6907. 'type',
  6908. ' TSetOfInt = set of byte;',
  6909. ' TIntRg = 3..7;',
  6910. ' TSetOfIntRg = set of TIntRg;',
  6911. 'var',
  6912. ' i,i2: longint;',
  6913. ' a1: array of byte;',
  6914. ' a2: array[1..3] of byte;',
  6915. ' soi: TSetOfInt;',
  6916. ' soir: TSetOfIntRg;',
  6917. ' ir: TIntRg;',
  6918. 'begin',
  6919. ' for i in byte do ;',
  6920. ' for i in a1 do ;',
  6921. ' for i in a2 do ;',
  6922. ' for i in [11..13] do ;',
  6923. ' for i in TSetOfInt do ;',
  6924. ' for i in TIntRg do ;',
  6925. ' for i in soi do i2:=i;',
  6926. ' for i in TSetOfIntRg do ;',
  6927. ' for i in soir do ;',
  6928. ' for ir in TIntRg do ;',
  6929. ' for ir in TSetOfIntRg do ;',
  6930. ' for ir in soir do ;',
  6931. '']);
  6932. ConvertProgram;
  6933. CheckSource('TestForIntInDo',
  6934. LinesToStr([ // statements
  6935. 'this.i = 0;',
  6936. 'this.i2 = 0;',
  6937. 'this.a1 = [];',
  6938. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  6939. 'this.soi = {};',
  6940. 'this.soir = {};',
  6941. 'this.ir = 0;',
  6942. '']),
  6943. LinesToStr([ // this.$main
  6944. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  6945. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  6946. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  6947. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  6948. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  6949. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  6950. 'for (var $l6 in $mod.soi) {',
  6951. ' $mod.i = +$l6;',
  6952. ' $mod.i2 = $mod.i;',
  6953. '};',
  6954. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  6955. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  6956. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  6957. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  6958. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  6959. '']));
  6960. end;
  6961. procedure TTestModule.TestCharConst;
  6962. begin
  6963. StartProgram(false);
  6964. Add([
  6965. 'const',
  6966. ' a = #$00F3;',
  6967. ' c: char = ''1'';',
  6968. 'begin',
  6969. ' c:=#0;',
  6970. ' c:=#1;',
  6971. ' c:=#9;',
  6972. ' c:=#10;',
  6973. ' c:=#13;',
  6974. ' c:=#31;',
  6975. ' c:=#32;',
  6976. ' c:=#$A;',
  6977. ' c:=#$0A;',
  6978. ' c:=#$b;',
  6979. ' c:=#$0b;',
  6980. ' c:=^A;',
  6981. ' c:=''"'';',
  6982. ' c:=default(char);',
  6983. ' c:=#$00E4;', // ä
  6984. ' c:=''ä'';',
  6985. ' c:=#$E4;', // ä
  6986. ' c:=#$D800;', // invalid UTF-16
  6987. ' c:=#$DFFF;', // invalid UTF-16
  6988. ' c:=#$FFFF;', // last UCS-2
  6989. ' c:=high(c);', // last UCS-2
  6990. '']);
  6991. ConvertProgram;
  6992. CheckSource('TestCharConst',
  6993. LinesToStr([
  6994. 'this.a="ó";',
  6995. 'this.c="1";'
  6996. ]),
  6997. LinesToStr([
  6998. '$mod.c="\x00";',
  6999. '$mod.c="\x01";',
  7000. '$mod.c="\t";',
  7001. '$mod.c="\n";',
  7002. '$mod.c="\r";',
  7003. '$mod.c="\x1F";',
  7004. '$mod.c=" ";',
  7005. '$mod.c="\n";',
  7006. '$mod.c="\n";',
  7007. '$mod.c="\x0B";',
  7008. '$mod.c="\x0B";',
  7009. '$mod.c="\x01";',
  7010. '$mod.c=''"'';',
  7011. '$mod.c="\x00";',
  7012. '$mod.c = "ä";',
  7013. '$mod.c = "ä";',
  7014. '$mod.c = "ä";',
  7015. '$mod.c="\uD800";',
  7016. '$mod.c="\uDFFF";',
  7017. '$mod.c="\uFFFF";',
  7018. '$mod.c="\uFFFF";',
  7019. '']));
  7020. end;
  7021. procedure TTestModule.TestChar_Compare;
  7022. begin
  7023. StartProgram(false);
  7024. Add('var');
  7025. Add(' c: char;');
  7026. Add(' b: boolean;');
  7027. Add('begin');
  7028. Add(' b:=c=''1'';');
  7029. Add(' b:=''2''=c;');
  7030. Add(' b:=''3''=''4'';');
  7031. Add(' b:=c<>''5'';');
  7032. Add(' b:=''6''<>c;');
  7033. Add(' b:=c>''7'';');
  7034. Add(' b:=''8''>c;');
  7035. Add(' b:=c>=''9'';');
  7036. Add(' b:=''A''>=c;');
  7037. Add(' b:=c<''B'';');
  7038. Add(' b:=''C''<c;');
  7039. Add(' b:=c<=''D'';');
  7040. Add(' b:=''E''<=c;');
  7041. ConvertProgram;
  7042. CheckSource('TestChar_Compare',
  7043. LinesToStr([
  7044. 'this.c="";',
  7045. 'this.b = false;'
  7046. ]),
  7047. LinesToStr([
  7048. '$mod.b = $mod.c === "1";',
  7049. '$mod.b = "2" === $mod.c;',
  7050. '$mod.b = "3" === "4";',
  7051. '$mod.b = $mod.c !== "5";',
  7052. '$mod.b = "6" !== $mod.c;',
  7053. '$mod.b = $mod.c > "7";',
  7054. '$mod.b = "8" > $mod.c;',
  7055. '$mod.b = $mod.c >= "9";',
  7056. '$mod.b = "A" >= $mod.c;',
  7057. '$mod.b = $mod.c < "B";',
  7058. '$mod.b = "C" < $mod.c;',
  7059. '$mod.b = $mod.c <= "D";',
  7060. '$mod.b = "E" <= $mod.c;',
  7061. '']));
  7062. end;
  7063. procedure TTestModule.TestChar_BuiltInProcs;
  7064. begin
  7065. StartProgram(false);
  7066. Add([
  7067. 'var',
  7068. ' c: char;',
  7069. ' i: longint;',
  7070. ' s: string;',
  7071. 'begin',
  7072. ' i:=ord(c);',
  7073. ' i:=ord(s[i]);',
  7074. ' c:=chr(i);',
  7075. ' c:=pred(c);',
  7076. ' c:=succ(c);',
  7077. ' c:=low(c);',
  7078. ' c:=high(c);',
  7079. ' i:=byte(c);',
  7080. ' i:=word(c);',
  7081. ' i:=longint(c);',
  7082. '']);
  7083. ConvertProgram;
  7084. CheckSource('TestChar_BuiltInProcs',
  7085. LinesToStr([
  7086. 'this.c = "";',
  7087. 'this.i = 0;',
  7088. 'this.s = "";'
  7089. ]),
  7090. LinesToStr([
  7091. '$mod.i = $mod.c.charCodeAt();',
  7092. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7093. '$mod.c = String.fromCharCode($mod.i);',
  7094. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7095. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7096. '$mod.c = "\x00";',
  7097. '$mod.c = "\uFFFF";',
  7098. '$mod.i = $mod.c.charCodeAt() & 255;',
  7099. '$mod.i = $mod.c.charCodeAt();',
  7100. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7101. '']));
  7102. end;
  7103. procedure TTestModule.TestStringConst;
  7104. begin
  7105. StartProgram(false);
  7106. Add([
  7107. '{$H+}',
  7108. 'const',
  7109. ' a = #$00F3#$017C;', // first <256, then >=256
  7110. ' b = string(''a'');',
  7111. ' c = string(''ä'');',
  7112. ' d = UnicodeString(''b'');',
  7113. ' e = UnicodeString(''ö'');',
  7114. 'var',
  7115. ' s: string = ''abc'';',
  7116. 'begin',
  7117. ' s:='''';',
  7118. ' s:=#13#10;',
  7119. ' s:=#9''foo'';',
  7120. ' s:=#$A9;',
  7121. ' s:=''foo''#13''bar'';',
  7122. ' s:=''"'';',
  7123. ' s:=''"''''"'';',
  7124. ' s:=#$20AC;', // euro
  7125. ' s:=#$10437;', // outside BMP
  7126. ' s:=default(string);',
  7127. ' s:=concat(s);',
  7128. ' s:=concat(s,''a'',s)',
  7129. '']);
  7130. ConvertProgram;
  7131. CheckSource('TestStringConst',
  7132. LinesToStr([
  7133. 'this.a = "óż";',
  7134. 'this.b = "a";',
  7135. 'this.c = "ä";',
  7136. 'this.d = "b";',
  7137. 'this.e = "ö";',
  7138. 'this.s="abc";',
  7139. '']),
  7140. LinesToStr([
  7141. '$mod.s="";',
  7142. '$mod.s="\r\n";',
  7143. '$mod.s="\tfoo";',
  7144. '$mod.s="©";',
  7145. '$mod.s="foo\rbar";',
  7146. '$mod.s=''"'';',
  7147. '$mod.s=''"\''"'';',
  7148. '$mod.s="€";',
  7149. '$mod.s="'#$F0#$90#$90#$B7'";',
  7150. '$mod.s="";',
  7151. '$mod.s = $mod.s;',
  7152. '$mod.s = $mod.s.concat("a", $mod.s);',
  7153. '']));
  7154. end;
  7155. procedure TTestModule.TestStringConstSurrogate;
  7156. begin
  7157. StartProgram(false);
  7158. Add([
  7159. 'var',
  7160. ' s: string;',
  7161. 'begin',
  7162. ' s:=''😊'';', // 1F60A
  7163. '']);
  7164. ConvertProgram;
  7165. CheckSource('TestStringConstSurrogate',
  7166. LinesToStr([
  7167. 'this.s="";'
  7168. ]),
  7169. LinesToStr([
  7170. '$mod.s="😊";'
  7171. ]));
  7172. end;
  7173. procedure TTestModule.TestString_Length;
  7174. begin
  7175. StartProgram(false);
  7176. Add('const c = ''foo'';');
  7177. Add('var');
  7178. Add(' s: string;');
  7179. Add(' i: longint;');
  7180. Add('begin');
  7181. Add(' i:=length(s);');
  7182. Add(' i:=length(s+s);');
  7183. Add(' i:=length(''abc'');');
  7184. Add(' i:=length(c);');
  7185. ConvertProgram;
  7186. CheckSource('TestString_Length',
  7187. LinesToStr([
  7188. 'this.c = "foo";',
  7189. 'this.s = "";',
  7190. 'this.i = 0;',
  7191. '']),
  7192. LinesToStr([
  7193. '$mod.i = $mod.s.length;',
  7194. '$mod.i = ($mod.s+$mod.s).length;',
  7195. '$mod.i = "abc".length;',
  7196. '$mod.i = $mod.c.length;',
  7197. '']));
  7198. end;
  7199. procedure TTestModule.TestString_Compare;
  7200. begin
  7201. StartProgram(false);
  7202. Add('var');
  7203. Add(' s, t: string;');
  7204. Add(' b: boolean;');
  7205. Add('begin');
  7206. Add(' b:=s=t;');
  7207. Add(' b:=s<>t;');
  7208. Add(' b:=s>t;');
  7209. Add(' b:=s>=t;');
  7210. Add(' b:=s<t;');
  7211. Add(' b:=s<=t;');
  7212. ConvertProgram;
  7213. CheckSource('TestString_Compare',
  7214. LinesToStr([ // statements
  7215. 'this.s = "";',
  7216. 'this.t = "";',
  7217. 'this.b =false;'
  7218. ]),
  7219. LinesToStr([ // this.$main
  7220. '$mod.b = $mod.s === $mod.t;',
  7221. '$mod.b = $mod.s !== $mod.t;',
  7222. '$mod.b = $mod.s > $mod.t;',
  7223. '$mod.b = $mod.s >= $mod.t;',
  7224. '$mod.b = $mod.s < $mod.t;',
  7225. '$mod.b = $mod.s <= $mod.t;',
  7226. '']));
  7227. end;
  7228. procedure TTestModule.TestString_SetLength;
  7229. begin
  7230. StartProgram(false);
  7231. Add([
  7232. 'procedure DoIt(var s: string);',
  7233. 'begin',
  7234. ' SetLength(s,2);',
  7235. 'end;',
  7236. 'var s: string;',
  7237. 'begin',
  7238. ' SetLength(s,3);',
  7239. '']);
  7240. ConvertProgram;
  7241. CheckSource('TestString_SetLength',
  7242. LinesToStr([ // statements
  7243. 'this.DoIt = function (s) {',
  7244. ' s.set(rtl.strSetLength(s.get(), 2));',
  7245. '};',
  7246. 'this.s = "";',
  7247. '']),
  7248. LinesToStr([ // this.$main
  7249. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7250. ]));
  7251. end;
  7252. procedure TTestModule.TestString_CharAt;
  7253. begin
  7254. StartProgram(false);
  7255. Add([
  7256. 'var',
  7257. ' s: string;',
  7258. ' c: char;',
  7259. ' b: boolean;',
  7260. 'begin',
  7261. ' b:= s[1] = c;',
  7262. ' b:= c = s[1];',
  7263. ' b:= c <> s[1];',
  7264. ' b:= c > s[1];',
  7265. ' b:= c >= s[1];',
  7266. ' b:= c < s[2];',
  7267. ' b:= c <= s[1];',
  7268. ' s[1] := c;',
  7269. ' s[2+3] := c;']);
  7270. ConvertProgram;
  7271. CheckSource('TestString_CharAt',
  7272. LinesToStr([ // statements
  7273. 'this.s = "";',
  7274. 'this.c = "";',
  7275. 'this.b = false;'
  7276. ]),
  7277. LinesToStr([ // this.$main
  7278. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7279. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7280. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7281. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7282. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7283. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7284. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7285. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7286. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7287. '']));
  7288. end;
  7289. procedure TTestModule.TestStringHMinusFail;
  7290. begin
  7291. StartProgram(false);
  7292. Add([
  7293. '{$H-}',
  7294. 'var s: string;',
  7295. 'begin']);
  7296. ConvertProgram;
  7297. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7298. end;
  7299. procedure TTestModule.TestStr;
  7300. begin
  7301. StartProgram(false);
  7302. Add('var');
  7303. Add(' b: boolean;');
  7304. Add(' i: longint;');
  7305. Add(' d: double;');
  7306. Add(' s: string;');
  7307. Add('begin');
  7308. Add(' str(b,s);');
  7309. Add(' str(i,s);');
  7310. Add(' str(d,s);');
  7311. Add(' str(i:3,s);');
  7312. Add(' str(d:3:2,s);');
  7313. Add(' Str(12.456:12:1,s);');
  7314. Add(' Str(12.456:12,s);');
  7315. Add(' s:=str(b);');
  7316. Add(' s:=str(i);');
  7317. Add(' s:=str(d);');
  7318. Add(' s:=str(i,i);');
  7319. Add(' s:=str(i:3);');
  7320. Add(' s:=str(d:3:2);');
  7321. Add(' s:=str(i:4,i);');
  7322. Add(' s:=str(i,i:5);');
  7323. Add(' s:=str(i:4,i:5);');
  7324. Add(' s:=str(s,s);');
  7325. Add(' s:=str(s,''foo'');');
  7326. ConvertProgram;
  7327. CheckSource('TestStr',
  7328. LinesToStr([ // statements
  7329. 'this.b = false;',
  7330. 'this.i = 0;',
  7331. 'this.d = 0.0;',
  7332. 'this.s = "";',
  7333. '']),
  7334. LinesToStr([ // this.$main
  7335. '$mod.s = ""+$mod.b;',
  7336. '$mod.s = ""+$mod.i;',
  7337. '$mod.s = rtl.floatToStr($mod.d);',
  7338. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7339. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7340. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7341. '$mod.s = rtl.floatToStr(12.456,12);',
  7342. '$mod.s = ""+$mod.b;',
  7343. '$mod.s = ""+$mod.i;',
  7344. '$mod.s = rtl.floatToStr($mod.d);',
  7345. '$mod.s = ""+$mod.i+$mod.i;',
  7346. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7347. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7348. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7349. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7350. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7351. '$mod.s = $mod.s + $mod.s;',
  7352. '$mod.s = $mod.s + "foo";',
  7353. '']));
  7354. end;
  7355. procedure TTestModule.TestBaseType_AnsiStringFail;
  7356. begin
  7357. StartProgram(false);
  7358. Add('var s: AnsiString');
  7359. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7360. ConvertProgram;
  7361. end;
  7362. procedure TTestModule.TestBaseType_WideStringFail;
  7363. begin
  7364. StartProgram(false);
  7365. Add('var s: WideString');
  7366. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7367. ConvertProgram;
  7368. end;
  7369. procedure TTestModule.TestBaseType_ShortStringFail;
  7370. begin
  7371. StartProgram(false);
  7372. Add('var s: ShortString');
  7373. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7374. ConvertProgram;
  7375. end;
  7376. procedure TTestModule.TestBaseType_RawByteStringFail;
  7377. begin
  7378. StartProgram(false);
  7379. Add('var s: RawByteString');
  7380. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7381. ConvertProgram;
  7382. end;
  7383. procedure TTestModule.TestTypeShortstring_Fail;
  7384. begin
  7385. StartProgram(false);
  7386. Add('type t = string[12];');
  7387. Add('var s: t;');
  7388. Add('begin');
  7389. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7390. ConvertProgram;
  7391. end;
  7392. procedure TTestModule.TestCharSet_Custom;
  7393. begin
  7394. StartProgram(false);
  7395. Add([
  7396. 'type',
  7397. ' TCharRg = ''a''..''z'';',
  7398. ' TSetOfCharRg = set of TCharRg;',
  7399. ' TCharRg2 = ''m''..''p'';',
  7400. 'const',
  7401. ' crg: TCharRg = ''b'';',
  7402. 'var',
  7403. ' c: char;',
  7404. ' crg2: TCharRg2;',
  7405. ' s: TSetOfCharRg;',
  7406. 'begin',
  7407. ' c:=crg;',
  7408. ' crg:=c;',
  7409. ' crg2:=crg;',
  7410. ' if c=crg then ;',
  7411. ' if crg=c then ;',
  7412. ' if crg=crg2 then ;',
  7413. ' if c in s then ;',
  7414. ' if crg2 in s then ;',
  7415. ' c:=default(TCharRg);',
  7416. '']);
  7417. ConvertProgram;
  7418. CheckSource('TestCharSet_Custom',
  7419. LinesToStr([ // statements
  7420. 'this.crg = "b";',
  7421. 'this.c = "";',
  7422. 'this.crg2 = "m";',
  7423. 'this.s = {};',
  7424. '']),
  7425. LinesToStr([ // this.$main
  7426. '$mod.c = $mod.crg;',
  7427. '$mod.crg = $mod.c;',
  7428. '$mod.crg2 = $mod.crg;',
  7429. 'if ($mod.c === $mod.crg) ;',
  7430. 'if ($mod.crg === $mod.c) ;',
  7431. 'if ($mod.crg === $mod.crg2) ;',
  7432. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7433. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7434. '$mod.c = "a";',
  7435. '']));
  7436. end;
  7437. procedure TTestModule.TestForCharDo;
  7438. begin
  7439. StartProgram(false);
  7440. Add([
  7441. 'var c: char;',
  7442. 'begin',
  7443. ' for c:=''a'' to ''c'' do ;',
  7444. ' for c:=c downto ''a'' do ;',
  7445. ' for c:=''Б'' to ''Я'' do ;',
  7446. '']);
  7447. ConvertProgram;
  7448. CheckSource('TestForCharDo',
  7449. LinesToStr([ // statements
  7450. 'this.c = "";']),
  7451. LinesToStr([ // this.$main
  7452. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7453. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7454. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7455. '']));
  7456. end;
  7457. procedure TTestModule.TestForCharInDo;
  7458. begin
  7459. StartProgram(false);
  7460. Add([
  7461. 'type',
  7462. ' TSetOfChar = set of char;',
  7463. ' TCharRg = ''a''..''z'';',
  7464. ' TSetOfCharRg = set of TCharRg;',
  7465. 'const Foo = ''foo'';',
  7466. 'var',
  7467. ' c,c2: char;',
  7468. ' s: string;',
  7469. ' a1: array of char;',
  7470. ' a2: array[1..3] of char;',
  7471. ' soc: TSetOfChar;',
  7472. ' socr: TSetOfCharRg;',
  7473. ' cr: TCharRg;',
  7474. 'begin',
  7475. ' for c in foo do ;',
  7476. ' for c in s do ;',
  7477. ' for c in char do ;',
  7478. ' for c in a1 do ;',
  7479. ' for c in a2 do ;',
  7480. ' for c in [''1''..''3''] do ;',
  7481. ' for c in TSetOfChar do ;',
  7482. ' for c in TCharRg do ;',
  7483. ' for c in soc do c2:=c;',
  7484. ' for c in TSetOfCharRg do ;',
  7485. ' for c in socr do ;',
  7486. ' for cr in TCharRg do ;',
  7487. ' for cr in TSetOfCharRg do ;',
  7488. ' for cr in socr do ;',
  7489. '']);
  7490. ConvertProgram;
  7491. CheckSource('TestForCharInDo',
  7492. LinesToStr([ // statements
  7493. 'this.Foo = "foo";',
  7494. 'this.c = "";',
  7495. 'this.c2 = "";',
  7496. 'this.s = "";',
  7497. 'this.a1 = [];',
  7498. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7499. 'this.soc = {};',
  7500. 'this.socr = {};',
  7501. 'this.cr = "a";',
  7502. '']),
  7503. LinesToStr([ // this.$main
  7504. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7505. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7506. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7507. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7508. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7509. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7510. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7511. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7512. 'for (var $l8 in $mod.soc) {',
  7513. ' $mod.c = String.fromCharCode($l8);',
  7514. ' $mod.c2 = $mod.c;',
  7515. '};',
  7516. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  7517. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  7518. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  7519. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  7520. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  7521. '']));
  7522. end;
  7523. procedure TTestModule.TestProcTwoArgs;
  7524. begin
  7525. StartProgram(false);
  7526. Add('procedure Test(a,b: longint);');
  7527. Add('begin');
  7528. Add('end;');
  7529. Add('begin');
  7530. ConvertProgram;
  7531. CheckSource('TestProcTwoArgs',
  7532. LinesToStr([ // statements
  7533. 'this.Test = function (a,b) {',
  7534. '};'
  7535. ]),
  7536. LinesToStr([ // this.$main
  7537. ''
  7538. ]));
  7539. end;
  7540. procedure TTestModule.TestProc_DefaultValue;
  7541. begin
  7542. StartProgram(false);
  7543. Add('procedure p1(i: longint = 1);');
  7544. Add('begin');
  7545. Add('end;');
  7546. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7547. Add('begin');
  7548. Add('end;');
  7549. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7550. Add('begin');
  7551. Add('end;');
  7552. Add('begin');
  7553. Add(' p1;');
  7554. Add(' p1();');
  7555. Add(' p1(11);');
  7556. Add(' p2;');
  7557. Add(' p2();');
  7558. Add(' p2(12);');
  7559. Add(' p2(13,''b'');');
  7560. Add(' p3();');
  7561. ConvertProgram;
  7562. CheckSource('TestProc_DefaultValue',
  7563. LinesToStr([ // statements
  7564. 'this.p1 = function (i) {',
  7565. '};',
  7566. 'this.p2 = function (i,c) {',
  7567. '};',
  7568. 'this.p3 = function (d,b,s) {',
  7569. '};'
  7570. ]),
  7571. LinesToStr([ // this.$main
  7572. ' $mod.p1(1);',
  7573. ' $mod.p1(1);',
  7574. ' $mod.p1(11);',
  7575. ' $mod.p2(1,"a");',
  7576. ' $mod.p2(1,"a");',
  7577. ' $mod.p2(12,"a");',
  7578. ' $mod.p2(13,"b");',
  7579. ' $mod.p3(1.0,false,"abc");'
  7580. ]));
  7581. end;
  7582. procedure TTestModule.TestFunctionInt;
  7583. begin
  7584. StartProgram(false);
  7585. Add('function MyTest(Bar: longint): longint;');
  7586. Add('begin');
  7587. Add(' Result:=2*bar');
  7588. Add('end;');
  7589. Add('begin');
  7590. ConvertProgram;
  7591. CheckSource('TestFunctionInt',
  7592. LinesToStr([ // statements
  7593. 'this.MyTest = function (Bar) {',
  7594. ' var Result = 0;',
  7595. ' Result = 2*Bar;',
  7596. ' return Result;',
  7597. '};'
  7598. ]),
  7599. LinesToStr([ // this.$main
  7600. ''
  7601. ]));
  7602. end;
  7603. procedure TTestModule.TestFunctionString;
  7604. begin
  7605. StartProgram(false);
  7606. Add('function Test(Bar: string): string;');
  7607. Add('begin');
  7608. Add(' Result:=bar+BAR');
  7609. Add('end;');
  7610. Add('begin');
  7611. ConvertProgram;
  7612. CheckSource('TestFunctionString',
  7613. LinesToStr([ // statements
  7614. 'this.Test = function (Bar) {',
  7615. ' var Result = "";',
  7616. ' Result = Bar+Bar;',
  7617. ' return Result;',
  7618. '};'
  7619. ]),
  7620. LinesToStr([ // this.$main
  7621. ''
  7622. ]));
  7623. end;
  7624. procedure TTestModule.TestIfThen;
  7625. begin
  7626. StartProgram(false);
  7627. Add([
  7628. 'var b: boolean;',
  7629. 'begin',
  7630. ' if b then ;',
  7631. ' if b then else ;']);
  7632. ConvertProgram;
  7633. CheckSource('TestIfThen',
  7634. LinesToStr([ // statements
  7635. 'this.b = false;',
  7636. '']),
  7637. LinesToStr([ // this.$main
  7638. 'if ($mod.b) ;',
  7639. 'if ($mod.b) ;',
  7640. '']));
  7641. end;
  7642. procedure TTestModule.TestForLoop;
  7643. begin
  7644. StartProgram(false);
  7645. Add('var');
  7646. Add(' vI, vJ, vN: longint;');
  7647. Add('begin');
  7648. Add(' VJ:=0;');
  7649. Add(' VN:=3;');
  7650. Add(' for VI:=1 to VN do');
  7651. Add(' begin');
  7652. Add(' VJ:=VJ+VI;');
  7653. Add(' end;');
  7654. ConvertProgram;
  7655. CheckSource('TestForLoop',
  7656. LinesToStr([ // statements
  7657. 'this.vI = 0;',
  7658. 'this.vJ = 0;',
  7659. 'this.vN = 0;'
  7660. ]),
  7661. LinesToStr([ // this.$main
  7662. ' $mod.vJ = 0;',
  7663. ' $mod.vN = 3;',
  7664. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  7665. ' $mod.vI = $l;',
  7666. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7667. ' };',
  7668. '']));
  7669. end;
  7670. procedure TTestModule.TestForLoopInsideFunction;
  7671. begin
  7672. StartProgram(false);
  7673. Add('function SumNumbers(Count: longint): longint;');
  7674. Add('var');
  7675. Add(' vI, vJ: longint;');
  7676. Add('begin');
  7677. Add(' vj:=0;');
  7678. Add(' for vi:=1 to count do');
  7679. Add(' begin');
  7680. Add(' vj:=vj+vi;');
  7681. Add(' end;');
  7682. Add('end;');
  7683. Add('begin');
  7684. Add(' sumnumbers(3);');
  7685. ConvertProgram;
  7686. CheckSource('TestForLoopInsideFunction',
  7687. LinesToStr([ // statements
  7688. 'this.SumNumbers = function (Count) {',
  7689. ' var Result = 0;',
  7690. ' var vI = 0;',
  7691. ' var vJ = 0;',
  7692. ' vJ = 0;',
  7693. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  7694. ' vI = $l;',
  7695. ' vJ = vJ + vI;',
  7696. ' };',
  7697. ' return Result;',
  7698. '};'
  7699. ]),
  7700. LinesToStr([ // $mod.$main
  7701. ' $mod.SumNumbers(3);'
  7702. ]));
  7703. end;
  7704. procedure TTestModule.TestForLoop_ReadVarAfter;
  7705. begin
  7706. StartProgram(false);
  7707. Add('var');
  7708. Add(' vI: longint;');
  7709. Add('begin');
  7710. Add(' for vi:=1 to 2 do ;');
  7711. Add(' if vi=3 then ;');
  7712. ConvertProgram;
  7713. CheckSource('TestForLoop',
  7714. LinesToStr([ // statements
  7715. 'this.vI = 0;'
  7716. ]),
  7717. LinesToStr([ // this.$main
  7718. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  7719. ' if ($mod.vI===3) ;'
  7720. ]));
  7721. end;
  7722. procedure TTestModule.TestForLoop_Nested;
  7723. begin
  7724. StartProgram(false);
  7725. Add('function SumNumbers(Count: longint): longint;');
  7726. Add('var');
  7727. Add(' vI, vJ, vK: longint;');
  7728. Add('begin');
  7729. Add(' VK:=0;');
  7730. Add(' for VI:=1 to count do');
  7731. Add(' begin');
  7732. Add(' for vj:=1 to vi do');
  7733. Add(' begin');
  7734. Add(' vk:=VK+VI;');
  7735. Add(' end;');
  7736. Add(' end;');
  7737. Add('end;');
  7738. Add('begin');
  7739. Add(' sumnumbers(3);');
  7740. ConvertProgram;
  7741. CheckSource('TestForLoopInFunction',
  7742. LinesToStr([ // statements
  7743. 'this.SumNumbers = function (Count) {',
  7744. ' var Result = 0;',
  7745. ' var vI = 0;',
  7746. ' var vJ = 0;',
  7747. ' var vK = 0;',
  7748. ' vK = 0;',
  7749. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  7750. ' vI = $l;',
  7751. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  7752. ' vJ = $l1;',
  7753. ' vK = vK + vI;',
  7754. ' };',
  7755. ' };',
  7756. ' return Result;',
  7757. '};'
  7758. ]),
  7759. LinesToStr([ // $mod.$main
  7760. ' $mod.SumNumbers(3);'
  7761. ]));
  7762. end;
  7763. procedure TTestModule.TestRepeatUntil;
  7764. begin
  7765. StartProgram(false);
  7766. Add('var');
  7767. Add(' vI, vJ, vN: longint;');
  7768. Add('begin');
  7769. Add(' vn:=3;');
  7770. Add(' vj:=0;');
  7771. Add(' VI:=0;');
  7772. Add(' repeat');
  7773. Add(' VI:=vi+1;');
  7774. Add(' vj:=VJ+vI;');
  7775. Add(' until vi>=vn');
  7776. ConvertProgram;
  7777. CheckSource('TestRepeatUntil',
  7778. LinesToStr([ // statements
  7779. 'this.vI = 0;',
  7780. 'this.vJ = 0;',
  7781. 'this.vN = 0;'
  7782. ]),
  7783. LinesToStr([ // $mod.$main
  7784. ' $mod.vN = 3;',
  7785. ' $mod.vJ = 0;',
  7786. ' $mod.vI = 0;',
  7787. ' do{',
  7788. ' $mod.vI = $mod.vI + 1;',
  7789. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7790. ' }while(!($mod.vI>=$mod.vN));'
  7791. ]));
  7792. end;
  7793. procedure TTestModule.TestAsmBlock;
  7794. begin
  7795. StartProgram(false);
  7796. Add([
  7797. 'var',
  7798. ' vI: longint;',
  7799. 'begin',
  7800. ' vi:=1;',
  7801. ' asm',
  7802. ' if (vI===1) {',
  7803. ' vI=2;',
  7804. //' console.log(''end;'');', ToDo
  7805. ' }',
  7806. ' if (vI===2){ vI=3; }',
  7807. ' end;',
  7808. ' VI:=4;']);
  7809. ConvertProgram;
  7810. CheckSource('TestAsmBlock',
  7811. LinesToStr([ // statements
  7812. 'this.vI = 0;'
  7813. ]),
  7814. LinesToStr([ // $mod.$main
  7815. '$mod.vI = 1;',
  7816. 'if (vI===1) {',
  7817. ' vI=2;',
  7818. '}',
  7819. 'if (vI===2){ vI=3; }',
  7820. ';',
  7821. '$mod.vI = 4;'
  7822. ]));
  7823. end;
  7824. procedure TTestModule.TestAsmPas_Impl;
  7825. begin
  7826. StartUnit(false);
  7827. Add('interface');
  7828. Add('const cIntf: longint = 1;');
  7829. Add('var vIntf: longint;');
  7830. Add('implementation');
  7831. Add('const cImpl: longint = 2;');
  7832. Add('var vImpl: longint;');
  7833. Add('procedure DoIt;');
  7834. Add('const cLoc: longint = 3;');
  7835. Add('var vLoc: longint;');
  7836. Add('begin;');
  7837. Add(' asm');
  7838. //Add(' pas(vIntf)=pas(cIntf);');
  7839. //Add(' pas(vImpl)=pas(cImpl);');
  7840. //Add(' pas(vLoc)=pas(cLoc);');
  7841. Add(' end;');
  7842. Add('end;');
  7843. ConvertUnit;
  7844. CheckSource('TestAsmPas_Impl',
  7845. LinesToStr([
  7846. 'var $impl = $mod.$impl;',
  7847. 'this.cIntf = 1;',
  7848. 'this.vIntf = 0;',
  7849. '']),
  7850. '', // this.$init
  7851. LinesToStr([ // implementation
  7852. '$impl.cImpl = 2;',
  7853. '$impl.vImpl = 0;',
  7854. 'var cLoc = 3;',
  7855. '$impl.DoIt = function () {',
  7856. ' var vLoc = 0;',
  7857. '};',
  7858. '']) );
  7859. end;
  7860. procedure TTestModule.TestTryFinally;
  7861. begin
  7862. StartProgram(false);
  7863. Add('var i: longint;');
  7864. Add('begin');
  7865. Add(' try');
  7866. Add(' i:=0; i:=2 div i;');
  7867. Add(' finally');
  7868. Add(' i:=3');
  7869. Add(' end;');
  7870. ConvertProgram;
  7871. CheckSource('TestTryFinally',
  7872. LinesToStr([ // statements
  7873. 'this.i = 0;'
  7874. ]),
  7875. LinesToStr([ // $mod.$main
  7876. 'try {',
  7877. ' $mod.i = 0;',
  7878. ' $mod.i = Math.floor(2 / $mod.i);',
  7879. '} finally {',
  7880. ' $mod.i = 3;',
  7881. '};'
  7882. ]));
  7883. end;
  7884. procedure TTestModule.TestTryExcept;
  7885. begin
  7886. StartProgram(false);
  7887. Add([
  7888. 'type',
  7889. ' TObject = class end;',
  7890. ' Exception = class Msg: string; end;',
  7891. ' EInvalidCast = class(Exception) end;',
  7892. 'var vI: longint;',
  7893. 'begin',
  7894. ' try',
  7895. ' vi:=1;',
  7896. ' except',
  7897. ' vi:=2',
  7898. ' end;',
  7899. ' try',
  7900. ' vi:=3;',
  7901. ' except',
  7902. ' raise;',
  7903. ' end;',
  7904. ' try',
  7905. ' VI:=4;',
  7906. ' except',
  7907. ' on einvalidcast do',
  7908. ' raise;',
  7909. ' on E: exception do',
  7910. ' if e.msg='''' then',
  7911. ' raise e;',
  7912. ' else',
  7913. ' vi:=5',
  7914. ' end;',
  7915. ' try',
  7916. ' VI:=6;',
  7917. ' except',
  7918. ' on einvalidcast do ;',
  7919. ' end;',
  7920. '']);
  7921. ConvertProgram;
  7922. CheckSource('TestTryExcept',
  7923. LinesToStr([ // statements
  7924. 'rtl.createClass($mod, "TObject", null, function () {',
  7925. ' this.$init = function () {',
  7926. ' };',
  7927. ' this.$final = function () {',
  7928. ' };',
  7929. '});',
  7930. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  7931. ' this.$init = function () {',
  7932. ' $mod.TObject.$init.call(this);',
  7933. ' this.Msg = "";',
  7934. ' };',
  7935. '});',
  7936. 'rtl.createClass($mod, "EInvalidCast", $mod.Exception, function () {',
  7937. '});',
  7938. 'this.vI = 0;'
  7939. ]),
  7940. LinesToStr([ // $mod.$main
  7941. 'try {',
  7942. ' $mod.vI = 1;',
  7943. '} catch ($e) {',
  7944. ' $mod.vI = 2;',
  7945. '};',
  7946. 'try {',
  7947. ' $mod.vI = 3;',
  7948. '} catch ($e) {',
  7949. ' throw $e;',
  7950. '};',
  7951. 'try {',
  7952. ' $mod.vI = 4;',
  7953. '} catch ($e) {',
  7954. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  7955. ' throw $e',
  7956. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  7957. ' var E = $e;',
  7958. ' if (E.Msg === "") throw E;',
  7959. ' } else {',
  7960. ' $mod.vI = 5;',
  7961. ' }',
  7962. '};',
  7963. 'try {',
  7964. ' $mod.vI = 6;',
  7965. '} catch ($e) {',
  7966. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  7967. ' } else throw $e',
  7968. '};',
  7969. '']));
  7970. end;
  7971. procedure TTestModule.TestTryExcept_ReservedWords;
  7972. begin
  7973. StartProgram(false);
  7974. Add([
  7975. 'type',
  7976. ' TObject = class end;',
  7977. ' Exception = class',
  7978. ' Symbol: string;',
  7979. ' end;',
  7980. 'var &try: longint;',
  7981. 'begin',
  7982. ' try',
  7983. ' &try:=4;',
  7984. ' except',
  7985. ' on Error: exception do',
  7986. ' if errOR.symBol='''' then',
  7987. ' raise ERRor;',
  7988. ' end;',
  7989. '']);
  7990. ConvertProgram;
  7991. CheckSource('TestTryExcept_ReservedWords',
  7992. LinesToStr([ // statements
  7993. 'rtl.createClass($mod, "TObject", null, function () {',
  7994. ' this.$init = function () {',
  7995. ' };',
  7996. ' this.$final = function () {',
  7997. ' };',
  7998. '});',
  7999. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  8000. ' this.$init = function () {',
  8001. ' $mod.TObject.$init.call(this);',
  8002. ' this.Symbol = "";',
  8003. ' };',
  8004. '});',
  8005. 'this.Try = 0;',
  8006. '']),
  8007. LinesToStr([ // $mod.$main
  8008. 'try {',
  8009. ' $mod.Try = 4;',
  8010. '} catch ($e) {',
  8011. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8012. ' var error = $e;',
  8013. ' if (error.Symbol === "") throw error;',
  8014. ' } else throw $e',
  8015. '};',
  8016. '']));
  8017. end;
  8018. procedure TTestModule.TestIfThenRaiseElse;
  8019. begin
  8020. StartProgram(false);
  8021. Add([
  8022. 'type',
  8023. ' TObject = class',
  8024. ' constructor Create;',
  8025. ' end;',
  8026. 'constructor TObject.Create;',
  8027. 'begin',
  8028. 'end;',
  8029. 'var b: boolean;',
  8030. 'begin',
  8031. ' if b then',
  8032. ' raise TObject.Create',
  8033. ' else',
  8034. ' b:=false;',
  8035. '']);
  8036. ConvertProgram;
  8037. CheckSource('TestIfThenRaiseElse',
  8038. LinesToStr([ // statements
  8039. 'rtl.createClass($mod, "TObject", null, function () {',
  8040. ' this.$init = function () {',
  8041. ' };',
  8042. ' this.$final = function () {',
  8043. ' };',
  8044. ' this.Create = function () {',
  8045. ' return this;',
  8046. ' };',
  8047. '});',
  8048. 'this.b = false;',
  8049. '']),
  8050. LinesToStr([ // $mod.$main
  8051. 'if ($mod.b) {',
  8052. ' throw $mod.TObject.$create("Create")}',
  8053. ' else $mod.b = false;',
  8054. '']));
  8055. end;
  8056. procedure TTestModule.TestCaseOf;
  8057. begin
  8058. StartProgram(false);
  8059. Add([
  8060. 'const e: longint; external name ''$e'';',
  8061. 'var vI: longint;',
  8062. 'begin',
  8063. ' case vi of',
  8064. ' 1: ;',
  8065. ' 2: vi:=3;',
  8066. ' e: ;',
  8067. ' else',
  8068. ' VI:=4',
  8069. ' end;']);
  8070. ConvertProgram;
  8071. CheckSource('TestCaseOf',
  8072. LinesToStr([ // statements
  8073. 'this.vI = 0;'
  8074. ]),
  8075. LinesToStr([ // $mod.$main
  8076. 'var $tmp = $mod.vI;',
  8077. 'if ($tmp === 1) {}',
  8078. 'else if ($tmp === 2) {',
  8079. ' $mod.vI = 3}',
  8080. ' else if ($tmp === $e) {}',
  8081. 'else {',
  8082. ' $mod.vI = 4;',
  8083. '};'
  8084. ]));
  8085. end;
  8086. procedure TTestModule.TestCaseOf_UseSwitch;
  8087. begin
  8088. StartProgram(false);
  8089. Converter.UseSwitchStatement:=true;
  8090. Add('var Vi: longint;');
  8091. Add('begin');
  8092. Add(' case vi of');
  8093. Add(' 1: ;');
  8094. Add(' 2: VI:=3;');
  8095. Add(' else');
  8096. Add(' vi:=4');
  8097. Add(' end;');
  8098. ConvertProgram;
  8099. CheckSource('TestCaseOf_UseSwitch',
  8100. LinesToStr([ // statements
  8101. 'this.Vi = 0;'
  8102. ]),
  8103. LinesToStr([ // $mod.$main
  8104. 'switch ($mod.Vi) {',
  8105. 'case 1:',
  8106. ' break;',
  8107. 'case 2:',
  8108. ' $mod.Vi = 3;',
  8109. ' break;',
  8110. 'default:',
  8111. ' $mod.Vi = 4;',
  8112. '};'
  8113. ]));
  8114. end;
  8115. procedure TTestModule.TestCaseOfNoElse;
  8116. begin
  8117. StartProgram(false);
  8118. Add('var Vi: longint;');
  8119. Add('begin');
  8120. Add(' case vi of');
  8121. Add(' 1: begin vi:=2; VI:=3; end;');
  8122. Add(' end;');
  8123. ConvertProgram;
  8124. CheckSource('TestCaseOfNoElse',
  8125. LinesToStr([ // statements
  8126. 'this.Vi = 0;'
  8127. ]),
  8128. LinesToStr([ // $mod.$main
  8129. 'var $tmp = $mod.Vi;',
  8130. 'if ($tmp === 1) {',
  8131. ' $mod.Vi = 2;',
  8132. ' $mod.Vi = 3;',
  8133. '};'
  8134. ]));
  8135. end;
  8136. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8137. begin
  8138. StartProgram(false);
  8139. Converter.UseSwitchStatement:=true;
  8140. Add('var vI: longint;');
  8141. Add('begin');
  8142. Add(' case vi of');
  8143. Add(' 1: begin VI:=2; vi:=3; end;');
  8144. Add(' end;');
  8145. ConvertProgram;
  8146. CheckSource('TestCaseOfNoElse_UseSwitch',
  8147. LinesToStr([ // statements
  8148. 'this.vI = 0;'
  8149. ]),
  8150. LinesToStr([ // $mod.$main
  8151. 'switch ($mod.vI) {',
  8152. 'case 1:',
  8153. ' $mod.vI = 2;',
  8154. ' $mod.vI = 3;',
  8155. ' break;',
  8156. '};'
  8157. ]));
  8158. end;
  8159. procedure TTestModule.TestCaseOfRange;
  8160. begin
  8161. StartProgram(false);
  8162. Add('var vI: longint;');
  8163. Add('begin');
  8164. Add(' case vi of');
  8165. Add(' 1..3: vi:=14;');
  8166. Add(' 4,5: vi:=16;');
  8167. Add(' 6..7,9..10: ;');
  8168. Add(' else ;');
  8169. Add(' end;');
  8170. ConvertProgram;
  8171. CheckSource('TestCaseOfRange',
  8172. LinesToStr([ // statements
  8173. 'this.vI = 0;'
  8174. ]),
  8175. LinesToStr([ // $mod.$main
  8176. 'var $tmp = $mod.vI;',
  8177. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8178. ' $mod.vI = 14',
  8179. '} else if (($tmp === 4) || ($tmp === 5)){',
  8180. ' $mod.vI = 16',
  8181. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8182. ]));
  8183. end;
  8184. procedure TTestModule.TestCaseOfString;
  8185. begin
  8186. StartProgram(false);
  8187. Add([
  8188. 'var s,h: string;',
  8189. 'begin',
  8190. ' case s of',
  8191. ' ''foo'': s:=h;',
  8192. ' ''a''..''z'': h:=s;',
  8193. ' ''ў'', ''ё'': ;',
  8194. ' ''Б''..''Я'': ;',
  8195. ' end;',
  8196. '']);
  8197. ConvertProgram;
  8198. CheckSource('TestCaseOfString',
  8199. LinesToStr([ // statements
  8200. 'this.s = "";',
  8201. 'this.h = "";',
  8202. '']),
  8203. LinesToStr([ // $mod.$main
  8204. 'var $tmp = $mod.s;',
  8205. 'if ($tmp === "foo") {',
  8206. ' $mod.s = $mod.h}',
  8207. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8208. ' $mod.h = $mod.s}',
  8209. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8210. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8211. '']));
  8212. end;
  8213. procedure TTestModule.TestCaseOfChar;
  8214. begin
  8215. StartProgram(false);
  8216. Add([
  8217. 'var s,h: char;',
  8218. 'begin',
  8219. ' case s of',
  8220. ' ''a''..''z'': h:=s;',
  8221. ' ''ä'': ;',
  8222. ' ''ў'', ''ё'': ;',
  8223. ' ''Б''..''Я'': ;',
  8224. ' end;',
  8225. '']);
  8226. ConvertProgram;
  8227. CheckSource('TestCaseOfString',
  8228. LinesToStr([ // statements
  8229. 'this.s = "";',
  8230. 'this.h = "";',
  8231. '']),
  8232. LinesToStr([ // $mod.$main
  8233. 'var $tmp = $mod.s;',
  8234. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8235. ' $mod.h = $mod.s}',
  8236. ' else if ($tmp === "ä") {}',
  8237. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8238. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8239. '']));
  8240. end;
  8241. procedure TTestModule.TestCaseOfExternalClassConst;
  8242. begin
  8243. StartProgram(false);
  8244. Add([
  8245. '{$modeswitch externalclass}',
  8246. 'type',
  8247. ' TBird = class external name ''Bird''',
  8248. ' const e: longint;',
  8249. ' end;',
  8250. 'var vI: longint;',
  8251. 'begin',
  8252. ' case vi of',
  8253. ' 1: vi:=3;',
  8254. ' TBird.e: ;',
  8255. ' end;']);
  8256. ConvertProgram;
  8257. CheckSource('TestCaseOfExternalClassConst',
  8258. LinesToStr([ // statements
  8259. 'this.vI = 0;'
  8260. ]),
  8261. LinesToStr([ // $mod.$main
  8262. 'var $tmp = $mod.vI;',
  8263. 'if ($tmp === 1) {',
  8264. ' $mod.vI = 3}',
  8265. ' else if ($tmp === Bird.e) ;'
  8266. ]));
  8267. end;
  8268. procedure TTestModule.TestDebugger;
  8269. begin
  8270. StartProgram(false);
  8271. Add([
  8272. 'procedure DoIt;',
  8273. 'begin',
  8274. ' deBugger;',
  8275. ' DeBugger();',
  8276. 'end;',
  8277. 'begin',
  8278. ' Debugger;']);
  8279. ConvertProgram;
  8280. CheckSource('TestDebugger',
  8281. LinesToStr([ // statements
  8282. 'this.DoIt = function () {',
  8283. ' debugger;',
  8284. ' debugger;',
  8285. '};',
  8286. '']),
  8287. LinesToStr([ // $mod.$main
  8288. 'debugger;',
  8289. '']));
  8290. end;
  8291. procedure TTestModule.TestArray_Dynamic;
  8292. begin
  8293. StartProgram(false);
  8294. Add([
  8295. 'type',
  8296. ' TArrayInt = array of longint;',
  8297. 'var',
  8298. ' Arr: TArrayInt;',
  8299. ' i: longint;',
  8300. ' b: boolean;',
  8301. 'begin',
  8302. ' SetLength(arr,3);',
  8303. ' arr[0]:=4;',
  8304. ' arr[1]:=length(arr)+arr[0];',
  8305. ' arr[i]:=5;',
  8306. ' arr[arr[i]]:=arr[6];',
  8307. ' i:=low(arr);',
  8308. ' i:=high(arr);',
  8309. ' b:=Assigned(arr);',
  8310. ' Arr:=default(TArrayInt);']);
  8311. ConvertProgram;
  8312. CheckSource('TestArray_Dynamic',
  8313. LinesToStr([ // statements
  8314. 'this.Arr = [];',
  8315. 'this.i = 0;',
  8316. 'this.b = false;'
  8317. ]),
  8318. LinesToStr([ // $mod.$main
  8319. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8320. '$mod.Arr[0] = 4;',
  8321. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8322. '$mod.Arr[$mod.i] = 5;',
  8323. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8324. '$mod.i = 0;',
  8325. '$mod.i = rtl.length($mod.Arr) - 1;',
  8326. '$mod.b = rtl.length($mod.Arr) > 0;',
  8327. '$mod.Arr = [];',
  8328. '']));
  8329. end;
  8330. procedure TTestModule.TestArray_Dynamic_Nil;
  8331. begin
  8332. StartProgram(false);
  8333. Add('type');
  8334. Add(' TArrayInt = array of longint;');
  8335. Add('var');
  8336. Add(' Arr: TArrayInt;');
  8337. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8338. Add('begin');
  8339. Add(' arr:=nil;');
  8340. Add(' if arr=nil then;');
  8341. Add(' if nil=arr then;');
  8342. Add(' if arr<>nil then;');
  8343. Add(' if nil<>arr then;');
  8344. Add(' DoIt(nil,nil);');
  8345. ConvertProgram;
  8346. CheckSource('TestArray_Dynamic',
  8347. LinesToStr([ // statements
  8348. 'this.Arr = [];',
  8349. 'this.DoIt = function(i,j){',
  8350. '};'
  8351. ]),
  8352. LinesToStr([ // $mod.$main
  8353. '$mod.Arr = [];',
  8354. 'if (rtl.length($mod.Arr) === 0) ;',
  8355. 'if (rtl.length($mod.Arr) === 0) ;',
  8356. 'if (rtl.length($mod.Arr) > 0) ;',
  8357. 'if (rtl.length($mod.Arr) > 0) ;',
  8358. '$mod.DoIt([],[]);',
  8359. '']));
  8360. end;
  8361. procedure TTestModule.TestArray_DynMultiDimensional;
  8362. begin
  8363. StartProgram(false);
  8364. Add([
  8365. 'type',
  8366. ' TArrayInt = array of longint;',
  8367. ' TArrayArrayInt = array of TArrayInt;',
  8368. 'var',
  8369. ' Arr: TArrayInt;',
  8370. ' Arr2: TArrayArrayInt;',
  8371. ' i: longint;',
  8372. 'begin',
  8373. ' arr2:=nil;',
  8374. ' if arr2=nil then;',
  8375. ' if nil=arr2 then;',
  8376. ' i:=low(arr2);',
  8377. ' i:=low(arr2[1]);',
  8378. ' i:=high(arr2);',
  8379. ' i:=high(arr2[2]);',
  8380. ' arr2[3]:=arr;',
  8381. ' arr2[4][5]:=i;',
  8382. ' i:=arr2[6][7];',
  8383. ' arr2[8,9]:=i;',
  8384. ' i:=arr2[10,11];',
  8385. ' SetLength(arr2,14);',
  8386. ' SetLength(arr2[15],16);']);
  8387. ConvertProgram;
  8388. CheckSource('TestArray_Dynamic',
  8389. LinesToStr([ // statements
  8390. 'this.Arr = [];',
  8391. 'this.Arr2 = [];',
  8392. 'this.i = 0;'
  8393. ]),
  8394. LinesToStr([ // $mod.$main
  8395. '$mod.Arr2 = [];',
  8396. 'if (rtl.length($mod.Arr2) === 0) ;',
  8397. 'if (rtl.length($mod.Arr2) === 0) ;',
  8398. '$mod.i = 0;',
  8399. '$mod.i = 0;',
  8400. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8401. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8402. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8403. '$mod.Arr2[4][5] = $mod.i;',
  8404. '$mod.i = $mod.Arr2[6][7];',
  8405. '$mod.Arr2[8][9] = $mod.i;',
  8406. '$mod.i = $mod.Arr2[10][11];',
  8407. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8408. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8409. '']));
  8410. end;
  8411. procedure TTestModule.TestArray_DynamicAssign;
  8412. begin
  8413. StartProgram(false);
  8414. Add([
  8415. 'type',
  8416. ' TArrayInt = array of longint;',
  8417. ' TArrayArrayInt = array of TArrayInt;',
  8418. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8419. 'begin',
  8420. 'end;',
  8421. 'procedure Fly(var a: TArrayInt);',
  8422. 'begin',
  8423. 'end;',
  8424. 'var',
  8425. ' Arr: TArrayInt;',
  8426. ' Arr2: TArrayArrayInt;',
  8427. 'begin',
  8428. ' arr:=nil;',
  8429. ' arr2:=nil;',
  8430. ' arr2[1]:=nil;',
  8431. ' arr2[2]:=arr;',
  8432. ' Run(arr,arr,arr);',
  8433. ' Fly(arr);',
  8434. ' Run(arr2[4],arr2[5],arr2[6]);',
  8435. ' Fly(arr2[7]);',
  8436. '']);
  8437. ConvertProgram;
  8438. CheckSource('TestArray_DynamicAssign',
  8439. LinesToStr([ // statements
  8440. 'this.Run = function (a, b, c) {',
  8441. '};',
  8442. 'this.Fly = function (a) {',
  8443. '};',
  8444. 'this.Arr = [];',
  8445. 'this.Arr2 = [];',
  8446. '']),
  8447. LinesToStr([ // $mod.$main
  8448. '$mod.Arr = [];',
  8449. '$mod.Arr2 = [];',
  8450. '$mod.Arr2[1] = [];',
  8451. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8452. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8453. '$mod.Fly({',
  8454. ' p: $mod,',
  8455. ' get: function () {',
  8456. ' return this.p.Arr;',
  8457. ' },',
  8458. ' set: function (v) {',
  8459. ' this.p.Arr = v;',
  8460. ' }',
  8461. '});',
  8462. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8463. '$mod.Fly({',
  8464. ' a: 7,',
  8465. ' p: $mod.Arr2,',
  8466. ' get: function () {',
  8467. ' return this.p[this.a];',
  8468. ' },',
  8469. ' set: function (v) {',
  8470. ' this.p[this.a] = v;',
  8471. ' }',
  8472. '});',
  8473. '']));
  8474. end;
  8475. procedure TTestModule.TestArray_StaticInt;
  8476. begin
  8477. StartProgram(false);
  8478. Add('type');
  8479. Add(' TArrayInt = array[2..4] of longint;');
  8480. Add('var');
  8481. Add(' Arr: TArrayInt;');
  8482. Add(' Arr2: TArrayInt = (5,6,7);');
  8483. Add(' i: longint;');
  8484. Add(' b: boolean;');
  8485. Add('begin');
  8486. Add(' arr[2]:=4;');
  8487. Add(' arr[3]:=arr[2]+arr[3];');
  8488. Add(' arr[i]:=5;');
  8489. Add(' arr[arr[i]]:=arr[high(arr)];');
  8490. Add(' i:=low(arr);');
  8491. Add(' i:=high(arr);');
  8492. Add(' b:=arr[2]=arr[3];');
  8493. Add(' arr:=default(TArrayInt);');
  8494. ConvertProgram;
  8495. CheckSource('TestArray_StaticInt',
  8496. LinesToStr([ // statements
  8497. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8498. 'this.Arr2 = [5, 6, 7];',
  8499. 'this.i = 0;',
  8500. 'this.b = false;'
  8501. ]),
  8502. LinesToStr([ // $mod.$main
  8503. '$mod.Arr[0] = 4;',
  8504. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8505. '$mod.Arr[$mod.i-2] = 5;',
  8506. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8507. '$mod.i = 2;',
  8508. '$mod.i = 4;',
  8509. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8510. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8511. '']));
  8512. end;
  8513. procedure TTestModule.TestArray_StaticBool;
  8514. begin
  8515. StartProgram(false);
  8516. Add('type');
  8517. Add(' TBools = array[boolean] of boolean;');
  8518. Add(' TBool2 = array[true..true] of boolean;');
  8519. Add('var');
  8520. Add(' Arr: TBools;');
  8521. Add(' Arr2: TBool2;');
  8522. Add(' Arr3: TBools = (true,false);');
  8523. Add(' b: boolean;');
  8524. Add('begin');
  8525. Add(' b:=low(arr);');
  8526. Add(' b:=high(arr);');
  8527. Add(' arr[true]:=false;');
  8528. Add(' arr[false]:=arr[b] or arr[true];');
  8529. Add(' arr[b]:=true;');
  8530. Add(' arr[arr[b]]:=arr[high(arr)];');
  8531. Add(' b:=arr[false]=arr[true];');
  8532. Add(' b:=low(arr2);');
  8533. Add(' b:=high(arr2);');
  8534. Add(' arr2[true]:=true;');
  8535. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8536. Add(' arr2[b]:=false;');
  8537. ConvertProgram;
  8538. CheckSource('TestArray_StaticBool',
  8539. LinesToStr([ // statements
  8540. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8541. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8542. 'this.Arr3 = [true, false];',
  8543. 'this.b = false;'
  8544. ]),
  8545. LinesToStr([ // $mod.$main
  8546. '$mod.b = false;',
  8547. '$mod.b = true;',
  8548. '$mod.Arr[1] = false;',
  8549. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8550. '$mod.Arr[+$mod.b] = true;',
  8551. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8552. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8553. '$mod.b = true;',
  8554. '$mod.b = true;',
  8555. '$mod.Arr2[0] = true;',
  8556. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8557. '$mod.Arr2[1-$mod.b] = false;',
  8558. '']));
  8559. end;
  8560. procedure TTestModule.TestArray_StaticChar;
  8561. begin
  8562. StartProgram(false);
  8563. Add([
  8564. 'type',
  8565. ' TChars = array[char] of char;',
  8566. ' TChars2 = array[''a''..''z''] of char;',
  8567. 'var',
  8568. ' Arr: TChars;',
  8569. ' Arr2: TChars2;',
  8570. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8571. ' Arr4: array[11..13] of char = ''pas'';',
  8572. ' Arr5: array[21..22] of char = ''äö'';',
  8573. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8574. ' c: char;',
  8575. ' b: boolean;',
  8576. 'begin',
  8577. ' c:=low(arr);',
  8578. ' c:=high(arr);',
  8579. ' arr[''B'']:=''a'';',
  8580. ' arr[''D'']:=arr[c];',
  8581. ' arr[c]:=arr[''d''];',
  8582. ' arr[arr[c]]:=arr[high(arr)];',
  8583. ' b:=arr[low(arr)]=arr[''e''];',
  8584. ' c:=low(arr2);',
  8585. ' c:=high(arr2);',
  8586. ' arr2[''b'']:=''f'';',
  8587. ' arr2[''a'']:=arr2[c];',
  8588. ' arr2[c]:=arr2[''g''];']);
  8589. ConvertProgram;
  8590. CheckSource('TestArray_StaticChar',
  8591. LinesToStr([ // statements
  8592. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8593. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8594. 'this.Arr3 = ["p", "a", "s"];',
  8595. 'this.Arr4 = ["p", "a", "s"];',
  8596. 'this.Arr5 = ["ä", "ö"];',
  8597. 'this.Arr6 = ["ä", "ö"];',
  8598. 'this.c = "";',
  8599. 'this.b = false;',
  8600. '']),
  8601. LinesToStr([ // $mod.$main
  8602. '$mod.c = "\x00";',
  8603. '$mod.c = "\uFFFF";',
  8604. '$mod.Arr[66] = "a";',
  8605. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8606. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8607. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8608. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8609. '$mod.c = "a";',
  8610. '$mod.c = "z";',
  8611. '$mod.Arr2[1] = "f";',
  8612. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8613. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8614. '']));
  8615. end;
  8616. procedure TTestModule.TestArray_StaticMultiDim;
  8617. begin
  8618. StartProgram(false);
  8619. Add([
  8620. 'type',
  8621. ' TArrayInt = array[1..3] of longint;',
  8622. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8623. 'var',
  8624. ' Arr: TArrayInt;',
  8625. ' Arr2: TArrayArrayInt;',
  8626. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8627. ' i: longint;',
  8628. 'begin',
  8629. ' i:=low(arr);',
  8630. ' i:=low(arr2);',
  8631. ' i:=low(arr2[5]);',
  8632. ' i:=high(arr);',
  8633. ' i:=high(arr2);',
  8634. ' i:=high(arr2[6]);',
  8635. ' arr2[5]:=arr;',
  8636. ' arr2[6][2]:=i;',
  8637. ' i:=arr2[6][3];',
  8638. ' arr2[6,3]:=i;',
  8639. ' i:=arr2[5,2];',
  8640. ' arr2:=arr2;',// clone multi dim static array
  8641. //' arr3:=arr3;',// clone anonymous multi dim static array
  8642. '']);
  8643. ConvertProgram;
  8644. CheckSource('TestArray_StaticMultiDim',
  8645. LinesToStr([ // statements
  8646. 'this.TArrayArrayInt$clone = function (a) {',
  8647. ' var r = [];',
  8648. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8649. ' return r;',
  8650. '};',
  8651. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8652. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8653. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8654. 'this.i = 0;'
  8655. ]),
  8656. LinesToStr([ // $mod.$main
  8657. '$mod.i = 1;',
  8658. '$mod.i = 5;',
  8659. '$mod.i = 1;',
  8660. '$mod.i = 3;',
  8661. '$mod.i = 6;',
  8662. '$mod.i = 3;',
  8663. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8664. '$mod.Arr2[1][1] = $mod.i;',
  8665. '$mod.i = $mod.Arr2[1][2];',
  8666. '$mod.Arr2[1][2] = $mod.i;',
  8667. '$mod.i = $mod.Arr2[0][1];',
  8668. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8669. '']));
  8670. end;
  8671. procedure TTestModule.TestArray_StaticInFunction;
  8672. begin
  8673. StartProgram(false);
  8674. Add([
  8675. 'const TArrayInt = 3;',
  8676. 'const TArrayArrayInt = 4;',
  8677. 'procedure DoIt;',
  8678. 'type',
  8679. ' TArrayInt = array[1..3] of longint;',
  8680. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8681. 'var',
  8682. ' Arr: TArrayInt;',
  8683. ' Arr2: TArrayArrayInt;',
  8684. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8685. ' i: longint;',
  8686. 'begin',
  8687. ' arr2[5]:=arr;',
  8688. ' arr2:=arr2;',// clone multi dim static array
  8689. 'end;',
  8690. 'begin',
  8691. '']);
  8692. ConvertProgram;
  8693. CheckSource('TestArray_StaticInFunction',
  8694. LinesToStr([ // statements
  8695. 'this.TArrayInt = 3;',
  8696. 'this.TArrayArrayInt = 4;',
  8697. 'var TArrayArrayInt$1$clone = function (a) {',
  8698. ' var r = [];',
  8699. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8700. ' return r;',
  8701. '};',
  8702. 'this.DoIt = function () {',
  8703. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  8704. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8705. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8706. ' var i = 0;',
  8707. ' Arr2[0] = Arr.slice(0);',
  8708. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  8709. '};',
  8710. '']),
  8711. LinesToStr([ // $mod.$main
  8712. '']));
  8713. end;
  8714. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  8715. begin
  8716. StartProgram(false);
  8717. Add([
  8718. 'type',
  8719. ' TArrayInt = array[1..3,1..2] of longint;',
  8720. 'var',
  8721. ' a,b: TArrayInt;',
  8722. 'begin',
  8723. ' if a=b then ;',
  8724. '']);
  8725. SetExpectedPasResolverError('compare static array is not supported',
  8726. nXIsNotSupported);
  8727. ConvertProgram;
  8728. end;
  8729. procedure TTestModule.TestArrayOfRecord;
  8730. begin
  8731. StartProgram(false);
  8732. Add([
  8733. 'type',
  8734. ' TRec = record',
  8735. ' Int: longint;',
  8736. ' end;',
  8737. ' TArrayRec = array of TRec;',
  8738. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  8739. 'begin',
  8740. 'end;',
  8741. 'var',
  8742. ' Arr: TArrayRec;',
  8743. ' r: TRec;',
  8744. ' i: longint;',
  8745. 'begin',
  8746. ' SetLength(arr,3);',
  8747. ' arr[0].int:=4;',
  8748. ' arr[1].int:=length(arr)+arr[2].int;',
  8749. ' arr[arr[i].int].int:=arr[5].int;',
  8750. ' arr[7]:=r;',
  8751. ' r:=arr[8];',
  8752. ' i:=low(arr);',
  8753. ' i:=high(arr);',
  8754. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  8755. ConvertProgram;
  8756. CheckSource('TestArrayOfRecord',
  8757. LinesToStr([ // statements
  8758. 'rtl.recNewT($mod, "TRec", function () {',
  8759. ' this.Int = 0;',
  8760. ' this.$eq = function (b) {',
  8761. ' return this.Int === b.Int;',
  8762. ' };',
  8763. ' this.$assign = function (s) {',
  8764. ' this.Int = s.Int;',
  8765. ' return this;',
  8766. ' };',
  8767. '});',
  8768. 'this.DoIt = function (vd, vc, vv) {',
  8769. '};',
  8770. 'this.Arr = [];',
  8771. 'this.r = $mod.TRec.$new();',
  8772. 'this.i = 0;'
  8773. ]),
  8774. LinesToStr([ // $mod.$main
  8775. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  8776. '$mod.Arr[0].Int = 4;',
  8777. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  8778. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  8779. '$mod.Arr[7].$assign($mod.r);',
  8780. '$mod.r.$assign($mod.Arr[8]);',
  8781. '$mod.i = 0;',
  8782. '$mod.i = rtl.length($mod.Arr)-1;',
  8783. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  8784. '']));
  8785. end;
  8786. procedure TTestModule.TestArray_StaticRecord;
  8787. begin
  8788. StartProgram(false);
  8789. Add([
  8790. 'type',
  8791. ' TRec = record',
  8792. ' Int: longint;',
  8793. ' end;',
  8794. ' TArrayRec = array[1..2] of TRec;',
  8795. 'var',
  8796. ' Arr: TArrayRec;',
  8797. 'begin',
  8798. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  8799. '']);
  8800. ConvertProgram;
  8801. CheckSource('TestArray_StaticRecord',
  8802. LinesToStr([ // statements
  8803. 'rtl.recNewT($mod, "TRec", function () {',
  8804. ' this.Int = 0;',
  8805. ' this.$eq = function (b) {',
  8806. ' return this.Int === b.Int;',
  8807. ' };',
  8808. ' this.$assign = function (s) {',
  8809. ' this.Int = s.Int;',
  8810. ' return this;',
  8811. ' };',
  8812. '});',
  8813. 'this.TArrayRec$clone = function (a) {',
  8814. ' var r = [];',
  8815. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  8816. ' return r;',
  8817. '};',
  8818. 'this.Arr = rtl.arraySetLength(null, $mod.TRec, 2);',
  8819. '']),
  8820. LinesToStr([ // $mod.$main
  8821. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  8822. end;
  8823. procedure TTestModule.TestArrayOfSet;
  8824. begin
  8825. StartProgram(false);
  8826. Add([
  8827. 'type',
  8828. ' TFlag = (big,small);',
  8829. ' TSetOfFlag = set of tflag;',
  8830. ' TArrayFlag = array of TSetOfFlag;',
  8831. 'procedure DoIt(const a: Tarrayflag);',
  8832. 'begin',
  8833. 'end;',
  8834. 'var',
  8835. ' f: TFlag;',
  8836. ' s: TSetOfFlag;',
  8837. ' Arr: TArrayFlag;',
  8838. ' i: longint;',
  8839. 'begin',
  8840. ' SetLength(arr,3);',
  8841. ' arr[0]:=s;',
  8842. ' arr[1]:=[big];',
  8843. ' arr[2]:=[big]+s;',
  8844. ' arr[3]:=s+[big];',
  8845. ' arr[4]:=arr[5];',
  8846. ' s:=arr[6];',
  8847. ' i:=low(arr);',
  8848. ' i:=high(arr);',
  8849. ' DoIt(arr);',
  8850. ' DoIt([s]);',
  8851. ' DoIt([[],s]);',
  8852. ' DoIt([s,[]]);',
  8853. '']);
  8854. ConvertProgram;
  8855. CheckSource('TestArrayOfSet',
  8856. LinesToStr([ // statements
  8857. 'this.TFlag = {',
  8858. ' "0": "big",',
  8859. ' big: 0,',
  8860. ' "1": "small",',
  8861. ' small: 1',
  8862. '};',
  8863. 'this.DoIt = function (a) {',
  8864. '};',
  8865. 'this.f = 0;',
  8866. 'this.s = {};',
  8867. 'this.Arr = [];',
  8868. 'this.i = 0;',
  8869. '']),
  8870. LinesToStr([ // $mod.$main
  8871. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  8872. '$mod.Arr[0] = rtl.refSet($mod.s);',
  8873. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  8874. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  8875. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  8876. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  8877. '$mod.s = rtl.refSet($mod.Arr[6]);',
  8878. '$mod.i = 0;',
  8879. '$mod.i = rtl.length($mod.Arr) - 1;',
  8880. '$mod.DoIt($mod.Arr);',
  8881. '$mod.DoIt([rtl.refSet($mod.s)]);',
  8882. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  8883. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  8884. '']));
  8885. end;
  8886. procedure TTestModule.TestArray_DynAsParam;
  8887. begin
  8888. StartProgram(false);
  8889. Add([
  8890. 'type integer = longint;',
  8891. 'type TArrInt = array of integer;',
  8892. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8893. 'var vJ: TArrInt;',
  8894. 'begin',
  8895. ' vg:=vg;',
  8896. ' vj:=vh;',
  8897. ' vi:=vi;',
  8898. ' doit(vg,vg,vg);',
  8899. ' doit(vh,vh,vj);',
  8900. ' doit(vi,vi,vi);',
  8901. ' doit(vj,vj,vj);',
  8902. 'end;',
  8903. 'var i: TArrInt;',
  8904. 'begin',
  8905. ' doit(i,i,i);']);
  8906. ConvertProgram;
  8907. CheckSource('TestArray_DynAsParams',
  8908. LinesToStr([ // statements
  8909. 'this.DoIt = function (vG,vH,vI) {',
  8910. ' var vJ = [];',
  8911. ' vG = rtl.arrayRef(vG);',
  8912. ' vJ = rtl.arrayRef(vH);',
  8913. ' vI.set(rtl.arrayRef(vI.get()));',
  8914. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  8915. ' get: function () {',
  8916. ' return vG;',
  8917. ' },',
  8918. ' set: function (v) {',
  8919. ' vG = v;',
  8920. ' }',
  8921. ' });',
  8922. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  8923. ' get: function () {',
  8924. ' return vJ;',
  8925. ' },',
  8926. ' set: function (v) {',
  8927. ' vJ = v;',
  8928. ' }',
  8929. ' });',
  8930. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  8931. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  8932. ' get: function () {',
  8933. ' return vJ;',
  8934. ' },',
  8935. ' set: function (v) {',
  8936. ' vJ = v;',
  8937. ' }',
  8938. ' });',
  8939. '};',
  8940. 'this.i = [];'
  8941. ]),
  8942. LinesToStr([
  8943. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  8944. ' p: $mod,',
  8945. ' get: function () {',
  8946. ' return this.p.i;',
  8947. ' },',
  8948. ' set: function (v) {',
  8949. ' this.p.i = v;',
  8950. ' }',
  8951. '});'
  8952. ]));
  8953. end;
  8954. procedure TTestModule.TestArray_StaticAsParam;
  8955. begin
  8956. StartProgram(false);
  8957. Add([
  8958. 'type integer = longint;',
  8959. 'type TArrInt = array[1..2] of integer;',
  8960. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  8961. 'var vJ: TArrInt;',
  8962. 'begin',
  8963. ' vg:=vg;',
  8964. ' vj:=vh;',
  8965. ' vi:=vi;',
  8966. ' doit(vg,vg,vg);',
  8967. ' doit(vh,vh,vj);',
  8968. ' doit(vi,vi,vi);',
  8969. ' doit(vj,vj,vj);',
  8970. 'end;',
  8971. 'var i: TArrInt;',
  8972. 'begin',
  8973. ' doit(i,i,i);']);
  8974. ConvertProgram;
  8975. CheckSource('TestArray_StaticAsParams',
  8976. LinesToStr([ // statements
  8977. 'this.DoIt = function (vG,vH,vI) {',
  8978. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  8979. ' vG = vG.slice(0);',
  8980. ' vJ = vH.slice(0);',
  8981. ' vI.set(vI.get().slice(0));',
  8982. ' $mod.DoIt(vG.slice(0), vG, {',
  8983. ' get: function () {',
  8984. ' return vG;',
  8985. ' },',
  8986. ' set: function (v) {',
  8987. ' vG = v;',
  8988. ' }',
  8989. ' });',
  8990. ' $mod.DoIt(vH.slice(0), vH, {',
  8991. ' get: function () {',
  8992. ' return vJ;',
  8993. ' },',
  8994. ' set: function (v) {',
  8995. ' vJ = v;',
  8996. ' }',
  8997. ' });',
  8998. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  8999. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9000. ' get: function () {',
  9001. ' return vJ;',
  9002. ' },',
  9003. ' set: function (v) {',
  9004. ' vJ = v;',
  9005. ' }',
  9006. ' });',
  9007. '};',
  9008. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9009. ]),
  9010. LinesToStr([
  9011. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9012. ' p: $mod,',
  9013. ' get: function () {',
  9014. ' return this.p.i;',
  9015. ' },',
  9016. ' set: function (v) {',
  9017. ' this.p.i = v;',
  9018. ' }',
  9019. '});'
  9020. ]));
  9021. end;
  9022. procedure TTestModule.TestArrayElement_AsParams;
  9023. begin
  9024. StartProgram(false);
  9025. Add('type integer = longint;');
  9026. Add('type TArrayInt = array of integer;');
  9027. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9028. Add('var vJ: tarrayint;');
  9029. Add('begin');
  9030. Add(' vi:=vi;');
  9031. Add(' doit(vi,vi,vi);');
  9032. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9033. Add('end;');
  9034. Add('var a: TArrayInt;');
  9035. Add('begin');
  9036. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9037. ConvertProgram;
  9038. CheckSource('TestArrayElement_AsParams',
  9039. LinesToStr([ // statements
  9040. 'this.DoIt = function (vG,vH,vI) {',
  9041. ' var vJ = [];',
  9042. ' vI.set(vI.get());',
  9043. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9044. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9045. ' a:1+3,',
  9046. ' p:vJ,',
  9047. ' get: function () {',
  9048. ' return this.p[this.a];',
  9049. ' },',
  9050. ' set: function (v) {',
  9051. ' this.p[this.a] = v;',
  9052. ' }',
  9053. ' });',
  9054. '};',
  9055. 'this.a = [];'
  9056. ]),
  9057. LinesToStr([
  9058. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9059. ' a: 1+6,',
  9060. ' p: $mod.a,',
  9061. ' get: function () {',
  9062. ' return this.p[this.a];',
  9063. ' },',
  9064. ' set: function (v) {',
  9065. ' this.p[this.a] = v;',
  9066. ' }',
  9067. '});'
  9068. ]));
  9069. end;
  9070. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9071. begin
  9072. StartProgram(false);
  9073. Add('type Integer = longint;');
  9074. Add('type TArrayInt = array of integer;');
  9075. Add('function GetArr(vB: integer = 0): tarrayint;');
  9076. Add('begin');
  9077. Add('end;');
  9078. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9079. Add('begin');
  9080. Add('end;');
  9081. Add('begin');
  9082. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9083. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9084. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9085. ConvertProgram;
  9086. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9087. LinesToStr([ // statements
  9088. 'this.GetArr = function (vB) {',
  9089. ' var Result = [];',
  9090. ' return Result;',
  9091. '};',
  9092. 'this.DoIt = function (vG,vH,vI) {',
  9093. '};'
  9094. ]),
  9095. LinesToStr([
  9096. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9097. ' a: 1+3,',
  9098. ' p: $mod.GetArr(0),',
  9099. ' get: function () {',
  9100. ' return this.p[this.a];',
  9101. ' },',
  9102. ' set: function (v) {',
  9103. ' this.p[this.a] = v;',
  9104. ' }',
  9105. '});',
  9106. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9107. ' a: 2+3,',
  9108. ' p: $mod.GetArr(0),',
  9109. ' get: function () {',
  9110. ' return this.p[this.a];',
  9111. ' },',
  9112. ' set: function (v) {',
  9113. ' this.p[this.a] = v;',
  9114. ' }',
  9115. '});',
  9116. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9117. ' a: 3+3,',
  9118. ' p: $mod.GetArr(9),',
  9119. ' get: function () {',
  9120. ' return this.p[this.a];',
  9121. ' },',
  9122. ' set: function (v) {',
  9123. ' this.p[this.a] = v;',
  9124. ' }',
  9125. '});',
  9126. '']));
  9127. end;
  9128. procedure TTestModule.TestArrayEnumTypeRange;
  9129. begin
  9130. StartProgram(false);
  9131. Add([
  9132. 'type',
  9133. ' TEnum = (red,blue);',
  9134. ' TEnumArray = array[TEnum] of longint;',
  9135. 'var',
  9136. ' e: TEnum;',
  9137. ' i: longint;',
  9138. ' a: TEnumArray;',
  9139. ' numbers: TEnumArray = (1,2);',
  9140. ' names: array[TEnum] of string = (''red'',''blue'');',
  9141. 'begin',
  9142. ' e:=low(a);',
  9143. ' e:=high(a);',
  9144. ' i:=a[red];',
  9145. ' a[e]:=a[e];']);
  9146. ConvertProgram;
  9147. CheckSource('TestArrayEnumTypeRange',
  9148. LinesToStr([ // statements
  9149. ' this.TEnum = {',
  9150. ' "0": "red",',
  9151. ' red: 0,',
  9152. ' "1": "blue",',
  9153. ' blue: 1',
  9154. '};',
  9155. 'this.e = 0;',
  9156. 'this.i = 0;',
  9157. 'this.a = rtl.arraySetLength(null,0,2);',
  9158. 'this.numbers = [1, 2];',
  9159. 'this.names = ["red", "blue"];',
  9160. '']),
  9161. LinesToStr([ // $mod.$main
  9162. '$mod.e = $mod.TEnum.red;',
  9163. '$mod.e = $mod.TEnum.blue;',
  9164. '$mod.i = $mod.a[$mod.TEnum.red];',
  9165. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9166. '']));
  9167. end;
  9168. procedure TTestModule.TestArray_SetLengthOutArg;
  9169. begin
  9170. StartProgram(false);
  9171. Add([
  9172. 'type TArrInt = array of longint;',
  9173. 'procedure DoIt(out a: TArrInt);',
  9174. 'begin',
  9175. ' SetLength(a,2);',
  9176. 'end;',
  9177. 'begin',
  9178. '']);
  9179. ConvertProgram;
  9180. CheckSource('TestArray_SetLengthOutArg',
  9181. LinesToStr([ // statements
  9182. 'this.DoIt = function (a) {',
  9183. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9184. '};',
  9185. '']),
  9186. LinesToStr([
  9187. '']));
  9188. end;
  9189. procedure TTestModule.TestArray_SetLengthProperty;
  9190. begin
  9191. StartProgram(false);
  9192. Add('type');
  9193. Add(' TArrInt = array of longint;');
  9194. Add(' TObject = class');
  9195. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9196. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9197. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9198. Add(' end;');
  9199. Add('var Obj: TObject;');
  9200. Add('begin');
  9201. Add(' SetLength(Obj.Colors,2);');
  9202. ConvertProgram;
  9203. CheckSource('TestArray_SetLengthProperty',
  9204. LinesToStr([ // statements
  9205. 'rtl.createClass($mod, "TObject", null, function () {',
  9206. ' this.$init = function () {',
  9207. ' };',
  9208. ' this.$final = function () {',
  9209. ' };',
  9210. '});',
  9211. 'this.Obj = null;',
  9212. '']),
  9213. LinesToStr([
  9214. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9215. '']));
  9216. end;
  9217. procedure TTestModule.TestArray_SetLengthMultiDim;
  9218. begin
  9219. StartProgram(false);
  9220. Add([
  9221. 'type',
  9222. ' TArrArrInt = array of array of longint;',
  9223. ' TArrStaInt = array of array[1..2] of longint;',
  9224. 'var',
  9225. ' a: TArrArrInt;',
  9226. ' b: TArrStaInt;',
  9227. 'begin',
  9228. ' SetLength(a,2);',
  9229. ' SetLength(a,3,4);',
  9230. ' SetLength(b,5);',
  9231. '']);
  9232. ConvertProgram;
  9233. CheckSource('TestArray_SetLengthMultiDim',
  9234. LinesToStr([ // statements
  9235. 'this.a = [];',
  9236. 'this.b = [];',
  9237. '']),
  9238. LinesToStr([
  9239. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9240. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9241. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9242. '']));
  9243. end;
  9244. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9245. begin
  9246. StartProgram(false);
  9247. Add([
  9248. 'type',
  9249. ' TStaArr1 = array[1..3] of boolean;',
  9250. //' TStaArr2 = array[5..6] of TStaArr1;',
  9251. ' TDynArr1StaArr1 = array of TStaArr1;',
  9252. //' TDynArr1StaArr2 = array of TStaArr2;',
  9253. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9254. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9255. 'var',
  9256. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9257. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9258. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9259. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9260. 'begin',
  9261. ' SetLength(DynArr1StaArr1,11);',
  9262. ' SetLength(DynArr2StaArr1,12);',
  9263. ' SetLength(DynArr2StaArr1[13],14);',
  9264. ' SetLength(DynArr2StaArr1,15,16);',
  9265. //' SetLength(DynArr1StaArr2,21);',
  9266. //' SetLength(DynArr2StaArr2,22);',
  9267. //' SetLength(DynArr2StaArr2[23],24);',
  9268. //' SetLength(DynArr2StaArr2,25,26);',
  9269. '']);
  9270. ConvertProgram;
  9271. CheckSource('TestArray_DynOfStatic',
  9272. LinesToStr([ // statements
  9273. 'this.DynArr1StaArr1 = [];',
  9274. 'this.DynArr2StaArr1 = [];',
  9275. '']),
  9276. LinesToStr([ // $mod.$main
  9277. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9278. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9279. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9280. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9281. ' $mod.DynArr2StaArr1,',
  9282. ' false,',
  9283. ' 15,',
  9284. ' 16,',
  9285. ' "s",',
  9286. ' 3',
  9287. ');',
  9288. '']));
  9289. end;
  9290. procedure TTestModule.TestArray_OpenArrayOfString;
  9291. begin
  9292. StartProgram(false);
  9293. Add('procedure DoIt(const a: array of String);');
  9294. Add('var');
  9295. Add(' i: longint;');
  9296. Add(' s: string;');
  9297. Add('begin');
  9298. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9299. Add('end;');
  9300. Add('var s: string;');
  9301. Add('begin');
  9302. Add(' DoIt([]);');
  9303. Add(' DoIt([s,''foo'','''',s+s]);');
  9304. ConvertProgram;
  9305. CheckSource('TestArray_OpenArrayOfString',
  9306. LinesToStr([ // statements
  9307. 'this.DoIt = function (a) {',
  9308. ' var i = 0;',
  9309. ' var s = "";',
  9310. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9311. ' i = $l;',
  9312. ' s = a[rtl.length(a) - i - 1];',
  9313. ' };',
  9314. '};',
  9315. 'this.s = "";',
  9316. '']),
  9317. LinesToStr([
  9318. '$mod.DoIt([]);',
  9319. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9320. '']));
  9321. end;
  9322. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9323. begin
  9324. StartProgram(false);
  9325. Add([
  9326. 'type TArr = array of char;',
  9327. 'var',
  9328. ' c: char;',
  9329. ' s: string;',
  9330. ' a: TArr;',
  9331. 'procedure Run(const a: array of char);',
  9332. 'begin',
  9333. ' Run(c);',
  9334. ' Run(s);',
  9335. 'end;',
  9336. 'begin',
  9337. ' a:=c;',
  9338. ' a:=s;',
  9339. ' a:=#13;',
  9340. ' a:=''Foo'';',
  9341. ' Run(c);',
  9342. ' Run(s);',
  9343. '']);
  9344. ConvertProgram;
  9345. CheckSource('TestArray_ArrayOfCharAssignString',
  9346. LinesToStr([ // statements
  9347. 'this.c = "";',
  9348. 'this.s = "";',
  9349. 'this.a = [];',
  9350. 'this.Run = function (a) {',
  9351. ' $mod.Run($mod.c.split(""));',
  9352. ' $mod.Run($mod.s.split(""));',
  9353. '};',
  9354. '']),
  9355. LinesToStr([
  9356. '$mod.a = $mod.c.split("");',
  9357. '$mod.a = $mod.s.split("");',
  9358. '$mod.a = "\r".split("");',
  9359. '$mod.a = "Foo".split("");',
  9360. '$mod.Run($mod.c.split(""));',
  9361. '$mod.Run($mod.s.split(""));',
  9362. '']));
  9363. end;
  9364. procedure TTestModule.TestArray_ConstRef;
  9365. begin
  9366. StartProgram(false);
  9367. Add([
  9368. 'type TArr = array of word;',
  9369. 'procedure Run(constref a: TArr);',
  9370. 'begin',
  9371. 'end;',
  9372. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9373. 'var l: TArr;',
  9374. 'begin',
  9375. ' Run(l);',
  9376. ' Run(a);',
  9377. ' Run(b);',
  9378. ' Run(c);',
  9379. ' Run(d);',
  9380. ' Run(e);',
  9381. 'end;',
  9382. 'begin',
  9383. '']);
  9384. ConvertProgram;
  9385. CheckResolverUnexpectedHints();
  9386. CheckSource('TestArray_ConstRef',
  9387. LinesToStr([ // statements
  9388. 'this.Run = function (a) {',
  9389. '};',
  9390. 'this.Fly = function (a, b, c, d, e) {',
  9391. ' var l = [];',
  9392. ' $mod.Run(l);',
  9393. ' $mod.Run(a);',
  9394. ' $mod.Run(b.get());',
  9395. ' $mod.Run(c.get());',
  9396. ' $mod.Run(d);',
  9397. ' $mod.Run(e);',
  9398. '};',
  9399. '']),
  9400. LinesToStr([
  9401. '']));
  9402. end;
  9403. procedure TTestModule.TestArray_Concat;
  9404. begin
  9405. StartProgram(false);
  9406. Add([
  9407. 'type',
  9408. ' integer = longint;',
  9409. ' TFlag = (big,small);',
  9410. ' TFlags = set of TFlag;',
  9411. ' TRec = record',
  9412. ' i: integer;',
  9413. ' end;',
  9414. ' TArrInt = array of integer;',
  9415. ' TArrRec = array of TRec;',
  9416. ' TArrFlag = array of TFlag;',
  9417. ' TArrSet = array of TFlags;',
  9418. ' TArrJSValue = array of jsvalue;',
  9419. 'var',
  9420. ' ArrInt: tarrint;',
  9421. ' ArrRec: tarrrec;',
  9422. ' ArrFlag: tarrflag;',
  9423. ' ArrSet: tarrset;',
  9424. ' ArrJSValue: tarrjsvalue;',
  9425. 'begin',
  9426. ' arrint:=concat(arrint);',
  9427. ' arrint:=concat(arrint,arrint);',
  9428. ' arrint:=concat(arrint,arrint,arrint);',
  9429. ' arrrec:=concat(arrrec);',
  9430. ' arrrec:=concat(arrrec,arrrec);',
  9431. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9432. ' arrset:=concat(arrset);',
  9433. ' arrset:=concat(arrset,arrset);',
  9434. ' arrset:=concat(arrset,arrset,arrset);',
  9435. ' arrjsvalue:=concat(arrjsvalue);',
  9436. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9437. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9438. ' arrint:=concat([1],arrint);',
  9439. ' arrflag:=concat([big]);',
  9440. ' arrflag:=concat([big],arrflag);',
  9441. ' arrflag:=concat(arrflag,[small]);',
  9442. '']);
  9443. ConvertProgram;
  9444. CheckSource('TestArray_Concat',
  9445. LinesToStr([ // statements
  9446. 'this.TFlag = {',
  9447. ' "0": "big",',
  9448. ' big: 0,',
  9449. ' "1": "small",',
  9450. ' small: 1',
  9451. '};',
  9452. 'rtl.recNewT($mod, "TRec", function () {',
  9453. ' this.i = 0;',
  9454. ' this.$eq = function (b) {',
  9455. ' return this.i === b.i;',
  9456. ' };',
  9457. ' this.$assign = function (s) {',
  9458. ' this.i = s.i;',
  9459. ' return this;',
  9460. ' };',
  9461. '});',
  9462. 'this.ArrInt = [];',
  9463. 'this.ArrRec = [];',
  9464. 'this.ArrFlag = [];',
  9465. 'this.ArrSet = [];',
  9466. 'this.ArrJSValue = [];',
  9467. '']),
  9468. LinesToStr([ // $mod.$main
  9469. '$mod.ArrInt = $mod.ArrInt;',
  9470. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9471. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9472. '$mod.ArrRec = $mod.ArrRec;',
  9473. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9474. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9475. '$mod.ArrSet = $mod.ArrSet;',
  9476. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9477. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9478. '$mod.ArrJSValue = $mod.ArrJSValue;',
  9479. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9480. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9481. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9482. '$mod.ArrFlag = [$mod.TFlag.big];',
  9483. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9484. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9485. '']));
  9486. end;
  9487. procedure TTestModule.TestArray_Copy;
  9488. begin
  9489. StartProgram(false);
  9490. Add([
  9491. 'type',
  9492. ' integer = longint;',
  9493. ' TFlag = (big,small);',
  9494. ' TFlags = set of TFlag;',
  9495. ' TRec = record',
  9496. ' i: integer;',
  9497. ' end;',
  9498. ' TArrInt = array of integer;',
  9499. ' TArrRec = array of TRec;',
  9500. ' TArrSet = array of TFlags;',
  9501. ' TArrJSValue = array of jsvalue;',
  9502. 'var',
  9503. ' ArrInt: tarrint;',
  9504. ' ArrRec: tarrrec;',
  9505. ' ArrSet: tarrset;',
  9506. ' ArrJSValue: tarrjsvalue;',
  9507. 'begin',
  9508. ' arrint:=copy(arrint);',
  9509. ' arrint:=copy(arrint,2);',
  9510. ' arrint:=copy(arrint,3,4);',
  9511. ' arrint:=copy([1,1],1,2);',
  9512. ' arrrec:=copy(arrrec);',
  9513. ' arrrec:=copy(arrrec,5);',
  9514. ' arrrec:=copy(arrrec,6,7);',
  9515. ' arrset:=copy(arrset);',
  9516. ' arrset:=copy(arrset,8);',
  9517. ' arrset:=copy(arrset,9,10);',
  9518. ' arrjsvalue:=copy(arrjsvalue);',
  9519. ' arrjsvalue:=copy(arrjsvalue,11);',
  9520. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9521. ' ']);
  9522. ConvertProgram;
  9523. CheckSource('TestArray_Copy',
  9524. LinesToStr([ // statements
  9525. 'this.TFlag = {',
  9526. ' "0": "big",',
  9527. ' big: 0,',
  9528. ' "1": "small",',
  9529. ' small: 1',
  9530. '};',
  9531. 'rtl.recNewT($mod, "TRec", function () {',
  9532. ' this.i = 0;',
  9533. ' this.$eq = function (b) {',
  9534. ' return this.i === b.i;',
  9535. ' };',
  9536. ' this.$assign = function (s) {',
  9537. ' this.i = s.i;',
  9538. ' return this;',
  9539. ' };',
  9540. '});',
  9541. 'this.ArrInt = [];',
  9542. 'this.ArrRec = [];',
  9543. 'this.ArrSet = [];',
  9544. 'this.ArrJSValue = [];',
  9545. '']),
  9546. LinesToStr([ // $mod.$main
  9547. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9548. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9549. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9550. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9551. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9552. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9553. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9554. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9555. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9556. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9557. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9558. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9559. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9560. '']));
  9561. end;
  9562. procedure TTestModule.TestArray_InsertDelete;
  9563. begin
  9564. StartProgram(false);
  9565. Add([
  9566. 'type',
  9567. ' integer = longint;',
  9568. ' TFlag = (big,small);',
  9569. ' TFlags = set of TFlag;',
  9570. ' TRec = record',
  9571. ' i: integer;',
  9572. ' end;',
  9573. ' TArrInt = array of integer;',
  9574. ' TArrRec = array of TRec;',
  9575. ' TArrSet = array of TFlags;',
  9576. ' TArrJSValue = array of jsvalue;',
  9577. ' TArrArrInt = array of TArrInt;',
  9578. 'var',
  9579. ' ArrInt: tarrint;',
  9580. ' ArrRec: tarrrec;',
  9581. ' ArrSet: tarrset;',
  9582. ' ArrJSValue: tarrjsvalue;',
  9583. ' ArrArrInt: TArrArrInt;',
  9584. 'begin',
  9585. ' Insert(1,arrint,2);',
  9586. ' Insert(arrint[3],arrint,4);',
  9587. ' Insert(arrrec[5],arrrec,6);',
  9588. ' Insert(arrset[7],arrset,7);',
  9589. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9590. ' Insert(10,arrjsvalue,11);',
  9591. ' Insert([23],arrarrint,22);',
  9592. ' Delete(arrint,12,13);',
  9593. ' Delete(arrrec,14,15);',
  9594. ' Delete(arrset,17,18);',
  9595. ' Delete(arrjsvalue,19,10);']);
  9596. ConvertProgram;
  9597. CheckSource('TestArray_InsertDelete',
  9598. LinesToStr([ // statements
  9599. 'this.TFlag = {',
  9600. ' "0": "big",',
  9601. ' big: 0,',
  9602. ' "1": "small",',
  9603. ' small: 1',
  9604. '};',
  9605. 'rtl.recNewT($mod, "TRec", function () {',
  9606. ' this.i = 0;',
  9607. ' this.$eq = function (b) {',
  9608. ' return this.i === b.i;',
  9609. ' };',
  9610. ' this.$assign = function (s) {',
  9611. ' this.i = s.i;',
  9612. ' return this;',
  9613. ' };',
  9614. '});',
  9615. 'this.ArrInt = [];',
  9616. 'this.ArrRec = [];',
  9617. 'this.ArrSet = [];',
  9618. 'this.ArrJSValue = [];',
  9619. 'this.ArrArrInt = [];',
  9620. '']),
  9621. LinesToStr([ // $mod.$main
  9622. '$mod.ArrInt.splice(2, 0, 1);',
  9623. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9624. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9625. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9626. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9627. '$mod.ArrJSValue.splice(11, 0, 10);',
  9628. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9629. '$mod.ArrInt.splice(12, 13);',
  9630. '$mod.ArrRec.splice(14, 15);',
  9631. '$mod.ArrSet.splice(17, 18);',
  9632. '$mod.ArrJSValue.splice(19, 10);',
  9633. '']));
  9634. end;
  9635. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9636. begin
  9637. StartProgram(false);
  9638. Add([
  9639. '{$modeswitch arrayoperators}',
  9640. 'type',
  9641. ' integer = longint;',
  9642. ' TArrInt = array of integer;',
  9643. ' TArrStr = array of string;',
  9644. 'const',
  9645. ' Ints: TArrInt = (1,2,3);',
  9646. ' Aliases: TarrStr = (''foo'',''b'');',
  9647. ' OneInt: TArrInt = (7);',
  9648. ' OneStr: array of integer = (7);',
  9649. ' Chars: array of char = ''aoc'';',
  9650. ' Names: array of string = (''a'',''foo'');',
  9651. ' NameCount = low(Names)+high(Names)+length(Names);',
  9652. 'var i: integer;',
  9653. 'begin',
  9654. ' Ints:=[];',
  9655. ' Ints:=[1,1];',
  9656. ' Ints:=[1]+[2];',
  9657. ' Ints:=[2];',
  9658. ' Ints:=[]+ints;',
  9659. ' Ints:=Ints+[];',
  9660. ' Ints:=Ints+OneInt;',
  9661. ' Ints:=Ints+[1,1];',
  9662. ' Ints:=[i,i]+Ints;',
  9663. ' Ints:=[1]+[i]+[3];',
  9664. '']);
  9665. ConvertProgram;
  9666. CheckSource('TestArray_DynArrayConstObjFPC',
  9667. LinesToStr([ // statements
  9668. 'this.Ints = [1, 2, 3];',
  9669. 'this.Aliases = ["foo", "b"];',
  9670. 'this.OneInt = [7];',
  9671. 'this.OneStr = [7];',
  9672. 'this.Chars = ["a", "o", "c"];',
  9673. 'this.Names = ["a", "foo"];',
  9674. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9675. 'this.i = 0;',
  9676. '']),
  9677. LinesToStr([ // $mod.$main
  9678. '$mod.Ints = [];',
  9679. '$mod.Ints = [1, 1];',
  9680. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9681. '$mod.Ints = [2];',
  9682. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  9683. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  9684. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  9685. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  9686. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  9687. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  9688. '']));
  9689. end;
  9690. procedure TTestModule.TestArray_DynArrayConstDelphi;
  9691. begin
  9692. StartProgram(false);
  9693. // Note: const c = [1,1]; defines a set!
  9694. Add([
  9695. '{$mode delphi}',
  9696. 'type',
  9697. ' integer = longint;',
  9698. ' TArrInt = array of integer;',
  9699. ' TArrStr = array of string;',
  9700. 'const',
  9701. ' Ints: TArrInt = [1,1,2];',
  9702. ' Aliases: TarrStr = [''foo'',''b''];',
  9703. ' OneInt: TArrInt = [7];',
  9704. ' OneStr: array of integer = [7]+[8];',
  9705. ' Chars: array of char = ''aoc'';',
  9706. ' Names: array of string = [''a'',''a''];',
  9707. ' NameCount = low(Names)+high(Names)+length(Names);',
  9708. 'begin',
  9709. '']);
  9710. ConvertProgram;
  9711. CheckSource('TestArray_DynArrayConstDelphi',
  9712. LinesToStr([ // statements
  9713. 'this.Ints = [1, 1, 2];',
  9714. 'this.Aliases = ["foo", "b"];',
  9715. 'this.OneInt = [7];',
  9716. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  9717. 'this.Chars = ["a", "o", "c"];',
  9718. 'this.Names = ["a", "a"];',
  9719. 'this.NameCount = 0 + (rtl.length($mod.Names) - 1) + rtl.length($mod.Names);',
  9720. '']),
  9721. LinesToStr([ // $mod.$main
  9722. '']));
  9723. end;
  9724. procedure TTestModule.TestArray_ArrayLitAsParam;
  9725. begin
  9726. StartProgram(false);
  9727. Add([
  9728. '{$modeswitch arrayoperators}',
  9729. 'type',
  9730. ' integer = longint;',
  9731. ' TArrInt = array of integer;',
  9732. ' TArrSet = array of (red,green,blue);',
  9733. 'procedure DoOpenInt(const a: array of integer); forward;',
  9734. 'procedure DoInt(const a: TArrInt);',
  9735. 'begin',
  9736. ' DoInt(a+[1]);',
  9737. ' DoInt([1]+a);',
  9738. ' DoOpenInt(a);',
  9739. ' DoOpenInt(a+[1]);',
  9740. ' DoOpenInt([1]+a);',
  9741. 'end;',
  9742. 'procedure DoOpenInt(const a: array of integer);',
  9743. 'begin',
  9744. ' DoOpenInt(a+[1]);',
  9745. ' DoOpenInt([1]+a);',
  9746. ' DoInt(a);',
  9747. ' DoInt(a+[1]);',
  9748. ' DoInt([1]+a);',
  9749. 'end;',
  9750. 'procedure DoSet(const a: TArrSet);',
  9751. 'begin',
  9752. ' DoSet(a+[red]);',
  9753. ' DoSet([blue]+a);',
  9754. 'end;',
  9755. 'var',
  9756. ' i: TArrInt;',
  9757. ' s: TArrSet;',
  9758. 'begin',
  9759. ' DoInt([1]);',
  9760. ' DoInt([1]+[2]);',
  9761. ' DoInt(i+[1]);',
  9762. ' DoInt([1]+i);',
  9763. ' DoOpenInt([1]);',
  9764. ' DoOpenInt([1]+[2]);',
  9765. ' DoOpenInt(i+[1]);',
  9766. ' DoOpenInt([1]+i);',
  9767. ' DoSet([red]);',
  9768. ' DoSet([blue]+[green]);',
  9769. ' DoSet(s+[blue]);',
  9770. ' DoSet([red]+s);',
  9771. '']);
  9772. ConvertProgram;
  9773. CheckSource('TestArray_ArrayLitAsParam',
  9774. LinesToStr([ // statements
  9775. 'this.TArrSet$a = {',
  9776. ' "0": "red",',
  9777. ' red: 0,',
  9778. ' "1": "green",',
  9779. ' green: 1,',
  9780. ' "2": "blue",',
  9781. ' blue: 2',
  9782. '};',
  9783. 'this.DoInt = function (a) {',
  9784. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9785. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9786. ' $mod.DoOpenInt(a);',
  9787. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9788. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9789. '};',
  9790. 'this.DoOpenInt = function (a) {',
  9791. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  9792. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  9793. ' $mod.DoInt(a);',
  9794. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  9795. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  9796. '};',
  9797. 'this.DoSet = function (a) {',
  9798. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  9799. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  9800. '};',
  9801. 'this.i = [];',
  9802. 'this.s = [];',
  9803. '']),
  9804. LinesToStr([ // $mod.$main
  9805. '$mod.DoInt([1]);',
  9806. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  9807. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  9808. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  9809. '$mod.DoOpenInt([1]);',
  9810. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  9811. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  9812. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  9813. '$mod.DoSet([$mod.TArrSet$a.red]);',
  9814. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  9815. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  9816. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  9817. '']));
  9818. end;
  9819. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  9820. begin
  9821. StartProgram(false);
  9822. Add([
  9823. '{$modeswitch arrayoperators}',
  9824. 'type',
  9825. ' integer = longint;',
  9826. ' TArrInt = array of integer;',
  9827. ' TArrArrInt = array of TArrInt;',
  9828. 'procedure DoInt(const a: TArrArrInt);',
  9829. 'begin',
  9830. ' DoInt(a+[[1]]);',
  9831. ' DoInt([[1]]+a);',
  9832. ' DoInt(a);',
  9833. 'end;',
  9834. 'var',
  9835. ' i: TArrInt;',
  9836. ' a: TArrArrInt;',
  9837. 'begin',
  9838. ' a:=[[1]];',
  9839. ' a:=[i];',
  9840. ' a:=a+[i];',
  9841. ' a:=[i]+a;',
  9842. ' a:=[[1]+i];',
  9843. ' a:=[[1]+[2]];',
  9844. ' a:=[i+[2]];',
  9845. ' DoInt([[1]]);',
  9846. ' DoInt([[1]+[2],[3,4],[5]]);',
  9847. ' DoInt([i+[1]]+a);',
  9848. ' DoInt([i]+a);',
  9849. '']);
  9850. ConvertProgram;
  9851. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  9852. LinesToStr([ // statements
  9853. 'this.DoInt = function (a) {',
  9854. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  9855. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  9856. ' $mod.DoInt(a);',
  9857. '};',
  9858. 'this.i = [];',
  9859. 'this.a = [];',
  9860. '']),
  9861. LinesToStr([ // $mod.$main
  9862. '$mod.a = [[1]];',
  9863. '$mod.a = [$mod.i];',
  9864. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  9865. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  9866. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  9867. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  9868. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  9869. '$mod.DoInt([[1]]);',
  9870. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  9871. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  9872. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  9873. '']));
  9874. end;
  9875. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  9876. begin
  9877. StartProgram(false);
  9878. Add([
  9879. '{$modeswitch arrayoperators}',
  9880. 'type',
  9881. ' integer = longint;',
  9882. ' TArrInt = array[1..2] of integer;',
  9883. ' TArrArrInt = array of TArrInt;',
  9884. 'procedure DoInt(const a: TArrArrInt);',
  9885. 'begin',
  9886. ' DoInt(a+[[1,2]]);',
  9887. ' DoInt([[1,2]]+a);',
  9888. ' DoInt(a);',
  9889. 'end;',
  9890. 'var',
  9891. ' i: TArrInt;',
  9892. ' a: TArrArrInt;',
  9893. 'begin',
  9894. ' a:=[[1,1]];',
  9895. ' a:=[i];',
  9896. ' a:=a+[i];',
  9897. ' a:=[i]+a;',
  9898. ' DoInt([[1,1]]);',
  9899. ' DoInt([[1,2],[3,4]]);',
  9900. '']);
  9901. ConvertProgram;
  9902. CheckSource('TestArray_ArrayLitStaticAsParam',
  9903. LinesToStr([ // statements
  9904. 'this.DoInt = function (a) {',
  9905. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  9906. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  9907. ' $mod.DoInt(a);',
  9908. '};',
  9909. 'this.i = rtl.arraySetLength(null, 0, 2);',
  9910. 'this.a = [];',
  9911. '']),
  9912. LinesToStr([ // $mod.$main
  9913. '$mod.a = [[1, 1]];',
  9914. '$mod.a = [$mod.i.slice(0)];',
  9915. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  9916. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  9917. '$mod.DoInt([[1, 1]]);',
  9918. '$mod.DoInt([[1, 2], [3, 4]]);',
  9919. '']));
  9920. end;
  9921. procedure TTestModule.TestArray_ForInArrOfString;
  9922. begin
  9923. StartProgram(false);
  9924. Add([
  9925. 'type',
  9926. 'type',
  9927. ' TMonthNameArray = array [1..12] of string;',
  9928. ' TMonthNames = TMonthNameArray;',
  9929. ' TObject = class',
  9930. ' private',
  9931. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  9932. ' public',
  9933. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  9934. ' end;',
  9935. 'var',
  9936. ' f: TObject;',
  9937. ' Month: string;',
  9938. ' Names: array of string = (''a'',''foo'',''bar'');',
  9939. ' i: longint;',
  9940. 'begin',
  9941. ' for Month in f.LongMonthNames do ;',
  9942. ' for Month in Names do ;',
  9943. ' for i:=low(Names) to high(Names) do ;',
  9944. '']);
  9945. ConvertProgram;
  9946. CheckSource('TestArray_ForInArrOfString',
  9947. LinesToStr([ // statements
  9948. 'rtl.createClass($mod, "TObject", null, function () {',
  9949. ' this.$init = function () {',
  9950. ' };',
  9951. ' this.$final = function () {',
  9952. ' };',
  9953. '});',
  9954. 'this.f = null;',
  9955. 'this.Month = "";',
  9956. 'this.Names = ["a", "foo", "bar"];',
  9957. 'this.i = 0;',
  9958. '']),
  9959. LinesToStr([ // $mod.$main
  9960. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  9961. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  9962. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  9963. '']));
  9964. end;
  9965. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  9966. begin
  9967. StartProgram(false);
  9968. Add([
  9969. '{$modeswitch externalclass}',
  9970. 'type',
  9971. ' TJSObject = class external name ''Object''',
  9972. ' end;',
  9973. ' TJSArray = class external name ''Array''',
  9974. ' class function isArray(Value: JSValue) : boolean;',
  9975. ' function concat() : TJSArray; varargs;',
  9976. ' end;',
  9977. 'var',
  9978. ' aObj: TJSArray;',
  9979. ' a: array of longint;',
  9980. ' o: TJSObject;',
  9981. 'begin',
  9982. ' if TJSArray.isArray(65) then ;',
  9983. ' aObj:=TJSArray(a).concat(a);',
  9984. ' o:=TJSObject(a);']);
  9985. ConvertProgram;
  9986. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  9987. LinesToStr([ // statements
  9988. 'this.aObj = null;',
  9989. 'this.a = [];',
  9990. 'this.o = null;',
  9991. '']),
  9992. LinesToStr([ // $mod.$main
  9993. 'if (Array.isArray(65)) ;',
  9994. '$mod.aObj = $mod.a.concat($mod.a);',
  9995. '$mod.o = $mod.a;',
  9996. '']));
  9997. end;
  9998. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  9999. begin
  10000. StartProgram(false);
  10001. Add([
  10002. '{$modeswitch externalclass}',
  10003. 'type',
  10004. ' TArrStr = array of string;',
  10005. ' TJSArray = class external name ''Array''',
  10006. ' end;',
  10007. ' TJSObject = class external name ''Object''',
  10008. ' end;',
  10009. 'var',
  10010. ' aObj: TJSArray;',
  10011. ' a: TArrStr;',
  10012. ' jo: TJSObject;',
  10013. 'begin',
  10014. ' a:=TArrStr(aObj);',
  10015. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10016. ' a:=TarrStr(jo);',
  10017. '']);
  10018. ConvertProgram;
  10019. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10020. LinesToStr([ // statements
  10021. 'this.aObj = null;',
  10022. 'this.a = [];',
  10023. 'this.jo = null;',
  10024. '']),
  10025. LinesToStr([ // $mod.$main
  10026. '$mod.a = $mod.aObj;',
  10027. '$mod.aObj[1] = $mod.aObj[2];',
  10028. '$mod.a = $mod.jo;',
  10029. '']));
  10030. end;
  10031. procedure TTestModule.TestArrayOfConst_TVarRec;
  10032. begin
  10033. StartProgram(true,[supTVarRec]);
  10034. Add([
  10035. 'procedure Say(args: array of const);',
  10036. 'var',
  10037. ' i: longint;',
  10038. ' v: TVarRec;',
  10039. 'begin',
  10040. ' for i:=low(args) to high(args) do begin',
  10041. ' v:=args[i];',
  10042. ' case v.vtype of',
  10043. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10044. ' end;',
  10045. ' end;',
  10046. ' for v in args do ;',
  10047. ' args:=nil;',
  10048. ' SetLength(args,2);',
  10049. 'end;',
  10050. 'begin']);
  10051. ConvertProgram;
  10052. CheckSource('TestArrayOfConst_TVarRec',
  10053. LinesToStr([ // statements
  10054. 'this.Say = function (args) {',
  10055. ' var i = 0;',
  10056. ' var v = pas.system.TVarRec.$new();',
  10057. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10058. ' i = $l;',
  10059. ' v.$assign(args[i]);',
  10060. ' var $tmp = v.VType;',
  10061. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10062. ' };',
  10063. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10064. ' args = [];',
  10065. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10066. '};',
  10067. '']),
  10068. LinesToStr([ // $mod.$main
  10069. ]));
  10070. end;
  10071. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10072. begin
  10073. StartProgram(true,[supTVarRec]);
  10074. Add([
  10075. 'procedure Say(args: array of const);',
  10076. 'begin',
  10077. ' Say(args);',
  10078. 'end;',
  10079. 'var',
  10080. ' p: Pointer;',
  10081. ' j: jsvalue;',
  10082. ' c: currency;',
  10083. 'begin',
  10084. ' Say([]);',
  10085. ' Say([1]);',
  10086. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10087. '']);
  10088. ConvertProgram;
  10089. CheckSource('TestArrayOfConst_PassBaseTypes',
  10090. LinesToStr([ // statements
  10091. 'this.Say = function (args) {',
  10092. ' $mod.Say(args);',
  10093. '};',
  10094. 'this.p = null;',
  10095. 'this.j = undefined;',
  10096. 'this.c = 0;',
  10097. '']),
  10098. LinesToStr([ // $mod.$main
  10099. '$mod.Say([]);',
  10100. '$mod.Say(pas.system.VarRecs(0, 1));',
  10101. '$mod.Say(pas.system.VarRecs(',
  10102. ' 9,',
  10103. ' "c",',
  10104. ' 18,',
  10105. ' "foo",',
  10106. ' 5,',
  10107. ' null,',
  10108. ' 1,',
  10109. ' true,',
  10110. ' 3,',
  10111. ' 1.3,',
  10112. ' 5,',
  10113. ' $mod.p,',
  10114. ' 20,',
  10115. ' $mod.j,',
  10116. ' 12,',
  10117. ' $mod.c',
  10118. ' ));',
  10119. '']));
  10120. end;
  10121. procedure TTestModule.TestArrayOfConst_PassObj;
  10122. begin
  10123. StartProgram(true,[supTVarRec]);
  10124. Add([
  10125. '{$interfaces corba}',
  10126. 'type',
  10127. ' TObject = class',
  10128. ' end;',
  10129. ' TClass = class of TObject;',
  10130. ' IUnknown = interface',
  10131. ' end;',
  10132. 'procedure Say(args: array of const);',
  10133. 'begin',
  10134. 'end;',
  10135. 'var',
  10136. ' o: TObject;',
  10137. ' c: TClass;',
  10138. ' i: IUnknown;',
  10139. 'begin',
  10140. ' Say([o,c,TObject]);',
  10141. ' Say([nil,i]);',
  10142. '']);
  10143. ConvertProgram;
  10144. CheckSource('TestArrayOfConst_PassObj',
  10145. LinesToStr([ // statements
  10146. 'rtl.createClass($mod, "TObject", null, function () {',
  10147. ' this.$init = function () {',
  10148. ' };',
  10149. ' this.$final = function () {',
  10150. ' };',
  10151. '});',
  10152. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10153. 'this.Say = function (args) {',
  10154. '};',
  10155. 'this.o = null;',
  10156. 'this.c = null;',
  10157. 'this.i = null;',
  10158. '']),
  10159. LinesToStr([ // $mod.$main
  10160. '$mod.Say(pas.system.VarRecs(',
  10161. ' 7,',
  10162. ' $mod.o,',
  10163. ' 8,',
  10164. ' $mod.c,',
  10165. ' 8,',
  10166. ' $mod.TObject',
  10167. '));',
  10168. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10169. '']));
  10170. end;
  10171. procedure TTestModule.TestRecord_Empty;
  10172. begin
  10173. StartProgram(false);
  10174. Add([
  10175. 'type',
  10176. ' TRecA = record',
  10177. ' end;',
  10178. 'var a,b: TRecA;',
  10179. 'begin',
  10180. ' if a=b then ;']);
  10181. ConvertProgram;
  10182. CheckSource('TestRecord_Empty',
  10183. LinesToStr([ // statements
  10184. 'rtl.recNewT($mod, "TRecA", function () {',
  10185. ' this.$eq = function (b) {',
  10186. ' return true;',
  10187. ' };',
  10188. ' this.$assign = function (s) {',
  10189. ' return this;',
  10190. ' };',
  10191. '});',
  10192. 'this.a = $mod.TRecA.$new();',
  10193. 'this.b = $mod.TRecA.$new();',
  10194. '']),
  10195. LinesToStr([ // $mod.$main
  10196. 'if ($mod.a.$eq($mod.b)) ;'
  10197. ]));
  10198. end;
  10199. procedure TTestModule.TestRecord_Var;
  10200. begin
  10201. StartProgram(false);
  10202. Add('type');
  10203. Add(' TRecA = record');
  10204. Add(' Bold: longint;');
  10205. Add(' end;');
  10206. Add('var Rec: TRecA;');
  10207. Add('begin');
  10208. Add(' rec.bold:=123');
  10209. ConvertProgram;
  10210. CheckSource('TestRecord_Var',
  10211. LinesToStr([ // statements
  10212. 'rtl.recNewT($mod, "TRecA", function () {',
  10213. ' this.Bold = 0;',
  10214. ' this.$eq = function (b) {',
  10215. ' return this.Bold === b.Bold;',
  10216. ' };',
  10217. ' this.$assign = function (s) {',
  10218. ' this.Bold = s.Bold;',
  10219. ' return this;',
  10220. ' };',
  10221. '});',
  10222. 'this.Rec = $mod.TRecA.$new();',
  10223. '']),
  10224. LinesToStr([ // $mod.$main
  10225. '$mod.Rec.Bold = 123;'
  10226. ]));
  10227. end;
  10228. procedure TTestModule.TestRecord_VarExternal;
  10229. begin
  10230. StartProgram(false);
  10231. Add([
  10232. '{$modeswitch externalclass}',
  10233. 'type',
  10234. ' TRecA = record',
  10235. ' i: byte;',
  10236. ' length_: longint external name ''length'';',
  10237. ' end;',
  10238. 'var Rec: TRecA;',
  10239. 'begin',
  10240. ' rec.length_ := rec.length_',
  10241. '']);
  10242. ConvertProgram;
  10243. CheckSource('TestRecord_VarExternal',
  10244. LinesToStr([ // statements
  10245. 'rtl.recNewT($mod, "TRecA", function () {',
  10246. ' this.i = 0;',
  10247. ' this.$eq = function (b) {',
  10248. ' return (this.i === b.i) && (this.length === b.length);',
  10249. ' };',
  10250. ' this.$assign = function (s) {',
  10251. ' this.i = s.i;',
  10252. ' this.length = s.length;',
  10253. ' return this;',
  10254. ' };',
  10255. '});',
  10256. 'this.Rec = $mod.TRecA.$new();',
  10257. '']),
  10258. LinesToStr([ // $mod.$main
  10259. '$mod.Rec.length = $mod.Rec.length;'
  10260. ]));
  10261. end;
  10262. procedure TTestModule.TestRecord_WithDo;
  10263. begin
  10264. StartProgram(false);
  10265. Add('type');
  10266. Add(' TRec = record');
  10267. Add(' vI: longint;');
  10268. Add(' end;');
  10269. Add('var');
  10270. Add(' Int: longint;');
  10271. Add(' r: TRec;');
  10272. Add('begin');
  10273. Add(' with r do');
  10274. Add(' int:=vi;');
  10275. Add(' with r do begin');
  10276. Add(' int:=vi;');
  10277. Add(' vi:=int;');
  10278. Add(' end;');
  10279. ConvertProgram;
  10280. CheckSource('TestWithRecordDo',
  10281. LinesToStr([ // statements
  10282. 'rtl.recNewT($mod, "TRec", function () {',
  10283. ' this.vI = 0;',
  10284. ' this.$eq = function (b) {',
  10285. ' return this.vI === b.vI;',
  10286. ' };',
  10287. ' this.$assign = function (s) {',
  10288. ' this.vI = s.vI;',
  10289. ' return this;',
  10290. ' };',
  10291. '});',
  10292. 'this.Int = 0;',
  10293. 'this.r = $mod.TRec.$new();',
  10294. '']),
  10295. LinesToStr([ // $mod.$main
  10296. 'var $with = $mod.r;',
  10297. '$mod.Int = $with.vI;',
  10298. 'var $with1 = $mod.r;',
  10299. '$mod.Int = $with1.vI;',
  10300. '$with1.vI = $mod.Int;'
  10301. ]));
  10302. end;
  10303. procedure TTestModule.TestRecord_Assign;
  10304. begin
  10305. StartProgram(false);
  10306. Add('type');
  10307. Add(' TEnum = (red,green);');
  10308. Add(' TEnums = set of TEnum;');
  10309. Add(' TSmallRec = record');
  10310. Add(' N: longint;');
  10311. Add(' end;');
  10312. Add(' TBigRec = record');
  10313. Add(' Int: longint;');
  10314. Add(' D: double;');
  10315. Add(' Arr: array of longint;');
  10316. Add(' Arr2: array[1..2] of longint;');
  10317. Add(' Small: TSmallRec;');
  10318. Add(' Enums: TEnums;');
  10319. Add(' end;');
  10320. Add('var');
  10321. Add(' r, s: TBigRec;');
  10322. Add('begin');
  10323. Add(' r:=s;');
  10324. Add(' r:=default(TBigRec);');
  10325. Add(' r:=default(s);');
  10326. ConvertProgram;
  10327. CheckSource('TestRecord_Assign',
  10328. LinesToStr([ // statements
  10329. 'this.TEnum = {',
  10330. ' "0": "red",',
  10331. ' red: 0,',
  10332. ' "1": "green",',
  10333. ' green: 1',
  10334. '};',
  10335. 'rtl.recNewT($mod, "TSmallRec", function () {',
  10336. ' this.N = 0;',
  10337. ' this.$eq = function (b) {',
  10338. ' return this.N === b.N;',
  10339. ' };',
  10340. ' this.$assign = function (s) {',
  10341. ' this.N = s.N;',
  10342. ' return this;',
  10343. ' };',
  10344. '});',
  10345. 'rtl.recNewT($mod, "TBigRec", function () {',
  10346. ' this.Int = 0;',
  10347. ' this.D = 0.0;',
  10348. ' this.$new = function () {',
  10349. ' var r = Object.create(this);',
  10350. ' r.Arr = [];',
  10351. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10352. ' r.Small = $mod.TSmallRec.$new();',
  10353. ' r.Enums = {};',
  10354. ' return r;',
  10355. ' };',
  10356. ' this.$eq = function (b) {',
  10357. ' 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);',
  10358. ' };',
  10359. ' this.$assign = function (s) {',
  10360. ' this.Int = s.Int;',
  10361. ' this.D = s.D;',
  10362. ' this.Arr = rtl.arrayRef(s.Arr);',
  10363. ' this.Arr2 = s.Arr2.slice(0);',
  10364. ' this.Small.$assign(s.Small);',
  10365. ' this.Enums = rtl.refSet(s.Enums);',
  10366. ' return this;',
  10367. ' };',
  10368. '});',
  10369. 'this.r = $mod.TBigRec.$new();',
  10370. 'this.s = $mod.TBigRec.$new();',
  10371. '']),
  10372. LinesToStr([ // $mod.$main
  10373. '$mod.r.$assign($mod.s);',
  10374. '$mod.r.$assign($mod.TBigRec.$new());',
  10375. '$mod.r.$assign($mod.TBigRec.$new());',
  10376. '']));
  10377. end;
  10378. procedure TTestModule.TestRecord_AsParams;
  10379. begin
  10380. StartProgram(false);
  10381. Add([
  10382. 'type',
  10383. ' integer = longint;',
  10384. ' TRecord = record',
  10385. ' i: integer;',
  10386. ' end;',
  10387. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10388. 'var vL: TRecord;',
  10389. 'begin',
  10390. ' vd:=vd;',
  10391. ' vd.i:=vd.i;',
  10392. ' vl:=vc;',
  10393. ' vv:=vv;',
  10394. ' vv.i:=vv.i;',
  10395. ' U:=vl;',
  10396. ' U:=vd;',
  10397. ' U:=vc;',
  10398. ' U:=vv;',
  10399. ' vl:=TRecord(U);',
  10400. ' vd:=TRecord(U);',
  10401. ' vv:=TRecord(U);',
  10402. ' doit(vd,vd,vd,vd);',
  10403. ' doit(vc,vc,vl,vl);',
  10404. ' doit(vv,vv,vv,vv);',
  10405. ' doit(vl,vl,vl,vl);',
  10406. ' TRecord(U).i:=3;',
  10407. 'end;',
  10408. 'var i: TRecord;',
  10409. 'begin',
  10410. ' doit(i,i,i,i);',
  10411. '']);
  10412. ConvertProgram;
  10413. CheckSource('TestRecord_AsParams',
  10414. LinesToStr([ // statements
  10415. 'rtl.recNewT($mod, "TRecord", function () {',
  10416. ' this.i = 0;',
  10417. ' this.$eq = function (b) {',
  10418. ' return this.i === b.i;',
  10419. ' };',
  10420. ' this.$assign = function (s) {',
  10421. ' this.i = s.i;',
  10422. ' return this;',
  10423. ' };',
  10424. '});',
  10425. 'this.DoIt = function (vD, vC, vV, U) {',
  10426. ' var vL = $mod.TRecord.$new();',
  10427. ' vD.$assign(vD);',
  10428. ' vD.i = vD.i;',
  10429. ' vL.$assign(vC);',
  10430. ' vV.$assign(vV);',
  10431. ' vV.i = vV.i;',
  10432. ' U.$assign(vL);',
  10433. ' U.$assign(vD);',
  10434. ' U.$assign(vC);',
  10435. ' U.$assign(vV);',
  10436. ' vL.$assign(U);',
  10437. ' vD.$assign(U);',
  10438. ' vV.$assign(U);',
  10439. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10440. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10441. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10442. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10443. ' U.i = 3;',
  10444. '};',
  10445. 'this.i = $mod.TRecord.$new();'
  10446. ]),
  10447. LinesToStr([
  10448. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10449. '']));
  10450. end;
  10451. procedure TTestModule.TestRecord_ConstRef;
  10452. begin
  10453. StartProgram(false);
  10454. Add([
  10455. 'type TRec = record i: word; end;',
  10456. 'procedure Run(constref a: TRec);',
  10457. 'begin',
  10458. 'end;',
  10459. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10460. 'var l: TRec;',
  10461. 'begin',
  10462. ' Run(l);',
  10463. ' Run(a);',
  10464. ' Run(b);',
  10465. ' Run(c);',
  10466. ' Run(d);',
  10467. ' Run(e);',
  10468. 'end;',
  10469. 'begin',
  10470. '']);
  10471. ConvertProgram;
  10472. CheckResolverUnexpectedHints();
  10473. CheckSource('TestRecord_ConstRef',
  10474. LinesToStr([ // statements
  10475. 'rtl.recNewT($mod, "TRec", function () {',
  10476. ' this.i = 0;',
  10477. ' this.$eq = function (b) {',
  10478. ' return this.i === b.i;',
  10479. ' };',
  10480. ' this.$assign = function (s) {',
  10481. ' this.i = s.i;',
  10482. ' return this;',
  10483. ' };',
  10484. '});',
  10485. 'this.Run = function (a) {',
  10486. '};',
  10487. 'this.Fly = function (a, b, c, d, e) {',
  10488. ' var l = $mod.TRec.$new();',
  10489. ' $mod.Run(l);',
  10490. ' $mod.Run(a);',
  10491. ' $mod.Run(b);',
  10492. ' $mod.Run(c);',
  10493. ' $mod.Run(d);',
  10494. ' $mod.Run(e);',
  10495. '};',
  10496. '']),
  10497. LinesToStr([
  10498. '']));
  10499. end;
  10500. procedure TTestModule.TestRecordElement_AsParams;
  10501. begin
  10502. StartProgram(false);
  10503. Add('type');
  10504. Add(' integer = longint;');
  10505. Add(' TRecord = record');
  10506. Add(' i: integer;');
  10507. Add(' end;');
  10508. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10509. Add('var vJ: TRecord;');
  10510. Add('begin');
  10511. Add(' doit(vj.i,vj.i,vj.i);');
  10512. Add('end;');
  10513. Add('var r: TRecord;');
  10514. Add('begin');
  10515. Add(' doit(r.i,r.i,r.i);');
  10516. ConvertProgram;
  10517. CheckSource('TestRecordElement_AsParams',
  10518. LinesToStr([ // statements
  10519. 'rtl.recNewT($mod, "TRecord", function () {',
  10520. ' this.i = 0;',
  10521. ' this.$eq = function (b) {',
  10522. ' return this.i === b.i;',
  10523. ' };',
  10524. ' this.$assign = function (s) {',
  10525. ' this.i = s.i;',
  10526. ' return this;',
  10527. ' };',
  10528. '});',
  10529. 'this.DoIt = function (vG,vH,vI) {',
  10530. ' var vJ = $mod.TRecord.$new();',
  10531. ' $mod.DoIt(vJ.i, vJ.i, {',
  10532. ' p: vJ,',
  10533. ' get: function () {',
  10534. ' return this.p.i;',
  10535. ' },',
  10536. ' set: function (v) {',
  10537. ' this.p.i = v;',
  10538. ' }',
  10539. ' });',
  10540. '};',
  10541. 'this.r = $mod.TRecord.$new();'
  10542. ]),
  10543. LinesToStr([
  10544. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10545. ' p: $mod.r,',
  10546. ' get: function () {',
  10547. ' return this.p.i;',
  10548. ' },',
  10549. ' set: function (v) {',
  10550. ' this.p.i = v;',
  10551. ' }',
  10552. '});'
  10553. ]));
  10554. end;
  10555. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10556. begin
  10557. StartProgram(false);
  10558. Add('type');
  10559. Add(' integer = longint;');
  10560. Add(' TRecord = record');
  10561. Add(' i: integer;');
  10562. Add(' end;');
  10563. Add('function GetRec(vB: integer = 0): TRecord;');
  10564. Add('begin');
  10565. Add('end;');
  10566. Add('procedure DoIt(vG: integer; const vH: integer);');
  10567. Add('begin');
  10568. Add('end;');
  10569. Add('begin');
  10570. Add(' doit(getrec.i,getrec.i);');
  10571. Add(' doit(getrec().i,getrec().i);');
  10572. Add(' doit(getrec(1).i,getrec(2).i);');
  10573. ConvertProgram;
  10574. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10575. LinesToStr([ // statements
  10576. 'rtl.recNewT($mod, "TRecord", function () {',
  10577. ' this.i = 0;',
  10578. ' this.$eq = function (b) {',
  10579. ' return this.i === b.i;',
  10580. ' };',
  10581. ' this.$assign = function (s) {',
  10582. ' this.i = s.i;',
  10583. ' return this;',
  10584. ' };',
  10585. '});',
  10586. 'this.GetRec = function (vB) {',
  10587. ' var Result = $mod.TRecord.$new();',
  10588. ' return Result;',
  10589. '};',
  10590. 'this.DoIt = function (vG, vH) {',
  10591. '};',
  10592. '']),
  10593. LinesToStr([
  10594. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10595. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10596. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10597. '']));
  10598. end;
  10599. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10600. begin
  10601. StartProgram(false);
  10602. Add('type');
  10603. Add(' integer = longint;');
  10604. Add(' TRecord = record');
  10605. Add(' i: integer;');
  10606. Add(' end;');
  10607. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10608. Add('begin');
  10609. Add('end;');
  10610. Add('var r: trecord;');
  10611. Add('begin');
  10612. Add(' with r do ');
  10613. Add(' doit(i,i,i);');
  10614. ConvertProgram;
  10615. CheckSource('TestRecordElementFromWith_AsParams',
  10616. LinesToStr([ // statements
  10617. 'rtl.recNewT($mod, "TRecord", function () {',
  10618. ' this.i = 0;',
  10619. ' this.$eq = function (b) {',
  10620. ' return this.i === b.i;',
  10621. ' };',
  10622. ' this.$assign = function (s) {',
  10623. ' this.i = s.i;',
  10624. ' return this;',
  10625. ' };',
  10626. '});',
  10627. 'this.DoIt = function (vG,vH,vI) {',
  10628. '};',
  10629. 'this.r = $mod.TRecord.$new();'
  10630. ]),
  10631. LinesToStr([
  10632. 'var $with = $mod.r;',
  10633. '$mod.DoIt($with.i,$with.i,{',
  10634. ' p: $with,',
  10635. ' get: function () {',
  10636. ' return this.p.i;',
  10637. ' },',
  10638. ' set: function (v) {',
  10639. ' this.p.i = v;',
  10640. ' }',
  10641. '});',
  10642. '']));
  10643. end;
  10644. procedure TTestModule.TestRecord_Equal;
  10645. begin
  10646. StartProgram(false);
  10647. Add('type');
  10648. Add(' integer = longint;');
  10649. Add(' TFlag = (red,blue);');
  10650. Add(' TFlags = set of TFlag;');
  10651. Add(' TProc = procedure;');
  10652. Add(' TRecord = record');
  10653. Add(' i: integer;');
  10654. Add(' Event: TProc;');
  10655. Add(' f: TFlags;');
  10656. Add(' end;');
  10657. Add(' TNested = record');
  10658. Add(' r: TRecord;');
  10659. Add(' end;');
  10660. Add('var');
  10661. Add(' b: boolean;');
  10662. Add(' r,s: trecord;');
  10663. Add('begin');
  10664. Add(' b:=r=s;');
  10665. Add(' b:=r<>s;');
  10666. ConvertProgram;
  10667. CheckSource('TestRecord_Equal',
  10668. LinesToStr([ // statements
  10669. 'this.TFlag = {',
  10670. ' "0": "red",',
  10671. ' red: 0,',
  10672. ' "1": "blue",',
  10673. ' blue: 1',
  10674. '};',
  10675. 'rtl.recNewT($mod, "TRecord", function () {',
  10676. ' this.i = 0;',
  10677. ' this.Event = null;',
  10678. ' this.$new = function () {',
  10679. ' var r = Object.create(this);',
  10680. ' r.f = {};',
  10681. ' return r;',
  10682. ' };',
  10683. ' this.$eq = function (b) {',
  10684. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  10685. ' };',
  10686. ' this.$assign = function (s) {',
  10687. ' this.i = s.i;',
  10688. ' this.Event = s.Event;',
  10689. ' this.f = rtl.refSet(s.f);',
  10690. ' return this;',
  10691. ' };',
  10692. '});',
  10693. 'rtl.recNewT($mod, "TNested", function () {',
  10694. ' this.$new = function () {',
  10695. ' var r = Object.create(this);',
  10696. ' r.r = $mod.TRecord.$new();',
  10697. ' return r;',
  10698. ' };',
  10699. ' this.$eq = function (b) {',
  10700. ' return this.r.$eq(b.r);',
  10701. ' };',
  10702. ' this.$assign = function (s) {',
  10703. ' this.r.$assign(s.r);',
  10704. ' return this;',
  10705. ' };',
  10706. '});',
  10707. 'this.b = false;',
  10708. 'this.r = $mod.TRecord.$new();',
  10709. 'this.s = $mod.TRecord.$new();',
  10710. '']),
  10711. LinesToStr([
  10712. '$mod.b = $mod.r.$eq($mod.s);',
  10713. '$mod.b = !$mod.r.$eq($mod.s);',
  10714. '']));
  10715. end;
  10716. procedure TTestModule.TestRecord_JSValue;
  10717. begin
  10718. StartProgram(false);
  10719. Add([
  10720. 'type',
  10721. ' TRecord = record',
  10722. ' i: longint;',
  10723. ' end;',
  10724. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  10725. 'begin',
  10726. 'end;',
  10727. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  10728. 'begin',
  10729. ' if jsvalue(d) then ;',
  10730. ' if jsvalue(c) then ;',
  10731. ' if jsvalue(v) then ;',
  10732. 'end;',
  10733. 'var',
  10734. ' Jv: jsvalue;',
  10735. ' Rec: trecord;',
  10736. 'begin',
  10737. ' rec:=trecord(jv);',
  10738. ' jv:=rec;',
  10739. ' Fly(rec,rec);',
  10740. ' Fly(@rec,@rec);',
  10741. ' if jsvalue(Rec) then ;',
  10742. ' Run(trecord(jv),trecord(jv),rec);',
  10743. '']);
  10744. ConvertProgram;
  10745. CheckSource('TestRecord_JSValue',
  10746. LinesToStr([ // statements
  10747. 'rtl.recNewT($mod, "TRecord", function () {',
  10748. ' this.i = 0;',
  10749. ' this.$eq = function (b) {',
  10750. ' return this.i === b.i;',
  10751. ' };',
  10752. ' this.$assign = function (s) {',
  10753. ' this.i = s.i;',
  10754. ' return this;',
  10755. ' };',
  10756. '});',
  10757. 'this.Fly = function (d, c) {',
  10758. '};',
  10759. 'this.Run = function (d, c, v) {',
  10760. ' if (d) ;',
  10761. ' if (c) ;',
  10762. ' if (v) ;',
  10763. '};',
  10764. 'this.Jv = undefined;',
  10765. 'this.Rec = $mod.TRecord.$new();',
  10766. '']),
  10767. LinesToStr([
  10768. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  10769. '$mod.Jv = $mod.Rec;',
  10770. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  10771. '$mod.Fly($mod.Rec, $mod.Rec);',
  10772. 'if ($mod.Rec) ;',
  10773. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  10774. '']));
  10775. end;
  10776. procedure TTestModule.TestRecord_VariantFail;
  10777. begin
  10778. StartProgram(false);
  10779. Add([
  10780. 'type',
  10781. ' TRec = record',
  10782. ' case word of',
  10783. ' 0: (b0, b1: Byte);',
  10784. ' 1: (i: word);',
  10785. ' end;',
  10786. 'begin']);
  10787. SetExpectedPasResolverError('variant record is not supported',
  10788. nXIsNotSupported);
  10789. ConvertProgram;
  10790. end;
  10791. procedure TTestModule.TestRecord_FieldArray;
  10792. begin
  10793. StartProgram(false);
  10794. Add([
  10795. 'type',
  10796. ' TArrInt = array[3..4] of longint;',
  10797. ' TArrArrInt = array[3..4] of longint;',
  10798. ' TRec = record',
  10799. ' a: array of longint;',
  10800. ' s: array[1..2] of longint;',
  10801. ' m: array[1..2,3..4] of longint;',
  10802. ' o: TArrArrInt;',
  10803. ' end;',
  10804. 'begin']);
  10805. ConvertProgram;
  10806. CheckSource('TestRecord_FieldArray',
  10807. LinesToStr([ // statements
  10808. 'rtl.recNewT($mod, "TRec", function () {',
  10809. ' this.$new = function () {',
  10810. ' var r = Object.create(this);',
  10811. ' r.a = [];',
  10812. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10813. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10814. ' r.o = rtl.arraySetLength(null, 0, 2);',
  10815. ' return r;',
  10816. ' };',
  10817. ' this.$eq = function (b) {',
  10818. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  10819. ' };',
  10820. ' this.$assign = function (s) {',
  10821. ' this.a = rtl.arrayRef(s.a);',
  10822. ' this.s = s.s.slice(0);',
  10823. ' this.m = s.m.slice(0);',
  10824. ' this.o = s.o.slice(0);',
  10825. ' return this;',
  10826. ' };',
  10827. '});',
  10828. '']),
  10829. LinesToStr([ // $mod.$main
  10830. '']));
  10831. end;
  10832. procedure TTestModule.TestRecord_Const;
  10833. begin
  10834. StartProgram(false);
  10835. Add([
  10836. 'type',
  10837. ' TArrInt = array[3..4] of longint;',
  10838. ' TPoint = record x,y: longint; end;',
  10839. ' TRec = record',
  10840. ' i: longint;',
  10841. ' a: array of longint;',
  10842. ' s: array[1..2] of longint;',
  10843. ' m: array[1..2,3..4] of longint;',
  10844. ' p: TPoint;',
  10845. ' end;',
  10846. ' TPoints = array of TPoint;',
  10847. 'const',
  10848. ' r: TRec = (',
  10849. ' i:1;',
  10850. ' a:(2,3);',
  10851. ' s:(4,5);',
  10852. ' m:( (11,12), (13,14) );',
  10853. ' p: (x:21; y:22)',
  10854. ' );',
  10855. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  10856. 'begin']);
  10857. ConvertProgram;
  10858. CheckSource('TestRecord_Const',
  10859. LinesToStr([ // statements
  10860. 'rtl.recNewT($mod, "TPoint", function () {',
  10861. ' this.x = 0;',
  10862. ' this.y = 0;',
  10863. ' this.$eq = function (b) {',
  10864. ' return (this.x === b.x) && (this.y === b.y);',
  10865. ' };',
  10866. ' this.$assign = function (s) {',
  10867. ' this.x = s.x;',
  10868. ' this.y = s.y;',
  10869. ' return this;',
  10870. ' };',
  10871. '});',
  10872. 'rtl.recNewT($mod, "TRec", function () {',
  10873. ' this.i = 0;',
  10874. ' this.$new = function () {',
  10875. ' var r = Object.create(this);',
  10876. ' r.a = [];',
  10877. ' r.s = rtl.arraySetLength(null, 0, 2);',
  10878. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  10879. ' r.p = $mod.TPoint.$new();',
  10880. ' return r;',
  10881. ' };',
  10882. ' this.$eq = function (b) {',
  10883. ' 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);',
  10884. ' };',
  10885. ' this.$assign = function (s) {',
  10886. ' this.i = s.i;',
  10887. ' this.a = rtl.arrayRef(s.a);',
  10888. ' this.s = s.s.slice(0);',
  10889. ' this.m = s.m.slice(0);',
  10890. ' this.p.$assign(s.p);',
  10891. ' return this;',
  10892. ' };',
  10893. '});',
  10894. 'this.r = $mod.TRec.$clone({',
  10895. ' i: 1,',
  10896. ' a: [2, 3],',
  10897. ' s: [4, 5],',
  10898. ' m: [[11, 12], [13, 14]],',
  10899. ' p: $mod.TPoint.$clone({',
  10900. ' x: 21,',
  10901. ' y: 22',
  10902. ' })',
  10903. '});',
  10904. 'this.p = [$mod.TPoint.$clone({',
  10905. ' x: 1,',
  10906. ' y: 2',
  10907. '}), $mod.TPoint.$clone({',
  10908. ' x: 3,',
  10909. ' y: 4',
  10910. '})];',
  10911. '']),
  10912. LinesToStr([ // $mod.$main
  10913. '']));
  10914. end;
  10915. procedure TTestModule.TestRecord_TypecastFail;
  10916. begin
  10917. StartProgram(false);
  10918. Add([
  10919. 'type',
  10920. ' TPoint = record x,y: longint; end;',
  10921. ' TRec = record l: longint end;',
  10922. 'var p: TPoint;',
  10923. 'begin',
  10924. ' if TRec(p).l=2 then ;']);
  10925. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  10926. nIllegalTypeConversionTo);
  10927. ConvertProgram;
  10928. end;
  10929. procedure TTestModule.TestRecord_InFunction;
  10930. begin
  10931. StartProgram(false);
  10932. Add([
  10933. 'var TPoint: longint = 3;',
  10934. 'procedure DoIt;',
  10935. 'type',
  10936. ' TPoint = record x,y: longint; end;',
  10937. ' TPoints = array of TPoint;',
  10938. 'var',
  10939. ' r: TPoint;',
  10940. ' p: TPoints;',
  10941. 'begin',
  10942. ' SetLength(p,2);',
  10943. 'end;',
  10944. 'begin']);
  10945. ConvertProgram;
  10946. CheckSource('TestRecord_InFunction',
  10947. LinesToStr([ // statements
  10948. 'this.TPoint = 3;',
  10949. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  10950. ' this.x = 0;',
  10951. ' this.y = 0;',
  10952. ' this.$eq = function (b) {',
  10953. ' return (this.x === b.x) && (this.y === b.y);',
  10954. ' };',
  10955. ' this.$assign = function (s) {',
  10956. ' this.x = s.x;',
  10957. ' this.y = s.y;',
  10958. ' return this;',
  10959. ' };',
  10960. '});',
  10961. 'this.DoIt = function () {',
  10962. ' var r = TPoint$1.$new();',
  10963. ' var p = [];',
  10964. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  10965. '};',
  10966. '']),
  10967. LinesToStr([ // $mod.$main
  10968. '']));
  10969. end;
  10970. procedure TTestModule.TestRecord_AnonymousFail;
  10971. begin
  10972. StartProgram(false);
  10973. Add([
  10974. 'var',
  10975. ' r: record x: word end;',
  10976. 'begin']);
  10977. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  10978. nNotYetImplemented);
  10979. ConvertProgram;
  10980. end;
  10981. procedure TTestModule.TestAdvRecord_Function;
  10982. begin
  10983. StartProgram(false);
  10984. Parser.Options:=Parser.Options+[po_cassignments];
  10985. Add([
  10986. '{$modeswitch AdvancedRecords}',
  10987. 'type',
  10988. ' TPoint = record',
  10989. ' x,y: word;',
  10990. ' function Add(const apt: TPoint): TPoint;',
  10991. ' end;',
  10992. 'function TPoint.Add(const apt: TPoint): TPoint;',
  10993. 'begin',
  10994. ' Result:=Self;',
  10995. ' Result.x+=apt.x;',
  10996. ' Result.y:=Result.y+apt.y;',
  10997. ' Self:=apt;',
  10998. 'end;',
  10999. 'var p,q: TPoint;',
  11000. 'begin',
  11001. ' p.add(q);',
  11002. ' p:=default(TPoint);',
  11003. ' p:=q;',
  11004. '']);
  11005. ConvertProgram;
  11006. CheckSource('TestAdvRecord_Function',
  11007. LinesToStr([ // statements
  11008. 'rtl.recNewT($mod, "TPoint", function () {',
  11009. ' this.x = 0;',
  11010. ' this.y = 0;',
  11011. ' this.$eq = function (b) {',
  11012. ' return (this.x === b.x) && (this.y === b.y);',
  11013. ' };',
  11014. ' this.$assign = function (s) {',
  11015. ' this.x = s.x;',
  11016. ' this.y = s.y;',
  11017. ' return this;',
  11018. ' };',
  11019. ' this.Add = function (apt) {',
  11020. ' var Result = $mod.TPoint.$new();',
  11021. ' Result.$assign(this);',
  11022. ' Result.x += apt.x;',
  11023. ' Result.y = Result.y + apt.y;',
  11024. ' this.$assign(apt);',
  11025. ' return Result;',
  11026. ' };',
  11027. '});',
  11028. 'this.p = $mod.TPoint.$new();',
  11029. 'this.q = $mod.TPoint.$new();',
  11030. '']),
  11031. LinesToStr([ // $mod.$main
  11032. '$mod.p.Add($mod.q);',
  11033. '$mod.p.$assign($mod.TPoint.$new());',
  11034. '$mod.p.$assign($mod.q);',
  11035. '']));
  11036. end;
  11037. procedure TTestModule.TestAdvRecord_Property;
  11038. begin
  11039. StartProgram(false);
  11040. Add([
  11041. '{$modeswitch AdvancedRecords}',
  11042. 'type',
  11043. ' TPoint = record',
  11044. ' x,y: word;',
  11045. ' strict private',
  11046. ' function GetSize: longword;',
  11047. ' procedure SetSize(Value: longword);',
  11048. ' public',
  11049. ' property Size: longword read GetSize write SetSize;',
  11050. ' property Left: word read x write y;',
  11051. ' end;',
  11052. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11053. 'function TPoint.GetSize: longword;',
  11054. 'begin',
  11055. ' x:=y;',
  11056. ' Size:=Size;',
  11057. ' Left:=Left;',
  11058. 'end;',
  11059. 'procedure TPoint.SetSize(Value: longword);',
  11060. 'begin',
  11061. 'end;',
  11062. 'var p,q: TPoint;',
  11063. 'begin',
  11064. ' p.Size:=q.Size;',
  11065. ' p.Left:=q.Left;',
  11066. '']);
  11067. ConvertProgram;
  11068. CheckSource('TestAdvRecord_Property',
  11069. LinesToStr([ // statements
  11070. 'rtl.recNewT($mod, "TPoint", function () {',
  11071. ' this.x = 0;',
  11072. ' this.y = 0;',
  11073. ' this.$eq = function (b) {',
  11074. ' return (this.x === b.x) && (this.y === b.y);',
  11075. ' };',
  11076. ' this.$assign = function (s) {',
  11077. ' this.x = s.x;',
  11078. ' this.y = s.y;',
  11079. ' return this;',
  11080. ' };',
  11081. ' this.GetSize = function () {',
  11082. ' var Result = 0;',
  11083. ' this.x = this.y;',
  11084. ' this.SetSize(this.GetSize());',
  11085. ' this.y = this.x;',
  11086. ' return Result;',
  11087. ' };',
  11088. ' this.SetSize = function (Value) {',
  11089. ' };',
  11090. '});',
  11091. 'this.SetSize = function (Value) {',
  11092. '};',
  11093. 'this.p = $mod.TPoint.$new();',
  11094. 'this.q = $mod.TPoint.$new();',
  11095. '']),
  11096. LinesToStr([ // $mod.$main
  11097. '$mod.p.SetSize($mod.q.GetSize());',
  11098. '$mod.p.y = $mod.q.x;',
  11099. '']));
  11100. end;
  11101. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11102. begin
  11103. StartProgram(false);
  11104. Add([
  11105. '{$modeswitch AdvancedRecords}',
  11106. 'type',
  11107. ' TPoint = record',
  11108. ' strict private',
  11109. ' function GetItems(Index: word): word;',
  11110. ' procedure SetItems(Index: word; Value: word);',
  11111. ' public',
  11112. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11113. ' end;',
  11114. 'function TPoint.GetItems(Index: word): word;',
  11115. 'begin',
  11116. ' Items[index]:=Items[index];',
  11117. ' self.Items[index]:=self.Items[index];',
  11118. 'end;',
  11119. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11120. 'begin',
  11121. 'end;',
  11122. 'var p: TPoint;',
  11123. 'begin',
  11124. ' p[1]:=p[2];',
  11125. ' p.Items[3]:=p.Items[4];',
  11126. '']);
  11127. ConvertProgram;
  11128. CheckSource('TestAdvRecord_PropertyDefault',
  11129. LinesToStr([ // statements
  11130. 'rtl.recNewT($mod, "TPoint", function () {',
  11131. ' this.$eq = function (b) {',
  11132. ' return true;',
  11133. ' };',
  11134. ' this.$assign = function (s) {',
  11135. ' return this;',
  11136. ' };',
  11137. ' this.GetItems = function (Index) {',
  11138. ' var Result = 0;',
  11139. ' this.SetItems(Index, this.GetItems(Index));',
  11140. ' this.SetItems(Index, this.GetItems(Index));',
  11141. ' return Result;',
  11142. ' };',
  11143. ' this.SetItems = function (Index, Value) {',
  11144. ' };',
  11145. '});',
  11146. 'this.p = $mod.TPoint.$new();',
  11147. '']),
  11148. LinesToStr([ // $mod.$main
  11149. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11150. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11151. '']));
  11152. end;
  11153. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11154. begin
  11155. StartProgram(false);
  11156. Add([
  11157. '{$modeswitch AdvancedRecords}',
  11158. 'type',
  11159. ' TRec = record',
  11160. ' class var',
  11161. ' Fx: longint;',
  11162. ' Fy: longint;',
  11163. ' class function GetInt: longint; static;',
  11164. ' class procedure SetInt(Value: longint); static;',
  11165. ' class procedure DoIt; static;',
  11166. ' class property IntA: longint read Fx write Fy;',
  11167. ' class property IntB: longint read GetInt write SetInt;',
  11168. ' end;',
  11169. 'class function trec.getint: longint;',
  11170. 'begin',
  11171. ' result:=fx;',
  11172. 'end;',
  11173. 'class procedure trec.setint(value: longint);',
  11174. 'begin',
  11175. 'end;',
  11176. 'class procedure trec.doit;',
  11177. 'begin',
  11178. ' IntA:=IntA+1;',
  11179. ' IntB:=IntB+1;',
  11180. 'end;',
  11181. 'var r: trec;',
  11182. 'begin',
  11183. ' trec.inta:=trec.inta+1;',
  11184. ' if trec.intb=2 then;',
  11185. ' trec.intb:=trec.intb+2;',
  11186. ' trec.setint(trec.inta);',
  11187. ' r.inta:=r.inta+1;',
  11188. ' if r.intb=2 then;',
  11189. ' r.intb:=r.intb+2;',
  11190. ' r.setint(r.inta);']);
  11191. ConvertProgram;
  11192. CheckSource('TestAdvRecord_Property_ClassMethod',
  11193. LinesToStr([ // statements
  11194. 'rtl.recNewT($mod, "TRec", function () {',
  11195. ' this.Fx = 0;',
  11196. ' this.Fy = 0;',
  11197. ' this.$eq = function (b) {',
  11198. ' return true;',
  11199. ' };',
  11200. ' this.$assign = function (s) {',
  11201. ' return this;',
  11202. ' };',
  11203. ' this.GetInt = function () {',
  11204. ' var Result = 0;',
  11205. ' Result = $mod.TRec.Fx;',
  11206. ' return Result;',
  11207. ' };',
  11208. ' this.SetInt = function (Value) {',
  11209. ' };',
  11210. ' this.DoIt = function () {',
  11211. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11212. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11213. ' };',
  11214. '}, true);',
  11215. 'this.r = $mod.TRec.$new();',
  11216. '']),
  11217. LinesToStr([ // $mod.$main
  11218. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11219. 'if ($mod.TRec.GetInt() === 2) ;',
  11220. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11221. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11222. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11223. 'if ($mod.r.GetInt() === 2) ;',
  11224. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  11225. '$mod.r.SetInt($mod.r.Fx);',
  11226. '']));
  11227. end;
  11228. procedure TTestModule.TestAdvRecord_Const;
  11229. begin
  11230. StartProgram(false);
  11231. Add([
  11232. '{$modeswitch AdvancedRecords}',
  11233. 'type',
  11234. ' TArrInt = array[3..4] of longint;',
  11235. ' TPoint = record',
  11236. ' x,y: longint;',
  11237. ' class var Count: nativeint;',
  11238. ' end;',
  11239. ' TRec = record',
  11240. ' i: longint;',
  11241. ' a: array of longint;',
  11242. ' s: array[1..2] of longint;',
  11243. ' m: array[1..2,3..4] of longint;',
  11244. ' p: TPoint;',
  11245. ' end;',
  11246. ' TPoints = array of TPoint;',
  11247. 'const',
  11248. ' r: TRec = (',
  11249. ' i:1;',
  11250. ' a:(2,3);',
  11251. ' s:(4,5);',
  11252. ' m:( (11,12), (13,14) );',
  11253. ' p: (x:21)',
  11254. ' );',
  11255. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11256. 'begin']);
  11257. ConvertProgram;
  11258. CheckSource('TestAdvRecord_Const',
  11259. LinesToStr([ // statements
  11260. 'rtl.recNewT($mod, "TPoint", function () {',
  11261. ' this.x = 0;',
  11262. ' this.y = 0;',
  11263. ' this.Count = 0;',
  11264. ' this.$eq = function (b) {',
  11265. ' return (this.x === b.x) && (this.y === b.y);',
  11266. ' };',
  11267. ' this.$assign = function (s) {',
  11268. ' this.x = s.x;',
  11269. ' this.y = s.y;',
  11270. ' return this;',
  11271. ' };',
  11272. '}, true);',
  11273. 'rtl.recNewT($mod, "TRec", function () {',
  11274. ' this.i = 0;',
  11275. ' this.$new = function () {',
  11276. ' var r = Object.create(this);',
  11277. ' r.a = [];',
  11278. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11279. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11280. ' r.p = $mod.TPoint.$new();',
  11281. ' return r;',
  11282. ' };',
  11283. ' this.$eq = function (b) {',
  11284. ' 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);',
  11285. ' };',
  11286. ' this.$assign = function (s) {',
  11287. ' this.i = s.i;',
  11288. ' this.a = rtl.arrayRef(s.a);',
  11289. ' this.s = s.s.slice(0);',
  11290. ' this.m = s.m.slice(0);',
  11291. ' this.p.$assign(s.p);',
  11292. ' return this;',
  11293. ' };',
  11294. '});',
  11295. 'this.r = $mod.TRec.$clone({',
  11296. ' i: 1,',
  11297. ' a: [2, 3],',
  11298. ' s: [4, 5],',
  11299. ' m: [[11, 12], [13, 14]],',
  11300. ' p: $mod.TPoint.$clone({',
  11301. ' x: 21,',
  11302. ' y: 0',
  11303. ' })',
  11304. '});',
  11305. 'this.p = [$mod.TPoint.$clone({',
  11306. ' x: 1,',
  11307. ' y: 2',
  11308. '}), $mod.TPoint.$clone({',
  11309. ' x: 3,',
  11310. ' y: 4',
  11311. '})];',
  11312. '']),
  11313. LinesToStr([ // $mod.$main
  11314. '']));
  11315. end;
  11316. procedure TTestModule.TestAdvRecord_ExternalField;
  11317. begin
  11318. StartProgram(false);
  11319. Add([
  11320. '{$modeswitch AdvancedRecords}',
  11321. '{$modeswitch externalclass}',
  11322. 'type',
  11323. ' TCar = record',
  11324. ' public',
  11325. ' Intern: longint external name ''$Intern'';',
  11326. ' Intern2: longint external name ''$Intern2'';',
  11327. ' Bracket: longint external name ''["A B"]'';',
  11328. ' procedure DoIt;',
  11329. ' end;',
  11330. 'procedure tcar.doit;',
  11331. 'begin',
  11332. ' Intern:=Intern+1;',
  11333. ' Intern2:=Intern2+2;',
  11334. ' Bracket:=Bracket+3;',
  11335. 'end;',
  11336. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11337. 'begin',
  11338. ' Rec.intern:=Rec.intern+1;',
  11339. ' Rec.intern2:=Rec.intern2+2;',
  11340. ' Rec.Bracket:=Rec.Bracket+3;',
  11341. ' with Rec do begin',
  11342. ' intern:=intern+1;',
  11343. ' intern2:=intern2+2;',
  11344. ' Bracket:=Bracket+3;',
  11345. ' end;']);
  11346. ConvertProgram;
  11347. CheckSource('TestAdvRecord_ExternalField',
  11348. LinesToStr([ // statements
  11349. 'rtl.recNewT($mod, "TCar", function () {',
  11350. ' this.$eq = function (b) {',
  11351. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11352. ' };',
  11353. ' this.$assign = function (s) {',
  11354. ' this.$Intern = s.$Intern;',
  11355. ' this.$Intern2 = s.$Intern2;',
  11356. ' this["A B"] = s["A B"];',
  11357. ' return this;',
  11358. ' };',
  11359. ' this.DoIt = function () {',
  11360. ' this.$Intern = this.$Intern + 1;',
  11361. ' this.$Intern2 = this.$Intern2 + 2;',
  11362. ' this["A B"] = this["A B"] + 3;',
  11363. ' };',
  11364. '});',
  11365. 'this.Rec = $mod.TCar.$clone({',
  11366. ' $Intern: 11,',
  11367. ' $Intern2: 12,',
  11368. ' "A B": 13',
  11369. '});',
  11370. '']),
  11371. LinesToStr([ // $mod.$main
  11372. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11373. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11374. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11375. 'var $with = $mod.Rec;',
  11376. '$with.$Intern = $with.$Intern + 1;',
  11377. '$with.$Intern2 = $with.$Intern2 + 2;',
  11378. '$with["A B"] = $with["A B"] + 3;',
  11379. '']));
  11380. end;
  11381. procedure TTestModule.TestAdvRecord_SubRecord;
  11382. begin
  11383. StartProgram(false);
  11384. Add([
  11385. '{$modeswitch AdvancedRecords}',
  11386. 'type',
  11387. ' TRec = record',
  11388. ' type',
  11389. ' TPoint = record',
  11390. ' x,y: longint;',
  11391. ' class var Count: nativeint;',
  11392. ' procedure DoIt;',
  11393. ' class procedure DoThat; static;',
  11394. ' end;',
  11395. ' var',
  11396. ' i: longint;',
  11397. ' p: TPoint;',
  11398. ' procedure DoSome;',
  11399. ' end;',
  11400. 'const',
  11401. ' r: TRec = (',
  11402. ' i:1;',
  11403. ' p: (x:21;y:22)',
  11404. ' );',
  11405. 'procedure TRec.DoSome;',
  11406. 'begin',
  11407. ' p.x:=p.y+1;',
  11408. ' p.Count:=p.Count+2;',
  11409. 'end;',
  11410. 'procedure TRec.TPoint.DoIt;',
  11411. 'begin',
  11412. ' Count:=Count+3;',
  11413. 'end;',
  11414. 'class procedure TRec.TPoint.DoThat;',
  11415. 'begin',
  11416. ' Count:=Count+4;',
  11417. 'end;',
  11418. 'begin']);
  11419. ConvertProgram;
  11420. CheckSource('TestAdvRecord_SubRecord',
  11421. LinesToStr([ // statements
  11422. 'rtl.recNewT($mod, "TRec", function () {',
  11423. ' rtl.recNewT(this, "TPoint", function () {',
  11424. ' this.x = 0;',
  11425. ' this.y = 0;',
  11426. ' this.Count = 0;',
  11427. ' this.$eq = function (b) {',
  11428. ' return (this.x === b.x) && (this.y === b.y);',
  11429. ' };',
  11430. ' this.$assign = function (s) {',
  11431. ' this.x = s.x;',
  11432. ' this.y = s.y;',
  11433. ' return this;',
  11434. ' };',
  11435. ' this.DoIt = function () {',
  11436. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11437. ' };',
  11438. ' this.DoThat = function () {',
  11439. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11440. ' };',
  11441. ' }, true);',
  11442. ' this.i = 0;',
  11443. ' this.$new = function () {',
  11444. ' var r = Object.create(this);',
  11445. ' r.p = this.TPoint.$new();',
  11446. ' return r;',
  11447. ' };',
  11448. ' this.$eq = function (b) {',
  11449. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11450. ' };',
  11451. ' this.$assign = function (s) {',
  11452. ' this.i = s.i;',
  11453. ' this.p.$assign(s.p);',
  11454. ' return this;',
  11455. ' };',
  11456. ' this.DoSome = function () {',
  11457. ' this.p.x = this.p.y + 1;',
  11458. ' this.TPoint.Count = this.p.Count + 2;',
  11459. ' };',
  11460. '}, true);',
  11461. 'this.r = $mod.TRec.$clone({',
  11462. ' i: 1,',
  11463. ' p: $mod.TRec.TPoint.$clone({',
  11464. ' x: 21,',
  11465. ' y: 22',
  11466. ' })',
  11467. '});',
  11468. '']),
  11469. LinesToStr([ // $mod.$main
  11470. '']));
  11471. end;
  11472. procedure TTestModule.TestAdvRecord_SubClass;
  11473. begin
  11474. StartProgram(false);
  11475. Add([
  11476. '{$modeswitch AdvancedRecords}',
  11477. 'type',
  11478. ' TObject = class end;',
  11479. ' TPoint = record',
  11480. ' type',
  11481. ' TBird = class',
  11482. ' procedure DoIt;',
  11483. ' class procedure Glob;',
  11484. ' end;',
  11485. ' procedure DoIt(b: TBird);',
  11486. ' end;',
  11487. 'procedure TPoint.TBird.DoIt;',
  11488. 'begin',
  11489. ' doit;',
  11490. ' self.doit;',
  11491. ' glob;',
  11492. ' self.glob;',
  11493. 'end;',
  11494. 'class procedure TPoint.TBird.Glob;',
  11495. 'begin',
  11496. ' glob;',
  11497. ' self.glob;',
  11498. 'end;',
  11499. 'procedure TPoint.DoIt(b: TBird);',
  11500. 'begin',
  11501. ' b.doit;',
  11502. ' b.glob;',
  11503. ' TBird.glob;',
  11504. 'end;',
  11505. 'begin',
  11506. '']);
  11507. ConvertProgram;
  11508. CheckSource('TestAdvRecord_SubClass',
  11509. LinesToStr([ // statements
  11510. 'rtl.createClass($mod, "TObject", null, function () {',
  11511. ' this.$init = function () {',
  11512. ' };',
  11513. ' this.$final = function () {',
  11514. ' };',
  11515. '});',
  11516. 'rtl.recNewT($mod, "TPoint", function () {',
  11517. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11518. ' this.DoIt = function () {',
  11519. ' this.DoIt();',
  11520. ' this.DoIt();',
  11521. ' this.$class.Glob();',
  11522. ' this.$class.Glob();',
  11523. ' };',
  11524. ' this.Glob = function () {',
  11525. ' this.Glob();',
  11526. ' this.Glob();',
  11527. ' };',
  11528. ' });',
  11529. ' this.$eq = function (b) {',
  11530. ' return true;',
  11531. ' };',
  11532. ' this.$assign = function (s) {',
  11533. ' return this;',
  11534. ' };',
  11535. ' this.DoIt = function (b) {',
  11536. ' b.DoIt();',
  11537. ' b.$class.Glob();',
  11538. ' this.TBird.Glob();',
  11539. ' };',
  11540. '}, true);',
  11541. '']),
  11542. LinesToStr([ // $mod.$main
  11543. '']));
  11544. end;
  11545. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11546. begin
  11547. StartProgram(false);
  11548. Add([
  11549. '{$modeswitch AdvancedRecords}',
  11550. 'type',
  11551. ' IUnknown = interface end;',
  11552. ' TPoint = record',
  11553. ' type IBird = interface end;',
  11554. ' end;',
  11555. 'begin',
  11556. '']);
  11557. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11558. nNotYetImplemented);
  11559. ParseProgram;
  11560. end;
  11561. procedure TTestModule.TestAdvRecord_Constructor;
  11562. begin
  11563. StartProgram(false);
  11564. Add([
  11565. '{$modeswitch AdvancedRecords}',
  11566. 'type',
  11567. ' TPoint = record',
  11568. ' x,y: longint;',
  11569. ' constructor Create(ax: longint; ay: longint = -1);',
  11570. ' end;',
  11571. 'constructor tpoint.create(ax,ay: longint);',
  11572. 'begin',
  11573. ' x:=ax;',
  11574. ' self.y:=ay;',
  11575. 'end;',
  11576. 'var r: TPoint;',
  11577. 'begin',
  11578. ' r:=TPoint.Create(1,2);',
  11579. ' with TPoint do r:=Create(1,2);',
  11580. ' r.Create(3);',
  11581. ' r:=r.Create(4);',
  11582. '']);
  11583. ConvertProgram;
  11584. CheckSource('TestAdvRecord_Constructor',
  11585. LinesToStr([ // statements
  11586. 'rtl.recNewT($mod, "TPoint", function () {',
  11587. ' this.x = 0;',
  11588. ' this.y = 0;',
  11589. ' this.$eq = function (b) {',
  11590. ' return (this.x === b.x) && (this.y === b.y);',
  11591. ' };',
  11592. ' this.$assign = function (s) {',
  11593. ' this.x = s.x;',
  11594. ' this.y = s.y;',
  11595. ' return this;',
  11596. ' };',
  11597. ' this.Create = function (ax, ay) {',
  11598. ' this.x = ax;',
  11599. ' this.y = ay;',
  11600. ' return this;',
  11601. ' };',
  11602. '}, true);',
  11603. 'this.r = $mod.TPoint.$new();',
  11604. '']),
  11605. LinesToStr([ // $mod.$main
  11606. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11607. 'var $with = $mod.TPoint;',
  11608. '$mod.r.$assign($with.$new().Create(1, 2));',
  11609. '$mod.r.Create(3, -1);',
  11610. '$mod.r.$assign($mod.r.Create(4, -1));',
  11611. '']));
  11612. end;
  11613. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11614. begin
  11615. StartProgram(false);
  11616. Add([
  11617. '{$modeswitch AdvancedRecords}',
  11618. 'type',
  11619. ' TPoint = record',
  11620. ' class var x: longint;',
  11621. ' class procedure Fly; static;',
  11622. ' class constructor Init;',
  11623. ' end;',
  11624. 'var count: word;',
  11625. 'class procedure Tpoint.Fly;',
  11626. 'begin',
  11627. 'end;',
  11628. 'class constructor tpoint.init;',
  11629. 'begin',
  11630. ' count:=count+1;',
  11631. ' x:=x+3;',
  11632. ' tpoint.x:=tpoint.x+4;',
  11633. ' fly;',
  11634. ' tpoint.fly;',
  11635. 'end;',
  11636. 'var r: TPoint;',
  11637. 'begin',
  11638. ' r.x:=r.x+10;',
  11639. ' r.Fly;',
  11640. ' r.Fly();',
  11641. '']);
  11642. ConvertProgram;
  11643. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11644. LinesToStr([ // statements
  11645. 'rtl.recNewT($mod, "TPoint", function () {',
  11646. ' this.x = 0;',
  11647. ' this.$eq = function (b) {',
  11648. ' return true;',
  11649. ' };',
  11650. ' this.$assign = function (s) {',
  11651. ' return this;',
  11652. ' };',
  11653. ' this.Fly = function () {',
  11654. ' };',
  11655. '}, true);',
  11656. 'this.count = 0;',
  11657. 'this.r = $mod.TPoint.$new();',
  11658. '']),
  11659. LinesToStr([ // $mod.$main
  11660. '(function () {',
  11661. ' $mod.count = $mod.count + 1;',
  11662. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11663. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11664. ' $mod.TPoint.Fly();',
  11665. ' $mod.TPoint.Fly();',
  11666. '})();',
  11667. '$mod.TPoint.x = $mod.r.x + 10;',
  11668. '$mod.r.Fly();',
  11669. '$mod.r.Fly();',
  11670. '']));
  11671. end;
  11672. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  11673. begin
  11674. StartUnit(false);
  11675. Add([
  11676. 'interface',
  11677. '{$modeswitch AdvancedRecords}',
  11678. 'type',
  11679. ' TPoint = record',
  11680. ' class var x: longint;',
  11681. ' class procedure Fly; static;',
  11682. ' class constructor Init;',
  11683. ' end;',
  11684. 'implementation',
  11685. 'var count: word;',
  11686. 'class procedure Tpoint.Fly;',
  11687. 'begin',
  11688. 'end;',
  11689. 'class constructor tpoint.init;',
  11690. 'begin',
  11691. ' count:=count+1;',
  11692. ' x:=3;',
  11693. ' tpoint.x:=4;',
  11694. ' fly;',
  11695. ' tpoint.fly;',
  11696. 'end;',
  11697. '']);
  11698. ConvertUnit;
  11699. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  11700. LinesToStr([ // statements
  11701. 'var $impl = $mod.$impl;',
  11702. 'rtl.recNewT($mod, "TPoint", function () {',
  11703. ' this.x = 0;',
  11704. ' this.$eq = function (b) {',
  11705. ' return true;',
  11706. ' };',
  11707. ' this.$assign = function (s) {',
  11708. ' return this;',
  11709. ' };',
  11710. ' this.Fly = function () {',
  11711. ' };',
  11712. '}, true);',
  11713. '']),
  11714. LinesToStr([ // $mod.$init
  11715. '(function () {',
  11716. ' $impl.count = $impl.count + 1;',
  11717. ' $mod.TPoint.x = 3;',
  11718. ' $mod.TPoint.x = 4;',
  11719. ' $mod.TPoint.Fly();',
  11720. ' $mod.TPoint.Fly();',
  11721. '})();',
  11722. '']),
  11723. LinesToStr([ // $mod.$main
  11724. '$impl.count = 0;',
  11725. '']));
  11726. end;
  11727. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  11728. begin
  11729. StartProgram(false);
  11730. Add(['type',
  11731. ' TObject = class',
  11732. ' public',
  11733. ' constructor Create;',
  11734. ' destructor Destroy;',
  11735. ' end;',
  11736. ' TBird = TObject;',
  11737. 'constructor tobject.create;',
  11738. 'begin end;',
  11739. 'destructor tobject.destroy;',
  11740. 'begin end;',
  11741. 'var Obj: tobject;',
  11742. 'begin',
  11743. ' obj:=tobject.create;',
  11744. ' obj:=tobject.create();',
  11745. ' obj:=tbird.create;',
  11746. ' obj:=tbird.create();',
  11747. ' obj:=obj.create();',
  11748. ' obj.destroy;',
  11749. '']);
  11750. ConvertProgram;
  11751. CheckSource('TestClass_TObjectDefaultConstructor',
  11752. LinesToStr([ // statements
  11753. 'rtl.createClass($mod,"TObject",null,function(){',
  11754. ' this.$init = function () {',
  11755. ' };',
  11756. ' this.$final = function () {',
  11757. ' };',
  11758. ' this.Create = function(){',
  11759. ' return this;',
  11760. ' };',
  11761. ' this.Destroy = function(){',
  11762. ' };',
  11763. '});',
  11764. 'this.Obj = null;'
  11765. ]),
  11766. LinesToStr([ // $mod.$main
  11767. '$mod.Obj = $mod.TObject.$create("Create");',
  11768. '$mod.Obj = $mod.TObject.$create("Create");',
  11769. '$mod.Obj = $mod.TObject.$create("Create");',
  11770. '$mod.Obj = $mod.TObject.$create("Create");',
  11771. '$mod.Obj = $mod.Obj.Create();',
  11772. '$mod.Obj.$destroy("Destroy");',
  11773. '']));
  11774. end;
  11775. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  11776. begin
  11777. StartProgram(false);
  11778. Add('type');
  11779. Add(' TObject = class');
  11780. Add(' public');
  11781. Add(' constructor Create(Par: longint);');
  11782. Add(' end;');
  11783. Add('constructor tobject.create(par: longint);');
  11784. Add('begin end;');
  11785. Add('var Obj: tobject;');
  11786. Add('begin');
  11787. Add(' obj:=tobject.create(3);');
  11788. ConvertProgram;
  11789. CheckSource('TestClass_TObjectConstructorWithParams',
  11790. LinesToStr([ // statements
  11791. 'rtl.createClass($mod,"TObject",null,function(){',
  11792. ' this.$init = function () {',
  11793. ' };',
  11794. ' this.$final = function () {',
  11795. ' };',
  11796. ' this.Create = function(Par){',
  11797. ' return this;',
  11798. ' };',
  11799. '});',
  11800. 'this.Obj = null;'
  11801. ]),
  11802. LinesToStr([ // $mod.$main
  11803. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  11804. ]));
  11805. end;
  11806. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  11807. begin
  11808. StartProgram(false);
  11809. Add('type');
  11810. Add(' TObject = class');
  11811. Add(' public');
  11812. Add(' constructor Create;');
  11813. Add(' end;');
  11814. Add(' TTest = class(TObject)');
  11815. Add(' public');
  11816. Add(' constructor Create(const Par: longint = 1);');
  11817. Add(' end;');
  11818. Add('constructor tobject.create;');
  11819. Add('begin end;');
  11820. Add('constructor ttest.create(const par: longint);');
  11821. Add('begin end;');
  11822. Add('var t: ttest;');
  11823. Add('begin');
  11824. Add(' t:=ttest.create;');
  11825. Add(' t:=ttest.create(2);');
  11826. ConvertProgram;
  11827. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  11828. LinesToStr([ // statements
  11829. 'rtl.createClass($mod,"TObject",null,function(){',
  11830. ' this.$init = function () {',
  11831. ' };',
  11832. ' this.$final = function () {',
  11833. ' };',
  11834. ' this.Create = function(){',
  11835. ' return this;',
  11836. ' };',
  11837. '});',
  11838. 'rtl.createClass($mod, "TTest", $mod.TObject, function () {',
  11839. ' this.Create$1 = function (Par) {',
  11840. ' return this;',
  11841. ' };',
  11842. '});',
  11843. 'this.t = null;'
  11844. ]),
  11845. LinesToStr([ // $mod.$main
  11846. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  11847. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  11848. ]));
  11849. end;
  11850. procedure TTestModule.TestClass_Var;
  11851. begin
  11852. StartProgram(false);
  11853. Add([
  11854. 'type',
  11855. ' TObject = class',
  11856. ' public',
  11857. ' vI: longint;',
  11858. ' constructor Create(Par: longint);',
  11859. ' end;',
  11860. 'constructor tobject.create(par: longint);',
  11861. 'begin',
  11862. ' vi:=par+3',
  11863. 'end;',
  11864. 'var Obj: tobject;',
  11865. 'begin',
  11866. ' obj:=tobject.create(4);',
  11867. ' obj.vi:=obj.VI+5;']);
  11868. ConvertProgram;
  11869. CheckSource('TestClass_Var',
  11870. LinesToStr([ // statements
  11871. 'rtl.createClass($mod,"TObject",null,function(){',
  11872. ' this.$init = function () {',
  11873. ' this.vI = 0;',
  11874. ' };',
  11875. ' this.$final = function () {',
  11876. ' };',
  11877. ' this.Create = function(Par){',
  11878. ' this.vI = Par+3;',
  11879. ' return this;',
  11880. ' };',
  11881. '});',
  11882. 'this.Obj = null;'
  11883. ]),
  11884. LinesToStr([ // $mod.$main
  11885. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  11886. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  11887. ]));
  11888. end;
  11889. procedure TTestModule.TestClass_Method;
  11890. begin
  11891. StartProgram(false);
  11892. Add('type');
  11893. Add(' TObject = class');
  11894. Add(' public');
  11895. Add(' vI: longint;');
  11896. Add(' Sub: TObject;');
  11897. Add(' constructor Create;');
  11898. Add(' function GetIt(Par: longint): tobject;');
  11899. Add(' end;');
  11900. Add('constructor tobject.create; begin end;');
  11901. Add('function tobject.getit(par: longint): tobject;');
  11902. Add('begin');
  11903. Add(' Self.vi:=par+3;');
  11904. Add(' Result:=self.sub;');
  11905. Add('end;');
  11906. Add('var Obj: tobject;');
  11907. Add('begin');
  11908. Add(' obj:=tobject.create;');
  11909. Add(' obj.getit(4);');
  11910. Add(' obj.sub.sub:=nil;');
  11911. Add(' obj.sub.getit(5);');
  11912. Add(' obj.sub.getit(6).SUB:=nil;');
  11913. Add(' obj.sub.getit(7).GETIT(8);');
  11914. Add(' obj.sub.getit(9).SuB.getit(10);');
  11915. ConvertProgram;
  11916. CheckSource('TestClass_Method',
  11917. LinesToStr([ // statements
  11918. 'rtl.createClass($mod,"TObject",null,function(){',
  11919. ' this.$init = function () {',
  11920. ' this.vI = 0;',
  11921. ' this.Sub = null;',
  11922. ' };',
  11923. ' this.$final = function () {',
  11924. ' this.Sub = undefined;',
  11925. ' };',
  11926. ' this.Create = function(){',
  11927. ' return this;',
  11928. ' };',
  11929. ' this.GetIt = function(Par){',
  11930. ' var Result = null;',
  11931. ' this.vI = Par + 3;',
  11932. ' Result = this.Sub;',
  11933. ' return Result;',
  11934. ' };',
  11935. '});',
  11936. 'this.Obj = null;'
  11937. ]),
  11938. LinesToStr([ // $mod.$main
  11939. '$mod.Obj = $mod.TObject.$create("Create");',
  11940. '$mod.Obj.GetIt(4);',
  11941. '$mod.Obj.Sub.Sub=null;',
  11942. '$mod.Obj.Sub.GetIt(5);',
  11943. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  11944. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  11945. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  11946. ]));
  11947. end;
  11948. procedure TTestModule.TestClass_Implementation;
  11949. begin
  11950. StartUnit(false);
  11951. Add([
  11952. 'interface',
  11953. 'type',
  11954. ' TObject = class',
  11955. ' constructor Create;',
  11956. ' end;',
  11957. 'implementation',
  11958. 'type',
  11959. ' TIntClass = class',
  11960. ' constructor Create; reintroduce;',
  11961. ' class procedure DoGlob;',
  11962. ' end;',
  11963. 'constructor tintclass.create;',
  11964. 'begin',
  11965. ' inherited;',
  11966. ' inherited create;',
  11967. ' doglob;',
  11968. 'end;',
  11969. 'class procedure tintclass.doglob;',
  11970. 'begin',
  11971. 'end;',
  11972. 'constructor tobject.create;',
  11973. 'var',
  11974. ' iC: tintclass;',
  11975. 'begin',
  11976. ' ic:=tintclass.create;',
  11977. ' tintclass.doglob;',
  11978. ' ic.doglob;',
  11979. 'end;',
  11980. 'initialization',
  11981. ' tintclass.doglob;',
  11982. '']);
  11983. ConvertUnit;
  11984. CheckSource('TestClass_Implementation',
  11985. LinesToStr([ // statements
  11986. 'var $impl = $mod.$impl;',
  11987. 'rtl.createClass($mod, "TObject", null, function () {',
  11988. ' this.$init = function () {',
  11989. ' };',
  11990. ' this.$final = function () {',
  11991. ' };',
  11992. ' this.Create = function () {',
  11993. ' var iC = null;',
  11994. ' iC = $impl.TIntClass.$create("Create$1");',
  11995. ' $impl.TIntClass.DoGlob();',
  11996. ' iC.$class.DoGlob();',
  11997. ' return this;',
  11998. ' };',
  11999. '});',
  12000. '']),
  12001. LinesToStr([ // $mod.$main
  12002. '$impl.TIntClass.DoGlob();',
  12003. '']),
  12004. LinesToStr([
  12005. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12006. ' this.Create$1 = function () {',
  12007. ' $mod.TObject.Create.call(this);',
  12008. ' $mod.TObject.Create.call(this);',
  12009. ' this.$class.DoGlob();',
  12010. ' return this;',
  12011. ' };',
  12012. ' this.DoGlob = function () {',
  12013. ' };',
  12014. '});',
  12015. '']));
  12016. end;
  12017. procedure TTestModule.TestClass_Inheritance;
  12018. begin
  12019. StartProgram(false);
  12020. Add('type');
  12021. Add(' TObject = class');
  12022. Add(' public');
  12023. Add(' constructor Create;');
  12024. Add(' end;');
  12025. Add(' TClassA = class');
  12026. Add(' end;');
  12027. Add(' TClassB = class(TObject)');
  12028. Add(' procedure ProcB;');
  12029. Add(' end;');
  12030. Add('constructor tobject.create; begin end;');
  12031. Add('procedure tclassb.procb; begin end;');
  12032. Add('var');
  12033. Add(' oO: TObject;');
  12034. Add(' oA: TClassA;');
  12035. Add(' oB: TClassB;');
  12036. Add('begin');
  12037. Add(' oO:=tobject.Create;');
  12038. Add(' oA:=tclassa.Create;');
  12039. Add(' ob:=tclassb.Create;');
  12040. Add(' if oo is tclassa then ;');
  12041. Add(' ob:=oo as tclassb;');
  12042. Add(' (oo as tclassb).procb;');
  12043. ConvertProgram;
  12044. CheckSource('TestClass_Inheritance',
  12045. LinesToStr([ // statements
  12046. 'rtl.createClass($mod,"TObject",null,function(){',
  12047. ' this.$init = function () {',
  12048. ' };',
  12049. ' this.$final = function () {',
  12050. ' };',
  12051. ' this.Create = function () {',
  12052. ' return this;',
  12053. ' };',
  12054. '});',
  12055. 'rtl.createClass($mod,"TClassA",$mod.TObject,function(){',
  12056. '});',
  12057. 'rtl.createClass($mod,"TClassB",$mod.TObject,function(){',
  12058. ' this.ProcB = function () {',
  12059. ' };',
  12060. '});',
  12061. 'this.oO = null;',
  12062. 'this.oA = null;',
  12063. 'this.oB = null;'
  12064. ]),
  12065. LinesToStr([ // $mod.$main
  12066. '$mod.oO = $mod.TObject.$create("Create");',
  12067. '$mod.oA = $mod.TClassA.$create("Create");',
  12068. '$mod.oB = $mod.TClassB.$create("Create");',
  12069. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12070. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12071. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12072. ]));
  12073. end;
  12074. procedure TTestModule.TestClass_TypeAlias;
  12075. begin
  12076. StartProgram(false);
  12077. Add([
  12078. '{$interfaces corba}',
  12079. 'type',
  12080. ' IObject = interface',
  12081. ' end;',
  12082. ' IBird = type IObject;',
  12083. ' TObject = class',
  12084. ' end;',
  12085. ' TBird = type TObject;',
  12086. 'var',
  12087. ' oObj: TObject;',
  12088. ' oBird: TBird;',
  12089. ' IntfObj: IObject;',
  12090. ' IntfBird: IBird;',
  12091. 'begin',
  12092. ' oObj:=oBird;',
  12093. '']);
  12094. ConvertProgram;
  12095. CheckSource('TestClass_TypeAlias',
  12096. LinesToStr([ // statements
  12097. 'rtl.createInterface($mod, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12098. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], $mod.IObject);',
  12099. 'rtl.createClass($mod, "TObject", null, function () {',
  12100. ' this.$init = function () {',
  12101. ' };',
  12102. ' this.$final = function () {',
  12103. ' };',
  12104. '});',
  12105. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12106. '});',
  12107. 'this.oObj = null;',
  12108. 'this.oBird = null;',
  12109. 'this.IntfObj = null;',
  12110. 'this.IntfBird = null;',
  12111. '']),
  12112. LinesToStr([ // $mod.$main
  12113. '$mod.oObj = $mod.oBird;',
  12114. '']));
  12115. end;
  12116. procedure TTestModule.TestClass_AbstractMethod;
  12117. begin
  12118. StartProgram(false);
  12119. Add('type');
  12120. Add(' TObject = class');
  12121. Add(' public');
  12122. Add(' procedure DoIt; virtual; abstract;');
  12123. Add(' end;');
  12124. Add('begin');
  12125. ConvertProgram;
  12126. CheckSource('TestClass_AbstractMethod',
  12127. LinesToStr([ // statements
  12128. 'rtl.createClass($mod,"TObject",null,function(){',
  12129. ' this.$init = function () {',
  12130. ' };',
  12131. ' this.$final = function () {',
  12132. ' };',
  12133. '});'
  12134. ]),
  12135. LinesToStr([ // this.$main
  12136. ''
  12137. ]));
  12138. end;
  12139. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12140. begin
  12141. StartProgram(false);
  12142. Add([
  12143. 'type',
  12144. ' TObject = class',
  12145. ' procedure DoAbstract; virtual; abstract;',
  12146. ' procedure DoVirtual; virtual;',
  12147. ' procedure DoIt;',
  12148. ' end;',
  12149. ' TA = class',
  12150. ' procedure doabstract; override;',
  12151. ' procedure dovirtual; override;',
  12152. ' procedure DoSome;',
  12153. ' end;',
  12154. 'procedure tobject.dovirtual;',
  12155. 'begin',
  12156. ' inherited; // call non existing ancestor -> ignore silently',
  12157. 'end;',
  12158. 'procedure tobject.doit;',
  12159. 'begin',
  12160. 'end;',
  12161. 'procedure ta.doabstract;',
  12162. 'begin',
  12163. ' inherited dovirtual; // call TObject.DoVirtual',
  12164. 'end;',
  12165. 'procedure ta.dovirtual;',
  12166. 'begin',
  12167. ' inherited; // call TObject.DoVirtual',
  12168. ' inherited dovirtual; // call TObject.DoVirtual',
  12169. ' inherited dovirtual(); // call TObject.DoVirtual',
  12170. ' doit;',
  12171. ' doit();',
  12172. 'end;',
  12173. 'procedure ta.dosome;',
  12174. 'begin',
  12175. ' inherited; // call non existing ancestor method -> silently ignore',
  12176. 'end;',
  12177. 'begin']);
  12178. ConvertProgram;
  12179. CheckSource('TestClass_CallInherited_ProcNoParams',
  12180. LinesToStr([ // statements
  12181. 'rtl.createClass($mod,"TObject",null,function(){',
  12182. ' this.$init = function () {',
  12183. ' };',
  12184. ' this.$final = function () {',
  12185. ' };',
  12186. ' this.DoVirtual = function () {',
  12187. ' };',
  12188. ' this.DoIt = function () {',
  12189. ' };',
  12190. '});',
  12191. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  12192. ' this.DoAbstract = function () {',
  12193. ' $mod.TObject.DoVirtual.call(this);',
  12194. ' };',
  12195. ' this.DoVirtual = function () {',
  12196. ' $mod.TObject.DoVirtual.call(this);',
  12197. ' $mod.TObject.DoVirtual.call(this);',
  12198. ' $mod.TObject.DoVirtual.call(this);',
  12199. ' this.DoIt();',
  12200. ' this.DoIt();',
  12201. ' };',
  12202. ' this.DoSome = function () {',
  12203. ' };',
  12204. '});'
  12205. ]),
  12206. LinesToStr([ // this.$main
  12207. ''
  12208. ]));
  12209. end;
  12210. procedure TTestModule.TestClass_CallInherited_WithParams;
  12211. begin
  12212. StartProgram(false);
  12213. Add([
  12214. 'type',
  12215. ' TObject = class',
  12216. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12217. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12218. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12219. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12220. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12221. ' end;',
  12222. ' TClassA = class',
  12223. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12224. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12225. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12226. ' end;',
  12227. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12228. 'begin',
  12229. 'end;',
  12230. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12231. 'begin',
  12232. 'end;',
  12233. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12234. 'begin',
  12235. 'end;',
  12236. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12237. 'begin',
  12238. 'end;',
  12239. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12240. 'begin',
  12241. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12242. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12243. 'end;',
  12244. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12245. 'begin',
  12246. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12247. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12248. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12249. ' doit(pa,pb);',
  12250. ' doit(pa);',
  12251. ' doit2(pa);',
  12252. ' doit2;',
  12253. 'end;',
  12254. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12255. 'begin',
  12256. ' pa:=inherited;',
  12257. 'end;',
  12258. 'begin']);
  12259. ConvertProgram;
  12260. CheckSource('TestClass_CallInherited_WithParams',
  12261. LinesToStr([ // statements
  12262. 'rtl.createClass($mod,"TObject",null,function(){',
  12263. ' this.$init = function () {',
  12264. ' };',
  12265. ' this.$final = function () {',
  12266. ' };',
  12267. ' this.DoVirtual = function (pA,pB) {',
  12268. ' };',
  12269. ' this.DoIt = function (pA,pB) {',
  12270. ' };',
  12271. ' this.DoIt2 = function (pA,pB) {',
  12272. ' };',
  12273. ' this.GetIt = function (pA, pB) {',
  12274. ' var Result = 0;',
  12275. ' return Result;',
  12276. ' };',
  12277. '});',
  12278. 'rtl.createClass($mod, "TClassA", $mod.TObject, function () {',
  12279. ' this.DoAbstract = function (pA,pB) {',
  12280. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12281. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12282. ' };',
  12283. ' this.DoVirtual = function (pA,pB) {',
  12284. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12285. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12286. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12287. ' this.DoIt(pA,pB);',
  12288. ' this.DoIt(pA,0);',
  12289. ' this.DoIt2(pA,2);',
  12290. ' this.DoIt2(1,2);',
  12291. ' };',
  12292. ' this.GetIt$1 = function (pA, pB) {',
  12293. ' var Result = 0;',
  12294. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12295. ' return Result;',
  12296. ' };',
  12297. '});'
  12298. ]),
  12299. LinesToStr([ // this.$main
  12300. ''
  12301. ]));
  12302. end;
  12303. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12304. begin
  12305. StartProgram(false);
  12306. Add('type');
  12307. Add(' TObject = class');
  12308. Add(' constructor Create; virtual;');
  12309. Add(' constructor CreateWithB(b: boolean);');
  12310. Add(' end;');
  12311. Add(' TA = class');
  12312. Add(' constructor Create; override;');
  12313. Add(' constructor CreateWithC(c: char);');
  12314. Add(' procedure DoIt;');
  12315. Add(' class function DoSome: TObject;');
  12316. Add(' end;');
  12317. Add('constructor tobject.create;');
  12318. Add('begin');
  12319. Add(' inherited; // call non existing ancestor -> ignore silently');
  12320. Add('end;');
  12321. Add('constructor tobject.createwithb(b: boolean);');
  12322. Add('begin');
  12323. Add(' inherited; // call non existing ancestor -> ignore silently');
  12324. Add(' create; // normal call');
  12325. Add('end;');
  12326. Add('constructor ta.create;');
  12327. Add('begin');
  12328. Add(' inherited; // normal call TObject.Create');
  12329. Add(' inherited create; // normal call TObject.Create');
  12330. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12331. Add('end;');
  12332. Add('constructor ta.createwithc(c: char);');
  12333. Add('begin');
  12334. Add(' inherited create; // call TObject.Create');
  12335. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12336. Add(' doit;');
  12337. Add(' doit();');
  12338. Add(' dosome;');
  12339. Add('end;');
  12340. Add('procedure ta.doit;');
  12341. Add('begin');
  12342. Add(' create; // normal call');
  12343. Add(' createwithb(false); // normal call');
  12344. Add(' createwithc(''c''); // normal call');
  12345. Add('end;');
  12346. Add('class function ta.dosome: TObject;');
  12347. Add('begin');
  12348. Add(' Result:=create; // constructor');
  12349. Add(' Result:=createwithb(true); // constructor');
  12350. Add(' Result:=createwithc(''c''); // constructor');
  12351. Add('end;');
  12352. Add('begin');
  12353. ConvertProgram;
  12354. CheckSource('TestClass_CallInheritedConstructor',
  12355. LinesToStr([ // statements
  12356. 'rtl.createClass($mod,"TObject",null,function(){',
  12357. ' this.$init = function () {',
  12358. ' };',
  12359. ' this.$final = function () {',
  12360. ' };',
  12361. ' this.Create = function () {',
  12362. ' return this;',
  12363. ' };',
  12364. ' this.CreateWithB = function (b) {',
  12365. ' this.Create();',
  12366. ' return this;',
  12367. ' };',
  12368. '});',
  12369. 'rtl.createClass($mod, "TA", $mod.TObject, function () {',
  12370. ' this.Create = function () {',
  12371. ' $mod.TObject.Create.call(this);',
  12372. ' $mod.TObject.Create.call(this);',
  12373. ' $mod.TObject.CreateWithB.call(this, false);',
  12374. ' return this;',
  12375. ' };',
  12376. ' this.CreateWithC = function (c) {',
  12377. ' $mod.TObject.Create.call(this);',
  12378. ' $mod.TObject.CreateWithB.call(this, true);',
  12379. ' this.DoIt();',
  12380. ' this.DoIt();',
  12381. ' this.$class.DoSome();',
  12382. ' return this;',
  12383. ' };',
  12384. ' this.DoIt = function () {',
  12385. ' this.Create();',
  12386. ' this.CreateWithB(false);',
  12387. ' this.CreateWithC("c");',
  12388. ' };',
  12389. ' this.DoSome = function () {',
  12390. ' var Result = null;',
  12391. ' Result = this.$create("Create");',
  12392. ' Result = this.$create("CreateWithB", [true]);',
  12393. ' Result = this.$create("CreateWithC", ["c"]);',
  12394. ' return Result;',
  12395. ' };',
  12396. '});'
  12397. ]),
  12398. LinesToStr([ // this.$main
  12399. ''
  12400. ]));
  12401. end;
  12402. procedure TTestModule.TestClass_ClassVar_Assign;
  12403. begin
  12404. StartProgram(false);
  12405. Add([
  12406. 'type',
  12407. ' TObject = class',
  12408. ' public',
  12409. ' class var vI: longint;',
  12410. ' class var Sub: TObject;',
  12411. ' constructor Create;',
  12412. ' class function GetIt(var Par: longint): tobject;',
  12413. ' end;',
  12414. 'constructor tobject.create;',
  12415. 'begin',
  12416. ' vi:=vi+1;',
  12417. ' Self.vi:=Self.vi+1;',
  12418. ' inc(vi);',
  12419. 'end;',
  12420. 'class function tobject.getit(var par: longint): tobject;',
  12421. 'begin',
  12422. ' vi:=vi+3;',
  12423. ' Self.vi:=Self.vi+4;',
  12424. ' inc(vi);',
  12425. ' Result:=self.sub;',
  12426. ' GetIt(vi);',
  12427. 'end;',
  12428. 'var Obj: tobject;',
  12429. 'begin',
  12430. ' obj:=tobject.create;',
  12431. ' tobject.vi:=3;',
  12432. ' if tobject.vi=4 then ;',
  12433. ' tobject.sub:=nil;',
  12434. ' obj.sub:=nil;',
  12435. ' obj.sub.sub:=nil;']);
  12436. ConvertProgram;
  12437. CheckSource('TestClass_ClassVar_Assign',
  12438. LinesToStr([ // statements
  12439. 'rtl.createClass($mod,"TObject",null,function(){',
  12440. ' this.vI = 0;',
  12441. ' this.Sub = null;',
  12442. ' this.$init = function () {',
  12443. ' };',
  12444. ' this.$final = function () {',
  12445. ' };',
  12446. ' this.Create = function(){',
  12447. ' $mod.TObject.vI = this.vI+1;',
  12448. ' $mod.TObject.vI = this.vI+1;',
  12449. ' $mod.TObject.vI += 1;',
  12450. ' return this;',
  12451. ' };',
  12452. ' this.GetIt = function(Par){',
  12453. ' var Result = null;',
  12454. ' $mod.TObject.vI = this.vI + 3;',
  12455. ' $mod.TObject.vI = this.vI + 4;',
  12456. ' $mod.TObject.vI += 1;',
  12457. ' Result = this.Sub;',
  12458. ' this.GetIt({',
  12459. ' p: $mod.TObject,',
  12460. ' get: function () {',
  12461. ' return this.p.vI;',
  12462. ' },',
  12463. ' set: function (v) {',
  12464. ' this.p.vI = v;',
  12465. ' }',
  12466. ' });',
  12467. ' return Result;',
  12468. ' };',
  12469. '});',
  12470. 'this.Obj = null;'
  12471. ]),
  12472. LinesToStr([ // $mod.$main
  12473. '$mod.Obj = $mod.TObject.$create("Create");',
  12474. '$mod.TObject.vI = 3;',
  12475. 'if ($mod.TObject.vI === 4);',
  12476. '$mod.TObject.Sub=null;',
  12477. '$mod.TObject.Sub=null;',
  12478. '$mod.TObject.Sub=null;',
  12479. '']));
  12480. end;
  12481. procedure TTestModule.TestClass_CallClassMethod;
  12482. begin
  12483. StartProgram(false);
  12484. Add('type');
  12485. Add(' TObject = class');
  12486. Add(' public');
  12487. Add(' class var vI: longint;');
  12488. Add(' class var Sub: TObject;');
  12489. Add(' constructor Create;');
  12490. Add(' function GetMore(Par: longint): longint;');
  12491. Add(' class function GetIt(Par: longint): tobject;');
  12492. Add(' end;');
  12493. Add('constructor tobject.create;');
  12494. Add('begin');
  12495. Add(' sub:=getit(3);');
  12496. Add(' vi:=getmore(4);');
  12497. Add(' sub:=Self.getit(5);');
  12498. Add(' vi:=Self.getmore(6);');
  12499. Add('end;');
  12500. Add('function tobject.getmore(par: longint): longint;');
  12501. Add('begin');
  12502. Add(' sub:=getit(11);');
  12503. Add(' vi:=getmore(12);');
  12504. Add(' sub:=self.getit(13);');
  12505. Add(' vi:=self.getmore(14);');
  12506. Add('end;');
  12507. Add('class function tobject.getit(par: longint): tobject;');
  12508. Add('begin');
  12509. Add(' sub:=getit(21);');
  12510. Add(' vi:=sub.getmore(22);');
  12511. Add(' sub:=self.getit(23);');
  12512. Add(' vi:=self.sub.getmore(24);');
  12513. Add('end;');
  12514. Add('var Obj: tobject;');
  12515. Add('begin');
  12516. Add(' obj:=tobject.create;');
  12517. Add(' tobject.getit(5);');
  12518. Add(' obj.getit(6);');
  12519. Add(' obj.sub.getit(7);');
  12520. Add(' obj.sub.getit(8).SUB:=nil;');
  12521. Add(' obj.sub.getit(9).GETIT(10);');
  12522. Add(' obj.sub.getit(11).SuB.getit(12);');
  12523. ConvertProgram;
  12524. CheckSource('TestClass_CallClassMethod',
  12525. LinesToStr([ // statements
  12526. 'rtl.createClass($mod,"TObject",null,function(){',
  12527. ' this.vI = 0;',
  12528. ' this.Sub = null;',
  12529. ' this.$init = function () {',
  12530. ' };',
  12531. ' this.$final = function () {',
  12532. ' };',
  12533. ' this.Create = function(){',
  12534. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12535. ' $mod.TObject.vI = this.GetMore(4);',
  12536. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12537. ' $mod.TObject.vI = this.GetMore(6);',
  12538. ' return this;',
  12539. ' };',
  12540. ' this.GetMore = function(Par){',
  12541. ' var Result = 0;',
  12542. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12543. ' $mod.TObject.vI = this.GetMore(12);',
  12544. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12545. ' $mod.TObject.vI = this.GetMore(14);',
  12546. ' return Result;',
  12547. ' };',
  12548. ' this.GetIt = function(Par){',
  12549. ' var Result = null;',
  12550. ' $mod.TObject.Sub = this.GetIt(21);',
  12551. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12552. ' $mod.TObject.Sub = this.GetIt(23);',
  12553. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12554. ' return Result;',
  12555. ' };',
  12556. '});',
  12557. 'this.Obj = null;'
  12558. ]),
  12559. LinesToStr([ // $mod.$main
  12560. '$mod.Obj = $mod.TObject.$create("Create");',
  12561. '$mod.TObject.GetIt(5);',
  12562. '$mod.Obj.$class.GetIt(6);',
  12563. '$mod.Obj.Sub.$class.GetIt(7);',
  12564. '$mod.TObject.Sub=null;',
  12565. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12566. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12567. '']));
  12568. end;
  12569. procedure TTestModule.TestClass_Property;
  12570. begin
  12571. StartProgram(false);
  12572. Add('type');
  12573. Add(' TObject = class');
  12574. Add(' Fx: longint;');
  12575. Add(' Fy: longint;');
  12576. Add(' function GetInt: longint;');
  12577. Add(' procedure SetInt(Value: longint);');
  12578. Add(' procedure DoIt;');
  12579. Add(' property IntA: longint read Fx write Fy;');
  12580. Add(' property IntB: longint read GetInt write SetInt;');
  12581. Add(' end;');
  12582. Add('function tobject.getint: longint;');
  12583. Add('begin');
  12584. Add(' result:=fx;');
  12585. Add('end;');
  12586. Add('procedure tobject.setint(value: longint);');
  12587. Add('begin');
  12588. Add(' if value=fy then exit;');
  12589. Add(' fy:=value;');
  12590. Add('end;');
  12591. Add('procedure tobject.doit;');
  12592. Add('begin');
  12593. Add(' IntA:=IntA+1;');
  12594. Add(' Self.IntA:=Self.IntA+1;');
  12595. Add(' IntB:=IntB+1;');
  12596. Add(' Self.IntB:=Self.IntB+1;');
  12597. Add('end;');
  12598. Add('var Obj: tobject;');
  12599. Add('begin');
  12600. Add(' obj.inta:=obj.inta+1;');
  12601. Add(' if obj.intb=2 then;');
  12602. Add(' obj.intb:=obj.intb+2;');
  12603. Add(' obj.setint(obj.inta);');
  12604. ConvertProgram;
  12605. CheckSource('TestClass_Property',
  12606. LinesToStr([ // statements
  12607. 'rtl.createClass($mod, "TObject", null, function () {',
  12608. ' this.$init = function () {',
  12609. ' this.Fx = 0;',
  12610. ' this.Fy = 0;',
  12611. ' };',
  12612. ' this.$final = function () {',
  12613. ' };',
  12614. ' this.GetInt = function () {',
  12615. ' var Result = 0;',
  12616. ' Result = this.Fx;',
  12617. ' return Result;',
  12618. ' };',
  12619. ' this.SetInt = function (Value) {',
  12620. ' if (Value === this.Fy) return;',
  12621. ' this.Fy = Value;',
  12622. ' };',
  12623. ' this.DoIt = function () {',
  12624. ' this.Fy = this.Fx + 1;',
  12625. ' this.Fy = this.Fx + 1;',
  12626. ' this.SetInt(this.GetInt() + 1);',
  12627. ' this.SetInt(this.GetInt() + 1);',
  12628. ' };',
  12629. '});',
  12630. 'this.Obj = null;'
  12631. ]),
  12632. LinesToStr([ // $mod.$main
  12633. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12634. 'if ($mod.Obj.GetInt() === 2);',
  12635. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12636. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12637. ]));
  12638. end;
  12639. procedure TTestModule.TestClass_Property_ClassMethod;
  12640. begin
  12641. StartProgram(false);
  12642. Add([
  12643. 'type',
  12644. ' TObject = class',
  12645. ' class var Fx: longint;',
  12646. ' class var Fy: longint;',
  12647. ' class function GetInt: longint;',
  12648. ' class procedure SetInt(Value: longint);',
  12649. ' end;',
  12650. ' TBird = class',
  12651. ' class procedure DoIt;',
  12652. ' class property IntA: longint read Fx write Fy;',
  12653. ' class property IntB: longint read GetInt write SetInt;',
  12654. ' end;',
  12655. 'class function tobject.getint: longint;',
  12656. 'begin',
  12657. ' result:=fx;',
  12658. 'end;',
  12659. 'class procedure tobject.setint(value: longint);',
  12660. 'begin',
  12661. 'end;',
  12662. 'class procedure tbird.doit;',
  12663. 'begin',
  12664. ' FX:=3;',
  12665. ' IntA:=IntA+1;',
  12666. ' Self.IntA:=Self.IntA+1;',
  12667. ' IntB:=IntB+1;',
  12668. ' Self.IntB:=Self.IntB+1;',
  12669. ' with Self do begin',
  12670. ' FX:=11;',
  12671. ' IntA:=IntA+12;',
  12672. ' IntB:=IntB+13;',
  12673. ' end;',
  12674. 'end;',
  12675. 'var Obj: tbird;',
  12676. 'begin',
  12677. ' tbird.fx:=tbird.fx+1;',
  12678. ' tbird.inta:=tbird.inta+1;',
  12679. ' if tbird.intb=2 then;',
  12680. ' tbird.intb:=tbird.intb+2;',
  12681. ' tbird.setint(tbird.inta);',
  12682. ' obj.inta:=obj.inta+1;',
  12683. ' if obj.intb=2 then;',
  12684. ' obj.intb:=obj.intb+2;',
  12685. ' obj.setint(obj.inta);',
  12686. ' with Tbird do begin',
  12687. ' FX:=FY+1;',
  12688. ' inta:=inta+2;',
  12689. ' intb:=intb+3;',
  12690. ' end;',
  12691. ' with Obj do begin',
  12692. ' FX:=FY+1;',
  12693. ' inta:=inta+2;',
  12694. ' intb:=intb+3;',
  12695. ' end;',
  12696. '']);
  12697. ConvertProgram;
  12698. CheckSource('TestClass_Property_ClassMethod',
  12699. LinesToStr([ // statements
  12700. 'rtl.createClass($mod, "TObject", null, function () {',
  12701. ' this.Fx = 0;',
  12702. ' this.Fy = 0;',
  12703. ' this.$init = function () {',
  12704. ' };',
  12705. ' this.$final = function () {',
  12706. ' };',
  12707. ' this.GetInt = function () {',
  12708. ' var Result = 0;',
  12709. ' Result = this.Fx;',
  12710. ' return Result;',
  12711. ' };',
  12712. ' this.SetInt = function (Value) {',
  12713. ' };',
  12714. '});',
  12715. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12716. ' this.DoIt = function () {',
  12717. ' $mod.TObject.Fx = 3;',
  12718. ' $mod.TObject.Fy = this.Fx + 1;',
  12719. ' $mod.TObject.Fy = this.Fx + 1;',
  12720. ' this.SetInt(this.GetInt() + 1);',
  12721. ' this.SetInt(this.GetInt() + 1);',
  12722. ' $mod.TObject.Fx = 11;',
  12723. ' $mod.TObject.Fy = this.Fx + 12;',
  12724. ' this.SetInt(this.GetInt() + 13);',
  12725. ' };',
  12726. '});',
  12727. 'this.Obj = null;'
  12728. ]),
  12729. LinesToStr([ // $mod.$main
  12730. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  12731. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  12732. 'if ($mod.TBird.GetInt() === 2);',
  12733. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  12734. '$mod.TBird.SetInt($mod.TBird.Fx);',
  12735. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  12736. 'if ($mod.Obj.$class.GetInt() === 2);',
  12737. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  12738. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  12739. 'var $with = $mod.TBird;',
  12740. '$mod.TObject.Fx = $with.Fy + 1;',
  12741. '$mod.TObject.Fy = $with.Fx + 2;',
  12742. '$with.SetInt($with.GetInt() + 3);',
  12743. 'var $with1 = $mod.Obj;',
  12744. '$mod.TObject.Fx = $with1.Fy + 1;',
  12745. '$mod.TObject.Fy = $with1.Fx + 2;',
  12746. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  12747. '']));
  12748. end;
  12749. procedure TTestModule.TestClass_Property_Indexed;
  12750. begin
  12751. StartProgram(false);
  12752. Add([
  12753. 'type',
  12754. ' TObject = class',
  12755. ' FItems: array of longint;',
  12756. ' function GetItems(Index: longint): longint;',
  12757. ' procedure SetItems(Index: longint; Value: longint);',
  12758. ' procedure DoIt;',
  12759. ' property Items[Index: longint]: longint read getitems write setitems;',
  12760. ' end;',
  12761. 'function tobject.getitems(index: longint): longint;',
  12762. 'begin',
  12763. ' Result:=fitems[index];',
  12764. 'end;',
  12765. 'procedure tobject.setitems(index: longint; value: longint);',
  12766. 'begin',
  12767. ' fitems[index]:=value;',
  12768. 'end;',
  12769. 'procedure tobject.doit;',
  12770. 'begin',
  12771. ' items[1]:=2;',
  12772. ' items[3]:=items[4];',
  12773. ' self.items[5]:=self.items[6];',
  12774. ' items[items[7]]:=items[items[8]];',
  12775. 'end;',
  12776. 'var Obj: tobject;',
  12777. 'begin',
  12778. ' obj.Items[11]:=obj.Items[12];',
  12779. '']);
  12780. ConvertProgram;
  12781. CheckSource('TestClass_Property_Indexed',
  12782. LinesToStr([ // statements
  12783. 'rtl.createClass($mod, "TObject", null, function () {',
  12784. ' this.$init = function () {',
  12785. ' this.FItems = [];',
  12786. ' };',
  12787. ' this.$final = function () {',
  12788. ' this.FItems = undefined;',
  12789. ' };',
  12790. ' this.GetItems = function (Index) {',
  12791. ' var Result = 0;',
  12792. ' Result = this.FItems[Index];',
  12793. ' return Result;',
  12794. ' };',
  12795. ' this.SetItems = function (Index, Value) {',
  12796. ' this.FItems[Index] = Value;',
  12797. ' };',
  12798. ' this.DoIt = function () {',
  12799. ' this.SetItems(1, 2);',
  12800. ' this.SetItems(3,this.GetItems(4));',
  12801. ' this.SetItems(5,this.GetItems(6));',
  12802. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  12803. ' };',
  12804. '});',
  12805. 'this.Obj = null;'
  12806. ]),
  12807. LinesToStr([ // $mod.$main
  12808. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  12809. ]));
  12810. end;
  12811. procedure TTestModule.TestClass_Property_IndexSpec;
  12812. begin
  12813. StartProgram(false);
  12814. Add([
  12815. 'type',
  12816. ' TEnum = (red, blue);',
  12817. ' TObject = class',
  12818. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  12819. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  12820. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  12821. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  12822. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  12823. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  12824. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  12825. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  12826. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  12827. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  12828. ' end;',
  12829. 'procedure DoIt(b: boolean); begin end;',
  12830. 'var',
  12831. ' o: TObject;',
  12832. 'begin',
  12833. ' o.B1:=o.B1;',
  12834. ' o.B2:=o.B2;',
  12835. ' o.B3:=o.B3;',
  12836. ' o.I1[''a'']:=o.I1[''b''];',
  12837. ' doit(o.b1);',
  12838. ' doit(o.b2);',
  12839. ' doit(o.i1[''c'']);',
  12840. '']);
  12841. ConvertProgram;
  12842. CheckSource('TestClass_Property_IndexSpec',
  12843. LinesToStr([ // statements
  12844. 'this.TEnum = {',
  12845. ' "0": "red",',
  12846. ' red: 0,',
  12847. ' "1": "blue",',
  12848. ' blue: 1',
  12849. '};',
  12850. 'rtl.createClass($mod, "TObject", null, function () {',
  12851. ' this.$init = function () {',
  12852. ' };',
  12853. ' this.$final = function () {',
  12854. ' };',
  12855. '});',
  12856. 'this.DoIt = function (b) {',
  12857. '};',
  12858. 'this.o = null;',
  12859. '']),
  12860. LinesToStr([ // $mod.$main
  12861. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  12862. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  12863. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  12864. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  12865. '$mod.DoIt($mod.o.GetIntBool(1));',
  12866. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  12867. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  12868. '']));
  12869. end;
  12870. procedure TTestModule.TestClass_PropertyOfTypeArray;
  12871. begin
  12872. StartProgram(false);
  12873. Add('type');
  12874. Add(' TArray = array of longint;');
  12875. Add(' TObject = class');
  12876. Add(' FItems: TArray;');
  12877. Add(' function GetItems: tarray;');
  12878. Add(' procedure SetItems(Value: tarray);');
  12879. Add(' property Items: tarray read getitems write setitems;');
  12880. Add(' procedure SetNumbers(const Value: tarray);');
  12881. Add(' property Numbers: tarray write setnumbers;');
  12882. Add(' end;');
  12883. Add('function tobject.getitems: tarray;');
  12884. Add('begin');
  12885. Add(' Result:=fitems;');
  12886. Add('end;');
  12887. Add('procedure tobject.setitems(value: tarray);');
  12888. Add('begin');
  12889. Add(' fitems:=value;');
  12890. Add(' fitems:=nil;');
  12891. Add(' Items:=nil;');
  12892. Add(' Items:=Items;');
  12893. Add(' Items[1]:=2;');
  12894. Add(' fitems[3]:=Items[4];');
  12895. Add(' Items[5]:=Items[6];');
  12896. Add(' Self.Items[7]:=8;');
  12897. Add(' Self.Items[9]:=Self.Items[10];');
  12898. Add(' Items[Items[11]]:=Items[Items[12]];');
  12899. Add('end;');
  12900. Add('procedure tobject.SetNumbers(const Value: tarray);');
  12901. Add('begin;');
  12902. Add(' Numbers:=nil;');
  12903. Add(' Numbers:=Value;');
  12904. Add(' Self.Numbers:=Value;');
  12905. Add('end;');
  12906. Add('var Obj: tobject;');
  12907. Add('begin');
  12908. Add(' obj.items:=nil;');
  12909. Add(' obj.items:=obj.items;');
  12910. Add(' obj.items[11]:=obj.items[12];');
  12911. ConvertProgram;
  12912. CheckSource('TestClass_PropertyOfTypeArray',
  12913. LinesToStr([ // statements
  12914. 'rtl.createClass($mod, "TObject", null, function () {',
  12915. ' this.$init = function () {',
  12916. ' this.FItems = [];',
  12917. ' };',
  12918. ' this.$final = function () {',
  12919. ' this.FItems = undefined;',
  12920. ' };',
  12921. ' this.GetItems = function () {',
  12922. ' var Result = [];',
  12923. ' Result = rtl.arrayRef(this.FItems);',
  12924. ' return Result;',
  12925. ' };',
  12926. ' this.SetItems = function (Value) {',
  12927. ' this.FItems = rtl.arrayRef(Value);',
  12928. ' this.FItems = [];',
  12929. ' this.SetItems([]);',
  12930. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  12931. ' this.GetItems()[1] = 2;',
  12932. ' this.FItems[3] = this.GetItems()[4];',
  12933. ' this.GetItems()[5] = this.GetItems()[6];',
  12934. ' this.GetItems()[7] = 8;',
  12935. ' this.GetItems()[9] = this.GetItems()[10];',
  12936. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  12937. ' };',
  12938. ' this.SetNumbers = function (Value) {',
  12939. ' this.SetNumbers([]);',
  12940. ' this.SetNumbers(Value);',
  12941. ' this.SetNumbers(Value);',
  12942. ' };',
  12943. '});',
  12944. 'this.Obj = null;'
  12945. ]),
  12946. LinesToStr([ // $mod.$main
  12947. '$mod.Obj.SetItems([]);',
  12948. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  12949. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  12950. ]));
  12951. end;
  12952. procedure TTestModule.TestClass_PropertyDefault;
  12953. begin
  12954. StartProgram(false);
  12955. Add([
  12956. 'type',
  12957. ' TArray = array of longint;',
  12958. ' TObject = class',
  12959. ' end;',
  12960. ' TBird = class',
  12961. ' FItems: TArray;',
  12962. ' function GetItems(Index: longint): longint;',
  12963. ' procedure SetItems(Index, Value: longint);',
  12964. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  12965. ' end;',
  12966. 'function TBird.getitems(index: longint): longint;',
  12967. 'begin',
  12968. 'end;',
  12969. 'procedure TBird.setitems(index, value: longint);',
  12970. 'begin',
  12971. ' Self[1]:=2;',
  12972. ' Self[3]:=Self[index];',
  12973. ' Self[index]:=Self[Self[value]];',
  12974. ' Self[Self[4]]:=value;',
  12975. 'end;',
  12976. 'var',
  12977. ' Bird: TBird;',
  12978. ' Obj: TObject;',
  12979. 'begin',
  12980. ' bird[11]:=12;',
  12981. ' bird[13]:=bird[14];',
  12982. ' bird[Bird[15]]:=bird[Bird[15]];',
  12983. ' TBird(obj)[16]:=TBird(obj)[17];',
  12984. ' (obj as tbird)[18]:=19;',
  12985. '']);
  12986. ConvertProgram;
  12987. CheckSource('TestClass_PropertyDefault',
  12988. LinesToStr([ // statements
  12989. 'rtl.createClass($mod, "TObject", null, function () {',
  12990. ' this.$init = function () {',
  12991. ' };',
  12992. ' this.$final = function () {',
  12993. ' };',
  12994. '});',
  12995. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  12996. ' this.$init = function () {',
  12997. ' $mod.TObject.$init.call(this);',
  12998. ' this.FItems = [];',
  12999. ' };',
  13000. ' this.$final = function () {',
  13001. ' this.FItems = undefined;',
  13002. ' $mod.TObject.$final.call(this);',
  13003. ' };',
  13004. ' this.GetItems = function (Index) {',
  13005. ' var Result = 0;',
  13006. ' return Result;',
  13007. ' };',
  13008. ' this.SetItems = function (Index, Value) {',
  13009. ' this.SetItems(1, 2);',
  13010. ' this.SetItems(3, this.GetItems(Index));',
  13011. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13012. ' this.SetItems(this.GetItems(4), Value);',
  13013. ' };',
  13014. '});',
  13015. 'this.Bird = null;',
  13016. 'this.Obj = null;',
  13017. '']),
  13018. LinesToStr([ // $mod.$main
  13019. '$mod.Bird.SetItems(11, 12);',
  13020. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13021. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13022. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13023. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13024. '']));
  13025. end;
  13026. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13027. begin
  13028. StartProgram(false);
  13029. Add([
  13030. 'type',
  13031. ' TObject = class end;',
  13032. ' TAlphaList = class',
  13033. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13034. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13035. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13036. ' end;',
  13037. ' TBetaList = class',
  13038. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13039. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13040. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13041. ' end;',
  13042. ' TBird = class',
  13043. ' procedure DoIt;',
  13044. ' end;',
  13045. 'procedure TBird.DoIt;',
  13046. 'var',
  13047. ' List: TAlphaList;',
  13048. 'begin',
  13049. ' if TBetaList(List[true])[3]=nil then ;',
  13050. ' TBetaList(List[false])[5]:=nil;',
  13051. 'end;',
  13052. 'var',
  13053. ' List: TAlphaList;',
  13054. 'begin',
  13055. ' if TBetaList(List[true])[3]=nil then ;',
  13056. ' TBetaList(List[false])[5]:=nil;',
  13057. '']);
  13058. ConvertProgram;
  13059. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13060. LinesToStr([ // statements
  13061. 'rtl.createClass($mod, "TObject", null, function () {',
  13062. ' this.$init = function () {',
  13063. ' };',
  13064. ' this.$final = function () {',
  13065. ' };',
  13066. '});',
  13067. 'rtl.createClass($mod, "TAlphaList", $mod.TObject, function () {',
  13068. '});',
  13069. 'rtl.createClass($mod, "TBetaList", $mod.TObject, function () {',
  13070. '});',
  13071. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13072. ' this.DoIt = function () {',
  13073. ' var List = null;',
  13074. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13075. ' List.GetAlphas(false).SetBetas(5, null);',
  13076. ' };',
  13077. '});',
  13078. 'this.List = null;',
  13079. '']),
  13080. LinesToStr([ // $mod.$main
  13081. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13082. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13083. '']));
  13084. end;
  13085. procedure TTestModule.TestClass_PropertyOverride;
  13086. begin
  13087. StartProgram(false);
  13088. Add('type');
  13089. Add(' integer = longint;');
  13090. Add(' TObject = class');
  13091. Add(' FItem: integer;');
  13092. Add(' function GetItem: integer; external name ''GetItem'';');
  13093. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13094. Add(' property Item: integer read getitem write setitem;');
  13095. Add(' end;');
  13096. Add(' TCar = class');
  13097. Add(' FBag: integer;');
  13098. Add(' function GetBag: integer; external name ''GetBag'';');
  13099. Add(' property Item read getbag;');
  13100. Add(' end;');
  13101. Add('var');
  13102. Add(' Obj: tobject;');
  13103. Add(' Car: tcar;');
  13104. Add('begin');
  13105. Add(' Obj.Item:=Obj.Item;');
  13106. Add(' Car.Item:=Car.Item;');
  13107. ConvertProgram;
  13108. CheckSource('TestClass_PropertyOverride',
  13109. LinesToStr([ // statements
  13110. 'rtl.createClass($mod, "TObject", null, function () {',
  13111. ' this.$init = function () {',
  13112. ' this.FItem = 0;',
  13113. ' };',
  13114. ' this.$final = function () {',
  13115. ' };',
  13116. '});',
  13117. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13118. ' this.$init = function () {',
  13119. ' $mod.TObject.$init.call(this);',
  13120. ' this.FBag = 0;',
  13121. ' };',
  13122. '});',
  13123. 'this.Obj = null;',
  13124. 'this.Car = null;',
  13125. '']),
  13126. LinesToStr([ // $mod.$main
  13127. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13128. '$mod.Car.SetItem($mod.Car.GetBag());',
  13129. '']));
  13130. end;
  13131. procedure TTestModule.TestClass_PropertyIncVisibility;
  13132. begin
  13133. AddModuleWithIntfImplSrc('unit1.pp',
  13134. LinesToStr([
  13135. 'type',
  13136. ' TNumber = longint;',
  13137. ' TInteger = longint;',
  13138. ' TObject = class',
  13139. ' private',
  13140. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13141. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13142. ' protected',
  13143. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13144. ' end;']),
  13145. LinesToStr([
  13146. '']));
  13147. StartProgram(true);
  13148. Add([
  13149. 'uses unit1;',
  13150. 'type',
  13151. ' TBird = class',
  13152. ' public',
  13153. ' property Items;',
  13154. ' end;',
  13155. 'procedure DoIt(i: TInteger);',
  13156. 'begin',
  13157. 'end;',
  13158. 'var b: TBird;',
  13159. 'begin',
  13160. ' b.Items[1]:=2;',
  13161. ' b.Items[3]:=b.Items[4];',
  13162. ' DoIt(b.Items[5]);',
  13163. '']);
  13164. ConvertProgram;
  13165. CheckSource('TestClass_PropertyIncVisibility',
  13166. LinesToStr([ // statements
  13167. 'rtl.createClass($mod, "TBird", pas.unit1.TObject, function () {',
  13168. '});',
  13169. 'this.DoIt = function (i) {',
  13170. '};',
  13171. 'this.b = null;'
  13172. ]),
  13173. LinesToStr([ // $mod.$main
  13174. '$mod.b.SetItems(1, 2);',
  13175. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13176. '$mod.DoIt($mod.b.GetItems(5));'
  13177. ]));
  13178. end;
  13179. procedure TTestModule.TestClass_Assigned;
  13180. begin
  13181. StartProgram(false);
  13182. Add('type');
  13183. Add(' TObject = class');
  13184. Add(' end;');
  13185. Add('var');
  13186. Add(' Obj: tobject;');
  13187. Add(' b: boolean;');
  13188. Add('begin');
  13189. Add(' if Assigned(obj) then ;');
  13190. Add(' b:=Assigned(obj) or false;');
  13191. ConvertProgram;
  13192. CheckSource('TestClass_Assigned',
  13193. LinesToStr([ // statements
  13194. 'rtl.createClass($mod, "TObject", null, function () {',
  13195. ' this.$init = function () {',
  13196. ' };',
  13197. ' this.$final = function () {',
  13198. ' };',
  13199. '});',
  13200. 'this.Obj = null;',
  13201. 'this.b = false;'
  13202. ]),
  13203. LinesToStr([ // $mod.$main
  13204. 'if ($mod.Obj != null);',
  13205. '$mod.b = ($mod.Obj != null) || false;'
  13206. ]));
  13207. end;
  13208. procedure TTestModule.TestClass_WithClassDoCreate;
  13209. begin
  13210. StartProgram(false);
  13211. Add('type');
  13212. Add(' TObject = class');
  13213. Add(' aBool: boolean;');
  13214. Add(' Arr: array of boolean;');
  13215. Add(' constructor Create;');
  13216. Add(' end;');
  13217. Add('constructor TObject.Create; begin end;');
  13218. Add('var');
  13219. Add(' Obj: tobject;');
  13220. Add(' b: boolean;');
  13221. Add('begin');
  13222. Add(' with tobject.create do begin');
  13223. Add(' b:=abool;');
  13224. Add(' abool:=b;');
  13225. Add(' b:=arr[1];');
  13226. Add(' arr[2]:=b;');
  13227. Add(' end;');
  13228. Add(' with tobject do');
  13229. Add(' obj:=create;');
  13230. Add(' with obj do begin');
  13231. Add(' create;');
  13232. Add(' b:=abool;');
  13233. Add(' abool:=b;');
  13234. Add(' b:=arr[3];');
  13235. Add(' arr[4]:=b;');
  13236. Add(' end;');
  13237. ConvertProgram;
  13238. CheckSource('TestClass_WithClassDoCreate',
  13239. LinesToStr([ // statements
  13240. 'rtl.createClass($mod, "TObject", null, function () {',
  13241. ' this.$init = function () {',
  13242. ' this.aBool = false;',
  13243. ' this.Arr = [];',
  13244. ' };',
  13245. ' this.$final = function () {',
  13246. ' this.Arr = undefined;',
  13247. ' };',
  13248. ' this.Create = function () {',
  13249. ' return this;',
  13250. ' };',
  13251. '});',
  13252. 'this.Obj = null;',
  13253. 'this.b = false;'
  13254. ]),
  13255. LinesToStr([ // $mod.$main
  13256. 'var $with = $mod.TObject.$create("Create");',
  13257. '$mod.b = $with.aBool;',
  13258. '$with.aBool = $mod.b;',
  13259. '$mod.b = $with.Arr[1];',
  13260. '$with.Arr[2] = $mod.b;',
  13261. 'var $with1 = $mod.TObject;',
  13262. '$mod.Obj = $with1.$create("Create");',
  13263. 'var $with2 = $mod.Obj;',
  13264. '$with2.Create();',
  13265. '$mod.b = $with2.aBool;',
  13266. '$with2.aBool = $mod.b;',
  13267. '$mod.b = $with2.Arr[3];',
  13268. '$with2.Arr[4] = $mod.b;',
  13269. '']));
  13270. end;
  13271. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13272. begin
  13273. StartProgram(false);
  13274. Add('type');
  13275. Add(' TObject = class');
  13276. Add(' FInt: longint;');
  13277. Add(' constructor Create;');
  13278. Add(' function GetSize: longint;');
  13279. Add(' procedure SetSize(Value: longint);');
  13280. Add(' property Int: longint read FInt write FInt;');
  13281. Add(' property Size: longint read GetSize write SetSize;');
  13282. Add(' end;');
  13283. Add('constructor TObject.Create; begin end;');
  13284. Add('function TObject.GetSize: longint; begin; end;');
  13285. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13286. Add('var');
  13287. Add(' Obj: tobject;');
  13288. Add(' i: longint;');
  13289. Add('begin');
  13290. Add(' with TObject.Create do begin');
  13291. Add(' i:=int;');
  13292. Add(' int:=i;');
  13293. Add(' i:=size;');
  13294. Add(' size:=i;');
  13295. Add(' end;');
  13296. Add(' with obj do begin');
  13297. Add(' i:=int;');
  13298. Add(' int:=i;');
  13299. Add(' i:=size;');
  13300. Add(' size:=i;');
  13301. Add(' end;');
  13302. ConvertProgram;
  13303. CheckSource('TestClass_WithClassInstDoProperty',
  13304. LinesToStr([ // statements
  13305. 'rtl.createClass($mod, "TObject", null, function () {',
  13306. ' this.$init = function () {',
  13307. ' this.FInt = 0;',
  13308. ' };',
  13309. ' this.$final = function () {',
  13310. ' };',
  13311. ' this.Create = function () {',
  13312. ' return this;',
  13313. ' };',
  13314. ' this.GetSize = function () {',
  13315. ' var Result = 0;',
  13316. ' return Result;',
  13317. ' };',
  13318. ' this.SetSize = function (Value) {',
  13319. ' };',
  13320. '});',
  13321. 'this.Obj = null;',
  13322. 'this.i = 0;'
  13323. ]),
  13324. LinesToStr([ // $mod.$main
  13325. 'var $with = $mod.TObject.$create("Create");',
  13326. '$mod.i = $with.FInt;',
  13327. '$with.FInt = $mod.i;',
  13328. '$mod.i = $with.GetSize();',
  13329. '$with.SetSize($mod.i);',
  13330. 'var $with1 = $mod.Obj;',
  13331. '$mod.i = $with1.FInt;',
  13332. '$with1.FInt = $mod.i;',
  13333. '$mod.i = $with1.GetSize();',
  13334. '$with1.SetSize($mod.i);',
  13335. '']));
  13336. end;
  13337. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13338. begin
  13339. StartProgram(false);
  13340. Add('type');
  13341. Add(' TObject = class');
  13342. Add(' constructor Create;');
  13343. Add(' function GetItems(Index: longint): longint;');
  13344. Add(' procedure SetItems(Index, Value: longint);');
  13345. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13346. Add(' end;');
  13347. Add('constructor TObject.Create; begin end;');
  13348. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13349. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13350. Add('var');
  13351. Add(' Obj: tobject;');
  13352. Add(' i: longint;');
  13353. Add('begin');
  13354. Add(' with TObject.Create do begin');
  13355. Add(' i:=Items[1];');
  13356. Add(' Items[2]:=i;');
  13357. Add(' end;');
  13358. Add(' with obj do begin');
  13359. Add(' i:=Items[3];');
  13360. Add(' Items[4]:=i;');
  13361. Add(' end;');
  13362. ConvertProgram;
  13363. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13364. LinesToStr([ // statements
  13365. 'rtl.createClass($mod, "TObject", null, function () {',
  13366. ' this.$init = function () {',
  13367. ' };',
  13368. ' this.$final = function () {',
  13369. ' };',
  13370. ' this.Create = function () {',
  13371. ' return this;',
  13372. ' };',
  13373. ' this.GetItems = function (Index) {',
  13374. ' var Result = 0;',
  13375. ' return Result;',
  13376. ' };',
  13377. ' this.SetItems = function (Index, Value) {',
  13378. ' };',
  13379. '});',
  13380. 'this.Obj = null;',
  13381. 'this.i = 0;'
  13382. ]),
  13383. LinesToStr([ // $mod.$main
  13384. 'var $with = $mod.TObject.$create("Create");',
  13385. '$mod.i = $with.GetItems(1);',
  13386. '$with.SetItems(2, $mod.i);',
  13387. 'var $with1 = $mod.Obj;',
  13388. '$mod.i = $with1.GetItems(3);',
  13389. '$with1.SetItems(4, $mod.i);',
  13390. '']));
  13391. end;
  13392. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13393. begin
  13394. StartProgram(false);
  13395. Add('type');
  13396. Add(' TObject = class');
  13397. Add(' constructor Create;');
  13398. Add(' function GetSize: longint;');
  13399. Add(' procedure SetSize(Value: longint);');
  13400. Add(' end;');
  13401. Add('constructor TObject.Create; begin end;');
  13402. Add('function TObject.GetSize: longint; begin; end;');
  13403. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13404. Add('var');
  13405. Add(' Obj: tobject;');
  13406. Add(' i: longint;');
  13407. Add('begin');
  13408. Add(' with TObject.Create do begin');
  13409. Add(' i:=GetSize;');
  13410. Add(' i:=GetSize();');
  13411. Add(' SetSize(i);');
  13412. Add(' end;');
  13413. Add(' with obj do begin');
  13414. Add(' i:=GetSize;');
  13415. Add(' i:=GetSize();');
  13416. Add(' SetSize(i);');
  13417. Add(' end;');
  13418. ConvertProgram;
  13419. CheckSource('TestClass_WithClassInstDoFunc',
  13420. LinesToStr([ // statements
  13421. 'rtl.createClass($mod, "TObject", null, function () {',
  13422. ' this.$init = function () {',
  13423. ' };',
  13424. ' this.$final = function () {',
  13425. ' };',
  13426. ' this.Create = function () {',
  13427. ' return this;',
  13428. ' };',
  13429. ' this.GetSize = function () {',
  13430. ' var Result = 0;',
  13431. ' return Result;',
  13432. ' };',
  13433. ' this.SetSize = function (Value) {',
  13434. ' };',
  13435. '});',
  13436. 'this.Obj = null;',
  13437. 'this.i = 0;'
  13438. ]),
  13439. LinesToStr([ // $mod.$main
  13440. 'var $with = $mod.TObject.$create("Create");',
  13441. '$mod.i = $with.GetSize();',
  13442. '$mod.i = $with.GetSize();',
  13443. '$with.SetSize($mod.i);',
  13444. 'var $with1 = $mod.Obj;',
  13445. '$mod.i = $with1.GetSize();',
  13446. '$mod.i = $with1.GetSize();',
  13447. '$with1.SetSize($mod.i);',
  13448. '']));
  13449. end;
  13450. procedure TTestModule.TestClass_TypeCast;
  13451. begin
  13452. StartProgram(false);
  13453. Add('type');
  13454. Add(' TObject = class');
  13455. Add(' Next: TObject;');
  13456. Add(' constructor Create;');
  13457. Add(' end;');
  13458. Add(' TControl = class(TObject)');
  13459. Add(' Arr: array of TObject;');
  13460. Add(' function GetIt(vI: longint = 0): TObject;');
  13461. Add(' end;');
  13462. Add('constructor tobject.create; begin end;');
  13463. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13464. Add('var');
  13465. Add(' Obj: tobject;');
  13466. Add('begin');
  13467. Add(' obj:=tcontrol(obj).next;');
  13468. Add(' tcontrol(obj):=nil;');
  13469. Add(' obj:=tcontrol(obj);');
  13470. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13471. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13472. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13473. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13474. Add(' obj:=tcontrol(nil);');
  13475. ConvertProgram;
  13476. CheckSource('TestClass_TypeCast',
  13477. LinesToStr([ // statements
  13478. 'rtl.createClass($mod, "TObject", null, function () {',
  13479. ' this.$init = function () {',
  13480. ' this.Next = null;',
  13481. ' };',
  13482. ' this.$final = function () {',
  13483. ' this.Next = undefined;',
  13484. ' };',
  13485. ' this.Create = function () {',
  13486. ' return this;',
  13487. ' };',
  13488. '});',
  13489. 'rtl.createClass($mod, "TControl", $mod.TObject, function () {',
  13490. ' this.$init = function () {',
  13491. ' $mod.TObject.$init.call(this);',
  13492. ' this.Arr = [];',
  13493. ' };',
  13494. ' this.$final = function () {',
  13495. ' this.Arr = undefined;',
  13496. ' $mod.TObject.$final.call(this);',
  13497. ' };',
  13498. ' this.GetIt = function (vI) {',
  13499. ' var Result = null;',
  13500. ' return Result;',
  13501. ' };',
  13502. '});',
  13503. 'this.Obj = null;'
  13504. ]),
  13505. LinesToStr([ // $mod.$main
  13506. '$mod.Obj = $mod.Obj.Next;',
  13507. '$mod.Obj = null;',
  13508. '$mod.Obj = $mod.Obj;',
  13509. '$mod.Obj = $mod.Obj.GetIt(0);',
  13510. '$mod.Obj = $mod.Obj.GetIt(0);',
  13511. '$mod.Obj = $mod.Obj.GetIt(1);',
  13512. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13513. '$mod.Obj = null;',
  13514. '']));
  13515. end;
  13516. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13517. begin
  13518. StartProgram(false);
  13519. Add('type');
  13520. Add(' TObject = class end;');
  13521. Add('procedure ProcA(var A);');
  13522. Add('begin');
  13523. Add(' TObject(A):=nil;');
  13524. Add(' TObject(A):=TObject(A);');
  13525. Add(' if TObject(A)=nil then ;');
  13526. Add(' if nil=TObject(A) then ;');
  13527. Add('end;');
  13528. Add('procedure ProcB(out A);');
  13529. Add('begin');
  13530. Add(' TObject(A):=nil;');
  13531. Add(' TObject(A):=TObject(A);');
  13532. Add(' if TObject(A)=nil then ;');
  13533. Add(' if nil=TObject(A) then ;');
  13534. Add('end;');
  13535. Add('procedure ProcC(const A);');
  13536. Add('begin');
  13537. Add(' if TObject(A)=nil then ;');
  13538. Add(' if nil=TObject(A) then ;');
  13539. Add('end;');
  13540. Add('var o: TObject;');
  13541. Add('begin');
  13542. Add(' ProcA(o);');
  13543. Add(' ProcB(o);');
  13544. Add(' ProcC(o);');
  13545. ConvertProgram;
  13546. CheckSource('TestClass_TypeCastUntypedParam',
  13547. LinesToStr([ // statements
  13548. 'rtl.createClass($mod, "TObject", null, function () {',
  13549. ' this.$init = function () {',
  13550. ' };',
  13551. ' this.$final = function () {',
  13552. ' };',
  13553. '});',
  13554. 'this.ProcA = function (A) {',
  13555. ' A.set(null);',
  13556. ' A.set(A.get());',
  13557. ' if (A.get() === null);',
  13558. ' if (null === A.get());',
  13559. '};',
  13560. 'this.ProcB = function (A) {',
  13561. ' A.set(null);',
  13562. ' A.set(A.get());',
  13563. ' if (A.get() === null);',
  13564. ' if (null === A.get());',
  13565. '};',
  13566. 'this.ProcC = function (A) {',
  13567. ' if (A === null);',
  13568. ' if (null === A);',
  13569. '};',
  13570. 'this.o = null;',
  13571. '']),
  13572. LinesToStr([ // $mod.$main
  13573. '$mod.ProcA({',
  13574. ' p: $mod,',
  13575. ' get: function () {',
  13576. ' return this.p.o;',
  13577. ' },',
  13578. ' set: function (v) {',
  13579. ' this.p.o = v;',
  13580. ' }',
  13581. '});',
  13582. '$mod.ProcB({',
  13583. ' p: $mod,',
  13584. ' get: function () {',
  13585. ' return this.p.o;',
  13586. ' },',
  13587. ' set: function (v) {',
  13588. ' this.p.o = v;',
  13589. ' }',
  13590. '});',
  13591. '$mod.ProcC($mod.o);',
  13592. '']));
  13593. end;
  13594. procedure TTestModule.TestClass_Overloads;
  13595. begin
  13596. StartProgram(false);
  13597. Add('type');
  13598. Add(' TObject = class');
  13599. Add(' procedure DoIt;');
  13600. Add(' procedure DoIt(vI: longint);');
  13601. Add(' end;');
  13602. Add('procedure TObject.DoIt;');
  13603. Add('begin');
  13604. Add(' DoIt;');
  13605. Add(' DoIt(1);');
  13606. Add('end;');
  13607. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13608. Add('begin');
  13609. ConvertProgram;
  13610. CheckSource('TestClass_Overloads',
  13611. LinesToStr([ // statements
  13612. 'rtl.createClass($mod, "TObject", null, function () {',
  13613. ' this.$init = function () {',
  13614. ' };',
  13615. ' this.$final = function () {',
  13616. ' };',
  13617. ' this.DoIt = function () {',
  13618. ' this.DoIt();',
  13619. ' this.DoIt$1(1);',
  13620. ' };',
  13621. ' this.DoIt$1 = function (vI) {',
  13622. ' };',
  13623. '});',
  13624. '']),
  13625. LinesToStr([ // $mod.$main
  13626. '']));
  13627. end;
  13628. procedure TTestModule.TestClass_OverloadsAncestor;
  13629. begin
  13630. StartProgram(false);
  13631. Add('type');
  13632. Add(' TObject = class;');
  13633. Add(' TObject = class');
  13634. Add(' procedure DoIt(vA: longint);');
  13635. Add(' procedure DoIt(vA, vB: longint);');
  13636. Add(' end;');
  13637. Add(' TCar = class;');
  13638. Add(' TCar = class');
  13639. Add(' procedure DoIt(vA: longint);');
  13640. Add(' procedure DoIt(vA, vB: longint);');
  13641. Add(' end;');
  13642. Add('procedure tobject.doit(va: longint);');
  13643. Add('begin');
  13644. Add(' doit(1);');
  13645. Add(' doit(1,2);');
  13646. Add('end;');
  13647. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13648. Add('procedure tcar.doit(va: longint);');
  13649. Add('begin');
  13650. Add(' doit(1);');
  13651. Add(' doit(1,2);');
  13652. Add(' inherited doit(1);');
  13653. Add(' inherited doit(1,2);');
  13654. Add('end;');
  13655. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13656. Add('begin');
  13657. ConvertProgram;
  13658. CheckSource('TestClass_OverloadsAncestor',
  13659. LinesToStr([ // statements
  13660. 'rtl.createClass($mod, "TObject", null, function () {',
  13661. ' this.$init = function () {',
  13662. ' };',
  13663. ' this.$final = function () {',
  13664. ' };',
  13665. ' this.DoIt = function (vA) {',
  13666. ' this.DoIt(1);',
  13667. ' this.DoIt$1(1,2);',
  13668. ' };',
  13669. ' this.DoIt$1 = function (vA, vB) {',
  13670. ' };',
  13671. '});',
  13672. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13673. ' this.DoIt$2 = function (vA) {',
  13674. ' this.DoIt$2(1);',
  13675. ' this.DoIt$3(1, 2);',
  13676. ' $mod.TObject.DoIt.call(this, 1);',
  13677. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13678. ' };',
  13679. ' this.DoIt$3 = function (vA, vB) {',
  13680. ' };',
  13681. '});',
  13682. '']),
  13683. LinesToStr([ // $mod.$main
  13684. '']));
  13685. end;
  13686. procedure TTestModule.TestClass_OverloadConstructor;
  13687. begin
  13688. StartProgram(false);
  13689. Add('type');
  13690. Add(' TObject = class');
  13691. Add(' constructor Create(vA: longint);');
  13692. Add(' constructor Create(vA, vB: longint);');
  13693. Add(' end;');
  13694. Add(' TCar = class');
  13695. Add(' constructor Create(vA: longint);');
  13696. Add(' constructor Create(vA, vB: longint);');
  13697. Add(' end;');
  13698. Add('constructor tobject.create(va: longint);');
  13699. Add('begin');
  13700. Add(' create(1);');
  13701. Add(' create(1,2);');
  13702. Add('end;');
  13703. Add('constructor tobject.create(va, vb: longint); begin end;');
  13704. Add('constructor tcar.create(va: longint);');
  13705. Add('begin');
  13706. Add(' create(1);');
  13707. Add(' create(1,2);');
  13708. Add(' inherited create(1);');
  13709. Add(' inherited create(1,2);');
  13710. Add('end;');
  13711. Add('constructor tcar.create(va, vb: longint); begin end;');
  13712. Add('begin');
  13713. Add(' tobject.create(1);');
  13714. Add(' tobject.create(1,2);');
  13715. Add(' tcar.create(1);');
  13716. Add(' tcar.create(1,2);');
  13717. ConvertProgram;
  13718. CheckSource('TestClass_OverloadConstructor',
  13719. LinesToStr([ // statements
  13720. 'rtl.createClass($mod, "TObject", null, function () {',
  13721. ' this.$init = function () {',
  13722. ' };',
  13723. ' this.$final = function () {',
  13724. ' };',
  13725. ' this.Create = function (vA) {',
  13726. ' this.Create(1);',
  13727. ' this.Create$1(1,2);',
  13728. ' return this;',
  13729. ' };',
  13730. ' this.Create$1 = function (vA, vB) {',
  13731. ' return this;',
  13732. ' };',
  13733. '});',
  13734. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  13735. ' this.Create$2 = function (vA) {',
  13736. ' this.Create$2(1);',
  13737. ' this.Create$3(1, 2);',
  13738. ' $mod.TObject.Create.call(this, 1);',
  13739. ' $mod.TObject.Create$1.call(this, 1, 2);',
  13740. ' return this;',
  13741. ' };',
  13742. ' this.Create$3 = function (vA, vB) {',
  13743. ' return this;',
  13744. ' };',
  13745. '});',
  13746. '']),
  13747. LinesToStr([ // $mod.$main
  13748. '$mod.TObject.$create("Create", [1]);',
  13749. '$mod.TObject.$create("Create$1", [1, 2]);',
  13750. '$mod.TCar.$create("Create$2", [1]);',
  13751. '$mod.TCar.$create("Create$3", [1, 2]);',
  13752. '']));
  13753. end;
  13754. procedure TTestModule.TestClass_OverloadDelphiOverride;
  13755. begin
  13756. StartProgram(false);
  13757. Add([
  13758. '{$mode delphi}',
  13759. 'type',
  13760. ' TObject = class end;',
  13761. ' TBird = class',
  13762. ' function {#a}GetValue: longint; overload; virtual;',
  13763. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  13764. ' end;',
  13765. ' TEagle = class(TBird)',
  13766. ' function {#c}GetValue: longint; overload; override;',
  13767. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  13768. ' end;',
  13769. 'function TBird.GetValue: longint;',
  13770. 'begin',
  13771. ' if 3={@a}GetValue then ;',
  13772. ' if 4={@b}GetValue(5) then ;',
  13773. 'end;',
  13774. 'function TBird.GetValue(AValue: longint): longint;',
  13775. 'begin',
  13776. 'end;',
  13777. 'function TEagle.GetValue: longint;',
  13778. 'begin',
  13779. ' if 13={@c}GetValue then ;',
  13780. ' if 14={@d}GetValue(15) then ;',
  13781. ' if 15=inherited {@a}GetValue then ;',
  13782. ' if 16=inherited {@b}GetValue(17) then ;',
  13783. 'end;',
  13784. 'function TEagle.GetValue(AValue: longint): longint;',
  13785. 'begin',
  13786. 'end;',
  13787. 'var',
  13788. ' e: TEagle;',
  13789. 'begin',
  13790. ' if 23=e.{@c}GetValue then ;',
  13791. ' if 24=e.{@d}GetValue(25) then ;']);
  13792. ConvertProgram;
  13793. CheckSource('TestClass_OverloadDelphiOverride',
  13794. LinesToStr([ // statements
  13795. 'rtl.createClass($mod, "TObject", null, function () {',
  13796. ' this.$init = function () {',
  13797. ' };',
  13798. ' this.$final = function () {',
  13799. ' };',
  13800. '});',
  13801. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  13802. ' this.GetValue = function () {',
  13803. ' var Result = 0;',
  13804. ' if (3 === this.GetValue()) ;',
  13805. ' if (4 === this.GetValue$1(5)) ;',
  13806. ' return Result;',
  13807. ' };',
  13808. ' this.GetValue$1 = function (AValue) {',
  13809. ' var Result = 0;',
  13810. ' return Result;',
  13811. ' };',
  13812. '});',
  13813. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13814. ' this.GetValue = function () {',
  13815. ' var Result = 0;',
  13816. ' if (13 === this.GetValue()) ;',
  13817. ' if (14 === this.GetValue$1(15)) ;',
  13818. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  13819. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  13820. ' return Result;',
  13821. ' };',
  13822. ' this.GetValue$1 = function (AValue) {',
  13823. ' var Result = 0;',
  13824. ' return Result;',
  13825. ' };',
  13826. '});',
  13827. 'this.e = null;',
  13828. '']),
  13829. LinesToStr([ // $mod.$main
  13830. 'if (23 === $mod.e.GetValue()) ;',
  13831. 'if (24 === $mod.e.GetValue$1(25)) ;',
  13832. '']));
  13833. end;
  13834. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  13835. begin
  13836. StartProgram(false);
  13837. Add([
  13838. '{$mode delphi}',
  13839. 'type',
  13840. ' TObject = class end;',
  13841. ' TAnimal = class',
  13842. ' public',
  13843. ' {#animal_a}A: longint;',
  13844. ' function {#animal_b}B: longint;',
  13845. ' end;',
  13846. ' TBird = class(TAnimal)',
  13847. ' public',
  13848. ' {#bird_a}A: double;',
  13849. ' {#bird_b}B: boolean;',
  13850. ' end;',
  13851. ' TEagle = class(TBird)',
  13852. ' public',
  13853. ' function {#eagle_a}A: boolean;',
  13854. ' {#eagle_b}B: double;',
  13855. ' end;',
  13856. 'function TAnimal.B: longint;',
  13857. 'begin',
  13858. 'end;',
  13859. 'function TEagle.A: boolean;',
  13860. 'begin',
  13861. ' {@eagle_b}B:=3.3;',
  13862. ' {@eagle_a}A();',
  13863. ' TBird(Self).{@bird_b}B:=true;',
  13864. ' TAnimal(Self).{@animal_a}A:=17;',
  13865. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  13866. 'end;',
  13867. 'var',
  13868. ' e: TEagle;',
  13869. 'begin',
  13870. ' e.{@eagle_b}B:=5.3;',
  13871. ' if e.{@eagle_a}A then ;',
  13872. '']);
  13873. ConvertProgram;
  13874. CheckSource('TestClass_ReintroduceVarDelphi',
  13875. LinesToStr([ // statements
  13876. 'rtl.createClass($mod, "TObject", null, function () {',
  13877. ' this.$init = function () {',
  13878. ' };',
  13879. ' this.$final = function () {',
  13880. ' };',
  13881. '});',
  13882. 'rtl.createClass($mod, "TAnimal", $mod.TObject, function () {',
  13883. ' this.$init = function () {',
  13884. ' $mod.TObject.$init.call(this);',
  13885. ' this.A = 0;',
  13886. ' };',
  13887. ' this.B = function () {',
  13888. ' var Result = 0;',
  13889. ' return Result;',
  13890. ' };',
  13891. '});',
  13892. 'rtl.createClass($mod, "TBird", $mod.TAnimal, function () {',
  13893. ' this.$init = function () {',
  13894. ' $mod.TAnimal.$init.call(this);',
  13895. ' this.A$1 = 0.0;',
  13896. ' this.B$1 = false;',
  13897. ' };',
  13898. '});',
  13899. 'rtl.createClass($mod, "TEagle", $mod.TBird, function () {',
  13900. ' this.$init = function () {',
  13901. ' $mod.TBird.$init.call(this);',
  13902. ' this.B$2 = 0.0;',
  13903. ' };',
  13904. ' this.A$2 = function () {',
  13905. ' var Result = false;',
  13906. ' this.B$2 = 3.3;',
  13907. ' this.A$2();',
  13908. ' this.B$1 = true;',
  13909. ' this.A = 17;',
  13910. ' this.B$1 = this.A$1 > 1;',
  13911. ' return Result;',
  13912. ' };',
  13913. '});',
  13914. 'this.e = null;',
  13915. '']),
  13916. LinesToStr([ // $mod.$main
  13917. '$mod.e.B$2 = 5.3;',
  13918. 'if ($mod.e.A$2()) ;',
  13919. '']));
  13920. end;
  13921. procedure TTestModule.TestClass_ReintroducedVar;
  13922. begin
  13923. StartProgram(false);
  13924. Add('type');
  13925. Add(' TObject = class');
  13926. Add(' strict private');
  13927. Add(' Some: longint;');
  13928. Add(' end;');
  13929. Add(' TMobile = class');
  13930. Add(' strict private');
  13931. Add(' Some: string;');
  13932. Add(' end;');
  13933. Add(' TCar = class(tmobile)');
  13934. Add(' procedure Some;');
  13935. Add(' procedure Some(vA: longint);');
  13936. Add(' end;');
  13937. Add('procedure tcar.some;');
  13938. Add('begin');
  13939. Add(' Some;');
  13940. Add(' Some(1);');
  13941. Add('end;');
  13942. Add('procedure tcar.some(va: longint); begin end;');
  13943. Add('begin');
  13944. ConvertProgram;
  13945. CheckSource('TestClass_ReintroducedVar',
  13946. LinesToStr([ // statements
  13947. 'rtl.createClass($mod, "TObject", null, function () {',
  13948. ' this.$init = function () {',
  13949. ' this.Some = 0;',
  13950. ' };',
  13951. ' this.$final = function () {',
  13952. ' };',
  13953. '});',
  13954. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  13955. ' this.$init = function () {',
  13956. ' $mod.TObject.$init.call(this);',
  13957. ' this.Some$1 = "";',
  13958. ' };',
  13959. '});',
  13960. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  13961. ' this.Some$2 = function () {',
  13962. ' this.Some$2();',
  13963. ' this.Some$3(1);',
  13964. ' };',
  13965. ' this.Some$3 = function (vA) {',
  13966. ' };',
  13967. '});',
  13968. '']),
  13969. LinesToStr([ // $mod.$main
  13970. '']));
  13971. end;
  13972. procedure TTestModule.TestClass_RaiseDescendant;
  13973. begin
  13974. StartProgram(false);
  13975. Add([
  13976. 'type',
  13977. ' TObject = class',
  13978. ' constructor Create(Msg: string);',
  13979. ' end;',
  13980. ' Exception = class',
  13981. ' end;',
  13982. ' EConvertError = class(Exception)',
  13983. ' end;',
  13984. 'constructor TObject.Create(Msg: string); begin end;',
  13985. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  13986. 'begin',
  13987. ' raise Exception.Create(''Bar1'');',
  13988. ' raise EConvertError.Create(''Bar2'');',
  13989. ' raise AssertConv(''Bar2'');',
  13990. ' raise AssertConv;',
  13991. '']);
  13992. ConvertProgram;
  13993. CheckSource('TestClass_RaiseDescendant',
  13994. LinesToStr([ // statements
  13995. 'rtl.createClass($mod, "TObject", null, function () {',
  13996. ' this.$init = function () {',
  13997. ' };',
  13998. ' this.$final = function () {',
  13999. ' };',
  14000. ' this.Create = function (Msg) {',
  14001. ' return this;',
  14002. ' };',
  14003. '});',
  14004. 'rtl.createClass($mod, "Exception", $mod.TObject, function () {',
  14005. '});',
  14006. 'rtl.createClass($mod, "EConvertError", $mod.Exception, function () {',
  14007. '});',
  14008. 'this.AssertConv = function (Msg) {',
  14009. ' var Result = null;',
  14010. ' return Result;',
  14011. '};',
  14012. '']),
  14013. LinesToStr([ // $mod.$main
  14014. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14015. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14016. 'throw $mod.AssertConv("Bar2");',
  14017. 'throw $mod.AssertConv("def");',
  14018. '']));
  14019. end;
  14020. procedure TTestModule.TestClass_ExternalMethod;
  14021. begin
  14022. AddModuleWithIntfImplSrc('unit2.pas',
  14023. LinesToStr([
  14024. 'type',
  14025. ' TObject = class',
  14026. ' public',
  14027. ' procedure Intern; external name ''$DoIntern'';',
  14028. ' end;',
  14029. '']),
  14030. LinesToStr([
  14031. '']));
  14032. StartUnit(true);
  14033. Add('interface');
  14034. Add('uses unit2;');
  14035. Add('type');
  14036. Add(' TCar = class(TObject)');
  14037. Add(' public');
  14038. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14039. Add(' procedure DoIt;');
  14040. Add(' end;');
  14041. Add('implementation');
  14042. Add('procedure tcar.doit;');
  14043. Add('begin');
  14044. Add(' Intern;');
  14045. Add(' Intern();');
  14046. Add(' Intern2;');
  14047. Add(' Intern2();');
  14048. Add('end;');
  14049. Add('var Obj: TCar;');
  14050. Add('begin');
  14051. Add(' obj.intern;');
  14052. Add(' obj.intern();');
  14053. Add(' obj.intern2;');
  14054. Add(' obj.intern2();');
  14055. Add(' obj.doit;');
  14056. Add(' obj.doit();');
  14057. Add(' with obj do begin');
  14058. Add(' Intern;');
  14059. Add(' Intern();');
  14060. Add(' Intern2;');
  14061. Add(' Intern2();');
  14062. Add(' end;');
  14063. ConvertUnit;
  14064. CheckSource('TestClass_ExternalMethod',
  14065. LinesToStr([
  14066. 'var $impl = $mod.$impl;',
  14067. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  14068. ' this.DoIt = function () {',
  14069. ' this.$DoIntern();',
  14070. ' this.$DoIntern();',
  14071. ' this.$DoIntern2();',
  14072. ' this.$DoIntern2();',
  14073. ' };',
  14074. ' });',
  14075. '']),
  14076. LinesToStr([ // this.$init
  14077. '$impl.Obj.$DoIntern();',
  14078. '$impl.Obj.$DoIntern();',
  14079. '$impl.Obj.$DoIntern2();',
  14080. '$impl.Obj.$DoIntern2();',
  14081. '$impl.Obj.DoIt();',
  14082. '$impl.Obj.DoIt();',
  14083. 'var $with = $impl.Obj;',
  14084. '$with.$DoIntern();',
  14085. '$with.$DoIntern();',
  14086. '$with.$DoIntern2();',
  14087. '$with.$DoIntern2();',
  14088. '']),
  14089. LinesToStr([ // implementation
  14090. '$impl.Obj = null;',
  14091. '']) );
  14092. end;
  14093. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14094. begin
  14095. StartProgram(false);
  14096. Add('type');
  14097. Add(' TObject = class');
  14098. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14099. Add(' end;');
  14100. Add('begin');
  14101. SetExpectedPasResolverError('Virtual method name must match external',
  14102. nVirtualMethodNameMustMatchExternal);
  14103. ConvertProgram;
  14104. end;
  14105. procedure TTestModule.TestClass_ExternalOverrideFail;
  14106. begin
  14107. StartProgram(false);
  14108. Add('type');
  14109. Add(' TObject = class');
  14110. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14111. Add(' end;');
  14112. Add(' TCar = class');
  14113. Add(' procedure DoIt; override; external name ''DoIt'';');
  14114. Add(' end;');
  14115. Add('begin');
  14116. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14117. nInvalidXModifierY);
  14118. ConvertProgram;
  14119. end;
  14120. procedure TTestModule.TestClass_ExternalVar;
  14121. begin
  14122. AddModuleWithIntfImplSrc('unit2.pas',
  14123. LinesToStr([
  14124. '{$modeswitch externalclass}',
  14125. 'type',
  14126. ' TObject = class',
  14127. ' public',
  14128. ' Intern: longint external name ''$Intern'';',
  14129. ' Bracket: longint external name ''["A B"]'';',
  14130. ' end;',
  14131. '']),
  14132. LinesToStr([
  14133. '']));
  14134. StartUnit(true);
  14135. Add([
  14136. 'interface',
  14137. 'uses unit2;',
  14138. '{$modeswitch externalclass}',
  14139. 'type',
  14140. ' TCar = class(tobject)',
  14141. ' public',
  14142. ' Intern2: longint external name ''$Intern2'';',
  14143. ' procedure DoIt;',
  14144. ' end;',
  14145. 'implementation',
  14146. 'procedure tcar.doit;',
  14147. 'begin',
  14148. ' Intern:=Intern+1;',
  14149. ' Intern2:=Intern2+2;',
  14150. ' Bracket:=Bracket+3;',
  14151. 'end;',
  14152. 'var Obj: TCar;',
  14153. 'begin',
  14154. ' obj.intern:=obj.intern+1;',
  14155. ' obj.intern2:=obj.intern2+2;',
  14156. ' obj.Bracket:=obj.Bracket+3;',
  14157. ' with obj do begin',
  14158. ' intern:=intern+1;',
  14159. ' intern2:=intern2+2;',
  14160. ' Bracket:=Bracket+3;',
  14161. ' end;']);
  14162. ConvertUnit;
  14163. CheckSource('TestClass_ExternalVar',
  14164. LinesToStr([
  14165. 'var $impl = $mod.$impl;',
  14166. 'rtl.createClass($mod, "TCar", pas.unit2.TObject, function () {',
  14167. ' this.DoIt = function () {',
  14168. ' this.$Intern = this.$Intern + 1;',
  14169. ' this.$Intern2 = this.$Intern2 + 2;',
  14170. ' this["A B"] = this["A B"] + 3;',
  14171. ' };',
  14172. ' });',
  14173. '']),
  14174. LinesToStr([
  14175. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14176. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14177. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14178. 'var $with = $impl.Obj;',
  14179. '$with.$Intern = $with.$Intern + 1;',
  14180. '$with.$Intern2 = $with.$Intern2 + 2;',
  14181. '$with["A B"] = $with["A B"] + 3;',
  14182. '']),
  14183. LinesToStr([ // implementation
  14184. '$impl.Obj = null;',
  14185. '']));
  14186. end;
  14187. procedure TTestModule.TestClass_Const;
  14188. begin
  14189. StartProgram(false);
  14190. Add('type');
  14191. Add(' integer = longint;');
  14192. Add(' TClass = class of TObject;');
  14193. Add(' TObject = class');
  14194. Add(' public');
  14195. Add(' const cI: integer = 3;');
  14196. Add(' procedure DoIt;');
  14197. Add(' class procedure DoMore;');
  14198. Add(' end;');
  14199. Add('procedure tobject.doit;');
  14200. Add('begin');
  14201. Add(' if cI=4 then;');
  14202. Add(' if 5=cI then;');
  14203. Add(' if Self.cI=6 then;');
  14204. Add(' if 7=Self.cI then;');
  14205. Add(' with Self do begin');
  14206. Add(' if cI=11 then;');
  14207. Add(' if 12=cI then;');
  14208. Add(' end;');
  14209. Add('end;');
  14210. Add('class procedure tobject.domore;');
  14211. Add('begin');
  14212. Add(' if cI=8 then;');
  14213. Add(' if Self.cI=9 then;');
  14214. Add(' if 10=cI then;');
  14215. Add(' if 11=Self.cI then;');
  14216. Add(' with Self do begin');
  14217. Add(' if cI=13 then;');
  14218. Add(' if 14=cI then;');
  14219. Add(' end;');
  14220. Add('end;');
  14221. Add('var');
  14222. Add(' Obj: TObject;');
  14223. Add(' Cla: TClass;');
  14224. Add('begin');
  14225. Add(' if TObject.cI=21 then ;');
  14226. Add(' if Obj.cI=22 then ;');
  14227. Add(' if Cla.cI=23 then ;');
  14228. Add(' with obj do if ci=24 then;');
  14229. Add(' with TObject do if ci=25 then;');
  14230. Add(' with Cla do if ci=26 then;');
  14231. ConvertProgram;
  14232. CheckSource('TestClass_Const',
  14233. LinesToStr([
  14234. 'rtl.createClass($mod, "TObject", null, function () {',
  14235. ' this.cI = 3;',
  14236. ' this.$init = function () {',
  14237. ' };',
  14238. ' this.$final = function () {',
  14239. ' };',
  14240. ' this.DoIt = function () {',
  14241. ' if (this.cI === 4) ;',
  14242. ' if (5 === this.cI) ;',
  14243. ' if (this.cI === 6) ;',
  14244. ' if (7 === this.cI) ;',
  14245. ' if (this.cI === 11) ;',
  14246. ' if (12 === this.cI) ;',
  14247. ' };',
  14248. ' this.DoMore = function () {',
  14249. ' if (this.cI === 8) ;',
  14250. ' if (this.cI === 9) ;',
  14251. ' if (10 === this.cI) ;',
  14252. ' if (11 === this.cI) ;',
  14253. ' if (this.cI === 13) ;',
  14254. ' if (14 === this.cI) ;',
  14255. ' };',
  14256. '});',
  14257. 'this.Obj = null;',
  14258. 'this.Cla = null;',
  14259. '']),
  14260. LinesToStr([
  14261. 'if ($mod.TObject.cI === 21) ;',
  14262. 'if ($mod.Obj.cI === 22) ;',
  14263. 'if ($mod.Cla.cI === 23) ;',
  14264. 'var $with = $mod.Obj;',
  14265. 'if ($with.cI === 24) ;',
  14266. 'var $with1 = $mod.TObject;',
  14267. 'if ($with1.cI === 25) ;',
  14268. 'var $with2 = $mod.Cla;',
  14269. 'if ($with2.cI === 26) ;',
  14270. '']));
  14271. end;
  14272. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14273. begin
  14274. StartProgram(false);
  14275. Add([
  14276. 'type',
  14277. ' TObject = class',
  14278. ' const cI: longint = 3;',
  14279. ' procedure Fly;',
  14280. ' procedure Run;',
  14281. ' end;',
  14282. ' TBird = class',
  14283. ' procedure Go;',
  14284. ' end;',
  14285. 'procedure tobject.fly;',
  14286. 'const cI: word = 4;',
  14287. 'begin',
  14288. ' if cI=Self.cI then ;',
  14289. 'end;',
  14290. 'procedure tobject.run;',
  14291. 'const cI: word = 5;',
  14292. 'begin',
  14293. ' if cI=Self.cI then ;',
  14294. 'end;',
  14295. 'procedure tbird.go;',
  14296. 'const cI: word = 6;',
  14297. 'begin',
  14298. ' if cI=Self.cI then ;',
  14299. 'end;',
  14300. 'begin',
  14301. '']);
  14302. ConvertProgram;
  14303. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14304. LinesToStr([
  14305. 'rtl.createClass($mod, "TObject", null, function () {',
  14306. ' this.cI = 3;',
  14307. ' this.$init = function () {',
  14308. ' };',
  14309. ' this.$final = function () {',
  14310. ' };',
  14311. ' var cI$1 = 4;',
  14312. ' this.Fly = function () {',
  14313. ' if (cI$1 === this.cI) ;',
  14314. ' };',
  14315. ' var cI$2 = 5;',
  14316. ' this.Run = function () {',
  14317. ' if (cI$2 === this.cI) ;',
  14318. ' };',
  14319. '});',
  14320. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14321. ' var cI$3 = 6;',
  14322. ' this.Go = function () {',
  14323. ' if (cI$3 === this.cI) ;',
  14324. ' };',
  14325. '});',
  14326. '']),
  14327. LinesToStr([
  14328. '']));
  14329. end;
  14330. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14331. begin
  14332. StartUnit(false);
  14333. Add([
  14334. 'interface',
  14335. 'type',
  14336. ' TObject = class',
  14337. ' const cI: longint = 3;',
  14338. ' procedure Fly;',
  14339. ' procedure Run;',
  14340. ' end;',
  14341. ' TBird = class',
  14342. ' procedure Go;',
  14343. ' end;',
  14344. 'implementation',
  14345. 'procedure tobject.fly;',
  14346. 'const cI: word = 4;',
  14347. 'begin',
  14348. ' if cI=Self.cI then ;',
  14349. 'end;',
  14350. 'procedure tobject.run;',
  14351. 'const cI: word = 5;',
  14352. 'begin',
  14353. ' if cI=Self.cI then ;',
  14354. 'end;',
  14355. 'procedure tbird.go;',
  14356. 'const cI: word = 6;',
  14357. 'begin',
  14358. ' if cI=Self.cI then ;',
  14359. 'end;',
  14360. '']);
  14361. ConvertUnit;
  14362. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14363. LinesToStr([
  14364. 'rtl.createClass($mod, "TObject", null, function () {',
  14365. ' this.cI = 3;',
  14366. ' this.$init = function () {',
  14367. ' };',
  14368. ' this.$final = function () {',
  14369. ' };',
  14370. ' var cI$1 = 4;',
  14371. ' this.Fly = function () {',
  14372. ' if (cI$1 === this.cI) ;',
  14373. ' };',
  14374. ' var cI$2 = 5;',
  14375. ' this.Run = function () {',
  14376. ' if (cI$2 === this.cI) ;',
  14377. ' };',
  14378. '});',
  14379. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14380. ' var cI$3 = 6;',
  14381. ' this.Go = function () {',
  14382. ' if (cI$3 === this.cI) ;',
  14383. ' };',
  14384. '});',
  14385. '']),
  14386. '',
  14387. '');
  14388. end;
  14389. procedure TTestModule.TestClass_LocalVarSelfFail;
  14390. begin
  14391. StartProgram(false);
  14392. Add([
  14393. 'type',
  14394. ' TObject = class',
  14395. ' constructor Create;',
  14396. ' end;',
  14397. 'constructor tobject.create;',
  14398. 'var self: longint;',
  14399. 'begin',
  14400. 'end',
  14401. 'begin',
  14402. '']);
  14403. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14404. ConvertProgram;
  14405. end;
  14406. procedure TTestModule.TestClass_ArgSelfFail;
  14407. begin
  14408. StartProgram(false);
  14409. Add([
  14410. 'type',
  14411. ' TObject = class',
  14412. ' procedure DoIt(Self: longint);',
  14413. ' end;',
  14414. 'procedure tobject.doit(self: longint);',
  14415. 'begin',
  14416. 'end',
  14417. 'begin',
  14418. '']);
  14419. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14420. ConvertProgram;
  14421. end;
  14422. procedure TTestModule.TestClass_NestedProcSelf;
  14423. begin
  14424. StartProgram(false);
  14425. Add([
  14426. 'type',
  14427. ' TObject = class',
  14428. ' Key: longint;',
  14429. ' class var State: longint;',
  14430. ' procedure DoIt;',
  14431. ' function GetSize: longint; virtual; abstract;',
  14432. ' procedure SetSize(Value: longint); virtual; abstract;',
  14433. ' property Size: longint read GetSize write SetSize;',
  14434. ' end;',
  14435. 'procedure tobject.doit;',
  14436. ' procedure Sub;',
  14437. ' begin',
  14438. ' key:=key+2;',
  14439. ' self.key:=self.key+3;',
  14440. ' state:=state+4;',
  14441. ' self.state:=self.state+5;',
  14442. ' tobject.state:=tobject.state+6;',
  14443. ' size:=size+7;',
  14444. ' self.size:=self.size+8;',
  14445. ' end;',
  14446. 'begin',
  14447. ' sub;',
  14448. ' key:=key+12;',
  14449. ' self.key:=self.key+13;',
  14450. ' state:=state+14;',
  14451. ' self.state:=self.state+15;',
  14452. ' tobject.state:=tobject.state+16;',
  14453. ' size:=size+17;',
  14454. ' self.size:=self.size+18;',
  14455. 'end;',
  14456. 'begin',
  14457. '']);
  14458. ConvertProgram;
  14459. CheckSource('TestClass_NestedProcSelf',
  14460. LinesToStr([ // statements
  14461. 'rtl.createClass($mod, "TObject", null, function () {',
  14462. ' this.State = 0;',
  14463. ' this.$init = function () {',
  14464. ' this.Key = 0;',
  14465. ' };',
  14466. ' this.$final = function () {',
  14467. ' };',
  14468. ' this.DoIt = function () {',
  14469. ' var $Self = this;',
  14470. ' function Sub() {',
  14471. ' $Self.Key = $Self.Key + 2;',
  14472. ' $Self.Key = $Self.Key + 3;',
  14473. ' $mod.TObject.State = $Self.State + 4;',
  14474. ' $mod.TObject.State = $Self.State + 5;',
  14475. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14476. ' $Self.SetSize($Self.GetSize() + 7);',
  14477. ' $Self.SetSize($Self.GetSize() + 8);',
  14478. ' };',
  14479. ' Sub();',
  14480. ' $Self.Key = $Self.Key + 12;',
  14481. ' $Self.Key = $Self.Key + 13;',
  14482. ' $mod.TObject.State = $Self.State + 14;',
  14483. ' $mod.TObject.State = $Self.State + 15;',
  14484. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14485. ' $Self.SetSize($Self.GetSize() + 17);',
  14486. ' $Self.SetSize($Self.GetSize() + 18);',
  14487. ' };',
  14488. '});',
  14489. '']),
  14490. LinesToStr([ // $mod.$main
  14491. '']));
  14492. end;
  14493. procedure TTestModule.TestClass_NestedProcSelf2;
  14494. begin
  14495. StartProgram(false);
  14496. Add([
  14497. 'type',
  14498. ' TObject = class',
  14499. ' Key: longint;',
  14500. ' class var State: longint;',
  14501. ' function GetSize: longint; virtual; abstract;',
  14502. ' procedure SetSize(Value: longint); virtual; abstract;',
  14503. ' property Size: longint read GetSize write SetSize;',
  14504. ' end;',
  14505. ' TBird = class',
  14506. ' procedure DoIt;',
  14507. ' end;',
  14508. 'procedure tbird.doit;',
  14509. ' procedure Sub;',
  14510. ' begin',
  14511. ' key:=key+2;',
  14512. ' self.key:=self.key+3;',
  14513. ' state:=state+4;',
  14514. ' self.state:=self.state+5;',
  14515. ' tobject.state:=tobject.state+6;',
  14516. ' size:=size+7;',
  14517. ' self.size:=self.size+8;',
  14518. ' end;',
  14519. 'begin',
  14520. ' sub;',
  14521. ' key:=key+12;',
  14522. ' self.key:=self.key+13;',
  14523. ' state:=state+14;',
  14524. ' self.state:=self.state+15;',
  14525. ' tobject.state:=tobject.state+16;',
  14526. ' size:=size+17;',
  14527. ' self.size:=self.size+18;',
  14528. 'end;',
  14529. 'begin',
  14530. '']);
  14531. ConvertProgram;
  14532. CheckSource('TestClass_NestedProcSelf2',
  14533. LinesToStr([ // statements
  14534. 'rtl.createClass($mod, "TObject", null, function () {',
  14535. ' this.State = 0;',
  14536. ' this.$init = function () {',
  14537. ' this.Key = 0;',
  14538. ' };',
  14539. ' this.$final = function () {',
  14540. ' };',
  14541. '});',
  14542. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14543. ' this.DoIt = function () {',
  14544. ' var $Self = this;',
  14545. ' function Sub() {',
  14546. ' $Self.Key = $Self.Key + 2;',
  14547. ' $Self.Key = $Self.Key + 3;',
  14548. ' $mod.TObject.State = $Self.State + 4;',
  14549. ' $mod.TObject.State = $Self.State + 5;',
  14550. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14551. ' $Self.SetSize($Self.GetSize() + 7);',
  14552. ' $Self.SetSize($Self.GetSize() + 8);',
  14553. ' };',
  14554. ' Sub();',
  14555. ' $Self.Key = $Self.Key + 12;',
  14556. ' $Self.Key = $Self.Key + 13;',
  14557. ' $mod.TObject.State = $Self.State + 14;',
  14558. ' $mod.TObject.State = $Self.State + 15;',
  14559. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14560. ' $Self.SetSize($Self.GetSize() + 17);',
  14561. ' $Self.SetSize($Self.GetSize() + 18);',
  14562. ' };',
  14563. '});',
  14564. '']),
  14565. LinesToStr([ // $mod.$main
  14566. '']));
  14567. end;
  14568. procedure TTestModule.TestClass_NestedProcClassSelf;
  14569. begin
  14570. StartProgram(false);
  14571. Add([
  14572. 'type',
  14573. ' TObject = class',
  14574. ' class var State: longint;',
  14575. ' class procedure DoIt;',
  14576. ' class function GetSize: longint; virtual; abstract;',
  14577. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14578. ' class property Size: longint read GetSize write SetSize;',
  14579. ' end;',
  14580. 'class procedure tobject.doit;',
  14581. ' procedure Sub;',
  14582. ' begin',
  14583. ' state:=state+2;',
  14584. ' self.state:=self.state+3;',
  14585. ' tobject.state:=tobject.state+4;',
  14586. ' size:=size+5;',
  14587. ' self.size:=self.size+6;',
  14588. ' tobject.size:=tobject.size+7;',
  14589. ' end;',
  14590. 'begin',
  14591. ' sub;',
  14592. ' state:=state+12;',
  14593. ' self.state:=self.state+13;',
  14594. ' tobject.state:=tobject.state+14;',
  14595. ' size:=size+15;',
  14596. ' self.size:=self.size+16;',
  14597. ' tobject.size:=tobject.size+17;',
  14598. 'end;',
  14599. 'begin',
  14600. '']);
  14601. ConvertProgram;
  14602. CheckSource('TestClass_NestedProcClassSelf',
  14603. LinesToStr([ // statements
  14604. 'rtl.createClass($mod, "TObject", null, function () {',
  14605. ' this.State = 0;',
  14606. ' this.$init = function () {',
  14607. ' };',
  14608. ' this.$final = function () {',
  14609. ' };',
  14610. ' this.DoIt = function () {',
  14611. ' var $Self = this;',
  14612. ' function Sub() {',
  14613. ' $mod.TObject.State = $Self.State + 2;',
  14614. ' $mod.TObject.State = $Self.State + 3;',
  14615. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14616. ' $Self.SetSize($Self.GetSize() + 5);',
  14617. ' $Self.SetSize($Self.GetSize() + 6);',
  14618. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  14619. ' };',
  14620. ' Sub();',
  14621. ' $mod.TObject.State = $Self.State + 12;',
  14622. ' $mod.TObject.State = $Self.State + 13;',
  14623. ' $mod.TObject.State = $mod.TObject.State + 14;',
  14624. ' $Self.SetSize($Self.GetSize() + 15);',
  14625. ' $Self.SetSize($Self.GetSize() + 16);',
  14626. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  14627. ' };',
  14628. '});',
  14629. '']),
  14630. LinesToStr([ // $mod.$main
  14631. '']));
  14632. end;
  14633. procedure TTestModule.TestClass_NestedProcCallInherited;
  14634. begin
  14635. StartProgram(false);
  14636. Add([
  14637. 'type',
  14638. ' TObject = class',
  14639. ' function DoIt(k: boolean): longint; virtual;',
  14640. ' end;',
  14641. ' TBird = class',
  14642. ' function DoIt(k: boolean): longint; override;',
  14643. ' end;',
  14644. 'function tobject.doit(k: boolean): longint;',
  14645. 'begin',
  14646. 'end;',
  14647. 'function tbird.doit(k: boolean): longint;',
  14648. ' procedure Sub;',
  14649. ' begin',
  14650. ' inherited DoIt(true);',
  14651. //' if inherited DoIt(false)=4 then ;',
  14652. ' end;',
  14653. 'begin',
  14654. ' Sub;',
  14655. ' inherited;',
  14656. ' inherited DoIt(true);',
  14657. //' if inherited DoIt(false)=14 then ;',
  14658. 'end;',
  14659. 'begin',
  14660. '']);
  14661. ConvertProgram;
  14662. CheckSource('TestClass_NestedProcCallInherited',
  14663. LinesToStr([ // statements
  14664. 'rtl.createClass($mod, "TObject", null, function () {',
  14665. ' this.$init = function () {',
  14666. ' };',
  14667. ' this.$final = function () {',
  14668. ' };',
  14669. ' this.DoIt = function (k) {',
  14670. ' var Result = 0;',
  14671. ' return Result;',
  14672. ' };',
  14673. '});',
  14674. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14675. ' this.DoIt = function (k) {',
  14676. ' var $Self = this;',
  14677. ' var Result = 0;',
  14678. ' function Sub() {',
  14679. ' $mod.TObject.DoIt.call($Self, true);',
  14680. ' };',
  14681. ' Sub();',
  14682. ' $mod.TObject.DoIt.apply($Self, arguments);',
  14683. ' $mod.TObject.DoIt.call($Self, true);',
  14684. ' return Result;',
  14685. ' };',
  14686. '});',
  14687. '']),
  14688. LinesToStr([ // $mod.$main
  14689. '']));
  14690. end;
  14691. procedure TTestModule.TestClass_TObjectFree;
  14692. begin
  14693. StartProgram(false);
  14694. Add([
  14695. 'type',
  14696. ' TObject = class',
  14697. ' Obj: tobject;',
  14698. ' procedure Free;',
  14699. ' procedure Release;',
  14700. ' end;',
  14701. 'procedure tobject.free;',
  14702. 'begin',
  14703. 'end;',
  14704. 'procedure tobject.release;',
  14705. 'begin',
  14706. ' free;',
  14707. ' if true then free;',
  14708. 'end;',
  14709. 'function DoIt(o: tobject): tobject;',
  14710. 'var l: tobject;',
  14711. 'begin',
  14712. ' o.free;',
  14713. ' o.free();',
  14714. ' l.free;',
  14715. ' l.free();',
  14716. ' o.obj.free;',
  14717. ' o.obj.free();',
  14718. ' with o do obj.free;',
  14719. ' with o do obj.free();',
  14720. ' result.Free;',
  14721. ' result.Free();',
  14722. 'end;',
  14723. 'var o: tobject;',
  14724. ' a: array of tobject;',
  14725. 'begin',
  14726. ' o.free;',
  14727. ' o.obj.free;',
  14728. ' a[1+2].free;',
  14729. '']);
  14730. ConvertProgram;
  14731. CheckSource('TestClass_TObjectFree',
  14732. LinesToStr([ // statements
  14733. 'rtl.createClass($mod, "TObject", null, function () {',
  14734. ' this.$init = function () {',
  14735. ' this.Obj = null;',
  14736. ' };',
  14737. ' this.$final = function () {',
  14738. ' this.Obj = undefined;',
  14739. ' };',
  14740. ' this.Free = function () {',
  14741. ' };',
  14742. ' this.Release = function () {',
  14743. ' this.Free();',
  14744. ' if (true) this.Free();',
  14745. ' };',
  14746. '});',
  14747. 'this.DoIt = function (o) {',
  14748. ' var Result = null;',
  14749. ' var l = null;',
  14750. ' o = rtl.freeLoc(o);',
  14751. ' o = rtl.freeLoc(o);',
  14752. ' l = rtl.freeLoc(l);',
  14753. ' l = rtl.freeLoc(l);',
  14754. ' rtl.free(o, "Obj");',
  14755. ' rtl.free(o, "Obj");',
  14756. ' rtl.free(o, "Obj");',
  14757. ' rtl.free(o, "Obj");',
  14758. ' Result = rtl.freeLoc(Result);',
  14759. ' Result = rtl.freeLoc(Result);',
  14760. ' return Result;',
  14761. '};',
  14762. 'this.o = null;',
  14763. 'this.a = [];',
  14764. '']),
  14765. LinesToStr([ // $mod.$main
  14766. 'rtl.free($mod, "o");',
  14767. 'rtl.free($mod.o, "Obj");',
  14768. 'rtl.free($mod.a, 1 + 2);',
  14769. '']));
  14770. end;
  14771. procedure TTestModule.TestClass_TObjectFree_VarArg;
  14772. begin
  14773. StartProgram(false);
  14774. Add([
  14775. 'type',
  14776. ' TObject = class',
  14777. ' Obj: tobject;',
  14778. ' procedure Free;',
  14779. ' end;',
  14780. 'procedure tobject.free;',
  14781. 'begin',
  14782. 'end;',
  14783. 'procedure DoIt(var o: tobject);',
  14784. 'begin',
  14785. ' o.free;',
  14786. ' o.free();',
  14787. 'end;',
  14788. 'begin',
  14789. '']);
  14790. ConvertProgram;
  14791. CheckSource('TestClass_TObjectFree_VarArg',
  14792. LinesToStr([ // statements
  14793. 'rtl.createClass($mod, "TObject", null, function () {',
  14794. ' this.$init = function () {',
  14795. ' this.Obj = null;',
  14796. ' };',
  14797. ' this.$final = function () {',
  14798. ' this.Obj = undefined;',
  14799. ' };',
  14800. ' this.Free = function () {',
  14801. ' };',
  14802. '});',
  14803. 'this.DoIt = function (o) {',
  14804. ' o.set(rtl.freeLoc(o.get()));',
  14805. ' o.set(rtl.freeLoc(o.get()));',
  14806. '};',
  14807. '']),
  14808. LinesToStr([ // $mod.$main
  14809. '']));
  14810. end;
  14811. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  14812. begin
  14813. StartProgram(false);
  14814. Add([
  14815. 'type',
  14816. ' TObject = class',
  14817. ' constructor Create;',
  14818. ' procedure Free;',
  14819. ' end;',
  14820. 'constructor TObject.Create; begin end;',
  14821. 'procedure tobject.free; begin end;',
  14822. 'begin',
  14823. ' with tobject.create do free;',
  14824. '']);
  14825. ConvertProgram;
  14826. CheckSource('TestClass_TObjectFreeNewInstance',
  14827. LinesToStr([ // statements
  14828. 'rtl.createClass($mod, "TObject", null, function () {',
  14829. ' this.$init = function () {',
  14830. ' };',
  14831. ' this.$final = function () {',
  14832. ' };',
  14833. ' this.Create = function () {',
  14834. ' return this;',
  14835. ' };',
  14836. ' this.Free = function () {',
  14837. ' };',
  14838. '});',
  14839. '']),
  14840. LinesToStr([ // $mod.$main
  14841. 'var $with = $mod.TObject.$create("Create");',
  14842. '$with=rtl.freeLoc($with);',
  14843. '']));
  14844. end;
  14845. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  14846. begin
  14847. StartProgram(false);
  14848. Add([
  14849. 'type',
  14850. ' TObject = class',
  14851. ' destructor Destroy;',
  14852. ' procedure Free;',
  14853. ' end;',
  14854. 'destructor TObject.Destroy; begin end;',
  14855. 'procedure tobject.free; begin end;',
  14856. 'var o: tobject;',
  14857. 'begin',
  14858. ' o.free;',
  14859. '']);
  14860. Converter.UseLowerCase:=true;
  14861. ConvertProgram;
  14862. CheckSource('TestClass_TObjectFreeLowerCase',
  14863. LinesToStr([ // statements
  14864. 'rtl.createClass($mod, "tobject", null, function () {',
  14865. ' this.$init = function () {',
  14866. ' };',
  14867. ' this.$final = function () {',
  14868. ' };',
  14869. ' rtl.tObjectDestroy = "destroy";',
  14870. ' this.destroy = function () {',
  14871. ' };',
  14872. ' this.free = function () {',
  14873. ' };',
  14874. '});',
  14875. 'this.o = null;',
  14876. '']),
  14877. LinesToStr([ // $mod.$main
  14878. 'rtl.free($mod, "o");',
  14879. '']));
  14880. end;
  14881. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  14882. begin
  14883. StartProgram(false);
  14884. Add([
  14885. 'type',
  14886. ' TObject = class',
  14887. ' procedure Free;',
  14888. ' function GetObj: tobject; virtual; abstract;',
  14889. ' end;',
  14890. 'procedure tobject.free;',
  14891. 'begin',
  14892. 'end;',
  14893. 'var o: tobject;',
  14894. 'begin',
  14895. ' o.getobj.free;',
  14896. '']);
  14897. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14898. ConvertProgram;
  14899. end;
  14900. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  14901. begin
  14902. StartProgram(false);
  14903. Add([
  14904. 'type',
  14905. ' TObject = class',
  14906. ' procedure Free;',
  14907. ' FObj: TObject;',
  14908. ' property Obj: tobject read FObj write FObj;',
  14909. ' end;',
  14910. 'procedure tobject.free;',
  14911. 'begin',
  14912. 'end;',
  14913. 'var o: tobject;',
  14914. 'begin',
  14915. ' o.obj.free;',
  14916. '']);
  14917. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  14918. ConvertProgram;
  14919. end;
  14920. procedure TTestModule.TestClass_ForIn;
  14921. begin
  14922. StartProgram(false);
  14923. Add([
  14924. 'type',
  14925. ' TObject = class end;',
  14926. ' TItem = TObject;',
  14927. ' TEnumerator = class',
  14928. ' FCurrent: TItem;',
  14929. ' property Current: TItem read FCurrent;',
  14930. ' function MoveNext: boolean;',
  14931. ' end;',
  14932. ' TBird = class',
  14933. ' function GetEnumerator: TEnumerator;',
  14934. ' end;',
  14935. 'function TEnumerator.MoveNext: boolean;',
  14936. 'begin',
  14937. 'end;',
  14938. 'function TBird.GetEnumerator: TEnumerator;',
  14939. 'begin',
  14940. 'end;',
  14941. 'var',
  14942. ' b: TBird;',
  14943. ' i, i2: TItem;',
  14944. 'begin',
  14945. ' for i in b do i2:=i;']);
  14946. ConvertProgram;
  14947. CheckSource('TestClass_ForIn',
  14948. LinesToStr([ // statements
  14949. 'rtl.createClass($mod, "TObject", null, function () {',
  14950. ' this.$init = function () {',
  14951. ' };',
  14952. ' this.$final = function () {',
  14953. ' };',
  14954. '});',
  14955. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  14956. ' this.$init = function () {',
  14957. ' $mod.TObject.$init.call(this);',
  14958. ' this.FCurrent = null;',
  14959. ' };',
  14960. ' this.$final = function () {',
  14961. ' this.FCurrent = undefined;',
  14962. ' $mod.TObject.$final.call(this);',
  14963. ' };',
  14964. ' this.MoveNext = function () {',
  14965. ' var Result = false;',
  14966. ' return Result;',
  14967. ' };',
  14968. '});',
  14969. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  14970. ' this.GetEnumerator = function () {',
  14971. ' var Result = null;',
  14972. ' return Result;',
  14973. ' };',
  14974. '});',
  14975. 'this.b = null;',
  14976. 'this.i = null;',
  14977. 'this.i2 = null;'
  14978. ]),
  14979. LinesToStr([ // $mod.$main
  14980. 'var $in = $mod.b.GetEnumerator();',
  14981. 'try {',
  14982. ' while ($in.MoveNext()){',
  14983. ' $mod.i = $in.FCurrent;',
  14984. ' $mod.i2 = $mod.i;',
  14985. ' }',
  14986. '} finally {',
  14987. ' $in = rtl.freeLoc($in)',
  14988. '};',
  14989. '']));
  14990. end;
  14991. procedure TTestModule.TestClass_DispatchMessage;
  14992. begin
  14993. StartProgram(false);
  14994. Add([
  14995. 'type',
  14996. ' TObject = class',
  14997. ' {$DispatchField DispInt}',
  14998. ' procedure Dispatch(var Msg); virtual; abstract;',
  14999. ' {$DispatchStrField DispStr}',
  15000. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15001. ' end;',
  15002. ' THopMsg = record',
  15003. ' DispInt: longint;',
  15004. ' end;',
  15005. ' TPutMsg = record',
  15006. ' DispStr: string;',
  15007. ' end;',
  15008. ' TBird = class',
  15009. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15010. ' procedure Run; overload; virtual; abstract;',
  15011. ' procedure Run(var Msg); overload; message ''Fast'';',
  15012. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15013. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15014. ' end;',
  15015. 'procedure TBird.Run(var Msg);',
  15016. 'begin',
  15017. 'end;',
  15018. 'begin',
  15019. '']);
  15020. ConvertProgram;
  15021. CheckSource('TestClass_Message',
  15022. LinesToStr([ // statements
  15023. 'rtl.createClass($mod, "TObject", null, function () {',
  15024. ' this.$init = function () {',
  15025. ' };',
  15026. ' this.$final = function () {',
  15027. ' };',
  15028. '});',
  15029. 'rtl.recNewT($mod, "THopMsg", function () {',
  15030. ' this.DispInt = 0;',
  15031. ' this.$eq = function (b) {',
  15032. ' return this.DispInt === b.DispInt;',
  15033. ' };',
  15034. ' this.$assign = function (s) {',
  15035. ' this.DispInt = s.DispInt;',
  15036. ' return this;',
  15037. ' };',
  15038. '});',
  15039. 'rtl.recNewT($mod, "TPutMsg", function () {',
  15040. ' this.DispStr = "";',
  15041. ' this.$eq = function (b) {',
  15042. ' return this.DispStr === b.DispStr;',
  15043. ' };',
  15044. ' this.$assign = function (s) {',
  15045. ' this.DispStr = s.DispStr;',
  15046. ' return this;',
  15047. ' };',
  15048. '});',
  15049. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15050. ' this.Run$1 = function (Msg) {',
  15051. ' };',
  15052. ' this.$msgint = {',
  15053. ' "2": "Fly",',
  15054. ' "3": "Hop"',
  15055. ' };',
  15056. ' this.$msgstr = {',
  15057. ' Fast: "Run$1",',
  15058. ' foo: "Put"',
  15059. ' };',
  15060. '});',
  15061. '']),
  15062. LinesToStr([ // $mod.$main
  15063. '']));
  15064. end;
  15065. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15066. begin
  15067. StartProgram(false);
  15068. Add([
  15069. 'type',
  15070. ' TObject = class',
  15071. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15072. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15073. ' end;',
  15074. 'begin',
  15075. '']);
  15076. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15077. ConvertProgram;
  15078. end;
  15079. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15080. begin
  15081. StartProgram(false);
  15082. Add([
  15083. 'type',
  15084. ' TObject = class',
  15085. ' {$dispatchfield Msg}',
  15086. ' procedure Dispatch(var Msg); virtual; abstract;',
  15087. ' end;',
  15088. ' TFlyMsg = record',
  15089. ' FlyId: longint;',
  15090. ' end;',
  15091. ' TBird = class',
  15092. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15093. ' end;',
  15094. 'begin',
  15095. '']);
  15096. ConvertProgram;
  15097. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15098. end;
  15099. procedure TTestModule.TestClassOf_Create;
  15100. begin
  15101. StartProgram(false);
  15102. Add('type');
  15103. Add(' TObject = class');
  15104. Add(' constructor Create;');
  15105. Add(' end;');
  15106. Add(' TClass = class of TObject;');
  15107. Add('constructor tobject.create; begin end;');
  15108. Add('var');
  15109. Add(' Obj: tobject;');
  15110. Add(' C: tclass;');
  15111. Add('begin');
  15112. Add(' obj:=C.create;');
  15113. Add(' with c do obj:=create;');
  15114. ConvertProgram;
  15115. CheckSource('TestClassOf_Create',
  15116. LinesToStr([ // statements
  15117. 'rtl.createClass($mod, "TObject", null, function () {',
  15118. ' this.$init = function () {',
  15119. ' };',
  15120. ' this.$final = function () {',
  15121. ' };',
  15122. ' this.Create = function () {',
  15123. ' return this;',
  15124. ' };',
  15125. '});',
  15126. 'this.Obj = null;',
  15127. 'this.C = null;'
  15128. ]),
  15129. LinesToStr([ // $mod.$main
  15130. '$mod.Obj = $mod.C.$create("Create");',
  15131. 'var $with = $mod.C;',
  15132. '$mod.Obj = $with.$create("Create");',
  15133. '']));
  15134. end;
  15135. procedure TTestModule.TestClassOf_Call;
  15136. begin
  15137. StartProgram(false);
  15138. Add('type');
  15139. Add(' TObject = class');
  15140. Add(' class procedure DoIt;');
  15141. Add(' end;');
  15142. Add(' TClass = class of TObject;');
  15143. Add('class procedure tobject.doit; begin end;');
  15144. Add('var');
  15145. Add(' C: tclass;');
  15146. Add('begin');
  15147. Add(' c.doit;');
  15148. Add(' with c do doit;');
  15149. ConvertProgram;
  15150. CheckSource('TestClassOf_Call',
  15151. LinesToStr([ // statements
  15152. 'rtl.createClass($mod, "TObject", null, function () {',
  15153. ' this.$init = function () {',
  15154. ' };',
  15155. ' this.$final = function () {',
  15156. ' };',
  15157. ' this.DoIt = function () {',
  15158. ' };',
  15159. '});',
  15160. 'this.C = null;'
  15161. ]),
  15162. LinesToStr([ // $mod.$main
  15163. '$mod.C.DoIt();',
  15164. 'var $with = $mod.C;',
  15165. '$with.DoIt();',
  15166. '']));
  15167. end;
  15168. procedure TTestModule.TestClassOf_Assign;
  15169. begin
  15170. StartProgram(false);
  15171. Add('type');
  15172. Add(' TClass = class of TObject;');
  15173. Add(' TObject = class');
  15174. Add(' ClassType: TClass; ');
  15175. Add(' end;');
  15176. Add('var');
  15177. Add(' Obj: tobject;');
  15178. Add(' C: tclass;');
  15179. Add('begin');
  15180. Add(' c:=nil;');
  15181. Add(' c:=obj.classtype;');
  15182. ConvertProgram;
  15183. CheckSource('TestClassOf_Assign',
  15184. LinesToStr([ // statements
  15185. 'rtl.createClass($mod, "TObject", null, function () {',
  15186. ' this.$init = function () {',
  15187. ' this.ClassType = null;',
  15188. ' };',
  15189. ' this.$final = function () {',
  15190. ' this.ClassType = undefined;',
  15191. ' };',
  15192. '});',
  15193. 'this.Obj = null;',
  15194. 'this.C = null;'
  15195. ]),
  15196. LinesToStr([ // $mod.$main
  15197. '$mod.C = null;',
  15198. '$mod.C = $mod.Obj.ClassType;',
  15199. '']));
  15200. end;
  15201. procedure TTestModule.TestClassOf_Is;
  15202. begin
  15203. StartProgram(false);
  15204. Add('type');
  15205. Add(' TClass = class of TObject;');
  15206. Add(' TObject = class');
  15207. Add(' end;');
  15208. Add(' TCar = class');
  15209. Add(' end;');
  15210. Add(' TCars = class of TCar;');
  15211. Add('var');
  15212. Add(' Obj: tobject;');
  15213. Add(' C: tclass;');
  15214. Add(' Cars: tcars;');
  15215. Add('begin');
  15216. Add(' if c is tcar then ;');
  15217. Add(' if c is tcars then ;');
  15218. ConvertProgram;
  15219. CheckSource('TestClassOf_Is',
  15220. LinesToStr([ // statements
  15221. 'rtl.createClass($mod, "TObject", null, function () {',
  15222. ' this.$init = function () {',
  15223. ' };',
  15224. ' this.$final = function () {',
  15225. ' };',
  15226. '});',
  15227. 'rtl.createClass($mod, "TCar", $mod.TObject, function () {',
  15228. '});',
  15229. 'this.Obj = null;',
  15230. 'this.C = null;',
  15231. 'this.Cars = null;'
  15232. ]),
  15233. LinesToStr([ // $mod.$main
  15234. 'if(rtl.is($mod.C,$mod.TCar));',
  15235. 'if(rtl.is($mod.C,$mod.TCar));',
  15236. '']));
  15237. end;
  15238. procedure TTestModule.TestClassOf_Compare;
  15239. begin
  15240. StartProgram(false);
  15241. Add('type');
  15242. Add(' TClass = class of TObject;');
  15243. Add(' TObject = class');
  15244. Add(' ClassType: TClass; ');
  15245. Add(' end;');
  15246. Add('var');
  15247. Add(' b: boolean;');
  15248. Add(' Obj: tobject;');
  15249. Add(' C: tclass;');
  15250. Add('begin');
  15251. Add(' b:=c=nil;');
  15252. Add(' b:=nil=c;');
  15253. Add(' b:=c=obj.classtype;');
  15254. Add(' b:=obj.classtype=c;');
  15255. Add(' b:=c=TObject;');
  15256. Add(' b:=TObject=c;');
  15257. Add(' b:=c<>nil;');
  15258. Add(' b:=nil<>c;');
  15259. Add(' b:=c<>obj.classtype;');
  15260. Add(' b:=obj.classtype<>c;');
  15261. Add(' b:=c<>TObject;');
  15262. Add(' b:=TObject<>c;');
  15263. ConvertProgram;
  15264. CheckSource('TestClassOf_Compare',
  15265. LinesToStr([ // statements
  15266. 'rtl.createClass($mod, "TObject", null, function () {',
  15267. ' this.$init = function () {',
  15268. ' this.ClassType = null;',
  15269. ' };',
  15270. ' this.$final = function () {',
  15271. ' this.ClassType = undefined;',
  15272. ' };',
  15273. '});',
  15274. 'this.b = false;',
  15275. 'this.Obj = null;',
  15276. 'this.C = null;'
  15277. ]),
  15278. LinesToStr([ // $mod.$main
  15279. '$mod.b = $mod.C === null;',
  15280. '$mod.b = null === $mod.C;',
  15281. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15282. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15283. '$mod.b = $mod.C === $mod.TObject;',
  15284. '$mod.b = $mod.TObject === $mod.C;',
  15285. '$mod.b = $mod.C !== null;',
  15286. '$mod.b = null !== $mod.C;',
  15287. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15288. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15289. '$mod.b = $mod.C !== $mod.TObject;',
  15290. '$mod.b = $mod.TObject !== $mod.C;',
  15291. '']));
  15292. end;
  15293. procedure TTestModule.TestClassOf_ClassVar;
  15294. begin
  15295. StartProgram(false);
  15296. Add('type');
  15297. Add(' TObject = class');
  15298. Add(' class var id: longint;');
  15299. Add(' end;');
  15300. Add(' TClass = class of TObject;');
  15301. Add('var');
  15302. Add(' C: tclass;');
  15303. Add('begin');
  15304. Add(' C.id:=C.id;');
  15305. ConvertProgram;
  15306. CheckSource('TestClassOf_ClassVar',
  15307. LinesToStr([ // statements
  15308. 'rtl.createClass($mod, "TObject", null, function () {',
  15309. ' this.id = 0;',
  15310. ' this.$init = function () {',
  15311. ' };',
  15312. ' this.$final = function () {',
  15313. ' };',
  15314. '});',
  15315. 'this.C = null;'
  15316. ]),
  15317. LinesToStr([ // $mod.$main
  15318. '$mod.TObject.id = $mod.C.id;',
  15319. '']));
  15320. end;
  15321. procedure TTestModule.TestClassOf_ClassMethod;
  15322. begin
  15323. StartProgram(false);
  15324. Add('type');
  15325. Add(' TObject = class');
  15326. Add(' class function DoIt(i: longint = 0): longint;');
  15327. Add(' end;');
  15328. Add(' TClass = class of TObject;');
  15329. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15330. Add('var');
  15331. Add(' i: longint;');
  15332. Add(' C: tclass;');
  15333. Add('begin');
  15334. Add(' C.DoIt;');
  15335. Add(' C.DoIt();');
  15336. Add(' i:=C.DoIt;');
  15337. Add(' i:=C.DoIt();');
  15338. ConvertProgram;
  15339. CheckSource('TestClassOf_ClassMethod',
  15340. LinesToStr([ // statements
  15341. 'rtl.createClass($mod, "TObject", null, function () {',
  15342. ' this.$init = function () {',
  15343. ' };',
  15344. ' this.$final = function () {',
  15345. ' };',
  15346. ' this.DoIt = function (i) {',
  15347. ' var Result = 0;',
  15348. ' return Result;',
  15349. ' };',
  15350. '});',
  15351. 'this.i = 0;',
  15352. 'this.C = null;'
  15353. ]),
  15354. LinesToStr([ // $mod.$main
  15355. '$mod.C.DoIt(0);',
  15356. '$mod.C.DoIt(0);',
  15357. '$mod.i = $mod.C.DoIt(0);',
  15358. '$mod.i = $mod.C.DoIt(0);',
  15359. '']));
  15360. end;
  15361. procedure TTestModule.TestClassOf_ClassProperty;
  15362. begin
  15363. StartProgram(false);
  15364. Add([
  15365. 'type',
  15366. ' TObject = class',
  15367. ' class var FA: longint;',
  15368. ' class function GetA: longint;',
  15369. ' class procedure SetA(Value: longint);',
  15370. ' class property pA: longint read fa write fa;',
  15371. ' class property pB: longint read geta write seta;',
  15372. ' end;',
  15373. ' TObjectClass = class of tobject;',
  15374. 'class function tobject.geta: longint; begin end;',
  15375. 'class procedure tobject.seta(value: longint); begin end;',
  15376. 'var',
  15377. ' b: boolean;',
  15378. ' Obj: tobject;',
  15379. ' Cla: tobjectclass;',
  15380. 'begin',
  15381. ' obj.pa:=obj.pa;',
  15382. ' obj.pb:=obj.pb;',
  15383. ' b:=obj.pa=4;',
  15384. ' b:=obj.pb=obj.pb;',
  15385. ' b:=5=obj.pa;',
  15386. ' cla.pa:=6;',
  15387. ' cla.pa:=cla.pa;',
  15388. ' cla.pb:=cla.pb;',
  15389. ' b:=cla.pa=7;',
  15390. ' b:=cla.pb=cla.pb;',
  15391. ' b:=8=cla.pa;',
  15392. ' tobject.pa:=9;',
  15393. ' tobject.pb:=tobject.pb;',
  15394. ' b:=tobject.pa=10;',
  15395. ' b:=11=tobject.pa;',
  15396. '']);
  15397. ConvertProgram;
  15398. CheckSource('TestClassOf_ClassProperty',
  15399. LinesToStr([ // statements
  15400. 'rtl.createClass($mod, "TObject", null, function () {',
  15401. ' this.FA = 0;',
  15402. ' this.$init = function () {',
  15403. ' };',
  15404. ' this.$final = function () {',
  15405. ' };',
  15406. ' this.GetA = function () {',
  15407. ' var Result = 0;',
  15408. ' return Result;',
  15409. ' };',
  15410. ' this.SetA = function (Value) {',
  15411. ' };',
  15412. '});',
  15413. 'this.b = false;',
  15414. 'this.Obj = null;',
  15415. 'this.Cla = null;'
  15416. ]),
  15417. LinesToStr([ // $mod.$main
  15418. '$mod.TObject.FA = $mod.Obj.FA;',
  15419. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15420. '$mod.b = $mod.Obj.FA === 4;',
  15421. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15422. '$mod.b = 5 === $mod.Obj.FA;',
  15423. '$mod.TObject.FA = 6;',
  15424. '$mod.TObject.FA = $mod.Cla.FA;',
  15425. '$mod.Cla.SetA($mod.Cla.GetA());',
  15426. '$mod.b = $mod.Cla.FA === 7;',
  15427. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15428. '$mod.b = 8 === $mod.Cla.FA;',
  15429. '$mod.TObject.FA = 9;',
  15430. '$mod.TObject.SetA($mod.TObject.GetA());',
  15431. '$mod.b = $mod.TObject.FA === 10;',
  15432. '$mod.b = 11 === $mod.TObject.FA;',
  15433. '']));
  15434. end;
  15435. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15436. begin
  15437. StartProgram(false);
  15438. Add('type');
  15439. Add(' TObject = class');
  15440. Add(' class var GlobalId: longint;');
  15441. Add(' class procedure ProcA;');
  15442. Add(' end;');
  15443. Add('class procedure tobject.proca;');
  15444. Add('var b: boolean;');
  15445. Add('begin');
  15446. Add(' b:=self=nil;');
  15447. Add(' b:=self.globalid=3;');
  15448. Add(' b:=4=self.globalid;');
  15449. Add(' self.globalid:=5;');
  15450. Add(' self.proca;');
  15451. Add('end;');
  15452. Add('begin');
  15453. ConvertProgram;
  15454. CheckSource('TestClassOf_ClassMethodSelf',
  15455. LinesToStr([ // statements
  15456. 'rtl.createClass($mod, "TObject", null, function () {',
  15457. ' this.GlobalId = 0;',
  15458. ' this.$init = function () {',
  15459. ' };',
  15460. ' this.$final = function () {',
  15461. ' };',
  15462. ' this.ProcA = function () {',
  15463. ' var b = false;',
  15464. ' b = this === null;',
  15465. ' b = this.GlobalId === 3;',
  15466. ' b = 4 === this.GlobalId;',
  15467. ' $mod.TObject.GlobalId = 5;',
  15468. ' this.ProcA();',
  15469. ' };',
  15470. '});'
  15471. ]),
  15472. LinesToStr([ // $mod.$main
  15473. '']));
  15474. end;
  15475. procedure TTestModule.TestClassOf_TypeCast;
  15476. begin
  15477. StartProgram(false);
  15478. Add('type');
  15479. Add(' TObject = class');
  15480. Add(' class procedure {#TObject_DoIt}DoIt;');
  15481. Add(' end;');
  15482. Add(' TClass = class of TObject;');
  15483. Add(' TMobile = class');
  15484. Add(' class procedure {#TMobile_DoIt}DoIt;');
  15485. Add(' end;');
  15486. Add(' TMobileClass = class of TMobile;');
  15487. Add(' TCar = class(TMobile)');
  15488. Add(' class procedure {#TCar_DoIt}DoIt;');
  15489. Add(' end;');
  15490. Add(' TCarClass = class of TCar;');
  15491. Add('class procedure TObject.DoIt;');
  15492. Add('begin');
  15493. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15494. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15495. Add('end;');
  15496. Add('class procedure TMobile.DoIt;');
  15497. Add('begin');
  15498. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15499. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15500. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  15501. Add('end;');
  15502. Add('class procedure TCar.DoIt; begin end;');
  15503. Add('var');
  15504. Add(' ObjC: TClass;');
  15505. Add(' MobileC: TMobileClass;');
  15506. Add(' CarC: TCarClass;');
  15507. Add('begin');
  15508. Add(' ObjC.{@TObject_DoIt}DoIt;');
  15509. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  15510. Add(' CarC.{@TCar_DoIt}DoIt;');
  15511. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  15512. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  15513. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  15514. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  15515. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  15516. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  15517. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  15518. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  15519. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  15520. ConvertProgram;
  15521. CheckSource('TestClassOf_TypeCast',
  15522. LinesToStr([ // statements
  15523. 'rtl.createClass($mod, "TObject", null, function () {',
  15524. ' this.$init = function () {',
  15525. ' };',
  15526. ' this.$final = function () {',
  15527. ' };',
  15528. ' this.DoIt = function () {',
  15529. ' this.DoIt();',
  15530. ' this.DoIt$1();',
  15531. ' };',
  15532. '});',
  15533. 'rtl.createClass($mod, "TMobile", $mod.TObject, function () {',
  15534. ' this.DoIt$1 = function () {',
  15535. ' this.DoIt();',
  15536. ' this.DoIt$1();',
  15537. ' this.DoIt$2();',
  15538. ' };',
  15539. '});',
  15540. 'rtl.createClass($mod, "TCar", $mod.TMobile, function () {',
  15541. ' this.DoIt$2 = function () {',
  15542. ' };',
  15543. '});',
  15544. 'this.ObjC = null;',
  15545. 'this.MobileC = null;',
  15546. 'this.CarC = null;',
  15547. '']),
  15548. LinesToStr([ // $mod.$main
  15549. '$mod.ObjC.DoIt();',
  15550. '$mod.MobileC.DoIt$1();',
  15551. '$mod.CarC.DoIt$2();',
  15552. '$mod.ObjC.DoIt();',
  15553. '$mod.ObjC.DoIt$1();',
  15554. '$mod.ObjC.DoIt$2();',
  15555. '$mod.MobileC.DoIt();',
  15556. '$mod.MobileC.DoIt$1();',
  15557. '$mod.MobileC.DoIt$2();',
  15558. '$mod.CarC.DoIt();',
  15559. '$mod.CarC.DoIt$1();',
  15560. '$mod.CarC.DoIt$2();',
  15561. '']));
  15562. end;
  15563. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  15564. begin
  15565. StartProgram(false);
  15566. Add('type');
  15567. Add(' TObject = class');
  15568. Add(' function CurNow: longint; ');
  15569. Add(' class function Now: longint; ');
  15570. Add(' end;');
  15571. Add('function TObject.CurNow: longint; begin end;');
  15572. Add('class function TObject.Now: longint; begin end;');
  15573. Add('var');
  15574. Add(' Obj: tobject;');
  15575. Add(' vI: longint;');
  15576. Add('begin');
  15577. Add(' obj.curnow;');
  15578. Add(' vi:=obj.curnow;');
  15579. Add(' tobject.now;');
  15580. Add(' vi:=tobject.now;');
  15581. ConvertProgram;
  15582. CheckSource('TestClassOf_ImplicitFunctionCall',
  15583. LinesToStr([ // statements
  15584. 'rtl.createClass($mod, "TObject", null, function () {',
  15585. ' this.$init = function () {',
  15586. ' };',
  15587. ' this.$final = function () {',
  15588. ' };',
  15589. ' this.CurNow = function () {',
  15590. ' var Result = 0;',
  15591. ' return Result;',
  15592. ' };',
  15593. ' this.Now = function () {',
  15594. ' var Result = 0;',
  15595. ' return Result;',
  15596. ' };',
  15597. '});',
  15598. 'this.Obj = null;',
  15599. 'this.vI = 0;',
  15600. '']),
  15601. LinesToStr([ // $mod.$main
  15602. '$mod.Obj.CurNow();',
  15603. '$mod.vI = $mod.Obj.CurNow();',
  15604. '$mod.TObject.Now();',
  15605. '$mod.vI = $mod.TObject.Now();',
  15606. '']));
  15607. end;
  15608. procedure TTestModule.TestClassOf_Const;
  15609. begin
  15610. StartProgram(false);
  15611. Add([
  15612. 'type',
  15613. ' TObject = class',
  15614. ' end;',
  15615. ' TBird = TObject;',
  15616. ' TBirds = class of TBird;',
  15617. ' TEagles = TBirds;',
  15618. ' THawk = class(TBird);',
  15619. 'const',
  15620. ' Hawk: TEagles = THawk;',
  15621. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  15622. ' TBird,',
  15623. ' THawk',
  15624. ' );',
  15625. 'begin']);
  15626. ConvertProgram;
  15627. CheckSource('TestClassOf_Const',
  15628. LinesToStr([ // statements
  15629. 'rtl.createClass($mod, "TObject", null, function () {',
  15630. ' this.$init = function () {',
  15631. ' };',
  15632. ' this.$final = function () {',
  15633. ' };',
  15634. '});',
  15635. 'rtl.createClass($mod, "THawk", $mod.TObject, function () {',
  15636. '});',
  15637. 'this.Hawk = $mod.THawk;',
  15638. 'this.DefaultBirdClasses = [$mod.TObject, $mod.THawk];',
  15639. '']),
  15640. LinesToStr([ // $mod.$main
  15641. '']));
  15642. end;
  15643. procedure TTestModule.TestNestedClass_Alias;
  15644. begin
  15645. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15646. StartProgram(false);
  15647. Add([
  15648. 'type',
  15649. ' TObject = class',
  15650. ' type TNested = type longint;',
  15651. ' end;',
  15652. 'type TAlias = type tobject.tnested;',
  15653. 'var i: tobject.tnested = 3;',
  15654. 'var j: TAlias = 4;',
  15655. 'begin',
  15656. ' if typeinfo(TAlias)=nil then ;',
  15657. ' if typeinfo(tobject.tnested)=nil then ;',
  15658. '']);
  15659. ConvertProgram;
  15660. CheckSource('TestNestedClass_Alias',
  15661. LinesToStr([ // statements
  15662. 'rtl.createClass($mod, "TObject", null, function () {',
  15663. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  15664. ' this.$init = function () {',
  15665. ' };',
  15666. ' this.$final = function () {',
  15667. ' };',
  15668. '});',
  15669. '$mod.$rtti.$inherited("TAlias", $mod.$rtti["TObject.TNested"], {});',
  15670. 'this.i = 3;',
  15671. 'this.j = 4;',
  15672. '']),
  15673. LinesToStr([ // $mod.$main
  15674. 'if ($mod.$rtti["TAlias"] === null) ;',
  15675. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  15676. '']));
  15677. end;
  15678. procedure TTestModule.TestNestedClass_Record;
  15679. begin
  15680. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15681. StartProgram(false);
  15682. Add([
  15683. 'type',
  15684. ' TObject = class',
  15685. ' type TPoint = record',
  15686. ' x,y: byte;',
  15687. ' end;',
  15688. ' procedure DoIt(t: TPoint);',
  15689. ' end;',
  15690. 'procedure tobject.DoIt(t: TPoint);',
  15691. 'var p: TPoint;',
  15692. 'begin',
  15693. ' t.x:=t.y;',
  15694. ' p:=t;',
  15695. 'end;',
  15696. 'var',
  15697. ' p: tobject.tpoint = (x:2; y:4);',
  15698. ' o: TObject;',
  15699. 'begin',
  15700. ' p:=p;',
  15701. ' o.doit(p);',
  15702. '']);
  15703. ConvertProgram;
  15704. CheckSource('TestNestedClass_Record',
  15705. LinesToStr([ // statements
  15706. 'rtl.createClass($mod, "TObject", null, function () {',
  15707. ' rtl.recNewT(this, "TPoint", function () {',
  15708. ' this.x = 0;',
  15709. ' this.y = 0;',
  15710. ' this.$eq = function (b) {',
  15711. ' return (this.x === b.x) && (this.y === b.y);',
  15712. ' };',
  15713. ' this.$assign = function (s) {',
  15714. ' this.x = s.x;',
  15715. ' this.y = s.y;',
  15716. ' return this;',
  15717. ' };',
  15718. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  15719. ' $r.addField("x", rtl.byte);',
  15720. ' $r.addField("y", rtl.byte);',
  15721. ' });',
  15722. ' this.$init = function () {',
  15723. ' };',
  15724. ' this.$final = function () {',
  15725. ' };',
  15726. ' this.DoIt = function (t) {',
  15727. ' var p = this.TPoint.$new();',
  15728. ' t.x = t.y;',
  15729. ' p.$assign(t);',
  15730. ' };',
  15731. '});',
  15732. 'this.p = $mod.TObject.TPoint.$clone({',
  15733. ' x: 2,',
  15734. ' y: 4',
  15735. '});',
  15736. 'this.o = null;',
  15737. '']),
  15738. LinesToStr([ // $mod.$main
  15739. '$mod.p.$assign($mod.p);',
  15740. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  15741. '']));
  15742. end;
  15743. procedure TTestModule.TestNestedClass_Class;
  15744. begin
  15745. Converter.Options:=Converter.Options-[coNoTypeInfo];
  15746. StartProgram(false);
  15747. Add([
  15748. 'type',
  15749. ' TObject = class end;',
  15750. ' TBird = class',
  15751. ' type TLeg = class',
  15752. ' FId: longint;',
  15753. ' constructor Create;',
  15754. ' function Create(i: longint): TLeg;',
  15755. ' end;',
  15756. ' function DoIt(b: TBird): Tleg;',
  15757. ' end;',
  15758. 'constructor tbird.tleg.create;',
  15759. 'begin',
  15760. ' FId:=3;',
  15761. 'end;',
  15762. 'function tbird.tleg.Create(i: longint): TLeg;',
  15763. 'begin',
  15764. ' Create;',
  15765. ' Result:=TLeg.Create;',
  15766. ' Result:=TBird.TLeg.Create;',
  15767. ' Result:=Create(3);',
  15768. ' FId:=i;',
  15769. 'end;',
  15770. 'function tbird.DoIt(b: tbird): tleg;',
  15771. 'begin',
  15772. ' Result.Create;',
  15773. ' Result:=TLeg.Create;',
  15774. ' Result:=TBird.TLeg.Create;',
  15775. ' Result:=Result.Create(3);',
  15776. 'end;',
  15777. 'var',
  15778. ' b: Tbird.tleg;',
  15779. 'begin',
  15780. ' b.Create;',
  15781. ' b:=TBird.TLeg.Create;',
  15782. ' b:=b.Create(3);',
  15783. '']);
  15784. ConvertProgram;
  15785. CheckSource('TestNestedClass_Class',
  15786. LinesToStr([ // statements
  15787. 'rtl.createClass($mod, "TObject", null, function () {',
  15788. ' this.$init = function () {',
  15789. ' };',
  15790. ' this.$final = function () {',
  15791. ' };',
  15792. '});',
  15793. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  15794. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  15795. ' this.$init = function () {',
  15796. ' $mod.TObject.$init.call(this);',
  15797. ' this.FId = 0;',
  15798. ' };',
  15799. ' this.Create = function () {',
  15800. ' this.FId = 3;',
  15801. ' return this;',
  15802. ' };',
  15803. ' this.Create$1 = function (i) {',
  15804. ' var Result = null;',
  15805. ' this.Create();',
  15806. ' Result = $mod.TBird.TLeg.$create("Create");',
  15807. ' Result = $mod.TBird.TLeg.$create("Create");',
  15808. ' Result = this.Create$1(3);',
  15809. ' this.FId = i;',
  15810. ' return Result;',
  15811. ' };',
  15812. ' });',
  15813. ' this.DoIt = function (b) {',
  15814. ' var Result = null;',
  15815. ' Result.Create();',
  15816. ' Result = this.TLeg.$create("Create");',
  15817. ' Result = $mod.TBird.TLeg.$create("Create");',
  15818. ' Result = Result.Create$1(3);',
  15819. ' return Result;',
  15820. ' };',
  15821. '});',
  15822. 'this.b = null;',
  15823. '']),
  15824. LinesToStr([ // $mod.$main
  15825. '$mod.b.Create();',
  15826. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  15827. '$mod.b = $mod.b.Create$1(3);',
  15828. '']));
  15829. end;
  15830. procedure TTestModule.TestExternalClass_Var;
  15831. begin
  15832. StartProgram(false);
  15833. Add([
  15834. '{$modeswitch externalclass}',
  15835. 'type',
  15836. ' TExtA = class external name ''ExtObj''',
  15837. ' Id: longint external name ''$Id'';',
  15838. ' B: longint;',
  15839. ' end;',
  15840. 'var Obj: TExtA;',
  15841. 'begin',
  15842. ' obj.id:=obj.id+1;',
  15843. ' obj.B:=obj.B+1;']);
  15844. ConvertProgram;
  15845. CheckSource('TestExternalClass_Var',
  15846. LinesToStr([ // statements
  15847. 'this.Obj = null;',
  15848. '']),
  15849. LinesToStr([ // $mod.$main
  15850. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  15851. '$mod.Obj.B = $mod.Obj.B + 1;',
  15852. '']));
  15853. end;
  15854. procedure TTestModule.TestExternalClass_Const;
  15855. begin
  15856. StartProgram(false);
  15857. Add([
  15858. '{$modeswitch externalclass}',
  15859. 'type',
  15860. ' TExtA = class external name ''ExtObj''',
  15861. ' const Two: longint = 2;',
  15862. ' const Three = 3;',
  15863. ' const Id: longint;',
  15864. ' end;',
  15865. ' TExtB = class external name ''ExtB''',
  15866. ' A: TExtA;',
  15867. ' end;',
  15868. 'var',
  15869. ' A: texta;',
  15870. ' B: textb;',
  15871. ' i: longint;',
  15872. 'begin',
  15873. ' i:=a.two;',
  15874. ' i:=texta.two;',
  15875. ' i:=a.three;',
  15876. ' i:=texta.three;',
  15877. ' i:=a.id;',
  15878. ' i:=texta.id;',
  15879. '']);
  15880. ConvertProgram;
  15881. CheckSource('TestExternalClass_Const',
  15882. LinesToStr([ // statements
  15883. 'this.A = null;',
  15884. 'this.B = null;',
  15885. 'this.i = 0;',
  15886. '']),
  15887. LinesToStr([ // $mod.$main
  15888. '$mod.i = 2;',
  15889. '$mod.i = 2;',
  15890. '$mod.i = 3;',
  15891. '$mod.i = 3;',
  15892. '$mod.i = $mod.A.Id;',
  15893. '$mod.i = ExtObj.Id;',
  15894. '']));
  15895. end;
  15896. procedure TTestModule.TestExternalClass_Dollar;
  15897. begin
  15898. StartProgram(false);
  15899. Add([
  15900. '{$modeswitch externalclass}',
  15901. 'type',
  15902. ' TExtA = class external name ''$''',
  15903. ' Id: longint external name ''$'';',
  15904. ' function Bla(i: longint): longint; external name ''$'';',
  15905. ' end;',
  15906. 'function dollar(k: longint): longint; external name ''$'';',
  15907. 'var Obj: TExtA;',
  15908. 'begin',
  15909. ' dollar(1);',
  15910. ' obj.id:=obj.id+2;',
  15911. ' obj.Bla(3);',
  15912. '']);
  15913. ConvertProgram;
  15914. CheckSource('TestExternalClass_Dollar',
  15915. LinesToStr([ // statements
  15916. 'this.Obj = null;',
  15917. '']),
  15918. LinesToStr([ // $mod.$main
  15919. '$(1);',
  15920. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  15921. '$mod.Obj.$(3);',
  15922. '']));
  15923. end;
  15924. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  15925. begin
  15926. StartProgram(false);
  15927. Add('{$modeswitch externalclass}');
  15928. Add('type');
  15929. Add(' TExtA = class external name ''ExtA''');
  15930. Add(' Id: longint external name ''$Id'';');
  15931. Add(' end;');
  15932. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  15933. Add(' Id: longint;');
  15934. Add(' end;');
  15935. Add('begin');
  15936. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  15937. ConvertProgram;
  15938. end;
  15939. procedure TTestModule.TestExternalClass_Method;
  15940. begin
  15941. StartProgram(false);
  15942. Add(['{$modeswitch externalclass}',
  15943. 'type',
  15944. ' TExtA = class external name ''ExtObj''',
  15945. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15946. ' procedure DoSome(Id: longint = 1);',
  15947. ' end;',
  15948. 'var Obj: texta;',
  15949. 'begin',
  15950. ' obj.doit;',
  15951. ' obj.doit();',
  15952. ' obj.doit(2);',
  15953. ' with obj do begin',
  15954. ' doit;',
  15955. ' doit();',
  15956. ' doit(3);',
  15957. ' end;']);
  15958. ConvertProgram;
  15959. CheckSource('TestExternalClass_Method',
  15960. LinesToStr([ // statements
  15961. 'this.Obj = null;',
  15962. '']),
  15963. LinesToStr([ // $mod.$main
  15964. '$mod.Obj.$Execute(1);',
  15965. '$mod.Obj.$Execute(1);',
  15966. '$mod.Obj.$Execute(2);',
  15967. 'var $with = $mod.Obj;',
  15968. '$with.$Execute(1);',
  15969. '$with.$Execute(1);',
  15970. '$with.$Execute(3);',
  15971. '']));
  15972. end;
  15973. procedure TTestModule.TestExternalClass_ClassMethod;
  15974. begin
  15975. StartProgram(false);
  15976. Add([
  15977. '{$modeswitch externalclass}',
  15978. 'type',
  15979. ' TExtA = class external name ''ExtObj''',
  15980. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  15981. ' end;',
  15982. ' TExtB = TExtA;',
  15983. 'var p: Pointer;',
  15984. 'begin',
  15985. ' texta.doit;',
  15986. ' texta.doit();',
  15987. ' texta.doit(2);',
  15988. ' p:[email protected];',
  15989. ' with texta do begin',
  15990. ' doit;',
  15991. ' doit();',
  15992. ' doit(3);',
  15993. ' p:=@DoIt;',
  15994. ' end;',
  15995. ' textb.doit;',
  15996. ' textb.doit();',
  15997. ' textb.doit(4);',
  15998. ' with textb do begin',
  15999. ' doit;',
  16000. ' doit();',
  16001. ' doit(5);',
  16002. ' end;',
  16003. '']);
  16004. ConvertProgram;
  16005. CheckSource('TestExternalClass_ClassMethod',
  16006. LinesToStr([ // statements
  16007. 'this.p = null;',
  16008. '']),
  16009. LinesToStr([ // $mod.$main
  16010. 'ExtObj.$Execute(1);',
  16011. 'ExtObj.$Execute(1);',
  16012. 'ExtObj.$Execute(2);',
  16013. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16014. 'ExtObj.$Execute(1);',
  16015. 'ExtObj.$Execute(1);',
  16016. 'ExtObj.$Execute(3);',
  16017. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16018. 'ExtObj.$Execute(1);',
  16019. 'ExtObj.$Execute(1);',
  16020. 'ExtObj.$Execute(4);',
  16021. 'ExtObj.$Execute(1);',
  16022. 'ExtObj.$Execute(1);',
  16023. 'ExtObj.$Execute(5);',
  16024. '']));
  16025. end;
  16026. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16027. begin
  16028. StartProgram(false);
  16029. Add([
  16030. '{$modeswitch externalclass}',
  16031. 'type',
  16032. ' TExtA = class external name ''ExtObj''',
  16033. ' class procedure DoIt(Id: longint = 1); static;',
  16034. ' end;',
  16035. 'var p: Pointer;',
  16036. 'begin',
  16037. ' texta.doit;',
  16038. ' texta.doit();',
  16039. ' texta.doit(2);',
  16040. ' p:[email protected];',
  16041. ' with texta do begin',
  16042. ' doit;',
  16043. ' doit();',
  16044. ' doit(3);',
  16045. ' p:=@DoIt;',
  16046. ' end;',
  16047. '']);
  16048. ConvertProgram;
  16049. CheckSource('TestExternalClass_ClassMethodStatic',
  16050. LinesToStr([ // statements
  16051. 'this.p = null;',
  16052. '']),
  16053. LinesToStr([ // $mod.$main
  16054. 'ExtObj.DoIt(1);',
  16055. 'ExtObj.DoIt(1);',
  16056. 'ExtObj.DoIt(2);',
  16057. '$mod.p = ExtObj.DoIt;',
  16058. 'ExtObj.DoIt(1);',
  16059. 'ExtObj.DoIt(1);',
  16060. 'ExtObj.DoIt(3);',
  16061. '$mod.p = ExtObj.DoIt;',
  16062. '']));
  16063. end;
  16064. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16065. begin
  16066. StartProgram(false);
  16067. Add([
  16068. '{$modeswitch externalclass}',
  16069. 'type',
  16070. ' TBird = class external name ''Array''',
  16071. ' end;',
  16072. 'function GetPtr: Pointer;',
  16073. 'begin',
  16074. 'end;',
  16075. 'procedure Write(const p);',
  16076. 'begin',
  16077. 'end;',
  16078. 'procedure WriteLn; varargs;',
  16079. 'begin',
  16080. 'end;',
  16081. 'begin',
  16082. ' if TBird(GetPtr)=nil then ;',
  16083. ' Write(GetPtr);',
  16084. ' WriteLn(GetPtr);',
  16085. ' Write(TBird(GetPtr));',
  16086. ' WriteLn(TBird(GetPtr));',
  16087. '']);
  16088. ConvertProgram;
  16089. CheckSource('TestFunctionResultInTypeCast',
  16090. LinesToStr([ // statements
  16091. 'this.GetPtr = function () {',
  16092. ' var Result = null;',
  16093. ' return Result;',
  16094. '};',
  16095. 'this.Write = function (p) {',
  16096. '};',
  16097. 'this.WriteLn = function () {',
  16098. '};',
  16099. '']),
  16100. LinesToStr([
  16101. 'if ($mod.GetPtr() === null) ;',
  16102. '$mod.Write($mod.GetPtr());',
  16103. '$mod.WriteLn($mod.GetPtr());',
  16104. '$mod.Write($mod.GetPtr());',
  16105. '$mod.WriteLn($mod.GetPtr());',
  16106. '']));
  16107. end;
  16108. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16109. begin
  16110. StartProgram(false);
  16111. Add([
  16112. '{$modeswitch externalclass}',
  16113. 'type',
  16114. ' TExtA = class external name ''ExtObjA''',
  16115. ' procedure ProcA; virtual;',
  16116. ' procedure ProcB; virtual;',
  16117. ' end;',
  16118. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16119. ' end;',
  16120. ' TExtC = class (TExtB)',
  16121. ' procedure ProcA; override;',
  16122. ' end;',
  16123. 'procedure TExtC.ProcA;',
  16124. 'begin',
  16125. ' ProcA;',
  16126. ' Self.ProcA;',
  16127. ' ProcB;',
  16128. ' Self.ProcB;',
  16129. 'end;',
  16130. 'var',
  16131. ' A: texta;',
  16132. ' B: textb;',
  16133. ' C: textc;',
  16134. 'begin',
  16135. ' a.proca;',
  16136. ' b.proca;',
  16137. ' c.proca;']);
  16138. ConvertProgram;
  16139. CheckSource('TestExternalClass_NonExternalOverride',
  16140. LinesToStr([ // statements
  16141. 'rtl.createClassExt($mod, "TExtC", ExtObjB, "", function () {',
  16142. ' this.$init = function () {',
  16143. ' };',
  16144. ' this.$final = function () {',
  16145. ' };',
  16146. ' this.ProcA = function () {',
  16147. ' this.ProcA();',
  16148. ' this.ProcA();',
  16149. ' this.ProcB();',
  16150. ' this.ProcB();',
  16151. ' };',
  16152. '});',
  16153. 'this.A = null;',
  16154. 'this.B = null;',
  16155. 'this.C = null;',
  16156. '']),
  16157. LinesToStr([ // $mod.$main
  16158. '$mod.A.ProcA();',
  16159. '$mod.B.ProcA();',
  16160. '$mod.C.ProcA();',
  16161. '']));
  16162. end;
  16163. procedure TTestModule.TestExternalClass_OverloadHint;
  16164. begin
  16165. StartProgram(false);
  16166. Add([
  16167. '{$modeswitch externalclass}',
  16168. 'type',
  16169. ' TExtA = class external name ''ExtObjA''',
  16170. ' procedure DoIt;',
  16171. ' procedure DoIt(i: longint);',
  16172. ' end;',
  16173. 'begin',
  16174. '']);
  16175. ConvertProgram;
  16176. CheckResolverUnexpectedHints(true);
  16177. CheckSource('TestExternalClass_OverloadHint',
  16178. LinesToStr([ // statements
  16179. '']),
  16180. LinesToStr([ // $mod.$main
  16181. '']));
  16182. end;
  16183. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16184. begin
  16185. StartProgram(false);
  16186. Add([
  16187. '{$modeswitch externalclass}',
  16188. 'type',
  16189. ' JSwiper = class external name ''Swiper''',
  16190. ' constructor New;',
  16191. ' end;',
  16192. ' TObject = class',
  16193. ' private',
  16194. ' FSwiper: JSwiper;',
  16195. ' published',
  16196. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16197. ' end;',
  16198. 'begin',
  16199. ' JSwiper.new;',
  16200. '']);
  16201. ConvertProgram;
  16202. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16203. LinesToStr([ // statements
  16204. 'rtl.createClass($mod, "TObject", null, function () {',
  16205. ' this.$init = function () {',
  16206. ' this.FSwiper = null;',
  16207. ' };',
  16208. ' this.$final = function () {',
  16209. ' this.FSwiper = undefined;',
  16210. ' };',
  16211. ' var $r = this.$rtti;',
  16212. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16213. '});',
  16214. '']),
  16215. LinesToStr([ // $mod.$main
  16216. 'new Swiper();',
  16217. '']));
  16218. end;
  16219. procedure TTestModule.TestExternalClass_Property;
  16220. begin
  16221. StartProgram(false);
  16222. Add([
  16223. '{$modeswitch externalclass}',
  16224. 'type',
  16225. ' TExtA = class external name ''ExtA''',
  16226. ' function getYear: longint;',
  16227. ' procedure setYear(Value: longint);',
  16228. ' property Year: longint read getyear write setyear;',
  16229. ' end;',
  16230. ' TExtB = class (TExtA)',
  16231. ' procedure OtherSetYear(Value: longint);',
  16232. ' property year write othersetyear;',
  16233. ' end;',
  16234. 'procedure textb.othersetyear(value: longint);',
  16235. 'begin',
  16236. ' setYear(Value+4);',
  16237. 'end;',
  16238. 'var',
  16239. ' A: texta;',
  16240. ' B: textb;',
  16241. 'begin',
  16242. ' a.year:=a.year+1;',
  16243. ' b.year:=b.year+2;']);
  16244. ConvertProgram;
  16245. CheckSource('TestExternalClass_NonExternalOverride',
  16246. LinesToStr([ // statements
  16247. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16248. ' this.$init = function () {',
  16249. ' };',
  16250. ' this.$final = function () {',
  16251. ' };',
  16252. ' this.OtherSetYear = function (Value) {',
  16253. ' this.setYear(Value+4);',
  16254. ' };',
  16255. '});',
  16256. 'this.A = null;',
  16257. 'this.B = null;',
  16258. '']),
  16259. LinesToStr([ // $mod.$main
  16260. '$mod.A.setYear($mod.A.getYear()+1);',
  16261. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16262. '']));
  16263. end;
  16264. procedure TTestModule.TestExternalClass_PropertyDate;
  16265. begin
  16266. StartProgram(false);
  16267. Add([
  16268. '{$modeswitch externalclass}',
  16269. 'type',
  16270. ' TExtA = class external name ''ExtA''',
  16271. ' end;',
  16272. ' TExtB = class (TExtA)',
  16273. ' FDate: string;',
  16274. ' property Date: string read FDate write FDate;',
  16275. ' property ExtA: string read FDate write FDate;',
  16276. ' end;',
  16277. ' {$M+}',
  16278. ' TObject = class',
  16279. ' FDate: string;',
  16280. ' published',
  16281. ' property Date: string read FDate write FDate;',
  16282. ' property ExtA: string read FDate write FDate;',
  16283. ' end;',
  16284. 'var',
  16285. ' B: textb;',
  16286. ' o: TObject;',
  16287. 'begin',
  16288. ' b.date:=b.exta;',
  16289. ' o.date:=o.exta;']);
  16290. ConvertProgram;
  16291. CheckSource('TestExternalClass_PropertyDate',
  16292. LinesToStr([ // statements
  16293. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16294. ' this.$init = function () {',
  16295. ' this.FDate = "";',
  16296. ' };',
  16297. ' this.$final = function () {',
  16298. ' };',
  16299. '});',
  16300. 'rtl.createClass($mod, "TObject", null, function () {',
  16301. ' this.$init = function () {',
  16302. ' this.FDate = "";',
  16303. ' };',
  16304. ' this.$final = function () {',
  16305. ' };',
  16306. ' var $r = this.$rtti;',
  16307. ' $r.addField("FDate", rtl.string);',
  16308. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16309. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16310. '});',
  16311. 'this.B = null;',
  16312. 'this.o = null;',
  16313. '']),
  16314. LinesToStr([ // $mod.$main
  16315. '$mod.B.FDate = $mod.B.FDate;',
  16316. '$mod.o.FDate = $mod.o.FDate;',
  16317. '']));
  16318. end;
  16319. procedure TTestModule.TestExternalClass_ClassProperty;
  16320. begin
  16321. StartProgram(false);
  16322. Add('{$modeswitch externalclass}');
  16323. Add('type');
  16324. Add(' TExtA = class external name ''ExtA''');
  16325. Add(' class function getYear: longint;');
  16326. Add(' class procedure setYear(Value: longint);');
  16327. Add(' class property Year: longint read getyear write setyear;');
  16328. Add(' end;');
  16329. Add(' TExtB = class (TExtA)');
  16330. Add(' class function GetCentury: longint;');
  16331. Add(' class procedure SetCentury(Value: longint);');
  16332. Add(' class property Century: longint read getcentury write setcentury;');
  16333. Add(' end;');
  16334. Add('class function textb.getcentury: longint;');
  16335. Add('begin');
  16336. Add('end;');
  16337. Add('class procedure textb.setcentury(value: longint);');
  16338. Add('begin');
  16339. Add(' setyear(value+11);');
  16340. Add(' texta.year:=texta.year+12;');
  16341. Add(' year:=year+13;');
  16342. Add(' textb.century:=textb.century+14;');
  16343. Add(' century:=century+15;');
  16344. Add('end;');
  16345. Add('var');
  16346. Add(' A: texta;');
  16347. Add(' B: textb;');
  16348. Add('begin');
  16349. Add(' texta.year:=texta.year+1;');
  16350. Add(' textb.year:=textb.year+2;');
  16351. Add(' TextA.year:=TextA.year+3;');
  16352. Add(' b.year:=b.year+4;');
  16353. Add(' textb.century:=textb.century+5;');
  16354. Add(' b.century:=b.century+6;');
  16355. ConvertProgram;
  16356. CheckSource('TestExternalClass_ClassProperty',
  16357. LinesToStr([ // statements
  16358. 'rtl.createClassExt($mod, "TExtB", ExtA, "", function () {',
  16359. ' this.$init = function () {',
  16360. ' };',
  16361. ' this.$final = function () {',
  16362. ' };',
  16363. ' this.GetCentury = function () {',
  16364. ' var Result = 0;',
  16365. ' return Result;',
  16366. ' };',
  16367. ' this.SetCentury = function (Value) {',
  16368. ' this.setYear(Value + 11);',
  16369. ' ExtA.setYear(ExtA.getYear() + 12);',
  16370. ' this.setYear(this.getYear() + 13);',
  16371. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16372. ' this.SetCentury(this.GetCentury() + 15);',
  16373. ' };',
  16374. '});',
  16375. 'this.A = null;',
  16376. 'this.B = null;',
  16377. '']),
  16378. LinesToStr([ // $mod.$main
  16379. 'ExtA.setYear(ExtA.getYear() + 1);',
  16380. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16381. 'ExtA.setYear(ExtA.getYear() + 3);',
  16382. '$mod.B.setYear($mod.B.getYear() + 4);',
  16383. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16384. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16385. '']));
  16386. end;
  16387. procedure TTestModule.TestExternalClass_ClassOf;
  16388. begin
  16389. StartProgram(false);
  16390. Add('{$modeswitch externalclass}');
  16391. Add('type');
  16392. Add(' TExtA = class external name ''ExtA''');
  16393. Add(' procedure ProcA; virtual;');
  16394. Add(' procedure ProcB; virtual;');
  16395. Add(' end;');
  16396. Add(' TExtAClass = class of TExtA;');
  16397. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16398. Add(' end;');
  16399. Add(' TExtBClass = class of TExtB;');
  16400. Add(' TExtC = class (TExtB)');
  16401. Add(' procedure ProcA; override;');
  16402. Add(' end;');
  16403. Add(' TExtCClass = class of TExtC;');
  16404. Add('procedure TExtC.ProcA; begin end;');
  16405. Add('var');
  16406. Add(' A: texta; ClA: TExtAClass;');
  16407. Add(' B: textb; ClB: TExtBClass;');
  16408. Add(' C: textc; ClC: TExtCClass;');
  16409. Add('begin');
  16410. Add(' ClA:=texta;');
  16411. Add(' ClA:=textb;');
  16412. Add(' ClA:=textc;');
  16413. Add(' ClB:=textb;');
  16414. Add(' ClB:=textc;');
  16415. Add(' ClC:=textc;');
  16416. ConvertProgram;
  16417. CheckSource('TestExternalClass_ClassOf',
  16418. LinesToStr([ // statements
  16419. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16420. ' this.$init = function () {',
  16421. ' };',
  16422. ' this.$final = function () {',
  16423. ' };',
  16424. ' this.ProcA = function () {',
  16425. ' };',
  16426. '});',
  16427. 'this.A = null;',
  16428. 'this.ClA = null;',
  16429. 'this.B = null;',
  16430. 'this.ClB = null;',
  16431. 'this.C = null;',
  16432. 'this.ClC = null;',
  16433. '']),
  16434. LinesToStr([ // $mod.$main
  16435. '$mod.ClA = ExtA;',
  16436. '$mod.ClA = ExtB;',
  16437. '$mod.ClA = $mod.TExtC;',
  16438. '$mod.ClB = ExtB;',
  16439. '$mod.ClB = $mod.TExtC;',
  16440. '$mod.ClC = $mod.TExtC;',
  16441. '']));
  16442. end;
  16443. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16444. begin
  16445. AddModuleWithIntfImplSrc('unit2.pas',
  16446. LinesToStr([
  16447. '{$modeswitch externalclass}',
  16448. 'type',
  16449. ' TExtA = class external name ''ExtA''',
  16450. ' class var Id: longint;',
  16451. ' end;',
  16452. '']),
  16453. '');
  16454. StartUnit(true);
  16455. Add('interface');
  16456. Add('uses unit2;');
  16457. Add('implementation');
  16458. Add('begin');
  16459. Add(' unit2.texta.id:=unit2.texta.id+1;');
  16460. ConvertUnit;
  16461. CheckSource('TestExternalClass_ClassOtherUnit',
  16462. LinesToStr([
  16463. '']),
  16464. LinesToStr([
  16465. 'ExtA.Id = ExtA.Id + 1;',
  16466. '']));
  16467. end;
  16468. procedure TTestModule.TestExternalClass_Is;
  16469. begin
  16470. StartProgram(false);
  16471. Add([
  16472. '{$modeswitch externalclass}',
  16473. 'type',
  16474. ' TExtA = class external name ''ExtA''',
  16475. ' end;',
  16476. ' TExtAClass = class of TExtA;',
  16477. ' TExtB = class external name ''ExtB'' (TExtA)',
  16478. ' end;',
  16479. ' TExtBClass = class of TExtB;',
  16480. ' TExtC = class (TExtB)',
  16481. ' end;',
  16482. ' TExtCClass = class of TExtC;',
  16483. 'var',
  16484. ' A: texta; ClA: TExtAClass;',
  16485. ' B: textb; ClB: TExtBClass;',
  16486. ' C: textc; ClC: TExtCClass;',
  16487. 'begin',
  16488. ' if a is textb then ;',
  16489. ' if a is textc then ;',
  16490. ' if b is textc then ;',
  16491. ' if cla is textb then ;',
  16492. ' if cla is textc then ;',
  16493. ' if clb is textc then ;',
  16494. ' try',
  16495. ' except',
  16496. ' on TExtA do ;',
  16497. ' on e: TExtB do ;',
  16498. ' end;',
  16499. '']);
  16500. ConvertProgram;
  16501. CheckSource('TestExternalClass_Is',
  16502. LinesToStr([ // statements
  16503. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16504. ' this.$init = function () {',
  16505. ' };',
  16506. ' this.$final = function () {',
  16507. ' };',
  16508. '});',
  16509. 'this.A = null;',
  16510. 'this.ClA = null;',
  16511. 'this.B = null;',
  16512. 'this.ClB = null;',
  16513. 'this.C = null;',
  16514. 'this.ClC = null;',
  16515. '']),
  16516. LinesToStr([ // $mod.$main
  16517. 'if (rtl.isExt($mod.A, ExtB)) ;',
  16518. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  16519. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  16520. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  16521. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  16522. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  16523. 'try {} catch ($e) {',
  16524. ' if (rtl.isExt($e,ExtA)) {}',
  16525. ' else if (rtl.isExt($e,ExtB)) {',
  16526. ' var e = $e;',
  16527. ' } else throw $e',
  16528. '};',
  16529. '']));
  16530. end;
  16531. procedure TTestModule.TestExternalClass_As;
  16532. begin
  16533. StartProgram(false);
  16534. Add('{$modeswitch externalclass}');
  16535. Add('type');
  16536. Add(' TExtA = class external name ''ExtA''');
  16537. Add(' end;');
  16538. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16539. Add(' end;');
  16540. Add(' TExtC = class (TExtB)');
  16541. Add(' end;');
  16542. Add('var');
  16543. Add(' A: texta;');
  16544. Add(' B: textb;');
  16545. Add(' C: textc;');
  16546. Add('begin');
  16547. Add(' b:=a as textb;');
  16548. Add(' c:=a as textc;');
  16549. Add(' c:=b as textc;');
  16550. ConvertProgram;
  16551. CheckSource('TestExternalClass_Is',
  16552. LinesToStr([ // statements
  16553. 'rtl.createClassExt($mod, "TExtC", ExtB, "", function () {',
  16554. ' this.$init = function () {',
  16555. ' };',
  16556. ' this.$final = function () {',
  16557. ' };',
  16558. '});',
  16559. 'this.A = null;',
  16560. 'this.B = null;',
  16561. 'this.C = null;',
  16562. '']),
  16563. LinesToStr([ // $mod.$main
  16564. '$mod.B = rtl.asExt($mod.A, ExtB);',
  16565. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  16566. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  16567. '']));
  16568. end;
  16569. procedure TTestModule.TestExternalClass_DestructorFail;
  16570. begin
  16571. StartProgram(false);
  16572. Add('{$modeswitch externalclass}');
  16573. Add('type');
  16574. Add(' TExtA = class external name ''ExtA''');
  16575. Add(' destructor Free;');
  16576. Add(' end;');
  16577. SetExpectedPasResolverError('Pascal element not supported: destructor',
  16578. nPasElementNotSupported);
  16579. ConvertProgram;
  16580. end;
  16581. procedure TTestModule.TestExternalClass_New;
  16582. begin
  16583. StartProgram(false);
  16584. Add([
  16585. '{$modeswitch externalclass}',
  16586. 'type',
  16587. ' TExtA = class external name ''ExtA''',
  16588. ' constructor New;',
  16589. ' constructor New(i: longint; j: longint = 2);',
  16590. ' end;',
  16591. 'var',
  16592. ' A: texta;',
  16593. 'begin',
  16594. ' a:=texta.new;',
  16595. ' a:=texta(texta.new);',
  16596. ' a:=texta.new();',
  16597. ' a:=texta.new(1);',
  16598. ' with texta do begin',
  16599. ' a:=new;',
  16600. ' a:=new();',
  16601. ' a:=new(2);',
  16602. ' end;',
  16603. ' a:=test1.texta.new;',
  16604. ' a:=test1.texta.new();',
  16605. ' a:=test1.texta.new(3);',
  16606. '']);
  16607. ConvertProgram;
  16608. CheckSource('TestExternalClass_New',
  16609. LinesToStr([ // statements
  16610. 'this.A = null;',
  16611. '']),
  16612. LinesToStr([ // $mod.$main
  16613. '$mod.A = new ExtA();',
  16614. '$mod.A = new ExtA();',
  16615. '$mod.A = new ExtA();',
  16616. '$mod.A = new ExtA(1,2);',
  16617. '$mod.A = new ExtA();',
  16618. '$mod.A = new ExtA();',
  16619. '$mod.A = new ExtA(2,2);',
  16620. '$mod.A = new ExtA();',
  16621. '$mod.A = new ExtA();',
  16622. '$mod.A = new ExtA(3,2);',
  16623. '']));
  16624. end;
  16625. procedure TTestModule.TestExternalClass_ClassOf_New;
  16626. begin
  16627. StartProgram(false);
  16628. Add('{$modeswitch externalclass}');
  16629. Add('type');
  16630. Add(' TExtAClass = class of TExtA;');
  16631. Add(' TExtA = class external name ''ExtA''');
  16632. Add(' C: TExtAClass;');
  16633. Add(' constructor New;');
  16634. Add(' end;');
  16635. Add('var');
  16636. Add(' A: texta;');
  16637. Add(' C: textaclass;');
  16638. Add('begin');
  16639. Add(' a:=c.new;');
  16640. Add(' a:=c.new();');
  16641. Add(' with C do begin');
  16642. Add(' a:=new;');
  16643. Add(' a:=new();');
  16644. Add(' end;');
  16645. Add(' a:=test1.c.new;');
  16646. Add(' a:=test1.c.new();');
  16647. Add(' a:=A.c.new();');
  16648. ConvertProgram;
  16649. CheckSource('TestExternalClass_ClassOf_New',
  16650. LinesToStr([ // statements
  16651. 'this.A = null;',
  16652. 'this.C = null;',
  16653. '']),
  16654. LinesToStr([ // $mod.$main
  16655. '$mod.A = new $mod.C();',
  16656. '$mod.A = new $mod.C();',
  16657. 'var $with = $mod.C;',
  16658. '$mod.A = new $with();',
  16659. '$mod.A = new $with();',
  16660. '$mod.A = new $mod.C();',
  16661. '$mod.A = new $mod.C();',
  16662. '$mod.A = new $mod.A.C();',
  16663. '']));
  16664. end;
  16665. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  16666. begin
  16667. StartProgram(false);
  16668. Add([
  16669. '{$modeswitch externalclass}',
  16670. 'type',
  16671. ' TExtAClass = class of TExtA;',
  16672. ' TExtA = class external name ''ExtA''',
  16673. ' constructor New;',
  16674. ' end;',
  16675. 'function GetCreator: TExtAClass;',
  16676. 'begin',
  16677. ' Result:=TExtA;',
  16678. 'end;',
  16679. 'var',
  16680. ' A: texta;',
  16681. 'begin',
  16682. ' a:=getcreator.new;',
  16683. ' a:=getcreator().new;',
  16684. ' a:=getcreator().new();',
  16685. ' a:=getcreator.new();',
  16686. ' with getcreator do begin',
  16687. ' a:=new;',
  16688. ' a:=new();',
  16689. ' end;']);
  16690. ConvertProgram;
  16691. CheckSource('TestExternalClass_FuncClassOf_New',
  16692. LinesToStr([ // statements
  16693. 'this.GetCreator = function () {',
  16694. ' var Result = null;',
  16695. ' Result = ExtA;',
  16696. ' return Result;',
  16697. '};',
  16698. 'this.A = null;',
  16699. '']),
  16700. LinesToStr([ // $mod.$main
  16701. '$mod.A = new ($mod.GetCreator())();',
  16702. '$mod.A = new ($mod.GetCreator())();',
  16703. '$mod.A = new ($mod.GetCreator())();',
  16704. '$mod.A = new ($mod.GetCreator())();',
  16705. 'var $with = $mod.GetCreator();',
  16706. '$mod.A = new $with();',
  16707. '$mod.A = new $with();',
  16708. '']));
  16709. end;
  16710. procedure TTestModule.TestExternalClass_New_PasClassFail;
  16711. begin
  16712. StartProgram(false);
  16713. Add([
  16714. '{$modeswitch externalclass}',
  16715. 'type',
  16716. ' TExtA = class external name ''ExtA''',
  16717. ' constructor New;',
  16718. ' end;',
  16719. ' TBird = class(TExtA)',
  16720. ' end;',
  16721. 'begin',
  16722. ' TBird.new;',
  16723. '']);
  16724. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16725. ConvertProgram;
  16726. end;
  16727. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  16728. begin
  16729. StartProgram(false);
  16730. Add([
  16731. '{$modeswitch externalclass}',
  16732. 'type',
  16733. ' TExtA = class external name ''ExtA''',
  16734. ' constructor New;',
  16735. ' end;',
  16736. ' TBird = class(TExtA)',
  16737. ' end;',
  16738. 'begin',
  16739. ' TBird.new();',
  16740. '']);
  16741. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  16742. ConvertProgram;
  16743. end;
  16744. procedure TTestModule.TestExternalClass_NewExtName;
  16745. begin
  16746. StartProgram(false);
  16747. Add([
  16748. '{$modeswitch externalclass}',
  16749. 'type',
  16750. ' TExtA = class external name ''ExtA''',
  16751. ' constructor New; external name ''Other'';',
  16752. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  16753. ' end;',
  16754. 'var',
  16755. ' A: texta;',
  16756. 'begin',
  16757. ' a:=texta.new;',
  16758. ' a:=texta(texta.new);',
  16759. ' a:=texta.new();',
  16760. ' a:=texta.new(1);',
  16761. ' with texta do begin',
  16762. ' a:=new;',
  16763. ' a:=new();',
  16764. ' a:=new(2);',
  16765. ' end;',
  16766. ' a:=test1.texta.new;',
  16767. ' a:=test1.texta.new();',
  16768. ' a:=test1.texta.new(3);',
  16769. '']);
  16770. ConvertProgram;
  16771. CheckSource('TestExternalClass_NewExtName',
  16772. LinesToStr([ // statements
  16773. 'this.A = null;',
  16774. '']),
  16775. LinesToStr([ // $mod.$main
  16776. '$mod.A = new Other();',
  16777. '$mod.A = new Other();',
  16778. '$mod.A = new Other();',
  16779. '$mod.A = new A.B(1,2);',
  16780. '$mod.A = new Other();',
  16781. '$mod.A = new Other();',
  16782. '$mod.A = new A.B(2,2);',
  16783. '$mod.A = new Other();',
  16784. '$mod.A = new Other();',
  16785. '$mod.A = new A.B(3,2);',
  16786. '']));
  16787. end;
  16788. procedure TTestModule.TestExternalClass_Constructor;
  16789. begin
  16790. StartProgram(false);
  16791. Add([
  16792. '{$modeswitch externalclass}',
  16793. 'type',
  16794. ' TExtA = class external name ''ExtA''',
  16795. ' constructor Create;',
  16796. ' constructor Create(i: longint; j: longint = 2);',
  16797. ' end;',
  16798. 'var',
  16799. ' A: texta;',
  16800. 'begin',
  16801. ' a:=texta.create;',
  16802. ' a:=texta(texta.create);',
  16803. ' a:=texta.create();',
  16804. ' a:=texta.create(1);',
  16805. ' with texta do begin',
  16806. ' a:=create;',
  16807. ' a:=create();',
  16808. ' a:=create(2);',
  16809. ' end;',
  16810. ' a:=test1.texta.create;',
  16811. ' a:=test1.texta.create();',
  16812. ' a:=test1.texta.create(3);',
  16813. '']);
  16814. ConvertProgram;
  16815. CheckSource('TestExternalClass_Constructor',
  16816. LinesToStr([ // statements
  16817. 'this.A = null;',
  16818. '']),
  16819. LinesToStr([ // $mod.$main
  16820. '$mod.A = new ExtA.Create();',
  16821. '$mod.A = new ExtA.Create();',
  16822. '$mod.A = new ExtA.Create();',
  16823. '$mod.A = new ExtA.Create(1,2);',
  16824. '$mod.A = new ExtA.Create();',
  16825. '$mod.A = new ExtA.Create();',
  16826. '$mod.A = new ExtA.Create(2,2);',
  16827. '$mod.A = new ExtA.Create();',
  16828. '$mod.A = new ExtA.Create();',
  16829. '$mod.A = new ExtA.Create(3,2);',
  16830. '']));
  16831. end;
  16832. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  16833. begin
  16834. StartProgram(false);
  16835. Add([
  16836. '{$modeswitch externalclass}',
  16837. 'type',
  16838. ' TExtA = class external name ''ExtA''',
  16839. ' constructor Create; external name ''{}'';',
  16840. ' end;',
  16841. 'var',
  16842. ' A: texta;',
  16843. 'begin',
  16844. ' a:=texta.create;',
  16845. ' a:=texta(texta.create);',
  16846. ' a:=texta.create();',
  16847. ' with texta do begin',
  16848. ' a:=create;',
  16849. ' a:=create();',
  16850. ' end;',
  16851. ' a:=test1.texta.create;',
  16852. ' a:=test1.texta.create();',
  16853. '']);
  16854. ConvertProgram;
  16855. CheckSource('TestExternalClass_ConstructorBrackets',
  16856. LinesToStr([ // statements
  16857. 'this.A = null;',
  16858. '']),
  16859. LinesToStr([ // $mod.$main
  16860. '$mod.A = {};',
  16861. '$mod.A = {};',
  16862. '$mod.A = {};',
  16863. '$mod.A = {};',
  16864. '$mod.A = {};',
  16865. '$mod.A = {};',
  16866. '$mod.A = {};',
  16867. '']));
  16868. end;
  16869. procedure TTestModule.TestExternalClass_LocalConstSameName;
  16870. begin
  16871. StartProgram(false);
  16872. Add('{$modeswitch externalclass}');
  16873. Add('type');
  16874. Add(' TExtA = class external name ''ExtA''');
  16875. Add(' constructor New;');
  16876. Add(' end;');
  16877. Add('function DoIt: longint;');
  16878. Add('const ExtA: longint = 3;');
  16879. Add('begin');
  16880. Add(' Result:=ExtA;');
  16881. Add('end;');
  16882. Add('var');
  16883. Add(' A: texta;');
  16884. Add('begin');
  16885. Add(' a:=texta.new;');
  16886. ConvertProgram;
  16887. CheckSource('TestExternalClass_LocalConstSameName',
  16888. LinesToStr([ // statements
  16889. 'var ExtA$1 = 3;',
  16890. 'this.DoIt = function () {',
  16891. ' var Result = 0;',
  16892. ' Result = ExtA$1;',
  16893. ' return Result;',
  16894. '};',
  16895. 'this.A = null;',
  16896. '']),
  16897. LinesToStr([ // $mod.$main
  16898. '$mod.A = new ExtA();',
  16899. '']));
  16900. end;
  16901. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  16902. begin
  16903. StartProgram(false);
  16904. Add('{$modeswitch externalclass}');
  16905. Add('type');
  16906. Add(' TExtA = class external name ''ExtA''');
  16907. Add(' procedure DoIt;');
  16908. Add(' end;');
  16909. Add(' TMyA = class(TExtA)');
  16910. Add(' procedure DoIt;');
  16911. Add(' end;');
  16912. Add('procedure TMyA.DoIt; begin end;');
  16913. Add('begin');
  16914. ConvertProgram;
  16915. CheckSource('TestExternalClass_ReintroduceOverload',
  16916. LinesToStr([ // statements
  16917. 'rtl.createClassExt($mod, "TMyA", ExtA, "", function () {',
  16918. ' this.$init = function () {',
  16919. ' };',
  16920. ' this.$final = function () {',
  16921. ' };',
  16922. ' this.DoIt$1 = function () {',
  16923. ' };',
  16924. '});',
  16925. '']),
  16926. LinesToStr([ // $mod.$main
  16927. '']));
  16928. end;
  16929. procedure TTestModule.TestExternalClass_Inherited;
  16930. begin
  16931. StartProgram(false);
  16932. Add('{$modeswitch externalclass}');
  16933. Add('type');
  16934. Add(' TExtA = class external name ''ExtA''');
  16935. Add(' procedure DoIt(i: longint = 1); virtual;');
  16936. Add(' procedure DoSome(j: longint = 2);');
  16937. Add(' end;');
  16938. Add(' TExtB = class external name ''ExtB''(TExtA)');
  16939. Add(' end;');
  16940. Add(' TMyC = class(TExtB)');
  16941. Add(' procedure DoIt(i: longint = 1); override;');
  16942. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  16943. Add(' end;');
  16944. Add('procedure TMyC.DoIt(i: longint);');
  16945. Add('begin');
  16946. Add(' inherited;');
  16947. Add(' inherited DoIt;');
  16948. Add(' inherited DoIt();');
  16949. Add(' inherited DoIt(3);');
  16950. Add(' inherited DoSome;');
  16951. Add(' inherited DoSome();');
  16952. Add(' inherited DoSome(4);');
  16953. Add('end;');
  16954. Add('procedure TMyC.DoSome(j: longint);');
  16955. Add('begin');
  16956. Add(' inherited;');
  16957. Add('end;');
  16958. Add('begin');
  16959. ConvertProgram;
  16960. CheckSource('TestExternalClass_ReintroduceOverload',
  16961. LinesToStr([ // statements
  16962. 'rtl.createClassExt($mod, "TMyC", ExtB, "", function () {',
  16963. ' this.$init = function () {',
  16964. ' };',
  16965. ' this.$final = function () {',
  16966. ' };',
  16967. ' this.DoIt = function (i) {',
  16968. ' ExtB.DoIt.apply(this, arguments);',
  16969. ' ExtB.DoIt.call(this, 1);',
  16970. ' ExtB.DoIt.call(this, 1);',
  16971. ' ExtB.DoIt.call(this, 3);',
  16972. ' ExtB.DoSome.call(this, 2);',
  16973. ' ExtB.DoSome.call(this, 2);',
  16974. ' ExtB.DoSome.call(this, 4);',
  16975. ' };',
  16976. ' this.DoSome$1 = function (j) {',
  16977. ' ExtB.DoSome.apply(this, arguments);',
  16978. ' };',
  16979. '});',
  16980. '']),
  16981. LinesToStr([ // $mod.$main
  16982. '']));
  16983. end;
  16984. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  16985. begin
  16986. StartProgram(false);
  16987. Add('{$modeswitch externalclass}');
  16988. Add('type');
  16989. Add(' TObject = class');
  16990. Add(' end;');
  16991. Add(' TExtA = class external name ''ExtA''(TObject)');
  16992. Add(' end;');
  16993. Add('begin');
  16994. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  16995. ConvertProgram;
  16996. end;
  16997. procedure TTestModule.TestExternalClass_NewInstance;
  16998. begin
  16999. StartProgram(false);
  17000. Add('{$modeswitch externalclass}');
  17001. Add('type');
  17002. Add(' TExtA = class external name ''ExtA''');
  17003. Add(' end;');
  17004. Add(' TMyB = class(TExtA)');
  17005. Add(' protected');
  17006. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17007. Add(' end;');
  17008. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17009. Add('begin end;');
  17010. Add('begin');
  17011. ConvertProgram;
  17012. CheckSource('TestExternalClass_NewInstance',
  17013. LinesToStr([ // statements
  17014. 'rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {',
  17015. ' this.$init = function () {',
  17016. ' };',
  17017. ' this.$final = function () {',
  17018. ' };',
  17019. ' this.NewInstance = function (fnname, paramarray) {',
  17020. ' var Result = null;',
  17021. ' return Result;',
  17022. ' };',
  17023. '});',
  17024. '']),
  17025. LinesToStr([ // $mod.$main
  17026. '']));
  17027. end;
  17028. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17029. begin
  17030. StartProgram(false);
  17031. Add('{$modeswitch externalclass}');
  17032. Add('type');
  17033. Add(' TExtA = class external name ''ExtA''');
  17034. Add(' end;');
  17035. Add(' TMyB = class(TExtA)');
  17036. Add(' protected');
  17037. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17038. Add(' end;');
  17039. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17040. Add('begin end;');
  17041. Add('begin');
  17042. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17043. ConvertProgram;
  17044. end;
  17045. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17046. begin
  17047. StartProgram(false);
  17048. Add('{$modeswitch externalclass}');
  17049. Add('type');
  17050. Add(' TExtA = class external name ''ExtA''');
  17051. Add(' end;');
  17052. Add(' TMyB = class(TExtA)');
  17053. Add(' protected');
  17054. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17055. Add(' end;');
  17056. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17057. Add('begin end;');
  17058. Add('begin');
  17059. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17060. nIncompatibleTypeArgNo);
  17061. ConvertProgram;
  17062. end;
  17063. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17064. begin
  17065. StartProgram(false);
  17066. Add('{$modeswitch externalclass}');
  17067. Add('type');
  17068. Add(' TExtA = class external name ''ExtA''');
  17069. Add(' end;');
  17070. Add(' TMyB = class(TExtA)');
  17071. Add(' protected');
  17072. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17073. Add(' end;');
  17074. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17075. Add('begin end;');
  17076. Add('begin');
  17077. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17078. nIncompatibleTypeArgNo);
  17079. ConvertProgram;
  17080. end;
  17081. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17082. begin
  17083. StartProgram(false);
  17084. Add([
  17085. '{$modeswitch externalclass}',
  17086. 'type',
  17087. ' TJSFunction = class external name ''Function''',
  17088. ' end;',
  17089. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17090. ' constructor New(w: word);',
  17091. ' end;',
  17092. ' TBird = class (TExtA)',
  17093. ' public',
  17094. ' Size: word;',
  17095. ' class var Legs: word;',
  17096. ' constructor Create(a: word);',
  17097. ' end;',
  17098. ' TEagle = class (TBird)',
  17099. ' public',
  17100. ' constructor Create(b: word); reintroduce;',
  17101. ' end;',
  17102. 'constructor TBird.Create(a: word);',
  17103. 'begin',
  17104. ' inherited;', // silently ignored
  17105. ' inherited New(a);', // this.$func(a)
  17106. 'end;',
  17107. 'constructor TEagle.Create(b: word);',
  17108. 'begin',
  17109. ' inherited Create(b);',
  17110. 'end;',
  17111. 'var',
  17112. ' Bird: TBird;',
  17113. ' Eagle: TEagle;',
  17114. 'begin',
  17115. ' Bird:=TBird.Create(3);',
  17116. ' Eagle:=TEagle.Create(4);',
  17117. ' Bird.Size:=Bird.Size+5;',
  17118. ' Bird.Legs:=Bird.Legs+6;',
  17119. ' Eagle.Size:=Eagle.Size+5;',
  17120. ' Eagle.Legs:=Eagle.Legs+6;',
  17121. '']);
  17122. ConvertProgram;
  17123. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17124. LinesToStr([ // statements
  17125. 'rtl.createClassExt($mod, "TBird", ExtA, "", function () {',
  17126. ' this.Legs = 0;',
  17127. ' this.$init = function () {',
  17128. ' this.Size = 0;',
  17129. ' };',
  17130. ' this.$final = function () {',
  17131. ' };',
  17132. ' this.Create = function (a) {',
  17133. ' this.$ancestorfunc(a);',
  17134. ' return this;',
  17135. ' };',
  17136. '});',
  17137. 'rtl.createClassExt($mod, "TEagle", $mod.TBird, "", function () {',
  17138. ' this.Create$1 = function (b) {',
  17139. ' $mod.TBird.Create.call(this, b);',
  17140. ' return this;',
  17141. ' };',
  17142. '});',
  17143. 'this.Bird = null;',
  17144. 'this.Eagle = null;',
  17145. '']),
  17146. LinesToStr([ // $mod.$main
  17147. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17148. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17149. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17150. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17151. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17152. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17153. '']));
  17154. end;
  17155. procedure TTestModule.TestExternalClass_PascalProperty;
  17156. begin
  17157. StartProgram(false);
  17158. Add('{$modeswitch externalclass}');
  17159. Add('type');
  17160. Add(' TJSElement = class;');
  17161. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17162. Add(' TJSElement = class external name ''ExtA''');
  17163. Add(' end;');
  17164. Add(' TControl = class(TJSElement)');
  17165. Add(' private');
  17166. Add(' FOnClick: TJSNotifyEvent;');
  17167. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17168. Add(' procedure Click(Sender: TJSElement);');
  17169. Add(' end;');
  17170. Add('procedure TControl.Click(Sender: TJSElement);');
  17171. Add('begin');
  17172. Add(' OnClick(Self);');
  17173. Add('end;');
  17174. Add('var');
  17175. Add(' Ctrl: TControl;');
  17176. Add('begin');
  17177. Add(' Ctrl.OnClick:[email protected];');
  17178. Add(' Ctrl.OnClick(Ctrl);');
  17179. ConvertProgram;
  17180. CheckSource('TestExternalClass_PascalProperty',
  17181. LinesToStr([ // statements
  17182. 'rtl.createClassExt($mod, "TControl", ExtA, "", function () {',
  17183. ' this.$init = function () {',
  17184. ' this.FOnClick = null;',
  17185. ' };',
  17186. ' this.$final = function () {',
  17187. ' this.FOnClick = undefined;',
  17188. ' };',
  17189. ' this.Click = function (Sender) {',
  17190. ' this.FOnClick(this);',
  17191. ' };',
  17192. '});',
  17193. 'this.Ctrl = null;',
  17194. '']),
  17195. LinesToStr([ // $mod.$main
  17196. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17197. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17198. '']));
  17199. end;
  17200. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17201. begin
  17202. StartProgram(false);
  17203. Add([
  17204. '{$modeswitch externalclass}',
  17205. 'type',
  17206. ' IUnknown = interface end;',
  17207. ' TObject = class',
  17208. ' end;',
  17209. ' TChild = class',
  17210. ' end;',
  17211. ' TExtRootA = class external name ''ExtRootA''',
  17212. ' end;',
  17213. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17214. ' end;',
  17215. ' TExtRootB = class external name ''ExtRootB''',
  17216. ' end;',
  17217. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17218. ' end;',
  17219. 'var',
  17220. ' Obj: TObject;',
  17221. ' Child: TChild;',
  17222. ' RootA: TExtRootA;',
  17223. ' ChildA: TExtChildA;',
  17224. ' RootB: TExtRootB;',
  17225. ' ChildB: TExtChildB;',
  17226. ' i: IUnknown;',
  17227. 'begin',
  17228. ' obj:=tobject(roota);',
  17229. ' obj:=tobject(childa);',
  17230. ' child:=tchild(tobject(roota));',
  17231. ' roota:=textroota(obj);',
  17232. ' roota:=textroota(child);',
  17233. ' roota:=textroota(rootb);',
  17234. ' roota:=textroota(childb);',
  17235. ' childa:=textchilda(textroota(obj));',
  17236. ' roota:=TExtRootA(i)',
  17237. '']);
  17238. ConvertProgram;
  17239. CheckSource('TestExternalClass_TypeCastToRootClass',
  17240. LinesToStr([ // statements
  17241. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17242. 'rtl.createClass($mod, "TObject", null, function () {',
  17243. ' this.$init = function () {',
  17244. ' };',
  17245. ' this.$final = function () {',
  17246. ' };',
  17247. '});',
  17248. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  17249. '});',
  17250. 'this.Obj = null;',
  17251. 'this.Child = null;',
  17252. 'this.RootA = null;',
  17253. 'this.ChildA = null;',
  17254. 'this.RootB = null;',
  17255. 'this.ChildB = null;',
  17256. 'this.i = null;',
  17257. '']),
  17258. LinesToStr([ // $mod.$main
  17259. '$mod.Obj = $mod.RootA;',
  17260. '$mod.Obj = $mod.ChildA;',
  17261. '$mod.Child = $mod.RootA;',
  17262. '$mod.RootA = $mod.Obj;',
  17263. '$mod.RootA = $mod.Child;',
  17264. '$mod.RootA = $mod.RootB;',
  17265. '$mod.RootA = $mod.ChildB;',
  17266. '$mod.ChildA = $mod.Obj;',
  17267. '$mod.RootA = $mod.i;',
  17268. '']));
  17269. end;
  17270. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17271. begin
  17272. StartProgram(false);
  17273. Add([
  17274. '{$modeswitch externalclass}',
  17275. 'type',
  17276. ' IUnknown = interface end;',
  17277. ' IBird = interface(IUnknown) end;',
  17278. ' TClass = class of TObject;',
  17279. ' TObject = class',
  17280. ' end;',
  17281. ' TChild = class',
  17282. ' end;',
  17283. ' TJSObject = class external name ''Object''',
  17284. ' end;',
  17285. ' TRec = record end;',
  17286. 'var',
  17287. ' Obj: TObject;',
  17288. ' Child: TChild;',
  17289. ' i: IUnknown;',
  17290. ' Bird: IBird;',
  17291. ' j: TJSObject;',
  17292. ' r: TRec;',
  17293. ' c: TClass;',
  17294. 'begin',
  17295. ' j:=tjsobject(IUnknown);',
  17296. ' j:=tjsobject(IBird);',
  17297. ' j:=tjsobject(TObject);',
  17298. ' j:=tjsobject(TChild);',
  17299. ' j:=tjsobject(TRec);',
  17300. ' j:=tjsobject(Obj);',
  17301. ' j:=tjsobject(Child);',
  17302. ' j:=tjsobject(i);',
  17303. ' j:=tjsobject(Bird);',
  17304. ' j:=tjsobject(r);',
  17305. ' j:=tjsobject(c);',
  17306. '']);
  17307. ConvertProgram;
  17308. CheckSource('TestExternalClass_TypeCastToJSObject',
  17309. LinesToStr([ // statements
  17310. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17311. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  17312. 'rtl.createClass($mod, "TObject", null, function () {',
  17313. ' this.$init = function () {',
  17314. ' };',
  17315. ' this.$final = function () {',
  17316. ' };',
  17317. '});',
  17318. 'rtl.createClass($mod, "TChild", $mod.TObject, function () {',
  17319. '});',
  17320. 'rtl.recNewT($mod, "TRec", function () {',
  17321. ' this.$eq = function (b) {',
  17322. ' return true;',
  17323. ' };',
  17324. ' this.$assign = function (s) {',
  17325. ' return this;',
  17326. ' };',
  17327. '});',
  17328. 'this.Obj = null;',
  17329. 'this.Child = null;',
  17330. 'this.i = null;',
  17331. 'this.Bird = null;',
  17332. 'this.j = null;',
  17333. 'this.r = $mod.TRec.$new();',
  17334. 'this.c = null;',
  17335. '']),
  17336. LinesToStr([ // $mod.$main
  17337. '$mod.j = $mod.IUnknown;',
  17338. '$mod.j = $mod.IBird;',
  17339. '$mod.j = $mod.TObject;',
  17340. '$mod.j = $mod.TChild;',
  17341. '$mod.j = $mod.TRec;',
  17342. '$mod.j = $mod.Obj;',
  17343. '$mod.j = $mod.Child;',
  17344. '$mod.j = $mod.i;',
  17345. '$mod.j = $mod.Bird;',
  17346. '$mod.j = $mod.r;',
  17347. '$mod.j = $mod.c;',
  17348. '']));
  17349. end;
  17350. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17351. begin
  17352. StartProgram(false);
  17353. Add('{$modeswitch externalclass}');
  17354. Add('type');
  17355. Add(' TJSString = class external name ''String''');
  17356. Add(' class function fromCharCode() : string; varargs;');
  17357. Add(' function anchor(const aName : string) : string;');
  17358. Add(' end;');
  17359. Add('var');
  17360. Add(' s: string;');
  17361. Add('begin');
  17362. Add(' s:=TJSString.fromCharCode(65,66);');
  17363. Add(' s:=TJSString(s).anchor(s);');
  17364. Add(' s:=TJSString(''foo'').anchor(s);');
  17365. ConvertProgram;
  17366. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17367. LinesToStr([ // statements
  17368. 'this.s = "";',
  17369. '']),
  17370. LinesToStr([ // $mod.$main
  17371. '$mod.s = String.fromCharCode(65, 66);',
  17372. '$mod.s = $mod.s.anchor($mod.s);',
  17373. '$mod.s = "foo".anchor($mod.s);',
  17374. '']));
  17375. end;
  17376. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17377. begin
  17378. StartProgram(false);
  17379. Add([
  17380. '{$modeswitch externalclass}',
  17381. 'type',
  17382. ' TJSObject = class external name ''Object'' end;',
  17383. ' TJSFunction = class external name ''Function''',
  17384. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17385. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17386. ' end;',
  17387. ' TObject = class',
  17388. ' procedure DoIt(i: longint);',
  17389. ' end;',
  17390. ' TFuncInt = function(o: TObject): longint;',
  17391. 'function GetIt(o: TObject): longint;',
  17392. ' procedure Sub; begin end;',
  17393. 'var',
  17394. ' f: TJSFunction;',
  17395. ' fi: TFuncInt;',
  17396. 'begin',
  17397. ' fi:=TFuncInt(f);',
  17398. ' f:=TJSFunction(fi);',
  17399. ' f:=TJSFunction(@GetIt);',
  17400. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  17401. ' f:=TJSFunction(@Sub);',
  17402. ' f:=TJSFunction(@o.doit);',
  17403. ' f:=TJSFunction(fi).bind(nil,4)',
  17404. 'end;',
  17405. 'procedure TObject.DoIt(i: longint);',
  17406. ' procedure Sub; begin end;',
  17407. 'var f: TJSFunction;',
  17408. 'begin',
  17409. ' f:=TJSFunction(@DoIt);',
  17410. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  17411. ' f:=TJSFunction(@Sub);',
  17412. ' f:=TJSFunction(@GetIt);',
  17413. 'end;',
  17414. 'begin']);
  17415. ConvertProgram;
  17416. CheckSource('TestExternalClass_TypeCastToJSFunction',
  17417. LinesToStr([ // statements
  17418. 'rtl.createClass($mod, "TObject", null, function () {',
  17419. ' this.$init = function () {',
  17420. ' };',
  17421. ' this.$final = function () {',
  17422. ' };',
  17423. ' this.DoIt = function (i) {',
  17424. ' var $Self = this;',
  17425. ' function Sub() {',
  17426. ' };',
  17427. ' var f = null;',
  17428. ' f = $Self.DoIt;',
  17429. ' f = $Self.DoIt.bind(null, 13);',
  17430. ' f = Sub;',
  17431. ' f = $mod.GetIt;',
  17432. ' };',
  17433. '});',
  17434. 'this.GetIt = function (o) {',
  17435. ' var Result = 0;',
  17436. ' function Sub() {',
  17437. ' };',
  17438. ' var f = null;',
  17439. ' var fi = null;',
  17440. ' fi = f;',
  17441. ' f = fi;',
  17442. ' f = $mod.GetIt;',
  17443. ' f = $mod.GetIt.bind(null, 3);',
  17444. ' f = Sub;',
  17445. ' f = $mod.TObject.DoIt;',
  17446. ' f = fi.bind(null, 4);',
  17447. ' return Result;',
  17448. '};',
  17449. '']),
  17450. LinesToStr([ // $mod.$main
  17451. '']));
  17452. end;
  17453. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  17454. begin
  17455. StartProgram(false);
  17456. Add([
  17457. '{$mode delphi}',
  17458. '{$modeswitch externalclass}',
  17459. 'type',
  17460. ' TJSObject = class external name ''Object'' end;',
  17461. ' TJSWindow = class external name ''Window''(TJSObject)',
  17462. ' procedure Open;',
  17463. ' end;',
  17464. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  17465. ' procedure Execute;',
  17466. ' end;',
  17467. 'procedure Fly;',
  17468. 'var',
  17469. ' w: TJSWindow;',
  17470. ' e: TJSEventTarget;',
  17471. 'begin',
  17472. ' w:=TJSWindow(e);',
  17473. ' e:=TJSEventTarget(w);',
  17474. 'end;',
  17475. 'begin']);
  17476. ConvertProgram;
  17477. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  17478. LinesToStr([ // statements
  17479. 'this.Fly = function () {',
  17480. ' var w = null;',
  17481. ' var e = null;',
  17482. ' w = e;',
  17483. ' e = w;',
  17484. '};',
  17485. '']),
  17486. LinesToStr([ // $mod.$main
  17487. '']));
  17488. end;
  17489. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  17490. begin
  17491. StartProgram(false);
  17492. Add('{$modeswitch externalclass}');
  17493. Add('type');
  17494. Add(' TJSString = class external name ''String''');
  17495. Add(' class function fromCharCode() : string; varargs;');
  17496. Add(' end;');
  17497. Add('var');
  17498. Add(' s: string;');
  17499. Add(' sObj: TJSString;');
  17500. Add('begin');
  17501. Add(' s:=sObj.fromCharCode(65,66);');
  17502. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  17503. nExternalClassInstanceCannotAccessStaticX);
  17504. ConvertProgram;
  17505. end;
  17506. procedure TTestModule.TestExternalClass_BracketAccessor;
  17507. begin
  17508. StartProgram(false);
  17509. Add([
  17510. '{$modeswitch externalclass}',
  17511. 'type',
  17512. ' TJSArray = class external name ''Array2''',
  17513. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17514. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17515. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17516. ' end;',
  17517. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  17518. 'begin end;',
  17519. 'var',
  17520. ' Arr: tjsarray;',
  17521. ' s: string;',
  17522. ' i: longint;',
  17523. ' v: jsvalue;',
  17524. 'begin',
  17525. ' v:=arr[0];',
  17526. ' v:=arr.items[1];',
  17527. ' arr[2]:=s;',
  17528. ' arr.items[3]:=s;',
  17529. ' arr[4]:=i;',
  17530. ' arr[5]:=arr[6];',
  17531. ' arr.items[7]:=arr.items[8];',
  17532. ' with arr do items[9]:=items[10];',
  17533. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  17534. ' with arr do begin',
  17535. ' v:=GetItems(14);',
  17536. ' setitems(15,16);',
  17537. ' end;',
  17538. ' v:=test1.arr.items[17];',
  17539. ' test1.arr.items[18]:=v;',
  17540. '']);
  17541. ConvertProgram;
  17542. CheckSource('TestExternalClass_BracketAccessor',
  17543. LinesToStr([ // statements
  17544. 'this.DoIt = function (vI, vJ, vK, vL) {',
  17545. '};',
  17546. 'this.Arr = null;',
  17547. 'this.s = "";',
  17548. 'this.i = 0;',
  17549. 'this.v = undefined;',
  17550. '']),
  17551. LinesToStr([ // $mod.$main
  17552. '$mod.v = $mod.Arr[0];',
  17553. '$mod.v = $mod.Arr[1];',
  17554. '$mod.Arr[2] = $mod.s;',
  17555. '$mod.Arr[3] = $mod.s;',
  17556. '$mod.Arr[4] = $mod.i;',
  17557. '$mod.Arr[5] = $mod.Arr[6];',
  17558. '$mod.Arr[7] = $mod.Arr[8];',
  17559. 'var $with = $mod.Arr;',
  17560. '$with[9] = $with[10];',
  17561. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  17562. ' a: 9,',
  17563. ' p: $mod.Arr,',
  17564. ' get: function () {',
  17565. ' return this.p[this.a];',
  17566. ' },',
  17567. ' set: function (v) {',
  17568. ' this.p[this.a] = v;',
  17569. ' }',
  17570. '}, {',
  17571. ' a: 10,',
  17572. ' p: $mod.Arr,',
  17573. ' get: function () {',
  17574. ' return this.p[this.a];',
  17575. ' },',
  17576. ' set: function (v) {',
  17577. ' this.p[this.a] = v;',
  17578. ' }',
  17579. '});',
  17580. 'var $with1 = $mod.Arr;',
  17581. '$mod.v = $with1[14];',
  17582. '$with1[15] = 16;',
  17583. '$mod.v = $mod.Arr[17];',
  17584. '$mod.Arr[18] = $mod.v;',
  17585. '']));
  17586. end;
  17587. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  17588. begin
  17589. StartProgram(false);
  17590. Add([
  17591. '{$modeswitch externalclass}',
  17592. 'type',
  17593. ' TJSArray = class external name ''Array2''',
  17594. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17595. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17596. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17597. ' end;',
  17598. ' TMyArr = class(TJSArray)',
  17599. ' procedure DoIt;',
  17600. ' end;',
  17601. 'procedure tmyarr.DoIt;',
  17602. 'begin',
  17603. ' Items[1]:=Items[2];',
  17604. ' SetItems(3,getItems(4));',
  17605. 'end;',
  17606. 'var',
  17607. ' Arr: tmyarr;',
  17608. ' s: string;',
  17609. ' i: longint;',
  17610. ' v: jsvalue;',
  17611. 'begin',
  17612. ' v:=arr[0];',
  17613. ' v:=arr.items[1];',
  17614. ' arr[2]:=s;',
  17615. ' arr.items[3]:=s;',
  17616. ' arr[4]:=i;',
  17617. ' arr[5]:=arr[6];',
  17618. ' arr.items[7]:=arr.items[8];',
  17619. ' with arr do items[9]:=items[10];',
  17620. ' with arr do begin',
  17621. ' v:=GetItems(14);',
  17622. ' setitems(15,16);',
  17623. ' end;',
  17624. '']);
  17625. ConvertProgram;
  17626. CheckSource('TestExternalClass_BracketAccessor_Call',
  17627. LinesToStr([ // statements
  17628. 'rtl.createClassExt($mod, "TMyArr", Array2, "", function () {',
  17629. ' this.$init = function () {',
  17630. ' };',
  17631. ' this.$final = function () {',
  17632. ' };',
  17633. ' this.DoIt = function () {',
  17634. ' this[1] = this[2];',
  17635. ' this[3] = this[4];',
  17636. ' };',
  17637. '});',
  17638. 'this.Arr = null;',
  17639. 'this.s = "";',
  17640. 'this.i = 0;',
  17641. 'this.v = undefined;',
  17642. '']),
  17643. LinesToStr([ // $mod.$main
  17644. '$mod.v = $mod.Arr[0];',
  17645. '$mod.v = $mod.Arr[1];',
  17646. '$mod.Arr[2] = $mod.s;',
  17647. '$mod.Arr[3] = $mod.s;',
  17648. '$mod.Arr[4] = $mod.i;',
  17649. '$mod.Arr[5] = $mod.Arr[6];',
  17650. '$mod.Arr[7] = $mod.Arr[8];',
  17651. 'var $with = $mod.Arr;',
  17652. '$with[9] = $with[10];',
  17653. 'var $with1 = $mod.Arr;',
  17654. '$mod.v = $with1[14];',
  17655. '$with1[15] = 16;',
  17656. '']));
  17657. end;
  17658. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  17659. begin
  17660. StartProgram(false);
  17661. Add('{$modeswitch externalclass}');
  17662. Add('type');
  17663. Add(' TJSArray = class external name ''Array2''');
  17664. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  17665. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  17666. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  17667. Add(' end;');
  17668. Add('begin');
  17669. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  17670. nBracketAccessorOfExternalClassMustHaveOneParameter);
  17671. ConvertProgram;
  17672. end;
  17673. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  17674. begin
  17675. StartProgram(false);
  17676. Add('{$modeswitch externalclass}');
  17677. Add('type');
  17678. Add(' TJSArray = class external name ''Array2''');
  17679. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17680. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  17681. Add(' end;');
  17682. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  17683. Add('begin end;');
  17684. Add('var');
  17685. Add(' Arr: tjsarray;');
  17686. Add(' v: jsvalue;');
  17687. Add('begin');
  17688. Add(' v:=arr[0];');
  17689. Add(' v:=arr.items[1];');
  17690. Add(' with arr do v:=items[2];');
  17691. Add(' doit(arr[3],arr[4]);');
  17692. ConvertProgram;
  17693. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  17694. LinesToStr([ // statements
  17695. 'this.DoIt = function (vI, vJ) {',
  17696. '};',
  17697. 'this.Arr = null;',
  17698. 'this.v = undefined;',
  17699. '']),
  17700. LinesToStr([ // $mod.$main
  17701. '$mod.v = $mod.Arr[0];',
  17702. '$mod.v = $mod.Arr[1];',
  17703. 'var $with = $mod.Arr;',
  17704. '$mod.v = $with[2];',
  17705. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  17706. '']));
  17707. end;
  17708. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  17709. begin
  17710. StartProgram(false);
  17711. Add('{$modeswitch externalclass}');
  17712. Add('type');
  17713. Add(' TJSArray = class external name ''Array2''');
  17714. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17715. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17716. Add(' end;');
  17717. Add('var');
  17718. Add(' Arr: tjsarray;');
  17719. Add(' s: string;');
  17720. Add(' i: longint;');
  17721. Add(' v: jsvalue;');
  17722. Add('begin');
  17723. Add(' arr[2]:=s;');
  17724. Add(' arr.items[3]:=s;');
  17725. Add(' arr[4]:=i;');
  17726. Add(' with arr do items[5]:=i;');
  17727. ConvertProgram;
  17728. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  17729. LinesToStr([ // statements
  17730. 'this.Arr = null;',
  17731. 'this.s = "";',
  17732. 'this.i = 0;',
  17733. 'this.v = undefined;',
  17734. '']),
  17735. LinesToStr([ // $mod.$main
  17736. '$mod.Arr[2] = $mod.s;',
  17737. '$mod.Arr[3] = $mod.s;',
  17738. '$mod.Arr[4] = $mod.i;',
  17739. 'var $with = $mod.Arr;',
  17740. '$with[5] = $mod.i;',
  17741. '']));
  17742. end;
  17743. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  17744. begin
  17745. StartProgram(false);
  17746. Add('{$modeswitch externalclass}');
  17747. Add('type');
  17748. Add(' TJSArray = class external name ''Array2''');
  17749. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17750. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  17751. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  17752. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  17753. Add(' end;');
  17754. Add('var');
  17755. Add(' Arr: tjsarray;');
  17756. Add(' s: string;');
  17757. Add(' i: longint;');
  17758. Add(' v: jsvalue;');
  17759. Add('begin');
  17760. Add(' arr[2]:=s;');
  17761. Add(' arr.items[3]:=s;');
  17762. Add(' arr.numbers[4]:=i;');
  17763. Add(' with arr do items[5]:=i;');
  17764. Add(' with arr do numbers[6]:=i;');
  17765. ConvertProgram;
  17766. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  17767. LinesToStr([ // statements
  17768. 'this.Arr = null;',
  17769. 'this.s = "";',
  17770. 'this.i = 0;',
  17771. 'this.v = undefined;',
  17772. '']),
  17773. LinesToStr([ // $mod.$main
  17774. '$mod.Arr[2] = $mod.s;',
  17775. '$mod.Arr[3] = $mod.s;',
  17776. '$mod.Arr[4] = $mod.i;',
  17777. 'var $with = $mod.Arr;',
  17778. '$with[5] = $mod.i;',
  17779. 'var $with1 = $mod.Arr;',
  17780. '$with1[6] = $mod.i;',
  17781. '']));
  17782. end;
  17783. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  17784. begin
  17785. StartProgram(false);
  17786. Add('{$modeswitch externalclass}');
  17787. Add('type');
  17788. Add(' TJSArray = class external name ''Array2''');
  17789. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  17790. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  17791. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  17792. Add(' end;');
  17793. Add('var');
  17794. Add(' Arr: tjsarray;');
  17795. Add(' i: longint;');
  17796. Add(' IntArr: array of longint;');
  17797. Add(' v: jsvalue;');
  17798. Add('begin');
  17799. Add(' v:=arr.items[i];');
  17800. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  17801. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  17802. ConvertProgram;
  17803. CheckSource('TestExternalClass_BracketAccessor_Index',
  17804. LinesToStr([ // statements
  17805. 'this.Arr = null;',
  17806. 'this.i = 0;',
  17807. 'this.IntArr = [];',
  17808. 'this.v = undefined;',
  17809. '']),
  17810. LinesToStr([ // $mod.$main
  17811. '$mod.v = $mod.Arr[$mod.i];',
  17812. '$mod.Arr[Math.floor($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  17813. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  17814. '']));
  17815. end;
  17816. procedure TTestModule.TestExternalClass_ForInJSObject;
  17817. begin
  17818. StartProgram(false);
  17819. Add([
  17820. '{$modeswitch externalclass}',
  17821. 'type',
  17822. ' TJSObject = class external name ''Object''',
  17823. ' end;',
  17824. 'var',
  17825. ' o: TJSObject;',
  17826. ' key: string;',
  17827. 'begin',
  17828. ' for key in o do',
  17829. ' if key=''abc'' then ;',
  17830. '']);
  17831. ConvertProgram;
  17832. CheckSource('TestExternalClass_ForInJSObject',
  17833. LinesToStr([ // statements
  17834. 'this.o = null;',
  17835. 'this.key = "";',
  17836. '']),
  17837. LinesToStr([ // $mod.$main
  17838. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  17839. '']));
  17840. end;
  17841. procedure TTestModule.TestExternalClass_ForInJSArray;
  17842. begin
  17843. StartProgram(false);
  17844. Add([
  17845. '{$modeswitch externalclass}',
  17846. 'type',
  17847. ' TJSInt8Array = class external name ''Int8Array''',
  17848. ' private',
  17849. ' flength: NativeInt external name ''length'';',
  17850. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  17851. ' public',
  17852. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  17853. ' property Length: NativeInt read flength;',
  17854. ' end;',
  17855. 'var',
  17856. ' a: TJSInt8Array;',
  17857. ' value: shortint;',
  17858. 'begin',
  17859. ' for value in a do',
  17860. ' if value=3 then ;',
  17861. '']);
  17862. ConvertProgram;
  17863. CheckSource('TestExternalClass_ForInJSArray',
  17864. LinesToStr([ // statements
  17865. 'this.a = null;',
  17866. 'this.value = 0;',
  17867. '']),
  17868. LinesToStr([ // $mod.$main
  17869. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  17870. ' $mod.value = $in[$l];',
  17871. ' if ($mod.value === 3) ;',
  17872. '};',
  17873. '']));
  17874. end;
  17875. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  17876. begin
  17877. AddModuleWithIntfImplSrc('unit2.pas',
  17878. LinesToStr([
  17879. '{$modeswitch externalclass}',
  17880. 'type',
  17881. ' TJSBufferSource = class external name ''BufferSource''',
  17882. ' end;',
  17883. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  17884. '']),
  17885. '');
  17886. AddModuleWithIntfImplSrc('unit3.pas',
  17887. LinesToStr([
  17888. '{$modeswitch externalclass}',
  17889. 'type',
  17890. ' TJSBufferSource = class external name ''BufferSource''',
  17891. ' end;',
  17892. '']),
  17893. '');
  17894. StartUnit(true);
  17895. Add([
  17896. 'interface',
  17897. 'uses unit2, unit3;',
  17898. 'procedure DoSome(s: TJSBufferSource);',
  17899. 'implementation',
  17900. 'procedure DoSome(s: TJSBufferSource);',
  17901. 'begin',
  17902. ' DoIt(s);',
  17903. 'end;',
  17904. '']);
  17905. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  17906. nIncompatibleTypeArgNo);
  17907. ConvertUnit;
  17908. end;
  17909. procedure TTestModule.TestClassInterface_Corba;
  17910. begin
  17911. StartProgram(false);
  17912. Add([
  17913. '{$interfaces corba}',
  17914. 'type',
  17915. ' IUnknown = interface;',
  17916. ' IUnknown = interface',
  17917. ' [''{00000000-0000-0000-C000-000000000046}'']',
  17918. ' end;',
  17919. ' IInterface = IUnknown;',
  17920. ' IBird = interface(IInterface)',
  17921. ' function GetSize: longint;',
  17922. ' procedure SetSize(i: longint);',
  17923. ' property Size: longint read GetSize write SetSize;',
  17924. ' procedure DoIt(i: longint);',
  17925. ' end;',
  17926. ' TObject = class',
  17927. ' end;',
  17928. ' TBird = class(TObject,IBird)',
  17929. ' function GetSize: longint; virtual; abstract;',
  17930. ' procedure SetSize(i: longint); virtual; abstract;',
  17931. ' procedure DoIt(i: longint); virtual; abstract;',
  17932. ' end;',
  17933. 'var',
  17934. ' BirdIntf: IBird;',
  17935. 'begin',
  17936. ' BirdIntf.Size:=BirdIntf.Size;',
  17937. '']);
  17938. ConvertProgram;
  17939. CheckSource('TestClassInterface_Corba',
  17940. LinesToStr([ // statements
  17941. 'rtl.createInterface($mod, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  17942. 'rtl.createInterface($mod, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], $mod.IUnknown);',
  17943. 'rtl.createClass($mod, "TObject", null, function () {',
  17944. ' this.$init = function () {',
  17945. ' };',
  17946. ' this.$final = function () {',
  17947. ' };',
  17948. '});',
  17949. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  17950. ' rtl.addIntf(this, $mod.IBird);',
  17951. '});',
  17952. 'this.BirdIntf = null;',
  17953. '']),
  17954. LinesToStr([ // $mod.$main
  17955. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  17956. '']));
  17957. end;
  17958. procedure TTestModule.TestClassInterface_ProcExternalFail;
  17959. begin
  17960. StartProgram(false);
  17961. Add([
  17962. '{$interfaces corba}',
  17963. 'type',
  17964. ' IUnknown = interface',
  17965. ' procedure DoIt; external name ''foo'';',
  17966. ' end;',
  17967. 'begin']);
  17968. SetExpectedParserError(
  17969. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  17970. nParserNoFieldsAllowed);
  17971. ConvertProgram;
  17972. end;
  17973. procedure TTestModule.TestClassInterface_Overloads;
  17974. begin
  17975. StartProgram(false);
  17976. Add([
  17977. '{$interfaces corba}',
  17978. 'type',
  17979. ' integer = longint;',
  17980. ' IUnknown = interface',
  17981. ' procedure DoIt(i: integer);',
  17982. ' procedure DoIt(s: string);',
  17983. ' end;',
  17984. ' IBird = interface(IUnknown)',
  17985. ' procedure DoIt(b: boolean); overload;',
  17986. ' end;',
  17987. ' TObject = class',
  17988. ' end;',
  17989. ' TBird = class(TObject,IBird)',
  17990. ' procedure DoIt(o: TObject);',
  17991. ' procedure DoIt(s: string);',
  17992. ' procedure DoIt(i: integer);',
  17993. ' procedure DoIt(b: boolean);',
  17994. ' end;',
  17995. 'procedure TBird.DoIt(o: TObject); begin end;',
  17996. 'procedure TBird.DoIt(s: string); begin end;',
  17997. 'procedure TBird.DoIt(i: integer); begin end;',
  17998. 'procedure TBird.DoIt(b: boolean); begin end;',
  17999. 'var',
  18000. ' BirdIntf: IBird;',
  18001. 'begin',
  18002. ' BirdIntf.DoIt(3);',
  18003. ' BirdIntf.DoIt(''abc'');',
  18004. ' BirdIntf.DoIt(true);',
  18005. '']);
  18006. ConvertProgram;
  18007. CheckSource('TestClassInterface_Overloads',
  18008. LinesToStr([ // statements
  18009. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18010. 'rtl.createInterface($mod, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], $mod.IUnknown);',
  18011. 'rtl.createClass($mod, "TObject", null, function () {',
  18012. ' this.$init = function () {',
  18013. ' };',
  18014. ' this.$final = function () {',
  18015. ' };',
  18016. '});',
  18017. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18018. ' this.DoIt = function (o) {',
  18019. ' };',
  18020. ' this.DoIt$1 = function (s) {',
  18021. ' };',
  18022. ' this.DoIt$2 = function (i) {',
  18023. ' };',
  18024. ' this.DoIt$3 = function (b) {',
  18025. ' };',
  18026. ' rtl.addIntf(this, $mod.IBird, {',
  18027. ' DoIt$2: "DoIt$3",',
  18028. ' DoIt: "DoIt$2"',
  18029. ' });',
  18030. '});',
  18031. 'this.BirdIntf = null;',
  18032. '']),
  18033. LinesToStr([ // $mod.$main
  18034. '$mod.BirdIntf.DoIt(3);',
  18035. '$mod.BirdIntf.DoIt$1("abc");',
  18036. '$mod.BirdIntf.DoIt$2(true);',
  18037. '']));
  18038. end;
  18039. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18040. begin
  18041. StartProgram(false);
  18042. Add([
  18043. '{$interfaces corba}',
  18044. 'type',
  18045. ' IBird = interface',
  18046. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18047. ' end;',
  18048. ' IDog = interface',
  18049. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18050. ' end;',
  18051. ' TObject = class(IBird,IDog)',
  18052. ' end;',
  18053. 'begin']);
  18054. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18055. nDuplicateGUIDXInYZ);
  18056. ConvertProgram;
  18057. end;
  18058. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18059. begin
  18060. StartProgram(false);
  18061. Add([
  18062. '{$interfaces corba}',
  18063. 'type',
  18064. ' IAnimal = interface',
  18065. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18066. ' end;',
  18067. ' IBird = interface(IAnimal)',
  18068. ' end;',
  18069. ' IHawk = interface(IBird)',
  18070. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18071. ' end;',
  18072. 'begin']);
  18073. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18074. nDuplicateGUIDXInYZ);
  18075. ConvertProgram;
  18076. end;
  18077. procedure TTestModule.TestClassInterface_AncestorImpl;
  18078. begin
  18079. StartProgram(false);
  18080. Add([
  18081. '{$interfaces corba}',
  18082. 'type',
  18083. ' integer = longint;',
  18084. ' IUnknown = interface',
  18085. ' procedure DoIt(i: integer);',
  18086. ' end;',
  18087. ' IBird = interface',
  18088. ' procedure Fly(i: integer);',
  18089. ' end;',
  18090. ' TObject = class(IUnknown)',
  18091. ' procedure DoIt(i: integer);',
  18092. ' end;',
  18093. ' TBird = class(IBird)',
  18094. ' procedure Fly(i: integer);',
  18095. ' end;',
  18096. 'procedure TObject.DoIt(i: integer); begin end;',
  18097. 'procedure TBird.Fly(i: integer); begin end;',
  18098. 'begin',
  18099. '']);
  18100. ConvertProgram;
  18101. CheckSource('TestClassInterface_AncestorIntf',
  18102. LinesToStr([ // statements
  18103. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18104. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18105. 'rtl.createClass($mod, "TObject", null, function () {',
  18106. ' this.$init = function () {',
  18107. ' };',
  18108. ' this.$final = function () {',
  18109. ' };',
  18110. ' this.DoIt = function (i) {',
  18111. ' };',
  18112. ' rtl.addIntf(this, $mod.IUnknown);',
  18113. '});',
  18114. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18115. ' this.Fly = function (i) {',
  18116. ' };',
  18117. ' rtl.addIntf(this, $mod.IBird);',
  18118. ' rtl.addIntf(this, $mod.IUnknown);',
  18119. '});',
  18120. '']),
  18121. LinesToStr([ // $mod.$main
  18122. '']));
  18123. end;
  18124. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18125. begin
  18126. StartProgram(false);
  18127. Add([
  18128. '{$interfaces corba}',
  18129. 'type',
  18130. ' integer = longint;',
  18131. ' IBird = interface',
  18132. ' procedure DoIt(i: integer);',
  18133. ' end;',
  18134. ' TObject = class',
  18135. ' procedure DoIt(i: integer);',
  18136. ' end;',
  18137. ' TBird = class(IBird)',
  18138. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18139. ' end;',
  18140. 'procedure TObject.DoIt(i: integer); begin end;',
  18141. 'procedure TBird.DoIt(i: integer); begin end;',
  18142. 'begin',
  18143. '']);
  18144. ConvertProgram;
  18145. CheckSource('TestClassInterface_ImplReintroduce',
  18146. LinesToStr([ // statements
  18147. 'rtl.createInterface($mod, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18148. 'rtl.createClass($mod, "TObject", null, function () {',
  18149. ' this.$init = function () {',
  18150. ' };',
  18151. ' this.$final = function () {',
  18152. ' };',
  18153. ' this.DoIt = function (i) {',
  18154. ' };',
  18155. '});',
  18156. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18157. ' this.DoIt$1 = function (i) {',
  18158. ' };',
  18159. ' rtl.addIntf(this, $mod.IBird, {',
  18160. ' DoIt: "DoIt$1"',
  18161. ' });',
  18162. '});',
  18163. '']),
  18164. LinesToStr([ // $mod.$main
  18165. '']));
  18166. end;
  18167. procedure TTestModule.TestClassInterface_MethodResolution;
  18168. begin
  18169. StartProgram(false);
  18170. Add([
  18171. '{$interfaces corba}',
  18172. 'type',
  18173. ' IUnknown = interface',
  18174. ' procedure Walk(i: longint);',
  18175. ' end;',
  18176. ' IBird = interface(IUnknown)',
  18177. ' procedure Walk(b: boolean); overload;',
  18178. ' procedure Fly(s: string);',
  18179. ' end;',
  18180. ' TObject = class',
  18181. ' end;',
  18182. ' TBird = class(TObject,IBird)',
  18183. ' procedure IBird.Fly = Move;',
  18184. ' procedure IBird.Walk = Hop;',
  18185. ' procedure Hop(i: longint);',
  18186. ' procedure Move(s: string);',
  18187. ' procedure Hop(b: boolean);',
  18188. ' end;',
  18189. 'procedure TBird.Move(s: string); begin end;',
  18190. 'procedure TBird.Hop(i: longint); begin end;',
  18191. 'procedure TBird.Hop(b: boolean); begin end;',
  18192. 'var',
  18193. ' BirdIntf: IBird;',
  18194. 'begin',
  18195. ' BirdIntf.Walk(3);',
  18196. ' BirdIntf.Walk(true);',
  18197. ' BirdIntf.Fly(''abc'');',
  18198. '']);
  18199. ConvertProgram;
  18200. CheckSource('TestClassInterface_MethodResolution',
  18201. LinesToStr([ // statements
  18202. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18203. 'rtl.createInterface($mod, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], $mod.IUnknown);',
  18204. 'rtl.createClass($mod, "TObject", null, function () {',
  18205. ' this.$init = function () {',
  18206. ' };',
  18207. ' this.$final = function () {',
  18208. ' };',
  18209. '});',
  18210. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18211. ' this.Hop = function (i) {',
  18212. ' };',
  18213. ' this.Move = function (s) {',
  18214. ' };',
  18215. ' this.Hop$1 = function (b) {',
  18216. ' };',
  18217. ' rtl.addIntf(this, $mod.IBird, {',
  18218. ' Walk$1: "Hop$1",',
  18219. ' Fly: "Move",',
  18220. ' Walk: "Hop"',
  18221. ' });',
  18222. '});',
  18223. 'this.BirdIntf = null;',
  18224. '']),
  18225. LinesToStr([ // $mod.$main
  18226. '$mod.BirdIntf.Walk(3);',
  18227. '$mod.BirdIntf.Walk$1(true);',
  18228. '$mod.BirdIntf.Fly("abc");',
  18229. '']));
  18230. end;
  18231. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18232. begin
  18233. StartProgram(false);
  18234. Add([
  18235. '{$interfaces com}',
  18236. 'type',
  18237. ' IUnknown = interface',
  18238. ' function _AddRef: longint;',
  18239. ' procedure Walk;',
  18240. ' end;',
  18241. ' IBird = interface end;',
  18242. ' IDog = interface end;',
  18243. ' TObject = class(IBird,IDog)',
  18244. ' function _AddRef: longint; virtual; abstract;',
  18245. ' procedure Walk; virtual; abstract;',
  18246. ' end;',
  18247. ' TBird = class(IUnknown)',
  18248. ' end;',
  18249. 'begin',
  18250. '']);
  18251. ConvertProgram;
  18252. CheckSource('TestClassInterface_COM_AncestorLess',
  18253. LinesToStr([ // statements
  18254. 'rtl.createInterface($mod, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18255. 'rtl.createInterface($mod, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], $mod.IUnknown);',
  18256. 'rtl.createInterface($mod, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], $mod.IUnknown);',
  18257. 'rtl.createClass($mod, "TObject", null, function () {',
  18258. ' this.$init = function () {',
  18259. ' };',
  18260. ' this.$final = function () {',
  18261. ' };',
  18262. ' rtl.addIntf(this, $mod.IBird);',
  18263. ' rtl.addIntf(this, $mod.IDog);',
  18264. '});',
  18265. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18266. ' rtl.addIntf(this, $mod.IUnknown);',
  18267. ' rtl.addIntf(this, $mod.IBird);',
  18268. ' rtl.addIntf(this, $mod.IDog);',
  18269. '});',
  18270. '']),
  18271. LinesToStr([ // $mod.$main
  18272. '']));
  18273. end;
  18274. procedure TTestModule.TestClassInterface_MethodOverride;
  18275. begin
  18276. StartProgram(false);
  18277. Add([
  18278. '{$interfaces corba}',
  18279. 'type',
  18280. ' IUnknown = interface',
  18281. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18282. ' procedure Go;',
  18283. ' end;',
  18284. ' TObject = class(IUnknown)',
  18285. ' procedure Go; virtual; abstract;',
  18286. ' end;',
  18287. ' TBird = class',
  18288. ' procedure Go; override;',
  18289. ' end;',
  18290. ' TCat = class(TObject)',
  18291. ' procedure Go; override;',
  18292. ' end;',
  18293. ' TDog = class(TObject, IUnknown)',
  18294. ' procedure Go; override;',
  18295. ' end;',
  18296. 'procedure TBird.Go; begin end;',
  18297. 'procedure TCat.Go; begin end;',
  18298. 'procedure TDog.Go; begin end;',
  18299. 'begin',
  18300. '']);
  18301. ConvertProgram;
  18302. CheckSource('TestClassInterface_MethodOverride',
  18303. LinesToStr([ // statements
  18304. 'rtl.createInterface($mod, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18305. 'rtl.createClass($mod, "TObject", null, function () {',
  18306. ' this.$init = function () {',
  18307. ' };',
  18308. ' this.$final = function () {',
  18309. ' };',
  18310. ' rtl.addIntf(this, $mod.IUnknown);',
  18311. '});',
  18312. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18313. ' this.Go = function () {',
  18314. ' };',
  18315. ' rtl.addIntf(this, $mod.IUnknown);',
  18316. '});',
  18317. 'rtl.createClass($mod, "TCat", $mod.TObject, function () {',
  18318. ' this.Go = function () {',
  18319. ' };',
  18320. ' rtl.addIntf(this, $mod.IUnknown);',
  18321. '});',
  18322. 'rtl.createClass($mod, "TDog", $mod.TObject, function () {',
  18323. ' this.Go = function () {',
  18324. ' };',
  18325. ' rtl.addIntf(this, $mod.IUnknown);',
  18326. '});',
  18327. '']),
  18328. LinesToStr([ // $mod.$main
  18329. '']));
  18330. end;
  18331. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18332. begin
  18333. StartProgram(false);
  18334. Add([
  18335. '{$interfaces corba}',
  18336. 'type',
  18337. ' IUnknown = interface',
  18338. ' end;',
  18339. ' IBird = interface(IUnknown)',
  18340. ' procedure Fly(s: string);',
  18341. ' end;',
  18342. ' IEagle = interface(IBird)',
  18343. ' end;',
  18344. ' IDove = interface(IBird)',
  18345. ' end;',
  18346. ' ISwallow = interface(IBird)',
  18347. ' end;',
  18348. ' TObject = class',
  18349. ' end;',
  18350. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18351. ' procedure Fly(s: string); virtual; abstract;',
  18352. ' end;',
  18353. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18354. ' FBirdIntf: IBird;',
  18355. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18356. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18357. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18358. ' FDoveObj: TBird;',
  18359. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18360. ' function GetSwallowObj: TBird; virtual; abstract;',
  18361. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18362. ' end;',
  18363. 'begin',
  18364. '']);
  18365. ConvertProgram;
  18366. CheckSource('TestClassInterface_Delegation',
  18367. LinesToStr([ // statements
  18368. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18369. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  18370. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  18371. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  18372. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  18373. 'rtl.createClass($mod, "TObject", null, function () {',
  18374. ' this.$init = function () {',
  18375. ' };',
  18376. ' this.$final = function () {',
  18377. ' };',
  18378. '});',
  18379. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18380. ' rtl.addIntf(this, $mod.IBird);',
  18381. ' rtl.addIntf(this, $mod.IEagle);',
  18382. ' rtl.addIntf(this, $mod.IDove);',
  18383. ' rtl.addIntf(this, $mod.ISwallow);',
  18384. '});',
  18385. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18386. ' this.$init = function () {',
  18387. ' $mod.TObject.$init.call(this);',
  18388. ' this.FBirdIntf = null;',
  18389. ' this.FDoveObj = null;',
  18390. ' };',
  18391. ' this.$final = function () {',
  18392. ' this.FBirdIntf = undefined;',
  18393. ' this.FDoveObj = undefined;',
  18394. ' $mod.TObject.$final.call(this);',
  18395. ' };',
  18396. ' this.$intfmaps = {',
  18397. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18398. ' return this.FBirdIntf;',
  18399. ' },',
  18400. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18401. ' return this.GetEagleIntf();',
  18402. ' },',
  18403. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18404. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18405. ' },',
  18406. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18407. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18408. ' }',
  18409. ' };',
  18410. '});',
  18411. '']),
  18412. LinesToStr([ // $mod.$main
  18413. '']));
  18414. end;
  18415. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  18416. begin
  18417. StartProgram(false);
  18418. Add([
  18419. '{$interfaces corba}',
  18420. 'type',
  18421. ' IUnknown = interface',
  18422. ' end;',
  18423. ' IBird = interface(IUnknown)',
  18424. ' procedure Fly(s: string);',
  18425. ' end;',
  18426. ' IEagle = interface(IBird)',
  18427. ' end;',
  18428. ' IDove = interface(IBird)',
  18429. ' end;',
  18430. ' ISwallow = interface(IBird)',
  18431. ' end;',
  18432. ' TObject = class',
  18433. ' end;',
  18434. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18435. ' procedure Fly(s: string); virtual; abstract;',
  18436. ' end;',
  18437. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18438. ' private',
  18439. ' class var FBirdIntf: IBird;',
  18440. ' class var FDoveObj: TBird;',
  18441. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  18442. ' class function GetSwallowObj: TBird; virtual; abstract;',
  18443. ' protected',
  18444. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  18445. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18446. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  18447. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18448. ' end;',
  18449. 'begin',
  18450. '']);
  18451. ConvertProgram;
  18452. CheckSource('TestClassInterface_DelegationStatic',
  18453. LinesToStr([ // statements
  18454. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18455. 'rtl.createInterface($mod, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], $mod.IUnknown);',
  18456. 'rtl.createInterface($mod, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], $mod.IBird);',
  18457. 'rtl.createInterface($mod, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], $mod.IBird);',
  18458. 'rtl.createInterface($mod, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], $mod.IBird);',
  18459. 'rtl.createClass($mod, "TObject", null, function () {',
  18460. ' this.$init = function () {',
  18461. ' };',
  18462. ' this.$final = function () {',
  18463. ' };',
  18464. '});',
  18465. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18466. ' rtl.addIntf(this, $mod.IBird);',
  18467. ' rtl.addIntf(this, $mod.IEagle);',
  18468. ' rtl.addIntf(this, $mod.IDove);',
  18469. ' rtl.addIntf(this, $mod.ISwallow);',
  18470. '});',
  18471. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  18472. ' this.FBirdIntf = null;',
  18473. ' this.FDoveObj = null;',
  18474. ' this.$intfmaps = {',
  18475. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18476. ' return this.FBirdIntf;',
  18477. ' },',
  18478. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18479. ' return this.GetEagleIntf();',
  18480. ' },',
  18481. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18482. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18483. ' },',
  18484. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18485. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18486. ' }',
  18487. ' };',
  18488. '});',
  18489. '']),
  18490. LinesToStr([ // $mod.$main
  18491. '']));
  18492. end;
  18493. procedure TTestModule.TestClassInterface_Corba_Operators;
  18494. begin
  18495. StartProgram(false);
  18496. Add([
  18497. '{$interfaces corba}',
  18498. 'type',
  18499. ' IUnknown = interface',
  18500. ' end;',
  18501. ' IBird = interface(IUnknown)',
  18502. ' function GetItems(Index: longint): longint;',
  18503. ' procedure SetItems(Index: longint; Value: longint);',
  18504. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  18505. ' end;',
  18506. ' TObject = class',
  18507. ' end;',
  18508. ' TBird = class(TObject,IBird)',
  18509. ' function GetItems(Index: longint): longint; virtual; abstract;',
  18510. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  18511. ' end;',
  18512. 'var',
  18513. ' IntfVar: IBird = nil;',
  18514. ' IntfVar2: IBird;',
  18515. ' ObjVar: TBird;',
  18516. ' v: JSValue;',
  18517. 'begin',
  18518. ' IntfVar:=nil;',
  18519. ' IntfVar[3]:=IntfVar[4];',
  18520. ' if Assigned(IntfVar) then ;',
  18521. ' IntfVar:=IntfVar2;',
  18522. ' IntfVar:=ObjVar;',
  18523. ' if IntfVar=IntfVar2 then ;',
  18524. ' if IntfVar<>IntfVar2 then ;',
  18525. ' if IntfVar is IBird then ;',
  18526. ' if IntfVar is TBird then ;',
  18527. ' if ObjVar is IBird then ;',
  18528. ' IntfVar:=IntfVar2 as IBird;',
  18529. ' ObjVar:=IntfVar2 as TBird;',
  18530. ' IntfVar:=ObjVar as IBird;',
  18531. ' IntfVar:=IBird(IntfVar2);',
  18532. ' ObjVar:=TBird(IntfVar);',
  18533. ' IntfVar:=IBird(ObjVar);',
  18534. ' v:=IntfVar;',
  18535. ' IntfVar:=IBird(v);',
  18536. ' if v is IBird then ;',
  18537. ' v:=JSValue(IntfVar);',
  18538. ' v:=IBird;',
  18539. '']);
  18540. ConvertProgram;
  18541. CheckSource('TestClassInterface_Corba_Operators',
  18542. LinesToStr([ // statements
  18543. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18544. 'rtl.createInterface($mod, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], $mod.IUnknown);',
  18545. 'rtl.createClass($mod, "TObject", null, function () {',
  18546. ' this.$init = function () {',
  18547. ' };',
  18548. ' this.$final = function () {',
  18549. ' };',
  18550. '});',
  18551. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18552. ' rtl.addIntf(this, $mod.IBird);',
  18553. '});',
  18554. 'this.IntfVar = null;',
  18555. 'this.IntfVar2 = null;',
  18556. 'this.ObjVar = null;',
  18557. 'this.v = undefined;',
  18558. '']),
  18559. LinesToStr([ // $mod.$main
  18560. '$mod.IntfVar = null;',
  18561. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  18562. 'if ($mod.IntfVar != null) ;',
  18563. '$mod.IntfVar = $mod.IntfVar2;',
  18564. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  18565. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  18566. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  18567. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  18568. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  18569. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  18570. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  18571. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  18572. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18573. '$mod.IntfVar = $mod.IntfVar2;',
  18574. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  18575. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18576. '$mod.v = $mod.IntfVar;',
  18577. '$mod.IntfVar = rtl.getObject($mod.v);',
  18578. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  18579. '$mod.v = $mod.IntfVar;',
  18580. '$mod.v = $mod.IBird;',
  18581. '']));
  18582. end;
  18583. procedure TTestModule.TestClassInterface_Corba_Args;
  18584. begin
  18585. StartProgram(false);
  18586. Add([
  18587. '{$interfaces corba}',
  18588. 'type',
  18589. ' IUnknown = interface',
  18590. ' end;',
  18591. ' IBird = interface(IUnknown)',
  18592. ' end;',
  18593. ' TObject = class',
  18594. ' end;',
  18595. ' TBird = class(TObject,IBird)',
  18596. ' end;',
  18597. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  18598. 'begin',
  18599. ' DoIt(i,i,i);',
  18600. 'end;',
  18601. 'procedure Change(var i: IBird; out j: IBird);',
  18602. 'begin',
  18603. ' DoIt(i,i,i);',
  18604. ' Change(i,i);',
  18605. 'end;',
  18606. 'var',
  18607. ' i: IBird;',
  18608. ' o: TBird;',
  18609. 'begin',
  18610. ' DoIt(i,i,i);',
  18611. ' Change(i,i);',
  18612. ' DoIt(o,o,o);',
  18613. '']);
  18614. ConvertProgram;
  18615. CheckSource('TestClassInterface_Corba_Args',
  18616. LinesToStr([ // statements
  18617. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18618. 'rtl.createInterface($mod, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], $mod.IUnknown);',
  18619. 'rtl.createClass($mod, "TObject", null, function () {',
  18620. ' this.$init = function () {',
  18621. ' };',
  18622. ' this.$final = function () {',
  18623. ' };',
  18624. '});',
  18625. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  18626. ' rtl.addIntf(this, $mod.IBird);',
  18627. '});',
  18628. 'this.DoIt = function (u, i, j) {',
  18629. ' $mod.DoIt({',
  18630. ' get: function () {',
  18631. ' return i;',
  18632. ' },',
  18633. ' set: function (v) {',
  18634. ' i = v;',
  18635. ' }',
  18636. ' }, i, i);',
  18637. '};',
  18638. 'this.Change = function (i, j) {',
  18639. ' $mod.DoIt(i, i.get(), i.get());',
  18640. ' $mod.Change(i, i);',
  18641. '};',
  18642. 'this.i = null;',
  18643. 'this.o = null;',
  18644. '']),
  18645. LinesToStr([ // $mod.$main
  18646. '$mod.DoIt({',
  18647. ' p: $mod,',
  18648. ' get: function () {',
  18649. ' return this.p.i;',
  18650. ' },',
  18651. ' set: function (v) {',
  18652. ' this.p.i = v;',
  18653. ' }',
  18654. '}, $mod.i, $mod.i);',
  18655. '$mod.Change({',
  18656. ' p: $mod,',
  18657. ' get: function () {',
  18658. ' return this.p.i;',
  18659. ' },',
  18660. ' set: function (v) {',
  18661. ' this.p.i = v;',
  18662. ' }',
  18663. '}, {',
  18664. ' p: $mod,',
  18665. ' get: function () {',
  18666. ' return this.p.i;',
  18667. ' },',
  18668. ' set: function (v) {',
  18669. ' this.p.i = v;',
  18670. ' }',
  18671. '});',
  18672. '$mod.DoIt({',
  18673. ' p: $mod,',
  18674. ' get: function () {',
  18675. ' return this.p.o;',
  18676. ' },',
  18677. ' set: function (v) {',
  18678. ' this.p.o = v;',
  18679. ' }',
  18680. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  18681. '']));
  18682. end;
  18683. procedure TTestModule.TestClassInterface_Corba_ForIn;
  18684. begin
  18685. StartProgram(false);
  18686. Add([
  18687. '{$interfaces corba}',
  18688. 'type',
  18689. ' IUnknown = interface end;',
  18690. ' TObject = class',
  18691. ' Id: longint;',
  18692. ' end;',
  18693. ' IEnumerator = interface(IUnknown)',
  18694. ' function GetCurrent: TObject;',
  18695. ' function MoveNext: Boolean;',
  18696. ' property Current: TObject read GetCurrent;',
  18697. ' end;',
  18698. ' IEnumerable = interface(IUnknown)',
  18699. ' function GetEnumerator: IEnumerator;',
  18700. ' end;',
  18701. 'var',
  18702. ' o: TObject;',
  18703. ' i: IEnumerable;',
  18704. 'begin',
  18705. ' for o in i do o.Id:=3;',
  18706. '']);
  18707. ConvertProgram;
  18708. CheckSource('TestClassInterface_Corba_ForIn',
  18709. LinesToStr([ // statements
  18710. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18711. 'rtl.createClass($mod, "TObject", null, function () {',
  18712. ' this.$init = function () {',
  18713. ' this.Id = 0;',
  18714. ' };',
  18715. ' this.$final = function () {',
  18716. ' };',
  18717. '});',
  18718. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  18719. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  18720. 'this.o = null;',
  18721. 'this.i = null;',
  18722. '']),
  18723. LinesToStr([ // $mod.$main
  18724. 'var $in = $mod.i.GetEnumerator();',
  18725. 'while ($in.MoveNext()) {',
  18726. ' $mod.o = $in.GetCurrent();',
  18727. ' $mod.o.Id = 3;',
  18728. '};',
  18729. '']));
  18730. end;
  18731. procedure TTestModule.TestClassInterface_COM_AssignVar;
  18732. begin
  18733. StartProgram(false);
  18734. Add([
  18735. '{$interfaces com}',
  18736. 'type',
  18737. ' IUnknown = interface',
  18738. ' function _AddRef: longint;',
  18739. ' function _Release: longint;',
  18740. ' end;',
  18741. ' TObject = class(IUnknown)',
  18742. ' function _AddRef: longint; virtual; abstract;',
  18743. ' function _Release: longint; virtual; abstract;',
  18744. ' end;',
  18745. 'var',
  18746. ' i: IUnknown;',
  18747. 'procedure DoGlobal(o: TObject);',
  18748. 'begin',
  18749. ' i:=nil;',
  18750. ' i:=o;',
  18751. ' i:=i;',
  18752. 'end;',
  18753. 'procedure DoLocal(o: TObject);',
  18754. 'const k: IUnknown = nil;',
  18755. 'var j: IUnknown;',
  18756. 'begin',
  18757. ' k:=o;',
  18758. ' k:=i;',
  18759. ' j:=o;',
  18760. ' j:=i;',
  18761. 'end;',
  18762. 'var o: TObject;',
  18763. 'begin',
  18764. ' i:=nil;',
  18765. ' i:=o;',
  18766. '']);
  18767. ConvertProgram;
  18768. CheckSource('TestClassInterface_COM_AssignVar',
  18769. LinesToStr([ // statements
  18770. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18771. 'rtl.createClass($mod, "TObject", null, function () {',
  18772. ' this.$init = function () {',
  18773. ' };',
  18774. ' this.$final = function () {',
  18775. ' };',
  18776. ' rtl.addIntf(this, $mod.IUnknown);',
  18777. '});',
  18778. 'this.i = null;',
  18779. 'this.DoGlobal = function (o) {',
  18780. ' rtl.setIntfP($mod, "i", null);',
  18781. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  18782. ' rtl.setIntfP($mod, "i", $mod.i);',
  18783. '};',
  18784. 'var k = null;',
  18785. 'this.DoLocal = function (o) {',
  18786. ' var j = null;',
  18787. ' try{',
  18788. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18789. ' k = rtl.setIntfL(k, $mod.i);',
  18790. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  18791. ' j = rtl.setIntfL(j, $mod.i);',
  18792. ' }finally{',
  18793. ' rtl._Release(j);',
  18794. ' };',
  18795. '};',
  18796. 'this.o = null;',
  18797. '']),
  18798. LinesToStr([ // $mod.$main
  18799. 'rtl.setIntfP($mod, "i", null);',
  18800. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  18801. '']));
  18802. end;
  18803. procedure TTestModule.TestClassInterface_COM_AssignArg;
  18804. begin
  18805. StartProgram(false);
  18806. Add([
  18807. '{$interfaces com}',
  18808. 'type',
  18809. ' IUnknown = interface',
  18810. ' function _AddRef: longint;',
  18811. ' function _Release: longint;',
  18812. ' end;',
  18813. ' TObject = class(IUnknown)',
  18814. ' function _AddRef: longint; virtual; abstract;',
  18815. ' function _Release: longint; virtual; abstract;',
  18816. ' end;',
  18817. 'procedure DoDefault(i, j: IUnknown);',
  18818. 'begin',
  18819. ' i:=nil;',
  18820. ' i:=j;',
  18821. 'end;',
  18822. 'begin',
  18823. '']);
  18824. ConvertProgram;
  18825. CheckSource('TestClassInterface_COM_AssignArg',
  18826. LinesToStr([ // statements
  18827. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18828. 'rtl.createClass($mod, "TObject", null, function () {',
  18829. ' this.$init = function () {',
  18830. ' };',
  18831. ' this.$final = function () {',
  18832. ' };',
  18833. ' rtl.addIntf(this, $mod.IUnknown);',
  18834. '});',
  18835. 'this.DoDefault = function (i, j) {',
  18836. ' rtl._AddRef(i);',
  18837. ' try {',
  18838. ' i = rtl.setIntfL(i, null);',
  18839. ' i = rtl.setIntfL(i, j);',
  18840. ' } finally {',
  18841. ' rtl._Release(i);',
  18842. ' };',
  18843. '};',
  18844. '']),
  18845. LinesToStr([ // $mod.$main
  18846. '']));
  18847. end;
  18848. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  18849. begin
  18850. StartProgram(false);
  18851. Add([
  18852. '{$interfaces com}',
  18853. 'type',
  18854. ' IUnknown = interface',
  18855. ' function _AddRef: longint;',
  18856. ' function _Release: longint;',
  18857. ' end;',
  18858. ' TObject = class(IUnknown)',
  18859. ' function _AddRef: longint; virtual; abstract;',
  18860. ' function _Release: longint; virtual; abstract;',
  18861. ' end;',
  18862. 'function DoDefault(i: IUnknown): IUnknown;',
  18863. 'begin',
  18864. ' Result:=i;',
  18865. ' if Result<>nil then exit;',
  18866. 'end;',
  18867. 'begin',
  18868. '']);
  18869. ConvertProgram;
  18870. CheckSource('TestClassInterface_COM_FunctionResult',
  18871. LinesToStr([ // statements
  18872. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18873. 'rtl.createClass($mod, "TObject", null, function () {',
  18874. ' this.$init = function () {',
  18875. ' };',
  18876. ' this.$final = function () {',
  18877. ' };',
  18878. ' rtl.addIntf(this, $mod.IUnknown);',
  18879. '});',
  18880. 'this.DoDefault = function (i) {',
  18881. ' var Result = null;',
  18882. ' var $ok = false;',
  18883. ' try {',
  18884. ' Result = rtl.setIntfL(Result, i);',
  18885. ' if(Result !== null){',
  18886. ' $ok = true;',
  18887. ' return Result;',
  18888. ' };',
  18889. ' $ok = true;',
  18890. ' } finally {',
  18891. ' if(!$ok) rtl._Release(Result);',
  18892. ' };',
  18893. ' return Result;',
  18894. '};',
  18895. '']),
  18896. LinesToStr([ // $mod.$main
  18897. '']));
  18898. end;
  18899. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  18900. begin
  18901. StartProgram(false);
  18902. Add([
  18903. '{$interfaces com}',
  18904. 'type',
  18905. ' IUnknown = interface',
  18906. ' function _AddRef: longint;',
  18907. ' function _Release: longint;',
  18908. ' end;',
  18909. ' TObject = class(IUnknown)',
  18910. ' function _AddRef: longint; virtual; abstract;',
  18911. ' function _Release: longint; virtual; abstract;',
  18912. ' function GetIntf: IUnknown; virtual;',
  18913. ' end;',
  18914. ' TMouse = class',
  18915. ' function GetIntf: IUnknown; override;',
  18916. ' end;',
  18917. 'function TObject.GetIntf: IUnknown; begin end;',
  18918. 'function TMouse.GetIntf: IUnknown;',
  18919. 'var i: IUnknown;',
  18920. 'begin',
  18921. ' inherited;',
  18922. ' inherited GetIntf;',
  18923. ' inherited GetIntf();',
  18924. ' Result:=inherited GetIntf;',
  18925. ' Result:=inherited GetIntf();',
  18926. ' i:=inherited GetIntf;',
  18927. ' i:=inherited GetIntf();',
  18928. 'end;',
  18929. 'begin',
  18930. '']);
  18931. ConvertProgram;
  18932. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  18933. LinesToStr([ // statements
  18934. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  18935. 'rtl.createClass($mod, "TObject", null, function () {',
  18936. ' this.$init = function () {',
  18937. ' };',
  18938. ' this.$final = function () {',
  18939. ' };',
  18940. ' this.GetIntf = function () {',
  18941. ' var Result = null;',
  18942. ' return Result;',
  18943. ' };',
  18944. ' rtl.addIntf(this, $mod.IUnknown);',
  18945. '});',
  18946. 'rtl.createClass($mod, "TMouse", $mod.TObject, function () {',
  18947. ' this.GetIntf = function () {',
  18948. ' var Result = null;',
  18949. ' var i = null;',
  18950. ' var $ir = rtl.createIntfRefs();',
  18951. ' var $ok = false;',
  18952. ' try {',
  18953. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  18954. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  18955. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  18956. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18957. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  18958. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18959. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  18960. ' $ok = true;',
  18961. ' } finally {',
  18962. ' $ir.free();',
  18963. ' rtl._Release(i);',
  18964. ' if (!$ok) rtl._Release(Result);',
  18965. ' };',
  18966. ' return Result;',
  18967. ' };',
  18968. ' rtl.addIntf(this, $mod.IUnknown);',
  18969. '});',
  18970. '']),
  18971. LinesToStr([ // $mod.$main
  18972. '']));
  18973. end;
  18974. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  18975. begin
  18976. StartProgram(false);
  18977. Add([
  18978. '{$interfaces com}',
  18979. 'type',
  18980. ' IUnknown = interface',
  18981. ' function _AddRef: longint;',
  18982. ' function _Release: longint;',
  18983. ' end;',
  18984. ' TObject = class(IUnknown)',
  18985. ' function _AddRef: longint; virtual; abstract;',
  18986. ' function _Release: longint; virtual; abstract;',
  18987. ' end;',
  18988. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  18989. 'begin',
  18990. ' if i is IUnknown then ;',
  18991. ' if o is IUnknown then ;',
  18992. ' if i is TObject then ;',
  18993. ' i:=j as IUnknown;',
  18994. ' i:=o as IUnknown;',
  18995. ' o:=j as TObject;',
  18996. ' i:=IUnknown(j);',
  18997. ' i:=IUnknown(o);',
  18998. ' o:=TObject(i);',
  18999. 'end;',
  19000. 'begin',
  19001. '']);
  19002. ConvertProgram;
  19003. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19004. LinesToStr([ // statements
  19005. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19006. 'rtl.createClass($mod, "TObject", null, function () {',
  19007. ' this.$init = function () {',
  19008. ' };',
  19009. ' this.$final = function () {',
  19010. ' };',
  19011. ' rtl.addIntf(this, $mod.IUnknown);',
  19012. '});',
  19013. 'this.DoDefault = function (i, j, o) {',
  19014. ' rtl._AddRef(i);',
  19015. ' try {',
  19016. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19017. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19018. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19019. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19020. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19021. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19022. ' i = rtl.setIntfL(i, j);',
  19023. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19024. ' o = rtl.intfToClass(i, $mod.TObject);',
  19025. ' } finally {',
  19026. ' rtl._Release(i);',
  19027. ' };',
  19028. '};',
  19029. '']),
  19030. LinesToStr([ // $mod.$main
  19031. '']));
  19032. end;
  19033. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19034. begin
  19035. StartProgram(false);
  19036. Add([
  19037. '{$interfaces com}',
  19038. 'type',
  19039. ' IUnknown = interface',
  19040. ' function _AddRef: longint;',
  19041. ' function _Release: longint;',
  19042. ' end;',
  19043. ' TObject = class(IUnknown)',
  19044. ' function _AddRef: longint; virtual; abstract;',
  19045. ' function _Release: longint; virtual; abstract;',
  19046. ' end;',
  19047. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19048. 'var o: TObject;',
  19049. 'begin',
  19050. ' DoIt(v,v,v,v);',
  19051. ' DoIt(o,o,k,k);',
  19052. 'end;',
  19053. 'procedure DoSome;',
  19054. 'var v: IUnknown;',
  19055. 'begin',
  19056. ' DoIt(v,v,v,v);',
  19057. 'end;',
  19058. 'var i: IUnknown;',
  19059. 'begin',
  19060. ' DoIt(i,i,i,i);',
  19061. '']);
  19062. ConvertProgram;
  19063. CheckSource('TestClassInterface_COM_PassAsArg',
  19064. LinesToStr([ // statements
  19065. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19066. 'rtl.createClass($mod, "TObject", null, function () {',
  19067. ' this.$init = function () {',
  19068. ' };',
  19069. ' this.$final = function () {',
  19070. ' };',
  19071. ' rtl.addIntf(this, $mod.IUnknown);',
  19072. '});',
  19073. 'this.DoIt = function (v, j, k, l) {',
  19074. ' var o = null;',
  19075. ' var $ir = rtl.createIntfRefs();',
  19076. ' rtl._AddRef(v);',
  19077. ' try {',
  19078. ' $mod.DoIt(v, v, {',
  19079. ' get: function () {',
  19080. ' return v;',
  19081. ' },',
  19082. ' set: function (w) {',
  19083. ' v = rtl.setIntfL(v, w);',
  19084. ' }',
  19085. ' }, {',
  19086. ' get: function () {',
  19087. ' return v;',
  19088. ' },',
  19089. ' set: function (w) {',
  19090. ' v = rtl.setIntfL(v, w);',
  19091. ' }',
  19092. ' });',
  19093. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19094. ' } finally {',
  19095. ' $ir.free();',
  19096. ' rtl._Release(v);',
  19097. ' };',
  19098. '};',
  19099. 'this.DoSome = function () {',
  19100. ' var v = null;',
  19101. ' try {',
  19102. ' $mod.DoIt(v, v, {',
  19103. ' get: function () {',
  19104. ' return v;',
  19105. ' },',
  19106. ' set: function (w) {',
  19107. ' v = rtl.setIntfL(v, w);',
  19108. ' }',
  19109. ' }, {',
  19110. ' get: function () {',
  19111. ' return v;',
  19112. ' },',
  19113. ' set: function (w) {',
  19114. ' v = rtl.setIntfL(v, w);',
  19115. ' }',
  19116. ' });',
  19117. ' } finally {',
  19118. ' rtl._Release(v);',
  19119. ' };',
  19120. '};',
  19121. 'this.i = null;',
  19122. '']),
  19123. LinesToStr([ // $mod.$main
  19124. '$mod.DoIt($mod.i, $mod.i, {',
  19125. ' p: $mod,',
  19126. ' get: function () {',
  19127. ' return this.p.i;',
  19128. ' },',
  19129. ' set: function (v) {',
  19130. ' rtl.setIntfP(this.p, "i", v);',
  19131. ' }',
  19132. '}, {',
  19133. ' p: $mod,',
  19134. ' get: function () {',
  19135. ' return this.p.i;',
  19136. ' },',
  19137. ' set: function (v) {',
  19138. ' rtl.setIntfP(this.p, "i", v);',
  19139. ' }',
  19140. '});',
  19141. '']));
  19142. end;
  19143. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19144. begin
  19145. StartProgram(false);
  19146. Add([
  19147. '{$interfaces com}',
  19148. 'type',
  19149. ' IUnknown = interface',
  19150. ' function _AddRef: longint;',
  19151. ' function _Release: longint;',
  19152. ' end;',
  19153. ' TObject = class(IUnknown)',
  19154. ' function _AddRef: longint; virtual; abstract;',
  19155. ' function _Release: longint; virtual; abstract;',
  19156. ' end;',
  19157. 'procedure DoIt(out i);',
  19158. 'begin end;',
  19159. 'procedure DoSome;',
  19160. 'var v: IUnknown;',
  19161. 'begin',
  19162. ' DoIt(v);',
  19163. 'end;',
  19164. 'function GetIt: IUnknown;',
  19165. 'begin',
  19166. ' DoIt(Result);',
  19167. 'end;',
  19168. 'var i: IUnknown;',
  19169. 'begin',
  19170. ' DoIt(i);',
  19171. '']);
  19172. ConvertProgram;
  19173. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19174. LinesToStr([ // statements
  19175. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19176. 'rtl.createClass($mod, "TObject", null, function () {',
  19177. ' this.$init = function () {',
  19178. ' };',
  19179. ' this.$final = function () {',
  19180. ' };',
  19181. ' rtl.addIntf(this, $mod.IUnknown);',
  19182. '});',
  19183. 'this.DoIt = function (i) {',
  19184. '};',
  19185. 'this.DoSome = function () {',
  19186. ' var v = null;',
  19187. ' try {',
  19188. ' $mod.DoIt({',
  19189. ' get: function () {',
  19190. ' return v;',
  19191. ' },',
  19192. ' set: function (w) {',
  19193. ' v = w;',
  19194. ' }',
  19195. ' });',
  19196. ' } finally {',
  19197. ' rtl._Release(v);',
  19198. ' };',
  19199. '};',
  19200. 'this.GetIt = function () {',
  19201. ' var Result = null;',
  19202. ' var $ok = false;',
  19203. ' try {',
  19204. ' $mod.DoIt({',
  19205. ' get: function () {',
  19206. ' return Result;',
  19207. ' },',
  19208. ' set: function (v) {',
  19209. ' Result = v;',
  19210. ' }',
  19211. ' });',
  19212. ' $ok = true;',
  19213. ' } finally {',
  19214. ' if (!$ok) rtl._Release(Result);',
  19215. ' };',
  19216. ' return Result;',
  19217. '};',
  19218. 'this.i = null;',
  19219. '']),
  19220. LinesToStr([ // $mod.$main
  19221. 'try {',
  19222. ' $mod.DoIt({',
  19223. ' p: $mod,',
  19224. ' get: function () {',
  19225. ' return this.p.i;',
  19226. ' },',
  19227. ' set: function (v) {',
  19228. ' this.p.i = v;',
  19229. ' }',
  19230. ' });',
  19231. '} finally {',
  19232. ' rtl._Release($mod.i);',
  19233. '};',
  19234. '']));
  19235. end;
  19236. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19237. begin
  19238. StartProgram(false);
  19239. Add([
  19240. '{$interfaces com}',
  19241. 'type',
  19242. ' IUnknown = interface',
  19243. ' function _AddRef: longint;',
  19244. ' function _Release: longint;',
  19245. ' end;',
  19246. ' TObject = class(IUnknown)',
  19247. ' function _AddRef: longint; virtual; abstract;',
  19248. ' function _Release: longint; virtual; abstract;',
  19249. ' end;',
  19250. 'function GetIt: IUnknown;',
  19251. 'begin',
  19252. 'end;',
  19253. 'procedure DoSome;',
  19254. 'var v: IUnknown;',
  19255. ' i: longint;',
  19256. 'begin',
  19257. ' v:=GetIt;',
  19258. ' v:=GetIt();',
  19259. ' GetIt()._AddRef;',
  19260. ' i:=GetIt()._AddRef;',
  19261. 'end;',
  19262. 'var v: IUnknown;',
  19263. ' i: longint;',
  19264. 'begin',
  19265. ' v:=GetIt;',
  19266. ' v:=GetIt();',
  19267. ' GetIt()._AddRef;',
  19268. ' i:=GetIt()._AddRef;',
  19269. '']);
  19270. ConvertProgram;
  19271. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19272. LinesToStr([ // statements
  19273. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19274. 'rtl.createClass($mod, "TObject", null, function () {',
  19275. ' this.$init = function () {',
  19276. ' };',
  19277. ' this.$final = function () {',
  19278. ' };',
  19279. ' rtl.addIntf(this, $mod.IUnknown);',
  19280. '});',
  19281. 'this.GetIt = function () {',
  19282. ' var Result = null;',
  19283. ' return Result;',
  19284. '};',
  19285. 'this.DoSome = function () {',
  19286. ' var v = null;',
  19287. ' var i = 0;',
  19288. ' var $ir = rtl.createIntfRefs();',
  19289. ' try {',
  19290. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19291. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19292. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19293. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19294. ' } finally {',
  19295. ' $ir.free();',
  19296. ' rtl._Release(v);',
  19297. ' };',
  19298. '};',
  19299. 'this.v = null;',
  19300. 'this.i = 0;',
  19301. '']),
  19302. LinesToStr([ // $mod.$main
  19303. 'var $ir = rtl.createIntfRefs();',
  19304. 'try {',
  19305. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19306. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19307. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19308. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19309. '} finally {',
  19310. ' $ir.free();',
  19311. '};',
  19312. '']));
  19313. end;
  19314. procedure TTestModule.TestClassInterface_COM_Property;
  19315. begin
  19316. StartProgram(false);
  19317. Add([
  19318. '{$interfaces com}',
  19319. 'type',
  19320. ' IUnknown = interface',
  19321. ' function _AddRef: longint;',
  19322. ' function _Release: longint;',
  19323. ' end;',
  19324. ' TObject = class(IUnknown)',
  19325. ' FAnt: IUnknown;',
  19326. ' function _AddRef: longint; virtual; abstract;',
  19327. ' function _Release: longint; virtual; abstract;',
  19328. ' function GetBird: IUnknown; virtual; abstract;',
  19329. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19330. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19331. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19332. ' property Ant: IUnknown read FAnt write FAnt;',
  19333. ' property Bird: IUnknown read GetBird write SetBird;',
  19334. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19335. ' end;',
  19336. 'procedure DoIt;',
  19337. 'var',
  19338. ' o: TObject;',
  19339. ' v: IUnknown;',
  19340. 'begin',
  19341. ' v:=o.Ant;',
  19342. ' o.Ant:=v;',
  19343. ' o.Ant:=o.Ant;',
  19344. ' v:=o.Bird;',
  19345. ' o.Bird:=v;',
  19346. ' o.Bird:=o.Bird;',
  19347. ' v:=o.Items[1];',
  19348. ' o.Items[2]:=v;',
  19349. ' o.Items[3]:=o.Items[4];',
  19350. ' v:=o[5];',
  19351. ' o[6]:=v;',
  19352. ' o[7]:=o[8];',
  19353. 'end;',
  19354. 'begin',
  19355. '']);
  19356. ConvertProgram;
  19357. CheckSource('TestClassInterface_COM_Property',
  19358. LinesToStr([ // statements
  19359. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19360. 'rtl.createClass($mod, "TObject", null, function () {',
  19361. ' this.$init = function () {',
  19362. ' this.FAnt = null;',
  19363. ' };',
  19364. ' this.$final = function () {',
  19365. ' this.FAnt = undefined;',
  19366. ' };',
  19367. ' rtl.addIntf(this, $mod.IUnknown);',
  19368. '});',
  19369. 'this.DoIt = function () {',
  19370. ' var o = null;',
  19371. ' var v = null;',
  19372. ' var $ir = rtl.createIntfRefs();',
  19373. ' try {',
  19374. ' v = rtl.setIntfL(v, o.FAnt);',
  19375. ' rtl.setIntfP(o, "FAnt", v);',
  19376. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19377. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19378. ' o.SetBird(v);',
  19379. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19380. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19381. ' o.SetItems(2, v);',
  19382. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19383. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19384. ' o.SetItems(6, v);',
  19385. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19386. ' } finally {',
  19387. ' $ir.free();',
  19388. ' rtl._Release(v);',
  19389. ' };',
  19390. '};',
  19391. '']),
  19392. LinesToStr([ // $mod.$main
  19393. '']));
  19394. end;
  19395. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  19396. begin
  19397. StartProgram(false);
  19398. Add([
  19399. '{$interfaces com}',
  19400. 'type',
  19401. ' IUnknown = interface',
  19402. ' function _AddRef: longint;',
  19403. ' function _Release: longint;',
  19404. ' function GetBird: IUnknown;',
  19405. ' procedure SetBird(Value: IUnknown);',
  19406. ' function GetItems(Index: longint): IUnknown;',
  19407. ' procedure SetItems(Index: longint; Value: IUnknown);',
  19408. ' property Bird: IUnknown read GetBird write SetBird;',
  19409. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19410. ' end;',
  19411. ' TObject = class(IUnknown)',
  19412. ' function _AddRef: longint; virtual; abstract;',
  19413. ' function _Release: longint; virtual; abstract;',
  19414. ' function GetBird: IUnknown; virtual; abstract;',
  19415. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19416. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19417. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19418. ' end;',
  19419. 'procedure DoIt;',
  19420. 'var',
  19421. ' o: TObject;',
  19422. ' v: IUnknown;',
  19423. 'begin',
  19424. ' v:=v.Items[1];',
  19425. ' v.Items[2]:=v;',
  19426. ' v.Items[3]:=v.Items[4];',
  19427. ' v:=v[5];',
  19428. ' v[6]:=v;',
  19429. ' v[7]:=v[8];',
  19430. ' v[9].Bird.Bird:=v;',
  19431. ' v:=v.Bird[10].Bird',
  19432. 'end;',
  19433. 'begin',
  19434. '']);
  19435. ConvertProgram;
  19436. CheckSource('TestClassInterface_COM_IntfProperty',
  19437. LinesToStr([ // statements
  19438. 'rtl.createInterface($mod, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  19439. ' "_AddRef",',
  19440. ' "_Release",',
  19441. ' "GetBird",',
  19442. ' "SetBird",',
  19443. ' "GetItems",',
  19444. ' "SetItems"',
  19445. '], null);',
  19446. 'rtl.createClass($mod, "TObject", null, function () {',
  19447. ' this.$init = function () {',
  19448. ' };',
  19449. ' this.$final = function () {',
  19450. ' };',
  19451. ' rtl.addIntf(this, $mod.IUnknown);',
  19452. '});',
  19453. 'this.DoIt = function () {',
  19454. ' var o = null;',
  19455. ' var v = null;',
  19456. ' var $ir = rtl.createIntfRefs();',
  19457. ' try {',
  19458. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  19459. ' v.SetItems(2, v);',
  19460. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  19461. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  19462. ' v.SetItems(6, v);',
  19463. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  19464. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  19465. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  19466. ' } finally {',
  19467. ' $ir.free();',
  19468. ' rtl._Release(v);',
  19469. ' };',
  19470. '};',
  19471. '']),
  19472. LinesToStr([ // $mod.$main
  19473. '']));
  19474. end;
  19475. procedure TTestModule.TestClassInterface_COM_Delegation;
  19476. begin
  19477. StartProgram(false);
  19478. Add([
  19479. '{$interfaces com}',
  19480. 'type',
  19481. ' IUnknown = interface',
  19482. ' function _AddRef: longint;',
  19483. ' function _Release: longint;',
  19484. ' end;',
  19485. ' IBird = interface(IUnknown)',
  19486. ' procedure Fly(s: string);',
  19487. ' end;',
  19488. ' IEagle = interface(IBird) end;',
  19489. ' IDove = interface(IBird) end;',
  19490. ' ISwallow = interface(IBird) end;',
  19491. ' TObject = class',
  19492. ' end;',
  19493. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19494. ' function _AddRef: longint; virtual; abstract;',
  19495. ' function _Release: longint; virtual; abstract;',
  19496. ' procedure Fly(s: string); virtual; abstract;',
  19497. ' end;',
  19498. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19499. ' function _AddRef: longint; virtual; abstract;',
  19500. ' function _Release: longint; virtual; abstract;',
  19501. ' FBirdIntf: IBird;',
  19502. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19503. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19504. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19505. ' FDoveObj: TBird;',
  19506. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19507. ' function GetSwallowObj: TBird; virtual; abstract;',
  19508. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19509. ' end;',
  19510. 'begin',
  19511. '']);
  19512. ConvertProgram;
  19513. CheckSource('TestClassInterface_COM_Delegation',
  19514. LinesToStr([ // statements
  19515. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19516. 'rtl.createInterface($mod, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], $mod.IUnknown);',
  19517. 'rtl.createInterface($mod, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], $mod.IBird);',
  19518. 'rtl.createInterface($mod, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], $mod.IBird);',
  19519. 'rtl.createInterface($mod, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], $mod.IBird);',
  19520. 'rtl.createClass($mod, "TObject", null, function () {',
  19521. ' this.$init = function () {',
  19522. ' };',
  19523. ' this.$final = function () {',
  19524. ' };',
  19525. '});',
  19526. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  19527. ' rtl.addIntf(this, $mod.IBird);',
  19528. ' rtl.addIntf(this, $mod.IEagle);',
  19529. ' rtl.addIntf(this, $mod.IDove);',
  19530. ' rtl.addIntf(this, $mod.ISwallow);',
  19531. '});',
  19532. 'rtl.createClass($mod, "TBat", $mod.TObject, function () {',
  19533. ' this.$init = function () {',
  19534. ' $mod.TObject.$init.call(this);',
  19535. ' this.FBirdIntf = null;',
  19536. ' this.FDoveObj = null;',
  19537. ' };',
  19538. ' this.$final = function () {',
  19539. ' this.FBirdIntf = undefined;',
  19540. ' this.FDoveObj = undefined;',
  19541. ' $mod.TObject.$final.call(this);',
  19542. ' };',
  19543. ' this.$intfmaps = {',
  19544. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  19545. ' return rtl._AddRef(this.FBirdIntf);',
  19546. ' },',
  19547. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  19548. ' return this.GetEagleIntf();',
  19549. ' },',
  19550. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  19551. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  19552. ' },',
  19553. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  19554. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19555. ' }',
  19556. ' };',
  19557. '});',
  19558. '']),
  19559. LinesToStr([ // $mod.$main
  19560. '']));
  19561. end;
  19562. procedure TTestModule.TestClassInterface_COM_With;
  19563. begin
  19564. StartProgram(false);
  19565. Add([
  19566. '{$interfaces com}',
  19567. 'type',
  19568. ' IUnknown = interface',
  19569. ' function _AddRef: longint;',
  19570. ' function _Release: longint;',
  19571. ' function GetAnt: IUnknown;',
  19572. ' property Ant: IUnknown read GetAnt;',
  19573. ' end;',
  19574. ' TObject = class(IUnknown)',
  19575. ' function _AddRef: longint; virtual; abstract;',
  19576. ' function _Release: longint; virtual; abstract;',
  19577. ' function GetAnt: IUnknown; virtual; abstract;',
  19578. ' property Ant: IUnknown read GetAnt;',
  19579. ' end;',
  19580. 'procedure DoIt;',
  19581. 'var',
  19582. ' i: IUnknown;',
  19583. 'begin',
  19584. ' with i do ',
  19585. ' GetAnt;',
  19586. ' with i.Ant, Ant do ',
  19587. ' GetAnt;',
  19588. 'end;',
  19589. 'begin',
  19590. '']);
  19591. ConvertProgram;
  19592. CheckSource('TestClassInterface_COM_With',
  19593. LinesToStr([ // statements
  19594. 'rtl.createInterface($mod, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  19595. 'rtl.createClass($mod, "TObject", null, function () {',
  19596. ' this.$init = function () {',
  19597. ' };',
  19598. ' this.$final = function () {',
  19599. ' };',
  19600. ' rtl.addIntf(this, $mod.IUnknown);',
  19601. '});',
  19602. 'this.DoIt = function () {',
  19603. ' var i = null;',
  19604. ' var $ir = rtl.createIntfRefs();',
  19605. ' try {',
  19606. ' $ir.ref(1, i.GetAnt());',
  19607. ' var $with = $ir.ref(2, i.GetAnt());',
  19608. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  19609. ' $ir.ref(4, $with1.GetAnt());',
  19610. ' } finally {',
  19611. ' $ir.free();',
  19612. ' };',
  19613. '};',
  19614. '']),
  19615. LinesToStr([ // $mod.$main
  19616. '']));
  19617. end;
  19618. procedure TTestModule.TestClassInterface_COM_ForIn;
  19619. begin
  19620. StartProgram(false);
  19621. Add([
  19622. '{$interfaces com}',
  19623. 'type',
  19624. ' IUnknown = interface end;',
  19625. ' TObject = class',
  19626. ' Id: longint;',
  19627. ' end;',
  19628. ' IEnumerator = interface(IUnknown)',
  19629. ' function GetCurrent: TObject;',
  19630. ' function MoveNext: Boolean;',
  19631. ' property Current: TObject read GetCurrent;',
  19632. ' end;',
  19633. ' IEnumerable = interface(IUnknown)',
  19634. ' function GetEnumerator: IEnumerator;',
  19635. ' end;',
  19636. 'var',
  19637. ' o: TObject;',
  19638. ' i: IEnumerable;',
  19639. 'begin',
  19640. ' for o in i do o.Id:=3;',
  19641. '']);
  19642. ConvertProgram;
  19643. CheckSource('TestClassInterface_COM_ForIn',
  19644. LinesToStr([ // statements
  19645. 'rtl.createInterface($mod, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19646. 'rtl.createClass($mod, "TObject", null, function () {',
  19647. ' this.$init = function () {',
  19648. ' this.Id = 0;',
  19649. ' };',
  19650. ' this.$final = function () {',
  19651. ' };',
  19652. '});',
  19653. 'rtl.createInterface($mod, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], $mod.IUnknown);',
  19654. 'rtl.createInterface($mod, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], $mod.IUnknown);',
  19655. 'this.o = null;',
  19656. 'this.i = null;',
  19657. '']),
  19658. LinesToStr([ // $mod.$main
  19659. 'var $in = $mod.i.GetEnumerator();',
  19660. 'try {',
  19661. ' while ($in.MoveNext()) {',
  19662. ' $mod.o = $in.GetCurrent();',
  19663. ' $mod.o.Id = 3;',
  19664. ' }',
  19665. '} finally {',
  19666. ' rtl._Release($in)',
  19667. '};',
  19668. '']));
  19669. end;
  19670. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  19671. begin
  19672. StartProgram(false);
  19673. Add([
  19674. '{$interfaces com}',
  19675. 'type',
  19676. ' IUnknown = interface',
  19677. ' function _AddRef: longint;',
  19678. ' function _Release: longint;',
  19679. ' end;',
  19680. ' TObject = class',
  19681. ' end;',
  19682. ' TArrOfIntf = array of IUnknown;',
  19683. 'begin',
  19684. '']);
  19685. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  19686. ConvertProgram;
  19687. end;
  19688. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  19689. begin
  19690. StartProgram(false);
  19691. Add([
  19692. '{$interfaces com}',
  19693. 'type',
  19694. ' IUnknown = interface',
  19695. ' function _AddRef: longint;',
  19696. ' function _Release: longint;',
  19697. ' end;',
  19698. ' TRec = record',
  19699. ' i: IUnknown;',
  19700. ' end;',
  19701. 'begin',
  19702. '']);
  19703. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  19704. ConvertProgram;
  19705. end;
  19706. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  19707. begin
  19708. StartUnit(false);
  19709. Add([
  19710. '{$interfaces com}',
  19711. 'interface',
  19712. 'implementation',
  19713. 'type',
  19714. ' IUnknown = interface',
  19715. ' function _AddRef: longint;',
  19716. ' end;',
  19717. ' TObject = class(IUnknown)',
  19718. ' function _AddRef: longint;',
  19719. ' end;',
  19720. 'function TObject._AddRef: longint; begin end;',
  19721. 'var i: IUnknown;',
  19722. ' o: TObject;',
  19723. 'initialization',
  19724. ' i:=nil;',
  19725. ' i:=i;',
  19726. ' i:=o;',
  19727. ' if (o as IUnknown)=nil then ;',
  19728. '']);
  19729. ConvertUnit;
  19730. CheckSource('TestClassInterface_COM_UnitInitialization',
  19731. LinesToStr([ // statements
  19732. 'var $impl = $mod.$impl;',
  19733. '']),
  19734. LinesToStr([ // this.$init
  19735. 'var $ir = rtl.createIntfRefs();',
  19736. 'try {',
  19737. ' rtl.setIntfP($impl, "i", null);',
  19738. ' rtl.setIntfP($impl, "i", $impl.i);',
  19739. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  19740. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  19741. '} finally {',
  19742. ' $ir.free();',
  19743. '};',
  19744. '']),
  19745. LinesToStr([ // implementation
  19746. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  19747. 'rtl.createClass($impl, "TObject", null, function () {',
  19748. ' this.$init = function () {',
  19749. ' };',
  19750. ' this.$final = function () {',
  19751. ' };',
  19752. ' this._AddRef = function () {',
  19753. ' var Result = 0;',
  19754. ' return Result;',
  19755. ' };',
  19756. ' rtl.addIntf(this, $impl.IUnknown);',
  19757. '});',
  19758. '$impl.i = null;',
  19759. '$impl.o = null;',
  19760. ''])
  19761. );
  19762. end;
  19763. procedure TTestModule.TestClassInterface_GUID;
  19764. begin
  19765. StartProgram(false);
  19766. Add([
  19767. '{$interfaces corba}',
  19768. 'type',
  19769. ' IUnknown = interface',
  19770. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19771. ' end;',
  19772. ' TObject = class end;',
  19773. ' TGUID = record D1, D2, D3, D4: word; end;',
  19774. ' TAliasGUID = TGUID;',
  19775. ' TGUIDString = type string;',
  19776. ' TAliasGUIDString = TGUIDString;',
  19777. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  19778. 'begin end;',
  19779. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  19780. 'begin end;',
  19781. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  19782. 'begin end;',
  19783. 'var',
  19784. ' i: IUnknown;',
  19785. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  19786. ' s: TAliasGUIDString;',
  19787. 'begin',
  19788. ' DoConstGUIDIt(IUnknown);',
  19789. ' DoDefGUID(IUnknown);',
  19790. ' DoStr(IUnknown);',
  19791. ' DoConstGUIDIt(i);',
  19792. ' DoDefGUID(i);',
  19793. ' DoStr(i);',
  19794. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19795. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  19796. ' DoStr(g);',
  19797. ' g:=i;',
  19798. ' g:=IUnknown;',
  19799. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19800. ' s:=i;',
  19801. ' s:=IUnknown;',
  19802. ' s:=g;',
  19803. ' if g=i then ;',
  19804. ' if i=g then ;',
  19805. ' if g=IUnknown then ;',
  19806. ' if IUnknown=g then ;',
  19807. ' if s=i then ;',
  19808. ' if i=s then ;',
  19809. ' if s=IUnknown then ;',
  19810. ' if IUnknown=s then ;',
  19811. ' if s=g then ;',
  19812. ' if g=s then ;',
  19813. '']);
  19814. ConvertProgram;
  19815. CheckSource('TestClassInterface_GUID',
  19816. LinesToStr([ // statements
  19817. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19818. 'rtl.createClass($mod, "TObject", null, function () {',
  19819. ' this.$init = function () {',
  19820. ' };',
  19821. ' this.$final = function () {',
  19822. ' };',
  19823. '});',
  19824. 'rtl.recNewT($mod, "TGUID", function () {',
  19825. ' this.D1 = 0;',
  19826. ' this.D2 = 0;',
  19827. ' this.D3 = 0;',
  19828. ' this.D4 = 0;',
  19829. ' this.$eq = function (b) {',
  19830. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19831. ' };',
  19832. ' this.$assign = function (s) {',
  19833. ' this.D1 = s.D1;',
  19834. ' this.D2 = s.D2;',
  19835. ' this.D3 = s.D3;',
  19836. ' this.D4 = s.D4;',
  19837. ' return this;',
  19838. ' };',
  19839. '});',
  19840. 'this.DoConstGUIDIt = function (g) {',
  19841. '};',
  19842. 'this.DoDefGUID = function (g) {',
  19843. '};',
  19844. 'this.DoStr = function (s) {',
  19845. '};',
  19846. 'this.i = null;',
  19847. 'this.g = $mod.TGUID.$clone({',
  19848. ' D1: 0xD91C9AF4,',
  19849. ' D2: 0x3C93,',
  19850. ' D3: 0x420F,',
  19851. ' D4: [',
  19852. ' 0xA3,',
  19853. ' 0x03,',
  19854. ' 0xBF,',
  19855. ' 0x5B,',
  19856. ' 0xA8,',
  19857. ' 0x2B,',
  19858. ' 0xFD,',
  19859. ' 0x23',
  19860. ' ]',
  19861. '});',
  19862. 'this.s = "";',
  19863. '']),
  19864. LinesToStr([ // $mod.$main
  19865. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  19866. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  19867. '$mod.DoStr($mod.IUnknown.$guid);',
  19868. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  19869. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  19870. '$mod.DoStr($mod.i.$guid);',
  19871. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19872. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  19873. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  19874. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  19875. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  19876. '$mod.g.$assign({',
  19877. ' D1: 0xD91C9AF4,',
  19878. ' D2: 0x3C93,',
  19879. ' D3: 0x420F,',
  19880. ' D4: [',
  19881. ' 0xA3,',
  19882. ' 0x03,',
  19883. ' 0xBF,',
  19884. ' 0x5B,',
  19885. ' 0xA8,',
  19886. ' 0x2B,',
  19887. ' 0xFD,',
  19888. ' 0x23',
  19889. ' ]',
  19890. '});',
  19891. '$mod.s = $mod.i.$guid;',
  19892. '$mod.s = $mod.IUnknown.$guid;',
  19893. '$mod.s = rtl.guidrToStr($mod.g);',
  19894. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19895. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  19896. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19897. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  19898. 'if ($mod.s === $mod.i.$guid) ;',
  19899. 'if ($mod.i.$guid === $mod.s) ;',
  19900. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  19901. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  19902. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19903. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  19904. '']));
  19905. end;
  19906. procedure TTestModule.TestClassInterface_GUIDProperty;
  19907. begin
  19908. StartProgram(false);
  19909. Add([
  19910. '{$interfaces corba}',
  19911. 'type',
  19912. ' IUnknown = interface',
  19913. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  19914. ' end;',
  19915. ' TGUID = record D1, D2, D3, D4: word; end;',
  19916. ' TAliasGUID = TGUID;',
  19917. ' TGUIDString = type string;',
  19918. ' TAliasGUIDString = TGUIDString;',
  19919. ' TObject = class',
  19920. ' function GetG: TAliasGUID; virtual; abstract;',
  19921. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  19922. ' function GetS: TAliasGUIDString; virtual; abstract;',
  19923. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  19924. ' property g: TAliasGUID read GetG write SetG;',
  19925. ' property s: TAliasGUIDString read GetS write SetS;',
  19926. ' end;',
  19927. 'var o: TObject;',
  19928. 'begin',
  19929. ' o.g:=IUnknown;',
  19930. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  19931. ' o.s:=IUnknown;',
  19932. ' o.s:=o.g;',
  19933. '']);
  19934. ConvertProgram;
  19935. CheckSource('TestClassInterface_GUIDProperty',
  19936. LinesToStr([ // statements
  19937. 'rtl.createInterface($mod, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  19938. 'rtl.recNewT($mod, "TGUID", function () {',
  19939. ' this.D1 = 0;',
  19940. ' this.D2 = 0;',
  19941. ' this.D3 = 0;',
  19942. ' this.D4 = 0;',
  19943. ' this.$eq = function (b) {',
  19944. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  19945. ' };',
  19946. ' this.$assign = function (s) {',
  19947. ' this.D1 = s.D1;',
  19948. ' this.D2 = s.D2;',
  19949. ' this.D3 = s.D3;',
  19950. ' this.D4 = s.D4;',
  19951. ' return this;',
  19952. ' };',
  19953. '});',
  19954. 'rtl.createClass($mod, "TObject", null, function () {',
  19955. ' this.$init = function () {',
  19956. ' };',
  19957. ' this.$final = function () {',
  19958. ' };',
  19959. '});',
  19960. 'this.o = null;',
  19961. '']),
  19962. LinesToStr([ // $mod.$main
  19963. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  19964. '$mod.o.SetG({',
  19965. ' D1: 0xD91C9AF4,',
  19966. ' D2: 0x3C93,',
  19967. ' D3: 0x420F,',
  19968. ' D4: [',
  19969. ' 0xA3,',
  19970. ' 0x03,',
  19971. ' 0xBF,',
  19972. ' 0x5B,',
  19973. ' 0xA8,',
  19974. ' 0x2B,',
  19975. ' 0xFD,',
  19976. ' 0x23',
  19977. ' ]',
  19978. '});',
  19979. '$mod.o.SetS($mod.IUnknown.$guid);',
  19980. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  19981. '']));
  19982. end;
  19983. procedure TTestModule.TestClassHelper_ClassVar;
  19984. begin
  19985. StartProgram(false);
  19986. Add([
  19987. 'type',
  19988. ' TObject = class',
  19989. ' end;',
  19990. ' THelper = class helper for TObject',
  19991. ' const',
  19992. ' One = 1;',
  19993. ' Two: word = 2;',
  19994. ' class var',
  19995. ' Glob: word;',
  19996. ' function Foo(w: word): word;',
  19997. ' class function Bar(w: word): word;',
  19998. ' end;',
  19999. 'function THelper.foo(w: word): word;',
  20000. 'begin',
  20001. ' Result:=w;',
  20002. ' Two:=One+w;',
  20003. ' Glob:=Glob;',
  20004. ' Result:=Self.Glob;',
  20005. ' Self.Glob:=Self.Glob;',
  20006. ' with Self do Glob:=Glob;',
  20007. 'end;',
  20008. 'class function THelper.bar(w: word): word;',
  20009. 'begin',
  20010. ' Result:=w;',
  20011. ' Two:=One;',
  20012. ' Glob:=Glob;',
  20013. ' Self.Glob:=Self.Glob;',
  20014. ' with Self do Glob:=Glob;',
  20015. 'end;',
  20016. 'var o: TObject;',
  20017. 'begin',
  20018. ' tobject.two:=tobject.one;',
  20019. ' tobject.Glob:=tobject.Glob;',
  20020. ' with tobject do begin',
  20021. ' two:=one;',
  20022. ' Glob:=Glob;',
  20023. ' end;',
  20024. ' o.two:=o.one;',
  20025. ' o.Glob:=o.Glob;',
  20026. ' with o do begin',
  20027. ' two:=one;',
  20028. ' Glob:=Glob;',
  20029. ' end;',
  20030. '']);
  20031. ConvertProgram;
  20032. CheckSource('TestClassHelper_ClassVar',
  20033. LinesToStr([ // statements
  20034. 'rtl.createClass($mod, "TObject", null, function () {',
  20035. ' this.$init = function () {',
  20036. ' };',
  20037. ' this.$final = function () {',
  20038. ' };',
  20039. '});',
  20040. 'rtl.createHelper($mod, "THelper", null, function () {',
  20041. ' this.One = 1;',
  20042. ' this.Two = 2;',
  20043. ' this.Glob = 0;',
  20044. ' this.Foo = function (w) {',
  20045. ' var Result = 0;',
  20046. ' Result = w;',
  20047. ' $mod.THelper.Two = 1 + w;',
  20048. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20049. ' Result = $mod.THelper.Glob;',
  20050. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20051. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20052. ' return Result;',
  20053. ' };',
  20054. ' this.Bar = function (w) {',
  20055. ' var Result = 0;',
  20056. ' Result = w;',
  20057. ' $mod.THelper.Two = 1;',
  20058. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20059. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20060. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20061. ' return Result;',
  20062. ' };',
  20063. '});',
  20064. 'this.o = null;',
  20065. '']),
  20066. LinesToStr([ // $mod.$main
  20067. '$mod.THelper.Two = 1;',
  20068. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20069. 'var $with = $mod.TObject;',
  20070. '$mod.THelper.Two = 1;',
  20071. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20072. '$mod.THelper.Two = 1;',
  20073. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20074. 'var $with1 = $mod.o;',
  20075. '$mod.THelper.Two = 1;',
  20076. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20077. '']));
  20078. end;
  20079. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20080. begin
  20081. StartProgram(false);
  20082. Add([
  20083. 'type',
  20084. ' TObject = class',
  20085. ' FSize: word;',
  20086. ' property Size: word read FSize write FSize;',
  20087. ' end;',
  20088. ' THelper = class helper for TObject',
  20089. ' function Foo(w: word = 1): word;',
  20090. ' end;',
  20091. 'function THelper.foo(w: word): word;',
  20092. 'begin',
  20093. ' Result:=Size;',
  20094. ' Size:=Size+2;',
  20095. ' Self.Size:=Self.Size+3;',
  20096. ' FSize:=FSize+4;',
  20097. ' Self.FSize:=Self.FSize+5;',
  20098. ' with Self do begin',
  20099. ' Size:=Size+6;',
  20100. ' FSize:=FSize+7;',
  20101. ' FSize:=FSize+8;',
  20102. ' end;',
  20103. 'end;',
  20104. 'begin',
  20105. '']);
  20106. ConvertProgram;
  20107. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20108. LinesToStr([ // statements
  20109. 'rtl.createClass($mod, "TObject", null, function () {',
  20110. ' this.$init = function () {',
  20111. ' this.FSize = 0;',
  20112. ' };',
  20113. ' this.$final = function () {',
  20114. ' };',
  20115. '});',
  20116. 'rtl.createHelper($mod, "THelper", null, function () {',
  20117. ' this.Foo = function (w) {',
  20118. ' var Result = 0;',
  20119. ' Result = this.FSize;',
  20120. ' this.FSize = this.FSize + 2;',
  20121. ' this.FSize = this.FSize + 3;',
  20122. ' this.FSize = this.FSize + 4;',
  20123. ' this.FSize = this.FSize + 5;',
  20124. ' this.FSize = this.FSize + 6;',
  20125. ' this.FSize = this.FSize + 7;',
  20126. ' this.FSize = this.FSize + 8;',
  20127. ' return Result;',
  20128. ' };',
  20129. '});',
  20130. '']),
  20131. LinesToStr([ // $mod.$main
  20132. '']));
  20133. end;
  20134. procedure TTestModule.TestClassHelper_Method_Call;
  20135. begin
  20136. StartProgram(false);
  20137. Add([
  20138. 'type',
  20139. ' TObject = class',
  20140. ' procedure Run(w: word = 10);',
  20141. ' end;',
  20142. ' THelper = class helper for TObject',
  20143. ' function Foo(w: word = 1): word;',
  20144. ' end;',
  20145. 'procedure TObject.Run(w: word);',
  20146. 'var o: TObject;',
  20147. 'begin',
  20148. ' Foo;',
  20149. ' Foo();',
  20150. ' Foo(2);',
  20151. ' Self.Foo;',
  20152. ' Self.Foo();',
  20153. ' Self.Foo(3);',
  20154. ' with Self do begin',
  20155. ' Foo;',
  20156. ' Foo();',
  20157. ' Foo(4);',
  20158. ' end;',
  20159. ' with o do Foo(5);',
  20160. 'end;',
  20161. 'function THelper.foo(w: word): word;',
  20162. 'begin',
  20163. ' Run;',
  20164. ' Run();',
  20165. ' Run(11);',
  20166. ' Foo;',
  20167. ' Foo();',
  20168. ' Foo(12);',
  20169. ' Self.Foo;',
  20170. ' Self.Foo();',
  20171. ' Self.Foo(13);',
  20172. ' with Self do begin',
  20173. ' Foo;',
  20174. ' Foo();',
  20175. ' Foo(14);',
  20176. ' end;',
  20177. 'end;',
  20178. 'var Obj: TObject;',
  20179. 'begin',
  20180. ' obj.Foo;',
  20181. ' obj.Foo();',
  20182. ' obj.Foo(21);',
  20183. ' with obj do begin',
  20184. ' Foo;',
  20185. ' Foo();',
  20186. ' Foo(22);',
  20187. ' end;',
  20188. '']);
  20189. ConvertProgram;
  20190. CheckSource('TestClassHelper_Method_Call',
  20191. LinesToStr([ // statements
  20192. 'rtl.createClass($mod, "TObject", null, function () {',
  20193. ' this.$init = function () {',
  20194. ' };',
  20195. ' this.$final = function () {',
  20196. ' };',
  20197. ' this.Run = function (w) {',
  20198. ' var o = null;',
  20199. ' $mod.THelper.Foo.call(this, 1);',
  20200. ' $mod.THelper.Foo.call(this, 1);',
  20201. ' $mod.THelper.Foo.call(this, 2);',
  20202. ' $mod.THelper.Foo.call(this, 1);',
  20203. ' $mod.THelper.Foo.call(this, 1);',
  20204. ' $mod.THelper.Foo.call(this, 3);',
  20205. ' $mod.THelper.Foo.call(this, 1);',
  20206. ' $mod.THelper.Foo.call(this, 1);',
  20207. ' $mod.THelper.Foo.call(this, 4);',
  20208. ' $mod.THelper.Foo.call(o, 5);',
  20209. ' };',
  20210. '});',
  20211. 'rtl.createHelper($mod, "THelper", null, function () {',
  20212. ' this.Foo = function (w) {',
  20213. ' var Result = 0;',
  20214. ' this.Run(10);',
  20215. ' this.Run(10);',
  20216. ' this.Run(11);',
  20217. ' $mod.THelper.Foo.call(this, 1);',
  20218. ' $mod.THelper.Foo.call(this, 1);',
  20219. ' $mod.THelper.Foo.call(this, 12);',
  20220. ' $mod.THelper.Foo.call(this, 1);',
  20221. ' $mod.THelper.Foo.call(this, 1);',
  20222. ' $mod.THelper.Foo.call(this, 13);',
  20223. ' $mod.THelper.Foo.call(this, 1);',
  20224. ' $mod.THelper.Foo.call(this, 1);',
  20225. ' $mod.THelper.Foo.call(this, 14);',
  20226. ' return Result;',
  20227. ' };',
  20228. '});',
  20229. 'this.Obj = null;',
  20230. '']),
  20231. LinesToStr([ // $mod.$main
  20232. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20233. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20234. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20235. 'var $with = $mod.Obj;',
  20236. '$mod.THelper.Foo.call($with, 1);',
  20237. '$mod.THelper.Foo.call($with, 1);',
  20238. '$mod.THelper.Foo.call($with, 22);',
  20239. '']));
  20240. end;
  20241. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20242. begin
  20243. StartProgram(false);
  20244. Add([
  20245. 'type',
  20246. ' TObject = class',
  20247. ' procedure Run(w: word = 10);',
  20248. ' end;',
  20249. ' THelper = class helper for TObject',
  20250. ' function Foo(w: word = 1): word;',
  20251. ' end;',
  20252. 'procedure TObject.Run(w: word);',
  20253. ' procedure Sub(Self: TObject);',
  20254. ' begin',
  20255. ' Foo;',
  20256. ' Foo();',
  20257. ' Self.Foo;',
  20258. ' Self.Foo();',
  20259. ' with Self do begin',
  20260. ' Foo;',
  20261. ' Foo();',
  20262. ' end;',
  20263. ' end;',
  20264. 'begin',
  20265. 'end;',
  20266. 'function THelper.foo(w: word): word;',
  20267. ' procedure Sub(Self: TObject);',
  20268. ' begin',
  20269. ' Run;',
  20270. ' Run();',
  20271. ' Foo;',
  20272. ' Foo();',
  20273. ' Self.Foo;',
  20274. ' Self.Foo();',
  20275. ' with Self do begin',
  20276. ' Foo;',
  20277. ' Foo();',
  20278. ' end;',
  20279. ' end;',
  20280. 'begin',
  20281. 'end;',
  20282. 'begin',
  20283. '']);
  20284. ConvertProgram;
  20285. CheckSource('TestClassHelper_Method_Nested_Call',
  20286. LinesToStr([ // statements
  20287. 'rtl.createClass($mod, "TObject", null, function () {',
  20288. ' this.$init = function () {',
  20289. ' };',
  20290. ' this.$final = function () {',
  20291. ' };',
  20292. ' this.Run = function (w) {',
  20293. ' var $Self = this;',
  20294. ' function Sub(Self) {',
  20295. ' $mod.THelper.Foo.call($Self, 1);',
  20296. ' $mod.THelper.Foo.call($Self, 1);',
  20297. ' $mod.THelper.Foo.call(Self, 1);',
  20298. ' $mod.THelper.Foo.call(Self, 1);',
  20299. ' $mod.THelper.Foo.call(Self, 1);',
  20300. ' $mod.THelper.Foo.call(Self, 1);',
  20301. ' };',
  20302. ' };',
  20303. '});',
  20304. 'rtl.createHelper($mod, "THelper", null, function () {',
  20305. ' this.Foo = function (w) {',
  20306. ' var $Self = this;',
  20307. ' var Result = 0;',
  20308. ' function Sub(Self) {',
  20309. ' $Self.Run(10);',
  20310. ' $Self.Run(10);',
  20311. ' $mod.THelper.Foo.call($Self, 1);',
  20312. ' $mod.THelper.Foo.call($Self, 1);',
  20313. ' $mod.THelper.Foo.call(Self, 1);',
  20314. ' $mod.THelper.Foo.call(Self, 1);',
  20315. ' $mod.THelper.Foo.call(Self, 1);',
  20316. ' $mod.THelper.Foo.call(Self, 1);',
  20317. ' };',
  20318. ' return Result;',
  20319. ' };',
  20320. '});',
  20321. '']),
  20322. LinesToStr([ // $mod.$main
  20323. '']));
  20324. end;
  20325. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20326. begin
  20327. StartProgram(false);
  20328. Add([
  20329. 'type',
  20330. ' TObject = class',
  20331. ' class procedure Run(w: word = 10);',
  20332. ' end;',
  20333. ' THelper = class helper for TObject',
  20334. ' class function Foo(w: word = 1): word;',
  20335. ' end;',
  20336. 'class procedure TObject.Run(w: word);',
  20337. 'begin',
  20338. ' Foo;',
  20339. ' Foo();',
  20340. ' Self.Foo;',
  20341. ' Self.Foo();',
  20342. ' with Self do begin',
  20343. ' Foo;',
  20344. ' Foo();',
  20345. ' end;',
  20346. 'end;',
  20347. 'class function THelper.foo(w: word): word;',
  20348. 'begin',
  20349. ' Run;',
  20350. ' Run();',
  20351. ' Foo;',
  20352. ' Foo();',
  20353. ' Self.Foo;',
  20354. ' Self.Foo();',
  20355. ' with Self do begin',
  20356. ' Foo;',
  20357. ' Foo();',
  20358. ' end;',
  20359. 'end;',
  20360. 'var',
  20361. ' Obj: TObject;',
  20362. 'begin',
  20363. ' obj.Foo;',
  20364. ' obj.Foo();',
  20365. ' with obj do begin',
  20366. ' Foo;',
  20367. ' Foo();',
  20368. ' end;',
  20369. ' tobject.Foo;',
  20370. ' tobject.Foo();',
  20371. ' with tobject do begin',
  20372. ' Foo;',
  20373. ' Foo();',
  20374. ' end;',
  20375. '']);
  20376. ConvertProgram;
  20377. CheckSource('TestClassHelper_ClassMethod_Call',
  20378. LinesToStr([ // statements
  20379. 'rtl.createClass($mod, "TObject", null, function () {',
  20380. ' this.$init = function () {',
  20381. ' };',
  20382. ' this.$final = function () {',
  20383. ' };',
  20384. ' this.Run = function (w) {',
  20385. ' $mod.THelper.Foo.call(this, 1);',
  20386. ' $mod.THelper.Foo.call(this, 1);',
  20387. ' $mod.THelper.Foo.call(this, 1);',
  20388. ' $mod.THelper.Foo.call(this, 1);',
  20389. ' $mod.THelper.Foo.call(this, 1);',
  20390. ' $mod.THelper.Foo.call(this, 1);',
  20391. ' };',
  20392. '});',
  20393. 'rtl.createHelper($mod, "THelper", null, function () {',
  20394. ' this.Foo = function (w) {',
  20395. ' var Result = 0;',
  20396. ' this.Run(10);',
  20397. ' this.Run(10);',
  20398. ' $mod.THelper.Foo.call(this, 1);',
  20399. ' $mod.THelper.Foo.call(this, 1);',
  20400. ' $mod.THelper.Foo.call(this, 1);',
  20401. ' $mod.THelper.Foo.call(this, 1);',
  20402. ' $mod.THelper.Foo.call(this, 1);',
  20403. ' $mod.THelper.Foo.call(this, 1);',
  20404. ' return Result;',
  20405. ' };',
  20406. '});',
  20407. 'this.Obj = null;',
  20408. '']),
  20409. LinesToStr([ // $mod.$main
  20410. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20411. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20412. 'var $with = $mod.Obj;',
  20413. '$mod.THelper.Foo.call($with.$class, 1);',
  20414. '$mod.THelper.Foo.call($with.$class, 1);',
  20415. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20416. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20417. 'var $with1 = $mod.TObject;',
  20418. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20419. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20420. '']));
  20421. end;
  20422. procedure TTestModule.TestClassHelper_ClassOf;
  20423. begin
  20424. StartProgram(false);
  20425. Add([
  20426. 'type',
  20427. ' TObject = class',
  20428. ' end;',
  20429. ' TClass = class of TObject;',
  20430. ' THelper = class helper for TObject',
  20431. ' class function Foo(w: word = 1): word;',
  20432. ' end;',
  20433. 'class function THelper.foo(w: word): word;',
  20434. 'begin',
  20435. 'end;',
  20436. 'var',
  20437. ' c: TClass;',
  20438. 'begin',
  20439. ' c.Foo;',
  20440. ' c.Foo();',
  20441. ' with c do begin',
  20442. ' Foo;',
  20443. ' Foo();',
  20444. ' end;',
  20445. '']);
  20446. ConvertProgram;
  20447. CheckSource('TestClassHelper_ClassOf',
  20448. LinesToStr([ // statements
  20449. 'rtl.createClass($mod, "TObject", null, function () {',
  20450. ' this.$init = function () {',
  20451. ' };',
  20452. ' this.$final = function () {',
  20453. ' };',
  20454. '});',
  20455. 'rtl.createHelper($mod, "THelper", null, function () {',
  20456. ' this.Foo = function (w) {',
  20457. ' var Result = 0;',
  20458. ' return Result;',
  20459. ' };',
  20460. '});',
  20461. 'this.c = null;',
  20462. '']),
  20463. LinesToStr([ // $mod.$main
  20464. '$mod.THelper.Foo.call($mod.c, 1);',
  20465. '$mod.THelper.Foo.call($mod.c, 1);',
  20466. 'var $with = $mod.c;',
  20467. '$mod.THelper.Foo.call($with, 1);',
  20468. '$mod.THelper.Foo.call($with, 1);',
  20469. '']));
  20470. end;
  20471. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  20472. begin
  20473. StartProgram(false);
  20474. Add([
  20475. '{$mode objfpc}',
  20476. 'type',
  20477. ' TObject = class',
  20478. ' procedure DoIt;',
  20479. ' end;',
  20480. ' THelper = class helper for TObject',
  20481. ' procedure Fly(w: word = 1);',
  20482. ' class procedure Glide(w: word = 1);',
  20483. ' class procedure Run(w: word = 1); static;',
  20484. ' end;',
  20485. ' TFly = procedure(w: word) of object;',
  20486. ' TGlide = TFly;',
  20487. ' TRun = procedure(w: word);',
  20488. 'var',
  20489. ' f: TFly;',
  20490. ' g: TGlide;',
  20491. ' r: TRun;',
  20492. 'procedure TObject.DoIt;',
  20493. 'begin',
  20494. ' f:=@fly;',
  20495. ' g:=@glide;',
  20496. ' r:=@run;',
  20497. ' f:[email protected];',
  20498. ' g:[email protected];',
  20499. ' r:[email protected];',
  20500. ' with self do begin',
  20501. ' f:=@fly;',
  20502. ' g:=@glide;',
  20503. ' r:=@run;',
  20504. ' end;',
  20505. 'end;',
  20506. 'procedure THelper.fly(w: word);',
  20507. 'begin',
  20508. ' f:=@fly;',
  20509. ' g:=@glide;',
  20510. ' r:=@run;',
  20511. 'end;',
  20512. 'class procedure THelper.glide(w: word);',
  20513. 'begin',
  20514. ' g:=@glide;',
  20515. ' r:=@run;',
  20516. 'end;',
  20517. 'class procedure THelper.run(w: word);',
  20518. 'begin',
  20519. ' g:=@glide;',
  20520. ' r:=@run;',
  20521. 'end;',
  20522. 'var',
  20523. ' Obj: TObject;',
  20524. 'begin',
  20525. ' f:[email protected];',
  20526. ' g:[email protected];',
  20527. ' r:[email protected];',
  20528. ' with obj do begin',
  20529. ' f:=@fly;',
  20530. ' g:=@glide;',
  20531. ' r:=@run;',
  20532. ' end;',
  20533. ' g:[email protected];',
  20534. ' r:[email protected];',
  20535. ' with tobject do begin',
  20536. ' g:=@glide;',
  20537. ' r:=@run;',
  20538. ' end;',
  20539. '']);
  20540. ConvertProgram;
  20541. CheckSource('TestClassHelper_MethodRefObjFPC',
  20542. LinesToStr([ // statements
  20543. 'rtl.createClass($mod, "TObject", null, function () {',
  20544. ' this.$init = function () {',
  20545. ' };',
  20546. ' this.$final = function () {',
  20547. ' };',
  20548. ' this.DoIt = function () {',
  20549. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20550. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20551. ' $mod.r = $mod.THelper.Run;',
  20552. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20553. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20554. ' $mod.r = $mod.THelper.Run;',
  20555. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20556. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20557. ' $mod.r = $mod.THelper.Run;',
  20558. ' };',
  20559. '});',
  20560. 'rtl.createHelper($mod, "THelper", null, function () {',
  20561. ' this.Fly = function (w) {',
  20562. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20563. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20564. ' $mod.r = $mod.THelper.Run;',
  20565. ' };',
  20566. ' this.Glide = function (w) {',
  20567. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  20568. ' $mod.r = $mod.THelper.Run;',
  20569. ' };',
  20570. ' this.Run = function (w) {',
  20571. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  20572. ' $mod.r = $mod.THelper.Run;',
  20573. ' };',
  20574. '});',
  20575. 'this.f = null;',
  20576. 'this.g = null;',
  20577. 'this.r = null;',
  20578. 'this.Obj = null;',
  20579. '']),
  20580. LinesToStr([ // $mod.$main
  20581. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  20582. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  20583. '$mod.r = $mod.THelper.Run;',
  20584. 'var $with = $mod.Obj;',
  20585. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  20586. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  20587. '$mod.r = $mod.THelper.Run;',
  20588. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  20589. '$mod.r = $mod.THelper.Run;',
  20590. 'var $with1 = $mod.TObject;',
  20591. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  20592. '$mod.r = $mod.THelper.Run;',
  20593. '']));
  20594. end;
  20595. procedure TTestModule.TestClassHelper_Constructor;
  20596. begin
  20597. StartProgram(false);
  20598. Add([
  20599. 'type',
  20600. ' TObject = class',
  20601. ' constructor Create;',
  20602. ' end;',
  20603. ' TClass = class of TObject;',
  20604. ' THelper = class helper for TObject',
  20605. ' constructor NewHlp(w: word);',
  20606. ' end;',
  20607. 'var',
  20608. ' obj: TObject;',
  20609. ' c: TClass;',
  20610. 'constructor TObject.Create;',
  20611. 'begin',
  20612. ' NewHlp(2);', // normal call
  20613. ' tobject.NewHlp(3);', // new instance
  20614. ' c.newhlp(4);', // new instance
  20615. 'end;',
  20616. 'constructor THelper.NewHlp(w: word);',
  20617. 'begin',
  20618. ' create;', // normal call
  20619. ' tobject.create;', // new instance
  20620. ' NewHlp(2);', // normal call
  20621. ' tobject.NewHlp(3);', // new instance
  20622. ' c.newhlp(4);', // new instance
  20623. 'end;',
  20624. 'begin',
  20625. ' obj.newhlp(2);', // normal call
  20626. ' with Obj do newhlp(12);', // normal call
  20627. ' tobject.newhlp(3);', // new instance
  20628. ' with tobject do newhlp(13);', // new instance
  20629. ' c.newhlp(4);', // new instance
  20630. ' with c do newhlp(14);', // new instance
  20631. '']);
  20632. ConvertProgram;
  20633. CheckSource('TestClassHelper_Constructor',
  20634. LinesToStr([ // statements
  20635. 'rtl.createClass($mod, "TObject", null, function () {',
  20636. ' this.$init = function () {',
  20637. ' };',
  20638. ' this.$final = function () {',
  20639. ' };',
  20640. ' this.Create = function () {',
  20641. ' $mod.THelper.NewHlp.call(this, 2);',
  20642. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20643. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20644. ' return this;',
  20645. ' };',
  20646. '});',
  20647. 'rtl.createHelper($mod, "THelper", null, function () {',
  20648. ' this.NewHlp = function (w) {',
  20649. ' this.Create();',
  20650. ' $mod.TObject.$create("Create");',
  20651. ' $mod.THelper.NewHlp.call(this, 2);',
  20652. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20653. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  20654. ' return this;',
  20655. ' };',
  20656. '});',
  20657. 'this.obj = null;',
  20658. 'this.c = null;',
  20659. '']),
  20660. LinesToStr([ // $mod.$main
  20661. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  20662. 'var $with = $mod.obj;',
  20663. '$mod.THelper.NewHlp.call($with, 12);',
  20664. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  20665. 'var $with1 = $mod.TObject;',
  20666. '$with1.$create($mod.THelper.NewHlp, [13]);',
  20667. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  20668. 'var $with2 = $mod.c;',
  20669. '$with2.$create($mod.THelper.NewHlp, [14]);',
  20670. '']));
  20671. end;
  20672. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  20673. begin
  20674. StartProgram(false);
  20675. Add([
  20676. 'type',
  20677. ' TObject = class',
  20678. ' procedure Fly;',
  20679. ' end;',
  20680. ' TObjHelper = class helper for TObject',
  20681. ' procedure Fly;',
  20682. ' end;',
  20683. ' TBird = class',
  20684. ' procedure Fly;',
  20685. ' end;',
  20686. ' TBirdHelper = class helper for TBird',
  20687. ' procedure Fly;',
  20688. ' procedure Walk(w: word);',
  20689. ' end;',
  20690. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  20691. ' procedure Fly;',
  20692. ' procedure Walk(w: word);',
  20693. ' end;',
  20694. 'procedure Tobject.fly;',
  20695. 'begin',
  20696. ' inherited;', // ignore
  20697. 'end;',
  20698. 'procedure Tobjhelper.fly;',
  20699. 'begin',
  20700. ' {@TObject_Fly}inherited;',
  20701. ' inherited {@TObject_Fly}Fly;',
  20702. 'end;',
  20703. 'procedure Tbird.fly;',
  20704. 'begin',
  20705. ' {@TObjHelper_Fly}inherited;',
  20706. ' inherited {@TObjHelper_Fly}Fly;',
  20707. 'end;',
  20708. 'procedure Tbirdhelper.fly;',
  20709. 'begin',
  20710. ' {@TBird_Fly}inherited;',
  20711. ' inherited {@TBird_Fly}Fly;',
  20712. 'end;',
  20713. 'procedure Tbirdhelper.walk(w: word);',
  20714. 'begin',
  20715. 'end;',
  20716. 'procedure teagleHelper.fly;',
  20717. 'begin',
  20718. ' {@TBird_Fly}inherited;',
  20719. ' inherited {@TBird_Fly}Fly;',
  20720. 'end;',
  20721. 'procedure teagleHelper.walk(w: word);',
  20722. 'begin',
  20723. ' {@TBirdHelper_Walk}inherited;',
  20724. ' inherited {@TBirdHelper_Walk}Walk(3);',
  20725. 'end;',
  20726. 'begin',
  20727. '']);
  20728. ConvertProgram;
  20729. CheckSource('TestClassHelper_InheritedObjFPC',
  20730. LinesToStr([ // statements
  20731. 'rtl.createClass($mod, "TObject", null, function () {',
  20732. ' this.$init = function () {',
  20733. ' };',
  20734. ' this.$final = function () {',
  20735. ' };',
  20736. ' this.Fly = function () {',
  20737. ' };',
  20738. '});',
  20739. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20740. ' this.Fly = function () {',
  20741. ' $mod.TObject.Fly.call(this);',
  20742. ' $mod.TObject.Fly.call(this);',
  20743. ' };',
  20744. '});',
  20745. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20746. ' this.Fly$1 = function () {',
  20747. ' $mod.TObjHelper.Fly.call(this);',
  20748. ' $mod.TObjHelper.Fly.call(this);',
  20749. ' };',
  20750. '});',
  20751. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  20752. ' this.Fly = function () {',
  20753. ' $mod.TBird.Fly$1.call(this);',
  20754. ' $mod.TBird.Fly$1.call(this);',
  20755. ' };',
  20756. ' this.Walk = function (w) {',
  20757. ' };',
  20758. '});',
  20759. 'rtl.createHelper($mod, "TEagleHelper", $mod.TBirdHelper, function () {',
  20760. ' this.Fly$1 = function () {',
  20761. ' $mod.TBird.Fly$1.call(this);',
  20762. ' $mod.TBird.Fly$1.call(this);',
  20763. ' };',
  20764. ' this.Walk$1 = function (w) {',
  20765. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  20766. ' $mod.TBirdHelper.Walk.call(this, 3);',
  20767. ' };',
  20768. '});',
  20769. '']),
  20770. LinesToStr([ // $mod.$main
  20771. '']));
  20772. end;
  20773. procedure TTestModule.TestClassHelper_Property;
  20774. begin
  20775. StartProgram(false);
  20776. Add([
  20777. 'type',
  20778. ' TObject = class',
  20779. ' FSize: word;',
  20780. ' function GetSpeed: word;',
  20781. ' procedure SetSpeed(Value: word);',
  20782. ' end;',
  20783. ' TObjHelper = class helper for TObject',
  20784. ' function GetLeft: word;',
  20785. ' procedure SetLeft(Value: word);',
  20786. ' property Size: word read FSize write FSize;',
  20787. ' property Speed: word read GetSpeed write SetSpeed;',
  20788. ' property Left: word read GetLeft write SetLeft;',
  20789. ' end;',
  20790. ' TBird = class',
  20791. ' property NotRight: word read GetLeft write SetLeft;',
  20792. ' procedure DoIt;',
  20793. ' end;',
  20794. 'var',
  20795. ' b: TBird;',
  20796. 'function Tobject.GetSpeed: word;',
  20797. 'begin',
  20798. ' Size:=Size+11;',
  20799. ' Speed:=Speed+12;',
  20800. ' Result:=Left+13;',
  20801. ' Left:=13;',
  20802. ' Left:=Left+13;',
  20803. ' Self.Size:=Self.Size+21;',
  20804. ' Self.Speed:=Self.Speed+22;',
  20805. ' Self.Left:=Self.Left+23;',
  20806. ' with Self do begin',
  20807. ' Size:=Size+31;',
  20808. ' Speed:=Speed+32;',
  20809. ' Left:=Left+33;',
  20810. ' end;',
  20811. 'end;',
  20812. 'procedure Tobject.SetSpeed(Value: word);',
  20813. 'begin',
  20814. 'end;',
  20815. 'function TObjHelper.GetLeft: word;',
  20816. 'begin',
  20817. ' Size:=Size+11;',
  20818. ' Speed:=Speed+12;',
  20819. ' Left:=Left+13;',
  20820. ' Self.Size:=Self.Size+21;',
  20821. ' Self.Speed:=Self.Speed+22;',
  20822. ' Self.Left:=Self.Left+23;',
  20823. ' with Self do begin',
  20824. ' Size:=Size+31;',
  20825. ' Speed:=Speed+32;',
  20826. ' Left:=Left+33;',
  20827. ' end;',
  20828. 'end;',
  20829. 'procedure TObjHelper.SetLeft(Value: word);',
  20830. 'begin',
  20831. 'end;',
  20832. 'procedure TBird.DoIt;',
  20833. 'begin',
  20834. ' NotRight:=NotRight+11;',
  20835. ' Self.NotRight:=Self.NotRight+21;',
  20836. ' with Self do begin',
  20837. ' NotRight:=NotRight+31;',
  20838. ' end;',
  20839. 'end;',
  20840. 'begin',
  20841. ' b.Size:=b.Size+11;',
  20842. ' b.Speed:=b.Speed+12;',
  20843. ' b.Left:=b.Left+13;',
  20844. ' b.NotRight:=b.NotRight+14;',
  20845. ' with b do begin',
  20846. ' Size:=Size+31;',
  20847. ' Speed:=Speed+32;',
  20848. ' Left:=Left+33;',
  20849. ' NotRight:=NotRight+34;',
  20850. ' end;',
  20851. '']);
  20852. ConvertProgram;
  20853. CheckSource('TestClassHelper_Property',
  20854. LinesToStr([ // statements
  20855. 'rtl.createClass($mod, "TObject", null, function () {',
  20856. ' this.$init = function () {',
  20857. ' this.FSize = 0;',
  20858. ' };',
  20859. ' this.$final = function () {',
  20860. ' };',
  20861. ' this.GetSpeed = function () {',
  20862. ' var Result = 0;',
  20863. ' this.FSize = this.FSize + 11;',
  20864. ' this.SetSpeed(this.GetSpeed() + 12);',
  20865. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  20866. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  20867. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20868. ' this.FSize = this.FSize + 21;',
  20869. ' this.SetSpeed(this.GetSpeed() + 22);',
  20870. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20871. ' this.FSize = this.FSize + 31;',
  20872. ' this.SetSpeed(this.GetSpeed() + 32);',
  20873. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20874. ' return Result;',
  20875. ' };',
  20876. ' this.SetSpeed = function (Value) {',
  20877. ' };',
  20878. '});',
  20879. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  20880. ' this.GetLeft = function () {',
  20881. ' var Result = 0;',
  20882. ' this.FSize = this.FSize + 11;',
  20883. ' this.SetSpeed(this.GetSpeed() + 12);',
  20884. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  20885. ' this.FSize = this.FSize + 21;',
  20886. ' this.SetSpeed(this.GetSpeed() + 22);',
  20887. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  20888. ' this.FSize = this.FSize + 31;',
  20889. ' this.SetSpeed(this.GetSpeed() + 32);',
  20890. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  20891. ' return Result;',
  20892. ' };',
  20893. ' this.SetLeft = function (Value) {',
  20894. ' };',
  20895. '});',
  20896. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  20897. ' this.DoIt = function () {',
  20898. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  20899. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  20900. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  20901. ' };',
  20902. '});',
  20903. 'this.b = null;',
  20904. '']),
  20905. LinesToStr([ // $mod.$main
  20906. '$mod.b.FSize = $mod.b.FSize + 11;',
  20907. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  20908. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  20909. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  20910. 'var $with = $mod.b;',
  20911. '$with.FSize = $with.FSize + 31;',
  20912. '$with.SetSpeed($with.GetSpeed() + 32);',
  20913. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  20914. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  20915. '']));
  20916. end;
  20917. procedure TTestModule.TestClassHelper_Property_Array;
  20918. begin
  20919. StartProgram(false);
  20920. Add([
  20921. 'type',
  20922. ' TObject = class',
  20923. ' function GetSpeed(Index: boolean): word;',
  20924. ' procedure SetSpeed(Index: boolean; Value: word);',
  20925. ' end;',
  20926. ' TObjHelper = class helper for TObject',
  20927. ' function GetSize(Index: boolean): word;',
  20928. ' procedure SetSize(Index: boolean; Value: word);',
  20929. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  20930. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  20931. ' end;',
  20932. ' TBird = class',
  20933. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  20934. ' procedure DoIt;',
  20935. ' end;',
  20936. 'var',
  20937. ' b: TBird;',
  20938. 'function Tobject.GetSpeed(Index: boolean): word;',
  20939. 'begin',
  20940. ' Result:=Size[false];',
  20941. ' Size[true]:=Size[false]+11;',
  20942. ' Speed[true]:=Speed[false]+12;',
  20943. ' Self.Size[true]:=Self.Size[false]+21;',
  20944. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20945. ' with Self do begin',
  20946. ' Size[true]:=Size[false]+31;',
  20947. ' Speed[true]:=Speed[false]+32;',
  20948. ' end;',
  20949. 'end;',
  20950. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  20951. 'begin',
  20952. 'end;',
  20953. 'function TObjHelper.GetSize(Index: boolean): word;',
  20954. 'begin',
  20955. ' Size[true]:=Size[false]+11;',
  20956. ' Speed[true]:=Speed[false]+12;',
  20957. ' Self.Size[true]:=Self.Size[false]+21;',
  20958. ' Self.Speed[true]:=Self.Speed[false]+22;',
  20959. ' with Self do begin',
  20960. ' Size[true]:=Size[false]+31;',
  20961. ' Speed[true]:=Speed[false]+32;',
  20962. ' end;',
  20963. 'end;',
  20964. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  20965. 'begin',
  20966. 'end;',
  20967. 'procedure TBird.DoIt;',
  20968. 'begin',
  20969. ' Items[true]:=Items[false]+11;',
  20970. ' Self.Items[true]:=Self.Items[false]+21;',
  20971. ' with Self do Items[true]:=Items[false]+31;',
  20972. 'end;',
  20973. 'begin',
  20974. ' b.Size[true]:=b.Size[false]+11;',
  20975. ' b.Speed[true]:=b.Speed[false]+12;',
  20976. ' b.Items[true]:=b.Items[false]+13;',
  20977. ' with b do begin',
  20978. ' Size[true]:=Size[false]+21;',
  20979. ' Speed[true]:=Speed[false]+22;',
  20980. ' Items[true]:=Items[false]+23;',
  20981. ' end;',
  20982. '']);
  20983. ConvertProgram;
  20984. CheckSource('TestClassHelper_Property_Array',
  20985. LinesToStr([ // statements
  20986. 'rtl.createClass($mod, "TObject", null, function () {',
  20987. ' this.$init = function () {',
  20988. ' };',
  20989. ' this.$final = function () {',
  20990. ' };',
  20991. ' this.GetSpeed = function (Index) {',
  20992. ' var Result = 0;',
  20993. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  20994. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  20995. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  20996. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  20997. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  20998. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  20999. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21000. ' return Result;',
  21001. ' };',
  21002. ' this.SetSpeed = function (Index, Value) {',
  21003. ' };',
  21004. '});',
  21005. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21006. ' this.GetSize = function (Index) {',
  21007. ' var Result = 0;',
  21008. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21009. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21010. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21011. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21012. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21013. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21014. ' return Result;',
  21015. ' };',
  21016. ' this.SetSize = function (Index, Value) {',
  21017. ' };',
  21018. '});',
  21019. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21020. ' this.DoIt = function () {',
  21021. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21022. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21023. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21024. ' };',
  21025. '});',
  21026. 'this.b = null;',
  21027. '']),
  21028. LinesToStr([ // $mod.$main
  21029. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21030. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21031. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21032. 'var $with = $mod.b;',
  21033. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21034. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21035. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21036. '']));
  21037. end;
  21038. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21039. begin
  21040. StartProgram(false);
  21041. Add([
  21042. 'type',
  21043. ' TObject = class',
  21044. ' function GetSpeed(Index: boolean): word;',
  21045. ' procedure SetSpeed(Index: boolean; Value: word);',
  21046. ' end;',
  21047. ' TObjHelper = class helper for TObject',
  21048. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21049. ' end;',
  21050. ' TBird = class',
  21051. ' end;',
  21052. ' TBirdHelper = class helper for TBird',
  21053. ' function GetSize(Index: word): boolean;',
  21054. ' procedure SetSize(Index: word; Value: boolean);',
  21055. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21056. ' end;',
  21057. 'function Tobject.GetSpeed(Index: boolean): word;',
  21058. 'begin',
  21059. ' Self[true]:=Self[false]+1;',
  21060. 'end;',
  21061. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21062. 'begin',
  21063. 'end;',
  21064. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21065. 'begin',
  21066. ' Self[1]:=not Self[2];',
  21067. 'end;',
  21068. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21069. 'begin',
  21070. 'end;',
  21071. 'var',
  21072. ' o: TObject;',
  21073. ' b: TBird;',
  21074. 'begin',
  21075. ' o[true]:=o[false]+1;',
  21076. ' b[3]:=not b[4];',
  21077. '']);
  21078. ConvertProgram;
  21079. CheckSource('TestClassHelper_Property_Array_Default',
  21080. LinesToStr([ // statements
  21081. 'rtl.createClass($mod, "TObject", null, function () {',
  21082. ' this.$init = function () {',
  21083. ' };',
  21084. ' this.$final = function () {',
  21085. ' };',
  21086. ' this.GetSpeed = function (Index) {',
  21087. ' var Result = 0;',
  21088. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21089. ' return Result;',
  21090. ' };',
  21091. ' this.SetSpeed = function (Index, Value) {',
  21092. ' };',
  21093. '});',
  21094. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21095. '});',
  21096. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21097. '});',
  21098. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  21099. ' this.GetSize = function (Index) {',
  21100. ' var Result = false;',
  21101. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21102. ' return Result;',
  21103. ' };',
  21104. ' this.SetSize = function (Index, Value) {',
  21105. ' };',
  21106. '});',
  21107. 'this.o = null;',
  21108. 'this.b = null;',
  21109. '']),
  21110. LinesToStr([ // $mod.$main
  21111. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21112. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21113. '']));
  21114. end;
  21115. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21116. begin
  21117. StartProgram(false);
  21118. Add([
  21119. 'type',
  21120. ' TObject = class',
  21121. ' end;',
  21122. ' TObjHelper = class helper for TObject',
  21123. ' function GetItems(Index: word): TObject;',
  21124. ' procedure SetItems(Index: word; Value: TObject);',
  21125. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21126. ' end;',
  21127. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21128. 'begin',
  21129. ' Self[1][2]:=Self[3][4];',
  21130. 'end;',
  21131. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21132. 'begin',
  21133. 'end;',
  21134. 'var',
  21135. ' o: TObject;',
  21136. 'begin',
  21137. ' o[1][2]:=o[3][4];',
  21138. '']);
  21139. ConvertProgram;
  21140. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21141. LinesToStr([ // statements
  21142. 'rtl.createClass($mod, "TObject", null, function () {',
  21143. ' this.$init = function () {',
  21144. ' };',
  21145. ' this.$final = function () {',
  21146. ' };',
  21147. '});',
  21148. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21149. ' this.GetItems = function (Index) {',
  21150. ' var Result = null;',
  21151. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21152. ' return Result;',
  21153. ' };',
  21154. ' this.SetItems = function (Index, Value) {',
  21155. ' };',
  21156. '});',
  21157. 'this.o = null;',
  21158. '']),
  21159. LinesToStr([ // $mod.$main
  21160. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21161. '']));
  21162. end;
  21163. procedure TTestModule.TestClassHelper_ClassProperty;
  21164. begin
  21165. StartProgram(false);
  21166. Add([
  21167. 'type',
  21168. ' TObject = class',
  21169. ' class var FSize: word;',
  21170. ' class function GetSpeed: word;',
  21171. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21172. ' end;',
  21173. ' TObjHelper = class helper for TObject',
  21174. ' class function GetLeft: word;',
  21175. ' class procedure SetLeft(Value: word);',
  21176. ' class property Size: word read FSize write FSize;',
  21177. ' class property Speed: word read GetSpeed write SetSpeed;',
  21178. ' class property Left: word read GetLeft write SetLeft;',
  21179. ' end;',
  21180. ' TBird = class',
  21181. ' class property NotRight: word read GetLeft write SetLeft;',
  21182. ' class procedure DoIt;',
  21183. ' end;',
  21184. ' TBirdClass = class of TBird;',
  21185. 'class function Tobject.GetSpeed: word;',
  21186. 'begin',
  21187. ' Size:=Size+11;',
  21188. ' Speed:=Speed+12;',
  21189. ' Left:=Left+13;',
  21190. ' Self.Size:=Self.Size+21;',
  21191. ' Self.Speed:=Self.Speed+22;',
  21192. ' Self.Left:=Self.Left+23;',
  21193. ' with Self do begin',
  21194. ' Size:=Size+31;',
  21195. ' Speed:=Speed+32;',
  21196. ' Left:=Left+33;',
  21197. ' end;',
  21198. 'end;',
  21199. 'class function TObjHelper.GetLeft: word;',
  21200. 'begin',
  21201. ' Size:=Size+11;',
  21202. ' Speed:=Speed+12;',
  21203. ' Left:=Left+13;',
  21204. ' Self.Size:=Self.Size+21;',
  21205. ' Self.Speed:=Self.Speed+22;',
  21206. ' Self.Left:=Self.Left+23;',
  21207. ' with Self do begin',
  21208. ' Size:=Size+31;',
  21209. ' Speed:=Speed+32;',
  21210. ' Left:=Left+33;',
  21211. ' end;',
  21212. 'end;',
  21213. 'class procedure TObjHelper.SetLeft(Value: word);',
  21214. 'begin',
  21215. 'end;',
  21216. 'class procedure TBird.DoIt;',
  21217. 'begin',
  21218. ' NotRight:=NotRight+11;',
  21219. ' Self.NotRight:=Self.NotRight+21;',
  21220. ' with Self do NotRight:=NotRight+31;',
  21221. 'end;',
  21222. 'var',
  21223. ' b: TBird;',
  21224. ' c: TBirdClass;',
  21225. 'begin',
  21226. ' b.Size:=b.Size+11;',
  21227. ' b.Speed:=b.Speed+12;',
  21228. ' b.Left:=b.Left+13;',
  21229. ' b.NotRight:=b.NotRight+14;',
  21230. ' with b do begin',
  21231. ' Size:=Size+31;',
  21232. ' Speed:=Speed+32;',
  21233. ' Left:=Left+33;',
  21234. ' NotRight:=NotRight+34;',
  21235. ' end;',
  21236. ' c.Size:=c.Size+11;',
  21237. ' c.Speed:=c.Speed+12;',
  21238. ' c.Left:=c.Left+13;',
  21239. ' c.NotRight:=c.NotRight+14;',
  21240. ' with c do begin',
  21241. ' Size:=Size+31;',
  21242. ' Speed:=Speed+32;',
  21243. ' Left:=Left+33;',
  21244. ' NotRight:=NotRight+34;',
  21245. ' end;',
  21246. ' tbird.Size:=tbird.Size+11;',
  21247. ' tbird.Speed:=tbird.Speed+12;',
  21248. ' tbird.Left:=tbird.Left+13;',
  21249. ' tbird.NotRight:=tbird.NotRight+14;',
  21250. ' with tbird do begin',
  21251. ' Size:=Size+31;',
  21252. ' Speed:=Speed+32;',
  21253. ' Left:=Left+33;',
  21254. ' NotRight:=NotRight+34;',
  21255. ' end;',
  21256. '']);
  21257. ConvertProgram;
  21258. CheckSource('TestClassHelper_ClassProperty',
  21259. LinesToStr([ // statements
  21260. 'rtl.createClass($mod, "TObject", null, function () {',
  21261. ' this.FSize = 0;',
  21262. ' this.$init = function () {',
  21263. ' };',
  21264. ' this.$final = function () {',
  21265. ' };',
  21266. ' this.GetSpeed = function () {',
  21267. ' var Result = 0;',
  21268. ' $mod.TObject.FSize = this.FSize + 11;',
  21269. ' this.SetSpeed(this.GetSpeed() + 12);',
  21270. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21271. ' $mod.TObject.FSize = this.FSize + 21;',
  21272. ' this.SetSpeed(this.GetSpeed() + 22);',
  21273. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21274. ' $mod.TObject.FSize = this.FSize + 31;',
  21275. ' this.SetSpeed(this.GetSpeed() + 32);',
  21276. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21277. ' return Result;',
  21278. ' };',
  21279. '});',
  21280. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21281. ' this.GetLeft = function () {',
  21282. ' var Result = 0;',
  21283. ' $mod.TObject.FSize = this.FSize + 11;',
  21284. ' this.SetSpeed(this.GetSpeed() + 12);',
  21285. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21286. ' $mod.TObject.FSize = this.FSize + 21;',
  21287. ' this.SetSpeed(this.GetSpeed() + 22);',
  21288. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21289. ' $mod.TObject.FSize = this.FSize + 31;',
  21290. ' this.SetSpeed(this.GetSpeed() + 32);',
  21291. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21292. ' return Result;',
  21293. ' };',
  21294. ' this.SetLeft = function (Value) {',
  21295. ' };',
  21296. '});',
  21297. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21298. ' this.DoIt = function () {',
  21299. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21300. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21301. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21302. ' };',
  21303. '});',
  21304. 'this.b = null;',
  21305. 'this.c = null;',
  21306. '']),
  21307. LinesToStr([ // $mod.$main
  21308. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21309. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21310. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21311. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21312. 'var $with = $mod.b;',
  21313. '$mod.TObject.FSize = $with.FSize + 31;',
  21314. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21315. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21316. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21317. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21318. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21319. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21320. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21321. 'var $with1 = $mod.c;',
  21322. '$mod.TObject.FSize = $with1.FSize + 31;',
  21323. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21324. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21325. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21326. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21327. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21328. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21329. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21330. 'var $with2 = $mod.TBird;',
  21331. '$mod.TObject.FSize = $with2.FSize + 31;',
  21332. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21333. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21334. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21335. '']));
  21336. end;
  21337. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21338. begin
  21339. StartProgram(false);
  21340. Add([
  21341. 'type',
  21342. ' TObject = class',
  21343. ' class function GetSpeed: word; static;',
  21344. ' class procedure SetSpeed(Value: word); static;',
  21345. ' end;',
  21346. ' TObjHelper = class helper for TObject',
  21347. ' class function GetLeft: word; static;',
  21348. ' class procedure SetLeft(Value: word); static;',
  21349. ' class property Speed: word read GetSpeed write SetSpeed;',
  21350. ' class property Left: word read GetLeft write SetLeft;',
  21351. ' end;',
  21352. ' TBird = class',
  21353. ' class property NotRight: word read GetLeft write SetLeft;',
  21354. ' class procedure DoIt; static;',
  21355. ' class procedure DoSome;',
  21356. ' end;',
  21357. ' TBirdClass = class of TBird;',
  21358. 'class function Tobject.GetSpeed: word;',
  21359. 'begin',
  21360. ' Speed:=Speed+12;',
  21361. ' Left:=Left+13;',
  21362. 'end;',
  21363. 'class procedure TObject.SetSpeed(Value: word);',
  21364. 'begin',
  21365. 'end;',
  21366. 'class function TObjHelper.GetLeft: word;',
  21367. 'begin',
  21368. ' Speed:=Speed+12;',
  21369. ' Left:=Left+13;',
  21370. 'end;',
  21371. 'class procedure TObjHelper.SetLeft(Value: word);',
  21372. 'begin',
  21373. 'end;',
  21374. 'class procedure TBird.DoIt;',
  21375. 'begin',
  21376. ' NotRight:=NotRight+11;',
  21377. 'end;',
  21378. 'class procedure TBird.DoSome;',
  21379. 'begin',
  21380. ' Speed:=Speed+12;',
  21381. ' Left:=Left+13;',
  21382. ' Self.Speed:=Self.Speed+22;',
  21383. ' Self.Left:=Self.Left+23;',
  21384. ' with Self do begin',
  21385. ' Speed:=Speed+32;',
  21386. ' Left:=Left+33;',
  21387. ' end;',
  21388. ' NotRight:=NotRight+11;',
  21389. ' Self.NotRight:=Self.NotRight+21;',
  21390. ' with Self do NotRight:=NotRight+31;',
  21391. 'end;',
  21392. 'var',
  21393. ' b: TBird;',
  21394. ' c: TBirdClass;',
  21395. 'begin',
  21396. ' b.Speed:=b.Speed+12;',
  21397. ' b.Left:=b.Left+13;',
  21398. ' b.NotRight:=b.NotRight+14;',
  21399. ' with b do begin',
  21400. ' Speed:=Speed+32;',
  21401. ' Left:=Left+33;',
  21402. ' NotRight:=NotRight+34;',
  21403. ' end;',
  21404. ' c.Speed:=c.Speed+12;',
  21405. ' c.Left:=c.Left+13;',
  21406. ' c.NotRight:=c.NotRight+14;',
  21407. ' with c do begin',
  21408. ' Speed:=Speed+32;',
  21409. ' Left:=Left+33;',
  21410. ' NotRight:=NotRight+34;',
  21411. ' end;',
  21412. ' tbird.Speed:=tbird.Speed+12;',
  21413. ' tbird.Left:=tbird.Left+13;',
  21414. ' tbird.NotRight:=tbird.NotRight+14;',
  21415. ' with tbird do begin',
  21416. ' Speed:=Speed+32;',
  21417. ' Left:=Left+33;',
  21418. ' NotRight:=NotRight+34;',
  21419. ' end;',
  21420. '']);
  21421. ConvertProgram;
  21422. CheckSource('TestClassHelper_ClassPropertyStatic',
  21423. LinesToStr([ // statements
  21424. 'rtl.createClass($mod, "TObject", null, function () {',
  21425. ' this.$init = function () {',
  21426. ' };',
  21427. ' this.$final = function () {',
  21428. ' };',
  21429. ' this.GetSpeed = function () {',
  21430. ' var Result = 0;',
  21431. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21432. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21433. ' return Result;',
  21434. ' };',
  21435. ' this.SetSpeed = function (Value) {',
  21436. ' };',
  21437. '});',
  21438. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21439. ' this.GetLeft = function () {',
  21440. ' var Result = 0;',
  21441. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21442. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21443. ' return Result;',
  21444. ' };',
  21445. ' this.SetLeft = function (Value) {',
  21446. ' };',
  21447. '});',
  21448. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21449. ' this.DoIt = function () {',
  21450. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21451. ' };',
  21452. ' this.DoSome = function () {',
  21453. ' this.SetSpeed(this.GetSpeed() + 12);',
  21454. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21455. ' this.SetSpeed(this.GetSpeed() + 22);',
  21456. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  21457. ' this.SetSpeed(this.GetSpeed() + 32);',
  21458. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21459. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21460. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  21461. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  21462. ' };',
  21463. '});',
  21464. 'this.b = null;',
  21465. 'this.c = null;',
  21466. '']),
  21467. LinesToStr([ // $mod.$main
  21468. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21469. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21470. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21471. 'var $with = $mod.b;',
  21472. '$with.SetSpeed($with.GetSpeed() + 32);',
  21473. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21474. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21475. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21476. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21477. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21478. 'var $with1 = $mod.c;',
  21479. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21480. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21481. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21482. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21483. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21484. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21485. 'var $with2 = $mod.TBird;',
  21486. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21487. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21488. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21489. '']));
  21490. end;
  21491. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  21492. begin
  21493. StartProgram(false);
  21494. Add([
  21495. 'type',
  21496. ' TObject = class',
  21497. ' class function GetSpeed(Index: boolean): word;',
  21498. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  21499. ' end;',
  21500. ' TObjHelper = class helper for TObject',
  21501. ' class function GetSize(Index: boolean): word;',
  21502. ' class procedure SetSize(Index: boolean; Value: word);',
  21503. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  21504. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21505. ' end;',
  21506. ' TBird = class',
  21507. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  21508. ' class procedure DoIt;',
  21509. ' end;',
  21510. ' TBirdClass = class of TBird;',
  21511. 'class function Tobject.GetSpeed(Index: boolean): word;',
  21512. 'begin',
  21513. ' Size[true]:=Size[false]+11;',
  21514. ' Speed[true]:=Speed[false]+12;',
  21515. ' Self.Size[true]:=Self.Size[false]+21;',
  21516. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21517. ' with Self do begin',
  21518. ' Size[true]:=Size[false]+31;',
  21519. ' Speed[true]:=Speed[false]+32;',
  21520. ' end;',
  21521. 'end;',
  21522. 'class function TObjHelper.GetSize(Index: boolean): word;',
  21523. 'begin',
  21524. ' Size[true]:=Size[false]+11;',
  21525. ' Speed[true]:=Speed[false]+12;',
  21526. ' Self.Size[true]:=Self.Size[false]+21;',
  21527. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21528. ' with Self do begin',
  21529. ' Size[true]:=Size[false]+31;',
  21530. ' Speed[true]:=Speed[false]+32;',
  21531. ' end;',
  21532. 'end;',
  21533. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21534. 'begin',
  21535. 'end;',
  21536. 'class procedure TBird.DoIt;',
  21537. 'begin',
  21538. ' Items[true]:=Items[false]+11;',
  21539. ' Self.Items[true]:=Self.Items[false]+21;',
  21540. ' with Self do Items[true]:=Items[false]+31;',
  21541. 'end;',
  21542. 'var',
  21543. ' b: TBird;',
  21544. ' c: TBirdClass;',
  21545. 'begin',
  21546. ' b.Size[true]:=b.Size[false]+11;',
  21547. ' b.Speed[true]:=b.Speed[false]+12;',
  21548. ' b.Items[true]:=b.Items[false]+13;',
  21549. ' with b do begin',
  21550. ' Size[true]:=Size[false]+21;',
  21551. ' Speed[true]:=Speed[false]+22;',
  21552. ' Items[true]:=Items[false]+23;',
  21553. ' end;',
  21554. ' c.Size[true]:=c.Size[false]+11;',
  21555. ' c.Speed[true]:=c.Speed[false]+12;',
  21556. ' c.Items[true]:=c.Items[false]+13;',
  21557. ' with c do begin',
  21558. ' Size[true]:=Size[false]+21;',
  21559. ' Speed[true]:=Speed[false]+22;',
  21560. ' Items[true]:=Items[false]+23;',
  21561. ' end;',
  21562. ' TBird.Size[true]:=TBird.Size[false]+11;',
  21563. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  21564. ' TBird.Items[true]:=TBird.Items[false]+13;',
  21565. ' with TBird do begin',
  21566. ' Size[true]:=Size[false]+21;',
  21567. ' Speed[true]:=Speed[false]+22;',
  21568. ' Items[true]:=Items[false]+23;',
  21569. ' end;',
  21570. '']);
  21571. ConvertProgram;
  21572. CheckSource('TestClassHelper_ClassProperty_Array',
  21573. LinesToStr([ // statements
  21574. 'rtl.createClass($mod, "TObject", null, function () {',
  21575. ' this.$init = function () {',
  21576. ' };',
  21577. ' this.$final = function () {',
  21578. ' };',
  21579. ' this.GetSpeed = function (Index) {',
  21580. ' var Result = 0;',
  21581. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21582. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21583. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21584. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21585. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21586. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21587. ' return Result;',
  21588. ' };',
  21589. '});',
  21590. 'rtl.createHelper($mod, "TObjHelper", null, function () {',
  21591. ' this.GetSize = function (Index) {',
  21592. ' var Result = 0;',
  21593. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21594. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21595. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21596. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21597. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21598. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21599. ' return Result;',
  21600. ' };',
  21601. ' this.SetSize = function (Index, Value) {',
  21602. ' };',
  21603. '});',
  21604. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21605. ' this.DoIt = function () {',
  21606. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21607. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21608. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21609. ' };',
  21610. '});',
  21611. 'this.b = null;',
  21612. 'this.c = null;',
  21613. '']),
  21614. LinesToStr([ // $mod.$main
  21615. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  21616. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  21617. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  21618. 'var $with = $mod.b;',
  21619. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  21620. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  21621. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  21622. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  21623. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  21624. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  21625. 'var $with1 = $mod.c;',
  21626. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  21627. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  21628. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  21629. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  21630. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  21631. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  21632. 'var $with2 = $mod.TBird;',
  21633. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  21634. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  21635. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  21636. '']));
  21637. end;
  21638. procedure TTestModule.TestClassHelper_ForIn;
  21639. begin
  21640. StartProgram(false);
  21641. Add([
  21642. 'type',
  21643. ' TObject = class end;',
  21644. ' TItem = TObject;',
  21645. ' TEnumerator = class',
  21646. ' FCurrent: TItem;',
  21647. ' property Current: TItem read FCurrent;',
  21648. ' function MoveNext: boolean;',
  21649. ' end;',
  21650. ' TBird = class',
  21651. ' end;',
  21652. ' TBirdHelper = class helper for TBird',
  21653. ' function GetEnumerator: TEnumerator;',
  21654. ' end;',
  21655. 'function TEnumerator.MoveNext: boolean;',
  21656. 'begin',
  21657. 'end;',
  21658. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  21659. 'begin',
  21660. 'end;',
  21661. 'var',
  21662. ' b: TBird;',
  21663. ' i, i2: TItem;',
  21664. 'begin',
  21665. ' for i in b do i2:=i;']);
  21666. ConvertProgram;
  21667. CheckSource('TestClassHelper_ForIn',
  21668. LinesToStr([ // statements
  21669. 'rtl.createClass($mod, "TObject", null, function () {',
  21670. ' this.$init = function () {',
  21671. ' };',
  21672. ' this.$final = function () {',
  21673. ' };',
  21674. '});',
  21675. 'rtl.createClass($mod, "TEnumerator", $mod.TObject, function () {',
  21676. ' this.$init = function () {',
  21677. ' $mod.TObject.$init.call(this);',
  21678. ' this.FCurrent = null;',
  21679. ' };',
  21680. ' this.$final = function () {',
  21681. ' this.FCurrent = undefined;',
  21682. ' $mod.TObject.$final.call(this);',
  21683. ' };',
  21684. ' this.MoveNext = function () {',
  21685. ' var Result = false;',
  21686. ' return Result;',
  21687. ' };',
  21688. '});',
  21689. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  21690. '});',
  21691. 'rtl.createHelper($mod, "TBirdHelper", null, function () {',
  21692. ' this.GetEnumerator = function () {',
  21693. ' var Result = null;',
  21694. ' return Result;',
  21695. ' };',
  21696. '});',
  21697. 'this.b = null;',
  21698. 'this.i = null;',
  21699. 'this.i2 = null;'
  21700. ]),
  21701. LinesToStr([ // $mod.$main
  21702. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  21703. 'try {',
  21704. ' while ($in.MoveNext()){',
  21705. ' $mod.i = $in.FCurrent;',
  21706. ' $mod.i2 = $mod.i;',
  21707. ' }',
  21708. '} finally {',
  21709. ' $in = rtl.freeLoc($in)',
  21710. '};',
  21711. '']));
  21712. end;
  21713. procedure TTestModule.TestClassHelper_PassProperty;
  21714. begin
  21715. StartProgram(false);
  21716. Add([
  21717. 'type',
  21718. ' TObject = class',
  21719. ' FField: TObject;',
  21720. ' property Field: TObject read FField write FField;',
  21721. ' end;',
  21722. ' THelper = class helper for TObject',
  21723. ' procedure Fly;',
  21724. ' class procedure Run;',
  21725. ' class procedure Jump; static;',
  21726. ' end;',
  21727. 'procedure THelper.Fly;',
  21728. 'begin',
  21729. ' Field.Fly;',
  21730. ' Field.Run;',
  21731. ' Field.Jump;',
  21732. ' with Field do begin',
  21733. ' Fly;',
  21734. ' Run;',
  21735. ' Jump;',
  21736. ' end;',
  21737. 'end;',
  21738. 'class procedure THelper.Run;',
  21739. 'begin',
  21740. 'end;',
  21741. 'class procedure THelper.Jump;',
  21742. 'begin',
  21743. 'end;',
  21744. 'var',
  21745. ' b: TObject;',
  21746. 'begin',
  21747. ' b.Field.Fly;',
  21748. ' b.Field.Run;',
  21749. ' b.Field.Jump;',
  21750. ' with b do begin',
  21751. ' Field.Run;',
  21752. ' Field.Fly;',
  21753. ' Field.Jump;',
  21754. ' end;',
  21755. ' with b.Field do begin',
  21756. ' Run;',
  21757. ' Fly;',
  21758. ' Jump;',
  21759. ' end;',
  21760. '']);
  21761. ConvertProgram;
  21762. CheckSource('TestClassHelper_PassProperty',
  21763. LinesToStr([ // statements
  21764. 'rtl.createClass($mod, "TObject", null, function () {',
  21765. ' this.$init = function () {',
  21766. ' this.FField = null;',
  21767. ' };',
  21768. ' this.$final = function () {',
  21769. ' this.FField = undefined;',
  21770. ' };',
  21771. '});',
  21772. 'rtl.createHelper($mod, "THelper", null, function () {',
  21773. ' this.Fly = function () {',
  21774. ' $mod.THelper.Fly.call(this.FField);',
  21775. ' $mod.THelper.Run.call(this.FField.$class);',
  21776. ' $mod.THelper.Jump();',
  21777. ' var $with = this.FField;',
  21778. ' $mod.THelper.Fly.call($with);',
  21779. ' $mod.THelper.Run.call($with.$class);',
  21780. ' $mod.THelper.Jump();',
  21781. ' };',
  21782. ' this.Run = function () {',
  21783. ' };',
  21784. ' this.Jump = function () {',
  21785. ' };',
  21786. '});',
  21787. 'this.b = null;',
  21788. '']),
  21789. LinesToStr([ // $mod.$main
  21790. '$mod.THelper.Fly.call($mod.b.FField);',
  21791. '$mod.THelper.Run.call($mod.b.FField.$class);',
  21792. '$mod.THelper.Jump();',
  21793. 'var $with = $mod.b;',
  21794. '$mod.THelper.Run.call($with.FField.$class);',
  21795. '$mod.THelper.Fly.call($with.FField);',
  21796. '$mod.THelper.Jump();',
  21797. 'var $with1 = $mod.b.FField;',
  21798. '$mod.THelper.Run.call($with1.$class);',
  21799. '$mod.THelper.Fly.call($with1);',
  21800. '$mod.THelper.Jump();',
  21801. '']));
  21802. end;
  21803. procedure TTestModule.TestExtClassHelper_ClassVar;
  21804. begin
  21805. StartProgram(false);
  21806. Add([
  21807. '{$modeswitch externalclass}',
  21808. 'type',
  21809. ' TExtA = class external name ''ExtObj''',
  21810. ' end;',
  21811. ' THelper = class helper for TExtA',
  21812. ' const',
  21813. ' One = 1;',
  21814. ' Two: word = 2;',
  21815. ' class var',
  21816. ' Glob: word;',
  21817. ' function Foo(w: word): word;',
  21818. ' class function Bar(w: word): word; static;',
  21819. ' end;',
  21820. 'function THelper.foo(w: word): word;',
  21821. 'begin',
  21822. ' Result:=w;',
  21823. ' Two:=One+w;',
  21824. ' Glob:=Glob;',
  21825. ' Result:=Self.Glob;',
  21826. ' Self.Glob:=Self.Glob;',
  21827. ' with Self do Glob:=Glob;',
  21828. 'end;',
  21829. 'class function THelper.bar(w: word): word;',
  21830. 'begin',
  21831. ' Result:=w;',
  21832. ' Two:=One;',
  21833. ' Glob:=Glob;',
  21834. 'end;',
  21835. 'var o: TExtA;',
  21836. 'begin',
  21837. ' texta.two:=texta.one;',
  21838. ' texta.Glob:=texta.Glob;',
  21839. ' with texta do begin',
  21840. ' two:=one;',
  21841. ' Glob:=Glob;',
  21842. ' end;',
  21843. ' o.two:=o.one;',
  21844. ' o.Glob:=o.Glob;',
  21845. ' with o do begin',
  21846. ' two:=one;',
  21847. ' Glob:=Glob;',
  21848. ' end;',
  21849. '']);
  21850. ConvertProgram;
  21851. CheckSource('TestExtClassHelper_ClassVar',
  21852. LinesToStr([ // statements
  21853. 'rtl.createHelper($mod, "THelper", null, function () {',
  21854. ' this.One = 1;',
  21855. ' this.Two = 2;',
  21856. ' this.Glob = 0;',
  21857. ' this.Foo = function (w) {',
  21858. ' var Result = 0;',
  21859. ' Result = w;',
  21860. ' $mod.THelper.Two = 1 + w;',
  21861. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21862. ' Result = $mod.THelper.Glob;',
  21863. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21864. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21865. ' return Result;',
  21866. ' };',
  21867. ' this.Bar = function (w) {',
  21868. ' var Result = 0;',
  21869. ' Result = w;',
  21870. ' $mod.THelper.Two = 1;',
  21871. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21872. ' return Result;',
  21873. ' };',
  21874. '});',
  21875. 'this.o = null;',
  21876. '']),
  21877. LinesToStr([ // $mod.$main
  21878. '$mod.THelper.Two = 1;',
  21879. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21880. '$mod.THelper.Two = 1;',
  21881. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21882. '$mod.THelper.Two = 1;',
  21883. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21884. 'var $with = $mod.o;',
  21885. '$mod.THelper.Two = 1;',
  21886. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21887. '']));
  21888. end;
  21889. procedure TTestModule.TestExtClassHelper_Method_Call;
  21890. begin
  21891. StartProgram(false);
  21892. Add([
  21893. '{$modeswitch externalclass}',
  21894. 'type',
  21895. ' TFly = function(w: word): word of object;',
  21896. ' TExtA = class external name ''ExtObj''',
  21897. ' procedure Run(w: word = 10);',
  21898. ' end;',
  21899. ' THelper = class helper for TExtA',
  21900. ' function Foo(w: word = 1): word;',
  21901. ' function Fly(w: word = 2): word; external name ''Fly'';',
  21902. ' end;',
  21903. 'var p: TFly;',
  21904. 'function THelper.foo(w: word): word;',
  21905. 'begin',
  21906. ' Run;',
  21907. ' Run();',
  21908. ' Run(11);',
  21909. ' Foo;',
  21910. ' Foo();',
  21911. ' Foo(12);',
  21912. ' Self.Foo;',
  21913. ' Self.Foo();',
  21914. ' Self.Foo(13);',
  21915. ' Fly;',
  21916. ' Fly();',
  21917. ' with Self do begin',
  21918. ' Foo;',
  21919. ' Foo();',
  21920. ' Foo(14);',
  21921. ' Fly;',
  21922. ' Fly();',
  21923. ' end;',
  21924. ' p:=@Fly;',
  21925. 'end;',
  21926. 'var Obj: TExtA;',
  21927. 'begin',
  21928. ' obj.Foo;',
  21929. ' obj.Foo();',
  21930. ' obj.Foo(21);',
  21931. ' obj.Fly;',
  21932. ' obj.Fly();',
  21933. ' with obj do begin',
  21934. ' Foo;',
  21935. ' Foo();',
  21936. ' Foo(22);',
  21937. ' Fly;',
  21938. ' Fly();',
  21939. ' end;',
  21940. ' p:[email protected];',
  21941. '']);
  21942. ConvertProgram;
  21943. CheckSource('TestExtClassHelper_Method_Call',
  21944. LinesToStr([ // statements
  21945. 'rtl.createHelper($mod, "THelper", null, function () {',
  21946. ' this.Foo = function (w) {',
  21947. ' var Result = 0;',
  21948. ' this.Run(10);',
  21949. ' this.Run(10);',
  21950. ' this.Run(11);',
  21951. ' $mod.THelper.Foo.call(this, 1);',
  21952. ' $mod.THelper.Foo.call(this, 1);',
  21953. ' $mod.THelper.Foo.call(this, 12);',
  21954. ' $mod.THelper.Foo.call(this, 1);',
  21955. ' $mod.THelper.Foo.call(this, 1);',
  21956. ' $mod.THelper.Foo.call(this, 13);',
  21957. ' this.Fly(2);',
  21958. ' this.Fly(2);',
  21959. ' $mod.THelper.Foo.call(this, 1);',
  21960. ' $mod.THelper.Foo.call(this, 1);',
  21961. ' $mod.THelper.Foo.call(this, 14);',
  21962. ' this.Fly(2);',
  21963. ' this.Fly(2);',
  21964. ' $mod.p = rtl.createCallback(this, "Fly");',
  21965. ' return Result;',
  21966. ' };',
  21967. '});',
  21968. 'this.p = null;',
  21969. 'this.Obj = null;',
  21970. '']),
  21971. LinesToStr([ // $mod.$main
  21972. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21973. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21974. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21975. '$mod.Obj.Fly(2);',
  21976. '$mod.Obj.Fly(2);',
  21977. 'var $with = $mod.Obj;',
  21978. '$mod.THelper.Foo.call($with, 1);',
  21979. '$mod.THelper.Foo.call($with, 1);',
  21980. '$mod.THelper.Foo.call($with, 22);',
  21981. '$with.Fly(2);',
  21982. '$with.Fly(2);',
  21983. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  21984. '']));
  21985. end;
  21986. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  21987. begin
  21988. StartProgram(false);
  21989. Add([
  21990. '{$modeswitch externalclass}',
  21991. 'type',
  21992. ' TExtA = class external name ''ExtObj''',
  21993. ' procedure Run(w: word = 10);',
  21994. ' end;',
  21995. ' THelper = class helper for TExtA',
  21996. ' class procedure Fly;',
  21997. ' end;',
  21998. 'class procedure THelper.Fly;',
  21999. 'begin end;',
  22000. 'begin',
  22001. '']);
  22002. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22003. nHelperClassMethodForExtClassMustBeStatic);
  22004. ConvertProgram;
  22005. end;
  22006. procedure TTestModule.TestRecordHelper_ClassVar;
  22007. begin
  22008. StartProgram(false);
  22009. Add([
  22010. 'type',
  22011. ' TRec = record',
  22012. ' end;',
  22013. ' THelper = record helper for TRec',
  22014. ' const',
  22015. ' One = 1;',
  22016. ' Two: word = 2;',
  22017. ' class var',
  22018. ' Glob: word;',
  22019. ' function Foo(w: word): word;',
  22020. ' class function Bar(w: word): word; static;',
  22021. ' end;',
  22022. 'function THelper.foo(w: word): word;',
  22023. 'begin',
  22024. ' Result:=w;',
  22025. ' Two:=One+w;',
  22026. ' Glob:=Glob;',
  22027. ' Result:=Self.Glob;',
  22028. ' Self.Glob:=Self.Glob;',
  22029. ' with Self do Glob:=Glob;',
  22030. ' Self:=Self;',
  22031. 'end;',
  22032. 'class function THelper.bar(w: word): word;',
  22033. 'begin',
  22034. ' Result:=w;',
  22035. ' Two:=One;',
  22036. ' Glob:=Glob;',
  22037. 'end;',
  22038. 'var r: TRec;',
  22039. 'begin',
  22040. ' trec.two:=trec.one;',
  22041. ' trec.Glob:=trec.Glob;',
  22042. ' with trec do begin',
  22043. ' two:=one;',
  22044. ' Glob:=Glob;',
  22045. ' end;',
  22046. ' r.two:=r.one;',
  22047. ' r.Glob:=r.Glob;',
  22048. ' with r do begin',
  22049. ' two:=one;',
  22050. ' Glob:=Glob;',
  22051. ' end;',
  22052. '']);
  22053. ConvertProgram;
  22054. CheckSource('TestRecordHelper_ClassVar',
  22055. LinesToStr([ // statements
  22056. 'rtl.recNewT($mod, "TRec", function () {',
  22057. ' this.$eq = function (b) {',
  22058. ' return true;',
  22059. ' };',
  22060. ' this.$assign = function (s) {',
  22061. ' return this;',
  22062. ' };',
  22063. '});',
  22064. 'rtl.createHelper($mod, "THelper", null, function () {',
  22065. ' this.One = 1;',
  22066. ' this.Two = 2;',
  22067. ' this.Glob = 0;',
  22068. ' this.Foo = function (w) {',
  22069. ' var Result = 0;',
  22070. ' Result = w;',
  22071. ' $mod.THelper.Two = 1 + w;',
  22072. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22073. ' Result = $mod.THelper.Glob;',
  22074. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22075. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22076. ' this.$assign(this);',
  22077. ' return Result;',
  22078. ' };',
  22079. ' this.Bar = function (w) {',
  22080. ' var Result = 0;',
  22081. ' Result = w;',
  22082. ' $mod.THelper.Two = 1;',
  22083. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22084. ' return Result;',
  22085. ' };',
  22086. '});',
  22087. 'this.r = $mod.TRec.$new();',
  22088. '']),
  22089. LinesToStr([ // $mod.$main
  22090. '$mod.THelper.Two = 1;',
  22091. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22092. 'var $with = $mod.TRec;',
  22093. '$mod.THelper.Two = 1;',
  22094. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22095. '$mod.THelper.Two = 1;',
  22096. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22097. 'var $with1 = $mod.r;',
  22098. '$mod.THelper.Two = 1;',
  22099. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22100. '']));
  22101. end;
  22102. procedure TTestModule.TestRecordHelper_Method_Call;
  22103. begin
  22104. StartProgram(false);
  22105. Add([
  22106. '{$modeswitch AdvancedRecords}',
  22107. 'type',
  22108. ' TRec = record',
  22109. ' procedure Run(w: word = 10);',
  22110. ' end;',
  22111. ' THelper = record helper for TRec',
  22112. ' function Foo(w: word = 1): word;',
  22113. ' end;',
  22114. 'procedure TRec.Run(w: word);',
  22115. 'begin',
  22116. ' Foo;',
  22117. ' Foo();',
  22118. ' Foo(2);',
  22119. ' Self.Foo;',
  22120. ' Self.Foo();',
  22121. ' Self.Foo(3);',
  22122. ' with Self do begin',
  22123. ' Foo;',
  22124. ' Foo();',
  22125. ' Foo(4);',
  22126. ' end;',
  22127. 'end;',
  22128. 'function THelper.foo(w: word): word;',
  22129. 'begin',
  22130. ' Run;',
  22131. ' Run();',
  22132. ' Run(11);',
  22133. ' Foo;',
  22134. ' Foo();',
  22135. ' Foo(12);',
  22136. ' Self.Foo;',
  22137. ' Self.Foo();',
  22138. ' Self.Foo(13);',
  22139. ' with Self do begin',
  22140. ' Foo;',
  22141. ' Foo();',
  22142. ' Foo(14);',
  22143. ' end;',
  22144. 'end;',
  22145. 'var Rec: TRec;',
  22146. 'begin',
  22147. ' Rec.Foo;',
  22148. ' Rec.Foo();',
  22149. ' Rec.Foo(21);',
  22150. ' with Rec do begin',
  22151. ' Foo;',
  22152. ' Foo();',
  22153. ' Foo(22);',
  22154. ' end;',
  22155. '']);
  22156. ConvertProgram;
  22157. CheckSource('TestRecordHelper_Method_Call',
  22158. LinesToStr([ // statements
  22159. 'rtl.recNewT($mod, "TRec", function () {',
  22160. ' this.$eq = function (b) {',
  22161. ' return true;',
  22162. ' };',
  22163. ' this.$assign = function (s) {',
  22164. ' return this;',
  22165. ' };',
  22166. ' this.Run = function (w) {',
  22167. ' $mod.THelper.Foo.call(this, 1);',
  22168. ' $mod.THelper.Foo.call(this, 1);',
  22169. ' $mod.THelper.Foo.call(this, 2);',
  22170. ' $mod.THelper.Foo.call(this, 1);',
  22171. ' $mod.THelper.Foo.call(this, 1);',
  22172. ' $mod.THelper.Foo.call(this, 3);',
  22173. ' $mod.THelper.Foo.call(this, 1);',
  22174. ' $mod.THelper.Foo.call(this, 1);',
  22175. ' $mod.THelper.Foo.call(this, 4);',
  22176. ' };',
  22177. '});',
  22178. 'rtl.createHelper($mod, "THelper", null, function () {',
  22179. ' this.Foo = function (w) {',
  22180. ' var Result = 0;',
  22181. ' this.Run(10);',
  22182. ' this.Run(10);',
  22183. ' this.Run(11);',
  22184. ' $mod.THelper.Foo.call(this, 1);',
  22185. ' $mod.THelper.Foo.call(this, 1);',
  22186. ' $mod.THelper.Foo.call(this, 12);',
  22187. ' $mod.THelper.Foo.call(this, 1);',
  22188. ' $mod.THelper.Foo.call(this, 1);',
  22189. ' $mod.THelper.Foo.call(this, 13);',
  22190. ' $mod.THelper.Foo.call(this, 1);',
  22191. ' $mod.THelper.Foo.call(this, 1);',
  22192. ' $mod.THelper.Foo.call(this, 14);',
  22193. ' return Result;',
  22194. ' };',
  22195. '});',
  22196. 'this.Rec = $mod.TRec.$new();',
  22197. '']),
  22198. LinesToStr([ // $mod.$main
  22199. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22200. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22201. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22202. 'var $with = $mod.Rec;',
  22203. '$mod.THelper.Foo.call($with, 1);',
  22204. '$mod.THelper.Foo.call($with, 1);',
  22205. '$mod.THelper.Foo.call($with, 22);',
  22206. '']));
  22207. end;
  22208. procedure TTestModule.TestRecordHelper_Constructor;
  22209. begin
  22210. StartProgram(false);
  22211. Add([
  22212. '{$modeswitch AdvancedRecords}',
  22213. 'type',
  22214. ' TRec = record',
  22215. ' constructor Create(w: word);',
  22216. ' end;',
  22217. ' THelper = record helper for TRec',
  22218. ' constructor NewHlp(w: word);',
  22219. ' end;',
  22220. 'var',
  22221. ' Rec: TRec;',
  22222. 'constructor TRec.Create(w: word);',
  22223. 'begin',
  22224. ' NewHlp(2);', // normal call
  22225. ' trec.NewHlp(3);', // new instance
  22226. 'end;',
  22227. 'constructor THelper.NewHlp(w: word);',
  22228. 'begin',
  22229. ' create(2);', // normal call
  22230. ' trec.create(3);', // new instance
  22231. ' NewHlp(4);', // normal call
  22232. ' trec.NewHlp(5);', // new instance
  22233. 'end;',
  22234. 'begin',
  22235. ' rec.newhlp(2);', // normal call
  22236. ' with rec do newhlp(12);', // normal call
  22237. ' trec.newhlp(3);', // new instance
  22238. ' with trec do newhlp(13);', // new instance
  22239. '']);
  22240. ConvertProgram;
  22241. CheckSource('TestRecordHelper_Constructor',
  22242. LinesToStr([ // statements
  22243. 'rtl.recNewT($mod, "TRec", function () {',
  22244. ' this.$eq = function (b) {',
  22245. ' return true;',
  22246. ' };',
  22247. ' this.$assign = function (s) {',
  22248. ' return this;',
  22249. ' };',
  22250. ' this.Create = function (w) {',
  22251. ' $mod.THelper.NewHlp.call(this, 2);',
  22252. ' $mod.THelper.$new("NewHlp", [3]);',
  22253. ' return this;',
  22254. ' };',
  22255. '}, true);',
  22256. 'rtl.createHelper($mod, "THelper", null, function () {',
  22257. ' this.NewHlp = function (w) {',
  22258. ' this.Create(2);',
  22259. ' $mod.TRec.$new().Create(3);',
  22260. ' $mod.THelper.NewHlp.call(this, 4);',
  22261. ' $mod.THelper.$new("NewHlp", [5]);',
  22262. ' return this;',
  22263. ' };',
  22264. ' this.$new = function (fn, args) {',
  22265. ' return this[fn].apply($mod.TRec.$new(), args);',
  22266. ' };',
  22267. '});',
  22268. 'this.Rec = $mod.TRec.$new();',
  22269. '']),
  22270. LinesToStr([ // $mod.$main
  22271. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22272. 'var $with = $mod.Rec;',
  22273. '$mod.THelper.NewHlp.call($with, 12);',
  22274. '$mod.THelper.$new("NewHlp", [3]);',
  22275. 'var $with1 = $mod.TRec;',
  22276. '$mod.THelper.$new("NewHlp", [13]);',
  22277. '']));
  22278. end;
  22279. procedure TTestModule.TestTypeHelper_ClassVar;
  22280. begin
  22281. StartProgram(false);
  22282. Add([
  22283. '{$modeswitch typehelpers}',
  22284. 'type',
  22285. ' THelper = type helper for byte',
  22286. ' const',
  22287. ' One = 1;',
  22288. ' Two: word = 2;',
  22289. ' class var',
  22290. ' Glob: word;',
  22291. ' function Foo(w: word): word;',
  22292. ' class function Bar(w: word): word; static;',
  22293. ' end;',
  22294. 'function THelper.foo(w: word): word;',
  22295. 'begin',
  22296. ' Result:=w;',
  22297. ' Two:=One+w;',
  22298. ' Glob:=Glob;',
  22299. ' Result:=Self.Glob;',
  22300. ' Self.Glob:=Self.Glob;',
  22301. ' with Self do Glob:=Glob;',
  22302. 'end;',
  22303. 'class function THelper.bar(w: word): word;',
  22304. 'begin',
  22305. ' Result:=w;',
  22306. ' Two:=One;',
  22307. ' Glob:=Glob;',
  22308. 'end;',
  22309. 'var b: byte;',
  22310. 'begin',
  22311. ' byte.two:=byte.one;',
  22312. ' byte.Glob:=byte.Glob;',
  22313. ' with byte do begin',
  22314. ' two:=one;',
  22315. ' Glob:=Glob;',
  22316. ' end;',
  22317. ' b.two:=b.one;',
  22318. ' b.Glob:=b.Glob;',
  22319. ' with b do begin',
  22320. ' two:=one;',
  22321. ' Glob:=Glob;',
  22322. ' end;',
  22323. '']);
  22324. ConvertProgram;
  22325. CheckSource('TestTypeHelper_ClassVar',
  22326. LinesToStr([ // statements
  22327. 'rtl.createHelper($mod, "THelper", null, function () {',
  22328. ' this.One = 1;',
  22329. ' this.Two = 2;',
  22330. ' this.Glob = 0;',
  22331. ' this.Foo = function (w) {',
  22332. ' var Result = 0;',
  22333. ' Result = w;',
  22334. ' $mod.THelper.Two = 1 + w;',
  22335. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22336. ' Result = $mod.THelper.Glob;',
  22337. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22338. ' var $with = this.get();',
  22339. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22340. ' return Result;',
  22341. ' };',
  22342. ' this.Bar = function (w) {',
  22343. ' var Result = 0;',
  22344. ' Result = w;',
  22345. ' $mod.THelper.Two = 1;',
  22346. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22347. ' return Result;',
  22348. ' };',
  22349. '});',
  22350. 'this.b = 0;',
  22351. '']),
  22352. LinesToStr([ // $mod.$main
  22353. '$mod.THelper.Two = 1;',
  22354. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22355. '$mod.THelper.Two = 1;',
  22356. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22357. '$mod.THelper.Two = 1;',
  22358. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22359. 'var $with = $mod.b;',
  22360. '$mod.THelper.Two = 1;',
  22361. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22362. '']));
  22363. end;
  22364. procedure TTestModule.TestTypeHelper_PassResultElement;
  22365. begin
  22366. StartProgram(false);
  22367. Add([
  22368. '{$modeswitch typehelpers}',
  22369. 'type',
  22370. ' THelper = type helper for word',
  22371. ' procedure DoIt(e: byte = 123);',
  22372. ' class procedure DoSome(e: byte = 456); static;',
  22373. ' end;',
  22374. 'procedure THelper.DoIt(e: byte);',
  22375. 'begin',
  22376. 'end;',
  22377. 'class procedure THelper.DoSome(e: byte);',
  22378. 'begin',
  22379. 'end;',
  22380. 'function Foo(w: word): word;',
  22381. 'begin',
  22382. ' Result.DoIt;',
  22383. ' Result.DoIt();',
  22384. ' Result.DoSome;',
  22385. ' Result.DoSome();',
  22386. ' with Result do begin',
  22387. ' DoIt;',
  22388. ' DoIt();',
  22389. ' DoSome;',
  22390. ' DoSome();',
  22391. ' end;',
  22392. 'end;',
  22393. 'begin',
  22394. '']);
  22395. ConvertProgram;
  22396. CheckSource('TestTypeHelper_PassResultElement',
  22397. LinesToStr([ // statements
  22398. 'rtl.createHelper($mod, "THelper", null, function () {',
  22399. ' this.DoIt = function (e) {',
  22400. ' };',
  22401. ' this.DoSome = function (e) {',
  22402. ' };',
  22403. '});',
  22404. 'this.Foo = function (w) {',
  22405. ' var Result = 0;',
  22406. ' $mod.THelper.DoIt.call({',
  22407. ' get: function () {',
  22408. ' return Result;',
  22409. ' },',
  22410. ' set: function (v) {',
  22411. ' Result = v;',
  22412. ' }',
  22413. ' }, 123);',
  22414. ' $mod.THelper.DoIt.call({',
  22415. ' get: function () {',
  22416. ' return Result;',
  22417. ' },',
  22418. ' set: function (v) {',
  22419. ' Result = v;',
  22420. ' }',
  22421. ' }, 123);',
  22422. ' $mod.THelper.DoSome(456);',
  22423. ' $mod.THelper.DoSome(456);',
  22424. ' $mod.THelper.DoIt.call({',
  22425. ' get: function () {',
  22426. ' return Result;',
  22427. ' },',
  22428. ' set: function (v) {',
  22429. ' Result = v;',
  22430. ' }',
  22431. ' }, 123);',
  22432. ' $mod.THelper.DoIt.call({',
  22433. ' get: function () {',
  22434. ' return Result;',
  22435. ' },',
  22436. ' set: function (v) {',
  22437. ' Result = v;',
  22438. ' }',
  22439. ' }, 123);',
  22440. ' $mod.THelper.DoSome(456);',
  22441. ' $mod.THelper.DoSome(456);',
  22442. ' return Result;',
  22443. '};',
  22444. '']),
  22445. LinesToStr([ // $mod.$main
  22446. '']));
  22447. end;
  22448. procedure TTestModule.TestTypeHelper_PassArgs;
  22449. begin
  22450. StartProgram(false);
  22451. Add([
  22452. '{$modeswitch typehelpers}',
  22453. 'type',
  22454. ' THelper = type helper for word',
  22455. ' procedure DoIt(e: byte = 123);',
  22456. ' end;',
  22457. 'procedure THelper.DoIt(e: byte);',
  22458. 'begin',
  22459. 'end;',
  22460. 'procedure FooDefault(a: word);',
  22461. 'begin',
  22462. ' a.DoIt;',
  22463. ' with a do DoIt;',
  22464. 'end;',
  22465. 'procedure FooConst(const a: word);',
  22466. 'begin',
  22467. ' a.DoIt;',
  22468. ' with a do DoIt;',
  22469. 'end;',
  22470. 'procedure FooVar(var a: word);',
  22471. 'begin',
  22472. ' a.DoIt;',
  22473. ' with a do DoIt;',
  22474. 'end;',
  22475. 'begin',
  22476. '']);
  22477. ConvertProgram;
  22478. CheckSource('TestTypeHelper_PassArgs',
  22479. LinesToStr([ // statements
  22480. 'rtl.createHelper($mod, "THelper", null, function () {',
  22481. ' this.DoIt = function (e) {',
  22482. ' };',
  22483. '});',
  22484. 'this.FooDefault = function (a) {',
  22485. ' $mod.THelper.DoIt.call({',
  22486. ' get: function () {',
  22487. ' return a;',
  22488. ' },',
  22489. ' set: function (v) {',
  22490. ' a = v;',
  22491. ' }',
  22492. ' }, 123);',
  22493. ' $mod.THelper.DoIt.call({',
  22494. ' get: function () {',
  22495. ' return a;',
  22496. ' },',
  22497. ' set: function (v) {',
  22498. ' a = v;',
  22499. ' }',
  22500. ' }, 123);',
  22501. '};',
  22502. 'this.FooConst = function (a) {',
  22503. ' $mod.THelper.DoIt.call({',
  22504. ' get: function () {',
  22505. ' return a;',
  22506. ' },',
  22507. ' set: function (v) {',
  22508. ' rtl.raiseE("EPropReadOnly");',
  22509. ' }',
  22510. ' }, 123);',
  22511. ' $mod.THelper.DoIt.call({',
  22512. ' get: function () {',
  22513. ' return a;',
  22514. ' },',
  22515. ' set: function () {',
  22516. ' rtl.raiseE("EPropReadOnly");',
  22517. ' }',
  22518. ' }, 123);',
  22519. '};',
  22520. 'this.FooVar = function (a) {',
  22521. ' $mod.THelper.DoIt.call(a, 123);',
  22522. ' var $with = a.get();',
  22523. ' $mod.THelper.DoIt.call(a, 123);',
  22524. '};',
  22525. '']),
  22526. LinesToStr([ // $mod.$main
  22527. '']));
  22528. end;
  22529. procedure TTestModule.TestTypeHelper_PassVarConst;
  22530. begin
  22531. StartProgram(false);
  22532. Add([
  22533. '{$modeswitch typehelpers}',
  22534. 'type',
  22535. ' THelper = type helper for word',
  22536. ' procedure DoIt(e: byte = 123);',
  22537. ' end;',
  22538. 'procedure THelper.DoIt(e: byte);',
  22539. 'begin',
  22540. 'end;',
  22541. 'var a: word;',
  22542. 'const c: word = 2;',
  22543. '{$writeableconst off}',
  22544. 'const r: word = 3;',
  22545. 'begin',
  22546. ' a.DoIt;',
  22547. ' with a do DoIt;',
  22548. ' c.DoIt;',
  22549. ' with c do DoIt;',
  22550. ' r.DoIt;',
  22551. ' with r do DoIt;',
  22552. '']);
  22553. ConvertProgram;
  22554. CheckSource('TestTypeHelper_PassVarConst',
  22555. LinesToStr([ // statements
  22556. 'rtl.createHelper($mod, "THelper", null, function () {',
  22557. ' this.DoIt = function (e) {',
  22558. ' };',
  22559. '});',
  22560. 'this.a = 0;',
  22561. 'this.c = 2;',
  22562. 'this.r = 3;',
  22563. '']),
  22564. LinesToStr([ // $mod.$main
  22565. '$mod.THelper.DoIt.call({',
  22566. ' p: $mod,',
  22567. ' get: function () {',
  22568. ' return this.p.a;',
  22569. ' },',
  22570. ' set: function (v) {',
  22571. ' this.p.a = v;',
  22572. ' }',
  22573. '}, 123);',
  22574. 'var $with = $mod.a;',
  22575. '$mod.THelper.DoIt.call({',
  22576. ' get: function () {',
  22577. ' return $with;',
  22578. ' },',
  22579. ' set: function (v) {',
  22580. ' $with = v;',
  22581. ' }',
  22582. '}, 123);',
  22583. '$mod.THelper.DoIt.call({',
  22584. ' p: $mod,',
  22585. ' get: function () {',
  22586. ' return this.p.c;',
  22587. ' },',
  22588. ' set: function (v) {',
  22589. ' this.p.c = v;',
  22590. ' }',
  22591. '}, 123);',
  22592. 'var $with1 = $mod.c;',
  22593. '$mod.THelper.DoIt.call({',
  22594. ' get: function () {',
  22595. ' return $with1;',
  22596. ' },',
  22597. ' set: function (v) {',
  22598. ' $with1 = v;',
  22599. ' }',
  22600. '}, 123);',
  22601. '$mod.THelper.DoIt.call({',
  22602. ' get: function () {',
  22603. ' return 3;',
  22604. ' },',
  22605. ' set: function (v) {',
  22606. ' rtl.raiseE("EPropReadOnly");',
  22607. ' }',
  22608. '}, 123);',
  22609. 'var $with2 = 3;',
  22610. ' $mod.THelper.DoIt.call({',
  22611. ' get: function () {',
  22612. ' return $with2;',
  22613. ' },',
  22614. ' set: function () {',
  22615. ' rtl.raiseE("EPropReadOnly");',
  22616. ' }',
  22617. ' }, 123);',
  22618. '']));
  22619. end;
  22620. procedure TTestModule.TestTypeHelper_PassFuncResult;
  22621. begin
  22622. StartProgram(false);
  22623. Add([
  22624. '{$modeswitch typehelpers}',
  22625. 'type',
  22626. ' THelper = type helper for word',
  22627. ' procedure DoIt(e: byte = 123);',
  22628. ' end;',
  22629. 'procedure THelper.DoIt(e: byte);',
  22630. 'begin',
  22631. 'end;',
  22632. 'function Foo(b: byte = 1): word;',
  22633. 'begin',
  22634. 'end;',
  22635. 'begin',
  22636. ' Foo.DoIt;',
  22637. ' Foo().DoIt;',
  22638. ' with Foo do DoIt;',
  22639. ' with Foo() do DoIt;',
  22640. '']);
  22641. ConvertProgram;
  22642. CheckSource('TestTypeHelper_PassFuncResult',
  22643. LinesToStr([ // statements
  22644. 'rtl.createHelper($mod, "THelper", null, function () {',
  22645. ' this.DoIt = function (e) {',
  22646. ' };',
  22647. '});',
  22648. 'this.Foo = function (b) {',
  22649. ' var Result = 0;',
  22650. ' return Result;',
  22651. '};',
  22652. '']),
  22653. LinesToStr([ // $mod.$main
  22654. '$mod.THelper.DoIt.call({',
  22655. ' a: $mod.Foo(1),',
  22656. ' get: function () {',
  22657. ' return this.a;',
  22658. ' },',
  22659. ' set: function (v) {',
  22660. ' this.a = v;',
  22661. ' }',
  22662. '}, 123);',
  22663. '$mod.THelper.DoIt.call({',
  22664. ' a: $mod.Foo(1),',
  22665. ' get: function () {',
  22666. ' return this.a;',
  22667. ' },',
  22668. ' set: function (v) {',
  22669. ' this.a = v;',
  22670. ' }',
  22671. '}, 123);',
  22672. 'var $with = $mod.Foo(1);',
  22673. '$mod.THelper.DoIt.call({',
  22674. ' get: function () {',
  22675. ' return $with;',
  22676. ' },',
  22677. ' set: function (v) {',
  22678. ' $with = v;',
  22679. ' }',
  22680. '}, 123);',
  22681. 'var $with1 = $mod.Foo(1);',
  22682. '$mod.THelper.DoIt.call({',
  22683. ' get: function () {',
  22684. ' return $with1;',
  22685. ' },',
  22686. ' set: function (v) {',
  22687. ' $with1 = v;',
  22688. ' }',
  22689. '}, 123);',
  22690. '']));
  22691. end;
  22692. procedure TTestModule.TestTypeHelper_PassPropertyField;
  22693. begin
  22694. StartProgram(false);
  22695. Add([
  22696. '{$modeswitch typehelpers}',
  22697. 'type',
  22698. ' TObject = class',
  22699. ' FField: word;',
  22700. ' procedure SetField(Value: word);',
  22701. ' property Field: word read FField write SetField;',
  22702. ' end;',
  22703. ' THelper = type helper for word',
  22704. ' procedure Fly;',
  22705. ' class procedure Run; static;',
  22706. ' end;',
  22707. 'procedure TObject.SetField(Value: word);',
  22708. 'begin',
  22709. ' Field.Fly;',
  22710. ' Field.Run;',
  22711. ' Self.Field.Fly;',
  22712. ' Self.Field.Run;',
  22713. ' with Self do begin',
  22714. ' Field.Fly;',
  22715. ' Field.Run;',
  22716. ' end;',
  22717. ' with Self.Field do begin',
  22718. ' Fly;',
  22719. ' Run;',
  22720. ' end;',
  22721. 'end;',
  22722. 'procedure THelper.Fly;',
  22723. 'begin',
  22724. 'end;',
  22725. 'class procedure THelper.Run;',
  22726. 'begin',
  22727. 'end;',
  22728. 'var',
  22729. ' o: TObject;',
  22730. 'begin',
  22731. ' o.Field.Fly;',
  22732. ' o.Field.Run;',
  22733. ' with o do begin',
  22734. ' Field.Fly;',
  22735. ' Field.Run;',
  22736. ' end;',
  22737. ' with o.Field do begin',
  22738. ' Fly;',
  22739. ' Run;',
  22740. ' end;',
  22741. '']);
  22742. ConvertProgram;
  22743. CheckSource('TestTypeHelper_PassPropertyField',
  22744. LinesToStr([ // statements
  22745. 'rtl.createClass($mod, "TObject", null, function () {',
  22746. ' this.$init = function () {',
  22747. ' this.FField = 0;',
  22748. ' };',
  22749. ' this.$final = function () {',
  22750. ' };',
  22751. ' this.SetField = function (Value) {',
  22752. ' $mod.THelper.Fly.call({',
  22753. ' p: this,',
  22754. ' get: function () {',
  22755. ' return this.p.FField;',
  22756. ' },',
  22757. ' set: function (v) {',
  22758. ' this.p.FField = v;',
  22759. ' }',
  22760. ' });',
  22761. ' $mod.THelper.Run();',
  22762. ' $mod.THelper.Fly.call({',
  22763. ' p: this,',
  22764. ' get: function () {',
  22765. ' return this.p.FField;',
  22766. ' },',
  22767. ' set: function (v) {',
  22768. ' this.p.FField = v;',
  22769. ' }',
  22770. ' });',
  22771. ' $mod.THelper.Run();',
  22772. ' $mod.THelper.Fly.call({',
  22773. ' p: this,',
  22774. ' get: function () {',
  22775. ' return this.p.FField;',
  22776. ' },',
  22777. ' set: function (v) {',
  22778. ' this.p.FField = v;',
  22779. ' }',
  22780. ' });',
  22781. ' $mod.THelper.Run();',
  22782. ' var $with = this.FField;',
  22783. ' $mod.THelper.Fly.call({',
  22784. ' get: function () {',
  22785. ' return $with;',
  22786. ' },',
  22787. ' set: function (v) {',
  22788. ' $with = v;',
  22789. ' }',
  22790. ' });',
  22791. ' $mod.THelper.Run();',
  22792. ' };',
  22793. '});',
  22794. 'rtl.createHelper($mod, "THelper", null, function () {',
  22795. ' this.Fly = function () {',
  22796. ' };',
  22797. ' this.Run = function () {',
  22798. ' };',
  22799. '});',
  22800. 'this.o = null;',
  22801. '']),
  22802. LinesToStr([ // $mod.$main
  22803. '$mod.THelper.Fly.call({',
  22804. ' p: $mod.o,',
  22805. ' get: function () {',
  22806. ' return this.p.FField;',
  22807. ' },',
  22808. ' set: function (v) {',
  22809. ' this.p.FField = v;',
  22810. ' }',
  22811. '});',
  22812. '$mod.THelper.Run();',
  22813. 'var $with = $mod.o;',
  22814. '$mod.THelper.Fly.call({',
  22815. ' p: $with,',
  22816. ' get: function () {',
  22817. ' return this.p.FField;',
  22818. ' },',
  22819. ' set: function (v) {',
  22820. ' this.p.FField = v;',
  22821. ' }',
  22822. '});',
  22823. '$mod.THelper.Run();',
  22824. 'var $with1 = $mod.o.FField;',
  22825. '$mod.THelper.Fly.call({',
  22826. ' get: function () {',
  22827. ' return $with1;',
  22828. ' },',
  22829. ' set: function (v) {',
  22830. ' $with1 = v;',
  22831. ' }',
  22832. '});',
  22833. '$mod.THelper.Run();',
  22834. '']));
  22835. end;
  22836. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  22837. begin
  22838. StartProgram(false);
  22839. Add([
  22840. '{$modeswitch typehelpers}',
  22841. 'type',
  22842. ' TObject = class',
  22843. ' FField: word;',
  22844. ' function GetField: word;',
  22845. ' property Field: word read GetField write FField;',
  22846. ' end;',
  22847. ' THelper = type helper for word',
  22848. ' procedure Fly;',
  22849. ' class procedure Run; static;',
  22850. ' end;',
  22851. 'function TObject.GetField: word;',
  22852. 'begin',
  22853. ' Field.Fly;',
  22854. ' Field.Run;',
  22855. ' Self.Field.Fly;',
  22856. ' Self.Field.Run;',
  22857. ' with Self do begin',
  22858. ' Field.Fly;',
  22859. ' Field.Run;',
  22860. ' end;',
  22861. ' with Self.Field do begin',
  22862. ' Fly;',
  22863. ' Run;',
  22864. ' end;',
  22865. 'end;',
  22866. 'procedure THelper.Fly;',
  22867. 'begin',
  22868. 'end;',
  22869. 'class procedure THelper.Run;',
  22870. 'begin',
  22871. 'end;',
  22872. 'var',
  22873. ' o: TObject;',
  22874. 'begin',
  22875. ' o.Field.Fly;',
  22876. ' o.Field.Run;',
  22877. ' with o do begin',
  22878. ' Field.Fly;',
  22879. ' Field.Run;',
  22880. ' end;',
  22881. ' with o.Field do begin',
  22882. ' Fly;',
  22883. ' Run;',
  22884. ' end;',
  22885. '']);
  22886. ConvertProgram;
  22887. CheckSource('TestTypeHelper_PassPropertyGetter',
  22888. LinesToStr([ // statements
  22889. 'rtl.createClass($mod, "TObject", null, function () {',
  22890. ' this.$init = function () {',
  22891. ' this.FField = 0;',
  22892. ' };',
  22893. ' this.$final = function () {',
  22894. ' };',
  22895. ' this.GetField = function () {',
  22896. ' var Result = 0;',
  22897. ' $mod.THelper.Fly.call({',
  22898. ' p: this.GetField(),',
  22899. ' get: function () {',
  22900. ' return this.p;',
  22901. ' },',
  22902. ' set: function (v) {',
  22903. ' this.p = v;',
  22904. ' }',
  22905. ' });',
  22906. ' $mod.THelper.Run();',
  22907. ' $mod.THelper.Fly.call({',
  22908. ' p: this.GetField(),',
  22909. ' get: function () {',
  22910. ' return this.p;',
  22911. ' },',
  22912. ' set: function (v) {',
  22913. ' this.p = v;',
  22914. ' }',
  22915. ' });',
  22916. ' $mod.THelper.Run();',
  22917. ' $mod.THelper.Fly.call({',
  22918. ' p: this.GetField(),',
  22919. ' get: function () {',
  22920. ' return this.p;',
  22921. ' },',
  22922. ' set: function (v) {',
  22923. ' this.p = v;',
  22924. ' }',
  22925. ' });',
  22926. ' $mod.THelper.Run();',
  22927. ' var $with = this.GetField();',
  22928. ' $mod.THelper.Fly.call({',
  22929. ' get: function () {',
  22930. ' return $with;',
  22931. ' },',
  22932. ' set: function (v) {',
  22933. ' $with = v;',
  22934. ' }',
  22935. ' });',
  22936. ' $mod.THelper.Run();',
  22937. ' return Result;',
  22938. ' };',
  22939. '});',
  22940. 'rtl.createHelper($mod, "THelper", null, function () {',
  22941. ' this.Fly = function () {',
  22942. ' };',
  22943. ' this.Run = function () {',
  22944. ' };',
  22945. '});',
  22946. 'this.o = null;',
  22947. '']),
  22948. LinesToStr([ // $mod.$main
  22949. '$mod.THelper.Fly.call({',
  22950. ' p: $mod.o.GetField(),',
  22951. ' get: function () {',
  22952. ' return this.p;',
  22953. ' },',
  22954. ' set: function (v) {',
  22955. ' this.p = v;',
  22956. ' }',
  22957. '});',
  22958. '$mod.THelper.Run();',
  22959. 'var $with = $mod.o;',
  22960. '$mod.THelper.Fly.call({',
  22961. ' p: $with.GetField(),',
  22962. ' get: function () {',
  22963. ' return this.p;',
  22964. ' },',
  22965. ' set: function (v) {',
  22966. ' this.p = v;',
  22967. ' }',
  22968. '});',
  22969. '$mod.THelper.Run();',
  22970. 'var $with1 = $mod.o.GetField();',
  22971. '$mod.THelper.Fly.call({',
  22972. ' get: function () {',
  22973. ' return $with1;',
  22974. ' },',
  22975. ' set: function (v) {',
  22976. ' $with1 = v;',
  22977. ' }',
  22978. '});',
  22979. '$mod.THelper.Run();',
  22980. '']));
  22981. end;
  22982. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  22983. begin
  22984. StartProgram(false);
  22985. Add([
  22986. '{$modeswitch typehelpers}',
  22987. 'type',
  22988. ' TObject = class',
  22989. ' class var FField: word;',
  22990. ' class procedure SetField(Value: word);',
  22991. ' class property Field: word read FField write SetField;',
  22992. ' end;',
  22993. ' THelper = type helper for word',
  22994. ' procedure Fly(n: byte);',
  22995. ' end;',
  22996. 'class procedure TObject.SetField(Value: word);',
  22997. 'begin',
  22998. ' Field.Fly(1);',
  22999. ' Self.Field.Fly(2);',
  23000. ' with Self do Field.Fly(3);',
  23001. ' with Self.Field do Fly(4);',
  23002. ' TObject.Field.Fly(5);',
  23003. ' with TObject do Field.Fly(6);',
  23004. ' with TObject.Field do Fly(7);',
  23005. 'end;',
  23006. 'procedure THelper.Fly(n: byte);',
  23007. 'begin',
  23008. 'end;',
  23009. 'var',
  23010. ' o: TObject;',
  23011. 'begin',
  23012. ' o.Field.Fly(11);',
  23013. ' with o do Field.Fly(12);',
  23014. ' with o.Field do Fly(13);',
  23015. ' TObject.Field.Fly(14);',
  23016. ' with TObject do Field.Fly(15);',
  23017. ' with TObject.Field do Fly(16);',
  23018. '']);
  23019. ConvertProgram;
  23020. CheckSource('TestTypeHelper_PassClassPropertyField',
  23021. LinesToStr([ // statements
  23022. 'rtl.createClass($mod, "TObject", null, function () {',
  23023. ' this.FField = 0;',
  23024. ' this.$init = function () {',
  23025. ' };',
  23026. ' this.$final = function () {',
  23027. ' };',
  23028. ' this.SetField = function (Value) {',
  23029. ' $mod.THelper.Fly.call({',
  23030. ' p: this,',
  23031. ' get: function () {',
  23032. ' return this.p.FField;',
  23033. ' },',
  23034. ' set: function (v) {',
  23035. ' $mod.TObject.FField = v;',
  23036. ' }',
  23037. ' }, 1);',
  23038. ' $mod.THelper.Fly.call({',
  23039. ' p: this,',
  23040. ' get: function () {',
  23041. ' return this.p.FField;',
  23042. ' },',
  23043. ' set: function (v) {',
  23044. ' $mod.TObject.FField = v;',
  23045. ' }',
  23046. ' }, 2);',
  23047. ' $mod.THelper.Fly.call({',
  23048. ' p: this,',
  23049. ' get: function () {',
  23050. ' return this.p.FField;',
  23051. ' },',
  23052. ' set: function (v) {',
  23053. ' $mod.TObject.FField = v;',
  23054. ' }',
  23055. ' }, 3);',
  23056. ' var $with = this.FField;',
  23057. ' $mod.THelper.Fly.call({',
  23058. ' get: function () {',
  23059. ' return $with;',
  23060. ' },',
  23061. ' set: function (v) {',
  23062. ' $with = v;',
  23063. ' }',
  23064. ' }, 4);',
  23065. ' $mod.THelper.Fly.call({',
  23066. ' p: $mod.TObject,',
  23067. ' get: function () {',
  23068. ' return this.p.FField;',
  23069. ' },',
  23070. ' set: function (v) {',
  23071. ' $mod.TObject.FField = v;',
  23072. ' }',
  23073. ' }, 5);',
  23074. ' var $with1 = $mod.TObject;',
  23075. ' $mod.THelper.Fly.call({',
  23076. ' p: $with1,',
  23077. ' get: function () {',
  23078. ' return this.p.FField;',
  23079. ' },',
  23080. ' set: function (v) {',
  23081. ' $mod.TObject.FField = v;',
  23082. ' }',
  23083. ' }, 6);',
  23084. ' var $with2 = $mod.TObject.FField;',
  23085. ' $mod.THelper.Fly.call({',
  23086. ' get: function () {',
  23087. ' return $with2;',
  23088. ' },',
  23089. ' set: function (v) {',
  23090. ' $with2 = v;',
  23091. ' }',
  23092. ' }, 7);',
  23093. ' };',
  23094. '});',
  23095. 'rtl.createHelper($mod, "THelper", null, function () {',
  23096. ' this.Fly = function (n) {',
  23097. ' };',
  23098. '});',
  23099. 'this.o = null;',
  23100. '']),
  23101. LinesToStr([ // $mod.$main
  23102. '$mod.THelper.Fly.call({',
  23103. ' p: $mod.o,',
  23104. ' get: function () {',
  23105. ' return this.p.FField;',
  23106. ' },',
  23107. ' set: function (v) {',
  23108. ' $mod.TObject.FField = v;',
  23109. ' }',
  23110. '}, 11);',
  23111. 'var $with = $mod.o;',
  23112. '$mod.THelper.Fly.call({',
  23113. ' p: $with,',
  23114. ' get: function () {',
  23115. ' return this.p.FField;',
  23116. ' },',
  23117. ' set: function (v) {',
  23118. ' $mod.TObject.FField = v;',
  23119. ' }',
  23120. '}, 12);',
  23121. 'var $with1 = $mod.o.FField;',
  23122. '$mod.THelper.Fly.call({',
  23123. ' get: function () {',
  23124. ' return $with1;',
  23125. ' },',
  23126. ' set: function (v) {',
  23127. ' $with1 = v;',
  23128. ' }',
  23129. '}, 13);',
  23130. '$mod.THelper.Fly.call({',
  23131. ' p: $mod.TObject,',
  23132. ' get: function () {',
  23133. ' return this.p.FField;',
  23134. ' },',
  23135. ' set: function (v) {',
  23136. ' $mod.TObject.FField = v;',
  23137. ' }',
  23138. '}, 14);',
  23139. 'var $with2 = $mod.TObject;',
  23140. '$mod.THelper.Fly.call({',
  23141. ' p: $with2,',
  23142. ' get: function () {',
  23143. ' return this.p.FField;',
  23144. ' },',
  23145. ' set: function (v) {',
  23146. ' $mod.TObject.FField = v;',
  23147. ' }',
  23148. '}, 15);',
  23149. 'var $with3 = $mod.TObject.FField;',
  23150. '$mod.THelper.Fly.call({',
  23151. ' get: function () {',
  23152. ' return $with3;',
  23153. ' },',
  23154. ' set: function (v) {',
  23155. ' $with3 = v;',
  23156. ' }',
  23157. '}, 16);',
  23158. '']));
  23159. end;
  23160. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23161. begin
  23162. StartProgram(false);
  23163. Add([
  23164. '{$modeswitch typehelpers}',
  23165. 'type',
  23166. ' TObject = class',
  23167. ' class var FField: word;',
  23168. ' class function GetField: word; static;',
  23169. ' class property Field: word read GetField write FField;',
  23170. ' end;',
  23171. ' THelper = type helper for word',
  23172. ' procedure Fly(n: byte);',
  23173. ' end;',
  23174. 'class function TObject.GetField: word;',
  23175. 'begin',
  23176. ' Field.Fly(1);',
  23177. ' TObject.Field.Fly(5);',
  23178. ' with TObject do Field.Fly(6);',
  23179. ' with TObject.Field do Fly(7);',
  23180. 'end;',
  23181. 'procedure THelper.Fly(n: byte);',
  23182. 'begin',
  23183. 'end;',
  23184. 'var',
  23185. ' o: TObject;',
  23186. 'begin',
  23187. ' o.Field.Fly(11);',
  23188. ' with o do Field.Fly(12);',
  23189. ' with o.Field do Fly(13);',
  23190. '']);
  23191. ConvertProgram;
  23192. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23193. LinesToStr([ // statements
  23194. 'rtl.createClass($mod, "TObject", null, function () {',
  23195. ' this.FField = 0;',
  23196. ' this.$init = function () {',
  23197. ' };',
  23198. ' this.$final = function () {',
  23199. ' };',
  23200. ' this.GetField = function () {',
  23201. ' var Result = 0;',
  23202. ' $mod.THelper.Fly.call({',
  23203. ' p: $mod.TObject.GetField(),',
  23204. ' get: function () {',
  23205. ' return this.p;',
  23206. ' },',
  23207. ' set: function (v) {',
  23208. ' this.p = v;',
  23209. ' }',
  23210. ' }, 1);',
  23211. ' $mod.THelper.Fly.call({',
  23212. ' p: $mod.TObject.GetField(),',
  23213. ' get: function () {',
  23214. ' return this.p;',
  23215. ' },',
  23216. ' set: function (v) {',
  23217. ' this.p = v;',
  23218. ' }',
  23219. ' }, 5);',
  23220. ' var $with = $mod.TObject;',
  23221. ' $mod.THelper.Fly.call({',
  23222. ' p: $with.GetField(),',
  23223. ' get: function () {',
  23224. ' return this.p;',
  23225. ' },',
  23226. ' set: function (v) {',
  23227. ' this.p = v;',
  23228. ' }',
  23229. ' }, 6);',
  23230. ' var $with1 = $mod.TObject.GetField();',
  23231. ' $mod.THelper.Fly.call({',
  23232. ' get: function () {',
  23233. ' return $with1;',
  23234. ' },',
  23235. ' set: function (v) {',
  23236. ' $with1 = v;',
  23237. ' }',
  23238. ' }, 7);',
  23239. ' return Result;',
  23240. ' };',
  23241. '});',
  23242. 'rtl.createHelper($mod, "THelper", null, function () {',
  23243. ' this.Fly = function (n) {',
  23244. ' };',
  23245. '});',
  23246. 'this.o = null;',
  23247. '']),
  23248. LinesToStr([ // $mod.$main
  23249. '$mod.THelper.Fly.call({',
  23250. ' p: $mod.o.GetField(),',
  23251. ' get: function () {',
  23252. ' return this.p;',
  23253. ' },',
  23254. ' set: function (v) {',
  23255. ' this.p = v;',
  23256. ' }',
  23257. '}, 11);',
  23258. 'var $with = $mod.o;',
  23259. '$mod.THelper.Fly.call({',
  23260. ' p: $with.GetField(),',
  23261. ' get: function () {',
  23262. ' return this.p;',
  23263. ' },',
  23264. ' set: function (v) {',
  23265. ' this.p = v;',
  23266. ' }',
  23267. '}, 12);',
  23268. 'var $with1 = $mod.o.GetField();',
  23269. '$mod.THelper.Fly.call({',
  23270. ' get: function () {',
  23271. ' return $with1;',
  23272. ' },',
  23273. ' set: function (v) {',
  23274. ' $with1 = v;',
  23275. ' }',
  23276. '}, 13);',
  23277. '']));
  23278. end;
  23279. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23280. begin
  23281. StartProgram(false);
  23282. Add([
  23283. '{$modeswitch typehelpers}',
  23284. 'type',
  23285. ' TObject = class',
  23286. ' class var FField: word;',
  23287. ' class function GetField: word;',
  23288. ' class property Field: word read GetField write FField;',
  23289. ' end;',
  23290. ' TClass = class of TObject;',
  23291. ' THelper = type helper for word',
  23292. ' procedure Fly(n: byte);',
  23293. ' end;',
  23294. 'class function TObject.GetField: word;',
  23295. 'begin',
  23296. ' Field.Fly(1);',
  23297. ' Self.Field.Fly(5);',
  23298. ' with Self do Field.Fly(6);',
  23299. ' with Self.Field do Fly(7);',
  23300. 'end;',
  23301. 'procedure THelper.Fly(n: byte);',
  23302. 'begin',
  23303. 'end;',
  23304. 'var',
  23305. ' o: TObject;',
  23306. ' c: TClass;',
  23307. 'begin',
  23308. ' o.Field.Fly(11);',
  23309. ' with o do Field.Fly(12);',
  23310. ' with o.Field do Fly(13);',
  23311. ' c.Field.Fly(14);',
  23312. ' with c do Field.Fly(15);',
  23313. ' with c.Field do Fly(16);',
  23314. '']);
  23315. ConvertProgram;
  23316. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23317. LinesToStr([ // statements
  23318. 'rtl.createClass($mod, "TObject", null, function () {',
  23319. ' this.FField = 0;',
  23320. ' this.$init = function () {',
  23321. ' };',
  23322. ' this.$final = function () {',
  23323. ' };',
  23324. ' this.GetField = function () {',
  23325. ' var Result = 0;',
  23326. ' $mod.THelper.Fly.call({',
  23327. ' p: this.GetField(),',
  23328. ' get: function () {',
  23329. ' return this.p;',
  23330. ' },',
  23331. ' set: function (v) {',
  23332. ' this.p = v;',
  23333. ' }',
  23334. ' }, 1);',
  23335. ' $mod.THelper.Fly.call({',
  23336. ' p: this.GetField(),',
  23337. ' get: function () {',
  23338. ' return this.p;',
  23339. ' },',
  23340. ' set: function (v) {',
  23341. ' this.p = v;',
  23342. ' }',
  23343. ' }, 5);',
  23344. ' $mod.THelper.Fly.call({',
  23345. ' p: this.GetField(),',
  23346. ' get: function () {',
  23347. ' return this.p;',
  23348. ' },',
  23349. ' set: function (v) {',
  23350. ' this.p = v;',
  23351. ' }',
  23352. ' }, 6);',
  23353. ' var $with = this.GetField();',
  23354. ' $mod.THelper.Fly.call({',
  23355. ' get: function () {',
  23356. ' return $with;',
  23357. ' },',
  23358. ' set: function (v) {',
  23359. ' $with = v;',
  23360. ' }',
  23361. ' }, 7);',
  23362. ' return Result;',
  23363. ' };',
  23364. '});',
  23365. 'rtl.createHelper($mod, "THelper", null, function () {',
  23366. ' this.Fly = function (n) {',
  23367. ' };',
  23368. '});',
  23369. 'this.o = null;',
  23370. 'this.c = null;',
  23371. '']),
  23372. LinesToStr([ // $mod.$main
  23373. '$mod.THelper.Fly.call({',
  23374. ' p: $mod.o.$class.GetField(),',
  23375. ' get: function () {',
  23376. ' return this.p;',
  23377. ' },',
  23378. ' set: function (v) {',
  23379. ' this.p = v;',
  23380. ' }',
  23381. '}, 11);',
  23382. 'var $with = $mod.o;',
  23383. '$mod.THelper.Fly.call({',
  23384. ' p: $with.$class.GetField(),',
  23385. ' get: function () {',
  23386. ' return this.p;',
  23387. ' },',
  23388. ' set: function (v) {',
  23389. ' this.p = v;',
  23390. ' }',
  23391. '}, 12);',
  23392. 'var $with1 = $mod.o.$class.GetField();',
  23393. '$mod.THelper.Fly.call({',
  23394. ' get: function () {',
  23395. ' return $with1;',
  23396. ' },',
  23397. ' set: function (v) {',
  23398. ' $with1 = v;',
  23399. ' }',
  23400. '}, 13);',
  23401. '$mod.THelper.Fly.call({',
  23402. ' p: $mod.c.GetField(),',
  23403. ' get: function () {',
  23404. ' return this.p;',
  23405. ' },',
  23406. ' set: function (v) {',
  23407. ' this.p = v;',
  23408. ' }',
  23409. '}, 14);',
  23410. 'var $with2 = $mod.c;',
  23411. '$mod.THelper.Fly.call({',
  23412. ' p: $with2.GetField(),',
  23413. ' get: function () {',
  23414. ' return this.p;',
  23415. ' },',
  23416. ' set: function (v) {',
  23417. ' this.p = v;',
  23418. ' }',
  23419. '}, 15);',
  23420. 'var $with3 = $mod.c.GetField();',
  23421. '$mod.THelper.Fly.call({',
  23422. ' get: function () {',
  23423. ' return $with3;',
  23424. ' },',
  23425. ' set: function (v) {',
  23426. ' $with3 = v;',
  23427. ' }',
  23428. '}, 16);',
  23429. '']));
  23430. end;
  23431. procedure TTestModule.TestTypeHelper_Property;
  23432. begin
  23433. StartProgram(false);
  23434. Add([
  23435. '{$modeswitch typehelpers}',
  23436. 'type',
  23437. ' THelper = type helper for word',
  23438. ' function GetSize: longint;',
  23439. ' procedure SetSize(Value: longint);',
  23440. ' property Size: longint read GetSize write SetSize;',
  23441. ' end;',
  23442. 'function THelper.GetSize: longint;',
  23443. 'begin',
  23444. ' Result:=Size+1;',
  23445. ' Size:=2;',
  23446. ' Result:=Self.Size+3;',
  23447. ' Self.Size:=4;',
  23448. ' with Self do begin',
  23449. ' Result:=Size+5;',
  23450. ' Size:=6;',
  23451. ' end;',
  23452. 'end;',
  23453. 'procedure THelper.SetSize(Value: longint);',
  23454. 'begin',
  23455. 'end;',
  23456. 'var w: word;',
  23457. 'begin',
  23458. ' w:=w.Size+7;',
  23459. ' w.Size:=w+8;',
  23460. ' with w do begin',
  23461. ' w:=Size+9;',
  23462. ' Size:=w+10;',
  23463. ' end;',
  23464. '']);
  23465. ConvertProgram;
  23466. CheckSource('TestTypeHelper_Property',
  23467. LinesToStr([ // statements
  23468. 'rtl.createHelper($mod, "THelper", null, function () {',
  23469. ' this.GetSize = function () {',
  23470. ' var Result = 0;',
  23471. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  23472. ' $mod.THelper.SetSize.call(this, 2);',
  23473. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  23474. ' $mod.THelper.SetSize.call(this, 4);',
  23475. ' var $with = this.get();',
  23476. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  23477. ' $mod.THelper.SetSize.call(this, 6);',
  23478. ' return Result;',
  23479. ' };',
  23480. ' this.SetSize = function (Value) {',
  23481. ' };',
  23482. '});',
  23483. 'this.w = 0;',
  23484. '']),
  23485. LinesToStr([ // $mod.$main
  23486. '$mod.w = $mod.THelper.GetSize.call({',
  23487. ' p: $mod,',
  23488. ' get: function () {',
  23489. ' return this.p.w;',
  23490. ' },',
  23491. ' set: function (v) {',
  23492. ' this.p.w = v;',
  23493. ' }',
  23494. '}) + 7;',
  23495. '$mod.THelper.SetSize.call({',
  23496. ' p: $mod,',
  23497. ' get: function () {',
  23498. ' return this.p.w;',
  23499. ' },',
  23500. ' set: function (v) {',
  23501. ' this.p.w = v;',
  23502. ' }',
  23503. '}, $mod.w + 8);',
  23504. 'var $with = $mod.w;',
  23505. '$mod.w = $mod.THelper.GetSize.call({',
  23506. ' get: function () {',
  23507. ' return $with;',
  23508. ' },',
  23509. ' set: function (v) {',
  23510. ' $with = v;',
  23511. ' }',
  23512. '}) + 9;',
  23513. '$mod.THelper.SetSize.call({',
  23514. ' get: function () {',
  23515. ' return $with;',
  23516. ' },',
  23517. ' set: function (v) {',
  23518. ' $with = v;',
  23519. ' }',
  23520. '}, $mod.w + 10);',
  23521. '']));
  23522. end;
  23523. procedure TTestModule.TestTypeHelper_Property_Array;
  23524. begin
  23525. StartProgram(false);
  23526. Add([
  23527. '{$modeswitch typehelpers}',
  23528. 'type',
  23529. ' THelper = type helper for word',
  23530. ' function GetItems(Index: byte): boolean;',
  23531. ' procedure SetItems(Index: byte; Value: boolean);',
  23532. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  23533. ' end;',
  23534. 'function THelper.GetItems(Index: byte): boolean;',
  23535. 'begin',
  23536. ' Result:=Items[1];',
  23537. ' Items[2]:=false;',
  23538. ' Result:=Self.Items[3];',
  23539. ' Self.Items[4]:=true;',
  23540. ' with Self do begin',
  23541. ' Result:=Items[5];',
  23542. ' Items[6]:=false;',
  23543. ' end;',
  23544. 'end;',
  23545. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  23546. 'begin',
  23547. 'end;',
  23548. 'var',
  23549. ' w: word;',
  23550. ' b: boolean;',
  23551. 'begin',
  23552. ' b:=w.Items[1];',
  23553. ' w.Items[2]:=b;',
  23554. ' with w do begin',
  23555. ' b:=Items[3];',
  23556. ' Items[4]:=b;',
  23557. ' end;',
  23558. '']);
  23559. ConvertProgram;
  23560. CheckSource('TestTypeHelper_Property_Array',
  23561. LinesToStr([ // statements
  23562. 'rtl.createHelper($mod, "THelper", null, function () {',
  23563. ' this.GetItems = function (Index) {',
  23564. ' var Result = false;',
  23565. ' Result = $mod.THelper.GetItems.call(this, 1);',
  23566. ' $mod.THelper.SetItems.call(this, 2, false);',
  23567. ' Result = $mod.THelper.GetItems.call(this, 3);',
  23568. ' $mod.THelper.SetItems.call(this, 4, true);',
  23569. ' var $with = this.get();',
  23570. ' Result = $mod.THelper.GetItems.call(this, 5);',
  23571. ' $mod.THelper.SetItems.call(this, 6, false);',
  23572. ' return Result;',
  23573. ' };',
  23574. ' this.SetItems = function (Index, Value) {',
  23575. ' };',
  23576. '});',
  23577. 'this.w = 0;',
  23578. 'this.b = false;',
  23579. '']),
  23580. LinesToStr([ // $mod.$main
  23581. '$mod.b = $mod.THelper.GetItems.call({',
  23582. ' p: $mod,',
  23583. ' get: function () {',
  23584. ' return this.p.w;',
  23585. ' },',
  23586. ' set: function (v) {',
  23587. ' this.p.w = v;',
  23588. ' }',
  23589. '}, 1);',
  23590. '$mod.THelper.SetItems.call({',
  23591. ' p: $mod,',
  23592. ' get: function () {',
  23593. ' return this.p.w;',
  23594. ' },',
  23595. ' set: function (v) {',
  23596. ' this.p.w = v;',
  23597. ' }',
  23598. '}, 2, $mod.b);',
  23599. 'var $with = $mod.w;',
  23600. '$mod.b = $mod.THelper.GetItems.call({',
  23601. ' get: function () {',
  23602. ' return $with;',
  23603. ' },',
  23604. ' set: function (v) {',
  23605. ' $with = v;',
  23606. ' }',
  23607. '}, 3);',
  23608. '$mod.THelper.SetItems.call({',
  23609. ' get: function () {',
  23610. ' return $with;',
  23611. ' },',
  23612. ' set: function (v) {',
  23613. ' $with = v;',
  23614. ' }',
  23615. '}, 4, $mod.b);',
  23616. '']));
  23617. end;
  23618. procedure TTestModule.TestTypeHelper_ClassProperty;
  23619. begin
  23620. StartProgram(false);
  23621. Add([
  23622. '{$modeswitch typehelpers}',
  23623. 'type',
  23624. ' THelper = type helper for word',
  23625. ' class function GetSize: longint; static;',
  23626. ' class procedure SetSize(Value: longint); static;',
  23627. ' class property Size: longint read GetSize write SetSize;',
  23628. ' end;',
  23629. 'class function THelper.GetSize: longint;',
  23630. 'begin',
  23631. ' Result:=Size+1;',
  23632. ' Size:=2;',
  23633. 'end;',
  23634. 'class procedure THelper.SetSize(Value: longint);',
  23635. 'begin',
  23636. 'end;',
  23637. 'begin',
  23638. '']);
  23639. ConvertProgram;
  23640. CheckSource('TestTypeHelper_ClassProperty',
  23641. LinesToStr([ // statements
  23642. 'rtl.createHelper($mod, "THelper", null, function () {',
  23643. ' this.GetSize = function () {',
  23644. ' var Result = 0;',
  23645. ' Result = $mod.THelper.GetSize() + 1;',
  23646. ' $mod.THelper.SetSize(2);',
  23647. ' return Result;',
  23648. ' };',
  23649. ' this.SetSize = function (Value) {',
  23650. ' };',
  23651. '});',
  23652. '']),
  23653. LinesToStr([ // $mod.$main
  23654. '']));
  23655. end;
  23656. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  23657. begin
  23658. StartProgram(false);
  23659. Add([
  23660. '{$modeswitch typehelpers}',
  23661. 'type',
  23662. ' THelper = type helper for word',
  23663. ' class function GetItems(Index: byte): boolean; static;',
  23664. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  23665. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  23666. ' end;',
  23667. 'class function THelper.GetItems(Index: byte): boolean;',
  23668. 'begin',
  23669. ' Result:=Items[1];',
  23670. ' Items[2]:=false;',
  23671. 'end;',
  23672. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  23673. 'begin',
  23674. 'end;',
  23675. 'var',
  23676. ' w: word;',
  23677. ' b: boolean;',
  23678. 'begin',
  23679. ' b:=w.Items[1];',
  23680. ' w.Items[2]:=b;',
  23681. ' with w do begin',
  23682. ' b:=Items[3];',
  23683. ' Items[4]:=b;',
  23684. ' end;',
  23685. '']);
  23686. ConvertProgram;
  23687. CheckSource('TestTypeHelper_ClassProperty_Array',
  23688. LinesToStr([ // statements
  23689. 'rtl.createHelper($mod, "THelper", null, function () {',
  23690. ' this.GetItems = function (Index) {',
  23691. ' var Result = false;',
  23692. ' Result = $mod.THelper.GetItems(1);',
  23693. ' $mod.THelper.SetItems(2, false);',
  23694. ' return Result;',
  23695. ' };',
  23696. ' this.SetItems = function (Index, Value) {',
  23697. ' };',
  23698. '});',
  23699. 'this.w = 0;',
  23700. 'this.b = false;',
  23701. '']),
  23702. LinesToStr([ // $mod.$main
  23703. '$mod.b = $mod.THelper.GetItems(1);',
  23704. '$mod.THelper.SetItems(2, $mod.b);',
  23705. 'var $with = $mod.w;',
  23706. '$mod.b = $mod.THelper.GetItems(3);',
  23707. '$mod.THelper.SetItems(4, $mod.b);',
  23708. '']));
  23709. end;
  23710. procedure TTestModule.TestTypeHelper_ClassMethod;
  23711. begin
  23712. StartProgram(false);
  23713. Add([
  23714. '{$modeswitch typehelpers}',
  23715. 'type',
  23716. ' THelper = type helper for word',
  23717. ' class procedure DoStatic; static;',
  23718. ' end;',
  23719. 'class procedure THelper.DoStatic;',
  23720. 'begin',
  23721. ' DoStatic;',
  23722. ' DoStatic();',
  23723. 'end;',
  23724. 'var w: word;',
  23725. 'begin',
  23726. ' w.DoStatic;',
  23727. ' w.DoStatic();',
  23728. '']);
  23729. ConvertProgram;
  23730. CheckSource('TestTypeHelper_ClassMethod',
  23731. LinesToStr([ // statements
  23732. 'rtl.createHelper($mod, "THelper", null, function () {',
  23733. ' this.DoStatic = function () {',
  23734. ' $mod.THelper.DoStatic();',
  23735. ' $mod.THelper.DoStatic();',
  23736. ' };',
  23737. '});',
  23738. 'this.w = 0;',
  23739. '']),
  23740. LinesToStr([ // $mod.$main
  23741. '$mod.THelper.DoStatic();',
  23742. '$mod.THelper.DoStatic();',
  23743. '']));
  23744. end;
  23745. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  23746. begin
  23747. StartProgram(false);
  23748. Add([
  23749. '{$modeswitch typehelpers}',
  23750. 'type',
  23751. ' THelper = type helper for word',
  23752. ' procedure Run; external name ''Run'';',
  23753. ' end;',
  23754. 'var w: word;',
  23755. 'begin',
  23756. ' w.Run;',
  23757. '']);
  23758. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  23759. ConvertProgram;
  23760. end;
  23761. procedure TTestModule.TestTypeHelper_Constructor;
  23762. begin
  23763. StartProgram(false);
  23764. Add([
  23765. '{$modeswitch typehelpers}',
  23766. 'type',
  23767. ' THelper = type helper for word',
  23768. ' constructor Init(e: longint);',
  23769. ' end;',
  23770. 'constructor THelper.Init(e: longint);',
  23771. 'begin',
  23772. ' Self:=e;',
  23773. ' Init(e+1);',
  23774. 'end;',
  23775. 'var w: word;',
  23776. 'begin',
  23777. ' w:=word.Init(2);',
  23778. ' w:=w.Init(3);',
  23779. ' with word do w:=Init(4);',
  23780. ' with w do w:=Init(5);',
  23781. '']);
  23782. ConvertProgram;
  23783. CheckSource('TestTypeHelper_Constructor',
  23784. LinesToStr([ // statements
  23785. 'rtl.createHelper($mod, "THelper", null, function () {',
  23786. ' this.Init = function (e) {',
  23787. ' this.set(e);',
  23788. ' $mod.THelper.Init.call(this, e + 1);',
  23789. ' return this.get();',
  23790. ' };',
  23791. ' this.$new = function (fn, args) {',
  23792. ' return this[fn].apply({',
  23793. ' p: 0,',
  23794. ' get: function () {',
  23795. ' return this.p;',
  23796. ' },',
  23797. ' set: function (v) {',
  23798. ' this.p = v;',
  23799. ' }',
  23800. ' }, args);',
  23801. ' };',
  23802. '});',
  23803. 'this.w = 0;',
  23804. '']),
  23805. LinesToStr([ // $mod.$main
  23806. '$mod.w = $mod.THelper.$new("Init", [2]);',
  23807. '$mod.w = $mod.THelper.Init.call({',
  23808. ' p: $mod,',
  23809. ' get: function () {',
  23810. ' return this.p.w;',
  23811. ' },',
  23812. ' set: function (v) {',
  23813. ' this.p.w = v;',
  23814. ' }',
  23815. '}, 3);',
  23816. '$mod.w = $mod.THelper.$new("Init", [4]);',
  23817. 'var $with = $mod.w;',
  23818. '$mod.w = $mod.THelper.Init.call({',
  23819. ' get: function () {',
  23820. ' return $with;',
  23821. ' },',
  23822. ' set: function (v) {',
  23823. ' $with = v;',
  23824. ' }',
  23825. '}, 5);',
  23826. '']));
  23827. end;
  23828. procedure TTestModule.TestTypeHelper_Word;
  23829. begin
  23830. StartProgram(false);
  23831. Add([
  23832. '{$modeswitch typehelpers}',
  23833. 'type',
  23834. ' THelper = type helper for word',
  23835. ' procedure DoIt(e: byte = 123);',
  23836. ' end;',
  23837. 'procedure THelper.DoIt(e: byte);',
  23838. 'begin',
  23839. ' Self:=e;',
  23840. ' Self:=Self+1;',
  23841. ' with Self do Doit;',
  23842. 'end;',
  23843. 'begin',
  23844. ' word(3).DoIt;',
  23845. '']);
  23846. ConvertProgram;
  23847. CheckSource('TestTypeHelper_Word',
  23848. LinesToStr([ // statements
  23849. 'rtl.createHelper($mod, "THelper", null, function () {',
  23850. ' this.DoIt = function (e) {',
  23851. ' this.set(e);',
  23852. ' this.set(this.get() + 1);',
  23853. ' var $with = this.get();',
  23854. ' $mod.THelper.DoIt.call(this, 123);',
  23855. ' };',
  23856. '});',
  23857. '']),
  23858. LinesToStr([ // $mod.$main
  23859. '$mod.THelper.DoIt.call({',
  23860. ' get: function () {',
  23861. ' return 3;',
  23862. ' },',
  23863. ' set: function (v) {',
  23864. ' rtl.raiseE("EPropReadOnly");',
  23865. ' }',
  23866. '}, 123);',
  23867. '']));
  23868. end;
  23869. procedure TTestModule.TestTypeHelper_Boolean;
  23870. begin
  23871. StartProgram(false);
  23872. Add([
  23873. '{$modeswitch typehelpers}',
  23874. 'type',
  23875. ' Integer = longint;',
  23876. ' THelper = type helper for boolean',
  23877. ' procedure Run(e: wordbool = true);',
  23878. ' end;',
  23879. 'procedure THelper.Run(e: wordbool);',
  23880. 'begin',
  23881. ' Self:=e;',
  23882. ' Self:=not Self;',
  23883. ' with Self do Run;',
  23884. ' if Integer(Self)=0 then ;',
  23885. 'end;',
  23886. 'begin',
  23887. ' boolean(3).Run;',
  23888. '']);
  23889. ConvertProgram;
  23890. CheckSource('TestTypeHelper_Boolean',
  23891. LinesToStr([ // statements
  23892. 'rtl.createHelper($mod, "THelper", null, function () {',
  23893. ' this.Run = function (e) {',
  23894. ' this.set(e);',
  23895. ' this.set(!this.get());',
  23896. ' var $with = this.get();',
  23897. ' $mod.THelper.Run.call(this, true);',
  23898. ' if ((this.get() ? 1 : 0) === 0) ;',
  23899. ' };',
  23900. '});',
  23901. '']),
  23902. LinesToStr([ // $mod.$main
  23903. '$mod.THelper.Run.call({',
  23904. ' a: 3 != 0,',
  23905. ' get: function () {',
  23906. ' return this.a;',
  23907. ' },',
  23908. ' set: function (v) {',
  23909. ' rtl.raiseE("EPropReadOnly");',
  23910. ' }',
  23911. '}, true);',
  23912. '']));
  23913. end;
  23914. procedure TTestModule.TestTypeHelper_WordBool;
  23915. begin
  23916. StartProgram(false);
  23917. Add([
  23918. '{$modeswitch typehelpers}',
  23919. 'type',
  23920. ' Integer = longint;',
  23921. ' THelper = type helper for WordBool',
  23922. ' procedure Run(e: wordbool = true);',
  23923. ' end;',
  23924. 'procedure THelper.Run(e: wordbool);',
  23925. 'var i: integer;',
  23926. 'begin',
  23927. ' i:=Integer(Self);',
  23928. 'end;',
  23929. 'var w: wordbool;',
  23930. 'begin',
  23931. ' w.Run;',
  23932. ' wordbool(3).Run;',
  23933. '']);
  23934. ConvertProgram;
  23935. CheckSource('TestTypeHelper_WordBool',
  23936. LinesToStr([ // statements
  23937. 'rtl.createHelper($mod, "THelper", null, function () {',
  23938. ' this.Run = function (e) {',
  23939. ' var i = 0;',
  23940. ' i = (this.get() ? 1 : 0);',
  23941. ' };',
  23942. '});',
  23943. 'this.w = false;',
  23944. '']),
  23945. LinesToStr([ // $mod.$main
  23946. '$mod.THelper.Run.call({',
  23947. ' p: $mod,',
  23948. ' get: function () {',
  23949. ' return this.p.w;',
  23950. ' },',
  23951. ' set: function (v) {',
  23952. ' this.p.w = v;',
  23953. ' }',
  23954. '}, true);',
  23955. '$mod.THelper.Run.call({',
  23956. ' a: 3 != 0,',
  23957. ' get: function () {',
  23958. ' return this.a;',
  23959. ' },',
  23960. ' set: function (v) {',
  23961. ' rtl.raiseE("EPropReadOnly");',
  23962. ' }',
  23963. '}, true);',
  23964. '']));
  23965. end;
  23966. procedure TTestModule.TestTypeHelper_Double;
  23967. begin
  23968. StartProgram(false);
  23969. Add([
  23970. '{$modeswitch typehelpers}',
  23971. 'type',
  23972. ' Float = type double;',
  23973. ' THelper = type helper for Float',
  23974. ' const NPI = 3.141592;',
  23975. ' function ToStr: String;',
  23976. ' end;',
  23977. 'function THelper.ToStr: String;',
  23978. 'begin',
  23979. 'end;',
  23980. 'procedure DoIt(s: string);',
  23981. 'begin',
  23982. 'end;',
  23983. 'var f: Float;',
  23984. 'begin',
  23985. ' DoIt(f.toStr);',
  23986. ' DoIt(f.toStr());',
  23987. ' (f*f).toStr;',
  23988. ' DoIt((f*f).toStr);',
  23989. '']);
  23990. ConvertProgram;
  23991. CheckSource('TestTypeHelper_Double',
  23992. LinesToStr([ // statements
  23993. 'rtl.createHelper($mod, "THelper", null, function () {',
  23994. ' this.NPI = 3.141592;',
  23995. ' this.ToStr = function () {',
  23996. ' var Result = "";',
  23997. ' return Result;',
  23998. ' };',
  23999. '});',
  24000. 'this.DoIt = function (s) {',
  24001. '};',
  24002. 'this.f = 0.0;',
  24003. '']),
  24004. LinesToStr([ // $mod.$main
  24005. '$mod.DoIt($mod.THelper.ToStr.call({',
  24006. ' p: $mod,',
  24007. ' get: function () {',
  24008. ' return this.p.f;',
  24009. ' },',
  24010. ' set: function (v) {',
  24011. ' this.p.f = v;',
  24012. ' }',
  24013. '}));',
  24014. '$mod.DoIt($mod.THelper.ToStr.call({',
  24015. ' p: $mod,',
  24016. ' get: function () {',
  24017. ' return this.p.f;',
  24018. ' },',
  24019. ' set: function (v) {',
  24020. ' this.p.f = v;',
  24021. ' }',
  24022. '}));',
  24023. '$mod.THelper.ToStr.call({',
  24024. ' a: $mod.f * $mod.f,',
  24025. ' get: function () {',
  24026. ' return this.a;',
  24027. ' },',
  24028. ' set: function (v) {',
  24029. ' rtl.raiseE("EPropReadOnly");',
  24030. ' }',
  24031. '});',
  24032. '$mod.DoIt($mod.THelper.ToStr.call({',
  24033. ' a: $mod.f * $mod.f,',
  24034. ' get: function () {',
  24035. ' return this.a;',
  24036. ' },',
  24037. ' set: function (v) {',
  24038. ' rtl.raiseE("EPropReadOnly");',
  24039. ' }',
  24040. '}));',
  24041. '']));
  24042. end;
  24043. procedure TTestModule.TestTypeHelper_NativeInt;
  24044. begin
  24045. StartProgram(false);
  24046. Add([
  24047. '{$modeswitch typehelpers}',
  24048. 'type',
  24049. ' MaxInt = type nativeint;',
  24050. ' THelperI = type helper for MaxInt',
  24051. ' function ToStr: String;',
  24052. ' end;',
  24053. ' MaxUInt = type nativeuint;',
  24054. ' THelperU = type helper for MaxUInt',
  24055. ' function ToStr: String;',
  24056. ' end;',
  24057. 'function THelperI.ToStr: String;',
  24058. 'begin',
  24059. ' Result:=str(Self);',
  24060. 'end;',
  24061. 'function THelperU.ToStr: String;',
  24062. 'begin',
  24063. ' Result:=str(Self);',
  24064. 'end;',
  24065. 'procedure DoIt(s: string);',
  24066. 'begin',
  24067. 'end;',
  24068. 'var i: MaxInt;',
  24069. 'begin',
  24070. ' DoIt(i.toStr);',
  24071. ' DoIt(i.toStr());',
  24072. ' (i*i).toStr;',
  24073. ' DoIt((i*i).toStr);',
  24074. '']);
  24075. ConvertProgram;
  24076. CheckSource('TestTypeHelper_NativeInt',
  24077. LinesToStr([ // statements
  24078. 'rtl.createHelper($mod, "THelperI", null, function () {',
  24079. ' this.ToStr = function () {',
  24080. ' var Result = "";',
  24081. ' Result = "" + this.get();',
  24082. ' return Result;',
  24083. ' };',
  24084. '});',
  24085. 'rtl.createHelper($mod, "THelperU", null, function () {',
  24086. ' this.ToStr = function () {',
  24087. ' var Result = "";',
  24088. ' Result = "" + this.get();',
  24089. ' return Result;',
  24090. ' };',
  24091. '});',
  24092. 'this.DoIt = function (s) {',
  24093. '};',
  24094. 'this.i = 0;',
  24095. '']),
  24096. LinesToStr([ // $mod.$main
  24097. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24098. ' p: $mod,',
  24099. ' get: function () {',
  24100. ' return this.p.i;',
  24101. ' },',
  24102. ' set: function (v) {',
  24103. ' this.p.i = v;',
  24104. ' }',
  24105. '}));',
  24106. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24107. ' p: $mod,',
  24108. ' get: function () {',
  24109. ' return this.p.i;',
  24110. ' },',
  24111. ' set: function (v) {',
  24112. ' this.p.i = v;',
  24113. ' }',
  24114. '}));',
  24115. '$mod.THelperI.ToStr.call({',
  24116. ' a: $mod.i * $mod.i,',
  24117. ' get: function () {',
  24118. ' return this.a;',
  24119. ' },',
  24120. ' set: function (v) {',
  24121. ' rtl.raiseE("EPropReadOnly");',
  24122. ' }',
  24123. '});',
  24124. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24125. ' a: $mod.i * $mod.i,',
  24126. ' get: function () {',
  24127. ' return this.a;',
  24128. ' },',
  24129. ' set: function (v) {',
  24130. ' rtl.raiseE("EPropReadOnly");',
  24131. ' }',
  24132. '}));',
  24133. '']));
  24134. end;
  24135. procedure TTestModule.TestTypeHelper_StringChar;
  24136. begin
  24137. StartProgram(false);
  24138. Add([
  24139. '{$modeswitch typehelpers}',
  24140. 'type',
  24141. ' TStringHelper = type helper for string',
  24142. ' procedure DoIt(e: byte = 123);',
  24143. ' end;',
  24144. ' TCharHelper = type helper for char',
  24145. ' procedure Fly;',
  24146. ' end;',
  24147. 'procedure TStringHelper.DoIt(e: byte);',
  24148. 'begin',
  24149. ' Self[1]:=''c'';',
  24150. ' Self[2]:=Self[3];',
  24151. 'end;',
  24152. 'procedure TCharHelper.Fly;',
  24153. 'begin',
  24154. ' Self:=''c'';',
  24155. 'end;',
  24156. 'begin',
  24157. ' ''abc''.DoIt;',
  24158. ' ''xyz''.DoIt();',
  24159. ' ''c''.Fly();',
  24160. '']);
  24161. ConvertProgram;
  24162. CheckSource('TestTypeHelper_StringChar',
  24163. LinesToStr([ // statements
  24164. 'rtl.createHelper($mod, "TStringHelper", null, function () {',
  24165. ' this.DoIt = function (e) {',
  24166. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24167. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24168. ' };',
  24169. '});',
  24170. 'rtl.createHelper($mod, "TCharHelper", null, function () {',
  24171. ' this.Fly = function () {',
  24172. ' this.set("c");',
  24173. ' };',
  24174. '});',
  24175. '']),
  24176. LinesToStr([ // $mod.$main
  24177. '$mod.TStringHelper.DoIt.call({',
  24178. ' get: function () {',
  24179. ' return "abc";',
  24180. ' },',
  24181. ' set: function (v) {',
  24182. ' rtl.raiseE("EPropReadOnly");',
  24183. ' }',
  24184. '}, 123);',
  24185. '$mod.TStringHelper.DoIt.call({',
  24186. ' get: function () {',
  24187. ' return "xyz";',
  24188. ' },',
  24189. ' set: function (v) {',
  24190. ' rtl.raiseE("EPropReadOnly");',
  24191. ' }',
  24192. '}, 123);',
  24193. '$mod.TCharHelper.Fly.call({',
  24194. ' get: function () {',
  24195. ' return "c";',
  24196. ' },',
  24197. ' set: function (v) {',
  24198. ' rtl.raiseE("EPropReadOnly");',
  24199. ' }',
  24200. '});',
  24201. '']));
  24202. end;
  24203. procedure TTestModule.TestTypeHelper_JSValue;
  24204. begin
  24205. StartProgram(false);
  24206. Add([
  24207. '{$modeswitch typehelpers}',
  24208. 'type',
  24209. ' TExtValue = type jsvalue;',
  24210. ' THelper = type helper for TExtValue',
  24211. ' function ToStr: String;',
  24212. ' end;',
  24213. 'function THelper.ToStr: String;',
  24214. 'begin',
  24215. 'end;',
  24216. 'var',
  24217. ' s: string;',
  24218. ' v: TExtValue;',
  24219. 'begin',
  24220. ' s:=v.toStr;',
  24221. ' s:=v.toStr();',
  24222. ' TExtValue(s).toStr;',
  24223. '']);
  24224. ConvertProgram;
  24225. CheckSource('TestTypeHelper_JSValue',
  24226. LinesToStr([ // statements
  24227. 'rtl.createHelper($mod, "THelper", null, function () {',
  24228. ' this.ToStr = function () {',
  24229. ' var Result = "";',
  24230. ' return Result;',
  24231. ' };',
  24232. '});',
  24233. 'this.s = "";',
  24234. 'this.v = undefined;',
  24235. '']),
  24236. LinesToStr([ // $mod.$main
  24237. '$mod.s = $mod.THelper.ToStr.call({',
  24238. ' p: $mod,',
  24239. ' get: function () {',
  24240. ' return this.p.v;',
  24241. ' },',
  24242. ' set: function (v) {',
  24243. ' this.p.v = v;',
  24244. ' }',
  24245. '});',
  24246. '$mod.s = $mod.THelper.ToStr.call({',
  24247. ' p: $mod,',
  24248. ' get: function () {',
  24249. ' return this.p.v;',
  24250. ' },',
  24251. ' set: function (v) {',
  24252. ' this.p.v = v;',
  24253. ' }',
  24254. '});',
  24255. '$mod.THelper.ToStr.call({',
  24256. ' p: $mod,',
  24257. ' get: function () {',
  24258. ' return this.p.s;',
  24259. ' },',
  24260. ' set: function (v) {',
  24261. ' rtl.raiseE("EPropReadOnly");',
  24262. ' }',
  24263. '});',
  24264. '']));
  24265. end;
  24266. procedure TTestModule.TestTypeHelper_Array;
  24267. begin
  24268. StartProgram(false);
  24269. Add([
  24270. '{$modeswitch typehelpers}',
  24271. 'type',
  24272. ' TArrOfBool = array of boolean;',
  24273. ' TArrOfJS = array of jsvalue;',
  24274. ' THelper = type helper for TArrOfBool',
  24275. ' procedure DoIt(e: byte = 123);',
  24276. ' end;',
  24277. 'procedure THelper.DoIt(e: byte);',
  24278. 'begin',
  24279. ' Self[1]:=true;',
  24280. ' Self[2]:=not Self[3];',
  24281. ' SetLength(Self,4);',
  24282. 'end;',
  24283. 'var',
  24284. ' b: TArrOfBool;',
  24285. ' j: TArrOfJS;',
  24286. 'begin',
  24287. ' b.DoIt;',
  24288. ' TArrOfBool(j).DoIt();',
  24289. '']);
  24290. ConvertProgram;
  24291. CheckSource('TestTypeHelper_Array',
  24292. LinesToStr([ // statements
  24293. 'rtl.createHelper($mod, "THelper", null, function () {',
  24294. ' this.DoIt = function (e) {',
  24295. ' this.get()[1] = true;',
  24296. ' this.get()[2] = !this.get()[3];',
  24297. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24298. ' };',
  24299. '});',
  24300. 'this.b = [];',
  24301. 'this.j = [];',
  24302. '']),
  24303. LinesToStr([ // $mod.$main
  24304. '$mod.THelper.DoIt.call({',
  24305. ' p: $mod,',
  24306. ' get: function () {',
  24307. ' return this.p.b;',
  24308. ' },',
  24309. ' set: function (v) {',
  24310. ' this.p.b = v;',
  24311. ' }',
  24312. '}, 123);',
  24313. '$mod.THelper.DoIt.call({',
  24314. ' p: $mod,',
  24315. ' get: function () {',
  24316. ' return this.p.j;',
  24317. ' },',
  24318. ' set: function (v) {',
  24319. ' this.p.j = v;',
  24320. ' }',
  24321. '}, 123);',
  24322. '']));
  24323. end;
  24324. procedure TTestModule.TestTypeHelper_EnumType;
  24325. begin
  24326. StartProgram(false);
  24327. Add([
  24328. '{$modeswitch typehelpers}',
  24329. 'type',
  24330. ' TEnum = (red,blue);',
  24331. ' THelper = type helper for TEnum',
  24332. ' procedure DoIt(e: byte = 123);',
  24333. ' class procedure Swing(w: word); static;',
  24334. ' end;',
  24335. 'procedure THelper.DoIt(e: byte);',
  24336. 'begin',
  24337. ' Self:=red;',
  24338. ' Self:=succ(Self);',
  24339. ' with Self do Doit;',
  24340. 'end;',
  24341. 'class procedure THelper.Swing(w: word);',
  24342. 'begin',
  24343. 'end;',
  24344. 'var e: TEnum;',
  24345. 'begin',
  24346. ' e.DoIt;',
  24347. ' red.DoIt;',
  24348. ' TEnum.blue.DoIt;',
  24349. ' TEnum(1).DoIt;',
  24350. ' TEnum.Swing(3);',
  24351. '']);
  24352. ConvertProgram;
  24353. CheckSource('TestTypeHelper_EnumType',
  24354. LinesToStr([ // statements
  24355. 'this.TEnum = {',
  24356. ' "0": "red",',
  24357. ' red: 0,',
  24358. ' "1": "blue",',
  24359. ' blue: 1',
  24360. '};',
  24361. 'rtl.createHelper($mod, "THelper", null, function () {',
  24362. ' this.DoIt = function (e) {',
  24363. ' this.set($mod.TEnum.red);',
  24364. ' this.set(this.get() + 1);',
  24365. ' var $with = this.get();',
  24366. ' $mod.THelper.DoIt.call(this, 123);',
  24367. ' };',
  24368. ' this.Swing = function (w) {',
  24369. ' };',
  24370. '});',
  24371. 'this.e = 0;',
  24372. '']),
  24373. LinesToStr([ // $mod.$main
  24374. '$mod.THelper.DoIt.call({',
  24375. ' p: $mod,',
  24376. ' get: function () {',
  24377. ' return this.p.e;',
  24378. ' },',
  24379. ' set: function (v) {',
  24380. ' this.p.e = v;',
  24381. ' }',
  24382. '}, 123);',
  24383. '$mod.THelper.DoIt.call({',
  24384. ' p: $mod.TEnum,',
  24385. ' get: function () {',
  24386. ' return this.p.red;',
  24387. ' },',
  24388. ' set: function (v) {',
  24389. ' rtl.raiseE("EPropReadOnly");',
  24390. ' }',
  24391. '}, 123);',
  24392. '$mod.THelper.DoIt.call({',
  24393. ' p: $mod.TEnum,',
  24394. ' get: function () {',
  24395. ' return this.p.blue;',
  24396. ' },',
  24397. ' set: function (v) {',
  24398. ' rtl.raiseE("EPropReadOnly");',
  24399. ' }',
  24400. '}, 123);',
  24401. '$mod.THelper.DoIt.call({',
  24402. ' get: function () {',
  24403. ' return 1;',
  24404. ' },',
  24405. ' set: function (v) {',
  24406. ' rtl.raiseE("EPropReadOnly");',
  24407. ' }',
  24408. '}, 123);',
  24409. '$mod.THelper.Swing(3);',
  24410. '']));
  24411. end;
  24412. procedure TTestModule.TestTypeHelper_SetType;
  24413. begin
  24414. StartProgram(false);
  24415. Add([
  24416. '{$modeswitch typehelpers}',
  24417. 'type',
  24418. ' TEnum = (red,blue);',
  24419. ' TSetOfEnum = set of TEnum;',
  24420. ' THelper = type helper for TSetOfEnum',
  24421. ' procedure DoIt(e: byte = 123);',
  24422. ' constructor Init(e: TEnum);',
  24423. ' constructor InitEmpty;',
  24424. ' end;',
  24425. 'procedure THelper.DoIt(e: byte);',
  24426. 'begin',
  24427. ' Self:=[];',
  24428. ' Self:=[red];',
  24429. ' Include(Self,blue);',
  24430. 'end;',
  24431. 'constructor THelper.Init(e: TEnum);',
  24432. 'begin',
  24433. ' Self:=[];',
  24434. ' Self:=[e];',
  24435. ' Include(Self,blue);',
  24436. 'end;',
  24437. 'constructor THelper.InitEmpty;',
  24438. 'begin',
  24439. 'end;',
  24440. 'var s: TSetOfEnum;',
  24441. 'begin',
  24442. ' s.DoIt;',
  24443. //' [red].DoIt;',
  24444. //' with s do DoIt;',
  24445. //' with [red,blue] do DoIt;',
  24446. ' s:=TSetOfEnum.Init(blue);',
  24447. ' s:=s.Init(blue);',
  24448. '']);
  24449. ConvertProgram;
  24450. CheckSource('TestTypeHelper_SetType',
  24451. LinesToStr([ // statements
  24452. 'this.TEnum = {',
  24453. ' "0": "red",',
  24454. ' red: 0,',
  24455. ' "1": "blue",',
  24456. ' blue: 1',
  24457. '};',
  24458. 'rtl.createHelper($mod, "THelper", null, function () {',
  24459. ' this.DoIt = function (e) {',
  24460. ' this.set({});',
  24461. ' this.set(rtl.createSet($mod.TEnum.red));',
  24462. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24463. ' };',
  24464. ' this.Init = function (e) {',
  24465. ' this.set({});',
  24466. ' this.set(rtl.createSet(e));',
  24467. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24468. ' return this.get();',
  24469. ' };',
  24470. ' this.InitEmpty = function () {',
  24471. ' return this.get();',
  24472. ' };',
  24473. ' this.$new = function (fn, args) {',
  24474. ' return this[fn].apply({',
  24475. ' p: {},',
  24476. ' get: function () {',
  24477. ' return this.p;',
  24478. ' },',
  24479. ' set: function (v) {',
  24480. ' this.p = v;',
  24481. ' }',
  24482. ' }, args);',
  24483. ' };',
  24484. '});',
  24485. 'this.s = {};',
  24486. '']),
  24487. LinesToStr([ // $mod.$main
  24488. '$mod.THelper.DoIt.call({',
  24489. ' p: $mod,',
  24490. ' get: function () {',
  24491. ' return this.p.s;',
  24492. ' },',
  24493. ' set: function (v) {',
  24494. ' this.p.s = v;',
  24495. ' }',
  24496. '}, 123);',
  24497. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  24498. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  24499. ' p: $mod,',
  24500. ' get: function () {',
  24501. ' return this.p.s;',
  24502. ' },',
  24503. ' set: function (v) {',
  24504. ' this.p.s = v;',
  24505. ' }',
  24506. '}, $mod.TEnum.blue));',
  24507. '']));
  24508. end;
  24509. procedure TTestModule.TestTypeHelper_InterfaceType;
  24510. begin
  24511. StartProgram(false);
  24512. Add([
  24513. '{$interfaces com}',
  24514. '{$modeswitch typehelpers}',
  24515. 'type',
  24516. ' IUnknown = interface',
  24517. ' function _AddRef: longint;',
  24518. ' function _Release: longint;',
  24519. ' end;',
  24520. ' TObject = class(IUnknown)',
  24521. ' function _AddRef: longint; virtual; abstract;',
  24522. ' function _Release: longint; virtual; abstract;',
  24523. ' end;',
  24524. ' THelper = type helper for IUnknown',
  24525. ' procedure Fly(e: byte = 123);',
  24526. ' class procedure Run; static;',
  24527. ' end;',
  24528. 'var',
  24529. ' i: IUnknown;',
  24530. ' o: TObject;',
  24531. 'procedure THelper.Fly(e: byte);',
  24532. 'begin',
  24533. ' i:=Self;',
  24534. ' o:=Self as TObject;',
  24535. ' Self:=nil;',
  24536. ' Self:=i;',
  24537. ' Self:=o;',
  24538. ' with Self do begin',
  24539. ' Fly;',
  24540. ' Fly();',
  24541. ' end;',
  24542. 'end;',
  24543. 'class procedure THelper.Run;',
  24544. 'var l: IUnknown;',
  24545. 'begin',
  24546. ' l.Fly;',
  24547. ' l.Fly();',
  24548. 'end;',
  24549. 'begin',
  24550. ' i.Fly;',
  24551. ' i.Fly();',
  24552. ' i.Run;',
  24553. ' i.Run();',
  24554. ' IUnknown.Run;',
  24555. ' IUnknown.Run();',
  24556. '']);
  24557. ConvertProgram;
  24558. CheckSource('TestTypeHelper_InterfaceType',
  24559. LinesToStr([ // statements
  24560. 'rtl.createInterface($mod, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  24561. 'rtl.createClass($mod, "TObject", null, function () {',
  24562. ' this.$init = function () {',
  24563. ' };',
  24564. ' this.$final = function () {',
  24565. ' };',
  24566. ' rtl.addIntf(this, $mod.IUnknown);',
  24567. '});',
  24568. 'rtl.createHelper($mod, "THelper", null, function () {',
  24569. ' this.Fly = function (e) {',
  24570. ' var $ir = rtl.createIntfRefs();',
  24571. ' try {',
  24572. ' rtl.setIntfP($mod, "i", this.get());',
  24573. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  24574. ' this.set(null);',
  24575. ' this.set($mod.i);',
  24576. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  24577. ' var $with = this.get();',
  24578. ' $mod.THelper.Fly.call(this, 123);',
  24579. ' $mod.THelper.Fly.call(this, 123);',
  24580. ' } finally {',
  24581. ' $ir.free();',
  24582. ' };',
  24583. ' };',
  24584. ' this.Run = function () {',
  24585. ' var l = null;',
  24586. ' try {',
  24587. ' $mod.THelper.Fly.call({',
  24588. ' get: function () {',
  24589. ' return l;',
  24590. ' },',
  24591. ' set: function (v) {',
  24592. ' l = rtl.setIntfL(l, v);',
  24593. ' }',
  24594. ' }, 123);',
  24595. ' $mod.THelper.Fly.call({',
  24596. ' get: function () {',
  24597. ' return l;',
  24598. ' },',
  24599. ' set: function (v) {',
  24600. ' l = rtl.setIntfL(l, v);',
  24601. ' }',
  24602. ' }, 123);',
  24603. ' } finally {',
  24604. ' rtl._Release(l);',
  24605. ' };',
  24606. ' };',
  24607. '});',
  24608. 'this.i = null;',
  24609. 'this.o = null;',
  24610. '']),
  24611. LinesToStr([ // $mod.$main
  24612. '$mod.THelper.Fly.call({',
  24613. ' p: $mod,',
  24614. ' get: function () {',
  24615. ' return this.p.i;',
  24616. ' },',
  24617. ' set: function (v) {',
  24618. ' rtl.setIntfP(this.p, "i", v);',
  24619. ' }',
  24620. '}, 123);',
  24621. '$mod.THelper.Fly.call({',
  24622. ' p: $mod,',
  24623. ' get: function () {',
  24624. ' return this.p.i;',
  24625. ' },',
  24626. ' set: function (v) {',
  24627. ' rtl.setIntfP(this.p, "i", v);',
  24628. ' }',
  24629. '}, 123);',
  24630. '$mod.THelper.Run();',
  24631. '$mod.THelper.Run();',
  24632. '$mod.THelper.Run();',
  24633. '$mod.THelper.Run();',
  24634. '']));
  24635. end;
  24636. procedure TTestModule.TestTypeHelper_NestedSelf;
  24637. begin
  24638. StartProgram(false);
  24639. Add([
  24640. '{$modeswitch typehelpers}',
  24641. 'type',
  24642. ' THelper = type helper for string',
  24643. ' procedure Run(Value: string);',
  24644. ' end;',
  24645. 'procedure THelper.Run(Value: string);',
  24646. ' function Sub(i: nativeint): boolean;',
  24647. ' begin',
  24648. ' Result:=Self[i+1]=Value[i];',
  24649. ' end;',
  24650. 'begin',
  24651. ' if Self[3]=Value[4] then ;',
  24652. 'end;',
  24653. 'begin',
  24654. '']);
  24655. ConvertProgram;
  24656. CheckSource('TestTypeHelper_NestedSelf',
  24657. LinesToStr([ // statements
  24658. 'rtl.createHelper($mod, "THelper", null, function () {',
  24659. ' this.Run = function (Value) {',
  24660. ' var $Self = this;',
  24661. ' function Sub(i) {',
  24662. ' var Result = false;',
  24663. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  24664. ' return Result;',
  24665. ' };',
  24666. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  24667. ' };',
  24668. '});',
  24669. '']),
  24670. LinesToStr([ // $mod.$main
  24671. '']));
  24672. end;
  24673. procedure TTestModule.TestProcType;
  24674. begin
  24675. StartProgram(false);
  24676. Add([
  24677. 'type',
  24678. ' TProcInt = procedure(vI: longint = 1);',
  24679. 'procedure DoIt(vJ: longint);',
  24680. 'begin end;',
  24681. 'var',
  24682. ' b: boolean;',
  24683. ' vP, vQ: tprocint;',
  24684. 'begin',
  24685. ' vp:=nil;',
  24686. ' vp:=vp;',
  24687. ' vp:=@doit;',
  24688. ' vp;',
  24689. ' vp();',
  24690. ' vp(2);',
  24691. ' b:=vp=nil;',
  24692. ' b:=nil=vp;',
  24693. ' b:=vp=vq;',
  24694. ' b:=vp=@doit;',
  24695. ' b:=@doit=vp;',
  24696. ' b:=vp<>nil;',
  24697. ' b:=nil<>vp;',
  24698. ' b:=vp<>vq;',
  24699. ' b:=vp<>@doit;',
  24700. ' b:=@doit<>vp;',
  24701. ' b:=Assigned(vp);',
  24702. ' if Assigned(vp) then ;']);
  24703. ConvertProgram;
  24704. CheckSource('TestProcType',
  24705. LinesToStr([ // statements
  24706. 'this.DoIt = function(vJ) {',
  24707. '};',
  24708. 'this.b = false;',
  24709. 'this.vP = null;',
  24710. 'this.vQ = null;'
  24711. ]),
  24712. LinesToStr([ // $mod.$main
  24713. '$mod.vP = null;',
  24714. '$mod.vP = $mod.vP;',
  24715. '$mod.vP = $mod.DoIt;',
  24716. '$mod.vP(1);',
  24717. '$mod.vP(1);',
  24718. '$mod.vP(2);',
  24719. '$mod.b = $mod.vP === null;',
  24720. '$mod.b = null === $mod.vP;',
  24721. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24722. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24723. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24724. '$mod.b = $mod.vP !== null;',
  24725. '$mod.b = null !== $mod.vP;',
  24726. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24727. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24728. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24729. '$mod.b = $mod.vP != null;',
  24730. 'if ($mod.vP != null) ;',
  24731. '']));
  24732. end;
  24733. procedure TTestModule.TestProcType_Arg;
  24734. begin
  24735. StartProgram(false);
  24736. Add([
  24737. 'type',
  24738. ' TProcInt = procedure(vI: longint = 1);',
  24739. 'procedure DoIt(vJ: longint); begin end;',
  24740. 'procedure DoSome(vP, vQ: TProcInt);',
  24741. 'var',
  24742. ' b: boolean;',
  24743. 'begin',
  24744. ' vp:=nil;',
  24745. ' vp:=vp;',
  24746. ' vp:=@doit;',
  24747. ' vp;',
  24748. ' vp();',
  24749. ' vp(2);',
  24750. ' b:=vp=nil;',
  24751. ' b:=nil=vp;',
  24752. ' b:=vp=vq;',
  24753. ' b:=vp=@doit;',
  24754. ' b:=@doit=vp;',
  24755. ' b:=vp<>nil;',
  24756. ' b:=nil<>vp;',
  24757. ' b:=vp<>vq;',
  24758. ' b:=vp<>@doit;',
  24759. ' b:=@doit<>vp;',
  24760. ' b:=Assigned(vp);',
  24761. ' if Assigned(vp) then ;',
  24762. 'end;',
  24763. 'begin',
  24764. ' DoSome(@DoIt,nil);']);
  24765. ConvertProgram;
  24766. CheckSource('TestProcType_Arg',
  24767. LinesToStr([ // statements
  24768. 'this.DoIt = function(vJ) {',
  24769. '};',
  24770. 'this.DoSome = function(vP, vQ) {',
  24771. ' var b = false;',
  24772. ' vP = null;',
  24773. ' vP = vP;',
  24774. ' vP = $mod.DoIt;',
  24775. ' vP(1);',
  24776. ' vP(1);',
  24777. ' vP(2);',
  24778. ' b = vP === null;',
  24779. ' b = null === vP;',
  24780. ' b = rtl.eqCallback(vP,vQ);',
  24781. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  24782. ' b = rtl.eqCallback($mod.DoIt, vP);',
  24783. ' b = vP !== null;',
  24784. ' b = null !== vP;',
  24785. ' b = !rtl.eqCallback(vP, vQ);',
  24786. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  24787. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  24788. ' b = vP != null;',
  24789. ' if (vP != null) ;',
  24790. '};',
  24791. '']),
  24792. LinesToStr([ // $mod.$main
  24793. '$mod.DoSome($mod.DoIt,null);',
  24794. '']));
  24795. end;
  24796. procedure TTestModule.TestProcType_FunctionFPC;
  24797. begin
  24798. StartProgram(false);
  24799. Add('type');
  24800. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24801. Add('function DoIt(vI: longint): longint;');
  24802. Add('begin end;');
  24803. Add('var');
  24804. Add(' b: boolean;');
  24805. Add(' vP, vQ: tfuncint;');
  24806. Add('begin');
  24807. Add(' vp:=nil;');
  24808. Add(' vp:=vp;');
  24809. Add(' vp:=@doit;'); // ok in fpc and delphi
  24810. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24811. Add(' vp;'); // ok in fpc and delphi
  24812. Add(' vp();');
  24813. Add(' vp(2);');
  24814. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24815. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24816. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24817. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24818. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24819. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  24820. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  24821. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24822. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24823. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24824. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24825. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24826. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  24827. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  24828. Add(' b:=Assigned(vp);');
  24829. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  24830. Add(' doit(vp());'); // ok in fpc and delphi
  24831. Add(' doit(vp(2));'); // ok in fpc and delphi
  24832. ConvertProgram;
  24833. CheckSource('TestProcType_FunctionFPC',
  24834. LinesToStr([ // statements
  24835. 'this.DoIt = function(vI) {',
  24836. ' var Result = 0;',
  24837. ' return Result;',
  24838. '};',
  24839. 'this.b = false;',
  24840. 'this.vP = null;',
  24841. 'this.vQ = null;'
  24842. ]),
  24843. LinesToStr([ // $mod.$main
  24844. '$mod.vP = null;',
  24845. '$mod.vP = $mod.vP;',
  24846. '$mod.vP = $mod.DoIt;',
  24847. '$mod.vP(1);',
  24848. '$mod.vP(1);',
  24849. '$mod.vP(2);',
  24850. '$mod.b = $mod.vP === null;',
  24851. '$mod.b = null === $mod.vP;',
  24852. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  24853. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24854. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  24855. '$mod.b = 4 === $mod.vP(1);',
  24856. '$mod.b = $mod.vP !== null;',
  24857. '$mod.b = null !== $mod.vP;',
  24858. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  24859. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  24860. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  24861. '$mod.b = 6 !== $mod.vP(1);',
  24862. '$mod.b = $mod.vP != null;',
  24863. '$mod.DoIt($mod.vP(1));',
  24864. '$mod.DoIt($mod.vP(2));',
  24865. '']));
  24866. end;
  24867. procedure TTestModule.TestProcType_FunctionDelphi;
  24868. begin
  24869. StartProgram(false);
  24870. Add('{$mode Delphi}');
  24871. Add('type');
  24872. Add(' TFuncInt = function(vA: longint = 1): longint;');
  24873. Add('function DoIt(vI: longint): longint;');
  24874. Add('begin end;');
  24875. Add('var');
  24876. Add(' b: boolean;');
  24877. Add(' vP, vQ: tfuncint;');
  24878. Add('begin');
  24879. Add(' vp:=nil;');
  24880. Add(' vp:=vp;');
  24881. Add(' vp:=@doit;'); // ok in fpc and delphi
  24882. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24883. Add(' vp;'); // ok in fpc and delphi
  24884. Add(' vp();');
  24885. Add(' vp(2);');
  24886. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24887. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24888. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24889. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24890. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24891. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  24892. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  24893. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24894. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24895. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24896. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24897. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24898. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  24899. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  24900. Add(' b:=Assigned(vp);');
  24901. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  24902. Add(' doit(vp());'); // ok in fpc and delphi
  24903. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  24904. ConvertProgram;
  24905. CheckSource('TestProcType_FunctionDelphi',
  24906. LinesToStr([ // statements
  24907. 'this.DoIt = function(vI) {',
  24908. ' var Result = 0;',
  24909. ' return Result;',
  24910. '};',
  24911. 'this.b = false;',
  24912. 'this.vP = null;',
  24913. 'this.vQ = null;'
  24914. ]),
  24915. LinesToStr([ // $mod.$main
  24916. '$mod.vP = null;',
  24917. '$mod.vP = $mod.vP;',
  24918. '$mod.vP = $mod.DoIt;',
  24919. '$mod.vP = $mod.DoIt;',
  24920. '$mod.vP(1);',
  24921. '$mod.vP(1);',
  24922. '$mod.vP(2);',
  24923. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  24924. '$mod.b = $mod.vP(1) === 3;',
  24925. '$mod.b = 4 === $mod.vP(1);',
  24926. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  24927. '$mod.b = $mod.vP(1) !== 5;',
  24928. '$mod.b = 6 !== $mod.vP(1);',
  24929. '$mod.b = $mod.vP != null;',
  24930. '$mod.DoIt($mod.vP(1));',
  24931. '$mod.DoIt($mod.vP(1));',
  24932. '$mod.DoIt($mod.vP(2));',
  24933. '']));
  24934. end;
  24935. procedure TTestModule.TestProcType_ProcedureDelphi;
  24936. begin
  24937. StartProgram(false);
  24938. Add('{$mode Delphi}');
  24939. Add('type');
  24940. Add(' TProc = procedure;');
  24941. Add('procedure DoIt;');
  24942. Add('begin end;');
  24943. Add('var');
  24944. Add(' b: boolean;');
  24945. Add(' vP, vQ: tproc;');
  24946. Add('begin');
  24947. Add(' vp:=nil;');
  24948. Add(' vp:=vp;');
  24949. Add(' vp:=vq;');
  24950. 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
  24951. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  24952. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  24953. Add(' vp;'); // ok in fpc and delphi
  24954. Add(' vp();');
  24955. // equal
  24956. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  24957. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  24958. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  24959. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  24960. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  24961. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  24962. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  24963. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  24964. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  24965. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  24966. // unequal
  24967. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  24968. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  24969. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  24970. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  24971. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  24972. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  24973. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  24974. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  24975. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  24976. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  24977. Add(' b:=Assigned(vp);');
  24978. ConvertProgram;
  24979. CheckSource('TestProcType_ProcedureDelphi',
  24980. LinesToStr([ // statements
  24981. 'this.DoIt = function() {',
  24982. '};',
  24983. 'this.b = false;',
  24984. 'this.vP = null;',
  24985. 'this.vQ = null;'
  24986. ]),
  24987. LinesToStr([ // $mod.$main
  24988. '$mod.vP = null;',
  24989. '$mod.vP = $mod.vP;',
  24990. '$mod.vP = $mod.vQ;',
  24991. '$mod.vP = $mod.DoIt;',
  24992. '$mod.vP = $mod.DoIt;',
  24993. '$mod.vP();',
  24994. '$mod.vP();',
  24995. '$mod.b = $mod.vP === null;',
  24996. '$mod.b = null === $mod.vP;',
  24997. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  24998. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  24999. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25000. '$mod.b = $mod.vP !== null;',
  25001. '$mod.b = null !== $mod.vP;',
  25002. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25003. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25004. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25005. '$mod.b = $mod.vP != null;',
  25006. '']));
  25007. end;
  25008. procedure TTestModule.TestProcType_AsParam;
  25009. begin
  25010. StartProgram(false);
  25011. Add('type');
  25012. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25013. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25014. Add('var vJ: tfuncint;');
  25015. Add('begin');
  25016. Add(' vg:=vg;');
  25017. Add(' vj:=vh;');
  25018. Add(' vi:=vi;');
  25019. Add(' doit(vg,vg,vg);');
  25020. Add(' doit(vh,vh,vj);');
  25021. Add(' doit(vi,vi,vi);');
  25022. Add(' doit(vj,vj,vj);');
  25023. Add('end;');
  25024. Add('var i: tfuncint;');
  25025. Add('begin');
  25026. Add(' doit(i,i,i);');
  25027. ConvertProgram;
  25028. CheckSource('TestProcType_AsParam',
  25029. LinesToStr([ // statements
  25030. 'this.DoIt = function (vG,vH,vI) {',
  25031. ' var vJ = null;',
  25032. ' vG = vG;',
  25033. ' vJ = vH;',
  25034. ' vI.set(vI.get());',
  25035. ' $mod.DoIt(vG, vG, {',
  25036. ' get: function () {',
  25037. ' return vG;',
  25038. ' },',
  25039. ' set: function (v) {',
  25040. ' vG = v;',
  25041. ' }',
  25042. ' });',
  25043. ' $mod.DoIt(vH, vH, {',
  25044. ' get: function () {',
  25045. ' return vJ;',
  25046. ' },',
  25047. ' set: function (v) {',
  25048. ' vJ = v;',
  25049. ' }',
  25050. ' });',
  25051. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25052. ' $mod.DoIt(vJ, vJ, {',
  25053. ' get: function () {',
  25054. ' return vJ;',
  25055. ' },',
  25056. ' set: function (v) {',
  25057. ' vJ = v;',
  25058. ' }',
  25059. ' });',
  25060. '};',
  25061. 'this.i = null;'
  25062. ]),
  25063. LinesToStr([
  25064. '$mod.DoIt($mod.i,$mod.i,{',
  25065. ' p: $mod,',
  25066. ' get: function () {',
  25067. ' return this.p.i;',
  25068. ' },',
  25069. ' set: function (v) {',
  25070. ' this.p.i = v;',
  25071. ' }',
  25072. '});'
  25073. ]));
  25074. end;
  25075. procedure TTestModule.TestProcType_MethodFPC;
  25076. begin
  25077. StartProgram(false);
  25078. Add('type');
  25079. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25080. Add(' TObject = class');
  25081. Add(' function DoIt(vA: longint = 1): longint;');
  25082. Add(' end;');
  25083. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25084. Add('begin');
  25085. Add('end;');
  25086. Add('var');
  25087. Add(' Obj: TObject;');
  25088. Add(' vP: tfuncint;');
  25089. Add(' b: boolean;');
  25090. Add('begin');
  25091. Add(' vp:[email protected];'); // ok in fpc and delphi
  25092. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25093. Add(' vp;'); // ok in fpc and delphi
  25094. Add(' vp();');
  25095. Add(' vp(2);');
  25096. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25097. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25098. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25099. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25100. ConvertProgram;
  25101. CheckSource('TestProcType_MethodFPC',
  25102. LinesToStr([ // statements
  25103. 'rtl.createClass($mod, "TObject", null, function () {',
  25104. ' this.$init = function () {',
  25105. ' };',
  25106. ' this.$final = function () {',
  25107. ' };',
  25108. ' this.DoIt = function (vA) {',
  25109. ' var Result = 0;',
  25110. ' return Result;',
  25111. ' };',
  25112. '});',
  25113. 'this.Obj = null;',
  25114. 'this.vP = null;',
  25115. 'this.b = false;'
  25116. ]),
  25117. LinesToStr([
  25118. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25119. '$mod.vP(1);',
  25120. '$mod.vP(1);',
  25121. '$mod.vP(2);',
  25122. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25123. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25124. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25125. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25126. '']));
  25127. end;
  25128. procedure TTestModule.TestProcType_MethodDelphi;
  25129. begin
  25130. StartProgram(false);
  25131. Add([
  25132. '{$mode delphi}',
  25133. 'type',
  25134. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25135. ' TObject = class',
  25136. ' function DoIt(vA: longint = 1): longint;',
  25137. ' end;',
  25138. 'function TObject.DoIt(vA: longint = 1): longint;',
  25139. 'begin',
  25140. 'end;',
  25141. 'var',
  25142. ' Obj: TObject;',
  25143. ' vP: tfuncint;',
  25144. ' b: boolean;',
  25145. 'begin',
  25146. ' vp:[email protected];', // ok in fpc and delphi
  25147. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25148. ' vp;', // ok in fpc and delphi
  25149. ' vp();',
  25150. ' vp(2);',
  25151. //' b:[email protected];', // ok in fpc, illegal in delphi
  25152. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25153. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25154. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25155. '']);
  25156. ConvertProgram;
  25157. CheckSource('TestProcType_MethodDelphi',
  25158. LinesToStr([ // statements
  25159. 'rtl.createClass($mod, "TObject", null, function () {',
  25160. ' this.$init = function () {',
  25161. ' };',
  25162. ' this.$final = function () {',
  25163. ' };',
  25164. ' this.DoIt = function (vA) {',
  25165. ' var Result = 0;',
  25166. ' return Result;',
  25167. ' };',
  25168. '});',
  25169. 'this.Obj = null;',
  25170. 'this.vP = null;',
  25171. 'this.b = false;'
  25172. ]),
  25173. LinesToStr([
  25174. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25175. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25176. '$mod.vP(1);',
  25177. '$mod.vP(1);',
  25178. '$mod.vP(2);',
  25179. '']));
  25180. end;
  25181. procedure TTestModule.TestProcType_PropertyFPC;
  25182. begin
  25183. StartProgram(false);
  25184. Add('type');
  25185. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25186. Add(' TObject = class');
  25187. Add(' FOnFoo: TFuncInt;');
  25188. Add(' function DoIt(vA: longint = 1): longint;');
  25189. Add(' function GetFoo: TFuncInt;');
  25190. Add(' procedure SetFoo(const Value: TFuncInt);');
  25191. Add(' function GetEvents(Index: longint): TFuncInt;');
  25192. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25193. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25194. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25195. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25196. Add(' end;');
  25197. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25198. Add('function tobject.getfoo: tfuncint; begin end;');
  25199. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25200. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25201. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25202. Add('var');
  25203. Add(' Obj: TObject;');
  25204. Add(' vP: tfuncint;');
  25205. Add(' b: boolean;');
  25206. Add('begin');
  25207. Add(' obj.onfoo:=nil;');
  25208. Add(' obj.onbar:=nil;');
  25209. Add(' obj.events[1]:=nil;');
  25210. Add(' obj.onfoo:=obj.onfoo;');
  25211. Add(' obj.onbar:=obj.onbar;');
  25212. Add(' obj.events[2]:=obj.events[3];');
  25213. Add(' obj.onfoo:[email protected];');
  25214. Add(' obj.onbar:[email protected];');
  25215. Add(' obj.events[4]:[email protected];');
  25216. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25217. //Add(' obj.onbar:=obj.doit;'); // delphi
  25218. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25219. Add(' obj.onfoo;');
  25220. Add(' obj.onbar;');
  25221. //Add(' obj.events[5];'); ToDo in pasresolver
  25222. Add(' obj.onfoo();');
  25223. Add(' obj.onbar();');
  25224. Add(' obj.events[6]();');
  25225. Add(' b:=obj.onfoo=nil;');
  25226. Add(' b:=obj.onbar=nil;');
  25227. Add(' b:=obj.events[7]=nil;');
  25228. Add(' b:=obj.onfoo<>nil;');
  25229. Add(' b:=obj.onbar<>nil;');
  25230. Add(' b:=obj.events[8]<>nil;');
  25231. Add(' b:=obj.onfoo=vp;');
  25232. Add(' b:=obj.onbar=vp;');
  25233. Add(' b:=obj.events[9]=vp;');
  25234. Add(' b:=obj.onfoo=obj.onfoo;');
  25235. Add(' b:=obj.onbar=obj.onfoo;');
  25236. Add(' b:=obj.events[10]=obj.onfoo;');
  25237. Add(' b:=obj.onfoo<>obj.onfoo;');
  25238. Add(' b:=obj.onbar<>obj.onfoo;');
  25239. Add(' b:=obj.events[11]<>obj.onfoo;');
  25240. Add(' b:[email protected];');
  25241. Add(' b:[email protected];');
  25242. Add(' b:=obj.events[12][email protected];');
  25243. Add(' b:=obj.onfoo<>@obj.doit;');
  25244. Add(' b:=obj.onbar<>@obj.doit;');
  25245. Add(' b:=obj.events[12]<>@obj.doit;');
  25246. Add(' b:=Assigned(obj.onfoo);');
  25247. Add(' b:=Assigned(obj.onbar);');
  25248. Add(' b:=Assigned(obj.events[13]);');
  25249. ConvertProgram;
  25250. CheckSource('TestProcType_PropertyFPC',
  25251. LinesToStr([ // statements
  25252. 'rtl.createClass($mod, "TObject", null, function () {',
  25253. ' this.$init = function () {',
  25254. ' this.FOnFoo = null;',
  25255. ' };',
  25256. ' this.$final = function () {',
  25257. ' this.FOnFoo = undefined;',
  25258. ' };',
  25259. ' this.DoIt = function (vA) {',
  25260. ' var Result = 0;',
  25261. ' return Result;',
  25262. ' };',
  25263. 'this.GetFoo = function () {',
  25264. ' var Result = null;',
  25265. ' return Result;',
  25266. '};',
  25267. 'this.SetFoo = function (Value) {',
  25268. '};',
  25269. 'this.GetEvents = function (Index) {',
  25270. ' var Result = null;',
  25271. ' return Result;',
  25272. '};',
  25273. 'this.SetEvents = function (Index, Value) {',
  25274. '};',
  25275. '});',
  25276. 'this.Obj = null;',
  25277. 'this.vP = null;',
  25278. 'this.b = false;'
  25279. ]),
  25280. LinesToStr([
  25281. '$mod.Obj.FOnFoo = null;',
  25282. '$mod.Obj.SetFoo(null);',
  25283. '$mod.Obj.SetEvents(1, null);',
  25284. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25285. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25286. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25287. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25288. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25289. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25290. '$mod.Obj.FOnFoo(1);',
  25291. '$mod.Obj.GetFoo();',
  25292. '$mod.Obj.FOnFoo(1);',
  25293. '$mod.Obj.GetFoo()(1);',
  25294. '$mod.Obj.GetEvents(6)(1);',
  25295. '$mod.b = $mod.Obj.FOnFoo === null;',
  25296. '$mod.b = $mod.Obj.GetFoo() === null;',
  25297. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25298. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25299. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25300. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25301. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25302. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25303. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25304. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25305. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25306. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25307. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25308. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25309. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25310. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25311. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25312. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25313. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25314. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25315. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25316. '$mod.b = $mod.Obj.FOnFoo != null;',
  25317. '$mod.b = $mod.Obj.GetFoo() != null;',
  25318. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25319. '']));
  25320. end;
  25321. procedure TTestModule.TestProcType_PropertyDelphi;
  25322. begin
  25323. StartProgram(false);
  25324. Add('{$mode delphi}');
  25325. Add('type');
  25326. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25327. Add(' TObject = class');
  25328. Add(' FOnFoo: TFuncInt;');
  25329. Add(' function DoIt(vA: longint = 1): longint;');
  25330. Add(' function GetFoo: TFuncInt;');
  25331. Add(' procedure SetFoo(const Value: TFuncInt);');
  25332. Add(' function GetEvents(Index: longint): TFuncInt;');
  25333. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25334. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25335. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25336. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25337. Add(' end;');
  25338. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25339. Add('function tobject.getfoo: tfuncint; begin end;');
  25340. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25341. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25342. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25343. Add('var');
  25344. Add(' Obj: TObject;');
  25345. Add(' vP: tfuncint;');
  25346. Add(' b: boolean;');
  25347. Add('begin');
  25348. Add(' obj.onfoo:=nil;');
  25349. Add(' obj.onbar:=nil;');
  25350. Add(' obj.events[1]:=nil;');
  25351. Add(' obj.onfoo:=obj.onfoo;');
  25352. Add(' obj.onbar:=obj.onbar;');
  25353. Add(' obj.events[2]:=obj.events[3];');
  25354. Add(' obj.onfoo:[email protected];');
  25355. Add(' obj.onbar:[email protected];');
  25356. Add(' obj.events[4]:[email protected];');
  25357. Add(' obj.onfoo:=obj.doit;'); // delphi
  25358. Add(' obj.onbar:=obj.doit;'); // delphi
  25359. Add(' obj.events[4]:=obj.doit;'); // delphi
  25360. Add(' obj.onfoo;');
  25361. Add(' obj.onbar;');
  25362. //Add(' obj.events[5];'); ToDo in pasresolver
  25363. Add(' obj.onfoo();');
  25364. Add(' obj.onbar();');
  25365. Add(' obj.events[6]();');
  25366. //Add(' b:=obj.onfoo=nil;'); // fpc
  25367. //Add(' b:=obj.onbar=nil;'); // fpc
  25368. //Add(' b:=obj.events[7]=nil;'); // fpc
  25369. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25370. //Add(' b:=obj.onbar<>nil;'); // fpc
  25371. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25372. Add(' b:=obj.onfoo=vp;');
  25373. Add(' b:=obj.onbar=vp;');
  25374. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25375. Add(' b:=obj.onfoo=obj.onfoo;');
  25376. Add(' b:=obj.onbar=obj.onfoo;');
  25377. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25378. Add(' b:=obj.onfoo<>obj.onfoo;');
  25379. Add(' b:=obj.onbar<>obj.onfoo;');
  25380. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25381. //Add(' b:[email protected];'); // fpc
  25382. //Add(' b:[email protected];'); // fpc
  25383. //Add(' b:=obj.events[12][email protected];'); // fpc
  25384. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25385. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25386. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25387. Add(' b:=Assigned(obj.onfoo);');
  25388. Add(' b:=Assigned(obj.onbar);');
  25389. Add(' b:=Assigned(obj.events[13]);');
  25390. ConvertProgram;
  25391. CheckSource('TestProcType_PropertyDelphi',
  25392. LinesToStr([ // statements
  25393. 'rtl.createClass($mod, "TObject", null, function () {',
  25394. ' this.$init = function () {',
  25395. ' this.FOnFoo = null;',
  25396. ' };',
  25397. ' this.$final = function () {',
  25398. ' this.FOnFoo = undefined;',
  25399. ' };',
  25400. ' this.DoIt = function (vA) {',
  25401. ' var Result = 0;',
  25402. ' return Result;',
  25403. ' };',
  25404. 'this.GetFoo = function () {',
  25405. ' var Result = null;',
  25406. ' return Result;',
  25407. '};',
  25408. 'this.SetFoo = function (Value) {',
  25409. '};',
  25410. 'this.GetEvents = function (Index) {',
  25411. ' var Result = null;',
  25412. ' return Result;',
  25413. '};',
  25414. 'this.SetEvents = function (Index, Value) {',
  25415. '};',
  25416. '});',
  25417. 'this.Obj = null;',
  25418. 'this.vP = null;',
  25419. 'this.b = false;'
  25420. ]),
  25421. LinesToStr([
  25422. '$mod.Obj.FOnFoo = null;',
  25423. '$mod.Obj.SetFoo(null);',
  25424. '$mod.Obj.SetEvents(1, null);',
  25425. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25426. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25427. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25428. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25429. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25430. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25431. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25432. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25433. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25434. '$mod.Obj.FOnFoo(1);',
  25435. '$mod.Obj.GetFoo();',
  25436. '$mod.Obj.FOnFoo(1);',
  25437. '$mod.Obj.GetFoo()(1);',
  25438. '$mod.Obj.GetEvents(6)(1);',
  25439. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  25440. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  25441. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  25442. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  25443. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  25444. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  25445. '$mod.b = $mod.Obj.FOnFoo != null;',
  25446. '$mod.b = $mod.Obj.GetFoo() != null;',
  25447. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25448. '']));
  25449. end;
  25450. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  25451. begin
  25452. StartProgram(false);
  25453. Add('type');
  25454. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25455. Add(' TObject = class');
  25456. Add(' FOnFoo: TFuncInt;');
  25457. Add(' function DoIt(vA: longint = 1): longint;');
  25458. Add(' function GetFoo: TFuncInt;');
  25459. Add(' procedure SetFoo(const Value: TFuncInt);');
  25460. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25461. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25462. Add(' end;');
  25463. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25464. Add('function tobject.getfoo: tfuncint; begin end;');
  25465. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25466. Add('var');
  25467. Add(' Obj: TObject;');
  25468. Add(' vP: tfuncint;');
  25469. Add(' b: boolean;');
  25470. Add('begin');
  25471. Add('with obj do begin');
  25472. Add(' fonfoo:=nil;');
  25473. Add(' onfoo:=nil;');
  25474. Add(' onbar:=nil;');
  25475. Add(' fonfoo:=fonfoo;');
  25476. Add(' onfoo:=onfoo;');
  25477. Add(' onbar:=onbar;');
  25478. Add(' fonfoo:=@doit;');
  25479. Add(' onfoo:=@doit;');
  25480. Add(' onbar:=@doit;');
  25481. //Add(' fonfoo:=doit;'); // delphi
  25482. //Add(' onfoo:=doit;'); // delphi
  25483. //Add(' onbar:=doit;'); // delphi
  25484. Add(' fonfoo;');
  25485. Add(' onfoo;');
  25486. Add(' onbar;');
  25487. Add(' fonfoo();');
  25488. Add(' onfoo();');
  25489. Add(' onbar();');
  25490. Add(' b:=fonfoo=nil;');
  25491. Add(' b:=onfoo=nil;');
  25492. Add(' b:=onbar=nil;');
  25493. Add(' b:=fonfoo<>nil;');
  25494. Add(' b:=onfoo<>nil;');
  25495. Add(' b:=onbar<>nil;');
  25496. Add(' b:=fonfoo=vp;');
  25497. Add(' b:=onfoo=vp;');
  25498. Add(' b:=onbar=vp;');
  25499. Add(' b:=fonfoo=fonfoo;');
  25500. Add(' b:=onfoo=onfoo;');
  25501. Add(' b:=onbar=onfoo;');
  25502. Add(' b:=fonfoo<>fonfoo;');
  25503. Add(' b:=onfoo<>onfoo;');
  25504. Add(' b:=onbar<>onfoo;');
  25505. Add(' b:=fonfoo=@doit;');
  25506. Add(' b:=onfoo=@doit;');
  25507. Add(' b:=onbar=@doit;');
  25508. Add(' b:=fonfoo<>@doit;');
  25509. Add(' b:=onfoo<>@doit;');
  25510. Add(' b:=onbar<>@doit;');
  25511. Add(' b:=Assigned(fonfoo);');
  25512. Add(' b:=Assigned(onfoo);');
  25513. Add(' b:=Assigned(onbar);');
  25514. Add('end;');
  25515. ConvertProgram;
  25516. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  25517. LinesToStr([ // statements
  25518. 'rtl.createClass($mod, "TObject", null, function () {',
  25519. ' this.$init = function () {',
  25520. ' this.FOnFoo = null;',
  25521. ' };',
  25522. ' this.$final = function () {',
  25523. ' this.FOnFoo = undefined;',
  25524. ' };',
  25525. ' this.DoIt = function (vA) {',
  25526. ' var Result = 0;',
  25527. ' return Result;',
  25528. ' };',
  25529. ' this.GetFoo = function () {',
  25530. ' var Result = null;',
  25531. ' return Result;',
  25532. ' };',
  25533. ' this.SetFoo = function (Value) {',
  25534. ' };',
  25535. '});',
  25536. 'this.Obj = null;',
  25537. 'this.vP = null;',
  25538. 'this.b = false;'
  25539. ]),
  25540. LinesToStr([
  25541. 'var $with = $mod.Obj;',
  25542. '$with.FOnFoo = null;',
  25543. '$with.FOnFoo = null;',
  25544. '$with.SetFoo(null);',
  25545. '$with.FOnFoo = $with.FOnFoo;',
  25546. '$with.FOnFoo = $with.FOnFoo;',
  25547. '$with.SetFoo($with.GetFoo());',
  25548. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25549. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25550. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  25551. '$with.FOnFoo(1);',
  25552. '$with.FOnFoo(1);',
  25553. '$with.GetFoo();',
  25554. '$with.FOnFoo(1);',
  25555. '$with.FOnFoo(1);',
  25556. '$with.GetFoo()(1);',
  25557. '$mod.b = $with.FOnFoo === null;',
  25558. '$mod.b = $with.FOnFoo === null;',
  25559. '$mod.b = $with.GetFoo() === null;',
  25560. '$mod.b = $with.FOnFoo !== null;',
  25561. '$mod.b = $with.FOnFoo !== null;',
  25562. '$mod.b = $with.GetFoo() !== null;',
  25563. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25564. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25565. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  25566. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25567. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25568. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25569. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25570. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25571. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25572. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25573. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25574. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25575. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25576. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25577. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25578. '$mod.b = $with.FOnFoo != null;',
  25579. '$mod.b = $with.FOnFoo != null;',
  25580. '$mod.b = $with.GetFoo() != null;',
  25581. '']));
  25582. end;
  25583. procedure TTestModule.TestProcType_Nested;
  25584. begin
  25585. StartProgram(false);
  25586. Add([
  25587. 'type',
  25588. ' TProcInt = procedure(vI: longint = 1);',
  25589. 'procedure DoIt(vJ: longint);',
  25590. 'var aProc: TProcInt;',
  25591. ' b: boolean;',
  25592. ' procedure Sub(vK: longint);',
  25593. ' var aSub: TProcInt;',
  25594. ' procedure SubSub(vK: longint);',
  25595. ' var aSubSub: TProcInt;',
  25596. ' begin;',
  25597. ' aProc:=@DoIt;',
  25598. ' aSub:=@DoIt;',
  25599. ' aSubSub:=@DoIt;',
  25600. ' aProc:=@Sub;',
  25601. ' aSub:=@Sub;',
  25602. ' aSubSub:=@Sub;',
  25603. ' aProc:=@SubSub;',
  25604. ' aSub:=@SubSub;',
  25605. ' aSubSub:=@SubSub;',
  25606. ' end;',
  25607. ' begin;',
  25608. ' end;',
  25609. 'begin;',
  25610. ' aProc:=@Sub;',
  25611. ' b:=aProc=@Sub;',
  25612. ' b:=@Sub=aProc;',
  25613. 'end;',
  25614. 'begin',
  25615. '']);
  25616. ConvertProgram;
  25617. CheckSource('TestProcType_Nested',
  25618. LinesToStr([ // statements
  25619. 'this.DoIt = function (vJ) {',
  25620. ' var aProc = null;',
  25621. ' var b = false;',
  25622. ' function Sub(vK) {',
  25623. ' var aSub = null;',
  25624. ' function SubSub(vK) {',
  25625. ' var aSubSub = null;',
  25626. ' aProc = $mod.DoIt;',
  25627. ' aSub = $mod.DoIt;',
  25628. ' aSubSub = $mod.DoIt;',
  25629. ' aProc = Sub;',
  25630. ' aSub = Sub;',
  25631. ' aSubSub = Sub;',
  25632. ' aProc = SubSub;',
  25633. ' aSub = SubSub;',
  25634. ' aSubSub = SubSub;',
  25635. ' };',
  25636. ' };',
  25637. ' aProc = Sub;',
  25638. ' b = rtl.eqCallback(aProc, Sub);',
  25639. ' b = rtl.eqCallback(Sub, aProc);',
  25640. '};',
  25641. '']),
  25642. LinesToStr([ // $mod.$main
  25643. '']));
  25644. end;
  25645. procedure TTestModule.TestProcType_NestedOfObject;
  25646. begin
  25647. StartProgram(false);
  25648. Add([
  25649. 'type',
  25650. ' TProcInt = procedure(vI: longint = 1) of object;',
  25651. ' TObject = class',
  25652. ' procedure DoIt(vJ: longint);',
  25653. ' end;',
  25654. 'procedure TObject.DoIt(vJ: longint);',
  25655. 'var aProc: TProcInt;',
  25656. ' b: boolean;',
  25657. ' procedure Sub(vK: longint);',
  25658. ' var aSub: TProcInt;',
  25659. ' procedure SubSub(vK: longint);',
  25660. ' var aSubSub: TProcInt;',
  25661. ' begin;',
  25662. ' aProc:=@DoIt;',
  25663. ' aSub:=@DoIt;',
  25664. ' aSubSub:=@DoIt;',
  25665. ' aProc:=@Sub;',
  25666. ' aSub:=@Sub;',
  25667. ' aSubSub:=@Sub;',
  25668. ' aProc:=@SubSub;',
  25669. ' aSub:=@SubSub;',
  25670. ' aSubSub:=@SubSub;',
  25671. ' end;',
  25672. ' begin;',
  25673. ' end;',
  25674. 'begin;',
  25675. ' aProc:=@Sub;',
  25676. ' b:=aProc=@Sub;',
  25677. ' b:=@Sub=aProc;',
  25678. 'end;',
  25679. 'begin',
  25680. '']);
  25681. ConvertProgram;
  25682. CheckSource('TestProcType_Nested',
  25683. LinesToStr([ // statements
  25684. 'rtl.createClass($mod, "TObject", null, function () {',
  25685. ' this.$init = function () {',
  25686. ' };',
  25687. ' this.$final = function () {',
  25688. ' };',
  25689. ' this.DoIt = function (vJ) {',
  25690. ' var $Self = this;',
  25691. ' var aProc = null;',
  25692. ' var b = false;',
  25693. ' function Sub(vK) {',
  25694. ' var aSub = null;',
  25695. ' function SubSub(vK) {',
  25696. ' var aSubSub = null;',
  25697. ' aProc = rtl.createCallback($Self, "DoIt");',
  25698. ' aSub = rtl.createCallback($Self, "DoIt");',
  25699. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  25700. ' aProc = Sub;',
  25701. ' aSub = Sub;',
  25702. ' aSubSub = Sub;',
  25703. ' aProc = SubSub;',
  25704. ' aSub = SubSub;',
  25705. ' aSubSub = SubSub;',
  25706. ' };',
  25707. ' };',
  25708. ' aProc = Sub;',
  25709. ' b = rtl.eqCallback(aProc, Sub);',
  25710. ' b = rtl.eqCallback(Sub, aProc);',
  25711. ' };',
  25712. '});',
  25713. '']),
  25714. LinesToStr([ // $mod.$main
  25715. '']));
  25716. end;
  25717. procedure TTestModule.TestProcType_ReferenceToProc;
  25718. begin
  25719. StartProgram(false);
  25720. Add([
  25721. 'type',
  25722. ' TProcRef = reference to procedure(i: longint = 0);',
  25723. ' TFuncRef = reference to function(i: longint = 0): longint;',
  25724. 'var',
  25725. ' p: TProcRef;',
  25726. ' f: TFuncRef;',
  25727. 'procedure DoIt(i: longint);',
  25728. 'begin',
  25729. 'end;',
  25730. 'function GetIt(i: longint): longint;',
  25731. 'begin',
  25732. ' p:=@DoIt;',
  25733. ' f:=@GetIt;',
  25734. ' f;',
  25735. ' f();',
  25736. ' f(1);',
  25737. 'end;',
  25738. 'begin',
  25739. ' p:=@DoIt;',
  25740. ' f:=@GetIt;',
  25741. ' f;',
  25742. ' f();',
  25743. ' f(1);',
  25744. ' p:=TProcRef(f);',
  25745. '']);
  25746. ConvertProgram;
  25747. CheckSource('TestProcType_ReferenceToProc',
  25748. LinesToStr([ // statements
  25749. 'this.p = null;',
  25750. 'this.f = null;',
  25751. 'this.DoIt = function (i) {',
  25752. '};',
  25753. 'this.GetIt = function (i) {',
  25754. ' var Result = 0;',
  25755. ' $mod.p = $mod.DoIt;',
  25756. ' $mod.f = $mod.GetIt;',
  25757. ' $mod.f(0);',
  25758. ' $mod.f(0);',
  25759. ' $mod.f(1);',
  25760. ' return Result;',
  25761. '};',
  25762. '']),
  25763. LinesToStr([ // $mod.$main
  25764. '$mod.p = $mod.DoIt;',
  25765. '$mod.f = $mod.GetIt;',
  25766. '$mod.f(0);',
  25767. '$mod.f(0);',
  25768. '$mod.f(1);',
  25769. '$mod.p = $mod.f;',
  25770. '']));
  25771. end;
  25772. procedure TTestModule.TestProcType_ReferenceToMethod;
  25773. begin
  25774. StartProgram(false);
  25775. Add([
  25776. 'type',
  25777. ' TFuncRef = reference to function(i: longint = 5): longint;',
  25778. ' TObject = class',
  25779. ' function Grow(s: longint): longint;',
  25780. ' end;',
  25781. 'var',
  25782. ' f: tfuncref;',
  25783. 'function tobject.grow(s: longint): longint;',
  25784. ' function GrowSub(i: longint): longint;',
  25785. ' begin',
  25786. ' f:=@grow;',
  25787. ' f:=@growsub;',
  25788. ' end;',
  25789. 'begin',
  25790. ' f:=@grow;',
  25791. ' f:=@growsub;',
  25792. 'end;',
  25793. 'begin',
  25794. '']);
  25795. ConvertProgram;
  25796. CheckSource('TestProcType_ReferenceToMethod',
  25797. LinesToStr([ // statements
  25798. 'rtl.createClass($mod, "TObject", null, function () {',
  25799. ' this.$init = function () {',
  25800. ' };',
  25801. ' this.$final = function () {',
  25802. ' };',
  25803. ' this.Grow = function (s) {',
  25804. ' var $Self = this;',
  25805. ' var Result = 0;',
  25806. ' function GrowSub(i) {',
  25807. ' var Result = 0;',
  25808. ' $mod.f = rtl.createCallback($Self, "Grow");',
  25809. ' $mod.f = GrowSub;',
  25810. ' return Result;',
  25811. ' };',
  25812. ' $mod.f = rtl.createCallback($Self, "Grow");',
  25813. ' $mod.f = GrowSub;',
  25814. ' return Result;',
  25815. ' };',
  25816. '});',
  25817. 'this.f = null;',
  25818. '']),
  25819. LinesToStr([ // $mod.$main
  25820. '']));
  25821. end;
  25822. procedure TTestModule.TestProcType_Typecast;
  25823. begin
  25824. StartProgram(false);
  25825. Add([
  25826. 'type',
  25827. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  25828. ' TEvent = procedure of object;',
  25829. ' TGetter = function:longint of object;',
  25830. ' TProcA = procedure(i: longint);',
  25831. ' TFuncB = function(i, j: longint): longint;',
  25832. 'procedure DoIt(); varargs; begin end;',
  25833. 'var',
  25834. ' Notify: tnotifyevent;',
  25835. ' Event: tevent;',
  25836. ' Getter: tgetter;',
  25837. ' ProcA: tproca;',
  25838. ' FuncB: tfuncb;',
  25839. ' p: pointer;',
  25840. 'begin',
  25841. ' notify:=tnotifyevent(event);',
  25842. ' event:=tevent(event);',
  25843. ' event:=tevent(notify);',
  25844. ' event:=tevent(getter);',
  25845. ' event:=tevent(proca);',
  25846. ' proca:=tproca(funcb);',
  25847. ' funcb:=tfuncb(funcb);',
  25848. ' funcb:=tfuncb(proca);',
  25849. ' funcb:=tfuncb(getter);',
  25850. ' proca:=tproca(p);',
  25851. ' funcb:=tfuncb(p);',
  25852. ' getter:=tgetter(p);',
  25853. ' p:=pointer(notify);',
  25854. ' p:=notify;',
  25855. ' p:=pointer(proca);',
  25856. ' p:=proca;',
  25857. ' p:=pointer(funcb);',
  25858. ' p:=funcb;',
  25859. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  25860. '']);
  25861. ConvertProgram;
  25862. CheckSource('TestProcType_Typecast',
  25863. LinesToStr([ // statements
  25864. 'this.DoIt = function () {',
  25865. '};',
  25866. 'this.Notify = null;',
  25867. 'this.Event = null;',
  25868. 'this.Getter = null;',
  25869. 'this.ProcA = null;',
  25870. 'this.FuncB = null;',
  25871. 'this.p = null;',
  25872. '']),
  25873. LinesToStr([ // $mod.$main
  25874. '$mod.Notify = $mod.Event;',
  25875. '$mod.Event = $mod.Event;',
  25876. '$mod.Event = $mod.Notify;',
  25877. '$mod.Event = $mod.Getter;',
  25878. '$mod.Event = $mod.ProcA;',
  25879. '$mod.ProcA = $mod.FuncB;',
  25880. '$mod.FuncB = $mod.FuncB;',
  25881. '$mod.FuncB = $mod.ProcA;',
  25882. '$mod.FuncB = $mod.Getter;',
  25883. '$mod.ProcA = $mod.p;',
  25884. '$mod.FuncB = $mod.p;',
  25885. '$mod.Getter = $mod.p;',
  25886. '$mod.p = $mod.Notify;',
  25887. '$mod.p = $mod.Notify;',
  25888. '$mod.p = $mod.ProcA;',
  25889. '$mod.p = $mod.ProcA;',
  25890. '$mod.p = $mod.FuncB;',
  25891. '$mod.p = $mod.FuncB;',
  25892. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  25893. '']));
  25894. end;
  25895. procedure TTestModule.TestProcType_PassProcToUntyped;
  25896. begin
  25897. StartProgram(false);
  25898. Add([
  25899. 'type',
  25900. ' TEvent = procedure of object;',
  25901. ' TFunc = function: longint;',
  25902. 'procedure DoIt(); varargs; begin end;',
  25903. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  25904. 'var',
  25905. ' Event: tevent;',
  25906. ' Func: TFunc;',
  25907. 'begin',
  25908. ' doit(event,func);',
  25909. ' dosome(event,event,event);',
  25910. ' dosome(func,func,func);',
  25911. '']);
  25912. ConvertProgram;
  25913. CheckSource('TestProcType_PassProcToUntyped',
  25914. LinesToStr([ // statements
  25915. 'this.DoIt = function () {',
  25916. '};',
  25917. 'this.DoSome = function (a, b, p) {',
  25918. '};',
  25919. 'this.Event = null;',
  25920. 'this.Func = null;',
  25921. '']),
  25922. LinesToStr([ // $mod.$main
  25923. '$mod.DoIt($mod.Event, $mod.Func);',
  25924. '$mod.DoSome($mod.Event, {',
  25925. ' p: $mod,',
  25926. ' get: function () {',
  25927. ' return this.p.Event;',
  25928. ' },',
  25929. ' set: function (v) {',
  25930. ' this.p.Event = v;',
  25931. ' }',
  25932. '}, $mod.Event);',
  25933. '$mod.DoSome($mod.Func, {',
  25934. ' p: $mod,',
  25935. ' get: function () {',
  25936. ' return this.p.Func;',
  25937. ' },',
  25938. ' set: function (v) {',
  25939. ' this.p.Func = v;',
  25940. ' }',
  25941. '}, $mod.Func);',
  25942. '']));
  25943. end;
  25944. procedure TTestModule.TestProcType_PassProcToArray;
  25945. begin
  25946. StartProgram(false);
  25947. Add([
  25948. 'type',
  25949. ' TFunc = function: longint;',
  25950. ' TArrFunc = array of TFunc;',
  25951. 'procedure DoIt(Arr: TArrFunc); begin end;',
  25952. 'function GetIt: longint; begin end;',
  25953. 'var',
  25954. ' Func: tfunc;',
  25955. 'begin',
  25956. ' doit([]);',
  25957. ' doit([@GetIt]);',
  25958. ' doit([Func]);',
  25959. '']);
  25960. ConvertProgram;
  25961. CheckSource('TestProcType_PassProcToArray',
  25962. LinesToStr([ // statements
  25963. 'this.DoIt = function (Arr) {',
  25964. '};',
  25965. 'this.GetIt = function () {',
  25966. ' var Result = 0;',
  25967. ' return Result;',
  25968. '};',
  25969. 'this.Func = null;',
  25970. '']),
  25971. LinesToStr([ // $mod.$main
  25972. '$mod.DoIt([]);',
  25973. '$mod.DoIt([$mod.GetIt]);',
  25974. '$mod.DoIt([$mod.Func]);',
  25975. '']));
  25976. end;
  25977. procedure TTestModule.TestProcType_SafeCallObjFPC;
  25978. begin
  25979. StartProgram(false);
  25980. Add([
  25981. '{$modeswitch externalclass}',
  25982. 'type',
  25983. ' TProc = reference to procedure(i: longint); safecall;',
  25984. ' TEvent = procedure(i: longint) of object; safecall;',
  25985. ' TExtA = class external name ''ExtObj''',
  25986. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  25987. ' procedure DoSome(Id: longint = 1);',
  25988. ' procedure SetOnClick(const e: TEvent);',
  25989. ' property OnClick: TEvent write SetOnClick;',
  25990. ' class procedure Fly(Id: longint = 1); static;',
  25991. ' procedure SetOnShow(const p: TProc);',
  25992. ' property OnShow: TProc write SetOnShow;',
  25993. ' end;',
  25994. 'procedure Run(i: longint = 1);',
  25995. 'begin',
  25996. 'end;',
  25997. 'var',
  25998. ' Obj: texta;',
  25999. ' e: TEvent;',
  26000. ' p: TProc;',
  26001. 'begin',
  26002. ' e:=e;',
  26003. ' e:[email protected];',
  26004. ' e:[email protected];',
  26005. ' e:=TEvent(@obj.dosome);', // no safecall
  26006. ' obj.OnClick:[email protected];',
  26007. ' obj.OnClick:[email protected];',
  26008. ' obj.setonclick(@obj.doit);',
  26009. ' obj.setonclick(@obj.dosome);',
  26010. ' p:=@Run;',
  26011. ' p:[email protected];',
  26012. ' obj.OnShow:=@Run;',
  26013. ' obj.OnShow:[email protected];',
  26014. ' obj.setOnShow(@Run);',
  26015. ' obj.setOnShow(@TExtA.Fly);',
  26016. ' with obj do begin',
  26017. ' e:=@doit;',
  26018. ' e:=@dosome;',
  26019. ' OnClick:=@doit;',
  26020. ' OnClick:=@dosome;',
  26021. ' setonclick(@doit);',
  26022. ' setonclick(@dosome);',
  26023. ' OnShow:=@Run;',
  26024. ' setOnShow(@Run);',
  26025. ' end;']);
  26026. ConvertProgram;
  26027. CheckSource('TestProcType_SafeCallObjFPC',
  26028. LinesToStr([ // statements
  26029. 'this.Run = function (i) {',
  26030. '};',
  26031. 'this.Obj = null;',
  26032. 'this.e = null;',
  26033. 'this.p = null;',
  26034. '']),
  26035. LinesToStr([ // $mod.$main
  26036. '$mod.e = $mod.e;',
  26037. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26038. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26039. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26040. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26041. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26042. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26043. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26044. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26045. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26046. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26047. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26048. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26049. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26050. 'var $with = $mod.Obj;',
  26051. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26052. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26053. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26054. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26055. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26056. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26057. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26058. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26059. '']));
  26060. end;
  26061. procedure TTestModule.TestProcType_SafeCallDelphi;
  26062. begin
  26063. StartProgram(false);
  26064. Add([
  26065. '{$mode delphi}',
  26066. '{$modeswitch externalclass}',
  26067. 'type',
  26068. ' TProc = reference to procedure(i: longint); safecall;',
  26069. ' TEvent = procedure(i: longint) of object; safecall;',
  26070. ' TExtA = class external name ''ExtObj''',
  26071. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26072. ' procedure DoSome(Id: longint = 1);',
  26073. ' procedure SetOnClick(const e: TEvent);',
  26074. ' property OnClick: TEvent write SetOnClick;',
  26075. ' class procedure Fly(Id: longint = 1); static;',
  26076. ' procedure SetOnShow(const p: TProc);',
  26077. ' property OnShow: TProc write SetOnShow;',
  26078. ' end;',
  26079. 'procedure Run(i: longint = 1);',
  26080. 'begin',
  26081. 'end;',
  26082. 'var',
  26083. ' Obj: texta;',
  26084. ' e: TEvent;',
  26085. ' p: TProc;',
  26086. 'begin',
  26087. ' e:=e;',
  26088. ' e:=obj.doit;',
  26089. ' e:=obj.dosome;',
  26090. ' e:=TEvent(@obj.dosome);', // no safecall
  26091. ' obj.OnClick:=obj.doit;',
  26092. ' obj.OnClick:=obj.dosome;',
  26093. ' obj.setonclick(obj.doit);',
  26094. ' obj.setonclick(obj.dosome);',
  26095. ' p:=Run;',
  26096. ' p:=TExtA.Fly;',
  26097. ' obj.OnShow:=Run;',
  26098. ' obj.OnShow:=TExtA.Fly;',
  26099. ' obj.setOnShow(Run);',
  26100. ' obj.setOnShow(TExtA.Fly);',
  26101. ' with obj do begin',
  26102. ' e:=doit;',
  26103. ' e:=dosome;',
  26104. ' OnClick:=doit;',
  26105. ' OnClick:=dosome;',
  26106. ' setonclick(doit);',
  26107. ' setonclick(dosome);',
  26108. ' OnShow:=@Run;',
  26109. ' setOnShow(@Run);',
  26110. ' end;']);
  26111. ConvertProgram;
  26112. CheckSource('TestProcType_SafeCallDelphi',
  26113. LinesToStr([ // statements
  26114. 'this.Run = function (i) {',
  26115. '};',
  26116. 'this.Obj = null;',
  26117. 'this.e = null;',
  26118. 'this.p = null;',
  26119. '']),
  26120. LinesToStr([ // $mod.$main
  26121. '$mod.e = $mod.e;',
  26122. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26123. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26124. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26125. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26126. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26127. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26128. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26129. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26130. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26131. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26132. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26133. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26134. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26135. 'var $with = $mod.Obj;',
  26136. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26137. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26138. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26139. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26140. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26141. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26142. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26143. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26144. '']));
  26145. end;
  26146. procedure TTestModule.TestPointer;
  26147. begin
  26148. StartProgram(false);
  26149. Add(['type',
  26150. ' TObject = class end;',
  26151. ' TClass = class of TObject;',
  26152. ' TArrInt = array of longint;',
  26153. 'const',
  26154. ' n = nil;',
  26155. 'var',
  26156. ' v: jsvalue;',
  26157. ' Obj: tobject;',
  26158. ' C: tclass;',
  26159. ' a: tarrint;',
  26160. ' p: Pointer = nil;',
  26161. ' s: string;',
  26162. 'begin',
  26163. ' p:=p;',
  26164. ' p:=nil;',
  26165. ' if p=nil then;',
  26166. ' if nil=p then;',
  26167. ' if Assigned(p) then;',
  26168. ' p:=Pointer(v);',
  26169. ' p:=obj;',
  26170. ' p:=c;',
  26171. ' p:=a;',
  26172. ' p:=tobject;',
  26173. ' obj:=TObject(p);',
  26174. ' c:=TClass(p);',
  26175. ' a:=TArrInt(p);',
  26176. ' p:=n;',
  26177. ' p:=Pointer(a);',
  26178. ' p:=pointer(s);',
  26179. ' s:=string(p);',
  26180. '']);
  26181. ConvertProgram;
  26182. CheckSource('TestPointer',
  26183. LinesToStr([ // statements
  26184. 'rtl.createClass($mod, "TObject", null, function () {',
  26185. ' this.$init = function () {',
  26186. ' };',
  26187. ' this.$final = function () {',
  26188. ' };',
  26189. '});',
  26190. 'this.n = null;',
  26191. 'this.v = undefined;',
  26192. 'this.Obj = null;',
  26193. 'this.C = null;',
  26194. 'this.a = [];',
  26195. 'this.p = null;',
  26196. 'this.s = "";',
  26197. '']),
  26198. LinesToStr([ // $mod.$main
  26199. '$mod.p = $mod.p;',
  26200. '$mod.p = null;',
  26201. 'if ($mod.p === null) ;',
  26202. 'if (null === $mod.p) ;',
  26203. 'if ($mod.p != null) ;',
  26204. '$mod.p = $mod.v;',
  26205. '$mod.p = $mod.Obj;',
  26206. '$mod.p = $mod.C;',
  26207. '$mod.p = $mod.a;',
  26208. '$mod.p = $mod.TObject;',
  26209. '$mod.Obj = $mod.p;',
  26210. '$mod.C = $mod.p;',
  26211. '$mod.a = $mod.p;',
  26212. '$mod.p = null;',
  26213. '$mod.p = $mod.a;',
  26214. '$mod.p = $mod.s;',
  26215. '$mod.s = $mod.p;',
  26216. '']));
  26217. end;
  26218. procedure TTestModule.TestPointer_Proc;
  26219. begin
  26220. StartProgram(false);
  26221. Add('type');
  26222. Add(' TObject = class');
  26223. Add(' procedure DoIt; virtual; abstract;');
  26224. Add(' end;');
  26225. Add('procedure DoSome; begin end;');
  26226. Add('var');
  26227. Add(' o: TObject;');
  26228. Add(' p: Pointer;');
  26229. Add('begin');
  26230. Add(' p:=@DoSome;');
  26231. Add(' p:[email protected];');
  26232. ConvertProgram;
  26233. CheckSource('TestPointer_Proc',
  26234. LinesToStr([ // statements
  26235. 'rtl.createClass($mod, "TObject", null, function () {',
  26236. ' this.$init = function () {',
  26237. ' };',
  26238. ' this.$final = function () {',
  26239. ' };',
  26240. '});',
  26241. 'this.DoSome = function () {',
  26242. '};',
  26243. 'this.o = null;',
  26244. 'this.p = null;',
  26245. '']),
  26246. LinesToStr([ // $mod.$main
  26247. '$mod.p = $mod.DoSome;',
  26248. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26249. '']));
  26250. end;
  26251. procedure TTestModule.TestPointer_AssignRecordFail;
  26252. begin
  26253. StartProgram(false);
  26254. Add('type');
  26255. Add(' TRec = record end;');
  26256. Add('var');
  26257. Add(' p: Pointer;');
  26258. Add(' r: TRec;');
  26259. Add('begin');
  26260. Add(' p:=r;');
  26261. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26262. nIncompatibleTypesGotExpected);
  26263. ConvertProgram;
  26264. end;
  26265. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26266. begin
  26267. StartProgram(false);
  26268. Add('type');
  26269. Add(' TArr = array[boolean] of longint;');
  26270. Add('var');
  26271. Add(' p: Pointer;');
  26272. Add(' a: TArr;');
  26273. Add('begin');
  26274. Add(' p:=a;');
  26275. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26276. nIncompatibleTypesGotExpected);
  26277. ConvertProgram;
  26278. end;
  26279. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26280. begin
  26281. StartProgram(false);
  26282. Add([
  26283. 'procedure DoIt(args: array of jsvalue); begin end;',
  26284. 'procedure DoAll; varargs; begin end;',
  26285. 'var',
  26286. ' v: jsvalue;',
  26287. 'begin',
  26288. ' DoIt([pointer(v)]);',
  26289. ' DoAll(pointer(v));',
  26290. '']);
  26291. ConvertProgram;
  26292. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26293. LinesToStr([ // statements
  26294. 'this.DoIt = function (args) {',
  26295. '};',
  26296. 'this.DoAll = function () {',
  26297. '};',
  26298. 'this.v = undefined;',
  26299. '']),
  26300. LinesToStr([ // $mod.$main
  26301. '$mod.DoIt([$mod.v]);',
  26302. '$mod.DoAll($mod.v);',
  26303. '']));
  26304. end;
  26305. procedure TTestModule.TestPointer_NonRecordFail;
  26306. begin
  26307. StartProgram(false);
  26308. Add([
  26309. 'type',
  26310. ' p = ^longint;',
  26311. 'begin',
  26312. '']);
  26313. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26314. ConvertProgram;
  26315. end;
  26316. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26317. begin
  26318. StartProgram(false);
  26319. Add([
  26320. 'procedure DoIt(p: ^longint); begin end;',
  26321. 'begin',
  26322. '']);
  26323. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26324. ConvertProgram;
  26325. end;
  26326. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26327. begin
  26328. StartProgram(false);
  26329. Add([
  26330. 'var p: ^longint;',
  26331. 'begin',
  26332. '']);
  26333. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26334. ConvertProgram;
  26335. end;
  26336. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26337. begin
  26338. StartProgram(false);
  26339. Add([
  26340. 'function DoIt: ^longint; begin end;',
  26341. 'begin',
  26342. '']);
  26343. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26344. ConvertProgram;
  26345. end;
  26346. procedure TTestModule.TestPointer_AddrOperatorFail;
  26347. begin
  26348. StartProgram(false);
  26349. Add([
  26350. 'var i: longint;',
  26351. 'begin',
  26352. ' if @i=nil then ;',
  26353. '']);
  26354. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26355. ConvertProgram;
  26356. end;
  26357. procedure TTestModule.TestPointer_ArrayParamsFail;
  26358. begin
  26359. StartProgram(false);
  26360. Add([
  26361. 'var',
  26362. ' p: Pointer;',
  26363. 'begin',
  26364. ' p:=p[1];',
  26365. '']);
  26366. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26367. ConvertProgram;
  26368. end;
  26369. procedure TTestModule.TestPointer_PointerAddFail;
  26370. begin
  26371. StartProgram(false);
  26372. Add([
  26373. 'var',
  26374. ' p: Pointer;',
  26375. 'begin',
  26376. ' p:=p+1;',
  26377. '']);
  26378. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26379. ConvertProgram;
  26380. end;
  26381. procedure TTestModule.TestPointer_IncPointerFail;
  26382. begin
  26383. StartProgram(false);
  26384. Add([
  26385. 'var',
  26386. ' p: Pointer;',
  26387. 'begin',
  26388. ' inc(p,1);',
  26389. '']);
  26390. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26391. nIncompatibleTypeArgNo);
  26392. ConvertProgram;
  26393. end;
  26394. procedure TTestModule.TestPointer_Record;
  26395. begin
  26396. StartProgram(false);
  26397. Add([
  26398. 'type',
  26399. ' TRec = record x: longint; end;',
  26400. ' PRec = ^TRec;',
  26401. 'var',
  26402. ' r: TRec;',
  26403. ' p: PRec;',
  26404. ' q: ^TRec;',
  26405. ' Ptr: pointer;',
  26406. 'begin',
  26407. ' new(p);',
  26408. ' p:=@r;',
  26409. ' r:=p^;',
  26410. ' r.x:=p^.x;',
  26411. ' p^.x:=r.x;',
  26412. ' if p^.x=3 then ;',
  26413. ' if 4=p^.x then ;',
  26414. ' dispose(p);',
  26415. ' new(q);',
  26416. ' dispose(q);',
  26417. ' Ptr:=p;',
  26418. ' p:=PRec(ptr);',
  26419. '']);
  26420. ConvertProgram;
  26421. CheckSource('TestPointer_Record',
  26422. LinesToStr([ // statements
  26423. 'rtl.recNewT($mod, "TRec", function () {',
  26424. ' this.x = 0;',
  26425. ' this.$eq = function (b) {',
  26426. ' return this.x === b.x;',
  26427. ' };',
  26428. ' this.$assign = function (s) {',
  26429. ' this.x = s.x;',
  26430. ' return this;',
  26431. ' };',
  26432. '});',
  26433. 'this.r = $mod.TRec.$new();',
  26434. 'this.p = null;',
  26435. 'this.q = null;',
  26436. 'this.Ptr = null;',
  26437. '']),
  26438. LinesToStr([ // $mod.$main
  26439. '$mod.p = $mod.TRec.$new();',
  26440. '$mod.p = $mod.r;',
  26441. '$mod.r.$assign($mod.p);',
  26442. '$mod.r.x = $mod.p.x;',
  26443. '$mod.p.x = $mod.r.x;',
  26444. 'if ($mod.p.x === 3) ;',
  26445. 'if (4 === $mod.p.x) ;',
  26446. '$mod.p = null;',
  26447. '$mod.q = $mod.TRec.$new();',
  26448. '$mod.q = null;',
  26449. '$mod.Ptr = $mod.p;',
  26450. '$mod.p = $mod.Ptr;',
  26451. '']));
  26452. end;
  26453. procedure TTestModule.TestPointer_RecordArg;
  26454. begin
  26455. StartProgram(false);
  26456. Add([
  26457. '{$modeswitch autoderef}',
  26458. 'type',
  26459. ' TRec = record x: longint; end;',
  26460. ' PRec = ^TRec;',
  26461. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  26462. 'begin',
  26463. ' a.x:=a.x;',
  26464. ' a^.x:=a^.x;',
  26465. ' with a^ do',
  26466. ' x:=x;',
  26467. 'end;',
  26468. 'function GetIt(p: PRec): PRec;',
  26469. 'begin',
  26470. ' p.x:=p.x;',
  26471. ' p^.x:=p^.x;',
  26472. ' with p^ do',
  26473. ' x:=x;',
  26474. 'end;',
  26475. 'var',
  26476. ' r: TRec;',
  26477. ' p: PRec;',
  26478. 'begin',
  26479. ' p:=GetIt(p);',
  26480. ' p^:=GetIt(@r)^;',
  26481. ' DoIt(p,p,p);',
  26482. ' DoIt(@r,p,p);',
  26483. '']);
  26484. ConvertProgram;
  26485. CheckSource('TestPointer_Record',
  26486. LinesToStr([ // statements
  26487. 'rtl.recNewT($mod, "TRec", function () {',
  26488. ' this.x = 0;',
  26489. ' this.$eq = function (b) {',
  26490. ' return this.x === b.x;',
  26491. ' };',
  26492. ' this.$assign = function (s) {',
  26493. ' this.x = s.x;',
  26494. ' return this;',
  26495. ' };',
  26496. '});',
  26497. 'this.DoIt = function (a, b, c) {',
  26498. ' var Result = $mod.TRec.$new();',
  26499. ' a.x = a.x;',
  26500. ' a.x = a.x;',
  26501. ' a.x = a.x;',
  26502. ' return Result;',
  26503. '};',
  26504. 'this.GetIt = function (p) {',
  26505. ' var Result = null;',
  26506. ' p.x = p.x;',
  26507. ' p.x = p.x;',
  26508. ' p.x = p.x;',
  26509. ' return Result;',
  26510. '};',
  26511. 'this.r = $mod.TRec.$new();',
  26512. 'this.p = null;',
  26513. '']),
  26514. LinesToStr([ // $mod.$main
  26515. '$mod.p = $mod.GetIt($mod.p);',
  26516. '$mod.p.$assign($mod.GetIt($mod.r));',
  26517. '$mod.DoIt($mod.p, {',
  26518. ' p: $mod,',
  26519. ' get: function () {',
  26520. ' return this.p.p;',
  26521. ' },',
  26522. ' set: function (v) {',
  26523. ' this.p.p = v;',
  26524. ' }',
  26525. '}, {',
  26526. ' p: $mod,',
  26527. ' get: function () {',
  26528. ' return this.p.p;',
  26529. ' },',
  26530. ' set: function (v) {',
  26531. ' this.p.p = v;',
  26532. ' }',
  26533. '});',
  26534. '$mod.DoIt($mod.r, {',
  26535. ' p: $mod,',
  26536. ' get: function () {',
  26537. ' return this.p.p;',
  26538. ' },',
  26539. ' set: function (v) {',
  26540. ' this.p.p = v;',
  26541. ' }',
  26542. '}, {',
  26543. ' p: $mod,',
  26544. ' get: function () {',
  26545. ' return this.p.p;',
  26546. ' },',
  26547. ' set: function (v) {',
  26548. ' this.p.p = v;',
  26549. ' }',
  26550. '});',
  26551. '']));
  26552. end;
  26553. procedure TTestModule.TestJSValue_AssignToJSValue;
  26554. begin
  26555. StartProgram(false);
  26556. Add('var');
  26557. Add(' v: jsvalue;');
  26558. Add(' i: longint;');
  26559. Add(' s: string;');
  26560. Add(' b: boolean;');
  26561. Add(' d: double;');
  26562. Add(' p: pointer;');
  26563. Add('begin');
  26564. Add(' v:=v;');
  26565. Add(' v:=1;');
  26566. Add(' v:=i;');
  26567. Add(' v:='''';');
  26568. Add(' v:=''c'';');
  26569. Add(' v:=''foo'';');
  26570. Add(' v:=s;');
  26571. Add(' v:=false;');
  26572. Add(' v:=true;');
  26573. Add(' v:=b;');
  26574. Add(' v:=0.1;');
  26575. Add(' v:=d;');
  26576. Add(' v:=nil;');
  26577. Add(' v:=p;');
  26578. ConvertProgram;
  26579. CheckSource('TestJSValue_AssignToJSValue',
  26580. LinesToStr([ // statements
  26581. 'this.v = undefined;',
  26582. 'this.i = 0;',
  26583. 'this.s = "";',
  26584. 'this.b = false;',
  26585. 'this.d = 0.0;',
  26586. 'this.p = null;',
  26587. '']),
  26588. LinesToStr([ // $mod.$main
  26589. '$mod.v = $mod.v;',
  26590. '$mod.v = 1;',
  26591. '$mod.v = $mod.i;',
  26592. '$mod.v = "";',
  26593. '$mod.v = "c";',
  26594. '$mod.v = "foo";',
  26595. '$mod.v = $mod.s;',
  26596. '$mod.v = false;',
  26597. '$mod.v = true;',
  26598. '$mod.v = $mod.b;',
  26599. '$mod.v = 0.1;',
  26600. '$mod.v = $mod.d;',
  26601. '$mod.v = null;',
  26602. '$mod.v = $mod.p;',
  26603. '']));
  26604. end;
  26605. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  26606. begin
  26607. StartProgram(false);
  26608. Add('type');
  26609. Add(' integer = longint;');
  26610. Add(' TYesNo = boolean;');
  26611. Add(' TFloat = double;');
  26612. Add(' TCaption = string;');
  26613. Add(' TChar = char;');
  26614. Add('var');
  26615. Add(' v: jsvalue;');
  26616. Add(' i: integer;');
  26617. Add(' s: TCaption;');
  26618. Add(' b: TYesNo;');
  26619. Add(' d: TFloat;');
  26620. Add(' c: char;');
  26621. Add('begin');
  26622. Add(' i:=longint(v);');
  26623. Add(' i:=integer(v);');
  26624. Add(' s:=string(v);');
  26625. Add(' s:=TCaption(v);');
  26626. Add(' b:=boolean(v);');
  26627. Add(' b:=TYesNo(v);');
  26628. Add(' d:=double(v);');
  26629. Add(' d:=TFloat(v);');
  26630. Add(' c:=char(v);');
  26631. Add(' c:=TChar(v);');
  26632. ConvertProgram;
  26633. CheckSource('TestJSValue_TypeCastToBaseType',
  26634. LinesToStr([ // statements
  26635. 'this.v = undefined;',
  26636. 'this.i = 0;',
  26637. 'this.s = "";',
  26638. 'this.b = false;',
  26639. 'this.d = 0.0;',
  26640. 'this.c = "";',
  26641. '']),
  26642. LinesToStr([ // $mod.$main
  26643. '$mod.i = Math.floor($mod.v);',
  26644. '$mod.i = Math.floor($mod.v);',
  26645. '$mod.s = "" + $mod.v;',
  26646. '$mod.s = "" + $mod.v;',
  26647. '$mod.b = !($mod.v == false);',
  26648. '$mod.b = !($mod.v == false);',
  26649. '$mod.d = rtl.getNumber($mod.v);',
  26650. '$mod.d = rtl.getNumber($mod.v);',
  26651. '$mod.c = rtl.getChar($mod.v);',
  26652. '$mod.c = rtl.getChar($mod.v);',
  26653. '']));
  26654. end;
  26655. procedure TTestModule.TestJSValue_TypecastToJSValue;
  26656. begin
  26657. StartProgram(false);
  26658. Add([
  26659. 'type',
  26660. ' TArr = array of word;',
  26661. ' TRec = record end;',
  26662. ' TSet = set of boolean;',
  26663. 'procedure Fly(v: jsvalue);',
  26664. 'begin',
  26665. 'end;',
  26666. 'var',
  26667. ' a: TArr;',
  26668. ' r: TRec;',
  26669. ' s: TSet;',
  26670. 'begin',
  26671. ' Fly(jsvalue(a));',
  26672. ' Fly(jsvalue(r));',
  26673. ' Fly(jsvalue(s));',
  26674. '']);
  26675. ConvertProgram;
  26676. CheckSource('TestJSValue_TypecastToJSValue',
  26677. LinesToStr([ // statements
  26678. 'rtl.recNewT($mod, "TRec", function () {',
  26679. ' this.$eq = function (b) {',
  26680. ' return true;',
  26681. ' };',
  26682. ' this.$assign = function (s) {',
  26683. ' return this;',
  26684. ' };',
  26685. '});',
  26686. 'this.Fly = function (v) {',
  26687. '};',
  26688. 'this.a = [];',
  26689. 'this.r = $mod.TRec.$new();',
  26690. 'this.s = {};',
  26691. '']),
  26692. LinesToStr([ // $mod.$main
  26693. '$mod.Fly($mod.a);',
  26694. '$mod.Fly($mod.r);',
  26695. '$mod.Fly($mod.s);',
  26696. '']));
  26697. end;
  26698. procedure TTestModule.TestJSValue_Equal;
  26699. begin
  26700. StartProgram(false);
  26701. Add('type');
  26702. Add(' integer = longint;');
  26703. Add(' TYesNo = boolean;');
  26704. Add(' TFloat = double;');
  26705. Add(' TCaption = string;');
  26706. Add(' TChar = char;');
  26707. Add(' TMulti = JSValue;');
  26708. Add('var');
  26709. Add(' v: jsvalue;');
  26710. Add(' i: integer;');
  26711. Add(' s: TCaption;');
  26712. Add(' b: TYesNo;');
  26713. Add(' d: TFloat;');
  26714. Add(' c: char;');
  26715. Add(' m: TMulti;');
  26716. Add('begin');
  26717. Add(' b:=v=v;');
  26718. Add(' b:=v<>v;');
  26719. Add(' b:=v=1;');
  26720. Add(' b:=v<>1;');
  26721. Add(' b:=2=v;');
  26722. Add(' b:=2<>v;');
  26723. Add(' b:=v=i;');
  26724. Add(' b:=i=v;');
  26725. Add(' b:=v=nil;');
  26726. Add(' b:=nil=v;');
  26727. Add(' b:=v=false;');
  26728. Add(' b:=true=v;');
  26729. Add(' b:=v=b;');
  26730. Add(' b:=b=v;');
  26731. Add(' b:=v=s;');
  26732. Add(' b:=s=v;');
  26733. Add(' b:=v=''foo'';');
  26734. Add(' b:=''''=v;');
  26735. Add(' b:=v=d;');
  26736. Add(' b:=d=v;');
  26737. Add(' b:=v=3.4;');
  26738. Add(' b:=5.6=v;');
  26739. Add(' b:=v=c;');
  26740. Add(' b:=c=v;');
  26741. Add(' b:=m=m;');
  26742. Add(' b:=v=m;');
  26743. Add(' b:=m=v;');
  26744. ConvertProgram;
  26745. CheckSource('TestJSValue_Equal',
  26746. LinesToStr([ // statements
  26747. 'this.v = undefined;',
  26748. 'this.i = 0;',
  26749. 'this.s = "";',
  26750. 'this.b = false;',
  26751. 'this.d = 0.0;',
  26752. 'this.c = "";',
  26753. 'this.m = undefined;',
  26754. '']),
  26755. LinesToStr([ // $mod.$main
  26756. '$mod.b = $mod.v == $mod.v;',
  26757. '$mod.b = $mod.v != $mod.v;',
  26758. '$mod.b = $mod.v == 1;',
  26759. '$mod.b = $mod.v != 1;',
  26760. '$mod.b = 2 == $mod.v;',
  26761. '$mod.b = 2 != $mod.v;',
  26762. '$mod.b = $mod.v == $mod.i;',
  26763. '$mod.b = $mod.i == $mod.v;',
  26764. '$mod.b = $mod.v == null;',
  26765. '$mod.b = null == $mod.v;',
  26766. '$mod.b = $mod.v == false;',
  26767. '$mod.b = true == $mod.v;',
  26768. '$mod.b = $mod.v == $mod.b;',
  26769. '$mod.b = $mod.b == $mod.v;',
  26770. '$mod.b = $mod.v == $mod.s;',
  26771. '$mod.b = $mod.s == $mod.v;',
  26772. '$mod.b = $mod.v == "foo";',
  26773. '$mod.b = "" == $mod.v;',
  26774. '$mod.b = $mod.v == $mod.d;',
  26775. '$mod.b = $mod.d == $mod.v;',
  26776. '$mod.b = $mod.v == 3.4;',
  26777. '$mod.b = 5.6 == $mod.v;',
  26778. '$mod.b = $mod.v == $mod.c;',
  26779. '$mod.b = $mod.c == $mod.v;',
  26780. '$mod.b = $mod.m == $mod.m;',
  26781. '$mod.b = $mod.v == $mod.m;',
  26782. '$mod.b = $mod.m == $mod.v;',
  26783. '']));
  26784. end;
  26785. procedure TTestModule.TestJSValue_If;
  26786. begin
  26787. StartProgram(false);
  26788. Add([
  26789. 'procedure Fly(var u);',
  26790. 'begin',
  26791. ' if jsvalue(u) then ;',
  26792. 'end;',
  26793. 'var',
  26794. ' v: jsvalue;',
  26795. 'begin',
  26796. ' if v then ;',
  26797. ' while v do ;',
  26798. ' repeat until v;',
  26799. '']);
  26800. ConvertProgram;
  26801. CheckSource('TestJSValue_If',
  26802. LinesToStr([ // statements
  26803. 'this.Fly = function (u) {',
  26804. ' if (u.get()) ;',
  26805. '};',
  26806. 'this.v = undefined;',
  26807. '']),
  26808. LinesToStr([ // $mod.$main
  26809. 'if ($mod.v) ;',
  26810. 'while($mod.v){',
  26811. '};',
  26812. 'do{',
  26813. '} while(!$mod.v);',
  26814. '']));
  26815. end;
  26816. procedure TTestModule.TestJSValue_Not;
  26817. begin
  26818. StartProgram(false);
  26819. Add([
  26820. 'var',
  26821. ' v: jsvalue;',
  26822. ' b: boolean;',
  26823. 'begin',
  26824. ' b:=not v;',
  26825. ' if not v then ;',
  26826. ' while not v do ;',
  26827. ' repeat until not v;',
  26828. '']);
  26829. ConvertProgram;
  26830. CheckSource('TestJSValue_If',
  26831. LinesToStr([ // statements
  26832. 'this.v = undefined;',
  26833. 'this.b = false;',
  26834. '']),
  26835. LinesToStr([ // $mod.$main
  26836. '$mod.b=!$mod.v;',
  26837. 'if (!$mod.v) ;',
  26838. 'while(!$mod.v){',
  26839. '};',
  26840. 'do{',
  26841. '} while($mod.v);',
  26842. '']));
  26843. end;
  26844. procedure TTestModule.TestJSValue_Enum;
  26845. begin
  26846. StartProgram(false);
  26847. Add('type');
  26848. Add(' TColor = (red, blue);');
  26849. Add(' TRedBlue = TColor;');
  26850. Add('var');
  26851. Add(' v: jsvalue;');
  26852. Add(' e: TColor;');
  26853. Add('begin');
  26854. Add(' v:=e;');
  26855. Add(' v:=TColor(e);');
  26856. Add(' v:=TRedBlue(e);');
  26857. Add(' e:=TColor(v);');
  26858. Add(' e:=TRedBlue(v);');
  26859. ConvertProgram;
  26860. CheckSource('TestJSValue_Enum',
  26861. LinesToStr([ // statements
  26862. 'this.TColor = {',
  26863. ' "0": "red",',
  26864. ' red: 0,',
  26865. ' "1": "blue",',
  26866. ' blue: 1',
  26867. '};',
  26868. 'this.v = undefined;',
  26869. 'this.e = 0;',
  26870. '']),
  26871. LinesToStr([ // $mod.$main
  26872. '$mod.v = $mod.e;',
  26873. '$mod.v = $mod.e;',
  26874. '$mod.v = $mod.e;',
  26875. '$mod.e = $mod.v;',
  26876. '$mod.e = $mod.v;',
  26877. '']));
  26878. end;
  26879. procedure TTestModule.TestJSValue_ClassInstance;
  26880. begin
  26881. StartProgram(false);
  26882. Add([
  26883. 'type',
  26884. ' TObject = class',
  26885. ' end;',
  26886. ' TBirdObject = TObject;',
  26887. 'var',
  26888. ' v: jsvalue;',
  26889. ' o: TObject;',
  26890. 'begin',
  26891. ' v:=o;',
  26892. ' v:=TObject(o);',
  26893. ' v:=TBirdObject(o);',
  26894. ' o:=TObject(v);',
  26895. ' o:=TBirdObject(v);',
  26896. ' if v is TObject then ;',
  26897. '']);
  26898. ConvertProgram;
  26899. CheckSource('TestJSValue_ClassInstance',
  26900. LinesToStr([ // statements
  26901. 'rtl.createClass($mod, "TObject", null, function () {',
  26902. ' this.$init = function () {',
  26903. ' };',
  26904. ' this.$final = function () {',
  26905. ' };',
  26906. '});',
  26907. 'this.v = undefined;',
  26908. 'this.o = null;',
  26909. '']),
  26910. LinesToStr([ // $mod.$main
  26911. '$mod.v = $mod.o;',
  26912. '$mod.v = $mod.o;',
  26913. '$mod.v = $mod.o;',
  26914. '$mod.o = rtl.getObject($mod.v);',
  26915. '$mod.o = rtl.getObject($mod.v);',
  26916. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  26917. '']));
  26918. end;
  26919. procedure TTestModule.TestJSValue_ClassOf;
  26920. begin
  26921. StartProgram(false);
  26922. Add([
  26923. 'type',
  26924. ' TClass = class of TObject;',
  26925. ' TObject = class',
  26926. ' end;',
  26927. ' TBirds = class of TBird;',
  26928. ' TBird = class(TObject) end;',
  26929. 'var',
  26930. ' v: jsvalue;',
  26931. ' c: TClass;',
  26932. 'begin',
  26933. ' v:=c;',
  26934. ' v:=TObject;',
  26935. ' v:=TClass(c);',
  26936. ' v:=TBirds(c);',
  26937. ' c:=TClass(v);',
  26938. ' c:=TBirds(v);',
  26939. ' if v is TClass then ;',
  26940. '']);
  26941. ConvertProgram;
  26942. CheckSource('TestJSValue_ClassOf',
  26943. LinesToStr([ // statements
  26944. 'rtl.createClass($mod, "TObject", null, function () {',
  26945. ' this.$init = function () {',
  26946. ' };',
  26947. ' this.$final = function () {',
  26948. ' };',
  26949. '});',
  26950. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  26951. '});',
  26952. 'this.v = undefined;',
  26953. 'this.c = null;',
  26954. '']),
  26955. LinesToStr([ // $mod.$main
  26956. '$mod.v = $mod.c;',
  26957. '$mod.v = $mod.TObject;',
  26958. '$mod.v = $mod.c;',
  26959. '$mod.v = $mod.c;',
  26960. '$mod.c = rtl.getObject($mod.v);',
  26961. '$mod.c = rtl.getObject($mod.v);',
  26962. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  26963. '']));
  26964. end;
  26965. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  26966. begin
  26967. StartProgram(false);
  26968. Add([
  26969. 'type',
  26970. ' integer = longint;',
  26971. ' TArray = array of JSValue;',
  26972. ' TArrgh = tarray;',
  26973. ' TArrInt = array of integer;',
  26974. 'var',
  26975. ' v: jsvalue;',
  26976. ' TheArray: tarray = (1,''2'');',
  26977. ' Arr: tarrgh;',
  26978. ' i: integer;',
  26979. ' ArrInt: tarrint;',
  26980. 'begin',
  26981. ' arr:=thearray;',
  26982. ' thearray:=arr;',
  26983. ' setlength(arr,2);',
  26984. ' setlength(thearray,3);',
  26985. ' arr[4]:=v;',
  26986. ' arr[5]:=length(thearray);',
  26987. ' arr[6]:=nil;',
  26988. ' arr[7]:=thearray[8];',
  26989. ' arr[low(arr)]:=high(thearray);',
  26990. ' arr:=arrint;',
  26991. ' arrInt:=tarrint(arr);',
  26992. ' if TheArray = nil then ;',
  26993. ' if nil = TheArray then ;',
  26994. ' if TheArray <> nil then ;',
  26995. ' if nil <> TheArray then ;',
  26996. '']);
  26997. ConvertProgram;
  26998. CheckSource('TestJSValue_ArrayOfJSValue',
  26999. LinesToStr([ // statements
  27000. 'this.v = undefined;',
  27001. 'this.TheArray = [1, "2"];',
  27002. 'this.Arr = [];',
  27003. 'this.i = 0;',
  27004. 'this.ArrInt = [];',
  27005. '']),
  27006. LinesToStr([ // $mod.$main
  27007. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27008. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27009. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27010. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27011. '$mod.Arr[4] = $mod.v;',
  27012. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27013. '$mod.Arr[6] = null;',
  27014. '$mod.Arr[7] = $mod.TheArray[8];',
  27015. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27016. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27017. '$mod.ArrInt = $mod.Arr;',
  27018. 'if (rtl.length($mod.TheArray) === 0) ;',
  27019. 'if (rtl.length($mod.TheArray) === 0) ;',
  27020. 'if (rtl.length($mod.TheArray) > 0) ;',
  27021. 'if (rtl.length($mod.TheArray) > 0) ;',
  27022. '']));
  27023. end;
  27024. procedure TTestModule.TestJSValue_ArrayLit;
  27025. begin
  27026. StartProgram(false);
  27027. Add([
  27028. 'type',
  27029. ' TFlag = (big,small);',
  27030. ' TArray = array of JSValue;',
  27031. ' TObject = class end;',
  27032. ' TClass = class of TObject;',
  27033. 'var',
  27034. ' v: jsvalue;',
  27035. ' a: TArray;',
  27036. ' o: TObject;',
  27037. 'begin',
  27038. ' a:=[];',
  27039. ' a:=[1];',
  27040. ' a:=[1,2];',
  27041. ' a:=[big];',
  27042. ' a:=[1,big];',
  27043. ' a:=[o,nil];',
  27044. '']);
  27045. ConvertProgram;
  27046. CheckSource('TestJSValue_ArrayLit',
  27047. LinesToStr([ // statements
  27048. 'this.TFlag = {',
  27049. ' "0": "big",',
  27050. ' big: 0,',
  27051. ' "1": "small",',
  27052. ' small: 1',
  27053. '};',
  27054. 'rtl.createClass($mod, "TObject", null, function () {',
  27055. ' this.$init = function () {',
  27056. ' };',
  27057. ' this.$final = function () {',
  27058. ' };',
  27059. '});',
  27060. 'this.v = undefined;',
  27061. 'this.a = [];',
  27062. 'this.o = null;',
  27063. '']),
  27064. LinesToStr([ // $mod.$main
  27065. '$mod.a = [];',
  27066. '$mod.a = [1];',
  27067. '$mod.a = [1, 2];',
  27068. '$mod.a = [$mod.TFlag.big];',
  27069. '$mod.a = [1, $mod.TFlag.big];',
  27070. '$mod.a = [$mod.o, null];',
  27071. '']));
  27072. end;
  27073. procedure TTestModule.TestJSValue_Params;
  27074. begin
  27075. StartProgram(false);
  27076. Add('type');
  27077. Add(' integer = longint;');
  27078. Add(' TYesNo = boolean;');
  27079. Add(' TFloat = double;');
  27080. Add(' TCaption = string;');
  27081. Add(' TChar = char;');
  27082. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27083. Add('var');
  27084. Add(' l: jsvalue;');
  27085. Add('begin');
  27086. Add(' a:=a;');
  27087. Add(' l:=b;');
  27088. Add(' c:=c;');
  27089. Add(' d:=d;');
  27090. Add(' Result:=l;');
  27091. Add('end;');
  27092. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27093. Add('var');
  27094. Add(' v: jsvalue;');
  27095. Add(' i: integer;');
  27096. Add(' b: TYesNo;');
  27097. Add(' d: TFloat;');
  27098. Add(' s: TCaption;');
  27099. Add(' c: TChar;');
  27100. Add('begin');
  27101. Add(' v:=doit(v,v,v,v);');
  27102. Add(' i:=integer(dosome(i,i));');
  27103. Add(' b:=TYesNo(dosome(b,b));');
  27104. Add(' d:=TFloat(dosome(d,d));');
  27105. Add(' s:=TCaption(dosome(s,s));');
  27106. Add(' c:=TChar(dosome(c,c));');
  27107. ConvertProgram;
  27108. CheckSource('TestJSValue_Params',
  27109. LinesToStr([ // statements
  27110. 'this.DoIt = function (a, b, c, d) {',
  27111. ' var Result = undefined;',
  27112. ' var l = undefined;',
  27113. ' a = a;',
  27114. ' l = b;',
  27115. ' c.set(c.get());',
  27116. ' d.set(d.get());',
  27117. ' Result = l;',
  27118. ' return Result;',
  27119. '};',
  27120. 'this.DoSome = function (a, b) {',
  27121. ' var Result = undefined;',
  27122. ' return Result;',
  27123. '};',
  27124. 'this.v = undefined;',
  27125. 'this.i = 0;',
  27126. 'this.b = false;',
  27127. 'this.d = 0.0;',
  27128. 'this.s = "";',
  27129. 'this.c = "";',
  27130. '']),
  27131. LinesToStr([ // $mod.$main
  27132. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27133. ' p: $mod,',
  27134. ' get: function () {',
  27135. ' return this.p.v;',
  27136. ' },',
  27137. ' set: function (v) {',
  27138. ' this.p.v = v;',
  27139. ' }',
  27140. '}, {',
  27141. ' p: $mod,',
  27142. ' get: function () {',
  27143. ' return this.p.v;',
  27144. ' },',
  27145. ' set: function (v) {',
  27146. ' this.p.v = v;',
  27147. ' }',
  27148. '});',
  27149. '$mod.i = Math.floor($mod.DoSome($mod.i, $mod.i));',
  27150. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27151. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27152. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27153. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27154. '']));
  27155. end;
  27156. procedure TTestModule.TestJSValue_UntypedParam;
  27157. begin
  27158. StartProgram(false);
  27159. Add('function DoIt(const a; var b; out c): jsvalue;');
  27160. Add('begin');
  27161. Add(' Result:=a;');
  27162. Add(' Result:=b;');
  27163. Add(' Result:=c;');
  27164. Add(' b:=Result;');
  27165. Add(' c:=Result;');
  27166. Add('end;');
  27167. Add('var i: longint;');
  27168. Add('begin');
  27169. Add(' doit(i,i,i);');
  27170. ConvertProgram;
  27171. CheckSource('TestJSValue_UntypedParam',
  27172. LinesToStr([ // statements
  27173. 'this.DoIt = function (a, b, c) {',
  27174. ' var Result = undefined;',
  27175. ' Result = a;',
  27176. ' Result = b.get();',
  27177. ' Result = c.get();',
  27178. ' b.set(Result);',
  27179. ' c.set(Result);',
  27180. ' return Result;',
  27181. '};',
  27182. 'this.i = 0;',
  27183. '']),
  27184. LinesToStr([ // $mod.$main
  27185. '$mod.DoIt($mod.i, {',
  27186. ' p: $mod,',
  27187. ' get: function () {',
  27188. ' return this.p.i;',
  27189. ' },',
  27190. ' set: function (v) {',
  27191. ' this.p.i = v;',
  27192. ' }',
  27193. '}, {',
  27194. ' p: $mod,',
  27195. ' get: function () {',
  27196. ' return this.p.i;',
  27197. ' },',
  27198. ' set: function (v) {',
  27199. ' this.p.i = v;',
  27200. ' }',
  27201. '});',
  27202. '']));
  27203. end;
  27204. procedure TTestModule.TestJSValue_FuncResultType;
  27205. begin
  27206. StartProgram(false);
  27207. Add('type');
  27208. Add(' integer = longint;');
  27209. Add(' TJSValueArray = array of JSValue;');
  27210. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27211. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27212. Add('begin');
  27213. Add(' while Compare(P,aList[0])>0 do ;');
  27214. Add('end;');
  27215. Add('var');
  27216. Add(' Compare: TListSortCompare;');
  27217. Add(' V: JSValue;');
  27218. Add(' i: integer;');
  27219. Add('begin');
  27220. Add(' if Compare(V,V)>0 then ;');
  27221. Add(' if Compare(i,i)>1 then ;');
  27222. Add(' if Compare(nil,false)>2 then ;');
  27223. Add(' if Compare(1,true)>3 then ;');
  27224. ConvertProgram;
  27225. CheckSource('TestJSValue_UntypedParam',
  27226. LinesToStr([ // statements
  27227. 'this.Sort = function (P, aList, Compare) {',
  27228. ' while (Compare(P, aList[0]) > 0) {',
  27229. ' };',
  27230. '};',
  27231. 'this.Compare = null;',
  27232. 'this.V = undefined;',
  27233. 'this.i = 0;',
  27234. '']),
  27235. LinesToStr([ // $mod.$main
  27236. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27237. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27238. 'if ($mod.Compare(null, false) > 2) ;',
  27239. 'if ($mod.Compare(1, true) > 3) ;',
  27240. '']));
  27241. end;
  27242. procedure TTestModule.TestJSValue_ProcType_Assign;
  27243. begin
  27244. StartProgram(false);
  27245. Add('type');
  27246. Add(' integer = longint;');
  27247. Add(' TObject = class');
  27248. Add(' class function GetGlob: integer;');
  27249. Add(' function Getter: integer;');
  27250. Add(' end;');
  27251. Add('class function TObject.GetGlob: integer;');
  27252. Add('var v1: jsvalue;');
  27253. Add('begin');
  27254. Add(' v1:=@GetGlob;');
  27255. Add(' v1:[email protected];');
  27256. Add('end;');
  27257. Add('function TObject.Getter: integer;');
  27258. Add('var v2: jsvalue;');
  27259. Add('begin');
  27260. Add(' v2:=@Getter;');
  27261. Add(' v2:[email protected];');
  27262. Add(' v2:=@GetGlob;');
  27263. Add(' v2:[email protected];');
  27264. Add('end;');
  27265. Add('function GetIt(i: integer): integer;');
  27266. Add('var v3: jsvalue;');
  27267. Add('begin');
  27268. Add(' v3:=@GetIt;');
  27269. Add('end;');
  27270. Add('var');
  27271. Add(' V: JSValue;');
  27272. Add(' o: TObject;');
  27273. Add('begin');
  27274. Add(' v:=@GetIt;');
  27275. Add(' v:[email protected];');
  27276. Add(' v:[email protected];');
  27277. ConvertProgram;
  27278. CheckSource('TestJSValue_ProcType_Assign',
  27279. LinesToStr([ // statements
  27280. 'rtl.createClass($mod, "TObject", null, function () {',
  27281. ' this.$init = function () {',
  27282. ' };',
  27283. ' this.$final = function () {',
  27284. ' };',
  27285. ' this.GetGlob = function () {',
  27286. ' var Result = 0;',
  27287. ' var v1 = undefined;',
  27288. ' v1 = rtl.createCallback(this, "GetGlob");',
  27289. ' v1 = rtl.createCallback(this, "GetGlob");',
  27290. ' return Result;',
  27291. ' };',
  27292. ' this.Getter = function () {',
  27293. ' var Result = 0;',
  27294. ' var v2 = undefined;',
  27295. ' v2 = rtl.createCallback(this, "Getter");',
  27296. ' v2 = rtl.createCallback(this, "Getter");',
  27297. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27298. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27299. ' return Result;',
  27300. ' };',
  27301. '});',
  27302. 'this.GetIt = function (i) {',
  27303. ' var Result = 0;',
  27304. ' var v3 = undefined;',
  27305. ' v3 = $mod.GetIt;',
  27306. ' return Result;',
  27307. '};',
  27308. 'this.V = undefined;',
  27309. 'this.o = null;',
  27310. '']),
  27311. LinesToStr([ // $mod.$main
  27312. '$mod.V = $mod.GetIt;',
  27313. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27314. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27315. '']));
  27316. end;
  27317. procedure TTestModule.TestJSValue_ProcType_Equal;
  27318. begin
  27319. StartProgram(false);
  27320. Add('type');
  27321. Add(' integer = longint;');
  27322. Add(' TObject = class');
  27323. Add(' class function GetGlob: integer;');
  27324. Add(' function Getter: integer;');
  27325. Add(' end;');
  27326. Add('class function TObject.GetGlob: integer;');
  27327. Add('var v1: jsvalue;');
  27328. Add('begin');
  27329. Add(' if v1=@GetGlob then;');
  27330. Add(' if [email protected] then ;');
  27331. Add('end;');
  27332. Add('function TObject.Getter: integer;');
  27333. Add('var v2: jsvalue;');
  27334. Add('begin');
  27335. Add(' if v2=@Getter then;');
  27336. Add(' if [email protected] then ;');
  27337. Add(' if v2=@GetGlob then;');
  27338. Add(' if [email protected] then;');
  27339. Add('end;');
  27340. Add('function GetIt(i: integer): integer;');
  27341. Add('var v3: jsvalue;');
  27342. Add('begin');
  27343. Add(' if v3=@GetIt then;');
  27344. Add('end;');
  27345. Add('var');
  27346. Add(' V: JSValue;');
  27347. Add(' o: TObject;');
  27348. Add('begin');
  27349. Add(' if v=@GetIt then;');
  27350. Add(' if [email protected] then;');
  27351. Add(' if [email protected] then;');
  27352. Add(' if @GetIt=v then;');
  27353. Add(' if @o.Getter=v then;');
  27354. Add(' if @o.GetGlob=v then;');
  27355. ConvertProgram;
  27356. CheckSource('TestJSValue_ProcType_Equal',
  27357. LinesToStr([ // statements
  27358. 'rtl.createClass($mod, "TObject", null, function () {',
  27359. ' this.$init = function () {',
  27360. ' };',
  27361. ' this.$final = function () {',
  27362. ' };',
  27363. ' this.GetGlob = function () {',
  27364. ' var Result = 0;',
  27365. ' var v1 = undefined;',
  27366. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27367. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27368. ' return Result;',
  27369. ' };',
  27370. ' this.Getter = function () {',
  27371. ' var Result = 0;',
  27372. ' var v2 = undefined;',
  27373. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27374. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27375. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27376. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27377. ' return Result;',
  27378. ' };',
  27379. '});',
  27380. 'this.GetIt = function (i) {',
  27381. ' var Result = 0;',
  27382. ' var v3 = undefined;',
  27383. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27384. ' return Result;',
  27385. '};',
  27386. 'this.V = undefined;',
  27387. 'this.o = null;',
  27388. '']),
  27389. LinesToStr([ // $mod.$main
  27390. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27391. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27392. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27393. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27394. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  27395. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  27396. '']));
  27397. end;
  27398. procedure TTestModule.TestJSValue_ProcType_Param;
  27399. begin
  27400. StartProgram(false);
  27401. Add([
  27402. 'type',
  27403. ' variant = jsvalue;',
  27404. ' TArrVariant = array of variant;',
  27405. ' TArrVar2 = TArrVariant;',
  27406. ' TFuncInt = function: longint;',
  27407. 'function GetIt: longint;',
  27408. 'begin',
  27409. 'end;',
  27410. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  27411. 'var v: variant;',
  27412. 'begin',
  27413. ' v:=arr[1];',
  27414. 'end;',
  27415. 'var s: string;',
  27416. 'begin',
  27417. ' DoIt(GetIt,[]);',
  27418. ' DoIt(@GetIt,[]);',
  27419. ' DoIt(1,[s,GetIt]);',
  27420. ' DoIt(1,[s,@GetIt]);',
  27421. '']);
  27422. ConvertProgram;
  27423. CheckSource('TestJSValue_ProcType_Param',
  27424. LinesToStr([ // statements
  27425. 'this.GetIt = function () {',
  27426. ' var Result = 0;',
  27427. ' return Result;',
  27428. '};',
  27429. 'this.DoIt = function (p, Arr) {',
  27430. ' var v = undefined;',
  27431. ' v = Arr[1];',
  27432. '};',
  27433. 'this.s = "";',
  27434. '']),
  27435. LinesToStr([ // $mod.$main
  27436. '$mod.DoIt($mod.GetIt(), []);',
  27437. '$mod.DoIt($mod.GetIt, []);',
  27438. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  27439. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  27440. '']));
  27441. end;
  27442. procedure TTestModule.TestJSValue_AssignToPointerFail;
  27443. begin
  27444. StartProgram(false);
  27445. Add([
  27446. 'var',
  27447. ' v: JSValue;',
  27448. ' p: Pointer;',
  27449. 'begin',
  27450. ' p:=v;',
  27451. '']);
  27452. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  27453. nIncompatibleTypesGotExpected);
  27454. ConvertProgram;
  27455. end;
  27456. procedure TTestModule.TestJSValue_OverloadDouble;
  27457. begin
  27458. StartProgram(false);
  27459. Add([
  27460. 'type',
  27461. ' integer = longint;',
  27462. ' tdatetime = double;',
  27463. 'procedure DoIt(d: double); begin end;',
  27464. 'procedure DoIt(v: jsvalue); begin end;',
  27465. 'var',
  27466. ' d: double;',
  27467. ' dt: tdatetime;',
  27468. ' i: integer;',
  27469. ' b: byte;',
  27470. ' shi: shortint;',
  27471. ' w: word;',
  27472. ' smi: smallint;',
  27473. ' lw: longword;',
  27474. ' li: longint;',
  27475. ' ni: nativeint;',
  27476. ' nu: nativeuint;',
  27477. 'begin',
  27478. ' DoIt(d);',
  27479. ' DoIt(dt);',
  27480. ' DoIt(i);',
  27481. ' DoIt(b);',
  27482. ' DoIt(shi);',
  27483. ' DoIt(w);',
  27484. ' DoIt(smi);',
  27485. ' DoIt(lw);',
  27486. ' DoIt(li);',
  27487. ' DoIt(ni);',
  27488. ' DoIt(nu);',
  27489. '']);
  27490. ConvertProgram;
  27491. CheckSource('TestJSValue_OverloadDouble',
  27492. LinesToStr([ // statements
  27493. 'this.DoIt = function (d) {',
  27494. '};',
  27495. 'this.DoIt$1 = function (v) {',
  27496. '};',
  27497. 'this.d = 0.0;',
  27498. 'this.dt = 0.0;',
  27499. 'this.i = 0;',
  27500. 'this.b = 0;',
  27501. 'this.shi = 0;',
  27502. 'this.w = 0;',
  27503. 'this.smi = 0;',
  27504. 'this.lw = 0;',
  27505. 'this.li = 0;',
  27506. 'this.ni = 0;',
  27507. 'this.nu = 0;',
  27508. '']),
  27509. LinesToStr([ // $mod.$main
  27510. '$mod.DoIt($mod.d);',
  27511. '$mod.DoIt($mod.dt);',
  27512. '$mod.DoIt$1($mod.i);',
  27513. '$mod.DoIt$1($mod.b);',
  27514. '$mod.DoIt$1($mod.shi);',
  27515. '$mod.DoIt$1($mod.w);',
  27516. '$mod.DoIt$1($mod.smi);',
  27517. '$mod.DoIt$1($mod.lw);',
  27518. '$mod.DoIt$1($mod.li);',
  27519. '$mod.DoIt$1($mod.ni);',
  27520. '$mod.DoIt$1($mod.nu);',
  27521. '']));
  27522. end;
  27523. procedure TTestModule.TestJSValue_OverloadNativeInt;
  27524. begin
  27525. StartProgram(false);
  27526. Add([
  27527. 'type',
  27528. ' integer = longint;',
  27529. ' int53 = nativeint;',
  27530. ' tdatetime = double;',
  27531. 'procedure DoIt(n: nativeint); begin end;',
  27532. 'procedure DoIt(v: jsvalue); begin end;',
  27533. 'var',
  27534. ' d: double;',
  27535. ' dt: tdatetime;',
  27536. ' i: integer;',
  27537. ' b: byte;',
  27538. ' shi: shortint;',
  27539. ' w: word;',
  27540. ' smi: smallint;',
  27541. ' lw: longword;',
  27542. ' li: longint;',
  27543. ' ni: nativeint;',
  27544. ' nu: nativeuint;',
  27545. 'begin',
  27546. ' DoIt(d);',
  27547. ' DoIt(dt);',
  27548. ' DoIt(i);',
  27549. ' DoIt(b);',
  27550. ' DoIt(shi);',
  27551. ' DoIt(w);',
  27552. ' DoIt(smi);',
  27553. ' DoIt(lw);',
  27554. ' DoIt(li);',
  27555. ' DoIt(ni);',
  27556. ' DoIt(nu);',
  27557. '']);
  27558. ConvertProgram;
  27559. CheckSource('TestJSValue_OverloadNativeInt',
  27560. LinesToStr([ // statements
  27561. 'this.DoIt = function (n) {',
  27562. '};',
  27563. 'this.DoIt$1 = function (v) {',
  27564. '};',
  27565. 'this.d = 0.0;',
  27566. 'this.dt = 0.0;',
  27567. 'this.i = 0;',
  27568. 'this.b = 0;',
  27569. 'this.shi = 0;',
  27570. 'this.w = 0;',
  27571. 'this.smi = 0;',
  27572. 'this.lw = 0;',
  27573. 'this.li = 0;',
  27574. 'this.ni = 0;',
  27575. 'this.nu = 0;',
  27576. '']),
  27577. LinesToStr([ // $mod.$main
  27578. '$mod.DoIt$1($mod.d);',
  27579. '$mod.DoIt$1($mod.dt);',
  27580. '$mod.DoIt($mod.i);',
  27581. '$mod.DoIt($mod.b);',
  27582. '$mod.DoIt($mod.shi);',
  27583. '$mod.DoIt($mod.w);',
  27584. '$mod.DoIt($mod.smi);',
  27585. '$mod.DoIt($mod.lw);',
  27586. '$mod.DoIt($mod.li);',
  27587. '$mod.DoIt($mod.ni);',
  27588. '$mod.DoIt($mod.nu);',
  27589. '']));
  27590. end;
  27591. procedure TTestModule.TestJSValue_OverloadWord;
  27592. begin
  27593. StartProgram(false);
  27594. Add([
  27595. 'type',
  27596. ' integer = longint;',
  27597. ' int53 = nativeint;',
  27598. ' tdatetime = double;',
  27599. 'procedure DoIt(w: word); begin end;',
  27600. 'procedure DoIt(v: jsvalue); begin end;',
  27601. 'var',
  27602. ' d: double;',
  27603. ' dt: tdatetime;',
  27604. ' i: integer;',
  27605. ' b: byte;',
  27606. ' shi: shortint;',
  27607. ' w: word;',
  27608. ' smi: smallint;',
  27609. ' lw: longword;',
  27610. ' li: longint;',
  27611. ' ni: nativeint;',
  27612. ' nu: nativeuint;',
  27613. 'begin',
  27614. ' DoIt(d);',
  27615. ' DoIt(dt);',
  27616. ' DoIt(i);',
  27617. ' DoIt(b);',
  27618. ' DoIt(shi);',
  27619. ' DoIt(w);',
  27620. ' DoIt(smi);',
  27621. ' DoIt(lw);',
  27622. ' DoIt(li);',
  27623. ' DoIt(ni);',
  27624. ' DoIt(nu);',
  27625. '']);
  27626. ConvertProgram;
  27627. CheckSource('TestJSValue_OverloadWord',
  27628. LinesToStr([ // statements
  27629. 'this.DoIt = function (w) {',
  27630. '};',
  27631. 'this.DoIt$1 = function (v) {',
  27632. '};',
  27633. 'this.d = 0.0;',
  27634. 'this.dt = 0.0;',
  27635. 'this.i = 0;',
  27636. 'this.b = 0;',
  27637. 'this.shi = 0;',
  27638. 'this.w = 0;',
  27639. 'this.smi = 0;',
  27640. 'this.lw = 0;',
  27641. 'this.li = 0;',
  27642. 'this.ni = 0;',
  27643. 'this.nu = 0;',
  27644. '']),
  27645. LinesToStr([ // $mod.$main
  27646. '$mod.DoIt$1($mod.d);',
  27647. '$mod.DoIt$1($mod.dt);',
  27648. '$mod.DoIt$1($mod.i);',
  27649. '$mod.DoIt($mod.b);',
  27650. '$mod.DoIt($mod.shi);',
  27651. '$mod.DoIt($mod.w);',
  27652. '$mod.DoIt$1($mod.smi);',
  27653. '$mod.DoIt$1($mod.lw);',
  27654. '$mod.DoIt$1($mod.li);',
  27655. '$mod.DoIt$1($mod.ni);',
  27656. '$mod.DoIt$1($mod.nu);',
  27657. '']));
  27658. end;
  27659. procedure TTestModule.TestJSValue_OverloadString;
  27660. begin
  27661. StartProgram(false);
  27662. Add([
  27663. 'type',
  27664. ' uni = string;',
  27665. ' WChar = char;',
  27666. 'procedure DoIt(s: string); begin end;',
  27667. 'procedure DoIt(v: jsvalue); begin end;',
  27668. 'var',
  27669. ' s: string;',
  27670. ' c: char;',
  27671. ' u: uni;',
  27672. 'begin',
  27673. ' DoIt(s);',
  27674. ' DoIt(c);',
  27675. ' DoIt(u);',
  27676. '']);
  27677. ConvertProgram;
  27678. CheckSource('TestJSValue_OverloadString',
  27679. LinesToStr([ // statements
  27680. 'this.DoIt = function (s) {',
  27681. '};',
  27682. 'this.DoIt$1 = function (v) {',
  27683. '};',
  27684. 'this.s = "";',
  27685. 'this.c = "";',
  27686. 'this.u = "";',
  27687. '']),
  27688. LinesToStr([ // $mod.$main
  27689. '$mod.DoIt($mod.s);',
  27690. '$mod.DoIt($mod.c);',
  27691. '$mod.DoIt($mod.u);',
  27692. '']));
  27693. end;
  27694. procedure TTestModule.TestJSValue_OverloadChar;
  27695. begin
  27696. StartProgram(false);
  27697. Add([
  27698. 'type',
  27699. ' uni = string;',
  27700. ' WChar = char;',
  27701. 'procedure DoIt(c: char); begin end;',
  27702. 'procedure DoIt(v: jsvalue); begin end;',
  27703. 'var',
  27704. ' s: string;',
  27705. ' c: char;',
  27706. ' u: uni;',
  27707. 'begin',
  27708. ' DoIt(s);',
  27709. ' DoIt(c);',
  27710. ' DoIt(u);',
  27711. '']);
  27712. ConvertProgram;
  27713. CheckSource('TestJSValue_OverloadChar',
  27714. LinesToStr([ // statements
  27715. 'this.DoIt = function (c) {',
  27716. '};',
  27717. 'this.DoIt$1 = function (v) {',
  27718. '};',
  27719. 'this.s = "";',
  27720. 'this.c = "";',
  27721. 'this.u = "";',
  27722. '']),
  27723. LinesToStr([ // $mod.$main
  27724. '$mod.DoIt$1($mod.s);',
  27725. '$mod.DoIt($mod.c);',
  27726. '$mod.DoIt$1($mod.u);',
  27727. '']));
  27728. end;
  27729. procedure TTestModule.TestJSValue_OverloadPointer;
  27730. begin
  27731. StartProgram(false);
  27732. Add([
  27733. 'type',
  27734. ' TObject = class end;',
  27735. 'procedure DoIt(p: pointer); begin end;',
  27736. 'procedure DoIt(v: jsvalue); begin end;',
  27737. 'var',
  27738. ' o: TObject;',
  27739. 'begin',
  27740. ' DoIt(o);',
  27741. '']);
  27742. ConvertProgram;
  27743. CheckSource('TestJSValue_OverloadPointer',
  27744. LinesToStr([ // statements
  27745. 'rtl.createClass($mod, "TObject", null, function () {',
  27746. ' this.$init = function () {',
  27747. ' };',
  27748. ' this.$final = function () {',
  27749. ' };',
  27750. '});',
  27751. 'this.DoIt = function (p) {',
  27752. '};',
  27753. 'this.DoIt$1 = function (v) {',
  27754. '};',
  27755. 'this.o = null;',
  27756. '']),
  27757. LinesToStr([ // $mod.$main
  27758. '$mod.DoIt($mod.o);',
  27759. '']));
  27760. end;
  27761. procedure TTestModule.TestJSValue_ForIn;
  27762. begin
  27763. StartProgram(false);
  27764. Add([
  27765. 'var',
  27766. ' v: JSValue;',
  27767. ' key: string;',
  27768. 'begin',
  27769. ' for key in v do begin',
  27770. ' if key=''abc'' then ;',
  27771. ' end;',
  27772. '']);
  27773. ConvertProgram;
  27774. CheckSource('TestJSValue_ForIn',
  27775. LinesToStr([ // statements
  27776. 'this.v = undefined;',
  27777. 'this.key = "";',
  27778. '']),
  27779. LinesToStr([ // $mod.$main
  27780. 'for ($mod.key in $mod.v) {',
  27781. ' if ($mod.key === "abc") ;',
  27782. '};',
  27783. '']));
  27784. end;
  27785. procedure TTestModule.TestRTTI_IntRange;
  27786. begin
  27787. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27788. StartProgram(true,[supTypeInfo]);
  27789. Add([
  27790. '{$modeswitch externalclass}',
  27791. 'type',
  27792. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  27793. ' TColor = type TGraphicsColor;',
  27794. 'var',
  27795. ' p: TTypeInfo;',
  27796. 'begin',
  27797. ' p:=typeinfo(TGraphicsColor);',
  27798. ' p:=typeinfo(TColor);',
  27799. '']);
  27800. ConvertProgram;
  27801. CheckSource('TestRTTI_IntRange',
  27802. LinesToStr([ // statements
  27803. '$mod.$rtti.$Int("TGraphicsColor", {',
  27804. ' minvalue: -2147483648,',
  27805. ' maxvalue: 2147483647,',
  27806. ' ordtype: 4',
  27807. '});',
  27808. '$mod.$rtti.$inherited("TColor", $mod.$rtti["TGraphicsColor"], {});',
  27809. 'this.p = null;',
  27810. '']),
  27811. LinesToStr([ // $mod.$main
  27812. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  27813. '$mod.p = $mod.$rtti["TColor"];',
  27814. '']));
  27815. end;
  27816. procedure TTestModule.TestRTTI_Double;
  27817. begin
  27818. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27819. StartProgram(true,[supTypeInfo]);
  27820. Add([
  27821. '{$modeswitch externalclass}',
  27822. 'type',
  27823. ' TFloat = type double;',
  27824. 'var',
  27825. ' p: TTypeInfo;',
  27826. 'begin',
  27827. ' p:=typeinfo(double);',
  27828. ' p:=typeinfo(TFloat);',
  27829. '']);
  27830. ConvertProgram;
  27831. CheckSource('TestRTTI_Double',
  27832. LinesToStr([ // statements
  27833. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  27834. 'this.p = null;',
  27835. '']),
  27836. LinesToStr([ // $mod.$main
  27837. '$mod.p = rtl.double;',
  27838. '$mod.p = $mod.$rtti["TFloat"];',
  27839. '']));
  27840. end;
  27841. procedure TTestModule.TestRTTI_ProcType;
  27842. begin
  27843. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27844. StartProgram(false);
  27845. Add('type');
  27846. Add(' TProcA = procedure;');
  27847. Add(' TMethodB = procedure of object;');
  27848. Add(' TProcC = procedure; varargs;');
  27849. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  27850. Add(' TProcE = function: nativeint;');
  27851. Add(' TProcF = function(const p: TProcA): nativeuint;');
  27852. Add('var p: pointer;');
  27853. Add('begin');
  27854. Add(' p:=typeinfo(tproca);');
  27855. ConvertProgram;
  27856. CheckSource('TestRTTI_ProcType',
  27857. LinesToStr([ // statements
  27858. '$mod.$rtti.$ProcVar("TProcA", {',
  27859. ' procsig: rtl.newTIProcSig(null)',
  27860. '});',
  27861. '$mod.$rtti.$MethodVar("TMethodB", {',
  27862. ' procsig: rtl.newTIProcSig(null),',
  27863. ' methodkind: 0',
  27864. '});',
  27865. '$mod.$rtti.$ProcVar("TProcC", {',
  27866. ' procsig: rtl.newTIProcSig(null, 2)',
  27867. '});',
  27868. '$mod.$rtti.$ProcVar("TProcD", {',
  27869. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  27870. '});',
  27871. '$mod.$rtti.$ProcVar("TProcE", {',
  27872. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  27873. '});',
  27874. '$mod.$rtti.$ProcVar("TProcF", {',
  27875. ' procsig: rtl.newTIProcSig([["p", $mod.$rtti["TProcA"], 2]], rtl.nativeuint)',
  27876. '});',
  27877. 'this.p = null;',
  27878. '']),
  27879. LinesToStr([ // $mod.$main
  27880. '$mod.p = $mod.$rtti["TProcA"];',
  27881. '']));
  27882. end;
  27883. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  27884. begin
  27885. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27886. AddModuleWithIntfImplSrc('unit2.pas',
  27887. LinesToStr([
  27888. 'type',
  27889. ' TObject = class end;'
  27890. ]),
  27891. '');
  27892. StartUnit(true);
  27893. Add('interface');
  27894. Add('uses unit2;');
  27895. Add('type');
  27896. Add(' TProcA = function(o: tobject): tobject;');
  27897. Add('implementation');
  27898. Add('type');
  27899. Add(' TProcB = function(o: tobject): tobject;');
  27900. Add('var p: Pointer;');
  27901. Add('initialization');
  27902. Add(' p:=typeinfo(tproca);');
  27903. Add(' p:=typeinfo(tprocb);');
  27904. ConvertUnit;
  27905. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  27906. LinesToStr([ // statements
  27907. 'var $impl = $mod.$impl;',
  27908. '$mod.$rtti.$ProcVar("TProcA", {',
  27909. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  27910. '});',
  27911. '']),
  27912. LinesToStr([ // this.$init
  27913. '$impl.p = $mod.$rtti["TProcA"];',
  27914. '$impl.p = $mod.$rtti["TProcB"];',
  27915. '']),
  27916. LinesToStr([ // implementation
  27917. '$mod.$rtti.$ProcVar("TProcB", {',
  27918. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  27919. '});',
  27920. '$impl.p = null;',
  27921. '']) );
  27922. end;
  27923. procedure TTestModule.TestRTTI_EnumAndSetType;
  27924. begin
  27925. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27926. StartProgram(false);
  27927. Add('type');
  27928. Add(' TFlag = (light,dark);');
  27929. Add(' TFlags = set of TFlag;');
  27930. Add(' TProc = function(f: TFlags): TFlag;');
  27931. Add('var p: pointer;');
  27932. Add('begin');
  27933. Add(' p:=typeinfo(tflag);');
  27934. Add(' p:=typeinfo(tflags);');
  27935. ConvertProgram;
  27936. CheckSource('TestRTTI_EnumAndType',
  27937. LinesToStr([ // statements
  27938. 'this.TFlag = {',
  27939. ' "0": "light",',
  27940. ' light: 0,',
  27941. ' "1": "dark",',
  27942. ' dark: 1',
  27943. '};',
  27944. '$mod.$rtti.$Enum("TFlag", {',
  27945. ' minvalue: 0,',
  27946. ' maxvalue: 1,',
  27947. ' ordtype: 1,',
  27948. ' enumtype: this.TFlag',
  27949. '});',
  27950. '$mod.$rtti.$Set("TFlags", {',
  27951. ' comptype: $mod.$rtti["TFlag"]',
  27952. '});',
  27953. '$mod.$rtti.$ProcVar("TProc", {',
  27954. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TFlags"]]], $mod.$rtti["TFlag"])',
  27955. '});',
  27956. 'this.p = null;',
  27957. '']),
  27958. LinesToStr([ // $mod.$main
  27959. '$mod.p = $mod.$rtti["TFlag"];',
  27960. '$mod.p = $mod.$rtti["TFlags"];',
  27961. '']));
  27962. end;
  27963. procedure TTestModule.TestRTTI_EnumRange;
  27964. begin
  27965. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27966. StartProgram(false);
  27967. Add([
  27968. 'type',
  27969. ' TCol = (red,green,blue);',
  27970. ' TColRg = green..blue;',
  27971. ' TSetOfColRg = set of TColRg;',
  27972. 'var p: pointer;',
  27973. 'begin',
  27974. ' p:=typeinfo(tcolrg);',
  27975. ' p:=typeinfo(tsetofcolrg);',
  27976. '']);
  27977. ConvertProgram;
  27978. end;
  27979. procedure TTestModule.TestRTTI_AnonymousEnumType;
  27980. begin
  27981. Converter.Options:=Converter.Options-[coNoTypeInfo];
  27982. StartProgram(false);
  27983. Add('type');
  27984. Add(' TFlags = set of (red, green);');
  27985. Add('var');
  27986. Add(' f: TFlags;');
  27987. Add('begin');
  27988. Add(' Include(f,red);');
  27989. ConvertProgram;
  27990. CheckSource('TestRTTI_AnonymousEnumType',
  27991. LinesToStr([ // statements
  27992. 'this.TFlags$a = {',
  27993. ' "0": "red",',
  27994. ' red: 0,',
  27995. ' "1": "green",',
  27996. ' green: 1',
  27997. '};',
  27998. '$mod.$rtti.$Enum("TFlags$a", {',
  27999. ' minvalue: 0,',
  28000. ' maxvalue: 1,',
  28001. ' ordtype: 1,',
  28002. ' enumtype: this.TFlags$a',
  28003. '});',
  28004. '$mod.$rtti.$Set("TFlags", {',
  28005. ' comptype: $mod.$rtti["TFlags$a"]',
  28006. '});',
  28007. 'this.f = {};',
  28008. '']),
  28009. LinesToStr([
  28010. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28011. '']));
  28012. end;
  28013. procedure TTestModule.TestRTTI_StaticArray;
  28014. begin
  28015. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28016. StartProgram(false);
  28017. Add('type');
  28018. Add(' TFlag = (light,dark);');
  28019. Add(' TFlagNames = array[TFlag] of string;');
  28020. Add(' TBoolNames = array[boolean] of string;');
  28021. Add(' TByteArray = array[1..32768] of byte;');
  28022. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28023. Add('var p: pointer;');
  28024. Add('begin');
  28025. Add(' p:=typeinfo(TFlagNames);');
  28026. Add(' p:=typeinfo(TBoolNames);');
  28027. ConvertProgram;
  28028. CheckSource('TestRTTI_StaticArray',
  28029. LinesToStr([ // statements
  28030. 'this.TFlag = {',
  28031. ' "0": "light",',
  28032. ' light: 0,',
  28033. ' "1": "dark",',
  28034. ' dark: 1',
  28035. '};',
  28036. '$mod.$rtti.$Enum("TFlag", {',
  28037. ' minvalue: 0,',
  28038. ' maxvalue: 1,',
  28039. ' ordtype: 1,',
  28040. ' enumtype: this.TFlag',
  28041. '});',
  28042. '$mod.$rtti.$StaticArray("TFlagNames", {',
  28043. ' dims: [2],',
  28044. ' eltype: rtl.string',
  28045. '});',
  28046. '$mod.$rtti.$StaticArray("TBoolNames", {',
  28047. ' dims: [2],',
  28048. ' eltype: rtl.string',
  28049. '});',
  28050. '$mod.$rtti.$StaticArray("TByteArray", {',
  28051. ' dims: [32768],',
  28052. ' eltype: rtl.byte',
  28053. '});',
  28054. '$mod.$rtti.$ProcVar("TProc", {',
  28055. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TBoolNames"]]], $mod.$rtti["TFlagNames"])',
  28056. '});',
  28057. 'this.p = null;',
  28058. '']),
  28059. LinesToStr([ // $mod.$main
  28060. '$mod.p = $mod.$rtti["TFlagNames"];',
  28061. '$mod.p = $mod.$rtti["TBoolNames"];',
  28062. '']));
  28063. end;
  28064. procedure TTestModule.TestRTTI_DynArray;
  28065. begin
  28066. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28067. StartProgram(false);
  28068. Add('type');
  28069. Add(' TArrStr = array of string;');
  28070. Add(' TArr2Dim = array of tarrstr;');
  28071. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28072. Add('var p: pointer;');
  28073. Add('begin');
  28074. Add(' p:=typeinfo(tarrstr);');
  28075. Add(' p:=typeinfo(tarr2dim);');
  28076. ConvertProgram;
  28077. CheckSource('TestRTTI_DynArray',
  28078. LinesToStr([ // statements
  28079. '$mod.$rtti.$DynArray("TArrStr", {',
  28080. ' eltype: rtl.string',
  28081. '});',
  28082. '$mod.$rtti.$DynArray("TArr2Dim", {',
  28083. ' eltype: $mod.$rtti["TArrStr"]',
  28084. '});',
  28085. '$mod.$rtti.$ProcVar("TProc", {',
  28086. ' procsig: rtl.newTIProcSig([["f", $mod.$rtti["TArrStr"]]], $mod.$rtti["TArr2Dim"])',
  28087. '});',
  28088. 'this.p = null;',
  28089. '']),
  28090. LinesToStr([ // $mod.$main
  28091. '$mod.p = $mod.$rtti["TArrStr"];',
  28092. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28093. '']));
  28094. end;
  28095. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28096. begin
  28097. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28098. StartProgram(false);
  28099. Add('type');
  28100. Add(' TArr = array of array of longint;');
  28101. Add('var a: TArr;');
  28102. Add('begin');
  28103. ConvertProgram;
  28104. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28105. LinesToStr([ // statements
  28106. '$mod.$rtti.$DynArray("TArr$a", {',
  28107. ' eltype: rtl.longint',
  28108. '});',
  28109. '$mod.$rtti.$DynArray("TArr", {',
  28110. ' eltype: $mod.$rtti["TArr$a"]',
  28111. '});',
  28112. 'this.a = [];',
  28113. '']),
  28114. LinesToStr([ // $mod.$main
  28115. ]));
  28116. end;
  28117. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28118. begin
  28119. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28120. StartProgram(false);
  28121. Add('type');
  28122. Add(' TObject = class');
  28123. Add(' published');
  28124. Add(' procedure Proc; virtual; abstract;');
  28125. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28126. Add(' end;');
  28127. Add('begin');
  28128. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28129. nDuplicatePublishedMethodXAtY);
  28130. ConvertProgram;
  28131. end;
  28132. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28133. begin
  28134. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28135. StartProgram(false);
  28136. Add('type');
  28137. Add(' TObject = class');
  28138. Add(' published');
  28139. Add(' procedure Proc; external name ''foo'';');
  28140. Add(' end;');
  28141. Add('begin');
  28142. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28143. nPublishedNameMustMatchExternal);
  28144. ConvertProgram;
  28145. end;
  28146. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28147. begin
  28148. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28149. StartProgram(false);
  28150. Add('type');
  28151. Add(' TObject = class');
  28152. Add(' class var FA: longint;');
  28153. Add(' published');
  28154. Add(' class property A: longint read FA;');
  28155. Add(' end;');
  28156. Add('begin');
  28157. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28158. nInvalidXModifierY);
  28159. ConvertProgram;
  28160. end;
  28161. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28162. begin
  28163. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28164. StartProgram(false);
  28165. Add('type');
  28166. Add(' TObject = class');
  28167. Add(' published');
  28168. Add(' class var FA: longint;');
  28169. Add(' end;');
  28170. Add('begin');
  28171. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28172. nSymbolCannotBePublished);
  28173. ConvertProgram;
  28174. end;
  28175. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28176. begin
  28177. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28178. StartProgram(false);
  28179. Add('{$modeswitch externalclass}');
  28180. Add('type');
  28181. Add(' TObject = class');
  28182. Add(' published');
  28183. Add(' V: longint; external name ''foo'';');
  28184. Add(' end;');
  28185. Add('begin');
  28186. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28187. nPublishedNameMustMatchExternal);
  28188. ConvertProgram;
  28189. end;
  28190. procedure TTestModule.TestRTTI_Class_Field;
  28191. begin
  28192. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28193. StartProgram(false);
  28194. Add('{$modeswitch externalclass}');
  28195. Add('type');
  28196. Add(' TObject = class');
  28197. Add(' private');
  28198. Add(' FPropA: string;');
  28199. Add(' published');
  28200. Add(' VarLI: longint;');
  28201. Add(' VarC: char;');
  28202. Add(' VarS: string;');
  28203. Add(' VarD: double;');
  28204. Add(' VarB: boolean;');
  28205. Add(' VarLW: longword;');
  28206. Add(' VarSmI: smallint;');
  28207. Add(' VarW: word;');
  28208. Add(' VarShI: shortint;');
  28209. Add(' VarBy: byte;');
  28210. Add(' VarExt: longint external name ''VarExt'';');
  28211. Add(' ArrA, ArrB: array of byte;');
  28212. Add(' end;');
  28213. Add('var p: pointer;');
  28214. Add(' Obj: tobject;');
  28215. Add('begin');
  28216. Add(' p:=typeinfo(tobject);');
  28217. Add(' p:=typeinfo(p);');
  28218. Add(' p:=typeinfo(obj);');
  28219. ConvertProgram;
  28220. CheckSource('TestRTTI_Class_Field',
  28221. LinesToStr([ // statements
  28222. 'rtl.createClass($mod, "TObject", null, function () {',
  28223. ' this.$init = function () {',
  28224. ' this.FPropA = "";',
  28225. ' this.VarLI = 0;',
  28226. ' this.VarC = "";',
  28227. ' this.VarS = "";',
  28228. ' this.VarD = 0.0;',
  28229. ' this.VarB = false;',
  28230. ' this.VarLW = 0;',
  28231. ' this.VarSmI = 0;',
  28232. ' this.VarW = 0;',
  28233. ' this.VarShI = 0;',
  28234. ' this.VarBy = 0;',
  28235. ' this.ArrA = [];',
  28236. ' this.ArrB = [];',
  28237. ' };',
  28238. ' this.$final = function () {',
  28239. ' this.ArrA = undefined;',
  28240. ' this.ArrB = undefined;',
  28241. ' };',
  28242. ' var $r = this.$rtti;',
  28243. ' $r.addField("VarLI", rtl.longint);',
  28244. ' $r.addField("VarC", rtl.char);',
  28245. ' $r.addField("VarS", rtl.string);',
  28246. ' $r.addField("VarD", rtl.double);',
  28247. ' $r.addField("VarB", rtl.boolean);',
  28248. ' $r.addField("VarLW", rtl.longword);',
  28249. ' $r.addField("VarSmI", rtl.smallint);',
  28250. ' $r.addField("VarW", rtl.word);',
  28251. ' $r.addField("VarShI", rtl.shortint);',
  28252. ' $r.addField("VarBy", rtl.byte);',
  28253. ' $r.addField("VarExt", rtl.longint);',
  28254. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28255. ' eltype: rtl.byte',
  28256. ' });',
  28257. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28258. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28259. '});',
  28260. 'this.p = null;',
  28261. 'this.Obj = null;',
  28262. '']),
  28263. LinesToStr([ // $mod.$main
  28264. '$mod.p = $mod.$rtti["TObject"];',
  28265. '$mod.p = rtl.pointer;',
  28266. '$mod.p = $mod.Obj.$rtti;',
  28267. '']));
  28268. end;
  28269. procedure TTestModule.TestRTTI_Class_Method;
  28270. begin
  28271. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28272. StartProgram(false);
  28273. Add('type');
  28274. Add(' TObject = class');
  28275. Add(' private');
  28276. Add(' procedure Internal; external name ''$intern'';');
  28277. Add(' published');
  28278. Add(' procedure Click; virtual; abstract;');
  28279. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28280. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28281. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28282. Add(' end;');
  28283. Add('begin');
  28284. ConvertProgram;
  28285. CheckSource('TestRTTI_Class_Method',
  28286. LinesToStr([ // statements
  28287. 'rtl.createClass($mod, "TObject", null, function () {',
  28288. ' this.$init = function () {',
  28289. ' };',
  28290. ' this.$final = function () {',
  28291. ' };',
  28292. ' var $r = this.$rtti;',
  28293. ' $r.addMethod("Click", 0, null);',
  28294. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28295. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28296. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28297. ' flags: 2',
  28298. ' });',
  28299. '});',
  28300. '']),
  28301. LinesToStr([ // $mod.$main
  28302. '']));
  28303. end;
  28304. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28305. begin
  28306. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28307. StartProgram(false);
  28308. Add('type');
  28309. Add(' TObject = class');
  28310. Add(' published');
  28311. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28312. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28313. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28314. Add(' end;');
  28315. Add('begin');
  28316. ConvertProgram;
  28317. CheckSource('TestRTTI_Class_MethodOpenArray',
  28318. LinesToStr([ // statements
  28319. 'rtl.createClass($mod, "TObject", null, function () {',
  28320. ' this.$init = function () {',
  28321. ' };',
  28322. ' this.$final = function () {',
  28323. ' };',
  28324. ' var $r = this.$rtti;',
  28325. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28326. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28327. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28328. '});',
  28329. '']),
  28330. LinesToStr([ // $mod.$main
  28331. '']));
  28332. end;
  28333. procedure TTestModule.TestRTTI_Class_Property;
  28334. begin
  28335. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28336. StartProgram(false);
  28337. Add('{$modeswitch externalclass}');
  28338. Add('type');
  28339. Add(' TObject = class');
  28340. Add(' private');
  28341. Add(' FColor: longint;');
  28342. Add(' FColorStored: boolean;');
  28343. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28344. Add(' function GetColor: longint; virtual; abstract;');
  28345. Add(' function GetColorStored: boolean; virtual; abstract;');
  28346. Add(' FExtSize: longint external name ''$extSize'';');
  28347. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28348. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28349. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28350. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28351. Add(' published');
  28352. Add(' property ColorA: longint read FColor;');
  28353. Add(' property ColorB: longint write FColor;');
  28354. Add(' property ColorC: longint read GetColor write SetColor;');
  28355. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28356. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28357. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28358. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28359. Add(' end;');
  28360. Add('begin');
  28361. ConvertProgram;
  28362. CheckSource('TestRTTI_Class_Property',
  28363. LinesToStr([ // statements
  28364. 'rtl.createClass($mod, "TObject", null, function () {',
  28365. ' this.$init = function () {',
  28366. ' this.FColor = 0;',
  28367. ' this.FColorStored = false;',
  28368. ' };',
  28369. ' this.$final = function () {',
  28370. ' };',
  28371. ' var $r = this.$rtti;',
  28372. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28373. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28374. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28375. ' $r.addProperty(',
  28376. ' "ColorD",',
  28377. ' 8,',
  28378. ' rtl.longint,',
  28379. ' "FColor",',
  28380. ' "FColor",',
  28381. ' {',
  28382. ' stored: "FColorStored"',
  28383. ' }',
  28384. ' );',
  28385. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28386. ' $r.addProperty(',
  28387. ' "ExtSizeB",',
  28388. ' 11,',
  28389. ' rtl.longint,',
  28390. ' "$getSize",',
  28391. ' "$setSize",',
  28392. ' {',
  28393. ' stored: "$extSizeStored"',
  28394. ' }',
  28395. ' );',
  28396. ' $r.addProperty(',
  28397. ' "ExtSizeC",',
  28398. ' 12,',
  28399. ' rtl.longint,',
  28400. ' "$extSize",',
  28401. ' "$extSize",',
  28402. ' {',
  28403. ' stored: "$getExtSizeStored"',
  28404. ' }',
  28405. ' );',
  28406. '});',
  28407. '']),
  28408. LinesToStr([ // $mod.$main
  28409. '']));
  28410. end;
  28411. procedure TTestModule.TestRTTI_Class_PropertyParams;
  28412. begin
  28413. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28414. StartProgram(false);
  28415. Add('{$modeswitch externalclass}');
  28416. Add('type');
  28417. Add(' integer = longint;');
  28418. Add(' TObject = class');
  28419. Add(' private');
  28420. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  28421. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  28422. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  28423. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  28424. Add(' published');
  28425. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  28426. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  28427. Add(' end;');
  28428. Add('begin');
  28429. ConvertProgram;
  28430. CheckSource('TestRTTI_Class_PropertyParams',
  28431. LinesToStr([ // statements
  28432. 'rtl.createClass($mod, "TObject", null, function () {',
  28433. ' this.$init = function () {',
  28434. ' };',
  28435. ' this.$final = function () {',
  28436. ' };',
  28437. ' var $r = this.$rtti;',
  28438. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  28439. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  28440. '});',
  28441. '']),
  28442. LinesToStr([ // $mod.$main
  28443. '']));
  28444. end;
  28445. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  28446. begin
  28447. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28448. AddModuleWithIntfImplSrc('unit1.pas',
  28449. 'type TColor = -5..5;',
  28450. '');
  28451. StartProgram(true);
  28452. Add([
  28453. 'uses unit1;',
  28454. 'type',
  28455. ' TColorAlias = TColor;',
  28456. ' TColorTypeAlias = type TColor;',
  28457. ' TObject = class',
  28458. ' private',
  28459. ' fColor: TColor;',
  28460. ' fAlias: TColorAlias;',
  28461. ' fTypeAlias: TColorTypeAlias;',
  28462. ' published',
  28463. ' property Color: TColor read fcolor;',
  28464. ' property Alias: TColorAlias read falias;',
  28465. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  28466. ' end;',
  28467. 'begin',
  28468. '']);
  28469. ConvertProgram;
  28470. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  28471. LinesToStr([ // statements
  28472. '$mod.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  28473. 'rtl.createClass($mod, "TObject", null, function () {',
  28474. ' this.$init = function () {',
  28475. ' this.fColor = 0;',
  28476. ' this.fAlias = 0;',
  28477. ' this.fTypeAlias = 0;',
  28478. ' };',
  28479. ' this.$final = function () {',
  28480. ' };',
  28481. ' var $r = this.$rtti;',
  28482. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  28483. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  28484. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  28485. '});',
  28486. '']),
  28487. LinesToStr([ // $mod.$main
  28488. '']));
  28489. end;
  28490. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  28491. begin
  28492. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28493. StartProgram(false);
  28494. Add([
  28495. '{$modeswitch omitrtti}',
  28496. 'type',
  28497. ' TObject = class',
  28498. ' private',
  28499. ' FA: byte;',
  28500. ' published',
  28501. ' property A: byte read FA write FA;',
  28502. ' end;',
  28503. 'begin']);
  28504. ConvertProgram;
  28505. CheckSource('TestRTTI_Class_OmitRTTI',
  28506. LinesToStr([ // statements
  28507. 'rtl.createClass($mod, "TObject", null, function () {',
  28508. ' this.$init = function () {',
  28509. ' this.FA = 0;',
  28510. ' };',
  28511. ' this.$final = function () {',
  28512. ' };',
  28513. '});',
  28514. '']),
  28515. LinesToStr([ // $mod.$main
  28516. '']));
  28517. end;
  28518. procedure TTestModule.TestRTTI_IndexModifier;
  28519. begin
  28520. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28521. StartProgram(false);
  28522. Add([
  28523. 'type',
  28524. ' TEnum = (red, blue);',
  28525. ' TObject = class',
  28526. ' FB: boolean;',
  28527. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  28528. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  28529. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  28530. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  28531. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  28532. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  28533. ' published',
  28534. ' property B1: boolean index 1 read FB write SetIntBool;',
  28535. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  28536. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  28537. ' end;',
  28538. 'begin']);
  28539. ConvertProgram;
  28540. CheckSource('TestRTTI_IndexModifier',
  28541. LinesToStr([ // statements
  28542. 'this.TEnum = {',
  28543. ' "0": "red",',
  28544. ' red: 0,',
  28545. ' "1": "blue",',
  28546. ' blue: 1',
  28547. '};',
  28548. '$mod.$rtti.$Enum("TEnum", {',
  28549. ' minvalue: 0,',
  28550. ' maxvalue: 1,',
  28551. ' ordtype: 1,',
  28552. ' enumtype: this.TEnum',
  28553. '});',
  28554. 'rtl.createClass($mod, "TObject", null, function () {',
  28555. ' this.$init = function () {',
  28556. ' this.FB = false;',
  28557. ' };',
  28558. ' this.$final = function () {',
  28559. ' };',
  28560. ' var $r = this.$rtti;',
  28561. ' $r.addProperty(',
  28562. ' "B1",',
  28563. ' 18,',
  28564. ' rtl.boolean,',
  28565. ' "FB",',
  28566. ' "SetIntBool",',
  28567. ' {',
  28568. ' index: 1',
  28569. ' }',
  28570. ' );',
  28571. ' $r.addProperty(',
  28572. ' "B2",',
  28573. ' 17,',
  28574. ' rtl.boolean,',
  28575. ' "GetEnumBool",',
  28576. ' "FB",',
  28577. ' {',
  28578. ' index: $mod.TEnum.blue',
  28579. ' }',
  28580. ' );',
  28581. ' $r.addProperty(',
  28582. ' "I1",',
  28583. ' 19,',
  28584. ' rtl.boolean,',
  28585. ' "GetStrIntBool",',
  28586. ' "SetStrIntBool",',
  28587. ' {',
  28588. ' index: 2',
  28589. ' }',
  28590. ' );',
  28591. '});',
  28592. '']),
  28593. LinesToStr([ // $mod.$main
  28594. '']));
  28595. end;
  28596. procedure TTestModule.TestRTTI_StoredModifier;
  28597. begin
  28598. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28599. StartProgram(false);
  28600. Add([
  28601. 'const',
  28602. ' ConstB = true;',
  28603. 'type',
  28604. ' TObject = class',
  28605. ' private',
  28606. ' FB: boolean;',
  28607. ' function IsBStored: boolean; virtual; abstract;',
  28608. ' published',
  28609. ' property BoolA: boolean read FB stored true;',
  28610. ' property BoolB: boolean read FB stored false;',
  28611. ' property BoolC: boolean read FB stored FB;',
  28612. ' property BoolD: boolean read FB stored ConstB;',
  28613. ' property BoolE: boolean read FB stored IsBStored;',
  28614. ' end;',
  28615. 'begin']);
  28616. ConvertProgram;
  28617. CheckSource('TestRTTI_StoredModifier',
  28618. LinesToStr([ // statements
  28619. 'this.ConstB = true;',
  28620. 'rtl.createClass($mod, "TObject", null, function () {',
  28621. ' this.$init = function () {',
  28622. ' this.FB = false;',
  28623. ' };',
  28624. ' this.$final = function () {',
  28625. ' };',
  28626. ' var $r = this.$rtti;',
  28627. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  28628. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  28629. ' $r.addProperty(',
  28630. ' "BoolC",',
  28631. ' 8,',
  28632. ' rtl.boolean,',
  28633. ' "FB",',
  28634. ' "",',
  28635. ' {',
  28636. ' stored: "FB"',
  28637. ' }',
  28638. ' );',
  28639. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  28640. ' $r.addProperty(',
  28641. ' "BoolE",',
  28642. ' 12,',
  28643. ' rtl.boolean,',
  28644. ' "FB",',
  28645. ' "",',
  28646. ' {',
  28647. ' stored: "IsBStored"',
  28648. ' }',
  28649. ' );',
  28650. '});',
  28651. '']),
  28652. LinesToStr([ // $mod.$main
  28653. '']));
  28654. end;
  28655. procedure TTestModule.TestRTTI_DefaultValue;
  28656. begin
  28657. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28658. StartProgram(false);
  28659. Add([
  28660. 'type',
  28661. ' TEnum = (red, blue);',
  28662. 'const',
  28663. ' CB = true or false;',
  28664. ' CI = 1+2;',
  28665. 'type',
  28666. ' TObject = class',
  28667. ' FB: boolean;',
  28668. ' FI: longint;',
  28669. ' FE: TEnum;',
  28670. ' published',
  28671. ' property B1: boolean read FB default true;',
  28672. ' property B2: boolean read FB default CB;',
  28673. ' property B3: boolean read FB default test1.cb;',
  28674. ' property I1: longint read FI default 2;',
  28675. ' property I2: longint read FI default CI;',
  28676. ' property E1: TEnum read FE default red;',
  28677. ' property E2: TEnum read FE default TEnum.blue;',
  28678. ' end;',
  28679. 'begin']);
  28680. ConvertProgram;
  28681. CheckSource('TestRTTI_DefaultValue',
  28682. LinesToStr([ // statements
  28683. 'this.TEnum = {',
  28684. ' "0": "red",',
  28685. ' red: 0,',
  28686. ' "1": "blue",',
  28687. ' blue: 1',
  28688. '};',
  28689. '$mod.$rtti.$Enum("TEnum", {',
  28690. ' minvalue: 0,',
  28691. ' maxvalue: 1,',
  28692. ' ordtype: 1,',
  28693. ' enumtype: this.TEnum',
  28694. '});',
  28695. 'this.CB = true || false;',
  28696. 'this.CI = 1 + 2;',
  28697. 'rtl.createClass($mod, "TObject", null, function () {',
  28698. ' this.$init = function () {',
  28699. ' this.FB = false;',
  28700. ' this.FI = 0;',
  28701. ' this.FE = 0;',
  28702. ' };',
  28703. ' this.$final = function () {',
  28704. ' };',
  28705. ' var $r = this.$rtti;',
  28706. ' $r.addProperty(',
  28707. ' "B1",',
  28708. ' 0,',
  28709. ' rtl.boolean,',
  28710. ' "FB",',
  28711. ' "",',
  28712. ' {',
  28713. ' Default: true',
  28714. ' }',
  28715. ' );',
  28716. ' $r.addProperty(',
  28717. ' "B2",',
  28718. ' 0,',
  28719. ' rtl.boolean,',
  28720. ' "FB",',
  28721. ' "",',
  28722. ' {',
  28723. ' Default: true',
  28724. ' }',
  28725. ' );',
  28726. ' $r.addProperty(',
  28727. ' "B3",',
  28728. ' 0,',
  28729. ' rtl.boolean,',
  28730. ' "FB",',
  28731. ' "",',
  28732. ' {',
  28733. ' Default: true',
  28734. ' }',
  28735. ' );',
  28736. ' $r.addProperty(',
  28737. ' "I1",',
  28738. ' 0,',
  28739. ' rtl.longint,',
  28740. ' "FI",',
  28741. ' "",',
  28742. ' {',
  28743. ' Default: 2',
  28744. ' }',
  28745. ' );',
  28746. ' $r.addProperty(',
  28747. ' "I2",',
  28748. ' 0,',
  28749. ' rtl.longint,',
  28750. ' "FI",',
  28751. ' "",',
  28752. ' {',
  28753. ' Default: 3',
  28754. ' }',
  28755. ' );',
  28756. ' $r.addProperty(',
  28757. ' "E1",',
  28758. ' 0,',
  28759. ' $mod.$rtti["TEnum"],',
  28760. ' "FE",',
  28761. ' "",',
  28762. ' {',
  28763. ' Default: $mod.TEnum.red',
  28764. ' }',
  28765. ' );',
  28766. ' $r.addProperty(',
  28767. ' "E2",',
  28768. ' 0,',
  28769. ' $mod.$rtti["TEnum"],',
  28770. ' "FE",',
  28771. ' "",',
  28772. ' {',
  28773. ' Default: $mod.TEnum.blue',
  28774. ' }',
  28775. ' );',
  28776. '});',
  28777. '']),
  28778. LinesToStr([ // $mod.$main
  28779. '']));
  28780. end;
  28781. procedure TTestModule.TestRTTI_DefaultValueSet;
  28782. begin
  28783. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28784. StartProgram(false);
  28785. Add([
  28786. 'type',
  28787. ' TEnum = (red, blue);',
  28788. ' TSet = set of TEnum;',
  28789. 'const',
  28790. ' CSet = [red,blue];',
  28791. 'type',
  28792. ' TObject = class',
  28793. ' FSet: TSet;',
  28794. ' published',
  28795. ' property Set1: TSet read FSet default [];',
  28796. ' property Set2: TSet read FSet default [red];',
  28797. ' property Set3: TSet read FSet default [red,blue];',
  28798. ' property Set4: TSet read FSet default CSet;',
  28799. ' end;',
  28800. 'begin']);
  28801. ConvertProgram;
  28802. CheckSource('TestRTTI_DefaultValueSet',
  28803. LinesToStr([ // statements
  28804. 'this.TEnum = {',
  28805. ' "0": "red",',
  28806. ' red: 0,',
  28807. ' "1": "blue",',
  28808. ' blue: 1',
  28809. '};',
  28810. '$mod.$rtti.$Enum("TEnum", {',
  28811. ' minvalue: 0,',
  28812. ' maxvalue: 1,',
  28813. ' ordtype: 1,',
  28814. ' enumtype: this.TEnum',
  28815. '});',
  28816. '$mod.$rtti.$Set("TSet", {',
  28817. ' comptype: $mod.$rtti["TEnum"]',
  28818. '});',
  28819. 'this.CSet = rtl.createSet($mod.TEnum.red, $mod.TEnum.blue);',
  28820. 'rtl.createClass($mod, "TObject", null, function () {',
  28821. ' this.$init = function () {',
  28822. ' this.FSet = {};',
  28823. ' };',
  28824. ' this.$final = function () {',
  28825. ' this.FSet = undefined;',
  28826. ' };',
  28827. ' var $r = this.$rtti;',
  28828. ' $r.addProperty(',
  28829. ' "Set1",',
  28830. ' 0,',
  28831. ' $mod.$rtti["TSet"],',
  28832. ' "FSet",',
  28833. ' "",',
  28834. ' {',
  28835. ' Default: {}',
  28836. ' }',
  28837. ' );',
  28838. ' $r.addProperty(',
  28839. ' "Set2",',
  28840. ' 0,',
  28841. ' $mod.$rtti["TSet"],',
  28842. ' "FSet",',
  28843. ' "",',
  28844. ' {',
  28845. ' Default: rtl.createSet($mod.TEnum.red)',
  28846. ' }',
  28847. ' );',
  28848. ' $r.addProperty(',
  28849. ' "Set3",',
  28850. ' 0,',
  28851. ' $mod.$rtti["TSet"],',
  28852. ' "FSet",',
  28853. ' "",',
  28854. ' {',
  28855. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  28856. ' }',
  28857. ' );',
  28858. ' $r.addProperty(',
  28859. ' "Set4",',
  28860. ' 0,',
  28861. ' $mod.$rtti["TSet"],',
  28862. ' "FSet",',
  28863. ' "",',
  28864. ' {',
  28865. ' Default: $mod.CSet',
  28866. ' }',
  28867. ' );',
  28868. '});',
  28869. '']),
  28870. LinesToStr([ // $mod.$main
  28871. '']));
  28872. end;
  28873. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  28874. begin
  28875. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28876. StartProgram(false);
  28877. Add([
  28878. 'type',
  28879. ' TRg = -1..1;',
  28880. 'const',
  28881. ' l = low(TRg);',
  28882. ' h = high(TRg);',
  28883. 'type',
  28884. ' TObject = class',
  28885. ' FV: TRg;',
  28886. ' published',
  28887. ' property V1: TRg read FV default -1;',
  28888. ' end;',
  28889. 'begin']);
  28890. ConvertProgram;
  28891. CheckSource('TestRTTI_DefaultValueRangeType',
  28892. LinesToStr([ // statements
  28893. '$mod.$rtti.$Int("TRg", {',
  28894. ' minvalue: -1,',
  28895. ' maxvalue: 1,',
  28896. ' ordtype: 0',
  28897. '});',
  28898. 'this.l = -1;',
  28899. 'this.h = 1;',
  28900. 'rtl.createClass($mod, "TObject", null, function () {',
  28901. ' this.$init = function () {',
  28902. ' this.FV = 0;',
  28903. ' };',
  28904. ' this.$final = function () {',
  28905. ' };',
  28906. ' var $r = this.$rtti;',
  28907. ' $r.addProperty(',
  28908. ' "V1",',
  28909. ' 0,',
  28910. ' $mod.$rtti["TRg"],',
  28911. ' "FV",',
  28912. ' "",',
  28913. ' {',
  28914. ' Default: -1',
  28915. ' }',
  28916. ' );',
  28917. '});',
  28918. '']),
  28919. LinesToStr([ // $mod.$main
  28920. '']));
  28921. end;
  28922. procedure TTestModule.TestRTTI_DefaultValueInherit;
  28923. begin
  28924. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28925. StartProgram(false);
  28926. Add([
  28927. 'type',
  28928. ' TObject = class',
  28929. ' FA, FB: byte;',
  28930. ' property A: byte read FA default 1;',
  28931. ' property B: byte read FB default 2;',
  28932. ' end;',
  28933. ' TBird = class',
  28934. ' published',
  28935. ' property A;',
  28936. ' property B nodefault;',
  28937. ' end;',
  28938. 'begin']);
  28939. ConvertProgram;
  28940. CheckSource('TestRTTI_DefaultValueInherit',
  28941. LinesToStr([ // statements
  28942. 'rtl.createClass($mod, "TObject", null, function () {',
  28943. ' this.$init = function () {',
  28944. ' this.FA = 0;',
  28945. ' this.FB = 0;',
  28946. ' };',
  28947. ' this.$final = function () {',
  28948. ' };',
  28949. '});',
  28950. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  28951. ' var $r = this.$rtti;',
  28952. ' $r.addProperty(',
  28953. ' "A",',
  28954. ' 0,',
  28955. ' rtl.byte,',
  28956. ' "FA",',
  28957. ' "",',
  28958. ' {',
  28959. ' Default: 1',
  28960. ' }',
  28961. ' );',
  28962. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  28963. '});',
  28964. '']),
  28965. LinesToStr([ // $mod.$main
  28966. '']));
  28967. end;
  28968. procedure TTestModule.TestRTTI_OverrideMethod;
  28969. begin
  28970. Converter.Options:=Converter.Options-[coNoTypeInfo];
  28971. StartProgram(false);
  28972. Add('type');
  28973. Add(' TObject = class');
  28974. Add(' published');
  28975. Add(' procedure DoIt; virtual; abstract;');
  28976. Add(' end;');
  28977. Add(' TSky = class');
  28978. Add(' published');
  28979. Add(' procedure DoIt; override;');
  28980. Add(' end;');
  28981. Add('procedure TSky.DoIt; begin end;');
  28982. Add('begin');
  28983. ConvertProgram;
  28984. CheckSource('TestRTTI_OverrideMethod',
  28985. LinesToStr([ // statements
  28986. 'rtl.createClass($mod, "TObject", null, function () {',
  28987. ' this.$init = function () {',
  28988. ' };',
  28989. ' this.$final = function () {',
  28990. ' };',
  28991. ' var $r = this.$rtti;',
  28992. ' $r.addMethod("DoIt", 0, null);',
  28993. '});',
  28994. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  28995. ' this.DoIt = function () {',
  28996. ' };',
  28997. '});',
  28998. '']),
  28999. LinesToStr([ // $mod.$main
  29000. '']));
  29001. end;
  29002. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29003. begin
  29004. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29005. StartProgram(false);
  29006. Add([
  29007. 'type',
  29008. ' TObject = class',
  29009. ' published',
  29010. ' procedure DoIt;',
  29011. ' end;',
  29012. ' TSky = class',
  29013. ' published',
  29014. ' procedure DoIt; reintroduce;',
  29015. ' end;',
  29016. 'procedure TObject.DoIt; begin end;',
  29017. 'procedure TSky.DoIt;',
  29018. 'begin',
  29019. ' inherited DoIt;',
  29020. 'end;',
  29021. 'begin']);
  29022. ConvertProgram;
  29023. CheckSource('TestRTTI_ReintroduceMethod',
  29024. LinesToStr([ // statements
  29025. 'rtl.createClass($mod, "TObject", null, function () {',
  29026. ' this.$init = function () {',
  29027. ' };',
  29028. ' this.$final = function () {',
  29029. ' };',
  29030. ' this.DoIt = function () {',
  29031. ' };',
  29032. ' var $r = this.$rtti;',
  29033. ' $r.addMethod("DoIt", 0, null);',
  29034. '});',
  29035. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  29036. ' this.DoIt = function () {',
  29037. ' $mod.TObject.DoIt.call(this);',
  29038. ' };',
  29039. ' var $r = this.$rtti;',
  29040. ' $r.addMethod("DoIt", 0, null);',
  29041. '});',
  29042. '']),
  29043. LinesToStr([ // $mod.$main
  29044. '']));
  29045. end;
  29046. procedure TTestModule.TestRTTI_OverloadProperty;
  29047. begin
  29048. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29049. StartProgram(false);
  29050. Add('type');
  29051. Add(' TObject = class');
  29052. Add(' protected');
  29053. Add(' FFlag: longint;');
  29054. Add(' published');
  29055. Add(' property Flag: longint read fflag;');
  29056. Add(' end;');
  29057. Add(' TSky = class');
  29058. Add(' published');
  29059. Add(' property FLAG: longint write fflag;');
  29060. Add(' end;');
  29061. Add('begin');
  29062. ConvertProgram;
  29063. CheckSource('TestRTTI_OverrideMethod',
  29064. LinesToStr([ // statements
  29065. 'rtl.createClass($mod, "TObject", null, function () {',
  29066. ' this.$init = function () {',
  29067. ' this.FFlag = 0;',
  29068. ' };',
  29069. ' this.$final = function () {',
  29070. ' };',
  29071. ' var $r = this.$rtti;',
  29072. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29073. '});',
  29074. 'rtl.createClass($mod, "TSky", $mod.TObject, function () {',
  29075. ' var $r = this.$rtti;',
  29076. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29077. '});',
  29078. '']),
  29079. LinesToStr([ // $mod.$main
  29080. '']));
  29081. end;
  29082. procedure TTestModule.TestRTTI_ClassForward;
  29083. begin
  29084. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29085. StartProgram(false);
  29086. Add('type');
  29087. Add(' TObject = class end;');
  29088. Add(' tbridge = class;');
  29089. Add(' TProc = function: tbridge;');
  29090. Add(' TOger = class');
  29091. Add(' published');
  29092. Add(' FBridge: tbridge;');
  29093. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29094. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29095. Add(' end;');
  29096. Add(' TBridge = class');
  29097. Add(' FOger: toger;');
  29098. Add(' end;');
  29099. Add('var p: Pointer;');
  29100. Add(' b: tbridge;');
  29101. Add('begin');
  29102. Add(' p:=typeinfo(tbridge);');
  29103. Add(' p:=typeinfo(b);');
  29104. ConvertProgram;
  29105. CheckSource('TestRTTI_ClassForward',
  29106. LinesToStr([ // statements
  29107. 'rtl.createClass($mod, "TObject", null, function () {',
  29108. ' this.$init = function () {',
  29109. ' };',
  29110. ' this.$final = function () {',
  29111. ' };',
  29112. '});',
  29113. '$mod.$rtti.$Class("TBridge");',
  29114. '$mod.$rtti.$ProcVar("TProc", {',
  29115. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TBridge"])',
  29116. '});',
  29117. 'rtl.createClass($mod, "TOger", $mod.TObject, function () {',
  29118. ' this.$init = function () {',
  29119. ' $mod.TObject.$init.call(this);',
  29120. ' this.FBridge = null;',
  29121. ' };',
  29122. ' this.$final = function () {',
  29123. ' this.FBridge = undefined;',
  29124. ' $mod.TObject.$final.call(this);',
  29125. ' };',
  29126. ' var $r = this.$rtti;',
  29127. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29128. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29129. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29130. '});',
  29131. 'rtl.createClass($mod, "TBridge", $mod.TObject, function () {',
  29132. ' this.$init = function () {',
  29133. ' $mod.TObject.$init.call(this);',
  29134. ' this.FOger = null;',
  29135. ' };',
  29136. ' this.$final = function () {',
  29137. ' this.FOger = undefined;',
  29138. ' $mod.TObject.$final.call(this);',
  29139. ' };',
  29140. '});',
  29141. 'this.p = null;',
  29142. 'this.b = null;',
  29143. '']),
  29144. LinesToStr([ // $mod.$main
  29145. '$mod.p = $mod.$rtti["TBridge"];',
  29146. '$mod.p = $mod.b.$rtti;',
  29147. '']));
  29148. end;
  29149. procedure TTestModule.TestRTTI_ClassOf;
  29150. begin
  29151. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29152. StartProgram(false);
  29153. Add('type');
  29154. Add(' TClass = class of tobject;');
  29155. Add(' TProcA = function: TClass;');
  29156. Add(' TObject = class');
  29157. Add(' published');
  29158. Add(' C: tclass;');
  29159. Add(' end;');
  29160. Add(' tfox = class;');
  29161. Add(' TBird = class end;');
  29162. Add(' TBirds = class of tbird;');
  29163. Add(' TFox = class end;');
  29164. Add(' TFoxes = class of tfox;');
  29165. Add(' TCows = class of TCow;');
  29166. Add(' TCow = class;');
  29167. Add(' TCow = class end;');
  29168. Add('begin');
  29169. ConvertProgram;
  29170. CheckSource('TestRTTI_ClassOf',
  29171. LinesToStr([ // statements
  29172. '$mod.$rtti.$Class("TObject");',
  29173. '$mod.$rtti.$ClassRef("TClass", {',
  29174. ' instancetype: $mod.$rtti["TObject"]',
  29175. '});',
  29176. '$mod.$rtti.$ProcVar("TProcA", {',
  29177. ' procsig: rtl.newTIProcSig(null, $mod.$rtti["TClass"])',
  29178. '});',
  29179. 'rtl.createClass($mod, "TObject", null, function () {',
  29180. ' this.$init = function () {',
  29181. ' this.C = null;',
  29182. ' };',
  29183. ' this.$final = function () {',
  29184. ' this.C = undefined;',
  29185. ' };',
  29186. ' var $r = this.$rtti;',
  29187. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29188. '});',
  29189. '$mod.$rtti.$Class("TFox");',
  29190. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  29191. '});',
  29192. '$mod.$rtti.$ClassRef("TBirds", {',
  29193. ' instancetype: $mod.$rtti["TBird"]',
  29194. '});',
  29195. 'rtl.createClass($mod, "TFox", $mod.TObject, function () {',
  29196. '});',
  29197. '$mod.$rtti.$ClassRef("TFoxes", {',
  29198. ' instancetype: $mod.$rtti["TFox"]',
  29199. '});',
  29200. '$mod.$rtti.$Class("TCow");',
  29201. '$mod.$rtti.$ClassRef("TCows", {',
  29202. ' instancetype: $mod.$rtti["TCow"]',
  29203. '});',
  29204. 'rtl.createClass($mod, "TCow", $mod.TObject, function () {',
  29205. '});',
  29206. '']),
  29207. LinesToStr([ // $mod.$main
  29208. '']));
  29209. end;
  29210. procedure TTestModule.TestRTTI_Record;
  29211. begin
  29212. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29213. StartProgram(false);
  29214. Add('type');
  29215. Add(' integer = longint;');
  29216. Add(' TPoint = record');
  29217. Add(' x,y: integer;');
  29218. Add(' end;');
  29219. Add('var p: pointer;');
  29220. Add(' r: tpoint;');
  29221. Add('begin');
  29222. Add(' p:=typeinfo(tpoint);');
  29223. Add(' p:=typeinfo(r);');
  29224. Add(' p:=typeinfo(r.x);');
  29225. ConvertProgram;
  29226. CheckSource('TestRTTI_Record',
  29227. LinesToStr([ // statements
  29228. 'rtl.recNewT($mod, "TPoint", function () {',
  29229. ' this.x = 0;',
  29230. ' this.y = 0;',
  29231. ' this.$eq = function (b) {',
  29232. ' return (this.x === b.x) && (this.y === b.y);',
  29233. ' };',
  29234. ' this.$assign = function (s) {',
  29235. ' this.x = s.x;',
  29236. ' this.y = s.y;',
  29237. ' return this;',
  29238. ' };',
  29239. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29240. ' $r.addField("x", rtl.longint);',
  29241. ' $r.addField("y", rtl.longint);',
  29242. '});',
  29243. 'this.p = null;',
  29244. 'this.r = $mod.TPoint.$new();',
  29245. '']),
  29246. LinesToStr([ // $mod.$main
  29247. '$mod.p = $mod.$rtti["TPoint"];',
  29248. '$mod.p = $mod.$rtti["TPoint"];',
  29249. '$mod.p = rtl.longint;',
  29250. '']));
  29251. end;
  29252. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29253. begin
  29254. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29255. StartProgram(false);
  29256. Add('type');
  29257. Add(' TFloatRec = record');
  29258. Add(' c,d: array of char;');
  29259. // Add(' i: array of array of longint;');
  29260. Add(' end;');
  29261. Add('var p: pointer;');
  29262. Add(' r: tfloatrec;');
  29263. Add('begin');
  29264. Add(' p:=typeinfo(tfloatrec);');
  29265. Add(' p:=typeinfo(r);');
  29266. Add(' p:=typeinfo(r.d);');
  29267. ConvertProgram;
  29268. CheckSource('TestRTTI_Record',
  29269. LinesToStr([ // statements
  29270. 'rtl.recNewT($mod, "TFloatRec", function () {',
  29271. ' this.$new = function () {',
  29272. ' var r = Object.create(this);',
  29273. ' r.c = [];',
  29274. ' r.d = [];',
  29275. ' return r;',
  29276. ' };',
  29277. ' this.$eq = function (b) {',
  29278. ' return (this.c === b.c) && (this.d === b.d);',
  29279. ' };',
  29280. ' this.$assign = function (s) {',
  29281. ' this.c = rtl.arrayRef(s.c);',
  29282. ' this.d = rtl.arrayRef(s.d);',
  29283. ' return this;',
  29284. ' };',
  29285. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29286. ' eltype: rtl.char',
  29287. ' });',
  29288. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29289. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29290. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29291. '});',
  29292. 'this.p = null;',
  29293. 'this.r = $mod.TFloatRec.$new();',
  29294. '']),
  29295. LinesToStr([ // $mod.$main
  29296. '$mod.p = $mod.$rtti["TFloatRec"];',
  29297. '$mod.p = $mod.$rtti["TFloatRec"];',
  29298. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29299. '']));
  29300. end;
  29301. procedure TTestModule.TestRTTI_LocalTypes;
  29302. begin
  29303. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29304. StartProgram(false);
  29305. Add([
  29306. 'procedure DoIt;',
  29307. 'type',
  29308. ' integer = longint;',
  29309. ' TPoint = record',
  29310. ' x,y: integer;',
  29311. ' end;',
  29312. 'var p: TPoint;',
  29313. 'begin',
  29314. 'end;',
  29315. 'begin']);
  29316. ConvertProgram;
  29317. CheckSource('TestRTTI_LocalTypes',
  29318. LinesToStr([ // statements
  29319. 'var TPoint = rtl.recNewT(null, "", function () {',
  29320. ' this.x = 0;',
  29321. ' this.y = 0;',
  29322. ' this.$eq = function (b) {',
  29323. ' return (this.x === b.x) && (this.y === b.y);',
  29324. ' };',
  29325. ' this.$assign = function (s) {',
  29326. ' this.x = s.x;',
  29327. ' this.y = s.y;',
  29328. ' return this;',
  29329. ' };',
  29330. '});',
  29331. 'this.DoIt = function () {',
  29332. ' var p = TPoint.$new();',
  29333. '};',
  29334. '']),
  29335. LinesToStr([ // $mod.$main
  29336. '']));
  29337. end;
  29338. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29339. begin
  29340. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29341. StartProgram(false);
  29342. Add([
  29343. 'type',
  29344. ' TCaption = string;',
  29345. ' TYesNo = boolean;',
  29346. ' TLetter = char;',
  29347. ' TFloat = double;',
  29348. ' TPtr = pointer;',
  29349. ' TShortInt = shortint;',
  29350. ' TByte = byte;',
  29351. ' TSmallInt = smallint;',
  29352. ' TWord = word;',
  29353. ' TInt32 = longint;',
  29354. ' TDWord = longword;',
  29355. ' TValue = jsvalue;',
  29356. 'var p: TPtr;',
  29357. 'begin',
  29358. ' p:=typeinfo(string);',
  29359. ' p:=typeinfo(tcaption);',
  29360. ' p:=typeinfo(boolean);',
  29361. ' p:=typeinfo(tyesno);',
  29362. ' p:=typeinfo(char);',
  29363. ' p:=typeinfo(tletter);',
  29364. ' p:=typeinfo(double);',
  29365. ' p:=typeinfo(tfloat);',
  29366. ' p:=typeinfo(pointer);',
  29367. ' p:=typeinfo(tptr);',
  29368. ' p:=typeinfo(shortint);',
  29369. ' p:=typeinfo(tshortint);',
  29370. ' p:=typeinfo(byte);',
  29371. ' p:=typeinfo(tbyte);',
  29372. ' p:=typeinfo(smallint);',
  29373. ' p:=typeinfo(tsmallint);',
  29374. ' p:=typeinfo(word);',
  29375. ' p:=typeinfo(tword);',
  29376. ' p:=typeinfo(longword);',
  29377. ' p:=typeinfo(tdword);',
  29378. ' p:=typeinfo(jsvalue);',
  29379. ' p:=typeinfo(tvalue);',
  29380. '']);
  29381. ConvertProgram;
  29382. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  29383. LinesToStr([ // statements
  29384. 'this.p = null;',
  29385. '']),
  29386. LinesToStr([ // $mod.$main
  29387. '$mod.p = rtl.string;',
  29388. '$mod.p = rtl.string;',
  29389. '$mod.p = rtl.boolean;',
  29390. '$mod.p = rtl.boolean;',
  29391. '$mod.p = rtl.char;',
  29392. '$mod.p = rtl.char;',
  29393. '$mod.p = rtl.double;',
  29394. '$mod.p = rtl.double;',
  29395. '$mod.p = rtl.pointer;',
  29396. '$mod.p = rtl.pointer;',
  29397. '$mod.p = rtl.shortint;',
  29398. '$mod.p = rtl.shortint;',
  29399. '$mod.p = rtl.byte;',
  29400. '$mod.p = rtl.byte;',
  29401. '$mod.p = rtl.smallint;',
  29402. '$mod.p = rtl.smallint;',
  29403. '$mod.p = rtl.word;',
  29404. '$mod.p = rtl.word;',
  29405. '$mod.p = rtl.longword;',
  29406. '$mod.p = rtl.longword;',
  29407. '$mod.p = rtl.jsvalue;',
  29408. '$mod.p = rtl.jsvalue;',
  29409. '']));
  29410. end;
  29411. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  29412. begin
  29413. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29414. StartProgram(false);
  29415. Add([
  29416. 'type',
  29417. ' TCaption = type string;',
  29418. ' TYesNo = type boolean;',
  29419. ' TLetter = type char;',
  29420. ' TFloat = type double;',
  29421. ' TPtr = type pointer;',
  29422. ' TShortInt = type shortint;',
  29423. ' TByte = type byte;',
  29424. ' TSmallInt = type smallint;',
  29425. ' TWord = type word;',
  29426. ' TInt32 = type longint;',
  29427. ' TDWord = type longword;',
  29428. ' TValue = type jsvalue;',
  29429. ' TAliasValue = type TValue;',
  29430. 'var',
  29431. ' p: TPtr;',
  29432. ' a: TAliasValue;',
  29433. 'begin',
  29434. ' p:=typeinfo(tcaption);',
  29435. ' p:=typeinfo(tyesno);',
  29436. ' p:=typeinfo(tletter);',
  29437. ' p:=typeinfo(tfloat);',
  29438. ' p:=typeinfo(tptr);',
  29439. ' p:=typeinfo(tshortint);',
  29440. ' p:=typeinfo(tbyte);',
  29441. ' p:=typeinfo(tsmallint);',
  29442. ' p:=typeinfo(tword);',
  29443. ' p:=typeinfo(tdword);',
  29444. ' p:=typeinfo(tvalue);',
  29445. ' p:=typeinfo(taliasvalue);',
  29446. ' p:=typeinfo(a);',
  29447. '']);
  29448. ConvertProgram;
  29449. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  29450. LinesToStr([ // statements
  29451. '$mod.$rtti.$inherited("TCaption", rtl.string, {});',
  29452. '$mod.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  29453. '$mod.$rtti.$inherited("TLetter", rtl.char, {});',
  29454. '$mod.$rtti.$inherited("TFloat", rtl.double, {});',
  29455. '$mod.$rtti.$inherited("TPtr", rtl.pointer, {});',
  29456. '$mod.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  29457. '$mod.$rtti.$inherited("TByte", rtl.byte, {});',
  29458. '$mod.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  29459. '$mod.$rtti.$inherited("TWord", rtl.word, {});',
  29460. '$mod.$rtti.$inherited("TInt32", rtl.longint, {});',
  29461. '$mod.$rtti.$inherited("TDWord", rtl.longword, {});',
  29462. '$mod.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  29463. '$mod.$rtti.$inherited("TAliasValue", $mod.$rtti["TValue"], {});',
  29464. 'this.p = null;',
  29465. 'this.a = undefined;',
  29466. '']),
  29467. LinesToStr([ // $mod.$main
  29468. '$mod.p = $mod.$rtti["TCaption"];',
  29469. '$mod.p = $mod.$rtti["TYesNo"];',
  29470. '$mod.p = $mod.$rtti["TLetter"];',
  29471. '$mod.p = $mod.$rtti["TFloat"];',
  29472. '$mod.p = $mod.$rtti["TPtr"];',
  29473. '$mod.p = $mod.$rtti["TShortInt"];',
  29474. '$mod.p = $mod.$rtti["TByte"];',
  29475. '$mod.p = $mod.$rtti["TSmallInt"];',
  29476. '$mod.p = $mod.$rtti["TWord"];',
  29477. '$mod.p = $mod.$rtti["TDWord"];',
  29478. '$mod.p = $mod.$rtti["TValue"];',
  29479. '$mod.p = $mod.$rtti["TAliasValue"];',
  29480. '$mod.p = $mod.$rtti["TAliasValue"];',
  29481. '']));
  29482. end;
  29483. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  29484. begin
  29485. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29486. StartProgram(false);
  29487. Add('procedure DoIt;');
  29488. Add('type');
  29489. Add(' integer = longint;');
  29490. Add(' TPoint = record');
  29491. Add(' x,y: integer;');
  29492. Add(' end;');
  29493. Add('var p: pointer;');
  29494. Add('begin');
  29495. Add(' p:=typeinfo(tpoint);');
  29496. Add('end;');
  29497. Add('begin');
  29498. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  29499. ConvertProgram;
  29500. end;
  29501. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  29502. begin
  29503. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29504. StartProgram(true,[supTypeInfo]);
  29505. Add([
  29506. '{$modeswitch externalclass}',
  29507. 'type',
  29508. ' TFlag = (up,down);',
  29509. ' TFlags = set of TFlag;',
  29510. 'var',
  29511. ' ti: TTypeInfo;',
  29512. ' tiInt: TTypeInfoInteger;',
  29513. ' tiEnum: TTypeInfoEnum;',
  29514. ' tiSet: TTypeInfoSet;',
  29515. 'begin',
  29516. ' ti:=typeinfo(string);',
  29517. ' ti:=typeinfo(boolean);',
  29518. ' ti:=typeinfo(char);',
  29519. ' ti:=typeinfo(double);',
  29520. ' tiInt:=typeinfo(shortint);',
  29521. ' tiInt:=typeinfo(byte);',
  29522. ' tiInt:=typeinfo(smallint);',
  29523. ' tiInt:=typeinfo(word);',
  29524. ' tiInt:=typeinfo(longint);',
  29525. ' tiInt:=typeinfo(longword);',
  29526. ' ti:=typeinfo(jsvalue);',
  29527. ' tiEnum:=typeinfo(tflag);',
  29528. ' tiSet:=typeinfo(tflags);']);
  29529. ConvertProgram;
  29530. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  29531. LinesToStr([ // statements
  29532. 'this.TFlag = {',
  29533. ' "0": "up",',
  29534. ' up: 0,',
  29535. ' "1": "down",',
  29536. ' down: 1',
  29537. '};',
  29538. '$mod.$rtti.$Enum("TFlag", {',
  29539. ' minvalue: 0,',
  29540. ' maxvalue: 1,',
  29541. ' ordtype: 1,',
  29542. ' enumtype: this.TFlag',
  29543. '});',
  29544. '$mod.$rtti.$Set("TFlags", {',
  29545. ' comptype: $mod.$rtti["TFlag"]',
  29546. '});',
  29547. 'this.ti = null;',
  29548. 'this.tiInt = null;',
  29549. 'this.tiEnum = null;',
  29550. 'this.tiSet = null;',
  29551. '']),
  29552. LinesToStr([ // $mod.$main
  29553. '$mod.ti = rtl.string;',
  29554. '$mod.ti = rtl.boolean;',
  29555. '$mod.ti = rtl.char;',
  29556. '$mod.ti = rtl.double;',
  29557. '$mod.tiInt = rtl.shortint;',
  29558. '$mod.tiInt = rtl.byte;',
  29559. '$mod.tiInt = rtl.smallint;',
  29560. '$mod.tiInt = rtl.word;',
  29561. '$mod.tiInt = rtl.longint;',
  29562. '$mod.tiInt = rtl.longword;',
  29563. '$mod.ti = rtl.jsvalue;',
  29564. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  29565. '$mod.tiSet = $mod.$rtti["TFlags"];',
  29566. '']));
  29567. end;
  29568. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  29569. begin
  29570. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29571. StartProgram(true,[supTypeInfo]);
  29572. Add('{$modeswitch externalclass}');
  29573. Add('type');
  29574. Add(' TStaticArr = array[boolean] of string;');
  29575. Add(' TDynArr = array of string;');
  29576. Add(' TProc = procedure;');
  29577. Add(' TMethod = procedure of object;');
  29578. Add('var');
  29579. Add(' StaticArray: TStaticArr;');
  29580. Add(' tiStaticArray: TTypeInfoStaticArray;');
  29581. Add(' DynArray: TDynArr;');
  29582. Add(' tiDynArray: TTypeInfoDynArray;');
  29583. Add(' ProcVar: TProc;');
  29584. Add(' tiProcVar: TTypeInfoProcVar;');
  29585. Add(' MethodVar: TMethod;');
  29586. Add(' tiMethodVar: TTypeInfoMethodVar;');
  29587. Add('begin');
  29588. Add(' tiStaticArray:=typeinfo(StaticArray);');
  29589. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  29590. Add(' tiDynArray:=typeinfo(DynArray);');
  29591. Add(' tiDynArray:=typeinfo(TDynArr);');
  29592. Add(' tiProcVar:=typeinfo(ProcVar);');
  29593. Add(' tiProcVar:=typeinfo(TProc);');
  29594. Add(' tiMethodVar:=typeinfo(MethodVar);');
  29595. Add(' tiMethodVar:=typeinfo(TMethod);');
  29596. ConvertProgram;
  29597. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  29598. LinesToStr([ // statements
  29599. ' $mod.$rtti.$StaticArray("TStaticArr", {',
  29600. ' dims: [2],',
  29601. ' eltype: rtl.string',
  29602. '});',
  29603. '$mod.$rtti.$DynArray("TDynArr", {',
  29604. ' eltype: rtl.string',
  29605. '});',
  29606. '$mod.$rtti.$ProcVar("TProc", {',
  29607. ' procsig: rtl.newTIProcSig(null)',
  29608. '});',
  29609. '$mod.$rtti.$MethodVar("TMethod", {',
  29610. ' procsig: rtl.newTIProcSig(null),',
  29611. ' methodkind: 0',
  29612. '});',
  29613. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  29614. 'this.tiStaticArray = null;',
  29615. 'this.DynArray = [];',
  29616. 'this.tiDynArray = null;',
  29617. 'this.ProcVar = null;',
  29618. 'this.tiProcVar = null;',
  29619. 'this.MethodVar = null;',
  29620. 'this.tiMethodVar = null;',
  29621. '']),
  29622. LinesToStr([ // $mod.$main
  29623. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29624. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  29625. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29626. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  29627. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29628. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  29629. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29630. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  29631. '']));
  29632. end;
  29633. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  29634. begin
  29635. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29636. StartProgram(true,[supTypeInfo]);
  29637. Add('{$modeswitch externalclass}');
  29638. Add('type');
  29639. Add(' TRec = record end;');
  29640. // ToDo: ^PRec
  29641. Add(' TObject = class end;');
  29642. Add(' TClass = class of tobject;');
  29643. Add('var');
  29644. Add(' Rec: trec;');
  29645. Add(' tiRecord: ttypeinforecord;');
  29646. Add(' Obj: tobject;');
  29647. Add(' tiClass: ttypeinfoclass;');
  29648. Add(' aClass: tclass;');
  29649. Add(' tiClassRef: ttypeinfoclassref;');
  29650. // ToDo: ^PRec
  29651. Add(' tiPointer: ttypeinfopointer;');
  29652. Add('begin');
  29653. Add(' tirecord:=typeinfo(trec);');
  29654. Add(' tirecord:=typeinfo(trec);');
  29655. Add(' ticlass:=typeinfo(obj);');
  29656. Add(' ticlass:=typeinfo(tobject);');
  29657. Add(' ticlass:=typeinfo(aclass);');
  29658. Add(' ticlassref:=typeinfo(tclass);');
  29659. ConvertProgram;
  29660. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  29661. LinesToStr([ // statements
  29662. 'rtl.recNewT($mod, "TRec", function () {',
  29663. ' this.$eq = function (b) {',
  29664. ' return true;',
  29665. ' };',
  29666. ' this.$assign = function (s) {',
  29667. ' return this;',
  29668. ' };',
  29669. ' $mod.$rtti.$Record("TRec", {});',
  29670. '});',
  29671. 'rtl.createClass($mod, "TObject", null, function () {',
  29672. ' this.$init = function () {',
  29673. ' };',
  29674. ' this.$final = function () {',
  29675. ' };',
  29676. '});',
  29677. '$mod.$rtti.$ClassRef("TClass", {',
  29678. ' instancetype: $mod.$rtti["TObject"]',
  29679. '});',
  29680. 'this.Rec = $mod.TRec.$new();',
  29681. 'this.tiRecord = null;',
  29682. 'this.Obj = null;',
  29683. 'this.tiClass = null;',
  29684. 'this.aClass = null;',
  29685. 'this.tiClassRef = null;',
  29686. 'this.tiPointer = null;',
  29687. '']),
  29688. LinesToStr([ // $mod.$main
  29689. '$mod.tiRecord = $mod.$rtti["TRec"];',
  29690. '$mod.tiRecord = $mod.$rtti["TRec"];',
  29691. '$mod.tiClass = $mod.Obj.$rtti;',
  29692. '$mod.tiClass = $mod.$rtti["TObject"];',
  29693. '$mod.tiClass = $mod.aClass.$rtti;',
  29694. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  29695. '']));
  29696. end;
  29697. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  29698. begin
  29699. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29700. StartProgram(true,[supTypeInfo]);
  29701. Add([
  29702. '{$modeswitch externalclass}',
  29703. 'type',
  29704. ' TClass = class of tobject;',
  29705. ' TObject = class',
  29706. ' function MyClass: TClass;',
  29707. ' class function ClassType: TClass;',
  29708. ' end;',
  29709. 'function TObject.MyClass: TClass;',
  29710. 'var t: TTypeInfoClass;',
  29711. 'begin',
  29712. ' t:=TypeInfo(Self);',
  29713. ' t:=TypeInfo(Result);',
  29714. ' t:=TypeInfo(TObject);',
  29715. 'end;',
  29716. 'class function TObject.ClassType: TClass;',
  29717. 'var t: TTypeInfoClass;',
  29718. 'begin',
  29719. ' t:=TypeInfo(Self);',
  29720. ' t:=TypeInfo(Result);',
  29721. 'end;',
  29722. 'var',
  29723. ' Obj: TObject;',
  29724. ' t: TTypeInfoClass;',
  29725. 'begin',
  29726. ' t:=TypeInfo(TObject.ClassType);',
  29727. ' t:=TypeInfo(Obj.ClassType);',
  29728. ' t:=TypeInfo(Obj.MyClass);',
  29729. '']);
  29730. ConvertProgram;
  29731. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  29732. LinesToStr([ // statements
  29733. '$mod.$rtti.$Class("TObject");',
  29734. '$mod.$rtti.$ClassRef("TClass", {',
  29735. ' instancetype: $mod.$rtti["TObject"]',
  29736. '});',
  29737. 'rtl.createClass($mod, "TObject", null, function () {',
  29738. ' this.$init = function () {',
  29739. ' };',
  29740. ' this.$final = function () {',
  29741. ' };',
  29742. ' this.MyClass = function () {',
  29743. ' var Result = null;',
  29744. ' var t = null;',
  29745. ' t = this.$rtti;',
  29746. ' t = Result.$rtti;',
  29747. ' t = $mod.$rtti["TObject"];',
  29748. ' return Result;',
  29749. ' };',
  29750. ' this.ClassType = function () {',
  29751. ' var Result = null;',
  29752. ' var t = null;',
  29753. ' t = this.$rtti;',
  29754. ' t = Result.$rtti;',
  29755. ' return Result;',
  29756. ' };',
  29757. '});',
  29758. 'this.Obj = null;',
  29759. 'this.t = null;',
  29760. '']),
  29761. LinesToStr([ // $mod.$main
  29762. '$mod.t = $mod.TObject.ClassType().$rtti;',
  29763. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  29764. '$mod.t = $mod.Obj.MyClass().$rtti;',
  29765. '']));
  29766. end;
  29767. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  29768. begin
  29769. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29770. AddModuleWithIntfImplSrc('typinfo.pas',
  29771. LinesToStr([
  29772. '{$modeswitch externalclass}',
  29773. 'type',
  29774. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  29775. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  29776. '']),
  29777. '');
  29778. AddModuleWithIntfImplSrc('unit2.pas',
  29779. LinesToStr([
  29780. 'uses typinfo;',
  29781. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  29782. 'procedure DoPtr(p: PTypeInfo);',
  29783. 'procedure DoInfo(t: TTypeInfo);',
  29784. 'procedure DoInt(t: TTypeInfoInteger);',
  29785. '']),
  29786. LinesToStr([
  29787. 'procedure DoPtr(p: PTypeInfo);',
  29788. 'begin end;',
  29789. 'procedure DoInfo(t: TTypeInfo);',
  29790. 'begin end;',
  29791. 'procedure DoInt(t: TTypeInfoInteger);',
  29792. 'begin end;',
  29793. '']));
  29794. StartUnit(true);
  29795. Add([
  29796. 'interface',
  29797. 'uses unit2;', // does not use unit typinfo
  29798. 'implementation',
  29799. 'var',
  29800. ' i: byte;',
  29801. ' p: pointer;',
  29802. ' t: PTypeInfo;',
  29803. 'initialization',
  29804. ' p:=typeinfo(i);',
  29805. ' t:=typeinfo(i);',
  29806. ' if p=t then ;',
  29807. ' if p=typeinfo(i) then ;',
  29808. ' if typeinfo(i)=p then ;',
  29809. ' if t=typeinfo(i) then ;',
  29810. ' if typeinfo(i)=t then ;',
  29811. ' DoPtr(p);',
  29812. ' DoPtr(t);',
  29813. ' DoPtr(typeinfo(i));',
  29814. ' DoInfo(p);',
  29815. ' DoInfo(t);',
  29816. ' DoInfo(typeinfo(i));',
  29817. ' DoInt(typeinfo(i));',
  29818. '']);
  29819. ConvertUnit;
  29820. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  29821. LinesToStr([ // statements
  29822. 'var $impl = $mod.$impl;',
  29823. '']),
  29824. LinesToStr([ // this.$init
  29825. '$impl.p = rtl.byte;',
  29826. '$impl.t = rtl.byte;',
  29827. 'if ($impl.p === $impl.t) ;',
  29828. 'if ($impl.p === rtl.byte) ;',
  29829. 'if (rtl.byte === $impl.p) ;',
  29830. 'if ($impl.t === rtl.byte) ;',
  29831. 'if (rtl.byte === $impl.t) ;',
  29832. 'pas.unit2.DoPtr($impl.p);',
  29833. 'pas.unit2.DoPtr($impl.t);',
  29834. 'pas.unit2.DoPtr(rtl.byte);',
  29835. 'pas.unit2.DoInfo($impl.p);',
  29836. 'pas.unit2.DoInfo($impl.t);',
  29837. 'pas.unit2.DoInfo(rtl.byte);',
  29838. 'pas.unit2.DoInt(rtl.byte);',
  29839. '']),
  29840. LinesToStr([ // implementation
  29841. '$impl.i = 0;',
  29842. '$impl.p = null;',
  29843. '$impl.t = null;',
  29844. '']) );
  29845. end;
  29846. procedure TTestModule.TestRTTI_Interface_Corba;
  29847. begin
  29848. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29849. StartProgram(true,[supTypeInfo]);
  29850. Add([
  29851. '{$interfaces corba}',
  29852. '{$modeswitch externalclass}',
  29853. 'type',
  29854. ' IUnknown = interface',
  29855. ' end;',
  29856. ' IBird = interface',
  29857. ' function GetItem: longint;',
  29858. ' procedure SetItem(Value: longint);',
  29859. ' property Item: longint read GetItem write SetItem;',
  29860. ' end;',
  29861. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  29862. 'var',
  29863. ' i: IBird;',
  29864. ' t: TTypeInfoInterface;',
  29865. 'begin',
  29866. ' t:=TypeInfo(IBird);',
  29867. ' t:=TypeInfo(i);',
  29868. ' DoIt(t);',
  29869. ' DoIt(TypeInfo(IBird));',
  29870. '']);
  29871. ConvertProgram;
  29872. CheckSource('TestRTTI_Interface_Corba',
  29873. LinesToStr([ // statements
  29874. 'rtl.createInterface(',
  29875. ' $mod,',
  29876. ' "IUnknown",',
  29877. ' "{B92D5841-758A-322B-B800-000000000000}",',
  29878. ' [],',
  29879. ' null,',
  29880. ' function () {',
  29881. ' }',
  29882. ');',
  29883. 'rtl.createInterface(',
  29884. ' $mod,',
  29885. ' "IBird",',
  29886. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  29887. ' ["GetItem", "SetItem"],',
  29888. ' null,',
  29889. ' function () {',
  29890. ' var $r = this.$rtti;',
  29891. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29892. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  29893. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  29894. ' }',
  29895. ');',
  29896. 'this.DoIt = function (t) {',
  29897. '}; ',
  29898. 'this.i = null;',
  29899. 'this.t = null;',
  29900. '']),
  29901. LinesToStr([ // $mod.$main
  29902. '$mod.t = $mod.$rtti["IBird"];',
  29903. '$mod.t = $mod.i.$rtti;',
  29904. '$mod.DoIt($mod.t);',
  29905. '$mod.DoIt($mod.$rtti["IBird"]);',
  29906. '']));
  29907. end;
  29908. procedure TTestModule.TestRTTI_Interface_COM;
  29909. begin
  29910. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29911. StartProgram(true,[supTypeInfo]);
  29912. Add([
  29913. '{$interfaces com}',
  29914. '{$modeswitch externalclass}',
  29915. 'type',
  29916. ' TGuid = record end;',
  29917. ' integer = longint;',
  29918. ' IUnknown = interface',
  29919. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  29920. ' function _AddRef: Integer;',
  29921. ' function _Release: Integer;',
  29922. ' end;',
  29923. ' IBird = interface',
  29924. ' function GetItem: longint;',
  29925. ' procedure SetItem(Value: longint);',
  29926. ' property Item: longint read GetItem write SetItem;',
  29927. ' end;',
  29928. 'var',
  29929. ' i: IBird;',
  29930. ' t: TTypeInfoInterface;',
  29931. 'begin',
  29932. ' t:=TypeInfo(IBird);',
  29933. ' t:=TypeInfo(i);',
  29934. '']);
  29935. ConvertProgram;
  29936. CheckSource('TestRTTI_Interface_COM',
  29937. LinesToStr([ // statements
  29938. 'rtl.recNewT($mod, "TGuid", function () {',
  29939. ' this.$eq = function (b) {',
  29940. ' return true;',
  29941. ' };',
  29942. ' this.$assign = function (s) {',
  29943. ' return this;',
  29944. ' };',
  29945. ' $mod.$rtti.$Record("TGuid", {});',
  29946. '});',
  29947. 'rtl.createInterface(',
  29948. ' $mod,',
  29949. ' "IUnknown",',
  29950. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  29951. ' ["QueryInterface", "_AddRef", "_Release"],',
  29952. ' null,',
  29953. ' function () {',
  29954. ' this.$kind = "com";',
  29955. ' var $r = this.$rtti;',
  29956. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  29957. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  29958. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  29959. ' }',
  29960. ');',
  29961. 'rtl.createInterface(',
  29962. ' $mod,',
  29963. ' "IBird",',
  29964. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  29965. ' ["GetItem", "SetItem"],',
  29966. ' $mod.IUnknown,',
  29967. ' function () {',
  29968. ' var $r = this.$rtti;',
  29969. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  29970. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  29971. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  29972. ' }',
  29973. ');',
  29974. 'this.i = null;',
  29975. 'this.t = null;',
  29976. '']),
  29977. LinesToStr([ // $mod.$main
  29978. '$mod.t = $mod.$rtti["IBird"];',
  29979. '$mod.t = $mod.i.$rtti;',
  29980. '']));
  29981. end;
  29982. procedure TTestModule.TestRTTI_ClassHelper;
  29983. begin
  29984. Converter.Options:=Converter.Options-[coNoTypeInfo];
  29985. StartProgram(true,[supTypeInfo]);
  29986. Add([
  29987. '{$interfaces com}',
  29988. '{$modeswitch externalclass}',
  29989. 'type',
  29990. ' TObject = class',
  29991. ' end;',
  29992. ' THelper = class helper for TObject',
  29993. ' published',
  29994. ' function GetItem: longint;',
  29995. ' property Item: longint read GetItem;',
  29996. ' end;',
  29997. 'function THelper.GetItem: longint;',
  29998. 'begin',
  29999. 'end;',
  30000. 'var',
  30001. ' t: TTypeInfoHelper;',
  30002. 'begin',
  30003. ' t:=TypeInfo(THelper);',
  30004. '']);
  30005. ConvertProgram;
  30006. CheckSource('TestRTTI_ClassHelper',
  30007. LinesToStr([ // statements
  30008. 'rtl.createClass($mod, "TObject", null, function () {',
  30009. ' this.$init = function () {',
  30010. ' };',
  30011. ' this.$final = function () {',
  30012. ' };',
  30013. '});',
  30014. 'rtl.createHelper($mod, "THelper", null, function () {',
  30015. ' this.GetItem = function () {',
  30016. ' var Result = 0;',
  30017. ' return Result;',
  30018. ' };',
  30019. ' var $r = this.$rtti;',
  30020. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30021. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30022. '});',
  30023. 'this.t = null;',
  30024. '']),
  30025. LinesToStr([ // $mod.$main
  30026. '$mod.t = $mod.$rtti["THelper"];',
  30027. '']));
  30028. end;
  30029. procedure TTestModule.TestRTTI_ExternalClass;
  30030. begin
  30031. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30032. StartProgram(true,[supTypeInfo]);
  30033. Add([
  30034. '{$modeswitch externalclass}',
  30035. 'type',
  30036. ' TJSObject = class external name ''Object''',
  30037. ' end;',
  30038. ' TJSArray = class external name ''Array'' (TJSObject)',
  30039. ' end;',
  30040. 'var',
  30041. ' p: Pointer;',
  30042. ' tc: TTypeInfoExtClass;',
  30043. 'begin',
  30044. ' p:=typeinfo(TJSArray);']);
  30045. ConvertProgram;
  30046. CheckSource('TestRTTI_ExternalClass',
  30047. LinesToStr([ // statements
  30048. '$mod.$rtti.$ExtClass("TJSObject", {',
  30049. ' jsclass: "Object"',
  30050. '});',
  30051. '$mod.$rtti.$ExtClass("TJSArray", {',
  30052. ' ancestor: $mod.$rtti["TJSObject"],',
  30053. ' jsclass: "Array"',
  30054. '});',
  30055. 'this.p = null;',
  30056. 'this.tc = null;',
  30057. '']),
  30058. LinesToStr([ // $mod.$main
  30059. '$mod.p = $mod.$rtti["TJSArray"];',
  30060. '']));
  30061. end;
  30062. procedure TTestModule.TestResourcestringProgram;
  30063. begin
  30064. AddModuleWithIntfImplSrc('unit2.pas',
  30065. LinesToStr([
  30066. 'resourcestring Title = ''Nice'';',
  30067. '']),
  30068. '');
  30069. StartProgram(true);
  30070. Add([
  30071. 'uses unit2;',
  30072. 'const Bar = ''bar'';',
  30073. 'resourcestring',
  30074. ' Red = ''red'';',
  30075. ' Foobar = ''fOo''+bar;',
  30076. 'var s: string;',
  30077. ' c: char;',
  30078. 'begin',
  30079. ' s:=red;',
  30080. ' s:=test1.red;',
  30081. ' s:=Title;',
  30082. ' c:=red[1];',
  30083. ' c:=test1.red[2];',
  30084. ' if red=foobar then ;',
  30085. ' if red[3]=red[4] then ;']);
  30086. ConvertProgram;
  30087. CheckSource('TestResourcestringProgram',
  30088. LinesToStr([ // statements
  30089. 'this.Bar = "bar";',
  30090. 'this.s = "";',
  30091. 'this.c = "";',
  30092. '$mod.$resourcestrings = {',
  30093. ' Red: {',
  30094. ' org: "red"',
  30095. ' },',
  30096. ' Foobar: {',
  30097. ' org: "fOobar"',
  30098. ' }',
  30099. '};',
  30100. '']),
  30101. LinesToStr([ // $mod.$main
  30102. '$mod.s = rtl.getResStr($mod, "Red");',
  30103. '$mod.s = rtl.getResStr($mod, "Red");',
  30104. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30105. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30106. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30107. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30108. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30109. '']));
  30110. end;
  30111. procedure TTestModule.TestResourcestringUnit;
  30112. begin
  30113. AddModuleWithIntfImplSrc('unit2.pas',
  30114. LinesToStr([
  30115. 'resourcestring Title = ''Nice'';',
  30116. '']),
  30117. '');
  30118. StartUnit(true);
  30119. Add([
  30120. 'interface',
  30121. 'uses unit2;',
  30122. 'const Red = ''rEd'';',
  30123. 'resourcestring',
  30124. ' Blue = ''blue'';',
  30125. ' NotRed = ''not''+Red;',
  30126. 'var s: string;',
  30127. 'implementation',
  30128. 'resourcestring',
  30129. ' ImplGreen = ''green'';',
  30130. 'initialization',
  30131. ' s:=blue+ImplGreen;',
  30132. ' s:=test1.blue+test1.implgreen;',
  30133. ' s:=blue[1]+implgreen[2];',
  30134. ' s:=Title;',
  30135. '']);
  30136. ConvertUnit;
  30137. CheckSource('TestResourcestringUnit',
  30138. LinesToStr([ // statements
  30139. 'this.Red = "rEd";',
  30140. 'this.s = "";',
  30141. '$mod.$resourcestrings = {',
  30142. ' Blue: {',
  30143. ' org: "blue"',
  30144. ' },',
  30145. ' NotRed: {',
  30146. ' org: "notrEd"',
  30147. ' },',
  30148. ' ImplGreen: {',
  30149. ' org: "green"',
  30150. ' }',
  30151. '};',
  30152. '']),
  30153. LinesToStr([ // $mod.$main
  30154. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30155. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30156. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30157. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30158. '']));
  30159. end;
  30160. procedure TTestModule.TestResourcestringImplementation;
  30161. begin
  30162. StartUnit(false);
  30163. Add([
  30164. 'interface',
  30165. 'implementation',
  30166. 'resourcestring',
  30167. ' ImplRed = ''red'';']);
  30168. ConvertUnit;
  30169. CheckSource('TestResourcestringImplementation',
  30170. LinesToStr([ // intf statements
  30171. 'var $impl = $mod.$impl;']),
  30172. LinesToStr([ // $mod.$init
  30173. '']),
  30174. LinesToStr([ // impl statements
  30175. '$mod.$resourcestrings = {',
  30176. ' ImplRed: {',
  30177. ' org: "red"',
  30178. ' }',
  30179. '};',
  30180. '']));
  30181. end;
  30182. procedure TTestModule.TestAttributes_Members;
  30183. begin
  30184. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30185. StartProgram(false);
  30186. Add([
  30187. '{$modeswitch PrefixedAttributes}',
  30188. 'type',
  30189. ' TObject = class',
  30190. ' constructor Create;',
  30191. ' end;',
  30192. ' TCustomAttribute = class',
  30193. ' constructor Create(Id: word);',
  30194. ' end;',
  30195. ' [Missing]',
  30196. ' TBird = class',
  30197. ' published',
  30198. ' [Tcustom]',
  30199. ' FField: word;',
  30200. ' [tcustom(14)]',
  30201. ' property Size: word read FField;',
  30202. ' [Tcustom(15)]',
  30203. ' procedure Fly; virtual; abstract;',
  30204. ' end;',
  30205. ' TRec = record',
  30206. ' [Tcustom,tcustom(14)]',
  30207. ' Size: word;',
  30208. ' end;',
  30209. 'constructor TObject.Create; begin end;',
  30210. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30211. 'begin',
  30212. '']);
  30213. ConvertProgram;
  30214. CheckSource('TestAttributes_Members',
  30215. LinesToStr([ // statements
  30216. 'rtl.createClass($mod, "TObject", null, function () {',
  30217. ' this.$init = function () {',
  30218. ' };',
  30219. ' this.$final = function () {',
  30220. ' };',
  30221. ' this.Create = function () {',
  30222. ' return this;',
  30223. ' };',
  30224. '});',
  30225. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  30226. ' this.Create$1 = function (Id) {',
  30227. ' return this;',
  30228. ' };',
  30229. '});',
  30230. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  30231. ' this.$init = function () {',
  30232. ' $mod.TObject.$init.call(this);',
  30233. ' this.FField = 0;',
  30234. ' };',
  30235. ' var $r = this.$rtti;',
  30236. ' $r.addField("FField", rtl.word, {',
  30237. ' attr: [$mod.TCustomAttribute, "Create"]',
  30238. ' });',
  30239. ' $r.addProperty(',
  30240. ' "Size",',
  30241. ' 0,',
  30242. ' rtl.word,',
  30243. ' "FField",',
  30244. ' "",',
  30245. ' {',
  30246. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30247. ' }',
  30248. ' );',
  30249. ' $r.addMethod("Fly", 0, null, null, {',
  30250. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30251. ' });',
  30252. '});',
  30253. 'rtl.recNewT($mod, "TRec", function () {',
  30254. ' this.Size = 0;',
  30255. ' this.$eq = function (b) {',
  30256. ' return this.Size === b.Size;',
  30257. ' };',
  30258. ' this.$assign = function (s) {',
  30259. ' this.Size = s.Size;',
  30260. ' return this;',
  30261. ' };',
  30262. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30263. ' $r.addField("Size", rtl.word, {',
  30264. ' attr: [',
  30265. ' $mod.TCustomAttribute,',
  30266. ' "Create",',
  30267. ' $mod.TCustomAttribute,',
  30268. ' "Create$1",',
  30269. ' [14]',
  30270. ' ]',
  30271. ' });',
  30272. '});',
  30273. '']),
  30274. LinesToStr([ // $mod.$main
  30275. '']));
  30276. end;
  30277. procedure TTestModule.TestAttributes_Types;
  30278. begin
  30279. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30280. StartProgram(false);
  30281. Add([
  30282. '{$modeswitch PrefixedAttributes}',
  30283. 'type',
  30284. ' TObject = class',
  30285. ' constructor Create(Id: word);',
  30286. ' end;',
  30287. ' TCustomAttribute = class',
  30288. ' end;',
  30289. ' [TCustom(1)]',
  30290. ' TMyClass = class',
  30291. ' end;',
  30292. ' [TCustom(2)]',
  30293. ' TRec = record',
  30294. ' end;',
  30295. ' [TCustom(3)]',
  30296. ' TInt = type word;',
  30297. 'constructor TObject.Create(Id: word);',
  30298. 'begin',
  30299. 'end;',
  30300. 'var p: pointer;',
  30301. 'begin',
  30302. ' p:=typeinfo(TMyClass);',
  30303. ' p:=typeinfo(TRec);',
  30304. ' p:=typeinfo(TInt);',
  30305. '']);
  30306. ConvertProgram;
  30307. CheckSource('TestAttributes_Types',
  30308. LinesToStr([ // statements
  30309. 'rtl.createClass($mod, "TObject", null, function () {',
  30310. ' this.$init = function () {',
  30311. ' };',
  30312. ' this.$final = function () {',
  30313. ' };',
  30314. ' this.Create = function (Id) {',
  30315. ' return this;',
  30316. ' };',
  30317. '});',
  30318. 'rtl.createClass($mod, "TCustomAttribute", $mod.TObject, function () {',
  30319. '});',
  30320. 'rtl.createClass($mod, "TMyClass", $mod.TObject, function () {',
  30321. ' var $r = this.$rtti;',
  30322. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  30323. '});',
  30324. 'rtl.recNewT($mod, "TRec", function () {',
  30325. ' this.$eq = function (b) {',
  30326. ' return true;',
  30327. ' };',
  30328. ' this.$assign = function (s) {',
  30329. ' return this;',
  30330. ' };',
  30331. ' $mod.$rtti.$Record("TRec", {',
  30332. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  30333. ' });',
  30334. '});',
  30335. '$mod.$rtti.$inherited("TInt", rtl.word, {',
  30336. ' attr: [$mod.TCustomAttribute, "Create", [3]]',
  30337. '});',
  30338. 'this.p = null;',
  30339. '']),
  30340. LinesToStr([ // $mod.$main
  30341. '$mod.p = $mod.$rtti["TMyClass"];',
  30342. '$mod.p = $mod.$rtti["TRec"];',
  30343. '$mod.p = $mod.$rtti["TInt"];',
  30344. '']));
  30345. end;
  30346. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  30347. begin
  30348. Converter.Options:=Converter.Options-[coNoTypeInfo];
  30349. StartProgram(false);
  30350. Add([
  30351. '{$modeswitch PrefixedAttributes}',
  30352. 'type',
  30353. ' TObject = class',
  30354. ' constructor Create;',
  30355. ' end;',
  30356. ' TCustomAttribute = class',
  30357. ' end;',
  30358. ' THelper = class helper for TCustomAttribute',
  30359. ' constructor Create(Id: word);',
  30360. ' end;',
  30361. ' [TCustom(3)]',
  30362. ' TMyInt = word;',
  30363. 'constructor TObject.Create; begin end;',
  30364. 'constructor THelper.Create(Id: word); begin end;',
  30365. 'begin',
  30366. ' if typeinfo(TMyInt)=nil then ;']);
  30367. ConvertProgram;
  30368. end;
  30369. procedure TTestModule.TestAssert;
  30370. begin
  30371. StartProgram(false);
  30372. Add([
  30373. 'procedure DoIt;',
  30374. 'var',
  30375. ' b: boolean;',
  30376. ' s: string;',
  30377. 'begin',
  30378. ' {$Assertions on}',
  30379. ' Assert(b);',
  30380. 'end;',
  30381. 'begin',
  30382. ' DoIt;',
  30383. '']);
  30384. ConvertProgram;
  30385. CheckSource('TestAssert',
  30386. LinesToStr([ // statements
  30387. 'this.DoIt = function () {',
  30388. ' var b = false;',
  30389. ' var s = "";',
  30390. ' if (!b) throw "assert failed";',
  30391. '};',
  30392. '']),
  30393. LinesToStr([ // $mod.$main
  30394. '$mod.DoIt();',
  30395. '']));
  30396. end;
  30397. procedure TTestModule.TestAssert_SysUtils;
  30398. begin
  30399. AddModuleWithIntfImplSrc('SysUtils.pas',
  30400. LinesToStr([
  30401. 'type',
  30402. ' TObject = class',
  30403. ' constructor Create;',
  30404. ' end;',
  30405. ' EAssertionFailed = class',
  30406. ' constructor Create(s: string);',
  30407. ' end;',
  30408. '']),
  30409. LinesToStr([
  30410. 'constructor TObject.Create;',
  30411. 'begin end;',
  30412. 'constructor EAssertionFailed.Create(s: string);',
  30413. 'begin end;',
  30414. '']) );
  30415. StartProgram(true);
  30416. Add([
  30417. 'uses sysutils;',
  30418. 'procedure DoIt;',
  30419. 'var',
  30420. ' b: boolean;',
  30421. ' s: string;',
  30422. 'begin',
  30423. ' {$Assertions on}',
  30424. ' Assert(b);',
  30425. ' Assert(b,''msg'');',
  30426. 'end;',
  30427. 'begin',
  30428. ' DoIt;',
  30429. '']);
  30430. ConvertProgram;
  30431. CheckSource('TestAssert_SysUtils',
  30432. LinesToStr([ // statements
  30433. 'this.DoIt = function () {',
  30434. ' var b = false;',
  30435. ' var s = "";',
  30436. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  30437. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  30438. '};',
  30439. '']),
  30440. LinesToStr([ // $mod.$main
  30441. '$mod.DoIt();',
  30442. '']));
  30443. end;
  30444. procedure TTestModule.TestObjectChecks;
  30445. begin
  30446. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  30447. StartProgram(false);
  30448. Add([
  30449. 'type',
  30450. ' TObject = class',
  30451. ' procedure DoIt;',
  30452. ' end;',
  30453. ' TClass = class of tobject;',
  30454. ' TBird = class',
  30455. ' end;',
  30456. ' TBirdClass = class of TBird;',
  30457. 'var',
  30458. ' o : TObject;',
  30459. ' c: TClass;',
  30460. ' b: TBird;',
  30461. ' bc: TBirdClass;',
  30462. 'procedure TObject.DoIt;',
  30463. 'begin',
  30464. ' b:=TBird(o);',
  30465. 'end;',
  30466. 'begin',
  30467. ' o.DoIt;',
  30468. ' b:=TBird(o);',
  30469. ' bc:=TBirdClass(c);',
  30470. '']);
  30471. ConvertProgram;
  30472. CheckSource('TestCheckMethodCall',
  30473. LinesToStr([ // statements
  30474. 'rtl.createClass($mod, "TObject", null, function () {',
  30475. ' this.$init = function () {',
  30476. ' };',
  30477. ' this.$final = function () {',
  30478. ' };',
  30479. ' this.DoIt = function () {',
  30480. ' rtl.checkMethodCall(this,$mod.TObject);',
  30481. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  30482. ' };',
  30483. '});',
  30484. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  30485. '});',
  30486. 'this.o = null;',
  30487. 'this.c = null;',
  30488. 'this.b = null;',
  30489. 'this.bc = null;',
  30490. '']),
  30491. LinesToStr([ // $mod.$main
  30492. '$mod.o.DoIt();',
  30493. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  30494. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  30495. '']));
  30496. end;
  30497. procedure TTestModule.TestOverflowChecks_Int;
  30498. begin
  30499. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  30500. StartProgram(false);
  30501. Add([
  30502. 'procedure DoIt;',
  30503. 'var',
  30504. ' b: byte;',
  30505. ' n: nativeint;',
  30506. ' u: nativeuint;',
  30507. ' c: currency;',
  30508. 'begin',
  30509. ' n:=n+n;',
  30510. ' n:=n-n;',
  30511. ' n:=n+b;',
  30512. ' n:=b-n;',
  30513. ' n:=n*n;',
  30514. ' n:=n*u;',
  30515. ' c:=c+b;',
  30516. ' c:=b+c;',
  30517. ' c:=c*b;',
  30518. ' c:=b*c;',
  30519. 'end;',
  30520. 'begin',
  30521. '']);
  30522. ConvertProgram;
  30523. CheckSource('TestOverflowChecks_Int',
  30524. LinesToStr([ // statements
  30525. 'this.DoIt = function () {',
  30526. ' var b = 0;',
  30527. ' var n = 0;',
  30528. ' var u = 0;',
  30529. ' var c = 0;',
  30530. ' n = rtl.oc(n + n);',
  30531. ' n = rtl.oc(n - n);',
  30532. ' n = rtl.oc(n + b);',
  30533. ' n = rtl.oc(b - n);',
  30534. ' n = rtl.oc(n * n);',
  30535. ' n = rtl.oc(n * u);',
  30536. ' c = rtl.oc(c + (b * 10000));',
  30537. ' c = rtl.oc((b * 10000) + c);',
  30538. ' c = rtl.oc(c * b);',
  30539. ' c = rtl.oc(b * c);',
  30540. '};',
  30541. '']),
  30542. LinesToStr([ // $mod.$main
  30543. '']));
  30544. end;
  30545. procedure TTestModule.TestRangeChecks_AssignInt;
  30546. begin
  30547. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30548. StartProgram(false);
  30549. Add([
  30550. '{$R+}',
  30551. 'var',
  30552. ' b: byte = 2;',
  30553. ' w: word = 3;',
  30554. 'procedure DoIt(p: byte);',
  30555. 'begin',
  30556. ' b:=w;',
  30557. ' b+=w;',
  30558. ' b:=1;',
  30559. 'end;',
  30560. '{$R-}',
  30561. 'procedure DoSome;',
  30562. 'begin',
  30563. ' DoIt(w);',
  30564. ' b:=w;',
  30565. ' b:=2;',
  30566. 'end;',
  30567. 'begin',
  30568. '{$R+}',
  30569. '']);
  30570. ConvertProgram;
  30571. CheckSource('TestRangeChecks_AssignInt',
  30572. LinesToStr([ // statements
  30573. 'this.b = 2;',
  30574. 'this.w = 3;',
  30575. 'this.DoIt = function (p) {',
  30576. ' rtl.rc(p, 0, 255);',
  30577. ' $mod.b = rtl.rc($mod.w,0,255);',
  30578. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  30579. ' $mod.b = 1;',
  30580. '};',
  30581. 'this.DoSome = function () {',
  30582. ' $mod.DoIt($mod.w);',
  30583. ' $mod.b = $mod.w;',
  30584. ' $mod.b = 2;',
  30585. '};',
  30586. '']),
  30587. LinesToStr([ // $mod.$main
  30588. '']));
  30589. end;
  30590. procedure TTestModule.TestRangeChecks_AssignIntRange;
  30591. begin
  30592. Scanner.Options:=Scanner.Options+[po_CAssignments];
  30593. StartProgram(false);
  30594. Add([
  30595. '{$R+}',
  30596. 'type Ten = 1..10;',
  30597. 'var',
  30598. ' b: Ten = 2;',
  30599. ' w: Ten = 3;',
  30600. 'procedure DoIt(p: Ten);',
  30601. 'begin',
  30602. ' b:=w;',
  30603. ' b+=w;',
  30604. ' b:=1;',
  30605. 'end;',
  30606. '{$R-}',
  30607. 'procedure DoSome;',
  30608. 'begin',
  30609. ' DoIt(w);',
  30610. ' b:=w;',
  30611. ' b:=2;',
  30612. 'end;',
  30613. 'begin',
  30614. '{$R+}',
  30615. '']);
  30616. ConvertProgram;
  30617. CheckSource('TestRangeChecks_AssignIntRange',
  30618. LinesToStr([ // statements
  30619. 'this.b = 2;',
  30620. 'this.w = 3;',
  30621. 'this.DoIt = function (p) {',
  30622. ' rtl.rc(p, 1, 10);',
  30623. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  30624. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  30625. ' $mod.b = 1;',
  30626. '};',
  30627. 'this.DoSome = function () {',
  30628. ' $mod.DoIt($mod.w);',
  30629. ' $mod.b = $mod.w;',
  30630. ' $mod.b = 2;',
  30631. '};',
  30632. '']),
  30633. LinesToStr([ // $mod.$main
  30634. '']));
  30635. end;
  30636. procedure TTestModule.TestRangeChecks_AssignEnum;
  30637. begin
  30638. StartProgram(false);
  30639. Add([
  30640. '{$R+}',
  30641. 'type TEnum = (red,green);',
  30642. 'var',
  30643. ' e: TEnum = red;',
  30644. 'procedure DoIt(p: TEnum);',
  30645. 'begin',
  30646. ' e:=p;',
  30647. ' p:=TEnum(0);',
  30648. ' p:=succ(e);',
  30649. 'end;',
  30650. '{$R-}',
  30651. 'procedure DoSome;',
  30652. 'begin',
  30653. ' DoIt(e);',
  30654. ' e:=TEnum(1);',
  30655. ' e:=pred(e);',
  30656. 'end;',
  30657. 'begin',
  30658. '{$R+}',
  30659. '']);
  30660. ConvertProgram;
  30661. CheckSource('TestRangeChecks_AssignEnum',
  30662. LinesToStr([ // statements
  30663. 'this.TEnum = {',
  30664. ' "0": "red",',
  30665. ' red: 0,',
  30666. ' "1": "green",',
  30667. ' green: 1',
  30668. '};',
  30669. 'this.e = $mod.TEnum.red;',
  30670. 'this.DoIt = function (p) {',
  30671. ' rtl.rc(p, 0, 1);',
  30672. ' $mod.e = rtl.rc(p, 0, 1);',
  30673. ' p = 0;',
  30674. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30675. '};',
  30676. 'this.DoSome = function () {',
  30677. ' $mod.DoIt($mod.e);',
  30678. ' $mod.e = 1;',
  30679. ' $mod.e = $mod.e - 1;',
  30680. '};',
  30681. '']),
  30682. LinesToStr([ // $mod.$main
  30683. '']));
  30684. end;
  30685. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  30686. begin
  30687. StartProgram(false);
  30688. Add([
  30689. '{$R+}',
  30690. 'type',
  30691. ' TEnum = (red,green);',
  30692. ' TEnumRg = red..green;',
  30693. 'var',
  30694. ' e: TEnumRg = red;',
  30695. 'procedure DoIt(p: TEnumRg);',
  30696. 'begin',
  30697. ' e:=p;',
  30698. ' p:=TEnumRg(0);',
  30699. ' p:=succ(e);',
  30700. 'end;',
  30701. '{$R-}',
  30702. 'procedure DoSome;',
  30703. 'begin',
  30704. ' DoIt(e);',
  30705. ' e:=TEnum(1);',
  30706. ' e:=pred(e);',
  30707. 'end;',
  30708. 'begin',
  30709. '{$R+}',
  30710. '']);
  30711. ConvertProgram;
  30712. CheckSource('TestRangeChecks_AssignEnumRange',
  30713. LinesToStr([ // statements
  30714. 'this.TEnum = {',
  30715. ' "0": "red",',
  30716. ' red: 0,',
  30717. ' "1": "green",',
  30718. ' green: 1',
  30719. '};',
  30720. 'this.e = $mod.TEnum.red;',
  30721. 'this.DoIt = function (p) {',
  30722. ' rtl.rc(p, 0, 1);',
  30723. ' $mod.e = rtl.rc(p, 0, 1);',
  30724. ' p = 0;',
  30725. ' p = rtl.rc($mod.e + 1, 0, 1);',
  30726. '};',
  30727. 'this.DoSome = function () {',
  30728. ' $mod.DoIt($mod.e);',
  30729. ' $mod.e = 1;',
  30730. ' $mod.e = $mod.e - 1;',
  30731. '};',
  30732. '']),
  30733. LinesToStr([ // $mod.$main
  30734. '']));
  30735. end;
  30736. procedure TTestModule.TestRangeChecks_AssignChar;
  30737. begin
  30738. StartProgram(false);
  30739. Add([
  30740. '{$R+}',
  30741. 'type',
  30742. ' TLetter = char;',
  30743. 'var',
  30744. ' b: TLetter = ''2'';',
  30745. ' w: TLetter = ''3'';',
  30746. 'procedure DoIt(p: TLetter);',
  30747. 'begin',
  30748. ' b:=w;',
  30749. ' b:=''1'';',
  30750. 'end;',
  30751. '{$R-}',
  30752. 'procedure DoSome;',
  30753. 'begin',
  30754. ' DoIt(w);',
  30755. ' b:=w;',
  30756. ' b:=''2'';',
  30757. 'end;',
  30758. 'begin',
  30759. '{$R+}',
  30760. '']);
  30761. ConvertProgram;
  30762. CheckSource('TestRangeChecks_AssignChar',
  30763. LinesToStr([ // statements
  30764. 'this.b = "2";',
  30765. 'this.w = "3";',
  30766. 'this.DoIt = function (p) {',
  30767. ' rtl.rcc(p, 0, 65535);',
  30768. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  30769. ' $mod.b = "1";',
  30770. '};',
  30771. 'this.DoSome = function () {',
  30772. ' $mod.DoIt($mod.w);',
  30773. ' $mod.b = $mod.w;',
  30774. ' $mod.b = "2";',
  30775. '};',
  30776. '']),
  30777. LinesToStr([ // $mod.$main
  30778. '']));
  30779. end;
  30780. procedure TTestModule.TestRangeChecks_AssignCharRange;
  30781. begin
  30782. StartProgram(false);
  30783. Add([
  30784. '{$R+}',
  30785. 'type TDigit = ''0''..''9'';',
  30786. 'var',
  30787. ' b: TDigit = ''2'';',
  30788. ' w: TDigit = ''3'';',
  30789. 'procedure DoIt(p: TDigit);',
  30790. 'begin',
  30791. ' b:=w;',
  30792. ' b:=''1'';',
  30793. 'end;',
  30794. '{$R-}',
  30795. 'procedure DoSome;',
  30796. 'begin',
  30797. ' DoIt(w);',
  30798. ' b:=w;',
  30799. ' b:=''2'';',
  30800. 'end;',
  30801. 'begin',
  30802. '{$R+}',
  30803. '']);
  30804. ConvertProgram;
  30805. CheckSource('TestRangeChecks_AssignCharRange',
  30806. LinesToStr([ // statements
  30807. 'this.b = "2";',
  30808. 'this.w = "3";',
  30809. 'this.DoIt = function (p) {',
  30810. ' rtl.rcc(p, 48, 57);',
  30811. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  30812. ' $mod.b = "1";',
  30813. '};',
  30814. 'this.DoSome = function () {',
  30815. ' $mod.DoIt($mod.w);',
  30816. ' $mod.b = $mod.w;',
  30817. ' $mod.b = "2";',
  30818. '};',
  30819. '']),
  30820. LinesToStr([ // $mod.$main
  30821. '']));
  30822. end;
  30823. procedure TTestModule.TestRangeChecks_ArrayIndex;
  30824. begin
  30825. StartProgram(false);
  30826. Add([
  30827. '{$R+}',
  30828. 'type',
  30829. ' Ten = 1..10;',
  30830. ' TArr = array of Ten;',
  30831. ' TArrArr = array of TArr;',
  30832. ' TArrByte = array[byte] of Ten;',
  30833. ' TArrChar = array[''0''..''9''] of Ten;',
  30834. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  30835. ' TObject = class',
  30836. ' A: TArr;',
  30837. ' end;',
  30838. 'procedure DoIt;',
  30839. 'var',
  30840. ' Arr: TArr;',
  30841. ' ArrArr: TArrArr;',
  30842. ' ArrByte: TArrByte;',
  30843. ' ArrChar: TArrChar;',
  30844. ' ArrByteChar: TArrByteChar;',
  30845. ' i: Ten;',
  30846. ' c: char;',
  30847. ' o: tobject;',
  30848. 'begin',
  30849. ' i:=Arr[1];',
  30850. ' i:=ArrByteChar[1,''2''];',
  30851. ' Arr[1]:=Arr[1];',
  30852. ' Arr[i]:=Arr[i];',
  30853. ' ArrByte[3]:=ArrByte[3];',
  30854. ' ArrByte[i]:=ArrByte[i];',
  30855. ' ArrChar[''5'']:=ArrChar[''5''];',
  30856. ' ArrChar[c]:=ArrChar[c];',
  30857. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  30858. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  30859. ' o.a[i]:=o.a[i];',
  30860. 'end;',
  30861. 'begin',
  30862. '']);
  30863. ConvertProgram;
  30864. CheckSource('TestRangeChecks_ArrayIndex',
  30865. LinesToStr([ // statements
  30866. 'rtl.createClass($mod, "TObject", null, function () {',
  30867. ' this.$init = function () {',
  30868. ' this.A = [];',
  30869. ' };',
  30870. ' this.$final = function () {',
  30871. ' this.A = undefined;',
  30872. ' };',
  30873. '});',
  30874. 'this.DoIt = function () {',
  30875. ' var Arr = [];',
  30876. ' var ArrArr = [];',
  30877. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  30878. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  30879. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  30880. ' var i = 0;',
  30881. ' var c = "";',
  30882. ' var o = null;',
  30883. ' i = rtl.rc(Arr[1], 1, 10);',
  30884. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  30885. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  30886. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  30887. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  30888. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  30889. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  30890. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  30891. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  30892. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  30893. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  30894. '};',
  30895. '']),
  30896. LinesToStr([ // $mod.$main
  30897. '']));
  30898. end;
  30899. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  30900. begin
  30901. StartProgram(false);
  30902. Add([
  30903. '{$R+}',
  30904. 'type',
  30905. ' Ten = 1..10;',
  30906. ' TRec = record x: Ten end;',
  30907. ' TArr = array of TRec;',
  30908. ' TArrArr = array of TArr;',
  30909. ' TObject = class',
  30910. ' A: TArr;',
  30911. ' end;',
  30912. 'procedure DoIt;',
  30913. 'var',
  30914. ' Arr: TArr;',
  30915. ' ArrArr: TArrArr;',
  30916. ' i: Ten;',
  30917. ' o: tobject;',
  30918. 'begin',
  30919. ' Arr[1]:=Arr[1];',
  30920. ' Arr[i]:=Arr[i+1];',
  30921. ' o.a[i]:=o.a[i+2];',
  30922. 'end;',
  30923. 'begin',
  30924. '']);
  30925. ConvertProgram;
  30926. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  30927. LinesToStr([ // statements
  30928. 'rtl.recNewT($mod, "TRec", function () {',
  30929. ' this.x = 0;',
  30930. ' this.$eq = function (b) {',
  30931. ' return this.x === b.x;',
  30932. ' };',
  30933. ' this.$assign = function (s) {',
  30934. ' this.x = s.x;',
  30935. ' return this;',
  30936. ' };',
  30937. '});',
  30938. 'rtl.createClass($mod, "TObject", null, function () {',
  30939. ' this.$init = function () {',
  30940. ' this.A = [];',
  30941. ' };',
  30942. ' this.$final = function () {',
  30943. ' this.A = undefined;',
  30944. ' };',
  30945. '});',
  30946. 'this.DoIt = function () {',
  30947. ' var Arr = [];',
  30948. ' var ArrArr = [];',
  30949. ' var i = 0;',
  30950. ' var o = null;',
  30951. ' Arr[1].$assign(Arr[1]);',
  30952. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  30953. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  30954. '};',
  30955. '']),
  30956. LinesToStr([ // $mod.$main
  30957. '']));
  30958. end;
  30959. procedure TTestModule.TestRangeChecks_StringIndex;
  30960. begin
  30961. StartProgram(false);
  30962. Add([
  30963. 'type',
  30964. ' TObject = class',
  30965. ' S: string;',
  30966. ' end;',
  30967. '{$R+}',
  30968. 'procedure DoIt(var h: string);',
  30969. 'var',
  30970. ' s: string;',
  30971. ' i: longint;',
  30972. ' c: char;',
  30973. ' o: tobject;',
  30974. 'begin',
  30975. ' c:=s[1];',
  30976. ' s[i]:=s[i];',
  30977. ' h[i]:=h[i];',
  30978. ' c:=o.s[i];',
  30979. ' o.s[i]:=c;',
  30980. 'end;',
  30981. 'begin',
  30982. '']);
  30983. ConvertProgram;
  30984. CheckSource('TestRangeChecks_StringIndex',
  30985. LinesToStr([ // statements
  30986. 'rtl.createClass($mod, "TObject", null, function () {',
  30987. ' this.$init = function () {',
  30988. ' this.S = "";',
  30989. ' };',
  30990. ' this.$final = function () {',
  30991. ' };',
  30992. '});',
  30993. 'this.DoIt = function (h) {',
  30994. ' var s = "";',
  30995. ' var i = 0;',
  30996. ' var c = "";',
  30997. ' var o = null;',
  30998. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  30999. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31000. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31001. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31002. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31003. '};',
  31004. '']),
  31005. LinesToStr([ // $mod.$main
  31006. '']));
  31007. end;
  31008. procedure TTestModule.TestRangeChecks_TypecastInt;
  31009. begin
  31010. StartProgram(false);
  31011. Add([
  31012. '{$R+}',
  31013. 'var',
  31014. ' i: nativeint;',
  31015. ' b: byte;',
  31016. ' sh: shortint;',
  31017. ' w: word;',
  31018. ' sm: smallint;',
  31019. ' lw: longword;',
  31020. ' li: longint;',
  31021. 'begin',
  31022. ' b:=12+byte(i);',
  31023. ' sh:=12+shortint(i);',
  31024. ' w:=12+word(i);',
  31025. ' sm:=12+smallint(i);',
  31026. ' lw:=12+longword(i);',
  31027. ' li:=12+longint(i);',
  31028. '']);
  31029. ConvertProgram;
  31030. CheckSource('TestRangeChecks_TypecastInt',
  31031. LinesToStr([
  31032. 'this.i = 0;',
  31033. 'this.b = 0;',
  31034. 'this.sh = 0;',
  31035. 'this.w = 0;',
  31036. 'this.sm = 0;',
  31037. 'this.lw = 0;',
  31038. 'this.li = 0;',
  31039. '']),
  31040. LinesToStr([
  31041. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31042. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31043. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31044. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31045. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31046. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31047. '']));
  31048. end;
  31049. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31050. begin
  31051. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31052. StartProgram(false);
  31053. Add([
  31054. '{$modeswitch typehelpers}',
  31055. '{$R+}',
  31056. 'type',
  31057. ' TObject = class',
  31058. ' FSize: byte;',
  31059. ' property Size: byte read FSize;',
  31060. ' end;',
  31061. ' THelper = type helper for byte',
  31062. ' procedure SetIt(w: word);',
  31063. ' end;',
  31064. 'procedure THelper.SetIt(w: word);',
  31065. 'begin',
  31066. ' Self:=w;',
  31067. 'end;',
  31068. 'function GetIt: byte;',
  31069. 'begin',
  31070. ' Result.SetIt(2);',
  31071. 'end;',
  31072. 'var',
  31073. ' b: byte = 3;',
  31074. ' o: TObject;',
  31075. 'begin',
  31076. ' b.SetIt(14);',
  31077. ' with b do SetIt(15);',
  31078. ' o.Size.SetIt(16);',
  31079. '']);
  31080. ConvertProgram;
  31081. CheckSource('TestRangeChecks_AssignInt',
  31082. LinesToStr([ // statements
  31083. 'rtl.createClass($mod, "TObject", null, function () {',
  31084. ' this.$init = function () {',
  31085. ' this.FSize = 0;',
  31086. ' };',
  31087. ' this.$final = function () {',
  31088. ' };',
  31089. '});',
  31090. 'rtl.createHelper($mod, "THelper", null, function () {',
  31091. ' this.SetIt = function (w) {',
  31092. ' rtl.rc(w, 0, 65535);',
  31093. ' this.set(w);',
  31094. ' };',
  31095. '});',
  31096. 'this.GetIt = function () {',
  31097. ' var Result = 0;',
  31098. ' $mod.THelper.SetIt.call({',
  31099. ' get: function () {',
  31100. ' return Result;',
  31101. ' },',
  31102. ' set: function (v) {',
  31103. ' rtl.rc(v, 0, 255);',
  31104. ' Result = v;',
  31105. ' }',
  31106. ' }, 2);',
  31107. ' return Result;',
  31108. '};',
  31109. 'this.b = 3;',
  31110. 'this.o = null;',
  31111. '']),
  31112. LinesToStr([ // $mod.$main
  31113. '$mod.THelper.SetIt.call({',
  31114. ' p: $mod,',
  31115. ' get: function () {',
  31116. ' return this.p.b;',
  31117. ' },',
  31118. ' set: function (v) {',
  31119. ' rtl.rc(v, 0, 255);',
  31120. ' this.p.b = v;',
  31121. ' }',
  31122. '}, 14);',
  31123. 'var $with = $mod.b;',
  31124. '$mod.THelper.SetIt.call({',
  31125. ' get: function () {',
  31126. ' return $with;',
  31127. ' },',
  31128. ' set: function (v) {',
  31129. ' rtl.rc(v, 0, 255);',
  31130. ' $with = v;',
  31131. ' }',
  31132. '}, 15);',
  31133. '$mod.THelper.SetIt.call({',
  31134. ' p: $mod.o,',
  31135. ' get: function () {',
  31136. ' return this.p.FSize;',
  31137. ' },',
  31138. ' set: function (v) {',
  31139. ' rtl.rc(v, 0, 255);',
  31140. ' this.p.FSize = v;',
  31141. ' }',
  31142. '}, 16);',
  31143. '']));
  31144. end;
  31145. procedure TTestModule.TestAsync_Proc;
  31146. begin
  31147. StartProgram(false);
  31148. Add([
  31149. 'procedure Fly(w: word = 1); async; forward;',
  31150. 'procedure Run(w: word = 2); async;',
  31151. 'begin',
  31152. ' Fly(w);',
  31153. ' Fly;',
  31154. ' await(Fly(w));',
  31155. ' await(Fly);',
  31156. 'end;',
  31157. 'procedure Fly(w: word); ',
  31158. 'begin',
  31159. 'end;',
  31160. 'begin',
  31161. ' Run;',
  31162. ' Run(3);',
  31163. '']);
  31164. ConvertProgram;
  31165. CheckSource('TestAsync_Proc',
  31166. LinesToStr([ // statements
  31167. 'this.Run = async function (w) {',
  31168. ' $mod.Fly(w);',
  31169. ' $mod.Fly(1);',
  31170. ' await $mod.Fly(w);',
  31171. ' await $mod.Fly(1);',
  31172. '};',
  31173. 'this.Fly = async function (w) {',
  31174. '};',
  31175. '']),
  31176. LinesToStr([
  31177. '$mod.Run(2);',
  31178. '$mod.Run(3);',
  31179. '']));
  31180. end;
  31181. procedure TTestModule.TestAsync_CallResultIsPromise;
  31182. begin
  31183. StartProgram(false);
  31184. Add([
  31185. '{$modeswitch externalclass}',
  31186. 'type',
  31187. ' TObject = class',
  31188. ' end;',
  31189. ' TJSPromise = class external name ''Promise''',
  31190. ' end;',
  31191. ' TBird = class',
  31192. ' function Fly: word; async; ',
  31193. ' end;',
  31194. 'function TBird.Fly: word; async; ',
  31195. 'begin',
  31196. ' Result:=3;',
  31197. ' Fly:=4+Result;',
  31198. ' if Result=5 then ;',
  31199. ' exit(6);',
  31200. 'end;',
  31201. 'function Run: word; async;',
  31202. 'begin',
  31203. ' Result:=11+Result;',
  31204. ' inc(Result);',
  31205. 'end;',
  31206. 'var',
  31207. ' p: TJSPromise;',
  31208. ' o: TBird;',
  31209. 'begin',
  31210. ' p:=Run;',
  31211. ' p:=Run();',
  31212. ' if Run=p then ;',
  31213. ' if p=Run then ;',
  31214. ' if Run()=p then ;',
  31215. ' if p=Run() then ;',
  31216. ' p:=o.Fly;',
  31217. ' p:=o.Fly();',
  31218. ' if o.Fly=p then ;',
  31219. ' if o.Fly()=p then ;',
  31220. ' with o do begin',
  31221. ' p:=Fly;',
  31222. ' p:=Fly();',
  31223. ' if Fly=p then ;',
  31224. ' if Fly()=p then ;',
  31225. ' end;',
  31226. '']);
  31227. ConvertProgram;
  31228. CheckSource('TestAsync_CallResultIsPromise',
  31229. LinesToStr([ // statements
  31230. 'rtl.createClass($mod, "TObject", null, function () {',
  31231. ' this.$init = function () {',
  31232. ' };',
  31233. ' this.$final = function () {',
  31234. ' };',
  31235. '});',
  31236. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  31237. ' this.Fly = async function () {',
  31238. ' var Result = 0;',
  31239. ' Result = 3;',
  31240. ' Result = 4 + Result;',
  31241. ' if (Result === 5) ;',
  31242. ' return 6;',
  31243. ' return Result;',
  31244. ' };',
  31245. '});',
  31246. 'this.Run = async function () {',
  31247. ' var Result = 0;',
  31248. ' Result = 11 + Result;',
  31249. ' Result += 1;',
  31250. ' return Result;',
  31251. '};',
  31252. 'this.p = null;',
  31253. 'this.o = null;',
  31254. '']),
  31255. LinesToStr([
  31256. '$mod.p = $mod.Run();',
  31257. '$mod.p = $mod.Run();',
  31258. 'if ($mod.Run() === $mod.p) ;',
  31259. 'if ($mod.p === $mod.Run()) ;',
  31260. 'if ($mod.Run() === $mod.p) ;',
  31261. 'if ($mod.p === $mod.Run()) ;',
  31262. '$mod.p = $mod.o.Fly();',
  31263. '$mod.p = $mod.o.Fly();',
  31264. 'if ($mod.o.Fly() === $mod.p) ;',
  31265. 'if ($mod.o.Fly() === $mod.p) ;',
  31266. 'var $with = $mod.o;',
  31267. '$mod.p = $with.Fly();',
  31268. '$mod.p = $with.Fly();',
  31269. 'if ($with.Fly() === $mod.p) ;',
  31270. 'if ($with.Fly() === $mod.p) ;',
  31271. '']));
  31272. end;
  31273. procedure TTestModule.TestAsync_ConstructorFail;
  31274. begin
  31275. StartProgram(false);
  31276. Add([
  31277. 'type',
  31278. ' TObject = class',
  31279. ' end;',
  31280. ' TBird = class',
  31281. ' constructor Create; async;',
  31282. ' end;',
  31283. 'constructor TBird.Create; async;',
  31284. 'begin',
  31285. 'end;',
  31286. 'begin',
  31287. '']);
  31288. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  31289. ConvertProgram;
  31290. end;
  31291. procedure TTestModule.TestAsync_PropertyGetterFail;
  31292. begin
  31293. StartProgram(false);
  31294. Add([
  31295. 'type',
  31296. ' TObject = class',
  31297. ' end;',
  31298. ' TBird = class',
  31299. ' function GetSize: word; async;',
  31300. ' property Size: word read GetSize;',
  31301. ' end;',
  31302. 'function TBird.GetSize: word; async;',
  31303. 'begin',
  31304. 'end;',
  31305. 'begin',
  31306. '']);
  31307. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  31308. ConvertProgram;
  31309. end;
  31310. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  31311. begin
  31312. StartProgram(false);
  31313. Add([
  31314. 'procedure Run; async;',
  31315. 'begin',
  31316. ' await(word,1);',
  31317. 'end;',
  31318. 'begin',
  31319. '']);
  31320. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  31321. ConvertProgram;
  31322. end;
  31323. procedure TTestModule.TestAWait_OutsideAsyncFail;
  31324. begin
  31325. StartProgram(false);
  31326. Add([
  31327. 'procedure Crawl(w: double); ',
  31328. 'begin',
  31329. 'end;',
  31330. 'procedure Run(w: double);',
  31331. 'begin',
  31332. ' await(Crawl(w));',
  31333. 'end;',
  31334. 'begin',
  31335. ' Run(1);']);
  31336. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  31337. ConvertProgram;
  31338. end;
  31339. procedure TTestModule.TestAWait_Result;
  31340. begin
  31341. StartProgram(false);
  31342. Add([
  31343. '{$modeswitch externalclass}',
  31344. 'type',
  31345. ' TJSPromise = class external name ''Promise''',
  31346. ' end;',
  31347. 'function Crawl(d: double = 1.3): word; ',
  31348. 'begin',
  31349. 'end;',
  31350. 'function Run(d: double = 1.6): word; async;',
  31351. 'begin',
  31352. ' Result:=await(1);',
  31353. ' Result:=await(Crawl);',
  31354. ' Result:=await(Crawl(4.5));',
  31355. ' Result:=await(Run);',
  31356. ' Result:=await(Run(6.7));',
  31357. 'end;',
  31358. 'begin',
  31359. ' Run(1);']);
  31360. ConvertProgram;
  31361. CheckSource('TestAWait_Result',
  31362. LinesToStr([ // statements
  31363. 'this.Crawl = function (d) {',
  31364. ' var Result = 0;',
  31365. ' return Result;',
  31366. '};',
  31367. 'this.Run = async function (d) {',
  31368. ' var Result = 0;',
  31369. ' Result = await 1;',
  31370. ' Result = await $mod.Crawl(1.3);',
  31371. ' Result = await $mod.Crawl(4.5);',
  31372. ' Result = await $mod.Run(1.6);',
  31373. ' Result = await $mod.Run(6.7);',
  31374. ' return Result;',
  31375. '};',
  31376. '']),
  31377. LinesToStr([
  31378. '$mod.Run(1);'
  31379. ]));
  31380. end;
  31381. procedure TTestModule.TestAWait_ExternalClassPromise;
  31382. begin
  31383. StartProgram(false);
  31384. Add([
  31385. '{$modeswitch externalclass}',
  31386. 'type',
  31387. ' TJSPromise = class external name ''Promise''',
  31388. ' end;',
  31389. 'function Fly(w: word): TJSPromise; async;',
  31390. 'begin',
  31391. 'end;',
  31392. 'function Jump(w: word): word; async;',
  31393. 'begin',
  31394. 'end;',
  31395. 'function Run(d: double): word; async;',
  31396. 'var',
  31397. ' p: TJSPromise;',
  31398. 'begin',
  31399. ' Result:=await(word,p);', // promise needs type
  31400. ' Result:=await(word,Fly(3));', // promise needs type
  31401. ' Result:=await(Jump(4));', // async non promise must omit the type
  31402. 'end;',
  31403. 'begin',
  31404. '']);
  31405. ConvertProgram;
  31406. CheckSource('TestAWait_ExternalClassPromise',
  31407. LinesToStr([ // statements
  31408. 'this.Fly = async function (w) {',
  31409. ' var Result = null;',
  31410. ' return Result;',
  31411. '};',
  31412. 'this.Jump = async function (w) {',
  31413. ' var Result = 0;',
  31414. ' return Result;',
  31415. '};',
  31416. 'this.Run = async function (d) {',
  31417. ' var Result = 0;',
  31418. ' var p = null;',
  31419. ' Result = await p;',
  31420. ' Result = await $mod.Fly(3);',
  31421. ' Result = await $mod.Jump(4);',
  31422. ' return Result;',
  31423. '};',
  31424. '']),
  31425. LinesToStr([
  31426. ]));
  31427. end;
  31428. procedure TTestModule.TestAsync_AnonymousProc;
  31429. begin
  31430. StartProgram(false);
  31431. Add([
  31432. '{$modeswitch externalclass}',
  31433. 'type',
  31434. ' TJSPromise = class external name ''Promise''',
  31435. ' end;',
  31436. '{$mode objfpc}',
  31437. 'type',
  31438. ' TFunc = reference to function(x: double): word; async;',
  31439. 'function Crawl(d: double = 1.3): word; async;',
  31440. 'begin',
  31441. 'end;',
  31442. 'var Func: TFunc;',
  31443. 'begin',
  31444. ' Func:=function(c:double):word async begin',
  31445. ' Result:=await(Crawl(c));',
  31446. ' end;',
  31447. ' Func:=function(c:double):word async assembler asm',
  31448. ' end;',
  31449. ' ']);
  31450. ConvertProgram;
  31451. CheckSource('TestAsync_AnonymousProc',
  31452. LinesToStr([ // statements
  31453. 'this.Crawl = async function (d) {',
  31454. ' var Result = 0;',
  31455. ' return Result;',
  31456. '};',
  31457. 'this.Func = null;',
  31458. '']),
  31459. LinesToStr([
  31460. '$mod.Func = async function (c) {',
  31461. ' var Result = 0;',
  31462. ' Result = await $mod.Crawl(c);',
  31463. ' return Result;',
  31464. '};',
  31465. '$mod.Func = async function (c) {',
  31466. '};',
  31467. '']));
  31468. end;
  31469. procedure TTestModule.TestAsync_ProcType;
  31470. begin
  31471. StartProgram(false);
  31472. Add([
  31473. '{$mode objfpc}',
  31474. 'type',
  31475. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  31476. ' TFunc = function(x: double = 1.1): word; async;',
  31477. ' TProc = procedure(x: longint = 7); async;',
  31478. 'function Crawl(d: double): word; async;',
  31479. 'begin',
  31480. 'end;',
  31481. 'procedure Run(e:longint); async;',
  31482. 'begin',
  31483. 'end;',
  31484. 'var',
  31485. ' RefFunc: TRefFunc;',
  31486. ' Func: TFunc;',
  31487. ' Proc, ProcB: TProc;',
  31488. 'begin',
  31489. ' Func:=@Crawl;',
  31490. ' RefFunc:=@Crawl;',
  31491. ' RefFunc:=function(c:double):word async begin',
  31492. ' Result:=await(RefFunc);',
  31493. ' Result:=await(RefFunc());',
  31494. ' Result:=await(Func);',
  31495. ' Result:=await(Func());',
  31496. ' await(Proc);',
  31497. ' await(Proc());',
  31498. ' await(Proc(13));',
  31499. ' end;',
  31500. ' Proc:=@Run;',
  31501. ' if Proc=ProcB then ;',
  31502. ' ']);
  31503. ConvertProgram;
  31504. CheckSource('TestAsync_ProcType',
  31505. LinesToStr([ // statements
  31506. 'this.Crawl = async function (d) {',
  31507. ' var Result = 0;',
  31508. ' return Result;',
  31509. '};',
  31510. 'this.Run = async function (e) {',
  31511. '};',
  31512. 'this.RefFunc = null;',
  31513. 'this.Func = null;',
  31514. 'this.Proc = null;',
  31515. 'this.ProcB = null;',
  31516. '']),
  31517. LinesToStr([
  31518. '$mod.Func = $mod.Crawl;',
  31519. '$mod.RefFunc = $mod.Crawl;',
  31520. '$mod.RefFunc = async function (c) {',
  31521. ' var Result = 0;',
  31522. ' Result = await $mod.RefFunc(1.3);',
  31523. ' Result = await $mod.RefFunc(1.3);',
  31524. ' Result = await $mod.Func(1.1);',
  31525. ' Result = await $mod.Func(1.1);',
  31526. ' await $mod.Proc(7);',
  31527. ' await $mod.Proc(7);',
  31528. ' await $mod.Proc(13);',
  31529. ' return Result;',
  31530. '};',
  31531. '$mod.Proc = $mod.Run;',
  31532. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  31533. '']));
  31534. end;
  31535. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  31536. begin
  31537. StartProgram(false);
  31538. Add([
  31539. '{$mode objfpc}',
  31540. 'type',
  31541. ' TRefFunc = reference to function(x: double = 1.3): word;',
  31542. 'function Crawl(d: double): word; async;',
  31543. 'begin',
  31544. 'end;',
  31545. 'var',
  31546. ' RefFunc: TRefFunc;',
  31547. 'begin',
  31548. ' RefFunc:=@Crawl;',
  31549. ' ']);
  31550. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  31551. ConvertProgram;
  31552. end;
  31553. procedure TTestModule.TestAsync_Inherited;
  31554. begin
  31555. StartProgram(false);
  31556. Add([
  31557. '{$mode objfpc}',
  31558. '{$modeswitch externalclass}',
  31559. 'type',
  31560. ' TJSPromise = class external name ''Promise''',
  31561. ' end;',
  31562. ' TObject = class',
  31563. ' function Run(w: word = 3): word; async; virtual;',
  31564. ' end;',
  31565. ' TBird = class',
  31566. ' function Run(w: word = 3): word; async; override;',
  31567. ' end;',
  31568. 'function TObject.Run(w: word = 3): word; async;',
  31569. 'begin',
  31570. 'end;',
  31571. 'function TBird.Run(w: word = 3): word; async;',
  31572. 'var p: TJSPromise;',
  31573. 'begin',
  31574. ' p:=inherited;',
  31575. ' p:=inherited Run;',
  31576. ' p:=inherited Run();',
  31577. ' p:=inherited Run(4);',
  31578. ' exit(p);',
  31579. ' exit(inherited);',
  31580. ' exit(inherited Run);',
  31581. ' exit(inherited Run(5));',
  31582. ' exit(6);',
  31583. 'end;',
  31584. 'begin',
  31585. ' ']);
  31586. ConvertProgram;
  31587. CheckSource('TestAsync_Inherited',
  31588. LinesToStr([ // statements
  31589. 'rtl.createClass($mod, "TObject", null, function () {',
  31590. ' this.$init = function () {',
  31591. ' };',
  31592. ' this.$final = function () {',
  31593. ' };',
  31594. ' this.Run = async function (w) {',
  31595. ' var Result = 0;',
  31596. ' return Result;',
  31597. ' };',
  31598. '});',
  31599. 'rtl.createClass($mod, "TBird", $mod.TObject, function () {',
  31600. ' this.Run = async function (w) {',
  31601. ' var Result = 0;',
  31602. ' var p = null;',
  31603. ' p = $mod.TObject.Run.apply(this, arguments);',
  31604. ' p = $mod.TObject.Run.call(this, 3);',
  31605. ' p = $mod.TObject.Run.call(this, 3);',
  31606. ' p = $mod.TObject.Run.call(this, 4);',
  31607. ' return p;',
  31608. ' return $mod.TObject.Run.apply(this, arguments);',
  31609. ' return $mod.TObject.Run.call(this, 3);',
  31610. ' return $mod.TObject.Run.call(this, 5);',
  31611. ' return 6;',
  31612. ' return Result;',
  31613. ' };',
  31614. '});',
  31615. '']),
  31616. LinesToStr([
  31617. '']));
  31618. end;
  31619. Initialization
  31620. RegisterTests([TTestModule]);
  31621. end.