tcmodules.pas 895 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140
  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. {$Optimization }
  18. interface
  19. uses
  20. Classes, SysUtils, fpcunit, testregistry, contnrs,
  21. jstree, jswriter, jsbase,
  22. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  23. FPPas2Js;
  24. const
  25. // default parser+scanner options
  26. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  27. co_tcmodules = [];
  28. type
  29. TSrcMarkerKind = (
  30. mkLabel,
  31. mkResolverReference,
  32. mkDirectReference
  33. );
  34. PSrcMarker = ^TSrcMarker;
  35. TSrcMarker = record
  36. Kind: TSrcMarkerKind;
  37. Filename: string;
  38. Row: integer;
  39. StartCol, EndCol: integer; // token start, end column
  40. Identifier: string;
  41. Next: PSrcMarker;
  42. end;
  43. TSystemUnitPart = (
  44. supTObject,
  45. supTVarRec,
  46. supTypeInfo,
  47. supTInterfacedObject,
  48. supWriteln
  49. );
  50. TSystemUnitParts = set of TSystemUnitPart;
  51. { TTestHintMessage }
  52. TTestHintMessage = class
  53. public
  54. Id: int64;
  55. MsgType: TMessageType;
  56. MsgNumber: integer;
  57. Msg: string;
  58. SourcePos: TPasSourcePos;
  59. end;
  60. { TTestPasParser }
  61. TTestPasParser = Class(TPasParser)
  62. end;
  63. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  64. { TTestEnginePasResolver }
  65. TTestEnginePasResolver = class(TPas2JsResolver)
  66. private
  67. FFilename: string;
  68. FModule: TPasModule;
  69. FOnFindUnit: TOnFindUnit;
  70. FParser: TTestPasParser;
  71. FStreamResolver: TStreamResolver;
  72. FScanner: TPas2jsPasScanner;
  73. FSource: string;
  74. public
  75. destructor Destroy; override;
  76. function FindUnit(const AName, InFilename: String; NameExpr,
  77. InFileExpr: TPasExpr): TPasModule; override;
  78. procedure UsedInterfacesFinished(Section: TPasSection); override;
  79. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  80. property Filename: string read FFilename write FFilename;
  81. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  82. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  83. property Parser: TTestPasParser read FParser write FParser;
  84. property Source: string read FSource write FSource;
  85. property Module: TPasModule read FModule;
  86. end;
  87. { TCustomTestModule }
  88. TCustomTestModule = Class(TTestCase)
  89. private
  90. FConverter: TPasToJSConverter;
  91. FEngine: TTestEnginePasResolver;
  92. FExpectedErrorClass: ExceptClass;
  93. FExpectedErrorMsg: string;
  94. FExpectedErrorNumber: integer;
  95. FFilename: string;
  96. FFileResolver: TStreamResolver;
  97. FHub: TPas2JSResolverHub;
  98. FJSImplementationUses: TJSArrayLiteral;
  99. FJSInitBody: TJSFunctionBody;
  100. FJSImplentationUses: TJSArrayLiteral;
  101. FJSInterfaceUses: TJSArrayLiteral;
  102. FJSModule: TJSSourceElements;
  103. FJSModuleSrc: TJSSourceElements;
  104. FJSSource: TStringList;
  105. FModule: TPasModule;
  106. FJSModuleCallArgs: TJSArguments;
  107. FModules: TObjectList;// list of TTestEnginePasResolver
  108. FParser: TTestPasParser;
  109. FPasProgram: TPasProgram;
  110. FPasLibrary: TPasLibrary;
  111. FHintMsgs: TObjectList; // list of TTestHintMessage
  112. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  113. FJSRegModuleCall: TJSCallExpression;
  114. FScanner: TPas2jsPasScanner;
  115. FSkipTests: boolean;
  116. FSource: TStringList;
  117. FFirstPasStatement: TPasImplBlock;
  118. FWithTypeInfo: boolean;
  119. {$IFDEF EnablePasTreeGlobalRefCount}
  120. FElementRefCountAtSetup: int64;
  121. {$ENDIF}
  122. function GetMsgCount: integer;
  123. function GetMsgs(Index: integer): TTestHintMessage;
  124. function GetResolverCount: integer;
  125. function GetResolvers(Index: integer): TTestEnginePasResolver;
  126. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  127. procedure OnParserLog(Sender: TObject; const Msg: String);
  128. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  129. procedure OnScannerLog(Sender: TObject; const Msg: String);
  130. procedure SetWithTypeInfo(const AValue: boolean);
  131. protected
  132. procedure SetUp; override;
  133. function CreateConverter: TPasToJSConverter; virtual;
  134. function LoadUnit(const aUnitName: String): TPasModule;
  135. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  136. procedure TearDown; override;
  137. Procedure Add(Line: string); virtual;
  138. Procedure Add(const Lines: array of string);
  139. Procedure StartParsing; virtual;
  140. procedure ParseModuleQueue; virtual;
  141. procedure ParseModule; virtual;
  142. procedure ParseProgram; virtual;
  143. procedure ParseLibrary; virtual;
  144. procedure ParseUnit; virtual;
  145. protected
  146. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  147. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  148. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  149. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  150. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  151. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  152. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  153. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  154. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  155. procedure ConvertModule; virtual;
  156. procedure ConvertProgram; virtual;
  157. procedure ConvertLibrary; virtual;
  158. procedure ConvertUnit; virtual;
  159. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  160. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  161. function GetDottedIdentifier(El: TJSElement): string;
  162. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  163. ImplStatements: string = ''); virtual;
  164. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  165. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  166. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  167. Msg: string; Marker: PSrcMarker = nil); virtual;
  168. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  169. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  170. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  171. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  172. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  173. function IsErrorExpected(E: Exception): boolean;
  174. procedure HandleScannerError(E: EScannerError);
  175. procedure HandleParserError(E: EParserError);
  176. procedure HandlePasResolveError(E: EPasResolve);
  177. procedure HandlePas2JSError(E: EPas2JS);
  178. procedure HandleException(E: Exception);
  179. procedure FailException(E: Exception);
  180. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  181. function IndexOfResolver(const Filename: string): integer;
  182. function GetResolver(const Filename: string): TTestEnginePasResolver;
  183. function GetDefaultNamespace: string;
  184. property PasProgram: TPasProgram Read FPasProgram;
  185. property PasLibrary: TPasLibrary Read FPasLibrary;
  186. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  187. property ResolverCount: integer read GetResolverCount;
  188. property Engine: TTestEnginePasResolver read FEngine;
  189. property Filename: string read FFilename;
  190. Property Module: TPasModule Read FModule;
  191. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  192. property Converter: TPasToJSConverter read FConverter;
  193. property JSSource: TStringList read FJSSource;
  194. property JSModule: TJSSourceElements read FJSModule;
  195. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  196. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  197. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  198. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  199. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  200. property JSInitBody: TJSFunctionBody read FJSInitBody;
  201. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  202. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  203. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  204. property SkipTests: boolean read FSkipTests write FSkipTests;
  205. public
  206. constructor Create; override;
  207. destructor Destroy; override;
  208. property Hub: TPas2JSResolverHub read FHub;
  209. property Source: TStringList read FSource;
  210. property FileResolver: TStreamResolver read FFileResolver;
  211. property Scanner: TPas2jsPasScanner read FScanner;
  212. property Parser: TTestPasParser read FParser;
  213. property MsgCount: integer read GetMsgCount;
  214. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  215. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  216. end;
  217. { TTestModule }
  218. TTestModule = class(TCustomTestModule)
  219. Published
  220. Procedure TestReservedWords;
  221. // program, units, includes
  222. Procedure TestEmptyProgram;
  223. Procedure TestEmptyProgramUseStrict;
  224. Procedure TestEmptyUnit;
  225. Procedure TestEmptyUnitUseStrict;
  226. Procedure TestDottedUnitNames;
  227. Procedure TestDottedUnitNameImpl;
  228. Procedure TestDottedUnitExpr;
  229. Procedure Test_ModeFPCFail;
  230. Procedure Test_ModeSwitchCBlocksFail;
  231. Procedure TestUnit_UseSystem;
  232. Procedure TestUnit_Intf1Impl2Intf1;
  233. Procedure TestIncludeVersion;
  234. // vars/const
  235. Procedure TestVarInt;
  236. Procedure TestVarBaseTypes;
  237. Procedure TestBaseTypeSingleFail;
  238. Procedure TestBaseTypeExtendedFail;
  239. Procedure TestConstBaseTypes;
  240. Procedure TestUnitImplVars;
  241. Procedure TestUnitImplConsts;
  242. Procedure TestUnitImplRecord;
  243. Procedure TestRenameJSNameConflict;
  244. Procedure TestLocalConst;
  245. Procedure TestVarExternal;
  246. Procedure TestVarExternalOtherUnit;
  247. Procedure TestVarAbsoluteFail;
  248. Procedure TestConstExternal;
  249. // numbers
  250. Procedure TestDouble;
  251. Procedure TestInteger;
  252. Procedure TestIntegerRange;
  253. Procedure TestIntegerTypecasts;
  254. Procedure TestInteger_BitwiseShrNativeInt;
  255. Procedure TestInteger_BitwiseShlNativeInt;
  256. Procedure TestInteger_SystemFunc;
  257. Procedure TestInteger_AssignOutsideConst;
  258. Procedure TestCurrency;
  259. Procedure TestForBoolDo;
  260. Procedure TestForIntDo;
  261. Procedure TestForIntInDo;
  262. // strings
  263. Procedure TestCharConst;
  264. Procedure TestChar_Compare;
  265. Procedure TestChar_BuiltInProcs;
  266. Procedure TestStringConst;
  267. Procedure TestStringConst_InvalidUTF16;
  268. Procedure TestStringConstSurrogate;
  269. Procedure TestString_Length;
  270. Procedure TestString_Compare;
  271. Procedure TestString_SetLength;
  272. Procedure TestString_CharAt;
  273. Procedure TestStringHMinusFail;
  274. Procedure TestStr;
  275. Procedure TestBaseType_AnsiStringFail;
  276. Procedure TestBaseType_WideStringFail;
  277. Procedure TestBaseType_ShortStringFail;
  278. Procedure TestBaseType_RawByteStringFail;
  279. Procedure TestTypeShortstring_Fail;
  280. Procedure TestCharSet_Custom;
  281. Procedure TestWideChar;
  282. Procedure TestForCharDo;
  283. Procedure TestForCharInDo;
  284. // alias types
  285. Procedure TestAliasTypeRef;
  286. Procedure TestTypeCast_BaseTypes;
  287. Procedure TestTypeCast_AliasBaseTypes;
  288. // functions
  289. Procedure TestEmptyProc;
  290. Procedure TestProcOneParam;
  291. Procedure TestFunctionWithoutParams;
  292. Procedure TestProcedureWithoutParams;
  293. Procedure TestPrgProcVar;
  294. Procedure TestProcTwoArgs;
  295. Procedure TestProc_DefaultValue;
  296. Procedure TestUnitProcVar;
  297. Procedure TestImplProc;
  298. Procedure TestFunctionResult;
  299. Procedure TestNestedProc;
  300. Procedure TestNestedProc_ResultString;
  301. Procedure TestForwardProc;
  302. Procedure TestNestedForwardProc;
  303. Procedure TestAssignFunctionResult;
  304. Procedure TestFunctionResultInCondition;
  305. Procedure TestFunctionResultInForLoop;
  306. Procedure TestFunctionResultInTypeCast;
  307. Procedure TestExit;
  308. Procedure TestExit_ResultInFinally;
  309. Procedure TestBreak;
  310. Procedure TestBreakAsVar;
  311. Procedure TestContinue;
  312. Procedure TestProc_External;
  313. Procedure TestProc_ExternalOtherUnit;
  314. Procedure TestProc_Asm;
  315. Procedure TestProc_AsmSubBlock;
  316. Procedure TestProc_Assembler;
  317. Procedure TestProc_VarParam;
  318. Procedure TestProc_VarParamString;
  319. Procedure TestProc_VarParamV;
  320. Procedure TestProc_Overload;
  321. Procedure TestProc_OverloadForward;
  322. Procedure TestProc_OverloadIntfImpl;
  323. Procedure TestProc_OverloadNested;
  324. Procedure TestProc_OverloadNestedForward;
  325. Procedure TestProc_OverloadUnitCycle;
  326. Procedure TestProc_Varargs;
  327. Procedure TestProc_ConstOrder;
  328. Procedure TestProc_DuplicateConst;
  329. Procedure TestProc_LocalVarAbsolute;
  330. Procedure TestProc_LocalVarInit;
  331. Procedure TestProc_ReservedWords;
  332. Procedure TestProc_ConstRefWord;
  333. // anonymous functions
  334. Procedure TestAnonymousProc_Assign_ObjFPC;
  335. Procedure TestAnonymousProc_Assign_Delphi;
  336. Procedure TestAnonymousProc_Arg;
  337. Procedure TestAnonymousProc_Typecast;
  338. Procedure TestAnonymousProc_With;
  339. Procedure TestAnonymousProc_ExceptOn;
  340. Procedure TestAnonymousProc_Nested;
  341. Procedure TestAnonymousProc_NestedAssignResult;
  342. Procedure TestAnonymousProc_Class;
  343. Procedure TestAnonymousProc_ForLoop;
  344. Procedure TestAnonymousProc_AsmDelphi;
  345. // enums, sets
  346. Procedure TestEnum_Name;
  347. Procedure TestEnum_Number;
  348. Procedure TestEnum_ConstFail;
  349. Procedure TestEnum_Functions;
  350. Procedure TestEnumRg_Functions;
  351. Procedure TestEnum_AsParams;
  352. Procedure TestEnumRange_Array;
  353. Procedure TestEnum_ForIn;
  354. Procedure TestEnum_ScopedNumber;
  355. Procedure TestEnum_InFunction;
  356. Procedure TestEnum_Name_Anonymous_Unit;
  357. Procedure TestSet_Enum;
  358. Procedure TestSet_Operators;
  359. Procedure TestSet_Operator_In;
  360. Procedure TestSet_Functions;
  361. Procedure TestSet_PassAsArgClone;
  362. Procedure TestSet_AsParams;
  363. Procedure TestSet_Property;
  364. Procedure TestSet_EnumConst;
  365. Procedure TestSet_IntConst;
  366. Procedure TestSet_IntRange;
  367. Procedure TestSet_AnonymousEnumType;
  368. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  369. Procedure TestSet_ConstEnum;
  370. Procedure TestSet_ConstChar;
  371. Procedure TestSet_ConstInt;
  372. Procedure TestSet_InFunction;
  373. Procedure TestSet_ForIn;
  374. // statements
  375. Procedure TestNestBegin;
  376. Procedure TestIncDec;
  377. Procedure TestLoHiFpcMode;
  378. Procedure TestLoHiDelphiMode;
  379. Procedure TestAssignments;
  380. Procedure TestArithmeticOperators1;
  381. Procedure TestMultiAdd;
  382. Procedure TestLogicalOperators;
  383. Procedure TestBitwiseOperators;
  384. Procedure TestBitwiseOperatorsLongword;
  385. Procedure TestFunctionInt;
  386. Procedure TestFunctionString;
  387. Procedure TestIfThen;
  388. Procedure TestForLoop;
  389. Procedure TestForLoopInsideFunction;
  390. Procedure TestForLoop_ReadVarAfter;
  391. Procedure TestForLoop_Nested;
  392. Procedure TestRepeatUntil;
  393. Procedure TestAsmBlock;
  394. Procedure TestAsmPas_Impl; // ToDo
  395. Procedure TestTryFinally;
  396. Procedure TestTryExcept;
  397. Procedure TestTryExcept_ReservedWords;
  398. Procedure TestIfThenRaiseElse;
  399. Procedure TestCaseOf;
  400. Procedure TestCaseOf_UseSwitch;
  401. Procedure TestCaseOfNoElse;
  402. Procedure TestCaseOfNoElse_UseSwitch;
  403. Procedure TestCaseOfRange;
  404. Procedure TestCaseOfString;
  405. Procedure TestCaseOfChar;
  406. Procedure TestCaseOfExternalClassConst;
  407. Procedure TestDebugger;
  408. // arrays
  409. Procedure TestArray_Dynamic;
  410. Procedure TestArray_Dynamic_Nil;
  411. Procedure TestArray_DynMultiDimensional;
  412. Procedure TestArray_DynamicAssign;
  413. Procedure TestArray_StaticInt;
  414. Procedure TestArray_StaticBool;
  415. Procedure TestArray_StaticChar;
  416. Procedure TestArray_StaticMultiDim;
  417. Procedure TestArray_StaticInFunction;
  418. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  419. Procedure TestArrayOfRecord;
  420. Procedure TestArray_StaticRecord;
  421. Procedure TestArrayOfSet;
  422. Procedure TestArray_DynAsParam;
  423. Procedure TestArray_StaticAsParam;
  424. Procedure TestArrayElement_AsParams;
  425. Procedure TestArrayElementFromFuncResult_AsParams;
  426. Procedure TestArrayEnumTypeRange;
  427. Procedure TestArray_SetLengthOutArg;
  428. Procedure TestArray_SetLengthProperty;
  429. Procedure TestArray_SetLengthMultiDim;
  430. Procedure TestArray_SetLengthDynOfStatic;
  431. Procedure TestArray_OpenArrayOfString;
  432. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  433. Procedure TestArray_ConstRef;
  434. Procedure TestArray_Concat;
  435. Procedure TestArray_Copy;
  436. Procedure TestArray_InsertDelete;
  437. Procedure TestArray_DynArrayConstObjFPC;
  438. Procedure TestArray_DynArrayConstDelphi;
  439. Procedure TestArray_ArrayLitAsParam;
  440. Procedure TestArray_ArrayLitMultiDimAsParam;
  441. Procedure TestArray_ArrayLitStaticAsParam;
  442. Procedure TestArray_ForInArrOfString;
  443. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  444. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  445. Procedure TestArrayOfConst_TVarRec;
  446. Procedure TestArrayOfConst_PassBaseTypes;
  447. Procedure TestArrayOfConst_PassObj;
  448. // record
  449. Procedure TestRecord_Empty;
  450. Procedure TestRecord_Var;
  451. Procedure TestRecord_VarExternal;
  452. Procedure TestRecord_WithDo;
  453. Procedure TestRecord_Assign;
  454. Procedure TestRecord_AsParams;
  455. Procedure TestRecord_ConstRef;
  456. Procedure TestRecordElement_AsParams;
  457. Procedure TestRecordElementFromFuncResult_AsParams;
  458. Procedure TestRecordElementFromWith_AsParams;
  459. Procedure TestRecord_Equal;
  460. Procedure TestRecord_JSValue;
  461. Procedure TestRecord_VariantFail;
  462. Procedure TestRecord_FieldArray;
  463. Procedure TestRecord_Const;
  464. Procedure TestRecord_TypecastFail;
  465. Procedure TestRecord_InFunction;
  466. Procedure TestRecord_AnonymousFail;
  467. // advanced record
  468. Procedure TestAdvRecord_Function;
  469. Procedure TestAdvRecord_Property;
  470. Procedure TestAdvRecord_PropertyDefault;
  471. Procedure TestAdvRecord_Property_ClassMethod;
  472. Procedure TestAdvRecord_Const;
  473. Procedure TestAdvRecord_ExternalField;
  474. Procedure TestAdvRecord_SubRecord;
  475. Procedure TestAdvRecord_SubClass;
  476. Procedure TestAdvRecord_SubInterfaceFail;
  477. Procedure TestAdvRecord_Constructor;
  478. Procedure TestAdvRecord_ClassConstructor_Program;
  479. Procedure TestAdvRecord_ClassConstructor_Unit;
  480. // classes
  481. Procedure TestClass_TObjectDefaultConstructor;
  482. Procedure TestClass_TObjectConstructorWithParams;
  483. Procedure TestClass_TObjectConstructorWithDefaultParam;
  484. Procedure TestClass_Var;
  485. Procedure TestClass_Method;
  486. Procedure TestClass_Implementation;
  487. Procedure TestClass_Inheritance;
  488. Procedure TestClass_TypeAlias;
  489. Procedure TestClass_AbstractMethod;
  490. Procedure TestClass_CallInherited_ProcNoParams;
  491. Procedure TestClass_CallInherited_WithParams;
  492. Procedure TestClasS_CallInheritedConstructor;
  493. Procedure TestClass_ClassVar_Assign;
  494. Procedure TestClass_CallClassMethod;
  495. Procedure TestClass_CallClassMethodStatic;
  496. Procedure TestClass_Property;
  497. Procedure TestClass_Property_ClassMethod;
  498. Procedure TestClass_Property_ClassMethodStatic;
  499. Procedure TestClass_Property_Indexed;
  500. Procedure TestClass_Property_IndexSpec;
  501. Procedure TestClass_PropertyOfTypeArray;
  502. Procedure TestClass_PropertyDefault;
  503. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  504. //Procedure TestClass_PropertyDefault;
  505. Procedure TestClass_PropertyOverride;
  506. Procedure TestClass_PropertyIncVisibility;
  507. Procedure TestClass_Assigned;
  508. Procedure TestClass_WithClassDoCreate;
  509. Procedure TestClass_WithClassInstDoProperty;
  510. Procedure TestClass_WithClassInstDoPropertyWithParams;
  511. Procedure TestClass_WithClassInstDoFunc;
  512. Procedure TestClass_TypeCast;
  513. Procedure TestClass_TypeCastUntypedParam;
  514. Procedure TestClass_Overloads;
  515. Procedure TestClass_OverloadsAncestor;
  516. Procedure TestClass_OverloadConstructor;
  517. Procedure TestClass_OverloadDelphiOverride;
  518. Procedure TestClass_ReintroduceVarDelphi;
  519. Procedure TestClass_ReintroducedVar;
  520. Procedure TestClass_RaiseDescendant;
  521. Procedure TestClass_ExternalMethod;
  522. Procedure TestClass_ExternalVirtualNameMismatchFail;
  523. Procedure TestClass_ExternalOverrideFail;
  524. Procedure TestClass_ExternalVar;
  525. Procedure TestClass_Const;
  526. Procedure TestClass_ConstEnum;
  527. Procedure TestClass_LocalConstDuplicate_Prg;
  528. Procedure TestClass_LocalConstDuplicate_Unit;
  529. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  530. Procedure TestClass_LocalVarSelfFail;
  531. Procedure TestClass_ArgSelfFail;
  532. Procedure TestClass_NestedProcSelf;
  533. Procedure TestClass_NestedProcSelf2;
  534. Procedure TestClass_NestedProcClassSelf;
  535. Procedure TestClass_NestedProcCallInherited;
  536. Procedure TestClass_TObjectFree;
  537. Procedure TestClass_TObjectFree_VarArg;
  538. Procedure TestClass_TObjectFreeNewInstance;
  539. Procedure TestClass_TObjectFreeLowerCase;
  540. Procedure TestClass_TObjectFreeFunctionFail;
  541. Procedure TestClass_TObjectFreePropertyFail;
  542. Procedure TestClass_ForIn;
  543. Procedure TestClass_DispatchMessage;
  544. Procedure TestClass_Message_DuplicateIntFail;
  545. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  546. // class of
  547. Procedure TestClassOf_Create;
  548. Procedure TestClassOf_Call;
  549. Procedure TestClassOf_Assign;
  550. Procedure TestClassOf_Is;
  551. Procedure TestClassOf_Compare;
  552. Procedure TestClassOf_ClassVar;
  553. Procedure TestClassOf_ClassMethod;
  554. Procedure TestClassOf_ClassProperty;
  555. Procedure TestClassOf_ClassMethodSelf;
  556. Procedure TestClassOf_TypeCast;
  557. Procedure TestClassOf_ImplicitFunctionCall;
  558. Procedure TestClassOf_Const;
  559. // nested class
  560. Procedure TestNestedClass_Alias;
  561. Procedure TestNestedClass_Record;
  562. Procedure TestNestedClass_Class;
  563. // external class
  564. Procedure TestExternalClass_Var;
  565. Procedure TestExternalClass_Const;
  566. Procedure TestExternalClass_Dollar;
  567. Procedure TestExternalClass_DuplicateVarFail;
  568. Procedure TestExternalClass_Method;
  569. Procedure TestExternalClass_ClassMethod;
  570. Procedure TestExternalClass_ClassMethodStatic;
  571. Procedure TestExternalClass_FunctionResultInTypeCast;
  572. Procedure TestExternalClass_NonExternalOverride;
  573. Procedure TestExternalClass_OverloadHint;
  574. Procedure TestExternalClass_SameNamePublishedProperty;
  575. Procedure TestExternalClass_Property;
  576. Procedure TestExternalClass_PropertyDate;
  577. Procedure TestExternalClass_ClassProperty;
  578. Procedure TestExternalClass_ClassOf;
  579. Procedure TestExternalClass_ClassOtherUnit;
  580. Procedure TestExternalClass_Is;
  581. Procedure TestExternalClass_As;
  582. Procedure TestExternalClass_DestructorFail;
  583. Procedure TestExternalClass_New;
  584. Procedure TestExternalClass_ClassOf_New;
  585. Procedure TestExternalClass_FuncClassOf_New;
  586. Procedure TestExternalClass_New_PasClassFail;
  587. Procedure TestExternalClass_New_PasClassBracketsFail;
  588. Procedure TestExternalClass_NewExtName;
  589. Procedure TestExternalClass_Constructor;
  590. Procedure TestExternalClass_ConstructorBrackets;
  591. Procedure TestExternalClass_LocalConstSameName;
  592. Procedure TestExternalClass_ReintroduceOverload;
  593. Procedure TestExternalClass_Inherited;
  594. Procedure TestExternalClass_PascalAncestorFail;
  595. Procedure TestExternalClass_NewInstance;
  596. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  597. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  598. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  599. Procedure TestExternalClass_JSFunctionPasDescendant;
  600. Procedure TestExternalClass_PascalProperty;
  601. Procedure TestExternalClass_TypeCastToRootClass;
  602. Procedure TestExternalClass_TypeCastToJSObject;
  603. Procedure TestExternalClass_TypeCastStringToExternalString;
  604. Procedure TestExternalClass_TypeCastToJSFunction;
  605. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  606. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  607. Procedure TestExternalClass_BracketAccessor;
  608. Procedure TestExternalClass_BracketAccessor_Call;
  609. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  610. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  611. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  612. Procedure TestExternalClass_BracketAccessor_MultiType;
  613. Procedure TestExternalClass_BracketAccessor_Index;
  614. Procedure TestExternalClass_ForInJSObject;
  615. Procedure TestExternalClass_ForInJSArray;
  616. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  617. // class interfaces
  618. Procedure TestClassInterface_Corba;
  619. Procedure TestClassInterface_ProcExternalFail;
  620. Procedure TestClassInterface_Overloads;
  621. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  622. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  623. Procedure TestClassInterface_AncestorImpl;
  624. Procedure TestClassInterface_ImplReintroduce;
  625. Procedure TestClassInterface_MethodResolution;
  626. Procedure TestClassInterface_AncestorMoreInterfaces;
  627. Procedure TestClassInterface_MethodOverride;
  628. Procedure TestClassInterface_Corba_Delegation;
  629. Procedure TestClassInterface_Corba_DelegationStatic;
  630. Procedure TestClassInterface_Corba_Operators;
  631. Procedure TestClassInterface_Corba_Args;
  632. Procedure TestClassInterface_Corba_ForIn;
  633. Procedure TestClassInterface_COM_AssignVar;
  634. Procedure TestClassInterface_COM_AssignArg;
  635. Procedure TestClassInterface_COM_FunctionResult;
  636. Procedure TestClassInterface_COM_InheritedFuncResult;
  637. Procedure TestClassInterface_COM_IsAsTypeCasts;
  638. Procedure TestClassInterface_COM_PassAsArg;
  639. Procedure TestClassInterface_COM_PassToUntypedParam;
  640. Procedure TestClassInterface_COM_FunctionInExpr;
  641. Procedure TestClassInterface_COM_Property;
  642. Procedure TestClassInterface_COM_IntfProperty;
  643. Procedure TestClassInterface_COM_Delegation;
  644. Procedure TestClassInterface_COM_With;
  645. Procedure TestClassInterface_COM_ForIn;
  646. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  647. Procedure TestClassInterface_COM_RecordIntfFail;
  648. Procedure TestClassInterface_COM_UnitInitialization;
  649. Procedure TestClassInterface_GUID;
  650. Procedure TestClassInterface_GUIDProperty;
  651. // helpers
  652. Procedure TestClassHelper_ClassVar;
  653. Procedure TestClassHelper_Method_AccessInstanceFields;
  654. Procedure TestClassHelper_Method_Call;
  655. Procedure TestClassHelper_Method_Nested_Call;
  656. Procedure TestClassHelper_ClassMethod_Call;
  657. Procedure TestClassHelper_ClassOf;
  658. Procedure TestClassHelper_MethodRefObjFPC;
  659. Procedure TestClassHelper_Constructor;
  660. Procedure TestClassHelper_InheritedObjFPC;
  661. Procedure TestClassHelper_Property;
  662. Procedure TestClassHelper_Property_Array;
  663. Procedure TestClassHelper_Property_Array_Default;
  664. Procedure TestClassHelper_Property_Array_DefaultDefault;
  665. Procedure TestClassHelper_ClassProperty;
  666. Procedure TestClassHelper_ClassPropertyStatic;
  667. Procedure TestClassHelper_ClassProperty_Array;
  668. Procedure TestClassHelper_ForIn;
  669. Procedure TestClassHelper_PassProperty;
  670. Procedure TestExtClassHelper_ClassVar;
  671. Procedure TestExtClassHelper_Method_Call;
  672. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  673. Procedure TestRecordHelper_ClassVar;
  674. Procedure TestRecordHelper_Method_Call;
  675. Procedure TestRecordHelper_Constructor;
  676. Procedure TestTypeHelper_ClassVar;
  677. Procedure TestTypeHelper_PassResultElement;
  678. Procedure TestTypeHelper_PassArgs;
  679. Procedure TestTypeHelper_PassVarConst;
  680. Procedure TestTypeHelper_PassFuncResult;
  681. Procedure TestTypeHelper_PassPropertyField;
  682. Procedure TestTypeHelper_PassPropertyGetter;
  683. Procedure TestTypeHelper_PassClassPropertyField;
  684. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  685. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  686. Procedure TestTypeHelper_Property;
  687. Procedure TestTypeHelper_Property_Array;
  688. Procedure TestTypeHelper_ClassProperty;
  689. Procedure TestTypeHelper_ClassProperty_Array;
  690. Procedure TestTypeHelper_ClassMethod;
  691. Procedure TestTypeHelper_ExtClassMethodFail;
  692. Procedure TestTypeHelper_Constructor;
  693. Procedure TestTypeHelper_Word;
  694. Procedure TestTypeHelper_Boolean;
  695. Procedure TestTypeHelper_WordBool;
  696. Procedure TestTypeHelper_Double;
  697. Procedure TestTypeHelper_NativeInt;
  698. Procedure TestTypeHelper_StringChar;
  699. Procedure TestTypeHelper_JSValue;
  700. Procedure TestTypeHelper_Array;
  701. Procedure TestTypeHelper_EnumType;
  702. Procedure TestTypeHelper_SetType;
  703. Procedure TestTypeHelper_InterfaceType;
  704. Procedure TestTypeHelper_NestedSelf;
  705. // proc types
  706. Procedure TestProcType;
  707. Procedure TestProcType_Arg;
  708. Procedure TestProcType_FunctionFPC;
  709. Procedure TestProcType_FunctionDelphi;
  710. Procedure TestProcType_ProcedureDelphi;
  711. Procedure TestProcType_AsParam;
  712. Procedure TestProcType_MethodFPC;
  713. Procedure TestProcType_MethodDelphi;
  714. Procedure TestProcType_PropertyFPC;
  715. Procedure TestProcType_PropertyDelphi;
  716. Procedure TestProcType_WithClassInstDoPropertyFPC;
  717. Procedure TestProcType_Nested;
  718. Procedure TestProcType_NestedOfObject;
  719. Procedure TestProcType_ReferenceToProc;
  720. Procedure TestProcType_ReferenceToMethod;
  721. Procedure TestProcType_Typecast;
  722. Procedure TestProcType_PassProcToUntyped;
  723. Procedure TestProcType_PassProcToArray;
  724. Procedure TestProcType_SafeCallObjFPC;
  725. Procedure TestProcType_SafeCallDelphi;
  726. // pointer
  727. Procedure TestPointer;
  728. Procedure TestPointer_Proc;
  729. Procedure TestPointer_AssignRecordFail;
  730. Procedure TestPointer_AssignStaticArrayFail;
  731. Procedure TestPointer_TypeCastJSValueToPointer;
  732. Procedure TestPointer_NonRecordFail;
  733. Procedure TestPointer_AnonymousArgTypeFail;
  734. Procedure TestPointer_AnonymousVarTypeFail;
  735. Procedure TestPointer_AnonymousResultTypeFail;
  736. Procedure TestPointer_AddrOperatorFail;
  737. Procedure TestPointer_ArrayParamsFail;
  738. Procedure TestPointer_PointerAddFail;
  739. Procedure TestPointer_IncPointerFail;
  740. Procedure TestPointer_Record;
  741. Procedure TestPointer_RecordArg;
  742. // jsvalue
  743. Procedure TestJSValue_AssignToJSValue;
  744. Procedure TestJSValue_TypeCastToBaseType;
  745. Procedure TestJSValue_TypecastToJSValue;
  746. Procedure TestJSValue_Equal;
  747. Procedure TestJSValue_If;
  748. Procedure TestJSValue_Not;
  749. Procedure TestJSValue_Enum;
  750. Procedure TestJSValue_ClassInstance;
  751. Procedure TestJSValue_ClassOf;
  752. Procedure TestJSValue_ArrayOfJSValue;
  753. Procedure TestJSValue_ArrayLit;
  754. Procedure TestJSValue_Params;
  755. Procedure TestJSValue_UntypedParam;
  756. Procedure TestJSValue_FuncResultType;
  757. Procedure TestJSValue_ProcType_Assign;
  758. Procedure TestJSValue_ProcType_Equal;
  759. Procedure TestJSValue_ProcType_Param;
  760. Procedure TestJSValue_AssignToPointerFail;
  761. Procedure TestJSValue_OverloadDouble;
  762. Procedure TestJSValue_OverloadNativeInt;
  763. Procedure TestJSValue_OverloadWord;
  764. Procedure TestJSValue_OverloadString;
  765. Procedure TestJSValue_OverloadChar;
  766. Procedure TestJSValue_OverloadPointer;
  767. Procedure TestJSValue_ForIn;
  768. // RTTI
  769. Procedure TestRTTI_IntRange;
  770. Procedure TestRTTI_Double;
  771. Procedure TestRTTI_ProcType;
  772. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  773. Procedure TestRTTI_EnumAndSetType;
  774. Procedure TestRTTI_EnumRange;
  775. Procedure TestRTTI_AnonymousEnumType;
  776. Procedure TestRTTI_StaticArray;
  777. Procedure TestRTTI_DynArray;
  778. Procedure TestRTTI_ArrayNestedAnonymous;
  779. Procedure TestRTTI_PublishedMethodOverloadFail;
  780. Procedure TestRTTI_PublishedMethodHideNoHint;
  781. Procedure TestRTTI_PublishedMethodExternalFail;
  782. Procedure TestRTTI_PublishedClassPropertyFail;
  783. Procedure TestRTTI_PublishedClassFieldFail;
  784. Procedure TestRTTI_PublishedFieldExternalFail;
  785. Procedure TestRTTI_Class_Field;
  786. Procedure TestRTTI_Class_Method;
  787. Procedure TestRTTI_Class_MethodArgFlags;
  788. Procedure TestRTTI_Class_Property;
  789. Procedure TestRTTI_Class_PropertyParams;
  790. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  791. Procedure TestRTTI_Class_OmitRTTI;
  792. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  793. Procedure TestRTTI_IndexModifier;
  794. Procedure TestRTTI_StoredModifier;
  795. Procedure TestRTTI_DefaultValue;
  796. Procedure TestRTTI_DefaultValueSet;
  797. Procedure TestRTTI_DefaultValueRangeType;
  798. Procedure TestRTTI_DefaultValueInherit;
  799. Procedure TestRTTI_OverrideMethod;
  800. Procedure TestRTTI_ReintroduceMethod;
  801. Procedure TestRTTI_OverloadProperty;
  802. // ToDo: array argument
  803. Procedure TestRTTI_ClassForward;
  804. Procedure TestRTTI_ClassOf;
  805. Procedure TestRTTI_Record;
  806. Procedure TestRTTI_RecordAnonymousArray;
  807. Procedure TestRTTI_Record_ClassVarType;
  808. Procedure TestRTTI_LocalTypes;
  809. Procedure TestRTTI_TypeInfo_BaseTypes;
  810. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  811. Procedure TestRTTI_TypeInfo_LocalFail;
  812. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  813. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  814. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  815. Procedure TestRTTI_TypeInfo_FunctionClassType;
  816. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  817. Procedure TestRTTI_Interface_Corba;
  818. Procedure TestRTTI_Interface_COM;
  819. Procedure TestRTTI_ClassHelper;
  820. Procedure TestRTTI_ExternalClass;
  821. Procedure TestRTTI_Unit;
  822. // Resourcestring
  823. Procedure TestResourcestringProgram;
  824. Procedure TestResourcestringUnit;
  825. Procedure TestResourcestringImplementation;
  826. // Attributes
  827. Procedure TestAttributes_Members;
  828. Procedure TestAttributes_Types;
  829. Procedure TestAttributes_HelperConstructor_Fail;
  830. Procedure TestAttributes_InterfacesList;
  831. // Assertions, checks
  832. procedure TestAssert;
  833. procedure TestAssert_SysUtils;
  834. procedure TestObjectChecks;
  835. procedure TestOverflowChecks_Int;
  836. procedure TestRangeChecks_AssignInt;
  837. procedure TestRangeChecks_AssignIntRange;
  838. procedure TestRangeChecks_AssignEnum;
  839. procedure TestRangeChecks_AssignEnumRange;
  840. procedure TestRangeChecks_AssignChar;
  841. procedure TestRangeChecks_AssignCharRange;
  842. procedure TestRangeChecks_ArrayIndex;
  843. procedure TestRangeChecks_ArrayOfRecIndex;
  844. procedure TestRangeChecks_StringIndex;
  845. procedure TestRangeChecks_TypecastInt;
  846. procedure TestRangeChecks_TypeHelperInt;
  847. // Async/AWait
  848. Procedure TestAsync_Proc;
  849. Procedure TestAsync_CallResultIsPromise;
  850. Procedure TestAsync_ConstructorFail;
  851. Procedure TestAsync_PropertyGetterFail;
  852. Procedure TestAwait_NonPromiseWithTypeFail;
  853. Procedure TestAwait_AsyncCallTypeMismatch;
  854. Procedure TestAWait_OutsideAsyncFail;
  855. Procedure TestAWait_IntegerFail;
  856. Procedure TestAWait_ExternalClassPromise;
  857. Procedure TestAWait_JSValue;
  858. Procedure TestAWait_Result;
  859. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  860. Procedure TestAsync_AnonymousProc;
  861. Procedure TestAsync_ProcType;
  862. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  863. Procedure TestAsync_Inherited;
  864. Procedure TestAsync_ClassInterface;
  865. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  866. Procedure TestAWait_ClassAs;
  867. // Library
  868. Procedure TestLibrary_Empty;
  869. Procedure TestLibrary_ExportFunc;
  870. Procedure TestLibrary_Export_Index_Fail;
  871. Procedure TestLibrary_ExportVar;
  872. Procedure TestLibrary_ExportUnitFunc;
  873. // ToDo: test delayed specialization init
  874. // ToDo: analyzer
  875. end;
  876. function LinesToStr(Args: array of const): string;
  877. function ExtractFileUnitName(aFilename: string): string;
  878. function JSToStr(El: TJSElement): string;
  879. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  880. implementation
  881. function LinesToStr(Args: array of const): string;
  882. var
  883. s: String;
  884. i: Integer;
  885. begin
  886. s:='';
  887. for i:=Low(Args) to High(Args) do
  888. case Args[i].VType of
  889. vtChar: s += Args[i].VChar+LineEnding;
  890. vtString: s += Args[i].VString^+LineEnding;
  891. vtPChar: s += Args[i].VPChar+LineEnding;
  892. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  893. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  894. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  895. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  896. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  897. end;
  898. Result:=s;
  899. end;
  900. function ExtractFileUnitName(aFilename: string): string;
  901. var
  902. p: Integer;
  903. begin
  904. Result:=ExtractFileName(aFilename);
  905. if Result='' then exit;
  906. for p:=length(Result) downto 1 do
  907. case Result[p] of
  908. '/','\': exit;
  909. '.':
  910. begin
  911. Delete(Result,p,length(Result));
  912. exit;
  913. end;
  914. end;
  915. end;
  916. function JSToStr(El: TJSElement): string;
  917. var
  918. aWriter: TBufferWriter;
  919. aJSWriter: TJSWriter;
  920. begin
  921. aJSWriter:=nil;
  922. aWriter:=TBufferWriter.Create(1000);
  923. try
  924. aJSWriter:=TJSWriter.Create(aWriter);
  925. aJSWriter.IndentSize:=2;
  926. aJSWriter.WriteJS(El);
  927. Result:=aWriter.AsString;
  928. finally
  929. aJSWriter.Free;
  930. aWriter.Free;
  931. end;
  932. end;
  933. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  934. // search diff, ignore changes in spaces
  935. const
  936. SpaceChars = [#9,#10,#13,' '];
  937. var
  938. ExpectedP, ActualP: PChar;
  939. function FindLineEnd(p: PChar): PChar;
  940. begin
  941. Result:=p;
  942. while not (Result^ in [#0,#10,#13]) do inc(Result);
  943. end;
  944. function FindLineStart(p, MinP: PChar): PChar;
  945. begin
  946. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  947. Result:=p;
  948. end;
  949. procedure SkipLineEnd(var p: PChar);
  950. begin
  951. if p^ in [#10,#13] then
  952. begin
  953. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  954. inc(p,2)
  955. else
  956. inc(p);
  957. end;
  958. end;
  959. function HasSpecialChar(s: string): boolean;
  960. var
  961. i: Integer;
  962. begin
  963. for i:=1 to length(s) do
  964. if s[i] in [#0..#31,#127..#255] then
  965. exit(true);
  966. Result:=false;
  967. end;
  968. function HashSpecialChars(s: string): string;
  969. var
  970. i: Integer;
  971. begin
  972. Result:='';
  973. for i:=1 to length(s) do
  974. if s[i] in [#0..#31,#127..#255] then
  975. Result:=Result+'#'+hexstr(ord(s[i]),2)
  976. else
  977. Result:=Result+s[i];
  978. end;
  979. procedure DiffFound;
  980. var
  981. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  982. ExpLine, ActLine: String;
  983. i, LineNo, DiffLineNo: Integer;
  984. begin
  985. writeln('Diff found "',Msg,'". Lines:');
  986. // write correct lines
  987. p:=PChar(Expected);
  988. LineNo:=0;
  989. DiffLineNo:=0;
  990. repeat
  991. StartPos:=p;
  992. while not (p^ in [#0,#10,#13]) do inc(p);
  993. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  994. SkipLineEnd(p);
  995. inc(LineNo);
  996. if (p<=ExpectedP) and (p^<>#0) then
  997. begin
  998. writeln('= ',ExpLine);
  999. end else begin
  1000. // diff line
  1001. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1002. // write actual line
  1003. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1004. ActLineEndP:=FindLineEnd(ActualP);
  1005. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1006. writeln('- ',ActLine);
  1007. if HasSpecialChar(ActLine) then
  1008. writeln('- ',HashSpecialChars(ActLine));
  1009. // write expected line
  1010. writeln('+ ',ExpLine);
  1011. if HasSpecialChar(ExpLine) then
  1012. writeln('- ',HashSpecialChars(ExpLine));
  1013. // write empty line with pointer ^
  1014. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1015. writeln('^');
  1016. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1017. CheckSrcDiff:=false;
  1018. // write up to three following actual lines to get some context
  1019. for i:=1 to 3 do begin
  1020. ActLineStartP:=ActLineEndP;
  1021. SkipLineEnd(ActLineStartP);
  1022. if ActLineStartP^=#0 then break;
  1023. ActLineEndP:=FindLineEnd(ActLineStartP);
  1024. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1025. writeln('~ ',ActLine);
  1026. end;
  1027. exit;
  1028. end;
  1029. until p^=#0;
  1030. writeln('DiffFound Actual:-----------------------');
  1031. writeln(Actual);
  1032. writeln('DiffFound Expected:---------------------');
  1033. writeln(Expected);
  1034. writeln('DiffFound ------------------------------');
  1035. Msg:='diff found, but lines are the same, internal error';
  1036. CheckSrcDiff:=false;
  1037. end;
  1038. var
  1039. IsSpaceNeeded: Boolean;
  1040. LastChar, Quote: Char;
  1041. begin
  1042. Result:=true;
  1043. Msg:='';
  1044. if Expected='' then Expected:=' ';
  1045. if Actual='' then Actual:=' ';
  1046. ExpectedP:=PChar(Expected);
  1047. ActualP:=PChar(Actual);
  1048. repeat
  1049. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1050. case ExpectedP^ of
  1051. #0:
  1052. begin
  1053. // check that rest of Actual has only spaces
  1054. while ActualP^ in SpaceChars do inc(ActualP);
  1055. if ActualP^<>#0 then
  1056. begin
  1057. DiffFound;
  1058. exit;
  1059. end;
  1060. exit(true);
  1061. end;
  1062. ' ',#9,#10,#13:
  1063. begin
  1064. // skip space in Expected
  1065. IsSpaceNeeded:=false;
  1066. if ExpectedP>PChar(Expected) then
  1067. LastChar:=ExpectedP[-1]
  1068. else
  1069. LastChar:=#0;
  1070. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1071. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1072. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1073. IsSpaceNeeded:=true;
  1074. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1075. begin
  1076. DiffFound;
  1077. exit;
  1078. end;
  1079. while ActualP^ in SpaceChars do inc(ActualP);
  1080. end;
  1081. '''','"':
  1082. begin
  1083. while ActualP^ in SpaceChars do inc(ActualP);
  1084. if ExpectedP^<>ActualP^ then
  1085. begin
  1086. DiffFound;
  1087. exit;
  1088. end;
  1089. Quote:=ExpectedP^;
  1090. repeat
  1091. inc(ExpectedP);
  1092. inc(ActualP);
  1093. if ExpectedP^<>ActualP^ then
  1094. begin
  1095. DiffFound;
  1096. exit;
  1097. end;
  1098. if (ExpectedP^ in [#0,#10,#13]) then
  1099. break
  1100. else if (ExpectedP^=Quote) then
  1101. begin
  1102. inc(ExpectedP);
  1103. inc(ActualP);
  1104. break;
  1105. end;
  1106. until false;
  1107. end;
  1108. else
  1109. while ActualP^ in SpaceChars do inc(ActualP);
  1110. if ExpectedP^<>ActualP^ then
  1111. begin
  1112. DiffFound;
  1113. exit;
  1114. end;
  1115. inc(ExpectedP);
  1116. inc(ActualP);
  1117. end;
  1118. until false;
  1119. end;
  1120. { TTestEnginePasResolver }
  1121. destructor TTestEnginePasResolver.Destroy;
  1122. begin
  1123. FreeAndNil(FStreamResolver);
  1124. FreeAndNil(FParser);
  1125. FreeAndNil(FScanner);
  1126. FreeAndNil(FStreamResolver);
  1127. if Module<>nil then
  1128. begin
  1129. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1130. FModule:=nil;
  1131. end;
  1132. inherited Destroy;
  1133. end;
  1134. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1135. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1136. begin
  1137. Result:=nil;
  1138. if InFilename<>'' then
  1139. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1140. if Assigned(OnFindUnit) then
  1141. Result:=OnFindUnit(AName);
  1142. if NameExpr=nil then ;
  1143. end;
  1144. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1145. begin
  1146. // do not parse recursively
  1147. // parse via the queue
  1148. if Section=nil then ;
  1149. end;
  1150. { TCustomTestModule }
  1151. function TCustomTestModule.GetMsgCount: integer;
  1152. begin
  1153. Result:=FHintMsgs.Count;
  1154. end;
  1155. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1156. begin
  1157. Result:=TTestHintMessage(FHintMsgs[Index]);
  1158. end;
  1159. function TCustomTestModule.GetResolverCount: integer;
  1160. begin
  1161. Result:=FModules.Count;
  1162. end;
  1163. function TCustomTestModule.GetResolvers(Index: integer
  1164. ): TTestEnginePasResolver;
  1165. begin
  1166. Result:=TTestEnginePasResolver(FModules[Index]);
  1167. end;
  1168. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1169. ): TPasModule;
  1170. var
  1171. DefNamespace: String;
  1172. begin
  1173. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1174. if (Pos('.',aUnitName)<1) then
  1175. begin
  1176. DefNamespace:=GetDefaultNamespace;
  1177. if DefNamespace<>'' then
  1178. begin
  1179. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1180. if Result<>nil then exit;
  1181. end;
  1182. end;
  1183. Result:=LoadUnit(aUnitName);
  1184. if Result<>nil then exit;
  1185. {$IFDEF VerbosePas2JS}
  1186. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1187. {$ENDIF}
  1188. Fail('can''t find unit "'+aUnitName+'"');
  1189. end;
  1190. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1191. var
  1192. aParser: TPasParser;
  1193. Item: TTestHintMessage;
  1194. begin
  1195. aParser:=Sender as TPasParser;
  1196. Item:=TTestHintMessage.Create;
  1197. Item.Id:=aParser.LastMsgNumber;
  1198. Item.MsgType:=aParser.LastMsgType;
  1199. Item.MsgNumber:=aParser.LastMsgNumber;
  1200. Item.Msg:=Msg;
  1201. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1202. {$IFDEF VerbosePas2JS}
  1203. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1204. {$ENDIF}
  1205. FHintMsgs.Add(Item);
  1206. end;
  1207. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1208. );
  1209. var
  1210. aResolver: TTestEnginePasResolver;
  1211. Item: TTestHintMessage;
  1212. begin
  1213. aResolver:=Sender as TTestEnginePasResolver;
  1214. Item:=TTestHintMessage.Create;
  1215. Item.Id:=aResolver.LastMsgId;
  1216. Item.MsgType:=aResolver.LastMsgType;
  1217. Item.MsgNumber:=aResolver.LastMsgNumber;
  1218. Item.Msg:=Msg;
  1219. Item.SourcePos:=aResolver.LastSourcePos;
  1220. {$IFDEF VerbosePas2JS}
  1221. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1222. {$ENDIF}
  1223. FHintMsgs.Add(Item);
  1224. end;
  1225. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1226. var
  1227. Item: TTestHintMessage;
  1228. aScanner: TPas2jsPasScanner;
  1229. begin
  1230. aScanner:=Sender as TPas2jsPasScanner;
  1231. Item:=TTestHintMessage.Create;
  1232. Item.Id:=aScanner.LastMsgNumber;
  1233. Item.MsgType:=aScanner.LastMsgType;
  1234. Item.MsgNumber:=aScanner.LastMsgNumber;
  1235. Item.Msg:=Msg;
  1236. Item.SourcePos:=aScanner.CurSourcePos;
  1237. {$IFDEF VerbosePas2JS}
  1238. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1239. {$ENDIF}
  1240. FHintMsgs.Add(Item);
  1241. end;
  1242. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1243. begin
  1244. if FWithTypeInfo=AValue then Exit;
  1245. FWithTypeInfo:=AValue;
  1246. if AValue then
  1247. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1248. else
  1249. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1250. end;
  1251. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1252. var
  1253. i: Integer;
  1254. CurEngine: TTestEnginePasResolver;
  1255. CurUnitName: String;
  1256. begin
  1257. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1258. Result:=nil;
  1259. if (Module.ClassType=TPasModule)
  1260. and (CompareText(Module.Name,aUnitName)=0) then
  1261. exit(Module);
  1262. for i:=0 to ResolverCount-1 do
  1263. begin
  1264. CurEngine:=Resolvers[i];
  1265. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1266. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1267. if CompareText(aUnitName,CurUnitName)=0 then
  1268. begin
  1269. Result:=CurEngine.Module;
  1270. if Result<>nil then exit;
  1271. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1272. FileResolver.FindSourceFile(aUnitName);
  1273. CurEngine.StreamResolver:=TStreamResolver.Create;
  1274. CurEngine.StreamResolver.OwnsStreams:=True;
  1275. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1276. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1277. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1278. InitScanner(CurEngine.Scanner);
  1279. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1280. CurEngine.Parser.Options:=po_tcmodules;
  1281. if CompareText(CurUnitName,'System')=0 then
  1282. CurEngine.Parser.ImplicitUses.Clear;
  1283. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1284. try
  1285. CurEngine.Parser.NextToken;
  1286. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1287. except
  1288. on E: Exception do
  1289. HandleException(E);
  1290. end;
  1291. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1292. Result:=CurEngine.Module;
  1293. exit;
  1294. end;
  1295. end;
  1296. end;
  1297. procedure TCustomTestModule.SetUp;
  1298. begin
  1299. {$IFDEF EnablePasTreeGlobalRefCount}
  1300. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1301. {$ENDIF}
  1302. if FModules<>nil then
  1303. begin
  1304. writeln('TCustomTestModule.SetUp FModules<>nil');
  1305. Halt;
  1306. end;
  1307. inherited SetUp;
  1308. FSkipTests:=false;
  1309. FWithTypeInfo:=false;
  1310. FSource:=TStringList.Create;
  1311. FHub:=TPas2JSResolverHub.Create(Self);
  1312. FModules:=TObjectList.Create(true);
  1313. FFilename:='test1.pp';
  1314. FFileResolver:=TStreamResolver.Create;
  1315. FFileResolver.OwnsStreams:=True;
  1316. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1317. InitScanner(FScanner);
  1318. FEngine:=AddModule(Filename);
  1319. FEngine.Scanner:=FScanner;
  1320. FScanner.Resolver:=FEngine;
  1321. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1322. FParser.OnLog:=@OnParserLog;
  1323. FEngine.Parser:=FParser;
  1324. Parser.Options:=po_tcmodules;
  1325. FModule:=Nil;
  1326. FConverter:=CreateConverter;
  1327. FExpectedErrorClass:=nil;
  1328. end;
  1329. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1330. var
  1331. Options: TPasToJsConverterOptions;
  1332. begin
  1333. Result:=TPasToJSConverter.Create;
  1334. Options:=co_tcmodules;
  1335. if WithTypeInfo then
  1336. Exclude(Options,coNoTypeInfo)
  1337. else
  1338. Include(Options,coNoTypeInfo);
  1339. Result.Options:=Options;
  1340. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1341. end;
  1342. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1343. begin
  1344. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1345. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1346. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1347. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1348. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1349. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1350. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1351. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1352. aScanner.OnLog:=@OnScannerLog;
  1353. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1354. end;
  1355. procedure TCustomTestModule.TearDown;
  1356. {$IFDEF CheckPasTreeRefCount}
  1357. var
  1358. El: TPasElement;
  1359. {$ENDIF}
  1360. var
  1361. i: Integer;
  1362. CurModule: TPasModule;
  1363. begin
  1364. FHintMsgs.Clear;
  1365. FHintMsgsGood.Clear;
  1366. FSkipTests:=false;
  1367. FWithTypeInfo:=false;
  1368. FJSRegModuleCall:=nil;
  1369. FJSModuleCallArgs:=nil;
  1370. FJSImplentationUses:=nil;
  1371. FJSInterfaceUses:=nil;
  1372. FJSModuleSrc:=nil;
  1373. FJSInitBody:=nil;
  1374. FreeAndNil(FJSSource);
  1375. FreeAndNil(FJSModule);
  1376. FreeAndNil(FConverter);
  1377. Engine.Clear;
  1378. FreeAndNil(FSource);
  1379. FreeAndNil(FFileResolver);
  1380. if FModules<>nil then
  1381. begin
  1382. for i:=0 to FModules.Count-1 do
  1383. begin
  1384. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1385. if CurModule=nil then continue;
  1386. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1387. CurModule.ReleaseUsedUnits;
  1388. end;
  1389. if FModule<>nil then
  1390. FModule.ReleaseUsedUnits;
  1391. for i:=0 to FModules.Count-1 do
  1392. begin
  1393. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1394. if CurModule=nil then continue;
  1395. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1396. end;
  1397. FreeAndNil(FModules);
  1398. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1399. FEngine:=nil;
  1400. end;
  1401. FreeAndNil(FHub);
  1402. inherited TearDown;
  1403. {$IFDEF EnablePasTreeGlobalRefCount}
  1404. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1405. begin
  1406. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1407. {$IFDEF CheckPasTreeRefCount}
  1408. El:=TPasElement.FirstRefEl;
  1409. while El<>nil do
  1410. begin
  1411. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1412. for i:=0 to El.RefIds.Count-1 do
  1413. writeln(' ',El.RefIds[i]);
  1414. El:=El.NextRefEl;
  1415. end;
  1416. {$ENDIF}
  1417. Halt;
  1418. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1419. end;
  1420. {$ENDIF}
  1421. end;
  1422. procedure TCustomTestModule.Add(Line: string);
  1423. begin
  1424. Source.Add(Line);
  1425. end;
  1426. procedure TCustomTestModule.Add(const Lines: array of string);
  1427. var
  1428. i: Integer;
  1429. begin
  1430. for i:=low(Lines) to high(Lines) do
  1431. Add(Lines[i]);
  1432. end;
  1433. procedure TCustomTestModule.StartParsing;
  1434. var
  1435. Src: String;
  1436. begin
  1437. Src:=Source.Text;
  1438. FEngine.Source:=Src;
  1439. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1440. Scanner.OpenFile(FileName);
  1441. Writeln('// Test : ',Self.TestName);
  1442. Writeln(Src);
  1443. end;
  1444. procedure TCustomTestModule.ParseModuleQueue;
  1445. var
  1446. i: Integer;
  1447. CurResolver: TTestEnginePasResolver;
  1448. Found: Boolean;
  1449. Section: TPasSection;
  1450. begin
  1451. // parse til exception or all modules finished
  1452. while not SkipTests do
  1453. begin
  1454. Found:=false;
  1455. for i:=0 to ResolverCount-1 do
  1456. begin
  1457. CurResolver:=Resolvers[i];
  1458. if CurResolver.CurrentParser=nil then continue;
  1459. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1460. continue;
  1461. CurResolver.Parser.ParseContinue;
  1462. Found:=true;
  1463. break;
  1464. end;
  1465. if not Found then break;
  1466. end;
  1467. for i:=0 to ResolverCount-1 do
  1468. begin
  1469. CurResolver:=Resolvers[i];
  1470. if CurResolver.Parser=nil then
  1471. begin
  1472. if CurResolver.CurrentParser<>nil then
  1473. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1474. continue;
  1475. end;
  1476. if CurResolver.Parser.CurModule<>nil then
  1477. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1478. end;
  1479. end;
  1480. procedure TCustomTestModule.ParseModule;
  1481. begin
  1482. if SkipTests then exit;
  1483. FFirstPasStatement:=nil;
  1484. try
  1485. StartParsing;
  1486. Parser.ParseMain(FModule);
  1487. ParseModuleQueue;
  1488. except
  1489. on E: Exception do
  1490. HandleException(E);
  1491. end;
  1492. if SkipTests then exit;
  1493. AssertNotNull('Module resulted in Module',Module);
  1494. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1495. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1496. end;
  1497. procedure TCustomTestModule.ParseProgram;
  1498. begin
  1499. if SkipTests then exit;
  1500. ParseModule;
  1501. if SkipTests then exit;
  1502. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1503. FPasProgram:=TPasProgram(Module);
  1504. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1505. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1506. if (PasProgram.InitializationSection.Elements.Count>0) then
  1507. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1508. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1509. end;
  1510. procedure TCustomTestModule.ParseLibrary;
  1511. var
  1512. Init: TInitializationSection;
  1513. begin
  1514. if SkipTests then exit;
  1515. ParseModule;
  1516. if SkipTests then exit;
  1517. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1518. FPasLibrary:=TPasLibrary(Module);
  1519. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1520. Init:=PasLibrary.InitializationSection;
  1521. if (Init<>nil) and (Init.Elements.Count>0) then
  1522. if TObject(Init.Elements[0]) is TPasImplBlock then
  1523. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1524. end;
  1525. procedure TCustomTestModule.ParseUnit;
  1526. begin
  1527. if SkipTests then exit;
  1528. ParseModule;
  1529. if SkipTests then exit;
  1530. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1531. AssertNotNull('Has interface section',Module.InterfaceSection);
  1532. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1533. if (Module.InitializationSection<>nil)
  1534. and (Module.InitializationSection.Elements.Count>0)
  1535. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1536. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1537. end;
  1538. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1539. ): TTestEnginePasResolver;
  1540. var
  1541. i: Integer;
  1542. begin
  1543. for i:=0 to ResolverCount-1 do
  1544. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1545. exit(Resolvers[i]);
  1546. Result:=nil;
  1547. end;
  1548. function TCustomTestModule.AddModule(aFilename: string
  1549. ): TTestEnginePasResolver;
  1550. begin
  1551. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1552. if FindModuleWithFilename(aFilename)<>nil then
  1553. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1554. Result:=TTestEnginePasResolver.Create;
  1555. Result.Filename:=aFilename;
  1556. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1557. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1558. Result.OnLog:=@OnPasResolverLog;
  1559. Result.Hub:=Hub;
  1560. FModules.Add(Result);
  1561. end;
  1562. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1563. ): TTestEnginePasResolver;
  1564. begin
  1565. Result:=AddModule(aFilename);
  1566. Result.Source:=Src;
  1567. end;
  1568. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1569. ImplementationSrc: string): TTestEnginePasResolver;
  1570. var
  1571. Src: String;
  1572. begin
  1573. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1574. Src+=LineEnding;
  1575. Src+='interface'+LineEnding;
  1576. Src+=LineEnding;
  1577. Src+=InterfaceSrc;
  1578. Src+='implementation'+LineEnding;
  1579. Src+=LineEnding;
  1580. Src+=ImplementationSrc;
  1581. Src+='end.'+LineEnding;
  1582. Result:=AddModuleWithSrc(aFilename,Src);
  1583. end;
  1584. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1585. var
  1586. Intf, Impl: TStringList;
  1587. begin
  1588. Intf:=TStringList.Create;
  1589. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1590. // unit interface
  1591. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1592. Intf.Add('{$modeswitch externalclass}');
  1593. Intf.Add('type');
  1594. Intf.Add(' integer=longint;');
  1595. Intf.Add(' sizeint=nativeint;');
  1596. //'const',
  1597. //' LineEnding = #10;',
  1598. //' DirectorySeparator = ''/'';',
  1599. //' DriveSeparator = '''';',
  1600. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1601. //' AllowDriveSeparators : set of char = [];',
  1602. if supTObject in Parts then
  1603. Intf.AddStrings([
  1604. 'type',
  1605. ' TClass = class of TObject;',
  1606. ' TObject = class',
  1607. ' constructor Create;',
  1608. ' destructor Destroy; virtual;',
  1609. ' class function ClassType: TClass; assembler;',
  1610. ' class function ClassName: String; assembler;',
  1611. ' class function ClassNameIs(const Name: string): boolean;',
  1612. ' class function ClassParent: TClass; assembler;',
  1613. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1614. ' class function UnitName: String; assembler;',
  1615. ' procedure AfterConstruction; virtual;',
  1616. ' procedure BeforeDestruction;virtual;',
  1617. ' function Equals(Obj: TObject): boolean; virtual;',
  1618. ' function ToString: String; virtual;',
  1619. ' end;']);
  1620. if supTInterfacedObject in Parts then
  1621. Intf.AddStrings([
  1622. ' {$Interfaces COM}',
  1623. ' IUnknown = interface',
  1624. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1625. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1626. ' function _AddRef: Integer;',
  1627. ' function _Release: Integer;',
  1628. ' end;',
  1629. ' IInterface = IUnknown;',
  1630. ' TInterfacedObject = class(TObject,IUnknown)',
  1631. ' protected',
  1632. ' fRefCount: Integer;',
  1633. ' { implement methods of IUnknown }',
  1634. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1635. ' function _AddRef: Integer; virtual;',
  1636. ' function _Release: Integer; virtual;',
  1637. ' end;',
  1638. ' TInterfacedClass = class of TInterfacedObject;',
  1639. '',
  1640. '']);
  1641. if supTVarRec in Parts then
  1642. Intf.AddStrings([
  1643. 'const',
  1644. ' vtInteger = 0;',
  1645. ' vtBoolean = 1;',
  1646. ' vtJSValue = 19;',
  1647. 'type',
  1648. ' PVarRec = ^TVarRec;',
  1649. ' TVarRec = record',
  1650. ' VType : byte;',
  1651. ' VJSValue: JSValue;',
  1652. ' vInteger: longint external name ''VJSValue'';',
  1653. ' vBoolean: boolean external name ''VJSValue'';',
  1654. ' end;',
  1655. ' TVarRecArray = array of TVarRec;',
  1656. 'function VarRecs: TVarRecArray; varargs;',
  1657. '']);
  1658. if supTypeInfo in Parts then
  1659. begin
  1660. Intf.AddStrings([
  1661. 'type',
  1662. ' TTypeKind = (',
  1663. ' tkUnknown, // 0',
  1664. ' tkInteger, // 1',
  1665. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1666. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1667. ' tkEnumeration, // 4',
  1668. ' tkSet, // 5',
  1669. ' tkDouble, // 6',
  1670. ' tkBool, // 7',
  1671. ' tkProcVar, // 8 function or procedure',
  1672. ' tkMethod, // 9 proc var of object',
  1673. ' tkArray, // 10 static array',
  1674. ' tkDynArray, // 11',
  1675. ' tkRecord, // 12',
  1676. ' tkClass, // 13',
  1677. ' tkClassRef, // 14',
  1678. ' tkPointer, // 15',
  1679. ' tkJSValue, // 16',
  1680. ' tkRefToProcVar, // 17 variable of procedure type',
  1681. ' tkInterface, // 18',
  1682. ' //tkObject,',
  1683. ' //tkSString,tkLString,tkAString,tkWString,',
  1684. ' //tkVariant,',
  1685. ' //tkWChar,',
  1686. ' //tkInt64,',
  1687. ' //tkQWord,',
  1688. ' //tkInterfaceRaw,',
  1689. ' //tkUString,tkUChar,',
  1690. ' tkHelper, // 19',
  1691. ' //tkFile,',
  1692. ' tkExtClass // 20',
  1693. ' );',
  1694. ' TTypeKinds = set of TTypeKind;',
  1695. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1696. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1697. ' end;',
  1698. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1699. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1700. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1701. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1702. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1703. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1704. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1705. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1706. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1707. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1708. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1709. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1710. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1711. '']);
  1712. end;
  1713. if supWriteln in Parts then
  1714. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1715. Intf.Add('var');
  1716. Intf.Add(' ExitCode: Longint = 0;');
  1717. // unit implementation
  1718. Impl:=TStringList.Create;
  1719. if supTObject in Parts then
  1720. Impl.AddStrings([
  1721. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1722. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1723. 'asm',
  1724. 'end;',
  1725. 'constructor TObject.Create; begin end;',
  1726. 'destructor TObject.Destroy; begin end;',
  1727. 'class function TObject.ClassType: TClass; assembler;',
  1728. 'asm',
  1729. 'end;',
  1730. 'class function TObject.ClassName: String; assembler;',
  1731. 'asm',
  1732. 'end;',
  1733. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1734. 'begin',
  1735. ' Result:=SameText(Name,ClassName);',
  1736. 'end;',
  1737. 'class function TObject.ClassParent: TClass; assembler;',
  1738. 'asm',
  1739. 'end;',
  1740. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1741. 'asm',
  1742. 'end;',
  1743. 'class function TObject.UnitName: String; assembler;',
  1744. 'asm',
  1745. 'end;',
  1746. 'procedure TObject.AfterConstruction; begin end;',
  1747. 'procedure TObject.BeforeDestruction; begin end;',
  1748. 'function TObject.Equals(Obj: TObject): boolean;',
  1749. 'begin',
  1750. ' Result:=Obj=Self;',
  1751. 'end;',
  1752. 'function TObject.ToString: String;',
  1753. 'begin',
  1754. ' Result:=ClassName;',
  1755. 'end;'
  1756. ]);
  1757. if supTInterfacedObject in Parts then
  1758. Impl.AddStrings([
  1759. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1760. //'begin',
  1761. //'end;',
  1762. 'function TInterfacedObject._AddRef: Integer;',
  1763. 'begin',
  1764. 'end;',
  1765. 'function TInterfacedObject._Release: Integer;',
  1766. 'begin',
  1767. 'end;',
  1768. '']);
  1769. if supTVarRec in Parts then
  1770. Impl.AddStrings([
  1771. 'function VarRecs: TVarRecArray; varargs;',
  1772. 'var',
  1773. ' v: PVarRec;',
  1774. 'begin',
  1775. ' v^.VType:=1;',
  1776. ' v^.VJSValue:=2;',
  1777. 'end;',
  1778. '']);
  1779. try
  1780. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1781. finally
  1782. Intf.Free;
  1783. Impl.Free;
  1784. end;
  1785. end;
  1786. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1787. SystemUnitParts: TSystemUnitParts);
  1788. begin
  1789. if NeedSystemUnit then
  1790. AddSystemUnit(SystemUnitParts)
  1791. else
  1792. Parser.ImplicitUses.Clear;
  1793. Add('program '+ExtractFileUnitName(Filename)+';');
  1794. Add('');
  1795. end;
  1796. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1797. SystemUnitParts: TSystemUnitParts);
  1798. begin
  1799. if NeedSystemUnit then
  1800. AddSystemUnit(SystemUnitParts)
  1801. else
  1802. Parser.ImplicitUses.Clear;
  1803. Add('library '+ExtractFileUnitName(Filename)+';');
  1804. Add('');
  1805. end;
  1806. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1807. SystemUnitParts: TSystemUnitParts);
  1808. begin
  1809. if NeedSystemUnit then
  1810. AddSystemUnit(SystemUnitParts)
  1811. else
  1812. Parser.ImplicitUses.Clear;
  1813. Add('unit Test1;');
  1814. Add('');
  1815. end;
  1816. procedure TCustomTestModule.ConvertModule;
  1817. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1818. out UsesLit: TJSArrayLiteral);
  1819. var
  1820. i: Integer;
  1821. Item: TJSElement;
  1822. Lit: TJSLiteral;
  1823. begin
  1824. UsesLit:=nil;
  1825. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1826. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1827. exit; // null is ok
  1828. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1829. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1830. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1831. begin
  1832. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1833. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1834. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1835. Lit:=TJSLiteral(Item);
  1836. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1837. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1838. end;
  1839. end;
  1840. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1841. out Src: TJSSourceElements);
  1842. var
  1843. FunDecl: TJSFunctionDeclarationStatement;
  1844. FunDef: TJSFuncDef;
  1845. FunBody: TJSFunctionBody;
  1846. begin
  1847. Src:=nil;
  1848. AssertNotNull(ParamName,Arg.Expr);
  1849. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1850. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1851. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1852. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1853. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1854. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1855. AssertNotNull(ParamName+' body',FunDef.Body);
  1856. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1857. FunBody:=FunDef.Body as TJSFunctionBody;
  1858. AssertNotNull(ParamName+' body.A',FunBody.A);
  1859. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1860. Src:=FunBody.A as TJSSourceElements;
  1861. end;
  1862. var
  1863. ModuleNameExpr: TJSLiteral;
  1864. InitFunction: TJSFunctionDeclarationStatement;
  1865. InitAssign: TJSSimpleAssignStatement;
  1866. InitName: String;
  1867. LastNode: TJSElement;
  1868. Arg: TJSArrayLiteralElement;
  1869. IsProg, IsLib: Boolean;
  1870. begin
  1871. if SkipTests then exit;
  1872. IsProg:=false;
  1873. IsLib:=false;
  1874. if Module is TPasProgram then
  1875. IsProg:=true
  1876. else if Module is TPasLibrary then
  1877. IsLib:=true;
  1878. try
  1879. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1880. except
  1881. on E: Exception do
  1882. HandleException(E);
  1883. end;
  1884. if SkipTests then exit;
  1885. if ExpectedErrorClass<>nil then
  1886. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1887. FJSSource:=TStringList.Create;
  1888. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1889. {$IFDEF VerbosePas2JS}
  1890. writeln('TTestModule.ConvertModule JS:');
  1891. write(FJSSource.Text);
  1892. {$ENDIF}
  1893. // rtl.module(...
  1894. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1895. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1896. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1897. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1898. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1899. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1900. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1901. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1902. // parameter 'unitname'
  1903. if JSModuleCallArgs.Elements.Count<1 then
  1904. Fail('rtl.module first param unit missing');
  1905. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1906. AssertNotNull('module name param',Arg.Expr);
  1907. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1908. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1909. if IsProg then
  1910. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1911. else if IsLib then
  1912. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  1913. else
  1914. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1915. // main uses section
  1916. if JSModuleCallArgs.Elements.Count<2 then
  1917. Fail('rtl.module second param main uses missing');
  1918. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1919. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1920. // program/library/interface function()
  1921. if JSModuleCallArgs.Elements.Count<3 then
  1922. Fail('rtl.module third param intf-function missing');
  1923. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1924. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1925. // search for $mod.$init or $mod.$main - the last statement
  1926. if IsProg or IsLib then
  1927. begin
  1928. InitName:='$main';
  1929. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1930. end
  1931. else
  1932. InitName:='$init';
  1933. InitAssign:=nil;
  1934. InitFunction:=nil;
  1935. FJSInitBody:=nil;
  1936. if JSModuleSrc.Statements.Count>0 then
  1937. begin
  1938. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1939. if LastNode is TJSSimpleAssignStatement then
  1940. begin
  1941. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1942. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1943. begin
  1944. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1945. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1946. end
  1947. else if IsProg or IsLib then
  1948. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1949. end;
  1950. end;
  1951. // optional: implementation uses section
  1952. if JSModuleCallArgs.Elements.Count<4 then
  1953. exit;
  1954. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1955. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1956. end;
  1957. procedure TCustomTestModule.ConvertProgram;
  1958. begin
  1959. Add('end.');
  1960. ParseProgram;
  1961. ConvertModule;
  1962. end;
  1963. procedure TCustomTestModule.ConvertLibrary;
  1964. begin
  1965. Add('end.');
  1966. ParseLibrary;
  1967. ConvertModule;
  1968. end;
  1969. procedure TCustomTestModule.ConvertUnit;
  1970. begin
  1971. Add('end.');
  1972. ParseUnit;
  1973. ConvertModule;
  1974. end;
  1975. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1976. begin
  1977. Result:=tcmodules.JSToStr(El);
  1978. end;
  1979. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1980. DottedName: string);
  1981. begin
  1982. if DottedName='' then
  1983. begin
  1984. AssertNull(Msg,El);
  1985. end
  1986. else
  1987. begin
  1988. AssertNotNull(Msg,El);
  1989. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1990. end;
  1991. end;
  1992. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1993. begin
  1994. if El=nil then
  1995. Result:=''
  1996. else if El is TJSPrimaryExpressionIdent then
  1997. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1998. else if El is TJSDotMemberExpression then
  1999. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2000. else
  2001. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2002. end;
  2003. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2004. InitStatements: string; ImplStatements: string);
  2005. var
  2006. ActualSrc, ExpectedSrc, InitName: String;
  2007. IsProg, IsLib: Boolean;
  2008. begin
  2009. ActualSrc:=JSToStr(JSModuleSrc);
  2010. if coUseStrict in Converter.Options then
  2011. ExpectedSrc:='"use strict";'+LineEnding
  2012. else
  2013. ExpectedSrc:='';
  2014. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2015. ExpectedSrc:=ExpectedSrc+Statements;
  2016. // unit implementation
  2017. if (Trim(ImplStatements)<>'') then
  2018. ExpectedSrc:=ExpectedSrc+LineEnding
  2019. +'$mod.$implcode = function () {'+LineEnding
  2020. +ImplStatements
  2021. +'};'+LineEnding;
  2022. // program main or unit initialization
  2023. IsProg:=false;
  2024. IsLib:=false;
  2025. if Module is TPasProgram then
  2026. IsProg:=true
  2027. else if Module is TPasLibrary then
  2028. IsLib:=true;
  2029. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2030. begin
  2031. if IsProg or IsLib then
  2032. InitName:='$main'
  2033. else
  2034. InitName:='$init';
  2035. ExpectedSrc:=ExpectedSrc+LineEnding
  2036. +'$mod.'+InitName+' = function () {'+LineEnding
  2037. +InitStatements
  2038. +'};'+LineEnding;
  2039. end;
  2040. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2041. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2042. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2043. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2044. end;
  2045. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2046. // search diff, ignore changes in spaces
  2047. var
  2048. s: string;
  2049. begin
  2050. if CheckSrcDiff(Expected,Actual,s) then exit;
  2051. Fail(Msg+': '+s);
  2052. end;
  2053. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2054. var
  2055. aResolver: TTestEnginePasResolver;
  2056. aConverter: TPasToJSConverter;
  2057. aJSModule: TJSSourceElements;
  2058. ActualSrc: String;
  2059. begin
  2060. aResolver:=GetResolver(Filename);
  2061. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2062. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2063. {$IFDEF VerbosePas2JS}
  2064. writeln('CheckUnit '+Filename+' converting ...');
  2065. {$ENDIF}
  2066. aConverter:=CreateConverter;
  2067. aJSModule:=nil;
  2068. try
  2069. try
  2070. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2071. except
  2072. on E: Exception do
  2073. HandleException(E);
  2074. end;
  2075. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2076. {$IFDEF VerbosePas2JS}
  2077. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2078. write(aResolver.Source);
  2079. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2080. write(ActualSrc);
  2081. {$ENDIF}
  2082. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2083. finally
  2084. aJSModule.Free;
  2085. aConverter.Free;
  2086. end;
  2087. end;
  2088. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2089. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2090. var
  2091. i: Integer;
  2092. Item: TTestHintMessage;
  2093. Expected,Actual: string;
  2094. begin
  2095. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2096. for i:=0 to MsgCount-1 do
  2097. begin
  2098. Item:=Msgs[i];
  2099. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2100. if (Marker<>nil) then
  2101. begin
  2102. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2103. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2104. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2105. end;
  2106. // found
  2107. FHintMsgsGood.Add(Item);
  2108. str(Item.MsgType,Actual);
  2109. str(MsgType,Expected);
  2110. AssertEquals('MsgType',Expected,Actual);
  2111. exit;
  2112. end;
  2113. // needed message missing -> show emitted messages
  2114. WriteSources('',0,0);
  2115. for i:=0 to MsgCount-1 do
  2116. begin
  2117. Item:=Msgs[i];
  2118. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2119. ' ('+IntToStr(Item.MsgNumber),')');
  2120. if Marker<>nil then
  2121. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2122. writeln(' {',Item.Msg,'}');
  2123. end;
  2124. str(MsgType,Expected);
  2125. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2126. if Marker<>nil then
  2127. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2128. Actual:=Actual+' '+Msg;
  2129. Fail(Actual);
  2130. end;
  2131. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2132. );
  2133. var
  2134. i: Integer;
  2135. s, Txt: String;
  2136. Msg: TTestHintMessage;
  2137. begin
  2138. for i:=0 to MsgCount-1 do
  2139. begin
  2140. Msg:=Msgs[i];
  2141. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2142. s:='';
  2143. str(Msg.MsgType,s);
  2144. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2145. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2146. if WithSourcePos then
  2147. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2148. Txt:=Txt+' {'+Msg.Msg+'}';
  2149. Fail(Txt);
  2150. end;
  2151. end;
  2152. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2153. MsgNumber: integer);
  2154. begin
  2155. ExpectedErrorClass:=EScannerError;
  2156. ExpectedErrorMsg:=Msg;
  2157. ExpectedErrorNumber:=MsgNumber;
  2158. end;
  2159. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2160. MsgNumber: integer);
  2161. begin
  2162. ExpectedErrorClass:=EParserError;
  2163. ExpectedErrorMsg:=Msg;
  2164. ExpectedErrorNumber:=MsgNumber;
  2165. end;
  2166. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2167. MsgNumber: integer);
  2168. begin
  2169. ExpectedErrorClass:=EPasResolve;
  2170. ExpectedErrorMsg:=Msg;
  2171. ExpectedErrorNumber:=MsgNumber;
  2172. end;
  2173. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2174. MsgNumber: integer);
  2175. begin
  2176. ExpectedErrorClass:=EPas2JS;
  2177. ExpectedErrorMsg:=Msg;
  2178. ExpectedErrorNumber:=MsgNumber;
  2179. end;
  2180. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2181. var
  2182. MsgNumber: Integer;
  2183. Msg: String;
  2184. begin
  2185. Result:=false;
  2186. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2187. Msg:=E.Message;
  2188. if E is EPas2JS then
  2189. MsgNumber:=EPas2JS(E).MsgNumber
  2190. else if E is EPasResolve then
  2191. MsgNumber:=EPasResolve(E).MsgNumber
  2192. else if E is EParserError then
  2193. MsgNumber:=Parser.LastMsgNumber
  2194. else if E is EScannerError then
  2195. begin
  2196. MsgNumber:=Scanner.LastMsgNumber;
  2197. Msg:=Scanner.LastMsg;
  2198. end
  2199. else
  2200. MsgNumber:=0;
  2201. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2202. if Result then
  2203. SkipTests:=true;
  2204. end;
  2205. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2206. begin
  2207. if IsErrorExpected(E) then exit;
  2208. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2209. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2210. +' '+Scanner.CurFilename
  2211. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2212. FailException(E);
  2213. end;
  2214. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2215. begin
  2216. if IsErrorExpected(E) then exit;
  2217. WriteSources(E.Filename,E.Row,E.Column);
  2218. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2219. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2220. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2221. );
  2222. FailException(E);
  2223. end;
  2224. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2225. var
  2226. P: TPasSourcePos;
  2227. begin
  2228. if IsErrorExpected(E) then exit;
  2229. P:=E.SourcePos;
  2230. WriteSources(P.FileName,P.Row,P.Column);
  2231. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2232. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2233. FailException(E);
  2234. end;
  2235. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2236. var
  2237. Row, Col: integer;
  2238. begin
  2239. if IsErrorExpected(E) then exit;
  2240. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2241. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2242. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2243. +' '+E.PasElement.SourceFilename
  2244. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2245. FailException(E);
  2246. end;
  2247. procedure TCustomTestModule.HandleException(E: Exception);
  2248. begin
  2249. if E is EScannerError then
  2250. HandleScannerError(EScannerError(E))
  2251. else if E is EParserError then
  2252. HandleParserError(EParserError(E))
  2253. else if E is EPasResolve then
  2254. HandlePasResolveError(EPasResolve(E))
  2255. else if E is EPas2JS then
  2256. HandlePas2JSError(EPas2JS(E))
  2257. else
  2258. begin
  2259. if IsErrorExpected(E) then exit;
  2260. if not (E is EAssertionFailedError) then
  2261. begin
  2262. WriteSources('',0,0);
  2263. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2264. end;
  2265. FailException(E);
  2266. end;
  2267. end;
  2268. procedure TCustomTestModule.FailException(E: Exception);
  2269. var
  2270. MsgNumber: Integer;
  2271. begin
  2272. if ExpectedErrorClass<>nil then
  2273. begin
  2274. if FExpectedErrorClass=E.ClassType then
  2275. begin
  2276. if E is EPas2JS then
  2277. MsgNumber:=EPas2JS(E).MsgNumber
  2278. else if E is EPasResolve then
  2279. MsgNumber:=EPasResolve(E).MsgNumber
  2280. else if E is EParserError then
  2281. MsgNumber:=Parser.LastMsgNumber
  2282. else if E is EScannerError then
  2283. MsgNumber:=Scanner.LastMsgNumber
  2284. else
  2285. MsgNumber:=0;
  2286. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2287. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2288. ExpectedErrorNumber,MsgNumber);
  2289. end else begin
  2290. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2291. end;
  2292. end;
  2293. Fail(E.Message);
  2294. end;
  2295. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2296. aCol: integer);
  2297. var
  2298. IsSrc: Boolean;
  2299. i, j: Integer;
  2300. SrcLines: TStringList;
  2301. Line: string;
  2302. aModule: TTestEnginePasResolver;
  2303. begin
  2304. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2305. for i:=0 to ResolverCount-1 do
  2306. begin
  2307. aModule:=Resolvers[i];
  2308. SrcLines:=TStringList.Create;
  2309. try
  2310. SrcLines.Text:=aModule.Source;
  2311. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2312. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2313. for j:=1 to SrcLines.Count do
  2314. begin
  2315. Line:=SrcLines[j-1];
  2316. if IsSrc and (j=aRow) then
  2317. begin
  2318. write('*');
  2319. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2320. end;
  2321. writeln(Format('%:4d: ',[j]),Line);
  2322. end;
  2323. finally
  2324. SrcLines.Free;
  2325. end;
  2326. end;
  2327. end;
  2328. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2329. var
  2330. i: Integer;
  2331. begin
  2332. for i:=0 to ResolverCount-1 do
  2333. if Filename=Resolvers[i].Filename then exit(i);
  2334. Result:=-1;
  2335. end;
  2336. function TCustomTestModule.GetResolver(const Filename: string
  2337. ): TTestEnginePasResolver;
  2338. var
  2339. i: Integer;
  2340. begin
  2341. i:=IndexOfResolver(Filename);
  2342. if i<0 then exit(nil);
  2343. Result:=Resolvers[i];
  2344. end;
  2345. function TCustomTestModule.GetDefaultNamespace: string;
  2346. var
  2347. C: TClass;
  2348. begin
  2349. Result:='';
  2350. if FModule=nil then exit;
  2351. C:=FModule.ClassType;
  2352. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2353. Result:=Engine.DefaultNameSpace;
  2354. end;
  2355. constructor TCustomTestModule.Create;
  2356. begin
  2357. inherited Create;
  2358. FHintMsgs:=TObjectList.Create(true);
  2359. FHintMsgsGood:=TFPList.Create;
  2360. end;
  2361. destructor TCustomTestModule.Destroy;
  2362. begin
  2363. FreeAndNil(FHintMsgs);
  2364. FreeAndNil(FHintMsgsGood);
  2365. inherited Destroy;
  2366. end;
  2367. { TTestModule }
  2368. procedure TTestModule.TestReservedWords;
  2369. var
  2370. i: integer;
  2371. begin
  2372. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2373. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2374. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2375. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2376. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2377. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2378. end;
  2379. procedure TTestModule.TestEmptyProgram;
  2380. begin
  2381. StartProgram(false);
  2382. Add('begin');
  2383. ConvertProgram;
  2384. CheckSource('TestEmptyProgram','','');
  2385. end;
  2386. procedure TTestModule.TestEmptyProgramUseStrict;
  2387. begin
  2388. Converter.Options:=Converter.Options+[coUseStrict];
  2389. StartProgram(false);
  2390. Add('begin');
  2391. ConvertProgram;
  2392. CheckSource('TestEmptyProgramUseStrict','','');
  2393. end;
  2394. procedure TTestModule.TestEmptyUnit;
  2395. begin
  2396. StartUnit(false);
  2397. Add('interface');
  2398. Add('implementation');
  2399. ConvertUnit;
  2400. CheckSource('TestEmptyUnit',
  2401. LinesToStr([
  2402. ]),
  2403. '');
  2404. end;
  2405. procedure TTestModule.TestEmptyUnitUseStrict;
  2406. begin
  2407. Converter.Options:=Converter.Options+[coUseStrict];
  2408. StartUnit(false);
  2409. Add('interface');
  2410. Add('implementation');
  2411. ConvertUnit;
  2412. CheckSource('TestEmptyUnitUseStrict',
  2413. LinesToStr([
  2414. ''
  2415. ]),
  2416. '');
  2417. end;
  2418. procedure TTestModule.TestDottedUnitNames;
  2419. begin
  2420. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2421. LinesToStr([
  2422. 'var iV: longint;'
  2423. ]),
  2424. '');
  2425. FFilename:='ns1.test1.pp';
  2426. StartProgram(true);
  2427. Add('uses unIt2;');
  2428. Add('var');
  2429. Add(' i: longint;');
  2430. Add('begin');
  2431. Add(' i:=iv;');
  2432. Add(' i:=uNit2.iv;');
  2433. Add(' i:=Ns1.TEst1.i;');
  2434. ConvertProgram;
  2435. CheckSource('TestDottedUnitNames',
  2436. LinesToStr([
  2437. 'this.i = 0;',
  2438. '']),
  2439. LinesToStr([ // this.$init
  2440. '$mod.i = pas["NS1.Unit2"].iV;',
  2441. '$mod.i = pas["NS1.Unit2"].iV;',
  2442. '$mod.i = $mod.i;',
  2443. '']) );
  2444. end;
  2445. procedure TTestModule.TestDottedUnitNameImpl;
  2446. begin
  2447. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2448. LinesToStr([
  2449. 'type',
  2450. ' TObject = class end;',
  2451. ' TTestA = class',
  2452. ' end;'
  2453. ]),
  2454. LinesToStr(['uses TEST.UnitB;'])
  2455. );
  2456. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2457. LinesToStr([
  2458. 'uses TEST.UnitA;',
  2459. 'type TTestB = class(TTestA);'
  2460. ]),
  2461. ''
  2462. );
  2463. StartProgram(true);
  2464. Add('uses TEST.UnitA;');
  2465. Add('begin');
  2466. ConvertProgram;
  2467. CheckSource('TestDottedUnitNameImpl',
  2468. LinesToStr([
  2469. '']),
  2470. LinesToStr([ // this.$init
  2471. '']) );
  2472. CheckUnit('TEST.UnitA.pas',
  2473. LinesToStr([
  2474. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2475. ' var $mod = this;',
  2476. ' rtl.createClass(this, "TObject", null, function () {',
  2477. ' this.$init = function () {',
  2478. ' };',
  2479. ' this.$final = function () {',
  2480. ' };',
  2481. ' });',
  2482. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2483. ' });',
  2484. '}, ["TEST.UnitB"]);'
  2485. ]));
  2486. CheckUnit('TEST.UnitB.pas',
  2487. LinesToStr([
  2488. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2489. ' var $mod = this;',
  2490. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2491. ' });',
  2492. '});'
  2493. ]));
  2494. end;
  2495. procedure TTestModule.TestDottedUnitExpr;
  2496. begin
  2497. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2498. LinesToStr([
  2499. 'procedure DoIt;'
  2500. ]),
  2501. 'procedure DoIt; begin end;');
  2502. FFilename:='Ns1.SubNs1.Test1.pp';
  2503. StartProgram(true);
  2504. Add('uses Ns2.sUbnS2.unIt2;');
  2505. Add('var');
  2506. Add(' i: longint;');
  2507. Add('begin');
  2508. Add(' ns2.subns2.unit2.doit;');
  2509. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2510. ConvertProgram;
  2511. CheckSource('TestDottedUnitExpr',
  2512. LinesToStr([
  2513. 'this.i = 0;',
  2514. '']),
  2515. LinesToStr([ // this.$init
  2516. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2517. '$mod.i = $mod.i;',
  2518. '']) );
  2519. end;
  2520. procedure TTestModule.Test_ModeFPCFail;
  2521. begin
  2522. StartProgram(false);
  2523. Add('{$mode FPC}');
  2524. Add('begin');
  2525. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2526. ConvertProgram;
  2527. end;
  2528. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2529. begin
  2530. StartProgram(false);
  2531. Add('{$modeswitch cblocks-}');
  2532. Add('begin');
  2533. ConvertProgram;
  2534. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2535. CheckResolverUnexpectedHints();
  2536. end;
  2537. procedure TTestModule.TestUnit_UseSystem;
  2538. begin
  2539. StartUnit(true);
  2540. Add([
  2541. 'interface',
  2542. 'var i: integer;',
  2543. 'implementation']);
  2544. ConvertUnit;
  2545. CheckSource('TestUnit_UseSystem',
  2546. LinesToStr([
  2547. 'this.i = 0;',
  2548. '']),
  2549. LinesToStr([
  2550. '']) );
  2551. end;
  2552. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2553. begin
  2554. AddModuleWithIntfImplSrc('unit1.pp',
  2555. LinesToStr([
  2556. 'type number = longint;']),
  2557. LinesToStr([
  2558. 'uses test1;',
  2559. 'procedure DoIt;',
  2560. 'begin',
  2561. ' i:=3;',
  2562. 'end;']));
  2563. StartUnit(true);
  2564. Add([
  2565. 'interface',
  2566. 'uses unit1;',
  2567. 'var i: number;',
  2568. 'implementation']);
  2569. ConvertUnit;
  2570. CheckSource('TestUnit_Intf1Impl2Intf1',
  2571. LinesToStr([
  2572. 'this.i = 0;',
  2573. '']),
  2574. LinesToStr([
  2575. '']) );
  2576. end;
  2577. procedure TTestModule.TestIncludeVersion;
  2578. begin
  2579. StartProgram(false);
  2580. Add([
  2581. 'var',
  2582. ' s: string;',
  2583. ' i: word;',
  2584. 'begin',
  2585. ' s:={$I %line%};',
  2586. ' i:={$I %linenum%};',
  2587. ' s:={$I %currentroutine%};',
  2588. ' s:={$I %pas2jsversion%};',
  2589. ' s:={$I %pas2jstarget%};',
  2590. ' s:={$I %pas2jstargetos%};',
  2591. ' s:={$I %pas2jstargetcpu%};',
  2592. ' s:={$I %file%};',
  2593. '']);
  2594. ConvertProgram;
  2595. CheckSource('TestIncludeVersion',
  2596. LinesToStr([
  2597. 'this.s="";',
  2598. 'this.i = 0;']),
  2599. LinesToStr([
  2600. '$mod.s = "7";',
  2601. '$mod.i = 8;',
  2602. '$mod.s = "<anonymous>";',
  2603. '$mod.s = "Comp.Ver.tcmodules";',
  2604. '$mod.s = "Browser";',
  2605. '$mod.s = "Browser";',
  2606. '$mod.s = "ECMAScript5";',
  2607. '$mod.s = "test1.pp";',
  2608. '']));
  2609. end;
  2610. procedure TTestModule.TestVarInt;
  2611. begin
  2612. StartProgram(false);
  2613. Add('var MyI: longint;');
  2614. Add('begin');
  2615. ConvertProgram;
  2616. CheckSource('TestVarInt','this.MyI=0;','');
  2617. end;
  2618. procedure TTestModule.TestVarBaseTypes;
  2619. begin
  2620. StartProgram(false);
  2621. Add('var');
  2622. Add(' i: longint;');
  2623. Add(' s: string;');
  2624. Add(' c: char;');
  2625. Add(' b: boolean;');
  2626. Add(' d: double;');
  2627. Add(' i2: longint = 3;');
  2628. Add(' s2: string = ''foo'';');
  2629. Add(' c2: char = ''4'';');
  2630. Add(' b2: boolean = true;');
  2631. Add(' d2: double = 5.6;');
  2632. Add(' i3: longint = $707;');
  2633. Add(' i4: nativeint = 9007199254740991;');
  2634. Add(' i5: nativeint = -9007199254740991-1;');
  2635. Add(' i6: nativeint = $fffffffffffff;');
  2636. Add(' i7: nativeint = -$fffffffffffff-1;');
  2637. Add(' i8: byte = 00;');
  2638. Add(' u8: nativeuint = $fffffffffffff;');
  2639. Add(' u9: nativeuint = $0000000000000;');
  2640. Add(' u10: nativeuint = $00ff00;');
  2641. Add('begin');
  2642. ConvertProgram;
  2643. CheckSource('TestVarBaseTypes',
  2644. LinesToStr([
  2645. 'this.i = 0;',
  2646. 'this.s = "";',
  2647. 'this.c = "";',
  2648. 'this.b = false;',
  2649. 'this.d = 0.0;',
  2650. 'this.i2 = 3;',
  2651. 'this.s2 = "foo";',
  2652. 'this.c2 = "4";',
  2653. 'this.b2 = true;',
  2654. 'this.d2 = 5.6;',
  2655. 'this.i3 = 0x707;',
  2656. 'this.i4 = 9007199254740991;',
  2657. 'this.i5 = -9007199254740991-1;',
  2658. 'this.i6 = 0xfffffffffffff;',
  2659. 'this.i7 =-0xfffffffffffff-1;',
  2660. 'this.i8 = 0;',
  2661. 'this.u8 = 0xfffffffffffff;',
  2662. 'this.u9 = 0x0;',
  2663. 'this.u10 = 0xff00;'
  2664. ]),
  2665. '');
  2666. end;
  2667. procedure TTestModule.TestBaseTypeSingleFail;
  2668. begin
  2669. StartProgram(false);
  2670. Add('var s: single;');
  2671. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2672. ConvertProgram;
  2673. end;
  2674. procedure TTestModule.TestBaseTypeExtendedFail;
  2675. begin
  2676. StartProgram(false);
  2677. Add('var e: extended;');
  2678. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2679. ConvertProgram;
  2680. end;
  2681. procedure TTestModule.TestConstBaseTypes;
  2682. begin
  2683. StartProgram(false);
  2684. Add('const');
  2685. Add(' i: longint = 3;');
  2686. Add(' s: string = ''foo'';');
  2687. Add(' c: char = ''4'';');
  2688. Add(' b: boolean = true;');
  2689. Add(' d: double = 5.6;');
  2690. Add(' e = low(word);');
  2691. Add(' f = high(word);');
  2692. Add('begin');
  2693. ConvertProgram;
  2694. CheckSource('TestVarBaseTypes',
  2695. LinesToStr([
  2696. 'this.i=3;',
  2697. 'this.s="foo";',
  2698. 'this.c="4";',
  2699. 'this.b=true;',
  2700. 'this.d=5.6;',
  2701. 'this.e = 0;',
  2702. 'this.f = 65535;'
  2703. ]),
  2704. '');
  2705. end;
  2706. procedure TTestModule.TestAliasTypeRef;
  2707. begin
  2708. StartProgram(false);
  2709. Add('type');
  2710. Add(' a=longint;');
  2711. Add(' b=a;');
  2712. Add('var');
  2713. Add(' c: A;');
  2714. Add(' d: B;');
  2715. Add('begin');
  2716. ConvertProgram;
  2717. CheckSource('TestAliasTypeRef',
  2718. LinesToStr([ // statements
  2719. 'this.c = 0;',
  2720. 'this.d = 0;'
  2721. ]),
  2722. LinesToStr([ // this.$main
  2723. ''
  2724. ]));
  2725. end;
  2726. procedure TTestModule.TestTypeCast_BaseTypes;
  2727. begin
  2728. StartProgram(false);
  2729. Add([
  2730. 'var',
  2731. ' i: longint;',
  2732. ' b: boolean;',
  2733. ' d: double;',
  2734. ' s: string;',
  2735. ' c: char;',
  2736. 'begin',
  2737. ' i:=longint(i);',
  2738. ' i:=longint(b);',
  2739. ' b:=boolean(b);',
  2740. ' b:=boolean(i);',
  2741. ' d:=double(d);',
  2742. ' d:=double(i);',
  2743. ' s:=string(s);',
  2744. ' s:=string(c);',
  2745. ' c:=char(c);',
  2746. ' c:=char(i);',
  2747. ' c:=char(65);',
  2748. ' c:=char(#10);',
  2749. ' c:=char(#$E000);',
  2750. '']);
  2751. ConvertProgram;
  2752. CheckSource('TestAliasTypeRef',
  2753. LinesToStr([ // statements
  2754. 'this.i = 0;',
  2755. 'this.b = false;',
  2756. 'this.d = 0.0;',
  2757. 'this.s = "";',
  2758. 'this.c = "";',
  2759. '']),
  2760. LinesToStr([ // this.$main
  2761. '$mod.i = $mod.i;',
  2762. '$mod.i = ($mod.b ? 1 : 0);',
  2763. '$mod.b = $mod.b;',
  2764. '$mod.b = $mod.i != 0;',
  2765. '$mod.d = $mod.d;',
  2766. '$mod.d = $mod.i;',
  2767. '$mod.s = $mod.s;',
  2768. '$mod.s = $mod.c;',
  2769. '$mod.c = $mod.c;',
  2770. '$mod.c = String.fromCharCode($mod.i);',
  2771. '$mod.c = "A";',
  2772. '$mod.c = "\n";',
  2773. '$mod.c = "";',
  2774. '']));
  2775. end;
  2776. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2777. begin
  2778. StartProgram(false);
  2779. Add('type');
  2780. Add(' integer = longint;');
  2781. Add(' TYesNo = boolean;');
  2782. Add(' TFloat = double;');
  2783. Add(' TCaption = string;');
  2784. Add(' TChar = char;');
  2785. Add('var');
  2786. Add(' i: integer;');
  2787. Add(' b: TYesNo;');
  2788. Add(' d: TFloat;');
  2789. Add(' s: TCaption;');
  2790. Add(' c: TChar;');
  2791. Add('begin');
  2792. Add(' i:=integer(i);');
  2793. Add(' i:=integer(b);');
  2794. Add(' b:=TYesNo(b);');
  2795. Add(' b:=TYesNo(i);');
  2796. Add(' d:=TFloat(d);');
  2797. Add(' d:=TFloat(i);');
  2798. Add(' s:=TCaption(s);');
  2799. Add(' s:=TCaption(c);');
  2800. Add(' c:=TChar(c);');
  2801. ConvertProgram;
  2802. CheckSource('TestAliasTypeRef',
  2803. LinesToStr([ // statements
  2804. 'this.i = 0;',
  2805. 'this.b = false;',
  2806. 'this.d = 0.0;',
  2807. 'this.s = "";',
  2808. 'this.c = "";',
  2809. '']),
  2810. LinesToStr([ // this.$main
  2811. '$mod.i = $mod.i;',
  2812. '$mod.i = ($mod.b ? 1 : 0);',
  2813. '$mod.b = $mod.b;',
  2814. '$mod.b = $mod.i != 0;',
  2815. '$mod.d = $mod.d;',
  2816. '$mod.d = $mod.i;',
  2817. '$mod.s = $mod.s;',
  2818. '$mod.s = $mod.c;',
  2819. '$mod.c = $mod.c;',
  2820. '']));
  2821. end;
  2822. procedure TTestModule.TestEmptyProc;
  2823. begin
  2824. StartProgram(false);
  2825. Add('procedure Test;');
  2826. Add('begin');
  2827. Add('end;');
  2828. Add('begin');
  2829. ConvertProgram;
  2830. CheckSource('TestEmptyProc',
  2831. LinesToStr([ // statements
  2832. 'this.Test = function () {',
  2833. '};'
  2834. ]),
  2835. LinesToStr([ // this.$main
  2836. ''
  2837. ]));
  2838. end;
  2839. procedure TTestModule.TestProcOneParam;
  2840. begin
  2841. StartProgram(false);
  2842. Add('procedure ProcA(i: longint);');
  2843. Add('begin');
  2844. Add('end;');
  2845. Add('begin');
  2846. Add(' PROCA(3);');
  2847. ConvertProgram;
  2848. CheckSource('TestProcOneParam',
  2849. LinesToStr([ // statements
  2850. 'this.ProcA = function (i) {',
  2851. '};'
  2852. ]),
  2853. LinesToStr([ // this.$main
  2854. '$mod.ProcA(3);'
  2855. ]));
  2856. end;
  2857. procedure TTestModule.TestFunctionWithoutParams;
  2858. begin
  2859. StartProgram(false);
  2860. Add('function FuncA: longint;');
  2861. Add('begin');
  2862. Add('end;');
  2863. Add('var i: longint;');
  2864. Add('begin');
  2865. Add(' I:=FUNCA();');
  2866. Add(' I:=FUNCA;');
  2867. Add(' FUNCA();');
  2868. Add(' FUNCA;');
  2869. ConvertProgram;
  2870. CheckSource('TestProcWithoutParams',
  2871. LinesToStr([ // statements
  2872. 'this.FuncA = function () {',
  2873. ' var Result = 0;',
  2874. ' return Result;',
  2875. '};',
  2876. 'this.i=0;'
  2877. ]),
  2878. LinesToStr([ // this.$main
  2879. '$mod.i=$mod.FuncA();',
  2880. '$mod.i=$mod.FuncA();',
  2881. '$mod.FuncA();',
  2882. '$mod.FuncA();'
  2883. ]));
  2884. end;
  2885. procedure TTestModule.TestProcedureWithoutParams;
  2886. begin
  2887. StartProgram(false);
  2888. Add('procedure ProcA;');
  2889. Add('begin');
  2890. Add('end;');
  2891. Add('begin');
  2892. Add(' PROCA();');
  2893. Add(' PROCA;');
  2894. ConvertProgram;
  2895. CheckSource('TestProcWithoutParams',
  2896. LinesToStr([ // statements
  2897. 'this.ProcA = function () {',
  2898. '};'
  2899. ]),
  2900. LinesToStr([ // this.$main
  2901. '$mod.ProcA();',
  2902. '$mod.ProcA();'
  2903. ]));
  2904. end;
  2905. procedure TTestModule.TestIncDec;
  2906. begin
  2907. StartProgram(false);
  2908. Add([
  2909. 'procedure DoIt(var i: longint);',
  2910. 'begin',
  2911. ' inc(i);',
  2912. ' inc(i,2);',
  2913. 'end;',
  2914. 'var',
  2915. ' Bar: longint;',
  2916. 'begin',
  2917. ' inc(bar);',
  2918. ' inc(bar,2);',
  2919. ' dec(bar);',
  2920. ' dec(bar,3);',
  2921. '']);
  2922. ConvertProgram;
  2923. CheckSource('TestIncDec',
  2924. LinesToStr([ // statements
  2925. 'this.DoIt = function (i) {',
  2926. ' i.set(i.get()+1);',
  2927. ' i.set(i.get()+2);',
  2928. '};',
  2929. 'this.Bar = 0;'
  2930. ]),
  2931. LinesToStr([ // this.$main
  2932. '$mod.Bar+=1;',
  2933. '$mod.Bar+=2;',
  2934. '$mod.Bar-=1;',
  2935. '$mod.Bar-=3;'
  2936. ]));
  2937. end;
  2938. procedure TTestModule.TestLoHiFpcMode;
  2939. begin
  2940. StartProgram(false);
  2941. Add([
  2942. '{$mode objfpc}',
  2943. 'const',
  2944. ' LoByte1 = Lo(Word($1234));',
  2945. ' HiByte1 = Hi(Word($1234));',
  2946. ' LoByte2 = Lo(SmallInt($1234));',
  2947. ' HiByte2 = Hi(SmallInt($1234));',
  2948. ' LoWord1 = Lo($1234CDEF);',
  2949. ' HiWord1 = Hi($1234CDEF);',
  2950. ' LoWord2 = Lo(-$1234CDEF);',
  2951. ' HiWord2 = Hi(-$1234CDEF);',
  2952. ' lo4:byte=lo(byte($34));',
  2953. ' hi4:byte=hi(byte($34));',
  2954. ' lo5:byte=lo(shortint(-$34));',
  2955. ' hi5:byte=hi(shortint(-$34));',
  2956. ' lo6:longword=lo($123456789ABCD);',
  2957. ' hi6:longword=hi($123456789ABCD);',
  2958. ' lo7:longword=lo(-$123456789ABCD);',
  2959. ' hi7:longword=hi(-$123456789ABCD);',
  2960. 'var',
  2961. ' b: Byte;',
  2962. ' ss: shortint;',
  2963. ' w: Word;',
  2964. ' si: SmallInt;',
  2965. ' lw: LongWord;',
  2966. ' li: LongInt;',
  2967. ' b2: Byte;',
  2968. ' ni: nativeint;',
  2969. 'begin',
  2970. ' w := $1234;',
  2971. ' ss := -$12;',
  2972. ' b := lo(ss);',
  2973. ' b := HI(ss);',
  2974. ' b := lo(w);',
  2975. ' b := HI(w);',
  2976. ' b2 := lo(b);',
  2977. ' b2 := hi(b);',
  2978. ' lw := $1234CDEF;',
  2979. ' w := lo(lw);',
  2980. ' w := hi(lw);',
  2981. ' ni := $123456789ABCD;',
  2982. ' lw := lo(ni);',
  2983. ' lw := hi(ni);',
  2984. '']);
  2985. ConvertProgram;
  2986. CheckSource('TestLoHiFpcMode',
  2987. LinesToStr([ // statements
  2988. 'this.LoByte1 = 0x1234 & 0xFF;',
  2989. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2990. 'this.LoByte2 = 0x1234 & 0xFF;',
  2991. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2992. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2993. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2994. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  2995. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  2996. 'this.lo4 = 0x34 & 0xF;',
  2997. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2998. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2999. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3000. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3001. 'this.hi6 = 74565 >>> 0;',
  3002. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3003. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3004. 'this.b = 0;',
  3005. 'this.ss = 0;',
  3006. 'this.w = 0;',
  3007. 'this.si = 0;',
  3008. 'this.lw = 0;',
  3009. 'this.li = 0;',
  3010. 'this.b2 = 0;',
  3011. 'this.ni = 0;',
  3012. '']),
  3013. LinesToStr([ // this.$main
  3014. '$mod.w = 0x1234;',
  3015. '$mod.ss = -0x12;',
  3016. '$mod.b = $mod.ss & 0xFF;',
  3017. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3018. '$mod.b = $mod.w & 0xFF;',
  3019. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3020. '$mod.b2 = $mod.b & 0xF;',
  3021. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3022. '$mod.lw = 0x1234CDEF;',
  3023. '$mod.w = $mod.lw & 0xFFFF;',
  3024. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3025. '$mod.ni = 0x123456789ABCD;',
  3026. '$mod.lw = $mod.ni >>> 0;',
  3027. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3028. '']));
  3029. end;
  3030. procedure TTestModule.TestLoHiDelphiMode;
  3031. begin
  3032. StartProgram(false);
  3033. Add([
  3034. '{$mode delphi}',
  3035. 'const',
  3036. ' LoByte1 = Lo(Word($1234));',
  3037. ' HiByte1 = Hi(Word($1234));',
  3038. ' LoByte2 = Lo(SmallInt($1234));',
  3039. ' HiByte2 = Hi(SmallInt($1234));',
  3040. ' LoByte3 = Lo($1234CDEF);',
  3041. ' HiByte3 = Hi($1234CDEF);',
  3042. ' LoByte4 = Lo(-$1234CDEF);',
  3043. ' HiByte4 = Hi(-$1234CDEF);',
  3044. 'var',
  3045. ' b: Byte;',
  3046. ' w: Word;',
  3047. ' si: SmallInt;',
  3048. ' lw: LongWord;',
  3049. ' li: LongInt;',
  3050. 'begin',
  3051. ' w := $1234;',
  3052. ' b := lo(w);',
  3053. ' b := HI(w);',
  3054. ' lw := $1234CDEF;',
  3055. ' b := lo(lw);',
  3056. ' b := hi(lw);',
  3057. '']);
  3058. ConvertProgram;
  3059. CheckSource('TestLoHiDelphiMode',
  3060. LinesToStr([ // statements
  3061. 'this.LoByte1 = 0x1234 & 0xFF;',
  3062. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3063. 'this.LoByte2 = 0x1234 & 0xFF;',
  3064. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3065. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3066. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3067. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3068. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3069. 'this.b = 0;',
  3070. 'this.w = 0;',
  3071. 'this.si = 0;',
  3072. 'this.lw = 0;',
  3073. 'this.li = 0;'
  3074. ]),
  3075. LinesToStr([ // this.$main
  3076. '$mod.w = 0x1234;',
  3077. '$mod.b = $mod.w & 0xFF;',
  3078. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3079. '$mod.lw = 0x1234CDEF;',
  3080. '$mod.b = $mod.lw & 0xFF;',
  3081. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3082. ]));
  3083. end;
  3084. procedure TTestModule.TestAssignments;
  3085. begin
  3086. StartProgram(false);
  3087. Parser.Options:=Parser.Options+[po_cassignments];
  3088. Add('var');
  3089. Add(' Bar:longint;');
  3090. Add('begin');
  3091. Add(' bar:=3;');
  3092. Add(' bar+=4;');
  3093. Add(' bar-=5;');
  3094. Add(' bar*=6;');
  3095. ConvertProgram;
  3096. CheckSource('TestAssignments',
  3097. LinesToStr([ // statements
  3098. 'this.Bar = 0;'
  3099. ]),
  3100. LinesToStr([ // this.$main
  3101. '$mod.Bar=3;',
  3102. '$mod.Bar+=4;',
  3103. '$mod.Bar-=5;',
  3104. '$mod.Bar*=6;'
  3105. ]));
  3106. end;
  3107. procedure TTestModule.TestArithmeticOperators1;
  3108. begin
  3109. StartProgram(false);
  3110. Add('var');
  3111. Add(' vA,vB,vC:longint;');
  3112. Add('begin');
  3113. Add(' va:=1;');
  3114. Add(' vb:=va+va;');
  3115. Add(' vb:=va div vb;');
  3116. Add(' vb:=va mod vb;');
  3117. Add(' vb:=va+va*vb+va div vb;');
  3118. Add(' vc:=-va;');
  3119. Add(' va:=va-vb;');
  3120. Add(' vb:=va;');
  3121. Add(' if va<vb then vc:=va else vc:=vb;');
  3122. ConvertProgram;
  3123. CheckSource('TestArithmeticOperators1',
  3124. LinesToStr([ // statements
  3125. 'this.vA = 0;',
  3126. 'this.vB = 0;',
  3127. 'this.vC = 0;'
  3128. ]),
  3129. LinesToStr([ // this.$main
  3130. '$mod.vA = 1;',
  3131. '$mod.vB = $mod.vA + $mod.vA;',
  3132. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3133. '$mod.vB = $mod.vA % $mod.vB;',
  3134. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3135. '$mod.vC = -$mod.vA;',
  3136. '$mod.vA = $mod.vA - $mod.vB;',
  3137. '$mod.vB = $mod.vA;',
  3138. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3139. ]));
  3140. end;
  3141. procedure TTestModule.TestMultiAdd;
  3142. begin
  3143. StartProgram(false);
  3144. Add([
  3145. 'function Fly: string; external name ''fly'';',
  3146. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3147. 'var',
  3148. ' Date: double;',
  3149. 'begin',
  3150. ' Result:=(Year>0) and (Year<10000) and',
  3151. ' (Month >= 1) and (Month<=12) and',
  3152. ' (Day>0) and (Day<=31);',
  3153. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3154. 'end;',
  3155. 'var s: string;',
  3156. 'begin',
  3157. ' s:=''a''+''b''+''c''+''d'';',
  3158. ' s:=s+Fly+''e'';',
  3159. ' s:=Fly+Fly+Fly;',
  3160. '']);
  3161. ConvertProgram;
  3162. CheckSource('TestMultiAdd',
  3163. LinesToStr([ // statements
  3164. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3165. ' var Result = false;',
  3166. ' var date = 0.0;',
  3167. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3168. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3169. ' return Result;',
  3170. '};',
  3171. 'this.s = "";',
  3172. '']),
  3173. LinesToStr([ // this.$main
  3174. '$mod.s = "a" + "b" + "c" + "d";',
  3175. '$mod.s = $mod.s + fly() + "e";',
  3176. '$mod.s = fly() + fly() + fly();',
  3177. '']));
  3178. end;
  3179. procedure TTestModule.TestLogicalOperators;
  3180. begin
  3181. StartProgram(false);
  3182. Add('var');
  3183. Add(' vA,vB,vC:boolean;');
  3184. Add('begin');
  3185. Add(' va:=vb and vc;');
  3186. Add(' va:=vb or vc;');
  3187. Add(' va:=vb xor vc;');
  3188. Add(' va:=true and vc;');
  3189. Add(' va:=(vb and vc) or (va and vb);');
  3190. Add(' va:=not vb;');
  3191. ConvertProgram;
  3192. CheckSource('TestLogicalOperators',
  3193. LinesToStr([ // statements
  3194. 'this.vA = false;',
  3195. 'this.vB = false;',
  3196. 'this.vC = false;'
  3197. ]),
  3198. LinesToStr([ // this.$main
  3199. '$mod.vA = $mod.vB && $mod.vC;',
  3200. '$mod.vA = $mod.vB || $mod.vC;',
  3201. '$mod.vA = $mod.vB ^ $mod.vC;',
  3202. '$mod.vA = true && $mod.vC;',
  3203. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3204. '$mod.vA = !$mod.vB;'
  3205. ]));
  3206. end;
  3207. procedure TTestModule.TestBitwiseOperators;
  3208. begin
  3209. StartProgram(false);
  3210. Add([
  3211. 'var',
  3212. ' vA,vB,vC:longint;',
  3213. ' X,Y,Z: nativeint;',
  3214. 'begin',
  3215. ' va:=vb and vc;',
  3216. ' va:=vb or vc;',
  3217. ' va:=vb xor vc;',
  3218. ' va:=vb shl vc;',
  3219. ' va:=vb shr vc;',
  3220. ' va:=3 and vc;',
  3221. ' va:=(vb and vc) or (va and vb);',
  3222. ' va:=not vb;',
  3223. ' X:=Y and Z;',
  3224. ' X:=Y and va;',
  3225. ' X:=Y or Z;',
  3226. ' X:=Y or va;',
  3227. ' X:=Y xor Z;',
  3228. ' X:=Y xor va;',
  3229. '']);
  3230. ConvertProgram;
  3231. CheckSource('TestBitwiseOperators',
  3232. LinesToStr([ // statements
  3233. 'this.vA = 0;',
  3234. 'this.vB = 0;',
  3235. 'this.vC = 0;',
  3236. 'this.X = 0;',
  3237. 'this.Y = 0;',
  3238. 'this.Z = 0;',
  3239. '']),
  3240. LinesToStr([ // this.$main
  3241. '$mod.vA = $mod.vB & $mod.vC;',
  3242. '$mod.vA = $mod.vB | $mod.vC;',
  3243. '$mod.vA = $mod.vB ^ $mod.vC;',
  3244. '$mod.vA = $mod.vB << $mod.vC;',
  3245. '$mod.vA = $mod.vB >>> $mod.vC;',
  3246. '$mod.vA = 3 & $mod.vC;',
  3247. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3248. '$mod.vA = ~$mod.vB;',
  3249. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3250. '$mod.X = $mod.Y & $mod.vA;',
  3251. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3252. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3253. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3254. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3255. '']));
  3256. end;
  3257. procedure TTestModule.TestBitwiseOperatorsLongword;
  3258. begin
  3259. StartProgram(false);
  3260. Add([
  3261. 'var',
  3262. ' a,b,c:longword;',
  3263. ' i: longint;',
  3264. 'begin',
  3265. ' a:=$12345678;',
  3266. ' b:=$EDCBA987;',
  3267. ' c:=not a;',
  3268. ' c:=a and b;',
  3269. ' c:=a and $ffff0000;',
  3270. ' c:=a or b;',
  3271. ' c:=a or $ff00ff00;',
  3272. ' c:=a xor b;',
  3273. ' c:=a xor $f0f0f0f0;',
  3274. ' c:=a shl 1;',
  3275. ' c:=a shl 16;',
  3276. ' c:=a shl 24;',
  3277. ' c:=a shl b;',
  3278. ' c:=a shr 1;',
  3279. ' c:=a shr 16;',
  3280. ' c:=a shr 24;',
  3281. ' c:=a shr b;',
  3282. ' c:=(b and c) or (a and b);',
  3283. ' c:=i and a;',
  3284. ' c:=i or a;',
  3285. ' c:=i xor a;',
  3286. '']);
  3287. ConvertProgram;
  3288. CheckSource('TestBitwiseOperatorsLongword',
  3289. LinesToStr([ // statements
  3290. 'this.a = 0;',
  3291. 'this.b = 0;',
  3292. 'this.c = 0;',
  3293. 'this.i = 0;',
  3294. '']),
  3295. LinesToStr([ // this.$main
  3296. '$mod.a = 0x12345678;',
  3297. '$mod.b = 0xEDCBA987;',
  3298. '$mod.c = rtl.lw(~$mod.a);',
  3299. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3300. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3301. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3302. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3303. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3304. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3305. '$mod.c = rtl.lw($mod.a << 1);',
  3306. '$mod.c = rtl.lw($mod.a << 16);',
  3307. '$mod.c = rtl.lw($mod.a << 24);',
  3308. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3309. '$mod.c = rtl.lw($mod.a >>> 1);',
  3310. '$mod.c = rtl.lw($mod.a >>> 16);',
  3311. '$mod.c = rtl.lw($mod.a >>> 24);',
  3312. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3313. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3314. '$mod.c = $mod.i & $mod.a;',
  3315. '$mod.c = $mod.i | $mod.a;',
  3316. '$mod.c = $mod.i ^ $mod.a;',
  3317. '']));
  3318. end;
  3319. procedure TTestModule.TestPrgProcVar;
  3320. begin
  3321. StartProgram(false);
  3322. Add('procedure Proc1;');
  3323. Add('type');
  3324. Add(' t1=longint;');
  3325. Add('var');
  3326. Add(' vA:t1;');
  3327. Add('begin');
  3328. Add('end;');
  3329. Add('begin');
  3330. ConvertProgram;
  3331. CheckSource('TestPrgProcVar',
  3332. LinesToStr([ // statements
  3333. 'this.Proc1 = function () {',
  3334. ' var vA=0;',
  3335. '};'
  3336. ]),
  3337. LinesToStr([ // this.$main
  3338. ''
  3339. ]));
  3340. end;
  3341. procedure TTestModule.TestUnitProcVar;
  3342. begin
  3343. StartUnit(false);
  3344. Add('interface');
  3345. Add('');
  3346. Add('type tA=string; // unit scope');
  3347. Add('procedure Proc1;');
  3348. Add('');
  3349. Add('implementation');
  3350. Add('');
  3351. Add('procedure Proc1;');
  3352. Add('type tA=longint; // local proc scope');
  3353. Add('var v1:tA; // using local tA');
  3354. Add('begin');
  3355. Add('end;');
  3356. Add('var v2:tA; // using interface tA');
  3357. ConvertUnit;
  3358. CheckSource('TestUnitProcVar',
  3359. LinesToStr([ // statements
  3360. 'var $impl = $mod.$impl;',
  3361. 'this.Proc1 = function () {',
  3362. ' var v1 = 0;',
  3363. '};',
  3364. '']),
  3365. // this.$init
  3366. '',
  3367. // implementation
  3368. LinesToStr([
  3369. '$impl.v2 = "";',
  3370. '']));
  3371. end;
  3372. procedure TTestModule.TestImplProc;
  3373. begin
  3374. StartUnit(false);
  3375. Add('interface');
  3376. Add('');
  3377. Add('procedure Proc1;');
  3378. Add('');
  3379. Add('implementation');
  3380. Add('');
  3381. Add('procedure Proc1; begin end;');
  3382. Add('procedure Proc2; begin end;');
  3383. Add('initialization');
  3384. Add(' Proc1;');
  3385. Add(' Proc2;');
  3386. ConvertUnit;
  3387. CheckSource('TestImplProc',
  3388. LinesToStr([ // statements
  3389. 'var $impl = $mod.$impl;',
  3390. 'this.Proc1 = function () {',
  3391. '};',
  3392. '']),
  3393. LinesToStr([ // this.$init
  3394. '$mod.Proc1();',
  3395. '$impl.Proc2();',
  3396. '']),
  3397. LinesToStr([ // implementation
  3398. '$impl.Proc2 = function () {',
  3399. '};',
  3400. ''])
  3401. );
  3402. end;
  3403. procedure TTestModule.TestFunctionResult;
  3404. begin
  3405. StartProgram(false);
  3406. Add('function Func1: longint;');
  3407. Add('begin');
  3408. Add(' Result:=3;');
  3409. Add(' Func1:=4;');
  3410. Add('end;');
  3411. Add('begin');
  3412. ConvertProgram;
  3413. CheckSource('TestFunctionResult',
  3414. LinesToStr([ // statements
  3415. 'this.Func1 = function () {',
  3416. ' var Result = 0;',
  3417. ' Result = 3;',
  3418. ' Result = 4;',
  3419. ' return Result;',
  3420. '};'
  3421. ]),
  3422. '');
  3423. end;
  3424. procedure TTestModule.TestNestedProc;
  3425. begin
  3426. StartProgram(false);
  3427. Add([
  3428. 'var vInUnit: longint;',
  3429. 'function DoIt(pA,pD: longint): longint;',
  3430. 'var',
  3431. ' vB: longint;',
  3432. ' vC: longint;',
  3433. ' function Nesty(pA: longint): longint; ',
  3434. ' var vB: longint;',
  3435. ' begin',
  3436. ' Result:=pa+vb+vc+pd+vInUnit;',
  3437. ' nesty:=3;',
  3438. ' doit:=4;',
  3439. ' exit;',
  3440. ' end;',
  3441. 'begin',
  3442. ' Result:=pa+vb+vc;',
  3443. ' doit:=6;',
  3444. ' exit;',
  3445. 'end;',
  3446. 'begin']);
  3447. ConvertProgram;
  3448. CheckSource('TestNestedProc',
  3449. LinesToStr([ // statements
  3450. 'this.vInUnit = 0;',
  3451. 'this.DoIt = function (pA, pD) {',
  3452. ' var Result = 0;',
  3453. ' var vB = 0;',
  3454. ' var vC = 0;',
  3455. ' function Nesty(pA) {',
  3456. ' var Result$1 = 0;',
  3457. ' var vB = 0;',
  3458. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3459. ' Result$1 = 3;',
  3460. ' Result = 4;',
  3461. ' return Result$1;',
  3462. ' return Result$1;',
  3463. ' };',
  3464. ' Result = pA + vB + vC;',
  3465. ' Result = 6;',
  3466. ' return Result;',
  3467. ' return Result;',
  3468. '};'
  3469. ]),
  3470. '');
  3471. end;
  3472. procedure TTestModule.TestNestedProc_ResultString;
  3473. begin
  3474. StartProgram(false);
  3475. Add([
  3476. 'function DoIt: string;',
  3477. ' function Nesty: string; ',
  3478. ' begin',
  3479. ' nesty:=#65#66;',
  3480. ' nesty[1]:=#67;',
  3481. ' doit:=#68;',
  3482. ' doit[2]:=#69;',
  3483. ' end;',
  3484. 'begin',
  3485. ' doit:=#70;',
  3486. ' doit[3]:=#71;',
  3487. 'end;',
  3488. 'begin']);
  3489. ConvertProgram;
  3490. CheckSource('TestNestedProc_ResultString',
  3491. LinesToStr([ // statements
  3492. 'this.DoIt = function () {',
  3493. ' var Result = "";',
  3494. ' function Nesty() {',
  3495. ' var Result$1 = "";',
  3496. ' Result$1 = "AB";',
  3497. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3498. ' Result = "D";',
  3499. ' Result = rtl.setCharAt(Result, 1, "E");',
  3500. ' return Result$1;',
  3501. ' };',
  3502. ' Result = "F";',
  3503. ' Result = rtl.setCharAt(Result, 2, "G");',
  3504. ' return Result;',
  3505. '};'
  3506. ]),
  3507. '');
  3508. end;
  3509. procedure TTestModule.TestForwardProc;
  3510. begin
  3511. StartProgram(false);
  3512. Add('procedure FuncA(Bar: longint); forward;');
  3513. Add('procedure FuncB(Bar: longint);');
  3514. Add('begin');
  3515. Add(' funca(bar);');
  3516. Add('end;');
  3517. Add('procedure funca(bar: longint);');
  3518. Add('begin');
  3519. Add(' if bar=3 then ;');
  3520. Add('end;');
  3521. Add('begin');
  3522. Add(' funca(4);');
  3523. Add(' funcb(5);');
  3524. ConvertProgram;
  3525. CheckSource('TestForwardProc',
  3526. LinesToStr([ // statements'
  3527. 'this.FuncB = function (Bar) {',
  3528. ' $mod.FuncA(Bar);',
  3529. '};',
  3530. 'this.FuncA = function (Bar) {',
  3531. ' if (Bar === 3);',
  3532. '};'
  3533. ]),
  3534. LinesToStr([
  3535. '$mod.FuncA(4);',
  3536. '$mod.FuncB(5);'
  3537. ])
  3538. );
  3539. end;
  3540. procedure TTestModule.TestNestedForwardProc;
  3541. begin
  3542. StartProgram(false);
  3543. Add('procedure FuncA;');
  3544. Add(' procedure FuncB(i: longint); forward;');
  3545. Add(' procedure FuncC(i: longint);');
  3546. Add(' begin');
  3547. Add(' funcb(i);');
  3548. Add(' end;');
  3549. Add(' procedure FuncB(i: longint);');
  3550. Add(' begin');
  3551. Add(' if i=3 then ;');
  3552. Add(' end;');
  3553. Add('begin');
  3554. Add(' funcc(4)');
  3555. Add('end;');
  3556. Add('begin');
  3557. Add(' funca;');
  3558. ConvertProgram;
  3559. CheckSource('TestNestedForwardProc',
  3560. LinesToStr([ // statements'
  3561. 'this.FuncA = function () {',
  3562. ' function FuncC(i) {',
  3563. ' FuncB(i);',
  3564. ' };',
  3565. ' function FuncB(i) {',
  3566. ' if (i === 3);',
  3567. ' };',
  3568. ' FuncC(4);',
  3569. '};'
  3570. ]),
  3571. LinesToStr([
  3572. '$mod.FuncA();'
  3573. ])
  3574. );
  3575. end;
  3576. procedure TTestModule.TestAssignFunctionResult;
  3577. begin
  3578. StartProgram(false);
  3579. Add('function Func1: longint;');
  3580. Add('begin');
  3581. Add('end;');
  3582. Add('var i: longint;');
  3583. Add('begin');
  3584. Add(' i:=func1();');
  3585. Add(' i:=func1()+func1();');
  3586. ConvertProgram;
  3587. CheckSource('TestAssignFunctionResult',
  3588. LinesToStr([ // statements
  3589. 'this.Func1 = function () {',
  3590. ' var Result = 0;',
  3591. ' return Result;',
  3592. '};',
  3593. 'this.i = 0;'
  3594. ]),
  3595. LinesToStr([
  3596. '$mod.i = $mod.Func1();',
  3597. '$mod.i = $mod.Func1() + $mod.Func1();'
  3598. ]));
  3599. end;
  3600. procedure TTestModule.TestFunctionResultInCondition;
  3601. begin
  3602. StartProgram(false);
  3603. Add('function Func1: longint;');
  3604. Add('begin');
  3605. Add('end;');
  3606. Add('function Func2: boolean;');
  3607. Add('begin');
  3608. Add('end;');
  3609. Add('var i: longint;');
  3610. Add('begin');
  3611. Add(' if func2 then ;');
  3612. Add(' if i=func1() then ;');
  3613. Add(' if i=func1 then ;');
  3614. ConvertProgram;
  3615. CheckSource('TestFunctionResultInCondition',
  3616. LinesToStr([ // statements
  3617. 'this.Func1 = function () {',
  3618. ' var Result = 0;',
  3619. ' return Result;',
  3620. '};',
  3621. 'this.Func2 = function () {',
  3622. ' var Result = false;',
  3623. ' return Result;',
  3624. '};',
  3625. 'this.i = 0;'
  3626. ]),
  3627. LinesToStr([
  3628. 'if ($mod.Func2());',
  3629. 'if ($mod.i === $mod.Func1());',
  3630. 'if ($mod.i === $mod.Func1());'
  3631. ]));
  3632. end;
  3633. procedure TTestModule.TestFunctionResultInForLoop;
  3634. begin
  3635. StartProgram(false);
  3636. Add([
  3637. 'function Func1(a: array of longint): longint;',
  3638. 'begin',
  3639. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3640. ' for Result in a do if a[Result]=0 then exit;',
  3641. 'end;',
  3642. 'begin',
  3643. ' Func1([1,2,3])']);
  3644. ConvertProgram;
  3645. CheckSource('TestFunctionResultInForLoop',
  3646. LinesToStr([ // statements
  3647. 'this.Func1 = function (a) {',
  3648. ' var Result = 0;',
  3649. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3650. ' Result = $l;',
  3651. ' if (a[Result] === 0) return Result;',
  3652. ' };',
  3653. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3654. ' Result = $in[$l1];',
  3655. ' if (a[Result] === 0) return Result;',
  3656. ' };',
  3657. ' return Result;',
  3658. '};',
  3659. '']),
  3660. LinesToStr([
  3661. '$mod.Func1([1, 2, 3]);'
  3662. ]));
  3663. end;
  3664. procedure TTestModule.TestFunctionResultInTypeCast;
  3665. begin
  3666. StartProgram(false);
  3667. Add([
  3668. 'function GetInt: longint;',
  3669. 'begin',
  3670. 'end;',
  3671. 'begin',
  3672. ' if Byte(GetInt)=0 then ;',
  3673. '']);
  3674. ConvertProgram;
  3675. CheckSource('TestFunctionResultInTypeCast',
  3676. LinesToStr([ // statements
  3677. 'this.GetInt = function () {',
  3678. ' var Result = 0;',
  3679. ' return Result;',
  3680. '};',
  3681. '']),
  3682. LinesToStr([
  3683. 'if (($mod.GetInt() & 255) === 0) ;'
  3684. ]));
  3685. end;
  3686. procedure TTestModule.TestExit;
  3687. begin
  3688. StartProgram(false);
  3689. Add('procedure ProcA;');
  3690. Add('begin');
  3691. Add(' exit;');
  3692. Add('end;');
  3693. Add('function FuncB: longint;');
  3694. Add('begin');
  3695. Add(' exit;');
  3696. Add(' exit(3);');
  3697. Add('end;');
  3698. Add('function FuncC: string;');
  3699. Add('begin');
  3700. Add(' exit;');
  3701. Add(' exit(''a'');');
  3702. Add(' exit(''abc'');');
  3703. Add('end;');
  3704. Add('begin');
  3705. Add(' exit;');
  3706. Add(' exit(1);');
  3707. ConvertProgram;
  3708. CheckSource('TestExit',
  3709. LinesToStr([ // statements
  3710. 'this.ProcA = function () {',
  3711. ' return;',
  3712. '};',
  3713. 'this.FuncB = function () {',
  3714. ' var Result = 0;',
  3715. ' return Result;',
  3716. ' return 3;',
  3717. ' return Result;',
  3718. '};',
  3719. 'this.FuncC = function () {',
  3720. ' var Result = "";',
  3721. ' return Result;',
  3722. ' return "a";',
  3723. ' return "abc";',
  3724. ' return Result;',
  3725. '};'
  3726. ]),
  3727. LinesToStr([
  3728. 'return;',
  3729. 'return 1;',
  3730. '']));
  3731. end;
  3732. procedure TTestModule.TestExit_ResultInFinally;
  3733. begin
  3734. StartProgram(false);
  3735. Add([
  3736. 'function Run: word;',
  3737. 'begin',
  3738. ' try',
  3739. ' exit(3);', // no Result in finally -> use return 3
  3740. ' finally',
  3741. ' end;',
  3742. 'end;',
  3743. 'function Fly: word;',
  3744. 'begin',
  3745. ' try',
  3746. ' exit(3);',
  3747. ' finally',
  3748. ' if Result>0 then ;',
  3749. ' end;',
  3750. 'end;',
  3751. 'function Jump: word;',
  3752. 'begin',
  3753. ' try',
  3754. ' try',
  3755. ' exit(4);',
  3756. ' finally',
  3757. ' end;',
  3758. ' finally',
  3759. ' if Result>0 then ;',
  3760. ' end;',
  3761. 'end;',
  3762. 'begin',
  3763. '']);
  3764. ConvertProgram;
  3765. CheckSource('TestExit_ResultInFinally',
  3766. LinesToStr([ // statements
  3767. 'this.Run = function () {',
  3768. ' var Result = 0;',
  3769. ' try {',
  3770. ' return 3;',
  3771. ' } finally {',
  3772. ' };',
  3773. ' return Result;',
  3774. '};',
  3775. 'this.Fly = function () {',
  3776. ' var Result = 0;',
  3777. ' try {',
  3778. ' Result = 3;',
  3779. ' return Result;',
  3780. ' } finally {',
  3781. ' if (Result > 0) ;',
  3782. ' };',
  3783. ' return Result;',
  3784. '};',
  3785. 'this.Jump = function () {',
  3786. ' var Result = 0;',
  3787. ' try {',
  3788. ' try {',
  3789. ' Result = 4;',
  3790. ' return Result;',
  3791. ' } finally {',
  3792. ' };',
  3793. ' } finally {',
  3794. ' if (Result > 0) ;',
  3795. ' };',
  3796. ' return Result;',
  3797. '};',
  3798. '']),
  3799. LinesToStr([
  3800. '']));
  3801. end;
  3802. procedure TTestModule.TestBreak;
  3803. begin
  3804. StartProgram(false);
  3805. Add([
  3806. 'var',
  3807. ' i: longint;',
  3808. 'begin',
  3809. ' repeat',
  3810. ' break;',
  3811. ' until true;',
  3812. ' while true do',
  3813. ' break;',
  3814. ' for i:=1 to 2 do',
  3815. ' break;']);
  3816. ConvertProgram;
  3817. CheckSource('TestBreak',
  3818. LinesToStr([ // statements
  3819. 'this.i = 0;'
  3820. ]),
  3821. LinesToStr([
  3822. 'do {',
  3823. ' break;',
  3824. '} while (!true);',
  3825. 'while (true) break;',
  3826. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3827. '']));
  3828. end;
  3829. procedure TTestModule.TestBreakAsVar;
  3830. begin
  3831. StartProgram(false);
  3832. Add([
  3833. 'procedure DoIt(break: boolean);',
  3834. 'begin',
  3835. ' if break then ;',
  3836. 'end;',
  3837. 'var',
  3838. ' break: boolean;',
  3839. 'begin',
  3840. ' if break then ;']);
  3841. ConvertProgram;
  3842. CheckSource('TestBreakAsVar',
  3843. LinesToStr([ // statements
  3844. 'this.DoIt = function (Break) {',
  3845. ' if (Break) ;',
  3846. '};',
  3847. 'this.Break = false;',
  3848. '']),
  3849. LinesToStr([
  3850. 'if($mod.Break) ;',
  3851. '']));
  3852. end;
  3853. procedure TTestModule.TestContinue;
  3854. begin
  3855. StartProgram(false);
  3856. Add('var i: longint;');
  3857. Add('begin');
  3858. Add(' repeat');
  3859. Add(' continue;');
  3860. Add(' until true;');
  3861. Add(' while true do');
  3862. Add(' continue;');
  3863. Add(' for i:=1 to 2 do');
  3864. Add(' continue;');
  3865. ConvertProgram;
  3866. CheckSource('TestContinue',
  3867. LinesToStr([ // statements
  3868. 'this.i = 0;'
  3869. ]),
  3870. LinesToStr([
  3871. 'do {',
  3872. ' continue;',
  3873. '} while (!true);',
  3874. 'while (true) continue;',
  3875. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3876. '']));
  3877. end;
  3878. procedure TTestModule.TestProc_External;
  3879. begin
  3880. StartProgram(false);
  3881. Add('procedure Foo; external name ''console.log'';');
  3882. Add('function Bar: longint; external name ''get.item'';');
  3883. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3884. Add('var');
  3885. Add(' i: longint;');
  3886. Add('begin');
  3887. Add(' Foo;');
  3888. Add(' i:=Bar;');
  3889. Add(' i:=Bla(''abc'');');
  3890. ConvertProgram;
  3891. CheckSource('TestProc_External',
  3892. LinesToStr([ // statements
  3893. 'this.i = 0;'
  3894. ]),
  3895. LinesToStr([
  3896. 'console.log();',
  3897. '$mod.i = get.item();',
  3898. '$mod.i = apply.something("abc");'
  3899. ]));
  3900. end;
  3901. procedure TTestModule.TestProc_ExternalOtherUnit;
  3902. begin
  3903. AddModuleWithIntfImplSrc('unit2.pas',
  3904. LinesToStr([
  3905. 'procedure Now; external name ''Date.now'';',
  3906. 'procedure DoIt;'
  3907. ]),
  3908. 'procedure doit; begin end;');
  3909. StartUnit(true);
  3910. Add('interface');
  3911. Add('uses unit2;');
  3912. Add('implementation');
  3913. Add('begin');
  3914. Add(' now;');
  3915. Add(' now();');
  3916. Add(' uNit2.now;');
  3917. Add(' uNit2.now();');
  3918. Add(' doit;');
  3919. Add(' uNit2.doit;');
  3920. ConvertUnit;
  3921. CheckSource('TestProc_ExternalOtherUnit',
  3922. LinesToStr([
  3923. '']),
  3924. LinesToStr([
  3925. 'Date.now();',
  3926. 'Date.now();',
  3927. 'Date.now();',
  3928. 'Date.now();',
  3929. 'pas.unit2.DoIt();',
  3930. 'pas.unit2.DoIt();',
  3931. '']));
  3932. end;
  3933. procedure TTestModule.TestProc_Asm;
  3934. begin
  3935. StartProgram(false);
  3936. Add([
  3937. '{$mode delphi}',
  3938. 'function DoIt: longint;',
  3939. 'begin;',
  3940. ' asm',
  3941. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3942. ' end;',
  3943. ' asm console.log(); end;',
  3944. ' asm',
  3945. ' s = "'' ";',
  3946. ' s = ''" '';',
  3947. ' s = s + "world" + "''";',
  3948. ' // end',
  3949. ' s = ''end'';',
  3950. ' s = "end";',
  3951. ' s = "foo\"bar";',
  3952. ' s = ''a\''b'';',
  3953. ' s = `${expr}\`-"-''-`;',
  3954. ' s = `multi',
  3955. 'line`;',
  3956. ' end;',
  3957. 'end;',
  3958. 'procedure Fly;',
  3959. 'asm',
  3960. ' return;',
  3961. 'end;',
  3962. 'begin']);
  3963. ConvertProgram;
  3964. CheckSource('TestProc_Asm',
  3965. LinesToStr([ // statements
  3966. 'this.DoIt = function () {',
  3967. ' var Result = 0;',
  3968. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3969. ' console.log();',
  3970. ' s = "'' ";',
  3971. ' s = ''" '';',
  3972. ' s = s + "world" + "''";',
  3973. ' // end',
  3974. ' s = ''end'';',
  3975. ' s = "end";',
  3976. ' s = "foo\"bar";',
  3977. ' s = ''a\''b'';',
  3978. ' s = `${expr}\`-"-''-`;',
  3979. ' s = `multi',
  3980. 'line`;',
  3981. ' return Result;',
  3982. '};',
  3983. 'this.Fly = function () {',
  3984. ' return;',
  3985. '};',
  3986. '']),
  3987. LinesToStr([
  3988. ''
  3989. ]));
  3990. end;
  3991. procedure TTestModule.TestProc_AsmSubBlock;
  3992. begin
  3993. StartProgram(true,[supTObject]);
  3994. Add([
  3995. '{$mode delphi}',
  3996. 'type',
  3997. ' TBird = class end;',
  3998. 'procedure Run(w: word);',
  3999. 'begin;',
  4000. ' if true then asm console.log(); end;',
  4001. ' if w>3 then asm',
  4002. ' var a = w+1;',
  4003. ' w = a+3;',
  4004. ' end;',
  4005. ' while (w>7) do asm',
  4006. ' w+=3; w*=2;',
  4007. ' end;',
  4008. ' try',
  4009. ' except',
  4010. ' on E: TBird do',
  4011. ' asm console.log(E); end;',
  4012. ' on E: TObject do',
  4013. ' asm var i=3; i--; end;',
  4014. ' else asm Fly; High; end;',
  4015. ' end;',
  4016. 'end;',
  4017. 'begin']);
  4018. ConvertProgram;
  4019. CheckSource('TestProc_AsmSubBlock',
  4020. LinesToStr([ // statements
  4021. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4022. '});',
  4023. 'this.Run = function (w) {',
  4024. ' if (true) console.log();',
  4025. ' if (w > 3) {',
  4026. ' var a = w+1;',
  4027. ' w = a+3;',
  4028. ' };',
  4029. ' while (w > 7) {',
  4030. ' w+=3; w*=2;',
  4031. ' };',
  4032. ' try {} catch ($e) {',
  4033. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4034. ' var E = $e;',
  4035. ' console.log(E);',
  4036. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4037. ' var E = $e;',
  4038. ' var i=3; i--;',
  4039. ' } else {',
  4040. ' Fly; High;',
  4041. ' }',
  4042. ' };',
  4043. '};',
  4044. '']),
  4045. LinesToStr([
  4046. ''
  4047. ]));
  4048. end;
  4049. procedure TTestModule.TestProc_Assembler;
  4050. begin
  4051. StartProgram(false);
  4052. Add('function DoIt: longint; assembler;');
  4053. Add('asm');
  4054. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4055. Add('end;');
  4056. Add('begin');
  4057. ConvertProgram;
  4058. CheckSource('TestProc_Assembler',
  4059. LinesToStr([ // statements
  4060. 'this.DoIt = function () {',
  4061. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4062. '};'
  4063. ]),
  4064. LinesToStr([
  4065. ''
  4066. ]));
  4067. end;
  4068. procedure TTestModule.TestProc_VarParam;
  4069. begin
  4070. StartProgram(false);
  4071. Add('type integer = longint;');
  4072. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4073. Add('var vJ: integer;');
  4074. Add('begin');
  4075. Add(' vg:=vg+1;');
  4076. Add(' vj:=vh+2;');
  4077. Add(' vi:=vi+3;');
  4078. Add(' doit(vg,vg,vg);');
  4079. Add(' doit(vh,vh,vj);');
  4080. Add(' doit(vi,vi,vi);');
  4081. Add(' doit(vj,vj,vj);');
  4082. Add('end;');
  4083. Add('var i: integer;');
  4084. Add('begin');
  4085. Add(' doit(i,i,i);');
  4086. ConvertProgram;
  4087. CheckSource('TestProc_VarParam',
  4088. LinesToStr([ // statements
  4089. 'this.DoIt = function (vG,vH,vI) {',
  4090. ' var vJ = 0;',
  4091. ' vG = vG + 1;',
  4092. ' vJ = vH + 2;',
  4093. ' vI.set(vI.get()+3);',
  4094. ' $mod.DoIt(vG, vG, {',
  4095. ' get: function () {',
  4096. ' return vG;',
  4097. ' },',
  4098. ' set: function (v) {',
  4099. ' vG = v;',
  4100. ' }',
  4101. ' });',
  4102. ' $mod.DoIt(vH, vH, {',
  4103. ' get: function () {',
  4104. ' return vJ;',
  4105. ' },',
  4106. ' set: function (v) {',
  4107. ' vJ = v;',
  4108. ' }',
  4109. ' });',
  4110. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4111. ' $mod.DoIt(vJ, vJ, {',
  4112. ' get: function () {',
  4113. ' return vJ;',
  4114. ' },',
  4115. ' set: function (v) {',
  4116. ' vJ = v;',
  4117. ' }',
  4118. ' });',
  4119. '};',
  4120. 'this.i = 0;'
  4121. ]),
  4122. LinesToStr([
  4123. '$mod.DoIt($mod.i,$mod.i,{',
  4124. ' p: $mod,',
  4125. ' get: function () {',
  4126. ' return this.p.i;',
  4127. ' },',
  4128. ' set: function (v) {',
  4129. ' this.p.i = v;',
  4130. ' }',
  4131. '});'
  4132. ]));
  4133. end;
  4134. procedure TTestModule.TestProc_VarParamString;
  4135. begin
  4136. StartProgram(false);
  4137. Add(['type TCaption = string;',
  4138. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4139. 'var c: char;',
  4140. 'begin',
  4141. ' va[1]:=c;',
  4142. ' vb[2]:=c;',
  4143. ' vc[3]:=c;',
  4144. 'end;',
  4145. 'begin']);
  4146. ConvertProgram;
  4147. CheckSource('TestProc_VarParamString',
  4148. LinesToStr([ // statements
  4149. 'this.DoIt = function (vA,vB,vC) {',
  4150. ' var c = "";',
  4151. ' vA = rtl.setCharAt(vA, 0, c);',
  4152. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4153. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4154. '};',
  4155. '']),
  4156. LinesToStr([
  4157. ]));
  4158. end;
  4159. procedure TTestModule.TestProc_VarParamV;
  4160. begin
  4161. StartProgram(false);
  4162. Add([
  4163. 'procedure Inc2(var i: longint);',
  4164. 'begin',
  4165. ' i:=i+2;',
  4166. 'end;',
  4167. 'procedure DoIt(v: longint);',
  4168. 'var p: array of longint;',
  4169. 'begin',
  4170. ' Inc2(v);',
  4171. ' Inc2(p[v]);',
  4172. 'end;',
  4173. 'begin']);
  4174. ConvertProgram;
  4175. CheckSource('TestProc_VarParamV',
  4176. LinesToStr([ // statements
  4177. 'this.Inc2 = function (i) {',
  4178. ' i.set(i.get()+2);',
  4179. '};',
  4180. 'this.DoIt = function (v) {',
  4181. ' var p = [];',
  4182. ' $mod.Inc2({get: function () {',
  4183. ' return v;',
  4184. ' }, set: function (w) {',
  4185. ' v = w;',
  4186. ' }});',
  4187. ' $mod.Inc2({',
  4188. ' a: v,',
  4189. ' p: p,',
  4190. ' get: function () {',
  4191. ' return this.p[this.a];',
  4192. ' },',
  4193. ' set: function (v) {',
  4194. ' this.p[this.a] = v;',
  4195. ' }',
  4196. ' });',
  4197. '};',
  4198. '']),
  4199. LinesToStr([
  4200. '']));
  4201. end;
  4202. procedure TTestModule.TestProc_Overload;
  4203. begin
  4204. StartProgram(false);
  4205. Add('procedure DoIt(vI: longint); begin end;');
  4206. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4207. Add('procedure DoIt(vD: double); begin end;');
  4208. Add('begin');
  4209. Add(' DoIt(1);');
  4210. Add(' DoIt(2,3);');
  4211. Add(' DoIt(4.5);');
  4212. ConvertProgram;
  4213. CheckSource('TestProcedureOverload',
  4214. LinesToStr([ // statements
  4215. 'this.DoIt = function (vI) {',
  4216. '};',
  4217. 'this.DoIt$1 = function (vI, vJ) {',
  4218. '};',
  4219. 'this.DoIt$2 = function (vD) {',
  4220. '};',
  4221. '']),
  4222. LinesToStr([
  4223. '$mod.DoIt(1);',
  4224. '$mod.DoIt$1(2, 3);',
  4225. '$mod.DoIt$2(4.5);',
  4226. '']));
  4227. end;
  4228. procedure TTestModule.TestProc_OverloadForward;
  4229. begin
  4230. StartProgram(false);
  4231. Add('procedure DoIt(vI: longint); forward;');
  4232. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4233. Add('procedure doit(vi: longint); begin end;');
  4234. Add('begin');
  4235. Add(' doit(1);');
  4236. Add(' doit(2,3);');
  4237. ConvertProgram;
  4238. CheckSource('TestProcedureOverloadForward',
  4239. LinesToStr([ // statements
  4240. 'this.DoIt$1 = function (vI, vJ) {',
  4241. '};',
  4242. 'this.DoIt = function (vI) {',
  4243. '};',
  4244. '']),
  4245. LinesToStr([
  4246. '$mod.DoIt(1);',
  4247. '$mod.DoIt$1(2, 3);',
  4248. '']));
  4249. end;
  4250. procedure TTestModule.TestProc_OverloadIntfImpl;
  4251. begin
  4252. StartUnit(false);
  4253. Add('interface');
  4254. Add('procedure DoIt(vI: longint);');
  4255. Add('procedure DoIt(vI, vJ: longint);');
  4256. Add('implementation');
  4257. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4258. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4259. Add('procedure DoIt(vi: longint); begin end;');
  4260. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4261. Add('procedure DoIt(vi, vj: longint); begin end;');
  4262. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4263. Add('begin');
  4264. Add(' doit(1);');
  4265. Add(' doit(2,3);');
  4266. Add(' doit(4,5,6);');
  4267. Add(' doit(7,8,9,10);');
  4268. Add(' doit(11,12,13,14,15);');
  4269. ConvertUnit;
  4270. CheckSource('TestProcedureOverloadUnit',
  4271. LinesToStr([ // statements
  4272. 'var $impl = $mod.$impl;',
  4273. 'this.DoIt = function (vI) {',
  4274. '};',
  4275. 'this.DoIt$1 = function (vI, vJ) {',
  4276. '};',
  4277. '']),
  4278. LinesToStr([ // this.$init
  4279. '$mod.DoIt(1);',
  4280. '$mod.DoIt$1(2, 3);',
  4281. '$impl.DoIt$3(4,5,6);',
  4282. '$impl.DoIt$4(7,8,9,10);',
  4283. '$impl.DoIt$2(11,12,13,14,15);',
  4284. '']),
  4285. LinesToStr([ // implementation
  4286. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4287. '};',
  4288. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4289. '};',
  4290. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4291. '};',
  4292. '']));
  4293. end;
  4294. procedure TTestModule.TestProc_OverloadNested;
  4295. begin
  4296. StartProgram(false);
  4297. Add([
  4298. 'procedure doit(vA: longint);',
  4299. ' procedure DoIt(vA, vB: longint); overload;',
  4300. ' begin',
  4301. ' doit(1);',
  4302. ' doit(1,2);',
  4303. ' end;',
  4304. ' procedure doit(vA, vB, vC: longint);',
  4305. ' begin',
  4306. ' doit(1);',
  4307. ' doit(1,2);',
  4308. ' doit(1,2,3);',
  4309. ' end;',
  4310. 'begin',
  4311. ' doit(1);',
  4312. ' doit(1,2);',
  4313. ' doit(1,2,3);',
  4314. 'end;',
  4315. 'begin // main',
  4316. ' doit(1);']);
  4317. ConvertProgram;
  4318. CheckSource('TestProcedureOverloadNested',
  4319. LinesToStr([ // statements
  4320. 'this.doit = function (vA) {',
  4321. ' function DoIt$1(vA, vB) {',
  4322. ' $mod.doit(1);',
  4323. ' DoIt$1(1, 2);',
  4324. ' };',
  4325. ' function doit$2(vA, vB, vC) {',
  4326. ' $mod.doit(1);',
  4327. ' DoIt$1(1, 2);',
  4328. ' doit$2(1, 2, 3);',
  4329. ' };',
  4330. ' $mod.doit(1);',
  4331. ' DoIt$1(1, 2);',
  4332. ' doit$2(1, 2, 3);',
  4333. '};',
  4334. '']),
  4335. LinesToStr([
  4336. '$mod.doit(1);',
  4337. '']));
  4338. end;
  4339. procedure TTestModule.TestProc_OverloadNestedForward;
  4340. begin
  4341. StartProgram(false);
  4342. Add([
  4343. 'procedure DoIt(vA: longint); overload; forward;',
  4344. 'procedure DoIt(vB, vC: longint); overload;',
  4345. 'begin // 2 param overload',
  4346. ' doit(1);',
  4347. ' doit(1,2);',
  4348. 'end;',
  4349. 'procedure doit(vA: longint);',
  4350. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4351. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4352. ' begin // 4 param overload',
  4353. ' doit(1);',
  4354. ' doit(1,2);',
  4355. ' doit(1,2,3);',
  4356. ' doit(1,2,3,4);',
  4357. ' end;',
  4358. ' procedure doit(vA, vB, vC: longint);',
  4359. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4360. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4361. ' begin // 6 param overload',
  4362. ' doit(1);',
  4363. ' doit(1,2);',
  4364. ' doit(1,2,3);',
  4365. ' doit(1,2,3,4);',
  4366. ' doit(1,2,3,4,5);',
  4367. ' doit(1,2,3,4,5,6);',
  4368. ' end;',
  4369. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4370. ' begin // 5 param overload',
  4371. ' doit(1);',
  4372. ' doit(1,2);',
  4373. ' doit(1,2,3);',
  4374. ' doit(1,2,3,4);',
  4375. ' doit(1,2,3,4,5);',
  4376. ' doit(1,2,3,4,5,6);',
  4377. ' end;',
  4378. ' begin // 3 param overload',
  4379. ' doit(1);',
  4380. ' doit(1,2);',
  4381. ' doit(1,2,3);',
  4382. ' doit(1,2,3,4);',
  4383. ' doit(1,2,3,4,5);',
  4384. ' doit(1,2,3,4,5,6);',
  4385. ' end;',
  4386. 'begin // 1 param overload',
  4387. ' doit(1);',
  4388. ' doit(1,2);',
  4389. ' doit(1,2,3);',
  4390. ' doit(1,2,3,4);',
  4391. 'end;',
  4392. 'begin // main',
  4393. ' doit(1);',
  4394. ' doit(1,2);']);
  4395. ConvertProgram;
  4396. CheckSource('TestProc_OverloadNestedForward',
  4397. LinesToStr([ // statements
  4398. 'this.DoIt$1 = function (vB, vC) {',
  4399. ' $mod.DoIt(1);',
  4400. ' $mod.DoIt$1(1, 2);',
  4401. '};',
  4402. 'this.DoIt = function (vA) {',
  4403. ' function DoIt$3(vA, vB, vC, vD) {',
  4404. ' $mod.DoIt(1);',
  4405. ' $mod.DoIt$1(1, 2);',
  4406. ' DoIt$2(1, 2, 3);',
  4407. ' DoIt$3(1, 2, 3, 4);',
  4408. ' };',
  4409. ' function DoIt$2(vA, vB, vC) {',
  4410. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4411. ' $mod.DoIt(1);',
  4412. ' $mod.DoIt$1(1, 2);',
  4413. ' DoIt$2(1, 2, 3);',
  4414. ' DoIt$3(1, 2, 3, 4);',
  4415. ' DoIt$4(1, 2, 3, 4, 5);',
  4416. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4417. ' };',
  4418. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4419. ' $mod.DoIt(1);',
  4420. ' $mod.DoIt$1(1, 2);',
  4421. ' DoIt$2(1, 2, 3);',
  4422. ' DoIt$3(1, 2, 3, 4);',
  4423. ' DoIt$4(1, 2, 3, 4, 5);',
  4424. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4425. ' };',
  4426. ' $mod.DoIt(1);',
  4427. ' $mod.DoIt$1(1, 2);',
  4428. ' DoIt$2(1, 2, 3);',
  4429. ' DoIt$3(1, 2, 3, 4);',
  4430. ' DoIt$4(1, 2, 3, 4, 5);',
  4431. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4432. ' };',
  4433. ' $mod.DoIt(1);',
  4434. ' $mod.DoIt$1(1, 2);',
  4435. ' DoIt$2(1, 2, 3);',
  4436. ' DoIt$3(1, 2, 3, 4);',
  4437. '};',
  4438. '']),
  4439. LinesToStr([
  4440. '$mod.DoIt(1);',
  4441. '$mod.DoIt$1(1, 2);',
  4442. '']));
  4443. end;
  4444. procedure TTestModule.TestProc_OverloadUnitCycle;
  4445. begin
  4446. AddModuleWithIntfImplSrc('Unit2.pas',
  4447. LinesToStr([
  4448. 'type',
  4449. ' TObject = class',
  4450. ' procedure DoIt(b: boolean); virtual; abstract;',
  4451. ' procedure DoIt(i: longint); virtual; abstract;',
  4452. ' end;',
  4453. '']),
  4454. 'uses test1;');
  4455. StartUnit(true);
  4456. Add([
  4457. 'interface',
  4458. 'uses unit2;',
  4459. 'type',
  4460. ' TEagle = class(TObject)',
  4461. ' procedure DoIt(b: boolean); override;',
  4462. ' procedure DoIt(i: longint); override;',
  4463. ' end;',
  4464. 'implementation',
  4465. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4466. 'procedure TEagle.DoIt(i: longint); begin end;',
  4467. '']);
  4468. ConvertUnit;
  4469. CheckSource('TestProc_OverloadUnitCycle',
  4470. LinesToStr([ // statements
  4471. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4472. ' this.DoIt = function (b) {',
  4473. ' };',
  4474. ' this.DoIt$1 = function (i) {',
  4475. ' };',
  4476. '});',
  4477. '']),
  4478. '',
  4479. LinesToStr([
  4480. '']));
  4481. end;
  4482. procedure TTestModule.TestProc_Varargs;
  4483. begin
  4484. StartProgram(false);
  4485. Add([
  4486. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4487. 'procedure ProcB; varargs; external name ''ProcB'';',
  4488. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4489. 'function GetIt: longint; begin end;',
  4490. 'begin',
  4491. ' ProcA(1);',
  4492. ' ProcA(1,2);',
  4493. ' ProcA(1,2.0);',
  4494. ' ProcA(1,2,3);',
  4495. ' ProcA(1,''2'');',
  4496. ' ProcA(2,'''');',
  4497. ' ProcA(3,false);',
  4498. ' ProcB;',
  4499. ' ProcB();',
  4500. ' ProcB(4);',
  4501. ' ProcB(''foo'');',
  4502. ' ProcC;',
  4503. ' ProcC();',
  4504. ' ProcC(4);',
  4505. ' ProcC(5,''foo'');',
  4506. ' ProcB(GetIt);',
  4507. ' ProcB(GetIt());',
  4508. ' ProcB(GetIt,GetIt());']);
  4509. ConvertProgram;
  4510. CheckSource('TestProc_Varargs',
  4511. LinesToStr([ // statements
  4512. 'this.GetIt = function () {',
  4513. ' var Result = 0;',
  4514. ' return Result;',
  4515. '};',
  4516. '']),
  4517. LinesToStr([
  4518. 'ProcA(1);',
  4519. 'ProcA(1, 2);',
  4520. 'ProcA(1, 2.0);',
  4521. 'ProcA(1, 2, 3);',
  4522. 'ProcA(1, "2");',
  4523. 'ProcA(2, "");',
  4524. 'ProcA(3, false);',
  4525. 'ProcB();',
  4526. 'ProcB();',
  4527. 'ProcB(4);',
  4528. 'ProcB("foo");',
  4529. 'ProcC(17);',
  4530. 'ProcC(17);',
  4531. 'ProcC(4);',
  4532. 'ProcC(5, "foo");',
  4533. 'ProcB($mod.GetIt());',
  4534. 'ProcB($mod.GetIt());',
  4535. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4536. '']));
  4537. end;
  4538. procedure TTestModule.TestProc_ConstOrder;
  4539. begin
  4540. StartProgram(false);
  4541. Add([
  4542. 'const A = 3;',
  4543. 'const B = A+1;',
  4544. 'procedure DoIt;',
  4545. 'const C = A+1;',
  4546. 'const D = B+1;',
  4547. 'const E = D+C+B+A;',
  4548. 'begin',
  4549. 'end;',
  4550. 'begin'
  4551. ]);
  4552. ConvertProgram;
  4553. CheckSource('TestProc_ConstOrder',
  4554. LinesToStr([ // statements
  4555. 'this.A = 3;',
  4556. 'this.B = 3 + 1;',
  4557. 'var C = 3 + 1;',
  4558. 'var D = 4 + 1;',
  4559. 'var E = 5 + 4 + 4 + 3;',
  4560. 'this.DoIt = function () {',
  4561. '};',
  4562. '']),
  4563. LinesToStr([
  4564. ''
  4565. ]));
  4566. end;
  4567. procedure TTestModule.TestProc_DuplicateConst;
  4568. begin
  4569. StartProgram(false);
  4570. Add([
  4571. 'const A = 1;',
  4572. 'procedure DoIt;',
  4573. 'const A = 2;',
  4574. ' procedure SubIt;',
  4575. ' const A = 21;',
  4576. ' begin',
  4577. ' end;',
  4578. 'begin',
  4579. 'end;',
  4580. 'procedure DoSome;',
  4581. 'const A = 3;',
  4582. 'begin',
  4583. 'end;',
  4584. 'begin'
  4585. ]);
  4586. ConvertProgram;
  4587. CheckSource('TestProc_DuplicateConst',
  4588. LinesToStr([ // statements
  4589. 'this.A = 1;',
  4590. 'var A$1 = 2;',
  4591. 'var A$2 = 21;',
  4592. 'this.DoIt = function () {',
  4593. ' function SubIt() {',
  4594. ' };',
  4595. '};',
  4596. 'var A$3 = 3;',
  4597. 'this.DoSome = function () {',
  4598. '};',
  4599. '']),
  4600. LinesToStr([
  4601. ''
  4602. ]));
  4603. end;
  4604. procedure TTestModule.TestProc_LocalVarAbsolute;
  4605. begin
  4606. StartProgram(false);
  4607. Add([
  4608. 'type',
  4609. ' TObject = class',
  4610. ' Index: longint;',
  4611. ' procedure DoAbs(Item: pointer);',
  4612. ' end;',
  4613. 'procedure TObject.DoAbs(Item: pointer);',
  4614. 'var',
  4615. ' o: TObject absolute Item;',
  4616. 'begin',
  4617. ' if o.Index<o.Index then o.Index:=o.Index;',
  4618. 'end;',
  4619. 'procedure DoIt(i: longint; p: pointer);',
  4620. 'var',
  4621. ' d: double absolute i;',
  4622. ' s: string absolute d;',
  4623. ' oi: TObject absolute i;',
  4624. ' op: TObject absolute p;',
  4625. 'begin',
  4626. ' if d=d then d:=d;',
  4627. ' if s=s then s:=s;',
  4628. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4629. ' if op.Index=op.Index then op.Index:=op.Index;',
  4630. 'end;',
  4631. 'begin']);
  4632. ConvertProgram;
  4633. CheckSource('TestProc_LocalVarAbsolute',
  4634. LinesToStr([ // statements
  4635. 'rtl.createClass(this, "TObject", null, function () {',
  4636. ' this.$init = function () {',
  4637. ' this.Index = 0;',
  4638. ' };',
  4639. ' this.$final = function () {',
  4640. ' };',
  4641. ' this.DoAbs = function (Item) {',
  4642. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4643. ' };',
  4644. '});',
  4645. 'this.DoIt = function (i, p) {',
  4646. ' if (i === i) i = i;',
  4647. ' if (i === i) i = i;',
  4648. ' if (i.Index < i.Index) i.Index = i.Index;',
  4649. ' if (p.Index === p.Index) p.Index = p.Index;',
  4650. '};'
  4651. ]),
  4652. LinesToStr([
  4653. ]));
  4654. end;
  4655. procedure TTestModule.TestProc_LocalVarInit;
  4656. begin
  4657. StartProgram(false);
  4658. Add([
  4659. 'type TBytes = array of byte;',
  4660. 'procedure DoIt;',
  4661. 'const c = 4;',
  4662. 'var',
  4663. ' b: byte = 1;',
  4664. ' w: word = 2+c;',
  4665. ' p: pointer = nil;',
  4666. ' Buffer: TBytes = nil;',
  4667. 'begin',
  4668. 'end;',
  4669. 'begin']);
  4670. ConvertProgram;
  4671. CheckSource('TestProc_LocalVarInit',
  4672. LinesToStr([ // statements
  4673. 'var c = 4;',
  4674. 'this.DoIt = function () {',
  4675. ' var b = 1;',
  4676. ' var w = 2 + 4;',
  4677. ' var p = null;',
  4678. ' var Buffer = [];',
  4679. '};',
  4680. '']),
  4681. LinesToStr([
  4682. ]));
  4683. end;
  4684. procedure TTestModule.TestProc_ReservedWords;
  4685. begin
  4686. StartProgram(false);
  4687. Add([
  4688. 'procedure Date(ArrayBuffer: longint);',
  4689. 'const',
  4690. ' NaN: longint = 3;',
  4691. 'var',
  4692. ' &Boolean: longint;',
  4693. ' procedure Error(ArrayBuffer: longint);',
  4694. ' begin',
  4695. ' end;',
  4696. 'begin',
  4697. ' Nan:=&bOolean;',
  4698. 'end;',
  4699. 'begin',
  4700. ' Date(1);']);
  4701. ConvertProgram;
  4702. CheckSource('TestProc_ReservedWords',
  4703. LinesToStr([ // statements
  4704. 'var naN = 3;',
  4705. 'this.Date = function (arrayBuffer) {',
  4706. ' var boolean = 0;',
  4707. ' function error(arrayBuffer) {',
  4708. ' };',
  4709. ' naN = boolean;',
  4710. '};',
  4711. '']),
  4712. LinesToStr([
  4713. ' $mod.Date(1);'
  4714. ]));
  4715. end;
  4716. procedure TTestModule.TestProc_ConstRefWord;
  4717. begin
  4718. StartProgram(false);
  4719. Add([
  4720. 'procedure Run(constref w: word);',
  4721. 'var l: word;',
  4722. 'begin',
  4723. ' l:=w;',
  4724. ' Run(w);',
  4725. ' Run(l);',
  4726. 'end;',
  4727. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4728. 'begin',
  4729. ' Run(a);',
  4730. ' Run(b);',
  4731. ' Run(c);',
  4732. ' Run(d);',
  4733. ' Run(e);',
  4734. 'end;',
  4735. 'begin',
  4736. ' Run(1);']);
  4737. ConvertProgram;
  4738. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4739. CheckSource('TestProc_ConstRefWord',
  4740. LinesToStr([ // statements
  4741. 'this.Run = function (w) {',
  4742. ' var l = 0;',
  4743. ' l = w;',
  4744. ' $mod.Run(w);',
  4745. ' $mod.Run(l);',
  4746. '};',
  4747. 'this.Fly = function (a, b, c, d, e) {',
  4748. ' $mod.Run(a);',
  4749. ' $mod.Run(b.get());',
  4750. ' $mod.Run(c.get());',
  4751. ' $mod.Run(d);',
  4752. ' $mod.Run(e);',
  4753. '};',
  4754. '']),
  4755. LinesToStr([
  4756. '$mod.Run(1);'
  4757. ]));
  4758. end;
  4759. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4760. begin
  4761. StartProgram(false);
  4762. Add([
  4763. '{$mode objfpc}',
  4764. 'type',
  4765. ' TFunc = reference to function(x: word): word;',
  4766. 'var Func: TFunc;',
  4767. 'procedure DoIt(a: word);',
  4768. 'begin',
  4769. ' Func:=function(b:word): word',
  4770. ' begin',
  4771. ' Result:=a+b;',
  4772. ' exit(b);',
  4773. ' exit(Result);',
  4774. ' end;',// test semicolon
  4775. ' a:=3;',
  4776. 'end;',
  4777. 'begin',
  4778. ' Func:=function(c:word):word begin',
  4779. ' Result:=3+c;',
  4780. ' exit(c);',
  4781. ' exit(Result);',
  4782. ' end;']);
  4783. ConvertProgram;
  4784. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4785. LinesToStr([ // statements
  4786. 'this.Func = null;',
  4787. 'this.DoIt = function (a) {',
  4788. ' $mod.Func = function (b) {',
  4789. ' var Result = 0;',
  4790. ' Result = a + b;',
  4791. ' return b;',
  4792. ' return Result;',
  4793. ' return Result;',
  4794. ' };',
  4795. ' a = 3;',
  4796. '};',
  4797. '']),
  4798. LinesToStr([
  4799. '$mod.Func = function (c) {',
  4800. ' var Result = 0;',
  4801. ' Result = 3 + c;',
  4802. ' return c;',
  4803. ' return Result;',
  4804. ' return Result;',
  4805. '};',
  4806. '']));
  4807. end;
  4808. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4809. begin
  4810. StartProgram(false);
  4811. Add([
  4812. '{$mode delphi}',
  4813. 'type',
  4814. ' TProc = reference to procedure(x: word);',
  4815. 'procedure DoIt(a: word);',
  4816. 'var Proc: TProc;',
  4817. 'begin',
  4818. ' Proc:=procedure(b:word) begin end;',
  4819. 'end;',
  4820. 'var Proc: TProc;',
  4821. 'begin',
  4822. ' Proc:=procedure(c:word) begin end;',
  4823. '']);
  4824. ConvertProgram;
  4825. CheckSource('TestAnonymousProc_Assign_Delphi',
  4826. LinesToStr([ // statements
  4827. 'this.DoIt = function (a) {',
  4828. ' var Proc = null;',
  4829. ' Proc = function (b) {',
  4830. ' };',
  4831. '};',
  4832. 'this.Proc = null;',
  4833. '']),
  4834. LinesToStr([
  4835. '$mod.Proc = function (c) {',
  4836. '};',
  4837. '']));
  4838. end;
  4839. procedure TTestModule.TestAnonymousProc_Arg;
  4840. begin
  4841. StartProgram(false);
  4842. Add([
  4843. 'type',
  4844. ' TProc = reference to procedure;',
  4845. ' TFunc = reference to function(x: word): word;',
  4846. 'procedure DoMore(f,g: TProc);',
  4847. 'begin',
  4848. 'end;',
  4849. 'procedure DoOdd(v: jsvalue);',
  4850. 'begin',
  4851. 'end;',
  4852. 'procedure DoIt(f: TFunc);',
  4853. 'begin',
  4854. ' DoIt(function(b:word): word',
  4855. ' begin',
  4856. ' Result:=1+b;',
  4857. ' end);',
  4858. ' DoMore(procedure begin end, procedure begin end);',
  4859. ' DoOdd(procedure begin end);',
  4860. 'end;',
  4861. 'begin',
  4862. ' DoMore(procedure begin end,',
  4863. ' procedure assembler asm',
  4864. ' console.log("c");',
  4865. ' end);',
  4866. '']);
  4867. ConvertProgram;
  4868. CheckSource('TestAnonymousProc_Arg',
  4869. LinesToStr([ // statements
  4870. 'this.DoMore = function (f, g) {',
  4871. '};',
  4872. 'this.DoOdd = function (v) {',
  4873. '};',
  4874. 'this.DoIt = function (f) {',
  4875. ' $mod.DoIt(function (b) {',
  4876. ' var Result = 0;',
  4877. ' Result = 1 + b;',
  4878. ' return Result;',
  4879. ' });',
  4880. ' $mod.DoMore(function () {',
  4881. ' }, function () {',
  4882. ' });',
  4883. ' $mod.DoOdd(function () {',
  4884. ' });',
  4885. '};',
  4886. '']),
  4887. LinesToStr([
  4888. '$mod.DoMore(function () {',
  4889. '}, function () {',
  4890. ' console.log("c");',
  4891. '});',
  4892. '']));
  4893. end;
  4894. procedure TTestModule.TestAnonymousProc_Typecast;
  4895. begin
  4896. StartProgram(false);
  4897. Add([
  4898. 'type',
  4899. ' TProc = reference to procedure(w: word);',
  4900. ' TArr = array of word;',
  4901. ' TFuncArr = reference to function: TArr;',
  4902. 'procedure DoIt(p: TProc);',
  4903. 'var',
  4904. ' w: word;',
  4905. ' a: TArr;',
  4906. 'begin',
  4907. ' p:=TProc(procedure(b: smallint) begin end);',
  4908. ' a:=TFuncArr(function: TArr begin end)();',
  4909. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4910. 'end;',
  4911. 'begin']);
  4912. ConvertProgram;
  4913. CheckSource('TestAnonymousProc_Typecast',
  4914. LinesToStr([ // statements
  4915. 'this.DoIt = function (p) {',
  4916. ' var w = 0;',
  4917. ' var a = [];',
  4918. ' p = function (b) {',
  4919. ' };',
  4920. ' a = function () {',
  4921. ' var Result = [];',
  4922. ' return Result;',
  4923. ' }();',
  4924. ' w = function () {',
  4925. ' var Result = [];',
  4926. ' return Result;',
  4927. ' }()[3];',
  4928. '};',
  4929. '']),
  4930. LinesToStr([
  4931. '']));
  4932. end;
  4933. procedure TTestModule.TestAnonymousProc_With;
  4934. begin
  4935. StartProgram(false);
  4936. Add([
  4937. 'type',
  4938. ' TProc = reference to procedure(w: word);',
  4939. ' TObject = class',
  4940. ' b: boolean;',
  4941. ' end;',
  4942. 'var',
  4943. ' p: TProc;',
  4944. ' bird: TObject;',
  4945. 'begin',
  4946. ' with bird do',
  4947. ' p:=procedure(w: word)',
  4948. ' begin',
  4949. ' b:=w>2;',
  4950. ' end;',
  4951. '']);
  4952. ConvertProgram;
  4953. CheckSource('TestAnonymousProc_With',
  4954. LinesToStr([ // statements
  4955. 'rtl.createClass(this, "TObject", null, function () {',
  4956. ' this.$init = function () {',
  4957. ' this.b = false;',
  4958. ' };',
  4959. ' this.$final = function () {',
  4960. ' };',
  4961. '});',
  4962. 'this.p = null;',
  4963. 'this.bird = null;',
  4964. '']),
  4965. LinesToStr([
  4966. 'var $with = $mod.bird;',
  4967. '$mod.p = function (w) {',
  4968. ' $with.b = w > 2;',
  4969. '};',
  4970. '']));
  4971. end;
  4972. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4973. begin
  4974. StartProgram(false);
  4975. Add([
  4976. 'type',
  4977. ' TProc = reference to procedure;',
  4978. ' TObject = class',
  4979. ' b: boolean;',
  4980. ' end;',
  4981. 'procedure DoIt;',
  4982. 'var',
  4983. ' p: TProc;',
  4984. 'begin',
  4985. ' try',
  4986. ' except',
  4987. ' on E: TObject do',
  4988. ' p:=procedure',
  4989. ' begin',
  4990. ' E.b:=true;',
  4991. ' end;',
  4992. ' end;',
  4993. 'end;',
  4994. 'begin']);
  4995. ConvertProgram;
  4996. CheckSource('TestAnonymousProc_ExceptOn',
  4997. LinesToStr([ // statements
  4998. 'rtl.createClass(this, "TObject", null, function () {',
  4999. ' this.$init = function () {',
  5000. ' this.b = false;',
  5001. ' };',
  5002. ' this.$final = function () {',
  5003. ' };',
  5004. '});',
  5005. 'this.DoIt = function () {',
  5006. ' var p = null;',
  5007. ' try {} catch ($e) {',
  5008. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5009. ' var E = $e;',
  5010. ' p = function () {',
  5011. ' E.b = true;',
  5012. ' };',
  5013. ' } else throw $e',
  5014. ' };',
  5015. '};',
  5016. '']),
  5017. LinesToStr([
  5018. '']));
  5019. end;
  5020. procedure TTestModule.TestAnonymousProc_Nested;
  5021. begin
  5022. StartProgram(false);
  5023. Add([
  5024. 'type',
  5025. ' TProc = reference to procedure;',
  5026. ' TObject = class',
  5027. ' i: byte;',
  5028. ' procedure DoIt;',
  5029. ' end;',
  5030. 'procedure TObject.DoIt;',
  5031. 'var',
  5032. ' p: TProc;',
  5033. ' procedure Sub;',
  5034. ' begin',
  5035. ' p:=procedure',
  5036. ' begin',
  5037. ' i:=3;',
  5038. ' Self.i:=4;',
  5039. ' p:=procedure',
  5040. ' procedure SubSub;',
  5041. ' begin',
  5042. ' i:=13;',
  5043. ' Self.i:=14;',
  5044. ' end;',
  5045. ' begin',
  5046. ' i:=13;',
  5047. ' Self.i:=14;',
  5048. ' end;',
  5049. ' end;',
  5050. ' end;',
  5051. 'begin',
  5052. 'end;',
  5053. 'begin']);
  5054. ConvertProgram;
  5055. CheckSource('TestAnonymousProc_Nested',
  5056. LinesToStr([ // statements
  5057. 'rtl.createClass(this, "TObject", null, function () {',
  5058. ' this.$init = function () {',
  5059. ' this.i = 0;',
  5060. ' };',
  5061. ' this.$final = function () {',
  5062. ' };',
  5063. ' this.DoIt = function () {',
  5064. ' var $Self = this;',
  5065. ' var p = null;',
  5066. ' function Sub() {',
  5067. ' p = function () {',
  5068. ' $Self.i = 3;',
  5069. ' $Self.i = 4;',
  5070. ' p = function () {',
  5071. ' function SubSub() {',
  5072. ' $Self.i = 13;',
  5073. ' $Self.i = 14;',
  5074. ' };',
  5075. ' $Self.i = 13;',
  5076. ' $Self.i = 14;',
  5077. ' };',
  5078. ' };',
  5079. ' };',
  5080. ' };',
  5081. '});',
  5082. '']),
  5083. LinesToStr([
  5084. '']));
  5085. end;
  5086. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5087. begin
  5088. StartProgram(false);
  5089. Add([
  5090. 'type',
  5091. ' TProc = reference to procedure;',
  5092. 'function DoIt: TProc;',
  5093. ' function Sub: TProc;',
  5094. ' begin',
  5095. ' Result:=procedure',
  5096. ' begin',
  5097. ' Sub:=procedure',
  5098. ' procedure SubSub;',
  5099. ' begin',
  5100. ' Result:=nil;',
  5101. ' Sub:=nil;',
  5102. ' DoIt:=nil;',
  5103. ' end;',
  5104. ' begin',
  5105. ' Result:=nil;',
  5106. ' Sub:=nil;',
  5107. ' DoIt:=nil;',
  5108. ' end;',
  5109. ' end;',
  5110. ' end;',
  5111. 'begin',
  5112. 'end;',
  5113. 'begin']);
  5114. ConvertProgram;
  5115. CheckSource('TestAnonymousProc_NestedAssignResult',
  5116. LinesToStr([ // statements
  5117. 'this.DoIt = function () {',
  5118. ' var Result = null;',
  5119. ' function Sub() {',
  5120. ' var Result$1 = null;',
  5121. ' Result$1 = function () {',
  5122. ' Result$1 = function () {',
  5123. ' function SubSub() {',
  5124. ' Result$1 = null;',
  5125. ' Result$1 = null;',
  5126. ' Result = null;',
  5127. ' };',
  5128. ' Result$1 = null;',
  5129. ' Result$1 = null;',
  5130. ' Result = null;',
  5131. ' };',
  5132. ' };',
  5133. ' return Result$1;',
  5134. ' };',
  5135. ' return Result;',
  5136. '};',
  5137. '']),
  5138. LinesToStr([
  5139. '']));
  5140. end;
  5141. procedure TTestModule.TestAnonymousProc_Class;
  5142. begin
  5143. StartProgram(false);
  5144. Add([
  5145. 'type',
  5146. ' TProc = reference to procedure;',
  5147. ' TEvent = procedure of object;',
  5148. ' TObject = class',
  5149. ' Size: word;',
  5150. ' function GetIt: TProc;',
  5151. ' procedure DoIt; virtual; abstract;',
  5152. ' end;',
  5153. 'function TObject.GetIt: TProc;',
  5154. 'begin',
  5155. ' Result:=procedure',
  5156. ' var p: TEvent;',
  5157. ' begin',
  5158. ' Size:=Size;',
  5159. ' Size:=Self.Size;',
  5160. ' p:=@DoIt;',
  5161. ' p:[email protected];',
  5162. ' end;',
  5163. 'end;',
  5164. 'begin']);
  5165. ConvertProgram;
  5166. CheckSource('TestAnonymousProc_Class',
  5167. LinesToStr([ // statements
  5168. 'rtl.createClass(this, "TObject", null, function () {',
  5169. ' this.$init = function () {',
  5170. ' this.Size = 0;',
  5171. ' };',
  5172. ' this.$final = function () {',
  5173. ' };',
  5174. ' this.GetIt = function () {',
  5175. ' var $Self = this;',
  5176. ' var Result = null;',
  5177. ' Result = function () {',
  5178. ' var p = null;',
  5179. ' $Self.Size = $Self.Size;',
  5180. ' $Self.Size = $Self.Size;',
  5181. ' p = rtl.createCallback($Self, "DoIt");',
  5182. ' p = rtl.createCallback($Self, "DoIt");',
  5183. ' };',
  5184. ' return Result;',
  5185. ' };',
  5186. '});',
  5187. '']),
  5188. LinesToStr([
  5189. '']));
  5190. end;
  5191. procedure TTestModule.TestAnonymousProc_ForLoop;
  5192. begin
  5193. StartProgram(false);
  5194. Add([
  5195. 'type TProc = reference to procedure;',
  5196. 'procedure Foo(p: TProc);',
  5197. 'begin',
  5198. 'end;',
  5199. 'procedure DoIt;',
  5200. 'var i: word;',
  5201. ' a: word;',
  5202. 'begin',
  5203. ' for i:=1 to 10 do begin',
  5204. ' Foo(procedure begin a:=3; end);',
  5205. ' end;',
  5206. 'end;',
  5207. 'begin',
  5208. ' DoIt;']);
  5209. ConvertProgram;
  5210. CheckSource('TestAnonymousProc_ForLoop',
  5211. LinesToStr([ // statements
  5212. 'this.Foo = function (p) {',
  5213. '};',
  5214. 'this.DoIt = function () {',
  5215. ' var i = 0;',
  5216. ' var a = 0;',
  5217. ' for (i = 1; i <= 10; i++) {',
  5218. ' $mod.Foo(function () {',
  5219. ' a = 3;',
  5220. ' });',
  5221. ' };',
  5222. '};',
  5223. '']),
  5224. LinesToStr([
  5225. '$mod.DoIt();'
  5226. ]));
  5227. end;
  5228. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5229. begin
  5230. StartProgram(false);
  5231. Add([
  5232. '{$mode delphi}',
  5233. 'type',
  5234. ' TProc = reference to procedure;',
  5235. ' TFunc = reference to function(x: word): word;',
  5236. 'procedure Run;',
  5237. 'asm',
  5238. 'end;',
  5239. 'procedure Walk(p: TProc; f: TFunc);',
  5240. 'begin',
  5241. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5242. 'end;',
  5243. 'begin',
  5244. ' Walk(procedure',
  5245. ' asm',
  5246. ' console.log("a");',
  5247. ' end,',
  5248. ' function(x: word): word asm',
  5249. ' console.log("c");',
  5250. ' end);',
  5251. '']);
  5252. ConvertProgram;
  5253. CheckSource('TestAnonymousProc_AsmDelphi',
  5254. LinesToStr([ // statements
  5255. 'this.Run = function () {',
  5256. '};',
  5257. 'this.Walk = function (p, f) {',
  5258. ' $mod.Walk(function () {',
  5259. ' }, function (b) {',
  5260. ' return 1+b;',
  5261. ' });',
  5262. '};',
  5263. '']),
  5264. LinesToStr([
  5265. '$mod.Walk(function () {',
  5266. ' console.log("a");',
  5267. '}, function (x) {',
  5268. ' console.log("c");',
  5269. '});',
  5270. '']));
  5271. end;
  5272. procedure TTestModule.TestEnum_Name;
  5273. begin
  5274. StartProgram(false);
  5275. Add('type TMyEnum = (Red, Green, Blue);');
  5276. Add('var e: TMyEnum;');
  5277. Add('var f: TMyEnum = Blue;');
  5278. Add('begin');
  5279. Add(' e:=green;');
  5280. Add(' e:=default(TMyEnum);');
  5281. ConvertProgram;
  5282. CheckSource('TestEnum_Name',
  5283. LinesToStr([ // statements
  5284. 'this.TMyEnum = {',
  5285. ' "0":"Red",',
  5286. ' Red:0,',
  5287. ' "1":"Green",',
  5288. ' Green:1,',
  5289. ' "2":"Blue",',
  5290. ' Blue:2',
  5291. ' };',
  5292. 'this.e = 0;',
  5293. 'this.f = this.TMyEnum.Blue;'
  5294. ]),
  5295. LinesToStr([
  5296. '$mod.e=$mod.TMyEnum.Green;',
  5297. '$mod.e=$mod.TMyEnum.Red;'
  5298. ]));
  5299. end;
  5300. procedure TTestModule.TestEnum_Number;
  5301. begin
  5302. Converter.Options:=Converter.Options+[coEnumNumbers];
  5303. StartProgram(false);
  5304. Add('type TMyEnum = (Red, Green);');
  5305. Add('var');
  5306. Add(' e: TMyEnum;');
  5307. Add(' f: TMyEnum = Green;');
  5308. Add(' i: longint;');
  5309. Add('begin');
  5310. Add(' e:=green;');
  5311. Add(' i:=longint(e);');
  5312. ConvertProgram;
  5313. CheckSource('TestEnumNumber',
  5314. LinesToStr([ // statements
  5315. 'this.TMyEnum = {',
  5316. ' "0":"Red",',
  5317. ' Red:0,',
  5318. ' "1":"Green",',
  5319. ' Green:1',
  5320. ' };',
  5321. 'this.e = 0;',
  5322. 'this.f = 1;',
  5323. 'this.i = 0;'
  5324. ]),
  5325. LinesToStr([
  5326. '$mod.e=1;',
  5327. '$mod.i=$mod.e;'
  5328. ]));
  5329. end;
  5330. procedure TTestModule.TestEnum_ConstFail;
  5331. begin
  5332. StartProgram(false);
  5333. Add([
  5334. 'type TMyEnum = (Red = 100, Green = 101);',
  5335. 'var',
  5336. ' e: TMyEnum;',
  5337. ' f: TMyEnum = Green;',
  5338. 'begin',
  5339. ' e:=green;']);
  5340. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5341. ConvertProgram;
  5342. end;
  5343. procedure TTestModule.TestEnum_Functions;
  5344. begin
  5345. StartProgram(false);
  5346. Add([
  5347. 'type TMyEnum = (Red, Green);',
  5348. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5349. 'var',
  5350. ' v: longint;',
  5351. ' s: string;',
  5352. 'begin',
  5353. ' val(s,e,v);',
  5354. ' val(s,e,i);',
  5355. 'end;',
  5356. 'var',
  5357. ' e: TMyEnum;',
  5358. ' i: longint;',
  5359. ' s: string;',
  5360. ' b: boolean;',
  5361. 'begin',
  5362. ' i:=ord(red);',
  5363. ' i:=ord(green);',
  5364. ' i:=ord(e);',
  5365. ' i:=ord(b);',
  5366. ' e:=low(tmyenum);',
  5367. ' e:=low(e);',
  5368. ' b:=low(boolean);',
  5369. ' e:=high(tmyenum);',
  5370. ' e:=high(e);',
  5371. ' b:=high(boolean);',
  5372. ' e:=pred(green);',
  5373. ' e:=pred(e);',
  5374. ' b:=pred(b);',
  5375. ' e:=succ(red);',
  5376. ' e:=succ(e);',
  5377. ' b:=succ(b);',
  5378. ' e:=tmyenum(1);',
  5379. ' e:=tmyenum(i);',
  5380. ' s:=str(e);',
  5381. ' str(e,s);',
  5382. ' str(red,s);',
  5383. ' s:=str(e:3);',
  5384. ' writestr(s,e:3,red);',
  5385. ' val(s,e,i);',
  5386. ' i:=longint(e);']);
  5387. ConvertProgram;
  5388. CheckSource('TestEnum_Functions',
  5389. LinesToStr([ // statements
  5390. 'this.TMyEnum = {',
  5391. ' "0":"Red",',
  5392. ' Red:0,',
  5393. ' "1":"Green",',
  5394. ' Green:1',
  5395. ' };',
  5396. 'this.DoIt = function (e, i) {',
  5397. ' var v = 0;',
  5398. ' var s = "";',
  5399. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5400. ' v = w;',
  5401. ' }));',
  5402. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5403. '};',
  5404. 'this.e = 0;',
  5405. 'this.i = 0;',
  5406. 'this.s = "";',
  5407. 'this.b = false;',
  5408. '']),
  5409. LinesToStr([
  5410. '$mod.i=$mod.TMyEnum.Red;',
  5411. '$mod.i=$mod.TMyEnum.Green;',
  5412. '$mod.i=$mod.e;',
  5413. '$mod.i=$mod.b+0;',
  5414. '$mod.e=$mod.TMyEnum.Red;',
  5415. '$mod.e=$mod.TMyEnum.Red;',
  5416. '$mod.b=false;',
  5417. '$mod.e=$mod.TMyEnum.Green;',
  5418. '$mod.e=$mod.TMyEnum.Green;',
  5419. '$mod.b=true;',
  5420. '$mod.e=$mod.TMyEnum.Green-1;',
  5421. '$mod.e=$mod.e-1;',
  5422. '$mod.b=false;',
  5423. '$mod.e=$mod.TMyEnum.Red+1;',
  5424. '$mod.e=$mod.e+1;',
  5425. '$mod.b=true;',
  5426. '$mod.e=1;',
  5427. '$mod.e=$mod.i;',
  5428. '$mod.s = $mod.TMyEnum[$mod.e];',
  5429. '$mod.s = $mod.TMyEnum[$mod.e];',
  5430. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5431. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5432. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5433. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5434. ' $mod.i = v;',
  5435. '});',
  5436. '$mod.i=$mod.e;',
  5437. '']));
  5438. end;
  5439. procedure TTestModule.TestEnumRg_Functions;
  5440. begin
  5441. StartProgram(false);
  5442. Add([
  5443. 'type',
  5444. ' TEnum = (Red, Green, Blue);',
  5445. ' TEnumRg = Green..Blue;',
  5446. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5447. 'var',
  5448. ' v: longint;',
  5449. ' s: string;',
  5450. 'begin',
  5451. ' val(s,e,v);',
  5452. ' val(s,e,i);',
  5453. 'end;',
  5454. 'var',
  5455. ' e: TEnumRg;',
  5456. ' i: longint;',
  5457. ' s: string;',
  5458. 'begin',
  5459. ' i:=ord(green);',
  5460. ' i:=ord(e);',
  5461. ' e:=low(tenumrg);',
  5462. ' e:=low(e);',
  5463. ' e:=high(tenumrg);',
  5464. ' e:=high(e);',
  5465. ' e:=pred(blue);',
  5466. ' e:=pred(e);',
  5467. ' e:=succ(green);',
  5468. ' e:=succ(e);',
  5469. ' e:=tenumrg(1);',
  5470. ' e:=tenumrg(i);',
  5471. ' s:=str(e);',
  5472. ' str(e,s);',
  5473. ' str(red,s);',
  5474. ' s:=str(e:3);',
  5475. ' writestr(s,e:3,blue);',
  5476. ' val(s,e,i);',
  5477. ' i:=longint(e);']);
  5478. ConvertProgram;
  5479. CheckSource('TestEnumRg_Functions',
  5480. LinesToStr([ // statements
  5481. 'this.TEnum = {',
  5482. ' "0":"Red",',
  5483. ' Red:0,',
  5484. ' "1":"Green",',
  5485. ' Green:1,',
  5486. ' "2":"Blue",',
  5487. ' Blue:2',
  5488. ' };',
  5489. 'this.DoIt = function (e, i) {',
  5490. ' var v = 0;',
  5491. ' var s = "";',
  5492. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5493. ' v = w;',
  5494. ' }));',
  5495. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5496. '};',
  5497. 'this.e = this.TEnum.Green;',
  5498. 'this.i = 0;',
  5499. 'this.s = "";',
  5500. '']),
  5501. LinesToStr([
  5502. '$mod.i=$mod.TEnum.Green;',
  5503. '$mod.i=$mod.e;',
  5504. '$mod.e=$mod.TEnum.Green;',
  5505. '$mod.e=$mod.TEnum.Green;',
  5506. '$mod.e=$mod.TEnum.Blue;',
  5507. '$mod.e=$mod.TEnum.Blue;',
  5508. '$mod.e=$mod.TEnum.Blue-1;',
  5509. '$mod.e=$mod.e-1;',
  5510. '$mod.e=$mod.TEnum.Green+1;',
  5511. '$mod.e=$mod.e+1;',
  5512. '$mod.e=1;',
  5513. '$mod.e=$mod.i;',
  5514. '$mod.s = $mod.TEnum[$mod.e];',
  5515. '$mod.s = $mod.TEnum[$mod.e];',
  5516. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5517. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5518. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5519. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5520. ' $mod.i = v;',
  5521. '});',
  5522. '$mod.i=$mod.e;',
  5523. '']));
  5524. end;
  5525. procedure TTestModule.TestEnum_AsParams;
  5526. begin
  5527. StartProgram(false);
  5528. Add('type TEnum = (Red,Blue);');
  5529. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5530. Add('var vJ: TEnum;');
  5531. Add('begin');
  5532. Add(' vg:=vg;');
  5533. Add(' vj:=vh;');
  5534. Add(' vi:=vi;');
  5535. Add(' doit(vg,vg,vg);');
  5536. Add(' doit(vh,vh,vj);');
  5537. Add(' doit(vi,vi,vi);');
  5538. Add(' doit(vj,vj,vj);');
  5539. Add('end;');
  5540. Add('var i: TEnum;');
  5541. Add('begin');
  5542. Add(' doit(i,i,i);');
  5543. ConvertProgram;
  5544. CheckSource('TestEnum_AsParams',
  5545. LinesToStr([ // statements
  5546. 'this.TEnum = {',
  5547. ' "0": "Red",',
  5548. ' Red: 0,',
  5549. ' "1": "Blue",',
  5550. ' Blue: 1',
  5551. '};',
  5552. 'this.DoIt = function (vG,vH,vI) {',
  5553. ' var vJ = 0;',
  5554. ' vG = vG;',
  5555. ' vJ = vH;',
  5556. ' vI.set(vI.get());',
  5557. ' $mod.DoIt(vG, vG, {',
  5558. ' get: function () {',
  5559. ' return vG;',
  5560. ' },',
  5561. ' set: function (v) {',
  5562. ' vG = v;',
  5563. ' }',
  5564. ' });',
  5565. ' $mod.DoIt(vH, vH, {',
  5566. ' get: function () {',
  5567. ' return vJ;',
  5568. ' },',
  5569. ' set: function (v) {',
  5570. ' vJ = v;',
  5571. ' }',
  5572. ' });',
  5573. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5574. ' $mod.DoIt(vJ, vJ, {',
  5575. ' get: function () {',
  5576. ' return vJ;',
  5577. ' },',
  5578. ' set: function (v) {',
  5579. ' vJ = v;',
  5580. ' }',
  5581. ' });',
  5582. '};',
  5583. 'this.i = 0;'
  5584. ]),
  5585. LinesToStr([
  5586. '$mod.DoIt($mod.i,$mod.i,{',
  5587. ' p: $mod,',
  5588. ' get: function () {',
  5589. ' return this.p.i;',
  5590. ' },',
  5591. ' set: function (v) {',
  5592. ' this.p.i = v;',
  5593. ' }',
  5594. '});'
  5595. ]));
  5596. end;
  5597. procedure TTestModule.TestEnumRange_Array;
  5598. begin
  5599. StartProgram(false);
  5600. Add([
  5601. 'type',
  5602. ' TEnum = (Red, Green, Blue);',
  5603. ' TEnumRg = green..blue;',
  5604. ' TArr = array[TEnumRg] of byte;',
  5605. ' TArr2 = array[green..blue] of byte;',
  5606. 'var',
  5607. ' a: TArr;',
  5608. ' b: TArr = (3,4);',
  5609. ' c: TArr2 = (5,6);',
  5610. 'begin',
  5611. ' a[green] := b[blue];',
  5612. ' c[green] := c[blue];',
  5613. '']);
  5614. ConvertProgram;
  5615. CheckSource('TestEnumRange_Array',
  5616. LinesToStr([ // statements
  5617. 'this.TEnum = {',
  5618. ' "0": "Red",',
  5619. ' Red: 0,',
  5620. ' "1": "Green",',
  5621. ' Green: 1,',
  5622. ' "2": "Blue",',
  5623. ' Blue: 2',
  5624. '};',
  5625. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5626. 'this.b = [3, 4];',
  5627. 'this.c = [5, 6];',
  5628. '']),
  5629. LinesToStr([
  5630. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5631. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5632. '']));
  5633. end;
  5634. procedure TTestModule.TestEnum_ForIn;
  5635. begin
  5636. StartProgram(false);
  5637. Add([
  5638. 'type',
  5639. ' TEnum = (Red, Green, Blue);',
  5640. ' TEnumRg = green..blue;',
  5641. ' TArr = array[TEnum] of byte;',
  5642. ' TArrRg = array[TEnumRg] of byte;',
  5643. 'var',
  5644. ' e: TEnum;',
  5645. ' a1: TArr = (3,4,5);',
  5646. ' a2: TArrRg = (11,12);',
  5647. ' b: byte;',
  5648. 'begin',
  5649. ' for e in TEnum do ;',
  5650. ' for e in TEnumRg do ;',
  5651. ' for e in TArr do ;',
  5652. ' for e in TArrRg do ;',
  5653. ' for b in a1 do ;',
  5654. ' for b in a2 do ;',
  5655. '']);
  5656. ConvertProgram;
  5657. CheckSource('TestEnum_ForIn',
  5658. LinesToStr([ // statements
  5659. 'this.TEnum = {',
  5660. ' "0": "Red",',
  5661. ' Red: 0,',
  5662. ' "1": "Green",',
  5663. ' Green: 1,',
  5664. ' "2": "Blue",',
  5665. ' Blue: 2',
  5666. '};',
  5667. 'this.e = 0;',
  5668. 'this.a1 = [3, 4, 5];',
  5669. 'this.a2 = [11, 12];',
  5670. 'this.b = 0;',
  5671. '']),
  5672. LinesToStr([
  5673. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5674. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5675. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5676. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5677. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5678. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5679. '']));
  5680. end;
  5681. procedure TTestModule.TestEnum_ScopedNumber;
  5682. begin
  5683. Converter.Options:=Converter.Options+[coEnumNumbers];
  5684. StartProgram(false);
  5685. Add([
  5686. 'type',
  5687. ' TEnum = (Red, Green);',
  5688. 'var',
  5689. ' e: TEnum;',
  5690. 'begin',
  5691. ' e:=TEnum.Green;',
  5692. '']);
  5693. ConvertProgram;
  5694. CheckSource('TestEnum_ScopedNumber',
  5695. LinesToStr([ // statements
  5696. 'this.TEnum = {',
  5697. ' "0": "Red",',
  5698. ' Red: 0,',
  5699. ' "1": "Green",',
  5700. ' Green: 1',
  5701. '};',
  5702. 'this.e = 0;',
  5703. '']),
  5704. LinesToStr([
  5705. '$mod.e = 1;']));
  5706. end;
  5707. procedure TTestModule.TestEnum_InFunction;
  5708. begin
  5709. StartProgram(false);
  5710. Add([
  5711. 'const TEnum = 3;',
  5712. 'procedure DoIt;',
  5713. 'type',
  5714. ' TEnum = (Red, Green, Blue);',
  5715. ' procedure Sub;',
  5716. ' type',
  5717. ' TEnumSub = (Left, Right);',
  5718. ' var',
  5719. ' es: TEnumSub;',
  5720. ' begin',
  5721. ' es:=Left;',
  5722. ' end;',
  5723. 'var',
  5724. ' e, e2: TEnum;',
  5725. 'begin',
  5726. ' if e in [red,blue] then e2:=e;',
  5727. 'end;',
  5728. 'begin']);
  5729. ConvertProgram;
  5730. CheckSource('TestEnum_InFunction',
  5731. LinesToStr([ // statements
  5732. 'this.TEnum = 3;',
  5733. 'var TEnum$1 = {',
  5734. ' "0":"Red",',
  5735. ' Red:0,',
  5736. ' "1":"Green",',
  5737. ' Green:1,',
  5738. ' "2":"Blue",',
  5739. ' Blue:2',
  5740. ' };',
  5741. 'var TEnumSub = {',
  5742. ' "0": "Left",',
  5743. ' Left: 0,',
  5744. ' "1": "Right",',
  5745. ' Right: 1',
  5746. '};',
  5747. 'this.DoIt = function () {',
  5748. ' function Sub() {',
  5749. ' var es = 0;',
  5750. ' es = TEnumSub.Left;',
  5751. ' };',
  5752. ' var e = 0;',
  5753. ' var e2 = 0;',
  5754. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5755. '};',
  5756. '']),
  5757. LinesToStr([
  5758. '']));
  5759. end;
  5760. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5761. begin
  5762. StartUnit(true);
  5763. Add([
  5764. 'interface',
  5765. 'var color: (red, green);',
  5766. 'implementation',
  5767. 'initialization',
  5768. ' color:=green;',
  5769. '']);
  5770. ConvertUnit;
  5771. CheckSource('TestEnum_Name_Anonymous_Unit',
  5772. LinesToStr([
  5773. 'this.color$a = {',
  5774. ' "0": "red",',
  5775. ' red: 0,',
  5776. ' "1": "green",',
  5777. ' green: 1',
  5778. '};',
  5779. 'this.color = 0;',
  5780. '']),
  5781. LinesToStr([ // this.$init
  5782. '$mod.color = $mod.color$a.green;',
  5783. '']),
  5784. LinesToStr([ // implementation
  5785. '']) );
  5786. end;
  5787. procedure TTestModule.TestSet_Enum;
  5788. begin
  5789. StartProgram(false);
  5790. Add([
  5791. 'type',
  5792. ' TColor = (Red, Green, Blue);',
  5793. ' TColors = set of TColor;',
  5794. 'var',
  5795. ' c: TColor;',
  5796. ' s: TColors;',
  5797. ' t: TColors = [];',
  5798. ' u: TColors = [Red];',
  5799. 'begin',
  5800. ' s:=[];',
  5801. ' s:=[Green];',
  5802. ' s:=[Green,Blue];',
  5803. ' s:=[Red..Blue];',
  5804. ' s:=[Red,Green..Blue];',
  5805. ' s:=[Red,c];',
  5806. ' s:=t;',
  5807. ' s:=default(TColors);',
  5808. '']);
  5809. ConvertProgram;
  5810. CheckSource('TestSet',
  5811. LinesToStr([ // statements
  5812. 'this.TColor = {',
  5813. ' "0":"Red",',
  5814. ' Red:0,',
  5815. ' "1":"Green",',
  5816. ' Green:1,',
  5817. ' "2":"Blue",',
  5818. ' Blue:2',
  5819. ' };',
  5820. 'this.c = 0;',
  5821. 'this.s = {};',
  5822. 'this.t = {};',
  5823. 'this.u = rtl.createSet(this.TColor.Red);'
  5824. ]),
  5825. LinesToStr([
  5826. '$mod.s={};',
  5827. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5828. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5829. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5830. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5831. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5832. '$mod.s=rtl.refSet($mod.t);',
  5833. '$mod.s={};',
  5834. '']));
  5835. end;
  5836. procedure TTestModule.TestSet_Operators;
  5837. begin
  5838. StartProgram(false);
  5839. Add('type');
  5840. Add(' TColor = (Red, Green, Blue);');
  5841. Add(' TColors = set of tcolor;');
  5842. Add('var');
  5843. Add(' vC: TColor;');
  5844. Add(' vS: TColors;');
  5845. Add(' vT: TColors;');
  5846. Add(' vU: TColors;');
  5847. Add(' B: boolean;');
  5848. Add('begin');
  5849. Add(' include(vs,green);');
  5850. Add(' exclude(vs,vc);');
  5851. Add(' vs:=vt+vu;');
  5852. Add(' vs:=vt+[red];');
  5853. Add(' vs:=[red]+vt;');
  5854. Add(' vs:=[red]+[green];');
  5855. Add(' vs:=vt-vu;');
  5856. Add(' vs:=vt-[red];');
  5857. Add(' vs:=[red]-vt;');
  5858. Add(' vs:=[red]-[green];');
  5859. Add(' vs:=vt*vu;');
  5860. Add(' vs:=vt*[red];');
  5861. Add(' vs:=[red]*vt;');
  5862. Add(' vs:=[red]*[green];');
  5863. Add(' vs:=vt><vu;');
  5864. Add(' vs:=vt><[red];');
  5865. Add(' vs:=[red]><vt;');
  5866. Add(' vs:=[red]><[green];');
  5867. Add(' b:=vt=vu;');
  5868. Add(' b:=vt=[red];');
  5869. Add(' b:=[red]=vt;');
  5870. Add(' b:=[red]=[green];');
  5871. Add(' b:=vt<>vu;');
  5872. Add(' b:=vt<>[red];');
  5873. Add(' b:=[red]<>vt;');
  5874. Add(' b:=[red]<>[green];');
  5875. Add(' b:=vt<=vu;');
  5876. Add(' b:=vt<=[red];');
  5877. Add(' b:=[red]<=vt;');
  5878. Add(' b:=[red]<=[green];');
  5879. Add(' b:=vt>=vu;');
  5880. Add(' b:=vt>=[red];');
  5881. Add(' b:=[red]>=vt;');
  5882. Add(' b:=[red]>=[green];');
  5883. ConvertProgram;
  5884. CheckSource('TestSet_Operators',
  5885. LinesToStr([ // statements
  5886. 'this.TColor = {',
  5887. ' "0":"Red",',
  5888. ' Red:0,',
  5889. ' "1":"Green",',
  5890. ' Green:1,',
  5891. ' "2":"Blue",',
  5892. ' Blue:2',
  5893. ' };',
  5894. 'this.vC = 0;',
  5895. 'this.vS = {};',
  5896. 'this.vT = {};',
  5897. 'this.vU = {};',
  5898. 'this.B = false;'
  5899. ]),
  5900. LinesToStr([
  5901. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5902. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5903. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5904. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5905. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5906. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5907. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5908. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5909. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5910. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5911. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5912. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5913. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5914. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5915. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5916. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5917. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5918. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5919. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5920. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5921. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5922. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5923. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5924. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5925. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5926. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5927. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5928. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5929. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5930. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5931. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5932. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5933. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5934. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5935. '']));
  5936. end;
  5937. procedure TTestModule.TestSet_Operator_In;
  5938. begin
  5939. StartProgram(false);
  5940. Add([
  5941. 'type',
  5942. ' TColor = (Red, Green, Blue);',
  5943. ' TColors = set of tcolor;',
  5944. ' TColorRg = green..blue;',
  5945. 'var',
  5946. ' vC: tcolor;',
  5947. ' vT: tcolors;',
  5948. ' B: boolean;',
  5949. ' rg: TColorRg;',
  5950. 'begin',
  5951. ' b:=red in vt;',
  5952. ' b:=vc in vt;',
  5953. ' b:=green in [red..blue];',
  5954. ' b:=vc in [red..blue];',
  5955. ' ',
  5956. ' if red in vt then ;',
  5957. ' while vC in vt do ;',
  5958. ' repeat',
  5959. ' until vC in vt;',
  5960. ' if rg in [green..blue] then ;',
  5961. '']);
  5962. ConvertProgram;
  5963. CheckSource('TestSet_Operator_In',
  5964. LinesToStr([ // statements
  5965. 'this.TColor = {',
  5966. ' "0":"Red",',
  5967. ' Red:0,',
  5968. ' "1":"Green",',
  5969. ' Green:1,',
  5970. ' "2":"Blue",',
  5971. ' Blue:2',
  5972. ' };',
  5973. 'this.vC = 0;',
  5974. 'this.vT = {};',
  5975. 'this.B = false;',
  5976. 'this.rg = this.TColor.Green;',
  5977. '']),
  5978. LinesToStr([
  5979. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5980. '$mod.B = $mod.vC in $mod.vT;',
  5981. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5982. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5983. 'if ($mod.TColor.Red in $mod.vT) ;',
  5984. 'while ($mod.vC in $mod.vT) {',
  5985. '};',
  5986. 'do {',
  5987. '} while (!($mod.vC in $mod.vT));',
  5988. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5989. '']));
  5990. end;
  5991. procedure TTestModule.TestSet_Functions;
  5992. begin
  5993. StartProgram(false);
  5994. Add('type');
  5995. Add(' TMyEnum = (Red, Green);');
  5996. Add(' TMyEnums = set of TMyEnum;');
  5997. Add('var');
  5998. Add(' e: TMyEnum;');
  5999. Add(' s: TMyEnums;');
  6000. Add('begin');
  6001. Add(' e:=Low(TMyEnums);');
  6002. Add(' e:=Low(s);');
  6003. Add(' e:=High(TMyEnums);');
  6004. Add(' e:=High(s);');
  6005. ConvertProgram;
  6006. CheckSource('TestSetFunctions',
  6007. LinesToStr([ // statements
  6008. 'this.TMyEnum = {',
  6009. ' "0":"Red",',
  6010. ' Red:0,',
  6011. ' "1":"Green",',
  6012. ' Green:1',
  6013. ' };',
  6014. 'this.e = 0;',
  6015. 'this.s = {};'
  6016. ]),
  6017. LinesToStr([
  6018. '$mod.e=$mod.TMyEnum.Red;',
  6019. '$mod.e=$mod.TMyEnum.Red;',
  6020. '$mod.e=$mod.TMyEnum.Green;',
  6021. '$mod.e=$mod.TMyEnum.Green;',
  6022. '']));
  6023. end;
  6024. procedure TTestModule.TestSet_PassAsArgClone;
  6025. begin
  6026. StartProgram(false);
  6027. Add('type');
  6028. Add(' TMyEnum = (Red, Green);');
  6029. Add(' TMyEnums = set of TMyEnum;');
  6030. Add('procedure DoDefault(s: tmyenums); begin end;');
  6031. Add('procedure DoConst(const s: tmyenums); begin end;');
  6032. Add('var');
  6033. Add(' aSet: tmyenums;');
  6034. Add('begin');
  6035. Add(' dodefault(aset);');
  6036. Add(' doconst(aset);');
  6037. ConvertProgram;
  6038. CheckSource('TestSetFunctions',
  6039. LinesToStr([ // statements
  6040. 'this.TMyEnum = {',
  6041. ' "0":"Red",',
  6042. ' Red:0,',
  6043. ' "1":"Green",',
  6044. ' Green:1',
  6045. ' };',
  6046. 'this.DoDefault = function (s) {',
  6047. '};',
  6048. 'this.DoConst = function (s) {',
  6049. '};',
  6050. 'this.aSet = {};'
  6051. ]),
  6052. LinesToStr([
  6053. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6054. '$mod.DoConst($mod.aSet);',
  6055. '']));
  6056. end;
  6057. procedure TTestModule.TestSet_AsParams;
  6058. begin
  6059. StartProgram(false);
  6060. Add([
  6061. 'type TEnum = (Red,Blue);',
  6062. 'type TEnums = set of TEnum;',
  6063. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6064. 'var vJ: TEnums;',
  6065. 'begin',
  6066. ' Include(vg,red);',
  6067. ' Include(result,blue);',
  6068. ' vg:=vg;',
  6069. ' vj:=vh;',
  6070. ' vi:=vi;',
  6071. ' doit(vg,vg,vg);',
  6072. ' doit(vh,vh,vj);',
  6073. ' doit(vi,vi,vi);',
  6074. ' doit(vj,vj,vj);',
  6075. 'end;',
  6076. 'var i: TEnums;',
  6077. 'begin',
  6078. ' doit(i,i,i);']);
  6079. ConvertProgram;
  6080. CheckSource('TestSet_AsParams',
  6081. LinesToStr([ // statements
  6082. 'this.TEnum = {',
  6083. ' "0": "Red",',
  6084. ' Red: 0,',
  6085. ' "1": "Blue",',
  6086. ' Blue: 1',
  6087. '};',
  6088. 'this.DoIt = function (vG,vH,vI) {',
  6089. ' var Result = {};',
  6090. ' var vJ = {};',
  6091. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6092. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6093. ' vG = rtl.refSet(vG);',
  6094. ' vJ = rtl.refSet(vH);',
  6095. ' vI.set(rtl.refSet(vI.get()));',
  6096. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6097. ' get: function () {',
  6098. ' return vG;',
  6099. ' },',
  6100. ' set: function (v) {',
  6101. ' vG = v;',
  6102. ' }',
  6103. ' });',
  6104. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6105. ' get: function () {',
  6106. ' return vJ;',
  6107. ' },',
  6108. ' set: function (v) {',
  6109. ' vJ = v;',
  6110. ' }',
  6111. ' });',
  6112. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6113. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6114. ' get: function () {',
  6115. ' return vJ;',
  6116. ' },',
  6117. ' set: function (v) {',
  6118. ' vJ = v;',
  6119. ' }',
  6120. ' });',
  6121. ' return Result;',
  6122. '};',
  6123. 'this.i = {};'
  6124. ]),
  6125. LinesToStr([
  6126. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6127. ' p: $mod,',
  6128. ' get: function () {',
  6129. ' return this.p.i;',
  6130. ' },',
  6131. ' set: function (v) {',
  6132. ' this.p.i = v;',
  6133. ' }',
  6134. '});'
  6135. ]));
  6136. end;
  6137. procedure TTestModule.TestSet_Property;
  6138. begin
  6139. StartProgram(false);
  6140. Add('type');
  6141. Add(' TEnum = (Red,Blue);');
  6142. Add(' TEnums = set of TEnum;');
  6143. Add(' TObject = class');
  6144. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6145. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6146. Add(' property Colors: TEnums read GetColors write SetColors;');
  6147. Add(' end;');
  6148. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6149. Add('begin end;');
  6150. Add('var Obj: TObject;');
  6151. Add('begin');
  6152. Add(' Include(Obj.Colors,Red);');
  6153. Add(' Exclude(Obj.Colors,Red);');
  6154. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6155. ConvertProgram;
  6156. CheckSource('TestSet_Property',
  6157. LinesToStr([ // statements
  6158. 'this.TEnum = {',
  6159. ' "0": "Red",',
  6160. ' Red: 0,',
  6161. ' "1": "Blue",',
  6162. ' Blue: 1',
  6163. '};',
  6164. 'rtl.createClass(this, "TObject", null, function () {',
  6165. ' this.$init = function () {',
  6166. ' };',
  6167. ' this.$final = function () {',
  6168. ' };',
  6169. '});',
  6170. 'this.DoIt = function (i, j, k, l) {',
  6171. '};',
  6172. 'this.Obj = null;',
  6173. '']),
  6174. LinesToStr([
  6175. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6176. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6177. '']));
  6178. end;
  6179. procedure TTestModule.TestSet_EnumConst;
  6180. begin
  6181. StartProgram(false);
  6182. Add([
  6183. 'type',
  6184. ' TEnum = (Red,Blue);',
  6185. ' TEnums = set of TEnum;',
  6186. 'const',
  6187. ' Orange = red;',
  6188. 'var',
  6189. ' Enum: tenum;',
  6190. ' Enums: tenums;',
  6191. 'begin',
  6192. ' Include(enums,orange);',
  6193. ' Exclude(enums,orange);',
  6194. ' if orange in enums then;',
  6195. ' if orange in [orange,red] then;']);
  6196. ConvertProgram;
  6197. CheckSource('TestSet_EnumConst',
  6198. LinesToStr([ // statements
  6199. 'this.TEnum = {',
  6200. ' "0": "Red",',
  6201. ' Red: 0,',
  6202. ' "1": "Blue",',
  6203. ' Blue: 1',
  6204. '};',
  6205. 'this.Orange = this.TEnum.Red;',
  6206. 'this.Enum = 0;',
  6207. 'this.Enums = {};',
  6208. '']),
  6209. LinesToStr([
  6210. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6211. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6212. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6213. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6214. '']));
  6215. end;
  6216. procedure TTestModule.TestSet_IntConst;
  6217. begin
  6218. StartProgram(false);
  6219. Add([
  6220. 'type',
  6221. ' TEnums = set of Byte;',
  6222. 'const',
  6223. ' Orange = 0;',
  6224. 'var',
  6225. ' Enum: byte;',
  6226. ' Enums: tenums;',
  6227. 'begin',
  6228. ' Enums:=[];',
  6229. ' Enums:=[0];',
  6230. ' Enums:=[1..2];',
  6231. //' Include(enums,orange);',
  6232. //' Exclude(enums,orange);',
  6233. ' if orange in enums then;',
  6234. ' if orange in [orange,1] then;']);
  6235. ConvertProgram;
  6236. CheckSource('TestSet_IntConst',
  6237. LinesToStr([ // statements
  6238. 'this.Orange = 0;',
  6239. 'this.Enum = 0;',
  6240. 'this.Enums = {};',
  6241. '']),
  6242. LinesToStr([
  6243. '$mod.Enums = {};',
  6244. '$mod.Enums = rtl.createSet(0);',
  6245. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6246. 'if (0 in $mod.Enums) ;',
  6247. 'if (0 in rtl.createSet(0, 1)) ;',
  6248. '']));
  6249. end;
  6250. procedure TTestModule.TestSet_IntRange;
  6251. begin
  6252. StartProgram(false);
  6253. Add([
  6254. 'type',
  6255. ' TRange = 1..3;',
  6256. ' TEnums = set of TRange;',
  6257. 'const',
  6258. ' Orange = 2;',
  6259. 'var',
  6260. ' Enum: byte;',
  6261. ' Enums: TEnums;',
  6262. 'begin',
  6263. ' Enums:=[];',
  6264. ' Enums:=[1];',
  6265. ' Enums:=[2..3];',
  6266. ' Include(enums,orange);',
  6267. ' Exclude(enums,orange);',
  6268. ' if orange in enums then;',
  6269. ' if orange in [orange,1] then;']);
  6270. ConvertProgram;
  6271. CheckSource('TestSet_IntRange',
  6272. LinesToStr([ // statements
  6273. 'this.Orange = 2;',
  6274. 'this.Enum = 0;',
  6275. 'this.Enums = {};',
  6276. '']),
  6277. LinesToStr([
  6278. '$mod.Enums = {};',
  6279. '$mod.Enums = rtl.createSet(1);',
  6280. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6281. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6282. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6283. 'if (2 in $mod.Enums) ;',
  6284. 'if (2 in rtl.createSet(2, 1)) ;',
  6285. '']));
  6286. end;
  6287. procedure TTestModule.TestSet_AnonymousEnumType;
  6288. begin
  6289. StartProgram(false);
  6290. Add('type');
  6291. Add(' TFlags = set of (red, green);');
  6292. Add('const');
  6293. Add(' favorite = red;');
  6294. Add('var');
  6295. Add(' f: TFlags;');
  6296. Add(' i: longint;');
  6297. Add('begin');
  6298. Add(' Include(f,red);');
  6299. Add(' Include(f,favorite);');
  6300. Add(' i:=ord(red);');
  6301. Add(' i:=ord(favorite);');
  6302. Add(' i:=ord(low(TFlags));');
  6303. Add(' i:=ord(low(f));');
  6304. Add(' i:=ord(low(favorite));');
  6305. Add(' i:=ord(high(TFlags));');
  6306. Add(' i:=ord(high(f));');
  6307. Add(' i:=ord(high(favorite));');
  6308. Add(' f:=[green,favorite];');
  6309. ConvertProgram;
  6310. CheckSource('TestSet_AnonymousEnumType',
  6311. LinesToStr([ // statements
  6312. 'this.TFlags$a = {',
  6313. ' "0": "red",',
  6314. ' red: 0,',
  6315. ' "1": "green",',
  6316. ' green: 1',
  6317. '};',
  6318. 'this.favorite = this.TFlags$a.red;',
  6319. 'this.f = {};',
  6320. 'this.i = 0;',
  6321. '']),
  6322. LinesToStr([
  6323. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6324. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6325. '$mod.i = $mod.TFlags$a.red;',
  6326. '$mod.i = $mod.TFlags$a.red;',
  6327. '$mod.i = $mod.TFlags$a.red;',
  6328. '$mod.i = $mod.TFlags$a.red;',
  6329. '$mod.i = $mod.TFlags$a.red;',
  6330. '$mod.i = $mod.TFlags$a.green;',
  6331. '$mod.i = $mod.TFlags$a.green;',
  6332. '$mod.i = $mod.TFlags$a.green;',
  6333. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6334. '']));
  6335. end;
  6336. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6337. begin
  6338. exit;
  6339. StartProgram(false);
  6340. Add([
  6341. 'type',
  6342. ' TAtoZ = ''A''..''Z'';',
  6343. ' TSetOfAZ = set of TAtoZ;',
  6344. 'var',
  6345. ' c: char;',
  6346. ' a: TAtoZ;',
  6347. ' s: TSetOfAZ = [''P'',''A''];',
  6348. ' i: longint;',
  6349. 'begin',
  6350. ' Include(s,''S'');',
  6351. ' Include(s,c);',
  6352. ' Include(s,a);',
  6353. ' c:=low(TAtoZ);',
  6354. ' i:=ord(low(TAtoZ));',
  6355. ' a:=high(TAtoZ);',
  6356. ' a:=high(TSetOfAtoZ);',
  6357. ' s:=[a,c,''M''];',
  6358. '']);
  6359. ConvertProgram;
  6360. CheckSource('TestSet_AnonymousEnumTypeChar',
  6361. LinesToStr([ // statements
  6362. '']),
  6363. LinesToStr([
  6364. '']));
  6365. end;
  6366. procedure TTestModule.TestSet_ConstEnum;
  6367. begin
  6368. StartProgram(false);
  6369. Add([
  6370. 'type',
  6371. ' TEnum = (red,blue,green);',
  6372. ' TEnums = set of TEnum;',
  6373. 'const',
  6374. ' teAny = [low(TEnum)..high(TEnum)];',
  6375. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6376. 'var',
  6377. ' e: TEnum;',
  6378. ' s: TEnums;',
  6379. 'begin',
  6380. ' if blue in teAny then;',
  6381. ' if blue in teAny+[e] then;',
  6382. ' if blue in teAny+teRedBlue then;',
  6383. ' if e in [red,blue] then;',
  6384. ' s:=teAny;',
  6385. ' s:=teAny+[e];',
  6386. ' s:=[e]+teAny;',
  6387. ' s:=teAny+teRedBlue;',
  6388. ' s:=teAny+teRedBlue+[e];',
  6389. '']);
  6390. ConvertProgram;
  6391. CheckSource('TestSet_ConstEnum',
  6392. LinesToStr([ // statements
  6393. 'this.TEnum = {',
  6394. ' "0": "red",',
  6395. ' red: 0,',
  6396. ' "1": "blue",',
  6397. ' blue: 1,',
  6398. ' "2": "green",',
  6399. ' green: 2',
  6400. '};',
  6401. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6402. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6403. 'this.e = 0;',
  6404. 'this.s = {};',
  6405. '']),
  6406. LinesToStr([
  6407. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6408. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6409. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6410. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6411. '$mod.s = rtl.refSet($mod.teAny);',
  6412. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6413. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6414. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6415. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6416. '']));
  6417. end;
  6418. procedure TTestModule.TestSet_ConstChar;
  6419. begin
  6420. StartProgram(false);
  6421. Add([
  6422. 'const',
  6423. ' LowChars = [''a''..''z''];',
  6424. ' Chars = LowChars+[''A''..''Z''];',
  6425. ' sc = [''А'', ''Я''];',
  6426. 'var',
  6427. ' c: char;',
  6428. ' s: string;',
  6429. 'begin',
  6430. ' if c in lowchars then ;',
  6431. ' if ''a'' in lowchars then ;',
  6432. ' if s[1] in lowchars then ;',
  6433. ' if c in chars then ;',
  6434. ' if c in [''a''..''z'',''_''] then ;',
  6435. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6436. ' if ''Я'' in sc then ;',
  6437. ' if 3=ord('' '') then ;',
  6438. '']);
  6439. ConvertProgram;
  6440. CheckSource('TestSet_ConstChar',
  6441. LinesToStr([ // statements
  6442. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6443. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6444. 'this.sc = rtl.createSet(1040, 1071);',
  6445. 'this.c = "";',
  6446. 'this.s = "";',
  6447. '']),
  6448. LinesToStr([
  6449. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6450. 'if (97 in $mod.LowChars) ;',
  6451. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6452. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6453. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6454. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6455. 'if (1071 in $mod.sc) ;',
  6456. 'if (3 === 32) ;',
  6457. '']));
  6458. end;
  6459. procedure TTestModule.TestSet_ConstInt;
  6460. begin
  6461. StartProgram(false);
  6462. Add([
  6463. 'const',
  6464. ' Months = [1..12];',
  6465. ' Mirror = [-12..-1]+Months;',
  6466. 'var',
  6467. ' i: smallint;',
  6468. 'begin',
  6469. ' if 3 in Months then;',
  6470. ' if i in Months+[i] then;',
  6471. ' if i in Months+Mirror then;',
  6472. ' if i in [4..6,8] then;',
  6473. '']);
  6474. ConvertProgram;
  6475. CheckSource('TestSet_ConstInt',
  6476. LinesToStr([ // statements
  6477. 'this.Months = rtl.createSet(null, 1, 12);',
  6478. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6479. 'this.i = 0;',
  6480. '']),
  6481. LinesToStr([
  6482. 'if (3 in $mod.Months) ;',
  6483. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6484. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6485. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6486. '']));
  6487. end;
  6488. procedure TTestModule.TestSet_InFunction;
  6489. begin
  6490. StartProgram(false);
  6491. Add([
  6492. 'const',
  6493. ' TEnum = 3;',
  6494. ' TSetOfEnum = 4;',
  6495. ' TSetOfAno = 5;',
  6496. 'procedure DoIt;',
  6497. 'type',
  6498. ' TEnum = (red, blue);',
  6499. ' TSetOfEnum = set of TEnum;',
  6500. ' TSetOfAno = set of (up,down);',
  6501. 'var',
  6502. ' e: TEnum;',
  6503. ' se: TSetOfEnum;',
  6504. ' sa: TSetOfAno;',
  6505. 'begin',
  6506. ' se:=[e];',
  6507. ' sa:=[up];',
  6508. 'end;',
  6509. 'begin',
  6510. '']);
  6511. ConvertProgram;
  6512. CheckSource('TestSet_InFunction',
  6513. LinesToStr([ // statements
  6514. 'this.TEnum = 3;',
  6515. 'this.TSetOfEnum = 4;',
  6516. 'this.TSetOfAno = 5;',
  6517. 'var TEnum$1 = {',
  6518. ' "0": "red",',
  6519. ' red: 0,',
  6520. ' "1": "blue",',
  6521. ' blue: 1',
  6522. '};',
  6523. 'var TSetOfAno$a = {',
  6524. ' "0": "up",',
  6525. ' up: 0,',
  6526. ' "1": "down",',
  6527. ' down: 1',
  6528. '};',
  6529. 'this.DoIt = function () {',
  6530. ' var e = 0;',
  6531. ' var se = {};',
  6532. ' var sa = {};',
  6533. ' se = rtl.createSet(e);',
  6534. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6535. '};',
  6536. '']),
  6537. LinesToStr([
  6538. '']));
  6539. end;
  6540. procedure TTestModule.TestSet_ForIn;
  6541. begin
  6542. StartProgram(false);
  6543. Add([
  6544. 'type',
  6545. ' TEnum = (Red, Green, Blue);',
  6546. ' TEnumRg = green..blue;',
  6547. ' TSetOfEnum = set of TEnum;',
  6548. ' TSetOfEnumRg = set of TEnumRg;',
  6549. 'var',
  6550. ' e, e2: TEnum;',
  6551. ' er: TEnum;',
  6552. ' s: TSetOfEnum;',
  6553. 'begin',
  6554. ' for e in TSetOfEnum do ;',
  6555. ' for e in TSetOfEnumRg do ;',
  6556. ' for e in [] do e2:=e;',
  6557. ' for e in [red..green] do e2:=e;',
  6558. ' for e in [green,blue] do e2:=e;',
  6559. ' for e in [red,blue] do e2:=e;',
  6560. ' for e in s do e2:=e;',
  6561. ' for er in TSetOfEnumRg do ;',
  6562. '']);
  6563. ConvertProgram;
  6564. CheckSource('TestSet_ForIn',
  6565. LinesToStr([ // statements
  6566. 'this.TEnum = {',
  6567. ' "0":"Red",',
  6568. ' Red:0,',
  6569. ' "1":"Green",',
  6570. ' Green:1,',
  6571. ' "2":"Blue",',
  6572. ' Blue:2',
  6573. ' };',
  6574. 'this.e = 0;',
  6575. 'this.e2 = 0;',
  6576. 'this.er = 0;',
  6577. 'this.s = {};',
  6578. '']),
  6579. LinesToStr([
  6580. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6581. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6582. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6583. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6584. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6585. 'for (var $l in $mod.s){',
  6586. ' $mod.e = +$l;',
  6587. ' $mod.e2 = $mod.e;',
  6588. '};',
  6589. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6590. '']));
  6591. end;
  6592. procedure TTestModule.TestNestBegin;
  6593. begin
  6594. StartProgram(false);
  6595. Add('begin');
  6596. Add(' begin');
  6597. Add(' begin');
  6598. Add(' end;');
  6599. Add(' begin');
  6600. Add(' if true then ;');
  6601. Add(' end;');
  6602. Add(' end;');
  6603. ConvertProgram;
  6604. CheckSource('TestNestBegin',
  6605. '',
  6606. 'if (true) ;');
  6607. end;
  6608. procedure TTestModule.TestUnitImplVars;
  6609. begin
  6610. StartUnit(false);
  6611. Add('interface');
  6612. Add('implementation');
  6613. Add('var');
  6614. Add(' V1:longint;');
  6615. Add(' V2:longint = 3;');
  6616. Add(' V3:string = ''abc'';');
  6617. ConvertUnit;
  6618. CheckSource('TestUnitImplVars',
  6619. LinesToStr([ // statements
  6620. 'var $impl = $mod.$impl;',
  6621. '']),
  6622. '', // this.$init
  6623. LinesToStr([ // implementation
  6624. '$impl.V1 = 0;',
  6625. '$impl.V2 = 3;',
  6626. '$impl.V3 = "abc";',
  6627. '']) );
  6628. end;
  6629. procedure TTestModule.TestUnitImplConsts;
  6630. begin
  6631. StartUnit(false);
  6632. Add('interface');
  6633. Add('implementation');
  6634. Add('const');
  6635. Add(' v1 = 3;');
  6636. Add(' v2:longint = 4;');
  6637. Add(' v3:string = ''abc'';');
  6638. ConvertUnit;
  6639. CheckSource('TestUnitImplConsts',
  6640. LinesToStr([ // statements
  6641. 'var $impl = $mod.$impl;',
  6642. '']),
  6643. '', // this.$init
  6644. LinesToStr([ // implementation
  6645. '$impl.v1 = 3;',
  6646. '$impl.v2 = 4;',
  6647. '$impl.v3 = "abc";',
  6648. '']) );
  6649. end;
  6650. procedure TTestModule.TestUnitImplRecord;
  6651. begin
  6652. StartUnit(false);
  6653. Add('interface');
  6654. Add('implementation');
  6655. Add('type');
  6656. Add(' TMyRecord = record');
  6657. Add(' i: longint;');
  6658. Add(' end;');
  6659. Add('var aRec: TMyRecord;');
  6660. Add('initialization');
  6661. Add(' arec.i:=3;');
  6662. ConvertUnit;
  6663. CheckSource('TestUnitImplRecord',
  6664. LinesToStr([ // statements
  6665. 'var $impl = $mod.$impl;',
  6666. '']),
  6667. // this.$init
  6668. '$impl.aRec.i = 3;',
  6669. LinesToStr([ // implementation
  6670. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6671. ' this.i = 0;',
  6672. ' this.$eq = function (b) {',
  6673. ' return this.i === b.i;',
  6674. ' };',
  6675. ' this.$assign = function (s) {',
  6676. ' this.i = s.i;',
  6677. ' return this;',
  6678. ' };',
  6679. '});',
  6680. '$impl.aRec = $impl.TMyRecord.$new();',
  6681. '']) );
  6682. end;
  6683. procedure TTestModule.TestRenameJSNameConflict;
  6684. begin
  6685. StartProgram(false);
  6686. Add('var apply: longint;');
  6687. Add('var bind: longint;');
  6688. Add('var call: longint;');
  6689. Add('begin');
  6690. ConvertProgram;
  6691. CheckSource('TestRenameJSNameConflict',
  6692. LinesToStr([ // statements
  6693. 'this.Apply = 0;',
  6694. 'this.Bind = 0;',
  6695. 'this.Call = 0;'
  6696. ]),
  6697. LinesToStr([ // this.$main
  6698. ''
  6699. ]));
  6700. end;
  6701. procedure TTestModule.TestLocalConst;
  6702. begin
  6703. StartProgram(false);
  6704. Add('procedure DoIt;');
  6705. Add('const');
  6706. Add(' cA: longint = 1;');
  6707. Add(' cB = 2;');
  6708. Add(' procedure Sub;');
  6709. Add(' const');
  6710. Add(' csA = 3;');
  6711. Add(' cB: double = 4;');
  6712. Add(' begin');
  6713. Add(' cb:=cb+csa;');
  6714. Add(' ca:=ca+csa+5;');
  6715. Add(' end;');
  6716. Add('begin');
  6717. Add(' ca:=ca+cb+6;');
  6718. Add('end;');
  6719. Add('begin');
  6720. ConvertProgram;
  6721. CheckSource('TestLocalConst',
  6722. LinesToStr([
  6723. 'var cA = 1;',
  6724. 'var cB = 2;',
  6725. 'var csA = 3;',
  6726. 'var cB$1 = 4;',
  6727. 'this.DoIt = function () {',
  6728. ' function Sub() {',
  6729. ' cB$1 = cB$1 + 3;',
  6730. ' cA = cA + 3 + 5;',
  6731. ' };',
  6732. ' cA = cA + 2 + 6;',
  6733. '};'
  6734. ]),
  6735. LinesToStr([
  6736. ]));
  6737. end;
  6738. procedure TTestModule.TestVarExternal;
  6739. begin
  6740. StartProgram(false);
  6741. Add('var');
  6742. Add(' NaN: double; external name ''Global.NaN'';');
  6743. Add(' d: double;');
  6744. Add('begin');
  6745. Add(' d:=NaN;');
  6746. ConvertProgram;
  6747. CheckSource('TestVarExternal',
  6748. LinesToStr([
  6749. 'this.d = 0.0;'
  6750. ]),
  6751. LinesToStr([
  6752. '$mod.d = Global.NaN;'
  6753. ]));
  6754. end;
  6755. procedure TTestModule.TestVarExternalOtherUnit;
  6756. begin
  6757. AddModuleWithIntfImplSrc('unit2.pas',
  6758. LinesToStr([
  6759. 'var NaN: double; external name ''Global.NaN'';',
  6760. 'var iV: longint;'
  6761. ]),
  6762. '');
  6763. StartUnit(true);
  6764. Add('interface');
  6765. Add('uses unit2;');
  6766. Add('implementation');
  6767. Add('var');
  6768. Add(' d: double;');
  6769. Add(' i: longint; external name ''$i'';');
  6770. Add('begin');
  6771. Add(' d:=nan;');
  6772. Add(' d:=uNit2.nan;');
  6773. Add(' d:=test1.d;');
  6774. Add(' i:=iv;');
  6775. Add(' i:=uNit2.iv;');
  6776. Add(' i:=test1.i;');
  6777. ConvertUnit;
  6778. CheckSource('TestVarExternalOtherUnit',
  6779. LinesToStr([
  6780. 'var $impl = $mod.$impl;',
  6781. '']),
  6782. LinesToStr([ // this.$init
  6783. '$impl.d = Global.NaN;',
  6784. '$impl.d = Global.NaN;',
  6785. '$impl.d = $impl.d;',
  6786. '$i = pas.unit2.iV;',
  6787. '$i = pas.unit2.iV;',
  6788. '$i = $i;',
  6789. '']),
  6790. LinesToStr([ // implementation
  6791. '$impl.d = 0.0;',
  6792. '']) );
  6793. end;
  6794. procedure TTestModule.TestVarAbsoluteFail;
  6795. begin
  6796. StartProgram(false);
  6797. Add([
  6798. 'var',
  6799. ' a: longint;',
  6800. ' b: longword absolute a;',
  6801. 'begin']);
  6802. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6803. ConvertProgram;
  6804. end;
  6805. procedure TTestModule.TestConstExternal;
  6806. begin
  6807. StartProgram(false);
  6808. Add([
  6809. 'const',
  6810. ' PI: double; external name ''Global.PI'';',
  6811. ' Tau = 2*pi;',
  6812. 'var d: double;',
  6813. 'begin',
  6814. ' d:=pi;',
  6815. ' d:=tau+pi;']);
  6816. ConvertProgram;
  6817. CheckSource('TestConstExternal',
  6818. LinesToStr([
  6819. 'this.Tau = 2*Global.PI;',
  6820. 'this.d = 0.0;'
  6821. ]),
  6822. LinesToStr([
  6823. '$mod.d = Global.PI;',
  6824. '$mod.d = $mod.Tau + Global.PI;'
  6825. ]));
  6826. end;
  6827. procedure TTestModule.TestDouble;
  6828. begin
  6829. StartProgram(false);
  6830. Add([
  6831. 'type',
  6832. ' TDateTime = double;',
  6833. 'const',
  6834. ' a = TDateTime(2.7);',
  6835. ' b = a + TDateTime(1.7);',
  6836. ' c = 0.9 + 0.1;',
  6837. ' f0_1 = 0.1;',
  6838. ' f0_3 = 0.3;',
  6839. ' fn0_1 = -0.1;',
  6840. ' fn0_3 = -0.3;',
  6841. ' fn0_003 = -0.003;',
  6842. ' fn0_123456789 = -0.123456789;',
  6843. ' fn300_0 = -300.0;',
  6844. ' fn123456_0 = -123456.0;',
  6845. ' fn1234567_8 = -1234567.8;',
  6846. ' fn12345678_9 = -12345678.9;',
  6847. ' f1_0En12 = 1E-12;',
  6848. ' fn1_0En12 = -1E-12;',
  6849. ' maxdouble = 1.7e+308;',
  6850. ' mindouble = -1.7e+308;',
  6851. ' MinSafeIntDouble = -$1fffffffffffff;',
  6852. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6853. ' MaxSafeIntDouble = $1fffffffffffff;',
  6854. ' DZeroResolution = 1E-12;',
  6855. ' Minus1 = -1E-12;',
  6856. ' EPS = 1E-9;',
  6857. ' DELTA = 0.001;',
  6858. ' Big = 129.789E+100;',
  6859. ' Test0_15 = 0.15;',
  6860. ' Test999 = 2.9999999999999;',
  6861. ' Test111999 = 211199999999999000.0;',
  6862. ' TestMinus111999 = -211199999999999000.0;',
  6863. ' Inf = 1.0 / 0.0;',
  6864. ' NegInf = -1.0 / 0.0;',
  6865. 'procedure Run(d: double); external name ''Run'';',
  6866. 'var',
  6867. ' d: double = b;',
  6868. 'begin',
  6869. ' d:=1.0;',
  6870. ' d:=1.0/3.0;',
  6871. ' d:=1.0/(3-2-1);',
  6872. ' d:=1/3;',
  6873. ' d:=5.0E-324;',
  6874. ' d:=1.7E308;',
  6875. ' d:=001.00E00;',
  6876. ' d:=002.00E001;',
  6877. ' d:=003.000E000;',
  6878. ' d:=-004.00E-00;',
  6879. ' d:=-005.00E-001;',
  6880. ' d:=10**3;',
  6881. ' d:=10 mod 3;',
  6882. ' d:=10 div 3;',
  6883. ' d:=c;',
  6884. ' d:=f0_1;',
  6885. ' d:=f0_3;',
  6886. ' d:=fn0_1;',
  6887. ' d:=fn0_3;',
  6888. ' d:=fn0_003;',
  6889. ' d:=fn0_123456789;',
  6890. ' d:=fn300_0;',
  6891. ' d:=fn123456_0;',
  6892. ' d:=fn1234567_8;',
  6893. ' d:=fn12345678_9;',
  6894. ' d:=f1_0En12;',
  6895. ' d:=fn1_0En12;',
  6896. ' d:=maxdouble;',
  6897. ' d:=mindouble;',
  6898. ' d:=MinSafeIntDouble;',
  6899. ' d:=double(MinSafeIntDouble);',
  6900. ' d:=MinSafeIntDouble2;',
  6901. ' d:=double(MinSafeIntDouble2);',
  6902. ' d:=MaxSafeIntDouble;',
  6903. ' d:=default(double);',
  6904. ' Run(Inf);',
  6905. ' Run(NegInf);',
  6906. '']);
  6907. ConvertProgram;
  6908. CheckSource('TestDouble',
  6909. LinesToStr([
  6910. 'this.a = 2.7;',
  6911. 'this.b = 2.7 + 1.7;',
  6912. 'this.c = 0.9 + 0.1;',
  6913. 'this.f0_1 = 0.1;',
  6914. 'this.f0_3 = 0.3;',
  6915. 'this.fn0_1 = -0.1;',
  6916. 'this.fn0_3 = -0.3;',
  6917. 'this.fn0_003 = -0.003;',
  6918. 'this.fn0_123456789 = -0.123456789;',
  6919. 'this.fn300_0 = -300.0;',
  6920. 'this.fn123456_0 = -123456.0;',
  6921. 'this.fn1234567_8 = -1234567.8;',
  6922. 'this.fn12345678_9 = -12345678.9;',
  6923. 'this.f1_0En12 = 1E-12;',
  6924. 'this.fn1_0En12 = -1E-12;',
  6925. 'this.maxdouble = 1.7e+308;',
  6926. 'this.mindouble = -1.7e+308;',
  6927. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6928. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6929. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6930. 'this.DZeroResolution = 1E-12;',
  6931. 'this.Minus1 = -1E-12;',
  6932. 'this.EPS = 1E-9;',
  6933. 'this.DELTA = 0.001;',
  6934. 'this.Big = 129.789E+100;',
  6935. 'this.Test0_15 = 0.15;',
  6936. 'this.Test999 = 2.9999999999999;',
  6937. 'this.Test111999 = 211199999999999000.0;',
  6938. 'this.TestMinus111999 = -211199999999999000.0;',
  6939. 'this.Inf = 1.0 / 0.0;',
  6940. 'this.NegInf = -1.0 / 0.0;',
  6941. 'this.d = 4.4;',
  6942. '']),
  6943. LinesToStr([
  6944. '$mod.d = 1.0;',
  6945. '$mod.d = 1.0 / 3.0;',
  6946. '$mod.d = 1.0 / (3 - 2 - 1);',
  6947. '$mod.d = 1 / 3;',
  6948. '$mod.d = 5.0E-324;',
  6949. '$mod.d = 1.7E308;',
  6950. '$mod.d = 1.00E0;',
  6951. '$mod.d = 2.00E1;',
  6952. '$mod.d = 3.000E0;',
  6953. '$mod.d = -4.00E-0;',
  6954. '$mod.d = -5.00E-1;',
  6955. '$mod.d = Math.pow(10, 3);',
  6956. '$mod.d = 10 % 3;',
  6957. '$mod.d = rtl.trunc(10 / 3);',
  6958. '$mod.d = 1;',
  6959. '$mod.d = 0.1;',
  6960. '$mod.d = 0.3;',
  6961. '$mod.d = -0.1;',
  6962. '$mod.d = -0.3;',
  6963. '$mod.d = -0.003;',
  6964. '$mod.d = -0.123456789;',
  6965. '$mod.d = -300;',
  6966. '$mod.d = -123456;',
  6967. '$mod.d = -1234567.8;',
  6968. '$mod.d = -1.23456789E7;',
  6969. '$mod.d = 1E-12;',
  6970. '$mod.d = -1E-12;',
  6971. '$mod.d = 1.7E308;',
  6972. '$mod.d = -1.7E308;',
  6973. '$mod.d = -9007199254740991;',
  6974. '$mod.d = -9007199254740991;',
  6975. '$mod.d = -9.007199254740992E15;',
  6976. '$mod.d = -9.007199254740992E15;',
  6977. '$mod.d = 9007199254740991;',
  6978. '$mod.d = 0.0;',
  6979. 'Run(1 / 0);',
  6980. 'Run(-1 / 0);',
  6981. '']));
  6982. end;
  6983. procedure TTestModule.TestInteger;
  6984. begin
  6985. StartProgram(false);
  6986. Add([
  6987. 'const',
  6988. ' MinInt = low(NativeInt);',
  6989. ' MaxInt = high(NativeInt);',
  6990. 'type',
  6991. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6992. 'const',
  6993. ' a = low(TMyInt)+High(TMyInt);',
  6994. 'var',
  6995. ' i: TMyInt;',
  6996. 'begin',
  6997. ' i:=-MinInt;',
  6998. ' i:=default(TMyInt);',
  6999. ' i:=low(i)+high(i);',
  7000. '']);
  7001. ConvertProgram;
  7002. CheckSource('TestIntegerRange',
  7003. LinesToStr([
  7004. 'this.MinInt = -9007199254740991;',
  7005. 'this.MaxInt = 9007199254740991;',
  7006. 'this.a = -9007199254740991 + 9007199254740991;',
  7007. 'this.i = 0;',
  7008. '']),
  7009. LinesToStr([
  7010. '$mod.i = - -9007199254740991;',
  7011. '$mod.i = -9007199254740991;',
  7012. '$mod.i = -9007199254740991 + 9007199254740991;',
  7013. '']));
  7014. end;
  7015. procedure TTestModule.TestIntegerRange;
  7016. begin
  7017. StartProgram(false);
  7018. Add([
  7019. 'const',
  7020. ' MinInt = -1;',
  7021. ' MaxInt = +1;',
  7022. 'type',
  7023. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7024. ' TInt2 = 1..3;',
  7025. 'const',
  7026. ' a = low(TMyInt)+High(TMyInt);',
  7027. ' b = low(TInt2)+High(TInt2);',
  7028. ' s1 = [1];',
  7029. ' s2 = [1,2];',
  7030. ' s3 = [1..3];',
  7031. ' s4 = [low(shortint)..high(shortint)];',
  7032. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7033. ' s6 = 1 in s2;',
  7034. 'var',
  7035. ' i: TMyInt;',
  7036. ' i2: TInt2;',
  7037. 'begin',
  7038. ' i:=i2;',
  7039. ' i:=default(TMyInt);',
  7040. ' if i=i2 then ;',
  7041. ' i:=ord(i2);',
  7042. '']);
  7043. ConvertProgram;
  7044. CheckSource('TestIntegerRange',
  7045. LinesToStr([
  7046. 'this.MinInt = -1;',
  7047. 'this.MaxInt = +1;',
  7048. 'this.a = -1 + 1;',
  7049. 'this.b = 1 + 3;',
  7050. 'this.s1 = rtl.createSet(1);',
  7051. 'this.s2 = rtl.createSet(1, 2);',
  7052. 'this.s3 = rtl.createSet(null, 1, 3);',
  7053. 'this.s4 = rtl.createSet(null, -128, 127);',
  7054. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7055. 'this.s6 = 1 in this.s2;',
  7056. 'this.i = 0;',
  7057. 'this.i2 = 0;',
  7058. '']),
  7059. LinesToStr([
  7060. '$mod.i = $mod.i2;',
  7061. '$mod.i = -1;',
  7062. 'if ($mod.i === $mod.i2) ;',
  7063. '$mod.i = $mod.i2;',
  7064. '']));
  7065. end;
  7066. procedure TTestModule.TestIntegerTypecasts;
  7067. begin
  7068. StartProgram(false);
  7069. Add([
  7070. 'var',
  7071. ' i: nativeint;',
  7072. ' b: byte;',
  7073. ' sh: shortint;',
  7074. ' w: word;',
  7075. ' sm: smallint;',
  7076. ' lw: longword;',
  7077. ' li: longint;',
  7078. 'begin',
  7079. ' b:=byte(i);',
  7080. ' sh:=shortint(i);',
  7081. ' w:=word(i);',
  7082. ' sm:=smallint(i);',
  7083. ' lw:=longword(i);',
  7084. ' li:=longint(i);',
  7085. '']);
  7086. ConvertProgram;
  7087. CheckSource('TestIntegerTypecasts',
  7088. LinesToStr([
  7089. 'this.i = 0;',
  7090. 'this.b = 0;',
  7091. 'this.sh = 0;',
  7092. 'this.w = 0;',
  7093. 'this.sm = 0;',
  7094. 'this.lw = 0;',
  7095. 'this.li = 0;',
  7096. '']),
  7097. LinesToStr([
  7098. '$mod.b = $mod.i & 255;',
  7099. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7100. '$mod.w = $mod.i & 65535;',
  7101. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7102. '$mod.lw = $mod.i >>> 0;',
  7103. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7104. '']));
  7105. end;
  7106. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7107. begin
  7108. StartProgram(false);
  7109. Add([
  7110. 'var',
  7111. ' i,j: nativeint;',
  7112. 'begin',
  7113. ' i:=i shr 0;',
  7114. ' i:=i shr 1;',
  7115. ' i:=i shr 3;',
  7116. ' i:=i shr 54;',
  7117. ' i:=j shr i;',
  7118. '']);
  7119. ConvertProgram;
  7120. CheckResolverUnexpectedHints;
  7121. CheckSource('TestInteger_BitwiseShrNativeInt',
  7122. LinesToStr([
  7123. 'this.i = 0;',
  7124. 'this.j = 0;',
  7125. '']),
  7126. LinesToStr([
  7127. '$mod.i = $mod.i;',
  7128. '$mod.i = Math.floor($mod.i / 2);',
  7129. '$mod.i = Math.floor($mod.i / 8);',
  7130. '$mod.i = 0;',
  7131. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7132. '']));
  7133. end;
  7134. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7135. begin
  7136. StartProgram(false);
  7137. Add([
  7138. 'var',
  7139. ' i: nativeint;',
  7140. 'begin',
  7141. ' i:=i shl 0;',
  7142. ' i:=i shl 54;',
  7143. ' i:=123456789012 shl 1;',
  7144. ' i:=i shl 1;',
  7145. '']);
  7146. ConvertProgram;
  7147. CheckResolverUnexpectedHints;
  7148. CheckSource('TestInteger_BitwiseShrNativeInt',
  7149. LinesToStr([
  7150. 'this.i = 0;',
  7151. '']),
  7152. LinesToStr([
  7153. '$mod.i = $mod.i;',
  7154. '$mod.i = 0;',
  7155. '$mod.i = 246913578024;',
  7156. '$mod.i = rtl.shl($mod.i, 1);',
  7157. '']));
  7158. end;
  7159. procedure TTestModule.TestInteger_SystemFunc;
  7160. begin
  7161. StartProgram(true);
  7162. Add([
  7163. 'var',
  7164. ' i: byte;',
  7165. ' s: string;',
  7166. 'begin',
  7167. ' system.inc(i);',
  7168. ' system.str(i,s);',
  7169. ' s:=system.str(i);',
  7170. ' i:=system.low(i);',
  7171. ' i:=system.high(i);',
  7172. ' i:=system.pred(i);',
  7173. ' i:=system.succ(i);',
  7174. ' i:=system.ord(i);',
  7175. '']);
  7176. ConvertProgram;
  7177. CheckResolverUnexpectedHints;
  7178. CheckSource('TestInteger_SystemFunc',
  7179. LinesToStr([
  7180. 'this.i = 0;',
  7181. 'this.s = "";',
  7182. '']),
  7183. LinesToStr([
  7184. '$mod.i += 1;',
  7185. '$mod.s = "" + $mod.i;',
  7186. '$mod.s = "" + $mod.i;',
  7187. '$mod.i = 0;',
  7188. '$mod.i = 255;',
  7189. '$mod.i = $mod.i - 1;',
  7190. '$mod.i = $mod.i + 1;',
  7191. '$mod.i = $mod.i;',
  7192. '']));
  7193. end;
  7194. procedure TTestModule.TestInteger_AssignOutsideConst;
  7195. begin
  7196. StartProgram(false);
  7197. Add([
  7198. 'const',
  7199. ' MinInt = low(longint);',
  7200. ' MaxInt = high(longint);',
  7201. 'type',
  7202. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7203. 'var',
  7204. ' i: TMyInt;',
  7205. ' aByte: byte;',
  7206. ' aShortInt: shortint;',
  7207. ' aWord: word;',
  7208. ' aSmallInt: smallint;',
  7209. ' aLongWord: longword;',
  7210. ' aLongInt: longint;',
  7211. ' aNativeInt: nativeint;',
  7212. ' aNativeUInt: nativeuint;',
  7213. 'begin',
  7214. ' aByte:=$FF;',
  7215. ' aByte:=$100;',
  7216. ' aByte:=-1;',
  7217. ' aByte:=-127;',
  7218. ' aByte:=-128;',
  7219. ' aByte:=-254;',
  7220. ' aByte:=-255;',
  7221. ' aByte:=-256;',
  7222. ' aShortInt:=127;',
  7223. ' aShortInt:=128;',
  7224. ' aShortInt:=-128;',
  7225. ' aShortInt:=-129;',
  7226. ' aWord:=$ffff;',
  7227. ' aWord:=$10000;',
  7228. ' aWord:=-1;',
  7229. ' aWord:=-$ffff;',
  7230. ' aWord:=-$10000;',
  7231. ' aWord:=-$10001;',
  7232. ' aSmallInt:=$7fff;',
  7233. ' aSmallInt:=$8000;',
  7234. ' aSmallInt:=-$8000;',
  7235. ' aSmallInt:=-$8001;',
  7236. ' aLongWord:=$ffffffff;',
  7237. ' aLongWord:=$100000000;',
  7238. ' aLongWord:=-1;',
  7239. ' aLongWord:=-$ffffffff;',
  7240. ' aNativeInt:=$1fffffffffffff;',
  7241. ' aNativeInt:=-$1fffffffffffff;',
  7242. ' aNativeUInt:=$1fffffffffffff;',
  7243. ' aNativeUInt:=-$1fffffffffffff;',
  7244. '']);
  7245. ConvertProgram;
  7246. CheckSource('TestInteger_AssignOutsideConst',
  7247. LinesToStr([
  7248. 'this.MinInt = -2147483648;',
  7249. 'this.MaxInt = 2147483647;',
  7250. 'this.i = 0;',
  7251. 'this.aByte = 0;',
  7252. 'this.aShortInt = 0;',
  7253. 'this.aWord = 0;',
  7254. 'this.aSmallInt = 0;',
  7255. 'this.aLongWord = 0;',
  7256. 'this.aLongInt = 0;',
  7257. 'this.aNativeInt = 0;',
  7258. 'this.aNativeUInt = 0;',
  7259. '']),
  7260. LinesToStr([
  7261. '$mod.aByte = 0xFF;',
  7262. '$mod.aByte = 0;',
  7263. '$mod.aByte = 255;',
  7264. '$mod.aByte = 129;',
  7265. '$mod.aByte = 128;',
  7266. '$mod.aByte = 2;',
  7267. '$mod.aByte = 1;',
  7268. '$mod.aByte = 0;',
  7269. '$mod.aShortInt = 127;',
  7270. '$mod.aShortInt = -128;',
  7271. '$mod.aShortInt = -128;',
  7272. '$mod.aShortInt = 127;',
  7273. '$mod.aWord = 0xffff;',
  7274. '$mod.aWord = 0;',
  7275. '$mod.aWord = 65535;',
  7276. '$mod.aWord = 1;',
  7277. '$mod.aWord = 0;',
  7278. '$mod.aWord = 65535;',
  7279. '$mod.aSmallInt = 0x7fff;',
  7280. '$mod.aSmallInt = -32768;',
  7281. '$mod.aSmallInt = -0x8000;',
  7282. '$mod.aSmallInt = 32767;',
  7283. '$mod.aLongWord = 0xffffffff;',
  7284. '$mod.aLongWord = 0;',
  7285. '$mod.aLongWord = 4294967295;',
  7286. '$mod.aLongWord = 1;',
  7287. '$mod.aNativeInt = 0x1fffffffffffff;',
  7288. '$mod.aNativeInt = -0x1fffffffffffff;',
  7289. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7290. '$mod.aNativeUInt = 1;',
  7291. '']));
  7292. end;
  7293. procedure TTestModule.TestCurrency;
  7294. begin
  7295. StartProgram(false);
  7296. Add([
  7297. 'type',
  7298. ' TCoin = currency;',
  7299. 'const',
  7300. ' a = TCoin(2.7);',
  7301. ' b = a + TCoin(1.7);',
  7302. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7303. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7304. 'var',
  7305. ' c: TCoin = b;',
  7306. ' i: nativeint;',
  7307. ' d: double;',
  7308. ' j: jsvalue;',
  7309. 'function DoIt(c: currency): currency; begin end;',
  7310. 'function GetIt(d: double): double; begin end;',
  7311. 'procedure Write(v: jsvalue); begin end;',
  7312. 'begin',
  7313. ' c:=1.0;',
  7314. ' c:=0.1;',
  7315. ' c:=1.0/3.0;',
  7316. ' c:=1/3;',
  7317. ' c:=a;',
  7318. ' d:=c;',
  7319. ' c:=d;',
  7320. ' c:=currency(c);',
  7321. ' c:=currency(d);',
  7322. ' d:=double(c);',
  7323. ' c:=i;',
  7324. ' c:=currency(i);',
  7325. //' i:=c;', not allowed
  7326. ' i:=nativeint(c);',
  7327. ' c:=c+a;',
  7328. ' c:=-c-a;',
  7329. ' c:=d+c;',
  7330. ' c:=c+d;',
  7331. ' c:=d-c;',
  7332. ' c:=c-d;',
  7333. ' c:=c*a;',
  7334. ' c:=a*c;',
  7335. ' c:=d*c;',
  7336. ' c:=c*d;',
  7337. ' c:=c/a;',
  7338. ' c:=a/c;',
  7339. ' c:=d/c;',
  7340. ' c:=c/d;',
  7341. ' c:=c**a;',
  7342. ' c:=a**c;',
  7343. ' c:=d**c;',
  7344. ' c:=c**d;',
  7345. ' if c=c then ;',
  7346. ' if c=a then ;',
  7347. ' if a=c then ;',
  7348. ' if d=c then ;',
  7349. ' if c=d then ;',
  7350. ' c:=DoIt(c);',
  7351. ' c:=DoIt(i);',
  7352. ' c:=DoIt(d);',
  7353. ' c:=GetIt(c);',
  7354. ' j:=c;',
  7355. ' Write(c);',
  7356. ' c:=default(currency);',
  7357. ' j:=str(c);',
  7358. ' j:=str(c:0:3);',
  7359. '']);
  7360. ConvertProgram;
  7361. CheckSource('TestCurrency',
  7362. LinesToStr([
  7363. 'this.a = 27000;',
  7364. 'this.b = this.a + 17000;',
  7365. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7366. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7367. 'this.c = this.b;',
  7368. 'this.i = 0;',
  7369. 'this.d = 0.0;',
  7370. 'this.j = undefined;',
  7371. 'this.DoIt = function (c) {',
  7372. ' var Result = 0;',
  7373. ' return Result;',
  7374. '};',
  7375. 'this.GetIt = function (d) {',
  7376. ' var Result = 0.0;',
  7377. ' return Result;',
  7378. '};',
  7379. 'this.Write = function (v) {',
  7380. '};',
  7381. '']),
  7382. LinesToStr([
  7383. '$mod.c = 10000;',
  7384. '$mod.c = 1000;',
  7385. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7386. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7387. '$mod.c = $mod.a;',
  7388. '$mod.d = $mod.c / 10000;',
  7389. '$mod.c = rtl.trunc($mod.d * 10000);',
  7390. '$mod.c = $mod.c;',
  7391. '$mod.c = $mod.d * 10000;',
  7392. '$mod.d = $mod.c / 10000;',
  7393. '$mod.c = $mod.i * 10000;',
  7394. '$mod.c = $mod.i * 10000;',
  7395. '$mod.i = rtl.trunc($mod.c / 10000);',
  7396. '$mod.c = $mod.c + $mod.a;',
  7397. '$mod.c = -$mod.c - $mod.a;',
  7398. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7399. '$mod.c = $mod.c + ($mod.d * 10000);',
  7400. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7401. '$mod.c = $mod.c - ($mod.d * 10000);',
  7402. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7403. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7404. '$mod.c = $mod.d * $mod.c;',
  7405. '$mod.c = $mod.c * $mod.d;',
  7406. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7407. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7408. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7409. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7410. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7411. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7412. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7413. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7414. 'if ($mod.c === $mod.c) ;',
  7415. 'if ($mod.c === $mod.a) ;',
  7416. 'if ($mod.a === $mod.c) ;',
  7417. 'if (($mod.d * 10000) === $mod.c) ;',
  7418. 'if ($mod.c === ($mod.d * 10000)) ;',
  7419. '$mod.c = $mod.DoIt($mod.c);',
  7420. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7421. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7422. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7423. '$mod.j = $mod.c / 10000;',
  7424. '$mod.Write($mod.c / 10000);',
  7425. '$mod.c = 0;',
  7426. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7427. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7428. '']));
  7429. end;
  7430. procedure TTestModule.TestForBoolDo;
  7431. begin
  7432. StartProgram(false);
  7433. Add([
  7434. 'var b: boolean;',
  7435. 'begin',
  7436. ' for b:=false to true do ;',
  7437. ' for b:=b downto false do ;',
  7438. ' for b in boolean do ;',
  7439. '']);
  7440. ConvertProgram;
  7441. CheckSource('TestForBoolDo',
  7442. LinesToStr([ // statements
  7443. 'this.b = false;']),
  7444. LinesToStr([ // this.$main
  7445. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7446. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7447. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7448. '']));
  7449. end;
  7450. procedure TTestModule.TestForIntDo;
  7451. begin
  7452. StartProgram(false);
  7453. Add([
  7454. 'var i: longint;',
  7455. 'begin',
  7456. ' for i:=3 to 5 do ;',
  7457. ' for i:=i downto 2 do ;',
  7458. ' for i in byte do ;',
  7459. '']);
  7460. ConvertProgram;
  7461. CheckSource('TestForIntDo',
  7462. LinesToStr([ // statements
  7463. 'this.i = 0;']),
  7464. LinesToStr([ // this.$main
  7465. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7466. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7467. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7468. '']));
  7469. end;
  7470. procedure TTestModule.TestForIntInDo;
  7471. begin
  7472. StartProgram(false);
  7473. Add([
  7474. 'type',
  7475. ' TSetOfInt = set of byte;',
  7476. ' TIntRg = 3..7;',
  7477. ' TSetOfIntRg = set of TIntRg;',
  7478. 'var',
  7479. ' i,i2: longint;',
  7480. ' a1: array of byte;',
  7481. ' a2: array[1..3] of byte;',
  7482. ' soi: TSetOfInt;',
  7483. ' soir: TSetOfIntRg;',
  7484. ' ir: TIntRg;',
  7485. 'begin',
  7486. ' for i in byte do ;',
  7487. ' for i in a1 do ;',
  7488. ' for i in a2 do ;',
  7489. ' for i in [11..13] do ;',
  7490. ' for i in TSetOfInt do ;',
  7491. ' for i in TIntRg do ;',
  7492. ' for i in soi do i2:=i;',
  7493. ' for i in TSetOfIntRg do ;',
  7494. ' for i in soir do ;',
  7495. ' for ir in TIntRg do ;',
  7496. ' for ir in TSetOfIntRg do ;',
  7497. ' for ir in soir do ;',
  7498. '']);
  7499. ConvertProgram;
  7500. CheckSource('TestForIntInDo',
  7501. LinesToStr([ // statements
  7502. 'this.i = 0;',
  7503. 'this.i2 = 0;',
  7504. 'this.a1 = [];',
  7505. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7506. 'this.soi = {};',
  7507. 'this.soir = {};',
  7508. 'this.ir = 0;',
  7509. '']),
  7510. LinesToStr([ // this.$main
  7511. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7512. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7513. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7514. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7515. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7516. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7517. 'for (var $l6 in $mod.soi) {',
  7518. ' $mod.i = +$l6;',
  7519. ' $mod.i2 = $mod.i;',
  7520. '};',
  7521. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7522. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7523. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7524. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7525. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7526. '']));
  7527. end;
  7528. procedure TTestModule.TestCharConst;
  7529. begin
  7530. StartProgram(false);
  7531. Add([
  7532. 'const',
  7533. ' a = #$00F3;',
  7534. ' c: char = ''1'';',
  7535. ' wc: widechar = ''ä'';',
  7536. 'begin',
  7537. ' c:=#0;',
  7538. ' c:=#1;',
  7539. ' c:=#9;',
  7540. ' c:=#10;',
  7541. ' c:=#13;',
  7542. ' c:=#31;',
  7543. ' c:=#32;',
  7544. ' c:=#$A;',
  7545. ' c:=#$0A;',
  7546. ' c:=#$b;',
  7547. ' c:=#$0b;',
  7548. ' c:=^A;',
  7549. ' c:=''"'';',
  7550. ' c:=default(char);',
  7551. ' c:=#$00E4;', // ä
  7552. ' c:=''ä'';',
  7553. ' c:=#$E4;', // ä
  7554. ' c:=#$D800;', // invalid UTF-16
  7555. ' c:=#$DFFF;', // invalid UTF-16
  7556. ' c:=#$FFFF;', // last UCS-2
  7557. ' c:=high(c);', // last UCS-2
  7558. ' c:=#269;',
  7559. '']);
  7560. ConvertProgram;
  7561. CheckSource('TestCharConst',
  7562. LinesToStr([
  7563. 'this.a="ó";',
  7564. 'this.c="1";',
  7565. 'this.wc="ä";'
  7566. ]),
  7567. LinesToStr([
  7568. '$mod.c="\x00";',
  7569. '$mod.c="\x01";',
  7570. '$mod.c="\t";',
  7571. '$mod.c="\n";',
  7572. '$mod.c="\r";',
  7573. '$mod.c="\x1F";',
  7574. '$mod.c=" ";',
  7575. '$mod.c="\n";',
  7576. '$mod.c="\n";',
  7577. '$mod.c="\x0B";',
  7578. '$mod.c="\x0B";',
  7579. '$mod.c="\x01";',
  7580. '$mod.c=''"'';',
  7581. '$mod.c="\x00";',
  7582. '$mod.c = "ä";',
  7583. '$mod.c = "ä";',
  7584. '$mod.c = "ä";',
  7585. '$mod.c="\uD800";',
  7586. '$mod.c="\uDFFF";',
  7587. '$mod.c="\uFFFF";',
  7588. '$mod.c="\uFFFF";',
  7589. '$mod.c = "č";',
  7590. '']));
  7591. end;
  7592. procedure TTestModule.TestChar_Compare;
  7593. begin
  7594. StartProgram(false);
  7595. Add('var');
  7596. Add(' c: char;');
  7597. Add(' b: boolean;');
  7598. Add('begin');
  7599. Add(' b:=c=''1'';');
  7600. Add(' b:=''2''=c;');
  7601. Add(' b:=''3''=''4'';');
  7602. Add(' b:=c<>''5'';');
  7603. Add(' b:=''6''<>c;');
  7604. Add(' b:=c>''7'';');
  7605. Add(' b:=''8''>c;');
  7606. Add(' b:=c>=''9'';');
  7607. Add(' b:=''A''>=c;');
  7608. Add(' b:=c<''B'';');
  7609. Add(' b:=''C''<c;');
  7610. Add(' b:=c<=''D'';');
  7611. Add(' b:=''E''<=c;');
  7612. ConvertProgram;
  7613. CheckSource('TestChar_Compare',
  7614. LinesToStr([
  7615. 'this.c="";',
  7616. 'this.b = false;'
  7617. ]),
  7618. LinesToStr([
  7619. '$mod.b = $mod.c === "1";',
  7620. '$mod.b = "2" === $mod.c;',
  7621. '$mod.b = "3" === "4";',
  7622. '$mod.b = $mod.c !== "5";',
  7623. '$mod.b = "6" !== $mod.c;',
  7624. '$mod.b = $mod.c > "7";',
  7625. '$mod.b = "8" > $mod.c;',
  7626. '$mod.b = $mod.c >= "9";',
  7627. '$mod.b = "A" >= $mod.c;',
  7628. '$mod.b = $mod.c < "B";',
  7629. '$mod.b = "C" < $mod.c;',
  7630. '$mod.b = $mod.c <= "D";',
  7631. '$mod.b = "E" <= $mod.c;',
  7632. '']));
  7633. end;
  7634. procedure TTestModule.TestChar_BuiltInProcs;
  7635. begin
  7636. StartProgram(false);
  7637. Add([
  7638. 'var',
  7639. ' c: char;',
  7640. ' i: longint;',
  7641. ' s: string;',
  7642. 'begin',
  7643. ' i:=ord(c);',
  7644. ' i:=ord(s[i]);',
  7645. ' c:=chr(i);',
  7646. ' c:=pred(c);',
  7647. ' c:=succ(c);',
  7648. ' c:=low(c);',
  7649. ' c:=high(c);',
  7650. ' i:=byte(c);',
  7651. ' i:=word(c);',
  7652. ' i:=longint(c);',
  7653. '']);
  7654. ConvertProgram;
  7655. CheckSource('TestChar_BuiltInProcs',
  7656. LinesToStr([
  7657. 'this.c = "";',
  7658. 'this.i = 0;',
  7659. 'this.s = "";'
  7660. ]),
  7661. LinesToStr([
  7662. '$mod.i = $mod.c.charCodeAt();',
  7663. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7664. '$mod.c = String.fromCharCode($mod.i);',
  7665. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7666. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7667. '$mod.c = "\x00";',
  7668. '$mod.c = "\uFFFF";',
  7669. '$mod.i = $mod.c.charCodeAt() & 255;',
  7670. '$mod.i = $mod.c.charCodeAt();',
  7671. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7672. '']));
  7673. end;
  7674. procedure TTestModule.TestStringConst;
  7675. begin
  7676. StartProgram(false);
  7677. Add([
  7678. '{$H+}',
  7679. 'const',
  7680. ' a = #$00F3#$017C;', // first <256, then >=256
  7681. ' b = string(''a'');',
  7682. ' c = string(''ä'');',
  7683. ' d = UnicodeString(''b'');',
  7684. ' e = UnicodeString(''ö'');',
  7685. ' f = low(a)+high(b);',
  7686. ' g: word = low(a);',
  7687. 'var',
  7688. ' s: string = ''abc'';',
  7689. ' i: longint;',
  7690. 'begin',
  7691. ' s:='''';',
  7692. ' s:=#13#10;',
  7693. ' s:=#9''foo'';',
  7694. ' s:=#$A9;',
  7695. ' s:=''foo''#13''bar'';',
  7696. ' s:=''"'';',
  7697. ' s:=''"''''"'';',
  7698. ' s:=#$20AC;', // euro
  7699. ' s:=#$10437;', // outside BMP
  7700. ' s:=''abc''#$20AC;', // ascii,#
  7701. ' s:=''ä''#$20AC;', // non ascii,#
  7702. ' s:=#$20AC''abc'';', // #, ascii
  7703. ' s:=#$20AC''ä'';', // #, non ascii
  7704. ' s:=default(string);',
  7705. ' s:=concat(s);',
  7706. ' s:=concat(s,''a'',s);',
  7707. ' s:=#250#269;',
  7708. ' i:=low(s)+high(a);',
  7709. //' s:=#$2F804;',
  7710. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7711. '']);
  7712. ConvertProgram;
  7713. CheckSource('TestStringConst',
  7714. LinesToStr([
  7715. 'this.a = "óż";',
  7716. 'this.b = "a";',
  7717. 'this.c = "ä";',
  7718. 'this.d = "b";',
  7719. 'this.e = "ö";',
  7720. 'this.f = 1 + this.b.length;',
  7721. 'this.g = 1;',
  7722. 'this.s="abc";',
  7723. 'this.i = 0;',
  7724. '']),
  7725. LinesToStr([
  7726. '$mod.s="";',
  7727. '$mod.s="\r\n";',
  7728. '$mod.s="\tfoo";',
  7729. '$mod.s="©";',
  7730. '$mod.s="foo\rbar";',
  7731. '$mod.s=''"'';',
  7732. '$mod.s=''"\''"'';',
  7733. '$mod.s="€";',
  7734. '$mod.s="'#$F0#$90#$90#$B7'";',
  7735. '$mod.s = "abc€";',
  7736. '$mod.s = "ä€";',
  7737. '$mod.s = "€abc";',
  7738. '$mod.s = "ۊ";',
  7739. '$mod.s="";',
  7740. '$mod.s = $mod.s;',
  7741. '$mod.s = $mod.s.concat("a", $mod.s);',
  7742. '$mod.s = "úč";',
  7743. '$mod.i = 1 + $mod.a.length;',
  7744. '']));
  7745. end;
  7746. procedure TTestModule.TestStringConst_InvalidUTF16;
  7747. begin
  7748. StartProgram(false);
  7749. Add([
  7750. 'const',
  7751. ' a: char = #$D87E;',
  7752. ' b: string = #$D87E;',
  7753. ' c: string = #$D87E#43;',
  7754. 'begin',
  7755. ' c:=''abc''#$D87E;',
  7756. ' c:=#0#1#2;',
  7757. ' c:=#127;',
  7758. ' c:=#128;',
  7759. ' c:=#255;',
  7760. ' c:=#256;',
  7761. '']);
  7762. ConvertProgram;
  7763. CheckSource('TestStringConst',
  7764. LinesToStr([
  7765. 'this.a = "\uD87E";',
  7766. 'this.b = "\uD87E";',
  7767. 'this.c = "\uD87E+";',
  7768. '']),
  7769. LinesToStr([
  7770. '$mod.c = "abc\uD87E";',
  7771. '$mod.c = "\x00\x01\x02";',
  7772. '$mod.c = "'#127'";',
  7773. '$mod.c = "'#$c2#$80'";',
  7774. '$mod.c = "'#$c3#$BF'";',
  7775. '$mod.c = "'#$c4#$80'";',
  7776. '']));
  7777. end;
  7778. procedure TTestModule.TestStringConstSurrogate;
  7779. begin
  7780. StartProgram(false);
  7781. Add([
  7782. 'var',
  7783. ' s: string;',
  7784. 'begin',
  7785. ' s:=''😊'';', // 1F60A
  7786. '']);
  7787. ConvertProgram;
  7788. CheckSource('TestStringConstSurrogate',
  7789. LinesToStr([
  7790. 'this.s="";'
  7791. ]),
  7792. LinesToStr([
  7793. '$mod.s="😊";'
  7794. ]));
  7795. end;
  7796. procedure TTestModule.TestString_Length;
  7797. begin
  7798. StartProgram(false);
  7799. Add('const c = ''foo'';');
  7800. Add('var');
  7801. Add(' s: string;');
  7802. Add(' i: longint;');
  7803. Add('begin');
  7804. Add(' i:=length(s);');
  7805. Add(' i:=length(s+s);');
  7806. Add(' i:=length(''abc'');');
  7807. Add(' i:=length(c);');
  7808. ConvertProgram;
  7809. CheckSource('TestString_Length',
  7810. LinesToStr([
  7811. 'this.c = "foo";',
  7812. 'this.s = "";',
  7813. 'this.i = 0;',
  7814. '']),
  7815. LinesToStr([
  7816. '$mod.i = $mod.s.length;',
  7817. '$mod.i = ($mod.s+$mod.s).length;',
  7818. '$mod.i = "abc".length;',
  7819. '$mod.i = $mod.c.length;',
  7820. '']));
  7821. end;
  7822. procedure TTestModule.TestString_Compare;
  7823. begin
  7824. StartProgram(false);
  7825. Add('var');
  7826. Add(' s, t: string;');
  7827. Add(' b: boolean;');
  7828. Add('begin');
  7829. Add(' b:=s=t;');
  7830. Add(' b:=s<>t;');
  7831. Add(' b:=s>t;');
  7832. Add(' b:=s>=t;');
  7833. Add(' b:=s<t;');
  7834. Add(' b:=s<=t;');
  7835. ConvertProgram;
  7836. CheckSource('TestString_Compare',
  7837. LinesToStr([ // statements
  7838. 'this.s = "";',
  7839. 'this.t = "";',
  7840. 'this.b =false;'
  7841. ]),
  7842. LinesToStr([ // this.$main
  7843. '$mod.b = $mod.s === $mod.t;',
  7844. '$mod.b = $mod.s !== $mod.t;',
  7845. '$mod.b = $mod.s > $mod.t;',
  7846. '$mod.b = $mod.s >= $mod.t;',
  7847. '$mod.b = $mod.s < $mod.t;',
  7848. '$mod.b = $mod.s <= $mod.t;',
  7849. '']));
  7850. end;
  7851. procedure TTestModule.TestString_SetLength;
  7852. begin
  7853. StartProgram(false);
  7854. Add([
  7855. 'procedure Fly(var s: string);',
  7856. 'begin',
  7857. ' SetLength(s,1);',
  7858. 'end;',
  7859. 'procedure Run(var s: unicodestring);',
  7860. 'begin',
  7861. ' SetLength(s,2);',
  7862. 'end;',
  7863. 'var s: string;',
  7864. ' u: unicodestring;',
  7865. 'begin',
  7866. ' SetLength(s,3);',
  7867. ' SetLength(u,4);',
  7868. '']);
  7869. ConvertProgram;
  7870. CheckSource('TestString_SetLength',
  7871. LinesToStr([ // statements
  7872. 'this.Fly = function (s) {',
  7873. ' s.set(rtl.strSetLength(s.get(), 1));',
  7874. '};',
  7875. 'this.Run = function (s) {',
  7876. ' s.set(rtl.strSetLength(s.get(), 2));',
  7877. '};',
  7878. 'this.s = "";',
  7879. 'this.u = "";',
  7880. '']),
  7881. LinesToStr([ // this.$main
  7882. '$mod.s = rtl.strSetLength($mod.s, 3);',
  7883. '$mod.u = rtl.strSetLength($mod.u, 4);'
  7884. ]));
  7885. end;
  7886. procedure TTestModule.TestString_CharAt;
  7887. begin
  7888. StartProgram(false);
  7889. Add([
  7890. 'var',
  7891. ' s: string;',
  7892. ' c: char;',
  7893. ' b: boolean;',
  7894. 'begin',
  7895. ' b:= s[1] = c;',
  7896. ' b:= c = s[1];',
  7897. ' b:= c <> s[1];',
  7898. ' b:= c > s[1];',
  7899. ' b:= c >= s[1];',
  7900. ' b:= c < s[2];',
  7901. ' b:= c <= s[1];',
  7902. ' s[1] := c;',
  7903. ' s[2+3] := c;']);
  7904. ConvertProgram;
  7905. CheckSource('TestString_CharAt',
  7906. LinesToStr([ // statements
  7907. 'this.s = "";',
  7908. 'this.c = "";',
  7909. 'this.b = false;'
  7910. ]),
  7911. LinesToStr([ // this.$main
  7912. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7913. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7914. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7915. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7916. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7917. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7918. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7919. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7920. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7921. '']));
  7922. end;
  7923. procedure TTestModule.TestStringHMinusFail;
  7924. begin
  7925. StartProgram(false);
  7926. Add([
  7927. '{$H-}',
  7928. 'var s: string;',
  7929. 'begin']);
  7930. ConvertProgram;
  7931. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7932. end;
  7933. procedure TTestModule.TestStr;
  7934. begin
  7935. StartProgram(false);
  7936. Add('var');
  7937. Add(' b: boolean;');
  7938. Add(' i: longint;');
  7939. Add(' d: double;');
  7940. Add(' s: string;');
  7941. Add('begin');
  7942. Add(' str(b,s);');
  7943. Add(' str(i,s);');
  7944. Add(' str(d,s);');
  7945. Add(' str(i:3,s);');
  7946. Add(' str(d:3:2,s);');
  7947. Add(' Str(12.456:12:1,s);');
  7948. Add(' Str(12.456:12,s);');
  7949. Add(' s:=str(b);');
  7950. Add(' s:=str(i);');
  7951. Add(' s:=str(d);');
  7952. Add(' s:=str(i,i);');
  7953. Add(' s:=str(i:3);');
  7954. Add(' s:=str(d:3:2);');
  7955. Add(' s:=str(i:4,i);');
  7956. Add(' s:=str(i,i:5);');
  7957. Add(' s:=str(i:4,i:5);');
  7958. Add(' s:=str(s,s);');
  7959. Add(' s:=str(s,''foo'');');
  7960. ConvertProgram;
  7961. CheckSource('TestStr',
  7962. LinesToStr([ // statements
  7963. 'this.b = false;',
  7964. 'this.i = 0;',
  7965. 'this.d = 0.0;',
  7966. 'this.s = "";',
  7967. '']),
  7968. LinesToStr([ // this.$main
  7969. '$mod.s = ""+$mod.b;',
  7970. '$mod.s = ""+$mod.i;',
  7971. '$mod.s = rtl.floatToStr($mod.d);',
  7972. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7973. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7974. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7975. '$mod.s = rtl.floatToStr(12.456,12);',
  7976. '$mod.s = ""+$mod.b;',
  7977. '$mod.s = ""+$mod.i;',
  7978. '$mod.s = rtl.floatToStr($mod.d);',
  7979. '$mod.s = ""+$mod.i+$mod.i;',
  7980. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7981. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7982. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7983. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7984. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7985. '$mod.s = $mod.s + $mod.s;',
  7986. '$mod.s = $mod.s + "foo";',
  7987. '']));
  7988. end;
  7989. procedure TTestModule.TestBaseType_AnsiStringFail;
  7990. begin
  7991. StartProgram(false);
  7992. Add('var s: AnsiString');
  7993. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7994. ConvertProgram;
  7995. end;
  7996. procedure TTestModule.TestBaseType_WideStringFail;
  7997. begin
  7998. StartProgram(false);
  7999. Add('var s: WideString');
  8000. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8001. ConvertProgram;
  8002. end;
  8003. procedure TTestModule.TestBaseType_ShortStringFail;
  8004. begin
  8005. StartProgram(false);
  8006. Add('var s: ShortString');
  8007. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8008. ConvertProgram;
  8009. end;
  8010. procedure TTestModule.TestBaseType_RawByteStringFail;
  8011. begin
  8012. StartProgram(false);
  8013. Add('var s: RawByteString');
  8014. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8015. ConvertProgram;
  8016. end;
  8017. procedure TTestModule.TestTypeShortstring_Fail;
  8018. begin
  8019. StartProgram(false);
  8020. Add('type t = string[12];');
  8021. Add('var s: t;');
  8022. Add('begin');
  8023. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8024. ConvertProgram;
  8025. end;
  8026. procedure TTestModule.TestCharSet_Custom;
  8027. begin
  8028. StartProgram(false);
  8029. Add([
  8030. 'type',
  8031. ' TCharRg = ''a''..''z'';',
  8032. ' TSetOfCharRg = set of TCharRg;',
  8033. ' TCharRg2 = ''m''..''p'';',
  8034. 'const',
  8035. ' crg: TCharRg = ''b'';',
  8036. 'var',
  8037. ' c: char;',
  8038. ' crg2: TCharRg2;',
  8039. ' s: TSetOfCharRg;',
  8040. 'begin',
  8041. ' c:=crg;',
  8042. ' crg:=c;',
  8043. ' crg2:=crg;',
  8044. ' if c=crg then ;',
  8045. ' if crg=c then ;',
  8046. ' if crg=crg2 then ;',
  8047. ' if c in s then ;',
  8048. ' if crg2 in s then ;',
  8049. ' c:=default(TCharRg);',
  8050. '']);
  8051. ConvertProgram;
  8052. CheckSource('TestCharSet_Custom',
  8053. LinesToStr([ // statements
  8054. 'this.crg = "b";',
  8055. 'this.c = "";',
  8056. 'this.crg2 = "m";',
  8057. 'this.s = {};',
  8058. '']),
  8059. LinesToStr([ // this.$main
  8060. '$mod.c = $mod.crg;',
  8061. '$mod.crg = $mod.c;',
  8062. '$mod.crg2 = $mod.crg;',
  8063. 'if ($mod.c === $mod.crg) ;',
  8064. 'if ($mod.crg === $mod.c) ;',
  8065. 'if ($mod.crg === $mod.crg2) ;',
  8066. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8067. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8068. '$mod.c = "a";',
  8069. '']));
  8070. end;
  8071. procedure TTestModule.TestWideChar;
  8072. begin
  8073. StartProgram(false);
  8074. Add([
  8075. 'procedure Fly(var c: char);',
  8076. 'begin',
  8077. 'end;',
  8078. 'procedure Run(var c: widechar);',
  8079. 'begin',
  8080. 'end;',
  8081. 'var',
  8082. ' c: char;',
  8083. ' wc: widechar;',
  8084. ' w: word;',
  8085. 'begin',
  8086. ' Fly(wc);',
  8087. ' Run(c);',
  8088. ' wc:=WideChar(w);',
  8089. ' w:=ord(wc);',
  8090. '']);
  8091. ConvertProgram;
  8092. CheckSource('TestWideChar_VarArg',
  8093. LinesToStr([ // statements
  8094. 'this.Fly = function (c) {',
  8095. '};',
  8096. 'this.Run = function (c) {',
  8097. '};',
  8098. 'this.c = "";',
  8099. 'this.wc = "";',
  8100. 'this.w = 0;',
  8101. '']),
  8102. LinesToStr([ // this.$main
  8103. '$mod.Fly({',
  8104. ' p: $mod,',
  8105. ' get: function () {',
  8106. ' return this.p.wc;',
  8107. ' },',
  8108. ' set: function (v) {',
  8109. ' this.p.wc = v;',
  8110. ' }',
  8111. '});',
  8112. '$mod.Run({',
  8113. ' p: $mod,',
  8114. ' get: function () {',
  8115. ' return this.p.c;',
  8116. ' },',
  8117. ' set: function (v) {',
  8118. ' this.p.c = v;',
  8119. ' }',
  8120. '});',
  8121. '$mod.wc = String.fromCharCode($mod.w);',
  8122. '$mod.w = $mod.wc.charCodeAt();',
  8123. '',
  8124. '']));
  8125. end;
  8126. procedure TTestModule.TestForCharDo;
  8127. begin
  8128. StartProgram(false);
  8129. Add([
  8130. 'var c: char;',
  8131. 'begin',
  8132. ' for c:=''a'' to ''c'' do ;',
  8133. ' for c:=c downto ''a'' do ;',
  8134. ' for c:=''Б'' to ''Я'' do ;',
  8135. '']);
  8136. ConvertProgram;
  8137. CheckSource('TestForCharDo',
  8138. LinesToStr([ // statements
  8139. 'this.c = "";']),
  8140. LinesToStr([ // this.$main
  8141. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8142. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8143. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8144. '']));
  8145. end;
  8146. procedure TTestModule.TestForCharInDo;
  8147. begin
  8148. StartProgram(false);
  8149. Add([
  8150. 'type',
  8151. ' TSetOfChar = set of char;',
  8152. ' TCharRg = ''a''..''z'';',
  8153. ' TSetOfCharRg = set of TCharRg;',
  8154. 'const Foo = ''foo'';',
  8155. 'var',
  8156. ' c,c2: char;',
  8157. ' s: string;',
  8158. ' a1: array of char;',
  8159. ' a2: array[1..3] of char;',
  8160. ' soc: TSetOfChar;',
  8161. ' socr: TSetOfCharRg;',
  8162. ' cr: TCharRg;',
  8163. 'begin',
  8164. ' for c in foo do ;',
  8165. ' for c in s do ;',
  8166. ' for c in char do ;',
  8167. ' for c in a1 do ;',
  8168. ' for c in a2 do ;',
  8169. ' for c in [''1''..''3''] do ;',
  8170. ' for c in TSetOfChar do ;',
  8171. ' for c in TCharRg do ;',
  8172. ' for c in soc do c2:=c;',
  8173. ' for c in TSetOfCharRg do ;',
  8174. ' for c in socr do ;',
  8175. ' for cr in TCharRg do ;',
  8176. ' for cr in TSetOfCharRg do ;',
  8177. ' for cr in socr do ;',
  8178. '']);
  8179. ConvertProgram;
  8180. CheckSource('TestForCharInDo',
  8181. LinesToStr([ // statements
  8182. 'this.Foo = "foo";',
  8183. 'this.c = "";',
  8184. 'this.c2 = "";',
  8185. 'this.s = "";',
  8186. 'this.a1 = [];',
  8187. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8188. 'this.soc = {};',
  8189. 'this.socr = {};',
  8190. 'this.cr = "a";',
  8191. '']),
  8192. LinesToStr([ // this.$main
  8193. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8194. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8195. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8196. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8197. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8198. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8199. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8200. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8201. 'for (var $l8 in $mod.soc) {',
  8202. ' $mod.c = String.fromCharCode($l8);',
  8203. ' $mod.c2 = $mod.c;',
  8204. '};',
  8205. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8206. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8207. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8208. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8209. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8210. '']));
  8211. end;
  8212. procedure TTestModule.TestProcTwoArgs;
  8213. begin
  8214. StartProgram(false);
  8215. Add('procedure Test(a,b: longint);');
  8216. Add('begin');
  8217. Add('end;');
  8218. Add('begin');
  8219. ConvertProgram;
  8220. CheckSource('TestProcTwoArgs',
  8221. LinesToStr([ // statements
  8222. 'this.Test = function (a,b) {',
  8223. '};'
  8224. ]),
  8225. LinesToStr([ // this.$main
  8226. ''
  8227. ]));
  8228. end;
  8229. procedure TTestModule.TestProc_DefaultValue;
  8230. begin
  8231. StartProgram(false);
  8232. Add('procedure p1(i: longint = 1);');
  8233. Add('begin');
  8234. Add('end;');
  8235. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8236. Add('begin');
  8237. Add('end;');
  8238. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8239. Add('begin');
  8240. Add('end;');
  8241. Add('begin');
  8242. Add(' p1;');
  8243. Add(' p1();');
  8244. Add(' p1(11);');
  8245. Add(' p2;');
  8246. Add(' p2();');
  8247. Add(' p2(12);');
  8248. Add(' p2(13,''b'');');
  8249. Add(' p3();');
  8250. ConvertProgram;
  8251. CheckSource('TestProc_DefaultValue',
  8252. LinesToStr([ // statements
  8253. 'this.p1 = function (i) {',
  8254. '};',
  8255. 'this.p2 = function (i,c) {',
  8256. '};',
  8257. 'this.p3 = function (d,b,s) {',
  8258. '};'
  8259. ]),
  8260. LinesToStr([ // this.$main
  8261. ' $mod.p1(1);',
  8262. ' $mod.p1(1);',
  8263. ' $mod.p1(11);',
  8264. ' $mod.p2(1,"a");',
  8265. ' $mod.p2(1,"a");',
  8266. ' $mod.p2(12,"a");',
  8267. ' $mod.p2(13,"b");',
  8268. ' $mod.p3(1.0,false,"abc");'
  8269. ]));
  8270. end;
  8271. procedure TTestModule.TestFunctionInt;
  8272. begin
  8273. StartProgram(false);
  8274. Add('function MyTest(Bar: longint): longint;');
  8275. Add('begin');
  8276. Add(' Result:=2*bar');
  8277. Add('end;');
  8278. Add('begin');
  8279. ConvertProgram;
  8280. CheckSource('TestFunctionInt',
  8281. LinesToStr([ // statements
  8282. 'this.MyTest = function (Bar) {',
  8283. ' var Result = 0;',
  8284. ' Result = 2*Bar;',
  8285. ' return Result;',
  8286. '};'
  8287. ]),
  8288. LinesToStr([ // this.$main
  8289. ''
  8290. ]));
  8291. end;
  8292. procedure TTestModule.TestFunctionString;
  8293. begin
  8294. StartProgram(false);
  8295. Add('function Test(Bar: string): string;');
  8296. Add('begin');
  8297. Add(' Result:=bar+BAR');
  8298. Add('end;');
  8299. Add('begin');
  8300. ConvertProgram;
  8301. CheckSource('TestFunctionString',
  8302. LinesToStr([ // statements
  8303. 'this.Test = function (Bar) {',
  8304. ' var Result = "";',
  8305. ' Result = Bar+Bar;',
  8306. ' return Result;',
  8307. '};'
  8308. ]),
  8309. LinesToStr([ // this.$main
  8310. ''
  8311. ]));
  8312. end;
  8313. procedure TTestModule.TestIfThen;
  8314. begin
  8315. StartProgram(false);
  8316. Add([
  8317. 'var b: boolean;',
  8318. 'begin',
  8319. ' if b then ;',
  8320. ' if b then else ;']);
  8321. ConvertProgram;
  8322. CheckSource('TestIfThen',
  8323. LinesToStr([ // statements
  8324. 'this.b = false;',
  8325. '']),
  8326. LinesToStr([ // this.$main
  8327. 'if ($mod.b) ;',
  8328. 'if ($mod.b) ;',
  8329. '']));
  8330. end;
  8331. procedure TTestModule.TestForLoop;
  8332. begin
  8333. StartProgram(false);
  8334. Add('var');
  8335. Add(' vI, vJ, vN: longint;');
  8336. Add('begin');
  8337. Add(' VJ:=0;');
  8338. Add(' VN:=3;');
  8339. Add(' for VI:=1 to VN do');
  8340. Add(' begin');
  8341. Add(' VJ:=VJ+VI;');
  8342. Add(' end;');
  8343. ConvertProgram;
  8344. CheckSource('TestForLoop',
  8345. LinesToStr([ // statements
  8346. 'this.vI = 0;',
  8347. 'this.vJ = 0;',
  8348. 'this.vN = 0;'
  8349. ]),
  8350. LinesToStr([ // this.$main
  8351. ' $mod.vJ = 0;',
  8352. ' $mod.vN = 3;',
  8353. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8354. ' $mod.vI = $l;',
  8355. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8356. ' };',
  8357. '']));
  8358. end;
  8359. procedure TTestModule.TestForLoopInsideFunction;
  8360. begin
  8361. StartProgram(false);
  8362. Add('function SumNumbers(Count: longint): longint;');
  8363. Add('var');
  8364. Add(' vI, vJ: longint;');
  8365. Add('begin');
  8366. Add(' vj:=0;');
  8367. Add(' for vi:=1 to count do');
  8368. Add(' begin');
  8369. Add(' vj:=vj+vi;');
  8370. Add(' end;');
  8371. Add('end;');
  8372. Add('begin');
  8373. Add(' sumnumbers(3);');
  8374. ConvertProgram;
  8375. CheckSource('TestForLoopInsideFunction',
  8376. LinesToStr([ // statements
  8377. 'this.SumNumbers = function (Count) {',
  8378. ' var Result = 0;',
  8379. ' var vI = 0;',
  8380. ' var vJ = 0;',
  8381. ' vJ = 0;',
  8382. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8383. ' vI = $l;',
  8384. ' vJ = vJ + vI;',
  8385. ' };',
  8386. ' return Result;',
  8387. '};'
  8388. ]),
  8389. LinesToStr([ // $mod.$main
  8390. ' $mod.SumNumbers(3);'
  8391. ]));
  8392. end;
  8393. procedure TTestModule.TestForLoop_ReadVarAfter;
  8394. begin
  8395. StartProgram(false);
  8396. Add('var');
  8397. Add(' vI: longint;');
  8398. Add('begin');
  8399. Add(' for vi:=1 to 2 do ;');
  8400. Add(' if vi=3 then ;');
  8401. ConvertProgram;
  8402. CheckSource('TestForLoop',
  8403. LinesToStr([ // statements
  8404. 'this.vI = 0;'
  8405. ]),
  8406. LinesToStr([ // this.$main
  8407. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8408. ' if ($mod.vI===3) ;'
  8409. ]));
  8410. end;
  8411. procedure TTestModule.TestForLoop_Nested;
  8412. begin
  8413. StartProgram(false);
  8414. Add('function SumNumbers(Count: longint): longint;');
  8415. Add('var');
  8416. Add(' vI, vJ, vK: longint;');
  8417. Add('begin');
  8418. Add(' VK:=0;');
  8419. Add(' for VI:=1 to count do');
  8420. Add(' begin');
  8421. Add(' for vj:=1 to vi do');
  8422. Add(' begin');
  8423. Add(' vk:=VK+VI;');
  8424. Add(' end;');
  8425. Add(' end;');
  8426. Add('end;');
  8427. Add('begin');
  8428. Add(' sumnumbers(3);');
  8429. ConvertProgram;
  8430. CheckSource('TestForLoopInFunction',
  8431. LinesToStr([ // statements
  8432. 'this.SumNumbers = function (Count) {',
  8433. ' var Result = 0;',
  8434. ' var vI = 0;',
  8435. ' var vJ = 0;',
  8436. ' var vK = 0;',
  8437. ' vK = 0;',
  8438. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8439. ' vI = $l;',
  8440. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8441. ' vJ = $l1;',
  8442. ' vK = vK + vI;',
  8443. ' };',
  8444. ' };',
  8445. ' return Result;',
  8446. '};'
  8447. ]),
  8448. LinesToStr([ // $mod.$main
  8449. ' $mod.SumNumbers(3);'
  8450. ]));
  8451. end;
  8452. procedure TTestModule.TestRepeatUntil;
  8453. begin
  8454. StartProgram(false);
  8455. Add('var');
  8456. Add(' vI, vJ, vN: longint;');
  8457. Add('begin');
  8458. Add(' vn:=3;');
  8459. Add(' vj:=0;');
  8460. Add(' VI:=0;');
  8461. Add(' repeat');
  8462. Add(' VI:=vi+1;');
  8463. Add(' vj:=VJ+vI;');
  8464. Add(' until vi>=vn');
  8465. ConvertProgram;
  8466. CheckSource('TestRepeatUntil',
  8467. LinesToStr([ // statements
  8468. 'this.vI = 0;',
  8469. 'this.vJ = 0;',
  8470. 'this.vN = 0;'
  8471. ]),
  8472. LinesToStr([ // $mod.$main
  8473. ' $mod.vN = 3;',
  8474. ' $mod.vJ = 0;',
  8475. ' $mod.vI = 0;',
  8476. ' do{',
  8477. ' $mod.vI = $mod.vI + 1;',
  8478. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8479. ' }while(!($mod.vI>=$mod.vN));'
  8480. ]));
  8481. end;
  8482. procedure TTestModule.TestAsmBlock;
  8483. begin
  8484. StartProgram(false);
  8485. Add([
  8486. 'var',
  8487. ' vI: longint;',
  8488. 'begin',
  8489. ' vi:=1;',
  8490. ' asm',
  8491. ' if (vI===1) {',
  8492. ' vI=2;',
  8493. //' console.log(''end;'');', ToDo
  8494. ' }',
  8495. ' if (vI===2){ vI=3; }',
  8496. ' end;',
  8497. ' VI:=4;']);
  8498. ConvertProgram;
  8499. CheckSource('TestAsmBlock',
  8500. LinesToStr([ // statements
  8501. 'this.vI = 0;'
  8502. ]),
  8503. LinesToStr([ // $mod.$main
  8504. '$mod.vI = 1;',
  8505. 'if (vI===1) {',
  8506. ' vI=2;',
  8507. '}',
  8508. 'if (vI===2){ vI=3; }',
  8509. ';',
  8510. '$mod.vI = 4;'
  8511. ]));
  8512. end;
  8513. procedure TTestModule.TestAsmPas_Impl;
  8514. begin
  8515. StartUnit(false);
  8516. Add('interface');
  8517. Add('const cIntf: longint = 1;');
  8518. Add('var vIntf: longint;');
  8519. Add('implementation');
  8520. Add('const cImpl: longint = 2;');
  8521. Add('var vImpl: longint;');
  8522. Add('procedure DoIt;');
  8523. Add('const cLoc: longint = 3;');
  8524. Add('var vLoc: longint;');
  8525. Add('begin;');
  8526. Add(' asm');
  8527. //Add(' pas(vIntf)=pas(cIntf);');
  8528. //Add(' pas(vImpl)=pas(cImpl);');
  8529. //Add(' pas(vLoc)=pas(cLoc);');
  8530. Add(' end;');
  8531. Add('end;');
  8532. ConvertUnit;
  8533. CheckSource('TestAsmPas_Impl',
  8534. LinesToStr([
  8535. 'var $impl = $mod.$impl;',
  8536. 'this.cIntf = 1;',
  8537. 'this.vIntf = 0;',
  8538. '']),
  8539. '', // this.$init
  8540. LinesToStr([ // implementation
  8541. '$impl.cImpl = 2;',
  8542. '$impl.vImpl = 0;',
  8543. 'var cLoc = 3;',
  8544. '$impl.DoIt = function () {',
  8545. ' var vLoc = 0;',
  8546. '};',
  8547. '']) );
  8548. end;
  8549. procedure TTestModule.TestTryFinally;
  8550. begin
  8551. StartProgram(false);
  8552. Add('var i: longint;');
  8553. Add('begin');
  8554. Add(' try');
  8555. Add(' i:=0; i:=2 div i;');
  8556. Add(' finally');
  8557. Add(' i:=3');
  8558. Add(' end;');
  8559. ConvertProgram;
  8560. CheckSource('TestTryFinally',
  8561. LinesToStr([ // statements
  8562. 'this.i = 0;'
  8563. ]),
  8564. LinesToStr([ // $mod.$main
  8565. 'try {',
  8566. ' $mod.i = 0;',
  8567. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8568. '} finally {',
  8569. ' $mod.i = 3;',
  8570. '};'
  8571. ]));
  8572. end;
  8573. procedure TTestModule.TestTryExcept;
  8574. begin
  8575. StartProgram(false);
  8576. Add([
  8577. 'type',
  8578. ' TObject = class end;',
  8579. ' Exception = class Msg: string; end;',
  8580. ' EInvalidCast = class(Exception) end;',
  8581. 'var vI: longint;',
  8582. 'begin',
  8583. ' try',
  8584. ' vi:=1;',
  8585. ' except',
  8586. ' vi:=2',
  8587. ' end;',
  8588. ' try',
  8589. ' vi:=3;',
  8590. ' except',
  8591. ' raise;',
  8592. ' end;',
  8593. ' try',
  8594. ' VI:=4;',
  8595. ' except',
  8596. ' on einvalidcast do',
  8597. ' raise;',
  8598. ' on E: exception do',
  8599. ' if e.msg='''' then',
  8600. ' raise e;',
  8601. ' else',
  8602. ' vi:=5',
  8603. ' end;',
  8604. ' try',
  8605. ' VI:=6;',
  8606. ' except',
  8607. ' on einvalidcast do ;',
  8608. ' end;',
  8609. '']);
  8610. ConvertProgram;
  8611. CheckSource('TestTryExcept',
  8612. LinesToStr([ // statements
  8613. 'rtl.createClass(this, "TObject", null, function () {',
  8614. ' this.$init = function () {',
  8615. ' };',
  8616. ' this.$final = function () {',
  8617. ' };',
  8618. '});',
  8619. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8620. ' this.$init = function () {',
  8621. ' $mod.TObject.$init.call(this);',
  8622. ' this.Msg = "";',
  8623. ' };',
  8624. '});',
  8625. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8626. '});',
  8627. 'this.vI = 0;'
  8628. ]),
  8629. LinesToStr([ // $mod.$main
  8630. 'try {',
  8631. ' $mod.vI = 1;',
  8632. '} catch ($e) {',
  8633. ' $mod.vI = 2;',
  8634. '};',
  8635. 'try {',
  8636. ' $mod.vI = 3;',
  8637. '} catch ($e) {',
  8638. ' throw $e;',
  8639. '};',
  8640. 'try {',
  8641. ' $mod.vI = 4;',
  8642. '} catch ($e) {',
  8643. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8644. ' throw $e',
  8645. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8646. ' var E = $e;',
  8647. ' if (E.Msg === "") throw E;',
  8648. ' } else {',
  8649. ' $mod.vI = 5;',
  8650. ' }',
  8651. '};',
  8652. 'try {',
  8653. ' $mod.vI = 6;',
  8654. '} catch ($e) {',
  8655. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8656. ' } else throw $e',
  8657. '};',
  8658. '']));
  8659. end;
  8660. procedure TTestModule.TestTryExcept_ReservedWords;
  8661. begin
  8662. StartProgram(false);
  8663. Add([
  8664. 'type',
  8665. ' TObject = class end;',
  8666. ' Exception = class',
  8667. ' Symbol: string;',
  8668. ' end;',
  8669. 'var &try: longint;',
  8670. 'begin',
  8671. ' try',
  8672. ' &try:=4;',
  8673. ' except',
  8674. ' on Error: exception do',
  8675. ' if errOR.symBol='''' then',
  8676. ' raise ERRor;',
  8677. ' end;',
  8678. '']);
  8679. ConvertProgram;
  8680. CheckSource('TestTryExcept_ReservedWords',
  8681. LinesToStr([ // statements
  8682. 'rtl.createClass(this, "TObject", null, function () {',
  8683. ' this.$init = function () {',
  8684. ' };',
  8685. ' this.$final = function () {',
  8686. ' };',
  8687. '});',
  8688. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8689. ' this.$init = function () {',
  8690. ' $mod.TObject.$init.call(this);',
  8691. ' this.Symbol = "";',
  8692. ' };',
  8693. '});',
  8694. 'this.Try = 0;',
  8695. '']),
  8696. LinesToStr([ // $mod.$main
  8697. 'try {',
  8698. ' $mod.Try = 4;',
  8699. '} catch ($e) {',
  8700. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8701. ' var error = $e;',
  8702. ' if (error.Symbol === "") throw error;',
  8703. ' } else throw $e',
  8704. '};',
  8705. '']));
  8706. end;
  8707. procedure TTestModule.TestIfThenRaiseElse;
  8708. begin
  8709. StartProgram(false);
  8710. Add([
  8711. 'type',
  8712. ' TObject = class',
  8713. ' constructor Create;',
  8714. ' end;',
  8715. 'constructor TObject.Create;',
  8716. 'begin',
  8717. 'end;',
  8718. 'var b: boolean;',
  8719. 'begin',
  8720. ' if b then',
  8721. ' raise TObject.Create',
  8722. ' else',
  8723. ' b:=false;',
  8724. '']);
  8725. ConvertProgram;
  8726. CheckSource('TestIfThenRaiseElse',
  8727. LinesToStr([ // statements
  8728. 'rtl.createClass(this, "TObject", null, function () {',
  8729. ' this.$init = function () {',
  8730. ' };',
  8731. ' this.$final = function () {',
  8732. ' };',
  8733. ' this.Create = function () {',
  8734. ' return this;',
  8735. ' };',
  8736. '});',
  8737. 'this.b = false;',
  8738. '']),
  8739. LinesToStr([ // $mod.$main
  8740. 'if ($mod.b) {',
  8741. ' throw $mod.TObject.$create("Create")}',
  8742. ' else $mod.b = false;',
  8743. '']));
  8744. end;
  8745. procedure TTestModule.TestCaseOf;
  8746. begin
  8747. StartProgram(false);
  8748. Add([
  8749. 'const e: longint; external name ''$e'';',
  8750. 'var vI: longint;',
  8751. 'begin',
  8752. ' case vi of',
  8753. ' 1: ;',
  8754. ' 2: vi:=3;',
  8755. ' e: ;',
  8756. ' else',
  8757. ' VI:=4',
  8758. ' end;']);
  8759. ConvertProgram;
  8760. CheckSource('TestCaseOf',
  8761. LinesToStr([ // statements
  8762. 'this.vI = 0;'
  8763. ]),
  8764. LinesToStr([ // $mod.$main
  8765. 'var $tmp = $mod.vI;',
  8766. 'if ($tmp === 1) {}',
  8767. 'else if ($tmp === 2) {',
  8768. ' $mod.vI = 3}',
  8769. ' else if ($tmp === $e) {}',
  8770. 'else {',
  8771. ' $mod.vI = 4;',
  8772. '};'
  8773. ]));
  8774. end;
  8775. procedure TTestModule.TestCaseOf_UseSwitch;
  8776. begin
  8777. StartProgram(false);
  8778. Converter.UseSwitchStatement:=true;
  8779. Add('var Vi: longint;');
  8780. Add('begin');
  8781. Add(' case vi of');
  8782. Add(' 1: ;');
  8783. Add(' 2: VI:=3;');
  8784. Add(' else');
  8785. Add(' vi:=4');
  8786. Add(' end;');
  8787. ConvertProgram;
  8788. CheckSource('TestCaseOf_UseSwitch',
  8789. LinesToStr([ // statements
  8790. 'this.Vi = 0;'
  8791. ]),
  8792. LinesToStr([ // $mod.$main
  8793. 'switch ($mod.Vi) {',
  8794. 'case 1:',
  8795. ' break;',
  8796. 'case 2:',
  8797. ' $mod.Vi = 3;',
  8798. ' break;',
  8799. 'default:',
  8800. ' $mod.Vi = 4;',
  8801. '};'
  8802. ]));
  8803. end;
  8804. procedure TTestModule.TestCaseOfNoElse;
  8805. begin
  8806. StartProgram(false);
  8807. Add('var Vi: longint;');
  8808. Add('begin');
  8809. Add(' case vi of');
  8810. Add(' 1: begin vi:=2; VI:=3; end;');
  8811. Add(' end;');
  8812. ConvertProgram;
  8813. CheckSource('TestCaseOfNoElse',
  8814. LinesToStr([ // statements
  8815. 'this.Vi = 0;'
  8816. ]),
  8817. LinesToStr([ // $mod.$main
  8818. 'var $tmp = $mod.Vi;',
  8819. 'if ($tmp === 1) {',
  8820. ' $mod.Vi = 2;',
  8821. ' $mod.Vi = 3;',
  8822. '};'
  8823. ]));
  8824. end;
  8825. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8826. begin
  8827. StartProgram(false);
  8828. Converter.UseSwitchStatement:=true;
  8829. Add('var vI: longint;');
  8830. Add('begin');
  8831. Add(' case vi of');
  8832. Add(' 1: begin VI:=2; vi:=3; end;');
  8833. Add(' end;');
  8834. ConvertProgram;
  8835. CheckSource('TestCaseOfNoElse_UseSwitch',
  8836. LinesToStr([ // statements
  8837. 'this.vI = 0;'
  8838. ]),
  8839. LinesToStr([ // $mod.$main
  8840. 'switch ($mod.vI) {',
  8841. 'case 1:',
  8842. ' $mod.vI = 2;',
  8843. ' $mod.vI = 3;',
  8844. ' break;',
  8845. '};'
  8846. ]));
  8847. end;
  8848. procedure TTestModule.TestCaseOfRange;
  8849. begin
  8850. StartProgram(false);
  8851. Add('var vI: longint;');
  8852. Add('begin');
  8853. Add(' case vi of');
  8854. Add(' 1..3: vi:=14;');
  8855. Add(' 4,5: vi:=16;');
  8856. Add(' 6..7,9..10: ;');
  8857. Add(' else ;');
  8858. Add(' end;');
  8859. ConvertProgram;
  8860. CheckSource('TestCaseOfRange',
  8861. LinesToStr([ // statements
  8862. 'this.vI = 0;'
  8863. ]),
  8864. LinesToStr([ // $mod.$main
  8865. 'var $tmp = $mod.vI;',
  8866. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8867. ' $mod.vI = 14',
  8868. '} else if (($tmp === 4) || ($tmp === 5)){',
  8869. ' $mod.vI = 16',
  8870. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8871. ]));
  8872. end;
  8873. procedure TTestModule.TestCaseOfString;
  8874. begin
  8875. StartProgram(false);
  8876. Add([
  8877. 'var s,h: string;',
  8878. 'begin',
  8879. ' case s of',
  8880. ' ''foo'': s:=h;',
  8881. ' ''a''..''z'': h:=s;',
  8882. ' ''ў'', ''ё'': ;',
  8883. ' ''Б''..''Я'': ;',
  8884. ' end;',
  8885. '']);
  8886. ConvertProgram;
  8887. CheckSource('TestCaseOfString',
  8888. LinesToStr([ // statements
  8889. 'this.s = "";',
  8890. 'this.h = "";',
  8891. '']),
  8892. LinesToStr([ // $mod.$main
  8893. 'var $tmp = $mod.s;',
  8894. 'if ($tmp === "foo") {',
  8895. ' $mod.s = $mod.h}',
  8896. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8897. ' $mod.h = $mod.s}',
  8898. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8899. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8900. '']));
  8901. end;
  8902. procedure TTestModule.TestCaseOfChar;
  8903. begin
  8904. StartProgram(false);
  8905. Add([
  8906. 'var s,h: char;',
  8907. 'begin',
  8908. ' case s of',
  8909. ' ''a''..''z'': h:=s;',
  8910. ' ''ä'': ;',
  8911. ' ''ў'', ''ё'': ;',
  8912. ' ''Б''..''Я'': ;',
  8913. ' end;',
  8914. '']);
  8915. ConvertProgram;
  8916. CheckSource('TestCaseOfString',
  8917. LinesToStr([ // statements
  8918. 'this.s = "";',
  8919. 'this.h = "";',
  8920. '']),
  8921. LinesToStr([ // $mod.$main
  8922. 'var $tmp = $mod.s;',
  8923. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8924. ' $mod.h = $mod.s}',
  8925. ' else if ($tmp === "ä") {}',
  8926. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8927. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8928. '']));
  8929. end;
  8930. procedure TTestModule.TestCaseOfExternalClassConst;
  8931. begin
  8932. StartProgram(false);
  8933. Add([
  8934. '{$modeswitch externalclass}',
  8935. 'type',
  8936. ' TBird = class external name ''Bird''',
  8937. ' const e: longint;',
  8938. ' end;',
  8939. 'var vI: longint;',
  8940. 'begin',
  8941. ' case vi of',
  8942. ' 1: vi:=3;',
  8943. ' TBird.e: ;',
  8944. ' end;']);
  8945. ConvertProgram;
  8946. CheckSource('TestCaseOfExternalClassConst',
  8947. LinesToStr([ // statements
  8948. 'this.vI = 0;'
  8949. ]),
  8950. LinesToStr([ // $mod.$main
  8951. 'var $tmp = $mod.vI;',
  8952. 'if ($tmp === 1) {',
  8953. ' $mod.vI = 3}',
  8954. ' else if ($tmp === Bird.e) ;'
  8955. ]));
  8956. end;
  8957. procedure TTestModule.TestDebugger;
  8958. begin
  8959. StartProgram(false);
  8960. Add([
  8961. 'procedure DoIt;',
  8962. 'begin',
  8963. ' deBugger;',
  8964. ' DeBugger();',
  8965. 'end;',
  8966. 'begin',
  8967. ' Debugger;']);
  8968. ConvertProgram;
  8969. CheckSource('TestDebugger',
  8970. LinesToStr([ // statements
  8971. 'this.DoIt = function () {',
  8972. ' debugger;',
  8973. ' debugger;',
  8974. '};',
  8975. '']),
  8976. LinesToStr([ // $mod.$main
  8977. 'debugger;',
  8978. '']));
  8979. end;
  8980. procedure TTestModule.TestArray_Dynamic;
  8981. begin
  8982. StartProgram(false);
  8983. Add([
  8984. 'type',
  8985. ' TArrayInt = array of longint;',
  8986. 'var',
  8987. ' Arr: TArrayInt;',
  8988. ' i: longint;',
  8989. ' b: boolean;',
  8990. 'begin',
  8991. ' SetLength(arr,3);',
  8992. ' arr[0]:=4;',
  8993. ' arr[1]:=length(arr)+arr[0];',
  8994. ' arr[i]:=5;',
  8995. ' arr[arr[i]]:=arr[6];',
  8996. ' i:=low(arr);',
  8997. ' i:=high(arr);',
  8998. ' b:=Assigned(arr);',
  8999. ' Arr:=default(TArrayInt);']);
  9000. ConvertProgram;
  9001. CheckSource('TestArray_Dynamic',
  9002. LinesToStr([ // statements
  9003. 'this.Arr = [];',
  9004. 'this.i = 0;',
  9005. 'this.b = false;'
  9006. ]),
  9007. LinesToStr([ // $mod.$main
  9008. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9009. '$mod.Arr[0] = 4;',
  9010. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9011. '$mod.Arr[$mod.i] = 5;',
  9012. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9013. '$mod.i = 0;',
  9014. '$mod.i = rtl.length($mod.Arr) - 1;',
  9015. '$mod.b = rtl.length($mod.Arr) > 0;',
  9016. '$mod.Arr = [];',
  9017. '']));
  9018. end;
  9019. procedure TTestModule.TestArray_Dynamic_Nil;
  9020. begin
  9021. StartProgram(false);
  9022. Add('type');
  9023. Add(' TArrayInt = array of longint;');
  9024. Add('var');
  9025. Add(' Arr: TArrayInt;');
  9026. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9027. Add('begin');
  9028. Add(' arr:=nil;');
  9029. Add(' if arr=nil then;');
  9030. Add(' if nil=arr then;');
  9031. Add(' if arr<>nil then;');
  9032. Add(' if nil<>arr then;');
  9033. Add(' DoIt(nil,nil);');
  9034. ConvertProgram;
  9035. CheckSource('TestArray_Dynamic',
  9036. LinesToStr([ // statements
  9037. 'this.Arr = [];',
  9038. 'this.DoIt = function(i,j){',
  9039. '};'
  9040. ]),
  9041. LinesToStr([ // $mod.$main
  9042. '$mod.Arr = [];',
  9043. 'if (rtl.length($mod.Arr) === 0) ;',
  9044. 'if (rtl.length($mod.Arr) === 0) ;',
  9045. 'if (rtl.length($mod.Arr) > 0) ;',
  9046. 'if (rtl.length($mod.Arr) > 0) ;',
  9047. '$mod.DoIt([],[]);',
  9048. '']));
  9049. end;
  9050. procedure TTestModule.TestArray_DynMultiDimensional;
  9051. begin
  9052. StartProgram(false);
  9053. Add([
  9054. 'type',
  9055. ' TArrayInt = array of longint;',
  9056. ' TArrayArrayInt = array of TArrayInt;',
  9057. 'var',
  9058. ' Arr: TArrayInt;',
  9059. ' Arr2: TArrayArrayInt;',
  9060. ' i: longint;',
  9061. 'begin',
  9062. ' arr2:=nil;',
  9063. ' if arr2=nil then;',
  9064. ' if nil=arr2 then;',
  9065. ' i:=low(arr2);',
  9066. ' i:=low(arr2[1]);',
  9067. ' i:=high(arr2);',
  9068. ' i:=high(arr2[2]);',
  9069. ' arr2[3]:=arr;',
  9070. ' arr2[4][5]:=i;',
  9071. ' i:=arr2[6][7];',
  9072. ' arr2[8,9]:=i;',
  9073. ' i:=arr2[10,11];',
  9074. ' SetLength(arr2,14);',
  9075. ' SetLength(arr2[15],16);']);
  9076. ConvertProgram;
  9077. CheckSource('TestArray_Dynamic',
  9078. LinesToStr([ // statements
  9079. 'this.Arr = [];',
  9080. 'this.Arr2 = [];',
  9081. 'this.i = 0;'
  9082. ]),
  9083. LinesToStr([ // $mod.$main
  9084. '$mod.Arr2 = [];',
  9085. 'if (rtl.length($mod.Arr2) === 0) ;',
  9086. 'if (rtl.length($mod.Arr2) === 0) ;',
  9087. '$mod.i = 0;',
  9088. '$mod.i = 0;',
  9089. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9090. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9091. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9092. '$mod.Arr2[4][5] = $mod.i;',
  9093. '$mod.i = $mod.Arr2[6][7];',
  9094. '$mod.Arr2[8][9] = $mod.i;',
  9095. '$mod.i = $mod.Arr2[10][11];',
  9096. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9097. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9098. '']));
  9099. end;
  9100. procedure TTestModule.TestArray_DynamicAssign;
  9101. begin
  9102. StartProgram(false);
  9103. Add([
  9104. 'type',
  9105. ' TArrayInt = array of longint;',
  9106. ' TArrayArrayInt = array of TArrayInt;',
  9107. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9108. 'begin',
  9109. 'end;',
  9110. 'procedure Fly(var a: TArrayInt);',
  9111. 'begin',
  9112. 'end;',
  9113. 'var',
  9114. ' Arr: TArrayInt;',
  9115. ' Arr2: TArrayArrayInt;',
  9116. 'begin',
  9117. ' arr:=nil;',
  9118. ' arr2:=nil;',
  9119. ' arr2[1]:=nil;',
  9120. ' arr2[2]:=arr;',
  9121. ' Run(arr,arr,arr);',
  9122. ' Fly(arr);',
  9123. ' Run(arr2[4],arr2[5],arr2[6]);',
  9124. ' Fly(arr2[7]);',
  9125. '']);
  9126. ConvertProgram;
  9127. CheckSource('TestArray_DynamicAssign',
  9128. LinesToStr([ // statements
  9129. 'this.Run = function (a, b, c) {',
  9130. '};',
  9131. 'this.Fly = function (a) {',
  9132. '};',
  9133. 'this.Arr = [];',
  9134. 'this.Arr2 = [];',
  9135. '']),
  9136. LinesToStr([ // $mod.$main
  9137. '$mod.Arr = [];',
  9138. '$mod.Arr2 = [];',
  9139. '$mod.Arr2[1] = [];',
  9140. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9141. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9142. '$mod.Fly({',
  9143. ' p: $mod,',
  9144. ' get: function () {',
  9145. ' return this.p.Arr;',
  9146. ' },',
  9147. ' set: function (v) {',
  9148. ' this.p.Arr = v;',
  9149. ' }',
  9150. '});',
  9151. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9152. '$mod.Fly({',
  9153. ' a: 7,',
  9154. ' p: $mod.Arr2,',
  9155. ' get: function () {',
  9156. ' return this.p[this.a];',
  9157. ' },',
  9158. ' set: function (v) {',
  9159. ' this.p[this.a] = v;',
  9160. ' }',
  9161. '});',
  9162. '']));
  9163. end;
  9164. procedure TTestModule.TestArray_StaticInt;
  9165. begin
  9166. StartProgram(false);
  9167. Add('type');
  9168. Add(' TArrayInt = array[2..4] of longint;');
  9169. Add('var');
  9170. Add(' Arr: TArrayInt;');
  9171. Add(' Arr2: TArrayInt = (5,6,7);');
  9172. Add(' i: longint;');
  9173. Add(' b: boolean;');
  9174. Add('begin');
  9175. Add(' arr[2]:=4;');
  9176. Add(' arr[3]:=arr[2]+arr[3];');
  9177. Add(' arr[i]:=5;');
  9178. Add(' arr[arr[i]]:=arr[high(arr)];');
  9179. Add(' i:=low(arr);');
  9180. Add(' i:=high(arr);');
  9181. Add(' b:=arr[2]=arr[3];');
  9182. Add(' arr:=default(TArrayInt);');
  9183. ConvertProgram;
  9184. CheckSource('TestArray_StaticInt',
  9185. LinesToStr([ // statements
  9186. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9187. 'this.Arr2 = [5, 6, 7];',
  9188. 'this.i = 0;',
  9189. 'this.b = false;'
  9190. ]),
  9191. LinesToStr([ // $mod.$main
  9192. '$mod.Arr[0] = 4;',
  9193. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9194. '$mod.Arr[$mod.i-2] = 5;',
  9195. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9196. '$mod.i = 2;',
  9197. '$mod.i = 4;',
  9198. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9199. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9200. '']));
  9201. end;
  9202. procedure TTestModule.TestArray_StaticBool;
  9203. begin
  9204. StartProgram(false);
  9205. Add('type');
  9206. Add(' TBools = array[boolean] of boolean;');
  9207. Add(' TBool2 = array[true..true] of boolean;');
  9208. Add('var');
  9209. Add(' Arr: TBools;');
  9210. Add(' Arr2: TBool2;');
  9211. Add(' Arr3: TBools = (true,false);');
  9212. Add(' b: boolean;');
  9213. Add('begin');
  9214. Add(' b:=low(arr);');
  9215. Add(' b:=high(arr);');
  9216. Add(' arr[true]:=false;');
  9217. Add(' arr[false]:=arr[b] or arr[true];');
  9218. Add(' arr[b]:=true;');
  9219. Add(' arr[arr[b]]:=arr[high(arr)];');
  9220. Add(' b:=arr[false]=arr[true];');
  9221. Add(' b:=low(arr2);');
  9222. Add(' b:=high(arr2);');
  9223. Add(' arr2[true]:=true;');
  9224. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9225. Add(' arr2[b]:=false;');
  9226. ConvertProgram;
  9227. CheckSource('TestArray_StaticBool',
  9228. LinesToStr([ // statements
  9229. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9230. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9231. 'this.Arr3 = [true, false];',
  9232. 'this.b = false;'
  9233. ]),
  9234. LinesToStr([ // $mod.$main
  9235. '$mod.b = false;',
  9236. '$mod.b = true;',
  9237. '$mod.Arr[1] = false;',
  9238. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9239. '$mod.Arr[+$mod.b] = true;',
  9240. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9241. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9242. '$mod.b = true;',
  9243. '$mod.b = true;',
  9244. '$mod.Arr2[0] = true;',
  9245. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9246. '$mod.Arr2[1-$mod.b] = false;',
  9247. '']));
  9248. end;
  9249. procedure TTestModule.TestArray_StaticChar;
  9250. begin
  9251. StartProgram(false);
  9252. Add([
  9253. 'type',
  9254. ' TChars = array[char] of char;',
  9255. ' TChars2 = array[''a''..''z''] of char;',
  9256. 'var',
  9257. ' Arr: TChars;',
  9258. ' Arr2: TChars2;',
  9259. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9260. ' Arr4: array[11..13] of char = ''pas'';',
  9261. ' Arr5: array[21..22] of char = ''äö'';',
  9262. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9263. ' c: char;',
  9264. ' b: boolean;',
  9265. 'begin',
  9266. ' c:=low(arr);',
  9267. ' c:=high(arr);',
  9268. ' arr[''B'']:=''a'';',
  9269. ' arr[''D'']:=arr[c];',
  9270. ' arr[c]:=arr[''d''];',
  9271. ' arr[arr[c]]:=arr[high(arr)];',
  9272. ' b:=arr[low(arr)]=arr[''e''];',
  9273. ' c:=low(arr2);',
  9274. ' c:=high(arr2);',
  9275. ' arr2[''b'']:=''f'';',
  9276. ' arr2[''a'']:=arr2[c];',
  9277. ' arr2[c]:=arr2[''g''];']);
  9278. ConvertProgram;
  9279. CheckSource('TestArray_StaticChar',
  9280. LinesToStr([ // statements
  9281. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9282. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9283. 'this.Arr3 = ["p", "a", "s"];',
  9284. 'this.Arr4 = ["p", "a", "s"];',
  9285. 'this.Arr5 = ["ä", "ö"];',
  9286. 'this.Arr6 = ["ä", "ö"];',
  9287. 'this.c = "";',
  9288. 'this.b = false;',
  9289. '']),
  9290. LinesToStr([ // $mod.$main
  9291. '$mod.c = "\x00";',
  9292. '$mod.c = "\uFFFF";',
  9293. '$mod.Arr[66] = "a";',
  9294. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9295. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9296. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9297. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9298. '$mod.c = "a";',
  9299. '$mod.c = "z";',
  9300. '$mod.Arr2[1] = "f";',
  9301. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9302. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9303. '']));
  9304. end;
  9305. procedure TTestModule.TestArray_StaticMultiDim;
  9306. begin
  9307. StartProgram(false);
  9308. Add([
  9309. 'type',
  9310. ' TArrayInt = array[1..3] of longint;',
  9311. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9312. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  9313. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  9314. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  9315. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  9316. 'var',
  9317. ' Arr: TArrayInt;',
  9318. ' Arr2: TArrayArrayInt;',
  9319. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9320. ' Arr4: TArrayArrayInt;',
  9321. ' ArrDim2: TArrayDim2Int;',
  9322. ' ArrDim3: TArrayDim3Int;',
  9323. ' ArrDim4: TArrayDim4Int;',
  9324. ' i: longint;',
  9325. 'begin',
  9326. ' i:=low(arr);',
  9327. ' i:=low(arr2);',
  9328. ' i:=low(arr2[5]);',
  9329. ' i:=high(arr);',
  9330. ' i:=high(arr2);',
  9331. ' i:=high(arr2[6]);',
  9332. ' arr2[5]:=arr;',
  9333. ' arr2[6][2]:=i;',
  9334. ' i:=arr2[6][3];',
  9335. ' arr2[6,3]:=i;',
  9336. ' i:=arr2[5,2];',
  9337. ' arr2:=arr2;',// clone multi dim static array
  9338. ' arr3:=arr3;',// clone anonymous multi dim static array
  9339. ' arr4:=arr4;',
  9340. ' Arr:=Arr;',
  9341. ' ArrDim2:=ArrDim2;',
  9342. ' ArrDim3:=ArrDim3;',
  9343. ' ArrDim4:=ArrDim4;',
  9344. '']);
  9345. ConvertProgram;
  9346. CheckSource('TestArray_StaticMultiDim',
  9347. LinesToStr([ // statements
  9348. 'this.TArrayArrayInt$clone = function (a) {',
  9349. ' var b = [];',
  9350. ' b.length = 2;',
  9351. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9352. ' return b;',
  9353. '};',
  9354. 'this.TArrayArrayArrayInt$clone = function (a) {',
  9355. ' var b = [];',
  9356. ' b.length = 2;',
  9357. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  9358. ' return b;',
  9359. '};',
  9360. 'this.TArrayDim2Int$clone = function (a) {',
  9361. ' var b = [];',
  9362. ' b.length = 2;',
  9363. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9364. ' return b;',
  9365. '};',
  9366. 'this.TArrayDim3Int$clone = function (a) {',
  9367. ' var b = [];',
  9368. ' b.length = 2;',
  9369. ' for (var c = 0; c < 2; c++) {',
  9370. ' var d = b[c] = [];',
  9371. ' d.length = 3;',
  9372. ' var e = a[c];',
  9373. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  9374. ' };',
  9375. ' return b;',
  9376. '};',
  9377. 'this.TArrayDim4Int$clone = function (a) {',
  9378. ' var b = [];',
  9379. ' b.length = 2;',
  9380. ' for (var c = 0; c < 2; c++) {',
  9381. ' var d = b[c] = [];',
  9382. ' d.length = 3;',
  9383. ' var e = a[c];',
  9384. ' for (var f = 0; f < 3; f++) {',
  9385. ' var g = d[f] = [];',
  9386. ' g.length = 4;',
  9387. ' var h = e[f];',
  9388. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  9389. ' };',
  9390. ' };',
  9391. ' return b;',
  9392. '};',
  9393. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9394. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9395. 'this.Arr3$a$clone = function (a) {',
  9396. ' var b = [];',
  9397. ' b.length = 2;',
  9398. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9399. ' return b;',
  9400. '};',
  9401. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9402. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  9403. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  9404. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  9405. 'this.ArrDim4 = rtl.arraySetLength(',
  9406. ' null,',
  9407. ' 0,',
  9408. ' 2,',
  9409. ' 3,',
  9410. ' 4,',
  9411. ' 5',
  9412. ');',
  9413. 'this.i = 0;'
  9414. ]),
  9415. LinesToStr([ // $mod.$main
  9416. '$mod.i = 1;',
  9417. '$mod.i = 5;',
  9418. '$mod.i = 1;',
  9419. '$mod.i = 3;',
  9420. '$mod.i = 6;',
  9421. '$mod.i = 3;',
  9422. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9423. '$mod.Arr2[1][1] = $mod.i;',
  9424. '$mod.i = $mod.Arr2[1][2];',
  9425. '$mod.Arr2[1][2] = $mod.i;',
  9426. '$mod.i = $mod.Arr2[0][1];',
  9427. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9428. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9429. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  9430. '$mod.Arr = $mod.Arr.slice(0);',
  9431. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  9432. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  9433. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  9434. '']));
  9435. end;
  9436. procedure TTestModule.TestArray_StaticInFunction;
  9437. begin
  9438. StartProgram(false);
  9439. Add([
  9440. 'const TArrayInt = 3;',
  9441. 'const TArrayArrayInt = 4;',
  9442. 'procedure DoIt;',
  9443. 'type',
  9444. ' TArrayInt = array[1..3] of longint;',
  9445. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9446. 'var',
  9447. ' Arr: TArrayInt;',
  9448. ' Arr2: TArrayArrayInt;',
  9449. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9450. ' i: longint;',
  9451. 'begin',
  9452. ' arr2[5]:=arr;',
  9453. ' arr2:=arr2;',// clone multi dim static array
  9454. ' arr3:=arr3;',// clone multi dim anonymous static array
  9455. 'end;',
  9456. 'begin',
  9457. '']);
  9458. ConvertProgram;
  9459. CheckSource('TestArray_StaticInFunction',
  9460. LinesToStr([ // statements
  9461. 'this.TArrayInt = 3;',
  9462. 'this.TArrayArrayInt = 4;',
  9463. 'var TArrayArrayInt$1$clone = function (a) {',
  9464. ' var b = [];',
  9465. ' b.length = 2;',
  9466. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9467. ' return b;',
  9468. '};',
  9469. 'var Arr3$a$clone = function (a) {',
  9470. ' var b = [];',
  9471. ' b.length = 2;',
  9472. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9473. ' return b;',
  9474. '};',
  9475. 'this.DoIt = function () {',
  9476. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9477. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9478. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9479. ' var i = 0;',
  9480. ' Arr2[0] = Arr.slice(0);',
  9481. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9482. ' Arr3 = Arr3$a$clone(Arr3);',
  9483. '};',
  9484. '']),
  9485. LinesToStr([ // $mod.$main
  9486. '']));
  9487. end;
  9488. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9489. begin
  9490. StartProgram(false);
  9491. Add([
  9492. 'type',
  9493. ' TArrayInt = array[1..3,1..2] of longint;',
  9494. 'var',
  9495. ' a,b: TArrayInt;',
  9496. 'begin',
  9497. ' if a=b then ;',
  9498. '']);
  9499. SetExpectedPasResolverError('compare static array is not supported',
  9500. nXIsNotSupported);
  9501. ConvertProgram;
  9502. end;
  9503. procedure TTestModule.TestArrayOfRecord;
  9504. begin
  9505. StartProgram(false);
  9506. Add([
  9507. 'type',
  9508. ' TRec = record',
  9509. ' Int: longint;',
  9510. ' end;',
  9511. ' TArrayRec = array of TRec;',
  9512. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9513. 'begin',
  9514. 'end;',
  9515. 'var',
  9516. ' Arr: TArrayRec;',
  9517. ' r: TRec;',
  9518. ' i: longint;',
  9519. 'begin',
  9520. ' SetLength(arr,3);',
  9521. ' arr[0].int:=4;',
  9522. ' arr[1].int:=length(arr)+arr[2].int;',
  9523. ' arr[arr[i].int].int:=arr[5].int;',
  9524. ' arr[7]:=r;',
  9525. ' r:=arr[8];',
  9526. ' i:=low(arr);',
  9527. ' i:=high(arr);',
  9528. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9529. ConvertProgram;
  9530. CheckSource('TestArrayOfRecord',
  9531. LinesToStr([ // statements
  9532. 'rtl.recNewT(this, "TRec", function () {',
  9533. ' this.Int = 0;',
  9534. ' this.$eq = function (b) {',
  9535. ' return this.Int === b.Int;',
  9536. ' };',
  9537. ' this.$assign = function (s) {',
  9538. ' this.Int = s.Int;',
  9539. ' return this;',
  9540. ' };',
  9541. '});',
  9542. 'this.DoIt = function (vd, vc, vv) {',
  9543. '};',
  9544. 'this.Arr = [];',
  9545. 'this.r = this.TRec.$new();',
  9546. 'this.i = 0;'
  9547. ]),
  9548. LinesToStr([ // $mod.$main
  9549. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9550. '$mod.Arr[0].Int = 4;',
  9551. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9552. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9553. '$mod.Arr[7].$assign($mod.r);',
  9554. '$mod.r.$assign($mod.Arr[8]);',
  9555. '$mod.i = 0;',
  9556. '$mod.i = rtl.length($mod.Arr)-1;',
  9557. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9558. '']));
  9559. end;
  9560. procedure TTestModule.TestArray_StaticRecord;
  9561. begin
  9562. StartProgram(false);
  9563. Add([
  9564. 'type',
  9565. ' TRec = record',
  9566. ' Int: longint;',
  9567. ' end;',
  9568. ' TArrayRec = array[1..2] of TRec;',
  9569. 'var',
  9570. ' Arr: TArrayRec;',
  9571. 'begin',
  9572. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9573. '']);
  9574. ConvertProgram;
  9575. CheckSource('TestArray_StaticRecord',
  9576. LinesToStr([ // statements
  9577. 'rtl.recNewT(this, "TRec", function () {',
  9578. ' this.Int = 0;',
  9579. ' this.$eq = function (b) {',
  9580. ' return this.Int === b.Int;',
  9581. ' };',
  9582. ' this.$assign = function (s) {',
  9583. ' this.Int = s.Int;',
  9584. ' return this;',
  9585. ' };',
  9586. '});',
  9587. 'this.TArrayRec$clone = function (a) {',
  9588. ' var b = [];',
  9589. ' b.length = 2;',
  9590. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  9591. ' return b;',
  9592. '};',
  9593. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9594. '']),
  9595. LinesToStr([ // $mod.$main
  9596. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9597. end;
  9598. procedure TTestModule.TestArrayOfSet;
  9599. begin
  9600. StartProgram(false);
  9601. Add([
  9602. 'type',
  9603. ' TFlag = (big,small);',
  9604. ' TSetOfFlag = set of tflag;',
  9605. ' TArrayFlag = array of TSetOfFlag;',
  9606. 'procedure DoIt(const a: Tarrayflag);',
  9607. 'begin',
  9608. 'end;',
  9609. 'var',
  9610. ' f: TFlag;',
  9611. ' s: TSetOfFlag;',
  9612. ' Arr: TArrayFlag;',
  9613. ' i: longint;',
  9614. 'begin',
  9615. ' SetLength(arr,3);',
  9616. ' arr[0]:=s;',
  9617. ' arr[1]:=[big];',
  9618. ' arr[2]:=[big]+s;',
  9619. ' arr[3]:=s+[big];',
  9620. ' arr[4]:=arr[5];',
  9621. ' s:=arr[6];',
  9622. ' i:=low(arr);',
  9623. ' i:=high(arr);',
  9624. ' DoIt(arr);',
  9625. ' DoIt([s]);',
  9626. ' DoIt([[],s]);',
  9627. ' DoIt([s,[]]);',
  9628. '']);
  9629. ConvertProgram;
  9630. CheckSource('TestArrayOfSet',
  9631. LinesToStr([ // statements
  9632. 'this.TFlag = {',
  9633. ' "0": "big",',
  9634. ' big: 0,',
  9635. ' "1": "small",',
  9636. ' small: 1',
  9637. '};',
  9638. 'this.DoIt = function (a) {',
  9639. '};',
  9640. 'this.f = 0;',
  9641. 'this.s = {};',
  9642. 'this.Arr = [];',
  9643. 'this.i = 0;',
  9644. '']),
  9645. LinesToStr([ // $mod.$main
  9646. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9647. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9648. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9649. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9650. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9651. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9652. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9653. '$mod.i = 0;',
  9654. '$mod.i = rtl.length($mod.Arr) - 1;',
  9655. '$mod.DoIt($mod.Arr);',
  9656. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9657. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9658. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9659. '']));
  9660. end;
  9661. procedure TTestModule.TestArray_DynAsParam;
  9662. begin
  9663. StartProgram(false);
  9664. Add([
  9665. 'type integer = longint;',
  9666. 'type TArrInt = array of integer;',
  9667. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9668. 'var vJ: TArrInt;',
  9669. 'begin',
  9670. ' vg:=vg;',
  9671. ' vj:=vh;',
  9672. ' vi:=vi;',
  9673. ' doit(vg,vg,vg);',
  9674. ' doit(vh,vh,vj);',
  9675. ' doit(vi,vi,vi);',
  9676. ' doit(vj,vj,vj);',
  9677. 'end;',
  9678. 'var i: TArrInt;',
  9679. 'begin',
  9680. ' doit(i,i,i);']);
  9681. ConvertProgram;
  9682. CheckSource('TestArray_DynAsParams',
  9683. LinesToStr([ // statements
  9684. 'this.DoIt = function (vG,vH,vI) {',
  9685. ' var vJ = [];',
  9686. ' vG = rtl.arrayRef(vG);',
  9687. ' vJ = rtl.arrayRef(vH);',
  9688. ' vI.set(rtl.arrayRef(vI.get()));',
  9689. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9690. ' get: function () {',
  9691. ' return vG;',
  9692. ' },',
  9693. ' set: function (v) {',
  9694. ' vG = v;',
  9695. ' }',
  9696. ' });',
  9697. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9698. ' get: function () {',
  9699. ' return vJ;',
  9700. ' },',
  9701. ' set: function (v) {',
  9702. ' vJ = v;',
  9703. ' }',
  9704. ' });',
  9705. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9706. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9707. ' get: function () {',
  9708. ' return vJ;',
  9709. ' },',
  9710. ' set: function (v) {',
  9711. ' vJ = v;',
  9712. ' }',
  9713. ' });',
  9714. '};',
  9715. 'this.i = [];'
  9716. ]),
  9717. LinesToStr([
  9718. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9719. ' p: $mod,',
  9720. ' get: function () {',
  9721. ' return this.p.i;',
  9722. ' },',
  9723. ' set: function (v) {',
  9724. ' this.p.i = v;',
  9725. ' }',
  9726. '});'
  9727. ]));
  9728. end;
  9729. procedure TTestModule.TestArray_StaticAsParam;
  9730. begin
  9731. StartProgram(false);
  9732. Add([
  9733. 'type integer = longint;',
  9734. 'type TArrInt = array[1..2] of integer;',
  9735. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9736. 'var vJ: TArrInt;',
  9737. 'begin',
  9738. ' vg:=vg;',
  9739. ' vj:=vh;',
  9740. ' vi:=vi;',
  9741. ' doit(vg,vg,vg);',
  9742. ' doit(vh,vh,vj);',
  9743. ' doit(vi,vi,vi);',
  9744. ' doit(vj,vj,vj);',
  9745. 'end;',
  9746. 'var i: TArrInt;',
  9747. 'begin',
  9748. ' doit(i,i,i);']);
  9749. ConvertProgram;
  9750. CheckSource('TestArray_StaticAsParams',
  9751. LinesToStr([ // statements
  9752. 'this.DoIt = function (vG,vH,vI) {',
  9753. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9754. ' vG = vG.slice(0);',
  9755. ' vJ = vH.slice(0);',
  9756. ' vI.set(vI.get().slice(0));',
  9757. ' $mod.DoIt(vG.slice(0), vG, {',
  9758. ' get: function () {',
  9759. ' return vG;',
  9760. ' },',
  9761. ' set: function (v) {',
  9762. ' vG = v;',
  9763. ' }',
  9764. ' });',
  9765. ' $mod.DoIt(vH.slice(0), vH, {',
  9766. ' get: function () {',
  9767. ' return vJ;',
  9768. ' },',
  9769. ' set: function (v) {',
  9770. ' vJ = v;',
  9771. ' }',
  9772. ' });',
  9773. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9774. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9775. ' get: function () {',
  9776. ' return vJ;',
  9777. ' },',
  9778. ' set: function (v) {',
  9779. ' vJ = v;',
  9780. ' }',
  9781. ' });',
  9782. '};',
  9783. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9784. ]),
  9785. LinesToStr([
  9786. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9787. ' p: $mod,',
  9788. ' get: function () {',
  9789. ' return this.p.i;',
  9790. ' },',
  9791. ' set: function (v) {',
  9792. ' this.p.i = v;',
  9793. ' }',
  9794. '});'
  9795. ]));
  9796. end;
  9797. procedure TTestModule.TestArrayElement_AsParams;
  9798. begin
  9799. StartProgram(false);
  9800. Add('type integer = longint;');
  9801. Add('type TArrayInt = array of integer;');
  9802. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9803. Add('var vJ: tarrayint;');
  9804. Add('begin');
  9805. Add(' vi:=vi;');
  9806. Add(' doit(vi,vi,vi);');
  9807. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9808. Add('end;');
  9809. Add('var a: TArrayInt;');
  9810. Add('begin');
  9811. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9812. ConvertProgram;
  9813. CheckSource('TestArrayElement_AsParams',
  9814. LinesToStr([ // statements
  9815. 'this.DoIt = function (vG,vH,vI) {',
  9816. ' var vJ = [];',
  9817. ' vI.set(vI.get());',
  9818. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9819. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9820. ' a:1+3,',
  9821. ' p:vJ,',
  9822. ' get: function () {',
  9823. ' return this.p[this.a];',
  9824. ' },',
  9825. ' set: function (v) {',
  9826. ' this.p[this.a] = v;',
  9827. ' }',
  9828. ' });',
  9829. '};',
  9830. 'this.a = [];'
  9831. ]),
  9832. LinesToStr([
  9833. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9834. ' a: 1+6,',
  9835. ' p: $mod.a,',
  9836. ' get: function () {',
  9837. ' return this.p[this.a];',
  9838. ' },',
  9839. ' set: function (v) {',
  9840. ' this.p[this.a] = v;',
  9841. ' }',
  9842. '});'
  9843. ]));
  9844. end;
  9845. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9846. begin
  9847. StartProgram(false);
  9848. Add('type Integer = longint;');
  9849. Add('type TArrayInt = array of integer;');
  9850. Add('function GetArr(vB: integer = 0): tarrayint;');
  9851. Add('begin');
  9852. Add('end;');
  9853. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9854. Add('begin');
  9855. Add('end;');
  9856. Add('begin');
  9857. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9858. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9859. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9860. ConvertProgram;
  9861. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9862. LinesToStr([ // statements
  9863. 'this.GetArr = function (vB) {',
  9864. ' var Result = [];',
  9865. ' return Result;',
  9866. '};',
  9867. 'this.DoIt = function (vG,vH,vI) {',
  9868. '};'
  9869. ]),
  9870. LinesToStr([
  9871. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9872. ' a: 1+3,',
  9873. ' p: $mod.GetArr(0),',
  9874. ' get: function () {',
  9875. ' return this.p[this.a];',
  9876. ' },',
  9877. ' set: function (v) {',
  9878. ' this.p[this.a] = v;',
  9879. ' }',
  9880. '});',
  9881. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9882. ' a: 2+3,',
  9883. ' p: $mod.GetArr(0),',
  9884. ' get: function () {',
  9885. ' return this.p[this.a];',
  9886. ' },',
  9887. ' set: function (v) {',
  9888. ' this.p[this.a] = v;',
  9889. ' }',
  9890. '});',
  9891. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9892. ' a: 3+3,',
  9893. ' p: $mod.GetArr(9),',
  9894. ' get: function () {',
  9895. ' return this.p[this.a];',
  9896. ' },',
  9897. ' set: function (v) {',
  9898. ' this.p[this.a] = v;',
  9899. ' }',
  9900. '});',
  9901. '']));
  9902. end;
  9903. procedure TTestModule.TestArrayEnumTypeRange;
  9904. begin
  9905. StartProgram(false);
  9906. Add([
  9907. 'type',
  9908. ' TEnum = (red,blue);',
  9909. ' TEnumArray = array[TEnum] of longint;',
  9910. 'var',
  9911. ' e: TEnum;',
  9912. ' i: longint;',
  9913. ' a: TEnumArray;',
  9914. ' numbers: TEnumArray = (1,2);',
  9915. ' names: array[TEnum] of string = (''red'',''blue'');',
  9916. 'begin',
  9917. ' e:=low(a);',
  9918. ' e:=high(a);',
  9919. ' i:=a[red];',
  9920. ' a[e]:=a[e];']);
  9921. ConvertProgram;
  9922. CheckSource('TestArrayEnumTypeRange',
  9923. LinesToStr([ // statements
  9924. ' this.TEnum = {',
  9925. ' "0": "red",',
  9926. ' red: 0,',
  9927. ' "1": "blue",',
  9928. ' blue: 1',
  9929. '};',
  9930. 'this.e = 0;',
  9931. 'this.i = 0;',
  9932. 'this.a = rtl.arraySetLength(null,0,2);',
  9933. 'this.numbers = [1, 2];',
  9934. 'this.names = ["red", "blue"];',
  9935. '']),
  9936. LinesToStr([ // $mod.$main
  9937. '$mod.e = $mod.TEnum.red;',
  9938. '$mod.e = $mod.TEnum.blue;',
  9939. '$mod.i = $mod.a[$mod.TEnum.red];',
  9940. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9941. '']));
  9942. end;
  9943. procedure TTestModule.TestArray_SetLengthOutArg;
  9944. begin
  9945. StartProgram(false);
  9946. Add([
  9947. 'type TArrInt = array of longint;',
  9948. 'procedure DoIt(out a: TArrInt);',
  9949. 'begin',
  9950. ' SetLength(a,2);',
  9951. 'end;',
  9952. 'begin',
  9953. '']);
  9954. ConvertProgram;
  9955. CheckSource('TestArray_SetLengthOutArg',
  9956. LinesToStr([ // statements
  9957. 'this.DoIt = function (a) {',
  9958. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9959. '};',
  9960. '']),
  9961. LinesToStr([
  9962. '']));
  9963. end;
  9964. procedure TTestModule.TestArray_SetLengthProperty;
  9965. begin
  9966. StartProgram(false);
  9967. Add('type');
  9968. Add(' TArrInt = array of longint;');
  9969. Add(' TObject = class');
  9970. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9971. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9972. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9973. Add(' end;');
  9974. Add('var Obj: TObject;');
  9975. Add('begin');
  9976. Add(' SetLength(Obj.Colors,2);');
  9977. ConvertProgram;
  9978. CheckSource('TestArray_SetLengthProperty',
  9979. LinesToStr([ // statements
  9980. 'rtl.createClass(this, "TObject", null, function () {',
  9981. ' this.$init = function () {',
  9982. ' };',
  9983. ' this.$final = function () {',
  9984. ' };',
  9985. '});',
  9986. 'this.Obj = null;',
  9987. '']),
  9988. LinesToStr([
  9989. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9990. '']));
  9991. end;
  9992. procedure TTestModule.TestArray_SetLengthMultiDim;
  9993. begin
  9994. StartProgram(false);
  9995. Add([
  9996. 'type',
  9997. ' TArrArrInt = array of array of longint;',
  9998. ' TArrStaInt = array of array[1..2] of longint;',
  9999. 'var',
  10000. ' a: TArrArrInt;',
  10001. ' b: TArrStaInt;',
  10002. 'begin',
  10003. ' SetLength(a,2);',
  10004. ' SetLength(a,3,4);',
  10005. ' SetLength(b,5);',
  10006. '']);
  10007. ConvertProgram;
  10008. CheckSource('TestArray_SetLengthMultiDim',
  10009. LinesToStr([ // statements
  10010. 'this.a = [];',
  10011. 'this.b = [];',
  10012. '']),
  10013. LinesToStr([
  10014. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10015. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10016. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10017. '']));
  10018. end;
  10019. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10020. begin
  10021. StartProgram(false);
  10022. Add([
  10023. 'type',
  10024. ' TStaArr1 = array[1..3] of boolean;',
  10025. //' TStaArr2 = array[5..6] of TStaArr1;',
  10026. ' TDynArr1StaArr1 = array of TStaArr1;',
  10027. //' TDynArr1StaArr2 = array of TStaArr2;',
  10028. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10029. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10030. 'var',
  10031. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10032. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10033. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10034. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10035. 'begin',
  10036. ' SetLength(DynArr1StaArr1,11);',
  10037. ' SetLength(DynArr2StaArr1,12);',
  10038. ' SetLength(DynArr2StaArr1[13],14);',
  10039. ' SetLength(DynArr2StaArr1,15,16);',
  10040. //' SetLength(DynArr1StaArr2,21);',
  10041. //' SetLength(DynArr2StaArr2,22);',
  10042. //' SetLength(DynArr2StaArr2[23],24);',
  10043. //' SetLength(DynArr2StaArr2,25,26);',
  10044. '']);
  10045. ConvertProgram;
  10046. CheckSource('TestArray_DynOfStatic',
  10047. LinesToStr([ // statements
  10048. 'this.DynArr1StaArr1 = [];',
  10049. 'this.DynArr2StaArr1 = [];',
  10050. '']),
  10051. LinesToStr([ // $mod.$main
  10052. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10053. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10054. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10055. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10056. ' $mod.DynArr2StaArr1,',
  10057. ' false,',
  10058. ' 15,',
  10059. ' 16,',
  10060. ' "s",',
  10061. ' 3',
  10062. ');',
  10063. '']));
  10064. end;
  10065. procedure TTestModule.TestArray_OpenArrayOfString;
  10066. begin
  10067. StartProgram(false);
  10068. Add('procedure DoIt(const a: array of String);');
  10069. Add('var');
  10070. Add(' i: longint;');
  10071. Add(' s: string;');
  10072. Add('begin');
  10073. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10074. Add('end;');
  10075. Add('var s: string;');
  10076. Add('begin');
  10077. Add(' DoIt([]);');
  10078. Add(' DoIt([s,''foo'','''',s+s]);');
  10079. ConvertProgram;
  10080. CheckSource('TestArray_OpenArrayOfString',
  10081. LinesToStr([ // statements
  10082. 'this.DoIt = function (a) {',
  10083. ' var i = 0;',
  10084. ' var s = "";',
  10085. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10086. ' i = $l;',
  10087. ' s = a[rtl.length(a) - i - 1];',
  10088. ' };',
  10089. '};',
  10090. 'this.s = "";',
  10091. '']),
  10092. LinesToStr([
  10093. '$mod.DoIt([]);',
  10094. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10095. '']));
  10096. end;
  10097. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10098. begin
  10099. StartProgram(false);
  10100. Add([
  10101. 'type TArr = array of char;',
  10102. 'var',
  10103. ' c: char;',
  10104. ' s: string;',
  10105. ' a: TArr;',
  10106. 'procedure Run(const a: array of char);',
  10107. 'begin',
  10108. ' Run(c);',
  10109. ' Run(s);',
  10110. 'end;',
  10111. 'begin',
  10112. ' a:=c;',
  10113. ' a:=s;',
  10114. ' a:=#13;',
  10115. ' a:=''Foo'';',
  10116. ' Run(c);',
  10117. ' Run(s);',
  10118. '']);
  10119. ConvertProgram;
  10120. CheckSource('TestArray_ArrayOfCharAssignString',
  10121. LinesToStr([ // statements
  10122. 'this.c = "";',
  10123. 'this.s = "";',
  10124. 'this.a = [];',
  10125. 'this.Run = function (a) {',
  10126. ' $mod.Run($mod.c.split(""));',
  10127. ' $mod.Run($mod.s.split(""));',
  10128. '};',
  10129. '']),
  10130. LinesToStr([
  10131. '$mod.a = $mod.c.split("");',
  10132. '$mod.a = $mod.s.split("");',
  10133. '$mod.a = "\r".split("");',
  10134. '$mod.a = "Foo".split("");',
  10135. '$mod.Run($mod.c.split(""));',
  10136. '$mod.Run($mod.s.split(""));',
  10137. '']));
  10138. end;
  10139. procedure TTestModule.TestArray_ConstRef;
  10140. begin
  10141. StartProgram(false);
  10142. Add([
  10143. 'type TArr = array of word;',
  10144. 'procedure Run(constref a: TArr);',
  10145. 'begin',
  10146. 'end;',
  10147. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10148. 'var l: TArr;',
  10149. 'begin',
  10150. ' Run(l);',
  10151. ' Run(a);',
  10152. ' Run(b);',
  10153. ' Run(c);',
  10154. ' Run(d);',
  10155. ' Run(e);',
  10156. 'end;',
  10157. 'begin',
  10158. '']);
  10159. ConvertProgram;
  10160. CheckResolverUnexpectedHints();
  10161. CheckSource('TestArray_ConstRef',
  10162. LinesToStr([ // statements
  10163. 'this.Run = function (a) {',
  10164. '};',
  10165. 'this.Fly = function (a, b, c, d, e) {',
  10166. ' var l = [];',
  10167. ' $mod.Run(l);',
  10168. ' $mod.Run(a);',
  10169. ' $mod.Run(b.get());',
  10170. ' $mod.Run(c.get());',
  10171. ' $mod.Run(d);',
  10172. ' $mod.Run(e);',
  10173. '};',
  10174. '']),
  10175. LinesToStr([
  10176. '']));
  10177. end;
  10178. procedure TTestModule.TestArray_Concat;
  10179. begin
  10180. StartProgram(false);
  10181. Add([
  10182. 'type',
  10183. ' integer = longint;',
  10184. ' TFlag = (big,small);',
  10185. ' TFlags = set of TFlag;',
  10186. ' TRec = record',
  10187. ' i: integer;',
  10188. ' end;',
  10189. ' TArrInt = array of integer;',
  10190. ' TArrRec = array of TRec;',
  10191. ' TArrFlag = array of TFlag;',
  10192. ' TArrSet = array of TFlags;',
  10193. ' TArrJSValue = array of jsvalue;',
  10194. 'var',
  10195. ' ArrInt: tarrint;',
  10196. ' ArrRec: tarrrec;',
  10197. ' ArrFlag: tarrflag;',
  10198. ' ArrSet: tarrset;',
  10199. ' ArrJSValue: tarrjsvalue;',
  10200. 'begin',
  10201. ' arrint:=concat(arrint);',
  10202. ' arrint:=concat(arrint,arrint);',
  10203. ' arrint:=concat(arrint,arrint,arrint);',
  10204. ' arrrec:=concat(arrrec);',
  10205. ' arrrec:=concat(arrrec,arrrec);',
  10206. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  10207. ' arrset:=concat(arrset);',
  10208. ' arrset:=concat(arrset,arrset);',
  10209. ' arrset:=concat(arrset,arrset,arrset);',
  10210. ' arrjsvalue:=concat(arrjsvalue);',
  10211. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  10212. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  10213. ' arrint:=concat([1],arrint);',
  10214. ' arrflag:=concat([big]);',
  10215. ' arrflag:=concat([big],arrflag);',
  10216. ' arrflag:=concat(arrflag,[small]);',
  10217. '']);
  10218. ConvertProgram;
  10219. CheckSource('TestArray_Concat',
  10220. LinesToStr([ // statements
  10221. 'this.TFlag = {',
  10222. ' "0": "big",',
  10223. ' big: 0,',
  10224. ' "1": "small",',
  10225. ' small: 1',
  10226. '};',
  10227. 'rtl.recNewT(this, "TRec", function () {',
  10228. ' this.i = 0;',
  10229. ' this.$eq = function (b) {',
  10230. ' return this.i === b.i;',
  10231. ' };',
  10232. ' this.$assign = function (s) {',
  10233. ' this.i = s.i;',
  10234. ' return this;',
  10235. ' };',
  10236. '});',
  10237. 'this.ArrInt = [];',
  10238. 'this.ArrRec = [];',
  10239. 'this.ArrFlag = [];',
  10240. 'this.ArrSet = [];',
  10241. 'this.ArrJSValue = [];',
  10242. '']),
  10243. LinesToStr([ // $mod.$main
  10244. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10245. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10246. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10247. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10248. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10249. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10250. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10251. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10252. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10253. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10254. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10255. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10256. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10257. '$mod.ArrFlag = [$mod.TFlag.big];',
  10258. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10259. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10260. '']));
  10261. end;
  10262. procedure TTestModule.TestArray_Copy;
  10263. begin
  10264. StartProgram(false);
  10265. Add([
  10266. 'type',
  10267. ' integer = longint;',
  10268. ' TFlag = (big,small);',
  10269. ' TFlags = set of TFlag;',
  10270. ' TRec = record',
  10271. ' i: integer;',
  10272. ' end;',
  10273. ' TArrInt = array of integer;',
  10274. ' TArrRec = array of TRec;',
  10275. ' TArrSet = array of TFlags;',
  10276. ' TArrJSValue = array of jsvalue;',
  10277. 'var',
  10278. ' ArrInt: tarrint;',
  10279. ' ArrRec: tarrrec;',
  10280. ' ArrSet: tarrset;',
  10281. ' ArrJSValue: tarrjsvalue;',
  10282. 'begin',
  10283. ' arrint:=copy(arrint);',
  10284. ' arrint:=copy(arrint,2);',
  10285. ' arrint:=copy(arrint,3,4);',
  10286. ' arrint:=copy([1,1],1,2);',
  10287. ' arrrec:=copy(arrrec);',
  10288. ' arrrec:=copy(arrrec,5);',
  10289. ' arrrec:=copy(arrrec,6,7);',
  10290. ' arrset:=copy(arrset);',
  10291. ' arrset:=copy(arrset,8);',
  10292. ' arrset:=copy(arrset,9,10);',
  10293. ' arrjsvalue:=copy(arrjsvalue);',
  10294. ' arrjsvalue:=copy(arrjsvalue,11);',
  10295. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10296. ' ']);
  10297. ConvertProgram;
  10298. CheckSource('TestArray_Copy',
  10299. LinesToStr([ // statements
  10300. 'this.TFlag = {',
  10301. ' "0": "big",',
  10302. ' big: 0,',
  10303. ' "1": "small",',
  10304. ' small: 1',
  10305. '};',
  10306. 'rtl.recNewT(this, "TRec", function () {',
  10307. ' this.i = 0;',
  10308. ' this.$eq = function (b) {',
  10309. ' return this.i === b.i;',
  10310. ' };',
  10311. ' this.$assign = function (s) {',
  10312. ' this.i = s.i;',
  10313. ' return this;',
  10314. ' };',
  10315. '});',
  10316. 'this.ArrInt = [];',
  10317. 'this.ArrRec = [];',
  10318. 'this.ArrSet = [];',
  10319. 'this.ArrJSValue = [];',
  10320. '']),
  10321. LinesToStr([ // $mod.$main
  10322. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10323. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10324. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10325. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10326. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10327. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10328. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10329. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10330. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10331. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10332. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10333. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10334. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10335. '']));
  10336. end;
  10337. procedure TTestModule.TestArray_InsertDelete;
  10338. begin
  10339. StartProgram(false);
  10340. Add([
  10341. 'type',
  10342. ' integer = longint;',
  10343. ' TFlag = (big,small);',
  10344. ' TFlags = set of TFlag;',
  10345. ' TRec = record',
  10346. ' i: integer;',
  10347. ' end;',
  10348. ' TArrInt = array of integer;',
  10349. ' TArrRec = array of TRec;',
  10350. ' TArrSet = array of TFlags;',
  10351. ' TArrJSValue = array of jsvalue;',
  10352. ' TArrArrInt = array of TArrInt;',
  10353. 'var',
  10354. ' ArrInt: tarrint;',
  10355. ' ArrRec: tarrrec;',
  10356. ' ArrSet: tarrset;',
  10357. ' ArrJSValue: tarrjsvalue;',
  10358. ' ArrArrInt: TArrArrInt;',
  10359. 'begin',
  10360. ' Insert(1,arrint,2);',
  10361. ' Insert(arrint[3],arrint,4);',
  10362. ' Insert(arrrec[5],arrrec,6);',
  10363. ' Insert(arrset[7],arrset,7);',
  10364. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10365. ' Insert(10,arrjsvalue,11);',
  10366. ' Insert([23],arrarrint,22);',
  10367. ' Delete(arrint,12,13);',
  10368. ' Delete(arrrec,14,15);',
  10369. ' Delete(arrset,17,18);',
  10370. ' Delete(arrjsvalue,19,10);']);
  10371. ConvertProgram;
  10372. CheckSource('TestArray_InsertDelete',
  10373. LinesToStr([ // statements
  10374. 'this.TFlag = {',
  10375. ' "0": "big",',
  10376. ' big: 0,',
  10377. ' "1": "small",',
  10378. ' small: 1',
  10379. '};',
  10380. 'rtl.recNewT(this, "TRec", function () {',
  10381. ' this.i = 0;',
  10382. ' this.$eq = function (b) {',
  10383. ' return this.i === b.i;',
  10384. ' };',
  10385. ' this.$assign = function (s) {',
  10386. ' this.i = s.i;',
  10387. ' return this;',
  10388. ' };',
  10389. '});',
  10390. 'this.ArrInt = [];',
  10391. 'this.ArrRec = [];',
  10392. 'this.ArrSet = [];',
  10393. 'this.ArrJSValue = [];',
  10394. 'this.ArrArrInt = [];',
  10395. '']),
  10396. LinesToStr([ // $mod.$main
  10397. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10398. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10399. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10400. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10401. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10402. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10403. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10404. '$mod.ArrInt.splice(12, 13);',
  10405. '$mod.ArrRec.splice(14, 15);',
  10406. '$mod.ArrSet.splice(17, 18);',
  10407. '$mod.ArrJSValue.splice(19, 10);',
  10408. '']));
  10409. end;
  10410. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10411. begin
  10412. Parser.Options:=Parser.Options+[po_cassignments];
  10413. StartProgram(false);
  10414. Add([
  10415. '{$modeswitch arrayoperators}',
  10416. 'type',
  10417. ' integer = longint;',
  10418. ' TArrInt = array of integer;',
  10419. ' TArrStr = array of string;',
  10420. 'const',
  10421. ' Ints: TArrInt = (1,2,3);',
  10422. ' Aliases: TarrStr = (''foo'',''b'');',
  10423. ' OneInt: TArrInt = (7);',
  10424. ' OneStr: array of integer = (7);',
  10425. ' Chars: array of char = ''aoc'';',
  10426. ' Names: array of string = (''a'',''foo'');',
  10427. ' NameCount = low(Names)+high(Names)+length(Names);',
  10428. 'var i: integer;',
  10429. 'begin',
  10430. ' Ints:=[];',
  10431. ' Ints:=[1,1];',
  10432. ' Ints:=[1]+[2];',
  10433. ' Ints:=[2];',
  10434. ' Ints:=[]+ints;',
  10435. ' Ints:=Ints+[];',
  10436. ' Ints:=Ints+OneInt;',
  10437. ' Ints:=Ints+[1,1];',
  10438. ' Ints:=[i,i]+Ints;',
  10439. ' Ints:=[1]+[i]+[3];',
  10440. '']);
  10441. ConvertProgram;
  10442. CheckSource('TestArray_DynArrayConstObjFPC',
  10443. LinesToStr([ // statements
  10444. 'this.Ints = [1, 2, 3];',
  10445. 'this.Aliases = ["foo", "b"];',
  10446. 'this.OneInt = [7];',
  10447. 'this.OneStr = [7];',
  10448. 'this.Chars = ["a", "o", "c"];',
  10449. 'this.Names = ["a", "foo"];',
  10450. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10451. 'this.i = 0;',
  10452. '']),
  10453. LinesToStr([ // $mod.$main
  10454. '$mod.Ints = [];',
  10455. '$mod.Ints = [1, 1];',
  10456. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10457. '$mod.Ints = [2];',
  10458. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10459. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10460. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10461. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10462. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10463. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10464. '']));
  10465. end;
  10466. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10467. begin
  10468. StartProgram(false);
  10469. // Note: const c = [1,1]; defines a set!
  10470. Add([
  10471. '{$mode delphi}',
  10472. 'type',
  10473. ' integer = longint;',
  10474. ' TArrInt = array of integer;',
  10475. ' TArrStr = array of string;',
  10476. 'const',
  10477. ' Ints: TArrInt = [1,1,2];',
  10478. ' Aliases: TarrStr = [''foo'',''b''];',
  10479. ' OneInt: TArrInt = [7];',
  10480. ' OneStr: array of integer = [7]+[8];',
  10481. ' Chars: array of char = ''aoc'';',
  10482. ' Names: array of string = [''a'',''a''];',
  10483. ' NameCount = low(Names)+high(Names)+length(Names);',
  10484. 'begin',
  10485. '']);
  10486. ConvertProgram;
  10487. CheckSource('TestArray_DynArrayConstDelphi',
  10488. LinesToStr([ // statements
  10489. 'this.Ints = [1, 1, 2];',
  10490. 'this.Aliases = ["foo", "b"];',
  10491. 'this.OneInt = [7];',
  10492. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10493. 'this.Chars = ["a", "o", "c"];',
  10494. 'this.Names = ["a", "a"];',
  10495. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10496. '']),
  10497. LinesToStr([ // $mod.$main
  10498. '']));
  10499. end;
  10500. procedure TTestModule.TestArray_ArrayLitAsParam;
  10501. begin
  10502. StartProgram(false);
  10503. Add([
  10504. '{$modeswitch arrayoperators}',
  10505. 'type',
  10506. ' integer = longint;',
  10507. ' TArrInt = array of integer;',
  10508. ' TArrSet = array of (red,green,blue);',
  10509. 'procedure DoOpenInt(const a: array of integer); forward;',
  10510. 'procedure DoInt(const a: TArrInt);',
  10511. 'begin',
  10512. ' DoInt(a+[1]);',
  10513. ' DoInt([1]+a);',
  10514. ' DoOpenInt(a);',
  10515. ' DoOpenInt(a+[1]);',
  10516. ' DoOpenInt([1]+a);',
  10517. 'end;',
  10518. 'procedure DoOpenInt(const a: array of integer);',
  10519. 'begin',
  10520. ' DoOpenInt(a+[1]);',
  10521. ' DoOpenInt([1]+a);',
  10522. ' DoInt(a);',
  10523. ' DoInt(a+[1]);',
  10524. ' DoInt([1]+a);',
  10525. 'end;',
  10526. 'procedure DoSet(const a: TArrSet);',
  10527. 'begin',
  10528. ' DoSet(a+[red]);',
  10529. ' DoSet([blue]+a);',
  10530. 'end;',
  10531. 'var',
  10532. ' i: TArrInt;',
  10533. ' s: TArrSet;',
  10534. 'begin',
  10535. ' DoInt([1]);',
  10536. ' DoInt([1]+[2]);',
  10537. ' DoInt(i+[1]);',
  10538. ' DoInt([1]+i);',
  10539. ' DoOpenInt([1]);',
  10540. ' DoOpenInt([1]+[2]);',
  10541. ' DoOpenInt(i+[1]);',
  10542. ' DoOpenInt([1]+i);',
  10543. ' DoSet([red]);',
  10544. ' DoSet([blue]+[green]);',
  10545. ' DoSet(s+[blue]);',
  10546. ' DoSet([red]+s);',
  10547. '']);
  10548. ConvertProgram;
  10549. CheckSource('TestArray_ArrayLitAsParam',
  10550. LinesToStr([ // statements
  10551. 'this.TArrSet$a = {',
  10552. ' "0": "red",',
  10553. ' red: 0,',
  10554. ' "1": "green",',
  10555. ' green: 1,',
  10556. ' "2": "blue",',
  10557. ' blue: 2',
  10558. '};',
  10559. 'this.DoInt = function (a) {',
  10560. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10561. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10562. ' $mod.DoOpenInt(a);',
  10563. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10564. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10565. '};',
  10566. 'this.DoOpenInt = function (a) {',
  10567. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10568. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10569. ' $mod.DoInt(a);',
  10570. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10571. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10572. '};',
  10573. 'this.DoSet = function (a) {',
  10574. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10575. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10576. '};',
  10577. 'this.i = [];',
  10578. 'this.s = [];',
  10579. '']),
  10580. LinesToStr([ // $mod.$main
  10581. '$mod.DoInt([1]);',
  10582. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10583. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10584. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10585. '$mod.DoOpenInt([1]);',
  10586. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10587. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10588. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10589. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10590. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10591. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10592. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10593. '']));
  10594. end;
  10595. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10596. begin
  10597. StartProgram(false);
  10598. Add([
  10599. '{$modeswitch arrayoperators}',
  10600. 'type',
  10601. ' integer = longint;',
  10602. ' TArrInt = array of integer;',
  10603. ' TArrArrInt = array of TArrInt;',
  10604. 'procedure DoInt(const a: TArrArrInt);',
  10605. 'begin',
  10606. ' DoInt(a+[[1]]);',
  10607. ' DoInt([[1]]+a);',
  10608. ' DoInt(a);',
  10609. 'end;',
  10610. 'var',
  10611. ' i: TArrInt;',
  10612. ' a: TArrArrInt;',
  10613. 'begin',
  10614. ' a:=[[1]];',
  10615. ' a:=[i];',
  10616. ' a:=a+[i];',
  10617. ' a:=[i]+a;',
  10618. ' a:=[[1]+i];',
  10619. ' a:=[[1]+[2]];',
  10620. ' a:=[i+[2]];',
  10621. ' DoInt([[1]]);',
  10622. ' DoInt([[1]+[2],[3,4],[5]]);',
  10623. ' DoInt([i+[1]]+a);',
  10624. ' DoInt([i]+a);',
  10625. '']);
  10626. ConvertProgram;
  10627. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10628. LinesToStr([ // statements
  10629. 'this.DoInt = function (a) {',
  10630. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10631. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10632. ' $mod.DoInt(a);',
  10633. '};',
  10634. 'this.i = [];',
  10635. 'this.a = [];',
  10636. '']),
  10637. LinesToStr([ // $mod.$main
  10638. '$mod.a = [[1]];',
  10639. '$mod.a = [$mod.i];',
  10640. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10641. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10642. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10643. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10644. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10645. '$mod.DoInt([[1]]);',
  10646. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10647. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10648. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10649. '']));
  10650. end;
  10651. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10652. begin
  10653. StartProgram(false);
  10654. Add([
  10655. '{$modeswitch arrayoperators}',
  10656. 'type',
  10657. ' integer = longint;',
  10658. ' TArrInt = array[1..2] of integer;',
  10659. ' TArrArrInt = array of TArrInt;',
  10660. 'procedure DoInt(const a: TArrArrInt);',
  10661. 'begin',
  10662. ' DoInt(a+[[1,2]]);',
  10663. ' DoInt([[1,2]]+a);',
  10664. ' DoInt(a);',
  10665. 'end;',
  10666. 'var',
  10667. ' i: TArrInt;',
  10668. ' a: TArrArrInt;',
  10669. 'begin',
  10670. ' a:=[[1,1]];',
  10671. ' a:=[i];',
  10672. ' a:=a+[i];',
  10673. ' a:=[i]+a;',
  10674. ' DoInt([[1,1]]);',
  10675. ' DoInt([[1,2],[3,4]]);',
  10676. '']);
  10677. ConvertProgram;
  10678. CheckSource('TestArray_ArrayLitStaticAsParam',
  10679. LinesToStr([ // statements
  10680. 'this.DoInt = function (a) {',
  10681. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10682. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10683. ' $mod.DoInt(a);',
  10684. '};',
  10685. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10686. 'this.a = [];',
  10687. '']),
  10688. LinesToStr([ // $mod.$main
  10689. '$mod.a = [[1, 1]];',
  10690. '$mod.a = [$mod.i.slice(0)];',
  10691. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10692. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10693. '$mod.DoInt([[1, 1]]);',
  10694. '$mod.DoInt([[1, 2], [3, 4]]);',
  10695. '']));
  10696. end;
  10697. procedure TTestModule.TestArray_ForInArrOfString;
  10698. begin
  10699. StartProgram(false);
  10700. Add([
  10701. 'type',
  10702. 'type',
  10703. ' TMonthNameArray = array [1..12] of string;',
  10704. ' TMonthNames = TMonthNameArray;',
  10705. ' TObject = class',
  10706. ' private',
  10707. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10708. ' public',
  10709. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10710. ' end;',
  10711. 'var',
  10712. ' f: TObject;',
  10713. ' Month: string;',
  10714. ' Names: array of string = (''a'',''foo'',''bar'');',
  10715. ' i: longint;',
  10716. 'begin',
  10717. ' for Month in f.LongMonthNames do ;',
  10718. ' for Month in Names do ;',
  10719. ' for i:=low(Names) to high(Names) do ;',
  10720. '']);
  10721. ConvertProgram;
  10722. CheckSource('TestArray_ForInArrOfString',
  10723. LinesToStr([ // statements
  10724. 'rtl.createClass(this, "TObject", null, function () {',
  10725. ' this.$init = function () {',
  10726. ' };',
  10727. ' this.$final = function () {',
  10728. ' };',
  10729. '});',
  10730. 'this.f = null;',
  10731. 'this.Month = "";',
  10732. 'this.Names = ["a", "foo", "bar"];',
  10733. 'this.i = 0;',
  10734. '']),
  10735. LinesToStr([ // $mod.$main
  10736. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10737. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10738. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10739. '']));
  10740. end;
  10741. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10742. begin
  10743. StartProgram(false);
  10744. Add([
  10745. '{$modeswitch externalclass}',
  10746. 'type',
  10747. ' TJSObject = class external name ''Object''',
  10748. ' end;',
  10749. ' TJSArray = class external name ''Array''',
  10750. ' class function isArray(Value: JSValue) : boolean;',
  10751. ' function concat() : TJSArray; varargs;',
  10752. ' end;',
  10753. 'var',
  10754. ' aObj: TJSArray;',
  10755. ' a: array of longint;',
  10756. ' o: TJSObject;',
  10757. 'begin',
  10758. ' if TJSArray.isArray(65) then ;',
  10759. ' aObj:=TJSArray(a).concat(a);',
  10760. ' o:=TJSObject(a);']);
  10761. ConvertProgram;
  10762. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10763. LinesToStr([ // statements
  10764. 'this.aObj = null;',
  10765. 'this.a = [];',
  10766. 'this.o = null;',
  10767. '']),
  10768. LinesToStr([ // $mod.$main
  10769. 'if (Array.isArray(65)) ;',
  10770. '$mod.aObj = $mod.a.concat($mod.a);',
  10771. '$mod.o = $mod.a;',
  10772. '']));
  10773. end;
  10774. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10775. begin
  10776. StartProgram(false);
  10777. Add([
  10778. '{$modeswitch externalclass}',
  10779. 'type',
  10780. ' TArrStr = array of string;',
  10781. ' TJSArray = class external name ''Array''',
  10782. ' end;',
  10783. ' TJSObject = class external name ''Object''',
  10784. ' end;',
  10785. 'var',
  10786. ' aObj: TJSArray;',
  10787. ' a: TArrStr;',
  10788. ' jo: TJSObject;',
  10789. 'begin',
  10790. ' a:=TArrStr(aObj);',
  10791. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10792. ' a:=TarrStr(jo);',
  10793. '']);
  10794. ConvertProgram;
  10795. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10796. LinesToStr([ // statements
  10797. 'this.aObj = null;',
  10798. 'this.a = [];',
  10799. 'this.jo = null;',
  10800. '']),
  10801. LinesToStr([ // $mod.$main
  10802. '$mod.a = $mod.aObj;',
  10803. '$mod.aObj[1] = $mod.aObj[2];',
  10804. '$mod.a = $mod.jo;',
  10805. '']));
  10806. end;
  10807. procedure TTestModule.TestArrayOfConst_TVarRec;
  10808. begin
  10809. StartProgram(true,[supTVarRec]);
  10810. Add([
  10811. 'procedure Say(args: array of const);',
  10812. 'var',
  10813. ' i: longint;',
  10814. ' v: TVarRec;',
  10815. 'begin',
  10816. ' for i:=low(args) to high(args) do begin',
  10817. ' v:=args[i];',
  10818. ' case v.vtype of',
  10819. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10820. ' end;',
  10821. ' end;',
  10822. ' for v in args do ;',
  10823. ' args:=nil;',
  10824. ' SetLength(args,2);',
  10825. 'end;',
  10826. 'begin']);
  10827. ConvertProgram;
  10828. CheckSource('TestArrayOfConst_TVarRec',
  10829. LinesToStr([ // statements
  10830. 'this.Say = function (args) {',
  10831. ' var i = 0;',
  10832. ' var v = pas.system.TVarRec.$new();',
  10833. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10834. ' i = $l;',
  10835. ' v.$assign(args[i]);',
  10836. ' var $tmp = v.VType;',
  10837. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10838. ' };',
  10839. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10840. ' args = [];',
  10841. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10842. '};',
  10843. '']),
  10844. LinesToStr([ // $mod.$main
  10845. ]));
  10846. end;
  10847. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10848. begin
  10849. StartProgram(true,[supTVarRec]);
  10850. Add([
  10851. 'procedure Say(args: array of const);',
  10852. 'begin',
  10853. ' Say(args);',
  10854. 'end;',
  10855. 'var',
  10856. ' p: Pointer;',
  10857. ' j: jsvalue;',
  10858. ' c: currency;',
  10859. 'begin',
  10860. ' Say([]);',
  10861. ' Say([1]);',
  10862. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10863. '']);
  10864. ConvertProgram;
  10865. CheckSource('TestArrayOfConst_PassBaseTypes',
  10866. LinesToStr([ // statements
  10867. 'this.Say = function (args) {',
  10868. ' $mod.Say(args);',
  10869. '};',
  10870. 'this.p = null;',
  10871. 'this.j = undefined;',
  10872. 'this.c = 0;',
  10873. '']),
  10874. LinesToStr([ // $mod.$main
  10875. '$mod.Say([]);',
  10876. '$mod.Say(pas.system.VarRecs(0, 1));',
  10877. '$mod.Say(pas.system.VarRecs(',
  10878. ' 9,',
  10879. ' "c",',
  10880. ' 18,',
  10881. ' "foo",',
  10882. ' 5,',
  10883. ' null,',
  10884. ' 1,',
  10885. ' true,',
  10886. ' 3,',
  10887. ' 1.3,',
  10888. ' 5,',
  10889. ' $mod.p,',
  10890. ' 20,',
  10891. ' $mod.j,',
  10892. ' 12,',
  10893. ' $mod.c',
  10894. ' ));',
  10895. '']));
  10896. end;
  10897. procedure TTestModule.TestArrayOfConst_PassObj;
  10898. begin
  10899. StartProgram(true,[supTVarRec]);
  10900. Add([
  10901. '{$interfaces corba}',
  10902. 'type',
  10903. ' TObject = class',
  10904. ' end;',
  10905. ' TClass = class of TObject;',
  10906. ' IUnknown = interface',
  10907. ' end;',
  10908. 'procedure Say(args: array of const);',
  10909. 'begin',
  10910. 'end;',
  10911. 'var',
  10912. ' o: TObject;',
  10913. ' c: TClass;',
  10914. ' i: IUnknown;',
  10915. 'begin',
  10916. ' Say([o,c,TObject]);',
  10917. ' Say([nil,i]);',
  10918. '']);
  10919. ConvertProgram;
  10920. CheckSource('TestArrayOfConst_PassObj',
  10921. LinesToStr([ // statements
  10922. 'rtl.createClass(this, "TObject", null, function () {',
  10923. ' this.$init = function () {',
  10924. ' };',
  10925. ' this.$final = function () {',
  10926. ' };',
  10927. '});',
  10928. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10929. 'this.Say = function (args) {',
  10930. '};',
  10931. 'this.o = null;',
  10932. 'this.c = null;',
  10933. 'this.i = null;',
  10934. '']),
  10935. LinesToStr([ // $mod.$main
  10936. '$mod.Say(pas.system.VarRecs(',
  10937. ' 7,',
  10938. ' $mod.o,',
  10939. ' 8,',
  10940. ' $mod.c,',
  10941. ' 8,',
  10942. ' $mod.TObject',
  10943. '));',
  10944. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10945. '']));
  10946. end;
  10947. procedure TTestModule.TestRecord_Empty;
  10948. begin
  10949. StartProgram(false);
  10950. Add([
  10951. 'type',
  10952. ' TRecA = record',
  10953. ' end;',
  10954. 'var a,b: TRecA;',
  10955. 'begin',
  10956. ' if a=b then ;']);
  10957. ConvertProgram;
  10958. CheckSource('TestRecord_Empty',
  10959. LinesToStr([ // statements
  10960. 'rtl.recNewT(this, "TRecA", function () {',
  10961. ' this.$eq = function (b) {',
  10962. ' return true;',
  10963. ' };',
  10964. ' this.$assign = function (s) {',
  10965. ' return this;',
  10966. ' };',
  10967. '});',
  10968. 'this.a = this.TRecA.$new();',
  10969. 'this.b = this.TRecA.$new();',
  10970. '']),
  10971. LinesToStr([ // $mod.$main
  10972. 'if ($mod.a.$eq($mod.b)) ;'
  10973. ]));
  10974. end;
  10975. procedure TTestModule.TestRecord_Var;
  10976. begin
  10977. StartProgram(false);
  10978. Add('type');
  10979. Add(' TRecA = record');
  10980. Add(' Bold: longint;');
  10981. Add(' end;');
  10982. Add('var Rec: TRecA;');
  10983. Add('begin');
  10984. Add(' rec.bold:=123');
  10985. ConvertProgram;
  10986. CheckSource('TestRecord_Var',
  10987. LinesToStr([ // statements
  10988. 'rtl.recNewT(this, "TRecA", function () {',
  10989. ' this.Bold = 0;',
  10990. ' this.$eq = function (b) {',
  10991. ' return this.Bold === b.Bold;',
  10992. ' };',
  10993. ' this.$assign = function (s) {',
  10994. ' this.Bold = s.Bold;',
  10995. ' return this;',
  10996. ' };',
  10997. '});',
  10998. 'this.Rec = this.TRecA.$new();',
  10999. '']),
  11000. LinesToStr([ // $mod.$main
  11001. '$mod.Rec.Bold = 123;'
  11002. ]));
  11003. end;
  11004. procedure TTestModule.TestRecord_VarExternal;
  11005. begin
  11006. StartProgram(false);
  11007. Add([
  11008. '{$modeswitch externalclass}',
  11009. 'type',
  11010. ' TRecA = record',
  11011. ' i: byte;',
  11012. ' length_: longint external name ''length'';',
  11013. ' end;',
  11014. 'var Rec: TRecA;',
  11015. 'begin',
  11016. ' rec.length_ := rec.length_',
  11017. '']);
  11018. ConvertProgram;
  11019. CheckSource('TestRecord_VarExternal',
  11020. LinesToStr([ // statements
  11021. 'rtl.recNewT(this, "TRecA", function () {',
  11022. ' this.i = 0;',
  11023. ' this.$eq = function (b) {',
  11024. ' return (this.i === b.i) && (this.length === b.length);',
  11025. ' };',
  11026. ' this.$assign = function (s) {',
  11027. ' this.i = s.i;',
  11028. ' this.length = s.length;',
  11029. ' return this;',
  11030. ' };',
  11031. '});',
  11032. 'this.Rec = this.TRecA.$new();',
  11033. '']),
  11034. LinesToStr([ // $mod.$main
  11035. '$mod.Rec.length = $mod.Rec.length;'
  11036. ]));
  11037. end;
  11038. procedure TTestModule.TestRecord_WithDo;
  11039. begin
  11040. StartProgram(false);
  11041. Add('type');
  11042. Add(' TRec = record');
  11043. Add(' vI: longint;');
  11044. Add(' end;');
  11045. Add('var');
  11046. Add(' Int: longint;');
  11047. Add(' r: TRec;');
  11048. Add('begin');
  11049. Add(' with r do');
  11050. Add(' int:=vi;');
  11051. Add(' with r do begin');
  11052. Add(' int:=vi;');
  11053. Add(' vi:=int;');
  11054. Add(' end;');
  11055. ConvertProgram;
  11056. CheckSource('TestWithRecordDo',
  11057. LinesToStr([ // statements
  11058. 'rtl.recNewT(this, "TRec", function () {',
  11059. ' this.vI = 0;',
  11060. ' this.$eq = function (b) {',
  11061. ' return this.vI === b.vI;',
  11062. ' };',
  11063. ' this.$assign = function (s) {',
  11064. ' this.vI = s.vI;',
  11065. ' return this;',
  11066. ' };',
  11067. '});',
  11068. 'this.Int = 0;',
  11069. 'this.r = this.TRec.$new();',
  11070. '']),
  11071. LinesToStr([ // $mod.$main
  11072. 'var $with = $mod.r;',
  11073. '$mod.Int = $with.vI;',
  11074. 'var $with1 = $mod.r;',
  11075. '$mod.Int = $with1.vI;',
  11076. '$with1.vI = $mod.Int;'
  11077. ]));
  11078. end;
  11079. procedure TTestModule.TestRecord_Assign;
  11080. begin
  11081. StartProgram(false);
  11082. Add([
  11083. 'type',
  11084. ' TEnum = (red,green);',
  11085. ' TEnums = set of TEnum;',
  11086. ' TSmallRec = record',
  11087. ' N: longint;',
  11088. ' end;',
  11089. ' TBigRec = record',
  11090. ' Int: longint;',
  11091. ' D: double;',
  11092. ' Arr: array of longint;',
  11093. ' Arr2: array[1..2] of longint;',
  11094. ' Small: TSmallRec;',
  11095. ' Enums: TEnums;',
  11096. ' end;',
  11097. 'var',
  11098. ' r, s: TBigRec;',
  11099. 'begin',
  11100. ' r:=s;',
  11101. ' r:=default(TBigRec);',
  11102. ' r:=default(s);',
  11103. '']);
  11104. ConvertProgram;
  11105. CheckSource('TestRecord_Assign',
  11106. LinesToStr([ // statements
  11107. 'this.TEnum = {',
  11108. ' "0": "red",',
  11109. ' red: 0,',
  11110. ' "1": "green",',
  11111. ' green: 1',
  11112. '};',
  11113. 'rtl.recNewT(this, "TSmallRec", function () {',
  11114. ' this.N = 0;',
  11115. ' this.$eq = function (b) {',
  11116. ' return this.N === b.N;',
  11117. ' };',
  11118. ' this.$assign = function (s) {',
  11119. ' this.N = s.N;',
  11120. ' return this;',
  11121. ' };',
  11122. '});',
  11123. 'rtl.recNewT(this, "TBigRec", function () {',
  11124. ' this.Int = 0;',
  11125. ' this.D = 0.0;',
  11126. ' this.$new = function () {',
  11127. ' var r = Object.create(this);',
  11128. ' r.Arr = [];',
  11129. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  11130. ' r.Small = $mod.TSmallRec.$new();',
  11131. ' r.Enums = {};',
  11132. ' return r;',
  11133. ' };',
  11134. ' this.$eq = function (b) {',
  11135. ' 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);',
  11136. ' };',
  11137. ' this.$assign = function (s) {',
  11138. ' this.Int = s.Int;',
  11139. ' this.D = s.D;',
  11140. ' this.Arr = rtl.arrayRef(s.Arr);',
  11141. ' this.Arr2 = s.Arr2.slice(0);',
  11142. ' this.Small.$assign(s.Small);',
  11143. ' this.Enums = rtl.refSet(s.Enums);',
  11144. ' return this;',
  11145. ' };',
  11146. '});',
  11147. 'this.r = this.TBigRec.$new();',
  11148. 'this.s = this.TBigRec.$new();',
  11149. '']),
  11150. LinesToStr([ // $mod.$main
  11151. '$mod.r.$assign($mod.s);',
  11152. '$mod.r.$assign($mod.TBigRec.$new());',
  11153. '$mod.r.$assign($mod.TBigRec.$new());',
  11154. '']));
  11155. end;
  11156. procedure TTestModule.TestRecord_AsParams;
  11157. begin
  11158. StartProgram(false);
  11159. Add([
  11160. 'type',
  11161. ' integer = longint;',
  11162. ' TRecord = record',
  11163. ' i: integer;',
  11164. ' end;',
  11165. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  11166. 'var vL: TRecord;',
  11167. 'begin',
  11168. ' vd:=vd;',
  11169. ' vd.i:=vd.i;',
  11170. ' vl:=vc;',
  11171. ' vv:=vv;',
  11172. ' vv.i:=vv.i;',
  11173. ' U:=vl;',
  11174. ' U:=vd;',
  11175. ' U:=vc;',
  11176. ' U:=vv;',
  11177. ' vl:=TRecord(U);',
  11178. ' vd:=TRecord(U);',
  11179. ' vv:=TRecord(U);',
  11180. ' doit(vd,vd,vd,vd);',
  11181. ' doit(vc,vc,vl,vl);',
  11182. ' doit(vv,vv,vv,vv);',
  11183. ' doit(vl,vl,vl,vl);',
  11184. ' TRecord(U).i:=3;',
  11185. 'end;',
  11186. 'var i: TRecord;',
  11187. 'begin',
  11188. ' doit(i,i,i,i);',
  11189. '']);
  11190. ConvertProgram;
  11191. CheckSource('TestRecord_AsParams',
  11192. LinesToStr([ // statements
  11193. 'rtl.recNewT(this, "TRecord", function () {',
  11194. ' this.i = 0;',
  11195. ' this.$eq = function (b) {',
  11196. ' return this.i === b.i;',
  11197. ' };',
  11198. ' this.$assign = function (s) {',
  11199. ' this.i = s.i;',
  11200. ' return this;',
  11201. ' };',
  11202. '});',
  11203. 'this.DoIt = function (vD, vC, vV, U) {',
  11204. ' var vL = $mod.TRecord.$new();',
  11205. ' vD.$assign(vD);',
  11206. ' vD.i = vD.i;',
  11207. ' vL.$assign(vC);',
  11208. ' vV.$assign(vV);',
  11209. ' vV.i = vV.i;',
  11210. ' U.$assign(vL);',
  11211. ' U.$assign(vD);',
  11212. ' U.$assign(vC);',
  11213. ' U.$assign(vV);',
  11214. ' vL.$assign(U);',
  11215. ' vD.$assign(U);',
  11216. ' vV.$assign(U);',
  11217. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  11218. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  11219. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  11220. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  11221. ' U.i = 3;',
  11222. '};',
  11223. 'this.i = this.TRecord.$new();'
  11224. ]),
  11225. LinesToStr([
  11226. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11227. '']));
  11228. end;
  11229. procedure TTestModule.TestRecord_ConstRef;
  11230. begin
  11231. StartProgram(false);
  11232. Add([
  11233. 'type TRec = record i: word; end;',
  11234. 'procedure Run(constref a: TRec);',
  11235. 'begin',
  11236. 'end;',
  11237. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11238. 'var l: TRec;',
  11239. 'begin',
  11240. ' Run(l);',
  11241. ' Run(a);',
  11242. ' Run(b);',
  11243. ' Run(c);',
  11244. ' Run(d);',
  11245. ' Run(e);',
  11246. 'end;',
  11247. 'begin',
  11248. '']);
  11249. ConvertProgram;
  11250. CheckResolverUnexpectedHints();
  11251. CheckSource('TestRecord_ConstRef',
  11252. LinesToStr([ // statements
  11253. 'rtl.recNewT(this, "TRec", function () {',
  11254. ' this.i = 0;',
  11255. ' this.$eq = function (b) {',
  11256. ' return this.i === b.i;',
  11257. ' };',
  11258. ' this.$assign = function (s) {',
  11259. ' this.i = s.i;',
  11260. ' return this;',
  11261. ' };',
  11262. '});',
  11263. 'this.Run = function (a) {',
  11264. '};',
  11265. 'this.Fly = function (a, b, c, d, e) {',
  11266. ' var l = $mod.TRec.$new();',
  11267. ' $mod.Run(l);',
  11268. ' $mod.Run(a);',
  11269. ' $mod.Run(b);',
  11270. ' $mod.Run(c);',
  11271. ' $mod.Run(d);',
  11272. ' $mod.Run(e);',
  11273. '};',
  11274. '']),
  11275. LinesToStr([
  11276. '']));
  11277. end;
  11278. procedure TTestModule.TestRecordElement_AsParams;
  11279. begin
  11280. StartProgram(false);
  11281. Add('type');
  11282. Add(' integer = longint;');
  11283. Add(' TRecord = record');
  11284. Add(' i: integer;');
  11285. Add(' end;');
  11286. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11287. Add('var vJ: TRecord;');
  11288. Add('begin');
  11289. Add(' doit(vj.i,vj.i,vj.i);');
  11290. Add('end;');
  11291. Add('var r: TRecord;');
  11292. Add('begin');
  11293. Add(' doit(r.i,r.i,r.i);');
  11294. ConvertProgram;
  11295. CheckSource('TestRecordElement_AsParams',
  11296. LinesToStr([ // statements
  11297. 'rtl.recNewT(this, "TRecord", function () {',
  11298. ' this.i = 0;',
  11299. ' this.$eq = function (b) {',
  11300. ' return this.i === b.i;',
  11301. ' };',
  11302. ' this.$assign = function (s) {',
  11303. ' this.i = s.i;',
  11304. ' return this;',
  11305. ' };',
  11306. '});',
  11307. 'this.DoIt = function (vG,vH,vI) {',
  11308. ' var vJ = $mod.TRecord.$new();',
  11309. ' $mod.DoIt(vJ.i, vJ.i, {',
  11310. ' p: vJ,',
  11311. ' get: function () {',
  11312. ' return this.p.i;',
  11313. ' },',
  11314. ' set: function (v) {',
  11315. ' this.p.i = v;',
  11316. ' }',
  11317. ' });',
  11318. '};',
  11319. 'this.r = this.TRecord.$new();'
  11320. ]),
  11321. LinesToStr([
  11322. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11323. ' p: $mod.r,',
  11324. ' get: function () {',
  11325. ' return this.p.i;',
  11326. ' },',
  11327. ' set: function (v) {',
  11328. ' this.p.i = v;',
  11329. ' }',
  11330. '});'
  11331. ]));
  11332. end;
  11333. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11334. begin
  11335. StartProgram(false);
  11336. Add('type');
  11337. Add(' integer = longint;');
  11338. Add(' TRecord = record');
  11339. Add(' i: integer;');
  11340. Add(' end;');
  11341. Add('function GetRec(vB: integer = 0): TRecord;');
  11342. Add('begin');
  11343. Add('end;');
  11344. Add('procedure DoIt(vG: integer; const vH: integer);');
  11345. Add('begin');
  11346. Add('end;');
  11347. Add('begin');
  11348. Add(' doit(getrec.i,getrec.i);');
  11349. Add(' doit(getrec().i,getrec().i);');
  11350. Add(' doit(getrec(1).i,getrec(2).i);');
  11351. ConvertProgram;
  11352. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11353. LinesToStr([ // statements
  11354. 'rtl.recNewT(this, "TRecord", function () {',
  11355. ' this.i = 0;',
  11356. ' this.$eq = function (b) {',
  11357. ' return this.i === b.i;',
  11358. ' };',
  11359. ' this.$assign = function (s) {',
  11360. ' this.i = s.i;',
  11361. ' return this;',
  11362. ' };',
  11363. '});',
  11364. 'this.GetRec = function (vB) {',
  11365. ' var Result = $mod.TRecord.$new();',
  11366. ' return Result;',
  11367. '};',
  11368. 'this.DoIt = function (vG, vH) {',
  11369. '};',
  11370. '']),
  11371. LinesToStr([
  11372. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11373. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11374. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11375. '']));
  11376. end;
  11377. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11378. begin
  11379. StartProgram(false);
  11380. Add('type');
  11381. Add(' integer = longint;');
  11382. Add(' TRecord = record');
  11383. Add(' i: integer;');
  11384. Add(' end;');
  11385. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11386. Add('begin');
  11387. Add('end;');
  11388. Add('var r: trecord;');
  11389. Add('begin');
  11390. Add(' with r do ');
  11391. Add(' doit(i,i,i);');
  11392. ConvertProgram;
  11393. CheckSource('TestRecordElementFromWith_AsParams',
  11394. LinesToStr([ // statements
  11395. 'rtl.recNewT(this, "TRecord", function () {',
  11396. ' this.i = 0;',
  11397. ' this.$eq = function (b) {',
  11398. ' return this.i === b.i;',
  11399. ' };',
  11400. ' this.$assign = function (s) {',
  11401. ' this.i = s.i;',
  11402. ' return this;',
  11403. ' };',
  11404. '});',
  11405. 'this.DoIt = function (vG,vH,vI) {',
  11406. '};',
  11407. 'this.r = this.TRecord.$new();'
  11408. ]),
  11409. LinesToStr([
  11410. 'var $with = $mod.r;',
  11411. '$mod.DoIt($with.i,$with.i,{',
  11412. ' p: $with,',
  11413. ' get: function () {',
  11414. ' return this.p.i;',
  11415. ' },',
  11416. ' set: function (v) {',
  11417. ' this.p.i = v;',
  11418. ' }',
  11419. '});',
  11420. '']));
  11421. end;
  11422. procedure TTestModule.TestRecord_Equal;
  11423. begin
  11424. StartProgram(false);
  11425. Add('type');
  11426. Add(' integer = longint;');
  11427. Add(' TFlag = (red,blue);');
  11428. Add(' TFlags = set of TFlag;');
  11429. Add(' TProc = procedure;');
  11430. Add(' TRecord = record');
  11431. Add(' i: integer;');
  11432. Add(' Event: TProc;');
  11433. Add(' f: TFlags;');
  11434. Add(' end;');
  11435. Add(' TNested = record');
  11436. Add(' r: TRecord;');
  11437. Add(' end;');
  11438. Add('var');
  11439. Add(' b: boolean;');
  11440. Add(' r,s: trecord;');
  11441. Add('begin');
  11442. Add(' b:=r=s;');
  11443. Add(' b:=r<>s;');
  11444. ConvertProgram;
  11445. CheckSource('TestRecord_Equal',
  11446. LinesToStr([ // statements
  11447. 'this.TFlag = {',
  11448. ' "0": "red",',
  11449. ' red: 0,',
  11450. ' "1": "blue",',
  11451. ' blue: 1',
  11452. '};',
  11453. 'rtl.recNewT(this, "TRecord", function () {',
  11454. ' this.i = 0;',
  11455. ' this.Event = null;',
  11456. ' this.$new = function () {',
  11457. ' var r = Object.create(this);',
  11458. ' r.f = {};',
  11459. ' return r;',
  11460. ' };',
  11461. ' this.$eq = function (b) {',
  11462. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11463. ' };',
  11464. ' this.$assign = function (s) {',
  11465. ' this.i = s.i;',
  11466. ' this.Event = s.Event;',
  11467. ' this.f = rtl.refSet(s.f);',
  11468. ' return this;',
  11469. ' };',
  11470. '});',
  11471. 'rtl.recNewT(this, "TNested", function () {',
  11472. ' this.$new = function () {',
  11473. ' var r = Object.create(this);',
  11474. ' r.r = $mod.TRecord.$new();',
  11475. ' return r;',
  11476. ' };',
  11477. ' this.$eq = function (b) {',
  11478. ' return this.r.$eq(b.r);',
  11479. ' };',
  11480. ' this.$assign = function (s) {',
  11481. ' this.r.$assign(s.r);',
  11482. ' return this;',
  11483. ' };',
  11484. '});',
  11485. 'this.b = false;',
  11486. 'this.r = this.TRecord.$new();',
  11487. 'this.s = this.TRecord.$new();',
  11488. '']),
  11489. LinesToStr([
  11490. '$mod.b = $mod.r.$eq($mod.s);',
  11491. '$mod.b = !$mod.r.$eq($mod.s);',
  11492. '']));
  11493. end;
  11494. procedure TTestModule.TestRecord_JSValue;
  11495. begin
  11496. StartProgram(false);
  11497. Add([
  11498. 'type',
  11499. ' TRecord = record',
  11500. ' i: longint;',
  11501. ' end;',
  11502. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11503. 'begin',
  11504. 'end;',
  11505. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11506. 'begin',
  11507. ' if jsvalue(d) then ;',
  11508. ' if jsvalue(c) then ;',
  11509. ' if jsvalue(v) then ;',
  11510. 'end;',
  11511. 'var',
  11512. ' Jv: jsvalue;',
  11513. ' Rec: trecord;',
  11514. 'begin',
  11515. ' rec:=trecord(jv);',
  11516. ' jv:=rec;',
  11517. ' Fly(rec,rec);',
  11518. ' Fly(@rec,@rec);',
  11519. ' if jsvalue(Rec) then ;',
  11520. ' Run(trecord(jv),trecord(jv),rec);',
  11521. '']);
  11522. ConvertProgram;
  11523. CheckSource('TestRecord_JSValue',
  11524. LinesToStr([ // statements
  11525. 'rtl.recNewT(this, "TRecord", function () {',
  11526. ' this.i = 0;',
  11527. ' this.$eq = function (b) {',
  11528. ' return this.i === b.i;',
  11529. ' };',
  11530. ' this.$assign = function (s) {',
  11531. ' this.i = s.i;',
  11532. ' return this;',
  11533. ' };',
  11534. '});',
  11535. 'this.Fly = function (d, c) {',
  11536. '};',
  11537. 'this.Run = function (d, c, v) {',
  11538. ' if (d) ;',
  11539. ' if (c) ;',
  11540. ' if (v) ;',
  11541. '};',
  11542. 'this.Jv = undefined;',
  11543. 'this.Rec = this.TRecord.$new();',
  11544. '']),
  11545. LinesToStr([
  11546. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11547. '$mod.Jv = $mod.Rec;',
  11548. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11549. '$mod.Fly($mod.Rec, $mod.Rec);',
  11550. 'if ($mod.Rec) ;',
  11551. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11552. '']));
  11553. end;
  11554. procedure TTestModule.TestRecord_VariantFail;
  11555. begin
  11556. StartProgram(false);
  11557. Add([
  11558. 'type',
  11559. ' TRec = record',
  11560. ' case word of',
  11561. ' 0: (b0, b1: Byte);',
  11562. ' 1: (i: word);',
  11563. ' end;',
  11564. 'begin']);
  11565. SetExpectedPasResolverError('variant record is not supported',
  11566. nXIsNotSupported);
  11567. ConvertProgram;
  11568. end;
  11569. procedure TTestModule.TestRecord_FieldArray;
  11570. begin
  11571. StartProgram(false);
  11572. Add([
  11573. 'type',
  11574. ' TArrInt = array[3..4] of longint;',
  11575. ' TArrArrInt = array[3..4] of longint;',
  11576. ' TRec = record',
  11577. ' a: array of longint;',
  11578. ' s: array[1..2] of longint;',
  11579. ' m: array[1..2,3..4] of longint;',
  11580. ' o: TArrArrInt;',
  11581. ' end;',
  11582. 'begin']);
  11583. ConvertProgram;
  11584. CheckSource('TestRecord_FieldArray',
  11585. LinesToStr([ // statements
  11586. 'rtl.recNewT(this, "TRec", function () {',
  11587. ' this.m$a$clone = function (a) {',
  11588. ' var b = [];',
  11589. ' b.length = 2;',
  11590. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  11591. ' return b;',
  11592. ' };',
  11593. ' this.$new = function () {',
  11594. ' var r = Object.create(this);',
  11595. ' r.a = [];',
  11596. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11597. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11598. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11599. ' return r;',
  11600. ' };',
  11601. ' this.$eq = function (b) {',
  11602. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11603. ' };',
  11604. ' this.$assign = function (s) {',
  11605. ' this.a = rtl.arrayRef(s.a);',
  11606. ' this.s = s.s.slice(0);',
  11607. ' this.m = this.m$a$clone(s.m);',
  11608. ' this.o = s.o.slice(0);',
  11609. ' return this;',
  11610. ' };',
  11611. '});',
  11612. '']),
  11613. LinesToStr([ // $mod.$main
  11614. '']));
  11615. end;
  11616. procedure TTestModule.TestRecord_Const;
  11617. begin
  11618. StartProgram(false);
  11619. Add([
  11620. 'type',
  11621. ' TArrInt = array[3..4] of longint;',
  11622. ' TPoint = record x,y: longint; end;',
  11623. ' TRec = record',
  11624. ' i: longint;',
  11625. ' a: array of longint;',
  11626. ' s: array[1..2] of longint;',
  11627. ' m: array[1..2,3..4] of longint;',
  11628. ' p: TPoint;',
  11629. ' end;',
  11630. ' TPoints = array of TPoint;',
  11631. 'const',
  11632. ' r: TRec = (',
  11633. ' i:1;',
  11634. ' a:(2,3);',
  11635. ' s:(4,5);',
  11636. ' m:( (11,12), (13,14) );',
  11637. ' p: (x:21; y:22)',
  11638. ' );',
  11639. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11640. 'begin']);
  11641. ConvertProgram;
  11642. CheckSource('TestRecord_Const',
  11643. LinesToStr([ // statements
  11644. 'rtl.recNewT(this, "TPoint", function () {',
  11645. ' this.x = 0;',
  11646. ' this.y = 0;',
  11647. ' this.$eq = function (b) {',
  11648. ' return (this.x === b.x) && (this.y === b.y);',
  11649. ' };',
  11650. ' this.$assign = function (s) {',
  11651. ' this.x = s.x;',
  11652. ' this.y = s.y;',
  11653. ' return this;',
  11654. ' };',
  11655. '});',
  11656. 'rtl.recNewT(this, "TRec", function () {',
  11657. ' this.i = 0;',
  11658. ' this.m$a$clone = function (a) {',
  11659. ' var b = [];',
  11660. ' b.length = 2;',
  11661. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  11662. ' return b;',
  11663. ' };',
  11664. ' this.$new = function () {',
  11665. ' var r = Object.create(this);',
  11666. ' r.a = [];',
  11667. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11668. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11669. ' r.p = $mod.TPoint.$new();',
  11670. ' return r;',
  11671. ' };',
  11672. ' this.$eq = function (b) {',
  11673. ' 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);',
  11674. ' };',
  11675. ' this.$assign = function (s) {',
  11676. ' this.i = s.i;',
  11677. ' this.a = rtl.arrayRef(s.a);',
  11678. ' this.s = s.s.slice(0);',
  11679. ' this.m = this.m$a$clone(s.m);',
  11680. ' this.p.$assign(s.p);',
  11681. ' return this;',
  11682. ' };',
  11683. '});',
  11684. 'this.r = this.TRec.$clone({',
  11685. ' i: 1,',
  11686. ' a: [2, 3],',
  11687. ' s: [4, 5],',
  11688. ' m: [[11, 12], [13, 14]],',
  11689. ' p: this.TPoint.$clone({',
  11690. ' x: 21,',
  11691. ' y: 22',
  11692. ' })',
  11693. '});',
  11694. 'this.p = [this.TPoint.$clone({',
  11695. ' x: 1,',
  11696. ' y: 2',
  11697. '}), this.TPoint.$clone({',
  11698. ' x: 3,',
  11699. ' y: 4',
  11700. '})];',
  11701. '']),
  11702. LinesToStr([ // $mod.$main
  11703. '']));
  11704. end;
  11705. procedure TTestModule.TestRecord_TypecastFail;
  11706. begin
  11707. StartProgram(false);
  11708. Add([
  11709. 'type',
  11710. ' TPoint = record x,y: longint; end;',
  11711. ' TRec = record l: longint end;',
  11712. 'var p: TPoint;',
  11713. 'begin',
  11714. ' if TRec(p).l=2 then ;']);
  11715. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11716. nIllegalTypeConversionTo);
  11717. ConvertProgram;
  11718. end;
  11719. procedure TTestModule.TestRecord_InFunction;
  11720. begin
  11721. StartProgram(false);
  11722. Add([
  11723. 'var TPoint: longint = 3;',
  11724. 'procedure DoIt;',
  11725. 'type',
  11726. ' TPoint = record x,y: longint; end;',
  11727. ' TPoints = array of TPoint;',
  11728. 'var',
  11729. ' r: TPoint;',
  11730. ' p: TPoints;',
  11731. 'begin',
  11732. ' SetLength(p,2);',
  11733. 'end;',
  11734. 'begin']);
  11735. ConvertProgram;
  11736. CheckSource('TestRecord_InFunction',
  11737. LinesToStr([ // statements
  11738. 'this.TPoint = 3;',
  11739. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11740. ' this.x = 0;',
  11741. ' this.y = 0;',
  11742. ' this.$eq = function (b) {',
  11743. ' return (this.x === b.x) && (this.y === b.y);',
  11744. ' };',
  11745. ' this.$assign = function (s) {',
  11746. ' this.x = s.x;',
  11747. ' this.y = s.y;',
  11748. ' return this;',
  11749. ' };',
  11750. '});',
  11751. 'this.DoIt = function () {',
  11752. ' var r = TPoint$1.$new();',
  11753. ' var p = [];',
  11754. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11755. '};',
  11756. '']),
  11757. LinesToStr([ // $mod.$main
  11758. '']));
  11759. end;
  11760. procedure TTestModule.TestRecord_AnonymousFail;
  11761. begin
  11762. StartProgram(false);
  11763. Add([
  11764. 'var',
  11765. ' r: record x: word end;',
  11766. 'begin']);
  11767. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11768. nNotYetImplemented);
  11769. ConvertProgram;
  11770. end;
  11771. procedure TTestModule.TestAdvRecord_Function;
  11772. begin
  11773. StartProgram(false);
  11774. Parser.Options:=Parser.Options+[po_cassignments];
  11775. Add([
  11776. '{$modeswitch AdvancedRecords}',
  11777. 'type',
  11778. ' TPoint = record',
  11779. ' x,y: word;',
  11780. ' function Add(const apt: TPoint): TPoint;',
  11781. ' end;',
  11782. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11783. 'begin',
  11784. ' Result:=Self;',
  11785. ' Result.x+=apt.x;',
  11786. ' Result.y:=Result.y+apt.y;',
  11787. ' Self:=apt;',
  11788. 'end;',
  11789. 'var p,q: TPoint;',
  11790. 'begin',
  11791. ' p.add(q);',
  11792. ' p:=default(TPoint);',
  11793. ' p:=q;',
  11794. '']);
  11795. ConvertProgram;
  11796. CheckSource('TestAdvRecord_Function',
  11797. LinesToStr([ // statements
  11798. 'rtl.recNewT(this, "TPoint", function () {',
  11799. ' this.x = 0;',
  11800. ' this.y = 0;',
  11801. ' this.$eq = function (b) {',
  11802. ' return (this.x === b.x) && (this.y === b.y);',
  11803. ' };',
  11804. ' this.$assign = function (s) {',
  11805. ' this.x = s.x;',
  11806. ' this.y = s.y;',
  11807. ' return this;',
  11808. ' };',
  11809. ' this.Add = function (apt) {',
  11810. ' var Result = $mod.TPoint.$new();',
  11811. ' Result.$assign(this);',
  11812. ' Result.x += apt.x;',
  11813. ' Result.y = Result.y + apt.y;',
  11814. ' this.$assign(apt);',
  11815. ' return Result;',
  11816. ' };',
  11817. '});',
  11818. 'this.p = this.TPoint.$new();',
  11819. 'this.q = this.TPoint.$new();',
  11820. '']),
  11821. LinesToStr([ // $mod.$main
  11822. '$mod.p.Add($mod.q);',
  11823. '$mod.p.$assign($mod.TPoint.$new());',
  11824. '$mod.p.$assign($mod.q);',
  11825. '']));
  11826. end;
  11827. procedure TTestModule.TestAdvRecord_Property;
  11828. begin
  11829. StartProgram(false);
  11830. Add([
  11831. '{$modeswitch AdvancedRecords}',
  11832. 'type',
  11833. ' TPoint = record',
  11834. ' x,y: word;',
  11835. ' strict private',
  11836. ' function GetSize: longword;',
  11837. ' procedure SetSize(Value: longword);',
  11838. ' public',
  11839. ' property Size: longword read GetSize write SetSize;',
  11840. ' property Left: word read x write y;',
  11841. ' end;',
  11842. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11843. 'function TPoint.GetSize: longword;',
  11844. 'begin',
  11845. ' x:=y;',
  11846. ' Size:=Size;',
  11847. ' Left:=Left;',
  11848. 'end;',
  11849. 'procedure TPoint.SetSize(Value: longword);',
  11850. 'begin',
  11851. 'end;',
  11852. 'var p,q: TPoint;',
  11853. 'begin',
  11854. ' p.Size:=q.Size;',
  11855. ' p.Left:=q.Left;',
  11856. '']);
  11857. ConvertProgram;
  11858. CheckSource('TestAdvRecord_Property',
  11859. LinesToStr([ // statements
  11860. 'rtl.recNewT(this, "TPoint", function () {',
  11861. ' this.x = 0;',
  11862. ' this.y = 0;',
  11863. ' this.$eq = function (b) {',
  11864. ' return (this.x === b.x) && (this.y === b.y);',
  11865. ' };',
  11866. ' this.$assign = function (s) {',
  11867. ' this.x = s.x;',
  11868. ' this.y = s.y;',
  11869. ' return this;',
  11870. ' };',
  11871. ' this.GetSize = function () {',
  11872. ' var Result = 0;',
  11873. ' this.x = this.y;',
  11874. ' this.SetSize(this.GetSize());',
  11875. ' this.y = this.x;',
  11876. ' return Result;',
  11877. ' };',
  11878. ' this.SetSize = function (Value) {',
  11879. ' };',
  11880. '});',
  11881. 'this.SetSize = function (Value) {',
  11882. '};',
  11883. 'this.p = this.TPoint.$new();',
  11884. 'this.q = this.TPoint.$new();',
  11885. '']),
  11886. LinesToStr([ // $mod.$main
  11887. '$mod.p.SetSize($mod.q.GetSize());',
  11888. '$mod.p.y = $mod.q.x;',
  11889. '']));
  11890. end;
  11891. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11892. begin
  11893. StartProgram(false);
  11894. Add([
  11895. '{$modeswitch AdvancedRecords}',
  11896. 'type',
  11897. ' TPoint = record',
  11898. ' strict private',
  11899. ' function GetItems(Index: word): word;',
  11900. ' procedure SetItems(Index: word; Value: word);',
  11901. ' public',
  11902. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11903. ' end;',
  11904. 'function TPoint.GetItems(Index: word): word;',
  11905. 'begin',
  11906. ' Items[index]:=Items[index];',
  11907. ' self.Items[index]:=self.Items[index];',
  11908. 'end;',
  11909. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11910. 'begin',
  11911. 'end;',
  11912. 'var p: TPoint;',
  11913. 'begin',
  11914. ' p[1]:=p[2];',
  11915. ' p.Items[3]:=p.Items[4];',
  11916. '']);
  11917. ConvertProgram;
  11918. CheckSource('TestAdvRecord_PropertyDefault',
  11919. LinesToStr([ // statements
  11920. 'rtl.recNewT(this, "TPoint", function () {',
  11921. ' this.$eq = function (b) {',
  11922. ' return true;',
  11923. ' };',
  11924. ' this.$assign = function (s) {',
  11925. ' return this;',
  11926. ' };',
  11927. ' this.GetItems = function (Index) {',
  11928. ' var Result = 0;',
  11929. ' this.SetItems(Index, this.GetItems(Index));',
  11930. ' this.SetItems(Index, this.GetItems(Index));',
  11931. ' return Result;',
  11932. ' };',
  11933. ' this.SetItems = function (Index, Value) {',
  11934. ' };',
  11935. '});',
  11936. 'this.p = this.TPoint.$new();',
  11937. '']),
  11938. LinesToStr([ // $mod.$main
  11939. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11940. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11941. '']));
  11942. end;
  11943. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11944. begin
  11945. StartProgram(false);
  11946. Add([
  11947. '{$modeswitch AdvancedRecords}',
  11948. 'type',
  11949. ' TRec = record',
  11950. ' class var',
  11951. ' Fx: longint;',
  11952. ' Fy: longint;',
  11953. ' class function GetInt: longint; static;',
  11954. ' class procedure SetInt(Value: longint); static;',
  11955. ' class procedure DoIt; static;',
  11956. ' class property IntA: longint read Fx write Fy;',
  11957. ' class property IntB: longint read GetInt write SetInt;',
  11958. ' end;',
  11959. 'class function trec.getint: longint;',
  11960. 'begin',
  11961. ' result:=fx;',
  11962. 'end;',
  11963. 'class procedure trec.setint(value: longint);',
  11964. 'begin',
  11965. 'end;',
  11966. 'class procedure trec.doit;',
  11967. 'begin',
  11968. ' IntA:=IntA+1;',
  11969. ' IntB:=IntB+1;',
  11970. 'end;',
  11971. 'var r: trec;',
  11972. 'begin',
  11973. ' trec.inta:=trec.inta+1;',
  11974. ' if trec.intb=2 then;',
  11975. ' trec.intb:=trec.intb+2;',
  11976. ' trec.setint(trec.inta);',
  11977. ' r.inta:=r.inta+1;',
  11978. ' if r.intb=2 then;',
  11979. ' r.intb:=r.intb+2;',
  11980. ' r.setint(r.inta);']);
  11981. ConvertProgram;
  11982. CheckSource('TestAdvRecord_Property_ClassMethod',
  11983. LinesToStr([ // statements
  11984. 'rtl.recNewT(this, "TRec", function () {',
  11985. ' this.Fx = 0;',
  11986. ' this.Fy = 0;',
  11987. ' this.$eq = function (b) {',
  11988. ' return true;',
  11989. ' };',
  11990. ' this.$assign = function (s) {',
  11991. ' return this;',
  11992. ' };',
  11993. ' this.GetInt = function () {',
  11994. ' var Result = 0;',
  11995. ' Result = $mod.TRec.Fx;',
  11996. ' return Result;',
  11997. ' };',
  11998. ' this.SetInt = function (Value) {',
  11999. ' };',
  12000. ' this.DoIt = function () {',
  12001. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12002. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  12003. ' };',
  12004. '}, true);',
  12005. 'this.r = this.TRec.$new();',
  12006. '']),
  12007. LinesToStr([ // $mod.$main
  12008. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  12009. 'if ($mod.TRec.GetInt() === 2) ;',
  12010. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12011. '$mod.TRec.SetInt($mod.TRec.Fx);',
  12012. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  12013. 'if ($mod.TRec.GetInt() === 2) ;',
  12014. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12015. '$mod.TRec.SetInt($mod.r.Fx);',
  12016. '']));
  12017. end;
  12018. procedure TTestModule.TestAdvRecord_Const;
  12019. begin
  12020. StartProgram(false);
  12021. Add([
  12022. '{$modeswitch AdvancedRecords}',
  12023. 'type',
  12024. ' TArrInt = array[3..4] of longint;',
  12025. ' TPoint = record',
  12026. ' x,y: longint;',
  12027. ' class var Count: nativeint;',
  12028. ' end;',
  12029. ' TRec = record',
  12030. ' i: longint;',
  12031. ' a: array of longint;',
  12032. ' s: array[1..2] of longint;',
  12033. ' m: array[1..2,3..4] of longint;',
  12034. ' p: TPoint;',
  12035. ' end;',
  12036. ' TPoints = array of TPoint;',
  12037. 'const',
  12038. ' r: TRec = (',
  12039. ' i:1;',
  12040. ' a:(2,3);',
  12041. ' s:(4,5);',
  12042. ' m:( (11,12), (13,14) );',
  12043. ' p: (x:21)',
  12044. ' );',
  12045. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12046. 'begin']);
  12047. ConvertProgram;
  12048. CheckSource('TestAdvRecord_Const',
  12049. LinesToStr([ // statements
  12050. 'rtl.recNewT(this, "TPoint", function () {',
  12051. ' this.x = 0;',
  12052. ' this.y = 0;',
  12053. ' this.Count = 0;',
  12054. ' this.$eq = function (b) {',
  12055. ' return (this.x === b.x) && (this.y === b.y);',
  12056. ' };',
  12057. ' this.$assign = function (s) {',
  12058. ' this.x = s.x;',
  12059. ' this.y = s.y;',
  12060. ' return this;',
  12061. ' };',
  12062. '}, true);',
  12063. 'rtl.recNewT(this, "TRec", function () {',
  12064. ' this.i = 0;',
  12065. ' this.m$a$clone = function (a) {',
  12066. ' var b = [];',
  12067. ' b.length = 2;',
  12068. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12069. ' return b;',
  12070. ' };',
  12071. ' this.$new = function () {',
  12072. ' var r = Object.create(this);',
  12073. ' r.a = [];',
  12074. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12075. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12076. ' r.p = $mod.TPoint.$new();',
  12077. ' return r;',
  12078. ' };',
  12079. ' this.$eq = function (b) {',
  12080. ' 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);',
  12081. ' };',
  12082. ' this.$assign = function (s) {',
  12083. ' this.i = s.i;',
  12084. ' this.a = rtl.arrayRef(s.a);',
  12085. ' this.s = s.s.slice(0);',
  12086. ' this.m = this.m$a$clone(s.m);',
  12087. ' this.p.$assign(s.p);',
  12088. ' return this;',
  12089. ' };',
  12090. '});',
  12091. 'this.r = this.TRec.$clone({',
  12092. ' i: 1,',
  12093. ' a: [2, 3],',
  12094. ' s: [4, 5],',
  12095. ' m: [[11, 12], [13, 14]],',
  12096. ' p: this.TPoint.$clone({',
  12097. ' x: 21,',
  12098. ' y: 0',
  12099. ' })',
  12100. '});',
  12101. 'this.p = [this.TPoint.$clone({',
  12102. ' x: 1,',
  12103. ' y: 2',
  12104. '}), this.TPoint.$clone({',
  12105. ' x: 3,',
  12106. ' y: 4',
  12107. '})];',
  12108. '']),
  12109. LinesToStr([ // $mod.$main
  12110. '']));
  12111. end;
  12112. procedure TTestModule.TestAdvRecord_ExternalField;
  12113. begin
  12114. StartProgram(false);
  12115. Add([
  12116. '{$modeswitch AdvancedRecords}',
  12117. '{$modeswitch externalclass}',
  12118. 'type',
  12119. ' TCar = record',
  12120. ' public',
  12121. ' Intern: longint external name ''$Intern'';',
  12122. ' Intern2: longint external name ''$Intern2'';',
  12123. ' Bracket: longint external name ''["A B"]'';',
  12124. ' procedure DoIt;',
  12125. ' end;',
  12126. 'procedure tcar.doit;',
  12127. 'begin',
  12128. ' Intern:=Intern+1;',
  12129. ' Intern2:=Intern2+2;',
  12130. ' Bracket:=Bracket+3;',
  12131. 'end;',
  12132. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  12133. 'begin',
  12134. ' Rec.intern:=Rec.intern+1;',
  12135. ' Rec.intern2:=Rec.intern2+2;',
  12136. ' Rec.Bracket:=Rec.Bracket+3;',
  12137. ' with Rec do begin',
  12138. ' intern:=intern+1;',
  12139. ' intern2:=intern2+2;',
  12140. ' Bracket:=Bracket+3;',
  12141. ' end;']);
  12142. ConvertProgram;
  12143. CheckSource('TestAdvRecord_ExternalField',
  12144. LinesToStr([ // statements
  12145. 'rtl.recNewT(this, "TCar", function () {',
  12146. ' this.$eq = function (b) {',
  12147. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  12148. ' };',
  12149. ' this.$assign = function (s) {',
  12150. ' this.$Intern = s.$Intern;',
  12151. ' this.$Intern2 = s.$Intern2;',
  12152. ' this["A B"] = s["A B"];',
  12153. ' return this;',
  12154. ' };',
  12155. ' this.DoIt = function () {',
  12156. ' this.$Intern = this.$Intern + 1;',
  12157. ' this.$Intern2 = this.$Intern2 + 2;',
  12158. ' this["A B"] = this["A B"] + 3;',
  12159. ' };',
  12160. '});',
  12161. 'this.Rec = this.TCar.$clone({',
  12162. ' $Intern: 11,',
  12163. ' $Intern2: 12,',
  12164. ' "A B": 13',
  12165. '});',
  12166. '']),
  12167. LinesToStr([ // $mod.$main
  12168. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  12169. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  12170. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  12171. 'var $with = $mod.Rec;',
  12172. '$with.$Intern = $with.$Intern + 1;',
  12173. '$with.$Intern2 = $with.$Intern2 + 2;',
  12174. '$with["A B"] = $with["A B"] + 3;',
  12175. '']));
  12176. end;
  12177. procedure TTestModule.TestAdvRecord_SubRecord;
  12178. begin
  12179. StartProgram(false);
  12180. Add([
  12181. '{$modeswitch AdvancedRecords}',
  12182. 'type',
  12183. ' TRec = record',
  12184. ' type',
  12185. ' TPoint = record',
  12186. ' x,y: longint;',
  12187. ' class var Count: nativeint;',
  12188. ' procedure DoIt;',
  12189. ' class procedure DoThat; static;',
  12190. ' end;',
  12191. ' var',
  12192. ' i: longint;',
  12193. ' p: TPoint;',
  12194. ' procedure DoSome;',
  12195. ' end;',
  12196. 'const',
  12197. ' r: TRec = (',
  12198. ' i:1;',
  12199. ' p: (x:21;y:22)',
  12200. ' );',
  12201. 'procedure TRec.DoSome;',
  12202. 'begin',
  12203. ' p.x:=p.y+1;',
  12204. ' p.Count:=p.Count+2;',
  12205. 'end;',
  12206. 'procedure TRec.TPoint.DoIt;',
  12207. 'begin',
  12208. ' Count:=Count+3;',
  12209. 'end;',
  12210. 'class procedure TRec.TPoint.DoThat;',
  12211. 'begin',
  12212. ' Count:=Count+4;',
  12213. 'end;',
  12214. 'begin']);
  12215. ConvertProgram;
  12216. CheckSource('TestAdvRecord_SubRecord',
  12217. LinesToStr([ // statements
  12218. 'rtl.recNewT(this, "TRec", function () {',
  12219. ' rtl.recNewT(this, "TPoint", function () {',
  12220. ' this.x = 0;',
  12221. ' this.y = 0;',
  12222. ' this.Count = 0;',
  12223. ' this.$eq = function (b) {',
  12224. ' return (this.x === b.x) && (this.y === b.y);',
  12225. ' };',
  12226. ' this.$assign = function (s) {',
  12227. ' this.x = s.x;',
  12228. ' this.y = s.y;',
  12229. ' return this;',
  12230. ' };',
  12231. ' this.DoIt = function () {',
  12232. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  12233. ' };',
  12234. ' this.DoThat = function () {',
  12235. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  12236. ' };',
  12237. ' }, true);',
  12238. ' this.i = 0;',
  12239. ' this.$new = function () {',
  12240. ' var r = Object.create(this);',
  12241. ' r.p = this.TPoint.$new();',
  12242. ' return r;',
  12243. ' };',
  12244. ' this.$eq = function (b) {',
  12245. ' return (this.i === b.i) && this.p.$eq(b.p);',
  12246. ' };',
  12247. ' this.$assign = function (s) {',
  12248. ' this.i = s.i;',
  12249. ' this.p.$assign(s.p);',
  12250. ' return this;',
  12251. ' };',
  12252. ' this.DoSome = function () {',
  12253. ' this.p.x = this.p.y + 1;',
  12254. ' this.TPoint.Count = this.p.Count + 2;',
  12255. ' };',
  12256. '}, true);',
  12257. 'this.r = this.TRec.$clone({',
  12258. ' i: 1,',
  12259. ' p: this.TRec.TPoint.$clone({',
  12260. ' x: 21,',
  12261. ' y: 22',
  12262. ' })',
  12263. '});',
  12264. '']),
  12265. LinesToStr([ // $mod.$main
  12266. '']));
  12267. end;
  12268. procedure TTestModule.TestAdvRecord_SubClass;
  12269. begin
  12270. StartProgram(false);
  12271. Add([
  12272. '{$modeswitch AdvancedRecords}',
  12273. 'type',
  12274. ' TObject = class end;',
  12275. ' TPoint = record',
  12276. ' type',
  12277. ' TBird = class',
  12278. ' procedure DoIt;',
  12279. ' class procedure Glob;',
  12280. ' end;',
  12281. ' procedure DoIt(b: TBird);',
  12282. ' end;',
  12283. 'procedure TPoint.TBird.DoIt;',
  12284. 'begin',
  12285. ' doit;',
  12286. ' self.doit;',
  12287. ' glob;',
  12288. ' self.glob;',
  12289. 'end;',
  12290. 'class procedure TPoint.TBird.Glob;',
  12291. 'begin',
  12292. ' glob;',
  12293. ' self.glob;',
  12294. 'end;',
  12295. 'procedure TPoint.DoIt(b: TBird);',
  12296. 'begin',
  12297. ' b.doit;',
  12298. ' b.glob;',
  12299. ' TBird.glob;',
  12300. 'end;',
  12301. 'begin',
  12302. '']);
  12303. ConvertProgram;
  12304. CheckSource('TestAdvRecord_SubClass',
  12305. LinesToStr([ // statements
  12306. 'rtl.createClass(this, "TObject", null, function () {',
  12307. ' this.$init = function () {',
  12308. ' };',
  12309. ' this.$final = function () {',
  12310. ' };',
  12311. '});',
  12312. 'rtl.recNewT(this, "TPoint", function () {',
  12313. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12314. ' this.DoIt = function () {',
  12315. ' this.DoIt();',
  12316. ' this.DoIt();',
  12317. ' this.$class.Glob();',
  12318. ' this.$class.Glob();',
  12319. ' };',
  12320. ' this.Glob = function () {',
  12321. ' this.Glob();',
  12322. ' this.Glob();',
  12323. ' };',
  12324. ' }, "TPoint.TBird");',
  12325. ' this.$eq = function (b) {',
  12326. ' return true;',
  12327. ' };',
  12328. ' this.$assign = function (s) {',
  12329. ' return this;',
  12330. ' };',
  12331. ' this.DoIt = function (b) {',
  12332. ' b.DoIt();',
  12333. ' b.$class.Glob();',
  12334. ' this.TBird.Glob();',
  12335. ' };',
  12336. '}, true);',
  12337. '']),
  12338. LinesToStr([ // $mod.$main
  12339. '']));
  12340. end;
  12341. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12342. begin
  12343. StartProgram(false);
  12344. Add([
  12345. '{$modeswitch AdvancedRecords}',
  12346. 'type',
  12347. ' IUnknown = interface end;',
  12348. ' TPoint = record',
  12349. ' type IBird = interface end;',
  12350. ' end;',
  12351. 'begin',
  12352. '']);
  12353. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12354. nNotYetImplemented);
  12355. ParseProgram;
  12356. end;
  12357. procedure TTestModule.TestAdvRecord_Constructor;
  12358. begin
  12359. StartProgram(false);
  12360. Add([
  12361. '{$modeswitch AdvancedRecords}',
  12362. 'type',
  12363. ' TPoint = record',
  12364. ' x,y: longint;',
  12365. ' class procedure Run(w: longint = 13); static;',
  12366. ' constructor Create(ax: longint; ay: longint = -1);',
  12367. ' end;',
  12368. 'class procedure tpoint.run(w: longint);',
  12369. 'begin',
  12370. ' run;',
  12371. ' run();',
  12372. 'end;',
  12373. 'constructor tpoint.create(ax,ay: longint);',
  12374. 'begin',
  12375. ' x:=ax;',
  12376. ' self.y:=ay;',
  12377. ' run;',
  12378. ' run(ax);',
  12379. 'end;',
  12380. 'var r: TPoint;',
  12381. 'begin',
  12382. ' r:=TPoint.Create(1,2);',
  12383. ' with TPoint do r:=Create(1,2);',
  12384. ' r.Create(3);',
  12385. ' r:=r.Create(4);',
  12386. '']);
  12387. ConvertProgram;
  12388. CheckSource('TestAdvRecord_Constructor',
  12389. LinesToStr([ // statements
  12390. 'rtl.recNewT(this, "TPoint", function () {',
  12391. ' this.x = 0;',
  12392. ' this.y = 0;',
  12393. ' this.$eq = function (b) {',
  12394. ' return (this.x === b.x) && (this.y === b.y);',
  12395. ' };',
  12396. ' this.$assign = function (s) {',
  12397. ' this.x = s.x;',
  12398. ' this.y = s.y;',
  12399. ' return this;',
  12400. ' };',
  12401. ' this.Run = function (w) {',
  12402. ' $mod.TPoint.Run(13);',
  12403. ' $mod.TPoint.Run(13);',
  12404. ' };',
  12405. ' this.Create = function (ax, ay) {',
  12406. ' this.x = ax;',
  12407. ' this.y = ay;',
  12408. ' this.Run(13);',
  12409. ' this.Run(ax);',
  12410. ' return this;',
  12411. ' };',
  12412. '});',
  12413. 'this.r = this.TPoint.$new();',
  12414. '']),
  12415. LinesToStr([ // $mod.$main
  12416. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12417. 'var $with = $mod.TPoint;',
  12418. '$mod.r.$assign($with.$new().Create(1, 2));',
  12419. '$mod.r.Create(3, -1);',
  12420. '$mod.r.$assign($mod.r.Create(4, -1));',
  12421. '']));
  12422. end;
  12423. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12424. begin
  12425. StartProgram(false);
  12426. Add([
  12427. '{$modeswitch AdvancedRecords}',
  12428. 'type',
  12429. ' TPoint = record',
  12430. ' class var x: longint;',
  12431. ' class procedure Fly; static;',
  12432. ' class constructor Init;',
  12433. ' end;',
  12434. 'var count: word;',
  12435. 'class procedure Tpoint.Fly;',
  12436. 'begin',
  12437. 'end;',
  12438. 'class constructor tpoint.init;',
  12439. 'begin',
  12440. ' count:=count+1;',
  12441. ' x:=x+3;',
  12442. ' tpoint.x:=tpoint.x+4;',
  12443. ' fly;',
  12444. ' tpoint.fly;',
  12445. 'end;',
  12446. 'var r: TPoint;',
  12447. 'begin',
  12448. ' r.x:=r.x+10;',
  12449. ' r.Fly;',
  12450. ' r.Fly();',
  12451. '']);
  12452. ConvertProgram;
  12453. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12454. LinesToStr([ // statements
  12455. 'rtl.recNewT(this, "TPoint", function () {',
  12456. ' this.x = 0;',
  12457. ' this.$eq = function (b) {',
  12458. ' return true;',
  12459. ' };',
  12460. ' this.$assign = function (s) {',
  12461. ' return this;',
  12462. ' };',
  12463. ' this.Fly = function () {',
  12464. ' };',
  12465. '}, true);',
  12466. 'this.count = 0;',
  12467. 'this.r = this.TPoint.$new();',
  12468. '']),
  12469. LinesToStr([ // $mod.$main
  12470. '(function () {',
  12471. ' $mod.count = $mod.count + 1;',
  12472. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12473. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12474. ' $mod.TPoint.Fly();',
  12475. ' $mod.TPoint.Fly();',
  12476. '})();',
  12477. '$mod.TPoint.x = $mod.r.x + 10;',
  12478. '$mod.TPoint.Fly();',
  12479. '$mod.TPoint.Fly();',
  12480. '']));
  12481. end;
  12482. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12483. begin
  12484. StartUnit(false);
  12485. Add([
  12486. 'interface',
  12487. '{$modeswitch AdvancedRecords}',
  12488. 'type',
  12489. ' TPoint = record',
  12490. ' class var x: longint;',
  12491. ' class procedure Fly; static;',
  12492. ' class constructor Init;',
  12493. ' end;',
  12494. 'implementation',
  12495. 'var count: word;',
  12496. 'class procedure Tpoint.Fly;',
  12497. 'begin',
  12498. 'end;',
  12499. 'class constructor tpoint.init;',
  12500. 'begin',
  12501. ' count:=count+1;',
  12502. ' x:=3;',
  12503. ' tpoint.x:=4;',
  12504. ' fly;',
  12505. ' tpoint.fly;',
  12506. 'end;',
  12507. '']);
  12508. ConvertUnit;
  12509. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12510. LinesToStr([ // statements
  12511. 'var $impl = $mod.$impl;',
  12512. 'rtl.recNewT(this, "TPoint", function () {',
  12513. ' this.x = 0;',
  12514. ' this.$eq = function (b) {',
  12515. ' return true;',
  12516. ' };',
  12517. ' this.$assign = function (s) {',
  12518. ' return this;',
  12519. ' };',
  12520. ' this.Fly = function () {',
  12521. ' };',
  12522. '}, true);',
  12523. '']),
  12524. LinesToStr([ // $mod.$init
  12525. '(function () {',
  12526. ' $impl.count = $impl.count + 1;',
  12527. ' $mod.TPoint.x = 3;',
  12528. ' $mod.TPoint.x = 4;',
  12529. ' $mod.TPoint.Fly();',
  12530. ' $mod.TPoint.Fly();',
  12531. '})();',
  12532. '']),
  12533. LinesToStr([ // $mod.$main
  12534. '$impl.count = 0;',
  12535. '']));
  12536. end;
  12537. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12538. begin
  12539. StartProgram(false);
  12540. Add(['type',
  12541. ' TObject = class',
  12542. ' public',
  12543. ' constructor Create;',
  12544. ' destructor Destroy;',
  12545. ' end;',
  12546. ' TBird = TObject;',
  12547. 'constructor tobject.create;',
  12548. 'begin end;',
  12549. 'destructor tobject.destroy;',
  12550. 'begin end;',
  12551. 'var Obj: tobject;',
  12552. 'begin',
  12553. ' obj:=tobject.create;',
  12554. ' obj:=tobject.create();',
  12555. ' obj:=tbird.create;',
  12556. ' obj:=tbird.create();',
  12557. ' obj:=obj.create();',
  12558. ' obj.destroy;',
  12559. '']);
  12560. ConvertProgram;
  12561. CheckSource('TestClass_TObjectDefaultConstructor',
  12562. LinesToStr([ // statements
  12563. 'rtl.createClass(this,"TObject",null,function(){',
  12564. ' this.$init = function () {',
  12565. ' };',
  12566. ' this.$final = function () {',
  12567. ' };',
  12568. ' this.Create = function(){',
  12569. ' return this;',
  12570. ' };',
  12571. ' this.Destroy = function(){',
  12572. ' };',
  12573. '});',
  12574. 'this.Obj = null;'
  12575. ]),
  12576. LinesToStr([ // $mod.$main
  12577. '$mod.Obj = $mod.TObject.$create("Create");',
  12578. '$mod.Obj = $mod.TObject.$create("Create");',
  12579. '$mod.Obj = $mod.TObject.$create("Create");',
  12580. '$mod.Obj = $mod.TObject.$create("Create");',
  12581. '$mod.Obj = $mod.Obj.Create();',
  12582. '$mod.Obj.$destroy("Destroy");',
  12583. '']));
  12584. end;
  12585. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12586. begin
  12587. StartProgram(false);
  12588. Add('type');
  12589. Add(' TObject = class');
  12590. Add(' public');
  12591. Add(' constructor Create(Par: longint);');
  12592. Add(' end;');
  12593. Add('constructor tobject.create(par: longint);');
  12594. Add('begin end;');
  12595. Add('var Obj: tobject;');
  12596. Add('begin');
  12597. Add(' obj:=tobject.create(3);');
  12598. ConvertProgram;
  12599. CheckSource('TestClass_TObjectConstructorWithParams',
  12600. LinesToStr([ // statements
  12601. 'rtl.createClass(this,"TObject",null,function(){',
  12602. ' this.$init = function () {',
  12603. ' };',
  12604. ' this.$final = function () {',
  12605. ' };',
  12606. ' this.Create = function(Par){',
  12607. ' return this;',
  12608. ' };',
  12609. '});',
  12610. 'this.Obj = null;'
  12611. ]),
  12612. LinesToStr([ // $mod.$main
  12613. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12614. ]));
  12615. end;
  12616. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12617. begin
  12618. StartProgram(false);
  12619. Add('type');
  12620. Add(' TObject = class');
  12621. Add(' public');
  12622. Add(' constructor Create;');
  12623. Add(' end;');
  12624. Add(' TTest = class(TObject)');
  12625. Add(' public');
  12626. Add(' constructor Create(const Par: longint = 1);');
  12627. Add(' end;');
  12628. Add('constructor tobject.create;');
  12629. Add('begin end;');
  12630. Add('constructor ttest.create(const par: longint);');
  12631. Add('begin end;');
  12632. Add('var t: ttest;');
  12633. Add('begin');
  12634. Add(' t:=ttest.create;');
  12635. Add(' t:=ttest.create(2);');
  12636. ConvertProgram;
  12637. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12638. LinesToStr([ // statements
  12639. 'rtl.createClass(this,"TObject",null,function(){',
  12640. ' this.$init = function () {',
  12641. ' };',
  12642. ' this.$final = function () {',
  12643. ' };',
  12644. ' this.Create = function(){',
  12645. ' return this;',
  12646. ' };',
  12647. '});',
  12648. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12649. ' this.Create$1 = function (Par) {',
  12650. ' return this;',
  12651. ' };',
  12652. '});',
  12653. 'this.t = null;'
  12654. ]),
  12655. LinesToStr([ // $mod.$main
  12656. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12657. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12658. ]));
  12659. end;
  12660. procedure TTestModule.TestClass_Var;
  12661. begin
  12662. StartProgram(false);
  12663. Add([
  12664. 'type',
  12665. ' TObject = class',
  12666. ' public',
  12667. ' vI: longint;',
  12668. ' constructor Create(Par: longint);',
  12669. ' end;',
  12670. 'constructor tobject.create(par: longint);',
  12671. 'begin',
  12672. ' vi:=par+3',
  12673. 'end;',
  12674. 'var Obj: tobject;',
  12675. 'begin',
  12676. ' obj:=tobject.create(4);',
  12677. ' obj.vi:=obj.VI+5;']);
  12678. ConvertProgram;
  12679. CheckSource('TestClass_Var',
  12680. LinesToStr([ // statements
  12681. 'rtl.createClass(this,"TObject",null,function(){',
  12682. ' this.$init = function () {',
  12683. ' this.vI = 0;',
  12684. ' };',
  12685. ' this.$final = function () {',
  12686. ' };',
  12687. ' this.Create = function(Par){',
  12688. ' this.vI = Par+3;',
  12689. ' return this;',
  12690. ' };',
  12691. '});',
  12692. 'this.Obj = null;'
  12693. ]),
  12694. LinesToStr([ // $mod.$main
  12695. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12696. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12697. ]));
  12698. end;
  12699. procedure TTestModule.TestClass_Method;
  12700. begin
  12701. StartProgram(false);
  12702. Add('type');
  12703. Add(' TObject = class');
  12704. Add(' public');
  12705. Add(' vI: longint;');
  12706. Add(' Sub: TObject;');
  12707. Add(' constructor Create;');
  12708. Add(' function GetIt(Par: longint): tobject;');
  12709. Add(' end;');
  12710. Add('constructor tobject.create; begin end;');
  12711. Add('function tobject.getit(par: longint): tobject;');
  12712. Add('begin');
  12713. Add(' Self.vi:=par+3;');
  12714. Add(' Result:=self.sub;');
  12715. Add('end;');
  12716. Add('var Obj: tobject;');
  12717. Add('begin');
  12718. Add(' obj:=tobject.create;');
  12719. Add(' obj.getit(4);');
  12720. Add(' obj.sub.sub:=nil;');
  12721. Add(' obj.sub.getit(5);');
  12722. Add(' obj.sub.getit(6).SUB:=nil;');
  12723. Add(' obj.sub.getit(7).GETIT(8);');
  12724. Add(' obj.sub.getit(9).SuB.getit(10);');
  12725. ConvertProgram;
  12726. CheckSource('TestClass_Method',
  12727. LinesToStr([ // statements
  12728. 'rtl.createClass(this,"TObject",null,function(){',
  12729. ' this.$init = function () {',
  12730. ' this.vI = 0;',
  12731. ' this.Sub = null;',
  12732. ' };',
  12733. ' this.$final = function () {',
  12734. ' this.Sub = undefined;',
  12735. ' };',
  12736. ' this.Create = function(){',
  12737. ' return this;',
  12738. ' };',
  12739. ' this.GetIt = function(Par){',
  12740. ' var Result = null;',
  12741. ' this.vI = Par + 3;',
  12742. ' Result = this.Sub;',
  12743. ' return Result;',
  12744. ' };',
  12745. '});',
  12746. 'this.Obj = null;'
  12747. ]),
  12748. LinesToStr([ // $mod.$main
  12749. '$mod.Obj = $mod.TObject.$create("Create");',
  12750. '$mod.Obj.GetIt(4);',
  12751. '$mod.Obj.Sub.Sub=null;',
  12752. '$mod.Obj.Sub.GetIt(5);',
  12753. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12754. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12755. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12756. ]));
  12757. end;
  12758. procedure TTestModule.TestClass_Implementation;
  12759. begin
  12760. StartUnit(false);
  12761. Add([
  12762. 'interface',
  12763. 'type',
  12764. ' TObject = class',
  12765. ' constructor Create;',
  12766. ' end;',
  12767. 'implementation',
  12768. 'type',
  12769. ' TIntClass = class',
  12770. ' constructor Create; reintroduce;',
  12771. ' class procedure DoGlob;',
  12772. ' end;',
  12773. 'constructor tintclass.create;',
  12774. 'begin',
  12775. ' inherited;',
  12776. ' inherited create;',
  12777. ' doglob;',
  12778. 'end;',
  12779. 'class procedure tintclass.doglob;',
  12780. 'begin',
  12781. 'end;',
  12782. 'constructor tobject.create;',
  12783. 'var',
  12784. ' iC: tintclass;',
  12785. 'begin',
  12786. ' ic:=tintclass.create;',
  12787. ' tintclass.doglob;',
  12788. ' ic.doglob;',
  12789. 'end;',
  12790. 'initialization',
  12791. ' tintclass.doglob;',
  12792. '']);
  12793. ConvertUnit;
  12794. CheckSource('TestClass_Implementation',
  12795. LinesToStr([ // statements
  12796. 'var $impl = $mod.$impl;',
  12797. 'rtl.createClass(this, "TObject", null, function () {',
  12798. ' this.$init = function () {',
  12799. ' };',
  12800. ' this.$final = function () {',
  12801. ' };',
  12802. ' this.Create = function () {',
  12803. ' var iC = null;',
  12804. ' iC = $impl.TIntClass.$create("Create$1");',
  12805. ' $impl.TIntClass.DoGlob();',
  12806. ' iC.$class.DoGlob();',
  12807. ' return this;',
  12808. ' };',
  12809. '});',
  12810. '']),
  12811. LinesToStr([ // $mod.$main
  12812. '$impl.TIntClass.DoGlob();',
  12813. '']),
  12814. LinesToStr([
  12815. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12816. ' this.Create$1 = function () {',
  12817. ' $mod.TObject.Create.call(this);',
  12818. ' $mod.TObject.Create.call(this);',
  12819. ' this.$class.DoGlob();',
  12820. ' return this;',
  12821. ' };',
  12822. ' this.DoGlob = function () {',
  12823. ' };',
  12824. '});',
  12825. '']));
  12826. end;
  12827. procedure TTestModule.TestClass_Inheritance;
  12828. begin
  12829. StartProgram(false);
  12830. Add('type');
  12831. Add(' TObject = class');
  12832. Add(' public');
  12833. Add(' constructor Create;');
  12834. Add(' end;');
  12835. Add(' TClassA = class');
  12836. Add(' end;');
  12837. Add(' TClassB = class(TObject)');
  12838. Add(' procedure ProcB;');
  12839. Add(' end;');
  12840. Add('constructor tobject.create; begin end;');
  12841. Add('procedure tclassb.procb; begin end;');
  12842. Add('var');
  12843. Add(' oO: TObject;');
  12844. Add(' oA: TClassA;');
  12845. Add(' oB: TClassB;');
  12846. Add('begin');
  12847. Add(' oO:=tobject.Create;');
  12848. Add(' oA:=tclassa.Create;');
  12849. Add(' ob:=tclassb.Create;');
  12850. Add(' if oo is tclassa then ;');
  12851. Add(' ob:=oo as tclassb;');
  12852. Add(' (oo as tclassb).procb;');
  12853. ConvertProgram;
  12854. CheckSource('TestClass_Inheritance',
  12855. LinesToStr([ // statements
  12856. 'rtl.createClass(this,"TObject",null,function(){',
  12857. ' this.$init = function () {',
  12858. ' };',
  12859. ' this.$final = function () {',
  12860. ' };',
  12861. ' this.Create = function () {',
  12862. ' return this;',
  12863. ' };',
  12864. '});',
  12865. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12866. '});',
  12867. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12868. ' this.ProcB = function () {',
  12869. ' };',
  12870. '});',
  12871. 'this.oO = null;',
  12872. 'this.oA = null;',
  12873. 'this.oB = null;'
  12874. ]),
  12875. LinesToStr([ // $mod.$main
  12876. '$mod.oO = $mod.TObject.$create("Create");',
  12877. '$mod.oA = $mod.TClassA.$create("Create");',
  12878. '$mod.oB = $mod.TClassB.$create("Create");',
  12879. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12880. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12881. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12882. ]));
  12883. end;
  12884. procedure TTestModule.TestClass_TypeAlias;
  12885. begin
  12886. StartProgram(false);
  12887. Add([
  12888. '{$interfaces corba}',
  12889. 'type',
  12890. ' IObject = interface',
  12891. ' end;',
  12892. ' IBird = type IObject;',
  12893. ' TObject = class',
  12894. ' end;',
  12895. ' TBird = type TObject;',
  12896. 'var',
  12897. ' oObj: TObject;',
  12898. ' oBird: TBird;',
  12899. ' IntfObj: IObject;',
  12900. ' IntfBird: IBird;',
  12901. 'begin',
  12902. ' oObj:=oBird;',
  12903. '']);
  12904. ConvertProgram;
  12905. CheckSource('TestClass_TypeAlias',
  12906. LinesToStr([ // statements
  12907. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12908. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12909. 'rtl.createClass(this, "TObject", null, function () {',
  12910. ' this.$init = function () {',
  12911. ' };',
  12912. ' this.$final = function () {',
  12913. ' };',
  12914. '});',
  12915. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12916. '});',
  12917. 'this.oObj = null;',
  12918. 'this.oBird = null;',
  12919. 'this.IntfObj = null;',
  12920. 'this.IntfBird = null;',
  12921. '']),
  12922. LinesToStr([ // $mod.$main
  12923. '$mod.oObj = $mod.oBird;',
  12924. '']));
  12925. end;
  12926. procedure TTestModule.TestClass_AbstractMethod;
  12927. begin
  12928. StartProgram(false);
  12929. Add('type');
  12930. Add(' TObject = class');
  12931. Add(' public');
  12932. Add(' procedure DoIt; virtual; abstract;');
  12933. Add(' end;');
  12934. Add('begin');
  12935. ConvertProgram;
  12936. CheckSource('TestClass_AbstractMethod',
  12937. LinesToStr([ // statements
  12938. 'rtl.createClass(this,"TObject",null,function(){',
  12939. ' this.$init = function () {',
  12940. ' };',
  12941. ' this.$final = function () {',
  12942. ' };',
  12943. '});'
  12944. ]),
  12945. LinesToStr([ // this.$main
  12946. ''
  12947. ]));
  12948. end;
  12949. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12950. begin
  12951. StartProgram(false);
  12952. Add([
  12953. 'type',
  12954. ' TObject = class',
  12955. ' procedure DoAbstract; virtual; abstract;',
  12956. ' procedure DoVirtual; virtual;',
  12957. ' procedure DoIt;',
  12958. ' end;',
  12959. ' TA = class',
  12960. ' procedure doabstract; override;',
  12961. ' procedure dovirtual; override;',
  12962. ' procedure DoSome;',
  12963. ' end;',
  12964. 'procedure tobject.dovirtual;',
  12965. 'begin',
  12966. ' inherited; // call non existing ancestor -> ignore silently',
  12967. 'end;',
  12968. 'procedure tobject.doit;',
  12969. 'begin',
  12970. 'end;',
  12971. 'procedure ta.doabstract;',
  12972. 'begin',
  12973. ' inherited dovirtual; // call TObject.DoVirtual',
  12974. 'end;',
  12975. 'procedure ta.dovirtual;',
  12976. 'begin',
  12977. ' inherited; // call TObject.DoVirtual',
  12978. ' inherited dovirtual; // call TObject.DoVirtual',
  12979. ' inherited dovirtual(); // call TObject.DoVirtual',
  12980. ' doit;',
  12981. ' doit();',
  12982. 'end;',
  12983. 'procedure ta.dosome;',
  12984. 'begin',
  12985. ' inherited; // call non existing ancestor method -> silently ignore',
  12986. 'end;',
  12987. 'begin']);
  12988. ConvertProgram;
  12989. CheckSource('TestClass_CallInherited_ProcNoParams',
  12990. LinesToStr([ // statements
  12991. 'rtl.createClass(this,"TObject",null,function(){',
  12992. ' this.$init = function () {',
  12993. ' };',
  12994. ' this.$final = function () {',
  12995. ' };',
  12996. ' this.DoVirtual = function () {',
  12997. ' };',
  12998. ' this.DoIt = function () {',
  12999. ' };',
  13000. '});',
  13001. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13002. ' this.DoAbstract = function () {',
  13003. ' $mod.TObject.DoVirtual.call(this);',
  13004. ' };',
  13005. ' this.DoVirtual = function () {',
  13006. ' $mod.TObject.DoVirtual.call(this);',
  13007. ' $mod.TObject.DoVirtual.call(this);',
  13008. ' $mod.TObject.DoVirtual.call(this);',
  13009. ' this.DoIt();',
  13010. ' this.DoIt();',
  13011. ' };',
  13012. ' this.DoSome = function () {',
  13013. ' };',
  13014. '});'
  13015. ]),
  13016. LinesToStr([ // this.$main
  13017. ''
  13018. ]));
  13019. end;
  13020. procedure TTestModule.TestClass_CallInherited_WithParams;
  13021. begin
  13022. StartProgram(false);
  13023. Add([
  13024. 'type',
  13025. ' TObject = class',
  13026. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  13027. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  13028. ' procedure DoIt(pA: longint; pB: longint = 0);',
  13029. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  13030. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13031. ' end;',
  13032. ' TClassA = class',
  13033. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  13034. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  13035. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13036. ' end;',
  13037. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  13038. 'begin',
  13039. 'end;',
  13040. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  13041. 'begin',
  13042. 'end;',
  13043. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  13044. 'begin',
  13045. 'end;',
  13046. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  13047. 'begin',
  13048. 'end;',
  13049. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  13050. 'begin',
  13051. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13052. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13053. 'end;',
  13054. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  13055. 'begin',
  13056. ' inherited; // call TObject.DoVirtual(pA,pB)',
  13057. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13058. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13059. ' doit(pa,pb);',
  13060. ' doit(pa);',
  13061. ' doit2(pa);',
  13062. ' doit2;',
  13063. 'end;',
  13064. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  13065. 'begin',
  13066. ' pa:=inherited;',
  13067. 'end;',
  13068. 'begin']);
  13069. ConvertProgram;
  13070. CheckSource('TestClass_CallInherited_WithParams',
  13071. LinesToStr([ // statements
  13072. 'rtl.createClass(this,"TObject",null,function(){',
  13073. ' this.$init = function () {',
  13074. ' };',
  13075. ' this.$final = function () {',
  13076. ' };',
  13077. ' this.DoVirtual = function (pA,pB) {',
  13078. ' };',
  13079. ' this.DoIt = function (pA,pB) {',
  13080. ' };',
  13081. ' this.DoIt2 = function (pA,pB) {',
  13082. ' };',
  13083. ' this.GetIt = function (pA, pB) {',
  13084. ' var Result = 0;',
  13085. ' return Result;',
  13086. ' };',
  13087. '});',
  13088. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  13089. ' this.DoAbstract = function (pA,pB) {',
  13090. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13091. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13092. ' };',
  13093. ' this.DoVirtual = function (pA,pB) {',
  13094. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  13095. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13096. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13097. ' this.DoIt(pA,pB);',
  13098. ' this.DoIt(pA,0);',
  13099. ' this.DoIt2(pA,2);',
  13100. ' this.DoIt2(1,2);',
  13101. ' };',
  13102. ' this.GetIt$1 = function (pA, pB) {',
  13103. ' var Result = 0;',
  13104. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  13105. ' return Result;',
  13106. ' };',
  13107. '});'
  13108. ]),
  13109. LinesToStr([ // this.$main
  13110. ''
  13111. ]));
  13112. end;
  13113. procedure TTestModule.TestClasS_CallInheritedConstructor;
  13114. begin
  13115. StartProgram(false);
  13116. Add('type');
  13117. Add(' TObject = class');
  13118. Add(' constructor Create; virtual;');
  13119. Add(' constructor CreateWithB(b: boolean);');
  13120. Add(' end;');
  13121. Add(' TA = class');
  13122. Add(' constructor Create; override;');
  13123. Add(' constructor CreateWithC(c: char);');
  13124. Add(' procedure DoIt;');
  13125. Add(' class function DoSome: TObject;');
  13126. Add(' end;');
  13127. Add('constructor tobject.create;');
  13128. Add('begin');
  13129. Add(' inherited; // call non existing ancestor -> ignore silently');
  13130. Add('end;');
  13131. Add('constructor tobject.createwithb(b: boolean);');
  13132. Add('begin');
  13133. Add(' inherited; // call non existing ancestor -> ignore silently');
  13134. Add(' create; // normal call');
  13135. Add('end;');
  13136. Add('constructor ta.create;');
  13137. Add('begin');
  13138. Add(' inherited; // normal call TObject.Create');
  13139. Add(' inherited create; // normal call TObject.Create');
  13140. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  13141. Add('end;');
  13142. Add('constructor ta.createwithc(c: char);');
  13143. Add('begin');
  13144. Add(' inherited create; // call TObject.Create');
  13145. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  13146. Add(' doit;');
  13147. Add(' doit();');
  13148. Add(' dosome;');
  13149. Add('end;');
  13150. Add('procedure ta.doit;');
  13151. Add('begin');
  13152. Add(' create; // normal call');
  13153. Add(' createwithb(false); // normal call');
  13154. Add(' createwithc(''c''); // normal call');
  13155. Add('end;');
  13156. Add('class function ta.dosome: TObject;');
  13157. Add('begin');
  13158. Add(' Result:=create; // constructor');
  13159. Add(' Result:=createwithb(true); // constructor');
  13160. Add(' Result:=createwithc(''c''); // constructor');
  13161. Add('end;');
  13162. Add('begin');
  13163. ConvertProgram;
  13164. CheckSource('TestClass_CallInheritedConstructor',
  13165. LinesToStr([ // statements
  13166. 'rtl.createClass(this,"TObject",null,function(){',
  13167. ' this.$init = function () {',
  13168. ' };',
  13169. ' this.$final = function () {',
  13170. ' };',
  13171. ' this.Create = function () {',
  13172. ' return this;',
  13173. ' };',
  13174. ' this.CreateWithB = function (b) {',
  13175. ' this.Create();',
  13176. ' return this;',
  13177. ' };',
  13178. '});',
  13179. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13180. ' this.Create = function () {',
  13181. ' $mod.TObject.Create.call(this);',
  13182. ' $mod.TObject.Create.call(this);',
  13183. ' $mod.TObject.CreateWithB.call(this, false);',
  13184. ' return this;',
  13185. ' };',
  13186. ' this.CreateWithC = function (c) {',
  13187. ' $mod.TObject.Create.call(this);',
  13188. ' $mod.TObject.CreateWithB.call(this, true);',
  13189. ' this.DoIt();',
  13190. ' this.DoIt();',
  13191. ' this.$class.DoSome();',
  13192. ' return this;',
  13193. ' };',
  13194. ' this.DoIt = function () {',
  13195. ' this.Create();',
  13196. ' this.CreateWithB(false);',
  13197. ' this.CreateWithC("c");',
  13198. ' };',
  13199. ' this.DoSome = function () {',
  13200. ' var Result = null;',
  13201. ' Result = this.$create("Create");',
  13202. ' Result = this.$create("CreateWithB", [true]);',
  13203. ' Result = this.$create("CreateWithC", ["c"]);',
  13204. ' return Result;',
  13205. ' };',
  13206. '});'
  13207. ]),
  13208. LinesToStr([ // this.$main
  13209. ''
  13210. ]));
  13211. end;
  13212. procedure TTestModule.TestClass_ClassVar_Assign;
  13213. begin
  13214. StartProgram(false);
  13215. Add([
  13216. 'type',
  13217. ' TObject = class',
  13218. ' public',
  13219. ' class var vI: longint;',
  13220. ' class var Sub: TObject;',
  13221. ' constructor Create;',
  13222. ' class function GetIt(var Par: longint): tobject;',
  13223. ' end;',
  13224. 'constructor tobject.create;',
  13225. 'begin',
  13226. ' vi:=vi+1;',
  13227. ' Self.vi:=Self.vi+1;',
  13228. ' inc(vi);',
  13229. 'end;',
  13230. 'class function tobject.getit(var par: longint): tobject;',
  13231. 'begin',
  13232. ' vi:=vi+3;',
  13233. ' Self.vi:=Self.vi+4;',
  13234. ' inc(vi);',
  13235. ' Result:=self.sub;',
  13236. ' GetIt(vi);',
  13237. 'end;',
  13238. 'var Obj: tobject;',
  13239. 'begin',
  13240. ' obj:=tobject.create;',
  13241. ' tobject.vi:=3;',
  13242. ' if tobject.vi=4 then ;',
  13243. ' tobject.sub:=nil;',
  13244. ' obj.sub:=nil;',
  13245. ' obj.sub.sub:=nil;']);
  13246. ConvertProgram;
  13247. CheckSource('TestClass_ClassVar_Assign',
  13248. LinesToStr([ // statements
  13249. 'rtl.createClass(this,"TObject",null,function(){',
  13250. ' this.vI = 0;',
  13251. ' this.Sub = null;',
  13252. ' this.$init = function () {',
  13253. ' };',
  13254. ' this.$final = function () {',
  13255. ' };',
  13256. ' this.Create = function(){',
  13257. ' $mod.TObject.vI = this.vI+1;',
  13258. ' $mod.TObject.vI = this.vI+1;',
  13259. ' $mod.TObject.vI += 1;',
  13260. ' return this;',
  13261. ' };',
  13262. ' this.GetIt = function(Par){',
  13263. ' var Result = null;',
  13264. ' $mod.TObject.vI = this.vI + 3;',
  13265. ' $mod.TObject.vI = this.vI + 4;',
  13266. ' $mod.TObject.vI += 1;',
  13267. ' Result = this.Sub;',
  13268. ' this.GetIt({',
  13269. ' p: $mod.TObject,',
  13270. ' get: function () {',
  13271. ' return this.p.vI;',
  13272. ' },',
  13273. ' set: function (v) {',
  13274. ' this.p.vI = v;',
  13275. ' }',
  13276. ' });',
  13277. ' return Result;',
  13278. ' };',
  13279. '});',
  13280. 'this.Obj = null;'
  13281. ]),
  13282. LinesToStr([ // $mod.$main
  13283. '$mod.Obj = $mod.TObject.$create("Create");',
  13284. '$mod.TObject.vI = 3;',
  13285. 'if ($mod.TObject.vI === 4);',
  13286. '$mod.TObject.Sub=null;',
  13287. '$mod.TObject.Sub=null;',
  13288. '$mod.TObject.Sub=null;',
  13289. '']));
  13290. end;
  13291. procedure TTestModule.TestClass_CallClassMethod;
  13292. begin
  13293. StartProgram(false);
  13294. Add('type');
  13295. Add(' TObject = class');
  13296. Add(' public');
  13297. Add(' class var vI: longint;');
  13298. Add(' class var Sub: TObject;');
  13299. Add(' constructor Create;');
  13300. Add(' function GetMore(Par: longint): longint;');
  13301. Add(' class function GetIt(Par: longint): tobject;');
  13302. Add(' end;');
  13303. Add('constructor tobject.create;');
  13304. Add('begin');
  13305. Add(' sub:=getit(3);');
  13306. Add(' vi:=getmore(4);');
  13307. Add(' sub:=Self.getit(5);');
  13308. Add(' vi:=Self.getmore(6);');
  13309. Add('end;');
  13310. Add('function tobject.getmore(par: longint): longint;');
  13311. Add('begin');
  13312. Add(' sub:=getit(11);');
  13313. Add(' vi:=getmore(12);');
  13314. Add(' sub:=self.getit(13);');
  13315. Add(' vi:=self.getmore(14);');
  13316. Add('end;');
  13317. Add('class function tobject.getit(par: longint): tobject;');
  13318. Add('begin');
  13319. Add(' sub:=getit(21);');
  13320. Add(' vi:=sub.getmore(22);');
  13321. Add(' sub:=self.getit(23);');
  13322. Add(' vi:=self.sub.getmore(24);');
  13323. Add('end;');
  13324. Add('var Obj: tobject;');
  13325. Add('begin');
  13326. Add(' obj:=tobject.create;');
  13327. Add(' tobject.getit(5);');
  13328. Add(' obj.getit(6);');
  13329. Add(' obj.sub.getit(7);');
  13330. Add(' obj.sub.getit(8).SUB:=nil;');
  13331. Add(' obj.sub.getit(9).GETIT(10);');
  13332. Add(' obj.sub.getit(11).SuB.getit(12);');
  13333. ConvertProgram;
  13334. CheckSource('TestClass_CallClassMethod',
  13335. LinesToStr([ // statements
  13336. 'rtl.createClass(this,"TObject",null,function(){',
  13337. ' this.vI = 0;',
  13338. ' this.Sub = null;',
  13339. ' this.$init = function () {',
  13340. ' };',
  13341. ' this.$final = function () {',
  13342. ' };',
  13343. ' this.Create = function(){',
  13344. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13345. ' $mod.TObject.vI = this.GetMore(4);',
  13346. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13347. ' $mod.TObject.vI = this.GetMore(6);',
  13348. ' return this;',
  13349. ' };',
  13350. ' this.GetMore = function(Par){',
  13351. ' var Result = 0;',
  13352. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13353. ' $mod.TObject.vI = this.GetMore(12);',
  13354. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13355. ' $mod.TObject.vI = this.GetMore(14);',
  13356. ' return Result;',
  13357. ' };',
  13358. ' this.GetIt = function(Par){',
  13359. ' var Result = null;',
  13360. ' $mod.TObject.Sub = this.GetIt(21);',
  13361. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13362. ' $mod.TObject.Sub = this.GetIt(23);',
  13363. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13364. ' return Result;',
  13365. ' };',
  13366. '});',
  13367. 'this.Obj = null;'
  13368. ]),
  13369. LinesToStr([ // $mod.$main
  13370. '$mod.Obj = $mod.TObject.$create("Create");',
  13371. '$mod.TObject.GetIt(5);',
  13372. '$mod.Obj.$class.GetIt(6);',
  13373. '$mod.Obj.Sub.$class.GetIt(7);',
  13374. '$mod.TObject.Sub=null;',
  13375. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13376. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13377. '']));
  13378. end;
  13379. procedure TTestModule.TestClass_CallClassMethodStatic;
  13380. begin
  13381. StartProgram(false);
  13382. Add([
  13383. 'type',
  13384. ' TObject = class',
  13385. ' public',
  13386. ' class function Fly: tobject; static;',
  13387. ' end;',
  13388. 'class function tobject.Fly: tobject;',
  13389. 'begin',
  13390. ' Result.Fly;',
  13391. ' Result.Fly();',
  13392. ' Fly;',
  13393. ' Fly();',
  13394. ' Fly.Fly;',
  13395. ' Fly.Fly();',
  13396. 'end;',
  13397. 'var Obj: tobject;',
  13398. 'begin',
  13399. ' obj.Fly;',
  13400. ' obj.Fly();',
  13401. ' with obj do begin',
  13402. ' Fly;',
  13403. ' Fly();',
  13404. ' end;',
  13405. '']);
  13406. ConvertProgram;
  13407. CheckSource('TestClass_CallClassMethodStatic',
  13408. LinesToStr([ // statements
  13409. 'rtl.createClass(this, "TObject", null, function () {',
  13410. ' this.$init = function () {',
  13411. ' };',
  13412. ' this.$final = function () {',
  13413. ' };',
  13414. ' this.Fly = function () {',
  13415. ' var Result = null;',
  13416. ' $mod.TObject.Fly();',
  13417. ' $mod.TObject.Fly();',
  13418. ' $mod.TObject.Fly();',
  13419. ' $mod.TObject.Fly();',
  13420. ' $mod.TObject.Fly();',
  13421. ' $mod.TObject.Fly();',
  13422. ' return Result;',
  13423. ' };',
  13424. '});',
  13425. 'this.Obj = null;'
  13426. ]),
  13427. LinesToStr([ // $mod.$main
  13428. '$mod.TObject.Fly();',
  13429. '$mod.TObject.Fly();',
  13430. 'var $with = $mod.Obj;',
  13431. '$with.Fly();',
  13432. '$with.Fly();',
  13433. '']));
  13434. end;
  13435. procedure TTestModule.TestClass_Property;
  13436. begin
  13437. StartProgram(false);
  13438. Add('type');
  13439. Add(' TObject = class');
  13440. Add(' Fx: longint;');
  13441. Add(' Fy: longint;');
  13442. Add(' function GetInt: longint;');
  13443. Add(' procedure SetInt(Value: longint);');
  13444. Add(' procedure DoIt;');
  13445. Add(' property IntA: longint read Fx write Fy;');
  13446. Add(' property IntB: longint read GetInt write SetInt;');
  13447. Add(' end;');
  13448. Add('function tobject.getint: longint;');
  13449. Add('begin');
  13450. Add(' result:=fx;');
  13451. Add('end;');
  13452. Add('procedure tobject.setint(value: longint);');
  13453. Add('begin');
  13454. Add(' if value=fy then exit;');
  13455. Add(' fy:=value;');
  13456. Add('end;');
  13457. Add('procedure tobject.doit;');
  13458. Add('begin');
  13459. Add(' IntA:=IntA+1;');
  13460. Add(' Self.IntA:=Self.IntA+1;');
  13461. Add(' IntB:=IntB+1;');
  13462. Add(' Self.IntB:=Self.IntB+1;');
  13463. Add('end;');
  13464. Add('var Obj: tobject;');
  13465. Add('begin');
  13466. Add(' obj.inta:=obj.inta+1;');
  13467. Add(' if obj.intb=2 then;');
  13468. Add(' obj.intb:=obj.intb+2;');
  13469. Add(' obj.setint(obj.inta);');
  13470. ConvertProgram;
  13471. CheckSource('TestClass_Property',
  13472. LinesToStr([ // statements
  13473. 'rtl.createClass(this, "TObject", null, function () {',
  13474. ' this.$init = function () {',
  13475. ' this.Fx = 0;',
  13476. ' this.Fy = 0;',
  13477. ' };',
  13478. ' this.$final = function () {',
  13479. ' };',
  13480. ' this.GetInt = function () {',
  13481. ' var Result = 0;',
  13482. ' Result = this.Fx;',
  13483. ' return Result;',
  13484. ' };',
  13485. ' this.SetInt = function (Value) {',
  13486. ' if (Value === this.Fy) return;',
  13487. ' this.Fy = Value;',
  13488. ' };',
  13489. ' this.DoIt = function () {',
  13490. ' this.Fy = this.Fx + 1;',
  13491. ' this.Fy = this.Fx + 1;',
  13492. ' this.SetInt(this.GetInt() + 1);',
  13493. ' this.SetInt(this.GetInt() + 1);',
  13494. ' };',
  13495. '});',
  13496. 'this.Obj = null;'
  13497. ]),
  13498. LinesToStr([ // $mod.$main
  13499. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13500. 'if ($mod.Obj.GetInt() === 2);',
  13501. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13502. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13503. ]));
  13504. end;
  13505. procedure TTestModule.TestClass_Property_ClassMethod;
  13506. begin
  13507. StartProgram(false);
  13508. Add([
  13509. 'type',
  13510. ' TObject = class',
  13511. ' class var Fx: longint;',
  13512. ' class var Fy: longint;',
  13513. ' class function GetInt: longint;',
  13514. ' class procedure SetInt(Value: longint);',
  13515. ' end;',
  13516. ' TBird = class',
  13517. ' class procedure DoIt;',
  13518. ' class property IntA: longint read Fx write Fy;',
  13519. ' class property IntB: longint read GetInt write SetInt;',
  13520. ' end;',
  13521. 'class function tobject.getint: longint;',
  13522. 'begin',
  13523. ' result:=fx;',
  13524. 'end;',
  13525. 'class procedure tobject.setint(value: longint);',
  13526. 'begin',
  13527. 'end;',
  13528. 'class procedure tbird.doit;',
  13529. 'begin',
  13530. ' FX:=3;',
  13531. ' IntA:=IntA+1;',
  13532. ' Self.IntA:=Self.IntA+1;',
  13533. ' IntB:=IntB+1;',
  13534. ' Self.IntB:=Self.IntB+1;',
  13535. ' with Self do begin',
  13536. ' FX:=11;',
  13537. ' IntA:=IntA+12;',
  13538. ' IntB:=IntB+13;',
  13539. ' end;',
  13540. 'end;',
  13541. 'var Obj: tbird;',
  13542. 'begin',
  13543. ' tbird.fx:=tbird.fx+1;',
  13544. ' tbird.inta:=tbird.inta+1;',
  13545. ' if tbird.intb=2 then;',
  13546. ' tbird.intb:=tbird.intb+2;',
  13547. ' tbird.setint(tbird.inta);',
  13548. ' obj.inta:=obj.inta+1;',
  13549. ' if obj.intb=2 then;',
  13550. ' obj.intb:=obj.intb+2;',
  13551. ' obj.setint(obj.inta);',
  13552. ' with Tbird do begin',
  13553. ' FX:=FY+1;',
  13554. ' inta:=inta+2;',
  13555. ' intb:=intb+3;',
  13556. ' end;',
  13557. ' with Obj do begin',
  13558. ' FX:=FY+1;',
  13559. ' inta:=inta+2;',
  13560. ' intb:=intb+3;',
  13561. ' end;',
  13562. '']);
  13563. ConvertProgram;
  13564. CheckSource('TestClass_Property_ClassMethod',
  13565. LinesToStr([ // statements
  13566. 'rtl.createClass(this, "TObject", null, function () {',
  13567. ' this.Fx = 0;',
  13568. ' this.Fy = 0;',
  13569. ' this.$init = function () {',
  13570. ' };',
  13571. ' this.$final = function () {',
  13572. ' };',
  13573. ' this.GetInt = function () {',
  13574. ' var Result = 0;',
  13575. ' Result = this.Fx;',
  13576. ' return Result;',
  13577. ' };',
  13578. ' this.SetInt = function (Value) {',
  13579. ' };',
  13580. '});',
  13581. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13582. ' this.DoIt = function () {',
  13583. ' $mod.TObject.Fx = 3;',
  13584. ' $mod.TObject.Fy = this.Fx + 1;',
  13585. ' $mod.TObject.Fy = this.Fx + 1;',
  13586. ' this.SetInt(this.GetInt() + 1);',
  13587. ' this.SetInt(this.GetInt() + 1);',
  13588. ' $mod.TObject.Fx = 11;',
  13589. ' $mod.TObject.Fy = this.Fx + 12;',
  13590. ' this.SetInt(this.GetInt() + 13);',
  13591. ' };',
  13592. '});',
  13593. 'this.Obj = null;'
  13594. ]),
  13595. LinesToStr([ // $mod.$main
  13596. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13597. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13598. 'if ($mod.TBird.GetInt() === 2);',
  13599. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13600. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13601. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13602. 'if ($mod.Obj.$class.GetInt() === 2);',
  13603. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13604. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13605. 'var $with = $mod.TBird;',
  13606. '$mod.TObject.Fx = $with.Fy + 1;',
  13607. '$mod.TObject.Fy = $with.Fx + 2;',
  13608. '$with.SetInt($with.GetInt() + 3);',
  13609. 'var $with1 = $mod.Obj;',
  13610. '$mod.TObject.Fx = $with1.Fy + 1;',
  13611. '$mod.TObject.Fy = $with1.Fx + 2;',
  13612. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13613. '']));
  13614. end;
  13615. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  13616. begin
  13617. StartProgram(false);
  13618. Add([
  13619. 'type',
  13620. ' TObject = class',
  13621. ' class function GetInt: longint; static;',
  13622. ' class procedure SetInt(Value: longint); static;',
  13623. ' class function GetItems(Index: word): longint; static;',
  13624. ' class procedure SetItems(Index: word; const Value: longint); static;',
  13625. ' end;',
  13626. ' TBird = class',
  13627. ' class procedure Fly;',
  13628. ' class property IntA: longint read GetInt write SetInt;',
  13629. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  13630. ' end;',
  13631. 'class function tobject.getint: longint;',
  13632. 'begin',
  13633. 'end;',
  13634. 'class procedure tobject.setint(value: longint);',
  13635. 'begin',
  13636. 'end;',
  13637. 'class function tobject.GetItems(Index: word): longint;',
  13638. 'begin',
  13639. 'end;',
  13640. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  13641. 'begin',
  13642. 'end;',
  13643. 'class procedure tbird.fly;',
  13644. 'var w: longint;',
  13645. 'begin',
  13646. ' inta:=inta+51;',
  13647. ' w:=items[52];',
  13648. ' items[53]:=54;',
  13649. 'end;',
  13650. 'var Obj: tbird;',
  13651. ' i: longint;',
  13652. 'begin',
  13653. ' tbird.inta:=tbird.inta+1;',
  13654. ' i:=tbird.items[2];',
  13655. ' tbird.items[3]:=4;',
  13656. ' obj.inta:=obj.inta+11;',
  13657. ' i:=obj.items[12];',
  13658. ' obj.items[13]:=14;',
  13659. ' with Tbird do begin',
  13660. ' inta:=inta+21;',
  13661. ' i:=items[22];',
  13662. ' items[23]:=24;',
  13663. ' end;',
  13664. ' with Obj do begin',
  13665. ' inta:=inta+31;',
  13666. ' i:=items[32];',
  13667. ' items[33]:=34;',
  13668. ' end;',
  13669. '']);
  13670. ConvertProgram;
  13671. CheckSource('TestClass_Property_ClassMethod',
  13672. LinesToStr([ // statements
  13673. 'rtl.createClass(this, "TObject", null, function () {',
  13674. ' this.$init = function () {',
  13675. ' };',
  13676. ' this.$final = function () {',
  13677. ' };',
  13678. ' this.GetInt = function () {',
  13679. ' var Result = 0;',
  13680. ' return Result;',
  13681. ' };',
  13682. ' this.SetInt = function (Value) {',
  13683. ' };',
  13684. ' this.GetItems = function (Index) {',
  13685. ' var Result = 0;',
  13686. ' return Result;',
  13687. ' };',
  13688. ' this.SetItems = function (Index, Value) {',
  13689. ' };',
  13690. '});',
  13691. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13692. ' this.Fly = function () {',
  13693. ' var w = 0;',
  13694. ' this.SetInt(this.GetInt() + 51);',
  13695. ' w = this.GetItems(52);',
  13696. ' this.SetItems(53, 54);',
  13697. ' };',
  13698. '});',
  13699. 'this.Obj = null;',
  13700. 'this.i = 0;',
  13701. '']),
  13702. LinesToStr([ // $mod.$main
  13703. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  13704. '$mod.i = $mod.TObject.GetItems(2);',
  13705. '$mod.TObject.SetItems(3, 4);',
  13706. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  13707. '$mod.i = $mod.TObject.GetItems(12);',
  13708. '$mod.TObject.SetItems(13, 14);',
  13709. 'var $with = $mod.TBird;',
  13710. '$with.SetInt($with.GetInt() + 21);',
  13711. '$mod.i = $with.GetItems(22);',
  13712. '$with.SetItems(23, 24);',
  13713. 'var $with1 = $mod.Obj;',
  13714. '$with1.SetInt($with1.GetInt() + 31);',
  13715. '$mod.i = $with1.GetItems(32);',
  13716. '$with1.SetItems(33, 34);',
  13717. '']));
  13718. end;
  13719. procedure TTestModule.TestClass_Property_Indexed;
  13720. begin
  13721. StartProgram(false);
  13722. Add([
  13723. 'type',
  13724. ' TObject = class',
  13725. ' FItems: array of longint;',
  13726. ' function GetItems(Index: longint): longint;',
  13727. ' procedure SetItems(Index: longint; Value: longint);',
  13728. ' procedure DoIt;',
  13729. ' property Items[Index: longint]: longint read getitems write setitems;',
  13730. ' end;',
  13731. 'function tobject.getitems(index: longint): longint;',
  13732. 'begin',
  13733. ' Result:=fitems[index];',
  13734. 'end;',
  13735. 'procedure tobject.setitems(index: longint; value: longint);',
  13736. 'begin',
  13737. ' fitems[index]:=value;',
  13738. 'end;',
  13739. 'procedure tobject.doit;',
  13740. 'begin',
  13741. ' items[1]:=2;',
  13742. ' items[3]:=items[4];',
  13743. ' self.items[5]:=self.items[6];',
  13744. ' items[items[7]]:=items[items[8]];',
  13745. 'end;',
  13746. 'var Obj: tobject;',
  13747. 'begin',
  13748. ' obj.Items[11]:=obj.Items[12];',
  13749. '']);
  13750. ConvertProgram;
  13751. CheckSource('TestClass_Property_Indexed',
  13752. LinesToStr([ // statements
  13753. 'rtl.createClass(this, "TObject", null, function () {',
  13754. ' this.$init = function () {',
  13755. ' this.FItems = [];',
  13756. ' };',
  13757. ' this.$final = function () {',
  13758. ' this.FItems = undefined;',
  13759. ' };',
  13760. ' this.GetItems = function (Index) {',
  13761. ' var Result = 0;',
  13762. ' Result = this.FItems[Index];',
  13763. ' return Result;',
  13764. ' };',
  13765. ' this.SetItems = function (Index, Value) {',
  13766. ' this.FItems[Index] = Value;',
  13767. ' };',
  13768. ' this.DoIt = function () {',
  13769. ' this.SetItems(1, 2);',
  13770. ' this.SetItems(3,this.GetItems(4));',
  13771. ' this.SetItems(5,this.GetItems(6));',
  13772. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13773. ' };',
  13774. '});',
  13775. 'this.Obj = null;'
  13776. ]),
  13777. LinesToStr([ // $mod.$main
  13778. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13779. ]));
  13780. end;
  13781. procedure TTestModule.TestClass_Property_IndexSpec;
  13782. begin
  13783. StartProgram(false);
  13784. Add([
  13785. 'type',
  13786. ' TEnum = (red, blue);',
  13787. ' TObject = class',
  13788. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13789. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13790. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13791. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13792. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13793. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13794. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13795. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13796. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13797. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13798. ' end;',
  13799. 'procedure DoIt(b: boolean); begin end;',
  13800. 'var',
  13801. ' o: TObject;',
  13802. 'begin',
  13803. ' o.B1:=o.B1;',
  13804. ' o.B2:=o.B2;',
  13805. ' o.B3:=o.B3;',
  13806. ' o.I1[''a'']:=o.I1[''b''];',
  13807. ' doit(o.b1);',
  13808. ' doit(o.b2);',
  13809. ' doit(o.i1[''c'']);',
  13810. '']);
  13811. ConvertProgram;
  13812. CheckSource('TestClass_Property_IndexSpec',
  13813. LinesToStr([ // statements
  13814. 'this.TEnum = {',
  13815. ' "0": "red",',
  13816. ' red: 0,',
  13817. ' "1": "blue",',
  13818. ' blue: 1',
  13819. '};',
  13820. 'rtl.createClass(this, "TObject", null, function () {',
  13821. ' this.$init = function () {',
  13822. ' };',
  13823. ' this.$final = function () {',
  13824. ' };',
  13825. '});',
  13826. 'this.DoIt = function (b) {',
  13827. '};',
  13828. 'this.o = null;',
  13829. '']),
  13830. LinesToStr([ // $mod.$main
  13831. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13832. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13833. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13834. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13835. '$mod.DoIt($mod.o.GetIntBool(1));',
  13836. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13837. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13838. '']));
  13839. end;
  13840. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13841. begin
  13842. StartProgram(false);
  13843. Add('type');
  13844. Add(' TArray = array of longint;');
  13845. Add(' TObject = class');
  13846. Add(' FItems: TArray;');
  13847. Add(' function GetItems: tarray;');
  13848. Add(' procedure SetItems(Value: tarray);');
  13849. Add(' property Items: tarray read getitems write setitems;');
  13850. Add(' procedure SetNumbers(const Value: tarray);');
  13851. Add(' property Numbers: tarray write setnumbers;');
  13852. Add(' end;');
  13853. Add('function tobject.getitems: tarray;');
  13854. Add('begin');
  13855. Add(' Result:=fitems;');
  13856. Add('end;');
  13857. Add('procedure tobject.setitems(value: tarray);');
  13858. Add('begin');
  13859. Add(' fitems:=value;');
  13860. Add(' fitems:=nil;');
  13861. Add(' Items:=nil;');
  13862. Add(' Items:=Items;');
  13863. Add(' Items[1]:=2;');
  13864. Add(' fitems[3]:=Items[4];');
  13865. Add(' Items[5]:=Items[6];');
  13866. Add(' Self.Items[7]:=8;');
  13867. Add(' Self.Items[9]:=Self.Items[10];');
  13868. Add(' Items[Items[11]]:=Items[Items[12]];');
  13869. Add('end;');
  13870. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13871. Add('begin;');
  13872. Add(' Numbers:=nil;');
  13873. Add(' Numbers:=Value;');
  13874. Add(' Self.Numbers:=Value;');
  13875. Add('end;');
  13876. Add('var Obj: tobject;');
  13877. Add('begin');
  13878. Add(' obj.items:=nil;');
  13879. Add(' obj.items:=obj.items;');
  13880. Add(' obj.items[11]:=obj.items[12];');
  13881. ConvertProgram;
  13882. CheckSource('TestClass_PropertyOfTypeArray',
  13883. LinesToStr([ // statements
  13884. 'rtl.createClass(this, "TObject", null, function () {',
  13885. ' this.$init = function () {',
  13886. ' this.FItems = [];',
  13887. ' };',
  13888. ' this.$final = function () {',
  13889. ' this.FItems = undefined;',
  13890. ' };',
  13891. ' this.GetItems = function () {',
  13892. ' var Result = [];',
  13893. ' Result = rtl.arrayRef(this.FItems);',
  13894. ' return Result;',
  13895. ' };',
  13896. ' this.SetItems = function (Value) {',
  13897. ' this.FItems = rtl.arrayRef(Value);',
  13898. ' this.FItems = [];',
  13899. ' this.SetItems([]);',
  13900. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13901. ' this.GetItems()[1] = 2;',
  13902. ' this.FItems[3] = this.GetItems()[4];',
  13903. ' this.GetItems()[5] = this.GetItems()[6];',
  13904. ' this.GetItems()[7] = 8;',
  13905. ' this.GetItems()[9] = this.GetItems()[10];',
  13906. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13907. ' };',
  13908. ' this.SetNumbers = function (Value) {',
  13909. ' this.SetNumbers([]);',
  13910. ' this.SetNumbers(Value);',
  13911. ' this.SetNumbers(Value);',
  13912. ' };',
  13913. '});',
  13914. 'this.Obj = null;'
  13915. ]),
  13916. LinesToStr([ // $mod.$main
  13917. '$mod.Obj.SetItems([]);',
  13918. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13919. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13920. ]));
  13921. end;
  13922. procedure TTestModule.TestClass_PropertyDefault;
  13923. begin
  13924. StartProgram(false);
  13925. Add([
  13926. 'type',
  13927. ' TArray = array of longint;',
  13928. ' TObject = class',
  13929. ' end;',
  13930. ' TBird = class',
  13931. ' FItems: TArray;',
  13932. ' function GetItems(Index: longint): longint;',
  13933. ' procedure SetItems(Index, Value: longint);',
  13934. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13935. ' end;',
  13936. 'function TBird.getitems(index: longint): longint;',
  13937. 'begin',
  13938. 'end;',
  13939. 'procedure TBird.setitems(index, value: longint);',
  13940. 'begin',
  13941. ' Self[1]:=2;',
  13942. ' Self[3]:=Self[index];',
  13943. ' Self[index]:=Self[Self[value]];',
  13944. ' Self[Self[4]]:=value;',
  13945. 'end;',
  13946. 'var',
  13947. ' Bird: TBird;',
  13948. ' Obj: TObject;',
  13949. 'begin',
  13950. ' bird[11]:=12;',
  13951. ' bird[13]:=bird[14];',
  13952. ' bird[Bird[15]]:=bird[Bird[15]];',
  13953. ' TBird(obj)[16]:=TBird(obj)[17];',
  13954. ' (obj as tbird)[18]:=19;',
  13955. '']);
  13956. ConvertProgram;
  13957. CheckSource('TestClass_PropertyDefault',
  13958. LinesToStr([ // statements
  13959. 'rtl.createClass(this, "TObject", null, function () {',
  13960. ' this.$init = function () {',
  13961. ' };',
  13962. ' this.$final = function () {',
  13963. ' };',
  13964. '});',
  13965. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13966. ' this.$init = function () {',
  13967. ' $mod.TObject.$init.call(this);',
  13968. ' this.FItems = [];',
  13969. ' };',
  13970. ' this.$final = function () {',
  13971. ' this.FItems = undefined;',
  13972. ' $mod.TObject.$final.call(this);',
  13973. ' };',
  13974. ' this.GetItems = function (Index) {',
  13975. ' var Result = 0;',
  13976. ' return Result;',
  13977. ' };',
  13978. ' this.SetItems = function (Index, Value) {',
  13979. ' this.SetItems(1, 2);',
  13980. ' this.SetItems(3, this.GetItems(Index));',
  13981. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13982. ' this.SetItems(this.GetItems(4), Value);',
  13983. ' };',
  13984. '});',
  13985. 'this.Bird = null;',
  13986. 'this.Obj = null;',
  13987. '']),
  13988. LinesToStr([ // $mod.$main
  13989. '$mod.Bird.SetItems(11, 12);',
  13990. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13991. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13992. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13993. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13994. '']));
  13995. end;
  13996. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13997. begin
  13998. StartProgram(false);
  13999. Add([
  14000. 'type',
  14001. ' TObject = class end;',
  14002. ' TAlphaList = class',
  14003. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  14004. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  14005. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  14006. ' end;',
  14007. ' TBetaList = class',
  14008. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  14009. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  14010. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  14011. ' end;',
  14012. ' TBird = class',
  14013. ' procedure DoIt;',
  14014. ' end;',
  14015. 'procedure TBird.DoIt;',
  14016. 'var',
  14017. ' List: TAlphaList;',
  14018. 'begin',
  14019. ' if TBetaList(List[true])[3]=nil then ;',
  14020. ' TBetaList(List[false])[5]:=nil;',
  14021. 'end;',
  14022. 'var',
  14023. ' List: TAlphaList;',
  14024. 'begin',
  14025. ' if TBetaList(List[true])[3]=nil then ;',
  14026. ' TBetaList(List[false])[5]:=nil;',
  14027. '']);
  14028. ConvertProgram;
  14029. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  14030. LinesToStr([ // statements
  14031. 'rtl.createClass(this, "TObject", null, function () {',
  14032. ' this.$init = function () {',
  14033. ' };',
  14034. ' this.$final = function () {',
  14035. ' };',
  14036. '});',
  14037. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  14038. '});',
  14039. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  14040. '});',
  14041. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14042. ' this.DoIt = function () {',
  14043. ' var List = null;',
  14044. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  14045. ' List.GetAlphas(false).SetBetas(5, null);',
  14046. ' };',
  14047. '});',
  14048. 'this.List = null;',
  14049. '']),
  14050. LinesToStr([ // $mod.$main
  14051. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  14052. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  14053. '']));
  14054. end;
  14055. procedure TTestModule.TestClass_PropertyOverride;
  14056. begin
  14057. StartProgram(false);
  14058. Add('type');
  14059. Add(' integer = longint;');
  14060. Add(' TObject = class');
  14061. Add(' FItem: integer;');
  14062. Add(' function GetItem: integer; external name ''GetItem'';');
  14063. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  14064. Add(' property Item: integer read getitem write setitem;');
  14065. Add(' end;');
  14066. Add(' TCar = class');
  14067. Add(' FBag: integer;');
  14068. Add(' function GetBag: integer; external name ''GetBag'';');
  14069. Add(' property Item read getbag;');
  14070. Add(' end;');
  14071. Add('var');
  14072. Add(' Obj: tobject;');
  14073. Add(' Car: tcar;');
  14074. Add('begin');
  14075. Add(' Obj.Item:=Obj.Item;');
  14076. Add(' Car.Item:=Car.Item;');
  14077. ConvertProgram;
  14078. CheckSource('TestClass_PropertyOverride',
  14079. LinesToStr([ // statements
  14080. 'rtl.createClass(this, "TObject", null, function () {',
  14081. ' this.$init = function () {',
  14082. ' this.FItem = 0;',
  14083. ' };',
  14084. ' this.$final = function () {',
  14085. ' };',
  14086. '});',
  14087. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14088. ' this.$init = function () {',
  14089. ' $mod.TObject.$init.call(this);',
  14090. ' this.FBag = 0;',
  14091. ' };',
  14092. '});',
  14093. 'this.Obj = null;',
  14094. 'this.Car = null;',
  14095. '']),
  14096. LinesToStr([ // $mod.$main
  14097. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  14098. '$mod.Car.SetItem($mod.Car.GetBag());',
  14099. '']));
  14100. end;
  14101. procedure TTestModule.TestClass_PropertyIncVisibility;
  14102. begin
  14103. AddModuleWithIntfImplSrc('unit1.pp',
  14104. LinesToStr([
  14105. 'type',
  14106. ' TNumber = longint;',
  14107. ' TInteger = longint;',
  14108. ' TObject = class',
  14109. ' private',
  14110. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  14111. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  14112. ' protected',
  14113. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  14114. ' end;']),
  14115. LinesToStr([
  14116. '']));
  14117. StartProgram(true);
  14118. Add([
  14119. 'uses unit1;',
  14120. 'type',
  14121. ' TBird = class',
  14122. ' public',
  14123. ' property Items;',
  14124. ' end;',
  14125. 'procedure DoIt(i: TInteger);',
  14126. 'begin',
  14127. 'end;',
  14128. 'var b: TBird;',
  14129. 'begin',
  14130. ' b.Items[1]:=2;',
  14131. ' b.Items[3]:=b.Items[4];',
  14132. ' DoIt(b.Items[5]);',
  14133. '']);
  14134. ConvertProgram;
  14135. CheckSource('TestClass_PropertyIncVisibility',
  14136. LinesToStr([ // statements
  14137. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  14138. '});',
  14139. 'this.DoIt = function (i) {',
  14140. '};',
  14141. 'this.b = null;'
  14142. ]),
  14143. LinesToStr([ // $mod.$main
  14144. '$mod.b.SetItems(1, 2);',
  14145. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  14146. '$mod.DoIt($mod.b.GetItems(5));'
  14147. ]));
  14148. end;
  14149. procedure TTestModule.TestClass_Assigned;
  14150. begin
  14151. StartProgram(false);
  14152. Add('type');
  14153. Add(' TObject = class');
  14154. Add(' end;');
  14155. Add('var');
  14156. Add(' Obj: tobject;');
  14157. Add(' b: boolean;');
  14158. Add('begin');
  14159. Add(' if Assigned(obj) then ;');
  14160. Add(' b:=Assigned(obj) or false;');
  14161. ConvertProgram;
  14162. CheckSource('TestClass_Assigned',
  14163. LinesToStr([ // statements
  14164. 'rtl.createClass(this, "TObject", null, function () {',
  14165. ' this.$init = function () {',
  14166. ' };',
  14167. ' this.$final = function () {',
  14168. ' };',
  14169. '});',
  14170. 'this.Obj = null;',
  14171. 'this.b = false;'
  14172. ]),
  14173. LinesToStr([ // $mod.$main
  14174. 'if ($mod.Obj != null);',
  14175. '$mod.b = ($mod.Obj != null) || false;'
  14176. ]));
  14177. end;
  14178. procedure TTestModule.TestClass_WithClassDoCreate;
  14179. begin
  14180. StartProgram(false);
  14181. Add('type');
  14182. Add(' TObject = class');
  14183. Add(' aBool: boolean;');
  14184. Add(' Arr: array of boolean;');
  14185. Add(' constructor Create;');
  14186. Add(' end;');
  14187. Add('constructor TObject.Create; begin end;');
  14188. Add('var');
  14189. Add(' Obj: tobject;');
  14190. Add(' b: boolean;');
  14191. Add('begin');
  14192. Add(' with tobject.create do begin');
  14193. Add(' b:=abool;');
  14194. Add(' abool:=b;');
  14195. Add(' b:=arr[1];');
  14196. Add(' arr[2]:=b;');
  14197. Add(' end;');
  14198. Add(' with tobject do');
  14199. Add(' obj:=create;');
  14200. Add(' with obj do begin');
  14201. Add(' create;');
  14202. Add(' b:=abool;');
  14203. Add(' abool:=b;');
  14204. Add(' b:=arr[3];');
  14205. Add(' arr[4]:=b;');
  14206. Add(' end;');
  14207. ConvertProgram;
  14208. CheckSource('TestClass_WithClassDoCreate',
  14209. LinesToStr([ // statements
  14210. 'rtl.createClass(this, "TObject", null, function () {',
  14211. ' this.$init = function () {',
  14212. ' this.aBool = false;',
  14213. ' this.Arr = [];',
  14214. ' };',
  14215. ' this.$final = function () {',
  14216. ' this.Arr = undefined;',
  14217. ' };',
  14218. ' this.Create = function () {',
  14219. ' return this;',
  14220. ' };',
  14221. '});',
  14222. 'this.Obj = null;',
  14223. 'this.b = false;'
  14224. ]),
  14225. LinesToStr([ // $mod.$main
  14226. 'var $with = $mod.TObject.$create("Create");',
  14227. '$mod.b = $with.aBool;',
  14228. '$with.aBool = $mod.b;',
  14229. '$mod.b = $with.Arr[1];',
  14230. '$with.Arr[2] = $mod.b;',
  14231. 'var $with1 = $mod.TObject;',
  14232. '$mod.Obj = $with1.$create("Create");',
  14233. 'var $with2 = $mod.Obj;',
  14234. '$with2.Create();',
  14235. '$mod.b = $with2.aBool;',
  14236. '$with2.aBool = $mod.b;',
  14237. '$mod.b = $with2.Arr[3];',
  14238. '$with2.Arr[4] = $mod.b;',
  14239. '']));
  14240. end;
  14241. procedure TTestModule.TestClass_WithClassInstDoProperty;
  14242. begin
  14243. StartProgram(false);
  14244. Add('type');
  14245. Add(' TObject = class');
  14246. Add(' FInt: longint;');
  14247. Add(' constructor Create;');
  14248. Add(' function GetSize: longint;');
  14249. Add(' procedure SetSize(Value: longint);');
  14250. Add(' property Int: longint read FInt write FInt;');
  14251. Add(' property Size: longint read GetSize write SetSize;');
  14252. Add(' end;');
  14253. Add('constructor TObject.Create; begin end;');
  14254. Add('function TObject.GetSize: longint; begin; end;');
  14255. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14256. Add('var');
  14257. Add(' Obj: tobject;');
  14258. Add(' i: longint;');
  14259. Add('begin');
  14260. Add(' with TObject.Create do begin');
  14261. Add(' i:=int;');
  14262. Add(' int:=i;');
  14263. Add(' i:=size;');
  14264. Add(' size:=i;');
  14265. Add(' end;');
  14266. Add(' with obj do begin');
  14267. Add(' i:=int;');
  14268. Add(' int:=i;');
  14269. Add(' i:=size;');
  14270. Add(' size:=i;');
  14271. Add(' end;');
  14272. ConvertProgram;
  14273. CheckSource('TestClass_WithClassInstDoProperty',
  14274. LinesToStr([ // statements
  14275. 'rtl.createClass(this, "TObject", null, function () {',
  14276. ' this.$init = function () {',
  14277. ' this.FInt = 0;',
  14278. ' };',
  14279. ' this.$final = function () {',
  14280. ' };',
  14281. ' this.Create = function () {',
  14282. ' return this;',
  14283. ' };',
  14284. ' this.GetSize = function () {',
  14285. ' var Result = 0;',
  14286. ' return Result;',
  14287. ' };',
  14288. ' this.SetSize = function (Value) {',
  14289. ' };',
  14290. '});',
  14291. 'this.Obj = null;',
  14292. 'this.i = 0;'
  14293. ]),
  14294. LinesToStr([ // $mod.$main
  14295. 'var $with = $mod.TObject.$create("Create");',
  14296. '$mod.i = $with.FInt;',
  14297. '$with.FInt = $mod.i;',
  14298. '$mod.i = $with.GetSize();',
  14299. '$with.SetSize($mod.i);',
  14300. 'var $with1 = $mod.Obj;',
  14301. '$mod.i = $with1.FInt;',
  14302. '$with1.FInt = $mod.i;',
  14303. '$mod.i = $with1.GetSize();',
  14304. '$with1.SetSize($mod.i);',
  14305. '']));
  14306. end;
  14307. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  14308. begin
  14309. StartProgram(false);
  14310. Add('type');
  14311. Add(' TObject = class');
  14312. Add(' constructor Create;');
  14313. Add(' function GetItems(Index: longint): longint;');
  14314. Add(' procedure SetItems(Index, Value: longint);');
  14315. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  14316. Add(' end;');
  14317. Add('constructor TObject.Create; begin end;');
  14318. Add('function tobject.getitems(index: longint): longint; begin; end;');
  14319. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  14320. Add('var');
  14321. Add(' Obj: tobject;');
  14322. Add(' i: longint;');
  14323. Add('begin');
  14324. Add(' with TObject.Create do begin');
  14325. Add(' i:=Items[1];');
  14326. Add(' Items[2]:=i;');
  14327. Add(' end;');
  14328. Add(' with obj do begin');
  14329. Add(' i:=Items[3];');
  14330. Add(' Items[4]:=i;');
  14331. Add(' end;');
  14332. ConvertProgram;
  14333. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  14334. LinesToStr([ // statements
  14335. 'rtl.createClass(this, "TObject", null, function () {',
  14336. ' this.$init = function () {',
  14337. ' };',
  14338. ' this.$final = function () {',
  14339. ' };',
  14340. ' this.Create = function () {',
  14341. ' return this;',
  14342. ' };',
  14343. ' this.GetItems = function (Index) {',
  14344. ' var Result = 0;',
  14345. ' return Result;',
  14346. ' };',
  14347. ' this.SetItems = function (Index, Value) {',
  14348. ' };',
  14349. '});',
  14350. 'this.Obj = null;',
  14351. 'this.i = 0;'
  14352. ]),
  14353. LinesToStr([ // $mod.$main
  14354. 'var $with = $mod.TObject.$create("Create");',
  14355. '$mod.i = $with.GetItems(1);',
  14356. '$with.SetItems(2, $mod.i);',
  14357. 'var $with1 = $mod.Obj;',
  14358. '$mod.i = $with1.GetItems(3);',
  14359. '$with1.SetItems(4, $mod.i);',
  14360. '']));
  14361. end;
  14362. procedure TTestModule.TestClass_WithClassInstDoFunc;
  14363. begin
  14364. StartProgram(false);
  14365. Add('type');
  14366. Add(' TObject = class');
  14367. Add(' constructor Create;');
  14368. Add(' function GetSize: longint;');
  14369. Add(' procedure SetSize(Value: longint);');
  14370. Add(' end;');
  14371. Add('constructor TObject.Create; begin end;');
  14372. Add('function TObject.GetSize: longint; begin; end;');
  14373. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14374. Add('var');
  14375. Add(' Obj: tobject;');
  14376. Add(' i: longint;');
  14377. Add('begin');
  14378. Add(' with TObject.Create do begin');
  14379. Add(' i:=GetSize;');
  14380. Add(' i:=GetSize();');
  14381. Add(' SetSize(i);');
  14382. Add(' end;');
  14383. Add(' with obj do begin');
  14384. Add(' i:=GetSize;');
  14385. Add(' i:=GetSize();');
  14386. Add(' SetSize(i);');
  14387. Add(' end;');
  14388. ConvertProgram;
  14389. CheckSource('TestClass_WithClassInstDoFunc',
  14390. LinesToStr([ // statements
  14391. 'rtl.createClass(this, "TObject", null, function () {',
  14392. ' this.$init = function () {',
  14393. ' };',
  14394. ' this.$final = function () {',
  14395. ' };',
  14396. ' this.Create = function () {',
  14397. ' return this;',
  14398. ' };',
  14399. ' this.GetSize = function () {',
  14400. ' var Result = 0;',
  14401. ' return Result;',
  14402. ' };',
  14403. ' this.SetSize = function (Value) {',
  14404. ' };',
  14405. '});',
  14406. 'this.Obj = null;',
  14407. 'this.i = 0;'
  14408. ]),
  14409. LinesToStr([ // $mod.$main
  14410. 'var $with = $mod.TObject.$create("Create");',
  14411. '$mod.i = $with.GetSize();',
  14412. '$mod.i = $with.GetSize();',
  14413. '$with.SetSize($mod.i);',
  14414. 'var $with1 = $mod.Obj;',
  14415. '$mod.i = $with1.GetSize();',
  14416. '$mod.i = $with1.GetSize();',
  14417. '$with1.SetSize($mod.i);',
  14418. '']));
  14419. end;
  14420. procedure TTestModule.TestClass_TypeCast;
  14421. begin
  14422. StartProgram(false);
  14423. Add('type');
  14424. Add(' TObject = class');
  14425. Add(' Next: TObject;');
  14426. Add(' constructor Create;');
  14427. Add(' end;');
  14428. Add(' TControl = class(TObject)');
  14429. Add(' Arr: array of TObject;');
  14430. Add(' function GetIt(vI: longint = 0): TObject;');
  14431. Add(' end;');
  14432. Add('constructor tobject.create; begin end;');
  14433. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14434. Add('var');
  14435. Add(' Obj: tobject;');
  14436. Add('begin');
  14437. Add(' obj:=tcontrol(obj).next;');
  14438. Add(' tcontrol(obj):=nil;');
  14439. Add(' obj:=tcontrol(obj);');
  14440. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14441. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14442. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14443. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14444. Add(' obj:=tcontrol(nil);');
  14445. ConvertProgram;
  14446. CheckSource('TestClass_TypeCast',
  14447. LinesToStr([ // statements
  14448. 'rtl.createClass(this, "TObject", null, function () {',
  14449. ' this.$init = function () {',
  14450. ' this.Next = null;',
  14451. ' };',
  14452. ' this.$final = function () {',
  14453. ' this.Next = undefined;',
  14454. ' };',
  14455. ' this.Create = function () {',
  14456. ' return this;',
  14457. ' };',
  14458. '});',
  14459. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14460. ' this.$init = function () {',
  14461. ' $mod.TObject.$init.call(this);',
  14462. ' this.Arr = [];',
  14463. ' };',
  14464. ' this.$final = function () {',
  14465. ' this.Arr = undefined;',
  14466. ' $mod.TObject.$final.call(this);',
  14467. ' };',
  14468. ' this.GetIt = function (vI) {',
  14469. ' var Result = null;',
  14470. ' return Result;',
  14471. ' };',
  14472. '});',
  14473. 'this.Obj = null;'
  14474. ]),
  14475. LinesToStr([ // $mod.$main
  14476. '$mod.Obj = $mod.Obj.Next;',
  14477. '$mod.Obj = null;',
  14478. '$mod.Obj = $mod.Obj;',
  14479. '$mod.Obj = $mod.Obj.GetIt(0);',
  14480. '$mod.Obj = $mod.Obj.GetIt(0);',
  14481. '$mod.Obj = $mod.Obj.GetIt(1);',
  14482. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14483. '$mod.Obj = null;',
  14484. '']));
  14485. end;
  14486. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14487. begin
  14488. StartProgram(false);
  14489. Add('type');
  14490. Add(' TObject = class end;');
  14491. Add('procedure ProcA(var A);');
  14492. Add('begin');
  14493. Add(' TObject(A):=nil;');
  14494. Add(' TObject(A):=TObject(A);');
  14495. Add(' if TObject(A)=nil then ;');
  14496. Add(' if nil=TObject(A) then ;');
  14497. Add('end;');
  14498. Add('procedure ProcB(out A);');
  14499. Add('begin');
  14500. Add(' TObject(A):=nil;');
  14501. Add(' TObject(A):=TObject(A);');
  14502. Add(' if TObject(A)=nil then ;');
  14503. Add(' if nil=TObject(A) then ;');
  14504. Add('end;');
  14505. Add('procedure ProcC(const A);');
  14506. Add('begin');
  14507. Add(' if TObject(A)=nil then ;');
  14508. Add(' if nil=TObject(A) then ;');
  14509. Add('end;');
  14510. Add('var o: TObject;');
  14511. Add('begin');
  14512. Add(' ProcA(o);');
  14513. Add(' ProcB(o);');
  14514. Add(' ProcC(o);');
  14515. ConvertProgram;
  14516. CheckSource('TestClass_TypeCastUntypedParam',
  14517. LinesToStr([ // statements
  14518. 'rtl.createClass(this, "TObject", null, function () {',
  14519. ' this.$init = function () {',
  14520. ' };',
  14521. ' this.$final = function () {',
  14522. ' };',
  14523. '});',
  14524. 'this.ProcA = function (A) {',
  14525. ' A.set(null);',
  14526. ' A.set(A.get());',
  14527. ' if (A.get() === null);',
  14528. ' if (null === A.get());',
  14529. '};',
  14530. 'this.ProcB = function (A) {',
  14531. ' A.set(null);',
  14532. ' A.set(A.get());',
  14533. ' if (A.get() === null);',
  14534. ' if (null === A.get());',
  14535. '};',
  14536. 'this.ProcC = function (A) {',
  14537. ' if (A === null);',
  14538. ' if (null === A);',
  14539. '};',
  14540. 'this.o = null;',
  14541. '']),
  14542. LinesToStr([ // $mod.$main
  14543. '$mod.ProcA({',
  14544. ' p: $mod,',
  14545. ' get: function () {',
  14546. ' return this.p.o;',
  14547. ' },',
  14548. ' set: function (v) {',
  14549. ' this.p.o = v;',
  14550. ' }',
  14551. '});',
  14552. '$mod.ProcB({',
  14553. ' p: $mod,',
  14554. ' get: function () {',
  14555. ' return this.p.o;',
  14556. ' },',
  14557. ' set: function (v) {',
  14558. ' this.p.o = v;',
  14559. ' }',
  14560. '});',
  14561. '$mod.ProcC($mod.o);',
  14562. '']));
  14563. end;
  14564. procedure TTestModule.TestClass_Overloads;
  14565. begin
  14566. StartProgram(false);
  14567. Add('type');
  14568. Add(' TObject = class');
  14569. Add(' procedure DoIt;');
  14570. Add(' procedure DoIt(vI: longint);');
  14571. Add(' end;');
  14572. Add('procedure TObject.DoIt;');
  14573. Add('begin');
  14574. Add(' DoIt;');
  14575. Add(' DoIt(1);');
  14576. Add('end;');
  14577. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14578. Add('begin');
  14579. ConvertProgram;
  14580. CheckSource('TestClass_Overloads',
  14581. LinesToStr([ // statements
  14582. 'rtl.createClass(this, "TObject", null, function () {',
  14583. ' this.$init = function () {',
  14584. ' };',
  14585. ' this.$final = function () {',
  14586. ' };',
  14587. ' this.DoIt = function () {',
  14588. ' this.DoIt();',
  14589. ' this.DoIt$1(1);',
  14590. ' };',
  14591. ' this.DoIt$1 = function (vI) {',
  14592. ' };',
  14593. '});',
  14594. '']),
  14595. LinesToStr([ // $mod.$main
  14596. '']));
  14597. end;
  14598. procedure TTestModule.TestClass_OverloadsAncestor;
  14599. begin
  14600. StartProgram(false);
  14601. Add('type');
  14602. Add(' TObject = class;');
  14603. Add(' TObject = class');
  14604. Add(' procedure DoIt(vA: longint);');
  14605. Add(' procedure DoIt(vA, vB: longint);');
  14606. Add(' end;');
  14607. Add(' TCar = class;');
  14608. Add(' TCar = class');
  14609. Add(' procedure DoIt(vA: longint);');
  14610. Add(' procedure DoIt(vA, vB: longint);');
  14611. Add(' end;');
  14612. Add('procedure tobject.doit(va: longint);');
  14613. Add('begin');
  14614. Add(' doit(1);');
  14615. Add(' doit(1,2);');
  14616. Add('end;');
  14617. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14618. Add('procedure tcar.doit(va: longint);');
  14619. Add('begin');
  14620. Add(' doit(1);');
  14621. Add(' doit(1,2);');
  14622. Add(' inherited doit(1);');
  14623. Add(' inherited doit(1,2);');
  14624. Add('end;');
  14625. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14626. Add('begin');
  14627. ConvertProgram;
  14628. CheckSource('TestClass_OverloadsAncestor',
  14629. LinesToStr([ // statements
  14630. 'rtl.createClass(this, "TObject", null, function () {',
  14631. ' this.$init = function () {',
  14632. ' };',
  14633. ' this.$final = function () {',
  14634. ' };',
  14635. ' this.DoIt = function (vA) {',
  14636. ' this.DoIt(1);',
  14637. ' this.DoIt$1(1,2);',
  14638. ' };',
  14639. ' this.DoIt$1 = function (vA, vB) {',
  14640. ' };',
  14641. '});',
  14642. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14643. ' this.DoIt$2 = function (vA) {',
  14644. ' this.DoIt$2(1);',
  14645. ' this.DoIt$3(1, 2);',
  14646. ' $mod.TObject.DoIt.call(this, 1);',
  14647. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14648. ' };',
  14649. ' this.DoIt$3 = function (vA, vB) {',
  14650. ' };',
  14651. '});',
  14652. '']),
  14653. LinesToStr([ // $mod.$main
  14654. '']));
  14655. end;
  14656. procedure TTestModule.TestClass_OverloadConstructor;
  14657. begin
  14658. StartProgram(false);
  14659. Add('type');
  14660. Add(' TObject = class');
  14661. Add(' constructor Create(vA: longint);');
  14662. Add(' constructor Create(vA, vB: longint);');
  14663. Add(' end;');
  14664. Add(' TCar = class');
  14665. Add(' constructor Create(vA: longint);');
  14666. Add(' constructor Create(vA, vB: longint);');
  14667. Add(' end;');
  14668. Add('constructor tobject.create(va: longint);');
  14669. Add('begin');
  14670. Add(' create(1);');
  14671. Add(' create(1,2);');
  14672. Add('end;');
  14673. Add('constructor tobject.create(va, vb: longint); begin end;');
  14674. Add('constructor tcar.create(va: longint);');
  14675. Add('begin');
  14676. Add(' create(1);');
  14677. Add(' create(1,2);');
  14678. Add(' inherited create(1);');
  14679. Add(' inherited create(1,2);');
  14680. Add('end;');
  14681. Add('constructor tcar.create(va, vb: longint); begin end;');
  14682. Add('begin');
  14683. Add(' tobject.create(1);');
  14684. Add(' tobject.create(1,2);');
  14685. Add(' tcar.create(1);');
  14686. Add(' tcar.create(1,2);');
  14687. ConvertProgram;
  14688. CheckSource('TestClass_OverloadConstructor',
  14689. LinesToStr([ // statements
  14690. 'rtl.createClass(this, "TObject", null, function () {',
  14691. ' this.$init = function () {',
  14692. ' };',
  14693. ' this.$final = function () {',
  14694. ' };',
  14695. ' this.Create = function (vA) {',
  14696. ' this.Create(1);',
  14697. ' this.Create$1(1,2);',
  14698. ' return this;',
  14699. ' };',
  14700. ' this.Create$1 = function (vA, vB) {',
  14701. ' return this;',
  14702. ' };',
  14703. '});',
  14704. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14705. ' this.Create$2 = function (vA) {',
  14706. ' this.Create$2(1);',
  14707. ' this.Create$3(1, 2);',
  14708. ' $mod.TObject.Create.call(this, 1);',
  14709. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14710. ' return this;',
  14711. ' };',
  14712. ' this.Create$3 = function (vA, vB) {',
  14713. ' return this;',
  14714. ' };',
  14715. '});',
  14716. '']),
  14717. LinesToStr([ // $mod.$main
  14718. '$mod.TObject.$create("Create", [1]);',
  14719. '$mod.TObject.$create("Create$1", [1, 2]);',
  14720. '$mod.TCar.$create("Create$2", [1]);',
  14721. '$mod.TCar.$create("Create$3", [1, 2]);',
  14722. '']));
  14723. end;
  14724. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14725. begin
  14726. StartProgram(false);
  14727. Add([
  14728. '{$mode delphi}',
  14729. 'type',
  14730. ' TObject = class end;',
  14731. ' TBird = class',
  14732. ' function {#a}GetValue: longint; overload; virtual;',
  14733. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14734. ' end;',
  14735. ' TEagle = class(TBird)',
  14736. ' function {#c}GetValue: longint; overload; override;',
  14737. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14738. ' end;',
  14739. 'function TBird.GetValue: longint;',
  14740. 'begin',
  14741. ' if 3={@a}GetValue then ;',
  14742. ' if 4={@b}GetValue(5) then ;',
  14743. 'end;',
  14744. 'function TBird.GetValue(AValue: longint): longint;',
  14745. 'begin',
  14746. 'end;',
  14747. 'function TEagle.GetValue: longint;',
  14748. 'begin',
  14749. ' if 13={@c}GetValue then ;',
  14750. ' if 14={@d}GetValue(15) then ;',
  14751. ' if 15=inherited {@a}GetValue then ;',
  14752. ' if 16=inherited {@b}GetValue(17) then ;',
  14753. 'end;',
  14754. 'function TEagle.GetValue(AValue: longint): longint;',
  14755. 'begin',
  14756. 'end;',
  14757. 'var',
  14758. ' e: TEagle;',
  14759. 'begin',
  14760. ' if 23=e.{@c}GetValue then ;',
  14761. ' if 24=e.{@d}GetValue(25) then ;']);
  14762. ConvertProgram;
  14763. CheckSource('TestClass_OverloadDelphiOverride',
  14764. LinesToStr([ // statements
  14765. 'rtl.createClass(this, "TObject", null, function () {',
  14766. ' this.$init = function () {',
  14767. ' };',
  14768. ' this.$final = function () {',
  14769. ' };',
  14770. '});',
  14771. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14772. ' this.GetValue = function () {',
  14773. ' var Result = 0;',
  14774. ' if (3 === this.GetValue()) ;',
  14775. ' if (4 === this.GetValue$1(5)) ;',
  14776. ' return Result;',
  14777. ' };',
  14778. ' this.GetValue$1 = function (AValue) {',
  14779. ' var Result = 0;',
  14780. ' return Result;',
  14781. ' };',
  14782. '});',
  14783. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14784. ' this.GetValue = function () {',
  14785. ' var Result = 0;',
  14786. ' if (13 === this.GetValue()) ;',
  14787. ' if (14 === this.GetValue$1(15)) ;',
  14788. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14789. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14790. ' return Result;',
  14791. ' };',
  14792. ' this.GetValue$1 = function (AValue) {',
  14793. ' var Result = 0;',
  14794. ' return Result;',
  14795. ' };',
  14796. '});',
  14797. 'this.e = null;',
  14798. '']),
  14799. LinesToStr([ // $mod.$main
  14800. 'if (23 === $mod.e.GetValue()) ;',
  14801. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14802. '']));
  14803. end;
  14804. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14805. begin
  14806. StartProgram(false);
  14807. Add([
  14808. '{$mode delphi}',
  14809. 'type',
  14810. ' TObject = class end;',
  14811. ' TAnimal = class',
  14812. ' public',
  14813. ' {#animal_a}A: longint;',
  14814. ' function {#animal_b}B: longint;',
  14815. ' end;',
  14816. ' TBird = class(TAnimal)',
  14817. ' public',
  14818. ' {#bird_a}A: double;',
  14819. ' {#bird_b}B: boolean;',
  14820. ' end;',
  14821. ' TEagle = class(TBird)',
  14822. ' public',
  14823. ' function {#eagle_a}A: boolean;',
  14824. ' {#eagle_b}B: double;',
  14825. ' end;',
  14826. 'function TAnimal.B: longint;',
  14827. 'begin',
  14828. 'end;',
  14829. 'function TEagle.A: boolean;',
  14830. 'begin',
  14831. ' {@eagle_b}B:=3.3;',
  14832. ' {@eagle_a}A();',
  14833. ' TBird(Self).{@bird_b}B:=true;',
  14834. ' TAnimal(Self).{@animal_a}A:=17;',
  14835. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14836. 'end;',
  14837. 'var',
  14838. ' e: TEagle;',
  14839. 'begin',
  14840. ' e.{@eagle_b}B:=5.3;',
  14841. ' if e.{@eagle_a}A then ;',
  14842. '']);
  14843. ConvertProgram;
  14844. CheckSource('TestClass_ReintroduceVarDelphi',
  14845. LinesToStr([ // statements
  14846. 'rtl.createClass(this, "TObject", null, function () {',
  14847. ' this.$init = function () {',
  14848. ' };',
  14849. ' this.$final = function () {',
  14850. ' };',
  14851. '});',
  14852. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14853. ' this.$init = function () {',
  14854. ' $mod.TObject.$init.call(this);',
  14855. ' this.A = 0;',
  14856. ' };',
  14857. ' this.B = function () {',
  14858. ' var Result = 0;',
  14859. ' return Result;',
  14860. ' };',
  14861. '});',
  14862. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14863. ' this.$init = function () {',
  14864. ' $mod.TAnimal.$init.call(this);',
  14865. ' this.A$1 = 0.0;',
  14866. ' this.B$1 = false;',
  14867. ' };',
  14868. '});',
  14869. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14870. ' this.$init = function () {',
  14871. ' $mod.TBird.$init.call(this);',
  14872. ' this.B$2 = 0.0;',
  14873. ' };',
  14874. ' this.A$2 = function () {',
  14875. ' var Result = false;',
  14876. ' this.B$2 = 3.3;',
  14877. ' this.A$2();',
  14878. ' this.B$1 = true;',
  14879. ' this.A = 17;',
  14880. ' this.B$1 = this.A$1 > 1;',
  14881. ' return Result;',
  14882. ' };',
  14883. '});',
  14884. 'this.e = null;',
  14885. '']),
  14886. LinesToStr([ // $mod.$main
  14887. '$mod.e.B$2 = 5.3;',
  14888. 'if ($mod.e.A$2()) ;',
  14889. '']));
  14890. end;
  14891. procedure TTestModule.TestClass_ReintroducedVar;
  14892. begin
  14893. StartProgram(false);
  14894. Add('type');
  14895. Add(' TObject = class');
  14896. Add(' strict private');
  14897. Add(' Some: longint;');
  14898. Add(' end;');
  14899. Add(' TMobile = class');
  14900. Add(' strict private');
  14901. Add(' Some: string;');
  14902. Add(' end;');
  14903. Add(' TCar = class(tmobile)');
  14904. Add(' procedure Some;');
  14905. Add(' procedure Some(vA: longint);');
  14906. Add(' end;');
  14907. Add('procedure tcar.some;');
  14908. Add('begin');
  14909. Add(' Some;');
  14910. Add(' Some(1);');
  14911. Add('end;');
  14912. Add('procedure tcar.some(va: longint); begin end;');
  14913. Add('begin');
  14914. ConvertProgram;
  14915. CheckSource('TestClass_ReintroducedVar',
  14916. LinesToStr([ // statements
  14917. 'rtl.createClass(this, "TObject", null, function () {',
  14918. ' this.$init = function () {',
  14919. ' this.Some = 0;',
  14920. ' };',
  14921. ' this.$final = function () {',
  14922. ' };',
  14923. '});',
  14924. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14925. ' this.$init = function () {',
  14926. ' $mod.TObject.$init.call(this);',
  14927. ' this.Some$1 = "";',
  14928. ' };',
  14929. '});',
  14930. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14931. ' this.Some$2 = function () {',
  14932. ' this.Some$2();',
  14933. ' this.Some$3(1);',
  14934. ' };',
  14935. ' this.Some$3 = function (vA) {',
  14936. ' };',
  14937. '});',
  14938. '']),
  14939. LinesToStr([ // $mod.$main
  14940. '']));
  14941. end;
  14942. procedure TTestModule.TestClass_RaiseDescendant;
  14943. begin
  14944. StartProgram(false);
  14945. Add([
  14946. 'type',
  14947. ' TObject = class',
  14948. ' constructor Create(Msg: string);',
  14949. ' end;',
  14950. ' Exception = class',
  14951. ' end;',
  14952. ' EConvertError = class(Exception)',
  14953. ' end;',
  14954. 'constructor TObject.Create(Msg: string); begin end;',
  14955. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14956. 'begin',
  14957. ' raise Exception.Create(''Bar1'');',
  14958. ' raise EConvertError.Create(''Bar2'');',
  14959. ' raise AssertConv(''Bar2'');',
  14960. ' raise AssertConv;',
  14961. '']);
  14962. ConvertProgram;
  14963. CheckSource('TestClass_RaiseDescendant',
  14964. LinesToStr([ // statements
  14965. 'rtl.createClass(this, "TObject", null, function () {',
  14966. ' this.$init = function () {',
  14967. ' };',
  14968. ' this.$final = function () {',
  14969. ' };',
  14970. ' this.Create = function (Msg) {',
  14971. ' return this;',
  14972. ' };',
  14973. '});',
  14974. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14975. '});',
  14976. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14977. '});',
  14978. 'this.AssertConv = function (Msg) {',
  14979. ' var Result = null;',
  14980. ' return Result;',
  14981. '};',
  14982. '']),
  14983. LinesToStr([ // $mod.$main
  14984. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14985. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14986. 'throw $mod.AssertConv("Bar2");',
  14987. 'throw $mod.AssertConv("def");',
  14988. '']));
  14989. end;
  14990. procedure TTestModule.TestClass_ExternalMethod;
  14991. begin
  14992. AddModuleWithIntfImplSrc('unit2.pas',
  14993. LinesToStr([
  14994. 'type',
  14995. ' TObject = class',
  14996. ' public',
  14997. ' procedure Intern; external name ''$DoIntern'';',
  14998. ' end;',
  14999. '']),
  15000. LinesToStr([
  15001. '']));
  15002. StartUnit(true);
  15003. Add('interface');
  15004. Add('uses unit2;');
  15005. Add('type');
  15006. Add(' TCar = class(TObject)');
  15007. Add(' public');
  15008. Add(' procedure Intern2; external name ''$DoIntern2'';');
  15009. Add(' procedure DoIt;');
  15010. Add(' end;');
  15011. Add('implementation');
  15012. Add('procedure tcar.doit;');
  15013. Add('begin');
  15014. Add(' Intern;');
  15015. Add(' Intern();');
  15016. Add(' Intern2;');
  15017. Add(' Intern2();');
  15018. Add('end;');
  15019. Add('var Obj: TCar;');
  15020. Add('begin');
  15021. Add(' obj.intern;');
  15022. Add(' obj.intern();');
  15023. Add(' obj.intern2;');
  15024. Add(' obj.intern2();');
  15025. Add(' obj.doit;');
  15026. Add(' obj.doit();');
  15027. Add(' with obj do begin');
  15028. Add(' Intern;');
  15029. Add(' Intern();');
  15030. Add(' Intern2;');
  15031. Add(' Intern2();');
  15032. Add(' end;');
  15033. ConvertUnit;
  15034. CheckSource('TestClass_ExternalMethod',
  15035. LinesToStr([
  15036. 'var $impl = $mod.$impl;',
  15037. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15038. ' this.DoIt = function () {',
  15039. ' this.$DoIntern();',
  15040. ' this.$DoIntern();',
  15041. ' this.$DoIntern2();',
  15042. ' this.$DoIntern2();',
  15043. ' };',
  15044. ' });',
  15045. '']),
  15046. LinesToStr([ // this.$init
  15047. '$impl.Obj.$DoIntern();',
  15048. '$impl.Obj.$DoIntern();',
  15049. '$impl.Obj.$DoIntern2();',
  15050. '$impl.Obj.$DoIntern2();',
  15051. '$impl.Obj.DoIt();',
  15052. '$impl.Obj.DoIt();',
  15053. 'var $with = $impl.Obj;',
  15054. '$with.$DoIntern();',
  15055. '$with.$DoIntern();',
  15056. '$with.$DoIntern2();',
  15057. '$with.$DoIntern2();',
  15058. '']),
  15059. LinesToStr([ // implementation
  15060. '$impl.Obj = null;',
  15061. '']) );
  15062. end;
  15063. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  15064. begin
  15065. StartProgram(false);
  15066. Add('type');
  15067. Add(' TObject = class');
  15068. Add(' procedure DoIt; virtual; external name ''Foo'';');
  15069. Add(' end;');
  15070. Add('begin');
  15071. SetExpectedPasResolverError('Virtual method name must match external',
  15072. nVirtualMethodNameMustMatchExternal);
  15073. ConvertProgram;
  15074. end;
  15075. procedure TTestModule.TestClass_ExternalOverrideFail;
  15076. begin
  15077. StartProgram(false);
  15078. Add('type');
  15079. Add(' TObject = class');
  15080. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  15081. Add(' end;');
  15082. Add(' TCar = class');
  15083. Add(' procedure DoIt; override; external name ''DoIt'';');
  15084. Add(' end;');
  15085. Add('begin');
  15086. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  15087. nInvalidXModifierY);
  15088. ConvertProgram;
  15089. end;
  15090. procedure TTestModule.TestClass_ExternalVar;
  15091. begin
  15092. AddModuleWithIntfImplSrc('unit2.pas',
  15093. LinesToStr([
  15094. '{$modeswitch externalclass}',
  15095. 'type',
  15096. ' TObject = class',
  15097. ' public',
  15098. ' Intern: longint external name ''$Intern'';',
  15099. ' Bracket: longint external name ''["A B"]'';',
  15100. ' end;',
  15101. '']),
  15102. LinesToStr([
  15103. '']));
  15104. StartUnit(true);
  15105. Add([
  15106. 'interface',
  15107. 'uses unit2;',
  15108. '{$modeswitch externalclass}',
  15109. 'type',
  15110. ' TCar = class(tobject)',
  15111. ' public',
  15112. ' Intern2: longint external name ''$Intern2'';',
  15113. ' procedure DoIt;',
  15114. ' end;',
  15115. 'implementation',
  15116. 'procedure tcar.doit;',
  15117. 'begin',
  15118. ' Intern:=Intern+1;',
  15119. ' Intern2:=Intern2+2;',
  15120. ' Bracket:=Bracket+3;',
  15121. 'end;',
  15122. 'var Obj: TCar;',
  15123. 'begin',
  15124. ' obj.intern:=obj.intern+1;',
  15125. ' obj.intern2:=obj.intern2+2;',
  15126. ' obj.Bracket:=obj.Bracket+3;',
  15127. ' with obj do begin',
  15128. ' intern:=intern+1;',
  15129. ' intern2:=intern2+2;',
  15130. ' Bracket:=Bracket+3;',
  15131. ' end;']);
  15132. ConvertUnit;
  15133. CheckSource('TestClass_ExternalVar',
  15134. LinesToStr([
  15135. 'var $impl = $mod.$impl;',
  15136. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15137. ' this.DoIt = function () {',
  15138. ' this.$Intern = this.$Intern + 1;',
  15139. ' this.$Intern2 = this.$Intern2 + 2;',
  15140. ' this["A B"] = this["A B"] + 3;',
  15141. ' };',
  15142. ' });',
  15143. '']),
  15144. LinesToStr([
  15145. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  15146. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  15147. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  15148. 'var $with = $impl.Obj;',
  15149. '$with.$Intern = $with.$Intern + 1;',
  15150. '$with.$Intern2 = $with.$Intern2 + 2;',
  15151. '$with["A B"] = $with["A B"] + 3;',
  15152. '']),
  15153. LinesToStr([ // implementation
  15154. '$impl.Obj = null;',
  15155. '']));
  15156. end;
  15157. procedure TTestModule.TestClass_Const;
  15158. begin
  15159. StartProgram(false);
  15160. Add([
  15161. 'type',
  15162. ' integer = longint;',
  15163. ' TClass = class of TObject;',
  15164. ' TObject = class',
  15165. ' public',
  15166. ' const cI: integer = 3;',
  15167. ' procedure DoIt;',
  15168. ' class procedure DoMore;',
  15169. ' end;',
  15170. 'procedure tobject.doit;',
  15171. 'begin',
  15172. ' if cI=4 then;',
  15173. ' if 5=cI then;',
  15174. ' if Self.cI=6 then;',
  15175. ' if 7=Self.cI then;',
  15176. ' with Self do begin',
  15177. ' if cI=11 then;',
  15178. ' if 12=cI then;',
  15179. ' end;',
  15180. 'end;',
  15181. 'class procedure tobject.domore;',
  15182. 'begin',
  15183. ' if cI=8 then;',
  15184. ' if Self.cI=9 then;',
  15185. ' if 10=cI then;',
  15186. ' if 11=Self.cI then;',
  15187. ' with Self do begin',
  15188. ' if cI=13 then;',
  15189. ' if 14=cI then;',
  15190. ' end;',
  15191. 'end;',
  15192. 'var',
  15193. ' Obj: TObject;',
  15194. ' Cla: TClass;',
  15195. 'begin',
  15196. ' if TObject.cI=21 then ;',
  15197. ' if Obj.cI=22 then ;',
  15198. ' if Cla.cI=23 then ;',
  15199. ' with obj do if ci=24 then;',
  15200. ' with TObject do if ci=25 then;',
  15201. ' with Cla do if ci=26 then;']);
  15202. ConvertProgram;
  15203. CheckSource('TestClass_Const',
  15204. LinesToStr([
  15205. 'rtl.createClass(this, "TObject", null, function () {',
  15206. ' this.cI = 3;',
  15207. ' this.$init = function () {',
  15208. ' };',
  15209. ' this.$final = function () {',
  15210. ' };',
  15211. ' this.DoIt = function () {',
  15212. ' if (this.cI === 4) ;',
  15213. ' if (5 === this.cI) ;',
  15214. ' if (this.cI === 6) ;',
  15215. ' if (7 === this.cI) ;',
  15216. ' if (this.cI === 11) ;',
  15217. ' if (12 === this.cI) ;',
  15218. ' };',
  15219. ' this.DoMore = function () {',
  15220. ' if (this.cI === 8) ;',
  15221. ' if (this.cI === 9) ;',
  15222. ' if (10 === this.cI) ;',
  15223. ' if (11 === this.cI) ;',
  15224. ' if (this.cI === 13) ;',
  15225. ' if (14 === this.cI) ;',
  15226. ' };',
  15227. '});',
  15228. 'this.Obj = null;',
  15229. 'this.Cla = null;',
  15230. '']),
  15231. LinesToStr([
  15232. 'if ($mod.TObject.cI === 21) ;',
  15233. 'if ($mod.Obj.cI === 22) ;',
  15234. 'if ($mod.Cla.cI === 23) ;',
  15235. 'var $with = $mod.Obj;',
  15236. 'if ($with.cI === 24) ;',
  15237. 'var $with1 = $mod.TObject;',
  15238. 'if ($with1.cI === 25) ;',
  15239. 'var $with2 = $mod.Cla;',
  15240. 'if ($with2.cI === 26) ;',
  15241. '']));
  15242. end;
  15243. procedure TTestModule.TestClass_ConstEnum;
  15244. begin
  15245. StartProgram(false);
  15246. Add([
  15247. 'type',
  15248. ' TEnum = (red,blue);',
  15249. ' TObject = class',
  15250. ' end;',
  15251. ' TAnimal = class',
  15252. ' public',
  15253. ' type TSubEnum = (light,dark);',
  15254. ' const a = high(TEnum);',
  15255. ' const b = high(TSubEnum);',
  15256. ' end;',
  15257. ' TBird = class(TAnimal)',
  15258. ' public',
  15259. ' const c = high(TEnum);',
  15260. ' const d = high(TSubEnum);',
  15261. ' end;',
  15262. ' TAnt = class',
  15263. ' public',
  15264. ' const e = high(TEnum);',
  15265. ' const f = high(TBird.TSubEnum);',
  15266. ' end;',
  15267. 'begin',
  15268. '']);
  15269. ConvertProgram;
  15270. CheckSource('TestClass_ConstEnum',
  15271. LinesToStr([
  15272. 'this.TEnum = {',
  15273. ' "0": "red",',
  15274. ' red: 0,',
  15275. ' "1": "blue",',
  15276. ' blue: 1',
  15277. '};',
  15278. 'rtl.createClass(this, "TObject", null, function () {',
  15279. ' this.$init = function () {',
  15280. ' };',
  15281. ' this.$final = function () {',
  15282. ' };',
  15283. '});',
  15284. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  15285. ' this.TSubEnum = {',
  15286. ' "0": "light",',
  15287. ' light: 0,',
  15288. ' "1": "dark",',
  15289. ' dark: 1',
  15290. ' };',
  15291. ' this.a = $mod.TEnum.blue;',
  15292. ' this.b = this.TSubEnum.dark;',
  15293. '});',
  15294. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  15295. ' this.c = $mod.TEnum.blue;',
  15296. ' this.d = this.TSubEnum.dark;',
  15297. '});',
  15298. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  15299. ' this.e = $mod.TEnum.blue;',
  15300. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  15301. '});',
  15302. '']),
  15303. LinesToStr([
  15304. '']));
  15305. end;
  15306. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  15307. begin
  15308. StartProgram(false);
  15309. Add([
  15310. 'type',
  15311. ' TObject = class',
  15312. ' const cI: longint = 3;',
  15313. ' procedure Fly;',
  15314. ' procedure Run;',
  15315. ' end;',
  15316. ' TBird = class',
  15317. ' procedure Go;',
  15318. ' end;',
  15319. 'procedure tobject.fly;',
  15320. 'const cI: word = 4;',
  15321. 'begin',
  15322. ' if cI=Self.cI then ;',
  15323. 'end;',
  15324. 'procedure tobject.run;',
  15325. 'const cI: word = 5;',
  15326. 'begin',
  15327. ' if cI=Self.cI then ;',
  15328. 'end;',
  15329. 'procedure tbird.go;',
  15330. 'const cI: word = 6;',
  15331. 'begin',
  15332. ' if cI=Self.cI then ;',
  15333. 'end;',
  15334. 'begin',
  15335. '']);
  15336. ConvertProgram;
  15337. CheckSource('TestClass_LocalConstDuplicate_Prg',
  15338. LinesToStr([
  15339. 'rtl.createClass(this, "TObject", null, function () {',
  15340. ' this.cI = 3;',
  15341. ' this.$init = function () {',
  15342. ' };',
  15343. ' this.$final = function () {',
  15344. ' };',
  15345. ' var cI$1 = 4;',
  15346. ' this.Fly = function () {',
  15347. ' if (cI$1 === this.cI) ;',
  15348. ' };',
  15349. ' var cI$2 = 5;',
  15350. ' this.Run = function () {',
  15351. ' if (cI$2 === this.cI) ;',
  15352. ' };',
  15353. '});',
  15354. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15355. ' var cI$3 = 6;',
  15356. ' this.Go = function () {',
  15357. ' if (cI$3 === this.cI) ;',
  15358. ' };',
  15359. '});',
  15360. '']),
  15361. LinesToStr([
  15362. '']));
  15363. end;
  15364. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  15365. begin
  15366. StartUnit(false);
  15367. Add([
  15368. 'interface',
  15369. 'type',
  15370. ' TObject = class',
  15371. ' const cI: longint = 3;',
  15372. ' procedure Fly;',
  15373. ' procedure Run;',
  15374. ' end;',
  15375. ' TBird = class',
  15376. ' procedure Go;',
  15377. ' end;',
  15378. 'implementation',
  15379. 'procedure tobject.fly;',
  15380. 'const cI: word = 4;',
  15381. 'begin',
  15382. ' if cI=Self.cI then ;',
  15383. 'end;',
  15384. 'procedure tobject.run;',
  15385. 'const cI: word = 5;',
  15386. 'begin',
  15387. ' if cI=Self.cI then ;',
  15388. 'end;',
  15389. 'procedure tbird.go;',
  15390. 'const cI: word = 6;',
  15391. 'begin',
  15392. ' if cI=Self.cI then ;',
  15393. 'end;',
  15394. '']);
  15395. ConvertUnit;
  15396. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15397. LinesToStr([
  15398. 'rtl.createClass(this, "TObject", null, function () {',
  15399. ' this.cI = 3;',
  15400. ' this.$init = function () {',
  15401. ' };',
  15402. ' this.$final = function () {',
  15403. ' };',
  15404. ' var cI$1 = 4;',
  15405. ' this.Fly = function () {',
  15406. ' if (cI$1 === this.cI) ;',
  15407. ' };',
  15408. ' var cI$2 = 5;',
  15409. ' this.Run = function () {',
  15410. ' if (cI$2 === this.cI) ;',
  15411. ' };',
  15412. '});',
  15413. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15414. ' var cI$3 = 6;',
  15415. ' this.Go = function () {',
  15416. ' if (cI$3 === this.cI) ;',
  15417. ' };',
  15418. '});',
  15419. '']),
  15420. '',
  15421. '');
  15422. end;
  15423. procedure TTestModule.TestClass_LocalVarSelfFail;
  15424. begin
  15425. StartProgram(false);
  15426. Add([
  15427. 'type',
  15428. ' TObject = class',
  15429. ' constructor Create;',
  15430. ' end;',
  15431. 'constructor tobject.create;',
  15432. 'var self: longint;',
  15433. 'begin',
  15434. 'end',
  15435. 'begin',
  15436. '']);
  15437. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15438. ConvertProgram;
  15439. end;
  15440. procedure TTestModule.TestClass_ArgSelfFail;
  15441. begin
  15442. StartProgram(false);
  15443. Add([
  15444. 'type',
  15445. ' TObject = class',
  15446. ' procedure DoIt(Self: longint);',
  15447. ' end;',
  15448. 'procedure tobject.doit(self: longint);',
  15449. 'begin',
  15450. 'end',
  15451. 'begin',
  15452. '']);
  15453. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15454. ConvertProgram;
  15455. end;
  15456. procedure TTestModule.TestClass_NestedProcSelf;
  15457. begin
  15458. StartProgram(false);
  15459. Add([
  15460. 'type',
  15461. ' TObject = class',
  15462. ' Key: longint;',
  15463. ' class var State: longint;',
  15464. ' procedure DoIt;',
  15465. ' function GetSize: longint; virtual; abstract;',
  15466. ' procedure SetSize(Value: longint); virtual; abstract;',
  15467. ' property Size: longint read GetSize write SetSize;',
  15468. ' end;',
  15469. 'procedure tobject.doit;',
  15470. ' procedure Sub;',
  15471. ' begin',
  15472. ' key:=key+2;',
  15473. ' self.key:=self.key+3;',
  15474. ' state:=state+4;',
  15475. ' self.state:=self.state+5;',
  15476. ' tobject.state:=tobject.state+6;',
  15477. ' size:=size+7;',
  15478. ' self.size:=self.size+8;',
  15479. ' end;',
  15480. 'begin',
  15481. ' sub;',
  15482. ' key:=key+12;',
  15483. ' self.key:=self.key+13;',
  15484. ' state:=state+14;',
  15485. ' self.state:=self.state+15;',
  15486. ' tobject.state:=tobject.state+16;',
  15487. ' size:=size+17;',
  15488. ' self.size:=self.size+18;',
  15489. 'end;',
  15490. 'begin',
  15491. '']);
  15492. ConvertProgram;
  15493. CheckSource('TestClass_NestedProcSelf',
  15494. LinesToStr([ // statements
  15495. 'rtl.createClass(this, "TObject", null, function () {',
  15496. ' this.State = 0;',
  15497. ' this.$init = function () {',
  15498. ' this.Key = 0;',
  15499. ' };',
  15500. ' this.$final = function () {',
  15501. ' };',
  15502. ' this.DoIt = function () {',
  15503. ' var $Self = this;',
  15504. ' function Sub() {',
  15505. ' $Self.Key = $Self.Key + 2;',
  15506. ' $Self.Key = $Self.Key + 3;',
  15507. ' $mod.TObject.State = $Self.State + 4;',
  15508. ' $mod.TObject.State = $Self.State + 5;',
  15509. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15510. ' $Self.SetSize($Self.GetSize() + 7);',
  15511. ' $Self.SetSize($Self.GetSize() + 8);',
  15512. ' };',
  15513. ' Sub();',
  15514. ' this.Key = this.Key + 12;',
  15515. ' $Self.Key = $Self.Key + 13;',
  15516. ' $mod.TObject.State = this.State + 14;',
  15517. ' $mod.TObject.State = $Self.State + 15;',
  15518. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15519. ' this.SetSize(this.GetSize() + 17);',
  15520. ' $Self.SetSize($Self.GetSize() + 18);',
  15521. ' };',
  15522. '});',
  15523. '']),
  15524. LinesToStr([ // $mod.$main
  15525. '']));
  15526. end;
  15527. procedure TTestModule.TestClass_NestedProcSelf2;
  15528. begin
  15529. StartProgram(false);
  15530. Add([
  15531. 'type',
  15532. ' TObject = class',
  15533. ' Key: longint;',
  15534. ' class var State: longint;',
  15535. ' function GetSize: longint; virtual; abstract;',
  15536. ' procedure SetSize(Value: longint); virtual; abstract;',
  15537. ' property Size: longint read GetSize write SetSize;',
  15538. ' end;',
  15539. ' TBird = class',
  15540. ' procedure DoIt;',
  15541. ' end;',
  15542. 'procedure tbird.doit;',
  15543. ' procedure Sub;',
  15544. ' begin',
  15545. ' key:=key+2;',
  15546. ' self.key:=self.key+3;',
  15547. ' state:=state+4;',
  15548. ' self.state:=self.state+5;',
  15549. ' tobject.state:=tobject.state+6;',
  15550. ' size:=size+7;',
  15551. ' self.size:=self.size+8;',
  15552. ' end;',
  15553. 'begin',
  15554. ' sub;',
  15555. ' key:=key+12;',
  15556. ' self.key:=self.key+13;',
  15557. ' state:=state+14;',
  15558. ' self.state:=self.state+15;',
  15559. ' tobject.state:=tobject.state+16;',
  15560. ' size:=size+17;',
  15561. ' self.size:=self.size+18;',
  15562. 'end;',
  15563. 'begin',
  15564. '']);
  15565. ConvertProgram;
  15566. CheckSource('TestClass_NestedProcSelf2',
  15567. LinesToStr([ // statements
  15568. 'rtl.createClass(this, "TObject", null, function () {',
  15569. ' this.State = 0;',
  15570. ' this.$init = function () {',
  15571. ' this.Key = 0;',
  15572. ' };',
  15573. ' this.$final = function () {',
  15574. ' };',
  15575. '});',
  15576. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15577. ' this.DoIt = function () {',
  15578. ' var $Self = this;',
  15579. ' function Sub() {',
  15580. ' $Self.Key = $Self.Key + 2;',
  15581. ' $Self.Key = $Self.Key + 3;',
  15582. ' $mod.TObject.State = $Self.State + 4;',
  15583. ' $mod.TObject.State = $Self.State + 5;',
  15584. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15585. ' $Self.SetSize($Self.GetSize() + 7);',
  15586. ' $Self.SetSize($Self.GetSize() + 8);',
  15587. ' };',
  15588. ' Sub();',
  15589. ' this.Key = this.Key + 12;',
  15590. ' $Self.Key = $Self.Key + 13;',
  15591. ' $mod.TObject.State = this.State + 14;',
  15592. ' $mod.TObject.State = $Self.State + 15;',
  15593. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15594. ' this.SetSize(this.GetSize() + 17);',
  15595. ' $Self.SetSize($Self.GetSize() + 18);',
  15596. ' };',
  15597. '});',
  15598. '']),
  15599. LinesToStr([ // $mod.$main
  15600. '']));
  15601. end;
  15602. procedure TTestModule.TestClass_NestedProcClassSelf;
  15603. begin
  15604. StartProgram(false);
  15605. Add([
  15606. 'type',
  15607. ' TObject = class',
  15608. ' class var State: longint;',
  15609. ' class procedure DoIt;',
  15610. ' class function GetSize: longint; virtual; abstract;',
  15611. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15612. ' class property Size: longint read GetSize write SetSize;',
  15613. ' end;',
  15614. 'class procedure tobject.doit;',
  15615. ' procedure Sub;',
  15616. ' begin',
  15617. ' state:=state+2;',
  15618. ' self.state:=self.state+3;',
  15619. ' tobject.state:=tobject.state+4;',
  15620. ' size:=size+5;',
  15621. ' self.size:=self.size+6;',
  15622. ' tobject.size:=tobject.size+7;',
  15623. ' end;',
  15624. 'begin',
  15625. ' sub;',
  15626. ' state:=state+12;',
  15627. ' self.state:=self.state+13;',
  15628. ' tobject.state:=tobject.state+14;',
  15629. ' size:=size+15;',
  15630. ' self.size:=self.size+16;',
  15631. ' tobject.size:=tobject.size+17;',
  15632. 'end;',
  15633. 'begin',
  15634. '']);
  15635. ConvertProgram;
  15636. CheckSource('TestClass_NestedProcClassSelf',
  15637. LinesToStr([ // statements
  15638. 'rtl.createClass(this, "TObject", null, function () {',
  15639. ' this.State = 0;',
  15640. ' this.$init = function () {',
  15641. ' };',
  15642. ' this.$final = function () {',
  15643. ' };',
  15644. ' this.DoIt = function () {',
  15645. ' var $Self = this;',
  15646. ' function Sub() {',
  15647. ' $mod.TObject.State = $Self.State + 2;',
  15648. ' $mod.TObject.State = $Self.State + 3;',
  15649. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15650. ' $Self.SetSize($Self.GetSize() + 5);',
  15651. ' $Self.SetSize($Self.GetSize() + 6);',
  15652. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15653. ' };',
  15654. ' Sub();',
  15655. ' $mod.TObject.State = this.State + 12;',
  15656. ' $mod.TObject.State = $Self.State + 13;',
  15657. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15658. ' this.SetSize(this.GetSize() + 15);',
  15659. ' $Self.SetSize($Self.GetSize() + 16);',
  15660. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15661. ' };',
  15662. '});',
  15663. '']),
  15664. LinesToStr([ // $mod.$main
  15665. '']));
  15666. end;
  15667. procedure TTestModule.TestClass_NestedProcCallInherited;
  15668. begin
  15669. StartProgram(false);
  15670. Add([
  15671. 'type',
  15672. ' TObject = class',
  15673. ' function DoIt(k: boolean): longint; virtual;',
  15674. ' end;',
  15675. ' TBird = class',
  15676. ' function DoIt(k: boolean): longint; override;',
  15677. ' end;',
  15678. 'function tobject.doit(k: boolean): longint;',
  15679. 'begin',
  15680. 'end;',
  15681. 'function tbird.doit(k: boolean): longint;',
  15682. ' procedure Sub;',
  15683. ' begin',
  15684. ' inherited DoIt(true);',
  15685. //' if inherited DoIt(false)=4 then ;',
  15686. ' end;',
  15687. 'begin',
  15688. ' Sub;',
  15689. ' inherited;',
  15690. ' inherited DoIt(true);',
  15691. //' if inherited DoIt(false)=14 then ;',
  15692. 'end;',
  15693. 'begin',
  15694. '']);
  15695. ConvertProgram;
  15696. CheckSource('TestClass_NestedProcCallInherited',
  15697. LinesToStr([ // statements
  15698. 'rtl.createClass(this, "TObject", null, function () {',
  15699. ' this.$init = function () {',
  15700. ' };',
  15701. ' this.$final = function () {',
  15702. ' };',
  15703. ' this.DoIt = function (k) {',
  15704. ' var Result = 0;',
  15705. ' return Result;',
  15706. ' };',
  15707. '});',
  15708. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15709. ' this.DoIt = function (k) {',
  15710. ' var $Self = this;',
  15711. ' var Result = 0;',
  15712. ' function Sub() {',
  15713. ' $mod.TObject.DoIt.call($Self, true);',
  15714. ' };',
  15715. ' Sub();',
  15716. ' $mod.TObject.DoIt.apply(this, arguments);',
  15717. ' $mod.TObject.DoIt.call(this, true);',
  15718. ' return Result;',
  15719. ' };',
  15720. '});',
  15721. '']),
  15722. LinesToStr([ // $mod.$main
  15723. '']));
  15724. end;
  15725. procedure TTestModule.TestClass_TObjectFree;
  15726. begin
  15727. StartProgram(false);
  15728. Add([
  15729. 'type',
  15730. ' TObject = class',
  15731. ' Obj: tobject;',
  15732. ' procedure Free;',
  15733. ' procedure Release;',
  15734. ' end;',
  15735. 'procedure tobject.free;',
  15736. 'begin',
  15737. 'end;',
  15738. 'procedure tobject.release;',
  15739. 'begin',
  15740. ' free;',
  15741. ' if true then free;',
  15742. 'end;',
  15743. 'function DoIt(o: tobject): tobject;',
  15744. 'var l: tobject;',
  15745. 'begin',
  15746. ' o.free;',
  15747. ' o.free();',
  15748. ' l.free;',
  15749. ' l.free();',
  15750. ' o.obj.free;',
  15751. ' o.obj.free();',
  15752. ' with o do obj.free;',
  15753. ' with o do obj.free();',
  15754. ' result.Free;',
  15755. ' result.Free();',
  15756. 'end;',
  15757. 'var o: tobject;',
  15758. ' a: array of tobject;',
  15759. 'begin',
  15760. ' o.free;',
  15761. ' o.obj.free;',
  15762. ' a[1+2].free;',
  15763. '']);
  15764. ConvertProgram;
  15765. CheckSource('TestClass_TObjectFree',
  15766. LinesToStr([ // statements
  15767. 'rtl.createClass(this, "TObject", null, function () {',
  15768. ' this.$init = function () {',
  15769. ' this.Obj = null;',
  15770. ' };',
  15771. ' this.$final = function () {',
  15772. ' this.Obj = undefined;',
  15773. ' };',
  15774. ' this.Free = function () {',
  15775. ' };',
  15776. ' this.Release = function () {',
  15777. ' this.Free();',
  15778. ' if (true) this.Free();',
  15779. ' };',
  15780. '});',
  15781. 'this.DoIt = function (o) {',
  15782. ' var Result = null;',
  15783. ' var l = null;',
  15784. ' o = rtl.freeLoc(o);',
  15785. ' o = rtl.freeLoc(o);',
  15786. ' l = rtl.freeLoc(l);',
  15787. ' l = rtl.freeLoc(l);',
  15788. ' rtl.free(o, "Obj");',
  15789. ' rtl.free(o, "Obj");',
  15790. ' rtl.free(o, "Obj");',
  15791. ' rtl.free(o, "Obj");',
  15792. ' Result = rtl.freeLoc(Result);',
  15793. ' Result = rtl.freeLoc(Result);',
  15794. ' return Result;',
  15795. '};',
  15796. 'this.o = null;',
  15797. 'this.a = [];',
  15798. '']),
  15799. LinesToStr([ // $mod.$main
  15800. 'rtl.free($mod, "o");',
  15801. 'rtl.free($mod.o, "Obj");',
  15802. 'rtl.free($mod.a, 1 + 2);',
  15803. '']));
  15804. end;
  15805. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15806. begin
  15807. StartProgram(false);
  15808. Add([
  15809. 'type',
  15810. ' TObject = class',
  15811. ' Obj: tobject;',
  15812. ' procedure Free;',
  15813. ' end;',
  15814. 'procedure tobject.free;',
  15815. 'begin',
  15816. 'end;',
  15817. 'procedure DoIt(var o: tobject);',
  15818. 'begin',
  15819. ' o.free;',
  15820. ' o.free();',
  15821. 'end;',
  15822. 'begin',
  15823. '']);
  15824. ConvertProgram;
  15825. CheckSource('TestClass_TObjectFree_VarArg',
  15826. LinesToStr([ // statements
  15827. 'rtl.createClass(this, "TObject", null, function () {',
  15828. ' this.$init = function () {',
  15829. ' this.Obj = null;',
  15830. ' };',
  15831. ' this.$final = function () {',
  15832. ' this.Obj = undefined;',
  15833. ' };',
  15834. ' this.Free = function () {',
  15835. ' };',
  15836. '});',
  15837. 'this.DoIt = function (o) {',
  15838. ' o.set(rtl.freeLoc(o.get()));',
  15839. ' o.set(rtl.freeLoc(o.get()));',
  15840. '};',
  15841. '']),
  15842. LinesToStr([ // $mod.$main
  15843. '']));
  15844. end;
  15845. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15846. begin
  15847. StartProgram(false);
  15848. Add([
  15849. 'type',
  15850. ' TObject = class',
  15851. ' constructor Create;',
  15852. ' procedure Free;',
  15853. ' end;',
  15854. 'constructor TObject.Create; begin end;',
  15855. 'procedure tobject.free; begin end;',
  15856. 'begin',
  15857. ' with tobject.create do free;',
  15858. '']);
  15859. ConvertProgram;
  15860. CheckSource('TestClass_TObjectFreeNewInstance',
  15861. LinesToStr([ // statements
  15862. 'rtl.createClass(this, "TObject", null, function () {',
  15863. ' this.$init = function () {',
  15864. ' };',
  15865. ' this.$final = function () {',
  15866. ' };',
  15867. ' this.Create = function () {',
  15868. ' return this;',
  15869. ' };',
  15870. ' this.Free = function () {',
  15871. ' };',
  15872. '});',
  15873. '']),
  15874. LinesToStr([ // $mod.$main
  15875. 'var $with = $mod.TObject.$create("Create");',
  15876. '$with=rtl.freeLoc($with);',
  15877. '']));
  15878. end;
  15879. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15880. begin
  15881. StartProgram(false);
  15882. Add([
  15883. 'type',
  15884. ' TObject = class',
  15885. ' destructor Destroy;',
  15886. ' procedure Free;',
  15887. ' end;',
  15888. 'destructor TObject.Destroy; begin end;',
  15889. 'procedure tobject.free; begin end;',
  15890. 'var o: tobject;',
  15891. 'begin',
  15892. ' o.free;',
  15893. '']);
  15894. Converter.UseLowerCase:=true;
  15895. ConvertProgram;
  15896. CheckSource('TestClass_TObjectFreeLowerCase',
  15897. LinesToStr([ // statements
  15898. 'rtl.createClass(this, "tobject", null, function () {',
  15899. ' this.$init = function () {',
  15900. ' };',
  15901. ' this.$final = function () {',
  15902. ' };',
  15903. ' rtl.tObjectDestroy = "destroy";',
  15904. ' this.destroy = function () {',
  15905. ' };',
  15906. ' this.free = function () {',
  15907. ' };',
  15908. '});',
  15909. 'this.o = null;',
  15910. '']),
  15911. LinesToStr([ // $mod.$main
  15912. 'rtl.free($mod, "o");',
  15913. '']));
  15914. end;
  15915. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15916. begin
  15917. StartProgram(false);
  15918. Add([
  15919. 'type',
  15920. ' TObject = class',
  15921. ' procedure Free;',
  15922. ' function GetObj: tobject; virtual; abstract;',
  15923. ' end;',
  15924. 'procedure tobject.free;',
  15925. 'begin',
  15926. 'end;',
  15927. 'var o: tobject;',
  15928. 'begin',
  15929. ' o.getobj.free;',
  15930. '']);
  15931. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15932. ConvertProgram;
  15933. end;
  15934. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15935. begin
  15936. StartProgram(false);
  15937. Add([
  15938. 'type',
  15939. ' TObject = class',
  15940. ' procedure Free;',
  15941. ' FObj: TObject;',
  15942. ' property Obj: tobject read FObj write FObj;',
  15943. ' end;',
  15944. 'procedure tobject.free;',
  15945. 'begin',
  15946. 'end;',
  15947. 'var o: tobject;',
  15948. 'begin',
  15949. ' o.obj.free;',
  15950. '']);
  15951. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15952. ConvertProgram;
  15953. end;
  15954. procedure TTestModule.TestClass_ForIn;
  15955. begin
  15956. StartProgram(false);
  15957. Add([
  15958. 'type',
  15959. ' TObject = class end;',
  15960. ' TItem = TObject;',
  15961. ' TEnumerator = class',
  15962. ' FCurrent: TItem;',
  15963. ' property Current: TItem read FCurrent;',
  15964. ' function MoveNext: boolean;',
  15965. ' end;',
  15966. ' TBird = class',
  15967. ' function GetEnumerator: TEnumerator;',
  15968. ' end;',
  15969. 'function TEnumerator.MoveNext: boolean;',
  15970. 'begin',
  15971. 'end;',
  15972. 'function TBird.GetEnumerator: TEnumerator;',
  15973. 'begin',
  15974. 'end;',
  15975. 'var',
  15976. ' b: TBird;',
  15977. ' i, i2: TItem;',
  15978. 'begin',
  15979. ' for i in b do i2:=i;']);
  15980. ConvertProgram;
  15981. CheckSource('TestClass_ForIn',
  15982. LinesToStr([ // statements
  15983. 'rtl.createClass(this, "TObject", null, function () {',
  15984. ' this.$init = function () {',
  15985. ' };',
  15986. ' this.$final = function () {',
  15987. ' };',
  15988. '});',
  15989. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15990. ' this.$init = function () {',
  15991. ' $mod.TObject.$init.call(this);',
  15992. ' this.FCurrent = null;',
  15993. ' };',
  15994. ' this.$final = function () {',
  15995. ' this.FCurrent = undefined;',
  15996. ' $mod.TObject.$final.call(this);',
  15997. ' };',
  15998. ' this.MoveNext = function () {',
  15999. ' var Result = false;',
  16000. ' return Result;',
  16001. ' };',
  16002. '});',
  16003. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16004. ' this.GetEnumerator = function () {',
  16005. ' var Result = null;',
  16006. ' return Result;',
  16007. ' };',
  16008. '});',
  16009. 'this.b = null;',
  16010. 'this.i = null;',
  16011. 'this.i2 = null;'
  16012. ]),
  16013. LinesToStr([ // $mod.$main
  16014. 'var $in = $mod.b.GetEnumerator();',
  16015. 'try {',
  16016. ' while ($in.MoveNext()){',
  16017. ' $mod.i = $in.FCurrent;',
  16018. ' $mod.i2 = $mod.i;',
  16019. ' }',
  16020. '} finally {',
  16021. ' $in = rtl.freeLoc($in)',
  16022. '};',
  16023. '']));
  16024. end;
  16025. procedure TTestModule.TestClass_DispatchMessage;
  16026. begin
  16027. StartProgram(false);
  16028. Add([
  16029. 'type',
  16030. ' TObject = class',
  16031. ' {$DispatchField DispInt}',
  16032. ' procedure Dispatch(var Msg); virtual; abstract;',
  16033. ' {$DispatchStrField DispStr}',
  16034. ' procedure DispatchStr(var Msg); virtual; abstract;',
  16035. ' end;',
  16036. ' THopMsg = record',
  16037. ' DispInt: longint;',
  16038. ' end;',
  16039. ' TPutMsg = record',
  16040. ' DispStr: string;',
  16041. ' end;',
  16042. ' TBird = class',
  16043. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  16044. ' procedure Run; overload; virtual; abstract;',
  16045. ' procedure Run(var Msg); overload; message ''Fast'';',
  16046. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  16047. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  16048. ' end;',
  16049. 'procedure TBird.Run(var Msg);',
  16050. 'begin',
  16051. 'end;',
  16052. 'begin',
  16053. '']);
  16054. ConvertProgram;
  16055. CheckSource('TestClass_Message',
  16056. LinesToStr([ // statements
  16057. 'rtl.createClass(this, "TObject", null, function () {',
  16058. ' this.$init = function () {',
  16059. ' };',
  16060. ' this.$final = function () {',
  16061. ' };',
  16062. '});',
  16063. 'rtl.recNewT(this, "THopMsg", function () {',
  16064. ' this.DispInt = 0;',
  16065. ' this.$eq = function (b) {',
  16066. ' return this.DispInt === b.DispInt;',
  16067. ' };',
  16068. ' this.$assign = function (s) {',
  16069. ' this.DispInt = s.DispInt;',
  16070. ' return this;',
  16071. ' };',
  16072. '});',
  16073. 'rtl.recNewT(this, "TPutMsg", function () {',
  16074. ' this.DispStr = "";',
  16075. ' this.$eq = function (b) {',
  16076. ' return this.DispStr === b.DispStr;',
  16077. ' };',
  16078. ' this.$assign = function (s) {',
  16079. ' this.DispStr = s.DispStr;',
  16080. ' return this;',
  16081. ' };',
  16082. '});',
  16083. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16084. ' this.Run$1 = function (Msg) {',
  16085. ' };',
  16086. ' this.$msgint = {',
  16087. ' "2": "Fly",',
  16088. ' "3": "Hop"',
  16089. ' };',
  16090. ' this.$msgstr = {',
  16091. ' Fast: "Run$1",',
  16092. ' foo: "Put"',
  16093. ' };',
  16094. '});',
  16095. '']),
  16096. LinesToStr([ // $mod.$main
  16097. '']));
  16098. end;
  16099. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  16100. begin
  16101. StartProgram(false);
  16102. Add([
  16103. 'type',
  16104. ' TObject = class',
  16105. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  16106. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  16107. ' end;',
  16108. 'begin',
  16109. '']);
  16110. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  16111. ConvertProgram;
  16112. end;
  16113. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  16114. begin
  16115. StartProgram(false);
  16116. Add([
  16117. 'type',
  16118. ' TObject = class',
  16119. ' {$dispatchfield Msg}',
  16120. ' procedure Dispatch(var Msg); virtual; abstract;',
  16121. ' end;',
  16122. ' TFlyMsg = record',
  16123. ' FlyId: longint;',
  16124. ' end;',
  16125. ' TBird = class',
  16126. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  16127. ' end;',
  16128. 'begin',
  16129. '']);
  16130. ConvertProgram;
  16131. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  16132. end;
  16133. procedure TTestModule.TestClassOf_Create;
  16134. begin
  16135. StartProgram(false);
  16136. Add('type');
  16137. Add(' TObject = class');
  16138. Add(' constructor Create;');
  16139. Add(' end;');
  16140. Add(' TClass = class of TObject;');
  16141. Add('constructor tobject.create; begin end;');
  16142. Add('var');
  16143. Add(' Obj: tobject;');
  16144. Add(' C: tclass;');
  16145. Add('begin');
  16146. Add(' obj:=C.create;');
  16147. Add(' with c do obj:=create;');
  16148. ConvertProgram;
  16149. CheckSource('TestClassOf_Create',
  16150. LinesToStr([ // statements
  16151. 'rtl.createClass(this, "TObject", null, function () {',
  16152. ' this.$init = function () {',
  16153. ' };',
  16154. ' this.$final = function () {',
  16155. ' };',
  16156. ' this.Create = function () {',
  16157. ' return this;',
  16158. ' };',
  16159. '});',
  16160. 'this.Obj = null;',
  16161. 'this.C = null;'
  16162. ]),
  16163. LinesToStr([ // $mod.$main
  16164. '$mod.Obj = $mod.C.$create("Create");',
  16165. 'var $with = $mod.C;',
  16166. '$mod.Obj = $with.$create("Create");',
  16167. '']));
  16168. end;
  16169. procedure TTestModule.TestClassOf_Call;
  16170. begin
  16171. StartProgram(false);
  16172. Add('type');
  16173. Add(' TObject = class');
  16174. Add(' class procedure DoIt;');
  16175. Add(' end;');
  16176. Add(' TClass = class of TObject;');
  16177. Add('class procedure tobject.doit; begin end;');
  16178. Add('var');
  16179. Add(' C: tclass;');
  16180. Add('begin');
  16181. Add(' c.doit;');
  16182. Add(' with c do doit;');
  16183. ConvertProgram;
  16184. CheckSource('TestClassOf_Call',
  16185. LinesToStr([ // statements
  16186. 'rtl.createClass(this, "TObject", null, function () {',
  16187. ' this.$init = function () {',
  16188. ' };',
  16189. ' this.$final = function () {',
  16190. ' };',
  16191. ' this.DoIt = function () {',
  16192. ' };',
  16193. '});',
  16194. 'this.C = null;'
  16195. ]),
  16196. LinesToStr([ // $mod.$main
  16197. '$mod.C.DoIt();',
  16198. 'var $with = $mod.C;',
  16199. '$with.DoIt();',
  16200. '']));
  16201. end;
  16202. procedure TTestModule.TestClassOf_Assign;
  16203. begin
  16204. StartProgram(false);
  16205. Add('type');
  16206. Add(' TClass = class of TObject;');
  16207. Add(' TObject = class');
  16208. Add(' ClassType: TClass; ');
  16209. Add(' end;');
  16210. Add('var');
  16211. Add(' Obj: tobject;');
  16212. Add(' C: tclass;');
  16213. Add('begin');
  16214. Add(' c:=nil;');
  16215. Add(' c:=obj.classtype;');
  16216. ConvertProgram;
  16217. CheckSource('TestClassOf_Assign',
  16218. LinesToStr([ // statements
  16219. 'rtl.createClass(this, "TObject", null, function () {',
  16220. ' this.$init = function () {',
  16221. ' this.ClassType = null;',
  16222. ' };',
  16223. ' this.$final = function () {',
  16224. ' this.ClassType = undefined;',
  16225. ' };',
  16226. '});',
  16227. 'this.Obj = null;',
  16228. 'this.C = null;'
  16229. ]),
  16230. LinesToStr([ // $mod.$main
  16231. '$mod.C = null;',
  16232. '$mod.C = $mod.Obj.ClassType;',
  16233. '']));
  16234. end;
  16235. procedure TTestModule.TestClassOf_Is;
  16236. begin
  16237. StartProgram(false);
  16238. Add('type');
  16239. Add(' TClass = class of TObject;');
  16240. Add(' TObject = class');
  16241. Add(' end;');
  16242. Add(' TCar = class');
  16243. Add(' end;');
  16244. Add(' TCars = class of TCar;');
  16245. Add('var');
  16246. Add(' Obj: tobject;');
  16247. Add(' C: tclass;');
  16248. Add(' Cars: tcars;');
  16249. Add('begin');
  16250. Add(' if c is tcar then ;');
  16251. Add(' if c is tcars then ;');
  16252. ConvertProgram;
  16253. CheckSource('TestClassOf_Is',
  16254. LinesToStr([ // statements
  16255. 'rtl.createClass(this, "TObject", null, function () {',
  16256. ' this.$init = function () {',
  16257. ' };',
  16258. ' this.$final = function () {',
  16259. ' };',
  16260. '});',
  16261. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  16262. '});',
  16263. 'this.Obj = null;',
  16264. 'this.C = null;',
  16265. 'this.Cars = null;'
  16266. ]),
  16267. LinesToStr([ // $mod.$main
  16268. 'if(rtl.is($mod.C,$mod.TCar));',
  16269. 'if(rtl.is($mod.C,$mod.TCar));',
  16270. '']));
  16271. end;
  16272. procedure TTestModule.TestClassOf_Compare;
  16273. begin
  16274. StartProgram(false);
  16275. Add('type');
  16276. Add(' TClass = class of TObject;');
  16277. Add(' TObject = class');
  16278. Add(' ClassType: TClass; ');
  16279. Add(' end;');
  16280. Add('var');
  16281. Add(' b: boolean;');
  16282. Add(' Obj: tobject;');
  16283. Add(' C: tclass;');
  16284. Add('begin');
  16285. Add(' b:=c=nil;');
  16286. Add(' b:=nil=c;');
  16287. Add(' b:=c=obj.classtype;');
  16288. Add(' b:=obj.classtype=c;');
  16289. Add(' b:=c=TObject;');
  16290. Add(' b:=TObject=c;');
  16291. Add(' b:=c<>nil;');
  16292. Add(' b:=nil<>c;');
  16293. Add(' b:=c<>obj.classtype;');
  16294. Add(' b:=obj.classtype<>c;');
  16295. Add(' b:=c<>TObject;');
  16296. Add(' b:=TObject<>c;');
  16297. ConvertProgram;
  16298. CheckSource('TestClassOf_Compare',
  16299. LinesToStr([ // statements
  16300. 'rtl.createClass(this, "TObject", null, function () {',
  16301. ' this.$init = function () {',
  16302. ' this.ClassType = null;',
  16303. ' };',
  16304. ' this.$final = function () {',
  16305. ' this.ClassType = undefined;',
  16306. ' };',
  16307. '});',
  16308. 'this.b = false;',
  16309. 'this.Obj = null;',
  16310. 'this.C = null;'
  16311. ]),
  16312. LinesToStr([ // $mod.$main
  16313. '$mod.b = $mod.C === null;',
  16314. '$mod.b = null === $mod.C;',
  16315. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  16316. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  16317. '$mod.b = $mod.C === $mod.TObject;',
  16318. '$mod.b = $mod.TObject === $mod.C;',
  16319. '$mod.b = $mod.C !== null;',
  16320. '$mod.b = null !== $mod.C;',
  16321. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  16322. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  16323. '$mod.b = $mod.C !== $mod.TObject;',
  16324. '$mod.b = $mod.TObject !== $mod.C;',
  16325. '']));
  16326. end;
  16327. procedure TTestModule.TestClassOf_ClassVar;
  16328. begin
  16329. StartProgram(false);
  16330. Add('type');
  16331. Add(' TObject = class');
  16332. Add(' class var id: longint;');
  16333. Add(' end;');
  16334. Add(' TClass = class of TObject;');
  16335. Add('var');
  16336. Add(' C: tclass;');
  16337. Add('begin');
  16338. Add(' C.id:=C.id;');
  16339. ConvertProgram;
  16340. CheckSource('TestClassOf_ClassVar',
  16341. LinesToStr([ // statements
  16342. 'rtl.createClass(this, "TObject", null, function () {',
  16343. ' this.id = 0;',
  16344. ' this.$init = function () {',
  16345. ' };',
  16346. ' this.$final = function () {',
  16347. ' };',
  16348. '});',
  16349. 'this.C = null;'
  16350. ]),
  16351. LinesToStr([ // $mod.$main
  16352. '$mod.TObject.id = $mod.C.id;',
  16353. '']));
  16354. end;
  16355. procedure TTestModule.TestClassOf_ClassMethod;
  16356. begin
  16357. StartProgram(false);
  16358. Add('type');
  16359. Add(' TObject = class');
  16360. Add(' class function DoIt(i: longint = 0): longint;');
  16361. Add(' end;');
  16362. Add(' TClass = class of TObject;');
  16363. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  16364. Add('var');
  16365. Add(' i: longint;');
  16366. Add(' C: tclass;');
  16367. Add('begin');
  16368. Add(' C.DoIt;');
  16369. Add(' C.DoIt();');
  16370. Add(' i:=C.DoIt;');
  16371. Add(' i:=C.DoIt();');
  16372. ConvertProgram;
  16373. CheckSource('TestClassOf_ClassMethod',
  16374. LinesToStr([ // statements
  16375. 'rtl.createClass(this, "TObject", null, function () {',
  16376. ' this.$init = function () {',
  16377. ' };',
  16378. ' this.$final = function () {',
  16379. ' };',
  16380. ' this.DoIt = function (i) {',
  16381. ' var Result = 0;',
  16382. ' return Result;',
  16383. ' };',
  16384. '});',
  16385. 'this.i = 0;',
  16386. 'this.C = null;'
  16387. ]),
  16388. LinesToStr([ // $mod.$main
  16389. '$mod.C.DoIt(0);',
  16390. '$mod.C.DoIt(0);',
  16391. '$mod.i = $mod.C.DoIt(0);',
  16392. '$mod.i = $mod.C.DoIt(0);',
  16393. '']));
  16394. end;
  16395. procedure TTestModule.TestClassOf_ClassProperty;
  16396. begin
  16397. StartProgram(false);
  16398. Add([
  16399. 'type',
  16400. ' TObject = class',
  16401. ' class var FA: longint;',
  16402. ' class function GetA: longint;',
  16403. ' class procedure SetA(Value: longint);',
  16404. ' class property pA: longint read fa write fa;',
  16405. ' class property pB: longint read geta write seta;',
  16406. ' end;',
  16407. ' TObjectClass = class of tobject;',
  16408. 'class function tobject.geta: longint; begin end;',
  16409. 'class procedure tobject.seta(value: longint); begin end;',
  16410. 'var',
  16411. ' b: boolean;',
  16412. ' Obj: tobject;',
  16413. ' Cla: tobjectclass;',
  16414. 'begin',
  16415. ' obj.pa:=obj.pa;',
  16416. ' obj.pb:=obj.pb;',
  16417. ' b:=obj.pa=4;',
  16418. ' b:=obj.pb=obj.pb;',
  16419. ' b:=5=obj.pa;',
  16420. ' cla.pa:=6;',
  16421. ' cla.pa:=cla.pa;',
  16422. ' cla.pb:=cla.pb;',
  16423. ' b:=cla.pa=7;',
  16424. ' b:=cla.pb=cla.pb;',
  16425. ' b:=8=cla.pa;',
  16426. ' tobject.pa:=9;',
  16427. ' tobject.pb:=tobject.pb;',
  16428. ' b:=tobject.pa=10;',
  16429. ' b:=11=tobject.pa;',
  16430. '']);
  16431. ConvertProgram;
  16432. CheckSource('TestClassOf_ClassProperty',
  16433. LinesToStr([ // statements
  16434. 'rtl.createClass(this, "TObject", null, function () {',
  16435. ' this.FA = 0;',
  16436. ' this.$init = function () {',
  16437. ' };',
  16438. ' this.$final = function () {',
  16439. ' };',
  16440. ' this.GetA = function () {',
  16441. ' var Result = 0;',
  16442. ' return Result;',
  16443. ' };',
  16444. ' this.SetA = function (Value) {',
  16445. ' };',
  16446. '});',
  16447. 'this.b = false;',
  16448. 'this.Obj = null;',
  16449. 'this.Cla = null;'
  16450. ]),
  16451. LinesToStr([ // $mod.$main
  16452. '$mod.TObject.FA = $mod.Obj.FA;',
  16453. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16454. '$mod.b = $mod.Obj.FA === 4;',
  16455. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16456. '$mod.b = 5 === $mod.Obj.FA;',
  16457. '$mod.TObject.FA = 6;',
  16458. '$mod.TObject.FA = $mod.Cla.FA;',
  16459. '$mod.Cla.SetA($mod.Cla.GetA());',
  16460. '$mod.b = $mod.Cla.FA === 7;',
  16461. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16462. '$mod.b = 8 === $mod.Cla.FA;',
  16463. '$mod.TObject.FA = 9;',
  16464. '$mod.TObject.SetA($mod.TObject.GetA());',
  16465. '$mod.b = $mod.TObject.FA === 10;',
  16466. '$mod.b = 11 === $mod.TObject.FA;',
  16467. '']));
  16468. end;
  16469. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16470. begin
  16471. StartProgram(false);
  16472. Add('type');
  16473. Add(' TObject = class');
  16474. Add(' class var GlobalId: longint;');
  16475. Add(' class procedure ProcA;');
  16476. Add(' end;');
  16477. Add('class procedure tobject.proca;');
  16478. Add('var b: boolean;');
  16479. Add('begin');
  16480. Add(' b:=self=nil;');
  16481. Add(' b:=self.globalid=3;');
  16482. Add(' b:=4=self.globalid;');
  16483. Add(' self.globalid:=5;');
  16484. Add(' self.proca;');
  16485. Add('end;');
  16486. Add('begin');
  16487. ConvertProgram;
  16488. CheckSource('TestClassOf_ClassMethodSelf',
  16489. LinesToStr([ // statements
  16490. 'rtl.createClass(this, "TObject", null, function () {',
  16491. ' this.GlobalId = 0;',
  16492. ' this.$init = function () {',
  16493. ' };',
  16494. ' this.$final = function () {',
  16495. ' };',
  16496. ' this.ProcA = function () {',
  16497. ' var b = false;',
  16498. ' b = this === null;',
  16499. ' b = this.GlobalId === 3;',
  16500. ' b = 4 === this.GlobalId;',
  16501. ' $mod.TObject.GlobalId = 5;',
  16502. ' this.ProcA();',
  16503. ' };',
  16504. '});'
  16505. ]),
  16506. LinesToStr([ // $mod.$main
  16507. '']));
  16508. end;
  16509. procedure TTestModule.TestClassOf_TypeCast;
  16510. begin
  16511. StartProgram(false);
  16512. Add('type');
  16513. Add(' TObject = class');
  16514. Add(' class procedure {#TObject_DoIt}DoIt;');
  16515. Add(' end;');
  16516. Add(' TClass = class of TObject;');
  16517. Add(' TMobile = class');
  16518. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16519. Add(' end;');
  16520. Add(' TMobileClass = class of TMobile;');
  16521. Add(' TCar = class(TMobile)');
  16522. Add(' class procedure {#TCar_DoIt}DoIt;');
  16523. Add(' end;');
  16524. Add(' TCarClass = class of TCar;');
  16525. Add('class procedure TObject.DoIt;');
  16526. Add('begin');
  16527. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16528. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16529. Add('end;');
  16530. Add('class procedure TMobile.DoIt;');
  16531. Add('begin');
  16532. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16533. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16534. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16535. Add('end;');
  16536. Add('class procedure TCar.DoIt; begin end;');
  16537. Add('var');
  16538. Add(' ObjC: TClass;');
  16539. Add(' MobileC: TMobileClass;');
  16540. Add(' CarC: TCarClass;');
  16541. Add('begin');
  16542. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16543. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16544. Add(' CarC.{@TCar_DoIt}DoIt;');
  16545. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16546. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16547. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16548. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16549. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16550. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16551. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16552. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16553. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16554. ConvertProgram;
  16555. CheckSource('TestClassOf_TypeCast',
  16556. LinesToStr([ // statements
  16557. 'rtl.createClass(this, "TObject", null, function () {',
  16558. ' this.$init = function () {',
  16559. ' };',
  16560. ' this.$final = function () {',
  16561. ' };',
  16562. ' this.DoIt = function () {',
  16563. ' this.DoIt();',
  16564. ' this.DoIt$1();',
  16565. ' };',
  16566. '});',
  16567. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16568. ' this.DoIt$1 = function () {',
  16569. ' this.DoIt();',
  16570. ' this.DoIt$1();',
  16571. ' this.DoIt$2();',
  16572. ' };',
  16573. '});',
  16574. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16575. ' this.DoIt$2 = function () {',
  16576. ' };',
  16577. '});',
  16578. 'this.ObjC = null;',
  16579. 'this.MobileC = null;',
  16580. 'this.CarC = null;',
  16581. '']),
  16582. LinesToStr([ // $mod.$main
  16583. '$mod.ObjC.DoIt();',
  16584. '$mod.MobileC.DoIt$1();',
  16585. '$mod.CarC.DoIt$2();',
  16586. '$mod.ObjC.DoIt();',
  16587. '$mod.ObjC.DoIt$1();',
  16588. '$mod.ObjC.DoIt$2();',
  16589. '$mod.MobileC.DoIt();',
  16590. '$mod.MobileC.DoIt$1();',
  16591. '$mod.MobileC.DoIt$2();',
  16592. '$mod.CarC.DoIt();',
  16593. '$mod.CarC.DoIt$1();',
  16594. '$mod.CarC.DoIt$2();',
  16595. '']));
  16596. end;
  16597. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16598. begin
  16599. StartProgram(false);
  16600. Add('type');
  16601. Add(' TObject = class');
  16602. Add(' function CurNow: longint; ');
  16603. Add(' class function Now: longint; ');
  16604. Add(' end;');
  16605. Add('function TObject.CurNow: longint; begin end;');
  16606. Add('class function TObject.Now: longint; begin end;');
  16607. Add('var');
  16608. Add(' Obj: tobject;');
  16609. Add(' vI: longint;');
  16610. Add('begin');
  16611. Add(' obj.curnow;');
  16612. Add(' vi:=obj.curnow;');
  16613. Add(' tobject.now;');
  16614. Add(' vi:=tobject.now;');
  16615. ConvertProgram;
  16616. CheckSource('TestClassOf_ImplicitFunctionCall',
  16617. LinesToStr([ // statements
  16618. 'rtl.createClass(this, "TObject", null, function () {',
  16619. ' this.$init = function () {',
  16620. ' };',
  16621. ' this.$final = function () {',
  16622. ' };',
  16623. ' this.CurNow = function () {',
  16624. ' var Result = 0;',
  16625. ' return Result;',
  16626. ' };',
  16627. ' this.Now = function () {',
  16628. ' var Result = 0;',
  16629. ' return Result;',
  16630. ' };',
  16631. '});',
  16632. 'this.Obj = null;',
  16633. 'this.vI = 0;',
  16634. '']),
  16635. LinesToStr([ // $mod.$main
  16636. '$mod.Obj.CurNow();',
  16637. '$mod.vI = $mod.Obj.CurNow();',
  16638. '$mod.TObject.Now();',
  16639. '$mod.vI = $mod.TObject.Now();',
  16640. '']));
  16641. end;
  16642. procedure TTestModule.TestClassOf_Const;
  16643. begin
  16644. StartProgram(false);
  16645. Add([
  16646. 'type',
  16647. ' TObject = class',
  16648. ' end;',
  16649. ' TBird = TObject;',
  16650. ' TBirds = class of TBird;',
  16651. ' TEagles = TBirds;',
  16652. ' THawk = class(TBird);',
  16653. 'const',
  16654. ' Hawk: TEagles = THawk;',
  16655. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16656. ' TBird,',
  16657. ' THawk',
  16658. ' );',
  16659. 'begin']);
  16660. ConvertProgram;
  16661. CheckSource('TestClassOf_Const',
  16662. LinesToStr([ // statements
  16663. 'rtl.createClass(this, "TObject", null, function () {',
  16664. ' this.$init = function () {',
  16665. ' };',
  16666. ' this.$final = function () {',
  16667. ' };',
  16668. '});',
  16669. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16670. '});',
  16671. 'this.Hawk = this.THawk;',
  16672. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16673. '']),
  16674. LinesToStr([ // $mod.$main
  16675. '']));
  16676. end;
  16677. procedure TTestModule.TestNestedClass_Alias;
  16678. begin
  16679. WithTypeInfo:=true;
  16680. StartProgram(false);
  16681. Add([
  16682. 'type',
  16683. ' TObject = class',
  16684. ' type TNested = type longint;',
  16685. ' end;',
  16686. 'type TAlias = type tobject.tnested;',
  16687. 'var i: tobject.tnested = 3;',
  16688. 'var j: TAlias = 4;',
  16689. 'begin',
  16690. ' if typeinfo(TAlias)=nil then ;',
  16691. ' if typeinfo(tobject.tnested)=nil then ;',
  16692. '']);
  16693. ConvertProgram;
  16694. CheckSource('TestNestedClass_Alias',
  16695. LinesToStr([ // statements
  16696. 'rtl.createClass(this, "TObject", null, function () {',
  16697. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16698. ' this.$init = function () {',
  16699. ' };',
  16700. ' this.$final = function () {',
  16701. ' };',
  16702. '});',
  16703. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16704. 'this.i = 3;',
  16705. 'this.j = 4;',
  16706. '']),
  16707. LinesToStr([ // $mod.$main
  16708. 'if ($mod.$rtti["TAlias"] === null) ;',
  16709. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16710. '']));
  16711. end;
  16712. procedure TTestModule.TestNestedClass_Record;
  16713. begin
  16714. WithTypeInfo:=true;
  16715. StartProgram(false);
  16716. Add([
  16717. 'type',
  16718. ' TObject = class',
  16719. ' type TPoint = record',
  16720. ' x,y: byte;',
  16721. ' end;',
  16722. ' procedure DoIt(t: TPoint);',
  16723. ' end;',
  16724. 'procedure tobject.DoIt(t: TPoint);',
  16725. 'var p: TPoint;',
  16726. 'begin',
  16727. ' t.x:=t.y;',
  16728. ' p:=t;',
  16729. 'end;',
  16730. 'var',
  16731. ' p: tobject.tpoint = (x:2; y:4);',
  16732. ' o: TObject;',
  16733. 'begin',
  16734. ' p:=p;',
  16735. ' o.doit(p);',
  16736. '']);
  16737. ConvertProgram;
  16738. CheckSource('TestNestedClass_Record',
  16739. LinesToStr([ // statements
  16740. 'rtl.createClass(this, "TObject", null, function () {',
  16741. ' rtl.recNewT(this, "TPoint", function () {',
  16742. ' this.x = 0;',
  16743. ' this.y = 0;',
  16744. ' this.$eq = function (b) {',
  16745. ' return (this.x === b.x) && (this.y === b.y);',
  16746. ' };',
  16747. ' this.$assign = function (s) {',
  16748. ' this.x = s.x;',
  16749. ' this.y = s.y;',
  16750. ' return this;',
  16751. ' };',
  16752. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16753. ' $r.addField("x", rtl.byte);',
  16754. ' $r.addField("y", rtl.byte);',
  16755. ' });',
  16756. ' this.$init = function () {',
  16757. ' };',
  16758. ' this.$final = function () {',
  16759. ' };',
  16760. ' this.DoIt = function (t) {',
  16761. ' var p = this.TPoint.$new();',
  16762. ' t.x = t.y;',
  16763. ' p.$assign(t);',
  16764. ' };',
  16765. '});',
  16766. 'this.p = this.TObject.TPoint.$clone({',
  16767. ' x: 2,',
  16768. ' y: 4',
  16769. '});',
  16770. 'this.o = null;',
  16771. '']),
  16772. LinesToStr([ // $mod.$main
  16773. '$mod.p.$assign($mod.p);',
  16774. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16775. '']));
  16776. end;
  16777. procedure TTestModule.TestNestedClass_Class;
  16778. begin
  16779. WithTypeInfo:=true;
  16780. StartProgram(false);
  16781. Add([
  16782. 'type',
  16783. ' TObject = class end;',
  16784. ' TBird = class',
  16785. ' type TLeg = class',
  16786. ' FId: longint;',
  16787. ' constructor Create;',
  16788. ' function Create(i: longint): TLeg;',
  16789. ' end;',
  16790. ' function DoIt(b: TBird): Tleg;',
  16791. ' end;',
  16792. 'constructor tbird.tleg.create;',
  16793. 'begin',
  16794. ' FId:=3;',
  16795. 'end;',
  16796. 'function tbird.tleg.Create(i: longint): TLeg;',
  16797. 'begin',
  16798. ' Create;',
  16799. ' Result:=TLeg.Create;',
  16800. ' Result:=TBird.TLeg.Create;',
  16801. ' Result:=Create(3);',
  16802. ' FId:=i;',
  16803. 'end;',
  16804. 'function tbird.DoIt(b: tbird): tleg;',
  16805. 'begin',
  16806. ' Result.Create;',
  16807. ' Result:=TLeg.Create;',
  16808. ' Result:=TBird.TLeg.Create;',
  16809. ' Result:=Result.Create(3);',
  16810. 'end;',
  16811. 'var',
  16812. ' b: Tbird.tleg;',
  16813. 'begin',
  16814. ' b.Create;',
  16815. ' b:=TBird.TLeg.Create;',
  16816. ' b:=b.Create(3);',
  16817. '']);
  16818. ConvertProgram;
  16819. CheckSource('TestNestedClass_Class',
  16820. LinesToStr([ // statements
  16821. 'rtl.createClass(this, "TObject", null, function () {',
  16822. ' this.$init = function () {',
  16823. ' };',
  16824. ' this.$final = function () {',
  16825. ' };',
  16826. '});',
  16827. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16828. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16829. ' this.$init = function () {',
  16830. ' $mod.TObject.$init.call(this);',
  16831. ' this.FId = 0;',
  16832. ' };',
  16833. ' this.Create = function () {',
  16834. ' this.FId = 3;',
  16835. ' return this;',
  16836. ' };',
  16837. ' this.Create$1 = function (i) {',
  16838. ' var Result = null;',
  16839. ' this.Create();',
  16840. ' Result = $mod.TBird.TLeg.$create("Create");',
  16841. ' Result = $mod.TBird.TLeg.$create("Create");',
  16842. ' Result = this.Create$1(3);',
  16843. ' this.FId = i;',
  16844. ' return Result;',
  16845. ' };',
  16846. ' }, "TBird.TLeg");',
  16847. ' this.DoIt = function (b) {',
  16848. ' var Result = null;',
  16849. ' Result.Create();',
  16850. ' Result = this.TLeg.$create("Create");',
  16851. ' Result = $mod.TBird.TLeg.$create("Create");',
  16852. ' Result = Result.Create$1(3);',
  16853. ' return Result;',
  16854. ' };',
  16855. '});',
  16856. 'this.b = null;',
  16857. '']),
  16858. LinesToStr([ // $mod.$main
  16859. '$mod.b.Create();',
  16860. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16861. '$mod.b = $mod.b.Create$1(3);',
  16862. '']));
  16863. end;
  16864. procedure TTestModule.TestExternalClass_Var;
  16865. begin
  16866. StartProgram(false);
  16867. Add([
  16868. '{$modeswitch externalclass}',
  16869. 'type',
  16870. ' TExtA = class external name ''ExtObj''',
  16871. ' Id: longint external name ''$Id'';',
  16872. ' B: longint;',
  16873. ' end;',
  16874. 'var Obj: TExtA;',
  16875. 'begin',
  16876. ' obj.id:=obj.id+1;',
  16877. ' obj.B:=obj.B+1;']);
  16878. ConvertProgram;
  16879. CheckSource('TestExternalClass_Var',
  16880. LinesToStr([ // statements
  16881. 'this.Obj = null;',
  16882. '']),
  16883. LinesToStr([ // $mod.$main
  16884. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16885. '$mod.Obj.B = $mod.Obj.B + 1;',
  16886. '']));
  16887. end;
  16888. procedure TTestModule.TestExternalClass_Const;
  16889. begin
  16890. StartProgram(false);
  16891. Add([
  16892. '{$modeswitch externalclass}',
  16893. 'type',
  16894. ' TExtA = class external name ''ExtObj''',
  16895. ' const Two: longint = 2;',
  16896. ' const Three = 3;',
  16897. ' const Id: longint;',
  16898. ' end;',
  16899. ' TExtB = class external name ''ExtB''',
  16900. ' A: TExtA;',
  16901. ' end;',
  16902. 'var',
  16903. ' A: texta;',
  16904. ' B: textb;',
  16905. ' i: longint;',
  16906. 'begin',
  16907. ' i:=a.two;',
  16908. ' i:=texta.two;',
  16909. ' i:=a.three;',
  16910. ' i:=texta.three;',
  16911. ' i:=a.id;',
  16912. ' i:=texta.id;',
  16913. '']);
  16914. ConvertProgram;
  16915. CheckSource('TestExternalClass_Const',
  16916. LinesToStr([ // statements
  16917. 'this.A = null;',
  16918. 'this.B = null;',
  16919. 'this.i = 0;',
  16920. '']),
  16921. LinesToStr([ // $mod.$main
  16922. '$mod.i = 2;',
  16923. '$mod.i = 2;',
  16924. '$mod.i = 3;',
  16925. '$mod.i = 3;',
  16926. '$mod.i = $mod.A.Id;',
  16927. '$mod.i = ExtObj.Id;',
  16928. '']));
  16929. end;
  16930. procedure TTestModule.TestExternalClass_Dollar;
  16931. begin
  16932. StartProgram(false);
  16933. Add([
  16934. '{$modeswitch externalclass}',
  16935. 'type',
  16936. ' TExtA = class external name ''$''',
  16937. ' Id: longint external name ''$'';',
  16938. ' function Bla(i: longint): longint; external name ''$'';',
  16939. ' end;',
  16940. 'function dollar(k: longint): longint; external name ''$'';',
  16941. 'var Obj: TExtA;',
  16942. 'begin',
  16943. ' dollar(1);',
  16944. ' obj.id:=obj.id+2;',
  16945. ' obj.Bla(3);',
  16946. '']);
  16947. ConvertProgram;
  16948. CheckSource('TestExternalClass_Dollar',
  16949. LinesToStr([ // statements
  16950. 'this.Obj = null;',
  16951. '']),
  16952. LinesToStr([ // $mod.$main
  16953. '$(1);',
  16954. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16955. '$mod.Obj.$(3);',
  16956. '']));
  16957. end;
  16958. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16959. begin
  16960. StartProgram(false);
  16961. Add('{$modeswitch externalclass}');
  16962. Add('type');
  16963. Add(' TExtA = class external name ''ExtA''');
  16964. Add(' Id: longint external name ''$Id'';');
  16965. Add(' end;');
  16966. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16967. Add(' Id: longint;');
  16968. Add(' end;');
  16969. Add('begin');
  16970. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16971. ConvertProgram;
  16972. end;
  16973. procedure TTestModule.TestExternalClass_Method;
  16974. begin
  16975. StartProgram(false);
  16976. Add(['{$modeswitch externalclass}',
  16977. 'type',
  16978. ' TExtA = class external name ''ExtObj''',
  16979. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16980. ' procedure DoSome(Id: longint = 1);',
  16981. ' end;',
  16982. 'var Obj: texta;',
  16983. 'begin',
  16984. ' obj.doit;',
  16985. ' obj.doit();',
  16986. ' obj.doit(2);',
  16987. ' with obj do begin',
  16988. ' doit;',
  16989. ' doit();',
  16990. ' doit(3);',
  16991. ' end;']);
  16992. ConvertProgram;
  16993. CheckSource('TestExternalClass_Method',
  16994. LinesToStr([ // statements
  16995. 'this.Obj = null;',
  16996. '']),
  16997. LinesToStr([ // $mod.$main
  16998. '$mod.Obj.$Execute(1);',
  16999. '$mod.Obj.$Execute(1);',
  17000. '$mod.Obj.$Execute(2);',
  17001. 'var $with = $mod.Obj;',
  17002. '$with.$Execute(1);',
  17003. '$with.$Execute(1);',
  17004. '$with.$Execute(3);',
  17005. '']));
  17006. end;
  17007. procedure TTestModule.TestExternalClass_ClassMethod;
  17008. begin
  17009. StartProgram(false);
  17010. Add([
  17011. '{$modeswitch externalclass}',
  17012. 'type',
  17013. ' TExtA = class external name ''ExtObj''',
  17014. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17015. ' end;',
  17016. ' TExtB = TExtA;',
  17017. 'var p: Pointer;',
  17018. 'begin',
  17019. ' texta.doit;',
  17020. ' texta.doit();',
  17021. ' texta.doit(2);',
  17022. ' p:[email protected];',
  17023. ' with texta do begin',
  17024. ' doit;',
  17025. ' doit();',
  17026. ' doit(3);',
  17027. ' p:=@DoIt;',
  17028. ' end;',
  17029. ' textb.doit;',
  17030. ' textb.doit();',
  17031. ' textb.doit(4);',
  17032. ' with textb do begin',
  17033. ' doit;',
  17034. ' doit();',
  17035. ' doit(5);',
  17036. ' end;',
  17037. '']);
  17038. ConvertProgram;
  17039. CheckSource('TestExternalClass_ClassMethod',
  17040. LinesToStr([ // statements
  17041. 'this.p = null;',
  17042. '']),
  17043. LinesToStr([ // $mod.$main
  17044. 'ExtObj.$Execute(1);',
  17045. 'ExtObj.$Execute(1);',
  17046. 'ExtObj.$Execute(2);',
  17047. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17048. 'ExtObj.$Execute(1);',
  17049. 'ExtObj.$Execute(1);',
  17050. 'ExtObj.$Execute(3);',
  17051. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17052. 'ExtObj.$Execute(1);',
  17053. 'ExtObj.$Execute(1);',
  17054. 'ExtObj.$Execute(4);',
  17055. 'ExtObj.$Execute(1);',
  17056. 'ExtObj.$Execute(1);',
  17057. 'ExtObj.$Execute(5);',
  17058. '']));
  17059. end;
  17060. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  17061. begin
  17062. StartProgram(false);
  17063. Add([
  17064. '{$modeswitch externalclass}',
  17065. 'type',
  17066. ' TExtA = class external name ''ExtObj''',
  17067. ' class procedure DoIt(Id: longint = 1); static;',
  17068. ' end;',
  17069. 'var p: Pointer;',
  17070. 'begin',
  17071. ' texta.doit;',
  17072. ' texta.doit();',
  17073. ' texta.doit(2);',
  17074. ' p:[email protected];',
  17075. ' with texta do begin',
  17076. ' doit;',
  17077. ' doit();',
  17078. ' doit(3);',
  17079. ' p:=@DoIt;',
  17080. ' end;',
  17081. '']);
  17082. ConvertProgram;
  17083. CheckSource('TestExternalClass_ClassMethodStatic',
  17084. LinesToStr([ // statements
  17085. 'this.p = null;',
  17086. '']),
  17087. LinesToStr([ // $mod.$main
  17088. 'ExtObj.DoIt(1);',
  17089. 'ExtObj.DoIt(1);',
  17090. 'ExtObj.DoIt(2);',
  17091. '$mod.p = ExtObj.DoIt;',
  17092. 'ExtObj.DoIt(1);',
  17093. 'ExtObj.DoIt(1);',
  17094. 'ExtObj.DoIt(3);',
  17095. '$mod.p = ExtObj.DoIt;',
  17096. '']));
  17097. end;
  17098. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  17099. begin
  17100. StartProgram(false);
  17101. Add([
  17102. '{$modeswitch externalclass}',
  17103. 'type',
  17104. ' TBird = class external name ''Array''',
  17105. ' end;',
  17106. 'function GetPtr: Pointer;',
  17107. 'begin',
  17108. 'end;',
  17109. 'procedure Write(const p);',
  17110. 'begin',
  17111. 'end;',
  17112. 'procedure WriteLn; varargs;',
  17113. 'begin',
  17114. 'end;',
  17115. 'begin',
  17116. ' if TBird(GetPtr)=nil then ;',
  17117. ' Write(GetPtr);',
  17118. ' WriteLn(GetPtr);',
  17119. ' Write(TBird(GetPtr));',
  17120. ' WriteLn(TBird(GetPtr));',
  17121. '']);
  17122. ConvertProgram;
  17123. CheckSource('TestFunctionResultInTypeCast',
  17124. LinesToStr([ // statements
  17125. 'this.GetPtr = function () {',
  17126. ' var Result = null;',
  17127. ' return Result;',
  17128. '};',
  17129. 'this.Write = function (p) {',
  17130. '};',
  17131. 'this.WriteLn = function () {',
  17132. '};',
  17133. '']),
  17134. LinesToStr([
  17135. 'if ($mod.GetPtr() === null) ;',
  17136. '$mod.Write($mod.GetPtr());',
  17137. '$mod.WriteLn($mod.GetPtr());',
  17138. '$mod.Write($mod.GetPtr());',
  17139. '$mod.WriteLn($mod.GetPtr());',
  17140. '']));
  17141. end;
  17142. procedure TTestModule.TestExternalClass_NonExternalOverride;
  17143. begin
  17144. StartProgram(false);
  17145. Add([
  17146. '{$modeswitch externalclass}',
  17147. 'type',
  17148. ' TExtA = class external name ''ExtObjA''',
  17149. ' procedure ProcA; virtual;',
  17150. ' procedure ProcB; virtual;',
  17151. ' end;',
  17152. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  17153. ' end;',
  17154. ' TExtC = class (TExtB)',
  17155. ' procedure ProcA; override;',
  17156. ' end;',
  17157. 'procedure TExtC.ProcA;',
  17158. 'begin',
  17159. ' ProcA;',
  17160. ' Self.ProcA;',
  17161. ' ProcB;',
  17162. ' Self.ProcB;',
  17163. 'end;',
  17164. 'var',
  17165. ' A: texta;',
  17166. ' B: textb;',
  17167. ' C: textc;',
  17168. 'begin',
  17169. ' a.proca;',
  17170. ' b.proca;',
  17171. ' c.proca;']);
  17172. ConvertProgram;
  17173. CheckSource('TestExternalClass_NonExternalOverride',
  17174. LinesToStr([ // statements
  17175. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  17176. ' this.$init = function () {',
  17177. ' };',
  17178. ' this.$final = function () {',
  17179. ' };',
  17180. ' this.ProcA = function () {',
  17181. ' this.ProcA();',
  17182. ' this.ProcA();',
  17183. ' this.ProcB();',
  17184. ' this.ProcB();',
  17185. ' };',
  17186. '});',
  17187. 'this.A = null;',
  17188. 'this.B = null;',
  17189. 'this.C = null;',
  17190. '']),
  17191. LinesToStr([ // $mod.$main
  17192. '$mod.A.ProcA();',
  17193. '$mod.B.ProcA();',
  17194. '$mod.C.ProcA();',
  17195. '']));
  17196. end;
  17197. procedure TTestModule.TestExternalClass_OverloadHint;
  17198. begin
  17199. StartProgram(false);
  17200. Add([
  17201. '{$modeswitch externalclass}',
  17202. 'type',
  17203. ' TExtA = class external name ''ExtObjA''',
  17204. ' procedure DoIt;',
  17205. ' procedure DoIt(i: longint);',
  17206. ' end;',
  17207. 'begin',
  17208. '']);
  17209. ConvertProgram;
  17210. CheckResolverUnexpectedHints(true);
  17211. CheckSource('TestExternalClass_OverloadHint',
  17212. LinesToStr([ // statements
  17213. '']),
  17214. LinesToStr([ // $mod.$main
  17215. '']));
  17216. end;
  17217. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  17218. begin
  17219. WithTypeInfo:=true;
  17220. StartProgram(false);
  17221. Add([
  17222. '{$modeswitch externalclass}',
  17223. 'type',
  17224. ' JSwiper = class external name ''Swiper''',
  17225. ' constructor New;',
  17226. ' end;',
  17227. ' TObject = class',
  17228. ' private',
  17229. ' FSwiper: JSwiper;',
  17230. ' published',
  17231. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  17232. ' end;',
  17233. 'begin',
  17234. ' JSwiper.new;',
  17235. '']);
  17236. ConvertProgram;
  17237. CheckSource('TestExternalClass_SameNamePublishedProperty',
  17238. LinesToStr([ // statements
  17239. 'this.$rtti.$ExtClass("JSwiper", {',
  17240. ' jsclass: "Swiper"',
  17241. '});',
  17242. 'rtl.createClass(this, "TObject", null, function () {',
  17243. ' this.$init = function () {',
  17244. ' this.FSwiper = null;',
  17245. ' };',
  17246. ' this.$final = function () {',
  17247. ' this.FSwiper = undefined;',
  17248. ' };',
  17249. ' var $r = this.$rtti;',
  17250. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  17251. '});',
  17252. '']),
  17253. LinesToStr([ // $mod.$main
  17254. 'new Swiper();',
  17255. '']));
  17256. end;
  17257. procedure TTestModule.TestExternalClass_Property;
  17258. begin
  17259. StartProgram(false);
  17260. Add([
  17261. '{$modeswitch externalclass}',
  17262. 'type',
  17263. ' TExtA = class external name ''ExtA''',
  17264. ' function getYear: longint;',
  17265. ' procedure setYear(Value: longint);',
  17266. ' property Year: longint read getyear write setyear;',
  17267. ' end;',
  17268. ' TExtB = class (TExtA)',
  17269. ' procedure OtherSetYear(Value: longint);',
  17270. ' property year write othersetyear;',
  17271. ' end;',
  17272. 'procedure textb.othersetyear(value: longint);',
  17273. 'begin',
  17274. ' setYear(Value+4);',
  17275. 'end;',
  17276. 'var',
  17277. ' A: texta;',
  17278. ' B: textb;',
  17279. 'begin',
  17280. ' a.year:=a.year+1;',
  17281. ' b.year:=b.year+2;']);
  17282. ConvertProgram;
  17283. CheckSource('TestExternalClass_NonExternalOverride',
  17284. LinesToStr([ // statements
  17285. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17286. ' this.$init = function () {',
  17287. ' };',
  17288. ' this.$final = function () {',
  17289. ' };',
  17290. ' this.OtherSetYear = function (Value) {',
  17291. ' this.setYear(Value+4);',
  17292. ' };',
  17293. '});',
  17294. 'this.A = null;',
  17295. 'this.B = null;',
  17296. '']),
  17297. LinesToStr([ // $mod.$main
  17298. '$mod.A.setYear($mod.A.getYear()+1);',
  17299. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  17300. '']));
  17301. end;
  17302. procedure TTestModule.TestExternalClass_PropertyDate;
  17303. begin
  17304. StartProgram(false);
  17305. Add([
  17306. '{$modeswitch externalclass}',
  17307. 'type',
  17308. ' TExtA = class external name ''ExtA''',
  17309. ' end;',
  17310. ' TExtB = class (TExtA)',
  17311. ' FDate: string;',
  17312. ' property Date: string read FDate write FDate;',
  17313. ' property ExtA: string read FDate write FDate;',
  17314. ' end;',
  17315. ' {$M+}',
  17316. ' TObject = class',
  17317. ' FDate: string;',
  17318. ' published',
  17319. ' property Date: string read FDate write FDate;',
  17320. ' property ExtA: string read FDate write FDate;',
  17321. ' end;',
  17322. 'var',
  17323. ' B: textb;',
  17324. ' o: TObject;',
  17325. 'begin',
  17326. ' b.date:=b.exta;',
  17327. ' o.date:=o.exta;']);
  17328. ConvertProgram;
  17329. CheckSource('TestExternalClass_PropertyDate',
  17330. LinesToStr([ // statements
  17331. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17332. ' this.$init = function () {',
  17333. ' this.FDate = "";',
  17334. ' };',
  17335. ' this.$final = function () {',
  17336. ' };',
  17337. '});',
  17338. 'rtl.createClass(this, "TObject", null, function () {',
  17339. ' this.$init = function () {',
  17340. ' this.FDate = "";',
  17341. ' };',
  17342. ' this.$final = function () {',
  17343. ' };',
  17344. ' var $r = this.$rtti;',
  17345. ' $r.addField("FDate", rtl.string);',
  17346. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  17347. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  17348. '});',
  17349. 'this.B = null;',
  17350. 'this.o = null;',
  17351. '']),
  17352. LinesToStr([ // $mod.$main
  17353. '$mod.B.FDate = $mod.B.FDate;',
  17354. '$mod.o.FDate = $mod.o.FDate;',
  17355. '']));
  17356. end;
  17357. procedure TTestModule.TestExternalClass_ClassProperty;
  17358. begin
  17359. StartProgram(false);
  17360. Add('{$modeswitch externalclass}');
  17361. Add('type');
  17362. Add(' TExtA = class external name ''ExtA''');
  17363. Add(' class function getYear: longint;');
  17364. Add(' class procedure setYear(Value: longint);');
  17365. Add(' class property Year: longint read getyear write setyear;');
  17366. Add(' end;');
  17367. Add(' TExtB = class (TExtA)');
  17368. Add(' class function GetCentury: longint;');
  17369. Add(' class procedure SetCentury(Value: longint);');
  17370. Add(' class property Century: longint read getcentury write setcentury;');
  17371. Add(' end;');
  17372. Add('class function textb.getcentury: longint;');
  17373. Add('begin');
  17374. Add('end;');
  17375. Add('class procedure textb.setcentury(value: longint);');
  17376. Add('begin');
  17377. Add(' setyear(value+11);');
  17378. Add(' texta.year:=texta.year+12;');
  17379. Add(' year:=year+13;');
  17380. Add(' textb.century:=textb.century+14;');
  17381. Add(' century:=century+15;');
  17382. Add('end;');
  17383. Add('var');
  17384. Add(' A: texta;');
  17385. Add(' B: textb;');
  17386. Add('begin');
  17387. Add(' texta.year:=texta.year+1;');
  17388. Add(' textb.year:=textb.year+2;');
  17389. Add(' TextA.year:=TextA.year+3;');
  17390. Add(' b.year:=b.year+4;');
  17391. Add(' textb.century:=textb.century+5;');
  17392. Add(' b.century:=b.century+6;');
  17393. ConvertProgram;
  17394. CheckSource('TestExternalClass_ClassProperty',
  17395. LinesToStr([ // statements
  17396. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17397. ' this.$init = function () {',
  17398. ' };',
  17399. ' this.$final = function () {',
  17400. ' };',
  17401. ' this.GetCentury = function () {',
  17402. ' var Result = 0;',
  17403. ' return Result;',
  17404. ' };',
  17405. ' this.SetCentury = function (Value) {',
  17406. ' this.setYear(Value + 11);',
  17407. ' ExtA.setYear(ExtA.getYear() + 12);',
  17408. ' this.setYear(this.getYear() + 13);',
  17409. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17410. ' this.SetCentury(this.GetCentury() + 15);',
  17411. ' };',
  17412. '});',
  17413. 'this.A = null;',
  17414. 'this.B = null;',
  17415. '']),
  17416. LinesToStr([ // $mod.$main
  17417. 'ExtA.setYear(ExtA.getYear() + 1);',
  17418. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17419. 'ExtA.setYear(ExtA.getYear() + 3);',
  17420. '$mod.B.setYear($mod.B.getYear() + 4);',
  17421. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17422. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17423. '']));
  17424. end;
  17425. procedure TTestModule.TestExternalClass_ClassOf;
  17426. begin
  17427. StartProgram(false);
  17428. Add('{$modeswitch externalclass}');
  17429. Add('type');
  17430. Add(' TExtA = class external name ''ExtA''');
  17431. Add(' procedure ProcA; virtual;');
  17432. Add(' procedure ProcB; virtual;');
  17433. Add(' end;');
  17434. Add(' TExtAClass = class of TExtA;');
  17435. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17436. Add(' end;');
  17437. Add(' TExtBClass = class of TExtB;');
  17438. Add(' TExtC = class (TExtB)');
  17439. Add(' procedure ProcA; override;');
  17440. Add(' end;');
  17441. Add(' TExtCClass = class of TExtC;');
  17442. Add('procedure TExtC.ProcA; begin end;');
  17443. Add('var');
  17444. Add(' A: texta; ClA: TExtAClass;');
  17445. Add(' B: textb; ClB: TExtBClass;');
  17446. Add(' C: textc; ClC: TExtCClass;');
  17447. Add('begin');
  17448. Add(' ClA:=texta;');
  17449. Add(' ClA:=textb;');
  17450. Add(' ClA:=textc;');
  17451. Add(' ClB:=textb;');
  17452. Add(' ClB:=textc;');
  17453. Add(' ClC:=textc;');
  17454. ConvertProgram;
  17455. CheckSource('TestExternalClass_ClassOf',
  17456. LinesToStr([ // statements
  17457. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17458. ' this.$init = function () {',
  17459. ' };',
  17460. ' this.$final = function () {',
  17461. ' };',
  17462. ' this.ProcA = function () {',
  17463. ' };',
  17464. '});',
  17465. 'this.A = null;',
  17466. 'this.ClA = null;',
  17467. 'this.B = null;',
  17468. 'this.ClB = null;',
  17469. 'this.C = null;',
  17470. 'this.ClC = null;',
  17471. '']),
  17472. LinesToStr([ // $mod.$main
  17473. '$mod.ClA = ExtA;',
  17474. '$mod.ClA = ExtB;',
  17475. '$mod.ClA = $mod.TExtC;',
  17476. '$mod.ClB = ExtB;',
  17477. '$mod.ClB = $mod.TExtC;',
  17478. '$mod.ClC = $mod.TExtC;',
  17479. '']));
  17480. end;
  17481. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17482. begin
  17483. AddModuleWithIntfImplSrc('unit2.pas',
  17484. LinesToStr([
  17485. '{$modeswitch externalclass}',
  17486. 'type',
  17487. ' TExtA = class external name ''ExtA''',
  17488. ' class var Id: longint;',
  17489. ' end;',
  17490. '']),
  17491. '');
  17492. StartUnit(true);
  17493. Add('interface');
  17494. Add('uses unit2;');
  17495. Add('implementation');
  17496. Add('begin');
  17497. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17498. ConvertUnit;
  17499. CheckSource('TestExternalClass_ClassOtherUnit',
  17500. LinesToStr([
  17501. '']),
  17502. LinesToStr([
  17503. 'ExtA.Id = ExtA.Id + 1;',
  17504. '']));
  17505. end;
  17506. procedure TTestModule.TestExternalClass_Is;
  17507. begin
  17508. StartProgram(false);
  17509. Add([
  17510. '{$modeswitch externalclass}',
  17511. 'type',
  17512. ' TExtA = class external name ''ExtA''',
  17513. ' end;',
  17514. ' TExtAClass = class of TExtA;',
  17515. ' TExtB = class external name ''ExtB'' (TExtA)',
  17516. ' end;',
  17517. ' TExtBClass = class of TExtB;',
  17518. ' TExtC = class (TExtB)',
  17519. ' end;',
  17520. ' TExtCClass = class of TExtC;',
  17521. 'var',
  17522. ' A: texta; ClA: TExtAClass;',
  17523. ' B: textb; ClB: TExtBClass;',
  17524. ' C: textc; ClC: TExtCClass;',
  17525. 'begin',
  17526. ' if a is textb then ;',
  17527. ' if a is textc then ;',
  17528. ' if b is textc then ;',
  17529. ' if cla is textb then ;',
  17530. ' if cla is textc then ;',
  17531. ' if clb is textc then ;',
  17532. ' try',
  17533. ' except',
  17534. ' on TExtA do ;',
  17535. ' on e: TExtB do ;',
  17536. ' end;',
  17537. '']);
  17538. ConvertProgram;
  17539. CheckSource('TestExternalClass_Is',
  17540. LinesToStr([ // statements
  17541. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17542. ' this.$init = function () {',
  17543. ' };',
  17544. ' this.$final = function () {',
  17545. ' };',
  17546. '});',
  17547. 'this.A = null;',
  17548. 'this.ClA = null;',
  17549. 'this.B = null;',
  17550. 'this.ClB = null;',
  17551. 'this.C = null;',
  17552. 'this.ClC = null;',
  17553. '']),
  17554. LinesToStr([ // $mod.$main
  17555. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17556. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17557. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17558. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17559. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17560. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17561. 'try {} catch ($e) {',
  17562. ' if (rtl.isExt($e,ExtA)) {}',
  17563. ' else if (rtl.isExt($e,ExtB)) {',
  17564. ' var e = $e;',
  17565. ' } else throw $e',
  17566. '};',
  17567. '']));
  17568. end;
  17569. procedure TTestModule.TestExternalClass_As;
  17570. begin
  17571. StartProgram(false);
  17572. Add('{$modeswitch externalclass}');
  17573. Add('type');
  17574. Add(' TExtA = class external name ''ExtA''');
  17575. Add(' end;');
  17576. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17577. Add(' end;');
  17578. Add(' TExtC = class (TExtB)');
  17579. Add(' end;');
  17580. Add('var');
  17581. Add(' A: texta;');
  17582. Add(' B: textb;');
  17583. Add(' C: textc;');
  17584. Add('begin');
  17585. Add(' b:=a as textb;');
  17586. Add(' c:=a as textc;');
  17587. Add(' c:=b as textc;');
  17588. ConvertProgram;
  17589. CheckSource('TestExternalClass_Is',
  17590. LinesToStr([ // statements
  17591. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17592. ' this.$init = function () {',
  17593. ' };',
  17594. ' this.$final = function () {',
  17595. ' };',
  17596. '});',
  17597. 'this.A = null;',
  17598. 'this.B = null;',
  17599. 'this.C = null;',
  17600. '']),
  17601. LinesToStr([ // $mod.$main
  17602. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17603. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17604. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17605. '']));
  17606. end;
  17607. procedure TTestModule.TestExternalClass_DestructorFail;
  17608. begin
  17609. StartProgram(false);
  17610. Add('{$modeswitch externalclass}');
  17611. Add('type');
  17612. Add(' TExtA = class external name ''ExtA''');
  17613. Add(' destructor Free;');
  17614. Add(' end;');
  17615. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17616. nPasElementNotSupported);
  17617. ConvertProgram;
  17618. end;
  17619. procedure TTestModule.TestExternalClass_New;
  17620. begin
  17621. StartProgram(false);
  17622. Add([
  17623. '{$modeswitch externalclass}',
  17624. 'type',
  17625. ' TExtA = class external name ''ExtA''',
  17626. ' constructor New;',
  17627. ' constructor New(i: longint; j: longint = 2);',
  17628. ' end;',
  17629. 'var',
  17630. ' A: texta;',
  17631. 'begin',
  17632. ' a:=texta.new;',
  17633. ' a:=texta(texta.new);',
  17634. ' a:=texta.new();',
  17635. ' a:=texta.new(1);',
  17636. ' with texta do begin',
  17637. ' a:=new;',
  17638. ' a:=new();',
  17639. ' a:=new(2);',
  17640. ' end;',
  17641. ' a:=test1.texta.new;',
  17642. ' a:=test1.texta.new();',
  17643. ' a:=test1.texta.new(3);',
  17644. '']);
  17645. ConvertProgram;
  17646. CheckSource('TestExternalClass_New',
  17647. LinesToStr([ // statements
  17648. 'this.A = null;',
  17649. '']),
  17650. LinesToStr([ // $mod.$main
  17651. '$mod.A = new ExtA();',
  17652. '$mod.A = new ExtA();',
  17653. '$mod.A = new ExtA();',
  17654. '$mod.A = new ExtA(1,2);',
  17655. '$mod.A = new ExtA();',
  17656. '$mod.A = new ExtA();',
  17657. '$mod.A = new ExtA(2,2);',
  17658. '$mod.A = new ExtA();',
  17659. '$mod.A = new ExtA();',
  17660. '$mod.A = new ExtA(3,2);',
  17661. '']));
  17662. end;
  17663. procedure TTestModule.TestExternalClass_ClassOf_New;
  17664. begin
  17665. StartProgram(false);
  17666. Add('{$modeswitch externalclass}');
  17667. Add('type');
  17668. Add(' TExtAClass = class of TExtA;');
  17669. Add(' TExtA = class external name ''ExtA''');
  17670. Add(' C: TExtAClass;');
  17671. Add(' constructor New;');
  17672. Add(' end;');
  17673. Add('var');
  17674. Add(' A: texta;');
  17675. Add(' C: textaclass;');
  17676. Add('begin');
  17677. Add(' a:=c.new;');
  17678. Add(' a:=c.new();');
  17679. Add(' with C do begin');
  17680. Add(' a:=new;');
  17681. Add(' a:=new();');
  17682. Add(' end;');
  17683. Add(' a:=test1.c.new;');
  17684. Add(' a:=test1.c.new();');
  17685. Add(' a:=A.c.new();');
  17686. ConvertProgram;
  17687. CheckSource('TestExternalClass_ClassOf_New',
  17688. LinesToStr([ // statements
  17689. 'this.A = null;',
  17690. 'this.C = null;',
  17691. '']),
  17692. LinesToStr([ // $mod.$main
  17693. '$mod.A = new $mod.C();',
  17694. '$mod.A = new $mod.C();',
  17695. 'var $with = $mod.C;',
  17696. '$mod.A = new $with();',
  17697. '$mod.A = new $with();',
  17698. '$mod.A = new $mod.C();',
  17699. '$mod.A = new $mod.C();',
  17700. '$mod.A = new $mod.A.C();',
  17701. '']));
  17702. end;
  17703. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17704. begin
  17705. StartProgram(false);
  17706. Add([
  17707. '{$modeswitch externalclass}',
  17708. 'type',
  17709. ' TExtAClass = class of TExtA;',
  17710. ' TExtA = class external name ''ExtA''',
  17711. ' constructor New;',
  17712. ' end;',
  17713. 'function GetCreator: TExtAClass;',
  17714. 'begin',
  17715. ' Result:=TExtA;',
  17716. 'end;',
  17717. 'var',
  17718. ' A: texta;',
  17719. 'begin',
  17720. ' a:=getcreator.new;',
  17721. ' a:=getcreator().new;',
  17722. ' a:=getcreator().new();',
  17723. ' a:=getcreator.new();',
  17724. ' with getcreator do begin',
  17725. ' a:=new;',
  17726. ' a:=new();',
  17727. ' end;']);
  17728. ConvertProgram;
  17729. CheckSource('TestExternalClass_FuncClassOf_New',
  17730. LinesToStr([ // statements
  17731. 'this.GetCreator = function () {',
  17732. ' var Result = null;',
  17733. ' Result = ExtA;',
  17734. ' return Result;',
  17735. '};',
  17736. 'this.A = null;',
  17737. '']),
  17738. LinesToStr([ // $mod.$main
  17739. '$mod.A = new ($mod.GetCreator())();',
  17740. '$mod.A = new ($mod.GetCreator())();',
  17741. '$mod.A = new ($mod.GetCreator())();',
  17742. '$mod.A = new ($mod.GetCreator())();',
  17743. 'var $with = $mod.GetCreator();',
  17744. '$mod.A = new $with();',
  17745. '$mod.A = new $with();',
  17746. '']));
  17747. end;
  17748. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17749. begin
  17750. StartProgram(false);
  17751. Add([
  17752. '{$modeswitch externalclass}',
  17753. 'type',
  17754. ' TExtA = class external name ''ExtA''',
  17755. ' constructor New;',
  17756. ' end;',
  17757. ' TBird = class(TExtA)',
  17758. ' end;',
  17759. 'begin',
  17760. ' TBird.new;',
  17761. '']);
  17762. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17763. ConvertProgram;
  17764. end;
  17765. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17766. begin
  17767. StartProgram(false);
  17768. Add([
  17769. '{$modeswitch externalclass}',
  17770. 'type',
  17771. ' TExtA = class external name ''ExtA''',
  17772. ' constructor New;',
  17773. ' end;',
  17774. ' TBird = class(TExtA)',
  17775. ' end;',
  17776. 'begin',
  17777. ' TBird.new();',
  17778. '']);
  17779. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17780. ConvertProgram;
  17781. end;
  17782. procedure TTestModule.TestExternalClass_NewExtName;
  17783. begin
  17784. StartProgram(false);
  17785. Add([
  17786. '{$modeswitch externalclass}',
  17787. 'type',
  17788. ' TExtA = class external name ''ExtA''',
  17789. ' constructor New; external name ''Other'';',
  17790. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17791. ' end;',
  17792. 'var',
  17793. ' A: texta;',
  17794. 'begin',
  17795. ' a:=texta.new;',
  17796. ' a:=texta(texta.new);',
  17797. ' a:=texta.new();',
  17798. ' a:=texta.new(1);',
  17799. ' with texta do begin',
  17800. ' a:=new;',
  17801. ' a:=new();',
  17802. ' a:=new(2);',
  17803. ' end;',
  17804. ' a:=test1.texta.new;',
  17805. ' a:=test1.texta.new();',
  17806. ' a:=test1.texta.new(3);',
  17807. '']);
  17808. ConvertProgram;
  17809. CheckSource('TestExternalClass_NewExtName',
  17810. LinesToStr([ // statements
  17811. 'this.A = null;',
  17812. '']),
  17813. LinesToStr([ // $mod.$main
  17814. '$mod.A = new Other();',
  17815. '$mod.A = new Other();',
  17816. '$mod.A = new Other();',
  17817. '$mod.A = new A.B(1,2);',
  17818. '$mod.A = new Other();',
  17819. '$mod.A = new Other();',
  17820. '$mod.A = new A.B(2,2);',
  17821. '$mod.A = new Other();',
  17822. '$mod.A = new Other();',
  17823. '$mod.A = new A.B(3,2);',
  17824. '']));
  17825. end;
  17826. procedure TTestModule.TestExternalClass_Constructor;
  17827. begin
  17828. StartProgram(false);
  17829. Add([
  17830. '{$modeswitch externalclass}',
  17831. 'type',
  17832. ' TExtA = class external name ''ExtA''',
  17833. ' public type',
  17834. ' TExtB = class external name ''ExtB''',
  17835. ' public type',
  17836. ' TExtC = class external name ''ExtC''',
  17837. ' constructor New;',
  17838. ' constructor New(i: word);',
  17839. ' end;',
  17840. ' end;',
  17841. ' constructor Create;',
  17842. ' constructor Create(i: longint; j: longint = 2);',
  17843. ' end;',
  17844. 'var',
  17845. ' A: texta;',
  17846. ' C: texta.textb.textc;',
  17847. 'begin',
  17848. ' a:=texta.create;',
  17849. ' a:=texta(texta.create);',
  17850. ' a:=texta.create();',
  17851. ' a:=texta.create(1);',
  17852. ' with texta do begin',
  17853. ' a:=create;',
  17854. ' a:=create();',
  17855. ' a:=create(2);',
  17856. ' end;',
  17857. ' a:=test1.texta.create;',
  17858. ' a:=test1.texta.create();',
  17859. ' a:=test1.texta.create(3);',
  17860. ' c:=texta.textb.textc.new;',
  17861. ' c:=texta.textb.textc.new();',
  17862. ' c:=texta.textb.textc.new(4);',
  17863. '']);
  17864. ConvertProgram;
  17865. CheckSource('TestExternalClass_Constructor',
  17866. LinesToStr([ // statements
  17867. 'this.A = null;',
  17868. 'this.C = null;',
  17869. '']),
  17870. LinesToStr([ // $mod.$main
  17871. '$mod.A = new ExtA.Create();',
  17872. '$mod.A = new ExtA.Create();',
  17873. '$mod.A = new ExtA.Create();',
  17874. '$mod.A = new ExtA.Create(1,2);',
  17875. '$mod.A = new ExtA.Create();',
  17876. '$mod.A = new ExtA.Create();',
  17877. '$mod.A = new ExtA.Create(2,2);',
  17878. '$mod.A = new ExtA.Create();',
  17879. '$mod.A = new ExtA.Create();',
  17880. '$mod.A = new ExtA.Create(3,2);',
  17881. '$mod.C = new ExtA.ExtB.ExtC();',
  17882. '$mod.C = new ExtA.ExtB.ExtC();',
  17883. '$mod.C = new ExtA.ExtB.ExtC(4);',
  17884. '']));
  17885. end;
  17886. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17887. begin
  17888. StartProgram(false);
  17889. Add([
  17890. '{$modeswitch externalclass}',
  17891. 'type',
  17892. ' TExtA = class external name ''ExtA''',
  17893. ' constructor Create; external name ''{}'';',
  17894. ' end;',
  17895. 'var',
  17896. ' A: texta;',
  17897. 'begin',
  17898. ' a:=texta.create;',
  17899. ' a:=texta(texta.create);',
  17900. ' a:=texta.create();',
  17901. ' with texta do begin',
  17902. ' a:=create;',
  17903. ' a:=create();',
  17904. ' end;',
  17905. ' a:=test1.texta.create;',
  17906. ' a:=test1.texta.create();',
  17907. '']);
  17908. ConvertProgram;
  17909. CheckSource('TestExternalClass_ConstructorBrackets',
  17910. LinesToStr([ // statements
  17911. 'this.A = null;',
  17912. '']),
  17913. LinesToStr([ // $mod.$main
  17914. '$mod.A = {};',
  17915. '$mod.A = {};',
  17916. '$mod.A = {};',
  17917. '$mod.A = {};',
  17918. '$mod.A = {};',
  17919. '$mod.A = {};',
  17920. '$mod.A = {};',
  17921. '']));
  17922. end;
  17923. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17924. begin
  17925. StartProgram(false);
  17926. Add('{$modeswitch externalclass}');
  17927. Add('type');
  17928. Add(' TExtA = class external name ''ExtA''');
  17929. Add(' constructor New;');
  17930. Add(' end;');
  17931. Add('function DoIt: longint;');
  17932. Add('const ExtA: longint = 3;');
  17933. Add('begin');
  17934. Add(' Result:=ExtA;');
  17935. Add('end;');
  17936. Add('var');
  17937. Add(' A: texta;');
  17938. Add('begin');
  17939. Add(' a:=texta.new;');
  17940. ConvertProgram;
  17941. CheckSource('TestExternalClass_LocalConstSameName',
  17942. LinesToStr([ // statements
  17943. 'var ExtA$1 = 3;',
  17944. 'this.DoIt = function () {',
  17945. ' var Result = 0;',
  17946. ' Result = ExtA$1;',
  17947. ' return Result;',
  17948. '};',
  17949. 'this.A = null;',
  17950. '']),
  17951. LinesToStr([ // $mod.$main
  17952. '$mod.A = new ExtA();',
  17953. '']));
  17954. end;
  17955. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17956. begin
  17957. StartProgram(false);
  17958. Add('{$modeswitch externalclass}');
  17959. Add('type');
  17960. Add(' TExtA = class external name ''ExtA''');
  17961. Add(' procedure DoIt;');
  17962. Add(' end;');
  17963. Add(' TMyA = class(TExtA)');
  17964. Add(' procedure DoIt;');
  17965. Add(' end;');
  17966. Add('procedure TMyA.DoIt; begin end;');
  17967. Add('begin');
  17968. ConvertProgram;
  17969. CheckSource('TestExternalClass_ReintroduceOverload',
  17970. LinesToStr([ // statements
  17971. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17972. ' this.$init = function () {',
  17973. ' };',
  17974. ' this.$final = function () {',
  17975. ' };',
  17976. ' this.DoIt$1 = function () {',
  17977. ' };',
  17978. '});',
  17979. '']),
  17980. LinesToStr([ // $mod.$main
  17981. '']));
  17982. end;
  17983. procedure TTestModule.TestExternalClass_Inherited;
  17984. begin
  17985. StartProgram(false);
  17986. Add('{$modeswitch externalclass}');
  17987. Add('type');
  17988. Add(' TExtA = class external name ''ExtA''');
  17989. Add(' procedure DoIt(i: longint = 1); virtual;');
  17990. Add(' procedure DoSome(j: longint = 2);');
  17991. Add(' end;');
  17992. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17993. Add(' end;');
  17994. Add(' TMyC = class(TExtB)');
  17995. Add(' procedure DoIt(i: longint = 1); override;');
  17996. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17997. Add(' end;');
  17998. Add('procedure TMyC.DoIt(i: longint);');
  17999. Add('begin');
  18000. Add(' inherited;');
  18001. Add(' inherited DoIt;');
  18002. Add(' inherited DoIt();');
  18003. Add(' inherited DoIt(3);');
  18004. Add(' inherited DoSome;');
  18005. Add(' inherited DoSome();');
  18006. Add(' inherited DoSome(4);');
  18007. Add('end;');
  18008. Add('procedure TMyC.DoSome(j: longint);');
  18009. Add('begin');
  18010. Add(' inherited;');
  18011. Add('end;');
  18012. Add('begin');
  18013. ConvertProgram;
  18014. CheckSource('TestExternalClass_ReintroduceOverload',
  18015. LinesToStr([ // statements
  18016. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  18017. ' this.$init = function () {',
  18018. ' };',
  18019. ' this.$final = function () {',
  18020. ' };',
  18021. ' this.DoIt = function (i) {',
  18022. ' ExtB.DoIt.apply(this, arguments);',
  18023. ' ExtB.DoIt.call(this, 1);',
  18024. ' ExtB.DoIt.call(this, 1);',
  18025. ' ExtB.DoIt.call(this, 3);',
  18026. ' ExtB.DoSome.call(this, 2);',
  18027. ' ExtB.DoSome.call(this, 2);',
  18028. ' ExtB.DoSome.call(this, 4);',
  18029. ' };',
  18030. ' this.DoSome$1 = function (j) {',
  18031. ' ExtB.DoSome.apply(this, arguments);',
  18032. ' };',
  18033. '});',
  18034. '']),
  18035. LinesToStr([ // $mod.$main
  18036. '']));
  18037. end;
  18038. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  18039. begin
  18040. StartProgram(false);
  18041. Add('{$modeswitch externalclass}');
  18042. Add('type');
  18043. Add(' TObject = class');
  18044. Add(' end;');
  18045. Add(' TExtA = class external name ''ExtA''(TObject)');
  18046. Add(' end;');
  18047. Add('begin');
  18048. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  18049. ConvertProgram;
  18050. end;
  18051. procedure TTestModule.TestExternalClass_NewInstance;
  18052. begin
  18053. StartProgram(false);
  18054. Add('{$modeswitch externalclass}');
  18055. Add('type');
  18056. Add(' TExtA = class external name ''ExtA''');
  18057. Add(' end;');
  18058. Add(' TMyB = class(TExtA)');
  18059. Add(' protected');
  18060. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  18061. Add(' end;');
  18062. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18063. Add('begin end;');
  18064. Add('begin');
  18065. ConvertProgram;
  18066. CheckSource('TestExternalClass_NewInstance',
  18067. LinesToStr([ // statements
  18068. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  18069. ' this.$init = function () {',
  18070. ' };',
  18071. ' this.$final = function () {',
  18072. ' };',
  18073. ' this.NewInstance = function (fnname, paramarray) {',
  18074. ' var Result = null;',
  18075. ' return Result;',
  18076. ' };',
  18077. '});',
  18078. '']),
  18079. LinesToStr([ // $mod.$main
  18080. '']));
  18081. end;
  18082. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  18083. begin
  18084. StartProgram(false);
  18085. Add('{$modeswitch externalclass}');
  18086. Add('type');
  18087. Add(' TExtA = class external name ''ExtA''');
  18088. Add(' end;');
  18089. Add(' TMyB = class(TExtA)');
  18090. Add(' protected');
  18091. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  18092. Add(' end;');
  18093. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18094. Add('begin end;');
  18095. Add('begin');
  18096. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  18097. ConvertProgram;
  18098. end;
  18099. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  18100. begin
  18101. StartProgram(false);
  18102. Add('{$modeswitch externalclass}');
  18103. Add('type');
  18104. Add(' TExtA = class external name ''ExtA''');
  18105. Add(' end;');
  18106. Add(' TMyB = class(TExtA)');
  18107. Add(' protected');
  18108. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  18109. Add(' end;');
  18110. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  18111. Add('begin end;');
  18112. Add('begin');
  18113. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  18114. nIncompatibleTypeArgNo);
  18115. ConvertProgram;
  18116. end;
  18117. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  18118. begin
  18119. StartProgram(false);
  18120. Add('{$modeswitch externalclass}');
  18121. Add('type');
  18122. Add(' TExtA = class external name ''ExtA''');
  18123. Add(' end;');
  18124. Add(' TMyB = class(TExtA)');
  18125. Add(' protected');
  18126. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  18127. Add(' end;');
  18128. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  18129. Add('begin end;');
  18130. Add('begin');
  18131. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  18132. nIncompatibleTypeArgNo);
  18133. ConvertProgram;
  18134. end;
  18135. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  18136. begin
  18137. StartProgram(false);
  18138. Add([
  18139. '{$modeswitch externalclass}',
  18140. 'type',
  18141. ' TJSFunction = class external name ''Function''',
  18142. ' end;',
  18143. ' TExtA = class external name ''ExtA''(TJSFunction)',
  18144. ' constructor New(w: word);',
  18145. ' end;',
  18146. ' TBird = class (TExtA)',
  18147. ' public',
  18148. ' Size: word;',
  18149. ' class var Legs: word;',
  18150. ' constructor Create(a: word);',
  18151. ' end;',
  18152. ' TEagle = class (TBird)',
  18153. ' public',
  18154. ' constructor Create(b: word); reintroduce;',
  18155. ' end;',
  18156. 'constructor TBird.Create(a: word);',
  18157. 'begin',
  18158. ' inherited;', // silently ignored
  18159. ' inherited New(a);', // this.$func(a)
  18160. 'end;',
  18161. 'constructor TEagle.Create(b: word);',
  18162. 'begin',
  18163. ' inherited Create(b);',
  18164. 'end;',
  18165. 'var',
  18166. ' Bird: TBird;',
  18167. ' Eagle: TEagle;',
  18168. 'begin',
  18169. ' Bird:=TBird.Create(3);',
  18170. ' Eagle:=TEagle.Create(4);',
  18171. ' Bird.Size:=Bird.Size+5;',
  18172. ' Bird.Legs:=Bird.Legs+6;',
  18173. ' Eagle.Size:=Eagle.Size+5;',
  18174. ' Eagle.Legs:=Eagle.Legs+6;',
  18175. '']);
  18176. ConvertProgram;
  18177. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  18178. LinesToStr([ // statements
  18179. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  18180. ' this.Legs = 0;',
  18181. ' this.$init = function () {',
  18182. ' this.Size = 0;',
  18183. ' };',
  18184. ' this.$final = function () {',
  18185. ' };',
  18186. ' this.Create = function (a) {',
  18187. ' this.$ancestorfunc(a);',
  18188. ' return this;',
  18189. ' };',
  18190. '});',
  18191. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  18192. ' this.Create$1 = function (b) {',
  18193. ' $mod.TBird.Create.call(this, b);',
  18194. ' return this;',
  18195. ' };',
  18196. '});',
  18197. 'this.Bird = null;',
  18198. 'this.Eagle = null;',
  18199. '']),
  18200. LinesToStr([ // $mod.$main
  18201. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  18202. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  18203. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  18204. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  18205. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  18206. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  18207. '']));
  18208. end;
  18209. procedure TTestModule.TestExternalClass_PascalProperty;
  18210. begin
  18211. StartProgram(false);
  18212. Add('{$modeswitch externalclass}');
  18213. Add('type');
  18214. Add(' TJSElement = class;');
  18215. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  18216. Add(' TJSElement = class external name ''ExtA''');
  18217. Add(' end;');
  18218. Add(' TControl = class(TJSElement)');
  18219. Add(' private');
  18220. Add(' FOnClick: TJSNotifyEvent;');
  18221. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  18222. Add(' procedure Click(Sender: TJSElement);');
  18223. Add(' end;');
  18224. Add('procedure TControl.Click(Sender: TJSElement);');
  18225. Add('begin');
  18226. Add(' OnClick(Self);');
  18227. Add('end;');
  18228. Add('var');
  18229. Add(' Ctrl: TControl;');
  18230. Add('begin');
  18231. Add(' Ctrl.OnClick:[email protected];');
  18232. Add(' Ctrl.OnClick(Ctrl);');
  18233. ConvertProgram;
  18234. CheckSource('TestExternalClass_PascalProperty',
  18235. LinesToStr([ // statements
  18236. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  18237. ' this.$init = function () {',
  18238. ' this.FOnClick = null;',
  18239. ' };',
  18240. ' this.$final = function () {',
  18241. ' this.FOnClick = undefined;',
  18242. ' };',
  18243. ' this.Click = function (Sender) {',
  18244. ' this.FOnClick(this);',
  18245. ' };',
  18246. '});',
  18247. 'this.Ctrl = null;',
  18248. '']),
  18249. LinesToStr([ // $mod.$main
  18250. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  18251. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  18252. '']));
  18253. end;
  18254. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  18255. begin
  18256. StartProgram(false);
  18257. Add([
  18258. '{$modeswitch externalclass}',
  18259. 'type',
  18260. ' IUnknown = interface end;',
  18261. ' TObject = class',
  18262. ' end;',
  18263. ' TChild = class',
  18264. ' end;',
  18265. ' TExtRootA = class external name ''ExtRootA''',
  18266. ' end;',
  18267. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  18268. ' end;',
  18269. ' TExtRootB = class external name ''ExtRootB''',
  18270. ' end;',
  18271. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  18272. ' end;',
  18273. ' TExtString = class external name ''String''',
  18274. ' function charAt(aIndex : NativeInt) : string;',
  18275. ' end;',
  18276. 'var',
  18277. ' Obj: TObject;',
  18278. ' Child: TChild;',
  18279. ' RootA: TExtRootA;',
  18280. ' ChildA: TExtChildA;',
  18281. ' RootB: TExtRootB;',
  18282. ' ChildB: TExtChildB;',
  18283. ' i: IUnknown;',
  18284. ' s: string;',
  18285. ' v: jsvalue;',
  18286. 'begin',
  18287. ' obj:=tobject(roota);',
  18288. ' obj:=tobject(childa);',
  18289. ' child:=tchild(tobject(roota));',
  18290. ' roota:=textroota(obj);',
  18291. ' roota:=textroota(child);',
  18292. ' roota:=textroota(rootb);',
  18293. ' roota:=textroota(childb);',
  18294. ' childa:=textchilda(textroota(obj));',
  18295. ' roota:=TExtRootA(i);',
  18296. ' s:=TExtString(s).charAt(7);',
  18297. ' s:=TExtString(v).charAt(8);',
  18298. '']);
  18299. ConvertProgram;
  18300. CheckSource('TestExternalClass_TypeCastToRootClass',
  18301. LinesToStr([ // statements
  18302. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18303. 'rtl.createClass(this, "TObject", null, function () {',
  18304. ' this.$init = function () {',
  18305. ' };',
  18306. ' this.$final = function () {',
  18307. ' };',
  18308. '});',
  18309. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18310. '});',
  18311. 'this.Obj = null;',
  18312. 'this.Child = null;',
  18313. 'this.RootA = null;',
  18314. 'this.ChildA = null;',
  18315. 'this.RootB = null;',
  18316. 'this.ChildB = null;',
  18317. 'this.i = null;',
  18318. 'this.s = "";',
  18319. 'this.v = undefined;',
  18320. '']),
  18321. LinesToStr([ // $mod.$main
  18322. '$mod.Obj = $mod.RootA;',
  18323. '$mod.Obj = $mod.ChildA;',
  18324. '$mod.Child = $mod.RootA;',
  18325. '$mod.RootA = $mod.Obj;',
  18326. '$mod.RootA = $mod.Child;',
  18327. '$mod.RootA = $mod.RootB;',
  18328. '$mod.RootA = $mod.ChildB;',
  18329. '$mod.ChildA = $mod.Obj;',
  18330. '$mod.RootA = $mod.i;',
  18331. '$mod.s = $mod.s.charAt(7);',
  18332. '$mod.s = $mod.v.charAt(8);',
  18333. '']));
  18334. end;
  18335. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  18336. begin
  18337. StartProgram(false);
  18338. Add([
  18339. '{$modeswitch externalclass}',
  18340. 'type',
  18341. ' IUnknown = interface end;',
  18342. ' IBird = interface(IUnknown) end;',
  18343. ' TClass = class of TObject;',
  18344. ' TObject = class',
  18345. ' end;',
  18346. ' TChild = class',
  18347. ' end;',
  18348. ' TJSObject = class external name ''Object''',
  18349. ' end;',
  18350. ' TRec = record end;',
  18351. 'var',
  18352. ' Obj: TObject;',
  18353. ' Child: TChild;',
  18354. ' i: IUnknown;',
  18355. ' Bird: IBird;',
  18356. ' j: TJSObject;',
  18357. ' r: TRec;',
  18358. ' c: TClass;',
  18359. 'begin',
  18360. ' j:=tjsobject(IUnknown);',
  18361. ' j:=tjsobject(IBird);',
  18362. ' j:=tjsobject(TObject);',
  18363. ' j:=tjsobject(TChild);',
  18364. ' j:=tjsobject(TRec);',
  18365. ' j:=tjsobject(Obj);',
  18366. ' j:=tjsobject(Child);',
  18367. ' j:=tjsobject(i);',
  18368. ' j:=tjsobject(Bird);',
  18369. ' j:=tjsobject(r);',
  18370. ' j:=tjsobject(c);',
  18371. '']);
  18372. ConvertProgram;
  18373. CheckSource('TestExternalClass_TypeCastToJSObject',
  18374. LinesToStr([ // statements
  18375. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18376. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18377. 'rtl.createClass(this, "TObject", null, function () {',
  18378. ' this.$init = function () {',
  18379. ' };',
  18380. ' this.$final = function () {',
  18381. ' };',
  18382. '});',
  18383. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18384. '});',
  18385. 'rtl.recNewT(this, "TRec", function () {',
  18386. ' this.$eq = function (b) {',
  18387. ' return true;',
  18388. ' };',
  18389. ' this.$assign = function (s) {',
  18390. ' return this;',
  18391. ' };',
  18392. '});',
  18393. 'this.Obj = null;',
  18394. 'this.Child = null;',
  18395. 'this.i = null;',
  18396. 'this.Bird = null;',
  18397. 'this.j = null;',
  18398. 'this.r = this.TRec.$new();',
  18399. 'this.c = null;',
  18400. '']),
  18401. LinesToStr([ // $mod.$main
  18402. '$mod.j = $mod.IUnknown;',
  18403. '$mod.j = $mod.IBird;',
  18404. '$mod.j = $mod.TObject;',
  18405. '$mod.j = $mod.TChild;',
  18406. '$mod.j = $mod.TRec;',
  18407. '$mod.j = $mod.Obj;',
  18408. '$mod.j = $mod.Child;',
  18409. '$mod.j = $mod.i;',
  18410. '$mod.j = $mod.Bird;',
  18411. '$mod.j = $mod.r;',
  18412. '$mod.j = $mod.c;',
  18413. '']));
  18414. end;
  18415. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  18416. begin
  18417. StartProgram(false);
  18418. Add('{$modeswitch externalclass}');
  18419. Add('type');
  18420. Add(' TJSString = class external name ''String''');
  18421. Add(' class function fromCharCode() : string; varargs;');
  18422. Add(' function anchor(const aName : string) : string;');
  18423. Add(' end;');
  18424. Add('var');
  18425. Add(' s: string;');
  18426. Add('begin');
  18427. Add(' s:=TJSString.fromCharCode(65,66);');
  18428. Add(' s:=TJSString(s).anchor(s);');
  18429. Add(' s:=TJSString(''foo'').anchor(s);');
  18430. ConvertProgram;
  18431. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18432. LinesToStr([ // statements
  18433. 'this.s = "";',
  18434. '']),
  18435. LinesToStr([ // $mod.$main
  18436. '$mod.s = String.fromCharCode(65, 66);',
  18437. '$mod.s = $mod.s.anchor($mod.s);',
  18438. '$mod.s = "foo".anchor($mod.s);',
  18439. '']));
  18440. end;
  18441. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18442. begin
  18443. StartProgram(false);
  18444. Add([
  18445. '{$modeswitch externalclass}',
  18446. 'type',
  18447. ' TJSObject = class external name ''Object'' end;',
  18448. ' TJSFunction = class external name ''Function''',
  18449. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18450. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18451. ' end;',
  18452. ' TObject = class',
  18453. ' procedure DoIt(i: longint);',
  18454. ' end;',
  18455. ' TFuncInt = function(o: TObject): longint;',
  18456. 'function GetIt(o: TObject): longint;',
  18457. ' procedure Sub; begin end;',
  18458. 'var',
  18459. ' f: TJSFunction;',
  18460. ' fi: TFuncInt;',
  18461. 'begin',
  18462. ' fi:=TFuncInt(f);',
  18463. ' f:=TJSFunction(fi);',
  18464. ' f:=TJSFunction(@GetIt);',
  18465. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18466. ' f:=TJSFunction(@Sub);',
  18467. ' f:=TJSFunction(@o.doit);',
  18468. ' f:=TJSFunction(fi).bind(nil,4)',
  18469. 'end;',
  18470. 'procedure TObject.DoIt(i: longint);',
  18471. ' procedure Sub; begin end;',
  18472. 'var f: TJSFunction;',
  18473. 'begin',
  18474. ' f:=TJSFunction(@DoIt);',
  18475. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18476. ' f:=TJSFunction(@Sub);',
  18477. ' f:=TJSFunction(@GetIt);',
  18478. 'end;',
  18479. 'begin']);
  18480. ConvertProgram;
  18481. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18482. LinesToStr([ // statements
  18483. 'rtl.createClass(this, "TObject", null, function () {',
  18484. ' this.$init = function () {',
  18485. ' };',
  18486. ' this.$final = function () {',
  18487. ' };',
  18488. ' this.DoIt = function (i) {',
  18489. ' var $Self = this;',
  18490. ' function Sub() {',
  18491. ' };',
  18492. ' var f = null;',
  18493. ' f = this.DoIt;',
  18494. ' f = this.DoIt.bind(null, 13);',
  18495. ' f = Sub;',
  18496. ' f = $mod.GetIt;',
  18497. ' };',
  18498. '});',
  18499. 'this.GetIt = function (o) {',
  18500. ' var Result = 0;',
  18501. ' function Sub() {',
  18502. ' };',
  18503. ' var f = null;',
  18504. ' var fi = null;',
  18505. ' fi = f;',
  18506. ' f = fi;',
  18507. ' f = $mod.GetIt;',
  18508. ' f = $mod.GetIt.bind(null, 3);',
  18509. ' f = Sub;',
  18510. ' f = $mod.TObject.DoIt;',
  18511. ' f = fi.bind(null, 4);',
  18512. ' return Result;',
  18513. '};',
  18514. '']),
  18515. LinesToStr([ // $mod.$main
  18516. '']));
  18517. end;
  18518. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18519. begin
  18520. StartProgram(false);
  18521. Add([
  18522. '{$mode delphi}',
  18523. '{$modeswitch externalclass}',
  18524. 'type',
  18525. ' TJSObject = class external name ''Object'' end;',
  18526. ' TJSWindow = class external name ''Window''(TJSObject)',
  18527. ' procedure Open;',
  18528. ' end;',
  18529. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18530. ' procedure Execute;',
  18531. ' end;',
  18532. 'procedure Fly;',
  18533. 'var',
  18534. ' w: TJSWindow;',
  18535. ' e: TJSEventTarget;',
  18536. 'begin',
  18537. ' w:=TJSWindow(e);',
  18538. ' e:=TJSEventTarget(w);',
  18539. 'end;',
  18540. 'begin']);
  18541. ConvertProgram;
  18542. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18543. LinesToStr([ // statements
  18544. 'this.Fly = function () {',
  18545. ' var w = null;',
  18546. ' var e = null;',
  18547. ' w = e;',
  18548. ' e = w;',
  18549. '};',
  18550. '']),
  18551. LinesToStr([ // $mod.$main
  18552. '']));
  18553. end;
  18554. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18555. begin
  18556. StartProgram(false);
  18557. Add('{$modeswitch externalclass}');
  18558. Add('type');
  18559. Add(' TJSString = class external name ''String''');
  18560. Add(' class function fromCharCode() : string; varargs;');
  18561. Add(' end;');
  18562. Add('var');
  18563. Add(' s: string;');
  18564. Add(' sObj: TJSString;');
  18565. Add('begin');
  18566. Add(' s:=sObj.fromCharCode(65,66);');
  18567. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18568. nExternalClassInstanceCannotAccessStaticX);
  18569. ConvertProgram;
  18570. end;
  18571. procedure TTestModule.TestExternalClass_BracketAccessor;
  18572. begin
  18573. StartProgram(false);
  18574. Add([
  18575. '{$modeswitch externalclass}',
  18576. 'type',
  18577. ' TJSArray = class external name ''Array2''',
  18578. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18579. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18580. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18581. ' end;',
  18582. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18583. 'begin end;',
  18584. 'var',
  18585. ' Arr: tjsarray;',
  18586. ' s: string;',
  18587. ' i: longint;',
  18588. ' v: jsvalue;',
  18589. 'begin',
  18590. ' v:=arr[0];',
  18591. ' v:=arr.items[1];',
  18592. ' arr[2]:=s;',
  18593. ' arr.items[3]:=s;',
  18594. ' arr[4]:=i;',
  18595. ' arr[5]:=arr[6];',
  18596. ' arr.items[7]:=arr.items[8];',
  18597. ' with arr do items[9]:=items[10];',
  18598. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18599. ' with arr do begin',
  18600. ' v:=GetItems(14);',
  18601. ' setitems(15,16);',
  18602. ' end;',
  18603. ' v:=test1.arr.items[17];',
  18604. ' test1.arr.items[18]:=v;',
  18605. '']);
  18606. ConvertProgram;
  18607. CheckSource('TestExternalClass_BracketAccessor',
  18608. LinesToStr([ // statements
  18609. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18610. '};',
  18611. 'this.Arr = null;',
  18612. 'this.s = "";',
  18613. 'this.i = 0;',
  18614. 'this.v = undefined;',
  18615. '']),
  18616. LinesToStr([ // $mod.$main
  18617. '$mod.v = $mod.Arr[0];',
  18618. '$mod.v = $mod.Arr[1];',
  18619. '$mod.Arr[2] = $mod.s;',
  18620. '$mod.Arr[3] = $mod.s;',
  18621. '$mod.Arr[4] = $mod.i;',
  18622. '$mod.Arr[5] = $mod.Arr[6];',
  18623. '$mod.Arr[7] = $mod.Arr[8];',
  18624. 'var $with = $mod.Arr;',
  18625. '$with[9] = $with[10];',
  18626. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18627. ' a: 9,',
  18628. ' p: $mod.Arr,',
  18629. ' get: function () {',
  18630. ' return this.p[this.a];',
  18631. ' },',
  18632. ' set: function (v) {',
  18633. ' this.p[this.a] = v;',
  18634. ' }',
  18635. '}, {',
  18636. ' a: 10,',
  18637. ' p: $mod.Arr,',
  18638. ' get: function () {',
  18639. ' return this.p[this.a];',
  18640. ' },',
  18641. ' set: function (v) {',
  18642. ' this.p[this.a] = v;',
  18643. ' }',
  18644. '});',
  18645. 'var $with1 = $mod.Arr;',
  18646. '$mod.v = $with1[14];',
  18647. '$with1[15] = 16;',
  18648. '$mod.v = $mod.Arr[17];',
  18649. '$mod.Arr[18] = $mod.v;',
  18650. '']));
  18651. end;
  18652. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18653. begin
  18654. StartProgram(false);
  18655. Add([
  18656. '{$modeswitch externalclass}',
  18657. 'type',
  18658. ' TJSArray = class external name ''Array2''',
  18659. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18660. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18661. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18662. ' end;',
  18663. ' TMyArr = class(TJSArray)',
  18664. ' procedure DoIt;',
  18665. ' end;',
  18666. 'procedure tmyarr.DoIt;',
  18667. 'begin',
  18668. ' Items[1]:=Items[2];',
  18669. ' SetItems(3,getItems(4));',
  18670. 'end;',
  18671. 'var',
  18672. ' Arr: tmyarr;',
  18673. ' s: string;',
  18674. ' i: longint;',
  18675. ' v: jsvalue;',
  18676. 'begin',
  18677. ' v:=arr[0];',
  18678. ' v:=arr.items[1];',
  18679. ' arr[2]:=s;',
  18680. ' arr.items[3]:=s;',
  18681. ' arr[4]:=i;',
  18682. ' arr[5]:=arr[6];',
  18683. ' arr.items[7]:=arr.items[8];',
  18684. ' with arr do items[9]:=items[10];',
  18685. ' with arr do begin',
  18686. ' v:=GetItems(14);',
  18687. ' setitems(15,16);',
  18688. ' end;',
  18689. '']);
  18690. ConvertProgram;
  18691. CheckSource('TestExternalClass_BracketAccessor_Call',
  18692. LinesToStr([ // statements
  18693. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18694. ' this.$init = function () {',
  18695. ' };',
  18696. ' this.$final = function () {',
  18697. ' };',
  18698. ' this.DoIt = function () {',
  18699. ' this[1] = this[2];',
  18700. ' this[3] = this[4];',
  18701. ' };',
  18702. '});',
  18703. 'this.Arr = null;',
  18704. 'this.s = "";',
  18705. 'this.i = 0;',
  18706. 'this.v = undefined;',
  18707. '']),
  18708. LinesToStr([ // $mod.$main
  18709. '$mod.v = $mod.Arr[0];',
  18710. '$mod.v = $mod.Arr[1];',
  18711. '$mod.Arr[2] = $mod.s;',
  18712. '$mod.Arr[3] = $mod.s;',
  18713. '$mod.Arr[4] = $mod.i;',
  18714. '$mod.Arr[5] = $mod.Arr[6];',
  18715. '$mod.Arr[7] = $mod.Arr[8];',
  18716. 'var $with = $mod.Arr;',
  18717. '$with[9] = $with[10];',
  18718. 'var $with1 = $mod.Arr;',
  18719. '$mod.v = $with1[14];',
  18720. '$with1[15] = 16;',
  18721. '']));
  18722. end;
  18723. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18724. begin
  18725. StartProgram(false);
  18726. Add('{$modeswitch externalclass}');
  18727. Add('type');
  18728. Add(' TJSArray = class external name ''Array2''');
  18729. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18730. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18731. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18732. Add(' end;');
  18733. Add('begin');
  18734. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18735. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18736. ConvertProgram;
  18737. end;
  18738. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18739. begin
  18740. StartProgram(false);
  18741. Add('{$modeswitch externalclass}');
  18742. Add('type');
  18743. Add(' TJSArray = class external name ''Array2''');
  18744. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18745. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18746. Add(' end;');
  18747. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18748. Add('begin end;');
  18749. Add('var');
  18750. Add(' Arr: tjsarray;');
  18751. Add(' v: jsvalue;');
  18752. Add('begin');
  18753. Add(' v:=arr[0];');
  18754. Add(' v:=arr.items[1];');
  18755. Add(' with arr do v:=items[2];');
  18756. Add(' doit(arr[3],arr[4]);');
  18757. ConvertProgram;
  18758. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18759. LinesToStr([ // statements
  18760. 'this.DoIt = function (vI, vJ) {',
  18761. '};',
  18762. 'this.Arr = null;',
  18763. 'this.v = undefined;',
  18764. '']),
  18765. LinesToStr([ // $mod.$main
  18766. '$mod.v = $mod.Arr[0];',
  18767. '$mod.v = $mod.Arr[1];',
  18768. 'var $with = $mod.Arr;',
  18769. '$mod.v = $with[2];',
  18770. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18771. '']));
  18772. end;
  18773. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18774. begin
  18775. StartProgram(false);
  18776. Add('{$modeswitch externalclass}');
  18777. Add('type');
  18778. Add(' TJSArray = class external name ''Array2''');
  18779. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18780. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18781. Add(' end;');
  18782. Add('var');
  18783. Add(' Arr: tjsarray;');
  18784. Add(' s: string;');
  18785. Add(' i: longint;');
  18786. Add(' v: jsvalue;');
  18787. Add('begin');
  18788. Add(' arr[2]:=s;');
  18789. Add(' arr.items[3]:=s;');
  18790. Add(' arr[4]:=i;');
  18791. Add(' with arr do items[5]:=i;');
  18792. ConvertProgram;
  18793. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18794. LinesToStr([ // statements
  18795. 'this.Arr = null;',
  18796. 'this.s = "";',
  18797. 'this.i = 0;',
  18798. 'this.v = undefined;',
  18799. '']),
  18800. LinesToStr([ // $mod.$main
  18801. '$mod.Arr[2] = $mod.s;',
  18802. '$mod.Arr[3] = $mod.s;',
  18803. '$mod.Arr[4] = $mod.i;',
  18804. 'var $with = $mod.Arr;',
  18805. '$with[5] = $mod.i;',
  18806. '']));
  18807. end;
  18808. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18809. begin
  18810. StartProgram(false);
  18811. Add('{$modeswitch externalclass}');
  18812. Add('type');
  18813. Add(' TJSArray = class external name ''Array2''');
  18814. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18815. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18816. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18817. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18818. Add(' end;');
  18819. Add('var');
  18820. Add(' Arr: tjsarray;');
  18821. Add(' s: string;');
  18822. Add(' i: longint;');
  18823. Add(' v: jsvalue;');
  18824. Add('begin');
  18825. Add(' arr[2]:=s;');
  18826. Add(' arr.items[3]:=s;');
  18827. Add(' arr.numbers[4]:=i;');
  18828. Add(' with arr do items[5]:=i;');
  18829. Add(' with arr do numbers[6]:=i;');
  18830. ConvertProgram;
  18831. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18832. LinesToStr([ // statements
  18833. 'this.Arr = null;',
  18834. 'this.s = "";',
  18835. 'this.i = 0;',
  18836. 'this.v = undefined;',
  18837. '']),
  18838. LinesToStr([ // $mod.$main
  18839. '$mod.Arr[2] = $mod.s;',
  18840. '$mod.Arr[3] = $mod.s;',
  18841. '$mod.Arr[4] = $mod.i;',
  18842. 'var $with = $mod.Arr;',
  18843. '$with[5] = $mod.i;',
  18844. 'var $with1 = $mod.Arr;',
  18845. '$with1[6] = $mod.i;',
  18846. '']));
  18847. end;
  18848. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18849. begin
  18850. StartProgram(false);
  18851. Add('{$modeswitch externalclass}');
  18852. Add('type');
  18853. Add(' TJSArray = class external name ''Array2''');
  18854. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18855. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18856. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18857. Add(' end;');
  18858. Add('var');
  18859. Add(' Arr: tjsarray;');
  18860. Add(' i: longint;');
  18861. Add(' IntArr: array of longint;');
  18862. Add(' v: jsvalue;');
  18863. Add('begin');
  18864. Add(' v:=arr.items[i];');
  18865. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18866. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18867. ConvertProgram;
  18868. CheckSource('TestExternalClass_BracketAccessor_Index',
  18869. LinesToStr([ // statements
  18870. 'this.Arr = null;',
  18871. 'this.i = 0;',
  18872. 'this.IntArr = [];',
  18873. 'this.v = undefined;',
  18874. '']),
  18875. LinesToStr([ // $mod.$main
  18876. '$mod.v = $mod.Arr[$mod.i];',
  18877. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18878. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18879. '']));
  18880. end;
  18881. procedure TTestModule.TestExternalClass_ForInJSObject;
  18882. begin
  18883. StartProgram(false);
  18884. Add([
  18885. '{$modeswitch externalclass}',
  18886. 'type',
  18887. ' TJSObject = class external name ''Object''',
  18888. ' end;',
  18889. 'var',
  18890. ' o: TJSObject;',
  18891. ' key: string;',
  18892. 'begin',
  18893. ' for key in o do',
  18894. ' if key=''abc'' then ;',
  18895. '']);
  18896. ConvertProgram;
  18897. CheckSource('TestExternalClass_ForInJSObject',
  18898. LinesToStr([ // statements
  18899. 'this.o = null;',
  18900. 'this.key = "";',
  18901. '']),
  18902. LinesToStr([ // $mod.$main
  18903. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18904. '']));
  18905. end;
  18906. procedure TTestModule.TestExternalClass_ForInJSArray;
  18907. begin
  18908. StartProgram(false);
  18909. Add([
  18910. '{$modeswitch externalclass}',
  18911. 'type',
  18912. ' TJSInt8Array = class external name ''Int8Array''',
  18913. ' private',
  18914. ' flength: NativeInt external name ''length'';',
  18915. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18916. ' public',
  18917. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18918. ' property Length: NativeInt read flength;',
  18919. ' end;',
  18920. 'var',
  18921. ' a: TJSInt8Array;',
  18922. ' value: shortint;',
  18923. 'begin',
  18924. ' for value in a do',
  18925. ' if value=3 then ;',
  18926. '']);
  18927. ConvertProgram;
  18928. CheckSource('TestExternalClass_ForInJSArray',
  18929. LinesToStr([ // statements
  18930. 'this.a = null;',
  18931. 'this.value = 0;',
  18932. '']),
  18933. LinesToStr([ // $mod.$main
  18934. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18935. ' $mod.value = $in[$l];',
  18936. ' if ($mod.value === 3) ;',
  18937. '};',
  18938. '']));
  18939. end;
  18940. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18941. begin
  18942. AddModuleWithIntfImplSrc('unit2.pas',
  18943. LinesToStr([
  18944. '{$modeswitch externalclass}',
  18945. 'type',
  18946. ' TJSBufferSource = class external name ''BufferSource''',
  18947. ' end;',
  18948. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18949. '']),
  18950. '');
  18951. AddModuleWithIntfImplSrc('unit3.pas',
  18952. LinesToStr([
  18953. '{$modeswitch externalclass}',
  18954. 'type',
  18955. ' TJSBufferSource = class external name ''BufferSource''',
  18956. ' end;',
  18957. '']),
  18958. '');
  18959. StartUnit(true);
  18960. Add([
  18961. 'interface',
  18962. 'uses unit2, unit3;',
  18963. 'procedure DoSome(s: TJSBufferSource);',
  18964. 'implementation',
  18965. 'procedure DoSome(s: TJSBufferSource);',
  18966. 'begin',
  18967. ' DoIt(s);',
  18968. 'end;',
  18969. '']);
  18970. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18971. nIncompatibleTypeArgNo);
  18972. ConvertUnit;
  18973. end;
  18974. procedure TTestModule.TestClassInterface_Corba;
  18975. begin
  18976. StartProgram(false);
  18977. Add([
  18978. '{$interfaces corba}',
  18979. 'type',
  18980. ' IUnknown = interface;',
  18981. ' IUnknown = interface',
  18982. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18983. ' end;',
  18984. ' IInterface = IUnknown;',
  18985. ' IBird = interface(IInterface)',
  18986. ' function GetSize: longint;',
  18987. ' procedure SetSize(i: longint);',
  18988. ' property Size: longint read GetSize write SetSize;',
  18989. ' procedure DoIt(i: longint);',
  18990. ' end;',
  18991. ' TObject = class',
  18992. ' end;',
  18993. ' TBird = class(TObject,IBird)',
  18994. ' function GetSize: longint; virtual; abstract;',
  18995. ' procedure SetSize(i: longint); virtual; abstract;',
  18996. ' procedure DoIt(i: longint); virtual; abstract;',
  18997. ' end;',
  18998. 'var',
  18999. ' BirdIntf: IBird;',
  19000. 'begin',
  19001. ' BirdIntf.Size:=BirdIntf.Size;',
  19002. '']);
  19003. ConvertProgram;
  19004. CheckSource('TestClassInterface_Corba',
  19005. LinesToStr([ // statements
  19006. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  19007. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  19008. 'rtl.createClass(this, "TObject", null, function () {',
  19009. ' this.$init = function () {',
  19010. ' };',
  19011. ' this.$final = function () {',
  19012. ' };',
  19013. '});',
  19014. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19015. ' rtl.addIntf(this, $mod.IBird);',
  19016. '});',
  19017. 'this.BirdIntf = null;',
  19018. '']),
  19019. LinesToStr([ // $mod.$main
  19020. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  19021. '']));
  19022. end;
  19023. procedure TTestModule.TestClassInterface_ProcExternalFail;
  19024. begin
  19025. StartProgram(false);
  19026. Add([
  19027. '{$interfaces corba}',
  19028. 'type',
  19029. ' IUnknown = interface',
  19030. ' procedure DoIt; external name ''foo'';',
  19031. ' end;',
  19032. 'begin']);
  19033. SetExpectedParserError(
  19034. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  19035. nParserNoFieldsAllowed);
  19036. ConvertProgram;
  19037. end;
  19038. procedure TTestModule.TestClassInterface_Overloads;
  19039. begin
  19040. StartProgram(false);
  19041. Add([
  19042. '{$interfaces corba}',
  19043. 'type',
  19044. ' integer = longint;',
  19045. ' IUnknown = interface',
  19046. ' procedure DoIt(i: integer);',
  19047. ' procedure DoIt(s: string);',
  19048. ' end;',
  19049. ' IBird = interface(IUnknown)',
  19050. ' procedure DoIt(b: boolean); overload;',
  19051. ' end;',
  19052. ' TObject = class',
  19053. ' end;',
  19054. ' TBird = class(TObject,IBird)',
  19055. ' procedure DoIt(o: TObject);',
  19056. ' procedure DoIt(s: string);',
  19057. ' procedure DoIt(i: integer);',
  19058. ' procedure DoIt(b: boolean);',
  19059. ' end;',
  19060. 'procedure TBird.DoIt(o: TObject); begin end;',
  19061. 'procedure TBird.DoIt(s: string); begin end;',
  19062. 'procedure TBird.DoIt(i: integer); begin end;',
  19063. 'procedure TBird.DoIt(b: boolean); begin end;',
  19064. 'var',
  19065. ' BirdIntf: IBird;',
  19066. 'begin',
  19067. ' BirdIntf.DoIt(3);',
  19068. ' BirdIntf.DoIt(''abc'');',
  19069. ' BirdIntf.DoIt(true);',
  19070. '']);
  19071. ConvertProgram;
  19072. CheckSource('TestClassInterface_Overloads',
  19073. LinesToStr([ // statements
  19074. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  19075. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  19076. 'rtl.createClass(this, "TObject", null, function () {',
  19077. ' this.$init = function () {',
  19078. ' };',
  19079. ' this.$final = function () {',
  19080. ' };',
  19081. '});',
  19082. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19083. ' this.DoIt = function (o) {',
  19084. ' };',
  19085. ' this.DoIt$1 = function (s) {',
  19086. ' };',
  19087. ' this.DoIt$2 = function (i) {',
  19088. ' };',
  19089. ' this.DoIt$3 = function (b) {',
  19090. ' };',
  19091. ' rtl.addIntf(this, $mod.IBird, {',
  19092. ' DoIt$2: "DoIt$3",',
  19093. ' DoIt: "DoIt$2"',
  19094. ' });',
  19095. '});',
  19096. 'this.BirdIntf = null;',
  19097. '']),
  19098. LinesToStr([ // $mod.$main
  19099. '$mod.BirdIntf.DoIt(3);',
  19100. '$mod.BirdIntf.DoIt$1("abc");',
  19101. '$mod.BirdIntf.DoIt$2(true);',
  19102. '']));
  19103. end;
  19104. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  19105. begin
  19106. StartProgram(false);
  19107. Add([
  19108. '{$interfaces corba}',
  19109. 'type',
  19110. ' IBird = interface',
  19111. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19112. ' end;',
  19113. ' IDog = interface',
  19114. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19115. ' end;',
  19116. ' TObject = class(IBird,IDog)',
  19117. ' end;',
  19118. 'begin']);
  19119. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  19120. nDuplicateGUIDXInYZ);
  19121. ConvertProgram;
  19122. end;
  19123. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  19124. begin
  19125. StartProgram(false);
  19126. Add([
  19127. '{$interfaces corba}',
  19128. 'type',
  19129. ' IAnimal = interface',
  19130. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19131. ' end;',
  19132. ' IBird = interface(IAnimal)',
  19133. ' end;',
  19134. ' IHawk = interface(IBird)',
  19135. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19136. ' end;',
  19137. 'begin']);
  19138. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  19139. nDuplicateGUIDXInYZ);
  19140. ConvertProgram;
  19141. end;
  19142. procedure TTestModule.TestClassInterface_AncestorImpl;
  19143. begin
  19144. StartProgram(false);
  19145. Add([
  19146. '{$interfaces corba}',
  19147. 'type',
  19148. ' integer = longint;',
  19149. ' IUnknown = interface',
  19150. ' procedure DoIt(i: integer);',
  19151. ' end;',
  19152. ' IBird = interface',
  19153. ' procedure Fly(i: integer);',
  19154. ' end;',
  19155. ' TObject = class(IUnknown)',
  19156. ' procedure DoIt(i: integer);',
  19157. ' end;',
  19158. ' TBird = class(IBird)',
  19159. ' procedure Fly(i: integer);',
  19160. ' end;',
  19161. 'procedure TObject.DoIt(i: integer); begin end;',
  19162. 'procedure TBird.Fly(i: integer); begin end;',
  19163. 'begin',
  19164. '']);
  19165. ConvertProgram;
  19166. CheckSource('TestClassInterface_AncestorIntf',
  19167. LinesToStr([ // statements
  19168. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  19169. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  19170. 'rtl.createClass(this, "TObject", null, function () {',
  19171. ' this.$init = function () {',
  19172. ' };',
  19173. ' this.$final = function () {',
  19174. ' };',
  19175. ' this.DoIt = function (i) {',
  19176. ' };',
  19177. ' rtl.addIntf(this, $mod.IUnknown);',
  19178. '});',
  19179. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19180. ' this.Fly = function (i) {',
  19181. ' };',
  19182. ' rtl.addIntf(this, $mod.IBird);',
  19183. ' rtl.addIntf(this, $mod.IUnknown);',
  19184. '});',
  19185. '']),
  19186. LinesToStr([ // $mod.$main
  19187. '']));
  19188. end;
  19189. procedure TTestModule.TestClassInterface_ImplReintroduce;
  19190. begin
  19191. StartProgram(false);
  19192. Add([
  19193. '{$interfaces corba}',
  19194. 'type',
  19195. ' integer = longint;',
  19196. ' IBird = interface',
  19197. ' procedure DoIt(i: integer);',
  19198. ' end;',
  19199. ' TObject = class',
  19200. ' procedure DoIt(i: integer);',
  19201. ' end;',
  19202. ' TBird = class(IBird)',
  19203. ' procedure DoIt(i: integer); virtual; reintroduce;',
  19204. ' end;',
  19205. 'procedure TObject.DoIt(i: integer); begin end;',
  19206. 'procedure TBird.DoIt(i: integer); begin end;',
  19207. 'begin',
  19208. '']);
  19209. ConvertProgram;
  19210. CheckSource('TestClassInterface_ImplReintroduce',
  19211. LinesToStr([ // statements
  19212. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  19213. 'rtl.createClass(this, "TObject", null, function () {',
  19214. ' this.$init = function () {',
  19215. ' };',
  19216. ' this.$final = function () {',
  19217. ' };',
  19218. ' this.DoIt = function (i) {',
  19219. ' };',
  19220. '});',
  19221. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19222. ' this.DoIt$1 = function (i) {',
  19223. ' };',
  19224. ' rtl.addIntf(this, $mod.IBird, {',
  19225. ' DoIt: "DoIt$1"',
  19226. ' });',
  19227. '});',
  19228. '']),
  19229. LinesToStr([ // $mod.$main
  19230. '']));
  19231. end;
  19232. procedure TTestModule.TestClassInterface_MethodResolution;
  19233. begin
  19234. StartProgram(false);
  19235. Add([
  19236. '{$interfaces corba}',
  19237. 'type',
  19238. ' IUnknown = interface',
  19239. ' procedure Walk(i: longint);',
  19240. ' end;',
  19241. ' IBird = interface(IUnknown)',
  19242. ' procedure Walk(b: boolean); overload;',
  19243. ' procedure Fly(s: string);',
  19244. ' end;',
  19245. ' TObject = class',
  19246. ' end;',
  19247. ' TBird = class(TObject,IBird)',
  19248. ' procedure IBird.Fly = Move;',
  19249. ' procedure IBird.Walk = Hop;',
  19250. ' procedure Hop(i: longint);',
  19251. ' procedure Move(s: string);',
  19252. ' procedure Hop(b: boolean);',
  19253. ' end;',
  19254. 'procedure TBird.Move(s: string); begin end;',
  19255. 'procedure TBird.Hop(i: longint); begin end;',
  19256. 'procedure TBird.Hop(b: boolean); begin end;',
  19257. 'var',
  19258. ' BirdIntf: IBird;',
  19259. 'begin',
  19260. ' BirdIntf.Walk(3);',
  19261. ' BirdIntf.Walk(true);',
  19262. ' BirdIntf.Fly(''abc'');',
  19263. '']);
  19264. ConvertProgram;
  19265. CheckSource('TestClassInterface_MethodResolution',
  19266. LinesToStr([ // statements
  19267. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  19268. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  19269. 'rtl.createClass(this, "TObject", null, function () {',
  19270. ' this.$init = function () {',
  19271. ' };',
  19272. ' this.$final = function () {',
  19273. ' };',
  19274. '});',
  19275. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19276. ' this.Hop = function (i) {',
  19277. ' };',
  19278. ' this.Move = function (s) {',
  19279. ' };',
  19280. ' this.Hop$1 = function (b) {',
  19281. ' };',
  19282. ' rtl.addIntf(this, $mod.IBird, {',
  19283. ' Walk$1: "Hop$1",',
  19284. ' Fly: "Move",',
  19285. ' Walk: "Hop"',
  19286. ' });',
  19287. '});',
  19288. 'this.BirdIntf = null;',
  19289. '']),
  19290. LinesToStr([ // $mod.$main
  19291. '$mod.BirdIntf.Walk(3);',
  19292. '$mod.BirdIntf.Walk$1(true);',
  19293. '$mod.BirdIntf.Fly("abc");',
  19294. '']));
  19295. end;
  19296. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  19297. begin
  19298. StartProgram(false);
  19299. Add([
  19300. '{$interfaces com}',
  19301. 'type',
  19302. ' IUnknown = interface',
  19303. ' function _AddRef: longint;',
  19304. ' procedure Walk;',
  19305. ' end;',
  19306. ' IBird = interface end;',
  19307. ' IDog = interface end;',
  19308. ' TObject = class(IBird,IDog)',
  19309. ' function _AddRef: longint; virtual; abstract;',
  19310. ' procedure Walk; virtual; abstract;',
  19311. ' end;',
  19312. ' TBird = class(IUnknown)',
  19313. ' end;',
  19314. 'begin',
  19315. '']);
  19316. ConvertProgram;
  19317. CheckSource('TestClassInterface_COM_AncestorLess',
  19318. LinesToStr([ // statements
  19319. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  19320. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  19321. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  19322. 'rtl.createClass(this, "TObject", null, function () {',
  19323. ' this.$init = function () {',
  19324. ' };',
  19325. ' this.$final = function () {',
  19326. ' };',
  19327. ' rtl.addIntf(this, $mod.IBird);',
  19328. ' rtl.addIntf(this, $mod.IDog);',
  19329. '});',
  19330. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19331. ' rtl.addIntf(this, $mod.IUnknown);',
  19332. ' rtl.addIntf(this, $mod.IBird);',
  19333. ' rtl.addIntf(this, $mod.IDog);',
  19334. '});',
  19335. '']),
  19336. LinesToStr([ // $mod.$main
  19337. '']));
  19338. end;
  19339. procedure TTestModule.TestClassInterface_MethodOverride;
  19340. begin
  19341. StartProgram(false);
  19342. Add([
  19343. '{$interfaces corba}',
  19344. 'type',
  19345. ' IUnknown = interface',
  19346. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  19347. ' procedure Go;',
  19348. ' end;',
  19349. ' TObject = class(IUnknown)',
  19350. ' procedure Go; virtual; abstract;',
  19351. ' end;',
  19352. ' TBird = class',
  19353. ' procedure Go; override;',
  19354. ' end;',
  19355. ' TCat = class(TObject)',
  19356. ' procedure Go; override;',
  19357. ' end;',
  19358. ' TDog = class(TObject, IUnknown)',
  19359. ' procedure Go; override;',
  19360. ' end;',
  19361. 'procedure TBird.Go; begin end;',
  19362. 'procedure TCat.Go; begin end;',
  19363. 'procedure TDog.Go; begin end;',
  19364. 'begin',
  19365. '']);
  19366. ConvertProgram;
  19367. CheckSource('TestClassInterface_MethodOverride',
  19368. LinesToStr([ // statements
  19369. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  19370. 'rtl.createClass(this, "TObject", null, function () {',
  19371. ' this.$init = function () {',
  19372. ' };',
  19373. ' this.$final = function () {',
  19374. ' };',
  19375. ' rtl.addIntf(this, $mod.IUnknown);',
  19376. '});',
  19377. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19378. ' this.Go = function () {',
  19379. ' };',
  19380. ' rtl.addIntf(this, $mod.IUnknown);',
  19381. '});',
  19382. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  19383. ' this.Go = function () {',
  19384. ' };',
  19385. ' rtl.addIntf(this, $mod.IUnknown);',
  19386. '});',
  19387. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  19388. ' this.Go = function () {',
  19389. ' };',
  19390. ' rtl.addIntf(this, $mod.IUnknown);',
  19391. '});',
  19392. '']),
  19393. LinesToStr([ // $mod.$main
  19394. '']));
  19395. end;
  19396. procedure TTestModule.TestClassInterface_Corba_Delegation;
  19397. begin
  19398. StartProgram(false);
  19399. Add([
  19400. '{$interfaces corba}',
  19401. 'type',
  19402. ' IUnknown = interface',
  19403. ' end;',
  19404. ' IBird = interface(IUnknown)',
  19405. ' procedure Fly(s: string);',
  19406. ' end;',
  19407. ' IEagle = interface(IBird)',
  19408. ' end;',
  19409. ' IDove = interface(IBird)',
  19410. ' end;',
  19411. ' ISwallow = interface(IBird)',
  19412. ' end;',
  19413. ' TObject = class',
  19414. ' end;',
  19415. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19416. ' procedure Fly(s: string); virtual; abstract;',
  19417. ' end;',
  19418. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19419. ' FBirdIntf: IBird;',
  19420. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19421. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19422. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19423. ' FDoveObj: TBird;',
  19424. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19425. ' function GetSwallowObj: TBird; virtual; abstract;',
  19426. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19427. ' end;',
  19428. 'begin',
  19429. '']);
  19430. ConvertProgram;
  19431. CheckSource('TestClassInterface_Corba_Delegation',
  19432. LinesToStr([ // statements
  19433. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19434. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19435. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19436. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19437. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19438. 'rtl.createClass(this, "TObject", null, function () {',
  19439. ' this.$init = function () {',
  19440. ' };',
  19441. ' this.$final = function () {',
  19442. ' };',
  19443. '});',
  19444. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19445. ' rtl.addIntf(this, $mod.IBird);',
  19446. ' rtl.addIntf(this, $mod.IEagle);',
  19447. ' rtl.addIntf(this, $mod.IDove);',
  19448. ' rtl.addIntf(this, $mod.ISwallow);',
  19449. '});',
  19450. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19451. ' this.$init = function () {',
  19452. ' $mod.TObject.$init.call(this);',
  19453. ' this.FBirdIntf = null;',
  19454. ' this.FDoveObj = null;',
  19455. ' };',
  19456. ' this.$final = function () {',
  19457. ' this.FBirdIntf = undefined;',
  19458. ' this.FDoveObj = undefined;',
  19459. ' $mod.TObject.$final.call(this);',
  19460. ' };',
  19461. ' this.$intfmaps = {',
  19462. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19463. ' return this.FBirdIntf;',
  19464. ' },',
  19465. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19466. ' return this.GetEagleIntf();',
  19467. ' },',
  19468. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19469. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19470. ' },',
  19471. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19472. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19473. ' }',
  19474. ' };',
  19475. '});',
  19476. '']),
  19477. LinesToStr([ // $mod.$main
  19478. '']));
  19479. end;
  19480. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19481. begin
  19482. StartProgram(false);
  19483. Add([
  19484. '{$interfaces corba}',
  19485. 'type',
  19486. ' IUnknown = interface',
  19487. ' end;',
  19488. ' IBird = interface(IUnknown)',
  19489. ' procedure Fly(s: string);',
  19490. ' end;',
  19491. ' IEagle = interface(IBird)',
  19492. ' end;',
  19493. ' IDove = interface(IBird)',
  19494. ' end;',
  19495. ' ISwallow = interface(IBird)',
  19496. ' end;',
  19497. ' TObject = class',
  19498. ' end;',
  19499. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19500. ' procedure Fly(s: string); virtual; abstract;',
  19501. ' end;',
  19502. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19503. ' private',
  19504. ' class var FBirdIntf: IBird;',
  19505. ' class var FDoveObj: TBird;',
  19506. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19507. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19508. ' protected',
  19509. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19510. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19511. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19512. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19513. ' end;',
  19514. 'begin',
  19515. '']);
  19516. ConvertProgram;
  19517. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19518. LinesToStr([ // statements
  19519. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19520. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19521. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19522. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19523. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19524. 'rtl.createClass(this, "TObject", null, function () {',
  19525. ' this.$init = function () {',
  19526. ' };',
  19527. ' this.$final = function () {',
  19528. ' };',
  19529. '});',
  19530. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19531. ' rtl.addIntf(this, $mod.IBird);',
  19532. ' rtl.addIntf(this, $mod.IEagle);',
  19533. ' rtl.addIntf(this, $mod.IDove);',
  19534. ' rtl.addIntf(this, $mod.ISwallow);',
  19535. '});',
  19536. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19537. ' this.FBirdIntf = null;',
  19538. ' this.FDoveObj = null;',
  19539. ' this.$intfmaps = {',
  19540. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19541. ' return this.FBirdIntf;',
  19542. ' },',
  19543. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19544. ' return this.GetEagleIntf();',
  19545. ' },',
  19546. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19547. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19548. ' },',
  19549. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19550. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19551. ' }',
  19552. ' };',
  19553. '});',
  19554. '']),
  19555. LinesToStr([ // $mod.$main
  19556. '']));
  19557. end;
  19558. procedure TTestModule.TestClassInterface_Corba_Operators;
  19559. begin
  19560. StartProgram(false);
  19561. Add([
  19562. '{$interfaces corba}',
  19563. 'type',
  19564. ' IUnknown = interface',
  19565. ' end;',
  19566. ' IBird = interface(IUnknown)',
  19567. ' function GetItems(Index: longint): longint;',
  19568. ' procedure SetItems(Index: longint; Value: longint);',
  19569. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19570. ' end;',
  19571. ' TObject = class',
  19572. ' end;',
  19573. ' TBird = class(TObject,IBird)',
  19574. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19575. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19576. ' end;',
  19577. 'var',
  19578. ' IntfVar: IBird = nil;',
  19579. ' IntfVar2: IBird;',
  19580. ' ObjVar: TBird;',
  19581. ' v: JSValue;',
  19582. 'begin',
  19583. ' IntfVar:=nil;',
  19584. ' IntfVar[3]:=IntfVar[4];',
  19585. ' if Assigned(IntfVar) then ;',
  19586. ' IntfVar:=IntfVar2;',
  19587. ' IntfVar:=ObjVar;',
  19588. ' if IntfVar=IntfVar2 then ;',
  19589. ' if IntfVar<>IntfVar2 then ;',
  19590. ' if IntfVar is IBird then ;',
  19591. ' if IntfVar is TBird then ;',
  19592. ' if ObjVar is IBird then ;',
  19593. ' IntfVar:=IntfVar2 as IBird;',
  19594. ' ObjVar:=IntfVar2 as TBird;',
  19595. ' IntfVar:=ObjVar as IBird;',
  19596. ' IntfVar:=IBird(IntfVar2);',
  19597. ' ObjVar:=TBird(IntfVar);',
  19598. ' IntfVar:=IBird(ObjVar);',
  19599. ' v:=IntfVar;',
  19600. ' IntfVar:=IBird(v);',
  19601. ' if v is IBird then ;',
  19602. ' v:=JSValue(IntfVar);',
  19603. ' v:=IBird;',
  19604. '']);
  19605. ConvertProgram;
  19606. CheckSource('TestClassInterface_Corba_Operators',
  19607. LinesToStr([ // statements
  19608. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19609. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19610. 'rtl.createClass(this, "TObject", null, function () {',
  19611. ' this.$init = function () {',
  19612. ' };',
  19613. ' this.$final = function () {',
  19614. ' };',
  19615. '});',
  19616. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19617. ' rtl.addIntf(this, $mod.IBird);',
  19618. '});',
  19619. 'this.IntfVar = null;',
  19620. 'this.IntfVar2 = null;',
  19621. 'this.ObjVar = null;',
  19622. 'this.v = undefined;',
  19623. '']),
  19624. LinesToStr([ // $mod.$main
  19625. '$mod.IntfVar = null;',
  19626. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19627. 'if ($mod.IntfVar != null) ;',
  19628. '$mod.IntfVar = $mod.IntfVar2;',
  19629. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19630. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19631. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19632. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19633. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19634. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19635. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19636. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19637. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19638. '$mod.IntfVar = $mod.IntfVar2;',
  19639. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19640. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19641. '$mod.v = $mod.IntfVar;',
  19642. '$mod.IntfVar = rtl.getObject($mod.v);',
  19643. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19644. '$mod.v = $mod.IntfVar;',
  19645. '$mod.v = $mod.IBird;',
  19646. '']));
  19647. end;
  19648. procedure TTestModule.TestClassInterface_Corba_Args;
  19649. begin
  19650. StartProgram(false);
  19651. Add([
  19652. '{$interfaces corba}',
  19653. 'type',
  19654. ' IUnknown = interface',
  19655. ' end;',
  19656. ' IBird = interface(IUnknown)',
  19657. ' end;',
  19658. ' TObject = class',
  19659. ' end;',
  19660. ' TBird = class(TObject,IBird)',
  19661. ' end;',
  19662. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19663. 'begin',
  19664. ' DoIt(i,i,i);',
  19665. 'end;',
  19666. 'procedure Change(var i: IBird; out j: IBird);',
  19667. 'begin',
  19668. ' DoIt(i,i,i);',
  19669. ' Change(i,i);',
  19670. 'end;',
  19671. 'var',
  19672. ' i: IBird;',
  19673. ' o: TBird;',
  19674. 'begin',
  19675. ' DoIt(i,i,i);',
  19676. ' Change(i,i);',
  19677. ' DoIt(o,o,o);',
  19678. '']);
  19679. ConvertProgram;
  19680. CheckSource('TestClassInterface_Corba_Args',
  19681. LinesToStr([ // statements
  19682. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19683. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19684. 'rtl.createClass(this, "TObject", null, function () {',
  19685. ' this.$init = function () {',
  19686. ' };',
  19687. ' this.$final = function () {',
  19688. ' };',
  19689. '});',
  19690. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19691. ' rtl.addIntf(this, $mod.IBird);',
  19692. '});',
  19693. 'this.DoIt = function (u, i, j) {',
  19694. ' $mod.DoIt({',
  19695. ' get: function () {',
  19696. ' return i;',
  19697. ' },',
  19698. ' set: function (v) {',
  19699. ' i = v;',
  19700. ' }',
  19701. ' }, i, i);',
  19702. '};',
  19703. 'this.Change = function (i, j) {',
  19704. ' $mod.DoIt(i, i.get(), i.get());',
  19705. ' $mod.Change(i, i);',
  19706. '};',
  19707. 'this.i = null;',
  19708. 'this.o = null;',
  19709. '']),
  19710. LinesToStr([ // $mod.$main
  19711. '$mod.DoIt({',
  19712. ' p: $mod,',
  19713. ' get: function () {',
  19714. ' return this.p.i;',
  19715. ' },',
  19716. ' set: function (v) {',
  19717. ' this.p.i = v;',
  19718. ' }',
  19719. '}, $mod.i, $mod.i);',
  19720. '$mod.Change({',
  19721. ' p: $mod,',
  19722. ' get: function () {',
  19723. ' return this.p.i;',
  19724. ' },',
  19725. ' set: function (v) {',
  19726. ' this.p.i = v;',
  19727. ' }',
  19728. '}, {',
  19729. ' p: $mod,',
  19730. ' get: function () {',
  19731. ' return this.p.i;',
  19732. ' },',
  19733. ' set: function (v) {',
  19734. ' this.p.i = v;',
  19735. ' }',
  19736. '});',
  19737. '$mod.DoIt({',
  19738. ' p: $mod,',
  19739. ' get: function () {',
  19740. ' return this.p.o;',
  19741. ' },',
  19742. ' set: function (v) {',
  19743. ' this.p.o = v;',
  19744. ' }',
  19745. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19746. '']));
  19747. end;
  19748. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19749. begin
  19750. StartProgram(false);
  19751. Add([
  19752. '{$interfaces corba}',
  19753. 'type',
  19754. ' IUnknown = interface end;',
  19755. ' TObject = class',
  19756. ' Id: longint;',
  19757. ' end;',
  19758. ' IEnumerator = interface(IUnknown)',
  19759. ' function GetCurrent: TObject;',
  19760. ' function MoveNext: Boolean;',
  19761. ' property Current: TObject read GetCurrent;',
  19762. ' end;',
  19763. ' IEnumerable = interface(IUnknown)',
  19764. ' function GetEnumerator: IEnumerator;',
  19765. ' end;',
  19766. 'var',
  19767. ' o: TObject;',
  19768. ' i: IEnumerable;',
  19769. 'begin',
  19770. ' for o in i do o.Id:=3;',
  19771. '']);
  19772. ConvertProgram;
  19773. CheckSource('TestClassInterface_Corba_ForIn',
  19774. LinesToStr([ // statements
  19775. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19776. 'rtl.createClass(this, "TObject", null, function () {',
  19777. ' this.$init = function () {',
  19778. ' this.Id = 0;',
  19779. ' };',
  19780. ' this.$final = function () {',
  19781. ' };',
  19782. '});',
  19783. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19784. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19785. 'this.o = null;',
  19786. 'this.i = null;',
  19787. '']),
  19788. LinesToStr([ // $mod.$main
  19789. 'var $in = $mod.i.GetEnumerator();',
  19790. 'while ($in.MoveNext()) {',
  19791. ' $mod.o = $in.GetCurrent();',
  19792. ' $mod.o.Id = 3;',
  19793. '};',
  19794. '']));
  19795. end;
  19796. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19797. begin
  19798. StartProgram(false);
  19799. Add([
  19800. '{$interfaces com}',
  19801. 'type',
  19802. ' IUnknown = interface',
  19803. ' function _AddRef: longint;',
  19804. ' function _Release: longint;',
  19805. ' end;',
  19806. ' TObject = class(IUnknown)',
  19807. ' function _AddRef: longint; virtual; abstract;',
  19808. ' function _Release: longint; virtual; abstract;',
  19809. ' end;',
  19810. 'var',
  19811. ' i: IUnknown;',
  19812. 'procedure DoGlobal(o: TObject);',
  19813. 'begin',
  19814. ' i:=nil;',
  19815. ' i:=o;',
  19816. ' i:=i;',
  19817. 'end;',
  19818. 'procedure DoLocal(o: TObject);',
  19819. 'const k: IUnknown = nil;',
  19820. 'var j: IUnknown;',
  19821. 'begin',
  19822. ' k:=o;',
  19823. ' k:=i;',
  19824. ' j:=o;',
  19825. ' j:=i;',
  19826. 'end;',
  19827. 'var o: TObject;',
  19828. 'begin',
  19829. ' i:=nil;',
  19830. ' i:=o;',
  19831. '']);
  19832. ConvertProgram;
  19833. CheckSource('TestClassInterface_COM_AssignVar',
  19834. LinesToStr([ // statements
  19835. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19836. 'rtl.createClass(this, "TObject", null, function () {',
  19837. ' this.$init = function () {',
  19838. ' };',
  19839. ' this.$final = function () {',
  19840. ' };',
  19841. ' rtl.addIntf(this, $mod.IUnknown);',
  19842. '});',
  19843. 'this.i = null;',
  19844. 'this.DoGlobal = function (o) {',
  19845. ' rtl.setIntfP($mod, "i", null);',
  19846. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19847. ' rtl.setIntfP($mod, "i", $mod.i);',
  19848. '};',
  19849. 'var k = null;',
  19850. 'this.DoLocal = function (o) {',
  19851. ' var j = null;',
  19852. ' try{',
  19853. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19854. ' k = rtl.setIntfL(k, $mod.i);',
  19855. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19856. ' j = rtl.setIntfL(j, $mod.i);',
  19857. ' }finally{',
  19858. ' rtl._Release(j);',
  19859. ' };',
  19860. '};',
  19861. 'this.o = null;',
  19862. '']),
  19863. LinesToStr([ // $mod.$main
  19864. 'rtl.setIntfP($mod, "i", null);',
  19865. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19866. '']));
  19867. end;
  19868. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19869. begin
  19870. StartProgram(false);
  19871. Add([
  19872. '{$interfaces com}',
  19873. 'type',
  19874. ' IUnknown = interface',
  19875. ' function _AddRef: longint;',
  19876. ' function _Release: longint;',
  19877. ' end;',
  19878. ' TObject = class(IUnknown)',
  19879. ' function _AddRef: longint; virtual; abstract;',
  19880. ' function _Release: longint; virtual; abstract;',
  19881. ' end;',
  19882. 'procedure DoDefault(i, j: IUnknown);',
  19883. 'begin',
  19884. ' i:=nil;',
  19885. ' i:=j;',
  19886. 'end;',
  19887. 'begin',
  19888. '']);
  19889. ConvertProgram;
  19890. CheckSource('TestClassInterface_COM_AssignArg',
  19891. LinesToStr([ // statements
  19892. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19893. 'rtl.createClass(this, "TObject", null, function () {',
  19894. ' this.$init = function () {',
  19895. ' };',
  19896. ' this.$final = function () {',
  19897. ' };',
  19898. ' rtl.addIntf(this, $mod.IUnknown);',
  19899. '});',
  19900. 'this.DoDefault = function (i, j) {',
  19901. ' rtl._AddRef(i);',
  19902. ' try {',
  19903. ' i = rtl.setIntfL(i, null);',
  19904. ' i = rtl.setIntfL(i, j);',
  19905. ' } finally {',
  19906. ' rtl._Release(i);',
  19907. ' };',
  19908. '};',
  19909. '']),
  19910. LinesToStr([ // $mod.$main
  19911. '']));
  19912. end;
  19913. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19914. begin
  19915. StartProgram(false);
  19916. Add([
  19917. '{$interfaces com}',
  19918. 'type',
  19919. ' IUnknown = interface',
  19920. ' function _AddRef: longint;',
  19921. ' function _Release: longint;',
  19922. ' end;',
  19923. ' TObject = class(IUnknown)',
  19924. ' function _AddRef: longint; virtual; abstract;',
  19925. ' function _Release: longint; virtual; abstract;',
  19926. ' end;',
  19927. 'function DoDefault(i: IUnknown): IUnknown;',
  19928. 'begin',
  19929. ' Result:=i;',
  19930. ' if Result<>nil then exit;',
  19931. 'end;',
  19932. 'begin',
  19933. '']);
  19934. ConvertProgram;
  19935. CheckSource('TestClassInterface_COM_FunctionResult',
  19936. LinesToStr([ // statements
  19937. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19938. 'rtl.createClass(this, "TObject", null, function () {',
  19939. ' this.$init = function () {',
  19940. ' };',
  19941. ' this.$final = function () {',
  19942. ' };',
  19943. ' rtl.addIntf(this, $mod.IUnknown);',
  19944. '});',
  19945. 'this.DoDefault = function (i) {',
  19946. ' var Result = null;',
  19947. ' var $ok = false;',
  19948. ' try {',
  19949. ' Result = rtl.setIntfL(Result, i);',
  19950. ' if(Result !== null){',
  19951. ' $ok = true;',
  19952. ' return Result;',
  19953. ' };',
  19954. ' $ok = true;',
  19955. ' } finally {',
  19956. ' if(!$ok) rtl._Release(Result);',
  19957. ' };',
  19958. ' return Result;',
  19959. '};',
  19960. '']),
  19961. LinesToStr([ // $mod.$main
  19962. '']));
  19963. end;
  19964. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19965. begin
  19966. StartProgram(false);
  19967. Add([
  19968. '{$interfaces com}',
  19969. 'type',
  19970. ' IUnknown = interface',
  19971. ' function _AddRef: longint;',
  19972. ' function _Release: longint;',
  19973. ' end;',
  19974. ' TObject = class(IUnknown)',
  19975. ' function _AddRef: longint; virtual; abstract;',
  19976. ' function _Release: longint; virtual; abstract;',
  19977. ' function GetIntf: IUnknown; virtual;',
  19978. ' end;',
  19979. ' TMouse = class',
  19980. ' function GetIntf: IUnknown; override;',
  19981. ' end;',
  19982. 'function TObject.GetIntf: IUnknown; begin end;',
  19983. 'function TMouse.GetIntf: IUnknown;',
  19984. 'var i: IUnknown;',
  19985. 'begin',
  19986. ' inherited;',
  19987. ' inherited GetIntf;',
  19988. ' inherited GetIntf();',
  19989. ' Result:=inherited GetIntf;',
  19990. ' Result:=inherited GetIntf();',
  19991. ' i:=inherited GetIntf;',
  19992. ' i:=inherited GetIntf();',
  19993. 'end;',
  19994. 'begin',
  19995. '']);
  19996. ConvertProgram;
  19997. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19998. LinesToStr([ // statements
  19999. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20000. 'rtl.createClass(this, "TObject", null, function () {',
  20001. ' this.$init = function () {',
  20002. ' };',
  20003. ' this.$final = function () {',
  20004. ' };',
  20005. ' this.GetIntf = function () {',
  20006. ' var Result = null;',
  20007. ' return Result;',
  20008. ' };',
  20009. ' rtl.addIntf(this, $mod.IUnknown);',
  20010. '});',
  20011. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  20012. ' this.GetIntf = function () {',
  20013. ' var Result = null;',
  20014. ' var i = null;',
  20015. ' var $ir = rtl.createIntfRefs();',
  20016. ' var $ok = false;',
  20017. ' try {',
  20018. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  20019. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  20020. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  20021. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20022. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20023. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20024. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20025. ' $ok = true;',
  20026. ' } finally {',
  20027. ' $ir.free();',
  20028. ' rtl._Release(i);',
  20029. ' if (!$ok) rtl._Release(Result);',
  20030. ' };',
  20031. ' return Result;',
  20032. ' };',
  20033. ' rtl.addIntf(this, $mod.IUnknown);',
  20034. '});',
  20035. '']),
  20036. LinesToStr([ // $mod.$main
  20037. '']));
  20038. end;
  20039. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  20040. begin
  20041. StartProgram(false);
  20042. Add([
  20043. '{$interfaces com}',
  20044. 'type',
  20045. ' IUnknown = interface',
  20046. ' function _AddRef: longint;',
  20047. ' function _Release: longint;',
  20048. ' end;',
  20049. ' TObject = class(IUnknown)',
  20050. ' function _AddRef: longint; virtual; abstract;',
  20051. ' function _Release: longint; virtual; abstract;',
  20052. ' end;',
  20053. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  20054. 'begin',
  20055. ' if i is IUnknown then ;',
  20056. ' if o is IUnknown then ;',
  20057. ' if i is TObject then ;',
  20058. ' i:=j as IUnknown;',
  20059. ' i:=o as IUnknown;',
  20060. ' o:=j as TObject;',
  20061. ' i:=IUnknown(j);',
  20062. ' i:=IUnknown(o);',
  20063. ' o:=TObject(i);',
  20064. 'end;',
  20065. 'begin',
  20066. '']);
  20067. ConvertProgram;
  20068. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  20069. LinesToStr([ // statements
  20070. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20071. 'rtl.createClass(this, "TObject", null, function () {',
  20072. ' this.$init = function () {',
  20073. ' };',
  20074. ' this.$final = function () {',
  20075. ' };',
  20076. ' rtl.addIntf(this, $mod.IUnknown);',
  20077. '});',
  20078. 'this.DoDefault = function (i, j, o) {',
  20079. ' rtl._AddRef(i);',
  20080. ' try {',
  20081. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  20082. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  20083. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  20084. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  20085. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20086. ' o = rtl.intfAsClass(j, $mod.TObject);',
  20087. ' i = rtl.setIntfL(i, j);',
  20088. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20089. ' o = rtl.intfToClass(i, $mod.TObject);',
  20090. ' } finally {',
  20091. ' rtl._Release(i);',
  20092. ' };',
  20093. '};',
  20094. '']),
  20095. LinesToStr([ // $mod.$main
  20096. '']));
  20097. end;
  20098. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  20099. begin
  20100. StartProgram(false);
  20101. Add([
  20102. '{$interfaces com}',
  20103. 'type',
  20104. ' IUnknown = interface',
  20105. ' function _AddRef: longint;',
  20106. ' function _Release: longint;',
  20107. ' end;',
  20108. ' TObject = class(IUnknown)',
  20109. ' function _AddRef: longint; virtual; abstract;',
  20110. ' function _Release: longint; virtual; abstract;',
  20111. ' end;',
  20112. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  20113. 'var o: TObject;',
  20114. 'begin',
  20115. ' DoIt(v,v,v,v);',
  20116. ' DoIt(o,o,k,k);',
  20117. 'end;',
  20118. 'procedure DoSome;',
  20119. 'var v: IUnknown;',
  20120. 'begin',
  20121. ' DoIt(v,v,v,v);',
  20122. 'end;',
  20123. 'var i: IUnknown;',
  20124. 'begin',
  20125. ' DoIt(i,i,i,i);',
  20126. '']);
  20127. ConvertProgram;
  20128. CheckSource('TestClassInterface_COM_PassAsArg',
  20129. LinesToStr([ // statements
  20130. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20131. 'rtl.createClass(this, "TObject", null, function () {',
  20132. ' this.$init = function () {',
  20133. ' };',
  20134. ' this.$final = function () {',
  20135. ' };',
  20136. ' rtl.addIntf(this, $mod.IUnknown);',
  20137. '});',
  20138. 'this.DoIt = function (v, j, k, l) {',
  20139. ' var o = null;',
  20140. ' var $ir = rtl.createIntfRefs();',
  20141. ' rtl._AddRef(v);',
  20142. ' try {',
  20143. ' $mod.DoIt(v, v, {',
  20144. ' get: function () {',
  20145. ' return v;',
  20146. ' },',
  20147. ' set: function (w) {',
  20148. ' v = rtl.setIntfL(v, w);',
  20149. ' }',
  20150. ' }, {',
  20151. ' get: function () {',
  20152. ' return v;',
  20153. ' },',
  20154. ' set: function (w) {',
  20155. ' v = rtl.setIntfL(v, w);',
  20156. ' }',
  20157. ' });',
  20158. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  20159. ' } finally {',
  20160. ' $ir.free();',
  20161. ' rtl._Release(v);',
  20162. ' };',
  20163. '};',
  20164. 'this.DoSome = function () {',
  20165. ' var v = null;',
  20166. ' try {',
  20167. ' $mod.DoIt(v, v, {',
  20168. ' get: function () {',
  20169. ' return v;',
  20170. ' },',
  20171. ' set: function (w) {',
  20172. ' v = rtl.setIntfL(v, w);',
  20173. ' }',
  20174. ' }, {',
  20175. ' get: function () {',
  20176. ' return v;',
  20177. ' },',
  20178. ' set: function (w) {',
  20179. ' v = rtl.setIntfL(v, w);',
  20180. ' }',
  20181. ' });',
  20182. ' } finally {',
  20183. ' rtl._Release(v);',
  20184. ' };',
  20185. '};',
  20186. 'this.i = null;',
  20187. '']),
  20188. LinesToStr([ // $mod.$main
  20189. '$mod.DoIt($mod.i, $mod.i, {',
  20190. ' p: $mod,',
  20191. ' get: function () {',
  20192. ' return this.p.i;',
  20193. ' },',
  20194. ' set: function (v) {',
  20195. ' rtl.setIntfP(this.p, "i", v);',
  20196. ' }',
  20197. '}, {',
  20198. ' p: $mod,',
  20199. ' get: function () {',
  20200. ' return this.p.i;',
  20201. ' },',
  20202. ' set: function (v) {',
  20203. ' rtl.setIntfP(this.p, "i", v);',
  20204. ' }',
  20205. '});',
  20206. '']));
  20207. end;
  20208. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  20209. begin
  20210. StartProgram(false);
  20211. Add([
  20212. '{$interfaces com}',
  20213. 'type',
  20214. ' IUnknown = interface',
  20215. ' function _AddRef: longint;',
  20216. ' function _Release: longint;',
  20217. ' end;',
  20218. ' TObject = class(IUnknown)',
  20219. ' function _AddRef: longint; virtual; abstract;',
  20220. ' function _Release: longint; virtual; abstract;',
  20221. ' end;',
  20222. 'procedure DoIt(out i);',
  20223. 'begin end;',
  20224. 'procedure DoSome;',
  20225. 'var v: IUnknown;',
  20226. 'begin',
  20227. ' DoIt(v);',
  20228. 'end;',
  20229. 'function GetIt: IUnknown;',
  20230. 'begin',
  20231. ' DoIt(Result);',
  20232. 'end;',
  20233. 'var i: IUnknown;',
  20234. 'begin',
  20235. ' DoIt(i);',
  20236. '']);
  20237. ConvertProgram;
  20238. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  20239. LinesToStr([ // statements
  20240. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20241. 'rtl.createClass(this, "TObject", null, function () {',
  20242. ' this.$init = function () {',
  20243. ' };',
  20244. ' this.$final = function () {',
  20245. ' };',
  20246. ' rtl.addIntf(this, $mod.IUnknown);',
  20247. '});',
  20248. 'this.DoIt = function (i) {',
  20249. '};',
  20250. 'this.DoSome = function () {',
  20251. ' var v = null;',
  20252. ' try {',
  20253. ' $mod.DoIt({',
  20254. ' get: function () {',
  20255. ' return v;',
  20256. ' },',
  20257. ' set: function (w) {',
  20258. ' v = w;',
  20259. ' }',
  20260. ' });',
  20261. ' } finally {',
  20262. ' rtl._Release(v);',
  20263. ' };',
  20264. '};',
  20265. 'this.GetIt = function () {',
  20266. ' var Result = null;',
  20267. ' var $ok = false;',
  20268. ' try {',
  20269. ' $mod.DoIt({',
  20270. ' get: function () {',
  20271. ' return Result;',
  20272. ' },',
  20273. ' set: function (v) {',
  20274. ' Result = v;',
  20275. ' }',
  20276. ' });',
  20277. ' $ok = true;',
  20278. ' } finally {',
  20279. ' if (!$ok) rtl._Release(Result);',
  20280. ' };',
  20281. ' return Result;',
  20282. '};',
  20283. 'this.i = null;',
  20284. '']),
  20285. LinesToStr([ // $mod.$main
  20286. 'try {',
  20287. ' $mod.DoIt({',
  20288. ' p: $mod,',
  20289. ' get: function () {',
  20290. ' return this.p.i;',
  20291. ' },',
  20292. ' set: function (v) {',
  20293. ' this.p.i = v;',
  20294. ' }',
  20295. ' });',
  20296. '} finally {',
  20297. ' rtl._Release($mod.i);',
  20298. '};',
  20299. '']));
  20300. end;
  20301. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  20302. begin
  20303. StartProgram(false);
  20304. Add([
  20305. '{$interfaces com}',
  20306. 'type',
  20307. ' IUnknown = interface',
  20308. ' function _AddRef: longint;',
  20309. ' function _Release: longint;',
  20310. ' end;',
  20311. ' TObject = class(IUnknown)',
  20312. ' function _AddRef: longint; virtual; abstract;',
  20313. ' function _Release: longint; virtual; abstract;',
  20314. ' end;',
  20315. 'function GetIt: IUnknown;',
  20316. 'begin',
  20317. 'end;',
  20318. 'procedure DoSome;',
  20319. 'var v: IUnknown;',
  20320. ' i: longint;',
  20321. 'begin',
  20322. ' v:=GetIt;',
  20323. ' v:=GetIt();',
  20324. ' GetIt()._AddRef;',
  20325. ' i:=GetIt()._AddRef;',
  20326. 'end;',
  20327. 'var v: IUnknown;',
  20328. ' i: longint;',
  20329. 'begin',
  20330. ' v:=GetIt;',
  20331. ' v:=GetIt();',
  20332. ' GetIt()._AddRef;',
  20333. ' i:=GetIt()._AddRef;',
  20334. '']);
  20335. ConvertProgram;
  20336. CheckSource('TestClassInterface_COM_FunctionInExpr',
  20337. LinesToStr([ // statements
  20338. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20339. 'rtl.createClass(this, "TObject", null, function () {',
  20340. ' this.$init = function () {',
  20341. ' };',
  20342. ' this.$final = function () {',
  20343. ' };',
  20344. ' rtl.addIntf(this, $mod.IUnknown);',
  20345. '});',
  20346. 'this.GetIt = function () {',
  20347. ' var Result = null;',
  20348. ' return Result;',
  20349. '};',
  20350. 'this.DoSome = function () {',
  20351. ' var v = null;',
  20352. ' var i = 0;',
  20353. ' var $ir = rtl.createIntfRefs();',
  20354. ' try {',
  20355. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  20356. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  20357. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20358. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20359. ' } finally {',
  20360. ' $ir.free();',
  20361. ' rtl._Release(v);',
  20362. ' };',
  20363. '};',
  20364. 'this.v = null;',
  20365. 'this.i = 0;',
  20366. '']),
  20367. LinesToStr([ // $mod.$main
  20368. 'var $ir = rtl.createIntfRefs();',
  20369. 'try {',
  20370. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20371. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20372. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20373. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20374. '} finally {',
  20375. ' $ir.free();',
  20376. '};',
  20377. '']));
  20378. end;
  20379. procedure TTestModule.TestClassInterface_COM_Property;
  20380. begin
  20381. StartProgram(false);
  20382. Add([
  20383. '{$interfaces com}',
  20384. 'type',
  20385. ' IUnknown = interface',
  20386. ' function _AddRef: longint;',
  20387. ' function _Release: longint;',
  20388. ' end;',
  20389. ' TObject = class(IUnknown)',
  20390. ' FAnt: IUnknown;',
  20391. ' function _AddRef: longint; virtual; abstract;',
  20392. ' function _Release: longint; virtual; abstract;',
  20393. ' function GetBird: IUnknown; virtual; abstract;',
  20394. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20395. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20396. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20397. ' property Ant: IUnknown read FAnt write FAnt;',
  20398. ' property Bird: IUnknown read GetBird write SetBird;',
  20399. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20400. ' end;',
  20401. 'procedure DoIt;',
  20402. 'var',
  20403. ' o: TObject;',
  20404. ' v: IUnknown;',
  20405. 'begin',
  20406. ' v:=o.Ant;',
  20407. ' o.Ant:=v;',
  20408. ' o.Ant:=o.Ant;',
  20409. ' v:=o.Bird;',
  20410. ' o.Bird:=v;',
  20411. ' o.Bird:=o.Bird;',
  20412. ' v:=o.Items[1];',
  20413. ' o.Items[2]:=v;',
  20414. ' o.Items[3]:=o.Items[4];',
  20415. ' v:=o[5];',
  20416. ' o[6]:=v;',
  20417. ' o[7]:=o[8];',
  20418. 'end;',
  20419. 'begin',
  20420. '']);
  20421. ConvertProgram;
  20422. CheckSource('TestClassInterface_COM_Property',
  20423. LinesToStr([ // statements
  20424. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20425. 'rtl.createClass(this, "TObject", null, function () {',
  20426. ' this.$init = function () {',
  20427. ' this.FAnt = null;',
  20428. ' };',
  20429. ' this.$final = function () {',
  20430. ' rtl.setIntfP(this, "FAnt", null);',
  20431. ' };',
  20432. ' rtl.addIntf(this, $mod.IUnknown);',
  20433. '});',
  20434. 'this.DoIt = function () {',
  20435. ' var o = null;',
  20436. ' var v = null;',
  20437. ' var $ir = rtl.createIntfRefs();',
  20438. ' try {',
  20439. ' v = rtl.setIntfL(v, o.FAnt);',
  20440. ' rtl.setIntfP(o, "FAnt", v);',
  20441. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20442. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20443. ' o.SetBird(v);',
  20444. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20445. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20446. ' o.SetItems(2, v);',
  20447. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20448. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20449. ' o.SetItems(6, v);',
  20450. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20451. ' } finally {',
  20452. ' $ir.free();',
  20453. ' rtl._Release(v);',
  20454. ' };',
  20455. '};',
  20456. '']),
  20457. LinesToStr([ // $mod.$main
  20458. '']));
  20459. end;
  20460. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20461. begin
  20462. StartProgram(false);
  20463. Add([
  20464. '{$interfaces com}',
  20465. 'type',
  20466. ' IUnknown = interface',
  20467. ' function _AddRef: longint;',
  20468. ' function _Release: longint;',
  20469. ' function GetBird: IUnknown;',
  20470. ' procedure SetBird(Value: IUnknown);',
  20471. ' function GetItems(Index: longint): IUnknown;',
  20472. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20473. ' property Bird: IUnknown read GetBird write SetBird;',
  20474. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20475. ' end;',
  20476. ' TObject = class(IUnknown)',
  20477. ' function _AddRef: longint; virtual; abstract;',
  20478. ' function _Release: longint; virtual; abstract;',
  20479. ' function GetBird: IUnknown; virtual; abstract;',
  20480. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20481. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20482. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20483. ' end;',
  20484. 'procedure DoIt;',
  20485. 'var',
  20486. ' o: TObject;',
  20487. ' v: IUnknown;',
  20488. 'begin',
  20489. ' v:=v.Items[1];',
  20490. ' v.Items[2]:=v;',
  20491. ' v.Items[3]:=v.Items[4];',
  20492. ' v:=v[5];',
  20493. ' v[6]:=v;',
  20494. ' v[7]:=v[8];',
  20495. ' v[9].Bird.Bird:=v;',
  20496. ' v:=v.Bird[10].Bird',
  20497. 'end;',
  20498. 'begin',
  20499. '']);
  20500. ConvertProgram;
  20501. CheckSource('TestClassInterface_COM_IntfProperty',
  20502. LinesToStr([ // statements
  20503. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20504. ' "_AddRef",',
  20505. ' "_Release",',
  20506. ' "GetBird",',
  20507. ' "SetBird",',
  20508. ' "GetItems",',
  20509. ' "SetItems"',
  20510. '], null);',
  20511. 'rtl.createClass(this, "TObject", null, function () {',
  20512. ' this.$init = function () {',
  20513. ' };',
  20514. ' this.$final = function () {',
  20515. ' };',
  20516. ' rtl.addIntf(this, $mod.IUnknown);',
  20517. '});',
  20518. 'this.DoIt = function () {',
  20519. ' var o = null;',
  20520. ' var v = null;',
  20521. ' var $ir = rtl.createIntfRefs();',
  20522. ' try {',
  20523. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20524. ' v.SetItems(2, v);',
  20525. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20526. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20527. ' v.SetItems(6, v);',
  20528. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20529. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20530. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20531. ' } finally {',
  20532. ' $ir.free();',
  20533. ' rtl._Release(v);',
  20534. ' };',
  20535. '};',
  20536. '']),
  20537. LinesToStr([ // $mod.$main
  20538. '']));
  20539. end;
  20540. procedure TTestModule.TestClassInterface_COM_Delegation;
  20541. begin
  20542. StartProgram(false);
  20543. Add([
  20544. '{$interfaces com}',
  20545. 'type',
  20546. ' IUnknown = interface',
  20547. ' function _AddRef: longint;',
  20548. ' function _Release: longint;',
  20549. ' end;',
  20550. ' IBird = interface(IUnknown)',
  20551. ' procedure Fly(s: string);',
  20552. ' end;',
  20553. ' IEagle = interface(IBird) end;',
  20554. ' IDove = interface(IBird) end;',
  20555. ' ISwallow = interface(IBird) end;',
  20556. ' TObject = class',
  20557. ' end;',
  20558. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20559. ' function _AddRef: longint; virtual; abstract;',
  20560. ' function _Release: longint; virtual; abstract;',
  20561. ' procedure Fly(s: string); virtual; abstract;',
  20562. ' end;',
  20563. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20564. ' function _AddRef: longint; virtual; abstract;',
  20565. ' function _Release: longint; virtual; abstract;',
  20566. ' FBirdIntf: IBird;',
  20567. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20568. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20569. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20570. ' FDoveObj: TBird;',
  20571. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20572. ' function GetSwallowObj: TBird; virtual; abstract;',
  20573. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20574. ' end;',
  20575. 'begin',
  20576. '']);
  20577. ConvertProgram;
  20578. CheckSource('TestClassInterface_COM_Delegation',
  20579. LinesToStr([ // statements
  20580. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20581. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20582. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20583. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20584. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20585. 'rtl.createClass(this, "TObject", null, function () {',
  20586. ' this.$init = function () {',
  20587. ' };',
  20588. ' this.$final = function () {',
  20589. ' };',
  20590. '});',
  20591. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20592. ' rtl.addIntf(this, $mod.IBird);',
  20593. ' rtl.addIntf(this, $mod.IEagle);',
  20594. ' rtl.addIntf(this, $mod.IDove);',
  20595. ' rtl.addIntf(this, $mod.ISwallow);',
  20596. '});',
  20597. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20598. ' this.$init = function () {',
  20599. ' $mod.TObject.$init.call(this);',
  20600. ' this.FBirdIntf = null;',
  20601. ' this.FDoveObj = null;',
  20602. ' };',
  20603. ' this.$final = function () {',
  20604. ' rtl.setIntfP(this, "FBirdIntf", null);',
  20605. ' this.FDoveObj = undefined;',
  20606. ' $mod.TObject.$final.call(this);',
  20607. ' };',
  20608. ' this.$intfmaps = {',
  20609. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20610. ' return rtl._AddRef(this.FBirdIntf);',
  20611. ' },',
  20612. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20613. ' return this.GetEagleIntf();',
  20614. ' },',
  20615. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20616. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20617. ' },',
  20618. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20619. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20620. ' }',
  20621. ' };',
  20622. '});',
  20623. '']),
  20624. LinesToStr([ // $mod.$main
  20625. '']));
  20626. end;
  20627. procedure TTestModule.TestClassInterface_COM_With;
  20628. begin
  20629. StartProgram(false);
  20630. Add([
  20631. '{$interfaces com}',
  20632. 'type',
  20633. ' IUnknown = interface',
  20634. ' function _AddRef: longint;',
  20635. ' function _Release: longint;',
  20636. ' function GetAnt: IUnknown;',
  20637. ' property Ant: IUnknown read GetAnt;',
  20638. ' end;',
  20639. ' TObject = class(IUnknown)',
  20640. ' function _AddRef: longint; virtual; abstract;',
  20641. ' function _Release: longint; virtual; abstract;',
  20642. ' function GetAnt: IUnknown; virtual; abstract;',
  20643. ' property Ant: IUnknown read GetAnt;',
  20644. ' end;',
  20645. 'procedure DoIt;',
  20646. 'var',
  20647. ' i: IUnknown;',
  20648. 'begin',
  20649. ' with i do ',
  20650. ' GetAnt;',
  20651. ' with i.Ant, Ant do ',
  20652. ' GetAnt;',
  20653. 'end;',
  20654. 'begin',
  20655. '']);
  20656. ConvertProgram;
  20657. CheckSource('TestClassInterface_COM_With',
  20658. LinesToStr([ // statements
  20659. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20660. 'rtl.createClass(this, "TObject", null, function () {',
  20661. ' this.$init = function () {',
  20662. ' };',
  20663. ' this.$final = function () {',
  20664. ' };',
  20665. ' rtl.addIntf(this, $mod.IUnknown);',
  20666. '});',
  20667. 'this.DoIt = function () {',
  20668. ' var i = null;',
  20669. ' var $ir = rtl.createIntfRefs();',
  20670. ' try {',
  20671. ' $ir.ref(1, i.GetAnt());',
  20672. ' var $with = $ir.ref(2, i.GetAnt());',
  20673. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20674. ' $ir.ref(4, $with1.GetAnt());',
  20675. ' } finally {',
  20676. ' $ir.free();',
  20677. ' };',
  20678. '};',
  20679. '']),
  20680. LinesToStr([ // $mod.$main
  20681. '']));
  20682. end;
  20683. procedure TTestModule.TestClassInterface_COM_ForIn;
  20684. begin
  20685. StartProgram(false);
  20686. Add([
  20687. '{$interfaces com}',
  20688. 'type',
  20689. ' IUnknown = interface end;',
  20690. ' TObject = class',
  20691. ' Id: longint;',
  20692. ' end;',
  20693. ' IEnumerator = interface(IUnknown)',
  20694. ' function GetCurrent: TObject;',
  20695. ' function MoveNext: Boolean;',
  20696. ' property Current: TObject read GetCurrent;',
  20697. ' end;',
  20698. ' IEnumerable = interface(IUnknown)',
  20699. ' function GetEnumerator: IEnumerator;',
  20700. ' end;',
  20701. 'var',
  20702. ' o: TObject;',
  20703. ' i: IEnumerable;',
  20704. 'begin',
  20705. ' for o in i do o.Id:=3;',
  20706. '']);
  20707. ConvertProgram;
  20708. CheckSource('TestClassInterface_COM_ForIn',
  20709. LinesToStr([ // statements
  20710. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20711. 'rtl.createClass(this, "TObject", null, function () {',
  20712. ' this.$init = function () {',
  20713. ' this.Id = 0;',
  20714. ' };',
  20715. ' this.$final = function () {',
  20716. ' };',
  20717. '});',
  20718. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20719. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20720. 'this.o = null;',
  20721. 'this.i = null;',
  20722. '']),
  20723. LinesToStr([ // $mod.$main
  20724. 'var $in = $mod.i.GetEnumerator();',
  20725. 'try {',
  20726. ' while ($in.MoveNext()) {',
  20727. ' $mod.o = $in.GetCurrent();',
  20728. ' $mod.o.Id = 3;',
  20729. ' }',
  20730. '} finally {',
  20731. ' rtl._Release($in)',
  20732. '};',
  20733. '']));
  20734. end;
  20735. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20736. begin
  20737. StartProgram(false);
  20738. Add([
  20739. '{$interfaces com}',
  20740. 'type',
  20741. ' IUnknown = interface',
  20742. ' function _AddRef: longint;',
  20743. ' function _Release: longint;',
  20744. ' end;',
  20745. ' TObject = class',
  20746. ' end;',
  20747. ' TArrOfIntf = array of IUnknown;',
  20748. 'begin',
  20749. '']);
  20750. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20751. ConvertProgram;
  20752. end;
  20753. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20754. begin
  20755. StartProgram(false);
  20756. Add([
  20757. '{$interfaces com}',
  20758. 'type',
  20759. ' IUnknown = interface',
  20760. ' function _AddRef: longint;',
  20761. ' function _Release: longint;',
  20762. ' end;',
  20763. ' TRec = record',
  20764. ' i: IUnknown;',
  20765. ' end;',
  20766. 'begin',
  20767. '']);
  20768. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20769. ConvertProgram;
  20770. end;
  20771. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20772. begin
  20773. StartUnit(false);
  20774. Add([
  20775. '{$interfaces com}',
  20776. 'interface',
  20777. 'implementation',
  20778. 'type',
  20779. ' IUnknown = interface',
  20780. ' function _AddRef: longint;',
  20781. ' end;',
  20782. ' TObject = class(IUnknown)',
  20783. ' function _AddRef: longint;',
  20784. ' end;',
  20785. 'function TObject._AddRef: longint; begin end;',
  20786. 'var i: IUnknown;',
  20787. ' o: TObject;',
  20788. 'initialization',
  20789. ' i:=nil;',
  20790. ' i:=i;',
  20791. ' i:=o;',
  20792. ' if (o as IUnknown)=nil then ;',
  20793. '']);
  20794. ConvertUnit;
  20795. CheckSource('TestClassInterface_COM_UnitInitialization',
  20796. LinesToStr([ // statements
  20797. 'var $impl = $mod.$impl;',
  20798. '']),
  20799. LinesToStr([ // this.$init
  20800. 'var $ir = rtl.createIntfRefs();',
  20801. 'try {',
  20802. ' rtl.setIntfP($impl, "i", null);',
  20803. ' rtl.setIntfP($impl, "i", $impl.i);',
  20804. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20805. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20806. '} finally {',
  20807. ' $ir.free();',
  20808. '};',
  20809. '']),
  20810. LinesToStr([ // implementation
  20811. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20812. 'rtl.createClass($impl, "TObject", null, function () {',
  20813. ' this.$init = function () {',
  20814. ' };',
  20815. ' this.$final = function () {',
  20816. ' };',
  20817. ' this._AddRef = function () {',
  20818. ' var Result = 0;',
  20819. ' return Result;',
  20820. ' };',
  20821. ' rtl.addIntf(this, $impl.IUnknown);',
  20822. '});',
  20823. '$impl.i = null;',
  20824. '$impl.o = null;',
  20825. ''])
  20826. );
  20827. end;
  20828. procedure TTestModule.TestClassInterface_GUID;
  20829. begin
  20830. StartProgram(false);
  20831. Add([
  20832. '{$interfaces corba}',
  20833. 'type',
  20834. ' IUnknown = interface',
  20835. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20836. ' end;',
  20837. ' TObject = class end;',
  20838. ' TGUID = record D1, D2, D3, D4: word; end;',
  20839. ' TAliasGUID = TGUID;',
  20840. ' TGUIDString = type string;',
  20841. ' TAliasGUIDString = TGUIDString;',
  20842. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20843. 'begin end;',
  20844. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20845. 'begin end;',
  20846. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20847. 'begin end;',
  20848. 'var',
  20849. ' i: IUnknown;',
  20850. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20851. ' s: TAliasGUIDString;',
  20852. 'begin',
  20853. ' DoConstGUIDIt(IUnknown);',
  20854. ' DoDefGUID(IUnknown);',
  20855. ' DoStr(IUnknown);',
  20856. ' DoConstGUIDIt(i);',
  20857. ' DoDefGUID(i);',
  20858. ' DoStr(i);',
  20859. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20860. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20861. ' DoStr(g);',
  20862. ' g:=i;',
  20863. ' g:=IUnknown;',
  20864. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20865. ' s:=i;',
  20866. ' s:=IUnknown;',
  20867. ' s:=g;',
  20868. ' if g=i then ;',
  20869. ' if i=g then ;',
  20870. ' if g=IUnknown then ;',
  20871. ' if IUnknown=g then ;',
  20872. ' if s=i then ;',
  20873. ' if i=s then ;',
  20874. ' if s=IUnknown then ;',
  20875. ' if IUnknown=s then ;',
  20876. ' if s=g then ;',
  20877. ' if g=s then ;',
  20878. '']);
  20879. ConvertProgram;
  20880. CheckSource('TestClassInterface_GUID',
  20881. LinesToStr([ // statements
  20882. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20883. 'rtl.createClass(this, "TObject", null, function () {',
  20884. ' this.$init = function () {',
  20885. ' };',
  20886. ' this.$final = function () {',
  20887. ' };',
  20888. '});',
  20889. 'rtl.recNewT(this, "TGUID", function () {',
  20890. ' this.D1 = 0;',
  20891. ' this.D2 = 0;',
  20892. ' this.D3 = 0;',
  20893. ' this.D4 = 0;',
  20894. ' this.$eq = function (b) {',
  20895. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20896. ' };',
  20897. ' this.$assign = function (s) {',
  20898. ' this.D1 = s.D1;',
  20899. ' this.D2 = s.D2;',
  20900. ' this.D3 = s.D3;',
  20901. ' this.D4 = s.D4;',
  20902. ' return this;',
  20903. ' };',
  20904. '});',
  20905. 'this.DoConstGUIDIt = function (g) {',
  20906. '};',
  20907. 'this.DoDefGUID = function (g) {',
  20908. '};',
  20909. 'this.DoStr = function (s) {',
  20910. '};',
  20911. 'this.i = null;',
  20912. 'this.g = this.TGUID.$clone({',
  20913. ' D1: 0xD91C9AF4,',
  20914. ' D2: 0x3C93,',
  20915. ' D3: 0x420F,',
  20916. ' D4: [',
  20917. ' 0xA3,',
  20918. ' 0x03,',
  20919. ' 0xBF,',
  20920. ' 0x5B,',
  20921. ' 0xA8,',
  20922. ' 0x2B,',
  20923. ' 0xFD,',
  20924. ' 0x23',
  20925. ' ]',
  20926. '});',
  20927. 'this.s = "";',
  20928. '']),
  20929. LinesToStr([ // $mod.$main
  20930. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20931. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20932. '$mod.DoStr($mod.IUnknown.$guid);',
  20933. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20934. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20935. '$mod.DoStr($mod.i.$guid);',
  20936. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20937. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20938. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20939. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20940. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20941. '$mod.g.$assign({',
  20942. ' D1: 0xD91C9AF4,',
  20943. ' D2: 0x3C93,',
  20944. ' D3: 0x420F,',
  20945. ' D4: [',
  20946. ' 0xA3,',
  20947. ' 0x03,',
  20948. ' 0xBF,',
  20949. ' 0x5B,',
  20950. ' 0xA8,',
  20951. ' 0x2B,',
  20952. ' 0xFD,',
  20953. ' 0x23',
  20954. ' ]',
  20955. '});',
  20956. '$mod.s = $mod.i.$guid;',
  20957. '$mod.s = $mod.IUnknown.$guid;',
  20958. '$mod.s = rtl.guidrToStr($mod.g);',
  20959. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20960. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20961. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20962. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20963. 'if ($mod.s === $mod.i.$guid) ;',
  20964. 'if ($mod.i.$guid === $mod.s) ;',
  20965. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20966. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20967. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20968. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20969. '']));
  20970. end;
  20971. procedure TTestModule.TestClassInterface_GUIDProperty;
  20972. begin
  20973. StartProgram(false);
  20974. Add([
  20975. '{$interfaces corba}',
  20976. 'type',
  20977. ' IUnknown = interface',
  20978. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20979. ' end;',
  20980. ' TGUID = record D1, D2, D3, D4: word; end;',
  20981. ' TAliasGUID = TGUID;',
  20982. ' TGUIDString = type string;',
  20983. ' TAliasGUIDString = TGUIDString;',
  20984. ' TObject = class',
  20985. ' function GetG: TAliasGUID; virtual; abstract;',
  20986. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20987. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20988. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20989. ' property g: TAliasGUID read GetG write SetG;',
  20990. ' property s: TAliasGUIDString read GetS write SetS;',
  20991. ' end;',
  20992. 'var o: TObject;',
  20993. 'begin',
  20994. ' o.g:=IUnknown;',
  20995. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20996. ' o.s:=IUnknown;',
  20997. ' o.s:=o.g;',
  20998. '']);
  20999. ConvertProgram;
  21000. CheckSource('TestClassInterface_GUIDProperty',
  21001. LinesToStr([ // statements
  21002. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  21003. 'rtl.recNewT(this, "TGUID", function () {',
  21004. ' this.D1 = 0;',
  21005. ' this.D2 = 0;',
  21006. ' this.D3 = 0;',
  21007. ' this.D4 = 0;',
  21008. ' this.$eq = function (b) {',
  21009. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  21010. ' };',
  21011. ' this.$assign = function (s) {',
  21012. ' this.D1 = s.D1;',
  21013. ' this.D2 = s.D2;',
  21014. ' this.D3 = s.D3;',
  21015. ' this.D4 = s.D4;',
  21016. ' return this;',
  21017. ' };',
  21018. '});',
  21019. 'rtl.createClass(this, "TObject", null, function () {',
  21020. ' this.$init = function () {',
  21021. ' };',
  21022. ' this.$final = function () {',
  21023. ' };',
  21024. '});',
  21025. 'this.o = null;',
  21026. '']),
  21027. LinesToStr([ // $mod.$main
  21028. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  21029. '$mod.o.SetG({',
  21030. ' D1: 0xD91C9AF4,',
  21031. ' D2: 0x3C93,',
  21032. ' D3: 0x420F,',
  21033. ' D4: [',
  21034. ' 0xA3,',
  21035. ' 0x03,',
  21036. ' 0xBF,',
  21037. ' 0x5B,',
  21038. ' 0xA8,',
  21039. ' 0x2B,',
  21040. ' 0xFD,',
  21041. ' 0x23',
  21042. ' ]',
  21043. '});',
  21044. '$mod.o.SetS($mod.IUnknown.$guid);',
  21045. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  21046. '']));
  21047. end;
  21048. procedure TTestModule.TestClassHelper_ClassVar;
  21049. begin
  21050. StartProgram(false);
  21051. Add([
  21052. 'type',
  21053. ' TObject = class',
  21054. ' end;',
  21055. ' THelper = class helper for TObject',
  21056. ' const',
  21057. ' One = 1;',
  21058. ' Two: word = 2;',
  21059. ' class var',
  21060. ' Glob: word;',
  21061. ' function Foo(w: word): word;',
  21062. ' class function Bar(w: word): word;',
  21063. ' end;',
  21064. 'function THelper.foo(w: word): word;',
  21065. 'begin',
  21066. ' Result:=w;',
  21067. ' Two:=One+w;',
  21068. ' Glob:=Glob;',
  21069. ' Result:=Self.Glob;',
  21070. ' Self.Glob:=Self.Glob;',
  21071. ' with Self do Glob:=Glob;',
  21072. 'end;',
  21073. 'class function THelper.bar(w: word): word;',
  21074. 'begin',
  21075. ' Result:=w;',
  21076. ' Two:=One;',
  21077. ' Glob:=Glob;',
  21078. ' Self.Glob:=Self.Glob;',
  21079. ' with Self do Glob:=Glob;',
  21080. 'end;',
  21081. 'var o: TObject;',
  21082. 'begin',
  21083. ' tobject.two:=tobject.one;',
  21084. ' tobject.Glob:=tobject.Glob;',
  21085. ' with tobject do begin',
  21086. ' two:=one;',
  21087. ' Glob:=Glob;',
  21088. ' end;',
  21089. ' o.two:=o.one;',
  21090. ' o.Glob:=o.Glob;',
  21091. ' with o do begin',
  21092. ' two:=one;',
  21093. ' Glob:=Glob;',
  21094. ' end;',
  21095. '']);
  21096. ConvertProgram;
  21097. CheckSource('TestClassHelper_ClassVar',
  21098. LinesToStr([ // statements
  21099. 'rtl.createClass(this, "TObject", null, function () {',
  21100. ' this.$init = function () {',
  21101. ' };',
  21102. ' this.$final = function () {',
  21103. ' };',
  21104. '});',
  21105. 'rtl.createHelper(this, "THelper", null, function () {',
  21106. ' this.One = 1;',
  21107. ' this.Two = 2;',
  21108. ' this.Glob = 0;',
  21109. ' this.Foo = function (w) {',
  21110. ' var Result = 0;',
  21111. ' Result = w;',
  21112. ' $mod.THelper.Two = 1 + w;',
  21113. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21114. ' Result = $mod.THelper.Glob;',
  21115. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21116. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21117. ' return Result;',
  21118. ' };',
  21119. ' this.Bar = function (w) {',
  21120. ' var Result = 0;',
  21121. ' Result = w;',
  21122. ' $mod.THelper.Two = 1;',
  21123. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21124. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21125. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21126. ' return Result;',
  21127. ' };',
  21128. '});',
  21129. 'this.o = null;',
  21130. '']),
  21131. LinesToStr([ // $mod.$main
  21132. '$mod.THelper.Two = 1;',
  21133. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21134. 'var $with = $mod.TObject;',
  21135. '$mod.THelper.Two = 1;',
  21136. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21137. '$mod.THelper.Two = 1;',
  21138. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21139. 'var $with1 = $mod.o;',
  21140. '$mod.THelper.Two = 1;',
  21141. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21142. '']));
  21143. end;
  21144. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  21145. begin
  21146. StartProgram(false);
  21147. Add([
  21148. 'type',
  21149. ' TObject = class',
  21150. ' FSize: word;',
  21151. ' property Size: word read FSize write FSize;',
  21152. ' end;',
  21153. ' THelper = class helper for TObject',
  21154. ' function Foo(w: word = 1): word;',
  21155. ' end;',
  21156. 'function THelper.foo(w: word): word;',
  21157. 'begin',
  21158. ' Result:=Size;',
  21159. ' Size:=Size+2;',
  21160. ' Self.Size:=Self.Size+3;',
  21161. ' FSize:=FSize+4;',
  21162. ' Self.FSize:=Self.FSize+5;',
  21163. ' with Self do begin',
  21164. ' Size:=Size+6;',
  21165. ' FSize:=FSize+7;',
  21166. ' FSize:=FSize+8;',
  21167. ' end;',
  21168. 'end;',
  21169. 'begin',
  21170. '']);
  21171. ConvertProgram;
  21172. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  21173. LinesToStr([ // statements
  21174. 'rtl.createClass(this, "TObject", null, function () {',
  21175. ' this.$init = function () {',
  21176. ' this.FSize = 0;',
  21177. ' };',
  21178. ' this.$final = function () {',
  21179. ' };',
  21180. '});',
  21181. 'rtl.createHelper(this, "THelper", null, function () {',
  21182. ' this.Foo = function (w) {',
  21183. ' var Result = 0;',
  21184. ' Result = this.FSize;',
  21185. ' this.FSize = this.FSize + 2;',
  21186. ' this.FSize = this.FSize + 3;',
  21187. ' this.FSize = this.FSize + 4;',
  21188. ' this.FSize = this.FSize + 5;',
  21189. ' this.FSize = this.FSize + 6;',
  21190. ' this.FSize = this.FSize + 7;',
  21191. ' this.FSize = this.FSize + 8;',
  21192. ' return Result;',
  21193. ' };',
  21194. '});',
  21195. '']),
  21196. LinesToStr([ // $mod.$main
  21197. '']));
  21198. end;
  21199. procedure TTestModule.TestClassHelper_Method_Call;
  21200. begin
  21201. StartProgram(false);
  21202. Add([
  21203. 'type',
  21204. ' TObject = class',
  21205. ' procedure Run(w: word = 10);',
  21206. ' end;',
  21207. ' THelper = class helper for TObject',
  21208. ' function Foo(w: word = 1): word;',
  21209. ' end;',
  21210. 'procedure TObject.Run(w: word);',
  21211. 'var o: TObject;',
  21212. 'begin',
  21213. ' Foo;',
  21214. ' Foo();',
  21215. ' Foo(2);',
  21216. ' Self.Foo;',
  21217. ' Self.Foo();',
  21218. ' Self.Foo(3);',
  21219. ' with Self do begin',
  21220. ' Foo;',
  21221. ' Foo();',
  21222. ' Foo(4);',
  21223. ' end;',
  21224. ' with o do Foo(5);',
  21225. 'end;',
  21226. 'function THelper.foo(w: word): word;',
  21227. 'begin',
  21228. ' Run;',
  21229. ' Run();',
  21230. ' Run(11);',
  21231. ' Foo;',
  21232. ' Foo();',
  21233. ' Foo(12);',
  21234. ' Self.Foo;',
  21235. ' Self.Foo();',
  21236. ' Self.Foo(13);',
  21237. ' with Self do begin',
  21238. ' Foo;',
  21239. ' Foo();',
  21240. ' Foo(14);',
  21241. ' end;',
  21242. 'end;',
  21243. 'var Obj: TObject;',
  21244. 'begin',
  21245. ' obj.Foo;',
  21246. ' obj.Foo();',
  21247. ' obj.Foo(21);',
  21248. ' with obj do begin',
  21249. ' Foo;',
  21250. ' Foo();',
  21251. ' Foo(22);',
  21252. ' end;',
  21253. '']);
  21254. ConvertProgram;
  21255. CheckSource('TestClassHelper_Method_Call',
  21256. LinesToStr([ // statements
  21257. 'rtl.createClass(this, "TObject", null, function () {',
  21258. ' this.$init = function () {',
  21259. ' };',
  21260. ' this.$final = function () {',
  21261. ' };',
  21262. ' this.Run = function (w) {',
  21263. ' var o = null;',
  21264. ' $mod.THelper.Foo.call(this, 1);',
  21265. ' $mod.THelper.Foo.call(this, 1);',
  21266. ' $mod.THelper.Foo.call(this, 2);',
  21267. ' $mod.THelper.Foo.call(this, 1);',
  21268. ' $mod.THelper.Foo.call(this, 1);',
  21269. ' $mod.THelper.Foo.call(this, 3);',
  21270. ' $mod.THelper.Foo.call(this, 1);',
  21271. ' $mod.THelper.Foo.call(this, 1);',
  21272. ' $mod.THelper.Foo.call(this, 4);',
  21273. ' $mod.THelper.Foo.call(o, 5);',
  21274. ' };',
  21275. '});',
  21276. 'rtl.createHelper(this, "THelper", null, function () {',
  21277. ' this.Foo = function (w) {',
  21278. ' var Result = 0;',
  21279. ' this.Run(10);',
  21280. ' this.Run(10);',
  21281. ' this.Run(11);',
  21282. ' $mod.THelper.Foo.call(this, 1);',
  21283. ' $mod.THelper.Foo.call(this, 1);',
  21284. ' $mod.THelper.Foo.call(this, 12);',
  21285. ' $mod.THelper.Foo.call(this, 1);',
  21286. ' $mod.THelper.Foo.call(this, 1);',
  21287. ' $mod.THelper.Foo.call(this, 13);',
  21288. ' $mod.THelper.Foo.call(this, 1);',
  21289. ' $mod.THelper.Foo.call(this, 1);',
  21290. ' $mod.THelper.Foo.call(this, 14);',
  21291. ' return Result;',
  21292. ' };',
  21293. '});',
  21294. 'this.Obj = null;',
  21295. '']),
  21296. LinesToStr([ // $mod.$main
  21297. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21298. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21299. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21300. 'var $with = $mod.Obj;',
  21301. '$mod.THelper.Foo.call($with, 1);',
  21302. '$mod.THelper.Foo.call($with, 1);',
  21303. '$mod.THelper.Foo.call($with, 22);',
  21304. '']));
  21305. end;
  21306. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  21307. begin
  21308. StartProgram(false);
  21309. Add([
  21310. 'type',
  21311. ' TObject = class',
  21312. ' procedure Run(w: word = 10);',
  21313. ' end;',
  21314. ' THelper = class helper for TObject',
  21315. ' function Foo(w: word = 1): word;',
  21316. ' end;',
  21317. 'procedure TObject.Run(w: word);',
  21318. ' procedure Sub(Self: TObject);',
  21319. ' begin',
  21320. ' Foo;',
  21321. ' Foo();',
  21322. ' Self.Foo;',
  21323. ' Self.Foo();',
  21324. ' with Self do begin',
  21325. ' Foo;',
  21326. ' Foo();',
  21327. ' end;',
  21328. ' end;',
  21329. 'begin',
  21330. 'end;',
  21331. 'function THelper.foo(w: word): word;',
  21332. ' procedure Sub(Self: TObject);',
  21333. ' begin',
  21334. ' Run;',
  21335. ' Run();',
  21336. ' Foo;',
  21337. ' Foo();',
  21338. ' Self.Foo;',
  21339. ' Self.Foo();',
  21340. ' with Self do begin',
  21341. ' Foo;',
  21342. ' Foo();',
  21343. ' end;',
  21344. ' end;',
  21345. 'begin',
  21346. 'end;',
  21347. 'begin',
  21348. '']);
  21349. ConvertProgram;
  21350. CheckSource('TestClassHelper_Method_Nested_Call',
  21351. LinesToStr([ // statements
  21352. 'rtl.createClass(this, "TObject", null, function () {',
  21353. ' this.$init = function () {',
  21354. ' };',
  21355. ' this.$final = function () {',
  21356. ' };',
  21357. ' this.Run = function (w) {',
  21358. ' var $Self = this;',
  21359. ' function Sub(Self) {',
  21360. ' $mod.THelper.Foo.call($Self, 1);',
  21361. ' $mod.THelper.Foo.call($Self, 1);',
  21362. ' $mod.THelper.Foo.call(Self, 1);',
  21363. ' $mod.THelper.Foo.call(Self, 1);',
  21364. ' $mod.THelper.Foo.call(Self, 1);',
  21365. ' $mod.THelper.Foo.call(Self, 1);',
  21366. ' };',
  21367. ' };',
  21368. '});',
  21369. 'rtl.createHelper(this, "THelper", null, function () {',
  21370. ' this.Foo = function (w) {',
  21371. ' var $Self = this;',
  21372. ' var Result = 0;',
  21373. ' function Sub(Self) {',
  21374. ' $Self.Run(10);',
  21375. ' $Self.Run(10);',
  21376. ' $mod.THelper.Foo.call($Self, 1);',
  21377. ' $mod.THelper.Foo.call($Self, 1);',
  21378. ' $mod.THelper.Foo.call(Self, 1);',
  21379. ' $mod.THelper.Foo.call(Self, 1);',
  21380. ' $mod.THelper.Foo.call(Self, 1);',
  21381. ' $mod.THelper.Foo.call(Self, 1);',
  21382. ' };',
  21383. ' return Result;',
  21384. ' };',
  21385. '});',
  21386. '']),
  21387. LinesToStr([ // $mod.$main
  21388. '']));
  21389. end;
  21390. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  21391. begin
  21392. StartProgram(false);
  21393. Add([
  21394. 'type',
  21395. ' TObject = class',
  21396. ' class procedure Run(w: word = 10);',
  21397. ' end;',
  21398. ' THelper = class helper for TObject',
  21399. ' class function Foo(w: word = 1): word;',
  21400. ' end;',
  21401. 'class procedure TObject.Run(w: word);',
  21402. 'begin',
  21403. ' Foo;',
  21404. ' Foo();',
  21405. ' Self.Foo;',
  21406. ' Self.Foo();',
  21407. ' with Self do begin',
  21408. ' Foo;',
  21409. ' Foo();',
  21410. ' end;',
  21411. 'end;',
  21412. 'class function THelper.foo(w: word): word;',
  21413. 'begin',
  21414. ' Run;',
  21415. ' Run();',
  21416. ' Foo;',
  21417. ' Foo();',
  21418. ' Self.Foo;',
  21419. ' Self.Foo();',
  21420. ' with Self do begin',
  21421. ' Foo;',
  21422. ' Foo();',
  21423. ' end;',
  21424. 'end;',
  21425. 'var',
  21426. ' Obj: TObject;',
  21427. 'begin',
  21428. ' obj.Foo;',
  21429. ' obj.Foo();',
  21430. ' with obj do begin',
  21431. ' Foo;',
  21432. ' Foo();',
  21433. ' end;',
  21434. ' tobject.Foo;',
  21435. ' tobject.Foo();',
  21436. ' with tobject do begin',
  21437. ' Foo;',
  21438. ' Foo();',
  21439. ' end;',
  21440. '']);
  21441. ConvertProgram;
  21442. CheckSource('TestClassHelper_ClassMethod_Call',
  21443. LinesToStr([ // statements
  21444. 'rtl.createClass(this, "TObject", null, function () {',
  21445. ' this.$init = function () {',
  21446. ' };',
  21447. ' this.$final = function () {',
  21448. ' };',
  21449. ' this.Run = function (w) {',
  21450. ' $mod.THelper.Foo.call(this, 1);',
  21451. ' $mod.THelper.Foo.call(this, 1);',
  21452. ' $mod.THelper.Foo.call(this, 1);',
  21453. ' $mod.THelper.Foo.call(this, 1);',
  21454. ' $mod.THelper.Foo.call(this, 1);',
  21455. ' $mod.THelper.Foo.call(this, 1);',
  21456. ' };',
  21457. '});',
  21458. 'rtl.createHelper(this, "THelper", null, function () {',
  21459. ' this.Foo = function (w) {',
  21460. ' var Result = 0;',
  21461. ' this.Run(10);',
  21462. ' this.Run(10);',
  21463. ' $mod.THelper.Foo.call(this, 1);',
  21464. ' $mod.THelper.Foo.call(this, 1);',
  21465. ' $mod.THelper.Foo.call(this, 1);',
  21466. ' $mod.THelper.Foo.call(this, 1);',
  21467. ' $mod.THelper.Foo.call(this, 1);',
  21468. ' $mod.THelper.Foo.call(this, 1);',
  21469. ' return Result;',
  21470. ' };',
  21471. '});',
  21472. 'this.Obj = null;',
  21473. '']),
  21474. LinesToStr([ // $mod.$main
  21475. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21476. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21477. 'var $with = $mod.Obj;',
  21478. '$mod.THelper.Foo.call($with.$class, 1);',
  21479. '$mod.THelper.Foo.call($with.$class, 1);',
  21480. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21481. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21482. 'var $with1 = $mod.TObject;',
  21483. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21484. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21485. '']));
  21486. end;
  21487. procedure TTestModule.TestClassHelper_ClassOf;
  21488. begin
  21489. StartProgram(false);
  21490. Add([
  21491. 'type',
  21492. ' TObject = class',
  21493. ' end;',
  21494. ' TClass = class of TObject;',
  21495. ' THelper = class helper for TObject',
  21496. ' class function Foo(w: word = 1): word;',
  21497. ' end;',
  21498. 'class function THelper.foo(w: word): word;',
  21499. 'begin',
  21500. 'end;',
  21501. 'var',
  21502. ' c: TClass;',
  21503. 'begin',
  21504. ' c.Foo;',
  21505. ' c.Foo();',
  21506. ' with c do begin',
  21507. ' Foo;',
  21508. ' Foo();',
  21509. ' end;',
  21510. '']);
  21511. ConvertProgram;
  21512. CheckSource('TestClassHelper_ClassOf',
  21513. LinesToStr([ // statements
  21514. 'rtl.createClass(this, "TObject", null, function () {',
  21515. ' this.$init = function () {',
  21516. ' };',
  21517. ' this.$final = function () {',
  21518. ' };',
  21519. '});',
  21520. 'rtl.createHelper(this, "THelper", null, function () {',
  21521. ' this.Foo = function (w) {',
  21522. ' var Result = 0;',
  21523. ' return Result;',
  21524. ' };',
  21525. '});',
  21526. 'this.c = null;',
  21527. '']),
  21528. LinesToStr([ // $mod.$main
  21529. '$mod.THelper.Foo.call($mod.c, 1);',
  21530. '$mod.THelper.Foo.call($mod.c, 1);',
  21531. 'var $with = $mod.c;',
  21532. '$mod.THelper.Foo.call($with, 1);',
  21533. '$mod.THelper.Foo.call($with, 1);',
  21534. '']));
  21535. end;
  21536. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21537. begin
  21538. StartProgram(false);
  21539. Add([
  21540. '{$mode objfpc}',
  21541. 'type',
  21542. ' TObject = class',
  21543. ' procedure DoIt;',
  21544. ' end;',
  21545. ' THelper = class helper for TObject',
  21546. ' procedure Fly(w: word = 1);',
  21547. ' class procedure Glide(w: word = 1);',
  21548. ' class procedure Run(w: word = 1); static;',
  21549. ' end;',
  21550. ' TFly = procedure(w: word) of object;',
  21551. ' TGlide = TFly;',
  21552. ' TRun = procedure(w: word);',
  21553. 'var',
  21554. ' f: TFly;',
  21555. ' g: TGlide;',
  21556. ' r: TRun;',
  21557. 'procedure TObject.DoIt;',
  21558. 'begin',
  21559. ' f:=@fly;',
  21560. ' g:=@glide;',
  21561. ' r:=@run;',
  21562. ' f:[email protected];',
  21563. ' g:[email protected];',
  21564. ' r:[email protected];',
  21565. ' with self do begin',
  21566. ' f:=@fly;',
  21567. ' g:=@glide;',
  21568. ' r:=@run;',
  21569. ' end;',
  21570. 'end;',
  21571. 'procedure THelper.fly(w: word);',
  21572. 'begin',
  21573. ' f:=@fly;',
  21574. ' g:=@glide;',
  21575. ' r:=@run;',
  21576. 'end;',
  21577. 'class procedure THelper.glide(w: word);',
  21578. 'begin',
  21579. ' g:=@glide;',
  21580. ' r:=@run;',
  21581. 'end;',
  21582. 'class procedure THelper.run(w: word);',
  21583. 'begin',
  21584. ' g:=@glide;',
  21585. ' r:=@run;',
  21586. 'end;',
  21587. 'var',
  21588. ' Obj: TObject;',
  21589. 'begin',
  21590. ' f:[email protected];',
  21591. ' g:[email protected];',
  21592. ' r:[email protected];',
  21593. ' with obj do begin',
  21594. ' f:=@fly;',
  21595. ' g:=@glide;',
  21596. ' r:=@run;',
  21597. ' end;',
  21598. ' g:[email protected];',
  21599. ' r:[email protected];',
  21600. ' with tobject do begin',
  21601. ' g:=@glide;',
  21602. ' r:=@run;',
  21603. ' end;',
  21604. '']);
  21605. ConvertProgram;
  21606. CheckSource('TestClassHelper_MethodRefObjFPC',
  21607. LinesToStr([ // statements
  21608. 'rtl.createClass(this, "TObject", null, function () {',
  21609. ' this.$init = function () {',
  21610. ' };',
  21611. ' this.$final = function () {',
  21612. ' };',
  21613. ' this.DoIt = function () {',
  21614. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21615. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21616. ' $mod.r = $mod.THelper.Run;',
  21617. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21618. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21619. ' $mod.r = $mod.THelper.Run;',
  21620. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21621. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21622. ' $mod.r = $mod.THelper.Run;',
  21623. ' };',
  21624. '});',
  21625. 'rtl.createHelper(this, "THelper", null, function () {',
  21626. ' this.Fly = function (w) {',
  21627. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21628. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21629. ' $mod.r = $mod.THelper.Run;',
  21630. ' };',
  21631. ' this.Glide = function (w) {',
  21632. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21633. ' $mod.r = $mod.THelper.Run;',
  21634. ' };',
  21635. ' this.Run = function (w) {',
  21636. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21637. ' $mod.r = $mod.THelper.Run;',
  21638. ' };',
  21639. '});',
  21640. 'this.f = null;',
  21641. 'this.g = null;',
  21642. 'this.r = null;',
  21643. 'this.Obj = null;',
  21644. '']),
  21645. LinesToStr([ // $mod.$main
  21646. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21647. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21648. '$mod.r = $mod.THelper.Run;',
  21649. 'var $with = $mod.Obj;',
  21650. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21651. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21652. '$mod.r = $mod.THelper.Run;',
  21653. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21654. '$mod.r = $mod.THelper.Run;',
  21655. 'var $with1 = $mod.TObject;',
  21656. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21657. '$mod.r = $mod.THelper.Run;',
  21658. '']));
  21659. end;
  21660. procedure TTestModule.TestClassHelper_Constructor;
  21661. begin
  21662. StartProgram(false);
  21663. Add([
  21664. 'type',
  21665. ' TObject = class',
  21666. ' constructor Create;',
  21667. ' end;',
  21668. ' TClass = class of TObject;',
  21669. ' THelper = class helper for TObject',
  21670. ' constructor NewHlp(w: word);',
  21671. ' end;',
  21672. 'var',
  21673. ' obj: TObject;',
  21674. ' c: TClass;',
  21675. 'constructor TObject.Create;',
  21676. 'begin',
  21677. ' NewHlp(2);', // normal call
  21678. ' tobject.NewHlp(3);', // new instance
  21679. ' c.newhlp(4);', // new instance
  21680. 'end;',
  21681. 'constructor THelper.NewHlp(w: word);',
  21682. 'begin',
  21683. ' create;', // normal call
  21684. ' tobject.create;', // new instance
  21685. ' NewHlp(2);', // normal call
  21686. ' tobject.NewHlp(3);', // new instance
  21687. ' c.newhlp(4);', // new instance
  21688. 'end;',
  21689. 'begin',
  21690. ' obj.newhlp(2);', // normal call
  21691. ' with Obj do newhlp(12);', // normal call
  21692. ' tobject.newhlp(3);', // new instance
  21693. ' with tobject do newhlp(13);', // new instance
  21694. ' c.newhlp(4);', // new instance
  21695. ' with c do newhlp(14);', // new instance
  21696. '']);
  21697. ConvertProgram;
  21698. CheckSource('TestClassHelper_Constructor',
  21699. LinesToStr([ // statements
  21700. 'rtl.createClass(this, "TObject", null, function () {',
  21701. ' this.$init = function () {',
  21702. ' };',
  21703. ' this.$final = function () {',
  21704. ' };',
  21705. ' this.Create = function () {',
  21706. ' $mod.THelper.NewHlp.call(this, 2);',
  21707. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21708. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21709. ' return this;',
  21710. ' };',
  21711. '});',
  21712. 'rtl.createHelper(this, "THelper", null, function () {',
  21713. ' this.NewHlp = function (w) {',
  21714. ' this.Create();',
  21715. ' $mod.TObject.$create("Create");',
  21716. ' $mod.THelper.NewHlp.call(this, 2);',
  21717. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21718. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21719. ' return this;',
  21720. ' };',
  21721. '});',
  21722. 'this.obj = null;',
  21723. 'this.c = null;',
  21724. '']),
  21725. LinesToStr([ // $mod.$main
  21726. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21727. 'var $with = $mod.obj;',
  21728. '$mod.THelper.NewHlp.call($with, 12);',
  21729. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21730. 'var $with1 = $mod.TObject;',
  21731. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21732. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21733. 'var $with2 = $mod.c;',
  21734. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21735. '']));
  21736. end;
  21737. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21738. begin
  21739. StartProgram(false);
  21740. Add([
  21741. 'type',
  21742. ' TObject = class',
  21743. ' procedure Fly;',
  21744. ' end;',
  21745. ' TObjHelper = class helper for TObject',
  21746. ' procedure Fly;',
  21747. ' end;',
  21748. ' TBird = class',
  21749. ' procedure Fly;',
  21750. ' end;',
  21751. ' TBirdHelper = class helper for TBird',
  21752. ' procedure Fly;',
  21753. ' procedure Walk(w: word);',
  21754. ' end;',
  21755. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21756. ' procedure Fly;',
  21757. ' procedure Walk(w: word);',
  21758. ' end;',
  21759. 'procedure Tobject.fly;',
  21760. 'begin',
  21761. ' inherited;', // ignore
  21762. 'end;',
  21763. 'procedure Tobjhelper.fly;',
  21764. 'begin',
  21765. ' {@TObject_Fly}inherited;',
  21766. ' inherited {@TObject_Fly}Fly;',
  21767. 'end;',
  21768. 'procedure Tbird.fly;',
  21769. 'begin',
  21770. ' {@TObjHelper_Fly}inherited;',
  21771. ' inherited {@TObjHelper_Fly}Fly;',
  21772. 'end;',
  21773. 'procedure Tbirdhelper.fly;',
  21774. 'begin',
  21775. ' {@TBird_Fly}inherited;',
  21776. ' inherited {@TBird_Fly}Fly;',
  21777. 'end;',
  21778. 'procedure Tbirdhelper.walk(w: word);',
  21779. 'begin',
  21780. 'end;',
  21781. 'procedure teagleHelper.fly;',
  21782. 'begin',
  21783. ' {@TBird_Fly}inherited;',
  21784. ' inherited {@TBird_Fly}Fly;',
  21785. 'end;',
  21786. 'procedure teagleHelper.walk(w: word);',
  21787. 'begin',
  21788. ' {@TBirdHelper_Walk}inherited;',
  21789. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21790. 'end;',
  21791. 'begin',
  21792. '']);
  21793. ConvertProgram;
  21794. CheckSource('TestClassHelper_InheritedObjFPC',
  21795. LinesToStr([ // statements
  21796. 'rtl.createClass(this, "TObject", null, function () {',
  21797. ' this.$init = function () {',
  21798. ' };',
  21799. ' this.$final = function () {',
  21800. ' };',
  21801. ' this.Fly = function () {',
  21802. ' };',
  21803. '});',
  21804. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21805. ' this.Fly = function () {',
  21806. ' $mod.TObject.Fly.call(this);',
  21807. ' $mod.TObject.Fly.call(this);',
  21808. ' };',
  21809. '});',
  21810. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21811. ' this.Fly$1 = function () {',
  21812. ' $mod.TObjHelper.Fly.call(this);',
  21813. ' $mod.TObjHelper.Fly.call(this);',
  21814. ' };',
  21815. '});',
  21816. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21817. ' this.Fly = function () {',
  21818. ' $mod.TBird.Fly$1.call(this);',
  21819. ' $mod.TBird.Fly$1.call(this);',
  21820. ' };',
  21821. ' this.Walk = function (w) {',
  21822. ' };',
  21823. '});',
  21824. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21825. ' this.Fly$1 = function () {',
  21826. ' $mod.TBird.Fly$1.call(this);',
  21827. ' $mod.TBird.Fly$1.call(this);',
  21828. ' };',
  21829. ' this.Walk$1 = function (w) {',
  21830. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21831. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21832. ' };',
  21833. '});',
  21834. '']),
  21835. LinesToStr([ // $mod.$main
  21836. '']));
  21837. end;
  21838. procedure TTestModule.TestClassHelper_Property;
  21839. begin
  21840. StartProgram(false);
  21841. Add([
  21842. 'type',
  21843. ' TObject = class',
  21844. ' FSize: word;',
  21845. ' function GetSpeed: word;',
  21846. ' procedure SetSpeed(Value: word);',
  21847. ' end;',
  21848. ' TObjHelper = class helper for TObject',
  21849. ' function GetLeft: word;',
  21850. ' procedure SetLeft(Value: word);',
  21851. ' property Size: word read FSize write FSize;',
  21852. ' property Speed: word read GetSpeed write SetSpeed;',
  21853. ' property Left: word read GetLeft write SetLeft;',
  21854. ' end;',
  21855. ' TBird = class',
  21856. ' property NotRight: word read GetLeft write SetLeft;',
  21857. ' procedure DoIt;',
  21858. ' end;',
  21859. 'var',
  21860. ' b: TBird;',
  21861. 'function Tobject.GetSpeed: word;',
  21862. 'begin',
  21863. ' Size:=Size+11;',
  21864. ' Speed:=Speed+12;',
  21865. ' Result:=Left+13;',
  21866. ' Left:=13;',
  21867. ' Left:=Left+13;',
  21868. ' Self.Size:=Self.Size+21;',
  21869. ' Self.Speed:=Self.Speed+22;',
  21870. ' Self.Left:=Self.Left+23;',
  21871. ' with Self do begin',
  21872. ' Size:=Size+31;',
  21873. ' Speed:=Speed+32;',
  21874. ' Left:=Left+33;',
  21875. ' end;',
  21876. 'end;',
  21877. 'procedure Tobject.SetSpeed(Value: word);',
  21878. 'begin',
  21879. 'end;',
  21880. 'function TObjHelper.GetLeft: word;',
  21881. 'begin',
  21882. ' Size:=Size+11;',
  21883. ' Speed:=Speed+12;',
  21884. ' Left:=Left+13;',
  21885. ' Self.Size:=Self.Size+21;',
  21886. ' Self.Speed:=Self.Speed+22;',
  21887. ' Self.Left:=Self.Left+23;',
  21888. ' with Self do begin',
  21889. ' Size:=Size+31;',
  21890. ' Speed:=Speed+32;',
  21891. ' Left:=Left+33;',
  21892. ' end;',
  21893. 'end;',
  21894. 'procedure TObjHelper.SetLeft(Value: word);',
  21895. 'begin',
  21896. 'end;',
  21897. 'procedure TBird.DoIt;',
  21898. 'begin',
  21899. ' NotRight:=NotRight+11;',
  21900. ' Self.NotRight:=Self.NotRight+21;',
  21901. ' with Self do begin',
  21902. ' NotRight:=NotRight+31;',
  21903. ' end;',
  21904. 'end;',
  21905. 'begin',
  21906. ' b.Size:=b.Size+11;',
  21907. ' b.Speed:=b.Speed+12;',
  21908. ' b.Left:=b.Left+13;',
  21909. ' b.NotRight:=b.NotRight+14;',
  21910. ' with b do begin',
  21911. ' Size:=Size+31;',
  21912. ' Speed:=Speed+32;',
  21913. ' Left:=Left+33;',
  21914. ' NotRight:=NotRight+34;',
  21915. ' end;',
  21916. '']);
  21917. ConvertProgram;
  21918. CheckSource('TestClassHelper_Property',
  21919. LinesToStr([ // statements
  21920. 'rtl.createClass(this, "TObject", null, function () {',
  21921. ' this.$init = function () {',
  21922. ' this.FSize = 0;',
  21923. ' };',
  21924. ' this.$final = function () {',
  21925. ' };',
  21926. ' this.GetSpeed = function () {',
  21927. ' var Result = 0;',
  21928. ' this.FSize = this.FSize + 11;',
  21929. ' this.SetSpeed(this.GetSpeed() + 12);',
  21930. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21931. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21932. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21933. ' this.FSize = this.FSize + 21;',
  21934. ' this.SetSpeed(this.GetSpeed() + 22);',
  21935. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21936. ' this.FSize = this.FSize + 31;',
  21937. ' this.SetSpeed(this.GetSpeed() + 32);',
  21938. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21939. ' return Result;',
  21940. ' };',
  21941. ' this.SetSpeed = function (Value) {',
  21942. ' };',
  21943. '});',
  21944. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21945. ' this.GetLeft = function () {',
  21946. ' var Result = 0;',
  21947. ' this.FSize = this.FSize + 11;',
  21948. ' this.SetSpeed(this.GetSpeed() + 12);',
  21949. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21950. ' this.FSize = this.FSize + 21;',
  21951. ' this.SetSpeed(this.GetSpeed() + 22);',
  21952. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21953. ' this.FSize = this.FSize + 31;',
  21954. ' this.SetSpeed(this.GetSpeed() + 32);',
  21955. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21956. ' return Result;',
  21957. ' };',
  21958. ' this.SetLeft = function (Value) {',
  21959. ' };',
  21960. '});',
  21961. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21962. ' this.DoIt = function () {',
  21963. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21964. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21965. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21966. ' };',
  21967. '});',
  21968. 'this.b = null;',
  21969. '']),
  21970. LinesToStr([ // $mod.$main
  21971. '$mod.b.FSize = $mod.b.FSize + 11;',
  21972. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21973. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21974. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21975. 'var $with = $mod.b;',
  21976. '$with.FSize = $with.FSize + 31;',
  21977. '$with.SetSpeed($with.GetSpeed() + 32);',
  21978. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21979. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21980. '']));
  21981. end;
  21982. procedure TTestModule.TestClassHelper_Property_Array;
  21983. begin
  21984. StartProgram(false);
  21985. Add([
  21986. 'type',
  21987. ' TObject = class',
  21988. ' function GetSpeed(Index: boolean): word;',
  21989. ' procedure SetSpeed(Index: boolean; Value: word);',
  21990. ' end;',
  21991. ' TObjHelper = class helper for TObject',
  21992. ' function GetSize(Index: boolean): word;',
  21993. ' procedure SetSize(Index: boolean; Value: word);',
  21994. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21995. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21996. ' end;',
  21997. ' TBird = class',
  21998. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21999. ' procedure DoIt;',
  22000. ' end;',
  22001. 'var',
  22002. ' b: TBird;',
  22003. 'function Tobject.GetSpeed(Index: boolean): word;',
  22004. 'begin',
  22005. ' Result:=Size[false];',
  22006. ' Size[true]:=Size[false]+11;',
  22007. ' Speed[true]:=Speed[false]+12;',
  22008. ' Self.Size[true]:=Self.Size[false]+21;',
  22009. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22010. ' with Self do begin',
  22011. ' Size[true]:=Size[false]+31;',
  22012. ' Speed[true]:=Speed[false]+32;',
  22013. ' end;',
  22014. 'end;',
  22015. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22016. 'begin',
  22017. 'end;',
  22018. 'function TObjHelper.GetSize(Index: boolean): word;',
  22019. 'begin',
  22020. ' Size[true]:=Size[false]+11;',
  22021. ' Speed[true]:=Speed[false]+12;',
  22022. ' Self.Size[true]:=Self.Size[false]+21;',
  22023. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22024. ' with Self do begin',
  22025. ' Size[true]:=Size[false]+31;',
  22026. ' Speed[true]:=Speed[false]+32;',
  22027. ' end;',
  22028. 'end;',
  22029. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22030. 'begin',
  22031. 'end;',
  22032. 'procedure TBird.DoIt;',
  22033. 'begin',
  22034. ' Items[true]:=Items[false]+11;',
  22035. ' Self.Items[true]:=Self.Items[false]+21;',
  22036. ' with Self do Items[true]:=Items[false]+31;',
  22037. 'end;',
  22038. 'begin',
  22039. ' b.Size[true]:=b.Size[false]+11;',
  22040. ' b.Speed[true]:=b.Speed[false]+12;',
  22041. ' b.Items[true]:=b.Items[false]+13;',
  22042. ' with b do begin',
  22043. ' Size[true]:=Size[false]+21;',
  22044. ' Speed[true]:=Speed[false]+22;',
  22045. ' Items[true]:=Items[false]+23;',
  22046. ' end;',
  22047. '']);
  22048. ConvertProgram;
  22049. CheckSource('TestClassHelper_Property_Array',
  22050. LinesToStr([ // statements
  22051. 'rtl.createClass(this, "TObject", null, function () {',
  22052. ' this.$init = function () {',
  22053. ' };',
  22054. ' this.$final = function () {',
  22055. ' };',
  22056. ' this.GetSpeed = function (Index) {',
  22057. ' var Result = 0;',
  22058. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  22059. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22060. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22061. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22062. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22063. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22064. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22065. ' return Result;',
  22066. ' };',
  22067. ' this.SetSpeed = function (Index, Value) {',
  22068. ' };',
  22069. '});',
  22070. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22071. ' this.GetSize = function (Index) {',
  22072. ' var Result = 0;',
  22073. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22074. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22075. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22076. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22077. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22078. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22079. ' return Result;',
  22080. ' };',
  22081. ' this.SetSize = function (Index, Value) {',
  22082. ' };',
  22083. '});',
  22084. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22085. ' this.DoIt = function () {',
  22086. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22087. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22088. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22089. ' };',
  22090. '});',
  22091. 'this.b = null;',
  22092. '']),
  22093. LinesToStr([ // $mod.$main
  22094. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  22095. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  22096. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  22097. 'var $with = $mod.b;',
  22098. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  22099. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  22100. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  22101. '']));
  22102. end;
  22103. procedure TTestModule.TestClassHelper_Property_Array_Default;
  22104. begin
  22105. StartProgram(false);
  22106. Add([
  22107. 'type',
  22108. ' TObject = class',
  22109. ' function GetSpeed(Index: boolean): word;',
  22110. ' procedure SetSpeed(Index: boolean; Value: word);',
  22111. ' end;',
  22112. ' TObjHelper = class helper for TObject',
  22113. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  22114. ' end;',
  22115. ' TBird = class',
  22116. ' end;',
  22117. ' TBirdHelper = class helper for TBird',
  22118. ' function GetSize(Index: word): boolean;',
  22119. ' procedure SetSize(Index: word; Value: boolean);',
  22120. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  22121. ' end;',
  22122. 'function Tobject.GetSpeed(Index: boolean): word;',
  22123. 'begin',
  22124. ' Self[true]:=Self[false]+1;',
  22125. 'end;',
  22126. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22127. 'begin',
  22128. 'end;',
  22129. 'function TBirdHelper.GetSize(Index: word): boolean;',
  22130. 'begin',
  22131. ' Self[1]:=not Self[2];',
  22132. 'end;',
  22133. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  22134. 'begin',
  22135. 'end;',
  22136. 'var',
  22137. ' o: TObject;',
  22138. ' b: TBird;',
  22139. 'begin',
  22140. ' o[true]:=o[false]+1;',
  22141. ' b[3]:=not b[4];',
  22142. '']);
  22143. ConvertProgram;
  22144. CheckSource('TestClassHelper_Property_Array_Default',
  22145. LinesToStr([ // statements
  22146. 'rtl.createClass(this, "TObject", null, function () {',
  22147. ' this.$init = function () {',
  22148. ' };',
  22149. ' this.$final = function () {',
  22150. ' };',
  22151. ' this.GetSpeed = function (Index) {',
  22152. ' var Result = 0;',
  22153. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  22154. ' return Result;',
  22155. ' };',
  22156. ' this.SetSpeed = function (Index, Value) {',
  22157. ' };',
  22158. '});',
  22159. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22160. '});',
  22161. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22162. '});',
  22163. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22164. ' this.GetSize = function (Index) {',
  22165. ' var Result = false;',
  22166. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  22167. ' return Result;',
  22168. ' };',
  22169. ' this.SetSize = function (Index, Value) {',
  22170. ' };',
  22171. '});',
  22172. 'this.o = null;',
  22173. 'this.b = null;',
  22174. '']),
  22175. LinesToStr([ // $mod.$main
  22176. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  22177. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  22178. '']));
  22179. end;
  22180. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  22181. begin
  22182. StartProgram(false);
  22183. Add([
  22184. 'type',
  22185. ' TObject = class',
  22186. ' end;',
  22187. ' TObjHelper = class helper for TObject',
  22188. ' function GetItems(Index: word): TObject;',
  22189. ' procedure SetItems(Index: word; Value: TObject);',
  22190. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  22191. ' end;',
  22192. 'function Tobjhelper.GetItems(Index: word): TObject;',
  22193. 'begin',
  22194. ' Self[1][2]:=Self[3][4];',
  22195. 'end;',
  22196. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  22197. 'begin',
  22198. 'end;',
  22199. 'var',
  22200. ' o: TObject;',
  22201. 'begin',
  22202. ' o[1][2]:=o[3][4];',
  22203. '']);
  22204. ConvertProgram;
  22205. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  22206. LinesToStr([ // statements
  22207. 'rtl.createClass(this, "TObject", null, function () {',
  22208. ' this.$init = function () {',
  22209. ' };',
  22210. ' this.$final = function () {',
  22211. ' };',
  22212. '});',
  22213. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22214. ' this.GetItems = function (Index) {',
  22215. ' var Result = null;',
  22216. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  22217. ' return Result;',
  22218. ' };',
  22219. ' this.SetItems = function (Index, Value) {',
  22220. ' };',
  22221. '});',
  22222. 'this.o = null;',
  22223. '']),
  22224. LinesToStr([ // $mod.$main
  22225. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  22226. '']));
  22227. end;
  22228. procedure TTestModule.TestClassHelper_ClassProperty;
  22229. begin
  22230. StartProgram(false);
  22231. Add([
  22232. 'type',
  22233. ' TObject = class',
  22234. ' class var FSize: word;',
  22235. ' class function GetSpeed: word;',
  22236. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  22237. ' end;',
  22238. ' TObjHelper = class helper for TObject',
  22239. ' class function GetLeft: word;',
  22240. ' class procedure SetLeft(Value: word);',
  22241. ' class property Size: word read FSize write FSize;',
  22242. ' class property Speed: word read GetSpeed write SetSpeed;',
  22243. ' class property Left: word read GetLeft write SetLeft;',
  22244. ' end;',
  22245. ' TBird = class',
  22246. ' class property NotRight: word read GetLeft write SetLeft;',
  22247. ' class procedure DoIt;',
  22248. ' end;',
  22249. ' TBirdClass = class of TBird;',
  22250. 'class function Tobject.GetSpeed: word;',
  22251. 'begin',
  22252. ' Size:=Size+11;',
  22253. ' Speed:=Speed+12;',
  22254. ' Left:=Left+13;',
  22255. ' Self.Size:=Self.Size+21;',
  22256. ' Self.Speed:=Self.Speed+22;',
  22257. ' Self.Left:=Self.Left+23;',
  22258. ' with Self do begin',
  22259. ' Size:=Size+31;',
  22260. ' Speed:=Speed+32;',
  22261. ' Left:=Left+33;',
  22262. ' end;',
  22263. 'end;',
  22264. 'class function TObjHelper.GetLeft: word;',
  22265. 'begin',
  22266. ' Size:=Size+11;',
  22267. ' Speed:=Speed+12;',
  22268. ' Left:=Left+13;',
  22269. ' Self.Size:=Self.Size+21;',
  22270. ' Self.Speed:=Self.Speed+22;',
  22271. ' Self.Left:=Self.Left+23;',
  22272. ' with Self do begin',
  22273. ' Size:=Size+31;',
  22274. ' Speed:=Speed+32;',
  22275. ' Left:=Left+33;',
  22276. ' end;',
  22277. 'end;',
  22278. 'class procedure TObjHelper.SetLeft(Value: word);',
  22279. 'begin',
  22280. 'end;',
  22281. 'class procedure TBird.DoIt;',
  22282. 'begin',
  22283. ' NotRight:=NotRight+11;',
  22284. ' Self.NotRight:=Self.NotRight+21;',
  22285. ' with Self do NotRight:=NotRight+31;',
  22286. 'end;',
  22287. 'var',
  22288. ' b: TBird;',
  22289. ' c: TBirdClass;',
  22290. 'begin',
  22291. ' b.Size:=b.Size+11;',
  22292. ' b.Speed:=b.Speed+12;',
  22293. ' b.Left:=b.Left+13;',
  22294. ' b.NotRight:=b.NotRight+14;',
  22295. ' with b do begin',
  22296. ' Size:=Size+31;',
  22297. ' Speed:=Speed+32;',
  22298. ' Left:=Left+33;',
  22299. ' NotRight:=NotRight+34;',
  22300. ' end;',
  22301. ' c.Size:=c.Size+11;',
  22302. ' c.Speed:=c.Speed+12;',
  22303. ' c.Left:=c.Left+13;',
  22304. ' c.NotRight:=c.NotRight+14;',
  22305. ' with c do begin',
  22306. ' Size:=Size+31;',
  22307. ' Speed:=Speed+32;',
  22308. ' Left:=Left+33;',
  22309. ' NotRight:=NotRight+34;',
  22310. ' end;',
  22311. ' tbird.Size:=tbird.Size+11;',
  22312. ' tbird.Speed:=tbird.Speed+12;',
  22313. ' tbird.Left:=tbird.Left+13;',
  22314. ' tbird.NotRight:=tbird.NotRight+14;',
  22315. ' with tbird do begin',
  22316. ' Size:=Size+31;',
  22317. ' Speed:=Speed+32;',
  22318. ' Left:=Left+33;',
  22319. ' NotRight:=NotRight+34;',
  22320. ' end;',
  22321. '']);
  22322. ConvertProgram;
  22323. CheckSource('TestClassHelper_ClassProperty',
  22324. LinesToStr([ // statements
  22325. 'rtl.createClass(this, "TObject", null, function () {',
  22326. ' this.FSize = 0;',
  22327. ' this.$init = function () {',
  22328. ' };',
  22329. ' this.$final = function () {',
  22330. ' };',
  22331. ' this.GetSpeed = function () {',
  22332. ' var Result = 0;',
  22333. ' $mod.TObject.FSize = this.FSize + 11;',
  22334. ' this.SetSpeed(this.GetSpeed() + 12);',
  22335. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22336. ' $mod.TObject.FSize = this.FSize + 21;',
  22337. ' this.SetSpeed(this.GetSpeed() + 22);',
  22338. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22339. ' $mod.TObject.FSize = this.FSize + 31;',
  22340. ' this.SetSpeed(this.GetSpeed() + 32);',
  22341. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22342. ' return Result;',
  22343. ' };',
  22344. '});',
  22345. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22346. ' this.GetLeft = function () {',
  22347. ' var Result = 0;',
  22348. ' $mod.TObject.FSize = this.FSize + 11;',
  22349. ' this.SetSpeed(this.GetSpeed() + 12);',
  22350. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22351. ' $mod.TObject.FSize = this.FSize + 21;',
  22352. ' this.SetSpeed(this.GetSpeed() + 22);',
  22353. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22354. ' $mod.TObject.FSize = this.FSize + 31;',
  22355. ' this.SetSpeed(this.GetSpeed() + 32);',
  22356. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22357. ' return Result;',
  22358. ' };',
  22359. ' this.SetLeft = function (Value) {',
  22360. ' };',
  22361. '});',
  22362. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22363. ' this.DoIt = function () {',
  22364. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  22365. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  22366. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  22367. ' };',
  22368. '});',
  22369. 'this.b = null;',
  22370. 'this.c = null;',
  22371. '']),
  22372. LinesToStr([ // $mod.$main
  22373. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  22374. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  22375. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  22376. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  22377. 'var $with = $mod.b;',
  22378. '$mod.TObject.FSize = $with.FSize + 31;',
  22379. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  22380. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  22381. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  22382. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  22383. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  22384. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  22385. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  22386. 'var $with1 = $mod.c;',
  22387. '$mod.TObject.FSize = $with1.FSize + 31;',
  22388. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22389. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  22390. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  22391. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  22392. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  22393. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  22394. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  22395. 'var $with2 = $mod.TBird;',
  22396. '$mod.TObject.FSize = $with2.FSize + 31;',
  22397. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22398. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  22399. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  22400. '']));
  22401. end;
  22402. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  22403. begin
  22404. StartProgram(false);
  22405. Add([
  22406. 'type',
  22407. ' TObject = class',
  22408. ' class function GetSpeed: word; static;',
  22409. ' class procedure SetSpeed(Value: word); static;',
  22410. ' end;',
  22411. ' TObjHelper = class helper for TObject',
  22412. ' class function GetLeft: word; static;',
  22413. ' class procedure SetLeft(Value: word); static;',
  22414. ' class property Speed: word read GetSpeed write SetSpeed;',
  22415. ' class property Left: word read GetLeft write SetLeft;',
  22416. ' end;',
  22417. ' TBird = class',
  22418. ' class property NotRight: word read GetLeft write SetLeft;',
  22419. ' class procedure DoIt; static;',
  22420. ' class procedure DoSome;',
  22421. ' end;',
  22422. ' TBirdClass = class of TBird;',
  22423. 'class function Tobject.GetSpeed: word;',
  22424. 'begin',
  22425. ' Speed:=Speed+12;',
  22426. ' Left:=Left+13;',
  22427. 'end;',
  22428. 'class procedure TObject.SetSpeed(Value: word);',
  22429. 'begin',
  22430. 'end;',
  22431. 'class function TObjHelper.GetLeft: word;',
  22432. 'begin',
  22433. ' Speed:=Speed+12;',
  22434. ' Left:=Left+13;',
  22435. 'end;',
  22436. 'class procedure TObjHelper.SetLeft(Value: word);',
  22437. 'begin',
  22438. 'end;',
  22439. 'class procedure TBird.DoIt;',
  22440. 'begin',
  22441. ' NotRight:=NotRight+11;',
  22442. 'end;',
  22443. 'class procedure TBird.DoSome;',
  22444. 'begin',
  22445. ' Speed:=Speed+12;',
  22446. ' Left:=Left+13;',
  22447. ' Self.Speed:=Self.Speed+22;',
  22448. ' Self.Left:=Self.Left+23;',
  22449. ' with Self do begin',
  22450. ' Speed:=Speed+32;',
  22451. ' Left:=Left+33;',
  22452. ' end;',
  22453. ' NotRight:=NotRight+11;',
  22454. ' Self.NotRight:=Self.NotRight+21;',
  22455. ' with Self do NotRight:=NotRight+31;',
  22456. 'end;',
  22457. 'var',
  22458. ' b: TBird;',
  22459. ' c: TBirdClass;',
  22460. 'begin',
  22461. ' b.Speed:=b.Speed+12;',
  22462. ' b.Left:=b.Left+13;',
  22463. ' b.NotRight:=b.NotRight+14;',
  22464. ' with b do begin',
  22465. ' Speed:=Speed+32;',
  22466. ' Left:=Left+33;',
  22467. ' NotRight:=NotRight+34;',
  22468. ' end;',
  22469. ' c.Speed:=c.Speed+12;',
  22470. ' c.Left:=c.Left+13;',
  22471. ' c.NotRight:=c.NotRight+14;',
  22472. ' with c do begin',
  22473. ' Speed:=Speed+32;',
  22474. ' Left:=Left+33;',
  22475. ' NotRight:=NotRight+34;',
  22476. ' end;',
  22477. ' tbird.Speed:=tbird.Speed+12;',
  22478. ' tbird.Left:=tbird.Left+13;',
  22479. ' tbird.NotRight:=tbird.NotRight+14;',
  22480. ' with tbird do begin',
  22481. ' Speed:=Speed+32;',
  22482. ' Left:=Left+33;',
  22483. ' NotRight:=NotRight+34;',
  22484. ' end;',
  22485. '']);
  22486. ConvertProgram;
  22487. CheckSource('TestClassHelper_ClassPropertyStatic',
  22488. LinesToStr([ // statements
  22489. 'rtl.createClass(this, "TObject", null, function () {',
  22490. ' this.$init = function () {',
  22491. ' };',
  22492. ' this.$final = function () {',
  22493. ' };',
  22494. ' this.GetSpeed = function () {',
  22495. ' var Result = 0;',
  22496. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22497. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22498. ' return Result;',
  22499. ' };',
  22500. ' this.SetSpeed = function (Value) {',
  22501. ' };',
  22502. '});',
  22503. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22504. ' this.GetLeft = function () {',
  22505. ' var Result = 0;',
  22506. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22507. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22508. ' return Result;',
  22509. ' };',
  22510. ' this.SetLeft = function (Value) {',
  22511. ' };',
  22512. '});',
  22513. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22514. ' this.DoIt = function () {',
  22515. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22516. ' };',
  22517. ' this.DoSome = function () {',
  22518. ' this.SetSpeed(this.GetSpeed() + 12);',
  22519. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22520. ' this.SetSpeed(this.GetSpeed() + 22);',
  22521. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22522. ' this.SetSpeed(this.GetSpeed() + 32);',
  22523. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22524. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22525. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22526. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22527. ' };',
  22528. '});',
  22529. 'this.b = null;',
  22530. 'this.c = null;',
  22531. '']),
  22532. LinesToStr([ // $mod.$main
  22533. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22534. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22535. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22536. 'var $with = $mod.b;',
  22537. '$with.SetSpeed($with.GetSpeed() + 32);',
  22538. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22539. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22540. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22541. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22542. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22543. 'var $with1 = $mod.c;',
  22544. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22545. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22546. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22547. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22548. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22549. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22550. 'var $with2 = $mod.TBird;',
  22551. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22552. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22553. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22554. '']));
  22555. end;
  22556. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22557. begin
  22558. StartProgram(false);
  22559. Add([
  22560. 'type',
  22561. ' TObject = class',
  22562. ' class function GetSpeed(Index: boolean): word;',
  22563. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22564. ' end;',
  22565. ' TObjHelper = class helper for TObject',
  22566. ' class function GetSize(Index: boolean): word;',
  22567. ' class procedure SetSize(Index: boolean; Value: word);',
  22568. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22569. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22570. ' end;',
  22571. ' TBird = class',
  22572. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22573. ' class procedure DoIt;',
  22574. ' end;',
  22575. ' TBirdClass = class of TBird;',
  22576. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22577. 'begin',
  22578. ' Size[true]:=Size[false]+11;',
  22579. ' Speed[true]:=Speed[false]+12;',
  22580. ' Self.Size[true]:=Self.Size[false]+21;',
  22581. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22582. ' with Self do begin',
  22583. ' Size[true]:=Size[false]+31;',
  22584. ' Speed[true]:=Speed[false]+32;',
  22585. ' end;',
  22586. 'end;',
  22587. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22588. 'begin',
  22589. ' Size[true]:=Size[false]+11;',
  22590. ' Speed[true]:=Speed[false]+12;',
  22591. ' Self.Size[true]:=Self.Size[false]+21;',
  22592. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22593. ' with Self do begin',
  22594. ' Size[true]:=Size[false]+31;',
  22595. ' Speed[true]:=Speed[false]+32;',
  22596. ' end;',
  22597. 'end;',
  22598. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22599. 'begin',
  22600. 'end;',
  22601. 'class procedure TBird.DoIt;',
  22602. 'begin',
  22603. ' Items[true]:=Items[false]+11;',
  22604. ' Self.Items[true]:=Self.Items[false]+21;',
  22605. ' with Self do Items[true]:=Items[false]+31;',
  22606. 'end;',
  22607. 'var',
  22608. ' b: TBird;',
  22609. ' c: TBirdClass;',
  22610. 'begin',
  22611. ' b.Size[true]:=b.Size[false]+11;',
  22612. ' b.Speed[true]:=b.Speed[false]+12;',
  22613. ' b.Items[true]:=b.Items[false]+13;',
  22614. ' with b do begin',
  22615. ' Size[true]:=Size[false]+21;',
  22616. ' Speed[true]:=Speed[false]+22;',
  22617. ' Items[true]:=Items[false]+23;',
  22618. ' end;',
  22619. ' c.Size[true]:=c.Size[false]+11;',
  22620. ' c.Speed[true]:=c.Speed[false]+12;',
  22621. ' c.Items[true]:=c.Items[false]+13;',
  22622. ' with c do begin',
  22623. ' Size[true]:=Size[false]+21;',
  22624. ' Speed[true]:=Speed[false]+22;',
  22625. ' Items[true]:=Items[false]+23;',
  22626. ' end;',
  22627. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22628. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22629. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22630. ' with TBird do begin',
  22631. ' Size[true]:=Size[false]+21;',
  22632. ' Speed[true]:=Speed[false]+22;',
  22633. ' Items[true]:=Items[false]+23;',
  22634. ' end;',
  22635. '']);
  22636. ConvertProgram;
  22637. CheckSource('TestClassHelper_ClassProperty_Array',
  22638. LinesToStr([ // statements
  22639. 'rtl.createClass(this, "TObject", null, function () {',
  22640. ' this.$init = function () {',
  22641. ' };',
  22642. ' this.$final = function () {',
  22643. ' };',
  22644. ' this.GetSpeed = function (Index) {',
  22645. ' var Result = 0;',
  22646. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22647. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22648. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22649. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22650. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22651. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22652. ' return Result;',
  22653. ' };',
  22654. '});',
  22655. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22656. ' this.GetSize = function (Index) {',
  22657. ' var Result = 0;',
  22658. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22659. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22660. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22661. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22662. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22663. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22664. ' return Result;',
  22665. ' };',
  22666. ' this.SetSize = function (Index, Value) {',
  22667. ' };',
  22668. '});',
  22669. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22670. ' this.DoIt = function () {',
  22671. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22672. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22673. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22674. ' };',
  22675. '});',
  22676. 'this.b = null;',
  22677. 'this.c = null;',
  22678. '']),
  22679. LinesToStr([ // $mod.$main
  22680. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22681. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22682. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22683. 'var $with = $mod.b;',
  22684. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22685. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22686. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22687. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22688. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22689. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22690. 'var $with1 = $mod.c;',
  22691. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22692. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22693. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22694. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22695. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22696. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22697. 'var $with2 = $mod.TBird;',
  22698. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22699. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22700. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22701. '']));
  22702. end;
  22703. procedure TTestModule.TestClassHelper_ForIn;
  22704. begin
  22705. StartProgram(false);
  22706. Add([
  22707. 'type',
  22708. ' TObject = class end;',
  22709. ' TItem = TObject;',
  22710. ' TEnumerator = class',
  22711. ' FCurrent: TItem;',
  22712. ' property Current: TItem read FCurrent;',
  22713. ' function MoveNext: boolean;',
  22714. ' end;',
  22715. ' TBird = class',
  22716. ' end;',
  22717. ' TBirdHelper = class helper for TBird',
  22718. ' function GetEnumerator: TEnumerator;',
  22719. ' end;',
  22720. 'function TEnumerator.MoveNext: boolean;',
  22721. 'begin',
  22722. 'end;',
  22723. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22724. 'begin',
  22725. 'end;',
  22726. 'var',
  22727. ' b: TBird;',
  22728. ' i, i2: TItem;',
  22729. 'begin',
  22730. ' for i in b do i2:=i;']);
  22731. ConvertProgram;
  22732. CheckSource('TestClassHelper_ForIn',
  22733. LinesToStr([ // statements
  22734. 'rtl.createClass(this, "TObject", null, function () {',
  22735. ' this.$init = function () {',
  22736. ' };',
  22737. ' this.$final = function () {',
  22738. ' };',
  22739. '});',
  22740. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22741. ' this.$init = function () {',
  22742. ' $mod.TObject.$init.call(this);',
  22743. ' this.FCurrent = null;',
  22744. ' };',
  22745. ' this.$final = function () {',
  22746. ' this.FCurrent = undefined;',
  22747. ' $mod.TObject.$final.call(this);',
  22748. ' };',
  22749. ' this.MoveNext = function () {',
  22750. ' var Result = false;',
  22751. ' return Result;',
  22752. ' };',
  22753. '});',
  22754. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22755. '});',
  22756. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22757. ' this.GetEnumerator = function () {',
  22758. ' var Result = null;',
  22759. ' return Result;',
  22760. ' };',
  22761. '});',
  22762. 'this.b = null;',
  22763. 'this.i = null;',
  22764. 'this.i2 = null;'
  22765. ]),
  22766. LinesToStr([ // $mod.$main
  22767. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22768. 'try {',
  22769. ' while ($in.MoveNext()){',
  22770. ' $mod.i = $in.FCurrent;',
  22771. ' $mod.i2 = $mod.i;',
  22772. ' }',
  22773. '} finally {',
  22774. ' $in = rtl.freeLoc($in)',
  22775. '};',
  22776. '']));
  22777. end;
  22778. procedure TTestModule.TestClassHelper_PassProperty;
  22779. begin
  22780. StartProgram(false);
  22781. Add([
  22782. 'type',
  22783. ' TObject = class',
  22784. ' FField: TObject;',
  22785. ' property Field: TObject read FField write FField;',
  22786. ' end;',
  22787. ' THelper = class helper for TObject',
  22788. ' procedure Fly;',
  22789. ' class procedure Run;',
  22790. ' class procedure Jump; static;',
  22791. ' end;',
  22792. 'procedure THelper.Fly;',
  22793. 'begin',
  22794. ' Field.Fly;',
  22795. ' Field.Run;',
  22796. ' Field.Jump;',
  22797. ' with Field do begin',
  22798. ' Fly;',
  22799. ' Run;',
  22800. ' Jump;',
  22801. ' end;',
  22802. 'end;',
  22803. 'class procedure THelper.Run;',
  22804. 'begin',
  22805. 'end;',
  22806. 'class procedure THelper.Jump;',
  22807. 'begin',
  22808. 'end;',
  22809. 'var',
  22810. ' b: TObject;',
  22811. 'begin',
  22812. ' b.Field.Fly;',
  22813. ' b.Field.Run;',
  22814. ' b.Field.Jump;',
  22815. ' with b do begin',
  22816. ' Field.Run;',
  22817. ' Field.Fly;',
  22818. ' Field.Jump;',
  22819. ' end;',
  22820. ' with b.Field do begin',
  22821. ' Run;',
  22822. ' Fly;',
  22823. ' Jump;',
  22824. ' end;',
  22825. '']);
  22826. ConvertProgram;
  22827. CheckSource('TestClassHelper_PassProperty',
  22828. LinesToStr([ // statements
  22829. 'rtl.createClass(this, "TObject", null, function () {',
  22830. ' this.$init = function () {',
  22831. ' this.FField = null;',
  22832. ' };',
  22833. ' this.$final = function () {',
  22834. ' this.FField = undefined;',
  22835. ' };',
  22836. '});',
  22837. 'rtl.createHelper(this, "THelper", null, function () {',
  22838. ' this.Fly = function () {',
  22839. ' $mod.THelper.Fly.call(this.FField);',
  22840. ' $mod.THelper.Run.call(this.FField.$class);',
  22841. ' $mod.THelper.Jump();',
  22842. ' var $with = this.FField;',
  22843. ' $mod.THelper.Fly.call($with);',
  22844. ' $mod.THelper.Run.call($with.$class);',
  22845. ' $mod.THelper.Jump();',
  22846. ' };',
  22847. ' this.Run = function () {',
  22848. ' };',
  22849. ' this.Jump = function () {',
  22850. ' };',
  22851. '});',
  22852. 'this.b = null;',
  22853. '']),
  22854. LinesToStr([ // $mod.$main
  22855. '$mod.THelper.Fly.call($mod.b.FField);',
  22856. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22857. '$mod.THelper.Jump();',
  22858. 'var $with = $mod.b;',
  22859. '$mod.THelper.Run.call($with.FField.$class);',
  22860. '$mod.THelper.Fly.call($with.FField);',
  22861. '$mod.THelper.Jump();',
  22862. 'var $with1 = $mod.b.FField;',
  22863. '$mod.THelper.Run.call($with1.$class);',
  22864. '$mod.THelper.Fly.call($with1);',
  22865. '$mod.THelper.Jump();',
  22866. '']));
  22867. end;
  22868. procedure TTestModule.TestExtClassHelper_ClassVar;
  22869. begin
  22870. StartProgram(false);
  22871. Add([
  22872. '{$modeswitch externalclass}',
  22873. 'type',
  22874. ' TExtA = class external name ''ExtObj''',
  22875. ' end;',
  22876. ' THelper = class helper for TExtA',
  22877. ' const',
  22878. ' One = 1;',
  22879. ' Two: word = 2;',
  22880. ' class var',
  22881. ' Glob: word;',
  22882. ' function Foo(w: word): word;',
  22883. ' class function Bar(w: word): word; static;',
  22884. ' end;',
  22885. 'function THelper.foo(w: word): word;',
  22886. 'begin',
  22887. ' Result:=w;',
  22888. ' Two:=One+w;',
  22889. ' Glob:=Glob;',
  22890. ' Result:=Self.Glob;',
  22891. ' Self.Glob:=Self.Glob;',
  22892. ' with Self do Glob:=Glob;',
  22893. 'end;',
  22894. 'class function THelper.bar(w: word): word;',
  22895. 'begin',
  22896. ' Result:=w;',
  22897. ' Two:=One;',
  22898. ' Glob:=Glob;',
  22899. 'end;',
  22900. 'var o: TExtA;',
  22901. 'begin',
  22902. ' texta.two:=texta.one;',
  22903. ' texta.Glob:=texta.Glob;',
  22904. ' with texta do begin',
  22905. ' two:=one;',
  22906. ' Glob:=Glob;',
  22907. ' end;',
  22908. ' o.two:=o.one;',
  22909. ' o.Glob:=o.Glob;',
  22910. ' with o do begin',
  22911. ' two:=one;',
  22912. ' Glob:=Glob;',
  22913. ' end;',
  22914. '']);
  22915. ConvertProgram;
  22916. CheckSource('TestExtClassHelper_ClassVar',
  22917. LinesToStr([ // statements
  22918. 'rtl.createHelper(this, "THelper", null, function () {',
  22919. ' this.One = 1;',
  22920. ' this.Two = 2;',
  22921. ' this.Glob = 0;',
  22922. ' this.Foo = function (w) {',
  22923. ' var Result = 0;',
  22924. ' Result = w;',
  22925. ' $mod.THelper.Two = 1 + w;',
  22926. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22927. ' Result = $mod.THelper.Glob;',
  22928. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22929. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22930. ' return Result;',
  22931. ' };',
  22932. ' this.Bar = function (w) {',
  22933. ' var Result = 0;',
  22934. ' Result = w;',
  22935. ' $mod.THelper.Two = 1;',
  22936. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22937. ' return Result;',
  22938. ' };',
  22939. '});',
  22940. 'this.o = null;',
  22941. '']),
  22942. LinesToStr([ // $mod.$main
  22943. '$mod.THelper.Two = 1;',
  22944. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22945. '$mod.THelper.Two = 1;',
  22946. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22947. '$mod.THelper.Two = 1;',
  22948. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22949. 'var $with = $mod.o;',
  22950. '$mod.THelper.Two = 1;',
  22951. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22952. '']));
  22953. end;
  22954. procedure TTestModule.TestExtClassHelper_Method_Call;
  22955. begin
  22956. StartProgram(false);
  22957. Add([
  22958. '{$modeswitch externalclass}',
  22959. 'type',
  22960. ' TFly = function(w: word): word of object;',
  22961. ' TExtA = class external name ''ExtObj''',
  22962. ' procedure Run(w: word = 10);',
  22963. ' end;',
  22964. ' THelper = class helper for TExtA',
  22965. ' function Foo(w: word = 1): word;',
  22966. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22967. ' end;',
  22968. 'var p: TFly;',
  22969. 'function THelper.foo(w: word): word;',
  22970. 'begin',
  22971. ' Run;',
  22972. ' Run();',
  22973. ' Run(11);',
  22974. ' Foo;',
  22975. ' Foo();',
  22976. ' Foo(12);',
  22977. ' Self.Foo;',
  22978. ' Self.Foo();',
  22979. ' Self.Foo(13);',
  22980. ' Fly;',
  22981. ' Fly();',
  22982. ' with Self do begin',
  22983. ' Foo;',
  22984. ' Foo();',
  22985. ' Foo(14);',
  22986. ' Fly;',
  22987. ' Fly();',
  22988. ' end;',
  22989. ' p:=@Fly;',
  22990. 'end;',
  22991. 'var Obj: TExtA;',
  22992. 'begin',
  22993. ' obj.Foo;',
  22994. ' obj.Foo();',
  22995. ' obj.Foo(21);',
  22996. ' obj.Fly;',
  22997. ' obj.Fly();',
  22998. ' with obj do begin',
  22999. ' Foo;',
  23000. ' Foo();',
  23001. ' Foo(22);',
  23002. ' Fly;',
  23003. ' Fly();',
  23004. ' end;',
  23005. ' p:[email protected];',
  23006. '']);
  23007. ConvertProgram;
  23008. CheckSource('TestExtClassHelper_Method_Call',
  23009. LinesToStr([ // statements
  23010. 'rtl.createHelper(this, "THelper", null, function () {',
  23011. ' this.Foo = function (w) {',
  23012. ' var Result = 0;',
  23013. ' this.Run(10);',
  23014. ' this.Run(10);',
  23015. ' this.Run(11);',
  23016. ' $mod.THelper.Foo.call(this, 1);',
  23017. ' $mod.THelper.Foo.call(this, 1);',
  23018. ' $mod.THelper.Foo.call(this, 12);',
  23019. ' $mod.THelper.Foo.call(this, 1);',
  23020. ' $mod.THelper.Foo.call(this, 1);',
  23021. ' $mod.THelper.Foo.call(this, 13);',
  23022. ' this.Fly(2);',
  23023. ' this.Fly(2);',
  23024. ' $mod.THelper.Foo.call(this, 1);',
  23025. ' $mod.THelper.Foo.call(this, 1);',
  23026. ' $mod.THelper.Foo.call(this, 14);',
  23027. ' this.Fly(2);',
  23028. ' this.Fly(2);',
  23029. ' $mod.p = rtl.createCallback(this, "Fly");',
  23030. ' return Result;',
  23031. ' };',
  23032. '});',
  23033. 'this.p = null;',
  23034. 'this.Obj = null;',
  23035. '']),
  23036. LinesToStr([ // $mod.$main
  23037. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23038. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23039. '$mod.THelper.Foo.call($mod.Obj, 21);',
  23040. '$mod.Obj.Fly(2);',
  23041. '$mod.Obj.Fly(2);',
  23042. 'var $with = $mod.Obj;',
  23043. '$mod.THelper.Foo.call($with, 1);',
  23044. '$mod.THelper.Foo.call($with, 1);',
  23045. '$mod.THelper.Foo.call($with, 22);',
  23046. '$with.Fly(2);',
  23047. '$with.Fly(2);',
  23048. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  23049. '']));
  23050. end;
  23051. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  23052. begin
  23053. StartProgram(false);
  23054. Add([
  23055. '{$modeswitch externalclass}',
  23056. 'type',
  23057. ' TExtA = class external name ''ExtObj''',
  23058. ' procedure Run(w: word = 10);',
  23059. ' end;',
  23060. ' THelper = class helper for TExtA',
  23061. ' class procedure Fly;',
  23062. ' end;',
  23063. 'class procedure THelper.Fly;',
  23064. 'begin end;',
  23065. 'begin',
  23066. '']);
  23067. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  23068. nHelperClassMethodForExtClassMustBeStatic);
  23069. ConvertProgram;
  23070. end;
  23071. procedure TTestModule.TestRecordHelper_ClassVar;
  23072. begin
  23073. StartProgram(false);
  23074. Add([
  23075. 'type',
  23076. ' TRec = record',
  23077. ' end;',
  23078. ' THelper = record helper for TRec',
  23079. ' const',
  23080. ' One = 1;',
  23081. ' Two: word = 2;',
  23082. ' class var',
  23083. ' Glob: word;',
  23084. ' function Foo(w: word): word;',
  23085. ' class function Bar(w: word): word; static;',
  23086. ' end;',
  23087. 'function THelper.foo(w: word): word;',
  23088. 'begin',
  23089. ' Result:=w;',
  23090. ' Two:=One+w;',
  23091. ' Glob:=Glob;',
  23092. ' Result:=Self.Glob;',
  23093. ' Self.Glob:=Self.Glob;',
  23094. ' with Self do Glob:=Glob;',
  23095. ' Self:=Self;',
  23096. 'end;',
  23097. 'class function THelper.bar(w: word): word;',
  23098. 'begin',
  23099. ' Result:=w;',
  23100. ' Two:=One;',
  23101. ' Glob:=Glob;',
  23102. 'end;',
  23103. 'var r: TRec;',
  23104. 'begin',
  23105. ' trec.two:=trec.one;',
  23106. ' trec.Glob:=trec.Glob;',
  23107. ' with trec do begin',
  23108. ' two:=one;',
  23109. ' Glob:=Glob;',
  23110. ' end;',
  23111. ' r.two:=r.one;',
  23112. ' r.Glob:=r.Glob;',
  23113. ' with r do begin',
  23114. ' two:=one;',
  23115. ' Glob:=Glob;',
  23116. ' end;',
  23117. '']);
  23118. ConvertProgram;
  23119. CheckSource('TestRecordHelper_ClassVar',
  23120. LinesToStr([ // statements
  23121. 'rtl.recNewT(this, "TRec", function () {',
  23122. ' this.$eq = function (b) {',
  23123. ' return true;',
  23124. ' };',
  23125. ' this.$assign = function (s) {',
  23126. ' return this;',
  23127. ' };',
  23128. '});',
  23129. 'rtl.createHelper(this, "THelper", null, function () {',
  23130. ' this.One = 1;',
  23131. ' this.Two = 2;',
  23132. ' this.Glob = 0;',
  23133. ' this.Foo = function (w) {',
  23134. ' var Result = 0;',
  23135. ' Result = w;',
  23136. ' $mod.THelper.Two = 1 + w;',
  23137. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23138. ' Result = $mod.THelper.Glob;',
  23139. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23140. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23141. ' this.$assign(this);',
  23142. ' return Result;',
  23143. ' };',
  23144. ' this.Bar = function (w) {',
  23145. ' var Result = 0;',
  23146. ' Result = w;',
  23147. ' $mod.THelper.Two = 1;',
  23148. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23149. ' return Result;',
  23150. ' };',
  23151. '});',
  23152. 'this.r = this.TRec.$new();',
  23153. '']),
  23154. LinesToStr([ // $mod.$main
  23155. '$mod.THelper.Two = 1;',
  23156. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23157. 'var $with = $mod.TRec;',
  23158. '$mod.THelper.Two = 1;',
  23159. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23160. '$mod.THelper.Two = 1;',
  23161. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23162. 'var $with1 = $mod.r;',
  23163. '$mod.THelper.Two = 1;',
  23164. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23165. '']));
  23166. end;
  23167. procedure TTestModule.TestRecordHelper_Method_Call;
  23168. begin
  23169. StartProgram(false);
  23170. Add([
  23171. '{$modeswitch AdvancedRecords}',
  23172. 'type',
  23173. ' TRec = record',
  23174. ' procedure Run(w: word = 10);',
  23175. ' end;',
  23176. ' THelper = record helper for TRec',
  23177. ' function Foo(w: word = 1): word;',
  23178. ' end;',
  23179. 'procedure TRec.Run(w: word);',
  23180. 'begin',
  23181. ' Foo;',
  23182. ' Foo();',
  23183. ' Foo(2);',
  23184. ' Self.Foo;',
  23185. ' Self.Foo();',
  23186. ' Self.Foo(3);',
  23187. ' with Self do begin',
  23188. ' Foo;',
  23189. ' Foo();',
  23190. ' Foo(4);',
  23191. ' end;',
  23192. 'end;',
  23193. 'function THelper.foo(w: word): word;',
  23194. 'begin',
  23195. ' Run;',
  23196. ' Run();',
  23197. ' Run(11);',
  23198. ' Foo;',
  23199. ' Foo();',
  23200. ' Foo(12);',
  23201. ' Self.Foo;',
  23202. ' Self.Foo();',
  23203. ' Self.Foo(13);',
  23204. ' with Self do begin',
  23205. ' Foo;',
  23206. ' Foo();',
  23207. ' Foo(14);',
  23208. ' end;',
  23209. 'end;',
  23210. 'var Rec: TRec;',
  23211. 'begin',
  23212. ' Rec.Foo;',
  23213. ' Rec.Foo();',
  23214. ' Rec.Foo(21);',
  23215. ' with Rec do begin',
  23216. ' Foo;',
  23217. ' Foo();',
  23218. ' Foo(22);',
  23219. ' end;',
  23220. '']);
  23221. ConvertProgram;
  23222. CheckSource('TestRecordHelper_Method_Call',
  23223. LinesToStr([ // statements
  23224. 'rtl.recNewT(this, "TRec", function () {',
  23225. ' this.$eq = function (b) {',
  23226. ' return true;',
  23227. ' };',
  23228. ' this.$assign = function (s) {',
  23229. ' return this;',
  23230. ' };',
  23231. ' this.Run = function (w) {',
  23232. ' $mod.THelper.Foo.call(this, 1);',
  23233. ' $mod.THelper.Foo.call(this, 1);',
  23234. ' $mod.THelper.Foo.call(this, 2);',
  23235. ' $mod.THelper.Foo.call(this, 1);',
  23236. ' $mod.THelper.Foo.call(this, 1);',
  23237. ' $mod.THelper.Foo.call(this, 3);',
  23238. ' $mod.THelper.Foo.call(this, 1);',
  23239. ' $mod.THelper.Foo.call(this, 1);',
  23240. ' $mod.THelper.Foo.call(this, 4);',
  23241. ' };',
  23242. '});',
  23243. 'rtl.createHelper(this, "THelper", null, function () {',
  23244. ' this.Foo = function (w) {',
  23245. ' var Result = 0;',
  23246. ' this.Run(10);',
  23247. ' this.Run(10);',
  23248. ' this.Run(11);',
  23249. ' $mod.THelper.Foo.call(this, 1);',
  23250. ' $mod.THelper.Foo.call(this, 1);',
  23251. ' $mod.THelper.Foo.call(this, 12);',
  23252. ' $mod.THelper.Foo.call(this, 1);',
  23253. ' $mod.THelper.Foo.call(this, 1);',
  23254. ' $mod.THelper.Foo.call(this, 13);',
  23255. ' $mod.THelper.Foo.call(this, 1);',
  23256. ' $mod.THelper.Foo.call(this, 1);',
  23257. ' $mod.THelper.Foo.call(this, 14);',
  23258. ' return Result;',
  23259. ' };',
  23260. '});',
  23261. 'this.Rec = this.TRec.$new();',
  23262. '']),
  23263. LinesToStr([ // $mod.$main
  23264. '$mod.THelper.Foo.call($mod.Rec, 1);',
  23265. '$mod.THelper.Foo.call($mod.Rec, 1);',
  23266. '$mod.THelper.Foo.call($mod.Rec, 21);',
  23267. 'var $with = $mod.Rec;',
  23268. '$mod.THelper.Foo.call($with, 1);',
  23269. '$mod.THelper.Foo.call($with, 1);',
  23270. '$mod.THelper.Foo.call($with, 22);',
  23271. '']));
  23272. end;
  23273. procedure TTestModule.TestRecordHelper_Constructor;
  23274. begin
  23275. StartProgram(false);
  23276. Add([
  23277. '{$modeswitch AdvancedRecords}',
  23278. 'type',
  23279. ' TRec = record',
  23280. ' constructor Create(w: word);',
  23281. ' end;',
  23282. ' THelper = record helper for TRec',
  23283. ' constructor NewHlp(w: word);',
  23284. ' end;',
  23285. 'var',
  23286. ' Rec: TRec;',
  23287. 'constructor TRec.Create(w: word);',
  23288. 'begin',
  23289. ' NewHlp(2);', // normal call
  23290. ' trec.NewHlp(3);', // new instance
  23291. 'end;',
  23292. 'constructor THelper.NewHlp(w: word);',
  23293. 'begin',
  23294. ' create(2);', // normal call
  23295. ' trec.create(3);', // new instance
  23296. ' NewHlp(4);', // normal call
  23297. ' trec.NewHlp(5);', // new instance
  23298. 'end;',
  23299. 'begin',
  23300. ' rec.newhlp(2);', // normal call
  23301. ' with rec do newhlp(12);', // normal call
  23302. ' trec.newhlp(3);', // new instance
  23303. ' with trec do newhlp(13);', // new instance
  23304. '']);
  23305. ConvertProgram;
  23306. CheckSource('TestRecordHelper_Constructor',
  23307. LinesToStr([ // statements
  23308. 'rtl.recNewT(this, "TRec", function () {',
  23309. ' this.$eq = function (b) {',
  23310. ' return true;',
  23311. ' };',
  23312. ' this.$assign = function (s) {',
  23313. ' return this;',
  23314. ' };',
  23315. ' this.Create = function (w) {',
  23316. ' $mod.THelper.NewHlp.call(this, 2);',
  23317. ' $mod.THelper.$new("NewHlp", [3]);',
  23318. ' return this;',
  23319. ' };',
  23320. '});',
  23321. 'rtl.createHelper(this, "THelper", null, function () {',
  23322. ' this.NewHlp = function (w) {',
  23323. ' this.Create(2);',
  23324. ' $mod.TRec.$new().Create(3);',
  23325. ' $mod.THelper.NewHlp.call(this, 4);',
  23326. ' $mod.THelper.$new("NewHlp", [5]);',
  23327. ' return this;',
  23328. ' };',
  23329. ' this.$new = function (fn, args) {',
  23330. ' return this[fn].apply($mod.TRec.$new(), args);',
  23331. ' };',
  23332. '});',
  23333. 'this.Rec = this.TRec.$new();',
  23334. '']),
  23335. LinesToStr([ // $mod.$main
  23336. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  23337. 'var $with = $mod.Rec;',
  23338. '$mod.THelper.NewHlp.call($with, 12);',
  23339. '$mod.THelper.$new("NewHlp", [3]);',
  23340. 'var $with1 = $mod.TRec;',
  23341. '$mod.THelper.$new("NewHlp", [13]);',
  23342. '']));
  23343. end;
  23344. procedure TTestModule.TestTypeHelper_ClassVar;
  23345. begin
  23346. StartProgram(false);
  23347. Add([
  23348. '{$modeswitch typehelpers}',
  23349. 'type',
  23350. ' THelper = type helper for byte',
  23351. ' const',
  23352. ' One = 1;',
  23353. ' Two: word = 2;',
  23354. ' class var',
  23355. ' Glob: word;',
  23356. ' function Foo(w: word): word;',
  23357. ' class function Bar(w: word): word; static;',
  23358. ' end;',
  23359. 'function THelper.foo(w: word): word;',
  23360. 'begin',
  23361. ' Result:=w;',
  23362. ' Two:=One+w;',
  23363. ' Glob:=Glob;',
  23364. ' Result:=Self.Glob;',
  23365. ' Self.Glob:=Self.Glob;',
  23366. ' with Self do Glob:=Glob;',
  23367. 'end;',
  23368. 'class function THelper.bar(w: word): word;',
  23369. 'begin',
  23370. ' Result:=w;',
  23371. ' Two:=One;',
  23372. ' Glob:=Glob;',
  23373. 'end;',
  23374. 'var b: byte;',
  23375. 'begin',
  23376. ' byte.two:=byte.one;',
  23377. ' byte.Glob:=byte.Glob;',
  23378. ' with byte do begin',
  23379. ' two:=one;',
  23380. ' Glob:=Glob;',
  23381. ' end;',
  23382. ' b.two:=b.one;',
  23383. ' b.Glob:=b.Glob;',
  23384. ' with b do begin',
  23385. ' two:=one;',
  23386. ' Glob:=Glob;',
  23387. ' end;',
  23388. '']);
  23389. ConvertProgram;
  23390. CheckSource('TestTypeHelper_ClassVar',
  23391. LinesToStr([ // statements
  23392. 'rtl.createHelper(this, "THelper", null, function () {',
  23393. ' this.One = 1;',
  23394. ' this.Two = 2;',
  23395. ' this.Glob = 0;',
  23396. ' this.Foo = function (w) {',
  23397. ' var Result = 0;',
  23398. ' Result = w;',
  23399. ' $mod.THelper.Two = 1 + w;',
  23400. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23401. ' Result = $mod.THelper.Glob;',
  23402. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23403. ' var $with = this.get();',
  23404. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23405. ' return Result;',
  23406. ' };',
  23407. ' this.Bar = function (w) {',
  23408. ' var Result = 0;',
  23409. ' Result = w;',
  23410. ' $mod.THelper.Two = 1;',
  23411. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23412. ' return Result;',
  23413. ' };',
  23414. '});',
  23415. 'this.b = 0;',
  23416. '']),
  23417. LinesToStr([ // $mod.$main
  23418. '$mod.THelper.Two = 1;',
  23419. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23420. '$mod.THelper.Two = 1;',
  23421. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23422. '$mod.THelper.Two = 1;',
  23423. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23424. 'var $with = $mod.b;',
  23425. '$mod.THelper.Two = 1;',
  23426. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23427. '']));
  23428. end;
  23429. procedure TTestModule.TestTypeHelper_PassResultElement;
  23430. begin
  23431. StartProgram(false);
  23432. Add([
  23433. '{$modeswitch typehelpers}',
  23434. 'type',
  23435. ' THelper = type helper for word',
  23436. ' procedure DoIt(e: byte = 123);',
  23437. ' class procedure DoSome(e: byte = 456); static;',
  23438. ' end;',
  23439. 'procedure THelper.DoIt(e: byte);',
  23440. 'begin',
  23441. 'end;',
  23442. 'class procedure THelper.DoSome(e: byte);',
  23443. 'begin',
  23444. 'end;',
  23445. 'function Foo(w: word): word;',
  23446. 'begin',
  23447. ' Result.DoIt;',
  23448. ' Result.DoIt();',
  23449. ' Result.DoSome;',
  23450. ' Result.DoSome();',
  23451. ' with Result do begin',
  23452. ' DoIt;',
  23453. ' DoIt();',
  23454. ' DoSome;',
  23455. ' DoSome();',
  23456. ' end;',
  23457. 'end;',
  23458. 'begin',
  23459. '']);
  23460. ConvertProgram;
  23461. CheckSource('TestTypeHelper_PassResultElement',
  23462. LinesToStr([ // statements
  23463. 'rtl.createHelper(this, "THelper", null, function () {',
  23464. ' this.DoIt = function (e) {',
  23465. ' };',
  23466. ' this.DoSome = function (e) {',
  23467. ' };',
  23468. '});',
  23469. 'this.Foo = function (w) {',
  23470. ' var Result = 0;',
  23471. ' $mod.THelper.DoIt.call({',
  23472. ' get: function () {',
  23473. ' return Result;',
  23474. ' },',
  23475. ' set: function (v) {',
  23476. ' Result = v;',
  23477. ' }',
  23478. ' }, 123);',
  23479. ' $mod.THelper.DoIt.call({',
  23480. ' get: function () {',
  23481. ' return Result;',
  23482. ' },',
  23483. ' set: function (v) {',
  23484. ' Result = v;',
  23485. ' }',
  23486. ' }, 123);',
  23487. ' $mod.THelper.DoSome(456);',
  23488. ' $mod.THelper.DoSome(456);',
  23489. ' $mod.THelper.DoIt.call({',
  23490. ' get: function () {',
  23491. ' return Result;',
  23492. ' },',
  23493. ' set: function (v) {',
  23494. ' Result = v;',
  23495. ' }',
  23496. ' }, 123);',
  23497. ' $mod.THelper.DoIt.call({',
  23498. ' get: function () {',
  23499. ' return Result;',
  23500. ' },',
  23501. ' set: function (v) {',
  23502. ' Result = v;',
  23503. ' }',
  23504. ' }, 123);',
  23505. ' $mod.THelper.DoSome(456);',
  23506. ' $mod.THelper.DoSome(456);',
  23507. ' return Result;',
  23508. '};',
  23509. '']),
  23510. LinesToStr([ // $mod.$main
  23511. '']));
  23512. end;
  23513. procedure TTestModule.TestTypeHelper_PassArgs;
  23514. begin
  23515. StartProgram(false);
  23516. Add([
  23517. '{$modeswitch typehelpers}',
  23518. 'type',
  23519. ' THelper = type helper for word',
  23520. ' procedure DoIt(e: byte = 123);',
  23521. ' end;',
  23522. 'procedure THelper.DoIt(e: byte);',
  23523. 'begin',
  23524. 'end;',
  23525. 'procedure FooDefault(a: word);',
  23526. 'begin',
  23527. ' a.DoIt;',
  23528. ' with a do DoIt;',
  23529. 'end;',
  23530. 'procedure FooConst(const a: word);',
  23531. 'begin',
  23532. ' a.DoIt;',
  23533. ' with a do DoIt;',
  23534. 'end;',
  23535. 'procedure FooVar(var a: word);',
  23536. 'begin',
  23537. ' a.DoIt;',
  23538. ' with a do DoIt;',
  23539. 'end;',
  23540. 'begin',
  23541. '']);
  23542. ConvertProgram;
  23543. CheckSource('TestTypeHelper_PassArgs',
  23544. LinesToStr([ // statements
  23545. 'rtl.createHelper(this, "THelper", null, function () {',
  23546. ' this.DoIt = function (e) {',
  23547. ' };',
  23548. '});',
  23549. 'this.FooDefault = function (a) {',
  23550. ' $mod.THelper.DoIt.call({',
  23551. ' get: function () {',
  23552. ' return a;',
  23553. ' },',
  23554. ' set: function (v) {',
  23555. ' a = v;',
  23556. ' }',
  23557. ' }, 123);',
  23558. ' $mod.THelper.DoIt.call({',
  23559. ' get: function () {',
  23560. ' return a;',
  23561. ' },',
  23562. ' set: function (v) {',
  23563. ' a = v;',
  23564. ' }',
  23565. ' }, 123);',
  23566. '};',
  23567. 'this.FooConst = function (a) {',
  23568. ' $mod.THelper.DoIt.call({',
  23569. ' get: function () {',
  23570. ' return a;',
  23571. ' },',
  23572. ' set: function (v) {',
  23573. ' rtl.raiseE("EPropReadOnly");',
  23574. ' }',
  23575. ' }, 123);',
  23576. ' $mod.THelper.DoIt.call({',
  23577. ' get: function () {',
  23578. ' return a;',
  23579. ' },',
  23580. ' set: function () {',
  23581. ' rtl.raiseE("EPropReadOnly");',
  23582. ' }',
  23583. ' }, 123);',
  23584. '};',
  23585. 'this.FooVar = function (a) {',
  23586. ' $mod.THelper.DoIt.call(a, 123);',
  23587. ' var $with = a.get();',
  23588. ' $mod.THelper.DoIt.call(a, 123);',
  23589. '};',
  23590. '']),
  23591. LinesToStr([ // $mod.$main
  23592. '']));
  23593. end;
  23594. procedure TTestModule.TestTypeHelper_PassVarConst;
  23595. begin
  23596. StartProgram(false);
  23597. Add([
  23598. '{$modeswitch typehelpers}',
  23599. 'type',
  23600. ' THelper = type helper for word',
  23601. ' procedure DoIt(e: byte = 123);',
  23602. ' end;',
  23603. 'procedure THelper.DoIt(e: byte);',
  23604. 'begin',
  23605. 'end;',
  23606. 'var a: word;',
  23607. 'const c: word = 2;',
  23608. '{$writeableconst off}',
  23609. 'const r: word = 3;',
  23610. 'begin',
  23611. ' a.DoIt;',
  23612. ' with a do DoIt;',
  23613. ' c.DoIt;',
  23614. ' with c do DoIt;',
  23615. ' r.DoIt;',
  23616. ' with r do DoIt;',
  23617. '']);
  23618. ConvertProgram;
  23619. CheckSource('TestTypeHelper_PassVarConst',
  23620. LinesToStr([ // statements
  23621. 'rtl.createHelper(this, "THelper", null, function () {',
  23622. ' this.DoIt = function (e) {',
  23623. ' };',
  23624. '});',
  23625. 'this.a = 0;',
  23626. 'this.c = 2;',
  23627. 'this.r = 3;',
  23628. '']),
  23629. LinesToStr([ // $mod.$main
  23630. '$mod.THelper.DoIt.call({',
  23631. ' p: $mod,',
  23632. ' get: function () {',
  23633. ' return this.p.a;',
  23634. ' },',
  23635. ' set: function (v) {',
  23636. ' this.p.a = v;',
  23637. ' }',
  23638. '}, 123);',
  23639. 'var $with = $mod.a;',
  23640. '$mod.THelper.DoIt.call({',
  23641. ' get: function () {',
  23642. ' return $with;',
  23643. ' },',
  23644. ' set: function (v) {',
  23645. ' $with = v;',
  23646. ' }',
  23647. '}, 123);',
  23648. '$mod.THelper.DoIt.call({',
  23649. ' p: $mod,',
  23650. ' get: function () {',
  23651. ' return this.p.c;',
  23652. ' },',
  23653. ' set: function (v) {',
  23654. ' this.p.c = v;',
  23655. ' }',
  23656. '}, 123);',
  23657. 'var $with1 = $mod.c;',
  23658. '$mod.THelper.DoIt.call({',
  23659. ' get: function () {',
  23660. ' return $with1;',
  23661. ' },',
  23662. ' set: function (v) {',
  23663. ' $with1 = v;',
  23664. ' }',
  23665. '}, 123);',
  23666. '$mod.THelper.DoIt.call({',
  23667. ' get: function () {',
  23668. ' return 3;',
  23669. ' },',
  23670. ' set: function (v) {',
  23671. ' rtl.raiseE("EPropReadOnly");',
  23672. ' }',
  23673. '}, 123);',
  23674. 'var $with2 = 3;',
  23675. ' $mod.THelper.DoIt.call({',
  23676. ' get: function () {',
  23677. ' return $with2;',
  23678. ' },',
  23679. ' set: function () {',
  23680. ' rtl.raiseE("EPropReadOnly");',
  23681. ' }',
  23682. ' }, 123);',
  23683. '']));
  23684. end;
  23685. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23686. begin
  23687. StartProgram(false);
  23688. Add([
  23689. '{$modeswitch typehelpers}',
  23690. 'type',
  23691. ' THelper = type helper for word',
  23692. ' procedure DoIt(e: byte = 123);',
  23693. ' end;',
  23694. 'procedure THelper.DoIt(e: byte);',
  23695. 'begin',
  23696. 'end;',
  23697. 'function Foo(b: byte = 1): word;',
  23698. 'begin',
  23699. 'end;',
  23700. 'begin',
  23701. ' Foo.DoIt;',
  23702. ' Foo().DoIt;',
  23703. ' with Foo do DoIt;',
  23704. ' with Foo() do DoIt;',
  23705. '']);
  23706. ConvertProgram;
  23707. CheckSource('TestTypeHelper_PassFuncResult',
  23708. LinesToStr([ // statements
  23709. 'rtl.createHelper(this, "THelper", null, function () {',
  23710. ' this.DoIt = function (e) {',
  23711. ' };',
  23712. '});',
  23713. 'this.Foo = function (b) {',
  23714. ' var Result = 0;',
  23715. ' return Result;',
  23716. '};',
  23717. '']),
  23718. LinesToStr([ // $mod.$main
  23719. '$mod.THelper.DoIt.call({',
  23720. ' a: $mod.Foo(1),',
  23721. ' get: function () {',
  23722. ' return this.a;',
  23723. ' },',
  23724. ' set: function (v) {',
  23725. ' this.a = v;',
  23726. ' }',
  23727. '}, 123);',
  23728. '$mod.THelper.DoIt.call({',
  23729. ' a: $mod.Foo(1),',
  23730. ' get: function () {',
  23731. ' return this.a;',
  23732. ' },',
  23733. ' set: function (v) {',
  23734. ' this.a = v;',
  23735. ' }',
  23736. '}, 123);',
  23737. 'var $with = $mod.Foo(1);',
  23738. '$mod.THelper.DoIt.call({',
  23739. ' get: function () {',
  23740. ' return $with;',
  23741. ' },',
  23742. ' set: function (v) {',
  23743. ' $with = v;',
  23744. ' }',
  23745. '}, 123);',
  23746. 'var $with1 = $mod.Foo(1);',
  23747. '$mod.THelper.DoIt.call({',
  23748. ' get: function () {',
  23749. ' return $with1;',
  23750. ' },',
  23751. ' set: function (v) {',
  23752. ' $with1 = v;',
  23753. ' }',
  23754. '}, 123);',
  23755. '']));
  23756. end;
  23757. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23758. begin
  23759. StartProgram(false);
  23760. Add([
  23761. '{$modeswitch typehelpers}',
  23762. 'type',
  23763. ' TObject = class',
  23764. ' FField: word;',
  23765. ' procedure SetField(Value: word);',
  23766. ' property Field: word read FField write SetField;',
  23767. ' end;',
  23768. ' THelper = type helper for word',
  23769. ' procedure Fly;',
  23770. ' class procedure Run; static;',
  23771. ' end;',
  23772. 'procedure TObject.SetField(Value: word);',
  23773. 'begin',
  23774. ' Field.Fly;',
  23775. ' Field.Run;',
  23776. ' Self.Field.Fly;',
  23777. ' Self.Field.Run;',
  23778. ' with Self do begin',
  23779. ' Field.Fly;',
  23780. ' Field.Run;',
  23781. ' end;',
  23782. ' with Self.Field do begin',
  23783. ' Fly;',
  23784. ' Run;',
  23785. ' end;',
  23786. 'end;',
  23787. 'procedure THelper.Fly;',
  23788. 'begin',
  23789. 'end;',
  23790. 'class procedure THelper.Run;',
  23791. 'begin',
  23792. 'end;',
  23793. 'var',
  23794. ' o: TObject;',
  23795. 'begin',
  23796. ' o.Field.Fly;',
  23797. ' o.Field.Run;',
  23798. ' with o do begin',
  23799. ' Field.Fly;',
  23800. ' Field.Run;',
  23801. ' end;',
  23802. ' with o.Field do begin',
  23803. ' Fly;',
  23804. ' Run;',
  23805. ' end;',
  23806. '']);
  23807. ConvertProgram;
  23808. CheckSource('TestTypeHelper_PassPropertyField',
  23809. LinesToStr([ // statements
  23810. 'rtl.createClass(this, "TObject", null, function () {',
  23811. ' this.$init = function () {',
  23812. ' this.FField = 0;',
  23813. ' };',
  23814. ' this.$final = function () {',
  23815. ' };',
  23816. ' this.SetField = function (Value) {',
  23817. ' $mod.THelper.Fly.call({',
  23818. ' p: this,',
  23819. ' get: function () {',
  23820. ' return this.p.FField;',
  23821. ' },',
  23822. ' set: function (v) {',
  23823. ' this.p.FField = v;',
  23824. ' }',
  23825. ' });',
  23826. ' $mod.THelper.Run();',
  23827. ' $mod.THelper.Fly.call({',
  23828. ' p: this,',
  23829. ' get: function () {',
  23830. ' return this.p.FField;',
  23831. ' },',
  23832. ' set: function (v) {',
  23833. ' this.p.FField = v;',
  23834. ' }',
  23835. ' });',
  23836. ' $mod.THelper.Run();',
  23837. ' $mod.THelper.Fly.call({',
  23838. ' p: this,',
  23839. ' get: function () {',
  23840. ' return this.p.FField;',
  23841. ' },',
  23842. ' set: function (v) {',
  23843. ' this.p.FField = v;',
  23844. ' }',
  23845. ' });',
  23846. ' $mod.THelper.Run();',
  23847. ' var $with = this.FField;',
  23848. ' $mod.THelper.Fly.call({',
  23849. ' get: function () {',
  23850. ' return $with;',
  23851. ' },',
  23852. ' set: function (v) {',
  23853. ' $with = v;',
  23854. ' }',
  23855. ' });',
  23856. ' $mod.THelper.Run();',
  23857. ' };',
  23858. '});',
  23859. 'rtl.createHelper(this, "THelper", null, function () {',
  23860. ' this.Fly = function () {',
  23861. ' };',
  23862. ' this.Run = function () {',
  23863. ' };',
  23864. '});',
  23865. 'this.o = null;',
  23866. '']),
  23867. LinesToStr([ // $mod.$main
  23868. '$mod.THelper.Fly.call({',
  23869. ' p: $mod.o,',
  23870. ' get: function () {',
  23871. ' return this.p.FField;',
  23872. ' },',
  23873. ' set: function (v) {',
  23874. ' this.p.FField = v;',
  23875. ' }',
  23876. '});',
  23877. '$mod.THelper.Run();',
  23878. 'var $with = $mod.o;',
  23879. '$mod.THelper.Fly.call({',
  23880. ' p: $with,',
  23881. ' get: function () {',
  23882. ' return this.p.FField;',
  23883. ' },',
  23884. ' set: function (v) {',
  23885. ' this.p.FField = v;',
  23886. ' }',
  23887. '});',
  23888. '$mod.THelper.Run();',
  23889. 'var $with1 = $mod.o.FField;',
  23890. '$mod.THelper.Fly.call({',
  23891. ' get: function () {',
  23892. ' return $with1;',
  23893. ' },',
  23894. ' set: function (v) {',
  23895. ' $with1 = v;',
  23896. ' }',
  23897. '});',
  23898. '$mod.THelper.Run();',
  23899. '']));
  23900. end;
  23901. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23902. begin
  23903. StartProgram(false);
  23904. Add([
  23905. '{$modeswitch typehelpers}',
  23906. 'type',
  23907. ' TObject = class',
  23908. ' FField: word;',
  23909. ' function GetField: word;',
  23910. ' property Field: word read GetField write FField;',
  23911. ' end;',
  23912. ' THelper = type helper for word',
  23913. ' procedure Fly;',
  23914. ' class procedure Run; static;',
  23915. ' end;',
  23916. 'function TObject.GetField: word;',
  23917. 'begin',
  23918. ' Field.Fly;',
  23919. ' Field.Run;',
  23920. ' Self.Field.Fly;',
  23921. ' Self.Field.Run;',
  23922. ' with Self do begin',
  23923. ' Field.Fly;',
  23924. ' Field.Run;',
  23925. ' end;',
  23926. ' with Self.Field do begin',
  23927. ' Fly;',
  23928. ' Run;',
  23929. ' end;',
  23930. 'end;',
  23931. 'procedure THelper.Fly;',
  23932. 'begin',
  23933. 'end;',
  23934. 'class procedure THelper.Run;',
  23935. 'begin',
  23936. 'end;',
  23937. 'var',
  23938. ' o: TObject;',
  23939. 'begin',
  23940. ' o.Field.Fly;',
  23941. ' o.Field.Run;',
  23942. ' with o do begin',
  23943. ' Field.Fly;',
  23944. ' Field.Run;',
  23945. ' end;',
  23946. ' with o.Field do begin',
  23947. ' Fly;',
  23948. ' Run;',
  23949. ' end;',
  23950. '']);
  23951. ConvertProgram;
  23952. CheckSource('TestTypeHelper_PassPropertyGetter',
  23953. LinesToStr([ // statements
  23954. 'rtl.createClass(this, "TObject", null, function () {',
  23955. ' this.$init = function () {',
  23956. ' this.FField = 0;',
  23957. ' };',
  23958. ' this.$final = function () {',
  23959. ' };',
  23960. ' this.GetField = function () {',
  23961. ' var Result = 0;',
  23962. ' $mod.THelper.Fly.call({',
  23963. ' p: this.GetField(),',
  23964. ' get: function () {',
  23965. ' return this.p;',
  23966. ' },',
  23967. ' set: function (v) {',
  23968. ' this.p = v;',
  23969. ' }',
  23970. ' });',
  23971. ' $mod.THelper.Run();',
  23972. ' $mod.THelper.Fly.call({',
  23973. ' p: this.GetField(),',
  23974. ' get: function () {',
  23975. ' return this.p;',
  23976. ' },',
  23977. ' set: function (v) {',
  23978. ' this.p = v;',
  23979. ' }',
  23980. ' });',
  23981. ' $mod.THelper.Run();',
  23982. ' $mod.THelper.Fly.call({',
  23983. ' p: this.GetField(),',
  23984. ' get: function () {',
  23985. ' return this.p;',
  23986. ' },',
  23987. ' set: function (v) {',
  23988. ' this.p = v;',
  23989. ' }',
  23990. ' });',
  23991. ' $mod.THelper.Run();',
  23992. ' var $with = this.GetField();',
  23993. ' $mod.THelper.Fly.call({',
  23994. ' get: function () {',
  23995. ' return $with;',
  23996. ' },',
  23997. ' set: function (v) {',
  23998. ' $with = v;',
  23999. ' }',
  24000. ' });',
  24001. ' $mod.THelper.Run();',
  24002. ' return Result;',
  24003. ' };',
  24004. '});',
  24005. 'rtl.createHelper(this, "THelper", null, function () {',
  24006. ' this.Fly = function () {',
  24007. ' };',
  24008. ' this.Run = function () {',
  24009. ' };',
  24010. '});',
  24011. 'this.o = null;',
  24012. '']),
  24013. LinesToStr([ // $mod.$main
  24014. '$mod.THelper.Fly.call({',
  24015. ' p: $mod.o.GetField(),',
  24016. ' get: function () {',
  24017. ' return this.p;',
  24018. ' },',
  24019. ' set: function (v) {',
  24020. ' this.p = v;',
  24021. ' }',
  24022. '});',
  24023. '$mod.THelper.Run();',
  24024. 'var $with = $mod.o;',
  24025. '$mod.THelper.Fly.call({',
  24026. ' p: $with.GetField(),',
  24027. ' get: function () {',
  24028. ' return this.p;',
  24029. ' },',
  24030. ' set: function (v) {',
  24031. ' this.p = v;',
  24032. ' }',
  24033. '});',
  24034. '$mod.THelper.Run();',
  24035. 'var $with1 = $mod.o.GetField();',
  24036. '$mod.THelper.Fly.call({',
  24037. ' get: function () {',
  24038. ' return $with1;',
  24039. ' },',
  24040. ' set: function (v) {',
  24041. ' $with1 = v;',
  24042. ' }',
  24043. '});',
  24044. '$mod.THelper.Run();',
  24045. '']));
  24046. end;
  24047. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  24048. begin
  24049. StartProgram(false);
  24050. Add([
  24051. '{$modeswitch typehelpers}',
  24052. 'type',
  24053. ' TObject = class',
  24054. ' class var FField: word;',
  24055. ' class procedure SetField(Value: word);',
  24056. ' class property Field: word read FField write SetField;',
  24057. ' end;',
  24058. ' THelper = type helper for word',
  24059. ' procedure Fly(n: byte);',
  24060. ' end;',
  24061. 'class procedure TObject.SetField(Value: word);',
  24062. 'begin',
  24063. ' Field.Fly(1);',
  24064. ' Self.Field.Fly(2);',
  24065. ' with Self do Field.Fly(3);',
  24066. ' with Self.Field do Fly(4);',
  24067. ' TObject.Field.Fly(5);',
  24068. ' with TObject do Field.Fly(6);',
  24069. ' with TObject.Field do Fly(7);',
  24070. 'end;',
  24071. 'procedure THelper.Fly(n: byte);',
  24072. 'begin',
  24073. 'end;',
  24074. 'var',
  24075. ' o: TObject;',
  24076. 'begin',
  24077. ' o.Field.Fly(11);',
  24078. ' with o do Field.Fly(12);',
  24079. ' with o.Field do Fly(13);',
  24080. ' TObject.Field.Fly(14);',
  24081. ' with TObject do Field.Fly(15);',
  24082. ' with TObject.Field do Fly(16);',
  24083. '']);
  24084. ConvertProgram;
  24085. CheckSource('TestTypeHelper_PassClassPropertyField',
  24086. LinesToStr([ // statements
  24087. 'rtl.createClass(this, "TObject", null, function () {',
  24088. ' this.FField = 0;',
  24089. ' this.$init = function () {',
  24090. ' };',
  24091. ' this.$final = function () {',
  24092. ' };',
  24093. ' this.SetField = function (Value) {',
  24094. ' $mod.THelper.Fly.call({',
  24095. ' p: this,',
  24096. ' get: function () {',
  24097. ' return this.p.FField;',
  24098. ' },',
  24099. ' set: function (v) {',
  24100. ' $mod.TObject.FField = v;',
  24101. ' }',
  24102. ' }, 1);',
  24103. ' $mod.THelper.Fly.call({',
  24104. ' p: this,',
  24105. ' get: function () {',
  24106. ' return this.p.FField;',
  24107. ' },',
  24108. ' set: function (v) {',
  24109. ' $mod.TObject.FField = v;',
  24110. ' }',
  24111. ' }, 2);',
  24112. ' $mod.THelper.Fly.call({',
  24113. ' p: this,',
  24114. ' get: function () {',
  24115. ' return this.p.FField;',
  24116. ' },',
  24117. ' set: function (v) {',
  24118. ' $mod.TObject.FField = v;',
  24119. ' }',
  24120. ' }, 3);',
  24121. ' var $with = this.FField;',
  24122. ' $mod.THelper.Fly.call({',
  24123. ' get: function () {',
  24124. ' return $with;',
  24125. ' },',
  24126. ' set: function (v) {',
  24127. ' $with = v;',
  24128. ' }',
  24129. ' }, 4);',
  24130. ' $mod.THelper.Fly.call({',
  24131. ' p: $mod.TObject,',
  24132. ' get: function () {',
  24133. ' return this.p.FField;',
  24134. ' },',
  24135. ' set: function (v) {',
  24136. ' $mod.TObject.FField = v;',
  24137. ' }',
  24138. ' }, 5);',
  24139. ' var $with1 = $mod.TObject;',
  24140. ' $mod.THelper.Fly.call({',
  24141. ' p: $with1,',
  24142. ' get: function () {',
  24143. ' return this.p.FField;',
  24144. ' },',
  24145. ' set: function (v) {',
  24146. ' $mod.TObject.FField = v;',
  24147. ' }',
  24148. ' }, 6);',
  24149. ' var $with2 = $mod.TObject.FField;',
  24150. ' $mod.THelper.Fly.call({',
  24151. ' get: function () {',
  24152. ' return $with2;',
  24153. ' },',
  24154. ' set: function (v) {',
  24155. ' $with2 = v;',
  24156. ' }',
  24157. ' }, 7);',
  24158. ' };',
  24159. '});',
  24160. 'rtl.createHelper(this, "THelper", null, function () {',
  24161. ' this.Fly = function (n) {',
  24162. ' };',
  24163. '});',
  24164. 'this.o = null;',
  24165. '']),
  24166. LinesToStr([ // $mod.$main
  24167. '$mod.THelper.Fly.call({',
  24168. ' p: $mod.o,',
  24169. ' get: function () {',
  24170. ' return this.p.FField;',
  24171. ' },',
  24172. ' set: function (v) {',
  24173. ' $mod.TObject.FField = v;',
  24174. ' }',
  24175. '}, 11);',
  24176. 'var $with = $mod.o;',
  24177. '$mod.THelper.Fly.call({',
  24178. ' p: $with,',
  24179. ' get: function () {',
  24180. ' return this.p.FField;',
  24181. ' },',
  24182. ' set: function (v) {',
  24183. ' $mod.TObject.FField = v;',
  24184. ' }',
  24185. '}, 12);',
  24186. 'var $with1 = $mod.o.FField;',
  24187. '$mod.THelper.Fly.call({',
  24188. ' get: function () {',
  24189. ' return $with1;',
  24190. ' },',
  24191. ' set: function (v) {',
  24192. ' $with1 = v;',
  24193. ' }',
  24194. '}, 13);',
  24195. '$mod.THelper.Fly.call({',
  24196. ' p: $mod.TObject,',
  24197. ' get: function () {',
  24198. ' return this.p.FField;',
  24199. ' },',
  24200. ' set: function (v) {',
  24201. ' $mod.TObject.FField = v;',
  24202. ' }',
  24203. '}, 14);',
  24204. 'var $with2 = $mod.TObject;',
  24205. '$mod.THelper.Fly.call({',
  24206. ' p: $with2,',
  24207. ' get: function () {',
  24208. ' return this.p.FField;',
  24209. ' },',
  24210. ' set: function (v) {',
  24211. ' $mod.TObject.FField = v;',
  24212. ' }',
  24213. '}, 15);',
  24214. 'var $with3 = $mod.TObject.FField;',
  24215. '$mod.THelper.Fly.call({',
  24216. ' get: function () {',
  24217. ' return $with3;',
  24218. ' },',
  24219. ' set: function (v) {',
  24220. ' $with3 = v;',
  24221. ' }',
  24222. '}, 16);',
  24223. '']));
  24224. end;
  24225. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  24226. begin
  24227. StartProgram(false);
  24228. Add([
  24229. '{$modeswitch typehelpers}',
  24230. 'type',
  24231. ' TObject = class',
  24232. ' class var FField: word;',
  24233. ' class function GetField: word; static;',
  24234. ' class property Field: word read GetField write FField;',
  24235. ' end;',
  24236. ' THelper = type helper for word',
  24237. ' procedure Fly(n: byte);',
  24238. ' end;',
  24239. 'class function TObject.GetField: word;',
  24240. 'begin',
  24241. ' Field.Fly(1);',
  24242. ' TObject.Field.Fly(5);',
  24243. ' with TObject do Field.Fly(6);',
  24244. ' with TObject.Field do Fly(7);',
  24245. 'end;',
  24246. 'procedure THelper.Fly(n: byte);',
  24247. 'begin',
  24248. 'end;',
  24249. 'var',
  24250. ' o: TObject;',
  24251. 'begin',
  24252. ' o.Field.Fly(11);',
  24253. ' with o do Field.Fly(12);',
  24254. ' with o.Field do Fly(13);',
  24255. '']);
  24256. ConvertProgram;
  24257. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  24258. LinesToStr([ // statements
  24259. 'rtl.createClass(this, "TObject", null, function () {',
  24260. ' this.FField = 0;',
  24261. ' this.$init = function () {',
  24262. ' };',
  24263. ' this.$final = function () {',
  24264. ' };',
  24265. ' this.GetField = function () {',
  24266. ' var Result = 0;',
  24267. ' $mod.THelper.Fly.call({',
  24268. ' p: $mod.TObject.GetField(),',
  24269. ' get: function () {',
  24270. ' return this.p;',
  24271. ' },',
  24272. ' set: function (v) {',
  24273. ' this.p = v;',
  24274. ' }',
  24275. ' }, 1);',
  24276. ' $mod.THelper.Fly.call({',
  24277. ' p: $mod.TObject.GetField(),',
  24278. ' get: function () {',
  24279. ' return this.p;',
  24280. ' },',
  24281. ' set: function (v) {',
  24282. ' this.p = v;',
  24283. ' }',
  24284. ' }, 5);',
  24285. ' var $with = $mod.TObject;',
  24286. ' $mod.THelper.Fly.call({',
  24287. ' p: $with.GetField(),',
  24288. ' get: function () {',
  24289. ' return this.p;',
  24290. ' },',
  24291. ' set: function (v) {',
  24292. ' this.p = v;',
  24293. ' }',
  24294. ' }, 6);',
  24295. ' var $with1 = $mod.TObject.GetField();',
  24296. ' $mod.THelper.Fly.call({',
  24297. ' get: function () {',
  24298. ' return $with1;',
  24299. ' },',
  24300. ' set: function (v) {',
  24301. ' $with1 = v;',
  24302. ' }',
  24303. ' }, 7);',
  24304. ' return Result;',
  24305. ' };',
  24306. '});',
  24307. 'rtl.createHelper(this, "THelper", null, function () {',
  24308. ' this.Fly = function (n) {',
  24309. ' };',
  24310. '});',
  24311. 'this.o = null;',
  24312. '']),
  24313. LinesToStr([ // $mod.$main
  24314. '$mod.THelper.Fly.call({',
  24315. ' p: $mod.TObject.GetField(),',
  24316. ' get: function () {',
  24317. ' return this.p;',
  24318. ' },',
  24319. ' set: function (v) {',
  24320. ' this.p = v;',
  24321. ' }',
  24322. '}, 11);',
  24323. 'var $with = $mod.o;',
  24324. '$mod.THelper.Fly.call({',
  24325. ' p: $with.GetField(),',
  24326. ' get: function () {',
  24327. ' return this.p;',
  24328. ' },',
  24329. ' set: function (v) {',
  24330. ' this.p = v;',
  24331. ' }',
  24332. '}, 12);',
  24333. 'var $with1 = $mod.TObject.GetField();',
  24334. '$mod.THelper.Fly.call({',
  24335. ' get: function () {',
  24336. ' return $with1;',
  24337. ' },',
  24338. ' set: function (v) {',
  24339. ' $with1 = v;',
  24340. ' }',
  24341. '}, 13);',
  24342. '']));
  24343. end;
  24344. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  24345. begin
  24346. StartProgram(false);
  24347. Add([
  24348. '{$modeswitch typehelpers}',
  24349. 'type',
  24350. ' TObject = class',
  24351. ' class var FField: word;',
  24352. ' class function GetField: word;',
  24353. ' class property Field: word read GetField write FField;',
  24354. ' end;',
  24355. ' TClass = class of TObject;',
  24356. ' THelper = type helper for word',
  24357. ' procedure Fly(n: byte);',
  24358. ' end;',
  24359. 'class function TObject.GetField: word;',
  24360. 'begin',
  24361. ' Field.Fly(1);',
  24362. ' Self.Field.Fly(5);',
  24363. ' with Self do Field.Fly(6);',
  24364. ' with Self.Field do Fly(7);',
  24365. 'end;',
  24366. 'procedure THelper.Fly(n: byte);',
  24367. 'begin',
  24368. 'end;',
  24369. 'var',
  24370. ' o: TObject;',
  24371. ' c: TClass;',
  24372. 'begin',
  24373. ' o.Field.Fly(11);',
  24374. ' with o do Field.Fly(12);',
  24375. ' with o.Field do Fly(13);',
  24376. ' c.Field.Fly(14);',
  24377. ' with c do Field.Fly(15);',
  24378. ' with c.Field do Fly(16);',
  24379. '']);
  24380. ConvertProgram;
  24381. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  24382. LinesToStr([ // statements
  24383. 'rtl.createClass(this, "TObject", null, function () {',
  24384. ' this.FField = 0;',
  24385. ' this.$init = function () {',
  24386. ' };',
  24387. ' this.$final = function () {',
  24388. ' };',
  24389. ' this.GetField = function () {',
  24390. ' var Result = 0;',
  24391. ' $mod.THelper.Fly.call({',
  24392. ' p: this.GetField(),',
  24393. ' get: function () {',
  24394. ' return this.p;',
  24395. ' },',
  24396. ' set: function (v) {',
  24397. ' this.p = v;',
  24398. ' }',
  24399. ' }, 1);',
  24400. ' $mod.THelper.Fly.call({',
  24401. ' p: this.GetField(),',
  24402. ' get: function () {',
  24403. ' return this.p;',
  24404. ' },',
  24405. ' set: function (v) {',
  24406. ' this.p = v;',
  24407. ' }',
  24408. ' }, 5);',
  24409. ' $mod.THelper.Fly.call({',
  24410. ' p: this.GetField(),',
  24411. ' get: function () {',
  24412. ' return this.p;',
  24413. ' },',
  24414. ' set: function (v) {',
  24415. ' this.p = v;',
  24416. ' }',
  24417. ' }, 6);',
  24418. ' var $with = this.GetField();',
  24419. ' $mod.THelper.Fly.call({',
  24420. ' get: function () {',
  24421. ' return $with;',
  24422. ' },',
  24423. ' set: function (v) {',
  24424. ' $with = v;',
  24425. ' }',
  24426. ' }, 7);',
  24427. ' return Result;',
  24428. ' };',
  24429. '});',
  24430. 'rtl.createHelper(this, "THelper", null, function () {',
  24431. ' this.Fly = function (n) {',
  24432. ' };',
  24433. '});',
  24434. 'this.o = null;',
  24435. 'this.c = null;',
  24436. '']),
  24437. LinesToStr([ // $mod.$main
  24438. '$mod.THelper.Fly.call({',
  24439. ' p: $mod.o.$class.GetField(),',
  24440. ' get: function () {',
  24441. ' return this.p;',
  24442. ' },',
  24443. ' set: function (v) {',
  24444. ' this.p = v;',
  24445. ' }',
  24446. '}, 11);',
  24447. 'var $with = $mod.o;',
  24448. '$mod.THelper.Fly.call({',
  24449. ' p: $with.$class.GetField(),',
  24450. ' get: function () {',
  24451. ' return this.p;',
  24452. ' },',
  24453. ' set: function (v) {',
  24454. ' this.p = v;',
  24455. ' }',
  24456. '}, 12);',
  24457. 'var $with1 = $mod.o.$class.GetField();',
  24458. '$mod.THelper.Fly.call({',
  24459. ' get: function () {',
  24460. ' return $with1;',
  24461. ' },',
  24462. ' set: function (v) {',
  24463. ' $with1 = v;',
  24464. ' }',
  24465. '}, 13);',
  24466. '$mod.THelper.Fly.call({',
  24467. ' p: $mod.c.GetField(),',
  24468. ' get: function () {',
  24469. ' return this.p;',
  24470. ' },',
  24471. ' set: function (v) {',
  24472. ' this.p = v;',
  24473. ' }',
  24474. '}, 14);',
  24475. 'var $with2 = $mod.c;',
  24476. '$mod.THelper.Fly.call({',
  24477. ' p: $with2.GetField(),',
  24478. ' get: function () {',
  24479. ' return this.p;',
  24480. ' },',
  24481. ' set: function (v) {',
  24482. ' this.p = v;',
  24483. ' }',
  24484. '}, 15);',
  24485. 'var $with3 = $mod.c.GetField();',
  24486. '$mod.THelper.Fly.call({',
  24487. ' get: function () {',
  24488. ' return $with3;',
  24489. ' },',
  24490. ' set: function (v) {',
  24491. ' $with3 = v;',
  24492. ' }',
  24493. '}, 16);',
  24494. '']));
  24495. end;
  24496. procedure TTestModule.TestTypeHelper_Property;
  24497. begin
  24498. StartProgram(false);
  24499. Add([
  24500. '{$modeswitch typehelpers}',
  24501. 'type',
  24502. ' THelper = type helper for word',
  24503. ' function GetSize: longint;',
  24504. ' procedure SetSize(Value: longint);',
  24505. ' property Size: longint read GetSize write SetSize;',
  24506. ' end;',
  24507. 'function THelper.GetSize: longint;',
  24508. 'begin',
  24509. ' Result:=Size+1;',
  24510. ' Size:=2;',
  24511. ' Result:=Self.Size+3;',
  24512. ' Self.Size:=4;',
  24513. ' with Self do begin',
  24514. ' Result:=Size+5;',
  24515. ' Size:=6;',
  24516. ' end;',
  24517. 'end;',
  24518. 'procedure THelper.SetSize(Value: longint);',
  24519. 'begin',
  24520. 'end;',
  24521. 'var w: word;',
  24522. 'begin',
  24523. ' w:=w.Size+7;',
  24524. ' w.Size:=w+8;',
  24525. ' with w do begin',
  24526. ' w:=Size+9;',
  24527. ' Size:=w+10;',
  24528. ' end;',
  24529. '']);
  24530. ConvertProgram;
  24531. CheckSource('TestTypeHelper_Property',
  24532. LinesToStr([ // statements
  24533. 'rtl.createHelper(this, "THelper", null, function () {',
  24534. ' this.GetSize = function () {',
  24535. ' var Result = 0;',
  24536. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24537. ' $mod.THelper.SetSize.call(this, 2);',
  24538. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24539. ' $mod.THelper.SetSize.call(this, 4);',
  24540. ' var $with = this.get();',
  24541. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24542. ' $mod.THelper.SetSize.call(this, 6);',
  24543. ' return Result;',
  24544. ' };',
  24545. ' this.SetSize = function (Value) {',
  24546. ' };',
  24547. '});',
  24548. 'this.w = 0;',
  24549. '']),
  24550. LinesToStr([ // $mod.$main
  24551. '$mod.w = $mod.THelper.GetSize.call({',
  24552. ' p: $mod,',
  24553. ' get: function () {',
  24554. ' return this.p.w;',
  24555. ' },',
  24556. ' set: function (v) {',
  24557. ' this.p.w = v;',
  24558. ' }',
  24559. '}) + 7;',
  24560. '$mod.THelper.SetSize.call({',
  24561. ' p: $mod,',
  24562. ' get: function () {',
  24563. ' return this.p.w;',
  24564. ' },',
  24565. ' set: function (v) {',
  24566. ' this.p.w = v;',
  24567. ' }',
  24568. '}, $mod.w + 8);',
  24569. 'var $with = $mod.w;',
  24570. '$mod.w = $mod.THelper.GetSize.call({',
  24571. ' get: function () {',
  24572. ' return $with;',
  24573. ' },',
  24574. ' set: function (v) {',
  24575. ' $with = v;',
  24576. ' }',
  24577. '}) + 9;',
  24578. '$mod.THelper.SetSize.call({',
  24579. ' get: function () {',
  24580. ' return $with;',
  24581. ' },',
  24582. ' set: function (v) {',
  24583. ' $with = v;',
  24584. ' }',
  24585. '}, $mod.w + 10);',
  24586. '']));
  24587. end;
  24588. procedure TTestModule.TestTypeHelper_Property_Array;
  24589. begin
  24590. StartProgram(false);
  24591. Add([
  24592. '{$modeswitch typehelpers}',
  24593. 'type',
  24594. ' THelper = type helper for word',
  24595. ' function GetItems(Index: byte): boolean;',
  24596. ' procedure SetItems(Index: byte; Value: boolean);',
  24597. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24598. ' end;',
  24599. 'function THelper.GetItems(Index: byte): boolean;',
  24600. 'begin',
  24601. ' Result:=Items[1];',
  24602. ' Items[2]:=false;',
  24603. ' Result:=Self.Items[3];',
  24604. ' Self.Items[4]:=true;',
  24605. ' with Self do begin',
  24606. ' Result:=Items[5];',
  24607. ' Items[6]:=false;',
  24608. ' end;',
  24609. 'end;',
  24610. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24611. 'begin',
  24612. 'end;',
  24613. 'var',
  24614. ' w: word;',
  24615. ' b: boolean;',
  24616. 'begin',
  24617. ' b:=w.Items[1];',
  24618. ' w.Items[2]:=b;',
  24619. ' with w do begin',
  24620. ' b:=Items[3];',
  24621. ' Items[4]:=b;',
  24622. ' end;',
  24623. '']);
  24624. ConvertProgram;
  24625. CheckSource('TestTypeHelper_Property_Array',
  24626. LinesToStr([ // statements
  24627. 'rtl.createHelper(this, "THelper", null, function () {',
  24628. ' this.GetItems = function (Index) {',
  24629. ' var Result = false;',
  24630. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24631. ' $mod.THelper.SetItems.call(this, 2, false);',
  24632. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24633. ' $mod.THelper.SetItems.call(this, 4, true);',
  24634. ' var $with = this.get();',
  24635. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24636. ' $mod.THelper.SetItems.call(this, 6, false);',
  24637. ' return Result;',
  24638. ' };',
  24639. ' this.SetItems = function (Index, Value) {',
  24640. ' };',
  24641. '});',
  24642. 'this.w = 0;',
  24643. 'this.b = false;',
  24644. '']),
  24645. LinesToStr([ // $mod.$main
  24646. '$mod.b = $mod.THelper.GetItems.call({',
  24647. ' p: $mod,',
  24648. ' get: function () {',
  24649. ' return this.p.w;',
  24650. ' },',
  24651. ' set: function (v) {',
  24652. ' this.p.w = v;',
  24653. ' }',
  24654. '}, 1);',
  24655. '$mod.THelper.SetItems.call({',
  24656. ' p: $mod,',
  24657. ' get: function () {',
  24658. ' return this.p.w;',
  24659. ' },',
  24660. ' set: function (v) {',
  24661. ' this.p.w = v;',
  24662. ' }',
  24663. '}, 2, $mod.b);',
  24664. 'var $with = $mod.w;',
  24665. '$mod.b = $mod.THelper.GetItems.call({',
  24666. ' get: function () {',
  24667. ' return $with;',
  24668. ' },',
  24669. ' set: function (v) {',
  24670. ' $with = v;',
  24671. ' }',
  24672. '}, 3);',
  24673. '$mod.THelper.SetItems.call({',
  24674. ' get: function () {',
  24675. ' return $with;',
  24676. ' },',
  24677. ' set: function (v) {',
  24678. ' $with = v;',
  24679. ' }',
  24680. '}, 4, $mod.b);',
  24681. '']));
  24682. end;
  24683. procedure TTestModule.TestTypeHelper_ClassProperty;
  24684. begin
  24685. StartProgram(false);
  24686. Add([
  24687. '{$modeswitch typehelpers}',
  24688. 'type',
  24689. ' THelper = type helper for word',
  24690. ' class function GetSize: longint; static;',
  24691. ' class procedure SetSize(Value: longint); static;',
  24692. ' class property Size: longint read GetSize write SetSize;',
  24693. ' end;',
  24694. 'class function THelper.GetSize: longint;',
  24695. 'begin',
  24696. ' Result:=Size+1;',
  24697. ' Size:=2;',
  24698. 'end;',
  24699. 'class procedure THelper.SetSize(Value: longint);',
  24700. 'begin',
  24701. 'end;',
  24702. 'begin',
  24703. '']);
  24704. ConvertProgram;
  24705. CheckSource('TestTypeHelper_ClassProperty',
  24706. LinesToStr([ // statements
  24707. 'rtl.createHelper(this, "THelper", null, function () {',
  24708. ' this.GetSize = function () {',
  24709. ' var Result = 0;',
  24710. ' Result = $mod.THelper.GetSize() + 1;',
  24711. ' $mod.THelper.SetSize(2);',
  24712. ' return Result;',
  24713. ' };',
  24714. ' this.SetSize = function (Value) {',
  24715. ' };',
  24716. '});',
  24717. '']),
  24718. LinesToStr([ // $mod.$main
  24719. '']));
  24720. end;
  24721. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24722. begin
  24723. StartProgram(false);
  24724. Add([
  24725. '{$modeswitch typehelpers}',
  24726. 'type',
  24727. ' THelper = type helper for word',
  24728. ' class function GetItems(Index: byte): boolean; static;',
  24729. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24730. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24731. ' end;',
  24732. 'class function THelper.GetItems(Index: byte): boolean;',
  24733. 'begin',
  24734. ' Result:=Items[1];',
  24735. ' Items[2]:=false;',
  24736. 'end;',
  24737. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24738. 'begin',
  24739. 'end;',
  24740. 'var',
  24741. ' w: word;',
  24742. ' b: boolean;',
  24743. 'begin',
  24744. ' b:=w.Items[1];',
  24745. ' w.Items[2]:=b;',
  24746. ' with w do begin',
  24747. ' b:=Items[3];',
  24748. ' Items[4]:=b;',
  24749. ' end;',
  24750. '']);
  24751. ConvertProgram;
  24752. CheckSource('TestTypeHelper_ClassProperty_Array',
  24753. LinesToStr([ // statements
  24754. 'rtl.createHelper(this, "THelper", null, function () {',
  24755. ' this.GetItems = function (Index) {',
  24756. ' var Result = false;',
  24757. ' Result = $mod.THelper.GetItems(1);',
  24758. ' $mod.THelper.SetItems(2, false);',
  24759. ' return Result;',
  24760. ' };',
  24761. ' this.SetItems = function (Index, Value) {',
  24762. ' };',
  24763. '});',
  24764. 'this.w = 0;',
  24765. 'this.b = false;',
  24766. '']),
  24767. LinesToStr([ // $mod.$main
  24768. '$mod.b = $mod.THelper.GetItems(1);',
  24769. '$mod.THelper.SetItems(2, $mod.b);',
  24770. 'var $with = $mod.w;',
  24771. '$mod.b = $mod.THelper.GetItems(3);',
  24772. '$mod.THelper.SetItems(4, $mod.b);',
  24773. '']));
  24774. end;
  24775. procedure TTestModule.TestTypeHelper_ClassMethod;
  24776. begin
  24777. StartProgram(false);
  24778. Add([
  24779. '{$modeswitch typehelpers}',
  24780. 'type',
  24781. ' THelper = type helper for word',
  24782. ' class procedure DoStatic; static;',
  24783. ' end;',
  24784. 'class procedure THelper.DoStatic;',
  24785. 'begin',
  24786. ' DoStatic;',
  24787. ' DoStatic();',
  24788. 'end;',
  24789. 'var w: word;',
  24790. 'begin',
  24791. ' w.DoStatic;',
  24792. ' w.DoStatic();',
  24793. '']);
  24794. ConvertProgram;
  24795. CheckSource('TestTypeHelper_ClassMethod',
  24796. LinesToStr([ // statements
  24797. 'rtl.createHelper(this, "THelper", null, function () {',
  24798. ' this.DoStatic = function () {',
  24799. ' $mod.THelper.DoStatic();',
  24800. ' $mod.THelper.DoStatic();',
  24801. ' };',
  24802. '});',
  24803. 'this.w = 0;',
  24804. '']),
  24805. LinesToStr([ // $mod.$main
  24806. '$mod.THelper.DoStatic();',
  24807. '$mod.THelper.DoStatic();',
  24808. '']));
  24809. end;
  24810. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24811. begin
  24812. StartProgram(false);
  24813. Add([
  24814. '{$modeswitch typehelpers}',
  24815. 'type',
  24816. ' THelper = type helper for word',
  24817. ' procedure Run; external name ''Run'';',
  24818. ' end;',
  24819. 'var w: word;',
  24820. 'begin',
  24821. ' w.Run;',
  24822. '']);
  24823. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24824. ConvertProgram;
  24825. end;
  24826. procedure TTestModule.TestTypeHelper_Constructor;
  24827. begin
  24828. StartProgram(false);
  24829. Add([
  24830. '{$modeswitch typehelpers}',
  24831. 'type',
  24832. ' THelper = type helper for word',
  24833. ' constructor Init(e: longint);',
  24834. ' end;',
  24835. 'constructor THelper.Init(e: longint);',
  24836. 'begin',
  24837. ' Self:=e;',
  24838. ' Init(e+1);',
  24839. 'end;',
  24840. 'var w: word;',
  24841. 'begin',
  24842. ' w:=word.Init(2);',
  24843. ' w:=w.Init(3);',
  24844. ' with word do w:=Init(4);',
  24845. ' with w do w:=Init(5);',
  24846. '']);
  24847. ConvertProgram;
  24848. CheckSource('TestTypeHelper_Constructor',
  24849. LinesToStr([ // statements
  24850. 'rtl.createHelper(this, "THelper", null, function () {',
  24851. ' this.Init = function (e) {',
  24852. ' this.set(e);',
  24853. ' $mod.THelper.Init.call(this, e + 1);',
  24854. ' return this.get();',
  24855. ' };',
  24856. ' this.$new = function (fn, args) {',
  24857. ' return this[fn].apply({',
  24858. ' p: 0,',
  24859. ' get: function () {',
  24860. ' return this.p;',
  24861. ' },',
  24862. ' set: function (v) {',
  24863. ' this.p = v;',
  24864. ' }',
  24865. ' }, args);',
  24866. ' };',
  24867. '});',
  24868. 'this.w = 0;',
  24869. '']),
  24870. LinesToStr([ // $mod.$main
  24871. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24872. '$mod.w = $mod.THelper.Init.call({',
  24873. ' p: $mod,',
  24874. ' get: function () {',
  24875. ' return this.p.w;',
  24876. ' },',
  24877. ' set: function (v) {',
  24878. ' this.p.w = v;',
  24879. ' }',
  24880. '}, 3);',
  24881. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24882. 'var $with = $mod.w;',
  24883. '$mod.w = $mod.THelper.Init.call({',
  24884. ' get: function () {',
  24885. ' return $with;',
  24886. ' },',
  24887. ' set: function (v) {',
  24888. ' $with = v;',
  24889. ' }',
  24890. '}, 5);',
  24891. '']));
  24892. end;
  24893. procedure TTestModule.TestTypeHelper_Word;
  24894. begin
  24895. StartProgram(false);
  24896. Add([
  24897. '{$modeswitch typehelpers}',
  24898. 'type',
  24899. ' THelper = type helper for word',
  24900. ' procedure DoIt(e: byte = 123);',
  24901. ' end;',
  24902. 'procedure THelper.DoIt(e: byte);',
  24903. 'begin',
  24904. ' Self:=e;',
  24905. ' Self:=Self+1;',
  24906. ' with Self do Doit;',
  24907. 'end;',
  24908. 'begin',
  24909. ' word(3).DoIt;',
  24910. '']);
  24911. ConvertProgram;
  24912. CheckSource('TestTypeHelper_Word',
  24913. LinesToStr([ // statements
  24914. 'rtl.createHelper(this, "THelper", null, function () {',
  24915. ' this.DoIt = function (e) {',
  24916. ' this.set(e);',
  24917. ' this.set(this.get() + 1);',
  24918. ' var $with = this.get();',
  24919. ' $mod.THelper.DoIt.call(this, 123);',
  24920. ' };',
  24921. '});',
  24922. '']),
  24923. LinesToStr([ // $mod.$main
  24924. '$mod.THelper.DoIt.call({',
  24925. ' get: function () {',
  24926. ' return 3;',
  24927. ' },',
  24928. ' set: function (v) {',
  24929. ' rtl.raiseE("EPropReadOnly");',
  24930. ' }',
  24931. '}, 123);',
  24932. '']));
  24933. end;
  24934. procedure TTestModule.TestTypeHelper_Boolean;
  24935. begin
  24936. StartProgram(false);
  24937. Add([
  24938. '{$modeswitch typehelpers}',
  24939. 'type',
  24940. ' Integer = longint;',
  24941. ' THelper = type helper for boolean',
  24942. ' procedure Run(e: wordbool = true);',
  24943. ' end;',
  24944. 'procedure THelper.Run(e: wordbool);',
  24945. 'begin',
  24946. ' Self:=e;',
  24947. ' Self:=not Self;',
  24948. ' with Self do Run;',
  24949. ' if Integer(Self)=0 then ;',
  24950. 'end;',
  24951. 'begin',
  24952. ' boolean(3).Run;',
  24953. '']);
  24954. ConvertProgram;
  24955. CheckSource('TestTypeHelper_Boolean',
  24956. LinesToStr([ // statements
  24957. 'rtl.createHelper(this, "THelper", null, function () {',
  24958. ' this.Run = function (e) {',
  24959. ' this.set(e);',
  24960. ' this.set(!this.get());',
  24961. ' var $with = this.get();',
  24962. ' $mod.THelper.Run.call(this, true);',
  24963. ' if ((this.get() ? 1 : 0) === 0) ;',
  24964. ' };',
  24965. '});',
  24966. '']),
  24967. LinesToStr([ // $mod.$main
  24968. '$mod.THelper.Run.call({',
  24969. ' a: 3 != 0,',
  24970. ' get: function () {',
  24971. ' return this.a;',
  24972. ' },',
  24973. ' set: function (v) {',
  24974. ' rtl.raiseE("EPropReadOnly");',
  24975. ' }',
  24976. '}, true);',
  24977. '']));
  24978. end;
  24979. procedure TTestModule.TestTypeHelper_WordBool;
  24980. begin
  24981. StartProgram(false);
  24982. Add([
  24983. '{$modeswitch typehelpers}',
  24984. 'type',
  24985. ' Integer = longint;',
  24986. ' THelper = type helper for WordBool',
  24987. ' procedure Run(e: wordbool = true);',
  24988. ' end;',
  24989. 'procedure THelper.Run(e: wordbool);',
  24990. 'var i: integer;',
  24991. 'begin',
  24992. ' i:=Integer(Self);',
  24993. 'end;',
  24994. 'var w: wordbool;',
  24995. 'begin',
  24996. ' w.Run;',
  24997. ' wordbool(3).Run;',
  24998. '']);
  24999. ConvertProgram;
  25000. CheckSource('TestTypeHelper_WordBool',
  25001. LinesToStr([ // statements
  25002. 'rtl.createHelper(this, "THelper", null, function () {',
  25003. ' this.Run = function (e) {',
  25004. ' var i = 0;',
  25005. ' i = (this.get() ? 1 : 0);',
  25006. ' };',
  25007. '});',
  25008. 'this.w = false;',
  25009. '']),
  25010. LinesToStr([ // $mod.$main
  25011. '$mod.THelper.Run.call({',
  25012. ' p: $mod,',
  25013. ' get: function () {',
  25014. ' return this.p.w;',
  25015. ' },',
  25016. ' set: function (v) {',
  25017. ' this.p.w = v;',
  25018. ' }',
  25019. '}, true);',
  25020. '$mod.THelper.Run.call({',
  25021. ' a: 3 != 0,',
  25022. ' get: function () {',
  25023. ' return this.a;',
  25024. ' },',
  25025. ' set: function (v) {',
  25026. ' rtl.raiseE("EPropReadOnly");',
  25027. ' }',
  25028. '}, true);',
  25029. '']));
  25030. end;
  25031. procedure TTestModule.TestTypeHelper_Double;
  25032. begin
  25033. StartProgram(false);
  25034. Add([
  25035. '{$modeswitch typehelpers}',
  25036. 'type',
  25037. ' Float = type double;',
  25038. ' THelper = type helper for Float',
  25039. ' const NPI = 3.141592;',
  25040. ' function ToStr: String;',
  25041. ' end;',
  25042. 'function THelper.ToStr: String;',
  25043. 'begin',
  25044. 'end;',
  25045. 'procedure DoIt(s: string);',
  25046. 'begin',
  25047. 'end;',
  25048. 'var f: Float;',
  25049. 'begin',
  25050. ' DoIt(f.toStr);',
  25051. ' DoIt(f.toStr());',
  25052. ' (f*f).toStr;',
  25053. ' DoIt((f*f).toStr);',
  25054. '']);
  25055. ConvertProgram;
  25056. CheckSource('TestTypeHelper_Double',
  25057. LinesToStr([ // statements
  25058. 'rtl.createHelper(this, "THelper", null, function () {',
  25059. ' this.NPI = 3.141592;',
  25060. ' this.ToStr = function () {',
  25061. ' var Result = "";',
  25062. ' return Result;',
  25063. ' };',
  25064. '});',
  25065. 'this.DoIt = function (s) {',
  25066. '};',
  25067. 'this.f = 0.0;',
  25068. '']),
  25069. LinesToStr([ // $mod.$main
  25070. '$mod.DoIt($mod.THelper.ToStr.call({',
  25071. ' p: $mod,',
  25072. ' get: function () {',
  25073. ' return this.p.f;',
  25074. ' },',
  25075. ' set: function (v) {',
  25076. ' this.p.f = v;',
  25077. ' }',
  25078. '}));',
  25079. '$mod.DoIt($mod.THelper.ToStr.call({',
  25080. ' p: $mod,',
  25081. ' get: function () {',
  25082. ' return this.p.f;',
  25083. ' },',
  25084. ' set: function (v) {',
  25085. ' this.p.f = v;',
  25086. ' }',
  25087. '}));',
  25088. '$mod.THelper.ToStr.call({',
  25089. ' a: $mod.f * $mod.f,',
  25090. ' get: function () {',
  25091. ' return this.a;',
  25092. ' },',
  25093. ' set: function (v) {',
  25094. ' rtl.raiseE("EPropReadOnly");',
  25095. ' }',
  25096. '});',
  25097. '$mod.DoIt($mod.THelper.ToStr.call({',
  25098. ' a: $mod.f * $mod.f,',
  25099. ' get: function () {',
  25100. ' return this.a;',
  25101. ' },',
  25102. ' set: function (v) {',
  25103. ' rtl.raiseE("EPropReadOnly");',
  25104. ' }',
  25105. '}));',
  25106. '']));
  25107. end;
  25108. procedure TTestModule.TestTypeHelper_NativeInt;
  25109. begin
  25110. StartProgram(false);
  25111. Add([
  25112. '{$modeswitch typehelpers}',
  25113. 'type',
  25114. ' MaxInt = type nativeint;',
  25115. ' THelperI = type helper for MaxInt',
  25116. ' function ToStr: String;',
  25117. ' end;',
  25118. ' MaxUInt = type nativeuint;',
  25119. ' THelperU = type helper for MaxUInt',
  25120. ' function ToStr: String;',
  25121. ' end;',
  25122. 'function THelperI.ToStr: String;',
  25123. 'begin',
  25124. ' Result:=str(Self);',
  25125. 'end;',
  25126. 'function THelperU.ToStr: String;',
  25127. 'begin',
  25128. ' Result:=str(Self);',
  25129. 'end;',
  25130. 'procedure DoIt(s: string);',
  25131. 'begin',
  25132. 'end;',
  25133. 'var i: MaxInt;',
  25134. 'begin',
  25135. ' DoIt(i.toStr);',
  25136. ' DoIt(i.toStr());',
  25137. ' (i*i).toStr;',
  25138. ' DoIt((i*i).toStr);',
  25139. '']);
  25140. ConvertProgram;
  25141. CheckSource('TestTypeHelper_NativeInt',
  25142. LinesToStr([ // statements
  25143. 'rtl.createHelper(this, "THelperI", null, function () {',
  25144. ' this.ToStr = function () {',
  25145. ' var Result = "";',
  25146. ' Result = "" + this.get();',
  25147. ' return Result;',
  25148. ' };',
  25149. '});',
  25150. 'rtl.createHelper(this, "THelperU", null, function () {',
  25151. ' this.ToStr = function () {',
  25152. ' var Result = "";',
  25153. ' Result = "" + this.get();',
  25154. ' return Result;',
  25155. ' };',
  25156. '});',
  25157. 'this.DoIt = function (s) {',
  25158. '};',
  25159. 'this.i = 0;',
  25160. '']),
  25161. LinesToStr([ // $mod.$main
  25162. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25163. ' p: $mod,',
  25164. ' get: function () {',
  25165. ' return this.p.i;',
  25166. ' },',
  25167. ' set: function (v) {',
  25168. ' this.p.i = v;',
  25169. ' }',
  25170. '}));',
  25171. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25172. ' p: $mod,',
  25173. ' get: function () {',
  25174. ' return this.p.i;',
  25175. ' },',
  25176. ' set: function (v) {',
  25177. ' this.p.i = v;',
  25178. ' }',
  25179. '}));',
  25180. '$mod.THelperI.ToStr.call({',
  25181. ' a: $mod.i * $mod.i,',
  25182. ' get: function () {',
  25183. ' return this.a;',
  25184. ' },',
  25185. ' set: function (v) {',
  25186. ' rtl.raiseE("EPropReadOnly");',
  25187. ' }',
  25188. '});',
  25189. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25190. ' a: $mod.i * $mod.i,',
  25191. ' get: function () {',
  25192. ' return this.a;',
  25193. ' },',
  25194. ' set: function (v) {',
  25195. ' rtl.raiseE("EPropReadOnly");',
  25196. ' }',
  25197. '}));',
  25198. '']));
  25199. end;
  25200. procedure TTestModule.TestTypeHelper_StringChar;
  25201. begin
  25202. StartProgram(false);
  25203. Add([
  25204. '{$modeswitch typehelpers}',
  25205. 'type',
  25206. ' TStringHelper = type helper for string',
  25207. ' procedure DoIt(e: byte = 123);',
  25208. ' end;',
  25209. ' TCharHelper = type helper for char',
  25210. ' procedure Fly;',
  25211. ' end;',
  25212. 'procedure TStringHelper.DoIt(e: byte);',
  25213. 'begin',
  25214. ' Self[1]:=''c'';',
  25215. ' Self[2]:=Self[3];',
  25216. 'end;',
  25217. 'procedure TCharHelper.Fly;',
  25218. 'begin',
  25219. ' Self:=''c'';',
  25220. 'end;',
  25221. 'begin',
  25222. ' ''abc''.DoIt;',
  25223. ' ''xyz''.DoIt();',
  25224. ' ''c''.Fly();',
  25225. '']);
  25226. ConvertProgram;
  25227. CheckSource('TestTypeHelper_StringChar',
  25228. LinesToStr([ // statements
  25229. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  25230. ' this.DoIt = function (e) {',
  25231. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  25232. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  25233. ' };',
  25234. '});',
  25235. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  25236. ' this.Fly = function () {',
  25237. ' this.set("c");',
  25238. ' };',
  25239. '});',
  25240. '']),
  25241. LinesToStr([ // $mod.$main
  25242. '$mod.TStringHelper.DoIt.call({',
  25243. ' get: function () {',
  25244. ' return "abc";',
  25245. ' },',
  25246. ' set: function (v) {',
  25247. ' rtl.raiseE("EPropReadOnly");',
  25248. ' }',
  25249. '}, 123);',
  25250. '$mod.TStringHelper.DoIt.call({',
  25251. ' get: function () {',
  25252. ' return "xyz";',
  25253. ' },',
  25254. ' set: function (v) {',
  25255. ' rtl.raiseE("EPropReadOnly");',
  25256. ' }',
  25257. '}, 123);',
  25258. '$mod.TCharHelper.Fly.call({',
  25259. ' get: function () {',
  25260. ' return "c";',
  25261. ' },',
  25262. ' set: function (v) {',
  25263. ' rtl.raiseE("EPropReadOnly");',
  25264. ' }',
  25265. '});',
  25266. '']));
  25267. end;
  25268. procedure TTestModule.TestTypeHelper_JSValue;
  25269. begin
  25270. StartProgram(false);
  25271. Add([
  25272. '{$modeswitch typehelpers}',
  25273. 'type',
  25274. ' TExtValue = type jsvalue;',
  25275. ' THelper = type helper for TExtValue',
  25276. ' function ToStr: String;',
  25277. ' end;',
  25278. 'function THelper.ToStr: String;',
  25279. 'begin',
  25280. 'end;',
  25281. 'var',
  25282. ' s: string;',
  25283. ' v: TExtValue;',
  25284. 'begin',
  25285. ' s:=v.toStr;',
  25286. ' s:=v.toStr();',
  25287. ' TExtValue(s).toStr;',
  25288. '']);
  25289. ConvertProgram;
  25290. CheckSource('TestTypeHelper_JSValue',
  25291. LinesToStr([ // statements
  25292. 'rtl.createHelper(this, "THelper", null, function () {',
  25293. ' this.ToStr = function () {',
  25294. ' var Result = "";',
  25295. ' return Result;',
  25296. ' };',
  25297. '});',
  25298. 'this.s = "";',
  25299. 'this.v = undefined;',
  25300. '']),
  25301. LinesToStr([ // $mod.$main
  25302. '$mod.s = $mod.THelper.ToStr.call({',
  25303. ' p: $mod,',
  25304. ' get: function () {',
  25305. ' return this.p.v;',
  25306. ' },',
  25307. ' set: function (v) {',
  25308. ' this.p.v = v;',
  25309. ' }',
  25310. '});',
  25311. '$mod.s = $mod.THelper.ToStr.call({',
  25312. ' p: $mod,',
  25313. ' get: function () {',
  25314. ' return this.p.v;',
  25315. ' },',
  25316. ' set: function (v) {',
  25317. ' this.p.v = v;',
  25318. ' }',
  25319. '});',
  25320. '$mod.THelper.ToStr.call({',
  25321. ' p: $mod,',
  25322. ' get: function () {',
  25323. ' return this.p.s;',
  25324. ' },',
  25325. ' set: function (v) {',
  25326. ' rtl.raiseE("EPropReadOnly");',
  25327. ' }',
  25328. '});',
  25329. '']));
  25330. end;
  25331. procedure TTestModule.TestTypeHelper_Array;
  25332. begin
  25333. StartProgram(false);
  25334. Add([
  25335. '{$modeswitch typehelpers}',
  25336. 'type',
  25337. ' TArrOfBool = array of boolean;',
  25338. ' TArrOfJS = array of jsvalue;',
  25339. ' THelper = type helper for TArrOfBool',
  25340. ' procedure DoIt(e: byte = 123);',
  25341. ' end;',
  25342. 'procedure THelper.DoIt(e: byte);',
  25343. 'begin',
  25344. ' Self[1]:=true;',
  25345. ' Self[2]:=not Self[3];',
  25346. ' SetLength(Self,4);',
  25347. 'end;',
  25348. 'var',
  25349. ' b: TArrOfBool;',
  25350. ' j: TArrOfJS;',
  25351. 'begin',
  25352. ' b.DoIt;',
  25353. ' TArrOfBool(j).DoIt();',
  25354. '']);
  25355. ConvertProgram;
  25356. CheckSource('TestTypeHelper_Array',
  25357. LinesToStr([ // statements
  25358. 'rtl.createHelper(this, "THelper", null, function () {',
  25359. ' this.DoIt = function (e) {',
  25360. ' this.get()[1] = true;',
  25361. ' this.get()[2] = !this.get()[3];',
  25362. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  25363. ' };',
  25364. '});',
  25365. 'this.b = [];',
  25366. 'this.j = [];',
  25367. '']),
  25368. LinesToStr([ // $mod.$main
  25369. '$mod.THelper.DoIt.call({',
  25370. ' p: $mod,',
  25371. ' get: function () {',
  25372. ' return this.p.b;',
  25373. ' },',
  25374. ' set: function (v) {',
  25375. ' this.p.b = v;',
  25376. ' }',
  25377. '}, 123);',
  25378. '$mod.THelper.DoIt.call({',
  25379. ' p: $mod,',
  25380. ' get: function () {',
  25381. ' return this.p.j;',
  25382. ' },',
  25383. ' set: function (v) {',
  25384. ' this.p.j = v;',
  25385. ' }',
  25386. '}, 123);',
  25387. '']));
  25388. end;
  25389. procedure TTestModule.TestTypeHelper_EnumType;
  25390. begin
  25391. StartProgram(false);
  25392. Add([
  25393. '{$modeswitch typehelpers}',
  25394. 'type',
  25395. ' TEnum = (red,blue);',
  25396. ' THelper = type helper for TEnum',
  25397. ' procedure DoIt(e: byte = 123);',
  25398. ' class procedure Swing(w: word); static;',
  25399. ' end;',
  25400. 'procedure THelper.DoIt(e: byte);',
  25401. 'begin',
  25402. ' Self:=red;',
  25403. ' Self:=succ(Self);',
  25404. ' with Self do Doit;',
  25405. 'end;',
  25406. 'class procedure THelper.Swing(w: word);',
  25407. 'begin',
  25408. 'end;',
  25409. 'var e: TEnum;',
  25410. 'begin',
  25411. ' e.DoIt;',
  25412. ' red.DoIt;',
  25413. ' TEnum.blue.DoIt;',
  25414. ' TEnum(1).DoIt;',
  25415. ' TEnum.Swing(3);',
  25416. '']);
  25417. ConvertProgram;
  25418. CheckSource('TestTypeHelper_EnumType',
  25419. LinesToStr([ // statements
  25420. 'this.TEnum = {',
  25421. ' "0": "red",',
  25422. ' red: 0,',
  25423. ' "1": "blue",',
  25424. ' blue: 1',
  25425. '};',
  25426. 'rtl.createHelper(this, "THelper", null, function () {',
  25427. ' this.DoIt = function (e) {',
  25428. ' this.set($mod.TEnum.red);',
  25429. ' this.set(this.get() + 1);',
  25430. ' var $with = this.get();',
  25431. ' $mod.THelper.DoIt.call(this, 123);',
  25432. ' };',
  25433. ' this.Swing = function (w) {',
  25434. ' };',
  25435. '});',
  25436. 'this.e = 0;',
  25437. '']),
  25438. LinesToStr([ // $mod.$main
  25439. '$mod.THelper.DoIt.call({',
  25440. ' p: $mod,',
  25441. ' get: function () {',
  25442. ' return this.p.e;',
  25443. ' },',
  25444. ' set: function (v) {',
  25445. ' this.p.e = v;',
  25446. ' }',
  25447. '}, 123);',
  25448. '$mod.THelper.DoIt.call({',
  25449. ' p: $mod.TEnum,',
  25450. ' get: function () {',
  25451. ' return this.p.red;',
  25452. ' },',
  25453. ' set: function (v) {',
  25454. ' rtl.raiseE("EPropReadOnly");',
  25455. ' }',
  25456. '}, 123);',
  25457. '$mod.THelper.DoIt.call({',
  25458. ' p: $mod.TEnum,',
  25459. ' get: function () {',
  25460. ' return this.p.blue;',
  25461. ' },',
  25462. ' set: function (v) {',
  25463. ' rtl.raiseE("EPropReadOnly");',
  25464. ' }',
  25465. '}, 123);',
  25466. '$mod.THelper.DoIt.call({',
  25467. ' get: function () {',
  25468. ' return 1;',
  25469. ' },',
  25470. ' set: function (v) {',
  25471. ' rtl.raiseE("EPropReadOnly");',
  25472. ' }',
  25473. '}, 123);',
  25474. '$mod.THelper.Swing(3);',
  25475. '']));
  25476. end;
  25477. procedure TTestModule.TestTypeHelper_SetType;
  25478. begin
  25479. StartProgram(false);
  25480. Add([
  25481. '{$modeswitch typehelpers}',
  25482. 'type',
  25483. ' TEnum = (red,blue);',
  25484. ' TSetOfEnum = set of TEnum;',
  25485. ' THelper = type helper for TSetOfEnum',
  25486. ' procedure DoIt(e: byte = 123);',
  25487. ' constructor Init(e: TEnum);',
  25488. ' constructor InitEmpty;',
  25489. ' end;',
  25490. 'procedure THelper.DoIt(e: byte);',
  25491. 'begin',
  25492. ' Self:=[];',
  25493. ' Self:=[red];',
  25494. ' Include(Self,blue);',
  25495. 'end;',
  25496. 'constructor THelper.Init(e: TEnum);',
  25497. 'begin',
  25498. ' Self:=[];',
  25499. ' Self:=[e];',
  25500. ' Include(Self,blue);',
  25501. 'end;',
  25502. 'constructor THelper.InitEmpty;',
  25503. 'begin',
  25504. 'end;',
  25505. 'var s: TSetOfEnum;',
  25506. 'begin',
  25507. ' s.DoIt;',
  25508. //' [red].DoIt;',
  25509. //' with s do DoIt;',
  25510. //' with [red,blue] do DoIt;',
  25511. ' s:=TSetOfEnum.Init(blue);',
  25512. ' s:=s.Init(blue);',
  25513. '']);
  25514. ConvertProgram;
  25515. CheckSource('TestTypeHelper_SetType',
  25516. LinesToStr([ // statements
  25517. 'this.TEnum = {',
  25518. ' "0": "red",',
  25519. ' red: 0,',
  25520. ' "1": "blue",',
  25521. ' blue: 1',
  25522. '};',
  25523. 'rtl.createHelper(this, "THelper", null, function () {',
  25524. ' this.DoIt = function (e) {',
  25525. ' this.set({});',
  25526. ' this.set(rtl.createSet($mod.TEnum.red));',
  25527. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25528. ' };',
  25529. ' this.Init = function (e) {',
  25530. ' this.set({});',
  25531. ' this.set(rtl.createSet(e));',
  25532. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25533. ' return this.get();',
  25534. ' };',
  25535. ' this.InitEmpty = function () {',
  25536. ' return this.get();',
  25537. ' };',
  25538. ' this.$new = function (fn, args) {',
  25539. ' return this[fn].apply({',
  25540. ' p: {},',
  25541. ' get: function () {',
  25542. ' return this.p;',
  25543. ' },',
  25544. ' set: function (v) {',
  25545. ' this.p = v;',
  25546. ' }',
  25547. ' }, args);',
  25548. ' };',
  25549. '});',
  25550. 'this.s = {};',
  25551. '']),
  25552. LinesToStr([ // $mod.$main
  25553. '$mod.THelper.DoIt.call({',
  25554. ' p: $mod,',
  25555. ' get: function () {',
  25556. ' return this.p.s;',
  25557. ' },',
  25558. ' set: function (v) {',
  25559. ' this.p.s = v;',
  25560. ' }',
  25561. '}, 123);',
  25562. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25563. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25564. ' p: $mod,',
  25565. ' get: function () {',
  25566. ' return this.p.s;',
  25567. ' },',
  25568. ' set: function (v) {',
  25569. ' this.p.s = v;',
  25570. ' }',
  25571. '}, $mod.TEnum.blue));',
  25572. '']));
  25573. end;
  25574. procedure TTestModule.TestTypeHelper_InterfaceType;
  25575. begin
  25576. StartProgram(false);
  25577. Add([
  25578. '{$interfaces com}',
  25579. '{$modeswitch typehelpers}',
  25580. 'type',
  25581. ' IUnknown = interface',
  25582. ' function _AddRef: longint;',
  25583. ' function _Release: longint;',
  25584. ' end;',
  25585. ' TObject = class(IUnknown)',
  25586. ' function _AddRef: longint; virtual; abstract;',
  25587. ' function _Release: longint; virtual; abstract;',
  25588. ' end;',
  25589. ' THelper = type helper for IUnknown',
  25590. ' procedure Fly(e: byte = 123);',
  25591. ' class procedure Run; static;',
  25592. ' end;',
  25593. 'var',
  25594. ' i: IUnknown;',
  25595. ' o: TObject;',
  25596. 'procedure THelper.Fly(e: byte);',
  25597. 'begin',
  25598. ' i:=Self;',
  25599. ' o:=Self as TObject;',
  25600. ' Self:=nil;',
  25601. ' Self:=i;',
  25602. ' Self:=o;',
  25603. ' with Self do begin',
  25604. ' Fly;',
  25605. ' Fly();',
  25606. ' end;',
  25607. 'end;',
  25608. 'class procedure THelper.Run;',
  25609. 'var l: IUnknown;',
  25610. 'begin',
  25611. ' l.Fly;',
  25612. ' l.Fly();',
  25613. 'end;',
  25614. 'begin',
  25615. ' i.Fly;',
  25616. ' i.Fly();',
  25617. ' i.Run;',
  25618. ' i.Run();',
  25619. ' IUnknown.Run;',
  25620. ' IUnknown.Run();',
  25621. '']);
  25622. ConvertProgram;
  25623. CheckSource('TestTypeHelper_InterfaceType',
  25624. LinesToStr([ // statements
  25625. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25626. 'rtl.createClass(this, "TObject", null, function () {',
  25627. ' this.$init = function () {',
  25628. ' };',
  25629. ' this.$final = function () {',
  25630. ' };',
  25631. ' rtl.addIntf(this, $mod.IUnknown);',
  25632. '});',
  25633. 'rtl.createHelper(this, "THelper", null, function () {',
  25634. ' this.Fly = function (e) {',
  25635. ' var $ir = rtl.createIntfRefs();',
  25636. ' try {',
  25637. ' rtl.setIntfP($mod, "i", this.get());',
  25638. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25639. ' this.set(null);',
  25640. ' this.set($mod.i);',
  25641. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25642. ' var $with = this.get();',
  25643. ' $mod.THelper.Fly.call(this, 123);',
  25644. ' $mod.THelper.Fly.call(this, 123);',
  25645. ' } finally {',
  25646. ' $ir.free();',
  25647. ' };',
  25648. ' };',
  25649. ' this.Run = function () {',
  25650. ' var l = null;',
  25651. ' try {',
  25652. ' $mod.THelper.Fly.call({',
  25653. ' get: function () {',
  25654. ' return l;',
  25655. ' },',
  25656. ' set: function (v) {',
  25657. ' l = rtl.setIntfL(l, v);',
  25658. ' }',
  25659. ' }, 123);',
  25660. ' $mod.THelper.Fly.call({',
  25661. ' get: function () {',
  25662. ' return l;',
  25663. ' },',
  25664. ' set: function (v) {',
  25665. ' l = rtl.setIntfL(l, v);',
  25666. ' }',
  25667. ' }, 123);',
  25668. ' } finally {',
  25669. ' rtl._Release(l);',
  25670. ' };',
  25671. ' };',
  25672. '});',
  25673. 'this.i = null;',
  25674. 'this.o = null;',
  25675. '']),
  25676. LinesToStr([ // $mod.$main
  25677. '$mod.THelper.Fly.call({',
  25678. ' p: $mod,',
  25679. ' get: function () {',
  25680. ' return this.p.i;',
  25681. ' },',
  25682. ' set: function (v) {',
  25683. ' rtl.setIntfP(this.p, "i", v);',
  25684. ' }',
  25685. '}, 123);',
  25686. '$mod.THelper.Fly.call({',
  25687. ' p: $mod,',
  25688. ' get: function () {',
  25689. ' return this.p.i;',
  25690. ' },',
  25691. ' set: function (v) {',
  25692. ' rtl.setIntfP(this.p, "i", v);',
  25693. ' }',
  25694. '}, 123);',
  25695. '$mod.THelper.Run();',
  25696. '$mod.THelper.Run();',
  25697. '$mod.THelper.Run();',
  25698. '$mod.THelper.Run();',
  25699. '']));
  25700. end;
  25701. procedure TTestModule.TestTypeHelper_NestedSelf;
  25702. begin
  25703. StartProgram(false);
  25704. Add([
  25705. '{$modeswitch typehelpers}',
  25706. 'type',
  25707. ' THelper = type helper for string',
  25708. ' procedure Run(Value: string);',
  25709. ' end;',
  25710. 'procedure THelper.Run(Value: string);',
  25711. ' function Sub(i: nativeint): boolean;',
  25712. ' begin',
  25713. ' Result:=Self[i+1]=Value[i];',
  25714. ' end;',
  25715. 'begin',
  25716. ' if Self[3]=Value[4] then ;',
  25717. 'end;',
  25718. 'begin',
  25719. '']);
  25720. ConvertProgram;
  25721. CheckSource('TestTypeHelper_NestedSelf',
  25722. LinesToStr([ // statements
  25723. 'rtl.createHelper(this, "THelper", null, function () {',
  25724. ' this.Run = function (Value) {',
  25725. ' var $Self = this;',
  25726. ' function Sub(i) {',
  25727. ' var Result = false;',
  25728. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25729. ' return Result;',
  25730. ' };',
  25731. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25732. ' };',
  25733. '});',
  25734. '']),
  25735. LinesToStr([ // $mod.$main
  25736. '']));
  25737. end;
  25738. procedure TTestModule.TestProcType;
  25739. begin
  25740. StartProgram(false);
  25741. Add([
  25742. 'type',
  25743. ' TProcInt = procedure(vI: longint = 1);',
  25744. 'procedure DoIt(vJ: longint);',
  25745. 'begin end;',
  25746. 'var',
  25747. ' b: boolean;',
  25748. ' vP, vQ: tprocint;',
  25749. 'begin',
  25750. ' vp:=nil;',
  25751. ' vp:=vp;',
  25752. ' vp:=@doit;',
  25753. ' vp;',
  25754. ' vp();',
  25755. ' vp(2);',
  25756. ' b:=vp=nil;',
  25757. ' b:=nil=vp;',
  25758. ' b:=vp=vq;',
  25759. ' b:=vp=@doit;',
  25760. ' b:=@doit=vp;',
  25761. ' b:=vp<>nil;',
  25762. ' b:=nil<>vp;',
  25763. ' b:=vp<>vq;',
  25764. ' b:=vp<>@doit;',
  25765. ' b:=@doit<>vp;',
  25766. ' b:=Assigned(vp);',
  25767. ' if Assigned(vp) then ;']);
  25768. ConvertProgram;
  25769. CheckSource('TestProcType',
  25770. LinesToStr([ // statements
  25771. 'this.DoIt = function(vJ) {',
  25772. '};',
  25773. 'this.b = false;',
  25774. 'this.vP = null;',
  25775. 'this.vQ = null;'
  25776. ]),
  25777. LinesToStr([ // $mod.$main
  25778. '$mod.vP = null;',
  25779. '$mod.vP = $mod.vP;',
  25780. '$mod.vP = $mod.DoIt;',
  25781. '$mod.vP(1);',
  25782. '$mod.vP(1);',
  25783. '$mod.vP(2);',
  25784. '$mod.b = $mod.vP === null;',
  25785. '$mod.b = null === $mod.vP;',
  25786. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25787. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25788. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25789. '$mod.b = $mod.vP !== null;',
  25790. '$mod.b = null !== $mod.vP;',
  25791. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25792. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25793. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25794. '$mod.b = $mod.vP != null;',
  25795. 'if ($mod.vP != null) ;',
  25796. '']));
  25797. end;
  25798. procedure TTestModule.TestProcType_Arg;
  25799. begin
  25800. StartProgram(false);
  25801. Add([
  25802. 'type',
  25803. ' TProcInt = procedure(vI: longint = 1);',
  25804. 'procedure DoIt(vJ: longint); begin end;',
  25805. 'procedure DoSome(vP, vQ: TProcInt);',
  25806. 'var',
  25807. ' b: boolean;',
  25808. 'begin',
  25809. ' vp:=nil;',
  25810. ' vp:=vp;',
  25811. ' vp:=@doit;',
  25812. ' vp;',
  25813. ' vp();',
  25814. ' vp(2);',
  25815. ' b:=vp=nil;',
  25816. ' b:=nil=vp;',
  25817. ' b:=vp=vq;',
  25818. ' b:=vp=@doit;',
  25819. ' b:=@doit=vp;',
  25820. ' b:=vp<>nil;',
  25821. ' b:=nil<>vp;',
  25822. ' b:=vp<>vq;',
  25823. ' b:=vp<>@doit;',
  25824. ' b:=@doit<>vp;',
  25825. ' b:=Assigned(vp);',
  25826. ' if Assigned(vp) then ;',
  25827. 'end;',
  25828. 'begin',
  25829. ' DoSome(@DoIt,nil);']);
  25830. ConvertProgram;
  25831. CheckSource('TestProcType_Arg',
  25832. LinesToStr([ // statements
  25833. 'this.DoIt = function(vJ) {',
  25834. '};',
  25835. 'this.DoSome = function(vP, vQ) {',
  25836. ' var b = false;',
  25837. ' vP = null;',
  25838. ' vP = vP;',
  25839. ' vP = $mod.DoIt;',
  25840. ' vP(1);',
  25841. ' vP(1);',
  25842. ' vP(2);',
  25843. ' b = vP === null;',
  25844. ' b = null === vP;',
  25845. ' b = rtl.eqCallback(vP,vQ);',
  25846. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25847. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25848. ' b = vP !== null;',
  25849. ' b = null !== vP;',
  25850. ' b = !rtl.eqCallback(vP, vQ);',
  25851. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25852. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25853. ' b = vP != null;',
  25854. ' if (vP != null) ;',
  25855. '};',
  25856. '']),
  25857. LinesToStr([ // $mod.$main
  25858. '$mod.DoSome($mod.DoIt,null);',
  25859. '']));
  25860. end;
  25861. procedure TTestModule.TestProcType_FunctionFPC;
  25862. begin
  25863. StartProgram(false);
  25864. Add('type');
  25865. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25866. Add('function DoIt(vI: longint): longint;');
  25867. Add('begin end;');
  25868. Add('var');
  25869. Add(' b: boolean;');
  25870. Add(' vP, vQ: tfuncint;');
  25871. Add('begin');
  25872. Add(' vp:=nil;');
  25873. Add(' vp:=vp;');
  25874. Add(' vp:=@doit;'); // ok in fpc and delphi
  25875. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25876. Add(' vp;'); // ok in fpc and delphi
  25877. Add(' vp();');
  25878. Add(' vp(2);');
  25879. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25880. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25881. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25882. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25883. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25884. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25885. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25886. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25887. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25888. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25889. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25890. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25891. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25892. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25893. Add(' b:=Assigned(vp);');
  25894. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25895. Add(' doit(vp());'); // ok in fpc and delphi
  25896. Add(' doit(vp(2));'); // ok in fpc and delphi
  25897. ConvertProgram;
  25898. CheckSource('TestProcType_FunctionFPC',
  25899. LinesToStr([ // statements
  25900. 'this.DoIt = function(vI) {',
  25901. ' var Result = 0;',
  25902. ' return Result;',
  25903. '};',
  25904. 'this.b = false;',
  25905. 'this.vP = null;',
  25906. 'this.vQ = null;'
  25907. ]),
  25908. LinesToStr([ // $mod.$main
  25909. '$mod.vP = null;',
  25910. '$mod.vP = $mod.vP;',
  25911. '$mod.vP = $mod.DoIt;',
  25912. '$mod.vP(1);',
  25913. '$mod.vP(1);',
  25914. '$mod.vP(2);',
  25915. '$mod.b = $mod.vP === null;',
  25916. '$mod.b = null === $mod.vP;',
  25917. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25918. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25919. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25920. '$mod.b = 4 === $mod.vP(1);',
  25921. '$mod.b = $mod.vP !== null;',
  25922. '$mod.b = null !== $mod.vP;',
  25923. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25924. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25925. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25926. '$mod.b = 6 !== $mod.vP(1);',
  25927. '$mod.b = $mod.vP != null;',
  25928. '$mod.DoIt($mod.vP(1));',
  25929. '$mod.DoIt($mod.vP(2));',
  25930. '']));
  25931. end;
  25932. procedure TTestModule.TestProcType_FunctionDelphi;
  25933. begin
  25934. StartProgram(false);
  25935. Add('{$mode Delphi}');
  25936. Add('type');
  25937. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25938. Add('function DoIt(vI: longint): longint;');
  25939. Add('begin end;');
  25940. Add('var');
  25941. Add(' b: boolean;');
  25942. Add(' vP, vQ: tfuncint;');
  25943. Add('begin');
  25944. Add(' vp:=nil;');
  25945. Add(' vp:=vp;');
  25946. Add(' vp:=@doit;'); // ok in fpc and delphi
  25947. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25948. Add(' vp;'); // ok in fpc and delphi
  25949. Add(' vp();');
  25950. Add(' vp(2);');
  25951. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25952. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25953. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25954. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25955. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25956. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25957. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25958. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25959. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25960. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25961. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25962. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25963. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25964. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25965. Add(' b:=Assigned(vp);');
  25966. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25967. Add(' doit(vp());'); // ok in fpc and delphi
  25968. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25969. ConvertProgram;
  25970. CheckSource('TestProcType_FunctionDelphi',
  25971. LinesToStr([ // statements
  25972. 'this.DoIt = function(vI) {',
  25973. ' var Result = 0;',
  25974. ' return Result;',
  25975. '};',
  25976. 'this.b = false;',
  25977. 'this.vP = null;',
  25978. 'this.vQ = null;'
  25979. ]),
  25980. LinesToStr([ // $mod.$main
  25981. '$mod.vP = null;',
  25982. '$mod.vP = $mod.vP;',
  25983. '$mod.vP = $mod.DoIt;',
  25984. '$mod.vP = $mod.DoIt;',
  25985. '$mod.vP(1);',
  25986. '$mod.vP(1);',
  25987. '$mod.vP(2);',
  25988. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25989. '$mod.b = $mod.vP(1) === 3;',
  25990. '$mod.b = 4 === $mod.vP(1);',
  25991. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25992. '$mod.b = $mod.vP(1) !== 5;',
  25993. '$mod.b = 6 !== $mod.vP(1);',
  25994. '$mod.b = $mod.vP != null;',
  25995. '$mod.DoIt($mod.vP(1));',
  25996. '$mod.DoIt($mod.vP(1));',
  25997. '$mod.DoIt($mod.vP(2));',
  25998. '']));
  25999. end;
  26000. procedure TTestModule.TestProcType_ProcedureDelphi;
  26001. begin
  26002. StartProgram(false);
  26003. Add('{$mode Delphi}');
  26004. Add('type');
  26005. Add(' TProc = procedure;');
  26006. Add('procedure DoIt;');
  26007. Add('begin end;');
  26008. Add('var');
  26009. Add(' b: boolean;');
  26010. Add(' vP, vQ: tproc;');
  26011. Add('begin');
  26012. Add(' vp:=nil;');
  26013. Add(' vp:=vp;');
  26014. Add(' vp:=vq;');
  26015. 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
  26016. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26017. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  26018. Add(' vp;'); // ok in fpc and delphi
  26019. Add(' vp();');
  26020. // equal
  26021. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26022. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  26023. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26024. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26025. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  26026. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26027. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26028. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  26029. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26030. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26031. // unequal
  26032. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26033. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  26034. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26035. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  26036. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26037. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  26038. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26039. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  26040. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26041. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  26042. Add(' b:=Assigned(vp);');
  26043. ConvertProgram;
  26044. CheckSource('TestProcType_ProcedureDelphi',
  26045. LinesToStr([ // statements
  26046. 'this.DoIt = function() {',
  26047. '};',
  26048. 'this.b = false;',
  26049. 'this.vP = null;',
  26050. 'this.vQ = null;'
  26051. ]),
  26052. LinesToStr([ // $mod.$main
  26053. '$mod.vP = null;',
  26054. '$mod.vP = $mod.vP;',
  26055. '$mod.vP = $mod.vQ;',
  26056. '$mod.vP = $mod.DoIt;',
  26057. '$mod.vP = $mod.DoIt;',
  26058. '$mod.vP();',
  26059. '$mod.vP();',
  26060. '$mod.b = $mod.vP === null;',
  26061. '$mod.b = null === $mod.vP;',
  26062. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  26063. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26064. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26065. '$mod.b = $mod.vP !== null;',
  26066. '$mod.b = null !== $mod.vP;',
  26067. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  26068. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26069. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26070. '$mod.b = $mod.vP != null;',
  26071. '']));
  26072. end;
  26073. procedure TTestModule.TestProcType_AsParam;
  26074. begin
  26075. StartProgram(false);
  26076. Add('type');
  26077. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26078. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  26079. Add('var vJ: tfuncint;');
  26080. Add('begin');
  26081. Add(' vg:=vg;');
  26082. Add(' vj:=vh;');
  26083. Add(' vi:=vi;');
  26084. Add(' doit(vg,vg,vg);');
  26085. Add(' doit(vh,vh,vj);');
  26086. Add(' doit(vi,vi,vi);');
  26087. Add(' doit(vj,vj,vj);');
  26088. Add('end;');
  26089. Add('var i: tfuncint;');
  26090. Add('begin');
  26091. Add(' doit(i,i,i);');
  26092. ConvertProgram;
  26093. CheckSource('TestProcType_AsParam',
  26094. LinesToStr([ // statements
  26095. 'this.DoIt = function (vG,vH,vI) {',
  26096. ' var vJ = null;',
  26097. ' vG = vG;',
  26098. ' vJ = vH;',
  26099. ' vI.set(vI.get());',
  26100. ' $mod.DoIt(vG, vG, {',
  26101. ' get: function () {',
  26102. ' return vG;',
  26103. ' },',
  26104. ' set: function (v) {',
  26105. ' vG = v;',
  26106. ' }',
  26107. ' });',
  26108. ' $mod.DoIt(vH, vH, {',
  26109. ' get: function () {',
  26110. ' return vJ;',
  26111. ' },',
  26112. ' set: function (v) {',
  26113. ' vJ = v;',
  26114. ' }',
  26115. ' });',
  26116. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  26117. ' $mod.DoIt(vJ, vJ, {',
  26118. ' get: function () {',
  26119. ' return vJ;',
  26120. ' },',
  26121. ' set: function (v) {',
  26122. ' vJ = v;',
  26123. ' }',
  26124. ' });',
  26125. '};',
  26126. 'this.i = null;'
  26127. ]),
  26128. LinesToStr([
  26129. '$mod.DoIt($mod.i,$mod.i,{',
  26130. ' p: $mod,',
  26131. ' get: function () {',
  26132. ' return this.p.i;',
  26133. ' },',
  26134. ' set: function (v) {',
  26135. ' this.p.i = v;',
  26136. ' }',
  26137. '});'
  26138. ]));
  26139. end;
  26140. procedure TTestModule.TestProcType_MethodFPC;
  26141. begin
  26142. StartProgram(false);
  26143. Add('type');
  26144. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26145. Add(' TObject = class');
  26146. Add(' function DoIt(vA: longint = 1): longint;');
  26147. Add(' end;');
  26148. Add('function TObject.DoIt(vA: longint = 1): longint;');
  26149. Add('begin');
  26150. Add('end;');
  26151. Add('var');
  26152. Add(' Obj: TObject;');
  26153. Add(' vP: tfuncint;');
  26154. Add(' b: boolean;');
  26155. Add('begin');
  26156. Add(' vp:[email protected];'); // ok in fpc and delphi
  26157. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  26158. Add(' vp;'); // ok in fpc and delphi
  26159. Add(' vp();');
  26160. Add(' vp(2);');
  26161. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  26162. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  26163. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  26164. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  26165. ConvertProgram;
  26166. CheckSource('TestProcType_MethodFPC',
  26167. LinesToStr([ // statements
  26168. 'rtl.createClass(this, "TObject", null, function () {',
  26169. ' this.$init = function () {',
  26170. ' };',
  26171. ' this.$final = function () {',
  26172. ' };',
  26173. ' this.DoIt = function (vA) {',
  26174. ' var Result = 0;',
  26175. ' return Result;',
  26176. ' };',
  26177. '});',
  26178. 'this.Obj = null;',
  26179. 'this.vP = null;',
  26180. 'this.b = false;'
  26181. ]),
  26182. LinesToStr([
  26183. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26184. '$mod.vP(1);',
  26185. '$mod.vP(1);',
  26186. '$mod.vP(2);',
  26187. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  26188. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  26189. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  26190. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  26191. '']));
  26192. end;
  26193. procedure TTestModule.TestProcType_MethodDelphi;
  26194. begin
  26195. StartProgram(false);
  26196. Add([
  26197. '{$mode delphi}',
  26198. 'type',
  26199. ' TFuncInt = function(vA: longint = 1): longint of object;',
  26200. ' TObject = class',
  26201. ' function DoIt(vA: longint = 1): longint;',
  26202. ' end;',
  26203. 'function TObject.DoIt(vA: longint = 1): longint;',
  26204. 'begin',
  26205. 'end;',
  26206. 'var',
  26207. ' Obj: TObject;',
  26208. ' vP: tfuncint;',
  26209. ' b: boolean;',
  26210. 'begin',
  26211. ' vp:[email protected];', // ok in fpc and delphi
  26212. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  26213. ' vp;', // ok in fpc and delphi
  26214. ' vp();',
  26215. ' vp(2);',
  26216. //' b:[email protected];', // ok in fpc, illegal in delphi
  26217. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  26218. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  26219. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  26220. '']);
  26221. ConvertProgram;
  26222. CheckSource('TestProcType_MethodDelphi',
  26223. LinesToStr([ // statements
  26224. 'rtl.createClass(this, "TObject", null, function () {',
  26225. ' this.$init = function () {',
  26226. ' };',
  26227. ' this.$final = function () {',
  26228. ' };',
  26229. ' this.DoIt = function (vA) {',
  26230. ' var Result = 0;',
  26231. ' return Result;',
  26232. ' };',
  26233. '});',
  26234. 'this.Obj = null;',
  26235. 'this.vP = null;',
  26236. 'this.b = false;'
  26237. ]),
  26238. LinesToStr([
  26239. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26240. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26241. '$mod.vP(1);',
  26242. '$mod.vP(1);',
  26243. '$mod.vP(2);',
  26244. '']));
  26245. end;
  26246. procedure TTestModule.TestProcType_PropertyFPC;
  26247. begin
  26248. StartProgram(false);
  26249. Add('type');
  26250. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26251. Add(' TObject = class');
  26252. Add(' FOnFoo: TFuncInt;');
  26253. Add(' function DoIt(vA: longint = 1): longint;');
  26254. Add(' function GetFoo: TFuncInt;');
  26255. Add(' procedure SetFoo(const Value: TFuncInt);');
  26256. Add(' function GetEvents(Index: longint): TFuncInt;');
  26257. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26258. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26259. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26260. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26261. Add(' end;');
  26262. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26263. Add('function tobject.getfoo: tfuncint; begin end;');
  26264. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26265. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26266. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26267. Add('var');
  26268. Add(' Obj: TObject;');
  26269. Add(' vP: tfuncint;');
  26270. Add(' b: boolean;');
  26271. Add('begin');
  26272. Add(' obj.onfoo:=nil;');
  26273. Add(' obj.onbar:=nil;');
  26274. Add(' obj.events[1]:=nil;');
  26275. Add(' obj.onfoo:=obj.onfoo;');
  26276. Add(' obj.onbar:=obj.onbar;');
  26277. Add(' obj.events[2]:=obj.events[3];');
  26278. Add(' obj.onfoo:[email protected];');
  26279. Add(' obj.onbar:[email protected];');
  26280. Add(' obj.events[4]:[email protected];');
  26281. //Add(' obj.onfoo:=obj.doit;'); // delphi
  26282. //Add(' obj.onbar:=obj.doit;'); // delphi
  26283. //Add(' obj.events[4]:=obj.doit;'); // delphi
  26284. Add(' obj.onfoo;');
  26285. Add(' obj.onbar;');
  26286. //Add(' obj.events[5];'); ToDo in pasresolver
  26287. Add(' obj.onfoo();');
  26288. Add(' obj.onbar();');
  26289. Add(' obj.events[6]();');
  26290. Add(' b:=obj.onfoo=nil;');
  26291. Add(' b:=obj.onbar=nil;');
  26292. Add(' b:=obj.events[7]=nil;');
  26293. Add(' b:=obj.onfoo<>nil;');
  26294. Add(' b:=obj.onbar<>nil;');
  26295. Add(' b:=obj.events[8]<>nil;');
  26296. Add(' b:=obj.onfoo=vp;');
  26297. Add(' b:=obj.onbar=vp;');
  26298. Add(' b:=obj.events[9]=vp;');
  26299. Add(' b:=obj.onfoo=obj.onfoo;');
  26300. Add(' b:=obj.onbar=obj.onfoo;');
  26301. Add(' b:=obj.events[10]=obj.onfoo;');
  26302. Add(' b:=obj.onfoo<>obj.onfoo;');
  26303. Add(' b:=obj.onbar<>obj.onfoo;');
  26304. Add(' b:=obj.events[11]<>obj.onfoo;');
  26305. Add(' b:[email protected];');
  26306. Add(' b:[email protected];');
  26307. Add(' b:=obj.events[12][email protected];');
  26308. Add(' b:=obj.onfoo<>@obj.doit;');
  26309. Add(' b:=obj.onbar<>@obj.doit;');
  26310. Add(' b:=obj.events[12]<>@obj.doit;');
  26311. Add(' b:=Assigned(obj.onfoo);');
  26312. Add(' b:=Assigned(obj.onbar);');
  26313. Add(' b:=Assigned(obj.events[13]);');
  26314. ConvertProgram;
  26315. CheckSource('TestProcType_PropertyFPC',
  26316. LinesToStr([ // statements
  26317. 'rtl.createClass(this, "TObject", null, function () {',
  26318. ' this.$init = function () {',
  26319. ' this.FOnFoo = null;',
  26320. ' };',
  26321. ' this.$final = function () {',
  26322. ' this.FOnFoo = undefined;',
  26323. ' };',
  26324. ' this.DoIt = function (vA) {',
  26325. ' var Result = 0;',
  26326. ' return Result;',
  26327. ' };',
  26328. 'this.GetFoo = function () {',
  26329. ' var Result = null;',
  26330. ' return Result;',
  26331. '};',
  26332. 'this.SetFoo = function (Value) {',
  26333. '};',
  26334. 'this.GetEvents = function (Index) {',
  26335. ' var Result = null;',
  26336. ' return Result;',
  26337. '};',
  26338. 'this.SetEvents = function (Index, Value) {',
  26339. '};',
  26340. '});',
  26341. 'this.Obj = null;',
  26342. 'this.vP = null;',
  26343. 'this.b = false;'
  26344. ]),
  26345. LinesToStr([
  26346. '$mod.Obj.FOnFoo = null;',
  26347. '$mod.Obj.SetFoo(null);',
  26348. '$mod.Obj.SetEvents(1, null);',
  26349. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26350. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26351. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26352. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26353. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26354. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26355. '$mod.Obj.FOnFoo(1);',
  26356. '$mod.Obj.GetFoo();',
  26357. '$mod.Obj.FOnFoo(1);',
  26358. '$mod.Obj.GetFoo()(1);',
  26359. '$mod.Obj.GetEvents(6)(1);',
  26360. '$mod.b = $mod.Obj.FOnFoo === null;',
  26361. '$mod.b = $mod.Obj.GetFoo() === null;',
  26362. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  26363. '$mod.b = $mod.Obj.FOnFoo !== null;',
  26364. '$mod.b = $mod.Obj.GetFoo() !== null;',
  26365. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  26366. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  26367. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  26368. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  26369. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26370. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26371. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  26372. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26373. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26374. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  26375. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26376. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26377. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26378. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26379. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26380. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26381. '$mod.b = $mod.Obj.FOnFoo != null;',
  26382. '$mod.b = $mod.Obj.GetFoo() != null;',
  26383. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26384. '']));
  26385. end;
  26386. procedure TTestModule.TestProcType_PropertyDelphi;
  26387. begin
  26388. StartProgram(false);
  26389. Add('{$mode delphi}');
  26390. Add('type');
  26391. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26392. Add(' TObject = class');
  26393. Add(' FOnFoo: TFuncInt;');
  26394. Add(' function DoIt(vA: longint = 1): longint;');
  26395. Add(' function GetFoo: TFuncInt;');
  26396. Add(' procedure SetFoo(const Value: TFuncInt);');
  26397. Add(' function GetEvents(Index: longint): TFuncInt;');
  26398. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26399. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26400. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26401. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26402. Add(' end;');
  26403. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26404. Add('function tobject.getfoo: tfuncint; begin end;');
  26405. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26406. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26407. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26408. Add('var');
  26409. Add(' Obj: TObject;');
  26410. Add(' vP: tfuncint;');
  26411. Add(' b: boolean;');
  26412. Add('begin');
  26413. Add(' obj.onfoo:=nil;');
  26414. Add(' obj.onbar:=nil;');
  26415. Add(' obj.events[1]:=nil;');
  26416. Add(' obj.onfoo:=obj.onfoo;');
  26417. Add(' obj.onbar:=obj.onbar;');
  26418. Add(' obj.events[2]:=obj.events[3];');
  26419. Add(' obj.onfoo:[email protected];');
  26420. Add(' obj.onbar:[email protected];');
  26421. Add(' obj.events[4]:[email protected];');
  26422. Add(' obj.onfoo:=obj.doit;'); // delphi
  26423. Add(' obj.onbar:=obj.doit;'); // delphi
  26424. Add(' obj.events[4]:=obj.doit;'); // delphi
  26425. Add(' obj.onfoo;');
  26426. Add(' obj.onbar;');
  26427. //Add(' obj.events[5];'); ToDo in pasresolver
  26428. Add(' obj.onfoo();');
  26429. Add(' obj.onbar();');
  26430. Add(' obj.events[6]();');
  26431. //Add(' b:=obj.onfoo=nil;'); // fpc
  26432. //Add(' b:=obj.onbar=nil;'); // fpc
  26433. //Add(' b:=obj.events[7]=nil;'); // fpc
  26434. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26435. //Add(' b:=obj.onbar<>nil;'); // fpc
  26436. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26437. Add(' b:=obj.onfoo=vp;');
  26438. Add(' b:=obj.onbar=vp;');
  26439. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26440. Add(' b:=obj.onfoo=obj.onfoo;');
  26441. Add(' b:=obj.onbar=obj.onfoo;');
  26442. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26443. Add(' b:=obj.onfoo<>obj.onfoo;');
  26444. Add(' b:=obj.onbar<>obj.onfoo;');
  26445. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26446. //Add(' b:[email protected];'); // fpc
  26447. //Add(' b:[email protected];'); // fpc
  26448. //Add(' b:=obj.events[12][email protected];'); // fpc
  26449. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26450. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26451. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26452. Add(' b:=Assigned(obj.onfoo);');
  26453. Add(' b:=Assigned(obj.onbar);');
  26454. Add(' b:=Assigned(obj.events[13]);');
  26455. ConvertProgram;
  26456. CheckSource('TestProcType_PropertyDelphi',
  26457. LinesToStr([ // statements
  26458. 'rtl.createClass(this, "TObject", null, function () {',
  26459. ' this.$init = function () {',
  26460. ' this.FOnFoo = null;',
  26461. ' };',
  26462. ' this.$final = function () {',
  26463. ' this.FOnFoo = undefined;',
  26464. ' };',
  26465. ' this.DoIt = function (vA) {',
  26466. ' var Result = 0;',
  26467. ' return Result;',
  26468. ' };',
  26469. 'this.GetFoo = function () {',
  26470. ' var Result = null;',
  26471. ' return Result;',
  26472. '};',
  26473. 'this.SetFoo = function (Value) {',
  26474. '};',
  26475. 'this.GetEvents = function (Index) {',
  26476. ' var Result = null;',
  26477. ' return Result;',
  26478. '};',
  26479. 'this.SetEvents = function (Index, Value) {',
  26480. '};',
  26481. '});',
  26482. 'this.Obj = null;',
  26483. 'this.vP = null;',
  26484. 'this.b = false;'
  26485. ]),
  26486. LinesToStr([
  26487. '$mod.Obj.FOnFoo = null;',
  26488. '$mod.Obj.SetFoo(null);',
  26489. '$mod.Obj.SetEvents(1, null);',
  26490. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26491. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26492. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26493. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26494. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26495. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26496. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26497. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26498. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26499. '$mod.Obj.FOnFoo(1);',
  26500. '$mod.Obj.GetFoo();',
  26501. '$mod.Obj.FOnFoo(1);',
  26502. '$mod.Obj.GetFoo()(1);',
  26503. '$mod.Obj.GetEvents(6)(1);',
  26504. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26505. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26506. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26507. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26508. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26509. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26510. '$mod.b = $mod.Obj.FOnFoo != null;',
  26511. '$mod.b = $mod.Obj.GetFoo() != null;',
  26512. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26513. '']));
  26514. end;
  26515. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26516. begin
  26517. StartProgram(false);
  26518. Add('type');
  26519. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26520. Add(' TObject = class');
  26521. Add(' FOnFoo: TFuncInt;');
  26522. Add(' function DoIt(vA: longint = 1): longint;');
  26523. Add(' function GetFoo: TFuncInt;');
  26524. Add(' procedure SetFoo(const Value: TFuncInt);');
  26525. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26526. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26527. Add(' end;');
  26528. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26529. Add('function tobject.getfoo: tfuncint; begin end;');
  26530. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26531. Add('var');
  26532. Add(' Obj: TObject;');
  26533. Add(' vP: tfuncint;');
  26534. Add(' b: boolean;');
  26535. Add('begin');
  26536. Add('with obj do begin');
  26537. Add(' fonfoo:=nil;');
  26538. Add(' onfoo:=nil;');
  26539. Add(' onbar:=nil;');
  26540. Add(' fonfoo:=fonfoo;');
  26541. Add(' onfoo:=onfoo;');
  26542. Add(' onbar:=onbar;');
  26543. Add(' fonfoo:=@doit;');
  26544. Add(' onfoo:=@doit;');
  26545. Add(' onbar:=@doit;');
  26546. //Add(' fonfoo:=doit;'); // delphi
  26547. //Add(' onfoo:=doit;'); // delphi
  26548. //Add(' onbar:=doit;'); // delphi
  26549. Add(' fonfoo;');
  26550. Add(' onfoo;');
  26551. Add(' onbar;');
  26552. Add(' fonfoo();');
  26553. Add(' onfoo();');
  26554. Add(' onbar();');
  26555. Add(' b:=fonfoo=nil;');
  26556. Add(' b:=onfoo=nil;');
  26557. Add(' b:=onbar=nil;');
  26558. Add(' b:=fonfoo<>nil;');
  26559. Add(' b:=onfoo<>nil;');
  26560. Add(' b:=onbar<>nil;');
  26561. Add(' b:=fonfoo=vp;');
  26562. Add(' b:=onfoo=vp;');
  26563. Add(' b:=onbar=vp;');
  26564. Add(' b:=fonfoo=fonfoo;');
  26565. Add(' b:=onfoo=onfoo;');
  26566. Add(' b:=onbar=onfoo;');
  26567. Add(' b:=fonfoo<>fonfoo;');
  26568. Add(' b:=onfoo<>onfoo;');
  26569. Add(' b:=onbar<>onfoo;');
  26570. Add(' b:=fonfoo=@doit;');
  26571. Add(' b:=onfoo=@doit;');
  26572. Add(' b:=onbar=@doit;');
  26573. Add(' b:=fonfoo<>@doit;');
  26574. Add(' b:=onfoo<>@doit;');
  26575. Add(' b:=onbar<>@doit;');
  26576. Add(' b:=Assigned(fonfoo);');
  26577. Add(' b:=Assigned(onfoo);');
  26578. Add(' b:=Assigned(onbar);');
  26579. Add('end;');
  26580. ConvertProgram;
  26581. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26582. LinesToStr([ // statements
  26583. 'rtl.createClass(this, "TObject", null, function () {',
  26584. ' this.$init = function () {',
  26585. ' this.FOnFoo = null;',
  26586. ' };',
  26587. ' this.$final = function () {',
  26588. ' this.FOnFoo = undefined;',
  26589. ' };',
  26590. ' this.DoIt = function (vA) {',
  26591. ' var Result = 0;',
  26592. ' return Result;',
  26593. ' };',
  26594. ' this.GetFoo = function () {',
  26595. ' var Result = null;',
  26596. ' return Result;',
  26597. ' };',
  26598. ' this.SetFoo = function (Value) {',
  26599. ' };',
  26600. '});',
  26601. 'this.Obj = null;',
  26602. 'this.vP = null;',
  26603. 'this.b = false;'
  26604. ]),
  26605. LinesToStr([
  26606. 'var $with = $mod.Obj;',
  26607. '$with.FOnFoo = null;',
  26608. '$with.FOnFoo = null;',
  26609. '$with.SetFoo(null);',
  26610. '$with.FOnFoo = $with.FOnFoo;',
  26611. '$with.FOnFoo = $with.FOnFoo;',
  26612. '$with.SetFoo($with.GetFoo());',
  26613. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26614. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26615. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26616. '$with.FOnFoo(1);',
  26617. '$with.FOnFoo(1);',
  26618. '$with.GetFoo();',
  26619. '$with.FOnFoo(1);',
  26620. '$with.FOnFoo(1);',
  26621. '$with.GetFoo()(1);',
  26622. '$mod.b = $with.FOnFoo === null;',
  26623. '$mod.b = $with.FOnFoo === null;',
  26624. '$mod.b = $with.GetFoo() === null;',
  26625. '$mod.b = $with.FOnFoo !== null;',
  26626. '$mod.b = $with.FOnFoo !== null;',
  26627. '$mod.b = $with.GetFoo() !== null;',
  26628. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26629. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26630. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26631. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26632. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26633. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26634. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26635. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26636. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26637. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26638. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26639. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26640. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26641. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26642. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26643. '$mod.b = $with.FOnFoo != null;',
  26644. '$mod.b = $with.FOnFoo != null;',
  26645. '$mod.b = $with.GetFoo() != null;',
  26646. '']));
  26647. end;
  26648. procedure TTestModule.TestProcType_Nested;
  26649. begin
  26650. StartProgram(false);
  26651. Add([
  26652. 'type',
  26653. ' TProcInt = procedure(vI: longint = 1);',
  26654. 'procedure DoIt(vJ: longint);',
  26655. 'var aProc: TProcInt;',
  26656. ' b: boolean;',
  26657. ' procedure Sub(vK: longint);',
  26658. ' var aSub: TProcInt;',
  26659. ' procedure SubSub(vK: longint);',
  26660. ' var aSubSub: TProcInt;',
  26661. ' begin;',
  26662. ' aProc:=@DoIt;',
  26663. ' aSub:=@DoIt;',
  26664. ' aSubSub:=@DoIt;',
  26665. ' aProc:=@Sub;',
  26666. ' aSub:=@Sub;',
  26667. ' aSubSub:=@Sub;',
  26668. ' aProc:=@SubSub;',
  26669. ' aSub:=@SubSub;',
  26670. ' aSubSub:=@SubSub;',
  26671. ' end;',
  26672. ' begin;',
  26673. ' end;',
  26674. 'begin;',
  26675. ' aProc:=@Sub;',
  26676. ' b:=aProc=@Sub;',
  26677. ' b:=@Sub=aProc;',
  26678. 'end;',
  26679. 'begin',
  26680. '']);
  26681. ConvertProgram;
  26682. CheckSource('TestProcType_Nested',
  26683. LinesToStr([ // statements
  26684. 'this.DoIt = function (vJ) {',
  26685. ' var aProc = null;',
  26686. ' var b = false;',
  26687. ' function Sub(vK) {',
  26688. ' var aSub = null;',
  26689. ' function SubSub(vK) {',
  26690. ' var aSubSub = null;',
  26691. ' aProc = $mod.DoIt;',
  26692. ' aSub = $mod.DoIt;',
  26693. ' aSubSub = $mod.DoIt;',
  26694. ' aProc = Sub;',
  26695. ' aSub = Sub;',
  26696. ' aSubSub = Sub;',
  26697. ' aProc = SubSub;',
  26698. ' aSub = SubSub;',
  26699. ' aSubSub = SubSub;',
  26700. ' };',
  26701. ' };',
  26702. ' aProc = Sub;',
  26703. ' b = rtl.eqCallback(aProc, Sub);',
  26704. ' b = rtl.eqCallback(Sub, aProc);',
  26705. '};',
  26706. '']),
  26707. LinesToStr([ // $mod.$main
  26708. '']));
  26709. end;
  26710. procedure TTestModule.TestProcType_NestedOfObject;
  26711. begin
  26712. StartProgram(false);
  26713. Add([
  26714. 'type',
  26715. ' TProcInt = procedure(vI: longint = 1) of object;',
  26716. ' TObject = class',
  26717. ' procedure DoIt(vJ: longint);',
  26718. ' end;',
  26719. 'procedure TObject.DoIt(vJ: longint);',
  26720. 'var aProc: TProcInt;',
  26721. ' b: boolean;',
  26722. ' procedure Sub(vK: longint);',
  26723. ' var aSub: TProcInt;',
  26724. ' procedure SubSub(vK: longint);',
  26725. ' var aSubSub: TProcInt;',
  26726. ' begin;',
  26727. ' aProc:=@DoIt;',
  26728. ' aSub:=@DoIt;',
  26729. ' aSubSub:=@DoIt;',
  26730. ' aProc:=@Sub;',
  26731. ' aSub:=@Sub;',
  26732. ' aSubSub:=@Sub;',
  26733. ' aProc:=@SubSub;',
  26734. ' aSub:=@SubSub;',
  26735. ' aSubSub:=@SubSub;',
  26736. ' end;',
  26737. ' begin;',
  26738. ' end;',
  26739. 'begin;',
  26740. ' aProc:=@Sub;',
  26741. ' b:=aProc=@Sub;',
  26742. ' b:=@Sub=aProc;',
  26743. 'end;',
  26744. 'begin',
  26745. '']);
  26746. ConvertProgram;
  26747. CheckSource('TestProcType_Nested',
  26748. LinesToStr([ // statements
  26749. 'rtl.createClass(this, "TObject", null, function () {',
  26750. ' this.$init = function () {',
  26751. ' };',
  26752. ' this.$final = function () {',
  26753. ' };',
  26754. ' this.DoIt = function (vJ) {',
  26755. ' var $Self = this;',
  26756. ' var aProc = null;',
  26757. ' var b = false;',
  26758. ' function Sub(vK) {',
  26759. ' var aSub = null;',
  26760. ' function SubSub(vK) {',
  26761. ' var aSubSub = null;',
  26762. ' aProc = rtl.createCallback($Self, "DoIt");',
  26763. ' aSub = rtl.createCallback($Self, "DoIt");',
  26764. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26765. ' aProc = Sub;',
  26766. ' aSub = Sub;',
  26767. ' aSubSub = Sub;',
  26768. ' aProc = SubSub;',
  26769. ' aSub = SubSub;',
  26770. ' aSubSub = SubSub;',
  26771. ' };',
  26772. ' };',
  26773. ' aProc = Sub;',
  26774. ' b = rtl.eqCallback(aProc, Sub);',
  26775. ' b = rtl.eqCallback(Sub, aProc);',
  26776. ' };',
  26777. '});',
  26778. '']),
  26779. LinesToStr([ // $mod.$main
  26780. '']));
  26781. end;
  26782. procedure TTestModule.TestProcType_ReferenceToProc;
  26783. begin
  26784. StartProgram(false);
  26785. Add([
  26786. 'type',
  26787. ' TProcRef = reference to procedure(i: longint = 0);',
  26788. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26789. 'var',
  26790. ' p: TProcRef;',
  26791. ' f: TFuncRef;',
  26792. 'procedure DoIt(i: longint);',
  26793. 'begin',
  26794. 'end;',
  26795. 'function GetIt(i: longint): longint;',
  26796. 'begin',
  26797. ' p:=@DoIt;',
  26798. ' f:=@GetIt;',
  26799. ' f;',
  26800. ' f();',
  26801. ' f(1);',
  26802. 'end;',
  26803. 'begin',
  26804. ' p:=@DoIt;',
  26805. ' f:=@GetIt;',
  26806. ' f;',
  26807. ' f();',
  26808. ' f(1);',
  26809. ' p:=TProcRef(f);',
  26810. '']);
  26811. ConvertProgram;
  26812. CheckSource('TestProcType_ReferenceToProc',
  26813. LinesToStr([ // statements
  26814. 'this.p = null;',
  26815. 'this.f = null;',
  26816. 'this.DoIt = function (i) {',
  26817. '};',
  26818. 'this.GetIt = function (i) {',
  26819. ' var Result = 0;',
  26820. ' $mod.p = $mod.DoIt;',
  26821. ' $mod.f = $mod.GetIt;',
  26822. ' $mod.f(0);',
  26823. ' $mod.f(0);',
  26824. ' $mod.f(1);',
  26825. ' return Result;',
  26826. '};',
  26827. '']),
  26828. LinesToStr([ // $mod.$main
  26829. '$mod.p = $mod.DoIt;',
  26830. '$mod.f = $mod.GetIt;',
  26831. '$mod.f(0);',
  26832. '$mod.f(0);',
  26833. '$mod.f(1);',
  26834. '$mod.p = $mod.f;',
  26835. '']));
  26836. end;
  26837. procedure TTestModule.TestProcType_ReferenceToMethod;
  26838. begin
  26839. StartProgram(false);
  26840. Add([
  26841. 'type',
  26842. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26843. ' TObject = class',
  26844. ' function Grow(s: longint): longint;',
  26845. ' end;',
  26846. 'var',
  26847. ' f: tfuncref;',
  26848. 'function tobject.grow(s: longint): longint;',
  26849. ' function GrowSub(i: longint): longint;',
  26850. ' begin',
  26851. ' f:=@grow;',
  26852. ' f:=@growsub;',
  26853. ' end;',
  26854. 'begin',
  26855. ' f:=@grow;',
  26856. ' f:=@growsub;',
  26857. 'end;',
  26858. 'begin',
  26859. '']);
  26860. ConvertProgram;
  26861. CheckSource('TestProcType_ReferenceToMethod',
  26862. LinesToStr([ // statements
  26863. 'rtl.createClass(this, "TObject", null, function () {',
  26864. ' this.$init = function () {',
  26865. ' };',
  26866. ' this.$final = function () {',
  26867. ' };',
  26868. ' this.Grow = function (s) {',
  26869. ' var $Self = this;',
  26870. ' var Result = 0;',
  26871. ' function GrowSub(i) {',
  26872. ' var Result = 0;',
  26873. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26874. ' $mod.f = GrowSub;',
  26875. ' return Result;',
  26876. ' };',
  26877. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26878. ' $mod.f = GrowSub;',
  26879. ' return Result;',
  26880. ' };',
  26881. '});',
  26882. 'this.f = null;',
  26883. '']),
  26884. LinesToStr([ // $mod.$main
  26885. '']));
  26886. end;
  26887. procedure TTestModule.TestProcType_Typecast;
  26888. begin
  26889. StartProgram(false);
  26890. Add([
  26891. 'type',
  26892. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26893. ' TEvent = procedure of object;',
  26894. ' TGetter = function:longint of object;',
  26895. ' TProcA = procedure(i: longint);',
  26896. ' TFuncB = function(i, j: longint): longint;',
  26897. 'procedure DoIt(); varargs; begin end;',
  26898. 'var',
  26899. ' Notify: tnotifyevent;',
  26900. ' Event: tevent;',
  26901. ' Getter: tgetter;',
  26902. ' ProcA: tproca;',
  26903. ' FuncB: tfuncb;',
  26904. ' p: pointer;',
  26905. 'begin',
  26906. ' notify:=tnotifyevent(event);',
  26907. ' event:=tevent(event);',
  26908. ' event:=tevent(notify);',
  26909. ' event:=tevent(getter);',
  26910. ' event:=tevent(proca);',
  26911. ' proca:=tproca(funcb);',
  26912. ' funcb:=tfuncb(funcb);',
  26913. ' funcb:=tfuncb(proca);',
  26914. ' funcb:=tfuncb(getter);',
  26915. ' proca:=tproca(p);',
  26916. ' funcb:=tfuncb(p);',
  26917. ' getter:=tgetter(p);',
  26918. ' p:=pointer(notify);',
  26919. ' p:=notify;',
  26920. ' p:=pointer(proca);',
  26921. ' p:=proca;',
  26922. ' p:=pointer(funcb);',
  26923. ' p:=funcb;',
  26924. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26925. '']);
  26926. ConvertProgram;
  26927. CheckSource('TestProcType_Typecast',
  26928. LinesToStr([ // statements
  26929. 'this.DoIt = function () {',
  26930. '};',
  26931. 'this.Notify = null;',
  26932. 'this.Event = null;',
  26933. 'this.Getter = null;',
  26934. 'this.ProcA = null;',
  26935. 'this.FuncB = null;',
  26936. 'this.p = null;',
  26937. '']),
  26938. LinesToStr([ // $mod.$main
  26939. '$mod.Notify = $mod.Event;',
  26940. '$mod.Event = $mod.Event;',
  26941. '$mod.Event = $mod.Notify;',
  26942. '$mod.Event = $mod.Getter;',
  26943. '$mod.Event = $mod.ProcA;',
  26944. '$mod.ProcA = $mod.FuncB;',
  26945. '$mod.FuncB = $mod.FuncB;',
  26946. '$mod.FuncB = $mod.ProcA;',
  26947. '$mod.FuncB = $mod.Getter;',
  26948. '$mod.ProcA = $mod.p;',
  26949. '$mod.FuncB = $mod.p;',
  26950. '$mod.Getter = $mod.p;',
  26951. '$mod.p = $mod.Notify;',
  26952. '$mod.p = $mod.Notify;',
  26953. '$mod.p = $mod.ProcA;',
  26954. '$mod.p = $mod.ProcA;',
  26955. '$mod.p = $mod.FuncB;',
  26956. '$mod.p = $mod.FuncB;',
  26957. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26958. '']));
  26959. end;
  26960. procedure TTestModule.TestProcType_PassProcToUntyped;
  26961. begin
  26962. StartProgram(false);
  26963. Add([
  26964. 'type',
  26965. ' TEvent = procedure of object;',
  26966. ' TFunc = function: longint;',
  26967. 'procedure DoIt(); varargs; begin end;',
  26968. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26969. 'var',
  26970. ' Event: tevent;',
  26971. ' Func: TFunc;',
  26972. 'begin',
  26973. ' doit(event,func);',
  26974. ' dosome(event,event,event);',
  26975. ' dosome(func,func,func);',
  26976. '']);
  26977. ConvertProgram;
  26978. CheckSource('TestProcType_PassProcToUntyped',
  26979. LinesToStr([ // statements
  26980. 'this.DoIt = function () {',
  26981. '};',
  26982. 'this.DoSome = function (a, b, p) {',
  26983. '};',
  26984. 'this.Event = null;',
  26985. 'this.Func = null;',
  26986. '']),
  26987. LinesToStr([ // $mod.$main
  26988. '$mod.DoIt($mod.Event, $mod.Func);',
  26989. '$mod.DoSome($mod.Event, {',
  26990. ' p: $mod,',
  26991. ' get: function () {',
  26992. ' return this.p.Event;',
  26993. ' },',
  26994. ' set: function (v) {',
  26995. ' this.p.Event = v;',
  26996. ' }',
  26997. '}, $mod.Event);',
  26998. '$mod.DoSome($mod.Func, {',
  26999. ' p: $mod,',
  27000. ' get: function () {',
  27001. ' return this.p.Func;',
  27002. ' },',
  27003. ' set: function (v) {',
  27004. ' this.p.Func = v;',
  27005. ' }',
  27006. '}, $mod.Func);',
  27007. '']));
  27008. end;
  27009. procedure TTestModule.TestProcType_PassProcToArray;
  27010. begin
  27011. StartProgram(false);
  27012. Add([
  27013. 'type',
  27014. ' TFunc = function: longint;',
  27015. ' TArrFunc = array of TFunc;',
  27016. 'procedure DoIt(Arr: TArrFunc); begin end;',
  27017. 'function GetIt: longint; begin end;',
  27018. 'var',
  27019. ' Func: tfunc;',
  27020. 'begin',
  27021. ' doit([]);',
  27022. ' doit([@GetIt]);',
  27023. ' doit([Func]);',
  27024. '']);
  27025. ConvertProgram;
  27026. CheckSource('TestProcType_PassProcToArray',
  27027. LinesToStr([ // statements
  27028. 'this.DoIt = function (Arr) {',
  27029. '};',
  27030. 'this.GetIt = function () {',
  27031. ' var Result = 0;',
  27032. ' return Result;',
  27033. '};',
  27034. 'this.Func = null;',
  27035. '']),
  27036. LinesToStr([ // $mod.$main
  27037. '$mod.DoIt([]);',
  27038. '$mod.DoIt([$mod.GetIt]);',
  27039. '$mod.DoIt([$mod.Func]);',
  27040. '']));
  27041. end;
  27042. procedure TTestModule.TestProcType_SafeCallObjFPC;
  27043. begin
  27044. StartProgram(false);
  27045. Add([
  27046. '{$modeswitch externalclass}',
  27047. 'type',
  27048. ' TProc = reference to procedure(i: longint); safecall;',
  27049. ' TEvent = procedure(i: longint) of object; safecall;',
  27050. ' TExtA = class external name ''ExtObj''',
  27051. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27052. ' procedure DoSome(Id: longint = 1);',
  27053. ' procedure SetOnClick(const e: TEvent);',
  27054. ' property OnClick: TEvent write SetOnClick;',
  27055. ' class procedure Fly(Id: longint = 1); static;',
  27056. ' procedure SetOnShow(const p: TProc);',
  27057. ' property OnShow: TProc write SetOnShow;',
  27058. ' end;',
  27059. 'procedure Run(i: longint = 1);',
  27060. 'begin',
  27061. 'end;',
  27062. 'var',
  27063. ' Obj: texta;',
  27064. ' e: TEvent;',
  27065. ' p: TProc;',
  27066. 'begin',
  27067. ' e:=e;',
  27068. ' e:[email protected];',
  27069. ' e:[email protected];',
  27070. ' e:=TEvent(@obj.dosome);', // no safecall
  27071. ' obj.OnClick:[email protected];',
  27072. ' obj.OnClick:[email protected];',
  27073. ' obj.setonclick(@obj.doit);',
  27074. ' obj.setonclick(@obj.dosome);',
  27075. ' p:=@Run;',
  27076. ' p:[email protected];',
  27077. ' obj.OnShow:=@Run;',
  27078. ' obj.OnShow:[email protected];',
  27079. ' obj.setOnShow(@Run);',
  27080. ' obj.setOnShow(@TExtA.Fly);',
  27081. ' with obj do begin',
  27082. ' e:=@doit;',
  27083. ' e:=@dosome;',
  27084. ' OnClick:=@doit;',
  27085. ' OnClick:=@dosome;',
  27086. ' setonclick(@doit);',
  27087. ' setonclick(@dosome);',
  27088. ' OnShow:=@Run;',
  27089. ' setOnShow(@Run);',
  27090. ' end;']);
  27091. ConvertProgram;
  27092. CheckSource('TestProcType_SafeCallObjFPC',
  27093. LinesToStr([ // statements
  27094. 'this.Run = function (i) {',
  27095. '};',
  27096. 'this.Obj = null;',
  27097. 'this.e = null;',
  27098. 'this.p = null;',
  27099. '']),
  27100. LinesToStr([ // $mod.$main
  27101. '$mod.e = $mod.e;',
  27102. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  27103. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  27104. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  27105. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27106. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27107. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27108. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27109. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  27110. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  27111. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27112. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27113. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27114. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27115. 'var $with = $mod.Obj;',
  27116. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  27117. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  27118. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27119. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27120. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27121. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27122. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27123. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27124. '']));
  27125. end;
  27126. procedure TTestModule.TestProcType_SafeCallDelphi;
  27127. begin
  27128. StartProgram(false);
  27129. Add([
  27130. '{$mode delphi}',
  27131. '{$modeswitch externalclass}',
  27132. 'type',
  27133. ' TProc = reference to procedure(i: longint); safecall;',
  27134. ' TEvent = procedure(i: longint) of object; safecall;',
  27135. ' TExtA = class external name ''ExtObj''',
  27136. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27137. ' procedure DoSome(Id: longint = 1);',
  27138. ' procedure SetOnClick(const e: TEvent);',
  27139. ' property OnClick: TEvent write SetOnClick;',
  27140. ' class procedure Fly(Id: longint = 1); static;',
  27141. ' procedure SetOnShow(const p: TProc);',
  27142. ' property OnShow: TProc write SetOnShow;',
  27143. ' end;',
  27144. 'procedure Run(i: longint = 1);',
  27145. 'begin',
  27146. 'end;',
  27147. 'var',
  27148. ' Obj: texta;',
  27149. ' e: TEvent;',
  27150. ' p: TProc;',
  27151. 'begin',
  27152. ' e:=e;',
  27153. ' e:=obj.doit;',
  27154. ' e:=obj.dosome;',
  27155. ' e:=TEvent(@obj.dosome);', // no safecall
  27156. ' obj.OnClick:=obj.doit;',
  27157. ' obj.OnClick:=obj.dosome;',
  27158. ' obj.setonclick(obj.doit);',
  27159. ' obj.setonclick(obj.dosome);',
  27160. ' p:=Run;',
  27161. ' p:=TExtA.Fly;',
  27162. ' obj.OnShow:=Run;',
  27163. ' obj.OnShow:=TExtA.Fly;',
  27164. ' obj.setOnShow(Run);',
  27165. ' obj.setOnShow(TExtA.Fly);',
  27166. ' with obj do begin',
  27167. ' e:=doit;',
  27168. ' e:=dosome;',
  27169. ' OnClick:=doit;',
  27170. ' OnClick:=dosome;',
  27171. ' setonclick(doit);',
  27172. ' setonclick(dosome);',
  27173. ' OnShow:=@Run;',
  27174. ' setOnShow(@Run);',
  27175. ' end;']);
  27176. ConvertProgram;
  27177. CheckSource('TestProcType_SafeCallDelphi',
  27178. LinesToStr([ // statements
  27179. 'this.Run = function (i) {',
  27180. '};',
  27181. 'this.Obj = null;',
  27182. 'this.e = null;',
  27183. 'this.p = null;',
  27184. '']),
  27185. LinesToStr([ // $mod.$main
  27186. '$mod.e = $mod.e;',
  27187. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  27188. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  27189. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  27190. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27191. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27192. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27193. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27194. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  27195. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  27196. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27197. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27198. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27199. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27200. 'var $with = $mod.Obj;',
  27201. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  27202. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  27203. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27204. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27205. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27206. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27207. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27208. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27209. '']));
  27210. end;
  27211. procedure TTestModule.TestPointer;
  27212. begin
  27213. StartProgram(false);
  27214. Add(['type',
  27215. ' TObject = class end;',
  27216. ' TClass = class of TObject;',
  27217. ' TArrInt = array of longint;',
  27218. 'const',
  27219. ' n = nil;',
  27220. 'var',
  27221. ' v: jsvalue;',
  27222. ' Obj: tobject;',
  27223. ' C: tclass;',
  27224. ' a: tarrint;',
  27225. ' p: Pointer = nil;',
  27226. ' s: string;',
  27227. 'begin',
  27228. ' p:=p;',
  27229. ' p:=nil;',
  27230. ' if p=nil then;',
  27231. ' if nil=p then;',
  27232. ' if Assigned(p) then;',
  27233. ' p:=Pointer(v);',
  27234. ' p:=obj;',
  27235. ' p:=c;',
  27236. ' p:=a;',
  27237. ' p:=tobject;',
  27238. ' obj:=TObject(p);',
  27239. ' c:=TClass(p);',
  27240. ' a:=TArrInt(p);',
  27241. ' p:=n;',
  27242. ' p:=Pointer(a);',
  27243. ' p:=pointer(s);',
  27244. ' s:=string(p);',
  27245. '']);
  27246. ConvertProgram;
  27247. CheckSource('TestPointer',
  27248. LinesToStr([ // statements
  27249. 'rtl.createClass(this, "TObject", null, function () {',
  27250. ' this.$init = function () {',
  27251. ' };',
  27252. ' this.$final = function () {',
  27253. ' };',
  27254. '});',
  27255. 'this.n = null;',
  27256. 'this.v = undefined;',
  27257. 'this.Obj = null;',
  27258. 'this.C = null;',
  27259. 'this.a = [];',
  27260. 'this.p = null;',
  27261. 'this.s = "";',
  27262. '']),
  27263. LinesToStr([ // $mod.$main
  27264. '$mod.p = $mod.p;',
  27265. '$mod.p = null;',
  27266. 'if ($mod.p === null) ;',
  27267. 'if (null === $mod.p) ;',
  27268. 'if ($mod.p != null) ;',
  27269. '$mod.p = $mod.v;',
  27270. '$mod.p = $mod.Obj;',
  27271. '$mod.p = $mod.C;',
  27272. '$mod.p = $mod.a;',
  27273. '$mod.p = $mod.TObject;',
  27274. '$mod.Obj = $mod.p;',
  27275. '$mod.C = $mod.p;',
  27276. '$mod.a = $mod.p;',
  27277. '$mod.p = null;',
  27278. '$mod.p = $mod.a;',
  27279. '$mod.p = $mod.s;',
  27280. '$mod.s = $mod.p;',
  27281. '']));
  27282. end;
  27283. procedure TTestModule.TestPointer_Proc;
  27284. begin
  27285. StartProgram(false);
  27286. Add('type');
  27287. Add(' TObject = class');
  27288. Add(' procedure DoIt; virtual; abstract;');
  27289. Add(' end;');
  27290. Add('procedure DoSome; begin end;');
  27291. Add('var');
  27292. Add(' o: TObject;');
  27293. Add(' p: Pointer;');
  27294. Add('begin');
  27295. Add(' p:=@DoSome;');
  27296. Add(' p:[email protected];');
  27297. ConvertProgram;
  27298. CheckSource('TestPointer_Proc',
  27299. LinesToStr([ // statements
  27300. 'rtl.createClass(this, "TObject", null, function () {',
  27301. ' this.$init = function () {',
  27302. ' };',
  27303. ' this.$final = function () {',
  27304. ' };',
  27305. '});',
  27306. 'this.DoSome = function () {',
  27307. '};',
  27308. 'this.o = null;',
  27309. 'this.p = null;',
  27310. '']),
  27311. LinesToStr([ // $mod.$main
  27312. '$mod.p = $mod.DoSome;',
  27313. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  27314. '']));
  27315. end;
  27316. procedure TTestModule.TestPointer_AssignRecordFail;
  27317. begin
  27318. StartProgram(false);
  27319. Add('type');
  27320. Add(' TRec = record end;');
  27321. Add('var');
  27322. Add(' p: Pointer;');
  27323. Add(' r: TRec;');
  27324. Add('begin');
  27325. Add(' p:=r;');
  27326. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  27327. nIncompatibleTypesGotExpected);
  27328. ConvertProgram;
  27329. end;
  27330. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  27331. begin
  27332. StartProgram(false);
  27333. Add('type');
  27334. Add(' TArr = array[boolean] of longint;');
  27335. Add('var');
  27336. Add(' p: Pointer;');
  27337. Add(' a: TArr;');
  27338. Add('begin');
  27339. Add(' p:=a;');
  27340. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  27341. nIncompatibleTypesGotExpected);
  27342. ConvertProgram;
  27343. end;
  27344. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  27345. begin
  27346. StartProgram(false);
  27347. Add([
  27348. 'procedure DoIt(args: array of jsvalue); begin end;',
  27349. 'procedure DoAll; varargs; begin end;',
  27350. 'var',
  27351. ' v: jsvalue;',
  27352. 'begin',
  27353. ' DoIt([pointer(v)]);',
  27354. ' DoAll(pointer(v));',
  27355. '']);
  27356. ConvertProgram;
  27357. CheckSource('TestPointer_TypeCastJSValueToPointer',
  27358. LinesToStr([ // statements
  27359. 'this.DoIt = function (args) {',
  27360. '};',
  27361. 'this.DoAll = function () {',
  27362. '};',
  27363. 'this.v = undefined;',
  27364. '']),
  27365. LinesToStr([ // $mod.$main
  27366. '$mod.DoIt([$mod.v]);',
  27367. '$mod.DoAll($mod.v);',
  27368. '']));
  27369. end;
  27370. procedure TTestModule.TestPointer_NonRecordFail;
  27371. begin
  27372. StartProgram(false);
  27373. Add([
  27374. 'type',
  27375. ' p = ^longint;',
  27376. 'begin',
  27377. '']);
  27378. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  27379. ConvertProgram;
  27380. end;
  27381. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  27382. begin
  27383. StartProgram(false);
  27384. Add([
  27385. 'procedure DoIt(p: ^longint); begin end;',
  27386. 'begin',
  27387. '']);
  27388. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27389. ConvertProgram;
  27390. end;
  27391. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  27392. begin
  27393. StartProgram(false);
  27394. Add([
  27395. 'var p: ^longint;',
  27396. 'begin',
  27397. '']);
  27398. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27399. ConvertProgram;
  27400. end;
  27401. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  27402. begin
  27403. StartProgram(false);
  27404. Add([
  27405. 'function DoIt: ^longint; begin end;',
  27406. 'begin',
  27407. '']);
  27408. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27409. ConvertProgram;
  27410. end;
  27411. procedure TTestModule.TestPointer_AddrOperatorFail;
  27412. begin
  27413. StartProgram(false);
  27414. Add([
  27415. 'var i: longint;',
  27416. 'begin',
  27417. ' if @i=nil then ;',
  27418. '']);
  27419. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  27420. ConvertProgram;
  27421. end;
  27422. procedure TTestModule.TestPointer_ArrayParamsFail;
  27423. begin
  27424. StartProgram(false);
  27425. Add([
  27426. 'var',
  27427. ' p: Pointer;',
  27428. 'begin',
  27429. ' p:=p[1];',
  27430. '']);
  27431. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27432. ConvertProgram;
  27433. end;
  27434. procedure TTestModule.TestPointer_PointerAddFail;
  27435. begin
  27436. StartProgram(false);
  27437. Add([
  27438. 'var',
  27439. ' p: Pointer;',
  27440. 'begin',
  27441. ' p:=p+1;',
  27442. '']);
  27443. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27444. ConvertProgram;
  27445. end;
  27446. procedure TTestModule.TestPointer_IncPointerFail;
  27447. begin
  27448. StartProgram(false);
  27449. Add([
  27450. 'var',
  27451. ' p: Pointer;',
  27452. 'begin',
  27453. ' inc(p,1);',
  27454. '']);
  27455. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27456. nIncompatibleTypeArgNo);
  27457. ConvertProgram;
  27458. end;
  27459. procedure TTestModule.TestPointer_Record;
  27460. begin
  27461. StartProgram(false);
  27462. Add([
  27463. 'type',
  27464. ' TRec = record x: longint; end;',
  27465. ' PRec = ^TRec;',
  27466. 'var',
  27467. ' r: TRec;',
  27468. ' p: PRec;',
  27469. ' q: ^TRec;',
  27470. ' Ptr: pointer;',
  27471. 'begin',
  27472. ' new(p);',
  27473. ' p:=@r;',
  27474. ' r:=p^;',
  27475. ' r.x:=p^.x;',
  27476. ' p^.x:=r.x;',
  27477. ' if p^.x=3 then ;',
  27478. ' if 4=p^.x then ;',
  27479. ' dispose(p);',
  27480. ' new(q);',
  27481. ' dispose(q);',
  27482. ' Ptr:=p;',
  27483. ' p:=PRec(ptr);',
  27484. '']);
  27485. ConvertProgram;
  27486. CheckSource('TestPointer_Record',
  27487. LinesToStr([ // statements
  27488. 'rtl.recNewT(this, "TRec", function () {',
  27489. ' this.x = 0;',
  27490. ' this.$eq = function (b) {',
  27491. ' return this.x === b.x;',
  27492. ' };',
  27493. ' this.$assign = function (s) {',
  27494. ' this.x = s.x;',
  27495. ' return this;',
  27496. ' };',
  27497. '});',
  27498. 'this.r = this.TRec.$new();',
  27499. 'this.p = null;',
  27500. 'this.q = null;',
  27501. 'this.Ptr = null;',
  27502. '']),
  27503. LinesToStr([ // $mod.$main
  27504. '$mod.p = $mod.TRec.$new();',
  27505. '$mod.p = $mod.r;',
  27506. '$mod.r.$assign($mod.p);',
  27507. '$mod.r.x = $mod.p.x;',
  27508. '$mod.p.x = $mod.r.x;',
  27509. 'if ($mod.p.x === 3) ;',
  27510. 'if (4 === $mod.p.x) ;',
  27511. '$mod.p = null;',
  27512. '$mod.q = $mod.TRec.$new();',
  27513. '$mod.q = null;',
  27514. '$mod.Ptr = $mod.p;',
  27515. '$mod.p = $mod.Ptr;',
  27516. '']));
  27517. end;
  27518. procedure TTestModule.TestPointer_RecordArg;
  27519. begin
  27520. StartProgram(false);
  27521. Add([
  27522. '{$modeswitch autoderef}',
  27523. 'type',
  27524. ' TRec = record x: longint; end;',
  27525. ' PRec = ^TRec;',
  27526. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27527. 'begin',
  27528. ' a.x:=a.x;',
  27529. ' a^.x:=a^.x;',
  27530. ' with a^ do',
  27531. ' x:=x;',
  27532. 'end;',
  27533. 'function GetIt(p: PRec): PRec;',
  27534. 'begin',
  27535. ' p.x:=p.x;',
  27536. ' p^.x:=p^.x;',
  27537. ' with p^ do',
  27538. ' x:=x;',
  27539. 'end;',
  27540. 'var',
  27541. ' r: TRec;',
  27542. ' p: PRec;',
  27543. 'begin',
  27544. ' p:=GetIt(p);',
  27545. ' p^:=GetIt(@r)^;',
  27546. ' DoIt(p,p,p);',
  27547. ' DoIt(@r,p,p);',
  27548. '']);
  27549. ConvertProgram;
  27550. CheckSource('TestPointer_RecordArg',
  27551. LinesToStr([ // statements
  27552. 'rtl.recNewT(this, "TRec", function () {',
  27553. ' this.x = 0;',
  27554. ' this.$eq = function (b) {',
  27555. ' return this.x === b.x;',
  27556. ' };',
  27557. ' this.$assign = function (s) {',
  27558. ' this.x = s.x;',
  27559. ' return this;',
  27560. ' };',
  27561. '});',
  27562. 'this.DoIt = function (a, b, c) {',
  27563. ' var Result = $mod.TRec.$new();',
  27564. ' a.x = a.x;',
  27565. ' a.x = a.x;',
  27566. ' a.x = a.x;',
  27567. ' return Result;',
  27568. '};',
  27569. 'this.GetIt = function (p) {',
  27570. ' var Result = null;',
  27571. ' p.x = p.x;',
  27572. ' p.x = p.x;',
  27573. ' p.x = p.x;',
  27574. ' return Result;',
  27575. '};',
  27576. 'this.r = this.TRec.$new();',
  27577. 'this.p = null;',
  27578. '']),
  27579. LinesToStr([ // $mod.$main
  27580. '$mod.p = $mod.GetIt($mod.p);',
  27581. '$mod.p.$assign($mod.GetIt($mod.r));',
  27582. '$mod.DoIt($mod.p, {',
  27583. ' p: $mod,',
  27584. ' get: function () {',
  27585. ' return this.p.p;',
  27586. ' },',
  27587. ' set: function (v) {',
  27588. ' this.p.p = v;',
  27589. ' }',
  27590. '}, {',
  27591. ' p: $mod,',
  27592. ' get: function () {',
  27593. ' return this.p.p;',
  27594. ' },',
  27595. ' set: function (v) {',
  27596. ' this.p.p = v;',
  27597. ' }',
  27598. '});',
  27599. '$mod.DoIt($mod.r, {',
  27600. ' p: $mod,',
  27601. ' get: function () {',
  27602. ' return this.p.p;',
  27603. ' },',
  27604. ' set: function (v) {',
  27605. ' this.p.p = v;',
  27606. ' }',
  27607. '}, {',
  27608. ' p: $mod,',
  27609. ' get: function () {',
  27610. ' return this.p.p;',
  27611. ' },',
  27612. ' set: function (v) {',
  27613. ' this.p.p = v;',
  27614. ' }',
  27615. '});',
  27616. '']));
  27617. end;
  27618. procedure TTestModule.TestJSValue_AssignToJSValue;
  27619. begin
  27620. StartProgram(false);
  27621. Add('var');
  27622. Add(' v: jsvalue;');
  27623. Add(' i: longint;');
  27624. Add(' s: string;');
  27625. Add(' b: boolean;');
  27626. Add(' d: double;');
  27627. Add(' p: pointer;');
  27628. Add('begin');
  27629. Add(' v:=v;');
  27630. Add(' v:=1;');
  27631. Add(' v:=i;');
  27632. Add(' v:='''';');
  27633. Add(' v:=''c'';');
  27634. Add(' v:=''foo'';');
  27635. Add(' v:=s;');
  27636. Add(' v:=false;');
  27637. Add(' v:=true;');
  27638. Add(' v:=b;');
  27639. Add(' v:=0.1;');
  27640. Add(' v:=d;');
  27641. Add(' v:=nil;');
  27642. Add(' v:=p;');
  27643. ConvertProgram;
  27644. CheckSource('TestJSValue_AssignToJSValue',
  27645. LinesToStr([ // statements
  27646. 'this.v = undefined;',
  27647. 'this.i = 0;',
  27648. 'this.s = "";',
  27649. 'this.b = false;',
  27650. 'this.d = 0.0;',
  27651. 'this.p = null;',
  27652. '']),
  27653. LinesToStr([ // $mod.$main
  27654. '$mod.v = $mod.v;',
  27655. '$mod.v = 1;',
  27656. '$mod.v = $mod.i;',
  27657. '$mod.v = "";',
  27658. '$mod.v = "c";',
  27659. '$mod.v = "foo";',
  27660. '$mod.v = $mod.s;',
  27661. '$mod.v = false;',
  27662. '$mod.v = true;',
  27663. '$mod.v = $mod.b;',
  27664. '$mod.v = 0.1;',
  27665. '$mod.v = $mod.d;',
  27666. '$mod.v = null;',
  27667. '$mod.v = $mod.p;',
  27668. '']));
  27669. end;
  27670. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27671. begin
  27672. StartProgram(false);
  27673. Add('type');
  27674. Add(' integer = longint;');
  27675. Add(' TYesNo = boolean;');
  27676. Add(' TFloat = double;');
  27677. Add(' TCaption = string;');
  27678. Add(' TChar = char;');
  27679. Add('var');
  27680. Add(' v: jsvalue;');
  27681. Add(' i: integer;');
  27682. Add(' s: TCaption;');
  27683. Add(' b: TYesNo;');
  27684. Add(' d: TFloat;');
  27685. Add(' c: char;');
  27686. Add('begin');
  27687. Add(' i:=longint(v);');
  27688. Add(' i:=integer(v);');
  27689. Add(' s:=string(v);');
  27690. Add(' s:=TCaption(v);');
  27691. Add(' b:=boolean(v);');
  27692. Add(' b:=TYesNo(v);');
  27693. Add(' d:=double(v);');
  27694. Add(' d:=TFloat(v);');
  27695. Add(' c:=char(v);');
  27696. Add(' c:=TChar(v);');
  27697. ConvertProgram;
  27698. CheckSource('TestJSValue_TypeCastToBaseType',
  27699. LinesToStr([ // statements
  27700. 'this.v = undefined;',
  27701. 'this.i = 0;',
  27702. 'this.s = "";',
  27703. 'this.b = false;',
  27704. 'this.d = 0.0;',
  27705. 'this.c = "";',
  27706. '']),
  27707. LinesToStr([ // $mod.$main
  27708. '$mod.i = rtl.trunc($mod.v);',
  27709. '$mod.i = rtl.trunc($mod.v);',
  27710. '$mod.s = "" + $mod.v;',
  27711. '$mod.s = "" + $mod.v;',
  27712. '$mod.b = !($mod.v == false);',
  27713. '$mod.b = !($mod.v == false);',
  27714. '$mod.d = rtl.getNumber($mod.v);',
  27715. '$mod.d = rtl.getNumber($mod.v);',
  27716. '$mod.c = rtl.getChar($mod.v);',
  27717. '$mod.c = rtl.getChar($mod.v);',
  27718. '']));
  27719. end;
  27720. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27721. begin
  27722. StartProgram(false);
  27723. Add([
  27724. 'type',
  27725. ' TArr = array of word;',
  27726. ' TRec = record end;',
  27727. ' TSet = set of boolean;',
  27728. 'procedure Fly(v: jsvalue);',
  27729. 'begin',
  27730. 'end;',
  27731. 'var',
  27732. ' a: TArr;',
  27733. ' r: TRec;',
  27734. ' s: TSet;',
  27735. 'begin',
  27736. ' Fly(jsvalue(a));',
  27737. ' Fly(jsvalue(r));',
  27738. ' Fly(jsvalue(s));',
  27739. '']);
  27740. ConvertProgram;
  27741. CheckSource('TestJSValue_TypecastToJSValue',
  27742. LinesToStr([ // statements
  27743. 'rtl.recNewT(this, "TRec", function () {',
  27744. ' this.$eq = function (b) {',
  27745. ' return true;',
  27746. ' };',
  27747. ' this.$assign = function (s) {',
  27748. ' return this;',
  27749. ' };',
  27750. '});',
  27751. 'this.Fly = function (v) {',
  27752. '};',
  27753. 'this.a = [];',
  27754. 'this.r = this.TRec.$new();',
  27755. 'this.s = {};',
  27756. '']),
  27757. LinesToStr([ // $mod.$main
  27758. '$mod.Fly($mod.a);',
  27759. '$mod.Fly($mod.r);',
  27760. '$mod.Fly($mod.s);',
  27761. '']));
  27762. end;
  27763. procedure TTestModule.TestJSValue_Equal;
  27764. begin
  27765. StartProgram(false);
  27766. Add('type');
  27767. Add(' integer = longint;');
  27768. Add(' TYesNo = boolean;');
  27769. Add(' TFloat = double;');
  27770. Add(' TCaption = string;');
  27771. Add(' TChar = char;');
  27772. Add(' TMulti = JSValue;');
  27773. Add('var');
  27774. Add(' v: jsvalue;');
  27775. Add(' i: integer;');
  27776. Add(' s: TCaption;');
  27777. Add(' b: TYesNo;');
  27778. Add(' d: TFloat;');
  27779. Add(' c: char;');
  27780. Add(' m: TMulti;');
  27781. Add('begin');
  27782. Add(' b:=v=v;');
  27783. Add(' b:=v<>v;');
  27784. Add(' b:=v=1;');
  27785. Add(' b:=v<>1;');
  27786. Add(' b:=2=v;');
  27787. Add(' b:=2<>v;');
  27788. Add(' b:=v=i;');
  27789. Add(' b:=i=v;');
  27790. Add(' b:=v=nil;');
  27791. Add(' b:=nil=v;');
  27792. Add(' b:=v=false;');
  27793. Add(' b:=true=v;');
  27794. Add(' b:=v=b;');
  27795. Add(' b:=b=v;');
  27796. Add(' b:=v=s;');
  27797. Add(' b:=s=v;');
  27798. Add(' b:=v=''foo'';');
  27799. Add(' b:=''''=v;');
  27800. Add(' b:=v=d;');
  27801. Add(' b:=d=v;');
  27802. Add(' b:=v=3.4;');
  27803. Add(' b:=5.6=v;');
  27804. Add(' b:=v=c;');
  27805. Add(' b:=c=v;');
  27806. Add(' b:=m=m;');
  27807. Add(' b:=v=m;');
  27808. Add(' b:=m=v;');
  27809. ConvertProgram;
  27810. CheckSource('TestJSValue_Equal',
  27811. LinesToStr([ // statements
  27812. 'this.v = undefined;',
  27813. 'this.i = 0;',
  27814. 'this.s = "";',
  27815. 'this.b = false;',
  27816. 'this.d = 0.0;',
  27817. 'this.c = "";',
  27818. 'this.m = undefined;',
  27819. '']),
  27820. LinesToStr([ // $mod.$main
  27821. '$mod.b = $mod.v == $mod.v;',
  27822. '$mod.b = $mod.v != $mod.v;',
  27823. '$mod.b = $mod.v == 1;',
  27824. '$mod.b = $mod.v != 1;',
  27825. '$mod.b = 2 == $mod.v;',
  27826. '$mod.b = 2 != $mod.v;',
  27827. '$mod.b = $mod.v == $mod.i;',
  27828. '$mod.b = $mod.i == $mod.v;',
  27829. '$mod.b = $mod.v == null;',
  27830. '$mod.b = null == $mod.v;',
  27831. '$mod.b = $mod.v == false;',
  27832. '$mod.b = true == $mod.v;',
  27833. '$mod.b = $mod.v == $mod.b;',
  27834. '$mod.b = $mod.b == $mod.v;',
  27835. '$mod.b = $mod.v == $mod.s;',
  27836. '$mod.b = $mod.s == $mod.v;',
  27837. '$mod.b = $mod.v == "foo";',
  27838. '$mod.b = "" == $mod.v;',
  27839. '$mod.b = $mod.v == $mod.d;',
  27840. '$mod.b = $mod.d == $mod.v;',
  27841. '$mod.b = $mod.v == 3.4;',
  27842. '$mod.b = 5.6 == $mod.v;',
  27843. '$mod.b = $mod.v == $mod.c;',
  27844. '$mod.b = $mod.c == $mod.v;',
  27845. '$mod.b = $mod.m == $mod.m;',
  27846. '$mod.b = $mod.v == $mod.m;',
  27847. '$mod.b = $mod.m == $mod.v;',
  27848. '']));
  27849. end;
  27850. procedure TTestModule.TestJSValue_If;
  27851. begin
  27852. StartProgram(false);
  27853. Add([
  27854. 'procedure Fly(var u);',
  27855. 'begin',
  27856. ' if jsvalue(u) then ;',
  27857. 'end;',
  27858. 'var',
  27859. ' v: jsvalue;',
  27860. 'begin',
  27861. ' if v then ;',
  27862. ' while v do ;',
  27863. ' repeat until v;',
  27864. '']);
  27865. ConvertProgram;
  27866. CheckSource('TestJSValue_If',
  27867. LinesToStr([ // statements
  27868. 'this.Fly = function (u) {',
  27869. ' if (u.get()) ;',
  27870. '};',
  27871. 'this.v = undefined;',
  27872. '']),
  27873. LinesToStr([ // $mod.$main
  27874. 'if ($mod.v) ;',
  27875. 'while($mod.v){',
  27876. '};',
  27877. 'do{',
  27878. '} while(!$mod.v);',
  27879. '']));
  27880. end;
  27881. procedure TTestModule.TestJSValue_Not;
  27882. begin
  27883. StartProgram(false);
  27884. Add([
  27885. 'var',
  27886. ' v: jsvalue;',
  27887. ' b: boolean;',
  27888. 'begin',
  27889. ' b:=not v;',
  27890. ' if not v then ;',
  27891. ' while not v do ;',
  27892. ' repeat until not v;',
  27893. '']);
  27894. ConvertProgram;
  27895. CheckSource('TestJSValue_If',
  27896. LinesToStr([ // statements
  27897. 'this.v = undefined;',
  27898. 'this.b = false;',
  27899. '']),
  27900. LinesToStr([ // $mod.$main
  27901. '$mod.b=!$mod.v;',
  27902. 'if (!$mod.v) ;',
  27903. 'while(!$mod.v){',
  27904. '};',
  27905. 'do{',
  27906. '} while($mod.v);',
  27907. '']));
  27908. end;
  27909. procedure TTestModule.TestJSValue_Enum;
  27910. begin
  27911. StartProgram(false);
  27912. Add('type');
  27913. Add(' TColor = (red, blue);');
  27914. Add(' TRedBlue = TColor;');
  27915. Add('var');
  27916. Add(' v: jsvalue;');
  27917. Add(' e: TColor;');
  27918. Add('begin');
  27919. Add(' v:=e;');
  27920. Add(' v:=TColor(e);');
  27921. Add(' v:=TRedBlue(e);');
  27922. Add(' e:=TColor(v);');
  27923. Add(' e:=TRedBlue(v);');
  27924. ConvertProgram;
  27925. CheckSource('TestJSValue_Enum',
  27926. LinesToStr([ // statements
  27927. 'this.TColor = {',
  27928. ' "0": "red",',
  27929. ' red: 0,',
  27930. ' "1": "blue",',
  27931. ' blue: 1',
  27932. '};',
  27933. 'this.v = undefined;',
  27934. 'this.e = 0;',
  27935. '']),
  27936. LinesToStr([ // $mod.$main
  27937. '$mod.v = $mod.e;',
  27938. '$mod.v = $mod.e;',
  27939. '$mod.v = $mod.e;',
  27940. '$mod.e = $mod.v;',
  27941. '$mod.e = $mod.v;',
  27942. '']));
  27943. end;
  27944. procedure TTestModule.TestJSValue_ClassInstance;
  27945. begin
  27946. StartProgram(false);
  27947. Add([
  27948. 'type',
  27949. ' TObject = class',
  27950. ' end;',
  27951. ' TBirdObject = TObject;',
  27952. 'var',
  27953. ' v: jsvalue;',
  27954. ' o: TObject;',
  27955. 'begin',
  27956. ' v:=o;',
  27957. ' v:=TObject(o);',
  27958. ' v:=TBirdObject(o);',
  27959. ' o:=TObject(v);',
  27960. ' o:=TBirdObject(v);',
  27961. ' if v is TObject then ;',
  27962. '']);
  27963. ConvertProgram;
  27964. CheckSource('TestJSValue_ClassInstance',
  27965. LinesToStr([ // statements
  27966. 'rtl.createClass(this, "TObject", null, function () {',
  27967. ' this.$init = function () {',
  27968. ' };',
  27969. ' this.$final = function () {',
  27970. ' };',
  27971. '});',
  27972. 'this.v = undefined;',
  27973. 'this.o = null;',
  27974. '']),
  27975. LinesToStr([ // $mod.$main
  27976. '$mod.v = $mod.o;',
  27977. '$mod.v = $mod.o;',
  27978. '$mod.v = $mod.o;',
  27979. '$mod.o = rtl.getObject($mod.v);',
  27980. '$mod.o = rtl.getObject($mod.v);',
  27981. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27982. '']));
  27983. end;
  27984. procedure TTestModule.TestJSValue_ClassOf;
  27985. begin
  27986. StartProgram(false);
  27987. Add([
  27988. 'type',
  27989. ' TClass = class of TObject;',
  27990. ' TObject = class',
  27991. ' end;',
  27992. ' TBirds = class of TBird;',
  27993. ' TBird = class(TObject) end;',
  27994. 'var',
  27995. ' v: jsvalue;',
  27996. ' c: TClass;',
  27997. 'begin',
  27998. ' v:=c;',
  27999. ' v:=TObject;',
  28000. ' v:=TClass(c);',
  28001. ' v:=TBirds(c);',
  28002. ' c:=TClass(v);',
  28003. ' c:=TBirds(v);',
  28004. ' if v is TClass then ;',
  28005. '']);
  28006. ConvertProgram;
  28007. CheckSource('TestJSValue_ClassOf',
  28008. LinesToStr([ // statements
  28009. 'rtl.createClass(this, "TObject", null, function () {',
  28010. ' this.$init = function () {',
  28011. ' };',
  28012. ' this.$final = function () {',
  28013. ' };',
  28014. '});',
  28015. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28016. '});',
  28017. 'this.v = undefined;',
  28018. 'this.c = null;',
  28019. '']),
  28020. LinesToStr([ // $mod.$main
  28021. '$mod.v = $mod.c;',
  28022. '$mod.v = $mod.TObject;',
  28023. '$mod.v = $mod.c;',
  28024. '$mod.v = $mod.c;',
  28025. '$mod.c = rtl.getObject($mod.v);',
  28026. '$mod.c = rtl.getObject($mod.v);',
  28027. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  28028. '']));
  28029. end;
  28030. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  28031. begin
  28032. StartProgram(false);
  28033. Add([
  28034. 'type',
  28035. ' integer = longint;',
  28036. ' TArray = array of JSValue;',
  28037. ' TArrgh = tarray;',
  28038. ' TArrInt = array of integer;',
  28039. 'var',
  28040. ' v: jsvalue;',
  28041. ' TheArray: tarray = (1,''2'');',
  28042. ' Arr: tarrgh;',
  28043. ' i: integer;',
  28044. ' ArrInt: tarrint;',
  28045. 'begin',
  28046. ' arr:=thearray;',
  28047. ' thearray:=arr;',
  28048. ' setlength(arr,2);',
  28049. ' setlength(thearray,3);',
  28050. ' arr[4]:=v;',
  28051. ' arr[5]:=length(thearray);',
  28052. ' arr[6]:=nil;',
  28053. ' arr[7]:=thearray[8];',
  28054. ' arr[low(arr)]:=high(thearray);',
  28055. ' arr:=arrint;',
  28056. ' arrInt:=tarrint(arr);',
  28057. ' if TheArray = nil then ;',
  28058. ' if nil = TheArray then ;',
  28059. ' if TheArray <> nil then ;',
  28060. ' if nil <> TheArray then ;',
  28061. '']);
  28062. ConvertProgram;
  28063. CheckSource('TestJSValue_ArrayOfJSValue',
  28064. LinesToStr([ // statements
  28065. 'this.v = undefined;',
  28066. 'this.TheArray = [1, "2"];',
  28067. 'this.Arr = [];',
  28068. 'this.i = 0;',
  28069. 'this.ArrInt = [];',
  28070. '']),
  28071. LinesToStr([ // $mod.$main
  28072. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  28073. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  28074. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  28075. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  28076. '$mod.Arr[4] = $mod.v;',
  28077. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  28078. '$mod.Arr[6] = null;',
  28079. '$mod.Arr[7] = $mod.TheArray[8];',
  28080. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  28081. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  28082. '$mod.ArrInt = $mod.Arr;',
  28083. 'if (rtl.length($mod.TheArray) === 0) ;',
  28084. 'if (rtl.length($mod.TheArray) === 0) ;',
  28085. 'if (rtl.length($mod.TheArray) > 0) ;',
  28086. 'if (rtl.length($mod.TheArray) > 0) ;',
  28087. '']));
  28088. end;
  28089. procedure TTestModule.TestJSValue_ArrayLit;
  28090. begin
  28091. StartProgram(false);
  28092. Add([
  28093. 'type',
  28094. ' TFlag = (big,small);',
  28095. ' TArray = array of JSValue;',
  28096. ' TObject = class end;',
  28097. ' TClass = class of TObject;',
  28098. 'var',
  28099. ' v: jsvalue;',
  28100. ' a: TArray;',
  28101. ' o: TObject;',
  28102. 'begin',
  28103. ' a:=[];',
  28104. ' a:=[1];',
  28105. ' a:=[1,2];',
  28106. ' a:=[big];',
  28107. ' a:=[1,big];',
  28108. ' a:=[o,nil];',
  28109. '']);
  28110. ConvertProgram;
  28111. CheckSource('TestJSValue_ArrayLit',
  28112. LinesToStr([ // statements
  28113. 'this.TFlag = {',
  28114. ' "0": "big",',
  28115. ' big: 0,',
  28116. ' "1": "small",',
  28117. ' small: 1',
  28118. '};',
  28119. 'rtl.createClass(this, "TObject", null, function () {',
  28120. ' this.$init = function () {',
  28121. ' };',
  28122. ' this.$final = function () {',
  28123. ' };',
  28124. '});',
  28125. 'this.v = undefined;',
  28126. 'this.a = [];',
  28127. 'this.o = null;',
  28128. '']),
  28129. LinesToStr([ // $mod.$main
  28130. '$mod.a = [];',
  28131. '$mod.a = [1];',
  28132. '$mod.a = [1, 2];',
  28133. '$mod.a = [$mod.TFlag.big];',
  28134. '$mod.a = [1, $mod.TFlag.big];',
  28135. '$mod.a = [$mod.o, null];',
  28136. '']));
  28137. end;
  28138. procedure TTestModule.TestJSValue_Params;
  28139. begin
  28140. StartProgram(false);
  28141. Add('type');
  28142. Add(' integer = longint;');
  28143. Add(' TYesNo = boolean;');
  28144. Add(' TFloat = double;');
  28145. Add(' TCaption = string;');
  28146. Add(' TChar = char;');
  28147. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  28148. Add('var');
  28149. Add(' l: jsvalue;');
  28150. Add('begin');
  28151. Add(' a:=a;');
  28152. Add(' l:=b;');
  28153. Add(' c:=c;');
  28154. Add(' d:=d;');
  28155. Add(' Result:=l;');
  28156. Add('end;');
  28157. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  28158. Add('var');
  28159. Add(' v: jsvalue;');
  28160. Add(' i: integer;');
  28161. Add(' b: TYesNo;');
  28162. Add(' d: TFloat;');
  28163. Add(' s: TCaption;');
  28164. Add(' c: TChar;');
  28165. Add('begin');
  28166. Add(' v:=doit(v,v,v,v);');
  28167. Add(' i:=integer(dosome(i,i));');
  28168. Add(' b:=TYesNo(dosome(b,b));');
  28169. Add(' d:=TFloat(dosome(d,d));');
  28170. Add(' s:=TCaption(dosome(s,s));');
  28171. Add(' c:=TChar(dosome(c,c));');
  28172. ConvertProgram;
  28173. CheckSource('TestJSValue_Params',
  28174. LinesToStr([ // statements
  28175. 'this.DoIt = function (a, b, c, d) {',
  28176. ' var Result = undefined;',
  28177. ' var l = undefined;',
  28178. ' a = a;',
  28179. ' l = b;',
  28180. ' c.set(c.get());',
  28181. ' d.set(d.get());',
  28182. ' Result = l;',
  28183. ' return Result;',
  28184. '};',
  28185. 'this.DoSome = function (a, b) {',
  28186. ' var Result = undefined;',
  28187. ' return Result;',
  28188. '};',
  28189. 'this.v = undefined;',
  28190. 'this.i = 0;',
  28191. 'this.b = false;',
  28192. 'this.d = 0.0;',
  28193. 'this.s = "";',
  28194. 'this.c = "";',
  28195. '']),
  28196. LinesToStr([ // $mod.$main
  28197. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  28198. ' p: $mod,',
  28199. ' get: function () {',
  28200. ' return this.p.v;',
  28201. ' },',
  28202. ' set: function (v) {',
  28203. ' this.p.v = v;',
  28204. ' }',
  28205. '}, {',
  28206. ' p: $mod,',
  28207. ' get: function () {',
  28208. ' return this.p.v;',
  28209. ' },',
  28210. ' set: function (v) {',
  28211. ' this.p.v = v;',
  28212. ' }',
  28213. '});',
  28214. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  28215. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  28216. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  28217. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  28218. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  28219. '']));
  28220. end;
  28221. procedure TTestModule.TestJSValue_UntypedParam;
  28222. begin
  28223. StartProgram(false);
  28224. Add('function DoIt(const a; var b; out c): jsvalue;');
  28225. Add('begin');
  28226. Add(' Result:=a;');
  28227. Add(' Result:=b;');
  28228. Add(' Result:=c;');
  28229. Add(' b:=Result;');
  28230. Add(' c:=Result;');
  28231. Add('end;');
  28232. Add('var i: longint;');
  28233. Add('begin');
  28234. Add(' doit(i,i,i);');
  28235. ConvertProgram;
  28236. CheckSource('TestJSValue_UntypedParam',
  28237. LinesToStr([ // statements
  28238. 'this.DoIt = function (a, b, c) {',
  28239. ' var Result = undefined;',
  28240. ' Result = a;',
  28241. ' Result = b.get();',
  28242. ' Result = c.get();',
  28243. ' b.set(Result);',
  28244. ' c.set(Result);',
  28245. ' return Result;',
  28246. '};',
  28247. 'this.i = 0;',
  28248. '']),
  28249. LinesToStr([ // $mod.$main
  28250. '$mod.DoIt($mod.i, {',
  28251. ' p: $mod,',
  28252. ' get: function () {',
  28253. ' return this.p.i;',
  28254. ' },',
  28255. ' set: function (v) {',
  28256. ' this.p.i = v;',
  28257. ' }',
  28258. '}, {',
  28259. ' p: $mod,',
  28260. ' get: function () {',
  28261. ' return this.p.i;',
  28262. ' },',
  28263. ' set: function (v) {',
  28264. ' this.p.i = v;',
  28265. ' }',
  28266. '});',
  28267. '']));
  28268. end;
  28269. procedure TTestModule.TestJSValue_FuncResultType;
  28270. begin
  28271. StartProgram(false);
  28272. Add('type');
  28273. Add(' integer = longint;');
  28274. Add(' TJSValueArray = array of JSValue;');
  28275. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  28276. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  28277. Add('begin');
  28278. Add(' while Compare(P,aList[0])>0 do ;');
  28279. Add('end;');
  28280. Add('var');
  28281. Add(' Compare: TListSortCompare;');
  28282. Add(' V: JSValue;');
  28283. Add(' i: integer;');
  28284. Add('begin');
  28285. Add(' if Compare(V,V)>0 then ;');
  28286. Add(' if Compare(i,i)>1 then ;');
  28287. Add(' if Compare(nil,false)>2 then ;');
  28288. Add(' if Compare(1,true)>3 then ;');
  28289. ConvertProgram;
  28290. CheckSource('TestJSValue_UntypedParam',
  28291. LinesToStr([ // statements
  28292. 'this.Sort = function (P, aList, Compare) {',
  28293. ' while (Compare(P, aList[0]) > 0) {',
  28294. ' };',
  28295. '};',
  28296. 'this.Compare = null;',
  28297. 'this.V = undefined;',
  28298. 'this.i = 0;',
  28299. '']),
  28300. LinesToStr([ // $mod.$main
  28301. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  28302. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  28303. 'if ($mod.Compare(null, false) > 2) ;',
  28304. 'if ($mod.Compare(1, true) > 3) ;',
  28305. '']));
  28306. end;
  28307. procedure TTestModule.TestJSValue_ProcType_Assign;
  28308. begin
  28309. StartProgram(false);
  28310. Add('type');
  28311. Add(' integer = longint;');
  28312. Add(' TObject = class');
  28313. Add(' class function GetGlob: integer;');
  28314. Add(' function Getter: integer;');
  28315. Add(' end;');
  28316. Add('class function TObject.GetGlob: integer;');
  28317. Add('var v1: jsvalue;');
  28318. Add('begin');
  28319. Add(' v1:=@GetGlob;');
  28320. Add(' v1:[email protected];');
  28321. Add('end;');
  28322. Add('function TObject.Getter: integer;');
  28323. Add('var v2: jsvalue;');
  28324. Add('begin');
  28325. Add(' v2:=@Getter;');
  28326. Add(' v2:[email protected];');
  28327. Add(' v2:=@GetGlob;');
  28328. Add(' v2:[email protected];');
  28329. Add('end;');
  28330. Add('function GetIt(i: integer): integer;');
  28331. Add('var v3: jsvalue;');
  28332. Add('begin');
  28333. Add(' v3:=@GetIt;');
  28334. Add('end;');
  28335. Add('var');
  28336. Add(' V: JSValue;');
  28337. Add(' o: TObject;');
  28338. Add('begin');
  28339. Add(' v:=@GetIt;');
  28340. Add(' v:[email protected];');
  28341. Add(' v:[email protected];');
  28342. ConvertProgram;
  28343. CheckSource('TestJSValue_ProcType_Assign',
  28344. LinesToStr([ // statements
  28345. 'rtl.createClass(this, "TObject", null, function () {',
  28346. ' this.$init = function () {',
  28347. ' };',
  28348. ' this.$final = function () {',
  28349. ' };',
  28350. ' this.GetGlob = function () {',
  28351. ' var Result = 0;',
  28352. ' var v1 = undefined;',
  28353. ' v1 = rtl.createCallback(this, "GetGlob");',
  28354. ' v1 = rtl.createCallback(this, "GetGlob");',
  28355. ' return Result;',
  28356. ' };',
  28357. ' this.Getter = function () {',
  28358. ' var Result = 0;',
  28359. ' var v2 = undefined;',
  28360. ' v2 = rtl.createCallback(this, "Getter");',
  28361. ' v2 = rtl.createCallback(this, "Getter");',
  28362. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28363. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28364. ' return Result;',
  28365. ' };',
  28366. '});',
  28367. 'this.GetIt = function (i) {',
  28368. ' var Result = 0;',
  28369. ' var v3 = undefined;',
  28370. ' v3 = $mod.GetIt;',
  28371. ' return Result;',
  28372. '};',
  28373. 'this.V = undefined;',
  28374. 'this.o = null;',
  28375. '']),
  28376. LinesToStr([ // $mod.$main
  28377. '$mod.V = $mod.GetIt;',
  28378. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  28379. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  28380. '']));
  28381. end;
  28382. procedure TTestModule.TestJSValue_ProcType_Equal;
  28383. begin
  28384. StartProgram(false);
  28385. Add('type');
  28386. Add(' integer = longint;');
  28387. Add(' TObject = class');
  28388. Add(' class function GetGlob: integer;');
  28389. Add(' function Getter: integer;');
  28390. Add(' end;');
  28391. Add('class function TObject.GetGlob: integer;');
  28392. Add('var v1: jsvalue;');
  28393. Add('begin');
  28394. Add(' if v1=@GetGlob then;');
  28395. Add(' if [email protected] then ;');
  28396. Add('end;');
  28397. Add('function TObject.Getter: integer;');
  28398. Add('var v2: jsvalue;');
  28399. Add('begin');
  28400. Add(' if v2=@Getter then;');
  28401. Add(' if [email protected] then ;');
  28402. Add(' if v2=@GetGlob then;');
  28403. Add(' if [email protected] then;');
  28404. Add('end;');
  28405. Add('function GetIt(i: integer): integer;');
  28406. Add('var v3: jsvalue;');
  28407. Add('begin');
  28408. Add(' if v3=@GetIt then;');
  28409. Add('end;');
  28410. Add('var');
  28411. Add(' V: JSValue;');
  28412. Add(' o: TObject;');
  28413. Add('begin');
  28414. Add(' if v=@GetIt then;');
  28415. Add(' if [email protected] then;');
  28416. Add(' if [email protected] then;');
  28417. Add(' if @GetIt=v then;');
  28418. Add(' if @o.Getter=v then;');
  28419. Add(' if @o.GetGlob=v then;');
  28420. ConvertProgram;
  28421. CheckSource('TestJSValue_ProcType_Equal',
  28422. LinesToStr([ // statements
  28423. 'rtl.createClass(this, "TObject", null, function () {',
  28424. ' this.$init = function () {',
  28425. ' };',
  28426. ' this.$final = function () {',
  28427. ' };',
  28428. ' this.GetGlob = function () {',
  28429. ' var Result = 0;',
  28430. ' var v1 = undefined;',
  28431. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28432. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28433. ' return Result;',
  28434. ' };',
  28435. ' this.Getter = function () {',
  28436. ' var Result = 0;',
  28437. ' var v2 = undefined;',
  28438. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28439. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28440. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28441. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28442. ' return Result;',
  28443. ' };',
  28444. '});',
  28445. 'this.GetIt = function (i) {',
  28446. ' var Result = 0;',
  28447. ' var v3 = undefined;',
  28448. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28449. ' return Result;',
  28450. '};',
  28451. 'this.V = undefined;',
  28452. 'this.o = null;',
  28453. '']),
  28454. LinesToStr([ // $mod.$main
  28455. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28456. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28457. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28458. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28459. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28460. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28461. '']));
  28462. end;
  28463. procedure TTestModule.TestJSValue_ProcType_Param;
  28464. begin
  28465. StartProgram(false);
  28466. Add([
  28467. 'type',
  28468. ' variant = jsvalue;',
  28469. ' TArrVariant = array of variant;',
  28470. ' TArrVar2 = TArrVariant;',
  28471. ' TFuncInt = function: longint;',
  28472. 'function GetIt: longint;',
  28473. 'begin',
  28474. 'end;',
  28475. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28476. 'var v: variant;',
  28477. 'begin',
  28478. ' v:=arr[1];',
  28479. 'end;',
  28480. 'var s: string;',
  28481. 'begin',
  28482. ' DoIt(GetIt,[]);',
  28483. ' DoIt(@GetIt,[]);',
  28484. ' DoIt(1,[s,GetIt]);',
  28485. ' DoIt(1,[s,@GetIt]);',
  28486. '']);
  28487. ConvertProgram;
  28488. CheckSource('TestJSValue_ProcType_Param',
  28489. LinesToStr([ // statements
  28490. 'this.GetIt = function () {',
  28491. ' var Result = 0;',
  28492. ' return Result;',
  28493. '};',
  28494. 'this.DoIt = function (p, Arr) {',
  28495. ' var v = undefined;',
  28496. ' v = Arr[1];',
  28497. '};',
  28498. 'this.s = "";',
  28499. '']),
  28500. LinesToStr([ // $mod.$main
  28501. '$mod.DoIt($mod.GetIt(), []);',
  28502. '$mod.DoIt($mod.GetIt, []);',
  28503. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28504. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28505. '']));
  28506. end;
  28507. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28508. begin
  28509. StartProgram(false);
  28510. Add([
  28511. 'var',
  28512. ' v: JSValue;',
  28513. ' p: Pointer;',
  28514. 'begin',
  28515. ' p:=v;',
  28516. '']);
  28517. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28518. nIncompatibleTypesGotExpected);
  28519. ConvertProgram;
  28520. end;
  28521. procedure TTestModule.TestJSValue_OverloadDouble;
  28522. begin
  28523. StartProgram(false);
  28524. Add([
  28525. 'type',
  28526. ' integer = longint;',
  28527. ' tdatetime = double;',
  28528. 'procedure DoIt(d: double); begin end;',
  28529. 'procedure DoIt(v: jsvalue); begin end;',
  28530. 'var',
  28531. ' d: double;',
  28532. ' dt: tdatetime;',
  28533. ' i: integer;',
  28534. ' b: byte;',
  28535. ' shi: shortint;',
  28536. ' w: word;',
  28537. ' smi: smallint;',
  28538. ' lw: longword;',
  28539. ' li: longint;',
  28540. ' ni: nativeint;',
  28541. ' nu: nativeuint;',
  28542. 'begin',
  28543. ' DoIt(d);',
  28544. ' DoIt(dt);',
  28545. ' DoIt(i);',
  28546. ' DoIt(b);',
  28547. ' DoIt(shi);',
  28548. ' DoIt(w);',
  28549. ' DoIt(smi);',
  28550. ' DoIt(lw);',
  28551. ' DoIt(li);',
  28552. ' DoIt(ni);',
  28553. ' DoIt(nu);',
  28554. '']);
  28555. ConvertProgram;
  28556. CheckSource('TestJSValue_OverloadDouble',
  28557. LinesToStr([ // statements
  28558. 'this.DoIt = function (d) {',
  28559. '};',
  28560. 'this.DoIt$1 = function (v) {',
  28561. '};',
  28562. 'this.d = 0.0;',
  28563. 'this.dt = 0.0;',
  28564. 'this.i = 0;',
  28565. 'this.b = 0;',
  28566. 'this.shi = 0;',
  28567. 'this.w = 0;',
  28568. 'this.smi = 0;',
  28569. 'this.lw = 0;',
  28570. 'this.li = 0;',
  28571. 'this.ni = 0;',
  28572. 'this.nu = 0;',
  28573. '']),
  28574. LinesToStr([ // $mod.$main
  28575. '$mod.DoIt($mod.d);',
  28576. '$mod.DoIt($mod.dt);',
  28577. '$mod.DoIt$1($mod.i);',
  28578. '$mod.DoIt$1($mod.b);',
  28579. '$mod.DoIt$1($mod.shi);',
  28580. '$mod.DoIt$1($mod.w);',
  28581. '$mod.DoIt$1($mod.smi);',
  28582. '$mod.DoIt$1($mod.lw);',
  28583. '$mod.DoIt$1($mod.li);',
  28584. '$mod.DoIt$1($mod.ni);',
  28585. '$mod.DoIt$1($mod.nu);',
  28586. '']));
  28587. end;
  28588. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28589. begin
  28590. StartProgram(false);
  28591. Add([
  28592. 'type',
  28593. ' integer = longint;',
  28594. ' int53 = nativeint;',
  28595. ' tdatetime = double;',
  28596. 'procedure DoIt(n: nativeint); begin end;',
  28597. 'procedure DoIt(v: jsvalue); begin end;',
  28598. 'var',
  28599. ' d: double;',
  28600. ' dt: tdatetime;',
  28601. ' i: integer;',
  28602. ' b: byte;',
  28603. ' shi: shortint;',
  28604. ' w: word;',
  28605. ' smi: smallint;',
  28606. ' lw: longword;',
  28607. ' li: longint;',
  28608. ' ni: nativeint;',
  28609. ' nu: nativeuint;',
  28610. 'begin',
  28611. ' DoIt(d);',
  28612. ' DoIt(dt);',
  28613. ' DoIt(i);',
  28614. ' DoIt(b);',
  28615. ' DoIt(shi);',
  28616. ' DoIt(w);',
  28617. ' DoIt(smi);',
  28618. ' DoIt(lw);',
  28619. ' DoIt(li);',
  28620. ' DoIt(ni);',
  28621. ' DoIt(nu);',
  28622. '']);
  28623. ConvertProgram;
  28624. CheckSource('TestJSValue_OverloadNativeInt',
  28625. LinesToStr([ // statements
  28626. 'this.DoIt = function (n) {',
  28627. '};',
  28628. 'this.DoIt$1 = function (v) {',
  28629. '};',
  28630. 'this.d = 0.0;',
  28631. 'this.dt = 0.0;',
  28632. 'this.i = 0;',
  28633. 'this.b = 0;',
  28634. 'this.shi = 0;',
  28635. 'this.w = 0;',
  28636. 'this.smi = 0;',
  28637. 'this.lw = 0;',
  28638. 'this.li = 0;',
  28639. 'this.ni = 0;',
  28640. 'this.nu = 0;',
  28641. '']),
  28642. LinesToStr([ // $mod.$main
  28643. '$mod.DoIt$1($mod.d);',
  28644. '$mod.DoIt$1($mod.dt);',
  28645. '$mod.DoIt($mod.i);',
  28646. '$mod.DoIt($mod.b);',
  28647. '$mod.DoIt($mod.shi);',
  28648. '$mod.DoIt($mod.w);',
  28649. '$mod.DoIt($mod.smi);',
  28650. '$mod.DoIt($mod.lw);',
  28651. '$mod.DoIt($mod.li);',
  28652. '$mod.DoIt($mod.ni);',
  28653. '$mod.DoIt($mod.nu);',
  28654. '']));
  28655. end;
  28656. procedure TTestModule.TestJSValue_OverloadWord;
  28657. begin
  28658. StartProgram(false);
  28659. Add([
  28660. 'type',
  28661. ' integer = longint;',
  28662. ' int53 = nativeint;',
  28663. ' tdatetime = double;',
  28664. 'procedure DoIt(w: word); begin end;',
  28665. 'procedure DoIt(v: jsvalue); begin end;',
  28666. 'var',
  28667. ' d: double;',
  28668. ' dt: tdatetime;',
  28669. ' i: integer;',
  28670. ' b: byte;',
  28671. ' shi: shortint;',
  28672. ' w: word;',
  28673. ' smi: smallint;',
  28674. ' lw: longword;',
  28675. ' li: longint;',
  28676. ' ni: nativeint;',
  28677. ' nu: nativeuint;',
  28678. 'begin',
  28679. ' DoIt(d);',
  28680. ' DoIt(dt);',
  28681. ' DoIt(i);',
  28682. ' DoIt(b);',
  28683. ' DoIt(shi);',
  28684. ' DoIt(w);',
  28685. ' DoIt(smi);',
  28686. ' DoIt(lw);',
  28687. ' DoIt(li);',
  28688. ' DoIt(ni);',
  28689. ' DoIt(nu);',
  28690. '']);
  28691. ConvertProgram;
  28692. CheckSource('TestJSValue_OverloadWord',
  28693. LinesToStr([ // statements
  28694. 'this.DoIt = function (w) {',
  28695. '};',
  28696. 'this.DoIt$1 = function (v) {',
  28697. '};',
  28698. 'this.d = 0.0;',
  28699. 'this.dt = 0.0;',
  28700. 'this.i = 0;',
  28701. 'this.b = 0;',
  28702. 'this.shi = 0;',
  28703. 'this.w = 0;',
  28704. 'this.smi = 0;',
  28705. 'this.lw = 0;',
  28706. 'this.li = 0;',
  28707. 'this.ni = 0;',
  28708. 'this.nu = 0;',
  28709. '']),
  28710. LinesToStr([ // $mod.$main
  28711. '$mod.DoIt$1($mod.d);',
  28712. '$mod.DoIt$1($mod.dt);',
  28713. '$mod.DoIt$1($mod.i);',
  28714. '$mod.DoIt($mod.b);',
  28715. '$mod.DoIt($mod.shi);',
  28716. '$mod.DoIt($mod.w);',
  28717. '$mod.DoIt$1($mod.smi);',
  28718. '$mod.DoIt$1($mod.lw);',
  28719. '$mod.DoIt$1($mod.li);',
  28720. '$mod.DoIt$1($mod.ni);',
  28721. '$mod.DoIt$1($mod.nu);',
  28722. '']));
  28723. end;
  28724. procedure TTestModule.TestJSValue_OverloadString;
  28725. begin
  28726. StartProgram(false);
  28727. Add([
  28728. 'type',
  28729. ' uni = string;',
  28730. ' WChar = char;',
  28731. 'procedure DoIt(s: string); begin end;',
  28732. 'procedure DoIt(v: jsvalue); begin end;',
  28733. 'var',
  28734. ' s: string;',
  28735. ' c: char;',
  28736. ' u: uni;',
  28737. 'begin',
  28738. ' DoIt(s);',
  28739. ' DoIt(c);',
  28740. ' DoIt(u);',
  28741. '']);
  28742. ConvertProgram;
  28743. CheckSource('TestJSValue_OverloadString',
  28744. LinesToStr([ // statements
  28745. 'this.DoIt = function (s) {',
  28746. '};',
  28747. 'this.DoIt$1 = function (v) {',
  28748. '};',
  28749. 'this.s = "";',
  28750. 'this.c = "";',
  28751. 'this.u = "";',
  28752. '']),
  28753. LinesToStr([ // $mod.$main
  28754. '$mod.DoIt($mod.s);',
  28755. '$mod.DoIt($mod.c);',
  28756. '$mod.DoIt($mod.u);',
  28757. '']));
  28758. end;
  28759. procedure TTestModule.TestJSValue_OverloadChar;
  28760. begin
  28761. StartProgram(false);
  28762. Add([
  28763. 'type',
  28764. ' uni = string;',
  28765. ' WChar = char;',
  28766. 'procedure DoIt(c: char); begin end;',
  28767. 'procedure DoIt(v: jsvalue); begin end;',
  28768. 'var',
  28769. ' s: string;',
  28770. ' c: char;',
  28771. ' u: uni;',
  28772. 'begin',
  28773. ' DoIt(s);',
  28774. ' DoIt(c);',
  28775. ' DoIt(u);',
  28776. '']);
  28777. ConvertProgram;
  28778. CheckSource('TestJSValue_OverloadChar',
  28779. LinesToStr([ // statements
  28780. 'this.DoIt = function (c) {',
  28781. '};',
  28782. 'this.DoIt$1 = function (v) {',
  28783. '};',
  28784. 'this.s = "";',
  28785. 'this.c = "";',
  28786. 'this.u = "";',
  28787. '']),
  28788. LinesToStr([ // $mod.$main
  28789. '$mod.DoIt$1($mod.s);',
  28790. '$mod.DoIt($mod.c);',
  28791. '$mod.DoIt$1($mod.u);',
  28792. '']));
  28793. end;
  28794. procedure TTestModule.TestJSValue_OverloadPointer;
  28795. begin
  28796. StartProgram(false);
  28797. Add([
  28798. 'type',
  28799. ' TObject = class end;',
  28800. 'procedure DoIt(p: pointer); begin end;',
  28801. 'procedure DoIt(v: jsvalue); begin end;',
  28802. 'var',
  28803. ' o: TObject;',
  28804. 'begin',
  28805. ' DoIt(o);',
  28806. '']);
  28807. ConvertProgram;
  28808. CheckSource('TestJSValue_OverloadPointer',
  28809. LinesToStr([ // statements
  28810. 'rtl.createClass(this, "TObject", null, function () {',
  28811. ' this.$init = function () {',
  28812. ' };',
  28813. ' this.$final = function () {',
  28814. ' };',
  28815. '});',
  28816. 'this.DoIt = function (p) {',
  28817. '};',
  28818. 'this.DoIt$1 = function (v) {',
  28819. '};',
  28820. 'this.o = null;',
  28821. '']),
  28822. LinesToStr([ // $mod.$main
  28823. '$mod.DoIt($mod.o);',
  28824. '']));
  28825. end;
  28826. procedure TTestModule.TestJSValue_ForIn;
  28827. begin
  28828. StartProgram(false);
  28829. Add([
  28830. 'var',
  28831. ' v: JSValue;',
  28832. ' key: string;',
  28833. 'begin',
  28834. ' for key in v do begin',
  28835. ' if key=''abc'' then ;',
  28836. ' end;',
  28837. '']);
  28838. ConvertProgram;
  28839. CheckSource('TestJSValue_ForIn',
  28840. LinesToStr([ // statements
  28841. 'this.v = undefined;',
  28842. 'this.key = "";',
  28843. '']),
  28844. LinesToStr([ // $mod.$main
  28845. 'for ($mod.key in $mod.v) {',
  28846. ' if ($mod.key === "abc") ;',
  28847. '};',
  28848. '']));
  28849. end;
  28850. procedure TTestModule.TestRTTI_IntRange;
  28851. begin
  28852. WithTypeInfo:=true;
  28853. StartProgram(true,[supTypeInfo]);
  28854. Add([
  28855. '{$modeswitch externalclass}',
  28856. 'type',
  28857. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28858. ' TColor = type TGraphicsColor;',
  28859. 'var',
  28860. ' p: TTypeInfo;',
  28861. ' k: TTypeKind;',
  28862. 'begin',
  28863. ' p:=typeinfo(TGraphicsColor);',
  28864. ' p:=typeinfo(TColor);',
  28865. ' k:=GetTypeKind(TGraphicsColor);',
  28866. ' k:=GetTypeKind(TColor);',
  28867. '']);
  28868. ConvertProgram;
  28869. CheckSource('TestRTTI_IntRange',
  28870. LinesToStr([ // statements
  28871. 'this.$rtti.$Int("TGraphicsColor", {',
  28872. ' minvalue: -2147483648,',
  28873. ' maxvalue: 2147483647,',
  28874. ' ordtype: 4',
  28875. '});',
  28876. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28877. 'this.p = null;',
  28878. 'this.k = 0;',
  28879. '']),
  28880. LinesToStr([ // $mod.$main
  28881. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28882. '$mod.p = $mod.$rtti["TColor"];',
  28883. '$mod.k = 1;',
  28884. '$mod.k = 1;',
  28885. '']));
  28886. end;
  28887. procedure TTestModule.TestRTTI_Double;
  28888. begin
  28889. WithTypeInfo:=true;
  28890. StartProgram(true,[supTypeInfo]);
  28891. Add([
  28892. '{$modeswitch externalclass}',
  28893. 'type',
  28894. ' TFloat = type double;',
  28895. 'var',
  28896. ' p: TTypeInfo;',
  28897. 'begin',
  28898. ' p:=typeinfo(double);',
  28899. ' p:=typeinfo(TFloat);',
  28900. '']);
  28901. ConvertProgram;
  28902. CheckSource('TestRTTI_Double',
  28903. LinesToStr([ // statements
  28904. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28905. 'this.p = null;',
  28906. '']),
  28907. LinesToStr([ // $mod.$main
  28908. '$mod.p = rtl.double;',
  28909. '$mod.p = $mod.$rtti["TFloat"];',
  28910. '']));
  28911. end;
  28912. procedure TTestModule.TestRTTI_ProcType;
  28913. begin
  28914. WithTypeInfo:=true;
  28915. StartProgram(false);
  28916. Add('type');
  28917. Add(' TProcA = procedure;');
  28918. Add(' TMethodB = procedure of object;');
  28919. Add(' TProcC = procedure; varargs;');
  28920. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28921. Add(' TProcE = function: nativeint;');
  28922. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28923. Add('var p: pointer;');
  28924. Add('begin');
  28925. Add(' p:=typeinfo(tproca);');
  28926. ConvertProgram;
  28927. CheckSource('TestRTTI_ProcType',
  28928. LinesToStr([ // statements
  28929. 'this.$rtti.$ProcVar("TProcA", {',
  28930. ' procsig: rtl.newTIProcSig([])',
  28931. '});',
  28932. 'this.$rtti.$MethodVar("TMethodB", {',
  28933. ' procsig: rtl.newTIProcSig([]),',
  28934. ' methodkind: 0',
  28935. '});',
  28936. 'this.$rtti.$ProcVar("TProcC", {',
  28937. ' procsig: rtl.newTIProcSig([], null, 2)',
  28938. '});',
  28939. 'this.$rtti.$ProcVar("TProcD", {',
  28940. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28941. '});',
  28942. 'this.$rtti.$ProcVar("TProcE", {',
  28943. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  28944. '});',
  28945. 'this.$rtti.$ProcVar("TProcF", {',
  28946. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28947. '});',
  28948. 'this.p = null;',
  28949. '']),
  28950. LinesToStr([ // $mod.$main
  28951. '$mod.p = $mod.$rtti["TProcA"];',
  28952. '']));
  28953. end;
  28954. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28955. begin
  28956. WithTypeInfo:=true;
  28957. AddModuleWithIntfImplSrc('unit2.pas',
  28958. LinesToStr([
  28959. 'type',
  28960. ' TObject = class end;'
  28961. ]),
  28962. '');
  28963. StartUnit(true);
  28964. Add('interface');
  28965. Add('uses unit2;');
  28966. Add('type');
  28967. Add(' TProcA = function(o: tobject): tobject;');
  28968. Add('implementation');
  28969. Add('type');
  28970. Add(' TProcB = function(o: tobject): tobject;');
  28971. Add('var p: Pointer;');
  28972. Add('initialization');
  28973. Add(' p:=typeinfo(tproca);');
  28974. Add(' p:=typeinfo(tprocb);');
  28975. ConvertUnit;
  28976. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28977. LinesToStr([ // statements
  28978. 'var $impl = $mod.$impl;',
  28979. 'this.$rtti.$ProcVar("TProcA", {',
  28980. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28981. '});',
  28982. '']),
  28983. LinesToStr([ // this.$init
  28984. '$impl.p = $mod.$rtti["TProcA"];',
  28985. '$impl.p = $mod.$rtti["TProcB"];',
  28986. '']),
  28987. LinesToStr([ // implementation
  28988. '$mod.$rtti.$ProcVar("TProcB", {',
  28989. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28990. '});',
  28991. '$impl.p = null;',
  28992. '']) );
  28993. end;
  28994. procedure TTestModule.TestRTTI_EnumAndSetType;
  28995. begin
  28996. WithTypeInfo:=true;
  28997. StartProgram(false);
  28998. Add('type');
  28999. Add(' TFlag = (light,dark);');
  29000. Add(' TFlags = set of TFlag;');
  29001. Add(' TProc = function(f: TFlags): TFlag;');
  29002. Add('var p: pointer;');
  29003. Add('begin');
  29004. Add(' p:=typeinfo(tflag);');
  29005. Add(' p:=typeinfo(tflags);');
  29006. ConvertProgram;
  29007. CheckSource('TestRTTI_EnumAndType',
  29008. LinesToStr([ // statements
  29009. 'this.TFlag = {',
  29010. ' "0": "light",',
  29011. ' light: 0,',
  29012. ' "1": "dark",',
  29013. ' dark: 1',
  29014. '};',
  29015. 'this.$rtti.$Enum("TFlag", {',
  29016. ' minvalue: 0,',
  29017. ' maxvalue: 1,',
  29018. ' ordtype: 1,',
  29019. ' enumtype: this.TFlag',
  29020. '});',
  29021. 'this.$rtti.$Set("TFlags", {',
  29022. ' comptype: this.$rtti["TFlag"]',
  29023. '});',
  29024. 'this.$rtti.$ProcVar("TProc", {',
  29025. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  29026. '});',
  29027. 'this.p = null;',
  29028. '']),
  29029. LinesToStr([ // $mod.$main
  29030. '$mod.p = $mod.$rtti["TFlag"];',
  29031. '$mod.p = $mod.$rtti["TFlags"];',
  29032. '']));
  29033. end;
  29034. procedure TTestModule.TestRTTI_EnumRange;
  29035. begin
  29036. WithTypeInfo:=true;
  29037. StartProgram(false);
  29038. Add([
  29039. 'type',
  29040. ' TCol = (red,green,blue);',
  29041. ' TColRg = green..blue;',
  29042. ' TSetOfColRg = set of TColRg;',
  29043. 'var p: pointer;',
  29044. 'begin',
  29045. ' p:=typeinfo(tcolrg);',
  29046. ' p:=typeinfo(tsetofcolrg);',
  29047. '']);
  29048. ConvertProgram;
  29049. end;
  29050. procedure TTestModule.TestRTTI_AnonymousEnumType;
  29051. begin
  29052. WithTypeInfo:=true;
  29053. StartProgram(false);
  29054. Add('type');
  29055. Add(' TFlags = set of (red, green);');
  29056. Add('var');
  29057. Add(' f: TFlags;');
  29058. Add('begin');
  29059. Add(' Include(f,red);');
  29060. ConvertProgram;
  29061. CheckSource('TestRTTI_AnonymousEnumType',
  29062. LinesToStr([ // statements
  29063. 'this.TFlags$a = {',
  29064. ' "0": "red",',
  29065. ' red: 0,',
  29066. ' "1": "green",',
  29067. ' green: 1',
  29068. '};',
  29069. 'this.$rtti.$Enum("TFlags$a", {',
  29070. ' minvalue: 0,',
  29071. ' maxvalue: 1,',
  29072. ' ordtype: 1,',
  29073. ' enumtype: this.TFlags$a',
  29074. '});',
  29075. 'this.$rtti.$Set("TFlags", {',
  29076. ' comptype: this.$rtti["TFlags$a"]',
  29077. '});',
  29078. 'this.f = {};',
  29079. '']),
  29080. LinesToStr([
  29081. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  29082. '']));
  29083. end;
  29084. procedure TTestModule.TestRTTI_StaticArray;
  29085. begin
  29086. WithTypeInfo:=true;
  29087. StartProgram(false);
  29088. Add('type');
  29089. Add(' TFlag = (light,dark);');
  29090. Add(' TFlagNames = array[TFlag] of string;');
  29091. Add(' TBoolNames = array[boolean] of string;');
  29092. Add(' TByteArray = array[1..32768] of byte;');
  29093. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  29094. Add('var p: pointer;');
  29095. Add('begin');
  29096. Add(' p:=typeinfo(TFlagNames);');
  29097. Add(' p:=typeinfo(TBoolNames);');
  29098. ConvertProgram;
  29099. CheckSource('TestRTTI_StaticArray',
  29100. LinesToStr([ // statements
  29101. 'this.TFlag = {',
  29102. ' "0": "light",',
  29103. ' light: 0,',
  29104. ' "1": "dark",',
  29105. ' dark: 1',
  29106. '};',
  29107. 'this.$rtti.$Enum("TFlag", {',
  29108. ' minvalue: 0,',
  29109. ' maxvalue: 1,',
  29110. ' ordtype: 1,',
  29111. ' enumtype: this.TFlag',
  29112. '});',
  29113. 'this.$rtti.$StaticArray("TFlagNames", {',
  29114. ' dims: [2],',
  29115. ' eltype: rtl.string',
  29116. '});',
  29117. 'this.$rtti.$StaticArray("TBoolNames", {',
  29118. ' dims: [2],',
  29119. ' eltype: rtl.string',
  29120. '});',
  29121. 'this.$rtti.$StaticArray("TByteArray", {',
  29122. ' dims: [32768],',
  29123. ' eltype: rtl.byte',
  29124. '});',
  29125. 'this.$rtti.$ProcVar("TProc", {',
  29126. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  29127. '});',
  29128. 'this.p = null;',
  29129. '']),
  29130. LinesToStr([ // $mod.$main
  29131. '$mod.p = $mod.$rtti["TFlagNames"];',
  29132. '$mod.p = $mod.$rtti["TBoolNames"];',
  29133. '']));
  29134. end;
  29135. procedure TTestModule.TestRTTI_DynArray;
  29136. begin
  29137. WithTypeInfo:=true;
  29138. StartProgram(false);
  29139. Add('type');
  29140. Add(' TArrStr = array of string;');
  29141. Add(' TArr2Dim = array of tarrstr;');
  29142. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  29143. Add('var p: pointer;');
  29144. Add('begin');
  29145. Add(' p:=typeinfo(tarrstr);');
  29146. Add(' p:=typeinfo(tarr2dim);');
  29147. ConvertProgram;
  29148. CheckSource('TestRTTI_DynArray',
  29149. LinesToStr([ // statements
  29150. 'this.$rtti.$DynArray("TArrStr", {',
  29151. ' eltype: rtl.string',
  29152. '});',
  29153. 'this.$rtti.$DynArray("TArr2Dim", {',
  29154. ' eltype: this.$rtti["TArrStr"]',
  29155. '});',
  29156. 'this.$rtti.$ProcVar("TProc", {',
  29157. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  29158. '});',
  29159. 'this.p = null;',
  29160. '']),
  29161. LinesToStr([ // $mod.$main
  29162. '$mod.p = $mod.$rtti["TArrStr"];',
  29163. '$mod.p = $mod.$rtti["TArr2Dim"];',
  29164. '']));
  29165. end;
  29166. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  29167. begin
  29168. WithTypeInfo:=true;
  29169. StartProgram(false);
  29170. Add('type');
  29171. Add(' TArr = array of array of longint;');
  29172. Add('var a: TArr;');
  29173. Add('begin');
  29174. ConvertProgram;
  29175. CheckSource('TestRTTI_ArrayNestedAnonymous',
  29176. LinesToStr([ // statements
  29177. 'this.$rtti.$DynArray("TArr$a", {',
  29178. ' eltype: rtl.longint',
  29179. '});',
  29180. 'this.$rtti.$DynArray("TArr", {',
  29181. ' eltype: this.$rtti["TArr$a"]',
  29182. '});',
  29183. 'this.a = [];',
  29184. '']),
  29185. LinesToStr([ // $mod.$main
  29186. ]));
  29187. end;
  29188. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  29189. begin
  29190. WithTypeInfo:=true;
  29191. StartProgram(false);
  29192. Add('type');
  29193. Add(' TObject = class');
  29194. Add(' published');
  29195. Add(' procedure Proc; virtual; abstract;');
  29196. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  29197. Add(' end;');
  29198. Add('begin');
  29199. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  29200. nDuplicatePublishedMethodXAtY);
  29201. ConvertProgram;
  29202. end;
  29203. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  29204. begin
  29205. WithTypeInfo:=true;
  29206. StartUnit(false);
  29207. Add([
  29208. 'interface',
  29209. 'type',
  29210. ' TObject = class',
  29211. ' end;',
  29212. ' {$M+}',
  29213. ' TBird = class',
  29214. ' procedure Fly;',
  29215. ' end;',
  29216. ' {$M-}',
  29217. 'type',
  29218. ' TEagle = class(TBird)',
  29219. ' procedure Fly;',
  29220. ' end;',
  29221. 'implementation',
  29222. 'procedure TBird.Fly;',
  29223. 'begin',
  29224. 'end;',
  29225. 'procedure TEagle.Fly;',
  29226. 'begin',
  29227. 'end;',
  29228. '']);
  29229. ConvertUnit;
  29230. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  29231. LinesToStr([ // statements
  29232. 'rtl.createClass(this, "TObject", null, function () {',
  29233. ' this.$init = function () {',
  29234. ' };',
  29235. ' this.$final = function () {',
  29236. ' };',
  29237. '});',
  29238. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29239. ' this.Fly = function () {',
  29240. ' };',
  29241. ' var $r = this.$rtti;',
  29242. ' $r.addMethod("Fly", 0, []);',
  29243. '});',
  29244. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  29245. ' this.Fly = function () {',
  29246. ' };',
  29247. ' var $r = this.$rtti;',
  29248. ' $r.addMethod("Fly", 0, []);',
  29249. '});',
  29250. '']),
  29251. LinesToStr([ // $mod.$main
  29252. ]));
  29253. CheckResolverUnexpectedHints(true);
  29254. end;
  29255. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  29256. begin
  29257. WithTypeInfo:=true;
  29258. StartProgram(false);
  29259. Add('type');
  29260. Add(' TObject = class');
  29261. Add(' published');
  29262. Add(' procedure Proc; external name ''foo'';');
  29263. Add(' end;');
  29264. Add('begin');
  29265. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  29266. nPublishedNameMustMatchExternal);
  29267. ConvertProgram;
  29268. end;
  29269. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  29270. begin
  29271. WithTypeInfo:=true;
  29272. StartProgram(false);
  29273. Add('type');
  29274. Add(' TObject = class');
  29275. Add(' class var FA: longint;');
  29276. Add(' published');
  29277. Add(' class property A: longint read FA;');
  29278. Add(' end;');
  29279. Add('begin');
  29280. SetExpectedPasResolverError('Invalid published property modifier "class"',
  29281. nInvalidXModifierY);
  29282. ConvertProgram;
  29283. end;
  29284. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  29285. begin
  29286. WithTypeInfo:=true;
  29287. StartProgram(false);
  29288. Add('type');
  29289. Add(' TObject = class');
  29290. Add(' published');
  29291. Add(' class var FA: longint;');
  29292. Add(' end;');
  29293. Add('begin');
  29294. SetExpectedPasResolverError(sSymbolCannotBePublished,
  29295. nSymbolCannotBePublished);
  29296. ConvertProgram;
  29297. end;
  29298. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  29299. begin
  29300. WithTypeInfo:=true;
  29301. StartProgram(false);
  29302. Add('{$modeswitch externalclass}');
  29303. Add('type');
  29304. Add(' TObject = class');
  29305. Add(' published');
  29306. Add(' V: longint; external name ''foo'';');
  29307. Add(' end;');
  29308. Add('begin');
  29309. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  29310. nPublishedNameMustMatchExternal);
  29311. ConvertProgram;
  29312. end;
  29313. procedure TTestModule.TestRTTI_Class_Field;
  29314. begin
  29315. WithTypeInfo:=true;
  29316. StartProgram(false);
  29317. Add('{$modeswitch externalclass}');
  29318. Add('type');
  29319. Add(' TObject = class');
  29320. Add(' private');
  29321. Add(' FPropA: string;');
  29322. Add(' published');
  29323. Add(' VarLI: longint;');
  29324. Add(' VarC: char;');
  29325. Add(' VarS: string;');
  29326. Add(' VarD: double;');
  29327. Add(' VarB: boolean;');
  29328. Add(' VarLW: longword;');
  29329. Add(' VarSmI: smallint;');
  29330. Add(' VarW: word;');
  29331. Add(' VarShI: shortint;');
  29332. Add(' VarBy: byte;');
  29333. Add(' VarExt: longint external name ''VarExt'';');
  29334. Add(' ArrA, ArrB: array of byte;');
  29335. Add(' end;');
  29336. Add('var p: pointer;');
  29337. Add(' Obj: tobject;');
  29338. Add('begin');
  29339. Add(' p:=typeinfo(tobject);');
  29340. Add(' p:=typeinfo(p);');
  29341. Add(' p:=typeinfo(obj);');
  29342. ConvertProgram;
  29343. CheckSource('TestRTTI_Class_Field',
  29344. LinesToStr([ // statements
  29345. 'rtl.createClass(this, "TObject", null, function () {',
  29346. ' this.$init = function () {',
  29347. ' this.FPropA = "";',
  29348. ' this.VarLI = 0;',
  29349. ' this.VarC = "";',
  29350. ' this.VarS = "";',
  29351. ' this.VarD = 0.0;',
  29352. ' this.VarB = false;',
  29353. ' this.VarLW = 0;',
  29354. ' this.VarSmI = 0;',
  29355. ' this.VarW = 0;',
  29356. ' this.VarShI = 0;',
  29357. ' this.VarBy = 0;',
  29358. ' this.ArrA = [];',
  29359. ' this.ArrB = [];',
  29360. ' };',
  29361. ' this.$final = function () {',
  29362. ' this.ArrA = undefined;',
  29363. ' this.ArrB = undefined;',
  29364. ' };',
  29365. ' var $r = this.$rtti;',
  29366. ' $r.addField("VarLI", rtl.longint);',
  29367. ' $r.addField("VarC", rtl.char);',
  29368. ' $r.addField("VarS", rtl.string);',
  29369. ' $r.addField("VarD", rtl.double);',
  29370. ' $r.addField("VarB", rtl.boolean);',
  29371. ' $r.addField("VarLW", rtl.longword);',
  29372. ' $r.addField("VarSmI", rtl.smallint);',
  29373. ' $r.addField("VarW", rtl.word);',
  29374. ' $r.addField("VarShI", rtl.shortint);',
  29375. ' $r.addField("VarBy", rtl.byte);',
  29376. ' $r.addField("VarExt", rtl.longint);',
  29377. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  29378. ' eltype: rtl.byte',
  29379. ' });',
  29380. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  29381. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  29382. '});',
  29383. 'this.p = null;',
  29384. 'this.Obj = null;',
  29385. '']),
  29386. LinesToStr([ // $mod.$main
  29387. '$mod.p = $mod.$rtti["TObject"];',
  29388. '$mod.p = rtl.pointer;',
  29389. '$mod.p = $mod.Obj.$rtti;',
  29390. '']));
  29391. end;
  29392. procedure TTestModule.TestRTTI_Class_Method;
  29393. begin
  29394. WithTypeInfo:=true;
  29395. StartProgram(false);
  29396. Add([
  29397. 'type',
  29398. ' TObject = class',
  29399. ' private',
  29400. ' procedure Internal; external name ''$intern'';',
  29401. ' published',
  29402. ' procedure Click; virtual; abstract;',
  29403. ' procedure Notify(Sender: TObject); virtual; abstract;',
  29404. ' function GetNotify: boolean; external name ''GetNotify'';',
  29405. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  29406. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  29407. ' end;',
  29408. 'begin']);
  29409. ConvertProgram;
  29410. CheckSource('TestRTTI_Class_Method',
  29411. LinesToStr([ // statements
  29412. 'rtl.createClass(this, "TObject", null, function () {',
  29413. ' this.$init = function () {',
  29414. ' };',
  29415. ' this.$final = function () {',
  29416. ' };',
  29417. ' var $r = this.$rtti;',
  29418. ' $r.addMethod("Click", 0, []);',
  29419. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  29420. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  29421. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  29422. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  29423. '});',
  29424. '']),
  29425. LinesToStr([ // $mod.$main
  29426. '']));
  29427. end;
  29428. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29429. begin
  29430. WithTypeInfo:=true;
  29431. StartProgram(false);
  29432. Add('type');
  29433. Add(' TObject = class');
  29434. Add(' published');
  29435. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29436. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29437. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29438. Add(' end;');
  29439. Add('begin');
  29440. ConvertProgram;
  29441. CheckSource('TestRTTI_Class_MethodOpenArray',
  29442. LinesToStr([ // statements
  29443. 'rtl.createClass(this, "TObject", null, function () {',
  29444. ' this.$init = function () {',
  29445. ' };',
  29446. ' this.$final = function () {',
  29447. ' };',
  29448. ' var $r = this.$rtti;',
  29449. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29450. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29451. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29452. '});',
  29453. '']),
  29454. LinesToStr([ // $mod.$main
  29455. '']));
  29456. end;
  29457. procedure TTestModule.TestRTTI_Class_Property;
  29458. begin
  29459. WithTypeInfo:=true;
  29460. StartProgram(false);
  29461. Add('{$modeswitch externalclass}');
  29462. Add('type');
  29463. Add(' TObject = class');
  29464. Add(' private');
  29465. Add(' FColor: longint;');
  29466. Add(' FColorStored: boolean;');
  29467. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29468. Add(' function GetColor: longint; virtual; abstract;');
  29469. Add(' function GetColorStored: boolean; virtual; abstract;');
  29470. Add(' FExtSize: longint external name ''$extSize'';');
  29471. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29472. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29473. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29474. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29475. Add(' published');
  29476. Add(' property ColorA: longint read FColor;');
  29477. Add(' property ColorB: longint write FColor;');
  29478. Add(' property ColorC: longint read GetColor write SetColor;');
  29479. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29480. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29481. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29482. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29483. Add(' end;');
  29484. Add('begin');
  29485. ConvertProgram;
  29486. CheckSource('TestRTTI_Class_Property',
  29487. LinesToStr([ // statements
  29488. 'rtl.createClass(this, "TObject", null, function () {',
  29489. ' this.$init = function () {',
  29490. ' this.FColor = 0;',
  29491. ' this.FColorStored = false;',
  29492. ' };',
  29493. ' this.$final = function () {',
  29494. ' };',
  29495. ' var $r = this.$rtti;',
  29496. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29497. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29498. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29499. ' $r.addProperty(',
  29500. ' "ColorD",',
  29501. ' 8,',
  29502. ' rtl.longint,',
  29503. ' "FColor",',
  29504. ' "FColor",',
  29505. ' {',
  29506. ' stored: "FColorStored"',
  29507. ' }',
  29508. ' );',
  29509. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29510. ' $r.addProperty(',
  29511. ' "ExtSizeB",',
  29512. ' 11,',
  29513. ' rtl.longint,',
  29514. ' "$getSize",',
  29515. ' "$setSize",',
  29516. ' {',
  29517. ' stored: "$extSizeStored"',
  29518. ' }',
  29519. ' );',
  29520. ' $r.addProperty(',
  29521. ' "ExtSizeC",',
  29522. ' 12,',
  29523. ' rtl.longint,',
  29524. ' "$extSize",',
  29525. ' "$extSize",',
  29526. ' {',
  29527. ' stored: "$getExtSizeStored"',
  29528. ' }',
  29529. ' );',
  29530. '});',
  29531. '']),
  29532. LinesToStr([ // $mod.$main
  29533. '']));
  29534. end;
  29535. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29536. begin
  29537. WithTypeInfo:=true;
  29538. StartProgram(false);
  29539. Add('{$modeswitch externalclass}');
  29540. Add('type');
  29541. Add(' integer = longint;');
  29542. Add(' TObject = class');
  29543. Add(' private');
  29544. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29545. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29546. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29547. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29548. Add(' published');
  29549. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29550. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29551. Add(' end;');
  29552. Add('begin');
  29553. ConvertProgram;
  29554. CheckSource('TestRTTI_Class_PropertyParams',
  29555. LinesToStr([ // statements
  29556. 'rtl.createClass(this, "TObject", null, function () {',
  29557. ' this.$init = function () {',
  29558. ' };',
  29559. ' this.$final = function () {',
  29560. ' };',
  29561. ' var $r = this.$rtti;',
  29562. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29563. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29564. '});',
  29565. '']),
  29566. LinesToStr([ // $mod.$main
  29567. '']));
  29568. end;
  29569. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29570. begin
  29571. WithTypeInfo:=true;
  29572. AddModuleWithIntfImplSrc('unit1.pas',
  29573. 'type TColor = -5..5;',
  29574. '');
  29575. StartProgram(true);
  29576. Add([
  29577. 'uses unit1;',
  29578. 'type',
  29579. ' TColorAlias = TColor;',
  29580. ' TColorTypeAlias = type TColor;',
  29581. ' TObject = class',
  29582. ' private',
  29583. ' fColor: TColor;',
  29584. ' fAlias: TColorAlias;',
  29585. ' fTypeAlias: TColorTypeAlias;',
  29586. ' published',
  29587. ' property Color: TColor read fcolor;',
  29588. ' property Alias: TColorAlias read falias;',
  29589. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29590. ' end;',
  29591. 'begin',
  29592. '']);
  29593. ConvertProgram;
  29594. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29595. LinesToStr([ // statements
  29596. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29597. 'rtl.createClass(this, "TObject", null, function () {',
  29598. ' this.$init = function () {',
  29599. ' this.fColor = 0;',
  29600. ' this.fAlias = 0;',
  29601. ' this.fTypeAlias = 0;',
  29602. ' };',
  29603. ' this.$final = function () {',
  29604. ' };',
  29605. ' var $r = this.$rtti;',
  29606. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29607. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29608. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29609. '});',
  29610. '']),
  29611. LinesToStr([ // $mod.$main
  29612. '']));
  29613. end;
  29614. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29615. begin
  29616. WithTypeInfo:=true;
  29617. StartProgram(false);
  29618. Add([
  29619. '{$modeswitch omitrtti}',
  29620. 'type',
  29621. ' TObject = class',
  29622. ' private',
  29623. ' FA: byte;',
  29624. ' published',
  29625. ' property A: byte read FA write FA;',
  29626. ' end;',
  29627. 'begin']);
  29628. ConvertProgram;
  29629. CheckSource('TestRTTI_Class_OmitRTTI',
  29630. LinesToStr([ // statements
  29631. 'rtl.createClass(this, "TObject", null, function () {',
  29632. ' this.$init = function () {',
  29633. ' this.FA = 0;',
  29634. ' };',
  29635. ' this.$final = function () {',
  29636. ' };',
  29637. '});',
  29638. '']),
  29639. LinesToStr([ // $mod.$main
  29640. '']));
  29641. end;
  29642. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  29643. begin
  29644. WithTypeInfo:=true;
  29645. StartUnit(true,[supTObject]);
  29646. Add([
  29647. 'interface',
  29648. 'type',
  29649. ' {$M+}',
  29650. ' TBird = class',
  29651. ' published',
  29652. ' Swarm: array of TBird;',
  29653. ' end;',
  29654. 'implementation',
  29655. '']);
  29656. ConvertUnit;
  29657. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  29658. LinesToStr([ // statements
  29659. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  29660. ' this.$init = function () {',
  29661. ' pas.system.TObject.$init.call(this);',
  29662. ' this.Swarm = [];',
  29663. ' };',
  29664. ' this.$final = function () {',
  29665. ' this.Swarm = undefined;',
  29666. ' pas.system.TObject.$final.call(this);',
  29667. ' };',
  29668. ' var $r = this.$rtti;',
  29669. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  29670. ' eltype: $r',
  29671. ' });',
  29672. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  29673. '});',
  29674. '']),
  29675. LinesToStr([ // $mod.$main
  29676. '']));
  29677. end;
  29678. procedure TTestModule.TestRTTI_IndexModifier;
  29679. begin
  29680. WithTypeInfo:=true;
  29681. StartProgram(false);
  29682. Add([
  29683. 'type',
  29684. ' TEnum = (red, blue);',
  29685. ' TObject = class',
  29686. ' FB: boolean;',
  29687. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29688. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29689. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29690. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29691. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29692. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29693. ' published',
  29694. ' property B1: boolean index 1 read FB write SetIntBool;',
  29695. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29696. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29697. ' end;',
  29698. 'begin']);
  29699. ConvertProgram;
  29700. CheckSource('TestRTTI_IndexModifier',
  29701. LinesToStr([ // statements
  29702. 'this.TEnum = {',
  29703. ' "0": "red",',
  29704. ' red: 0,',
  29705. ' "1": "blue",',
  29706. ' blue: 1',
  29707. '};',
  29708. 'this.$rtti.$Enum("TEnum", {',
  29709. ' minvalue: 0,',
  29710. ' maxvalue: 1,',
  29711. ' ordtype: 1,',
  29712. ' enumtype: this.TEnum',
  29713. '});',
  29714. 'rtl.createClass(this, "TObject", null, function () {',
  29715. ' this.$init = function () {',
  29716. ' this.FB = false;',
  29717. ' };',
  29718. ' this.$final = function () {',
  29719. ' };',
  29720. ' var $r = this.$rtti;',
  29721. ' $r.addProperty(',
  29722. ' "B1",',
  29723. ' 18,',
  29724. ' rtl.boolean,',
  29725. ' "FB",',
  29726. ' "SetIntBool",',
  29727. ' {',
  29728. ' index: 1',
  29729. ' }',
  29730. ' );',
  29731. ' $r.addProperty(',
  29732. ' "B2",',
  29733. ' 17,',
  29734. ' rtl.boolean,',
  29735. ' "GetEnumBool",',
  29736. ' "FB",',
  29737. ' {',
  29738. ' index: $mod.TEnum.blue',
  29739. ' }',
  29740. ' );',
  29741. ' $r.addProperty(',
  29742. ' "I1",',
  29743. ' 19,',
  29744. ' rtl.boolean,',
  29745. ' "GetStrIntBool",',
  29746. ' "SetStrIntBool",',
  29747. ' {',
  29748. ' index: 2',
  29749. ' }',
  29750. ' );',
  29751. '});',
  29752. '']),
  29753. LinesToStr([ // $mod.$main
  29754. '']));
  29755. end;
  29756. procedure TTestModule.TestRTTI_StoredModifier;
  29757. begin
  29758. WithTypeInfo:=true;
  29759. StartProgram(false);
  29760. Add([
  29761. 'const',
  29762. ' ConstB = true;',
  29763. 'type',
  29764. ' TObject = class',
  29765. ' private',
  29766. ' FB: boolean;',
  29767. ' function IsBStored: boolean; virtual; abstract;',
  29768. ' published',
  29769. ' property BoolA: boolean read FB stored true;',
  29770. ' property BoolB: boolean read FB stored false;',
  29771. ' property BoolC: boolean read FB stored FB;',
  29772. ' property BoolD: boolean read FB stored ConstB;',
  29773. ' property BoolE: boolean read FB stored IsBStored;',
  29774. ' end;',
  29775. 'begin']);
  29776. ConvertProgram;
  29777. CheckSource('TestRTTI_StoredModifier',
  29778. LinesToStr([ // statements
  29779. 'this.ConstB = true;',
  29780. 'rtl.createClass(this, "TObject", null, function () {',
  29781. ' this.$init = function () {',
  29782. ' this.FB = false;',
  29783. ' };',
  29784. ' this.$final = function () {',
  29785. ' };',
  29786. ' var $r = this.$rtti;',
  29787. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29788. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29789. ' $r.addProperty(',
  29790. ' "BoolC",',
  29791. ' 8,',
  29792. ' rtl.boolean,',
  29793. ' "FB",',
  29794. ' "",',
  29795. ' {',
  29796. ' stored: "FB"',
  29797. ' }',
  29798. ' );',
  29799. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29800. ' $r.addProperty(',
  29801. ' "BoolE",',
  29802. ' 12,',
  29803. ' rtl.boolean,',
  29804. ' "FB",',
  29805. ' "",',
  29806. ' {',
  29807. ' stored: "IsBStored"',
  29808. ' }',
  29809. ' );',
  29810. '});',
  29811. '']),
  29812. LinesToStr([ // $mod.$main
  29813. '']));
  29814. end;
  29815. procedure TTestModule.TestRTTI_DefaultValue;
  29816. begin
  29817. WithTypeInfo:=true;
  29818. StartProgram(false);
  29819. Add([
  29820. 'type',
  29821. ' TEnum = (red, blue);',
  29822. 'const',
  29823. ' CB = true or false;',
  29824. ' CI = 1+2;',
  29825. 'type',
  29826. ' TObject = class',
  29827. ' FB: boolean;',
  29828. ' FI: longint;',
  29829. ' FE: TEnum;',
  29830. ' published',
  29831. ' property B1: boolean read FB default true;',
  29832. ' property B2: boolean read FB default CB;',
  29833. ' property B3: boolean read FB default test1.cb;',
  29834. ' property I1: longint read FI default 2;',
  29835. ' property I2: longint read FI default CI;',
  29836. ' property E1: TEnum read FE default red;',
  29837. ' property E2: TEnum read FE default TEnum.blue;',
  29838. ' end;',
  29839. 'begin']);
  29840. ConvertProgram;
  29841. CheckSource('TestRTTI_DefaultValue',
  29842. LinesToStr([ // statements
  29843. 'this.TEnum = {',
  29844. ' "0": "red",',
  29845. ' red: 0,',
  29846. ' "1": "blue",',
  29847. ' blue: 1',
  29848. '};',
  29849. 'this.$rtti.$Enum("TEnum", {',
  29850. ' minvalue: 0,',
  29851. ' maxvalue: 1,',
  29852. ' ordtype: 1,',
  29853. ' enumtype: this.TEnum',
  29854. '});',
  29855. 'this.CB = true || false;',
  29856. 'this.CI = 1 + 2;',
  29857. 'rtl.createClass(this, "TObject", null, function () {',
  29858. ' this.$init = function () {',
  29859. ' this.FB = false;',
  29860. ' this.FI = 0;',
  29861. ' this.FE = 0;',
  29862. ' };',
  29863. ' this.$final = function () {',
  29864. ' };',
  29865. ' var $r = this.$rtti;',
  29866. ' $r.addProperty(',
  29867. ' "B1",',
  29868. ' 0,',
  29869. ' rtl.boolean,',
  29870. ' "FB",',
  29871. ' "",',
  29872. ' {',
  29873. ' Default: true',
  29874. ' }',
  29875. ' );',
  29876. ' $r.addProperty(',
  29877. ' "B2",',
  29878. ' 0,',
  29879. ' rtl.boolean,',
  29880. ' "FB",',
  29881. ' "",',
  29882. ' {',
  29883. ' Default: true',
  29884. ' }',
  29885. ' );',
  29886. ' $r.addProperty(',
  29887. ' "B3",',
  29888. ' 0,',
  29889. ' rtl.boolean,',
  29890. ' "FB",',
  29891. ' "",',
  29892. ' {',
  29893. ' Default: true',
  29894. ' }',
  29895. ' );',
  29896. ' $r.addProperty(',
  29897. ' "I1",',
  29898. ' 0,',
  29899. ' rtl.longint,',
  29900. ' "FI",',
  29901. ' "",',
  29902. ' {',
  29903. ' Default: 2',
  29904. ' }',
  29905. ' );',
  29906. ' $r.addProperty(',
  29907. ' "I2",',
  29908. ' 0,',
  29909. ' rtl.longint,',
  29910. ' "FI",',
  29911. ' "",',
  29912. ' {',
  29913. ' Default: 3',
  29914. ' }',
  29915. ' );',
  29916. ' $r.addProperty(',
  29917. ' "E1",',
  29918. ' 0,',
  29919. ' $mod.$rtti["TEnum"],',
  29920. ' "FE",',
  29921. ' "",',
  29922. ' {',
  29923. ' Default: $mod.TEnum.red',
  29924. ' }',
  29925. ' );',
  29926. ' $r.addProperty(',
  29927. ' "E2",',
  29928. ' 0,',
  29929. ' $mod.$rtti["TEnum"],',
  29930. ' "FE",',
  29931. ' "",',
  29932. ' {',
  29933. ' Default: $mod.TEnum.blue',
  29934. ' }',
  29935. ' );',
  29936. '});',
  29937. '']),
  29938. LinesToStr([ // $mod.$main
  29939. '']));
  29940. end;
  29941. procedure TTestModule.TestRTTI_DefaultValueSet;
  29942. begin
  29943. WithTypeInfo:=true;
  29944. StartProgram(false);
  29945. Add([
  29946. 'type',
  29947. ' TEnum = (red, blue);',
  29948. ' TSet = set of TEnum;',
  29949. 'const',
  29950. ' CSet = [red,blue];',
  29951. 'type',
  29952. ' TObject = class',
  29953. ' FSet: TSet;',
  29954. ' published',
  29955. ' property Set1: TSet read FSet default [];',
  29956. ' property Set2: TSet read FSet default [red];',
  29957. ' property Set3: TSet read FSet default [red,blue];',
  29958. ' property Set4: TSet read FSet default CSet;',
  29959. ' end;',
  29960. 'begin']);
  29961. ConvertProgram;
  29962. CheckSource('TestRTTI_DefaultValueSet',
  29963. LinesToStr([ // statements
  29964. 'this.TEnum = {',
  29965. ' "0": "red",',
  29966. ' red: 0,',
  29967. ' "1": "blue",',
  29968. ' blue: 1',
  29969. '};',
  29970. 'this.$rtti.$Enum("TEnum", {',
  29971. ' minvalue: 0,',
  29972. ' maxvalue: 1,',
  29973. ' ordtype: 1,',
  29974. ' enumtype: this.TEnum',
  29975. '});',
  29976. 'this.$rtti.$Set("TSet", {',
  29977. ' comptype: this.$rtti["TEnum"]',
  29978. '});',
  29979. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29980. 'rtl.createClass(this, "TObject", null, function () {',
  29981. ' this.$init = function () {',
  29982. ' this.FSet = {};',
  29983. ' };',
  29984. ' this.$final = function () {',
  29985. ' this.FSet = undefined;',
  29986. ' };',
  29987. ' var $r = this.$rtti;',
  29988. ' $r.addProperty(',
  29989. ' "Set1",',
  29990. ' 0,',
  29991. ' $mod.$rtti["TSet"],',
  29992. ' "FSet",',
  29993. ' "",',
  29994. ' {',
  29995. ' Default: {}',
  29996. ' }',
  29997. ' );',
  29998. ' $r.addProperty(',
  29999. ' "Set2",',
  30000. ' 0,',
  30001. ' $mod.$rtti["TSet"],',
  30002. ' "FSet",',
  30003. ' "",',
  30004. ' {',
  30005. ' Default: rtl.createSet($mod.TEnum.red)',
  30006. ' }',
  30007. ' );',
  30008. ' $r.addProperty(',
  30009. ' "Set3",',
  30010. ' 0,',
  30011. ' $mod.$rtti["TSet"],',
  30012. ' "FSet",',
  30013. ' "",',
  30014. ' {',
  30015. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  30016. ' }',
  30017. ' );',
  30018. ' $r.addProperty(',
  30019. ' "Set4",',
  30020. ' 0,',
  30021. ' $mod.$rtti["TSet"],',
  30022. ' "FSet",',
  30023. ' "",',
  30024. ' {',
  30025. ' Default: $mod.CSet',
  30026. ' }',
  30027. ' );',
  30028. '});',
  30029. '']),
  30030. LinesToStr([ // $mod.$main
  30031. '']));
  30032. end;
  30033. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  30034. begin
  30035. WithTypeInfo:=true;
  30036. StartProgram(false);
  30037. Add([
  30038. 'type',
  30039. ' TRg = -1..1;',
  30040. 'const',
  30041. ' l = low(TRg);',
  30042. ' h = high(TRg);',
  30043. 'type',
  30044. ' TObject = class',
  30045. ' FV: TRg;',
  30046. ' published',
  30047. ' property V1: TRg read FV default -1;',
  30048. ' end;',
  30049. 'begin']);
  30050. ConvertProgram;
  30051. CheckSource('TestRTTI_DefaultValueRangeType',
  30052. LinesToStr([ // statements
  30053. 'this.$rtti.$Int("TRg", {',
  30054. ' minvalue: -1,',
  30055. ' maxvalue: 1,',
  30056. ' ordtype: 0',
  30057. '});',
  30058. 'this.l = -1;',
  30059. 'this.h = 1;',
  30060. 'rtl.createClass(this, "TObject", null, function () {',
  30061. ' this.$init = function () {',
  30062. ' this.FV = 0;',
  30063. ' };',
  30064. ' this.$final = function () {',
  30065. ' };',
  30066. ' var $r = this.$rtti;',
  30067. ' $r.addProperty(',
  30068. ' "V1",',
  30069. ' 0,',
  30070. ' $mod.$rtti["TRg"],',
  30071. ' "FV",',
  30072. ' "",',
  30073. ' {',
  30074. ' Default: -1',
  30075. ' }',
  30076. ' );',
  30077. '});',
  30078. '']),
  30079. LinesToStr([ // $mod.$main
  30080. '']));
  30081. end;
  30082. procedure TTestModule.TestRTTI_DefaultValueInherit;
  30083. begin
  30084. WithTypeInfo:=true;
  30085. StartProgram(false);
  30086. Add([
  30087. 'type',
  30088. ' TObject = class',
  30089. ' FA, FB: byte;',
  30090. ' property A: byte read FA default 1;',
  30091. ' property B: byte read FB default 2;',
  30092. ' end;',
  30093. ' TBird = class',
  30094. ' published',
  30095. ' property A;',
  30096. ' property B nodefault;',
  30097. ' end;',
  30098. 'begin']);
  30099. ConvertProgram;
  30100. CheckSource('TestRTTI_DefaultValueInherit',
  30101. LinesToStr([ // statements
  30102. 'rtl.createClass(this, "TObject", null, function () {',
  30103. ' this.$init = function () {',
  30104. ' this.FA = 0;',
  30105. ' this.FB = 0;',
  30106. ' };',
  30107. ' this.$final = function () {',
  30108. ' };',
  30109. '});',
  30110. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30111. ' var $r = this.$rtti;',
  30112. ' $r.addProperty(',
  30113. ' "A",',
  30114. ' 0,',
  30115. ' rtl.byte,',
  30116. ' "FA",',
  30117. ' "",',
  30118. ' {',
  30119. ' Default: 1',
  30120. ' }',
  30121. ' );',
  30122. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  30123. '});',
  30124. '']),
  30125. LinesToStr([ // $mod.$main
  30126. '']));
  30127. end;
  30128. procedure TTestModule.TestRTTI_OverrideMethod;
  30129. begin
  30130. WithTypeInfo:=true;
  30131. StartProgram(false);
  30132. Add('type');
  30133. Add(' TObject = class');
  30134. Add(' published');
  30135. Add(' procedure DoIt; virtual; abstract;');
  30136. Add(' end;');
  30137. Add(' TSky = class');
  30138. Add(' published');
  30139. Add(' procedure DoIt; override;');
  30140. Add(' end;');
  30141. Add('procedure TSky.DoIt; begin end;');
  30142. Add('begin');
  30143. ConvertProgram;
  30144. CheckSource('TestRTTI_OverrideMethod',
  30145. LinesToStr([ // statements
  30146. 'rtl.createClass(this, "TObject", null, function () {',
  30147. ' this.$init = function () {',
  30148. ' };',
  30149. ' this.$final = function () {',
  30150. ' };',
  30151. ' var $r = this.$rtti;',
  30152. ' $r.addMethod("DoIt", 0, []);',
  30153. '});',
  30154. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30155. ' this.DoIt = function () {',
  30156. ' };',
  30157. '});',
  30158. '']),
  30159. LinesToStr([ // $mod.$main
  30160. '']));
  30161. end;
  30162. procedure TTestModule.TestRTTI_ReintroduceMethod;
  30163. begin
  30164. WithTypeInfo:=true;
  30165. StartProgram(false);
  30166. Add([
  30167. 'type',
  30168. ' TObject = class',
  30169. ' published',
  30170. ' procedure DoIt;',
  30171. ' end;',
  30172. ' TSky = class',
  30173. ' published',
  30174. ' procedure DoIt; reintroduce;',
  30175. ' end;',
  30176. 'procedure TObject.DoIt; begin end;',
  30177. 'procedure TSky.DoIt;',
  30178. 'begin',
  30179. ' inherited DoIt;',
  30180. 'end;',
  30181. 'begin']);
  30182. ConvertProgram;
  30183. CheckSource('TestRTTI_ReintroduceMethod',
  30184. LinesToStr([ // statements
  30185. 'rtl.createClass(this, "TObject", null, function () {',
  30186. ' this.$init = function () {',
  30187. ' };',
  30188. ' this.$final = function () {',
  30189. ' };',
  30190. ' this.DoIt = function () {',
  30191. ' };',
  30192. ' var $r = this.$rtti;',
  30193. ' $r.addMethod("DoIt", 0, []);',
  30194. '});',
  30195. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30196. ' this.DoIt = function () {',
  30197. ' $mod.TObject.DoIt.call(this);',
  30198. ' };',
  30199. ' var $r = this.$rtti;',
  30200. ' $r.addMethod("DoIt", 0, []);',
  30201. '});',
  30202. '']),
  30203. LinesToStr([ // $mod.$main
  30204. '']));
  30205. end;
  30206. procedure TTestModule.TestRTTI_OverloadProperty;
  30207. begin
  30208. WithTypeInfo:=true;
  30209. StartProgram(false);
  30210. Add('type');
  30211. Add(' TObject = class');
  30212. Add(' protected');
  30213. Add(' FFlag: longint;');
  30214. Add(' published');
  30215. Add(' property Flag: longint read fflag;');
  30216. Add(' end;');
  30217. Add(' TSky = class');
  30218. Add(' published');
  30219. Add(' property FLAG: longint write fflag;');
  30220. Add(' end;');
  30221. Add('begin');
  30222. ConvertProgram;
  30223. CheckSource('TestRTTI_OverrideMethod',
  30224. LinesToStr([ // statements
  30225. 'rtl.createClass(this, "TObject", null, function () {',
  30226. ' this.$init = function () {',
  30227. ' this.FFlag = 0;',
  30228. ' };',
  30229. ' this.$final = function () {',
  30230. ' };',
  30231. ' var $r = this.$rtti;',
  30232. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  30233. '});',
  30234. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30235. ' var $r = this.$rtti;',
  30236. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  30237. '});',
  30238. '']),
  30239. LinesToStr([ // $mod.$main
  30240. '']));
  30241. end;
  30242. procedure TTestModule.TestRTTI_ClassForward;
  30243. begin
  30244. WithTypeInfo:=true;
  30245. StartProgram(false);
  30246. Add('type');
  30247. Add(' TObject = class end;');
  30248. Add(' tbridge = class;');
  30249. Add(' TProc = function: tbridge;');
  30250. Add(' TOger = class');
  30251. Add(' published');
  30252. Add(' FBridge: tbridge;');
  30253. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  30254. Add(' property Bridge: tbridge read fbridge write setbridge;');
  30255. Add(' end;');
  30256. Add(' TBridge = class');
  30257. Add(' FOger: toger;');
  30258. Add(' end;');
  30259. Add('var p: Pointer;');
  30260. Add(' b: tbridge;');
  30261. Add('begin');
  30262. Add(' p:=typeinfo(tbridge);');
  30263. Add(' p:=typeinfo(b);');
  30264. ConvertProgram;
  30265. CheckSource('TestRTTI_ClassForward',
  30266. LinesToStr([ // statements
  30267. 'rtl.createClass(this, "TObject", null, function () {',
  30268. ' this.$init = function () {',
  30269. ' };',
  30270. ' this.$final = function () {',
  30271. ' };',
  30272. '});',
  30273. 'this.$rtti.$Class("TBridge");',
  30274. 'this.$rtti.$ProcVar("TProc", {',
  30275. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  30276. '});',
  30277. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  30278. ' this.$init = function () {',
  30279. ' $mod.TObject.$init.call(this);',
  30280. ' this.FBridge = null;',
  30281. ' };',
  30282. ' this.$final = function () {',
  30283. ' this.FBridge = undefined;',
  30284. ' $mod.TObject.$final.call(this);',
  30285. ' };',
  30286. ' var $r = this.$rtti;',
  30287. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  30288. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  30289. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  30290. '});',
  30291. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  30292. ' this.$init = function () {',
  30293. ' $mod.TObject.$init.call(this);',
  30294. ' this.FOger = null;',
  30295. ' };',
  30296. ' this.$final = function () {',
  30297. ' this.FOger = undefined;',
  30298. ' $mod.TObject.$final.call(this);',
  30299. ' };',
  30300. '});',
  30301. 'this.p = null;',
  30302. 'this.b = null;',
  30303. '']),
  30304. LinesToStr([ // $mod.$main
  30305. '$mod.p = $mod.$rtti["TBridge"];',
  30306. '$mod.p = $mod.b.$rtti;',
  30307. '']));
  30308. end;
  30309. procedure TTestModule.TestRTTI_ClassOf;
  30310. begin
  30311. WithTypeInfo:=true;
  30312. StartProgram(false);
  30313. Add('type');
  30314. Add(' TClass = class of tobject;');
  30315. Add(' TProcA = function: TClass;');
  30316. Add(' TObject = class');
  30317. Add(' published');
  30318. Add(' C: tclass;');
  30319. Add(' end;');
  30320. Add(' tfox = class;');
  30321. Add(' TBird = class end;');
  30322. Add(' TBirds = class of tbird;');
  30323. Add(' TFox = class end;');
  30324. Add(' TFoxes = class of tfox;');
  30325. Add(' TCows = class of TCow;');
  30326. Add(' TCow = class;');
  30327. Add(' TCow = class end;');
  30328. Add('begin');
  30329. ConvertProgram;
  30330. CheckSource('TestRTTI_ClassOf',
  30331. LinesToStr([ // statements
  30332. 'this.$rtti.$Class("TObject");',
  30333. 'this.$rtti.$ClassRef("TClass", {',
  30334. ' instancetype: this.$rtti["TObject"]',
  30335. '});',
  30336. 'this.$rtti.$ProcVar("TProcA", {',
  30337. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  30338. '});',
  30339. 'rtl.createClass(this, "TObject", null, function () {',
  30340. ' this.$init = function () {',
  30341. ' this.C = null;',
  30342. ' };',
  30343. ' this.$final = function () {',
  30344. ' this.C = undefined;',
  30345. ' };',
  30346. ' var $r = this.$rtti;',
  30347. ' $r.addField("C", $mod.$rtti["TClass"]);',
  30348. '});',
  30349. 'this.$rtti.$Class("TFox");',
  30350. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30351. '});',
  30352. 'this.$rtti.$ClassRef("TBirds", {',
  30353. ' instancetype: this.$rtti["TBird"]',
  30354. '});',
  30355. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  30356. '});',
  30357. 'this.$rtti.$ClassRef("TFoxes", {',
  30358. ' instancetype: this.$rtti["TFox"]',
  30359. '});',
  30360. 'this.$rtti.$Class("TCow");',
  30361. 'this.$rtti.$ClassRef("TCows", {',
  30362. ' instancetype: this.$rtti["TCow"]',
  30363. '});',
  30364. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  30365. '});',
  30366. '']),
  30367. LinesToStr([ // $mod.$main
  30368. '']));
  30369. end;
  30370. procedure TTestModule.TestRTTI_Record;
  30371. begin
  30372. WithTypeInfo:=true;
  30373. StartProgram(false);
  30374. Add('type');
  30375. Add(' integer = longint;');
  30376. Add(' TPoint = record');
  30377. Add(' x,y: integer;');
  30378. Add(' end;');
  30379. Add('var p: pointer;');
  30380. Add(' r: tpoint;');
  30381. Add('begin');
  30382. Add(' p:=typeinfo(tpoint);');
  30383. Add(' p:=typeinfo(r);');
  30384. Add(' p:=typeinfo(r.x);');
  30385. ConvertProgram;
  30386. CheckSource('TestRTTI_Record',
  30387. LinesToStr([ // statements
  30388. 'rtl.recNewT(this, "TPoint", function () {',
  30389. ' this.x = 0;',
  30390. ' this.y = 0;',
  30391. ' this.$eq = function (b) {',
  30392. ' return (this.x === b.x) && (this.y === b.y);',
  30393. ' };',
  30394. ' this.$assign = function (s) {',
  30395. ' this.x = s.x;',
  30396. ' this.y = s.y;',
  30397. ' return this;',
  30398. ' };',
  30399. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30400. ' $r.addField("x", rtl.longint);',
  30401. ' $r.addField("y", rtl.longint);',
  30402. '});',
  30403. 'this.p = null;',
  30404. 'this.r = this.TPoint.$new();',
  30405. '']),
  30406. LinesToStr([ // $mod.$main
  30407. '$mod.p = $mod.$rtti["TPoint"];',
  30408. '$mod.p = $mod.$rtti["TPoint"];',
  30409. '$mod.p = rtl.longint;',
  30410. '']));
  30411. end;
  30412. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  30413. begin
  30414. WithTypeInfo:=true;
  30415. StartProgram(false);
  30416. Add('type');
  30417. Add(' TFloatRec = record');
  30418. Add(' c,d: array of char;');
  30419. // Add(' i: array of array of longint;');
  30420. Add(' end;');
  30421. Add('var p: pointer;');
  30422. Add(' r: tfloatrec;');
  30423. Add('begin');
  30424. Add(' p:=typeinfo(tfloatrec);');
  30425. Add(' p:=typeinfo(r);');
  30426. Add(' p:=typeinfo(r.d);');
  30427. ConvertProgram;
  30428. CheckSource('TestRTTI_Record',
  30429. LinesToStr([ // statements
  30430. 'rtl.recNewT(this, "TFloatRec", function () {',
  30431. ' this.$new = function () {',
  30432. ' var r = Object.create(this);',
  30433. ' r.c = [];',
  30434. ' r.d = [];',
  30435. ' return r;',
  30436. ' };',
  30437. ' this.$eq = function (b) {',
  30438. ' return (this.c === b.c) && (this.d === b.d);',
  30439. ' };',
  30440. ' this.$assign = function (s) {',
  30441. ' this.c = rtl.arrayRef(s.c);',
  30442. ' this.d = rtl.arrayRef(s.d);',
  30443. ' return this;',
  30444. ' };',
  30445. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  30446. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  30447. ' eltype: rtl.char',
  30448. ' });',
  30449. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  30450. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  30451. '});',
  30452. 'this.p = null;',
  30453. 'this.r = this.TFloatRec.$new();',
  30454. '']),
  30455. LinesToStr([ // $mod.$main
  30456. '$mod.p = $mod.$rtti["TFloatRec"];',
  30457. '$mod.p = $mod.$rtti["TFloatRec"];',
  30458. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  30459. '']));
  30460. end;
  30461. procedure TTestModule.TestRTTI_Record_ClassVarType;
  30462. begin
  30463. WithTypeInfo:=true;
  30464. StartProgram(false);
  30465. Add([
  30466. '{$modeswitch AdvancedRecords}',
  30467. 'type',
  30468. ' TPoint = record',
  30469. ' type TProc = procedure(w: word);',
  30470. ' class var p: TProc;',
  30471. ' end;',
  30472. 'begin',
  30473. '']);
  30474. ConvertProgram;
  30475. CheckSource('TestRTTI_Record_ClassVarType',
  30476. LinesToStr([ // statements
  30477. 'rtl.recNewT(this, "TPoint", function () {',
  30478. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30479. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30480. ' });',
  30481. ' this.p = null;',
  30482. ' this.$eq = function (b) {',
  30483. ' return true;',
  30484. ' };',
  30485. ' this.$assign = function (s) {',
  30486. ' return this;',
  30487. ' };',
  30488. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30489. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30490. '}, true);',
  30491. '']),
  30492. LinesToStr([ // $mod.$main
  30493. '']));
  30494. end;
  30495. procedure TTestModule.TestRTTI_LocalTypes;
  30496. begin
  30497. WithTypeInfo:=true;
  30498. StartProgram(false);
  30499. Add([
  30500. 'procedure DoIt;',
  30501. 'type',
  30502. ' integer = longint;',
  30503. ' TPoint = record',
  30504. ' x,y: integer;',
  30505. ' end;',
  30506. 'var p: TPoint;',
  30507. 'begin',
  30508. 'end;',
  30509. 'begin']);
  30510. ConvertProgram;
  30511. CheckSource('TestRTTI_LocalTypes',
  30512. LinesToStr([ // statements
  30513. 'var TPoint = rtl.recNewT(null, "", function () {',
  30514. ' this.x = 0;',
  30515. ' this.y = 0;',
  30516. ' this.$eq = function (b) {',
  30517. ' return (this.x === b.x) && (this.y === b.y);',
  30518. ' };',
  30519. ' this.$assign = function (s) {',
  30520. ' this.x = s.x;',
  30521. ' this.y = s.y;',
  30522. ' return this;',
  30523. ' };',
  30524. '});',
  30525. 'this.DoIt = function () {',
  30526. ' var p = TPoint.$new();',
  30527. '};',
  30528. '']),
  30529. LinesToStr([ // $mod.$main
  30530. '']));
  30531. end;
  30532. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30533. begin
  30534. WithTypeInfo:=true;
  30535. StartProgram(false);
  30536. Add([
  30537. 'type',
  30538. ' TCaption = string;',
  30539. ' TYesNo = boolean;',
  30540. ' TLetter = char;',
  30541. ' TFloat = double;',
  30542. ' TPtr = pointer;',
  30543. ' TShortInt = shortint;',
  30544. ' TByte = byte;',
  30545. ' TSmallInt = smallint;',
  30546. ' TWord = word;',
  30547. ' TInt32 = longint;',
  30548. ' TDWord = longword;',
  30549. ' TValue = jsvalue;',
  30550. 'var p: TPtr;',
  30551. 'begin',
  30552. ' p:=typeinfo(string);',
  30553. ' p:=typeinfo(tcaption);',
  30554. ' p:=typeinfo(boolean);',
  30555. ' p:=typeinfo(tyesno);',
  30556. ' p:=typeinfo(char);',
  30557. ' p:=typeinfo(tletter);',
  30558. ' p:=typeinfo(double);',
  30559. ' p:=typeinfo(tfloat);',
  30560. ' p:=typeinfo(pointer);',
  30561. ' p:=typeinfo(tptr);',
  30562. ' p:=typeinfo(shortint);',
  30563. ' p:=typeinfo(tshortint);',
  30564. ' p:=typeinfo(byte);',
  30565. ' p:=typeinfo(tbyte);',
  30566. ' p:=typeinfo(smallint);',
  30567. ' p:=typeinfo(tsmallint);',
  30568. ' p:=typeinfo(word);',
  30569. ' p:=typeinfo(tword);',
  30570. ' p:=typeinfo(longword);',
  30571. ' p:=typeinfo(tdword);',
  30572. ' p:=typeinfo(jsvalue);',
  30573. ' p:=typeinfo(tvalue);',
  30574. '']);
  30575. ConvertProgram;
  30576. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30577. LinesToStr([ // statements
  30578. 'this.p = null;',
  30579. '']),
  30580. LinesToStr([ // $mod.$main
  30581. '$mod.p = rtl.string;',
  30582. '$mod.p = rtl.string;',
  30583. '$mod.p = rtl.boolean;',
  30584. '$mod.p = rtl.boolean;',
  30585. '$mod.p = rtl.char;',
  30586. '$mod.p = rtl.char;',
  30587. '$mod.p = rtl.double;',
  30588. '$mod.p = rtl.double;',
  30589. '$mod.p = rtl.pointer;',
  30590. '$mod.p = rtl.pointer;',
  30591. '$mod.p = rtl.shortint;',
  30592. '$mod.p = rtl.shortint;',
  30593. '$mod.p = rtl.byte;',
  30594. '$mod.p = rtl.byte;',
  30595. '$mod.p = rtl.smallint;',
  30596. '$mod.p = rtl.smallint;',
  30597. '$mod.p = rtl.word;',
  30598. '$mod.p = rtl.word;',
  30599. '$mod.p = rtl.longword;',
  30600. '$mod.p = rtl.longword;',
  30601. '$mod.p = rtl.jsvalue;',
  30602. '$mod.p = rtl.jsvalue;',
  30603. '']));
  30604. end;
  30605. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30606. begin
  30607. WithTypeInfo:=true;
  30608. StartProgram(false);
  30609. Add([
  30610. 'type',
  30611. ' TCaption = type string;',
  30612. ' TYesNo = type boolean;',
  30613. ' TLetter = type char;',
  30614. ' TFloat = type double;',
  30615. ' TPtr = type pointer;',
  30616. ' TShortInt = type shortint;',
  30617. ' TByte = type byte;',
  30618. ' TSmallInt = type smallint;',
  30619. ' TWord = type word;',
  30620. ' TInt32 = type longint;',
  30621. ' TDWord = type longword;',
  30622. ' TValue = type jsvalue;',
  30623. ' TAliasValue = type TValue;',
  30624. 'var',
  30625. ' p: TPtr;',
  30626. ' a: TAliasValue;',
  30627. 'begin',
  30628. ' p:=typeinfo(tcaption);',
  30629. ' p:=typeinfo(tyesno);',
  30630. ' p:=typeinfo(tletter);',
  30631. ' p:=typeinfo(tfloat);',
  30632. ' p:=typeinfo(tptr);',
  30633. ' p:=typeinfo(tshortint);',
  30634. ' p:=typeinfo(tbyte);',
  30635. ' p:=typeinfo(tsmallint);',
  30636. ' p:=typeinfo(tword);',
  30637. ' p:=typeinfo(tdword);',
  30638. ' p:=typeinfo(tvalue);',
  30639. ' p:=typeinfo(taliasvalue);',
  30640. ' p:=typeinfo(a);',
  30641. '']);
  30642. ConvertProgram;
  30643. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30644. LinesToStr([ // statements
  30645. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30646. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30647. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30648. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30649. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30650. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30651. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30652. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30653. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30654. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30655. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30656. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30657. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30658. 'this.p = null;',
  30659. 'this.a = undefined;',
  30660. '']),
  30661. LinesToStr([ // $mod.$main
  30662. '$mod.p = $mod.$rtti["TCaption"];',
  30663. '$mod.p = $mod.$rtti["TYesNo"];',
  30664. '$mod.p = $mod.$rtti["TLetter"];',
  30665. '$mod.p = $mod.$rtti["TFloat"];',
  30666. '$mod.p = $mod.$rtti["TPtr"];',
  30667. '$mod.p = $mod.$rtti["TShortInt"];',
  30668. '$mod.p = $mod.$rtti["TByte"];',
  30669. '$mod.p = $mod.$rtti["TSmallInt"];',
  30670. '$mod.p = $mod.$rtti["TWord"];',
  30671. '$mod.p = $mod.$rtti["TDWord"];',
  30672. '$mod.p = $mod.$rtti["TValue"];',
  30673. '$mod.p = $mod.$rtti["TAliasValue"];',
  30674. '$mod.p = $mod.$rtti["TAliasValue"];',
  30675. '']));
  30676. end;
  30677. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30678. begin
  30679. WithTypeInfo:=true;
  30680. StartProgram(false);
  30681. Add('procedure DoIt;');
  30682. Add('type');
  30683. Add(' integer = longint;');
  30684. Add(' TPoint = record');
  30685. Add(' x,y: integer;');
  30686. Add(' end;');
  30687. Add('var p: pointer;');
  30688. Add('begin');
  30689. Add(' p:=typeinfo(tpoint);');
  30690. Add('end;');
  30691. Add('begin');
  30692. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30693. ConvertProgram;
  30694. end;
  30695. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30696. begin
  30697. WithTypeInfo:=true;
  30698. StartProgram(true,[supTypeInfo]);
  30699. Add([
  30700. '{$modeswitch externalclass}',
  30701. 'type',
  30702. ' TFlag = (up,down);',
  30703. ' TFlags = set of TFlag;',
  30704. 'var',
  30705. ' ti: TTypeInfo;',
  30706. ' tiInt: TTypeInfoInteger;',
  30707. ' tiEnum: TTypeInfoEnum;',
  30708. ' tiSet: TTypeInfoSet;',
  30709. 'begin',
  30710. ' ti:=typeinfo(string);',
  30711. ' ti:=typeinfo(boolean);',
  30712. ' ti:=typeinfo(char);',
  30713. ' ti:=typeinfo(double);',
  30714. ' tiInt:=typeinfo(shortint);',
  30715. ' tiInt:=typeinfo(byte);',
  30716. ' tiInt:=typeinfo(smallint);',
  30717. ' tiInt:=typeinfo(word);',
  30718. ' tiInt:=typeinfo(longint);',
  30719. ' tiInt:=typeinfo(longword);',
  30720. ' ti:=typeinfo(jsvalue);',
  30721. ' tiEnum:=typeinfo(tflag);',
  30722. ' tiSet:=typeinfo(tflags);']);
  30723. ConvertProgram;
  30724. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30725. LinesToStr([ // statements
  30726. 'this.TFlag = {',
  30727. ' "0": "up",',
  30728. ' up: 0,',
  30729. ' "1": "down",',
  30730. ' down: 1',
  30731. '};',
  30732. 'this.$rtti.$Enum("TFlag", {',
  30733. ' minvalue: 0,',
  30734. ' maxvalue: 1,',
  30735. ' ordtype: 1,',
  30736. ' enumtype: this.TFlag',
  30737. '});',
  30738. 'this.$rtti.$Set("TFlags", {',
  30739. ' comptype: this.$rtti["TFlag"]',
  30740. '});',
  30741. 'this.ti = null;',
  30742. 'this.tiInt = null;',
  30743. 'this.tiEnum = null;',
  30744. 'this.tiSet = null;',
  30745. '']),
  30746. LinesToStr([ // $mod.$main
  30747. '$mod.ti = rtl.string;',
  30748. '$mod.ti = rtl.boolean;',
  30749. '$mod.ti = rtl.char;',
  30750. '$mod.ti = rtl.double;',
  30751. '$mod.tiInt = rtl.shortint;',
  30752. '$mod.tiInt = rtl.byte;',
  30753. '$mod.tiInt = rtl.smallint;',
  30754. '$mod.tiInt = rtl.word;',
  30755. '$mod.tiInt = rtl.longint;',
  30756. '$mod.tiInt = rtl.longword;',
  30757. '$mod.ti = rtl.jsvalue;',
  30758. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30759. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30760. '']));
  30761. end;
  30762. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30763. begin
  30764. WithTypeInfo:=true;
  30765. StartProgram(true,[supTypeInfo]);
  30766. Add('{$modeswitch externalclass}');
  30767. Add('type');
  30768. Add(' TStaticArr = array[boolean] of string;');
  30769. Add(' TDynArr = array of string;');
  30770. Add(' TProc = procedure;');
  30771. Add(' TMethod = procedure of object;');
  30772. Add('var');
  30773. Add(' StaticArray: TStaticArr;');
  30774. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30775. Add(' DynArray: TDynArr;');
  30776. Add(' tiDynArray: TTypeInfoDynArray;');
  30777. Add(' ProcVar: TProc;');
  30778. Add(' tiProcVar: TTypeInfoProcVar;');
  30779. Add(' MethodVar: TMethod;');
  30780. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30781. Add('begin');
  30782. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30783. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30784. Add(' tiDynArray:=typeinfo(DynArray);');
  30785. Add(' tiDynArray:=typeinfo(TDynArr);');
  30786. Add(' tiProcVar:=typeinfo(ProcVar);');
  30787. Add(' tiProcVar:=typeinfo(TProc);');
  30788. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30789. Add(' tiMethodVar:=typeinfo(TMethod);');
  30790. ConvertProgram;
  30791. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30792. LinesToStr([ // statements
  30793. 'this.$rtti.$StaticArray("TStaticArr", {',
  30794. ' dims: [2],',
  30795. ' eltype: rtl.string',
  30796. '});',
  30797. 'this.$rtti.$DynArray("TDynArr", {',
  30798. ' eltype: rtl.string',
  30799. '});',
  30800. 'this.$rtti.$ProcVar("TProc", {',
  30801. ' procsig: rtl.newTIProcSig([])',
  30802. '});',
  30803. 'this.$rtti.$MethodVar("TMethod", {',
  30804. ' procsig: rtl.newTIProcSig([]),',
  30805. ' methodkind: 0',
  30806. '});',
  30807. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30808. 'this.tiStaticArray = null;',
  30809. 'this.DynArray = [];',
  30810. 'this.tiDynArray = null;',
  30811. 'this.ProcVar = null;',
  30812. 'this.tiProcVar = null;',
  30813. 'this.MethodVar = null;',
  30814. 'this.tiMethodVar = null;',
  30815. '']),
  30816. LinesToStr([ // $mod.$main
  30817. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30818. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30819. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30820. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30821. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30822. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30823. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30824. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30825. '']));
  30826. end;
  30827. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30828. begin
  30829. WithTypeInfo:=true;
  30830. StartProgram(true,[supTypeInfo]);
  30831. Add('{$modeswitch externalclass}');
  30832. Add('type');
  30833. Add(' TRec = record end;');
  30834. // ToDo: ^TRec
  30835. Add(' TObject = class end;');
  30836. Add(' TClass = class of tobject;');
  30837. Add('var');
  30838. Add(' Rec: trec;');
  30839. Add(' tiRecord: ttypeinforecord;');
  30840. Add(' Obj: tobject;');
  30841. Add(' tiClass: ttypeinfoclass;');
  30842. Add(' aClass: tclass;');
  30843. Add(' tiClassRef: ttypeinfoclassref;');
  30844. // ToDo: ^TRec
  30845. Add(' tiPointer: ttypeinfopointer;');
  30846. Add('begin');
  30847. Add(' tirecord:=typeinfo(trec);');
  30848. Add(' tirecord:=typeinfo(trec);');
  30849. Add(' ticlass:=typeinfo(obj);');
  30850. Add(' ticlass:=typeinfo(tobject);');
  30851. Add(' ticlass:=typeinfo(aclass);');
  30852. Add(' ticlassref:=typeinfo(tclass);');
  30853. ConvertProgram;
  30854. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30855. LinesToStr([ // statements
  30856. 'rtl.recNewT(this, "TRec", function () {',
  30857. ' this.$eq = function (b) {',
  30858. ' return true;',
  30859. ' };',
  30860. ' this.$assign = function (s) {',
  30861. ' return this;',
  30862. ' };',
  30863. ' $mod.$rtti.$Record("TRec", {});',
  30864. '});',
  30865. 'rtl.createClass(this, "TObject", null, function () {',
  30866. ' this.$init = function () {',
  30867. ' };',
  30868. ' this.$final = function () {',
  30869. ' };',
  30870. '});',
  30871. 'this.$rtti.$ClassRef("TClass", {',
  30872. ' instancetype: this.$rtti["TObject"]',
  30873. '});',
  30874. 'this.Rec = this.TRec.$new();',
  30875. 'this.tiRecord = null;',
  30876. 'this.Obj = null;',
  30877. 'this.tiClass = null;',
  30878. 'this.aClass = null;',
  30879. 'this.tiClassRef = null;',
  30880. 'this.tiPointer = null;',
  30881. '']),
  30882. LinesToStr([ // $mod.$main
  30883. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30884. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30885. '$mod.tiClass = $mod.Obj.$rtti;',
  30886. '$mod.tiClass = $mod.$rtti["TObject"];',
  30887. '$mod.tiClass = $mod.aClass.$rtti;',
  30888. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30889. '']));
  30890. end;
  30891. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30892. begin
  30893. WithTypeInfo:=true;
  30894. StartProgram(true,[supTypeInfo]);
  30895. Add([
  30896. '{$modeswitch externalclass}',
  30897. 'type',
  30898. ' TClass = class of tobject;',
  30899. ' TObject = class',
  30900. ' function MyClass: TClass;',
  30901. ' class function ClassType: TClass;',
  30902. ' end;',
  30903. 'function TObject.MyClass: TClass;',
  30904. 'var t: TTypeInfoClass;',
  30905. 'begin',
  30906. ' t:=TypeInfo(Self);',
  30907. ' t:=TypeInfo(Result);',
  30908. ' t:=TypeInfo(TObject);',
  30909. 'end;',
  30910. 'class function TObject.ClassType: TClass;',
  30911. 'var t: TTypeInfoClass;',
  30912. 'begin',
  30913. ' t:=TypeInfo(Self);',
  30914. ' t:=TypeInfo(Result);',
  30915. 'end;',
  30916. 'var',
  30917. ' Obj: TObject;',
  30918. ' t: TTypeInfoClass;',
  30919. 'begin',
  30920. ' t:=TypeInfo(TObject.ClassType);',
  30921. ' t:=TypeInfo(Obj.ClassType);',
  30922. ' t:=TypeInfo(Obj.MyClass);',
  30923. '']);
  30924. ConvertProgram;
  30925. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30926. LinesToStr([ // statements
  30927. 'this.$rtti.$Class("TObject");',
  30928. 'this.$rtti.$ClassRef("TClass", {',
  30929. ' instancetype: this.$rtti["TObject"]',
  30930. '});',
  30931. 'rtl.createClass(this, "TObject", null, function () {',
  30932. ' this.$init = function () {',
  30933. ' };',
  30934. ' this.$final = function () {',
  30935. ' };',
  30936. ' this.MyClass = function () {',
  30937. ' var Result = null;',
  30938. ' var t = null;',
  30939. ' t = this.$rtti;',
  30940. ' t = Result.$rtti;',
  30941. ' t = $mod.$rtti["TObject"];',
  30942. ' return Result;',
  30943. ' };',
  30944. ' this.ClassType = function () {',
  30945. ' var Result = null;',
  30946. ' var t = null;',
  30947. ' t = this.$rtti;',
  30948. ' t = Result.$rtti;',
  30949. ' return Result;',
  30950. ' };',
  30951. '});',
  30952. 'this.Obj = null;',
  30953. 'this.t = null;',
  30954. '']),
  30955. LinesToStr([ // $mod.$main
  30956. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30957. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30958. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30959. '']));
  30960. end;
  30961. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30962. begin
  30963. WithTypeInfo:=true;
  30964. AddModuleWithIntfImplSrc('typinfo.pas',
  30965. LinesToStr([
  30966. '{$modeswitch externalclass}',
  30967. 'type',
  30968. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30969. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30970. '']),
  30971. '');
  30972. AddModuleWithIntfImplSrc('unit2.pas',
  30973. LinesToStr([
  30974. 'uses typinfo;',
  30975. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30976. 'procedure DoPtr(p: PTypeInfo);',
  30977. 'procedure DoInfo(t: TTypeInfo);',
  30978. 'procedure DoInt(t: TTypeInfoInteger);',
  30979. '']),
  30980. LinesToStr([
  30981. 'procedure DoPtr(p: PTypeInfo);',
  30982. 'begin end;',
  30983. 'procedure DoInfo(t: TTypeInfo);',
  30984. 'begin end;',
  30985. 'procedure DoInt(t: TTypeInfoInteger);',
  30986. 'begin end;',
  30987. '']));
  30988. StartUnit(true);
  30989. Add([
  30990. 'interface',
  30991. 'uses unit2;', // does not use unit typinfo
  30992. 'implementation',
  30993. 'var',
  30994. ' i: byte;',
  30995. ' p: pointer;',
  30996. ' t: PTypeInfo;',
  30997. 'initialization',
  30998. ' p:=typeinfo(i);',
  30999. ' t:=typeinfo(i);',
  31000. ' if p=t then ;',
  31001. ' if p=typeinfo(i) then ;',
  31002. ' if typeinfo(i)=p then ;',
  31003. ' if t=typeinfo(i) then ;',
  31004. ' if typeinfo(i)=t then ;',
  31005. ' DoPtr(p);',
  31006. ' DoPtr(t);',
  31007. ' DoPtr(typeinfo(i));',
  31008. ' DoInfo(p);',
  31009. ' DoInfo(t);',
  31010. ' DoInfo(typeinfo(i));',
  31011. ' DoInt(typeinfo(i));',
  31012. '']);
  31013. ConvertUnit;
  31014. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  31015. LinesToStr([ // statements
  31016. 'var $impl = $mod.$impl;',
  31017. '']),
  31018. LinesToStr([ // this.$init
  31019. '$impl.p = rtl.byte;',
  31020. '$impl.t = rtl.byte;',
  31021. 'if ($impl.p === $impl.t) ;',
  31022. 'if ($impl.p === rtl.byte) ;',
  31023. 'if (rtl.byte === $impl.p) ;',
  31024. 'if ($impl.t === rtl.byte) ;',
  31025. 'if (rtl.byte === $impl.t) ;',
  31026. 'pas.unit2.DoPtr($impl.p);',
  31027. 'pas.unit2.DoPtr($impl.t);',
  31028. 'pas.unit2.DoPtr(rtl.byte);',
  31029. 'pas.unit2.DoInfo($impl.p);',
  31030. 'pas.unit2.DoInfo($impl.t);',
  31031. 'pas.unit2.DoInfo(rtl.byte);',
  31032. 'pas.unit2.DoInt(rtl.byte);',
  31033. '']),
  31034. LinesToStr([ // implementation
  31035. '$impl.i = 0;',
  31036. '$impl.p = null;',
  31037. '$impl.t = null;',
  31038. '']) );
  31039. end;
  31040. procedure TTestModule.TestRTTI_Interface_Corba;
  31041. begin
  31042. WithTypeInfo:=true;
  31043. StartProgram(true,[supTypeInfo]);
  31044. Add([
  31045. '{$interfaces corba}',
  31046. '{$modeswitch externalclass}',
  31047. 'type',
  31048. ' IUnknown = interface',
  31049. ' end;',
  31050. ' IBird = interface',
  31051. ' function GetItem: longint;',
  31052. ' procedure SetItem(Value: longint);',
  31053. ' property Item: longint read GetItem write SetItem;',
  31054. ' end;',
  31055. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  31056. 'var',
  31057. ' i: IBird;',
  31058. ' t: TTypeInfoInterface;',
  31059. 'begin',
  31060. ' t:=TypeInfo(IBird);',
  31061. ' t:=TypeInfo(i);',
  31062. ' DoIt(t);',
  31063. ' DoIt(TypeInfo(IBird));',
  31064. '']);
  31065. ConvertProgram;
  31066. CheckSource('TestRTTI_Interface_Corba',
  31067. LinesToStr([ // statements
  31068. 'rtl.createInterface(',
  31069. ' this,',
  31070. ' "IUnknown",',
  31071. ' "{B92D5841-758A-322B-B800-000000000000}",',
  31072. ' [],',
  31073. ' null,',
  31074. ' function () {',
  31075. ' }',
  31076. ');',
  31077. 'rtl.createInterface(',
  31078. ' this,',
  31079. ' "IBird",',
  31080. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  31081. ' ["GetItem", "SetItem"],',
  31082. ' null,',
  31083. ' function () {',
  31084. ' var $r = this.$rtti;',
  31085. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31086. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  31087. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  31088. ' }',
  31089. ');',
  31090. 'this.DoIt = function (t) {',
  31091. '}; ',
  31092. 'this.i = null;',
  31093. 'this.t = null;',
  31094. '']),
  31095. LinesToStr([ // $mod.$main
  31096. '$mod.t = $mod.$rtti["IBird"];',
  31097. '$mod.t = $mod.i.$rtti;',
  31098. '$mod.DoIt($mod.t);',
  31099. '$mod.DoIt($mod.$rtti["IBird"]);',
  31100. '']));
  31101. end;
  31102. procedure TTestModule.TestRTTI_Interface_COM;
  31103. begin
  31104. WithTypeInfo:=true;
  31105. StartProgram(true,[supTypeInfo]);
  31106. Add([
  31107. '{$interfaces com}',
  31108. '{$modeswitch externalclass}',
  31109. 'type',
  31110. ' TGuid = record end;',
  31111. ' integer = longint;',
  31112. ' IUnknown = interface',
  31113. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  31114. ' function _AddRef: Integer;',
  31115. ' function _Release: Integer;',
  31116. ' end;',
  31117. ' IBird = interface',
  31118. ' function GetItem: longint;',
  31119. ' procedure SetItem(Value: longint);',
  31120. ' property Item: longint read GetItem write SetItem;',
  31121. ' end;',
  31122. 'var',
  31123. ' i: IBird;',
  31124. ' t: TTypeInfoInterface;',
  31125. 'begin',
  31126. ' t:=TypeInfo(IBird);',
  31127. ' t:=TypeInfo(i);',
  31128. '']);
  31129. ConvertProgram;
  31130. CheckSource('TestRTTI_Interface_COM',
  31131. LinesToStr([ // statements
  31132. 'rtl.recNewT(this, "TGuid", function () {',
  31133. ' this.$eq = function (b) {',
  31134. ' return true;',
  31135. ' };',
  31136. ' this.$assign = function (s) {',
  31137. ' return this;',
  31138. ' };',
  31139. ' $mod.$rtti.$Record("TGuid", {});',
  31140. '});',
  31141. 'rtl.createInterface(',
  31142. ' this,',
  31143. ' "IUnknown",',
  31144. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  31145. ' ["QueryInterface", "_AddRef", "_Release"],',
  31146. ' null,',
  31147. ' function () {',
  31148. ' this.$kind = "com";',
  31149. ' var $r = this.$rtti;',
  31150. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  31151. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  31152. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  31153. ' }',
  31154. ');',
  31155. 'rtl.createInterface(',
  31156. ' this,',
  31157. ' "IBird",',
  31158. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  31159. ' ["GetItem", "SetItem"],',
  31160. ' this.IUnknown,',
  31161. ' function () {',
  31162. ' var $r = this.$rtti;',
  31163. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31164. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  31165. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  31166. ' }',
  31167. ');',
  31168. 'this.i = null;',
  31169. 'this.t = null;',
  31170. '']),
  31171. LinesToStr([ // $mod.$main
  31172. '$mod.t = $mod.$rtti["IBird"];',
  31173. '$mod.t = $mod.i.$rtti;',
  31174. '']));
  31175. end;
  31176. procedure TTestModule.TestRTTI_ClassHelper;
  31177. begin
  31178. WithTypeInfo:=true;
  31179. StartProgram(true,[supTypeInfo]);
  31180. Add([
  31181. '{$interfaces com}',
  31182. '{$modeswitch externalclass}',
  31183. 'type',
  31184. ' TObject = class',
  31185. ' end;',
  31186. ' THelper = class helper for TObject',
  31187. ' published',
  31188. ' function GetItem: longint;',
  31189. ' property Item: longint read GetItem;',
  31190. ' end;',
  31191. 'function THelper.GetItem: longint;',
  31192. 'begin',
  31193. 'end;',
  31194. 'var',
  31195. ' t: TTypeInfoHelper;',
  31196. 'begin',
  31197. ' t:=TypeInfo(THelper);',
  31198. '']);
  31199. ConvertProgram;
  31200. CheckSource('TestRTTI_ClassHelper',
  31201. LinesToStr([ // statements
  31202. 'rtl.createClass(this, "TObject", null, function () {',
  31203. ' this.$init = function () {',
  31204. ' };',
  31205. ' this.$final = function () {',
  31206. ' };',
  31207. '});',
  31208. 'rtl.createHelper(this, "THelper", null, function () {',
  31209. ' this.GetItem = function () {',
  31210. ' var Result = 0;',
  31211. ' return Result;',
  31212. ' };',
  31213. ' var $r = this.$rtti;',
  31214. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31215. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  31216. '});',
  31217. 'this.t = null;',
  31218. '']),
  31219. LinesToStr([ // $mod.$main
  31220. '$mod.t = $mod.$rtti["THelper"];',
  31221. '']));
  31222. end;
  31223. procedure TTestModule.TestRTTI_ExternalClass;
  31224. begin
  31225. WithTypeInfo:=true;
  31226. StartProgram(true,[supTypeInfo]);
  31227. Add([
  31228. '{$modeswitch externalclass}',
  31229. 'type',
  31230. ' TJSObject = class external name ''Object''',
  31231. ' end;',
  31232. ' TJSArray = class external name ''Array'' (TJSObject)',
  31233. ' end;',
  31234. 'var',
  31235. ' p: Pointer;',
  31236. ' tc: TTypeInfoExtClass;',
  31237. 'begin',
  31238. ' p:=typeinfo(TJSArray);']);
  31239. ConvertProgram;
  31240. CheckSource('TestRTTI_ExternalClass',
  31241. LinesToStr([ // statements
  31242. 'this.$rtti.$ExtClass("TJSObject", {',
  31243. ' jsclass: "Object"',
  31244. '});',
  31245. 'this.$rtti.$ExtClass("TJSArray", {',
  31246. ' ancestor: this.$rtti["TJSObject"],',
  31247. ' jsclass: "Array"',
  31248. '});',
  31249. 'this.p = null;',
  31250. 'this.tc = null;',
  31251. '']),
  31252. LinesToStr([ // $mod.$main
  31253. '$mod.p = $mod.$rtti["TJSArray"];',
  31254. '']));
  31255. end;
  31256. procedure TTestModule.TestRTTI_Unit;
  31257. begin
  31258. WithTypeInfo:=true;
  31259. AddModuleWithIntfImplSrc('unit2.pas',
  31260. LinesToStr([
  31261. '{$mode delphi}',
  31262. 'type',
  31263. ' TWordArray = array of word;',
  31264. ' TArray<T> = array of T;',
  31265. '']),
  31266. '');
  31267. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  31268. Add([
  31269. '{$mode delphi}',
  31270. 'interface',
  31271. 'uses unit2;',
  31272. 'type',
  31273. ' IBird = interface',
  31274. ' function Swoop: TWordArray;',
  31275. ' function Glide: TArray<word>;',
  31276. ' end;',
  31277. 'procedure Fly;',
  31278. 'implementation',
  31279. 'procedure Fly;',
  31280. 'var',
  31281. ' ta: tTypeInfoDynArray;',
  31282. ' ti: tTypeInfoInterface;',
  31283. 'begin',
  31284. ' ta:=typeinfo(TWordArray);',
  31285. ' ta:=typeinfo(TArray<word>);',
  31286. ' ti:=typeinfo(IBird);',
  31287. 'end;',
  31288. '']);
  31289. ConvertUnit;
  31290. CheckSource('TestRTTI_ExternalClass',
  31291. LinesToStr([ // statements
  31292. 'rtl.createInterface(',
  31293. ' this,',
  31294. ' "IBird",',
  31295. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  31296. ' ["Swoop", "Glide"],',
  31297. ' pas.system.IUnknown,',
  31298. ' function () {',
  31299. ' var $r = this.$rtti;',
  31300. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  31301. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  31302. ' }',
  31303. ');',
  31304. 'this.Fly = function () {',
  31305. ' var ta = null;',
  31306. ' var ti = null;',
  31307. ' ta = pas.unit2.$rtti["TWordArray"];',
  31308. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  31309. ' ti = $mod.$rtti["IBird"];',
  31310. '};',
  31311. '']),
  31312. LinesToStr([ // $mod.$main
  31313. '']));
  31314. end;
  31315. procedure TTestModule.TestResourcestringProgram;
  31316. begin
  31317. AddModuleWithIntfImplSrc('unit2.pas',
  31318. LinesToStr([
  31319. 'resourcestring Title = ''Nice'';',
  31320. '']),
  31321. '');
  31322. StartProgram(true);
  31323. Add([
  31324. 'uses unit2;',
  31325. 'const Bar = ''bar'';',
  31326. 'resourcestring',
  31327. ' Red = ''red'';',
  31328. ' Foobar = ''fOo''+bar;',
  31329. 'var s: string;',
  31330. ' c: char;',
  31331. 'begin',
  31332. ' s:=red;',
  31333. ' s:=test1.red;',
  31334. ' s:=Title;',
  31335. ' c:=red[1];',
  31336. ' c:=test1.red[2];',
  31337. ' if red=foobar then ;',
  31338. ' if red[3]=red[4] then ;']);
  31339. ConvertProgram;
  31340. CheckSource('TestResourcestringProgram',
  31341. LinesToStr([ // statements
  31342. 'this.Bar = "bar";',
  31343. 'this.s = "";',
  31344. 'this.c = "";',
  31345. '$mod.$resourcestrings = {',
  31346. ' Red: {',
  31347. ' org: "red"',
  31348. ' },',
  31349. ' Foobar: {',
  31350. ' org: "fOobar"',
  31351. ' }',
  31352. '};',
  31353. '']),
  31354. LinesToStr([ // $mod.$main
  31355. '$mod.s = rtl.getResStr($mod, "Red");',
  31356. '$mod.s = rtl.getResStr($mod, "Red");',
  31357. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31358. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  31359. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  31360. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  31361. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  31362. '']));
  31363. end;
  31364. procedure TTestModule.TestResourcestringUnit;
  31365. begin
  31366. AddModuleWithIntfImplSrc('unit2.pas',
  31367. LinesToStr([
  31368. 'resourcestring Title = ''Nice'';',
  31369. '']),
  31370. '');
  31371. StartUnit(true);
  31372. Add([
  31373. 'interface',
  31374. 'uses unit2;',
  31375. 'const Red = ''rEd'';',
  31376. 'resourcestring',
  31377. ' Blue = ''blue'';',
  31378. ' NotRed = ''not''+Red;',
  31379. 'var s: string;',
  31380. 'implementation',
  31381. 'resourcestring',
  31382. ' ImplGreen = ''green'';',
  31383. 'initialization',
  31384. ' s:=blue+ImplGreen;',
  31385. ' s:=test1.blue+test1.implgreen;',
  31386. ' s:=blue[1]+implgreen[2];',
  31387. ' s:=Title;',
  31388. '']);
  31389. ConvertUnit;
  31390. CheckSource('TestResourcestringUnit',
  31391. LinesToStr([ // statements
  31392. 'this.Red = "rEd";',
  31393. 'this.s = "";',
  31394. '$mod.$resourcestrings = {',
  31395. ' Blue: {',
  31396. ' org: "blue"',
  31397. ' },',
  31398. ' NotRed: {',
  31399. ' org: "notrEd"',
  31400. ' },',
  31401. ' ImplGreen: {',
  31402. ' org: "green"',
  31403. ' }',
  31404. '};',
  31405. '']),
  31406. LinesToStr([ // $mod.$main
  31407. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31408. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31409. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  31410. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31411. '']));
  31412. end;
  31413. procedure TTestModule.TestResourcestringImplementation;
  31414. begin
  31415. StartUnit(false);
  31416. Add([
  31417. 'interface',
  31418. 'implementation',
  31419. 'resourcestring',
  31420. ' ImplRed = ''red'';']);
  31421. ConvertUnit;
  31422. CheckSource('TestResourcestringImplementation',
  31423. LinesToStr([ // intf statements
  31424. 'var $impl = $mod.$impl;']),
  31425. LinesToStr([ // $mod.$init
  31426. '']),
  31427. LinesToStr([ // impl statements
  31428. '$mod.$resourcestrings = {',
  31429. ' ImplRed: {',
  31430. ' org: "red"',
  31431. ' }',
  31432. '};',
  31433. '']));
  31434. end;
  31435. procedure TTestModule.TestAttributes_Members;
  31436. begin
  31437. WithTypeInfo:=true;
  31438. StartProgram(false);
  31439. Add([
  31440. '{$modeswitch PrefixedAttributes}',
  31441. 'type',
  31442. ' TObject = class',
  31443. ' constructor Create;',
  31444. ' end;',
  31445. ' TCustomAttribute = class',
  31446. ' constructor Create(Id: word);',
  31447. ' end;',
  31448. ' [Missing]',
  31449. ' TBird = class',
  31450. ' published',
  31451. ' [Tcustom]',
  31452. ' FField: word;',
  31453. ' [tcustom(14)]',
  31454. ' property Size: word read FField;',
  31455. ' [Tcustom(15)]',
  31456. ' procedure Fly; virtual; abstract;',
  31457. ' end;',
  31458. ' TRec = record',
  31459. ' [Tcustom,tcustom(14)]',
  31460. ' Size: word;',
  31461. ' end;',
  31462. 'constructor TObject.Create; begin end;',
  31463. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31464. 'begin',
  31465. '']);
  31466. ConvertProgram;
  31467. CheckSource('TestAttributes_Members',
  31468. LinesToStr([ // statements
  31469. 'rtl.createClass(this, "TObject", null, function () {',
  31470. ' this.$init = function () {',
  31471. ' };',
  31472. ' this.$final = function () {',
  31473. ' };',
  31474. ' this.Create = function () {',
  31475. ' return this;',
  31476. ' };',
  31477. '});',
  31478. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31479. ' this.Create$1 = function (Id) {',
  31480. ' return this;',
  31481. ' };',
  31482. '});',
  31483. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31484. ' this.$init = function () {',
  31485. ' $mod.TObject.$init.call(this);',
  31486. ' this.FField = 0;',
  31487. ' };',
  31488. ' var $r = this.$rtti;',
  31489. ' $r.addField("FField", rtl.word, {',
  31490. ' attr: [$mod.TCustomAttribute, "Create"]',
  31491. ' });',
  31492. ' $r.addProperty(',
  31493. ' "Size",',
  31494. ' 0,',
  31495. ' rtl.word,',
  31496. ' "FField",',
  31497. ' "",',
  31498. ' {',
  31499. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31500. ' }',
  31501. ' );',
  31502. ' $r.addMethod("Fly", 0, [], null, 0, {',
  31503. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31504. ' });',
  31505. '});',
  31506. 'rtl.recNewT(this, "TRec", function () {',
  31507. ' this.Size = 0;',
  31508. ' this.$eq = function (b) {',
  31509. ' return this.Size === b.Size;',
  31510. ' };',
  31511. ' this.$assign = function (s) {',
  31512. ' this.Size = s.Size;',
  31513. ' return this;',
  31514. ' };',
  31515. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31516. ' $r.addField("Size", rtl.word, {',
  31517. ' attr: [',
  31518. ' $mod.TCustomAttribute,',
  31519. ' "Create",',
  31520. ' $mod.TCustomAttribute,',
  31521. ' "Create$1",',
  31522. ' [14]',
  31523. ' ]',
  31524. ' });',
  31525. '});',
  31526. '']),
  31527. LinesToStr([ // $mod.$main
  31528. '']));
  31529. end;
  31530. procedure TTestModule.TestAttributes_Types;
  31531. begin
  31532. WithTypeInfo:=true;
  31533. StartProgram(false);
  31534. Add([
  31535. '{$modeswitch PrefixedAttributes}',
  31536. 'type',
  31537. ' TObject = class',
  31538. ' constructor Create(Id: word);',
  31539. ' end;',
  31540. ' TCustomAttribute = class',
  31541. ' end;',
  31542. ' [TCustom(1)]',
  31543. ' TMyClass = class',
  31544. ' end;',
  31545. ' [TCustom(11)]',
  31546. ' TMyDescendant = class(TMyClass)',
  31547. ' end;',
  31548. ' [TCustom(2)]',
  31549. ' TRec = record',
  31550. ' end;',
  31551. ' [TCustom(3)]',
  31552. ' TInt = type word;',
  31553. 'constructor TObject.Create(Id: word);',
  31554. 'begin',
  31555. 'end;',
  31556. 'var p: pointer;',
  31557. 'begin',
  31558. ' p:=typeinfo(TMyClass);',
  31559. ' p:=typeinfo(TRec);',
  31560. ' p:=typeinfo(TInt);',
  31561. '']);
  31562. ConvertProgram;
  31563. CheckSource('TestAttributes_Types',
  31564. LinesToStr([ // statements
  31565. 'rtl.createClass(this, "TObject", null, function () {',
  31566. ' this.$init = function () {',
  31567. ' };',
  31568. ' this.$final = function () {',
  31569. ' };',
  31570. ' this.Create = function (Id) {',
  31571. ' return this;',
  31572. ' };',
  31573. '});',
  31574. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31575. '});',
  31576. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31577. ' var $r = this.$rtti;',
  31578. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31579. '});',
  31580. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31581. ' var $r = this.$rtti;',
  31582. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31583. '});',
  31584. 'rtl.recNewT(this, "TRec", function () {',
  31585. ' this.$eq = function (b) {',
  31586. ' return true;',
  31587. ' };',
  31588. ' this.$assign = function (s) {',
  31589. ' return this;',
  31590. ' };',
  31591. ' $mod.$rtti.$Record("TRec", {',
  31592. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31593. ' });',
  31594. '});',
  31595. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31596. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31597. '});',
  31598. 'this.p = null;',
  31599. '']),
  31600. LinesToStr([ // $mod.$main
  31601. '$mod.p = $mod.$rtti["TMyClass"];',
  31602. '$mod.p = $mod.$rtti["TRec"];',
  31603. '$mod.p = $mod.$rtti["TInt"];',
  31604. '']));
  31605. end;
  31606. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31607. begin
  31608. WithTypeInfo:=true;
  31609. StartProgram(false);
  31610. Add([
  31611. '{$modeswitch PrefixedAttributes}',
  31612. 'type',
  31613. ' TObject = class',
  31614. ' constructor Create;',
  31615. ' end;',
  31616. ' TCustomAttribute = class',
  31617. ' end;',
  31618. ' THelper = class helper for TCustomAttribute',
  31619. ' constructor Create(Id: word);',
  31620. ' end;',
  31621. ' [TCustom(3)]',
  31622. ' TMyInt = word;',
  31623. 'constructor TObject.Create; begin end;',
  31624. 'constructor THelper.Create(Id: word); begin end;',
  31625. 'begin',
  31626. ' if typeinfo(TMyInt)=nil then ;']);
  31627. ConvertProgram;
  31628. end;
  31629. procedure TTestModule.TestAttributes_InterfacesList;
  31630. begin
  31631. WithTypeInfo:=true;
  31632. StartProgram(false);
  31633. Add([
  31634. '{$mode Delphi}',
  31635. 'type',
  31636. ' TObject = class',
  31637. ' constructor Create;',
  31638. ' end;',
  31639. ' IInterface = interface end;',
  31640. ' TCustomAttribute = class',
  31641. ' end;',
  31642. ' Red = class(TCustomAttribute);',
  31643. ' Blue = class(TCustomAttribute);',
  31644. ' [Red]',
  31645. ' IBird<T> = interface',
  31646. ' procedure Fly;',
  31647. ' end;',
  31648. ' [Blue]',
  31649. ' IEagle = interface(IBird<Word>)',
  31650. ' procedure Dive;',
  31651. ' end;',
  31652. ' TAnt = class(TObject, IEagle)',
  31653. ' procedure Fly; virtual; abstract;',
  31654. ' procedure Dive; virtual; abstract;',
  31655. ' end;',
  31656. 'constructor TObject.Create;',
  31657. 'begin',
  31658. 'end;',
  31659. 'begin',
  31660. '']);
  31661. ConvertProgram;
  31662. CheckSource('TestAttributes_InterfacesList',
  31663. LinesToStr([ // statements
  31664. '$mod.$rtti.$Interface("IBird<System.Word>");',
  31665. 'rtl.createClass(this, "TObject", null, function () {',
  31666. ' this.$init = function () {',
  31667. ' };',
  31668. ' this.$final = function () {',
  31669. ' };',
  31670. ' this.Create = function () {',
  31671. ' return this;',
  31672. ' };',
  31673. '});',
  31674. 'rtl.createInterface(',
  31675. ' this,',
  31676. ' "IInterface",',
  31677. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  31678. ' [],',
  31679. ' null,',
  31680. ' function () {',
  31681. ' this.$kind = "com";',
  31682. ' }',
  31683. ');',
  31684. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31685. '});',
  31686. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  31687. '});',
  31688. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  31689. '});',
  31690. 'rtl.createInterface(',
  31691. ' this,',
  31692. ' "IBird$G1",',
  31693. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  31694. ' ["Fly"],',
  31695. ' this.IInterface,',
  31696. ' function () {',
  31697. ' var $r = this.$rtti;',
  31698. ' $r.addMethod("Fly", 0, []);',
  31699. ' $r.attr = [$mod.Red, "Create"];',
  31700. ' },',
  31701. ' "IBird<System.Word>"',
  31702. ');',
  31703. 'rtl.createInterface(',
  31704. ' this,',
  31705. ' "IEagle",',
  31706. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  31707. ' ["Dive"],',
  31708. ' this.IBird$G1,',
  31709. ' function () {',
  31710. ' var $r = this.$rtti;',
  31711. ' $r.addMethod("Dive", 0, []);',
  31712. ' $r.attr = [$mod.Blue, "Create"];',
  31713. ' }',
  31714. ');',
  31715. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  31716. ' rtl.addIntf(this, $mod.IEagle);',
  31717. '});',
  31718. '']),
  31719. LinesToStr([ // $mod.$main
  31720. '']));
  31721. end;
  31722. procedure TTestModule.TestAssert;
  31723. begin
  31724. StartProgram(false);
  31725. Add([
  31726. 'procedure DoIt;',
  31727. 'var',
  31728. ' b: boolean;',
  31729. ' s: string;',
  31730. 'begin',
  31731. ' {$Assertions on}',
  31732. ' Assert(b);',
  31733. 'end;',
  31734. 'begin',
  31735. ' DoIt;',
  31736. '']);
  31737. ConvertProgram;
  31738. CheckSource('TestAssert',
  31739. LinesToStr([ // statements
  31740. 'this.DoIt = function () {',
  31741. ' var b = false;',
  31742. ' var s = "";',
  31743. ' if (!b) throw "assert failed";',
  31744. '};',
  31745. '']),
  31746. LinesToStr([ // $mod.$main
  31747. '$mod.DoIt();',
  31748. '']));
  31749. end;
  31750. procedure TTestModule.TestAssert_SysUtils;
  31751. begin
  31752. AddModuleWithIntfImplSrc('SysUtils.pas',
  31753. LinesToStr([
  31754. 'type',
  31755. ' TObject = class',
  31756. ' constructor Create;',
  31757. ' end;',
  31758. ' EAssertionFailed = class',
  31759. ' constructor Create(s: string);',
  31760. ' end;',
  31761. '']),
  31762. LinesToStr([
  31763. 'constructor TObject.Create;',
  31764. 'begin end;',
  31765. 'constructor EAssertionFailed.Create(s: string);',
  31766. 'begin end;',
  31767. '']) );
  31768. StartProgram(true);
  31769. Add([
  31770. 'uses sysutils;',
  31771. 'procedure DoIt;',
  31772. 'var',
  31773. ' b: boolean;',
  31774. ' s: string;',
  31775. 'begin',
  31776. ' {$Assertions on}',
  31777. ' Assert(b);',
  31778. ' Assert(b,''msg'');',
  31779. 'end;',
  31780. 'begin',
  31781. ' DoIt;',
  31782. '']);
  31783. ConvertProgram;
  31784. CheckSource('TestAssert_SysUtils',
  31785. LinesToStr([ // statements
  31786. 'this.DoIt = function () {',
  31787. ' var b = false;',
  31788. ' var s = "";',
  31789. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31790. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31791. '};',
  31792. '']),
  31793. LinesToStr([ // $mod.$main
  31794. '$mod.DoIt();',
  31795. '']));
  31796. end;
  31797. procedure TTestModule.TestObjectChecks;
  31798. begin
  31799. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31800. StartProgram(false);
  31801. Add([
  31802. 'type',
  31803. ' TObject = class',
  31804. ' procedure DoIt;',
  31805. ' end;',
  31806. ' TClass = class of tobject;',
  31807. ' TBird = class',
  31808. ' end;',
  31809. ' TBirdClass = class of TBird;',
  31810. 'var',
  31811. ' o : TObject;',
  31812. ' c: TClass;',
  31813. ' b: TBird;',
  31814. ' bc: TBirdClass;',
  31815. 'procedure TObject.DoIt;',
  31816. 'begin',
  31817. ' b:=TBird(o);',
  31818. 'end;',
  31819. 'begin',
  31820. ' o.DoIt;',
  31821. ' b:=TBird(o);',
  31822. ' bc:=TBirdClass(c);',
  31823. '']);
  31824. ConvertProgram;
  31825. CheckSource('TestCheckMethodCall',
  31826. LinesToStr([ // statements
  31827. 'rtl.createClass(this, "TObject", null, function () {',
  31828. ' this.$init = function () {',
  31829. ' };',
  31830. ' this.$final = function () {',
  31831. ' };',
  31832. ' this.DoIt = function () {',
  31833. ' rtl.checkMethodCall(this,$mod.TObject);',
  31834. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31835. ' };',
  31836. '});',
  31837. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31838. '});',
  31839. 'this.o = null;',
  31840. 'this.c = null;',
  31841. 'this.b = null;',
  31842. 'this.bc = null;',
  31843. '']),
  31844. LinesToStr([ // $mod.$main
  31845. '$mod.o.DoIt();',
  31846. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31847. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31848. '']));
  31849. end;
  31850. procedure TTestModule.TestOverflowChecks_Int;
  31851. begin
  31852. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31853. StartProgram(false);
  31854. Add([
  31855. 'procedure DoIt;',
  31856. 'var',
  31857. ' b: byte;',
  31858. ' n: nativeint;',
  31859. ' u: nativeuint;',
  31860. ' c: currency;',
  31861. 'begin',
  31862. ' n:=n+n;',
  31863. ' n:=n-n;',
  31864. ' n:=n+b;',
  31865. ' n:=b-n;',
  31866. ' n:=n*n;',
  31867. ' n:=n*u;',
  31868. ' c:=c+b;',
  31869. ' c:=b+c;',
  31870. ' c:=c*b;',
  31871. ' c:=b*c;',
  31872. 'end;',
  31873. 'begin',
  31874. '']);
  31875. ConvertProgram;
  31876. CheckSource('TestOverflowChecks_Int',
  31877. LinesToStr([ // statements
  31878. 'this.DoIt = function () {',
  31879. ' var b = 0;',
  31880. ' var n = 0;',
  31881. ' var u = 0;',
  31882. ' var c = 0;',
  31883. ' n = rtl.oc(n + n);',
  31884. ' n = rtl.oc(n - n);',
  31885. ' n = rtl.oc(n + b);',
  31886. ' n = rtl.oc(b - n);',
  31887. ' n = rtl.oc(n * n);',
  31888. ' n = rtl.oc(n * u);',
  31889. ' c = rtl.oc(c + (b * 10000));',
  31890. ' c = rtl.oc((b * 10000) + c);',
  31891. ' c = rtl.oc(c * b);',
  31892. ' c = rtl.oc(b * c);',
  31893. '};',
  31894. '']),
  31895. LinesToStr([ // $mod.$main
  31896. '']));
  31897. end;
  31898. procedure TTestModule.TestRangeChecks_AssignInt;
  31899. begin
  31900. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31901. StartProgram(false);
  31902. Add([
  31903. '{$R+}',
  31904. 'var',
  31905. ' b: byte = 2;',
  31906. ' w: word = 3;',
  31907. 'procedure DoIt(p: byte);',
  31908. 'begin',
  31909. ' b:=w;',
  31910. ' b+=w;',
  31911. ' b:=1;',
  31912. 'end;',
  31913. '{$R-}',
  31914. 'procedure DoSome;',
  31915. 'begin',
  31916. ' DoIt(w);',
  31917. ' b:=w;',
  31918. ' b:=2;',
  31919. 'end;',
  31920. 'begin',
  31921. '{$R+}',
  31922. '']);
  31923. ConvertProgram;
  31924. CheckSource('TestRangeChecks_AssignInt',
  31925. LinesToStr([ // statements
  31926. 'this.b = 2;',
  31927. 'this.w = 3;',
  31928. 'this.DoIt = function (p) {',
  31929. ' rtl.rc(p, 0, 255);',
  31930. ' $mod.b = rtl.rc($mod.w,0,255);',
  31931. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31932. ' $mod.b = 1;',
  31933. '};',
  31934. 'this.DoSome = function () {',
  31935. ' $mod.DoIt($mod.w);',
  31936. ' $mod.b = $mod.w;',
  31937. ' $mod.b = 2;',
  31938. '};',
  31939. '']),
  31940. LinesToStr([ // $mod.$main
  31941. '']));
  31942. end;
  31943. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31944. begin
  31945. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31946. StartProgram(false);
  31947. Add([
  31948. '{$R+}',
  31949. 'type Ten = 1..10;',
  31950. 'var',
  31951. ' b: Ten = 2;',
  31952. ' w: Ten = 3;',
  31953. 'procedure DoIt(p: Ten);',
  31954. 'begin',
  31955. ' b:=w;',
  31956. ' b+=w;',
  31957. ' b:=1;',
  31958. 'end;',
  31959. '{$R-}',
  31960. 'procedure DoSome;',
  31961. 'begin',
  31962. ' DoIt(w);',
  31963. ' b:=w;',
  31964. ' b:=2;',
  31965. 'end;',
  31966. 'begin',
  31967. '{$R+}',
  31968. '']);
  31969. ConvertProgram;
  31970. CheckSource('TestRangeChecks_AssignIntRange',
  31971. LinesToStr([ // statements
  31972. 'this.b = 2;',
  31973. 'this.w = 3;',
  31974. 'this.DoIt = function (p) {',
  31975. ' rtl.rc(p, 1, 10);',
  31976. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31977. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31978. ' $mod.b = 1;',
  31979. '};',
  31980. 'this.DoSome = function () {',
  31981. ' $mod.DoIt($mod.w);',
  31982. ' $mod.b = $mod.w;',
  31983. ' $mod.b = 2;',
  31984. '};',
  31985. '']),
  31986. LinesToStr([ // $mod.$main
  31987. '']));
  31988. end;
  31989. procedure TTestModule.TestRangeChecks_AssignEnum;
  31990. begin
  31991. StartProgram(false);
  31992. Add([
  31993. '{$R+}',
  31994. 'type TEnum = (red,green);',
  31995. 'var',
  31996. ' e: TEnum = red;',
  31997. 'procedure DoIt(p: TEnum);',
  31998. 'begin',
  31999. ' e:=p;',
  32000. ' p:=TEnum(0);',
  32001. ' p:=succ(e);',
  32002. 'end;',
  32003. '{$R-}',
  32004. 'procedure DoSome;',
  32005. 'begin',
  32006. ' DoIt(e);',
  32007. ' e:=TEnum(1);',
  32008. ' e:=pred(e);',
  32009. 'end;',
  32010. 'begin',
  32011. '{$R+}',
  32012. '']);
  32013. ConvertProgram;
  32014. CheckSource('TestRangeChecks_AssignEnum',
  32015. LinesToStr([ // statements
  32016. 'this.TEnum = {',
  32017. ' "0": "red",',
  32018. ' red: 0,',
  32019. ' "1": "green",',
  32020. ' green: 1',
  32021. '};',
  32022. 'this.e = this.TEnum.red;',
  32023. 'this.DoIt = function (p) {',
  32024. ' rtl.rc(p, 0, 1);',
  32025. ' $mod.e = rtl.rc(p, 0, 1);',
  32026. ' p = 0;',
  32027. ' p = rtl.rc($mod.e + 1, 0, 1);',
  32028. '};',
  32029. 'this.DoSome = function () {',
  32030. ' $mod.DoIt($mod.e);',
  32031. ' $mod.e = 1;',
  32032. ' $mod.e = $mod.e - 1;',
  32033. '};',
  32034. '']),
  32035. LinesToStr([ // $mod.$main
  32036. '']));
  32037. end;
  32038. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  32039. begin
  32040. StartProgram(false);
  32041. Add([
  32042. '{$R+}',
  32043. 'type',
  32044. ' TEnum = (red,green);',
  32045. ' TEnumRg = red..green;',
  32046. 'var',
  32047. ' e: TEnumRg = red;',
  32048. 'procedure DoIt(p: TEnumRg);',
  32049. 'begin',
  32050. ' e:=p;',
  32051. ' p:=TEnumRg(0);',
  32052. ' p:=succ(e);',
  32053. 'end;',
  32054. '{$R-}',
  32055. 'procedure DoSome;',
  32056. 'begin',
  32057. ' DoIt(e);',
  32058. ' e:=TEnum(1);',
  32059. ' e:=pred(e);',
  32060. 'end;',
  32061. 'begin',
  32062. '{$R+}',
  32063. '']);
  32064. ConvertProgram;
  32065. CheckSource('TestRangeChecks_AssignEnumRange',
  32066. LinesToStr([ // statements
  32067. 'this.TEnum = {',
  32068. ' "0": "red",',
  32069. ' red: 0,',
  32070. ' "1": "green",',
  32071. ' green: 1',
  32072. '};',
  32073. 'this.e = this.TEnum.red;',
  32074. 'this.DoIt = function (p) {',
  32075. ' rtl.rc(p, 0, 1);',
  32076. ' $mod.e = rtl.rc(p, 0, 1);',
  32077. ' p = 0;',
  32078. ' p = rtl.rc($mod.e + 1, 0, 1);',
  32079. '};',
  32080. 'this.DoSome = function () {',
  32081. ' $mod.DoIt($mod.e);',
  32082. ' $mod.e = 1;',
  32083. ' $mod.e = $mod.e - 1;',
  32084. '};',
  32085. '']),
  32086. LinesToStr([ // $mod.$main
  32087. '']));
  32088. end;
  32089. procedure TTestModule.TestRangeChecks_AssignChar;
  32090. begin
  32091. StartProgram(false);
  32092. Add([
  32093. '{$R+}',
  32094. 'type',
  32095. ' TLetter = char;',
  32096. 'var',
  32097. ' b: TLetter = ''2'';',
  32098. ' w: TLetter = ''3'';',
  32099. 'procedure DoIt(p: TLetter);',
  32100. 'begin',
  32101. ' b:=w;',
  32102. ' b:=''1'';',
  32103. 'end;',
  32104. '{$R-}',
  32105. 'procedure DoSome;',
  32106. 'begin',
  32107. ' DoIt(w);',
  32108. ' b:=w;',
  32109. ' b:=''2'';',
  32110. 'end;',
  32111. 'begin',
  32112. '{$R+}',
  32113. '']);
  32114. ConvertProgram;
  32115. CheckSource('TestRangeChecks_AssignChar',
  32116. LinesToStr([ // statements
  32117. 'this.b = "2";',
  32118. 'this.w = "3";',
  32119. 'this.DoIt = function (p) {',
  32120. ' rtl.rcc(p, 0, 65535);',
  32121. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  32122. ' $mod.b = "1";',
  32123. '};',
  32124. 'this.DoSome = function () {',
  32125. ' $mod.DoIt($mod.w);',
  32126. ' $mod.b = $mod.w;',
  32127. ' $mod.b = "2";',
  32128. '};',
  32129. '']),
  32130. LinesToStr([ // $mod.$main
  32131. '']));
  32132. end;
  32133. procedure TTestModule.TestRangeChecks_AssignCharRange;
  32134. begin
  32135. StartProgram(false);
  32136. Add([
  32137. '{$R+}',
  32138. 'type TDigit = ''0''..''9'';',
  32139. 'var',
  32140. ' b: TDigit = ''2'';',
  32141. ' w: TDigit = ''3'';',
  32142. 'procedure DoIt(p: TDigit);',
  32143. 'begin',
  32144. ' b:=w;',
  32145. ' b:=''1'';',
  32146. 'end;',
  32147. '{$R-}',
  32148. 'procedure DoSome;',
  32149. 'begin',
  32150. ' DoIt(w);',
  32151. ' b:=w;',
  32152. ' b:=''2'';',
  32153. 'end;',
  32154. 'begin',
  32155. '{$R+}',
  32156. '']);
  32157. ConvertProgram;
  32158. CheckSource('TestRangeChecks_AssignCharRange',
  32159. LinesToStr([ // statements
  32160. 'this.b = "2";',
  32161. 'this.w = "3";',
  32162. 'this.DoIt = function (p) {',
  32163. ' rtl.rcc(p, 48, 57);',
  32164. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  32165. ' $mod.b = "1";',
  32166. '};',
  32167. 'this.DoSome = function () {',
  32168. ' $mod.DoIt($mod.w);',
  32169. ' $mod.b = $mod.w;',
  32170. ' $mod.b = "2";',
  32171. '};',
  32172. '']),
  32173. LinesToStr([ // $mod.$main
  32174. '']));
  32175. end;
  32176. procedure TTestModule.TestRangeChecks_ArrayIndex;
  32177. begin
  32178. StartProgram(false);
  32179. Add([
  32180. '{$R+}',
  32181. 'type',
  32182. ' Ten = 1..10;',
  32183. ' TArr = array of Ten;',
  32184. ' TArrArr = array of TArr;',
  32185. ' TArrByte = array[byte] of Ten;',
  32186. ' TArrChar = array[''0''..''9''] of Ten;',
  32187. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  32188. ' TObject = class',
  32189. ' A: TArr;',
  32190. ' end;',
  32191. 'procedure DoIt;',
  32192. 'var',
  32193. ' Arr: TArr;',
  32194. ' ArrArr: TArrArr;',
  32195. ' ArrByte: TArrByte;',
  32196. ' ArrChar: TArrChar;',
  32197. ' ArrByteChar: TArrByteChar;',
  32198. ' i: Ten;',
  32199. ' c: char;',
  32200. ' o: tobject;',
  32201. 'begin',
  32202. ' i:=Arr[1];',
  32203. ' i:=ArrByteChar[1,''2''];',
  32204. ' Arr[1]:=Arr[1];',
  32205. ' Arr[i]:=Arr[i];',
  32206. ' ArrByte[3]:=ArrByte[3];',
  32207. ' ArrByte[i]:=ArrByte[i];',
  32208. ' ArrChar[''5'']:=ArrChar[''5''];',
  32209. ' ArrChar[c]:=ArrChar[c];',
  32210. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  32211. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  32212. ' o.a[i]:=o.a[i];',
  32213. 'end;',
  32214. 'begin',
  32215. '']);
  32216. ConvertProgram;
  32217. CheckSource('TestRangeChecks_ArrayIndex',
  32218. LinesToStr([ // statements
  32219. 'this.TArrByteChar$clone = function (a) {',
  32220. ' var b = [];',
  32221. ' b.length = 256;',
  32222. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  32223. ' return b;',
  32224. '};',
  32225. 'rtl.createClass(this, "TObject", null, function () {',
  32226. ' this.$init = function () {',
  32227. ' this.A = [];',
  32228. ' };',
  32229. ' this.$final = function () {',
  32230. ' this.A = undefined;',
  32231. ' };',
  32232. '});',
  32233. 'this.DoIt = function () {',
  32234. ' var Arr = [];',
  32235. ' var ArrArr = [];',
  32236. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  32237. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  32238. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  32239. ' var i = 0;',
  32240. ' var c = "";',
  32241. ' var o = null;',
  32242. ' i = rtl.rc(Arr[1], 1, 10);',
  32243. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  32244. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  32245. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  32246. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  32247. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  32248. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  32249. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  32250. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  32251. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  32252. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  32253. '};',
  32254. '']),
  32255. LinesToStr([ // $mod.$main
  32256. '']));
  32257. end;
  32258. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  32259. begin
  32260. StartProgram(false);
  32261. Add([
  32262. '{$R+}',
  32263. 'type',
  32264. ' Ten = 1..10;',
  32265. ' TRec = record x: Ten end;',
  32266. ' TArr = array of TRec;',
  32267. ' TArrArr = array of TArr;',
  32268. ' TObject = class',
  32269. ' A: TArr;',
  32270. ' end;',
  32271. 'procedure DoIt;',
  32272. 'var',
  32273. ' Arr: TArr;',
  32274. ' ArrArr: TArrArr;',
  32275. ' i: Ten;',
  32276. ' o: tobject;',
  32277. 'begin',
  32278. ' Arr[1]:=Arr[1];',
  32279. ' Arr[i]:=Arr[i+1];',
  32280. ' o.a[i]:=o.a[i+2];',
  32281. 'end;',
  32282. 'begin',
  32283. '']);
  32284. ConvertProgram;
  32285. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  32286. LinesToStr([ // statements
  32287. 'rtl.recNewT(this, "TRec", function () {',
  32288. ' this.x = 0;',
  32289. ' this.$eq = function (b) {',
  32290. ' return this.x === b.x;',
  32291. ' };',
  32292. ' this.$assign = function (s) {',
  32293. ' this.x = s.x;',
  32294. ' return this;',
  32295. ' };',
  32296. '});',
  32297. 'rtl.createClass(this, "TObject", null, function () {',
  32298. ' this.$init = function () {',
  32299. ' this.A = [];',
  32300. ' };',
  32301. ' this.$final = function () {',
  32302. ' this.A = undefined;',
  32303. ' };',
  32304. '});',
  32305. 'this.DoIt = function () {',
  32306. ' var Arr = [];',
  32307. ' var ArrArr = [];',
  32308. ' var i = 0;',
  32309. ' var o = null;',
  32310. ' Arr[1].$assign(Arr[1]);',
  32311. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  32312. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  32313. '};',
  32314. '']),
  32315. LinesToStr([ // $mod.$main
  32316. '']));
  32317. end;
  32318. procedure TTestModule.TestRangeChecks_StringIndex;
  32319. begin
  32320. StartProgram(false);
  32321. Add([
  32322. 'type',
  32323. ' TObject = class',
  32324. ' S: string;',
  32325. ' end;',
  32326. '{$R+}',
  32327. 'procedure DoIt(var h: string);',
  32328. 'var',
  32329. ' s: string;',
  32330. ' i: longint;',
  32331. ' c: char;',
  32332. ' o: tobject;',
  32333. 'begin',
  32334. ' c:=s[1];',
  32335. ' s[i]:=s[i];',
  32336. ' h[i]:=h[i];',
  32337. ' c:=o.s[i];',
  32338. ' o.s[i]:=c;',
  32339. 'end;',
  32340. 'begin',
  32341. '']);
  32342. ConvertProgram;
  32343. CheckSource('TestRangeChecks_StringIndex',
  32344. LinesToStr([ // statements
  32345. 'rtl.createClass(this, "TObject", null, function () {',
  32346. ' this.$init = function () {',
  32347. ' this.S = "";',
  32348. ' };',
  32349. ' this.$final = function () {',
  32350. ' };',
  32351. '});',
  32352. 'this.DoIt = function (h) {',
  32353. ' var s = "";',
  32354. ' var i = 0;',
  32355. ' var c = "";',
  32356. ' var o = null;',
  32357. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  32358. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  32359. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  32360. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  32361. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  32362. '};',
  32363. '']),
  32364. LinesToStr([ // $mod.$main
  32365. '']));
  32366. end;
  32367. procedure TTestModule.TestRangeChecks_TypecastInt;
  32368. begin
  32369. StartProgram(false);
  32370. Add([
  32371. '{$R+}',
  32372. 'var',
  32373. ' i: nativeint;',
  32374. ' b: byte;',
  32375. ' sh: shortint;',
  32376. ' w: word;',
  32377. ' sm: smallint;',
  32378. ' lw: longword;',
  32379. ' li: longint;',
  32380. 'begin',
  32381. ' b:=12+byte(i);',
  32382. ' sh:=12+shortint(i);',
  32383. ' w:=12+word(i);',
  32384. ' sm:=12+smallint(i);',
  32385. ' lw:=12+longword(i);',
  32386. ' li:=12+longint(i);',
  32387. '']);
  32388. ConvertProgram;
  32389. CheckSource('TestRangeChecks_TypecastInt',
  32390. LinesToStr([
  32391. 'this.i = 0;',
  32392. 'this.b = 0;',
  32393. 'this.sh = 0;',
  32394. 'this.w = 0;',
  32395. 'this.sm = 0;',
  32396. 'this.lw = 0;',
  32397. 'this.li = 0;',
  32398. '']),
  32399. LinesToStr([
  32400. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  32401. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  32402. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  32403. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  32404. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  32405. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  32406. '']));
  32407. end;
  32408. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  32409. begin
  32410. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32411. StartProgram(false);
  32412. Add([
  32413. '{$modeswitch typehelpers}',
  32414. '{$R+}',
  32415. 'type',
  32416. ' TObject = class',
  32417. ' FSize: byte;',
  32418. ' property Size: byte read FSize;',
  32419. ' end;',
  32420. ' THelper = type helper for byte',
  32421. ' procedure SetIt(w: word);',
  32422. ' end;',
  32423. 'procedure THelper.SetIt(w: word);',
  32424. 'begin',
  32425. ' Self:=w;',
  32426. 'end;',
  32427. 'function GetIt: byte;',
  32428. 'begin',
  32429. ' Result.SetIt(2);',
  32430. 'end;',
  32431. 'var',
  32432. ' b: byte = 3;',
  32433. ' o: TObject;',
  32434. 'begin',
  32435. ' b.SetIt(14);',
  32436. ' with b do SetIt(15);',
  32437. ' o.Size.SetIt(16);',
  32438. '']);
  32439. ConvertProgram;
  32440. CheckSource('TestRangeChecks_AssignInt',
  32441. LinesToStr([ // statements
  32442. 'rtl.createClass(this, "TObject", null, function () {',
  32443. ' this.$init = function () {',
  32444. ' this.FSize = 0;',
  32445. ' };',
  32446. ' this.$final = function () {',
  32447. ' };',
  32448. '});',
  32449. 'rtl.createHelper(this, "THelper", null, function () {',
  32450. ' this.SetIt = function (w) {',
  32451. ' rtl.rc(w, 0, 65535);',
  32452. ' this.set(w);',
  32453. ' };',
  32454. '});',
  32455. 'this.GetIt = function () {',
  32456. ' var Result = 0;',
  32457. ' $mod.THelper.SetIt.call({',
  32458. ' get: function () {',
  32459. ' return Result;',
  32460. ' },',
  32461. ' set: function (v) {',
  32462. ' rtl.rc(v, 0, 255);',
  32463. ' Result = v;',
  32464. ' }',
  32465. ' }, 2);',
  32466. ' return Result;',
  32467. '};',
  32468. 'this.b = 3;',
  32469. 'this.o = null;',
  32470. '']),
  32471. LinesToStr([ // $mod.$main
  32472. '$mod.THelper.SetIt.call({',
  32473. ' p: $mod,',
  32474. ' get: function () {',
  32475. ' return this.p.b;',
  32476. ' },',
  32477. ' set: function (v) {',
  32478. ' rtl.rc(v, 0, 255);',
  32479. ' this.p.b = v;',
  32480. ' }',
  32481. '}, 14);',
  32482. 'var $with = $mod.b;',
  32483. '$mod.THelper.SetIt.call({',
  32484. ' get: function () {',
  32485. ' return $with;',
  32486. ' },',
  32487. ' set: function (v) {',
  32488. ' rtl.rc(v, 0, 255);',
  32489. ' $with = v;',
  32490. ' }',
  32491. '}, 15);',
  32492. '$mod.THelper.SetIt.call({',
  32493. ' p: $mod.o,',
  32494. ' get: function () {',
  32495. ' return this.p.FSize;',
  32496. ' },',
  32497. ' set: function (v) {',
  32498. ' rtl.rc(v, 0, 255);',
  32499. ' this.p.FSize = v;',
  32500. ' }',
  32501. '}, 16);',
  32502. '']));
  32503. end;
  32504. procedure TTestModule.TestAsync_Proc;
  32505. begin
  32506. StartProgram(false);
  32507. Add([
  32508. 'procedure Fly(w: word = 1); async; forward;',
  32509. 'procedure Run(w: word = 2); async;',
  32510. 'begin',
  32511. ' Fly(w);',
  32512. ' Fly;',
  32513. ' await(Fly(w));',
  32514. ' await(Fly);',
  32515. 'end;',
  32516. 'procedure Fly(w: word); ',
  32517. 'begin',
  32518. 'end;',
  32519. 'begin',
  32520. ' Run;',
  32521. ' Run(3);',
  32522. '']);
  32523. CheckResolverUnexpectedHints();
  32524. ConvertProgram;
  32525. CheckSource('TestAsync_Proc',
  32526. LinesToStr([ // statements
  32527. 'this.Run = async function (w) {',
  32528. ' $mod.Fly(w);',
  32529. ' $mod.Fly(1);',
  32530. ' await $mod.Fly(w);',
  32531. ' await $mod.Fly(1);',
  32532. '};',
  32533. 'this.Fly = async function (w) {',
  32534. '};',
  32535. '']),
  32536. LinesToStr([
  32537. '$mod.Run(2);',
  32538. '$mod.Run(3);',
  32539. '']));
  32540. end;
  32541. procedure TTestModule.TestAsync_CallResultIsPromise;
  32542. begin
  32543. StartProgram(false);
  32544. Add([
  32545. '{$modeswitch externalclass}',
  32546. 'type',
  32547. ' TObject = class',
  32548. ' end;',
  32549. ' TJSPromise = class external name ''Promise''',
  32550. ' end;',
  32551. ' TBird = class',
  32552. ' function Fly: word; async; ',
  32553. ' end;',
  32554. 'function TBird.Fly: word; async; ',
  32555. 'begin',
  32556. ' Result:=3;',
  32557. ' Fly:=4+Result;',
  32558. ' if Result=5 then ;',
  32559. ' exit(6);',
  32560. 'end;',
  32561. 'function Run: word; async;',
  32562. 'begin',
  32563. ' Result:=11+Result;',
  32564. ' inc(Result);',
  32565. 'end;',
  32566. 'var',
  32567. ' p: TJSPromise;',
  32568. ' o: TBird;',
  32569. 'begin',
  32570. ' p:=Run;',
  32571. ' p:=Run();',
  32572. ' if Run=p then ;',
  32573. ' if p=Run then ;',
  32574. ' if Run()=p then ;',
  32575. ' if p=Run() then ;',
  32576. ' p:=o.Fly;',
  32577. ' p:=o.Fly();',
  32578. ' if o.Fly=p then ;',
  32579. ' if o.Fly()=p then ;',
  32580. ' with o do begin',
  32581. ' p:=Fly;',
  32582. ' p:=Fly();',
  32583. ' if Fly=p then ;',
  32584. ' if Fly()=p then ;',
  32585. ' end;',
  32586. '']);
  32587. CheckResolverUnexpectedHints();
  32588. ConvertProgram;
  32589. CheckSource('TestAsync_CallResultIsPromise',
  32590. LinesToStr([ // statements
  32591. 'rtl.createClass(this, "TObject", null, function () {',
  32592. ' this.$init = function () {',
  32593. ' };',
  32594. ' this.$final = function () {',
  32595. ' };',
  32596. '});',
  32597. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32598. ' this.Fly = async function () {',
  32599. ' var Result = 0;',
  32600. ' Result = 3;',
  32601. ' Result = 4 + Result;',
  32602. ' if (Result === 5) ;',
  32603. ' return 6;',
  32604. ' return Result;',
  32605. ' };',
  32606. '});',
  32607. 'this.Run = async function () {',
  32608. ' var Result = 0;',
  32609. ' Result = 11 + Result;',
  32610. ' Result += 1;',
  32611. ' return Result;',
  32612. '};',
  32613. 'this.p = null;',
  32614. 'this.o = null;',
  32615. '']),
  32616. LinesToStr([
  32617. '$mod.p = $mod.Run();',
  32618. '$mod.p = $mod.Run();',
  32619. 'if ($mod.Run() === $mod.p) ;',
  32620. 'if ($mod.p === $mod.Run()) ;',
  32621. 'if ($mod.Run() === $mod.p) ;',
  32622. 'if ($mod.p === $mod.Run()) ;',
  32623. '$mod.p = $mod.o.Fly();',
  32624. '$mod.p = $mod.o.Fly();',
  32625. 'if ($mod.o.Fly() === $mod.p) ;',
  32626. 'if ($mod.o.Fly() === $mod.p) ;',
  32627. 'var $with = $mod.o;',
  32628. '$mod.p = $with.Fly();',
  32629. '$mod.p = $with.Fly();',
  32630. 'if ($with.Fly() === $mod.p) ;',
  32631. 'if ($with.Fly() === $mod.p) ;',
  32632. '']));
  32633. end;
  32634. procedure TTestModule.TestAsync_ConstructorFail;
  32635. begin
  32636. StartProgram(false);
  32637. Add([
  32638. 'type',
  32639. ' TObject = class',
  32640. ' end;',
  32641. ' TBird = class',
  32642. ' constructor Create; async;',
  32643. ' end;',
  32644. 'constructor TBird.Create; async;',
  32645. 'begin',
  32646. 'end;',
  32647. 'begin',
  32648. '']);
  32649. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32650. ConvertProgram;
  32651. end;
  32652. procedure TTestModule.TestAsync_PropertyGetterFail;
  32653. begin
  32654. StartProgram(false);
  32655. Add([
  32656. 'type',
  32657. ' TObject = class',
  32658. ' end;',
  32659. ' TBird = class',
  32660. ' function GetSize: word; async;',
  32661. ' property Size: word read GetSize;',
  32662. ' end;',
  32663. 'function TBird.GetSize: word; async;',
  32664. 'begin',
  32665. 'end;',
  32666. 'begin',
  32667. '']);
  32668. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32669. ConvertProgram;
  32670. end;
  32671. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32672. begin
  32673. StartProgram(false);
  32674. Add([
  32675. 'procedure Run; async;',
  32676. 'begin',
  32677. ' await(word,1);',
  32678. 'end;',
  32679. 'begin',
  32680. '']);
  32681. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32682. ConvertProgram;
  32683. end;
  32684. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32685. begin
  32686. StartProgram(false);
  32687. Add([
  32688. 'type',
  32689. ' TObject = class',
  32690. ' end;',
  32691. ' TBird = class',
  32692. ' end;',
  32693. 'function Fly: TObject; async;',
  32694. 'begin',
  32695. 'end;',
  32696. 'procedure Run; async;',
  32697. 'begin',
  32698. ' await(TBird,Fly);',
  32699. 'end;',
  32700. 'begin',
  32701. '']);
  32702. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32703. ConvertProgram;
  32704. end;
  32705. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32706. begin
  32707. StartProgram(false);
  32708. Add([
  32709. 'procedure Crawl(w: double); ',
  32710. 'begin',
  32711. 'end;',
  32712. 'procedure Run(w: double);',
  32713. 'begin',
  32714. ' await(Crawl(w));',
  32715. 'end;',
  32716. 'begin',
  32717. ' Run(1);']);
  32718. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32719. ConvertProgram;
  32720. end;
  32721. procedure TTestModule.TestAWait_IntegerFail;
  32722. begin
  32723. StartProgram(false);
  32724. Add([
  32725. 'function Run: word;',
  32726. 'begin',
  32727. 'end;',
  32728. 'procedure Fly(w: word); async;',
  32729. 'begin',
  32730. ' await(Run());',
  32731. 'end;',
  32732. 'begin',
  32733. ' Fly(1);']);
  32734. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32735. ConvertProgram;
  32736. end;
  32737. procedure TTestModule.TestAWait_ExternalClassPromise;
  32738. begin
  32739. StartProgram(false);
  32740. Add([
  32741. '{$modeswitch externalclass}',
  32742. 'type',
  32743. ' TJSPromise = class external name ''Promise''',
  32744. ' end;',
  32745. ' TJSThenable = class external name ''Thenable''',
  32746. ' end;',
  32747. 'function Fly(w: word): TJSPromise;',
  32748. 'begin',
  32749. 'end;',
  32750. 'function Jump(w: word): word; async;',
  32751. 'begin',
  32752. 'end;',
  32753. 'function Eat(w: word): TJSPromise; async;',
  32754. 'begin',
  32755. 'end;',
  32756. 'function Run(d: double): word; async;',
  32757. 'var',
  32758. ' p: TJSPromise;',
  32759. 'begin',
  32760. ' Result:=await(word,p);', // promise needs type
  32761. ' Result:=await(word,Fly(3));', // promise needs type
  32762. ' Result:=await(Jump(4));', // async non promise must omit the type
  32763. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32764. ' Result:=await(word,Eat(6));', // promise needs type
  32765. 'end;',
  32766. 'begin',
  32767. '']);
  32768. ConvertProgram;
  32769. CheckSource('TestAWait_ExternalClassPromise',
  32770. LinesToStr([ // statements
  32771. 'this.Fly = function (w) {',
  32772. ' var Result = null;',
  32773. ' return Result;',
  32774. '};',
  32775. 'this.Jump = async function (w) {',
  32776. ' var Result = 0;',
  32777. ' return Result;',
  32778. '};',
  32779. 'this.Eat = async function (w) {',
  32780. ' var Result = null;',
  32781. ' return Result;',
  32782. '};',
  32783. 'this.Run = async function (d) {',
  32784. ' var Result = 0;',
  32785. ' var p = null;',
  32786. ' Result = await p;',
  32787. ' Result = await $mod.Fly(3);',
  32788. ' Result = await $mod.Jump(4);',
  32789. ' Result = await $mod.Jump(5);',
  32790. ' Result = await $mod.Eat(6);',
  32791. ' return Result;',
  32792. '};',
  32793. '']),
  32794. LinesToStr([
  32795. ]));
  32796. CheckResolverUnexpectedHints();
  32797. end;
  32798. procedure TTestModule.TestAWait_JSValue;
  32799. begin
  32800. StartProgram(false);
  32801. Add([
  32802. '{$modeswitch externalclass}',
  32803. 'type',
  32804. ' TJSPromise = class external name ''Promise''',
  32805. ' end;',
  32806. 'function Fly(w: word): jsvalue; async;',
  32807. 'begin',
  32808. 'end;',
  32809. 'function Run(d: jsvalue; var e): word; async;',
  32810. 'begin',
  32811. ' Result:=await(word,d);', // promise needs type
  32812. ' d:=await(Fly(4));', // async non promise must omit the type
  32813. ' Result:=await(word,e);', // promise needs type
  32814. 'end;',
  32815. 'begin',
  32816. '']);
  32817. ConvertProgram;
  32818. CheckSource('TestAWait_JSValue',
  32819. LinesToStr([ // statements
  32820. 'this.Fly = async function (w) {',
  32821. ' var Result = undefined;',
  32822. ' return Result;',
  32823. '};',
  32824. 'this.Run = async function (d, e) {',
  32825. ' var Result = 0;',
  32826. ' Result = await d;',
  32827. ' d = await $mod.Fly(4);',
  32828. ' Result = await e.get();',
  32829. ' return Result;',
  32830. '};',
  32831. '']),
  32832. LinesToStr([
  32833. ]));
  32834. CheckResolverUnexpectedHints();
  32835. end;
  32836. procedure TTestModule.TestAWait_Result;
  32837. begin
  32838. StartProgram(false);
  32839. Add([
  32840. '{$modeswitch externalclass}',
  32841. 'type',
  32842. ' TJSPromise = class external name ''Promise''',
  32843. ' end;',
  32844. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32845. 'begin',
  32846. 'end;',
  32847. 'function Run(d: double = 1.6): word; async;',
  32848. 'begin',
  32849. ' Result:=await(word,Crawl);',
  32850. ' Result:=await(word,Crawl(4.5));',
  32851. ' Result:=await(Run);',
  32852. ' Result:=await(Run(6.7));',
  32853. 'end;',
  32854. 'begin',
  32855. ' Run(1);']);
  32856. ConvertProgram;
  32857. CheckSource('TestAWait_Result',
  32858. LinesToStr([ // statements
  32859. 'this.Crawl = function (d) {',
  32860. ' var Result = null;',
  32861. ' return Result;',
  32862. '};',
  32863. 'this.Run = async function (d) {',
  32864. ' var Result = 0;',
  32865. ' Result = await $mod.Crawl(1.3);',
  32866. ' Result = await $mod.Crawl(4.5);',
  32867. ' Result = await $mod.Run(1.6);',
  32868. ' Result = await $mod.Run(6.7);',
  32869. ' return Result;',
  32870. '};',
  32871. '']),
  32872. LinesToStr([
  32873. '$mod.Run(1);'
  32874. ]));
  32875. CheckResolverUnexpectedHints();
  32876. end;
  32877. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32878. begin
  32879. StartProgram(false);
  32880. Add([
  32881. '{$mode objfpc}',
  32882. '{$modeswitch externalclass}',
  32883. 'type',
  32884. ' TJSPromise = class external name ''Promise''',
  32885. ' end;',
  32886. 'function Run: TJSPromise; async;',
  32887. 'begin',
  32888. 'end;',
  32889. 'procedure Fly(w: word); async;',
  32890. 'begin',
  32891. ' await(Run());',
  32892. 'end;',
  32893. 'begin',
  32894. ' Fly(1);']);
  32895. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32896. nWrongNumberOfParametersForCallTo);
  32897. ConvertProgram;
  32898. end;
  32899. procedure TTestModule.TestAsync_AnonymousProc;
  32900. begin
  32901. StartProgram(false);
  32902. Add([
  32903. '{$mode objfpc}',
  32904. '{$modeswitch externalclass}',
  32905. 'type',
  32906. ' TJSPromise = class external name ''Promise''',
  32907. ' end;',
  32908. 'type',
  32909. ' TFunc = reference to function(x: double): word; async;',
  32910. 'function Crawl(d: double = 1.3): word; async;',
  32911. 'begin',
  32912. 'end;',
  32913. 'var Func: TFunc;',
  32914. 'begin',
  32915. ' Func:=function(c:double):word async begin',
  32916. ' Result:=await(Crawl(c));',
  32917. ' end;',
  32918. ' Func:=function(c:double):word async assembler asm',
  32919. ' end;',
  32920. ' ']);
  32921. ConvertProgram;
  32922. CheckSource('TestAsync_AnonymousProc',
  32923. LinesToStr([ // statements
  32924. 'this.Crawl = async function (d) {',
  32925. ' var Result = 0;',
  32926. ' return Result;',
  32927. '};',
  32928. 'this.Func = null;',
  32929. '']),
  32930. LinesToStr([
  32931. '$mod.Func = async function (c) {',
  32932. ' var Result = 0;',
  32933. ' Result = await $mod.Crawl(c);',
  32934. ' return Result;',
  32935. '};',
  32936. '$mod.Func = async function (c) {',
  32937. '};',
  32938. '']));
  32939. CheckResolverUnexpectedHints();
  32940. end;
  32941. procedure TTestModule.TestAsync_ProcType;
  32942. begin
  32943. StartProgram(false);
  32944. Add([
  32945. '{$mode objfpc}',
  32946. 'type',
  32947. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32948. ' TFunc = function(x: double = 1.1): word; async;',
  32949. ' TProc = procedure(x: longint = 7); async;',
  32950. 'function Crawl(d: double): word; async;',
  32951. 'begin',
  32952. 'end;',
  32953. 'procedure Run(e:longint); async;',
  32954. 'begin',
  32955. 'end;',
  32956. 'procedure Fly(p: TProc); async;',
  32957. 'begin',
  32958. ' await(p);',
  32959. ' await(p());',
  32960. 'end;',
  32961. 'var',
  32962. ' RefFunc: TRefFunc;',
  32963. ' Func: TFunc;',
  32964. ' Proc, ProcB: TProc;',
  32965. 'begin',
  32966. ' Func:=@Crawl;',
  32967. ' RefFunc:=@Crawl;',
  32968. ' RefFunc:=function(c:double):word async begin',
  32969. ' Result:=await(RefFunc);',
  32970. ' Result:=await(RefFunc());',
  32971. ' Result:=await(Func);',
  32972. ' Result:=await(Func());',
  32973. ' await(Proc);',
  32974. ' await(Proc());',
  32975. ' await(Proc(13));',
  32976. ' end;',
  32977. ' Proc:=@Run;',
  32978. ' if Proc=ProcB then ;',
  32979. ' ']);
  32980. ConvertProgram;
  32981. CheckResolverUnexpectedHints();
  32982. CheckSource('TestAsync_ProcType',
  32983. LinesToStr([ // statements
  32984. 'this.Crawl = async function (d) {',
  32985. ' var Result = 0;',
  32986. ' return Result;',
  32987. '};',
  32988. 'this.Run = async function (e) {',
  32989. '};',
  32990. 'this.Fly = async function (p) {',
  32991. ' await p(7);',
  32992. ' await p(7);',
  32993. '};',
  32994. 'this.RefFunc = null;',
  32995. 'this.Func = null;',
  32996. 'this.Proc = null;',
  32997. 'this.ProcB = null;',
  32998. '']),
  32999. LinesToStr([
  33000. '$mod.Func = $mod.Crawl;',
  33001. '$mod.RefFunc = $mod.Crawl;',
  33002. '$mod.RefFunc = async function (c) {',
  33003. ' var Result = 0;',
  33004. ' Result = await $mod.RefFunc(1.3);',
  33005. ' Result = await $mod.RefFunc(1.3);',
  33006. ' Result = await $mod.Func(1.1);',
  33007. ' Result = await $mod.Func(1.1);',
  33008. ' await $mod.Proc(7);',
  33009. ' await $mod.Proc(7);',
  33010. ' await $mod.Proc(13);',
  33011. ' return Result;',
  33012. '};',
  33013. '$mod.Proc = $mod.Run;',
  33014. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  33015. '']));
  33016. end;
  33017. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  33018. begin
  33019. StartProgram(false);
  33020. Add([
  33021. '{$mode objfpc}',
  33022. 'type',
  33023. ' TRefFunc = reference to function(x: double = 1.3): word;',
  33024. 'function Crawl(d: double): word; async;',
  33025. 'begin',
  33026. 'end;',
  33027. 'var',
  33028. ' RefFunc: TRefFunc;',
  33029. 'begin',
  33030. ' RefFunc:=@Crawl;',
  33031. ' ']);
  33032. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  33033. ConvertProgram;
  33034. end;
  33035. procedure TTestModule.TestAsync_Inherited;
  33036. begin
  33037. StartProgram(false);
  33038. Add([
  33039. '{$mode objfpc}',
  33040. '{$modeswitch externalclass}',
  33041. 'type',
  33042. ' TJSPromise = class external name ''Promise''',
  33043. ' end;',
  33044. ' TObject = class',
  33045. ' function Run(w: word = 3): word; async; virtual;',
  33046. ' end;',
  33047. ' TBird = class',
  33048. ' function Run(w: word = 3): word; async; override;',
  33049. ' end;',
  33050. 'function TObject.Run(w: word = 3): word; async;',
  33051. 'begin',
  33052. 'end;',
  33053. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  33054. 'var p: TJSPromise;',
  33055. 'begin',
  33056. ' p:=inherited;',
  33057. ' p:=inherited Run;',
  33058. ' p:=inherited Run();',
  33059. ' p:=inherited Run(4);',
  33060. ' exit(p);',
  33061. ' exit(inherited);',
  33062. ' exit(inherited Run);',
  33063. ' exit(inherited Run(5));',
  33064. ' exit(6);',
  33065. 'end;',
  33066. 'begin',
  33067. ' ']);
  33068. ConvertProgram;
  33069. CheckSource('TestAsync_Inherited',
  33070. LinesToStr([ // statements
  33071. 'rtl.createClass(this, "TObject", null, function () {',
  33072. ' this.$init = function () {',
  33073. ' };',
  33074. ' this.$final = function () {',
  33075. ' };',
  33076. ' this.Run = async function (w) {',
  33077. ' var Result = 0;',
  33078. ' return Result;',
  33079. ' };',
  33080. '});',
  33081. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33082. ' this.Run = async function (w) {',
  33083. ' var Result = 0;',
  33084. ' var p = null;',
  33085. ' p = $mod.TObject.Run.apply(this, arguments);',
  33086. ' p = $mod.TObject.Run.call(this, 3);',
  33087. ' p = $mod.TObject.Run.call(this, 3);',
  33088. ' p = $mod.TObject.Run.call(this, 4);',
  33089. ' return p;',
  33090. ' return $mod.TObject.Run.apply(this, arguments);',
  33091. ' return $mod.TObject.Run.call(this, 3);',
  33092. ' return $mod.TObject.Run.call(this, 5);',
  33093. ' return 6;',
  33094. ' return Result;',
  33095. ' };',
  33096. '});',
  33097. '']),
  33098. LinesToStr([
  33099. '']));
  33100. CheckResolverUnexpectedHints();
  33101. end;
  33102. procedure TTestModule.TestAsync_ClassInterface;
  33103. begin
  33104. StartProgram(false);
  33105. Add([
  33106. '{$mode objfpc}',
  33107. '{$modeswitch externalclass}',
  33108. 'type',
  33109. ' TJSPromise = class external name ''Promise''',
  33110. ' end;',
  33111. ' IUnknown = interface',
  33112. ' function _AddRef: longint;',
  33113. ' function _Release: longint;',
  33114. ' end;',
  33115. 'function Say(i: IUnknown): IUnknown; async;',
  33116. 'begin',
  33117. 'end;',
  33118. 'function Run: IUnknown; async;',
  33119. 'begin',
  33120. ' Result:=await(Run);',
  33121. ' Result:=await(Run());',
  33122. ' Result:=await(Run) as IUnknown;',
  33123. ' Result:=await(Say(nil));',
  33124. ' Result:=await(Say(await(Run())));',
  33125. ' Result:=await(Say(await(Run()) as IUnknown));',
  33126. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  33127. 'end;',
  33128. 'procedure Fly;',
  33129. 'var p: TJSPromise;',
  33130. 'begin',
  33131. ' Run;',
  33132. ' Run();',
  33133. ' p:=Run;',
  33134. ' p:=Run();',
  33135. 'end;',
  33136. 'begin',
  33137. ' ']);
  33138. ConvertProgram;
  33139. CheckSource('TestAsync_ClassInterface',
  33140. LinesToStr([ // statements
  33141. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  33142. 'this.Say = async function (i) {',
  33143. ' var Result = null;',
  33144. ' return Result;',
  33145. '};',
  33146. 'this.Run = async function () {',
  33147. ' var Result = null;',
  33148. ' var $ok = false;',
  33149. ' try {',
  33150. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  33151. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  33152. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  33153. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  33154. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  33155. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  33156. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  33157. ' $ok = true;',
  33158. ' } finally {',
  33159. ' if (!$ok) rtl._Release(Result);',
  33160. ' };',
  33161. ' return Result;',
  33162. '};',
  33163. 'this.Fly = function () {',
  33164. ' var p = null;',
  33165. ' $mod.Run();',
  33166. ' $mod.Run();',
  33167. ' p = $mod.Run();',
  33168. ' p = $mod.Run();',
  33169. '};',
  33170. '']),
  33171. LinesToStr([
  33172. '']));
  33173. CheckResolverUnexpectedHints();
  33174. end;
  33175. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  33176. begin
  33177. StartProgram(true,[supTInterfacedObject]);
  33178. Add([
  33179. '{$mode objfpc}',
  33180. '{$modeswitch externalclass}',
  33181. 'type',
  33182. ' TJSPromise = class external name ''Promise''',
  33183. ' end;',
  33184. ' IBird = interface',
  33185. ' procedure Run;',
  33186. ' end;',
  33187. ' TBird = class(TInterfacedObject,IBird)',
  33188. ' procedure Run; async;',
  33189. ' end;',
  33190. 'procedure TBird.Run;',
  33191. 'begin',
  33192. 'end;',
  33193. 'begin',
  33194. ' ']);
  33195. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  33196. ConvertProgram;
  33197. end;
  33198. procedure TTestModule.TestAWait_ClassAs;
  33199. begin
  33200. StartProgram(false);
  33201. Add([
  33202. '{$mode objfpc}',
  33203. '{$modeswitch externalclass}',
  33204. 'type',
  33205. ' TJSPromise = class external name ''Promise''',
  33206. ' end;',
  33207. ' TObject = class',
  33208. ' function Run: TObject; async;',
  33209. ' end;',
  33210. ' TBird = class',
  33211. ' function Fly: TBird; async;',
  33212. ' end;',
  33213. 'function TObject.Run: TObject; async;',
  33214. 'begin',
  33215. 'end;',
  33216. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  33217. 'var o: TObject;',
  33218. 'begin',
  33219. ' o:=await(TObject,Run);',
  33220. ' o:=await(TObject,Fly);',
  33221. ' o:=await(TBird,Fly);',
  33222. ' o:=await(TObject,inherited Run);',
  33223. ' o:=await(TObject,inherited Run) as TBird;',
  33224. 'end;',
  33225. 'begin',
  33226. ' ']);
  33227. ConvertProgram;
  33228. CheckSource('TestAWait_ClassAs',
  33229. LinesToStr([ // statements
  33230. 'rtl.createClass(this, "TObject", null, function () {',
  33231. ' this.$init = function () {',
  33232. ' };',
  33233. ' this.$final = function () {',
  33234. ' };',
  33235. ' this.Run = async function () {',
  33236. ' var Result = null;',
  33237. ' return Result;',
  33238. ' };',
  33239. '});',
  33240. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33241. ' this.Fly = async function () {',
  33242. ' var Result = null;',
  33243. ' var o = null;',
  33244. ' o = await this.Run();',
  33245. ' o = await this.Fly();',
  33246. ' o = await this.Fly();',
  33247. ' o = await $mod.TObject.Run.call(this);',
  33248. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  33249. ' return Result;',
  33250. ' };',
  33251. '});',
  33252. '']),
  33253. LinesToStr([
  33254. '']));
  33255. CheckResolverUnexpectedHints();
  33256. end;
  33257. procedure TTestModule.TestLibrary_Empty;
  33258. begin
  33259. StartLibrary(false);
  33260. Add([
  33261. '']);
  33262. ConvertLibrary;
  33263. CheckSource('TestLibrary_Empty',
  33264. LinesToStr([ // statements
  33265. '']),
  33266. LinesToStr([
  33267. '']));
  33268. CheckResolverUnexpectedHints();
  33269. end;
  33270. procedure TTestModule.TestLibrary_ExportFunc;
  33271. begin
  33272. StartLibrary(false);
  33273. Add([
  33274. 'procedure Run(w: word);',
  33275. 'begin',
  33276. 'end;',
  33277. 'exports',
  33278. ' Run;',
  33279. ' run name ''Foo'';',
  33280. ' test1.run name ''Test1Run'';',
  33281. '']);
  33282. ConvertLibrary;
  33283. CheckSource('TestLibrary_ExportFunc',
  33284. LinesToStr([ // statements
  33285. 'this.Run = function (w) {',
  33286. '};',
  33287. 'export { this.Run as Run, this.Run as Foo, this.Run as Test1Run };',
  33288. '']),
  33289. LinesToStr([
  33290. '']));
  33291. CheckResolverUnexpectedHints();
  33292. end;
  33293. procedure TTestModule.TestLibrary_Export_Index_Fail;
  33294. begin
  33295. StartLibrary(false);
  33296. Add([
  33297. 'procedure Run(w: word);',
  33298. 'begin',
  33299. 'end;',
  33300. 'exports',
  33301. ' Run index 3;',
  33302. '']);
  33303. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  33304. ConvertLibrary;
  33305. end;
  33306. procedure TTestModule.TestLibrary_ExportVar;
  33307. begin
  33308. StartLibrary(false);
  33309. Add([
  33310. 'var Wing: word;',
  33311. 'exports',
  33312. ' Wing;',
  33313. '']);
  33314. ConvertLibrary;
  33315. CheckSource('TestLibrary_ExportVar',
  33316. LinesToStr([ // statements
  33317. 'this.Wing = 0;',
  33318. 'export { this.Wing as Wing };',
  33319. '']),
  33320. LinesToStr([
  33321. '']));
  33322. CheckResolverUnexpectedHints();
  33323. end;
  33324. procedure TTestModule.TestLibrary_ExportUnitFunc;
  33325. begin
  33326. end;
  33327. Initialization
  33328. RegisterTests([TTestModule]);
  33329. end.