tcmodules.pas 958 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval, FPPas2Js;
  22. const
  23. // default parser+scanner options
  24. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  25. co_tcmodules = [];
  26. JSONNewLine = {$IFDEF Windows}'\r\n'{$ELSE}'\n'{$ENDIF};
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. const
  34. SrcMarker: array[TSrcMarkerKind] of char = (
  35. '#', // mkLabel
  36. '@', // mkResolverReference
  37. '=' // mkDirectReference
  38. );
  39. type
  40. PSrcMarker = ^TSrcMarker;
  41. TSrcMarker = record
  42. Kind: TSrcMarkerKind;
  43. Filename: string;
  44. Row: integer;
  45. StartCol, EndCol: integer; // token start, end column
  46. Identifier: string;
  47. Next: PSrcMarker;
  48. end;
  49. TSystemUnitPart = (
  50. supTObject,
  51. supTVarRec,
  52. supTypeInfo,
  53. supTInterfacedObject,
  54. supWriteln
  55. );
  56. TSystemUnitParts = set of TSystemUnitPart;
  57. { TTestHintMessage }
  58. TTestHintMessage = class
  59. public
  60. Id: int64;
  61. MsgType: TMessageType;
  62. MsgNumber: integer;
  63. Msg: string;
  64. SourcePos: TPasSourcePos;
  65. end;
  66. TTestResolverReferenceData = record
  67. Filename: string;
  68. Row: integer;
  69. StartCol: integer;
  70. EndCol: integer;
  71. Found: TFPList; // list of TPasElement at this token
  72. end;
  73. PTestResolverReferenceData = ^TTestResolverReferenceData;
  74. { TTestPasParser }
  75. TTestPasParser = Class(TPasParser)
  76. end;
  77. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  78. { TTestEnginePasResolver }
  79. TTestEnginePasResolver = class(TPas2JsResolver)
  80. private
  81. FFilename: string;
  82. FModule: TPasModule;
  83. FOnFindUnit: TOnFindUnit;
  84. FParser: TTestPasParser;
  85. FStreamResolver: TStreamResolver;
  86. FScanner: TPas2jsPasScanner;
  87. FSource: string;
  88. procedure SetModule(const AValue: TPasModule);
  89. public
  90. destructor Destroy; override;
  91. function CreateElement(AClass: TPTreeElement; const AName: String;
  92. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  93. const ASrcPos: TPasSourcePos; TypeParams: TFPList = nil): TPasElement;
  94. overload; override;
  95. function FindUnit(const AName, InFilename: String; NameExpr,
  96. InFileExpr: TPasExpr): TPasModule; override;
  97. procedure UsedInterfacesFinished(Section: TPasSection); override;
  98. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  99. property Filename: string read FFilename write FFilename;
  100. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  101. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  102. property Parser: TTestPasParser read FParser write FParser;
  103. property Source: string read FSource write FSource;
  104. property Module: TPasModule read FModule write SetModule;
  105. end;
  106. { TCustomTestModule }
  107. TCustomTestModule = Class(TTestCase)
  108. private
  109. FWithTypeInfo: boolean;
  110. FSource: TStringList;
  111. FSkipTests: boolean;
  112. FScanner: TPas2jsPasScanner;
  113. FResolvers: TObjectList;// list of TTestEnginePasResolver
  114. FPasProgram: TPasProgram;
  115. FPasLibrary: TPasLibrary;
  116. FParser: TTestPasParser;
  117. FModule: TPasModule;
  118. FJSSource: TStringList;
  119. FJSRegModuleCall: TJSCallExpression;
  120. FJSModuleSrc: TJSSourceElements;
  121. FJSModuleCallArgs: TJSArguments;
  122. FJSModule: TJSSourceElements;
  123. FJSInterfaceUses: TJSArrayLiteral;
  124. FJSInitBody: TJSFunctionBody;
  125. FJSImplentationUses: TJSArrayLiteral;
  126. FJSImplementationUses: TJSArrayLiteral;
  127. FHub: TPas2JSResolverHub;
  128. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  129. FHintMsgs: TObjectList; // list of TTestHintMessage
  130. FFirstPasStatement: TPasImplBlock;
  131. FFileResolver: TStreamResolver;
  132. FFilename: string;
  133. FExpectedErrorNumber: integer;
  134. FExpectedErrorMsg: string;
  135. FExpectedErrorClass: ExceptClass;
  136. FEngine: TTestEnginePasResolver;
  137. FConverter: TPasToJSConverter;
  138. {$IFDEF EnablePasTreeGlobalRefCount}
  139. FElementRefCountAtSetup: int64;
  140. {$ENDIF}
  141. procedure FreeSrcMarkers;
  142. function GetResolverCount: integer;
  143. function GetResolvers(Index: integer): TTestEnginePasResolver;
  144. function GetMsgCount: integer;
  145. function GetMsgs(Index: integer): TTestHintMessage;
  146. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  147. procedure OnParserLog(Sender: TObject; const Msg: String);
  148. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  149. procedure OnScannerLog(Sender: TObject; const Msg: String);
  150. procedure OnCheckElementParent(El: TPasElement; arg: pointer);
  151. procedure OnFindReference(El: TPasElement; FindData: pointer);
  152. procedure SetWithTypeInfo(const AValue: boolean);
  153. protected
  154. procedure SetUp; override;
  155. function CreateConverter: TPasToJSConverter; virtual;
  156. function LoadUnit(const aUnitName: String): TPasModule;
  157. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  158. procedure TearDown; override;
  159. Procedure Add(Line: string); virtual;
  160. Procedure Add(const Lines: array of string);
  161. Procedure StartParsing; virtual;
  162. procedure ParseModuleQueue; virtual;
  163. procedure ParseModule; virtual;
  164. procedure ParseProgram; virtual;
  165. procedure ParseLibrary; virtual;
  166. procedure ParseUnit; virtual;
  167. protected
  168. FirstSrcMarker, LastSrcMarker: PSrcMarker;
  169. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  170. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  171. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  172. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  173. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  174. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  175. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  176. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  177. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  178. procedure ConvertModule; virtual;
  179. procedure ConvertProgram; virtual;
  180. procedure ConvertLibrary; virtual;
  181. procedure ConvertUnit; virtual;
  182. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  183. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  184. function GetDottedIdentifier(El: TJSElement): string;
  185. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  186. ImplStatements: string = ''); virtual;
  187. procedure CheckFullSource(Msg,ExpectedSrc: String); virtual;
  188. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  189. procedure CheckUnit(aFilename, ExpectedSrc: string); virtual;
  190. procedure CheckReferenceDirectives; virtual;
  191. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  192. Msg: string; Marker: PSrcMarker = nil); virtual;
  193. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  194. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  195. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  196. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  197. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  198. function IsErrorExpected(E: Exception): boolean;
  199. procedure RaiseErrorAtSrc(Msg: string; const aFilename: string; aRow, aCol: integer);
  200. procedure RaiseErrorAtSrcMarker(Msg: string; aMarker: PSrcMarker);
  201. procedure HandleScannerError(E: EScannerError);
  202. procedure HandleParserError(E: EParserError);
  203. procedure HandlePasResolveError(E: EPasResolve);
  204. procedure HandlePas2JSError(E: EPas2JS);
  205. procedure HandleException(E: Exception);
  206. procedure FailException(E: Exception);
  207. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  208. function IndexOfResolver(const aFilename: string): integer;
  209. function GetResolver(const aFilename: string): TTestEnginePasResolver;
  210. procedure GetSrc(Index: integer; out SrcLines: TStringList; out aFilename: string);
  211. function FindElementsAt(aFilename: string; aLine, aStartCol, aEndCol: integer): TFPList;// list of TPasElement
  212. function FindElementsAt(aMarker: PSrcMarker; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  213. function FindSrcLabel(const Identifier: string): PSrcMarker;
  214. function FindElementsAtSrcLabel(const Identifier: string; ErrorOnNoElements: boolean = true): TFPList;// list of TPasElement
  215. function GetDefaultNamespace: string;
  216. property PasProgram: TPasProgram Read FPasProgram;
  217. property PasLibrary: TPasLibrary Read FPasLibrary;
  218. property ResolverEngine: TTestEnginePasResolver read FEngine;
  219. property Filename: string read FFilename;
  220. Property Module: TPasModule Read FModule;
  221. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  222. property Converter: TPasToJSConverter read FConverter;
  223. property JSSource: TStringList read FJSSource;
  224. property JSModule: TJSSourceElements read FJSModule;
  225. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  226. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  227. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  228. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  229. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  230. property JSInitBody: TJSFunctionBody read FJSInitBody;
  231. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  232. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  233. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  234. property SkipTests: boolean read FSkipTests write FSkipTests;
  235. public
  236. constructor Create; override;
  237. destructor Destroy; override;
  238. property Hub: TPas2JSResolverHub read FHub;
  239. property Source: TStringList read FSource;
  240. property FileResolver: TStreamResolver read FFileResolver;
  241. property Scanner: TPas2jsPasScanner read FScanner;
  242. property Parser: TTestPasParser read FParser;
  243. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  244. property ResolverCount: integer read GetResolverCount;
  245. property MsgCount: integer read GetMsgCount;
  246. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  247. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  248. end;
  249. { TTestModule }
  250. TTestModule = class(TCustomTestModule)
  251. Published
  252. Procedure TestReservedWords;
  253. // program, units, includes
  254. Procedure TestEmptyProgram;
  255. Procedure TestEmptyProgramUseStrict;
  256. Procedure TestEmptyUnit;
  257. Procedure TestEmptyUnitUseStrict;
  258. Procedure TestDottedUnitNames;
  259. Procedure TestDottedUnitNameImpl;
  260. Procedure TestDottedUnitExpr;
  261. Procedure Test_ModeFPCFail;
  262. Procedure Test_ModeSwitchCBlocksFail;
  263. Procedure TestUnit_UseSystem;
  264. Procedure TestUnit_Intf1Impl2Intf1;
  265. Procedure TestIncludeVersion;
  266. // vars/const
  267. Procedure TestVarInt;
  268. Procedure TestVarBaseTypes;
  269. Procedure TestBaseTypeSingleFail;
  270. Procedure TestBaseTypeExtendedFail;
  271. Procedure TestConstBaseTypes;
  272. Procedure TestUnitImplVars;
  273. Procedure TestUnitImplConsts;
  274. Procedure TestUnitImplRecord;
  275. Procedure TestRenameJSNameConflict;
  276. Procedure TestLocalConst;
  277. Procedure TestVarExternal;
  278. Procedure TestVarExternalOtherUnit;
  279. Procedure TestVarAbsoluteFail;
  280. Procedure TestConstExternal;
  281. // numbers
  282. Procedure TestDouble;
  283. Procedure TestDoubleSmall;
  284. Procedure TestInteger;
  285. Procedure TestIntegerRange;
  286. Procedure TestIntegerTypecasts;
  287. Procedure TestInteger_BitwiseShrNativeInt;
  288. Procedure TestInteger_BitwiseShlNativeInt;
  289. Procedure TestInteger_SystemFunc;
  290. Procedure TestInteger_AssignOutsideConst;
  291. Procedure TestCurrency;
  292. Procedure TestForBoolDo;
  293. Procedure TestForIntDo;
  294. Procedure TestForIntInDo;
  295. // strings
  296. Procedure TestCharConst;
  297. Procedure TestChar_Compare;
  298. Procedure TestChar_BuiltInProcs;
  299. Procedure TestStringConst;
  300. Procedure TestStringConst_InvalidUTF16;
  301. Procedure TestStringConstSurrogate;
  302. Procedure TestStringConstWhitespaces;
  303. Procedure TestStringConst_Multiline;
  304. Procedure TestString_Length;
  305. Procedure TestString_Compare;
  306. Procedure TestString_SetLength;
  307. Procedure TestString_CharAt;
  308. Procedure TestStringHMinusFail;
  309. Procedure TestStr;
  310. Procedure TestBaseType_AnsiStringFail;
  311. Procedure TestBaseType_WideStringFail;
  312. Procedure TestBaseType_ShortStringFail;
  313. Procedure TestBaseType_RawByteStringFail;
  314. Procedure TestTypeShortstring_Fail;
  315. Procedure TestCharSet_Custom;
  316. Procedure TestWideChar;
  317. Procedure TestForCharDo;
  318. Procedure TestForCharInDo;
  319. // alias types
  320. Procedure TestAliasTypeRef;
  321. Procedure TestTypeCast_BaseTypes;
  322. Procedure TestTypeCast_AliasBaseTypes;
  323. // functions
  324. Procedure TestEmptyProc;
  325. Procedure TestProcOneParam;
  326. Procedure TestFunctionWithoutParams;
  327. Procedure TestProcedureWithoutParams;
  328. Procedure TestPrgProcVar;
  329. Procedure TestProcTwoArgs;
  330. Procedure TestProc_DefaultValue;
  331. Procedure TestUnitProcVar;
  332. Procedure TestImplProc;
  333. Procedure TestFunctionResult;
  334. Procedure TestNestedProc;
  335. Procedure TestNestedProc_ResultString;
  336. Procedure TestForwardProc;
  337. Procedure TestNestedForwardProc;
  338. Procedure TestAssignFunctionResult;
  339. Procedure TestFunctionResultInCondition;
  340. Procedure TestFunctionResultInForLoop;
  341. Procedure TestFunctionResultInTypeCast;
  342. Procedure TestExit;
  343. Procedure TestExit_ResultInFinally;
  344. Procedure TestBreak;
  345. Procedure TestBreakAsVar;
  346. Procedure TestContinue;
  347. Procedure TestProc_External;
  348. Procedure TestProc_ExternalOtherUnit;
  349. Procedure TestProc_Asm;
  350. Procedure TestProc_AsmSubBlock;
  351. Procedure TestProc_Assembler;
  352. Procedure TestProc_VarParam;
  353. Procedure TestProc_VarParamString;
  354. Procedure TestProc_VarParamV;
  355. Procedure TestProc_Overload;
  356. Procedure TestProc_OverloadForward;
  357. Procedure TestProc_OverloadIntfImpl;
  358. Procedure TestProc_OverloadNested;
  359. Procedure TestProc_OverloadNestedForward;
  360. Procedure TestProc_OverloadUnitCycle;
  361. Procedure TestProc_Varargs;
  362. Procedure TestProc_ConstOrder;
  363. Procedure TestProc_DuplicateConst;
  364. Procedure TestProc_LocalVarAbsolute;
  365. Procedure TestProc_ResultAbsolute;
  366. Procedure TestProc_LocalVarInit;
  367. Procedure TestProc_ReservedWords;
  368. Procedure TestProc_ConstRefWord;
  369. // anonymous functions
  370. Procedure TestAnonymousProc_Assign_ObjFPC;
  371. Procedure TestAnonymousProc_Assign_Delphi;
  372. Procedure TestAnonymousProc_Arg;
  373. Procedure TestAnonymousProc_Typecast;
  374. Procedure TestAnonymousProc_With;
  375. Procedure TestAnonymousProc_ExceptOn;
  376. Procedure TestAnonymousProc_Nested;
  377. Procedure TestAnonymousProc_NestedAssignResult;
  378. Procedure TestAnonymousProc_Class;
  379. Procedure TestAnonymousProc_ForLoop;
  380. Procedure TestAnonymousProc_AsmDelphi;
  381. // enums, sets
  382. Procedure TestEnum_Name;
  383. Procedure TestEnum_Number;
  384. Procedure TestEnum_ConstFail;
  385. Procedure TestEnum_Functions;
  386. Procedure TestEnumRg_Functions;
  387. Procedure TestEnum_AsParams;
  388. Procedure TestEnumRange_Array;
  389. Procedure TestEnum_ForIn;
  390. Procedure TestEnum_ScopedNumber;
  391. Procedure TestEnum_InFunction;
  392. Procedure TestEnum_Name_Anonymous_Unit;
  393. Procedure TestSet_Enum;
  394. Procedure TestSet_Operators;
  395. Procedure TestSet_Operator_In;
  396. Procedure TestSet_Functions;
  397. Procedure TestSet_PassAsArgClone;
  398. Procedure TestSet_AsParams;
  399. Procedure TestSet_Property;
  400. Procedure TestSet_EnumConst;
  401. Procedure TestSet_IntConst;
  402. Procedure TestSet_IntRange;
  403. Procedure TestSet_AnonymousEnumType;
  404. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  405. Procedure TestSet_ConstEnum;
  406. Procedure TestSet_ConstChar;
  407. Procedure TestSet_ConstInt;
  408. Procedure TestSet_InFunction;
  409. Procedure TestSet_ForIn;
  410. // statements
  411. Procedure TestNestBegin;
  412. Procedure TestIncDec;
  413. Procedure TestLoHiFpcMode;
  414. Procedure TestLoHiDelphiMode;
  415. Procedure TestAssignments;
  416. Procedure TestArithmeticOperators1;
  417. Procedure TestMultiAdd;
  418. Procedure TestLogicalOperators;
  419. Procedure TestBitwiseOperators;
  420. Procedure TestBitwiseOperatorsLongword;
  421. Procedure TestFunctionInt;
  422. Procedure TestFunctionString;
  423. Procedure TestIfThen;
  424. Procedure TestForLoop;
  425. Procedure TestForLoopInsideFunction;
  426. Procedure TestForLoop_ReadVarAfter;
  427. Procedure TestForLoop_Nested;
  428. Procedure TestRepeatUntil;
  429. Procedure TestAsmBlock;
  430. Procedure TestAsmPas_Impl; // ToDo
  431. Procedure TestTryFinally;
  432. Procedure TestTryExcept;
  433. Procedure TestTryExcept_ReservedWords;
  434. Procedure TestIfThenRaiseElse;
  435. Procedure TestCaseOf;
  436. Procedure TestCaseOf_UseSwitch;
  437. Procedure TestCaseOfNoElse;
  438. Procedure TestCaseOfNoElse_UseSwitch;
  439. Procedure TestCaseOfRange;
  440. Procedure TestCaseOfString;
  441. Procedure TestCaseOfChar;
  442. Procedure TestCaseOfExternalClassConst;
  443. Procedure TestDebugger;
  444. // arrays
  445. Procedure TestArray_Dynamic;
  446. Procedure TestArray_Dynamic_Nil;
  447. Procedure TestArray_DynMultiDimensional;
  448. Procedure TestArray_DynamicAssign;
  449. Procedure TestArray_StaticInt;
  450. Procedure TestArray_StaticBool;
  451. Procedure TestArray_StaticChar;
  452. Procedure TestArray_StaticMultiDim;
  453. Procedure TestArray_StaticInFunction;
  454. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  455. Procedure TestArrayOfRecord;
  456. Procedure TestArray_StaticRecord;
  457. Procedure TestArrayOfSet;
  458. Procedure TestArray_DynAsParam;
  459. Procedure TestArray_StaticAsParam;
  460. Procedure TestArrayElement_AsParams;
  461. Procedure TestArrayElementFromFuncResult_AsParams;
  462. Procedure TestArrayEnumTypeRange;
  463. Procedure TestArray_SetLengthOutArg;
  464. Procedure TestArray_SetLengthProperty;
  465. Procedure TestArray_SetLengthMultiDim;
  466. Procedure TestArray_SetLengthDynOfStatic;
  467. Procedure TestArray_OpenArrayOfString;
  468. Procedure TestArray_ArrayOfCharAssignString;
  469. Procedure TestArray_ConstRef;
  470. Procedure TestArray_Concat;
  471. Procedure TestArray_Concat_Append;
  472. Procedure TestArray_Concat_Append_Var;
  473. Procedure TestArray_Copy;
  474. Procedure TestArray_InsertDelete;
  475. Procedure TestArray_Add_Append;
  476. Procedure TestArray_DynArrayConstObjFPC;
  477. Procedure TestArray_DynArrayConstDelphi;
  478. Procedure TestArray_ArrayLitAsParam;
  479. Procedure TestArray_ArrayLitMultiDimAsParam;
  480. Procedure TestArray_ArrayLitStaticAsParam;
  481. Procedure TestArray_ForInArrOfString;
  482. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  483. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  484. Procedure TestArrayOfConst_TVarRec;
  485. Procedure TestArrayOfConst_PassBaseTypes;
  486. Procedure TestArrayOfConst_PassObj;
  487. // record
  488. Procedure TestRecord_Empty;
  489. Procedure TestRecord_Var;
  490. Procedure TestRecord_VarExternal;
  491. Procedure TestRecord_WithDo;
  492. Procedure TestRecord_Assign;
  493. Procedure TestRecord_AsParams;
  494. Procedure TestRecord_ConstRef;
  495. Procedure TestRecordElement_AsParams;
  496. Procedure TestRecordElementFromFuncResult_AsParams;
  497. Procedure TestRecordElementFromWith_AsParams;
  498. Procedure TestRecord_Equal;
  499. Procedure TestRecord_JSValue;
  500. Procedure TestRecord_VariantFail;
  501. Procedure TestRecord_FieldArray;
  502. Procedure TestRecord_Const;
  503. Procedure TestRecord_TypecastFail;
  504. Procedure TestRecord_InFunction;
  505. Procedure TestRecord_ArrayConstMultiline;
  506. // anonymous record
  507. Procedure TestRecordAnonym_Field;
  508. Procedure TestRecordAnonym_Assign;
  509. Procedure TestRecordAnonym_Nested;
  510. Procedure TestRecordAnonym_Const;
  511. Procedure TestRecordAnonym_InFunction;
  512. // advanced record
  513. Procedure TestAdvRecord_Function;
  514. Procedure TestAdvRecord_Property;
  515. Procedure TestAdvRecord_PropertyDefault;
  516. Procedure TestAdvRecord_Property_ClassMethod;
  517. Procedure TestAdvRecord_Const;
  518. Procedure TestAdvRecord_ExternalField;
  519. Procedure TestAdvRecord_SubRecord;
  520. Procedure TestAdvRecord_SubClass;
  521. Procedure TestAdvRecord_SubInterfaceFail;
  522. Procedure TestAdvRecord_Constructor;
  523. Procedure TestAdvRecord_ClassConstructor_Program;
  524. Procedure TestAdvRecord_ClassConstructor_Unit;
  525. // classes
  526. Procedure TestClass_TObjectDefaultConstructor;
  527. Procedure TestClass_TObjectConstructorWithParams;
  528. Procedure TestClass_TObjectConstructorWithDefaultParam;
  529. Procedure TestClass_Var;
  530. Procedure TestClass_Method;
  531. Procedure TestClass_Implementation;
  532. Procedure TestClass_Inheritance;
  533. Procedure TestClass_TypeAlias;
  534. Procedure TestClass_AbstractMethod;
  535. Procedure TestClass_CallInherited_ProcNoParams;
  536. Procedure TestClass_CallInherited_WithParams;
  537. Procedure TestClasS_CallInheritedConstructor;
  538. Procedure TestClass_ClassVar_Assign;
  539. Procedure TestClass_CallClassMethod;
  540. Procedure TestClass_CallClassMethodStatic;
  541. Procedure TestClass_Property;
  542. Procedure TestClass_Property_ClassMethod;
  543. Procedure TestClass_Property_ClassMethodStatic;
  544. Procedure TestClass_Property_Indexed;
  545. Procedure TestClass_Property_IndexSpec;
  546. Procedure TestClass_PropertyOfTypeArray;
  547. Procedure TestClass_PropertyDefault;
  548. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  549. //Procedure TestClass_PropertyDefault;
  550. Procedure TestClass_PropertyOverride;
  551. Procedure TestClass_PropertyIncVisibility;
  552. Procedure TestClass_Assigned;
  553. Procedure TestClass_WithClassDoCreate;
  554. Procedure TestClass_WithClassInstDoProperty;
  555. Procedure TestClass_WithClassInstDoPropertyWithParams;
  556. Procedure TestClass_WithClassInstDoFunc;
  557. Procedure TestClass_TypeCast;
  558. Procedure TestClass_TypeCastUntypedParam;
  559. Procedure TestClass_Overloads;
  560. Procedure TestClass_OverloadsAncestor;
  561. Procedure TestClass_OverloadConstructor;
  562. Procedure TestClass_OverloadDelphiOverride;
  563. Procedure TestClass_ReintroduceVarDelphi;
  564. Procedure TestClass_ReintroducedVar;
  565. Procedure TestClass_RaiseDescendant;
  566. Procedure TestClass_ExternalMethod;
  567. Procedure TestClass_ExternalVirtualNameMismatchFail;
  568. Procedure TestClass_ExternalOverrideFail;
  569. Procedure TestClass_ExternalVar;
  570. Procedure TestClass_Const;
  571. Procedure TestClass_ConstEnum;
  572. Procedure TestClass_LocalConstDuplicate_Prg;
  573. Procedure TestClass_LocalConstDuplicate_Unit;
  574. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  575. Procedure TestClass_LocalVarSelfFail;
  576. Procedure TestClass_ArgSelfFail;
  577. Procedure TestClass_NestedProcSelf;
  578. Procedure TestClass_NestedProcSelf2;
  579. Procedure TestClass_NestedProcClassSelf;
  580. Procedure TestClass_NestedProcCallInherited;
  581. Procedure TestClass_TObjectFree;
  582. Procedure TestClass_TObjectFree_VarArg;
  583. Procedure TestClass_TObjectFreeNewInstance;
  584. Procedure TestClass_TObjectFreeLowerCase;
  585. Procedure TestClass_TObjectFreeFunctionFail;
  586. Procedure TestClass_TObjectFreePropertyFail;
  587. Procedure TestClass_ForIn;
  588. Procedure TestClass_DispatchMessage;
  589. Procedure TestClass_Message_DuplicateIntFail;
  590. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  591. // class of
  592. Procedure TestClassOf_Create;
  593. Procedure TestClassOf_Call;
  594. Procedure TestClassOf_Assign;
  595. Procedure TestClassOf_Is;
  596. Procedure TestClassOf_Compare;
  597. Procedure TestClassOf_ClassVar;
  598. Procedure TestClassOf_ClassMethod;
  599. Procedure TestClassOf_ClassProperty;
  600. Procedure TestClassOf_ClassMethodSelf;
  601. Procedure TestClassOf_TypeCast;
  602. Procedure TestClassOf_ImplicitFunctionCall;
  603. Procedure TestClassOf_Const;
  604. // nested class
  605. Procedure TestNestedClass_Alias;
  606. Procedure TestNestedClass_Record;
  607. Procedure TestNestedClass_Class;
  608. Procedure TestNestedClass_CallInherited;
  609. // external class
  610. Procedure TestExternalClass_Var;
  611. Procedure TestExternalClass_Const;
  612. Procedure TestExternalClass_Dollar;
  613. Procedure TestExternalClass_DuplicateVarFail;
  614. Procedure TestExternalClass_Method;
  615. Procedure TestExternalClass_ClassMethod;
  616. Procedure TestExternalClass_ClassMethodStatic;
  617. Procedure TestExternalClass_FunctionResultInTypeCast;
  618. Procedure TestExternalClass_NonExternalOverride;
  619. Procedure TestExternalClass_OverloadHint;
  620. Procedure TestExternalClass_SameNamePublishedProperty;
  621. Procedure TestExternalClass_Property;
  622. Procedure TestExternalClass_PropertyDate;
  623. Procedure TestExternalClass_ClassProperty;
  624. Procedure TestExternalClass_ClassOf;
  625. Procedure TestExternalClass_ClassOtherUnit;
  626. Procedure TestExternalClass_Is;
  627. Procedure TestExternalClass_As;
  628. Procedure TestExternalClass_DestructorFail;
  629. Procedure TestExternalClass_New;
  630. Procedure TestExternalClass_ClassOf_New;
  631. Procedure TestExternalClass_FuncClassOf_New;
  632. Procedure TestExternalClass_New_PasClassFail;
  633. Procedure TestExternalClass_New_PasClassBracketsFail;
  634. Procedure TestExternalClass_NewExtName;
  635. Procedure TestExternalClass_Constructor;
  636. Procedure TestExternalClass_ConstructorBrackets;
  637. Procedure TestExternalClass_LocalConstSameName;
  638. Procedure TestExternalClass_ReintroduceOverload;
  639. Procedure TestExternalClass_Inherited;
  640. Procedure TestExternalClass_PascalAncestorFail;
  641. Procedure TestExternalClass_NewInstance;
  642. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  643. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  644. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  645. Procedure TestExternalClass_JSFunctionPasDescendant;
  646. Procedure TestExternalClass_PascalProperty;
  647. Procedure TestExternalClass_TypeCastToRootClass;
  648. Procedure TestExternalClass_TypeCastToJSObject;
  649. Procedure TestExternalClass_TypeCastStringToExternalString;
  650. Procedure TestExternalClass_TypeCastToJSFunction;
  651. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  652. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  653. Procedure TestExternalClass_BracketAccessor;
  654. Procedure TestExternalClass_BracketAccessor_Call;
  655. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  656. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  657. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  658. Procedure TestExternalClass_BracketAccessor_MultiType;
  659. Procedure TestExternalClass_BracketAccessor_Index;
  660. Procedure TestExternalClass_ForInJSObject;
  661. Procedure TestExternalClass_ForInJSArray;
  662. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  663. Procedure TestExternalClass_NestedConstructor;
  664. // class interfaces
  665. Procedure TestClassInterface_Corba;
  666. Procedure TestClassInterface_Corba_ProcExternalFail;
  667. Procedure TestClassInterface_Corba_Overloads;
  668. Procedure TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  669. Procedure TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  670. Procedure TestClassInterface_Corba_AncestorImpl;
  671. Procedure TestClassInterface_Corba_ImplReintroduce;
  672. Procedure TestClassInterface_Corba_MethodResolution;
  673. Procedure TestClassInterface_COM_AncestorMoreInterfaces;
  674. Procedure TestClassInterface_Corba_MethodOverride;
  675. Procedure TestClassInterface_Corba_Delegation;
  676. Procedure TestClassInterface_Corba_DelegationStatic;
  677. Procedure TestClassInterface_Corba_Operators;
  678. Procedure TestClassInterface_Corba_Args;
  679. Procedure TestClassInterface_Corba_ForIn;
  680. Procedure TestClassInterface_Corba_ArrayOfIntf;
  681. Procedure TestClassInterface_COM_AssignVar;
  682. Procedure TestClassInterface_COM_AssignArg;
  683. Procedure TestClassInterface_COM_FunctionResult;
  684. Procedure TestClassInterface_COM_InheritedFuncResult;
  685. Procedure TestClassInterface_COM_FunctionExit;
  686. Procedure TestClassInterface_COM_IsAsTypeCasts;
  687. Procedure TestClassInterface_COM_PassAsArg;
  688. Procedure TestClassInterface_COM_PassToUntypedParam;
  689. Procedure TestClassInterface_COM_FunctionInExpr;
  690. Procedure TestClassInterface_COM_Property;
  691. Procedure TestClassInterface_COM_IntfProperty;
  692. Procedure TestClassInterface_COM_Delegation;
  693. Procedure TestClassInterface_COM_With;
  694. Procedure TestClassInterface_COM_ForObjectInInterface;
  695. Procedure TestClassInterface_COM_ForInterfaceInObject;
  696. Procedure TestClassInterface_COM_ArrayOfIntf; // todo
  697. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  698. Procedure TestClassInterface_COM_RecordIntfFail;
  699. Procedure TestClassInterface_COM_UnitInitialization;
  700. Procedure TestClassInterface_Corba_GUID;
  701. Procedure TestClassInterface_Corba_GUIDProperty;
  702. // helpers
  703. Procedure TestClassHelper_ClassVar;
  704. Procedure TestClassHelper_Method_AccessInstanceFields;
  705. Procedure TestClassHelper_Method_Call;
  706. Procedure TestClassHelper_Method_Nested_Call;
  707. Procedure TestClassHelper_ClassMethod_Call;
  708. Procedure TestClassHelper_ClassOf;
  709. Procedure TestClassHelper_MethodRefObjFPC;
  710. Procedure TestClassHelper_Constructor;
  711. Procedure TestClassHelper_InheritedObjFPC;
  712. Procedure TestClassHelper_Property;
  713. Procedure TestClassHelper_Property_Array;
  714. Procedure TestClassHelper_Property_Array_Default;
  715. Procedure TestClassHelper_Property_Array_DefaultDefault;
  716. Procedure TestClassHelper_ClassProperty;
  717. Procedure TestClassHelper_ClassPropertyStatic;
  718. Procedure TestClassHelper_ClassProperty_Array;
  719. Procedure TestClassHelper_ForIn;
  720. Procedure TestClassHelper_PassProperty;
  721. Procedure TestExtClassHelper_ClassVar;
  722. Procedure TestExtClassHelper_Method_Call;
  723. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  724. Procedure TestRecordHelper_ClassVar;
  725. Procedure TestRecordHelper_Method_Call;
  726. Procedure TestRecordHelper_Constructor;
  727. Procedure TestTypeHelper_ClassVar;
  728. Procedure TestTypeHelper_PassResultElement;
  729. Procedure TestTypeHelper_PassArgs;
  730. Procedure TestTypeHelper_PassVarConst;
  731. Procedure TestTypeHelper_PassFuncResult;
  732. Procedure TestTypeHelper_PassPropertyField;
  733. Procedure TestTypeHelper_PassPropertyGetter;
  734. Procedure TestTypeHelper_PassClassPropertyField;
  735. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  736. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  737. Procedure TestTypeHelper_Property;
  738. Procedure TestTypeHelper_Property_Array;
  739. Procedure TestTypeHelper_ClassProperty;
  740. Procedure TestTypeHelper_ClassProperty_Array;
  741. Procedure TestTypeHelper_ClassMethod;
  742. Procedure TestTypeHelper_ExtClassMethodFail;
  743. Procedure TestTypeHelper_Constructor;
  744. Procedure TestTypeHelper_Word;
  745. Procedure TestTypeHelper_Boolean;
  746. Procedure TestTypeHelper_WordBool;
  747. Procedure TestTypeHelper_Double;
  748. Procedure TestTypeHelper_NativeInt;
  749. Procedure TestTypeHelper_StringChar;
  750. Procedure TestTypeHelper_JSValue;
  751. Procedure TestTypeHelper_Array;
  752. Procedure TestTypeHelper_EnumType;
  753. Procedure TestTypeHelper_SetType;
  754. Procedure TestTypeHelper_InterfaceType;
  755. Procedure TestTypeHelper_NestedSelf;
  756. // proc types
  757. Procedure TestProcType;
  758. Procedure TestProcType_Arg;
  759. Procedure TestProcType_FunctionFPC;
  760. Procedure TestProcType_FunctionDelphi;
  761. Procedure TestProcType_ProcedureDelphi;
  762. Procedure TestProcType_AsParam;
  763. Procedure TestProcType_MethodFPC;
  764. Procedure TestProcType_MethodDelphi;
  765. Procedure TestProcType_PropertyFPC;
  766. Procedure TestProcType_PropertyDelphi;
  767. Procedure TestProcType_WithClassInstDoPropertyFPC;
  768. Procedure TestProcType_Nested;
  769. Procedure TestProcType_NestedOfObject;
  770. Procedure TestProcType_ReferenceToProc;
  771. Procedure TestProcType_ReferenceToMethod;
  772. Procedure TestProcType_Typecast;
  773. Procedure TestProcType_PassProcToUntyped;
  774. Procedure TestProcType_PassProcToArray;
  775. Procedure TestProcType_SafeCallObjFPC;
  776. Procedure TestProcType_SafeCallDelphi;
  777. Procedure TestProcType_SafeCall_Arg;
  778. // pointer
  779. Procedure TestPointer;
  780. Procedure TestPointer_Proc;
  781. Procedure TestPointer_AssignRecordFail;
  782. Procedure TestPointer_AssignStaticArrayFail;
  783. Procedure TestPointer_TypeCastJSValueToPointer;
  784. Procedure TestPointer_NonRecordFail;
  785. Procedure TestPointer_AnonymousArgTypeFail;
  786. Procedure TestPointer_AnonymousVarTypeFail;
  787. Procedure TestPointer_AnonymousResultTypeFail;
  788. Procedure TestPointer_AddrOperatorFail;
  789. Procedure TestPointer_ArrayParamsFail;
  790. Procedure TestPointer_PointerAddFail;
  791. Procedure TestPointer_IncPointerFail;
  792. Procedure TestPointer_Record;
  793. Procedure TestPointer_RecordArg;
  794. // jsvalue
  795. Procedure TestJSValue_AssignToJSValue;
  796. Procedure TestJSValue_TypeCastToBaseType;
  797. Procedure TestJSValue_TypecastToJSValue;
  798. Procedure TestJSValue_Equal;
  799. Procedure TestJSValue_If;
  800. Procedure TestJSValue_Not;
  801. Procedure TestJSValue_Enum;
  802. Procedure TestJSValue_ClassInstance;
  803. Procedure TestJSValue_ClassOf;
  804. Procedure TestJSValue_ArrayOfJSValue;
  805. Procedure TestJSValue_ArrayLit;
  806. Procedure TestJSValue_Params;
  807. Procedure TestJSValue_UntypedParam;
  808. Procedure TestJSValue_FuncResultType;
  809. Procedure TestJSValue_ProcType_Assign;
  810. Procedure TestJSValue_ProcType_Equal;
  811. Procedure TestJSValue_ProcType_Param;
  812. Procedure TestJSValue_AssignToPointerFail;
  813. Procedure TestJSValue_OverloadDouble;
  814. Procedure TestJSValue_OverloadNativeInt;
  815. Procedure TestJSValue_OverloadWord;
  816. Procedure TestJSValue_OverloadString;
  817. Procedure TestJSValue_OverloadChar;
  818. Procedure TestJSValue_OverloadPointer;
  819. Procedure TestJSValue_ForIn;
  820. // RTTI
  821. Procedure TestRTTI_IntRange;
  822. Procedure TestRTTI_Double;
  823. Procedure TestRTTI_ProcType;
  824. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  825. Procedure TestRTTI_ProcTypeAnonymous;
  826. Procedure TestRTTI_EnumAndSetType;
  827. Procedure TestRTTI_EnumRange;
  828. Procedure TestRTTI_AnonymousEnumType;
  829. Procedure TestRTTI_StaticArray;
  830. Procedure TestRTTI_DynArray;
  831. Procedure TestRTTI_ArrayNestedAnonymous;
  832. Procedure TestRTTI_PublishedMethodOverloadFail;
  833. Procedure TestRTTI_PublishedMethodHideNoHint;
  834. Procedure TestRTTI_PublishedMethodExternalFail;
  835. Procedure TestRTTI_PublishedClassPropertyFail;
  836. Procedure TestRTTI_PublishedClassFieldFail;
  837. Procedure TestRTTI_PublishedFieldExternalFail;
  838. Procedure TestRTTI_Class_Field;
  839. Procedure TestRTTI_Class_FieldPrivate;
  840. Procedure TestRTTI_Class_Method;
  841. Procedure TestRTTI_Class_MethodArgFlags;
  842. Procedure TestRTTI_Class_MethodPrivate;
  843. Procedure TestRTTI_Class_Property;
  844. Procedure TestRTTI_Class_PropertyParams;
  845. Procedure TestRTTI_Class_PropertyPrivate;
  846. Procedure TestRTTI_Class_ClassProperty;
  847. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  848. Procedure TestRTTI_Class_OmitRTTI;
  849. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  850. Procedure TestRTTI_IndexModifier;
  851. Procedure TestRTTI_StoredModifier;
  852. Procedure TestRTTI_DefaultValue;
  853. Procedure TestRTTI_DefaultValueSet;
  854. Procedure TestRTTI_DefaultValueRangeType;
  855. Procedure TestRTTI_DefaultValueInherit;
  856. Procedure TestRTTI_OverrideMethod;
  857. Procedure TestRTTI_ReintroduceMethod;
  858. Procedure TestRTTI_OverloadProperty;
  859. // ToDo: array argument
  860. Procedure TestRTTI_ClassForward;
  861. Procedure TestRTTI_ClassOf;
  862. Procedure TestRTTI_Record;
  863. Procedure TestRTTI_RecordAnonymousArray;
  864. Procedure TestRTTI_Record_ClassVarType;
  865. Procedure TestRTTI_LocalTypes;
  866. Procedure TestRTTI_TypeInfo_BaseTypes;
  867. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  868. Procedure TestRTTI_TypeInfo_LocalFail;
  869. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  870. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  871. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  872. Procedure TestRTTI_TypeInfo_FunctionClassType;
  873. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  874. Procedure TestRTTI_Interface_Corba;
  875. Procedure TestRTTI_Interface_COM;
  876. Procedure TestRTTI_ClassHelper;
  877. Procedure TestRTTI_ExternalClass;
  878. Procedure TestRTTI_Unit;
  879. // Resourcestring
  880. Procedure TestResourcestringProgram;
  881. Procedure TestResourcestringUnit;
  882. Procedure TestResourcestringImplementation;
  883. // Attributes
  884. Procedure TestAttributes_Members;
  885. Procedure TestAttributes_Types;
  886. Procedure TestAttributes_HelperConstructor_Fail;
  887. Procedure TestAttributes_InterfacesList;
  888. // Assertions, checks
  889. procedure TestAssert;
  890. procedure TestAssert_SysUtils;
  891. procedure TestObjectChecks;
  892. procedure TestOverflowChecks_Int;
  893. procedure TestRangeChecks_AssignInt;
  894. procedure TestRangeChecks_AssignIntRange;
  895. procedure TestRangeChecks_AssignEnum;
  896. procedure TestRangeChecks_AssignEnumRange;
  897. procedure TestRangeChecks_AssignChar;
  898. procedure TestRangeChecks_AssignCharRange;
  899. procedure TestRangeChecks_ArrayIndex;
  900. procedure TestRangeChecks_ArrayOfRecIndex;
  901. procedure TestRangeChecks_StringIndex;
  902. procedure TestRangeChecks_TypecastInt;
  903. procedure TestRangeChecks_TypeHelperInt;
  904. procedure TestRangeChecks_AssignCurrency;
  905. // Async/AWait
  906. Procedure TestAsync_Proc;
  907. Procedure TestAsync_CallResultIsPromise;
  908. Procedure TestAsync_ConstructorFail;
  909. Procedure TestAsync_PropertyGetterFail;
  910. Procedure TestAwait_NonPromiseWithTypeFail;
  911. Procedure TestAwait_AsyncCallTypeMismatch;
  912. Procedure TestAWait_OutsideAsyncFail;
  913. Procedure TestAWait_IntegerFail;
  914. Procedure TestAWait_ExternalClassPromise;
  915. Procedure TestAWait_JSValue;
  916. Procedure TestAWait_Result;
  917. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  918. Procedure TestAsync_AnonymousProc;
  919. Procedure TestAsync_AnonymousProc_PromiseViaDotContext;
  920. Procedure TestAsync_ProcType;
  921. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  922. Procedure TestAsync_Inherited;
  923. Procedure TestAsync_ClassInterface;
  924. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  925. Procedure TestAWait_ClassAs;
  926. // Library
  927. Procedure TestLibrary_Empty;
  928. Procedure TestLibrary_ExportFunc;
  929. Procedure TestLibrary_ExportFuncOverloadedFail;
  930. Procedure TestLibrary_Export_Index_Fail;
  931. Procedure TestLibrary_ExportVar;
  932. Procedure TestLibrary_ExportUnitFunc;
  933. end;
  934. function LinesToStr(Args: array of const): string;
  935. function ExtractFileUnitName(aFilename: string): string;
  936. function JSToStr(El: TJSElement): string;
  937. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  938. implementation
  939. function LinesToStr(Args: array of const): string;
  940. var
  941. s: String;
  942. i: Integer;
  943. begin
  944. s:='';
  945. for i:=Low(Args) to High(Args) do
  946. case Args[i].VType of
  947. vtChar: s += Args[i].VChar+LineEnding;
  948. vtString: s += Args[i].VString^+LineEnding;
  949. vtPChar: s += Args[i].VPChar+LineEnding;
  950. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  951. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  952. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  953. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  954. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  955. end;
  956. Result:=s;
  957. end;
  958. function ExtractFileUnitName(aFilename: string): string;
  959. var
  960. p: Integer;
  961. begin
  962. Result:=ExtractFileName(aFilename);
  963. if Result='' then exit;
  964. for p:=length(Result) downto 1 do
  965. case Result[p] of
  966. '/','\': exit;
  967. '.':
  968. begin
  969. Delete(Result,p,length(Result));
  970. exit;
  971. end;
  972. end;
  973. end;
  974. function JSToStr(El: TJSElement): string;
  975. var
  976. aWriter: TBufferWriter;
  977. aJSWriter: TJSWriter;
  978. begin
  979. aJSWriter:=nil;
  980. aWriter:=TBufferWriter.Create(1000);
  981. try
  982. aJSWriter:=TJSWriter.Create(aWriter);
  983. aJSWriter.IndentSize:=2;
  984. aJSWriter.WriteJS(El);
  985. Result:=aWriter.AsString;
  986. finally
  987. aJSWriter.Free;
  988. aWriter.Free;
  989. end;
  990. end;
  991. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  992. // search diff, ignore changes in spaces
  993. const
  994. SpaceChars = [#9,#10,#13,' '];
  995. var
  996. ExpectedP, ActualP: PChar;
  997. function FindLineEnd(p: PChar): PChar;
  998. begin
  999. Result:=p;
  1000. while not (Result^ in [#0,#10,#13]) do inc(Result);
  1001. end;
  1002. function FindLineStart(p, MinP: PChar): PChar;
  1003. begin
  1004. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  1005. Result:=p;
  1006. end;
  1007. procedure SkipLineEnd(var p: PChar);
  1008. begin
  1009. if p^ in [#10,#13] then
  1010. begin
  1011. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  1012. inc(p,2)
  1013. else
  1014. inc(p);
  1015. end;
  1016. end;
  1017. function HasSpecialChar(s: string): boolean;
  1018. var
  1019. i: Integer;
  1020. begin
  1021. for i:=1 to length(s) do
  1022. if s[i] in [#0..#31,#127..#255] then
  1023. exit(true);
  1024. Result:=false;
  1025. end;
  1026. function HashSpecialChars(s: string): string;
  1027. var
  1028. i: Integer;
  1029. begin
  1030. Result:='';
  1031. for i:=1 to length(s) do
  1032. if s[i] in [#0..#31,#127..#255] then
  1033. Result:=Result+'#'+hexstr(ord(s[i]),2)
  1034. else
  1035. Result:=Result+s[i];
  1036. end;
  1037. procedure DiffFound;
  1038. var
  1039. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  1040. ExpLine, ActLine: String;
  1041. i, LineNo, DiffLineNo: Integer;
  1042. begin
  1043. writeln('Diff found "',Msg,'". Lines:');
  1044. // write correct lines
  1045. p:=PChar(Expected);
  1046. LineNo:=0;
  1047. DiffLineNo:=0;
  1048. repeat
  1049. StartPos:=p;
  1050. while not (p^ in [#0,#10,#13]) do inc(p);
  1051. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  1052. SkipLineEnd(p);
  1053. inc(LineNo);
  1054. if (p<=ExpectedP) and (p^<>#0) then
  1055. begin
  1056. writeln('= ',ExpLine);
  1057. end else begin
  1058. // diff line
  1059. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1060. // write actual line
  1061. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1062. ActLineEndP:=FindLineEnd(ActualP);
  1063. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1064. writeln('- ',ActLine);
  1065. if HasSpecialChar(ActLine) then
  1066. writeln('- ',HashSpecialChars(ActLine));
  1067. // write expected line
  1068. writeln('+ ',ExpLine);
  1069. if HasSpecialChar(ExpLine) then
  1070. writeln('- ',HashSpecialChars(ExpLine));
  1071. // write empty line with pointer ^
  1072. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1073. writeln('^');
  1074. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1075. CheckSrcDiff:=false;
  1076. // write up to three following actual lines to get some context
  1077. for i:=1 to 3 do begin
  1078. ActLineStartP:=ActLineEndP;
  1079. SkipLineEnd(ActLineStartP);
  1080. if ActLineStartP^=#0 then break;
  1081. ActLineEndP:=FindLineEnd(ActLineStartP);
  1082. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1083. writeln('~ ',ActLine);
  1084. end;
  1085. exit;
  1086. end;
  1087. until p^=#0;
  1088. writeln('DiffFound Actual:-----------------------');
  1089. writeln(Actual);
  1090. writeln('DiffFound Expected:---------------------');
  1091. writeln(Expected);
  1092. writeln('DiffFound ------------------------------');
  1093. Msg:='diff found, but lines are the same, internal error';
  1094. CheckSrcDiff:=false;
  1095. end;
  1096. var
  1097. IsSpaceNeeded: Boolean;
  1098. LastChar, Quote: Char;
  1099. begin
  1100. Result:=true;
  1101. Msg:='';
  1102. if Expected='' then Expected:=' ';
  1103. if Actual='' then Actual:=' ';
  1104. ExpectedP:=PChar(Expected);
  1105. ActualP:=PChar(Actual);
  1106. repeat
  1107. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1108. case ExpectedP^ of
  1109. #0:
  1110. begin
  1111. // check that rest of Actual has only spaces
  1112. while ActualP^ in SpaceChars do inc(ActualP);
  1113. if ActualP^<>#0 then
  1114. begin
  1115. DiffFound;
  1116. exit;
  1117. end;
  1118. exit(true);
  1119. end;
  1120. ' ',#9,#10,#13:
  1121. begin
  1122. // skip space in Expected
  1123. IsSpaceNeeded:=false;
  1124. if ExpectedP>PChar(Expected) then
  1125. LastChar:=ExpectedP[-1]
  1126. else
  1127. LastChar:=#0;
  1128. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1129. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1130. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1131. IsSpaceNeeded:=true;
  1132. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1133. begin
  1134. DiffFound;
  1135. exit;
  1136. end;
  1137. while ActualP^ in SpaceChars do inc(ActualP);
  1138. end;
  1139. '''','"':
  1140. begin
  1141. while ActualP^ in SpaceChars do inc(ActualP);
  1142. if ExpectedP^<>ActualP^ then
  1143. begin
  1144. DiffFound;
  1145. exit;
  1146. end;
  1147. Quote:=ExpectedP^;
  1148. repeat
  1149. inc(ExpectedP);
  1150. inc(ActualP);
  1151. if ExpectedP^<>ActualP^ then
  1152. begin
  1153. DiffFound;
  1154. exit;
  1155. end;
  1156. if (ExpectedP^ in [#0,#10,#13]) then
  1157. break
  1158. else if (ExpectedP^=Quote) then
  1159. begin
  1160. inc(ExpectedP);
  1161. inc(ActualP);
  1162. break;
  1163. end;
  1164. until false;
  1165. end;
  1166. else
  1167. while ActualP^ in SpaceChars do inc(ActualP);
  1168. if ExpectedP^<>ActualP^ then
  1169. begin
  1170. DiffFound;
  1171. exit;
  1172. end;
  1173. inc(ExpectedP);
  1174. inc(ActualP);
  1175. end;
  1176. until false;
  1177. end;
  1178. { TTestEnginePasResolver }
  1179. procedure TTestEnginePasResolver.SetModule(const AValue: TPasModule);
  1180. begin
  1181. if FModule=AValue then Exit;
  1182. FModule:=AValue;
  1183. end;
  1184. destructor TTestEnginePasResolver.Destroy;
  1185. begin
  1186. FreeAndNil(FStreamResolver);
  1187. FreeAndNil(FParser);
  1188. FreeAndNil(FScanner);
  1189. FreeAndNil(FStreamResolver);
  1190. Module:=nil;
  1191. inherited Destroy;
  1192. end;
  1193. function TTestEnginePasResolver.CreateElement(AClass: TPTreeElement;
  1194. const AName: String; AParent: TPasElement; AVisibility: TPasMemberVisibility;
  1195. const ASrcPos: TPasSourcePos; TypeParams: TFPList): TPasElement;
  1196. begin
  1197. Result:=inherited CreateElement(AClass, AName, AParent, AVisibility, ASrcPos,
  1198. TypeParams);
  1199. if (FModule=nil) and AClass.InheritsFrom(TPasModule) then
  1200. Module:=TPasModule(Result);
  1201. end;
  1202. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1203. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1204. begin
  1205. Result:=nil;
  1206. if InFilename<>'' then
  1207. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1208. if Assigned(OnFindUnit) then
  1209. Result:=OnFindUnit(AName);
  1210. if NameExpr=nil then ;
  1211. end;
  1212. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1213. begin
  1214. // do not parse recursively
  1215. // parse via the queue
  1216. if Section=nil then ;
  1217. end;
  1218. { TCustomTestModule }
  1219. procedure TCustomTestModule.FreeSrcMarkers;
  1220. var
  1221. aMarker, Last: PSrcMarker;
  1222. begin
  1223. aMarker:=FirstSrcMarker;
  1224. while aMarker<>nil do
  1225. begin
  1226. Last:=aMarker;
  1227. aMarker:=aMarker^.Next;
  1228. Dispose(Last);
  1229. end;
  1230. FirstSrcMarker:=nil;
  1231. LastSrcMarker:=nil;
  1232. end;
  1233. function TCustomTestModule.GetResolverCount: integer;
  1234. begin
  1235. Result:=FResolvers.Count;
  1236. end;
  1237. function TCustomTestModule.GetResolvers(Index: integer): TTestEnginePasResolver;
  1238. begin
  1239. Result:=TTestEnginePasResolver(FResolvers[Index]);
  1240. end;
  1241. function TCustomTestModule.GetMsgCount: integer;
  1242. begin
  1243. Result:=FHintMsgs.Count;
  1244. end;
  1245. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1246. begin
  1247. Result:=TTestHintMessage(FHintMsgs[Index]);
  1248. end;
  1249. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1250. ): TPasModule;
  1251. var
  1252. DefNamespace: String;
  1253. begin
  1254. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1255. if (Pos('.',aUnitName)<1) then
  1256. begin
  1257. DefNamespace:=GetDefaultNamespace;
  1258. if DefNamespace<>'' then
  1259. begin
  1260. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1261. if Result<>nil then exit;
  1262. end;
  1263. end;
  1264. Result:=LoadUnit(aUnitName);
  1265. if Result<>nil then exit;
  1266. {$IFDEF VerbosePas2JS}
  1267. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1268. {$ENDIF}
  1269. Fail('can''t find unit "'+aUnitName+'"');
  1270. end;
  1271. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1272. var
  1273. aParser: TPasParser;
  1274. Item: TTestHintMessage;
  1275. begin
  1276. aParser:=Sender as TPasParser;
  1277. Item:=TTestHintMessage.Create;
  1278. Item.Id:=aParser.LastMsgNumber;
  1279. Item.MsgType:=aParser.LastMsgType;
  1280. Item.MsgNumber:=aParser.LastMsgNumber;
  1281. Item.Msg:=Msg;
  1282. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1283. {$IFDEF VerbosePas2JS}
  1284. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1285. {$ENDIF}
  1286. FHintMsgs.Add(Item);
  1287. end;
  1288. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1289. );
  1290. var
  1291. aResolver: TTestEnginePasResolver;
  1292. Item: TTestHintMessage;
  1293. begin
  1294. aResolver:=Sender as TTestEnginePasResolver;
  1295. Item:=TTestHintMessage.Create;
  1296. Item.Id:=aResolver.LastMsgId;
  1297. Item.MsgType:=aResolver.LastMsgType;
  1298. Item.MsgNumber:=aResolver.LastMsgNumber;
  1299. Item.Msg:=Msg;
  1300. Item.SourcePos:=aResolver.LastSourcePos;
  1301. {$IFDEF VerbosePas2JS}
  1302. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1303. {$ENDIF}
  1304. FHintMsgs.Add(Item);
  1305. end;
  1306. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1307. var
  1308. Item: TTestHintMessage;
  1309. aScanner: TPas2jsPasScanner;
  1310. begin
  1311. aScanner:=Sender as TPas2jsPasScanner;
  1312. Item:=TTestHintMessage.Create;
  1313. Item.Id:=aScanner.LastMsgNumber;
  1314. Item.MsgType:=aScanner.LastMsgType;
  1315. Item.MsgNumber:=aScanner.LastMsgNumber;
  1316. Item.Msg:=Msg;
  1317. Item.SourcePos:=aScanner.CurSourcePos;
  1318. {$IFDEF VerbosePas2JS}
  1319. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1320. {$ENDIF}
  1321. FHintMsgs.Add(Item);
  1322. end;
  1323. procedure TCustomTestModule.OnCheckElementParent(El: TPasElement; arg: pointer);
  1324. var
  1325. SubEl: TPasElement;
  1326. i: Integer;
  1327. procedure E(Msg: string);
  1328. var
  1329. s: String;
  1330. begin
  1331. s:='TCustomTestModule.OnCheckElementParent El='+GetTreeDbg(El)+' '+
  1332. ResolverEngine.GetElementSourcePosStr(El)+' '+Msg;
  1333. writeln('ERROR: ',s);
  1334. Fail(s);
  1335. end;
  1336. begin
  1337. if arg=nil then ;
  1338. if El=nil then exit;
  1339. if El.Parent=El then
  1340. E('El.Parent=El='+GetObjName(El));
  1341. if El is TBinaryExpr then
  1342. begin
  1343. if (TBinaryExpr(El).left<>nil) and (TBinaryExpr(El).left.Parent<>El) then
  1344. E('TBinaryExpr(El).left.Parent='+GetObjName(TBinaryExpr(El).left.Parent)+'<>El');
  1345. if (TBinaryExpr(El).right<>nil) and (TBinaryExpr(El).right.Parent<>El) then
  1346. E('TBinaryExpr(El).right.Parent='+GetObjName(TBinaryExpr(El).right.Parent)+'<>El');
  1347. end
  1348. else if El is TParamsExpr then
  1349. begin
  1350. if (TParamsExpr(El).Value<>nil) and (TParamsExpr(El).Value.Parent<>El) then
  1351. E('TParamsExpr(El).Value.Parent='+GetObjName(TParamsExpr(El).Value.Parent)+'<>El');
  1352. for i:=0 to length(TParamsExpr(El).Params)-1 do
  1353. if TParamsExpr(El).Params[i].Parent<>El then
  1354. E('TParamsExpr(El).Params[i].Parent='+GetObjName(TParamsExpr(El).Params[i].Parent)+'<>El');
  1355. end
  1356. else if El is TProcedureExpr then
  1357. begin
  1358. if (TProcedureExpr(El).Proc<>nil) and (TProcedureExpr(El).Proc.Parent<>El) then
  1359. E('TProcedureExpr(El).Proc.Parent='+GetObjName(TProcedureExpr(El).Proc.Parent)+'<>El');
  1360. end
  1361. else if El is TPasDeclarations then
  1362. begin
  1363. for i:=0 to TPasDeclarations(El).Declarations.Count-1 do
  1364. begin
  1365. SubEl:=TPasElement(TPasDeclarations(El).Declarations[i]);
  1366. if SubEl.Parent<>El then
  1367. E('SubEl=TPasElement(TPasDeclarations(El).Declarations[i])='+GetObjName(SubEl)+' SubEl.Parent='+GetObjName(SubEl.Parent)+'<>El');
  1368. end;
  1369. end
  1370. else if El is TPasImplBlock then
  1371. begin
  1372. for i:=0 to TPasImplBlock(El).Elements.Count-1 do
  1373. begin
  1374. SubEl:=TPasElement(TPasImplBlock(El).Elements[i]);
  1375. if SubEl.Parent<>El then
  1376. E('TPasElement(TPasImplBlock(El).Elements[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1377. end;
  1378. end
  1379. else if El is TPasImplWithDo then
  1380. begin
  1381. for i:=0 to TPasImplWithDo(El).Expressions.Count-1 do
  1382. begin
  1383. SubEl:=TPasExpr(TPasImplWithDo(El).Expressions[i]);
  1384. if SubEl.Parent<>El then
  1385. E('TPasExpr(TPasImplWithDo(El).Expressions[i]).Parent='+GetObjName(SubEl.Parent)+'<>El');
  1386. end;
  1387. end
  1388. else if El is TPasProcedure then
  1389. begin
  1390. if TPasProcedure(El).ProcType.Parent<>El then
  1391. E('TPasProcedure(El).ProcType.Parent='+GetObjName(TPasProcedure(El).ProcType.Parent)+'<>El');
  1392. end
  1393. else if El is TPasProcedureType then
  1394. begin
  1395. for i:=0 to TPasProcedureType(El).Args.Count-1 do
  1396. if TPasArgument(TPasProcedureType(El).Args[i]).Parent<>El then
  1397. E('TPasArgument(TPasProcedureType(El).Args[i]).Parent='+GetObjName(TPasArgument(TPasProcedureType(El).Args[i]).Parent)+'<>El');
  1398. end;
  1399. end;
  1400. procedure TCustomTestModule.OnFindReference(El: TPasElement; FindData: pointer);
  1401. var
  1402. Data: PTestResolverReferenceData absolute FindData;
  1403. Line, Col: integer;
  1404. begin
  1405. ResolverEngine.UnmangleSourceLineNumber(El.SourceLinenumber,Line,Col);
  1406. //writeln('TCustomTestModule.OnFindReference ',El.SourceFilename,' Line=',Line,',Col=',Col,' ',GetObjName(El),' SearchFile=',Data^.Filename,',Line=',Data^.Row,',Col=',Data^.StartCol,'-',Data^.EndCol);
  1407. if (Data^.Filename=El.SourceFilename)
  1408. and (Data^.Row=Line)
  1409. and (Data^.StartCol<=Col)
  1410. and (Data^.EndCol>=Col)
  1411. then
  1412. Data^.Found.Add(El);
  1413. end;
  1414. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1415. begin
  1416. if FWithTypeInfo=AValue then Exit;
  1417. FWithTypeInfo:=AValue;
  1418. if AValue then
  1419. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1420. else
  1421. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1422. end;
  1423. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1424. var
  1425. i: Integer;
  1426. CurEngine: TTestEnginePasResolver;
  1427. CurUnitName: String;
  1428. begin
  1429. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1430. Result:=nil;
  1431. if (Module.ClassType=TPasModule)
  1432. and (CompareText(Module.Name,aUnitName)=0) then
  1433. exit(Module);
  1434. for i:=0 to ResolverCount-1 do
  1435. begin
  1436. CurEngine:=Resolvers[i];
  1437. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1438. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1439. if CompareText(aUnitName,CurUnitName)=0 then
  1440. begin
  1441. Result:=CurEngine.Module;
  1442. if Result<>nil then exit;
  1443. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1444. FileResolver.FindSourceFile(aUnitName);
  1445. CurEngine.StreamResolver:=TStreamResolver.Create;
  1446. CurEngine.StreamResolver.OwnsStreams:=True;
  1447. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1448. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1449. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1450. InitScanner(CurEngine.Scanner);
  1451. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1452. CurEngine.Parser.Options:=po_tcmodules;
  1453. if CompareText(CurUnitName,'System')=0 then
  1454. CurEngine.Parser.ImplicitUses.Clear;
  1455. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1456. try
  1457. CurEngine.Parser.NextToken;
  1458. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1459. except
  1460. on E: Exception do
  1461. HandleException(E);
  1462. end;
  1463. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1464. Result:=CurEngine.Module;
  1465. exit;
  1466. end;
  1467. end;
  1468. end;
  1469. procedure TCustomTestModule.SetUp;
  1470. begin
  1471. {$IFDEF EnablePasTreeGlobalRefCount}
  1472. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1473. {$ENDIF}
  1474. if FResolvers<>nil then
  1475. begin
  1476. writeln('TCustomTestModule.SetUp FModules<>nil');
  1477. Halt;
  1478. end;
  1479. inherited SetUp;
  1480. FSkipTests:=false;
  1481. FWithTypeInfo:=false;
  1482. FSource:=TStringList.Create;
  1483. FHub:=TPas2JSResolverHub.Create(Self);
  1484. FResolvers:=TObjectList.Create(true);
  1485. FFilename:='test1.pp';
  1486. FFileResolver:=TStreamResolver.Create;
  1487. FFileResolver.OwnsStreams:=True;
  1488. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1489. InitScanner(FScanner);
  1490. FEngine:=AddModule(Filename);
  1491. FEngine.Scanner:=FScanner;
  1492. FScanner.Resolver:=FEngine;
  1493. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1494. FParser.OnLog:=@OnParserLog;
  1495. FEngine.Parser:=FParser;
  1496. Parser.Options:=po_tcmodules;
  1497. FModule:=Nil;
  1498. FConverter:=CreateConverter;
  1499. FExpectedErrorClass:=nil;
  1500. end;
  1501. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1502. var
  1503. Options: TPasToJsConverterOptions;
  1504. begin
  1505. Result:=TPasToJSConverter.Create;
  1506. Options:=co_tcmodules;
  1507. if WithTypeInfo then
  1508. Exclude(Options,coNoTypeInfo)
  1509. else
  1510. Include(Options,coNoTypeInfo);
  1511. Result.Options:=Options;
  1512. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1513. end;
  1514. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1515. begin
  1516. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1517. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1518. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1519. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1520. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1521. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1522. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1523. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1524. aScanner.OnLog:=@OnScannerLog;
  1525. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1526. end;
  1527. procedure TCustomTestModule.TearDown;
  1528. {$IFDEF CheckPasTreeRefCount}
  1529. var
  1530. El: TPasElement;
  1531. {$ENDIF}
  1532. var
  1533. i: Integer;
  1534. CurModule: TPasModule;
  1535. begin
  1536. FreeSrcMarkers;
  1537. FHintMsgs.Clear;
  1538. FHintMsgsGood.Clear;
  1539. FSkipTests:=false;
  1540. FWithTypeInfo:=false;
  1541. FJSRegModuleCall:=nil;
  1542. FJSModuleCallArgs:=nil;
  1543. FJSImplentationUses:=nil;
  1544. FJSInterfaceUses:=nil;
  1545. FJSModuleSrc:=nil;
  1546. FJSInitBody:=nil;
  1547. FreeAndNil(FJSSource);
  1548. FreeAndNil(FJSModule);
  1549. FreeAndNil(FConverter);
  1550. ResolverEngine.Clear;
  1551. FreeAndNil(FSource);
  1552. FreeAndNil(FFileResolver);
  1553. if FResolvers<>nil then
  1554. begin
  1555. for i:=0 to FResolvers.Count-1 do
  1556. begin
  1557. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1558. if CurModule=nil then continue;
  1559. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1560. end;
  1561. for i:=0 to FResolvers.Count-1 do
  1562. begin
  1563. CurModule:=TTestEnginePasResolver(FResolvers[i]).Module;
  1564. if CurModule=nil then continue;
  1565. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1566. end;
  1567. FreeAndNil(FResolvers);
  1568. FModule:=nil;
  1569. FEngine:=nil;
  1570. end;
  1571. FreeAndNil(FHub);
  1572. inherited TearDown;
  1573. {$IFDEF EnablePasTreeGlobalRefCount}
  1574. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1575. begin
  1576. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1577. {$IFDEF CheckPasTreeRefCount}
  1578. El:=TPasElement.FirstRefEl;
  1579. while El<>nil do
  1580. begin
  1581. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1582. for i:=0 to El.RefIds.Count-1 do
  1583. writeln(' ',El.RefIds[i]);
  1584. El:=El.NextRefEl;
  1585. end;
  1586. {$ENDIF}
  1587. Halt;
  1588. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1589. end;
  1590. {$ENDIF}
  1591. end;
  1592. procedure TCustomTestModule.Add(Line: string);
  1593. begin
  1594. Source.Add(Line);
  1595. end;
  1596. procedure TCustomTestModule.Add(const Lines: array of string);
  1597. var
  1598. i: Integer;
  1599. begin
  1600. for i:=low(Lines) to high(Lines) do
  1601. Add(Lines[i]);
  1602. end;
  1603. procedure TCustomTestModule.StartParsing;
  1604. var
  1605. Src: String;
  1606. begin
  1607. Src:=Source.Text;
  1608. FEngine.Source:=Src;
  1609. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1610. Scanner.OpenFile(FileName);
  1611. Writeln('// Test : ',Self.TestName);
  1612. Writeln(Src);
  1613. end;
  1614. procedure TCustomTestModule.ParseModuleQueue;
  1615. var
  1616. i: Integer;
  1617. CurResolver: TTestEnginePasResolver;
  1618. Found: Boolean;
  1619. Section: TPasSection;
  1620. begin
  1621. // parse til exception or all Resolvers finished
  1622. while not SkipTests do
  1623. begin
  1624. Found:=false;
  1625. for i:=0 to ResolverCount-1 do
  1626. begin
  1627. CurResolver:=Resolvers[i];
  1628. if CurResolver.CurrentParser=nil then continue;
  1629. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1630. continue;
  1631. CurResolver.Parser.ParseContinue;
  1632. Found:=true;
  1633. break;
  1634. end;
  1635. if not Found then break;
  1636. end;
  1637. for i:=0 to ResolverCount-1 do
  1638. begin
  1639. CurResolver:=Resolvers[i];
  1640. if CurResolver.Parser=nil then
  1641. begin
  1642. if CurResolver.CurrentParser<>nil then
  1643. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1644. continue;
  1645. end;
  1646. if CurResolver.Parser.CurModule<>nil then
  1647. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1648. end;
  1649. end;
  1650. procedure TCustomTestModule.ParseModule;
  1651. begin
  1652. if SkipTests then exit;
  1653. FFirstPasStatement:=nil;
  1654. try
  1655. StartParsing;
  1656. Parser.ParseMain(FModule);
  1657. ParseModuleQueue;
  1658. except
  1659. on E: Exception do
  1660. HandleException(E);
  1661. end;
  1662. if SkipTests then exit;
  1663. AssertNotNull('Module resulted in Module',Module);
  1664. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1665. TAssert.AssertSame('Has resolver',ResolverEngine,Parser.Engine);
  1666. end;
  1667. procedure TCustomTestModule.ParseProgram;
  1668. begin
  1669. if SkipTests then exit;
  1670. ParseModule;
  1671. if SkipTests then exit;
  1672. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1673. FPasProgram:=TPasProgram(Module);
  1674. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1675. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1676. if (PasProgram.InitializationSection.Elements.Count>0) then
  1677. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1678. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1679. end;
  1680. procedure TCustomTestModule.ParseLibrary;
  1681. var
  1682. Init: TInitializationSection;
  1683. begin
  1684. if SkipTests then exit;
  1685. ParseModule;
  1686. if SkipTests then exit;
  1687. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1688. FPasLibrary:=TPasLibrary(Module);
  1689. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1690. Init:=PasLibrary.InitializationSection;
  1691. if (Init<>nil) and (Init.Elements.Count>0) then
  1692. if TObject(Init.Elements[0]) is TPasImplBlock then
  1693. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1694. end;
  1695. procedure TCustomTestModule.ParseUnit;
  1696. begin
  1697. if SkipTests then exit;
  1698. ParseModule;
  1699. if SkipTests then exit;
  1700. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1701. AssertNotNull('Has interface section',Module.InterfaceSection);
  1702. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1703. if (Module.InitializationSection<>nil)
  1704. and (Module.InitializationSection.Elements.Count>0)
  1705. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1706. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1707. end;
  1708. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1709. ): TTestEnginePasResolver;
  1710. var
  1711. i: Integer;
  1712. begin
  1713. for i:=0 to ResolverCount-1 do
  1714. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1715. exit(Resolvers[i]);
  1716. Result:=nil;
  1717. end;
  1718. function TCustomTestModule.AddModule(aFilename: string
  1719. ): TTestEnginePasResolver;
  1720. begin
  1721. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1722. if FindModuleWithFilename(aFilename)<>nil then
  1723. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1724. Result:=TTestEnginePasResolver.Create;
  1725. Result.Filename:=aFilename;
  1726. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1727. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1728. Result.OnLog:=@OnPasResolverLog;
  1729. Result.Hub:=Hub;
  1730. FResolvers.Add(Result);
  1731. end;
  1732. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1733. ): TTestEnginePasResolver;
  1734. begin
  1735. Result:=AddModule(aFilename);
  1736. Result.Source:=Src;
  1737. end;
  1738. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1739. ImplementationSrc: string): TTestEnginePasResolver;
  1740. var
  1741. Src: String;
  1742. begin
  1743. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1744. Src+=LineEnding;
  1745. Src+='interface'+LineEnding;
  1746. Src+=LineEnding;
  1747. Src+=InterfaceSrc;
  1748. Src+='implementation'+LineEnding;
  1749. Src+=LineEnding;
  1750. Src+=ImplementationSrc;
  1751. Src+='end.'+LineEnding;
  1752. Result:=AddModuleWithSrc(aFilename,Src);
  1753. end;
  1754. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1755. var
  1756. Intf, Impl: TStringList;
  1757. begin
  1758. Intf:=TStringList.Create;
  1759. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1760. // unit interface
  1761. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1762. Intf.Add('{$modeswitch externalclass}');
  1763. Intf.Add('type');
  1764. Intf.Add(' integer=longint;');
  1765. Intf.Add(' sizeint=nativeint;');
  1766. //'const',
  1767. //' LineEnding = #10;',
  1768. //' DirectorySeparator = ''/'';',
  1769. //' DriveSeparator = '''';',
  1770. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1771. //' AllowDriveSeparators : set of char = [];',
  1772. if supTObject in Parts then
  1773. Intf.AddStrings([
  1774. 'type',
  1775. ' TClass = class of TObject;',
  1776. ' TObject = class',
  1777. ' constructor Create;',
  1778. ' destructor Destroy; virtual;',
  1779. ' class function ClassType: TClass; assembler;',
  1780. ' class function ClassName: String; assembler;',
  1781. ' class function ClassNameIs(const Name: string): boolean;',
  1782. ' class function ClassParent: TClass; assembler;',
  1783. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1784. ' class function UnitName: String; assembler;',
  1785. ' procedure AfterConstruction; virtual;',
  1786. ' procedure BeforeDestruction;virtual;',
  1787. ' function Equals(Obj: TObject): boolean; virtual;',
  1788. ' function ToString: String; virtual;',
  1789. ' end;']);
  1790. if supTInterfacedObject in Parts then
  1791. Intf.AddStrings([
  1792. ' {$Interfaces COM}',
  1793. ' IUnknown = interface',
  1794. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1795. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1796. ' function _AddRef: Integer;',
  1797. ' function _Release: Integer;',
  1798. ' end;',
  1799. ' IInterface = IUnknown;',
  1800. ' TInterfacedObject = class(TObject,IUnknown)',
  1801. ' protected',
  1802. ' fRefCount: Integer;',
  1803. ' { implement methods of IUnknown }',
  1804. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1805. ' function _AddRef: Integer; virtual;',
  1806. ' function _Release: Integer; virtual;',
  1807. ' end;',
  1808. ' TInterfacedClass = class of TInterfacedObject;',
  1809. '',
  1810. '']);
  1811. if supTVarRec in Parts then
  1812. Intf.AddStrings([
  1813. 'const',
  1814. ' vtInteger = 0;',
  1815. ' vtBoolean = 1;',
  1816. ' vtJSValue = 19;',
  1817. 'type',
  1818. ' PVarRec = ^TVarRec;',
  1819. ' TVarRec = record',
  1820. ' VType : byte;',
  1821. ' VJSValue: JSValue;',
  1822. ' vInteger: longint external name ''VJSValue'';',
  1823. ' vBoolean: boolean external name ''VJSValue'';',
  1824. ' end;',
  1825. ' TVarRecArray = array of TVarRec;',
  1826. 'function VarRecs: TVarRecArray; varargs;',
  1827. '']);
  1828. if supTypeInfo in Parts then
  1829. begin
  1830. Intf.AddStrings([
  1831. 'type',
  1832. ' TTypeKind = (',
  1833. ' tkUnknown, // 0',
  1834. ' tkInteger, // 1',
  1835. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1836. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1837. ' tkEnumeration, // 4',
  1838. ' tkSet, // 5',
  1839. ' tkDouble, // 6',
  1840. ' tkBool, // 7',
  1841. ' tkProcVar, // 8 function or procedure',
  1842. ' tkMethod, // 9 proc var of object',
  1843. ' tkArray, // 10 static array',
  1844. ' tkDynArray, // 11',
  1845. ' tkRecord, // 12',
  1846. ' tkClass, // 13',
  1847. ' tkClassRef, // 14',
  1848. ' tkPointer, // 15',
  1849. ' tkJSValue, // 16',
  1850. ' tkRefToProcVar, // 17 variable of procedure type',
  1851. ' tkInterface, // 18',
  1852. ' //tkObject,',
  1853. ' //tkSString,tkLString,tkAString,tkWString,',
  1854. ' //tkVariant,',
  1855. ' //tkWChar,',
  1856. ' //tkInt64,',
  1857. ' //tkQWord,',
  1858. ' //tkInterfaceRaw,',
  1859. ' //tkUString,tkUChar,',
  1860. ' tkHelper, // 19',
  1861. ' //tkFile,',
  1862. ' tkExtClass // 20',
  1863. ' );',
  1864. ' TTypeKinds = set of TTypeKind;',
  1865. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1866. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1867. ' end;',
  1868. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1869. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1870. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1871. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1872. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1873. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1874. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1875. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1876. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1877. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1878. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1879. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1880. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1881. '']);
  1882. end;
  1883. if supWriteln in Parts then
  1884. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1885. Intf.Add('var');
  1886. Intf.Add(' ExitCode: Longint = 0;');
  1887. // unit implementation
  1888. Impl:=TStringList.Create;
  1889. if supTObject in Parts then
  1890. Impl.AddStrings([
  1891. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1892. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1893. 'asm',
  1894. 'end;',
  1895. 'constructor TObject.Create; begin end;',
  1896. 'destructor TObject.Destroy; begin end;',
  1897. 'class function TObject.ClassType: TClass; assembler;',
  1898. 'asm',
  1899. 'end;',
  1900. 'class function TObject.ClassName: String; assembler;',
  1901. 'asm',
  1902. 'end;',
  1903. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1904. 'begin',
  1905. ' Result:=SameText(Name,ClassName);',
  1906. 'end;',
  1907. 'class function TObject.ClassParent: TClass; assembler;',
  1908. 'asm',
  1909. 'end;',
  1910. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1911. 'asm',
  1912. 'end;',
  1913. 'class function TObject.UnitName: String; assembler;',
  1914. 'asm',
  1915. 'end;',
  1916. 'procedure TObject.AfterConstruction; begin end;',
  1917. 'procedure TObject.BeforeDestruction; begin end;',
  1918. 'function TObject.Equals(Obj: TObject): boolean;',
  1919. 'begin',
  1920. ' Result:=Obj=Self;',
  1921. 'end;',
  1922. 'function TObject.ToString: String;',
  1923. 'begin',
  1924. ' Result:=ClassName;',
  1925. 'end;'
  1926. ]);
  1927. if supTInterfacedObject in Parts then
  1928. Impl.AddStrings([
  1929. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1930. //'begin',
  1931. //'end;',
  1932. 'function TInterfacedObject._AddRef: Integer;',
  1933. 'begin',
  1934. 'end;',
  1935. 'function TInterfacedObject._Release: Integer;',
  1936. 'begin',
  1937. 'end;',
  1938. '']);
  1939. if supTVarRec in Parts then
  1940. Impl.AddStrings([
  1941. 'function VarRecs: TVarRecArray; varargs;',
  1942. 'var',
  1943. ' v: PVarRec;',
  1944. 'begin',
  1945. ' v^.VType:=1;',
  1946. ' v^.VJSValue:=2;',
  1947. 'end;',
  1948. '']);
  1949. try
  1950. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1951. finally
  1952. Intf.Free;
  1953. Impl.Free;
  1954. end;
  1955. end;
  1956. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1957. SystemUnitParts: TSystemUnitParts);
  1958. begin
  1959. if NeedSystemUnit then
  1960. AddSystemUnit(SystemUnitParts)
  1961. else
  1962. Parser.ImplicitUses.Clear;
  1963. Add('program '+ExtractFileUnitName(Filename)+';');
  1964. Add('');
  1965. end;
  1966. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1967. SystemUnitParts: TSystemUnitParts);
  1968. begin
  1969. if NeedSystemUnit then
  1970. AddSystemUnit(SystemUnitParts)
  1971. else
  1972. Parser.ImplicitUses.Clear;
  1973. Add('library '+ExtractFileUnitName(Filename)+';');
  1974. Add('');
  1975. end;
  1976. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1977. SystemUnitParts: TSystemUnitParts);
  1978. begin
  1979. if NeedSystemUnit then
  1980. AddSystemUnit(SystemUnitParts)
  1981. else
  1982. Parser.ImplicitUses.Clear;
  1983. Add('unit Test1;');
  1984. Add('');
  1985. end;
  1986. procedure TCustomTestModule.ConvertModule;
  1987. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1988. out UsesLit: TJSArrayLiteral);
  1989. var
  1990. i: Integer;
  1991. Item: TJSElement;
  1992. Lit: TJSLiteral;
  1993. begin
  1994. UsesLit:=nil;
  1995. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1996. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1997. exit; // null is ok
  1998. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1999. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  2000. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  2001. begin
  2002. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  2003. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  2004. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  2005. Lit:=TJSLiteral(Item);
  2006. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  2007. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  2008. end;
  2009. end;
  2010. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  2011. out Src: TJSSourceElements);
  2012. var
  2013. FunDecl: TJSFunctionDeclarationStatement;
  2014. FunDef: TJSFuncDef;
  2015. FunBody: TJSFunctionBody;
  2016. begin
  2017. Src:=nil;
  2018. AssertNotNull(ParamName,Arg.Expr);
  2019. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  2020. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  2021. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  2022. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  2023. FunDef:=FunDecl.AFunction as TJSFuncDef;
  2024. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  2025. AssertNotNull(ParamName+' body',FunDef.Body);
  2026. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  2027. FunBody:=FunDef.Body as TJSFunctionBody;
  2028. AssertNotNull(ParamName+' body.A',FunBody.A);
  2029. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  2030. Src:=FunBody.A as TJSSourceElements;
  2031. end;
  2032. var
  2033. ModuleNameExpr: TJSLiteral;
  2034. InitFunction: TJSFunctionDeclarationStatement;
  2035. InitAssign: TJSSimpleAssignStatement;
  2036. InitName: String;
  2037. LastNode, FirstNode: TJSElement;
  2038. Arg: TJSArrayLiteralElement;
  2039. IsProg, IsLib: Boolean;
  2040. begin
  2041. if SkipTests then exit;
  2042. IsProg:=false;
  2043. IsLib:=false;
  2044. if Module is TPasProgram then
  2045. IsProg:=true
  2046. else if Module is TPasLibrary then
  2047. IsLib:=true;
  2048. try
  2049. FJSModule:=FConverter.ConvertPasElement(Module,ResolverEngine) as TJSSourceElements;
  2050. except
  2051. on E: Exception do
  2052. HandleException(E);
  2053. end;
  2054. if SkipTests then exit;
  2055. if ExpectedErrorClass<>nil then
  2056. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  2057. FJSSource:=TStringList.Create;
  2058. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  2059. {$IFDEF VerbosePas2JS}
  2060. writeln('TTestModule.ConvertModule JS:');
  2061. write(FJSSource.Text);
  2062. {$ENDIF}
  2063. // rtl.module(...
  2064. if JSModule.Statements.Count<1 then
  2065. AssertEquals('jsmodule has at least one statement - the call',1,JSModule.Statements.Count);
  2066. FirstNode:=JSModule.Statements.Nodes[0].Node;
  2067. AssertNotNull('register module call',FirstNode);
  2068. AssertEquals('register module call',TJSCallExpression,FirstNode.ClassType);
  2069. FJSRegModuleCall:=FirstNode as TJSCallExpression;
  2070. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  2071. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  2072. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  2073. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  2074. // parameter 'unitname'
  2075. if JSModuleCallArgs.Elements.Count<1 then
  2076. Fail('rtl.module first param unit missing');
  2077. Arg:=JSModuleCallArgs.Elements.Elements[0];
  2078. AssertNotNull('module name param',Arg.Expr);
  2079. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  2080. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  2081. if IsProg then
  2082. begin
  2083. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString));
  2084. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2085. end
  2086. else if IsLib then
  2087. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  2088. else
  2089. begin
  2090. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  2091. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  2092. end;
  2093. // main uses section
  2094. if JSModuleCallArgs.Elements.Count<2 then
  2095. Fail('rtl.module second param main uses missing');
  2096. Arg:=JSModuleCallArgs.Elements.Elements[1];
  2097. CheckUsesList('interface',Arg,FJSInterfaceUses);
  2098. // program/library/interface function()
  2099. if JSModuleCallArgs.Elements.Count<3 then
  2100. Fail('rtl.module third param intf-function missing');
  2101. Arg:=JSModuleCallArgs.Elements.Elements[2];
  2102. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  2103. // search for $mod.$init or $mod.$main - the last statement
  2104. if IsProg or IsLib then
  2105. begin
  2106. InitName:='$main';
  2107. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  2108. end
  2109. else
  2110. InitName:='$init';
  2111. InitAssign:=nil;
  2112. InitFunction:=nil;
  2113. FJSInitBody:=nil;
  2114. if JSModuleSrc.Statements.Count>0 then
  2115. begin
  2116. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  2117. if LastNode is TJSSimpleAssignStatement then
  2118. begin
  2119. InitAssign:=LastNode as TJSSimpleAssignStatement;
  2120. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  2121. begin
  2122. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  2123. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  2124. end
  2125. else if IsProg or IsLib then
  2126. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  2127. end;
  2128. end;
  2129. // optional: implementation uses section
  2130. if JSModuleCallArgs.Elements.Count<4 then
  2131. exit;
  2132. Arg:=JSModuleCallArgs.Elements.Elements[3];
  2133. CheckUsesList('implementation',Arg,FJSImplentationUses);
  2134. end;
  2135. procedure TCustomTestModule.ConvertProgram;
  2136. begin
  2137. Add('end.');
  2138. ParseProgram;
  2139. ConvertModule;
  2140. end;
  2141. procedure TCustomTestModule.ConvertLibrary;
  2142. begin
  2143. Add('end.');
  2144. ParseLibrary;
  2145. ConvertModule;
  2146. end;
  2147. procedure TCustomTestModule.ConvertUnit;
  2148. begin
  2149. Add('end.');
  2150. ParseUnit;
  2151. ConvertModule;
  2152. end;
  2153. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  2154. begin
  2155. Result:=tcmodules.JSToStr(El);
  2156. end;
  2157. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  2158. DottedName: string);
  2159. begin
  2160. if DottedName='' then
  2161. begin
  2162. AssertNull(Msg,El);
  2163. end
  2164. else
  2165. begin
  2166. AssertNotNull(Msg,El);
  2167. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  2168. end;
  2169. end;
  2170. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  2171. begin
  2172. if El=nil then
  2173. Result:=''
  2174. else if El is TJSPrimaryExpressionIdent then
  2175. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  2176. else if El is TJSDotMemberExpression then
  2177. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  2178. else
  2179. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2180. end;
  2181. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2182. InitStatements: string; ImplStatements: string);
  2183. var
  2184. ActualSrc, ExpectedSrc, InitName: String;
  2185. IsProg, IsLib: Boolean;
  2186. begin
  2187. ActualSrc:=JSToStr(JSModuleSrc);
  2188. if coUseStrict in Converter.Options then
  2189. ExpectedSrc:='"use strict";'+LineEnding
  2190. else
  2191. ExpectedSrc:='';
  2192. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2193. ExpectedSrc:=ExpectedSrc+Statements;
  2194. // unit implementation
  2195. if (Trim(ImplStatements)<>'') then
  2196. ExpectedSrc:=ExpectedSrc+LineEnding
  2197. +'$mod.$implcode = function () {'+LineEnding
  2198. +ImplStatements
  2199. +'};'+LineEnding;
  2200. // program main or unit initialization
  2201. IsProg:=false;
  2202. IsLib:=false;
  2203. if Module is TPasProgram then
  2204. IsProg:=true
  2205. else if Module is TPasLibrary then
  2206. IsLib:=true;
  2207. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2208. begin
  2209. if IsProg or IsLib then
  2210. InitName:='$main'
  2211. else
  2212. InitName:='$init';
  2213. ExpectedSrc:=ExpectedSrc+LineEnding
  2214. +'$mod.'+InitName+' = function () {'+LineEnding
  2215. +InitStatements
  2216. +'};'+LineEnding;
  2217. end;
  2218. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2219. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2220. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2221. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2222. end;
  2223. procedure TCustomTestModule.CheckFullSource(Msg, ExpectedSrc: String);
  2224. var
  2225. ActualSrc: String;
  2226. begin
  2227. ActualSrc:=JSToStr(JSModule);
  2228. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2229. end;
  2230. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2231. // search diff, ignore changes in spaces
  2232. var
  2233. s: string;
  2234. begin
  2235. if CheckSrcDiff(Expected,Actual,s) then exit;
  2236. Fail(Msg+': '+s);
  2237. end;
  2238. procedure TCustomTestModule.CheckUnit(aFilename, ExpectedSrc: string);
  2239. var
  2240. aResolver: TTestEnginePasResolver;
  2241. aConverter: TPasToJSConverter;
  2242. aJSModule: TJSSourceElements;
  2243. ActualSrc: String;
  2244. begin
  2245. aResolver:=GetResolver(aFilename);
  2246. AssertNotNull('missing resolver of unit '+aFilename,aResolver);
  2247. AssertNotNull('missing resolver.module of unit '+aFilename,aResolver.Module);
  2248. {$IFDEF VerbosePas2JS}
  2249. writeln('CheckUnit '+aFilename+' converting ...');
  2250. {$ENDIF}
  2251. aConverter:=CreateConverter;
  2252. aJSModule:=nil;
  2253. try
  2254. try
  2255. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2256. except
  2257. on E: Exception do
  2258. HandleException(E);
  2259. end;
  2260. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2261. {$IFDEF VerbosePas2JS}
  2262. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2263. write(aResolver.Source);
  2264. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2265. write(ActualSrc);
  2266. {$ENDIF}
  2267. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2268. finally
  2269. aJSModule.Free;
  2270. aConverter.Free;
  2271. end;
  2272. end;
  2273. procedure TCustomTestModule.CheckReferenceDirectives;
  2274. var
  2275. CurFilename: string;
  2276. LineNumber: Integer;
  2277. SrcLine: String;
  2278. CommentStartP, CommentEndP: PChar;
  2279. procedure RaiseError(Msg: string; p: PChar);
  2280. begin
  2281. RaiseErrorAtSrc(Msg,CurFilename,LineNumber,p-PChar(SrcLine)+1);
  2282. end;
  2283. procedure AddMarker(Marker: PSrcMarker);
  2284. begin
  2285. if LastSrcMarker<>nil then
  2286. LastSrcMarker^.Next:=Marker
  2287. else
  2288. FirstSrcMarker:=Marker;
  2289. LastSrcMarker:=Marker;
  2290. end;
  2291. function AddMarker(Kind: TSrcMarkerKind; const aFilename: string;
  2292. aLine, aStartCol, aEndCol: integer; const Identifier: string): PSrcMarker;
  2293. begin
  2294. New(Result);
  2295. Result^.Kind:=Kind;
  2296. Result^.Filename:=aFilename;
  2297. Result^.Row:=aLine;
  2298. Result^.StartCol:=aStartCol;
  2299. Result^.EndCol:=aEndCol;
  2300. Result^.Identifier:=Identifier;
  2301. Result^.Next:=nil;
  2302. //writeln('AddMarker Line="',SrcLine,'" Identifier=',Identifier,' Col=',aStartCol,'-',aEndCol,' "',copy(SrcLine,aStartCol,aEndCol-aStartCol),'"');
  2303. AddMarker(Result);
  2304. end;
  2305. function AddMarkerForTokenBehindComment(Kind: TSrcMarkerKind;
  2306. const Identifier: string): PSrcMarker;
  2307. var
  2308. TokenStart, p: PChar;
  2309. begin
  2310. p:=CommentEndP;
  2311. ReadNextPascalToken(p,TokenStart,false,false);
  2312. Result:=AddMarker(Kind,CurFilename,LineNumber,
  2313. CommentEndP-PChar(SrcLine)+1,p-PChar(SrcLine)+1,Identifier);
  2314. end;
  2315. function ReadIdentifier(var p: PChar): string;
  2316. var
  2317. StartP: PChar;
  2318. begin
  2319. if not (p^ in ['a'..'z','A'..'Z','_']) then
  2320. RaiseError('identifier expected',p);
  2321. StartP:=p;
  2322. inc(p);
  2323. while p^ in ['a'..'z','A'..'Z','_','0'..'9'] do inc(p);
  2324. Result:='';
  2325. SetLength(Result,p-StartP);
  2326. Move(StartP^,Result[1],length(Result));
  2327. end;
  2328. procedure AddLabel;
  2329. var
  2330. Identifier: String;
  2331. p: PChar;
  2332. begin
  2333. p:=CommentStartP+2;
  2334. Identifier:=ReadIdentifier(p);
  2335. //writeln('TCustomTestModule.CheckReferenceDirectives.AddLabel ',Identifier);
  2336. if FindSrcLabel(Identifier)<>nil then
  2337. RaiseError('duplicate label "'+Identifier+'"',p);
  2338. AddMarkerForTokenBehindComment(mkLabel,Identifier);
  2339. end;
  2340. procedure AddResolverReference;
  2341. var
  2342. Identifier: String;
  2343. p: PChar;
  2344. begin
  2345. p:=CommentStartP+2;
  2346. Identifier:=ReadIdentifier(p);
  2347. //writeln('TCustomTestModule.CheckReferenceDirectives.AddReference ',Identifier);
  2348. AddMarkerForTokenBehindComment(mkResolverReference,Identifier);
  2349. end;
  2350. procedure AddDirectReference;
  2351. var
  2352. Identifier: String;
  2353. p: PChar;
  2354. begin
  2355. p:=CommentStartP+2;
  2356. Identifier:=ReadIdentifier(p);
  2357. //writeln('TCustomTestModule.CheckReferenceDirectives.AddDirectReference ',Identifier);
  2358. AddMarkerForTokenBehindComment(mkDirectReference,Identifier);
  2359. end;
  2360. procedure ParseCode(SrcLines: TStringList; aFilename: string);
  2361. var
  2362. p: PChar;
  2363. IsDirective: Boolean;
  2364. begin
  2365. //writeln('TCustomTestModule.CheckReferenceDirectives.ParseCode File=',aFilename);
  2366. CurFilename:=aFilename;
  2367. // parse code, find all labels
  2368. LineNumber:=0;
  2369. while LineNumber<SrcLines.Count do
  2370. begin
  2371. inc(LineNumber);
  2372. SrcLine:=SrcLines[LineNumber-1];
  2373. if SrcLine='' then continue;
  2374. //writeln('TCustomTestModule.CheckReferenceDirectives Line=',SrcLine);
  2375. p:=PChar(SrcLine);
  2376. repeat
  2377. case p^ of
  2378. #0: if (p-PChar(SrcLine)=length(SrcLine)) then break;
  2379. '{':
  2380. begin
  2381. CommentStartP:=p;
  2382. inc(p);
  2383. IsDirective:=p^ in ['#','@','='];
  2384. // skip to end of comment
  2385. repeat
  2386. case p^ of
  2387. #0:
  2388. if (p-PChar(SrcLine)=length(SrcLine)) then
  2389. begin
  2390. // multi line comment
  2391. if IsDirective then
  2392. RaiseError('directive missing closing bracket',CommentStartP);
  2393. repeat
  2394. inc(LineNumber);
  2395. if LineNumber>SrcLines.Count then exit;
  2396. SrcLine:=SrcLines[LineNumber-1];
  2397. //writeln('TCustomTestModule.CheckReferenceDirectives Comment Line=',SrcLine);
  2398. until SrcLine<>'';
  2399. p:=PChar(SrcLine);
  2400. continue;
  2401. end;
  2402. '}':
  2403. begin
  2404. inc(p);
  2405. break;
  2406. end;
  2407. end;
  2408. inc(p);
  2409. until false;
  2410. CommentEndP:=p;
  2411. case CommentStartP[1] of
  2412. '#': AddLabel;
  2413. '@': AddResolverReference;
  2414. '=': AddDirectReference;
  2415. end;
  2416. p:=CommentEndP;
  2417. continue;
  2418. end;
  2419. '/':
  2420. if p[1]='/' then
  2421. break; // rest of line is comment -> skip
  2422. end;
  2423. inc(p);
  2424. until false;
  2425. end;
  2426. end;
  2427. procedure CheckResolverReference(aMarker: PSrcMarker);
  2428. // check if one element at {@a} has a TResolvedReference to an element labeled {#a}
  2429. var
  2430. aLabel: PSrcMarker;
  2431. ReferenceElements, LabelElements: TFPList;
  2432. i, j, aLine, aCol: Integer;
  2433. El, Ref, LabelEl: TPasElement;
  2434. begin
  2435. //writeln('TCustomTestModule.CheckResolverReference searching reference: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2436. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2437. if aLabel=nil then
  2438. RaiseErrorAtSrc('label "'+aMarker^.Identifier+'" not found',aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2439. LabelElements:=nil;
  2440. ReferenceElements:=nil;
  2441. try
  2442. LabelElements:=FindElementsAt(aLabel);
  2443. ReferenceElements:=FindElementsAt(aMarker);
  2444. for i:=0 to ReferenceElements.Count-1 do
  2445. begin
  2446. El:=TPasElement(ReferenceElements[i]);
  2447. Ref:=nil;
  2448. if El.CustomData is TResolvedReference then
  2449. Ref:=TResolvedReference(El.CustomData).Declaration
  2450. else if El.CustomData is TPasPropertyScope then
  2451. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2452. else if El.CustomData is TPasSpecializeTypeData then
  2453. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2454. if Ref<>nil then
  2455. for j:=0 to LabelElements.Count-1 do
  2456. begin
  2457. LabelEl:=TPasElement(LabelElements[j]);
  2458. if Ref=LabelEl then
  2459. exit; // success
  2460. end;
  2461. end;
  2462. // failure write candidates
  2463. for i:=0 to ReferenceElements.Count-1 do
  2464. begin
  2465. El:=TPasElement(ReferenceElements[i]);
  2466. write('Reference candidate for "',aMarker^.Identifier,'" at reference ',aMarker^.Filename,'(',aMarker^.Row,',',aMarker^.StartCol,'-',aMarker^.EndCol,')');
  2467. write(' El=',GetObjName(El));
  2468. if EL is TPrimitiveExpr then
  2469. begin
  2470. writeln('TCustomTestModule.CheckResolverReference ',TPrimitiveExpr(El).Value);
  2471. end;
  2472. Ref:=nil;
  2473. if El.CustomData is TResolvedReference then
  2474. Ref:=TResolvedReference(El.CustomData).Declaration
  2475. else if El.CustomData is TPasPropertyScope then
  2476. Ref:=TPasPropertyScope(El.CustomData).AncestorProp
  2477. else if El.CustomData is TPasSpecializeTypeData then
  2478. Ref:=TPasSpecializeTypeData(El.CustomData).SpecializedType;
  2479. if Ref<>nil then
  2480. begin
  2481. write(' Decl=',GetObjName(Ref));
  2482. ResolverEngine.UnmangleSourceLineNumber(Ref.SourceLinenumber,aLine,aCol);
  2483. write(',',Ref.SourceFilename,'(',aLine,',',aCol,')');
  2484. end
  2485. else
  2486. write(' has no TResolvedReference. El.CustomData=',GetObjName(El.CustomData));
  2487. writeln;
  2488. end;
  2489. for i:=0 to LabelElements.Count-1 do
  2490. begin
  2491. El:=TPasElement(LabelElements[i]);
  2492. write('Label candidate for "',aLabel^.Identifier,'" at reference ',aLabel^.Filename,'(',aLabel^.Row,',',aLabel^.StartCol,'-',aLabel^.EndCol,')');
  2493. write(' El=',GetObjName(El));
  2494. writeln;
  2495. end;
  2496. RaiseErrorAtSrcMarker('wrong resolved reference "'+aMarker^.Identifier+'"',aMarker);
  2497. finally
  2498. LabelElements.Free;
  2499. ReferenceElements.Free;
  2500. end;
  2501. end;
  2502. procedure CheckDirectReference(aMarker: PSrcMarker);
  2503. // check if one element at {=a} is a TPasAliasType pointing to an element labeled {#a}
  2504. var
  2505. aLabel: PSrcMarker;
  2506. ReferenceElements, LabelElements: TFPList;
  2507. i, LabelLine, LabelCol, j: Integer;
  2508. El, LabelEl: TPasElement;
  2509. DeclEl, TypeEl: TPasType;
  2510. begin
  2511. //writeln('CheckDirectReference searching pointer: ',aMarker^.Filename,' Line=',aMarker^.Row,' Col=',aMarker^.StartCol,'-',aMarker^.EndCol,' Label="',aMarker^.Identifier,'"');
  2512. aLabel:=FindSrcLabel(aMarker^.Identifier);
  2513. if aLabel=nil then
  2514. RaiseErrorAtSrcMarker('label "'+aMarker^.Identifier+'" not found',aMarker);
  2515. LabelElements:=nil;
  2516. ReferenceElements:=nil;
  2517. try
  2518. //writeln('CheckDirectReference finding elements at label ...');
  2519. LabelElements:=FindElementsAt(aLabel);
  2520. //writeln('CheckDirectReference finding elements at reference ...');
  2521. ReferenceElements:=FindElementsAt(aMarker);
  2522. for i:=0 to ReferenceElements.Count-1 do
  2523. begin
  2524. El:=TPasElement(ReferenceElements[i]);
  2525. //writeln('CheckDirectReference ',i,'/',ReferenceElements.Count,' ',GetTreeDbg(El,2));
  2526. if El.ClassType=TPasVariable then
  2527. begin
  2528. if TPasVariable(El).VarType=nil then
  2529. begin
  2530. //writeln('CheckDirectReference Var without Type: ',GetObjName(El),' El.Parent=',GetObjName(El.Parent));
  2531. AssertNotNull('TPasVariable(El='+El.Name+').VarType',TPasVariable(El).VarType);
  2532. end;
  2533. TypeEl:=TPasVariable(El).VarType;
  2534. for j:=0 to LabelElements.Count-1 do
  2535. begin
  2536. LabelEl:=TPasElement(LabelElements[j]);
  2537. if TypeEl=LabelEl then
  2538. exit; // success
  2539. end;
  2540. end
  2541. else if El is TPasAliasType then
  2542. begin
  2543. DeclEl:=TPasAliasType(El).DestType;
  2544. ResolverEngine.UnmangleSourceLineNumber(DeclEl.SourceLinenumber,LabelLine,LabelCol);
  2545. if (aLabel^.Filename=DeclEl.SourceFilename)
  2546. and (integer(aLabel^.Row)=LabelLine)
  2547. and (aLabel^.StartCol<=LabelCol)
  2548. and (aLabel^.EndCol>=LabelCol) then
  2549. exit; // success
  2550. end
  2551. else if El.ClassType=TPasArgument then
  2552. begin
  2553. TypeEl:=TPasArgument(El).ArgType;
  2554. for j:=0 to LabelElements.Count-1 do
  2555. begin
  2556. LabelEl:=TPasElement(LabelElements[j]);
  2557. if TypeEl=LabelEl then
  2558. exit; // success
  2559. end;
  2560. end;
  2561. end;
  2562. // failed -> show candidates
  2563. writeln('CheckDirectReference failed: Labels:');
  2564. for j:=0 to LabelElements.Count-1 do
  2565. begin
  2566. LabelEl:=TPasElement(LabelElements[j]);
  2567. writeln(' Label ',GetObjName(LabelEl),' at ',ResolverEngine.GetElementSourcePosStr(LabelEl));
  2568. end;
  2569. writeln('CheckDirectReference failed: References:');
  2570. for i:=0 to ReferenceElements.Count-1 do
  2571. begin
  2572. El:=TPasElement(ReferenceElements[i]);
  2573. writeln(' Reference ',GetObjName(El),' at ',ResolverEngine.GetElementSourcePosStr(El));
  2574. //if EL is TPasVariable then
  2575. // writeln('CheckDirectReference ',GetObjPath(TPasVariable(El).VarType),' ',ResolverEngine.GetElementSourcePosStr(TPasVariable(EL).VarType));
  2576. end;
  2577. RaiseErrorAtSrcMarker('wrong direct reference "'+aMarker^.Identifier+'"',aMarker);
  2578. finally
  2579. LabelElements.Free;
  2580. ReferenceElements.Free;
  2581. end;
  2582. end;
  2583. var
  2584. aMarker: PSrcMarker;
  2585. i: Integer;
  2586. SrcLines: TStringList;
  2587. begin
  2588. Module.ForEachCall(@OnCheckElementParent,nil);
  2589. //writeln('TCustomTestModule.CheckReferenceDirectives find all markers');
  2590. // find all markers
  2591. for i:=0 to FileResolver.Streams.Count-1 do
  2592. begin
  2593. GetSrc(i,SrcLines,CurFilename);
  2594. ParseCode(SrcLines,CurFilename);
  2595. SrcLines.Free;
  2596. end;
  2597. //writeln('TCustomTestModule.CheckReferenceDirectives check references');
  2598. // check references
  2599. aMarker:=FirstSrcMarker;
  2600. while aMarker<>nil do
  2601. begin
  2602. case aMarker^.Kind of
  2603. mkResolverReference: CheckResolverReference(aMarker);
  2604. mkDirectReference: CheckDirectReference(aMarker);
  2605. end;
  2606. aMarker:=aMarker^.Next;
  2607. end;
  2608. //writeln('TCustomTestModule.CheckReferenceDirectives COMPLETE');
  2609. end;
  2610. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2611. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2612. var
  2613. i: Integer;
  2614. Item: TTestHintMessage;
  2615. Expected,Actual: string;
  2616. begin
  2617. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2618. for i:=0 to MsgCount-1 do
  2619. begin
  2620. Item:=Msgs[i];
  2621. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2622. if (Marker<>nil) then
  2623. begin
  2624. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2625. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2626. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2627. end;
  2628. // found
  2629. FHintMsgsGood.Add(Item);
  2630. str(Item.MsgType,Actual);
  2631. str(MsgType,Expected);
  2632. AssertEquals('MsgType',Expected,Actual);
  2633. exit;
  2634. end;
  2635. // needed message missing -> show emitted messages
  2636. WriteSources('',0,0);
  2637. for i:=0 to MsgCount-1 do
  2638. begin
  2639. Item:=Msgs[i];
  2640. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2641. ' ('+IntToStr(Item.MsgNumber),')');
  2642. if Marker<>nil then
  2643. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2644. writeln(' {',Item.Msg,'}');
  2645. end;
  2646. str(MsgType,Expected);
  2647. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2648. if Marker<>nil then
  2649. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2650. Actual:=Actual+' '+Msg;
  2651. Fail(Actual);
  2652. end;
  2653. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2654. );
  2655. var
  2656. i: Integer;
  2657. s, Txt: String;
  2658. Msg: TTestHintMessage;
  2659. begin
  2660. for i:=0 to MsgCount-1 do
  2661. begin
  2662. Msg:=Msgs[i];
  2663. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2664. s:='';
  2665. str(Msg.MsgType,s);
  2666. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2667. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2668. if WithSourcePos then
  2669. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2670. Txt:=Txt+' {'+Msg.Msg+'}';
  2671. Fail(Txt);
  2672. end;
  2673. end;
  2674. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2675. MsgNumber: integer);
  2676. begin
  2677. ExpectedErrorClass:=EScannerError;
  2678. ExpectedErrorMsg:=Msg;
  2679. ExpectedErrorNumber:=MsgNumber;
  2680. end;
  2681. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2682. MsgNumber: integer);
  2683. begin
  2684. ExpectedErrorClass:=EParserError;
  2685. ExpectedErrorMsg:=Msg;
  2686. ExpectedErrorNumber:=MsgNumber;
  2687. end;
  2688. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2689. MsgNumber: integer);
  2690. begin
  2691. ExpectedErrorClass:=EPasResolve;
  2692. ExpectedErrorMsg:=Msg;
  2693. ExpectedErrorNumber:=MsgNumber;
  2694. end;
  2695. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2696. MsgNumber: integer);
  2697. begin
  2698. ExpectedErrorClass:=EPas2JS;
  2699. ExpectedErrorMsg:=Msg;
  2700. ExpectedErrorNumber:=MsgNumber;
  2701. end;
  2702. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2703. var
  2704. MsgNumber: Integer;
  2705. Msg: String;
  2706. begin
  2707. Result:=false;
  2708. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2709. Msg:=E.Message;
  2710. if E is EPas2JS then
  2711. MsgNumber:=EPas2JS(E).MsgNumber
  2712. else if E is EPasResolve then
  2713. MsgNumber:=EPasResolve(E).MsgNumber
  2714. else if E is EParserError then
  2715. MsgNumber:=Parser.LastMsgNumber
  2716. else if E is EScannerError then
  2717. begin
  2718. MsgNumber:=Scanner.LastMsgNumber;
  2719. Msg:=Scanner.LastMsg;
  2720. end
  2721. else
  2722. MsgNumber:=0;
  2723. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2724. if Result then
  2725. SkipTests:=true;
  2726. end;
  2727. procedure TCustomTestModule.RaiseErrorAtSrc(Msg: string;
  2728. const aFilename: string; aRow, aCol: integer);
  2729. var
  2730. s: String;
  2731. begin
  2732. WriteSources(aFilename,aRow,aCol);
  2733. s:='[TCustomTestModule.RaiseErrorAtSrc] '+aFilename+'('+IntToStr(aRow)+','+IntToStr(aCol)+') Error: '+Msg;
  2734. writeln('ERROR: ',s);
  2735. Fail(s);
  2736. end;
  2737. procedure TCustomTestModule.RaiseErrorAtSrcMarker(Msg: string;
  2738. aMarker: PSrcMarker);
  2739. begin
  2740. RaiseErrorAtSrc(Msg,aMarker^.Filename,aMarker^.Row,aMarker^.StartCol);
  2741. end;
  2742. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2743. begin
  2744. if IsErrorExpected(E) then exit;
  2745. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2746. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2747. +' '+Scanner.CurFilename
  2748. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2749. FailException(E);
  2750. end;
  2751. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2752. begin
  2753. if IsErrorExpected(E) then exit;
  2754. WriteSources(E.Filename,E.Row,E.Column);
  2755. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2756. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2757. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2758. );
  2759. FailException(E);
  2760. end;
  2761. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2762. var
  2763. P: TPasSourcePos;
  2764. begin
  2765. if IsErrorExpected(E) then exit;
  2766. P:=E.SourcePos;
  2767. WriteSources(P.FileName,P.Row,P.Column);
  2768. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2769. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2770. FailException(E);
  2771. end;
  2772. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2773. var
  2774. Row, Col: integer;
  2775. begin
  2776. if IsErrorExpected(E) then exit;
  2777. ResolverEngine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2778. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2779. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2780. +' '+E.PasElement.SourceFilename
  2781. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2782. FailException(E);
  2783. end;
  2784. procedure TCustomTestModule.HandleException(E: Exception);
  2785. begin
  2786. if E is EScannerError then
  2787. HandleScannerError(EScannerError(E))
  2788. else if E is EParserError then
  2789. HandleParserError(EParserError(E))
  2790. else if E is EPasResolve then
  2791. HandlePasResolveError(EPasResolve(E))
  2792. else if E is EPas2JS then
  2793. HandlePas2JSError(EPas2JS(E))
  2794. else
  2795. begin
  2796. if IsErrorExpected(E) then exit;
  2797. if not (E is EAssertionFailedError) then
  2798. begin
  2799. WriteSources('',0,0);
  2800. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2801. end;
  2802. FailException(E);
  2803. end;
  2804. end;
  2805. procedure TCustomTestModule.FailException(E: Exception);
  2806. var
  2807. MsgNumber: Integer;
  2808. begin
  2809. if ExpectedErrorClass<>nil then
  2810. begin
  2811. if FExpectedErrorClass=E.ClassType then
  2812. begin
  2813. if E is EPas2JS then
  2814. MsgNumber:=EPas2JS(E).MsgNumber
  2815. else if E is EPasResolve then
  2816. MsgNumber:=EPasResolve(E).MsgNumber
  2817. else if E is EParserError then
  2818. MsgNumber:=Parser.LastMsgNumber
  2819. else if E is EScannerError then
  2820. MsgNumber:=Scanner.LastMsgNumber
  2821. else
  2822. MsgNumber:=0;
  2823. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2824. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2825. ExpectedErrorNumber,MsgNumber);
  2826. end else begin
  2827. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2828. end;
  2829. end;
  2830. Fail(E.Message);
  2831. end;
  2832. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2833. aCol: integer);
  2834. var
  2835. IsSrc: Boolean;
  2836. i, j: Integer;
  2837. SrcLines: TStringList;
  2838. Line: string;
  2839. aModule: TTestEnginePasResolver;
  2840. begin
  2841. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2842. for i:=0 to ResolverCount-1 do
  2843. begin
  2844. aModule:=Resolvers[i];
  2845. SrcLines:=TStringList.Create;
  2846. try
  2847. SrcLines.Text:=aModule.Source;
  2848. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2849. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2850. for j:=1 to SrcLines.Count do
  2851. begin
  2852. Line:=SrcLines[j-1];
  2853. if IsSrc and (j=aRow) then
  2854. begin
  2855. write('*');
  2856. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2857. end;
  2858. writeln(Format('%:4d: ',[j]),Line);
  2859. end;
  2860. finally
  2861. SrcLines.Free;
  2862. end;
  2863. end;
  2864. end;
  2865. function TCustomTestModule.IndexOfResolver(const aFilename: string): integer;
  2866. var
  2867. i: Integer;
  2868. begin
  2869. for i:=0 to ResolverCount-1 do
  2870. if aFilename=Resolvers[i].Filename then exit(i);
  2871. Result:=-1;
  2872. end;
  2873. function TCustomTestModule.GetResolver(const aFilename: string
  2874. ): TTestEnginePasResolver;
  2875. var
  2876. i: Integer;
  2877. begin
  2878. i:=IndexOfResolver(aFilename);
  2879. if i<0 then exit(nil);
  2880. Result:=Resolvers[i];
  2881. end;
  2882. procedure TCustomTestModule.GetSrc(Index: integer; out SrcLines: TStringList;
  2883. out aFilename: string);
  2884. var
  2885. aStream: TStream;
  2886. begin
  2887. SrcLines:=TStringList.Create;
  2888. aStream:=FileResolver.Streams.Objects[Index] as TStream;
  2889. aStream.Position:=0;
  2890. SrcLines.LoadFromStream(aStream);
  2891. aFilename:=FileResolver.Streams[Index];
  2892. end;
  2893. function TCustomTestModule.FindElementsAt(aFilename: string; aLine, aStartCol,
  2894. aEndCol: integer): TFPList;
  2895. var
  2896. ok: Boolean;
  2897. FoundRefs: TTestResolverReferenceData;
  2898. i: Integer;
  2899. CurResolver: TTestEnginePasResolver;
  2900. begin
  2901. //writeln('TCustomTestModule.FindElementsAt START "',aFilename,'" Line=',aLine,' Col=',aStartCol,'-',aEndCol);
  2902. FoundRefs:=Default(TTestResolverReferenceData);
  2903. FoundRefs.Filename:=aFilename;
  2904. FoundRefs.Row:=aLine;
  2905. FoundRefs.StartCol:=aStartCol;
  2906. FoundRefs.EndCol:=aEndCol;
  2907. FoundRefs.Found:=TFPList.Create;
  2908. ok:=false;
  2909. try
  2910. // find all markers
  2911. Module.ForEachCall(@OnFindReference,@FoundRefs);
  2912. for i:=0 to ResolverCount-1 do
  2913. begin
  2914. CurResolver:=Resolvers[i];
  2915. if CurResolver.Module=Module then continue;
  2916. //writeln('TCustomTestResolver.FindElementsAt ',CurResolver.Filename);
  2917. CurResolver.Module.ForEachCall(@OnFindReference,@FoundRefs);
  2918. end;
  2919. ok:=true;
  2920. finally
  2921. if not ok then
  2922. FreeAndNil(FoundRefs.Found);
  2923. end;
  2924. Result:=FoundRefs.Found;
  2925. FoundRefs.Found:=nil;
  2926. end;
  2927. function TCustomTestModule.FindElementsAt(aMarker: PSrcMarker;
  2928. ErrorOnNoElements: boolean): TFPList;
  2929. begin
  2930. Result:=FindElementsAt(aMarker^.Filename,aMarker^.Row,aMarker^.StartCol,aMarker^.EndCol);
  2931. if ErrorOnNoElements and ((Result=nil) or (Result.Count=0)) then
  2932. RaiseErrorAtSrcMarker('marker '+SrcMarker[aMarker^.Kind]+aMarker^.Identifier+' has no elements',aMarker);
  2933. end;
  2934. function TCustomTestModule.FindSrcLabel(const Identifier: string): PSrcMarker;
  2935. begin
  2936. Result:=FirstSrcMarker;
  2937. while Result<>nil do
  2938. begin
  2939. if (Result^.Kind=mkLabel)
  2940. and (CompareText(Result^.Identifier,Identifier)=0) then
  2941. exit;
  2942. Result:=Result^.Next;
  2943. end;
  2944. end;
  2945. function TCustomTestModule.FindElementsAtSrcLabel(const Identifier: string;
  2946. ErrorOnNoElements: boolean): TFPList;
  2947. var
  2948. SrcLabel: PSrcMarker;
  2949. begin
  2950. SrcLabel:=FindSrcLabel(Identifier);
  2951. if SrcLabel=nil then
  2952. Fail('missing label "'+Identifier+'"');
  2953. Result:=FindElementsAt(SrcLabel,ErrorOnNoElements);
  2954. end;
  2955. function TCustomTestModule.GetDefaultNamespace: string;
  2956. var
  2957. C: TClass;
  2958. begin
  2959. Result:='';
  2960. if FModule=nil then exit;
  2961. C:=FModule.ClassType;
  2962. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2963. Result:=ResolverEngine.DefaultNameSpace;
  2964. end;
  2965. constructor TCustomTestModule.Create;
  2966. begin
  2967. inherited Create;
  2968. FHintMsgs:=TObjectList.Create(true);
  2969. FHintMsgsGood:=TFPList.Create;
  2970. end;
  2971. destructor TCustomTestModule.Destroy;
  2972. begin
  2973. FreeAndNil(FHintMsgs);
  2974. FreeAndNil(FHintMsgsGood);
  2975. inherited Destroy;
  2976. end;
  2977. { TTestModule }
  2978. procedure TTestModule.TestReservedWords;
  2979. var
  2980. i: integer;
  2981. begin
  2982. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2983. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2984. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2985. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2986. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2987. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2988. end;
  2989. procedure TTestModule.TestEmptyProgram;
  2990. begin
  2991. StartProgram(false);
  2992. Add('begin');
  2993. ConvertProgram;
  2994. CheckSource('TestEmptyProgram','','');
  2995. end;
  2996. procedure TTestModule.TestEmptyProgramUseStrict;
  2997. begin
  2998. Converter.Options:=Converter.Options+[coUseStrict];
  2999. StartProgram(false);
  3000. Add('begin');
  3001. ConvertProgram;
  3002. CheckSource('TestEmptyProgramUseStrict','','');
  3003. end;
  3004. procedure TTestModule.TestEmptyUnit;
  3005. begin
  3006. StartUnit(false);
  3007. Add('interface');
  3008. Add('implementation');
  3009. ConvertUnit;
  3010. CheckSource('TestEmptyUnit',
  3011. LinesToStr([
  3012. ]),
  3013. '');
  3014. end;
  3015. procedure TTestModule.TestEmptyUnitUseStrict;
  3016. begin
  3017. Converter.Options:=Converter.Options+[coUseStrict];
  3018. StartUnit(false);
  3019. Add('interface');
  3020. Add('implementation');
  3021. ConvertUnit;
  3022. CheckSource('TestEmptyUnitUseStrict',
  3023. LinesToStr([
  3024. ''
  3025. ]),
  3026. '');
  3027. end;
  3028. procedure TTestModule.TestDottedUnitNames;
  3029. begin
  3030. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  3031. LinesToStr([
  3032. 'var iV: longint;'
  3033. ]),
  3034. '');
  3035. FFilename:='ns1.test1.pp';
  3036. StartProgram(true);
  3037. Add('uses unIt2;');
  3038. Add('var');
  3039. Add(' i: longint;');
  3040. Add('begin');
  3041. Add(' i:=iv;');
  3042. Add(' i:=uNit2.iv;');
  3043. Add(' i:=Ns1.TEst1.i;');
  3044. ConvertProgram;
  3045. CheckSource('TestDottedUnitNames',
  3046. LinesToStr([
  3047. 'this.i = 0;',
  3048. '']),
  3049. LinesToStr([ // this.$init
  3050. '$mod.i = pas["NS1.Unit2"].iV;',
  3051. '$mod.i = pas["NS1.Unit2"].iV;',
  3052. '$mod.i = $mod.i;',
  3053. '']) );
  3054. end;
  3055. procedure TTestModule.TestDottedUnitNameImpl;
  3056. begin
  3057. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  3058. LinesToStr([
  3059. 'type',
  3060. ' TObject = class end;',
  3061. ' TTestA = class',
  3062. ' end;'
  3063. ]),
  3064. LinesToStr(['uses TEST.UnitB;'])
  3065. );
  3066. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  3067. LinesToStr([
  3068. 'uses TEST.UnitA;',
  3069. 'type TTestB = class(TTestA);'
  3070. ]),
  3071. ''
  3072. );
  3073. StartProgram(true);
  3074. Add('uses TEST.UnitA;');
  3075. Add('begin');
  3076. ConvertProgram;
  3077. CheckSource('TestDottedUnitNameImpl',
  3078. LinesToStr([
  3079. '']),
  3080. LinesToStr([ // this.$init
  3081. '']) );
  3082. CheckUnit('TEST.UnitA.pas',
  3083. LinesToStr([
  3084. 'rtl.module("TEST.UnitA", ["system"], function () {',
  3085. ' var $mod = this;',
  3086. ' rtl.createClass(this, "TObject", null, function () {',
  3087. ' this.$init = function () {',
  3088. ' };',
  3089. ' this.$final = function () {',
  3090. ' };',
  3091. ' });',
  3092. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  3093. ' });',
  3094. '}, ["TEST.UnitB"]);'
  3095. ]));
  3096. CheckUnit('TEST.UnitB.pas',
  3097. LinesToStr([
  3098. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  3099. ' var $mod = this;',
  3100. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  3101. ' });',
  3102. '});'
  3103. ]));
  3104. end;
  3105. procedure TTestModule.TestDottedUnitExpr;
  3106. begin
  3107. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  3108. LinesToStr([
  3109. 'procedure DoIt;'
  3110. ]),
  3111. 'procedure DoIt; begin end;');
  3112. FFilename:='Ns1.SubNs1.Test1.pp';
  3113. StartProgram(true);
  3114. Add('uses Ns2.sUbnS2.unIt2;');
  3115. Add('var');
  3116. Add(' i: longint;');
  3117. Add('begin');
  3118. Add(' ns2.subns2.unit2.doit;');
  3119. Add(' i:=Ns1.SubNS1.TEst1.i;');
  3120. ConvertProgram;
  3121. CheckSource('TestDottedUnitExpr',
  3122. LinesToStr([
  3123. 'this.i = 0;',
  3124. '']),
  3125. LinesToStr([ // this.$init
  3126. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  3127. '$mod.i = $mod.i;',
  3128. '']) );
  3129. end;
  3130. procedure TTestModule.Test_ModeFPCFail;
  3131. begin
  3132. StartProgram(false);
  3133. Add('{$mode FPC}');
  3134. Add('begin');
  3135. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  3136. ConvertProgram;
  3137. end;
  3138. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  3139. begin
  3140. StartProgram(false);
  3141. Add('{$modeswitch cblocks-}');
  3142. Add('begin');
  3143. ConvertProgram;
  3144. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  3145. CheckResolverUnexpectedHints();
  3146. end;
  3147. procedure TTestModule.TestUnit_UseSystem;
  3148. begin
  3149. StartUnit(true);
  3150. Add([
  3151. 'interface',
  3152. 'var i: integer;',
  3153. 'implementation']);
  3154. ConvertUnit;
  3155. CheckSource('TestUnit_UseSystem',
  3156. LinesToStr([
  3157. 'this.i = 0;',
  3158. '']),
  3159. LinesToStr([
  3160. '']) );
  3161. end;
  3162. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  3163. begin
  3164. AddModuleWithIntfImplSrc('unit1.pp',
  3165. LinesToStr([
  3166. 'type number = longint;']),
  3167. LinesToStr([
  3168. 'uses test1;',
  3169. 'procedure DoIt;',
  3170. 'begin',
  3171. ' i:=3;',
  3172. 'end;']));
  3173. StartUnit(true);
  3174. Add([
  3175. 'interface',
  3176. 'uses unit1;',
  3177. 'var i: number;',
  3178. 'implementation']);
  3179. ConvertUnit;
  3180. CheckSource('TestUnit_Intf1Impl2Intf1',
  3181. LinesToStr([
  3182. 'this.i = 0;',
  3183. '']),
  3184. LinesToStr([
  3185. '']) );
  3186. end;
  3187. procedure TTestModule.TestIncludeVersion;
  3188. begin
  3189. StartProgram(false);
  3190. Add([
  3191. 'var',
  3192. ' s: string;',
  3193. ' i: word;',
  3194. 'begin',
  3195. ' s:={$I %line%};',
  3196. ' i:={$I %linenum%};',
  3197. ' s:={$I %currentroutine%};',
  3198. ' s:={$I %pas2jsversion%};',
  3199. ' s:={$I %pas2jstarget%};',
  3200. ' s:={$I %pas2jstargetos%};',
  3201. ' s:={$I %pas2jstargetcpu%};',
  3202. ' s:={$I %file%};',
  3203. '']);
  3204. ConvertProgram;
  3205. CheckSource('TestIncludeVersion',
  3206. LinesToStr([
  3207. 'this.s="";',
  3208. 'this.i = 0;']),
  3209. LinesToStr([
  3210. '$mod.s = "7";',
  3211. '$mod.i = 8;',
  3212. '$mod.s = "<anonymous>";',
  3213. '$mod.s = "Comp.Ver.tcmodules";',
  3214. '$mod.s = "Browser";',
  3215. '$mod.s = "Browser";',
  3216. '$mod.s = "ECMAScript5";',
  3217. '$mod.s = "test1.pp";',
  3218. '']));
  3219. end;
  3220. procedure TTestModule.TestVarInt;
  3221. begin
  3222. StartProgram(false);
  3223. Add('var MyI: longint;');
  3224. Add('begin');
  3225. ConvertProgram;
  3226. CheckSource('TestVarInt','this.MyI=0;','');
  3227. end;
  3228. procedure TTestModule.TestVarBaseTypes;
  3229. begin
  3230. StartProgram(false);
  3231. Add('var');
  3232. Add(' i: longint;');
  3233. Add(' s: string;');
  3234. Add(' c: char;');
  3235. Add(' b: boolean;');
  3236. Add(' d: double;');
  3237. Add(' i2: longint = 3;');
  3238. Add(' s2: string = ''foo'';');
  3239. Add(' c2: char = ''4'';');
  3240. Add(' b2: boolean = true;');
  3241. Add(' d2: double = 5.6;');
  3242. Add(' i3: longint = $707;');
  3243. Add(' i4: nativeint = 9007199254740991;');
  3244. Add(' i5: nativeint = -9007199254740991-1;');
  3245. Add(' i6: nativeint = $fffffffffffff;');
  3246. Add(' i7: nativeint = -$fffffffffffff-1;');
  3247. Add(' i8: byte = 00;');
  3248. Add(' u8: nativeuint = $fffffffffffff;');
  3249. Add(' u9: nativeuint = $0000000000000;');
  3250. Add(' u10: nativeuint = $00ff00;');
  3251. Add('begin');
  3252. ConvertProgram;
  3253. CheckSource('TestVarBaseTypes',
  3254. LinesToStr([
  3255. 'this.i = 0;',
  3256. 'this.s = "";',
  3257. 'this.c = "\x00";',
  3258. 'this.b = false;',
  3259. 'this.d = 0.0;',
  3260. 'this.i2 = 3;',
  3261. 'this.s2 = "foo";',
  3262. 'this.c2 = "4";',
  3263. 'this.b2 = true;',
  3264. 'this.d2 = 5.6;',
  3265. 'this.i3 = 0x707;',
  3266. 'this.i4 = 9007199254740991;',
  3267. 'this.i5 = -9007199254740991-1;',
  3268. 'this.i6 = 0xfffffffffffff;',
  3269. 'this.i7 =-0xfffffffffffff-1;',
  3270. 'this.i8 = 0;',
  3271. 'this.u8 = 0xfffffffffffff;',
  3272. 'this.u9 = 0x0;',
  3273. 'this.u10 = 0xff00;'
  3274. ]),
  3275. '');
  3276. end;
  3277. procedure TTestModule.TestBaseTypeSingleFail;
  3278. begin
  3279. StartProgram(false);
  3280. Add('var s: single;');
  3281. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  3282. ConvertProgram;
  3283. end;
  3284. procedure TTestModule.TestBaseTypeExtendedFail;
  3285. begin
  3286. StartProgram(false);
  3287. Add('var e: extended;');
  3288. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  3289. ConvertProgram;
  3290. end;
  3291. procedure TTestModule.TestConstBaseTypes;
  3292. begin
  3293. StartProgram(false);
  3294. Add('const');
  3295. Add(' i: longint = 3;');
  3296. Add(' s: string = ''foo'';');
  3297. Add(' c: char = ''4'';');
  3298. Add(' b: boolean = true;');
  3299. Add(' d: double = 5.6;');
  3300. Add(' e = low(word);');
  3301. Add(' f = high(word);');
  3302. Add('begin');
  3303. ConvertProgram;
  3304. CheckSource('TestVarBaseTypes',
  3305. LinesToStr([
  3306. 'this.i=3;',
  3307. 'this.s="foo";',
  3308. 'this.c="4";',
  3309. 'this.b=true;',
  3310. 'this.d=5.6;',
  3311. 'this.e = 0;',
  3312. 'this.f = 65535;'
  3313. ]),
  3314. '');
  3315. end;
  3316. procedure TTestModule.TestAliasTypeRef;
  3317. begin
  3318. StartProgram(false);
  3319. Add('type');
  3320. Add(' a=longint;');
  3321. Add(' b=a;');
  3322. Add('var');
  3323. Add(' c: A;');
  3324. Add(' d: B;');
  3325. Add('begin');
  3326. ConvertProgram;
  3327. CheckSource('TestAliasTypeRef',
  3328. LinesToStr([ // statements
  3329. 'this.c = 0;',
  3330. 'this.d = 0;'
  3331. ]),
  3332. LinesToStr([ // this.$main
  3333. ''
  3334. ]));
  3335. end;
  3336. procedure TTestModule.TestTypeCast_BaseTypes;
  3337. begin
  3338. StartProgram(false);
  3339. Add([
  3340. 'var',
  3341. ' i: longint;',
  3342. ' b: boolean;',
  3343. ' d: double;',
  3344. ' s: string;',
  3345. ' c: char;',
  3346. 'begin',
  3347. ' i:=longint(i);',
  3348. ' i:=longint(b);',
  3349. ' b:=boolean(b);',
  3350. ' b:=boolean(i);',
  3351. ' d:=double(d);',
  3352. ' d:=double(i);',
  3353. ' s:=string(s);',
  3354. ' s:=string(c);',
  3355. ' c:=char(c);',
  3356. ' c:=char(i);',
  3357. ' c:=char(65);',
  3358. ' c:=char(#10);',
  3359. ' c:=char(#$E000);',
  3360. '']);
  3361. ConvertProgram;
  3362. CheckSource('TestAliasTypeRef',
  3363. LinesToStr([ // statements
  3364. 'this.i = 0;',
  3365. 'this.b = false;',
  3366. 'this.d = 0.0;',
  3367. 'this.s = "";',
  3368. 'this.c = "\x00";',
  3369. '']),
  3370. LinesToStr([ // this.$main
  3371. '$mod.i = $mod.i;',
  3372. '$mod.i = ($mod.b ? 1 : 0);',
  3373. '$mod.b = $mod.b;',
  3374. '$mod.b = $mod.i != 0;',
  3375. '$mod.d = $mod.d;',
  3376. '$mod.d = $mod.i;',
  3377. '$mod.s = $mod.s;',
  3378. '$mod.s = $mod.c;',
  3379. '$mod.c = $mod.c;',
  3380. '$mod.c = String.fromCharCode($mod.i);',
  3381. '$mod.c = "A";',
  3382. '$mod.c = "\n";',
  3383. '$mod.c = "";',
  3384. '']));
  3385. end;
  3386. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  3387. begin
  3388. StartProgram(false);
  3389. Add('type');
  3390. Add(' integer = longint;');
  3391. Add(' TYesNo = boolean;');
  3392. Add(' TFloat = double;');
  3393. Add(' TCaption = string;');
  3394. Add(' TChar = char;');
  3395. Add('var');
  3396. Add(' i: integer;');
  3397. Add(' b: TYesNo;');
  3398. Add(' d: TFloat;');
  3399. Add(' s: TCaption;');
  3400. Add(' c: TChar;');
  3401. Add('begin');
  3402. Add(' i:=integer(i);');
  3403. Add(' i:=integer(b);');
  3404. Add(' b:=TYesNo(b);');
  3405. Add(' b:=TYesNo(i);');
  3406. Add(' d:=TFloat(d);');
  3407. Add(' d:=TFloat(i);');
  3408. Add(' s:=TCaption(s);');
  3409. Add(' s:=TCaption(c);');
  3410. Add(' c:=TChar(c);');
  3411. ConvertProgram;
  3412. CheckSource('TestAliasTypeRef',
  3413. LinesToStr([ // statements
  3414. 'this.i = 0;',
  3415. 'this.b = false;',
  3416. 'this.d = 0.0;',
  3417. 'this.s = "";',
  3418. 'this.c = "\x00";',
  3419. '']),
  3420. LinesToStr([ // this.$main
  3421. '$mod.i = $mod.i;',
  3422. '$mod.i = ($mod.b ? 1 : 0);',
  3423. '$mod.b = $mod.b;',
  3424. '$mod.b = $mod.i != 0;',
  3425. '$mod.d = $mod.d;',
  3426. '$mod.d = $mod.i;',
  3427. '$mod.s = $mod.s;',
  3428. '$mod.s = $mod.c;',
  3429. '$mod.c = $mod.c;',
  3430. '']));
  3431. end;
  3432. procedure TTestModule.TestEmptyProc;
  3433. begin
  3434. StartProgram(false);
  3435. Add('procedure Test;');
  3436. Add('begin');
  3437. Add('end;');
  3438. Add('begin');
  3439. ConvertProgram;
  3440. CheckSource('TestEmptyProc',
  3441. LinesToStr([ // statements
  3442. 'this.Test = function () {',
  3443. '};'
  3444. ]),
  3445. LinesToStr([ // this.$main
  3446. ''
  3447. ]));
  3448. end;
  3449. procedure TTestModule.TestProcOneParam;
  3450. begin
  3451. StartProgram(false);
  3452. Add('procedure ProcA(i: longint);');
  3453. Add('begin');
  3454. Add('end;');
  3455. Add('begin');
  3456. Add(' PROCA(3);');
  3457. ConvertProgram;
  3458. CheckSource('TestProcOneParam',
  3459. LinesToStr([ // statements
  3460. 'this.ProcA = function (i) {',
  3461. '};'
  3462. ]),
  3463. LinesToStr([ // this.$main
  3464. '$mod.ProcA(3);'
  3465. ]));
  3466. end;
  3467. procedure TTestModule.TestFunctionWithoutParams;
  3468. begin
  3469. StartProgram(false);
  3470. Add('function FuncA: longint;');
  3471. Add('begin');
  3472. Add('end;');
  3473. Add('var i: longint;');
  3474. Add('begin');
  3475. Add(' I:=FUNCA();');
  3476. Add(' I:=FUNCA;');
  3477. Add(' FUNCA();');
  3478. Add(' FUNCA;');
  3479. ConvertProgram;
  3480. CheckSource('TestProcWithoutParams',
  3481. LinesToStr([ // statements
  3482. 'this.FuncA = function () {',
  3483. ' var Result = 0;',
  3484. ' return Result;',
  3485. '};',
  3486. 'this.i=0;'
  3487. ]),
  3488. LinesToStr([ // this.$main
  3489. '$mod.i=$mod.FuncA();',
  3490. '$mod.i=$mod.FuncA();',
  3491. '$mod.FuncA();',
  3492. '$mod.FuncA();'
  3493. ]));
  3494. end;
  3495. procedure TTestModule.TestProcedureWithoutParams;
  3496. begin
  3497. StartProgram(false);
  3498. Add('procedure ProcA;');
  3499. Add('begin');
  3500. Add('end;');
  3501. Add('begin');
  3502. Add(' PROCA();');
  3503. Add(' PROCA;');
  3504. ConvertProgram;
  3505. CheckSource('TestProcWithoutParams',
  3506. LinesToStr([ // statements
  3507. 'this.ProcA = function () {',
  3508. '};'
  3509. ]),
  3510. LinesToStr([ // this.$main
  3511. '$mod.ProcA();',
  3512. '$mod.ProcA();'
  3513. ]));
  3514. end;
  3515. procedure TTestModule.TestIncDec;
  3516. begin
  3517. StartProgram(false);
  3518. Add([
  3519. 'procedure DoIt(var i: longint);',
  3520. 'begin',
  3521. ' inc(i);',
  3522. ' inc(i,2);',
  3523. 'end;',
  3524. 'var',
  3525. ' Bar: longint;',
  3526. 'begin',
  3527. ' inc(bar);',
  3528. ' inc(bar,2);',
  3529. ' dec(bar);',
  3530. ' dec(bar,3);',
  3531. '']);
  3532. ConvertProgram;
  3533. CheckSource('TestIncDec',
  3534. LinesToStr([ // statements
  3535. 'this.DoIt = function (i) {',
  3536. ' i.set(i.get()+1);',
  3537. ' i.set(i.get()+2);',
  3538. '};',
  3539. 'this.Bar = 0;'
  3540. ]),
  3541. LinesToStr([ // this.$main
  3542. '$mod.Bar+=1;',
  3543. '$mod.Bar+=2;',
  3544. '$mod.Bar-=1;',
  3545. '$mod.Bar-=3;'
  3546. ]));
  3547. end;
  3548. procedure TTestModule.TestLoHiFpcMode;
  3549. begin
  3550. StartProgram(false);
  3551. Add([
  3552. '{$mode objfpc}',
  3553. 'const',
  3554. ' LoByte1 = Lo(Word($1234));',
  3555. ' HiByte1 = Hi(Word($1234));',
  3556. ' LoByte2 = Lo(SmallInt($1234));',
  3557. ' HiByte2 = Hi(SmallInt($1234));',
  3558. ' LoWord1 = Lo($1234CDEF);',
  3559. ' HiWord1 = Hi($1234CDEF);',
  3560. ' LoWord2 = Lo(-$1234CDEF);',
  3561. ' HiWord2 = Hi(-$1234CDEF);',
  3562. ' lo4:byte=lo(byte($34));',
  3563. ' hi4:byte=hi(byte($34));',
  3564. ' lo5:byte=lo(shortint(-$34));',
  3565. ' hi5:byte=hi(shortint(-$34));',
  3566. ' lo6:longword=lo($123456789ABCD);',
  3567. ' hi6:longword=hi($123456789ABCD);',
  3568. ' lo7:longword=lo(-$123456789ABCD);',
  3569. ' hi7:longword=hi(-$123456789ABCD);',
  3570. 'var',
  3571. ' b: Byte;',
  3572. ' ss: shortint;',
  3573. ' w: Word;',
  3574. ' si: SmallInt;',
  3575. ' lw: LongWord;',
  3576. ' li: LongInt;',
  3577. ' b2: Byte;',
  3578. ' ni: nativeint;',
  3579. 'begin',
  3580. ' w := $1234;',
  3581. ' ss := -$12;',
  3582. ' b := lo(ss);',
  3583. ' b := HI(ss);',
  3584. ' b := lo(w);',
  3585. ' b := HI(w);',
  3586. ' b2 := lo(b);',
  3587. ' b2 := hi(b);',
  3588. ' lw := $1234CDEF;',
  3589. ' w := lo(lw);',
  3590. ' w := hi(lw);',
  3591. ' ni := $123456789ABCD;',
  3592. ' lw := lo(ni);',
  3593. ' lw := hi(ni);',
  3594. '']);
  3595. ConvertProgram;
  3596. CheckSource('TestLoHiFpcMode',
  3597. LinesToStr([ // statements
  3598. 'this.LoByte1 = 0x1234 & 0xFF;',
  3599. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3600. 'this.LoByte2 = 0x1234 & 0xFF;',
  3601. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3602. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  3603. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  3604. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  3605. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  3606. 'this.lo4 = 0x34 & 0xF;',
  3607. 'this.hi4 = (0x34 >> 4) & 0xF;',
  3608. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  3609. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  3610. 'this.lo6 = 0x123456789ABCD >>> 0;',
  3611. 'this.hi6 = 74565 >>> 0;',
  3612. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3613. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3614. 'this.b = 0;',
  3615. 'this.ss = 0;',
  3616. 'this.w = 0;',
  3617. 'this.si = 0;',
  3618. 'this.lw = 0;',
  3619. 'this.li = 0;',
  3620. 'this.b2 = 0;',
  3621. 'this.ni = 0;',
  3622. '']),
  3623. LinesToStr([ // this.$main
  3624. '$mod.w = 0x1234;',
  3625. '$mod.ss = -0x12;',
  3626. '$mod.b = $mod.ss & 0xFF;',
  3627. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3628. '$mod.b = $mod.w & 0xFF;',
  3629. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3630. '$mod.b2 = $mod.b & 0xF;',
  3631. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3632. '$mod.lw = 0x1234CDEF;',
  3633. '$mod.w = $mod.lw & 0xFFFF;',
  3634. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3635. '$mod.ni = 0x123456789ABCD;',
  3636. '$mod.lw = $mod.ni >>> 0;',
  3637. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3638. '']));
  3639. end;
  3640. procedure TTestModule.TestLoHiDelphiMode;
  3641. begin
  3642. StartProgram(false);
  3643. Add([
  3644. '{$mode delphi}',
  3645. 'const',
  3646. ' LoByte1 = Lo(Word($1234));',
  3647. ' HiByte1 = Hi(Word($1234));',
  3648. ' LoByte2 = Lo(SmallInt($1234));',
  3649. ' HiByte2 = Hi(SmallInt($1234));',
  3650. ' LoByte3 = Lo($1234CDEF);',
  3651. ' HiByte3 = Hi($1234CDEF);',
  3652. ' LoByte4 = Lo(-$1234CDEF);',
  3653. ' HiByte4 = Hi(-$1234CDEF);',
  3654. 'var',
  3655. ' b: Byte;',
  3656. ' w: Word;',
  3657. ' si: SmallInt;',
  3658. ' lw: LongWord;',
  3659. ' li: LongInt;',
  3660. 'begin',
  3661. ' w := $1234;',
  3662. ' b := lo(w);',
  3663. ' b := HI(w);',
  3664. ' lw := $1234CDEF;',
  3665. ' b := lo(lw);',
  3666. ' b := hi(lw);',
  3667. '']);
  3668. ConvertProgram;
  3669. CheckSource('TestLoHiDelphiMode',
  3670. LinesToStr([ // statements
  3671. 'this.LoByte1 = 0x1234 & 0xFF;',
  3672. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3673. 'this.LoByte2 = 0x1234 & 0xFF;',
  3674. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3675. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3676. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3677. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3678. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3679. 'this.b = 0;',
  3680. 'this.w = 0;',
  3681. 'this.si = 0;',
  3682. 'this.lw = 0;',
  3683. 'this.li = 0;'
  3684. ]),
  3685. LinesToStr([ // this.$main
  3686. '$mod.w = 0x1234;',
  3687. '$mod.b = $mod.w & 0xFF;',
  3688. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3689. '$mod.lw = 0x1234CDEF;',
  3690. '$mod.b = $mod.lw & 0xFF;',
  3691. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3692. ]));
  3693. end;
  3694. procedure TTestModule.TestAssignments;
  3695. begin
  3696. StartProgram(false);
  3697. Parser.Options:=Parser.Options+[po_cassignments];
  3698. Add('var');
  3699. Add(' Bar:longint;');
  3700. Add('begin');
  3701. Add(' bar:=3;');
  3702. Add(' bar+=4;');
  3703. Add(' bar-=5;');
  3704. Add(' bar*=6;');
  3705. ConvertProgram;
  3706. CheckSource('TestAssignments',
  3707. LinesToStr([ // statements
  3708. 'this.Bar = 0;'
  3709. ]),
  3710. LinesToStr([ // this.$main
  3711. '$mod.Bar=3;',
  3712. '$mod.Bar+=4;',
  3713. '$mod.Bar-=5;',
  3714. '$mod.Bar*=6;'
  3715. ]));
  3716. end;
  3717. procedure TTestModule.TestArithmeticOperators1;
  3718. begin
  3719. StartProgram(false);
  3720. Add('var');
  3721. Add(' vA,vB,vC:longint;');
  3722. Add('begin');
  3723. Add(' va:=1;');
  3724. Add(' vb:=va+va;');
  3725. Add(' vb:=va div vb;');
  3726. Add(' vb:=va mod vb;');
  3727. Add(' vb:=va+va*vb+va div vb;');
  3728. Add(' vc:=-va;');
  3729. Add(' va:=va-vb;');
  3730. Add(' vb:=va;');
  3731. Add(' if va<vb then vc:=va else vc:=vb;');
  3732. ConvertProgram;
  3733. CheckSource('TestArithmeticOperators1',
  3734. LinesToStr([ // statements
  3735. 'this.vA = 0;',
  3736. 'this.vB = 0;',
  3737. 'this.vC = 0;'
  3738. ]),
  3739. LinesToStr([ // this.$main
  3740. '$mod.vA = 1;',
  3741. '$mod.vB = $mod.vA + $mod.vA;',
  3742. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3743. '$mod.vB = $mod.vA % $mod.vB;',
  3744. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3745. '$mod.vC = -$mod.vA;',
  3746. '$mod.vA = $mod.vA - $mod.vB;',
  3747. '$mod.vB = $mod.vA;',
  3748. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3749. ]));
  3750. end;
  3751. procedure TTestModule.TestMultiAdd;
  3752. begin
  3753. StartProgram(false);
  3754. Add([
  3755. 'function Fly: string; external name ''fly'';',
  3756. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3757. 'var',
  3758. ' Date: double;',
  3759. 'begin',
  3760. ' Result:=(Year>0) and (Year<10000) and',
  3761. ' (Month >= 1) and (Month<=12) and',
  3762. ' (Day>0) and (Day<=31);',
  3763. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3764. 'end;',
  3765. 'var s: string;',
  3766. 'begin',
  3767. ' s:=''a''+''b''+''c''+''d'';',
  3768. ' s:=s+Fly+''e'';',
  3769. ' s:=Fly+Fly+Fly;',
  3770. '']);
  3771. ConvertProgram;
  3772. CheckSource('TestMultiAdd',
  3773. LinesToStr([ // statements
  3774. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3775. ' var Result = false;',
  3776. ' var date = 0.0;',
  3777. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3778. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3779. ' return Result;',
  3780. '};',
  3781. 'this.s = "";',
  3782. '']),
  3783. LinesToStr([ // this.$main
  3784. '$mod.s = "a" + "b" + "c" + "d";',
  3785. '$mod.s = $mod.s + fly() + "e";',
  3786. '$mod.s = fly() + fly() + fly();',
  3787. '']));
  3788. end;
  3789. procedure TTestModule.TestLogicalOperators;
  3790. begin
  3791. StartProgram(false);
  3792. Add('var');
  3793. Add(' vA,vB,vC:boolean;');
  3794. Add('begin');
  3795. Add(' va:=vb and vc;');
  3796. Add(' va:=vb or vc;');
  3797. Add(' va:=vb xor vc;');
  3798. Add(' va:=true and vc;');
  3799. Add(' va:=(vb and vc) or (va and vb);');
  3800. Add(' va:=not vb;');
  3801. ConvertProgram;
  3802. CheckSource('TestLogicalOperators',
  3803. LinesToStr([ // statements
  3804. 'this.vA = false;',
  3805. 'this.vB = false;',
  3806. 'this.vC = false;'
  3807. ]),
  3808. LinesToStr([ // this.$main
  3809. '$mod.vA = $mod.vB && $mod.vC;',
  3810. '$mod.vA = $mod.vB || $mod.vC;',
  3811. '$mod.vA = $mod.vB ^ $mod.vC;',
  3812. '$mod.vA = true && $mod.vC;',
  3813. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3814. '$mod.vA = !$mod.vB;'
  3815. ]));
  3816. end;
  3817. procedure TTestModule.TestBitwiseOperators;
  3818. begin
  3819. StartProgram(false);
  3820. Add([
  3821. 'var',
  3822. ' vA,vB,vC:longint;',
  3823. ' X,Y,Z: nativeint;',
  3824. 'begin',
  3825. ' va:=vb and vc;',
  3826. ' va:=vb or vc;',
  3827. ' va:=vb xor vc;',
  3828. ' va:=vb shl vc;',
  3829. ' va:=vb shr vc;',
  3830. ' va:=3 and vc;',
  3831. ' va:=(vb and vc) or (va and vb);',
  3832. ' va:=not vb;',
  3833. ' X:=Y and Z;',
  3834. ' X:=Y and va;',
  3835. ' X:=Y or Z;',
  3836. ' X:=Y or va;',
  3837. ' X:=Y xor Z;',
  3838. ' X:=Y xor va;',
  3839. '']);
  3840. ConvertProgram;
  3841. CheckSource('TestBitwiseOperators',
  3842. LinesToStr([ // statements
  3843. 'this.vA = 0;',
  3844. 'this.vB = 0;',
  3845. 'this.vC = 0;',
  3846. 'this.X = 0;',
  3847. 'this.Y = 0;',
  3848. 'this.Z = 0;',
  3849. '']),
  3850. LinesToStr([ // this.$main
  3851. '$mod.vA = $mod.vB & $mod.vC;',
  3852. '$mod.vA = $mod.vB | $mod.vC;',
  3853. '$mod.vA = $mod.vB ^ $mod.vC;',
  3854. '$mod.vA = $mod.vB << $mod.vC;',
  3855. '$mod.vA = $mod.vB >>> $mod.vC;',
  3856. '$mod.vA = 3 & $mod.vC;',
  3857. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3858. '$mod.vA = ~$mod.vB;',
  3859. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3860. '$mod.X = $mod.Y & $mod.vA;',
  3861. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3862. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3863. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3864. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3865. '']));
  3866. end;
  3867. procedure TTestModule.TestBitwiseOperatorsLongword;
  3868. begin
  3869. StartProgram(false);
  3870. Add([
  3871. 'var',
  3872. ' a,b,c:longword;',
  3873. ' i: longint;',
  3874. 'begin',
  3875. ' a:=$12345678;',
  3876. ' b:=$EDCBA987;',
  3877. ' c:=not a;',
  3878. ' c:=a and b;',
  3879. ' c:=a and $ffff0000;',
  3880. ' c:=a or b;',
  3881. ' c:=a or $ff00ff00;',
  3882. ' c:=a xor b;',
  3883. ' c:=a xor $f0f0f0f0;',
  3884. ' c:=a shl 1;',
  3885. ' c:=a shl 16;',
  3886. ' c:=a shl 24;',
  3887. ' c:=a shl b;',
  3888. ' c:=a shr 1;',
  3889. ' c:=a shr 16;',
  3890. ' c:=a shr 24;',
  3891. ' c:=a shr b;',
  3892. ' c:=(b and c) or (a and b);',
  3893. ' c:=i and a;',
  3894. ' c:=i or a;',
  3895. ' c:=i xor a;',
  3896. '']);
  3897. ConvertProgram;
  3898. CheckSource('TestBitwiseOperatorsLongword',
  3899. LinesToStr([ // statements
  3900. 'this.a = 0;',
  3901. 'this.b = 0;',
  3902. 'this.c = 0;',
  3903. 'this.i = 0;',
  3904. '']),
  3905. LinesToStr([ // this.$main
  3906. '$mod.a = 0x12345678;',
  3907. '$mod.b = 0xEDCBA987;',
  3908. '$mod.c = rtl.lw(~$mod.a);',
  3909. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3910. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3911. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3912. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3913. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3914. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3915. '$mod.c = rtl.lw($mod.a << 1);',
  3916. '$mod.c = rtl.lw($mod.a << 16);',
  3917. '$mod.c = rtl.lw($mod.a << 24);',
  3918. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3919. '$mod.c = rtl.lw($mod.a >>> 1);',
  3920. '$mod.c = rtl.lw($mod.a >>> 16);',
  3921. '$mod.c = rtl.lw($mod.a >>> 24);',
  3922. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3923. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3924. '$mod.c = $mod.i & $mod.a;',
  3925. '$mod.c = $mod.i | $mod.a;',
  3926. '$mod.c = $mod.i ^ $mod.a;',
  3927. '']));
  3928. end;
  3929. procedure TTestModule.TestPrgProcVar;
  3930. begin
  3931. StartProgram(false);
  3932. Add('procedure Proc1;');
  3933. Add('type');
  3934. Add(' t1=longint;');
  3935. Add('var');
  3936. Add(' vA:t1;');
  3937. Add('begin');
  3938. Add('end;');
  3939. Add('begin');
  3940. ConvertProgram;
  3941. CheckSource('TestPrgProcVar',
  3942. LinesToStr([ // statements
  3943. 'this.Proc1 = function () {',
  3944. ' var vA=0;',
  3945. '};'
  3946. ]),
  3947. LinesToStr([ // this.$main
  3948. ''
  3949. ]));
  3950. end;
  3951. procedure TTestModule.TestUnitProcVar;
  3952. begin
  3953. StartUnit(false);
  3954. Add('interface');
  3955. Add('');
  3956. Add('type tA=string; // unit scope');
  3957. Add('procedure Proc1;');
  3958. Add('');
  3959. Add('implementation');
  3960. Add('');
  3961. Add('procedure Proc1;');
  3962. Add('type tA=longint; // local proc scope');
  3963. Add('var v1:tA; // using local tA');
  3964. Add('begin');
  3965. Add('end;');
  3966. Add('var v2:tA; // using interface tA');
  3967. ConvertUnit;
  3968. CheckSource('TestUnitProcVar',
  3969. LinesToStr([ // statements
  3970. 'var $impl = $mod.$impl;',
  3971. 'this.Proc1 = function () {',
  3972. ' var v1 = 0;',
  3973. '};',
  3974. '']),
  3975. // this.$init
  3976. '',
  3977. // implementation
  3978. LinesToStr([
  3979. '$impl.v2 = "";',
  3980. '']));
  3981. end;
  3982. procedure TTestModule.TestImplProc;
  3983. begin
  3984. StartUnit(false);
  3985. Add('interface');
  3986. Add('');
  3987. Add('procedure Proc1;');
  3988. Add('');
  3989. Add('implementation');
  3990. Add('');
  3991. Add('procedure Proc1; begin end;');
  3992. Add('procedure Proc2; begin end;');
  3993. Add('initialization');
  3994. Add(' Proc1;');
  3995. Add(' Proc2;');
  3996. ConvertUnit;
  3997. CheckSource('TestImplProc',
  3998. LinesToStr([ // statements
  3999. 'var $impl = $mod.$impl;',
  4000. 'this.Proc1 = function () {',
  4001. '};',
  4002. '']),
  4003. LinesToStr([ // this.$init
  4004. '$mod.Proc1();',
  4005. '$impl.Proc2();',
  4006. '']),
  4007. LinesToStr([ // implementation
  4008. '$impl.Proc2 = function () {',
  4009. '};',
  4010. ''])
  4011. );
  4012. end;
  4013. procedure TTestModule.TestFunctionResult;
  4014. begin
  4015. StartProgram(false);
  4016. Add('function Func1: longint;');
  4017. Add('begin');
  4018. Add(' Result:=3;');
  4019. Add(' Func1:=4;');
  4020. Add('end;');
  4021. Add('begin');
  4022. ConvertProgram;
  4023. CheckSource('TestFunctionResult',
  4024. LinesToStr([ // statements
  4025. 'this.Func1 = function () {',
  4026. ' var Result = 0;',
  4027. ' Result = 3;',
  4028. ' Result = 4;',
  4029. ' return Result;',
  4030. '};'
  4031. ]),
  4032. '');
  4033. end;
  4034. procedure TTestModule.TestNestedProc;
  4035. begin
  4036. StartProgram(false);
  4037. Add([
  4038. 'var vInUnit: longint;',
  4039. 'function DoIt(pA,pD: longint): longint;',
  4040. 'var',
  4041. ' vB: longint;',
  4042. ' vC: longint;',
  4043. ' function Nesty(pA: longint): longint; ',
  4044. ' var vB: longint;',
  4045. ' begin',
  4046. ' Result:=pa+vb+vc+pd+vInUnit;',
  4047. ' nesty:=3;',
  4048. ' doit:=4;',
  4049. ' exit;',
  4050. ' end;',
  4051. 'begin',
  4052. ' Result:=pa+vb+vc;',
  4053. ' doit:=6;',
  4054. ' exit;',
  4055. 'end;',
  4056. 'begin']);
  4057. ConvertProgram;
  4058. CheckSource('TestNestedProc',
  4059. LinesToStr([ // statements
  4060. 'this.vInUnit = 0;',
  4061. 'this.DoIt = function (pA, pD) {',
  4062. ' var Result = 0;',
  4063. ' var vB = 0;',
  4064. ' var vC = 0;',
  4065. ' function Nesty(pA) {',
  4066. ' var Result$1 = 0;',
  4067. ' var vB = 0;',
  4068. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  4069. ' Result$1 = 3;',
  4070. ' Result = 4;',
  4071. ' return Result$1;',
  4072. ' return Result$1;',
  4073. ' };',
  4074. ' Result = pA + vB + vC;',
  4075. ' Result = 6;',
  4076. ' return Result;',
  4077. ' return Result;',
  4078. '};'
  4079. ]),
  4080. '');
  4081. end;
  4082. procedure TTestModule.TestNestedProc_ResultString;
  4083. begin
  4084. StartProgram(false);
  4085. Add([
  4086. 'function DoIt: string;',
  4087. ' function Nesty: string; ',
  4088. ' begin',
  4089. ' nesty:=#65#66;',
  4090. ' nesty[1]:=#67;',
  4091. ' doit:=#68;',
  4092. ' doit[2]:=#69;',
  4093. ' end;',
  4094. 'begin',
  4095. ' doit:=#70;',
  4096. ' doit[3]:=#71;',
  4097. 'end;',
  4098. 'begin']);
  4099. ConvertProgram;
  4100. CheckSource('TestNestedProc_ResultString',
  4101. LinesToStr([ // statements
  4102. 'this.DoIt = function () {',
  4103. ' var Result = "";',
  4104. ' function Nesty() {',
  4105. ' var Result$1 = "";',
  4106. ' Result$1 = "AB";',
  4107. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  4108. ' Result = "D";',
  4109. ' Result = rtl.setCharAt(Result, 1, "E");',
  4110. ' return Result$1;',
  4111. ' };',
  4112. ' Result = "F";',
  4113. ' Result = rtl.setCharAt(Result, 2, "G");',
  4114. ' return Result;',
  4115. '};'
  4116. ]),
  4117. '');
  4118. end;
  4119. procedure TTestModule.TestForwardProc;
  4120. begin
  4121. StartProgram(false);
  4122. Add('procedure FuncA(Bar: longint); forward;');
  4123. Add('procedure FuncB(Bar: longint);');
  4124. Add('begin');
  4125. Add(' funca(bar);');
  4126. Add('end;');
  4127. Add('procedure funca(bar: longint);');
  4128. Add('begin');
  4129. Add(' if bar=3 then ;');
  4130. Add('end;');
  4131. Add('begin');
  4132. Add(' funca(4);');
  4133. Add(' funcb(5);');
  4134. ConvertProgram;
  4135. CheckSource('TestForwardProc',
  4136. LinesToStr([ // statements'
  4137. 'this.FuncB = function (Bar) {',
  4138. ' $mod.FuncA(Bar);',
  4139. '};',
  4140. 'this.FuncA = function (Bar) {',
  4141. ' if (Bar === 3);',
  4142. '};'
  4143. ]),
  4144. LinesToStr([
  4145. '$mod.FuncA(4);',
  4146. '$mod.FuncB(5);'
  4147. ])
  4148. );
  4149. end;
  4150. procedure TTestModule.TestNestedForwardProc;
  4151. begin
  4152. StartProgram(false);
  4153. Add('procedure FuncA;');
  4154. Add(' procedure FuncB(i: longint); forward;');
  4155. Add(' procedure FuncC(i: longint);');
  4156. Add(' begin');
  4157. Add(' funcb(i);');
  4158. Add(' end;');
  4159. Add(' procedure FuncB(i: longint);');
  4160. Add(' begin');
  4161. Add(' if i=3 then ;');
  4162. Add(' end;');
  4163. Add('begin');
  4164. Add(' funcc(4)');
  4165. Add('end;');
  4166. Add('begin');
  4167. Add(' funca;');
  4168. ConvertProgram;
  4169. CheckSource('TestNestedForwardProc',
  4170. LinesToStr([ // statements'
  4171. 'this.FuncA = function () {',
  4172. ' function FuncC(i) {',
  4173. ' FuncB(i);',
  4174. ' };',
  4175. ' function FuncB(i) {',
  4176. ' if (i === 3);',
  4177. ' };',
  4178. ' FuncC(4);',
  4179. '};'
  4180. ]),
  4181. LinesToStr([
  4182. '$mod.FuncA();'
  4183. ])
  4184. );
  4185. end;
  4186. procedure TTestModule.TestAssignFunctionResult;
  4187. begin
  4188. StartProgram(false);
  4189. Add('function Func1: longint;');
  4190. Add('begin');
  4191. Add('end;');
  4192. Add('var i: longint;');
  4193. Add('begin');
  4194. Add(' i:=func1();');
  4195. Add(' i:=func1()+func1();');
  4196. ConvertProgram;
  4197. CheckSource('TestAssignFunctionResult',
  4198. LinesToStr([ // statements
  4199. 'this.Func1 = function () {',
  4200. ' var Result = 0;',
  4201. ' return Result;',
  4202. '};',
  4203. 'this.i = 0;'
  4204. ]),
  4205. LinesToStr([
  4206. '$mod.i = $mod.Func1();',
  4207. '$mod.i = $mod.Func1() + $mod.Func1();'
  4208. ]));
  4209. end;
  4210. procedure TTestModule.TestFunctionResultInCondition;
  4211. begin
  4212. StartProgram(false);
  4213. Add('function Func1: longint;');
  4214. Add('begin');
  4215. Add('end;');
  4216. Add('function Func2: boolean;');
  4217. Add('begin');
  4218. Add('end;');
  4219. Add('var i: longint;');
  4220. Add('begin');
  4221. Add(' if func2 then ;');
  4222. Add(' if i=func1() then ;');
  4223. Add(' if i=func1 then ;');
  4224. ConvertProgram;
  4225. CheckSource('TestFunctionResultInCondition',
  4226. LinesToStr([ // statements
  4227. 'this.Func1 = function () {',
  4228. ' var Result = 0;',
  4229. ' return Result;',
  4230. '};',
  4231. 'this.Func2 = function () {',
  4232. ' var Result = false;',
  4233. ' return Result;',
  4234. '};',
  4235. 'this.i = 0;'
  4236. ]),
  4237. LinesToStr([
  4238. 'if ($mod.Func2());',
  4239. 'if ($mod.i === $mod.Func1());',
  4240. 'if ($mod.i === $mod.Func1());'
  4241. ]));
  4242. end;
  4243. procedure TTestModule.TestFunctionResultInForLoop;
  4244. begin
  4245. StartProgram(false);
  4246. Add([
  4247. 'function Func1(a: array of longint): longint;',
  4248. 'begin',
  4249. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  4250. ' for Result in a do if a[Result]=0 then exit;',
  4251. 'end;',
  4252. 'begin',
  4253. ' Func1([1,2,3])']);
  4254. ConvertProgram;
  4255. CheckSource('TestFunctionResultInForLoop',
  4256. LinesToStr([ // statements
  4257. 'this.Func1 = function (a) {',
  4258. ' var Result = 0;',
  4259. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  4260. ' Result = $l;',
  4261. ' if (a[Result] === 0) return Result;',
  4262. ' };',
  4263. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  4264. ' Result = $in[$l1];',
  4265. ' if (a[Result] === 0) return Result;',
  4266. ' };',
  4267. ' return Result;',
  4268. '};',
  4269. '']),
  4270. LinesToStr([
  4271. '$mod.Func1([1, 2, 3]);'
  4272. ]));
  4273. end;
  4274. procedure TTestModule.TestFunctionResultInTypeCast;
  4275. begin
  4276. StartProgram(false);
  4277. Add([
  4278. 'function GetInt: longint;',
  4279. 'begin',
  4280. 'end;',
  4281. 'begin',
  4282. ' if Byte(GetInt)=0 then ;',
  4283. '']);
  4284. ConvertProgram;
  4285. CheckSource('TestFunctionResultInTypeCast',
  4286. LinesToStr([ // statements
  4287. 'this.GetInt = function () {',
  4288. ' var Result = 0;',
  4289. ' return Result;',
  4290. '};',
  4291. '']),
  4292. LinesToStr([
  4293. 'if (($mod.GetInt() & 255) === 0) ;'
  4294. ]));
  4295. end;
  4296. procedure TTestModule.TestExit;
  4297. begin
  4298. StartProgram(false);
  4299. Add('procedure ProcA;');
  4300. Add('begin');
  4301. Add(' exit;');
  4302. Add('end;');
  4303. Add('function FuncB: longint;');
  4304. Add('begin');
  4305. Add(' exit;');
  4306. Add(' exit(3);');
  4307. Add('end;');
  4308. Add('function FuncC: string;');
  4309. Add('begin');
  4310. Add(' exit;');
  4311. Add(' exit(''a'');');
  4312. Add(' exit(''abc'');');
  4313. Add('end;');
  4314. Add('begin');
  4315. Add(' exit;');
  4316. Add(' exit(1);');
  4317. ConvertProgram;
  4318. CheckSource('TestExit',
  4319. LinesToStr([ // statements
  4320. 'this.ProcA = function () {',
  4321. ' return;',
  4322. '};',
  4323. 'this.FuncB = function () {',
  4324. ' var Result = 0;',
  4325. ' return Result;',
  4326. ' return 3;',
  4327. ' return Result;',
  4328. '};',
  4329. 'this.FuncC = function () {',
  4330. ' var Result = "";',
  4331. ' return Result;',
  4332. ' return "a";',
  4333. ' return "abc";',
  4334. ' return Result;',
  4335. '};'
  4336. ]),
  4337. LinesToStr([
  4338. 'return;',
  4339. 'return 1;',
  4340. '']));
  4341. end;
  4342. procedure TTestModule.TestExit_ResultInFinally;
  4343. begin
  4344. StartProgram(false);
  4345. Add([
  4346. 'function Run: word;',
  4347. 'begin',
  4348. ' try',
  4349. ' exit(3);', // no Result in finally -> use return 3
  4350. ' finally',
  4351. ' end;',
  4352. 'end;',
  4353. 'function Fly: word;',
  4354. 'begin',
  4355. ' try',
  4356. ' exit(3);',
  4357. ' finally',
  4358. ' if Result>0 then ;',
  4359. ' end;',
  4360. 'end;',
  4361. 'function Jump: word;',
  4362. 'begin',
  4363. ' try',
  4364. ' try',
  4365. ' exit(4);',
  4366. ' finally',
  4367. ' end;',
  4368. ' finally',
  4369. ' if Result>0 then ;',
  4370. ' end;',
  4371. 'end;',
  4372. 'begin',
  4373. '']);
  4374. ConvertProgram;
  4375. CheckSource('TestExit_ResultInFinally',
  4376. LinesToStr([ // statements
  4377. 'this.Run = function () {',
  4378. ' var Result = 0;',
  4379. ' try {',
  4380. ' return 3;',
  4381. ' } finally {',
  4382. ' };',
  4383. ' return Result;',
  4384. '};',
  4385. 'this.Fly = function () {',
  4386. ' var Result = 0;',
  4387. ' try {',
  4388. ' Result = 3;',
  4389. ' return Result;',
  4390. ' } finally {',
  4391. ' if (Result > 0) ;',
  4392. ' };',
  4393. ' return Result;',
  4394. '};',
  4395. 'this.Jump = function () {',
  4396. ' var Result = 0;',
  4397. ' try {',
  4398. ' try {',
  4399. ' Result = 4;',
  4400. ' return Result;',
  4401. ' } finally {',
  4402. ' };',
  4403. ' } finally {',
  4404. ' if (Result > 0) ;',
  4405. ' };',
  4406. ' return Result;',
  4407. '};',
  4408. '']),
  4409. LinesToStr([
  4410. '']));
  4411. end;
  4412. procedure TTestModule.TestBreak;
  4413. begin
  4414. StartProgram(false);
  4415. Add([
  4416. 'var',
  4417. ' i: longint;',
  4418. 'begin',
  4419. ' repeat',
  4420. ' break;',
  4421. ' until true;',
  4422. ' while true do',
  4423. ' break;',
  4424. ' for i:=1 to 2 do',
  4425. ' break;']);
  4426. ConvertProgram;
  4427. CheckSource('TestBreak',
  4428. LinesToStr([ // statements
  4429. 'this.i = 0;'
  4430. ]),
  4431. LinesToStr([
  4432. 'do {',
  4433. ' break;',
  4434. '} while (!true);',
  4435. 'while (true) break;',
  4436. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  4437. '']));
  4438. end;
  4439. procedure TTestModule.TestBreakAsVar;
  4440. begin
  4441. StartProgram(false);
  4442. Add([
  4443. 'procedure DoIt(break: boolean);',
  4444. 'begin',
  4445. ' if break then ;',
  4446. 'end;',
  4447. 'var',
  4448. ' break: boolean;',
  4449. 'begin',
  4450. ' if break then ;']);
  4451. ConvertProgram;
  4452. CheckSource('TestBreakAsVar',
  4453. LinesToStr([ // statements
  4454. 'this.DoIt = function (Break) {',
  4455. ' if (Break) ;',
  4456. '};',
  4457. 'this.Break = false;',
  4458. '']),
  4459. LinesToStr([
  4460. 'if($mod.Break) ;',
  4461. '']));
  4462. end;
  4463. procedure TTestModule.TestContinue;
  4464. begin
  4465. StartProgram(false);
  4466. Add('var i: longint;');
  4467. Add('begin');
  4468. Add(' repeat');
  4469. Add(' continue;');
  4470. Add(' until true;');
  4471. Add(' while true do');
  4472. Add(' continue;');
  4473. Add(' for i:=1 to 2 do');
  4474. Add(' continue;');
  4475. ConvertProgram;
  4476. CheckSource('TestContinue',
  4477. LinesToStr([ // statements
  4478. 'this.i = 0;'
  4479. ]),
  4480. LinesToStr([
  4481. 'do {',
  4482. ' continue;',
  4483. '} while (!true);',
  4484. 'while (true) continue;',
  4485. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  4486. '']));
  4487. end;
  4488. procedure TTestModule.TestProc_External;
  4489. begin
  4490. StartProgram(false);
  4491. Add('procedure Foo; external name ''console.log'';');
  4492. Add('function Bar: longint; external name ''get.item'';');
  4493. Add('function Bla(s: string): longint; external name ''apply.something'';');
  4494. Add('var');
  4495. Add(' i: longint;');
  4496. Add('begin');
  4497. Add(' Foo;');
  4498. Add(' i:=Bar;');
  4499. Add(' i:=Bla(''abc'');');
  4500. ConvertProgram;
  4501. CheckSource('TestProc_External',
  4502. LinesToStr([ // statements
  4503. 'this.i = 0;'
  4504. ]),
  4505. LinesToStr([
  4506. 'console.log();',
  4507. '$mod.i = get.item();',
  4508. '$mod.i = apply.something("abc");'
  4509. ]));
  4510. end;
  4511. procedure TTestModule.TestProc_ExternalOtherUnit;
  4512. begin
  4513. AddModuleWithIntfImplSrc('unit2.pas',
  4514. LinesToStr([
  4515. 'procedure Now; external name ''Date.now'';',
  4516. 'procedure DoIt;'
  4517. ]),
  4518. 'procedure doit; begin end;');
  4519. StartUnit(true);
  4520. Add('interface');
  4521. Add('uses unit2;');
  4522. Add('implementation');
  4523. Add('begin');
  4524. Add(' now;');
  4525. Add(' now();');
  4526. Add(' uNit2.now;');
  4527. Add(' uNit2.now();');
  4528. Add(' doit;');
  4529. Add(' uNit2.doit;');
  4530. ConvertUnit;
  4531. CheckSource('TestProc_ExternalOtherUnit',
  4532. LinesToStr([
  4533. '']),
  4534. LinesToStr([
  4535. 'Date.now();',
  4536. 'Date.now();',
  4537. 'Date.now();',
  4538. 'Date.now();',
  4539. 'pas.unit2.DoIt();',
  4540. 'pas.unit2.DoIt();',
  4541. '']));
  4542. end;
  4543. procedure TTestModule.TestProc_Asm;
  4544. begin
  4545. StartProgram(false);
  4546. Add([
  4547. '{$mode delphi}',
  4548. 'function DoIt: longint;',
  4549. 'begin;',
  4550. ' asm',
  4551. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4552. ' end;',
  4553. ' asm console.log(); end;',
  4554. ' asm',
  4555. ' s = "'' ";',
  4556. ' s = ''" '';',
  4557. ' s = s + "world" + "''";',
  4558. ' // end',
  4559. ' s = ''end'';',
  4560. ' s = "end";',
  4561. ' s = "foo\"bar";',
  4562. ' s = ''a\''b'';',
  4563. ' s = `${expr}\`-"-''-`;',
  4564. ' s = `multi',
  4565. 'line`;',
  4566. ' end;',
  4567. 'end;',
  4568. 'procedure Fly;',
  4569. 'asm',
  4570. ' return;',
  4571. 'end;',
  4572. 'begin']);
  4573. ConvertProgram;
  4574. CheckSource('TestProc_Asm',
  4575. LinesToStr([ // statements
  4576. 'this.DoIt = function () {',
  4577. ' var Result = 0;',
  4578. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4579. ' console.log();',
  4580. ' s = "'' ";',
  4581. ' s = ''" '';',
  4582. ' s = s + "world" + "''";',
  4583. ' // end',
  4584. ' s = ''end'';',
  4585. ' s = "end";',
  4586. ' s = "foo\"bar";',
  4587. ' s = ''a\''b'';',
  4588. ' s = `${expr}\`-"-''-`;',
  4589. ' s = `multi',
  4590. 'line`;',
  4591. ' return Result;',
  4592. '};',
  4593. 'this.Fly = function () {',
  4594. ' return;',
  4595. '};',
  4596. '']),
  4597. LinesToStr([
  4598. ''
  4599. ]));
  4600. end;
  4601. procedure TTestModule.TestProc_AsmSubBlock;
  4602. begin
  4603. StartProgram(true,[supTObject]);
  4604. Add([
  4605. '{$mode delphi}',
  4606. 'type',
  4607. ' TBird = class end;',
  4608. 'procedure Run(w: word);',
  4609. 'begin;',
  4610. ' if true then asm console.log(); end;',
  4611. ' if w>3 then asm',
  4612. ' var a = w+1;',
  4613. ' w = a+3;',
  4614. ' end;',
  4615. ' while (w>7) do asm',
  4616. ' w+=3; w*=2;',
  4617. ' end;',
  4618. ' try',
  4619. ' except',
  4620. ' on E: TBird do',
  4621. ' asm console.log(E); end;',
  4622. ' on E: TObject do',
  4623. ' asm var i=3; i--; end;',
  4624. ' else asm Fly; High; end;',
  4625. ' end;',
  4626. 'end;',
  4627. 'begin']);
  4628. ConvertProgram;
  4629. CheckSource('TestProc_AsmSubBlock',
  4630. LinesToStr([ // statements
  4631. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4632. '});',
  4633. 'this.Run = function (w) {',
  4634. ' if (true) console.log();',
  4635. ' if (w > 3) {',
  4636. ' var a = w+1;',
  4637. ' w = a+3;',
  4638. ' };',
  4639. ' while (w > 7) {',
  4640. ' w+=3; w*=2;',
  4641. ' };',
  4642. ' try {} catch ($e) {',
  4643. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4644. ' var E = $e;',
  4645. ' console.log(E);',
  4646. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4647. ' var E = $e;',
  4648. ' var i=3; i--;',
  4649. ' } else {',
  4650. ' Fly; High;',
  4651. ' }',
  4652. ' };',
  4653. '};',
  4654. '']),
  4655. LinesToStr([
  4656. ''
  4657. ]));
  4658. end;
  4659. procedure TTestModule.TestProc_Assembler;
  4660. begin
  4661. StartProgram(false);
  4662. Add('function DoIt: longint; assembler;');
  4663. Add('asm');
  4664. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4665. Add('end;');
  4666. Add('begin');
  4667. ConvertProgram;
  4668. CheckSource('TestProc_Assembler',
  4669. LinesToStr([ // statements
  4670. 'this.DoIt = function () {',
  4671. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4672. '};'
  4673. ]),
  4674. LinesToStr([
  4675. ''
  4676. ]));
  4677. end;
  4678. procedure TTestModule.TestProc_VarParam;
  4679. begin
  4680. StartProgram(false);
  4681. Add('type integer = longint;');
  4682. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4683. Add('var vJ: integer;');
  4684. Add('begin');
  4685. Add(' vg:=vg+1;');
  4686. Add(' vj:=vh+2;');
  4687. Add(' vi:=vi+3;');
  4688. Add(' doit(vg,vg,vg);');
  4689. Add(' doit(vh,vh,vj);');
  4690. Add(' doit(vi,vi,vi);');
  4691. Add(' doit(vj,vj,vj);');
  4692. Add('end;');
  4693. Add('var i: integer;');
  4694. Add('begin');
  4695. Add(' doit(i,i,i);');
  4696. ConvertProgram;
  4697. CheckSource('TestProc_VarParam',
  4698. LinesToStr([ // statements
  4699. 'this.DoIt = function (vG,vH,vI) {',
  4700. ' var vJ = 0;',
  4701. ' vG = vG + 1;',
  4702. ' vJ = vH + 2;',
  4703. ' vI.set(vI.get()+3);',
  4704. ' $mod.DoIt(vG, vG, {',
  4705. ' get: function () {',
  4706. ' return vG;',
  4707. ' },',
  4708. ' set: function (v) {',
  4709. ' vG = v;',
  4710. ' }',
  4711. ' });',
  4712. ' $mod.DoIt(vH, vH, {',
  4713. ' get: function () {',
  4714. ' return vJ;',
  4715. ' },',
  4716. ' set: function (v) {',
  4717. ' vJ = v;',
  4718. ' }',
  4719. ' });',
  4720. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4721. ' $mod.DoIt(vJ, vJ, {',
  4722. ' get: function () {',
  4723. ' return vJ;',
  4724. ' },',
  4725. ' set: function (v) {',
  4726. ' vJ = v;',
  4727. ' }',
  4728. ' });',
  4729. '};',
  4730. 'this.i = 0;'
  4731. ]),
  4732. LinesToStr([
  4733. '$mod.DoIt($mod.i,$mod.i,{',
  4734. ' p: $mod,',
  4735. ' get: function () {',
  4736. ' return this.p.i;',
  4737. ' },',
  4738. ' set: function (v) {',
  4739. ' this.p.i = v;',
  4740. ' }',
  4741. '});'
  4742. ]));
  4743. end;
  4744. procedure TTestModule.TestProc_VarParamString;
  4745. begin
  4746. StartProgram(false);
  4747. Add(['type TCaption = string;',
  4748. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4749. 'var c: char;',
  4750. 'begin',
  4751. ' va[1]:=c;',
  4752. ' vb[2]:=c;',
  4753. ' vc[3]:=c;',
  4754. 'end;',
  4755. 'begin']);
  4756. ConvertProgram;
  4757. CheckSource('TestProc_VarParamString',
  4758. LinesToStr([ // statements
  4759. 'this.DoIt = function (vA,vB,vC) {',
  4760. ' var c = "\x00";',
  4761. ' vA = rtl.setCharAt(vA, 0, c);',
  4762. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4763. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4764. '};',
  4765. '']),
  4766. LinesToStr([
  4767. ]));
  4768. end;
  4769. procedure TTestModule.TestProc_VarParamV;
  4770. begin
  4771. StartProgram(false);
  4772. Add([
  4773. 'procedure Inc2(var i: longint);',
  4774. 'begin',
  4775. ' i:=i+2;',
  4776. 'end;',
  4777. 'procedure DoIt(v: longint);',
  4778. 'var p: array of longint;',
  4779. 'begin',
  4780. ' Inc2(v);',
  4781. ' Inc2(p[v]);',
  4782. 'end;',
  4783. 'begin']);
  4784. ConvertProgram;
  4785. CheckSource('TestProc_VarParamV',
  4786. LinesToStr([ // statements
  4787. 'this.Inc2 = function (i) {',
  4788. ' i.set(i.get()+2);',
  4789. '};',
  4790. 'this.DoIt = function (v) {',
  4791. ' var p = [];',
  4792. ' $mod.Inc2({get: function () {',
  4793. ' return v;',
  4794. ' }, set: function (w) {',
  4795. ' v = w;',
  4796. ' }});',
  4797. ' $mod.Inc2({',
  4798. ' a: v,',
  4799. ' p: p,',
  4800. ' get: function () {',
  4801. ' return this.p[this.a];',
  4802. ' },',
  4803. ' set: function (v) {',
  4804. ' this.p[this.a] = v;',
  4805. ' }',
  4806. ' });',
  4807. '};',
  4808. '']),
  4809. LinesToStr([
  4810. '']));
  4811. end;
  4812. procedure TTestModule.TestProc_Overload;
  4813. begin
  4814. StartProgram(false);
  4815. Add('procedure DoIt(vI: longint); begin end;');
  4816. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4817. Add('procedure DoIt(vD: double); begin end;');
  4818. Add('begin');
  4819. Add(' DoIt(1);');
  4820. Add(' DoIt(2,3);');
  4821. Add(' DoIt(4.5);');
  4822. ConvertProgram;
  4823. CheckSource('TestProcedureOverload',
  4824. LinesToStr([ // statements
  4825. 'this.DoIt = function (vI) {',
  4826. '};',
  4827. 'this.DoIt$1 = function (vI, vJ) {',
  4828. '};',
  4829. 'this.DoIt$2 = function (vD) {',
  4830. '};',
  4831. '']),
  4832. LinesToStr([
  4833. '$mod.DoIt(1);',
  4834. '$mod.DoIt$1(2, 3);',
  4835. '$mod.DoIt$2(4.5);',
  4836. '']));
  4837. end;
  4838. procedure TTestModule.TestProc_OverloadForward;
  4839. begin
  4840. StartProgram(false);
  4841. Add('procedure DoIt(vI: longint); forward;');
  4842. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4843. Add('procedure doit(vi: longint); begin end;');
  4844. Add('begin');
  4845. Add(' doit(1);');
  4846. Add(' doit(2,3);');
  4847. ConvertProgram;
  4848. CheckSource('TestProcedureOverloadForward',
  4849. LinesToStr([ // statements
  4850. 'this.DoIt$1 = function (vI, vJ) {',
  4851. '};',
  4852. 'this.DoIt = function (vI) {',
  4853. '};',
  4854. '']),
  4855. LinesToStr([
  4856. '$mod.DoIt(1);',
  4857. '$mod.DoIt$1(2, 3);',
  4858. '']));
  4859. end;
  4860. procedure TTestModule.TestProc_OverloadIntfImpl;
  4861. begin
  4862. StartUnit(false);
  4863. Add('interface');
  4864. Add('procedure DoIt(vI: longint);');
  4865. Add('procedure DoIt(vI, vJ: longint);');
  4866. Add('implementation');
  4867. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4868. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4869. Add('procedure DoIt(vi: longint); begin end;');
  4870. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4871. Add('procedure DoIt(vi, vj: longint); begin end;');
  4872. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4873. Add('begin');
  4874. Add(' doit(1);');
  4875. Add(' doit(2,3);');
  4876. Add(' doit(4,5,6);');
  4877. Add(' doit(7,8,9,10);');
  4878. Add(' doit(11,12,13,14,15);');
  4879. ConvertUnit;
  4880. CheckSource('TestProcedureOverloadUnit',
  4881. LinesToStr([ // statements
  4882. 'var $impl = $mod.$impl;',
  4883. 'this.DoIt = function (vI) {',
  4884. '};',
  4885. 'this.DoIt$1 = function (vI, vJ) {',
  4886. '};',
  4887. '']),
  4888. LinesToStr([ // this.$init
  4889. '$mod.DoIt(1);',
  4890. '$mod.DoIt$1(2, 3);',
  4891. '$impl.DoIt$3(4,5,6);',
  4892. '$impl.DoIt$4(7,8,9,10);',
  4893. '$impl.DoIt$2(11,12,13,14,15);',
  4894. '']),
  4895. LinesToStr([ // implementation
  4896. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4897. '};',
  4898. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4899. '};',
  4900. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4901. '};',
  4902. '']));
  4903. end;
  4904. procedure TTestModule.TestProc_OverloadNested;
  4905. begin
  4906. StartProgram(false);
  4907. Add([
  4908. 'procedure doit(vA: longint);',
  4909. ' procedure DoIt(vA, vB: longint); overload;',
  4910. ' begin',
  4911. ' doit(1);',
  4912. ' doit(1,2);',
  4913. ' end;',
  4914. ' procedure doit(vA, vB, vC: longint);',
  4915. ' begin',
  4916. ' doit(1);',
  4917. ' doit(1,2);',
  4918. ' doit(1,2,3);',
  4919. ' end;',
  4920. 'begin',
  4921. ' doit(1);',
  4922. ' doit(1,2);',
  4923. ' doit(1,2,3);',
  4924. 'end;',
  4925. 'begin // main',
  4926. ' doit(1);']);
  4927. ConvertProgram;
  4928. CheckSource('TestProcedureOverloadNested',
  4929. LinesToStr([ // statements
  4930. 'this.doit = function (vA) {',
  4931. ' function DoIt$1(vA, vB) {',
  4932. ' $mod.doit(1);',
  4933. ' DoIt$1(1, 2);',
  4934. ' };',
  4935. ' function doit$2(vA, vB, vC) {',
  4936. ' $mod.doit(1);',
  4937. ' DoIt$1(1, 2);',
  4938. ' doit$2(1, 2, 3);',
  4939. ' };',
  4940. ' $mod.doit(1);',
  4941. ' DoIt$1(1, 2);',
  4942. ' doit$2(1, 2, 3);',
  4943. '};',
  4944. '']),
  4945. LinesToStr([
  4946. '$mod.doit(1);',
  4947. '']));
  4948. end;
  4949. procedure TTestModule.TestProc_OverloadNestedForward;
  4950. begin
  4951. StartProgram(false);
  4952. Add([
  4953. 'procedure DoIt(vA: longint); overload; forward;',
  4954. 'procedure DoIt(vB, vC: longint); overload;',
  4955. 'begin // 2 param overload',
  4956. ' doit(1);',
  4957. ' doit(1,2);',
  4958. 'end;',
  4959. 'procedure doit(vA: longint);',
  4960. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4961. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4962. ' begin // 4 param overload',
  4963. ' doit(1);',
  4964. ' doit(1,2);',
  4965. ' doit(1,2,3);',
  4966. ' doit(1,2,3,4);',
  4967. ' end;',
  4968. ' procedure doit(vA, vB, vC: longint);',
  4969. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4970. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4971. ' begin // 6 param overload',
  4972. ' doit(1);',
  4973. ' doit(1,2);',
  4974. ' doit(1,2,3);',
  4975. ' doit(1,2,3,4);',
  4976. ' doit(1,2,3,4,5);',
  4977. ' doit(1,2,3,4,5,6);',
  4978. ' end;',
  4979. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4980. ' begin // 5 param overload',
  4981. ' doit(1);',
  4982. ' doit(1,2);',
  4983. ' doit(1,2,3);',
  4984. ' doit(1,2,3,4);',
  4985. ' doit(1,2,3,4,5);',
  4986. ' doit(1,2,3,4,5,6);',
  4987. ' end;',
  4988. ' begin // 3 param overload',
  4989. ' doit(1);',
  4990. ' doit(1,2);',
  4991. ' doit(1,2,3);',
  4992. ' doit(1,2,3,4);',
  4993. ' doit(1,2,3,4,5);',
  4994. ' doit(1,2,3,4,5,6);',
  4995. ' end;',
  4996. 'begin // 1 param overload',
  4997. ' doit(1);',
  4998. ' doit(1,2);',
  4999. ' doit(1,2,3);',
  5000. ' doit(1,2,3,4);',
  5001. 'end;',
  5002. 'begin // main',
  5003. ' doit(1);',
  5004. ' doit(1,2);']);
  5005. ConvertProgram;
  5006. CheckSource('TestProc_OverloadNestedForward',
  5007. LinesToStr([ // statements
  5008. 'this.DoIt$1 = function (vB, vC) {',
  5009. ' $mod.DoIt(1);',
  5010. ' $mod.DoIt$1(1, 2);',
  5011. '};',
  5012. 'this.DoIt = function (vA) {',
  5013. ' function DoIt$3(vA, vB, vC, vD) {',
  5014. ' $mod.DoIt(1);',
  5015. ' $mod.DoIt$1(1, 2);',
  5016. ' DoIt$2(1, 2, 3);',
  5017. ' DoIt$3(1, 2, 3, 4);',
  5018. ' };',
  5019. ' function DoIt$2(vA, vB, vC) {',
  5020. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  5021. ' $mod.DoIt(1);',
  5022. ' $mod.DoIt$1(1, 2);',
  5023. ' DoIt$2(1, 2, 3);',
  5024. ' DoIt$3(1, 2, 3, 4);',
  5025. ' DoIt$4(1, 2, 3, 4, 5);',
  5026. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5027. ' };',
  5028. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  5029. ' $mod.DoIt(1);',
  5030. ' $mod.DoIt$1(1, 2);',
  5031. ' DoIt$2(1, 2, 3);',
  5032. ' DoIt$3(1, 2, 3, 4);',
  5033. ' DoIt$4(1, 2, 3, 4, 5);',
  5034. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5035. ' };',
  5036. ' $mod.DoIt(1);',
  5037. ' $mod.DoIt$1(1, 2);',
  5038. ' DoIt$2(1, 2, 3);',
  5039. ' DoIt$3(1, 2, 3, 4);',
  5040. ' DoIt$4(1, 2, 3, 4, 5);',
  5041. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  5042. ' };',
  5043. ' $mod.DoIt(1);',
  5044. ' $mod.DoIt$1(1, 2);',
  5045. ' DoIt$2(1, 2, 3);',
  5046. ' DoIt$3(1, 2, 3, 4);',
  5047. '};',
  5048. '']),
  5049. LinesToStr([
  5050. '$mod.DoIt(1);',
  5051. '$mod.DoIt$1(1, 2);',
  5052. '']));
  5053. end;
  5054. procedure TTestModule.TestProc_OverloadUnitCycle;
  5055. begin
  5056. AddModuleWithIntfImplSrc('Unit2.pas',
  5057. LinesToStr([
  5058. 'type',
  5059. ' TObject = class',
  5060. ' procedure DoIt(b: boolean); virtual; abstract;',
  5061. ' procedure DoIt(i: longint); virtual; abstract;',
  5062. ' end;',
  5063. '']),
  5064. 'uses test1;');
  5065. StartUnit(true);
  5066. Add([
  5067. 'interface',
  5068. 'uses unit2;',
  5069. 'type',
  5070. ' TEagle = class(TObject)',
  5071. ' procedure DoIt(b: boolean); override;',
  5072. ' procedure DoIt(i: longint); override;',
  5073. ' end;',
  5074. 'implementation',
  5075. 'procedure TEagle.DoIt(b: boolean); begin end;',
  5076. 'procedure TEagle.DoIt(i: longint); begin end;',
  5077. '']);
  5078. ConvertUnit;
  5079. CheckSource('TestProc_OverloadUnitCycle',
  5080. LinesToStr([ // statements
  5081. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  5082. ' this.DoIt = function (b) {',
  5083. ' };',
  5084. ' this.DoIt$1 = function (i) {',
  5085. ' };',
  5086. '});',
  5087. '']),
  5088. '',
  5089. LinesToStr([
  5090. '']));
  5091. end;
  5092. procedure TTestModule.TestProc_Varargs;
  5093. begin
  5094. StartProgram(false);
  5095. Add([
  5096. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  5097. 'procedure ProcB; varargs; external name ''ProcB'';',
  5098. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  5099. 'function GetIt: longint; begin end;',
  5100. 'begin',
  5101. ' ProcA(1);',
  5102. ' ProcA(1,2);',
  5103. ' ProcA(1,2.0);',
  5104. ' ProcA(1,2,3);',
  5105. ' ProcA(1,''2'');',
  5106. ' ProcA(2,'''');',
  5107. ' ProcA(3,false);',
  5108. ' ProcB;',
  5109. ' ProcB();',
  5110. ' ProcB(4);',
  5111. ' ProcB(''foo'');',
  5112. ' ProcC;',
  5113. ' ProcC();',
  5114. ' ProcC(4);',
  5115. ' ProcC(5,''foo'');',
  5116. ' ProcB(GetIt);',
  5117. ' ProcB(GetIt());',
  5118. ' ProcB(GetIt,GetIt());']);
  5119. ConvertProgram;
  5120. CheckSource('TestProc_Varargs',
  5121. LinesToStr([ // statements
  5122. 'this.GetIt = function () {',
  5123. ' var Result = 0;',
  5124. ' return Result;',
  5125. '};',
  5126. '']),
  5127. LinesToStr([
  5128. 'ProcA(1);',
  5129. 'ProcA(1, 2);',
  5130. 'ProcA(1, 2.0);',
  5131. 'ProcA(1, 2, 3);',
  5132. 'ProcA(1, "2");',
  5133. 'ProcA(2, "");',
  5134. 'ProcA(3, false);',
  5135. 'ProcB();',
  5136. 'ProcB();',
  5137. 'ProcB(4);',
  5138. 'ProcB("foo");',
  5139. 'ProcC(17);',
  5140. 'ProcC(17);',
  5141. 'ProcC(4);',
  5142. 'ProcC(5, "foo");',
  5143. 'ProcB($mod.GetIt());',
  5144. 'ProcB($mod.GetIt());',
  5145. 'ProcB($mod.GetIt(), $mod.GetIt());',
  5146. '']));
  5147. end;
  5148. procedure TTestModule.TestProc_ConstOrder;
  5149. begin
  5150. StartProgram(false);
  5151. Add([
  5152. 'const A = 3;',
  5153. 'const B = A+1;',
  5154. 'procedure DoIt;',
  5155. 'const C = A+1;',
  5156. 'const D = B+1;',
  5157. 'const E = D+C+B+A;',
  5158. 'begin',
  5159. 'end;',
  5160. 'begin'
  5161. ]);
  5162. ConvertProgram;
  5163. CheckSource('TestProc_ConstOrder',
  5164. LinesToStr([ // statements
  5165. 'this.A = 3;',
  5166. 'this.B = 3 + 1;',
  5167. 'var C = 3 + 1;',
  5168. 'var D = 4 + 1;',
  5169. 'var E = 5 + 4 + 4 + 3;',
  5170. 'this.DoIt = function () {',
  5171. '};',
  5172. '']),
  5173. LinesToStr([
  5174. ''
  5175. ]));
  5176. end;
  5177. procedure TTestModule.TestProc_DuplicateConst;
  5178. begin
  5179. StartProgram(false);
  5180. Add([
  5181. 'const A = 1;',
  5182. 'procedure DoIt;',
  5183. 'const A = 2;',
  5184. ' procedure SubIt;',
  5185. ' const A = 21;',
  5186. ' begin',
  5187. ' end;',
  5188. 'begin',
  5189. 'end;',
  5190. 'procedure DoSome;',
  5191. 'const A = 3;',
  5192. 'begin',
  5193. 'end;',
  5194. 'begin'
  5195. ]);
  5196. ConvertProgram;
  5197. CheckSource('TestProc_DuplicateConst',
  5198. LinesToStr([ // statements
  5199. 'this.A = 1;',
  5200. 'var A$1 = 2;',
  5201. 'var A$2 = 21;',
  5202. 'this.DoIt = function () {',
  5203. ' function SubIt() {',
  5204. ' };',
  5205. '};',
  5206. 'var A$3 = 3;',
  5207. 'this.DoSome = function () {',
  5208. '};',
  5209. '']),
  5210. LinesToStr([
  5211. ''
  5212. ]));
  5213. end;
  5214. procedure TTestModule.TestProc_LocalVarAbsolute;
  5215. begin
  5216. StartProgram(false);
  5217. Add([
  5218. 'type',
  5219. ' TObject = class',
  5220. ' Index: longint;',
  5221. ' procedure DoAbs(Item: pointer);',
  5222. ' end;',
  5223. 'procedure TObject.DoAbs(Item: pointer);',
  5224. 'var',
  5225. ' o: TObject absolute Item;',
  5226. 'begin',
  5227. ' if o.Index<o.Index then o.Index:=o.Index;',
  5228. 'end;',
  5229. 'procedure DoIt(i: longint; p: pointer);',
  5230. 'var',
  5231. ' d: double absolute i;',
  5232. ' s: string absolute d;',
  5233. ' oi: TObject absolute i;',
  5234. ' op: TObject absolute p;',
  5235. 'begin',
  5236. ' if d=d then d:=d;',
  5237. ' if s=s then s:=s;',
  5238. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  5239. ' if op.Index=op.Index then op.Index:=op.Index;',
  5240. 'end;',
  5241. 'begin']);
  5242. ConvertProgram;
  5243. CheckSource('TestProc_LocalVarAbsolute',
  5244. LinesToStr([ // statements
  5245. 'rtl.createClass(this, "TObject", null, function () {',
  5246. ' this.$init = function () {',
  5247. ' this.Index = 0;',
  5248. ' };',
  5249. ' this.$final = function () {',
  5250. ' };',
  5251. ' this.DoAbs = function (Item) {',
  5252. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  5253. ' };',
  5254. '});',
  5255. 'this.DoIt = function (i, p) {',
  5256. ' if (i === i) i = i;',
  5257. ' if (i === i) i = i;',
  5258. ' if (i.Index < i.Index) i.Index = i.Index;',
  5259. ' if (p.Index === p.Index) p.Index = p.Index;',
  5260. '};'
  5261. ]),
  5262. LinesToStr([
  5263. ]));
  5264. end;
  5265. procedure TTestModule.TestProc_ResultAbsolute;
  5266. begin
  5267. StartProgram(false);
  5268. Add([
  5269. 'type',
  5270. ' TObject = class',
  5271. ' Index: longint;',
  5272. ' function DoAbs: pointer;',
  5273. ' end;',
  5274. 'function TObject.DoAbs: pointer;',
  5275. 'var',
  5276. ' o: TObject absolute Result;',
  5277. 'begin',
  5278. ' if o.Index<o.Index then o.Index:=o.Index;',
  5279. 'end;',
  5280. 'function DoIt: jsvalue;',
  5281. 'var',
  5282. ' d: double absolute Result;',
  5283. ' s: string absolute Result;',
  5284. ' o: TObject absolute Result;',
  5285. 'begin',
  5286. ' if d=d then d:=d;',
  5287. ' if s=s then s:=s;',
  5288. ' if o.Index<o.Index then o.Index:=o.Index;',
  5289. 'end;',
  5290. 'begin']);
  5291. ConvertProgram;
  5292. CheckSource('TestProc_ResultAbsolute',
  5293. LinesToStr([ // statements
  5294. 'rtl.createClass(this, "TObject", null, function () {',
  5295. ' this.$init = function () {',
  5296. ' this.Index = 0;',
  5297. ' };',
  5298. ' this.$final = function () {',
  5299. ' };',
  5300. ' this.DoAbs = function () {',
  5301. ' var Result = null;',
  5302. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5303. ' return Result;',
  5304. ' };',
  5305. '});',
  5306. 'this.DoIt = function () {',
  5307. ' var Result = undefined;',
  5308. ' if (Result === Result) Result = Result;',
  5309. ' if (Result === Result) Result = Result;',
  5310. ' if (Result.Index < Result.Index) Result.Index = Result.Index;',
  5311. ' return Result;',
  5312. '};',
  5313. '']),
  5314. LinesToStr([
  5315. ]));
  5316. end;
  5317. procedure TTestModule.TestProc_LocalVarInit;
  5318. begin
  5319. StartProgram(false);
  5320. Add([
  5321. 'type TBytes = array of byte;',
  5322. 'procedure DoIt;',
  5323. 'const c = 4;',
  5324. 'var',
  5325. ' b: byte = 1;',
  5326. ' w: word = 2+c;',
  5327. ' p: pointer = nil;',
  5328. ' Buffer: TBytes = nil;',
  5329. 'begin',
  5330. 'end;',
  5331. 'begin']);
  5332. ConvertProgram;
  5333. CheckSource('TestProc_LocalVarInit',
  5334. LinesToStr([ // statements
  5335. 'var c = 4;',
  5336. 'this.DoIt = function () {',
  5337. ' var b = 1;',
  5338. ' var w = 2 + 4;',
  5339. ' var p = null;',
  5340. ' var Buffer = [];',
  5341. '};',
  5342. '']),
  5343. LinesToStr([
  5344. ]));
  5345. end;
  5346. procedure TTestModule.TestProc_ReservedWords;
  5347. begin
  5348. StartProgram(false);
  5349. Add([
  5350. 'procedure Date(ArrayBuffer: longint);',
  5351. 'const',
  5352. ' NaN: longint = 3;',
  5353. 'var',
  5354. ' &Boolean: longint;',
  5355. ' procedure Error(ArrayBuffer: longint);',
  5356. ' begin',
  5357. ' end;',
  5358. 'begin',
  5359. ' Nan:=&bOolean;',
  5360. 'end;',
  5361. 'begin',
  5362. ' Date(1);']);
  5363. ConvertProgram;
  5364. CheckSource('TestProc_ReservedWords',
  5365. LinesToStr([ // statements
  5366. 'var naN = 3;',
  5367. 'this.Date = function (arrayBuffer) {',
  5368. ' var boolean = 0;',
  5369. ' function error(arrayBuffer) {',
  5370. ' };',
  5371. ' naN = boolean;',
  5372. '};',
  5373. '']),
  5374. LinesToStr([
  5375. ' $mod.Date(1);'
  5376. ]));
  5377. end;
  5378. procedure TTestModule.TestProc_ConstRefWord;
  5379. begin
  5380. StartProgram(false);
  5381. Add([
  5382. 'procedure Run(constref w: word);',
  5383. 'var l: word;',
  5384. 'begin',
  5385. ' l:=w;',
  5386. ' Run(w);',
  5387. ' Run(l);',
  5388. 'end;',
  5389. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  5390. 'begin',
  5391. ' Run(a);',
  5392. ' Run(b);',
  5393. ' Run(c);',
  5394. ' Run(d);',
  5395. ' Run(e);',
  5396. 'end;',
  5397. 'begin',
  5398. ' Run(1);']);
  5399. ConvertProgram;
  5400. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  5401. CheckSource('TestProc_ConstRefWord',
  5402. LinesToStr([ // statements
  5403. 'this.Run = function (w) {',
  5404. ' var l = 0;',
  5405. ' l = w;',
  5406. ' $mod.Run(w);',
  5407. ' $mod.Run(l);',
  5408. '};',
  5409. 'this.Fly = function (a, b, c, d, e) {',
  5410. ' $mod.Run(a);',
  5411. ' $mod.Run(b.get());',
  5412. ' $mod.Run(c.get());',
  5413. ' $mod.Run(d);',
  5414. ' $mod.Run(e);',
  5415. '};',
  5416. '']),
  5417. LinesToStr([
  5418. '$mod.Run(1);'
  5419. ]));
  5420. end;
  5421. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  5422. begin
  5423. StartProgram(false);
  5424. Add([
  5425. '{$mode objfpc}',
  5426. 'type',
  5427. ' TFunc = reference to function(x: word): word;',
  5428. 'var Func: TFunc;',
  5429. 'procedure DoIt(a: word);',
  5430. 'begin',
  5431. ' Func:=function(b:word): word',
  5432. ' begin',
  5433. ' Result:=a+b;',
  5434. ' exit(b);',
  5435. ' exit(Result);',
  5436. ' end;',// test semicolon
  5437. ' a:=3;',
  5438. 'end;',
  5439. 'begin',
  5440. ' Func:=function(c:word):word begin',
  5441. ' Result:=3+c;',
  5442. ' exit(c);',
  5443. ' exit(Result);',
  5444. ' end;']);
  5445. ConvertProgram;
  5446. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  5447. LinesToStr([ // statements
  5448. 'this.Func = null;',
  5449. 'this.DoIt = function (a) {',
  5450. ' $mod.Func = function (b) {',
  5451. ' var Result = 0;',
  5452. ' Result = a + b;',
  5453. ' return b;',
  5454. ' return Result;',
  5455. ' return Result;',
  5456. ' };',
  5457. ' a = 3;',
  5458. '};',
  5459. '']),
  5460. LinesToStr([
  5461. '$mod.Func = function (c) {',
  5462. ' var Result = 0;',
  5463. ' Result = 3 + c;',
  5464. ' return c;',
  5465. ' return Result;',
  5466. ' return Result;',
  5467. '};',
  5468. '']));
  5469. end;
  5470. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  5471. begin
  5472. StartProgram(false);
  5473. Add([
  5474. '{$mode delphi}',
  5475. 'type',
  5476. ' TProc = reference to procedure(x: word);',
  5477. 'procedure DoIt(a: word);',
  5478. 'var Proc: TProc;',
  5479. 'begin',
  5480. ' Proc:=procedure(b:word) begin end;',
  5481. 'end;',
  5482. 'var Proc: TProc;',
  5483. 'begin',
  5484. ' Proc:=procedure(c:word) begin end;',
  5485. '']);
  5486. ConvertProgram;
  5487. CheckSource('TestAnonymousProc_Assign_Delphi',
  5488. LinesToStr([ // statements
  5489. 'this.DoIt = function (a) {',
  5490. ' var Proc = null;',
  5491. ' Proc = function (b) {',
  5492. ' };',
  5493. '};',
  5494. 'this.Proc = null;',
  5495. '']),
  5496. LinesToStr([
  5497. '$mod.Proc = function (c) {',
  5498. '};',
  5499. '']));
  5500. end;
  5501. procedure TTestModule.TestAnonymousProc_Arg;
  5502. begin
  5503. StartProgram(false);
  5504. Add([
  5505. 'type',
  5506. ' TProc = reference to procedure;',
  5507. ' TFunc = reference to function(x: word): word;',
  5508. 'procedure DoMore(f,g: TProc);',
  5509. 'begin',
  5510. 'end;',
  5511. 'procedure DoOdd(v: jsvalue);',
  5512. 'begin',
  5513. 'end;',
  5514. 'procedure DoIt(f: TFunc);',
  5515. 'begin',
  5516. ' DoIt(function(b:word): word',
  5517. ' begin',
  5518. ' Result:=1+b;',
  5519. ' end);',
  5520. ' DoMore(procedure begin end, procedure begin end);',
  5521. ' DoOdd(procedure begin end);',
  5522. 'end;',
  5523. 'begin',
  5524. ' DoMore(procedure begin end,',
  5525. ' procedure assembler asm',
  5526. ' console.log("c");',
  5527. ' end);',
  5528. '']);
  5529. ConvertProgram;
  5530. CheckSource('TestAnonymousProc_Arg',
  5531. LinesToStr([ // statements
  5532. 'this.DoMore = function (f, g) {',
  5533. '};',
  5534. 'this.DoOdd = function (v) {',
  5535. '};',
  5536. 'this.DoIt = function (f) {',
  5537. ' $mod.DoIt(function (b) {',
  5538. ' var Result = 0;',
  5539. ' Result = 1 + b;',
  5540. ' return Result;',
  5541. ' });',
  5542. ' $mod.DoMore(function () {',
  5543. ' }, function () {',
  5544. ' });',
  5545. ' $mod.DoOdd(function () {',
  5546. ' });',
  5547. '};',
  5548. '']),
  5549. LinesToStr([
  5550. '$mod.DoMore(function () {',
  5551. '}, function () {',
  5552. ' console.log("c");',
  5553. '});',
  5554. '']));
  5555. end;
  5556. procedure TTestModule.TestAnonymousProc_Typecast;
  5557. begin
  5558. StartProgram(false);
  5559. Add([
  5560. 'type',
  5561. ' TProc = reference to procedure(w: word);',
  5562. ' TArr = array of word;',
  5563. ' TFuncArr = reference to function: TArr;',
  5564. 'procedure DoIt(p: TProc);',
  5565. 'var',
  5566. ' w: word;',
  5567. ' a: TArr;',
  5568. 'begin',
  5569. ' p:=TProc(procedure(b: smallint) begin end);',
  5570. ' a:=TFuncArr(function: TArr begin end)();',
  5571. ' w:=TFuncArr(function: TArr begin end)()[3];',
  5572. 'end;',
  5573. 'begin']);
  5574. ConvertProgram;
  5575. CheckSource('TestAnonymousProc_Typecast',
  5576. LinesToStr([ // statements
  5577. 'this.DoIt = function (p) {',
  5578. ' var w = 0;',
  5579. ' var a = [];',
  5580. ' p = function (b) {',
  5581. ' };',
  5582. ' a = function () {',
  5583. ' var Result = [];',
  5584. ' return Result;',
  5585. ' }();',
  5586. ' w = function () {',
  5587. ' var Result = [];',
  5588. ' return Result;',
  5589. ' }()[3];',
  5590. '};',
  5591. '']),
  5592. LinesToStr([
  5593. '']));
  5594. end;
  5595. procedure TTestModule.TestAnonymousProc_With;
  5596. begin
  5597. StartProgram(false);
  5598. Add([
  5599. 'type',
  5600. ' TProc = reference to procedure(w: word);',
  5601. ' TObject = class',
  5602. ' b: boolean;',
  5603. ' end;',
  5604. 'var',
  5605. ' p: TProc;',
  5606. ' bird: TObject;',
  5607. 'begin',
  5608. ' with bird do',
  5609. ' p:=procedure(w: word)',
  5610. ' begin',
  5611. ' b:=w>2;',
  5612. ' end;',
  5613. '']);
  5614. ConvertProgram;
  5615. CheckSource('TestAnonymousProc_With',
  5616. LinesToStr([ // statements
  5617. 'rtl.createClass(this, "TObject", null, function () {',
  5618. ' this.$init = function () {',
  5619. ' this.b = false;',
  5620. ' };',
  5621. ' this.$final = function () {',
  5622. ' };',
  5623. '});',
  5624. 'this.p = null;',
  5625. 'this.bird = null;',
  5626. '']),
  5627. LinesToStr([
  5628. 'var $with = $mod.bird;',
  5629. '$mod.p = function (w) {',
  5630. ' $with.b = w > 2;',
  5631. '};',
  5632. '']));
  5633. end;
  5634. procedure TTestModule.TestAnonymousProc_ExceptOn;
  5635. begin
  5636. StartProgram(false);
  5637. Add([
  5638. 'type',
  5639. ' TProc = reference to procedure;',
  5640. ' TObject = class',
  5641. ' b: boolean;',
  5642. ' end;',
  5643. 'procedure DoIt;',
  5644. 'var',
  5645. ' p: TProc;',
  5646. 'begin',
  5647. ' try',
  5648. ' except',
  5649. ' on E: TObject do',
  5650. ' p:=procedure',
  5651. ' begin',
  5652. ' E.b:=true;',
  5653. ' end;',
  5654. ' end;',
  5655. 'end;',
  5656. 'begin']);
  5657. ConvertProgram;
  5658. CheckSource('TestAnonymousProc_ExceptOn',
  5659. LinesToStr([ // statements
  5660. 'rtl.createClass(this, "TObject", null, function () {',
  5661. ' this.$init = function () {',
  5662. ' this.b = false;',
  5663. ' };',
  5664. ' this.$final = function () {',
  5665. ' };',
  5666. '});',
  5667. 'this.DoIt = function () {',
  5668. ' var p = null;',
  5669. ' try {} catch ($e) {',
  5670. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5671. ' var E = $e;',
  5672. ' p = function () {',
  5673. ' E.b = true;',
  5674. ' };',
  5675. ' } else throw $e',
  5676. ' };',
  5677. '};',
  5678. '']),
  5679. LinesToStr([
  5680. '']));
  5681. end;
  5682. procedure TTestModule.TestAnonymousProc_Nested;
  5683. begin
  5684. StartProgram(false);
  5685. Add([
  5686. 'type',
  5687. ' TProc = reference to procedure;',
  5688. ' TObject = class',
  5689. ' i: byte;',
  5690. ' procedure DoIt;',
  5691. ' end;',
  5692. 'procedure TObject.DoIt;',
  5693. 'var',
  5694. ' p: TProc;',
  5695. ' procedure Sub;',
  5696. ' begin',
  5697. ' p:=procedure',
  5698. ' begin',
  5699. ' i:=3;',
  5700. ' Self.i:=4;',
  5701. ' p:=procedure',
  5702. ' procedure SubSub;',
  5703. ' begin',
  5704. ' i:=13;',
  5705. ' Self.i:=14;',
  5706. ' end;',
  5707. ' begin',
  5708. ' i:=13;',
  5709. ' Self.i:=14;',
  5710. ' end;',
  5711. ' end;',
  5712. ' end;',
  5713. 'begin',
  5714. 'end;',
  5715. 'begin']);
  5716. ConvertProgram;
  5717. CheckSource('TestAnonymousProc_Nested',
  5718. LinesToStr([ // statements
  5719. 'rtl.createClass(this, "TObject", null, function () {',
  5720. ' this.$init = function () {',
  5721. ' this.i = 0;',
  5722. ' };',
  5723. ' this.$final = function () {',
  5724. ' };',
  5725. ' this.DoIt = function () {',
  5726. ' var $Self = this;',
  5727. ' var p = null;',
  5728. ' function Sub() {',
  5729. ' p = function () {',
  5730. ' $Self.i = 3;',
  5731. ' $Self.i = 4;',
  5732. ' p = function () {',
  5733. ' function SubSub() {',
  5734. ' $Self.i = 13;',
  5735. ' $Self.i = 14;',
  5736. ' };',
  5737. ' $Self.i = 13;',
  5738. ' $Self.i = 14;',
  5739. ' };',
  5740. ' };',
  5741. ' };',
  5742. ' };',
  5743. '});',
  5744. '']),
  5745. LinesToStr([
  5746. '']));
  5747. end;
  5748. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5749. begin
  5750. StartProgram(false);
  5751. Add([
  5752. 'type',
  5753. ' TProc = reference to procedure;',
  5754. 'function DoIt: TProc;',
  5755. ' function Sub: TProc;',
  5756. ' begin',
  5757. ' Result:=procedure',
  5758. ' begin',
  5759. ' Sub:=procedure',
  5760. ' procedure SubSub;',
  5761. ' begin',
  5762. ' Result:=nil;',
  5763. ' Sub:=nil;',
  5764. ' DoIt:=nil;',
  5765. ' end;',
  5766. ' begin',
  5767. ' Result:=nil;',
  5768. ' Sub:=nil;',
  5769. ' DoIt:=nil;',
  5770. ' end;',
  5771. ' end;',
  5772. ' end;',
  5773. 'begin',
  5774. 'end;',
  5775. 'begin']);
  5776. ConvertProgram;
  5777. CheckSource('TestAnonymousProc_NestedAssignResult',
  5778. LinesToStr([ // statements
  5779. 'this.DoIt = function () {',
  5780. ' var Result = null;',
  5781. ' function Sub() {',
  5782. ' var Result$1 = null;',
  5783. ' Result$1 = function () {',
  5784. ' Result$1 = function () {',
  5785. ' function SubSub() {',
  5786. ' Result$1 = null;',
  5787. ' Result$1 = null;',
  5788. ' Result = null;',
  5789. ' };',
  5790. ' Result$1 = null;',
  5791. ' Result$1 = null;',
  5792. ' Result = null;',
  5793. ' };',
  5794. ' };',
  5795. ' return Result$1;',
  5796. ' };',
  5797. ' return Result;',
  5798. '};',
  5799. '']),
  5800. LinesToStr([
  5801. '']));
  5802. end;
  5803. procedure TTestModule.TestAnonymousProc_Class;
  5804. begin
  5805. StartProgram(false);
  5806. Add([
  5807. 'type',
  5808. ' TProc = reference to procedure;',
  5809. ' TEvent = procedure of object;',
  5810. ' TObject = class',
  5811. ' Size: word;',
  5812. ' function GetIt: TProc;',
  5813. ' procedure DoIt; virtual; abstract;',
  5814. ' end;',
  5815. 'function TObject.GetIt: TProc;',
  5816. 'begin',
  5817. ' Result:=procedure',
  5818. ' var p: TEvent;',
  5819. ' begin',
  5820. ' Size:=Size;',
  5821. ' Size:=Self.Size;',
  5822. ' p:=@DoIt;',
  5823. ' p:[email protected];',
  5824. ' end;',
  5825. 'end;',
  5826. 'begin']);
  5827. ConvertProgram;
  5828. CheckSource('TestAnonymousProc_Class',
  5829. LinesToStr([ // statements
  5830. 'rtl.createClass(this, "TObject", null, function () {',
  5831. ' this.$init = function () {',
  5832. ' this.Size = 0;',
  5833. ' };',
  5834. ' this.$final = function () {',
  5835. ' };',
  5836. ' this.GetIt = function () {',
  5837. ' var $Self = this;',
  5838. ' var Result = null;',
  5839. ' Result = function () {',
  5840. ' var p = null;',
  5841. ' $Self.Size = $Self.Size;',
  5842. ' $Self.Size = $Self.Size;',
  5843. ' p = rtl.createCallback($Self, "DoIt");',
  5844. ' p = rtl.createCallback($Self, "DoIt");',
  5845. ' };',
  5846. ' return Result;',
  5847. ' };',
  5848. '});',
  5849. '']),
  5850. LinesToStr([
  5851. '']));
  5852. end;
  5853. procedure TTestModule.TestAnonymousProc_ForLoop;
  5854. begin
  5855. StartProgram(false);
  5856. Add([
  5857. 'type TProc = reference to procedure;',
  5858. 'procedure Foo(p: TProc);',
  5859. 'begin',
  5860. 'end;',
  5861. 'procedure DoIt;',
  5862. 'var i: word;',
  5863. ' a: word;',
  5864. 'begin',
  5865. ' for i:=1 to 10 do begin',
  5866. ' Foo(procedure begin a:=3; end);',
  5867. ' end;',
  5868. 'end;',
  5869. 'begin',
  5870. ' DoIt;']);
  5871. ConvertProgram;
  5872. CheckSource('TestAnonymousProc_ForLoop',
  5873. LinesToStr([ // statements
  5874. 'this.Foo = function (p) {',
  5875. '};',
  5876. 'this.DoIt = function () {',
  5877. ' var i = 0;',
  5878. ' var a = 0;',
  5879. ' for (i = 1; i <= 10; i++) {',
  5880. ' $mod.Foo(function () {',
  5881. ' a = 3;',
  5882. ' });',
  5883. ' };',
  5884. '};',
  5885. '']),
  5886. LinesToStr([
  5887. '$mod.DoIt();'
  5888. ]));
  5889. end;
  5890. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5891. begin
  5892. StartProgram(false);
  5893. Add([
  5894. '{$mode delphi}',
  5895. 'type',
  5896. ' TProc = reference to procedure;',
  5897. ' TFunc = reference to function(x: word): word;',
  5898. 'procedure Run;',
  5899. 'asm',
  5900. 'end;',
  5901. 'procedure Walk(p: TProc; f: TFunc);',
  5902. 'begin',
  5903. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5904. 'end;',
  5905. 'begin',
  5906. ' Walk(procedure',
  5907. ' asm',
  5908. ' console.log("a");',
  5909. ' end,',
  5910. ' function(x: word): word asm',
  5911. ' console.log("c");',
  5912. ' end);',
  5913. '']);
  5914. ConvertProgram;
  5915. CheckSource('TestAnonymousProc_AsmDelphi',
  5916. LinesToStr([ // statements
  5917. 'this.Run = function () {',
  5918. '};',
  5919. 'this.Walk = function (p, f) {',
  5920. ' $mod.Walk(function () {',
  5921. ' }, function (b) {',
  5922. ' return 1+b;',
  5923. ' });',
  5924. '};',
  5925. '']),
  5926. LinesToStr([
  5927. '$mod.Walk(function () {',
  5928. ' console.log("a");',
  5929. '}, function (x) {',
  5930. ' console.log("c");',
  5931. '});',
  5932. '']));
  5933. end;
  5934. procedure TTestModule.TestEnum_Name;
  5935. begin
  5936. StartProgram(false);
  5937. Add('type TMyEnum = (Red, Green, Blue);');
  5938. Add('var e: TMyEnum;');
  5939. Add('var f: TMyEnum = Blue;');
  5940. Add('begin');
  5941. Add(' e:=green;');
  5942. Add(' e:=default(TMyEnum);');
  5943. ConvertProgram;
  5944. CheckSource('TestEnum_Name',
  5945. LinesToStr([ // statements
  5946. 'this.TMyEnum = {',
  5947. ' "0":"Red",',
  5948. ' Red:0,',
  5949. ' "1":"Green",',
  5950. ' Green:1,',
  5951. ' "2":"Blue",',
  5952. ' Blue:2',
  5953. ' };',
  5954. 'this.e = 0;',
  5955. 'this.f = this.TMyEnum.Blue;'
  5956. ]),
  5957. LinesToStr([
  5958. '$mod.e=$mod.TMyEnum.Green;',
  5959. '$mod.e=$mod.TMyEnum.Red;'
  5960. ]));
  5961. end;
  5962. procedure TTestModule.TestEnum_Number;
  5963. begin
  5964. Converter.Options:=Converter.Options+[coEnumNumbers];
  5965. StartProgram(false);
  5966. Add('type TMyEnum = (Red, Green);');
  5967. Add('var');
  5968. Add(' e: TMyEnum;');
  5969. Add(' f: TMyEnum = Green;');
  5970. Add(' i: longint;');
  5971. Add('begin');
  5972. Add(' e:=green;');
  5973. Add(' i:=longint(e);');
  5974. ConvertProgram;
  5975. CheckSource('TestEnumNumber',
  5976. LinesToStr([ // statements
  5977. 'this.TMyEnum = {',
  5978. ' "0":"Red",',
  5979. ' Red:0,',
  5980. ' "1":"Green",',
  5981. ' Green:1',
  5982. ' };',
  5983. 'this.e = 0;',
  5984. 'this.f = 1;',
  5985. 'this.i = 0;'
  5986. ]),
  5987. LinesToStr([
  5988. '$mod.e=1;',
  5989. '$mod.i=$mod.e;'
  5990. ]));
  5991. end;
  5992. procedure TTestModule.TestEnum_ConstFail;
  5993. begin
  5994. StartProgram(false);
  5995. Add([
  5996. 'type TMyEnum = (Red = 100, Green = 101);',
  5997. 'var',
  5998. ' e: TMyEnum;',
  5999. ' f: TMyEnum = Green;',
  6000. 'begin',
  6001. ' e:=green;']);
  6002. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  6003. ConvertProgram;
  6004. end;
  6005. procedure TTestModule.TestEnum_Functions;
  6006. begin
  6007. StartProgram(false);
  6008. Add([
  6009. 'type TMyEnum = (Red, Green);',
  6010. 'procedure DoIt(var e: TMyEnum; var i: word);',
  6011. 'var',
  6012. ' v: longint;',
  6013. ' s: string;',
  6014. 'begin',
  6015. ' val(s,e,v);',
  6016. ' val(s,e,i);',
  6017. 'end;',
  6018. 'var',
  6019. ' e: TMyEnum;',
  6020. ' i: longint;',
  6021. ' s: string;',
  6022. ' b: boolean;',
  6023. 'begin',
  6024. ' i:=ord(red);',
  6025. ' i:=ord(green);',
  6026. ' i:=ord(e);',
  6027. ' i:=ord(b);',
  6028. ' e:=low(tmyenum);',
  6029. ' e:=low(e);',
  6030. ' b:=low(boolean);',
  6031. ' e:=high(tmyenum);',
  6032. ' e:=high(e);',
  6033. ' b:=high(boolean);',
  6034. ' e:=pred(green);',
  6035. ' e:=pred(e);',
  6036. ' b:=pred(b);',
  6037. ' e:=succ(red);',
  6038. ' e:=succ(e);',
  6039. ' b:=succ(b);',
  6040. ' e:=tmyenum(1);',
  6041. ' e:=tmyenum(i);',
  6042. ' s:=str(e);',
  6043. ' str(e,s);',
  6044. ' str(red,s);',
  6045. ' s:=str(e:3);',
  6046. ' writestr(s,e:3,red);',
  6047. ' val(s,e,i);',
  6048. ' i:=longint(e);']);
  6049. ConvertProgram;
  6050. CheckSource('TestEnum_Functions',
  6051. LinesToStr([ // statements
  6052. 'this.TMyEnum = {',
  6053. ' "0":"Red",',
  6054. ' Red:0,',
  6055. ' "1":"Green",',
  6056. ' Green:1',
  6057. ' };',
  6058. 'this.DoIt = function (e, i) {',
  6059. ' var v = 0;',
  6060. ' var s = "";',
  6061. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  6062. ' v = w;',
  6063. ' }));',
  6064. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  6065. '};',
  6066. 'this.e = 0;',
  6067. 'this.i = 0;',
  6068. 'this.s = "";',
  6069. 'this.b = false;',
  6070. '']),
  6071. LinesToStr([
  6072. '$mod.i=$mod.TMyEnum.Red;',
  6073. '$mod.i=$mod.TMyEnum.Green;',
  6074. '$mod.i=$mod.e;',
  6075. '$mod.i=$mod.b+0;',
  6076. '$mod.e=$mod.TMyEnum.Red;',
  6077. '$mod.e=$mod.TMyEnum.Red;',
  6078. '$mod.b=false;',
  6079. '$mod.e=$mod.TMyEnum.Green;',
  6080. '$mod.e=$mod.TMyEnum.Green;',
  6081. '$mod.b=true;',
  6082. '$mod.e=$mod.TMyEnum.Green-1;',
  6083. '$mod.e=$mod.e-1;',
  6084. '$mod.b=false;',
  6085. '$mod.e=$mod.TMyEnum.Red+1;',
  6086. '$mod.e=$mod.e+1;',
  6087. '$mod.b=true;',
  6088. '$mod.e=1;',
  6089. '$mod.e=$mod.i;',
  6090. '$mod.s = $mod.TMyEnum[$mod.e];',
  6091. '$mod.s = $mod.TMyEnum[$mod.e];',
  6092. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  6093. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  6094. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  6095. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  6096. ' $mod.i = v;',
  6097. '});',
  6098. '$mod.i=$mod.e;',
  6099. '']));
  6100. end;
  6101. procedure TTestModule.TestEnumRg_Functions;
  6102. begin
  6103. StartProgram(false);
  6104. Add([
  6105. 'type',
  6106. ' TEnum = (Red, Green, Blue);',
  6107. ' TEnumRg = Green..Blue;',
  6108. 'procedure DoIt(var e: TEnumRg; var i: word);',
  6109. 'var',
  6110. ' v: longint;',
  6111. ' s: string;',
  6112. 'begin',
  6113. ' val(s,e,v);',
  6114. ' val(s,e,i);',
  6115. 'end;',
  6116. 'var',
  6117. ' e: TEnumRg;',
  6118. ' i: longint;',
  6119. ' s: string;',
  6120. 'begin',
  6121. ' i:=ord(green);',
  6122. ' i:=ord(e);',
  6123. ' e:=low(tenumrg);',
  6124. ' e:=low(e);',
  6125. ' e:=high(tenumrg);',
  6126. ' e:=high(e);',
  6127. ' e:=pred(blue);',
  6128. ' e:=pred(e);',
  6129. ' e:=succ(green);',
  6130. ' e:=succ(e);',
  6131. ' e:=tenumrg(1);',
  6132. ' e:=tenumrg(i);',
  6133. ' s:=str(e);',
  6134. ' str(e,s);',
  6135. ' str(red,s);',
  6136. ' s:=str(e:3);',
  6137. ' writestr(s,e:3,blue);',
  6138. ' val(s,e,i);',
  6139. ' i:=longint(e);']);
  6140. ConvertProgram;
  6141. CheckSource('TestEnumRg_Functions',
  6142. LinesToStr([ // statements
  6143. 'this.TEnum = {',
  6144. ' "0":"Red",',
  6145. ' Red:0,',
  6146. ' "1":"Green",',
  6147. ' Green:1,',
  6148. ' "2":"Blue",',
  6149. ' Blue:2',
  6150. ' };',
  6151. 'this.DoIt = function (e, i) {',
  6152. ' var v = 0;',
  6153. ' var s = "";',
  6154. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  6155. ' v = w;',
  6156. ' }));',
  6157. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  6158. '};',
  6159. 'this.e = this.TEnum.Green;',
  6160. 'this.i = 0;',
  6161. 'this.s = "";',
  6162. '']),
  6163. LinesToStr([
  6164. '$mod.i=$mod.TEnum.Green;',
  6165. '$mod.i=$mod.e;',
  6166. '$mod.e=$mod.TEnum.Green;',
  6167. '$mod.e=$mod.TEnum.Green;',
  6168. '$mod.e=$mod.TEnum.Blue;',
  6169. '$mod.e=$mod.TEnum.Blue;',
  6170. '$mod.e=$mod.TEnum.Blue-1;',
  6171. '$mod.e=$mod.e-1;',
  6172. '$mod.e=$mod.TEnum.Green+1;',
  6173. '$mod.e=$mod.e+1;',
  6174. '$mod.e=1;',
  6175. '$mod.e=$mod.i;',
  6176. '$mod.s = $mod.TEnum[$mod.e];',
  6177. '$mod.s = $mod.TEnum[$mod.e];',
  6178. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  6179. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  6180. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  6181. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  6182. ' $mod.i = v;',
  6183. '});',
  6184. '$mod.i=$mod.e;',
  6185. '']));
  6186. end;
  6187. procedure TTestModule.TestEnum_AsParams;
  6188. begin
  6189. StartProgram(false);
  6190. Add('type TEnum = (Red,Blue);');
  6191. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  6192. Add('var vJ: TEnum;');
  6193. Add('begin');
  6194. Add(' vg:=vg;');
  6195. Add(' vj:=vh;');
  6196. Add(' vi:=vi;');
  6197. Add(' doit(vg,vg,vg);');
  6198. Add(' doit(vh,vh,vj);');
  6199. Add(' doit(vi,vi,vi);');
  6200. Add(' doit(vj,vj,vj);');
  6201. Add('end;');
  6202. Add('var i: TEnum;');
  6203. Add('begin');
  6204. Add(' doit(i,i,i);');
  6205. ConvertProgram;
  6206. CheckSource('TestEnum_AsParams',
  6207. LinesToStr([ // statements
  6208. 'this.TEnum = {',
  6209. ' "0": "Red",',
  6210. ' Red: 0,',
  6211. ' "1": "Blue",',
  6212. ' Blue: 1',
  6213. '};',
  6214. 'this.DoIt = function (vG,vH,vI) {',
  6215. ' var vJ = 0;',
  6216. ' vG = vG;',
  6217. ' vJ = vH;',
  6218. ' vI.set(vI.get());',
  6219. ' $mod.DoIt(vG, vG, {',
  6220. ' get: function () {',
  6221. ' return vG;',
  6222. ' },',
  6223. ' set: function (v) {',
  6224. ' vG = v;',
  6225. ' }',
  6226. ' });',
  6227. ' $mod.DoIt(vH, vH, {',
  6228. ' get: function () {',
  6229. ' return vJ;',
  6230. ' },',
  6231. ' set: function (v) {',
  6232. ' vJ = v;',
  6233. ' }',
  6234. ' });',
  6235. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  6236. ' $mod.DoIt(vJ, vJ, {',
  6237. ' get: function () {',
  6238. ' return vJ;',
  6239. ' },',
  6240. ' set: function (v) {',
  6241. ' vJ = v;',
  6242. ' }',
  6243. ' });',
  6244. '};',
  6245. 'this.i = 0;'
  6246. ]),
  6247. LinesToStr([
  6248. '$mod.DoIt($mod.i,$mod.i,{',
  6249. ' p: $mod,',
  6250. ' get: function () {',
  6251. ' return this.p.i;',
  6252. ' },',
  6253. ' set: function (v) {',
  6254. ' this.p.i = v;',
  6255. ' }',
  6256. '});'
  6257. ]));
  6258. end;
  6259. procedure TTestModule.TestEnumRange_Array;
  6260. begin
  6261. StartProgram(false);
  6262. Add([
  6263. 'type',
  6264. ' TEnum = (Red, Green, Blue);',
  6265. ' TEnumRg = green..blue;',
  6266. ' TArr = array[TEnumRg] of byte;',
  6267. ' TArr2 = array[green..blue] of byte;',
  6268. 'var',
  6269. ' a: TArr;',
  6270. ' b: TArr = (3,4);',
  6271. ' c: TArr2 = (5,6);',
  6272. 'begin',
  6273. ' a[green] := b[blue];',
  6274. ' c[green] := c[blue];',
  6275. '']);
  6276. ConvertProgram;
  6277. CheckSource('TestEnumRange_Array',
  6278. LinesToStr([ // statements
  6279. 'this.TEnum = {',
  6280. ' "0": "Red",',
  6281. ' Red: 0,',
  6282. ' "1": "Green",',
  6283. ' Green: 1,',
  6284. ' "2": "Blue",',
  6285. ' Blue: 2',
  6286. '};',
  6287. 'this.a = rtl.arraySetLength(null, 0, 2);',
  6288. 'this.b = [3, 4];',
  6289. 'this.c = [5, 6];',
  6290. '']),
  6291. LinesToStr([
  6292. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  6293. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  6294. '']));
  6295. end;
  6296. procedure TTestModule.TestEnum_ForIn;
  6297. begin
  6298. StartProgram(false);
  6299. Add([
  6300. 'type',
  6301. ' TEnum = (Red, Green, Blue);',
  6302. ' TEnumRg = green..blue;',
  6303. ' TArr = array[TEnum] of byte;',
  6304. ' TArrRg = array[TEnumRg] of byte;',
  6305. 'var',
  6306. ' e: TEnum;',
  6307. ' a1: TArr = (3,4,5);',
  6308. ' a2: TArrRg = (11,12);',
  6309. ' b: byte;',
  6310. 'begin',
  6311. ' for e in TEnum do ;',
  6312. ' for e in TEnumRg do ;',
  6313. ' for e in TArr do ;',
  6314. ' for e in TArrRg do ;',
  6315. ' for b in a1 do ;',
  6316. ' for b in a2 do ;',
  6317. '']);
  6318. ConvertProgram;
  6319. CheckSource('TestEnum_ForIn',
  6320. LinesToStr([ // statements
  6321. 'this.TEnum = {',
  6322. ' "0": "Red",',
  6323. ' Red: 0,',
  6324. ' "1": "Green",',
  6325. ' Green: 1,',
  6326. ' "2": "Blue",',
  6327. ' Blue: 2',
  6328. '};',
  6329. 'this.e = 0;',
  6330. 'this.a1 = [3, 4, 5];',
  6331. 'this.a2 = [11, 12];',
  6332. 'this.b = 0;',
  6333. '']),
  6334. LinesToStr([
  6335. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6336. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6337. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6338. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6339. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  6340. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  6341. '']));
  6342. end;
  6343. procedure TTestModule.TestEnum_ScopedNumber;
  6344. begin
  6345. Converter.Options:=Converter.Options+[coEnumNumbers];
  6346. StartProgram(false);
  6347. Add([
  6348. 'type',
  6349. ' TEnum = (Red, Green);',
  6350. 'var',
  6351. ' e: TEnum;',
  6352. 'begin',
  6353. ' e:=TEnum.Green;',
  6354. '']);
  6355. ConvertProgram;
  6356. CheckSource('TestEnum_ScopedNumber',
  6357. LinesToStr([ // statements
  6358. 'this.TEnum = {',
  6359. ' "0": "Red",',
  6360. ' Red: 0,',
  6361. ' "1": "Green",',
  6362. ' Green: 1',
  6363. '};',
  6364. 'this.e = 0;',
  6365. '']),
  6366. LinesToStr([
  6367. '$mod.e = 1;']));
  6368. end;
  6369. procedure TTestModule.TestEnum_InFunction;
  6370. begin
  6371. StartProgram(false);
  6372. Add([
  6373. 'const TEnum = 3;',
  6374. 'procedure DoIt;',
  6375. 'type',
  6376. ' TEnum = (Red, Green, Blue);',
  6377. ' procedure Sub;',
  6378. ' type',
  6379. ' TEnumSub = (Left, Right);',
  6380. ' var',
  6381. ' es: TEnumSub;',
  6382. ' begin',
  6383. ' es:=Left;',
  6384. ' end;',
  6385. 'var',
  6386. ' e, e2: TEnum;',
  6387. 'begin',
  6388. ' if e in [red,blue] then e2:=e;',
  6389. 'end;',
  6390. 'begin']);
  6391. ConvertProgram;
  6392. CheckSource('TestEnum_InFunction',
  6393. LinesToStr([ // statements
  6394. 'this.TEnum = 3;',
  6395. 'var TEnum$1 = {',
  6396. ' "0":"Red",',
  6397. ' Red:0,',
  6398. ' "1":"Green",',
  6399. ' Green:1,',
  6400. ' "2":"Blue",',
  6401. ' Blue:2',
  6402. ' };',
  6403. 'var TEnumSub = {',
  6404. ' "0": "Left",',
  6405. ' Left: 0,',
  6406. ' "1": "Right",',
  6407. ' Right: 1',
  6408. '};',
  6409. 'this.DoIt = function () {',
  6410. ' function Sub() {',
  6411. ' var es = 0;',
  6412. ' es = TEnumSub.Left;',
  6413. ' };',
  6414. ' var e = 0;',
  6415. ' var e2 = 0;',
  6416. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  6417. '};',
  6418. '']),
  6419. LinesToStr([
  6420. '']));
  6421. end;
  6422. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  6423. begin
  6424. StartUnit(true);
  6425. Add([
  6426. 'interface',
  6427. 'var color: (red, green);',
  6428. 'implementation',
  6429. 'initialization',
  6430. ' color:=green;',
  6431. '']);
  6432. ConvertUnit;
  6433. CheckSource('TestEnum_Name_Anonymous_Unit',
  6434. LinesToStr([
  6435. 'this.color$a = {',
  6436. ' "0": "red",',
  6437. ' red: 0,',
  6438. ' "1": "green",',
  6439. ' green: 1',
  6440. '};',
  6441. 'this.color = 0;',
  6442. '']),
  6443. LinesToStr([ // this.$init
  6444. '$mod.color = $mod.color$a.green;',
  6445. '']),
  6446. LinesToStr([ // implementation
  6447. '']) );
  6448. end;
  6449. procedure TTestModule.TestSet_Enum;
  6450. begin
  6451. StartProgram(false);
  6452. Add([
  6453. 'type',
  6454. ' TColor = (Red, Green, Blue);',
  6455. ' TColors = set of TColor;',
  6456. 'var',
  6457. ' c: TColor;',
  6458. ' s: TColors;',
  6459. ' t: TColors = [];',
  6460. ' u: TColors = [Red];',
  6461. 'begin',
  6462. ' s:=[];',
  6463. ' s:=[Green];',
  6464. ' s:=[Green,Blue];',
  6465. ' s:=[Red..Blue];',
  6466. ' s:=[Red,Green..Blue];',
  6467. ' s:=[Red,c];',
  6468. ' s:=t;',
  6469. ' s:=default(TColors);',
  6470. '']);
  6471. ConvertProgram;
  6472. CheckSource('TestSet',
  6473. LinesToStr([ // statements
  6474. 'this.TColor = {',
  6475. ' "0":"Red",',
  6476. ' Red:0,',
  6477. ' "1":"Green",',
  6478. ' Green:1,',
  6479. ' "2":"Blue",',
  6480. ' Blue:2',
  6481. ' };',
  6482. 'this.c = 0;',
  6483. 'this.s = {};',
  6484. 'this.t = {};',
  6485. 'this.u = rtl.createSet(this.TColor.Red);'
  6486. ]),
  6487. LinesToStr([
  6488. '$mod.s={};',
  6489. '$mod.s=rtl.createSet($mod.TColor.Green);',
  6490. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  6491. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  6492. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  6493. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  6494. '$mod.s=rtl.refSet($mod.t);',
  6495. '$mod.s={};',
  6496. '']));
  6497. end;
  6498. procedure TTestModule.TestSet_Operators;
  6499. begin
  6500. StartProgram(false);
  6501. Add('type');
  6502. Add(' TColor = (Red, Green, Blue);');
  6503. Add(' TColors = set of tcolor;');
  6504. Add('var');
  6505. Add(' vC: TColor;');
  6506. Add(' vS: TColors;');
  6507. Add(' vT: TColors;');
  6508. Add(' vU: TColors;');
  6509. Add(' B: boolean;');
  6510. Add('begin');
  6511. Add(' include(vs,green);');
  6512. Add(' exclude(vs,vc);');
  6513. Add(' vs:=vt+vu;');
  6514. Add(' vs:=vt+[red];');
  6515. Add(' vs:=[red]+vt;');
  6516. Add(' vs:=[red]+[green];');
  6517. Add(' vs:=vt-vu;');
  6518. Add(' vs:=vt-[red];');
  6519. Add(' vs:=[red]-vt;');
  6520. Add(' vs:=[red]-[green];');
  6521. Add(' vs:=vt*vu;');
  6522. Add(' vs:=vt*[red];');
  6523. Add(' vs:=[red]*vt;');
  6524. Add(' vs:=[red]*[green];');
  6525. Add(' vs:=vt><vu;');
  6526. Add(' vs:=vt><[red];');
  6527. Add(' vs:=[red]><vt;');
  6528. Add(' vs:=[red]><[green];');
  6529. Add(' b:=vt=vu;');
  6530. Add(' b:=vt=[red];');
  6531. Add(' b:=[red]=vt;');
  6532. Add(' b:=[red]=[green];');
  6533. Add(' b:=vt<>vu;');
  6534. Add(' b:=vt<>[red];');
  6535. Add(' b:=[red]<>vt;');
  6536. Add(' b:=[red]<>[green];');
  6537. Add(' b:=vt<=vu;');
  6538. Add(' b:=vt<=[red];');
  6539. Add(' b:=[red]<=vt;');
  6540. Add(' b:=[red]<=[green];');
  6541. Add(' b:=vt>=vu;');
  6542. Add(' b:=vt>=[red];');
  6543. Add(' b:=[red]>=vt;');
  6544. Add(' b:=[red]>=[green];');
  6545. ConvertProgram;
  6546. CheckSource('TestSet_Operators',
  6547. LinesToStr([ // statements
  6548. 'this.TColor = {',
  6549. ' "0":"Red",',
  6550. ' Red:0,',
  6551. ' "1":"Green",',
  6552. ' Green:1,',
  6553. ' "2":"Blue",',
  6554. ' Blue:2',
  6555. ' };',
  6556. 'this.vC = 0;',
  6557. 'this.vS = {};',
  6558. 'this.vT = {};',
  6559. 'this.vU = {};',
  6560. 'this.B = false;'
  6561. ]),
  6562. LinesToStr([
  6563. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  6564. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  6565. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  6566. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6567. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6568. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6569. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  6570. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6571. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6572. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6573. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  6574. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6575. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6576. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6577. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  6578. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6579. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6580. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6581. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  6582. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6583. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6584. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6585. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  6586. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6587. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6588. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6589. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  6590. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6591. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6592. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6593. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  6594. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  6595. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  6596. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  6597. '']));
  6598. end;
  6599. procedure TTestModule.TestSet_Operator_In;
  6600. begin
  6601. StartProgram(false);
  6602. Add([
  6603. 'type',
  6604. ' TColor = (Red, Green, Blue);',
  6605. ' TColors = set of tcolor;',
  6606. ' TColorRg = green..blue;',
  6607. 'var',
  6608. ' vC: tcolor;',
  6609. ' vT: tcolors;',
  6610. ' B: boolean;',
  6611. ' rg: TColorRg;',
  6612. 'begin',
  6613. ' b:=red in vt;',
  6614. ' b:=vc in vt;',
  6615. ' b:=green in [red..blue];',
  6616. ' b:=vc in [red..blue];',
  6617. ' ',
  6618. ' if red in vt then ;',
  6619. ' while vC in vt do ;',
  6620. ' repeat',
  6621. ' until vC in vt;',
  6622. ' if rg in [green..blue] then ;',
  6623. '']);
  6624. ConvertProgram;
  6625. CheckSource('TestSet_Operator_In',
  6626. LinesToStr([ // statements
  6627. 'this.TColor = {',
  6628. ' "0":"Red",',
  6629. ' Red:0,',
  6630. ' "1":"Green",',
  6631. ' Green:1,',
  6632. ' "2":"Blue",',
  6633. ' Blue:2',
  6634. ' };',
  6635. 'this.vC = 0;',
  6636. 'this.vT = {};',
  6637. 'this.B = false;',
  6638. 'this.rg = this.TColor.Green;',
  6639. '']),
  6640. LinesToStr([
  6641. '$mod.B = $mod.TColor.Red in $mod.vT;',
  6642. '$mod.B = $mod.vC in $mod.vT;',
  6643. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6644. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  6645. 'if ($mod.TColor.Red in $mod.vT) ;',
  6646. 'while ($mod.vC in $mod.vT) {',
  6647. '};',
  6648. 'do {',
  6649. '} while (!($mod.vC in $mod.vT));',
  6650. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  6651. '']));
  6652. end;
  6653. procedure TTestModule.TestSet_Functions;
  6654. begin
  6655. StartProgram(false);
  6656. Add('type');
  6657. Add(' TMyEnum = (Red, Green);');
  6658. Add(' TMyEnums = set of TMyEnum;');
  6659. Add('var');
  6660. Add(' e: TMyEnum;');
  6661. Add(' s: TMyEnums;');
  6662. Add('begin');
  6663. Add(' e:=Low(TMyEnums);');
  6664. Add(' e:=Low(s);');
  6665. Add(' e:=High(TMyEnums);');
  6666. Add(' e:=High(s);');
  6667. ConvertProgram;
  6668. CheckSource('TestSetFunctions',
  6669. LinesToStr([ // statements
  6670. 'this.TMyEnum = {',
  6671. ' "0":"Red",',
  6672. ' Red:0,',
  6673. ' "1":"Green",',
  6674. ' Green:1',
  6675. ' };',
  6676. 'this.e = 0;',
  6677. 'this.s = {};'
  6678. ]),
  6679. LinesToStr([
  6680. '$mod.e=$mod.TMyEnum.Red;',
  6681. '$mod.e=$mod.TMyEnum.Red;',
  6682. '$mod.e=$mod.TMyEnum.Green;',
  6683. '$mod.e=$mod.TMyEnum.Green;',
  6684. '']));
  6685. end;
  6686. procedure TTestModule.TestSet_PassAsArgClone;
  6687. begin
  6688. StartProgram(false);
  6689. Add('type');
  6690. Add(' TMyEnum = (Red, Green);');
  6691. Add(' TMyEnums = set of TMyEnum;');
  6692. Add('procedure DoDefault(s: tmyenums); begin end;');
  6693. Add('procedure DoConst(const s: tmyenums); begin end;');
  6694. Add('var');
  6695. Add(' aSet: tmyenums;');
  6696. Add('begin');
  6697. Add(' dodefault(aset);');
  6698. Add(' doconst(aset);');
  6699. ConvertProgram;
  6700. CheckSource('TestSetFunctions',
  6701. LinesToStr([ // statements
  6702. 'this.TMyEnum = {',
  6703. ' "0":"Red",',
  6704. ' Red:0,',
  6705. ' "1":"Green",',
  6706. ' Green:1',
  6707. ' };',
  6708. 'this.DoDefault = function (s) {',
  6709. '};',
  6710. 'this.DoConst = function (s) {',
  6711. '};',
  6712. 'this.aSet = {};'
  6713. ]),
  6714. LinesToStr([
  6715. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6716. '$mod.DoConst($mod.aSet);',
  6717. '']));
  6718. end;
  6719. procedure TTestModule.TestSet_AsParams;
  6720. begin
  6721. StartProgram(false);
  6722. Add([
  6723. 'type TEnum = (Red,Blue);',
  6724. 'type TEnums = set of TEnum;',
  6725. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6726. 'var vJ: TEnums;',
  6727. 'begin',
  6728. ' Include(vg,red);',
  6729. ' Include(result,blue);',
  6730. ' vg:=vg;',
  6731. ' vj:=vh;',
  6732. ' vi:=vi;',
  6733. ' doit(vg,vg,vg);',
  6734. ' doit(vh,vh,vj);',
  6735. ' doit(vi,vi,vi);',
  6736. ' doit(vj,vj,vj);',
  6737. 'end;',
  6738. 'var i: TEnums;',
  6739. 'begin',
  6740. ' doit(i,i,i);']);
  6741. ConvertProgram;
  6742. CheckSource('TestSet_AsParams',
  6743. LinesToStr([ // statements
  6744. 'this.TEnum = {',
  6745. ' "0": "Red",',
  6746. ' Red: 0,',
  6747. ' "1": "Blue",',
  6748. ' Blue: 1',
  6749. '};',
  6750. 'this.DoIt = function (vG,vH,vI) {',
  6751. ' var Result = {};',
  6752. ' var vJ = {};',
  6753. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6754. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6755. ' vG = rtl.refSet(vG);',
  6756. ' vJ = rtl.refSet(vH);',
  6757. ' vI.set(rtl.refSet(vI.get()));',
  6758. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6759. ' get: function () {',
  6760. ' return vG;',
  6761. ' },',
  6762. ' set: function (v) {',
  6763. ' vG = v;',
  6764. ' }',
  6765. ' });',
  6766. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6767. ' get: function () {',
  6768. ' return vJ;',
  6769. ' },',
  6770. ' set: function (v) {',
  6771. ' vJ = v;',
  6772. ' }',
  6773. ' });',
  6774. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6775. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6776. ' get: function () {',
  6777. ' return vJ;',
  6778. ' },',
  6779. ' set: function (v) {',
  6780. ' vJ = v;',
  6781. ' }',
  6782. ' });',
  6783. ' return Result;',
  6784. '};',
  6785. 'this.i = {};'
  6786. ]),
  6787. LinesToStr([
  6788. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6789. ' p: $mod,',
  6790. ' get: function () {',
  6791. ' return this.p.i;',
  6792. ' },',
  6793. ' set: function (v) {',
  6794. ' this.p.i = v;',
  6795. ' }',
  6796. '});'
  6797. ]));
  6798. end;
  6799. procedure TTestModule.TestSet_Property;
  6800. begin
  6801. StartProgram(false);
  6802. Add('type');
  6803. Add(' TEnum = (Red,Blue);');
  6804. Add(' TEnums = set of TEnum;');
  6805. Add(' TObject = class');
  6806. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6807. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6808. Add(' property Colors: TEnums read GetColors write SetColors;');
  6809. Add(' end;');
  6810. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6811. Add('begin end;');
  6812. Add('var Obj: TObject;');
  6813. Add('begin');
  6814. Add(' Include(Obj.Colors,Red);');
  6815. Add(' Exclude(Obj.Colors,Red);');
  6816. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6817. ConvertProgram;
  6818. CheckSource('TestSet_Property',
  6819. LinesToStr([ // statements
  6820. 'this.TEnum = {',
  6821. ' "0": "Red",',
  6822. ' Red: 0,',
  6823. ' "1": "Blue",',
  6824. ' Blue: 1',
  6825. '};',
  6826. 'rtl.createClass(this, "TObject", null, function () {',
  6827. ' this.$init = function () {',
  6828. ' };',
  6829. ' this.$final = function () {',
  6830. ' };',
  6831. '});',
  6832. 'this.DoIt = function (i, j, k, l) {',
  6833. '};',
  6834. 'this.Obj = null;',
  6835. '']),
  6836. LinesToStr([
  6837. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6838. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6839. '']));
  6840. end;
  6841. procedure TTestModule.TestSet_EnumConst;
  6842. begin
  6843. StartProgram(false);
  6844. Add([
  6845. 'type',
  6846. ' TEnum = (Red,Blue);',
  6847. ' TEnums = set of TEnum;',
  6848. 'const',
  6849. ' Orange = red;',
  6850. 'var',
  6851. ' Enum: tenum;',
  6852. ' Enums: tenums;',
  6853. 'begin',
  6854. ' Include(enums,orange);',
  6855. ' Exclude(enums,orange);',
  6856. ' if orange in enums then;',
  6857. ' if orange in [orange,red] then;']);
  6858. ConvertProgram;
  6859. CheckSource('TestSet_EnumConst',
  6860. LinesToStr([ // statements
  6861. 'this.TEnum = {',
  6862. ' "0": "Red",',
  6863. ' Red: 0,',
  6864. ' "1": "Blue",',
  6865. ' Blue: 1',
  6866. '};',
  6867. 'this.Orange = this.TEnum.Red;',
  6868. 'this.Enum = 0;',
  6869. 'this.Enums = {};',
  6870. '']),
  6871. LinesToStr([
  6872. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6873. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6874. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6875. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6876. '']));
  6877. end;
  6878. procedure TTestModule.TestSet_IntConst;
  6879. begin
  6880. StartProgram(false);
  6881. Add([
  6882. 'type',
  6883. ' TEnums = set of Byte;',
  6884. 'const',
  6885. ' Orange = 0;',
  6886. 'var',
  6887. ' Enum: byte;',
  6888. ' Enums: tenums;',
  6889. 'begin',
  6890. ' Enum:=ord(1);',
  6891. ' Enums:=[];',
  6892. ' Enums:=[0];',
  6893. ' Enums:=[1..2];',
  6894. //' Include(enums,orange);',
  6895. //' Exclude(enums,orange);',
  6896. ' if orange in enums then;',
  6897. ' if orange in [orange,1] then;']);
  6898. ConvertProgram;
  6899. CheckSource('TestSet_IntConst',
  6900. LinesToStr([ // statements
  6901. 'this.Orange = 0;',
  6902. 'this.Enum = 0;',
  6903. 'this.Enums = {};',
  6904. '']),
  6905. LinesToStr([
  6906. '$mod.Enum = 1;',
  6907. '$mod.Enums = {};',
  6908. '$mod.Enums = rtl.createSet(0);',
  6909. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6910. 'if (0 in $mod.Enums) ;',
  6911. 'if (0 in rtl.createSet(0, 1)) ;',
  6912. '']));
  6913. end;
  6914. procedure TTestModule.TestSet_IntRange;
  6915. begin
  6916. StartProgram(false);
  6917. Add([
  6918. 'type',
  6919. ' TRange = 1..3;',
  6920. ' TEnums = set of TRange;',
  6921. 'const',
  6922. ' Orange = 2;',
  6923. 'var',
  6924. ' Enum: byte;',
  6925. ' Enums: TEnums;',
  6926. 'begin',
  6927. ' Enums:=[];',
  6928. ' Enums:=[1];',
  6929. ' Enums:=[2..3];',
  6930. ' Include(enums,orange);',
  6931. ' Exclude(enums,orange);',
  6932. ' if orange in enums then;',
  6933. ' if orange in [orange,1] then;']);
  6934. ConvertProgram;
  6935. CheckSource('TestSet_IntRange',
  6936. LinesToStr([ // statements
  6937. 'this.Orange = 2;',
  6938. 'this.Enum = 0;',
  6939. 'this.Enums = {};',
  6940. '']),
  6941. LinesToStr([
  6942. '$mod.Enums = {};',
  6943. '$mod.Enums = rtl.createSet(1);',
  6944. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6945. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6946. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6947. 'if (2 in $mod.Enums) ;',
  6948. 'if (2 in rtl.createSet(2, 1)) ;',
  6949. '']));
  6950. end;
  6951. procedure TTestModule.TestSet_AnonymousEnumType;
  6952. begin
  6953. StartProgram(false);
  6954. Add('type');
  6955. Add(' TFlags = set of (red, green);');
  6956. Add('const');
  6957. Add(' favorite = red;');
  6958. Add('var');
  6959. Add(' f: TFlags;');
  6960. Add(' i: longint;');
  6961. Add('begin');
  6962. Add(' Include(f,red);');
  6963. Add(' Include(f,favorite);');
  6964. Add(' i:=ord(red);');
  6965. Add(' i:=ord(favorite);');
  6966. Add(' i:=ord(low(TFlags));');
  6967. Add(' i:=ord(low(f));');
  6968. Add(' i:=ord(low(favorite));');
  6969. Add(' i:=ord(high(TFlags));');
  6970. Add(' i:=ord(high(f));');
  6971. Add(' i:=ord(high(favorite));');
  6972. Add(' f:=[green,favorite];');
  6973. ConvertProgram;
  6974. CheckSource('TestSet_AnonymousEnumType',
  6975. LinesToStr([ // statements
  6976. 'this.TFlags$a = {',
  6977. ' "0": "red",',
  6978. ' red: 0,',
  6979. ' "1": "green",',
  6980. ' green: 1',
  6981. '};',
  6982. 'this.favorite = this.TFlags$a.red;',
  6983. 'this.f = {};',
  6984. 'this.i = 0;',
  6985. '']),
  6986. LinesToStr([
  6987. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6988. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6989. '$mod.i = $mod.TFlags$a.red;',
  6990. '$mod.i = $mod.TFlags$a.red;',
  6991. '$mod.i = $mod.TFlags$a.red;',
  6992. '$mod.i = $mod.TFlags$a.red;',
  6993. '$mod.i = $mod.TFlags$a.red;',
  6994. '$mod.i = $mod.TFlags$a.green;',
  6995. '$mod.i = $mod.TFlags$a.green;',
  6996. '$mod.i = $mod.TFlags$a.green;',
  6997. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6998. '']));
  6999. end;
  7000. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  7001. begin
  7002. exit;
  7003. StartProgram(false);
  7004. Add([
  7005. 'type',
  7006. ' TAtoZ = ''A''..''Z'';',
  7007. ' TSetOfAZ = set of TAtoZ;',
  7008. 'var',
  7009. ' c: char;',
  7010. ' a: TAtoZ;',
  7011. ' s: TSetOfAZ = [''P'',''A''];',
  7012. ' i: longint;',
  7013. 'begin',
  7014. ' Include(s,''S'');',
  7015. ' Include(s,c);',
  7016. ' Include(s,a);',
  7017. ' c:=low(TAtoZ);',
  7018. ' i:=ord(low(TAtoZ));',
  7019. ' a:=high(TAtoZ);',
  7020. ' a:=high(TSetOfAtoZ);',
  7021. ' s:=[a,c,''M''];',
  7022. '']);
  7023. ConvertProgram;
  7024. CheckSource('TestSet_AnonymousEnumTypeChar',
  7025. LinesToStr([ // statements
  7026. '']),
  7027. LinesToStr([
  7028. '']));
  7029. end;
  7030. procedure TTestModule.TestSet_ConstEnum;
  7031. begin
  7032. StartProgram(false);
  7033. Add([
  7034. 'type',
  7035. ' TEnum = (red,blue,green);',
  7036. ' TEnums = set of TEnum;',
  7037. 'const',
  7038. ' teAny = [low(TEnum)..high(TEnum)];',
  7039. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  7040. 'var',
  7041. ' e: TEnum;',
  7042. ' s: TEnums;',
  7043. 'begin',
  7044. ' if blue in teAny then;',
  7045. ' if blue in teAny+[e] then;',
  7046. ' if blue in teAny+teRedBlue then;',
  7047. ' if e in [red,blue] then;',
  7048. ' s:=teAny;',
  7049. ' s:=teAny+[e];',
  7050. ' s:=[e]+teAny;',
  7051. ' s:=teAny+teRedBlue;',
  7052. ' s:=teAny+teRedBlue+[e];',
  7053. '']);
  7054. ConvertProgram;
  7055. CheckSource('TestSet_ConstEnum',
  7056. LinesToStr([ // statements
  7057. 'this.TEnum = {',
  7058. ' "0": "red",',
  7059. ' red: 0,',
  7060. ' "1": "blue",',
  7061. ' blue: 1,',
  7062. ' "2": "green",',
  7063. ' green: 2',
  7064. '};',
  7065. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  7066. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  7067. 'this.e = 0;',
  7068. 'this.s = {};',
  7069. '']),
  7070. LinesToStr([
  7071. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  7072. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  7073. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  7074. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  7075. '$mod.s = rtl.refSet($mod.teAny);',
  7076. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  7077. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  7078. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  7079. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  7080. '']));
  7081. end;
  7082. procedure TTestModule.TestSet_ConstChar;
  7083. begin
  7084. StartProgram(false);
  7085. Add([
  7086. 'const',
  7087. ' LowChars = [''a''..''z''];',
  7088. ' Chars = LowChars+[''A''..''Z''];',
  7089. ' sc = [''А'', ''Я''];',
  7090. 'var',
  7091. ' c: char;',
  7092. ' s: string;',
  7093. 'begin',
  7094. ' if c in lowchars then ;',
  7095. ' if ''a'' in lowchars then ;',
  7096. ' if s[1] in lowchars then ;',
  7097. ' if c in chars then ;',
  7098. ' if c in [''a''..''z'',''_''] then ;',
  7099. ' if ''b'' in [''a''..''z'',''_''] then ;',
  7100. ' if ''Я'' in sc then ;',
  7101. ' if 3=ord('' '') then ;',
  7102. '']);
  7103. ConvertProgram;
  7104. CheckSource('TestSet_ConstChar',
  7105. LinesToStr([ // statements
  7106. 'this.LowChars = rtl.createSet(null, 97, 122);',
  7107. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  7108. 'this.sc = rtl.createSet(1040, 1071);',
  7109. 'this.c = "\x00";',
  7110. 'this.s = "";',
  7111. '']),
  7112. LinesToStr([
  7113. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  7114. 'if (97 in $mod.LowChars) ;',
  7115. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  7116. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  7117. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  7118. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  7119. 'if (1071 in $mod.sc) ;',
  7120. 'if (3 === 32) ;',
  7121. '']));
  7122. end;
  7123. procedure TTestModule.TestSet_ConstInt;
  7124. begin
  7125. StartProgram(false);
  7126. Add([
  7127. 'const',
  7128. ' Months = [1..12];',
  7129. ' Mirror = [-12..-1]+Months;',
  7130. 'var',
  7131. ' i: smallint;',
  7132. 'begin',
  7133. ' if 3 in Months then;',
  7134. ' if i in Months+[i] then;',
  7135. ' if i in Months+Mirror then;',
  7136. ' if i in [4..6,8] then;',
  7137. '']);
  7138. ConvertProgram;
  7139. CheckSource('TestSet_ConstInt',
  7140. LinesToStr([ // statements
  7141. 'this.Months = rtl.createSet(null, 1, 12);',
  7142. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  7143. 'this.i = 0;',
  7144. '']),
  7145. LinesToStr([
  7146. 'if (3 in $mod.Months) ;',
  7147. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  7148. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  7149. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  7150. '']));
  7151. end;
  7152. procedure TTestModule.TestSet_InFunction;
  7153. begin
  7154. StartProgram(false);
  7155. Add([
  7156. 'const',
  7157. ' TEnum = 3;',
  7158. ' TSetOfEnum = 4;',
  7159. ' TSetOfAno = 5;',
  7160. 'procedure DoIt;',
  7161. 'type',
  7162. ' TEnum = (red, blue);',
  7163. ' TSetOfEnum = set of TEnum;',
  7164. ' TSetOfAno = set of (up,down);',
  7165. 'var',
  7166. ' e: TEnum;',
  7167. ' se: TSetOfEnum;',
  7168. ' sa: TSetOfAno;',
  7169. 'begin',
  7170. ' se:=[e];',
  7171. ' sa:=[up];',
  7172. 'end;',
  7173. 'begin',
  7174. '']);
  7175. ConvertProgram;
  7176. CheckSource('TestSet_InFunction',
  7177. LinesToStr([ // statements
  7178. 'this.TEnum = 3;',
  7179. 'this.TSetOfEnum = 4;',
  7180. 'this.TSetOfAno = 5;',
  7181. 'var TEnum$1 = {',
  7182. ' "0": "red",',
  7183. ' red: 0,',
  7184. ' "1": "blue",',
  7185. ' blue: 1',
  7186. '};',
  7187. 'var TSetOfAno$a = {',
  7188. ' "0": "up",',
  7189. ' up: 0,',
  7190. ' "1": "down",',
  7191. ' down: 1',
  7192. '};',
  7193. 'this.DoIt = function () {',
  7194. ' var e = 0;',
  7195. ' var se = {};',
  7196. ' var sa = {};',
  7197. ' se = rtl.createSet(e);',
  7198. ' sa = rtl.createSet(TSetOfAno$a.up);',
  7199. '};',
  7200. '']),
  7201. LinesToStr([
  7202. '']));
  7203. end;
  7204. procedure TTestModule.TestSet_ForIn;
  7205. begin
  7206. StartProgram(false);
  7207. Add([
  7208. 'type',
  7209. ' TEnum = (Red, Green, Blue);',
  7210. ' TEnumRg = green..blue;',
  7211. ' TSetOfEnum = set of TEnum;',
  7212. ' TSetOfEnumRg = set of TEnumRg;',
  7213. 'var',
  7214. ' e, e2: TEnum;',
  7215. ' er: TEnum;',
  7216. ' s: TSetOfEnum;',
  7217. 'begin',
  7218. ' for e in TSetOfEnum do ;',
  7219. ' for e in TSetOfEnumRg do ;',
  7220. ' for e in [] do e2:=e;',
  7221. ' for e in [red..green] do e2:=e;',
  7222. ' for e in [green,blue] do e2:=e;',
  7223. ' for e in [red,blue] do e2:=e;',
  7224. ' for e in s do e2:=e;',
  7225. ' for er in TSetOfEnumRg do ;',
  7226. '']);
  7227. ConvertProgram;
  7228. CheckSource('TestSet_ForIn',
  7229. LinesToStr([ // statements
  7230. 'this.TEnum = {',
  7231. ' "0":"Red",',
  7232. ' Red:0,',
  7233. ' "1":"Green",',
  7234. ' Green:1,',
  7235. ' "2":"Blue",',
  7236. ' Blue:2',
  7237. ' };',
  7238. 'this.e = 0;',
  7239. 'this.e2 = 0;',
  7240. 'this.er = 0;',
  7241. 'this.s = {};',
  7242. '']),
  7243. LinesToStr([
  7244. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  7245. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  7246. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  7247. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  7248. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  7249. 'for (var $l in $mod.s){',
  7250. ' $mod.e = +$l;',
  7251. ' $mod.e2 = $mod.e;',
  7252. '};',
  7253. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  7254. '']));
  7255. end;
  7256. procedure TTestModule.TestNestBegin;
  7257. begin
  7258. StartProgram(false);
  7259. Add('begin');
  7260. Add(' begin');
  7261. Add(' begin');
  7262. Add(' end;');
  7263. Add(' begin');
  7264. Add(' if true then ;');
  7265. Add(' end;');
  7266. Add(' end;');
  7267. ConvertProgram;
  7268. CheckSource('TestNestBegin',
  7269. '',
  7270. 'if (true) ;');
  7271. end;
  7272. procedure TTestModule.TestUnitImplVars;
  7273. begin
  7274. StartUnit(false);
  7275. Add('interface');
  7276. Add('implementation');
  7277. Add('var');
  7278. Add(' V1:longint;');
  7279. Add(' V2:longint = 3;');
  7280. Add(' V3:string = ''abc'';');
  7281. ConvertUnit;
  7282. CheckSource('TestUnitImplVars',
  7283. LinesToStr([ // statements
  7284. 'var $impl = $mod.$impl;',
  7285. '']),
  7286. '', // this.$init
  7287. LinesToStr([ // implementation
  7288. '$impl.V1 = 0;',
  7289. '$impl.V2 = 3;',
  7290. '$impl.V3 = "abc";',
  7291. '']) );
  7292. end;
  7293. procedure TTestModule.TestUnitImplConsts;
  7294. begin
  7295. StartUnit(false);
  7296. Add('interface');
  7297. Add('implementation');
  7298. Add('const');
  7299. Add(' v1 = 3;');
  7300. Add(' v2:longint = 4;');
  7301. Add(' v3:string = ''abc'';');
  7302. ConvertUnit;
  7303. CheckSource('TestUnitImplConsts',
  7304. LinesToStr([ // statements
  7305. 'var $impl = $mod.$impl;',
  7306. '']),
  7307. '', // this.$init
  7308. LinesToStr([ // implementation
  7309. '$impl.v1 = 3;',
  7310. '$impl.v2 = 4;',
  7311. '$impl.v3 = "abc";',
  7312. '']) );
  7313. end;
  7314. procedure TTestModule.TestUnitImplRecord;
  7315. begin
  7316. StartUnit(false);
  7317. Add('interface');
  7318. Add('implementation');
  7319. Add('type');
  7320. Add(' TMyRecord = record');
  7321. Add(' i: longint;');
  7322. Add(' end;');
  7323. Add('var aRec: TMyRecord;');
  7324. Add('initialization');
  7325. Add(' arec.i:=3;');
  7326. ConvertUnit;
  7327. CheckSource('TestUnitImplRecord',
  7328. LinesToStr([ // statements
  7329. 'var $impl = $mod.$impl;',
  7330. '']),
  7331. // this.$init
  7332. '$impl.aRec.i = 3;',
  7333. LinesToStr([ // implementation
  7334. 'rtl.recNewT($impl, "TMyRecord", function () {',
  7335. ' this.i = 0;',
  7336. ' this.$eq = function (b) {',
  7337. ' return this.i === b.i;',
  7338. ' };',
  7339. ' this.$assign = function (s) {',
  7340. ' this.i = s.i;',
  7341. ' return this;',
  7342. ' };',
  7343. '});',
  7344. '$impl.aRec = $impl.TMyRecord.$new();',
  7345. '']) );
  7346. end;
  7347. procedure TTestModule.TestRenameJSNameConflict;
  7348. begin
  7349. StartProgram(false);
  7350. Add('var apply: longint;');
  7351. Add('var bind: longint;');
  7352. Add('var call: longint;');
  7353. Add('begin');
  7354. ConvertProgram;
  7355. CheckSource('TestRenameJSNameConflict',
  7356. LinesToStr([ // statements
  7357. 'this.Apply = 0;',
  7358. 'this.Bind = 0;',
  7359. 'this.Call = 0;'
  7360. ]),
  7361. LinesToStr([ // this.$main
  7362. ''
  7363. ]));
  7364. end;
  7365. procedure TTestModule.TestLocalConst;
  7366. begin
  7367. StartProgram(false);
  7368. Add('procedure DoIt;');
  7369. Add('const');
  7370. Add(' cA: longint = 1;');
  7371. Add(' cB = 2;');
  7372. Add(' procedure Sub;');
  7373. Add(' const');
  7374. Add(' csA = 3;');
  7375. Add(' cB: double = 4;');
  7376. Add(' begin');
  7377. Add(' cb:=cb+csa;');
  7378. Add(' ca:=ca+csa+5;');
  7379. Add(' end;');
  7380. Add('begin');
  7381. Add(' ca:=ca+cb+6;');
  7382. Add('end;');
  7383. Add('begin');
  7384. ConvertProgram;
  7385. CheckSource('TestLocalConst',
  7386. LinesToStr([
  7387. 'var cA = 1;',
  7388. 'var cB = 2;',
  7389. 'var csA = 3;',
  7390. 'var cB$1 = 4;',
  7391. 'this.DoIt = function () {',
  7392. ' function Sub() {',
  7393. ' cB$1 = cB$1 + 3;',
  7394. ' cA = cA + 3 + 5;',
  7395. ' };',
  7396. ' cA = cA + 2 + 6;',
  7397. '};'
  7398. ]),
  7399. LinesToStr([
  7400. ]));
  7401. end;
  7402. procedure TTestModule.TestVarExternal;
  7403. begin
  7404. StartProgram(false);
  7405. Add('var');
  7406. Add(' NaN: double; external name ''Global.NaN'';');
  7407. Add(' d: double;');
  7408. Add('begin');
  7409. Add(' d:=NaN;');
  7410. ConvertProgram;
  7411. CheckSource('TestVarExternal',
  7412. LinesToStr([
  7413. 'this.d = 0.0;'
  7414. ]),
  7415. LinesToStr([
  7416. '$mod.d = Global.NaN;'
  7417. ]));
  7418. end;
  7419. procedure TTestModule.TestVarExternalOtherUnit;
  7420. begin
  7421. AddModuleWithIntfImplSrc('unit2.pas',
  7422. LinesToStr([
  7423. 'var NaN: double; external name ''Global.NaN'';',
  7424. 'var iV: longint;'
  7425. ]),
  7426. '');
  7427. StartUnit(true);
  7428. Add('interface');
  7429. Add('uses unit2;');
  7430. Add('implementation');
  7431. Add('var');
  7432. Add(' d: double;');
  7433. Add(' i: longint; external name ''$i'';');
  7434. Add('begin');
  7435. Add(' d:=nan;');
  7436. Add(' d:=uNit2.nan;');
  7437. Add(' d:=test1.d;');
  7438. Add(' i:=iv;');
  7439. Add(' i:=uNit2.iv;');
  7440. Add(' i:=test1.i;');
  7441. ConvertUnit;
  7442. CheckSource('TestVarExternalOtherUnit',
  7443. LinesToStr([
  7444. 'var $impl = $mod.$impl;',
  7445. '']),
  7446. LinesToStr([ // this.$init
  7447. '$impl.d = Global.NaN;',
  7448. '$impl.d = Global.NaN;',
  7449. '$impl.d = $impl.d;',
  7450. '$i = pas.unit2.iV;',
  7451. '$i = pas.unit2.iV;',
  7452. '$i = $i;',
  7453. '']),
  7454. LinesToStr([ // implementation
  7455. '$impl.d = 0.0;',
  7456. '']) );
  7457. end;
  7458. procedure TTestModule.TestVarAbsoluteFail;
  7459. begin
  7460. StartProgram(false);
  7461. Add([
  7462. 'var',
  7463. ' a: longint;',
  7464. ' b: longword absolute a;',
  7465. 'begin']);
  7466. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  7467. ConvertProgram;
  7468. end;
  7469. procedure TTestModule.TestConstExternal;
  7470. begin
  7471. StartProgram(false);
  7472. Add([
  7473. 'const',
  7474. ' PI: double; external name ''Global.PI'';',
  7475. ' Tau = 2*pi;',
  7476. 'var d: double;',
  7477. 'begin',
  7478. ' d:=pi;',
  7479. ' d:=tau+pi;']);
  7480. ConvertProgram;
  7481. CheckSource('TestConstExternal',
  7482. LinesToStr([
  7483. 'this.Tau = 2*Global.PI;',
  7484. 'this.d = 0.0;'
  7485. ]),
  7486. LinesToStr([
  7487. '$mod.d = Global.PI;',
  7488. '$mod.d = $mod.Tau + Global.PI;'
  7489. ]));
  7490. end;
  7491. procedure TTestModule.TestDouble;
  7492. begin
  7493. StartProgram(false);
  7494. Add([
  7495. 'type',
  7496. ' TDateTime = double;',
  7497. 'const',
  7498. ' a = TDateTime(2.7);',
  7499. ' b = a + TDateTime(1.7);',
  7500. ' c = 0.9 + 0.1;',
  7501. ' f0_1 = 0.1;',
  7502. ' f0_3 = 0.3;',
  7503. ' fn0_1 = -0.1;',
  7504. ' fn0_3 = -0.3;',
  7505. ' fn0_003 = -0.003;',
  7506. ' fn0_123456789 = -0.123456789;',
  7507. ' fn300_0 = -300.0;',
  7508. ' fn123456_0 = -123456.0;',
  7509. ' fn1234567_8 = -1234567.8;',
  7510. ' fn12345678_9 = -12345678.9;',
  7511. ' f1_0En12 = 1E-12;',
  7512. ' fn1_0En12 = -1E-12;',
  7513. ' maxdouble = 1.7e+308;',
  7514. ' mindouble = -1.7e+308;',
  7515. ' MinSafeIntDouble = -$1fffffffffffff;',
  7516. ' MinSafeIntDouble2 = -$20000000000000-1;',
  7517. ' MaxSafeIntDouble = $1fffffffffffff;',
  7518. ' DZeroResolution = 1E-12;',
  7519. ' Minus1 = -1E-12;',
  7520. ' EPS = 1E-9;',
  7521. ' DELTA = 0.001;',
  7522. ' Big = 129.789E+100;',
  7523. ' Test0_15 = 0.15;',
  7524. ' Test999 = 2.9999999999999;',
  7525. ' Test111999 = 211199999999999000.0;',
  7526. ' TestMinus111999 = -211199999999999000.0;',
  7527. ' Inf = 1.0 / 0.0;',
  7528. ' NegInf = -1.0 / 0.0;',
  7529. 'procedure Run(d: double); external name ''Run'';',
  7530. 'var',
  7531. ' d: double = b;',
  7532. 'begin',
  7533. ' d:=1.0;',
  7534. ' d:=1.0/3.0;',
  7535. ' d:=1.0/(3-2-1);',
  7536. ' d:=1/3;',
  7537. ' d:=5.0E-324;',
  7538. ' d:=1.7E308;',
  7539. ' d:=001.00E00;',
  7540. ' d:=002.00E001;',
  7541. ' d:=003.000E000;',
  7542. ' d:=-004.00E-00;',
  7543. ' d:=-005.00E-001;',
  7544. ' d:=10**3;',
  7545. ' d:=100*9**0.5;',
  7546. ' d:=10 mod 3;',
  7547. ' d:=10 div 3;',
  7548. ' d:=c;',
  7549. ' d:=f0_1;',
  7550. ' d:=f0_3;',
  7551. ' d:=fn0_1;',
  7552. ' d:=fn0_3;',
  7553. ' d:=fn0_003;',
  7554. ' d:=fn0_123456789;',
  7555. ' d:=fn300_0;',
  7556. ' d:=fn123456_0;',
  7557. ' d:=fn1234567_8;',
  7558. ' d:=fn12345678_9;',
  7559. ' d:=f1_0En12;',
  7560. ' d:=fn1_0En12;',
  7561. ' d:=maxdouble;',
  7562. ' d:=mindouble;',
  7563. ' d:=MinSafeIntDouble;',
  7564. ' d:=double(MinSafeIntDouble);',
  7565. ' d:=MinSafeIntDouble2;',
  7566. ' d:=double(MinSafeIntDouble2);',
  7567. ' d:=MaxSafeIntDouble;',
  7568. ' d:=default(double);',
  7569. ' Run(Inf);',
  7570. ' Run(NegInf);',
  7571. '']);
  7572. ConvertProgram;
  7573. CheckSource('TestDouble',
  7574. LinesToStr([
  7575. 'this.a = 2.7;',
  7576. 'this.b = 2.7 + 1.7;',
  7577. 'this.c = 0.9 + 0.1;',
  7578. 'this.f0_1 = 0.1;',
  7579. 'this.f0_3 = 0.3;',
  7580. 'this.fn0_1 = -0.1;',
  7581. 'this.fn0_3 = -0.3;',
  7582. 'this.fn0_003 = -0.003;',
  7583. 'this.fn0_123456789 = -0.123456789;',
  7584. 'this.fn300_0 = -300.0;',
  7585. 'this.fn123456_0 = -123456.0;',
  7586. 'this.fn1234567_8 = -1234567.8;',
  7587. 'this.fn12345678_9 = -12345678.9;',
  7588. 'this.f1_0En12 = 1E-12;',
  7589. 'this.fn1_0En12 = -1E-12;',
  7590. 'this.maxdouble = 1.7e+308;',
  7591. 'this.mindouble = -1.7e+308;',
  7592. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  7593. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  7594. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  7595. 'this.DZeroResolution = 1E-12;',
  7596. 'this.Minus1 = -1E-12;',
  7597. 'this.EPS = 1E-9;',
  7598. 'this.DELTA = 0.001;',
  7599. 'this.Big = 129.789E+100;',
  7600. 'this.Test0_15 = 0.15;',
  7601. 'this.Test999 = 2.9999999999999;',
  7602. 'this.Test111999 = 211199999999999000.0;',
  7603. 'this.TestMinus111999 = -211199999999999000.0;',
  7604. 'this.Inf = 1.0 / 0.0;',
  7605. 'this.NegInf = -1.0 / 0.0;',
  7606. 'this.d = 4.4;',
  7607. '']),
  7608. LinesToStr([
  7609. '$mod.d = 1.0;',
  7610. '$mod.d = 1.0 / 3.0;',
  7611. '$mod.d = 1.0 / (3 - 2 - 1);',
  7612. '$mod.d = 1 / 3;',
  7613. '$mod.d = 5.0E-324;',
  7614. '$mod.d = 1.7E308;',
  7615. '$mod.d = 1.00E0;',
  7616. '$mod.d = 2.00E1;',
  7617. '$mod.d = 3.000E0;',
  7618. '$mod.d = -4.00E-0;',
  7619. '$mod.d = -5.00E-1;',
  7620. '$mod.d = 10 ** 3;',
  7621. '$mod.d = 100 * (9 ** 0.5);',
  7622. '$mod.d = 10 % 3;',
  7623. '$mod.d = rtl.trunc(10 / 3);',
  7624. '$mod.d = 1;',
  7625. '$mod.d = 0.1;',
  7626. '$mod.d = 0.3;',
  7627. '$mod.d = -0.1;',
  7628. '$mod.d = -0.3;',
  7629. '$mod.d = -3E-3;',
  7630. '$mod.d = -0.123456789;',
  7631. '$mod.d = -300;',
  7632. '$mod.d = -123456;',
  7633. '$mod.d = -1234567.8;',
  7634. '$mod.d = -1.23456789E7;',
  7635. '$mod.d = 1E-12;',
  7636. '$mod.d = -1E-12;',
  7637. '$mod.d = 1.7E308;',
  7638. '$mod.d = -1.7E308;',
  7639. '$mod.d = -9007199254740991;',
  7640. '$mod.d = -9007199254740991;',
  7641. '$mod.d = -9.007199254740992E15;',
  7642. '$mod.d = -9.007199254740992E15;',
  7643. '$mod.d = 9007199254740991;',
  7644. '$mod.d = 0.0;',
  7645. 'Run(1 / 0);',
  7646. 'Run(-1 / 0);',
  7647. '']));
  7648. end;
  7649. procedure TTestModule.TestDoubleSmall;
  7650. begin
  7651. StartProgram(false);
  7652. Add([
  7653. 'const',
  7654. ' a = 1e-1;',
  7655. ' b = 1e-2;',
  7656. ' c = 1e-3;',
  7657. ' d = 1e-4;',
  7658. ' e = 1e-5;',
  7659. ' f = 1e-6;',
  7660. ' g = 1e-7;',
  7661. ' h = -1e-1;',
  7662. ' i = -1e-2;',
  7663. 'procedure Fly(d: double);',
  7664. 'begin',
  7665. 'end;',
  7666. 'begin',
  7667. ' Fly(a);',
  7668. ' Fly(b);',
  7669. ' Fly(c);',
  7670. ' Fly(d);',
  7671. ' Fly(e);',
  7672. ' Fly(f);',
  7673. ' Fly(g);',
  7674. ' Fly(h);',
  7675. ' Fly(i);',
  7676. '']);
  7677. ConvertProgram;
  7678. CheckSource('TestDoubleSmall',
  7679. LinesToStr([
  7680. 'this.a = 1e-1;',
  7681. 'this.b = 1e-2;',
  7682. 'this.c = 1e-3;',
  7683. 'this.d = 1e-4;',
  7684. 'this.e = 1e-5;',
  7685. 'this.f = 1e-6;',
  7686. 'this.g = 1e-7;',
  7687. 'this.h = -1e-1;',
  7688. 'this.i = -1e-2;',
  7689. 'this.Fly = function (d) {',
  7690. '};',
  7691. '']),
  7692. LinesToStr([
  7693. '$mod.Fly(0.1);',
  7694. '$mod.Fly(0.01);',
  7695. '$mod.Fly(1E-3);',
  7696. '$mod.Fly(1E-4);',
  7697. '$mod.Fly(1E-5);',
  7698. '$mod.Fly(1E-6);',
  7699. '$mod.Fly(1E-7);',
  7700. '$mod.Fly(-0.1);',
  7701. '$mod.Fly(-0.01);',
  7702. '']));
  7703. end;
  7704. procedure TTestModule.TestInteger;
  7705. begin
  7706. StartProgram(false);
  7707. Add([
  7708. 'const',
  7709. ' MinInt = low(NativeInt);',
  7710. ' MaxInt = high(NativeInt);',
  7711. 'type',
  7712. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7713. 'const',
  7714. ' a = low(TMyInt)+High(TMyInt);',
  7715. 'var',
  7716. ' i: TMyInt;',
  7717. 'begin',
  7718. ' i:=-MinInt;',
  7719. ' i:=default(TMyInt);',
  7720. ' i:=low(i)+high(i);',
  7721. '']);
  7722. ConvertProgram;
  7723. CheckSource('TestIntegerRange',
  7724. LinesToStr([
  7725. 'this.MinInt = -9007199254740991;',
  7726. 'this.MaxInt = 9007199254740991;',
  7727. 'this.a = -9007199254740991 + 9007199254740991;',
  7728. 'this.i = 0;',
  7729. '']),
  7730. LinesToStr([
  7731. '$mod.i = - -9007199254740991;',
  7732. '$mod.i = -9007199254740991;',
  7733. '$mod.i = -9007199254740991 + 9007199254740991;',
  7734. '']));
  7735. end;
  7736. procedure TTestModule.TestIntegerRange;
  7737. begin
  7738. StartProgram(false);
  7739. Add([
  7740. 'const',
  7741. ' MinInt = -1;',
  7742. ' MaxInt = +1;',
  7743. 'type',
  7744. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7745. ' TInt2 = 1..3;',
  7746. 'const',
  7747. ' a = low(TMyInt)+High(TMyInt);',
  7748. ' b = low(TInt2)+High(TInt2);',
  7749. ' s1 = [1];',
  7750. ' s2 = [1,2];',
  7751. ' s3 = [1..3];',
  7752. ' s4 = [low(shortint)..high(shortint)];',
  7753. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7754. ' s6 = 1 in s2;',
  7755. 'var',
  7756. ' i: TMyInt;',
  7757. ' i2: TInt2;',
  7758. 'begin',
  7759. ' i:=i2;',
  7760. ' i:=default(TMyInt);',
  7761. ' if i=i2 then ;',
  7762. ' i:=ord(i2);',
  7763. '']);
  7764. ConvertProgram;
  7765. CheckSource('TestIntegerRange',
  7766. LinesToStr([
  7767. 'this.MinInt = -1;',
  7768. 'this.MaxInt = +1;',
  7769. 'this.a = -1 + 1;',
  7770. 'this.b = 1 + 3;',
  7771. 'this.s1 = rtl.createSet(1);',
  7772. 'this.s2 = rtl.createSet(1, 2);',
  7773. 'this.s3 = rtl.createSet(null, 1, 3);',
  7774. 'this.s4 = rtl.createSet(null, -128, 127);',
  7775. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7776. 'this.s6 = 1 in this.s2;',
  7777. 'this.i = 0;',
  7778. 'this.i2 = 0;',
  7779. '']),
  7780. LinesToStr([
  7781. '$mod.i = $mod.i2;',
  7782. '$mod.i = -1;',
  7783. 'if ($mod.i === $mod.i2) ;',
  7784. '$mod.i = $mod.i2;',
  7785. '']));
  7786. end;
  7787. procedure TTestModule.TestIntegerTypecasts;
  7788. begin
  7789. StartProgram(false);
  7790. Add([
  7791. 'var',
  7792. ' i: nativeint;',
  7793. ' b: byte;',
  7794. ' sh: shortint;',
  7795. ' w: word;',
  7796. ' sm: smallint;',
  7797. ' lw: longword;',
  7798. ' li: longint;',
  7799. 'begin',
  7800. ' b:=byte(i);',
  7801. ' sh:=shortint(i);',
  7802. ' w:=word(i);',
  7803. ' sm:=smallint(i);',
  7804. ' lw:=longword(i);',
  7805. ' li:=longint(i);',
  7806. '']);
  7807. ConvertProgram;
  7808. CheckSource('TestIntegerTypecasts',
  7809. LinesToStr([
  7810. 'this.i = 0;',
  7811. 'this.b = 0;',
  7812. 'this.sh = 0;',
  7813. 'this.w = 0;',
  7814. 'this.sm = 0;',
  7815. 'this.lw = 0;',
  7816. 'this.li = 0;',
  7817. '']),
  7818. LinesToStr([
  7819. '$mod.b = $mod.i & 255;',
  7820. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7821. '$mod.w = $mod.i & 65535;',
  7822. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7823. '$mod.lw = $mod.i >>> 0;',
  7824. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7825. '']));
  7826. end;
  7827. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7828. begin
  7829. StartProgram(false);
  7830. Add([
  7831. 'var',
  7832. ' i,j: nativeint;',
  7833. 'begin',
  7834. ' i:=i shr 0;',
  7835. ' i:=i shr 1;',
  7836. ' i:=i shr 3;',
  7837. ' i:=i shr 54;',
  7838. ' i:=j shr i;',
  7839. '']);
  7840. ConvertProgram;
  7841. CheckResolverUnexpectedHints;
  7842. CheckSource('TestInteger_BitwiseShrNativeInt',
  7843. LinesToStr([
  7844. 'this.i = 0;',
  7845. 'this.j = 0;',
  7846. '']),
  7847. LinesToStr([
  7848. '$mod.i = $mod.i;',
  7849. '$mod.i = Math.floor($mod.i / 2);',
  7850. '$mod.i = Math.floor($mod.i / 8);',
  7851. '$mod.i = 0;',
  7852. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7853. '']));
  7854. end;
  7855. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7856. begin
  7857. StartProgram(false);
  7858. Add([
  7859. 'var',
  7860. ' i: nativeint;',
  7861. 'begin',
  7862. ' i:=i shl 0;',
  7863. ' i:=i shl 54;',
  7864. ' i:=123456789012 shl 1;',
  7865. ' i:=i shl 1;',
  7866. '']);
  7867. ConvertProgram;
  7868. CheckResolverUnexpectedHints;
  7869. CheckSource('TestInteger_BitwiseShrNativeInt',
  7870. LinesToStr([
  7871. 'this.i = 0;',
  7872. '']),
  7873. LinesToStr([
  7874. '$mod.i = $mod.i;',
  7875. '$mod.i = 0;',
  7876. '$mod.i = 246913578024;',
  7877. '$mod.i = rtl.shl($mod.i, 1);',
  7878. '']));
  7879. end;
  7880. procedure TTestModule.TestInteger_SystemFunc;
  7881. begin
  7882. StartProgram(true);
  7883. Add([
  7884. 'var',
  7885. ' i: byte;',
  7886. ' s: string;',
  7887. 'begin',
  7888. ' system.inc(i);',
  7889. ' system.str(i,s);',
  7890. ' s:=system.str(i);',
  7891. ' i:=system.low(i);',
  7892. ' i:=system.high(i);',
  7893. ' i:=system.pred(i);',
  7894. ' i:=system.succ(i);',
  7895. ' i:=system.ord(i);',
  7896. '']);
  7897. ConvertProgram;
  7898. CheckResolverUnexpectedHints;
  7899. CheckSource('TestInteger_SystemFunc',
  7900. LinesToStr([
  7901. 'this.i = 0;',
  7902. 'this.s = "";',
  7903. '']),
  7904. LinesToStr([
  7905. '$mod.i += 1;',
  7906. '$mod.s = "" + $mod.i;',
  7907. '$mod.s = "" + $mod.i;',
  7908. '$mod.i = 0;',
  7909. '$mod.i = 255;',
  7910. '$mod.i = $mod.i - 1;',
  7911. '$mod.i = $mod.i + 1;',
  7912. '$mod.i = $mod.i;',
  7913. '']));
  7914. end;
  7915. procedure TTestModule.TestInteger_AssignOutsideConst;
  7916. begin
  7917. StartProgram(false);
  7918. Add([
  7919. 'const',
  7920. ' MinInt = low(longint);',
  7921. ' MaxInt = high(longint);',
  7922. 'type',
  7923. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7924. 'var',
  7925. ' i: TMyInt;',
  7926. ' aByte: byte;',
  7927. ' aShortInt: shortint;',
  7928. ' aWord: word;',
  7929. ' aSmallInt: smallint;',
  7930. ' aLongWord: longword;',
  7931. ' aLongInt: longint;',
  7932. ' aNativeInt: nativeint;',
  7933. ' aNativeUInt: nativeuint;',
  7934. 'begin',
  7935. ' aByte:=$FF;',
  7936. ' aByte:=$100;',
  7937. ' aByte:=-1;',
  7938. ' aByte:=-127;',
  7939. ' aByte:=-128;',
  7940. ' aByte:=-254;',
  7941. ' aByte:=-255;',
  7942. ' aByte:=-256;',
  7943. ' aShortInt:=127;',
  7944. ' aShortInt:=128;',
  7945. ' aShortInt:=-128;',
  7946. ' aShortInt:=-129;',
  7947. ' aWord:=$ffff;',
  7948. ' aWord:=$10000;',
  7949. ' aWord:=-1;',
  7950. ' aWord:=-$ffff;',
  7951. ' aWord:=-$10000;',
  7952. ' aWord:=-$10001;',
  7953. ' aSmallInt:=$7fff;',
  7954. ' aSmallInt:=$8000;',
  7955. ' aSmallInt:=-$8000;',
  7956. ' aSmallInt:=-$8001;',
  7957. ' aLongWord:=$ffffffff;',
  7958. ' aLongWord:=$100000000;',
  7959. ' aLongWord:=-1;',
  7960. ' aLongWord:=-$ffffffff;',
  7961. ' aNativeInt:=$1fffffffffffff;',
  7962. ' aNativeInt:=-$1fffffffffffff;',
  7963. ' aNativeUInt:=$1fffffffffffff;',
  7964. ' aNativeUInt:=-$1fffffffffffff;',
  7965. '']);
  7966. ConvertProgram;
  7967. CheckSource('TestInteger_AssignOutsideConst',
  7968. LinesToStr([
  7969. 'this.MinInt = -2147483648;',
  7970. 'this.MaxInt = 2147483647;',
  7971. 'this.i = 0;',
  7972. 'this.aByte = 0;',
  7973. 'this.aShortInt = 0;',
  7974. 'this.aWord = 0;',
  7975. 'this.aSmallInt = 0;',
  7976. 'this.aLongWord = 0;',
  7977. 'this.aLongInt = 0;',
  7978. 'this.aNativeInt = 0;',
  7979. 'this.aNativeUInt = 0;',
  7980. '']),
  7981. LinesToStr([
  7982. '$mod.aByte = 0xFF;',
  7983. '$mod.aByte = 0;',
  7984. '$mod.aByte = 255;',
  7985. '$mod.aByte = 129;',
  7986. '$mod.aByte = 128;',
  7987. '$mod.aByte = 2;',
  7988. '$mod.aByte = 1;',
  7989. '$mod.aByte = 0;',
  7990. '$mod.aShortInt = 127;',
  7991. '$mod.aShortInt = -128;',
  7992. '$mod.aShortInt = -128;',
  7993. '$mod.aShortInt = 127;',
  7994. '$mod.aWord = 0xffff;',
  7995. '$mod.aWord = 0;',
  7996. '$mod.aWord = 65535;',
  7997. '$mod.aWord = 1;',
  7998. '$mod.aWord = 0;',
  7999. '$mod.aWord = 65535;',
  8000. '$mod.aSmallInt = 0x7fff;',
  8001. '$mod.aSmallInt = -32768;',
  8002. '$mod.aSmallInt = -0x8000;',
  8003. '$mod.aSmallInt = 32767;',
  8004. '$mod.aLongWord = 0xffffffff;',
  8005. '$mod.aLongWord = 0;',
  8006. '$mod.aLongWord = 4294967295;',
  8007. '$mod.aLongWord = 1;',
  8008. '$mod.aNativeInt = 0x1fffffffffffff;',
  8009. '$mod.aNativeInt = -0x1fffffffffffff;',
  8010. '$mod.aNativeUInt = 0x1fffffffffffff;',
  8011. '$mod.aNativeUInt = 1;',
  8012. '']));
  8013. end;
  8014. procedure TTestModule.TestCurrency;
  8015. begin
  8016. StartProgram(false);
  8017. Add([
  8018. 'type',
  8019. ' TCoin = currency;',
  8020. 'const',
  8021. ' a = TCoin(2.7);',
  8022. ' b = a + TCoin(1.7);',
  8023. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  8024. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  8025. 'var',
  8026. ' c: TCoin = b;',
  8027. ' i: nativeint;',
  8028. ' d: double;',
  8029. ' j: jsvalue;',
  8030. 'function DoIt(c: currency): currency; begin end;',
  8031. 'function GetIt(d: double): double; begin end;',
  8032. 'procedure Write(v: jsvalue); begin end;',
  8033. 'begin',
  8034. ' c:=1.0;',
  8035. ' c:=0.1;',
  8036. ' c:=1.0/3.0;',
  8037. ' c:=1/3;',
  8038. ' c:=a;',
  8039. ' d:=c;',
  8040. ' c:=d;',
  8041. ' c:=currency(c);',
  8042. ' c:=currency(d);',
  8043. ' d:=double(c);',
  8044. ' c:=i;',
  8045. ' c:=currency(i);',
  8046. //' i:=c;', not allowed
  8047. ' i:=nativeint(c);',
  8048. ' c:=c+a;',
  8049. ' c:=-c-a;',
  8050. ' c:=d+c;',
  8051. ' c:=c+d;',
  8052. ' c:=d-c;',
  8053. ' c:=c-d;',
  8054. ' c:=c*a;',
  8055. ' c:=a*c;',
  8056. ' c:=d*c;',
  8057. ' c:=c*d;',
  8058. ' c:=c/a;',
  8059. ' c:=a/c;',
  8060. ' c:=d/c;',
  8061. ' c:=c/d;',
  8062. ' c:=c**a;',
  8063. ' c:=a**c;',
  8064. ' c:=d**c;',
  8065. ' c:=c**d;',
  8066. ' if c=c then ;',
  8067. ' if c=a then ;',
  8068. ' if a=c then ;',
  8069. ' if d=c then ;',
  8070. ' if c=d then ;',
  8071. ' c:=DoIt(c);',
  8072. ' c:=DoIt(i);',
  8073. ' c:=DoIt(d);',
  8074. ' c:=GetIt(c);',
  8075. ' j:=c;',
  8076. ' Write(c);',
  8077. ' c:=default(currency);',
  8078. ' j:=str(c);',
  8079. ' j:=str(c:0:3);',
  8080. '']);
  8081. ConvertProgram;
  8082. CheckSource('TestCurrency',
  8083. LinesToStr([
  8084. 'this.a = 27000;',
  8085. 'this.b = this.a + 17000;',
  8086. 'this.MinSafeIntCurrency = -92233720368.5477;',
  8087. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  8088. 'this.c = this.b;',
  8089. 'this.i = 0;',
  8090. 'this.d = 0.0;',
  8091. 'this.j = undefined;',
  8092. 'this.DoIt = function (c) {',
  8093. ' var Result = 0;',
  8094. ' return Result;',
  8095. '};',
  8096. 'this.GetIt = function (d) {',
  8097. ' var Result = 0.0;',
  8098. ' return Result;',
  8099. '};',
  8100. 'this.Write = function (v) {',
  8101. '};',
  8102. '']),
  8103. LinesToStr([
  8104. '$mod.c = 10000;',
  8105. '$mod.c = 1000;',
  8106. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  8107. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  8108. '$mod.c = $mod.a;',
  8109. '$mod.d = $mod.c / 10000;',
  8110. '$mod.c = rtl.trunc($mod.d * 10000);',
  8111. '$mod.c = $mod.c;',
  8112. '$mod.c = $mod.d * 10000;',
  8113. '$mod.d = $mod.c / 10000;',
  8114. '$mod.c = $mod.i * 10000;',
  8115. '$mod.c = $mod.i * 10000;',
  8116. '$mod.i = rtl.trunc($mod.c / 10000);',
  8117. '$mod.c = $mod.c + $mod.a;',
  8118. '$mod.c = -$mod.c - $mod.a;',
  8119. '$mod.c = ($mod.d * 10000) + $mod.c;',
  8120. '$mod.c = $mod.c + ($mod.d * 10000);',
  8121. '$mod.c = ($mod.d * 10000) - $mod.c;',
  8122. '$mod.c = $mod.c - ($mod.d * 10000);',
  8123. '$mod.c = ($mod.c * $mod.a) / 10000;',
  8124. '$mod.c = ($mod.a * $mod.c) / 10000;',
  8125. '$mod.c = $mod.d * $mod.c;',
  8126. '$mod.c = $mod.c * $mod.d;',
  8127. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  8128. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  8129. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  8130. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  8131. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  8132. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  8133. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  8134. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  8135. 'if ($mod.c === $mod.c) ;',
  8136. 'if ($mod.c === $mod.a) ;',
  8137. 'if ($mod.a === $mod.c) ;',
  8138. 'if (($mod.d * 10000) === $mod.c) ;',
  8139. 'if ($mod.c === ($mod.d * 10000)) ;',
  8140. '$mod.c = $mod.DoIt($mod.c);',
  8141. '$mod.c = $mod.DoIt($mod.i * 10000);',
  8142. '$mod.c = $mod.DoIt($mod.d * 10000);',
  8143. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  8144. '$mod.j = $mod.c / 10000;',
  8145. '$mod.Write($mod.c / 10000);',
  8146. '$mod.c = 0;',
  8147. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  8148. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  8149. '']));
  8150. end;
  8151. procedure TTestModule.TestForBoolDo;
  8152. begin
  8153. StartProgram(false);
  8154. Add([
  8155. 'var b: boolean;',
  8156. 'begin',
  8157. ' for b:=false to true do ;',
  8158. ' for b:=b downto false do ;',
  8159. ' for b in boolean do ;',
  8160. '']);
  8161. ConvertProgram;
  8162. CheckSource('TestForBoolDo',
  8163. LinesToStr([ // statements
  8164. 'this.b = false;']),
  8165. LinesToStr([ // this.$main
  8166. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  8167. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  8168. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  8169. '']));
  8170. end;
  8171. procedure TTestModule.TestForIntDo;
  8172. begin
  8173. StartProgram(false);
  8174. Add([
  8175. 'var i: longint;',
  8176. 'begin',
  8177. ' for i:=3 to 5 do ;',
  8178. ' for i:=i downto 2 do ;',
  8179. ' for i in byte do ;',
  8180. '']);
  8181. ConvertProgram;
  8182. CheckSource('TestForIntDo',
  8183. LinesToStr([ // statements
  8184. 'this.i = 0;']),
  8185. LinesToStr([ // this.$main
  8186. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  8187. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  8188. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  8189. '']));
  8190. end;
  8191. procedure TTestModule.TestForIntInDo;
  8192. begin
  8193. StartProgram(false);
  8194. Add([
  8195. 'type',
  8196. ' TSetOfInt = set of byte;',
  8197. ' TIntRg = 3..7;',
  8198. ' TSetOfIntRg = set of TIntRg;',
  8199. 'var',
  8200. ' i,i2: longint;',
  8201. ' a1: array of byte;',
  8202. ' a2: array[1..3] of byte;',
  8203. ' soi: TSetOfInt;',
  8204. ' soir: TSetOfIntRg;',
  8205. ' ir: TIntRg;',
  8206. 'begin',
  8207. ' for i in byte do ;',
  8208. ' for i in a1 do ;',
  8209. ' for i in a2 do ;',
  8210. ' for i in [11..13] do ;',
  8211. ' for i in TSetOfInt do ;',
  8212. ' for i in TIntRg do ;',
  8213. ' for i in soi do i2:=i;',
  8214. ' for i in TSetOfIntRg do ;',
  8215. ' for i in soir do ;',
  8216. ' for ir in TIntRg do ;',
  8217. ' for ir in TSetOfIntRg do ;',
  8218. ' for ir in soir do ;',
  8219. '']);
  8220. ConvertProgram;
  8221. CheckSource('TestForIntInDo',
  8222. LinesToStr([ // statements
  8223. 'this.i = 0;',
  8224. 'this.i2 = 0;',
  8225. 'this.a1 = [];',
  8226. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  8227. 'this.soi = {};',
  8228. 'this.soir = {};',
  8229. 'this.ir = 0;',
  8230. '']),
  8231. LinesToStr([ // this.$main
  8232. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  8233. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  8234. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  8235. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  8236. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  8237. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  8238. 'for (var $l6 in $mod.soi) {',
  8239. ' $mod.i = +$l6;',
  8240. ' $mod.i2 = $mod.i;',
  8241. '};',
  8242. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  8243. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  8244. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  8245. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  8246. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  8247. '']));
  8248. end;
  8249. procedure TTestModule.TestCharConst;
  8250. begin
  8251. StartProgram(false);
  8252. Add([
  8253. 'const',
  8254. ' a = #$00F3;',
  8255. ' c: char = ''1'';',
  8256. ' wc: widechar = ''ä'';',
  8257. 'begin',
  8258. ' c:=#0;',
  8259. ' c:=#1;',
  8260. ' c:=#9;',
  8261. ' c:=#10;',
  8262. ' c:=#13;',
  8263. ' c:=#31;',
  8264. ' c:=#32;',
  8265. ' c:=#$A;',
  8266. ' c:=#$0A;',
  8267. ' c:=#$b;',
  8268. ' c:=#$0b;',
  8269. ' c:=^A;',
  8270. ' c:=''"'';',
  8271. ' c:=default(char);',
  8272. ' c:=#$00E4;', // ä
  8273. ' c:=''ä'';',
  8274. ' c:=#$E4;', // ä
  8275. ' c:=#$D800;', // invalid UTF-16
  8276. ' c:=#$DFFF;', // invalid UTF-16
  8277. ' c:=#$FFFF;', // last UCS-2
  8278. ' c:=high(c);', // last UCS-2
  8279. ' c:=#269;',
  8280. '']);
  8281. ConvertProgram;
  8282. CheckSource('TestCharConst',
  8283. LinesToStr([
  8284. 'this.a="ó";',
  8285. 'this.c="1";',
  8286. 'this.wc="ä";'
  8287. ]),
  8288. LinesToStr([
  8289. '$mod.c="\x00";',
  8290. '$mod.c="\x01";',
  8291. '$mod.c="\t";',
  8292. '$mod.c="\n";',
  8293. '$mod.c="\r";',
  8294. '$mod.c="\x1F";',
  8295. '$mod.c=" ";',
  8296. '$mod.c="\n";',
  8297. '$mod.c="\n";',
  8298. '$mod.c="\x0B";',
  8299. '$mod.c="\x0B";',
  8300. '$mod.c="\x01";',
  8301. '$mod.c=''"'';',
  8302. '$mod.c="\x00";',
  8303. '$mod.c = "ä";',
  8304. '$mod.c = "ä";',
  8305. '$mod.c = "ä";',
  8306. '$mod.c="\uD800";',
  8307. '$mod.c="\uDFFF";',
  8308. '$mod.c="\uFFFF";',
  8309. '$mod.c="\uFFFF";',
  8310. '$mod.c = "č";',
  8311. '']));
  8312. end;
  8313. procedure TTestModule.TestChar_Compare;
  8314. begin
  8315. StartProgram(false);
  8316. Add('var');
  8317. Add(' c: char;');
  8318. Add(' b: boolean;');
  8319. Add('begin');
  8320. Add(' b:=c=''1'';');
  8321. Add(' b:=''2''=c;');
  8322. Add(' b:=''3''=''4'';');
  8323. Add(' b:=c<>''5'';');
  8324. Add(' b:=''6''<>c;');
  8325. Add(' b:=c>''7'';');
  8326. Add(' b:=''8''>c;');
  8327. Add(' b:=c>=''9'';');
  8328. Add(' b:=''A''>=c;');
  8329. Add(' b:=c<''B'';');
  8330. Add(' b:=''C''<c;');
  8331. Add(' b:=c<=''D'';');
  8332. Add(' b:=''E''<=c;');
  8333. ConvertProgram;
  8334. CheckSource('TestChar_Compare',
  8335. LinesToStr([
  8336. 'this.c = "\x00";',
  8337. 'this.b = false;'
  8338. ]),
  8339. LinesToStr([
  8340. '$mod.b = $mod.c === "1";',
  8341. '$mod.b = "2" === $mod.c;',
  8342. '$mod.b = "3" === "4";',
  8343. '$mod.b = $mod.c !== "5";',
  8344. '$mod.b = "6" !== $mod.c;',
  8345. '$mod.b = $mod.c > "7";',
  8346. '$mod.b = "8" > $mod.c;',
  8347. '$mod.b = $mod.c >= "9";',
  8348. '$mod.b = "A" >= $mod.c;',
  8349. '$mod.b = $mod.c < "B";',
  8350. '$mod.b = "C" < $mod.c;',
  8351. '$mod.b = $mod.c <= "D";',
  8352. '$mod.b = "E" <= $mod.c;',
  8353. '']));
  8354. end;
  8355. procedure TTestModule.TestChar_BuiltInProcs;
  8356. begin
  8357. StartProgram(false);
  8358. Add([
  8359. 'var',
  8360. ' c: char;',
  8361. ' i: longint;',
  8362. ' s: string;',
  8363. 'begin',
  8364. ' i:=ord(c);',
  8365. ' i:=ord(s[i]);',
  8366. ' c:=chr(i);',
  8367. ' c:=pred(c);',
  8368. ' c:=succ(c);',
  8369. ' c:=low(c);',
  8370. ' c:=high(c);',
  8371. ' i:=byte(c);',
  8372. ' i:=word(c);',
  8373. ' i:=longint(c);',
  8374. '']);
  8375. ConvertProgram;
  8376. CheckSource('TestChar_BuiltInProcs',
  8377. LinesToStr([
  8378. 'this.c = "\x00";',
  8379. 'this.i = 0;',
  8380. 'this.s = "";'
  8381. ]),
  8382. LinesToStr([
  8383. '$mod.i = $mod.c.charCodeAt();',
  8384. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  8385. '$mod.c = String.fromCharCode($mod.i);',
  8386. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  8387. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  8388. '$mod.c = "\x00";',
  8389. '$mod.c = "\uFFFF";',
  8390. '$mod.i = $mod.c.charCodeAt() & 255;',
  8391. '$mod.i = $mod.c.charCodeAt();',
  8392. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  8393. '']));
  8394. end;
  8395. procedure TTestModule.TestStringConst;
  8396. begin
  8397. StartProgram(false);
  8398. Add([
  8399. '{$H+}',
  8400. 'const',
  8401. ' a = #$00F3#$017C;', // first <256, then >=256
  8402. ' b = string(''a'');',
  8403. ' c = string(''ä'');',
  8404. ' d = UnicodeString(''b'');',
  8405. ' e = UnicodeString(''ö'');',
  8406. ' f = low(a)+high(b);',
  8407. ' g: word = low(a);',
  8408. 'var',
  8409. ' s: string = ''abc'';',
  8410. ' i: longint;',
  8411. 'begin',
  8412. ' s:='''';',
  8413. ' s:=#13#10;',
  8414. ' s:=#9''foo'';',
  8415. ' s:=#$A9;',
  8416. ' s:=''foo''#13''bar'';',
  8417. ' s:=''"'';',
  8418. ' s:=''"''''"'';',
  8419. ' s:=#$20AC;', // euro
  8420. ' s:=#$10437;', // outside BMP
  8421. ' s:=''abc''#$20AC;', // ascii,#
  8422. ' s:=''ä''#$20AC;', // non ascii,#
  8423. ' s:=#$20AC''abc'';', // #, ascii
  8424. ' s:=#$20AC''ä'';', // #, non ascii
  8425. ' s:=default(string);',
  8426. ' s:=concat(s);',
  8427. ' s:=concat(s,''a'',s);',
  8428. ' s:=#250#269;',
  8429. ' i:=low(s)+high(a);',
  8430. ' s:=''a/b'';',
  8431. // ToDo: \uD87E\uDC04 -> \u{2F804}
  8432. '']);
  8433. ConvertProgram;
  8434. CheckSource('TestStringConst',
  8435. LinesToStr([
  8436. 'this.a = "óż";',
  8437. 'this.b = "a";',
  8438. 'this.c = "ä";',
  8439. 'this.d = "b";',
  8440. 'this.e = "ö";',
  8441. 'this.f = 1 + this.b.length;',
  8442. 'this.g = 1;',
  8443. 'this.s="abc";',
  8444. 'this.i = 0;',
  8445. '']),
  8446. LinesToStr([
  8447. '$mod.s="";',
  8448. '$mod.s="\r\n";',
  8449. '$mod.s="\tfoo";',
  8450. '$mod.s="©";',
  8451. '$mod.s="foo\rbar";',
  8452. '$mod.s=''"'';',
  8453. '$mod.s=''"\''"'';',
  8454. '$mod.s="€";',
  8455. '$mod.s="'#$F0#$90#$90#$B7'";',
  8456. '$mod.s = "abc€";',
  8457. '$mod.s = "ä€";',
  8458. '$mod.s = "€abc";',
  8459. '$mod.s = "ۊ";',
  8460. '$mod.s="";',
  8461. '$mod.s = $mod.s;',
  8462. '$mod.s = $mod.s.concat("a", $mod.s);',
  8463. '$mod.s = "úč";',
  8464. '$mod.i = 1 + $mod.a.length;',
  8465. '$mod.s = "a/b";',
  8466. '']));
  8467. end;
  8468. procedure TTestModule.TestStringConst_InvalidUTF16;
  8469. begin
  8470. StartProgram(false);
  8471. Add([
  8472. 'const',
  8473. ' a: char = #$D87E;',
  8474. ' b: string = #$D87E;',
  8475. ' c: string = #$D87E#43;',
  8476. 'begin',
  8477. ' c:=''abc''#$D87E;',
  8478. ' c:=#0#1#2;',
  8479. ' c:=#127;',
  8480. ' c:=#128;',
  8481. ' c:=#255;',
  8482. ' c:=#256;',
  8483. '']);
  8484. ConvertProgram;
  8485. CheckSource('TestStringConst',
  8486. LinesToStr([
  8487. 'this.a = "\uD87E";',
  8488. 'this.b = "\uD87E";',
  8489. 'this.c = "\uD87E+";',
  8490. '']),
  8491. LinesToStr([
  8492. '$mod.c = "abc\uD87E";',
  8493. '$mod.c = "\x00\x01\x02";',
  8494. '$mod.c = "'#127'";',
  8495. '$mod.c = "'#$c2#$80'";',
  8496. '$mod.c = "'#$c3#$BF'";',
  8497. '$mod.c = "'#$c4#$80'";',
  8498. '']));
  8499. end;
  8500. procedure TTestModule.TestStringConstSurrogate;
  8501. begin
  8502. StartProgram(false);
  8503. Add([
  8504. 'var',
  8505. ' s: string;',
  8506. 'begin',
  8507. ' s:=''😊'';', // 1F60A
  8508. ' s:=''Hello ''#55357#56841', // #$D83D#$DE09
  8509. '']);
  8510. ConvertProgram;
  8511. CheckSource('TestStringConstSurrogate',
  8512. LinesToStr([
  8513. 'this.s="";'
  8514. ]),
  8515. LinesToStr([
  8516. '$mod.s="😊";',
  8517. '$mod.s="Hello 😉";'
  8518. ]));
  8519. end;
  8520. procedure TTestModule.TestStringConstWhitespaces;
  8521. begin
  8522. StartProgram(false);
  8523. Add([
  8524. 'var',
  8525. ' s: string;',
  8526. 'begin',
  8527. ' s:=#$2028;', // line separator not supported by some editors, e.g. vsc
  8528. ' s:=''Medium Mathematical Space ''#$205f',
  8529. '']);
  8530. ConvertProgram;
  8531. CheckSource('TestStringConstSurrogate',
  8532. LinesToStr([
  8533. 'this.s="";'
  8534. ]),
  8535. LinesToStr([
  8536. '$mod.s="\u2028";',
  8537. '$mod.s="Medium Mathematical Space \u205F";'
  8538. ]));
  8539. end;
  8540. procedure TTestModule.TestStringConst_Multiline;
  8541. begin
  8542. StartProgram(false);
  8543. Add([
  8544. '{$modeswitch multilinestrings}',
  8545. 'const',
  8546. ' a = ``;',
  8547. ' b = `',
  8548. 'line`;',
  8549. ' c = `Single`;',
  8550. ' d = ````;',
  8551. ' e = `abc``xyz`;',
  8552. ' f = `first''line',
  8553. ' second''line`#10;',
  8554. 'begin',
  8555. '']);
  8556. ConvertProgram;
  8557. CheckSource('TestStringConst_Multiline',
  8558. LinesToStr([
  8559. 'this.a = "";',
  8560. 'this.b = "'+JSONNewLine+'line";',
  8561. 'this.c = "Single";',
  8562. 'this.d = "`";',
  8563. 'this.e = "abc`xyz";',
  8564. 'this.f = "first''line'+JSONNewLine+' second''line\n";',
  8565. '']),
  8566. LinesToStr([
  8567. ]));
  8568. end;
  8569. procedure TTestModule.TestString_Length;
  8570. begin
  8571. StartProgram(false);
  8572. Add('const c = ''foo'';');
  8573. Add('var');
  8574. Add(' s: string;');
  8575. Add(' i: longint;');
  8576. Add('begin');
  8577. Add(' i:=length(s);');
  8578. Add(' i:=length(s+s);');
  8579. Add(' i:=length(''abc'');');
  8580. Add(' i:=length(c);');
  8581. ConvertProgram;
  8582. CheckSource('TestString_Length',
  8583. LinesToStr([
  8584. 'this.c = "foo";',
  8585. 'this.s = "";',
  8586. 'this.i = 0;',
  8587. '']),
  8588. LinesToStr([
  8589. '$mod.i = $mod.s.length;',
  8590. '$mod.i = ($mod.s+$mod.s).length;',
  8591. '$mod.i = "abc".length;',
  8592. '$mod.i = $mod.c.length;',
  8593. '']));
  8594. end;
  8595. procedure TTestModule.TestString_Compare;
  8596. begin
  8597. StartProgram(false);
  8598. Add('var');
  8599. Add(' s, t: string;');
  8600. Add(' b: boolean;');
  8601. Add('begin');
  8602. Add(' b:=s=t;');
  8603. Add(' b:=s<>t;');
  8604. Add(' b:=s>t;');
  8605. Add(' b:=s>=t;');
  8606. Add(' b:=s<t;');
  8607. Add(' b:=s<=t;');
  8608. ConvertProgram;
  8609. CheckSource('TestString_Compare',
  8610. LinesToStr([ // statements
  8611. 'this.s = "";',
  8612. 'this.t = "";',
  8613. 'this.b =false;'
  8614. ]),
  8615. LinesToStr([ // this.$main
  8616. '$mod.b = $mod.s === $mod.t;',
  8617. '$mod.b = $mod.s !== $mod.t;',
  8618. '$mod.b = $mod.s > $mod.t;',
  8619. '$mod.b = $mod.s >= $mod.t;',
  8620. '$mod.b = $mod.s < $mod.t;',
  8621. '$mod.b = $mod.s <= $mod.t;',
  8622. '']));
  8623. end;
  8624. procedure TTestModule.TestString_SetLength;
  8625. begin
  8626. StartProgram(false);
  8627. Add([
  8628. 'procedure Fly(var s: string);',
  8629. 'begin',
  8630. ' SetLength(s,1);',
  8631. 'end;',
  8632. 'procedure Run(var s: unicodestring);',
  8633. 'begin',
  8634. ' SetLength(s,2);',
  8635. 'end;',
  8636. 'var s: string;',
  8637. ' u: unicodestring;',
  8638. 'begin',
  8639. ' SetLength(s,3);',
  8640. ' SetLength(u,4);',
  8641. '']);
  8642. ConvertProgram;
  8643. CheckSource('TestString_SetLength',
  8644. LinesToStr([ // statements
  8645. 'this.Fly = function (s) {',
  8646. ' s.set(rtl.strSetLength(s.get(), 1));',
  8647. '};',
  8648. 'this.Run = function (s) {',
  8649. ' s.set(rtl.strSetLength(s.get(), 2));',
  8650. '};',
  8651. 'this.s = "";',
  8652. 'this.u = "";',
  8653. '']),
  8654. LinesToStr([ // this.$main
  8655. '$mod.s = rtl.strSetLength($mod.s, 3);',
  8656. '$mod.u = rtl.strSetLength($mod.u, 4);'
  8657. ]));
  8658. end;
  8659. procedure TTestModule.TestString_CharAt;
  8660. begin
  8661. StartProgram(false);
  8662. Add([
  8663. 'var',
  8664. ' s: string;',
  8665. ' c: char;',
  8666. ' b: boolean;',
  8667. 'begin',
  8668. ' b:= s[1] = c;',
  8669. ' b:= c = s[1];',
  8670. ' b:= c <> s[1];',
  8671. ' b:= c > s[1];',
  8672. ' b:= c >= s[1];',
  8673. ' b:= c < s[2];',
  8674. ' b:= c <= s[1];',
  8675. ' s[1] := c;',
  8676. ' s[2+3] := c;']);
  8677. ConvertProgram;
  8678. CheckSource('TestString_CharAt',
  8679. LinesToStr([ // statements
  8680. 'this.s = "";',
  8681. 'this.c = "\x00";',
  8682. 'this.b = false;'
  8683. ]),
  8684. LinesToStr([ // this.$main
  8685. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  8686. '$mod.b = $mod.c === $mod.s.charAt(0);',
  8687. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  8688. '$mod.b = $mod.c > $mod.s.charAt(0);',
  8689. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  8690. '$mod.b = $mod.c < $mod.s.charAt(1);',
  8691. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  8692. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  8693. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  8694. '']));
  8695. end;
  8696. procedure TTestModule.TestStringHMinusFail;
  8697. begin
  8698. StartProgram(false);
  8699. Add([
  8700. '{$H-}',
  8701. 'var s: string;',
  8702. 'begin']);
  8703. ConvertProgram;
  8704. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  8705. end;
  8706. procedure TTestModule.TestStr;
  8707. begin
  8708. StartProgram(false);
  8709. Add('var');
  8710. Add(' b: boolean;');
  8711. Add(' i: longint;');
  8712. Add(' d: double;');
  8713. Add(' s: string;');
  8714. Add('begin');
  8715. Add(' str(b,s);');
  8716. Add(' str(i,s);');
  8717. Add(' str(d,s);');
  8718. Add(' str(i:3,s);');
  8719. Add(' str(d:3:2,s);');
  8720. Add(' Str(12.456:12:1,s);');
  8721. Add(' Str(12.456:12,s);');
  8722. Add(' s:=str(b);');
  8723. Add(' s:=str(i);');
  8724. Add(' s:=str(d);');
  8725. Add(' s:=str(i,i);');
  8726. Add(' s:=str(i:3);');
  8727. Add(' s:=str(d:3:2);');
  8728. Add(' s:=str(i:4,i);');
  8729. Add(' s:=str(i,i:5);');
  8730. Add(' s:=str(i:4,i:5);');
  8731. Add(' s:=str(s,s);');
  8732. Add(' s:=str(s,''foo'');');
  8733. ConvertProgram;
  8734. CheckSource('TestStr',
  8735. LinesToStr([ // statements
  8736. 'this.b = false;',
  8737. 'this.i = 0;',
  8738. 'this.d = 0.0;',
  8739. 'this.s = "";',
  8740. '']),
  8741. LinesToStr([ // this.$main
  8742. '$mod.s = ""+$mod.b;',
  8743. '$mod.s = ""+$mod.i;',
  8744. '$mod.s = rtl.floatToStr($mod.d);',
  8745. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8746. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8747. '$mod.s = rtl.floatToStr(12.456,12,1);',
  8748. '$mod.s = rtl.floatToStr(12.456,12);',
  8749. '$mod.s = ""+$mod.b;',
  8750. '$mod.s = ""+$mod.i;',
  8751. '$mod.s = rtl.floatToStr($mod.d);',
  8752. '$mod.s = ""+$mod.i+$mod.i;',
  8753. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  8754. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  8755. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  8756. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  8757. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  8758. '$mod.s = $mod.s + $mod.s;',
  8759. '$mod.s = $mod.s + "foo";',
  8760. '']));
  8761. end;
  8762. procedure TTestModule.TestBaseType_AnsiStringFail;
  8763. begin
  8764. StartProgram(false);
  8765. Add('var s: AnsiString');
  8766. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  8767. ConvertProgram;
  8768. end;
  8769. procedure TTestModule.TestBaseType_WideStringFail;
  8770. begin
  8771. StartProgram(false);
  8772. Add('var s: WideString');
  8773. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  8774. ConvertProgram;
  8775. end;
  8776. procedure TTestModule.TestBaseType_ShortStringFail;
  8777. begin
  8778. StartProgram(false);
  8779. Add('var s: ShortString');
  8780. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  8781. ConvertProgram;
  8782. end;
  8783. procedure TTestModule.TestBaseType_RawByteStringFail;
  8784. begin
  8785. StartProgram(false);
  8786. Add('var s: RawByteString');
  8787. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8788. ConvertProgram;
  8789. end;
  8790. procedure TTestModule.TestTypeShortstring_Fail;
  8791. begin
  8792. StartProgram(false);
  8793. Add('type t = string[12];');
  8794. Add('var s: t;');
  8795. Add('begin');
  8796. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8797. ConvertProgram;
  8798. end;
  8799. procedure TTestModule.TestCharSet_Custom;
  8800. begin
  8801. StartProgram(false);
  8802. Add([
  8803. 'type',
  8804. ' TCharRg = ''a''..''z'';',
  8805. ' TSetOfCharRg = set of TCharRg;',
  8806. ' TCharRg2 = ''m''..''p'';',
  8807. 'const',
  8808. ' crg: TCharRg = ''b'';',
  8809. 'var',
  8810. ' c: char;',
  8811. ' crg2: TCharRg2;',
  8812. ' s: TSetOfCharRg;',
  8813. 'begin',
  8814. ' c:=crg;',
  8815. ' crg:=c;',
  8816. ' crg2:=crg;',
  8817. ' if c=crg then ;',
  8818. ' if crg=c then ;',
  8819. ' if crg=crg2 then ;',
  8820. ' if c in s then ;',
  8821. ' if crg2 in s then ;',
  8822. ' c:=default(TCharRg);',
  8823. '']);
  8824. ConvertProgram;
  8825. CheckSource('TestCharSet_Custom',
  8826. LinesToStr([ // statements
  8827. 'this.crg = "b";',
  8828. 'this.c = "\x00";',
  8829. 'this.crg2 = "m";',
  8830. 'this.s = {};',
  8831. '']),
  8832. LinesToStr([ // this.$main
  8833. '$mod.c = $mod.crg;',
  8834. '$mod.crg = $mod.c;',
  8835. '$mod.crg2 = $mod.crg;',
  8836. 'if ($mod.c === $mod.crg) ;',
  8837. 'if ($mod.crg === $mod.c) ;',
  8838. 'if ($mod.crg === $mod.crg2) ;',
  8839. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8840. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8841. '$mod.c = "a";',
  8842. '']));
  8843. end;
  8844. procedure TTestModule.TestWideChar;
  8845. begin
  8846. StartProgram(false);
  8847. Add([
  8848. 'procedure Fly(var c: char);',
  8849. 'begin',
  8850. 'end;',
  8851. 'procedure Run(var c: widechar);',
  8852. 'begin',
  8853. 'end;',
  8854. 'var',
  8855. ' c: char;',
  8856. ' wc: widechar;',
  8857. ' w: word;',
  8858. 'begin',
  8859. ' Fly(wc);',
  8860. ' Run(c);',
  8861. ' wc:=WideChar(w);',
  8862. ' w:=ord(wc);',
  8863. '']);
  8864. ConvertProgram;
  8865. CheckSource('TestWideChar_VarArg',
  8866. LinesToStr([ // statements
  8867. 'this.Fly = function (c) {',
  8868. '};',
  8869. 'this.Run = function (c) {',
  8870. '};',
  8871. 'this.c = "\x00";',
  8872. 'this.wc = "\x00";',
  8873. 'this.w = 0;',
  8874. '']),
  8875. LinesToStr([ // this.$main
  8876. '$mod.Fly({',
  8877. ' p: $mod,',
  8878. ' get: function () {',
  8879. ' return this.p.wc;',
  8880. ' },',
  8881. ' set: function (v) {',
  8882. ' this.p.wc = v;',
  8883. ' }',
  8884. '});',
  8885. '$mod.Run({',
  8886. ' p: $mod,',
  8887. ' get: function () {',
  8888. ' return this.p.c;',
  8889. ' },',
  8890. ' set: function (v) {',
  8891. ' this.p.c = v;',
  8892. ' }',
  8893. '});',
  8894. '$mod.wc = String.fromCharCode($mod.w);',
  8895. '$mod.w = $mod.wc.charCodeAt();',
  8896. '',
  8897. '']));
  8898. end;
  8899. procedure TTestModule.TestForCharDo;
  8900. begin
  8901. StartProgram(false);
  8902. Add([
  8903. 'var c: char;',
  8904. 'begin',
  8905. ' for c:=''a'' to ''c'' do ;',
  8906. ' for c:=c downto ''a'' do ;',
  8907. ' for c:=''Б'' to ''Я'' do ;',
  8908. '']);
  8909. ConvertProgram;
  8910. CheckSource('TestForCharDo',
  8911. LinesToStr([ // statements
  8912. 'this.c = "\x00";']),
  8913. LinesToStr([ // this.$main
  8914. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8915. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8916. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8917. '']));
  8918. end;
  8919. procedure TTestModule.TestForCharInDo;
  8920. begin
  8921. StartProgram(false);
  8922. Add([
  8923. 'type',
  8924. ' TSetOfChar = set of char;',
  8925. ' TCharRg = ''a''..''z'';',
  8926. ' TSetOfCharRg = set of TCharRg;',
  8927. 'const Foo = ''foo'';',
  8928. 'var',
  8929. ' c,c2: char;',
  8930. ' s: string;',
  8931. ' a1: array of char;',
  8932. ' a2: array[1..3] of char;',
  8933. ' soc: TSetOfChar;',
  8934. ' socr: TSetOfCharRg;',
  8935. ' cr: TCharRg;',
  8936. 'begin',
  8937. ' for c in foo do ;',
  8938. ' for c in s do ;',
  8939. ' for c in char do ;',
  8940. ' for c in a1 do ;',
  8941. ' for c in a2 do ;',
  8942. ' for c in [''1''..''3''] do ;',
  8943. ' for c in TSetOfChar do ;',
  8944. ' for c in TCharRg do ;',
  8945. ' for c in soc do c2:=c;',
  8946. ' for c in TSetOfCharRg do ;',
  8947. ' for c in socr do ;',
  8948. ' for cr in TCharRg do ;',
  8949. ' for cr in TSetOfCharRg do ;',
  8950. ' for cr in socr do ;',
  8951. '']);
  8952. ConvertProgram;
  8953. CheckSource('TestForCharInDo',
  8954. LinesToStr([ // statements
  8955. 'this.Foo = "foo";',
  8956. 'this.c = "\x00";',
  8957. 'this.c2 = "\x00";',
  8958. 'this.s = "";',
  8959. 'this.a1 = [];',
  8960. 'this.a2 = rtl.arraySetLength(null, "\x00", 3);',
  8961. 'this.soc = {};',
  8962. 'this.socr = {};',
  8963. 'this.cr = "a";',
  8964. '']),
  8965. LinesToStr([ // this.$main
  8966. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8967. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8968. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8969. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8970. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8971. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8972. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8973. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8974. 'for (var $l8 in $mod.soc) {',
  8975. ' $mod.c = String.fromCharCode($l8);',
  8976. ' $mod.c2 = $mod.c;',
  8977. '};',
  8978. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8979. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8980. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8981. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8982. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8983. '']));
  8984. end;
  8985. procedure TTestModule.TestProcTwoArgs;
  8986. begin
  8987. StartProgram(false);
  8988. Add('procedure Test(a,b: longint);');
  8989. Add('begin');
  8990. Add('end;');
  8991. Add('begin');
  8992. ConvertProgram;
  8993. CheckSource('TestProcTwoArgs',
  8994. LinesToStr([ // statements
  8995. 'this.Test = function (a,b) {',
  8996. '};'
  8997. ]),
  8998. LinesToStr([ // this.$main
  8999. ''
  9000. ]));
  9001. end;
  9002. procedure TTestModule.TestProc_DefaultValue;
  9003. begin
  9004. StartProgram(false);
  9005. Add('procedure p1(i: longint = 1);');
  9006. Add('begin');
  9007. Add('end;');
  9008. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  9009. Add('begin');
  9010. Add('end;');
  9011. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  9012. Add('begin');
  9013. Add('end;');
  9014. Add('begin');
  9015. Add(' p1;');
  9016. Add(' p1();');
  9017. Add(' p1(11);');
  9018. Add(' p2;');
  9019. Add(' p2();');
  9020. Add(' p2(12);');
  9021. Add(' p2(13,''b'');');
  9022. Add(' p3();');
  9023. ConvertProgram;
  9024. CheckSource('TestProc_DefaultValue',
  9025. LinesToStr([ // statements
  9026. 'this.p1 = function (i) {',
  9027. '};',
  9028. 'this.p2 = function (i,c) {',
  9029. '};',
  9030. 'this.p3 = function (d,b,s) {',
  9031. '};'
  9032. ]),
  9033. LinesToStr([ // this.$main
  9034. ' $mod.p1(1);',
  9035. ' $mod.p1(1);',
  9036. ' $mod.p1(11);',
  9037. ' $mod.p2(1,"a");',
  9038. ' $mod.p2(1,"a");',
  9039. ' $mod.p2(12,"a");',
  9040. ' $mod.p2(13,"b");',
  9041. ' $mod.p3(1.0,false,"abc");'
  9042. ]));
  9043. end;
  9044. procedure TTestModule.TestFunctionInt;
  9045. begin
  9046. StartProgram(false);
  9047. Add('function MyTest(Bar: longint): longint;');
  9048. Add('begin');
  9049. Add(' Result:=2*bar');
  9050. Add('end;');
  9051. Add('begin');
  9052. ConvertProgram;
  9053. CheckSource('TestFunctionInt',
  9054. LinesToStr([ // statements
  9055. 'this.MyTest = function (Bar) {',
  9056. ' var Result = 0;',
  9057. ' Result = 2*Bar;',
  9058. ' return Result;',
  9059. '};'
  9060. ]),
  9061. LinesToStr([ // this.$main
  9062. ''
  9063. ]));
  9064. end;
  9065. procedure TTestModule.TestFunctionString;
  9066. begin
  9067. StartProgram(false);
  9068. Add('function Test(Bar: string): string;');
  9069. Add('begin');
  9070. Add(' Result:=bar+BAR');
  9071. Add('end;');
  9072. Add('begin');
  9073. ConvertProgram;
  9074. CheckSource('TestFunctionString',
  9075. LinesToStr([ // statements
  9076. 'this.Test = function (Bar) {',
  9077. ' var Result = "";',
  9078. ' Result = Bar+Bar;',
  9079. ' return Result;',
  9080. '};'
  9081. ]),
  9082. LinesToStr([ // this.$main
  9083. ''
  9084. ]));
  9085. end;
  9086. procedure TTestModule.TestIfThen;
  9087. begin
  9088. StartProgram(false);
  9089. Add([
  9090. 'var b: boolean;',
  9091. 'begin',
  9092. ' if b then ;',
  9093. ' if b then else ;']);
  9094. ConvertProgram;
  9095. CheckSource('TestIfThen',
  9096. LinesToStr([ // statements
  9097. 'this.b = false;',
  9098. '']),
  9099. LinesToStr([ // this.$main
  9100. 'if ($mod.b) ;',
  9101. 'if ($mod.b) ;',
  9102. '']));
  9103. end;
  9104. procedure TTestModule.TestForLoop;
  9105. begin
  9106. StartProgram(false);
  9107. Add('var');
  9108. Add(' vI, vJ, vN: longint;');
  9109. Add('begin');
  9110. Add(' VJ:=0;');
  9111. Add(' VN:=3;');
  9112. Add(' for VI:=1 to VN do');
  9113. Add(' begin');
  9114. Add(' VJ:=VJ+VI;');
  9115. Add(' end;');
  9116. ConvertProgram;
  9117. CheckSource('TestForLoop',
  9118. LinesToStr([ // statements
  9119. 'this.vI = 0;',
  9120. 'this.vJ = 0;',
  9121. 'this.vN = 0;'
  9122. ]),
  9123. LinesToStr([ // this.$main
  9124. ' $mod.vJ = 0;',
  9125. ' $mod.vN = 3;',
  9126. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  9127. ' $mod.vI = $l;',
  9128. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9129. ' };',
  9130. '']));
  9131. end;
  9132. procedure TTestModule.TestForLoopInsideFunction;
  9133. begin
  9134. StartProgram(false);
  9135. Add('function SumNumbers(Count: longint): longint;');
  9136. Add('var');
  9137. Add(' vI, vJ: longint;');
  9138. Add('begin');
  9139. Add(' vj:=0;');
  9140. Add(' for vi:=1 to count do');
  9141. Add(' begin');
  9142. Add(' vj:=vj+vi;');
  9143. Add(' end;');
  9144. Add('end;');
  9145. Add('begin');
  9146. Add(' sumnumbers(3);');
  9147. ConvertProgram;
  9148. CheckSource('TestForLoopInsideFunction',
  9149. LinesToStr([ // statements
  9150. 'this.SumNumbers = function (Count) {',
  9151. ' var Result = 0;',
  9152. ' var vI = 0;',
  9153. ' var vJ = 0;',
  9154. ' vJ = 0;',
  9155. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9156. ' vI = $l;',
  9157. ' vJ = vJ + vI;',
  9158. ' };',
  9159. ' return Result;',
  9160. '};'
  9161. ]),
  9162. LinesToStr([ // $mod.$main
  9163. ' $mod.SumNumbers(3);'
  9164. ]));
  9165. end;
  9166. procedure TTestModule.TestForLoop_ReadVarAfter;
  9167. begin
  9168. StartProgram(false);
  9169. Add('var');
  9170. Add(' vI: longint;');
  9171. Add('begin');
  9172. Add(' for vi:=1 to 2 do ;');
  9173. Add(' if vi=3 then ;');
  9174. ConvertProgram;
  9175. CheckSource('TestForLoop',
  9176. LinesToStr([ // statements
  9177. 'this.vI = 0;'
  9178. ]),
  9179. LinesToStr([ // this.$main
  9180. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  9181. ' if ($mod.vI===3) ;'
  9182. ]));
  9183. end;
  9184. procedure TTestModule.TestForLoop_Nested;
  9185. begin
  9186. StartProgram(false);
  9187. Add('function SumNumbers(Count: longint): longint;');
  9188. Add('var');
  9189. Add(' vI, vJ, vK: longint;');
  9190. Add('begin');
  9191. Add(' VK:=0;');
  9192. Add(' for VI:=1 to count do');
  9193. Add(' begin');
  9194. Add(' for vj:=1 to vi do');
  9195. Add(' begin');
  9196. Add(' vk:=VK+VI;');
  9197. Add(' end;');
  9198. Add(' end;');
  9199. Add('end;');
  9200. Add('begin');
  9201. Add(' sumnumbers(3);');
  9202. ConvertProgram;
  9203. CheckSource('TestForLoopInFunction',
  9204. LinesToStr([ // statements
  9205. 'this.SumNumbers = function (Count) {',
  9206. ' var Result = 0;',
  9207. ' var vI = 0;',
  9208. ' var vJ = 0;',
  9209. ' var vK = 0;',
  9210. ' vK = 0;',
  9211. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  9212. ' vI = $l;',
  9213. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  9214. ' vJ = $l1;',
  9215. ' vK = vK + vI;',
  9216. ' };',
  9217. ' };',
  9218. ' return Result;',
  9219. '};'
  9220. ]),
  9221. LinesToStr([ // $mod.$main
  9222. ' $mod.SumNumbers(3);'
  9223. ]));
  9224. end;
  9225. procedure TTestModule.TestRepeatUntil;
  9226. begin
  9227. StartProgram(false);
  9228. Add('var');
  9229. Add(' vI, vJ, vN: longint;');
  9230. Add('begin');
  9231. Add(' vn:=3;');
  9232. Add(' vj:=0;');
  9233. Add(' VI:=0;');
  9234. Add(' repeat');
  9235. Add(' VI:=vi+1;');
  9236. Add(' vj:=VJ+vI;');
  9237. Add(' until vi>=vn');
  9238. ConvertProgram;
  9239. CheckSource('TestRepeatUntil',
  9240. LinesToStr([ // statements
  9241. 'this.vI = 0;',
  9242. 'this.vJ = 0;',
  9243. 'this.vN = 0;'
  9244. ]),
  9245. LinesToStr([ // $mod.$main
  9246. ' $mod.vN = 3;',
  9247. ' $mod.vJ = 0;',
  9248. ' $mod.vI = 0;',
  9249. ' do{',
  9250. ' $mod.vI = $mod.vI + 1;',
  9251. ' $mod.vJ = $mod.vJ + $mod.vI;',
  9252. ' }while(!($mod.vI>=$mod.vN));'
  9253. ]));
  9254. end;
  9255. procedure TTestModule.TestAsmBlock;
  9256. begin
  9257. StartProgram(false);
  9258. Add([
  9259. 'var',
  9260. ' vI: longint;',
  9261. 'begin',
  9262. ' vi:=1;',
  9263. ' asm',
  9264. ' if (vI===1) {',
  9265. ' vI=2;',
  9266. //' console.log(''end;'');', ToDo
  9267. ' }',
  9268. ' if (vI===2){ vI=3; }',
  9269. ' end;',
  9270. ' VI:=4;']);
  9271. ConvertProgram;
  9272. CheckSource('TestAsmBlock',
  9273. LinesToStr([ // statements
  9274. 'this.vI = 0;'
  9275. ]),
  9276. LinesToStr([ // $mod.$main
  9277. '$mod.vI = 1;',
  9278. 'if (vI===1) {',
  9279. ' vI=2;',
  9280. '}',
  9281. 'if (vI===2){ vI=3; }',
  9282. ';',
  9283. '$mod.vI = 4;'
  9284. ]));
  9285. end;
  9286. procedure TTestModule.TestAsmPas_Impl;
  9287. begin
  9288. StartUnit(false);
  9289. Add('interface');
  9290. Add('const cIntf: longint = 1;');
  9291. Add('var vIntf: longint;');
  9292. Add('implementation');
  9293. Add('const cImpl: longint = 2;');
  9294. Add('var vImpl: longint;');
  9295. Add('procedure DoIt;');
  9296. Add('const cLoc: longint = 3;');
  9297. Add('var vLoc: longint;');
  9298. Add('begin;');
  9299. Add(' asm');
  9300. //Add(' pas(vIntf)=pas(cIntf);');
  9301. //Add(' pas(vImpl)=pas(cImpl);');
  9302. //Add(' pas(vLoc)=pas(cLoc);');
  9303. Add(' end;');
  9304. Add('end;');
  9305. ConvertUnit;
  9306. CheckSource('TestAsmPas_Impl',
  9307. LinesToStr([
  9308. 'var $impl = $mod.$impl;',
  9309. 'this.cIntf = 1;',
  9310. 'this.vIntf = 0;',
  9311. '']),
  9312. '', // this.$init
  9313. LinesToStr([ // implementation
  9314. '$impl.cImpl = 2;',
  9315. '$impl.vImpl = 0;',
  9316. 'var cLoc = 3;',
  9317. '$impl.DoIt = function () {',
  9318. ' var vLoc = 0;',
  9319. '};',
  9320. '']) );
  9321. end;
  9322. procedure TTestModule.TestTryFinally;
  9323. begin
  9324. StartProgram(false);
  9325. Add('var i: longint;');
  9326. Add('begin');
  9327. Add(' try');
  9328. Add(' i:=0; i:=2 div i;');
  9329. Add(' finally');
  9330. Add(' i:=3');
  9331. Add(' end;');
  9332. ConvertProgram;
  9333. CheckSource('TestTryFinally',
  9334. LinesToStr([ // statements
  9335. 'this.i = 0;'
  9336. ]),
  9337. LinesToStr([ // $mod.$main
  9338. 'try {',
  9339. ' $mod.i = 0;',
  9340. ' $mod.i = rtl.trunc(2 / $mod.i);',
  9341. '} finally {',
  9342. ' $mod.i = 3;',
  9343. '};'
  9344. ]));
  9345. end;
  9346. procedure TTestModule.TestTryExcept;
  9347. begin
  9348. StartProgram(false);
  9349. Add([
  9350. 'type',
  9351. ' TObject = class end;',
  9352. ' Exception = class Msg: string; end;',
  9353. ' EInvalidCast = class(Exception) end;',
  9354. 'var vI: longint;',
  9355. 'begin',
  9356. ' try',
  9357. ' vi:=1;',
  9358. ' except',
  9359. ' vi:=2',
  9360. ' end;',
  9361. ' try',
  9362. ' vi:=3;',
  9363. ' except',
  9364. ' raise;',
  9365. ' end;',
  9366. ' try',
  9367. ' VI:=4;',
  9368. ' except',
  9369. ' on einvalidcast do',
  9370. ' raise;',
  9371. ' on E: exception do',
  9372. ' if e.msg='''' then',
  9373. ' raise e;',
  9374. ' else',
  9375. ' vi:=5',
  9376. ' end;',
  9377. ' try',
  9378. ' VI:=6;',
  9379. ' except',
  9380. ' on einvalidcast do ;',
  9381. ' end;',
  9382. '']);
  9383. ConvertProgram;
  9384. CheckSource('TestTryExcept',
  9385. LinesToStr([ // statements
  9386. 'rtl.createClass(this, "TObject", null, function () {',
  9387. ' this.$init = function () {',
  9388. ' };',
  9389. ' this.$final = function () {',
  9390. ' };',
  9391. '});',
  9392. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9393. ' this.$init = function () {',
  9394. ' $mod.TObject.$init.call(this);',
  9395. ' this.Msg = "";',
  9396. ' };',
  9397. '});',
  9398. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  9399. '});',
  9400. 'this.vI = 0;'
  9401. ]),
  9402. LinesToStr([ // $mod.$main
  9403. 'try {',
  9404. ' $mod.vI = 1;',
  9405. '} catch ($e) {',
  9406. ' $mod.vI = 2;',
  9407. '};',
  9408. 'try {',
  9409. ' $mod.vI = 3;',
  9410. '} catch ($e) {',
  9411. ' throw $e;',
  9412. '};',
  9413. 'try {',
  9414. ' $mod.vI = 4;',
  9415. '} catch ($e) {',
  9416. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  9417. ' throw $e',
  9418. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  9419. ' var E = $e;',
  9420. ' if (E.Msg === "") throw E;',
  9421. ' } else {',
  9422. ' $mod.vI = 5;',
  9423. ' }',
  9424. '};',
  9425. 'try {',
  9426. ' $mod.vI = 6;',
  9427. '} catch ($e) {',
  9428. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  9429. ' } else throw $e',
  9430. '};',
  9431. '']));
  9432. end;
  9433. procedure TTestModule.TestTryExcept_ReservedWords;
  9434. begin
  9435. StartProgram(false);
  9436. Add([
  9437. 'type',
  9438. ' TObject = class end;',
  9439. ' Exception = class',
  9440. ' Symbol: string;',
  9441. ' end;',
  9442. 'var &try: longint;',
  9443. 'begin',
  9444. ' try',
  9445. ' &try:=4;',
  9446. ' except',
  9447. ' on Error: exception do',
  9448. ' if errOR.symBol='''' then',
  9449. ' raise ERRor;',
  9450. ' end;',
  9451. '']);
  9452. ConvertProgram;
  9453. CheckSource('TestTryExcept_ReservedWords',
  9454. LinesToStr([ // statements
  9455. 'rtl.createClass(this, "TObject", null, function () {',
  9456. ' this.$init = function () {',
  9457. ' };',
  9458. ' this.$final = function () {',
  9459. ' };',
  9460. '});',
  9461. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  9462. ' this.$init = function () {',
  9463. ' $mod.TObject.$init.call(this);',
  9464. ' this.Symbol = "";',
  9465. ' };',
  9466. '});',
  9467. 'this.Try = 0;',
  9468. '']),
  9469. LinesToStr([ // $mod.$main
  9470. 'try {',
  9471. ' $mod.Try = 4;',
  9472. '} catch ($e) {',
  9473. ' if ($mod.Exception.isPrototypeOf($e)) {',
  9474. ' var error = $e;',
  9475. ' if (error.Symbol === "") throw error;',
  9476. ' } else throw $e',
  9477. '};',
  9478. '']));
  9479. end;
  9480. procedure TTestModule.TestIfThenRaiseElse;
  9481. begin
  9482. StartProgram(false);
  9483. Add([
  9484. 'type',
  9485. ' TObject = class',
  9486. ' constructor Create;',
  9487. ' end;',
  9488. 'constructor TObject.Create;',
  9489. 'begin',
  9490. 'end;',
  9491. 'var b: boolean;',
  9492. 'begin',
  9493. ' if b then',
  9494. ' raise TObject.Create',
  9495. ' else',
  9496. ' b:=false;',
  9497. '']);
  9498. ConvertProgram;
  9499. CheckSource('TestIfThenRaiseElse',
  9500. LinesToStr([ // statements
  9501. 'rtl.createClass(this, "TObject", null, function () {',
  9502. ' this.$init = function () {',
  9503. ' };',
  9504. ' this.$final = function () {',
  9505. ' };',
  9506. ' this.Create = function () {',
  9507. ' return this;',
  9508. ' };',
  9509. '});',
  9510. 'this.b = false;',
  9511. '']),
  9512. LinesToStr([ // $mod.$main
  9513. 'if ($mod.b) {',
  9514. ' throw $mod.TObject.$create("Create")}',
  9515. ' else $mod.b = false;',
  9516. '']));
  9517. end;
  9518. procedure TTestModule.TestCaseOf;
  9519. begin
  9520. StartProgram(false);
  9521. Add([
  9522. 'const e: longint; external name ''$e'';',
  9523. 'var vI: longint;',
  9524. 'begin',
  9525. ' case vi of',
  9526. ' 1: ;',
  9527. ' 2: vi:=3;',
  9528. ' e: ;',
  9529. ' else',
  9530. ' VI:=4',
  9531. ' end;']);
  9532. ConvertProgram;
  9533. CheckSource('TestCaseOf',
  9534. LinesToStr([ // statements
  9535. 'this.vI = 0;'
  9536. ]),
  9537. LinesToStr([ // $mod.$main
  9538. 'var $tmp = $mod.vI;',
  9539. 'if ($tmp === 1) {}',
  9540. 'else if ($tmp === 2) {',
  9541. ' $mod.vI = 3}',
  9542. ' else if ($tmp === $e) {}',
  9543. 'else {',
  9544. ' $mod.vI = 4;',
  9545. '};'
  9546. ]));
  9547. end;
  9548. procedure TTestModule.TestCaseOf_UseSwitch;
  9549. begin
  9550. StartProgram(false);
  9551. Converter.UseSwitchStatement:=true;
  9552. Add('var Vi: longint;');
  9553. Add('begin');
  9554. Add(' case vi of');
  9555. Add(' 1: ;');
  9556. Add(' 2: VI:=3;');
  9557. Add(' else');
  9558. Add(' vi:=4');
  9559. Add(' end;');
  9560. ConvertProgram;
  9561. CheckSource('TestCaseOf_UseSwitch',
  9562. LinesToStr([ // statements
  9563. 'this.Vi = 0;'
  9564. ]),
  9565. LinesToStr([ // $mod.$main
  9566. 'switch ($mod.Vi) {',
  9567. 'case 1:',
  9568. ' break;',
  9569. 'case 2:',
  9570. ' $mod.Vi = 3;',
  9571. ' break;',
  9572. 'default:',
  9573. ' $mod.Vi = 4;',
  9574. '};'
  9575. ]));
  9576. end;
  9577. procedure TTestModule.TestCaseOfNoElse;
  9578. begin
  9579. StartProgram(false);
  9580. Add('var Vi: longint;');
  9581. Add('begin');
  9582. Add(' case vi of');
  9583. Add(' 1: begin vi:=2; VI:=3; end;');
  9584. Add(' end;');
  9585. ConvertProgram;
  9586. CheckSource('TestCaseOfNoElse',
  9587. LinesToStr([ // statements
  9588. 'this.Vi = 0;'
  9589. ]),
  9590. LinesToStr([ // $mod.$main
  9591. 'var $tmp = $mod.Vi;',
  9592. 'if ($tmp === 1) {',
  9593. ' $mod.Vi = 2;',
  9594. ' $mod.Vi = 3;',
  9595. '};'
  9596. ]));
  9597. end;
  9598. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  9599. begin
  9600. StartProgram(false);
  9601. Converter.UseSwitchStatement:=true;
  9602. Add('var vI: longint;');
  9603. Add('begin');
  9604. Add(' case vi of');
  9605. Add(' 1: begin VI:=2; vi:=3; end;');
  9606. Add(' end;');
  9607. ConvertProgram;
  9608. CheckSource('TestCaseOfNoElse_UseSwitch',
  9609. LinesToStr([ // statements
  9610. 'this.vI = 0;'
  9611. ]),
  9612. LinesToStr([ // $mod.$main
  9613. 'switch ($mod.vI) {',
  9614. 'case 1:',
  9615. ' $mod.vI = 2;',
  9616. ' $mod.vI = 3;',
  9617. ' break;',
  9618. '};'
  9619. ]));
  9620. end;
  9621. procedure TTestModule.TestCaseOfRange;
  9622. begin
  9623. StartProgram(false);
  9624. Add('var vI: longint;');
  9625. Add('begin');
  9626. Add(' case vi of');
  9627. Add(' 1..3: vi:=14;');
  9628. Add(' 4,5: vi:=16;');
  9629. Add(' 6..7,9..10: ;');
  9630. Add(' else ;');
  9631. Add(' end;');
  9632. ConvertProgram;
  9633. CheckSource('TestCaseOfRange',
  9634. LinesToStr([ // statements
  9635. 'this.vI = 0;'
  9636. ]),
  9637. LinesToStr([ // $mod.$main
  9638. 'var $tmp = $mod.vI;',
  9639. 'if (($tmp >= 1) && ($tmp <= 3)){',
  9640. ' $mod.vI = 14',
  9641. '} else if (($tmp === 4) || ($tmp === 5)){',
  9642. ' $mod.vI = 16',
  9643. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  9644. ]));
  9645. end;
  9646. procedure TTestModule.TestCaseOfString;
  9647. begin
  9648. StartProgram(false);
  9649. Add([
  9650. 'var s,h: string;',
  9651. 'begin',
  9652. ' case s of',
  9653. ' ''foo'': s:=h;',
  9654. ' ''a''..''z'': h:=s;',
  9655. ' ''ў'', ''ё'': ;',
  9656. ' ''Б''..''Я'': ;',
  9657. ' end;',
  9658. '']);
  9659. ConvertProgram;
  9660. CheckSource('TestCaseOfString',
  9661. LinesToStr([ // statements
  9662. 'this.s = "";',
  9663. 'this.h = "";',
  9664. '']),
  9665. LinesToStr([ // $mod.$main
  9666. 'var $tmp = $mod.s;',
  9667. 'if ($tmp === "foo") {',
  9668. ' $mod.s = $mod.h}',
  9669. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  9670. ' $mod.h = $mod.s}',
  9671. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9672. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  9673. '']));
  9674. end;
  9675. procedure TTestModule.TestCaseOfChar;
  9676. begin
  9677. StartProgram(false);
  9678. Add([
  9679. 'var s,h: char;',
  9680. 'begin',
  9681. ' case s of',
  9682. ' ''a''..''z'': h:=s;',
  9683. ' ''ä'': ;',
  9684. ' ''ў'', ''ё'': ;',
  9685. ' ''Б''..''Я'': ;',
  9686. ' end;',
  9687. '']);
  9688. ConvertProgram;
  9689. CheckSource('TestCaseOfString',
  9690. LinesToStr([ // statements
  9691. 'this.s = "\x00";',
  9692. 'this.h = "\x00";',
  9693. '']),
  9694. LinesToStr([ // $mod.$main
  9695. 'var $tmp = $mod.s;',
  9696. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  9697. ' $mod.h = $mod.s}',
  9698. ' else if ($tmp === "ä") {}',
  9699. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  9700. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  9701. '']));
  9702. end;
  9703. procedure TTestModule.TestCaseOfExternalClassConst;
  9704. begin
  9705. StartProgram(false);
  9706. Add([
  9707. '{$modeswitch externalclass}',
  9708. 'type',
  9709. ' TBird = class external name ''Bird''',
  9710. ' const e: longint;',
  9711. ' end;',
  9712. 'var vI: longint;',
  9713. 'begin',
  9714. ' case vi of',
  9715. ' 1: vi:=3;',
  9716. ' TBird.e: ;',
  9717. ' end;']);
  9718. ConvertProgram;
  9719. CheckSource('TestCaseOfExternalClassConst',
  9720. LinesToStr([ // statements
  9721. 'this.vI = 0;'
  9722. ]),
  9723. LinesToStr([ // $mod.$main
  9724. 'var $tmp = $mod.vI;',
  9725. 'if ($tmp === 1) {',
  9726. ' $mod.vI = 3}',
  9727. ' else if ($tmp === Bird.e) ;'
  9728. ]));
  9729. end;
  9730. procedure TTestModule.TestDebugger;
  9731. begin
  9732. StartProgram(false);
  9733. Add([
  9734. 'procedure DoIt;',
  9735. 'begin',
  9736. ' deBugger;',
  9737. ' DeBugger();',
  9738. 'end;',
  9739. 'begin',
  9740. ' Debugger;']);
  9741. ConvertProgram;
  9742. CheckSource('TestDebugger',
  9743. LinesToStr([ // statements
  9744. 'this.DoIt = function () {',
  9745. ' debugger;',
  9746. ' debugger;',
  9747. '};',
  9748. '']),
  9749. LinesToStr([ // $mod.$main
  9750. 'debugger;',
  9751. '']));
  9752. end;
  9753. procedure TTestModule.TestArray_Dynamic;
  9754. begin
  9755. StartProgram(false);
  9756. Add([
  9757. 'type',
  9758. ' TArrayInt = array of longint;',
  9759. 'var',
  9760. ' Arr: TArrayInt;',
  9761. ' i: longint;',
  9762. ' b: boolean;',
  9763. 'begin',
  9764. ' SetLength(arr,3);',
  9765. ' arr[0]:=4;',
  9766. ' arr[1]:=length(arr)+arr[0];',
  9767. ' arr[i]:=5;',
  9768. ' arr[arr[i]]:=arr[6];',
  9769. ' i:=low(arr);',
  9770. ' i:=high(arr);',
  9771. ' b:=Assigned(arr);',
  9772. ' Arr:=default(TArrayInt);']);
  9773. ConvertProgram;
  9774. CheckSource('TestArray_Dynamic',
  9775. LinesToStr([ // statements
  9776. 'this.Arr = [];',
  9777. 'this.i = 0;',
  9778. 'this.b = false;'
  9779. ]),
  9780. LinesToStr([ // $mod.$main
  9781. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  9782. '$mod.Arr[0] = 4;',
  9783. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  9784. '$mod.Arr[$mod.i] = 5;',
  9785. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9786. '$mod.i = 0;',
  9787. '$mod.i = rtl.length($mod.Arr) - 1;',
  9788. '$mod.b = rtl.length($mod.Arr) > 0;',
  9789. '$mod.Arr = [];',
  9790. '']));
  9791. end;
  9792. procedure TTestModule.TestArray_Dynamic_Nil;
  9793. begin
  9794. StartProgram(false);
  9795. Add('type');
  9796. Add(' TArrayInt = array of longint;');
  9797. Add('var');
  9798. Add(' Arr: TArrayInt;');
  9799. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9800. Add('begin');
  9801. Add(' arr:=nil;');
  9802. Add(' if arr=nil then;');
  9803. Add(' if nil=arr then;');
  9804. Add(' if arr<>nil then;');
  9805. Add(' if nil<>arr then;');
  9806. Add(' DoIt(nil,nil);');
  9807. ConvertProgram;
  9808. CheckSource('TestArray_Dynamic',
  9809. LinesToStr([ // statements
  9810. 'this.Arr = [];',
  9811. 'this.DoIt = function(i,j){',
  9812. '};'
  9813. ]),
  9814. LinesToStr([ // $mod.$main
  9815. '$mod.Arr = [];',
  9816. 'if (rtl.length($mod.Arr) === 0) ;',
  9817. 'if (rtl.length($mod.Arr) === 0) ;',
  9818. 'if (rtl.length($mod.Arr) > 0) ;',
  9819. 'if (rtl.length($mod.Arr) > 0) ;',
  9820. '$mod.DoIt([],[]);',
  9821. '']));
  9822. end;
  9823. procedure TTestModule.TestArray_DynMultiDimensional;
  9824. begin
  9825. StartProgram(false);
  9826. Add([
  9827. 'type',
  9828. ' TArrayInt = array of longint;',
  9829. ' TArrayArrayInt = array of TArrayInt;',
  9830. 'var',
  9831. ' Arr: TArrayInt;',
  9832. ' Arr2: TArrayArrayInt;',
  9833. ' i: longint;',
  9834. 'begin',
  9835. ' arr2:=nil;',
  9836. ' if arr2=nil then;',
  9837. ' if nil=arr2 then;',
  9838. ' i:=low(arr2);',
  9839. ' i:=low(arr2[1]);',
  9840. ' i:=high(arr2);',
  9841. ' i:=high(arr2[2]);',
  9842. ' arr2[3]:=arr;',
  9843. ' arr2[4][5]:=i;',
  9844. ' i:=arr2[6][7];',
  9845. ' arr2[8,9]:=i;',
  9846. ' i:=arr2[10,11];',
  9847. ' SetLength(arr2,14);',
  9848. ' SetLength(arr2[15],16);']);
  9849. ConvertProgram;
  9850. CheckSource('TestArray_Dynamic',
  9851. LinesToStr([ // statements
  9852. 'this.Arr = [];',
  9853. 'this.Arr2 = [];',
  9854. 'this.i = 0;'
  9855. ]),
  9856. LinesToStr([ // $mod.$main
  9857. '$mod.Arr2 = [];',
  9858. 'if (rtl.length($mod.Arr2) === 0) ;',
  9859. 'if (rtl.length($mod.Arr2) === 0) ;',
  9860. '$mod.i = 0;',
  9861. '$mod.i = 0;',
  9862. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9863. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9864. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9865. '$mod.Arr2[4][5] = $mod.i;',
  9866. '$mod.i = $mod.Arr2[6][7];',
  9867. '$mod.Arr2[8][9] = $mod.i;',
  9868. '$mod.i = $mod.Arr2[10][11];',
  9869. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9870. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9871. '']));
  9872. end;
  9873. procedure TTestModule.TestArray_DynamicAssign;
  9874. begin
  9875. StartProgram(false);
  9876. Add([
  9877. 'type',
  9878. ' TArrayInt = array of longint;',
  9879. ' TArrayArrayInt = array of TArrayInt;',
  9880. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9881. 'begin',
  9882. 'end;',
  9883. 'procedure Fly(var a: TArrayInt);',
  9884. 'begin',
  9885. 'end;',
  9886. 'var',
  9887. ' Arr: TArrayInt;',
  9888. ' Arr2: TArrayArrayInt;',
  9889. 'begin',
  9890. ' arr:=nil;',
  9891. ' arr2:=nil;',
  9892. ' arr2[1]:=nil;',
  9893. ' arr2[2]:=arr;',
  9894. ' Run(arr,arr,arr);',
  9895. ' Fly(arr);',
  9896. ' Run(arr2[4],arr2[5],arr2[6]);',
  9897. ' Fly(arr2[7]);',
  9898. '']);
  9899. ConvertProgram;
  9900. CheckSource('TestArray_DynamicAssign',
  9901. LinesToStr([ // statements
  9902. 'this.Run = function (a, b, c) {',
  9903. '};',
  9904. 'this.Fly = function (a) {',
  9905. '};',
  9906. 'this.Arr = [];',
  9907. 'this.Arr2 = [];',
  9908. '']),
  9909. LinesToStr([ // $mod.$main
  9910. '$mod.Arr = [];',
  9911. '$mod.Arr2 = [];',
  9912. '$mod.Arr2[1] = [];',
  9913. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9914. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9915. '$mod.Fly({',
  9916. ' p: $mod,',
  9917. ' get: function () {',
  9918. ' return this.p.Arr;',
  9919. ' },',
  9920. ' set: function (v) {',
  9921. ' this.p.Arr = v;',
  9922. ' }',
  9923. '});',
  9924. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9925. '$mod.Fly({',
  9926. ' a: 7,',
  9927. ' p: $mod.Arr2,',
  9928. ' get: function () {',
  9929. ' return this.p[this.a];',
  9930. ' },',
  9931. ' set: function (v) {',
  9932. ' this.p[this.a] = v;',
  9933. ' }',
  9934. '});',
  9935. '']));
  9936. end;
  9937. procedure TTestModule.TestArray_StaticInt;
  9938. begin
  9939. StartProgram(false);
  9940. Add('type');
  9941. Add(' TArrayInt = array[2..4] of longint;');
  9942. Add('var');
  9943. Add(' Arr: TArrayInt;');
  9944. Add(' Arr2: TArrayInt = (5,6,7);');
  9945. Add(' i: longint;');
  9946. Add(' b: boolean;');
  9947. Add('begin');
  9948. Add(' arr[2]:=4;');
  9949. Add(' arr[3]:=arr[2]+arr[3];');
  9950. Add(' arr[i]:=5;');
  9951. Add(' arr[arr[i]]:=arr[high(arr)];');
  9952. Add(' i:=low(arr);');
  9953. Add(' i:=high(arr);');
  9954. Add(' b:=arr[2]=arr[3];');
  9955. Add(' arr:=default(TArrayInt);');
  9956. ConvertProgram;
  9957. CheckSource('TestArray_StaticInt',
  9958. LinesToStr([ // statements
  9959. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9960. 'this.Arr2 = [5, 6, 7];',
  9961. 'this.i = 0;',
  9962. 'this.b = false;'
  9963. ]),
  9964. LinesToStr([ // $mod.$main
  9965. '$mod.Arr[0] = 4;',
  9966. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9967. '$mod.Arr[$mod.i-2] = 5;',
  9968. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9969. '$mod.i = 2;',
  9970. '$mod.i = 4;',
  9971. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9972. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9973. '']));
  9974. end;
  9975. procedure TTestModule.TestArray_StaticBool;
  9976. begin
  9977. StartProgram(false);
  9978. Add('type');
  9979. Add(' TBools = array[boolean] of boolean;');
  9980. Add(' TBool2 = array[true..true] of boolean;');
  9981. Add('var');
  9982. Add(' Arr: TBools;');
  9983. Add(' Arr2: TBool2;');
  9984. Add(' Arr3: TBools = (true,false);');
  9985. Add(' b: boolean;');
  9986. Add('begin');
  9987. Add(' b:=low(arr);');
  9988. Add(' b:=high(arr);');
  9989. Add(' arr[true]:=false;');
  9990. Add(' arr[false]:=arr[b] or arr[true];');
  9991. Add(' arr[b]:=true;');
  9992. Add(' arr[arr[b]]:=arr[high(arr)];');
  9993. Add(' b:=arr[false]=arr[true];');
  9994. Add(' b:=low(arr2);');
  9995. Add(' b:=high(arr2);');
  9996. Add(' arr2[true]:=true;');
  9997. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9998. Add(' arr2[b]:=false;');
  9999. ConvertProgram;
  10000. CheckSource('TestArray_StaticBool',
  10001. LinesToStr([ // statements
  10002. 'this.Arr = rtl.arraySetLength(null,false,2);',
  10003. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  10004. 'this.Arr3 = [true, false];',
  10005. 'this.b = false;'
  10006. ]),
  10007. LinesToStr([ // $mod.$main
  10008. '$mod.b = false;',
  10009. '$mod.b = true;',
  10010. '$mod.Arr[1] = false;',
  10011. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  10012. '$mod.Arr[+$mod.b] = true;',
  10013. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  10014. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  10015. '$mod.b = true;',
  10016. '$mod.b = true;',
  10017. '$mod.Arr2[0] = true;',
  10018. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  10019. '$mod.Arr2[1-$mod.b] = false;',
  10020. '']));
  10021. end;
  10022. procedure TTestModule.TestArray_StaticChar;
  10023. begin
  10024. StartProgram(false);
  10025. Add([
  10026. 'type',
  10027. ' TChars = array[char] of char;',
  10028. ' TChars2 = array[''a''..''z''] of char;',
  10029. 'var',
  10030. ' Arr: TChars;',
  10031. ' Arr2: TChars2;',
  10032. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  10033. ' Arr4: array[11..13] of char = ''pas'';',
  10034. ' Arr5: array[21..22] of char = ''äö'';',
  10035. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  10036. ' c: char;',
  10037. ' b: boolean;',
  10038. 'begin',
  10039. ' c:=low(arr);',
  10040. ' c:=high(arr);',
  10041. ' arr[''B'']:=''a'';',
  10042. ' arr[''D'']:=arr[c];',
  10043. ' arr[c]:=arr[''d''];',
  10044. ' arr[arr[c]]:=arr[high(arr)];',
  10045. ' b:=arr[low(arr)]=arr[''e''];',
  10046. ' c:=low(arr2);',
  10047. ' c:=high(arr2);',
  10048. ' arr2[''b'']:=''f'';',
  10049. ' arr2[''a'']:=arr2[c];',
  10050. ' arr2[c]:=arr2[''g''];']);
  10051. ConvertProgram;
  10052. CheckSource('TestArray_StaticChar',
  10053. LinesToStr([ // statements
  10054. 'this.Arr = rtl.arraySetLength(null, "\x00", 65536);',
  10055. 'this.Arr2 = rtl.arraySetLength(null, "\x00", 26);',
  10056. 'this.Arr3 = ["p", "a", "s"];',
  10057. 'this.Arr4 = ["p", "a", "s"];',
  10058. 'this.Arr5 = ["ä", "ö"];',
  10059. 'this.Arr6 = ["ä", "ö"];',
  10060. 'this.c = "\x00";',
  10061. 'this.b = false;',
  10062. '']),
  10063. LinesToStr([ // $mod.$main
  10064. '$mod.c = "\x00";',
  10065. '$mod.c = "\uFFFF";',
  10066. '$mod.Arr[66] = "a";',
  10067. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  10068. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  10069. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  10070. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  10071. '$mod.c = "a";',
  10072. '$mod.c = "z";',
  10073. '$mod.Arr2[1] = "f";',
  10074. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  10075. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  10076. '']));
  10077. end;
  10078. procedure TTestModule.TestArray_StaticMultiDim;
  10079. begin
  10080. StartProgram(false);
  10081. Add([
  10082. 'type',
  10083. ' TArrayInt = array[1..3] of longint;',
  10084. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10085. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  10086. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  10087. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  10088. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  10089. 'var',
  10090. ' Arr: TArrayInt;',
  10091. ' Arr2: TArrayArrayInt;',
  10092. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10093. ' Arr4: TArrayArrayInt;',
  10094. ' ArrDim2: TArrayDim2Int;',
  10095. ' ArrDim3: TArrayDim3Int;',
  10096. ' ArrDim4: TArrayDim4Int;',
  10097. ' i: longint;',
  10098. 'begin',
  10099. ' i:=low(arr);',
  10100. ' i:=low(arr2);',
  10101. ' i:=low(arr2[5]);',
  10102. ' i:=high(arr);',
  10103. ' i:=high(arr2);',
  10104. ' i:=high(arr2[6]);',
  10105. ' arr2[5]:=arr;',
  10106. ' arr2[6][2]:=i;',
  10107. ' i:=arr2[6][3];',
  10108. ' arr2[6,3]:=i;',
  10109. ' i:=arr2[5,2];',
  10110. ' arr2:=arr2;',// clone multi dim static array
  10111. ' arr3:=arr3;',// clone anonymous multi dim static array
  10112. ' arr4:=arr4;',
  10113. ' Arr:=Arr;',
  10114. ' ArrDim2:=ArrDim2;',
  10115. ' ArrDim3:=ArrDim3;',
  10116. ' ArrDim4:=ArrDim4;',
  10117. '']);
  10118. ConvertProgram;
  10119. CheckSource('TestArray_StaticMultiDim',
  10120. LinesToStr([ // statements
  10121. 'this.TArrayArrayInt$clone = function (a) {',
  10122. ' var b = [];',
  10123. ' b.length = 2;',
  10124. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10125. ' return b;',
  10126. '};',
  10127. 'this.TArrayArrayArrayInt$clone = function (a) {',
  10128. ' var b = [];',
  10129. ' b.length = 2;',
  10130. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  10131. ' return b;',
  10132. '};',
  10133. 'this.TArrayDim2Int$clone = function (a) {',
  10134. ' var b = [];',
  10135. ' b.length = 2;',
  10136. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10137. ' return b;',
  10138. '};',
  10139. 'this.TArrayDim3Int$clone = function (a) {',
  10140. ' var b = [];',
  10141. ' b.length = 2;',
  10142. ' for (var c = 0; c < 2; c++) {',
  10143. ' var d = b[c] = [];',
  10144. ' d.length = 3;',
  10145. ' var e = a[c];',
  10146. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  10147. ' };',
  10148. ' return b;',
  10149. '};',
  10150. 'this.TArrayDim4Int$clone = function (a) {',
  10151. ' var b = [];',
  10152. ' b.length = 2;',
  10153. ' for (var c = 0; c < 2; c++) {',
  10154. ' var d = b[c] = [];',
  10155. ' d.length = 3;',
  10156. ' var e = a[c];',
  10157. ' for (var f = 0; f < 3; f++) {',
  10158. ' var g = d[f] = [];',
  10159. ' g.length = 4;',
  10160. ' var h = e[f];',
  10161. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  10162. ' };',
  10163. ' };',
  10164. ' return b;',
  10165. '};',
  10166. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  10167. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10168. 'this.Arr3$a$clone = function (a) {',
  10169. ' var b = [];',
  10170. ' b.length = 2;',
  10171. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10172. ' return b;',
  10173. '};',
  10174. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10175. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  10176. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  10177. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  10178. 'this.ArrDim4 = rtl.arraySetLength(',
  10179. ' null,',
  10180. ' 0,',
  10181. ' 2,',
  10182. ' 3,',
  10183. ' 4,',
  10184. ' 5',
  10185. ');',
  10186. 'this.i = 0;'
  10187. ]),
  10188. LinesToStr([ // $mod.$main
  10189. '$mod.i = 1;',
  10190. '$mod.i = 5;',
  10191. '$mod.i = 1;',
  10192. '$mod.i = 3;',
  10193. '$mod.i = 6;',
  10194. '$mod.i = 3;',
  10195. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  10196. '$mod.Arr2[1][1] = $mod.i;',
  10197. '$mod.i = $mod.Arr2[1][2];',
  10198. '$mod.Arr2[1][2] = $mod.i;',
  10199. '$mod.i = $mod.Arr2[0][1];',
  10200. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  10201. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  10202. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  10203. '$mod.Arr = $mod.Arr.slice(0);',
  10204. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  10205. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  10206. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  10207. '']));
  10208. end;
  10209. procedure TTestModule.TestArray_StaticInFunction;
  10210. begin
  10211. StartProgram(false);
  10212. Add([
  10213. 'const TArrayInt = 3;',
  10214. 'const TArrayArrayInt = 4;',
  10215. 'procedure DoIt;',
  10216. 'type',
  10217. ' TArrayInt = array[1..3] of longint;',
  10218. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  10219. 'var',
  10220. ' Arr: TArrayInt;',
  10221. ' Arr2: TArrayArrayInt;',
  10222. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  10223. ' i: longint;',
  10224. 'begin',
  10225. ' arr2[5]:=arr;',
  10226. ' arr2:=arr2;',// clone multi dim static array
  10227. ' arr3:=arr3;',// clone multi dim anonymous static array
  10228. 'end;',
  10229. 'begin',
  10230. '']);
  10231. ConvertProgram;
  10232. CheckSource('TestArray_StaticInFunction',
  10233. LinesToStr([ // statements
  10234. 'this.TArrayInt = 3;',
  10235. 'this.TArrayArrayInt = 4;',
  10236. 'var TArrayArrayInt$1$clone = function (a) {',
  10237. ' var b = [];',
  10238. ' b.length = 2;',
  10239. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10240. ' return b;',
  10241. '};',
  10242. 'var Arr3$a$clone = function (a) {',
  10243. ' var b = [];',
  10244. ' b.length = 2;',
  10245. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  10246. ' return b;',
  10247. '};',
  10248. 'this.DoIt = function () {',
  10249. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  10250. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  10251. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  10252. ' var i = 0;',
  10253. ' Arr2[0] = Arr.slice(0);',
  10254. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  10255. ' Arr3 = Arr3$a$clone(Arr3);',
  10256. '};',
  10257. '']),
  10258. LinesToStr([ // $mod.$main
  10259. '']));
  10260. end;
  10261. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  10262. begin
  10263. StartProgram(false);
  10264. Add([
  10265. 'type',
  10266. ' TArrayInt = array[1..3,1..2] of longint;',
  10267. 'var',
  10268. ' a,b: TArrayInt;',
  10269. 'begin',
  10270. ' if a=b then ;',
  10271. '']);
  10272. SetExpectedPasResolverError('compare static array is not supported',
  10273. nXIsNotSupported);
  10274. ConvertProgram;
  10275. end;
  10276. procedure TTestModule.TestArrayOfRecord;
  10277. begin
  10278. StartProgram(false);
  10279. Add([
  10280. 'type',
  10281. ' TRec = record',
  10282. ' Int: longint;',
  10283. ' end;',
  10284. ' TArrayRec = array of TRec;',
  10285. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  10286. 'begin',
  10287. 'end;',
  10288. 'var',
  10289. ' Arr: TArrayRec;',
  10290. ' r: TRec;',
  10291. ' i: longint;',
  10292. 'begin',
  10293. ' SetLength(arr,3);',
  10294. ' arr[0].int:=4;',
  10295. ' arr[1].int:=length(arr)+arr[2].int;',
  10296. ' arr[arr[i].int].int:=arr[5].int;',
  10297. ' arr[7]:=r;',
  10298. ' r:=arr[8];',
  10299. ' i:=low(arr);',
  10300. ' i:=high(arr);',
  10301. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  10302. ConvertProgram;
  10303. CheckSource('TestArrayOfRecord',
  10304. LinesToStr([ // statements
  10305. 'rtl.recNewT(this, "TRec", function () {',
  10306. ' this.Int = 0;',
  10307. ' this.$eq = function (b) {',
  10308. ' return this.Int === b.Int;',
  10309. ' };',
  10310. ' this.$assign = function (s) {',
  10311. ' this.Int = s.Int;',
  10312. ' return this;',
  10313. ' };',
  10314. '});',
  10315. 'this.DoIt = function (vd, vc, vv) {',
  10316. '};',
  10317. 'this.Arr = [];',
  10318. 'this.r = this.TRec.$new();',
  10319. 'this.i = 0;'
  10320. ]),
  10321. LinesToStr([ // $mod.$main
  10322. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  10323. '$mod.Arr[0].Int = 4;',
  10324. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  10325. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  10326. '$mod.Arr[7].$assign($mod.r);',
  10327. '$mod.r.$assign($mod.Arr[8]);',
  10328. '$mod.i = 0;',
  10329. '$mod.i = rtl.length($mod.Arr)-1;',
  10330. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  10331. '']));
  10332. end;
  10333. procedure TTestModule.TestArray_StaticRecord;
  10334. begin
  10335. StartProgram(false);
  10336. Add([
  10337. 'type',
  10338. ' TRec = record',
  10339. ' Int: longint;',
  10340. ' end;',
  10341. ' TArrayRec = array[1..2] of TRec;',
  10342. 'var',
  10343. ' Arr: TArrayRec;',
  10344. 'begin',
  10345. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  10346. '']);
  10347. ConvertProgram;
  10348. CheckSource('TestArray_StaticRecord',
  10349. LinesToStr([ // statements
  10350. 'rtl.recNewT(this, "TRec", function () {',
  10351. ' this.Int = 0;',
  10352. ' this.$eq = function (b) {',
  10353. ' return this.Int === b.Int;',
  10354. ' };',
  10355. ' this.$assign = function (s) {',
  10356. ' this.Int = s.Int;',
  10357. ' return this;',
  10358. ' };',
  10359. '});',
  10360. 'this.TArrayRec$clone = function (a) {',
  10361. ' var b = [];',
  10362. ' b.length = 2;',
  10363. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  10364. ' return b;',
  10365. '};',
  10366. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  10367. '']),
  10368. LinesToStr([ // $mod.$main
  10369. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  10370. end;
  10371. procedure TTestModule.TestArrayOfSet;
  10372. begin
  10373. StartProgram(false);
  10374. Add([
  10375. 'type',
  10376. ' TFlag = (big,small);',
  10377. ' TSetOfFlag = set of tflag;',
  10378. ' TArrayFlag = array of TSetOfFlag;',
  10379. 'procedure DoIt(const a: Tarrayflag);',
  10380. 'begin',
  10381. 'end;',
  10382. 'var',
  10383. ' f: TFlag;',
  10384. ' s: TSetOfFlag;',
  10385. ' Arr: TArrayFlag;',
  10386. ' i: longint;',
  10387. 'begin',
  10388. ' SetLength(arr,3);',
  10389. ' arr[0]:=s;',
  10390. ' arr[1]:=[big];',
  10391. ' arr[2]:=[big]+s;',
  10392. ' arr[3]:=s+[big];',
  10393. ' arr[4]:=arr[5];',
  10394. ' s:=arr[6];',
  10395. ' i:=low(arr);',
  10396. ' i:=high(arr);',
  10397. ' DoIt(arr);',
  10398. ' DoIt([s]);',
  10399. ' DoIt([[],s]);',
  10400. ' DoIt([s,[]]);',
  10401. '']);
  10402. ConvertProgram;
  10403. CheckSource('TestArrayOfSet',
  10404. LinesToStr([ // statements
  10405. 'this.TFlag = {',
  10406. ' "0": "big",',
  10407. ' big: 0,',
  10408. ' "1": "small",',
  10409. ' small: 1',
  10410. '};',
  10411. 'this.DoIt = function (a) {',
  10412. '};',
  10413. 'this.f = 0;',
  10414. 'this.s = {};',
  10415. 'this.Arr = [];',
  10416. 'this.i = 0;',
  10417. '']),
  10418. LinesToStr([ // $mod.$main
  10419. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  10420. '$mod.Arr[0] = rtl.refSet($mod.s);',
  10421. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  10422. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  10423. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  10424. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  10425. '$mod.s = rtl.refSet($mod.Arr[6]);',
  10426. '$mod.i = 0;',
  10427. '$mod.i = rtl.length($mod.Arr) - 1;',
  10428. '$mod.DoIt($mod.Arr);',
  10429. '$mod.DoIt([rtl.refSet($mod.s)]);',
  10430. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  10431. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  10432. '']));
  10433. end;
  10434. procedure TTestModule.TestArray_DynAsParam;
  10435. begin
  10436. StartProgram(false);
  10437. Add([
  10438. 'type integer = longint;',
  10439. 'type TArrInt = array of integer;',
  10440. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10441. 'var vJ: TArrInt;',
  10442. 'begin',
  10443. ' vg:=vg;',
  10444. ' vj:=vh;',
  10445. ' vi:=vi;',
  10446. ' doit(vg,vg,vg);',
  10447. ' doit(vh,vh,vj);',
  10448. ' doit(vi,vi,vi);',
  10449. ' doit(vj,vj,vj);',
  10450. 'end;',
  10451. 'var i: TArrInt;',
  10452. 'begin',
  10453. ' doit(i,i,i);']);
  10454. ConvertProgram;
  10455. CheckSource('TestArray_DynAsParams',
  10456. LinesToStr([ // statements
  10457. 'this.DoIt = function (vG,vH,vI) {',
  10458. ' var vJ = [];',
  10459. ' vG = rtl.arrayRef(vG);',
  10460. ' vJ = rtl.arrayRef(vH);',
  10461. ' vI.set(rtl.arrayRef(vI.get()));',
  10462. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  10463. ' get: function () {',
  10464. ' return vG;',
  10465. ' },',
  10466. ' set: function (v) {',
  10467. ' vG = v;',
  10468. ' }',
  10469. ' });',
  10470. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  10471. ' get: function () {',
  10472. ' return vJ;',
  10473. ' },',
  10474. ' set: function (v) {',
  10475. ' vJ = v;',
  10476. ' }',
  10477. ' });',
  10478. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  10479. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  10480. ' get: function () {',
  10481. ' return vJ;',
  10482. ' },',
  10483. ' set: function (v) {',
  10484. ' vJ = v;',
  10485. ' }',
  10486. ' });',
  10487. '};',
  10488. 'this.i = [];'
  10489. ]),
  10490. LinesToStr([
  10491. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  10492. ' p: $mod,',
  10493. ' get: function () {',
  10494. ' return this.p.i;',
  10495. ' },',
  10496. ' set: function (v) {',
  10497. ' this.p.i = v;',
  10498. ' }',
  10499. '});'
  10500. ]));
  10501. end;
  10502. procedure TTestModule.TestArray_StaticAsParam;
  10503. begin
  10504. StartProgram(false);
  10505. Add([
  10506. 'type integer = longint;',
  10507. 'type TArrInt = array[1..2] of integer;',
  10508. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  10509. 'var vJ: TArrInt;',
  10510. 'begin',
  10511. ' vg:=vg;',
  10512. ' vj:=vh;',
  10513. ' vi:=vi;',
  10514. ' doit(vg,vg,vg);',
  10515. ' doit(vh,vh,vj);',
  10516. ' doit(vi,vi,vi);',
  10517. ' doit(vj,vj,vj);',
  10518. 'end;',
  10519. 'var i: TArrInt;',
  10520. 'begin',
  10521. ' doit(i,i,i);']);
  10522. ConvertProgram;
  10523. CheckSource('TestArray_StaticAsParams',
  10524. LinesToStr([ // statements
  10525. 'this.DoIt = function (vG,vH,vI) {',
  10526. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  10527. ' vG = vG.slice(0);',
  10528. ' vJ = vH.slice(0);',
  10529. ' vI.set(vI.get().slice(0));',
  10530. ' $mod.DoIt(vG.slice(0), vG, {',
  10531. ' get: function () {',
  10532. ' return vG;',
  10533. ' },',
  10534. ' set: function (v) {',
  10535. ' vG = v;',
  10536. ' }',
  10537. ' });',
  10538. ' $mod.DoIt(vH.slice(0), vH, {',
  10539. ' get: function () {',
  10540. ' return vJ;',
  10541. ' },',
  10542. ' set: function (v) {',
  10543. ' vJ = v;',
  10544. ' }',
  10545. ' });',
  10546. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  10547. ' $mod.DoIt(vJ.slice(0), vJ, {',
  10548. ' get: function () {',
  10549. ' return vJ;',
  10550. ' },',
  10551. ' set: function (v) {',
  10552. ' vJ = v;',
  10553. ' }',
  10554. ' });',
  10555. '};',
  10556. 'this.i = rtl.arraySetLength(null, 0, 2);'
  10557. ]),
  10558. LinesToStr([
  10559. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  10560. ' p: $mod,',
  10561. ' get: function () {',
  10562. ' return this.p.i;',
  10563. ' },',
  10564. ' set: function (v) {',
  10565. ' this.p.i = v;',
  10566. ' }',
  10567. '});'
  10568. ]));
  10569. end;
  10570. procedure TTestModule.TestArrayElement_AsParams;
  10571. begin
  10572. StartProgram(false);
  10573. Add('type integer = longint;');
  10574. Add('type TArrayInt = array of integer;');
  10575. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  10576. Add('var vJ: tarrayint;');
  10577. Add('begin');
  10578. Add(' vi:=vi;');
  10579. Add(' doit(vi,vi,vi);');
  10580. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  10581. Add('end;');
  10582. Add('var a: TArrayInt;');
  10583. Add('begin');
  10584. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  10585. ConvertProgram;
  10586. CheckSource('TestArrayElement_AsParams',
  10587. LinesToStr([ // statements
  10588. 'this.DoIt = function (vG,vH,vI) {',
  10589. ' var vJ = [];',
  10590. ' vI.set(vI.get());',
  10591. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  10592. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  10593. ' a:1+3,',
  10594. ' p:vJ,',
  10595. ' get: function () {',
  10596. ' return this.p[this.a];',
  10597. ' },',
  10598. ' set: function (v) {',
  10599. ' this.p[this.a] = v;',
  10600. ' }',
  10601. ' });',
  10602. '};',
  10603. 'this.a = [];'
  10604. ]),
  10605. LinesToStr([
  10606. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  10607. ' a: 1+6,',
  10608. ' p: $mod.a,',
  10609. ' get: function () {',
  10610. ' return this.p[this.a];',
  10611. ' },',
  10612. ' set: function (v) {',
  10613. ' this.p[this.a] = v;',
  10614. ' }',
  10615. '});'
  10616. ]));
  10617. end;
  10618. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  10619. begin
  10620. StartProgram(false);
  10621. Add('type Integer = longint;');
  10622. Add('type TArrayInt = array of integer;');
  10623. Add('function GetArr(vB: integer = 0): tarrayint;');
  10624. Add('begin');
  10625. Add('end;');
  10626. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10627. Add('begin');
  10628. Add('end;');
  10629. Add('begin');
  10630. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  10631. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  10632. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  10633. ConvertProgram;
  10634. CheckSource('TestArrayElementFromFuncResult_AsParams',
  10635. LinesToStr([ // statements
  10636. 'this.GetArr = function (vB) {',
  10637. ' var Result = [];',
  10638. ' return Result;',
  10639. '};',
  10640. 'this.DoIt = function (vG,vH,vI) {',
  10641. '};'
  10642. ]),
  10643. LinesToStr([
  10644. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  10645. ' a: 1+3,',
  10646. ' p: $mod.GetArr(0),',
  10647. ' get: function () {',
  10648. ' return this.p[this.a];',
  10649. ' },',
  10650. ' set: function (v) {',
  10651. ' this.p[this.a] = v;',
  10652. ' }',
  10653. '});',
  10654. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  10655. ' a: 2+3,',
  10656. ' p: $mod.GetArr(0),',
  10657. ' get: function () {',
  10658. ' return this.p[this.a];',
  10659. ' },',
  10660. ' set: function (v) {',
  10661. ' this.p[this.a] = v;',
  10662. ' }',
  10663. '});',
  10664. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  10665. ' a: 3+3,',
  10666. ' p: $mod.GetArr(9),',
  10667. ' get: function () {',
  10668. ' return this.p[this.a];',
  10669. ' },',
  10670. ' set: function (v) {',
  10671. ' this.p[this.a] = v;',
  10672. ' }',
  10673. '});',
  10674. '']));
  10675. end;
  10676. procedure TTestModule.TestArrayEnumTypeRange;
  10677. begin
  10678. StartProgram(false);
  10679. Add([
  10680. 'type',
  10681. ' TEnum = (red,blue);',
  10682. ' TEnumArray = array[TEnum] of longint;',
  10683. 'var',
  10684. ' e: TEnum;',
  10685. ' i: longint;',
  10686. ' a: TEnumArray;',
  10687. ' numbers: TEnumArray = (1,2);',
  10688. ' names: array[TEnum] of string = (''red'',''blue'');',
  10689. 'begin',
  10690. ' e:=low(a);',
  10691. ' e:=high(a);',
  10692. ' i:=a[red];',
  10693. ' a[e]:=a[e];']);
  10694. ConvertProgram;
  10695. CheckSource('TestArrayEnumTypeRange',
  10696. LinesToStr([ // statements
  10697. ' this.TEnum = {',
  10698. ' "0": "red",',
  10699. ' red: 0,',
  10700. ' "1": "blue",',
  10701. ' blue: 1',
  10702. '};',
  10703. 'this.e = 0;',
  10704. 'this.i = 0;',
  10705. 'this.a = rtl.arraySetLength(null,0,2);',
  10706. 'this.numbers = [1, 2];',
  10707. 'this.names = ["red", "blue"];',
  10708. '']),
  10709. LinesToStr([ // $mod.$main
  10710. '$mod.e = $mod.TEnum.red;',
  10711. '$mod.e = $mod.TEnum.blue;',
  10712. '$mod.i = $mod.a[$mod.TEnum.red];',
  10713. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  10714. '']));
  10715. end;
  10716. procedure TTestModule.TestArray_SetLengthOutArg;
  10717. begin
  10718. StartProgram(false);
  10719. Add([
  10720. 'type TArrInt = array of longint;',
  10721. 'procedure DoIt(out a: TArrInt);',
  10722. 'begin',
  10723. ' SetLength(a,2);',
  10724. 'end;',
  10725. 'begin',
  10726. '']);
  10727. ConvertProgram;
  10728. CheckSource('TestArray_SetLengthOutArg',
  10729. LinesToStr([ // statements
  10730. 'this.DoIt = function (a) {',
  10731. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  10732. '};',
  10733. '']),
  10734. LinesToStr([
  10735. '']));
  10736. end;
  10737. procedure TTestModule.TestArray_SetLengthProperty;
  10738. begin
  10739. StartProgram(false);
  10740. Add('type');
  10741. Add(' TArrInt = array of longint;');
  10742. Add(' TObject = class');
  10743. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  10744. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  10745. Add(' property Colors: TArrInt read GetColors write SetColors;');
  10746. Add(' end;');
  10747. Add('var Obj: TObject;');
  10748. Add('begin');
  10749. Add(' SetLength(Obj.Colors,2);');
  10750. ConvertProgram;
  10751. CheckSource('TestArray_SetLengthProperty',
  10752. LinesToStr([ // statements
  10753. 'rtl.createClass(this, "TObject", null, function () {',
  10754. ' this.$init = function () {',
  10755. ' };',
  10756. ' this.$final = function () {',
  10757. ' };',
  10758. '});',
  10759. 'this.Obj = null;',
  10760. '']),
  10761. LinesToStr([
  10762. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  10763. '']));
  10764. end;
  10765. procedure TTestModule.TestArray_SetLengthMultiDim;
  10766. begin
  10767. StartProgram(false);
  10768. Add([
  10769. 'type',
  10770. ' TArrArrInt = array of array of longint;',
  10771. ' TArrStaInt = array of array[1..2] of longint;',
  10772. 'var',
  10773. ' a: TArrArrInt;',
  10774. ' b: TArrStaInt;',
  10775. 'begin',
  10776. ' SetLength(a,2);',
  10777. ' SetLength(a,3,4);',
  10778. ' SetLength(b,5);',
  10779. '']);
  10780. ConvertProgram;
  10781. CheckSource('TestArray_SetLengthMultiDim',
  10782. LinesToStr([ // statements
  10783. 'this.a = [];',
  10784. 'this.b = [];',
  10785. '']),
  10786. LinesToStr([
  10787. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10788. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10789. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10790. '']));
  10791. end;
  10792. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10793. begin
  10794. StartProgram(false);
  10795. Add([
  10796. 'type',
  10797. ' TStaArr1 = array[1..3] of boolean;',
  10798. //' TStaArr2 = array[5..6] of TStaArr1;',
  10799. ' TDynArr1StaArr1 = array of TStaArr1;',
  10800. //' TDynArr1StaArr2 = array of TStaArr2;',
  10801. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10802. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10803. 'var',
  10804. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10805. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10806. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10807. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10808. 'begin',
  10809. ' SetLength(DynArr1StaArr1,11);',
  10810. ' SetLength(DynArr2StaArr1,12);',
  10811. ' SetLength(DynArr2StaArr1[13],14);',
  10812. ' SetLength(DynArr2StaArr1,15,16);',
  10813. //' SetLength(DynArr1StaArr2,21);',
  10814. //' SetLength(DynArr2StaArr2,22);',
  10815. //' SetLength(DynArr2StaArr2[23],24);',
  10816. //' SetLength(DynArr2StaArr2,25,26);',
  10817. '']);
  10818. ConvertProgram;
  10819. CheckSource('TestArray_DynOfStatic',
  10820. LinesToStr([ // statements
  10821. 'this.DynArr1StaArr1 = [];',
  10822. 'this.DynArr2StaArr1 = [];',
  10823. '']),
  10824. LinesToStr([ // $mod.$main
  10825. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10826. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10827. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10828. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10829. ' $mod.DynArr2StaArr1,',
  10830. ' false,',
  10831. ' 15,',
  10832. ' 16,',
  10833. ' "s",',
  10834. ' 3',
  10835. ');',
  10836. '']));
  10837. end;
  10838. procedure TTestModule.TestArray_OpenArrayOfString;
  10839. begin
  10840. StartProgram(false);
  10841. Add('procedure DoIt(const a: array of String);');
  10842. Add('var');
  10843. Add(' i: longint;');
  10844. Add(' s: string;');
  10845. Add('begin');
  10846. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10847. Add('end;');
  10848. Add('var s: string;');
  10849. Add('begin');
  10850. Add(' DoIt([]);');
  10851. Add(' DoIt([s,''foo'','''',s+s]);');
  10852. ConvertProgram;
  10853. CheckSource('TestArray_OpenArrayOfString',
  10854. LinesToStr([ // statements
  10855. 'this.DoIt = function (a) {',
  10856. ' var i = 0;',
  10857. ' var s = "";',
  10858. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10859. ' i = $l;',
  10860. ' s = a[rtl.length(a) - i - 1];',
  10861. ' };',
  10862. '};',
  10863. 'this.s = "";',
  10864. '']),
  10865. LinesToStr([
  10866. '$mod.DoIt([]);',
  10867. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10868. '']));
  10869. end;
  10870. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10871. begin
  10872. StartProgram(false);
  10873. Add([
  10874. 'type TArr = array of char;',
  10875. 'var',
  10876. ' c: char;',
  10877. ' s: string;',
  10878. ' a: TArr;',
  10879. 'procedure Run(const a: array of char);',
  10880. 'begin',
  10881. ' Run(c);',
  10882. ' Run(s);',
  10883. 'end;',
  10884. 'begin',
  10885. ' a:=c;',
  10886. ' a:=s;',
  10887. ' a:=#13;',
  10888. ' a:=''Foo'';',
  10889. ' Run(c);',
  10890. ' Run(s);',
  10891. '']);
  10892. ConvertProgram;
  10893. CheckSource('TestArray_ArrayOfCharAssignString',
  10894. LinesToStr([ // statements
  10895. 'this.c = "\x00";',
  10896. 'this.s = "";',
  10897. 'this.a = [];',
  10898. 'this.Run = function (a) {',
  10899. ' $mod.Run($mod.c.split(""));',
  10900. ' $mod.Run($mod.s.split(""));',
  10901. '};',
  10902. '']),
  10903. LinesToStr([
  10904. '$mod.a = $mod.c.split("");',
  10905. '$mod.a = $mod.s.split("");',
  10906. '$mod.a = "\r".split("");',
  10907. '$mod.a = "Foo".split("");',
  10908. '$mod.Run($mod.c.split(""));',
  10909. '$mod.Run($mod.s.split(""));',
  10910. '']));
  10911. end;
  10912. procedure TTestModule.TestArray_ConstRef;
  10913. begin
  10914. StartProgram(false);
  10915. Add([
  10916. 'type TArr = array of word;',
  10917. 'procedure Run(constref a: TArr);',
  10918. 'begin',
  10919. 'end;',
  10920. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10921. 'var l: TArr;',
  10922. 'begin',
  10923. ' Run(l);',
  10924. ' Run(a);',
  10925. ' Run(b);',
  10926. ' Run(c);',
  10927. ' Run(d);',
  10928. ' Run(e);',
  10929. 'end;',
  10930. 'begin',
  10931. '']);
  10932. ConvertProgram;
  10933. CheckResolverUnexpectedHints();
  10934. CheckSource('TestArray_ConstRef',
  10935. LinesToStr([ // statements
  10936. 'this.Run = function (a) {',
  10937. '};',
  10938. 'this.Fly = function (a, b, c, d, e) {',
  10939. ' var l = [];',
  10940. ' $mod.Run(l);',
  10941. ' $mod.Run(a);',
  10942. ' $mod.Run(b.get());',
  10943. ' $mod.Run(c.get());',
  10944. ' $mod.Run(d);',
  10945. ' $mod.Run(e);',
  10946. '};',
  10947. '']),
  10948. LinesToStr([
  10949. '']));
  10950. end;
  10951. procedure TTestModule.TestArray_Concat;
  10952. begin
  10953. StartProgram(false);
  10954. Add([
  10955. 'type',
  10956. ' integer = longint;',
  10957. ' TFlag = (big,small);',
  10958. ' TFlags = set of TFlag;',
  10959. ' TRec = record',
  10960. ' i: integer;',
  10961. ' end;',
  10962. ' TArrInt = array of integer;',
  10963. ' TArrRec = array of TRec;',
  10964. ' TArrFlag = array of TFlag;',
  10965. ' TArrSet = array of TFlags;',
  10966. ' TArrJSValue = array of jsvalue;',
  10967. 'var',
  10968. ' ArrInt1, ArrInt2: tarrint;',
  10969. ' ArrRec1, ArrRec2: tarrrec;',
  10970. ' ArrFlag1, ArrFlag2: tarrflag;',
  10971. ' ArrSet1, ArrSet2: tarrset;',
  10972. ' ArrJSValue1, ArrJSValue2: tarrjsvalue;',
  10973. 'begin',
  10974. ' arrint1:=concat(arrint2);',
  10975. ' arrint1:=concat(arrint2,arrint2);',
  10976. ' arrint1:=concat(arrint2,arrint2,arrint2);',
  10977. ' arrrec1:=concat(arrrec2);',
  10978. ' arrrec1:=concat(arrrec2,arrrec2);',
  10979. ' arrrec1:=concat(arrrec2,arrrec2,arrrec2);',
  10980. ' arrset1:=concat(arrset2);',
  10981. ' arrset1:=concat(arrset2,arrset2);',
  10982. ' arrset1:=concat(arrset2,arrset2,arrset2);',
  10983. ' arrjsvalue1:=concat(arrjsvalue2);',
  10984. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2);',
  10985. ' arrjsvalue1:=concat(arrjsvalue2,arrjsvalue2,arrjsvalue2);',
  10986. ' arrint1:=concat([1],arrint2);',
  10987. ' arrflag1:=concat([big]);',
  10988. ' arrflag1:=concat([big],arrflag2);',
  10989. ' arrflag1:=concat(arrflag2,[small]);',
  10990. '']);
  10991. ConvertProgram;
  10992. CheckSource('TestArray_Concat',
  10993. LinesToStr([ // statements
  10994. 'this.TFlag = {',
  10995. ' "0": "big",',
  10996. ' big: 0,',
  10997. ' "1": "small",',
  10998. ' small: 1',
  10999. '};',
  11000. 'rtl.recNewT(this, "TRec", function () {',
  11001. ' this.i = 0;',
  11002. ' this.$eq = function (b) {',
  11003. ' return this.i === b.i;',
  11004. ' };',
  11005. ' this.$assign = function (s) {',
  11006. ' this.i = s.i;',
  11007. ' return this;',
  11008. ' };',
  11009. '});',
  11010. 'this.ArrInt1 = [];',
  11011. 'this.ArrInt2 = [];',
  11012. 'this.ArrRec1 = [];',
  11013. 'this.ArrRec2 = [];',
  11014. 'this.ArrFlag1 = [];',
  11015. 'this.ArrFlag2 = [];',
  11016. 'this.ArrSet1 = [];',
  11017. 'this.ArrSet2 = [];',
  11018. 'this.ArrJSValue1 = [];',
  11019. 'this.ArrJSValue2 = [];',
  11020. '']),
  11021. LinesToStr([ // $mod.$main
  11022. '$mod.ArrInt1 = rtl.arrayRef($mod.ArrInt2);',
  11023. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2);',
  11024. '$mod.ArrInt1 = rtl.arrayConcatN($mod.ArrInt2, $mod.ArrInt2, $mod.ArrInt2);',
  11025. '$mod.ArrRec1 = rtl.arrayRef($mod.ArrRec2);',
  11026. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2);',
  11027. '$mod.ArrRec1 = rtl.arrayConcat($mod.TRec, $mod.ArrRec2, $mod.ArrRec2, $mod.ArrRec2);',
  11028. '$mod.ArrSet1 = rtl.arrayRef($mod.ArrSet2);',
  11029. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2);',
  11030. '$mod.ArrSet1 = rtl.arrayConcat("refSet", $mod.ArrSet2, $mod.ArrSet2, $mod.ArrSet2);',
  11031. '$mod.ArrJSValue1 = rtl.arrayRef($mod.ArrJSValue2);',
  11032. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2);',
  11033. '$mod.ArrJSValue1 = rtl.arrayConcatN($mod.ArrJSValue2, $mod.ArrJSValue2, $mod.ArrJSValue2);',
  11034. '$mod.ArrInt1 = rtl.arrayConcatN([1], $mod.ArrInt2);',
  11035. '$mod.ArrFlag1 = [$mod.TFlag.big];',
  11036. '$mod.ArrFlag1 = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag2);',
  11037. '$mod.ArrFlag1 = rtl.arrayConcatN($mod.ArrFlag2, [$mod.TFlag.small]);',
  11038. '']));
  11039. end;
  11040. procedure TTestModule.TestArray_Concat_Append;
  11041. begin
  11042. StartProgram(false);
  11043. Add([
  11044. 'type',
  11045. ' integer = longint;',
  11046. ' TFlag = (big,small);',
  11047. ' TFlags = set of TFlag;',
  11048. ' TRec = record',
  11049. ' i: integer;',
  11050. ' end;',
  11051. ' TArrInt = array of integer;',
  11052. ' TArrRec = array of TRec;',
  11053. ' TArrFlag = array of TFlag;',
  11054. ' TArrSet = array of TFlags;',
  11055. ' TArrJSValue = array of jsvalue;',
  11056. 'var',
  11057. ' ArrInt: tarrint;',
  11058. ' ArrRec: tarrrec;',
  11059. ' ArrFlag: tarrflag;',
  11060. ' ArrSet: tarrset;',
  11061. ' ArrJSValue: tarrjsvalue;',
  11062. ' r: TRec;',
  11063. ' f: TFlags;',
  11064. 'begin',
  11065. ' // append',
  11066. ' arrint:=concat(arrint);',
  11067. ' arrint:=concat(arrint,[2]);',
  11068. ' arrint:=concat(arrint,[3,4]);',
  11069. ' arrrec:=concat(arrrec);',
  11070. ' arrrec:=concat(arrrec,[r]);',
  11071. ' arrrec:=concat(arrrec,[r,r]);',
  11072. ' arrset:=concat(arrset);',
  11073. ' arrset:=concat(arrset,[f]);',
  11074. ' arrset:=concat(arrset,[f,f]);',
  11075. ' arrjsvalue:=concat(arrjsvalue);',
  11076. ' arrjsvalue:=concat(arrjsvalue,[11]);',
  11077. ' arrjsvalue:=concat(arrjsvalue,[12,13]);',
  11078. ' arrflag:=concat(arrflag);',
  11079. ' arrflag:=concat(arrflag,[small]);',
  11080. ' arrflag:=concat(arrflag,[small,big]);',
  11081. '']);
  11082. ConvertProgram;
  11083. CheckSource('TestArray_Concat_Append',
  11084. LinesToStr([ // statements
  11085. 'this.TFlag = {',
  11086. ' "0": "big",',
  11087. ' big: 0,',
  11088. ' "1": "small",',
  11089. ' small: 1',
  11090. '};',
  11091. 'rtl.recNewT(this, "TRec", function () {',
  11092. ' this.i = 0;',
  11093. ' this.$eq = function (b) {',
  11094. ' return this.i === b.i;',
  11095. ' };',
  11096. ' this.$assign = function (s) {',
  11097. ' this.i = s.i;',
  11098. ' return this;',
  11099. ' };',
  11100. '});',
  11101. 'this.ArrInt = [];',
  11102. 'this.ArrRec = [];',
  11103. 'this.ArrFlag = [];',
  11104. 'this.ArrSet = [];',
  11105. 'this.ArrJSValue = [];',
  11106. 'this.r = this.TRec.$new();',
  11107. 'this.f = {};',
  11108. '']),
  11109. LinesToStr([ // $mod.$main
  11110. '$mod.ArrInt = $mod.ArrInt;',
  11111. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11112. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11113. '$mod.ArrRec = $mod.ArrRec;',
  11114. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11115. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11116. '$mod.ArrSet = $mod.ArrSet;',
  11117. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11118. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11119. '$mod.ArrJSValue = $mod.ArrJSValue;',
  11120. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11121. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11122. '$mod.ArrFlag = $mod.ArrFlag;',
  11123. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11124. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11125. '']));
  11126. end;
  11127. procedure TTestModule.TestArray_Concat_Append_Var;
  11128. begin
  11129. StartProgram(false);
  11130. Add([
  11131. 'type',
  11132. ' TArrInt = array of word;',
  11133. '',
  11134. 'procedure Fly(a: TArrInt; var b: TArrInt);',
  11135. 'begin',
  11136. ' a:=concat(a,[2]);',
  11137. ' b:=concat(b,[2]);',
  11138. 'end;',
  11139. 'var',
  11140. ' ArrInt: tarrint;',
  11141. 'begin',
  11142. ' Fly(ArrInt,ArrInt);',
  11143. '']);
  11144. ConvertProgram;
  11145. CheckSource('TestArray_Concat_Append_Var',
  11146. LinesToStr([ // statements
  11147. 'this.Fly = function (a, b) {',
  11148. ' a = rtl.arrayPushN(a, 2);',
  11149. ' b.set(rtl.arrayPushN(b.get(), 2));',
  11150. '};',
  11151. 'this.ArrInt = [];',
  11152. '']),
  11153. LinesToStr([ // $mod.$main
  11154. '$mod.Fly(rtl.arrayRef($mod.ArrInt), {',
  11155. ' p: $mod,',
  11156. ' get: function () {',
  11157. ' return this.p.ArrInt;',
  11158. ' },',
  11159. ' set: function (v) {',
  11160. ' this.p.ArrInt = v;',
  11161. ' }',
  11162. '});',
  11163. '']));
  11164. end;
  11165. procedure TTestModule.TestArray_Copy;
  11166. begin
  11167. StartProgram(false);
  11168. Add([
  11169. 'type',
  11170. ' integer = longint;',
  11171. ' TFlag = (big,small);',
  11172. ' TFlags = set of TFlag;',
  11173. ' TRec = record',
  11174. ' i: integer;',
  11175. ' end;',
  11176. ' TArrInt = array of integer;',
  11177. ' TArrRec = array of TRec;',
  11178. ' TArrSet = array of TFlags;',
  11179. ' TArrJSValue = array of jsvalue;',
  11180. 'var',
  11181. ' ArrInt: tarrint;',
  11182. ' ArrRec: tarrrec;',
  11183. ' ArrSet: tarrset;',
  11184. ' ArrJSValue: tarrjsvalue;',
  11185. 'begin',
  11186. ' arrint:=copy(arrint);',
  11187. ' arrint:=copy(arrint,2);',
  11188. ' arrint:=copy(arrint,3,4);',
  11189. ' arrint:=copy([1,1],1,2);',
  11190. ' arrrec:=copy(arrrec);',
  11191. ' arrrec:=copy(arrrec,5);',
  11192. ' arrrec:=copy(arrrec,6,7);',
  11193. ' arrset:=copy(arrset);',
  11194. ' arrset:=copy(arrset,8);',
  11195. ' arrset:=copy(arrset,9,10);',
  11196. ' arrjsvalue:=copy(arrjsvalue);',
  11197. ' arrjsvalue:=copy(arrjsvalue,11);',
  11198. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  11199. ' ']);
  11200. ConvertProgram;
  11201. CheckSource('TestArray_Copy',
  11202. LinesToStr([ // statements
  11203. 'this.TFlag = {',
  11204. ' "0": "big",',
  11205. ' big: 0,',
  11206. ' "1": "small",',
  11207. ' small: 1',
  11208. '};',
  11209. 'rtl.recNewT(this, "TRec", function () {',
  11210. ' this.i = 0;',
  11211. ' this.$eq = function (b) {',
  11212. ' return this.i === b.i;',
  11213. ' };',
  11214. ' this.$assign = function (s) {',
  11215. ' this.i = s.i;',
  11216. ' return this;',
  11217. ' };',
  11218. '});',
  11219. 'this.ArrInt = [];',
  11220. 'this.ArrRec = [];',
  11221. 'this.ArrSet = [];',
  11222. 'this.ArrJSValue = [];',
  11223. '']),
  11224. LinesToStr([ // $mod.$main
  11225. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  11226. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  11227. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  11228. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  11229. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  11230. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  11231. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  11232. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  11233. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  11234. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  11235. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  11236. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  11237. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  11238. '']));
  11239. end;
  11240. procedure TTestModule.TestArray_InsertDelete;
  11241. begin
  11242. StartProgram(false);
  11243. Add([
  11244. 'type',
  11245. ' integer = longint;',
  11246. ' TFlag = (big,small);',
  11247. ' TFlags = set of TFlag;',
  11248. ' TRec = record',
  11249. ' i: integer;',
  11250. ' end;',
  11251. ' TArrInt = array of integer;',
  11252. ' TArrRec = array of TRec;',
  11253. ' TArrSet = array of TFlags;',
  11254. ' TArrJSValue = array of jsvalue;',
  11255. ' TArrArrInt = array of TArrInt;',
  11256. 'var',
  11257. ' ArrInt: tarrint;',
  11258. ' ArrRec: tarrrec;',
  11259. ' ArrSet: tarrset;',
  11260. ' ArrJSValue: tarrjsvalue;',
  11261. ' ArrArrInt: TArrArrInt;',
  11262. 'begin',
  11263. ' Insert(1,arrint,2);',
  11264. ' Insert(arrint[3],arrint,4);',
  11265. ' Insert(arrrec[5],arrrec,6);',
  11266. ' Insert(arrset[7],arrset,7);',
  11267. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  11268. ' Insert(10,arrjsvalue,11);',
  11269. ' Insert([23],arrarrint,22);',
  11270. ' Delete(arrint,12,13);',
  11271. ' Delete(arrrec,14,15);',
  11272. ' Delete(arrset,17,18);',
  11273. ' Delete(arrjsvalue,19,10);']);
  11274. ConvertProgram;
  11275. CheckSource('TestArray_InsertDelete',
  11276. LinesToStr([ // statements
  11277. 'this.TFlag = {',
  11278. ' "0": "big",',
  11279. ' big: 0,',
  11280. ' "1": "small",',
  11281. ' small: 1',
  11282. '};',
  11283. 'rtl.recNewT(this, "TRec", function () {',
  11284. ' this.i = 0;',
  11285. ' this.$eq = function (b) {',
  11286. ' return this.i === b.i;',
  11287. ' };',
  11288. ' this.$assign = function (s) {',
  11289. ' this.i = s.i;',
  11290. ' return this;',
  11291. ' };',
  11292. '});',
  11293. 'this.ArrInt = [];',
  11294. 'this.ArrRec = [];',
  11295. 'this.ArrSet = [];',
  11296. 'this.ArrJSValue = [];',
  11297. 'this.ArrArrInt = [];',
  11298. '']),
  11299. LinesToStr([ // $mod.$main
  11300. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  11301. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  11302. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  11303. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  11304. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  11305. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  11306. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  11307. '$mod.ArrInt.splice(12, 13);',
  11308. '$mod.ArrRec.splice(14, 15);',
  11309. '$mod.ArrSet.splice(17, 18);',
  11310. '$mod.ArrJSValue.splice(19, 10);',
  11311. '']));
  11312. end;
  11313. procedure TTestModule.TestArray_Add_Append;
  11314. begin
  11315. StartProgram(false);
  11316. Add([
  11317. '{$modeswitch arrayoperators}',
  11318. 'type',
  11319. ' integer = longint;',
  11320. ' TFlag = (big,small);',
  11321. ' TFlags = set of TFlag;',
  11322. ' TRec = record',
  11323. ' i: integer;',
  11324. ' end;',
  11325. ' TArrInt = array of integer;',
  11326. ' TArrRec = array of TRec;',
  11327. ' TArrFlag = array of TFlag;',
  11328. ' TArrSet = array of TFlags;',
  11329. ' TArrJSValue = array of jsvalue;',
  11330. 'var',
  11331. ' ArrInt: tarrint;',
  11332. ' ArrRec: tarrrec;',
  11333. ' ArrFlag: tarrflag;',
  11334. ' ArrSet: tarrset;',
  11335. ' ArrJSValue: tarrjsvalue;',
  11336. ' r: TRec;',
  11337. ' f: TFlags;',
  11338. 'begin',
  11339. ' // append',
  11340. ' arrint:=arrint+[2];',
  11341. ' arrint:=arrint+[3,4];',
  11342. ' arrrec:=arrrec+[r];',
  11343. ' arrrec:=arrrec+[r,r];',
  11344. ' arrset:=arrset+[f];',
  11345. ' arrset:=arrset+[f,f];',
  11346. ' arrjsvalue:=arrjsvalue+[11];',
  11347. ' arrjsvalue:=arrjsvalue+[12,13];',
  11348. ' arrflag:=arrflag+[small];',
  11349. ' arrflag:=arrflag+[small,big];',
  11350. '']);
  11351. ConvertProgram;
  11352. CheckSource('TestArray_Add_Append',
  11353. LinesToStr([ // statements
  11354. 'this.TFlag = {',
  11355. ' "0": "big",',
  11356. ' big: 0,',
  11357. ' "1": "small",',
  11358. ' small: 1',
  11359. '};',
  11360. 'rtl.recNewT(this, "TRec", function () {',
  11361. ' this.i = 0;',
  11362. ' this.$eq = function (b) {',
  11363. ' return this.i === b.i;',
  11364. ' };',
  11365. ' this.$assign = function (s) {',
  11366. ' this.i = s.i;',
  11367. ' return this;',
  11368. ' };',
  11369. '});',
  11370. 'this.ArrInt = [];',
  11371. 'this.ArrRec = [];',
  11372. 'this.ArrFlag = [];',
  11373. 'this.ArrSet = [];',
  11374. 'this.ArrJSValue = [];',
  11375. 'this.r = this.TRec.$new();',
  11376. 'this.f = {};',
  11377. '']),
  11378. LinesToStr([ // $mod.$main
  11379. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 2);',
  11380. '$mod.ArrInt = rtl.arrayPushN($mod.ArrInt, 3, 4);',
  11381. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r);',
  11382. '$mod.ArrRec = rtl.arrayPush($mod.TRec, $mod.ArrRec, $mod.r, $mod.r);',
  11383. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f);',
  11384. '$mod.ArrSet = rtl.arrayPush("refSet", $mod.ArrSet, $mod.f, $mod.f);',
  11385. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 11);',
  11386. '$mod.ArrJSValue = rtl.arrayPushN($mod.ArrJSValue, 12, 13);',
  11387. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small);',
  11388. '$mod.ArrFlag = rtl.arrayPushN($mod.ArrFlag, $mod.TFlag.small, $mod.TFlag.big);',
  11389. '']));
  11390. end;
  11391. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  11392. begin
  11393. Parser.Options:=Parser.Options+[po_cassignments];
  11394. StartProgram(false);
  11395. Add([
  11396. '{$modeswitch arrayoperators}',
  11397. 'type',
  11398. ' integer = longint;',
  11399. ' TArrInt = array of integer;',
  11400. ' TArrStr = array of string;',
  11401. 'const',
  11402. ' Ints: TArrInt = (1,2,3);',
  11403. ' Aliases: TarrStr = (''foo'',''b'');',
  11404. ' OneInt: TArrInt = (7);',
  11405. ' OneStr: array of integer = (7);',
  11406. ' Chars: array of char = ''aoc'';',
  11407. ' Names: array of string = (''a'',''foo'');',
  11408. ' NameCount = low(Names)+high(Names)+length(Names);',
  11409. 'var i: integer;',
  11410. 'begin',
  11411. ' Ints:=[];',
  11412. ' Ints:=[1,1];',
  11413. ' Ints:=[1]+[2];',
  11414. ' Ints:=[2];',
  11415. ' Ints:=[]+ints;',
  11416. ' Ints:=Ints+[];',
  11417. ' Ints:=Ints+OneInt;',
  11418. ' Ints:=Ints+[1,1];',
  11419. ' Ints:=[i,i]+Ints;',
  11420. ' Ints:=[1]+[i]+[3];',
  11421. '']);
  11422. ConvertProgram;
  11423. CheckSource('TestArray_DynArrayConstObjFPC',
  11424. LinesToStr([ // statements
  11425. 'this.Ints = [1, 2, 3];',
  11426. 'this.Aliases = ["foo", "b"];',
  11427. 'this.OneInt = [7];',
  11428. 'this.OneStr = [7];',
  11429. 'this.Chars = ["a", "o", "c"];',
  11430. 'this.Names = ["a", "foo"];',
  11431. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11432. 'this.i = 0;',
  11433. '']),
  11434. LinesToStr([ // $mod.$main
  11435. '$mod.Ints = [];',
  11436. '$mod.Ints = [1, 1];',
  11437. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  11438. '$mod.Ints = [2];',
  11439. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  11440. '$mod.Ints = $mod.Ints;',
  11441. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  11442. '$mod.Ints = rtl.arrayPushN($mod.Ints, 1, 1);',
  11443. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  11444. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  11445. '']));
  11446. end;
  11447. procedure TTestModule.TestArray_DynArrayConstDelphi;
  11448. begin
  11449. StartProgram(false);
  11450. // Note: const c = [1,1]; defines a set!
  11451. Add([
  11452. '{$mode delphi}',
  11453. 'type',
  11454. ' integer = longint;',
  11455. ' TArrInt = array of integer;',
  11456. ' TArrStr = array of string;',
  11457. 'const',
  11458. ' Ints: TArrInt = [1,1,2];',
  11459. ' Aliases: TarrStr = [''foo'',''b''];',
  11460. ' OneInt: TArrInt = [7];',
  11461. ' OneStr: array of integer = [7]+[8];',
  11462. ' Chars: array of char = ''aoc'';',
  11463. ' Names: array of string = [''a'',''a''];',
  11464. ' NameCount = low(Names)+high(Names)+length(Names);',
  11465. 'begin',
  11466. '']);
  11467. ConvertProgram;
  11468. CheckSource('TestArray_DynArrayConstDelphi',
  11469. LinesToStr([ // statements
  11470. 'this.Ints = [1, 1, 2];',
  11471. 'this.Aliases = ["foo", "b"];',
  11472. 'this.OneInt = [7];',
  11473. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  11474. 'this.Chars = ["a", "o", "c"];',
  11475. 'this.Names = ["a", "a"];',
  11476. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  11477. '']),
  11478. LinesToStr([ // $mod.$main
  11479. '']));
  11480. end;
  11481. procedure TTestModule.TestArray_ArrayLitAsParam;
  11482. begin
  11483. StartProgram(false);
  11484. Add([
  11485. '{$modeswitch arrayoperators}',
  11486. 'type',
  11487. ' integer = longint;',
  11488. ' TArrInt = array of integer;',
  11489. ' TArrSet = array of (red,green,blue);',
  11490. 'procedure DoOpenInt(const a: array of integer); forward;',
  11491. 'procedure DoInt(const a: TArrInt);',
  11492. 'begin',
  11493. ' DoInt(a+[1]);',
  11494. ' DoInt([1]+a);',
  11495. ' DoOpenInt(a);',
  11496. ' DoOpenInt(a+[1]);',
  11497. ' DoOpenInt([1]+a);',
  11498. 'end;',
  11499. 'procedure DoOpenInt(const a: array of integer);',
  11500. 'begin',
  11501. ' DoOpenInt(a+[1]);',
  11502. ' DoOpenInt([1]+a);',
  11503. ' DoInt(a);',
  11504. ' DoInt(a+[1]);',
  11505. ' DoInt([1]+a);',
  11506. 'end;',
  11507. 'procedure DoSet(const a: TArrSet);',
  11508. 'begin',
  11509. ' DoSet(a+[red]);',
  11510. ' DoSet([blue]+a);',
  11511. 'end;',
  11512. 'var',
  11513. ' i: TArrInt;',
  11514. ' s: TArrSet;',
  11515. 'begin',
  11516. ' DoInt([1]);',
  11517. ' DoInt([1]+[2]);',
  11518. ' DoInt(i+[1]);',
  11519. ' DoInt([1]+i);',
  11520. ' DoOpenInt([1]);',
  11521. ' DoOpenInt([1]+[2]);',
  11522. ' DoOpenInt(i+[1]);',
  11523. ' DoOpenInt([1]+i);',
  11524. ' DoSet([red]);',
  11525. ' DoSet([blue]+[green]);',
  11526. ' DoSet(s+[blue]);',
  11527. ' DoSet([red]+s);',
  11528. '']);
  11529. ConvertProgram;
  11530. CheckSource('TestArray_ArrayLitAsParam',
  11531. LinesToStr([ // statements
  11532. 'this.TArrSet$a = {',
  11533. ' "0": "red",',
  11534. ' red: 0,',
  11535. ' "1": "green",',
  11536. ' green: 1,',
  11537. ' "2": "blue",',
  11538. ' blue: 2',
  11539. '};',
  11540. 'this.DoInt = function (a) {',
  11541. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11542. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11543. ' $mod.DoOpenInt(a);',
  11544. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11545. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11546. '};',
  11547. 'this.DoOpenInt = function (a) {',
  11548. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  11549. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  11550. ' $mod.DoInt(a);',
  11551. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  11552. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  11553. '};',
  11554. 'this.DoSet = function (a) {',
  11555. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  11556. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  11557. '};',
  11558. 'this.i = [];',
  11559. 'this.s = [];',
  11560. '']),
  11561. LinesToStr([ // $mod.$main
  11562. '$mod.DoInt([1]);',
  11563. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  11564. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  11565. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  11566. '$mod.DoOpenInt([1]);',
  11567. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  11568. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  11569. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  11570. '$mod.DoSet([$mod.TArrSet$a.red]);',
  11571. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  11572. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  11573. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  11574. '']));
  11575. end;
  11576. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  11577. begin
  11578. StartProgram(false);
  11579. Add([
  11580. '{$modeswitch arrayoperators}',
  11581. 'type',
  11582. ' integer = longint;',
  11583. ' TArrInt = array of integer;',
  11584. ' TArrArrInt = array of TArrInt;',
  11585. 'procedure DoInt(const a: TArrArrInt);',
  11586. 'begin',
  11587. ' DoInt(a+[[1]]);',
  11588. ' DoInt([[1]]+a);',
  11589. ' DoInt(a);',
  11590. 'end;',
  11591. 'var',
  11592. ' i: TArrInt;',
  11593. ' a: TArrArrInt;',
  11594. 'begin',
  11595. ' a:=[[1]];',
  11596. ' a:=[i];',
  11597. ' a:=a+[i];',
  11598. ' a:=[i]+a;',
  11599. ' a:=[[1]+i];',
  11600. ' a:=[[1]+[2]];',
  11601. ' a:=[i+[2]];',
  11602. ' DoInt([[1]]);',
  11603. ' DoInt([[1]+[2],[3,4],[5]]);',
  11604. ' DoInt([i+[1]]+a);',
  11605. ' DoInt([i]+a);',
  11606. '']);
  11607. ConvertProgram;
  11608. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  11609. LinesToStr([ // statements
  11610. 'this.DoInt = function (a) {',
  11611. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  11612. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  11613. ' $mod.DoInt(a);',
  11614. '};',
  11615. 'this.i = [];',
  11616. 'this.a = [];',
  11617. '']),
  11618. LinesToStr([ // $mod.$main
  11619. '$mod.a = [[1]];',
  11620. '$mod.a = [$mod.i];',
  11621. '$mod.a = rtl.arrayPushN($mod.a, $mod.i);',
  11622. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  11623. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  11624. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  11625. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  11626. '$mod.DoInt([[1]]);',
  11627. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  11628. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  11629. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  11630. '']));
  11631. end;
  11632. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  11633. begin
  11634. StartProgram(false);
  11635. Add([
  11636. '{$modeswitch arrayoperators}',
  11637. 'type',
  11638. ' integer = longint;',
  11639. ' TArrInt = array[1..2] of integer;',
  11640. ' TArrArrInt = array of TArrInt;',
  11641. 'procedure DoInt(const a: TArrArrInt);',
  11642. 'begin',
  11643. ' DoInt(a+[[1,2]]);',
  11644. ' DoInt([[1,2]]+a);',
  11645. ' DoInt(a);',
  11646. 'end;',
  11647. 'var',
  11648. ' i: TArrInt;',
  11649. ' a: TArrArrInt;',
  11650. 'begin',
  11651. ' a:=[[1,1]];',
  11652. ' a:=[i];',
  11653. ' a:=a+[i];',
  11654. ' a:=[i]+a;',
  11655. ' DoInt([[1,1]]);',
  11656. ' DoInt([[1,2],[3,4]]);',
  11657. '']);
  11658. ConvertProgram;
  11659. CheckSource('TestArray_ArrayLitStaticAsParam',
  11660. LinesToStr([ // statements
  11661. 'this.DoInt = function (a) {',
  11662. ' $mod.DoInt(rtl.arrayConcat("slice", a, [[1, 2]]));',
  11663. ' $mod.DoInt(rtl.arrayConcat("slice", [[1, 2]], a));',
  11664. ' $mod.DoInt(a);',
  11665. '};',
  11666. 'this.i = rtl.arraySetLength(null, 0, 2);',
  11667. 'this.a = [];',
  11668. '']),
  11669. LinesToStr([ // $mod.$main
  11670. '$mod.a = [[1, 1]];',
  11671. '$mod.a = [$mod.i.slice(0)];',
  11672. '$mod.a = rtl.arrayPush("slice", $mod.a, $mod.i);',
  11673. '$mod.a = rtl.arrayConcat("slice", [$mod.i.slice(0)], $mod.a);',
  11674. '$mod.DoInt([[1, 1]]);',
  11675. '$mod.DoInt([[1, 2], [3, 4]]);',
  11676. '']));
  11677. end;
  11678. procedure TTestModule.TestArray_ForInArrOfString;
  11679. begin
  11680. StartProgram(false);
  11681. Add([
  11682. 'type',
  11683. 'type',
  11684. ' TMonthNameArray = array [1..12] of string;',
  11685. ' TMonthNames = TMonthNameArray;',
  11686. ' TObject = class',
  11687. ' private',
  11688. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  11689. ' public',
  11690. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  11691. ' end;',
  11692. 'var',
  11693. ' f: TObject;',
  11694. ' Month: string;',
  11695. ' Names: array of string = (''a'',''foo'',''bar'');',
  11696. ' i: longint;',
  11697. 'begin',
  11698. ' for Month in f.LongMonthNames do ;',
  11699. ' for Month in Names do ;',
  11700. ' for i:=low(Names) to high(Names) do ;',
  11701. '']);
  11702. ConvertProgram;
  11703. CheckSource('TestArray_ForInArrOfString',
  11704. LinesToStr([ // statements
  11705. 'rtl.createClass(this, "TObject", null, function () {',
  11706. ' this.$init = function () {',
  11707. ' };',
  11708. ' this.$final = function () {',
  11709. ' };',
  11710. '});',
  11711. 'this.f = null;',
  11712. 'this.Month = "";',
  11713. 'this.Names = ["a", "foo", "bar"];',
  11714. 'this.i = 0;',
  11715. '']),
  11716. LinesToStr([ // $mod.$main
  11717. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  11718. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  11719. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  11720. '']));
  11721. end;
  11722. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  11723. begin
  11724. StartProgram(false);
  11725. Add([
  11726. '{$modeswitch externalclass}',
  11727. 'type',
  11728. ' TJSObject = class external name ''Object''',
  11729. ' end;',
  11730. ' TJSArray = class external name ''Array''',
  11731. ' class function isArray(Value: JSValue) : boolean;',
  11732. ' function concat() : TJSArray; varargs;',
  11733. ' end;',
  11734. 'var',
  11735. ' aObj: TJSArray;',
  11736. ' a: array of longint;',
  11737. ' o: TJSObject;',
  11738. 'begin',
  11739. ' if TJSArray.isArray(65) then ;',
  11740. ' aObj:=TJSArray(a).concat(a);',
  11741. ' o:=TJSObject(a);',
  11742. ' aObj:=TJSArray([''bird'',''ant'']);',
  11743. '']);
  11744. ConvertProgram;
  11745. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  11746. LinesToStr([ // statements
  11747. 'this.aObj = null;',
  11748. 'this.a = [];',
  11749. 'this.o = null;',
  11750. '']),
  11751. LinesToStr([ // $mod.$main
  11752. 'if (Array.isArray(65)) ;',
  11753. '$mod.aObj = $mod.a.concat($mod.a);',
  11754. '$mod.o = $mod.a;',
  11755. '$mod.aObj = ["bird", "ant"];',
  11756. '']));
  11757. end;
  11758. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  11759. begin
  11760. StartProgram(false);
  11761. Add([
  11762. '{$modeswitch externalclass}',
  11763. 'type',
  11764. ' TArrStr = array of string;',
  11765. ' TJSArray = class external name ''Array''',
  11766. ' end;',
  11767. ' TJSObject = class external name ''Object''',
  11768. ' end;',
  11769. 'var',
  11770. ' aObj: TJSArray;',
  11771. ' a: TArrStr;',
  11772. ' jo: TJSObject;',
  11773. 'begin',
  11774. ' a:=TArrStr(aObj);',
  11775. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  11776. ' a:=TarrStr(jo);',
  11777. '']);
  11778. ConvertProgram;
  11779. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  11780. LinesToStr([ // statements
  11781. 'this.aObj = null;',
  11782. 'this.a = [];',
  11783. 'this.jo = null;',
  11784. '']),
  11785. LinesToStr([ // $mod.$main
  11786. '$mod.a = $mod.aObj;',
  11787. '$mod.aObj[1] = $mod.aObj[2];',
  11788. '$mod.a = $mod.jo;',
  11789. '']));
  11790. end;
  11791. procedure TTestModule.TestArrayOfConst_TVarRec;
  11792. begin
  11793. StartProgram(true,[supTVarRec]);
  11794. Add([
  11795. 'procedure Say(args: array of const);',
  11796. 'var',
  11797. ' i: longint;',
  11798. ' v: TVarRec;',
  11799. 'begin',
  11800. ' for i:=low(args) to high(args) do begin',
  11801. ' v:=args[i];',
  11802. ' case v.vtype of',
  11803. ' vtInteger: if length(args)=args[i].vInteger then ;',
  11804. ' end;',
  11805. ' end;',
  11806. ' for v in args do ;',
  11807. ' args:=nil;',
  11808. ' SetLength(args,2);',
  11809. 'end;',
  11810. 'begin']);
  11811. ConvertProgram;
  11812. CheckSource('TestArrayOfConst_TVarRec',
  11813. LinesToStr([ // statements
  11814. 'this.Say = function (args) {',
  11815. ' var i = 0;',
  11816. ' var v = pas.system.TVarRec.$new();',
  11817. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  11818. ' i = $l;',
  11819. ' v.$assign(args[i]);',
  11820. ' var $tmp = v.VType;',
  11821. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  11822. ' };',
  11823. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  11824. ' args = [];',
  11825. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  11826. '};',
  11827. '']),
  11828. LinesToStr([ // $mod.$main
  11829. ]));
  11830. end;
  11831. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  11832. begin
  11833. StartProgram(true,[supTVarRec]);
  11834. Add([
  11835. 'procedure Say(args: array of const);',
  11836. 'begin',
  11837. ' Say(args);',
  11838. 'end;',
  11839. 'var',
  11840. ' p: Pointer;',
  11841. ' j: jsvalue;',
  11842. ' c: currency;',
  11843. 'begin',
  11844. ' Say([]);',
  11845. ' Say([1]);',
  11846. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  11847. '']);
  11848. ConvertProgram;
  11849. CheckSource('TestArrayOfConst_PassBaseTypes',
  11850. LinesToStr([ // statements
  11851. 'this.Say = function (args) {',
  11852. ' $mod.Say(args);',
  11853. '};',
  11854. 'this.p = null;',
  11855. 'this.j = undefined;',
  11856. 'this.c = 0;',
  11857. '']),
  11858. LinesToStr([ // $mod.$main
  11859. '$mod.Say([]);',
  11860. '$mod.Say(pas.system.VarRecs(0, 1));',
  11861. '$mod.Say(pas.system.VarRecs(',
  11862. ' 9,',
  11863. ' "c",',
  11864. ' 18,',
  11865. ' "foo",',
  11866. ' 5,',
  11867. ' null,',
  11868. ' 1,',
  11869. ' true,',
  11870. ' 3,',
  11871. ' 1.3,',
  11872. ' 5,',
  11873. ' $mod.p,',
  11874. ' 20,',
  11875. ' $mod.j,',
  11876. ' 12,',
  11877. ' $mod.c',
  11878. ' ));',
  11879. '']));
  11880. end;
  11881. procedure TTestModule.TestArrayOfConst_PassObj;
  11882. begin
  11883. StartProgram(true,[supTVarRec]);
  11884. Add([
  11885. '{$interfaces corba}',
  11886. 'type',
  11887. ' TObject = class',
  11888. ' end;',
  11889. ' TClass = class of TObject;',
  11890. ' IUnknown = interface',
  11891. ' end;',
  11892. 'procedure Say(args: array of const);',
  11893. 'begin',
  11894. 'end;',
  11895. 'var',
  11896. ' o: TObject;',
  11897. ' c: TClass;',
  11898. ' i: IUnknown;',
  11899. 'begin',
  11900. ' Say([o,c,TObject]);',
  11901. ' Say([nil,i]);',
  11902. '']);
  11903. ConvertProgram;
  11904. CheckSource('TestArrayOfConst_PassObj',
  11905. LinesToStr([ // statements
  11906. 'rtl.createClass(this, "TObject", null, function () {',
  11907. ' this.$init = function () {',
  11908. ' };',
  11909. ' this.$final = function () {',
  11910. ' };',
  11911. '});',
  11912. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  11913. 'this.Say = function (args) {',
  11914. '};',
  11915. 'this.o = null;',
  11916. 'this.c = null;',
  11917. 'this.i = null;',
  11918. '']),
  11919. LinesToStr([ // $mod.$main
  11920. '$mod.Say(pas.system.VarRecs(',
  11921. ' 7,',
  11922. ' $mod.o,',
  11923. ' 8,',
  11924. ' $mod.c,',
  11925. ' 8,',
  11926. ' $mod.TObject',
  11927. '));',
  11928. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  11929. '']));
  11930. end;
  11931. procedure TTestModule.TestRecord_Empty;
  11932. begin
  11933. StartProgram(false);
  11934. Add([
  11935. 'type',
  11936. ' TRecA = record',
  11937. ' end;',
  11938. 'var a,b: TRecA;',
  11939. 'begin',
  11940. ' if a=b then ;']);
  11941. ConvertProgram;
  11942. CheckSource('TestRecord_Empty',
  11943. LinesToStr([ // statements
  11944. 'rtl.recNewT(this, "TRecA", function () {',
  11945. ' this.$eq = function (b) {',
  11946. ' return true;',
  11947. ' };',
  11948. ' this.$assign = function (s) {',
  11949. ' return this;',
  11950. ' };',
  11951. '});',
  11952. 'this.a = this.TRecA.$new();',
  11953. 'this.b = this.TRecA.$new();',
  11954. '']),
  11955. LinesToStr([ // $mod.$main
  11956. 'if ($mod.a.$eq($mod.b)) ;'
  11957. ]));
  11958. end;
  11959. procedure TTestModule.TestRecord_Var;
  11960. begin
  11961. StartProgram(false);
  11962. Add('type');
  11963. Add(' TRecA = record');
  11964. Add(' Bold: longint;');
  11965. Add(' end;');
  11966. Add('var Rec: TRecA;');
  11967. Add('begin');
  11968. Add(' rec.bold:=123');
  11969. ConvertProgram;
  11970. CheckSource('TestRecord_Var',
  11971. LinesToStr([ // statements
  11972. 'rtl.recNewT(this, "TRecA", function () {',
  11973. ' this.Bold = 0;',
  11974. ' this.$eq = function (b) {',
  11975. ' return this.Bold === b.Bold;',
  11976. ' };',
  11977. ' this.$assign = function (s) {',
  11978. ' this.Bold = s.Bold;',
  11979. ' return this;',
  11980. ' };',
  11981. '});',
  11982. 'this.Rec = this.TRecA.$new();',
  11983. '']),
  11984. LinesToStr([ // $mod.$main
  11985. '$mod.Rec.Bold = 123;'
  11986. ]));
  11987. end;
  11988. procedure TTestModule.TestRecord_VarExternal;
  11989. begin
  11990. StartProgram(false);
  11991. Add([
  11992. '{$modeswitch externalclass}',
  11993. 'type',
  11994. ' TRecA = record',
  11995. ' i: byte;',
  11996. ' length_: longint external name ''length'';',
  11997. ' end;',
  11998. 'var Rec: TRecA;',
  11999. 'begin',
  12000. ' rec.length_ := rec.length_',
  12001. '']);
  12002. ConvertProgram;
  12003. CheckSource('TestRecord_VarExternal',
  12004. LinesToStr([ // statements
  12005. 'rtl.recNewT(this, "TRecA", function () {',
  12006. ' this.i = 0;',
  12007. ' this.$eq = function (b) {',
  12008. ' return (this.i === b.i) && (this.length === b.length);',
  12009. ' };',
  12010. ' this.$assign = function (s) {',
  12011. ' this.i = s.i;',
  12012. ' this.length = s.length;',
  12013. ' return this;',
  12014. ' };',
  12015. '});',
  12016. 'this.Rec = this.TRecA.$new();',
  12017. '']),
  12018. LinesToStr([ // $mod.$main
  12019. '$mod.Rec.length = $mod.Rec.length;'
  12020. ]));
  12021. end;
  12022. procedure TTestModule.TestRecord_WithDo;
  12023. begin
  12024. StartProgram(false);
  12025. Add('type');
  12026. Add(' TRec = record');
  12027. Add(' vI: longint;');
  12028. Add(' end;');
  12029. Add('var');
  12030. Add(' Int: longint;');
  12031. Add(' r: TRec;');
  12032. Add('begin');
  12033. Add(' with r do');
  12034. Add(' int:=vi;');
  12035. Add(' with r do begin');
  12036. Add(' int:=vi;');
  12037. Add(' vi:=int;');
  12038. Add(' end;');
  12039. ConvertProgram;
  12040. CheckSource('TestWithRecordDo',
  12041. LinesToStr([ // statements
  12042. 'rtl.recNewT(this, "TRec", function () {',
  12043. ' this.vI = 0;',
  12044. ' this.$eq = function (b) {',
  12045. ' return this.vI === b.vI;',
  12046. ' };',
  12047. ' this.$assign = function (s) {',
  12048. ' this.vI = s.vI;',
  12049. ' return this;',
  12050. ' };',
  12051. '});',
  12052. 'this.Int = 0;',
  12053. 'this.r = this.TRec.$new();',
  12054. '']),
  12055. LinesToStr([ // $mod.$main
  12056. 'var $with = $mod.r;',
  12057. '$mod.Int = $with.vI;',
  12058. 'var $with1 = $mod.r;',
  12059. '$mod.Int = $with1.vI;',
  12060. '$with1.vI = $mod.Int;'
  12061. ]));
  12062. end;
  12063. procedure TTestModule.TestRecord_Assign;
  12064. begin
  12065. StartProgram(false);
  12066. Add([
  12067. 'type',
  12068. ' TEnum = (red,green);',
  12069. ' TEnums = set of TEnum;',
  12070. ' TSmallRec = record',
  12071. ' N: longint;',
  12072. ' end;',
  12073. ' TBigRec = record',
  12074. ' Int: longint;',
  12075. ' D: double;',
  12076. ' Arr: array of longint;',
  12077. ' Arr2: array[1..2] of longint;',
  12078. ' Small: TSmallRec;',
  12079. ' Enums: TEnums;',
  12080. ' end;',
  12081. 'var',
  12082. ' r, s: TBigRec;',
  12083. 'begin',
  12084. ' r:=s;',
  12085. ' r:=default(TBigRec);',
  12086. ' r:=default(s);',
  12087. '']);
  12088. ConvertProgram;
  12089. CheckSource('TestRecord_Assign',
  12090. LinesToStr([ // statements
  12091. 'this.TEnum = {',
  12092. ' "0": "red",',
  12093. ' red: 0,',
  12094. ' "1": "green",',
  12095. ' green: 1',
  12096. '};',
  12097. 'rtl.recNewT(this, "TSmallRec", function () {',
  12098. ' this.N = 0;',
  12099. ' this.$eq = function (b) {',
  12100. ' return this.N === b.N;',
  12101. ' };',
  12102. ' this.$assign = function (s) {',
  12103. ' this.N = s.N;',
  12104. ' return this;',
  12105. ' };',
  12106. '});',
  12107. 'rtl.recNewT(this, "TBigRec", function () {',
  12108. ' this.Int = 0;',
  12109. ' this.D = 0.0;',
  12110. ' this.$new = function () {',
  12111. ' var r = Object.create(this);',
  12112. ' r.Arr = [];',
  12113. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  12114. ' r.Small = $mod.TSmallRec.$new();',
  12115. ' r.Enums = {};',
  12116. ' return r;',
  12117. ' };',
  12118. ' this.$eq = function (b) {',
  12119. ' 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);',
  12120. ' };',
  12121. ' this.$assign = function (s) {',
  12122. ' this.Int = s.Int;',
  12123. ' this.D = s.D;',
  12124. ' this.Arr = rtl.arrayRef(s.Arr);',
  12125. ' this.Arr2 = s.Arr2.slice(0);',
  12126. ' this.Small.$assign(s.Small);',
  12127. ' this.Enums = rtl.refSet(s.Enums);',
  12128. ' return this;',
  12129. ' };',
  12130. '});',
  12131. 'this.r = this.TBigRec.$new();',
  12132. 'this.s = this.TBigRec.$new();',
  12133. '']),
  12134. LinesToStr([ // $mod.$main
  12135. '$mod.r.$assign($mod.s);',
  12136. '$mod.r.$assign($mod.TBigRec.$new());',
  12137. '$mod.r.$assign($mod.TBigRec.$new());',
  12138. '']));
  12139. end;
  12140. procedure TTestModule.TestRecord_AsParams;
  12141. begin
  12142. StartProgram(false);
  12143. Add([
  12144. 'type',
  12145. ' integer = longint;',
  12146. ' TRecord = record',
  12147. ' i: integer;',
  12148. ' end;',
  12149. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  12150. 'var vL: TRecord;',
  12151. 'begin',
  12152. ' vd:=vd;',
  12153. ' vd.i:=vd.i;',
  12154. ' vl:=vc;',
  12155. ' vv:=vv;',
  12156. ' vv.i:=vv.i;',
  12157. ' U:=vl;',
  12158. ' U:=vd;',
  12159. ' U:=vc;',
  12160. ' U:=vv;',
  12161. ' vl:=TRecord(U);',
  12162. ' vd:=TRecord(U);',
  12163. ' vv:=TRecord(U);',
  12164. ' doit(vd,vd,vd,vd);',
  12165. ' doit(vc,vc,vl,vl);',
  12166. ' doit(vv,vv,vv,vv);',
  12167. ' doit(vl,vl,vl,vl);',
  12168. ' TRecord(U).i:=3;',
  12169. 'end;',
  12170. 'var i: TRecord;',
  12171. 'begin',
  12172. ' doit(i,i,i,i);',
  12173. '']);
  12174. ConvertProgram;
  12175. CheckSource('TestRecord_AsParams',
  12176. LinesToStr([ // statements
  12177. 'rtl.recNewT(this, "TRecord", function () {',
  12178. ' this.i = 0;',
  12179. ' this.$eq = function (b) {',
  12180. ' return this.i === b.i;',
  12181. ' };',
  12182. ' this.$assign = function (s) {',
  12183. ' this.i = s.i;',
  12184. ' return this;',
  12185. ' };',
  12186. '});',
  12187. 'this.DoIt = function (vD, vC, vV, U) {',
  12188. ' var vL = $mod.TRecord.$new();',
  12189. ' vD.$assign(vD);',
  12190. ' vD.i = vD.i;',
  12191. ' vL.$assign(vC);',
  12192. ' vV.$assign(vV);',
  12193. ' vV.i = vV.i;',
  12194. ' U.$assign(vL);',
  12195. ' U.$assign(vD);',
  12196. ' U.$assign(vC);',
  12197. ' U.$assign(vV);',
  12198. ' vL.$assign(U);',
  12199. ' vD.$assign(U);',
  12200. ' vV.$assign(U);',
  12201. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  12202. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  12203. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  12204. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  12205. ' U.i = 3;',
  12206. '};',
  12207. 'this.i = this.TRecord.$new();'
  12208. ]),
  12209. LinesToStr([
  12210. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  12211. '']));
  12212. end;
  12213. procedure TTestModule.TestRecord_ConstRef;
  12214. begin
  12215. StartProgram(false);
  12216. Add([
  12217. 'type TRec = record i: word; end;',
  12218. 'procedure Run(constref a: TRec);',
  12219. 'begin',
  12220. 'end;',
  12221. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  12222. 'var l: TRec;',
  12223. 'begin',
  12224. ' Run(l);',
  12225. ' Run(a);',
  12226. ' Run(b);',
  12227. ' Run(c);',
  12228. ' Run(d);',
  12229. ' Run(e);',
  12230. 'end;',
  12231. 'begin',
  12232. '']);
  12233. ConvertProgram;
  12234. CheckResolverUnexpectedHints();
  12235. CheckSource('TestRecord_ConstRef',
  12236. LinesToStr([ // statements
  12237. 'rtl.recNewT(this, "TRec", function () {',
  12238. ' this.i = 0;',
  12239. ' this.$eq = function (b) {',
  12240. ' return this.i === b.i;',
  12241. ' };',
  12242. ' this.$assign = function (s) {',
  12243. ' this.i = s.i;',
  12244. ' return this;',
  12245. ' };',
  12246. '});',
  12247. 'this.Run = function (a) {',
  12248. '};',
  12249. 'this.Fly = function (a, b, c, d, e) {',
  12250. ' var l = $mod.TRec.$new();',
  12251. ' $mod.Run(l);',
  12252. ' $mod.Run(a);',
  12253. ' $mod.Run(b);',
  12254. ' $mod.Run(c);',
  12255. ' $mod.Run(d);',
  12256. ' $mod.Run(e);',
  12257. '};',
  12258. '']),
  12259. LinesToStr([
  12260. '']));
  12261. end;
  12262. procedure TTestModule.TestRecordElement_AsParams;
  12263. begin
  12264. StartProgram(false);
  12265. Add('type');
  12266. Add(' integer = longint;');
  12267. Add(' TRecord = record');
  12268. Add(' i: integer;');
  12269. Add(' end;');
  12270. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12271. Add('var vJ: TRecord;');
  12272. Add('begin');
  12273. Add(' doit(vj.i,vj.i,vj.i);');
  12274. Add('end;');
  12275. Add('var r: TRecord;');
  12276. Add('begin');
  12277. Add(' doit(r.i,r.i,r.i);');
  12278. ConvertProgram;
  12279. CheckSource('TestRecordElement_AsParams',
  12280. LinesToStr([ // statements
  12281. 'rtl.recNewT(this, "TRecord", function () {',
  12282. ' this.i = 0;',
  12283. ' this.$eq = function (b) {',
  12284. ' return this.i === b.i;',
  12285. ' };',
  12286. ' this.$assign = function (s) {',
  12287. ' this.i = s.i;',
  12288. ' return this;',
  12289. ' };',
  12290. '});',
  12291. 'this.DoIt = function (vG,vH,vI) {',
  12292. ' var vJ = $mod.TRecord.$new();',
  12293. ' $mod.DoIt(vJ.i, vJ.i, {',
  12294. ' p: vJ,',
  12295. ' get: function () {',
  12296. ' return this.p.i;',
  12297. ' },',
  12298. ' set: function (v) {',
  12299. ' this.p.i = v;',
  12300. ' }',
  12301. ' });',
  12302. '};',
  12303. 'this.r = this.TRecord.$new();'
  12304. ]),
  12305. LinesToStr([
  12306. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  12307. ' p: $mod.r,',
  12308. ' get: function () {',
  12309. ' return this.p.i;',
  12310. ' },',
  12311. ' set: function (v) {',
  12312. ' this.p.i = v;',
  12313. ' }',
  12314. '});'
  12315. ]));
  12316. end;
  12317. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  12318. begin
  12319. StartProgram(false);
  12320. Add('type');
  12321. Add(' integer = longint;');
  12322. Add(' TRecord = record');
  12323. Add(' i: integer;');
  12324. Add(' end;');
  12325. Add('function GetRec(vB: integer = 0): TRecord;');
  12326. Add('begin');
  12327. Add('end;');
  12328. Add('procedure DoIt(vG: integer; const vH: integer);');
  12329. Add('begin');
  12330. Add('end;');
  12331. Add('begin');
  12332. Add(' doit(getrec.i,getrec.i);');
  12333. Add(' doit(getrec().i,getrec().i);');
  12334. Add(' doit(getrec(1).i,getrec(2).i);');
  12335. ConvertProgram;
  12336. CheckSource('TestRecordElementFromFuncResult_AsParams',
  12337. LinesToStr([ // statements
  12338. 'rtl.recNewT(this, "TRecord", function () {',
  12339. ' this.i = 0;',
  12340. ' this.$eq = function (b) {',
  12341. ' return this.i === b.i;',
  12342. ' };',
  12343. ' this.$assign = function (s) {',
  12344. ' this.i = s.i;',
  12345. ' return this;',
  12346. ' };',
  12347. '});',
  12348. 'this.GetRec = function (vB) {',
  12349. ' var Result = $mod.TRecord.$new();',
  12350. ' return Result;',
  12351. '};',
  12352. 'this.DoIt = function (vG, vH) {',
  12353. '};',
  12354. '']),
  12355. LinesToStr([
  12356. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12357. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  12358. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  12359. '']));
  12360. end;
  12361. procedure TTestModule.TestRecordElementFromWith_AsParams;
  12362. begin
  12363. StartProgram(false);
  12364. Add('type');
  12365. Add(' integer = longint;');
  12366. Add(' TRecord = record');
  12367. Add(' i: integer;');
  12368. Add(' end;');
  12369. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  12370. Add('begin');
  12371. Add('end;');
  12372. Add('var r: trecord;');
  12373. Add('begin');
  12374. Add(' with r do ');
  12375. Add(' doit(i,i,i);');
  12376. ConvertProgram;
  12377. CheckSource('TestRecordElementFromWith_AsParams',
  12378. LinesToStr([ // statements
  12379. 'rtl.recNewT(this, "TRecord", function () {',
  12380. ' this.i = 0;',
  12381. ' this.$eq = function (b) {',
  12382. ' return this.i === b.i;',
  12383. ' };',
  12384. ' this.$assign = function (s) {',
  12385. ' this.i = s.i;',
  12386. ' return this;',
  12387. ' };',
  12388. '});',
  12389. 'this.DoIt = function (vG,vH,vI) {',
  12390. '};',
  12391. 'this.r = this.TRecord.$new();'
  12392. ]),
  12393. LinesToStr([
  12394. 'var $with = $mod.r;',
  12395. '$mod.DoIt($with.i,$with.i,{',
  12396. ' p: $with,',
  12397. ' get: function () {',
  12398. ' return this.p.i;',
  12399. ' },',
  12400. ' set: function (v) {',
  12401. ' this.p.i = v;',
  12402. ' }',
  12403. '});',
  12404. '']));
  12405. end;
  12406. procedure TTestModule.TestRecord_Equal;
  12407. begin
  12408. StartProgram(false);
  12409. Add('type');
  12410. Add(' integer = longint;');
  12411. Add(' TFlag = (red,blue);');
  12412. Add(' TFlags = set of TFlag;');
  12413. Add(' TProc = procedure;');
  12414. Add(' TRecord = record');
  12415. Add(' i: integer;');
  12416. Add(' Event: TProc;');
  12417. Add(' f: TFlags;');
  12418. Add(' end;');
  12419. Add(' TNested = record');
  12420. Add(' r: TRecord;');
  12421. Add(' end;');
  12422. Add('var');
  12423. Add(' b: boolean;');
  12424. Add(' r,s: trecord;');
  12425. Add('begin');
  12426. Add(' b:=r=s;');
  12427. Add(' b:=r<>s;');
  12428. ConvertProgram;
  12429. CheckSource('TestRecord_Equal',
  12430. LinesToStr([ // statements
  12431. 'this.TFlag = {',
  12432. ' "0": "red",',
  12433. ' red: 0,',
  12434. ' "1": "blue",',
  12435. ' blue: 1',
  12436. '};',
  12437. 'rtl.recNewT(this, "TRecord", function () {',
  12438. ' this.i = 0;',
  12439. ' this.Event = null;',
  12440. ' this.$new = function () {',
  12441. ' var r = Object.create(this);',
  12442. ' r.f = {};',
  12443. ' return r;',
  12444. ' };',
  12445. ' this.$eq = function (b) {',
  12446. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  12447. ' };',
  12448. ' this.$assign = function (s) {',
  12449. ' this.i = s.i;',
  12450. ' this.Event = s.Event;',
  12451. ' this.f = rtl.refSet(s.f);',
  12452. ' return this;',
  12453. ' };',
  12454. '});',
  12455. 'rtl.recNewT(this, "TNested", function () {',
  12456. ' this.$new = function () {',
  12457. ' var r = Object.create(this);',
  12458. ' r.r = $mod.TRecord.$new();',
  12459. ' return r;',
  12460. ' };',
  12461. ' this.$eq = function (b) {',
  12462. ' return this.r.$eq(b.r);',
  12463. ' };',
  12464. ' this.$assign = function (s) {',
  12465. ' this.r.$assign(s.r);',
  12466. ' return this;',
  12467. ' };',
  12468. '});',
  12469. 'this.b = false;',
  12470. 'this.r = this.TRecord.$new();',
  12471. 'this.s = this.TRecord.$new();',
  12472. '']),
  12473. LinesToStr([
  12474. '$mod.b = $mod.r.$eq($mod.s);',
  12475. '$mod.b = !$mod.r.$eq($mod.s);',
  12476. '']));
  12477. end;
  12478. procedure TTestModule.TestRecord_JSValue;
  12479. begin
  12480. StartProgram(false);
  12481. Add([
  12482. 'type',
  12483. ' TRecord = record',
  12484. ' i: longint;',
  12485. ' end;',
  12486. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  12487. 'begin',
  12488. 'end;',
  12489. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  12490. 'begin',
  12491. ' if jsvalue(d) then ;',
  12492. ' if jsvalue(c) then ;',
  12493. ' if jsvalue(v) then ;',
  12494. 'end;',
  12495. 'var',
  12496. ' Jv: jsvalue;',
  12497. ' Rec: trecord;',
  12498. 'begin',
  12499. ' rec:=trecord(jv);',
  12500. ' jv:=rec;',
  12501. ' Fly(rec,rec);',
  12502. ' Fly(@rec,@rec);',
  12503. ' if jsvalue(Rec) then ;',
  12504. ' Run(trecord(jv),trecord(jv),rec);',
  12505. '']);
  12506. ConvertProgram;
  12507. CheckSource('TestRecord_JSValue',
  12508. LinesToStr([ // statements
  12509. 'rtl.recNewT(this, "TRecord", function () {',
  12510. ' this.i = 0;',
  12511. ' this.$eq = function (b) {',
  12512. ' return this.i === b.i;',
  12513. ' };',
  12514. ' this.$assign = function (s) {',
  12515. ' this.i = s.i;',
  12516. ' return this;',
  12517. ' };',
  12518. '});',
  12519. 'this.Fly = function (d, c) {',
  12520. '};',
  12521. 'this.Run = function (d, c, v) {',
  12522. ' if (d) ;',
  12523. ' if (c) ;',
  12524. ' if (v) ;',
  12525. '};',
  12526. 'this.Jv = undefined;',
  12527. 'this.Rec = this.TRecord.$new();',
  12528. '']),
  12529. LinesToStr([
  12530. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  12531. '$mod.Jv = $mod.Rec;',
  12532. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  12533. '$mod.Fly($mod.Rec, $mod.Rec);',
  12534. 'if ($mod.Rec) ;',
  12535. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  12536. '']));
  12537. end;
  12538. procedure TTestModule.TestRecord_VariantFail;
  12539. begin
  12540. StartProgram(false);
  12541. Add([
  12542. 'type',
  12543. ' TRec = record',
  12544. ' case word of',
  12545. ' 0: (b0, b1: Byte);',
  12546. ' 1: (i: word);',
  12547. ' end;',
  12548. 'begin']);
  12549. SetExpectedPasResolverError('Not supported: variant record',
  12550. nNotSupportedX);
  12551. ConvertProgram;
  12552. end;
  12553. procedure TTestModule.TestRecord_FieldArray;
  12554. begin
  12555. StartProgram(false);
  12556. Add([
  12557. 'type',
  12558. ' TArrInt = array[3..4] of longint;',
  12559. ' TArrArrInt = array[3..4] of longint;',
  12560. ' TRec = record',
  12561. ' a: array of longint;',
  12562. ' s: array[1..2] of longint;',
  12563. ' m: array[1..2,3..4] of longint;',
  12564. ' o: TArrArrInt;',
  12565. ' end;',
  12566. 'begin']);
  12567. ConvertProgram;
  12568. CheckSource('TestRecord_FieldArray',
  12569. LinesToStr([ // statements
  12570. 'rtl.recNewT(this, "TRec", function () {',
  12571. ' this.m$a$clone = function (a) {',
  12572. ' var b = [];',
  12573. ' b.length = 2;',
  12574. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12575. ' return b;',
  12576. ' };',
  12577. ' this.$new = function () {',
  12578. ' var r = Object.create(this);',
  12579. ' r.a = [];',
  12580. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12581. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12582. ' r.o = rtl.arraySetLength(null, 0, 2);',
  12583. ' return r;',
  12584. ' };',
  12585. ' this.$eq = function (b) {',
  12586. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  12587. ' };',
  12588. ' this.$assign = function (s) {',
  12589. ' this.a = rtl.arrayRef(s.a);',
  12590. ' this.s = s.s.slice(0);',
  12591. ' this.m = this.m$a$clone(s.m);',
  12592. ' this.o = s.o.slice(0);',
  12593. ' return this;',
  12594. ' };',
  12595. '});',
  12596. '']),
  12597. LinesToStr([ // $mod.$main
  12598. '']));
  12599. end;
  12600. procedure TTestModule.TestRecord_Const;
  12601. begin
  12602. StartProgram(false);
  12603. Add([
  12604. 'type',
  12605. ' TArrInt = array[3..4] of longint;',
  12606. ' TPoint = record x,y: longint; end;',
  12607. ' TRec = record',
  12608. ' i: longint;',
  12609. ' a: array of longint;',
  12610. ' s: array[1..2] of longint;',
  12611. ' m: array[1..2,3..4] of longint;',
  12612. ' p: TPoint;',
  12613. ' end;',
  12614. ' TPoints = array of TPoint;',
  12615. 'const',
  12616. ' r: TRec = (',
  12617. ' i:1;',
  12618. ' a:(2,3);',
  12619. ' s:(4,5);',
  12620. ' m:( (11,12), (13,14) );',
  12621. ' p: (x:21; y:22)',
  12622. ' );',
  12623. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12624. 'begin']);
  12625. ConvertProgram;
  12626. CheckSource('TestRecord_Const',
  12627. LinesToStr([ // statements
  12628. 'rtl.recNewT(this, "TPoint", function () {',
  12629. ' this.x = 0;',
  12630. ' this.y = 0;',
  12631. ' this.$eq = function (b) {',
  12632. ' return (this.x === b.x) && (this.y === b.y);',
  12633. ' };',
  12634. ' this.$assign = function (s) {',
  12635. ' this.x = s.x;',
  12636. ' this.y = s.y;',
  12637. ' return this;',
  12638. ' };',
  12639. '});',
  12640. 'rtl.recNewT(this, "TRec", function () {',
  12641. ' this.i = 0;',
  12642. ' this.m$a$clone = function (a) {',
  12643. ' var b = [];',
  12644. ' b.length = 2;',
  12645. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12646. ' return b;',
  12647. ' };',
  12648. ' this.$new = function () {',
  12649. ' var r = Object.create(this);',
  12650. ' r.a = [];',
  12651. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12652. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12653. ' r.p = $mod.TPoint.$new();',
  12654. ' return r;',
  12655. ' };',
  12656. ' this.$eq = function (b) {',
  12657. ' 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);',
  12658. ' };',
  12659. ' this.$assign = function (s) {',
  12660. ' this.i = s.i;',
  12661. ' this.a = rtl.arrayRef(s.a);',
  12662. ' this.s = s.s.slice(0);',
  12663. ' this.m = this.m$a$clone(s.m);',
  12664. ' this.p.$assign(s.p);',
  12665. ' return this;',
  12666. ' };',
  12667. '});',
  12668. 'this.r = this.TRec.$clone({',
  12669. ' i: 1,',
  12670. ' a: [2, 3],',
  12671. ' s: [4, 5],',
  12672. ' m: [[11, 12], [13, 14]],',
  12673. ' p: this.TPoint.$clone({',
  12674. ' x: 21,',
  12675. ' y: 22',
  12676. ' })',
  12677. '});',
  12678. 'this.p = [this.TPoint.$clone({',
  12679. ' x: 1,',
  12680. ' y: 2',
  12681. '}), this.TPoint.$clone({',
  12682. ' x: 3,',
  12683. ' y: 4',
  12684. '})];',
  12685. '']),
  12686. LinesToStr([ // $mod.$main
  12687. '']));
  12688. end;
  12689. procedure TTestModule.TestRecord_TypecastFail;
  12690. begin
  12691. StartProgram(false);
  12692. Add([
  12693. 'type',
  12694. ' TPoint = record x,y: longint; end;',
  12695. ' TRec = record l: longint end;',
  12696. 'var p: TPoint;',
  12697. 'begin',
  12698. ' if TRec(p).l=2 then ;']);
  12699. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  12700. nIllegalTypeConversionTo);
  12701. ConvertProgram;
  12702. end;
  12703. procedure TTestModule.TestRecord_InFunction;
  12704. begin
  12705. StartProgram(false);
  12706. Add([
  12707. 'var TPoint: longint = 3;',
  12708. 'procedure DoIt;',
  12709. 'type',
  12710. ' TPoint = record x,y: longint; end;',
  12711. ' TPoints = array of TPoint;',
  12712. 'var',
  12713. ' r: TPoint;',
  12714. ' p: TPoints;',
  12715. 'begin',
  12716. ' SetLength(p,2);',
  12717. 'end;',
  12718. 'begin']);
  12719. ConvertProgram;
  12720. CheckSource('TestRecord_InFunction',
  12721. LinesToStr([ // statements
  12722. 'this.TPoint = 3;',
  12723. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  12724. ' this.x = 0;',
  12725. ' this.y = 0;',
  12726. ' this.$eq = function (b) {',
  12727. ' return (this.x === b.x) && (this.y === b.y);',
  12728. ' };',
  12729. ' this.$assign = function (s) {',
  12730. ' this.x = s.x;',
  12731. ' this.y = s.y;',
  12732. ' return this;',
  12733. ' };',
  12734. '});',
  12735. 'this.DoIt = function () {',
  12736. ' var r = TPoint$1.$new();',
  12737. ' var p = [];',
  12738. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  12739. '};',
  12740. '']),
  12741. LinesToStr([ // $mod.$main
  12742. '']));
  12743. end;
  12744. procedure TTestModule.TestRecord_ArrayConstMultiline;
  12745. begin
  12746. StartProgram(false);
  12747. Add([
  12748. '{$mode delphi}',
  12749. 'type',
  12750. ' TBird = record Wing: string; end;',
  12751. 'const',
  12752. ' Birds: array[1..2] of TBird = (',
  12753. ' (Wing: ''''''',
  12754. ' First',
  12755. ' Second',
  12756. ' Third',
  12757. ' ''''''),',
  12758. ' (Wing: ''''''',
  12759. ' Value:=''Im in quotes''; ',
  12760. ' '''''')',
  12761. ' );',
  12762. 'begin']);
  12763. ConvertProgram;
  12764. CheckSource('TestRecord_ArrayConstMultiline',
  12765. LinesToStr([ // statements
  12766. 'rtl.recNewT(this, "TBird", function () {',
  12767. ' this.Wing = "";',
  12768. ' this.$eq = function (b) {',
  12769. ' return this.Wing === b.Wing;',
  12770. ' };',
  12771. ' this.$assign = function (s) {',
  12772. ' this.Wing = s.Wing;',
  12773. ' return this;',
  12774. ' };',
  12775. '});',
  12776. 'this.Birds$a$clone = function (a) {',
  12777. ' var b = [];',
  12778. ' b.length = 2;',
  12779. ' for (var c = 0; c < 2; c++) b[c] = $mod.TBird.$clone(a[c]);',
  12780. ' return b;',
  12781. '};',
  12782. 'this.Birds = [this.TBird.$clone({',
  12783. ' Wing: " First\n Second\n Third"',
  12784. '}), this.TBird.$clone({',
  12785. ' Wing: " Value:=''Im in quotes''; "',
  12786. '})];',
  12787. '']),
  12788. LinesToStr([ // $mod.$main
  12789. '']));
  12790. end;
  12791. procedure TTestModule.TestRecordAnonym_Field;
  12792. begin
  12793. StartProgram(false);
  12794. Add(['',
  12795. 'var Rec: record',
  12796. ' Bold: longint;',
  12797. ' end;',
  12798. 'begin',
  12799. ' rec.bold:=123;',
  12800. ' rec.bold:=rec.bold+7;',
  12801. '']);
  12802. ConvertProgram;
  12803. CheckSource('TestRecordAnonym_Field',
  12804. LinesToStr([ // statements
  12805. 'rtl.recNewT(this, "Rec$a", function () {',
  12806. ' this.Bold = 0;',
  12807. ' this.$eq = function (b) {',
  12808. ' return this.Bold === b.Bold;',
  12809. ' };',
  12810. ' this.$assign = function (s) {',
  12811. ' this.Bold = s.Bold;',
  12812. ' return this;',
  12813. ' };',
  12814. '});',
  12815. 'this.Rec = this.Rec$a.$new();',
  12816. '']),
  12817. LinesToStr([ // $mod.$main
  12818. '$mod.Rec.Bold = 123;',
  12819. '$mod.Rec.Bold = $mod.Rec.Bold + 7;',
  12820. '']));
  12821. end;
  12822. procedure TTestModule.TestRecordAnonym_Assign;
  12823. begin
  12824. StartProgram(false);
  12825. Add(['',
  12826. 'var S,T: record',
  12827. ' Bold: longint;',
  12828. ' end;',
  12829. ' b: boolean;',
  12830. 'begin',
  12831. ' S:=T;',
  12832. ' b:=s=t;',
  12833. '']);
  12834. ConvertProgram;
  12835. CheckSource('TestRecordAnonym_Assign',
  12836. LinesToStr([ // statements
  12837. 'rtl.recNewT(this, "T$a", function () {',
  12838. ' this.Bold = 0;',
  12839. ' this.$eq = function (b) {',
  12840. ' return this.Bold === b.Bold;',
  12841. ' };',
  12842. ' this.$assign = function (s) {',
  12843. ' this.Bold = s.Bold;',
  12844. ' return this;',
  12845. ' };',
  12846. '});',
  12847. 'this.S = this.T$a.$new();',
  12848. 'this.T = this.T$a.$new();',
  12849. 'this.b = false;',
  12850. '']),
  12851. LinesToStr([ // $mod.$main
  12852. '$mod.S.$assign($mod.T);',
  12853. '$mod.b = $mod.S.$eq($mod.T);',
  12854. '']));
  12855. end;
  12856. procedure TTestModule.TestRecordAnonym_Nested;
  12857. begin
  12858. StartProgram(false);
  12859. Add(['',
  12860. 'var S,T: record',
  12861. ' Bold: longint;',
  12862. ' Sub: record',
  12863. ' Color: word;',
  12864. ' end;',
  12865. ' end;',
  12866. ' b: boolean;',
  12867. 'begin',
  12868. ' S:=T;',
  12869. ' S.Sub:=T.Sub;',
  12870. ' S.Sub.Color:=T.Sub.Color+3;',
  12871. ' b:=s=t;',
  12872. ' b:=s.Sub=t.Sub;',
  12873. '']);
  12874. ConvertProgram;
  12875. CheckSource('TestRecordAnonym_Nested',
  12876. LinesToStr([ // statements
  12877. 'rtl.recNewT(this, "T$a", function () {',
  12878. ' this.Bold = 0;',
  12879. ' rtl.recNewT(this, "Sub$a", function () {',
  12880. ' this.Color = 0;',
  12881. ' this.$eq = function (b) {',
  12882. ' return this.Color === b.Color;',
  12883. ' };',
  12884. ' this.$assign = function (s) {',
  12885. ' this.Color = s.Color;',
  12886. ' return this;',
  12887. ' };',
  12888. ' });',
  12889. ' this.$new = function () {',
  12890. ' var r = Object.create(this);',
  12891. ' r.Sub = this.Sub$a.$new();',
  12892. ' return r;',
  12893. ' };',
  12894. ' this.$eq = function (b) {',
  12895. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12896. ' };',
  12897. ' this.$assign = function (s) {',
  12898. ' this.Bold = s.Bold;',
  12899. ' this.Sub.$assign(s.Sub);',
  12900. ' return this;',
  12901. ' };',
  12902. '}, true);',
  12903. 'this.S = this.T$a.$new();',
  12904. 'this.T = this.T$a.$new();',
  12905. 'this.b = false;',
  12906. '']),
  12907. LinesToStr([ // $mod.$main
  12908. '$mod.S.$assign($mod.T);',
  12909. '$mod.S.Sub.$assign($mod.T.Sub);',
  12910. '$mod.S.Sub.Color = $mod.T.Sub.Color + 3;',
  12911. '$mod.b = $mod.S.$eq($mod.T);',
  12912. '$mod.b = $mod.S.Sub.$eq($mod.T.Sub);',
  12913. '']));
  12914. end;
  12915. procedure TTestModule.TestRecordAnonym_Const;
  12916. begin
  12917. StartProgram(false);
  12918. Add(['',
  12919. 'var T: record',
  12920. ' Bold: longint;',
  12921. ' Sub: record',
  12922. ' Color: word;',
  12923. ' end;',
  12924. ' end = (Bold: 2; Sub: (Color: 3));',
  12925. 'begin',
  12926. '']);
  12927. ConvertProgram;
  12928. CheckSource('TestRecordAnonym_Const',
  12929. LinesToStr([ // statements
  12930. 'rtl.recNewT(this, "T$a", function () {',
  12931. ' this.Bold = 0;',
  12932. ' rtl.recNewT(this, "Sub$a", function () {',
  12933. ' this.Color = 0;',
  12934. ' this.$eq = function (b) {',
  12935. ' return this.Color === b.Color;',
  12936. ' };',
  12937. ' this.$assign = function (s) {',
  12938. ' this.Color = s.Color;',
  12939. ' return this;',
  12940. ' };',
  12941. ' });',
  12942. ' this.$new = function () {',
  12943. ' var r = Object.create(this);',
  12944. ' r.Sub = this.Sub$a.$new();',
  12945. ' return r;',
  12946. ' };',
  12947. ' this.$eq = function (b) {',
  12948. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  12949. ' };',
  12950. ' this.$assign = function (s) {',
  12951. ' this.Bold = s.Bold;',
  12952. ' this.Sub.$assign(s.Sub);',
  12953. ' return this;',
  12954. ' };',
  12955. '}, true);',
  12956. 'this.T = this.T$a.$clone({',
  12957. ' Bold: 2,',
  12958. ' Sub: this.T$a.Sub$a.$clone({',
  12959. ' Color: 3',
  12960. ' })',
  12961. '});',
  12962. '']),
  12963. LinesToStr([ // $mod.$main
  12964. '']));
  12965. end;
  12966. procedure TTestModule.TestRecordAnonym_InFunction;
  12967. begin
  12968. StartProgram(false);
  12969. Add(['',
  12970. 'procedure Fly;',
  12971. 'var T: record',
  12972. ' Bold: longint;',
  12973. ' Sub: record',
  12974. ' Color: word;',
  12975. ' end;',
  12976. ' end = (Bold: 2; Sub: (Color: 3));',
  12977. 'begin',
  12978. 'end;',
  12979. 'begin',
  12980. '']);
  12981. ConvertProgram;
  12982. CheckSource('TestRecordAnonym_InFunction',
  12983. LinesToStr([ // statements
  12984. 'var T$a = rtl.recNewT(null, "", function () {',
  12985. ' this.Bold = 0;',
  12986. ' rtl.recNewT(this, "Sub$a", function () {',
  12987. ' this.Color = 0;',
  12988. ' this.$eq = function (b) {',
  12989. ' return this.Color === b.Color;',
  12990. ' };',
  12991. ' this.$assign = function (s) {',
  12992. ' this.Color = s.Color;',
  12993. ' return this;',
  12994. ' };',
  12995. ' });',
  12996. ' this.$new = function () {',
  12997. ' var r = Object.create(this);',
  12998. ' r.Sub = this.Sub$a.$new();',
  12999. ' return r;',
  13000. ' };',
  13001. ' this.$eq = function (b) {',
  13002. ' return (this.Bold === b.Bold) && this.Sub.$eq(b.Sub);',
  13003. ' };',
  13004. ' this.$assign = function (s) {',
  13005. ' this.Bold = s.Bold;',
  13006. ' this.Sub.$assign(s.Sub);',
  13007. ' return this;',
  13008. ' };',
  13009. '}, true);',
  13010. 'this.Fly = function () {',
  13011. ' var T = T$a.$clone({',
  13012. ' Bold: 2,',
  13013. ' Sub: T$a.Sub$a.$clone({',
  13014. ' Color: 3',
  13015. ' })',
  13016. ' });',
  13017. '};',
  13018. '']),
  13019. LinesToStr([ // $mod.$main
  13020. '']));
  13021. end;
  13022. procedure TTestModule.TestAdvRecord_Function;
  13023. begin
  13024. StartProgram(false);
  13025. Parser.Options:=Parser.Options+[po_cassignments];
  13026. Add([
  13027. '{$modeswitch AdvancedRecords}',
  13028. 'type',
  13029. ' TPoint = record',
  13030. ' x,y: word;',
  13031. ' function Add(const apt: TPoint): TPoint;',
  13032. ' end;',
  13033. 'function TPoint.Add(const apt: TPoint): TPoint;',
  13034. 'begin',
  13035. ' Result:=Self;',
  13036. ' Result.x+=apt.x;',
  13037. ' Result.y:=Result.y+apt.y;',
  13038. ' Self:=apt;',
  13039. 'end;',
  13040. 'var p,q: TPoint;',
  13041. 'begin',
  13042. ' p.add(q);',
  13043. ' p:=default(TPoint);',
  13044. ' p:=q;',
  13045. '']);
  13046. ConvertProgram;
  13047. CheckSource('TestAdvRecord_Function',
  13048. LinesToStr([ // statements
  13049. 'rtl.recNewT(this, "TPoint", function () {',
  13050. ' this.x = 0;',
  13051. ' this.y = 0;',
  13052. ' this.$eq = function (b) {',
  13053. ' return (this.x === b.x) && (this.y === b.y);',
  13054. ' };',
  13055. ' this.$assign = function (s) {',
  13056. ' this.x = s.x;',
  13057. ' this.y = s.y;',
  13058. ' return this;',
  13059. ' };',
  13060. ' this.Add = function (apt) {',
  13061. ' var Result = $mod.TPoint.$new();',
  13062. ' Result.$assign(this);',
  13063. ' Result.x += apt.x;',
  13064. ' Result.y = Result.y + apt.y;',
  13065. ' this.$assign(apt);',
  13066. ' return Result;',
  13067. ' };',
  13068. '});',
  13069. 'this.p = this.TPoint.$new();',
  13070. 'this.q = this.TPoint.$new();',
  13071. '']),
  13072. LinesToStr([ // $mod.$main
  13073. '$mod.p.Add($mod.q);',
  13074. '$mod.p.$assign($mod.TPoint.$new());',
  13075. '$mod.p.$assign($mod.q);',
  13076. '']));
  13077. end;
  13078. procedure TTestModule.TestAdvRecord_Property;
  13079. begin
  13080. StartProgram(false);
  13081. Add([
  13082. '{$modeswitch AdvancedRecords}',
  13083. 'type',
  13084. ' TPoint = record',
  13085. ' x,y: word;',
  13086. ' strict private',
  13087. ' function GetSize: longword;',
  13088. ' procedure SetSize(Value: longword);',
  13089. ' public',
  13090. ' property Size: longword read GetSize write SetSize;',
  13091. ' property Left: word read x write y;',
  13092. ' end;',
  13093. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  13094. 'function TPoint.GetSize: longword;',
  13095. 'begin',
  13096. ' x:=y;',
  13097. ' Size:=Size;',
  13098. ' Left:=Left;',
  13099. 'end;',
  13100. 'procedure TPoint.SetSize(Value: longword);',
  13101. 'begin',
  13102. 'end;',
  13103. 'var p,q: TPoint;',
  13104. 'begin',
  13105. ' p.Size:=q.Size;',
  13106. ' p.Left:=q.Left;',
  13107. '']);
  13108. ConvertProgram;
  13109. CheckSource('TestAdvRecord_Property',
  13110. LinesToStr([ // statements
  13111. 'rtl.recNewT(this, "TPoint", function () {',
  13112. ' this.x = 0;',
  13113. ' this.y = 0;',
  13114. ' this.$eq = function (b) {',
  13115. ' return (this.x === b.x) && (this.y === b.y);',
  13116. ' };',
  13117. ' this.$assign = function (s) {',
  13118. ' this.x = s.x;',
  13119. ' this.y = s.y;',
  13120. ' return this;',
  13121. ' };',
  13122. ' this.GetSize = function () {',
  13123. ' var Result = 0;',
  13124. ' this.x = this.y;',
  13125. ' this.SetSize(this.GetSize());',
  13126. ' this.y = this.x;',
  13127. ' return Result;',
  13128. ' };',
  13129. ' this.SetSize = function (Value) {',
  13130. ' };',
  13131. '});',
  13132. 'this.SetSize = function (Value) {',
  13133. '};',
  13134. 'this.p = this.TPoint.$new();',
  13135. 'this.q = this.TPoint.$new();',
  13136. '']),
  13137. LinesToStr([ // $mod.$main
  13138. '$mod.p.SetSize($mod.q.GetSize());',
  13139. '$mod.p.y = $mod.q.x;',
  13140. '']));
  13141. end;
  13142. procedure TTestModule.TestAdvRecord_PropertyDefault;
  13143. begin
  13144. StartProgram(false);
  13145. Add([
  13146. '{$modeswitch AdvancedRecords}',
  13147. 'type',
  13148. ' TPoint = record',
  13149. ' strict private',
  13150. ' function GetItems(Index: word): word;',
  13151. ' procedure SetItems(Index: word; Value: word);',
  13152. ' public',
  13153. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  13154. ' end;',
  13155. 'function TPoint.GetItems(Index: word): word;',
  13156. 'begin',
  13157. ' Items[index]:=Items[index];',
  13158. ' self.Items[index]:=self.Items[index];',
  13159. 'end;',
  13160. 'procedure TPoint.SetItems(Index: word; Value: word);',
  13161. 'begin',
  13162. 'end;',
  13163. 'var p: TPoint;',
  13164. 'begin',
  13165. ' p[1]:=p[2];',
  13166. ' p.Items[3]:=p.Items[4];',
  13167. '']);
  13168. ConvertProgram;
  13169. CheckSource('TestAdvRecord_PropertyDefault',
  13170. LinesToStr([ // statements
  13171. 'rtl.recNewT(this, "TPoint", function () {',
  13172. ' this.$eq = function (b) {',
  13173. ' return true;',
  13174. ' };',
  13175. ' this.$assign = function (s) {',
  13176. ' return this;',
  13177. ' };',
  13178. ' this.GetItems = function (Index) {',
  13179. ' var Result = 0;',
  13180. ' this.SetItems(Index, this.GetItems(Index));',
  13181. ' this.SetItems(Index, this.GetItems(Index));',
  13182. ' return Result;',
  13183. ' };',
  13184. ' this.SetItems = function (Index, Value) {',
  13185. ' };',
  13186. '});',
  13187. 'this.p = this.TPoint.$new();',
  13188. '']),
  13189. LinesToStr([ // $mod.$main
  13190. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  13191. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  13192. '']));
  13193. end;
  13194. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  13195. begin
  13196. StartProgram(false);
  13197. Add([
  13198. '{$modeswitch AdvancedRecords}',
  13199. 'type',
  13200. ' TRec = record',
  13201. ' class var',
  13202. ' Fx: longint;',
  13203. ' Fy: longint;',
  13204. ' class function GetInt: longint; static;',
  13205. ' class procedure SetInt(Value: longint); static;',
  13206. ' class procedure DoIt; static;',
  13207. ' class property IntA: longint read Fx write Fy;',
  13208. ' class property IntB: longint read GetInt write SetInt;',
  13209. ' end;',
  13210. 'class function trec.getint: longint;',
  13211. 'begin',
  13212. ' result:=fx;',
  13213. 'end;',
  13214. 'class procedure trec.setint(value: longint);',
  13215. 'begin',
  13216. 'end;',
  13217. 'class procedure trec.doit;',
  13218. 'begin',
  13219. ' IntA:=IntA+1;',
  13220. ' IntB:=IntB+1;',
  13221. 'end;',
  13222. 'var r: trec;',
  13223. 'begin',
  13224. ' trec.inta:=trec.inta+1;',
  13225. ' if trec.intb=2 then;',
  13226. ' trec.intb:=trec.intb+2;',
  13227. ' trec.setint(trec.inta);',
  13228. ' r.inta:=r.inta+1;',
  13229. ' if r.intb=2 then;',
  13230. ' r.intb:=r.intb+2;',
  13231. ' r.setint(r.inta);']);
  13232. ConvertProgram;
  13233. CheckSource('TestAdvRecord_Property_ClassMethod',
  13234. LinesToStr([ // statements
  13235. 'rtl.recNewT(this, "TRec", function () {',
  13236. ' this.Fx = 0;',
  13237. ' this.Fy = 0;',
  13238. ' this.$eq = function (b) {',
  13239. ' return true;',
  13240. ' };',
  13241. ' this.$assign = function (s) {',
  13242. ' return this;',
  13243. ' };',
  13244. ' this.GetInt = function () {',
  13245. ' var Result = 0;',
  13246. ' Result = $mod.TRec.Fx;',
  13247. ' return Result;',
  13248. ' };',
  13249. ' this.SetInt = function (Value) {',
  13250. ' };',
  13251. ' this.DoIt = function () {',
  13252. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13253. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  13254. ' };',
  13255. '}, true);',
  13256. 'this.r = this.TRec.$new();',
  13257. '']),
  13258. LinesToStr([ // $mod.$main
  13259. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  13260. 'if ($mod.TRec.GetInt() === 2) ;',
  13261. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13262. '$mod.TRec.SetInt($mod.TRec.Fx);',
  13263. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  13264. 'if ($mod.TRec.GetInt() === 2) ;',
  13265. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  13266. '$mod.TRec.SetInt($mod.r.Fx);',
  13267. '']));
  13268. end;
  13269. procedure TTestModule.TestAdvRecord_Const;
  13270. begin
  13271. StartProgram(false);
  13272. Add([
  13273. '{$modeswitch AdvancedRecords}',
  13274. 'type',
  13275. ' TArrInt = array[3..4] of longint;',
  13276. ' TPoint = record',
  13277. ' x,y: longint;',
  13278. ' class var Count: nativeint;',
  13279. ' end;',
  13280. ' TRec = record',
  13281. ' i: longint;',
  13282. ' a: array of longint;',
  13283. ' s: array[1..2] of longint;',
  13284. ' m: array[1..2,3..4] of longint;',
  13285. ' p: TPoint;',
  13286. ' end;',
  13287. ' TPoints = array of TPoint;',
  13288. 'const',
  13289. ' r: TRec = (',
  13290. ' i:1;',
  13291. ' a:(2,3);',
  13292. ' s:(4,5);',
  13293. ' m:( (11,12), (13,14) );',
  13294. ' p: (x:21)',
  13295. ' );',
  13296. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  13297. 'begin']);
  13298. ConvertProgram;
  13299. CheckSource('TestAdvRecord_Const',
  13300. LinesToStr([ // statements
  13301. 'rtl.recNewT(this, "TPoint", function () {',
  13302. ' this.x = 0;',
  13303. ' this.y = 0;',
  13304. ' this.Count = 0;',
  13305. ' this.$eq = function (b) {',
  13306. ' return (this.x === b.x) && (this.y === b.y);',
  13307. ' };',
  13308. ' this.$assign = function (s) {',
  13309. ' this.x = s.x;',
  13310. ' this.y = s.y;',
  13311. ' return this;',
  13312. ' };',
  13313. '}, true);',
  13314. 'rtl.recNewT(this, "TRec", function () {',
  13315. ' this.i = 0;',
  13316. ' this.m$a$clone = function (a) {',
  13317. ' var b = [];',
  13318. ' b.length = 2;',
  13319. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  13320. ' return b;',
  13321. ' };',
  13322. ' this.$new = function () {',
  13323. ' var r = Object.create(this);',
  13324. ' r.a = [];',
  13325. ' r.s = rtl.arraySetLength(null, 0, 2);',
  13326. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  13327. ' r.p = $mod.TPoint.$new();',
  13328. ' return r;',
  13329. ' };',
  13330. ' this.$eq = function (b) {',
  13331. ' 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);',
  13332. ' };',
  13333. ' this.$assign = function (s) {',
  13334. ' this.i = s.i;',
  13335. ' this.a = rtl.arrayRef(s.a);',
  13336. ' this.s = s.s.slice(0);',
  13337. ' this.m = this.m$a$clone(s.m);',
  13338. ' this.p.$assign(s.p);',
  13339. ' return this;',
  13340. ' };',
  13341. '});',
  13342. 'this.r = this.TRec.$clone({',
  13343. ' i: 1,',
  13344. ' a: [2, 3],',
  13345. ' s: [4, 5],',
  13346. ' m: [[11, 12], [13, 14]],',
  13347. ' p: this.TPoint.$clone({',
  13348. ' x: 21,',
  13349. ' y: 0',
  13350. ' })',
  13351. '});',
  13352. 'this.p = [this.TPoint.$clone({',
  13353. ' x: 1,',
  13354. ' y: 2',
  13355. '}), this.TPoint.$clone({',
  13356. ' x: 3,',
  13357. ' y: 4',
  13358. '})];',
  13359. '']),
  13360. LinesToStr([ // $mod.$main
  13361. '']));
  13362. end;
  13363. procedure TTestModule.TestAdvRecord_ExternalField;
  13364. begin
  13365. StartProgram(false);
  13366. Add([
  13367. '{$modeswitch AdvancedRecords}',
  13368. '{$modeswitch externalclass}',
  13369. 'type',
  13370. ' TCar = record',
  13371. ' public',
  13372. ' Intern: longint external name ''$Intern'';',
  13373. ' Intern2: longint external name ''$Intern2'';',
  13374. ' Bracket: longint external name ''["A B"]'';',
  13375. ' procedure DoIt;',
  13376. ' end;',
  13377. 'procedure tcar.doit;',
  13378. 'begin',
  13379. ' Intern:=Intern+1;',
  13380. ' Intern2:=Intern2+2;',
  13381. ' Bracket:=Bracket+3;',
  13382. 'end;',
  13383. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  13384. 'begin',
  13385. ' Rec.intern:=Rec.intern+1;',
  13386. ' Rec.intern2:=Rec.intern2+2;',
  13387. ' Rec.Bracket:=Rec.Bracket+3;',
  13388. ' with Rec do begin',
  13389. ' intern:=intern+1;',
  13390. ' intern2:=intern2+2;',
  13391. ' Bracket:=Bracket+3;',
  13392. ' end;']);
  13393. ConvertProgram;
  13394. CheckSource('TestAdvRecord_ExternalField',
  13395. LinesToStr([ // statements
  13396. 'rtl.recNewT(this, "TCar", function () {',
  13397. ' this.$eq = function (b) {',
  13398. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  13399. ' };',
  13400. ' this.$assign = function (s) {',
  13401. ' this.$Intern = s.$Intern;',
  13402. ' this.$Intern2 = s.$Intern2;',
  13403. ' this["A B"] = s["A B"];',
  13404. ' return this;',
  13405. ' };',
  13406. ' this.DoIt = function () {',
  13407. ' this.$Intern = this.$Intern + 1;',
  13408. ' this.$Intern2 = this.$Intern2 + 2;',
  13409. ' this["A B"] = this["A B"] + 3;',
  13410. ' };',
  13411. '});',
  13412. 'this.Rec = this.TCar.$clone({',
  13413. ' $Intern: 11,',
  13414. ' $Intern2: 12,',
  13415. ' "A B": 13',
  13416. '});',
  13417. '']),
  13418. LinesToStr([ // $mod.$main
  13419. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  13420. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  13421. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  13422. 'var $with = $mod.Rec;',
  13423. '$with.$Intern = $with.$Intern + 1;',
  13424. '$with.$Intern2 = $with.$Intern2 + 2;',
  13425. '$with["A B"] = $with["A B"] + 3;',
  13426. '']));
  13427. end;
  13428. procedure TTestModule.TestAdvRecord_SubRecord;
  13429. begin
  13430. StartProgram(false);
  13431. Add([
  13432. '{$modeswitch AdvancedRecords}',
  13433. 'type',
  13434. ' TRec = record',
  13435. ' type',
  13436. ' TPoint = record',
  13437. ' x,y: longint;',
  13438. ' class var Count: nativeint;',
  13439. ' procedure DoIt;',
  13440. ' class procedure DoThat; static;',
  13441. ' end;',
  13442. ' var',
  13443. ' i: longint;',
  13444. ' p: TPoint;',
  13445. ' procedure DoSome;',
  13446. ' end;',
  13447. 'const',
  13448. ' r: TRec = (',
  13449. ' i:1;',
  13450. ' p: (x:21;y:22)',
  13451. ' );',
  13452. 'procedure TRec.DoSome;',
  13453. 'begin',
  13454. ' p.x:=p.y+1;',
  13455. ' p.Count:=p.Count+2;',
  13456. 'end;',
  13457. 'procedure TRec.TPoint.DoIt;',
  13458. 'begin',
  13459. ' Count:=Count+3;',
  13460. 'end;',
  13461. 'class procedure TRec.TPoint.DoThat;',
  13462. 'begin',
  13463. ' Count:=Count+4;',
  13464. 'end;',
  13465. 'begin']);
  13466. ConvertProgram;
  13467. CheckSource('TestAdvRecord_SubRecord',
  13468. LinesToStr([ // statements
  13469. 'rtl.recNewT(this, "TRec", function () {',
  13470. ' rtl.recNewT(this, "TPoint", function () {',
  13471. ' this.x = 0;',
  13472. ' this.y = 0;',
  13473. ' this.Count = 0;',
  13474. ' this.$eq = function (b) {',
  13475. ' return (this.x === b.x) && (this.y === b.y);',
  13476. ' };',
  13477. ' this.$assign = function (s) {',
  13478. ' this.x = s.x;',
  13479. ' this.y = s.y;',
  13480. ' return this;',
  13481. ' };',
  13482. ' this.DoIt = function () {',
  13483. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  13484. ' };',
  13485. ' this.DoThat = function () {',
  13486. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  13487. ' };',
  13488. ' }, true);',
  13489. ' this.i = 0;',
  13490. ' this.$new = function () {',
  13491. ' var r = Object.create(this);',
  13492. ' r.p = this.TPoint.$new();',
  13493. ' return r;',
  13494. ' };',
  13495. ' this.$eq = function (b) {',
  13496. ' return (this.i === b.i) && this.p.$eq(b.p);',
  13497. ' };',
  13498. ' this.$assign = function (s) {',
  13499. ' this.i = s.i;',
  13500. ' this.p.$assign(s.p);',
  13501. ' return this;',
  13502. ' };',
  13503. ' this.DoSome = function () {',
  13504. ' this.p.x = this.p.y + 1;',
  13505. ' this.TPoint.Count = this.p.Count + 2;',
  13506. ' };',
  13507. '}, true);',
  13508. 'this.r = this.TRec.$clone({',
  13509. ' i: 1,',
  13510. ' p: this.TRec.TPoint.$clone({',
  13511. ' x: 21,',
  13512. ' y: 22',
  13513. ' })',
  13514. '});',
  13515. '']),
  13516. LinesToStr([ // $mod.$main
  13517. '']));
  13518. end;
  13519. procedure TTestModule.TestAdvRecord_SubClass;
  13520. begin
  13521. StartProgram(false);
  13522. Add([
  13523. '{$modeswitch AdvancedRecords}',
  13524. 'type',
  13525. ' TObject = class end;',
  13526. ' TPoint = record',
  13527. ' type',
  13528. ' TBird = class',
  13529. ' procedure DoIt;',
  13530. ' class procedure Glob;',
  13531. ' end;',
  13532. ' procedure DoIt(b: TBird);',
  13533. ' end;',
  13534. 'procedure TPoint.TBird.DoIt;',
  13535. 'begin',
  13536. ' doit;',
  13537. ' self.doit;',
  13538. ' glob;',
  13539. ' self.glob;',
  13540. 'end;',
  13541. 'class procedure TPoint.TBird.Glob;',
  13542. 'begin',
  13543. ' glob;',
  13544. ' self.glob;',
  13545. 'end;',
  13546. 'procedure TPoint.DoIt(b: TBird);',
  13547. 'begin',
  13548. ' b.doit;',
  13549. ' b.glob;',
  13550. ' TBird.glob;',
  13551. 'end;',
  13552. 'begin',
  13553. '']);
  13554. ConvertProgram;
  13555. CheckSource('TestAdvRecord_SubClass',
  13556. LinesToStr([ // statements
  13557. 'rtl.createClass(this, "TObject", null, function () {',
  13558. ' this.$init = function () {',
  13559. ' };',
  13560. ' this.$final = function () {',
  13561. ' };',
  13562. '});',
  13563. 'rtl.recNewT(this, "TPoint", function () {',
  13564. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  13565. ' this.DoIt = function () {',
  13566. ' this.DoIt();',
  13567. ' this.DoIt();',
  13568. ' this.$class.Glob();',
  13569. ' this.$class.Glob();',
  13570. ' };',
  13571. ' this.Glob = function () {',
  13572. ' this.Glob();',
  13573. ' this.Glob();',
  13574. ' };',
  13575. ' }, "TPoint.TBird");',
  13576. ' this.$eq = function (b) {',
  13577. ' return true;',
  13578. ' };',
  13579. ' this.$assign = function (s) {',
  13580. ' return this;',
  13581. ' };',
  13582. ' this.DoIt = function (b) {',
  13583. ' b.DoIt();',
  13584. ' b.$class.Glob();',
  13585. ' this.TBird.Glob();',
  13586. ' };',
  13587. '}, true);',
  13588. '']),
  13589. LinesToStr([ // $mod.$main
  13590. '']));
  13591. end;
  13592. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  13593. begin
  13594. StartProgram(false);
  13595. Add([
  13596. '{$modeswitch AdvancedRecords}',
  13597. 'type',
  13598. ' IUnknown = interface end;',
  13599. ' TPoint = record',
  13600. ' type IBird = interface end;',
  13601. ' end;',
  13602. 'begin',
  13603. '']);
  13604. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  13605. nNotYetImplemented);
  13606. ParseProgram;
  13607. end;
  13608. procedure TTestModule.TestAdvRecord_Constructor;
  13609. begin
  13610. StartProgram(false);
  13611. Add([
  13612. '{$modeswitch AdvancedRecords}',
  13613. 'type',
  13614. ' TPoint = record',
  13615. ' x,y: longint;',
  13616. ' class procedure Run(w: longint = 13); static;',
  13617. ' constructor Create(ax: longint; ay: longint = -1);',
  13618. ' end;',
  13619. 'class procedure tpoint.run(w: longint);',
  13620. 'begin',
  13621. ' run;',
  13622. ' run();',
  13623. 'end;',
  13624. 'constructor tpoint.create(ax,ay: longint);',
  13625. 'begin',
  13626. ' x:=ax;',
  13627. ' self.y:=ay;',
  13628. ' run;',
  13629. ' run(ax);',
  13630. 'end;',
  13631. 'var r: TPoint;',
  13632. 'begin',
  13633. ' r:=TPoint.Create(1,2);',
  13634. ' with TPoint do r:=Create(1,2);',
  13635. ' r.Create(3);',
  13636. ' r:=r.Create(4);',
  13637. '']);
  13638. ConvertProgram;
  13639. CheckSource('TestAdvRecord_Constructor',
  13640. LinesToStr([ // statements
  13641. 'rtl.recNewT(this, "TPoint", function () {',
  13642. ' this.x = 0;',
  13643. ' this.y = 0;',
  13644. ' this.$eq = function (b) {',
  13645. ' return (this.x === b.x) && (this.y === b.y);',
  13646. ' };',
  13647. ' this.$assign = function (s) {',
  13648. ' this.x = s.x;',
  13649. ' this.y = s.y;',
  13650. ' return this;',
  13651. ' };',
  13652. ' this.Run = function (w) {',
  13653. ' $mod.TPoint.Run(13);',
  13654. ' $mod.TPoint.Run(13);',
  13655. ' };',
  13656. ' this.Create = function (ax, ay) {',
  13657. ' this.x = ax;',
  13658. ' this.y = ay;',
  13659. ' this.Run(13);',
  13660. ' this.Run(ax);',
  13661. ' return this;',
  13662. ' };',
  13663. '});',
  13664. 'this.r = this.TPoint.$new();',
  13665. '']),
  13666. LinesToStr([ // $mod.$main
  13667. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  13668. 'var $with = $mod.TPoint;',
  13669. '$mod.r.$assign($with.$new().Create(1, 2));',
  13670. '$mod.r.Create(3, -1);',
  13671. '$mod.r.$assign($mod.r.Create(4, -1));',
  13672. '']));
  13673. end;
  13674. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  13675. begin
  13676. StartProgram(false);
  13677. Add([
  13678. '{$modeswitch AdvancedRecords}',
  13679. 'type',
  13680. ' TPoint = record',
  13681. ' class var x: longint;',
  13682. ' class procedure Fly; static;',
  13683. ' class constructor Init;',
  13684. ' end;',
  13685. 'var count: word;',
  13686. 'class procedure Tpoint.Fly;',
  13687. 'begin',
  13688. 'end;',
  13689. 'class constructor tpoint.init;',
  13690. 'begin',
  13691. ' count:=count+1;',
  13692. ' x:=x+3;',
  13693. ' tpoint.x:=tpoint.x+4;',
  13694. ' fly;',
  13695. ' tpoint.fly;',
  13696. 'end;',
  13697. 'var r: TPoint;',
  13698. 'begin',
  13699. ' r.x:=r.x+10;',
  13700. ' r.Fly;',
  13701. ' r.Fly();',
  13702. '']);
  13703. ConvertProgram;
  13704. CheckSource('TestAdvRecord_ClassConstructor_Program',
  13705. LinesToStr([ // statements
  13706. 'rtl.recNewT(this, "TPoint", function () {',
  13707. ' this.x = 0;',
  13708. ' this.$eq = function (b) {',
  13709. ' return true;',
  13710. ' };',
  13711. ' this.$assign = function (s) {',
  13712. ' return this;',
  13713. ' };',
  13714. ' this.Fly = function () {',
  13715. ' };',
  13716. '}, true);',
  13717. 'this.count = 0;',
  13718. 'this.r = this.TPoint.$new();',
  13719. '']),
  13720. LinesToStr([ // $mod.$main
  13721. '(function () {',
  13722. ' $mod.count = $mod.count + 1;',
  13723. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  13724. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  13725. ' $mod.TPoint.Fly();',
  13726. ' $mod.TPoint.Fly();',
  13727. '})();',
  13728. '$mod.TPoint.x = $mod.r.x + 10;',
  13729. '$mod.TPoint.Fly();',
  13730. '$mod.TPoint.Fly();',
  13731. '']));
  13732. end;
  13733. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  13734. begin
  13735. StartUnit(false);
  13736. Add([
  13737. 'interface',
  13738. '{$modeswitch AdvancedRecords}',
  13739. 'type',
  13740. ' TPoint = record',
  13741. ' class var x: longint;',
  13742. ' class procedure Fly; static;',
  13743. ' class constructor Init;',
  13744. ' end;',
  13745. 'implementation',
  13746. 'var count: word;',
  13747. 'class procedure Tpoint.Fly;',
  13748. 'begin',
  13749. 'end;',
  13750. 'class constructor tpoint.init;',
  13751. 'begin',
  13752. ' count:=count+1;',
  13753. ' x:=3;',
  13754. ' tpoint.x:=4;',
  13755. ' fly;',
  13756. ' tpoint.fly;',
  13757. 'end;',
  13758. '']);
  13759. ConvertUnit;
  13760. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  13761. LinesToStr([ // statements
  13762. 'var $impl = $mod.$impl;',
  13763. 'rtl.recNewT(this, "TPoint", function () {',
  13764. ' this.x = 0;',
  13765. ' this.$eq = function (b) {',
  13766. ' return true;',
  13767. ' };',
  13768. ' this.$assign = function (s) {',
  13769. ' return this;',
  13770. ' };',
  13771. ' this.Fly = function () {',
  13772. ' };',
  13773. '}, true);',
  13774. '']),
  13775. LinesToStr([ // $mod.$init
  13776. '(function () {',
  13777. ' $impl.count = $impl.count + 1;',
  13778. ' $mod.TPoint.x = 3;',
  13779. ' $mod.TPoint.x = 4;',
  13780. ' $mod.TPoint.Fly();',
  13781. ' $mod.TPoint.Fly();',
  13782. '})();',
  13783. '']),
  13784. LinesToStr([ // $mod.$main
  13785. '$impl.count = 0;',
  13786. '']));
  13787. end;
  13788. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  13789. begin
  13790. StartProgram(false);
  13791. Add(['type',
  13792. ' TObject = class',
  13793. ' public',
  13794. ' constructor Create;',
  13795. ' destructor Destroy;',
  13796. ' end;',
  13797. ' TBird = TObject;',
  13798. 'constructor tobject.create;',
  13799. 'begin end;',
  13800. 'destructor tobject.destroy;',
  13801. 'begin end;',
  13802. 'var Obj: tobject;',
  13803. 'begin',
  13804. ' obj:=tobject.create;',
  13805. ' obj:=tobject.create();',
  13806. ' obj:=tbird.create;',
  13807. ' obj:=tbird.create();',
  13808. ' obj:=obj.create();',
  13809. ' obj.destroy;',
  13810. '']);
  13811. ConvertProgram;
  13812. CheckSource('TestClass_TObjectDefaultConstructor',
  13813. LinesToStr([ // statements
  13814. 'rtl.createClass(this,"TObject",null,function(){',
  13815. ' this.$init = function () {',
  13816. ' };',
  13817. ' this.$final = function () {',
  13818. ' };',
  13819. ' this.Create = function(){',
  13820. ' return this;',
  13821. ' };',
  13822. ' this.Destroy = function(){',
  13823. ' };',
  13824. '});',
  13825. 'this.Obj = null;'
  13826. ]),
  13827. LinesToStr([ // $mod.$main
  13828. '$mod.Obj = $mod.TObject.$create("Create");',
  13829. '$mod.Obj = $mod.TObject.$create("Create");',
  13830. '$mod.Obj = $mod.TObject.$create("Create");',
  13831. '$mod.Obj = $mod.TObject.$create("Create");',
  13832. '$mod.Obj = $mod.Obj.Create();',
  13833. '$mod.Obj.$destroy("Destroy");',
  13834. '']));
  13835. end;
  13836. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  13837. begin
  13838. StartProgram(false);
  13839. Add('type');
  13840. Add(' TObject = class');
  13841. Add(' public');
  13842. Add(' constructor Create(Par: longint);');
  13843. Add(' end;');
  13844. Add('constructor tobject.create(par: longint);');
  13845. Add('begin end;');
  13846. Add('var Obj: tobject;');
  13847. Add('begin');
  13848. Add(' obj:=tobject.create(3);');
  13849. ConvertProgram;
  13850. CheckSource('TestClass_TObjectConstructorWithParams',
  13851. LinesToStr([ // statements
  13852. 'rtl.createClass(this,"TObject",null,function(){',
  13853. ' this.$init = function () {',
  13854. ' };',
  13855. ' this.$final = function () {',
  13856. ' };',
  13857. ' this.Create = function(Par){',
  13858. ' return this;',
  13859. ' };',
  13860. '});',
  13861. 'this.Obj = null;'
  13862. ]),
  13863. LinesToStr([ // $mod.$main
  13864. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  13865. ]));
  13866. end;
  13867. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  13868. begin
  13869. StartProgram(false);
  13870. Add('type');
  13871. Add(' TObject = class');
  13872. Add(' public');
  13873. Add(' constructor Create;');
  13874. Add(' end;');
  13875. Add(' TTest = class(TObject)');
  13876. Add(' public');
  13877. Add(' constructor Create(const Par: longint = 1);');
  13878. Add(' end;');
  13879. Add('constructor tobject.create;');
  13880. Add('begin end;');
  13881. Add('constructor ttest.create(const par: longint);');
  13882. Add('begin end;');
  13883. Add('var t: ttest;');
  13884. Add('begin');
  13885. Add(' t:=ttest.create;');
  13886. Add(' t:=ttest.create(2);');
  13887. ConvertProgram;
  13888. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  13889. LinesToStr([ // statements
  13890. 'rtl.createClass(this,"TObject",null,function(){',
  13891. ' this.$init = function () {',
  13892. ' };',
  13893. ' this.$final = function () {',
  13894. ' };',
  13895. ' this.Create = function(){',
  13896. ' return this;',
  13897. ' };',
  13898. '});',
  13899. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  13900. ' this.Create$1 = function (Par) {',
  13901. ' return this;',
  13902. ' };',
  13903. '});',
  13904. 'this.t = null;'
  13905. ]),
  13906. LinesToStr([ // $mod.$main
  13907. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  13908. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  13909. ]));
  13910. end;
  13911. procedure TTestModule.TestClass_Var;
  13912. begin
  13913. StartProgram(false);
  13914. Add([
  13915. 'type',
  13916. ' TObject = class',
  13917. ' public',
  13918. ' vI: longint;',
  13919. ' constructor Create(Par: longint);',
  13920. ' end;',
  13921. 'constructor tobject.create(par: longint);',
  13922. 'begin',
  13923. ' vi:=par+3',
  13924. 'end;',
  13925. 'var Obj: tobject;',
  13926. 'begin',
  13927. ' obj:=tobject.create(4);',
  13928. ' obj.vi:=obj.VI+5;']);
  13929. ConvertProgram;
  13930. CheckSource('TestClass_Var',
  13931. LinesToStr([ // statements
  13932. 'rtl.createClass(this,"TObject",null,function(){',
  13933. ' this.$init = function () {',
  13934. ' this.vI = 0;',
  13935. ' };',
  13936. ' this.$final = function () {',
  13937. ' };',
  13938. ' this.Create = function(Par){',
  13939. ' this.vI = Par+3;',
  13940. ' return this;',
  13941. ' };',
  13942. '});',
  13943. 'this.Obj = null;'
  13944. ]),
  13945. LinesToStr([ // $mod.$main
  13946. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  13947. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  13948. ]));
  13949. end;
  13950. procedure TTestModule.TestClass_Method;
  13951. begin
  13952. StartProgram(false);
  13953. Add('type');
  13954. Add(' TObject = class');
  13955. Add(' public');
  13956. Add(' vI: longint;');
  13957. Add(' Sub: TObject;');
  13958. Add(' constructor Create;');
  13959. Add(' function GetIt(Par: longint): tobject;');
  13960. Add(' end;');
  13961. Add('constructor tobject.create; begin end;');
  13962. Add('function tobject.getit(par: longint): tobject;');
  13963. Add('begin');
  13964. Add(' Self.vi:=par+3;');
  13965. Add(' Result:=self.sub;');
  13966. Add('end;');
  13967. Add('var Obj: tobject;');
  13968. Add('begin');
  13969. Add(' obj:=tobject.create;');
  13970. Add(' obj.getit(4);');
  13971. Add(' obj.sub.sub:=nil;');
  13972. Add(' obj.sub.getit(5);');
  13973. Add(' obj.sub.getit(6).SUB:=nil;');
  13974. Add(' obj.sub.getit(7).GETIT(8);');
  13975. Add(' obj.sub.getit(9).SuB.getit(10);');
  13976. ConvertProgram;
  13977. CheckSource('TestClass_Method',
  13978. LinesToStr([ // statements
  13979. 'rtl.createClass(this,"TObject",null,function(){',
  13980. ' this.$init = function () {',
  13981. ' this.vI = 0;',
  13982. ' this.Sub = null;',
  13983. ' };',
  13984. ' this.$final = function () {',
  13985. ' this.Sub = undefined;',
  13986. ' };',
  13987. ' this.Create = function(){',
  13988. ' return this;',
  13989. ' };',
  13990. ' this.GetIt = function(Par){',
  13991. ' var Result = null;',
  13992. ' this.vI = Par + 3;',
  13993. ' Result = this.Sub;',
  13994. ' return Result;',
  13995. ' };',
  13996. '});',
  13997. 'this.Obj = null;'
  13998. ]),
  13999. LinesToStr([ // $mod.$main
  14000. '$mod.Obj = $mod.TObject.$create("Create");',
  14001. '$mod.Obj.GetIt(4);',
  14002. '$mod.Obj.Sub.Sub=null;',
  14003. '$mod.Obj.Sub.GetIt(5);',
  14004. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  14005. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  14006. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  14007. ]));
  14008. end;
  14009. procedure TTestModule.TestClass_Implementation;
  14010. begin
  14011. StartUnit(false);
  14012. Add([
  14013. 'interface',
  14014. 'type',
  14015. ' TObject = class',
  14016. ' constructor Create;',
  14017. ' end;',
  14018. 'implementation',
  14019. 'type',
  14020. ' TIntClass = class',
  14021. ' constructor Create; reintroduce;',
  14022. ' class procedure DoGlob;',
  14023. ' end;',
  14024. 'constructor tintclass.create;',
  14025. 'begin',
  14026. ' inherited;',
  14027. ' inherited create;',
  14028. ' doglob;',
  14029. 'end;',
  14030. 'class procedure tintclass.doglob;',
  14031. 'begin',
  14032. 'end;',
  14033. 'constructor tobject.create;',
  14034. 'var',
  14035. ' iC: tintclass;',
  14036. 'begin',
  14037. ' ic:=tintclass.create;',
  14038. ' tintclass.doglob;',
  14039. ' ic.doglob;',
  14040. 'end;',
  14041. 'initialization',
  14042. ' tintclass.doglob;',
  14043. '']);
  14044. ConvertUnit;
  14045. CheckSource('TestClass_Implementation',
  14046. LinesToStr([ // statements
  14047. 'var $impl = $mod.$impl;',
  14048. 'rtl.createClass(this, "TObject", null, function () {',
  14049. ' this.$init = function () {',
  14050. ' };',
  14051. ' this.$final = function () {',
  14052. ' };',
  14053. ' this.Create = function () {',
  14054. ' var iC = null;',
  14055. ' iC = $impl.TIntClass.$create("Create$1");',
  14056. ' $impl.TIntClass.DoGlob();',
  14057. ' iC.$class.DoGlob();',
  14058. ' return this;',
  14059. ' };',
  14060. '});',
  14061. '']),
  14062. LinesToStr([ // $mod.$main
  14063. '$impl.TIntClass.DoGlob();',
  14064. '']),
  14065. LinesToStr([
  14066. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  14067. ' this.Create$1 = function () {',
  14068. ' $mod.TObject.Create.call(this);',
  14069. ' $mod.TObject.Create.call(this);',
  14070. ' this.$class.DoGlob();',
  14071. ' return this;',
  14072. ' };',
  14073. ' this.DoGlob = function () {',
  14074. ' };',
  14075. '});',
  14076. '']));
  14077. end;
  14078. procedure TTestModule.TestClass_Inheritance;
  14079. begin
  14080. StartProgram(false);
  14081. Add('type');
  14082. Add(' TObject = class');
  14083. Add(' public');
  14084. Add(' constructor Create;');
  14085. Add(' end;');
  14086. Add(' TClassA = class');
  14087. Add(' end;');
  14088. Add(' TClassB = class(TObject)');
  14089. Add(' procedure ProcB;');
  14090. Add(' end;');
  14091. Add('constructor tobject.create; begin end;');
  14092. Add('procedure tclassb.procb; begin end;');
  14093. Add('var');
  14094. Add(' oO: TObject;');
  14095. Add(' oA: TClassA;');
  14096. Add(' oB: TClassB;');
  14097. Add('begin');
  14098. Add(' oO:=tobject.Create;');
  14099. Add(' oA:=tclassa.Create;');
  14100. Add(' ob:=tclassb.Create;');
  14101. Add(' if oo is tclassa then ;');
  14102. Add(' ob:=oo as tclassb;');
  14103. Add(' (oo as tclassb).procb;');
  14104. ConvertProgram;
  14105. CheckSource('TestClass_Inheritance',
  14106. LinesToStr([ // statements
  14107. 'rtl.createClass(this,"TObject",null,function(){',
  14108. ' this.$init = function () {',
  14109. ' };',
  14110. ' this.$final = function () {',
  14111. ' };',
  14112. ' this.Create = function () {',
  14113. ' return this;',
  14114. ' };',
  14115. '});',
  14116. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  14117. '});',
  14118. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  14119. ' this.ProcB = function () {',
  14120. ' };',
  14121. '});',
  14122. 'this.oO = null;',
  14123. 'this.oA = null;',
  14124. 'this.oB = null;'
  14125. ]),
  14126. LinesToStr([ // $mod.$main
  14127. '$mod.oO = $mod.TObject.$create("Create");',
  14128. '$mod.oA = $mod.TClassA.$create("Create");',
  14129. '$mod.oB = $mod.TClassB.$create("Create");',
  14130. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  14131. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  14132. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  14133. ]));
  14134. end;
  14135. procedure TTestModule.TestClass_TypeAlias;
  14136. begin
  14137. StartProgram(false);
  14138. Add([
  14139. '{$interfaces corba}',
  14140. 'type',
  14141. ' IObject = interface',
  14142. ' end;',
  14143. ' IBird = type IObject;',
  14144. ' TObject = class',
  14145. ' end;',
  14146. ' TBird = type TObject;',
  14147. 'var',
  14148. ' oObj: TObject;',
  14149. ' oBird: TBird;',
  14150. ' IntfObj: IObject;',
  14151. ' IntfBird: IBird;',
  14152. 'begin',
  14153. ' oObj:=oBird;',
  14154. '']);
  14155. ConvertProgram;
  14156. CheckSource('TestClass_TypeAlias',
  14157. LinesToStr([ // statements
  14158. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  14159. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  14160. 'rtl.createClass(this, "TObject", null, function () {',
  14161. ' this.$init = function () {',
  14162. ' };',
  14163. ' this.$final = function () {',
  14164. ' };',
  14165. '});',
  14166. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14167. '});',
  14168. 'this.oObj = null;',
  14169. 'this.oBird = null;',
  14170. 'this.IntfObj = null;',
  14171. 'this.IntfBird = null;',
  14172. '']),
  14173. LinesToStr([ // $mod.$main
  14174. '$mod.oObj = $mod.oBird;',
  14175. '']));
  14176. end;
  14177. procedure TTestModule.TestClass_AbstractMethod;
  14178. begin
  14179. StartProgram(false);
  14180. Add('type');
  14181. Add(' TObject = class');
  14182. Add(' public');
  14183. Add(' procedure DoIt; virtual; abstract;');
  14184. Add(' end;');
  14185. Add('begin');
  14186. ConvertProgram;
  14187. CheckSource('TestClass_AbstractMethod',
  14188. LinesToStr([ // statements
  14189. 'rtl.createClass(this,"TObject",null,function(){',
  14190. ' this.$init = function () {',
  14191. ' };',
  14192. ' this.$final = function () {',
  14193. ' };',
  14194. '});'
  14195. ]),
  14196. LinesToStr([ // this.$main
  14197. ''
  14198. ]));
  14199. end;
  14200. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  14201. begin
  14202. StartProgram(false);
  14203. Add([
  14204. 'type',
  14205. ' TObject = class',
  14206. ' procedure DoAbstract; virtual; abstract;',
  14207. ' procedure DoVirtual; virtual;',
  14208. ' procedure DoIt;',
  14209. ' end;',
  14210. ' TA = class',
  14211. ' procedure doabstract; override;',
  14212. ' procedure dovirtual; override;',
  14213. ' procedure DoSome;',
  14214. ' end;',
  14215. 'procedure tobject.dovirtual;',
  14216. 'begin',
  14217. ' inherited; // call non existing ancestor -> ignore silently',
  14218. 'end;',
  14219. 'procedure tobject.doit;',
  14220. 'begin',
  14221. 'end;',
  14222. 'procedure ta.doabstract;',
  14223. 'begin',
  14224. ' inherited dovirtual; // call TObject.DoVirtual',
  14225. 'end;',
  14226. 'procedure ta.dovirtual;',
  14227. 'begin',
  14228. ' inherited; // call TObject.DoVirtual',
  14229. ' inherited dovirtual; // call TObject.DoVirtual',
  14230. ' inherited dovirtual(); // call TObject.DoVirtual',
  14231. ' doit;',
  14232. ' doit();',
  14233. 'end;',
  14234. 'procedure ta.dosome;',
  14235. 'begin',
  14236. ' inherited; // call non existing ancestor method -> silently ignore',
  14237. 'end;',
  14238. 'begin']);
  14239. ConvertProgram;
  14240. CheckSource('TestClass_CallInherited_ProcNoParams',
  14241. LinesToStr([ // statements
  14242. 'rtl.createClass(this,"TObject",null,function(){',
  14243. ' this.$init = function () {',
  14244. ' };',
  14245. ' this.$final = function () {',
  14246. ' };',
  14247. ' this.DoVirtual = function () {',
  14248. ' };',
  14249. ' this.DoIt = function () {',
  14250. ' };',
  14251. '});',
  14252. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14253. ' this.DoAbstract = function () {',
  14254. ' $mod.TObject.DoVirtual.call(this);',
  14255. ' };',
  14256. ' this.DoVirtual = function () {',
  14257. ' $mod.TObject.DoVirtual.call(this);',
  14258. ' $mod.TObject.DoVirtual.call(this);',
  14259. ' $mod.TObject.DoVirtual.call(this);',
  14260. ' this.DoIt();',
  14261. ' this.DoIt();',
  14262. ' };',
  14263. ' this.DoSome = function () {',
  14264. ' };',
  14265. '});'
  14266. ]),
  14267. LinesToStr([ // this.$main
  14268. ''
  14269. ]));
  14270. end;
  14271. procedure TTestModule.TestClass_CallInherited_WithParams;
  14272. begin
  14273. StartProgram(false);
  14274. Add([
  14275. 'type',
  14276. ' TObject = class',
  14277. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  14278. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  14279. ' procedure DoIt(pA: longint; pB: longint = 0);',
  14280. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  14281. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14282. ' end;',
  14283. ' TClassA = class',
  14284. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  14285. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  14286. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  14287. ' end;',
  14288. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  14289. 'begin',
  14290. 'end;',
  14291. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  14292. 'begin',
  14293. 'end;',
  14294. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  14295. 'begin',
  14296. 'end;',
  14297. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  14298. 'begin',
  14299. 'end;',
  14300. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  14301. 'begin',
  14302. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14303. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14304. 'end;',
  14305. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  14306. 'begin',
  14307. ' inherited; // call TObject.DoVirtual(pA,pB)',
  14308. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  14309. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  14310. ' doit(pa,pb);',
  14311. ' doit(pa);',
  14312. ' doit2(pa);',
  14313. ' doit2;',
  14314. 'end;',
  14315. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  14316. 'begin',
  14317. ' pa:=inherited;',
  14318. 'end;',
  14319. 'begin']);
  14320. ConvertProgram;
  14321. CheckSource('TestClass_CallInherited_WithParams',
  14322. LinesToStr([ // statements
  14323. 'rtl.createClass(this,"TObject",null,function(){',
  14324. ' this.$init = function () {',
  14325. ' };',
  14326. ' this.$final = function () {',
  14327. ' };',
  14328. ' this.DoVirtual = function (pA,pB) {',
  14329. ' };',
  14330. ' this.DoIt = function (pA,pB) {',
  14331. ' };',
  14332. ' this.DoIt2 = function (pA,pB) {',
  14333. ' };',
  14334. ' this.GetIt = function (pA, pB) {',
  14335. ' var Result = 0;',
  14336. ' return Result;',
  14337. ' };',
  14338. '});',
  14339. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  14340. ' this.DoAbstract = function (pA,pB) {',
  14341. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14342. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14343. ' };',
  14344. ' this.DoVirtual = function (pA,pB) {',
  14345. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  14346. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  14347. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  14348. ' this.DoIt(pA,pB);',
  14349. ' this.DoIt(pA,0);',
  14350. ' this.DoIt2(pA,2);',
  14351. ' this.DoIt2(1,2);',
  14352. ' };',
  14353. ' this.GetIt$1 = function (pA, pB) {',
  14354. ' var Result = 0;',
  14355. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  14356. ' return Result;',
  14357. ' };',
  14358. '});'
  14359. ]),
  14360. LinesToStr([ // this.$main
  14361. ''
  14362. ]));
  14363. end;
  14364. procedure TTestModule.TestClasS_CallInheritedConstructor;
  14365. begin
  14366. StartProgram(false);
  14367. Add('type');
  14368. Add(' TObject = class');
  14369. Add(' constructor Create; virtual;');
  14370. Add(' constructor CreateWithB(b: boolean);');
  14371. Add(' end;');
  14372. Add(' TA = class');
  14373. Add(' constructor Create; override;');
  14374. Add(' constructor CreateWithC(c: char);');
  14375. Add(' procedure DoIt;');
  14376. Add(' class function DoSome: TObject;');
  14377. Add(' end;');
  14378. Add('constructor tobject.create;');
  14379. Add('begin');
  14380. Add(' inherited; // call non existing ancestor -> ignore silently');
  14381. Add('end;');
  14382. Add('constructor tobject.createwithb(b: boolean);');
  14383. Add('begin');
  14384. Add(' inherited; // call non existing ancestor -> ignore silently');
  14385. Add(' create; // normal call');
  14386. Add('end;');
  14387. Add('constructor ta.create;');
  14388. Add('begin');
  14389. Add(' inherited; // normal call TObject.Create');
  14390. Add(' inherited create; // normal call TObject.Create');
  14391. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  14392. Add('end;');
  14393. Add('constructor ta.createwithc(c: char);');
  14394. Add('begin');
  14395. Add(' inherited create; // call TObject.Create');
  14396. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  14397. Add(' doit;');
  14398. Add(' doit();');
  14399. Add(' dosome;');
  14400. Add('end;');
  14401. Add('procedure ta.doit;');
  14402. Add('begin');
  14403. Add(' create; // normal call');
  14404. Add(' createwithb(false); // normal call');
  14405. Add(' createwithc(''c''); // normal call');
  14406. Add('end;');
  14407. Add('class function ta.dosome: TObject;');
  14408. Add('begin');
  14409. Add(' Result:=create; // constructor');
  14410. Add(' Result:=createwithb(true); // constructor');
  14411. Add(' Result:=createwithc(''c''); // constructor');
  14412. Add('end;');
  14413. Add('begin');
  14414. ConvertProgram;
  14415. CheckSource('TestClass_CallInheritedConstructor',
  14416. LinesToStr([ // statements
  14417. 'rtl.createClass(this,"TObject",null,function(){',
  14418. ' this.$init = function () {',
  14419. ' };',
  14420. ' this.$final = function () {',
  14421. ' };',
  14422. ' this.Create = function () {',
  14423. ' return this;',
  14424. ' };',
  14425. ' this.CreateWithB = function (b) {',
  14426. ' this.Create();',
  14427. ' return this;',
  14428. ' };',
  14429. '});',
  14430. 'rtl.createClass(this, "TA", this.TObject, function () {',
  14431. ' this.Create = function () {',
  14432. ' $mod.TObject.Create.call(this);',
  14433. ' $mod.TObject.Create.call(this);',
  14434. ' $mod.TObject.CreateWithB.call(this, false);',
  14435. ' return this;',
  14436. ' };',
  14437. ' this.CreateWithC = function (c) {',
  14438. ' $mod.TObject.Create.call(this);',
  14439. ' $mod.TObject.CreateWithB.call(this, true);',
  14440. ' this.DoIt();',
  14441. ' this.DoIt();',
  14442. ' this.$class.DoSome();',
  14443. ' return this;',
  14444. ' };',
  14445. ' this.DoIt = function () {',
  14446. ' this.Create();',
  14447. ' this.CreateWithB(false);',
  14448. ' this.CreateWithC("c");',
  14449. ' };',
  14450. ' this.DoSome = function () {',
  14451. ' var Result = null;',
  14452. ' Result = this.$create("Create");',
  14453. ' Result = this.$create("CreateWithB", [true]);',
  14454. ' Result = this.$create("CreateWithC", ["c"]);',
  14455. ' return Result;',
  14456. ' };',
  14457. '});'
  14458. ]),
  14459. LinesToStr([ // this.$main
  14460. ''
  14461. ]));
  14462. end;
  14463. procedure TTestModule.TestClass_ClassVar_Assign;
  14464. begin
  14465. StartProgram(false);
  14466. Add([
  14467. 'type',
  14468. ' TObject = class',
  14469. ' public',
  14470. ' class var vI: longint;',
  14471. ' class var Sub: TObject;',
  14472. ' constructor Create;',
  14473. ' class function GetIt(var Par: longint): tobject;',
  14474. ' end;',
  14475. 'constructor tobject.create;',
  14476. 'begin',
  14477. ' vi:=vi+1;',
  14478. ' Self.vi:=Self.vi+1;',
  14479. ' inc(vi);',
  14480. 'end;',
  14481. 'class function tobject.getit(var par: longint): tobject;',
  14482. 'begin',
  14483. ' vi:=vi+3;',
  14484. ' Self.vi:=Self.vi+4;',
  14485. ' inc(vi);',
  14486. ' Result:=self.sub;',
  14487. ' GetIt(vi);',
  14488. 'end;',
  14489. 'var Obj: tobject;',
  14490. 'begin',
  14491. ' obj:=tobject.create;',
  14492. ' tobject.vi:=3;',
  14493. ' if tobject.vi=4 then ;',
  14494. ' tobject.sub:=nil;',
  14495. ' obj.sub:=nil;',
  14496. ' obj.sub.sub:=nil;']);
  14497. ConvertProgram;
  14498. CheckSource('TestClass_ClassVar_Assign',
  14499. LinesToStr([ // statements
  14500. 'rtl.createClass(this,"TObject",null,function(){',
  14501. ' this.vI = 0;',
  14502. ' this.Sub = null;',
  14503. ' this.$init = function () {',
  14504. ' };',
  14505. ' this.$final = function () {',
  14506. ' };',
  14507. ' this.Create = function(){',
  14508. ' $mod.TObject.vI = this.vI+1;',
  14509. ' $mod.TObject.vI = this.vI+1;',
  14510. ' $mod.TObject.vI += 1;',
  14511. ' return this;',
  14512. ' };',
  14513. ' this.GetIt = function(Par){',
  14514. ' var Result = null;',
  14515. ' $mod.TObject.vI = this.vI + 3;',
  14516. ' $mod.TObject.vI = this.vI + 4;',
  14517. ' $mod.TObject.vI += 1;',
  14518. ' Result = this.Sub;',
  14519. ' this.GetIt({',
  14520. ' p: $mod.TObject,',
  14521. ' get: function () {',
  14522. ' return this.p.vI;',
  14523. ' },',
  14524. ' set: function (v) {',
  14525. ' this.p.vI = v;',
  14526. ' }',
  14527. ' });',
  14528. ' return Result;',
  14529. ' };',
  14530. '});',
  14531. 'this.Obj = null;'
  14532. ]),
  14533. LinesToStr([ // $mod.$main
  14534. '$mod.Obj = $mod.TObject.$create("Create");',
  14535. '$mod.TObject.vI = 3;',
  14536. 'if ($mod.TObject.vI === 4);',
  14537. '$mod.TObject.Sub=null;',
  14538. '$mod.TObject.Sub=null;',
  14539. '$mod.TObject.Sub=null;',
  14540. '']));
  14541. end;
  14542. procedure TTestModule.TestClass_CallClassMethod;
  14543. begin
  14544. StartProgram(false);
  14545. Add('type');
  14546. Add(' TObject = class');
  14547. Add(' public');
  14548. Add(' class var vI: longint;');
  14549. Add(' class var Sub: TObject;');
  14550. Add(' constructor Create;');
  14551. Add(' function GetMore(Par: longint): longint;');
  14552. Add(' class function GetIt(Par: longint): tobject;');
  14553. Add(' end;');
  14554. Add('constructor tobject.create;');
  14555. Add('begin');
  14556. Add(' sub:=getit(3);');
  14557. Add(' vi:=getmore(4);');
  14558. Add(' sub:=Self.getit(5);');
  14559. Add(' vi:=Self.getmore(6);');
  14560. Add('end;');
  14561. Add('function tobject.getmore(par: longint): longint;');
  14562. Add('begin');
  14563. Add(' sub:=getit(11);');
  14564. Add(' vi:=getmore(12);');
  14565. Add(' sub:=self.getit(13);');
  14566. Add(' vi:=self.getmore(14);');
  14567. Add('end;');
  14568. Add('class function tobject.getit(par: longint): tobject;');
  14569. Add('begin');
  14570. Add(' sub:=getit(21);');
  14571. Add(' vi:=sub.getmore(22);');
  14572. Add(' sub:=self.getit(23);');
  14573. Add(' vi:=self.sub.getmore(24);');
  14574. Add('end;');
  14575. Add('var Obj: tobject;');
  14576. Add('begin');
  14577. Add(' obj:=tobject.create;');
  14578. Add(' tobject.getit(5);');
  14579. Add(' obj.getit(6);');
  14580. Add(' obj.sub.getit(7);');
  14581. Add(' obj.sub.getit(8).SUB:=nil;');
  14582. Add(' obj.sub.getit(9).GETIT(10);');
  14583. Add(' obj.sub.getit(11).SuB.getit(12);');
  14584. ConvertProgram;
  14585. CheckSource('TestClass_CallClassMethod',
  14586. LinesToStr([ // statements
  14587. 'rtl.createClass(this,"TObject",null,function(){',
  14588. ' this.vI = 0;',
  14589. ' this.Sub = null;',
  14590. ' this.$init = function () {',
  14591. ' };',
  14592. ' this.$final = function () {',
  14593. ' };',
  14594. ' this.Create = function(){',
  14595. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  14596. ' $mod.TObject.vI = this.GetMore(4);',
  14597. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  14598. ' $mod.TObject.vI = this.GetMore(6);',
  14599. ' return this;',
  14600. ' };',
  14601. ' this.GetMore = function(Par){',
  14602. ' var Result = 0;',
  14603. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  14604. ' $mod.TObject.vI = this.GetMore(12);',
  14605. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  14606. ' $mod.TObject.vI = this.GetMore(14);',
  14607. ' return Result;',
  14608. ' };',
  14609. ' this.GetIt = function(Par){',
  14610. ' var Result = null;',
  14611. ' $mod.TObject.Sub = this.GetIt(21);',
  14612. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  14613. ' $mod.TObject.Sub = this.GetIt(23);',
  14614. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  14615. ' return Result;',
  14616. ' };',
  14617. '});',
  14618. 'this.Obj = null;'
  14619. ]),
  14620. LinesToStr([ // $mod.$main
  14621. '$mod.Obj = $mod.TObject.$create("Create");',
  14622. '$mod.TObject.GetIt(5);',
  14623. '$mod.Obj.$class.GetIt(6);',
  14624. '$mod.Obj.Sub.$class.GetIt(7);',
  14625. '$mod.TObject.Sub=null;',
  14626. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  14627. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  14628. '']));
  14629. end;
  14630. procedure TTestModule.TestClass_CallClassMethodStatic;
  14631. begin
  14632. StartProgram(false);
  14633. Add([
  14634. 'type',
  14635. ' TObject = class',
  14636. ' public',
  14637. ' class function Fly: tobject; static;',
  14638. ' end;',
  14639. 'class function tobject.Fly: tobject;',
  14640. 'begin',
  14641. ' Result.Fly;',
  14642. ' Result.Fly();',
  14643. ' Fly;',
  14644. ' Fly();',
  14645. ' Fly.Fly;',
  14646. ' Fly.Fly();',
  14647. 'end;',
  14648. 'var Obj: tobject;',
  14649. 'begin',
  14650. ' obj.Fly;',
  14651. ' obj.Fly();',
  14652. ' with obj do begin',
  14653. ' Fly;',
  14654. ' Fly();',
  14655. ' end;',
  14656. '']);
  14657. ConvertProgram;
  14658. CheckSource('TestClass_CallClassMethodStatic',
  14659. LinesToStr([ // statements
  14660. 'rtl.createClass(this, "TObject", null, function () {',
  14661. ' this.$init = function () {',
  14662. ' };',
  14663. ' this.$final = function () {',
  14664. ' };',
  14665. ' this.Fly = function () {',
  14666. ' var Result = null;',
  14667. ' $mod.TObject.Fly();',
  14668. ' $mod.TObject.Fly();',
  14669. ' $mod.TObject.Fly();',
  14670. ' $mod.TObject.Fly();',
  14671. ' $mod.TObject.Fly();',
  14672. ' $mod.TObject.Fly();',
  14673. ' return Result;',
  14674. ' };',
  14675. '});',
  14676. 'this.Obj = null;'
  14677. ]),
  14678. LinesToStr([ // $mod.$main
  14679. '$mod.TObject.Fly();',
  14680. '$mod.TObject.Fly();',
  14681. 'var $with = $mod.Obj;',
  14682. '$with.Fly();',
  14683. '$with.Fly();',
  14684. '']));
  14685. end;
  14686. procedure TTestModule.TestClass_Property;
  14687. begin
  14688. StartProgram(false);
  14689. Add('type');
  14690. Add(' TObject = class');
  14691. Add(' Fx: longint;');
  14692. Add(' Fy: longint;');
  14693. Add(' function GetInt: longint;');
  14694. Add(' procedure SetInt(Value: longint);');
  14695. Add(' procedure DoIt;');
  14696. Add(' property IntA: longint read Fx write Fy;');
  14697. Add(' property IntB: longint read GetInt write SetInt;');
  14698. Add(' end;');
  14699. Add('function tobject.getint: longint;');
  14700. Add('begin');
  14701. Add(' result:=fx;');
  14702. Add('end;');
  14703. Add('procedure tobject.setint(value: longint);');
  14704. Add('begin');
  14705. Add(' if value=fy then exit;');
  14706. Add(' fy:=value;');
  14707. Add('end;');
  14708. Add('procedure tobject.doit;');
  14709. Add('begin');
  14710. Add(' IntA:=IntA+1;');
  14711. Add(' Self.IntA:=Self.IntA+1;');
  14712. Add(' IntB:=IntB+1;');
  14713. Add(' Self.IntB:=Self.IntB+1;');
  14714. Add('end;');
  14715. Add('var Obj: tobject;');
  14716. Add('begin');
  14717. Add(' obj.inta:=obj.inta+1;');
  14718. Add(' if obj.intb=2 then;');
  14719. Add(' obj.intb:=obj.intb+2;');
  14720. Add(' obj.setint(obj.inta);');
  14721. ConvertProgram;
  14722. CheckSource('TestClass_Property',
  14723. LinesToStr([ // statements
  14724. 'rtl.createClass(this, "TObject", null, function () {',
  14725. ' this.$init = function () {',
  14726. ' this.Fx = 0;',
  14727. ' this.Fy = 0;',
  14728. ' };',
  14729. ' this.$final = function () {',
  14730. ' };',
  14731. ' this.GetInt = function () {',
  14732. ' var Result = 0;',
  14733. ' Result = this.Fx;',
  14734. ' return Result;',
  14735. ' };',
  14736. ' this.SetInt = function (Value) {',
  14737. ' if (Value === this.Fy) return;',
  14738. ' this.Fy = Value;',
  14739. ' };',
  14740. ' this.DoIt = function () {',
  14741. ' this.Fy = this.Fx + 1;',
  14742. ' this.Fy = this.Fx + 1;',
  14743. ' this.SetInt(this.GetInt() + 1);',
  14744. ' this.SetInt(this.GetInt() + 1);',
  14745. ' };',
  14746. '});',
  14747. 'this.Obj = null;'
  14748. ]),
  14749. LinesToStr([ // $mod.$main
  14750. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  14751. 'if ($mod.Obj.GetInt() === 2);',
  14752. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  14753. '$mod.Obj.SetInt($mod.Obj.Fx);'
  14754. ]));
  14755. end;
  14756. procedure TTestModule.TestClass_Property_ClassMethod;
  14757. begin
  14758. StartProgram(false);
  14759. Add([
  14760. 'type',
  14761. ' TObject = class',
  14762. ' class var Fx: longint;',
  14763. ' class var Fy: longint;',
  14764. ' class function GetInt: longint;',
  14765. ' class procedure SetInt(Value: longint);',
  14766. ' end;',
  14767. ' TBird = class',
  14768. ' class procedure DoIt;',
  14769. ' class property IntA: longint read Fx write Fy;',
  14770. ' class property IntB: longint read GetInt write SetInt;',
  14771. ' end;',
  14772. 'class function tobject.getint: longint;',
  14773. 'begin',
  14774. ' result:=fx;',
  14775. 'end;',
  14776. 'class procedure tobject.setint(value: longint);',
  14777. 'begin',
  14778. 'end;',
  14779. 'class procedure tbird.doit;',
  14780. 'begin',
  14781. ' FX:=3;',
  14782. ' IntA:=IntA+1;',
  14783. ' Self.IntA:=Self.IntA+1;',
  14784. ' IntB:=IntB+1;',
  14785. ' Self.IntB:=Self.IntB+1;',
  14786. ' with Self do begin',
  14787. ' FX:=11;',
  14788. ' IntA:=IntA+12;',
  14789. ' IntB:=IntB+13;',
  14790. ' end;',
  14791. 'end;',
  14792. 'var Obj: tbird;',
  14793. 'begin',
  14794. ' tbird.fx:=tbird.fx+1;',
  14795. ' tbird.inta:=tbird.inta+1;',
  14796. ' if tbird.intb=2 then;',
  14797. ' tbird.intb:=tbird.intb+2;',
  14798. ' tbird.setint(tbird.inta);',
  14799. ' obj.inta:=obj.inta+1;',
  14800. ' if obj.intb=2 then;',
  14801. ' obj.intb:=obj.intb+2;',
  14802. ' obj.setint(obj.inta);',
  14803. ' with Tbird do begin',
  14804. ' FX:=FY+1;',
  14805. ' inta:=inta+2;',
  14806. ' intb:=intb+3;',
  14807. ' end;',
  14808. ' with Obj do begin',
  14809. ' FX:=FY+1;',
  14810. ' inta:=inta+2;',
  14811. ' intb:=intb+3;',
  14812. ' end;',
  14813. '']);
  14814. ConvertProgram;
  14815. CheckSource('TestClass_Property_ClassMethod',
  14816. LinesToStr([ // statements
  14817. 'rtl.createClass(this, "TObject", null, function () {',
  14818. ' this.Fx = 0;',
  14819. ' this.Fy = 0;',
  14820. ' this.$init = function () {',
  14821. ' };',
  14822. ' this.$final = function () {',
  14823. ' };',
  14824. ' this.GetInt = function () {',
  14825. ' var Result = 0;',
  14826. ' Result = this.Fx;',
  14827. ' return Result;',
  14828. ' };',
  14829. ' this.SetInt = function (Value) {',
  14830. ' };',
  14831. '});',
  14832. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14833. ' this.DoIt = function () {',
  14834. ' $mod.TObject.Fx = 3;',
  14835. ' $mod.TObject.Fy = this.Fx + 1;',
  14836. ' $mod.TObject.Fy = this.Fx + 1;',
  14837. ' this.SetInt(this.GetInt() + 1);',
  14838. ' this.SetInt(this.GetInt() + 1);',
  14839. ' $mod.TObject.Fx = 11;',
  14840. ' $mod.TObject.Fy = this.Fx + 12;',
  14841. ' this.SetInt(this.GetInt() + 13);',
  14842. ' };',
  14843. '});',
  14844. 'this.Obj = null;'
  14845. ]),
  14846. LinesToStr([ // $mod.$main
  14847. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  14848. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  14849. 'if ($mod.TBird.GetInt() === 2);',
  14850. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  14851. '$mod.TBird.SetInt($mod.TBird.Fx);',
  14852. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  14853. 'if ($mod.Obj.$class.GetInt() === 2);',
  14854. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  14855. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  14856. 'var $with = $mod.TBird;',
  14857. '$mod.TObject.Fx = $with.Fy + 1;',
  14858. '$mod.TObject.Fy = $with.Fx + 2;',
  14859. '$with.SetInt($with.GetInt() + 3);',
  14860. 'var $with1 = $mod.Obj;',
  14861. '$mod.TObject.Fx = $with1.Fy + 1;',
  14862. '$mod.TObject.Fy = $with1.Fx + 2;',
  14863. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  14864. '']));
  14865. end;
  14866. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  14867. begin
  14868. StartProgram(false);
  14869. Add([
  14870. 'type',
  14871. ' TObject = class',
  14872. ' class function GetInt: longint; static;',
  14873. ' class procedure SetInt(Value: longint); static;',
  14874. ' class function GetItems(Index: word): longint; static;',
  14875. ' class procedure SetItems(Index: word; const Value: longint); static;',
  14876. ' end;',
  14877. ' TBird = class',
  14878. ' class procedure Fly;',
  14879. ' class property IntA: longint read GetInt write SetInt;',
  14880. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  14881. ' end;',
  14882. 'class function tobject.getint: longint;',
  14883. 'begin',
  14884. 'end;',
  14885. 'class procedure tobject.setint(value: longint);',
  14886. 'begin',
  14887. 'end;',
  14888. 'class function tobject.GetItems(Index: word): longint;',
  14889. 'begin',
  14890. 'end;',
  14891. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  14892. 'begin',
  14893. 'end;',
  14894. 'class procedure tbird.fly;',
  14895. 'var w: longint;',
  14896. 'begin',
  14897. ' inta:=inta+51;',
  14898. ' w:=items[52];',
  14899. ' items[53]:=54;',
  14900. 'end;',
  14901. 'var Obj: tbird;',
  14902. ' i: longint;',
  14903. 'begin',
  14904. ' tbird.inta:=tbird.inta+1;',
  14905. ' i:=tbird.items[2];',
  14906. ' tbird.items[3]:=4;',
  14907. ' obj.inta:=obj.inta+11;',
  14908. ' i:=obj.items[12];',
  14909. ' obj.items[13]:=14;',
  14910. ' with Tbird do begin',
  14911. ' inta:=inta+21;',
  14912. ' i:=items[22];',
  14913. ' items[23]:=24;',
  14914. ' end;',
  14915. ' with Obj do begin',
  14916. ' inta:=inta+31;',
  14917. ' i:=items[32];',
  14918. ' items[33]:=34;',
  14919. ' end;',
  14920. '']);
  14921. ConvertProgram;
  14922. CheckSource('TestClass_Property_ClassMethod',
  14923. LinesToStr([ // statements
  14924. 'rtl.createClass(this, "TObject", null, function () {',
  14925. ' this.$init = function () {',
  14926. ' };',
  14927. ' this.$final = function () {',
  14928. ' };',
  14929. ' this.GetInt = function () {',
  14930. ' var Result = 0;',
  14931. ' return Result;',
  14932. ' };',
  14933. ' this.SetInt = function (Value) {',
  14934. ' };',
  14935. ' this.GetItems = function (Index) {',
  14936. ' var Result = 0;',
  14937. ' return Result;',
  14938. ' };',
  14939. ' this.SetItems = function (Index, Value) {',
  14940. ' };',
  14941. '});',
  14942. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14943. ' this.Fly = function () {',
  14944. ' var w = 0;',
  14945. ' this.SetInt(this.GetInt() + 51);',
  14946. ' w = this.GetItems(52);',
  14947. ' this.SetItems(53, 54);',
  14948. ' };',
  14949. '});',
  14950. 'this.Obj = null;',
  14951. 'this.i = 0;',
  14952. '']),
  14953. LinesToStr([ // $mod.$main
  14954. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  14955. '$mod.i = $mod.TObject.GetItems(2);',
  14956. '$mod.TObject.SetItems(3, 4);',
  14957. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  14958. '$mod.i = $mod.TObject.GetItems(12);',
  14959. '$mod.TObject.SetItems(13, 14);',
  14960. 'var $with = $mod.TBird;',
  14961. '$with.SetInt($with.GetInt() + 21);',
  14962. '$mod.i = $with.GetItems(22);',
  14963. '$with.SetItems(23, 24);',
  14964. 'var $with1 = $mod.Obj;',
  14965. '$with1.SetInt($with1.GetInt() + 31);',
  14966. '$mod.i = $with1.GetItems(32);',
  14967. '$with1.SetItems(33, 34);',
  14968. '']));
  14969. end;
  14970. procedure TTestModule.TestClass_Property_Indexed;
  14971. begin
  14972. StartProgram(false);
  14973. Add([
  14974. 'type',
  14975. ' TObject = class',
  14976. ' FItems: array of longint;',
  14977. ' function GetItems(Index: longint): longint;',
  14978. ' procedure SetItems(Index: longint; Value: longint);',
  14979. ' procedure DoIt;',
  14980. ' property Items[Index: longint]: longint read getitems write setitems;',
  14981. ' end;',
  14982. 'function tobject.getitems(index: longint): longint;',
  14983. 'begin',
  14984. ' Result:=fitems[index];',
  14985. 'end;',
  14986. 'procedure tobject.setitems(index: longint; value: longint);',
  14987. 'begin',
  14988. ' fitems[index]:=value;',
  14989. 'end;',
  14990. 'procedure tobject.doit;',
  14991. 'begin',
  14992. ' items[1]:=2;',
  14993. ' items[3]:=items[4];',
  14994. ' self.items[5]:=self.items[6];',
  14995. ' items[items[7]]:=items[items[8]];',
  14996. 'end;',
  14997. 'var Obj: tobject;',
  14998. 'begin',
  14999. ' obj.Items[11]:=obj.Items[12];',
  15000. '']);
  15001. ConvertProgram;
  15002. CheckSource('TestClass_Property_Indexed',
  15003. LinesToStr([ // statements
  15004. 'rtl.createClass(this, "TObject", null, function () {',
  15005. ' this.$init = function () {',
  15006. ' this.FItems = [];',
  15007. ' };',
  15008. ' this.$final = function () {',
  15009. ' this.FItems = undefined;',
  15010. ' };',
  15011. ' this.GetItems = function (Index) {',
  15012. ' var Result = 0;',
  15013. ' Result = this.FItems[Index];',
  15014. ' return Result;',
  15015. ' };',
  15016. ' this.SetItems = function (Index, Value) {',
  15017. ' this.FItems[Index] = Value;',
  15018. ' };',
  15019. ' this.DoIt = function () {',
  15020. ' this.SetItems(1, 2);',
  15021. ' this.SetItems(3,this.GetItems(4));',
  15022. ' this.SetItems(5,this.GetItems(6));',
  15023. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  15024. ' };',
  15025. '});',
  15026. 'this.Obj = null;'
  15027. ]),
  15028. LinesToStr([ // $mod.$main
  15029. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  15030. ]));
  15031. end;
  15032. procedure TTestModule.TestClass_Property_IndexSpec;
  15033. begin
  15034. StartProgram(false);
  15035. Add([
  15036. 'type',
  15037. ' TEnum = (red, blue);',
  15038. ' TObject = class',
  15039. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  15040. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  15041. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  15042. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  15043. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  15044. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  15045. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  15046. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  15047. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  15048. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  15049. ' end;',
  15050. 'procedure DoIt(b: boolean); begin end;',
  15051. 'var',
  15052. ' o: TObject;',
  15053. 'begin',
  15054. ' o.B1:=o.B1;',
  15055. ' o.B2:=o.B2;',
  15056. ' o.B3:=o.B3;',
  15057. ' o.I1[''a'']:=o.I1[''b''];',
  15058. ' doit(o.b1);',
  15059. ' doit(o.b2);',
  15060. ' doit(o.i1[''c'']);',
  15061. '']);
  15062. ConvertProgram;
  15063. CheckSource('TestClass_Property_IndexSpec',
  15064. LinesToStr([ // statements
  15065. 'this.TEnum = {',
  15066. ' "0": "red",',
  15067. ' red: 0,',
  15068. ' "1": "blue",',
  15069. ' blue: 1',
  15070. '};',
  15071. 'rtl.createClass(this, "TObject", null, function () {',
  15072. ' this.$init = function () {',
  15073. ' };',
  15074. ' this.$final = function () {',
  15075. ' };',
  15076. '});',
  15077. 'this.DoIt = function (b) {',
  15078. '};',
  15079. 'this.o = null;',
  15080. '']),
  15081. LinesToStr([ // $mod.$main
  15082. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  15083. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  15084. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  15085. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  15086. '$mod.DoIt($mod.o.GetIntBool(1));',
  15087. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  15088. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  15089. '']));
  15090. end;
  15091. procedure TTestModule.TestClass_PropertyOfTypeArray;
  15092. begin
  15093. StartProgram(false);
  15094. Add('type');
  15095. Add(' TArray = array of longint;');
  15096. Add(' TObject = class');
  15097. Add(' FItems: TArray;');
  15098. Add(' function GetItems: tarray;');
  15099. Add(' procedure SetItems(Value: tarray);');
  15100. Add(' property Items: tarray read getitems write setitems;');
  15101. Add(' procedure SetNumbers(const Value: tarray);');
  15102. Add(' property Numbers: tarray write setnumbers;');
  15103. Add(' end;');
  15104. Add('function tobject.getitems: tarray;');
  15105. Add('begin');
  15106. Add(' Result:=fitems;');
  15107. Add('end;');
  15108. Add('procedure tobject.setitems(value: tarray);');
  15109. Add('begin');
  15110. Add(' fitems:=value;');
  15111. Add(' fitems:=nil;');
  15112. Add(' Items:=nil;');
  15113. Add(' Items:=Items;');
  15114. Add(' Items[1]:=2;');
  15115. Add(' fitems[3]:=Items[4];');
  15116. Add(' Items[5]:=Items[6];');
  15117. Add(' Self.Items[7]:=8;');
  15118. Add(' Self.Items[9]:=Self.Items[10];');
  15119. Add(' Items[Items[11]]:=Items[Items[12]];');
  15120. Add('end;');
  15121. Add('procedure tobject.SetNumbers(const Value: tarray);');
  15122. Add('begin;');
  15123. Add(' Numbers:=nil;');
  15124. Add(' Numbers:=Value;');
  15125. Add(' Self.Numbers:=Value;');
  15126. Add('end;');
  15127. Add('var Obj: tobject;');
  15128. Add('begin');
  15129. Add(' obj.items:=nil;');
  15130. Add(' obj.items:=obj.items;');
  15131. Add(' obj.items[11]:=obj.items[12];');
  15132. ConvertProgram;
  15133. CheckSource('TestClass_PropertyOfTypeArray',
  15134. LinesToStr([ // statements
  15135. 'rtl.createClass(this, "TObject", null, function () {',
  15136. ' this.$init = function () {',
  15137. ' this.FItems = [];',
  15138. ' };',
  15139. ' this.$final = function () {',
  15140. ' this.FItems = undefined;',
  15141. ' };',
  15142. ' this.GetItems = function () {',
  15143. ' var Result = [];',
  15144. ' Result = rtl.arrayRef(this.FItems);',
  15145. ' return Result;',
  15146. ' };',
  15147. ' this.SetItems = function (Value) {',
  15148. ' this.FItems = rtl.arrayRef(Value);',
  15149. ' this.FItems = [];',
  15150. ' this.SetItems([]);',
  15151. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  15152. ' this.GetItems()[1] = 2;',
  15153. ' this.FItems[3] = this.GetItems()[4];',
  15154. ' this.GetItems()[5] = this.GetItems()[6];',
  15155. ' this.GetItems()[7] = 8;',
  15156. ' this.GetItems()[9] = this.GetItems()[10];',
  15157. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  15158. ' };',
  15159. ' this.SetNumbers = function (Value) {',
  15160. ' this.SetNumbers([]);',
  15161. ' this.SetNumbers(Value);',
  15162. ' this.SetNumbers(Value);',
  15163. ' };',
  15164. '});',
  15165. 'this.Obj = null;'
  15166. ]),
  15167. LinesToStr([ // $mod.$main
  15168. '$mod.Obj.SetItems([]);',
  15169. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  15170. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  15171. ]));
  15172. end;
  15173. procedure TTestModule.TestClass_PropertyDefault;
  15174. begin
  15175. StartProgram(false);
  15176. Add([
  15177. 'type',
  15178. ' TArray = array of longint;',
  15179. ' TObject = class',
  15180. ' end;',
  15181. ' TBird = class',
  15182. ' FItems: TArray;',
  15183. ' function GetItems(Index: longint): longint;',
  15184. ' procedure SetItems(Index, Value: longint);',
  15185. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  15186. ' end;',
  15187. 'function TBird.getitems(index: longint): longint;',
  15188. 'begin',
  15189. 'end;',
  15190. 'procedure TBird.setitems(index, value: longint);',
  15191. 'begin',
  15192. ' Self[1]:=2;',
  15193. ' Self[3]:=Self[index];',
  15194. ' Self[index]:=Self[Self[value]];',
  15195. ' Self[Self[4]]:=value;',
  15196. 'end;',
  15197. 'var',
  15198. ' Bird: TBird;',
  15199. ' Obj: TObject;',
  15200. 'begin',
  15201. ' bird[11]:=12;',
  15202. ' bird[13]:=bird[14];',
  15203. ' bird[Bird[15]]:=bird[Bird[15]];',
  15204. ' TBird(obj)[16]:=TBird(obj)[17];',
  15205. ' (obj as tbird)[18]:=19;',
  15206. '']);
  15207. ConvertProgram;
  15208. CheckSource('TestClass_PropertyDefault',
  15209. LinesToStr([ // statements
  15210. 'rtl.createClass(this, "TObject", null, function () {',
  15211. ' this.$init = function () {',
  15212. ' };',
  15213. ' this.$final = function () {',
  15214. ' };',
  15215. '});',
  15216. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15217. ' this.$init = function () {',
  15218. ' $mod.TObject.$init.call(this);',
  15219. ' this.FItems = [];',
  15220. ' };',
  15221. ' this.$final = function () {',
  15222. ' this.FItems = undefined;',
  15223. ' $mod.TObject.$final.call(this);',
  15224. ' };',
  15225. ' this.GetItems = function (Index) {',
  15226. ' var Result = 0;',
  15227. ' return Result;',
  15228. ' };',
  15229. ' this.SetItems = function (Index, Value) {',
  15230. ' this.SetItems(1, 2);',
  15231. ' this.SetItems(3, this.GetItems(Index));',
  15232. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  15233. ' this.SetItems(this.GetItems(4), Value);',
  15234. ' };',
  15235. '});',
  15236. 'this.Bird = null;',
  15237. 'this.Obj = null;',
  15238. '']),
  15239. LinesToStr([ // $mod.$main
  15240. '$mod.Bird.SetItems(11, 12);',
  15241. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  15242. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  15243. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  15244. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  15245. '']));
  15246. end;
  15247. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  15248. begin
  15249. StartProgram(false);
  15250. Add([
  15251. 'type',
  15252. ' TObject = class end;',
  15253. ' TAlphaList = class',
  15254. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  15255. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  15256. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  15257. ' end;',
  15258. ' TBetaList = class',
  15259. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  15260. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  15261. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  15262. ' end;',
  15263. ' TBird = class',
  15264. ' procedure DoIt;',
  15265. ' end;',
  15266. 'procedure TBird.DoIt;',
  15267. 'var',
  15268. ' List: TAlphaList;',
  15269. 'begin',
  15270. ' if TBetaList(List[true])[3]=nil then ;',
  15271. ' TBetaList(List[false])[5]:=nil;',
  15272. 'end;',
  15273. 'var',
  15274. ' List: TAlphaList;',
  15275. 'begin',
  15276. ' if TBetaList(List[true])[3]=nil then ;',
  15277. ' TBetaList(List[false])[5]:=nil;',
  15278. '']);
  15279. ConvertProgram;
  15280. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  15281. LinesToStr([ // statements
  15282. 'rtl.createClass(this, "TObject", null, function () {',
  15283. ' this.$init = function () {',
  15284. ' };',
  15285. ' this.$final = function () {',
  15286. ' };',
  15287. '});',
  15288. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  15289. '});',
  15290. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  15291. '});',
  15292. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15293. ' this.DoIt = function () {',
  15294. ' var List = null;',
  15295. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  15296. ' List.GetAlphas(false).SetBetas(5, null);',
  15297. ' };',
  15298. '});',
  15299. 'this.List = null;',
  15300. '']),
  15301. LinesToStr([ // $mod.$main
  15302. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  15303. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  15304. '']));
  15305. end;
  15306. procedure TTestModule.TestClass_PropertyOverride;
  15307. begin
  15308. StartProgram(false);
  15309. Add('type');
  15310. Add(' integer = longint;');
  15311. Add(' TObject = class');
  15312. Add(' FItem: integer;');
  15313. Add(' function GetItem: integer; external name ''GetItem'';');
  15314. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  15315. Add(' property Item: integer read getitem write setitem;');
  15316. Add(' end;');
  15317. Add(' TCar = class');
  15318. Add(' FBag: integer;');
  15319. Add(' function GetBag: integer; external name ''GetBag'';');
  15320. Add(' property Item read getbag;');
  15321. Add(' end;');
  15322. Add('var');
  15323. Add(' Obj: tobject;');
  15324. Add(' Car: tcar;');
  15325. Add('begin');
  15326. Add(' Obj.Item:=Obj.Item;');
  15327. Add(' Car.Item:=Car.Item;');
  15328. ConvertProgram;
  15329. CheckSource('TestClass_PropertyOverride',
  15330. LinesToStr([ // statements
  15331. 'rtl.createClass(this, "TObject", null, function () {',
  15332. ' this.$init = function () {',
  15333. ' this.FItem = 0;',
  15334. ' };',
  15335. ' this.$final = function () {',
  15336. ' };',
  15337. '});',
  15338. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15339. ' this.$init = function () {',
  15340. ' $mod.TObject.$init.call(this);',
  15341. ' this.FBag = 0;',
  15342. ' };',
  15343. '});',
  15344. 'this.Obj = null;',
  15345. 'this.Car = null;',
  15346. '']),
  15347. LinesToStr([ // $mod.$main
  15348. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  15349. '$mod.Car.SetItem($mod.Car.GetBag());',
  15350. '']));
  15351. end;
  15352. procedure TTestModule.TestClass_PropertyIncVisibility;
  15353. begin
  15354. AddModuleWithIntfImplSrc('unit1.pp',
  15355. LinesToStr([
  15356. 'type',
  15357. ' TNumber = longint;',
  15358. ' TInteger = longint;',
  15359. ' TObject = class',
  15360. ' private',
  15361. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  15362. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  15363. ' protected',
  15364. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  15365. ' end;']),
  15366. LinesToStr([
  15367. '']));
  15368. StartProgram(true);
  15369. Add([
  15370. 'uses unit1;',
  15371. 'type',
  15372. ' TBird = class',
  15373. ' public',
  15374. ' property Items;',
  15375. ' end;',
  15376. 'procedure DoIt(i: TInteger);',
  15377. 'begin',
  15378. 'end;',
  15379. 'var b: TBird;',
  15380. 'begin',
  15381. ' b.Items[1]:=2;',
  15382. ' b.Items[3]:=b.Items[4];',
  15383. ' DoIt(b.Items[5]);',
  15384. '']);
  15385. ConvertProgram;
  15386. CheckSource('TestClass_PropertyIncVisibility',
  15387. LinesToStr([ // statements
  15388. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  15389. '});',
  15390. 'this.DoIt = function (i) {',
  15391. '};',
  15392. 'this.b = null;'
  15393. ]),
  15394. LinesToStr([ // $mod.$main
  15395. '$mod.b.SetItems(1, 2);',
  15396. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  15397. '$mod.DoIt($mod.b.GetItems(5));'
  15398. ]));
  15399. end;
  15400. procedure TTestModule.TestClass_Assigned;
  15401. begin
  15402. StartProgram(false);
  15403. Add('type');
  15404. Add(' TObject = class');
  15405. Add(' end;');
  15406. Add('var');
  15407. Add(' Obj: tobject;');
  15408. Add(' b: boolean;');
  15409. Add('begin');
  15410. Add(' if Assigned(obj) then ;');
  15411. Add(' b:=Assigned(obj) or false;');
  15412. ConvertProgram;
  15413. CheckSource('TestClass_Assigned',
  15414. LinesToStr([ // statements
  15415. 'rtl.createClass(this, "TObject", null, function () {',
  15416. ' this.$init = function () {',
  15417. ' };',
  15418. ' this.$final = function () {',
  15419. ' };',
  15420. '});',
  15421. 'this.Obj = null;',
  15422. 'this.b = false;'
  15423. ]),
  15424. LinesToStr([ // $mod.$main
  15425. 'if ($mod.Obj != null);',
  15426. '$mod.b = ($mod.Obj != null) || false;'
  15427. ]));
  15428. end;
  15429. procedure TTestModule.TestClass_WithClassDoCreate;
  15430. begin
  15431. StartProgram(false);
  15432. Add('type');
  15433. Add(' TObject = class');
  15434. Add(' aBool: boolean;');
  15435. Add(' Arr: array of boolean;');
  15436. Add(' constructor Create;');
  15437. Add(' end;');
  15438. Add('constructor TObject.Create; begin end;');
  15439. Add('var');
  15440. Add(' Obj: tobject;');
  15441. Add(' b: boolean;');
  15442. Add('begin');
  15443. Add(' with tobject.create do begin');
  15444. Add(' b:=abool;');
  15445. Add(' abool:=b;');
  15446. Add(' b:=arr[1];');
  15447. Add(' arr[2]:=b;');
  15448. Add(' end;');
  15449. Add(' with tobject do');
  15450. Add(' obj:=create;');
  15451. Add(' with obj do begin');
  15452. Add(' create;');
  15453. Add(' b:=abool;');
  15454. Add(' abool:=b;');
  15455. Add(' b:=arr[3];');
  15456. Add(' arr[4]:=b;');
  15457. Add(' end;');
  15458. ConvertProgram;
  15459. CheckSource('TestClass_WithClassDoCreate',
  15460. LinesToStr([ // statements
  15461. 'rtl.createClass(this, "TObject", null, function () {',
  15462. ' this.$init = function () {',
  15463. ' this.aBool = false;',
  15464. ' this.Arr = [];',
  15465. ' };',
  15466. ' this.$final = function () {',
  15467. ' this.Arr = undefined;',
  15468. ' };',
  15469. ' this.Create = function () {',
  15470. ' return this;',
  15471. ' };',
  15472. '});',
  15473. 'this.Obj = null;',
  15474. 'this.b = false;'
  15475. ]),
  15476. LinesToStr([ // $mod.$main
  15477. 'var $with = $mod.TObject.$create("Create");',
  15478. '$mod.b = $with.aBool;',
  15479. '$with.aBool = $mod.b;',
  15480. '$mod.b = $with.Arr[1];',
  15481. '$with.Arr[2] = $mod.b;',
  15482. 'var $with1 = $mod.TObject;',
  15483. '$mod.Obj = $with1.$create("Create");',
  15484. 'var $with2 = $mod.Obj;',
  15485. '$with2.Create();',
  15486. '$mod.b = $with2.aBool;',
  15487. '$with2.aBool = $mod.b;',
  15488. '$mod.b = $with2.Arr[3];',
  15489. '$with2.Arr[4] = $mod.b;',
  15490. '']));
  15491. end;
  15492. procedure TTestModule.TestClass_WithClassInstDoProperty;
  15493. begin
  15494. StartProgram(false);
  15495. Add('type');
  15496. Add(' TObject = class');
  15497. Add(' FInt: longint;');
  15498. Add(' constructor Create;');
  15499. Add(' function GetSize: longint;');
  15500. Add(' procedure SetSize(Value: longint);');
  15501. Add(' property Int: longint read FInt write FInt;');
  15502. Add(' property Size: longint read GetSize write SetSize;');
  15503. Add(' end;');
  15504. Add('constructor TObject.Create; begin end;');
  15505. Add('function TObject.GetSize: longint; begin; end;');
  15506. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15507. Add('var');
  15508. Add(' Obj: tobject;');
  15509. Add(' i: longint;');
  15510. Add('begin');
  15511. Add(' with TObject.Create do begin');
  15512. Add(' i:=int;');
  15513. Add(' int:=i;');
  15514. Add(' i:=size;');
  15515. Add(' size:=i;');
  15516. Add(' end;');
  15517. Add(' with obj do begin');
  15518. Add(' i:=int;');
  15519. Add(' int:=i;');
  15520. Add(' i:=size;');
  15521. Add(' size:=i;');
  15522. Add(' end;');
  15523. ConvertProgram;
  15524. CheckSource('TestClass_WithClassInstDoProperty',
  15525. LinesToStr([ // statements
  15526. 'rtl.createClass(this, "TObject", null, function () {',
  15527. ' this.$init = function () {',
  15528. ' this.FInt = 0;',
  15529. ' };',
  15530. ' this.$final = function () {',
  15531. ' };',
  15532. ' this.Create = function () {',
  15533. ' return this;',
  15534. ' };',
  15535. ' this.GetSize = function () {',
  15536. ' var Result = 0;',
  15537. ' return Result;',
  15538. ' };',
  15539. ' this.SetSize = function (Value) {',
  15540. ' };',
  15541. '});',
  15542. 'this.Obj = null;',
  15543. 'this.i = 0;'
  15544. ]),
  15545. LinesToStr([ // $mod.$main
  15546. 'var $with = $mod.TObject.$create("Create");',
  15547. '$mod.i = $with.FInt;',
  15548. '$with.FInt = $mod.i;',
  15549. '$mod.i = $with.GetSize();',
  15550. '$with.SetSize($mod.i);',
  15551. 'var $with1 = $mod.Obj;',
  15552. '$mod.i = $with1.FInt;',
  15553. '$with1.FInt = $mod.i;',
  15554. '$mod.i = $with1.GetSize();',
  15555. '$with1.SetSize($mod.i);',
  15556. '']));
  15557. end;
  15558. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  15559. begin
  15560. StartProgram(false);
  15561. Add('type');
  15562. Add(' TObject = class');
  15563. Add(' constructor Create;');
  15564. Add(' function GetItems(Index: longint): longint;');
  15565. Add(' procedure SetItems(Index, Value: longint);');
  15566. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  15567. Add(' end;');
  15568. Add('constructor TObject.Create; begin end;');
  15569. Add('function tobject.getitems(index: longint): longint; begin; end;');
  15570. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  15571. Add('var');
  15572. Add(' Obj: tobject;');
  15573. Add(' i: longint;');
  15574. Add('begin');
  15575. Add(' with TObject.Create do begin');
  15576. Add(' i:=Items[1];');
  15577. Add(' Items[2]:=i;');
  15578. Add(' end;');
  15579. Add(' with obj do begin');
  15580. Add(' i:=Items[3];');
  15581. Add(' Items[4]:=i;');
  15582. Add(' end;');
  15583. ConvertProgram;
  15584. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  15585. LinesToStr([ // statements
  15586. 'rtl.createClass(this, "TObject", null, function () {',
  15587. ' this.$init = function () {',
  15588. ' };',
  15589. ' this.$final = function () {',
  15590. ' };',
  15591. ' this.Create = function () {',
  15592. ' return this;',
  15593. ' };',
  15594. ' this.GetItems = function (Index) {',
  15595. ' var Result = 0;',
  15596. ' return Result;',
  15597. ' };',
  15598. ' this.SetItems = function (Index, Value) {',
  15599. ' };',
  15600. '});',
  15601. 'this.Obj = null;',
  15602. 'this.i = 0;'
  15603. ]),
  15604. LinesToStr([ // $mod.$main
  15605. 'var $with = $mod.TObject.$create("Create");',
  15606. '$mod.i = $with.GetItems(1);',
  15607. '$with.SetItems(2, $mod.i);',
  15608. 'var $with1 = $mod.Obj;',
  15609. '$mod.i = $with1.GetItems(3);',
  15610. '$with1.SetItems(4, $mod.i);',
  15611. '']));
  15612. end;
  15613. procedure TTestModule.TestClass_WithClassInstDoFunc;
  15614. begin
  15615. StartProgram(false);
  15616. Add('type');
  15617. Add(' TObject = class');
  15618. Add(' constructor Create;');
  15619. Add(' function GetSize: longint;');
  15620. Add(' procedure SetSize(Value: longint);');
  15621. Add(' end;');
  15622. Add('constructor TObject.Create; begin end;');
  15623. Add('function TObject.GetSize: longint; begin; end;');
  15624. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  15625. Add('var');
  15626. Add(' Obj: tobject;');
  15627. Add(' i: longint;');
  15628. Add('begin');
  15629. Add(' with TObject.Create do begin');
  15630. Add(' i:=GetSize;');
  15631. Add(' i:=GetSize();');
  15632. Add(' SetSize(i);');
  15633. Add(' end;');
  15634. Add(' with obj do begin');
  15635. Add(' i:=GetSize;');
  15636. Add(' i:=GetSize();');
  15637. Add(' SetSize(i);');
  15638. Add(' end;');
  15639. ConvertProgram;
  15640. CheckSource('TestClass_WithClassInstDoFunc',
  15641. LinesToStr([ // statements
  15642. 'rtl.createClass(this, "TObject", null, function () {',
  15643. ' this.$init = function () {',
  15644. ' };',
  15645. ' this.$final = function () {',
  15646. ' };',
  15647. ' this.Create = function () {',
  15648. ' return this;',
  15649. ' };',
  15650. ' this.GetSize = function () {',
  15651. ' var Result = 0;',
  15652. ' return Result;',
  15653. ' };',
  15654. ' this.SetSize = function (Value) {',
  15655. ' };',
  15656. '});',
  15657. 'this.Obj = null;',
  15658. 'this.i = 0;'
  15659. ]),
  15660. LinesToStr([ // $mod.$main
  15661. 'var $with = $mod.TObject.$create("Create");',
  15662. '$mod.i = $with.GetSize();',
  15663. '$mod.i = $with.GetSize();',
  15664. '$with.SetSize($mod.i);',
  15665. 'var $with1 = $mod.Obj;',
  15666. '$mod.i = $with1.GetSize();',
  15667. '$mod.i = $with1.GetSize();',
  15668. '$with1.SetSize($mod.i);',
  15669. '']));
  15670. end;
  15671. procedure TTestModule.TestClass_TypeCast;
  15672. begin
  15673. StartProgram(false);
  15674. Add('type');
  15675. Add(' TObject = class');
  15676. Add(' Next: TObject;');
  15677. Add(' constructor Create;');
  15678. Add(' end;');
  15679. Add(' TControl = class(TObject)');
  15680. Add(' Arr: array of TObject;');
  15681. Add(' function GetIt(vI: longint = 0): TObject;');
  15682. Add(' end;');
  15683. Add('constructor tobject.create; begin end;');
  15684. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  15685. Add('var');
  15686. Add(' Obj: tobject;');
  15687. Add('begin');
  15688. Add(' obj:=tcontrol(obj).next;');
  15689. Add(' tcontrol(obj):=nil;');
  15690. Add(' obj:=tcontrol(obj);');
  15691. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  15692. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  15693. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  15694. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  15695. Add(' obj:=tcontrol(nil);');
  15696. ConvertProgram;
  15697. CheckSource('TestClass_TypeCast',
  15698. LinesToStr([ // statements
  15699. 'rtl.createClass(this, "TObject", null, function () {',
  15700. ' this.$init = function () {',
  15701. ' this.Next = null;',
  15702. ' };',
  15703. ' this.$final = function () {',
  15704. ' this.Next = undefined;',
  15705. ' };',
  15706. ' this.Create = function () {',
  15707. ' return this;',
  15708. ' };',
  15709. '});',
  15710. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  15711. ' this.$init = function () {',
  15712. ' $mod.TObject.$init.call(this);',
  15713. ' this.Arr = [];',
  15714. ' };',
  15715. ' this.$final = function () {',
  15716. ' this.Arr = undefined;',
  15717. ' $mod.TObject.$final.call(this);',
  15718. ' };',
  15719. ' this.GetIt = function (vI) {',
  15720. ' var Result = null;',
  15721. ' return Result;',
  15722. ' };',
  15723. '});',
  15724. 'this.Obj = null;'
  15725. ]),
  15726. LinesToStr([ // $mod.$main
  15727. '$mod.Obj = $mod.Obj.Next;',
  15728. '$mod.Obj = null;',
  15729. '$mod.Obj = $mod.Obj;',
  15730. '$mod.Obj = $mod.Obj.GetIt(0);',
  15731. '$mod.Obj = $mod.Obj.GetIt(0);',
  15732. '$mod.Obj = $mod.Obj.GetIt(1);',
  15733. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  15734. '$mod.Obj = null;',
  15735. '']));
  15736. end;
  15737. procedure TTestModule.TestClass_TypeCastUntypedParam;
  15738. begin
  15739. StartProgram(false);
  15740. Add('type');
  15741. Add(' TObject = class end;');
  15742. Add('procedure ProcA(var A);');
  15743. Add('begin');
  15744. Add(' TObject(A):=nil;');
  15745. Add(' TObject(A):=TObject(A);');
  15746. Add(' if TObject(A)=nil then ;');
  15747. Add(' if nil=TObject(A) then ;');
  15748. Add('end;');
  15749. Add('procedure ProcB(out A);');
  15750. Add('begin');
  15751. Add(' TObject(A):=nil;');
  15752. Add(' TObject(A):=TObject(A);');
  15753. Add(' if TObject(A)=nil then ;');
  15754. Add(' if nil=TObject(A) then ;');
  15755. Add('end;');
  15756. Add('procedure ProcC(const A);');
  15757. Add('begin');
  15758. Add(' if TObject(A)=nil then ;');
  15759. Add(' if nil=TObject(A) then ;');
  15760. Add('end;');
  15761. Add('var o: TObject;');
  15762. Add('begin');
  15763. Add(' ProcA(o);');
  15764. Add(' ProcB(o);');
  15765. Add(' ProcC(o);');
  15766. ConvertProgram;
  15767. CheckSource('TestClass_TypeCastUntypedParam',
  15768. LinesToStr([ // statements
  15769. 'rtl.createClass(this, "TObject", null, function () {',
  15770. ' this.$init = function () {',
  15771. ' };',
  15772. ' this.$final = function () {',
  15773. ' };',
  15774. '});',
  15775. 'this.ProcA = function (A) {',
  15776. ' A.set(null);',
  15777. ' A.set(A.get());',
  15778. ' if (A.get() === null);',
  15779. ' if (null === A.get());',
  15780. '};',
  15781. 'this.ProcB = function (A) {',
  15782. ' A.set(null);',
  15783. ' A.set(A.get());',
  15784. ' if (A.get() === null);',
  15785. ' if (null === A.get());',
  15786. '};',
  15787. 'this.ProcC = function (A) {',
  15788. ' if (A === null);',
  15789. ' if (null === A);',
  15790. '};',
  15791. 'this.o = null;',
  15792. '']),
  15793. LinesToStr([ // $mod.$main
  15794. '$mod.ProcA({',
  15795. ' p: $mod,',
  15796. ' get: function () {',
  15797. ' return this.p.o;',
  15798. ' },',
  15799. ' set: function (v) {',
  15800. ' this.p.o = v;',
  15801. ' }',
  15802. '});',
  15803. '$mod.ProcB({',
  15804. ' p: $mod,',
  15805. ' get: function () {',
  15806. ' return this.p.o;',
  15807. ' },',
  15808. ' set: function (v) {',
  15809. ' this.p.o = v;',
  15810. ' }',
  15811. '});',
  15812. '$mod.ProcC($mod.o);',
  15813. '']));
  15814. end;
  15815. procedure TTestModule.TestClass_Overloads;
  15816. begin
  15817. StartProgram(false);
  15818. Add('type');
  15819. Add(' TObject = class');
  15820. Add(' procedure DoIt;');
  15821. Add(' procedure DoIt(vI: longint);');
  15822. Add(' end;');
  15823. Add('procedure TObject.DoIt;');
  15824. Add('begin');
  15825. Add(' DoIt;');
  15826. Add(' DoIt(1);');
  15827. Add('end;');
  15828. Add('procedure TObject.DoIt(vI: longint); begin end;');
  15829. Add('begin');
  15830. ConvertProgram;
  15831. CheckSource('TestClass_Overloads',
  15832. LinesToStr([ // statements
  15833. 'rtl.createClass(this, "TObject", null, function () {',
  15834. ' this.$init = function () {',
  15835. ' };',
  15836. ' this.$final = function () {',
  15837. ' };',
  15838. ' this.DoIt = function () {',
  15839. ' this.DoIt();',
  15840. ' this.DoIt$1(1);',
  15841. ' };',
  15842. ' this.DoIt$1 = function (vI) {',
  15843. ' };',
  15844. '});',
  15845. '']),
  15846. LinesToStr([ // $mod.$main
  15847. '']));
  15848. end;
  15849. procedure TTestModule.TestClass_OverloadsAncestor;
  15850. begin
  15851. StartProgram(false);
  15852. Add('type');
  15853. Add(' TObject = class;');
  15854. Add(' TObject = class');
  15855. Add(' procedure DoIt(vA: longint);');
  15856. Add(' procedure DoIt(vA, vB: longint);');
  15857. Add(' end;');
  15858. Add(' TCar = class;');
  15859. Add(' TCar = class');
  15860. Add(' procedure DoIt(vA: longint);');
  15861. Add(' procedure DoIt(vA, vB: longint);');
  15862. Add(' end;');
  15863. Add('procedure tobject.doit(va: longint);');
  15864. Add('begin');
  15865. Add(' doit(1);');
  15866. Add(' doit(1,2);');
  15867. Add('end;');
  15868. Add('procedure tobject.doit(va, vb: longint); begin end;');
  15869. Add('procedure tcar.doit(va: longint);');
  15870. Add('begin');
  15871. Add(' doit(1);');
  15872. Add(' doit(1,2);');
  15873. Add(' inherited doit(1);');
  15874. Add(' inherited doit(1,2);');
  15875. Add('end;');
  15876. Add('procedure tcar.doit(va, vb: longint); begin end;');
  15877. Add('begin');
  15878. ConvertProgram;
  15879. CheckSource('TestClass_OverloadsAncestor',
  15880. LinesToStr([ // statements
  15881. 'rtl.createClass(this, "TObject", null, function () {',
  15882. ' this.$init = function () {',
  15883. ' };',
  15884. ' this.$final = function () {',
  15885. ' };',
  15886. ' this.DoIt = function (vA) {',
  15887. ' this.DoIt(1);',
  15888. ' this.DoIt$1(1,2);',
  15889. ' };',
  15890. ' this.DoIt$1 = function (vA, vB) {',
  15891. ' };',
  15892. '});',
  15893. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15894. ' this.DoIt$2 = function (vA) {',
  15895. ' this.DoIt$2(1);',
  15896. ' this.DoIt$3(1, 2);',
  15897. ' $mod.TObject.DoIt.call(this, 1);',
  15898. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  15899. ' };',
  15900. ' this.DoIt$3 = function (vA, vB) {',
  15901. ' };',
  15902. '});',
  15903. '']),
  15904. LinesToStr([ // $mod.$main
  15905. '']));
  15906. end;
  15907. procedure TTestModule.TestClass_OverloadConstructor;
  15908. begin
  15909. StartProgram(false);
  15910. Add('type');
  15911. Add(' TObject = class');
  15912. Add(' constructor Create(vA: longint);');
  15913. Add(' constructor Create(vA, vB: longint);');
  15914. Add(' end;');
  15915. Add(' TCar = class');
  15916. Add(' constructor Create(vA: longint);');
  15917. Add(' constructor Create(vA, vB: longint);');
  15918. Add(' end;');
  15919. Add('constructor tobject.create(va: longint);');
  15920. Add('begin');
  15921. Add(' create(1);');
  15922. Add(' create(1,2);');
  15923. Add('end;');
  15924. Add('constructor tobject.create(va, vb: longint); begin end;');
  15925. Add('constructor tcar.create(va: longint);');
  15926. Add('begin');
  15927. Add(' create(1);');
  15928. Add(' create(1,2);');
  15929. Add(' inherited create(1);');
  15930. Add(' inherited create(1,2);');
  15931. Add('end;');
  15932. Add('constructor tcar.create(va, vb: longint); begin end;');
  15933. Add('begin');
  15934. Add(' tobject.create(1);');
  15935. Add(' tobject.create(1,2);');
  15936. Add(' tcar.create(1);');
  15937. Add(' tcar.create(1,2);');
  15938. ConvertProgram;
  15939. CheckSource('TestClass_OverloadConstructor',
  15940. LinesToStr([ // statements
  15941. 'rtl.createClass(this, "TObject", null, function () {',
  15942. ' this.$init = function () {',
  15943. ' };',
  15944. ' this.$final = function () {',
  15945. ' };',
  15946. ' this.Create = function (vA) {',
  15947. ' this.Create(1);',
  15948. ' this.Create$1(1,2);',
  15949. ' return this;',
  15950. ' };',
  15951. ' this.Create$1 = function (vA, vB) {',
  15952. ' return this;',
  15953. ' };',
  15954. '});',
  15955. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15956. ' this.Create$2 = function (vA) {',
  15957. ' this.Create$2(1);',
  15958. ' this.Create$3(1, 2);',
  15959. ' $mod.TObject.Create.call(this, 1);',
  15960. ' $mod.TObject.Create$1.call(this, 1, 2);',
  15961. ' return this;',
  15962. ' };',
  15963. ' this.Create$3 = function (vA, vB) {',
  15964. ' return this;',
  15965. ' };',
  15966. '});',
  15967. '']),
  15968. LinesToStr([ // $mod.$main
  15969. '$mod.TObject.$create("Create", [1]);',
  15970. '$mod.TObject.$create("Create$1", [1, 2]);',
  15971. '$mod.TCar.$create("Create$2", [1]);',
  15972. '$mod.TCar.$create("Create$3", [1, 2]);',
  15973. '']));
  15974. end;
  15975. procedure TTestModule.TestClass_OverloadDelphiOverride;
  15976. begin
  15977. StartProgram(false);
  15978. Add([
  15979. '{$mode delphi}',
  15980. 'type',
  15981. ' TObject = class end;',
  15982. ' TBird = class',
  15983. ' function {#a}GetValue: longint; overload; virtual;',
  15984. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  15985. ' end;',
  15986. ' TEagle = class(TBird)',
  15987. ' function {#c}GetValue: longint; overload; override;',
  15988. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  15989. ' end;',
  15990. 'function TBird.GetValue: longint;',
  15991. 'begin',
  15992. ' if 3={@a}GetValue then ;',
  15993. ' if 4={@b}GetValue(5) then ;',
  15994. 'end;',
  15995. 'function TBird.GetValue(AValue: longint): longint;',
  15996. 'begin',
  15997. 'end;',
  15998. 'function TEagle.GetValue: longint;',
  15999. 'begin',
  16000. ' if 13={@c}GetValue then ;',
  16001. ' if 14={@d}GetValue(15) then ;',
  16002. ' if 15=inherited {@a}GetValue then ;',
  16003. ' if 16=inherited {@b}GetValue(17) then ;',
  16004. 'end;',
  16005. 'function TEagle.GetValue(AValue: longint): longint;',
  16006. 'begin',
  16007. 'end;',
  16008. 'var',
  16009. ' e: TEagle;',
  16010. 'begin',
  16011. ' if 23=e.{@c}GetValue then ;',
  16012. ' if 24=e.{@d}GetValue(25) then ;']);
  16013. ConvertProgram;
  16014. CheckSource('TestClass_OverloadDelphiOverride',
  16015. LinesToStr([ // statements
  16016. 'rtl.createClass(this, "TObject", null, function () {',
  16017. ' this.$init = function () {',
  16018. ' };',
  16019. ' this.$final = function () {',
  16020. ' };',
  16021. '});',
  16022. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16023. ' this.GetValue = function () {',
  16024. ' var Result = 0;',
  16025. ' if (3 === this.GetValue()) ;',
  16026. ' if (4 === this.GetValue$1(5)) ;',
  16027. ' return Result;',
  16028. ' };',
  16029. ' this.GetValue$1 = function (AValue) {',
  16030. ' var Result = 0;',
  16031. ' return Result;',
  16032. ' };',
  16033. '});',
  16034. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16035. ' this.GetValue = function () {',
  16036. ' var Result = 0;',
  16037. ' if (13 === this.GetValue()) ;',
  16038. ' if (14 === this.GetValue$1(15)) ;',
  16039. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  16040. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  16041. ' return Result;',
  16042. ' };',
  16043. ' this.GetValue$1 = function (AValue) {',
  16044. ' var Result = 0;',
  16045. ' return Result;',
  16046. ' };',
  16047. '});',
  16048. 'this.e = null;',
  16049. '']),
  16050. LinesToStr([ // $mod.$main
  16051. 'if (23 === $mod.e.GetValue()) ;',
  16052. 'if (24 === $mod.e.GetValue$1(25)) ;',
  16053. '']));
  16054. end;
  16055. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  16056. begin
  16057. StartProgram(false);
  16058. Add([
  16059. '{$mode delphi}',
  16060. 'type',
  16061. ' TObject = class end;',
  16062. ' TAnimal = class',
  16063. ' public',
  16064. ' {#animal_a}A: longint;',
  16065. ' function {#animal_b}B: longint;',
  16066. ' end;',
  16067. ' TBird = class(TAnimal)',
  16068. ' public',
  16069. ' {#bird_a}A: double;',
  16070. ' {#bird_b}B: boolean;',
  16071. ' end;',
  16072. ' TEagle = class(TBird)',
  16073. ' public',
  16074. ' function {#eagle_a}A: boolean;',
  16075. ' {#eagle_b}B: double;',
  16076. ' end;',
  16077. 'function TAnimal.B: longint;',
  16078. 'begin',
  16079. 'end;',
  16080. 'function TEagle.A: boolean;',
  16081. 'begin',
  16082. ' {@eagle_b}B:=3.3;',
  16083. ' {@eagle_a}A();',
  16084. ' TBird(Self).{@bird_b}B:=true;',
  16085. ' TAnimal(Self).{@animal_a}A:=17;',
  16086. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  16087. 'end;',
  16088. 'var',
  16089. ' e: TEagle;',
  16090. 'begin',
  16091. ' e.{@eagle_b}B:=5.3;',
  16092. ' if e.{@eagle_a}A then ;',
  16093. '']);
  16094. ConvertProgram;
  16095. CheckSource('TestClass_ReintroduceVarDelphi',
  16096. LinesToStr([ // statements
  16097. 'rtl.createClass(this, "TObject", null, function () {',
  16098. ' this.$init = function () {',
  16099. ' };',
  16100. ' this.$final = function () {',
  16101. ' };',
  16102. '});',
  16103. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16104. ' this.$init = function () {',
  16105. ' $mod.TObject.$init.call(this);',
  16106. ' this.A = 0;',
  16107. ' };',
  16108. ' this.B = function () {',
  16109. ' var Result = 0;',
  16110. ' return Result;',
  16111. ' };',
  16112. '});',
  16113. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16114. ' this.$init = function () {',
  16115. ' $mod.TAnimal.$init.call(this);',
  16116. ' this.A$1 = 0.0;',
  16117. ' this.B$1 = false;',
  16118. ' };',
  16119. '});',
  16120. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  16121. ' this.$init = function () {',
  16122. ' $mod.TBird.$init.call(this);',
  16123. ' this.B$2 = 0.0;',
  16124. ' };',
  16125. ' this.A$2 = function () {',
  16126. ' var Result = false;',
  16127. ' this.B$2 = 3.3;',
  16128. ' this.A$2();',
  16129. ' this.B$1 = true;',
  16130. ' this.A = 17;',
  16131. ' this.B$1 = this.A$1 > 1;',
  16132. ' return Result;',
  16133. ' };',
  16134. '});',
  16135. 'this.e = null;',
  16136. '']),
  16137. LinesToStr([ // $mod.$main
  16138. '$mod.e.B$2 = 5.3;',
  16139. 'if ($mod.e.A$2()) ;',
  16140. '']));
  16141. end;
  16142. procedure TTestModule.TestClass_ReintroducedVar;
  16143. begin
  16144. StartProgram(false);
  16145. Add('type');
  16146. Add(' TObject = class');
  16147. Add(' strict private');
  16148. Add(' Some: longint;');
  16149. Add(' end;');
  16150. Add(' TMobile = class');
  16151. Add(' strict private');
  16152. Add(' Some: string;');
  16153. Add(' end;');
  16154. Add(' TCar = class(tmobile)');
  16155. Add(' procedure Some;');
  16156. Add(' procedure Some(vA: longint);');
  16157. Add(' end;');
  16158. Add('procedure tcar.some;');
  16159. Add('begin');
  16160. Add(' Some;');
  16161. Add(' Some(1);');
  16162. Add('end;');
  16163. Add('procedure tcar.some(va: longint); begin end;');
  16164. Add('begin');
  16165. ConvertProgram;
  16166. CheckSource('TestClass_ReintroducedVar',
  16167. LinesToStr([ // statements
  16168. 'rtl.createClass(this, "TObject", null, function () {',
  16169. ' this.$init = function () {',
  16170. ' this.Some = 0;',
  16171. ' };',
  16172. ' this.$final = function () {',
  16173. ' };',
  16174. '});',
  16175. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16176. ' this.$init = function () {',
  16177. ' $mod.TObject.$init.call(this);',
  16178. ' this.Some$1 = "";',
  16179. ' };',
  16180. '});',
  16181. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16182. ' this.Some$2 = function () {',
  16183. ' this.Some$2();',
  16184. ' this.Some$3(1);',
  16185. ' };',
  16186. ' this.Some$3 = function (vA) {',
  16187. ' };',
  16188. '});',
  16189. '']),
  16190. LinesToStr([ // $mod.$main
  16191. '']));
  16192. end;
  16193. procedure TTestModule.TestClass_RaiseDescendant;
  16194. begin
  16195. StartProgram(false);
  16196. Add([
  16197. 'type',
  16198. ' TObject = class',
  16199. ' constructor Create(Msg: string);',
  16200. ' end;',
  16201. ' Exception = class',
  16202. ' end;',
  16203. ' EConvertError = class(Exception)',
  16204. ' end;',
  16205. 'constructor TObject.Create(Msg: string); begin end;',
  16206. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  16207. 'begin',
  16208. ' raise Exception.Create(''Bar1'');',
  16209. ' raise EConvertError.Create(''Bar2'');',
  16210. ' raise AssertConv(''Bar2'');',
  16211. ' raise AssertConv;',
  16212. '']);
  16213. ConvertProgram;
  16214. CheckSource('TestClass_RaiseDescendant',
  16215. LinesToStr([ // statements
  16216. 'rtl.createClass(this, "TObject", null, function () {',
  16217. ' this.$init = function () {',
  16218. ' };',
  16219. ' this.$final = function () {',
  16220. ' };',
  16221. ' this.Create = function (Msg) {',
  16222. ' return this;',
  16223. ' };',
  16224. '});',
  16225. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  16226. '});',
  16227. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  16228. '});',
  16229. 'this.AssertConv = function (Msg) {',
  16230. ' var Result = null;',
  16231. ' return Result;',
  16232. '};',
  16233. '']),
  16234. LinesToStr([ // $mod.$main
  16235. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  16236. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  16237. 'throw $mod.AssertConv("Bar2");',
  16238. 'throw $mod.AssertConv("def");',
  16239. '']));
  16240. end;
  16241. procedure TTestModule.TestClass_ExternalMethod;
  16242. begin
  16243. AddModuleWithIntfImplSrc('unit2.pas',
  16244. LinesToStr([
  16245. 'type',
  16246. ' TObject = class',
  16247. ' public',
  16248. ' procedure Intern; external name ''$DoIntern'';',
  16249. ' end;',
  16250. '']),
  16251. LinesToStr([
  16252. '']));
  16253. StartUnit(true);
  16254. Add('interface');
  16255. Add('uses unit2;');
  16256. Add('type');
  16257. Add(' TCar = class(TObject)');
  16258. Add(' public');
  16259. Add(' procedure Intern2; external name ''$DoIntern2'';');
  16260. Add(' procedure DoIt;');
  16261. Add(' end;');
  16262. Add('implementation');
  16263. Add('procedure tcar.doit;');
  16264. Add('begin');
  16265. Add(' Intern;');
  16266. Add(' Intern();');
  16267. Add(' Intern2;');
  16268. Add(' Intern2();');
  16269. Add('end;');
  16270. Add('var Obj: TCar;');
  16271. Add('begin');
  16272. Add(' obj.intern;');
  16273. Add(' obj.intern();');
  16274. Add(' obj.intern2;');
  16275. Add(' obj.intern2();');
  16276. Add(' obj.doit;');
  16277. Add(' obj.doit();');
  16278. Add(' with obj do begin');
  16279. Add(' Intern;');
  16280. Add(' Intern();');
  16281. Add(' Intern2;');
  16282. Add(' Intern2();');
  16283. Add(' end;');
  16284. ConvertUnit;
  16285. CheckSource('TestClass_ExternalMethod',
  16286. LinesToStr([
  16287. 'var $impl = $mod.$impl;',
  16288. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16289. ' this.DoIt = function () {',
  16290. ' this.$DoIntern();',
  16291. ' this.$DoIntern();',
  16292. ' this.$DoIntern2();',
  16293. ' this.$DoIntern2();',
  16294. ' };',
  16295. ' });',
  16296. '']),
  16297. LinesToStr([ // this.$init
  16298. '$impl.Obj.$DoIntern();',
  16299. '$impl.Obj.$DoIntern();',
  16300. '$impl.Obj.$DoIntern2();',
  16301. '$impl.Obj.$DoIntern2();',
  16302. '$impl.Obj.DoIt();',
  16303. '$impl.Obj.DoIt();',
  16304. 'var $with = $impl.Obj;',
  16305. '$with.$DoIntern();',
  16306. '$with.$DoIntern();',
  16307. '$with.$DoIntern2();',
  16308. '$with.$DoIntern2();',
  16309. '']),
  16310. LinesToStr([ // implementation
  16311. '$impl.Obj = null;',
  16312. '']) );
  16313. end;
  16314. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  16315. begin
  16316. StartProgram(false);
  16317. Add('type');
  16318. Add(' TObject = class');
  16319. Add(' procedure DoIt; virtual; external name ''Foo'';');
  16320. Add(' end;');
  16321. Add('begin');
  16322. SetExpectedPasResolverError('Virtual method name must match external',
  16323. nVirtualMethodNameMustMatchExternal);
  16324. ConvertProgram;
  16325. end;
  16326. procedure TTestModule.TestClass_ExternalOverrideFail;
  16327. begin
  16328. StartProgram(false);
  16329. Add('type');
  16330. Add(' TObject = class');
  16331. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  16332. Add(' end;');
  16333. Add(' TCar = class');
  16334. Add(' procedure DoIt; override; external name ''DoIt'';');
  16335. Add(' end;');
  16336. Add('begin');
  16337. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  16338. nInvalidXModifierY);
  16339. ConvertProgram;
  16340. end;
  16341. procedure TTestModule.TestClass_ExternalVar;
  16342. begin
  16343. AddModuleWithIntfImplSrc('unit2.pas',
  16344. LinesToStr([
  16345. '{$modeswitch externalclass}',
  16346. 'type',
  16347. ' TObject = class',
  16348. ' public',
  16349. ' Intern: longint external name ''$Intern'';',
  16350. ' Bracket: longint external name ''["A B"]'';',
  16351. ' end;',
  16352. '']),
  16353. LinesToStr([
  16354. '']));
  16355. StartUnit(true);
  16356. Add([
  16357. 'interface',
  16358. 'uses unit2;',
  16359. '{$modeswitch externalclass}',
  16360. 'type',
  16361. ' TCar = class(tobject)',
  16362. ' public',
  16363. ' Intern2: longint external name ''$Intern2'';',
  16364. ' procedure DoIt;',
  16365. ' end;',
  16366. 'implementation',
  16367. 'procedure tcar.doit;',
  16368. 'begin',
  16369. ' Intern:=Intern+1;',
  16370. ' Intern2:=Intern2+2;',
  16371. ' Bracket:=Bracket+3;',
  16372. 'end;',
  16373. 'var Obj: TCar;',
  16374. 'begin',
  16375. ' obj.intern:=obj.intern+1;',
  16376. ' obj.intern2:=obj.intern2+2;',
  16377. ' obj.Bracket:=obj.Bracket+3;',
  16378. ' with obj do begin',
  16379. ' intern:=intern+1;',
  16380. ' intern2:=intern2+2;',
  16381. ' Bracket:=Bracket+3;',
  16382. ' end;']);
  16383. ConvertUnit;
  16384. CheckSource('TestClass_ExternalVar',
  16385. LinesToStr([
  16386. 'var $impl = $mod.$impl;',
  16387. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  16388. ' this.DoIt = function () {',
  16389. ' this.$Intern = this.$Intern + 1;',
  16390. ' this.$Intern2 = this.$Intern2 + 2;',
  16391. ' this["A B"] = this["A B"] + 3;',
  16392. ' };',
  16393. ' });',
  16394. '']),
  16395. LinesToStr([
  16396. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  16397. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  16398. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  16399. 'var $with = $impl.Obj;',
  16400. '$with.$Intern = $with.$Intern + 1;',
  16401. '$with.$Intern2 = $with.$Intern2 + 2;',
  16402. '$with["A B"] = $with["A B"] + 3;',
  16403. '']),
  16404. LinesToStr([ // implementation
  16405. '$impl.Obj = null;',
  16406. '']));
  16407. end;
  16408. procedure TTestModule.TestClass_Const;
  16409. begin
  16410. StartProgram(false);
  16411. Add([
  16412. 'type',
  16413. ' integer = longint;',
  16414. ' TClass = class of TObject;',
  16415. ' TObject = class',
  16416. ' public',
  16417. ' const cI: integer = 3;',
  16418. ' procedure DoIt;',
  16419. ' class procedure DoMore;',
  16420. ' end;',
  16421. 'procedure tobject.doit;',
  16422. 'begin',
  16423. ' if cI=4 then;',
  16424. ' if 5=cI then;',
  16425. ' if Self.cI=6 then;',
  16426. ' if 7=Self.cI then;',
  16427. ' with Self do begin',
  16428. ' if cI=11 then;',
  16429. ' if 12=cI then;',
  16430. ' end;',
  16431. 'end;',
  16432. 'class procedure tobject.domore;',
  16433. 'begin',
  16434. ' if cI=8 then;',
  16435. ' if Self.cI=9 then;',
  16436. ' if 10=cI then;',
  16437. ' if 11=Self.cI then;',
  16438. ' with Self do begin',
  16439. ' if cI=13 then;',
  16440. ' if 14=cI then;',
  16441. ' end;',
  16442. 'end;',
  16443. 'var',
  16444. ' Obj: TObject;',
  16445. ' Cla: TClass;',
  16446. 'begin',
  16447. ' if TObject.cI=21 then ;',
  16448. ' if Obj.cI=22 then ;',
  16449. ' if Cla.cI=23 then ;',
  16450. ' with obj do if ci=24 then;',
  16451. ' with TObject do if ci=25 then;',
  16452. ' with Cla do if ci=26 then;']);
  16453. ConvertProgram;
  16454. CheckSource('TestClass_Const',
  16455. LinesToStr([
  16456. 'rtl.createClass(this, "TObject", null, function () {',
  16457. ' this.cI = 3;',
  16458. ' this.$init = function () {',
  16459. ' };',
  16460. ' this.$final = function () {',
  16461. ' };',
  16462. ' this.DoIt = function () {',
  16463. ' if (this.cI === 4) ;',
  16464. ' if (5 === this.cI) ;',
  16465. ' if (this.cI === 6) ;',
  16466. ' if (7 === this.cI) ;',
  16467. ' if (this.cI === 11) ;',
  16468. ' if (12 === this.cI) ;',
  16469. ' };',
  16470. ' this.DoMore = function () {',
  16471. ' if (this.cI === 8) ;',
  16472. ' if (this.cI === 9) ;',
  16473. ' if (10 === this.cI) ;',
  16474. ' if (11 === this.cI) ;',
  16475. ' if (this.cI === 13) ;',
  16476. ' if (14 === this.cI) ;',
  16477. ' };',
  16478. '});',
  16479. 'this.Obj = null;',
  16480. 'this.Cla = null;',
  16481. '']),
  16482. LinesToStr([
  16483. 'if ($mod.TObject.cI === 21) ;',
  16484. 'if ($mod.Obj.cI === 22) ;',
  16485. 'if ($mod.Cla.cI === 23) ;',
  16486. 'var $with = $mod.Obj;',
  16487. 'if ($with.cI === 24) ;',
  16488. 'var $with1 = $mod.TObject;',
  16489. 'if ($with1.cI === 25) ;',
  16490. 'var $with2 = $mod.Cla;',
  16491. 'if ($with2.cI === 26) ;',
  16492. '']));
  16493. end;
  16494. procedure TTestModule.TestClass_ConstEnum;
  16495. begin
  16496. StartProgram(false);
  16497. Add([
  16498. 'type',
  16499. ' TEnum = (red,blue);',
  16500. ' TObject = class',
  16501. ' end;',
  16502. ' TAnimal = class',
  16503. ' public',
  16504. ' type TSubEnum = (light,dark);',
  16505. ' const a = high(TEnum);',
  16506. ' const b = high(TSubEnum);',
  16507. ' end;',
  16508. ' TBird = class(TAnimal)',
  16509. ' public',
  16510. ' const c = high(TEnum);',
  16511. ' const d = high(TSubEnum);',
  16512. ' end;',
  16513. ' TAnt = class',
  16514. ' public',
  16515. ' const e = high(TEnum);',
  16516. ' const f = high(TBird.TSubEnum);',
  16517. ' end;',
  16518. 'begin',
  16519. '']);
  16520. ConvertProgram;
  16521. CheckSource('TestClass_ConstEnum',
  16522. LinesToStr([
  16523. 'this.TEnum = {',
  16524. ' "0": "red",',
  16525. ' red: 0,',
  16526. ' "1": "blue",',
  16527. ' blue: 1',
  16528. '};',
  16529. 'rtl.createClass(this, "TObject", null, function () {',
  16530. ' this.$init = function () {',
  16531. ' };',
  16532. ' this.$final = function () {',
  16533. ' };',
  16534. '});',
  16535. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  16536. ' this.TSubEnum = {',
  16537. ' "0": "light",',
  16538. ' light: 0,',
  16539. ' "1": "dark",',
  16540. ' dark: 1',
  16541. ' };',
  16542. ' this.a = $mod.TEnum.blue;',
  16543. ' this.b = this.TSubEnum.dark;',
  16544. '});',
  16545. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  16546. ' this.c = $mod.TEnum.blue;',
  16547. ' this.d = this.TSubEnum.dark;',
  16548. '});',
  16549. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  16550. ' this.e = $mod.TEnum.blue;',
  16551. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  16552. '});',
  16553. '']),
  16554. LinesToStr([
  16555. '']));
  16556. end;
  16557. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  16558. begin
  16559. StartProgram(false);
  16560. Add([
  16561. 'type',
  16562. ' TObject = class',
  16563. ' const cI: longint = 3;',
  16564. ' procedure Fly;',
  16565. ' procedure Run;',
  16566. ' end;',
  16567. ' TBird = class',
  16568. ' procedure Go;',
  16569. ' end;',
  16570. 'procedure tobject.fly;',
  16571. 'const cI: word = 4;',
  16572. 'begin',
  16573. ' if cI=Self.cI then ;',
  16574. 'end;',
  16575. 'procedure tobject.run;',
  16576. 'const cI: word = 5;',
  16577. 'begin',
  16578. ' if cI=Self.cI then ;',
  16579. 'end;',
  16580. 'procedure tbird.go;',
  16581. 'const cI: word = 6;',
  16582. 'begin',
  16583. ' if cI=Self.cI then ;',
  16584. 'end;',
  16585. 'begin',
  16586. '']);
  16587. ConvertProgram;
  16588. CheckSource('TestClass_LocalConstDuplicate_Prg',
  16589. LinesToStr([
  16590. 'rtl.createClass(this, "TObject", null, function () {',
  16591. ' this.cI = 3;',
  16592. ' this.$init = function () {',
  16593. ' };',
  16594. ' this.$final = function () {',
  16595. ' };',
  16596. ' var cI$1 = 4;',
  16597. ' this.Fly = function () {',
  16598. ' if (cI$1 === this.cI) ;',
  16599. ' };',
  16600. ' var cI$2 = 5;',
  16601. ' this.Run = function () {',
  16602. ' if (cI$2 === this.cI) ;',
  16603. ' };',
  16604. '});',
  16605. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16606. ' var cI$3 = 6;',
  16607. ' this.Go = function () {',
  16608. ' if (cI$3 === this.cI) ;',
  16609. ' };',
  16610. '});',
  16611. '']),
  16612. LinesToStr([
  16613. '']));
  16614. end;
  16615. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  16616. begin
  16617. StartUnit(false);
  16618. Add([
  16619. 'interface',
  16620. 'type',
  16621. ' TObject = class',
  16622. ' const cI: longint = 3;',
  16623. ' procedure Fly;',
  16624. ' procedure Run;',
  16625. ' end;',
  16626. ' TBird = class',
  16627. ' procedure Go;',
  16628. ' end;',
  16629. 'implementation',
  16630. 'procedure tobject.fly;',
  16631. 'const cI: word = 4;',
  16632. 'begin',
  16633. ' if cI=Self.cI then ;',
  16634. 'end;',
  16635. 'procedure tobject.run;',
  16636. 'const cI: word = 5;',
  16637. 'begin',
  16638. ' if cI=Self.cI then ;',
  16639. 'end;',
  16640. 'procedure tbird.go;',
  16641. 'const cI: word = 6;',
  16642. 'begin',
  16643. ' if cI=Self.cI then ;',
  16644. 'end;',
  16645. '']);
  16646. ConvertUnit;
  16647. CheckSource('TestClass_LocalConstDuplicate_Unit',
  16648. LinesToStr([
  16649. 'rtl.createClass(this, "TObject", null, function () {',
  16650. ' this.cI = 3;',
  16651. ' this.$init = function () {',
  16652. ' };',
  16653. ' this.$final = function () {',
  16654. ' };',
  16655. ' var cI$1 = 4;',
  16656. ' this.Fly = function () {',
  16657. ' if (cI$1 === this.cI) ;',
  16658. ' };',
  16659. ' var cI$2 = 5;',
  16660. ' this.Run = function () {',
  16661. ' if (cI$2 === this.cI) ;',
  16662. ' };',
  16663. '});',
  16664. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16665. ' var cI$3 = 6;',
  16666. ' this.Go = function () {',
  16667. ' if (cI$3 === this.cI) ;',
  16668. ' };',
  16669. '});',
  16670. '']),
  16671. '',
  16672. '');
  16673. end;
  16674. procedure TTestModule.TestClass_LocalVarSelfFail;
  16675. begin
  16676. StartProgram(false);
  16677. Add([
  16678. 'type',
  16679. ' TObject = class',
  16680. ' constructor Create;',
  16681. ' end;',
  16682. 'constructor tobject.create;',
  16683. 'var self: longint;',
  16684. 'begin',
  16685. 'end',
  16686. 'begin',
  16687. '']);
  16688. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  16689. ConvertProgram;
  16690. end;
  16691. procedure TTestModule.TestClass_ArgSelfFail;
  16692. begin
  16693. StartProgram(false);
  16694. Add([
  16695. 'type',
  16696. ' TObject = class',
  16697. ' procedure DoIt(Self: longint);',
  16698. ' end;',
  16699. 'procedure tobject.doit(self: longint);',
  16700. 'begin',
  16701. 'end',
  16702. 'begin',
  16703. '']);
  16704. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  16705. ConvertProgram;
  16706. end;
  16707. procedure TTestModule.TestClass_NestedProcSelf;
  16708. begin
  16709. StartProgram(false);
  16710. Add([
  16711. 'type',
  16712. ' TObject = class',
  16713. ' Key: longint;',
  16714. ' class var State: longint;',
  16715. ' procedure DoIt;',
  16716. ' function GetSize: longint; virtual; abstract;',
  16717. ' procedure SetSize(Value: longint); virtual; abstract;',
  16718. ' property Size: longint read GetSize write SetSize;',
  16719. ' end;',
  16720. 'procedure tobject.doit;',
  16721. ' procedure Sub;',
  16722. ' begin',
  16723. ' key:=key+2;',
  16724. ' self.key:=self.key+3;',
  16725. ' state:=state+4;',
  16726. ' self.state:=self.state+5;',
  16727. ' tobject.state:=tobject.state+6;',
  16728. ' size:=size+7;',
  16729. ' self.size:=self.size+8;',
  16730. ' end;',
  16731. 'begin',
  16732. ' sub;',
  16733. ' key:=key+12;',
  16734. ' self.key:=self.key+13;',
  16735. ' state:=state+14;',
  16736. ' self.state:=self.state+15;',
  16737. ' tobject.state:=tobject.state+16;',
  16738. ' size:=size+17;',
  16739. ' self.size:=self.size+18;',
  16740. 'end;',
  16741. 'begin',
  16742. '']);
  16743. ConvertProgram;
  16744. CheckSource('TestClass_NestedProcSelf',
  16745. LinesToStr([ // statements
  16746. 'rtl.createClass(this, "TObject", null, function () {',
  16747. ' this.State = 0;',
  16748. ' this.$init = function () {',
  16749. ' this.Key = 0;',
  16750. ' };',
  16751. ' this.$final = function () {',
  16752. ' };',
  16753. ' this.DoIt = function () {',
  16754. ' var $Self = this;',
  16755. ' function Sub() {',
  16756. ' $Self.Key = $Self.Key + 2;',
  16757. ' $Self.Key = $Self.Key + 3;',
  16758. ' $mod.TObject.State = $Self.State + 4;',
  16759. ' $mod.TObject.State = $Self.State + 5;',
  16760. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16761. ' $Self.SetSize($Self.GetSize() + 7);',
  16762. ' $Self.SetSize($Self.GetSize() + 8);',
  16763. ' };',
  16764. ' Sub();',
  16765. ' this.Key = this.Key + 12;',
  16766. ' $Self.Key = $Self.Key + 13;',
  16767. ' $mod.TObject.State = this.State + 14;',
  16768. ' $mod.TObject.State = $Self.State + 15;',
  16769. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16770. ' this.SetSize(this.GetSize() + 17);',
  16771. ' $Self.SetSize($Self.GetSize() + 18);',
  16772. ' };',
  16773. '});',
  16774. '']),
  16775. LinesToStr([ // $mod.$main
  16776. '']));
  16777. end;
  16778. procedure TTestModule.TestClass_NestedProcSelf2;
  16779. begin
  16780. StartProgram(false);
  16781. Add([
  16782. 'type',
  16783. ' TObject = class',
  16784. ' Key: longint;',
  16785. ' class var State: longint;',
  16786. ' function GetSize: longint; virtual; abstract;',
  16787. ' procedure SetSize(Value: longint); virtual; abstract;',
  16788. ' property Size: longint read GetSize write SetSize;',
  16789. ' end;',
  16790. ' TBird = class',
  16791. ' procedure DoIt;',
  16792. ' end;',
  16793. 'procedure tbird.doit;',
  16794. ' procedure Sub;',
  16795. ' begin',
  16796. ' key:=key+2;',
  16797. ' self.key:=self.key+3;',
  16798. ' state:=state+4;',
  16799. ' self.state:=self.state+5;',
  16800. ' tobject.state:=tobject.state+6;',
  16801. ' size:=size+7;',
  16802. ' self.size:=self.size+8;',
  16803. ' end;',
  16804. 'begin',
  16805. ' sub;',
  16806. ' key:=key+12;',
  16807. ' self.key:=self.key+13;',
  16808. ' state:=state+14;',
  16809. ' self.state:=self.state+15;',
  16810. ' tobject.state:=tobject.state+16;',
  16811. ' size:=size+17;',
  16812. ' self.size:=self.size+18;',
  16813. 'end;',
  16814. 'begin',
  16815. '']);
  16816. ConvertProgram;
  16817. CheckSource('TestClass_NestedProcSelf2',
  16818. LinesToStr([ // statements
  16819. 'rtl.createClass(this, "TObject", null, function () {',
  16820. ' this.State = 0;',
  16821. ' this.$init = function () {',
  16822. ' this.Key = 0;',
  16823. ' };',
  16824. ' this.$final = function () {',
  16825. ' };',
  16826. '});',
  16827. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16828. ' this.DoIt = function () {',
  16829. ' var $Self = this;',
  16830. ' function Sub() {',
  16831. ' $Self.Key = $Self.Key + 2;',
  16832. ' $Self.Key = $Self.Key + 3;',
  16833. ' $mod.TObject.State = $Self.State + 4;',
  16834. ' $mod.TObject.State = $Self.State + 5;',
  16835. ' $mod.TObject.State = $mod.TObject.State + 6;',
  16836. ' $Self.SetSize($Self.GetSize() + 7);',
  16837. ' $Self.SetSize($Self.GetSize() + 8);',
  16838. ' };',
  16839. ' Sub();',
  16840. ' this.Key = this.Key + 12;',
  16841. ' $Self.Key = $Self.Key + 13;',
  16842. ' $mod.TObject.State = this.State + 14;',
  16843. ' $mod.TObject.State = $Self.State + 15;',
  16844. ' $mod.TObject.State = $mod.TObject.State + 16;',
  16845. ' this.SetSize(this.GetSize() + 17);',
  16846. ' $Self.SetSize($Self.GetSize() + 18);',
  16847. ' };',
  16848. '});',
  16849. '']),
  16850. LinesToStr([ // $mod.$main
  16851. '']));
  16852. end;
  16853. procedure TTestModule.TestClass_NestedProcClassSelf;
  16854. begin
  16855. StartProgram(false);
  16856. Add([
  16857. 'type',
  16858. ' TObject = class',
  16859. ' class var State: longint;',
  16860. ' class procedure DoIt;',
  16861. ' class function GetSize: longint; virtual; abstract;',
  16862. ' class procedure SetSize(Value: longint); virtual; abstract;',
  16863. ' class property Size: longint read GetSize write SetSize;',
  16864. ' end;',
  16865. 'class procedure tobject.doit;',
  16866. ' procedure Sub;',
  16867. ' begin',
  16868. ' state:=state+2;',
  16869. ' self.state:=self.state+3;',
  16870. ' tobject.state:=tobject.state+4;',
  16871. ' size:=size+5;',
  16872. ' self.size:=self.size+6;',
  16873. ' tobject.size:=tobject.size+7;',
  16874. ' end;',
  16875. 'begin',
  16876. ' sub;',
  16877. ' state:=state+12;',
  16878. ' self.state:=self.state+13;',
  16879. ' tobject.state:=tobject.state+14;',
  16880. ' size:=size+15;',
  16881. ' self.size:=self.size+16;',
  16882. ' tobject.size:=tobject.size+17;',
  16883. 'end;',
  16884. 'begin',
  16885. '']);
  16886. ConvertProgram;
  16887. CheckSource('TestClass_NestedProcClassSelf',
  16888. LinesToStr([ // statements
  16889. 'rtl.createClass(this, "TObject", null, function () {',
  16890. ' this.State = 0;',
  16891. ' this.$init = function () {',
  16892. ' };',
  16893. ' this.$final = function () {',
  16894. ' };',
  16895. ' this.DoIt = function () {',
  16896. ' var $Self = this;',
  16897. ' function Sub() {',
  16898. ' $mod.TObject.State = $Self.State + 2;',
  16899. ' $mod.TObject.State = $Self.State + 3;',
  16900. ' $mod.TObject.State = $mod.TObject.State + 4;',
  16901. ' $Self.SetSize($Self.GetSize() + 5);',
  16902. ' $Self.SetSize($Self.GetSize() + 6);',
  16903. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  16904. ' };',
  16905. ' Sub();',
  16906. ' $mod.TObject.State = this.State + 12;',
  16907. ' $mod.TObject.State = $Self.State + 13;',
  16908. ' $mod.TObject.State = $mod.TObject.State + 14;',
  16909. ' this.SetSize(this.GetSize() + 15);',
  16910. ' $Self.SetSize($Self.GetSize() + 16);',
  16911. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  16912. ' };',
  16913. '});',
  16914. '']),
  16915. LinesToStr([ // $mod.$main
  16916. '']));
  16917. end;
  16918. procedure TTestModule.TestClass_NestedProcCallInherited;
  16919. begin
  16920. StartProgram(false);
  16921. Add([
  16922. 'type',
  16923. ' TObject = class',
  16924. ' function DoIt(k: boolean): longint; virtual;',
  16925. ' end;',
  16926. ' TBird = class',
  16927. ' function DoIt(k: boolean): longint; override;',
  16928. ' end;',
  16929. 'function tobject.doit(k: boolean): longint;',
  16930. 'begin',
  16931. 'end;',
  16932. 'function tbird.doit(k: boolean): longint;',
  16933. ' procedure Sub;',
  16934. ' begin',
  16935. ' inherited DoIt(true);',
  16936. //' if inherited DoIt(false)=4 then ;',
  16937. ' end;',
  16938. 'begin',
  16939. ' Sub;',
  16940. ' inherited;',
  16941. ' inherited DoIt(true);',
  16942. //' if inherited DoIt(false)=14 then ;',
  16943. 'end;',
  16944. 'begin',
  16945. '']);
  16946. ConvertProgram;
  16947. CheckSource('TestClass_NestedProcCallInherited',
  16948. LinesToStr([ // statements
  16949. 'rtl.createClass(this, "TObject", null, function () {',
  16950. ' this.$init = function () {',
  16951. ' };',
  16952. ' this.$final = function () {',
  16953. ' };',
  16954. ' this.DoIt = function (k) {',
  16955. ' var Result = 0;',
  16956. ' return Result;',
  16957. ' };',
  16958. '});',
  16959. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16960. ' this.DoIt = function (k) {',
  16961. ' var $Self = this;',
  16962. ' var Result = 0;',
  16963. ' function Sub() {',
  16964. ' $mod.TObject.DoIt.call($Self, true);',
  16965. ' };',
  16966. ' Sub();',
  16967. ' $mod.TObject.DoIt.apply(this, arguments);',
  16968. ' $mod.TObject.DoIt.call(this, true);',
  16969. ' return Result;',
  16970. ' };',
  16971. '});',
  16972. '']),
  16973. LinesToStr([ // $mod.$main
  16974. '']));
  16975. end;
  16976. procedure TTestModule.TestClass_TObjectFree;
  16977. begin
  16978. StartProgram(false);
  16979. Add([
  16980. 'type',
  16981. ' TObject = class',
  16982. ' Obj: tobject;',
  16983. ' procedure Free;',
  16984. ' procedure Release;',
  16985. ' end;',
  16986. 'procedure tobject.free;',
  16987. 'begin',
  16988. 'end;',
  16989. 'procedure tobject.release;',
  16990. 'begin',
  16991. ' free;',
  16992. ' if true then free;',
  16993. 'end;',
  16994. 'function DoIt(o: tobject): tobject;',
  16995. 'var l: tobject;',
  16996. 'begin',
  16997. ' o.free;',
  16998. ' o.free();',
  16999. ' l.free;',
  17000. ' l.free();',
  17001. ' o.obj.free;',
  17002. ' o.obj.free();',
  17003. ' with o do obj.free;',
  17004. ' with o do obj.free();',
  17005. ' result.Free;',
  17006. ' result.Free();',
  17007. 'end;',
  17008. 'var o: tobject;',
  17009. ' a: array of tobject;',
  17010. 'begin',
  17011. ' o.free;',
  17012. ' o.obj.free;',
  17013. ' a[1+2].free;',
  17014. '']);
  17015. ConvertProgram;
  17016. CheckSource('TestClass_TObjectFree',
  17017. LinesToStr([ // statements
  17018. 'rtl.createClass(this, "TObject", null, function () {',
  17019. ' this.$init = function () {',
  17020. ' this.Obj = null;',
  17021. ' };',
  17022. ' this.$final = function () {',
  17023. ' this.Obj = undefined;',
  17024. ' };',
  17025. ' this.Free = function () {',
  17026. ' };',
  17027. ' this.Release = function () {',
  17028. ' this.Free();',
  17029. ' if (true) this.Free();',
  17030. ' };',
  17031. '});',
  17032. 'this.DoIt = function (o) {',
  17033. ' var Result = null;',
  17034. ' var l = null;',
  17035. ' o = rtl.freeLoc(o);',
  17036. ' o = rtl.freeLoc(o);',
  17037. ' l = rtl.freeLoc(l);',
  17038. ' l = rtl.freeLoc(l);',
  17039. ' rtl.free(o, "Obj");',
  17040. ' rtl.free(o, "Obj");',
  17041. ' rtl.free(o, "Obj");',
  17042. ' rtl.free(o, "Obj");',
  17043. ' Result = rtl.freeLoc(Result);',
  17044. ' Result = rtl.freeLoc(Result);',
  17045. ' return Result;',
  17046. '};',
  17047. 'this.o = null;',
  17048. 'this.a = [];',
  17049. '']),
  17050. LinesToStr([ // $mod.$main
  17051. 'rtl.free($mod, "o");',
  17052. 'rtl.free($mod.o, "Obj");',
  17053. 'rtl.free($mod.a, 1 + 2);',
  17054. '']));
  17055. end;
  17056. procedure TTestModule.TestClass_TObjectFree_VarArg;
  17057. begin
  17058. StartProgram(false);
  17059. Add([
  17060. 'type',
  17061. ' TObject = class',
  17062. ' Obj: tobject;',
  17063. ' procedure Free;',
  17064. ' end;',
  17065. 'procedure tobject.free;',
  17066. 'begin',
  17067. 'end;',
  17068. 'procedure DoIt(var o: tobject);',
  17069. 'begin',
  17070. ' o.free;',
  17071. ' o.free();',
  17072. 'end;',
  17073. 'begin',
  17074. '']);
  17075. ConvertProgram;
  17076. CheckSource('TestClass_TObjectFree_VarArg',
  17077. LinesToStr([ // statements
  17078. 'rtl.createClass(this, "TObject", null, function () {',
  17079. ' this.$init = function () {',
  17080. ' this.Obj = null;',
  17081. ' };',
  17082. ' this.$final = function () {',
  17083. ' this.Obj = undefined;',
  17084. ' };',
  17085. ' this.Free = function () {',
  17086. ' };',
  17087. '});',
  17088. 'this.DoIt = function (o) {',
  17089. ' o.set(rtl.freeLoc(o.get()));',
  17090. ' o.set(rtl.freeLoc(o.get()));',
  17091. '};',
  17092. '']),
  17093. LinesToStr([ // $mod.$main
  17094. '']));
  17095. end;
  17096. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  17097. begin
  17098. StartProgram(false);
  17099. Add([
  17100. 'type',
  17101. ' TObject = class',
  17102. ' constructor Create;',
  17103. ' procedure Free;',
  17104. ' end;',
  17105. 'constructor TObject.Create; begin end;',
  17106. 'procedure tobject.free; begin end;',
  17107. 'begin',
  17108. ' with tobject.create do free;',
  17109. '']);
  17110. ConvertProgram;
  17111. CheckSource('TestClass_TObjectFreeNewInstance',
  17112. LinesToStr([ // statements
  17113. 'rtl.createClass(this, "TObject", null, function () {',
  17114. ' this.$init = function () {',
  17115. ' };',
  17116. ' this.$final = function () {',
  17117. ' };',
  17118. ' this.Create = function () {',
  17119. ' return this;',
  17120. ' };',
  17121. ' this.Free = function () {',
  17122. ' };',
  17123. '});',
  17124. '']),
  17125. LinesToStr([ // $mod.$main
  17126. 'var $with = $mod.TObject.$create("Create");',
  17127. '$with=rtl.freeLoc($with);',
  17128. '']));
  17129. end;
  17130. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  17131. begin
  17132. StartProgram(false);
  17133. Add([
  17134. 'type',
  17135. ' TObject = class',
  17136. ' destructor Destroy;',
  17137. ' procedure Free;',
  17138. ' end;',
  17139. 'destructor TObject.Destroy; begin end;',
  17140. 'procedure tobject.free; begin end;',
  17141. 'var o: tobject;',
  17142. 'begin',
  17143. ' o.free;',
  17144. '']);
  17145. Converter.UseLowerCase:=true;
  17146. ConvertProgram;
  17147. CheckSource('TestClass_TObjectFreeLowerCase',
  17148. LinesToStr([ // statements
  17149. 'rtl.createClass(this, "tobject", null, function () {',
  17150. ' this.$init = function () {',
  17151. ' };',
  17152. ' this.$final = function () {',
  17153. ' };',
  17154. ' rtl.tObjectDestroy = "destroy";',
  17155. ' this.destroy = function () {',
  17156. ' };',
  17157. ' this.free = function () {',
  17158. ' };',
  17159. '});',
  17160. 'this.o = null;',
  17161. '']),
  17162. LinesToStr([ // $mod.$main
  17163. 'rtl.free($mod, "o");',
  17164. '']));
  17165. end;
  17166. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  17167. begin
  17168. StartProgram(false);
  17169. Add([
  17170. 'type',
  17171. ' TObject = class',
  17172. ' procedure Free;',
  17173. ' function GetObj: tobject; virtual; abstract;',
  17174. ' end;',
  17175. 'procedure tobject.free;',
  17176. 'begin',
  17177. 'end;',
  17178. 'var o: tobject;',
  17179. 'begin',
  17180. ' o.getobj.free;',
  17181. '']);
  17182. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17183. ConvertProgram;
  17184. end;
  17185. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  17186. begin
  17187. StartProgram(false);
  17188. Add([
  17189. 'type',
  17190. ' TObject = class',
  17191. ' procedure Free;',
  17192. ' FObj: TObject;',
  17193. ' property Obj: tobject read FObj write FObj;',
  17194. ' end;',
  17195. 'procedure tobject.free;',
  17196. 'begin',
  17197. 'end;',
  17198. 'var o: tobject;',
  17199. 'begin',
  17200. ' o.obj.free;',
  17201. '']);
  17202. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  17203. ConvertProgram;
  17204. end;
  17205. procedure TTestModule.TestClass_ForIn;
  17206. begin
  17207. StartProgram(false);
  17208. Add([
  17209. 'type',
  17210. ' TObject = class end;',
  17211. ' TItem = TObject;',
  17212. ' TEnumerator = class',
  17213. ' FCurrent: TItem;',
  17214. ' property Current: TItem read FCurrent;',
  17215. ' function MoveNext: boolean;',
  17216. ' end;',
  17217. ' TBird = class',
  17218. ' function GetEnumerator: TEnumerator;',
  17219. ' end;',
  17220. 'function TEnumerator.MoveNext: boolean;',
  17221. 'begin',
  17222. 'end;',
  17223. 'function TBird.GetEnumerator: TEnumerator;',
  17224. 'begin',
  17225. 'end;',
  17226. 'var',
  17227. ' b: TBird;',
  17228. ' i, i2: TItem;',
  17229. 'begin',
  17230. ' for i in b do i2:=i;']);
  17231. ConvertProgram;
  17232. CheckSource('TestClass_ForIn',
  17233. LinesToStr([ // statements
  17234. 'rtl.createClass(this, "TObject", null, function () {',
  17235. ' this.$init = function () {',
  17236. ' };',
  17237. ' this.$final = function () {',
  17238. ' };',
  17239. '});',
  17240. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  17241. ' this.$init = function () {',
  17242. ' $mod.TObject.$init.call(this);',
  17243. ' this.FCurrent = null;',
  17244. ' };',
  17245. ' this.$final = function () {',
  17246. ' this.FCurrent = undefined;',
  17247. ' $mod.TObject.$final.call(this);',
  17248. ' };',
  17249. ' this.MoveNext = function () {',
  17250. ' var Result = false;',
  17251. ' return Result;',
  17252. ' };',
  17253. '});',
  17254. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17255. ' this.GetEnumerator = function () {',
  17256. ' var Result = null;',
  17257. ' return Result;',
  17258. ' };',
  17259. '});',
  17260. 'this.b = null;',
  17261. 'this.i = null;',
  17262. 'this.i2 = null;'
  17263. ]),
  17264. LinesToStr([ // $mod.$main
  17265. 'var $in = $mod.b.GetEnumerator();',
  17266. 'try {',
  17267. ' while ($in.MoveNext()){',
  17268. ' $mod.i = $in.FCurrent;',
  17269. ' $mod.i2 = $mod.i;',
  17270. ' }',
  17271. '} finally {',
  17272. ' $in = rtl.freeLoc($in)',
  17273. '};',
  17274. '']));
  17275. end;
  17276. procedure TTestModule.TestClass_DispatchMessage;
  17277. begin
  17278. StartProgram(false);
  17279. Add([
  17280. 'type',
  17281. ' {$DispatchField DispInt}',
  17282. ' {$DispatchStrField DispStr}',
  17283. ' TObject = class',
  17284. ' procedure Dispatch(var Msg); virtual; abstract;',
  17285. ' procedure DispatchStr(var Msg); virtual; abstract;',
  17286. ' end;',
  17287. ' THopMsg = record',
  17288. ' DispInt: longint;',
  17289. ' end;',
  17290. ' TPutMsg = record',
  17291. ' DispStr: string;',
  17292. ' end;',
  17293. ' TBird = class',
  17294. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  17295. ' procedure Run; overload; virtual; abstract;',
  17296. ' procedure Run(var Msg); overload; message ''Fast'';',
  17297. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  17298. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  17299. ' end;',
  17300. 'procedure TBird.Run(var Msg);',
  17301. 'begin',
  17302. 'end;',
  17303. 'begin',
  17304. '']);
  17305. ConvertProgram;
  17306. CheckResolverUnexpectedHints(true);
  17307. CheckSource('TestClass_Message',
  17308. LinesToStr([ // statements
  17309. 'rtl.createClass(this, "TObject", null, function () {',
  17310. ' this.$init = function () {',
  17311. ' };',
  17312. ' this.$final = function () {',
  17313. ' };',
  17314. '});',
  17315. 'rtl.recNewT(this, "THopMsg", function () {',
  17316. ' this.DispInt = 0;',
  17317. ' this.$eq = function (b) {',
  17318. ' return this.DispInt === b.DispInt;',
  17319. ' };',
  17320. ' this.$assign = function (s) {',
  17321. ' this.DispInt = s.DispInt;',
  17322. ' return this;',
  17323. ' };',
  17324. '});',
  17325. 'rtl.recNewT(this, "TPutMsg", function () {',
  17326. ' this.DispStr = "";',
  17327. ' this.$eq = function (b) {',
  17328. ' return this.DispStr === b.DispStr;',
  17329. ' };',
  17330. ' this.$assign = function (s) {',
  17331. ' this.DispStr = s.DispStr;',
  17332. ' return this;',
  17333. ' };',
  17334. '});',
  17335. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  17336. ' this.Run$1 = function (Msg) {',
  17337. ' };',
  17338. ' this.$msgint = {',
  17339. ' "2": "Fly",',
  17340. ' "3": "Hop"',
  17341. ' };',
  17342. ' this.$msgstr = {',
  17343. ' Fast: "Run$1",',
  17344. ' foo: "Put"',
  17345. ' };',
  17346. '});',
  17347. '']),
  17348. LinesToStr([ // $mod.$main
  17349. '']));
  17350. end;
  17351. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  17352. begin
  17353. StartProgram(false);
  17354. Add([
  17355. 'type',
  17356. ' TObject = class',
  17357. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  17358. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  17359. ' end;',
  17360. 'begin',
  17361. '']);
  17362. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  17363. ConvertProgram;
  17364. end;
  17365. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  17366. begin
  17367. StartProgram(false);
  17368. Add([
  17369. 'type',
  17370. ' TObject = class',
  17371. ' procedure Dispatch(var Msg); virtual; abstract;',
  17372. ' end;',
  17373. ' TFlyMsg = record',
  17374. ' FlyId: longint;',
  17375. ' end;',
  17376. ' TBird = class',
  17377. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  17378. ' end;',
  17379. 'begin',
  17380. '']);
  17381. ConvertProgram;
  17382. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  17383. end;
  17384. procedure TTestModule.TestClassOf_Create;
  17385. begin
  17386. StartProgram(false);
  17387. Add('type');
  17388. Add(' TObject = class');
  17389. Add(' constructor Create;');
  17390. Add(' end;');
  17391. Add(' TClass = class of TObject;');
  17392. Add('constructor tobject.create; begin end;');
  17393. Add('var');
  17394. Add(' Obj: tobject;');
  17395. Add(' C: tclass;');
  17396. Add('begin');
  17397. Add(' obj:=C.create;');
  17398. Add(' with c do obj:=create;');
  17399. ConvertProgram;
  17400. CheckSource('TestClassOf_Create',
  17401. LinesToStr([ // statements
  17402. 'rtl.createClass(this, "TObject", null, function () {',
  17403. ' this.$init = function () {',
  17404. ' };',
  17405. ' this.$final = function () {',
  17406. ' };',
  17407. ' this.Create = function () {',
  17408. ' return this;',
  17409. ' };',
  17410. '});',
  17411. 'this.Obj = null;',
  17412. 'this.C = null;'
  17413. ]),
  17414. LinesToStr([ // $mod.$main
  17415. '$mod.Obj = $mod.C.$create("Create");',
  17416. 'var $with = $mod.C;',
  17417. '$mod.Obj = $with.$create("Create");',
  17418. '']));
  17419. end;
  17420. procedure TTestModule.TestClassOf_Call;
  17421. begin
  17422. StartProgram(false);
  17423. Add('type');
  17424. Add(' TObject = class');
  17425. Add(' class procedure DoIt;');
  17426. Add(' end;');
  17427. Add(' TClass = class of TObject;');
  17428. Add('class procedure tobject.doit; begin end;');
  17429. Add('var');
  17430. Add(' C: tclass;');
  17431. Add('begin');
  17432. Add(' c.doit;');
  17433. Add(' with c do doit;');
  17434. ConvertProgram;
  17435. CheckSource('TestClassOf_Call',
  17436. LinesToStr([ // statements
  17437. 'rtl.createClass(this, "TObject", null, function () {',
  17438. ' this.$init = function () {',
  17439. ' };',
  17440. ' this.$final = function () {',
  17441. ' };',
  17442. ' this.DoIt = function () {',
  17443. ' };',
  17444. '});',
  17445. 'this.C = null;'
  17446. ]),
  17447. LinesToStr([ // $mod.$main
  17448. '$mod.C.DoIt();',
  17449. 'var $with = $mod.C;',
  17450. '$with.DoIt();',
  17451. '']));
  17452. end;
  17453. procedure TTestModule.TestClassOf_Assign;
  17454. begin
  17455. StartProgram(false);
  17456. Add('type');
  17457. Add(' TClass = class of TObject;');
  17458. Add(' TObject = class');
  17459. Add(' ClassType: TClass; ');
  17460. Add(' end;');
  17461. Add('var');
  17462. Add(' Obj: tobject;');
  17463. Add(' C: tclass;');
  17464. Add('begin');
  17465. Add(' c:=nil;');
  17466. Add(' c:=obj.classtype;');
  17467. ConvertProgram;
  17468. CheckSource('TestClassOf_Assign',
  17469. LinesToStr([ // statements
  17470. 'rtl.createClass(this, "TObject", null, function () {',
  17471. ' this.$init = function () {',
  17472. ' this.ClassType = null;',
  17473. ' };',
  17474. ' this.$final = function () {',
  17475. ' this.ClassType = undefined;',
  17476. ' };',
  17477. '});',
  17478. 'this.Obj = null;',
  17479. 'this.C = null;'
  17480. ]),
  17481. LinesToStr([ // $mod.$main
  17482. '$mod.C = null;',
  17483. '$mod.C = $mod.Obj.ClassType;',
  17484. '']));
  17485. end;
  17486. procedure TTestModule.TestClassOf_Is;
  17487. begin
  17488. StartProgram(false);
  17489. Add('type');
  17490. Add(' TClass = class of TObject;');
  17491. Add(' TObject = class');
  17492. Add(' end;');
  17493. Add(' TCar = class');
  17494. Add(' end;');
  17495. Add(' TCars = class of TCar;');
  17496. Add('var');
  17497. Add(' Obj: tobject;');
  17498. Add(' C: tclass;');
  17499. Add(' Cars: tcars;');
  17500. Add('begin');
  17501. Add(' if c is tcar then ;');
  17502. Add(' if c is tcars then ;');
  17503. ConvertProgram;
  17504. CheckSource('TestClassOf_Is',
  17505. LinesToStr([ // statements
  17506. 'rtl.createClass(this, "TObject", null, function () {',
  17507. ' this.$init = function () {',
  17508. ' };',
  17509. ' this.$final = function () {',
  17510. ' };',
  17511. '});',
  17512. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  17513. '});',
  17514. 'this.Obj = null;',
  17515. 'this.C = null;',
  17516. 'this.Cars = null;'
  17517. ]),
  17518. LinesToStr([ // $mod.$main
  17519. 'if(rtl.is($mod.C,$mod.TCar));',
  17520. 'if(rtl.is($mod.C,$mod.TCar));',
  17521. '']));
  17522. end;
  17523. procedure TTestModule.TestClassOf_Compare;
  17524. begin
  17525. StartProgram(false);
  17526. Add('type');
  17527. Add(' TClass = class of TObject;');
  17528. Add(' TObject = class');
  17529. Add(' ClassType: TClass; ');
  17530. Add(' end;');
  17531. Add('var');
  17532. Add(' b: boolean;');
  17533. Add(' Obj: tobject;');
  17534. Add(' C: tclass;');
  17535. Add('begin');
  17536. Add(' b:=c=nil;');
  17537. Add(' b:=nil=c;');
  17538. Add(' b:=c=obj.classtype;');
  17539. Add(' b:=obj.classtype=c;');
  17540. Add(' b:=c=TObject;');
  17541. Add(' b:=TObject=c;');
  17542. Add(' b:=c<>nil;');
  17543. Add(' b:=nil<>c;');
  17544. Add(' b:=c<>obj.classtype;');
  17545. Add(' b:=obj.classtype<>c;');
  17546. Add(' b:=c<>TObject;');
  17547. Add(' b:=TObject<>c;');
  17548. ConvertProgram;
  17549. CheckSource('TestClassOf_Compare',
  17550. LinesToStr([ // statements
  17551. 'rtl.createClass(this, "TObject", null, function () {',
  17552. ' this.$init = function () {',
  17553. ' this.ClassType = null;',
  17554. ' };',
  17555. ' this.$final = function () {',
  17556. ' this.ClassType = undefined;',
  17557. ' };',
  17558. '});',
  17559. 'this.b = false;',
  17560. 'this.Obj = null;',
  17561. 'this.C = null;'
  17562. ]),
  17563. LinesToStr([ // $mod.$main
  17564. '$mod.b = $mod.C === null;',
  17565. '$mod.b = null === $mod.C;',
  17566. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  17567. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  17568. '$mod.b = $mod.C === $mod.TObject;',
  17569. '$mod.b = $mod.TObject === $mod.C;',
  17570. '$mod.b = $mod.C !== null;',
  17571. '$mod.b = null !== $mod.C;',
  17572. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  17573. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  17574. '$mod.b = $mod.C !== $mod.TObject;',
  17575. '$mod.b = $mod.TObject !== $mod.C;',
  17576. '']));
  17577. end;
  17578. procedure TTestModule.TestClassOf_ClassVar;
  17579. begin
  17580. StartProgram(false);
  17581. Add('type');
  17582. Add(' TObject = class');
  17583. Add(' class var id: longint;');
  17584. Add(' end;');
  17585. Add(' TClass = class of TObject;');
  17586. Add('var');
  17587. Add(' C: tclass;');
  17588. Add('begin');
  17589. Add(' C.id:=C.id;');
  17590. ConvertProgram;
  17591. CheckSource('TestClassOf_ClassVar',
  17592. LinesToStr([ // statements
  17593. 'rtl.createClass(this, "TObject", null, function () {',
  17594. ' this.id = 0;',
  17595. ' this.$init = function () {',
  17596. ' };',
  17597. ' this.$final = function () {',
  17598. ' };',
  17599. '});',
  17600. 'this.C = null;'
  17601. ]),
  17602. LinesToStr([ // $mod.$main
  17603. '$mod.TObject.id = $mod.C.id;',
  17604. '']));
  17605. end;
  17606. procedure TTestModule.TestClassOf_ClassMethod;
  17607. begin
  17608. StartProgram(false);
  17609. Add('type');
  17610. Add(' TObject = class');
  17611. Add(' class function DoIt(i: longint = 0): longint;');
  17612. Add(' end;');
  17613. Add(' TClass = class of TObject;');
  17614. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  17615. Add('var');
  17616. Add(' i: longint;');
  17617. Add(' C: tclass;');
  17618. Add('begin');
  17619. Add(' C.DoIt;');
  17620. Add(' C.DoIt();');
  17621. Add(' i:=C.DoIt;');
  17622. Add(' i:=C.DoIt();');
  17623. ConvertProgram;
  17624. CheckSource('TestClassOf_ClassMethod',
  17625. LinesToStr([ // statements
  17626. 'rtl.createClass(this, "TObject", null, function () {',
  17627. ' this.$init = function () {',
  17628. ' };',
  17629. ' this.$final = function () {',
  17630. ' };',
  17631. ' this.DoIt = function (i) {',
  17632. ' var Result = 0;',
  17633. ' return Result;',
  17634. ' };',
  17635. '});',
  17636. 'this.i = 0;',
  17637. 'this.C = null;'
  17638. ]),
  17639. LinesToStr([ // $mod.$main
  17640. '$mod.C.DoIt(0);',
  17641. '$mod.C.DoIt(0);',
  17642. '$mod.i = $mod.C.DoIt(0);',
  17643. '$mod.i = $mod.C.DoIt(0);',
  17644. '']));
  17645. end;
  17646. procedure TTestModule.TestClassOf_ClassProperty;
  17647. begin
  17648. StartProgram(false);
  17649. Add([
  17650. 'type',
  17651. ' TObject = class',
  17652. ' class var FA: longint;',
  17653. ' class function GetA: longint;',
  17654. ' class procedure SetA(Value: longint);',
  17655. ' class property pA: longint read fa write fa;',
  17656. ' class property pB: longint read geta write seta;',
  17657. ' end;',
  17658. ' TObjectClass = class of tobject;',
  17659. 'class function tobject.geta: longint; begin end;',
  17660. 'class procedure tobject.seta(value: longint); begin end;',
  17661. 'var',
  17662. ' b: boolean;',
  17663. ' Obj: tobject;',
  17664. ' Cla: tobjectclass;',
  17665. 'begin',
  17666. ' obj.pa:=obj.pa;',
  17667. ' obj.pb:=obj.pb;',
  17668. ' b:=obj.pa=4;',
  17669. ' b:=obj.pb=obj.pb;',
  17670. ' b:=5=obj.pa;',
  17671. ' cla.pa:=6;',
  17672. ' cla.pa:=cla.pa;',
  17673. ' cla.pb:=cla.pb;',
  17674. ' b:=cla.pa=7;',
  17675. ' b:=cla.pb=cla.pb;',
  17676. ' b:=8=cla.pa;',
  17677. ' tobject.pa:=9;',
  17678. ' tobject.pb:=tobject.pb;',
  17679. ' b:=tobject.pa=10;',
  17680. ' b:=11=tobject.pa;',
  17681. '']);
  17682. ConvertProgram;
  17683. CheckSource('TestClassOf_ClassProperty',
  17684. LinesToStr([ // statements
  17685. 'rtl.createClass(this, "TObject", null, function () {',
  17686. ' this.FA = 0;',
  17687. ' this.$init = function () {',
  17688. ' };',
  17689. ' this.$final = function () {',
  17690. ' };',
  17691. ' this.GetA = function () {',
  17692. ' var Result = 0;',
  17693. ' return Result;',
  17694. ' };',
  17695. ' this.SetA = function (Value) {',
  17696. ' };',
  17697. '});',
  17698. 'this.b = false;',
  17699. 'this.Obj = null;',
  17700. 'this.Cla = null;'
  17701. ]),
  17702. LinesToStr([ // $mod.$main
  17703. '$mod.TObject.FA = $mod.Obj.FA;',
  17704. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  17705. '$mod.b = $mod.Obj.FA === 4;',
  17706. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  17707. '$mod.b = 5 === $mod.Obj.FA;',
  17708. '$mod.TObject.FA = 6;',
  17709. '$mod.TObject.FA = $mod.Cla.FA;',
  17710. '$mod.Cla.SetA($mod.Cla.GetA());',
  17711. '$mod.b = $mod.Cla.FA === 7;',
  17712. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  17713. '$mod.b = 8 === $mod.Cla.FA;',
  17714. '$mod.TObject.FA = 9;',
  17715. '$mod.TObject.SetA($mod.TObject.GetA());',
  17716. '$mod.b = $mod.TObject.FA === 10;',
  17717. '$mod.b = 11 === $mod.TObject.FA;',
  17718. '']));
  17719. end;
  17720. procedure TTestModule.TestClassOf_ClassMethodSelf;
  17721. begin
  17722. StartProgram(false);
  17723. Add('type');
  17724. Add(' TObject = class');
  17725. Add(' class var GlobalId: longint;');
  17726. Add(' class procedure ProcA;');
  17727. Add(' end;');
  17728. Add('class procedure tobject.proca;');
  17729. Add('var b: boolean;');
  17730. Add('begin');
  17731. Add(' b:=self=nil;');
  17732. Add(' b:=self.globalid=3;');
  17733. Add(' b:=4=self.globalid;');
  17734. Add(' self.globalid:=5;');
  17735. Add(' self.proca;');
  17736. Add('end;');
  17737. Add('begin');
  17738. ConvertProgram;
  17739. CheckSource('TestClassOf_ClassMethodSelf',
  17740. LinesToStr([ // statements
  17741. 'rtl.createClass(this, "TObject", null, function () {',
  17742. ' this.GlobalId = 0;',
  17743. ' this.$init = function () {',
  17744. ' };',
  17745. ' this.$final = function () {',
  17746. ' };',
  17747. ' this.ProcA = function () {',
  17748. ' var b = false;',
  17749. ' b = this === null;',
  17750. ' b = this.GlobalId === 3;',
  17751. ' b = 4 === this.GlobalId;',
  17752. ' $mod.TObject.GlobalId = 5;',
  17753. ' this.ProcA();',
  17754. ' };',
  17755. '});'
  17756. ]),
  17757. LinesToStr([ // $mod.$main
  17758. '']));
  17759. end;
  17760. procedure TTestModule.TestClassOf_TypeCast;
  17761. begin
  17762. StartProgram(false);
  17763. Add('type');
  17764. Add(' TObject = class');
  17765. Add(' class procedure {#TObject_DoIt}DoIt;');
  17766. Add(' end;');
  17767. Add(' TClass = class of TObject;');
  17768. Add(' TMobile = class');
  17769. Add(' class procedure {#TMobile_DoIt}DoIt;');
  17770. Add(' end;');
  17771. Add(' TMobileClass = class of TMobile;');
  17772. Add(' TCar = class(TMobile)');
  17773. Add(' class procedure {#TCar_DoIt}DoIt;');
  17774. Add(' end;');
  17775. Add(' TCarClass = class of TCar;');
  17776. Add('class procedure TObject.DoIt;');
  17777. Add('begin');
  17778. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17779. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17780. Add('end;');
  17781. Add('class procedure TMobile.DoIt;');
  17782. Add('begin');
  17783. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  17784. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  17785. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  17786. Add('end;');
  17787. Add('class procedure TCar.DoIt; begin end;');
  17788. Add('var');
  17789. Add(' ObjC: TClass;');
  17790. Add(' MobileC: TMobileClass;');
  17791. Add(' CarC: TCarClass;');
  17792. Add('begin');
  17793. Add(' ObjC.{@TObject_DoIt}DoIt;');
  17794. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  17795. Add(' CarC.{@TCar_DoIt}DoIt;');
  17796. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  17797. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  17798. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  17799. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  17800. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  17801. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  17802. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  17803. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  17804. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  17805. ConvertProgram;
  17806. CheckSource('TestClassOf_TypeCast',
  17807. LinesToStr([ // statements
  17808. 'rtl.createClass(this, "TObject", null, function () {',
  17809. ' this.$init = function () {',
  17810. ' };',
  17811. ' this.$final = function () {',
  17812. ' };',
  17813. ' this.DoIt = function () {',
  17814. ' this.DoIt();',
  17815. ' this.DoIt$1();',
  17816. ' };',
  17817. '});',
  17818. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  17819. ' this.DoIt$1 = function () {',
  17820. ' this.DoIt();',
  17821. ' this.DoIt$1();',
  17822. ' this.DoIt$2();',
  17823. ' };',
  17824. '});',
  17825. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  17826. ' this.DoIt$2 = function () {',
  17827. ' };',
  17828. '});',
  17829. 'this.ObjC = null;',
  17830. 'this.MobileC = null;',
  17831. 'this.CarC = null;',
  17832. '']),
  17833. LinesToStr([ // $mod.$main
  17834. '$mod.ObjC.DoIt();',
  17835. '$mod.MobileC.DoIt$1();',
  17836. '$mod.CarC.DoIt$2();',
  17837. '$mod.ObjC.DoIt();',
  17838. '$mod.ObjC.DoIt$1();',
  17839. '$mod.ObjC.DoIt$2();',
  17840. '$mod.MobileC.DoIt();',
  17841. '$mod.MobileC.DoIt$1();',
  17842. '$mod.MobileC.DoIt$2();',
  17843. '$mod.CarC.DoIt();',
  17844. '$mod.CarC.DoIt$1();',
  17845. '$mod.CarC.DoIt$2();',
  17846. '']));
  17847. end;
  17848. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  17849. begin
  17850. StartProgram(false);
  17851. Add('type');
  17852. Add(' TObject = class');
  17853. Add(' function CurNow: longint; ');
  17854. Add(' class function Now: longint; ');
  17855. Add(' end;');
  17856. Add('function TObject.CurNow: longint; begin end;');
  17857. Add('class function TObject.Now: longint; begin end;');
  17858. Add('var');
  17859. Add(' Obj: tobject;');
  17860. Add(' vI: longint;');
  17861. Add('begin');
  17862. Add(' obj.curnow;');
  17863. Add(' vi:=obj.curnow;');
  17864. Add(' tobject.now;');
  17865. Add(' vi:=tobject.now;');
  17866. ConvertProgram;
  17867. CheckSource('TestClassOf_ImplicitFunctionCall',
  17868. LinesToStr([ // statements
  17869. 'rtl.createClass(this, "TObject", null, function () {',
  17870. ' this.$init = function () {',
  17871. ' };',
  17872. ' this.$final = function () {',
  17873. ' };',
  17874. ' this.CurNow = function () {',
  17875. ' var Result = 0;',
  17876. ' return Result;',
  17877. ' };',
  17878. ' this.Now = function () {',
  17879. ' var Result = 0;',
  17880. ' return Result;',
  17881. ' };',
  17882. '});',
  17883. 'this.Obj = null;',
  17884. 'this.vI = 0;',
  17885. '']),
  17886. LinesToStr([ // $mod.$main
  17887. '$mod.Obj.CurNow();',
  17888. '$mod.vI = $mod.Obj.CurNow();',
  17889. '$mod.TObject.Now();',
  17890. '$mod.vI = $mod.TObject.Now();',
  17891. '']));
  17892. end;
  17893. procedure TTestModule.TestClassOf_Const;
  17894. begin
  17895. StartProgram(false);
  17896. Add([
  17897. 'type',
  17898. ' TObject = class',
  17899. ' end;',
  17900. ' TBird = TObject;',
  17901. ' TBirds = class of TBird;',
  17902. ' TEagles = TBirds;',
  17903. ' THawk = class(TBird);',
  17904. 'const',
  17905. ' Hawk: TEagles = THawk;',
  17906. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  17907. ' TBird,',
  17908. ' THawk',
  17909. ' );',
  17910. 'begin']);
  17911. ConvertProgram;
  17912. CheckSource('TestClassOf_Const',
  17913. LinesToStr([ // statements
  17914. 'rtl.createClass(this, "TObject", null, function () {',
  17915. ' this.$init = function () {',
  17916. ' };',
  17917. ' this.$final = function () {',
  17918. ' };',
  17919. '});',
  17920. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  17921. '});',
  17922. 'this.Hawk = this.THawk;',
  17923. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  17924. '']),
  17925. LinesToStr([ // $mod.$main
  17926. '']));
  17927. end;
  17928. procedure TTestModule.TestNestedClass_Alias;
  17929. begin
  17930. WithTypeInfo:=true;
  17931. StartProgram(false);
  17932. Add([
  17933. 'type',
  17934. ' TObject = class',
  17935. ' type TNested = type longint;',
  17936. ' end;',
  17937. 'type TAlias = type tobject.tnested;',
  17938. 'var i: tobject.tnested = 3;',
  17939. 'var j: TAlias = 4;',
  17940. 'begin',
  17941. ' if typeinfo(TAlias)=nil then ;',
  17942. ' if typeinfo(tobject.tnested)=nil then ;',
  17943. '']);
  17944. ConvertProgram;
  17945. CheckSource('TestNestedClass_Alias',
  17946. LinesToStr([ // statements
  17947. 'rtl.createClass(this, "TObject", null, function () {',
  17948. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  17949. ' this.$init = function () {',
  17950. ' };',
  17951. ' this.$final = function () {',
  17952. ' };',
  17953. '});',
  17954. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  17955. 'this.i = 3;',
  17956. 'this.j = 4;',
  17957. '']),
  17958. LinesToStr([ // $mod.$main
  17959. 'if ($mod.$rtti["TAlias"] === null) ;',
  17960. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  17961. '']));
  17962. end;
  17963. procedure TTestModule.TestNestedClass_Record;
  17964. begin
  17965. WithTypeInfo:=true;
  17966. StartProgram(false);
  17967. Add([
  17968. 'type',
  17969. ' TObject = class',
  17970. ' type TPoint = record',
  17971. ' x,y: byte;',
  17972. ' end;',
  17973. ' procedure DoIt(t: TPoint);',
  17974. ' end;',
  17975. 'procedure tobject.DoIt(t: TPoint);',
  17976. 'var p: TPoint;',
  17977. 'begin',
  17978. ' t.x:=t.y;',
  17979. ' p:=t;',
  17980. 'end;',
  17981. 'var',
  17982. ' p: tobject.tpoint = (x:2; y:4);',
  17983. ' o: TObject;',
  17984. 'begin',
  17985. ' p:=p;',
  17986. ' o.doit(p);',
  17987. '']);
  17988. ConvertProgram;
  17989. CheckSource('TestNestedClass_Record',
  17990. LinesToStr([ // statements
  17991. 'rtl.createClass(this, "TObject", null, function () {',
  17992. ' rtl.recNewT(this, "TPoint", function () {',
  17993. ' this.x = 0;',
  17994. ' this.y = 0;',
  17995. ' this.$eq = function (b) {',
  17996. ' return (this.x === b.x) && (this.y === b.y);',
  17997. ' };',
  17998. ' this.$assign = function (s) {',
  17999. ' this.x = s.x;',
  18000. ' this.y = s.y;',
  18001. ' return this;',
  18002. ' };',
  18003. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  18004. ' $r.addField("x", rtl.byte);',
  18005. ' $r.addField("y", rtl.byte);',
  18006. ' });',
  18007. ' this.$init = function () {',
  18008. ' };',
  18009. ' this.$final = function () {',
  18010. ' };',
  18011. ' this.DoIt = function (t) {',
  18012. ' var p = this.TPoint.$new();',
  18013. ' t.x = t.y;',
  18014. ' p.$assign(t);',
  18015. ' };',
  18016. '});',
  18017. 'this.p = this.TObject.TPoint.$clone({',
  18018. ' x: 2,',
  18019. ' y: 4',
  18020. '});',
  18021. 'this.o = null;',
  18022. '']),
  18023. LinesToStr([ // $mod.$main
  18024. '$mod.p.$assign($mod.p);',
  18025. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  18026. '']));
  18027. end;
  18028. procedure TTestModule.TestNestedClass_Class;
  18029. begin
  18030. StartProgram(false);
  18031. Add([
  18032. 'type',
  18033. ' TObject = class end;',
  18034. ' TBird = class',
  18035. ' type TLeg = class',
  18036. ' FId: longint;',
  18037. ' constructor Create;',
  18038. ' function Create(i: longint): TLeg;',
  18039. ' end;',
  18040. ' function DoIt(b: TBird): Tleg;',
  18041. ' end;',
  18042. 'constructor tbird.tleg.create;',
  18043. 'begin',
  18044. ' FId:=3;',
  18045. 'end;',
  18046. 'function tbird.tleg.Create(i: longint): TLeg;',
  18047. 'begin',
  18048. ' Create;',
  18049. ' Result:=TLeg.Create;',
  18050. ' Result:=TBird.TLeg.Create;',
  18051. ' Result:=Create(3);',
  18052. ' FId:=i;',
  18053. 'end;',
  18054. 'function tbird.DoIt(b: tbird): tleg;',
  18055. 'begin',
  18056. ' Result.Create;',
  18057. ' Result:=TLeg.Create;',
  18058. ' Result:=TBird.TLeg.Create;',
  18059. ' Result:=Result.Create(3);',
  18060. 'end;',
  18061. 'var',
  18062. ' b: Tbird.tleg;',
  18063. 'begin',
  18064. ' b.Create;',
  18065. ' b:=TBird.TLeg.Create;',
  18066. ' b:=b.Create(3);',
  18067. '']);
  18068. ConvertProgram;
  18069. CheckSource('TestNestedClass_Class',
  18070. LinesToStr([ // statements
  18071. 'rtl.createClass(this, "TObject", null, function () {',
  18072. ' this.$init = function () {',
  18073. ' };',
  18074. ' this.$final = function () {',
  18075. ' };',
  18076. '});',
  18077. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18078. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  18079. ' this.$init = function () {',
  18080. ' $mod.TObject.$init.call(this);',
  18081. ' this.FId = 0;',
  18082. ' };',
  18083. ' this.Create = function () {',
  18084. ' this.FId = 3;',
  18085. ' return this;',
  18086. ' };',
  18087. ' this.Create$1 = function (i) {',
  18088. ' var Result = null;',
  18089. ' this.Create();',
  18090. ' Result = $mod.TBird.TLeg.$create("Create");',
  18091. ' Result = $mod.TBird.TLeg.$create("Create");',
  18092. ' Result = this.Create$1(3);',
  18093. ' this.FId = i;',
  18094. ' return Result;',
  18095. ' };',
  18096. ' }, "TBird.TLeg");',
  18097. ' this.DoIt = function (b) {',
  18098. ' var Result = null;',
  18099. ' Result.Create();',
  18100. ' Result = this.TLeg.$create("Create");',
  18101. ' Result = $mod.TBird.TLeg.$create("Create");',
  18102. ' Result = Result.Create$1(3);',
  18103. ' return Result;',
  18104. ' };',
  18105. '});',
  18106. 'this.b = null;',
  18107. '']),
  18108. LinesToStr([ // $mod.$main
  18109. '$mod.b.Create();',
  18110. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  18111. '$mod.b = $mod.b.Create$1(3);',
  18112. '']));
  18113. end;
  18114. procedure TTestModule.TestNestedClass_CallInherited;
  18115. begin
  18116. StartProgram(false);
  18117. Add([
  18118. 'type',
  18119. ' TObject = class end;',
  18120. ' TBird = class',
  18121. ' type',
  18122. ' TWing = class',
  18123. ' function Fly(w: word = 17): word; virtual;',
  18124. ' end;',
  18125. ' end;',
  18126. ' TEagle = class(TBird)',
  18127. ' type',
  18128. ' TEagleWing = class(TWing)',
  18129. ' function Fly(w: word): word; override;',
  18130. ' end;',
  18131. ' end;',
  18132. 'function TBird.TWing.Fly(w: word): word;',
  18133. 'begin',
  18134. 'end;',
  18135. 'function TEagle.TEagleWing.Fly(w: word): word;',
  18136. 'begin',
  18137. ' inherited;',
  18138. ' inherited Fly;',
  18139. ' inherited Fly(3);',
  18140. ' Result:=inherited Fly;',
  18141. ' Result:=inherited Fly(4);',
  18142. 'end;',
  18143. 'begin',
  18144. '']);
  18145. ConvertProgram;
  18146. CheckSource('TestNestedClass_CallInherited',
  18147. LinesToStr([ // statements
  18148. 'rtl.createClass(this, "TObject", null, function () {',
  18149. ' this.$init = function () {',
  18150. ' };',
  18151. ' this.$final = function () {',
  18152. ' };',
  18153. '});',
  18154. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18155. ' rtl.createClass(this, "TWing", $mod.TObject, function () {',
  18156. ' this.Fly = function (w) {',
  18157. ' var Result = 0;',
  18158. ' return Result;',
  18159. ' };',
  18160. ' }, "TBird.TWing");',
  18161. '});',
  18162. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  18163. ' rtl.createClass(this, "TEagleWing", this.TWing, function () {',
  18164. ' this.Fly = function (w) {',
  18165. ' var Result = 0;',
  18166. ' $mod.TBird.TWing.Fly.apply(this, arguments);',
  18167. ' $mod.TBird.TWing.Fly.call(this, 17);',
  18168. ' $mod.TBird.TWing.Fly.call(this, 3);',
  18169. ' Result = $mod.TBird.TWing.Fly.call(this, 17);',
  18170. ' Result = $mod.TBird.TWing.Fly.call(this, 4);',
  18171. ' return Result;',
  18172. ' };',
  18173. ' }, "TEagle.TEagleWing");',
  18174. '});',
  18175. '']),
  18176. LinesToStr([ // $mod.$main
  18177. '']));
  18178. end;
  18179. procedure TTestModule.TestExternalClass_Var;
  18180. begin
  18181. StartProgram(false);
  18182. Add([
  18183. '{$modeswitch externalclass}',
  18184. 'type',
  18185. ' TExtA = class external name ''ExtObj''',
  18186. ' Id: longint external name ''$Id'';',
  18187. ' B: longint;',
  18188. ' end;',
  18189. 'var Obj: TExtA;',
  18190. 'begin',
  18191. ' obj.id:=obj.id+1;',
  18192. ' obj.B:=obj.B+1;']);
  18193. ConvertProgram;
  18194. CheckSource('TestExternalClass_Var',
  18195. LinesToStr([ // statements
  18196. 'this.Obj = null;',
  18197. '']),
  18198. LinesToStr([ // $mod.$main
  18199. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  18200. '$mod.Obj.B = $mod.Obj.B + 1;',
  18201. '']));
  18202. end;
  18203. procedure TTestModule.TestExternalClass_Const;
  18204. begin
  18205. StartProgram(false);
  18206. Add([
  18207. '{$modeswitch externalclass}',
  18208. 'type',
  18209. ' TExtA = class external name ''ExtObj''',
  18210. ' const Two: longint = 2;',
  18211. ' const Three = 3;',
  18212. ' const Id: longint;',
  18213. ' end;',
  18214. ' TExtB = class external name ''ExtB''',
  18215. ' A: TExtA;',
  18216. ' end;',
  18217. 'var',
  18218. ' A: texta;',
  18219. ' B: textb;',
  18220. ' i: longint;',
  18221. 'begin',
  18222. ' i:=a.two;',
  18223. ' i:=texta.two;',
  18224. ' i:=a.three;',
  18225. ' i:=texta.three;',
  18226. ' i:=a.id;',
  18227. ' i:=texta.id;',
  18228. '']);
  18229. ConvertProgram;
  18230. CheckSource('TestExternalClass_Const',
  18231. LinesToStr([ // statements
  18232. 'this.A = null;',
  18233. 'this.B = null;',
  18234. 'this.i = 0;',
  18235. '']),
  18236. LinesToStr([ // $mod.$main
  18237. '$mod.i = 2;',
  18238. '$mod.i = 2;',
  18239. '$mod.i = 3;',
  18240. '$mod.i = 3;',
  18241. '$mod.i = $mod.A.Id;',
  18242. '$mod.i = ExtObj.Id;',
  18243. '']));
  18244. end;
  18245. procedure TTestModule.TestExternalClass_Dollar;
  18246. begin
  18247. StartProgram(false);
  18248. Add([
  18249. '{$modeswitch externalclass}',
  18250. 'type',
  18251. ' TExtA = class external name ''$''',
  18252. ' Id: longint external name ''$'';',
  18253. ' function Bla(i: longint): longint; external name ''$'';',
  18254. ' end;',
  18255. 'function dollar(k: longint): longint; external name ''$'';',
  18256. 'var Obj: TExtA;',
  18257. 'begin',
  18258. ' dollar(1);',
  18259. ' obj.id:=obj.id+2;',
  18260. ' obj.Bla(3);',
  18261. '']);
  18262. ConvertProgram;
  18263. CheckSource('TestExternalClass_Dollar',
  18264. LinesToStr([ // statements
  18265. 'this.Obj = null;',
  18266. '']),
  18267. LinesToStr([ // $mod.$main
  18268. '$(1);',
  18269. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  18270. '$mod.Obj.$(3);',
  18271. '']));
  18272. end;
  18273. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  18274. begin
  18275. StartProgram(false);
  18276. Add('{$modeswitch externalclass}');
  18277. Add('type');
  18278. Add(' TExtA = class external name ''ExtA''');
  18279. Add(' Id: longint external name ''$Id'';');
  18280. Add(' end;');
  18281. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  18282. Add(' Id: longint;');
  18283. Add(' end;');
  18284. Add('begin');
  18285. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  18286. ConvertProgram;
  18287. end;
  18288. procedure TTestModule.TestExternalClass_Method;
  18289. begin
  18290. StartProgram(false);
  18291. Add(['{$modeswitch externalclass}',
  18292. 'type',
  18293. ' TExtA = class external name ''ExtObj''',
  18294. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18295. ' procedure DoSome(Id: longint = 1);',
  18296. ' end;',
  18297. 'var Obj: texta;',
  18298. 'begin',
  18299. ' obj.doit;',
  18300. ' obj.doit();',
  18301. ' obj.doit(2);',
  18302. ' with obj do begin',
  18303. ' doit;',
  18304. ' doit();',
  18305. ' doit(3);',
  18306. ' end;']);
  18307. ConvertProgram;
  18308. CheckSource('TestExternalClass_Method',
  18309. LinesToStr([ // statements
  18310. 'this.Obj = null;',
  18311. '']),
  18312. LinesToStr([ // $mod.$main
  18313. '$mod.Obj.$Execute(1);',
  18314. '$mod.Obj.$Execute(1);',
  18315. '$mod.Obj.$Execute(2);',
  18316. 'var $with = $mod.Obj;',
  18317. '$with.$Execute(1);',
  18318. '$with.$Execute(1);',
  18319. '$with.$Execute(3);',
  18320. '']));
  18321. end;
  18322. procedure TTestModule.TestExternalClass_ClassMethod;
  18323. begin
  18324. StartProgram(false);
  18325. Add([
  18326. '{$modeswitch externalclass}',
  18327. 'type',
  18328. ' TExtA = class external name ''ExtObj''',
  18329. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  18330. ' end;',
  18331. ' TExtB = TExtA;',
  18332. 'var p: Pointer;',
  18333. 'begin',
  18334. ' texta.doit;',
  18335. ' texta.doit();',
  18336. ' texta.doit(2);',
  18337. ' p:[email protected];',
  18338. ' with texta do begin',
  18339. ' doit;',
  18340. ' doit();',
  18341. ' doit(3);',
  18342. ' p:=@DoIt;',
  18343. ' end;',
  18344. ' textb.doit;',
  18345. ' textb.doit();',
  18346. ' textb.doit(4);',
  18347. ' with textb do begin',
  18348. ' doit;',
  18349. ' doit();',
  18350. ' doit(5);',
  18351. ' end;',
  18352. '']);
  18353. ConvertProgram;
  18354. CheckSource('TestExternalClass_ClassMethod',
  18355. LinesToStr([ // statements
  18356. 'this.p = null;',
  18357. '']),
  18358. LinesToStr([ // $mod.$main
  18359. 'ExtObj.$Execute(1);',
  18360. 'ExtObj.$Execute(1);',
  18361. 'ExtObj.$Execute(2);',
  18362. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18363. 'ExtObj.$Execute(1);',
  18364. 'ExtObj.$Execute(1);',
  18365. 'ExtObj.$Execute(3);',
  18366. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  18367. 'ExtObj.$Execute(1);',
  18368. 'ExtObj.$Execute(1);',
  18369. 'ExtObj.$Execute(4);',
  18370. 'ExtObj.$Execute(1);',
  18371. 'ExtObj.$Execute(1);',
  18372. 'ExtObj.$Execute(5);',
  18373. '']));
  18374. end;
  18375. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  18376. begin
  18377. StartProgram(false);
  18378. Add([
  18379. '{$modeswitch externalclass}',
  18380. 'type',
  18381. ' TExtA = class external name ''ExtObj''',
  18382. ' class procedure DoIt(Id: longint = 1); static;',
  18383. ' end;',
  18384. 'var p: Pointer;',
  18385. 'begin',
  18386. ' texta.doit;',
  18387. ' texta.doit();',
  18388. ' texta.doit(2);',
  18389. ' p:[email protected];',
  18390. ' with texta do begin',
  18391. ' doit;',
  18392. ' doit();',
  18393. ' doit(3);',
  18394. ' p:=@DoIt;',
  18395. ' end;',
  18396. '']);
  18397. ConvertProgram;
  18398. CheckSource('TestExternalClass_ClassMethodStatic',
  18399. LinesToStr([ // statements
  18400. 'this.p = null;',
  18401. '']),
  18402. LinesToStr([ // $mod.$main
  18403. 'ExtObj.DoIt(1);',
  18404. 'ExtObj.DoIt(1);',
  18405. 'ExtObj.DoIt(2);',
  18406. '$mod.p = ExtObj.DoIt;',
  18407. 'ExtObj.DoIt(1);',
  18408. 'ExtObj.DoIt(1);',
  18409. 'ExtObj.DoIt(3);',
  18410. '$mod.p = ExtObj.DoIt;',
  18411. '']));
  18412. end;
  18413. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  18414. begin
  18415. StartProgram(false);
  18416. Add([
  18417. '{$modeswitch externalclass}',
  18418. 'type',
  18419. ' TBird = class external name ''Array''',
  18420. ' end;',
  18421. 'function GetPtr: Pointer;',
  18422. 'begin',
  18423. 'end;',
  18424. 'procedure Write(const p);',
  18425. 'begin',
  18426. 'end;',
  18427. 'procedure WriteLn; varargs;',
  18428. 'begin',
  18429. 'end;',
  18430. 'begin',
  18431. ' if TBird(GetPtr)=nil then ;',
  18432. ' Write(GetPtr);',
  18433. ' WriteLn(GetPtr);',
  18434. ' Write(TBird(GetPtr));',
  18435. ' WriteLn(TBird(GetPtr));',
  18436. '']);
  18437. ConvertProgram;
  18438. CheckSource('TestFunctionResultInTypeCast',
  18439. LinesToStr([ // statements
  18440. 'this.GetPtr = function () {',
  18441. ' var Result = null;',
  18442. ' return Result;',
  18443. '};',
  18444. 'this.Write = function (p) {',
  18445. '};',
  18446. 'this.WriteLn = function () {',
  18447. '};',
  18448. '']),
  18449. LinesToStr([
  18450. 'if ($mod.GetPtr() === null) ;',
  18451. '$mod.Write($mod.GetPtr());',
  18452. '$mod.WriteLn($mod.GetPtr());',
  18453. '$mod.Write($mod.GetPtr());',
  18454. '$mod.WriteLn($mod.GetPtr());',
  18455. '']));
  18456. end;
  18457. procedure TTestModule.TestExternalClass_NonExternalOverride;
  18458. begin
  18459. StartProgram(false);
  18460. Add([
  18461. '{$modeswitch externalclass}',
  18462. 'type',
  18463. ' TExtA = class external name ''ExtObjA''',
  18464. ' procedure ProcA; virtual;',
  18465. ' procedure ProcB; virtual;',
  18466. ' end;',
  18467. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  18468. ' end;',
  18469. ' TExtC = class (TExtB)',
  18470. ' procedure ProcA; override;',
  18471. ' end;',
  18472. 'procedure TExtC.ProcA;',
  18473. 'begin',
  18474. ' ProcA;',
  18475. ' Self.ProcA;',
  18476. ' ProcB;',
  18477. ' Self.ProcB;',
  18478. 'end;',
  18479. 'var',
  18480. ' A: texta;',
  18481. ' B: textb;',
  18482. ' C: textc;',
  18483. 'begin',
  18484. ' a.proca;',
  18485. ' b.proca;',
  18486. ' c.proca;']);
  18487. ConvertProgram;
  18488. CheckSource('TestExternalClass_NonExternalOverride',
  18489. LinesToStr([ // statements
  18490. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  18491. ' this.$init = function () {',
  18492. ' };',
  18493. ' this.$final = function () {',
  18494. ' };',
  18495. ' this.ProcA = function () {',
  18496. ' this.ProcA();',
  18497. ' this.ProcA();',
  18498. ' this.ProcB();',
  18499. ' this.ProcB();',
  18500. ' };',
  18501. '});',
  18502. 'this.A = null;',
  18503. 'this.B = null;',
  18504. 'this.C = null;',
  18505. '']),
  18506. LinesToStr([ // $mod.$main
  18507. '$mod.A.ProcA();',
  18508. '$mod.B.ProcA();',
  18509. '$mod.C.ProcA();',
  18510. '']));
  18511. end;
  18512. procedure TTestModule.TestExternalClass_OverloadHint;
  18513. begin
  18514. StartProgram(false);
  18515. Add([
  18516. '{$modeswitch externalclass}',
  18517. 'type',
  18518. ' TExtA = class external name ''ExtObjA''',
  18519. ' procedure DoIt;',
  18520. ' procedure DoIt(i: longint);',
  18521. ' end;',
  18522. 'begin',
  18523. '']);
  18524. ConvertProgram;
  18525. CheckResolverUnexpectedHints(true);
  18526. CheckSource('TestExternalClass_OverloadHint',
  18527. LinesToStr([ // statements
  18528. '']),
  18529. LinesToStr([ // $mod.$main
  18530. '']));
  18531. end;
  18532. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  18533. begin
  18534. WithTypeInfo:=true;
  18535. StartProgram(false);
  18536. Add([
  18537. '{$modeswitch externalclass}',
  18538. 'type',
  18539. ' JSwiper = class external name ''Swiper''',
  18540. ' constructor New;',
  18541. ' end;',
  18542. ' TObject = class',
  18543. ' private',
  18544. ' FSwiper: JSwiper;',
  18545. ' published',
  18546. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  18547. ' end;',
  18548. 'begin',
  18549. ' JSwiper.new;',
  18550. '']);
  18551. ConvertProgram;
  18552. CheckSource('TestExternalClass_SameNamePublishedProperty',
  18553. LinesToStr([ // statements
  18554. 'this.$rtti.$ExtClass("JSwiper", {',
  18555. ' jsclass: "Swiper"',
  18556. '});',
  18557. 'rtl.createClass(this, "TObject", null, function () {',
  18558. ' this.$init = function () {',
  18559. ' this.FSwiper = null;',
  18560. ' };',
  18561. ' this.$final = function () {',
  18562. ' this.FSwiper = undefined;',
  18563. ' };',
  18564. ' var $r = this.$rtti;',
  18565. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  18566. '});',
  18567. '']),
  18568. LinesToStr([ // $mod.$main
  18569. 'new Swiper();',
  18570. '']));
  18571. end;
  18572. procedure TTestModule.TestExternalClass_Property;
  18573. begin
  18574. StartProgram(false);
  18575. Add([
  18576. '{$modeswitch externalclass}',
  18577. 'type',
  18578. ' TExtA = class external name ''ExtA''',
  18579. ' function getYear: longint;',
  18580. ' procedure setYear(Value: longint);',
  18581. ' property Year: longint read getyear write setyear;',
  18582. ' end;',
  18583. ' TExtB = class (TExtA)',
  18584. ' procedure OtherSetYear(Value: longint);',
  18585. ' property year write othersetyear;',
  18586. ' end;',
  18587. 'procedure textb.othersetyear(value: longint);',
  18588. 'begin',
  18589. ' setYear(Value+4);',
  18590. 'end;',
  18591. 'var',
  18592. ' A: texta;',
  18593. ' B: textb;',
  18594. 'begin',
  18595. ' a.year:=a.year+1;',
  18596. ' b.year:=b.year+2;']);
  18597. ConvertProgram;
  18598. CheckSource('TestExternalClass_NonExternalOverride',
  18599. LinesToStr([ // statements
  18600. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18601. ' this.$init = function () {',
  18602. ' };',
  18603. ' this.$final = function () {',
  18604. ' };',
  18605. ' this.OtherSetYear = function (Value) {',
  18606. ' this.setYear(Value+4);',
  18607. ' };',
  18608. '});',
  18609. 'this.A = null;',
  18610. 'this.B = null;',
  18611. '']),
  18612. LinesToStr([ // $mod.$main
  18613. '$mod.A.setYear($mod.A.getYear()+1);',
  18614. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  18615. '']));
  18616. end;
  18617. procedure TTestModule.TestExternalClass_PropertyDate;
  18618. begin
  18619. StartProgram(false);
  18620. Add([
  18621. '{$modeswitch externalclass}',
  18622. 'type',
  18623. ' TExtA = class external name ''ExtA''',
  18624. ' end;',
  18625. ' TExtB = class (TExtA)',
  18626. ' FDate: string;',
  18627. ' property Date: string read FDate write FDate;',
  18628. ' property ExtA: string read FDate write FDate;',
  18629. ' end;',
  18630. ' {$M+}',
  18631. ' TObject = class',
  18632. ' FDate: string;',
  18633. ' published',
  18634. ' property Date: string read FDate write FDate;',
  18635. ' property ExtA: string read FDate write FDate;',
  18636. ' end;',
  18637. 'var',
  18638. ' B: textb;',
  18639. ' o: TObject;',
  18640. 'begin',
  18641. ' b.date:=b.exta;',
  18642. ' o.date:=o.exta;']);
  18643. ConvertProgram;
  18644. CheckSource('TestExternalClass_PropertyDate',
  18645. LinesToStr([ // statements
  18646. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18647. ' this.$init = function () {',
  18648. ' this.FDate = "";',
  18649. ' };',
  18650. ' this.$final = function () {',
  18651. ' };',
  18652. '});',
  18653. 'rtl.createClass(this, "TObject", null, function () {',
  18654. ' this.$init = function () {',
  18655. ' this.FDate = "";',
  18656. ' };',
  18657. ' this.$final = function () {',
  18658. ' };',
  18659. ' var $r = this.$rtti;',
  18660. ' $r.addField("FDate", rtl.string, 4);',
  18661. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  18662. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  18663. '});',
  18664. 'this.B = null;',
  18665. 'this.o = null;',
  18666. '']),
  18667. LinesToStr([ // $mod.$main
  18668. '$mod.B.FDate = $mod.B.FDate;',
  18669. '$mod.o.FDate = $mod.o.FDate;',
  18670. '']));
  18671. end;
  18672. procedure TTestModule.TestExternalClass_ClassProperty;
  18673. begin
  18674. StartProgram(false);
  18675. Add('{$modeswitch externalclass}');
  18676. Add('type');
  18677. Add(' TExtA = class external name ''ExtA''');
  18678. Add(' class function getYear: longint;');
  18679. Add(' class procedure setYear(Value: longint);');
  18680. Add(' class property Year: longint read getyear write setyear;');
  18681. Add(' end;');
  18682. Add(' TExtB = class (TExtA)');
  18683. Add(' class function GetCentury: longint;');
  18684. Add(' class procedure SetCentury(Value: longint);');
  18685. Add(' class property Century: longint read getcentury write setcentury;');
  18686. Add(' end;');
  18687. Add('class function textb.getcentury: longint;');
  18688. Add('begin');
  18689. Add('end;');
  18690. Add('class procedure textb.setcentury(value: longint);');
  18691. Add('begin');
  18692. Add(' setyear(value+11);');
  18693. Add(' texta.year:=texta.year+12;');
  18694. Add(' year:=year+13;');
  18695. Add(' textb.century:=textb.century+14;');
  18696. Add(' century:=century+15;');
  18697. Add('end;');
  18698. Add('var');
  18699. Add(' A: texta;');
  18700. Add(' B: textb;');
  18701. Add('begin');
  18702. Add(' texta.year:=texta.year+1;');
  18703. Add(' textb.year:=textb.year+2;');
  18704. Add(' TextA.year:=TextA.year+3;');
  18705. Add(' b.year:=b.year+4;');
  18706. Add(' textb.century:=textb.century+5;');
  18707. Add(' b.century:=b.century+6;');
  18708. ConvertProgram;
  18709. CheckSource('TestExternalClass_ClassProperty',
  18710. LinesToStr([ // statements
  18711. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  18712. ' this.$init = function () {',
  18713. ' };',
  18714. ' this.$final = function () {',
  18715. ' };',
  18716. ' this.GetCentury = function () {',
  18717. ' var Result = 0;',
  18718. ' return Result;',
  18719. ' };',
  18720. ' this.SetCentury = function (Value) {',
  18721. ' this.setYear(Value + 11);',
  18722. ' ExtA.setYear(ExtA.getYear() + 12);',
  18723. ' this.setYear(this.getYear() + 13);',
  18724. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  18725. ' this.SetCentury(this.GetCentury() + 15);',
  18726. ' };',
  18727. '});',
  18728. 'this.A = null;',
  18729. 'this.B = null;',
  18730. '']),
  18731. LinesToStr([ // $mod.$main
  18732. 'ExtA.setYear(ExtA.getYear() + 1);',
  18733. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  18734. 'ExtA.setYear(ExtA.getYear() + 3);',
  18735. '$mod.B.setYear($mod.B.getYear() + 4);',
  18736. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  18737. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  18738. '']));
  18739. end;
  18740. procedure TTestModule.TestExternalClass_ClassOf;
  18741. begin
  18742. StartProgram(false);
  18743. Add('{$modeswitch externalclass}');
  18744. Add('type');
  18745. Add(' TExtA = class external name ''ExtA''');
  18746. Add(' procedure ProcA; virtual;');
  18747. Add(' procedure ProcB; virtual;');
  18748. Add(' end;');
  18749. Add(' TExtAClass = class of TExtA;');
  18750. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18751. Add(' end;');
  18752. Add(' TExtBClass = class of TExtB;');
  18753. Add(' TExtC = class (TExtB)');
  18754. Add(' procedure ProcA; override;');
  18755. Add(' end;');
  18756. Add(' TExtCClass = class of TExtC;');
  18757. Add('procedure TExtC.ProcA; begin end;');
  18758. Add('var');
  18759. Add(' A: texta; ClA: TExtAClass;');
  18760. Add(' B: textb; ClB: TExtBClass;');
  18761. Add(' C: textc; ClC: TExtCClass;');
  18762. Add('begin');
  18763. Add(' ClA:=texta;');
  18764. Add(' ClA:=textb;');
  18765. Add(' ClA:=textc;');
  18766. Add(' ClB:=textb;');
  18767. Add(' ClB:=textc;');
  18768. Add(' ClC:=textc;');
  18769. ConvertProgram;
  18770. CheckSource('TestExternalClass_ClassOf',
  18771. LinesToStr([ // statements
  18772. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18773. ' this.$init = function () {',
  18774. ' };',
  18775. ' this.$final = function () {',
  18776. ' };',
  18777. ' this.ProcA = function () {',
  18778. ' };',
  18779. '});',
  18780. 'this.A = null;',
  18781. 'this.ClA = null;',
  18782. 'this.B = null;',
  18783. 'this.ClB = null;',
  18784. 'this.C = null;',
  18785. 'this.ClC = null;',
  18786. '']),
  18787. LinesToStr([ // $mod.$main
  18788. '$mod.ClA = ExtA;',
  18789. '$mod.ClA = ExtB;',
  18790. '$mod.ClA = $mod.TExtC;',
  18791. '$mod.ClB = ExtB;',
  18792. '$mod.ClB = $mod.TExtC;',
  18793. '$mod.ClC = $mod.TExtC;',
  18794. '']));
  18795. end;
  18796. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  18797. begin
  18798. AddModuleWithIntfImplSrc('unit2.pas',
  18799. LinesToStr([
  18800. '{$modeswitch externalclass}',
  18801. 'type',
  18802. ' TExtA = class external name ''ExtA''',
  18803. ' class var Id: longint;',
  18804. ' end;',
  18805. '']),
  18806. '');
  18807. StartUnit(true);
  18808. Add('interface');
  18809. Add('uses unit2;');
  18810. Add('implementation');
  18811. Add('begin');
  18812. Add(' unit2.texta.id:=unit2.texta.id+1;');
  18813. ConvertUnit;
  18814. CheckSource('TestExternalClass_ClassOtherUnit',
  18815. LinesToStr([
  18816. '']),
  18817. LinesToStr([
  18818. 'ExtA.Id = ExtA.Id + 1;',
  18819. '']));
  18820. end;
  18821. procedure TTestModule.TestExternalClass_Is;
  18822. begin
  18823. StartProgram(false);
  18824. Add([
  18825. '{$modeswitch externalclass}',
  18826. 'type',
  18827. ' TExtA = class external name ''ExtA''',
  18828. ' end;',
  18829. ' TExtAClass = class of TExtA;',
  18830. ' TExtB = class external name ''ExtB'' (TExtA)',
  18831. ' end;',
  18832. ' TExtBClass = class of TExtB;',
  18833. ' TExtC = class (TExtB)',
  18834. ' end;',
  18835. ' TExtCClass = class of TExtC;',
  18836. 'var',
  18837. ' A: texta; ClA: TExtAClass;',
  18838. ' B: textb; ClB: TExtBClass;',
  18839. ' C: textc; ClC: TExtCClass;',
  18840. 'begin',
  18841. ' if a is textb then ;',
  18842. ' if a is textc then ;',
  18843. ' if b is textc then ;',
  18844. ' if cla is textb then ;',
  18845. ' if cla is textc then ;',
  18846. ' if clb is textc then ;',
  18847. ' try',
  18848. ' except',
  18849. ' on TExtA do ;',
  18850. ' on e: TExtB do ;',
  18851. ' end;',
  18852. '']);
  18853. ConvertProgram;
  18854. CheckSource('TestExternalClass_Is',
  18855. LinesToStr([ // statements
  18856. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18857. ' this.$init = function () {',
  18858. ' };',
  18859. ' this.$final = function () {',
  18860. ' };',
  18861. '});',
  18862. 'this.A = null;',
  18863. 'this.ClA = null;',
  18864. 'this.B = null;',
  18865. 'this.ClB = null;',
  18866. 'this.C = null;',
  18867. 'this.ClC = null;',
  18868. '']),
  18869. LinesToStr([ // $mod.$main
  18870. 'if (rtl.isExt($mod.A, ExtB)) ;',
  18871. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  18872. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  18873. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  18874. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  18875. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  18876. 'try {} catch ($e) {',
  18877. ' if (rtl.isExt($e,ExtA)) {}',
  18878. ' else if (rtl.isExt($e,ExtB)) {',
  18879. ' var e = $e;',
  18880. ' } else throw $e',
  18881. '};',
  18882. '']));
  18883. end;
  18884. procedure TTestModule.TestExternalClass_As;
  18885. begin
  18886. StartProgram(false);
  18887. Add('{$modeswitch externalclass}');
  18888. Add('type');
  18889. Add(' TExtA = class external name ''ExtA''');
  18890. Add(' end;');
  18891. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  18892. Add(' end;');
  18893. Add(' TExtC = class (TExtB)');
  18894. Add(' end;');
  18895. Add('var');
  18896. Add(' A: texta;');
  18897. Add(' B: textb;');
  18898. Add(' C: textc;');
  18899. Add('begin');
  18900. Add(' b:=a as textb;');
  18901. Add(' c:=a as textc;');
  18902. Add(' c:=b as textc;');
  18903. ConvertProgram;
  18904. CheckSource('TestExternalClass_Is',
  18905. LinesToStr([ // statements
  18906. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  18907. ' this.$init = function () {',
  18908. ' };',
  18909. ' this.$final = function () {',
  18910. ' };',
  18911. '});',
  18912. 'this.A = null;',
  18913. 'this.B = null;',
  18914. 'this.C = null;',
  18915. '']),
  18916. LinesToStr([ // $mod.$main
  18917. '$mod.B = rtl.asExt($mod.A, ExtB);',
  18918. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  18919. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  18920. '']));
  18921. end;
  18922. procedure TTestModule.TestExternalClass_DestructorFail;
  18923. begin
  18924. StartProgram(false);
  18925. Add('{$modeswitch externalclass}');
  18926. Add('type');
  18927. Add(' TExtA = class external name ''ExtA''');
  18928. Add(' destructor Free;');
  18929. Add(' end;');
  18930. SetExpectedPasResolverError('Pascal element not supported: destructor',
  18931. nPasElementNotSupported);
  18932. ConvertProgram;
  18933. end;
  18934. procedure TTestModule.TestExternalClass_New;
  18935. begin
  18936. StartProgram(false);
  18937. Add([
  18938. '{$modeswitch externalclass}',
  18939. 'type',
  18940. ' TExtA = class external name ''ExtA''',
  18941. ' constructor New;',
  18942. ' constructor New(i: longint; j: longint = 2);',
  18943. ' end;',
  18944. 'var',
  18945. ' A: texta;',
  18946. 'begin',
  18947. ' a:=texta.new;',
  18948. ' a:=texta(texta.new);',
  18949. ' a:=texta.new();',
  18950. ' a:=texta.new(1);',
  18951. ' with texta do begin',
  18952. ' a:=new;',
  18953. ' a:=new();',
  18954. ' a:=new(2);',
  18955. ' end;',
  18956. ' a:=test1.texta.new;',
  18957. ' a:=test1.texta.new();',
  18958. ' a:=test1.texta.new(3);',
  18959. '']);
  18960. ConvertProgram;
  18961. CheckSource('TestExternalClass_New',
  18962. LinesToStr([ // statements
  18963. 'this.A = null;',
  18964. '']),
  18965. LinesToStr([ // $mod.$main
  18966. '$mod.A = new ExtA();',
  18967. '$mod.A = new ExtA();',
  18968. '$mod.A = new ExtA();',
  18969. '$mod.A = new ExtA(1,2);',
  18970. '$mod.A = new ExtA();',
  18971. '$mod.A = new ExtA();',
  18972. '$mod.A = new ExtA(2,2);',
  18973. '$mod.A = new ExtA();',
  18974. '$mod.A = new ExtA();',
  18975. '$mod.A = new ExtA(3,2);',
  18976. '']));
  18977. end;
  18978. procedure TTestModule.TestExternalClass_ClassOf_New;
  18979. begin
  18980. StartProgram(false);
  18981. Add('{$modeswitch externalclass}');
  18982. Add('type');
  18983. Add(' TExtAClass = class of TExtA;');
  18984. Add(' TExtA = class external name ''ExtA''');
  18985. Add(' C: TExtAClass;');
  18986. Add(' constructor New;');
  18987. Add(' end;');
  18988. Add('var');
  18989. Add(' A: texta;');
  18990. Add(' C: textaclass;');
  18991. Add('begin');
  18992. Add(' a:=c.new;');
  18993. Add(' a:=c.new();');
  18994. Add(' with C do begin');
  18995. Add(' a:=new;');
  18996. Add(' a:=new();');
  18997. Add(' end;');
  18998. Add(' a:=test1.c.new;');
  18999. Add(' a:=test1.c.new();');
  19000. Add(' a:=A.c.new();');
  19001. ConvertProgram;
  19002. CheckSource('TestExternalClass_ClassOf_New',
  19003. LinesToStr([ // statements
  19004. 'this.A = null;',
  19005. 'this.C = null;',
  19006. '']),
  19007. LinesToStr([ // $mod.$main
  19008. '$mod.A = new $mod.C();',
  19009. '$mod.A = new $mod.C();',
  19010. 'var $with = $mod.C;',
  19011. '$mod.A = new $with();',
  19012. '$mod.A = new $with();',
  19013. '$mod.A = new $mod.C();',
  19014. '$mod.A = new $mod.C();',
  19015. '$mod.A = new $mod.A.C();',
  19016. '']));
  19017. end;
  19018. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  19019. begin
  19020. StartProgram(false);
  19021. Add([
  19022. '{$modeswitch externalclass}',
  19023. 'type',
  19024. ' TExtAClass = class of TExtA;',
  19025. ' TExtA = class external name ''ExtA''',
  19026. ' constructor New;',
  19027. ' end;',
  19028. 'function GetCreator: TExtAClass;',
  19029. 'begin',
  19030. ' Result:=TExtA;',
  19031. 'end;',
  19032. 'var',
  19033. ' A: texta;',
  19034. 'begin',
  19035. ' a:=getcreator.new;',
  19036. ' a:=getcreator().new;',
  19037. ' a:=getcreator().new();',
  19038. ' a:=getcreator.new();',
  19039. ' with getcreator do begin',
  19040. ' a:=new;',
  19041. ' a:=new();',
  19042. ' end;']);
  19043. ConvertProgram;
  19044. CheckSource('TestExternalClass_FuncClassOf_New',
  19045. LinesToStr([ // statements
  19046. 'this.GetCreator = function () {',
  19047. ' var Result = null;',
  19048. ' Result = ExtA;',
  19049. ' return Result;',
  19050. '};',
  19051. 'this.A = null;',
  19052. '']),
  19053. LinesToStr([ // $mod.$main
  19054. '$mod.A = new ($mod.GetCreator())();',
  19055. '$mod.A = new ($mod.GetCreator())();',
  19056. '$mod.A = new ($mod.GetCreator())();',
  19057. '$mod.A = new ($mod.GetCreator())();',
  19058. 'var $with = $mod.GetCreator();',
  19059. '$mod.A = new $with();',
  19060. '$mod.A = new $with();',
  19061. '']));
  19062. end;
  19063. procedure TTestModule.TestExternalClass_New_PasClassFail;
  19064. begin
  19065. StartProgram(false);
  19066. Add([
  19067. '{$modeswitch externalclass}',
  19068. 'type',
  19069. ' TExtA = class external name ''ExtA''',
  19070. ' constructor New;',
  19071. ' end;',
  19072. ' TBird = class(TExtA)',
  19073. ' end;',
  19074. 'begin',
  19075. ' TBird.new;',
  19076. '']);
  19077. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19078. ConvertProgram;
  19079. end;
  19080. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  19081. begin
  19082. StartProgram(false);
  19083. Add([
  19084. '{$modeswitch externalclass}',
  19085. 'type',
  19086. ' TExtA = class external name ''ExtA''',
  19087. ' constructor New;',
  19088. ' end;',
  19089. ' TBird = class(TExtA)',
  19090. ' end;',
  19091. 'begin',
  19092. ' TBird.new();',
  19093. '']);
  19094. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  19095. ConvertProgram;
  19096. end;
  19097. procedure TTestModule.TestExternalClass_NewExtName;
  19098. begin
  19099. StartProgram(false);
  19100. Add([
  19101. '{$modeswitch externalclass}',
  19102. 'type',
  19103. ' TExtA = class external name ''ExtA''',
  19104. ' constructor New; external name ''Other'';',
  19105. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  19106. ' end;',
  19107. 'var',
  19108. ' A: texta;',
  19109. 'begin',
  19110. ' a:=texta.new;',
  19111. ' a:=texta(texta.new);',
  19112. ' a:=texta.new();',
  19113. ' a:=texta.new(1);',
  19114. ' with texta do begin',
  19115. ' a:=new;',
  19116. ' a:=new();',
  19117. ' a:=new(2);',
  19118. ' end;',
  19119. ' a:=test1.texta.new;',
  19120. ' a:=test1.texta.new();',
  19121. ' a:=test1.texta.new(3);',
  19122. '']);
  19123. ConvertProgram;
  19124. CheckSource('TestExternalClass_NewExtName',
  19125. LinesToStr([ // statements
  19126. 'this.A = null;',
  19127. '']),
  19128. LinesToStr([ // $mod.$main
  19129. '$mod.A = new Other();',
  19130. '$mod.A = new Other();',
  19131. '$mod.A = new Other();',
  19132. '$mod.A = new A.B(1,2);',
  19133. '$mod.A = new Other();',
  19134. '$mod.A = new Other();',
  19135. '$mod.A = new A.B(2,2);',
  19136. '$mod.A = new Other();',
  19137. '$mod.A = new Other();',
  19138. '$mod.A = new A.B(3,2);',
  19139. '']));
  19140. end;
  19141. procedure TTestModule.TestExternalClass_Constructor;
  19142. begin
  19143. StartProgram(false);
  19144. Add([
  19145. '{$modeswitch externalclass}',
  19146. 'type',
  19147. ' TExtA = class external name ''ExtA''',
  19148. ' public type',
  19149. ' TExtB = class external name ''ExtB''',
  19150. ' public type',
  19151. ' TExtC = class external name ''ExtC''',
  19152. ' constructor New;',
  19153. ' constructor New(i: word);',
  19154. ' end;',
  19155. ' end;',
  19156. ' constructor Create;',
  19157. ' constructor Create(i: longint; j: longint = 2);',
  19158. ' end;',
  19159. 'var',
  19160. ' A: texta;',
  19161. ' C: texta.textb.textc;',
  19162. 'begin',
  19163. ' a:=texta.create;',
  19164. ' a:=texta(texta.create);',
  19165. ' a:=texta.create();',
  19166. ' a:=texta.create(1);',
  19167. ' with texta do begin',
  19168. ' a:=create;',
  19169. ' a:=create();',
  19170. ' a:=create(2);',
  19171. ' end;',
  19172. ' a:=test1.texta.create;',
  19173. ' a:=test1.texta.create();',
  19174. ' a:=test1.texta.create(3);',
  19175. ' c:=texta.textb.textc.new;',
  19176. ' c:=texta.textb.textc.new();',
  19177. ' c:=texta.textb.textc.new(4);',
  19178. '']);
  19179. ConvertProgram;
  19180. CheckSource('TestExternalClass_Constructor',
  19181. LinesToStr([ // statements
  19182. 'this.A = null;',
  19183. 'this.C = null;',
  19184. '']),
  19185. LinesToStr([ // $mod.$main
  19186. '$mod.A = new ExtA.Create();',
  19187. '$mod.A = new ExtA.Create();',
  19188. '$mod.A = new ExtA.Create();',
  19189. '$mod.A = new ExtA.Create(1,2);',
  19190. '$mod.A = new ExtA.Create();',
  19191. '$mod.A = new ExtA.Create();',
  19192. '$mod.A = new ExtA.Create(2,2);',
  19193. '$mod.A = new ExtA.Create();',
  19194. '$mod.A = new ExtA.Create();',
  19195. '$mod.A = new ExtA.Create(3,2);',
  19196. '$mod.C = new ExtA.ExtB.ExtC();',
  19197. '$mod.C = new ExtA.ExtB.ExtC();',
  19198. '$mod.C = new ExtA.ExtB.ExtC(4);',
  19199. '']));
  19200. end;
  19201. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  19202. begin
  19203. StartProgram(false);
  19204. Add([
  19205. '{$modeswitch externalclass}',
  19206. 'type',
  19207. ' TExtA = class external name ''ExtA''',
  19208. ' constructor Create; external name ''{}'';',
  19209. ' end;',
  19210. 'var',
  19211. ' A: texta;',
  19212. 'begin',
  19213. ' a:=texta.create;',
  19214. ' a:=texta(texta.create);',
  19215. ' a:=texta.create();',
  19216. ' with texta do begin',
  19217. ' a:=create;',
  19218. ' a:=create();',
  19219. ' end;',
  19220. ' a:=test1.texta.create;',
  19221. ' a:=test1.texta.create();',
  19222. '']);
  19223. ConvertProgram;
  19224. CheckSource('TestExternalClass_ConstructorBrackets',
  19225. LinesToStr([ // statements
  19226. 'this.A = null;',
  19227. '']),
  19228. LinesToStr([ // $mod.$main
  19229. '$mod.A = {};',
  19230. '$mod.A = {};',
  19231. '$mod.A = {};',
  19232. '$mod.A = {};',
  19233. '$mod.A = {};',
  19234. '$mod.A = {};',
  19235. '$mod.A = {};',
  19236. '']));
  19237. end;
  19238. procedure TTestModule.TestExternalClass_LocalConstSameName;
  19239. begin
  19240. StartProgram(false);
  19241. Add('{$modeswitch externalclass}');
  19242. Add('type');
  19243. Add(' TExtA = class external name ''ExtA''');
  19244. Add(' constructor New;');
  19245. Add(' end;');
  19246. Add('function DoIt: longint;');
  19247. Add('const ExtA: longint = 3;');
  19248. Add('begin');
  19249. Add(' Result:=ExtA;');
  19250. Add('end;');
  19251. Add('var');
  19252. Add(' A: texta;');
  19253. Add('begin');
  19254. Add(' a:=texta.new;');
  19255. ConvertProgram;
  19256. CheckSource('TestExternalClass_LocalConstSameName',
  19257. LinesToStr([ // statements
  19258. 'var ExtA$1 = 3;',
  19259. 'this.DoIt = function () {',
  19260. ' var Result = 0;',
  19261. ' Result = ExtA$1;',
  19262. ' return Result;',
  19263. '};',
  19264. 'this.A = null;',
  19265. '']),
  19266. LinesToStr([ // $mod.$main
  19267. '$mod.A = new ExtA();',
  19268. '']));
  19269. end;
  19270. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  19271. begin
  19272. StartProgram(false);
  19273. Add('{$modeswitch externalclass}');
  19274. Add('type');
  19275. Add(' TExtA = class external name ''ExtA''');
  19276. Add(' procedure DoIt;');
  19277. Add(' end;');
  19278. Add(' TMyA = class(TExtA)');
  19279. Add(' procedure DoIt;');
  19280. Add(' end;');
  19281. Add('procedure TMyA.DoIt; begin end;');
  19282. Add('begin');
  19283. ConvertProgram;
  19284. CheckSource('TestExternalClass_ReintroduceOverload',
  19285. LinesToStr([ // statements
  19286. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  19287. ' this.$init = function () {',
  19288. ' };',
  19289. ' this.$final = function () {',
  19290. ' };',
  19291. ' this.DoIt$1 = function () {',
  19292. ' };',
  19293. '});',
  19294. '']),
  19295. LinesToStr([ // $mod.$main
  19296. '']));
  19297. end;
  19298. procedure TTestModule.TestExternalClass_Inherited;
  19299. begin
  19300. StartProgram(false);
  19301. Add('{$modeswitch externalclass}');
  19302. Add('type');
  19303. Add(' TExtA = class external name ''ExtA''');
  19304. Add(' procedure DoIt(i: longint = 1); virtual;');
  19305. Add(' procedure DoSome(j: longint = 2);');
  19306. Add(' end;');
  19307. Add(' TExtB = class external name ''ExtB''(TExtA)');
  19308. Add(' end;');
  19309. Add(' TMyC = class(TExtB)');
  19310. Add(' procedure DoIt(i: longint = 1); override;');
  19311. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  19312. Add(' end;');
  19313. Add('procedure TMyC.DoIt(i: longint);');
  19314. Add('begin');
  19315. Add(' inherited;');
  19316. Add(' inherited DoIt;');
  19317. Add(' inherited DoIt();');
  19318. Add(' inherited DoIt(3);');
  19319. Add(' inherited DoSome;');
  19320. Add(' inherited DoSome();');
  19321. Add(' inherited DoSome(4);');
  19322. Add('end;');
  19323. Add('procedure TMyC.DoSome(j: longint);');
  19324. Add('begin');
  19325. Add(' inherited;');
  19326. Add('end;');
  19327. Add('begin');
  19328. ConvertProgram;
  19329. CheckSource('TestExternalClass_ReintroduceOverload',
  19330. LinesToStr([ // statements
  19331. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  19332. ' this.$init = function () {',
  19333. ' };',
  19334. ' this.$final = function () {',
  19335. ' };',
  19336. ' this.DoIt = function (i) {',
  19337. ' ExtB.DoIt.apply(this, arguments);',
  19338. ' ExtB.DoIt.call(this, 1);',
  19339. ' ExtB.DoIt.call(this, 1);',
  19340. ' ExtB.DoIt.call(this, 3);',
  19341. ' ExtB.DoSome.call(this, 2);',
  19342. ' ExtB.DoSome.call(this, 2);',
  19343. ' ExtB.DoSome.call(this, 4);',
  19344. ' };',
  19345. ' this.DoSome$1 = function (j) {',
  19346. ' ExtB.DoSome.apply(this, arguments);',
  19347. ' };',
  19348. '});',
  19349. '']),
  19350. LinesToStr([ // $mod.$main
  19351. '']));
  19352. end;
  19353. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  19354. begin
  19355. StartProgram(false);
  19356. Add('{$modeswitch externalclass}');
  19357. Add('type');
  19358. Add(' TObject = class');
  19359. Add(' end;');
  19360. Add(' TExtA = class external name ''ExtA''(TObject)');
  19361. Add(' end;');
  19362. Add('begin');
  19363. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  19364. ConvertProgram;
  19365. end;
  19366. procedure TTestModule.TestExternalClass_NewInstance;
  19367. begin
  19368. StartProgram(false);
  19369. Add('{$modeswitch externalclass}');
  19370. Add('type');
  19371. Add(' TExtA = class external name ''ExtA''');
  19372. Add(' end;');
  19373. Add(' TMyB = class(TExtA)');
  19374. Add(' protected');
  19375. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  19376. Add(' end;');
  19377. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19378. Add('begin end;');
  19379. Add('begin');
  19380. ConvertProgram;
  19381. CheckSource('TestExternalClass_NewInstance',
  19382. LinesToStr([ // statements
  19383. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  19384. ' this.$init = function () {',
  19385. ' };',
  19386. ' this.$final = function () {',
  19387. ' };',
  19388. ' this.NewInstance = function (fnname, paramarray) {',
  19389. ' var Result = null;',
  19390. ' return Result;',
  19391. ' };',
  19392. '});',
  19393. '']),
  19394. LinesToStr([ // $mod.$main
  19395. '']));
  19396. end;
  19397. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  19398. begin
  19399. StartProgram(false);
  19400. Add('{$modeswitch externalclass}');
  19401. Add('type');
  19402. Add(' TExtA = class external name ''ExtA''');
  19403. Add(' end;');
  19404. Add(' TMyB = class(TExtA)');
  19405. Add(' protected');
  19406. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  19407. Add(' end;');
  19408. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  19409. Add('begin end;');
  19410. Add('begin');
  19411. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  19412. ConvertProgram;
  19413. end;
  19414. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  19415. begin
  19416. StartProgram(false);
  19417. Add('{$modeswitch externalclass}');
  19418. Add('type');
  19419. Add(' TExtA = class external name ''ExtA''');
  19420. Add(' end;');
  19421. Add(' TMyB = class(TExtA)');
  19422. Add(' protected');
  19423. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  19424. Add(' end;');
  19425. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  19426. Add('begin end;');
  19427. Add('begin');
  19428. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Longint", expected "String"',
  19429. nIncompatibleTypeArgNo);
  19430. ConvertProgram;
  19431. end;
  19432. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  19433. begin
  19434. StartProgram(false);
  19435. Add('{$modeswitch externalclass}');
  19436. Add('type');
  19437. Add(' TExtA = class external name ''ExtA''');
  19438. Add(' end;');
  19439. Add(' TMyB = class(TExtA)');
  19440. Add(' protected');
  19441. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  19442. Add(' end;');
  19443. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  19444. Add('begin end;');
  19445. Add('begin');
  19446. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "type", expected "untyped"',
  19447. nIncompatibleTypeArgNo);
  19448. ConvertProgram;
  19449. end;
  19450. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  19451. begin
  19452. StartProgram(false);
  19453. Add([
  19454. '{$modeswitch externalclass}',
  19455. 'type',
  19456. ' TJSFunction = class external name ''Function''',
  19457. ' end;',
  19458. ' TExtA = class external name ''ExtA''(TJSFunction)',
  19459. ' constructor New(w: word);',
  19460. ' end;',
  19461. ' TBird = class (TExtA)',
  19462. ' public',
  19463. ' Size: word;',
  19464. ' class var Legs: word;',
  19465. ' constructor Create(a: word);',
  19466. ' end;',
  19467. ' TEagle = class (TBird)',
  19468. ' public',
  19469. ' constructor Create(b: word); reintroduce;',
  19470. ' end;',
  19471. 'constructor TBird.Create(a: word);',
  19472. 'begin',
  19473. ' inherited;', // silently ignored
  19474. ' inherited New(a);', // this.$func(a)
  19475. 'end;',
  19476. 'constructor TEagle.Create(b: word);',
  19477. 'begin',
  19478. ' inherited Create(b);',
  19479. 'end;',
  19480. 'var',
  19481. ' Bird: TBird;',
  19482. ' Eagle: TEagle;',
  19483. 'begin',
  19484. ' Bird:=TBird.Create(3);',
  19485. ' Eagle:=TEagle.Create(4);',
  19486. ' Bird.Size:=Bird.Size+5;',
  19487. ' Bird.Legs:=Bird.Legs+6;',
  19488. ' Eagle.Size:=Eagle.Size+5;',
  19489. ' Eagle.Legs:=Eagle.Legs+6;',
  19490. '']);
  19491. ConvertProgram;
  19492. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  19493. LinesToStr([ // statements
  19494. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  19495. ' this.Legs = 0;',
  19496. ' this.$init = function () {',
  19497. ' this.Size = 0;',
  19498. ' };',
  19499. ' this.$final = function () {',
  19500. ' };',
  19501. ' this.Create = function (a) {',
  19502. ' this.$ancestorfunc(a);',
  19503. ' return this;',
  19504. ' };',
  19505. '});',
  19506. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  19507. ' this.Create$1 = function (b) {',
  19508. ' $mod.TBird.Create.call(this, b);',
  19509. ' return this;',
  19510. ' };',
  19511. '});',
  19512. 'this.Bird = null;',
  19513. 'this.Eagle = null;',
  19514. '']),
  19515. LinesToStr([ // $mod.$main
  19516. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  19517. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  19518. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  19519. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  19520. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  19521. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  19522. '']));
  19523. end;
  19524. procedure TTestModule.TestExternalClass_PascalProperty;
  19525. begin
  19526. StartProgram(false);
  19527. Add('{$modeswitch externalclass}');
  19528. Add('type');
  19529. Add(' TJSElement = class;');
  19530. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  19531. Add(' TJSElement = class external name ''ExtA''');
  19532. Add(' end;');
  19533. Add(' TControl = class(TJSElement)');
  19534. Add(' private');
  19535. Add(' FOnClick: TJSNotifyEvent;');
  19536. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  19537. Add(' procedure Click(Sender: TJSElement);');
  19538. Add(' end;');
  19539. Add('procedure TControl.Click(Sender: TJSElement);');
  19540. Add('begin');
  19541. Add(' OnClick(Self);');
  19542. Add('end;');
  19543. Add('var');
  19544. Add(' Ctrl: TControl;');
  19545. Add('begin');
  19546. Add(' Ctrl.OnClick:[email protected];');
  19547. Add(' Ctrl.OnClick(Ctrl);');
  19548. ConvertProgram;
  19549. CheckSource('TestExternalClass_PascalProperty',
  19550. LinesToStr([ // statements
  19551. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  19552. ' this.$init = function () {',
  19553. ' this.FOnClick = null;',
  19554. ' };',
  19555. ' this.$final = function () {',
  19556. ' this.FOnClick = undefined;',
  19557. ' };',
  19558. ' this.Click = function (Sender) {',
  19559. ' this.FOnClick(this);',
  19560. ' };',
  19561. '});',
  19562. 'this.Ctrl = null;',
  19563. '']),
  19564. LinesToStr([ // $mod.$main
  19565. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  19566. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  19567. '']));
  19568. end;
  19569. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  19570. begin
  19571. StartProgram(false);
  19572. Add([
  19573. '{$modeswitch externalclass}',
  19574. 'type',
  19575. ' IUnknown = interface end;',
  19576. ' TObject = class',
  19577. ' end;',
  19578. ' TChild = class',
  19579. ' end;',
  19580. ' TExtRootA = class external name ''ExtRootA''',
  19581. ' end;',
  19582. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  19583. ' end;',
  19584. ' TExtRootB = class external name ''ExtRootB''',
  19585. ' end;',
  19586. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  19587. ' end;',
  19588. ' TExtString = class external name ''String''',
  19589. ' function charAt(aIndex : NativeInt) : string;',
  19590. ' end;',
  19591. 'var',
  19592. ' Obj: TObject;',
  19593. ' Child: TChild;',
  19594. ' RootA: TExtRootA;',
  19595. ' ChildA: TExtChildA;',
  19596. ' RootB: TExtRootB;',
  19597. ' ChildB: TExtChildB;',
  19598. ' i: IUnknown;',
  19599. ' s: string;',
  19600. ' v: jsvalue;',
  19601. 'begin',
  19602. ' obj:=tobject(roota);',
  19603. ' obj:=tobject(childa);',
  19604. ' child:=tchild(tobject(roota));',
  19605. ' roota:=textroota(obj);',
  19606. ' roota:=textroota(child);',
  19607. ' roota:=textroota(rootb);',
  19608. ' roota:=textroota(childb);',
  19609. ' childa:=textchilda(textroota(obj));',
  19610. ' roota:=TExtRootA(i);',
  19611. ' s:=TExtString(s).charAt(7);',
  19612. ' s:=TExtString(v).charAt(8);',
  19613. '']);
  19614. ConvertProgram;
  19615. CheckSource('TestExternalClass_TypeCastToRootClass',
  19616. LinesToStr([ // statements
  19617. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19618. 'rtl.createClass(this, "TObject", null, function () {',
  19619. ' this.$init = function () {',
  19620. ' };',
  19621. ' this.$final = function () {',
  19622. ' };',
  19623. '});',
  19624. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19625. '});',
  19626. 'this.Obj = null;',
  19627. 'this.Child = null;',
  19628. 'this.RootA = null;',
  19629. 'this.ChildA = null;',
  19630. 'this.RootB = null;',
  19631. 'this.ChildB = null;',
  19632. 'this.i = null;',
  19633. 'this.s = "";',
  19634. 'this.v = undefined;',
  19635. '']),
  19636. LinesToStr([ // $mod.$main
  19637. '$mod.Obj = $mod.RootA;',
  19638. '$mod.Obj = $mod.ChildA;',
  19639. '$mod.Child = $mod.RootA;',
  19640. '$mod.RootA = $mod.Obj;',
  19641. '$mod.RootA = $mod.Child;',
  19642. '$mod.RootA = $mod.RootB;',
  19643. '$mod.RootA = $mod.ChildB;',
  19644. '$mod.ChildA = $mod.Obj;',
  19645. '$mod.RootA = $mod.i;',
  19646. '$mod.s = $mod.s.charAt(7);',
  19647. '$mod.s = $mod.v.charAt(8);',
  19648. '']));
  19649. end;
  19650. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  19651. begin
  19652. StartProgram(false);
  19653. Add([
  19654. '{$modeswitch externalclass}',
  19655. 'type',
  19656. ' IUnknown = interface end;',
  19657. ' IBird = interface(IUnknown) end;',
  19658. ' TClass = class of TObject;',
  19659. ' TObject = class',
  19660. ' end;',
  19661. ' TChild = class',
  19662. ' end;',
  19663. ' TJSObject = class external name ''Object''',
  19664. ' end;',
  19665. ' TRec = record end;',
  19666. 'var',
  19667. ' Obj: TObject;',
  19668. ' Child: TChild;',
  19669. ' i: IUnknown;',
  19670. ' Bird: IBird;',
  19671. ' j: TJSObject;',
  19672. ' r: TRec;',
  19673. ' c: TClass;',
  19674. 'begin',
  19675. ' j:=tjsobject(IUnknown);',
  19676. ' j:=tjsobject(IBird);',
  19677. ' j:=tjsobject(TObject);',
  19678. ' j:=tjsobject(TChild);',
  19679. ' j:=tjsobject(TRec);',
  19680. ' j:=tjsobject(Obj);',
  19681. ' j:=tjsobject(Child);',
  19682. ' j:=tjsobject(i);',
  19683. ' j:=tjsobject(Bird);',
  19684. ' j:=tjsobject(r);',
  19685. ' j:=tjsobject(c);',
  19686. '']);
  19687. ConvertProgram;
  19688. CheckSource('TestExternalClass_TypeCastToJSObject',
  19689. LinesToStr([ // statements
  19690. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19691. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19692. 'rtl.createClass(this, "TObject", null, function () {',
  19693. ' this.$init = function () {',
  19694. ' };',
  19695. ' this.$final = function () {',
  19696. ' };',
  19697. '});',
  19698. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  19699. '});',
  19700. 'rtl.recNewT(this, "TRec", function () {',
  19701. ' this.$eq = function (b) {',
  19702. ' return true;',
  19703. ' };',
  19704. ' this.$assign = function (s) {',
  19705. ' return this;',
  19706. ' };',
  19707. '});',
  19708. 'this.Obj = null;',
  19709. 'this.Child = null;',
  19710. 'this.i = null;',
  19711. 'this.Bird = null;',
  19712. 'this.j = null;',
  19713. 'this.r = this.TRec.$new();',
  19714. 'this.c = null;',
  19715. '']),
  19716. LinesToStr([ // $mod.$main
  19717. '$mod.j = $mod.IUnknown;',
  19718. '$mod.j = $mod.IBird;',
  19719. '$mod.j = $mod.TObject;',
  19720. '$mod.j = $mod.TChild;',
  19721. '$mod.j = $mod.TRec;',
  19722. '$mod.j = $mod.Obj;',
  19723. '$mod.j = $mod.Child;',
  19724. '$mod.j = $mod.i;',
  19725. '$mod.j = $mod.Bird;',
  19726. '$mod.j = $mod.r;',
  19727. '$mod.j = $mod.c;',
  19728. '']));
  19729. end;
  19730. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  19731. begin
  19732. StartProgram(false);
  19733. Add('{$modeswitch externalclass}');
  19734. Add('type');
  19735. Add(' TJSString = class external name ''String''');
  19736. Add(' class function fromCharCode() : string; varargs;');
  19737. Add(' function anchor(const aName : string) : string;');
  19738. Add(' end;');
  19739. Add('var');
  19740. Add(' s: string;');
  19741. Add('begin');
  19742. Add(' s:=TJSString.fromCharCode(65,66);');
  19743. Add(' s:=TJSString(s).anchor(s);');
  19744. Add(' s:=TJSString(''foo'').anchor(s);');
  19745. ConvertProgram;
  19746. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  19747. LinesToStr([ // statements
  19748. 'this.s = "";',
  19749. '']),
  19750. LinesToStr([ // $mod.$main
  19751. '$mod.s = String.fromCharCode(65, 66);',
  19752. '$mod.s = $mod.s.anchor($mod.s);',
  19753. '$mod.s = "foo".anchor($mod.s);',
  19754. '']));
  19755. end;
  19756. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  19757. begin
  19758. StartProgram(false);
  19759. Add([
  19760. '{$modeswitch externalclass}',
  19761. 'type',
  19762. ' TJSObject = class external name ''Object'' end;',
  19763. ' TJSFunction = class external name ''Function''',
  19764. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  19765. ' function call(thisArg: TJSObject): JSValue; varargs;',
  19766. ' end;',
  19767. ' TObject = class',
  19768. ' procedure DoIt(i: longint);',
  19769. ' end;',
  19770. ' TFuncInt = function(o: TObject): longint;',
  19771. 'function GetIt(o: TObject): longint;',
  19772. ' procedure Sub; begin end;',
  19773. 'var',
  19774. ' f: TJSFunction;',
  19775. ' fi: TFuncInt;',
  19776. 'begin',
  19777. ' fi:=TFuncInt(f);',
  19778. ' f:=TJSFunction(fi);',
  19779. ' f:=TJSFunction(@GetIt);',
  19780. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  19781. ' f:=TJSFunction(@Sub);',
  19782. ' f:=TJSFunction(@o.doit);',
  19783. ' f:=TJSFunction(fi).bind(nil,4)',
  19784. 'end;',
  19785. 'procedure TObject.DoIt(i: longint);',
  19786. ' procedure Sub; begin end;',
  19787. 'var f: TJSFunction;',
  19788. 'begin',
  19789. ' f:=TJSFunction(@DoIt);',
  19790. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  19791. ' f:=TJSFunction(@Sub);',
  19792. ' f:=TJSFunction(@GetIt);',
  19793. 'end;',
  19794. 'begin']);
  19795. ConvertProgram;
  19796. CheckSource('TestExternalClass_TypeCastToJSFunction',
  19797. LinesToStr([ // statements
  19798. 'rtl.createClass(this, "TObject", null, function () {',
  19799. ' this.$init = function () {',
  19800. ' };',
  19801. ' this.$final = function () {',
  19802. ' };',
  19803. ' this.DoIt = function (i) {',
  19804. ' var $Self = this;',
  19805. ' function Sub() {',
  19806. ' };',
  19807. ' var f = null;',
  19808. ' f = this.DoIt;',
  19809. ' f = this.DoIt.bind(null, 13);',
  19810. ' f = Sub;',
  19811. ' f = $mod.GetIt;',
  19812. ' };',
  19813. '});',
  19814. 'this.GetIt = function (o) {',
  19815. ' var Result = 0;',
  19816. ' function Sub() {',
  19817. ' };',
  19818. ' var f = null;',
  19819. ' var fi = null;',
  19820. ' fi = f;',
  19821. ' f = fi;',
  19822. ' f = $mod.GetIt;',
  19823. ' f = $mod.GetIt.bind(null, 3);',
  19824. ' f = Sub;',
  19825. ' f = $mod.TObject.DoIt;',
  19826. ' f = fi.bind(null, 4);',
  19827. ' return Result;',
  19828. '};',
  19829. '']),
  19830. LinesToStr([ // $mod.$main
  19831. '']));
  19832. end;
  19833. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  19834. begin
  19835. StartProgram(false);
  19836. Add([
  19837. '{$mode delphi}',
  19838. '{$modeswitch externalclass}',
  19839. 'type',
  19840. ' TJSObject = class external name ''Object'' end;',
  19841. ' TJSWindow = class external name ''Window''(TJSObject)',
  19842. ' procedure Open;',
  19843. ' end;',
  19844. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  19845. ' procedure Execute;',
  19846. ' end;',
  19847. 'procedure Fly;',
  19848. 'var',
  19849. ' w: TJSWindow;',
  19850. ' e: TJSEventTarget;',
  19851. 'begin',
  19852. ' w:=TJSWindow(e);',
  19853. ' e:=TJSEventTarget(w);',
  19854. 'end;',
  19855. 'begin']);
  19856. ConvertProgram;
  19857. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  19858. LinesToStr([ // statements
  19859. 'this.Fly = function () {',
  19860. ' var w = null;',
  19861. ' var e = null;',
  19862. ' w = e;',
  19863. ' e = w;',
  19864. '};',
  19865. '']),
  19866. LinesToStr([ // $mod.$main
  19867. '']));
  19868. end;
  19869. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  19870. begin
  19871. StartProgram(false);
  19872. Add('{$modeswitch externalclass}');
  19873. Add('type');
  19874. Add(' TJSString = class external name ''String''');
  19875. Add(' class function fromCharCode() : string; varargs;');
  19876. Add(' end;');
  19877. Add('var');
  19878. Add(' s: string;');
  19879. Add(' sObj: TJSString;');
  19880. Add('begin');
  19881. Add(' s:=sObj.fromCharCode(65,66);');
  19882. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  19883. nExternalClassInstanceCannotAccessStaticX);
  19884. ConvertProgram;
  19885. end;
  19886. procedure TTestModule.TestExternalClass_BracketAccessor;
  19887. begin
  19888. StartProgram(false);
  19889. Add([
  19890. '{$modeswitch externalclass}',
  19891. 'type',
  19892. ' TJSArray = class external name ''Array2''',
  19893. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19894. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19895. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19896. ' end;',
  19897. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  19898. 'begin end;',
  19899. 'var',
  19900. ' Arr: tjsarray;',
  19901. ' s: string;',
  19902. ' i: longint;',
  19903. ' v: jsvalue;',
  19904. 'begin',
  19905. ' v:=arr[0];',
  19906. ' v:=arr.items[1];',
  19907. ' arr[2]:=s;',
  19908. ' arr.items[3]:=s;',
  19909. ' arr[4]:=i;',
  19910. ' arr[5]:=arr[6];',
  19911. ' arr.items[7]:=arr.items[8];',
  19912. ' with arr do items[9]:=items[10];',
  19913. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  19914. ' with arr do begin',
  19915. ' v:=GetItems(14);',
  19916. ' setitems(15,16);',
  19917. ' end;',
  19918. ' v:=test1.arr.items[17];',
  19919. ' test1.arr.items[18]:=v;',
  19920. '']);
  19921. ConvertProgram;
  19922. CheckSource('TestExternalClass_BracketAccessor',
  19923. LinesToStr([ // statements
  19924. 'this.DoIt = function (vI, vJ, vK, vL) {',
  19925. '};',
  19926. 'this.Arr = null;',
  19927. 'this.s = "";',
  19928. 'this.i = 0;',
  19929. 'this.v = undefined;',
  19930. '']),
  19931. LinesToStr([ // $mod.$main
  19932. '$mod.v = $mod.Arr[0];',
  19933. '$mod.v = $mod.Arr[1];',
  19934. '$mod.Arr[2] = $mod.s;',
  19935. '$mod.Arr[3] = $mod.s;',
  19936. '$mod.Arr[4] = $mod.i;',
  19937. '$mod.Arr[5] = $mod.Arr[6];',
  19938. '$mod.Arr[7] = $mod.Arr[8];',
  19939. 'var $with = $mod.Arr;',
  19940. '$with[9] = $with[10];',
  19941. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  19942. ' a: 9,',
  19943. ' p: $mod.Arr,',
  19944. ' get: function () {',
  19945. ' return this.p[this.a];',
  19946. ' },',
  19947. ' set: function (v) {',
  19948. ' this.p[this.a] = v;',
  19949. ' }',
  19950. '}, {',
  19951. ' a: 10,',
  19952. ' p: $mod.Arr,',
  19953. ' get: function () {',
  19954. ' return this.p[this.a];',
  19955. ' },',
  19956. ' set: function (v) {',
  19957. ' this.p[this.a] = v;',
  19958. ' }',
  19959. '});',
  19960. 'var $with1 = $mod.Arr;',
  19961. '$mod.v = $with1[14];',
  19962. '$with1[15] = 16;',
  19963. '$mod.v = $mod.Arr[17];',
  19964. '$mod.Arr[18] = $mod.v;',
  19965. '']));
  19966. end;
  19967. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  19968. begin
  19969. StartProgram(false);
  19970. Add([
  19971. '{$modeswitch externalclass}',
  19972. 'type',
  19973. ' TJSArray = class external name ''Array2''',
  19974. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  19975. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  19976. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  19977. ' end;',
  19978. ' TMyArr = class(TJSArray)',
  19979. ' procedure DoIt;',
  19980. ' end;',
  19981. 'procedure tmyarr.DoIt;',
  19982. 'begin',
  19983. ' Items[1]:=Items[2];',
  19984. ' SetItems(3,getItems(4));',
  19985. 'end;',
  19986. 'var',
  19987. ' Arr: tmyarr;',
  19988. ' s: string;',
  19989. ' i: longint;',
  19990. ' v: jsvalue;',
  19991. 'begin',
  19992. ' v:=arr[0];',
  19993. ' v:=arr.items[1];',
  19994. ' arr[2]:=s;',
  19995. ' arr.items[3]:=s;',
  19996. ' arr[4]:=i;',
  19997. ' arr[5]:=arr[6];',
  19998. ' arr.items[7]:=arr.items[8];',
  19999. ' with arr do items[9]:=items[10];',
  20000. ' with arr do begin',
  20001. ' v:=GetItems(14);',
  20002. ' setitems(15,16);',
  20003. ' end;',
  20004. '']);
  20005. ConvertProgram;
  20006. CheckSource('TestExternalClass_BracketAccessor_Call',
  20007. LinesToStr([ // statements
  20008. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  20009. ' this.$init = function () {',
  20010. ' };',
  20011. ' this.$final = function () {',
  20012. ' };',
  20013. ' this.DoIt = function () {',
  20014. ' this[1] = this[2];',
  20015. ' this[3] = this[4];',
  20016. ' };',
  20017. '});',
  20018. 'this.Arr = null;',
  20019. 'this.s = "";',
  20020. 'this.i = 0;',
  20021. 'this.v = undefined;',
  20022. '']),
  20023. LinesToStr([ // $mod.$main
  20024. '$mod.v = $mod.Arr[0];',
  20025. '$mod.v = $mod.Arr[1];',
  20026. '$mod.Arr[2] = $mod.s;',
  20027. '$mod.Arr[3] = $mod.s;',
  20028. '$mod.Arr[4] = $mod.i;',
  20029. '$mod.Arr[5] = $mod.Arr[6];',
  20030. '$mod.Arr[7] = $mod.Arr[8];',
  20031. 'var $with = $mod.Arr;',
  20032. '$with[9] = $with[10];',
  20033. 'var $with1 = $mod.Arr;',
  20034. '$mod.v = $with1[14];',
  20035. '$with1[15] = 16;',
  20036. '']));
  20037. end;
  20038. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  20039. begin
  20040. StartProgram(false);
  20041. Add('{$modeswitch externalclass}');
  20042. Add('type');
  20043. Add(' TJSArray = class external name ''Array2''');
  20044. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  20045. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  20046. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  20047. Add(' end;');
  20048. Add('begin');
  20049. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  20050. nBracketAccessorOfExternalClassMustHaveOneParameter);
  20051. ConvertProgram;
  20052. end;
  20053. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  20054. begin
  20055. StartProgram(false);
  20056. Add('{$modeswitch externalclass}');
  20057. Add('type');
  20058. Add(' TJSArray = class external name ''Array2''');
  20059. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20060. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  20061. Add(' end;');
  20062. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  20063. Add('begin end;');
  20064. Add('var');
  20065. Add(' Arr: tjsarray;');
  20066. Add(' v: jsvalue;');
  20067. Add('begin');
  20068. Add(' v:=arr[0];');
  20069. Add(' v:=arr.items[1];');
  20070. Add(' with arr do v:=items[2];');
  20071. Add(' doit(arr[3],arr[4]);');
  20072. ConvertProgram;
  20073. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  20074. LinesToStr([ // statements
  20075. 'this.DoIt = function (vI, vJ) {',
  20076. '};',
  20077. 'this.Arr = null;',
  20078. 'this.v = undefined;',
  20079. '']),
  20080. LinesToStr([ // $mod.$main
  20081. '$mod.v = $mod.Arr[0];',
  20082. '$mod.v = $mod.Arr[1];',
  20083. 'var $with = $mod.Arr;',
  20084. '$mod.v = $with[2];',
  20085. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  20086. '']));
  20087. end;
  20088. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  20089. begin
  20090. StartProgram(false);
  20091. Add('{$modeswitch externalclass}');
  20092. Add('type');
  20093. Add(' TJSArray = class external name ''Array2''');
  20094. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20095. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20096. Add(' end;');
  20097. Add('var');
  20098. Add(' Arr: tjsarray;');
  20099. Add(' s: string;');
  20100. Add(' i: longint;');
  20101. Add(' v: jsvalue;');
  20102. Add('begin');
  20103. Add(' arr[2]:=s;');
  20104. Add(' arr.items[3]:=s;');
  20105. Add(' arr[4]:=i;');
  20106. Add(' with arr do items[5]:=i;');
  20107. ConvertProgram;
  20108. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  20109. LinesToStr([ // statements
  20110. 'this.Arr = null;',
  20111. 'this.s = "";',
  20112. 'this.i = 0;',
  20113. 'this.v = undefined;',
  20114. '']),
  20115. LinesToStr([ // $mod.$main
  20116. '$mod.Arr[2] = $mod.s;',
  20117. '$mod.Arr[3] = $mod.s;',
  20118. '$mod.Arr[4] = $mod.i;',
  20119. 'var $with = $mod.Arr;',
  20120. '$with[5] = $mod.i;',
  20121. '']));
  20122. end;
  20123. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  20124. begin
  20125. StartProgram(false);
  20126. Add('{$modeswitch externalclass}');
  20127. Add('type');
  20128. Add(' TJSArray = class external name ''Array2''');
  20129. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20130. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  20131. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  20132. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  20133. Add(' end;');
  20134. Add('var');
  20135. Add(' Arr: tjsarray;');
  20136. Add(' s: string;');
  20137. Add(' i: longint;');
  20138. Add(' v: jsvalue;');
  20139. Add('begin');
  20140. Add(' arr[2]:=s;');
  20141. Add(' arr.items[3]:=s;');
  20142. Add(' arr.numbers[4]:=i;');
  20143. Add(' with arr do items[5]:=i;');
  20144. Add(' with arr do numbers[6]:=i;');
  20145. ConvertProgram;
  20146. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  20147. LinesToStr([ // statements
  20148. 'this.Arr = null;',
  20149. 'this.s = "";',
  20150. 'this.i = 0;',
  20151. 'this.v = undefined;',
  20152. '']),
  20153. LinesToStr([ // $mod.$main
  20154. '$mod.Arr[2] = $mod.s;',
  20155. '$mod.Arr[3] = $mod.s;',
  20156. '$mod.Arr[4] = $mod.i;',
  20157. 'var $with = $mod.Arr;',
  20158. '$with[5] = $mod.i;',
  20159. 'var $with1 = $mod.Arr;',
  20160. '$with1[6] = $mod.i;',
  20161. '']));
  20162. end;
  20163. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  20164. begin
  20165. StartProgram(false);
  20166. Add('{$modeswitch externalclass}');
  20167. Add('type');
  20168. Add(' TJSArray = class external name ''Array2''');
  20169. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  20170. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  20171. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  20172. Add(' end;');
  20173. Add('var');
  20174. Add(' Arr: tjsarray;');
  20175. Add(' i: longint;');
  20176. Add(' IntArr: array of longint;');
  20177. Add(' v: jsvalue;');
  20178. Add('begin');
  20179. Add(' v:=arr.items[i];');
  20180. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  20181. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  20182. ConvertProgram;
  20183. CheckSource('TestExternalClass_BracketAccessor_Index',
  20184. LinesToStr([ // statements
  20185. 'this.Arr = null;',
  20186. 'this.i = 0;',
  20187. 'this.IntArr = [];',
  20188. 'this.v = undefined;',
  20189. '']),
  20190. LinesToStr([ // $mod.$main
  20191. '$mod.v = $mod.Arr[$mod.i];',
  20192. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  20193. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  20194. '']));
  20195. end;
  20196. procedure TTestModule.TestExternalClass_ForInJSObject;
  20197. begin
  20198. StartProgram(false);
  20199. Add([
  20200. '{$modeswitch externalclass}',
  20201. 'type',
  20202. ' TJSObject = class external name ''Object''',
  20203. ' end;',
  20204. 'var',
  20205. ' o: TJSObject;',
  20206. ' key: string;',
  20207. 'begin',
  20208. ' for key in o do',
  20209. ' if key=''abc'' then ;',
  20210. '']);
  20211. ConvertProgram;
  20212. CheckSource('TestExternalClass_ForInJSObject',
  20213. LinesToStr([ // statements
  20214. 'this.o = null;',
  20215. 'this.key = "";',
  20216. '']),
  20217. LinesToStr([ // $mod.$main
  20218. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  20219. '']));
  20220. end;
  20221. procedure TTestModule.TestExternalClass_ForInJSArray;
  20222. begin
  20223. StartProgram(false);
  20224. Add([
  20225. '{$modeswitch externalclass}',
  20226. 'type',
  20227. ' TJSInt8Array = class external name ''Int8Array''',
  20228. ' private',
  20229. ' flength: NativeInt external name ''length'';',
  20230. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  20231. ' public',
  20232. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  20233. ' property Length: NativeInt read flength;',
  20234. ' end;',
  20235. 'var',
  20236. ' a: TJSInt8Array;',
  20237. ' value: shortint;',
  20238. 'begin',
  20239. ' for value in a do',
  20240. ' if value=3 then ;',
  20241. '']);
  20242. ConvertProgram;
  20243. CheckSource('TestExternalClass_ForInJSArray',
  20244. LinesToStr([ // statements
  20245. 'this.a = null;',
  20246. 'this.value = 0;',
  20247. '']),
  20248. LinesToStr([ // $mod.$main
  20249. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  20250. ' $mod.value = $in[$l];',
  20251. ' if ($mod.value === 3) ;',
  20252. '};',
  20253. '']));
  20254. end;
  20255. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  20256. begin
  20257. AddModuleWithIntfImplSrc('unit2.pas',
  20258. LinesToStr([
  20259. '{$modeswitch externalclass}',
  20260. 'type',
  20261. ' TJSBufferSource = class external name ''BufferSource''',
  20262. ' end;',
  20263. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  20264. '']),
  20265. '');
  20266. AddModuleWithIntfImplSrc('unit3.pas',
  20267. LinesToStr([
  20268. '{$modeswitch externalclass}',
  20269. 'type',
  20270. ' TJSBufferSource = class external name ''BufferSource''',
  20271. ' end;',
  20272. '']),
  20273. '');
  20274. StartUnit(true);
  20275. Add([
  20276. 'interface',
  20277. 'uses unit2, unit3;',
  20278. 'procedure DoSome(s: TJSBufferSource);',
  20279. 'implementation',
  20280. 'procedure DoSome(s: TJSBufferSource);',
  20281. 'begin',
  20282. ' DoIt(s);',
  20283. 'end;',
  20284. '']);
  20285. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  20286. nIncompatibleTypeArgNo);
  20287. ConvertUnit;
  20288. end;
  20289. procedure TTestModule.TestExternalClass_NestedConstructor;
  20290. begin
  20291. StartProgram(false);
  20292. Add([
  20293. '{$modeswitch externalclass}',
  20294. 'type',
  20295. ' TJSObject = class external name ''Object''',
  20296. ' type TBird = class external name ''Bird''',
  20297. ' type TWing = class external name ''Wing''',
  20298. ' constructor New;',
  20299. ' constructor Create(w: word = 3);',
  20300. ' end;',
  20301. ' end;',
  20302. ' end;',
  20303. 'var',
  20304. ' w: TJSObject.TBird.TWing;',
  20305. 'begin',
  20306. ' w:=tjsobject.tbird.twing.new;',
  20307. ' w:=tjsobject.tbird.twing.new();',
  20308. ' w:=tjsobject.tbird.twing.create;',
  20309. ' w:=tjsobject.tbird.twing.create(4);',
  20310. ' with tjsobject do begin',
  20311. ' w:=tbird.twing.new;',
  20312. ' w:=tbird.twing.new();',
  20313. ' w:=tbird.twing.create;',
  20314. ' w:=tbird.twing.create(11);',
  20315. ' end;',
  20316. ' with tjsobject.tbird do begin',
  20317. ' w:=twing.new;',
  20318. ' w:=twing.new();',
  20319. ' w:=twing.create;',
  20320. ' w:=twing.create(21);',
  20321. ' end;',
  20322. ' with tjsobject.tbird.twing do begin',
  20323. ' w:=new;',
  20324. ' w:=new();',
  20325. ' w:=create;',
  20326. ' w:=create(31);',
  20327. ' end;',
  20328. '']);
  20329. ConvertProgram;
  20330. CheckSource('TestExternalClass_NestedConstructor',
  20331. LinesToStr([ // statements
  20332. 'this.w = null;',
  20333. '']),
  20334. LinesToStr([ // $mod.$main
  20335. '$mod.w = new Object.Bird.Wing();',
  20336. '$mod.w = new Object.Bird.Wing();',
  20337. '$mod.w = new Object.Bird.Wing.Create();',
  20338. '$mod.w = new Object.Bird.Wing.Create(4);',
  20339. '$mod.w = new Object.Bird.Wing();',
  20340. '$mod.w = new Object.Bird.Wing();',
  20341. '$mod.w = new Object.Bird.Wing.Create();',
  20342. '$mod.w = new Object.Bird.Wing.Create(11);',
  20343. 'var $with = Object.Bird;',
  20344. '$mod.w = new Object.Bird.Wing();',
  20345. '$mod.w = new Object.Bird.Wing();',
  20346. '$mod.w = new Object.Bird.Wing.Create();',
  20347. '$mod.w = new Object.Bird.Wing.Create(21);',
  20348. 'var $with1 = Object.Bird.Wing;',
  20349. '$mod.w = new $with1();',
  20350. '$mod.w = new $with1();',
  20351. '$mod.w = new Object.Bird.Wing.Create();',
  20352. '$mod.w = new Object.Bird.Wing.Create(31);',
  20353. '']));
  20354. end;
  20355. procedure TTestModule.TestClassInterface_Corba;
  20356. begin
  20357. StartProgram(false);
  20358. Add([
  20359. '{$interfaces corba}',
  20360. 'type',
  20361. ' IUnknown = interface;',
  20362. ' IUnknown = interface',
  20363. ' [''{00000000-0000-0000-C000-000000000046}'']',
  20364. ' end;',
  20365. ' IInterface = IUnknown;',
  20366. ' IBird = interface(IInterface)',
  20367. ' function GetSize: longint;',
  20368. ' procedure SetSize(i: longint);',
  20369. ' property Size: longint read GetSize write SetSize;',
  20370. ' procedure DoIt(i: longint);',
  20371. ' end;',
  20372. ' TObject = class',
  20373. ' end;',
  20374. ' TBird = class(TObject,IBird)',
  20375. ' function GetSize: longint; virtual; abstract;',
  20376. ' procedure SetSize(i: longint); virtual; abstract;',
  20377. ' procedure DoIt(i: longint); virtual; abstract;',
  20378. ' end;',
  20379. 'var',
  20380. ' BirdIntf: IBird;',
  20381. 'begin',
  20382. ' BirdIntf.Size:=BirdIntf.Size;',
  20383. '']);
  20384. ConvertProgram;
  20385. CheckSource('TestClassInterface_Corba',
  20386. LinesToStr([ // statements
  20387. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  20388. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  20389. 'rtl.createClass(this, "TObject", null, function () {',
  20390. ' this.$init = function () {',
  20391. ' };',
  20392. ' this.$final = function () {',
  20393. ' };',
  20394. '});',
  20395. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20396. ' rtl.addIntf(this, $mod.IBird);',
  20397. '});',
  20398. 'this.BirdIntf = null;',
  20399. '']),
  20400. LinesToStr([ // $mod.$main
  20401. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  20402. '']));
  20403. end;
  20404. procedure TTestModule.TestClassInterface_Corba_ProcExternalFail;
  20405. begin
  20406. StartProgram(false);
  20407. Add([
  20408. '{$interfaces corba}',
  20409. 'type',
  20410. ' IUnknown = interface',
  20411. ' procedure DoIt; external name ''foo'';',
  20412. ' end;',
  20413. 'begin']);
  20414. SetExpectedParserError(
  20415. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  20416. nParserNoFieldsAllowed);
  20417. ConvertProgram;
  20418. end;
  20419. procedure TTestModule.TestClassInterface_Corba_Overloads;
  20420. begin
  20421. StartProgram(false);
  20422. Add([
  20423. '{$interfaces corba}',
  20424. 'type',
  20425. ' integer = longint;',
  20426. ' IUnknown = interface',
  20427. ' procedure DoIt(i: integer);',
  20428. ' procedure DoIt(s: string);',
  20429. ' end;',
  20430. ' IBird = interface(IUnknown)',
  20431. ' procedure DoIt(b: boolean); overload;',
  20432. ' end;',
  20433. ' TObject = class',
  20434. ' end;',
  20435. ' TBird = class(TObject,IBird)',
  20436. ' procedure DoIt(o: TObject);',
  20437. ' procedure DoIt(s: string);',
  20438. ' procedure DoIt(i: integer);',
  20439. ' procedure DoIt(b: boolean);',
  20440. ' end;',
  20441. 'procedure TBird.DoIt(o: TObject); begin end;',
  20442. 'procedure TBird.DoIt(s: string); begin end;',
  20443. 'procedure TBird.DoIt(i: integer); begin end;',
  20444. 'procedure TBird.DoIt(b: boolean); begin end;',
  20445. 'var',
  20446. ' BirdIntf: IBird;',
  20447. 'begin',
  20448. ' BirdIntf.DoIt(3);',
  20449. ' BirdIntf.DoIt(''abc'');',
  20450. ' BirdIntf.DoIt(true);',
  20451. '']);
  20452. ConvertProgram;
  20453. CheckSource('TestClassInterface_Overloads',
  20454. LinesToStr([ // statements
  20455. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  20456. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  20457. 'rtl.createClass(this, "TObject", null, function () {',
  20458. ' this.$init = function () {',
  20459. ' };',
  20460. ' this.$final = function () {',
  20461. ' };',
  20462. '});',
  20463. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20464. ' this.DoIt = function (o) {',
  20465. ' };',
  20466. ' this.DoIt$1 = function (s) {',
  20467. ' };',
  20468. ' this.DoIt$2 = function (i) {',
  20469. ' };',
  20470. ' this.DoIt$3 = function (b) {',
  20471. ' };',
  20472. ' rtl.addIntf(this, $mod.IBird, {',
  20473. ' DoIt$2: "DoIt$3",',
  20474. ' DoIt: "DoIt$2"',
  20475. ' });',
  20476. '});',
  20477. 'this.BirdIntf = null;',
  20478. '']),
  20479. LinesToStr([ // $mod.$main
  20480. '$mod.BirdIntf.DoIt(3);',
  20481. '$mod.BirdIntf.DoIt$1("abc");',
  20482. '$mod.BirdIntf.DoIt$2(true);',
  20483. '']));
  20484. end;
  20485. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInIntfListFail;
  20486. begin
  20487. StartProgram(false);
  20488. Add([
  20489. '{$interfaces corba}',
  20490. 'type',
  20491. ' IBird = interface',
  20492. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20493. ' end;',
  20494. ' IDog = interface',
  20495. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20496. ' end;',
  20497. ' TObject = class(IBird,IDog)',
  20498. ' end;',
  20499. 'begin']);
  20500. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  20501. nDuplicateGUIDXInYZ);
  20502. ConvertProgram;
  20503. end;
  20504. procedure TTestModule.TestClassInterface_Corba_DuplicateGUIInAncestorFail;
  20505. begin
  20506. StartProgram(false);
  20507. Add([
  20508. '{$interfaces corba}',
  20509. 'type',
  20510. ' IAnimal = interface',
  20511. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20512. ' end;',
  20513. ' IBird = interface(IAnimal)',
  20514. ' end;',
  20515. ' IHawk = interface(IBird)',
  20516. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  20517. ' end;',
  20518. 'begin']);
  20519. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  20520. nDuplicateGUIDXInYZ);
  20521. ConvertProgram;
  20522. end;
  20523. procedure TTestModule.TestClassInterface_Corba_AncestorImpl;
  20524. begin
  20525. StartProgram(false);
  20526. Add([
  20527. '{$interfaces corba}',
  20528. 'type',
  20529. ' integer = longint;',
  20530. ' IUnknown = interface',
  20531. ' procedure DoIt(i: integer);',
  20532. ' end;',
  20533. ' IBird = interface',
  20534. ' procedure Fly(i: integer);',
  20535. ' end;',
  20536. ' TObject = class(IUnknown)',
  20537. ' procedure DoIt(i: integer);',
  20538. ' end;',
  20539. ' TBird = class(IBird)',
  20540. ' procedure Fly(i: integer);',
  20541. ' end;',
  20542. 'procedure TObject.DoIt(i: integer); begin end;',
  20543. 'procedure TBird.Fly(i: integer); begin end;',
  20544. 'begin',
  20545. '']);
  20546. ConvertProgram;
  20547. CheckSource('TestClassInterface_Corba_AncestorImpl',
  20548. LinesToStr([ // statements
  20549. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  20550. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  20551. 'rtl.createClass(this, "TObject", null, function () {',
  20552. ' this.$init = function () {',
  20553. ' };',
  20554. ' this.$final = function () {',
  20555. ' };',
  20556. ' this.DoIt = function (i) {',
  20557. ' };',
  20558. ' rtl.addIntf(this, $mod.IUnknown);',
  20559. '});',
  20560. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20561. ' this.Fly = function (i) {',
  20562. ' };',
  20563. ' rtl.addIntf(this, $mod.IBird);',
  20564. ' rtl.addIntf(this, $mod.IUnknown);',
  20565. '});',
  20566. '']),
  20567. LinesToStr([ // $mod.$main
  20568. '']));
  20569. end;
  20570. procedure TTestModule.TestClassInterface_Corba_ImplReintroduce;
  20571. begin
  20572. StartProgram(false);
  20573. Add([
  20574. '{$interfaces corba}',
  20575. 'type',
  20576. ' integer = longint;',
  20577. ' IBird = interface',
  20578. ' procedure DoIt(i: integer);',
  20579. ' end;',
  20580. ' TObject = class',
  20581. ' procedure DoIt(i: integer);',
  20582. ' end;',
  20583. ' TBird = class(IBird)',
  20584. ' procedure DoIt(i: integer); virtual; reintroduce;',
  20585. ' end;',
  20586. 'procedure TObject.DoIt(i: integer); begin end;',
  20587. 'procedure TBird.DoIt(i: integer); begin end;',
  20588. 'begin',
  20589. '']);
  20590. ConvertProgram;
  20591. CheckSource('TestClassInterface_Corba_ImplReintroduce',
  20592. LinesToStr([ // statements
  20593. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  20594. 'rtl.createClass(this, "TObject", null, function () {',
  20595. ' this.$init = function () {',
  20596. ' };',
  20597. ' this.$final = function () {',
  20598. ' };',
  20599. ' this.DoIt = function (i) {',
  20600. ' };',
  20601. '});',
  20602. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20603. ' this.DoIt$1 = function (i) {',
  20604. ' };',
  20605. ' rtl.addIntf(this, $mod.IBird, {',
  20606. ' DoIt: "DoIt$1"',
  20607. ' });',
  20608. '});',
  20609. '']),
  20610. LinesToStr([ // $mod.$main
  20611. '']));
  20612. end;
  20613. procedure TTestModule.TestClassInterface_Corba_MethodResolution;
  20614. begin
  20615. StartProgram(false);
  20616. Add([
  20617. '{$interfaces corba}',
  20618. 'type',
  20619. ' IUnknown = interface',
  20620. ' procedure Walk(i: longint);',
  20621. ' end;',
  20622. ' IBird = interface(IUnknown)',
  20623. ' procedure Walk(b: boolean); overload;',
  20624. ' procedure Fly(s: string);',
  20625. ' end;',
  20626. ' TObject = class',
  20627. ' end;',
  20628. ' TBird = class(TObject,IBird)',
  20629. ' procedure IBird.Fly = Move;',
  20630. ' procedure IBird.Walk = Hop;',
  20631. ' procedure Hop(i: longint);',
  20632. ' procedure Move(s: string);',
  20633. ' procedure Hop(b: boolean);',
  20634. ' end;',
  20635. 'procedure TBird.Move(s: string); begin end;',
  20636. 'procedure TBird.Hop(i: longint); begin end;',
  20637. 'procedure TBird.Hop(b: boolean); begin end;',
  20638. 'var',
  20639. ' BirdIntf: IBird;',
  20640. 'begin',
  20641. ' BirdIntf.Walk(3);',
  20642. ' BirdIntf.Walk(true);',
  20643. ' BirdIntf.Fly(''abc'');',
  20644. '']);
  20645. ConvertProgram;
  20646. CheckSource('TestClassInterface_Corba_MethodResolution',
  20647. LinesToStr([ // statements
  20648. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  20649. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  20650. 'rtl.createClass(this, "TObject", null, function () {',
  20651. ' this.$init = function () {',
  20652. ' };',
  20653. ' this.$final = function () {',
  20654. ' };',
  20655. '});',
  20656. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20657. ' this.Hop = function (i) {',
  20658. ' };',
  20659. ' this.Move = function (s) {',
  20660. ' };',
  20661. ' this.Hop$1 = function (b) {',
  20662. ' };',
  20663. ' rtl.addIntf(this, $mod.IBird, {',
  20664. ' Walk$1: "Hop$1",',
  20665. ' Fly: "Move",',
  20666. ' Walk: "Hop"',
  20667. ' });',
  20668. '});',
  20669. 'this.BirdIntf = null;',
  20670. '']),
  20671. LinesToStr([ // $mod.$main
  20672. '$mod.BirdIntf.Walk(3);',
  20673. '$mod.BirdIntf.Walk$1(true);',
  20674. '$mod.BirdIntf.Fly("abc");',
  20675. '']));
  20676. end;
  20677. procedure TTestModule.TestClassInterface_COM_AncestorMoreInterfaces;
  20678. begin
  20679. StartProgram(false);
  20680. Add([
  20681. '{$interfaces com}',
  20682. 'type',
  20683. ' IUnknown = interface',
  20684. ' function _AddRef: longint;',
  20685. ' procedure Walk;',
  20686. ' end;',
  20687. ' IBird = interface end;',
  20688. ' IDog = interface end;',
  20689. ' TObject = class(IBird,IDog)',
  20690. ' function _AddRef: longint; virtual; abstract;',
  20691. ' procedure Walk; virtual; abstract;',
  20692. ' end;',
  20693. ' TBird = class(IUnknown)',
  20694. ' end;',
  20695. 'begin',
  20696. '']);
  20697. ConvertProgram;
  20698. CheckSource('TestClassInterface_COM_AncestorMoreInterfaces',
  20699. LinesToStr([ // statements
  20700. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  20701. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  20702. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  20703. 'rtl.createClass(this, "TObject", null, function () {',
  20704. ' this.$init = function () {',
  20705. ' };',
  20706. ' this.$final = function () {',
  20707. ' };',
  20708. ' rtl.addIntf(this, $mod.IBird);',
  20709. ' rtl.addIntf(this, $mod.IDog);',
  20710. '});',
  20711. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20712. ' rtl.addIntf(this, $mod.IUnknown);',
  20713. ' rtl.addIntf(this, $mod.IBird);',
  20714. ' rtl.addIntf(this, $mod.IDog);',
  20715. '});',
  20716. '']),
  20717. LinesToStr([ // $mod.$main
  20718. '']));
  20719. end;
  20720. procedure TTestModule.TestClassInterface_Corba_MethodOverride;
  20721. begin
  20722. StartProgram(false);
  20723. Add([
  20724. '{$interfaces corba}',
  20725. 'type',
  20726. ' IUnknown = interface',
  20727. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  20728. ' procedure Go;',
  20729. ' end;',
  20730. ' TObject = class(IUnknown)',
  20731. ' procedure Go; virtual; abstract;',
  20732. ' end;',
  20733. ' TBird = class',
  20734. ' procedure Go; override;',
  20735. ' end;',
  20736. ' TCat = class(TObject)',
  20737. ' procedure Go; override;',
  20738. ' end;',
  20739. ' TDog = class(TObject, IUnknown)',
  20740. ' procedure Go; override;',
  20741. ' end;',
  20742. 'procedure TBird.Go; begin end;',
  20743. 'procedure TCat.Go; begin end;',
  20744. 'procedure TDog.Go; begin end;',
  20745. 'begin',
  20746. '']);
  20747. ConvertProgram;
  20748. CheckSource('TestClassInterface_Corba_MethodOverride',
  20749. LinesToStr([ // statements
  20750. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  20751. 'rtl.createClass(this, "TObject", null, function () {',
  20752. ' this.$init = function () {',
  20753. ' };',
  20754. ' this.$final = function () {',
  20755. ' };',
  20756. ' rtl.addIntf(this, $mod.IUnknown);',
  20757. '});',
  20758. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20759. ' this.Go = function () {',
  20760. ' };',
  20761. ' rtl.addIntf(this, $mod.IUnknown);',
  20762. '});',
  20763. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  20764. ' this.Go = function () {',
  20765. ' };',
  20766. ' rtl.addIntf(this, $mod.IUnknown);',
  20767. '});',
  20768. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  20769. ' this.Go = function () {',
  20770. ' };',
  20771. ' rtl.addIntf(this, $mod.IUnknown);',
  20772. '});',
  20773. '']),
  20774. LinesToStr([ // $mod.$main
  20775. '']));
  20776. end;
  20777. procedure TTestModule.TestClassInterface_Corba_Delegation;
  20778. begin
  20779. StartProgram(false);
  20780. Add([
  20781. '{$interfaces corba}',
  20782. 'type',
  20783. ' IUnknown = interface',
  20784. ' end;',
  20785. ' IBird = interface(IUnknown)',
  20786. ' procedure Fly(s: string);',
  20787. ' end;',
  20788. ' IEagle = interface(IBird)',
  20789. ' end;',
  20790. ' IDove = interface(IBird)',
  20791. ' end;',
  20792. ' ISwallow = interface(IBird)',
  20793. ' end;',
  20794. ' TObject = class',
  20795. ' end;',
  20796. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20797. ' procedure Fly(s: string); virtual; abstract;',
  20798. ' end;',
  20799. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20800. ' FBirdIntf: IBird;',
  20801. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20802. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20803. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20804. ' FDoveObj: TBird;',
  20805. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20806. ' function GetSwallowObj: TBird; virtual; abstract;',
  20807. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20808. ' end;',
  20809. 'begin',
  20810. '']);
  20811. ConvertProgram;
  20812. CheckSource('TestClassInterface_Corba_Delegation',
  20813. LinesToStr([ // statements
  20814. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20815. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20816. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20817. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20818. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20819. 'rtl.createClass(this, "TObject", null, function () {',
  20820. ' this.$init = function () {',
  20821. ' };',
  20822. ' this.$final = function () {',
  20823. ' };',
  20824. '});',
  20825. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20826. ' rtl.addIntf(this, $mod.IBird);',
  20827. ' rtl.addIntf(this, $mod.IEagle);',
  20828. ' rtl.addIntf(this, $mod.IDove);',
  20829. ' rtl.addIntf(this, $mod.ISwallow);',
  20830. '});',
  20831. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20832. ' this.$init = function () {',
  20833. ' $mod.TObject.$init.call(this);',
  20834. ' this.FBirdIntf = null;',
  20835. ' this.FDoveObj = null;',
  20836. ' };',
  20837. ' this.$final = function () {',
  20838. ' this.FBirdIntf = undefined;',
  20839. ' this.FDoveObj = undefined;',
  20840. ' $mod.TObject.$final.call(this);',
  20841. ' };',
  20842. ' this.$intfmaps = {',
  20843. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20844. ' return this.FBirdIntf;',
  20845. ' },',
  20846. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20847. ' return this.GetEagleIntf();',
  20848. ' },',
  20849. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20850. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20851. ' },',
  20852. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20853. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20854. ' }',
  20855. ' };',
  20856. '});',
  20857. '']),
  20858. LinesToStr([ // $mod.$main
  20859. '']));
  20860. end;
  20861. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  20862. begin
  20863. StartProgram(false);
  20864. Add([
  20865. '{$interfaces corba}',
  20866. 'type',
  20867. ' IUnknown = interface',
  20868. ' end;',
  20869. ' IBird = interface(IUnknown)',
  20870. ' procedure Fly(s: string);',
  20871. ' end;',
  20872. ' IEagle = interface(IBird)',
  20873. ' end;',
  20874. ' IDove = interface(IBird)',
  20875. ' end;',
  20876. ' ISwallow = interface(IBird)',
  20877. ' end;',
  20878. ' TObject = class',
  20879. ' end;',
  20880. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20881. ' procedure Fly(s: string); virtual; abstract;',
  20882. ' end;',
  20883. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20884. ' private',
  20885. ' class var FBirdIntf: IBird;',
  20886. ' class var FDoveObj: TBird;',
  20887. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  20888. ' class function GetSwallowObj: TBird; virtual; abstract;',
  20889. ' protected',
  20890. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  20891. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20892. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  20893. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20894. ' end;',
  20895. 'begin',
  20896. '']);
  20897. ConvertProgram;
  20898. CheckSource('TestClassInterface_Corba_DelegationStatic',
  20899. LinesToStr([ // statements
  20900. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20901. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  20902. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  20903. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  20904. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  20905. 'rtl.createClass(this, "TObject", null, function () {',
  20906. ' this.$init = function () {',
  20907. ' };',
  20908. ' this.$final = function () {',
  20909. ' };',
  20910. '});',
  20911. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20912. ' rtl.addIntf(this, $mod.IBird);',
  20913. ' rtl.addIntf(this, $mod.IEagle);',
  20914. ' rtl.addIntf(this, $mod.IDove);',
  20915. ' rtl.addIntf(this, $mod.ISwallow);',
  20916. '});',
  20917. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20918. ' this.FBirdIntf = null;',
  20919. ' this.FDoveObj = null;',
  20920. ' this.$intfmaps = {',
  20921. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  20922. ' return this.FBirdIntf;',
  20923. ' },',
  20924. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  20925. ' return this.GetEagleIntf();',
  20926. ' },',
  20927. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  20928. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  20929. ' },',
  20930. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  20931. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20932. ' }',
  20933. ' };',
  20934. '});',
  20935. '']),
  20936. LinesToStr([ // $mod.$main
  20937. '']));
  20938. end;
  20939. procedure TTestModule.TestClassInterface_Corba_Operators;
  20940. begin
  20941. StartProgram(false);
  20942. Add([
  20943. '{$interfaces corba}',
  20944. 'type',
  20945. ' IUnknown = interface',
  20946. ' end;',
  20947. ' IBird = interface(IUnknown)',
  20948. ' function GetItems(Index: longint): longint;',
  20949. ' procedure SetItems(Index: longint; Value: longint);',
  20950. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  20951. ' end;',
  20952. ' TObject = class',
  20953. ' end;',
  20954. ' TBird = class(TObject,IBird)',
  20955. ' function GetItems(Index: longint): longint; virtual; abstract;',
  20956. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  20957. ' end;',
  20958. 'var',
  20959. ' IntfVar: IBird = nil;',
  20960. ' IntfVar2: IBird;',
  20961. ' ObjVar: TBird;',
  20962. ' v: JSValue;',
  20963. 'begin',
  20964. ' IntfVar:=nil;',
  20965. ' IntfVar[3]:=IntfVar[4];',
  20966. ' if Assigned(IntfVar) then ;',
  20967. ' IntfVar:=IntfVar2;',
  20968. ' IntfVar:=ObjVar;',
  20969. ' if IntfVar=IntfVar2 then ;',
  20970. ' if IntfVar<>IntfVar2 then ;',
  20971. ' if IntfVar is IBird then ;',
  20972. ' if IntfVar is TBird then ;',
  20973. ' if ObjVar is IBird then ;',
  20974. ' IntfVar:=IntfVar2 as IBird;',
  20975. ' ObjVar:=IntfVar2 as TBird;',
  20976. ' IntfVar:=ObjVar as IBird;',
  20977. ' IntfVar:=IBird(IntfVar2);',
  20978. ' ObjVar:=TBird(IntfVar);',
  20979. ' IntfVar:=IBird(ObjVar);',
  20980. ' v:=IntfVar;',
  20981. ' IntfVar:=IBird(v);',
  20982. ' if v is IBird then ;',
  20983. ' v:=JSValue(IntfVar);',
  20984. ' v:=IBird;',
  20985. '']);
  20986. ConvertProgram;
  20987. CheckSource('TestClassInterface_Corba_Operators',
  20988. LinesToStr([ // statements
  20989. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20990. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  20991. 'rtl.createClass(this, "TObject", null, function () {',
  20992. ' this.$init = function () {',
  20993. ' };',
  20994. ' this.$final = function () {',
  20995. ' };',
  20996. '});',
  20997. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20998. ' rtl.addIntf(this, $mod.IBird);',
  20999. '});',
  21000. 'this.IntfVar = null;',
  21001. 'this.IntfVar2 = null;',
  21002. 'this.ObjVar = null;',
  21003. 'this.v = undefined;',
  21004. '']),
  21005. LinesToStr([ // $mod.$main
  21006. '$mod.IntfVar = null;',
  21007. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  21008. 'if ($mod.IntfVar != null) ;',
  21009. '$mod.IntfVar = $mod.IntfVar2;',
  21010. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  21011. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  21012. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  21013. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  21014. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  21015. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  21016. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  21017. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  21018. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  21019. '$mod.IntfVar = $mod.IntfVar2;',
  21020. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  21021. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  21022. '$mod.v = $mod.IntfVar;',
  21023. '$mod.IntfVar = rtl.getObject($mod.v);',
  21024. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  21025. '$mod.v = $mod.IntfVar;',
  21026. '$mod.v = $mod.IBird;',
  21027. '']));
  21028. end;
  21029. procedure TTestModule.TestClassInterface_Corba_Args;
  21030. begin
  21031. StartProgram(false);
  21032. Add([
  21033. '{$interfaces corba}',
  21034. 'type',
  21035. ' IUnknown = interface',
  21036. ' end;',
  21037. ' IBird = interface(IUnknown)',
  21038. ' end;',
  21039. ' TObject = class',
  21040. ' end;',
  21041. ' TBird = class(TObject,IBird)',
  21042. ' end;',
  21043. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  21044. 'begin',
  21045. ' DoIt(i,i,i);',
  21046. 'end;',
  21047. 'procedure Change(var i: IBird; out j: IBird);',
  21048. 'begin',
  21049. ' DoIt(i,i,i);',
  21050. ' Change(i,i);',
  21051. 'end;',
  21052. 'var',
  21053. ' i: IBird;',
  21054. ' o: TBird;',
  21055. 'begin',
  21056. ' DoIt(i,i,i);',
  21057. ' Change(i,i);',
  21058. ' DoIt(o,o,o);',
  21059. '']);
  21060. ConvertProgram;
  21061. CheckSource('TestClassInterface_Corba_Args',
  21062. LinesToStr([ // statements
  21063. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21064. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  21065. 'rtl.createClass(this, "TObject", null, function () {',
  21066. ' this.$init = function () {',
  21067. ' };',
  21068. ' this.$final = function () {',
  21069. ' };',
  21070. '});',
  21071. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21072. ' rtl.addIntf(this, $mod.IBird);',
  21073. '});',
  21074. 'this.DoIt = function (u, i, j) {',
  21075. ' $mod.DoIt({',
  21076. ' get: function () {',
  21077. ' return i;',
  21078. ' },',
  21079. ' set: function (v) {',
  21080. ' i = v;',
  21081. ' }',
  21082. ' }, i, i);',
  21083. '};',
  21084. 'this.Change = function (i, j) {',
  21085. ' $mod.DoIt(i, i.get(), i.get());',
  21086. ' $mod.Change(i, i);',
  21087. '};',
  21088. 'this.i = null;',
  21089. 'this.o = null;',
  21090. '']),
  21091. LinesToStr([ // $mod.$main
  21092. '$mod.DoIt({',
  21093. ' p: $mod,',
  21094. ' get: function () {',
  21095. ' return this.p.i;',
  21096. ' },',
  21097. ' set: function (v) {',
  21098. ' this.p.i = v;',
  21099. ' }',
  21100. '}, $mod.i, $mod.i);',
  21101. '$mod.Change({',
  21102. ' p: $mod,',
  21103. ' get: function () {',
  21104. ' return this.p.i;',
  21105. ' },',
  21106. ' set: function (v) {',
  21107. ' this.p.i = v;',
  21108. ' }',
  21109. '}, {',
  21110. ' p: $mod,',
  21111. ' get: function () {',
  21112. ' return this.p.i;',
  21113. ' },',
  21114. ' set: function (v) {',
  21115. ' this.p.i = v;',
  21116. ' }',
  21117. '});',
  21118. '$mod.DoIt({',
  21119. ' p: $mod,',
  21120. ' get: function () {',
  21121. ' return this.p.o;',
  21122. ' },',
  21123. ' set: function (v) {',
  21124. ' this.p.o = v;',
  21125. ' }',
  21126. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  21127. '']));
  21128. end;
  21129. procedure TTestModule.TestClassInterface_Corba_ForIn;
  21130. begin
  21131. StartProgram(false);
  21132. Add([
  21133. '{$interfaces corba}',
  21134. 'type',
  21135. ' IUnknown = interface end;',
  21136. ' TObject = class',
  21137. ' Id: longint;',
  21138. ' end;',
  21139. ' IEnumerator = interface(IUnknown)',
  21140. ' function GetCurrent: TObject;',
  21141. ' function MoveNext: Boolean;',
  21142. ' property Current: TObject read GetCurrent;',
  21143. ' end;',
  21144. ' IEnumerable = interface(IUnknown)',
  21145. ' function GetEnumerator: IEnumerator;',
  21146. ' end;',
  21147. 'var',
  21148. ' o: TObject;',
  21149. ' i: IEnumerable;',
  21150. 'begin',
  21151. ' for o in i do o.Id:=3;',
  21152. '']);
  21153. ConvertProgram;
  21154. CheckSource('TestClassInterface_Corba_ForIn',
  21155. LinesToStr([ // statements
  21156. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21157. 'rtl.createClass(this, "TObject", null, function () {',
  21158. ' this.$init = function () {',
  21159. ' this.Id = 0;',
  21160. ' };',
  21161. ' this.$final = function () {',
  21162. ' };',
  21163. '});',
  21164. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  21165. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  21166. 'this.o = null;',
  21167. 'this.i = null;',
  21168. '']),
  21169. LinesToStr([ // $mod.$main
  21170. 'var $in = $mod.i.GetEnumerator();',
  21171. 'while ($in.MoveNext()) {',
  21172. ' $mod.o = $in.GetCurrent();',
  21173. ' $mod.o.Id = 3;',
  21174. '};',
  21175. '']));
  21176. end;
  21177. procedure TTestModule.TestClassInterface_Corba_ArrayOfIntf;
  21178. begin
  21179. StartProgram(false);
  21180. Add([
  21181. '{$interfaces corba}',
  21182. 'type',
  21183. ' IUnknown = interface end;',
  21184. ' IBird = interface(IUnknown)',
  21185. ' function Fly(w: word): word;',
  21186. ' end;',
  21187. ' TBirdArray = array of IBird;',
  21188. 'var',
  21189. ' i: IBird;',
  21190. ' a: TBirdArray;',
  21191. 'begin',
  21192. ' SetLength(a,3);',
  21193. ' i:=a[1];',
  21194. ' a[2]:=i;',
  21195. ' for i in a do i.fly(3);',
  21196. '']);
  21197. ConvertProgram;
  21198. CheckSource('TestClassInterface_Corba_ArrayOfIntf',
  21199. LinesToStr([ // statements
  21200. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  21201. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  21202. 'this.i = null;',
  21203. 'this.a = [];',
  21204. '']),
  21205. LinesToStr([ // $mod.$main
  21206. '$mod.a = rtl.arraySetLength($mod.a, null, 3);',
  21207. '$mod.i = $mod.a[1];',
  21208. '$mod.a[2] = $mod.i;',
  21209. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  21210. ' $mod.i = $in[$l];',
  21211. ' $mod.i.Fly(3);',
  21212. '};',
  21213. '']));
  21214. end;
  21215. procedure TTestModule.TestClassInterface_COM_AssignVar;
  21216. begin
  21217. StartProgram(false);
  21218. Add([
  21219. '{$interfaces com}',
  21220. 'type',
  21221. ' IUnknown = interface',
  21222. ' function _AddRef: longint;',
  21223. ' function _Release: longint;',
  21224. ' end;',
  21225. ' TObject = class(IUnknown)',
  21226. ' function _AddRef: longint; virtual; abstract;',
  21227. ' function _Release: longint; virtual; abstract;',
  21228. ' end;',
  21229. 'var',
  21230. ' i: IUnknown;',
  21231. 'procedure DoGlobal(o: TObject);',
  21232. 'begin',
  21233. ' i:=nil;',
  21234. ' i:=o;',
  21235. ' i:=i;',
  21236. 'end;',
  21237. 'procedure DoLocal(o: TObject);',
  21238. 'const k: IUnknown = nil;',
  21239. 'var j: IUnknown;',
  21240. 'begin',
  21241. ' k:=o;',
  21242. ' k:=i;',
  21243. ' j:=o;',
  21244. ' j:=i;',
  21245. 'end;',
  21246. 'var o: TObject;',
  21247. 'begin',
  21248. ' i:=nil;',
  21249. ' i:=o;',
  21250. '']);
  21251. ConvertProgram;
  21252. CheckSource('TestClassInterface_COM_AssignVar',
  21253. LinesToStr([ // statements
  21254. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21255. 'rtl.createClass(this, "TObject", null, function () {',
  21256. ' this.$init = function () {',
  21257. ' };',
  21258. ' this.$final = function () {',
  21259. ' };',
  21260. ' rtl.addIntf(this, $mod.IUnknown);',
  21261. '});',
  21262. 'this.i = null;',
  21263. 'this.DoGlobal = function (o) {',
  21264. ' rtl.setIntfP($mod, "i", null);',
  21265. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  21266. ' rtl.setIntfP($mod, "i", $mod.i);',
  21267. '};',
  21268. 'var k = null;',
  21269. 'this.DoLocal = function (o) {',
  21270. ' var j = null;',
  21271. ' try{',
  21272. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21273. ' k = rtl.setIntfL(k, $mod.i);',
  21274. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21275. ' j = rtl.setIntfL(j, $mod.i);',
  21276. ' }finally{',
  21277. ' rtl._Release(j);',
  21278. ' };',
  21279. '};',
  21280. 'this.o = null;',
  21281. '']),
  21282. LinesToStr([ // $mod.$main
  21283. 'rtl.setIntfP($mod, "i", null);',
  21284. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  21285. '']));
  21286. end;
  21287. procedure TTestModule.TestClassInterface_COM_AssignArg;
  21288. begin
  21289. StartProgram(false);
  21290. Add([
  21291. '{$interfaces com}',
  21292. 'type',
  21293. ' IUnknown = interface',
  21294. ' function _AddRef: longint;',
  21295. ' function _Release: longint;',
  21296. ' end;',
  21297. ' TObject = class(IUnknown)',
  21298. ' function _AddRef: longint; virtual; abstract;',
  21299. ' function _Release: longint; virtual; abstract;',
  21300. ' end;',
  21301. 'procedure DoDefault(i, j: IUnknown);',
  21302. 'begin',
  21303. ' i:=nil;',
  21304. ' i:=j;',
  21305. 'end;',
  21306. 'begin',
  21307. '']);
  21308. ConvertProgram;
  21309. CheckSource('TestClassInterface_COM_AssignArg',
  21310. LinesToStr([ // statements
  21311. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21312. 'rtl.createClass(this, "TObject", null, function () {',
  21313. ' this.$init = function () {',
  21314. ' };',
  21315. ' this.$final = function () {',
  21316. ' };',
  21317. ' rtl.addIntf(this, $mod.IUnknown);',
  21318. '});',
  21319. 'this.DoDefault = function (i, j) {',
  21320. ' rtl._AddRef(i);',
  21321. ' try {',
  21322. ' i = rtl.setIntfL(i, null);',
  21323. ' i = rtl.setIntfL(i, j);',
  21324. ' } finally {',
  21325. ' rtl._Release(i);',
  21326. ' };',
  21327. '};',
  21328. '']),
  21329. LinesToStr([ // $mod.$main
  21330. '']));
  21331. end;
  21332. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  21333. begin
  21334. StartProgram(false);
  21335. Add([
  21336. '{$interfaces com}',
  21337. 'type',
  21338. ' IUnknown = interface',
  21339. ' function _AddRef: longint;',
  21340. ' function _Release: longint;',
  21341. ' end;',
  21342. ' TObject = class(IUnknown)',
  21343. ' function _AddRef: longint; virtual; abstract;',
  21344. ' function _Release: longint; virtual; abstract;',
  21345. ' end;',
  21346. 'function DoDefault(i: IUnknown): IUnknown;',
  21347. 'begin',
  21348. ' Result:=i;',
  21349. ' if Result<>nil then exit;',
  21350. 'end;',
  21351. 'begin',
  21352. '']);
  21353. ConvertProgram;
  21354. CheckSource('TestClassInterface_COM_FunctionResult',
  21355. LinesToStr([ // statements
  21356. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21357. 'rtl.createClass(this, "TObject", null, function () {',
  21358. ' this.$init = function () {',
  21359. ' };',
  21360. ' this.$final = function () {',
  21361. ' };',
  21362. ' rtl.addIntf(this, $mod.IUnknown);',
  21363. '});',
  21364. 'this.DoDefault = function (i) {',
  21365. ' var Result = null;',
  21366. ' var $ok = false;',
  21367. ' try {',
  21368. ' Result = rtl.setIntfL(Result, i);',
  21369. ' if(Result !== null){',
  21370. ' $ok = true;',
  21371. ' return Result;',
  21372. ' };',
  21373. ' $ok = true;',
  21374. ' } finally {',
  21375. ' if(!$ok) rtl._Release(Result);',
  21376. ' };',
  21377. ' return Result;',
  21378. '};',
  21379. '']),
  21380. LinesToStr([ // $mod.$main
  21381. '']));
  21382. end;
  21383. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  21384. begin
  21385. StartProgram(false);
  21386. Add([
  21387. '{$interfaces com}',
  21388. 'type',
  21389. ' IUnknown = interface',
  21390. ' function _AddRef: longint;',
  21391. ' function _Release: longint;',
  21392. ' end;',
  21393. ' TObject = class(IUnknown)',
  21394. ' function _AddRef: longint; virtual; abstract;',
  21395. ' function _Release: longint; virtual; abstract;',
  21396. ' function GetIntf: IUnknown; virtual;',
  21397. ' end;',
  21398. ' TMouse = class',
  21399. ' function GetIntf: IUnknown; override;',
  21400. ' end;',
  21401. 'function TObject.GetIntf: IUnknown; begin end;',
  21402. 'function TMouse.GetIntf: IUnknown;',
  21403. 'var i: IUnknown;',
  21404. 'begin',
  21405. ' inherited;',
  21406. ' inherited GetIntf;',
  21407. ' inherited GetIntf();',
  21408. ' Result:=inherited GetIntf;',
  21409. ' Result:=inherited GetIntf();',
  21410. ' i:=inherited GetIntf;',
  21411. ' i:=inherited GetIntf();',
  21412. 'end;',
  21413. 'begin',
  21414. '']);
  21415. ConvertProgram;
  21416. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  21417. LinesToStr([ // statements
  21418. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21419. 'rtl.createClass(this, "TObject", null, function () {',
  21420. ' this.$init = function () {',
  21421. ' };',
  21422. ' this.$final = function () {',
  21423. ' };',
  21424. ' this.GetIntf = function () {',
  21425. ' var Result = null;',
  21426. ' return Result;',
  21427. ' };',
  21428. ' rtl.addIntf(this, $mod.IUnknown);',
  21429. '});',
  21430. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  21431. ' this.GetIntf = function () {',
  21432. ' var Result = null;',
  21433. ' var i = null;',
  21434. ' var $ir = rtl.createIntfRefs();',
  21435. ' var $ok = false;',
  21436. ' try {',
  21437. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  21438. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  21439. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  21440. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21441. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  21442. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21443. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  21444. ' $ok = true;',
  21445. ' } finally {',
  21446. ' $ir.free();',
  21447. ' rtl._Release(i);',
  21448. ' if (!$ok) rtl._Release(Result);',
  21449. ' };',
  21450. ' return Result;',
  21451. ' };',
  21452. ' rtl.addIntf(this, $mod.IUnknown);',
  21453. '});',
  21454. '']),
  21455. LinesToStr([ // $mod.$main
  21456. '']));
  21457. end;
  21458. procedure TTestModule.TestClassInterface_COM_FunctionExit;
  21459. begin
  21460. StartProgram(false);
  21461. Add([
  21462. '{$interfaces com}',
  21463. 'type',
  21464. ' IUnknown = interface',
  21465. ' function _AddRef: longint;',
  21466. ' function _Release: longint;',
  21467. ' end;',
  21468. ' TObject = class(IUnknown)',
  21469. ' function _AddRef: longint; virtual; abstract;',
  21470. ' function _Release: longint; virtual; abstract;',
  21471. ' constructor Create;',
  21472. ' end;',
  21473. 'constructor TObject.Create;',
  21474. 'begin',
  21475. 'end;',
  21476. 'function GetIntf: IUnknown;',
  21477. 'var Intf: IUnknown;',
  21478. 'begin',
  21479. ' Intf := TObject.Create;',
  21480. ' Exit(Intf);',
  21481. 'end;',
  21482. 'begin',
  21483. '']);
  21484. ConvertProgram;
  21485. CheckSource('TestClassInterface_COM_FunctionExit',
  21486. LinesToStr([ // statements
  21487. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21488. 'rtl.createClass(this, "TObject", null, function () {',
  21489. ' this.$init = function () {',
  21490. ' };',
  21491. ' this.$final = function () {',
  21492. ' };',
  21493. ' this.Create = function () {',
  21494. ' return this;',
  21495. ' };',
  21496. ' rtl.addIntf(this, $mod.IUnknown);',
  21497. '});',
  21498. 'this.GetIntf = function () {',
  21499. ' var Result = null;',
  21500. ' var Intf = null;',
  21501. ' var $ok = false;',
  21502. ' try {',
  21503. ' Intf = rtl.setIntfL(Intf, rtl.queryIntfT($mod.TObject.$create("Create"), $mod.IUnknown), true);',
  21504. ' $ok = true;',
  21505. ' Result = rtl.setIntfL(Result, Intf);',
  21506. ' return Result;',
  21507. ' $ok = true;',
  21508. ' } finally {',
  21509. ' rtl._Release(Intf);',
  21510. ' if (!$ok) rtl._Release(Result);',
  21511. ' };',
  21512. ' return Result;',
  21513. '};',
  21514. '']),
  21515. LinesToStr([ // $mod.$main
  21516. '']));
  21517. end;
  21518. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  21519. begin
  21520. StartProgram(false);
  21521. Add([
  21522. '{$interfaces com}',
  21523. 'type',
  21524. ' IUnknown = interface',
  21525. ' function _AddRef: longint;',
  21526. ' function _Release: longint;',
  21527. ' end;',
  21528. ' TObject = class(IUnknown)',
  21529. ' function _AddRef: longint; virtual; abstract;',
  21530. ' function _Release: longint; virtual; abstract;',
  21531. ' end;',
  21532. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  21533. 'begin',
  21534. ' if i is IUnknown then ;',
  21535. ' if o is IUnknown then ;',
  21536. ' if i is TObject then ;',
  21537. ' i:=j as IUnknown;',
  21538. ' i:=o as IUnknown;',
  21539. ' o:=j as TObject;',
  21540. ' i:=IUnknown(j);',
  21541. ' i:=IUnknown(o);',
  21542. ' o:=TObject(i);',
  21543. 'end;',
  21544. 'begin',
  21545. '']);
  21546. ConvertProgram;
  21547. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  21548. LinesToStr([ // statements
  21549. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21550. 'rtl.createClass(this, "TObject", null, function () {',
  21551. ' this.$init = function () {',
  21552. ' };',
  21553. ' this.$final = function () {',
  21554. ' };',
  21555. ' rtl.addIntf(this, $mod.IUnknown);',
  21556. '});',
  21557. 'this.DoDefault = function (i, j, o) {',
  21558. ' rtl._AddRef(i);',
  21559. ' try {',
  21560. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  21561. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  21562. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  21563. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  21564. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21565. ' o = rtl.intfAsClass(j, $mod.TObject);',
  21566. ' i = rtl.setIntfL(i, j);',
  21567. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  21568. ' o = rtl.intfToClass(i, $mod.TObject);',
  21569. ' } finally {',
  21570. ' rtl._Release(i);',
  21571. ' };',
  21572. '};',
  21573. '']),
  21574. LinesToStr([ // $mod.$main
  21575. '']));
  21576. end;
  21577. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  21578. begin
  21579. StartProgram(false);
  21580. Add([
  21581. '{$interfaces com}',
  21582. 'type',
  21583. ' IUnknown = interface',
  21584. ' function _AddRef: longint;',
  21585. ' function _Release: longint;',
  21586. ' end;',
  21587. ' TObject = class(IUnknown)',
  21588. ' function _AddRef: longint; virtual; abstract;',
  21589. ' function _Release: longint; virtual; abstract;',
  21590. ' end;',
  21591. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  21592. 'var o: TObject;',
  21593. 'begin',
  21594. ' DoIt(v,v,v,v);',
  21595. ' DoIt(o,o,k,k);',
  21596. 'end;',
  21597. 'procedure DoSome;',
  21598. 'var v: IUnknown;',
  21599. 'begin',
  21600. ' DoIt(v,v,v,v);',
  21601. 'end;',
  21602. 'var i: IUnknown;',
  21603. 'begin',
  21604. ' DoIt(i,i,i,i);',
  21605. '']);
  21606. ConvertProgram;
  21607. CheckSource('TestClassInterface_COM_PassAsArg',
  21608. LinesToStr([ // statements
  21609. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21610. 'rtl.createClass(this, "TObject", null, function () {',
  21611. ' this.$init = function () {',
  21612. ' };',
  21613. ' this.$final = function () {',
  21614. ' };',
  21615. ' rtl.addIntf(this, $mod.IUnknown);',
  21616. '});',
  21617. 'this.DoIt = function (v, j, k, l) {',
  21618. ' var o = null;',
  21619. ' var $ir = rtl.createIntfRefs();',
  21620. ' rtl._AddRef(v);',
  21621. ' try {',
  21622. ' $mod.DoIt(v, v, {',
  21623. ' get: function () {',
  21624. ' return v;',
  21625. ' },',
  21626. ' set: function (w) {',
  21627. ' v = rtl.setIntfL(v, w);',
  21628. ' }',
  21629. ' }, {',
  21630. ' get: function () {',
  21631. ' return v;',
  21632. ' },',
  21633. ' set: function (w) {',
  21634. ' v = rtl.setIntfL(v, w);',
  21635. ' }',
  21636. ' });',
  21637. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  21638. ' } finally {',
  21639. ' $ir.free();',
  21640. ' rtl._Release(v);',
  21641. ' };',
  21642. '};',
  21643. 'this.DoSome = function () {',
  21644. ' var v = null;',
  21645. ' try {',
  21646. ' $mod.DoIt(v, v, {',
  21647. ' get: function () {',
  21648. ' return v;',
  21649. ' },',
  21650. ' set: function (w) {',
  21651. ' v = rtl.setIntfL(v, w);',
  21652. ' }',
  21653. ' }, {',
  21654. ' get: function () {',
  21655. ' return v;',
  21656. ' },',
  21657. ' set: function (w) {',
  21658. ' v = rtl.setIntfL(v, w);',
  21659. ' }',
  21660. ' });',
  21661. ' } finally {',
  21662. ' rtl._Release(v);',
  21663. ' };',
  21664. '};',
  21665. 'this.i = null;',
  21666. '']),
  21667. LinesToStr([ // $mod.$main
  21668. '$mod.DoIt($mod.i, $mod.i, {',
  21669. ' p: $mod,',
  21670. ' get: function () {',
  21671. ' return this.p.i;',
  21672. ' },',
  21673. ' set: function (v) {',
  21674. ' rtl.setIntfP(this.p, "i", v);',
  21675. ' }',
  21676. '}, {',
  21677. ' p: $mod,',
  21678. ' get: function () {',
  21679. ' return this.p.i;',
  21680. ' },',
  21681. ' set: function (v) {',
  21682. ' rtl.setIntfP(this.p, "i", v);',
  21683. ' }',
  21684. '});',
  21685. '']));
  21686. end;
  21687. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  21688. begin
  21689. StartProgram(false);
  21690. Add([
  21691. '{$interfaces com}',
  21692. 'type',
  21693. ' IUnknown = interface',
  21694. ' function _AddRef: longint;',
  21695. ' function _Release: longint;',
  21696. ' end;',
  21697. ' TObject = class(IUnknown)',
  21698. ' function _AddRef: longint; virtual; abstract;',
  21699. ' function _Release: longint; virtual; abstract;',
  21700. ' end;',
  21701. 'procedure DoIt(out i);',
  21702. 'begin end;',
  21703. 'procedure DoSome;',
  21704. 'var v: IUnknown;',
  21705. 'begin',
  21706. ' DoIt(v);',
  21707. 'end;',
  21708. 'function GetIt: IUnknown;',
  21709. 'begin',
  21710. ' DoIt(Result);',
  21711. 'end;',
  21712. 'var i: IUnknown;',
  21713. 'begin',
  21714. ' DoIt(i);',
  21715. '']);
  21716. ConvertProgram;
  21717. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  21718. LinesToStr([ // statements
  21719. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21720. 'rtl.createClass(this, "TObject", null, function () {',
  21721. ' this.$init = function () {',
  21722. ' };',
  21723. ' this.$final = function () {',
  21724. ' };',
  21725. ' rtl.addIntf(this, $mod.IUnknown);',
  21726. '});',
  21727. 'this.DoIt = function (i) {',
  21728. '};',
  21729. 'this.DoSome = function () {',
  21730. ' var v = null;',
  21731. ' try {',
  21732. ' $mod.DoIt({',
  21733. ' get: function () {',
  21734. ' return v;',
  21735. ' },',
  21736. ' set: function (w) {',
  21737. ' v = w;',
  21738. ' }',
  21739. ' });',
  21740. ' } finally {',
  21741. ' rtl._Release(v);',
  21742. ' };',
  21743. '};',
  21744. 'this.GetIt = function () {',
  21745. ' var Result = null;',
  21746. ' var $ok = false;',
  21747. ' try {',
  21748. ' $mod.DoIt({',
  21749. ' get: function () {',
  21750. ' return Result;',
  21751. ' },',
  21752. ' set: function (v) {',
  21753. ' Result = v;',
  21754. ' }',
  21755. ' });',
  21756. ' $ok = true;',
  21757. ' } finally {',
  21758. ' if (!$ok) rtl._Release(Result);',
  21759. ' };',
  21760. ' return Result;',
  21761. '};',
  21762. 'this.i = null;',
  21763. '']),
  21764. LinesToStr([ // $mod.$main
  21765. 'try {',
  21766. ' $mod.DoIt({',
  21767. ' p: $mod,',
  21768. ' get: function () {',
  21769. ' return this.p.i;',
  21770. ' },',
  21771. ' set: function (v) {',
  21772. ' this.p.i = v;',
  21773. ' }',
  21774. ' });',
  21775. '} finally {',
  21776. ' rtl._Release($mod.i);',
  21777. '};',
  21778. '']));
  21779. end;
  21780. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  21781. begin
  21782. StartProgram(false);
  21783. Add([
  21784. '{$interfaces com}',
  21785. 'type',
  21786. ' IUnknown = interface',
  21787. ' function _AddRef: longint;',
  21788. ' function _Release: longint;',
  21789. ' end;',
  21790. ' TObject = class(IUnknown)',
  21791. ' function _AddRef: longint; virtual; abstract;',
  21792. ' function _Release: longint; virtual; abstract;',
  21793. ' end;',
  21794. 'function GetIt: IUnknown;',
  21795. 'begin',
  21796. 'end;',
  21797. 'procedure DoSome;',
  21798. 'var v: IUnknown;',
  21799. ' i: longint;',
  21800. 'begin',
  21801. ' v:=GetIt;',
  21802. ' v:=GetIt();',
  21803. ' GetIt()._AddRef;',
  21804. ' i:=GetIt()._AddRef;',
  21805. 'end;',
  21806. 'var v: IUnknown;',
  21807. ' i: longint;',
  21808. 'begin',
  21809. ' v:=GetIt;',
  21810. ' v:=GetIt();',
  21811. ' GetIt()._AddRef;',
  21812. ' i:=GetIt()._AddRef;',
  21813. '']);
  21814. ConvertProgram;
  21815. CheckSource('TestClassInterface_COM_FunctionInExpr',
  21816. LinesToStr([ // statements
  21817. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21818. 'rtl.createClass(this, "TObject", null, function () {',
  21819. ' this.$init = function () {',
  21820. ' };',
  21821. ' this.$final = function () {',
  21822. ' };',
  21823. ' rtl.addIntf(this, $mod.IUnknown);',
  21824. '});',
  21825. 'this.GetIt = function () {',
  21826. ' var Result = null;',
  21827. ' return Result;',
  21828. '};',
  21829. 'this.DoSome = function () {',
  21830. ' var v = null;',
  21831. ' var i = 0;',
  21832. ' var $ir = rtl.createIntfRefs();',
  21833. ' try {',
  21834. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21835. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  21836. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21837. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21838. ' } finally {',
  21839. ' $ir.free();',
  21840. ' rtl._Release(v);',
  21841. ' };',
  21842. '};',
  21843. 'this.v = null;',
  21844. 'this.i = 0;',
  21845. '']),
  21846. LinesToStr([ // $mod.$main
  21847. 'var $ir = rtl.createIntfRefs();',
  21848. 'try {',
  21849. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21850. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  21851. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  21852. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  21853. '} finally {',
  21854. ' $ir.free();',
  21855. '};',
  21856. '']));
  21857. end;
  21858. procedure TTestModule.TestClassInterface_COM_Property;
  21859. begin
  21860. StartProgram(false);
  21861. Add([
  21862. '{$interfaces com}',
  21863. 'type',
  21864. ' IUnknown = interface',
  21865. ' function _AddRef: longint;',
  21866. ' function _Release: longint;',
  21867. ' end;',
  21868. ' TObject = class(IUnknown)',
  21869. ' FAnt: IUnknown;',
  21870. ' function _AddRef: longint; virtual; abstract;',
  21871. ' function _Release: longint; virtual; abstract;',
  21872. ' function GetBird: IUnknown; virtual; abstract;',
  21873. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21874. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21875. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21876. ' property Ant: IUnknown read FAnt write FAnt;',
  21877. ' property Bird: IUnknown read GetBird write SetBird;',
  21878. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21879. ' end;',
  21880. 'procedure DoIt;',
  21881. 'var',
  21882. ' o: TObject;',
  21883. ' v: IUnknown;',
  21884. 'begin',
  21885. ' v:=o.Ant;',
  21886. ' o.Ant:=v;',
  21887. ' o.Ant:=o.Ant;',
  21888. ' v:=o.Bird;',
  21889. ' o.Bird:=v;',
  21890. ' o.Bird:=o.Bird;',
  21891. ' v:=o.Items[1];',
  21892. ' o.Items[2]:=v;',
  21893. ' o.Items[3]:=o.Items[4];',
  21894. ' v:=o[5];',
  21895. ' o[6]:=v;',
  21896. ' o[7]:=o[8];',
  21897. 'end;',
  21898. 'begin',
  21899. '']);
  21900. ConvertProgram;
  21901. CheckSource('TestClassInterface_COM_Property',
  21902. LinesToStr([ // statements
  21903. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  21904. 'rtl.createClass(this, "TObject", null, function () {',
  21905. ' this.$init = function () {',
  21906. ' this.FAnt = null;',
  21907. ' };',
  21908. ' this.$final = function () {',
  21909. ' rtl.setIntfP(this, "FAnt", null);',
  21910. ' };',
  21911. ' rtl.addIntf(this, $mod.IUnknown);',
  21912. '});',
  21913. 'this.DoIt = function () {',
  21914. ' var o = null;',
  21915. ' var v = null;',
  21916. ' var $ir = rtl.createIntfRefs();',
  21917. ' try {',
  21918. ' v = rtl.setIntfL(v, o.FAnt);',
  21919. ' rtl.setIntfP(o, "FAnt", v);',
  21920. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  21921. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  21922. ' o.SetBird(v);',
  21923. ' o.SetBird($ir.ref(1, o.GetBird()));',
  21924. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  21925. ' o.SetItems(2, v);',
  21926. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  21927. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  21928. ' o.SetItems(6, v);',
  21929. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  21930. ' } finally {',
  21931. ' $ir.free();',
  21932. ' rtl._Release(v);',
  21933. ' };',
  21934. '};',
  21935. '']),
  21936. LinesToStr([ // $mod.$main
  21937. '']));
  21938. end;
  21939. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  21940. begin
  21941. StartProgram(false);
  21942. Add([
  21943. '{$interfaces com}',
  21944. 'type',
  21945. ' IUnknown = interface',
  21946. ' function _AddRef: longint;',
  21947. ' function _Release: longint;',
  21948. ' function GetBird: IUnknown;',
  21949. ' procedure SetBird(Value: IUnknown);',
  21950. ' function GetItems(Index: longint): IUnknown;',
  21951. ' procedure SetItems(Index: longint; Value: IUnknown);',
  21952. ' property Bird: IUnknown read GetBird write SetBird;',
  21953. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  21954. ' end;',
  21955. ' TObject = class(IUnknown)',
  21956. ' function _AddRef: longint; virtual; abstract;',
  21957. ' function _Release: longint; virtual; abstract;',
  21958. ' function GetBird: IUnknown; virtual; abstract;',
  21959. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  21960. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  21961. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  21962. ' end;',
  21963. 'procedure DoIt;',
  21964. 'var',
  21965. ' o: TObject;',
  21966. ' v: IUnknown;',
  21967. 'begin',
  21968. ' v:=v.Items[1];',
  21969. ' v.Items[2]:=v;',
  21970. ' v.Items[3]:=v.Items[4];',
  21971. ' v:=v[5];',
  21972. ' v[6]:=v;',
  21973. ' v[7]:=v[8];',
  21974. ' v[9].Bird.Bird:=v;',
  21975. ' v:=v.Bird[10].Bird',
  21976. 'end;',
  21977. 'begin',
  21978. '']);
  21979. ConvertProgram;
  21980. CheckSource('TestClassInterface_COM_IntfProperty',
  21981. LinesToStr([ // statements
  21982. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  21983. ' "_AddRef",',
  21984. ' "_Release",',
  21985. ' "GetBird",',
  21986. ' "SetBird",',
  21987. ' "GetItems",',
  21988. ' "SetItems"',
  21989. '], null);',
  21990. 'rtl.createClass(this, "TObject", null, function () {',
  21991. ' this.$init = function () {',
  21992. ' };',
  21993. ' this.$final = function () {',
  21994. ' };',
  21995. ' rtl.addIntf(this, $mod.IUnknown);',
  21996. '});',
  21997. 'this.DoIt = function () {',
  21998. ' var o = null;',
  21999. ' var v = null;',
  22000. ' var $ir = rtl.createIntfRefs();',
  22001. ' try {',
  22002. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  22003. ' v.SetItems(2, v);',
  22004. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  22005. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  22006. ' v.SetItems(6, v);',
  22007. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  22008. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  22009. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  22010. ' } finally {',
  22011. ' $ir.free();',
  22012. ' rtl._Release(v);',
  22013. ' };',
  22014. '};',
  22015. '']),
  22016. LinesToStr([ // $mod.$main
  22017. '']));
  22018. end;
  22019. procedure TTestModule.TestClassInterface_COM_Delegation;
  22020. begin
  22021. StartProgram(false);
  22022. Add([
  22023. '{$interfaces com}',
  22024. 'type',
  22025. ' IUnknown = interface',
  22026. ' function _AddRef: longint;',
  22027. ' function _Release: longint;',
  22028. ' end;',
  22029. ' IBird = interface(IUnknown)',
  22030. ' procedure Fly(s: string);',
  22031. ' end;',
  22032. ' IEagle = interface(IBird) end;',
  22033. ' IDove = interface(IBird) end;',
  22034. ' ISwallow = interface(IBird) end;',
  22035. ' TObject = class',
  22036. ' end;',
  22037. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  22038. ' function _AddRef: longint; virtual; abstract;',
  22039. ' function _Release: longint; virtual; abstract;',
  22040. ' procedure Fly(s: string); virtual; abstract;',
  22041. ' end;',
  22042. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  22043. ' function _AddRef: longint; virtual; abstract;',
  22044. ' function _Release: longint; virtual; abstract;',
  22045. ' FBirdIntf: IBird;',
  22046. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  22047. ' function GetEagleIntf: IEagle; virtual; abstract;',
  22048. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  22049. ' FDoveObj: TBird;',
  22050. ' property DoveObj: TBird read FDoveObj implements IDove;',
  22051. ' function GetSwallowObj: TBird; virtual; abstract;',
  22052. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  22053. ' end;',
  22054. 'begin',
  22055. '']);
  22056. ConvertProgram;
  22057. CheckSource('TestClassInterface_COM_Delegation',
  22058. LinesToStr([ // statements
  22059. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  22060. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  22061. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  22062. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  22063. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  22064. 'rtl.createClass(this, "TObject", null, function () {',
  22065. ' this.$init = function () {',
  22066. ' };',
  22067. ' this.$final = function () {',
  22068. ' };',
  22069. '});',
  22070. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22071. ' rtl.addIntf(this, $mod.IBird);',
  22072. ' rtl.addIntf(this, $mod.IEagle);',
  22073. ' rtl.addIntf(this, $mod.IDove);',
  22074. ' rtl.addIntf(this, $mod.ISwallow);',
  22075. '});',
  22076. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  22077. ' this.$init = function () {',
  22078. ' $mod.TObject.$init.call(this);',
  22079. ' this.FBirdIntf = null;',
  22080. ' this.FDoveObj = null;',
  22081. ' };',
  22082. ' this.$final = function () {',
  22083. ' rtl.setIntfP(this, "FBirdIntf", null);',
  22084. ' this.FDoveObj = undefined;',
  22085. ' $mod.TObject.$final.call(this);',
  22086. ' };',
  22087. ' this.$intfmaps = {',
  22088. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  22089. ' return rtl._AddRef(this.FBirdIntf);',
  22090. ' },',
  22091. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  22092. ' return this.GetEagleIntf();',
  22093. ' },',
  22094. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  22095. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  22096. ' },',
  22097. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  22098. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  22099. ' }',
  22100. ' };',
  22101. '});',
  22102. '']),
  22103. LinesToStr([ // $mod.$main
  22104. '']));
  22105. end;
  22106. procedure TTestModule.TestClassInterface_COM_With;
  22107. begin
  22108. StartProgram(false);
  22109. Add([
  22110. '{$interfaces com}',
  22111. 'type',
  22112. ' IUnknown = interface',
  22113. ' function _AddRef: longint;',
  22114. ' function _Release: longint;',
  22115. ' function GetAnt: IUnknown;',
  22116. ' property Ant: IUnknown read GetAnt;',
  22117. ' end;',
  22118. ' TObject = class(IUnknown)',
  22119. ' function _AddRef: longint; virtual; abstract;',
  22120. ' function _Release: longint; virtual; abstract;',
  22121. ' function GetAnt: IUnknown; virtual; abstract;',
  22122. ' property Ant: IUnknown read GetAnt;',
  22123. ' end;',
  22124. 'procedure DoIt;',
  22125. 'var',
  22126. ' i: IUnknown;',
  22127. 'begin',
  22128. ' with i do ',
  22129. ' GetAnt;',
  22130. ' with i.Ant, Ant do ',
  22131. ' GetAnt;',
  22132. 'end;',
  22133. 'begin',
  22134. '']);
  22135. ConvertProgram;
  22136. CheckSource('TestClassInterface_COM_With',
  22137. LinesToStr([ // statements
  22138. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  22139. 'rtl.createClass(this, "TObject", null, function () {',
  22140. ' this.$init = function () {',
  22141. ' };',
  22142. ' this.$final = function () {',
  22143. ' };',
  22144. ' rtl.addIntf(this, $mod.IUnknown);',
  22145. '});',
  22146. 'this.DoIt = function () {',
  22147. ' var i = null;',
  22148. ' var $ir = rtl.createIntfRefs();',
  22149. ' try {',
  22150. ' $ir.ref(1, i.GetAnt());',
  22151. ' var $with = $ir.ref(2, i.GetAnt());',
  22152. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  22153. ' $ir.ref(4, $with1.GetAnt());',
  22154. ' } finally {',
  22155. ' $ir.free();',
  22156. ' };',
  22157. '};',
  22158. '']),
  22159. LinesToStr([ // $mod.$main
  22160. '']));
  22161. end;
  22162. procedure TTestModule.TestClassInterface_COM_ForObjectInInterface;
  22163. begin
  22164. StartProgram(false);
  22165. Add([
  22166. '{$interfaces com}',
  22167. 'type',
  22168. ' IUnknown = interface end;',
  22169. ' TObject = class',
  22170. ' Id: longint;',
  22171. ' end;',
  22172. ' IEnumerator = interface(IUnknown)',
  22173. ' function GetCurrent: TObject;',
  22174. ' function MoveNext: Boolean;',
  22175. ' property Current: TObject read GetCurrent;',
  22176. ' end;',
  22177. ' IEnumerable = interface(IUnknown)',
  22178. ' function GetEnumerator: IEnumerator;',
  22179. ' end;',
  22180. 'var',
  22181. ' o: TObject;',
  22182. ' i: IEnumerable;',
  22183. 'begin',
  22184. ' for o in i do o.Id:=3;',
  22185. '']);
  22186. ConvertProgram;
  22187. CheckSource('TestClassInterface_COM_ForObjectInInterface',
  22188. LinesToStr([ // statements
  22189. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22190. 'rtl.createClass(this, "TObject", null, function () {',
  22191. ' this.$init = function () {',
  22192. ' this.Id = 0;',
  22193. ' };',
  22194. ' this.$final = function () {',
  22195. ' };',
  22196. '});',
  22197. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  22198. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  22199. 'this.o = null;',
  22200. 'this.i = null;',
  22201. '']),
  22202. LinesToStr([ // $mod.$main
  22203. 'var $in = $mod.i.GetEnumerator();',
  22204. 'try {',
  22205. ' while ($in.MoveNext()) {',
  22206. ' $mod.o = $in.GetCurrent();',
  22207. ' $mod.o.Id = 3;',
  22208. ' }',
  22209. '} finally {',
  22210. ' rtl._Release($in)',
  22211. '};',
  22212. '']));
  22213. end;
  22214. procedure TTestModule.TestClassInterface_COM_ForInterfaceInObject;
  22215. begin
  22216. StartProgram(false);
  22217. Add([
  22218. '{$interfaces com}',
  22219. 'type',
  22220. ' IUnknown = interface end;',
  22221. ' TObject = class',
  22222. ' end;',
  22223. ' IWing = interface',
  22224. ' function Id: longint;',
  22225. ' end;',
  22226. ' TEnumerator = class',
  22227. ' function GetCurrent: IWing; virtual; abstract;',
  22228. ' function MoveNext: Boolean; virtual; abstract;',
  22229. ' property Current: IWing read GetCurrent;',
  22230. ' end;',
  22231. ' TBird = class',
  22232. ' function GetEnumerator: TEnumerator; virtual; abstract;',
  22233. ' procedure Test;',
  22234. ' end;',
  22235. 'procedure TBird.Test;',
  22236. 'var',
  22237. ' Wing: IWing;',
  22238. 'begin',
  22239. ' for Wing in Self do',
  22240. ' if Wing.Id=1 then ;',
  22241. 'end;',
  22242. 'var',
  22243. ' Bird: TBird;',
  22244. ' Wing: IWing;',
  22245. 'begin',
  22246. ' for Wing in Bird do',
  22247. ' if Wing.Id=2 then ;',
  22248. '']);
  22249. ConvertProgram;
  22250. CheckSource('TestClassInterface_COM_ForInterfaceInObject',
  22251. LinesToStr([ // statements
  22252. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22253. 'rtl.createClass(this, "TObject", null, function () {',
  22254. ' this.$init = function () {',
  22255. ' };',
  22256. ' this.$final = function () {',
  22257. ' };',
  22258. '});',
  22259. 'rtl.createInterface(this, "IWing", "{8B0D080B-C0F6-396E-AE88-000BDB74730C}", ["Id"], this.IUnknown);',
  22260. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22261. '});',
  22262. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22263. ' this.Test = function () {',
  22264. ' var Wing = null;',
  22265. ' try {',
  22266. ' var $in = this.GetEnumerator();',
  22267. ' try {',
  22268. ' while ($in.MoveNext()) {',
  22269. ' Wing = rtl.setIntfL(Wing, $in.GetCurrent(), true);',
  22270. ' if (Wing.Id() === 1) ;',
  22271. ' }',
  22272. ' } finally {',
  22273. ' $in = rtl.freeLoc($in)',
  22274. ' };',
  22275. ' } finally {',
  22276. ' rtl._Release(Wing);',
  22277. ' };',
  22278. ' };',
  22279. '});',
  22280. 'this.Bird = null;',
  22281. 'this.Wing = null;',
  22282. '']),
  22283. LinesToStr([ // $mod.$main
  22284. 'var $in = $mod.Bird.GetEnumerator();',
  22285. 'try {',
  22286. ' while ($in.MoveNext()) {',
  22287. ' rtl.setIntfP($mod, "Wing", $in.GetCurrent(), true);',
  22288. ' if ($mod.Wing.Id() === 2) ;',
  22289. ' }',
  22290. '} finally {',
  22291. ' $in = rtl.freeLoc($in)',
  22292. '};',
  22293. '']));
  22294. end;
  22295. procedure TTestModule.TestClassInterface_COM_ArrayOfIntf;
  22296. begin
  22297. {$IFNDEF EnableCOMArrayOfIntf}
  22298. exit;
  22299. {$ENDIF}
  22300. StartProgram(false);
  22301. Add([
  22302. '{$interfaces com}',
  22303. 'type',
  22304. ' IUnknown = interface end;',
  22305. ' IBird = interface(IUnknown)',
  22306. ' function Fly(w: word): word;',
  22307. ' end;',
  22308. ' TBirdArray = array of IBird;',
  22309. 'procedure Run;',
  22310. 'var',
  22311. ' i: IBird;',
  22312. ' a,b: TBirdArray;',
  22313. 'begin',
  22314. //' SetLength(a,3);',
  22315. ' a:=b;',
  22316. ' i:=a[1];',
  22317. ' a[2]:=i;',
  22318. //' for i in a do i.fly(3);',
  22319. // a:=copy(b,1,2);
  22320. // a:=concat(b,a);
  22321. // insert(i,b,1);
  22322. // a:=[i,i];
  22323. 'end;',
  22324. // ToDo: pass TBirdArray as arg
  22325. 'begin',
  22326. '']);
  22327. ConvertProgram;
  22328. CheckSource('TestClassInterface_COM_ArrayOfIntf',
  22329. LinesToStr([ // statements
  22330. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  22331. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  22332. 'this.Run = function () {',
  22333. ' var i = null;',
  22334. ' var a = [];',
  22335. ' var b = [];',
  22336. ' try {',
  22337. ' a = rtl.arrayRef(b);',
  22338. ' i = rtl.setIntfL(i, a[1]);',
  22339. ' rtl.setIntfP(a, 2, i);',
  22340. ' } finally {',
  22341. ' rtl._Release(i);',
  22342. ' rtl._ReleaseArray(a,1);',
  22343. ' };',
  22344. '};',
  22345. '']),
  22346. LinesToStr([ // $mod.$main
  22347. '']));
  22348. end;
  22349. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  22350. begin
  22351. StartProgram(false);
  22352. Add([
  22353. '{$interfaces com}',
  22354. 'type',
  22355. ' IUnknown = interface',
  22356. ' function _AddRef: longint;',
  22357. ' function _Release: longint;',
  22358. ' end;',
  22359. ' TObject = class',
  22360. ' end;',
  22361. ' TArrOfIntf = array of IUnknown;',
  22362. 'begin',
  22363. '']);
  22364. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  22365. ConvertProgram;
  22366. end;
  22367. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  22368. begin
  22369. StartProgram(false);
  22370. Add([
  22371. '{$interfaces com}',
  22372. 'type',
  22373. ' IUnknown = interface',
  22374. ' function _AddRef: longint;',
  22375. ' function _Release: longint;',
  22376. ' end;',
  22377. ' TRec = record',
  22378. ' i: IUnknown;',
  22379. ' end;',
  22380. 'begin',
  22381. '']);
  22382. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  22383. ConvertProgram;
  22384. end;
  22385. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  22386. begin
  22387. StartUnit(false);
  22388. Add([
  22389. '{$interfaces com}',
  22390. 'interface',
  22391. 'implementation',
  22392. 'type',
  22393. ' IUnknown = interface',
  22394. ' function _AddRef: longint;',
  22395. ' end;',
  22396. ' TObject = class(IUnknown)',
  22397. ' function _AddRef: longint;',
  22398. ' end;',
  22399. 'function TObject._AddRef: longint; begin end;',
  22400. 'var i: IUnknown;',
  22401. ' o: TObject;',
  22402. 'initialization',
  22403. ' i:=nil;',
  22404. ' i:=i;',
  22405. ' i:=o;',
  22406. ' if (o as IUnknown)=nil then ;',
  22407. '']);
  22408. ConvertUnit;
  22409. CheckSource('TestClassInterface_COM_UnitInitialization',
  22410. LinesToStr([ // statements
  22411. 'var $impl = $mod.$impl;',
  22412. '']),
  22413. LinesToStr([ // this.$init
  22414. 'var $ir = rtl.createIntfRefs();',
  22415. 'try {',
  22416. ' rtl.setIntfP($impl, "i", null);',
  22417. ' rtl.setIntfP($impl, "i", $impl.i);',
  22418. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  22419. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  22420. '} finally {',
  22421. ' $ir.free();',
  22422. '};',
  22423. '']),
  22424. LinesToStr([ // implementation
  22425. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  22426. 'rtl.createClass($impl, "TObject", null, function () {',
  22427. ' this.$init = function () {',
  22428. ' };',
  22429. ' this.$final = function () {',
  22430. ' };',
  22431. ' this._AddRef = function () {',
  22432. ' var Result = 0;',
  22433. ' return Result;',
  22434. ' };',
  22435. ' rtl.addIntf(this, $impl.IUnknown);',
  22436. '});',
  22437. '$impl.i = null;',
  22438. '$impl.o = null;',
  22439. ''])
  22440. );
  22441. end;
  22442. procedure TTestModule.TestClassInterface_Corba_GUID;
  22443. begin
  22444. StartProgram(false);
  22445. Add([
  22446. '{$interfaces corba}',
  22447. 'type',
  22448. ' IUnknown = interface',
  22449. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22450. ' end;',
  22451. ' TObject = class end;',
  22452. ' TGUID = record D1, D2, D3, D4: word; end;',
  22453. ' TAliasGUID = TGUID;',
  22454. ' TGUIDString = type string;',
  22455. ' TAliasGUIDString = TGUIDString;',
  22456. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  22457. 'begin end;',
  22458. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  22459. 'begin end;',
  22460. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  22461. 'begin end;',
  22462. 'var',
  22463. ' i: IUnknown;',
  22464. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  22465. ' s: TAliasGUIDString;',
  22466. 'begin',
  22467. ' DoConstGUIDIt(IUnknown);',
  22468. ' DoDefGUID(IUnknown);',
  22469. ' DoStr(IUnknown);',
  22470. ' DoConstGUIDIt(i);',
  22471. ' DoDefGUID(i);',
  22472. ' DoStr(i);',
  22473. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22474. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  22475. ' DoStr(g);',
  22476. ' g:=i;',
  22477. ' g:=IUnknown;',
  22478. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22479. ' s:=i;',
  22480. ' s:=IUnknown;',
  22481. ' s:=g;',
  22482. ' if g=i then ;',
  22483. ' if i=g then ;',
  22484. ' if g=IUnknown then ;',
  22485. ' if IUnknown=g then ;',
  22486. ' if s=i then ;',
  22487. ' if i=s then ;',
  22488. ' if s=IUnknown then ;',
  22489. ' if IUnknown=s then ;',
  22490. ' if s=g then ;',
  22491. ' if g=s then ;',
  22492. '']);
  22493. ConvertProgram;
  22494. CheckSource('TestClassInterface_Corba_GUID',
  22495. LinesToStr([ // statements
  22496. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22497. 'rtl.createClass(this, "TObject", null, function () {',
  22498. ' this.$init = function () {',
  22499. ' };',
  22500. ' this.$final = function () {',
  22501. ' };',
  22502. '});',
  22503. 'rtl.recNewT(this, "TGUID", function () {',
  22504. ' this.D1 = 0;',
  22505. ' this.D2 = 0;',
  22506. ' this.D3 = 0;',
  22507. ' this.D4 = 0;',
  22508. ' this.$eq = function (b) {',
  22509. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22510. ' };',
  22511. ' this.$assign = function (s) {',
  22512. ' this.D1 = s.D1;',
  22513. ' this.D2 = s.D2;',
  22514. ' this.D3 = s.D3;',
  22515. ' this.D4 = s.D4;',
  22516. ' return this;',
  22517. ' };',
  22518. '});',
  22519. 'this.DoConstGUIDIt = function (g) {',
  22520. '};',
  22521. 'this.DoDefGUID = function (g) {',
  22522. '};',
  22523. 'this.DoStr = function (s) {',
  22524. '};',
  22525. 'this.i = null;',
  22526. 'this.g = this.TGUID.$clone({',
  22527. ' D1: 0xD91C9AF4,',
  22528. ' D2: 0x3C93,',
  22529. ' D3: 0x420F,',
  22530. ' D4: [',
  22531. ' 0xA3,',
  22532. ' 0x03,',
  22533. ' 0xBF,',
  22534. ' 0x5B,',
  22535. ' 0xA8,',
  22536. ' 0x2B,',
  22537. ' 0xFD,',
  22538. ' 0x23',
  22539. ' ]',
  22540. '});',
  22541. 'this.s = "";',
  22542. '']),
  22543. LinesToStr([ // $mod.$main
  22544. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  22545. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  22546. '$mod.DoStr($mod.IUnknown.$guid);',
  22547. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  22548. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  22549. '$mod.DoStr($mod.i.$guid);',
  22550. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22551. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  22552. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  22553. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  22554. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  22555. '$mod.g.$assign({',
  22556. ' D1: 0xD91C9AF4,',
  22557. ' D2: 0x3C93,',
  22558. ' D3: 0x420F,',
  22559. ' D4: [',
  22560. ' 0xA3,',
  22561. ' 0x03,',
  22562. ' 0xBF,',
  22563. ' 0x5B,',
  22564. ' 0xA8,',
  22565. ' 0x2B,',
  22566. ' 0xFD,',
  22567. ' 0x23',
  22568. ' ]',
  22569. '});',
  22570. '$mod.s = $mod.i.$guid;',
  22571. '$mod.s = $mod.IUnknown.$guid;',
  22572. '$mod.s = rtl.guidrToStr($mod.g);',
  22573. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22574. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  22575. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22576. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  22577. 'if ($mod.s === $mod.i.$guid) ;',
  22578. 'if ($mod.i.$guid === $mod.s) ;',
  22579. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  22580. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  22581. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22582. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  22583. '']));
  22584. end;
  22585. procedure TTestModule.TestClassInterface_Corba_GUIDProperty;
  22586. begin
  22587. StartProgram(false);
  22588. Add([
  22589. '{$interfaces corba}',
  22590. 'type',
  22591. ' IUnknown = interface',
  22592. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  22593. ' end;',
  22594. ' TGUID = record D1, D2, D3, D4: word; end;',
  22595. ' TAliasGUID = TGUID;',
  22596. ' TGUIDString = type string;',
  22597. ' TAliasGUIDString = TGUIDString;',
  22598. ' TObject = class',
  22599. ' function GetG: TAliasGUID; virtual; abstract;',
  22600. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  22601. ' function GetS: TAliasGUIDString; virtual; abstract;',
  22602. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  22603. ' property g: TAliasGUID read GetG write SetG;',
  22604. ' property s: TAliasGUIDString read GetS write SetS;',
  22605. ' end;',
  22606. 'var o: TObject;',
  22607. 'begin',
  22608. ' o.g:=IUnknown;',
  22609. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  22610. ' o.s:=IUnknown;',
  22611. ' o.s:=o.g;',
  22612. '']);
  22613. ConvertProgram;
  22614. CheckSource('TestClassInterface_GUIDProperty',
  22615. LinesToStr([ // statements
  22616. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  22617. 'rtl.recNewT(this, "TGUID", function () {',
  22618. ' this.D1 = 0;',
  22619. ' this.D2 = 0;',
  22620. ' this.D3 = 0;',
  22621. ' this.D4 = 0;',
  22622. ' this.$eq = function (b) {',
  22623. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  22624. ' };',
  22625. ' this.$assign = function (s) {',
  22626. ' this.D1 = s.D1;',
  22627. ' this.D2 = s.D2;',
  22628. ' this.D3 = s.D3;',
  22629. ' this.D4 = s.D4;',
  22630. ' return this;',
  22631. ' };',
  22632. '});',
  22633. 'rtl.createClass(this, "TObject", null, function () {',
  22634. ' this.$init = function () {',
  22635. ' };',
  22636. ' this.$final = function () {',
  22637. ' };',
  22638. '});',
  22639. 'this.o = null;',
  22640. '']),
  22641. LinesToStr([ // $mod.$main
  22642. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  22643. '$mod.o.SetG({',
  22644. ' D1: 0xD91C9AF4,',
  22645. ' D2: 0x3C93,',
  22646. ' D3: 0x420F,',
  22647. ' D4: [',
  22648. ' 0xA3,',
  22649. ' 0x03,',
  22650. ' 0xBF,',
  22651. ' 0x5B,',
  22652. ' 0xA8,',
  22653. ' 0x2B,',
  22654. ' 0xFD,',
  22655. ' 0x23',
  22656. ' ]',
  22657. '});',
  22658. '$mod.o.SetS($mod.IUnknown.$guid);',
  22659. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  22660. '']));
  22661. end;
  22662. procedure TTestModule.TestClassHelper_ClassVar;
  22663. begin
  22664. StartProgram(false);
  22665. Add([
  22666. 'type',
  22667. ' TObject = class',
  22668. ' end;',
  22669. ' THelper = class helper for TObject',
  22670. ' const',
  22671. ' One = 1;',
  22672. ' Two: word = 2;',
  22673. ' class var',
  22674. ' Glob: word;',
  22675. ' function Foo(w: word): word;',
  22676. ' class function Bar(w: word): word;',
  22677. ' end;',
  22678. 'function THelper.foo(w: word): word;',
  22679. 'begin',
  22680. ' Result:=w;',
  22681. ' Two:=One+w;',
  22682. ' Glob:=Glob;',
  22683. ' Result:=Self.Glob;',
  22684. ' Self.Glob:=Self.Glob;',
  22685. ' with Self do Glob:=Glob;',
  22686. 'end;',
  22687. 'class function THelper.bar(w: word): word;',
  22688. 'begin',
  22689. ' Result:=w;',
  22690. ' Two:=One;',
  22691. ' Glob:=Glob;',
  22692. ' Self.Glob:=Self.Glob;',
  22693. ' with Self do Glob:=Glob;',
  22694. 'end;',
  22695. 'var o: TObject;',
  22696. 'begin',
  22697. ' tobject.two:=tobject.one;',
  22698. ' tobject.Glob:=tobject.Glob;',
  22699. ' with tobject do begin',
  22700. ' two:=one;',
  22701. ' Glob:=Glob;',
  22702. ' end;',
  22703. ' o.two:=o.one;',
  22704. ' o.Glob:=o.Glob;',
  22705. ' with o do begin',
  22706. ' two:=one;',
  22707. ' Glob:=Glob;',
  22708. ' end;',
  22709. '']);
  22710. ConvertProgram;
  22711. CheckSource('TestClassHelper_ClassVar',
  22712. LinesToStr([ // statements
  22713. 'rtl.createClass(this, "TObject", null, function () {',
  22714. ' this.$init = function () {',
  22715. ' };',
  22716. ' this.$final = function () {',
  22717. ' };',
  22718. '});',
  22719. 'rtl.createHelper(this, "THelper", null, function () {',
  22720. ' this.One = 1;',
  22721. ' this.Two = 2;',
  22722. ' this.Glob = 0;',
  22723. ' this.Foo = function (w) {',
  22724. ' var Result = 0;',
  22725. ' Result = w;',
  22726. ' $mod.THelper.Two = 1 + w;',
  22727. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22728. ' Result = $mod.THelper.Glob;',
  22729. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22730. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22731. ' return Result;',
  22732. ' };',
  22733. ' this.Bar = function (w) {',
  22734. ' var Result = 0;',
  22735. ' Result = w;',
  22736. ' $mod.THelper.Two = 1;',
  22737. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22738. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22739. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22740. ' return Result;',
  22741. ' };',
  22742. '});',
  22743. 'this.o = null;',
  22744. '']),
  22745. LinesToStr([ // $mod.$main
  22746. '$mod.THelper.Two = 1;',
  22747. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22748. 'var $with = $mod.TObject;',
  22749. '$mod.THelper.Two = 1;',
  22750. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22751. '$mod.THelper.Two = 1;',
  22752. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22753. 'var $with1 = $mod.o;',
  22754. '$mod.THelper.Two = 1;',
  22755. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22756. '']));
  22757. end;
  22758. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  22759. begin
  22760. StartProgram(false);
  22761. Add([
  22762. 'type',
  22763. ' TObject = class',
  22764. ' FSize: word;',
  22765. ' property Size: word read FSize write FSize;',
  22766. ' end;',
  22767. ' THelper = class helper for TObject',
  22768. ' function Foo(w: word = 1): word;',
  22769. ' end;',
  22770. 'function THelper.foo(w: word): word;',
  22771. 'begin',
  22772. ' Result:=Size;',
  22773. ' Size:=Size+2;',
  22774. ' Self.Size:=Self.Size+3;',
  22775. ' FSize:=FSize+4;',
  22776. ' Self.FSize:=Self.FSize+5;',
  22777. ' with Self do begin',
  22778. ' Size:=Size+6;',
  22779. ' FSize:=FSize+7;',
  22780. ' FSize:=FSize+8;',
  22781. ' end;',
  22782. 'end;',
  22783. 'begin',
  22784. '']);
  22785. ConvertProgram;
  22786. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  22787. LinesToStr([ // statements
  22788. 'rtl.createClass(this, "TObject", null, function () {',
  22789. ' this.$init = function () {',
  22790. ' this.FSize = 0;',
  22791. ' };',
  22792. ' this.$final = function () {',
  22793. ' };',
  22794. '});',
  22795. 'rtl.createHelper(this, "THelper", null, function () {',
  22796. ' this.Foo = function (w) {',
  22797. ' var Result = 0;',
  22798. ' Result = this.FSize;',
  22799. ' this.FSize = this.FSize + 2;',
  22800. ' this.FSize = this.FSize + 3;',
  22801. ' this.FSize = this.FSize + 4;',
  22802. ' this.FSize = this.FSize + 5;',
  22803. ' this.FSize = this.FSize + 6;',
  22804. ' this.FSize = this.FSize + 7;',
  22805. ' this.FSize = this.FSize + 8;',
  22806. ' return Result;',
  22807. ' };',
  22808. '});',
  22809. '']),
  22810. LinesToStr([ // $mod.$main
  22811. '']));
  22812. end;
  22813. procedure TTestModule.TestClassHelper_Method_Call;
  22814. begin
  22815. StartProgram(false);
  22816. Add([
  22817. 'type',
  22818. ' TObject = class',
  22819. ' procedure Run(w: word = 10);',
  22820. ' end;',
  22821. ' THelper = class helper for TObject',
  22822. ' function Foo(w: word = 1): word;',
  22823. ' end;',
  22824. 'procedure TObject.Run(w: word);',
  22825. 'var o: TObject;',
  22826. 'begin',
  22827. ' Foo;',
  22828. ' Foo();',
  22829. ' Foo(2);',
  22830. ' Self.Foo;',
  22831. ' Self.Foo();',
  22832. ' Self.Foo(3);',
  22833. ' with Self do begin',
  22834. ' Foo;',
  22835. ' Foo();',
  22836. ' Foo(4);',
  22837. ' end;',
  22838. ' with o do Foo(5);',
  22839. 'end;',
  22840. 'function THelper.foo(w: word): word;',
  22841. 'begin',
  22842. ' Run;',
  22843. ' Run();',
  22844. ' Run(11);',
  22845. ' Foo;',
  22846. ' Foo();',
  22847. ' Foo(12);',
  22848. ' Self.Foo;',
  22849. ' Self.Foo();',
  22850. ' Self.Foo(13);',
  22851. ' with Self do begin',
  22852. ' Foo;',
  22853. ' Foo();',
  22854. ' Foo(14);',
  22855. ' end;',
  22856. 'end;',
  22857. 'var Obj: TObject;',
  22858. 'begin',
  22859. ' obj.Foo;',
  22860. ' obj.Foo();',
  22861. ' obj.Foo(21);',
  22862. ' with obj do begin',
  22863. ' Foo;',
  22864. ' Foo();',
  22865. ' Foo(22);',
  22866. ' end;',
  22867. '']);
  22868. ConvertProgram;
  22869. CheckSource('TestClassHelper_Method_Call',
  22870. LinesToStr([ // statements
  22871. 'rtl.createClass(this, "TObject", null, function () {',
  22872. ' this.$init = function () {',
  22873. ' };',
  22874. ' this.$final = function () {',
  22875. ' };',
  22876. ' this.Run = function (w) {',
  22877. ' var o = null;',
  22878. ' $mod.THelper.Foo.call(this, 1);',
  22879. ' $mod.THelper.Foo.call(this, 1);',
  22880. ' $mod.THelper.Foo.call(this, 2);',
  22881. ' $mod.THelper.Foo.call(this, 1);',
  22882. ' $mod.THelper.Foo.call(this, 1);',
  22883. ' $mod.THelper.Foo.call(this, 3);',
  22884. ' $mod.THelper.Foo.call(this, 1);',
  22885. ' $mod.THelper.Foo.call(this, 1);',
  22886. ' $mod.THelper.Foo.call(this, 4);',
  22887. ' $mod.THelper.Foo.call(o, 5);',
  22888. ' };',
  22889. '});',
  22890. 'rtl.createHelper(this, "THelper", null, function () {',
  22891. ' this.Foo = function (w) {',
  22892. ' var Result = 0;',
  22893. ' this.Run(10);',
  22894. ' this.Run(10);',
  22895. ' this.Run(11);',
  22896. ' $mod.THelper.Foo.call(this, 1);',
  22897. ' $mod.THelper.Foo.call(this, 1);',
  22898. ' $mod.THelper.Foo.call(this, 12);',
  22899. ' $mod.THelper.Foo.call(this, 1);',
  22900. ' $mod.THelper.Foo.call(this, 1);',
  22901. ' $mod.THelper.Foo.call(this, 13);',
  22902. ' $mod.THelper.Foo.call(this, 1);',
  22903. ' $mod.THelper.Foo.call(this, 1);',
  22904. ' $mod.THelper.Foo.call(this, 14);',
  22905. ' return Result;',
  22906. ' };',
  22907. '});',
  22908. 'this.Obj = null;',
  22909. '']),
  22910. LinesToStr([ // $mod.$main
  22911. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22912. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22913. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22914. 'var $with = $mod.Obj;',
  22915. '$mod.THelper.Foo.call($with, 1);',
  22916. '$mod.THelper.Foo.call($with, 1);',
  22917. '$mod.THelper.Foo.call($with, 22);',
  22918. '']));
  22919. end;
  22920. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  22921. begin
  22922. StartProgram(false);
  22923. Add([
  22924. 'type',
  22925. ' TObject = class',
  22926. ' procedure Run(w: word = 10);',
  22927. ' end;',
  22928. ' THelper = class helper for TObject',
  22929. ' function Foo(w: word = 1): word;',
  22930. ' end;',
  22931. 'procedure TObject.Run(w: word);',
  22932. ' procedure Sub(Self: TObject);',
  22933. ' begin',
  22934. ' Foo;',
  22935. ' Foo();',
  22936. ' Self.Foo;',
  22937. ' Self.Foo();',
  22938. ' with Self do begin',
  22939. ' Foo;',
  22940. ' Foo();',
  22941. ' end;',
  22942. ' end;',
  22943. 'begin',
  22944. 'end;',
  22945. 'function THelper.foo(w: word): word;',
  22946. ' procedure Sub(Self: TObject);',
  22947. ' begin',
  22948. ' Run;',
  22949. ' Run();',
  22950. ' Foo;',
  22951. ' Foo();',
  22952. ' Self.Foo;',
  22953. ' Self.Foo();',
  22954. ' with Self do begin',
  22955. ' Foo;',
  22956. ' Foo();',
  22957. ' end;',
  22958. ' end;',
  22959. 'begin',
  22960. 'end;',
  22961. 'begin',
  22962. '']);
  22963. ConvertProgram;
  22964. CheckSource('TestClassHelper_Method_Nested_Call',
  22965. LinesToStr([ // statements
  22966. 'rtl.createClass(this, "TObject", null, function () {',
  22967. ' this.$init = function () {',
  22968. ' };',
  22969. ' this.$final = function () {',
  22970. ' };',
  22971. ' this.Run = function (w) {',
  22972. ' var $Self = this;',
  22973. ' function Sub(Self) {',
  22974. ' $mod.THelper.Foo.call($Self, 1);',
  22975. ' $mod.THelper.Foo.call($Self, 1);',
  22976. ' $mod.THelper.Foo.call(Self, 1);',
  22977. ' $mod.THelper.Foo.call(Self, 1);',
  22978. ' $mod.THelper.Foo.call(Self, 1);',
  22979. ' $mod.THelper.Foo.call(Self, 1);',
  22980. ' };',
  22981. ' };',
  22982. '});',
  22983. 'rtl.createHelper(this, "THelper", null, function () {',
  22984. ' this.Foo = function (w) {',
  22985. ' var $Self = this;',
  22986. ' var Result = 0;',
  22987. ' function Sub(Self) {',
  22988. ' $Self.Run(10);',
  22989. ' $Self.Run(10);',
  22990. ' $mod.THelper.Foo.call($Self, 1);',
  22991. ' $mod.THelper.Foo.call($Self, 1);',
  22992. ' $mod.THelper.Foo.call(Self, 1);',
  22993. ' $mod.THelper.Foo.call(Self, 1);',
  22994. ' $mod.THelper.Foo.call(Self, 1);',
  22995. ' $mod.THelper.Foo.call(Self, 1);',
  22996. ' };',
  22997. ' return Result;',
  22998. ' };',
  22999. '});',
  23000. '']),
  23001. LinesToStr([ // $mod.$main
  23002. '']));
  23003. end;
  23004. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  23005. begin
  23006. StartProgram(false);
  23007. Add([
  23008. 'type',
  23009. ' TObject = class',
  23010. ' class procedure Run(w: word = 10);',
  23011. ' end;',
  23012. ' THelper = class helper for TObject',
  23013. ' class function Foo(w: word = 1): word;',
  23014. ' end;',
  23015. 'class procedure TObject.Run(w: word);',
  23016. 'begin',
  23017. ' Foo;',
  23018. ' Foo();',
  23019. ' Self.Foo;',
  23020. ' Self.Foo();',
  23021. ' with Self do begin',
  23022. ' Foo;',
  23023. ' Foo();',
  23024. ' end;',
  23025. 'end;',
  23026. 'class function THelper.foo(w: word): word;',
  23027. 'begin',
  23028. ' Run;',
  23029. ' Run();',
  23030. ' Foo;',
  23031. ' Foo();',
  23032. ' Self.Foo;',
  23033. ' Self.Foo();',
  23034. ' with Self do begin',
  23035. ' Foo;',
  23036. ' Foo();',
  23037. ' end;',
  23038. 'end;',
  23039. 'var',
  23040. ' Obj: TObject;',
  23041. 'begin',
  23042. ' obj.Foo;',
  23043. ' obj.Foo();',
  23044. ' with obj do begin',
  23045. ' Foo;',
  23046. ' Foo();',
  23047. ' end;',
  23048. ' tobject.Foo;',
  23049. ' tobject.Foo();',
  23050. ' with tobject do begin',
  23051. ' Foo;',
  23052. ' Foo();',
  23053. ' end;',
  23054. '']);
  23055. ConvertProgram;
  23056. CheckSource('TestClassHelper_ClassMethod_Call',
  23057. LinesToStr([ // statements
  23058. 'rtl.createClass(this, "TObject", null, function () {',
  23059. ' this.$init = function () {',
  23060. ' };',
  23061. ' this.$final = function () {',
  23062. ' };',
  23063. ' this.Run = function (w) {',
  23064. ' $mod.THelper.Foo.call(this, 1);',
  23065. ' $mod.THelper.Foo.call(this, 1);',
  23066. ' $mod.THelper.Foo.call(this, 1);',
  23067. ' $mod.THelper.Foo.call(this, 1);',
  23068. ' $mod.THelper.Foo.call(this, 1);',
  23069. ' $mod.THelper.Foo.call(this, 1);',
  23070. ' };',
  23071. '});',
  23072. 'rtl.createHelper(this, "THelper", null, function () {',
  23073. ' this.Foo = function (w) {',
  23074. ' var Result = 0;',
  23075. ' this.Run(10);',
  23076. ' this.Run(10);',
  23077. ' $mod.THelper.Foo.call(this, 1);',
  23078. ' $mod.THelper.Foo.call(this, 1);',
  23079. ' $mod.THelper.Foo.call(this, 1);',
  23080. ' $mod.THelper.Foo.call(this, 1);',
  23081. ' $mod.THelper.Foo.call(this, 1);',
  23082. ' $mod.THelper.Foo.call(this, 1);',
  23083. ' return Result;',
  23084. ' };',
  23085. '});',
  23086. 'this.Obj = null;',
  23087. '']),
  23088. LinesToStr([ // $mod.$main
  23089. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  23090. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  23091. 'var $with = $mod.Obj;',
  23092. '$mod.THelper.Foo.call($with.$class, 1);',
  23093. '$mod.THelper.Foo.call($with.$class, 1);',
  23094. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23095. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23096. 'var $with1 = $mod.TObject;',
  23097. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23098. '$mod.THelper.Foo.call($mod.TObject, 1);',
  23099. '']));
  23100. end;
  23101. procedure TTestModule.TestClassHelper_ClassOf;
  23102. begin
  23103. StartProgram(false);
  23104. Add([
  23105. 'type',
  23106. ' TObject = class',
  23107. ' end;',
  23108. ' TClass = class of TObject;',
  23109. ' THelper = class helper for TObject',
  23110. ' class function Foo(w: word = 1): word;',
  23111. ' end;',
  23112. 'class function THelper.foo(w: word): word;',
  23113. 'begin',
  23114. 'end;',
  23115. 'var',
  23116. ' c: TClass;',
  23117. 'begin',
  23118. ' c.Foo;',
  23119. ' c.Foo();',
  23120. ' with c do begin',
  23121. ' Foo;',
  23122. ' Foo();',
  23123. ' end;',
  23124. '']);
  23125. ConvertProgram;
  23126. CheckSource('TestClassHelper_ClassOf',
  23127. LinesToStr([ // statements
  23128. 'rtl.createClass(this, "TObject", null, function () {',
  23129. ' this.$init = function () {',
  23130. ' };',
  23131. ' this.$final = function () {',
  23132. ' };',
  23133. '});',
  23134. 'rtl.createHelper(this, "THelper", null, function () {',
  23135. ' this.Foo = function (w) {',
  23136. ' var Result = 0;',
  23137. ' return Result;',
  23138. ' };',
  23139. '});',
  23140. 'this.c = null;',
  23141. '']),
  23142. LinesToStr([ // $mod.$main
  23143. '$mod.THelper.Foo.call($mod.c, 1);',
  23144. '$mod.THelper.Foo.call($mod.c, 1);',
  23145. 'var $with = $mod.c;',
  23146. '$mod.THelper.Foo.call($with, 1);',
  23147. '$mod.THelper.Foo.call($with, 1);',
  23148. '']));
  23149. end;
  23150. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  23151. begin
  23152. StartProgram(false);
  23153. Add([
  23154. '{$mode objfpc}',
  23155. 'type',
  23156. ' TObject = class',
  23157. ' procedure DoIt;',
  23158. ' end;',
  23159. ' THelper = class helper for TObject',
  23160. ' procedure Fly(w: word = 1);',
  23161. ' class procedure Glide(w: word = 1);',
  23162. ' class procedure Run(w: word = 1); static;',
  23163. ' end;',
  23164. ' TFly = procedure(w: word) of object;',
  23165. ' TGlide = TFly;',
  23166. ' TRun = procedure(w: word);',
  23167. 'var',
  23168. ' f: TFly;',
  23169. ' g: TGlide;',
  23170. ' r: TRun;',
  23171. 'procedure TObject.DoIt;',
  23172. 'begin',
  23173. ' f:=@fly;',
  23174. ' g:=@glide;',
  23175. ' r:=@run;',
  23176. ' f:[email protected];',
  23177. ' g:[email protected];',
  23178. ' r:[email protected];',
  23179. ' with self do begin',
  23180. ' f:=@fly;',
  23181. ' g:=@glide;',
  23182. ' r:=@run;',
  23183. ' end;',
  23184. 'end;',
  23185. 'procedure THelper.fly(w: word);',
  23186. 'begin',
  23187. ' f:=@fly;',
  23188. ' g:=@glide;',
  23189. ' r:=@run;',
  23190. 'end;',
  23191. 'class procedure THelper.glide(w: word);',
  23192. 'begin',
  23193. ' g:=@glide;',
  23194. ' r:=@run;',
  23195. 'end;',
  23196. 'class procedure THelper.run(w: word);',
  23197. 'begin',
  23198. ' g:=@glide;',
  23199. ' r:=@run;',
  23200. 'end;',
  23201. 'var',
  23202. ' Obj: TObject;',
  23203. 'begin',
  23204. ' f:[email protected];',
  23205. ' g:[email protected];',
  23206. ' r:[email protected];',
  23207. ' with obj do begin',
  23208. ' f:=@fly;',
  23209. ' g:=@glide;',
  23210. ' r:=@run;',
  23211. ' end;',
  23212. ' g:[email protected];',
  23213. ' r:[email protected];',
  23214. ' with tobject do begin',
  23215. ' g:=@glide;',
  23216. ' r:=@run;',
  23217. ' end;',
  23218. '']);
  23219. ConvertProgram;
  23220. CheckSource('TestClassHelper_MethodRefObjFPC',
  23221. LinesToStr([ // statements
  23222. 'rtl.createClass(this, "TObject", null, function () {',
  23223. ' this.$init = function () {',
  23224. ' };',
  23225. ' this.$final = function () {',
  23226. ' };',
  23227. ' this.DoIt = function () {',
  23228. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23229. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23230. ' $mod.r = $mod.THelper.Run;',
  23231. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23232. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23233. ' $mod.r = $mod.THelper.Run;',
  23234. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23235. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23236. ' $mod.r = $mod.THelper.Run;',
  23237. ' };',
  23238. '});',
  23239. 'rtl.createHelper(this, "THelper", null, function () {',
  23240. ' this.Fly = function (w) {',
  23241. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  23242. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  23243. ' $mod.r = $mod.THelper.Run;',
  23244. ' };',
  23245. ' this.Glide = function (w) {',
  23246. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  23247. ' $mod.r = $mod.THelper.Run;',
  23248. ' };',
  23249. ' this.Run = function (w) {',
  23250. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  23251. ' $mod.r = $mod.THelper.Run;',
  23252. ' };',
  23253. '});',
  23254. 'this.f = null;',
  23255. 'this.g = null;',
  23256. 'this.r = null;',
  23257. 'this.Obj = null;',
  23258. '']),
  23259. LinesToStr([ // $mod.$main
  23260. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  23261. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  23262. '$mod.r = $mod.THelper.Run;',
  23263. 'var $with = $mod.Obj;',
  23264. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  23265. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  23266. '$mod.r = $mod.THelper.Run;',
  23267. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  23268. '$mod.r = $mod.THelper.Run;',
  23269. 'var $with1 = $mod.TObject;',
  23270. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  23271. '$mod.r = $mod.THelper.Run;',
  23272. '']));
  23273. end;
  23274. procedure TTestModule.TestClassHelper_Constructor;
  23275. begin
  23276. StartProgram(false);
  23277. Add([
  23278. 'type',
  23279. ' TObject = class',
  23280. ' constructor Create;',
  23281. ' end;',
  23282. ' TClass = class of TObject;',
  23283. ' THelper = class helper for TObject',
  23284. ' constructor NewHlp(w: word);',
  23285. ' end;',
  23286. 'var',
  23287. ' obj: TObject;',
  23288. ' c: TClass;',
  23289. 'constructor TObject.Create;',
  23290. 'begin',
  23291. ' NewHlp(2);', // normal call
  23292. ' tobject.NewHlp(3);', // new instance
  23293. ' c.newhlp(4);', // new instance
  23294. 'end;',
  23295. 'constructor THelper.NewHlp(w: word);',
  23296. 'begin',
  23297. ' create;', // normal call
  23298. ' tobject.create;', // new instance
  23299. ' NewHlp(2);', // normal call
  23300. ' tobject.NewHlp(3);', // new instance
  23301. ' c.newhlp(4);', // new instance
  23302. 'end;',
  23303. 'begin',
  23304. ' obj.newhlp(2);', // normal call
  23305. ' with Obj do newhlp(12);', // normal call
  23306. ' tobject.newhlp(3);', // new instance
  23307. ' with tobject do newhlp(13);', // new instance
  23308. ' c.newhlp(4);', // new instance
  23309. ' with c do newhlp(14);', // new instance
  23310. '']);
  23311. ConvertProgram;
  23312. CheckSource('TestClassHelper_Constructor',
  23313. LinesToStr([ // statements
  23314. 'rtl.createClass(this, "TObject", null, function () {',
  23315. ' this.$init = function () {',
  23316. ' };',
  23317. ' this.$final = function () {',
  23318. ' };',
  23319. ' this.Create = function () {',
  23320. ' $mod.THelper.NewHlp.call(this, 2);',
  23321. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23322. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23323. ' return this;',
  23324. ' };',
  23325. '});',
  23326. 'rtl.createHelper(this, "THelper", null, function () {',
  23327. ' this.NewHlp = function (w) {',
  23328. ' this.Create();',
  23329. ' $mod.TObject.$create("Create");',
  23330. ' $mod.THelper.NewHlp.call(this, 2);',
  23331. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23332. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  23333. ' return this;',
  23334. ' };',
  23335. '});',
  23336. 'this.obj = null;',
  23337. 'this.c = null;',
  23338. '']),
  23339. LinesToStr([ // $mod.$main
  23340. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  23341. 'var $with = $mod.obj;',
  23342. '$mod.THelper.NewHlp.call($with, 12);',
  23343. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  23344. 'var $with1 = $mod.TObject;',
  23345. '$with1.$create($mod.THelper.NewHlp, [13]);',
  23346. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  23347. 'var $with2 = $mod.c;',
  23348. '$with2.$create($mod.THelper.NewHlp, [14]);',
  23349. '']));
  23350. end;
  23351. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  23352. begin
  23353. StartProgram(false);
  23354. Add([
  23355. 'type',
  23356. ' TObject = class',
  23357. ' procedure Fly;',
  23358. ' end;',
  23359. ' TObjHelper = class helper for TObject',
  23360. ' procedure Fly;',
  23361. ' end;',
  23362. ' TBird = class',
  23363. ' procedure Fly;',
  23364. ' end;',
  23365. ' TBirdHelper = class helper for TBird',
  23366. ' procedure Fly;',
  23367. ' procedure Walk(w: word);',
  23368. ' end;',
  23369. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  23370. ' procedure Fly;',
  23371. ' procedure Walk(w: word);',
  23372. ' end;',
  23373. 'procedure Tobject.fly;',
  23374. 'begin',
  23375. ' inherited;', // ignore
  23376. 'end;',
  23377. 'procedure Tobjhelper.fly;',
  23378. 'begin',
  23379. ' {@TObject_Fly}inherited;',
  23380. ' inherited {@TObject_Fly}Fly;',
  23381. 'end;',
  23382. 'procedure Tbird.fly;',
  23383. 'begin',
  23384. ' {@TObjHelper_Fly}inherited;',
  23385. ' inherited {@TObjHelper_Fly}Fly;',
  23386. 'end;',
  23387. 'procedure Tbirdhelper.fly;',
  23388. 'begin',
  23389. ' {@TBird_Fly}inherited;',
  23390. ' inherited {@TBird_Fly}Fly;',
  23391. 'end;',
  23392. 'procedure Tbirdhelper.walk(w: word);',
  23393. 'begin',
  23394. 'end;',
  23395. 'procedure teagleHelper.fly;',
  23396. 'begin',
  23397. ' {@TBird_Fly}inherited;',
  23398. ' inherited {@TBird_Fly}Fly;',
  23399. 'end;',
  23400. 'procedure teagleHelper.walk(w: word);',
  23401. 'begin',
  23402. ' {@TBirdHelper_Walk}inherited;',
  23403. ' inherited {@TBirdHelper_Walk}Walk(3);',
  23404. 'end;',
  23405. 'begin',
  23406. '']);
  23407. ConvertProgram;
  23408. CheckSource('TestClassHelper_InheritedObjFPC',
  23409. LinesToStr([ // statements
  23410. 'rtl.createClass(this, "TObject", null, function () {',
  23411. ' this.$init = function () {',
  23412. ' };',
  23413. ' this.$final = function () {',
  23414. ' };',
  23415. ' this.Fly = function () {',
  23416. ' };',
  23417. '});',
  23418. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23419. ' this.Fly = function () {',
  23420. ' $mod.TObject.Fly.call(this);',
  23421. ' $mod.TObject.Fly.call(this);',
  23422. ' };',
  23423. '});',
  23424. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23425. ' this.Fly$1 = function () {',
  23426. ' $mod.TObjHelper.Fly.call(this);',
  23427. ' $mod.TObjHelper.Fly.call(this);',
  23428. ' };',
  23429. '});',
  23430. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23431. ' this.Fly = function () {',
  23432. ' $mod.TBird.Fly$1.call(this);',
  23433. ' $mod.TBird.Fly$1.call(this);',
  23434. ' };',
  23435. ' this.Walk = function (w) {',
  23436. ' };',
  23437. '});',
  23438. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  23439. ' this.Fly$1 = function () {',
  23440. ' $mod.TBird.Fly$1.call(this);',
  23441. ' $mod.TBird.Fly$1.call(this);',
  23442. ' };',
  23443. ' this.Walk$1 = function (w) {',
  23444. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  23445. ' $mod.TBirdHelper.Walk.call(this, 3);',
  23446. ' };',
  23447. '});',
  23448. '']),
  23449. LinesToStr([ // $mod.$main
  23450. '']));
  23451. end;
  23452. procedure TTestModule.TestClassHelper_Property;
  23453. begin
  23454. StartProgram(false);
  23455. Add([
  23456. 'type',
  23457. ' TObject = class',
  23458. ' FSize: word;',
  23459. ' function GetSpeed: word;',
  23460. ' procedure SetSpeed(Value: word);',
  23461. ' end;',
  23462. ' TObjHelper = class helper for TObject',
  23463. ' function GetLeft: word;',
  23464. ' procedure SetLeft(Value: word);',
  23465. ' property Size: word read FSize write FSize;',
  23466. ' property Speed: word read GetSpeed write SetSpeed;',
  23467. ' property Left: word read GetLeft write SetLeft;',
  23468. ' end;',
  23469. ' TBird = class',
  23470. ' property NotRight: word read GetLeft write SetLeft;',
  23471. ' procedure DoIt;',
  23472. ' end;',
  23473. 'var',
  23474. ' b: TBird;',
  23475. 'function Tobject.GetSpeed: word;',
  23476. 'begin',
  23477. ' Size:=Size+11;',
  23478. ' Speed:=Speed+12;',
  23479. ' Result:=Left+13;',
  23480. ' Left:=13;',
  23481. ' Left:=Left+13;',
  23482. ' Self.Size:=Self.Size+21;',
  23483. ' Self.Speed:=Self.Speed+22;',
  23484. ' Self.Left:=Self.Left+23;',
  23485. ' with Self do begin',
  23486. ' Size:=Size+31;',
  23487. ' Speed:=Speed+32;',
  23488. ' Left:=Left+33;',
  23489. ' end;',
  23490. 'end;',
  23491. 'procedure Tobject.SetSpeed(Value: word);',
  23492. 'begin',
  23493. 'end;',
  23494. 'function TObjHelper.GetLeft: word;',
  23495. 'begin',
  23496. ' Size:=Size+11;',
  23497. ' Speed:=Speed+12;',
  23498. ' Left:=Left+13;',
  23499. ' Self.Size:=Self.Size+21;',
  23500. ' Self.Speed:=Self.Speed+22;',
  23501. ' Self.Left:=Self.Left+23;',
  23502. ' with Self do begin',
  23503. ' Size:=Size+31;',
  23504. ' Speed:=Speed+32;',
  23505. ' Left:=Left+33;',
  23506. ' end;',
  23507. 'end;',
  23508. 'procedure TObjHelper.SetLeft(Value: word);',
  23509. 'begin',
  23510. 'end;',
  23511. 'procedure TBird.DoIt;',
  23512. 'begin',
  23513. ' NotRight:=NotRight+11;',
  23514. ' Self.NotRight:=Self.NotRight+21;',
  23515. ' with Self do begin',
  23516. ' NotRight:=NotRight+31;',
  23517. ' end;',
  23518. 'end;',
  23519. 'begin',
  23520. ' b.Size:=b.Size+11;',
  23521. ' b.Speed:=b.Speed+12;',
  23522. ' b.Left:=b.Left+13;',
  23523. ' b.NotRight:=b.NotRight+14;',
  23524. ' with b do begin',
  23525. ' Size:=Size+31;',
  23526. ' Speed:=Speed+32;',
  23527. ' Left:=Left+33;',
  23528. ' NotRight:=NotRight+34;',
  23529. ' end;',
  23530. '']);
  23531. ConvertProgram;
  23532. CheckSource('TestClassHelper_Property',
  23533. LinesToStr([ // statements
  23534. 'rtl.createClass(this, "TObject", null, function () {',
  23535. ' this.$init = function () {',
  23536. ' this.FSize = 0;',
  23537. ' };',
  23538. ' this.$final = function () {',
  23539. ' };',
  23540. ' this.GetSpeed = function () {',
  23541. ' var Result = 0;',
  23542. ' this.FSize = this.FSize + 11;',
  23543. ' this.SetSpeed(this.GetSpeed() + 12);',
  23544. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  23545. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  23546. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23547. ' this.FSize = this.FSize + 21;',
  23548. ' this.SetSpeed(this.GetSpeed() + 22);',
  23549. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23550. ' this.FSize = this.FSize + 31;',
  23551. ' this.SetSpeed(this.GetSpeed() + 32);',
  23552. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23553. ' return Result;',
  23554. ' };',
  23555. ' this.SetSpeed = function (Value) {',
  23556. ' };',
  23557. '});',
  23558. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23559. ' this.GetLeft = function () {',
  23560. ' var Result = 0;',
  23561. ' this.FSize = this.FSize + 11;',
  23562. ' this.SetSpeed(this.GetSpeed() + 12);',
  23563. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23564. ' this.FSize = this.FSize + 21;',
  23565. ' this.SetSpeed(this.GetSpeed() + 22);',
  23566. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23567. ' this.FSize = this.FSize + 31;',
  23568. ' this.SetSpeed(this.GetSpeed() + 32);',
  23569. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23570. ' return Result;',
  23571. ' };',
  23572. ' this.SetLeft = function (Value) {',
  23573. ' };',
  23574. '});',
  23575. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23576. ' this.DoIt = function () {',
  23577. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23578. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23579. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23580. ' };',
  23581. '});',
  23582. 'this.b = null;',
  23583. '']),
  23584. LinesToStr([ // $mod.$main
  23585. '$mod.b.FSize = $mod.b.FSize + 11;',
  23586. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  23587. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  23588. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  23589. 'var $with = $mod.b;',
  23590. '$with.FSize = $with.FSize + 31;',
  23591. '$with.SetSpeed($with.GetSpeed() + 32);',
  23592. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  23593. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  23594. '']));
  23595. end;
  23596. procedure TTestModule.TestClassHelper_Property_Array;
  23597. begin
  23598. StartProgram(false);
  23599. Add([
  23600. 'type',
  23601. ' TObject = class',
  23602. ' function GetSpeed(Index: boolean): word;',
  23603. ' procedure SetSpeed(Index: boolean; Value: word);',
  23604. ' end;',
  23605. ' TObjHelper = class helper for TObject',
  23606. ' function GetSize(Index: boolean): word;',
  23607. ' procedure SetSize(Index: boolean; Value: word);',
  23608. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  23609. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  23610. ' end;',
  23611. ' TBird = class',
  23612. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  23613. ' procedure DoIt;',
  23614. ' end;',
  23615. 'var',
  23616. ' b: TBird;',
  23617. 'function Tobject.GetSpeed(Index: boolean): word;',
  23618. 'begin',
  23619. ' Result:=Size[false];',
  23620. ' Size[true]:=Size[false]+11;',
  23621. ' Speed[true]:=Speed[false]+12;',
  23622. ' Self.Size[true]:=Self.Size[false]+21;',
  23623. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23624. ' with Self do begin',
  23625. ' Size[true]:=Size[false]+31;',
  23626. ' Speed[true]:=Speed[false]+32;',
  23627. ' end;',
  23628. 'end;',
  23629. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23630. 'begin',
  23631. 'end;',
  23632. 'function TObjHelper.GetSize(Index: boolean): word;',
  23633. 'begin',
  23634. ' Size[true]:=Size[false]+11;',
  23635. ' Speed[true]:=Speed[false]+12;',
  23636. ' Self.Size[true]:=Self.Size[false]+21;',
  23637. ' Self.Speed[true]:=Self.Speed[false]+22;',
  23638. ' with Self do begin',
  23639. ' Size[true]:=Size[false]+31;',
  23640. ' Speed[true]:=Speed[false]+32;',
  23641. ' end;',
  23642. 'end;',
  23643. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  23644. 'begin',
  23645. 'end;',
  23646. 'procedure TBird.DoIt;',
  23647. 'begin',
  23648. ' Items[true]:=Items[false]+11;',
  23649. ' Self.Items[true]:=Self.Items[false]+21;',
  23650. ' with Self do Items[true]:=Items[false]+31;',
  23651. 'end;',
  23652. 'begin',
  23653. ' b.Size[true]:=b.Size[false]+11;',
  23654. ' b.Speed[true]:=b.Speed[false]+12;',
  23655. ' b.Items[true]:=b.Items[false]+13;',
  23656. ' with b do begin',
  23657. ' Size[true]:=Size[false]+21;',
  23658. ' Speed[true]:=Speed[false]+22;',
  23659. ' Items[true]:=Items[false]+23;',
  23660. ' end;',
  23661. '']);
  23662. ConvertProgram;
  23663. CheckSource('TestClassHelper_Property_Array',
  23664. LinesToStr([ // statements
  23665. 'rtl.createClass(this, "TObject", null, function () {',
  23666. ' this.$init = function () {',
  23667. ' };',
  23668. ' this.$final = function () {',
  23669. ' };',
  23670. ' this.GetSpeed = function (Index) {',
  23671. ' var Result = 0;',
  23672. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  23673. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23674. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23675. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23676. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23677. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23678. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23679. ' return Result;',
  23680. ' };',
  23681. ' this.SetSpeed = function (Index, Value) {',
  23682. ' };',
  23683. '});',
  23684. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23685. ' this.GetSize = function (Index) {',
  23686. ' var Result = 0;',
  23687. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23688. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  23689. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23690. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  23691. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23692. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  23693. ' return Result;',
  23694. ' };',
  23695. ' this.SetSize = function (Index, Value) {',
  23696. ' };',
  23697. '});',
  23698. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23699. ' this.DoIt = function () {',
  23700. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  23701. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  23702. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  23703. ' };',
  23704. '});',
  23705. 'this.b = null;',
  23706. '']),
  23707. LinesToStr([ // $mod.$main
  23708. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  23709. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  23710. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  23711. 'var $with = $mod.b;',
  23712. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  23713. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  23714. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  23715. '']));
  23716. end;
  23717. procedure TTestModule.TestClassHelper_Property_Array_Default;
  23718. begin
  23719. StartProgram(false);
  23720. Add([
  23721. 'type',
  23722. ' TObject = class',
  23723. ' function GetSpeed(Index: boolean): word;',
  23724. ' procedure SetSpeed(Index: boolean; Value: word);',
  23725. ' end;',
  23726. ' TObjHelper = class helper for TObject',
  23727. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  23728. ' end;',
  23729. ' TBird = class',
  23730. ' end;',
  23731. ' TBirdHelper = class helper for TBird',
  23732. ' function GetSize(Index: word): boolean;',
  23733. ' procedure SetSize(Index: word; Value: boolean);',
  23734. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  23735. ' end;',
  23736. 'function Tobject.GetSpeed(Index: boolean): word;',
  23737. 'begin',
  23738. ' Self[true]:=Self[false]+1;',
  23739. 'end;',
  23740. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  23741. 'begin',
  23742. 'end;',
  23743. 'function TBirdHelper.GetSize(Index: word): boolean;',
  23744. 'begin',
  23745. ' Self[1]:=not Self[2];',
  23746. 'end;',
  23747. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  23748. 'begin',
  23749. 'end;',
  23750. 'var',
  23751. ' o: TObject;',
  23752. ' b: TBird;',
  23753. 'begin',
  23754. ' o[true]:=o[false]+1;',
  23755. ' b[3]:=not b[4];',
  23756. '']);
  23757. ConvertProgram;
  23758. CheckSource('TestClassHelper_Property_Array_Default',
  23759. LinesToStr([ // statements
  23760. 'rtl.createClass(this, "TObject", null, function () {',
  23761. ' this.$init = function () {',
  23762. ' };',
  23763. ' this.$final = function () {',
  23764. ' };',
  23765. ' this.GetSpeed = function (Index) {',
  23766. ' var Result = 0;',
  23767. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  23768. ' return Result;',
  23769. ' };',
  23770. ' this.SetSpeed = function (Index, Value) {',
  23771. ' };',
  23772. '});',
  23773. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23774. '});',
  23775. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23776. '});',
  23777. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  23778. ' this.GetSize = function (Index) {',
  23779. ' var Result = false;',
  23780. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  23781. ' return Result;',
  23782. ' };',
  23783. ' this.SetSize = function (Index, Value) {',
  23784. ' };',
  23785. '});',
  23786. 'this.o = null;',
  23787. 'this.b = null;',
  23788. '']),
  23789. LinesToStr([ // $mod.$main
  23790. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  23791. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  23792. '']));
  23793. end;
  23794. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  23795. begin
  23796. StartProgram(false);
  23797. Add([
  23798. 'type',
  23799. ' TObject = class',
  23800. ' end;',
  23801. ' TObjHelper = class helper for TObject',
  23802. ' function GetItems(Index: word): TObject;',
  23803. ' procedure SetItems(Index: word; Value: TObject);',
  23804. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  23805. ' end;',
  23806. 'function Tobjhelper.GetItems(Index: word): TObject;',
  23807. 'begin',
  23808. ' Self[1][2]:=Self[3][4];',
  23809. 'end;',
  23810. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  23811. 'begin',
  23812. 'end;',
  23813. 'var',
  23814. ' o: TObject;',
  23815. 'begin',
  23816. ' o[1][2]:=o[3][4];',
  23817. '']);
  23818. ConvertProgram;
  23819. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  23820. LinesToStr([ // statements
  23821. 'rtl.createClass(this, "TObject", null, function () {',
  23822. ' this.$init = function () {',
  23823. ' };',
  23824. ' this.$final = function () {',
  23825. ' };',
  23826. '});',
  23827. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23828. ' this.GetItems = function (Index) {',
  23829. ' var Result = null;',
  23830. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  23831. ' return Result;',
  23832. ' };',
  23833. ' this.SetItems = function (Index, Value) {',
  23834. ' };',
  23835. '});',
  23836. 'this.o = null;',
  23837. '']),
  23838. LinesToStr([ // $mod.$main
  23839. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  23840. '']));
  23841. end;
  23842. procedure TTestModule.TestClassHelper_ClassProperty;
  23843. begin
  23844. StartProgram(false);
  23845. Add([
  23846. 'type',
  23847. ' TObject = class',
  23848. ' class var FSize: word;',
  23849. ' class function GetSpeed: word;',
  23850. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  23851. ' end;',
  23852. ' TObjHelper = class helper for TObject',
  23853. ' class function GetLeft: word;',
  23854. ' class procedure SetLeft(Value: word);',
  23855. ' class property Size: word read FSize write FSize;',
  23856. ' class property Speed: word read GetSpeed write SetSpeed;',
  23857. ' class property Left: word read GetLeft write SetLeft;',
  23858. ' end;',
  23859. ' TBird = class',
  23860. ' class property NotRight: word read GetLeft write SetLeft;',
  23861. ' class procedure DoIt;',
  23862. ' end;',
  23863. ' TBirdClass = class of TBird;',
  23864. 'class function Tobject.GetSpeed: word;',
  23865. 'begin',
  23866. ' Size:=Size+11;',
  23867. ' Speed:=Speed+12;',
  23868. ' Left:=Left+13;',
  23869. ' Self.Size:=Self.Size+21;',
  23870. ' Self.Speed:=Self.Speed+22;',
  23871. ' Self.Left:=Self.Left+23;',
  23872. ' with Self do begin',
  23873. ' Size:=Size+31;',
  23874. ' Speed:=Speed+32;',
  23875. ' Left:=Left+33;',
  23876. ' end;',
  23877. 'end;',
  23878. 'class function TObjHelper.GetLeft: word;',
  23879. 'begin',
  23880. ' Size:=Size+11;',
  23881. ' Speed:=Speed+12;',
  23882. ' Left:=Left+13;',
  23883. ' Self.Size:=Self.Size+21;',
  23884. ' Self.Speed:=Self.Speed+22;',
  23885. ' Self.Left:=Self.Left+23;',
  23886. ' with Self do begin',
  23887. ' Size:=Size+31;',
  23888. ' Speed:=Speed+32;',
  23889. ' Left:=Left+33;',
  23890. ' end;',
  23891. 'end;',
  23892. 'class procedure TObjHelper.SetLeft(Value: word);',
  23893. 'begin',
  23894. 'end;',
  23895. 'class procedure TBird.DoIt;',
  23896. 'begin',
  23897. ' NotRight:=NotRight+11;',
  23898. ' Self.NotRight:=Self.NotRight+21;',
  23899. ' with Self do NotRight:=NotRight+31;',
  23900. 'end;',
  23901. 'var',
  23902. ' b: TBird;',
  23903. ' c: TBirdClass;',
  23904. 'begin',
  23905. ' b.Size:=b.Size+11;',
  23906. ' b.Speed:=b.Speed+12;',
  23907. ' b.Left:=b.Left+13;',
  23908. ' b.NotRight:=b.NotRight+14;',
  23909. ' with b do begin',
  23910. ' Size:=Size+31;',
  23911. ' Speed:=Speed+32;',
  23912. ' Left:=Left+33;',
  23913. ' NotRight:=NotRight+34;',
  23914. ' end;',
  23915. ' c.Size:=c.Size+11;',
  23916. ' c.Speed:=c.Speed+12;',
  23917. ' c.Left:=c.Left+13;',
  23918. ' c.NotRight:=c.NotRight+14;',
  23919. ' with c do begin',
  23920. ' Size:=Size+31;',
  23921. ' Speed:=Speed+32;',
  23922. ' Left:=Left+33;',
  23923. ' NotRight:=NotRight+34;',
  23924. ' end;',
  23925. ' tbird.Size:=tbird.Size+11;',
  23926. ' tbird.Speed:=tbird.Speed+12;',
  23927. ' tbird.Left:=tbird.Left+13;',
  23928. ' tbird.NotRight:=tbird.NotRight+14;',
  23929. ' with tbird do begin',
  23930. ' Size:=Size+31;',
  23931. ' Speed:=Speed+32;',
  23932. ' Left:=Left+33;',
  23933. ' NotRight:=NotRight+34;',
  23934. ' end;',
  23935. '']);
  23936. ConvertProgram;
  23937. CheckSource('TestClassHelper_ClassProperty',
  23938. LinesToStr([ // statements
  23939. 'rtl.createClass(this, "TObject", null, function () {',
  23940. ' this.FSize = 0;',
  23941. ' this.$init = function () {',
  23942. ' };',
  23943. ' this.$final = function () {',
  23944. ' };',
  23945. ' this.GetSpeed = function () {',
  23946. ' var Result = 0;',
  23947. ' $mod.TObject.FSize = this.FSize + 11;',
  23948. ' this.SetSpeed(this.GetSpeed() + 12);',
  23949. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23950. ' $mod.TObject.FSize = this.FSize + 21;',
  23951. ' this.SetSpeed(this.GetSpeed() + 22);',
  23952. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23953. ' $mod.TObject.FSize = this.FSize + 31;',
  23954. ' this.SetSpeed(this.GetSpeed() + 32);',
  23955. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23956. ' return Result;',
  23957. ' };',
  23958. '});',
  23959. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  23960. ' this.GetLeft = function () {',
  23961. ' var Result = 0;',
  23962. ' $mod.TObject.FSize = this.FSize + 11;',
  23963. ' this.SetSpeed(this.GetSpeed() + 12);',
  23964. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  23965. ' $mod.TObject.FSize = this.FSize + 21;',
  23966. ' this.SetSpeed(this.GetSpeed() + 22);',
  23967. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  23968. ' $mod.TObject.FSize = this.FSize + 31;',
  23969. ' this.SetSpeed(this.GetSpeed() + 32);',
  23970. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  23971. ' return Result;',
  23972. ' };',
  23973. ' this.SetLeft = function (Value) {',
  23974. ' };',
  23975. '});',
  23976. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  23977. ' this.DoIt = function () {',
  23978. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  23979. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  23980. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  23981. ' };',
  23982. '});',
  23983. 'this.b = null;',
  23984. 'this.c = null;',
  23985. '']),
  23986. LinesToStr([ // $mod.$main
  23987. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  23988. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  23989. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  23990. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  23991. 'var $with = $mod.b;',
  23992. '$mod.TObject.FSize = $with.FSize + 31;',
  23993. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  23994. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  23995. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  23996. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  23997. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  23998. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  23999. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  24000. 'var $with1 = $mod.c;',
  24001. '$mod.TObject.FSize = $with1.FSize + 31;',
  24002. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  24003. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  24004. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  24005. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  24006. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  24007. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  24008. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  24009. 'var $with2 = $mod.TBird;',
  24010. '$mod.TObject.FSize = $with2.FSize + 31;',
  24011. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  24012. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  24013. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  24014. '']));
  24015. end;
  24016. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  24017. begin
  24018. StartProgram(false);
  24019. Add([
  24020. 'type',
  24021. ' TObject = class',
  24022. ' class function GetSpeed: word; static;',
  24023. ' class procedure SetSpeed(Value: word); static;',
  24024. ' end;',
  24025. ' TObjHelper = class helper for TObject',
  24026. ' class function GetLeft: word; static;',
  24027. ' class procedure SetLeft(Value: word); static;',
  24028. ' class property Speed: word read GetSpeed write SetSpeed;',
  24029. ' class property Left: word read GetLeft write SetLeft;',
  24030. ' end;',
  24031. ' TBird = class',
  24032. ' class property NotRight: word read GetLeft write SetLeft;',
  24033. ' class procedure DoIt; static;',
  24034. ' class procedure DoSome;',
  24035. ' end;',
  24036. ' TBirdClass = class of TBird;',
  24037. 'class function Tobject.GetSpeed: word;',
  24038. 'begin',
  24039. ' Speed:=Speed+12;',
  24040. ' Left:=Left+13;',
  24041. 'end;',
  24042. 'class procedure TObject.SetSpeed(Value: word);',
  24043. 'begin',
  24044. 'end;',
  24045. 'class function TObjHelper.GetLeft: word;',
  24046. 'begin',
  24047. ' Speed:=Speed+12;',
  24048. ' Left:=Left+13;',
  24049. 'end;',
  24050. 'class procedure TObjHelper.SetLeft(Value: word);',
  24051. 'begin',
  24052. 'end;',
  24053. 'class procedure TBird.DoIt;',
  24054. 'begin',
  24055. ' NotRight:=NotRight+11;',
  24056. 'end;',
  24057. 'class procedure TBird.DoSome;',
  24058. 'begin',
  24059. ' Speed:=Speed+12;',
  24060. ' Left:=Left+13;',
  24061. ' Self.Speed:=Self.Speed+22;',
  24062. ' Self.Left:=Self.Left+23;',
  24063. ' with Self do begin',
  24064. ' Speed:=Speed+32;',
  24065. ' Left:=Left+33;',
  24066. ' end;',
  24067. ' NotRight:=NotRight+11;',
  24068. ' Self.NotRight:=Self.NotRight+21;',
  24069. ' with Self do NotRight:=NotRight+31;',
  24070. 'end;',
  24071. 'var',
  24072. ' b: TBird;',
  24073. ' c: TBirdClass;',
  24074. 'begin',
  24075. ' b.Speed:=b.Speed+12;',
  24076. ' b.Left:=b.Left+13;',
  24077. ' b.NotRight:=b.NotRight+14;',
  24078. ' with b do begin',
  24079. ' Speed:=Speed+32;',
  24080. ' Left:=Left+33;',
  24081. ' NotRight:=NotRight+34;',
  24082. ' end;',
  24083. ' c.Speed:=c.Speed+12;',
  24084. ' c.Left:=c.Left+13;',
  24085. ' c.NotRight:=c.NotRight+14;',
  24086. ' with c do begin',
  24087. ' Speed:=Speed+32;',
  24088. ' Left:=Left+33;',
  24089. ' NotRight:=NotRight+34;',
  24090. ' end;',
  24091. ' tbird.Speed:=tbird.Speed+12;',
  24092. ' tbird.Left:=tbird.Left+13;',
  24093. ' tbird.NotRight:=tbird.NotRight+14;',
  24094. ' with tbird do begin',
  24095. ' Speed:=Speed+32;',
  24096. ' Left:=Left+33;',
  24097. ' NotRight:=NotRight+34;',
  24098. ' end;',
  24099. '']);
  24100. ConvertProgram;
  24101. CheckSource('TestClassHelper_ClassPropertyStatic',
  24102. LinesToStr([ // statements
  24103. 'rtl.createClass(this, "TObject", null, function () {',
  24104. ' this.$init = function () {',
  24105. ' };',
  24106. ' this.$final = function () {',
  24107. ' };',
  24108. ' this.GetSpeed = function () {',
  24109. ' var Result = 0;',
  24110. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24111. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24112. ' return Result;',
  24113. ' };',
  24114. ' this.SetSpeed = function (Value) {',
  24115. ' };',
  24116. '});',
  24117. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24118. ' this.GetLeft = function () {',
  24119. ' var Result = 0;',
  24120. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24121. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24122. ' return Result;',
  24123. ' };',
  24124. ' this.SetLeft = function (Value) {',
  24125. ' };',
  24126. '});',
  24127. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24128. ' this.DoIt = function () {',
  24129. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  24130. ' };',
  24131. ' this.DoSome = function () {',
  24132. ' this.SetSpeed(this.GetSpeed() + 12);',
  24133. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24134. ' this.SetSpeed(this.GetSpeed() + 22);',
  24135. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  24136. ' this.SetSpeed(this.GetSpeed() + 32);',
  24137. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24138. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  24139. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  24140. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  24141. ' };',
  24142. '});',
  24143. 'this.b = null;',
  24144. 'this.c = null;',
  24145. '']),
  24146. LinesToStr([ // $mod.$main
  24147. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24148. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24149. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24150. 'var $with = $mod.b;',
  24151. '$with.SetSpeed($with.GetSpeed() + 32);',
  24152. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24153. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24154. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24155. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24156. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24157. 'var $with1 = $mod.c;',
  24158. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  24159. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24160. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24161. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  24162. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  24163. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  24164. 'var $with2 = $mod.TBird;',
  24165. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  24166. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  24167. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  24168. '']));
  24169. end;
  24170. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  24171. begin
  24172. StartProgram(false);
  24173. Add([
  24174. 'type',
  24175. ' TObject = class',
  24176. ' class function GetSpeed(Index: boolean): word;',
  24177. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  24178. ' end;',
  24179. ' TObjHelper = class helper for TObject',
  24180. ' class function GetSize(Index: boolean): word;',
  24181. ' class procedure SetSize(Index: boolean; Value: word);',
  24182. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  24183. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  24184. ' end;',
  24185. ' TBird = class',
  24186. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  24187. ' class procedure DoIt;',
  24188. ' end;',
  24189. ' TBirdClass = class of TBird;',
  24190. 'class function Tobject.GetSpeed(Index: boolean): word;',
  24191. 'begin',
  24192. ' Size[true]:=Size[false]+11;',
  24193. ' Speed[true]:=Speed[false]+12;',
  24194. ' Self.Size[true]:=Self.Size[false]+21;',
  24195. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24196. ' with Self do begin',
  24197. ' Size[true]:=Size[false]+31;',
  24198. ' Speed[true]:=Speed[false]+32;',
  24199. ' end;',
  24200. 'end;',
  24201. 'class function TObjHelper.GetSize(Index: boolean): word;',
  24202. 'begin',
  24203. ' Size[true]:=Size[false]+11;',
  24204. ' Speed[true]:=Speed[false]+12;',
  24205. ' Self.Size[true]:=Self.Size[false]+21;',
  24206. ' Self.Speed[true]:=Self.Speed[false]+22;',
  24207. ' with Self do begin',
  24208. ' Size[true]:=Size[false]+31;',
  24209. ' Speed[true]:=Speed[false]+32;',
  24210. ' end;',
  24211. 'end;',
  24212. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  24213. 'begin',
  24214. 'end;',
  24215. 'class procedure TBird.DoIt;',
  24216. 'begin',
  24217. ' Items[true]:=Items[false]+11;',
  24218. ' Self.Items[true]:=Self.Items[false]+21;',
  24219. ' with Self do Items[true]:=Items[false]+31;',
  24220. 'end;',
  24221. 'var',
  24222. ' b: TBird;',
  24223. ' c: TBirdClass;',
  24224. 'begin',
  24225. ' b.Size[true]:=b.Size[false]+11;',
  24226. ' b.Speed[true]:=b.Speed[false]+12;',
  24227. ' b.Items[true]:=b.Items[false]+13;',
  24228. ' with b do begin',
  24229. ' Size[true]:=Size[false]+21;',
  24230. ' Speed[true]:=Speed[false]+22;',
  24231. ' Items[true]:=Items[false]+23;',
  24232. ' end;',
  24233. ' c.Size[true]:=c.Size[false]+11;',
  24234. ' c.Speed[true]:=c.Speed[false]+12;',
  24235. ' c.Items[true]:=c.Items[false]+13;',
  24236. ' with c do begin',
  24237. ' Size[true]:=Size[false]+21;',
  24238. ' Speed[true]:=Speed[false]+22;',
  24239. ' Items[true]:=Items[false]+23;',
  24240. ' end;',
  24241. ' TBird.Size[true]:=TBird.Size[false]+11;',
  24242. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  24243. ' TBird.Items[true]:=TBird.Items[false]+13;',
  24244. ' with TBird do begin',
  24245. ' Size[true]:=Size[false]+21;',
  24246. ' Speed[true]:=Speed[false]+22;',
  24247. ' Items[true]:=Items[false]+23;',
  24248. ' end;',
  24249. '']);
  24250. ConvertProgram;
  24251. CheckSource('TestClassHelper_ClassProperty_Array',
  24252. LinesToStr([ // statements
  24253. 'rtl.createClass(this, "TObject", null, function () {',
  24254. ' this.$init = function () {',
  24255. ' };',
  24256. ' this.$final = function () {',
  24257. ' };',
  24258. ' this.GetSpeed = function (Index) {',
  24259. ' var Result = 0;',
  24260. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24261. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24262. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24263. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24264. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24265. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24266. ' return Result;',
  24267. ' };',
  24268. '});',
  24269. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  24270. ' this.GetSize = function (Index) {',
  24271. ' var Result = 0;',
  24272. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24273. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  24274. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24275. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  24276. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24277. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  24278. ' return Result;',
  24279. ' };',
  24280. ' this.SetSize = function (Index, Value) {',
  24281. ' };',
  24282. '});',
  24283. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24284. ' this.DoIt = function () {',
  24285. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  24286. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  24287. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  24288. ' };',
  24289. '});',
  24290. 'this.b = null;',
  24291. 'this.c = null;',
  24292. '']),
  24293. LinesToStr([ // $mod.$main
  24294. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  24295. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  24296. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  24297. 'var $with = $mod.b;',
  24298. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  24299. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  24300. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  24301. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  24302. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  24303. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  24304. 'var $with1 = $mod.c;',
  24305. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  24306. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  24307. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  24308. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  24309. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  24310. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  24311. 'var $with2 = $mod.TBird;',
  24312. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  24313. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  24314. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  24315. '']));
  24316. end;
  24317. procedure TTestModule.TestClassHelper_ForIn;
  24318. begin
  24319. StartProgram(false);
  24320. Add([
  24321. 'type',
  24322. ' TObject = class end;',
  24323. ' TItem = TObject;',
  24324. ' TEnumerator = class',
  24325. ' FCurrent: TItem;',
  24326. ' property Current: TItem read FCurrent;',
  24327. ' function MoveNext: boolean;',
  24328. ' end;',
  24329. ' TBird = class',
  24330. ' end;',
  24331. ' TBirdHelper = class helper for TBird',
  24332. ' function GetEnumerator: TEnumerator;',
  24333. ' end;',
  24334. 'function TEnumerator.MoveNext: boolean;',
  24335. 'begin',
  24336. 'end;',
  24337. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  24338. 'begin',
  24339. 'end;',
  24340. 'var',
  24341. ' b: TBird;',
  24342. ' i, i2: TItem;',
  24343. 'begin',
  24344. ' for i in b do i2:=i;']);
  24345. ConvertProgram;
  24346. CheckSource('TestClassHelper_ForIn',
  24347. LinesToStr([ // statements
  24348. 'rtl.createClass(this, "TObject", null, function () {',
  24349. ' this.$init = function () {',
  24350. ' };',
  24351. ' this.$final = function () {',
  24352. ' };',
  24353. '});',
  24354. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  24355. ' this.$init = function () {',
  24356. ' $mod.TObject.$init.call(this);',
  24357. ' this.FCurrent = null;',
  24358. ' };',
  24359. ' this.$final = function () {',
  24360. ' this.FCurrent = undefined;',
  24361. ' $mod.TObject.$final.call(this);',
  24362. ' };',
  24363. ' this.MoveNext = function () {',
  24364. ' var Result = false;',
  24365. ' return Result;',
  24366. ' };',
  24367. '});',
  24368. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  24369. '});',
  24370. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  24371. ' this.GetEnumerator = function () {',
  24372. ' var Result = null;',
  24373. ' return Result;',
  24374. ' };',
  24375. '});',
  24376. 'this.b = null;',
  24377. 'this.i = null;',
  24378. 'this.i2 = null;'
  24379. ]),
  24380. LinesToStr([ // $mod.$main
  24381. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  24382. 'try {',
  24383. ' while ($in.MoveNext()){',
  24384. ' $mod.i = $in.FCurrent;',
  24385. ' $mod.i2 = $mod.i;',
  24386. ' }',
  24387. '} finally {',
  24388. ' $in = rtl.freeLoc($in)',
  24389. '};',
  24390. '']));
  24391. end;
  24392. procedure TTestModule.TestClassHelper_PassProperty;
  24393. begin
  24394. StartProgram(false);
  24395. Add([
  24396. 'type',
  24397. ' TObject = class',
  24398. ' FField: TObject;',
  24399. ' property Field: TObject read FField write FField;',
  24400. ' end;',
  24401. ' THelper = class helper for TObject',
  24402. ' procedure Fly;',
  24403. ' class procedure Run;',
  24404. ' class procedure Jump; static;',
  24405. ' end;',
  24406. 'procedure THelper.Fly;',
  24407. 'begin',
  24408. ' Field.Fly;',
  24409. ' Field.Run;',
  24410. ' Field.Jump;',
  24411. ' with Field do begin',
  24412. ' Fly;',
  24413. ' Run;',
  24414. ' Jump;',
  24415. ' end;',
  24416. 'end;',
  24417. 'class procedure THelper.Run;',
  24418. 'begin',
  24419. 'end;',
  24420. 'class procedure THelper.Jump;',
  24421. 'begin',
  24422. 'end;',
  24423. 'var',
  24424. ' b: TObject;',
  24425. 'begin',
  24426. ' b.Field.Fly;',
  24427. ' b.Field.Run;',
  24428. ' b.Field.Jump;',
  24429. ' with b do begin',
  24430. ' Field.Run;',
  24431. ' Field.Fly;',
  24432. ' Field.Jump;',
  24433. ' end;',
  24434. ' with b.Field do begin',
  24435. ' Run;',
  24436. ' Fly;',
  24437. ' Jump;',
  24438. ' end;',
  24439. '']);
  24440. ConvertProgram;
  24441. CheckSource('TestClassHelper_PassProperty',
  24442. LinesToStr([ // statements
  24443. 'rtl.createClass(this, "TObject", null, function () {',
  24444. ' this.$init = function () {',
  24445. ' this.FField = null;',
  24446. ' };',
  24447. ' this.$final = function () {',
  24448. ' this.FField = undefined;',
  24449. ' };',
  24450. '});',
  24451. 'rtl.createHelper(this, "THelper", null, function () {',
  24452. ' this.Fly = function () {',
  24453. ' $mod.THelper.Fly.call(this.FField);',
  24454. ' $mod.THelper.Run.call(this.FField.$class);',
  24455. ' $mod.THelper.Jump();',
  24456. ' var $with = this.FField;',
  24457. ' $mod.THelper.Fly.call($with);',
  24458. ' $mod.THelper.Run.call($with.$class);',
  24459. ' $mod.THelper.Jump();',
  24460. ' };',
  24461. ' this.Run = function () {',
  24462. ' };',
  24463. ' this.Jump = function () {',
  24464. ' };',
  24465. '});',
  24466. 'this.b = null;',
  24467. '']),
  24468. LinesToStr([ // $mod.$main
  24469. '$mod.THelper.Fly.call($mod.b.FField);',
  24470. '$mod.THelper.Run.call($mod.b.FField.$class);',
  24471. '$mod.THelper.Jump();',
  24472. 'var $with = $mod.b;',
  24473. '$mod.THelper.Run.call($with.FField.$class);',
  24474. '$mod.THelper.Fly.call($with.FField);',
  24475. '$mod.THelper.Jump();',
  24476. 'var $with1 = $mod.b.FField;',
  24477. '$mod.THelper.Run.call($with1.$class);',
  24478. '$mod.THelper.Fly.call($with1);',
  24479. '$mod.THelper.Jump();',
  24480. '']));
  24481. end;
  24482. procedure TTestModule.TestExtClassHelper_ClassVar;
  24483. begin
  24484. StartProgram(false);
  24485. Add([
  24486. '{$modeswitch externalclass}',
  24487. 'type',
  24488. ' TExtA = class external name ''ExtObj''',
  24489. ' end;',
  24490. ' THelper = class helper for TExtA',
  24491. ' const',
  24492. ' One = 1;',
  24493. ' Two: word = 2;',
  24494. ' class var',
  24495. ' Glob: word;',
  24496. ' function Foo(w: word): word;',
  24497. ' class function Bar(w: word): word; static;',
  24498. ' end;',
  24499. 'function THelper.foo(w: word): word;',
  24500. 'begin',
  24501. ' Result:=w;',
  24502. ' Two:=One+w;',
  24503. ' Glob:=Glob;',
  24504. ' Result:=Self.Glob;',
  24505. ' Self.Glob:=Self.Glob;',
  24506. ' with Self do Glob:=Glob;',
  24507. 'end;',
  24508. 'class function THelper.bar(w: word): word;',
  24509. 'begin',
  24510. ' Result:=w;',
  24511. ' Two:=One;',
  24512. ' Glob:=Glob;',
  24513. 'end;',
  24514. 'var o: TExtA;',
  24515. 'begin',
  24516. ' texta.two:=texta.one;',
  24517. ' texta.Glob:=texta.Glob;',
  24518. ' with texta do begin',
  24519. ' two:=one;',
  24520. ' Glob:=Glob;',
  24521. ' end;',
  24522. ' o.two:=o.one;',
  24523. ' o.Glob:=o.Glob;',
  24524. ' with o do begin',
  24525. ' two:=one;',
  24526. ' Glob:=Glob;',
  24527. ' end;',
  24528. '']);
  24529. ConvertProgram;
  24530. CheckSource('TestExtClassHelper_ClassVar',
  24531. LinesToStr([ // statements
  24532. 'rtl.createHelper(this, "THelper", null, function () {',
  24533. ' this.One = 1;',
  24534. ' this.Two = 2;',
  24535. ' this.Glob = 0;',
  24536. ' this.Foo = function (w) {',
  24537. ' var Result = 0;',
  24538. ' Result = w;',
  24539. ' $mod.THelper.Two = 1 + w;',
  24540. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24541. ' Result = $mod.THelper.Glob;',
  24542. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24543. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24544. ' return Result;',
  24545. ' };',
  24546. ' this.Bar = function (w) {',
  24547. ' var Result = 0;',
  24548. ' Result = w;',
  24549. ' $mod.THelper.Two = 1;',
  24550. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24551. ' return Result;',
  24552. ' };',
  24553. '});',
  24554. 'this.o = null;',
  24555. '']),
  24556. LinesToStr([ // $mod.$main
  24557. '$mod.THelper.Two = 1;',
  24558. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24559. '$mod.THelper.Two = 1;',
  24560. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24561. '$mod.THelper.Two = 1;',
  24562. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24563. 'var $with = $mod.o;',
  24564. '$mod.THelper.Two = 1;',
  24565. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24566. '']));
  24567. end;
  24568. procedure TTestModule.TestExtClassHelper_Method_Call;
  24569. begin
  24570. StartProgram(false);
  24571. Add([
  24572. '{$modeswitch externalclass}',
  24573. 'type',
  24574. ' TFly = function(w: word): word of object;',
  24575. ' TExtA = class external name ''ExtObj''',
  24576. ' procedure Run(w: word = 10);',
  24577. ' end;',
  24578. ' THelper = class helper for TExtA',
  24579. ' function Foo(w: word = 1): word;',
  24580. ' function Fly(w: word = 2): word; external name ''Fly'';',
  24581. ' end;',
  24582. 'var p: TFly;',
  24583. 'function THelper.foo(w: word): word;',
  24584. 'begin',
  24585. ' Run;',
  24586. ' Run();',
  24587. ' Run(11);',
  24588. ' Foo;',
  24589. ' Foo();',
  24590. ' Foo(12);',
  24591. ' Self.Foo;',
  24592. ' Self.Foo();',
  24593. ' Self.Foo(13);',
  24594. ' Fly;',
  24595. ' Fly();',
  24596. ' with Self do begin',
  24597. ' Foo;',
  24598. ' Foo();',
  24599. ' Foo(14);',
  24600. ' Fly;',
  24601. ' Fly();',
  24602. ' end;',
  24603. ' p:=@Fly;',
  24604. 'end;',
  24605. 'var Obj: TExtA;',
  24606. 'begin',
  24607. ' obj.Foo;',
  24608. ' obj.Foo();',
  24609. ' obj.Foo(21);',
  24610. ' obj.Fly;',
  24611. ' obj.Fly();',
  24612. ' with obj do begin',
  24613. ' Foo;',
  24614. ' Foo();',
  24615. ' Foo(22);',
  24616. ' Fly;',
  24617. ' Fly();',
  24618. ' end;',
  24619. ' p:[email protected];',
  24620. '']);
  24621. ConvertProgram;
  24622. CheckSource('TestExtClassHelper_Method_Call',
  24623. LinesToStr([ // statements
  24624. 'rtl.createHelper(this, "THelper", null, function () {',
  24625. ' this.Foo = function (w) {',
  24626. ' var Result = 0;',
  24627. ' this.Run(10);',
  24628. ' this.Run(10);',
  24629. ' this.Run(11);',
  24630. ' $mod.THelper.Foo.call(this, 1);',
  24631. ' $mod.THelper.Foo.call(this, 1);',
  24632. ' $mod.THelper.Foo.call(this, 12);',
  24633. ' $mod.THelper.Foo.call(this, 1);',
  24634. ' $mod.THelper.Foo.call(this, 1);',
  24635. ' $mod.THelper.Foo.call(this, 13);',
  24636. ' this.Fly(2);',
  24637. ' this.Fly(2);',
  24638. ' $mod.THelper.Foo.call(this, 1);',
  24639. ' $mod.THelper.Foo.call(this, 1);',
  24640. ' $mod.THelper.Foo.call(this, 14);',
  24641. ' this.Fly(2);',
  24642. ' this.Fly(2);',
  24643. ' $mod.p = rtl.createCallback(this, "Fly");',
  24644. ' return Result;',
  24645. ' };',
  24646. '});',
  24647. 'this.p = null;',
  24648. 'this.Obj = null;',
  24649. '']),
  24650. LinesToStr([ // $mod.$main
  24651. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24652. '$mod.THelper.Foo.call($mod.Obj, 1);',
  24653. '$mod.THelper.Foo.call($mod.Obj, 21);',
  24654. '$mod.Obj.Fly(2);',
  24655. '$mod.Obj.Fly(2);',
  24656. 'var $with = $mod.Obj;',
  24657. '$mod.THelper.Foo.call($with, 1);',
  24658. '$mod.THelper.Foo.call($with, 1);',
  24659. '$mod.THelper.Foo.call($with, 22);',
  24660. '$with.Fly(2);',
  24661. '$with.Fly(2);',
  24662. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  24663. '']));
  24664. end;
  24665. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  24666. begin
  24667. StartProgram(false);
  24668. Add([
  24669. '{$modeswitch externalclass}',
  24670. 'type',
  24671. ' TExtA = class external name ''ExtObj''',
  24672. ' procedure Run(w: word = 10);',
  24673. ' end;',
  24674. ' THelper = class helper for TExtA',
  24675. ' class procedure Fly;',
  24676. ' end;',
  24677. 'class procedure THelper.Fly;',
  24678. 'begin end;',
  24679. 'begin',
  24680. '']);
  24681. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  24682. nHelperClassMethodForExtClassMustBeStatic);
  24683. ConvertProgram;
  24684. end;
  24685. procedure TTestModule.TestRecordHelper_ClassVar;
  24686. begin
  24687. StartProgram(false);
  24688. Add([
  24689. 'type',
  24690. ' TRec = record',
  24691. ' end;',
  24692. ' THelper = record helper for TRec',
  24693. ' const',
  24694. ' One = 1;',
  24695. ' Two: word = 2;',
  24696. ' class var',
  24697. ' Glob: word;',
  24698. ' function Foo(w: word): word;',
  24699. ' class function Bar(w: word): word; static;',
  24700. ' end;',
  24701. 'function THelper.foo(w: word): word;',
  24702. 'begin',
  24703. ' Result:=w;',
  24704. ' Two:=One+w;',
  24705. ' Glob:=Glob;',
  24706. ' Result:=Self.Glob;',
  24707. ' Self.Glob:=Self.Glob;',
  24708. ' with Self do Glob:=Glob;',
  24709. ' Self:=Self;',
  24710. 'end;',
  24711. 'class function THelper.bar(w: word): word;',
  24712. 'begin',
  24713. ' Result:=w;',
  24714. ' Two:=One;',
  24715. ' Glob:=Glob;',
  24716. 'end;',
  24717. 'var r: TRec;',
  24718. 'begin',
  24719. ' trec.two:=trec.one;',
  24720. ' trec.Glob:=trec.Glob;',
  24721. ' with trec do begin',
  24722. ' two:=one;',
  24723. ' Glob:=Glob;',
  24724. ' end;',
  24725. ' r.two:=r.one;',
  24726. ' r.Glob:=r.Glob;',
  24727. ' with r do begin',
  24728. ' two:=one;',
  24729. ' Glob:=Glob;',
  24730. ' end;',
  24731. '']);
  24732. ConvertProgram;
  24733. CheckSource('TestRecordHelper_ClassVar',
  24734. LinesToStr([ // statements
  24735. 'rtl.recNewT(this, "TRec", function () {',
  24736. ' this.$eq = function (b) {',
  24737. ' return true;',
  24738. ' };',
  24739. ' this.$assign = function (s) {',
  24740. ' return this;',
  24741. ' };',
  24742. '});',
  24743. 'rtl.createHelper(this, "THelper", null, function () {',
  24744. ' this.One = 1;',
  24745. ' this.Two = 2;',
  24746. ' this.Glob = 0;',
  24747. ' this.Foo = function (w) {',
  24748. ' var Result = 0;',
  24749. ' Result = w;',
  24750. ' $mod.THelper.Two = 1 + w;',
  24751. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24752. ' Result = $mod.THelper.Glob;',
  24753. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24754. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24755. ' this.$assign(this);',
  24756. ' return Result;',
  24757. ' };',
  24758. ' this.Bar = function (w) {',
  24759. ' var Result = 0;',
  24760. ' Result = w;',
  24761. ' $mod.THelper.Two = 1;',
  24762. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  24763. ' return Result;',
  24764. ' };',
  24765. '});',
  24766. 'this.r = this.TRec.$new();',
  24767. '']),
  24768. LinesToStr([ // $mod.$main
  24769. '$mod.THelper.Two = 1;',
  24770. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24771. 'var $with = $mod.TRec;',
  24772. '$mod.THelper.Two = 1;',
  24773. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24774. '$mod.THelper.Two = 1;',
  24775. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24776. 'var $with1 = $mod.r;',
  24777. '$mod.THelper.Two = 1;',
  24778. '$mod.THelper.Glob = $mod.THelper.Glob;',
  24779. '']));
  24780. end;
  24781. procedure TTestModule.TestRecordHelper_Method_Call;
  24782. begin
  24783. StartProgram(false);
  24784. Add([
  24785. '{$modeswitch AdvancedRecords}',
  24786. 'type',
  24787. ' TRec = record',
  24788. ' procedure Run(w: word = 10);',
  24789. ' end;',
  24790. ' THelper = record helper for TRec',
  24791. ' function Foo(w: word = 1): word;',
  24792. ' end;',
  24793. 'procedure TRec.Run(w: word);',
  24794. 'begin',
  24795. ' Foo;',
  24796. ' Foo();',
  24797. ' Foo(2);',
  24798. ' Self.Foo;',
  24799. ' Self.Foo();',
  24800. ' Self.Foo(3);',
  24801. ' with Self do begin',
  24802. ' Foo;',
  24803. ' Foo();',
  24804. ' Foo(4);',
  24805. ' end;',
  24806. 'end;',
  24807. 'function THelper.foo(w: word): word;',
  24808. 'begin',
  24809. ' Run;',
  24810. ' Run();',
  24811. ' Run(11);',
  24812. ' Foo;',
  24813. ' Foo();',
  24814. ' Foo(12);',
  24815. ' Self.Foo;',
  24816. ' Self.Foo();',
  24817. ' Self.Foo(13);',
  24818. ' with Self do begin',
  24819. ' Foo;',
  24820. ' Foo();',
  24821. ' Foo(14);',
  24822. ' end;',
  24823. 'end;',
  24824. 'var Rec: TRec;',
  24825. 'begin',
  24826. ' Rec.Foo;',
  24827. ' Rec.Foo();',
  24828. ' Rec.Foo(21);',
  24829. ' with Rec do begin',
  24830. ' Foo;',
  24831. ' Foo();',
  24832. ' Foo(22);',
  24833. ' end;',
  24834. '']);
  24835. ConvertProgram;
  24836. CheckSource('TestRecordHelper_Method_Call',
  24837. LinesToStr([ // statements
  24838. 'rtl.recNewT(this, "TRec", function () {',
  24839. ' this.$eq = function (b) {',
  24840. ' return true;',
  24841. ' };',
  24842. ' this.$assign = function (s) {',
  24843. ' return this;',
  24844. ' };',
  24845. ' this.Run = function (w) {',
  24846. ' $mod.THelper.Foo.call(this, 1);',
  24847. ' $mod.THelper.Foo.call(this, 1);',
  24848. ' $mod.THelper.Foo.call(this, 2);',
  24849. ' $mod.THelper.Foo.call(this, 1);',
  24850. ' $mod.THelper.Foo.call(this, 1);',
  24851. ' $mod.THelper.Foo.call(this, 3);',
  24852. ' $mod.THelper.Foo.call(this, 1);',
  24853. ' $mod.THelper.Foo.call(this, 1);',
  24854. ' $mod.THelper.Foo.call(this, 4);',
  24855. ' };',
  24856. '});',
  24857. 'rtl.createHelper(this, "THelper", null, function () {',
  24858. ' this.Foo = function (w) {',
  24859. ' var Result = 0;',
  24860. ' this.Run(10);',
  24861. ' this.Run(10);',
  24862. ' this.Run(11);',
  24863. ' $mod.THelper.Foo.call(this, 1);',
  24864. ' $mod.THelper.Foo.call(this, 1);',
  24865. ' $mod.THelper.Foo.call(this, 12);',
  24866. ' $mod.THelper.Foo.call(this, 1);',
  24867. ' $mod.THelper.Foo.call(this, 1);',
  24868. ' $mod.THelper.Foo.call(this, 13);',
  24869. ' $mod.THelper.Foo.call(this, 1);',
  24870. ' $mod.THelper.Foo.call(this, 1);',
  24871. ' $mod.THelper.Foo.call(this, 14);',
  24872. ' return Result;',
  24873. ' };',
  24874. '});',
  24875. 'this.Rec = this.TRec.$new();',
  24876. '']),
  24877. LinesToStr([ // $mod.$main
  24878. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24879. '$mod.THelper.Foo.call($mod.Rec, 1);',
  24880. '$mod.THelper.Foo.call($mod.Rec, 21);',
  24881. 'var $with = $mod.Rec;',
  24882. '$mod.THelper.Foo.call($with, 1);',
  24883. '$mod.THelper.Foo.call($with, 1);',
  24884. '$mod.THelper.Foo.call($with, 22);',
  24885. '']));
  24886. end;
  24887. procedure TTestModule.TestRecordHelper_Constructor;
  24888. begin
  24889. StartProgram(false);
  24890. Add([
  24891. '{$modeswitch AdvancedRecords}',
  24892. 'type',
  24893. ' TRec = record',
  24894. ' constructor Create(w: word);',
  24895. ' end;',
  24896. ' THelper = record helper for TRec',
  24897. ' constructor NewHlp(w: word);',
  24898. ' end;',
  24899. 'var',
  24900. ' Rec: TRec;',
  24901. 'constructor TRec.Create(w: word);',
  24902. 'begin',
  24903. ' NewHlp(2);', // normal call
  24904. ' trec.NewHlp(3);', // new instance
  24905. 'end;',
  24906. 'constructor THelper.NewHlp(w: word);',
  24907. 'begin',
  24908. ' create(2);', // normal call
  24909. ' trec.create(3);', // new instance
  24910. ' NewHlp(4);', // normal call
  24911. ' trec.NewHlp(5);', // new instance
  24912. 'end;',
  24913. 'begin',
  24914. ' rec.newhlp(2);', // normal call
  24915. ' with rec do newhlp(12);', // normal call
  24916. ' trec.newhlp(3);', // new instance
  24917. ' with trec do newhlp(13);', // new instance
  24918. '']);
  24919. ConvertProgram;
  24920. CheckSource('TestRecordHelper_Constructor',
  24921. LinesToStr([ // statements
  24922. 'rtl.recNewT(this, "TRec", function () {',
  24923. ' this.$eq = function (b) {',
  24924. ' return true;',
  24925. ' };',
  24926. ' this.$assign = function (s) {',
  24927. ' return this;',
  24928. ' };',
  24929. ' this.Create = function (w) {',
  24930. ' $mod.THelper.NewHlp.call(this, 2);',
  24931. ' $mod.THelper.$new("NewHlp", [3]);',
  24932. ' return this;',
  24933. ' };',
  24934. '});',
  24935. 'rtl.createHelper(this, "THelper", null, function () {',
  24936. ' this.NewHlp = function (w) {',
  24937. ' this.Create(2);',
  24938. ' $mod.TRec.$new().Create(3);',
  24939. ' $mod.THelper.NewHlp.call(this, 4);',
  24940. ' $mod.THelper.$new("NewHlp", [5]);',
  24941. ' return this;',
  24942. ' };',
  24943. ' this.$new = function (fn, args) {',
  24944. ' return this[fn].apply($mod.TRec.$new(), args);',
  24945. ' };',
  24946. '});',
  24947. 'this.Rec = this.TRec.$new();',
  24948. '']),
  24949. LinesToStr([ // $mod.$main
  24950. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  24951. 'var $with = $mod.Rec;',
  24952. '$mod.THelper.NewHlp.call($with, 12);',
  24953. '$mod.THelper.$new("NewHlp", [3]);',
  24954. 'var $with1 = $mod.TRec;',
  24955. '$mod.THelper.$new("NewHlp", [13]);',
  24956. '']));
  24957. end;
  24958. procedure TTestModule.TestTypeHelper_ClassVar;
  24959. begin
  24960. StartProgram(false);
  24961. Add([
  24962. '{$modeswitch typehelpers}',
  24963. 'type',
  24964. ' THelper = type helper for byte',
  24965. ' const',
  24966. ' One = 1;',
  24967. ' Two: word = 2;',
  24968. ' class var',
  24969. ' Glob: word;',
  24970. ' function Foo(w: word): word;',
  24971. ' class function Bar(w: word): word; static;',
  24972. ' end;',
  24973. 'function THelper.foo(w: word): word;',
  24974. 'begin',
  24975. ' Result:=w;',
  24976. ' Two:=One+w;',
  24977. ' Glob:=Glob;',
  24978. ' Result:=Self.Glob;',
  24979. ' Self.Glob:=Self.Glob;',
  24980. ' with Self do Glob:=Glob;',
  24981. 'end;',
  24982. 'class function THelper.bar(w: word): word;',
  24983. 'begin',
  24984. ' Result:=w;',
  24985. ' Two:=One;',
  24986. ' Glob:=Glob;',
  24987. 'end;',
  24988. 'var b: byte;',
  24989. 'begin',
  24990. ' byte.two:=byte.one;',
  24991. ' byte.Glob:=byte.Glob;',
  24992. ' with byte do begin',
  24993. ' two:=one;',
  24994. ' Glob:=Glob;',
  24995. ' end;',
  24996. ' b.two:=b.one;',
  24997. ' b.Glob:=b.Glob;',
  24998. ' with b do begin',
  24999. ' two:=one;',
  25000. ' Glob:=Glob;',
  25001. ' end;',
  25002. '']);
  25003. ConvertProgram;
  25004. CheckSource('TestTypeHelper_ClassVar',
  25005. LinesToStr([ // statements
  25006. 'rtl.createHelper(this, "THelper", null, function () {',
  25007. ' this.One = 1;',
  25008. ' this.Two = 2;',
  25009. ' this.Glob = 0;',
  25010. ' this.Foo = function (w) {',
  25011. ' var Result = 0;',
  25012. ' Result = w;',
  25013. ' $mod.THelper.Two = 1 + w;',
  25014. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25015. ' Result = $mod.THelper.Glob;',
  25016. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25017. ' var $with = this.get();',
  25018. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25019. ' return Result;',
  25020. ' };',
  25021. ' this.Bar = function (w) {',
  25022. ' var Result = 0;',
  25023. ' Result = w;',
  25024. ' $mod.THelper.Two = 1;',
  25025. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  25026. ' return Result;',
  25027. ' };',
  25028. '});',
  25029. 'this.b = 0;',
  25030. '']),
  25031. LinesToStr([ // $mod.$main
  25032. '$mod.THelper.Two = 1;',
  25033. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25034. '$mod.THelper.Two = 1;',
  25035. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25036. '$mod.THelper.Two = 1;',
  25037. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25038. 'var $with = $mod.b;',
  25039. '$mod.THelper.Two = 1;',
  25040. '$mod.THelper.Glob = $mod.THelper.Glob;',
  25041. '']));
  25042. end;
  25043. procedure TTestModule.TestTypeHelper_PassResultElement;
  25044. begin
  25045. StartProgram(false);
  25046. Add([
  25047. '{$modeswitch typehelpers}',
  25048. 'type',
  25049. ' THelper = type helper for word',
  25050. ' procedure DoIt(e: byte = 123);',
  25051. ' class procedure DoSome(e: byte = 456); static;',
  25052. ' end;',
  25053. 'procedure THelper.DoIt(e: byte);',
  25054. 'begin',
  25055. 'end;',
  25056. 'class procedure THelper.DoSome(e: byte);',
  25057. 'begin',
  25058. 'end;',
  25059. 'function Foo(w: word): word;',
  25060. 'begin',
  25061. ' Result.DoIt;',
  25062. ' Result.DoIt();',
  25063. ' Result.DoSome;',
  25064. ' Result.DoSome();',
  25065. ' with Result do begin',
  25066. ' DoIt;',
  25067. ' DoIt();',
  25068. ' DoSome;',
  25069. ' DoSome();',
  25070. ' end;',
  25071. 'end;',
  25072. 'begin',
  25073. '']);
  25074. ConvertProgram;
  25075. CheckSource('TestTypeHelper_PassResultElement',
  25076. LinesToStr([ // statements
  25077. 'rtl.createHelper(this, "THelper", null, function () {',
  25078. ' this.DoIt = function (e) {',
  25079. ' };',
  25080. ' this.DoSome = function (e) {',
  25081. ' };',
  25082. '});',
  25083. 'this.Foo = function (w) {',
  25084. ' var Result = 0;',
  25085. ' $mod.THelper.DoIt.call({',
  25086. ' get: function () {',
  25087. ' return Result;',
  25088. ' },',
  25089. ' set: function (v) {',
  25090. ' Result = v;',
  25091. ' }',
  25092. ' }, 123);',
  25093. ' $mod.THelper.DoIt.call({',
  25094. ' get: function () {',
  25095. ' return Result;',
  25096. ' },',
  25097. ' set: function (v) {',
  25098. ' Result = v;',
  25099. ' }',
  25100. ' }, 123);',
  25101. ' $mod.THelper.DoSome(456);',
  25102. ' $mod.THelper.DoSome(456);',
  25103. ' $mod.THelper.DoIt.call({',
  25104. ' get: function () {',
  25105. ' return Result;',
  25106. ' },',
  25107. ' set: function (v) {',
  25108. ' Result = v;',
  25109. ' }',
  25110. ' }, 123);',
  25111. ' $mod.THelper.DoIt.call({',
  25112. ' get: function () {',
  25113. ' return Result;',
  25114. ' },',
  25115. ' set: function (v) {',
  25116. ' Result = v;',
  25117. ' }',
  25118. ' }, 123);',
  25119. ' $mod.THelper.DoSome(456);',
  25120. ' $mod.THelper.DoSome(456);',
  25121. ' return Result;',
  25122. '};',
  25123. '']),
  25124. LinesToStr([ // $mod.$main
  25125. '']));
  25126. end;
  25127. procedure TTestModule.TestTypeHelper_PassArgs;
  25128. begin
  25129. StartProgram(false);
  25130. Add([
  25131. '{$modeswitch typehelpers}',
  25132. 'type',
  25133. ' THelper = type helper for word',
  25134. ' procedure DoIt(e: byte = 123);',
  25135. ' end;',
  25136. 'procedure THelper.DoIt(e: byte);',
  25137. 'begin',
  25138. 'end;',
  25139. 'procedure FooDefault(a: word);',
  25140. 'begin',
  25141. ' a.DoIt;',
  25142. ' with a do DoIt;',
  25143. 'end;',
  25144. 'procedure FooConst(const a: word);',
  25145. 'begin',
  25146. ' a.DoIt;',
  25147. ' with a do DoIt;',
  25148. 'end;',
  25149. 'procedure FooVar(var a: word);',
  25150. 'begin',
  25151. ' a.DoIt;',
  25152. ' with a do DoIt;',
  25153. 'end;',
  25154. 'begin',
  25155. '']);
  25156. ConvertProgram;
  25157. CheckSource('TestTypeHelper_PassArgs',
  25158. LinesToStr([ // statements
  25159. 'rtl.createHelper(this, "THelper", null, function () {',
  25160. ' this.DoIt = function (e) {',
  25161. ' };',
  25162. '});',
  25163. 'this.FooDefault = function (a) {',
  25164. ' $mod.THelper.DoIt.call({',
  25165. ' get: function () {',
  25166. ' return a;',
  25167. ' },',
  25168. ' set: function (v) {',
  25169. ' a = v;',
  25170. ' }',
  25171. ' }, 123);',
  25172. ' $mod.THelper.DoIt.call({',
  25173. ' get: function () {',
  25174. ' return a;',
  25175. ' },',
  25176. ' set: function (v) {',
  25177. ' a = v;',
  25178. ' }',
  25179. ' }, 123);',
  25180. '};',
  25181. 'this.FooConst = function (a) {',
  25182. ' $mod.THelper.DoIt.call({',
  25183. ' get: function () {',
  25184. ' return a;',
  25185. ' },',
  25186. ' set: function (v) {',
  25187. ' rtl.raiseE("EPropReadOnly");',
  25188. ' }',
  25189. ' }, 123);',
  25190. ' $mod.THelper.DoIt.call({',
  25191. ' get: function () {',
  25192. ' return a;',
  25193. ' },',
  25194. ' set: function () {',
  25195. ' rtl.raiseE("EPropReadOnly");',
  25196. ' }',
  25197. ' }, 123);',
  25198. '};',
  25199. 'this.FooVar = function (a) {',
  25200. ' $mod.THelper.DoIt.call(a, 123);',
  25201. ' var $with = a.get();',
  25202. ' $mod.THelper.DoIt.call(a, 123);',
  25203. '};',
  25204. '']),
  25205. LinesToStr([ // $mod.$main
  25206. '']));
  25207. end;
  25208. procedure TTestModule.TestTypeHelper_PassVarConst;
  25209. begin
  25210. StartProgram(false);
  25211. Add([
  25212. '{$modeswitch typehelpers}',
  25213. 'type',
  25214. ' THelper = type helper for word',
  25215. ' procedure DoIt(e: byte = 123);',
  25216. ' end;',
  25217. 'procedure THelper.DoIt(e: byte);',
  25218. 'begin',
  25219. 'end;',
  25220. 'var a: word;',
  25221. 'const c: word = 2;',
  25222. '{$writeableconst off}',
  25223. 'const r: word = 3;',
  25224. 'begin',
  25225. ' a.DoIt;',
  25226. ' with a do DoIt;',
  25227. ' c.DoIt;',
  25228. ' with c do DoIt;',
  25229. ' r.DoIt;',
  25230. ' with r do DoIt;',
  25231. '']);
  25232. ConvertProgram;
  25233. CheckSource('TestTypeHelper_PassVarConst',
  25234. LinesToStr([ // statements
  25235. 'rtl.createHelper(this, "THelper", null, function () {',
  25236. ' this.DoIt = function (e) {',
  25237. ' };',
  25238. '});',
  25239. 'this.a = 0;',
  25240. 'this.c = 2;',
  25241. 'this.r = 3;',
  25242. '']),
  25243. LinesToStr([ // $mod.$main
  25244. '$mod.THelper.DoIt.call({',
  25245. ' p: $mod,',
  25246. ' get: function () {',
  25247. ' return this.p.a;',
  25248. ' },',
  25249. ' set: function (v) {',
  25250. ' this.p.a = v;',
  25251. ' }',
  25252. '}, 123);',
  25253. 'var $with = $mod.a;',
  25254. '$mod.THelper.DoIt.call({',
  25255. ' get: function () {',
  25256. ' return $with;',
  25257. ' },',
  25258. ' set: function (v) {',
  25259. ' $with = v;',
  25260. ' }',
  25261. '}, 123);',
  25262. '$mod.THelper.DoIt.call({',
  25263. ' p: $mod,',
  25264. ' get: function () {',
  25265. ' return this.p.c;',
  25266. ' },',
  25267. ' set: function (v) {',
  25268. ' this.p.c = v;',
  25269. ' }',
  25270. '}, 123);',
  25271. 'var $with1 = $mod.c;',
  25272. '$mod.THelper.DoIt.call({',
  25273. ' get: function () {',
  25274. ' return $with1;',
  25275. ' },',
  25276. ' set: function (v) {',
  25277. ' $with1 = v;',
  25278. ' }',
  25279. '}, 123);',
  25280. '$mod.THelper.DoIt.call({',
  25281. ' get: function () {',
  25282. ' return 3;',
  25283. ' },',
  25284. ' set: function (v) {',
  25285. ' rtl.raiseE("EPropReadOnly");',
  25286. ' }',
  25287. '}, 123);',
  25288. 'var $with2 = 3;',
  25289. ' $mod.THelper.DoIt.call({',
  25290. ' get: function () {',
  25291. ' return $with2;',
  25292. ' },',
  25293. ' set: function () {',
  25294. ' rtl.raiseE("EPropReadOnly");',
  25295. ' }',
  25296. ' }, 123);',
  25297. '']));
  25298. end;
  25299. procedure TTestModule.TestTypeHelper_PassFuncResult;
  25300. begin
  25301. StartProgram(false);
  25302. Add([
  25303. '{$modeswitch typehelpers}',
  25304. 'type',
  25305. ' THelper = type helper for word',
  25306. ' procedure DoIt(e: byte = 123);',
  25307. ' end;',
  25308. 'procedure THelper.DoIt(e: byte);',
  25309. 'begin',
  25310. 'end;',
  25311. 'function Foo(b: byte = 1): word;',
  25312. 'begin',
  25313. 'end;',
  25314. 'begin',
  25315. ' Foo.DoIt;',
  25316. ' Foo().DoIt;',
  25317. ' with Foo do DoIt;',
  25318. ' with Foo() do DoIt;',
  25319. '']);
  25320. ConvertProgram;
  25321. CheckSource('TestTypeHelper_PassFuncResult',
  25322. LinesToStr([ // statements
  25323. 'rtl.createHelper(this, "THelper", null, function () {',
  25324. ' this.DoIt = function (e) {',
  25325. ' };',
  25326. '});',
  25327. 'this.Foo = function (b) {',
  25328. ' var Result = 0;',
  25329. ' return Result;',
  25330. '};',
  25331. '']),
  25332. LinesToStr([ // $mod.$main
  25333. '$mod.THelper.DoIt.call({',
  25334. ' a: $mod.Foo(1),',
  25335. ' get: function () {',
  25336. ' return this.a;',
  25337. ' },',
  25338. ' set: function (v) {',
  25339. ' this.a = v;',
  25340. ' }',
  25341. '}, 123);',
  25342. '$mod.THelper.DoIt.call({',
  25343. ' a: $mod.Foo(1),',
  25344. ' get: function () {',
  25345. ' return this.a;',
  25346. ' },',
  25347. ' set: function (v) {',
  25348. ' this.a = v;',
  25349. ' }',
  25350. '}, 123);',
  25351. 'var $with = $mod.Foo(1);',
  25352. '$mod.THelper.DoIt.call({',
  25353. ' get: function () {',
  25354. ' return $with;',
  25355. ' },',
  25356. ' set: function (v) {',
  25357. ' $with = v;',
  25358. ' }',
  25359. '}, 123);',
  25360. 'var $with1 = $mod.Foo(1);',
  25361. '$mod.THelper.DoIt.call({',
  25362. ' get: function () {',
  25363. ' return $with1;',
  25364. ' },',
  25365. ' set: function (v) {',
  25366. ' $with1 = v;',
  25367. ' }',
  25368. '}, 123);',
  25369. '']));
  25370. end;
  25371. procedure TTestModule.TestTypeHelper_PassPropertyField;
  25372. begin
  25373. StartProgram(false);
  25374. Add([
  25375. '{$modeswitch typehelpers}',
  25376. 'type',
  25377. ' TObject = class',
  25378. ' FField: word;',
  25379. ' procedure SetField(Value: word);',
  25380. ' property Field: word read FField write SetField;',
  25381. ' end;',
  25382. ' THelper = type helper for word',
  25383. ' procedure Fly;',
  25384. ' class procedure Run; static;',
  25385. ' end;',
  25386. 'procedure TObject.SetField(Value: word);',
  25387. 'begin',
  25388. ' Field.Fly;',
  25389. ' Field.Run;',
  25390. ' Self.Field.Fly;',
  25391. ' Self.Field.Run;',
  25392. ' with Self do begin',
  25393. ' Field.Fly;',
  25394. ' Field.Run;',
  25395. ' end;',
  25396. ' with Self.Field do begin',
  25397. ' Fly;',
  25398. ' Run;',
  25399. ' end;',
  25400. 'end;',
  25401. 'procedure THelper.Fly;',
  25402. 'begin',
  25403. 'end;',
  25404. 'class procedure THelper.Run;',
  25405. 'begin',
  25406. 'end;',
  25407. 'var',
  25408. ' o: TObject;',
  25409. 'begin',
  25410. ' o.Field.Fly;',
  25411. ' o.Field.Run;',
  25412. ' with o do begin',
  25413. ' Field.Fly;',
  25414. ' Field.Run;',
  25415. ' end;',
  25416. ' with o.Field do begin',
  25417. ' Fly;',
  25418. ' Run;',
  25419. ' end;',
  25420. '']);
  25421. ConvertProgram;
  25422. CheckSource('TestTypeHelper_PassPropertyField',
  25423. LinesToStr([ // statements
  25424. 'rtl.createClass(this, "TObject", null, function () {',
  25425. ' this.$init = function () {',
  25426. ' this.FField = 0;',
  25427. ' };',
  25428. ' this.$final = function () {',
  25429. ' };',
  25430. ' this.SetField = function (Value) {',
  25431. ' $mod.THelper.Fly.call({',
  25432. ' p: this,',
  25433. ' get: function () {',
  25434. ' return this.p.FField;',
  25435. ' },',
  25436. ' set: function (v) {',
  25437. ' this.p.FField = v;',
  25438. ' }',
  25439. ' });',
  25440. ' $mod.THelper.Run();',
  25441. ' $mod.THelper.Fly.call({',
  25442. ' p: this,',
  25443. ' get: function () {',
  25444. ' return this.p.FField;',
  25445. ' },',
  25446. ' set: function (v) {',
  25447. ' this.p.FField = v;',
  25448. ' }',
  25449. ' });',
  25450. ' $mod.THelper.Run();',
  25451. ' $mod.THelper.Fly.call({',
  25452. ' p: this,',
  25453. ' get: function () {',
  25454. ' return this.p.FField;',
  25455. ' },',
  25456. ' set: function (v) {',
  25457. ' this.p.FField = v;',
  25458. ' }',
  25459. ' });',
  25460. ' $mod.THelper.Run();',
  25461. ' var $with = this.FField;',
  25462. ' $mod.THelper.Fly.call({',
  25463. ' get: function () {',
  25464. ' return $with;',
  25465. ' },',
  25466. ' set: function (v) {',
  25467. ' $with = v;',
  25468. ' }',
  25469. ' });',
  25470. ' $mod.THelper.Run();',
  25471. ' };',
  25472. '});',
  25473. 'rtl.createHelper(this, "THelper", null, function () {',
  25474. ' this.Fly = function () {',
  25475. ' };',
  25476. ' this.Run = function () {',
  25477. ' };',
  25478. '});',
  25479. 'this.o = null;',
  25480. '']),
  25481. LinesToStr([ // $mod.$main
  25482. '$mod.THelper.Fly.call({',
  25483. ' p: $mod.o,',
  25484. ' get: function () {',
  25485. ' return this.p.FField;',
  25486. ' },',
  25487. ' set: function (v) {',
  25488. ' this.p.FField = v;',
  25489. ' }',
  25490. '});',
  25491. '$mod.THelper.Run();',
  25492. 'var $with = $mod.o;',
  25493. '$mod.THelper.Fly.call({',
  25494. ' p: $with,',
  25495. ' get: function () {',
  25496. ' return this.p.FField;',
  25497. ' },',
  25498. ' set: function (v) {',
  25499. ' this.p.FField = v;',
  25500. ' }',
  25501. '});',
  25502. '$mod.THelper.Run();',
  25503. 'var $with1 = $mod.o.FField;',
  25504. '$mod.THelper.Fly.call({',
  25505. ' get: function () {',
  25506. ' return $with1;',
  25507. ' },',
  25508. ' set: function (v) {',
  25509. ' $with1 = v;',
  25510. ' }',
  25511. '});',
  25512. '$mod.THelper.Run();',
  25513. '']));
  25514. end;
  25515. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  25516. begin
  25517. StartProgram(false);
  25518. Add([
  25519. '{$modeswitch typehelpers}',
  25520. 'type',
  25521. ' TObject = class',
  25522. ' FField: word;',
  25523. ' function GetField: word;',
  25524. ' property Field: word read GetField write FField;',
  25525. ' end;',
  25526. ' THelper = type helper for word',
  25527. ' procedure Fly;',
  25528. ' class procedure Run; static;',
  25529. ' end;',
  25530. 'function TObject.GetField: word;',
  25531. 'begin',
  25532. ' Field.Fly;',
  25533. ' Field.Run;',
  25534. ' Self.Field.Fly;',
  25535. ' Self.Field.Run;',
  25536. ' with Self do begin',
  25537. ' Field.Fly;',
  25538. ' Field.Run;',
  25539. ' end;',
  25540. ' with Self.Field do begin',
  25541. ' Fly;',
  25542. ' Run;',
  25543. ' end;',
  25544. 'end;',
  25545. 'procedure THelper.Fly;',
  25546. 'begin',
  25547. 'end;',
  25548. 'class procedure THelper.Run;',
  25549. 'begin',
  25550. 'end;',
  25551. 'var',
  25552. ' o: TObject;',
  25553. 'begin',
  25554. ' o.Field.Fly;',
  25555. ' o.Field.Run;',
  25556. ' with o do begin',
  25557. ' Field.Fly;',
  25558. ' Field.Run;',
  25559. ' end;',
  25560. ' with o.Field do begin',
  25561. ' Fly;',
  25562. ' Run;',
  25563. ' end;',
  25564. '']);
  25565. ConvertProgram;
  25566. CheckSource('TestTypeHelper_PassPropertyGetter',
  25567. LinesToStr([ // statements
  25568. 'rtl.createClass(this, "TObject", null, function () {',
  25569. ' this.$init = function () {',
  25570. ' this.FField = 0;',
  25571. ' };',
  25572. ' this.$final = function () {',
  25573. ' };',
  25574. ' this.GetField = function () {',
  25575. ' var Result = 0;',
  25576. ' $mod.THelper.Fly.call({',
  25577. ' p: this.GetField(),',
  25578. ' get: function () {',
  25579. ' return this.p;',
  25580. ' },',
  25581. ' set: function (v) {',
  25582. ' this.p = v;',
  25583. ' }',
  25584. ' });',
  25585. ' $mod.THelper.Run();',
  25586. ' $mod.THelper.Fly.call({',
  25587. ' p: this.GetField(),',
  25588. ' get: function () {',
  25589. ' return this.p;',
  25590. ' },',
  25591. ' set: function (v) {',
  25592. ' this.p = v;',
  25593. ' }',
  25594. ' });',
  25595. ' $mod.THelper.Run();',
  25596. ' $mod.THelper.Fly.call({',
  25597. ' p: this.GetField(),',
  25598. ' get: function () {',
  25599. ' return this.p;',
  25600. ' },',
  25601. ' set: function (v) {',
  25602. ' this.p = v;',
  25603. ' }',
  25604. ' });',
  25605. ' $mod.THelper.Run();',
  25606. ' var $with = this.GetField();',
  25607. ' $mod.THelper.Fly.call({',
  25608. ' get: function () {',
  25609. ' return $with;',
  25610. ' },',
  25611. ' set: function (v) {',
  25612. ' $with = v;',
  25613. ' }',
  25614. ' });',
  25615. ' $mod.THelper.Run();',
  25616. ' return Result;',
  25617. ' };',
  25618. '});',
  25619. 'rtl.createHelper(this, "THelper", null, function () {',
  25620. ' this.Fly = function () {',
  25621. ' };',
  25622. ' this.Run = function () {',
  25623. ' };',
  25624. '});',
  25625. 'this.o = null;',
  25626. '']),
  25627. LinesToStr([ // $mod.$main
  25628. '$mod.THelper.Fly.call({',
  25629. ' p: $mod.o.GetField(),',
  25630. ' get: function () {',
  25631. ' return this.p;',
  25632. ' },',
  25633. ' set: function (v) {',
  25634. ' this.p = v;',
  25635. ' }',
  25636. '});',
  25637. '$mod.THelper.Run();',
  25638. 'var $with = $mod.o;',
  25639. '$mod.THelper.Fly.call({',
  25640. ' p: $with.GetField(),',
  25641. ' get: function () {',
  25642. ' return this.p;',
  25643. ' },',
  25644. ' set: function (v) {',
  25645. ' this.p = v;',
  25646. ' }',
  25647. '});',
  25648. '$mod.THelper.Run();',
  25649. 'var $with1 = $mod.o.GetField();',
  25650. '$mod.THelper.Fly.call({',
  25651. ' get: function () {',
  25652. ' return $with1;',
  25653. ' },',
  25654. ' set: function (v) {',
  25655. ' $with1 = v;',
  25656. ' }',
  25657. '});',
  25658. '$mod.THelper.Run();',
  25659. '']));
  25660. end;
  25661. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  25662. begin
  25663. StartProgram(false);
  25664. Add([
  25665. '{$modeswitch typehelpers}',
  25666. 'type',
  25667. ' TObject = class',
  25668. ' class var FField: word;',
  25669. ' class procedure SetField(Value: word);',
  25670. ' class property Field: word read FField write SetField;',
  25671. ' end;',
  25672. ' THelper = type helper for word',
  25673. ' procedure Fly(n: byte);',
  25674. ' end;',
  25675. 'class procedure TObject.SetField(Value: word);',
  25676. 'begin',
  25677. ' Field.Fly(1);',
  25678. ' Self.Field.Fly(2);',
  25679. ' with Self do Field.Fly(3);',
  25680. ' with Self.Field do Fly(4);',
  25681. ' TObject.Field.Fly(5);',
  25682. ' with TObject do Field.Fly(6);',
  25683. ' with TObject.Field do Fly(7);',
  25684. 'end;',
  25685. 'procedure THelper.Fly(n: byte);',
  25686. 'begin',
  25687. 'end;',
  25688. 'var',
  25689. ' o: TObject;',
  25690. 'begin',
  25691. ' o.Field.Fly(11);',
  25692. ' with o do Field.Fly(12);',
  25693. ' with o.Field do Fly(13);',
  25694. ' TObject.Field.Fly(14);',
  25695. ' with TObject do Field.Fly(15);',
  25696. ' with TObject.Field do Fly(16);',
  25697. '']);
  25698. ConvertProgram;
  25699. CheckSource('TestTypeHelper_PassClassPropertyField',
  25700. LinesToStr([ // statements
  25701. 'rtl.createClass(this, "TObject", null, function () {',
  25702. ' this.FField = 0;',
  25703. ' this.$init = function () {',
  25704. ' };',
  25705. ' this.$final = function () {',
  25706. ' };',
  25707. ' this.SetField = function (Value) {',
  25708. ' $mod.THelper.Fly.call({',
  25709. ' p: this,',
  25710. ' get: function () {',
  25711. ' return this.p.FField;',
  25712. ' },',
  25713. ' set: function (v) {',
  25714. ' $mod.TObject.FField = v;',
  25715. ' }',
  25716. ' }, 1);',
  25717. ' $mod.THelper.Fly.call({',
  25718. ' p: this,',
  25719. ' get: function () {',
  25720. ' return this.p.FField;',
  25721. ' },',
  25722. ' set: function (v) {',
  25723. ' $mod.TObject.FField = v;',
  25724. ' }',
  25725. ' }, 2);',
  25726. ' $mod.THelper.Fly.call({',
  25727. ' p: this,',
  25728. ' get: function () {',
  25729. ' return this.p.FField;',
  25730. ' },',
  25731. ' set: function (v) {',
  25732. ' $mod.TObject.FField = v;',
  25733. ' }',
  25734. ' }, 3);',
  25735. ' var $with = this.FField;',
  25736. ' $mod.THelper.Fly.call({',
  25737. ' get: function () {',
  25738. ' return $with;',
  25739. ' },',
  25740. ' set: function (v) {',
  25741. ' $with = v;',
  25742. ' }',
  25743. ' }, 4);',
  25744. ' $mod.THelper.Fly.call({',
  25745. ' p: $mod.TObject,',
  25746. ' get: function () {',
  25747. ' return this.p.FField;',
  25748. ' },',
  25749. ' set: function (v) {',
  25750. ' $mod.TObject.FField = v;',
  25751. ' }',
  25752. ' }, 5);',
  25753. ' var $with1 = $mod.TObject;',
  25754. ' $mod.THelper.Fly.call({',
  25755. ' p: $with1,',
  25756. ' get: function () {',
  25757. ' return this.p.FField;',
  25758. ' },',
  25759. ' set: function (v) {',
  25760. ' $mod.TObject.FField = v;',
  25761. ' }',
  25762. ' }, 6);',
  25763. ' var $with2 = $mod.TObject.FField;',
  25764. ' $mod.THelper.Fly.call({',
  25765. ' get: function () {',
  25766. ' return $with2;',
  25767. ' },',
  25768. ' set: function (v) {',
  25769. ' $with2 = v;',
  25770. ' }',
  25771. ' }, 7);',
  25772. ' };',
  25773. '});',
  25774. 'rtl.createHelper(this, "THelper", null, function () {',
  25775. ' this.Fly = function (n) {',
  25776. ' };',
  25777. '});',
  25778. 'this.o = null;',
  25779. '']),
  25780. LinesToStr([ // $mod.$main
  25781. '$mod.THelper.Fly.call({',
  25782. ' p: $mod.o,',
  25783. ' get: function () {',
  25784. ' return this.p.FField;',
  25785. ' },',
  25786. ' set: function (v) {',
  25787. ' $mod.TObject.FField = v;',
  25788. ' }',
  25789. '}, 11);',
  25790. 'var $with = $mod.o;',
  25791. '$mod.THelper.Fly.call({',
  25792. ' p: $with,',
  25793. ' get: function () {',
  25794. ' return this.p.FField;',
  25795. ' },',
  25796. ' set: function (v) {',
  25797. ' $mod.TObject.FField = v;',
  25798. ' }',
  25799. '}, 12);',
  25800. 'var $with1 = $mod.o.FField;',
  25801. '$mod.THelper.Fly.call({',
  25802. ' get: function () {',
  25803. ' return $with1;',
  25804. ' },',
  25805. ' set: function (v) {',
  25806. ' $with1 = v;',
  25807. ' }',
  25808. '}, 13);',
  25809. '$mod.THelper.Fly.call({',
  25810. ' p: $mod.TObject,',
  25811. ' get: function () {',
  25812. ' return this.p.FField;',
  25813. ' },',
  25814. ' set: function (v) {',
  25815. ' $mod.TObject.FField = v;',
  25816. ' }',
  25817. '}, 14);',
  25818. 'var $with2 = $mod.TObject;',
  25819. '$mod.THelper.Fly.call({',
  25820. ' p: $with2,',
  25821. ' get: function () {',
  25822. ' return this.p.FField;',
  25823. ' },',
  25824. ' set: function (v) {',
  25825. ' $mod.TObject.FField = v;',
  25826. ' }',
  25827. '}, 15);',
  25828. 'var $with3 = $mod.TObject.FField;',
  25829. '$mod.THelper.Fly.call({',
  25830. ' get: function () {',
  25831. ' return $with3;',
  25832. ' },',
  25833. ' set: function (v) {',
  25834. ' $with3 = v;',
  25835. ' }',
  25836. '}, 16);',
  25837. '']));
  25838. end;
  25839. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  25840. begin
  25841. StartProgram(false);
  25842. Add([
  25843. '{$modeswitch typehelpers}',
  25844. 'type',
  25845. ' TObject = class',
  25846. ' class var FField: word;',
  25847. ' class function GetField: word; static;',
  25848. ' class property Field: word read GetField write FField;',
  25849. ' end;',
  25850. ' THelper = type helper for word',
  25851. ' procedure Fly(n: byte);',
  25852. ' end;',
  25853. 'class function TObject.GetField: word;',
  25854. 'begin',
  25855. ' Field.Fly(1);',
  25856. ' TObject.Field.Fly(5);',
  25857. ' with TObject do Field.Fly(6);',
  25858. ' with TObject.Field do Fly(7);',
  25859. 'end;',
  25860. 'procedure THelper.Fly(n: byte);',
  25861. 'begin',
  25862. 'end;',
  25863. 'var',
  25864. ' o: TObject;',
  25865. 'begin',
  25866. ' o.Field.Fly(11);',
  25867. ' with o do Field.Fly(12);',
  25868. ' with o.Field do Fly(13);',
  25869. '']);
  25870. ConvertProgram;
  25871. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  25872. LinesToStr([ // statements
  25873. 'rtl.createClass(this, "TObject", null, function () {',
  25874. ' this.FField = 0;',
  25875. ' this.$init = function () {',
  25876. ' };',
  25877. ' this.$final = function () {',
  25878. ' };',
  25879. ' this.GetField = function () {',
  25880. ' var Result = 0;',
  25881. ' $mod.THelper.Fly.call({',
  25882. ' p: $mod.TObject.GetField(),',
  25883. ' get: function () {',
  25884. ' return this.p;',
  25885. ' },',
  25886. ' set: function (v) {',
  25887. ' this.p = v;',
  25888. ' }',
  25889. ' }, 1);',
  25890. ' $mod.THelper.Fly.call({',
  25891. ' p: $mod.TObject.GetField(),',
  25892. ' get: function () {',
  25893. ' return this.p;',
  25894. ' },',
  25895. ' set: function (v) {',
  25896. ' this.p = v;',
  25897. ' }',
  25898. ' }, 5);',
  25899. ' var $with = $mod.TObject;',
  25900. ' $mod.THelper.Fly.call({',
  25901. ' p: $with.GetField(),',
  25902. ' get: function () {',
  25903. ' return this.p;',
  25904. ' },',
  25905. ' set: function (v) {',
  25906. ' this.p = v;',
  25907. ' }',
  25908. ' }, 6);',
  25909. ' var $with1 = $mod.TObject.GetField();',
  25910. ' $mod.THelper.Fly.call({',
  25911. ' get: function () {',
  25912. ' return $with1;',
  25913. ' },',
  25914. ' set: function (v) {',
  25915. ' $with1 = v;',
  25916. ' }',
  25917. ' }, 7);',
  25918. ' return Result;',
  25919. ' };',
  25920. '});',
  25921. 'rtl.createHelper(this, "THelper", null, function () {',
  25922. ' this.Fly = function (n) {',
  25923. ' };',
  25924. '});',
  25925. 'this.o = null;',
  25926. '']),
  25927. LinesToStr([ // $mod.$main
  25928. '$mod.THelper.Fly.call({',
  25929. ' p: $mod.TObject.GetField(),',
  25930. ' get: function () {',
  25931. ' return this.p;',
  25932. ' },',
  25933. ' set: function (v) {',
  25934. ' this.p = v;',
  25935. ' }',
  25936. '}, 11);',
  25937. 'var $with = $mod.o;',
  25938. '$mod.THelper.Fly.call({',
  25939. ' p: $with.GetField(),',
  25940. ' get: function () {',
  25941. ' return this.p;',
  25942. ' },',
  25943. ' set: function (v) {',
  25944. ' this.p = v;',
  25945. ' }',
  25946. '}, 12);',
  25947. 'var $with1 = $mod.TObject.GetField();',
  25948. '$mod.THelper.Fly.call({',
  25949. ' get: function () {',
  25950. ' return $with1;',
  25951. ' },',
  25952. ' set: function (v) {',
  25953. ' $with1 = v;',
  25954. ' }',
  25955. '}, 13);',
  25956. '']));
  25957. end;
  25958. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  25959. begin
  25960. StartProgram(false);
  25961. Add([
  25962. '{$modeswitch typehelpers}',
  25963. 'type',
  25964. ' TObject = class',
  25965. ' class var FField: word;',
  25966. ' class function GetField: word;',
  25967. ' class property Field: word read GetField write FField;',
  25968. ' end;',
  25969. ' TClass = class of TObject;',
  25970. ' THelper = type helper for word',
  25971. ' procedure Fly(n: byte);',
  25972. ' end;',
  25973. 'class function TObject.GetField: word;',
  25974. 'begin',
  25975. ' Field.Fly(1);',
  25976. ' Self.Field.Fly(5);',
  25977. ' with Self do Field.Fly(6);',
  25978. ' with Self.Field do Fly(7);',
  25979. 'end;',
  25980. 'procedure THelper.Fly(n: byte);',
  25981. 'begin',
  25982. 'end;',
  25983. 'var',
  25984. ' o: TObject;',
  25985. ' c: TClass;',
  25986. 'begin',
  25987. ' o.Field.Fly(11);',
  25988. ' with o do Field.Fly(12);',
  25989. ' with o.Field do Fly(13);',
  25990. ' c.Field.Fly(14);',
  25991. ' with c do Field.Fly(15);',
  25992. ' with c.Field do Fly(16);',
  25993. '']);
  25994. ConvertProgram;
  25995. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  25996. LinesToStr([ // statements
  25997. 'rtl.createClass(this, "TObject", null, function () {',
  25998. ' this.FField = 0;',
  25999. ' this.$init = function () {',
  26000. ' };',
  26001. ' this.$final = function () {',
  26002. ' };',
  26003. ' this.GetField = function () {',
  26004. ' var Result = 0;',
  26005. ' $mod.THelper.Fly.call({',
  26006. ' p: this.GetField(),',
  26007. ' get: function () {',
  26008. ' return this.p;',
  26009. ' },',
  26010. ' set: function (v) {',
  26011. ' this.p = v;',
  26012. ' }',
  26013. ' }, 1);',
  26014. ' $mod.THelper.Fly.call({',
  26015. ' p: this.GetField(),',
  26016. ' get: function () {',
  26017. ' return this.p;',
  26018. ' },',
  26019. ' set: function (v) {',
  26020. ' this.p = v;',
  26021. ' }',
  26022. ' }, 5);',
  26023. ' $mod.THelper.Fly.call({',
  26024. ' p: this.GetField(),',
  26025. ' get: function () {',
  26026. ' return this.p;',
  26027. ' },',
  26028. ' set: function (v) {',
  26029. ' this.p = v;',
  26030. ' }',
  26031. ' }, 6);',
  26032. ' var $with = this.GetField();',
  26033. ' $mod.THelper.Fly.call({',
  26034. ' get: function () {',
  26035. ' return $with;',
  26036. ' },',
  26037. ' set: function (v) {',
  26038. ' $with = v;',
  26039. ' }',
  26040. ' }, 7);',
  26041. ' return Result;',
  26042. ' };',
  26043. '});',
  26044. 'rtl.createHelper(this, "THelper", null, function () {',
  26045. ' this.Fly = function (n) {',
  26046. ' };',
  26047. '});',
  26048. 'this.o = null;',
  26049. 'this.c = null;',
  26050. '']),
  26051. LinesToStr([ // $mod.$main
  26052. '$mod.THelper.Fly.call({',
  26053. ' p: $mod.o.$class.GetField(),',
  26054. ' get: function () {',
  26055. ' return this.p;',
  26056. ' },',
  26057. ' set: function (v) {',
  26058. ' this.p = v;',
  26059. ' }',
  26060. '}, 11);',
  26061. 'var $with = $mod.o;',
  26062. '$mod.THelper.Fly.call({',
  26063. ' p: $with.$class.GetField(),',
  26064. ' get: function () {',
  26065. ' return this.p;',
  26066. ' },',
  26067. ' set: function (v) {',
  26068. ' this.p = v;',
  26069. ' }',
  26070. '}, 12);',
  26071. 'var $with1 = $mod.o.$class.GetField();',
  26072. '$mod.THelper.Fly.call({',
  26073. ' get: function () {',
  26074. ' return $with1;',
  26075. ' },',
  26076. ' set: function (v) {',
  26077. ' $with1 = v;',
  26078. ' }',
  26079. '}, 13);',
  26080. '$mod.THelper.Fly.call({',
  26081. ' p: $mod.c.GetField(),',
  26082. ' get: function () {',
  26083. ' return this.p;',
  26084. ' },',
  26085. ' set: function (v) {',
  26086. ' this.p = v;',
  26087. ' }',
  26088. '}, 14);',
  26089. 'var $with2 = $mod.c;',
  26090. '$mod.THelper.Fly.call({',
  26091. ' p: $with2.GetField(),',
  26092. ' get: function () {',
  26093. ' return this.p;',
  26094. ' },',
  26095. ' set: function (v) {',
  26096. ' this.p = v;',
  26097. ' }',
  26098. '}, 15);',
  26099. 'var $with3 = $mod.c.GetField();',
  26100. '$mod.THelper.Fly.call({',
  26101. ' get: function () {',
  26102. ' return $with3;',
  26103. ' },',
  26104. ' set: function (v) {',
  26105. ' $with3 = v;',
  26106. ' }',
  26107. '}, 16);',
  26108. '']));
  26109. end;
  26110. procedure TTestModule.TestTypeHelper_Property;
  26111. begin
  26112. StartProgram(false);
  26113. Add([
  26114. '{$modeswitch typehelpers}',
  26115. 'type',
  26116. ' THelper = type helper for word',
  26117. ' function GetSize: longint;',
  26118. ' procedure SetSize(Value: longint);',
  26119. ' property Size: longint read GetSize write SetSize;',
  26120. ' end;',
  26121. 'function THelper.GetSize: longint;',
  26122. 'begin',
  26123. ' Result:=Size+1;',
  26124. ' Size:=2;',
  26125. ' Result:=Self.Size+3;',
  26126. ' Self.Size:=4;',
  26127. ' with Self do begin',
  26128. ' Result:=Size+5;',
  26129. ' Size:=6;',
  26130. ' end;',
  26131. 'end;',
  26132. 'procedure THelper.SetSize(Value: longint);',
  26133. 'begin',
  26134. 'end;',
  26135. 'var w: word;',
  26136. 'begin',
  26137. ' w:=w.Size+7;',
  26138. ' w.Size:=w+8;',
  26139. ' with w do begin',
  26140. ' w:=Size+9;',
  26141. ' Size:=w+10;',
  26142. ' end;',
  26143. '']);
  26144. ConvertProgram;
  26145. CheckSource('TestTypeHelper_Property',
  26146. LinesToStr([ // statements
  26147. 'rtl.createHelper(this, "THelper", null, function () {',
  26148. ' this.GetSize = function () {',
  26149. ' var Result = 0;',
  26150. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  26151. ' $mod.THelper.SetSize.call(this, 2);',
  26152. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  26153. ' $mod.THelper.SetSize.call(this, 4);',
  26154. ' var $with = this.get();',
  26155. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  26156. ' $mod.THelper.SetSize.call(this, 6);',
  26157. ' return Result;',
  26158. ' };',
  26159. ' this.SetSize = function (Value) {',
  26160. ' };',
  26161. '});',
  26162. 'this.w = 0;',
  26163. '']),
  26164. LinesToStr([ // $mod.$main
  26165. '$mod.w = $mod.THelper.GetSize.call({',
  26166. ' p: $mod,',
  26167. ' get: function () {',
  26168. ' return this.p.w;',
  26169. ' },',
  26170. ' set: function (v) {',
  26171. ' this.p.w = v;',
  26172. ' }',
  26173. '}) + 7;',
  26174. '$mod.THelper.SetSize.call({',
  26175. ' p: $mod,',
  26176. ' get: function () {',
  26177. ' return this.p.w;',
  26178. ' },',
  26179. ' set: function (v) {',
  26180. ' this.p.w = v;',
  26181. ' }',
  26182. '}, $mod.w + 8);',
  26183. 'var $with = $mod.w;',
  26184. '$mod.w = $mod.THelper.GetSize.call({',
  26185. ' get: function () {',
  26186. ' return $with;',
  26187. ' },',
  26188. ' set: function (v) {',
  26189. ' $with = v;',
  26190. ' }',
  26191. '}) + 9;',
  26192. '$mod.THelper.SetSize.call({',
  26193. ' get: function () {',
  26194. ' return $with;',
  26195. ' },',
  26196. ' set: function (v) {',
  26197. ' $with = v;',
  26198. ' }',
  26199. '}, $mod.w + 10);',
  26200. '']));
  26201. end;
  26202. procedure TTestModule.TestTypeHelper_Property_Array;
  26203. begin
  26204. StartProgram(false);
  26205. Add([
  26206. '{$modeswitch typehelpers}',
  26207. 'type',
  26208. ' THelper = type helper for word',
  26209. ' function GetItems(Index: byte): boolean;',
  26210. ' procedure SetItems(Index: byte; Value: boolean);',
  26211. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  26212. ' end;',
  26213. 'function THelper.GetItems(Index: byte): boolean;',
  26214. 'begin',
  26215. ' Result:=Items[1];',
  26216. ' Items[2]:=false;',
  26217. ' Result:=Self.Items[3];',
  26218. ' Self.Items[4]:=true;',
  26219. ' with Self do begin',
  26220. ' Result:=Items[5];',
  26221. ' Items[6]:=false;',
  26222. ' end;',
  26223. 'end;',
  26224. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  26225. 'begin',
  26226. 'end;',
  26227. 'var',
  26228. ' w: word;',
  26229. ' b: boolean;',
  26230. 'begin',
  26231. ' b:=w.Items[1];',
  26232. ' w.Items[2]:=b;',
  26233. ' with w do begin',
  26234. ' b:=Items[3];',
  26235. ' Items[4]:=b;',
  26236. ' end;',
  26237. '']);
  26238. ConvertProgram;
  26239. CheckSource('TestTypeHelper_Property_Array',
  26240. LinesToStr([ // statements
  26241. 'rtl.createHelper(this, "THelper", null, function () {',
  26242. ' this.GetItems = function (Index) {',
  26243. ' var Result = false;',
  26244. ' Result = $mod.THelper.GetItems.call(this, 1);',
  26245. ' $mod.THelper.SetItems.call(this, 2, false);',
  26246. ' Result = $mod.THelper.GetItems.call(this, 3);',
  26247. ' $mod.THelper.SetItems.call(this, 4, true);',
  26248. ' var $with = this.get();',
  26249. ' Result = $mod.THelper.GetItems.call(this, 5);',
  26250. ' $mod.THelper.SetItems.call(this, 6, false);',
  26251. ' return Result;',
  26252. ' };',
  26253. ' this.SetItems = function (Index, Value) {',
  26254. ' };',
  26255. '});',
  26256. 'this.w = 0;',
  26257. 'this.b = false;',
  26258. '']),
  26259. LinesToStr([ // $mod.$main
  26260. '$mod.b = $mod.THelper.GetItems.call({',
  26261. ' p: $mod,',
  26262. ' get: function () {',
  26263. ' return this.p.w;',
  26264. ' },',
  26265. ' set: function (v) {',
  26266. ' this.p.w = v;',
  26267. ' }',
  26268. '}, 1);',
  26269. '$mod.THelper.SetItems.call({',
  26270. ' p: $mod,',
  26271. ' get: function () {',
  26272. ' return this.p.w;',
  26273. ' },',
  26274. ' set: function (v) {',
  26275. ' this.p.w = v;',
  26276. ' }',
  26277. '}, 2, $mod.b);',
  26278. 'var $with = $mod.w;',
  26279. '$mod.b = $mod.THelper.GetItems.call({',
  26280. ' get: function () {',
  26281. ' return $with;',
  26282. ' },',
  26283. ' set: function (v) {',
  26284. ' $with = v;',
  26285. ' }',
  26286. '}, 3);',
  26287. '$mod.THelper.SetItems.call({',
  26288. ' get: function () {',
  26289. ' return $with;',
  26290. ' },',
  26291. ' set: function (v) {',
  26292. ' $with = v;',
  26293. ' }',
  26294. '}, 4, $mod.b);',
  26295. '']));
  26296. end;
  26297. procedure TTestModule.TestTypeHelper_ClassProperty;
  26298. begin
  26299. StartProgram(false);
  26300. Add([
  26301. '{$modeswitch typehelpers}',
  26302. 'type',
  26303. ' THelper = type helper for word',
  26304. ' class function GetSize: longint; static;',
  26305. ' class procedure SetSize(Value: longint); static;',
  26306. ' class property Size: longint read GetSize write SetSize;',
  26307. ' end;',
  26308. 'class function THelper.GetSize: longint;',
  26309. 'begin',
  26310. ' Result:=Size+1;',
  26311. ' Size:=2;',
  26312. 'end;',
  26313. 'class procedure THelper.SetSize(Value: longint);',
  26314. 'begin',
  26315. 'end;',
  26316. 'begin',
  26317. '']);
  26318. ConvertProgram;
  26319. CheckSource('TestTypeHelper_ClassProperty',
  26320. LinesToStr([ // statements
  26321. 'rtl.createHelper(this, "THelper", null, function () {',
  26322. ' this.GetSize = function () {',
  26323. ' var Result = 0;',
  26324. ' Result = $mod.THelper.GetSize() + 1;',
  26325. ' $mod.THelper.SetSize(2);',
  26326. ' return Result;',
  26327. ' };',
  26328. ' this.SetSize = function (Value) {',
  26329. ' };',
  26330. '});',
  26331. '']),
  26332. LinesToStr([ // $mod.$main
  26333. '']));
  26334. end;
  26335. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  26336. begin
  26337. StartProgram(false);
  26338. Add([
  26339. '{$modeswitch typehelpers}',
  26340. 'type',
  26341. ' THelper = type helper for word',
  26342. ' class function GetItems(Index: byte): boolean; static;',
  26343. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  26344. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  26345. ' end;',
  26346. 'class function THelper.GetItems(Index: byte): boolean;',
  26347. 'begin',
  26348. ' Result:=Items[1];',
  26349. ' Items[2]:=false;',
  26350. 'end;',
  26351. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  26352. 'begin',
  26353. 'end;',
  26354. 'var',
  26355. ' w: word;',
  26356. ' b: boolean;',
  26357. 'begin',
  26358. ' b:=w.Items[1];',
  26359. ' w.Items[2]:=b;',
  26360. ' with w do begin',
  26361. ' b:=Items[3];',
  26362. ' Items[4]:=b;',
  26363. ' end;',
  26364. '']);
  26365. ConvertProgram;
  26366. CheckSource('TestTypeHelper_ClassProperty_Array',
  26367. LinesToStr([ // statements
  26368. 'rtl.createHelper(this, "THelper", null, function () {',
  26369. ' this.GetItems = function (Index) {',
  26370. ' var Result = false;',
  26371. ' Result = $mod.THelper.GetItems(1);',
  26372. ' $mod.THelper.SetItems(2, false);',
  26373. ' return Result;',
  26374. ' };',
  26375. ' this.SetItems = function (Index, Value) {',
  26376. ' };',
  26377. '});',
  26378. 'this.w = 0;',
  26379. 'this.b = false;',
  26380. '']),
  26381. LinesToStr([ // $mod.$main
  26382. '$mod.b = $mod.THelper.GetItems(1);',
  26383. '$mod.THelper.SetItems(2, $mod.b);',
  26384. 'var $with = $mod.w;',
  26385. '$mod.b = $mod.THelper.GetItems(3);',
  26386. '$mod.THelper.SetItems(4, $mod.b);',
  26387. '']));
  26388. end;
  26389. procedure TTestModule.TestTypeHelper_ClassMethod;
  26390. begin
  26391. StartProgram(false);
  26392. Add([
  26393. '{$modeswitch typehelpers}',
  26394. 'type',
  26395. ' THelper = type helper for word',
  26396. ' class procedure DoStatic; static;',
  26397. ' end;',
  26398. 'class procedure THelper.DoStatic;',
  26399. 'begin',
  26400. ' DoStatic;',
  26401. ' DoStatic();',
  26402. 'end;',
  26403. 'var w: word;',
  26404. 'begin',
  26405. ' w.DoStatic;',
  26406. ' w.DoStatic();',
  26407. '']);
  26408. ConvertProgram;
  26409. CheckSource('TestTypeHelper_ClassMethod',
  26410. LinesToStr([ // statements
  26411. 'rtl.createHelper(this, "THelper", null, function () {',
  26412. ' this.DoStatic = function () {',
  26413. ' $mod.THelper.DoStatic();',
  26414. ' $mod.THelper.DoStatic();',
  26415. ' };',
  26416. '});',
  26417. 'this.w = 0;',
  26418. '']),
  26419. LinesToStr([ // $mod.$main
  26420. '$mod.THelper.DoStatic();',
  26421. '$mod.THelper.DoStatic();',
  26422. '']));
  26423. end;
  26424. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  26425. begin
  26426. StartProgram(false);
  26427. Add([
  26428. '{$modeswitch typehelpers}',
  26429. 'type',
  26430. ' THelper = type helper for word',
  26431. ' procedure Run; external name ''Run'';',
  26432. ' end;',
  26433. 'var w: word;',
  26434. 'begin',
  26435. ' w.Run;',
  26436. '']);
  26437. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  26438. ConvertProgram;
  26439. end;
  26440. procedure TTestModule.TestTypeHelper_Constructor;
  26441. begin
  26442. StartProgram(false);
  26443. Add([
  26444. '{$modeswitch typehelpers}',
  26445. 'type',
  26446. ' THelper = type helper for word',
  26447. ' constructor Init(e: longint);',
  26448. ' end;',
  26449. 'constructor THelper.Init(e: longint);',
  26450. 'begin',
  26451. ' Self:=e;',
  26452. ' Init(e+1);',
  26453. 'end;',
  26454. 'var w: word;',
  26455. 'begin',
  26456. ' w:=word.Init(2);',
  26457. ' w:=w.Init(3);',
  26458. ' with word do w:=Init(4);',
  26459. ' with w do w:=Init(5);',
  26460. '']);
  26461. ConvertProgram;
  26462. CheckSource('TestTypeHelper_Constructor',
  26463. LinesToStr([ // statements
  26464. 'rtl.createHelper(this, "THelper", null, function () {',
  26465. ' this.Init = function (e) {',
  26466. ' this.set(e);',
  26467. ' $mod.THelper.Init.call(this, e + 1);',
  26468. ' return this.get();',
  26469. ' };',
  26470. ' this.$new = function (fn, args) {',
  26471. ' return this[fn].apply({',
  26472. ' p: 0,',
  26473. ' get: function () {',
  26474. ' return this.p;',
  26475. ' },',
  26476. ' set: function (v) {',
  26477. ' this.p = v;',
  26478. ' }',
  26479. ' }, args);',
  26480. ' };',
  26481. '});',
  26482. 'this.w = 0;',
  26483. '']),
  26484. LinesToStr([ // $mod.$main
  26485. '$mod.w = $mod.THelper.$new("Init", [2]);',
  26486. '$mod.w = $mod.THelper.Init.call({',
  26487. ' p: $mod,',
  26488. ' get: function () {',
  26489. ' return this.p.w;',
  26490. ' },',
  26491. ' set: function (v) {',
  26492. ' this.p.w = v;',
  26493. ' }',
  26494. '}, 3);',
  26495. '$mod.w = $mod.THelper.$new("Init", [4]);',
  26496. 'var $with = $mod.w;',
  26497. '$mod.w = $mod.THelper.Init.call({',
  26498. ' get: function () {',
  26499. ' return $with;',
  26500. ' },',
  26501. ' set: function (v) {',
  26502. ' $with = v;',
  26503. ' }',
  26504. '}, 5);',
  26505. '']));
  26506. end;
  26507. procedure TTestModule.TestTypeHelper_Word;
  26508. begin
  26509. StartProgram(false);
  26510. Add([
  26511. '{$modeswitch typehelpers}',
  26512. 'type',
  26513. ' THelper = type helper for word',
  26514. ' procedure DoIt(e: byte = 123);',
  26515. ' end;',
  26516. 'procedure THelper.DoIt(e: byte);',
  26517. 'begin',
  26518. ' Self:=e;',
  26519. ' Self:=Self+1;',
  26520. ' with Self do Doit;',
  26521. 'end;',
  26522. 'begin',
  26523. ' word(3).DoIt;',
  26524. '']);
  26525. ConvertProgram;
  26526. CheckSource('TestTypeHelper_Word',
  26527. LinesToStr([ // statements
  26528. 'rtl.createHelper(this, "THelper", null, function () {',
  26529. ' this.DoIt = function (e) {',
  26530. ' this.set(e);',
  26531. ' this.set(this.get() + 1);',
  26532. ' var $with = this.get();',
  26533. ' $mod.THelper.DoIt.call(this, 123);',
  26534. ' };',
  26535. '});',
  26536. '']),
  26537. LinesToStr([ // $mod.$main
  26538. '$mod.THelper.DoIt.call({',
  26539. ' get: function () {',
  26540. ' return 3;',
  26541. ' },',
  26542. ' set: function (v) {',
  26543. ' rtl.raiseE("EPropReadOnly");',
  26544. ' }',
  26545. '}, 123);',
  26546. '']));
  26547. end;
  26548. procedure TTestModule.TestTypeHelper_Boolean;
  26549. begin
  26550. StartProgram(false);
  26551. Add([
  26552. '{$modeswitch typehelpers}',
  26553. 'type',
  26554. ' Integer = longint;',
  26555. ' THelper = type helper for boolean',
  26556. ' procedure Run(e: wordbool = true);',
  26557. ' end;',
  26558. 'procedure THelper.Run(e: wordbool);',
  26559. 'begin',
  26560. ' Self:=e;',
  26561. ' Self:=not Self;',
  26562. ' with Self do Run;',
  26563. ' if Integer(Self)=0 then ;',
  26564. 'end;',
  26565. 'begin',
  26566. ' boolean(3).Run;',
  26567. '']);
  26568. ConvertProgram;
  26569. CheckSource('TestTypeHelper_Boolean',
  26570. LinesToStr([ // statements
  26571. 'rtl.createHelper(this, "THelper", null, function () {',
  26572. ' this.Run = function (e) {',
  26573. ' this.set(e);',
  26574. ' this.set(!this.get());',
  26575. ' var $with = this.get();',
  26576. ' $mod.THelper.Run.call(this, true);',
  26577. ' if ((this.get() ? 1 : 0) === 0) ;',
  26578. ' };',
  26579. '});',
  26580. '']),
  26581. LinesToStr([ // $mod.$main
  26582. '$mod.THelper.Run.call({',
  26583. ' a: 3 != 0,',
  26584. ' get: function () {',
  26585. ' return this.a;',
  26586. ' },',
  26587. ' set: function (v) {',
  26588. ' rtl.raiseE("EPropReadOnly");',
  26589. ' }',
  26590. '}, true);',
  26591. '']));
  26592. end;
  26593. procedure TTestModule.TestTypeHelper_WordBool;
  26594. begin
  26595. StartProgram(false);
  26596. Add([
  26597. '{$modeswitch typehelpers}',
  26598. 'type',
  26599. ' Integer = longint;',
  26600. ' THelper = type helper for WordBool',
  26601. ' procedure Run(e: wordbool = true);',
  26602. ' end;',
  26603. 'procedure THelper.Run(e: wordbool);',
  26604. 'var i: integer;',
  26605. 'begin',
  26606. ' i:=Integer(Self);',
  26607. 'end;',
  26608. 'var w: wordbool;',
  26609. 'begin',
  26610. ' w.Run;',
  26611. ' wordbool(3).Run;',
  26612. '']);
  26613. ConvertProgram;
  26614. CheckSource('TestTypeHelper_WordBool',
  26615. LinesToStr([ // statements
  26616. 'rtl.createHelper(this, "THelper", null, function () {',
  26617. ' this.Run = function (e) {',
  26618. ' var i = 0;',
  26619. ' i = (this.get() ? 1 : 0);',
  26620. ' };',
  26621. '});',
  26622. 'this.w = false;',
  26623. '']),
  26624. LinesToStr([ // $mod.$main
  26625. '$mod.THelper.Run.call({',
  26626. ' p: $mod,',
  26627. ' get: function () {',
  26628. ' return this.p.w;',
  26629. ' },',
  26630. ' set: function (v) {',
  26631. ' this.p.w = v;',
  26632. ' }',
  26633. '}, true);',
  26634. '$mod.THelper.Run.call({',
  26635. ' a: 3 != 0,',
  26636. ' get: function () {',
  26637. ' return this.a;',
  26638. ' },',
  26639. ' set: function (v) {',
  26640. ' rtl.raiseE("EPropReadOnly");',
  26641. ' }',
  26642. '}, true);',
  26643. '']));
  26644. end;
  26645. procedure TTestModule.TestTypeHelper_Double;
  26646. begin
  26647. StartProgram(false);
  26648. Add([
  26649. '{$modeswitch typehelpers}',
  26650. 'type',
  26651. ' Float = type double;',
  26652. ' THelper = type helper for Float',
  26653. ' const NPI = 3.141592;',
  26654. ' function ToStr: String;',
  26655. ' end;',
  26656. 'function THelper.ToStr: String;',
  26657. 'begin',
  26658. 'end;',
  26659. 'procedure DoIt(s: string);',
  26660. 'begin',
  26661. 'end;',
  26662. 'var f: Float;',
  26663. 'begin',
  26664. ' DoIt(f.toStr);',
  26665. ' DoIt(f.toStr());',
  26666. ' (f*f).toStr;',
  26667. ' DoIt((f*f).toStr);',
  26668. '']);
  26669. ConvertProgram;
  26670. CheckSource('TestTypeHelper_Double',
  26671. LinesToStr([ // statements
  26672. 'rtl.createHelper(this, "THelper", null, function () {',
  26673. ' this.NPI = 3.141592;',
  26674. ' this.ToStr = function () {',
  26675. ' var Result = "";',
  26676. ' return Result;',
  26677. ' };',
  26678. '});',
  26679. 'this.DoIt = function (s) {',
  26680. '};',
  26681. 'this.f = 0.0;',
  26682. '']),
  26683. LinesToStr([ // $mod.$main
  26684. '$mod.DoIt($mod.THelper.ToStr.call({',
  26685. ' p: $mod,',
  26686. ' get: function () {',
  26687. ' return this.p.f;',
  26688. ' },',
  26689. ' set: function (v) {',
  26690. ' this.p.f = v;',
  26691. ' }',
  26692. '}));',
  26693. '$mod.DoIt($mod.THelper.ToStr.call({',
  26694. ' p: $mod,',
  26695. ' get: function () {',
  26696. ' return this.p.f;',
  26697. ' },',
  26698. ' set: function (v) {',
  26699. ' this.p.f = v;',
  26700. ' }',
  26701. '}));',
  26702. '$mod.THelper.ToStr.call({',
  26703. ' a: $mod.f * $mod.f,',
  26704. ' get: function () {',
  26705. ' return this.a;',
  26706. ' },',
  26707. ' set: function (v) {',
  26708. ' rtl.raiseE("EPropReadOnly");',
  26709. ' }',
  26710. '});',
  26711. '$mod.DoIt($mod.THelper.ToStr.call({',
  26712. ' a: $mod.f * $mod.f,',
  26713. ' get: function () {',
  26714. ' return this.a;',
  26715. ' },',
  26716. ' set: function (v) {',
  26717. ' rtl.raiseE("EPropReadOnly");',
  26718. ' }',
  26719. '}));',
  26720. '']));
  26721. end;
  26722. procedure TTestModule.TestTypeHelper_NativeInt;
  26723. begin
  26724. StartProgram(false);
  26725. Add([
  26726. '{$modeswitch typehelpers}',
  26727. 'type',
  26728. ' MaxInt = type nativeint;',
  26729. ' THelperI = type helper for MaxInt',
  26730. ' function ToStr: String;',
  26731. ' end;',
  26732. ' MaxUInt = type nativeuint;',
  26733. ' THelperU = type helper for MaxUInt',
  26734. ' function ToStr: String;',
  26735. ' end;',
  26736. 'function THelperI.ToStr: String;',
  26737. 'begin',
  26738. ' Result:=str(Self);',
  26739. 'end;',
  26740. 'function THelperU.ToStr: String;',
  26741. 'begin',
  26742. ' Result:=str(Self);',
  26743. 'end;',
  26744. 'procedure DoIt(s: string);',
  26745. 'begin',
  26746. 'end;',
  26747. 'var i: MaxInt;',
  26748. 'begin',
  26749. ' DoIt(i.toStr);',
  26750. ' DoIt(i.toStr());',
  26751. ' (i*i).toStr;',
  26752. ' DoIt((i*i).toStr);',
  26753. '']);
  26754. ConvertProgram;
  26755. CheckSource('TestTypeHelper_NativeInt',
  26756. LinesToStr([ // statements
  26757. 'rtl.createHelper(this, "THelperI", null, function () {',
  26758. ' this.ToStr = function () {',
  26759. ' var Result = "";',
  26760. ' Result = "" + this.get();',
  26761. ' return Result;',
  26762. ' };',
  26763. '});',
  26764. 'rtl.createHelper(this, "THelperU", null, function () {',
  26765. ' this.ToStr = function () {',
  26766. ' var Result = "";',
  26767. ' Result = "" + this.get();',
  26768. ' return Result;',
  26769. ' };',
  26770. '});',
  26771. 'this.DoIt = function (s) {',
  26772. '};',
  26773. 'this.i = 0;',
  26774. '']),
  26775. LinesToStr([ // $mod.$main
  26776. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26777. ' p: $mod,',
  26778. ' get: function () {',
  26779. ' return this.p.i;',
  26780. ' },',
  26781. ' set: function (v) {',
  26782. ' this.p.i = v;',
  26783. ' }',
  26784. '}));',
  26785. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26786. ' p: $mod,',
  26787. ' get: function () {',
  26788. ' return this.p.i;',
  26789. ' },',
  26790. ' set: function (v) {',
  26791. ' this.p.i = v;',
  26792. ' }',
  26793. '}));',
  26794. '$mod.THelperI.ToStr.call({',
  26795. ' a: $mod.i * $mod.i,',
  26796. ' get: function () {',
  26797. ' return this.a;',
  26798. ' },',
  26799. ' set: function (v) {',
  26800. ' rtl.raiseE("EPropReadOnly");',
  26801. ' }',
  26802. '});',
  26803. '$mod.DoIt($mod.THelperI.ToStr.call({',
  26804. ' a: $mod.i * $mod.i,',
  26805. ' get: function () {',
  26806. ' return this.a;',
  26807. ' },',
  26808. ' set: function (v) {',
  26809. ' rtl.raiseE("EPropReadOnly");',
  26810. ' }',
  26811. '}));',
  26812. '']));
  26813. end;
  26814. procedure TTestModule.TestTypeHelper_StringChar;
  26815. begin
  26816. StartProgram(false);
  26817. Add([
  26818. '{$modeswitch typehelpers}',
  26819. 'type',
  26820. ' TStringHelper = type helper for string',
  26821. ' procedure DoIt(e: byte = 123);',
  26822. ' end;',
  26823. ' TCharHelper = type helper for char',
  26824. ' procedure Fly;',
  26825. ' end;',
  26826. 'procedure TStringHelper.DoIt(e: byte);',
  26827. 'begin',
  26828. ' Self[1]:=''c'';',
  26829. ' Self[2]:=Self[3];',
  26830. 'end;',
  26831. 'procedure TCharHelper.Fly;',
  26832. 'begin',
  26833. ' Self:=''c'';',
  26834. 'end;',
  26835. 'begin',
  26836. ' ''abc''.DoIt;',
  26837. ' ''xyz''.DoIt();',
  26838. ' ''c''.Fly();',
  26839. '']);
  26840. ConvertProgram;
  26841. CheckSource('TestTypeHelper_StringChar',
  26842. LinesToStr([ // statements
  26843. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  26844. ' this.DoIt = function (e) {',
  26845. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  26846. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  26847. ' };',
  26848. '});',
  26849. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  26850. ' this.Fly = function () {',
  26851. ' this.set("c");',
  26852. ' };',
  26853. '});',
  26854. '']),
  26855. LinesToStr([ // $mod.$main
  26856. '$mod.TStringHelper.DoIt.call({',
  26857. ' get: function () {',
  26858. ' return "abc";',
  26859. ' },',
  26860. ' set: function (v) {',
  26861. ' rtl.raiseE("EPropReadOnly");',
  26862. ' }',
  26863. '}, 123);',
  26864. '$mod.TStringHelper.DoIt.call({',
  26865. ' get: function () {',
  26866. ' return "xyz";',
  26867. ' },',
  26868. ' set: function (v) {',
  26869. ' rtl.raiseE("EPropReadOnly");',
  26870. ' }',
  26871. '}, 123);',
  26872. '$mod.TCharHelper.Fly.call({',
  26873. ' get: function () {',
  26874. ' return "c";',
  26875. ' },',
  26876. ' set: function (v) {',
  26877. ' rtl.raiseE("EPropReadOnly");',
  26878. ' }',
  26879. '});',
  26880. '']));
  26881. end;
  26882. procedure TTestModule.TestTypeHelper_JSValue;
  26883. begin
  26884. StartProgram(false);
  26885. Add([
  26886. '{$modeswitch typehelpers}',
  26887. 'type',
  26888. ' TExtValue = type jsvalue;',
  26889. ' THelper = type helper for TExtValue',
  26890. ' function ToStr: String;',
  26891. ' end;',
  26892. 'function THelper.ToStr: String;',
  26893. 'begin',
  26894. 'end;',
  26895. 'var',
  26896. ' s: string;',
  26897. ' v: TExtValue;',
  26898. 'begin',
  26899. ' s:=v.toStr;',
  26900. ' s:=v.toStr();',
  26901. ' TExtValue(s).toStr;',
  26902. '']);
  26903. ConvertProgram;
  26904. CheckSource('TestTypeHelper_JSValue',
  26905. LinesToStr([ // statements
  26906. 'rtl.createHelper(this, "THelper", null, function () {',
  26907. ' this.ToStr = function () {',
  26908. ' var Result = "";',
  26909. ' return Result;',
  26910. ' };',
  26911. '});',
  26912. 'this.s = "";',
  26913. 'this.v = undefined;',
  26914. '']),
  26915. LinesToStr([ // $mod.$main
  26916. '$mod.s = $mod.THelper.ToStr.call({',
  26917. ' p: $mod,',
  26918. ' get: function () {',
  26919. ' return this.p.v;',
  26920. ' },',
  26921. ' set: function (v) {',
  26922. ' this.p.v = v;',
  26923. ' }',
  26924. '});',
  26925. '$mod.s = $mod.THelper.ToStr.call({',
  26926. ' p: $mod,',
  26927. ' get: function () {',
  26928. ' return this.p.v;',
  26929. ' },',
  26930. ' set: function (v) {',
  26931. ' this.p.v = v;',
  26932. ' }',
  26933. '});',
  26934. '$mod.THelper.ToStr.call({',
  26935. ' p: $mod,',
  26936. ' get: function () {',
  26937. ' return this.p.s;',
  26938. ' },',
  26939. ' set: function (v) {',
  26940. ' rtl.raiseE("EPropReadOnly");',
  26941. ' }',
  26942. '});',
  26943. '']));
  26944. end;
  26945. procedure TTestModule.TestTypeHelper_Array;
  26946. begin
  26947. StartProgram(false);
  26948. Add([
  26949. '{$modeswitch typehelpers}',
  26950. 'type',
  26951. ' TArrOfBool = array of boolean;',
  26952. ' TArrOfJS = array of jsvalue;',
  26953. ' THelper = type helper for TArrOfBool',
  26954. ' procedure DoIt(e: byte = 123);',
  26955. ' end;',
  26956. 'procedure THelper.DoIt(e: byte);',
  26957. 'begin',
  26958. ' Self[1]:=true;',
  26959. ' Self[2]:=not Self[3];',
  26960. ' SetLength(Self,4);',
  26961. ' Self:=Concat(Self,[true]);',
  26962. 'end;',
  26963. 'var',
  26964. ' b: TArrOfBool;',
  26965. ' j: TArrOfJS;',
  26966. 'begin',
  26967. ' b.DoIt;',
  26968. ' TArrOfBool(j).DoIt();',
  26969. '']);
  26970. ConvertProgram;
  26971. CheckSource('TestTypeHelper_Array',
  26972. LinesToStr([ // statements
  26973. 'rtl.createHelper(this, "THelper", null, function () {',
  26974. ' this.DoIt = function (e) {',
  26975. ' this.get()[1] = true;',
  26976. ' this.get()[2] = !this.get()[3];',
  26977. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  26978. ' this.set(rtl.arrayPushN(this.get(), true));',
  26979. ' };',
  26980. '});',
  26981. 'this.b = [];',
  26982. 'this.j = [];',
  26983. '']),
  26984. LinesToStr([ // $mod.$main
  26985. '$mod.THelper.DoIt.call({',
  26986. ' p: $mod,',
  26987. ' get: function () {',
  26988. ' return this.p.b;',
  26989. ' },',
  26990. ' set: function (v) {',
  26991. ' this.p.b = v;',
  26992. ' }',
  26993. '}, 123);',
  26994. '$mod.THelper.DoIt.call({',
  26995. ' p: $mod,',
  26996. ' get: function () {',
  26997. ' return this.p.j;',
  26998. ' },',
  26999. ' set: function (v) {',
  27000. ' this.p.j = v;',
  27001. ' }',
  27002. '}, 123);',
  27003. '']));
  27004. end;
  27005. procedure TTestModule.TestTypeHelper_EnumType;
  27006. begin
  27007. StartProgram(false);
  27008. Add([
  27009. '{$modeswitch typehelpers}',
  27010. 'type',
  27011. ' TEnum = (red,blue);',
  27012. ' THelper = type helper for TEnum',
  27013. ' procedure DoIt(e: byte = 123);',
  27014. ' class procedure Swing(w: word); static;',
  27015. ' end;',
  27016. 'procedure THelper.DoIt(e: byte);',
  27017. 'begin',
  27018. ' Self:=red;',
  27019. ' Self:=succ(Self);',
  27020. ' with Self do Doit;',
  27021. 'end;',
  27022. 'class procedure THelper.Swing(w: word);',
  27023. 'begin',
  27024. 'end;',
  27025. 'var e: TEnum;',
  27026. 'begin',
  27027. ' e.DoIt;',
  27028. ' red.DoIt;',
  27029. ' TEnum.blue.DoIt;',
  27030. ' TEnum(1).DoIt;',
  27031. ' TEnum.Swing(3);',
  27032. '']);
  27033. ConvertProgram;
  27034. CheckSource('TestTypeHelper_EnumType',
  27035. LinesToStr([ // statements
  27036. 'this.TEnum = {',
  27037. ' "0": "red",',
  27038. ' red: 0,',
  27039. ' "1": "blue",',
  27040. ' blue: 1',
  27041. '};',
  27042. 'rtl.createHelper(this, "THelper", null, function () {',
  27043. ' this.DoIt = function (e) {',
  27044. ' this.set($mod.TEnum.red);',
  27045. ' this.set(this.get() + 1);',
  27046. ' var $with = this.get();',
  27047. ' $mod.THelper.DoIt.call(this, 123);',
  27048. ' };',
  27049. ' this.Swing = function (w) {',
  27050. ' };',
  27051. '});',
  27052. 'this.e = 0;',
  27053. '']),
  27054. LinesToStr([ // $mod.$main
  27055. '$mod.THelper.DoIt.call({',
  27056. ' p: $mod,',
  27057. ' get: function () {',
  27058. ' return this.p.e;',
  27059. ' },',
  27060. ' set: function (v) {',
  27061. ' this.p.e = v;',
  27062. ' }',
  27063. '}, 123);',
  27064. '$mod.THelper.DoIt.call({',
  27065. ' p: $mod.TEnum,',
  27066. ' get: function () {',
  27067. ' return this.p.red;',
  27068. ' },',
  27069. ' set: function (v) {',
  27070. ' rtl.raiseE("EPropReadOnly");',
  27071. ' }',
  27072. '}, 123);',
  27073. '$mod.THelper.DoIt.call({',
  27074. ' p: $mod.TEnum,',
  27075. ' get: function () {',
  27076. ' return this.p.blue;',
  27077. ' },',
  27078. ' set: function (v) {',
  27079. ' rtl.raiseE("EPropReadOnly");',
  27080. ' }',
  27081. '}, 123);',
  27082. '$mod.THelper.DoIt.call({',
  27083. ' get: function () {',
  27084. ' return 1;',
  27085. ' },',
  27086. ' set: function (v) {',
  27087. ' rtl.raiseE("EPropReadOnly");',
  27088. ' }',
  27089. '}, 123);',
  27090. '$mod.THelper.Swing(3);',
  27091. '']));
  27092. end;
  27093. procedure TTestModule.TestTypeHelper_SetType;
  27094. begin
  27095. StartProgram(false);
  27096. Add([
  27097. '{$modeswitch typehelpers}',
  27098. 'type',
  27099. ' TEnum = (red,blue);',
  27100. ' TSetOfEnum = set of TEnum;',
  27101. ' THelper = type helper for TSetOfEnum',
  27102. ' procedure DoIt(e: byte = 123);',
  27103. ' constructor Init(e: TEnum);',
  27104. ' constructor InitEmpty;',
  27105. ' end;',
  27106. 'procedure THelper.DoIt(e: byte);',
  27107. 'begin',
  27108. ' Self:=[];',
  27109. ' Self:=[red];',
  27110. ' Include(Self,blue);',
  27111. 'end;',
  27112. 'constructor THelper.Init(e: TEnum);',
  27113. 'begin',
  27114. ' Self:=[];',
  27115. ' Self:=[e];',
  27116. ' Include(Self,blue);',
  27117. 'end;',
  27118. 'constructor THelper.InitEmpty;',
  27119. 'begin',
  27120. 'end;',
  27121. 'var s: TSetOfEnum;',
  27122. 'begin',
  27123. ' s.DoIt;',
  27124. //' [red].DoIt;',
  27125. //' with s do DoIt;',
  27126. //' with [red,blue] do DoIt;',
  27127. ' s:=TSetOfEnum.Init(blue);',
  27128. ' s:=s.Init(blue);',
  27129. '']);
  27130. ConvertProgram;
  27131. CheckSource('TestTypeHelper_SetType',
  27132. LinesToStr([ // statements
  27133. 'this.TEnum = {',
  27134. ' "0": "red",',
  27135. ' red: 0,',
  27136. ' "1": "blue",',
  27137. ' blue: 1',
  27138. '};',
  27139. 'rtl.createHelper(this, "THelper", null, function () {',
  27140. ' this.DoIt = function (e) {',
  27141. ' this.set({});',
  27142. ' this.set(rtl.createSet($mod.TEnum.red));',
  27143. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  27144. ' };',
  27145. ' this.Init = function (e) {',
  27146. ' this.set({});',
  27147. ' this.set(rtl.createSet(e));',
  27148. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  27149. ' return this.get();',
  27150. ' };',
  27151. ' this.InitEmpty = function () {',
  27152. ' return this.get();',
  27153. ' };',
  27154. ' this.$new = function (fn, args) {',
  27155. ' return this[fn].apply({',
  27156. ' p: {},',
  27157. ' get: function () {',
  27158. ' return this.p;',
  27159. ' },',
  27160. ' set: function (v) {',
  27161. ' this.p = v;',
  27162. ' }',
  27163. ' }, args);',
  27164. ' };',
  27165. '});',
  27166. 'this.s = {};',
  27167. '']),
  27168. LinesToStr([ // $mod.$main
  27169. '$mod.THelper.DoIt.call({',
  27170. ' p: $mod,',
  27171. ' get: function () {',
  27172. ' return this.p.s;',
  27173. ' },',
  27174. ' set: function (v) {',
  27175. ' this.p.s = v;',
  27176. ' }',
  27177. '}, 123);',
  27178. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  27179. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  27180. ' p: $mod,',
  27181. ' get: function () {',
  27182. ' return this.p.s;',
  27183. ' },',
  27184. ' set: function (v) {',
  27185. ' this.p.s = v;',
  27186. ' }',
  27187. '}, $mod.TEnum.blue));',
  27188. '']));
  27189. end;
  27190. procedure TTestModule.TestTypeHelper_InterfaceType;
  27191. begin
  27192. StartProgram(false);
  27193. Add([
  27194. '{$interfaces com}',
  27195. '{$modeswitch typehelpers}',
  27196. 'type',
  27197. ' IUnknown = interface',
  27198. ' function _AddRef: longint;',
  27199. ' function _Release: longint;',
  27200. ' end;',
  27201. ' TObject = class(IUnknown)',
  27202. ' function _AddRef: longint; virtual; abstract;',
  27203. ' function _Release: longint; virtual; abstract;',
  27204. ' end;',
  27205. ' THelper = type helper for IUnknown',
  27206. ' procedure Fly(e: byte = 123);',
  27207. ' class procedure Run; static;',
  27208. ' end;',
  27209. 'var',
  27210. ' i: IUnknown;',
  27211. ' o: TObject;',
  27212. 'procedure THelper.Fly(e: byte);',
  27213. 'begin',
  27214. ' i:=Self;',
  27215. ' o:=Self as TObject;',
  27216. ' Self:=nil;',
  27217. ' Self:=i;',
  27218. ' Self:=o;',
  27219. ' with Self do begin',
  27220. ' Fly;',
  27221. ' Fly();',
  27222. ' end;',
  27223. 'end;',
  27224. 'class procedure THelper.Run;',
  27225. 'var l: IUnknown;',
  27226. 'begin',
  27227. ' l.Fly;',
  27228. ' l.Fly();',
  27229. 'end;',
  27230. 'begin',
  27231. ' i.Fly;',
  27232. ' i.Fly();',
  27233. ' i.Run;',
  27234. ' i.Run();',
  27235. ' IUnknown.Run;',
  27236. ' IUnknown.Run();',
  27237. '']);
  27238. ConvertProgram;
  27239. CheckSource('TestTypeHelper_InterfaceType',
  27240. LinesToStr([ // statements
  27241. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  27242. 'rtl.createClass(this, "TObject", null, function () {',
  27243. ' this.$init = function () {',
  27244. ' };',
  27245. ' this.$final = function () {',
  27246. ' };',
  27247. ' rtl.addIntf(this, $mod.IUnknown);',
  27248. '});',
  27249. 'rtl.createHelper(this, "THelper", null, function () {',
  27250. ' this.Fly = function (e) {',
  27251. ' var $ir = rtl.createIntfRefs();',
  27252. ' try {',
  27253. ' rtl.setIntfP($mod, "i", this.get());',
  27254. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  27255. ' this.set(null);',
  27256. ' this.set($mod.i);',
  27257. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  27258. ' var $with = this.get();',
  27259. ' $mod.THelper.Fly.call(this, 123);',
  27260. ' $mod.THelper.Fly.call(this, 123);',
  27261. ' } finally {',
  27262. ' $ir.free();',
  27263. ' };',
  27264. ' };',
  27265. ' this.Run = function () {',
  27266. ' var l = null;',
  27267. ' try {',
  27268. ' $mod.THelper.Fly.call({',
  27269. ' get: function () {',
  27270. ' return l;',
  27271. ' },',
  27272. ' set: function (v) {',
  27273. ' l = rtl.setIntfL(l, v);',
  27274. ' }',
  27275. ' }, 123);',
  27276. ' $mod.THelper.Fly.call({',
  27277. ' get: function () {',
  27278. ' return l;',
  27279. ' },',
  27280. ' set: function (v) {',
  27281. ' l = rtl.setIntfL(l, v);',
  27282. ' }',
  27283. ' }, 123);',
  27284. ' } finally {',
  27285. ' rtl._Release(l);',
  27286. ' };',
  27287. ' };',
  27288. '});',
  27289. 'this.i = null;',
  27290. 'this.o = null;',
  27291. '']),
  27292. LinesToStr([ // $mod.$main
  27293. '$mod.THelper.Fly.call({',
  27294. ' p: $mod,',
  27295. ' get: function () {',
  27296. ' return this.p.i;',
  27297. ' },',
  27298. ' set: function (v) {',
  27299. ' rtl.setIntfP(this.p, "i", v);',
  27300. ' }',
  27301. '}, 123);',
  27302. '$mod.THelper.Fly.call({',
  27303. ' p: $mod,',
  27304. ' get: function () {',
  27305. ' return this.p.i;',
  27306. ' },',
  27307. ' set: function (v) {',
  27308. ' rtl.setIntfP(this.p, "i", v);',
  27309. ' }',
  27310. '}, 123);',
  27311. '$mod.THelper.Run();',
  27312. '$mod.THelper.Run();',
  27313. '$mod.THelper.Run();',
  27314. '$mod.THelper.Run();',
  27315. '']));
  27316. end;
  27317. procedure TTestModule.TestTypeHelper_NestedSelf;
  27318. begin
  27319. StartProgram(false);
  27320. Add([
  27321. '{$modeswitch typehelpers}',
  27322. 'type',
  27323. ' THelper = type helper for string',
  27324. ' procedure Run(Value: string);',
  27325. ' end;',
  27326. 'procedure THelper.Run(Value: string);',
  27327. ' function Sub(i: nativeint): boolean;',
  27328. ' begin',
  27329. ' Result:=Self[i+1]=Value[i];',
  27330. ' end;',
  27331. 'begin',
  27332. ' if Self[3]=Value[4] then ;',
  27333. 'end;',
  27334. 'begin',
  27335. '']);
  27336. ConvertProgram;
  27337. CheckSource('TestTypeHelper_NestedSelf',
  27338. LinesToStr([ // statements
  27339. 'rtl.createHelper(this, "THelper", null, function () {',
  27340. ' this.Run = function (Value) {',
  27341. ' var $Self = this;',
  27342. ' function Sub(i) {',
  27343. ' var Result = false;',
  27344. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  27345. ' return Result;',
  27346. ' };',
  27347. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  27348. ' };',
  27349. '});',
  27350. '']),
  27351. LinesToStr([ // $mod.$main
  27352. '']));
  27353. end;
  27354. procedure TTestModule.TestProcType;
  27355. begin
  27356. StartProgram(false);
  27357. Add([
  27358. 'type',
  27359. ' TProcInt = procedure(vI: longint = 1);',
  27360. 'procedure DoIt(vJ: longint);',
  27361. 'begin end;',
  27362. 'var',
  27363. ' b: boolean;',
  27364. ' vP, vQ: tprocint;',
  27365. 'begin',
  27366. ' vp:=nil;',
  27367. ' vp:=vp;',
  27368. ' vp:=@doit;',
  27369. ' vp;',
  27370. ' vp();',
  27371. ' vp(2);',
  27372. ' b:=vp=nil;',
  27373. ' b:=nil=vp;',
  27374. ' b:=vp=vq;',
  27375. ' b:=vp=@doit;',
  27376. ' b:=@doit=vp;',
  27377. ' b:=vp<>nil;',
  27378. ' b:=nil<>vp;',
  27379. ' b:=vp<>vq;',
  27380. ' b:=vp<>@doit;',
  27381. ' b:=@doit<>vp;',
  27382. ' b:=Assigned(vp);',
  27383. ' if Assigned(vp) then ;']);
  27384. ConvertProgram;
  27385. CheckSource('TestProcType',
  27386. LinesToStr([ // statements
  27387. 'this.DoIt = function(vJ) {',
  27388. '};',
  27389. 'this.b = false;',
  27390. 'this.vP = null;',
  27391. 'this.vQ = null;'
  27392. ]),
  27393. LinesToStr([ // $mod.$main
  27394. '$mod.vP = null;',
  27395. '$mod.vP = $mod.vP;',
  27396. '$mod.vP = $mod.DoIt;',
  27397. '$mod.vP(1);',
  27398. '$mod.vP(1);',
  27399. '$mod.vP(2);',
  27400. '$mod.b = $mod.vP === null;',
  27401. '$mod.b = null === $mod.vP;',
  27402. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27403. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27404. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27405. '$mod.b = $mod.vP !== null;',
  27406. '$mod.b = null !== $mod.vP;',
  27407. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27408. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27409. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27410. '$mod.b = $mod.vP != null;',
  27411. 'if ($mod.vP != null) ;',
  27412. '']));
  27413. end;
  27414. procedure TTestModule.TestProcType_Arg;
  27415. begin
  27416. StartProgram(false);
  27417. Add([
  27418. 'type',
  27419. ' TProcInt = procedure(vI: longint = 1);',
  27420. 'procedure DoIt(vJ: longint); begin end;',
  27421. 'procedure DoSome(vP, vQ: TProcInt);',
  27422. 'var',
  27423. ' b: boolean;',
  27424. 'begin',
  27425. ' vp:=nil;',
  27426. ' vp:=vp;',
  27427. ' vp:=@doit;',
  27428. ' vp;',
  27429. ' vp();',
  27430. ' vp(2);',
  27431. ' b:=vp=nil;',
  27432. ' b:=nil=vp;',
  27433. ' b:=vp=vq;',
  27434. ' b:=vp=@doit;',
  27435. ' b:=@doit=vp;',
  27436. ' b:=vp<>nil;',
  27437. ' b:=nil<>vp;',
  27438. ' b:=vp<>vq;',
  27439. ' b:=vp<>@doit;',
  27440. ' b:=@doit<>vp;',
  27441. ' b:=Assigned(vp);',
  27442. ' if Assigned(vp) then ;',
  27443. 'end;',
  27444. 'begin',
  27445. ' DoSome(@DoIt,nil);']);
  27446. ConvertProgram;
  27447. CheckSource('TestProcType_Arg',
  27448. LinesToStr([ // statements
  27449. 'this.DoIt = function(vJ) {',
  27450. '};',
  27451. 'this.DoSome = function(vP, vQ) {',
  27452. ' var b = false;',
  27453. ' vP = null;',
  27454. ' vP = vP;',
  27455. ' vP = $mod.DoIt;',
  27456. ' vP(1);',
  27457. ' vP(1);',
  27458. ' vP(2);',
  27459. ' b = vP === null;',
  27460. ' b = null === vP;',
  27461. ' b = rtl.eqCallback(vP,vQ);',
  27462. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  27463. ' b = rtl.eqCallback($mod.DoIt, vP);',
  27464. ' b = vP !== null;',
  27465. ' b = null !== vP;',
  27466. ' b = !rtl.eqCallback(vP, vQ);',
  27467. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  27468. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  27469. ' b = vP != null;',
  27470. ' if (vP != null) ;',
  27471. '};',
  27472. '']),
  27473. LinesToStr([ // $mod.$main
  27474. '$mod.DoSome($mod.DoIt,null);',
  27475. '']));
  27476. end;
  27477. procedure TTestModule.TestProcType_FunctionFPC;
  27478. begin
  27479. StartProgram(false);
  27480. Add('type');
  27481. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27482. Add('function DoIt(vI: longint): longint;');
  27483. Add('begin end;');
  27484. Add('var');
  27485. Add(' b: boolean;');
  27486. Add(' vP, vQ: tfuncint;');
  27487. Add('begin');
  27488. Add(' vp:=nil;');
  27489. Add(' vp:=vp;');
  27490. Add(' vp:=@doit;'); // ok in fpc and delphi
  27491. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27492. Add(' vp;'); // ok in fpc and delphi
  27493. Add(' vp();');
  27494. Add(' vp(2);');
  27495. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27496. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27497. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27498. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27499. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27500. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27501. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27502. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27503. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27504. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27505. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27506. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27507. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27508. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27509. Add(' b:=Assigned(vp);');
  27510. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27511. Add(' doit(vp());'); // ok in fpc and delphi
  27512. Add(' doit(vp(2));'); // ok in fpc and delphi
  27513. ConvertProgram;
  27514. CheckSource('TestProcType_FunctionFPC',
  27515. LinesToStr([ // statements
  27516. 'this.DoIt = function(vI) {',
  27517. ' var Result = 0;',
  27518. ' return Result;',
  27519. '};',
  27520. 'this.b = false;',
  27521. 'this.vP = null;',
  27522. 'this.vQ = null;'
  27523. ]),
  27524. LinesToStr([ // $mod.$main
  27525. '$mod.vP = null;',
  27526. '$mod.vP = $mod.vP;',
  27527. '$mod.vP = $mod.DoIt;',
  27528. '$mod.vP(1);',
  27529. '$mod.vP(1);',
  27530. '$mod.vP(2);',
  27531. '$mod.b = $mod.vP === null;',
  27532. '$mod.b = null === $mod.vP;',
  27533. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  27534. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27535. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27536. '$mod.b = 4 === $mod.vP(1);',
  27537. '$mod.b = $mod.vP !== null;',
  27538. '$mod.b = null !== $mod.vP;',
  27539. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  27540. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27541. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27542. '$mod.b = 6 !== $mod.vP(1);',
  27543. '$mod.b = $mod.vP != null;',
  27544. '$mod.DoIt($mod.vP(1));',
  27545. '$mod.DoIt($mod.vP(2));',
  27546. '']));
  27547. end;
  27548. procedure TTestModule.TestProcType_FunctionDelphi;
  27549. begin
  27550. StartProgram(false);
  27551. Add('{$mode Delphi}');
  27552. Add('type');
  27553. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27554. Add('function DoIt(vI: longint): longint;');
  27555. Add('begin end;');
  27556. Add('var');
  27557. Add(' b: boolean;');
  27558. Add(' vP, vQ: tfuncint;');
  27559. Add('begin');
  27560. Add(' vp:=nil;');
  27561. Add(' vp:=vp;');
  27562. Add(' vp:=@doit;'); // ok in fpc and delphi
  27563. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27564. Add(' vp;'); // ok in fpc and delphi
  27565. Add(' vp();');
  27566. Add(' vp(2);');
  27567. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27568. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27569. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27570. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27571. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27572. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  27573. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  27574. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27575. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27576. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27577. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27578. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27579. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  27580. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  27581. Add(' b:=Assigned(vp);');
  27582. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  27583. Add(' doit(vp());'); // ok in fpc and delphi
  27584. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  27585. ConvertProgram;
  27586. CheckSource('TestProcType_FunctionDelphi',
  27587. LinesToStr([ // statements
  27588. 'this.DoIt = function(vI) {',
  27589. ' var Result = 0;',
  27590. ' return Result;',
  27591. '};',
  27592. 'this.b = false;',
  27593. 'this.vP = null;',
  27594. 'this.vQ = null;'
  27595. ]),
  27596. LinesToStr([ // $mod.$main
  27597. '$mod.vP = null;',
  27598. '$mod.vP = $mod.vP;',
  27599. '$mod.vP = $mod.DoIt;',
  27600. '$mod.vP = $mod.DoIt;',
  27601. '$mod.vP(1);',
  27602. '$mod.vP(1);',
  27603. '$mod.vP(2);',
  27604. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  27605. '$mod.b = $mod.vP(1) === 3;',
  27606. '$mod.b = 4 === $mod.vP(1);',
  27607. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  27608. '$mod.b = $mod.vP(1) !== 5;',
  27609. '$mod.b = 6 !== $mod.vP(1);',
  27610. '$mod.b = $mod.vP != null;',
  27611. '$mod.DoIt($mod.vP(1));',
  27612. '$mod.DoIt($mod.vP(1));',
  27613. '$mod.DoIt($mod.vP(2));',
  27614. '']));
  27615. end;
  27616. procedure TTestModule.TestProcType_ProcedureDelphi;
  27617. begin
  27618. StartProgram(false);
  27619. Add('{$mode Delphi}');
  27620. Add('type');
  27621. Add(' TProc = procedure;');
  27622. Add('procedure DoIt;');
  27623. Add('begin end;');
  27624. Add('var');
  27625. Add(' b: boolean;');
  27626. Add(' vP, vQ: tproc;');
  27627. Add('begin');
  27628. Add(' vp:=nil;');
  27629. Add(' vp:=vp;');
  27630. Add(' vp:=vq;');
  27631. 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
  27632. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  27633. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  27634. Add(' vp;'); // ok in fpc and delphi
  27635. Add(' vp();');
  27636. // equal
  27637. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  27638. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  27639. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  27640. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27641. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  27642. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  27643. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  27644. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  27645. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  27646. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  27647. // unequal
  27648. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  27649. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  27650. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  27651. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  27652. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  27653. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  27654. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  27655. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  27656. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  27657. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  27658. Add(' b:=Assigned(vp);');
  27659. ConvertProgram;
  27660. CheckSource('TestProcType_ProcedureDelphi',
  27661. LinesToStr([ // statements
  27662. 'this.DoIt = function() {',
  27663. '};',
  27664. 'this.b = false;',
  27665. 'this.vP = null;',
  27666. 'this.vQ = null;'
  27667. ]),
  27668. LinesToStr([ // $mod.$main
  27669. '$mod.vP = null;',
  27670. '$mod.vP = $mod.vP;',
  27671. '$mod.vP = $mod.vQ;',
  27672. '$mod.vP = $mod.DoIt;',
  27673. '$mod.vP = $mod.DoIt;',
  27674. '$mod.vP();',
  27675. '$mod.vP();',
  27676. '$mod.b = $mod.vP === null;',
  27677. '$mod.b = null === $mod.vP;',
  27678. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  27679. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  27680. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  27681. '$mod.b = $mod.vP !== null;',
  27682. '$mod.b = null !== $mod.vP;',
  27683. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  27684. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  27685. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  27686. '$mod.b = $mod.vP != null;',
  27687. '']));
  27688. end;
  27689. procedure TTestModule.TestProcType_AsParam;
  27690. begin
  27691. StartProgram(false);
  27692. Add('type');
  27693. Add(' TFuncInt = function(vA: longint = 1): longint;');
  27694. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  27695. Add('var vJ: tfuncint;');
  27696. Add('begin');
  27697. Add(' vg:=vg;');
  27698. Add(' vj:=vh;');
  27699. Add(' vi:=vi;');
  27700. Add(' doit(vg,vg,vg);');
  27701. Add(' doit(vh,vh,vj);');
  27702. Add(' doit(vi,vi,vi);');
  27703. Add(' doit(vj,vj,vj);');
  27704. Add('end;');
  27705. Add('var i: tfuncint;');
  27706. Add('begin');
  27707. Add(' doit(i,i,i);');
  27708. ConvertProgram;
  27709. CheckSource('TestProcType_AsParam',
  27710. LinesToStr([ // statements
  27711. 'this.DoIt = function (vG,vH,vI) {',
  27712. ' var vJ = null;',
  27713. ' vG = vG;',
  27714. ' vJ = vH;',
  27715. ' vI.set(vI.get());',
  27716. ' $mod.DoIt(vG, vG, {',
  27717. ' get: function () {',
  27718. ' return vG;',
  27719. ' },',
  27720. ' set: function (v) {',
  27721. ' vG = v;',
  27722. ' }',
  27723. ' });',
  27724. ' $mod.DoIt(vH, vH, {',
  27725. ' get: function () {',
  27726. ' return vJ;',
  27727. ' },',
  27728. ' set: function (v) {',
  27729. ' vJ = v;',
  27730. ' }',
  27731. ' });',
  27732. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  27733. ' $mod.DoIt(vJ, vJ, {',
  27734. ' get: function () {',
  27735. ' return vJ;',
  27736. ' },',
  27737. ' set: function (v) {',
  27738. ' vJ = v;',
  27739. ' }',
  27740. ' });',
  27741. '};',
  27742. 'this.i = null;'
  27743. ]),
  27744. LinesToStr([
  27745. '$mod.DoIt($mod.i,$mod.i,{',
  27746. ' p: $mod,',
  27747. ' get: function () {',
  27748. ' return this.p.i;',
  27749. ' },',
  27750. ' set: function (v) {',
  27751. ' this.p.i = v;',
  27752. ' }',
  27753. '});'
  27754. ]));
  27755. end;
  27756. procedure TTestModule.TestProcType_MethodFPC;
  27757. begin
  27758. StartProgram(false);
  27759. Add('type');
  27760. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27761. Add(' TObject = class');
  27762. Add(' function DoIt(vA: longint = 1): longint;');
  27763. Add(' end;');
  27764. Add('function TObject.DoIt(vA: longint = 1): longint;');
  27765. Add('begin');
  27766. Add('end;');
  27767. Add('var');
  27768. Add(' Obj: TObject;');
  27769. Add(' vP: tfuncint;');
  27770. Add(' b: boolean;');
  27771. Add('begin');
  27772. Add(' vp:[email protected];'); // ok in fpc and delphi
  27773. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  27774. Add(' vp;'); // ok in fpc and delphi
  27775. Add(' vp();');
  27776. Add(' vp(2);');
  27777. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  27778. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  27779. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  27780. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27781. ConvertProgram;
  27782. CheckSource('TestProcType_MethodFPC',
  27783. LinesToStr([ // statements
  27784. 'rtl.createClass(this, "TObject", null, function () {',
  27785. ' this.$init = function () {',
  27786. ' };',
  27787. ' this.$final = function () {',
  27788. ' };',
  27789. ' this.DoIt = function (vA) {',
  27790. ' var Result = 0;',
  27791. ' return Result;',
  27792. ' };',
  27793. '});',
  27794. 'this.Obj = null;',
  27795. 'this.vP = null;',
  27796. 'this.b = false;'
  27797. ]),
  27798. LinesToStr([
  27799. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27800. '$mod.vP(1);',
  27801. '$mod.vP(1);',
  27802. '$mod.vP(2);',
  27803. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27804. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27805. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  27806. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  27807. '']));
  27808. end;
  27809. procedure TTestModule.TestProcType_MethodDelphi;
  27810. begin
  27811. StartProgram(false);
  27812. Add([
  27813. '{$mode delphi}',
  27814. 'type',
  27815. ' TFuncInt = function(vA: longint = 1): longint of object;',
  27816. ' TObject = class',
  27817. ' function DoIt(vA: longint = 1): longint;',
  27818. ' end;',
  27819. 'function TObject.DoIt(vA: longint = 1): longint;',
  27820. 'begin',
  27821. 'end;',
  27822. 'var',
  27823. ' Obj: TObject;',
  27824. ' vP: tfuncint;',
  27825. ' b: boolean;',
  27826. 'begin',
  27827. ' vp:[email protected];', // ok in fpc and delphi
  27828. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  27829. ' vp;', // ok in fpc and delphi
  27830. ' vp();',
  27831. ' vp(2);',
  27832. //' b:[email protected];', // ok in fpc, illegal in delphi
  27833. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  27834. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  27835. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  27836. '']);
  27837. ConvertProgram;
  27838. CheckSource('TestProcType_MethodDelphi',
  27839. LinesToStr([ // statements
  27840. 'rtl.createClass(this, "TObject", null, function () {',
  27841. ' this.$init = function () {',
  27842. ' };',
  27843. ' this.$final = function () {',
  27844. ' };',
  27845. ' this.DoIt = function (vA) {',
  27846. ' var Result = 0;',
  27847. ' return Result;',
  27848. ' };',
  27849. '});',
  27850. 'this.Obj = null;',
  27851. 'this.vP = null;',
  27852. 'this.b = false;'
  27853. ]),
  27854. LinesToStr([
  27855. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27856. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  27857. '$mod.vP(1);',
  27858. '$mod.vP(1);',
  27859. '$mod.vP(2);',
  27860. '']));
  27861. end;
  27862. procedure TTestModule.TestProcType_PropertyFPC;
  27863. begin
  27864. StartProgram(false);
  27865. Add('type');
  27866. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  27867. Add(' TObject = class');
  27868. Add(' FOnFoo: TFuncInt;');
  27869. Add(' function DoIt(vA: longint = 1): longint;');
  27870. Add(' function GetFoo: TFuncInt;');
  27871. Add(' procedure SetFoo(const Value: TFuncInt);');
  27872. Add(' function GetEvents(Index: longint): TFuncInt;');
  27873. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  27874. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  27875. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  27876. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  27877. Add(' end;');
  27878. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  27879. Add('function tobject.getfoo: tfuncint; begin end;');
  27880. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  27881. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  27882. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  27883. Add('var');
  27884. Add(' Obj: TObject;');
  27885. Add(' vP: tfuncint;');
  27886. Add(' b: boolean;');
  27887. Add('begin');
  27888. Add(' obj.onfoo:=nil;');
  27889. Add(' obj.onbar:=nil;');
  27890. Add(' obj.events[1]:=nil;');
  27891. Add(' obj.onfoo:=obj.onfoo;');
  27892. Add(' obj.onbar:=obj.onbar;');
  27893. Add(' obj.events[2]:=obj.events[3];');
  27894. Add(' obj.onfoo:[email protected];');
  27895. Add(' obj.onbar:[email protected];');
  27896. Add(' obj.events[4]:[email protected];');
  27897. //Add(' obj.onfoo:=obj.doit;'); // delphi
  27898. //Add(' obj.onbar:=obj.doit;'); // delphi
  27899. //Add(' obj.events[4]:=obj.doit;'); // delphi
  27900. Add(' obj.onfoo;');
  27901. Add(' obj.onbar;');
  27902. //Add(' obj.events[5];'); ToDo in pasresolver
  27903. Add(' obj.onfoo();');
  27904. Add(' obj.onbar();');
  27905. Add(' obj.events[6]();');
  27906. Add(' b:=obj.onfoo=nil;');
  27907. Add(' b:=obj.onbar=nil;');
  27908. Add(' b:=obj.events[7]=nil;');
  27909. Add(' b:=obj.onfoo<>nil;');
  27910. Add(' b:=obj.onbar<>nil;');
  27911. Add(' b:=obj.events[8]<>nil;');
  27912. Add(' b:=obj.onfoo=vp;');
  27913. Add(' b:=obj.onbar=vp;');
  27914. Add(' b:=obj.events[9]=vp;');
  27915. Add(' b:=obj.onfoo=obj.onfoo;');
  27916. Add(' b:=obj.onbar=obj.onfoo;');
  27917. Add(' b:=obj.events[10]=obj.onfoo;');
  27918. Add(' b:=obj.onfoo<>obj.onfoo;');
  27919. Add(' b:=obj.onbar<>obj.onfoo;');
  27920. Add(' b:=obj.events[11]<>obj.onfoo;');
  27921. Add(' b:[email protected];');
  27922. Add(' b:[email protected];');
  27923. Add(' b:=obj.events[12][email protected];');
  27924. Add(' b:=obj.onfoo<>@obj.doit;');
  27925. Add(' b:=obj.onbar<>@obj.doit;');
  27926. Add(' b:=obj.events[12]<>@obj.doit;');
  27927. Add(' b:=Assigned(obj.onfoo);');
  27928. Add(' b:=Assigned(obj.onbar);');
  27929. Add(' b:=Assigned(obj.events[13]);');
  27930. ConvertProgram;
  27931. CheckSource('TestProcType_PropertyFPC',
  27932. LinesToStr([ // statements
  27933. 'rtl.createClass(this, "TObject", null, function () {',
  27934. ' this.$init = function () {',
  27935. ' this.FOnFoo = null;',
  27936. ' };',
  27937. ' this.$final = function () {',
  27938. ' this.FOnFoo = undefined;',
  27939. ' };',
  27940. ' this.DoIt = function (vA) {',
  27941. ' var Result = 0;',
  27942. ' return Result;',
  27943. ' };',
  27944. 'this.GetFoo = function () {',
  27945. ' var Result = null;',
  27946. ' return Result;',
  27947. '};',
  27948. 'this.SetFoo = function (Value) {',
  27949. '};',
  27950. 'this.GetEvents = function (Index) {',
  27951. ' var Result = null;',
  27952. ' return Result;',
  27953. '};',
  27954. 'this.SetEvents = function (Index, Value) {',
  27955. '};',
  27956. '});',
  27957. 'this.Obj = null;',
  27958. 'this.vP = null;',
  27959. 'this.b = false;'
  27960. ]),
  27961. LinesToStr([
  27962. '$mod.Obj.FOnFoo = null;',
  27963. '$mod.Obj.SetFoo(null);',
  27964. '$mod.Obj.SetEvents(1, null);',
  27965. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  27966. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  27967. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  27968. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  27969. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  27970. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  27971. '$mod.Obj.FOnFoo(1);',
  27972. '$mod.Obj.GetFoo();',
  27973. '$mod.Obj.FOnFoo(1);',
  27974. '$mod.Obj.GetFoo()(1);',
  27975. '$mod.Obj.GetEvents(6)(1);',
  27976. '$mod.b = $mod.Obj.FOnFoo === null;',
  27977. '$mod.b = $mod.Obj.GetFoo() === null;',
  27978. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  27979. '$mod.b = $mod.Obj.FOnFoo !== null;',
  27980. '$mod.b = $mod.Obj.GetFoo() !== null;',
  27981. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  27982. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  27983. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  27984. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  27985. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27986. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27987. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  27988. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  27989. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  27990. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  27991. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27992. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27993. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27994. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  27995. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  27996. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  27997. '$mod.b = $mod.Obj.FOnFoo != null;',
  27998. '$mod.b = $mod.Obj.GetFoo() != null;',
  27999. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  28000. '']));
  28001. end;
  28002. procedure TTestModule.TestProcType_PropertyDelphi;
  28003. begin
  28004. StartProgram(false);
  28005. Add('{$mode delphi}');
  28006. Add('type');
  28007. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28008. Add(' TObject = class');
  28009. Add(' FOnFoo: TFuncInt;');
  28010. Add(' function DoIt(vA: longint = 1): longint;');
  28011. Add(' function GetFoo: TFuncInt;');
  28012. Add(' procedure SetFoo(const Value: TFuncInt);');
  28013. Add(' function GetEvents(Index: longint): TFuncInt;');
  28014. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  28015. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  28016. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28017. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  28018. Add(' end;');
  28019. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28020. Add('function tobject.getfoo: tfuncint; begin end;');
  28021. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28022. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  28023. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  28024. Add('var');
  28025. Add(' Obj: TObject;');
  28026. Add(' vP: tfuncint;');
  28027. Add(' b: boolean;');
  28028. Add('begin');
  28029. Add(' obj.onfoo:=nil;');
  28030. Add(' obj.onbar:=nil;');
  28031. Add(' obj.events[1]:=nil;');
  28032. Add(' obj.onfoo:=obj.onfoo;');
  28033. Add(' obj.onbar:=obj.onbar;');
  28034. Add(' obj.events[2]:=obj.events[3];');
  28035. Add(' obj.onfoo:[email protected];');
  28036. Add(' obj.onbar:[email protected];');
  28037. Add(' obj.events[4]:[email protected];');
  28038. Add(' obj.onfoo:=obj.doit;'); // delphi
  28039. Add(' obj.onbar:=obj.doit;'); // delphi
  28040. Add(' obj.events[4]:=obj.doit;'); // delphi
  28041. Add(' obj.onfoo;');
  28042. Add(' obj.onbar;');
  28043. //Add(' obj.events[5];'); ToDo in pasresolver
  28044. Add(' obj.onfoo();');
  28045. Add(' obj.onbar();');
  28046. Add(' obj.events[6]();');
  28047. //Add(' b:=obj.onfoo=nil;'); // fpc
  28048. //Add(' b:=obj.onbar=nil;'); // fpc
  28049. //Add(' b:=obj.events[7]=nil;'); // fpc
  28050. //Add(' b:=obj.onfoo<>nil;'); // fpc
  28051. //Add(' b:=obj.onbar<>nil;'); // fpc
  28052. //Add(' b:=obj.events[8]<>nil;'); // fpc
  28053. Add(' b:=obj.onfoo=vp;');
  28054. Add(' b:=obj.onbar=vp;');
  28055. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  28056. Add(' b:=obj.onfoo=obj.onfoo;');
  28057. Add(' b:=obj.onbar=obj.onfoo;');
  28058. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  28059. Add(' b:=obj.onfoo<>obj.onfoo;');
  28060. Add(' b:=obj.onbar<>obj.onfoo;');
  28061. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  28062. //Add(' b:[email protected];'); // fpc
  28063. //Add(' b:[email protected];'); // fpc
  28064. //Add(' b:=obj.events[12][email protected];'); // fpc
  28065. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  28066. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  28067. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  28068. Add(' b:=Assigned(obj.onfoo);');
  28069. Add(' b:=Assigned(obj.onbar);');
  28070. Add(' b:=Assigned(obj.events[13]);');
  28071. ConvertProgram;
  28072. CheckSource('TestProcType_PropertyDelphi',
  28073. LinesToStr([ // statements
  28074. 'rtl.createClass(this, "TObject", null, function () {',
  28075. ' this.$init = function () {',
  28076. ' this.FOnFoo = null;',
  28077. ' };',
  28078. ' this.$final = function () {',
  28079. ' this.FOnFoo = undefined;',
  28080. ' };',
  28081. ' this.DoIt = function (vA) {',
  28082. ' var Result = 0;',
  28083. ' return Result;',
  28084. ' };',
  28085. 'this.GetFoo = function () {',
  28086. ' var Result = null;',
  28087. ' return Result;',
  28088. '};',
  28089. 'this.SetFoo = function (Value) {',
  28090. '};',
  28091. 'this.GetEvents = function (Index) {',
  28092. ' var Result = null;',
  28093. ' return Result;',
  28094. '};',
  28095. 'this.SetEvents = function (Index, Value) {',
  28096. '};',
  28097. '});',
  28098. 'this.Obj = null;',
  28099. 'this.vP = null;',
  28100. 'this.b = false;'
  28101. ]),
  28102. LinesToStr([
  28103. '$mod.Obj.FOnFoo = null;',
  28104. '$mod.Obj.SetFoo(null);',
  28105. '$mod.Obj.SetEvents(1, null);',
  28106. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  28107. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  28108. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  28109. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  28110. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  28111. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  28112. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  28113. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  28114. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  28115. '$mod.Obj.FOnFoo(1);',
  28116. '$mod.Obj.GetFoo();',
  28117. '$mod.Obj.FOnFoo(1);',
  28118. '$mod.Obj.GetFoo()(1);',
  28119. '$mod.Obj.GetEvents(6)(1);',
  28120. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  28121. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  28122. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  28123. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  28124. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  28125. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  28126. '$mod.b = $mod.Obj.FOnFoo != null;',
  28127. '$mod.b = $mod.Obj.GetFoo() != null;',
  28128. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  28129. '']));
  28130. end;
  28131. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  28132. begin
  28133. StartProgram(false);
  28134. Add('type');
  28135. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  28136. Add(' TObject = class');
  28137. Add(' FOnFoo: TFuncInt;');
  28138. Add(' function DoIt(vA: longint = 1): longint;');
  28139. Add(' function GetFoo: TFuncInt;');
  28140. Add(' procedure SetFoo(const Value: TFuncInt);');
  28141. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  28142. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  28143. Add(' end;');
  28144. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  28145. Add('function tobject.getfoo: tfuncint; begin end;');
  28146. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  28147. Add('var');
  28148. Add(' Obj: TObject;');
  28149. Add(' vP: tfuncint;');
  28150. Add(' b: boolean;');
  28151. Add('begin');
  28152. Add('with obj do begin');
  28153. Add(' fonfoo:=nil;');
  28154. Add(' onfoo:=nil;');
  28155. Add(' onbar:=nil;');
  28156. Add(' fonfoo:=fonfoo;');
  28157. Add(' onfoo:=onfoo;');
  28158. Add(' onbar:=onbar;');
  28159. Add(' fonfoo:=@doit;');
  28160. Add(' onfoo:=@doit;');
  28161. Add(' onbar:=@doit;');
  28162. //Add(' fonfoo:=doit;'); // delphi
  28163. //Add(' onfoo:=doit;'); // delphi
  28164. //Add(' onbar:=doit;'); // delphi
  28165. Add(' fonfoo;');
  28166. Add(' onfoo;');
  28167. Add(' onbar;');
  28168. Add(' fonfoo();');
  28169. Add(' onfoo();');
  28170. Add(' onbar();');
  28171. Add(' b:=fonfoo=nil;');
  28172. Add(' b:=onfoo=nil;');
  28173. Add(' b:=onbar=nil;');
  28174. Add(' b:=fonfoo<>nil;');
  28175. Add(' b:=onfoo<>nil;');
  28176. Add(' b:=onbar<>nil;');
  28177. Add(' b:=fonfoo=vp;');
  28178. Add(' b:=onfoo=vp;');
  28179. Add(' b:=onbar=vp;');
  28180. Add(' b:=fonfoo=fonfoo;');
  28181. Add(' b:=onfoo=onfoo;');
  28182. Add(' b:=onbar=onfoo;');
  28183. Add(' b:=fonfoo<>fonfoo;');
  28184. Add(' b:=onfoo<>onfoo;');
  28185. Add(' b:=onbar<>onfoo;');
  28186. Add(' b:=fonfoo=@doit;');
  28187. Add(' b:=onfoo=@doit;');
  28188. Add(' b:=onbar=@doit;');
  28189. Add(' b:=fonfoo<>@doit;');
  28190. Add(' b:=onfoo<>@doit;');
  28191. Add(' b:=onbar<>@doit;');
  28192. Add(' b:=Assigned(fonfoo);');
  28193. Add(' b:=Assigned(onfoo);');
  28194. Add(' b:=Assigned(onbar);');
  28195. Add('end;');
  28196. ConvertProgram;
  28197. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  28198. LinesToStr([ // statements
  28199. 'rtl.createClass(this, "TObject", null, function () {',
  28200. ' this.$init = function () {',
  28201. ' this.FOnFoo = null;',
  28202. ' };',
  28203. ' this.$final = function () {',
  28204. ' this.FOnFoo = undefined;',
  28205. ' };',
  28206. ' this.DoIt = function (vA) {',
  28207. ' var Result = 0;',
  28208. ' return Result;',
  28209. ' };',
  28210. ' this.GetFoo = function () {',
  28211. ' var Result = null;',
  28212. ' return Result;',
  28213. ' };',
  28214. ' this.SetFoo = function (Value) {',
  28215. ' };',
  28216. '});',
  28217. 'this.Obj = null;',
  28218. 'this.vP = null;',
  28219. 'this.b = false;'
  28220. ]),
  28221. LinesToStr([
  28222. 'var $with = $mod.Obj;',
  28223. '$with.FOnFoo = null;',
  28224. '$with.FOnFoo = null;',
  28225. '$with.SetFoo(null);',
  28226. '$with.FOnFoo = $with.FOnFoo;',
  28227. '$with.FOnFoo = $with.FOnFoo;',
  28228. '$with.SetFoo($with.GetFoo());',
  28229. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28230. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  28231. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  28232. '$with.FOnFoo(1);',
  28233. '$with.FOnFoo(1);',
  28234. '$with.GetFoo();',
  28235. '$with.FOnFoo(1);',
  28236. '$with.FOnFoo(1);',
  28237. '$with.GetFoo()(1);',
  28238. '$mod.b = $with.FOnFoo === null;',
  28239. '$mod.b = $with.FOnFoo === null;',
  28240. '$mod.b = $with.GetFoo() === null;',
  28241. '$mod.b = $with.FOnFoo !== null;',
  28242. '$mod.b = $with.FOnFoo !== null;',
  28243. '$mod.b = $with.GetFoo() !== null;',
  28244. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28245. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  28246. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  28247. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28248. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28249. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28250. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28251. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  28252. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  28253. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28254. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28255. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28256. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28257. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  28258. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  28259. '$mod.b = $with.FOnFoo != null;',
  28260. '$mod.b = $with.FOnFoo != null;',
  28261. '$mod.b = $with.GetFoo() != null;',
  28262. '']));
  28263. end;
  28264. procedure TTestModule.TestProcType_Nested;
  28265. begin
  28266. StartProgram(false);
  28267. Add([
  28268. 'type',
  28269. ' TProcInt = procedure(vI: longint = 1);',
  28270. 'procedure DoIt(vJ: longint);',
  28271. 'var aProc: TProcInt;',
  28272. ' b: boolean;',
  28273. ' procedure Sub(vK: longint);',
  28274. ' var aSub: TProcInt;',
  28275. ' procedure SubSub(vK: longint);',
  28276. ' var aSubSub: TProcInt;',
  28277. ' begin;',
  28278. ' aProc:=@DoIt;',
  28279. ' aSub:=@DoIt;',
  28280. ' aSubSub:=@DoIt;',
  28281. ' aProc:=@Sub;',
  28282. ' aSub:=@Sub;',
  28283. ' aSubSub:=@Sub;',
  28284. ' aProc:=@SubSub;',
  28285. ' aSub:=@SubSub;',
  28286. ' aSubSub:=@SubSub;',
  28287. ' end;',
  28288. ' begin;',
  28289. ' end;',
  28290. 'begin;',
  28291. ' aProc:=@Sub;',
  28292. ' b:=aProc=@Sub;',
  28293. ' b:=@Sub=aProc;',
  28294. 'end;',
  28295. 'begin',
  28296. '']);
  28297. ConvertProgram;
  28298. CheckSource('TestProcType_Nested',
  28299. LinesToStr([ // statements
  28300. 'this.DoIt = function (vJ) {',
  28301. ' var aProc = null;',
  28302. ' var b = false;',
  28303. ' function Sub(vK) {',
  28304. ' var aSub = null;',
  28305. ' function SubSub(vK) {',
  28306. ' var aSubSub = null;',
  28307. ' aProc = $mod.DoIt;',
  28308. ' aSub = $mod.DoIt;',
  28309. ' aSubSub = $mod.DoIt;',
  28310. ' aProc = Sub;',
  28311. ' aSub = Sub;',
  28312. ' aSubSub = Sub;',
  28313. ' aProc = SubSub;',
  28314. ' aSub = SubSub;',
  28315. ' aSubSub = SubSub;',
  28316. ' };',
  28317. ' };',
  28318. ' aProc = Sub;',
  28319. ' b = rtl.eqCallback(aProc, Sub);',
  28320. ' b = rtl.eqCallback(Sub, aProc);',
  28321. '};',
  28322. '']),
  28323. LinesToStr([ // $mod.$main
  28324. '']));
  28325. end;
  28326. procedure TTestModule.TestProcType_NestedOfObject;
  28327. begin
  28328. StartProgram(false);
  28329. Add([
  28330. 'type',
  28331. ' TProcInt = procedure(vI: longint = 1) of object;',
  28332. ' TObject = class',
  28333. ' procedure DoIt(vJ: longint);',
  28334. ' end;',
  28335. 'procedure TObject.DoIt(vJ: longint);',
  28336. 'var aProc: TProcInt;',
  28337. ' b: boolean;',
  28338. ' procedure Sub(vK: longint);',
  28339. ' var aSub: TProcInt;',
  28340. ' procedure SubSub(vK: longint);',
  28341. ' var aSubSub: TProcInt;',
  28342. ' begin;',
  28343. ' aProc:=@DoIt;',
  28344. ' aSub:=@DoIt;',
  28345. ' aSubSub:=@DoIt;',
  28346. ' aProc:=@Sub;',
  28347. ' aSub:=@Sub;',
  28348. ' aSubSub:=@Sub;',
  28349. ' aProc:=@SubSub;',
  28350. ' aSub:=@SubSub;',
  28351. ' aSubSub:=@SubSub;',
  28352. ' end;',
  28353. ' begin;',
  28354. ' end;',
  28355. 'begin;',
  28356. ' aProc:=@Sub;',
  28357. ' b:=aProc=@Sub;',
  28358. ' b:=@Sub=aProc;',
  28359. 'end;',
  28360. 'begin',
  28361. '']);
  28362. ConvertProgram;
  28363. CheckSource('TestProcType_Nested',
  28364. LinesToStr([ // statements
  28365. 'rtl.createClass(this, "TObject", null, function () {',
  28366. ' this.$init = function () {',
  28367. ' };',
  28368. ' this.$final = function () {',
  28369. ' };',
  28370. ' this.DoIt = function (vJ) {',
  28371. ' var $Self = this;',
  28372. ' var aProc = null;',
  28373. ' var b = false;',
  28374. ' function Sub(vK) {',
  28375. ' var aSub = null;',
  28376. ' function SubSub(vK) {',
  28377. ' var aSubSub = null;',
  28378. ' aProc = rtl.createCallback($Self, "DoIt");',
  28379. ' aSub = rtl.createCallback($Self, "DoIt");',
  28380. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  28381. ' aProc = Sub;',
  28382. ' aSub = Sub;',
  28383. ' aSubSub = Sub;',
  28384. ' aProc = SubSub;',
  28385. ' aSub = SubSub;',
  28386. ' aSubSub = SubSub;',
  28387. ' };',
  28388. ' };',
  28389. ' aProc = Sub;',
  28390. ' b = rtl.eqCallback(aProc, Sub);',
  28391. ' b = rtl.eqCallback(Sub, aProc);',
  28392. ' };',
  28393. '});',
  28394. '']),
  28395. LinesToStr([ // $mod.$main
  28396. '']));
  28397. end;
  28398. procedure TTestModule.TestProcType_ReferenceToProc;
  28399. begin
  28400. StartProgram(false);
  28401. Add([
  28402. 'type',
  28403. ' TProcRef = reference to procedure(i: longint = 0);',
  28404. ' TFuncRef = reference to function(i: longint = 0): longint;',
  28405. 'var',
  28406. ' p: TProcRef;',
  28407. ' f: TFuncRef;',
  28408. 'procedure DoIt(i: longint);',
  28409. 'begin',
  28410. 'end;',
  28411. 'function GetIt(i: longint): longint;',
  28412. 'begin',
  28413. ' p:=@DoIt;',
  28414. ' f:=@GetIt;',
  28415. ' f;',
  28416. ' f();',
  28417. ' f(1);',
  28418. 'end;',
  28419. 'begin',
  28420. ' p:=@DoIt;',
  28421. ' f:=@GetIt;',
  28422. ' f;',
  28423. ' f();',
  28424. ' f(1);',
  28425. ' p:=TProcRef(f);',
  28426. '']);
  28427. ConvertProgram;
  28428. CheckSource('TestProcType_ReferenceToProc',
  28429. LinesToStr([ // statements
  28430. 'this.p = null;',
  28431. 'this.f = null;',
  28432. 'this.DoIt = function (i) {',
  28433. '};',
  28434. 'this.GetIt = function (i) {',
  28435. ' var Result = 0;',
  28436. ' $mod.p = $mod.DoIt;',
  28437. ' $mod.f = $mod.GetIt;',
  28438. ' $mod.f(0);',
  28439. ' $mod.f(0);',
  28440. ' $mod.f(1);',
  28441. ' return Result;',
  28442. '};',
  28443. '']),
  28444. LinesToStr([ // $mod.$main
  28445. '$mod.p = $mod.DoIt;',
  28446. '$mod.f = $mod.GetIt;',
  28447. '$mod.f(0);',
  28448. '$mod.f(0);',
  28449. '$mod.f(1);',
  28450. '$mod.p = $mod.f;',
  28451. '']));
  28452. end;
  28453. procedure TTestModule.TestProcType_ReferenceToMethod;
  28454. begin
  28455. StartProgram(false);
  28456. Add([
  28457. 'type',
  28458. ' TFuncRef = reference to function(i: longint = 5): longint;',
  28459. ' TObject = class',
  28460. ' function Grow(s: longint): longint;',
  28461. ' end;',
  28462. 'var',
  28463. ' f: tfuncref;',
  28464. 'function tobject.grow(s: longint): longint;',
  28465. ' function GrowSub(i: longint): longint;',
  28466. ' begin',
  28467. ' f:=@grow;',
  28468. ' f:=@growsub;',
  28469. ' end;',
  28470. 'begin',
  28471. ' f:=@grow;',
  28472. ' f:=@growsub;',
  28473. 'end;',
  28474. 'begin',
  28475. '']);
  28476. ConvertProgram;
  28477. CheckSource('TestProcType_ReferenceToMethod',
  28478. LinesToStr([ // statements
  28479. 'rtl.createClass(this, "TObject", null, function () {',
  28480. ' this.$init = function () {',
  28481. ' };',
  28482. ' this.$final = function () {',
  28483. ' };',
  28484. ' this.Grow = function (s) {',
  28485. ' var $Self = this;',
  28486. ' var Result = 0;',
  28487. ' function GrowSub(i) {',
  28488. ' var Result = 0;',
  28489. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28490. ' $mod.f = GrowSub;',
  28491. ' return Result;',
  28492. ' };',
  28493. ' $mod.f = rtl.createCallback($Self, "Grow");',
  28494. ' $mod.f = GrowSub;',
  28495. ' return Result;',
  28496. ' };',
  28497. '});',
  28498. 'this.f = null;',
  28499. '']),
  28500. LinesToStr([ // $mod.$main
  28501. '']));
  28502. end;
  28503. procedure TTestModule.TestProcType_Typecast;
  28504. begin
  28505. StartProgram(false);
  28506. Add([
  28507. 'type',
  28508. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  28509. ' TEvent = procedure of object;',
  28510. ' TGetter = function:longint of object;',
  28511. ' TProcA = procedure(i: longint);',
  28512. ' TFuncB = function(i, j: longint): longint;',
  28513. 'procedure DoIt(); varargs; begin end;',
  28514. 'var',
  28515. ' Notify: tnotifyevent;',
  28516. ' Event: tevent;',
  28517. ' Getter: tgetter;',
  28518. ' ProcA: tproca;',
  28519. ' FuncB: tfuncb;',
  28520. ' p: pointer;',
  28521. 'begin',
  28522. ' notify:=tnotifyevent(event);',
  28523. ' event:=tevent(event);',
  28524. ' event:=tevent(notify);',
  28525. ' event:=tevent(getter);',
  28526. ' event:=tevent(proca);',
  28527. ' proca:=tproca(funcb);',
  28528. ' funcb:=tfuncb(funcb);',
  28529. ' funcb:=tfuncb(proca);',
  28530. ' funcb:=tfuncb(getter);',
  28531. ' proca:=tproca(p);',
  28532. ' funcb:=tfuncb(p);',
  28533. ' getter:=tgetter(p);',
  28534. ' p:=pointer(notify);',
  28535. ' p:=notify;',
  28536. ' p:=pointer(proca);',
  28537. ' p:=proca;',
  28538. ' p:=pointer(funcb);',
  28539. ' p:=funcb;',
  28540. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  28541. '']);
  28542. ConvertProgram;
  28543. CheckSource('TestProcType_Typecast',
  28544. LinesToStr([ // statements
  28545. 'this.DoIt = function () {',
  28546. '};',
  28547. 'this.Notify = null;',
  28548. 'this.Event = null;',
  28549. 'this.Getter = null;',
  28550. 'this.ProcA = null;',
  28551. 'this.FuncB = null;',
  28552. 'this.p = null;',
  28553. '']),
  28554. LinesToStr([ // $mod.$main
  28555. '$mod.Notify = $mod.Event;',
  28556. '$mod.Event = $mod.Event;',
  28557. '$mod.Event = $mod.Notify;',
  28558. '$mod.Event = $mod.Getter;',
  28559. '$mod.Event = $mod.ProcA;',
  28560. '$mod.ProcA = $mod.FuncB;',
  28561. '$mod.FuncB = $mod.FuncB;',
  28562. '$mod.FuncB = $mod.ProcA;',
  28563. '$mod.FuncB = $mod.Getter;',
  28564. '$mod.ProcA = $mod.p;',
  28565. '$mod.FuncB = $mod.p;',
  28566. '$mod.Getter = $mod.p;',
  28567. '$mod.p = $mod.Notify;',
  28568. '$mod.p = $mod.Notify;',
  28569. '$mod.p = $mod.ProcA;',
  28570. '$mod.p = $mod.ProcA;',
  28571. '$mod.p = $mod.FuncB;',
  28572. '$mod.p = $mod.FuncB;',
  28573. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  28574. '']));
  28575. end;
  28576. procedure TTestModule.TestProcType_PassProcToUntyped;
  28577. begin
  28578. StartProgram(false);
  28579. Add([
  28580. 'type',
  28581. ' TEvent = procedure of object;',
  28582. ' TFunc = function: longint;',
  28583. 'procedure DoIt(); varargs; begin end;',
  28584. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  28585. 'var',
  28586. ' Event: tevent;',
  28587. ' Func: TFunc;',
  28588. 'begin',
  28589. ' doit(event,func);',
  28590. ' dosome(event,event,event);',
  28591. ' dosome(func,func,func);',
  28592. '']);
  28593. ConvertProgram;
  28594. CheckSource('TestProcType_PassProcToUntyped',
  28595. LinesToStr([ // statements
  28596. 'this.DoIt = function () {',
  28597. '};',
  28598. 'this.DoSome = function (a, b, p) {',
  28599. '};',
  28600. 'this.Event = null;',
  28601. 'this.Func = null;',
  28602. '']),
  28603. LinesToStr([ // $mod.$main
  28604. '$mod.DoIt($mod.Event, $mod.Func);',
  28605. '$mod.DoSome($mod.Event, {',
  28606. ' p: $mod,',
  28607. ' get: function () {',
  28608. ' return this.p.Event;',
  28609. ' },',
  28610. ' set: function (v) {',
  28611. ' this.p.Event = v;',
  28612. ' }',
  28613. '}, $mod.Event);',
  28614. '$mod.DoSome($mod.Func, {',
  28615. ' p: $mod,',
  28616. ' get: function () {',
  28617. ' return this.p.Func;',
  28618. ' },',
  28619. ' set: function (v) {',
  28620. ' this.p.Func = v;',
  28621. ' }',
  28622. '}, $mod.Func);',
  28623. '']));
  28624. end;
  28625. procedure TTestModule.TestProcType_PassProcToArray;
  28626. begin
  28627. StartProgram(false);
  28628. Add([
  28629. 'type',
  28630. ' TFunc = function: longint;',
  28631. ' TArrFunc = array of TFunc;',
  28632. 'procedure DoIt(Arr: TArrFunc); begin end;',
  28633. 'function GetIt: longint; begin end;',
  28634. 'var',
  28635. ' Func: tfunc;',
  28636. 'begin',
  28637. ' doit([]);',
  28638. ' doit([@GetIt]);',
  28639. ' doit([Func]);',
  28640. '']);
  28641. ConvertProgram;
  28642. CheckSource('TestProcType_PassProcToArray',
  28643. LinesToStr([ // statements
  28644. 'this.DoIt = function (Arr) {',
  28645. '};',
  28646. 'this.GetIt = function () {',
  28647. ' var Result = 0;',
  28648. ' return Result;',
  28649. '};',
  28650. 'this.Func = null;',
  28651. '']),
  28652. LinesToStr([ // $mod.$main
  28653. '$mod.DoIt([]);',
  28654. '$mod.DoIt([$mod.GetIt]);',
  28655. '$mod.DoIt([$mod.Func]);',
  28656. '']));
  28657. end;
  28658. procedure TTestModule.TestProcType_SafeCallObjFPC;
  28659. begin
  28660. StartProgram(false);
  28661. Add([
  28662. '{$modeswitch externalclass}',
  28663. 'type',
  28664. ' TProc = reference to procedure(i: longint); safecall;',
  28665. ' TEvent = procedure(i: longint) of object; safecall;',
  28666. ' TExtA = class external name ''ExtObj''',
  28667. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28668. ' procedure DoSome(Id: longint = 1);',
  28669. ' procedure SetOnClick(const e: TEvent);',
  28670. ' property OnClick: TEvent write SetOnClick;',
  28671. ' class procedure Fly(Id: longint = 1); static;',
  28672. ' procedure SetOnShow(const p: TProc);',
  28673. ' property OnShow: TProc write SetOnShow;',
  28674. ' end;',
  28675. 'procedure Run(i: longint = 1);',
  28676. 'begin',
  28677. 'end;',
  28678. 'var',
  28679. ' Obj: texta;',
  28680. ' e: TEvent;',
  28681. ' p: TProc;',
  28682. 'begin',
  28683. ' e:=e;',
  28684. ' e:[email protected];',
  28685. ' e:[email protected];',
  28686. ' e:=TEvent(@obj.dosome);', // no safecall
  28687. ' obj.OnClick:[email protected];',
  28688. ' obj.OnClick:[email protected];',
  28689. ' obj.setonclick(@obj.doit);',
  28690. ' obj.setonclick(@obj.dosome);',
  28691. ' p:=@Run;',
  28692. ' p:[email protected];',
  28693. ' obj.OnShow:=@Run;',
  28694. ' obj.OnShow:[email protected];',
  28695. ' obj.setOnShow(@Run);',
  28696. ' obj.setOnShow(@TExtA.Fly);',
  28697. ' with obj do begin',
  28698. ' e:=@doit;',
  28699. ' e:=@dosome;',
  28700. ' OnClick:=@doit;',
  28701. ' OnClick:=@dosome;',
  28702. ' setonclick(@doit);',
  28703. ' setonclick(@dosome);',
  28704. ' OnShow:=@Run;',
  28705. ' setOnShow(@Run);',
  28706. ' end;']);
  28707. ConvertProgram;
  28708. CheckSource('TestProcType_SafeCallObjFPC',
  28709. LinesToStr([ // statements
  28710. 'this.Run = function (i) {',
  28711. '};',
  28712. 'this.Obj = null;',
  28713. 'this.e = null;',
  28714. 'this.p = null;',
  28715. '']),
  28716. LinesToStr([ // $mod.$main
  28717. '$mod.e = $mod.e;',
  28718. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28719. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28720. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28721. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28722. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28723. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28724. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28725. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28726. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28727. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28728. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28729. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28730. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28731. 'var $with = $mod.Obj;',
  28732. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28733. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28734. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28735. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28736. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28737. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28738. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28739. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28740. '']));
  28741. end;
  28742. procedure TTestModule.TestProcType_SafeCallDelphi;
  28743. begin
  28744. StartProgram(false);
  28745. Add([
  28746. '{$mode delphi}',
  28747. '{$modeswitch externalclass}',
  28748. 'type',
  28749. ' TProc = reference to procedure(i: longint); safecall;',
  28750. ' TEvent = procedure(i: longint) of object; safecall;',
  28751. ' TExtA = class external name ''ExtObj''',
  28752. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  28753. ' procedure DoSome(Id: longint = 1);',
  28754. ' procedure SetOnClick(const e: TEvent);',
  28755. ' property OnClick: TEvent write SetOnClick;',
  28756. ' class procedure Fly(Id: longint = 1); static;',
  28757. ' procedure SetOnShow(const p: TProc);',
  28758. ' property OnShow: TProc write SetOnShow;',
  28759. ' end;',
  28760. 'procedure Run(i: longint = 1);',
  28761. 'begin',
  28762. 'end;',
  28763. 'var',
  28764. ' Obj: texta;',
  28765. ' e: TEvent;',
  28766. ' p: TProc;',
  28767. 'begin',
  28768. ' e:=e;',
  28769. ' e:=obj.doit;',
  28770. ' e:=obj.dosome;',
  28771. ' e:=TEvent(@obj.dosome);', // no safecall
  28772. ' obj.OnClick:=obj.doit;',
  28773. ' obj.OnClick:=obj.dosome;',
  28774. ' obj.setonclick(obj.doit);',
  28775. ' obj.setonclick(obj.dosome);',
  28776. ' p:=Run;',
  28777. ' p:=TExtA.Fly;',
  28778. ' obj.OnShow:=Run;',
  28779. ' obj.OnShow:=TExtA.Fly;',
  28780. ' obj.setOnShow(Run);',
  28781. ' obj.setOnShow(TExtA.Fly);',
  28782. ' with obj do begin',
  28783. ' e:=doit;',
  28784. ' e:=dosome;',
  28785. ' OnClick:=doit;',
  28786. ' OnClick:=dosome;',
  28787. ' setonclick(doit);',
  28788. ' setonclick(dosome);',
  28789. ' OnShow:=@Run;',
  28790. ' setOnShow(@Run);',
  28791. ' end;']);
  28792. ConvertProgram;
  28793. CheckSource('TestProcType_SafeCallDelphi',
  28794. LinesToStr([ // statements
  28795. 'this.Run = function (i) {',
  28796. '};',
  28797. 'this.Obj = null;',
  28798. 'this.e = null;',
  28799. 'this.p = null;',
  28800. '']),
  28801. LinesToStr([ // $mod.$main
  28802. '$mod.e = $mod.e;',
  28803. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  28804. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  28805. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  28806. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28807. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28808. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  28809. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  28810. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  28811. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  28812. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28813. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28814. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28815. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  28816. 'var $with = $mod.Obj;',
  28817. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  28818. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  28819. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28820. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28821. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  28822. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  28823. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28824. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  28825. '']));
  28826. end;
  28827. procedure TTestModule.TestProcType_SafeCall_Arg;
  28828. begin
  28829. StartProgram(false);
  28830. Add([
  28831. 'type',
  28832. ' TSafecallProc = reference to procedure; safecall;',
  28833. 'procedure Fly(const aHandler: TSafecallProc);',
  28834. 'var',
  28835. ' P: TSafecallProc;',
  28836. 'begin',
  28837. ' P := aHandler;',
  28838. ' Fly(P);',
  28839. ' Fly(aHandler);',
  28840. 'end;',
  28841. 'begin',
  28842. ' Fly(nil);',
  28843. '']);
  28844. ConvertProgram;
  28845. CheckSource('TestProcType_SafeCall_Arg',
  28846. LinesToStr([ // statements
  28847. 'this.Fly = function (aHandler) {',
  28848. ' var P = null;',
  28849. ' P = aHandler;',
  28850. ' $mod.Fly(P);',
  28851. ' $mod.Fly(aHandler);',
  28852. '};',
  28853. '']),
  28854. LinesToStr([ // $mod.$main
  28855. '$mod.Fly(null);',
  28856. '']));
  28857. end;
  28858. procedure TTestModule.TestPointer;
  28859. begin
  28860. StartProgram(false);
  28861. Add(['type',
  28862. ' TObject = class end;',
  28863. ' TClass = class of TObject;',
  28864. ' TArrInt = array of longint;',
  28865. 'const',
  28866. ' n = nil;',
  28867. 'var',
  28868. ' v: jsvalue;',
  28869. ' Obj: tobject;',
  28870. ' C: tclass;',
  28871. ' a: tarrint;',
  28872. ' p: Pointer = nil;',
  28873. ' s: string;',
  28874. 'begin',
  28875. ' p:=p;',
  28876. ' p:=nil;',
  28877. ' if p=nil then;',
  28878. ' if nil=p then;',
  28879. ' if Assigned(p) then;',
  28880. ' p:=Pointer(v);',
  28881. ' p:=obj;',
  28882. ' p:=c;',
  28883. ' p:=a;',
  28884. ' p:=tobject;',
  28885. ' obj:=TObject(p);',
  28886. ' c:=TClass(p);',
  28887. ' a:=TArrInt(p);',
  28888. ' p:=n;',
  28889. ' p:=Pointer(a);',
  28890. ' p:=pointer(s);',
  28891. ' s:=string(p);',
  28892. '']);
  28893. ConvertProgram;
  28894. CheckSource('TestPointer',
  28895. LinesToStr([ // statements
  28896. 'rtl.createClass(this, "TObject", null, function () {',
  28897. ' this.$init = function () {',
  28898. ' };',
  28899. ' this.$final = function () {',
  28900. ' };',
  28901. '});',
  28902. 'this.n = null;',
  28903. 'this.v = undefined;',
  28904. 'this.Obj = null;',
  28905. 'this.C = null;',
  28906. 'this.a = [];',
  28907. 'this.p = null;',
  28908. 'this.s = "";',
  28909. '']),
  28910. LinesToStr([ // $mod.$main
  28911. '$mod.p = $mod.p;',
  28912. '$mod.p = null;',
  28913. 'if ($mod.p === null) ;',
  28914. 'if (null === $mod.p) ;',
  28915. 'if ($mod.p != null) ;',
  28916. '$mod.p = $mod.v;',
  28917. '$mod.p = $mod.Obj;',
  28918. '$mod.p = $mod.C;',
  28919. '$mod.p = $mod.a;',
  28920. '$mod.p = $mod.TObject;',
  28921. '$mod.Obj = $mod.p;',
  28922. '$mod.C = $mod.p;',
  28923. '$mod.a = $mod.p;',
  28924. '$mod.p = null;',
  28925. '$mod.p = $mod.a;',
  28926. '$mod.p = $mod.s;',
  28927. '$mod.s = $mod.p;',
  28928. '']));
  28929. end;
  28930. procedure TTestModule.TestPointer_Proc;
  28931. begin
  28932. StartProgram(false);
  28933. Add('type');
  28934. Add(' TObject = class');
  28935. Add(' procedure DoIt; virtual; abstract;');
  28936. Add(' end;');
  28937. Add('procedure DoSome; begin end;');
  28938. Add('var');
  28939. Add(' o: TObject;');
  28940. Add(' p: Pointer;');
  28941. Add('begin');
  28942. Add(' p:=@DoSome;');
  28943. Add(' p:[email protected];');
  28944. ConvertProgram;
  28945. CheckSource('TestPointer_Proc',
  28946. LinesToStr([ // statements
  28947. 'rtl.createClass(this, "TObject", null, function () {',
  28948. ' this.$init = function () {',
  28949. ' };',
  28950. ' this.$final = function () {',
  28951. ' };',
  28952. '});',
  28953. 'this.DoSome = function () {',
  28954. '};',
  28955. 'this.o = null;',
  28956. 'this.p = null;',
  28957. '']),
  28958. LinesToStr([ // $mod.$main
  28959. '$mod.p = $mod.DoSome;',
  28960. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  28961. '']));
  28962. end;
  28963. procedure TTestModule.TestPointer_AssignRecordFail;
  28964. begin
  28965. StartProgram(false);
  28966. Add('type');
  28967. Add(' TRec = record end;');
  28968. Add('var');
  28969. Add(' p: Pointer;');
  28970. Add(' r: TRec;');
  28971. Add('begin');
  28972. Add(' p:=r;');
  28973. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  28974. nIncompatibleTypesGotExpected);
  28975. ConvertProgram;
  28976. end;
  28977. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  28978. begin
  28979. StartProgram(false);
  28980. Add('type');
  28981. Add(' TArr = array[boolean] of longint;');
  28982. Add('var');
  28983. Add(' p: Pointer;');
  28984. Add(' a: TArr;');
  28985. Add('begin');
  28986. Add(' p:=a;');
  28987. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  28988. nIncompatibleTypesGotExpected);
  28989. ConvertProgram;
  28990. end;
  28991. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  28992. begin
  28993. StartProgram(false);
  28994. Add([
  28995. 'procedure DoIt(args: array of jsvalue); begin end;',
  28996. 'procedure DoAll; varargs; begin end;',
  28997. 'var',
  28998. ' v: jsvalue;',
  28999. 'begin',
  29000. ' DoIt([pointer(v)]);',
  29001. ' DoAll(pointer(v));',
  29002. '']);
  29003. ConvertProgram;
  29004. CheckSource('TestPointer_TypeCastJSValueToPointer',
  29005. LinesToStr([ // statements
  29006. 'this.DoIt = function (args) {',
  29007. '};',
  29008. 'this.DoAll = function () {',
  29009. '};',
  29010. 'this.v = undefined;',
  29011. '']),
  29012. LinesToStr([ // $mod.$main
  29013. '$mod.DoIt([$mod.v]);',
  29014. '$mod.DoAll($mod.v);',
  29015. '']));
  29016. end;
  29017. procedure TTestModule.TestPointer_NonRecordFail;
  29018. begin
  29019. StartProgram(false);
  29020. Add([
  29021. 'type',
  29022. ' p = ^longint;',
  29023. 'begin',
  29024. '']);
  29025. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  29026. ConvertProgram;
  29027. end;
  29028. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  29029. begin
  29030. StartProgram(false);
  29031. Add([
  29032. 'procedure DoIt(p: ^longint); begin end;',
  29033. 'begin',
  29034. '']);
  29035. SetExpectedParserError('Parameters or result types cannot contain local type definitions. Use a separate type definition in a type block. at token "^" in file test1.pp at line 3 column 19',nParserParamsOrResultTypesNoLocalTypeDefs);
  29036. ConvertProgram;
  29037. end;
  29038. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  29039. begin
  29040. StartProgram(false);
  29041. Add([
  29042. 'var p: ^longint;',
  29043. 'begin',
  29044. '']);
  29045. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  29046. ConvertProgram;
  29047. end;
  29048. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  29049. begin
  29050. StartProgram(false);
  29051. Add([
  29052. 'function DoIt: ^longint; begin end;',
  29053. 'begin',
  29054. '']);
  29055. SetExpectedParserError('Parameters or result types cannot contain local type definitions. Use a separate type definition in a type block. at token "^" in file test1.pp at line 3 column 16',nParserParamsOrResultTypesNoLocalTypeDefs);
  29056. ConvertProgram;
  29057. end;
  29058. procedure TTestModule.TestPointer_AddrOperatorFail;
  29059. begin
  29060. StartProgram(false);
  29061. Add([
  29062. 'var i: longint;',
  29063. 'begin',
  29064. ' if @i=nil then ;',
  29065. '']);
  29066. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  29067. ConvertProgram;
  29068. end;
  29069. procedure TTestModule.TestPointer_ArrayParamsFail;
  29070. begin
  29071. StartProgram(false);
  29072. Add([
  29073. 'var',
  29074. ' p: Pointer;',
  29075. 'begin',
  29076. ' p:=p[1];',
  29077. '']);
  29078. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  29079. ConvertProgram;
  29080. end;
  29081. procedure TTestModule.TestPointer_PointerAddFail;
  29082. begin
  29083. StartProgram(false);
  29084. Add([
  29085. 'var',
  29086. ' p: Pointer;',
  29087. 'begin',
  29088. ' p:=p+1;',
  29089. '']);
  29090. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  29091. ConvertProgram;
  29092. end;
  29093. procedure TTestModule.TestPointer_IncPointerFail;
  29094. begin
  29095. StartProgram(false);
  29096. Add([
  29097. 'var',
  29098. ' p: Pointer;',
  29099. 'begin',
  29100. ' inc(p,1);',
  29101. '']);
  29102. SetExpectedPasResolverError('Incompatible type for arg no. 1: Got "Pointer", expected "integer"',
  29103. nIncompatibleTypeArgNo);
  29104. ConvertProgram;
  29105. end;
  29106. procedure TTestModule.TestPointer_Record;
  29107. begin
  29108. StartProgram(false);
  29109. Add([
  29110. 'type',
  29111. ' TRec = record x: longint; end;',
  29112. ' PRec = ^TRec;',
  29113. 'var',
  29114. ' r: TRec;',
  29115. ' p: PRec;',
  29116. ' q: ^TRec;',
  29117. ' Ptr: pointer;',
  29118. 'begin',
  29119. ' new(p);',
  29120. ' p:=@r;',
  29121. ' r:=p^;',
  29122. ' r.x:=p^.x;',
  29123. ' p^.x:=r.x;',
  29124. ' if p^.x=3 then ;',
  29125. ' if 4=p^.x then ;',
  29126. ' dispose(p);',
  29127. ' new(q);',
  29128. ' dispose(q);',
  29129. ' Ptr:=p;',
  29130. ' p:=PRec(ptr);',
  29131. '']);
  29132. ConvertProgram;
  29133. CheckSource('TestPointer_Record',
  29134. LinesToStr([ // statements
  29135. 'rtl.recNewT(this, "TRec", function () {',
  29136. ' this.x = 0;',
  29137. ' this.$eq = function (b) {',
  29138. ' return this.x === b.x;',
  29139. ' };',
  29140. ' this.$assign = function (s) {',
  29141. ' this.x = s.x;',
  29142. ' return this;',
  29143. ' };',
  29144. '});',
  29145. 'this.r = this.TRec.$new();',
  29146. 'this.p = null;',
  29147. 'this.q = null;',
  29148. 'this.Ptr = null;',
  29149. '']),
  29150. LinesToStr([ // $mod.$main
  29151. '$mod.p = $mod.TRec.$new();',
  29152. '$mod.p = $mod.r;',
  29153. '$mod.r.$assign($mod.p);',
  29154. '$mod.r.x = $mod.p.x;',
  29155. '$mod.p.x = $mod.r.x;',
  29156. 'if ($mod.p.x === 3) ;',
  29157. 'if (4 === $mod.p.x) ;',
  29158. '$mod.p = null;',
  29159. '$mod.q = $mod.TRec.$new();',
  29160. '$mod.q = null;',
  29161. '$mod.Ptr = $mod.p;',
  29162. '$mod.p = $mod.Ptr;',
  29163. '']));
  29164. end;
  29165. procedure TTestModule.TestPointer_RecordArg;
  29166. begin
  29167. StartProgram(false);
  29168. Add([
  29169. '{$modeswitch autoderef}',
  29170. 'type',
  29171. ' TRec = record x: longint; end;',
  29172. ' PRec = ^TRec;',
  29173. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  29174. 'begin',
  29175. ' a.x:=a.x;',
  29176. ' a^.x:=a^.x;',
  29177. ' with a^ do',
  29178. ' x:=x;',
  29179. 'end;',
  29180. 'function GetIt(p: PRec): PRec;',
  29181. 'begin',
  29182. ' p.x:=p.x;',
  29183. ' p^.x:=p^.x;',
  29184. ' with p^ do',
  29185. ' x:=x;',
  29186. 'end;',
  29187. 'var',
  29188. ' r: TRec;',
  29189. ' p: PRec;',
  29190. 'begin',
  29191. ' p:=GetIt(p);',
  29192. ' p^:=GetIt(@r)^;',
  29193. ' DoIt(p,p,p);',
  29194. ' DoIt(@r,p,p);',
  29195. '']);
  29196. ConvertProgram;
  29197. CheckSource('TestPointer_RecordArg',
  29198. LinesToStr([ // statements
  29199. 'rtl.recNewT(this, "TRec", function () {',
  29200. ' this.x = 0;',
  29201. ' this.$eq = function (b) {',
  29202. ' return this.x === b.x;',
  29203. ' };',
  29204. ' this.$assign = function (s) {',
  29205. ' this.x = s.x;',
  29206. ' return this;',
  29207. ' };',
  29208. '});',
  29209. 'this.DoIt = function (a, b, c) {',
  29210. ' var Result = $mod.TRec.$new();',
  29211. ' a.x = a.x;',
  29212. ' a.x = a.x;',
  29213. ' a.x = a.x;',
  29214. ' return Result;',
  29215. '};',
  29216. 'this.GetIt = function (p) {',
  29217. ' var Result = null;',
  29218. ' p.x = p.x;',
  29219. ' p.x = p.x;',
  29220. ' p.x = p.x;',
  29221. ' return Result;',
  29222. '};',
  29223. 'this.r = this.TRec.$new();',
  29224. 'this.p = null;',
  29225. '']),
  29226. LinesToStr([ // $mod.$main
  29227. '$mod.p = $mod.GetIt($mod.p);',
  29228. '$mod.p.$assign($mod.GetIt($mod.r));',
  29229. '$mod.DoIt($mod.p, {',
  29230. ' p: $mod,',
  29231. ' get: function () {',
  29232. ' return this.p.p;',
  29233. ' },',
  29234. ' set: function (v) {',
  29235. ' this.p.p = v;',
  29236. ' }',
  29237. '}, {',
  29238. ' p: $mod,',
  29239. ' get: function () {',
  29240. ' return this.p.p;',
  29241. ' },',
  29242. ' set: function (v) {',
  29243. ' this.p.p = v;',
  29244. ' }',
  29245. '});',
  29246. '$mod.DoIt($mod.r, {',
  29247. ' p: $mod,',
  29248. ' get: function () {',
  29249. ' return this.p.p;',
  29250. ' },',
  29251. ' set: function (v) {',
  29252. ' this.p.p = v;',
  29253. ' }',
  29254. '}, {',
  29255. ' p: $mod,',
  29256. ' get: function () {',
  29257. ' return this.p.p;',
  29258. ' },',
  29259. ' set: function (v) {',
  29260. ' this.p.p = v;',
  29261. ' }',
  29262. '});',
  29263. '']));
  29264. end;
  29265. procedure TTestModule.TestJSValue_AssignToJSValue;
  29266. begin
  29267. StartProgram(false);
  29268. Add('var');
  29269. Add(' v: jsvalue;');
  29270. Add(' i: longint;');
  29271. Add(' s: string;');
  29272. Add(' b: boolean;');
  29273. Add(' d: double;');
  29274. Add(' p: pointer;');
  29275. Add('begin');
  29276. Add(' v:=v;');
  29277. Add(' v:=1;');
  29278. Add(' v:=i;');
  29279. Add(' v:='''';');
  29280. Add(' v:=''c'';');
  29281. Add(' v:=''foo'';');
  29282. Add(' v:=s;');
  29283. Add(' v:=false;');
  29284. Add(' v:=true;');
  29285. Add(' v:=b;');
  29286. Add(' v:=0.1;');
  29287. Add(' v:=d;');
  29288. Add(' v:=nil;');
  29289. Add(' v:=p;');
  29290. ConvertProgram;
  29291. CheckSource('TestJSValue_AssignToJSValue',
  29292. LinesToStr([ // statements
  29293. 'this.v = undefined;',
  29294. 'this.i = 0;',
  29295. 'this.s = "";',
  29296. 'this.b = false;',
  29297. 'this.d = 0.0;',
  29298. 'this.p = null;',
  29299. '']),
  29300. LinesToStr([ // $mod.$main
  29301. '$mod.v = $mod.v;',
  29302. '$mod.v = 1;',
  29303. '$mod.v = $mod.i;',
  29304. '$mod.v = "";',
  29305. '$mod.v = "c";',
  29306. '$mod.v = "foo";',
  29307. '$mod.v = $mod.s;',
  29308. '$mod.v = false;',
  29309. '$mod.v = true;',
  29310. '$mod.v = $mod.b;',
  29311. '$mod.v = 0.1;',
  29312. '$mod.v = $mod.d;',
  29313. '$mod.v = null;',
  29314. '$mod.v = $mod.p;',
  29315. '']));
  29316. end;
  29317. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  29318. begin
  29319. StartProgram(false);
  29320. Add('type');
  29321. Add(' integer = longint;');
  29322. Add(' TYesNo = boolean;');
  29323. Add(' TFloat = double;');
  29324. Add(' TCaption = string;');
  29325. Add(' TChar = char;');
  29326. Add('var');
  29327. Add(' v: jsvalue;');
  29328. Add(' i: integer;');
  29329. Add(' s: TCaption;');
  29330. Add(' b: TYesNo;');
  29331. Add(' d: TFloat;');
  29332. Add(' c: char;');
  29333. Add('begin');
  29334. Add(' i:=longint(v);');
  29335. Add(' i:=integer(v);');
  29336. Add(' s:=string(v);');
  29337. Add(' s:=TCaption(v);');
  29338. Add(' b:=boolean(v);');
  29339. Add(' b:=TYesNo(v);');
  29340. Add(' d:=double(v);');
  29341. Add(' d:=TFloat(v);');
  29342. Add(' c:=char(v);');
  29343. Add(' c:=TChar(v);');
  29344. ConvertProgram;
  29345. CheckSource('TestJSValue_TypeCastToBaseType',
  29346. LinesToStr([ // statements
  29347. 'this.v = undefined;',
  29348. 'this.i = 0;',
  29349. 'this.s = "";',
  29350. 'this.b = false;',
  29351. 'this.d = 0.0;',
  29352. 'this.c = "\x00";',
  29353. '']),
  29354. LinesToStr([ // $mod.$main
  29355. '$mod.i = rtl.trunc($mod.v);',
  29356. '$mod.i = rtl.trunc($mod.v);',
  29357. '$mod.s = "" + $mod.v;',
  29358. '$mod.s = "" + $mod.v;',
  29359. '$mod.b = !($mod.v == false);',
  29360. '$mod.b = !($mod.v == false);',
  29361. '$mod.d = rtl.getNumber($mod.v);',
  29362. '$mod.d = rtl.getNumber($mod.v);',
  29363. '$mod.c = rtl.getChar($mod.v);',
  29364. '$mod.c = rtl.getChar($mod.v);',
  29365. '']));
  29366. end;
  29367. procedure TTestModule.TestJSValue_TypecastToJSValue;
  29368. begin
  29369. StartProgram(false);
  29370. Add([
  29371. 'type',
  29372. ' TArr = array of word;',
  29373. ' TRec = record end;',
  29374. ' TSet = set of boolean;',
  29375. 'procedure Fly(v: jsvalue);',
  29376. 'begin',
  29377. 'end;',
  29378. 'var',
  29379. ' a: TArr;',
  29380. ' r: TRec;',
  29381. ' s: TSet;',
  29382. 'begin',
  29383. ' Fly(jsvalue(a));',
  29384. ' Fly(jsvalue(r));',
  29385. ' Fly(jsvalue(s));',
  29386. '']);
  29387. ConvertProgram;
  29388. CheckSource('TestJSValue_TypecastToJSValue',
  29389. LinesToStr([ // statements
  29390. 'rtl.recNewT(this, "TRec", function () {',
  29391. ' this.$eq = function (b) {',
  29392. ' return true;',
  29393. ' };',
  29394. ' this.$assign = function (s) {',
  29395. ' return this;',
  29396. ' };',
  29397. '});',
  29398. 'this.Fly = function (v) {',
  29399. '};',
  29400. 'this.a = [];',
  29401. 'this.r = this.TRec.$new();',
  29402. 'this.s = {};',
  29403. '']),
  29404. LinesToStr([ // $mod.$main
  29405. '$mod.Fly($mod.a);',
  29406. '$mod.Fly($mod.r);',
  29407. '$mod.Fly($mod.s);',
  29408. '']));
  29409. end;
  29410. procedure TTestModule.TestJSValue_Equal;
  29411. begin
  29412. StartProgram(false);
  29413. Add('type');
  29414. Add(' integer = longint;');
  29415. Add(' TYesNo = boolean;');
  29416. Add(' TFloat = double;');
  29417. Add(' TCaption = string;');
  29418. Add(' TChar = char;');
  29419. Add(' TMulti = JSValue;');
  29420. Add('var');
  29421. Add(' v: jsvalue;');
  29422. Add(' i: integer;');
  29423. Add(' s: TCaption;');
  29424. Add(' b: TYesNo;');
  29425. Add(' d: TFloat;');
  29426. Add(' c: char;');
  29427. Add(' m: TMulti;');
  29428. Add('begin');
  29429. Add(' b:=v=v;');
  29430. Add(' b:=v<>v;');
  29431. Add(' b:=v=1;');
  29432. Add(' b:=v<>1;');
  29433. Add(' b:=2=v;');
  29434. Add(' b:=2<>v;');
  29435. Add(' b:=v=i;');
  29436. Add(' b:=i=v;');
  29437. Add(' b:=v=nil;');
  29438. Add(' b:=nil=v;');
  29439. Add(' b:=v=false;');
  29440. Add(' b:=true=v;');
  29441. Add(' b:=v=b;');
  29442. Add(' b:=b=v;');
  29443. Add(' b:=v=s;');
  29444. Add(' b:=s=v;');
  29445. Add(' b:=v=''foo'';');
  29446. Add(' b:=''''=v;');
  29447. Add(' b:=v=d;');
  29448. Add(' b:=d=v;');
  29449. Add(' b:=v=3.4;');
  29450. Add(' b:=5.6=v;');
  29451. Add(' b:=v=c;');
  29452. Add(' b:=c=v;');
  29453. Add(' b:=m=m;');
  29454. Add(' b:=v=m;');
  29455. Add(' b:=m=v;');
  29456. ConvertProgram;
  29457. CheckSource('TestJSValue_Equal',
  29458. LinesToStr([ // statements
  29459. 'this.v = undefined;',
  29460. 'this.i = 0;',
  29461. 'this.s = "";',
  29462. 'this.b = false;',
  29463. 'this.d = 0.0;',
  29464. 'this.c = "\x00";',
  29465. 'this.m = undefined;',
  29466. '']),
  29467. LinesToStr([ // $mod.$main
  29468. '$mod.b = $mod.v == $mod.v;',
  29469. '$mod.b = $mod.v != $mod.v;',
  29470. '$mod.b = $mod.v == 1;',
  29471. '$mod.b = $mod.v != 1;',
  29472. '$mod.b = 2 == $mod.v;',
  29473. '$mod.b = 2 != $mod.v;',
  29474. '$mod.b = $mod.v == $mod.i;',
  29475. '$mod.b = $mod.i == $mod.v;',
  29476. '$mod.b = $mod.v == null;',
  29477. '$mod.b = null == $mod.v;',
  29478. '$mod.b = $mod.v == false;',
  29479. '$mod.b = true == $mod.v;',
  29480. '$mod.b = $mod.v == $mod.b;',
  29481. '$mod.b = $mod.b == $mod.v;',
  29482. '$mod.b = $mod.v == $mod.s;',
  29483. '$mod.b = $mod.s == $mod.v;',
  29484. '$mod.b = $mod.v == "foo";',
  29485. '$mod.b = "" == $mod.v;',
  29486. '$mod.b = $mod.v == $mod.d;',
  29487. '$mod.b = $mod.d == $mod.v;',
  29488. '$mod.b = $mod.v == 3.4;',
  29489. '$mod.b = 5.6 == $mod.v;',
  29490. '$mod.b = $mod.v == $mod.c;',
  29491. '$mod.b = $mod.c == $mod.v;',
  29492. '$mod.b = $mod.m == $mod.m;',
  29493. '$mod.b = $mod.v == $mod.m;',
  29494. '$mod.b = $mod.m == $mod.v;',
  29495. '']));
  29496. end;
  29497. procedure TTestModule.TestJSValue_If;
  29498. begin
  29499. StartProgram(false);
  29500. Add([
  29501. 'procedure Fly(var u);',
  29502. 'begin',
  29503. ' if jsvalue(u) then ;',
  29504. 'end;',
  29505. 'var',
  29506. ' v: jsvalue;',
  29507. 'begin',
  29508. ' if v then ;',
  29509. ' while v do ;',
  29510. ' repeat until v;',
  29511. '']);
  29512. ConvertProgram;
  29513. CheckSource('TestJSValue_If',
  29514. LinesToStr([ // statements
  29515. 'this.Fly = function (u) {',
  29516. ' if (u.get()) ;',
  29517. '};',
  29518. 'this.v = undefined;',
  29519. '']),
  29520. LinesToStr([ // $mod.$main
  29521. 'if ($mod.v) ;',
  29522. 'while($mod.v){',
  29523. '};',
  29524. 'do{',
  29525. '} while(!$mod.v);',
  29526. '']));
  29527. end;
  29528. procedure TTestModule.TestJSValue_Not;
  29529. begin
  29530. StartProgram(false);
  29531. Add([
  29532. 'var',
  29533. ' v: jsvalue;',
  29534. ' b: boolean;',
  29535. 'begin',
  29536. ' b:=not v;',
  29537. ' if not v then ;',
  29538. ' while not v do ;',
  29539. ' repeat until not v;',
  29540. '']);
  29541. ConvertProgram;
  29542. CheckSource('TestJSValue_If',
  29543. LinesToStr([ // statements
  29544. 'this.v = undefined;',
  29545. 'this.b = false;',
  29546. '']),
  29547. LinesToStr([ // $mod.$main
  29548. '$mod.b=!$mod.v;',
  29549. 'if (!$mod.v) ;',
  29550. 'while(!$mod.v){',
  29551. '};',
  29552. 'do{',
  29553. '} while($mod.v);',
  29554. '']));
  29555. end;
  29556. procedure TTestModule.TestJSValue_Enum;
  29557. begin
  29558. StartProgram(false);
  29559. Add('type');
  29560. Add(' TColor = (red, blue);');
  29561. Add(' TRedBlue = TColor;');
  29562. Add('var');
  29563. Add(' v: jsvalue;');
  29564. Add(' e: TColor;');
  29565. Add('begin');
  29566. Add(' v:=e;');
  29567. Add(' v:=TColor(e);');
  29568. Add(' v:=TRedBlue(e);');
  29569. Add(' e:=TColor(v);');
  29570. Add(' e:=TRedBlue(v);');
  29571. ConvertProgram;
  29572. CheckSource('TestJSValue_Enum',
  29573. LinesToStr([ // statements
  29574. 'this.TColor = {',
  29575. ' "0": "red",',
  29576. ' red: 0,',
  29577. ' "1": "blue",',
  29578. ' blue: 1',
  29579. '};',
  29580. 'this.v = undefined;',
  29581. 'this.e = 0;',
  29582. '']),
  29583. LinesToStr([ // $mod.$main
  29584. '$mod.v = $mod.e;',
  29585. '$mod.v = $mod.e;',
  29586. '$mod.v = $mod.e;',
  29587. '$mod.e = $mod.v;',
  29588. '$mod.e = $mod.v;',
  29589. '']));
  29590. end;
  29591. procedure TTestModule.TestJSValue_ClassInstance;
  29592. begin
  29593. StartProgram(false);
  29594. Add([
  29595. 'type',
  29596. ' TObject = class',
  29597. ' end;',
  29598. ' TBirdObject = TObject;',
  29599. 'var',
  29600. ' v: jsvalue;',
  29601. ' o: TObject;',
  29602. 'begin',
  29603. ' v:=o;',
  29604. ' v:=TObject(o);',
  29605. ' v:=TBirdObject(o);',
  29606. ' o:=TObject(v);',
  29607. ' o:=TBirdObject(v);',
  29608. ' if v is TObject then ;',
  29609. '']);
  29610. ConvertProgram;
  29611. CheckSource('TestJSValue_ClassInstance',
  29612. LinesToStr([ // statements
  29613. 'rtl.createClass(this, "TObject", null, function () {',
  29614. ' this.$init = function () {',
  29615. ' };',
  29616. ' this.$final = function () {',
  29617. ' };',
  29618. '});',
  29619. 'this.v = undefined;',
  29620. 'this.o = null;',
  29621. '']),
  29622. LinesToStr([ // $mod.$main
  29623. '$mod.v = $mod.o;',
  29624. '$mod.v = $mod.o;',
  29625. '$mod.v = $mod.o;',
  29626. '$mod.o = rtl.getObject($mod.v);',
  29627. '$mod.o = rtl.getObject($mod.v);',
  29628. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  29629. '']));
  29630. end;
  29631. procedure TTestModule.TestJSValue_ClassOf;
  29632. begin
  29633. StartProgram(false);
  29634. Add([
  29635. 'type',
  29636. ' TClass = class of TObject;',
  29637. ' TObject = class',
  29638. ' end;',
  29639. ' TBirds = class of TBird;',
  29640. ' TBird = class(TObject) end;',
  29641. 'var',
  29642. ' v: jsvalue;',
  29643. ' c: TClass;',
  29644. 'begin',
  29645. ' v:=c;',
  29646. ' v:=TObject;',
  29647. ' v:=TClass(c);',
  29648. ' v:=TBirds(c);',
  29649. ' c:=TClass(v);',
  29650. ' c:=TBirds(v);',
  29651. ' if v is TClass then ;',
  29652. '']);
  29653. ConvertProgram;
  29654. CheckSource('TestJSValue_ClassOf',
  29655. LinesToStr([ // statements
  29656. 'rtl.createClass(this, "TObject", null, function () {',
  29657. ' this.$init = function () {',
  29658. ' };',
  29659. ' this.$final = function () {',
  29660. ' };',
  29661. '});',
  29662. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29663. '});',
  29664. 'this.v = undefined;',
  29665. 'this.c = null;',
  29666. '']),
  29667. LinesToStr([ // $mod.$main
  29668. '$mod.v = $mod.c;',
  29669. '$mod.v = $mod.TObject;',
  29670. '$mod.v = $mod.c;',
  29671. '$mod.v = $mod.c;',
  29672. '$mod.c = rtl.getObject($mod.v);',
  29673. '$mod.c = rtl.getObject($mod.v);',
  29674. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  29675. '']));
  29676. end;
  29677. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  29678. begin
  29679. StartProgram(false);
  29680. Add([
  29681. 'type',
  29682. ' integer = longint;',
  29683. ' TArray = array of JSValue;',
  29684. ' TArrgh = tarray;',
  29685. ' TArrInt = array of integer;',
  29686. 'var',
  29687. ' v: jsvalue;',
  29688. ' TheArray: tarray = (1,''2'');',
  29689. ' Arr: tarrgh;',
  29690. ' i: integer;',
  29691. ' ArrInt: tarrint;',
  29692. 'begin',
  29693. ' arr:=thearray;',
  29694. ' thearray:=arr;',
  29695. ' setlength(arr,2);',
  29696. ' setlength(thearray,3);',
  29697. ' arr[4]:=v;',
  29698. ' arr[5]:=length(thearray);',
  29699. ' arr[6]:=nil;',
  29700. ' arr[7]:=thearray[8];',
  29701. ' arr[low(arr)]:=high(thearray);',
  29702. ' arr:=arrint;',
  29703. ' arrInt:=tarrint(arr);',
  29704. ' if TheArray = nil then ;',
  29705. ' if nil = TheArray then ;',
  29706. ' if TheArray <> nil then ;',
  29707. ' if nil <> TheArray then ;',
  29708. '']);
  29709. ConvertProgram;
  29710. CheckSource('TestJSValue_ArrayOfJSValue',
  29711. LinesToStr([ // statements
  29712. 'this.v = undefined;',
  29713. 'this.TheArray = [1, "2"];',
  29714. 'this.Arr = [];',
  29715. 'this.i = 0;',
  29716. 'this.ArrInt = [];',
  29717. '']),
  29718. LinesToStr([ // $mod.$main
  29719. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  29720. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  29721. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  29722. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  29723. '$mod.Arr[4] = $mod.v;',
  29724. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  29725. '$mod.Arr[6] = null;',
  29726. '$mod.Arr[7] = $mod.TheArray[8];',
  29727. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  29728. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  29729. '$mod.ArrInt = $mod.Arr;',
  29730. 'if (rtl.length($mod.TheArray) === 0) ;',
  29731. 'if (rtl.length($mod.TheArray) === 0) ;',
  29732. 'if (rtl.length($mod.TheArray) > 0) ;',
  29733. 'if (rtl.length($mod.TheArray) > 0) ;',
  29734. '']));
  29735. end;
  29736. procedure TTestModule.TestJSValue_ArrayLit;
  29737. begin
  29738. StartProgram(false);
  29739. Add([
  29740. 'type',
  29741. ' TFlag = (big,small);',
  29742. ' TArray = array of JSValue;',
  29743. ' TObject = class end;',
  29744. ' TClass = class of TObject;',
  29745. 'var',
  29746. ' v: jsvalue;',
  29747. ' a: TArray;',
  29748. ' o: TObject;',
  29749. 'begin',
  29750. ' a:=[];',
  29751. ' a:=[1];',
  29752. ' a:=[1,2];',
  29753. ' a:=[big];',
  29754. ' a:=[1,big];',
  29755. ' a:=[o,nil];',
  29756. '']);
  29757. ConvertProgram;
  29758. CheckSource('TestJSValue_ArrayLit',
  29759. LinesToStr([ // statements
  29760. 'this.TFlag = {',
  29761. ' "0": "big",',
  29762. ' big: 0,',
  29763. ' "1": "small",',
  29764. ' small: 1',
  29765. '};',
  29766. 'rtl.createClass(this, "TObject", null, function () {',
  29767. ' this.$init = function () {',
  29768. ' };',
  29769. ' this.$final = function () {',
  29770. ' };',
  29771. '});',
  29772. 'this.v = undefined;',
  29773. 'this.a = [];',
  29774. 'this.o = null;',
  29775. '']),
  29776. LinesToStr([ // $mod.$main
  29777. '$mod.a = [];',
  29778. '$mod.a = [1];',
  29779. '$mod.a = [1, 2];',
  29780. '$mod.a = [$mod.TFlag.big];',
  29781. '$mod.a = [1, $mod.TFlag.big];',
  29782. '$mod.a = [$mod.o, null];',
  29783. '']));
  29784. end;
  29785. procedure TTestModule.TestJSValue_Params;
  29786. begin
  29787. StartProgram(false);
  29788. Add('type');
  29789. Add(' integer = longint;');
  29790. Add(' TYesNo = boolean;');
  29791. Add(' TFloat = double;');
  29792. Add(' TCaption = string;');
  29793. Add(' TChar = char;');
  29794. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  29795. Add('var');
  29796. Add(' l: jsvalue;');
  29797. Add('begin');
  29798. Add(' a:=a;');
  29799. Add(' l:=b;');
  29800. Add(' c:=c;');
  29801. Add(' d:=d;');
  29802. Add(' Result:=l;');
  29803. Add('end;');
  29804. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  29805. Add('var');
  29806. Add(' v: jsvalue;');
  29807. Add(' i: integer;');
  29808. Add(' b: TYesNo;');
  29809. Add(' d: TFloat;');
  29810. Add(' s: TCaption;');
  29811. Add(' c: TChar;');
  29812. Add('begin');
  29813. Add(' v:=doit(v,v,v,v);');
  29814. Add(' i:=integer(dosome(i,i));');
  29815. Add(' b:=TYesNo(dosome(b,b));');
  29816. Add(' d:=TFloat(dosome(d,d));');
  29817. Add(' s:=TCaption(dosome(s,s));');
  29818. Add(' c:=TChar(dosome(c,c));');
  29819. ConvertProgram;
  29820. CheckSource('TestJSValue_Params',
  29821. LinesToStr([ // statements
  29822. 'this.DoIt = function (a, b, c, d) {',
  29823. ' var Result = undefined;',
  29824. ' var l = undefined;',
  29825. ' a = a;',
  29826. ' l = b;',
  29827. ' c.set(c.get());',
  29828. ' d.set(d.get());',
  29829. ' Result = l;',
  29830. ' return Result;',
  29831. '};',
  29832. 'this.DoSome = function (a, b) {',
  29833. ' var Result = undefined;',
  29834. ' return Result;',
  29835. '};',
  29836. 'this.v = undefined;',
  29837. 'this.i = 0;',
  29838. 'this.b = false;',
  29839. 'this.d = 0.0;',
  29840. 'this.s = "";',
  29841. 'this.c = "\x00";',
  29842. '']),
  29843. LinesToStr([ // $mod.$main
  29844. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  29845. ' p: $mod,',
  29846. ' get: function () {',
  29847. ' return this.p.v;',
  29848. ' },',
  29849. ' set: function (v) {',
  29850. ' this.p.v = v;',
  29851. ' }',
  29852. '}, {',
  29853. ' p: $mod,',
  29854. ' get: function () {',
  29855. ' return this.p.v;',
  29856. ' },',
  29857. ' set: function (v) {',
  29858. ' this.p.v = v;',
  29859. ' }',
  29860. '});',
  29861. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  29862. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  29863. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  29864. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  29865. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  29866. '']));
  29867. end;
  29868. procedure TTestModule.TestJSValue_UntypedParam;
  29869. begin
  29870. StartProgram(false);
  29871. Add('function DoIt(const a; var b; out c): jsvalue;');
  29872. Add('begin');
  29873. Add(' Result:=a;');
  29874. Add(' Result:=b;');
  29875. Add(' Result:=c;');
  29876. Add(' b:=Result;');
  29877. Add(' c:=Result;');
  29878. Add('end;');
  29879. Add('var i: longint;');
  29880. Add('begin');
  29881. Add(' doit(i,i,i);');
  29882. ConvertProgram;
  29883. CheckSource('TestJSValue_UntypedParam',
  29884. LinesToStr([ // statements
  29885. 'this.DoIt = function (a, b, c) {',
  29886. ' var Result = undefined;',
  29887. ' Result = a;',
  29888. ' Result = b.get();',
  29889. ' Result = c.get();',
  29890. ' b.set(Result);',
  29891. ' c.set(Result);',
  29892. ' return Result;',
  29893. '};',
  29894. 'this.i = 0;',
  29895. '']),
  29896. LinesToStr([ // $mod.$main
  29897. '$mod.DoIt($mod.i, {',
  29898. ' p: $mod,',
  29899. ' get: function () {',
  29900. ' return this.p.i;',
  29901. ' },',
  29902. ' set: function (v) {',
  29903. ' this.p.i = v;',
  29904. ' }',
  29905. '}, {',
  29906. ' p: $mod,',
  29907. ' get: function () {',
  29908. ' return this.p.i;',
  29909. ' },',
  29910. ' set: function (v) {',
  29911. ' this.p.i = v;',
  29912. ' }',
  29913. '});',
  29914. '']));
  29915. end;
  29916. procedure TTestModule.TestJSValue_FuncResultType;
  29917. begin
  29918. StartProgram(false);
  29919. Add('type');
  29920. Add(' integer = longint;');
  29921. Add(' TJSValueArray = array of JSValue;');
  29922. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  29923. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  29924. Add('begin');
  29925. Add(' while Compare(P,aList[0])>0 do ;');
  29926. Add('end;');
  29927. Add('var');
  29928. Add(' Compare: TListSortCompare;');
  29929. Add(' V: JSValue;');
  29930. Add(' i: integer;');
  29931. Add('begin');
  29932. Add(' if Compare(V,V)>0 then ;');
  29933. Add(' if Compare(i,i)>1 then ;');
  29934. Add(' if Compare(nil,false)>2 then ;');
  29935. Add(' if Compare(1,true)>3 then ;');
  29936. ConvertProgram;
  29937. CheckSource('TestJSValue_UntypedParam',
  29938. LinesToStr([ // statements
  29939. 'this.Sort = function (P, aList, Compare) {',
  29940. ' while (Compare(P, aList[0]) > 0) {',
  29941. ' };',
  29942. '};',
  29943. 'this.Compare = null;',
  29944. 'this.V = undefined;',
  29945. 'this.i = 0;',
  29946. '']),
  29947. LinesToStr([ // $mod.$main
  29948. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  29949. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  29950. 'if ($mod.Compare(null, false) > 2) ;',
  29951. 'if ($mod.Compare(1, true) > 3) ;',
  29952. '']));
  29953. end;
  29954. procedure TTestModule.TestJSValue_ProcType_Assign;
  29955. begin
  29956. StartProgram(false);
  29957. Add('type');
  29958. Add(' integer = longint;');
  29959. Add(' TObject = class');
  29960. Add(' class function GetGlob: integer;');
  29961. Add(' function Getter: integer;');
  29962. Add(' end;');
  29963. Add('class function TObject.GetGlob: integer;');
  29964. Add('var v1: jsvalue;');
  29965. Add('begin');
  29966. Add(' v1:=@GetGlob;');
  29967. Add(' v1:[email protected];');
  29968. Add('end;');
  29969. Add('function TObject.Getter: integer;');
  29970. Add('var v2: jsvalue;');
  29971. Add('begin');
  29972. Add(' v2:=@Getter;');
  29973. Add(' v2:[email protected];');
  29974. Add(' v2:=@GetGlob;');
  29975. Add(' v2:[email protected];');
  29976. Add('end;');
  29977. Add('function GetIt(i: integer): integer;');
  29978. Add('var v3: jsvalue;');
  29979. Add('begin');
  29980. Add(' v3:=@GetIt;');
  29981. Add('end;');
  29982. Add('var');
  29983. Add(' V: JSValue;');
  29984. Add(' o: TObject;');
  29985. Add('begin');
  29986. Add(' v:=@GetIt;');
  29987. Add(' v:[email protected];');
  29988. Add(' v:[email protected];');
  29989. ConvertProgram;
  29990. CheckSource('TestJSValue_ProcType_Assign',
  29991. LinesToStr([ // statements
  29992. 'rtl.createClass(this, "TObject", null, function () {',
  29993. ' this.$init = function () {',
  29994. ' };',
  29995. ' this.$final = function () {',
  29996. ' };',
  29997. ' this.GetGlob = function () {',
  29998. ' var Result = 0;',
  29999. ' var v1 = undefined;',
  30000. ' v1 = rtl.createCallback(this, "GetGlob");',
  30001. ' v1 = rtl.createCallback(this, "GetGlob");',
  30002. ' return Result;',
  30003. ' };',
  30004. ' this.Getter = function () {',
  30005. ' var Result = 0;',
  30006. ' var v2 = undefined;',
  30007. ' v2 = rtl.createCallback(this, "Getter");',
  30008. ' v2 = rtl.createCallback(this, "Getter");',
  30009. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  30010. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  30011. ' return Result;',
  30012. ' };',
  30013. '});',
  30014. 'this.GetIt = function (i) {',
  30015. ' var Result = 0;',
  30016. ' var v3 = undefined;',
  30017. ' v3 = $mod.GetIt;',
  30018. ' return Result;',
  30019. '};',
  30020. 'this.V = undefined;',
  30021. 'this.o = null;',
  30022. '']),
  30023. LinesToStr([ // $mod.$main
  30024. '$mod.V = $mod.GetIt;',
  30025. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  30026. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  30027. '']));
  30028. end;
  30029. procedure TTestModule.TestJSValue_ProcType_Equal;
  30030. begin
  30031. StartProgram(false);
  30032. Add('type');
  30033. Add(' integer = longint;');
  30034. Add(' TObject = class');
  30035. Add(' class function GetGlob: integer;');
  30036. Add(' function Getter: integer;');
  30037. Add(' end;');
  30038. Add('class function TObject.GetGlob: integer;');
  30039. Add('var v1: jsvalue;');
  30040. Add('begin');
  30041. Add(' if v1=@GetGlob then;');
  30042. Add(' if [email protected] then ;');
  30043. Add('end;');
  30044. Add('function TObject.Getter: integer;');
  30045. Add('var v2: jsvalue;');
  30046. Add('begin');
  30047. Add(' if v2=@Getter then;');
  30048. Add(' if [email protected] then ;');
  30049. Add(' if v2=@GetGlob then;');
  30050. Add(' if [email protected] then;');
  30051. Add('end;');
  30052. Add('function GetIt(i: integer): integer;');
  30053. Add('var v3: jsvalue;');
  30054. Add('begin');
  30055. Add(' if v3=@GetIt then;');
  30056. Add('end;');
  30057. Add('var');
  30058. Add(' V: JSValue;');
  30059. Add(' o: TObject;');
  30060. Add('begin');
  30061. Add(' if v=@GetIt then;');
  30062. Add(' if [email protected] then;');
  30063. Add(' if [email protected] then;');
  30064. Add(' if @GetIt=v then;');
  30065. Add(' if @o.Getter=v then;');
  30066. Add(' if @o.GetGlob=v then;');
  30067. ConvertProgram;
  30068. CheckSource('TestJSValue_ProcType_Equal',
  30069. LinesToStr([ // statements
  30070. 'rtl.createClass(this, "TObject", null, function () {',
  30071. ' this.$init = function () {',
  30072. ' };',
  30073. ' this.$final = function () {',
  30074. ' };',
  30075. ' this.GetGlob = function () {',
  30076. ' var Result = 0;',
  30077. ' var v1 = undefined;',
  30078. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  30079. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  30080. ' return Result;',
  30081. ' };',
  30082. ' this.Getter = function () {',
  30083. ' var Result = 0;',
  30084. ' var v2 = undefined;',
  30085. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  30086. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  30087. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  30088. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  30089. ' return Result;',
  30090. ' };',
  30091. '});',
  30092. 'this.GetIt = function (i) {',
  30093. ' var Result = 0;',
  30094. ' var v3 = undefined;',
  30095. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  30096. ' return Result;',
  30097. '};',
  30098. 'this.V = undefined;',
  30099. 'this.o = null;',
  30100. '']),
  30101. LinesToStr([ // $mod.$main
  30102. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  30103. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  30104. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  30105. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  30106. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  30107. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  30108. '']));
  30109. end;
  30110. procedure TTestModule.TestJSValue_ProcType_Param;
  30111. begin
  30112. StartProgram(false);
  30113. Add([
  30114. 'type',
  30115. ' variant = jsvalue;',
  30116. ' TArrVariant = array of variant;',
  30117. ' TArrVar2 = TArrVariant;',
  30118. ' TFuncInt = function: longint;',
  30119. 'function GetIt: longint;',
  30120. 'begin',
  30121. 'end;',
  30122. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  30123. 'var v: variant;',
  30124. 'begin',
  30125. ' v:=arr[1];',
  30126. 'end;',
  30127. 'var s: string;',
  30128. 'begin',
  30129. ' DoIt(GetIt,[]);',
  30130. ' DoIt(@GetIt,[]);',
  30131. ' DoIt(1,[s,GetIt]);',
  30132. ' DoIt(1,[s,@GetIt]);',
  30133. '']);
  30134. ConvertProgram;
  30135. CheckSource('TestJSValue_ProcType_Param',
  30136. LinesToStr([ // statements
  30137. 'this.GetIt = function () {',
  30138. ' var Result = 0;',
  30139. ' return Result;',
  30140. '};',
  30141. 'this.DoIt = function (p, Arr) {',
  30142. ' var v = undefined;',
  30143. ' v = Arr[1];',
  30144. '};',
  30145. 'this.s = "";',
  30146. '']),
  30147. LinesToStr([ // $mod.$main
  30148. '$mod.DoIt($mod.GetIt(), []);',
  30149. '$mod.DoIt($mod.GetIt, []);',
  30150. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  30151. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  30152. '']));
  30153. end;
  30154. procedure TTestModule.TestJSValue_AssignToPointerFail;
  30155. begin
  30156. StartProgram(false);
  30157. Add([
  30158. 'var',
  30159. ' v: JSValue;',
  30160. ' p: Pointer;',
  30161. 'begin',
  30162. ' p:=v;',
  30163. '']);
  30164. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  30165. nIncompatibleTypesGotExpected);
  30166. ConvertProgram;
  30167. end;
  30168. procedure TTestModule.TestJSValue_OverloadDouble;
  30169. begin
  30170. StartProgram(false);
  30171. Add([
  30172. 'type',
  30173. ' integer = longint;',
  30174. ' tdatetime = double;',
  30175. 'procedure DoIt(d: double); begin end;',
  30176. 'procedure DoIt(v: jsvalue); begin end;',
  30177. 'var',
  30178. ' d: double;',
  30179. ' dt: tdatetime;',
  30180. ' i: integer;',
  30181. ' b: byte;',
  30182. ' shi: shortint;',
  30183. ' w: word;',
  30184. ' smi: smallint;',
  30185. ' lw: longword;',
  30186. ' li: longint;',
  30187. ' ni: nativeint;',
  30188. ' nu: nativeuint;',
  30189. 'begin',
  30190. ' DoIt(d);',
  30191. ' DoIt(dt);',
  30192. ' DoIt(i);',
  30193. ' DoIt(b);',
  30194. ' DoIt(shi);',
  30195. ' DoIt(w);',
  30196. ' DoIt(smi);',
  30197. ' DoIt(lw);',
  30198. ' DoIt(li);',
  30199. ' DoIt(ni);',
  30200. ' DoIt(nu);',
  30201. '']);
  30202. ConvertProgram;
  30203. CheckSource('TestJSValue_OverloadDouble',
  30204. LinesToStr([ // statements
  30205. 'this.DoIt = function (d) {',
  30206. '};',
  30207. 'this.DoIt$1 = function (v) {',
  30208. '};',
  30209. 'this.d = 0.0;',
  30210. 'this.dt = 0.0;',
  30211. 'this.i = 0;',
  30212. 'this.b = 0;',
  30213. 'this.shi = 0;',
  30214. 'this.w = 0;',
  30215. 'this.smi = 0;',
  30216. 'this.lw = 0;',
  30217. 'this.li = 0;',
  30218. 'this.ni = 0;',
  30219. 'this.nu = 0;',
  30220. '']),
  30221. LinesToStr([ // $mod.$main
  30222. '$mod.DoIt($mod.d);',
  30223. '$mod.DoIt($mod.dt);',
  30224. '$mod.DoIt$1($mod.i);',
  30225. '$mod.DoIt$1($mod.b);',
  30226. '$mod.DoIt$1($mod.shi);',
  30227. '$mod.DoIt$1($mod.w);',
  30228. '$mod.DoIt$1($mod.smi);',
  30229. '$mod.DoIt$1($mod.lw);',
  30230. '$mod.DoIt$1($mod.li);',
  30231. '$mod.DoIt$1($mod.ni);',
  30232. '$mod.DoIt$1($mod.nu);',
  30233. '']));
  30234. end;
  30235. procedure TTestModule.TestJSValue_OverloadNativeInt;
  30236. begin
  30237. StartProgram(false);
  30238. Add([
  30239. 'type',
  30240. ' integer = longint;',
  30241. ' int53 = nativeint;',
  30242. ' tdatetime = double;',
  30243. 'procedure DoIt(n: nativeint); begin end;',
  30244. 'procedure DoIt(v: jsvalue); begin end;',
  30245. 'var',
  30246. ' d: double;',
  30247. ' dt: tdatetime;',
  30248. ' i: integer;',
  30249. ' b: byte;',
  30250. ' shi: shortint;',
  30251. ' w: word;',
  30252. ' smi: smallint;',
  30253. ' lw: longword;',
  30254. ' li: longint;',
  30255. ' ni: nativeint;',
  30256. ' nu: nativeuint;',
  30257. 'begin',
  30258. ' DoIt(d);',
  30259. ' DoIt(dt);',
  30260. ' DoIt(i);',
  30261. ' DoIt(b);',
  30262. ' DoIt(shi);',
  30263. ' DoIt(w);',
  30264. ' DoIt(smi);',
  30265. ' DoIt(lw);',
  30266. ' DoIt(li);',
  30267. ' DoIt(ni);',
  30268. ' DoIt(nu);',
  30269. '']);
  30270. ConvertProgram;
  30271. CheckSource('TestJSValue_OverloadNativeInt',
  30272. LinesToStr([ // statements
  30273. 'this.DoIt = function (n) {',
  30274. '};',
  30275. 'this.DoIt$1 = function (v) {',
  30276. '};',
  30277. 'this.d = 0.0;',
  30278. 'this.dt = 0.0;',
  30279. 'this.i = 0;',
  30280. 'this.b = 0;',
  30281. 'this.shi = 0;',
  30282. 'this.w = 0;',
  30283. 'this.smi = 0;',
  30284. 'this.lw = 0;',
  30285. 'this.li = 0;',
  30286. 'this.ni = 0;',
  30287. 'this.nu = 0;',
  30288. '']),
  30289. LinesToStr([ // $mod.$main
  30290. '$mod.DoIt$1($mod.d);',
  30291. '$mod.DoIt$1($mod.dt);',
  30292. '$mod.DoIt($mod.i);',
  30293. '$mod.DoIt($mod.b);',
  30294. '$mod.DoIt($mod.shi);',
  30295. '$mod.DoIt($mod.w);',
  30296. '$mod.DoIt($mod.smi);',
  30297. '$mod.DoIt($mod.lw);',
  30298. '$mod.DoIt($mod.li);',
  30299. '$mod.DoIt($mod.ni);',
  30300. '$mod.DoIt($mod.nu);',
  30301. '']));
  30302. end;
  30303. procedure TTestModule.TestJSValue_OverloadWord;
  30304. begin
  30305. StartProgram(false);
  30306. Add([
  30307. 'type',
  30308. ' integer = longint;',
  30309. ' int53 = nativeint;',
  30310. ' tdatetime = double;',
  30311. 'procedure DoIt(w: word); begin end;',
  30312. 'procedure DoIt(v: jsvalue); begin end;',
  30313. 'var',
  30314. ' d: double;',
  30315. ' dt: tdatetime;',
  30316. ' i: integer;',
  30317. ' b: byte;',
  30318. ' shi: shortint;',
  30319. ' w: word;',
  30320. ' smi: smallint;',
  30321. ' lw: longword;',
  30322. ' li: longint;',
  30323. ' ni: nativeint;',
  30324. ' nu: nativeuint;',
  30325. 'begin',
  30326. ' DoIt(d);',
  30327. ' DoIt(dt);',
  30328. ' DoIt(i);',
  30329. ' DoIt(b);',
  30330. ' DoIt(shi);',
  30331. ' DoIt(w);',
  30332. ' DoIt(smi);',
  30333. ' DoIt(lw);',
  30334. ' DoIt(li);',
  30335. ' DoIt(ni);',
  30336. ' DoIt(nu);',
  30337. '']);
  30338. ConvertProgram;
  30339. CheckSource('TestJSValue_OverloadWord',
  30340. LinesToStr([ // statements
  30341. 'this.DoIt = function (w) {',
  30342. '};',
  30343. 'this.DoIt$1 = function (v) {',
  30344. '};',
  30345. 'this.d = 0.0;',
  30346. 'this.dt = 0.0;',
  30347. 'this.i = 0;',
  30348. 'this.b = 0;',
  30349. 'this.shi = 0;',
  30350. 'this.w = 0;',
  30351. 'this.smi = 0;',
  30352. 'this.lw = 0;',
  30353. 'this.li = 0;',
  30354. 'this.ni = 0;',
  30355. 'this.nu = 0;',
  30356. '']),
  30357. LinesToStr([ // $mod.$main
  30358. '$mod.DoIt$1($mod.d);',
  30359. '$mod.DoIt$1($mod.dt);',
  30360. '$mod.DoIt$1($mod.i);',
  30361. '$mod.DoIt($mod.b);',
  30362. '$mod.DoIt($mod.shi);',
  30363. '$mod.DoIt($mod.w);',
  30364. '$mod.DoIt$1($mod.smi);',
  30365. '$mod.DoIt$1($mod.lw);',
  30366. '$mod.DoIt$1($mod.li);',
  30367. '$mod.DoIt$1($mod.ni);',
  30368. '$mod.DoIt$1($mod.nu);',
  30369. '']));
  30370. end;
  30371. procedure TTestModule.TestJSValue_OverloadString;
  30372. begin
  30373. StartProgram(false);
  30374. Add([
  30375. 'type',
  30376. ' uni = string;',
  30377. ' WChar = char;',
  30378. 'procedure DoIt(s: string); begin end;',
  30379. 'procedure DoIt(v: jsvalue); begin end;',
  30380. 'var',
  30381. ' s: string;',
  30382. ' c: char;',
  30383. ' u: uni;',
  30384. 'begin',
  30385. ' DoIt(s);',
  30386. ' DoIt(c);',
  30387. ' DoIt(u);',
  30388. '']);
  30389. ConvertProgram;
  30390. CheckSource('TestJSValue_OverloadString',
  30391. LinesToStr([ // statements
  30392. 'this.DoIt = function (s) {',
  30393. '};',
  30394. 'this.DoIt$1 = function (v) {',
  30395. '};',
  30396. 'this.s = "";',
  30397. 'this.c = "\x00";',
  30398. 'this.u = "";',
  30399. '']),
  30400. LinesToStr([ // $mod.$main
  30401. '$mod.DoIt($mod.s);',
  30402. '$mod.DoIt($mod.c);',
  30403. '$mod.DoIt($mod.u);',
  30404. '']));
  30405. end;
  30406. procedure TTestModule.TestJSValue_OverloadChar;
  30407. begin
  30408. StartProgram(false);
  30409. Add([
  30410. 'type',
  30411. ' uni = string;',
  30412. ' WChar = char;',
  30413. 'procedure DoIt(c: char); begin end;',
  30414. 'procedure DoIt(v: jsvalue); begin end;',
  30415. 'var',
  30416. ' s: string;',
  30417. ' c: char;',
  30418. ' u: uni;',
  30419. 'begin',
  30420. ' DoIt(s);',
  30421. ' DoIt(c);',
  30422. ' DoIt(u);',
  30423. '']);
  30424. ConvertProgram;
  30425. CheckSource('TestJSValue_OverloadChar',
  30426. LinesToStr([ // statements
  30427. 'this.DoIt = function (c) {',
  30428. '};',
  30429. 'this.DoIt$1 = function (v) {',
  30430. '};',
  30431. 'this.s = "";',
  30432. 'this.c = "\x00";',
  30433. 'this.u = "";',
  30434. '']),
  30435. LinesToStr([ // $mod.$main
  30436. '$mod.DoIt$1($mod.s);',
  30437. '$mod.DoIt($mod.c);',
  30438. '$mod.DoIt$1($mod.u);',
  30439. '']));
  30440. end;
  30441. procedure TTestModule.TestJSValue_OverloadPointer;
  30442. begin
  30443. StartProgram(false);
  30444. Add([
  30445. 'type',
  30446. ' TObject = class end;',
  30447. 'procedure DoIt(p: pointer); begin end;',
  30448. 'procedure DoIt(v: jsvalue); begin end;',
  30449. 'var',
  30450. ' o: TObject;',
  30451. 'begin',
  30452. ' DoIt(o);',
  30453. '']);
  30454. ConvertProgram;
  30455. CheckSource('TestJSValue_OverloadPointer',
  30456. LinesToStr([ // statements
  30457. 'rtl.createClass(this, "TObject", null, function () {',
  30458. ' this.$init = function () {',
  30459. ' };',
  30460. ' this.$final = function () {',
  30461. ' };',
  30462. '});',
  30463. 'this.DoIt = function (p) {',
  30464. '};',
  30465. 'this.DoIt$1 = function (v) {',
  30466. '};',
  30467. 'this.o = null;',
  30468. '']),
  30469. LinesToStr([ // $mod.$main
  30470. '$mod.DoIt($mod.o);',
  30471. '']));
  30472. end;
  30473. procedure TTestModule.TestJSValue_ForIn;
  30474. begin
  30475. StartProgram(false);
  30476. Add([
  30477. 'var',
  30478. ' v: JSValue;',
  30479. ' key: string;',
  30480. 'begin',
  30481. ' for key in v do begin',
  30482. ' if key=''abc'' then ;',
  30483. ' end;',
  30484. '']);
  30485. ConvertProgram;
  30486. CheckSource('TestJSValue_ForIn',
  30487. LinesToStr([ // statements
  30488. 'this.v = undefined;',
  30489. 'this.key = "";',
  30490. '']),
  30491. LinesToStr([ // $mod.$main
  30492. 'for ($mod.key in $mod.v) {',
  30493. ' if ($mod.key === "abc") ;',
  30494. '};',
  30495. '']));
  30496. end;
  30497. procedure TTestModule.TestRTTI_IntRange;
  30498. begin
  30499. WithTypeInfo:=true;
  30500. StartProgram(true,[supTypeInfo]);
  30501. Add([
  30502. '{$modeswitch externalclass}',
  30503. 'type',
  30504. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  30505. ' TColor = type TGraphicsColor;',
  30506. 'var',
  30507. ' p: TTypeInfo;',
  30508. ' k: TTypeKind;',
  30509. 'begin',
  30510. ' p:=typeinfo(TGraphicsColor);',
  30511. ' p:=typeinfo(TColor);',
  30512. ' k:=GetTypeKind(TGraphicsColor);',
  30513. ' k:=GetTypeKind(TColor);',
  30514. '']);
  30515. ConvertProgram;
  30516. CheckSource('TestRTTI_IntRange',
  30517. LinesToStr([ // statements
  30518. 'this.$rtti.$Int("TGraphicsColor", {',
  30519. ' minvalue: -2147483648,',
  30520. ' maxvalue: 2147483647,',
  30521. ' ordtype: 4',
  30522. '});',
  30523. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  30524. 'this.p = null;',
  30525. 'this.k = 0;',
  30526. '']),
  30527. LinesToStr([ // $mod.$main
  30528. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  30529. '$mod.p = $mod.$rtti["TColor"];',
  30530. '$mod.k = 1;',
  30531. '$mod.k = 1;',
  30532. '']));
  30533. end;
  30534. procedure TTestModule.TestRTTI_Double;
  30535. begin
  30536. WithTypeInfo:=true;
  30537. StartProgram(true,[supTypeInfo]);
  30538. Add([
  30539. '{$modeswitch externalclass}',
  30540. 'type',
  30541. ' TFloat = type double;',
  30542. 'var',
  30543. ' p: TTypeInfo;',
  30544. 'begin',
  30545. ' p:=typeinfo(double);',
  30546. ' p:=typeinfo(TFloat);',
  30547. '']);
  30548. ConvertProgram;
  30549. CheckSource('TestRTTI_Double',
  30550. LinesToStr([ // statements
  30551. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30552. 'this.p = null;',
  30553. '']),
  30554. LinesToStr([ // $mod.$main
  30555. '$mod.p = rtl.double;',
  30556. '$mod.p = $mod.$rtti["TFloat"];',
  30557. '']));
  30558. end;
  30559. procedure TTestModule.TestRTTI_ProcType;
  30560. begin
  30561. WithTypeInfo:=true;
  30562. StartProgram(false);
  30563. Add('type');
  30564. Add(' TProcA = procedure;');
  30565. Add(' TMethodB = procedure of object;');
  30566. Add(' TProcC = procedure; varargs;');
  30567. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  30568. Add(' TProcE = function: nativeint;');
  30569. Add(' TProcF = function(const p: TProcA): nativeuint;');
  30570. Add('var p: pointer;');
  30571. Add('begin');
  30572. Add(' p:=typeinfo(tproca);');
  30573. ConvertProgram;
  30574. CheckSource('TestRTTI_ProcType',
  30575. LinesToStr([ // statements
  30576. 'this.$rtti.$ProcVar("TProcA", {',
  30577. ' procsig: rtl.newTIProcSig([])',
  30578. '});',
  30579. 'this.$rtti.$MethodVar("TMethodB", {',
  30580. ' procsig: rtl.newTIProcSig([]),',
  30581. ' methodkind: 0',
  30582. '});',
  30583. 'this.$rtti.$ProcVar("TProcC", {',
  30584. ' procsig: rtl.newTIProcSig([], null, 2)',
  30585. '});',
  30586. 'this.$rtti.$ProcVar("TProcD", {',
  30587. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30588. '});',
  30589. 'this.$rtti.$ProcVar("TProcE", {',
  30590. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30591. '});',
  30592. 'this.$rtti.$ProcVar("TProcF", {',
  30593. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  30594. '});',
  30595. 'this.p = null;',
  30596. '']),
  30597. LinesToStr([ // $mod.$main
  30598. '$mod.p = $mod.$rtti["TProcA"];',
  30599. '']));
  30600. end;
  30601. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  30602. begin
  30603. WithTypeInfo:=true;
  30604. AddModuleWithIntfImplSrc('unit2.pas',
  30605. LinesToStr([
  30606. 'type',
  30607. ' TObject = class end;'
  30608. ]),
  30609. '');
  30610. StartUnit(true);
  30611. Add('interface');
  30612. Add('uses unit2;');
  30613. Add('type');
  30614. Add(' TProcA = function(o: tobject): tobject;');
  30615. Add('implementation');
  30616. Add('type');
  30617. Add(' TProcB = function(o: tobject): tobject;');
  30618. Add('var p: Pointer;');
  30619. Add('initialization');
  30620. Add(' p:=typeinfo(tproca);');
  30621. Add(' p:=typeinfo(tprocb);');
  30622. ConvertUnit;
  30623. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  30624. LinesToStr([ // statements
  30625. 'var $impl = $mod.$impl;',
  30626. 'this.$rtti.$ProcVar("TProcA", {',
  30627. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30628. '});',
  30629. '']),
  30630. LinesToStr([ // this.$init
  30631. '$impl.p = $mod.$rtti["TProcA"];',
  30632. '$impl.p = $mod.$rtti["TProcB"];',
  30633. '']),
  30634. LinesToStr([ // implementation
  30635. '$mod.$rtti.$ProcVar("TProcB", {',
  30636. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  30637. '});',
  30638. '$impl.p = null;',
  30639. '']) );
  30640. end;
  30641. procedure TTestModule.TestRTTI_ProcTypeAnonymous;
  30642. begin
  30643. WithTypeInfo:=true;
  30644. StartProgram(false);
  30645. Add(['var',
  30646. ' ProcA: procedure;',
  30647. ' MethodB: procedure of object;',
  30648. ' ProcC: procedure; varargs;',
  30649. ' ProcD: procedure(i: longint; const j: string; var c: char; out d: double);',
  30650. ' ProcE: function: nativeint;',
  30651. ' p: pointer;',
  30652. 'begin',
  30653. ' p:=typeinfo(proca);']);
  30654. ConvertProgram;
  30655. CheckSource('TestRTTI_ProcTypeAnonymous',
  30656. LinesToStr([ // statements
  30657. 'this.$rtti.$ProcVar("ProcA$a", {',
  30658. ' procsig: rtl.newTIProcSig([])',
  30659. '});',
  30660. 'this.ProcA = null;',
  30661. 'this.$rtti.$MethodVar("MethodB$a", {',
  30662. ' procsig: rtl.newTIProcSig([]),',
  30663. ' methodkind: 0',
  30664. '});',
  30665. 'this.MethodB = null;',
  30666. 'this.$rtti.$ProcVar("ProcC$a", {',
  30667. ' procsig: rtl.newTIProcSig([], null, 2)',
  30668. '});',
  30669. 'this.ProcC = null;',
  30670. 'this.$rtti.$ProcVar("ProcD$a", {',
  30671. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  30672. '});',
  30673. 'this.ProcD = null;',
  30674. 'this.$rtti.$ProcVar("ProcE$a", {',
  30675. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  30676. '});',
  30677. 'this.ProcE = null;',
  30678. 'this.p = null;',
  30679. '']),
  30680. LinesToStr([ // $mod.$main
  30681. '$mod.p = $mod.$rtti["ProcA$a"];',
  30682. '']));
  30683. end;
  30684. procedure TTestModule.TestRTTI_EnumAndSetType;
  30685. begin
  30686. WithTypeInfo:=true;
  30687. StartProgram(false);
  30688. Add('type');
  30689. Add(' TFlag = (light,dark);');
  30690. Add(' TFlags = set of TFlag;');
  30691. Add(' TProc = function(f: TFlags): TFlag;');
  30692. Add('var p: pointer;');
  30693. Add('begin');
  30694. Add(' p:=typeinfo(tflag);');
  30695. Add(' p:=typeinfo(tflags);');
  30696. ConvertProgram;
  30697. CheckSource('TestRTTI_EnumAndType',
  30698. LinesToStr([ // statements
  30699. 'this.TFlag = {',
  30700. ' "0": "light",',
  30701. ' light: 0,',
  30702. ' "1": "dark",',
  30703. ' dark: 1',
  30704. '};',
  30705. 'this.$rtti.$Enum("TFlag", {',
  30706. ' minvalue: 0,',
  30707. ' maxvalue: 1,',
  30708. ' ordtype: 1,',
  30709. ' enumtype: this.TFlag',
  30710. '});',
  30711. 'this.$rtti.$Set("TFlags", {',
  30712. ' comptype: this.$rtti["TFlag"]',
  30713. '});',
  30714. 'this.$rtti.$ProcVar("TProc", {',
  30715. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  30716. '});',
  30717. 'this.p = null;',
  30718. '']),
  30719. LinesToStr([ // $mod.$main
  30720. '$mod.p = $mod.$rtti["TFlag"];',
  30721. '$mod.p = $mod.$rtti["TFlags"];',
  30722. '']));
  30723. end;
  30724. procedure TTestModule.TestRTTI_EnumRange;
  30725. begin
  30726. WithTypeInfo:=true;
  30727. StartProgram(false);
  30728. Add([
  30729. 'type',
  30730. ' TCol = (red,green,blue);',
  30731. ' TColRg = green..blue;',
  30732. ' TSetOfColRg = set of TColRg;',
  30733. 'var p: pointer;',
  30734. 'begin',
  30735. ' p:=typeinfo(tcolrg);',
  30736. ' p:=typeinfo(tsetofcolrg);',
  30737. '']);
  30738. ConvertProgram;
  30739. end;
  30740. procedure TTestModule.TestRTTI_AnonymousEnumType;
  30741. begin
  30742. WithTypeInfo:=true;
  30743. StartProgram(false);
  30744. Add('type');
  30745. Add(' TFlags = set of (red, green);');
  30746. Add('var');
  30747. Add(' f: TFlags;');
  30748. Add('begin');
  30749. Add(' Include(f,red);');
  30750. ConvertProgram;
  30751. CheckSource('TestRTTI_AnonymousEnumType',
  30752. LinesToStr([ // statements
  30753. 'this.TFlags$a = {',
  30754. ' "0": "red",',
  30755. ' red: 0,',
  30756. ' "1": "green",',
  30757. ' green: 1',
  30758. '};',
  30759. 'this.$rtti.$Enum("TFlags$a", {',
  30760. ' minvalue: 0,',
  30761. ' maxvalue: 1,',
  30762. ' ordtype: 1,',
  30763. ' enumtype: this.TFlags$a',
  30764. '});',
  30765. 'this.$rtti.$Set("TFlags", {',
  30766. ' comptype: this.$rtti["TFlags$a"]',
  30767. '});',
  30768. 'this.f = {};',
  30769. '']),
  30770. LinesToStr([
  30771. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  30772. '']));
  30773. end;
  30774. procedure TTestModule.TestRTTI_StaticArray;
  30775. begin
  30776. WithTypeInfo:=true;
  30777. StartProgram(false);
  30778. Add('type');
  30779. Add(' TFlag = (light,dark);');
  30780. Add(' TFlagNames = array[TFlag] of string;');
  30781. Add(' TBoolNames = array[boolean] of string;');
  30782. Add(' TByteArray = array[1..32768] of byte;');
  30783. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  30784. Add('var p: pointer;');
  30785. Add('begin');
  30786. Add(' p:=typeinfo(TFlagNames);');
  30787. Add(' p:=typeinfo(TBoolNames);');
  30788. ConvertProgram;
  30789. CheckSource('TestRTTI_StaticArray',
  30790. LinesToStr([ // statements
  30791. 'this.TFlag = {',
  30792. ' "0": "light",',
  30793. ' light: 0,',
  30794. ' "1": "dark",',
  30795. ' dark: 1',
  30796. '};',
  30797. 'this.$rtti.$Enum("TFlag", {',
  30798. ' minvalue: 0,',
  30799. ' maxvalue: 1,',
  30800. ' ordtype: 1,',
  30801. ' enumtype: this.TFlag',
  30802. '});',
  30803. 'this.$rtti.$StaticArray("TFlagNames", {',
  30804. ' dims: [2],',
  30805. ' eltype: rtl.string',
  30806. '});',
  30807. 'this.$rtti.$StaticArray("TBoolNames", {',
  30808. ' dims: [2],',
  30809. ' eltype: rtl.string',
  30810. '});',
  30811. 'this.$rtti.$StaticArray("TByteArray", {',
  30812. ' dims: [32768],',
  30813. ' eltype: rtl.byte',
  30814. '});',
  30815. 'this.$rtti.$ProcVar("TProc", {',
  30816. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  30817. '});',
  30818. 'this.p = null;',
  30819. '']),
  30820. LinesToStr([ // $mod.$main
  30821. '$mod.p = $mod.$rtti["TFlagNames"];',
  30822. '$mod.p = $mod.$rtti["TBoolNames"];',
  30823. '']));
  30824. end;
  30825. procedure TTestModule.TestRTTI_DynArray;
  30826. begin
  30827. WithTypeInfo:=true;
  30828. StartProgram(false);
  30829. Add('type');
  30830. Add(' TArrStr = array of string;');
  30831. Add(' TArr2Dim = array of tarrstr;');
  30832. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  30833. Add('var p: pointer;');
  30834. Add('begin');
  30835. Add(' p:=typeinfo(tarrstr);');
  30836. Add(' p:=typeinfo(tarr2dim);');
  30837. ConvertProgram;
  30838. CheckSource('TestRTTI_DynArray',
  30839. LinesToStr([ // statements
  30840. 'this.$rtti.$DynArray("TArrStr", {',
  30841. ' eltype: rtl.string',
  30842. '});',
  30843. 'this.$rtti.$DynArray("TArr2Dim", {',
  30844. ' eltype: this.$rtti["TArrStr"]',
  30845. '});',
  30846. 'this.$rtti.$ProcVar("TProc", {',
  30847. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  30848. '});',
  30849. 'this.p = null;',
  30850. '']),
  30851. LinesToStr([ // $mod.$main
  30852. '$mod.p = $mod.$rtti["TArrStr"];',
  30853. '$mod.p = $mod.$rtti["TArr2Dim"];',
  30854. '']));
  30855. end;
  30856. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  30857. begin
  30858. WithTypeInfo:=true;
  30859. StartProgram(false);
  30860. Add('type');
  30861. Add(' TArr = array of array of longint;');
  30862. Add('var a: TArr;');
  30863. Add('begin');
  30864. ConvertProgram;
  30865. CheckSource('TestRTTI_ArrayNestedAnonymous',
  30866. LinesToStr([ // statements
  30867. 'this.$rtti.$DynArray("TArr$a", {',
  30868. ' eltype: rtl.longint',
  30869. '});',
  30870. 'this.$rtti.$DynArray("TArr", {',
  30871. ' eltype: this.$rtti["TArr$a"]',
  30872. '});',
  30873. 'this.a = [];',
  30874. '']),
  30875. LinesToStr([ // $mod.$main
  30876. ]));
  30877. end;
  30878. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  30879. begin
  30880. WithTypeInfo:=true;
  30881. StartProgram(false);
  30882. Add('type');
  30883. Add(' TObject = class');
  30884. Add(' published');
  30885. Add(' procedure Proc; virtual; abstract;');
  30886. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  30887. Add(' end;');
  30888. Add('begin');
  30889. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  30890. nDuplicatePublishedMethodXAtY);
  30891. ConvertProgram;
  30892. end;
  30893. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  30894. begin
  30895. WithTypeInfo:=true;
  30896. StartUnit(false);
  30897. Add([
  30898. 'interface',
  30899. 'type',
  30900. ' TObject = class',
  30901. ' end;',
  30902. ' {$M+}',
  30903. ' TBird = class',
  30904. ' procedure Fly;',
  30905. ' end;',
  30906. ' {$M-}',
  30907. 'type',
  30908. ' TEagle = class(TBird)',
  30909. ' procedure Fly;',
  30910. ' end;',
  30911. 'implementation',
  30912. 'procedure TBird.Fly;',
  30913. 'begin',
  30914. 'end;',
  30915. 'procedure TEagle.Fly;',
  30916. 'begin',
  30917. 'end;',
  30918. '']);
  30919. ConvertUnit;
  30920. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  30921. LinesToStr([ // statements
  30922. 'rtl.createClass(this, "TObject", null, function () {',
  30923. ' this.$init = function () {',
  30924. ' };',
  30925. ' this.$final = function () {',
  30926. ' };',
  30927. '});',
  30928. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30929. ' this.Fly = function () {',
  30930. ' };',
  30931. ' var $r = this.$rtti;',
  30932. ' $r.addMethod("Fly", 0, [], 4);',
  30933. '});',
  30934. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  30935. ' this.Fly = function () {',
  30936. ' };',
  30937. ' var $r = this.$rtti;',
  30938. ' $r.addMethod("Fly", 0, [], 4);',
  30939. '});',
  30940. '']),
  30941. LinesToStr([ // $mod.$main
  30942. ]));
  30943. CheckResolverUnexpectedHints(true);
  30944. end;
  30945. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  30946. begin
  30947. WithTypeInfo:=true;
  30948. StartProgram(false);
  30949. Add('type');
  30950. Add(' TObject = class');
  30951. Add(' published');
  30952. Add(' procedure Proc; external name ''foo'';');
  30953. Add(' end;');
  30954. Add('begin');
  30955. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  30956. nPublishedNameMustMatchExternal);
  30957. ConvertProgram;
  30958. end;
  30959. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  30960. begin
  30961. WithTypeInfo:=true;
  30962. StartProgram(false);
  30963. Add('type');
  30964. Add(' TObject = class');
  30965. Add(' class var FA: longint;');
  30966. Add(' published');
  30967. Add(' class property A: longint read FA;');
  30968. Add(' end;');
  30969. Add('begin');
  30970. SetExpectedPasResolverError('Invalid published property modifier "class"',
  30971. nInvalidXModifierY);
  30972. ConvertProgram;
  30973. end;
  30974. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  30975. begin
  30976. WithTypeInfo:=true;
  30977. StartProgram(false);
  30978. Add('type');
  30979. Add(' TObject = class');
  30980. Add(' published');
  30981. Add(' class var FA: longint;');
  30982. Add(' end;');
  30983. Add('begin');
  30984. SetExpectedPasResolverError(sSymbolCannotBePublished,
  30985. nSymbolCannotBePublished);
  30986. ConvertProgram;
  30987. end;
  30988. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  30989. begin
  30990. WithTypeInfo:=true;
  30991. StartProgram(false);
  30992. Add('{$modeswitch externalclass}');
  30993. Add('type');
  30994. Add(' TObject = class');
  30995. Add(' published');
  30996. Add(' V: longint; external name ''foo'';');
  30997. Add(' end;');
  30998. Add('begin');
  30999. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  31000. nPublishedNameMustMatchExternal);
  31001. ConvertProgram;
  31002. end;
  31003. procedure TTestModule.TestRTTI_Class_Field;
  31004. begin
  31005. WithTypeInfo:=true;
  31006. StartProgram(false);
  31007. Add('{$modeswitch externalclass}');
  31008. Add('type');
  31009. Add(' TObject = class');
  31010. Add(' private');
  31011. Add(' FPropA: string;');
  31012. Add(' published');
  31013. Add(' VarLI: longint;');
  31014. Add(' VarC: char;');
  31015. Add(' VarS: string;');
  31016. Add(' VarD: double;');
  31017. Add(' VarB: boolean;');
  31018. Add(' VarLW: longword;');
  31019. Add(' VarSmI: smallint;');
  31020. Add(' VarW: word;');
  31021. Add(' VarShI: shortint;');
  31022. Add(' VarBy: byte;');
  31023. Add(' VarExt: longint external name ''VarExt'';');
  31024. Add(' ArrA, ArrB: array of byte;');
  31025. Add(' end;');
  31026. Add('var p: pointer;');
  31027. Add(' Obj: tobject;');
  31028. Add('begin');
  31029. Add(' p:=typeinfo(tobject);');
  31030. Add(' p:=typeinfo(p);');
  31031. Add(' p:=typeinfo(obj);');
  31032. ConvertProgram;
  31033. CheckSource('TestRTTI_Class_Field',
  31034. LinesToStr([ // statements
  31035. 'rtl.createClass(this, "TObject", null, function () {',
  31036. ' this.$init = function () {',
  31037. ' this.FPropA = "";',
  31038. ' this.VarLI = 0;',
  31039. ' this.VarC = "\x00";',
  31040. ' this.VarS = "";',
  31041. ' this.VarD = 0.0;',
  31042. ' this.VarB = false;',
  31043. ' this.VarLW = 0;',
  31044. ' this.VarSmI = 0;',
  31045. ' this.VarW = 0;',
  31046. ' this.VarShI = 0;',
  31047. ' this.VarBy = 0;',
  31048. ' this.ArrA = [];',
  31049. ' this.ArrB = [];',
  31050. ' };',
  31051. ' this.$final = function () {',
  31052. ' this.ArrA = undefined;',
  31053. ' this.ArrB = undefined;',
  31054. ' };',
  31055. ' var $r = this.$rtti;',
  31056. ' $r.addField("VarLI", rtl.longint, 4);',
  31057. ' $r.addField("VarC", rtl.char, 4);',
  31058. ' $r.addField("VarS", rtl.string, 4);',
  31059. ' $r.addField("VarD", rtl.double, 4);',
  31060. ' $r.addField("VarB", rtl.boolean, 4);',
  31061. ' $r.addField("VarLW", rtl.longword, 4);',
  31062. ' $r.addField("VarSmI", rtl.smallint, 4);',
  31063. ' $r.addField("VarW", rtl.word, 4);',
  31064. ' $r.addField("VarShI", rtl.shortint, 4);',
  31065. ' $r.addField("VarBy", rtl.byte, 4);',
  31066. ' $r.addField("VarExt", rtl.longint, 4);',
  31067. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  31068. ' eltype: rtl.byte',
  31069. ' });',
  31070. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"], 4);',
  31071. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"], 4);',
  31072. '});',
  31073. 'this.p = null;',
  31074. 'this.Obj = null;',
  31075. '']),
  31076. LinesToStr([ // $mod.$main
  31077. '$mod.p = $mod.$rtti["TObject"];',
  31078. '$mod.p = rtl.pointer;',
  31079. '$mod.p = $mod.Obj.$rtti;',
  31080. '']));
  31081. end;
  31082. procedure TTestModule.TestRTTI_Class_FieldPrivate;
  31083. begin
  31084. WithTypeInfo:=true;
  31085. StartProgram(false);
  31086. Add('type');
  31087. Add('{$RTTI explicit fields([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31088. Add(' TObject = class');
  31089. Add(' strict private');
  31090. Add(' A1: word;');
  31091. Add(' private');
  31092. Add(' A2: word;');
  31093. Add(' strict protected');
  31094. Add(' B1: word;');
  31095. Add(' protected');
  31096. Add(' B2, B3: word;');
  31097. Add(' public');
  31098. Add(' C: word;');
  31099. Add(' published');
  31100. Add(' D: word;');
  31101. Add(' end;');
  31102. Add('begin');
  31103. ConvertProgram;
  31104. CheckSource('TestRTTI_Class_FieldPrivate',
  31105. LinesToStr([ // statements
  31106. 'rtl.createClass(this, "TObject", null, function () {',
  31107. ' this.$init = function () {',
  31108. ' this.A1 = 0;',
  31109. ' this.A2 = 0;',
  31110. ' this.B1 = 0;',
  31111. ' this.B2 = 0;',
  31112. ' this.B3 = 0;',
  31113. ' this.C = 0;',
  31114. ' this.D = 0;',
  31115. ' };',
  31116. ' this.$final = function () {',
  31117. ' };',
  31118. ' var $r = this.$rtti;',
  31119. ' $r.addField("A1", rtl.word, 5);',
  31120. ' $r.addField("A2", rtl.word, 0);',
  31121. ' $r.addField("B1", rtl.word, 6);',
  31122. ' $r.addField("B2", rtl.word, 1);',
  31123. ' $r.addField("B3", rtl.word, 1);',
  31124. ' $r.addField("C", rtl.word);',
  31125. ' $r.addField("D", rtl.word, 3);',
  31126. '});',
  31127. '']),
  31128. LinesToStr([ // $mod.$main
  31129. '']));
  31130. end;
  31131. procedure TTestModule.TestRTTI_Class_Method;
  31132. begin
  31133. WithTypeInfo:=true;
  31134. StartProgram(false);
  31135. Add([
  31136. 'type',
  31137. ' TObject = class',
  31138. ' private',
  31139. ' procedure Internal; external name ''$intern'';',
  31140. ' published',
  31141. ' procedure Click; virtual; abstract;',
  31142. ' procedure Notify(Sender: TObject); virtual; abstract;',
  31143. ' function GetNotify: boolean; external name ''GetNotify'';',
  31144. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  31145. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  31146. ' end;',
  31147. 'begin']);
  31148. ConvertProgram;
  31149. CheckSource('TestRTTI_Class_Method',
  31150. LinesToStr([ // statements
  31151. 'rtl.createClass(this, "TObject", null, function () {',
  31152. ' this.$init = function () {',
  31153. ' };',
  31154. ' this.$final = function () {',
  31155. ' };',
  31156. ' var $r = this.$rtti;',
  31157. ' $r.addMethod("Click", 0, [], 4);',
  31158. ' $r.addMethod("Notify", 0, [["Sender", $r]], 4);',
  31159. ' $r.addMethod("GetNotify", 1, [], 4, rtl.boolean, 4);',
  31160. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], 4, null, 2);',
  31161. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], 4, rtl.word, 20);',
  31162. '});',
  31163. '']),
  31164. LinesToStr([ // $mod.$main
  31165. '']));
  31166. end;
  31167. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  31168. begin
  31169. WithTypeInfo:=true;
  31170. StartProgram(false);
  31171. Add('type');
  31172. Add(' TObject = class');
  31173. Add(' published');
  31174. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  31175. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  31176. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  31177. Add(' end;');
  31178. Add('begin');
  31179. ConvertProgram;
  31180. CheckSource('TestRTTI_Class_MethodOpenArray',
  31181. LinesToStr([ // statements
  31182. 'rtl.createClass(this, "TObject", null, function () {',
  31183. ' this.$init = function () {',
  31184. ' };',
  31185. ' this.$final = function () {',
  31186. ' };',
  31187. ' var $r = this.$rtti;',
  31188. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]], 4);',
  31189. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]], 4);',
  31190. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]], 4);',
  31191. '});',
  31192. '']),
  31193. LinesToStr([ // $mod.$main
  31194. '']));
  31195. end;
  31196. procedure TTestModule.TestRTTI_Class_MethodPrivate;
  31197. begin
  31198. WithTypeInfo:=true;
  31199. StartProgram(false);
  31200. Add('type');
  31201. Add('{$RTTI explicit methods([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31202. Add(' TObject = class');
  31203. Add(' private');
  31204. Add(' procedure PrivateProc(a: word); virtual; abstract;');
  31205. Add(' protected');
  31206. Add(' class function ProtectedFunc: word; virtual; abstract;');
  31207. Add(' public');
  31208. Add(' class procedure PublicProc; virtual; abstract;');
  31209. Add(' constructor Create;');
  31210. Add(' destructor Destroy;');
  31211. Add(' published');
  31212. Add(' function PublishedProc: word; virtual; abstract;');
  31213. Add(' end;');
  31214. Add('constructor TObject.Create;');
  31215. Add('begin');
  31216. Add('end;');
  31217. Add('destructor TObject.Destroy;');
  31218. Add('begin');
  31219. Add('end;');
  31220. Add('begin');
  31221. ConvertProgram;
  31222. CheckSource('TestRTTI_Class_MethodPrivate',
  31223. LinesToStr([ // statements
  31224. 'rtl.createClass(this, "TObject", null, function () {',
  31225. ' this.$init = function () {',
  31226. ' };',
  31227. ' this.$final = function () {',
  31228. ' };',
  31229. ' this.Create = function () {',
  31230. ' return this;',
  31231. ' };',
  31232. ' this.Destroy = function () {',
  31233. ' };',
  31234. ' var $r = this.$rtti;',
  31235. ' $r.addMethod("PrivateProc", 0, [["a", rtl.word]], 0);',
  31236. ' $r.addMethod("ProtectedFunc", 5, [], 1, rtl.word);',
  31237. ' $r.addMethod("PublicProc", 4, []);',
  31238. ' $r.addMethod("Create", 2, []);',
  31239. ' $r.addMethod("Destroy", 3, []);',
  31240. ' $r.addMethod("PublishedProc", 1, [], 4, rtl.word);',
  31241. '});',
  31242. '']),
  31243. LinesToStr([ // $mod.$main
  31244. '']));
  31245. end;
  31246. procedure TTestModule.TestRTTI_Class_Property;
  31247. begin
  31248. WithTypeInfo:=true;
  31249. StartProgram(false);
  31250. Add('{$modeswitch externalclass}');
  31251. Add('type');
  31252. Add(' TObject = class');
  31253. Add(' private');
  31254. Add(' FColor: longint;');
  31255. Add(' FColorStored: boolean;');
  31256. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  31257. Add(' function GetColor: longint; virtual; abstract;');
  31258. Add(' function GetColorStored: boolean; virtual; abstract;');
  31259. Add(' FExtSize: longint external name ''$extSize'';');
  31260. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  31261. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  31262. Add(' function GetExtSize: longint; external name ''$getSize'';');
  31263. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  31264. Add(' published');
  31265. Add(' property ColorA: longint read FColor;');
  31266. Add(' property ColorB: longint write FColor;');
  31267. Add(' property ColorC: longint read GetColor write SetColor;');
  31268. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  31269. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  31270. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  31271. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  31272. Add(' end;');
  31273. Add('begin');
  31274. ConvertProgram;
  31275. CheckSource('TestRTTI_Class_Property',
  31276. LinesToStr([ // statements
  31277. 'rtl.createClass(this, "TObject", null, function () {',
  31278. ' this.$init = function () {',
  31279. ' this.FColor = 0;',
  31280. ' this.FColorStored = false;',
  31281. ' };',
  31282. ' this.$final = function () {',
  31283. ' };',
  31284. ' var $r = this.$rtti;',
  31285. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  31286. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  31287. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  31288. ' $r.addProperty(',
  31289. ' "ColorD",',
  31290. ' 8,',
  31291. ' rtl.longint,',
  31292. ' "FColor",',
  31293. ' "FColor",',
  31294. ' 4,',
  31295. ' {',
  31296. ' stored: "FColorStored"',
  31297. ' }',
  31298. ' );',
  31299. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  31300. ' $r.addProperty(',
  31301. ' "ExtSizeB",',
  31302. ' 11,',
  31303. ' rtl.longint,',
  31304. ' "$getSize",',
  31305. ' "$setSize",',
  31306. ' 4,',
  31307. ' {',
  31308. ' stored: "$extSizeStored"',
  31309. ' }',
  31310. ' );',
  31311. ' $r.addProperty(',
  31312. ' "ExtSizeC",',
  31313. ' 12,',
  31314. ' rtl.longint,',
  31315. ' "$extSize",',
  31316. ' "$extSize",',
  31317. ' 4,',
  31318. ' {',
  31319. ' stored: "$getExtSizeStored"',
  31320. ' }',
  31321. ' );',
  31322. '});',
  31323. '']),
  31324. LinesToStr([ // $mod.$main
  31325. '']));
  31326. end;
  31327. procedure TTestModule.TestRTTI_Class_PropertyParams;
  31328. begin
  31329. WithTypeInfo:=true;
  31330. StartProgram(false);
  31331. Add('{$modeswitch externalclass}');
  31332. Add('type');
  31333. Add(' integer = longint;');
  31334. Add(' TObject = class');
  31335. Add(' private');
  31336. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  31337. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  31338. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  31339. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  31340. Add(' published');
  31341. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  31342. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  31343. Add(' end;');
  31344. Add('begin');
  31345. ConvertProgram;
  31346. CheckSource('TestRTTI_Class_PropertyParams',
  31347. LinesToStr([ // statements
  31348. 'rtl.createClass(this, "TObject", null, function () {',
  31349. ' this.$init = function () {',
  31350. ' };',
  31351. ' this.$final = function () {',
  31352. ' };',
  31353. ' var $r = this.$rtti;',
  31354. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  31355. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  31356. '});',
  31357. '']),
  31358. LinesToStr([ // $mod.$main
  31359. '']));
  31360. end;
  31361. procedure TTestModule.TestRTTI_Class_PropertyPrivate;
  31362. begin
  31363. WithTypeInfo:=true;
  31364. StartProgram(false);
  31365. Add('type');
  31366. Add('{$RTTI explicit properties([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31367. Add(' TObject = class');
  31368. Add(' private');
  31369. Add(' FWord: word;');
  31370. Add(' function GetWord: word; virtual; abstract;');
  31371. Add(' procedure SetWord(Value: word); virtual; abstract;');
  31372. Add(' property PrivateWord: word read FWord write FWord;');
  31373. Add(' protected');
  31374. Add(' property ProtectedWord: word read FWord write SetWord;');
  31375. Add(' public');
  31376. Add(' property PublicWord: word read GetWord;');
  31377. Add(' published');
  31378. Add(' property PublishedWord: word read FWord;');
  31379. Add(' end;');
  31380. Add('begin');
  31381. ConvertProgram;
  31382. CheckSource('TestRTTI_Class_PropertyPrivate',
  31383. LinesToStr([ // statements
  31384. 'rtl.createClass(this, "TObject", null, function () {',
  31385. ' this.$init = function () {',
  31386. ' this.FWord = 0;',
  31387. ' };',
  31388. ' this.$final = function () {',
  31389. ' };',
  31390. ' var $r = this.$rtti;',
  31391. ' $r.addProperty(',
  31392. ' "PrivateWord",',
  31393. ' 0,',
  31394. ' rtl.word,',
  31395. ' "FWord",',
  31396. ' "FWord",',
  31397. ' 0',
  31398. ' );',
  31399. ' $r.addProperty(',
  31400. ' "ProtectedWord",',
  31401. ' 2,',
  31402. ' rtl.word,',
  31403. ' "FWord",',
  31404. ' "SetWord",',
  31405. ' 1',
  31406. ' );',
  31407. ' $r.addProperty("PublicWord", 1, rtl.word, "GetWord", "", 2);',
  31408. ' $r.addProperty(',
  31409. ' "PublishedWord",',
  31410. ' 0,',
  31411. ' rtl.word,',
  31412. ' "FWord",',
  31413. ' ""',
  31414. ' );',
  31415. '});',
  31416. '']),
  31417. LinesToStr([ // $mod.$main
  31418. '']));
  31419. end;
  31420. procedure TTestModule.TestRTTI_Class_ClassProperty;
  31421. begin
  31422. WithTypeInfo:=true;
  31423. StartProgram(false);
  31424. Add('type');
  31425. Add('{$RTTI explicit properties([vcPrivate,vcProtected,vcPublic,vcPublished])}');
  31426. Add(' TObject = class');
  31427. Add(' private');
  31428. Add(' class var FWord: word;');
  31429. Add(' class function GetWord: word; virtual; abstract;');
  31430. Add(' class procedure SetWord(Value: word); virtual; abstract;');
  31431. Add(' class property PrivateWord: word read FWord write FWord;');
  31432. Add(' protected');
  31433. Add(' class property ProtectedWord: word read FWord write SetWord;');
  31434. Add(' public');
  31435. Add(' class property PublicWord: word read GetWord;');
  31436. Add(' end;');
  31437. Add('begin');
  31438. ConvertProgram;
  31439. CheckSource('TestRTTI_Class_ClassProperty',
  31440. LinesToStr([ // statements
  31441. 'rtl.createClass(this, "TObject", null, function () {',
  31442. ' this.FWord = 0;',
  31443. ' this.$init = function () {',
  31444. ' };',
  31445. ' this.$final = function () {',
  31446. ' };',
  31447. ' var $r = this.$rtti;',
  31448. ' $r.addProperty(',
  31449. ' "PrivateWord",',
  31450. ' 32,',
  31451. ' rtl.word,',
  31452. ' "FWord",',
  31453. ' "FWord",',
  31454. ' 0',
  31455. ' );',
  31456. ' $r.addProperty(',
  31457. ' "ProtectedWord",',
  31458. ' 34,',
  31459. ' rtl.word,',
  31460. ' "FWord",',
  31461. ' "SetWord",',
  31462. ' 1',
  31463. ' );',
  31464. ' $r.addProperty(',
  31465. ' "PublicWord",',
  31466. ' 33,',
  31467. ' rtl.word,',
  31468. ' "GetWord",',
  31469. ' "",',
  31470. ' 2',
  31471. ' );',
  31472. '});',
  31473. '']),
  31474. LinesToStr([ // $mod.$main
  31475. '']));
  31476. end;
  31477. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  31478. begin
  31479. WithTypeInfo:=true;
  31480. AddModuleWithIntfImplSrc('unit1.pas',
  31481. 'type TColor = -5..5;',
  31482. '');
  31483. StartProgram(true);
  31484. Add([
  31485. 'uses unit1;',
  31486. 'type',
  31487. ' TColorAlias = TColor;',
  31488. ' TColorTypeAlias = type TColor;',
  31489. ' TObject = class',
  31490. ' private',
  31491. ' fColor: TColor;',
  31492. ' fAlias: TColorAlias;',
  31493. ' fTypeAlias: TColorTypeAlias;',
  31494. ' published',
  31495. ' property Color: TColor read fcolor;',
  31496. ' property Alias: TColorAlias read falias;',
  31497. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  31498. ' end;',
  31499. 'begin',
  31500. '']);
  31501. ConvertProgram;
  31502. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  31503. LinesToStr([ // statements
  31504. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  31505. 'rtl.createClass(this, "TObject", null, function () {',
  31506. ' this.$init = function () {',
  31507. ' this.fColor = 0;',
  31508. ' this.fAlias = 0;',
  31509. ' this.fTypeAlias = 0;',
  31510. ' };',
  31511. ' this.$final = function () {',
  31512. ' };',
  31513. ' var $r = this.$rtti;',
  31514. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  31515. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  31516. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  31517. '});',
  31518. '']),
  31519. LinesToStr([ // $mod.$main
  31520. '']));
  31521. end;
  31522. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  31523. begin
  31524. WithTypeInfo:=true;
  31525. StartProgram(false);
  31526. Add([
  31527. '{$modeswitch omitrtti}',
  31528. 'type',
  31529. ' TObject = class',
  31530. ' private',
  31531. ' FA: byte;',
  31532. ' published',
  31533. ' property A: byte read FA write FA;',
  31534. ' end;',
  31535. 'begin']);
  31536. ConvertProgram;
  31537. CheckSource('TestRTTI_Class_OmitRTTI',
  31538. LinesToStr([ // statements
  31539. 'rtl.createClass(this, "TObject", null, function () {',
  31540. ' this.$init = function () {',
  31541. ' this.FA = 0;',
  31542. ' };',
  31543. ' this.$final = function () {',
  31544. ' };',
  31545. '});',
  31546. '']),
  31547. LinesToStr([ // $mod.$main
  31548. '']));
  31549. end;
  31550. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  31551. begin
  31552. WithTypeInfo:=true;
  31553. StartUnit(true,[supTObject]);
  31554. Add([
  31555. 'interface',
  31556. 'type',
  31557. ' {$M+}',
  31558. ' TBird = class',
  31559. ' published',
  31560. ' Swarm: array of TBird;',
  31561. ' end;',
  31562. 'implementation',
  31563. '']);
  31564. ConvertUnit;
  31565. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  31566. LinesToStr([ // statements
  31567. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  31568. ' this.$init = function () {',
  31569. ' pas.system.TObject.$init.call(this);',
  31570. ' this.Swarm = [];',
  31571. ' };',
  31572. ' this.$final = function () {',
  31573. ' this.Swarm = undefined;',
  31574. ' pas.system.TObject.$final.call(this);',
  31575. ' };',
  31576. ' var $r = this.$rtti;',
  31577. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  31578. ' eltype: $r',
  31579. ' });',
  31580. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"], 4);',
  31581. '});',
  31582. '']),
  31583. LinesToStr([ // $mod.$main
  31584. '']));
  31585. end;
  31586. procedure TTestModule.TestRTTI_IndexModifier;
  31587. begin
  31588. WithTypeInfo:=true;
  31589. StartProgram(false);
  31590. Add([
  31591. 'type',
  31592. ' TEnum = (red, blue);',
  31593. ' TObject = class',
  31594. ' FB: boolean;',
  31595. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  31596. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  31597. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  31598. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  31599. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  31600. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  31601. ' published',
  31602. ' property B1: boolean index 1 read FB write SetIntBool;',
  31603. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  31604. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  31605. ' end;',
  31606. 'begin']);
  31607. ConvertProgram;
  31608. CheckSource('TestRTTI_IndexModifier',
  31609. LinesToStr([ // statements
  31610. 'this.TEnum = {',
  31611. ' "0": "red",',
  31612. ' red: 0,',
  31613. ' "1": "blue",',
  31614. ' blue: 1',
  31615. '};',
  31616. 'this.$rtti.$Enum("TEnum", {',
  31617. ' minvalue: 0,',
  31618. ' maxvalue: 1,',
  31619. ' ordtype: 1,',
  31620. ' enumtype: this.TEnum',
  31621. '});',
  31622. 'rtl.createClass(this, "TObject", null, function () {',
  31623. ' this.$init = function () {',
  31624. ' this.FB = false;',
  31625. ' };',
  31626. ' this.$final = function () {',
  31627. ' };',
  31628. ' var $r = this.$rtti;',
  31629. ' $r.addProperty(',
  31630. ' "B1",',
  31631. ' 18,',
  31632. ' rtl.boolean,',
  31633. ' "FB",',
  31634. ' "SetIntBool",',
  31635. ' 4,',
  31636. ' {',
  31637. ' index: 1',
  31638. ' }',
  31639. ' );',
  31640. ' $r.addProperty(',
  31641. ' "B2",',
  31642. ' 17,',
  31643. ' rtl.boolean,',
  31644. ' "GetEnumBool",',
  31645. ' "FB",',
  31646. ' 4,',
  31647. ' {',
  31648. ' index: $mod.TEnum.blue',
  31649. ' }',
  31650. ' );',
  31651. ' $r.addProperty(',
  31652. ' "I1",',
  31653. ' 19,',
  31654. ' rtl.boolean,',
  31655. ' "GetStrIntBool",',
  31656. ' "SetStrIntBool",',
  31657. ' 4,',
  31658. ' {',
  31659. ' index: 2',
  31660. ' }',
  31661. ' );',
  31662. '});',
  31663. '']),
  31664. LinesToStr([ // $mod.$main
  31665. '']));
  31666. end;
  31667. procedure TTestModule.TestRTTI_StoredModifier;
  31668. begin
  31669. WithTypeInfo:=true;
  31670. StartProgram(false);
  31671. Add([
  31672. 'const',
  31673. ' ConstB = true;',
  31674. 'type',
  31675. ' TObject = class',
  31676. ' private',
  31677. ' FB: boolean;',
  31678. ' function IsBStored: boolean; virtual; abstract;',
  31679. ' published',
  31680. ' property BoolA: boolean read FB stored true;',
  31681. ' property BoolB: boolean read FB stored false;',
  31682. ' property BoolC: boolean read FB stored FB;',
  31683. ' property BoolD: boolean read FB stored ConstB;',
  31684. ' property BoolE: boolean read FB stored IsBStored;',
  31685. ' end;',
  31686. 'begin']);
  31687. ConvertProgram;
  31688. CheckSource('TestRTTI_StoredModifier',
  31689. LinesToStr([ // statements
  31690. 'this.ConstB = true;',
  31691. 'rtl.createClass(this, "TObject", null, function () {',
  31692. ' this.$init = function () {',
  31693. ' this.FB = false;',
  31694. ' };',
  31695. ' this.$final = function () {',
  31696. ' };',
  31697. ' var $r = this.$rtti;',
  31698. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  31699. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  31700. ' $r.addProperty(',
  31701. ' "BoolC",',
  31702. ' 8,',
  31703. ' rtl.boolean,',
  31704. ' "FB",',
  31705. ' "",',
  31706. ' 4,',
  31707. ' {',
  31708. ' stored: "FB"',
  31709. ' }',
  31710. ' );',
  31711. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  31712. ' $r.addProperty(',
  31713. ' "BoolE",',
  31714. ' 12,',
  31715. ' rtl.boolean,',
  31716. ' "FB",',
  31717. ' "",',
  31718. ' 4,',
  31719. ' {',
  31720. ' stored: "IsBStored"',
  31721. ' }',
  31722. ' );',
  31723. '});',
  31724. '']),
  31725. LinesToStr([ // $mod.$main
  31726. '']));
  31727. end;
  31728. procedure TTestModule.TestRTTI_DefaultValue;
  31729. begin
  31730. WithTypeInfo:=true;
  31731. StartProgram(false);
  31732. Add([
  31733. 'type',
  31734. ' TEnum = (red, blue);',
  31735. 'const',
  31736. ' CB = true or false;',
  31737. ' CI = 1+2;',
  31738. 'type',
  31739. ' TObject = class',
  31740. ' FB: boolean;',
  31741. ' FI: longint;',
  31742. ' FE: TEnum;',
  31743. ' published',
  31744. ' property B1: boolean read FB default true;',
  31745. ' property B2: boolean read FB default CB;',
  31746. ' property B3: boolean read FB default test1.cb;',
  31747. ' property I1: longint read FI default 2;',
  31748. ' property I2: longint read FI default CI;',
  31749. ' property E1: TEnum read FE default red;',
  31750. ' property E2: TEnum read FE default TEnum.blue;',
  31751. ' end;',
  31752. 'begin']);
  31753. ConvertProgram;
  31754. CheckSource('TestRTTI_DefaultValue',
  31755. LinesToStr([ // statements
  31756. 'this.TEnum = {',
  31757. ' "0": "red",',
  31758. ' red: 0,',
  31759. ' "1": "blue",',
  31760. ' blue: 1',
  31761. '};',
  31762. 'this.$rtti.$Enum("TEnum", {',
  31763. ' minvalue: 0,',
  31764. ' maxvalue: 1,',
  31765. ' ordtype: 1,',
  31766. ' enumtype: this.TEnum',
  31767. '});',
  31768. 'this.CB = true || false;',
  31769. 'this.CI = 1 + 2;',
  31770. 'rtl.createClass(this, "TObject", null, function () {',
  31771. ' this.$init = function () {',
  31772. ' this.FB = false;',
  31773. ' this.FI = 0;',
  31774. ' this.FE = 0;',
  31775. ' };',
  31776. ' this.$final = function () {',
  31777. ' };',
  31778. ' var $r = this.$rtti;',
  31779. ' $r.addProperty(',
  31780. ' "B1",',
  31781. ' 0,',
  31782. ' rtl.boolean,',
  31783. ' "FB",',
  31784. ' "",',
  31785. ' 4,',
  31786. ' {',
  31787. ' Default: true',
  31788. ' }',
  31789. ' );',
  31790. ' $r.addProperty(',
  31791. ' "B2",',
  31792. ' 0,',
  31793. ' rtl.boolean,',
  31794. ' "FB",',
  31795. ' "",',
  31796. ' 4,',
  31797. ' {',
  31798. ' Default: true',
  31799. ' }',
  31800. ' );',
  31801. ' $r.addProperty(',
  31802. ' "B3",',
  31803. ' 0,',
  31804. ' rtl.boolean,',
  31805. ' "FB",',
  31806. ' "",',
  31807. ' 4,',
  31808. ' {',
  31809. ' Default: true',
  31810. ' }',
  31811. ' );',
  31812. ' $r.addProperty(',
  31813. ' "I1",',
  31814. ' 0,',
  31815. ' rtl.longint,',
  31816. ' "FI",',
  31817. ' "",',
  31818. ' 4,',
  31819. ' {',
  31820. ' Default: 2',
  31821. ' }',
  31822. ' );',
  31823. ' $r.addProperty(',
  31824. ' "I2",',
  31825. ' 0,',
  31826. ' rtl.longint,',
  31827. ' "FI",',
  31828. ' "",',
  31829. ' 4,',
  31830. ' {',
  31831. ' Default: 3',
  31832. ' }',
  31833. ' );',
  31834. ' $r.addProperty(',
  31835. ' "E1",',
  31836. ' 0,',
  31837. ' $mod.$rtti["TEnum"],',
  31838. ' "FE",',
  31839. ' "",',
  31840. ' 4,',
  31841. ' {',
  31842. ' Default: $mod.TEnum.red',
  31843. ' }',
  31844. ' );',
  31845. ' $r.addProperty(',
  31846. ' "E2",',
  31847. ' 0,',
  31848. ' $mod.$rtti["TEnum"],',
  31849. ' "FE",',
  31850. ' "",',
  31851. ' 4,',
  31852. ' {',
  31853. ' Default: $mod.TEnum.blue',
  31854. ' }',
  31855. ' );',
  31856. '});',
  31857. '']),
  31858. LinesToStr([ // $mod.$main
  31859. '']));
  31860. end;
  31861. procedure TTestModule.TestRTTI_DefaultValueSet;
  31862. begin
  31863. WithTypeInfo:=true;
  31864. StartProgram(false);
  31865. Add([
  31866. 'type',
  31867. ' TEnum = (red, blue);',
  31868. ' TSet = set of TEnum;',
  31869. 'const',
  31870. ' CSet = [red,blue];',
  31871. 'type',
  31872. ' TObject = class',
  31873. ' FSet: TSet;',
  31874. ' published',
  31875. ' property Set1: TSet read FSet default [];',
  31876. ' property Set2: TSet read FSet default [red];',
  31877. ' property Set3: TSet read FSet default [red,blue];',
  31878. ' property Set4: TSet read FSet default CSet;',
  31879. ' end;',
  31880. 'begin']);
  31881. ConvertProgram;
  31882. CheckSource('TestRTTI_DefaultValueSet',
  31883. LinesToStr([ // statements
  31884. 'this.TEnum = {',
  31885. ' "0": "red",',
  31886. ' red: 0,',
  31887. ' "1": "blue",',
  31888. ' blue: 1',
  31889. '};',
  31890. 'this.$rtti.$Enum("TEnum", {',
  31891. ' minvalue: 0,',
  31892. ' maxvalue: 1,',
  31893. ' ordtype: 1,',
  31894. ' enumtype: this.TEnum',
  31895. '});',
  31896. 'this.$rtti.$Set("TSet", {',
  31897. ' comptype: this.$rtti["TEnum"]',
  31898. '});',
  31899. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  31900. 'rtl.createClass(this, "TObject", null, function () {',
  31901. ' this.$init = function () {',
  31902. ' this.FSet = {};',
  31903. ' };',
  31904. ' this.$final = function () {',
  31905. ' this.FSet = undefined;',
  31906. ' };',
  31907. ' var $r = this.$rtti;',
  31908. ' $r.addProperty(',
  31909. ' "Set1",',
  31910. ' 0,',
  31911. ' $mod.$rtti["TSet"],',
  31912. ' "FSet",',
  31913. ' "",',
  31914. ' 4,',
  31915. ' {',
  31916. ' Default: {}',
  31917. ' }',
  31918. ' );',
  31919. ' $r.addProperty(',
  31920. ' "Set2",',
  31921. ' 0,',
  31922. ' $mod.$rtti["TSet"],',
  31923. ' "FSet",',
  31924. ' "",',
  31925. ' 4,',
  31926. ' {',
  31927. ' Default: rtl.createSet($mod.TEnum.red)',
  31928. ' }',
  31929. ' );',
  31930. ' $r.addProperty(',
  31931. ' "Set3",',
  31932. ' 0,',
  31933. ' $mod.$rtti["TSet"],',
  31934. ' "FSet",',
  31935. ' "",',
  31936. ' 4,',
  31937. ' {',
  31938. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  31939. ' }',
  31940. ' );',
  31941. ' $r.addProperty(',
  31942. ' "Set4",',
  31943. ' 0,',
  31944. ' $mod.$rtti["TSet"],',
  31945. ' "FSet",',
  31946. ' "",',
  31947. ' 4,',
  31948. ' {',
  31949. ' Default: $mod.CSet',
  31950. ' }',
  31951. ' );',
  31952. '});',
  31953. '']),
  31954. LinesToStr([ // $mod.$main
  31955. '']));
  31956. end;
  31957. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  31958. begin
  31959. WithTypeInfo:=true;
  31960. StartProgram(false);
  31961. Add([
  31962. 'type',
  31963. ' TRg = -1..1;',
  31964. 'const',
  31965. ' l = low(TRg);',
  31966. ' h = high(TRg);',
  31967. 'type',
  31968. ' TObject = class',
  31969. ' FV: TRg;',
  31970. ' published',
  31971. ' property V1: TRg read FV default -1;',
  31972. ' end;',
  31973. 'begin']);
  31974. ConvertProgram;
  31975. CheckSource('TestRTTI_DefaultValueRangeType',
  31976. LinesToStr([ // statements
  31977. 'this.$rtti.$Int("TRg", {',
  31978. ' minvalue: -1,',
  31979. ' maxvalue: 1,',
  31980. ' ordtype: 0',
  31981. '});',
  31982. 'this.l = -1;',
  31983. 'this.h = 1;',
  31984. 'rtl.createClass(this, "TObject", null, function () {',
  31985. ' this.$init = function () {',
  31986. ' this.FV = 0;',
  31987. ' };',
  31988. ' this.$final = function () {',
  31989. ' };',
  31990. ' var $r = this.$rtti;',
  31991. ' $r.addProperty(',
  31992. ' "V1",',
  31993. ' 0,',
  31994. ' $mod.$rtti["TRg"],',
  31995. ' "FV",',
  31996. ' "",',
  31997. ' 4,',
  31998. ' {',
  31999. ' Default: -1',
  32000. ' }',
  32001. ' );',
  32002. '});',
  32003. '']),
  32004. LinesToStr([ // $mod.$main
  32005. '']));
  32006. end;
  32007. procedure TTestModule.TestRTTI_DefaultValueInherit;
  32008. begin
  32009. WithTypeInfo:=true;
  32010. StartProgram(false);
  32011. Add([
  32012. 'type',
  32013. ' TObject = class',
  32014. ' FA, FB: byte;',
  32015. ' property A: byte read FA default 1;',
  32016. ' property B: byte read FB default 2;',
  32017. ' end;',
  32018. ' TBird = class',
  32019. ' published',
  32020. ' property A;',
  32021. ' property B nodefault;',
  32022. ' end;',
  32023. 'begin']);
  32024. ConvertProgram;
  32025. CheckSource('TestRTTI_DefaultValueInherit',
  32026. LinesToStr([ // statements
  32027. 'rtl.createClass(this, "TObject", null, function () {',
  32028. ' this.$init = function () {',
  32029. ' this.FA = 0;',
  32030. ' this.FB = 0;',
  32031. ' };',
  32032. ' this.$final = function () {',
  32033. ' };',
  32034. '});',
  32035. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32036. ' var $r = this.$rtti;',
  32037. ' $r.addProperty(',
  32038. ' "A",',
  32039. ' 0,',
  32040. ' rtl.byte,',
  32041. ' "FA",',
  32042. ' "",',
  32043. ' 4,',
  32044. ' {',
  32045. ' Default: 1',
  32046. ' }',
  32047. ' );',
  32048. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  32049. '});',
  32050. '']),
  32051. LinesToStr([ // $mod.$main
  32052. '']));
  32053. end;
  32054. procedure TTestModule.TestRTTI_OverrideMethod;
  32055. begin
  32056. WithTypeInfo:=true;
  32057. StartProgram(false);
  32058. Add('type');
  32059. Add(' TObject = class');
  32060. Add(' published');
  32061. Add(' procedure DoIt; virtual; abstract;');
  32062. Add(' end;');
  32063. Add(' TSky = class');
  32064. Add(' published');
  32065. Add(' procedure DoIt; override;');
  32066. Add(' end;');
  32067. Add('procedure TSky.DoIt; begin end;');
  32068. Add('begin');
  32069. ConvertProgram;
  32070. CheckSource('TestRTTI_OverrideMethod',
  32071. LinesToStr([ // statements
  32072. 'rtl.createClass(this, "TObject", null, function () {',
  32073. ' this.$init = function () {',
  32074. ' };',
  32075. ' this.$final = function () {',
  32076. ' };',
  32077. ' var $r = this.$rtti;',
  32078. ' $r.addMethod("DoIt", 0, [], 4);',
  32079. '});',
  32080. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32081. ' this.DoIt = function () {',
  32082. ' };',
  32083. '});',
  32084. '']),
  32085. LinesToStr([ // $mod.$main
  32086. '']));
  32087. end;
  32088. procedure TTestModule.TestRTTI_ReintroduceMethod;
  32089. begin
  32090. WithTypeInfo:=true;
  32091. StartProgram(false);
  32092. Add([
  32093. 'type',
  32094. ' TObject = class',
  32095. ' published',
  32096. ' procedure DoIt;',
  32097. ' end;',
  32098. ' TSky = class',
  32099. ' published',
  32100. ' procedure DoIt; reintroduce;',
  32101. ' end;',
  32102. 'procedure TObject.DoIt; begin end;',
  32103. 'procedure TSky.DoIt;',
  32104. 'begin',
  32105. ' inherited DoIt;',
  32106. 'end;',
  32107. 'begin']);
  32108. ConvertProgram;
  32109. CheckSource('TestRTTI_ReintroduceMethod',
  32110. LinesToStr([ // statements
  32111. 'rtl.createClass(this, "TObject", null, function () {',
  32112. ' this.$init = function () {',
  32113. ' };',
  32114. ' this.$final = function () {',
  32115. ' };',
  32116. ' this.DoIt = function () {',
  32117. ' };',
  32118. ' var $r = this.$rtti;',
  32119. ' $r.addMethod("DoIt", 0, [], 4);',
  32120. '});',
  32121. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32122. ' this.DoIt = function () {',
  32123. ' $mod.TObject.DoIt.call(this);',
  32124. ' };',
  32125. ' var $r = this.$rtti;',
  32126. ' $r.addMethod("DoIt", 0, [], 4);',
  32127. '});',
  32128. '']),
  32129. LinesToStr([ // $mod.$main
  32130. '']));
  32131. end;
  32132. procedure TTestModule.TestRTTI_OverloadProperty;
  32133. begin
  32134. WithTypeInfo:=true;
  32135. StartProgram(false);
  32136. Add('type');
  32137. Add(' TObject = class');
  32138. Add(' protected');
  32139. Add(' FFlag: longint;');
  32140. Add(' published');
  32141. Add(' property Flag: longint read fflag;');
  32142. Add(' end;');
  32143. Add(' TSky = class');
  32144. Add(' published');
  32145. Add(' property FLAG: longint write fflag;');
  32146. Add(' end;');
  32147. Add('begin');
  32148. ConvertProgram;
  32149. CheckSource('TestRTTI_OverrideMethod',
  32150. LinesToStr([ // statements
  32151. 'rtl.createClass(this, "TObject", null, function () {',
  32152. ' this.$init = function () {',
  32153. ' this.FFlag = 0;',
  32154. ' };',
  32155. ' this.$final = function () {',
  32156. ' };',
  32157. ' var $r = this.$rtti;',
  32158. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  32159. '});',
  32160. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  32161. ' var $r = this.$rtti;',
  32162. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  32163. '});',
  32164. '']),
  32165. LinesToStr([ // $mod.$main
  32166. '']));
  32167. end;
  32168. procedure TTestModule.TestRTTI_ClassForward;
  32169. begin
  32170. WithTypeInfo:=true;
  32171. StartProgram(false);
  32172. Add('type');
  32173. Add(' TObject = class end;');
  32174. Add(' tbridge = class;');
  32175. Add(' TProc = function: tbridge;');
  32176. Add(' TOger = class');
  32177. Add(' published');
  32178. Add(' FBridge: tbridge;');
  32179. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  32180. Add(' property Bridge: tbridge read fbridge write setbridge;');
  32181. Add(' end;');
  32182. Add(' TBridge = class');
  32183. Add(' FOger: toger;');
  32184. Add(' end;');
  32185. Add('var p: Pointer;');
  32186. Add(' b: tbridge;');
  32187. Add('begin');
  32188. Add(' p:=typeinfo(tbridge);');
  32189. Add(' p:=typeinfo(b);');
  32190. ConvertProgram;
  32191. CheckSource('TestRTTI_ClassForward',
  32192. LinesToStr([ // statements
  32193. 'rtl.createClass(this, "TObject", null, function () {',
  32194. ' this.$init = function () {',
  32195. ' };',
  32196. ' this.$final = function () {',
  32197. ' };',
  32198. '});',
  32199. 'this.$rtti.$Class("TBridge");',
  32200. 'this.$rtti.$ProcVar("TProc", {',
  32201. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  32202. '});',
  32203. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  32204. ' this.$init = function () {',
  32205. ' $mod.TObject.$init.call(this);',
  32206. ' this.FBridge = null;',
  32207. ' };',
  32208. ' this.$final = function () {',
  32209. ' this.FBridge = undefined;',
  32210. ' $mod.TObject.$final.call(this);',
  32211. ' };',
  32212. ' var $r = this.$rtti;',
  32213. ' $r.addField("FBridge", $mod.$rtti["TBridge"], 4);',
  32214. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]], 4);',
  32215. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  32216. '});',
  32217. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  32218. ' this.$init = function () {',
  32219. ' $mod.TObject.$init.call(this);',
  32220. ' this.FOger = null;',
  32221. ' };',
  32222. ' this.$final = function () {',
  32223. ' this.FOger = undefined;',
  32224. ' $mod.TObject.$final.call(this);',
  32225. ' };',
  32226. '});',
  32227. 'this.p = null;',
  32228. 'this.b = null;',
  32229. '']),
  32230. LinesToStr([ // $mod.$main
  32231. '$mod.p = $mod.$rtti["TBridge"];',
  32232. '$mod.p = $mod.b.$rtti;',
  32233. '']));
  32234. end;
  32235. procedure TTestModule.TestRTTI_ClassOf;
  32236. begin
  32237. WithTypeInfo:=true;
  32238. StartProgram(false);
  32239. Add('type');
  32240. Add(' TClass = class of tobject;');
  32241. Add(' TProcA = function: TClass;');
  32242. Add(' TObject = class');
  32243. Add(' published');
  32244. Add(' C: tclass;');
  32245. Add(' end;');
  32246. Add(' tfox = class;');
  32247. Add(' TBird = class end;');
  32248. Add(' TBirds = class of tbird;');
  32249. Add(' TFox = class end;');
  32250. Add(' TFoxes = class of tfox;');
  32251. Add(' TCows = class of TCow;');
  32252. Add(' TCow = class;');
  32253. Add(' TCow = class end;');
  32254. Add('begin');
  32255. ConvertProgram;
  32256. CheckSource('TestRTTI_ClassOf',
  32257. LinesToStr([ // statements
  32258. 'this.$rtti.$Class("TObject");',
  32259. 'this.$rtti.$ClassRef("TClass", {',
  32260. ' instancetype: this.$rtti["TObject"]',
  32261. '});',
  32262. 'this.$rtti.$ProcVar("TProcA", {',
  32263. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  32264. '});',
  32265. 'rtl.createClass(this, "TObject", null, function () {',
  32266. ' this.$init = function () {',
  32267. ' this.C = null;',
  32268. ' };',
  32269. ' this.$final = function () {',
  32270. ' this.C = undefined;',
  32271. ' };',
  32272. ' var $r = this.$rtti;',
  32273. ' $r.addField("C", $mod.$rtti["TClass"], 4);',
  32274. '});',
  32275. 'this.$rtti.$Class("TFox");',
  32276. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32277. '});',
  32278. 'this.$rtti.$ClassRef("TBirds", {',
  32279. ' instancetype: this.$rtti["TBird"]',
  32280. '});',
  32281. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  32282. '});',
  32283. 'this.$rtti.$ClassRef("TFoxes", {',
  32284. ' instancetype: this.$rtti["TFox"]',
  32285. '});',
  32286. 'this.$rtti.$Class("TCow");',
  32287. 'this.$rtti.$ClassRef("TCows", {',
  32288. ' instancetype: this.$rtti["TCow"]',
  32289. '});',
  32290. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  32291. '});',
  32292. '']),
  32293. LinesToStr([ // $mod.$main
  32294. '']));
  32295. end;
  32296. procedure TTestModule.TestRTTI_Record;
  32297. begin
  32298. WithTypeInfo:=true;
  32299. StartProgram(false);
  32300. Add('type');
  32301. Add(' integer = longint;');
  32302. Add(' TPoint = record');
  32303. Add(' x,y: integer;');
  32304. Add(' end;');
  32305. Add('var p: pointer;');
  32306. Add(' r: tpoint;');
  32307. Add('begin');
  32308. Add(' p:=typeinfo(tpoint);');
  32309. Add(' p:=typeinfo(r);');
  32310. Add(' p:=typeinfo(r.x);');
  32311. ConvertProgram;
  32312. CheckSource('TestRTTI_Record',
  32313. LinesToStr([ // statements
  32314. 'rtl.recNewT(this, "TPoint", function () {',
  32315. ' this.x = 0;',
  32316. ' this.y = 0;',
  32317. ' this.$eq = function (b) {',
  32318. ' return (this.x === b.x) && (this.y === b.y);',
  32319. ' };',
  32320. ' this.$assign = function (s) {',
  32321. ' this.x = s.x;',
  32322. ' this.y = s.y;',
  32323. ' return this;',
  32324. ' };',
  32325. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  32326. ' $r.addField("x", rtl.longint);',
  32327. ' $r.addField("y", rtl.longint);',
  32328. '});',
  32329. 'this.p = null;',
  32330. 'this.r = this.TPoint.$new();',
  32331. '']),
  32332. LinesToStr([ // $mod.$main
  32333. '$mod.p = $mod.$rtti["TPoint"];',
  32334. '$mod.p = $mod.$rtti["TPoint"];',
  32335. '$mod.p = rtl.longint;',
  32336. '']));
  32337. end;
  32338. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  32339. begin
  32340. WithTypeInfo:=true;
  32341. StartProgram(false);
  32342. Add('type');
  32343. Add(' TFloatRec = record');
  32344. Add(' c,d: array of char;');
  32345. // Add(' i: array of array of longint;');
  32346. Add(' end;');
  32347. Add('var p: pointer;');
  32348. Add(' r: tfloatrec;');
  32349. Add('begin');
  32350. Add(' p:=typeinfo(tfloatrec);');
  32351. Add(' p:=typeinfo(r);');
  32352. Add(' p:=typeinfo(r.d);');
  32353. ConvertProgram;
  32354. CheckSource('TestRTTI_Record',
  32355. LinesToStr([ // statements
  32356. 'rtl.recNewT(this, "TFloatRec", function () {',
  32357. ' this.$new = function () {',
  32358. ' var r = Object.create(this);',
  32359. ' r.c = [];',
  32360. ' r.d = [];',
  32361. ' return r;',
  32362. ' };',
  32363. ' this.$eq = function (b) {',
  32364. ' return (this.c === b.c) && (this.d === b.d);',
  32365. ' };',
  32366. ' this.$assign = function (s) {',
  32367. ' this.c = rtl.arrayRef(s.c);',
  32368. ' this.d = rtl.arrayRef(s.d);',
  32369. ' return this;',
  32370. ' };',
  32371. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  32372. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  32373. ' eltype: rtl.char',
  32374. ' });',
  32375. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  32376. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  32377. '});',
  32378. 'this.p = null;',
  32379. 'this.r = this.TFloatRec.$new();',
  32380. '']),
  32381. LinesToStr([ // $mod.$main
  32382. '$mod.p = $mod.$rtti["TFloatRec"];',
  32383. '$mod.p = $mod.$rtti["TFloatRec"];',
  32384. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  32385. '']));
  32386. end;
  32387. procedure TTestModule.TestRTTI_Record_ClassVarType;
  32388. begin
  32389. WithTypeInfo:=true;
  32390. StartProgram(false);
  32391. Add([
  32392. '{$modeswitch AdvancedRecords}',
  32393. 'type',
  32394. ' TPoint = record',
  32395. ' type TProc = procedure(w: word);',
  32396. ' class var p: TProc;',
  32397. ' end;',
  32398. 'begin',
  32399. '']);
  32400. ConvertProgram;
  32401. CheckSource('TestRTTI_Record_ClassVarType',
  32402. LinesToStr([ // statements
  32403. 'rtl.recNewT(this, "TPoint", function () {',
  32404. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  32405. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  32406. ' });',
  32407. ' this.p = null;',
  32408. ' this.$eq = function (b) {',
  32409. ' return true;',
  32410. ' };',
  32411. ' this.$assign = function (s) {',
  32412. ' return this;',
  32413. ' };',
  32414. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  32415. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  32416. '}, true);',
  32417. '']),
  32418. LinesToStr([ // $mod.$main
  32419. '']));
  32420. end;
  32421. procedure TTestModule.TestRTTI_LocalTypes;
  32422. begin
  32423. WithTypeInfo:=true;
  32424. StartProgram(false);
  32425. Add([
  32426. 'procedure DoIt;',
  32427. 'type',
  32428. ' integer = longint;',
  32429. ' TPoint = record',
  32430. ' x,y: integer;',
  32431. ' end;',
  32432. 'var p: TPoint;',
  32433. 'begin',
  32434. 'end;',
  32435. 'begin']);
  32436. ConvertProgram;
  32437. CheckSource('TestRTTI_LocalTypes',
  32438. LinesToStr([ // statements
  32439. 'var TPoint = rtl.recNewT(null, "", function () {',
  32440. ' this.x = 0;',
  32441. ' this.y = 0;',
  32442. ' this.$eq = function (b) {',
  32443. ' return (this.x === b.x) && (this.y === b.y);',
  32444. ' };',
  32445. ' this.$assign = function (s) {',
  32446. ' this.x = s.x;',
  32447. ' this.y = s.y;',
  32448. ' return this;',
  32449. ' };',
  32450. '});',
  32451. 'this.DoIt = function () {',
  32452. ' var p = TPoint.$new();',
  32453. '};',
  32454. '']),
  32455. LinesToStr([ // $mod.$main
  32456. '']));
  32457. end;
  32458. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  32459. begin
  32460. WithTypeInfo:=true;
  32461. StartProgram(false);
  32462. Add([
  32463. 'type',
  32464. ' TCaption = string;',
  32465. ' TYesNo = boolean;',
  32466. ' TLetter = char;',
  32467. ' TFloat = double;',
  32468. ' TPtr = pointer;',
  32469. ' TShortInt = shortint;',
  32470. ' TByte = byte;',
  32471. ' TSmallInt = smallint;',
  32472. ' TWord = word;',
  32473. ' TInt32 = longint;',
  32474. ' TDWord = longword;',
  32475. ' TValue = jsvalue;',
  32476. 'var p: TPtr;',
  32477. 'begin',
  32478. ' p:=typeinfo(string);',
  32479. ' p:=typeinfo(tcaption);',
  32480. ' p:=typeinfo(boolean);',
  32481. ' p:=typeinfo(tyesno);',
  32482. ' p:=typeinfo(char);',
  32483. ' p:=typeinfo(tletter);',
  32484. ' p:=typeinfo(double);',
  32485. ' p:=typeinfo(tfloat);',
  32486. ' p:=typeinfo(pointer);',
  32487. ' p:=typeinfo(tptr);',
  32488. ' p:=typeinfo(shortint);',
  32489. ' p:=typeinfo(tshortint);',
  32490. ' p:=typeinfo(byte);',
  32491. ' p:=typeinfo(tbyte);',
  32492. ' p:=typeinfo(smallint);',
  32493. ' p:=typeinfo(tsmallint);',
  32494. ' p:=typeinfo(word);',
  32495. ' p:=typeinfo(tword);',
  32496. ' p:=typeinfo(longword);',
  32497. ' p:=typeinfo(tdword);',
  32498. ' p:=typeinfo(jsvalue);',
  32499. ' p:=typeinfo(tvalue);',
  32500. '']);
  32501. ConvertProgram;
  32502. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  32503. LinesToStr([ // statements
  32504. 'this.p = null;',
  32505. '']),
  32506. LinesToStr([ // $mod.$main
  32507. '$mod.p = rtl.string;',
  32508. '$mod.p = rtl.string;',
  32509. '$mod.p = rtl.boolean;',
  32510. '$mod.p = rtl.boolean;',
  32511. '$mod.p = rtl.char;',
  32512. '$mod.p = rtl.char;',
  32513. '$mod.p = rtl.double;',
  32514. '$mod.p = rtl.double;',
  32515. '$mod.p = rtl.pointer;',
  32516. '$mod.p = rtl.pointer;',
  32517. '$mod.p = rtl.shortint;',
  32518. '$mod.p = rtl.shortint;',
  32519. '$mod.p = rtl.byte;',
  32520. '$mod.p = rtl.byte;',
  32521. '$mod.p = rtl.smallint;',
  32522. '$mod.p = rtl.smallint;',
  32523. '$mod.p = rtl.word;',
  32524. '$mod.p = rtl.word;',
  32525. '$mod.p = rtl.longword;',
  32526. '$mod.p = rtl.longword;',
  32527. '$mod.p = rtl.jsvalue;',
  32528. '$mod.p = rtl.jsvalue;',
  32529. '']));
  32530. end;
  32531. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  32532. begin
  32533. WithTypeInfo:=true;
  32534. StartProgram(false);
  32535. Add([
  32536. 'type',
  32537. ' TCaption = type string;',
  32538. ' TYesNo = type boolean;',
  32539. ' TLetter = type char;',
  32540. ' TFloat = type double;',
  32541. ' TPtr = type pointer;',
  32542. ' TShortInt = type shortint;',
  32543. ' TByte = type byte;',
  32544. ' TSmallInt = type smallint;',
  32545. ' TWord = type word;',
  32546. ' TInt32 = type longint;',
  32547. ' TDWord = type longword;',
  32548. ' TValue = type jsvalue;',
  32549. ' TAliasValue = type TValue;',
  32550. 'var',
  32551. ' p: TPtr;',
  32552. ' a: TAliasValue;',
  32553. 'begin',
  32554. ' p:=typeinfo(tcaption);',
  32555. ' p:=typeinfo(tyesno);',
  32556. ' p:=typeinfo(tletter);',
  32557. ' p:=typeinfo(tfloat);',
  32558. ' p:=typeinfo(tptr);',
  32559. ' p:=typeinfo(tshortint);',
  32560. ' p:=typeinfo(tbyte);',
  32561. ' p:=typeinfo(tsmallint);',
  32562. ' p:=typeinfo(tword);',
  32563. ' p:=typeinfo(tdword);',
  32564. ' p:=typeinfo(tvalue);',
  32565. ' p:=typeinfo(taliasvalue);',
  32566. ' p:=typeinfo(a);',
  32567. '']);
  32568. ConvertProgram;
  32569. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  32570. LinesToStr([ // statements
  32571. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  32572. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  32573. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  32574. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  32575. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  32576. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  32577. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  32578. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  32579. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  32580. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  32581. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  32582. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  32583. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  32584. 'this.p = null;',
  32585. 'this.a = undefined;',
  32586. '']),
  32587. LinesToStr([ // $mod.$main
  32588. '$mod.p = $mod.$rtti["TCaption"];',
  32589. '$mod.p = $mod.$rtti["TYesNo"];',
  32590. '$mod.p = $mod.$rtti["TLetter"];',
  32591. '$mod.p = $mod.$rtti["TFloat"];',
  32592. '$mod.p = $mod.$rtti["TPtr"];',
  32593. '$mod.p = $mod.$rtti["TShortInt"];',
  32594. '$mod.p = $mod.$rtti["TByte"];',
  32595. '$mod.p = $mod.$rtti["TSmallInt"];',
  32596. '$mod.p = $mod.$rtti["TWord"];',
  32597. '$mod.p = $mod.$rtti["TDWord"];',
  32598. '$mod.p = $mod.$rtti["TValue"];',
  32599. '$mod.p = $mod.$rtti["TAliasValue"];',
  32600. '$mod.p = $mod.$rtti["TAliasValue"];',
  32601. '']));
  32602. end;
  32603. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  32604. begin
  32605. WithTypeInfo:=true;
  32606. StartProgram(false);
  32607. Add('procedure DoIt;');
  32608. Add('type');
  32609. Add(' integer = longint;');
  32610. Add(' TPoint = record');
  32611. Add(' x,y: integer;');
  32612. Add(' end;');
  32613. Add('var p: pointer;');
  32614. Add('begin');
  32615. Add(' p:=typeinfo(tpoint);');
  32616. Add('end;');
  32617. Add('begin');
  32618. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  32619. ConvertProgram;
  32620. end;
  32621. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  32622. begin
  32623. WithTypeInfo:=true;
  32624. StartProgram(true,[supTypeInfo]);
  32625. Add([
  32626. '{$modeswitch externalclass}',
  32627. 'type',
  32628. ' TFlag = (up,down);',
  32629. ' TFlags = set of TFlag;',
  32630. 'var',
  32631. ' ti: TTypeInfo;',
  32632. ' tiInt: TTypeInfoInteger;',
  32633. ' tiEnum: TTypeInfoEnum;',
  32634. ' tiSet: TTypeInfoSet;',
  32635. 'begin',
  32636. ' ti:=typeinfo(string);',
  32637. ' ti:=typeinfo(boolean);',
  32638. ' ti:=typeinfo(char);',
  32639. ' ti:=typeinfo(double);',
  32640. ' tiInt:=typeinfo(shortint);',
  32641. ' tiInt:=typeinfo(byte);',
  32642. ' tiInt:=typeinfo(smallint);',
  32643. ' tiInt:=typeinfo(word);',
  32644. ' tiInt:=typeinfo(longint);',
  32645. ' tiInt:=typeinfo(longword);',
  32646. ' ti:=typeinfo(jsvalue);',
  32647. ' tiEnum:=typeinfo(tflag);',
  32648. ' tiSet:=typeinfo(tflags);']);
  32649. ConvertProgram;
  32650. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  32651. LinesToStr([ // statements
  32652. 'this.TFlag = {',
  32653. ' "0": "up",',
  32654. ' up: 0,',
  32655. ' "1": "down",',
  32656. ' down: 1',
  32657. '};',
  32658. 'this.$rtti.$Enum("TFlag", {',
  32659. ' minvalue: 0,',
  32660. ' maxvalue: 1,',
  32661. ' ordtype: 1,',
  32662. ' enumtype: this.TFlag',
  32663. '});',
  32664. 'this.$rtti.$Set("TFlags", {',
  32665. ' comptype: this.$rtti["TFlag"]',
  32666. '});',
  32667. 'this.ti = null;',
  32668. 'this.tiInt = null;',
  32669. 'this.tiEnum = null;',
  32670. 'this.tiSet = null;',
  32671. '']),
  32672. LinesToStr([ // $mod.$main
  32673. '$mod.ti = rtl.string;',
  32674. '$mod.ti = rtl.boolean;',
  32675. '$mod.ti = rtl.char;',
  32676. '$mod.ti = rtl.double;',
  32677. '$mod.tiInt = rtl.shortint;',
  32678. '$mod.tiInt = rtl.byte;',
  32679. '$mod.tiInt = rtl.smallint;',
  32680. '$mod.tiInt = rtl.word;',
  32681. '$mod.tiInt = rtl.longint;',
  32682. '$mod.tiInt = rtl.longword;',
  32683. '$mod.ti = rtl.jsvalue;',
  32684. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  32685. '$mod.tiSet = $mod.$rtti["TFlags"];',
  32686. '']));
  32687. end;
  32688. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  32689. begin
  32690. WithTypeInfo:=true;
  32691. StartProgram(true,[supTypeInfo]);
  32692. Add('{$modeswitch externalclass}');
  32693. Add('type');
  32694. Add(' TStaticArr = array[boolean] of string;');
  32695. Add(' TDynArr = array of string;');
  32696. Add(' TProc = procedure;');
  32697. Add(' TMethod = procedure of object;');
  32698. Add('var');
  32699. Add(' StaticArray: TStaticArr;');
  32700. Add(' tiStaticArray: TTypeInfoStaticArray;');
  32701. Add(' DynArray: TDynArr;');
  32702. Add(' tiDynArray: TTypeInfoDynArray;');
  32703. Add(' ProcVar: TProc;');
  32704. Add(' tiProcVar: TTypeInfoProcVar;');
  32705. Add(' MethodVar: TMethod;');
  32706. Add(' tiMethodVar: TTypeInfoMethodVar;');
  32707. Add('begin');
  32708. Add(' tiStaticArray:=typeinfo(StaticArray);');
  32709. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  32710. Add(' tiDynArray:=typeinfo(DynArray);');
  32711. Add(' tiDynArray:=typeinfo(TDynArr);');
  32712. Add(' tiProcVar:=typeinfo(ProcVar);');
  32713. Add(' tiProcVar:=typeinfo(TProc);');
  32714. Add(' tiMethodVar:=typeinfo(MethodVar);');
  32715. Add(' tiMethodVar:=typeinfo(TMethod);');
  32716. ConvertProgram;
  32717. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  32718. LinesToStr([ // statements
  32719. 'this.$rtti.$StaticArray("TStaticArr", {',
  32720. ' dims: [2],',
  32721. ' eltype: rtl.string',
  32722. '});',
  32723. 'this.$rtti.$DynArray("TDynArr", {',
  32724. ' eltype: rtl.string',
  32725. '});',
  32726. 'this.$rtti.$ProcVar("TProc", {',
  32727. ' procsig: rtl.newTIProcSig([])',
  32728. '});',
  32729. 'this.$rtti.$MethodVar("TMethod", {',
  32730. ' procsig: rtl.newTIProcSig([]),',
  32731. ' methodkind: 0',
  32732. '});',
  32733. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  32734. 'this.tiStaticArray = null;',
  32735. 'this.DynArray = [];',
  32736. 'this.tiDynArray = null;',
  32737. 'this.ProcVar = null;',
  32738. 'this.tiProcVar = null;',
  32739. 'this.MethodVar = null;',
  32740. 'this.tiMethodVar = null;',
  32741. '']),
  32742. LinesToStr([ // $mod.$main
  32743. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32744. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  32745. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32746. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  32747. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32748. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  32749. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32750. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  32751. '']));
  32752. end;
  32753. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  32754. begin
  32755. WithTypeInfo:=true;
  32756. StartProgram(true,[supTypeInfo]);
  32757. Add('{$modeswitch externalclass}');
  32758. Add('type');
  32759. Add(' TRec = record end;');
  32760. // ToDo: ^TRec
  32761. Add(' TObject = class end;');
  32762. Add(' TClass = class of tobject;');
  32763. Add('var');
  32764. Add(' Rec: trec;');
  32765. Add(' tiRecord: ttypeinforecord;');
  32766. Add(' Obj: tobject;');
  32767. Add(' tiClass: ttypeinfoclass;');
  32768. Add(' aClass: tclass;');
  32769. Add(' tiClassRef: ttypeinfoclassref;');
  32770. // ToDo: ^TRec
  32771. Add(' tiPointer: ttypeinfopointer;');
  32772. Add('begin');
  32773. Add(' tirecord:=typeinfo(trec);');
  32774. Add(' tirecord:=typeinfo(trec);');
  32775. Add(' ticlass:=typeinfo(obj);');
  32776. Add(' ticlass:=typeinfo(tobject);');
  32777. Add(' ticlass:=typeinfo(aclass);');
  32778. Add(' ticlassref:=typeinfo(tclass);');
  32779. ConvertProgram;
  32780. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  32781. LinesToStr([ // statements
  32782. 'rtl.recNewT(this, "TRec", function () {',
  32783. ' this.$eq = function (b) {',
  32784. ' return true;',
  32785. ' };',
  32786. ' this.$assign = function (s) {',
  32787. ' return this;',
  32788. ' };',
  32789. ' $mod.$rtti.$Record("TRec", {});',
  32790. '});',
  32791. 'rtl.createClass(this, "TObject", null, function () {',
  32792. ' this.$init = function () {',
  32793. ' };',
  32794. ' this.$final = function () {',
  32795. ' };',
  32796. '});',
  32797. 'this.$rtti.$ClassRef("TClass", {',
  32798. ' instancetype: this.$rtti["TObject"]',
  32799. '});',
  32800. 'this.Rec = this.TRec.$new();',
  32801. 'this.tiRecord = null;',
  32802. 'this.Obj = null;',
  32803. 'this.tiClass = null;',
  32804. 'this.aClass = null;',
  32805. 'this.tiClassRef = null;',
  32806. 'this.tiPointer = null;',
  32807. '']),
  32808. LinesToStr([ // $mod.$main
  32809. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32810. '$mod.tiRecord = $mod.$rtti["TRec"];',
  32811. '$mod.tiClass = $mod.Obj.$rtti;',
  32812. '$mod.tiClass = $mod.$rtti["TObject"];',
  32813. '$mod.tiClass = $mod.aClass.$rtti;',
  32814. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  32815. '']));
  32816. end;
  32817. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  32818. begin
  32819. WithTypeInfo:=true;
  32820. StartProgram(true,[supTypeInfo]);
  32821. Add([
  32822. '{$modeswitch externalclass}',
  32823. 'type',
  32824. ' TClass = class of tobject;',
  32825. ' TObject = class',
  32826. ' function MyClass: TClass;',
  32827. ' class function ClassType: TClass;',
  32828. ' end;',
  32829. 'function TObject.MyClass: TClass;',
  32830. 'var t: TTypeInfoClass;',
  32831. 'begin',
  32832. ' t:=TypeInfo(Self);',
  32833. ' t:=TypeInfo(Result);',
  32834. ' t:=TypeInfo(TObject);',
  32835. 'end;',
  32836. 'class function TObject.ClassType: TClass;',
  32837. 'var t: TTypeInfoClass;',
  32838. 'begin',
  32839. ' t:=TypeInfo(Self);',
  32840. ' t:=TypeInfo(Result);',
  32841. 'end;',
  32842. 'var',
  32843. ' Obj: TObject;',
  32844. ' t: TTypeInfoClass;',
  32845. 'begin',
  32846. ' t:=TypeInfo(TObject.ClassType);',
  32847. ' t:=TypeInfo(Obj.ClassType);',
  32848. ' t:=TypeInfo(Obj.MyClass);',
  32849. '']);
  32850. ConvertProgram;
  32851. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  32852. LinesToStr([ // statements
  32853. 'this.$rtti.$Class("TObject");',
  32854. 'this.$rtti.$ClassRef("TClass", {',
  32855. ' instancetype: this.$rtti["TObject"]',
  32856. '});',
  32857. 'rtl.createClass(this, "TObject", null, function () {',
  32858. ' this.$init = function () {',
  32859. ' };',
  32860. ' this.$final = function () {',
  32861. ' };',
  32862. ' this.MyClass = function () {',
  32863. ' var Result = null;',
  32864. ' var t = null;',
  32865. ' t = this.$rtti;',
  32866. ' t = Result.$rtti;',
  32867. ' t = $mod.$rtti["TObject"];',
  32868. ' return Result;',
  32869. ' };',
  32870. ' this.ClassType = function () {',
  32871. ' var Result = null;',
  32872. ' var t = null;',
  32873. ' t = this.$rtti;',
  32874. ' t = Result.$rtti;',
  32875. ' return Result;',
  32876. ' };',
  32877. '});',
  32878. 'this.Obj = null;',
  32879. 'this.t = null;',
  32880. '']),
  32881. LinesToStr([ // $mod.$main
  32882. '$mod.t = $mod.TObject.ClassType().$rtti;',
  32883. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  32884. '$mod.t = $mod.Obj.MyClass().$rtti;',
  32885. '']));
  32886. end;
  32887. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  32888. begin
  32889. WithTypeInfo:=true;
  32890. AddModuleWithIntfImplSrc('typinfo.pas',
  32891. LinesToStr([
  32892. '{$modeswitch externalclass}',
  32893. 'type',
  32894. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  32895. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  32896. '']),
  32897. '');
  32898. AddModuleWithIntfImplSrc('unit2.pas',
  32899. LinesToStr([
  32900. 'uses typinfo;',
  32901. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  32902. 'procedure DoPtr(p: PTypeInfo);',
  32903. 'procedure DoInfo(t: TTypeInfo);',
  32904. 'procedure DoInt(t: TTypeInfoInteger);',
  32905. '']),
  32906. LinesToStr([
  32907. 'procedure DoPtr(p: PTypeInfo);',
  32908. 'begin end;',
  32909. 'procedure DoInfo(t: TTypeInfo);',
  32910. 'begin end;',
  32911. 'procedure DoInt(t: TTypeInfoInteger);',
  32912. 'begin end;',
  32913. '']));
  32914. StartUnit(true);
  32915. Add([
  32916. 'interface',
  32917. 'uses unit2;', // does not use unit typinfo
  32918. 'implementation',
  32919. 'var',
  32920. ' i: byte;',
  32921. ' p: pointer;',
  32922. ' t: PTypeInfo;',
  32923. 'initialization',
  32924. ' p:=typeinfo(i);',
  32925. ' t:=typeinfo(i);',
  32926. ' if p=t then ;',
  32927. ' if p=typeinfo(i) then ;',
  32928. ' if typeinfo(i)=p then ;',
  32929. ' if t=typeinfo(i) then ;',
  32930. ' if typeinfo(i)=t then ;',
  32931. ' DoPtr(p);',
  32932. ' DoPtr(t);',
  32933. ' DoPtr(typeinfo(i));',
  32934. ' DoInfo(p);',
  32935. ' DoInfo(t);',
  32936. ' DoInfo(typeinfo(i));',
  32937. ' DoInt(typeinfo(i));',
  32938. '']);
  32939. ConvertUnit;
  32940. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  32941. LinesToStr([ // statements
  32942. 'var $impl = $mod.$impl;',
  32943. '']),
  32944. LinesToStr([ // this.$init
  32945. '$impl.p = rtl.byte;',
  32946. '$impl.t = rtl.byte;',
  32947. 'if ($impl.p === $impl.t) ;',
  32948. 'if ($impl.p === rtl.byte) ;',
  32949. 'if (rtl.byte === $impl.p) ;',
  32950. 'if ($impl.t === rtl.byte) ;',
  32951. 'if (rtl.byte === $impl.t) ;',
  32952. 'pas.unit2.DoPtr($impl.p);',
  32953. 'pas.unit2.DoPtr($impl.t);',
  32954. 'pas.unit2.DoPtr(rtl.byte);',
  32955. 'pas.unit2.DoInfo($impl.p);',
  32956. 'pas.unit2.DoInfo($impl.t);',
  32957. 'pas.unit2.DoInfo(rtl.byte);',
  32958. 'pas.unit2.DoInt(rtl.byte);',
  32959. '']),
  32960. LinesToStr([ // implementation
  32961. '$impl.i = 0;',
  32962. '$impl.p = null;',
  32963. '$impl.t = null;',
  32964. '']) );
  32965. end;
  32966. procedure TTestModule.TestRTTI_Interface_Corba;
  32967. begin
  32968. WithTypeInfo:=true;
  32969. StartProgram(true,[supTypeInfo]);
  32970. Add([
  32971. '{$interfaces corba}',
  32972. '{$modeswitch externalclass}',
  32973. 'type',
  32974. ' IUnknown = interface',
  32975. ' end;',
  32976. ' IBird = interface',
  32977. ' function GetItem: longint;',
  32978. ' procedure SetItem(Value: longint);',
  32979. ' property Item: longint read GetItem write SetItem;',
  32980. ' end;',
  32981. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  32982. 'var',
  32983. ' i: IBird;',
  32984. ' t: TTypeInfoInterface;',
  32985. 'begin',
  32986. ' t:=TypeInfo(IBird);',
  32987. ' t:=TypeInfo(i);',
  32988. ' DoIt(t);',
  32989. ' DoIt(TypeInfo(IBird));',
  32990. '']);
  32991. ConvertProgram;
  32992. CheckSource('TestRTTI_Interface_Corba',
  32993. LinesToStr([ // statements
  32994. 'rtl.createInterface(',
  32995. ' this,',
  32996. ' "IUnknown",',
  32997. ' "{B92D5841-758A-322B-B800-000000000000}",',
  32998. ' [],',
  32999. ' null,',
  33000. ' function () {',
  33001. ' }',
  33002. ');',
  33003. 'rtl.createInterface(',
  33004. ' this,',
  33005. ' "IBird",',
  33006. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  33007. ' ["GetItem", "SetItem"],',
  33008. ' null,',
  33009. ' function () {',
  33010. ' var $r = this.$rtti;',
  33011. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  33012. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  33013. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem", 2);',
  33014. ' }',
  33015. ');',
  33016. 'this.DoIt = function (t) {',
  33017. '}; ',
  33018. 'this.i = null;',
  33019. 'this.t = null;',
  33020. '']),
  33021. LinesToStr([ // $mod.$main
  33022. '$mod.t = $mod.$rtti["IBird"];',
  33023. '$mod.t = $mod.i.$rtti;',
  33024. '$mod.DoIt($mod.t);',
  33025. '$mod.DoIt($mod.$rtti["IBird"]);',
  33026. '']));
  33027. end;
  33028. procedure TTestModule.TestRTTI_Interface_COM;
  33029. begin
  33030. WithTypeInfo:=true;
  33031. StartProgram(true,[supTypeInfo]);
  33032. Add([
  33033. '{$interfaces com}',
  33034. '{$modeswitch externalclass}',
  33035. 'type',
  33036. ' TGuid = record end;',
  33037. ' integer = longint;',
  33038. ' IUnknown = interface',
  33039. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  33040. ' function _AddRef: Integer;',
  33041. ' function _Release: Integer;',
  33042. ' end;',
  33043. ' IBird = interface',
  33044. ' function GetItem: longint;',
  33045. ' procedure SetItem(Value: longint);',
  33046. ' property Item: longint read GetItem write SetItem;',
  33047. ' end;',
  33048. 'var',
  33049. ' i: IBird;',
  33050. ' t: TTypeInfoInterface;',
  33051. 'begin',
  33052. ' t:=TypeInfo(IBird);',
  33053. ' t:=TypeInfo(i);',
  33054. '']);
  33055. ConvertProgram;
  33056. CheckSource('TestRTTI_Interface_COM',
  33057. LinesToStr([ // statements
  33058. 'rtl.recNewT(this, "TGuid", function () {',
  33059. ' this.$eq = function (b) {',
  33060. ' return true;',
  33061. ' };',
  33062. ' this.$assign = function (s) {',
  33063. ' return this;',
  33064. ' };',
  33065. ' $mod.$rtti.$Record("TGuid", {});',
  33066. '});',
  33067. 'rtl.createInterface(',
  33068. ' this,',
  33069. ' "IUnknown",',
  33070. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  33071. ' ["QueryInterface", "_AddRef", "_Release"],',
  33072. ' null,',
  33073. ' function () {',
  33074. ' this.$kind = "com";',
  33075. ' var $r = this.$rtti;',
  33076. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  33077. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  33078. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  33079. ' }',
  33080. ');',
  33081. 'rtl.createInterface(',
  33082. ' this,',
  33083. ' "IBird",',
  33084. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  33085. ' ["GetItem", "SetItem"],',
  33086. ' this.IUnknown,',
  33087. ' function () {',
  33088. ' var $r = this.$rtti;',
  33089. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  33090. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  33091. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem", 2);',
  33092. ' }',
  33093. ');',
  33094. 'this.i = null;',
  33095. 'this.t = null;',
  33096. '']),
  33097. LinesToStr([ // $mod.$main
  33098. '$mod.t = $mod.$rtti["IBird"];',
  33099. '$mod.t = $mod.i.$rtti;',
  33100. '']));
  33101. end;
  33102. procedure TTestModule.TestRTTI_ClassHelper;
  33103. begin
  33104. WithTypeInfo:=true;
  33105. StartProgram(true,[supTypeInfo]);
  33106. Add([
  33107. '{$interfaces com}',
  33108. '{$modeswitch externalclass}',
  33109. 'type',
  33110. ' TObject = class',
  33111. ' end;',
  33112. ' THelper = class helper for TObject',
  33113. ' published',
  33114. ' function GetItem: longint;',
  33115. ' property Item: longint read GetItem;',
  33116. ' end;',
  33117. 'function THelper.GetItem: longint;',
  33118. 'begin',
  33119. 'end;',
  33120. 'var',
  33121. ' t: TTypeInfoHelper;',
  33122. 'begin',
  33123. ' t:=TypeInfo(THelper);',
  33124. '']);
  33125. ConvertProgram;
  33126. CheckSource('TestRTTI_ClassHelper',
  33127. LinesToStr([ // statements
  33128. 'rtl.createClass(this, "TObject", null, function () {',
  33129. ' this.$init = function () {',
  33130. ' };',
  33131. ' this.$final = function () {',
  33132. ' };',
  33133. '});',
  33134. 'rtl.createHelper(this, "THelper", null, function () {',
  33135. ' this.GetItem = function () {',
  33136. ' var Result = 0;',
  33137. ' return Result;',
  33138. ' };',
  33139. ' var $r = this.$rtti;',
  33140. ' $r.addMethod("GetItem", 1, [], 4, rtl.longint);',
  33141. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  33142. '});',
  33143. 'this.t = null;',
  33144. '']),
  33145. LinesToStr([ // $mod.$main
  33146. '$mod.t = $mod.$rtti["THelper"];',
  33147. '']));
  33148. end;
  33149. procedure TTestModule.TestRTTI_ExternalClass;
  33150. begin
  33151. WithTypeInfo:=true;
  33152. StartProgram(true,[supTypeInfo]);
  33153. Add([
  33154. '{$modeswitch externalclass}',
  33155. 'type',
  33156. ' TJSObject = class external name ''Object''',
  33157. ' end;',
  33158. ' TJSArray = class external name ''Array'' (TJSObject)',
  33159. ' end;',
  33160. 'var',
  33161. ' p: Pointer;',
  33162. ' tc: TTypeInfoExtClass;',
  33163. 'begin',
  33164. ' p:=typeinfo(TJSArray);']);
  33165. ConvertProgram;
  33166. CheckSource('TestRTTI_ExternalClass',
  33167. LinesToStr([ // statements
  33168. 'this.$rtti.$ExtClass("TJSObject", {',
  33169. ' jsclass: "Object"',
  33170. '});',
  33171. 'this.$rtti.$ExtClass("TJSArray", {',
  33172. ' ancestor: this.$rtti["TJSObject"],',
  33173. ' jsclass: "Array"',
  33174. '});',
  33175. 'this.p = null;',
  33176. 'this.tc = null;',
  33177. '']),
  33178. LinesToStr([ // $mod.$main
  33179. '$mod.p = $mod.$rtti["TJSArray"];',
  33180. '']));
  33181. end;
  33182. procedure TTestModule.TestRTTI_Unit;
  33183. begin
  33184. WithTypeInfo:=true;
  33185. AddModuleWithIntfImplSrc('unit2.pas',
  33186. LinesToStr([
  33187. '{$mode delphi}',
  33188. 'type',
  33189. ' TWordArray = array of word;',
  33190. ' TArray<T> = array of T;',
  33191. '']),
  33192. '');
  33193. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  33194. Add([
  33195. '{$mode delphi}',
  33196. 'interface',
  33197. 'uses unit2;',
  33198. 'type',
  33199. ' IBird = interface',
  33200. ' function Swoop: TWordArray;',
  33201. ' function Glide: TArray<word>;',
  33202. ' end;',
  33203. 'procedure Fly;',
  33204. 'implementation',
  33205. 'procedure Fly;',
  33206. 'var',
  33207. ' ta: tTypeInfoDynArray;',
  33208. ' ti: tTypeInfoInterface;',
  33209. 'begin',
  33210. ' ta:=typeinfo(TWordArray);',
  33211. ' ta:=typeinfo(TArray<word>);',
  33212. ' ti:=typeinfo(IBird);',
  33213. 'end;',
  33214. '']);
  33215. ConvertUnit;
  33216. CheckSource('TestRTTI_ExternalClass',
  33217. LinesToStr([ // statements
  33218. 'rtl.createInterface(',
  33219. ' this,',
  33220. ' "IBird",',
  33221. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  33222. ' ["Swoop", "Glide"],',
  33223. ' pas.system.IUnknown,',
  33224. ' function () {',
  33225. ' var $r = this.$rtti;',
  33226. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  33227. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  33228. ' }',
  33229. ');',
  33230. 'this.Fly = function () {',
  33231. ' var ta = null;',
  33232. ' var ti = null;',
  33233. ' ta = pas.unit2.$rtti["TWordArray"];',
  33234. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  33235. ' ti = $mod.$rtti["IBird"];',
  33236. '};',
  33237. '']),
  33238. LinesToStr([ // $mod.$main
  33239. '']));
  33240. end;
  33241. procedure TTestModule.TestResourcestringProgram;
  33242. begin
  33243. AddModuleWithIntfImplSrc('unit2.pas',
  33244. LinesToStr([
  33245. 'resourcestring Title = ''Nice'';',
  33246. '']),
  33247. '');
  33248. StartProgram(true);
  33249. Add([
  33250. 'uses unit2;',
  33251. 'const Bar = ''bar'';',
  33252. 'resourcestring',
  33253. ' Red = ''red'';',
  33254. ' Foobar = ''fOo''+bar;',
  33255. 'var s: string;',
  33256. ' c: char;',
  33257. 'begin',
  33258. ' s:=red;',
  33259. ' s:=test1.red;',
  33260. ' s:=Title;',
  33261. ' c:=red[1];',
  33262. ' c:=test1.red[2];',
  33263. ' if red=foobar then ;',
  33264. ' if red[3]=red[4] then ;']);
  33265. ConvertProgram;
  33266. CheckSource('TestResourcestringProgram',
  33267. LinesToStr([ // statements
  33268. 'this.Bar = "bar";',
  33269. 'this.s = "";',
  33270. 'this.c = "\x00";',
  33271. '$mod.$resourcestrings = {',
  33272. ' Red: {',
  33273. ' org: "red"',
  33274. ' },',
  33275. ' Foobar: {',
  33276. ' org: "fOobar"',
  33277. ' }',
  33278. '};',
  33279. '']),
  33280. LinesToStr([ // $mod.$main
  33281. '$mod.s = rtl.getResStr($mod, "Red");',
  33282. '$mod.s = rtl.getResStr($mod, "Red");',
  33283. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  33284. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  33285. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  33286. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  33287. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  33288. '']));
  33289. end;
  33290. procedure TTestModule.TestResourcestringUnit;
  33291. begin
  33292. AddModuleWithIntfImplSrc('unit2.pas',
  33293. LinesToStr([
  33294. 'resourcestring Title = ''Nice'';',
  33295. '']),
  33296. '');
  33297. StartUnit(true);
  33298. Add([
  33299. 'interface',
  33300. 'uses unit2;',
  33301. 'const Red = ''rEd'';',
  33302. 'resourcestring',
  33303. ' Blue = ''blue'';',
  33304. ' NotRed = ''not''+Red;',
  33305. 'var s: string;',
  33306. 'implementation',
  33307. 'resourcestring',
  33308. ' ImplGreen = ''green'';',
  33309. 'initialization',
  33310. ' s:=blue+ImplGreen;',
  33311. ' s:=test1.blue+test1.implgreen;',
  33312. ' s:=blue[1]+implgreen[2];',
  33313. ' s:=Title;',
  33314. '']);
  33315. ConvertUnit;
  33316. CheckSource('TestResourcestringUnit',
  33317. LinesToStr([ // statements
  33318. 'this.Red = "rEd";',
  33319. 'this.s = "";',
  33320. '$mod.$resourcestrings = {',
  33321. ' Blue: {',
  33322. ' org: "blue"',
  33323. ' },',
  33324. ' NotRed: {',
  33325. ' org: "notrEd"',
  33326. ' },',
  33327. ' ImplGreen: {',
  33328. ' org: "green"',
  33329. ' }',
  33330. '};',
  33331. '']),
  33332. LinesToStr([ // $mod.$main
  33333. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  33334. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  33335. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  33336. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  33337. '']));
  33338. end;
  33339. procedure TTestModule.TestResourcestringImplementation;
  33340. begin
  33341. StartUnit(false);
  33342. Add([
  33343. 'interface',
  33344. 'implementation',
  33345. 'resourcestring',
  33346. ' ImplRed = ''red'';']);
  33347. ConvertUnit;
  33348. CheckSource('TestResourcestringImplementation',
  33349. LinesToStr([ // intf statements
  33350. 'var $impl = $mod.$impl;']),
  33351. LinesToStr([ // $mod.$init
  33352. '']),
  33353. LinesToStr([ // impl statements
  33354. '$mod.$resourcestrings = {',
  33355. ' ImplRed: {',
  33356. ' org: "red"',
  33357. ' }',
  33358. '};',
  33359. '']));
  33360. end;
  33361. procedure TTestModule.TestAttributes_Members;
  33362. begin
  33363. WithTypeInfo:=true;
  33364. StartProgram(false);
  33365. Add([
  33366. '{$modeswitch PrefixedAttributes}',
  33367. 'type',
  33368. ' TObject = class',
  33369. ' constructor Create;',
  33370. ' end;',
  33371. ' TCustomAttribute = class',
  33372. ' constructor Create(Id: word);',
  33373. ' end;',
  33374. ' [Missing]',
  33375. ' TBird = class',
  33376. ' published',
  33377. ' [Tcustom]',
  33378. ' FField: word;',
  33379. ' [tcustom(14)]',
  33380. ' property Size: word read FField;',
  33381. ' [Tcustom(15)]',
  33382. ' procedure Fly; virtual; abstract;',
  33383. ' end;',
  33384. ' TRec = record',
  33385. ' [Tcustom,tcustom(14)]',
  33386. ' Size: word;',
  33387. ' [Tcustom(15)]',
  33388. ' Width, Height: word;',
  33389. ' end;',
  33390. 'constructor TObject.Create; begin end;',
  33391. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  33392. 'begin',
  33393. '']);
  33394. ConvertProgram;
  33395. CheckSource('TestAttributes_Members',
  33396. LinesToStr([ // statements
  33397. 'rtl.createClass(this, "TObject", null, function () {',
  33398. ' this.$init = function () {',
  33399. ' };',
  33400. ' this.$final = function () {',
  33401. ' };',
  33402. ' this.Create = function () {',
  33403. ' return this;',
  33404. ' };',
  33405. '});',
  33406. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33407. ' this.Create$1 = function (Id) {',
  33408. ' return this;',
  33409. ' };',
  33410. '});',
  33411. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33412. ' this.$init = function () {',
  33413. ' $mod.TObject.$init.call(this);',
  33414. ' this.FField = 0;',
  33415. ' };',
  33416. ' var $r = this.$rtti;',
  33417. ' $r.addField("FField", rtl.word, 4, {',
  33418. ' attr: [$mod.TCustomAttribute, "Create"]',
  33419. ' });',
  33420. ' $r.addProperty(',
  33421. ' "Size",',
  33422. ' 0,',
  33423. ' rtl.word,',
  33424. ' "FField",',
  33425. ' "",',
  33426. ' 4,',
  33427. ' {',
  33428. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  33429. ' }',
  33430. ' );',
  33431. ' $r.addMethod(',
  33432. ' "Fly",',
  33433. ' 0,',
  33434. ' [],',
  33435. ' 4,',
  33436. ' null,',
  33437. ' 0,',
  33438. ' {',
  33439. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  33440. ' });',
  33441. '});',
  33442. 'rtl.recNewT(this, "TRec", function () {',
  33443. ' this.Size = 0;',
  33444. ' this.Width = 0;',
  33445. ' this.Height = 0;',
  33446. ' this.$eq = function (b) {',
  33447. ' return (this.Size === b.Size) && (this.Width === b.Width) && (this.Height === b.Height);',
  33448. ' };',
  33449. ' this.$assign = function (s) {',
  33450. ' this.Size = s.Size;',
  33451. ' this.Width = s.Width;',
  33452. ' this.Height = s.Height;',
  33453. ' return this;',
  33454. ' };',
  33455. ' var $r = $mod.$rtti.$Record("TRec", {});',
  33456. ' $r.addField("Size", rtl.word, 2, {',
  33457. ' attr: [',
  33458. ' $mod.TCustomAttribute,',
  33459. ' "Create",',
  33460. ' $mod.TCustomAttribute,',
  33461. ' "Create$1",',
  33462. ' [14]',
  33463. ' ]',
  33464. ' });',
  33465. ' $r.addField("Width", rtl.word, 2, {',
  33466. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  33467. ' });',
  33468. ' $r.addField("Height", rtl.word);',
  33469. '});',
  33470. '']),
  33471. LinesToStr([ // $mod.$main
  33472. '']));
  33473. end;
  33474. procedure TTestModule.TestAttributes_Types;
  33475. begin
  33476. WithTypeInfo:=true;
  33477. StartProgram(false);
  33478. Add([
  33479. '{$modeswitch PrefixedAttributes}',
  33480. 'type',
  33481. ' TObject = class',
  33482. ' constructor Create(Id: word);',
  33483. ' end;',
  33484. ' TCustomAttribute = class',
  33485. ' end;',
  33486. ' [TCustom(1)]',
  33487. ' TMyClass = class',
  33488. ' end;',
  33489. ' [TCustom(11)]',
  33490. ' TMyDescendant = class(TMyClass)',
  33491. ' end;',
  33492. ' [TCustom(2)]',
  33493. ' TRec = record',
  33494. ' end;',
  33495. ' [TCustom(3)]',
  33496. ' TInt = type word;',
  33497. 'constructor TObject.Create(Id: word);',
  33498. 'begin',
  33499. 'end;',
  33500. 'var p: pointer;',
  33501. 'begin',
  33502. ' p:=typeinfo(TMyClass);',
  33503. ' p:=typeinfo(TRec);',
  33504. ' p:=typeinfo(TInt);',
  33505. '']);
  33506. ConvertProgram;
  33507. CheckSource('TestAttributes_Types',
  33508. LinesToStr([ // statements
  33509. 'rtl.createClass(this, "TObject", null, function () {',
  33510. ' this.$init = function () {',
  33511. ' };',
  33512. ' this.$final = function () {',
  33513. ' };',
  33514. ' this.Create = function (Id) {',
  33515. ' return this;',
  33516. ' };',
  33517. '});',
  33518. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33519. '});',
  33520. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  33521. ' var $r = this.$rtti;',
  33522. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  33523. '});',
  33524. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  33525. ' var $r = this.$rtti;',
  33526. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  33527. '});',
  33528. 'rtl.recNewT(this, "TRec", function () {',
  33529. ' this.$eq = function (b) {',
  33530. ' return true;',
  33531. ' };',
  33532. ' this.$assign = function (s) {',
  33533. ' return this;',
  33534. ' };',
  33535. ' $mod.$rtti.$Record("TRec", {',
  33536. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  33537. ' });',
  33538. '});',
  33539. 'this.$rtti.$inherited("TInt", rtl.word, {',
  33540. ' attr: [this.TCustomAttribute, "Create", [3]]',
  33541. '});',
  33542. 'this.p = null;',
  33543. '']),
  33544. LinesToStr([ // $mod.$main
  33545. '$mod.p = $mod.$rtti["TMyClass"];',
  33546. '$mod.p = $mod.$rtti["TRec"];',
  33547. '$mod.p = $mod.$rtti["TInt"];',
  33548. '']));
  33549. end;
  33550. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  33551. begin
  33552. WithTypeInfo:=true;
  33553. StartProgram(false);
  33554. Add([
  33555. '{$modeswitch PrefixedAttributes}',
  33556. 'type',
  33557. ' TObject = class',
  33558. ' constructor Create;',
  33559. ' end;',
  33560. ' TCustomAttribute = class',
  33561. ' end;',
  33562. ' THelper = class helper for TCustomAttribute',
  33563. ' constructor Create(Id: word);',
  33564. ' end;',
  33565. ' [TCustom(3)]',
  33566. ' TMyInt = word;',
  33567. 'constructor TObject.Create; begin end;',
  33568. 'constructor THelper.Create(Id: word); begin end;',
  33569. 'begin',
  33570. ' if typeinfo(TMyInt)=nil then ;']);
  33571. ConvertProgram;
  33572. end;
  33573. procedure TTestModule.TestAttributes_InterfacesList;
  33574. begin
  33575. WithTypeInfo:=true;
  33576. StartProgram(false);
  33577. Add([
  33578. '{$mode Delphi}',
  33579. 'type',
  33580. ' TObject = class',
  33581. ' constructor Create;',
  33582. ' end;',
  33583. ' IInterface = interface end;',
  33584. ' TCustomAttribute = class',
  33585. ' end;',
  33586. ' Red = class(TCustomAttribute);',
  33587. ' Blue = class(TCustomAttribute);',
  33588. ' [Red]',
  33589. ' IBird<T> = interface',
  33590. ' procedure Fly;',
  33591. ' end;',
  33592. ' [Blue]',
  33593. ' IEagle = interface(IBird<Word>)',
  33594. ' procedure Dive;',
  33595. ' end;',
  33596. ' TAnt = class(TObject, IEagle)',
  33597. ' procedure Fly; virtual; abstract;',
  33598. ' procedure Dive; virtual; abstract;',
  33599. ' end;',
  33600. 'constructor TObject.Create;',
  33601. 'begin',
  33602. 'end;',
  33603. 'begin',
  33604. '']);
  33605. ConvertProgram;
  33606. CheckSource('TestAttributes_InterfacesList',
  33607. LinesToStr([ // statements
  33608. '$mod.$rtti.$Interface("IBird<System.Word>");',
  33609. 'rtl.createClass(this, "TObject", null, function () {',
  33610. ' this.$init = function () {',
  33611. ' };',
  33612. ' this.$final = function () {',
  33613. ' };',
  33614. ' this.Create = function () {',
  33615. ' return this;',
  33616. ' };',
  33617. '});',
  33618. 'rtl.createInterface(',
  33619. ' this,',
  33620. ' "IInterface",',
  33621. ' "{B92D5841-698D-3153-90C5-000000000000}",',
  33622. ' [],',
  33623. ' null,',
  33624. ' function () {',
  33625. ' this.$kind = "com";',
  33626. ' }',
  33627. ');',
  33628. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  33629. '});',
  33630. 'rtl.createClass(this, "Red", this.TCustomAttribute, function () {',
  33631. '});',
  33632. 'rtl.createClass(this, "Blue", this.TCustomAttribute, function () {',
  33633. '});',
  33634. 'rtl.createInterface(',
  33635. ' this,',
  33636. ' "IBird$G1",',
  33637. ' "{14691591-6648-3574-B8C8-FAAD81DAC421}",',
  33638. ' ["Fly"],',
  33639. ' this.IInterface,',
  33640. ' function () {',
  33641. ' var $r = this.$rtti;',
  33642. ' $r.addMethod("Fly", 0, []);',
  33643. ' $r.attr = [$mod.Red, "Create"];',
  33644. ' },',
  33645. ' "IBird<System.Word>"',
  33646. ');',
  33647. 'rtl.createInterface(',
  33648. ' this,',
  33649. ' "IEagle",',
  33650. ' "{5F4202AE-F2BE-37FD-8A88-1A2F926F1117}",',
  33651. ' ["Dive"],',
  33652. ' this.IBird$G1,',
  33653. ' function () {',
  33654. ' var $r = this.$rtti;',
  33655. ' $r.addMethod("Dive", 0, []);',
  33656. ' $r.attr = [$mod.Blue, "Create"];',
  33657. ' }',
  33658. ');',
  33659. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  33660. ' rtl.addIntf(this, $mod.IEagle);',
  33661. '});',
  33662. '']),
  33663. LinesToStr([ // $mod.$main
  33664. '']));
  33665. end;
  33666. procedure TTestModule.TestAssert;
  33667. begin
  33668. StartProgram(false);
  33669. Add([
  33670. 'procedure DoIt;',
  33671. 'var',
  33672. ' b: boolean;',
  33673. ' s: string;',
  33674. 'begin',
  33675. ' {$Assertions on}',
  33676. ' Assert(b);',
  33677. 'end;',
  33678. 'begin',
  33679. ' DoIt;',
  33680. '']);
  33681. ConvertProgram;
  33682. CheckSource('TestAssert',
  33683. LinesToStr([ // statements
  33684. 'this.DoIt = function () {',
  33685. ' var b = false;',
  33686. ' var s = "";',
  33687. ' if (!b) throw "assert failed";',
  33688. '};',
  33689. '']),
  33690. LinesToStr([ // $mod.$main
  33691. '$mod.DoIt();',
  33692. '']));
  33693. end;
  33694. procedure TTestModule.TestAssert_SysUtils;
  33695. begin
  33696. AddModuleWithIntfImplSrc('SysUtils.pas',
  33697. LinesToStr([
  33698. 'type',
  33699. ' TObject = class',
  33700. ' constructor Create;',
  33701. ' end;',
  33702. ' EAssertionFailed = class',
  33703. ' constructor Create(s: string);',
  33704. ' end;',
  33705. '']),
  33706. LinesToStr([
  33707. 'constructor TObject.Create;',
  33708. 'begin end;',
  33709. 'constructor EAssertionFailed.Create(s: string);',
  33710. 'begin end;',
  33711. '']) );
  33712. StartProgram(true);
  33713. Add([
  33714. 'uses sysutils;',
  33715. 'procedure DoIt;',
  33716. 'var',
  33717. ' b: boolean;',
  33718. ' s: string;',
  33719. 'begin',
  33720. ' {$Assertions on}',
  33721. ' Assert(b);',
  33722. ' Assert(b,''msg'');',
  33723. 'end;',
  33724. 'begin',
  33725. ' DoIt;',
  33726. '']);
  33727. ConvertProgram;
  33728. CheckSource('TestAssert_SysUtils',
  33729. LinesToStr([ // statements
  33730. 'this.DoIt = function () {',
  33731. ' var b = false;',
  33732. ' var s = "";',
  33733. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  33734. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  33735. '};',
  33736. '']),
  33737. LinesToStr([ // $mod.$main
  33738. '$mod.DoIt();',
  33739. '']));
  33740. end;
  33741. procedure TTestModule.TestObjectChecks;
  33742. begin
  33743. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  33744. StartProgram(false);
  33745. Add([
  33746. 'type',
  33747. ' TObject = class',
  33748. ' procedure DoIt;',
  33749. ' end;',
  33750. ' TClass = class of tobject;',
  33751. ' TBird = class',
  33752. ' end;',
  33753. ' TBirdClass = class of TBird;',
  33754. 'var',
  33755. ' o : TObject;',
  33756. ' c: TClass;',
  33757. ' b: TBird;',
  33758. ' bc: TBirdClass;',
  33759. 'procedure TObject.DoIt;',
  33760. 'begin',
  33761. ' b:=TBird(o);',
  33762. 'end;',
  33763. 'begin',
  33764. ' o.DoIt;',
  33765. ' b:=TBird(o);',
  33766. ' bc:=TBirdClass(c);',
  33767. '']);
  33768. ConvertProgram;
  33769. CheckSource('TestCheckMethodCall',
  33770. LinesToStr([ // statements
  33771. 'rtl.createClass(this, "TObject", null, function () {',
  33772. ' this.$init = function () {',
  33773. ' };',
  33774. ' this.$final = function () {',
  33775. ' };',
  33776. ' this.DoIt = function () {',
  33777. ' rtl.checkMethodCall(this,$mod.TObject);',
  33778. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  33779. ' };',
  33780. '});',
  33781. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  33782. '});',
  33783. 'this.o = null;',
  33784. 'this.c = null;',
  33785. 'this.b = null;',
  33786. 'this.bc = null;',
  33787. '']),
  33788. LinesToStr([ // $mod.$main
  33789. '$mod.o.DoIt();',
  33790. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  33791. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  33792. '']));
  33793. end;
  33794. procedure TTestModule.TestOverflowChecks_Int;
  33795. begin
  33796. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  33797. StartProgram(false);
  33798. Add([
  33799. 'procedure DoIt;',
  33800. 'var',
  33801. ' b: byte;',
  33802. ' n: nativeint;',
  33803. ' u: nativeuint;',
  33804. ' c: currency;',
  33805. 'begin',
  33806. ' n:=n+n;',
  33807. ' n:=n-n;',
  33808. ' n:=n+b;',
  33809. ' n:=b-n;',
  33810. ' n:=n*n;',
  33811. ' n:=n*u;',
  33812. ' c:=c+b;',
  33813. ' c:=b+c;',
  33814. ' c:=c*b;',
  33815. ' c:=b*c;',
  33816. 'end;',
  33817. 'begin',
  33818. '']);
  33819. ConvertProgram;
  33820. CheckSource('TestOverflowChecks_Int',
  33821. LinesToStr([ // statements
  33822. 'this.DoIt = function () {',
  33823. ' var b = 0;',
  33824. ' var n = 0;',
  33825. ' var u = 0;',
  33826. ' var c = 0;',
  33827. ' n = rtl.oc(n + n);',
  33828. ' n = rtl.oc(n - n);',
  33829. ' n = rtl.oc(n + b);',
  33830. ' n = rtl.oc(b - n);',
  33831. ' n = rtl.oc(n * n);',
  33832. ' n = rtl.oc(n * u);',
  33833. ' c = rtl.oc(c + (b * 10000));',
  33834. ' c = rtl.oc((b * 10000) + c);',
  33835. ' c = rtl.oc(c * b);',
  33836. ' c = rtl.oc(b * c);',
  33837. '};',
  33838. '']),
  33839. LinesToStr([ // $mod.$main
  33840. '']));
  33841. end;
  33842. procedure TTestModule.TestRangeChecks_AssignInt;
  33843. begin
  33844. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33845. StartProgram(false);
  33846. Add([
  33847. '{$R+}',
  33848. 'var',
  33849. ' b: byte = 2;',
  33850. ' w: word = 3;',
  33851. 'procedure DoIt(p: byte);',
  33852. 'begin',
  33853. ' b:=w;',
  33854. ' b+=w;',
  33855. ' b:=1;',
  33856. 'end;',
  33857. '{$R-}',
  33858. 'procedure DoSome;',
  33859. 'begin',
  33860. ' DoIt(w);',
  33861. ' b:=w;',
  33862. ' b:=2;',
  33863. 'end;',
  33864. 'begin',
  33865. '{$R+}',
  33866. '']);
  33867. ConvertProgram;
  33868. CheckSource('TestRangeChecks_AssignInt',
  33869. LinesToStr([ // statements
  33870. 'this.b = 2;',
  33871. 'this.w = 3;',
  33872. 'this.DoIt = function (p) {',
  33873. ' rtl.rc(p, 0, 255);',
  33874. ' $mod.b = rtl.rc($mod.w,0,255);',
  33875. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  33876. ' $mod.b = 1;',
  33877. '};',
  33878. 'this.DoSome = function () {',
  33879. ' $mod.DoIt($mod.w);',
  33880. ' $mod.b = $mod.w;',
  33881. ' $mod.b = 2;',
  33882. '};',
  33883. '']),
  33884. LinesToStr([ // $mod.$main
  33885. '']));
  33886. end;
  33887. procedure TTestModule.TestRangeChecks_AssignIntRange;
  33888. begin
  33889. Scanner.Options:=Scanner.Options+[po_CAssignments];
  33890. StartProgram(false);
  33891. Add([
  33892. '{$R+}',
  33893. 'type Ten = 1..10;',
  33894. 'var',
  33895. ' b: Ten = 2;',
  33896. ' w: Ten = 3;',
  33897. 'procedure DoIt(p: Ten);',
  33898. 'begin',
  33899. ' b:=w;',
  33900. ' b+=w;',
  33901. ' b:=1;',
  33902. 'end;',
  33903. '{$R-}',
  33904. 'procedure DoSome;',
  33905. 'begin',
  33906. ' DoIt(w);',
  33907. ' b:=w;',
  33908. ' b:=2;',
  33909. 'end;',
  33910. 'begin',
  33911. '{$R+}',
  33912. '']);
  33913. ConvertProgram;
  33914. CheckSource('TestRangeChecks_AssignIntRange',
  33915. LinesToStr([ // statements
  33916. 'this.b = 2;',
  33917. 'this.w = 3;',
  33918. 'this.DoIt = function (p) {',
  33919. ' rtl.rc(p, 1, 10);',
  33920. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  33921. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  33922. ' $mod.b = 1;',
  33923. '};',
  33924. 'this.DoSome = function () {',
  33925. ' $mod.DoIt($mod.w);',
  33926. ' $mod.b = $mod.w;',
  33927. ' $mod.b = 2;',
  33928. '};',
  33929. '']),
  33930. LinesToStr([ // $mod.$main
  33931. '']));
  33932. end;
  33933. procedure TTestModule.TestRangeChecks_AssignEnum;
  33934. begin
  33935. StartProgram(false);
  33936. Add([
  33937. '{$R+}',
  33938. 'type TEnum = (red,green);',
  33939. 'var',
  33940. ' e: TEnum = red;',
  33941. 'procedure DoIt(p: TEnum);',
  33942. 'begin',
  33943. ' e:=p;',
  33944. ' p:=TEnum(0);',
  33945. ' p:=succ(e);',
  33946. 'end;',
  33947. '{$R-}',
  33948. 'procedure DoSome;',
  33949. 'begin',
  33950. ' DoIt(e);',
  33951. ' e:=TEnum(1);',
  33952. ' e:=pred(e);',
  33953. 'end;',
  33954. 'begin',
  33955. '{$R+}',
  33956. '']);
  33957. ConvertProgram;
  33958. CheckSource('TestRangeChecks_AssignEnum',
  33959. LinesToStr([ // statements
  33960. 'this.TEnum = {',
  33961. ' "0": "red",',
  33962. ' red: 0,',
  33963. ' "1": "green",',
  33964. ' green: 1',
  33965. '};',
  33966. 'this.e = this.TEnum.red;',
  33967. 'this.DoIt = function (p) {',
  33968. ' rtl.rc(p, 0, 1);',
  33969. ' $mod.e = rtl.rc(p, 0, 1);',
  33970. ' p = 0;',
  33971. ' p = rtl.rc($mod.e + 1, 0, 1);',
  33972. '};',
  33973. 'this.DoSome = function () {',
  33974. ' $mod.DoIt($mod.e);',
  33975. ' $mod.e = 1;',
  33976. ' $mod.e = $mod.e - 1;',
  33977. '};',
  33978. '']),
  33979. LinesToStr([ // $mod.$main
  33980. '']));
  33981. end;
  33982. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  33983. begin
  33984. StartProgram(false);
  33985. Add([
  33986. '{$R+}',
  33987. 'type',
  33988. ' TEnum = (red,green);',
  33989. ' TEnumRg = red..green;',
  33990. 'var',
  33991. ' e: TEnumRg = red;',
  33992. 'procedure DoIt(p: TEnumRg);',
  33993. 'begin',
  33994. ' e:=p;',
  33995. ' p:=TEnumRg(0);',
  33996. ' p:=succ(e);',
  33997. 'end;',
  33998. '{$R-}',
  33999. 'procedure DoSome;',
  34000. 'begin',
  34001. ' DoIt(e);',
  34002. ' e:=TEnum(1);',
  34003. ' e:=pred(e);',
  34004. 'end;',
  34005. 'begin',
  34006. '{$R+}',
  34007. '']);
  34008. ConvertProgram;
  34009. CheckSource('TestRangeChecks_AssignEnumRange',
  34010. LinesToStr([ // statements
  34011. 'this.TEnum = {',
  34012. ' "0": "red",',
  34013. ' red: 0,',
  34014. ' "1": "green",',
  34015. ' green: 1',
  34016. '};',
  34017. 'this.e = this.TEnum.red;',
  34018. 'this.DoIt = function (p) {',
  34019. ' rtl.rc(p, 0, 1);',
  34020. ' $mod.e = rtl.rc(p, 0, 1);',
  34021. ' p = 0;',
  34022. ' p = rtl.rc($mod.e + 1, 0, 1);',
  34023. '};',
  34024. 'this.DoSome = function () {',
  34025. ' $mod.DoIt($mod.e);',
  34026. ' $mod.e = 1;',
  34027. ' $mod.e = $mod.e - 1;',
  34028. '};',
  34029. '']),
  34030. LinesToStr([ // $mod.$main
  34031. '']));
  34032. end;
  34033. procedure TTestModule.TestRangeChecks_AssignChar;
  34034. begin
  34035. StartProgram(false);
  34036. Add([
  34037. '{$R+}',
  34038. 'type',
  34039. ' TLetter = char;',
  34040. 'var',
  34041. ' b: TLetter = ''2'';',
  34042. ' w: TLetter = ''3'';',
  34043. 'procedure DoIt(p: TLetter);',
  34044. 'begin',
  34045. ' b:=w;',
  34046. ' b:=''1'';',
  34047. 'end;',
  34048. '{$R-}',
  34049. 'procedure DoSome;',
  34050. 'begin',
  34051. ' DoIt(w);',
  34052. ' b:=w;',
  34053. ' b:=''2'';',
  34054. 'end;',
  34055. 'begin',
  34056. '{$R+}',
  34057. '']);
  34058. ConvertProgram;
  34059. CheckSource('TestRangeChecks_AssignChar',
  34060. LinesToStr([ // statements
  34061. 'this.b = "2";',
  34062. 'this.w = "3";',
  34063. 'this.DoIt = function (p) {',
  34064. ' rtl.rcc(p, 0, 65535);',
  34065. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  34066. ' $mod.b = "1";',
  34067. '};',
  34068. 'this.DoSome = function () {',
  34069. ' $mod.DoIt($mod.w);',
  34070. ' $mod.b = $mod.w;',
  34071. ' $mod.b = "2";',
  34072. '};',
  34073. '']),
  34074. LinesToStr([ // $mod.$main
  34075. '']));
  34076. end;
  34077. procedure TTestModule.TestRangeChecks_AssignCharRange;
  34078. begin
  34079. StartProgram(false);
  34080. Add([
  34081. '{$R+}',
  34082. 'type TDigit = ''0''..''9'';',
  34083. 'var',
  34084. ' b: TDigit = ''2'';',
  34085. ' w: TDigit = ''3'';',
  34086. 'procedure DoIt(p: TDigit);',
  34087. 'begin',
  34088. ' b:=w;',
  34089. ' b:=''1'';',
  34090. 'end;',
  34091. '{$R-}',
  34092. 'procedure DoSome;',
  34093. 'begin',
  34094. ' DoIt(w);',
  34095. ' b:=w;',
  34096. ' b:=''2'';',
  34097. 'end;',
  34098. 'begin',
  34099. '{$R+}',
  34100. '']);
  34101. ConvertProgram;
  34102. CheckSource('TestRangeChecks_AssignCharRange',
  34103. LinesToStr([ // statements
  34104. 'this.b = "2";',
  34105. 'this.w = "3";',
  34106. 'this.DoIt = function (p) {',
  34107. ' rtl.rcc(p, 48, 57);',
  34108. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  34109. ' $mod.b = "1";',
  34110. '};',
  34111. 'this.DoSome = function () {',
  34112. ' $mod.DoIt($mod.w);',
  34113. ' $mod.b = $mod.w;',
  34114. ' $mod.b = "2";',
  34115. '};',
  34116. '']),
  34117. LinesToStr([ // $mod.$main
  34118. '']));
  34119. end;
  34120. procedure TTestModule.TestRangeChecks_ArrayIndex;
  34121. begin
  34122. StartProgram(false);
  34123. Add([
  34124. '{$R+}',
  34125. 'type',
  34126. ' Ten = 1..10;',
  34127. ' TArr = array of Ten;',
  34128. ' TArrArr = array of TArr;',
  34129. ' TArrByte = array[byte] of Ten;',
  34130. ' TArrChar = array[''0''..''9''] of Ten;',
  34131. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  34132. ' TObject = class',
  34133. ' A: TArr;',
  34134. ' end;',
  34135. 'procedure DoIt;',
  34136. 'var',
  34137. ' Arr: TArr;',
  34138. ' ArrArr: TArrArr;',
  34139. ' ArrByte: TArrByte;',
  34140. ' ArrChar: TArrChar;',
  34141. ' ArrByteChar: TArrByteChar;',
  34142. ' i: Ten;',
  34143. ' c: char;',
  34144. ' o: tobject;',
  34145. 'begin',
  34146. ' i:=Arr[1];',
  34147. ' i:=ArrByteChar[1,''2''];',
  34148. ' Arr[1]:=Arr[1];',
  34149. ' Arr[i]:=Arr[i];',
  34150. ' ArrByte[3]:=ArrByte[3];',
  34151. ' ArrByte[i]:=ArrByte[i];',
  34152. ' ArrChar[''5'']:=ArrChar[''5''];',
  34153. ' ArrChar[c]:=ArrChar[c];',
  34154. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  34155. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  34156. ' o.a[i]:=o.a[i];',
  34157. 'end;',
  34158. 'begin',
  34159. '']);
  34160. ConvertProgram;
  34161. CheckSource('TestRangeChecks_ArrayIndex',
  34162. LinesToStr([ // statements
  34163. 'this.TArrByteChar$clone = function (a) {',
  34164. ' var b = [];',
  34165. ' b.length = 256;',
  34166. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  34167. ' return b;',
  34168. '};',
  34169. 'rtl.createClass(this, "TObject", null, function () {',
  34170. ' this.$init = function () {',
  34171. ' this.A = [];',
  34172. ' };',
  34173. ' this.$final = function () {',
  34174. ' this.A = undefined;',
  34175. ' };',
  34176. '});',
  34177. 'this.DoIt = function () {',
  34178. ' var Arr = [];',
  34179. ' var ArrArr = [];',
  34180. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  34181. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  34182. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  34183. ' var i = 0;',
  34184. ' var c = "\x00";',
  34185. ' var o = null;',
  34186. ' i = rtl.rc(Arr[1], 1, 10);',
  34187. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  34188. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  34189. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  34190. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  34191. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  34192. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  34193. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  34194. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  34195. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  34196. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  34197. '};',
  34198. '']),
  34199. LinesToStr([ // $mod.$main
  34200. '']));
  34201. end;
  34202. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  34203. begin
  34204. StartProgram(false);
  34205. Add([
  34206. '{$R+}',
  34207. 'type',
  34208. ' Ten = 1..10;',
  34209. ' TRec = record x: Ten end;',
  34210. ' TArr = array of TRec;',
  34211. ' TArrArr = array of TArr;',
  34212. ' TObject = class',
  34213. ' A: TArr;',
  34214. ' end;',
  34215. 'procedure DoIt;',
  34216. 'var',
  34217. ' Arr: TArr;',
  34218. ' ArrArr: TArrArr;',
  34219. ' i: Ten;',
  34220. ' o: tobject;',
  34221. 'begin',
  34222. ' Arr[1]:=Arr[1];',
  34223. ' Arr[i]:=Arr[i+1];',
  34224. ' o.a[i]:=o.a[i+2];',
  34225. 'end;',
  34226. 'begin',
  34227. '']);
  34228. ConvertProgram;
  34229. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  34230. LinesToStr([ // statements
  34231. 'rtl.recNewT(this, "TRec", function () {',
  34232. ' this.x = 0;',
  34233. ' this.$eq = function (b) {',
  34234. ' return this.x === b.x;',
  34235. ' };',
  34236. ' this.$assign = function (s) {',
  34237. ' this.x = s.x;',
  34238. ' return this;',
  34239. ' };',
  34240. '});',
  34241. 'rtl.createClass(this, "TObject", null, function () {',
  34242. ' this.$init = function () {',
  34243. ' this.A = [];',
  34244. ' };',
  34245. ' this.$final = function () {',
  34246. ' this.A = undefined;',
  34247. ' };',
  34248. '});',
  34249. 'this.DoIt = function () {',
  34250. ' var Arr = [];',
  34251. ' var ArrArr = [];',
  34252. ' var i = 0;',
  34253. ' var o = null;',
  34254. ' Arr[1].$assign(Arr[1]);',
  34255. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  34256. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  34257. '};',
  34258. '']),
  34259. LinesToStr([ // $mod.$main
  34260. '']));
  34261. end;
  34262. procedure TTestModule.TestRangeChecks_StringIndex;
  34263. begin
  34264. StartProgram(false);
  34265. Add([
  34266. 'type',
  34267. ' TObject = class',
  34268. ' S: string;',
  34269. ' end;',
  34270. '{$R+}',
  34271. 'procedure DoIt(var h: string);',
  34272. 'var',
  34273. ' s: string;',
  34274. ' i: longint;',
  34275. ' c: char;',
  34276. ' o: tobject;',
  34277. 'begin',
  34278. ' c:=s[1];',
  34279. ' s[i]:=s[i];',
  34280. ' h[i]:=h[i];',
  34281. ' c:=o.s[i];',
  34282. ' o.s[i]:=c;',
  34283. 'end;',
  34284. 'begin',
  34285. '']);
  34286. ConvertProgram;
  34287. CheckSource('TestRangeChecks_StringIndex',
  34288. LinesToStr([ // statements
  34289. 'rtl.createClass(this, "TObject", null, function () {',
  34290. ' this.$init = function () {',
  34291. ' this.S = "";',
  34292. ' };',
  34293. ' this.$final = function () {',
  34294. ' };',
  34295. '});',
  34296. 'this.DoIt = function (h) {',
  34297. ' var s = "";',
  34298. ' var i = 0;',
  34299. ' var c = "\x00";',
  34300. ' var o = null;',
  34301. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  34302. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  34303. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  34304. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  34305. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  34306. '};',
  34307. '']),
  34308. LinesToStr([ // $mod.$main
  34309. '']));
  34310. end;
  34311. procedure TTestModule.TestRangeChecks_TypecastInt;
  34312. begin
  34313. StartProgram(false);
  34314. Add([
  34315. '{$R+}',
  34316. 'var',
  34317. ' i: nativeint;',
  34318. ' b: byte;',
  34319. ' sh: shortint;',
  34320. ' w: word;',
  34321. ' sm: smallint;',
  34322. ' lw: longword;',
  34323. ' li: longint;',
  34324. 'begin',
  34325. ' b:=12+byte(i);',
  34326. ' sh:=12+shortint(i);',
  34327. ' w:=12+word(i);',
  34328. ' sm:=12+smallint(i);',
  34329. ' lw:=12+longword(i);',
  34330. ' li:=12+longint(i);',
  34331. '']);
  34332. ConvertProgram;
  34333. CheckSource('TestRangeChecks_TypecastInt',
  34334. LinesToStr([
  34335. 'this.i = 0;',
  34336. 'this.b = 0;',
  34337. 'this.sh = 0;',
  34338. 'this.w = 0;',
  34339. 'this.sm = 0;',
  34340. 'this.lw = 0;',
  34341. 'this.li = 0;',
  34342. '']),
  34343. LinesToStr([
  34344. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  34345. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  34346. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  34347. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  34348. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  34349. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  34350. '']));
  34351. end;
  34352. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  34353. begin
  34354. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34355. StartProgram(false);
  34356. Add([
  34357. '{$modeswitch typehelpers}',
  34358. '{$R+}',
  34359. 'type',
  34360. ' TObject = class',
  34361. ' FSize: byte;',
  34362. ' property Size: byte read FSize;',
  34363. ' end;',
  34364. ' THelper = type helper for byte',
  34365. ' procedure SetIt(w: word);',
  34366. ' end;',
  34367. 'procedure THelper.SetIt(w: word);',
  34368. 'begin',
  34369. ' Self:=w;',
  34370. 'end;',
  34371. 'function GetIt: byte;',
  34372. 'begin',
  34373. ' Result.SetIt(2);',
  34374. 'end;',
  34375. 'var',
  34376. ' b: byte = 3;',
  34377. ' o: TObject;',
  34378. 'begin',
  34379. ' b.SetIt(14);',
  34380. ' with b do SetIt(15);',
  34381. ' o.Size.SetIt(16);',
  34382. '']);
  34383. ConvertProgram;
  34384. CheckSource('TestRangeChecks_AssignInt',
  34385. LinesToStr([ // statements
  34386. 'rtl.createClass(this, "TObject", null, function () {',
  34387. ' this.$init = function () {',
  34388. ' this.FSize = 0;',
  34389. ' };',
  34390. ' this.$final = function () {',
  34391. ' };',
  34392. '});',
  34393. 'rtl.createHelper(this, "THelper", null, function () {',
  34394. ' this.SetIt = function (w) {',
  34395. ' rtl.rc(w, 0, 65535);',
  34396. ' this.set(w);',
  34397. ' };',
  34398. '});',
  34399. 'this.GetIt = function () {',
  34400. ' var Result = 0;',
  34401. ' $mod.THelper.SetIt.call({',
  34402. ' get: function () {',
  34403. ' return Result;',
  34404. ' },',
  34405. ' set: function (v) {',
  34406. ' rtl.rc(v, 0, 255);',
  34407. ' Result = v;',
  34408. ' }',
  34409. ' }, 2);',
  34410. ' return Result;',
  34411. '};',
  34412. 'this.b = 3;',
  34413. 'this.o = null;',
  34414. '']),
  34415. LinesToStr([ // $mod.$main
  34416. '$mod.THelper.SetIt.call({',
  34417. ' p: $mod,',
  34418. ' get: function () {',
  34419. ' return this.p.b;',
  34420. ' },',
  34421. ' set: function (v) {',
  34422. ' rtl.rc(v, 0, 255);',
  34423. ' this.p.b = v;',
  34424. ' }',
  34425. '}, 14);',
  34426. 'var $with = $mod.b;',
  34427. '$mod.THelper.SetIt.call({',
  34428. ' get: function () {',
  34429. ' return $with;',
  34430. ' },',
  34431. ' set: function (v) {',
  34432. ' rtl.rc(v, 0, 255);',
  34433. ' $with = v;',
  34434. ' }',
  34435. '}, 15);',
  34436. '$mod.THelper.SetIt.call({',
  34437. ' p: $mod.o,',
  34438. ' get: function () {',
  34439. ' return this.p.FSize;',
  34440. ' },',
  34441. ' set: function (v) {',
  34442. ' rtl.rc(v, 0, 255);',
  34443. ' this.p.FSize = v;',
  34444. ' }',
  34445. '}, 16);',
  34446. '']));
  34447. end;
  34448. procedure TTestModule.TestRangeChecks_AssignCurrency;
  34449. begin
  34450. Scanner.Options:=Scanner.Options+[po_CAssignments];
  34451. StartProgram(false);
  34452. Add([
  34453. '{$R+}',
  34454. 'var',
  34455. ' c: currency = 2.34;',
  34456. ' i: double;',
  34457. 'procedure DoIt(p: currency);',
  34458. 'begin',
  34459. ' c:=i;',
  34460. ' c+=i;',
  34461. ' c:=1;',
  34462. 'end;',
  34463. '{$R-}',
  34464. 'procedure DoSome;',
  34465. 'begin',
  34466. ' DoIt(i);',
  34467. ' c:=i;',
  34468. ' c:=2;',
  34469. 'end;',
  34470. 'begin',
  34471. '{$R+}',
  34472. '']);
  34473. ConvertProgram;
  34474. CheckSource('TestRangeChecks_AssignCurrency',
  34475. LinesToStr([ // statements
  34476. 'this.c = 2.34;',
  34477. 'this.i = 0.0;',
  34478. 'this.DoIt = function (p) {',
  34479. ' rtl.rc(p, -922337203685477, 922337203685477);',
  34480. ' $mod.c = rtl.rc(rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34481. ' rtl.rc($mod.c += rtl.trunc($mod.i * 10000), -922337203685477, 922337203685477);',
  34482. ' $mod.c = 10000;',
  34483. '};',
  34484. 'this.DoSome = function () {',
  34485. ' $mod.DoIt($mod.i * 10000);',
  34486. ' $mod.c = rtl.trunc($mod.i * 10000);',
  34487. ' $mod.c = 20000;',
  34488. '};',
  34489. '']),
  34490. LinesToStr([ // $mod.$main
  34491. '']));
  34492. end;
  34493. procedure TTestModule.TestAsync_Proc;
  34494. begin
  34495. StartProgram(false);
  34496. Add([
  34497. 'procedure Fly(w: word = 1); async; forward;',
  34498. 'procedure Run(w: word = 2); async;',
  34499. 'begin',
  34500. ' Fly(w);',
  34501. ' Fly;',
  34502. ' await(Fly(w));',
  34503. ' await(Fly);',
  34504. 'end;',
  34505. 'procedure Fly(w: word); ',
  34506. 'begin',
  34507. 'end;',
  34508. 'begin',
  34509. ' Run;',
  34510. ' Run(3);',
  34511. '']);
  34512. CheckResolverUnexpectedHints();
  34513. ConvertProgram;
  34514. CheckSource('TestAsync_Proc',
  34515. LinesToStr([ // statements
  34516. 'this.Run = async function (w) {',
  34517. ' $mod.Fly(w);',
  34518. ' $mod.Fly(1);',
  34519. ' await $mod.Fly(w);',
  34520. ' await $mod.Fly(1);',
  34521. '};',
  34522. 'this.Fly = async function (w) {',
  34523. '};',
  34524. '']),
  34525. LinesToStr([
  34526. '$mod.Run(2);',
  34527. '$mod.Run(3);',
  34528. '']));
  34529. end;
  34530. procedure TTestModule.TestAsync_CallResultIsPromise;
  34531. begin
  34532. StartProgram(false);
  34533. Add([
  34534. '{$modeswitch externalclass}',
  34535. 'type',
  34536. ' TObject = class',
  34537. ' end;',
  34538. ' TJSPromise = class external name ''Promise''',
  34539. ' end;',
  34540. ' TBird = class',
  34541. ' function Fly: word; async; ',
  34542. ' end;',
  34543. 'function TBird.Fly: word; async; ',
  34544. 'begin',
  34545. ' Result:=3;',
  34546. ' Fly:=4+Result;',
  34547. ' if Result=5 then ;',
  34548. ' exit(6);',
  34549. 'end;',
  34550. 'function Run: word; async;',
  34551. 'begin',
  34552. ' Result:=11+Result;',
  34553. ' inc(Result);',
  34554. 'end;',
  34555. 'var',
  34556. ' p: TJSPromise;',
  34557. ' o: TBird;',
  34558. 'begin',
  34559. ' p:=Run;',
  34560. ' p:=Run();',
  34561. ' if Run=p then ;',
  34562. ' if p=Run then ;',
  34563. ' if Run()=p then ;',
  34564. ' if p=Run() then ;',
  34565. ' p:=o.Fly;',
  34566. ' p:=o.Fly();',
  34567. ' if o.Fly=p then ;',
  34568. ' if o.Fly()=p then ;',
  34569. ' with o do begin',
  34570. ' p:=Fly;',
  34571. ' p:=Fly();',
  34572. ' if Fly=p then ;',
  34573. ' if Fly()=p then ;',
  34574. ' end;',
  34575. '']);
  34576. CheckResolverUnexpectedHints();
  34577. ConvertProgram;
  34578. CheckSource('TestAsync_CallResultIsPromise',
  34579. LinesToStr([ // statements
  34580. 'rtl.createClass(this, "TObject", null, function () {',
  34581. ' this.$init = function () {',
  34582. ' };',
  34583. ' this.$final = function () {',
  34584. ' };',
  34585. '});',
  34586. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  34587. ' this.Fly = async function () {',
  34588. ' var Result = 0;',
  34589. ' Result = 3;',
  34590. ' Result = 4 + Result;',
  34591. ' if (Result === 5) ;',
  34592. ' return 6;',
  34593. ' return Result;',
  34594. ' };',
  34595. '});',
  34596. 'this.Run = async function () {',
  34597. ' var Result = 0;',
  34598. ' Result = 11 + Result;',
  34599. ' Result += 1;',
  34600. ' return Result;',
  34601. '};',
  34602. 'this.p = null;',
  34603. 'this.o = null;',
  34604. '']),
  34605. LinesToStr([
  34606. '$mod.p = $mod.Run();',
  34607. '$mod.p = $mod.Run();',
  34608. 'if ($mod.Run() === $mod.p) ;',
  34609. 'if ($mod.p === $mod.Run()) ;',
  34610. 'if ($mod.Run() === $mod.p) ;',
  34611. 'if ($mod.p === $mod.Run()) ;',
  34612. '$mod.p = $mod.o.Fly();',
  34613. '$mod.p = $mod.o.Fly();',
  34614. 'if ($mod.o.Fly() === $mod.p) ;',
  34615. 'if ($mod.o.Fly() === $mod.p) ;',
  34616. 'var $with = $mod.o;',
  34617. '$mod.p = $with.Fly();',
  34618. '$mod.p = $with.Fly();',
  34619. 'if ($with.Fly() === $mod.p) ;',
  34620. 'if ($with.Fly() === $mod.p) ;',
  34621. '']));
  34622. end;
  34623. procedure TTestModule.TestAsync_ConstructorFail;
  34624. begin
  34625. StartProgram(false);
  34626. Add([
  34627. 'type',
  34628. ' TObject = class',
  34629. ' end;',
  34630. ' TBird = class',
  34631. ' constructor Create; async;',
  34632. ' end;',
  34633. 'constructor TBird.Create; async;',
  34634. 'begin',
  34635. 'end;',
  34636. 'begin',
  34637. '']);
  34638. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  34639. ConvertProgram;
  34640. end;
  34641. procedure TTestModule.TestAsync_PropertyGetterFail;
  34642. begin
  34643. StartProgram(false);
  34644. Add([
  34645. 'type',
  34646. ' TObject = class',
  34647. ' end;',
  34648. ' TBird = class',
  34649. ' function GetSize: word; async;',
  34650. ' property Size: word read GetSize;',
  34651. ' end;',
  34652. 'function TBird.GetSize: word; async;',
  34653. 'begin',
  34654. 'end;',
  34655. 'begin',
  34656. '']);
  34657. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  34658. ConvertProgram;
  34659. end;
  34660. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  34661. begin
  34662. StartProgram(false);
  34663. Add([
  34664. 'procedure Run; async;',
  34665. 'begin',
  34666. ' await(word,1);',
  34667. 'end;',
  34668. 'begin',
  34669. '']);
  34670. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  34671. ConvertProgram;
  34672. end;
  34673. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  34674. begin
  34675. StartProgram(false);
  34676. Add([
  34677. 'type',
  34678. ' TObject = class',
  34679. ' end;',
  34680. ' TBird = class',
  34681. ' end;',
  34682. 'function Fly: TObject; async;',
  34683. 'begin',
  34684. 'end;',
  34685. 'procedure Run; async;',
  34686. 'begin',
  34687. ' await(TBird,Fly);',
  34688. 'end;',
  34689. 'begin',
  34690. '']);
  34691. SetExpectedPasResolverError('Incompatible type for arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  34692. ConvertProgram;
  34693. end;
  34694. procedure TTestModule.TestAWait_OutsideAsyncFail;
  34695. begin
  34696. StartProgram(false);
  34697. Add([
  34698. 'procedure Crawl(w: double); ',
  34699. 'begin',
  34700. 'end;',
  34701. 'procedure Run(w: double);',
  34702. 'begin',
  34703. ' await(Crawl(w));',
  34704. 'end;',
  34705. 'begin',
  34706. ' Run(1);']);
  34707. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  34708. ConvertProgram;
  34709. end;
  34710. procedure TTestModule.TestAWait_IntegerFail;
  34711. begin
  34712. StartProgram(false);
  34713. Add([
  34714. 'function Run: word;',
  34715. 'begin',
  34716. 'end;',
  34717. 'procedure Fly(w: word); async;',
  34718. 'begin',
  34719. ' await(Run());',
  34720. 'end;',
  34721. 'begin',
  34722. ' Fly(1);']);
  34723. SetExpectedPasResolverError('async function or promise expected, but Result:Word found',nXExpectedButYFound);
  34724. ConvertProgram;
  34725. end;
  34726. procedure TTestModule.TestAWait_ExternalClassPromise;
  34727. begin
  34728. StartProgram(false);
  34729. Add([
  34730. '{$modeswitch externalclass}',
  34731. 'type',
  34732. ' TJSPromise = class external name ''Promise''',
  34733. ' end;',
  34734. ' TJSThenable = class external name ''Thenable''',
  34735. ' end;',
  34736. 'function Fly(w: word): TJSPromise;',
  34737. 'begin',
  34738. 'end;',
  34739. 'function Jump(w: word): word; async;',
  34740. 'begin',
  34741. 'end;',
  34742. 'function Eat(w: word): TJSPromise; async;',
  34743. 'begin',
  34744. 'end;',
  34745. 'function Run(d: double): word; async;',
  34746. 'var',
  34747. ' p: TJSPromise;',
  34748. 'begin',
  34749. ' Result:=await(word,p);', // promise needs type
  34750. ' Result:=await(word,Fly(3));', // promise needs type
  34751. ' Result:=await(Jump(4));', // async non promise must omit the type
  34752. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  34753. ' Result:=await(word,Eat(6));', // promise needs type
  34754. 'end;',
  34755. 'begin',
  34756. '']);
  34757. ConvertProgram;
  34758. CheckSource('TestAWait_ExternalClassPromise',
  34759. LinesToStr([ // statements
  34760. 'this.Fly = function (w) {',
  34761. ' var Result = null;',
  34762. ' return Result;',
  34763. '};',
  34764. 'this.Jump = async function (w) {',
  34765. ' var Result = 0;',
  34766. ' return Result;',
  34767. '};',
  34768. 'this.Eat = async function (w) {',
  34769. ' var Result = null;',
  34770. ' return Result;',
  34771. '};',
  34772. 'this.Run = async function (d) {',
  34773. ' var Result = 0;',
  34774. ' var p = null;',
  34775. ' Result = await p;',
  34776. ' Result = await $mod.Fly(3);',
  34777. ' Result = await $mod.Jump(4);',
  34778. ' Result = await $mod.Jump(5);',
  34779. ' Result = await $mod.Eat(6);',
  34780. ' return Result;',
  34781. '};',
  34782. '']),
  34783. LinesToStr([
  34784. ]));
  34785. CheckResolverUnexpectedHints();
  34786. end;
  34787. procedure TTestModule.TestAWait_JSValue;
  34788. begin
  34789. StartProgram(false);
  34790. Add([
  34791. '{$modeswitch externalclass}',
  34792. 'type',
  34793. ' TJSPromise = class external name ''Promise''',
  34794. ' end;',
  34795. 'function Fly(w: word): jsvalue; async;',
  34796. 'begin',
  34797. 'end;',
  34798. 'function Run(d: jsvalue; var e): word; async;',
  34799. 'begin',
  34800. ' Result:=await(word,d);', // promise needs type
  34801. ' d:=await(Fly(4));', // async non promise must omit the type
  34802. ' Result:=await(word,e);', // promise needs type
  34803. 'end;',
  34804. 'begin',
  34805. '']);
  34806. ConvertProgram;
  34807. CheckSource('TestAWait_JSValue',
  34808. LinesToStr([ // statements
  34809. 'this.Fly = async function (w) {',
  34810. ' var Result = undefined;',
  34811. ' return Result;',
  34812. '};',
  34813. 'this.Run = async function (d, e) {',
  34814. ' var Result = 0;',
  34815. ' Result = await d;',
  34816. ' d = await $mod.Fly(4);',
  34817. ' Result = await e.get();',
  34818. ' return Result;',
  34819. '};',
  34820. '']),
  34821. LinesToStr([
  34822. ]));
  34823. CheckResolverUnexpectedHints();
  34824. end;
  34825. procedure TTestModule.TestAWait_Result;
  34826. begin
  34827. StartProgram(false);
  34828. Add([
  34829. '{$modeswitch externalclass}',
  34830. 'type',
  34831. ' TJSPromise = class external name ''Promise''',
  34832. ' end;',
  34833. 'function Crawl(d: double = 1.3): TJSPromise; ',
  34834. 'begin',
  34835. 'end;',
  34836. 'function Run(d: double = 1.6): word; async;',
  34837. 'begin',
  34838. ' Result:=await(word,Crawl);',
  34839. ' Result:=await(word,Crawl(4.5));',
  34840. ' Result:=await(Run);',
  34841. ' Result:=await(Run(6.7));',
  34842. 'end;',
  34843. 'begin',
  34844. ' Run(1);']);
  34845. ConvertProgram;
  34846. CheckSource('TestAWait_Result',
  34847. LinesToStr([ // statements
  34848. 'this.Crawl = function (d) {',
  34849. ' var Result = null;',
  34850. ' return Result;',
  34851. '};',
  34852. 'this.Run = async function (d) {',
  34853. ' var Result = 0;',
  34854. ' Result = await $mod.Crawl(1.3);',
  34855. ' Result = await $mod.Crawl(4.5);',
  34856. ' Result = await $mod.Run(1.6);',
  34857. ' Result = await $mod.Run(6.7);',
  34858. ' return Result;',
  34859. '};',
  34860. '']),
  34861. LinesToStr([
  34862. '$mod.Run(1);'
  34863. ]));
  34864. CheckResolverUnexpectedHints();
  34865. end;
  34866. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  34867. begin
  34868. StartProgram(false);
  34869. Add([
  34870. '{$mode objfpc}',
  34871. '{$modeswitch externalclass}',
  34872. 'type',
  34873. ' TJSPromise = class external name ''Promise''',
  34874. ' end;',
  34875. 'function Run: TJSPromise; async;',
  34876. 'begin',
  34877. 'end;',
  34878. 'procedure Fly(w: word); async;',
  34879. 'begin',
  34880. ' await(Run());',
  34881. 'end;',
  34882. 'begin',
  34883. ' Fly(1);']);
  34884. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  34885. nWrongNumberOfParametersForCallTo);
  34886. ConvertProgram;
  34887. end;
  34888. procedure TTestModule.TestAsync_AnonymousProc;
  34889. begin
  34890. StartProgram(false);
  34891. Add([
  34892. '{$mode objfpc}',
  34893. '{$modeswitch externalclass}',
  34894. 'type',
  34895. ' TJSPromise = class external name ''Promise''',
  34896. ' end;',
  34897. 'type',
  34898. ' TFunc = reference to function(x: double): word; async;',
  34899. 'function Crawl(d: double = 1.3): word; async;',
  34900. 'begin',
  34901. 'end;',
  34902. 'var Func: TFunc;',
  34903. 'begin',
  34904. ' Func:=function(c:double):word async begin',
  34905. ' Result:=await(Crawl(c));',
  34906. ' end;',
  34907. ' Func:=function(c:double):word async assembler asm',
  34908. ' end;',
  34909. '']);
  34910. ConvertProgram;
  34911. CheckSource('TestAsync_AnonymousProc',
  34912. LinesToStr([ // statements
  34913. 'this.Crawl = async function (d) {',
  34914. ' var Result = 0;',
  34915. ' return Result;',
  34916. '};',
  34917. 'this.Func = null;',
  34918. '']),
  34919. LinesToStr([
  34920. '$mod.Func = async function (c) {',
  34921. ' var Result = 0;',
  34922. ' Result = await $mod.Crawl(c);',
  34923. ' return Result;',
  34924. '};',
  34925. '$mod.Func = async function (c) {',
  34926. '};',
  34927. '']));
  34928. CheckResolverUnexpectedHints();
  34929. end;
  34930. procedure TTestModule.TestAsync_AnonymousProc_PromiseViaDotContext;
  34931. begin
  34932. StartProgram(false);
  34933. Add([
  34934. '{$mode objfpc}',
  34935. '{$modeswitch externalclass}',
  34936. 'type',
  34937. ' TJSPromise = class external name ''Promise''',
  34938. ' end;',
  34939. ' TObject = class',
  34940. ' public',
  34941. ' procedure Fly(Prom: TJSPromise);',
  34942. ' end;',
  34943. ' TFunc = reference to procedure(Bird: TObject);',
  34944. 'procedure TObject.Fly(Prom: TJSPromise);',
  34945. 'begin',
  34946. 'end;',
  34947. 'function Crawl: jsvalue; async;',
  34948. 'begin',
  34949. 'end;',
  34950. 'procedure Add(Func: TFunc);',
  34951. 'begin',
  34952. 'end;',
  34953. 'begin',
  34954. ' Add(procedure(Bird: TObject)',
  34955. ' begin',
  34956. ' Bird.Fly(Crawl());',
  34957. ' end);',
  34958. '']);
  34959. ConvertProgram;
  34960. CheckSource('TestAsync_AnonymousProc_PromiseViaDotContext',
  34961. LinesToStr([ // statements
  34962. 'rtl.createClass(this, "TObject", null, function () {',
  34963. ' this.$init = function () {',
  34964. ' };',
  34965. ' this.$final = function () {',
  34966. ' };',
  34967. ' this.Fly = function (Prom) {',
  34968. ' };',
  34969. '});',
  34970. 'this.Crawl = async function () {',
  34971. ' var Result = undefined;',
  34972. ' return Result;',
  34973. '};',
  34974. 'this.Add = function (Func) {',
  34975. '};',
  34976. '']),
  34977. LinesToStr([
  34978. '$mod.Add(function (Bird) {',
  34979. ' Bird.Fly($mod.Crawl());',
  34980. '});',
  34981. '']));
  34982. end;
  34983. procedure TTestModule.TestAsync_ProcType;
  34984. begin
  34985. StartProgram(false);
  34986. Add([
  34987. '{$mode objfpc}',
  34988. 'type',
  34989. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  34990. ' TFunc = function(x: double = 1.1): word; async;',
  34991. ' TProc = procedure(x: longint = 7); async;',
  34992. 'function Crawl(d: double): word; async;',
  34993. 'begin',
  34994. 'end;',
  34995. 'procedure Run(e:longint); async;',
  34996. 'begin',
  34997. 'end;',
  34998. 'procedure Fly(p: TProc); async;',
  34999. 'begin',
  35000. ' await(p);',
  35001. ' await(p());',
  35002. 'end;',
  35003. 'var',
  35004. ' RefFunc: TRefFunc;',
  35005. ' Func: TFunc;',
  35006. ' Proc, ProcB: TProc;',
  35007. 'begin',
  35008. ' Func:=@Crawl;',
  35009. ' RefFunc:=@Crawl;',
  35010. ' RefFunc:=function(c:double):word async begin',
  35011. ' Result:=await(RefFunc);',
  35012. ' Result:=await(RefFunc());',
  35013. ' Result:=await(Func);',
  35014. ' Result:=await(Func());',
  35015. ' await(Proc);',
  35016. ' await(Proc());',
  35017. ' await(Proc(13));',
  35018. ' end;',
  35019. ' Proc:=@Run;',
  35020. ' if Proc=ProcB then ;',
  35021. ' ']);
  35022. ConvertProgram;
  35023. CheckResolverUnexpectedHints();
  35024. CheckSource('TestAsync_ProcType',
  35025. LinesToStr([ // statements
  35026. 'this.Crawl = async function (d) {',
  35027. ' var Result = 0;',
  35028. ' return Result;',
  35029. '};',
  35030. 'this.Run = async function (e) {',
  35031. '};',
  35032. 'this.Fly = async function (p) {',
  35033. ' await p(7);',
  35034. ' await p(7);',
  35035. '};',
  35036. 'this.RefFunc = null;',
  35037. 'this.Func = null;',
  35038. 'this.Proc = null;',
  35039. 'this.ProcB = null;',
  35040. '']),
  35041. LinesToStr([
  35042. '$mod.Func = $mod.Crawl;',
  35043. '$mod.RefFunc = $mod.Crawl;',
  35044. '$mod.RefFunc = async function (c) {',
  35045. ' var Result = 0;',
  35046. ' Result = await $mod.RefFunc(1.3);',
  35047. ' Result = await $mod.RefFunc(1.3);',
  35048. ' Result = await $mod.Func(1.1);',
  35049. ' Result = await $mod.Func(1.1);',
  35050. ' await $mod.Proc(7);',
  35051. ' await $mod.Proc(7);',
  35052. ' await $mod.Proc(13);',
  35053. ' return Result;',
  35054. '};',
  35055. '$mod.Proc = $mod.Run;',
  35056. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  35057. '']));
  35058. end;
  35059. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  35060. begin
  35061. StartProgram(false);
  35062. Add([
  35063. '{$mode objfpc}',
  35064. 'type',
  35065. ' TRefFunc = reference to function(x: double = 1.3): word;',
  35066. 'function Crawl(d: double): word; async;',
  35067. 'begin',
  35068. 'end;',
  35069. 'var',
  35070. ' RefFunc: TRefFunc;',
  35071. 'begin',
  35072. ' RefFunc:=@Crawl;',
  35073. ' ']);
  35074. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  35075. ConvertProgram;
  35076. end;
  35077. procedure TTestModule.TestAsync_Inherited;
  35078. begin
  35079. StartProgram(false);
  35080. Add([
  35081. '{$mode objfpc}',
  35082. '{$modeswitch externalclass}',
  35083. 'type',
  35084. ' TJSPromise = class external name ''Promise''',
  35085. ' end;',
  35086. ' TObject = class',
  35087. ' function Run(w: word = 3): word; async; virtual;',
  35088. ' end;',
  35089. ' TBird = class',
  35090. ' function Run(w: word = 3): word; async; override;',
  35091. ' end;',
  35092. 'function TObject.Run(w: word = 3): word; async;',
  35093. 'begin',
  35094. 'end;',
  35095. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  35096. 'var p: TJSPromise;',
  35097. 'begin',
  35098. ' p:=inherited;',
  35099. ' p:=inherited Run;',
  35100. ' p:=inherited Run();',
  35101. ' p:=inherited Run(4);',
  35102. ' exit(p);',
  35103. ' exit(inherited);',
  35104. ' exit(inherited Run);',
  35105. ' exit(inherited Run(5));',
  35106. ' exit(6);',
  35107. 'end;',
  35108. 'begin',
  35109. ' ']);
  35110. ConvertProgram;
  35111. CheckSource('TestAsync_Inherited',
  35112. LinesToStr([ // statements
  35113. 'rtl.createClass(this, "TObject", null, function () {',
  35114. ' this.$init = function () {',
  35115. ' };',
  35116. ' this.$final = function () {',
  35117. ' };',
  35118. ' this.Run = async function (w) {',
  35119. ' var Result = 0;',
  35120. ' return Result;',
  35121. ' };',
  35122. '});',
  35123. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35124. ' this.Run = async function (w) {',
  35125. ' var Result = 0;',
  35126. ' var p = null;',
  35127. ' p = $mod.TObject.Run.apply(this, arguments);',
  35128. ' p = $mod.TObject.Run.call(this, 3);',
  35129. ' p = $mod.TObject.Run.call(this, 3);',
  35130. ' p = $mod.TObject.Run.call(this, 4);',
  35131. ' return p;',
  35132. ' return $mod.TObject.Run.apply(this, arguments);',
  35133. ' return $mod.TObject.Run.call(this, 3);',
  35134. ' return $mod.TObject.Run.call(this, 5);',
  35135. ' return 6;',
  35136. ' return Result;',
  35137. ' };',
  35138. '});',
  35139. '']),
  35140. LinesToStr([
  35141. '']));
  35142. CheckResolverUnexpectedHints();
  35143. end;
  35144. procedure TTestModule.TestAsync_ClassInterface;
  35145. begin
  35146. StartProgram(false);
  35147. Add([
  35148. '{$mode objfpc}',
  35149. '{$modeswitch externalclass}',
  35150. 'type',
  35151. ' TJSPromise = class external name ''Promise''',
  35152. ' end;',
  35153. ' IUnknown = interface',
  35154. ' function _AddRef: longint;',
  35155. ' function _Release: longint;',
  35156. ' end;',
  35157. 'function Say(i: IUnknown): IUnknown; async;',
  35158. 'begin',
  35159. 'end;',
  35160. 'function Run: IUnknown; async;',
  35161. 'begin',
  35162. ' Result:=await(Run);',
  35163. ' Result:=await(Run());',
  35164. ' Result:=await(Run) as IUnknown;',
  35165. ' Result:=await(Say(nil));',
  35166. ' Result:=await(Say(await(Run())));',
  35167. ' Result:=await(Say(await(Run()) as IUnknown));',
  35168. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  35169. 'end;',
  35170. 'procedure Fly;',
  35171. 'var p: TJSPromise;',
  35172. 'begin',
  35173. ' Run;',
  35174. ' Run();',
  35175. ' p:=Run;',
  35176. ' p:=Run();',
  35177. 'end;',
  35178. 'begin',
  35179. ' ']);
  35180. ConvertProgram;
  35181. CheckSource('TestAsync_ClassInterface',
  35182. LinesToStr([ // statements
  35183. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  35184. 'this.Say = async function (i) {',
  35185. ' var Result = null;',
  35186. ' return Result;',
  35187. '};',
  35188. 'this.Run = async function () {',
  35189. ' var Result = null;',
  35190. ' var $ok = false;',
  35191. ' try {',
  35192. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  35193. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  35194. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  35195. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  35196. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  35197. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  35198. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  35199. ' $ok = true;',
  35200. ' } finally {',
  35201. ' if (!$ok) rtl._Release(Result);',
  35202. ' };',
  35203. ' return Result;',
  35204. '};',
  35205. 'this.Fly = function () {',
  35206. ' var p = null;',
  35207. ' $mod.Run();',
  35208. ' $mod.Run();',
  35209. ' p = $mod.Run();',
  35210. ' p = $mod.Run();',
  35211. '};',
  35212. '']),
  35213. LinesToStr([
  35214. '']));
  35215. CheckResolverUnexpectedHints();
  35216. end;
  35217. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  35218. begin
  35219. StartProgram(true,[supTInterfacedObject]);
  35220. Add([
  35221. '{$mode objfpc}',
  35222. '{$modeswitch externalclass}',
  35223. 'type',
  35224. ' TJSPromise = class external name ''Promise''',
  35225. ' end;',
  35226. ' IBird = interface',
  35227. ' procedure Run;',
  35228. ' end;',
  35229. ' TBird = class(TInterfacedObject,IBird)',
  35230. ' procedure Run; async;',
  35231. ' end;',
  35232. 'procedure TBird.Run;',
  35233. 'begin',
  35234. 'end;',
  35235. 'begin',
  35236. ' ']);
  35237. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  35238. ConvertProgram;
  35239. end;
  35240. procedure TTestModule.TestAWait_ClassAs;
  35241. begin
  35242. StartProgram(false);
  35243. Add([
  35244. '{$mode objfpc}',
  35245. '{$modeswitch externalclass}',
  35246. 'type',
  35247. ' TJSPromise = class external name ''Promise''',
  35248. ' end;',
  35249. ' TObject = class',
  35250. ' function Run: TObject; async;',
  35251. ' end;',
  35252. ' TBird = class',
  35253. ' function Fly: TBird; async;',
  35254. ' end;',
  35255. 'function TObject.Run: TObject; async;',
  35256. 'begin',
  35257. 'end;',
  35258. 'function TBird.Fly: TBird;', // async modifier not needed in impl
  35259. 'var o: TObject;',
  35260. 'begin',
  35261. ' o:=await(TObject,Run);',
  35262. ' o:=await(TObject,Fly);',
  35263. ' o:=await(TBird,Fly);',
  35264. ' o:=await(TObject,inherited Run);',
  35265. ' o:=await(TObject,inherited Run) as TBird;',
  35266. 'end;',
  35267. 'begin',
  35268. ' ']);
  35269. ConvertProgram;
  35270. CheckSource('TestAWait_ClassAs',
  35271. LinesToStr([ // statements
  35272. 'rtl.createClass(this, "TObject", null, function () {',
  35273. ' this.$init = function () {',
  35274. ' };',
  35275. ' this.$final = function () {',
  35276. ' };',
  35277. ' this.Run = async function () {',
  35278. ' var Result = null;',
  35279. ' return Result;',
  35280. ' };',
  35281. '});',
  35282. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  35283. ' this.Fly = async function () {',
  35284. ' var Result = null;',
  35285. ' var o = null;',
  35286. ' o = await this.Run();',
  35287. ' o = await this.Fly();',
  35288. ' o = await this.Fly();',
  35289. ' o = await $mod.TObject.Run.call(this);',
  35290. ' o = rtl.as(await $mod.TObject.Run.call(this), $mod.TBird);',
  35291. ' return Result;',
  35292. ' };',
  35293. '});',
  35294. '']),
  35295. LinesToStr([
  35296. '']));
  35297. CheckResolverUnexpectedHints();
  35298. end;
  35299. procedure TTestModule.TestLibrary_Empty;
  35300. begin
  35301. StartLibrary(false);
  35302. Add([
  35303. '']);
  35304. ConvertLibrary;
  35305. CheckFullSource('TestLibrary_Empty',
  35306. LinesToStr([ // statements
  35307. 'rtl.module("library", [], function () {',
  35308. ' var $mod = this;',
  35309. ' $mod.$main = function () {',
  35310. ' };',
  35311. '});',
  35312. 'rtl.run("library");',
  35313. '']));
  35314. CheckResolverUnexpectedHints();
  35315. end;
  35316. procedure TTestModule.TestLibrary_ExportFunc;
  35317. begin
  35318. StartLibrary(false);
  35319. Add([
  35320. 'procedure Run(w: word);',
  35321. 'begin',
  35322. 'end;',
  35323. 'exports',
  35324. ' Run;',
  35325. ' run name ''Foo'';',
  35326. ' test1.run name ''Test1Run'';',
  35327. '']);
  35328. ConvertLibrary;
  35329. CheckFullSource('TestLibrary_ExportFunc',
  35330. LinesToStr([ // statements
  35331. 'rtl.module("library", [], function () {',
  35332. ' var $mod = this;',
  35333. ' this.Run = function (w) {',
  35334. ' };',
  35335. ' $mod.$main = function () {',
  35336. ' };',
  35337. '});',
  35338. 'rtl.run("library");',
  35339. 'export const Run = pas.library.Run;',
  35340. 'export const Foo = pas.library.Run;',
  35341. 'export const Test1Run = pas.library.Run;',
  35342. '']));
  35343. CheckResolverUnexpectedHints();
  35344. end;
  35345. procedure TTestModule.TestLibrary_ExportFuncOverloadedFail;
  35346. begin
  35347. StartLibrary(false);
  35348. Add([
  35349. 'procedure Run(w: word); overload;',
  35350. 'begin',
  35351. 'end;',
  35352. 'procedure Run(s: string); overload;',
  35353. 'begin',
  35354. 'end;',
  35355. 'exports',
  35356. ' Run;',
  35357. '']);
  35358. SetExpectedPasResolverError(sCantDetermineWhichOverloadedFunctionToCall,
  35359. nCantDetermineWhichOverloadedFunctionToCall);
  35360. ConvertLibrary;
  35361. end;
  35362. procedure TTestModule.TestLibrary_Export_Index_Fail;
  35363. begin
  35364. StartLibrary(false);
  35365. Add([
  35366. 'procedure Run(w: word);',
  35367. 'begin',
  35368. 'end;',
  35369. 'exports',
  35370. ' Run index 3;',
  35371. '']);
  35372. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  35373. ConvertLibrary;
  35374. end;
  35375. procedure TTestModule.TestLibrary_ExportVar;
  35376. begin
  35377. StartLibrary(false);
  35378. Add([
  35379. 'var Wing: word;',
  35380. 'exports',
  35381. ' Wing, wing name ''BirdArm'';',
  35382. '']);
  35383. ConvertLibrary;
  35384. CheckFullSource('TestLibrary_ExportVar',
  35385. LinesToStr([ // statements
  35386. 'rtl.module("library", [], function () {',
  35387. ' var $mod = this;',
  35388. ' this.Wing = 0;',
  35389. ' $mod.$main = function () {',
  35390. ' };',
  35391. '});',
  35392. 'rtl.run("library");',
  35393. 'export const vars = {};',
  35394. 'Object.defineProperties(vars, {',
  35395. ' Wing: {',
  35396. ' enumerable: true,',
  35397. ' get: function () {',
  35398. ' return pas.library.Wing;',
  35399. ' },',
  35400. ' set: function (v) {',
  35401. ' pas.library.Wing = v;',
  35402. ' }',
  35403. ' },',
  35404. ' BirdArm: {',
  35405. ' enumerable: true,',
  35406. ' get: function () {',
  35407. ' return pas.library.Wing;',
  35408. ' },',
  35409. ' set: function (v) {',
  35410. ' pas.library.Wing = v;',
  35411. ' }',
  35412. ' }',
  35413. '});',
  35414. '']));
  35415. CheckResolverUnexpectedHints();
  35416. end;
  35417. procedure TTestModule.TestLibrary_ExportUnitFunc;
  35418. begin
  35419. AddModuleWithIntfImplSrc('Unit1.pas',
  35420. LinesToStr([
  35421. 'type',
  35422. ' TAnt = class',
  35423. ' class function Crawl: word; static;',
  35424. ' end;',
  35425. 'function Fly: word;',
  35426. '']),
  35427. LinesToStr([
  35428. 'function Fly: word;',
  35429. 'begin',
  35430. 'end;',
  35431. 'class function TAnt.Crawl: word;',
  35432. 'begin',
  35433. 'end;',
  35434. '']));
  35435. StartLibrary(true,[supTObject]);
  35436. Add([
  35437. 'uses unit1;',
  35438. 'exports',
  35439. ' Fly;',
  35440. ' TAnt.Crawl;',
  35441. '']);
  35442. ConvertLibrary;
  35443. CheckFullSource('TestLibrary_ExportUnitFunc',
  35444. LinesToStr([ // statements
  35445. 'rtl.module("library", ["system", "Unit1"], function () {',
  35446. ' var $mod = this;',
  35447. ' $mod.$main = function () {',
  35448. ' };',
  35449. '});',
  35450. 'rtl.run("library");',
  35451. 'export const Fly = pas.Unit1.Fly;',
  35452. 'export const Crawl = pas.Unit1.TAnt.Crawl;',
  35453. '']));
  35454. CheckResolverUnexpectedHints();
  35455. end;
  35456. Initialization
  35457. RegisterTests([TTestModule]);
  35458. end.